xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_bufmgr.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef D3D12_BUFMGR_H
25 #define D3D12_BUFMGR_H
26 
27 #include "pipebuffer/pb_buffer.h"
28 #include "util/u_atomic.h"
29 #include "util/list.h"
30 
31 #include "d3d12_common.h"
32 #include "d3d12_resource_state.h"
33 
34 struct d3d12_bufmgr;
35 struct d3d12_screen;
36 struct pb_manager;
37 
38 enum d3d12_residency_status {
39    d3d12_evicted,
40    d3d12_resident,
41    d3d12_permanently_resident,
42 };
43 
44 enum batch_bo_reference_state {
45    batch_bo_reference_none = 0,
46    batch_bo_reference_read = (1 << 0),
47    batch_bo_reference_written = (1 << 1),
48 };
49 
50 struct d3d12_bo {
51    struct pipe_reference reference;
52    struct d3d12_screen *screen;
53    ID3D12Resource *res;
54    struct pb_buffer *buffer;
55    struct d3d12_resource_state global_state;
56 
57    /* Used as a key in per-context resource state maps,
58     * to avoid needing to lock them for single-threaded lookups to
59     * protect against resource destruction.
60     */
61    uint64_t unique_id;
62 
63    struct list_head residency_list_entry;
64    uint64_t estimated_size;
65    int64_t last_used_timestamp;
66    uint64_t last_used_fence;
67    enum d3d12_residency_status residency_status;
68    uint16_t local_needs_resolve_state;
69 
70    unsigned local_context_state_mask;
71    uint8_t local_reference_mask[16];
72 
73    d3d12_context_state_table_entry local_context_states[16];
74    uint8_t local_reference_state[16][8];
75 };
76 
77 struct d3d12_buffer {
78    struct pb_buffer base;
79 
80    struct d3d12_bo *bo;
81    D3D12_RANGE range;
82    void *map;
83 };
84 
85 static inline struct d3d12_buffer *
d3d12_buffer(struct pb_buffer * buf)86 d3d12_buffer(struct pb_buffer *buf)
87 {
88    assert(buf);
89    return (struct d3d12_buffer *)buf;
90 }
91 
92 static inline struct d3d12_bo *
d3d12_bo_get_base(struct d3d12_bo * bo,uint64_t * offset)93 d3d12_bo_get_base(struct d3d12_bo *bo, uint64_t *offset)
94 {
95    if (bo->buffer) {
96       struct pb_buffer *base_buffer;
97       pb_get_base_buffer(bo->buffer, &base_buffer, offset);
98       return d3d12_buffer(base_buffer)->bo;
99    } else {
100       *offset = 0;
101       return bo;
102    }
103 }
104 
105 static inline uint64_t
d3d12_bo_get_size(struct d3d12_bo * bo)106 d3d12_bo_get_size(struct d3d12_bo *bo)
107 {
108    if (bo->buffer)
109       return bo->buffer->base.size;
110    else
111       return GetDesc(bo->res).Width;
112 }
113 
114 static inline bool
d3d12_bo_is_suballocated(struct d3d12_bo * bo)115 d3d12_bo_is_suballocated(struct d3d12_bo *bo)
116 {
117    struct d3d12_bo *base_bo;
118    uint64_t offset;
119 
120    if (!bo->buffer)
121       return false;
122 
123    base_bo = d3d12_bo_get_base(bo, &offset);
124    return d3d12_bo_get_size(base_bo) != d3d12_bo_get_size(bo);
125 }
126 
127 struct d3d12_bo *
128 d3d12_bo_new(struct d3d12_screen *screen, uint64_t size, uint64_t alignment);
129 
130 struct d3d12_bo *
131 d3d12_bo_wrap_res(struct d3d12_screen *screen, ID3D12Resource *res, enum d3d12_residency_status residency);
132 
133 struct d3d12_bo *
134 d3d12_bo_wrap_buffer(struct d3d12_screen *screen, struct pb_buffer *buf);
135 
136 void
137 d3d12_debug_describe_bo(char* buf, struct d3d12_bo* ptr);
138 
139 static inline void
d3d12_bo_reference(struct d3d12_bo * bo)140 d3d12_bo_reference(struct d3d12_bo *bo)
141 {
142    pipe_reference_described(NULL, &bo->reference,
143                             (debug_reference_descriptor)
144                             d3d12_debug_describe_bo);
145 }
146 
147 void
148 d3d12_bo_unreference(struct d3d12_bo *bo);
149 
150 void *
151 d3d12_bo_map(struct d3d12_bo *bo, D3D12_RANGE *range);
152 
153 void
154 d3d12_bo_unmap(struct d3d12_bo *bo, D3D12_RANGE *range);
155 
156 struct pb_manager *
157 d3d12_bufmgr_create(struct d3d12_screen *screen);
158 
159 #endif
160