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 #ifdef __cplusplus 56 extern "C" { 57 #endif 58 59 struct util_live_shader_cache { 60 simple_mtx_t lock; 61 struct hash_table *hashtable; 62 63 void *(*create_shader)(struct pipe_context *, 64 const struct pipe_shader_state *state); 65 void (*destroy_shader)(struct pipe_context *, void *); 66 67 unsigned hits, misses; 68 }; 69 70 struct util_live_shader { 71 struct pipe_reference reference; 72 unsigned char sha1[20]; 73 }; 74 75 void 76 util_live_shader_cache_init(struct util_live_shader_cache *cache, 77 void *(*create_shader)(struct pipe_context *, 78 const struct pipe_shader_state *state), 79 void (*destroy_shader)(struct pipe_context *, void *)); 80 81 void 82 util_live_shader_cache_deinit(struct util_live_shader_cache *cache); 83 84 void * 85 util_live_shader_cache_get(struct pipe_context *ctx, 86 struct util_live_shader_cache *cache, 87 const struct pipe_shader_state *state, 88 bool* cache_hit); 89 90 void 91 util_shader_reference(struct pipe_context *ctx, 92 struct util_live_shader_cache *cache, 93 void **dst, void *src); 94 95 #ifdef __cplusplus 96 } 97 #endif 98 99 #endif 100