2 * Copyright 2019 Advanced Micro Devices, Inc.
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:
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
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.
25 /* This deduplicates live shader CSOs, meaning that creating 2 shaders with
26 * the same IR will return the same CSO.
30 * - create_xx_state should only call util_live_shader_cache_get.
32 * - delete_xx_state should only call util_shader_reference(&shader, NULL).
33 * This will decrease the reference count.
35 * - Driver shaders must inherit util_live_shader. They don't have to
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.
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.
46 * - Call util_live_shader_cache_deinit when you destroy your screen or context.
49 #ifndef U_LIVE_SHADER_CACHE_H
50 #define U_LIVE_SHADER_CACHE_H
52 #include "util/simple_mtx.h"
53 #include "pipe/p_state.h"
55 struct util_live_shader_cache
{
57 struct hash_table
*hashtable
;
59 void *(*create_shader
)(struct pipe_context
*,
60 const struct pipe_shader_state
*state
);
61 void (*destroy_shader
)(struct pipe_context
*, void *);
63 unsigned hits
, misses
;
66 struct util_live_shader
{
67 struct pipe_reference reference
;
68 unsigned char sha1
[20];
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 *));
78 util_live_shader_cache_deinit(struct util_live_shader_cache
*cache
);
81 util_live_shader_cache_get(struct pipe_context
*ctx
,
82 struct util_live_shader_cache
*cache
,
83 const struct pipe_shader_state
*state
);
86 util_shader_reference(struct pipe_context
*ctx
,
87 struct util_live_shader_cache
*cache
,
88 void **dst
, void *src
);