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 #include "d3d12_batch.h"
25 #include "d3d12_context.h"
26 #include "d3d12_fence.h"
27 #include "d3d12_query.h"
28 #include "d3d12_residency.h"
29 #include "d3d12_resource.h"
30 #include "d3d12_resource_state.h"
31 #include "d3d12_screen.h"
32 #include "d3d12_surface.h"
33
34 #include "util/hash_table.h"
35 #include "util/set.h"
36 #include "util/u_inlines.h"
37
38 #include <dxguids/dxguids.h>
39
40 bool
d3d12_init_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)41 d3d12_init_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
42 {
43 struct d3d12_screen *screen = d3d12_screen(ctx->base.screen);
44
45 batch->bos = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
46 _mesa_key_pointer_equal);
47 batch->sampler_views = _mesa_set_create(NULL, _mesa_hash_pointer,
48 _mesa_key_pointer_equal);
49 batch->surfaces = _mesa_set_create(NULL, _mesa_hash_pointer,
50 _mesa_key_pointer_equal);
51 batch->objects = _mesa_set_create(NULL,
52 _mesa_hash_pointer,
53 _mesa_key_pointer_equal);
54
55 if (!batch->bos || !batch->sampler_views || !batch->surfaces || !batch->objects)
56 return false;
57
58 util_dynarray_init(&batch->zombie_samplers, NULL);
59
60 if (FAILED(screen->dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
61 IID_PPV_ARGS(&batch->cmdalloc))))
62 return false;
63
64
65 batch->sampler_heap =
66 d3d12_descriptor_heap_new(screen->dev,
67 D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
68 D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
69 128);
70
71 batch->view_heap =
72 d3d12_descriptor_heap_new(screen->dev,
73 D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
74 D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
75 1024);
76
77 if (!batch->sampler_heap && !batch->view_heap)
78 return false;
79
80 return true;
81 }
82
83 static void
delete_bo(hash_entry * entry)84 delete_bo(hash_entry *entry)
85 {
86 struct d3d12_bo *bo = (struct d3d12_bo *)entry->key;
87 d3d12_bo_unreference(bo);
88 }
89
90 static void
delete_sampler_view(set_entry * entry)91 delete_sampler_view(set_entry *entry)
92 {
93 struct pipe_sampler_view *pres = (struct pipe_sampler_view *)entry->key;
94 pipe_sampler_view_reference(&pres, NULL);
95 }
96
97 static void
delete_surface(set_entry * entry)98 delete_surface(set_entry *entry)
99 {
100 struct pipe_surface *surf = (struct pipe_surface *)entry->key;
101 pipe_surface_reference(&surf, NULL);
102 }
103
104 static void
delete_object(set_entry * entry)105 delete_object(set_entry *entry)
106 {
107 ID3D12Object *object = (ID3D12Object *)entry->key;
108 object->Release();
109 }
110
111 bool
d3d12_reset_batch(struct d3d12_context * ctx,struct d3d12_batch * batch,uint64_t timeout_ns)112 d3d12_reset_batch(struct d3d12_context *ctx, struct d3d12_batch *batch, uint64_t timeout_ns)
113 {
114 // batch hasn't been submitted before
115 if (!batch->fence && !batch->has_errors)
116 return true;
117
118 if (batch->fence) {
119 if (!d3d12_fence_finish(batch->fence, timeout_ns))
120 return false;
121 d3d12_fence_reference(&batch->fence, NULL);
122 }
123
124 _mesa_hash_table_clear(batch->bos, delete_bo);
125 _mesa_set_clear(batch->sampler_views, delete_sampler_view);
126 _mesa_set_clear(batch->surfaces, delete_surface);
127 _mesa_set_clear(batch->objects, delete_object);
128
129 util_dynarray_foreach(&batch->zombie_samplers, d3d12_descriptor_handle, handle)
130 d3d12_descriptor_handle_free(handle);
131 util_dynarray_clear(&batch->zombie_samplers);
132
133 d3d12_descriptor_heap_clear(batch->view_heap);
134 d3d12_descriptor_heap_clear(batch->sampler_heap);
135
136 if (FAILED(batch->cmdalloc->Reset())) {
137 debug_printf("D3D12: resetting ID3D12CommandAllocator failed\n");
138 return false;
139 }
140 batch->has_errors = false;
141 batch->pending_memory_barrier = false;
142 return true;
143 }
144
145 void
d3d12_destroy_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)146 d3d12_destroy_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
147 {
148 d3d12_reset_batch(ctx, batch, PIPE_TIMEOUT_INFINITE);
149 batch->cmdalloc->Release();
150 d3d12_descriptor_heap_free(batch->sampler_heap);
151 d3d12_descriptor_heap_free(batch->view_heap);
152 _mesa_hash_table_destroy(batch->bos, NULL);
153 _mesa_set_destroy(batch->sampler_views, NULL);
154 _mesa_set_destroy(batch->surfaces, NULL);
155 _mesa_set_destroy(batch->objects, NULL);
156 util_dynarray_fini(&batch->zombie_samplers);
157 }
158
159 void
d3d12_start_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)160 d3d12_start_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
161 {
162 struct d3d12_screen *screen = d3d12_screen(ctx->base.screen);
163 ID3D12DescriptorHeap* heaps[2] = { d3d12_descriptor_heap_get(batch->view_heap),
164 d3d12_descriptor_heap_get(batch->sampler_heap) };
165
166 d3d12_reset_batch(ctx, batch, PIPE_TIMEOUT_INFINITE);
167
168 /* Create or reset global command list */
169 if (ctx->cmdlist) {
170 if (FAILED(ctx->cmdlist->Reset(batch->cmdalloc, NULL))) {
171 debug_printf("D3D12: resetting ID3D12GraphicsCommandList failed\n");
172 batch->has_errors = true;
173 return;
174 }
175 } else {
176 if (FAILED(screen->dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT,
177 batch->cmdalloc, NULL,
178 IID_PPV_ARGS(&ctx->cmdlist)))) {
179 debug_printf("D3D12: creating ID3D12GraphicsCommandList failed\n");
180 batch->has_errors = true;
181 return;
182 }
183 }
184
185 ctx->cmdlist->SetDescriptorHeaps(2, heaps);
186 ctx->cmdlist_dirty = ~0;
187 for (int i = 0; i < PIPE_SHADER_TYPES; ++i)
188 ctx->shader_dirty[i] = ~0;
189
190 if (!ctx->queries_disabled)
191 d3d12_resume_queries(ctx);
192 if (ctx->current_predication)
193 d3d12_enable_predication(ctx);
194
195 batch->submit_id = ++ctx->submit_id;
196 }
197
198 void
d3d12_end_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)199 d3d12_end_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
200 {
201 struct d3d12_screen *screen = d3d12_screen(ctx->base.screen);
202
203 if (!ctx->queries_disabled)
204 d3d12_suspend_queries(ctx);
205
206 if (FAILED(ctx->cmdlist->Close())) {
207 debug_printf("D3D12: closing ID3D12GraphicsCommandList failed\n");
208 batch->has_errors = true;
209 return;
210 }
211
212 mtx_lock(&screen->submit_mutex);
213
214 d3d12_process_batch_residency(screen, batch);
215
216 bool has_state_fixup = d3d12_context_state_resolve_submission(ctx, batch);
217
218 ID3D12CommandList *cmdlists[] = { ctx->state_fixup_cmdlist, ctx->cmdlist };
219 ID3D12CommandList **to_execute = cmdlists;
220 UINT count_to_execute = ARRAY_SIZE(cmdlists);
221 if (!has_state_fixup) {
222 to_execute++;
223 count_to_execute--;
224 }
225 screen->cmdqueue->ExecuteCommandLists(count_to_execute, to_execute);
226 batch->fence = d3d12_create_fence(screen);
227
228 mtx_unlock(&screen->submit_mutex);
229 }
230
231 enum batch_bo_reference_state
232 {
233 batch_bo_reference_read = (1 << 0),
234 batch_bo_reference_written = (1 << 1),
235 };
236
237 bool
d3d12_batch_has_references(struct d3d12_batch * batch,struct d3d12_bo * bo,bool want_to_write)238 d3d12_batch_has_references(struct d3d12_batch *batch,
239 struct d3d12_bo *bo,
240 bool want_to_write)
241 {
242 hash_entry *entry = _mesa_hash_table_search(batch->bos, bo);
243 if (entry == NULL)
244 return false;
245 bool resource_was_written = ((batch_bo_reference_state)(size_t)entry->data & batch_bo_reference_written) != 0;
246 return want_to_write || resource_was_written;
247 }
248
249 void
d3d12_batch_reference_resource(struct d3d12_batch * batch,struct d3d12_resource * res,bool write)250 d3d12_batch_reference_resource(struct d3d12_batch *batch,
251 struct d3d12_resource *res,
252 bool write)
253 {
254 hash_entry *entry = _mesa_hash_table_search(batch->bos, res->bo);
255 if (entry == NULL) {
256 d3d12_bo_reference(res->bo);
257 entry = _mesa_hash_table_insert(batch->bos, res->bo, NULL);
258 }
259 size_t new_data = write ? batch_bo_reference_written : batch_bo_reference_read;
260 size_t old_data = (size_t)entry->data;
261 entry->data = (void*)(old_data | new_data);
262 }
263
264 void
d3d12_batch_reference_sampler_view(struct d3d12_batch * batch,struct d3d12_sampler_view * sv)265 d3d12_batch_reference_sampler_view(struct d3d12_batch *batch,
266 struct d3d12_sampler_view *sv)
267 {
268 struct set_entry *entry = _mesa_set_search(batch->sampler_views, sv);
269 if (!entry) {
270 entry = _mesa_set_add(batch->sampler_views, sv);
271 pipe_reference(NULL, &sv->base.reference);
272
273 d3d12_batch_reference_resource(batch, d3d12_resource(sv->base.texture), false);
274 }
275 }
276
277 void
d3d12_batch_reference_surface_texture(struct d3d12_batch * batch,struct d3d12_surface * surf)278 d3d12_batch_reference_surface_texture(struct d3d12_batch *batch,
279 struct d3d12_surface *surf)
280 {
281 d3d12_batch_reference_resource(batch, d3d12_resource(surf->base.texture), true);
282 }
283
284 void
d3d12_batch_reference_object(struct d3d12_batch * batch,ID3D12Object * object)285 d3d12_batch_reference_object(struct d3d12_batch *batch,
286 ID3D12Object *object)
287 {
288 struct set_entry *entry = _mesa_set_search(batch->objects, object);
289 if (!entry) {
290 entry = _mesa_set_add(batch->objects, object);
291 object->AddRef();
292 }
293 }
294