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 
key_hash(const void * key)37 static uint32_t key_hash(const void *key)
38 {
39    /* Take the first dword of SHA1. */
40    return *(uint32_t*)key;
41 }
42 
key_equals(const void * a,const void * b)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
util_live_shader_cache_init(struct util_live_shader_cache * cache,void * (* create_shader)(struct pipe_context *,const struct pipe_shader_state * state),void (* destroy_shader)(struct pipe_context *,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
util_live_shader_cache_deinit(struct util_live_shader_cache * cache)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 *
util_live_shader_cache_get(struct pipe_context * ctx,struct util_live_shader_cache * cache,const struct pipe_shader_state * state,bool * cache_hit)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       if (state->type == PIPE_SHADER_IR_NIR)
134           ralloc_free(state->ir.nir);
135       return shader;
136    }
137 
138    /* The cache mutex is unlocked to allow multiple create_shader
139     * invocations to run simultaneously.
140     */
141    shader = (struct util_live_shader*)cache->create_shader(ctx, state);
142    pipe_reference_init(&shader->reference, 1);
143    memcpy(shader->sha1, sha1, sizeof(sha1));
144 
145    simple_mtx_lock(&cache->lock);
146    /* The same shader might have been created in parallel. This is rare.
147     * If so, keep the one already in cache.
148     */
149    struct hash_entry *entry2 = _mesa_hash_table_search(cache->hashtable, sha1);
150    struct util_live_shader *shader2 = entry2 ? entry2->data : NULL;
151 
152    if (shader2) {
153       cache->destroy_shader(ctx, shader);
154       shader = shader2;
155       /* Increase the refcount. */
156       pipe_reference(NULL, &shader->reference);
157    } else {
158       _mesa_hash_table_insert(cache->hashtable, shader->sha1, shader);
159    }
160    cache->misses++;
161    simple_mtx_unlock(&cache->lock);
162 
163    return shader;
164 }
165 
166 void
util_shader_reference(struct pipe_context * ctx,struct util_live_shader_cache * cache,void ** dst,void * src)167 util_shader_reference(struct pipe_context *ctx,
168                       struct util_live_shader_cache *cache,
169                       void **dst, void *src)
170 {
171    if (*dst == src)
172       return;
173 
174    struct util_live_shader *dst_shader = (struct util_live_shader*)*dst;
175    struct util_live_shader *src_shader = (struct util_live_shader*)src;
176 
177    simple_mtx_lock(&cache->lock);
178    bool destroy = pipe_reference(&dst_shader->reference, &src_shader->reference);
179    if (destroy) {
180       struct hash_entry *entry = _mesa_hash_table_search(cache->hashtable,
181                                                          dst_shader->sha1);
182       assert(entry);
183       _mesa_hash_table_remove(cache->hashtable, entry);
184    }
185    simple_mtx_unlock(&cache->lock);
186 
187    if (destroy)
188       cache->destroy_shader(ctx, dst_shader);
189 
190    *dst = src;
191 }
192