1 /*
2 * Copyright 2020 Mike Blumenkrantz
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25 /** this file exists for organization and to be included in nir_to_spirv/ without pulling in extra deps */
26 #ifndef ZINK_SHADER_KEYS_H
27 # define ZINK_SHADER_KEYS_H
28
29 #include "compiler/shader_info.h"
30
31 struct zink_vs_key_base {
32 bool last_vertex_stage : 1;
33 bool clip_halfz : 1;
34 bool push_drawid : 1;
35 bool robust_access : 1;
36 uint8_t pad : 4;
37 };
38
39 struct zink_vs_key {
40 struct zink_vs_key_base base;
41 uint8_t pad;
42 union {
43 struct {
44 uint32_t decomposed_attrs;
45 uint32_t decomposed_attrs_without_w;
46 } u32;
47 struct {
48 uint16_t decomposed_attrs;
49 uint16_t decomposed_attrs_without_w;
50 } u16;
51 struct {
52 uint8_t decomposed_attrs;
53 uint8_t decomposed_attrs_without_w;
54 } u8;
55 };
56 // not hashed
57 unsigned size;
58 };
59
60 struct zink_gs_key {
61 struct zink_vs_key_base base;
62 uint8_t pad;
63 bool lower_line_stipple : 1;
64 bool lower_line_smooth : 1;
65 bool lower_gl_point : 1;
66 bool line_rectangular : 1;
67 unsigned lower_pv_mode : 2;
68 // not hashed
69 unsigned size;
70 };
71
72 struct zink_zs_swizzle {
73 uint8_t s[4];
74 };
75
76 struct zink_zs_swizzle_key {
77 /* Mask of sampler views with zs_view, i.e. have swizzles other than GL_RED for depth */
78 uint32_t mask;
79 struct zink_zs_swizzle swizzle[32];
80 };
81
82 struct zink_fs_key_base {
83 bool point_coord_yinvert : 1;
84 bool samples : 1;
85 bool force_dual_color_blend : 1;
86 bool force_persample_interp : 1;
87 bool fbfetch_ms : 1;
88 bool shadow_needs_shader_swizzle : 1; //append zink_zs_swizzle_key after the key data
89 uint8_t pad : 2;
90 uint8_t coord_replace_bits;
91 };
92
93 struct zink_fs_key {
94 struct zink_fs_key_base base;
95 /* non-optimal bits after this point */
96 bool lower_line_stipple : 1;
97 bool lower_line_smooth : 1;
98 bool lower_point_smooth : 1;
99 bool robust_access : 1;
100 uint16_t pad2 : 12;
101 };
102
103 struct zink_tcs_key {
104 uint8_t patch_vertices;
105 };
106
107 /* when adding a new field, make sure
108 * ctx->compute_pipeline_state.key.size is set in zink_context_create.
109 */
110 struct zink_cs_key {
111 bool robust_access : 1;
112 uint32_t pad : 31;
113 };
114
115 struct zink_shader_key_base {
116 bool needs_zs_shader_swizzle;
117 uint32_t nonseamless_cube_mask;
118 uint32_t inlined_uniform_values[MAX_INLINABLE_UNIFORMS];
119 };
120
121 /* a shader key is used for swapping out shader modules based on pipeline states,
122 * e.g., if sampleCount changes, we must verify that the fs doesn't need a recompile
123 * to account for GL ignoring gl_SampleMask in some cases when VK will not
124 * which allows us to avoid recompiling shaders when the pipeline state changes repeatedly
125 */
126 struct zink_shader_key {
127 union {
128 /* reuse vs key for now with tes since we only use clip_halfz */
129 struct zink_vs_key vs;
130 struct zink_vs_key_base vs_base;
131 struct zink_tcs_key tcs;
132 struct zink_gs_key gs;
133 struct zink_fs_key fs;
134 struct zink_fs_key_base fs_base;
135 struct zink_cs_key cs;
136 } key;
137 struct zink_shader_key_base base;
138 unsigned inline_uniforms:1;
139 uint32_t size;
140 };
141
142 union zink_shader_key_optimal {
143 struct {
144 struct zink_vs_key_base vs_base;
145 struct zink_tcs_key tcs;
146 struct zink_fs_key_base fs;
147 };
148 struct {
149 uint8_t vs_bits;
150 uint8_t tcs_bits;
151 uint16_t fs_bits;
152 };
153 uint32_t val;
154 };
155
156 /* the default key has only last_vertex_stage set*/
157 #define ZINK_SHADER_KEY_OPTIMAL_DEFAULT (1<<0)
158 /* Ignore patch_vertices bits that would only be used if we had to generate the missing TCS */
159 static inline uint32_t
zink_shader_key_optimal_no_tcs(uint32_t key)160 zink_shader_key_optimal_no_tcs(uint32_t key)
161 {
162 union zink_shader_key_optimal k;
163 k.val = key;
164 k.tcs_bits = 0;
165 return k.val;
166 }
167 #define ZINK_SHADER_KEY_OPTIMAL_IS_DEFAULT(key) (zink_shader_key_optimal_no_tcs(key) == ZINK_SHADER_KEY_OPTIMAL_DEFAULT)
168
169 static inline const struct zink_fs_key_base *
zink_fs_key_base(const struct zink_shader_key * key)170 zink_fs_key_base(const struct zink_shader_key *key)
171 {
172 assert(key);
173 return &key->key.fs.base;
174 }
175
176 static inline const struct zink_fs_key *
zink_fs_key(const struct zink_shader_key * key)177 zink_fs_key(const struct zink_shader_key *key)
178 {
179 assert(key);
180 return &key->key.fs;
181 }
182
183 static inline const struct zink_vs_key_base *
zink_vs_key_base(const struct zink_shader_key * key)184 zink_vs_key_base(const struct zink_shader_key *key)
185 {
186 return &key->key.vs_base;
187 }
188
189 static inline const struct zink_vs_key *
zink_vs_key(const struct zink_shader_key * key)190 zink_vs_key(const struct zink_shader_key *key)
191 {
192 assert(key);
193 return &key->key.vs;
194 }
195
196 static inline const struct zink_gs_key *
zink_gs_key(const struct zink_shader_key * key)197 zink_gs_key(const struct zink_shader_key *key)
198 {
199 assert(key);
200 return &key->key.gs;
201 }
202
203 static inline const struct zink_tcs_key *
zink_tcs_key(const struct zink_shader_key * key)204 zink_tcs_key(const struct zink_shader_key *key)
205 {
206 assert(key);
207 return &key->key.tcs;
208 }
209
210 static inline const struct zink_cs_key *
zink_cs_key(const struct zink_shader_key * key)211 zink_cs_key(const struct zink_shader_key *key)
212 {
213 assert(key);
214 return &key->key.cs;
215 }
216
217 #endif
218