util: add a resource wrapper to get resource samples
[mesa.git] / src / gallium / auxiliary / util / u_live_shader_cache.c
1 /*
2 * Copyright 2019 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "util/u_live_shader_cache.h"
26
27 #include "util/u_inlines.h"
28 #include "tgsi/tgsi_from_mesa.h"
29 #include "tgsi/tgsi_parse.h"
30
31 #include "compiler/nir/nir_serialize.h"
32
33 #include "util/blob.h"
34 #include "util/hash_table.h"
35 #include "util/mesa-sha1.h"
36
37 static uint32_t key_hash(const void *key)
38 {
39 /* Take the first dword of SHA1. */
40 return *(uint32_t*)key;
41 }
42
43 static bool key_equals(const void *a, const void *b)
44 {
45 /* Compare SHA1s. */
46 return memcmp(a, b, 20) == 0;
47 }
48
49 void
50 util_live_shader_cache_init(struct util_live_shader_cache *cache,
51 void *(*create_shader)(struct pipe_context *,
52 const struct pipe_shader_state *state),
53 void (*destroy_shader)(struct pipe_context *, void *))
54 {
55 simple_mtx_init(&cache->lock, mtx_plain);
56 cache->hashtable = _mesa_hash_table_create(NULL, key_hash, key_equals);
57 cache->create_shader = create_shader;
58 cache->destroy_shader = destroy_shader;
59 }
60
61 void
62 util_live_shader_cache_deinit(struct util_live_shader_cache *cache)
63 {
64 if (cache->hashtable) {
65 /* The hash table should be empty at this point. */
66 _mesa_hash_table_destroy(cache->hashtable, NULL);
67 simple_mtx_destroy(&cache->lock);
68 }
69 }
70
71 void *
72 util_live_shader_cache_get(struct pipe_context *ctx,
73 struct util_live_shader_cache *cache,
74 const struct pipe_shader_state *state,
75 bool* cache_hit)
76 {
77 struct blob blob = {0};
78 unsigned ir_size;
79 const void *ir_binary;
80 enum pipe_shader_type stage;
81
82 /* Get the shader binary and shader stage. */
83 if (state->type == PIPE_SHADER_IR_TGSI) {
84 ir_binary = state->tokens;
85 ir_size = tgsi_num_tokens(state->tokens) *
86 sizeof(struct tgsi_token);
87 stage = tgsi_get_processor_type(state->tokens);
88 } else if (state->type == PIPE_SHADER_IR_NIR) {
89 blob_init(&blob);
90 nir_serialize(&blob, state->ir.nir, true);
91 ir_binary = blob.data;
92 ir_size = blob.size;
93 stage = pipe_shader_type_from_mesa(((nir_shader*)state->ir.nir)->info.stage);
94 } else {
95 assert(0);
96 return NULL;
97 }
98
99 /* Compute SHA1 of pipe_shader_state. */
100 struct mesa_sha1 sha1_ctx;
101 unsigned char sha1[20];
102 _mesa_sha1_init(&sha1_ctx);
103 _mesa_sha1_update(&sha1_ctx, ir_binary, ir_size);
104 if ((stage == PIPE_SHADER_VERTEX ||
105 stage == PIPE_SHADER_TESS_EVAL ||
106 stage == PIPE_SHADER_GEOMETRY) &&
107 state->stream_output.num_outputs) {
108 _mesa_sha1_update(&sha1_ctx, &state->stream_output,
109 sizeof(state->stream_output));
110 }
111 _mesa_sha1_final(&sha1_ctx, sha1);
112
113 if (ir_binary == blob.data)
114 blob_finish(&blob);
115
116 /* Find the shader in the live cache. */
117 simple_mtx_lock(&cache->lock);
118 struct hash_entry *entry = _mesa_hash_table_search(cache->hashtable, sha1);
119 struct util_live_shader *shader = entry ? entry->data : NULL;
120
121 /* Increase the refcount. */
122 if (shader) {
123 pipe_reference(NULL, &shader->reference);
124 cache->hits++;
125 }
126 simple_mtx_unlock(&cache->lock);
127
128 if (cache_hit)
129 *cache_hit = (shader != NULL);
130
131 /* Return if the shader already exists. */
132 if (shader)
133 return shader;
134
135 /* The cache mutex is unlocked to allow multiple create_shader
136 * invocations to run simultaneously.
137 */
138 shader = (struct util_live_shader*)cache->create_shader(ctx, state);
139 pipe_reference_init(&shader->reference, 1);
140 memcpy(shader->sha1, sha1, sizeof(sha1));
141
142 simple_mtx_lock(&cache->lock);
143 /* The same shader might have been created in parallel. This is rare.
144 * If so, keep the one already in cache.
145 */
146 struct hash_entry *entry2 = _mesa_hash_table_search(cache->hashtable, sha1);
147 struct util_live_shader *shader2 = entry2 ? entry2->data : NULL;
148
149 if (shader2) {
150 cache->destroy_shader(ctx, shader);
151 shader = shader2;
152 /* Increase the refcount. */
153 pipe_reference(NULL, &shader->reference);
154 } else {
155 _mesa_hash_table_insert(cache->hashtable, shader->sha1, shader);
156 }
157 cache->misses++;
158 simple_mtx_unlock(&cache->lock);
159
160 return shader;
161 }
162
163 void
164 util_shader_reference(struct pipe_context *ctx,
165 struct util_live_shader_cache *cache,
166 void **dst, void *src)
167 {
168 if (*dst == src)
169 return;
170
171 struct util_live_shader *dst_shader = (struct util_live_shader*)*dst;
172 struct util_live_shader *src_shader = (struct util_live_shader*)src;
173
174 simple_mtx_lock(&cache->lock);
175 bool destroy = pipe_reference(&dst_shader->reference, &src_shader->reference);
176 if (destroy) {
177 struct hash_entry *entry = _mesa_hash_table_search(cache->hashtable,
178 dst_shader->sha1);
179 assert(entry);
180 _mesa_hash_table_remove(cache->hashtable, entry);
181 }
182 simple_mtx_unlock(&cache->lock);
183
184 if (destroy)
185 cache->destroy_shader(ctx, dst_shader);
186
187 *dst = src;
188 }