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_cmd_signature.h"
25 #include "d3d12_compiler.h"
26 #include "d3d12_screen.h"
27
28 #include "util/u_memory.h"
29
30 #include <dxguids/dxguids.h>
31
32 struct d3d12_cmd_signature {
33 struct d3d12_cmd_signature_key key;
34 ID3D12CommandSignature *sig;
35 };
36
37 static ID3D12CommandSignature *
create_cmd_signature(struct d3d12_context * ctx,const struct d3d12_cmd_signature_key * key)38 create_cmd_signature(struct d3d12_context *ctx, const struct d3d12_cmd_signature_key *key)
39 {
40 D3D12_COMMAND_SIGNATURE_DESC cmd_sig_desc = {};
41 D3D12_INDIRECT_ARGUMENT_DESC indirect_args[2] = {};
42
43 unsigned num_args = 0;
44 if (key->draw_or_dispatch_params) {
45 indirect_args[num_args].Type = D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT;
46 indirect_args[num_args].Constant.RootParameterIndex = key->params_root_const_param;
47 indirect_args[num_args].Constant.DestOffsetIn32BitValues = key->params_root_const_offset;
48 indirect_args[num_args++].Constant.Num32BitValuesToSet = key->compute ? 3 : 4;
49 }
50
51 indirect_args[num_args++].Type = key->compute ? D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH :
52 key->indexed ? D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED :
53 D3D12_INDIRECT_ARGUMENT_TYPE_DRAW;
54 cmd_sig_desc.ByteStride = key->multi_draw_stride;
55 cmd_sig_desc.NumArgumentDescs = num_args;
56 cmd_sig_desc.pArgumentDescs = indirect_args;
57
58 ID3D12CommandSignature *ret = nullptr;
59 d3d12_screen(ctx->base.screen)->dev->CreateCommandSignature(&cmd_sig_desc, key->root_sig,
60 IID_PPV_ARGS(&ret));
61 return ret;
62 }
63
64 ID3D12CommandSignature *
d3d12_get_cmd_signature(struct d3d12_context * ctx,const struct d3d12_cmd_signature_key * key)65 d3d12_get_cmd_signature(struct d3d12_context *ctx,
66 const struct d3d12_cmd_signature_key *key)
67 {
68 struct hash_entry *entry = _mesa_hash_table_search(ctx->cmd_signature_cache, &key);
69
70 if (!entry) {
71 struct d3d12_cmd_signature *data =
72 (struct d3d12_cmd_signature *)MALLOC(sizeof(struct d3d12_cmd_signature));
73 if (!data)
74 return NULL;
75
76 memcpy(&data->key, key, sizeof(*key));
77 data->sig = create_cmd_signature(ctx, key);
78 if (!data->sig) {
79 FREE(data);
80 return NULL;
81 }
82
83 entry = _mesa_hash_table_insert(ctx->cmd_signature_cache, &data->key, data);
84 assert(entry);
85 }
86
87 return ((struct d3d12_cmd_signature *)entry->data)->sig;
88 }
89
90 static uint32_t
hash_cmd_signature_key(const void * key)91 hash_cmd_signature_key(const void *key)
92 {
93 return _mesa_hash_data(key, sizeof(struct d3d12_cmd_signature_key));
94 }
95
96 static bool
equals_cmd_signature_key(const void * a,const void * b)97 equals_cmd_signature_key(const void *a, const void *b)
98 {
99 return memcmp(a, b, sizeof(struct d3d12_cmd_signature_key)) == 0;
100 }
101
102 void
d3d12_cmd_signature_cache_init(struct d3d12_context * ctx)103 d3d12_cmd_signature_cache_init(struct d3d12_context *ctx)
104 {
105 ctx->cmd_signature_cache = _mesa_hash_table_create(NULL,
106 hash_cmd_signature_key,
107 equals_cmd_signature_key);
108 }
109
110 static void
delete_entry(struct hash_entry * entry)111 delete_entry(struct hash_entry *entry)
112 {
113 struct d3d12_cmd_signature *data = (struct d3d12_cmd_signature *)entry->data;
114 data->sig->Release();
115 FREE(data);
116 }
117
118 void
d3d12_cmd_signature_cache_destroy(struct d3d12_context * ctx)119 d3d12_cmd_signature_cache_destroy(struct d3d12_context *ctx)
120 {
121 _mesa_hash_table_destroy(ctx->cmd_signature_cache, delete_entry);
122 }
123