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