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
d3d12_sampler_desc_table_key_hash(const void * key)41 unsigned d3d12_sampler_desc_table_key_hash(const void* key)
42 {
43 const d3d12_sampler_desc_table_key* table = (d3d12_sampler_desc_table_key*)key;
44
45 return _mesa_hash_data(table->descs, sizeof(table->descs[0]) * table->count);
46 }
d3d12_sampler_desc_table_key_equals(const void * a,const void * b)47 bool d3d12_sampler_desc_table_key_equals(const void* a, const void* b)
48 {
49 const d3d12_sampler_desc_table_key* table_a = (d3d12_sampler_desc_table_key*)a;
50 const d3d12_sampler_desc_table_key* table_b = (d3d12_sampler_desc_table_key*)b;
51 return table_a->count == table_b->count && memcmp(table_a->descs, table_b->descs, sizeof(table_a->descs[0]) * table_a->count) == 0;
52 }
53
54 bool
d3d12_init_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)55 d3d12_init_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
56 {
57 struct d3d12_screen *screen = d3d12_screen(ctx->base.screen);
58
59 batch->bos = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
60 _mesa_key_pointer_equal);
61
62 util_dynarray_init(&batch->local_bos, NULL);
63
64 batch->surfaces = _mesa_set_create(NULL, _mesa_hash_pointer,
65 _mesa_key_pointer_equal);
66 batch->objects = _mesa_set_create(NULL,
67 _mesa_hash_pointer,
68 _mesa_key_pointer_equal);
69
70 if (!batch->bos || !batch->surfaces || !batch->objects)
71 return false;
72
73 #ifdef HAVE_GALLIUM_D3D12_GRAPHICS
74 if (screen->max_feature_level >= D3D_FEATURE_LEVEL_11_0) {
75 batch->queries = _mesa_set_create(NULL, _mesa_hash_pointer,
76 _mesa_key_pointer_equal);
77
78 batch->view_heap =
79 d3d12_descriptor_heap_new(screen->dev,
80 D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
81 D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
82 8096);
83
84 batch->sampler_tables = _mesa_hash_table_create(NULL, d3d12_sampler_desc_table_key_hash,
85 d3d12_sampler_desc_table_key_equals);
86 batch->sampler_views = _mesa_set_create(NULL, _mesa_hash_pointer,
87 _mesa_key_pointer_equal);
88
89 if (!batch->sampler_tables || !batch->sampler_views || !batch->view_heap || !batch->queries)
90 return false;
91
92 util_dynarray_init(&batch->zombie_samplers, NULL);
93
94 batch->sampler_heap =
95 d3d12_descriptor_heap_new(screen->dev,
96 D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
97 D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
98 1024);
99
100 if (!batch->sampler_heap)
101 return false;
102 }
103 #endif // HAVE_GALLIUM_D3D12_GRAPHICS
104
105 if (FAILED(screen->dev->CreateCommandAllocator(screen->queue_type,
106 IID_PPV_ARGS(&batch->cmdalloc))))
107 return false;
108
109 return true;
110 }
111
112 static inline void
delete_bo(d3d12_bo * bo)113 delete_bo(d3d12_bo *bo)
114 {
115 d3d12_bo_unreference(bo);
116 }
117 static void
delete_bo_entry(hash_entry * entry)118 delete_bo_entry(hash_entry *entry)
119 {
120 struct d3d12_bo *bo = (struct d3d12_bo *)entry->key;
121 d3d12_bo_unreference(bo);
122 }
123
124 static void
delete_sampler_view_table(hash_entry * entry)125 delete_sampler_view_table(hash_entry *entry)
126 {
127 FREE((void*)entry->key);
128 FREE(entry->data);
129 }
130
131 static void
delete_sampler_view(set_entry * entry)132 delete_sampler_view(set_entry *entry)
133 {
134 struct pipe_sampler_view *pres = (struct pipe_sampler_view *)entry->key;
135 pipe_sampler_view_reference(&pres, NULL);
136 }
137
138 static void
delete_surface(set_entry * entry)139 delete_surface(set_entry *entry)
140 {
141 struct pipe_surface *surf = (struct pipe_surface *)entry->key;
142 pipe_surface_reference(&surf, NULL);
143 }
144
145 static void
delete_object(set_entry * entry)146 delete_object(set_entry *entry)
147 {
148 ID3D12Object *object = (ID3D12Object *)entry->key;
149 object->Release();
150 }
151
152 #ifdef HAVE_GALLIUM_D3D12_GRAPHICS
153 static void
delete_query(set_entry * entry)154 delete_query(set_entry *entry)
155 {
156 struct d3d12_query *query = (struct d3d12_query *)entry->key;
157 if (pipe_reference(&query->reference, nullptr))
158 d3d12_destroy_query(query);
159 }
160 #endif // HAVE_GALLIUM_D3D12_GRAPHICS
161
162 bool
d3d12_reset_batch(struct d3d12_context * ctx,struct d3d12_batch * batch,uint64_t timeout_ns)163 d3d12_reset_batch(struct d3d12_context *ctx, struct d3d12_batch *batch, uint64_t timeout_ns)
164 {
165 // batch hasn't been submitted before
166 if (!batch->fence && !batch->has_errors)
167 return true;
168
169 if (batch->fence) {
170 if (!d3d12_fence_finish(batch->fence, timeout_ns))
171 return false;
172 d3d12_fence_reference(&batch->fence, NULL);
173 }
174
175 _mesa_hash_table_clear(batch->bos, delete_bo_entry);
176 _mesa_set_clear(batch->surfaces, delete_surface);
177 _mesa_set_clear(batch->objects, delete_object);
178
179 util_dynarray_foreach(&batch->local_bos, d3d12_bo*, bo) {
180 (*bo)->local_reference_mask[batch->ctx_id] &= ~(1 << batch->ctx_index);
181 delete_bo(*bo);
182 }
183 util_dynarray_clear(&batch->local_bos);
184
185 #ifdef HAVE_GALLIUM_D3D12_GRAPHICS
186 if (d3d12_screen(ctx->base.screen)->max_feature_level >= D3D_FEATURE_LEVEL_11_0) {
187 _mesa_hash_table_clear(batch->sampler_tables, delete_sampler_view_table);
188 _mesa_set_clear(batch->sampler_views, delete_sampler_view);
189
190 _mesa_set_clear(batch->queries, delete_query);
191 util_dynarray_foreach(&batch->zombie_samplers, d3d12_descriptor_handle, handle)
192 d3d12_descriptor_handle_free(handle);
193 util_dynarray_clear(&batch->zombie_samplers);
194 d3d12_descriptor_heap_clear(batch->view_heap);
195 d3d12_descriptor_heap_clear(batch->sampler_heap);
196 }
197 #endif // HAVE_GALLIUM_D3D12_GRAPHICS
198
199 if (FAILED(batch->cmdalloc->Reset())) {
200 debug_printf("D3D12: resetting ID3D12CommandAllocator failed\n");
201 return false;
202 }
203 batch->has_errors = false;
204 batch->pending_memory_barrier = false;
205 return true;
206 }
207
208 void
d3d12_destroy_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)209 d3d12_destroy_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
210 {
211 d3d12_reset_batch(ctx, batch, OS_TIMEOUT_INFINITE);
212 batch->cmdalloc->Release();
213 _mesa_hash_table_destroy(batch->bos, NULL);
214
215 #ifdef HAVE_GALLIUM_D3D12_GRAPHICS
216 if (d3d12_screen(ctx->base.screen)->max_feature_level >= D3D_FEATURE_LEVEL_11_0) {
217 d3d12_descriptor_heap_free(batch->sampler_heap);
218 d3d12_descriptor_heap_free(batch->view_heap);
219 _mesa_hash_table_destroy(batch->sampler_tables, NULL);
220 _mesa_set_destroy(batch->sampler_views, NULL);
221 _mesa_set_destroy(batch->queries, NULL);
222 util_dynarray_fini(&batch->zombie_samplers);
223 }
224 #endif // HAVE_GALLIUM_D3D12_GRAPHICS
225
226 _mesa_set_destroy(batch->surfaces, NULL);
227 _mesa_set_destroy(batch->objects, NULL);
228 util_dynarray_fini(&batch->local_bos);
229 }
230
231 void
d3d12_start_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)232 d3d12_start_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
233 {
234 struct d3d12_screen *screen = d3d12_screen(ctx->base.screen);
235 d3d12_reset_batch(ctx, batch, OS_TIMEOUT_INFINITE);
236
237 /* Create or reset global command list */
238 if (ctx->cmdlist) {
239 if (FAILED(ctx->cmdlist->Reset(batch->cmdalloc, NULL))) {
240 debug_printf("D3D12: resetting ID3D12GraphicsCommandList failed\n");
241 batch->has_errors = true;
242 return;
243 }
244 } else {
245 if (FAILED(screen->dev->CreateCommandList(0, screen->queue_type,
246 batch->cmdalloc, NULL,
247 IID_PPV_ARGS(&ctx->cmdlist)))) {
248 debug_printf("D3D12: creating ID3D12GraphicsCommandList failed\n");
249 batch->has_errors = true;
250 return;
251 }
252 if (FAILED(ctx->cmdlist->QueryInterface(IID_PPV_ARGS(&ctx->cmdlist2)))) {
253 ctx->cmdlist2 = nullptr;
254 }
255 if (FAILED(ctx->cmdlist->QueryInterface(IID_PPV_ARGS(&ctx->cmdlist8)))) {
256 ctx->cmdlist8 = nullptr;
257 }
258 }
259
260 #ifdef HAVE_GALLIUM_D3D12_GRAPHICS
261 if (screen->max_feature_level >= D3D_FEATURE_LEVEL_11_0) {
262 ID3D12DescriptorHeap* heaps[2] = { d3d12_descriptor_heap_get(batch->view_heap),
263 d3d12_descriptor_heap_get(batch->sampler_heap) };
264 ctx->cmdlist->SetDescriptorHeaps(2, heaps);
265
266 ctx->cmdlist_dirty = ~0;
267 for (int i = 0; i < PIPE_SHADER_TYPES; ++i)
268 ctx->shader_dirty[i] = ~0;
269
270 if (!ctx->queries_disabled)
271 d3d12_resume_queries(ctx);
272 if (ctx->current_predication)
273 d3d12_enable_predication(ctx);
274 }
275 #endif // HAVE_GALLIUM_D3D12_GRAPHICS
276
277 batch->submit_id = ++ctx->submit_id;
278 }
279
280 void
d3d12_end_batch(struct d3d12_context * ctx,struct d3d12_batch * batch)281 d3d12_end_batch(struct d3d12_context *ctx, struct d3d12_batch *batch)
282 {
283 struct d3d12_screen *screen = d3d12_screen(ctx->base.screen);
284
285 #ifdef HAVE_GALLIUM_D3D12_GRAPHICS
286 if (!ctx->queries_disabled)
287 d3d12_suspend_queries(ctx);
288 #endif // HAVE_GALLIUM_D3D12_GRAPHICS
289
290 if (FAILED(ctx->cmdlist->Close())) {
291 debug_printf("D3D12: closing ID3D12GraphicsCommandList failed\n");
292 batch->has_errors = true;
293 return;
294 }
295
296 mtx_lock(&screen->submit_mutex);
297
298 #ifndef _GAMING_XBOX
299 d3d12_process_batch_residency(screen, batch);
300 #endif
301
302 bool has_state_fixup = d3d12_context_state_resolve_submission(ctx, batch);
303
304 ID3D12CommandList *cmdlists[] = { ctx->state_fixup_cmdlist, ctx->cmdlist };
305 ID3D12CommandList **to_execute = cmdlists;
306 UINT count_to_execute = ARRAY_SIZE(cmdlists);
307 if (!has_state_fixup) {
308 to_execute++;
309 count_to_execute--;
310 }
311 screen->cmdqueue->ExecuteCommandLists(count_to_execute, to_execute);
312
313 batch->fence = d3d12_create_fence(screen);
314
315 #ifdef HAVE_GALLIUM_D3D12_GRAPHICS
316 /* batch->queries is NULL when no grfx supported */
317 if (screen->max_feature_level >= D3D_FEATURE_LEVEL_11_0) {
318 set_foreach_remove(batch->queries, entry) {
319 d3d12_query *query = (struct d3d12_query *)entry->key;
320 if (pipe_reference(&query->reference, nullptr))
321 d3d12_destroy_query(query);
322 else
323 query->fence_value = screen->fence_value;
324 }
325 }
326 #endif // HAVE_GALLIUM_D3D12_GRAPHICS
327
328 mtx_unlock(&screen->submit_mutex);
329 }
330
331
332 inline uint8_t*
d3d12_batch_get_reference(struct d3d12_batch * batch,struct d3d12_bo * bo)333 d3d12_batch_get_reference(struct d3d12_batch *batch,
334 struct d3d12_bo *bo)
335 {
336 if (batch->ctx_id != D3D12_CONTEXT_NO_ID) {
337 if ((bo->local_reference_mask[batch->ctx_id] & (1 << batch->ctx_index)) != 0) {
338 return &bo->local_reference_state[batch->ctx_id][batch->ctx_index];
339 }
340 else
341 return NULL;
342 }
343 else {
344 hash_entry* entry = _mesa_hash_table_search(batch->bos, bo);
345 if (entry == NULL)
346 return NULL;
347 else
348 return (uint8_t*)&entry->data;
349 }
350 }
351
352 inline uint8_t*
d3d12_batch_acquire_reference(struct d3d12_batch * batch,struct d3d12_bo * bo)353 d3d12_batch_acquire_reference(struct d3d12_batch *batch,
354 struct d3d12_bo *bo)
355 {
356 if (batch->ctx_id != D3D12_CONTEXT_NO_ID) {
357 if ((bo->local_reference_mask[batch->ctx_id] & (1 << batch->ctx_index)) == 0) {
358 d3d12_bo_reference(bo);
359 util_dynarray_append(&batch->local_bos, d3d12_bo*, bo);
360 bo->local_reference_mask[batch->ctx_id] |= (1 << batch->ctx_index);
361 bo->local_reference_state[batch->ctx_id][batch->ctx_index] = batch_bo_reference_none;
362 }
363 return &bo->local_reference_state[batch->ctx_id][batch->ctx_index];
364 }
365 else {
366 hash_entry* entry = _mesa_hash_table_search(batch->bos, bo);
367 if (entry == NULL) {
368 d3d12_bo_reference(bo);
369 entry = _mesa_hash_table_insert(batch->bos, bo, NULL);
370 }
371
372 return (uint8_t*)&entry->data;
373 }
374 }
375
376 bool
d3d12_batch_has_references(struct d3d12_batch * batch,struct d3d12_bo * bo,bool want_to_write)377 d3d12_batch_has_references(struct d3d12_batch *batch,
378 struct d3d12_bo *bo,
379 bool want_to_write)
380 {
381 uint8_t*state = d3d12_batch_get_reference(batch, bo);
382 if (state == NULL)
383 return false;
384 bool resource_was_written = ((batch_bo_reference_state)(size_t)*state & batch_bo_reference_written) != 0;
385 return want_to_write || resource_was_written;
386 }
387
388 void
d3d12_batch_reference_resource(struct d3d12_batch * batch,struct d3d12_resource * res,bool write)389 d3d12_batch_reference_resource(struct d3d12_batch *batch,
390 struct d3d12_resource *res,
391 bool write)
392 {
393 uint8_t*state = d3d12_batch_acquire_reference(batch, res->bo);
394
395 uint8_t new_data = write ? batch_bo_reference_written : batch_bo_reference_read;
396 uint8_t old_data = (uint8_t)*state;
397 *state = (old_data | new_data);
398 }
399
400 void
d3d12_batch_reference_sampler_view(struct d3d12_batch * batch,struct d3d12_sampler_view * sv)401 d3d12_batch_reference_sampler_view(struct d3d12_batch *batch,
402 struct d3d12_sampler_view *sv)
403 {
404 struct set_entry *entry = _mesa_set_search(batch->sampler_views, sv);
405 if (!entry) {
406 entry = _mesa_set_add(batch->sampler_views, sv);
407 pipe_reference(NULL, &sv->base.reference);
408
409 d3d12_batch_reference_resource(batch, d3d12_resource(sv->base.texture), false);
410 }
411 }
412
413 void
d3d12_batch_reference_surface_texture(struct d3d12_batch * batch,struct d3d12_surface * surf)414 d3d12_batch_reference_surface_texture(struct d3d12_batch *batch,
415 struct d3d12_surface *surf)
416 {
417 d3d12_batch_reference_resource(batch, d3d12_resource(surf->base.texture), true);
418 }
419
420 void
d3d12_batch_reference_object(struct d3d12_batch * batch,ID3D12Object * object)421 d3d12_batch_reference_object(struct d3d12_batch *batch,
422 ID3D12Object *object)
423 {
424 struct set_entry *entry = _mesa_set_search(batch->objects, object);
425 if (!entry) {
426 entry = _mesa_set_add(batch->objects, object);
427 object->AddRef();
428 }
429 }
430
431 void
d3d12_batch_reference_query(struct d3d12_batch * batch,struct d3d12_query * query)432 d3d12_batch_reference_query(struct d3d12_batch *batch,
433 struct d3d12_query *query)
434 {
435 struct set_entry *entry = _mesa_set_search(batch->queries, query);
436 if (!entry) {
437 entry = _mesa_set_add(batch->queries, query);
438 pipe_reference(NULL, &query->reference);
439 }
440 }
441