1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © Microsoft Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "d3d12_cmd_signature.h"
25bf215546Sopenharmony_ci#include "d3d12_compiler.h"
26bf215546Sopenharmony_ci#include "d3d12_screen.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "util/u_memory.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <dxguids/dxguids.h>
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistruct d3d12_cmd_signature {
33bf215546Sopenharmony_ci   struct d3d12_cmd_signature_key key;
34bf215546Sopenharmony_ci   ID3D12CommandSignature *sig;
35bf215546Sopenharmony_ci};
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic ID3D12CommandSignature *
38bf215546Sopenharmony_cicreate_cmd_signature(struct d3d12_context *ctx, const struct d3d12_cmd_signature_key *key)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   D3D12_COMMAND_SIGNATURE_DESC cmd_sig_desc = {};
41bf215546Sopenharmony_ci   D3D12_INDIRECT_ARGUMENT_DESC indirect_args[2] = {};
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   unsigned num_args = 0;
44bf215546Sopenharmony_ci   if (key->draw_or_dispatch_params) {
45bf215546Sopenharmony_ci      indirect_args[num_args].Type = D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT;
46bf215546Sopenharmony_ci      indirect_args[num_args].Constant.RootParameterIndex = key->params_root_const_param;
47bf215546Sopenharmony_ci      indirect_args[num_args].Constant.DestOffsetIn32BitValues = key->params_root_const_offset;
48bf215546Sopenharmony_ci      indirect_args[num_args++].Constant.Num32BitValuesToSet = key->compute ? 3 : 4;
49bf215546Sopenharmony_ci   }
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   indirect_args[num_args++].Type = key->compute ? D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH :
52bf215546Sopenharmony_ci      key->indexed ? D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED :
53bf215546Sopenharmony_ci      D3D12_INDIRECT_ARGUMENT_TYPE_DRAW;
54bf215546Sopenharmony_ci   cmd_sig_desc.ByteStride = key->multi_draw_stride;
55bf215546Sopenharmony_ci   cmd_sig_desc.NumArgumentDescs = num_args;
56bf215546Sopenharmony_ci   cmd_sig_desc.pArgumentDescs = indirect_args;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   ID3D12CommandSignature *ret = nullptr;
59bf215546Sopenharmony_ci   d3d12_screen(ctx->base.screen)->dev->CreateCommandSignature(&cmd_sig_desc, key->root_sig,
60bf215546Sopenharmony_ci      IID_PPV_ARGS(&ret));
61bf215546Sopenharmony_ci   return ret;
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ciID3D12CommandSignature *
65bf215546Sopenharmony_cid3d12_get_cmd_signature(struct d3d12_context *ctx,
66bf215546Sopenharmony_ci                            const struct d3d12_cmd_signature_key *key)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   struct hash_entry *entry = _mesa_hash_table_search(ctx->cmd_signature_cache, &key);
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   if (!entry) {
71bf215546Sopenharmony_ci      struct d3d12_cmd_signature *data =
72bf215546Sopenharmony_ci         (struct d3d12_cmd_signature *)MALLOC(sizeof(struct d3d12_cmd_signature));
73bf215546Sopenharmony_ci      if (!data)
74bf215546Sopenharmony_ci         return NULL;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci      memcpy(&data->key, key, sizeof(*key));
77bf215546Sopenharmony_ci      data->sig = create_cmd_signature(ctx, key);
78bf215546Sopenharmony_ci      if (!data->sig) {
79bf215546Sopenharmony_ci         FREE(data);
80bf215546Sopenharmony_ci         return NULL;
81bf215546Sopenharmony_ci      }
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci      entry = _mesa_hash_table_insert(ctx->cmd_signature_cache, &data->key, data);
84bf215546Sopenharmony_ci      assert(entry);
85bf215546Sopenharmony_ci   }
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   return ((struct d3d12_cmd_signature *)entry->data)->sig;
88bf215546Sopenharmony_ci}
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_cistatic uint32_t
91bf215546Sopenharmony_cihash_cmd_signature_key(const void *key)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   return _mesa_hash_data(key, sizeof(struct d3d12_cmd_signature_key));
94bf215546Sopenharmony_ci}
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_cistatic bool
97bf215546Sopenharmony_ciequals_cmd_signature_key(const void *a, const void *b)
98bf215546Sopenharmony_ci{
99bf215546Sopenharmony_ci   return memcmp(a, b, sizeof(struct d3d12_cmd_signature_key)) == 0;
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_civoid
103bf215546Sopenharmony_cid3d12_cmd_signature_cache_init(struct d3d12_context *ctx)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   ctx->cmd_signature_cache = _mesa_hash_table_create(NULL,
106bf215546Sopenharmony_ci                                                      hash_cmd_signature_key,
107bf215546Sopenharmony_ci                                                      equals_cmd_signature_key);
108bf215546Sopenharmony_ci}
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_cistatic void
111bf215546Sopenharmony_cidelete_entry(struct hash_entry *entry)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci   struct d3d12_cmd_signature *data = (struct d3d12_cmd_signature *)entry->data;
114bf215546Sopenharmony_ci   data->sig->Release();
115bf215546Sopenharmony_ci   FREE(data);
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_civoid
119bf215546Sopenharmony_cid3d12_cmd_signature_cache_destroy(struct d3d12_context *ctx)
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci   _mesa_hash_table_destroy(ctx->cmd_signature_cache, delete_entry);
122bf215546Sopenharmony_ci}
123