• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* This deduplicates live shader CSOs, meaning that creating 2 shaders with
26  * the same IR will return the same CSO.
27  *
28  * How to use this:
29  *
30  * - create_xx_state should only call util_live_shader_cache_get.
31  *
32  * - delete_xx_state should only call util_shader_reference(&shader, NULL).
33  *   This will decrease the reference count.
34  *
35  * - Driver shaders must inherit util_live_shader. They don't have to
36  *   initialize it.
37  *
38  * - Declare struct util_live_shader_cache in your pipe_screen (no pointer) if
39  *   you support shareable shaders. If not, declare it in your pipe_context.
40  *
41  * - Set your create_shader and destroy_shader driver callbacks with
42  *   util_live_shader_cache_init. These are your driver versions of
43  *   create_xx_state and delete_xx_state. There is no distinction between
44  *   vs, tcs, tes, gs, fs. Instead, get the shader type from the IR.
45  *
46  * - Call util_live_shader_cache_deinit when you destroy your screen or context.
47  */
48 
49 #ifndef U_LIVE_SHADER_CACHE_H
50 #define U_LIVE_SHADER_CACHE_H
51 
52 #include "util/simple_mtx.h"
53 #include "pipe/p_state.h"
54 
55 struct util_live_shader_cache {
56    simple_mtx_t lock;
57    struct hash_table *hashtable;
58 
59    void *(*create_shader)(struct pipe_context *,
60                           const struct pipe_shader_state *state);
61    void (*destroy_shader)(struct pipe_context *, void *);
62 
63    unsigned hits, misses;
64 };
65 
66 struct util_live_shader {
67    struct pipe_reference reference;
68    unsigned char sha1[20];
69 };
70 
71 void
72 util_live_shader_cache_init(struct util_live_shader_cache *cache,
73                               void *(*create_shader)(struct pipe_context *,
74                                                      const struct pipe_shader_state *state),
75                               void (*destroy_shader)(struct pipe_context *, void *));
76 
77 void
78 util_live_shader_cache_deinit(struct util_live_shader_cache *cache);
79 
80 void *
81 util_live_shader_cache_get(struct pipe_context *ctx,
82                            struct util_live_shader_cache *cache,
83                            const struct pipe_shader_state *state,
84                            bool* cache_hit);
85 
86 void
87 util_shader_reference(struct pipe_context *ctx,
88                       struct util_live_shader_cache *cache,
89                       void **dst, void *src);
90 
91 #endif
92