1 #include "vk_graphics_state.h"
2 
3 #include "vk_alloc.h"
4 #include "vk_command_buffer.h"
5 #include "vk_common_entrypoints.h"
6 #include "vk_device.h"
7 #include "vk_log.h"
8 #include "vk_render_pass.h"
9 #include "vk_standard_sample_locations.h"
10 #include "vk_util.h"
11 
12 #include <assert.h>
13 
14 enum mesa_vk_graphics_state_groups {
15    MESA_VK_GRAPHICS_STATE_VERTEX_INPUT_BIT            = (1 << 0),
16    MESA_VK_GRAPHICS_STATE_INPUT_ASSEMBLY_BIT          = (1 << 1),
17    MESA_VK_GRAPHICS_STATE_TESSELLATION_BIT            = (1 << 2),
18    MESA_VK_GRAPHICS_STATE_VIEWPORT_BIT                = (1 << 3),
19    MESA_VK_GRAPHICS_STATE_DISCARD_RECTANGLES_BIT      = (1 << 4),
20    MESA_VK_GRAPHICS_STATE_RASTERIZATION_BIT           = (1 << 5),
21    MESA_VK_GRAPHICS_STATE_FRAGMENT_SHADING_RATE_BIT   = (1 << 6),
22    MESA_VK_GRAPHICS_STATE_MULTISAMPLE_BIT             = (1 << 7),
23    MESA_VK_GRAPHICS_STATE_DEPTH_STENCIL_BIT           = (1 << 8),
24    MESA_VK_GRAPHICS_STATE_COLOR_BLEND_BIT             = (1 << 9),
25    MESA_VK_GRAPHICS_STATE_RENDER_PASS_BIT             = (1 << 10),
26 };
27 
28 static void
clear_all_dynamic_state(BITSET_WORD * dynamic)29 clear_all_dynamic_state(BITSET_WORD *dynamic)
30 {
31    /* Clear the whole array so there are no undefined bits at the top */
32    memset(dynamic, 0, sizeof(*dynamic) *
33           BITSET_WORDS(MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX));
34 }
35 
36 static void
get_dynamic_state_groups(BITSET_WORD * dynamic,enum mesa_vk_graphics_state_groups groups)37 get_dynamic_state_groups(BITSET_WORD *dynamic,
38                          enum mesa_vk_graphics_state_groups groups)
39 {
40    clear_all_dynamic_state(dynamic);
41 
42    if (groups & MESA_VK_GRAPHICS_STATE_VERTEX_INPUT_BIT) {
43       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VI);
44       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VI_BINDINGS_VALID);
45       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VI_BINDING_STRIDES);
46    }
47 
48    if (groups & MESA_VK_GRAPHICS_STATE_INPUT_ASSEMBLY_BIT) {
49       BITSET_SET(dynamic, MESA_VK_DYNAMIC_IA_PRIMITIVE_TOPOLOGY);
50       BITSET_SET(dynamic, MESA_VK_DYNAMIC_IA_PRIMITIVE_RESTART_ENABLE);
51    }
52 
53    if (groups & MESA_VK_GRAPHICS_STATE_TESSELLATION_BIT) {
54       BITSET_SET(dynamic, MESA_VK_DYNAMIC_TS_PATCH_CONTROL_POINTS);
55       BITSET_SET(dynamic, MESA_VK_DYNAMIC_TS_DOMAIN_ORIGIN);
56    }
57 
58    if (groups & MESA_VK_GRAPHICS_STATE_VIEWPORT_BIT) {
59       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VP_VIEWPORT_COUNT);
60       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VP_VIEWPORTS);
61       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VP_SCISSOR_COUNT);
62       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VP_SCISSORS);
63       BITSET_SET(dynamic, MESA_VK_DYNAMIC_VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE);
64    }
65 
66    if (groups & MESA_VK_GRAPHICS_STATE_DISCARD_RECTANGLES_BIT) {
67       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DR_RECTANGLES);
68       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DR_ENABLE);
69       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DR_MODE);
70    }
71 
72    if (groups & MESA_VK_GRAPHICS_STATE_RASTERIZATION_BIT) {
73       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_RASTERIZER_DISCARD_ENABLE);
74       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_DEPTH_CLAMP_ENABLE);
75       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_DEPTH_CLIP_ENABLE);
76       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_POLYGON_MODE);
77       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_CULL_MODE);
78       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_FRONT_FACE);
79       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_CONSERVATIVE_MODE);
80       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_RASTERIZATION_ORDER_AMD);
81       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_PROVOKING_VERTEX);
82       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_RASTERIZATION_STREAM);
83       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_DEPTH_BIAS_ENABLE);
84       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_DEPTH_BIAS_FACTORS);
85       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_LINE_WIDTH);
86       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_LINE_MODE);
87       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_LINE_STIPPLE_ENABLE);
88       BITSET_SET(dynamic, MESA_VK_DYNAMIC_RS_LINE_STIPPLE);
89    }
90 
91    if (groups & MESA_VK_GRAPHICS_STATE_FRAGMENT_SHADING_RATE_BIT)
92       BITSET_SET(dynamic, MESA_VK_DYNAMIC_FSR);
93 
94    if (groups & MESA_VK_GRAPHICS_STATE_MULTISAMPLE_BIT) {
95       BITSET_SET(dynamic, MESA_VK_DYNAMIC_MS_RASTERIZATION_SAMPLES);
96       BITSET_SET(dynamic, MESA_VK_DYNAMIC_MS_SAMPLE_MASK);
97       BITSET_SET(dynamic, MESA_VK_DYNAMIC_MS_ALPHA_TO_COVERAGE_ENABLE);
98       BITSET_SET(dynamic, MESA_VK_DYNAMIC_MS_ALPHA_TO_ONE_ENABLE);
99       BITSET_SET(dynamic, MESA_VK_DYNAMIC_MS_SAMPLE_LOCATIONS_ENABLE);
100       BITSET_SET(dynamic, MESA_VK_DYNAMIC_MS_SAMPLE_LOCATIONS);
101    }
102 
103    if (groups & MESA_VK_GRAPHICS_STATE_DEPTH_STENCIL_BIT) {
104       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_TEST_ENABLE);
105       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_WRITE_ENABLE);
106       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_COMPARE_OP);
107       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_BOUNDS_TEST_ENABLE);
108       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_BOUNDS_TEST_BOUNDS);
109       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_TEST_ENABLE);
110       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_OP);
111       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_COMPARE_MASK);
112       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_WRITE_MASK);
113       BITSET_SET(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_REFERENCE);
114    }
115 
116    if (groups & MESA_VK_GRAPHICS_STATE_COLOR_BLEND_BIT) {
117       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_LOGIC_OP_ENABLE);
118       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_LOGIC_OP);
119       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_ATTACHMENT_COUNT);
120       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_COLOR_WRITE_ENABLES);
121       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_BLEND_ENABLES);
122       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_BLEND_EQUATIONS);
123       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_WRITE_MASKS);
124       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_BLEND_CONSTANTS);
125    }
126 }
127 
128 static enum mesa_vk_graphics_state_groups
fully_dynamic_state_groups(const BITSET_WORD * dynamic)129 fully_dynamic_state_groups(const BITSET_WORD *dynamic)
130 {
131    enum mesa_vk_graphics_state_groups groups = 0;
132 
133    if (BITSET_TEST(dynamic, MESA_VK_DYNAMIC_VI) &&
134        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_VI_BINDING_STRIDES) &&
135        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_VI_BINDINGS_VALID))
136       groups |= MESA_VK_GRAPHICS_STATE_VERTEX_INPUT_BIT;
137 
138    if (BITSET_TEST(dynamic, MESA_VK_DYNAMIC_TS_PATCH_CONTROL_POINTS) &&
139        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_TS_DOMAIN_ORIGIN))
140       groups |= MESA_VK_GRAPHICS_STATE_TESSELLATION_BIT;
141 
142    if (BITSET_TEST(dynamic, MESA_VK_DYNAMIC_FSR))
143       groups |= MESA_VK_GRAPHICS_STATE_FRAGMENT_SHADING_RATE_BIT;
144 
145    if (BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_TEST_ENABLE) &&
146        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_WRITE_ENABLE) &&
147        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_COMPARE_OP) &&
148        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_BOUNDS_TEST_ENABLE) &&
149        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_DEPTH_BOUNDS_TEST_BOUNDS) &&
150        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_TEST_ENABLE) &&
151        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_OP) &&
152        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_COMPARE_MASK) &&
153        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_WRITE_MASK) &&
154        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_DS_STENCIL_REFERENCE))
155       groups |= MESA_VK_GRAPHICS_STATE_DEPTH_STENCIL_BIT;
156 
157    if (BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_LOGIC_OP_ENABLE) &&
158        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_LOGIC_OP) &&
159        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_ATTACHMENT_COUNT) &&
160        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_COLOR_WRITE_ENABLES) &&
161        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_BLEND_ENABLES) &&
162        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_BLEND_EQUATIONS) &&
163        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_WRITE_MASKS) &&
164        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_BLEND_CONSTANTS))
165       groups |= MESA_VK_GRAPHICS_STATE_COLOR_BLEND_BIT;
166 
167    return groups;
168 }
169 
170 static void
validate_dynamic_state_groups(const BITSET_WORD * dynamic,enum mesa_vk_graphics_state_groups groups)171 validate_dynamic_state_groups(const BITSET_WORD *dynamic,
172                               enum mesa_vk_graphics_state_groups groups)
173 {
174 #ifndef NDEBUG
175    BITSET_DECLARE(all_dynamic, MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX);
176    get_dynamic_state_groups(all_dynamic, groups);
177 
178    for (uint32_t w = 0; w < ARRAY_SIZE(all_dynamic); w++)
179       assert(!(dynamic[w] & ~all_dynamic[w]));
180 #endif
181 }
182 
183 void
vk_get_dynamic_graphics_states(BITSET_WORD * dynamic,const VkPipelineDynamicStateCreateInfo * info)184 vk_get_dynamic_graphics_states(BITSET_WORD *dynamic,
185                                const VkPipelineDynamicStateCreateInfo *info)
186 {
187    clear_all_dynamic_state(dynamic);
188 
189    /* From the Vulkan 1.3.218 spec:
190     *
191     *    "pDynamicState is a pointer to a VkPipelineDynamicStateCreateInfo
192     *    structure defining which properties of the pipeline state object are
193     *    dynamic and can be changed independently of the pipeline state. This
194     *    can be NULL, which means no state in the pipeline is considered
195     *    dynamic."
196     */
197    if (info == NULL)
198       return;
199 
200 #define CASE(VK, MESA) \
201    case VK_DYNAMIC_STATE_##VK: \
202       BITSET_SET(dynamic, MESA_VK_DYNAMIC_##MESA); \
203       break;
204 
205 #define CASE2(VK, MESA1, MESA2) \
206    case VK_DYNAMIC_STATE_##VK: \
207       BITSET_SET(dynamic, MESA_VK_DYNAMIC_##MESA1); \
208       BITSET_SET(dynamic, MESA_VK_DYNAMIC_##MESA2); \
209       break;
210 
211 #define CASE3(VK, MESA1, MESA2, MESA3) \
212    case VK_DYNAMIC_STATE_##VK: \
213       BITSET_SET(dynamic, MESA_VK_DYNAMIC_##MESA1); \
214       BITSET_SET(dynamic, MESA_VK_DYNAMIC_##MESA2); \
215       BITSET_SET(dynamic, MESA_VK_DYNAMIC_##MESA3); \
216       break;
217 
218    for (uint32_t i = 0; i < info->dynamicStateCount; i++) {
219       switch (info->pDynamicStates[i]) {
220       CASE3(VERTEX_INPUT_EXT,             VI, VI_BINDINGS_VALID, VI_BINDING_STRIDES)
221       CASE( VERTEX_INPUT_BINDING_STRIDE,  VI_BINDING_STRIDES)
222       CASE( VIEWPORT,                     VP_VIEWPORTS)
223       CASE( SCISSOR,                      VP_SCISSORS)
224       CASE( LINE_WIDTH,                   RS_LINE_WIDTH)
225       CASE( DEPTH_BIAS,                   RS_DEPTH_BIAS_FACTORS)
226       CASE( BLEND_CONSTANTS,              CB_BLEND_CONSTANTS)
227       CASE( DEPTH_BOUNDS,                 DS_DEPTH_BOUNDS_TEST_BOUNDS)
228       CASE( STENCIL_COMPARE_MASK,         DS_STENCIL_COMPARE_MASK)
229       CASE( STENCIL_WRITE_MASK,           DS_STENCIL_WRITE_MASK)
230       CASE( STENCIL_REFERENCE,            DS_STENCIL_REFERENCE)
231       CASE( CULL_MODE,                    RS_CULL_MODE)
232       CASE( FRONT_FACE,                   RS_FRONT_FACE)
233       CASE( PRIMITIVE_TOPOLOGY,           IA_PRIMITIVE_TOPOLOGY)
234       CASE2(VIEWPORT_WITH_COUNT,          VP_VIEWPORT_COUNT, VP_VIEWPORTS)
235       CASE2(SCISSOR_WITH_COUNT,           VP_SCISSOR_COUNT, VP_SCISSORS)
236       CASE( DEPTH_TEST_ENABLE,            DS_DEPTH_TEST_ENABLE)
237       CASE( DEPTH_WRITE_ENABLE,           DS_DEPTH_WRITE_ENABLE)
238       CASE( DEPTH_COMPARE_OP,             DS_DEPTH_COMPARE_OP)
239       CASE( DEPTH_BOUNDS_TEST_ENABLE,     DS_DEPTH_BOUNDS_TEST_ENABLE)
240       CASE( STENCIL_TEST_ENABLE,          DS_STENCIL_TEST_ENABLE)
241       CASE( STENCIL_OP,                   DS_STENCIL_OP)
242       CASE( RASTERIZER_DISCARD_ENABLE,    RS_RASTERIZER_DISCARD_ENABLE)
243       CASE( DEPTH_BIAS_ENABLE,            RS_DEPTH_BIAS_ENABLE)
244       CASE( PRIMITIVE_RESTART_ENABLE,     IA_PRIMITIVE_RESTART_ENABLE)
245       CASE( DISCARD_RECTANGLE_EXT,        DR_RECTANGLES)
246       CASE( DISCARD_RECTANGLE_ENABLE_EXT, DR_ENABLE)
247       CASE( DISCARD_RECTANGLE_MODE_EXT,   DR_MODE)
248       CASE( SAMPLE_LOCATIONS_EXT,         MS_SAMPLE_LOCATIONS)
249       CASE( FRAGMENT_SHADING_RATE_KHR,    FSR)
250       CASE( LINE_STIPPLE_EXT,             RS_LINE_STIPPLE)
251       CASE( PATCH_CONTROL_POINTS_EXT,     TS_PATCH_CONTROL_POINTS)
252       CASE( LOGIC_OP_EXT,                 CB_LOGIC_OP)
253       CASE( COLOR_WRITE_ENABLE_EXT,       CB_COLOR_WRITE_ENABLES)
254       CASE( TESSELLATION_DOMAIN_ORIGIN_EXT, TS_DOMAIN_ORIGIN)
255       CASE( DEPTH_CLAMP_ENABLE_EXT,       RS_DEPTH_CLAMP_ENABLE)
256       CASE( POLYGON_MODE_EXT,             RS_POLYGON_MODE)
257       CASE( RASTERIZATION_SAMPLES_EXT,    MS_RASTERIZATION_SAMPLES)
258       CASE( SAMPLE_MASK_EXT,              MS_SAMPLE_MASK)
259       CASE( ALPHA_TO_COVERAGE_ENABLE_EXT, MS_ALPHA_TO_COVERAGE_ENABLE)
260       CASE( ALPHA_TO_ONE_ENABLE_EXT,      MS_ALPHA_TO_ONE_ENABLE)
261       CASE( LOGIC_OP_ENABLE_EXT,          CB_LOGIC_OP_ENABLE)
262       CASE( COLOR_BLEND_ENABLE_EXT,       CB_BLEND_ENABLES)
263       CASE( COLOR_BLEND_EQUATION_EXT,     CB_BLEND_EQUATIONS)
264       CASE( COLOR_WRITE_MASK_EXT,         CB_WRITE_MASKS)
265       CASE( RASTERIZATION_STREAM_EXT,     RS_RASTERIZATION_STREAM)
266       CASE( CONSERVATIVE_RASTERIZATION_MODE_EXT, RS_CONSERVATIVE_MODE)
267       CASE( DEPTH_CLIP_ENABLE_EXT,        RS_DEPTH_CLIP_ENABLE)
268       CASE( SAMPLE_LOCATIONS_ENABLE_EXT,  MS_SAMPLE_LOCATIONS_ENABLE)
269       CASE( PROVOKING_VERTEX_MODE_EXT,    RS_PROVOKING_VERTEX)
270       CASE( LINE_RASTERIZATION_MODE_EXT,  RS_LINE_MODE)
271       CASE( LINE_STIPPLE_ENABLE_EXT,      RS_LINE_STIPPLE_ENABLE)
272       CASE( DEPTH_CLIP_NEGATIVE_ONE_TO_ONE_EXT, VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE)
273       CASE( ATTACHMENT_FEEDBACK_LOOP_ENABLE_EXT, ATTACHMENT_FEEDBACK_LOOP_ENABLE)
274       default:
275          unreachable("Unsupported dynamic graphics state");
276       }
277    }
278 
279    /* attachmentCount is ignored if all of the states using it are dyanmic.
280     *
281     * TODO: Handle advanced blending here when supported.
282     */
283    if (BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_BLEND_ENABLES) &&
284        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_BLEND_EQUATIONS) &&
285        BITSET_TEST(dynamic, MESA_VK_DYNAMIC_CB_WRITE_MASKS))
286       BITSET_SET(dynamic, MESA_VK_DYNAMIC_CB_ATTACHMENT_COUNT);
287 }
288 
289 #define IS_DYNAMIC(STATE) \
290    BITSET_TEST(dynamic, MESA_VK_DYNAMIC_##STATE)
291 
292 #define IS_NEEDED(STATE) \
293    BITSET_TEST(needed, MESA_VK_DYNAMIC_##STATE)
294 
295 static void
vk_vertex_input_state_init(struct vk_vertex_input_state * vi,const BITSET_WORD * dynamic,const VkPipelineVertexInputStateCreateInfo * vi_info)296 vk_vertex_input_state_init(struct vk_vertex_input_state *vi,
297                            const BITSET_WORD *dynamic,
298                            const VkPipelineVertexInputStateCreateInfo *vi_info)
299 {
300    assert(!IS_DYNAMIC(VI));
301 
302    memset(vi, 0, sizeof(*vi));
303    if (!vi_info)
304       return;
305 
306    for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
307       const VkVertexInputBindingDescription *desc =
308          &vi_info->pVertexBindingDescriptions[i];
309 
310       assert(desc->binding < MESA_VK_MAX_VERTEX_BINDINGS);
311       assert(desc->stride <= MESA_VK_MAX_VERTEX_BINDING_STRIDE);
312       assert(desc->inputRate <= 1);
313 
314       const uint32_t b = desc->binding;
315       vi->bindings_valid |= BITFIELD_BIT(b);
316       vi->bindings[b].stride = desc->stride;
317       vi->bindings[b].input_rate = desc->inputRate;
318       vi->bindings[b].divisor = 1;
319    }
320 
321    for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
322       const VkVertexInputAttributeDescription *desc =
323          &vi_info->pVertexAttributeDescriptions[i];
324 
325       assert(desc->location < MESA_VK_MAX_VERTEX_ATTRIBUTES);
326       assert(desc->binding < MESA_VK_MAX_VERTEX_BINDINGS);
327       assert(vi->bindings_valid & BITFIELD_BIT(desc->binding));
328 
329       const uint32_t a = desc->location;
330       vi->attributes_valid |= BITFIELD_BIT(a);
331       vi->attributes[a].binding = desc->binding;
332       vi->attributes[a].format = desc->format;
333       vi->attributes[a].offset = desc->offset;
334    }
335 
336    const VkPipelineVertexInputDivisorStateCreateInfoEXT *vi_div_state =
337       vk_find_struct_const(vi_info->pNext,
338                            PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT);
339    if (vi_div_state) {
340       for (uint32_t i = 0; i < vi_div_state->vertexBindingDivisorCount; i++) {
341          const VkVertexInputBindingDivisorDescriptionEXT *desc =
342             &vi_div_state->pVertexBindingDivisors[i];
343 
344          assert(desc->binding < MESA_VK_MAX_VERTEX_BINDINGS);
345          assert(vi->bindings_valid & BITFIELD_BIT(desc->binding));
346 
347          const uint32_t b = desc->binding;
348          vi->bindings[b].divisor = desc->divisor;
349       }
350    }
351 }
352 
353 static void
vk_dynamic_graphics_state_init_vi(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_vertex_input_state * vi)354 vk_dynamic_graphics_state_init_vi(struct vk_dynamic_graphics_state *dst,
355                                   const BITSET_WORD *needed,
356                                   const struct vk_vertex_input_state *vi)
357 {
358    if (IS_NEEDED(VI))
359       *dst->vi = *vi;
360 
361    if (IS_NEEDED(VI_BINDINGS_VALID))
362       dst->vi_bindings_valid = vi->bindings_valid;
363 
364    if (IS_NEEDED(VI_BINDING_STRIDES)) {
365       for (uint32_t b = 0; b < MESA_VK_MAX_VERTEX_BINDINGS; b++) {
366          if (vi->bindings_valid & BITFIELD_BIT(b))
367             dst->vi_binding_strides[b] = vi->bindings[b].stride;
368          else
369             dst->vi_binding_strides[b] = 0;
370       }
371    }
372 }
373 
374 static void
vk_input_assembly_state_init(struct vk_input_assembly_state * ia,const BITSET_WORD * dynamic,const VkPipelineInputAssemblyStateCreateInfo * ia_info)375 vk_input_assembly_state_init(struct vk_input_assembly_state *ia,
376                              const BITSET_WORD *dynamic,
377                              const VkPipelineInputAssemblyStateCreateInfo *ia_info)
378 {
379    memset(ia, 0, sizeof(*ia));
380    if (!ia_info)
381       return;
382 
383    /* From the Vulkan 1.3.224 spec:
384     *
385     *    "VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY specifies that the topology
386     *    state in VkPipelineInputAssemblyStateCreateInfo only specifies the
387     *    topology class, and the specific topology order and adjacency must be
388     *    set dynamically with vkCmdSetPrimitiveTopology before any drawing
389     *    commands."
390    */
391    assert(ia_info->topology <= UINT8_MAX);
392    ia->primitive_topology = ia_info->topology;
393 
394    ia->primitive_restart_enable = ia_info->primitiveRestartEnable;
395 }
396 
397 static void
vk_dynamic_graphics_state_init_ia(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_input_assembly_state * ia)398 vk_dynamic_graphics_state_init_ia(struct vk_dynamic_graphics_state *dst,
399                                   const BITSET_WORD *needed,
400                                   const struct vk_input_assembly_state *ia)
401 {
402    dst->ia = *ia;
403 }
404 
405 static void
vk_tessellation_state_init(struct vk_tessellation_state * ts,const BITSET_WORD * dynamic,const VkPipelineTessellationStateCreateInfo * ts_info)406 vk_tessellation_state_init(struct vk_tessellation_state *ts,
407                            const BITSET_WORD *dynamic,
408                            const VkPipelineTessellationStateCreateInfo *ts_info)
409 {
410    *ts = (struct vk_tessellation_state) {
411       .domain_origin = VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT,
412    };
413    if (!ts_info)
414       return;
415 
416    if (!IS_DYNAMIC(TS_PATCH_CONTROL_POINTS)) {
417       assert(ts_info->patchControlPoints <= UINT8_MAX);
418       ts->patch_control_points = ts_info->patchControlPoints;
419    }
420 
421    if (!IS_DYNAMIC(TS_DOMAIN_ORIGIN)) {
422       const VkPipelineTessellationDomainOriginStateCreateInfo *ts_do_info =
423          vk_find_struct_const(ts_info->pNext,
424                               PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO);
425       if (ts_do_info != NULL) {
426          assert(ts_do_info->domainOrigin <= UINT8_MAX);
427          ts->domain_origin = ts_do_info->domainOrigin;
428       }
429    }
430 }
431 
432 static void
vk_dynamic_graphics_state_init_ts(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_tessellation_state * ts)433 vk_dynamic_graphics_state_init_ts(struct vk_dynamic_graphics_state *dst,
434                                   const BITSET_WORD *needed,
435                                   const struct vk_tessellation_state *ts)
436 {
437    dst->ts = *ts;
438 }
439 
440 static void
vk_viewport_state_init(struct vk_viewport_state * vp,const BITSET_WORD * dynamic,const VkPipelineViewportStateCreateInfo * vp_info)441 vk_viewport_state_init(struct vk_viewport_state *vp,
442                        const BITSET_WORD *dynamic,
443                        const VkPipelineViewportStateCreateInfo *vp_info)
444 {
445    memset(vp, 0, sizeof(*vp));
446    if (!vp_info)
447       return;
448 
449    if (!IS_DYNAMIC(VP_VIEWPORT_COUNT)) {
450       assert(vp_info->viewportCount <= MESA_VK_MAX_VIEWPORTS);
451       vp->viewport_count = vp_info->viewportCount;
452    }
453 
454    if (!IS_DYNAMIC(VP_VIEWPORTS)) {
455       assert(!IS_DYNAMIC(VP_VIEWPORT_COUNT));
456       typed_memcpy(vp->viewports, vp_info->pViewports,
457                    vp_info->viewportCount);
458    }
459 
460    if (!IS_DYNAMIC(VP_SCISSOR_COUNT)) {
461       assert(vp_info->scissorCount <= MESA_VK_MAX_SCISSORS);
462       vp->scissor_count = vp_info->scissorCount;
463    }
464 
465    if (!IS_DYNAMIC(VP_SCISSORS)) {
466       assert(!IS_DYNAMIC(VP_SCISSOR_COUNT));
467       typed_memcpy(vp->scissors, vp_info->pScissors,
468                    vp_info->scissorCount);
469    }
470 
471    if (!IS_DYNAMIC(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE)) {
472       const VkPipelineViewportDepthClipControlCreateInfoEXT *vp_dcc_info =
473          vk_find_struct_const(vp_info->pNext,
474                               PIPELINE_VIEWPORT_DEPTH_CLIP_CONTROL_CREATE_INFO_EXT);
475       if (vp_dcc_info != NULL)
476          vp->depth_clip_negative_one_to_one = vp_dcc_info->negativeOneToOne;
477    }
478 }
479 
480 static void
vk_dynamic_graphics_state_init_vp(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_viewport_state * vp)481 vk_dynamic_graphics_state_init_vp(struct vk_dynamic_graphics_state *dst,
482                                   const BITSET_WORD *needed,
483                                   const struct vk_viewport_state *vp)
484 {
485    dst->vp.viewport_count = vp->viewport_count;
486    if (IS_NEEDED(VP_VIEWPORTS))
487       typed_memcpy(dst->vp.viewports, vp->viewports, vp->viewport_count);
488 
489    dst->vp.scissor_count = vp->scissor_count;
490    if (IS_NEEDED(VP_SCISSORS))
491       typed_memcpy(dst->vp.scissors, vp->scissors, vp->scissor_count);
492 
493    dst->vp.depth_clip_negative_one_to_one = vp->depth_clip_negative_one_to_one;
494 }
495 
496 static void
vk_discard_rectangles_state_init(struct vk_discard_rectangles_state * dr,const BITSET_WORD * dynamic,const VkPipelineDiscardRectangleStateCreateInfoEXT * dr_info)497 vk_discard_rectangles_state_init(struct vk_discard_rectangles_state *dr,
498                                  const BITSET_WORD *dynamic,
499                                  const VkPipelineDiscardRectangleStateCreateInfoEXT *dr_info)
500 {
501    memset(dr, 0, sizeof(*dr));
502 
503    if (dr_info == NULL)
504       return;
505 
506    assert(dr_info->discardRectangleCount <= MESA_VK_MAX_DISCARD_RECTANGLES);
507    dr->mode = dr_info->discardRectangleMode;
508    dr->rectangle_count = dr_info->discardRectangleCount;
509 
510    if (!IS_DYNAMIC(DR_RECTANGLES)) {
511       typed_memcpy(dr->rectangles, dr_info->pDiscardRectangles,
512                    dr_info->discardRectangleCount);
513    }
514 }
515 
516 static void
vk_dynamic_graphics_state_init_dr(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_discard_rectangles_state * dr)517 vk_dynamic_graphics_state_init_dr(struct vk_dynamic_graphics_state *dst,
518                                   const BITSET_WORD *needed,
519                                   const struct vk_discard_rectangles_state *dr)
520 {
521    dst->dr.enable = dr->rectangle_count > 0;
522    dst->dr.mode = dr->mode;
523    dst->dr.rectangle_count = dr->rectangle_count;
524    typed_memcpy(dst->dr.rectangles, dr->rectangles, dr->rectangle_count);
525 }
526 
527 static void
vk_rasterization_state_init(struct vk_rasterization_state * rs,const BITSET_WORD * dynamic,const VkPipelineRasterizationStateCreateInfo * rs_info)528 vk_rasterization_state_init(struct vk_rasterization_state *rs,
529                             const BITSET_WORD *dynamic,
530                             const VkPipelineRasterizationStateCreateInfo *rs_info)
531 {
532    *rs = (struct vk_rasterization_state) {
533       .rasterizer_discard_enable = false,
534       .conservative_mode = VK_CONSERVATIVE_RASTERIZATION_MODE_DISABLED_EXT,
535       .extra_primitive_overestimation_size = 0.0f,
536       .rasterization_order_amd = VK_RASTERIZATION_ORDER_STRICT_AMD,
537       .provoking_vertex = VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT,
538       .line.mode = VK_LINE_RASTERIZATION_MODE_DEFAULT_EXT,
539       .depth_clip_enable = IS_DYNAMIC(RS_DEPTH_CLAMP_ENABLE) ? VK_MESA_DEPTH_CLIP_ENABLE_NOT_CLAMP : VK_MESA_DEPTH_CLIP_ENABLE_FALSE,
540       .depth_bias.representation = VK_DEPTH_BIAS_REPRESENTATION_LEAST_REPRESENTABLE_VALUE_FORMAT_EXT,
541       .depth_bias.exact = false,
542    };
543    if (!rs_info)
544       return;
545 
546    if (!IS_DYNAMIC(RS_RASTERIZER_DISCARD_ENABLE))
547       rs->rasterizer_discard_enable = rs_info->rasterizerDiscardEnable;
548 
549    /* From the Vulkan 1.3.218 spec:
550     *
551     *    "If VkPipelineRasterizationDepthClipStateCreateInfoEXT is present in
552     *    the graphics pipeline state then depth clipping is disabled if
553     *    VkPipelineRasterizationDepthClipStateCreateInfoEXT::depthClipEnable
554     *    is VK_FALSE. Otherwise, if
555     *    VkPipelineRasterizationDepthClipStateCreateInfoEXT is not present,
556     *    depth clipping is disabled when
557     *    VkPipelineRasterizationStateCreateInfo::depthClampEnable is VK_TRUE.
558     */
559    if (!IS_DYNAMIC(RS_DEPTH_CLAMP_ENABLE)) {
560       rs->depth_clamp_enable = rs_info->depthClampEnable;
561       rs->depth_clip_enable = rs_info->depthClampEnable ?
562                               VK_MESA_DEPTH_CLIP_ENABLE_FALSE :
563                               VK_MESA_DEPTH_CLIP_ENABLE_TRUE;
564    }
565 
566    rs->polygon_mode = rs_info->polygonMode;
567 
568    rs->cull_mode = rs_info->cullMode;
569    rs->front_face = rs_info->frontFace;
570    rs->depth_bias.enable = rs_info->depthBiasEnable;
571    if ((rs_info->depthBiasEnable || IS_DYNAMIC(RS_DEPTH_BIAS_ENABLE)) &&
572        !IS_DYNAMIC(RS_DEPTH_BIAS_FACTORS)) {
573       rs->depth_bias.constant = rs_info->depthBiasConstantFactor;
574       rs->depth_bias.clamp = rs_info->depthBiasClamp;
575       rs->depth_bias.slope = rs_info->depthBiasSlopeFactor;
576    }
577    rs->line.width = rs_info->lineWidth;
578 
579    vk_foreach_struct_const(ext, rs_info->pNext) {
580       switch (ext->sType) {
581       case VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_CONSERVATIVE_STATE_CREATE_INFO_EXT: {
582          const VkPipelineRasterizationConservativeStateCreateInfoEXT *rcs_info =
583             (const VkPipelineRasterizationConservativeStateCreateInfoEXT *)ext;
584          rs->conservative_mode = rcs_info->conservativeRasterizationMode;
585          rs->extra_primitive_overestimation_size =
586             rcs_info->extraPrimitiveOverestimationSize;
587          break;
588       }
589 
590       case VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT: {
591          const VkPipelineRasterizationDepthClipStateCreateInfoEXT *rdc_info =
592             (const VkPipelineRasterizationDepthClipStateCreateInfoEXT *)ext;
593          rs->depth_clip_enable = rdc_info->depthClipEnable ?
594                                  VK_MESA_DEPTH_CLIP_ENABLE_TRUE :
595                                  VK_MESA_DEPTH_CLIP_ENABLE_FALSE;
596          break;
597       }
598 
599       case VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT: {
600          const VkPipelineRasterizationLineStateCreateInfoEXT *rl_info =
601             (const VkPipelineRasterizationLineStateCreateInfoEXT *)ext;
602          rs->line.mode = rl_info->lineRasterizationMode;
603          if (!IS_DYNAMIC(RS_LINE_STIPPLE_ENABLE))
604             rs->line.stipple.enable = rl_info->stippledLineEnable;
605          if ((IS_DYNAMIC(RS_LINE_STIPPLE_ENABLE) || rs->line.stipple.enable) && !IS_DYNAMIC(RS_LINE_STIPPLE)) {
606             rs->line.stipple.factor = rl_info->lineStippleFactor;
607             rs->line.stipple.pattern = rl_info->lineStipplePattern;
608          }
609          break;
610       }
611 
612       case VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_PROVOKING_VERTEX_STATE_CREATE_INFO_EXT: {
613          const VkPipelineRasterizationProvokingVertexStateCreateInfoEXT *rpv_info =
614             (const VkPipelineRasterizationProvokingVertexStateCreateInfoEXT *)ext;
615          rs->provoking_vertex = rpv_info->provokingVertexMode;
616          break;
617       }
618 
619       case VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD: {
620          const VkPipelineRasterizationStateRasterizationOrderAMD *rro_info =
621             (const VkPipelineRasterizationStateRasterizationOrderAMD *)ext;
622          rs->rasterization_order_amd = rro_info->rasterizationOrder;
623          break;
624       }
625 
626       case VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_STREAM_CREATE_INFO_EXT: {
627          const VkPipelineRasterizationStateStreamCreateInfoEXT *rss_info =
628             (const VkPipelineRasterizationStateStreamCreateInfoEXT *)ext;
629          rs->rasterization_stream = rss_info->rasterizationStream;
630          break;
631       }
632 
633       case VK_STRUCTURE_TYPE_DEPTH_BIAS_REPRESENTATION_INFO_EXT: {
634          const VkDepthBiasRepresentationInfoEXT *dbr_info =
635             (const VkDepthBiasRepresentationInfoEXT *)ext;
636          if (!IS_DYNAMIC(RS_DEPTH_BIAS_FACTORS)) {
637             rs->depth_bias.representation = dbr_info->depthBiasRepresentation;
638             rs->depth_bias.exact = dbr_info->depthBiasExact;
639          }
640          break;
641       }
642 
643       default:
644          break;
645       }
646    }
647 }
648 
649 static void
vk_dynamic_graphics_state_init_rs(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_rasterization_state * rs)650 vk_dynamic_graphics_state_init_rs(struct vk_dynamic_graphics_state *dst,
651                                   const BITSET_WORD *needed,
652                                   const struct vk_rasterization_state *rs)
653 {
654    dst->rs = *rs;
655 }
656 
657 static void
vk_fragment_shading_rate_state_init(struct vk_fragment_shading_rate_state * fsr,const BITSET_WORD * dynamic,const VkPipelineFragmentShadingRateStateCreateInfoKHR * fsr_info)658 vk_fragment_shading_rate_state_init(
659    struct vk_fragment_shading_rate_state *fsr,
660    const BITSET_WORD *dynamic,
661    const VkPipelineFragmentShadingRateStateCreateInfoKHR *fsr_info)
662 {
663    if (fsr_info != NULL) {
664       fsr->fragment_size = fsr_info->fragmentSize;
665       fsr->combiner_ops[0] = fsr_info->combinerOps[0];
666       fsr->combiner_ops[1] = fsr_info->combinerOps[1];
667    } else {
668       fsr->fragment_size = (VkExtent2D) { 1, 1 };
669       fsr->combiner_ops[0] = VK_FRAGMENT_SHADING_RATE_COMBINER_OP_KEEP_KHR;
670       fsr->combiner_ops[1] = VK_FRAGMENT_SHADING_RATE_COMBINER_OP_KEEP_KHR;
671    }
672 }
673 
674 static void
vk_dynamic_graphics_state_init_fsr(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_fragment_shading_rate_state * fsr)675 vk_dynamic_graphics_state_init_fsr(
676    struct vk_dynamic_graphics_state *dst,
677    const BITSET_WORD *needed,
678    const struct vk_fragment_shading_rate_state *fsr)
679 {
680    dst->fsr = *fsr;
681 }
682 
683 static void
vk_sample_locations_state_init(struct vk_sample_locations_state * sl,const VkSampleLocationsInfoEXT * sl_info)684 vk_sample_locations_state_init(struct vk_sample_locations_state *sl,
685                                const VkSampleLocationsInfoEXT *sl_info)
686 {
687    sl->per_pixel = sl_info->sampleLocationsPerPixel;
688    sl->grid_size = sl_info->sampleLocationGridSize;
689 
690    /* From the Vulkan 1.3.218 spec:
691     *
692     *    VUID-VkSampleLocationsInfoEXT-sampleLocationsCount-01527
693     *
694     *    "sampleLocationsCount must equal sampleLocationsPerPixel *
695     *    sampleLocationGridSize.width * sampleLocationGridSize.height"
696     */
697    assert(sl_info->sampleLocationsCount ==
698           sl_info->sampleLocationsPerPixel *
699           sl_info->sampleLocationGridSize.width *
700           sl_info->sampleLocationGridSize.height);
701 
702    assert(sl_info->sampleLocationsCount <= MESA_VK_MAX_SAMPLE_LOCATIONS);
703    typed_memcpy(sl->locations, sl_info->pSampleLocations,
704                 sl_info->sampleLocationsCount);
705 }
706 
707 static void
vk_multisample_state_init(struct vk_multisample_state * ms,const BITSET_WORD * dynamic,const VkPipelineMultisampleStateCreateInfo * ms_info)708 vk_multisample_state_init(struct vk_multisample_state *ms,
709                           const BITSET_WORD *dynamic,
710                           const VkPipelineMultisampleStateCreateInfo *ms_info)
711 {
712    memset(ms, 0, sizeof(*ms));
713    if (!ms_info)
714       return;
715 
716    if (!IS_DYNAMIC(MS_RASTERIZATION_SAMPLES)) {
717       assert(ms_info->rasterizationSamples <= MESA_VK_MAX_SAMPLES);
718       ms->rasterization_samples = ms_info->rasterizationSamples;
719    }
720 
721    ms->sample_shading_enable = ms_info->sampleShadingEnable;
722    ms->min_sample_shading = ms_info->minSampleShading;
723 
724    /* From the Vulkan 1.3.218 spec:
725     *
726     *    "If pSampleMask is NULL, it is treated as if the mask has all bits
727     *    set to 1."
728     */
729    ms->sample_mask = ms_info->pSampleMask ? *ms_info->pSampleMask : ~0;
730 
731    ms->alpha_to_coverage_enable = ms_info->alphaToCoverageEnable;
732    ms->alpha_to_one_enable = ms_info->alphaToOneEnable;
733 
734    /* These get filled in by vk_multisample_sample_locations_state_init() */
735    ms->sample_locations_enable = false;
736    ms->sample_locations = NULL;
737 }
738 
739 static bool
needs_sample_locations_state(const BITSET_WORD * dynamic,const VkPipelineSampleLocationsStateCreateInfoEXT * sl_info)740 needs_sample_locations_state(
741    const BITSET_WORD *dynamic,
742    const VkPipelineSampleLocationsStateCreateInfoEXT *sl_info)
743 {
744    return !IS_DYNAMIC(MS_SAMPLE_LOCATIONS) &&
745           (IS_DYNAMIC(MS_SAMPLE_LOCATIONS_ENABLE) ||
746            (sl_info != NULL && sl_info->sampleLocationsEnable));
747 }
748 
749 static void
vk_multisample_sample_locations_state_init(struct vk_multisample_state * ms,struct vk_sample_locations_state * sl,const BITSET_WORD * dynamic,const VkPipelineMultisampleStateCreateInfo * ms_info,const VkPipelineSampleLocationsStateCreateInfoEXT * sl_info)750 vk_multisample_sample_locations_state_init(
751    struct vk_multisample_state *ms,
752    struct vk_sample_locations_state *sl,
753    const BITSET_WORD *dynamic,
754    const VkPipelineMultisampleStateCreateInfo *ms_info,
755    const VkPipelineSampleLocationsStateCreateInfoEXT *sl_info)
756 {
757    ms->sample_locations_enable =
758       IS_DYNAMIC(MS_SAMPLE_LOCATIONS_ENABLE) ||
759       (sl_info != NULL && sl_info->sampleLocationsEnable);
760 
761    assert(ms->sample_locations == NULL);
762    if (!IS_DYNAMIC(MS_SAMPLE_LOCATIONS)) {
763       if (ms->sample_locations_enable) {
764          vk_sample_locations_state_init(sl, &sl_info->sampleLocationsInfo);
765          ms->sample_locations = sl;
766       } else if (!IS_DYNAMIC(MS_RASTERIZATION_SAMPLES)) {
767          /* Otherwise, pre-populate with the standard sample locations.  If
768           * the driver doesn't support standard sample locations, it probably
769           * doesn't support custom locations either and can completely ignore
770           * this state.
771           */
772          ms->sample_locations =
773             vk_standard_sample_locations_state(ms_info->rasterizationSamples);
774       }
775    }
776 }
777 
778 static void
vk_dynamic_graphics_state_init_ms(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_multisample_state * ms)779 vk_dynamic_graphics_state_init_ms(struct vk_dynamic_graphics_state *dst,
780                                   const BITSET_WORD *needed,
781                                   const struct vk_multisample_state *ms)
782 {
783    dst->ms.rasterization_samples = ms->rasterization_samples;
784    dst->ms.sample_mask = ms->sample_mask;
785    dst->ms.alpha_to_coverage_enable = ms->alpha_to_coverage_enable;
786    dst->ms.alpha_to_one_enable = ms->alpha_to_one_enable;
787    dst->ms.sample_locations_enable = ms->sample_locations_enable;
788 
789    if (IS_NEEDED(MS_SAMPLE_LOCATIONS))
790       *dst->ms.sample_locations = *ms->sample_locations;
791 }
792 
793 static void
vk_stencil_test_face_state_init(struct vk_stencil_test_face_state * face,const VkStencilOpState * info)794 vk_stencil_test_face_state_init(struct vk_stencil_test_face_state *face,
795                                 const VkStencilOpState *info)
796 {
797    face->op.fail = info->failOp;
798    face->op.pass = info->passOp;
799    face->op.depth_fail = info->depthFailOp;
800    face->op.compare = info->compareOp;
801    face->compare_mask = info->compareMask;
802    face->write_mask = info->writeMask;
803    face->reference = info->reference;
804 }
805 
806 static void
vk_depth_stencil_state_init(struct vk_depth_stencil_state * ds,const BITSET_WORD * dynamic,const VkPipelineDepthStencilStateCreateInfo * ds_info)807 vk_depth_stencil_state_init(struct vk_depth_stencil_state *ds,
808                             const BITSET_WORD *dynamic,
809                             const VkPipelineDepthStencilStateCreateInfo *ds_info)
810 {
811    *ds = (struct vk_depth_stencil_state) {
812       .stencil.write_enable = true,
813    };
814    if (!ds_info)
815       return;
816 
817    ds->depth.test_enable = ds_info->depthTestEnable;
818    ds->depth.write_enable = ds_info->depthWriteEnable;
819    ds->depth.compare_op = ds_info->depthCompareOp;
820    ds->depth.bounds_test.enable = ds_info->depthBoundsTestEnable;
821    ds->depth.bounds_test.min = ds_info->minDepthBounds;
822    ds->depth.bounds_test.max = ds_info->maxDepthBounds;
823    ds->stencil.test_enable = ds_info->stencilTestEnable;
824    vk_stencil_test_face_state_init(&ds->stencil.front, &ds_info->front);
825    vk_stencil_test_face_state_init(&ds->stencil.back, &ds_info->back);
826 }
827 
828 static void
vk_dynamic_graphics_state_init_ds(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_depth_stencil_state * ds)829 vk_dynamic_graphics_state_init_ds(struct vk_dynamic_graphics_state *dst,
830                                   const BITSET_WORD *needed,
831                                   const struct vk_depth_stencil_state *ds)
832 {
833    dst->ds = *ds;
834 }
835 
836 static bool
optimize_stencil_face(struct vk_stencil_test_face_state * face,VkCompareOp depthCompareOp,bool consider_write_mask)837 optimize_stencil_face(struct vk_stencil_test_face_state *face,
838                       VkCompareOp depthCompareOp,
839                       bool consider_write_mask)
840 {
841    /* If compareOp is ALWAYS then the stencil test will never fail and failOp
842     * will never happen.  Set failOp to KEEP in this case.
843     */
844    if (face->op.compare == VK_COMPARE_OP_ALWAYS)
845       face->op.fail = VK_STENCIL_OP_KEEP;
846 
847    /* If compareOp is NEVER or depthCompareOp is NEVER then one of the depth
848     * or stencil tests will fail and passOp will never happen.
849     */
850    if (face->op.compare == VK_COMPARE_OP_NEVER ||
851        depthCompareOp == VK_COMPARE_OP_NEVER)
852       face->op.pass = VK_STENCIL_OP_KEEP;
853 
854    /* If compareOp is NEVER or depthCompareOp is ALWAYS then either the
855     * stencil test will fail or the depth test will pass.  In either case,
856     * depthFailOp will never happen.
857     */
858    if (face->op.compare == VK_COMPARE_OP_NEVER ||
859        depthCompareOp == VK_COMPARE_OP_ALWAYS)
860       face->op.depth_fail = VK_STENCIL_OP_KEEP;
861 
862    /* If the write mask is zero, nothing will be written to the stencil buffer
863     * so it's as if all operations are KEEP.
864     */
865    if (consider_write_mask && face->write_mask == 0) {
866       face->op.pass = VK_STENCIL_OP_KEEP;
867       face->op.fail = VK_STENCIL_OP_KEEP;
868       face->op.depth_fail = VK_STENCIL_OP_KEEP;
869    }
870 
871    return face->op.fail != VK_STENCIL_OP_KEEP ||
872           face->op.depth_fail != VK_STENCIL_OP_KEEP ||
873           face->op.pass != VK_STENCIL_OP_KEEP;
874 }
875 
876 void
vk_optimize_depth_stencil_state(struct vk_depth_stencil_state * ds,VkImageAspectFlags ds_aspects,bool consider_write_mask)877 vk_optimize_depth_stencil_state(struct vk_depth_stencil_state *ds,
878                                 VkImageAspectFlags ds_aspects,
879                                 bool consider_write_mask)
880 {
881    /* stencil.write_enable is a dummy right now that should always be true */
882    assert(ds->stencil.write_enable);
883 
884    /* From the Vulkan 1.3.221 spec:
885     *
886     *    "If there is no depth attachment then the depth test is skipped."
887     */
888    if (!(ds_aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
889       ds->depth.test_enable = false;
890 
891    /* From the Vulkan 1.3.221 spec:
892     *
893     *    "...or if there is no stencil attachment, the coverage mask is
894     *    unmodified by this operation."
895     */
896    if (!(ds_aspects & VK_IMAGE_ASPECT_STENCIL_BIT))
897       ds->stencil.test_enable = false;
898 
899    /* If the depth test is disabled, we won't be writing anything. Make sure we
900     * treat the test as always passing later on as well.
901     */
902    if (!ds->depth.test_enable) {
903       ds->depth.write_enable = false;
904       ds->depth.compare_op = VK_COMPARE_OP_ALWAYS;
905    }
906 
907    /* If the stencil test is disabled, we won't be writing anything. Make sure
908     * we treat the test as always passing later on as well.
909     */
910    if (!ds->stencil.test_enable) {
911       ds->stencil.write_enable = false;
912       ds->stencil.front.op.compare = VK_COMPARE_OP_ALWAYS;
913       ds->stencil.back.op.compare = VK_COMPARE_OP_ALWAYS;
914    }
915 
916    /* If the stencil test is enabled and always fails, then we will never get
917     * to the depth test so we can just disable the depth test entirely.
918     */
919    if (ds->stencil.test_enable &&
920        ds->stencil.front.op.compare == VK_COMPARE_OP_NEVER &&
921        ds->stencil.back.op.compare == VK_COMPARE_OP_NEVER) {
922       ds->depth.test_enable = false;
923       ds->depth.write_enable = false;
924    }
925 
926    /* If depthCompareOp is EQUAL then the value we would be writing to the
927     * depth buffer is the same as the value that's already there so there's no
928     * point in writing it.
929     */
930    if (ds->depth.compare_op == VK_COMPARE_OP_EQUAL)
931       ds->depth.write_enable = false;
932 
933    /* If the stencil ops are such that we don't actually ever modify the
934     * stencil buffer, we should disable writes.
935     */
936    if (!optimize_stencil_face(&ds->stencil.front, ds->depth.compare_op,
937                               consider_write_mask) &&
938        !optimize_stencil_face(&ds->stencil.back, ds->depth.compare_op,
939                               consider_write_mask))
940       ds->stencil.write_enable = false;
941 
942    /* If the depth test always passes and we never write out depth, that's the
943     * same as if the depth test is disabled entirely.
944     */
945    if (ds->depth.compare_op == VK_COMPARE_OP_ALWAYS && !ds->depth.write_enable)
946       ds->depth.test_enable = false;
947 
948    /* If the stencil test always passes and we never write out stencil, that's
949     * the same as if the stencil test is disabled entirely.
950     */
951    if (ds->stencil.front.op.compare == VK_COMPARE_OP_ALWAYS &&
952        ds->stencil.back.op.compare == VK_COMPARE_OP_ALWAYS &&
953        !ds->stencil.write_enable)
954       ds->stencil.test_enable = false;
955 }
956 
957 static void
vk_color_blend_state_init(struct vk_color_blend_state * cb,const BITSET_WORD * dynamic,const VkPipelineColorBlendStateCreateInfo * cb_info)958 vk_color_blend_state_init(struct vk_color_blend_state *cb,
959                           const BITSET_WORD *dynamic,
960                           const VkPipelineColorBlendStateCreateInfo *cb_info)
961 {
962    *cb = (struct vk_color_blend_state) {
963       .color_write_enables = BITFIELD_MASK(MESA_VK_MAX_COLOR_ATTACHMENTS),
964    };
965    if (!cb_info)
966       return;
967 
968    cb->logic_op_enable = cb_info->logicOpEnable;
969    cb->logic_op = cb_info->logicOp;
970 
971    assert(cb_info->attachmentCount <= MESA_VK_MAX_COLOR_ATTACHMENTS);
972    cb->attachment_count = cb_info->attachmentCount;
973    /* pAttachments is ignored if any of these is not set */
974    bool full_dynamic = IS_DYNAMIC(CB_BLEND_ENABLES) && IS_DYNAMIC(CB_BLEND_EQUATIONS) && IS_DYNAMIC(CB_WRITE_MASKS);
975    for (uint32_t a = 0; a < cb_info->attachmentCount; a++) {
976       const VkPipelineColorBlendAttachmentState *att = full_dynamic ? NULL : &cb_info->pAttachments[a];
977 
978       cb->attachments[a] = (struct vk_color_blend_attachment_state) {
979          .blend_enable = IS_DYNAMIC(CB_BLEND_ENABLES) || att->blendEnable,
980          .src_color_blend_factor = IS_DYNAMIC(CB_BLEND_EQUATIONS) ? 0 : att->srcColorBlendFactor,
981          .dst_color_blend_factor = IS_DYNAMIC(CB_BLEND_EQUATIONS) ? 0 : att->dstColorBlendFactor,
982          .src_alpha_blend_factor = IS_DYNAMIC(CB_BLEND_EQUATIONS) ? 0 : att->srcAlphaBlendFactor,
983          .dst_alpha_blend_factor = IS_DYNAMIC(CB_BLEND_EQUATIONS) ? 0 : att->dstAlphaBlendFactor,
984          .write_mask = IS_DYNAMIC(CB_WRITE_MASKS) ? 0xf : att->colorWriteMask,
985          .color_blend_op = IS_DYNAMIC(CB_BLEND_EQUATIONS) ? 0 : att->colorBlendOp,
986          .alpha_blend_op = IS_DYNAMIC(CB_BLEND_EQUATIONS) ? 0 : att->alphaBlendOp,
987       };
988    }
989 
990    for (uint32_t i = 0; i < 4; i++)
991       cb->blend_constants[i] = cb_info->blendConstants[i];
992 
993    const VkPipelineColorWriteCreateInfoEXT *cw_info =
994       vk_find_struct_const(cb_info->pNext, PIPELINE_COLOR_WRITE_CREATE_INFO_EXT);
995    if (!IS_DYNAMIC(CB_COLOR_WRITE_ENABLES) && cw_info != NULL) {
996       uint8_t color_write_enables = 0;
997       assert(cb_info->attachmentCount == cw_info->attachmentCount);
998       for (uint32_t a = 0; a < cw_info->attachmentCount; a++) {
999          if (cw_info->pColorWriteEnables[a])
1000             color_write_enables |= BITFIELD_BIT(a);
1001       }
1002       cb->color_write_enables = color_write_enables;
1003    } else {
1004       cb->color_write_enables = BITFIELD_MASK(MESA_VK_MAX_COLOR_ATTACHMENTS);
1005    }
1006 }
1007 
1008 static void
vk_dynamic_graphics_state_init_cb(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_color_blend_state * cb)1009 vk_dynamic_graphics_state_init_cb(struct vk_dynamic_graphics_state *dst,
1010                                   const BITSET_WORD *needed,
1011                                   const struct vk_color_blend_state *cb)
1012 {
1013    dst->cb.logic_op_enable = cb->logic_op_enable;
1014    dst->cb.logic_op = cb->logic_op;
1015    dst->cb.color_write_enables = cb->color_write_enables;
1016    dst->cb.attachment_count = cb->attachment_count;
1017 
1018    if (IS_NEEDED(CB_BLEND_ENABLES) ||
1019        IS_NEEDED(CB_BLEND_EQUATIONS) ||
1020        IS_NEEDED(CB_WRITE_MASKS)) {
1021       typed_memcpy(dst->cb.attachments, cb->attachments, cb->attachment_count);
1022    }
1023 
1024    if (IS_NEEDED(CB_BLEND_CONSTANTS))
1025       typed_memcpy(dst->cb.blend_constants, cb->blend_constants, 4);
1026 }
1027 
1028 static bool
vk_render_pass_state_is_complete(const struct vk_render_pass_state * rp)1029 vk_render_pass_state_is_complete(const struct vk_render_pass_state *rp)
1030 {
1031    return rp->attachment_aspects != VK_IMAGE_ASPECT_METADATA_BIT;
1032 }
1033 
1034 static void
vk_render_pass_state_init(struct vk_render_pass_state * rp,const struct vk_render_pass_state * old_rp,const struct vk_render_pass_state * driver_rp,const VkGraphicsPipelineCreateInfo * info,VkGraphicsPipelineLibraryFlagsEXT lib)1035 vk_render_pass_state_init(struct vk_render_pass_state *rp,
1036                           const struct vk_render_pass_state *old_rp,
1037                           const struct vk_render_pass_state *driver_rp,
1038                           const VkGraphicsPipelineCreateInfo *info,
1039                           VkGraphicsPipelineLibraryFlagsEXT lib)
1040 {
1041    VkPipelineCreateFlags valid_pipeline_flags = 0;
1042    if (lib & VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT) {
1043       valid_pipeline_flags |=
1044          VK_PIPELINE_CREATE_RENDERING_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR |
1045          VK_PIPELINE_CREATE_RENDERING_FRAGMENT_DENSITY_MAP_ATTACHMENT_BIT_EXT;
1046    }
1047    if (lib & VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT) {
1048       valid_pipeline_flags |=
1049          VK_PIPELINE_CREATE_COLOR_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT |
1050          VK_PIPELINE_CREATE_DEPTH_STENCIL_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT;
1051    }
1052    const VkPipelineCreateFlags pipeline_flags =
1053       (driver_rp ? driver_rp->pipeline_flags :
1054        vk_get_pipeline_rendering_flags(info)) & valid_pipeline_flags;
1055 
1056    /* If we already have render pass state and it has attachment info, then
1057     * it's complete and we don't need a new one.  The one caveat here is that
1058     * we may need to add in some rendering flags.
1059     */
1060    if (old_rp != NULL && vk_render_pass_state_is_complete(old_rp)) {
1061       *rp = *old_rp;
1062       rp->pipeline_flags |= pipeline_flags;
1063       return;
1064    }
1065 
1066    *rp = (struct vk_render_pass_state) {
1067       .render_pass = info->renderPass,
1068       .subpass = info->subpass,
1069       .pipeline_flags = pipeline_flags,
1070       .depth_attachment_format = VK_FORMAT_UNDEFINED,
1071       .stencil_attachment_format = VK_FORMAT_UNDEFINED,
1072    };
1073 
1074    if (info->renderPass != VK_NULL_HANDLE && driver_rp != NULL) {
1075       assert(driver_rp->render_pass == info->renderPass);
1076       assert(driver_rp->subpass == info->subpass);
1077       *rp = *driver_rp;
1078       return;
1079    }
1080 
1081    const VkPipelineRenderingCreateInfo *r_info =
1082       vk_get_pipeline_rendering_create_info(info);
1083 
1084    if (r_info == NULL)
1085       return;
1086 
1087    rp->view_mask = r_info->viewMask;
1088 
1089    /* From the Vulkan 1.3.218 spec description of pre-rasterization state:
1090     *
1091     *    "Fragment shader state is defined by:
1092     *    ...
1093     *     * VkRenderPass and subpass parameter
1094     *     * The viewMask parameter of VkPipelineRenderingCreateInfo (formats
1095     *       are ignored)"
1096     *
1097     * The description of fragment shader state contains identical text.
1098     *
1099     * If we have a render pass then we have full information.  Even if we're
1100     * dynamic-rendering-only, the presence of a render pass means the
1101     * rendering info came from a vk_render_pass and is therefore complete.
1102     * Otherwise, all we can grab is the view mask and we have to leave the
1103     * rest for later.
1104     */
1105    if (info->renderPass == VK_NULL_HANDLE &&
1106        !(lib & VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT)) {
1107       rp->attachment_aspects = VK_IMAGE_ASPECT_METADATA_BIT;
1108       return;
1109    }
1110 
1111    assert(r_info->colorAttachmentCount <= MESA_VK_MAX_COLOR_ATTACHMENTS);
1112    rp->color_attachment_count = r_info->colorAttachmentCount;
1113    for (uint32_t i = 0; i < r_info->colorAttachmentCount; i++) {
1114       rp->color_attachment_formats[i] = r_info->pColorAttachmentFormats[i];
1115       if (r_info->pColorAttachmentFormats[i] != VK_FORMAT_UNDEFINED)
1116          rp->attachment_aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
1117    }
1118 
1119    rp->depth_attachment_format = r_info->depthAttachmentFormat;
1120    if (r_info->depthAttachmentFormat != VK_FORMAT_UNDEFINED)
1121       rp->attachment_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
1122 
1123    rp->stencil_attachment_format = r_info->stencilAttachmentFormat;
1124    if (r_info->stencilAttachmentFormat != VK_FORMAT_UNDEFINED)
1125       rp->attachment_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
1126 
1127    const VkAttachmentSampleCountInfoAMD *asc_info =
1128       vk_get_pipeline_sample_count_info_amd(info);
1129    if (asc_info != NULL) {
1130       assert(asc_info->colorAttachmentCount == rp->color_attachment_count);
1131       for (uint32_t i = 0; i < asc_info->colorAttachmentCount; i++) {
1132          rp->color_attachment_samples[i] = asc_info->pColorAttachmentSamples[i];
1133       }
1134 
1135       rp->depth_stencil_attachment_samples = asc_info->depthStencilAttachmentSamples;
1136    }
1137 }
1138 
1139 static void
vk_dynamic_graphics_state_init_rp(struct vk_dynamic_graphics_state * dst,const BITSET_WORD * needed,const struct vk_render_pass_state * rp)1140 vk_dynamic_graphics_state_init_rp(struct vk_dynamic_graphics_state *dst,
1141                                   const BITSET_WORD *needed,
1142                                   const struct vk_render_pass_state *rp)
1143 { }
1144 
1145 #define FOREACH_STATE_GROUP(f)                           \
1146    f(MESA_VK_GRAPHICS_STATE_VERTEX_INPUT_BIT,            \
1147      vk_vertex_input_state, vi);                         \
1148    f(MESA_VK_GRAPHICS_STATE_INPUT_ASSEMBLY_BIT,          \
1149      vk_input_assembly_state, ia);                       \
1150    f(MESA_VK_GRAPHICS_STATE_TESSELLATION_BIT,            \
1151      vk_tessellation_state, ts);                         \
1152    f(MESA_VK_GRAPHICS_STATE_VIEWPORT_BIT,                \
1153      vk_viewport_state, vp);                             \
1154    f(MESA_VK_GRAPHICS_STATE_DISCARD_RECTANGLES_BIT,      \
1155      vk_discard_rectangles_state, dr);                   \
1156    f(MESA_VK_GRAPHICS_STATE_RASTERIZATION_BIT,           \
1157      vk_rasterization_state, rs);                        \
1158    f(MESA_VK_GRAPHICS_STATE_FRAGMENT_SHADING_RATE_BIT,   \
1159      vk_fragment_shading_rate_state, fsr);               \
1160    f(MESA_VK_GRAPHICS_STATE_MULTISAMPLE_BIT,             \
1161      vk_multisample_state, ms);                          \
1162    f(MESA_VK_GRAPHICS_STATE_DEPTH_STENCIL_BIT,           \
1163      vk_depth_stencil_state, ds);                        \
1164    f(MESA_VK_GRAPHICS_STATE_COLOR_BLEND_BIT,             \
1165      vk_color_blend_state, cb);                          \
1166    f(MESA_VK_GRAPHICS_STATE_RENDER_PASS_BIT,             \
1167      vk_render_pass_state, rp);
1168 
1169 static enum mesa_vk_graphics_state_groups
vk_graphics_pipeline_state_groups(const struct vk_graphics_pipeline_state * state)1170 vk_graphics_pipeline_state_groups(const struct vk_graphics_pipeline_state *state)
1171 {
1172    /* For now, we just validate dynamic state */
1173    enum mesa_vk_graphics_state_groups groups = 0;
1174 
1175 #define FILL_HAS(STATE, type, s) \
1176    if (state->s != NULL) groups |= STATE
1177 
1178    FOREACH_STATE_GROUP(FILL_HAS)
1179 
1180 #undef FILL_HAS
1181 
1182    return groups | fully_dynamic_state_groups(state->dynamic);
1183 }
1184 
1185 void
vk_graphics_pipeline_get_state(const struct vk_graphics_pipeline_state * state,BITSET_WORD * set_state_out)1186 vk_graphics_pipeline_get_state(const struct vk_graphics_pipeline_state *state,
1187                                BITSET_WORD *set_state_out)
1188 {
1189    /* For now, we just validate dynamic state */
1190    enum mesa_vk_graphics_state_groups groups = 0;
1191 
1192 #define FILL_HAS(STATE, type, s) \
1193    if (state->s != NULL) groups |= STATE
1194 
1195    FOREACH_STATE_GROUP(FILL_HAS)
1196 
1197 #undef FILL_HAS
1198 
1199    BITSET_DECLARE(set_state, MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX);
1200    get_dynamic_state_groups(set_state, groups);
1201    BITSET_ANDNOT(set_state, set_state, state->dynamic);
1202    memcpy(set_state_out, set_state, sizeof(set_state));
1203 }
1204 
1205 static void
vk_graphics_pipeline_state_validate(const struct vk_graphics_pipeline_state * state)1206 vk_graphics_pipeline_state_validate(const struct vk_graphics_pipeline_state *state)
1207 {
1208 #ifndef NDEBUG
1209    /* For now, we just validate dynamic state */
1210    enum mesa_vk_graphics_state_groups groups =
1211       vk_graphics_pipeline_state_groups(state);
1212    validate_dynamic_state_groups(state->dynamic, groups);
1213 #endif
1214 }
1215 
1216 static bool
may_have_rasterization(const struct vk_graphics_pipeline_state * state,const BITSET_WORD * dynamic,const VkGraphicsPipelineCreateInfo * info)1217 may_have_rasterization(const struct vk_graphics_pipeline_state *state,
1218                        const BITSET_WORD *dynamic,
1219                        const VkGraphicsPipelineCreateInfo *info)
1220 {
1221    if (state->rs) {
1222       /* We default rasterizer_discard_enable to false when dynamic */
1223       return !state->rs->rasterizer_discard_enable;
1224    } else {
1225       return IS_DYNAMIC(RS_RASTERIZER_DISCARD_ENABLE) ||
1226              !info->pRasterizationState->rasterizerDiscardEnable;
1227    }
1228 }
1229 
1230 VkResult
vk_graphics_pipeline_state_fill(const struct vk_device * device,struct vk_graphics_pipeline_state * state,const VkGraphicsPipelineCreateInfo * info,const struct vk_render_pass_state * driver_rp,struct vk_graphics_pipeline_all_state * all,const VkAllocationCallbacks * alloc,VkSystemAllocationScope scope,void ** alloc_ptr_out)1231 vk_graphics_pipeline_state_fill(const struct vk_device *device,
1232                                 struct vk_graphics_pipeline_state *state,
1233                                 const VkGraphicsPipelineCreateInfo *info,
1234                                 const struct vk_render_pass_state *driver_rp,
1235                                 struct vk_graphics_pipeline_all_state *all,
1236                                 const VkAllocationCallbacks *alloc,
1237                                 VkSystemAllocationScope scope,
1238                                 void **alloc_ptr_out)
1239 {
1240    vk_graphics_pipeline_state_validate(state);
1241 
1242    BITSET_DECLARE(dynamic, MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX);
1243    vk_get_dynamic_graphics_states(dynamic, info->pDynamicState);
1244 
1245    /*
1246     * First, figure out which library-level shader/state groups we need
1247     */
1248 
1249    VkGraphicsPipelineLibraryFlagsEXT lib;
1250    const VkGraphicsPipelineLibraryCreateInfoEXT *gpl_info =
1251       vk_find_struct_const(info->pNext, GRAPHICS_PIPELINE_LIBRARY_CREATE_INFO_EXT);
1252    const VkPipelineLibraryCreateInfoKHR *lib_info =
1253       vk_find_struct_const(info->pNext, PIPELINE_LIBRARY_CREATE_INFO_KHR);
1254 
1255    VkShaderStageFlagBits allowed_stages;
1256    if (!(info->flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR)) {
1257       allowed_stages = VK_SHADER_STAGE_ALL_GRAPHICS |
1258                        VK_SHADER_STAGE_TASK_BIT_EXT |
1259                        VK_SHADER_STAGE_MESH_BIT_EXT;
1260    } else if (gpl_info) {
1261       allowed_stages = 0;
1262 
1263       /* If we're creating a pipeline library without pre-rasterization,
1264        * discard all the associated stages.
1265        */
1266       if (gpl_info->flags &
1267           VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT) {
1268          allowed_stages |= (VK_SHADER_STAGE_VERTEX_BIT |
1269                             VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT |
1270                             VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT |
1271                             VK_SHADER_STAGE_GEOMETRY_BIT |
1272                             VK_SHADER_STAGE_TASK_BIT_EXT |
1273                             VK_SHADER_STAGE_MESH_BIT_EXT);
1274       }
1275 
1276       /* If we're creating a pipeline library without fragment shader,
1277        * discard that stage.
1278        */
1279       if (gpl_info->flags &
1280            VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT)
1281          allowed_stages |= VK_SHADER_STAGE_FRAGMENT_BIT;
1282    } else {
1283       /* VkGraphicsPipelineLibraryCreateInfoEXT was omitted, flags should
1284        * be assumed to be empty and therefore no shader stage should be
1285        * considered.
1286        */
1287       allowed_stages = 0;
1288    }
1289 
1290    for (uint32_t i = 0; i < info->stageCount; i++) {
1291       state->shader_stages |= info->pStages[i].stage & allowed_stages;
1292    }
1293 
1294    /* In case we return early */
1295    if (alloc_ptr_out != NULL)
1296       *alloc_ptr_out = NULL;
1297 
1298    if (gpl_info) {
1299       lib = gpl_info->flags;
1300    } else if ((lib_info && lib_info->libraryCount > 0) ||
1301               (info->flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR)) {
1302      /*
1303       * From the Vulkan 1.3.210 spec:
1304       *    "If this structure is omitted, and either VkGraphicsPipelineCreateInfo::flags
1305       *    includes VK_PIPELINE_CREATE_LIBRARY_BIT_KHR or the
1306       *    VkGraphicsPipelineCreateInfo::pNext chain includes a
1307       *    VkPipelineLibraryCreateInfoKHR structure with a libraryCount greater than 0,
1308       *    it is as if flags is 0. Otherwise if this structure is omitted, it is as if
1309       *    flags includes all possible subsets of the graphics pipeline."
1310       */
1311       lib = 0;
1312    } else {
1313       /* We're building a complete pipeline.  From the Vulkan 1.3.218 spec:
1314        *
1315        *    "A complete graphics pipeline always includes pre-rasterization
1316        *    shader state, with other subsets included depending on that state.
1317        *    If the pre-rasterization shader state includes a vertex shader,
1318        *    then vertex input state is included in a complete graphics
1319        *    pipeline. If the value of
1320        *    VkPipelineRasterizationStateCreateInfo::rasterizerDiscardEnable in
1321        *    the pre-rasterization shader state is VK_FALSE or the
1322        *    VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE dynamic state is
1323        *    enabled fragment shader state and fragment output interface state
1324        *    is included in a complete graphics pipeline."
1325        */
1326       lib = VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT;
1327 
1328       if (state->shader_stages & VK_SHADER_STAGE_VERTEX_BIT)
1329          lib |= VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT;
1330 
1331       if (may_have_rasterization(state, dynamic, info)) {
1332          lib |= VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT;
1333          lib |= VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT;
1334       }
1335    }
1336 
1337    /*
1338     * Next, turn those into individual states.  Among other things, this
1339     * de-duplicates things like FSR and multisample state which appear in
1340     * multiple library groups.
1341     */
1342 
1343    enum mesa_vk_graphics_state_groups needs = 0;
1344    if (lib & VK_GRAPHICS_PIPELINE_LIBRARY_VERTEX_INPUT_INTERFACE_BIT_EXT) {
1345       needs |= MESA_VK_GRAPHICS_STATE_VERTEX_INPUT_BIT;
1346       needs |= MESA_VK_GRAPHICS_STATE_INPUT_ASSEMBLY_BIT;
1347    }
1348 
1349    /* Other stuff potentially depends on this so gather it early */
1350    struct vk_render_pass_state rp;
1351    if (lib & (VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT |
1352               VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT |
1353               VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT)) {
1354       vk_render_pass_state_init(&rp, state->rp, driver_rp, info, lib);
1355 
1356       needs |= MESA_VK_GRAPHICS_STATE_RENDER_PASS_BIT;
1357 
1358       /* If the old state was incomplete but the new one isn't, set state->rp
1359        * to NULL so it gets replaced with the new version.
1360        */
1361       if (state->rp != NULL &&
1362           !vk_render_pass_state_is_complete(state->rp) &&
1363           vk_render_pass_state_is_complete(&rp))
1364          state->rp = NULL;
1365    }
1366 
1367    if (lib & VK_GRAPHICS_PIPELINE_LIBRARY_PRE_RASTERIZATION_SHADERS_BIT_EXT) {
1368       /* From the Vulkan 1.3.218 spec:
1369        *
1370        *    VUID-VkGraphicsPipelineCreateInfo-stage-02096
1371        *
1372        *    "If the pipeline is being created with pre-rasterization shader
1373        *    state the stage member of one element of pStages must be either
1374        *    VK_SHADER_STAGE_VERTEX_BIT or VK_SHADER_STAGE_MESH_BIT_NV"
1375        */
1376       assert(state->shader_stages & (VK_SHADER_STAGE_VERTEX_BIT |
1377                                      VK_SHADER_STAGE_MESH_BIT_NV));
1378 
1379       if (state->shader_stages & (VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT |
1380                                   VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT))
1381          needs |= MESA_VK_GRAPHICS_STATE_TESSELLATION_BIT;
1382 
1383       if (may_have_rasterization(state, dynamic, info))
1384          needs |= MESA_VK_GRAPHICS_STATE_VIEWPORT_BIT;
1385 
1386       needs |= MESA_VK_GRAPHICS_STATE_DISCARD_RECTANGLES_BIT;
1387       needs |= MESA_VK_GRAPHICS_STATE_RASTERIZATION_BIT;
1388       needs |= MESA_VK_GRAPHICS_STATE_FRAGMENT_SHADING_RATE_BIT;
1389    }
1390 
1391    if (lib & VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_SHADER_BIT_EXT) {
1392       needs |= MESA_VK_GRAPHICS_STATE_FRAGMENT_SHADING_RATE_BIT;
1393 
1394       /* From the Vulkan 1.3.218 spec:
1395        *
1396        *    "Fragment shader state is defined by:
1397        *    ...
1398        *     - VkPipelineMultisampleStateCreateInfo if sample shading is
1399        *       enabled or renderpass is not VK_NULL_HANDLE"
1400        *
1401        * and
1402        *
1403        *    VUID-VkGraphicsPipelineCreateInfo-pMultisampleState-06629
1404        *
1405        *    "If the pipeline is being created with fragment shader state
1406        *    pMultisampleState must be NULL or a valid pointer to a valid
1407        *    VkPipelineMultisampleStateCreateInfo structure"
1408        *
1409        * so we can reliably detect when to include it based on the
1410        * pMultisampleState pointer.
1411        */
1412       if (info->pMultisampleState != NULL)
1413          needs |= MESA_VK_GRAPHICS_STATE_MULTISAMPLE_BIT;
1414 
1415       /* From the Vulkan 1.3.218 spec:
1416        *
1417        *    VUID-VkGraphicsPipelineCreateInfo-renderPass-06043
1418        *
1419        *    "If renderPass is not VK_NULL_HANDLE, the pipeline is being
1420        *    created with fragment shader state, and subpass uses a
1421        *    depth/stencil attachment, pDepthStencilState must be a valid
1422        *    pointer to a valid VkPipelineDepthStencilStateCreateInfo
1423        *    structure"
1424        *
1425        *    VUID-VkGraphicsPipelineCreateInfo-renderPass-06053
1426        *
1427        *    "If renderPass is VK_NULL_HANDLE, the pipeline is being created
1428        *    with fragment shader state and fragment output interface state,
1429        *    and either of VkPipelineRenderingCreateInfo::depthAttachmentFormat
1430        *    or VkPipelineRenderingCreateInfo::stencilAttachmentFormat are not
1431        *    VK_FORMAT_UNDEFINED, pDepthStencilState must be a valid pointer to
1432        *    a valid VkPipelineDepthStencilStateCreateInfo structure"
1433        *
1434        *    VUID-VkGraphicsPipelineCreateInfo-renderPass-06590
1435        *
1436        *    "If renderPass is VK_NULL_HANDLE and the pipeline is being created
1437        *    with fragment shader state but not fragment output interface
1438        *    state, pDepthStencilState must be a valid pointer to a valid
1439        *    VkPipelineDepthStencilStateCreateInfo structure"
1440        *
1441        * In the first case, we'll have a real set of aspects in rp.  In the
1442        * second case, where we have both fragment shader and fragment output
1443        * state, we will also have a valid set of aspects.  In the third case
1444        * where we only have fragment shader state and no render pass, the
1445        * vk_render_pass_state will be incomplete.
1446        */
1447       if ((rp.attachment_aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
1448                                     VK_IMAGE_ASPECT_STENCIL_BIT)) ||
1449           !vk_render_pass_state_is_complete(&rp))
1450          needs |= MESA_VK_GRAPHICS_STATE_DEPTH_STENCIL_BIT;
1451    }
1452 
1453    if (lib & VK_GRAPHICS_PIPELINE_LIBRARY_FRAGMENT_OUTPUT_INTERFACE_BIT_EXT) {
1454       if (rp.attachment_aspects & (VK_IMAGE_ASPECT_COLOR_BIT))
1455          needs |= MESA_VK_GRAPHICS_STATE_COLOR_BLEND_BIT;
1456 
1457       needs |= MESA_VK_GRAPHICS_STATE_MULTISAMPLE_BIT;
1458    }
1459 
1460    /*
1461     * Next, Filter off any states we already have.
1462     */
1463 
1464 #define FILTER_NEEDS(STATE, type, s) \
1465    if (state->s != NULL) needs &= ~STATE
1466 
1467    FOREACH_STATE_GROUP(FILTER_NEEDS)
1468 
1469 #undef FILTER_NEEDS
1470 
1471    /* Filter dynamic state down to just what we're adding */
1472    BITSET_DECLARE(dynamic_filter, MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX);
1473    get_dynamic_state_groups(dynamic_filter, needs);
1474    BITSET_AND(dynamic, dynamic, dynamic_filter);
1475 
1476    /* And add it in */
1477    BITSET_OR(state->dynamic, state->dynamic, dynamic);
1478 
1479    /*
1480     * If a state is fully dynamic, we don't need to even allocate them.  Do
1481     * this after we've filtered dynamic state because we still want them to
1482     * show up in the dynamic state but don't want the actual state.
1483     */
1484    needs &= ~fully_dynamic_state_groups(state->dynamic);
1485 
1486    /* If we don't need to set up any new states, bail early */
1487    if (needs == 0)
1488       return VK_SUCCESS;
1489 
1490    /*
1491     * Now, ensure that we have space for each of the states we're going to
1492     * fill.  If all != NULL, we'll pull from that.  Otherwise, we need to
1493     * allocate memory.
1494     */
1495 
1496    VK_MULTIALLOC(ma);
1497 
1498 #define ENSURE_STATE_IF_NEEDED(STATE, type, s) \
1499    struct type *new_##s = NULL; \
1500    if (needs & STATE) { \
1501       if (all == NULL) { \
1502          vk_multialloc_add(&ma, &new_##s, struct type, 1); \
1503       } else { \
1504          new_##s = &all->s; \
1505       } \
1506    }
1507 
1508    FOREACH_STATE_GROUP(ENSURE_STATE_IF_NEEDED)
1509 
1510 #undef ENSURE_STATE_IF_NEEDED
1511 
1512    /* Sample locations are a bit special.  We don't want to waste the memory
1513     * for 64 floats if we don't need to.  Also, we set up standard sample
1514     * locations if no user-provided sample locations are available.
1515     */
1516    const VkPipelineSampleLocationsStateCreateInfoEXT *sl_info = NULL;
1517    struct vk_sample_locations_state *new_sl = NULL;
1518    if (needs & MESA_VK_GRAPHICS_STATE_MULTISAMPLE_BIT) {
1519       if (info->pMultisampleState)
1520          sl_info = vk_find_struct_const(info->pMultisampleState->pNext,
1521                                        PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT);
1522       if (needs_sample_locations_state(dynamic, sl_info)) {
1523          if (all == NULL) {
1524             vk_multialloc_add(&ma, &new_sl, struct vk_sample_locations_state, 1);
1525          } else {
1526             new_sl = &all->ms_sample_locations;
1527          }
1528       }
1529    }
1530 
1531    /*
1532     * Allocate memory, if needed
1533     */
1534 
1535    if (ma.size > 0) {
1536       assert(all == NULL);
1537       *alloc_ptr_out = vk_multialloc_alloc2(&ma, &device->alloc, alloc, scope);
1538       if (*alloc_ptr_out == NULL)
1539          return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1540    }
1541 
1542    /*
1543     * Create aliases for various input infos so we can use or FOREACH macro
1544     */
1545 
1546 #define INFO_ALIAS(_State, s) \
1547    const VkPipeline##_State##StateCreateInfo *s##_info = info->p##_State##State
1548 
1549    INFO_ALIAS(VertexInput, vi);
1550    INFO_ALIAS(InputAssembly, ia);
1551    INFO_ALIAS(Tessellation, ts);
1552    INFO_ALIAS(Viewport, vp);
1553    INFO_ALIAS(Rasterization, rs);
1554    INFO_ALIAS(Multisample, ms);
1555    INFO_ALIAS(DepthStencil, ds);
1556    INFO_ALIAS(ColorBlend, cb);
1557 
1558 #undef INFO_ALIAS
1559 
1560    const VkPipelineDiscardRectangleStateCreateInfoEXT *dr_info =
1561       vk_find_struct_const(info->pNext, PIPELINE_DISCARD_RECTANGLE_STATE_CREATE_INFO_EXT);
1562 
1563    const VkPipelineFragmentShadingRateStateCreateInfoKHR *fsr_info =
1564       vk_find_struct_const(info->pNext, PIPELINE_FRAGMENT_SHADING_RATE_STATE_CREATE_INFO_KHR);
1565 
1566    /*
1567     * Finally, fill out all the states
1568     */
1569 
1570 #define INIT_STATE_IF_NEEDED(STATE, type, s) \
1571    if (needs & STATE) { \
1572       type##_init(new_##s, dynamic, s##_info); \
1573       state->s = new_##s; \
1574    }
1575 
1576    /* render pass state is special and we just copy it */
1577 #define vk_render_pass_state_init(s, d, i) *s = rp
1578 
1579    FOREACH_STATE_GROUP(INIT_STATE_IF_NEEDED)
1580 
1581 #undef vk_render_pass_state_init
1582 #undef INIT_STATE_IF_NEEDED
1583 
1584    if (needs & MESA_VK_GRAPHICS_STATE_MULTISAMPLE_BIT) {
1585        vk_multisample_sample_locations_state_init(new_ms, new_sl, dynamic,
1586                                                   ms_info, sl_info);
1587    }
1588 
1589    return VK_SUCCESS;
1590 }
1591 
1592 #undef IS_DYNAMIC
1593 #undef IS_NEEDED
1594 
1595 void
vk_graphics_pipeline_state_merge(struct vk_graphics_pipeline_state * dst,const struct vk_graphics_pipeline_state * src)1596 vk_graphics_pipeline_state_merge(struct vk_graphics_pipeline_state *dst,
1597                                  const struct vk_graphics_pipeline_state *src)
1598 {
1599    vk_graphics_pipeline_state_validate(dst);
1600    vk_graphics_pipeline_state_validate(src);
1601 
1602    BITSET_OR(dst->dynamic, dst->dynamic, src->dynamic);
1603 
1604    dst->shader_stages |= src->shader_stages;
1605 
1606    /* Render pass state needs special care because a render pass state may be
1607     * incomplete (view mask only).  See vk_render_pass_state_init().
1608     */
1609    if (dst->rp != NULL && src->rp != NULL &&
1610        !vk_render_pass_state_is_complete(dst->rp) &&
1611        vk_render_pass_state_is_complete(src->rp))
1612       dst->rp = src->rp;
1613 
1614 #define MERGE(STATE, type, state) \
1615    if (dst->state == NULL && src->state != NULL) dst->state = src->state;
1616 
1617    FOREACH_STATE_GROUP(MERGE)
1618 
1619 #undef MERGE
1620 }
1621 
1622 static bool
is_group_all_dynamic(const struct vk_graphics_pipeline_state * state,enum mesa_vk_graphics_state_groups group)1623 is_group_all_dynamic(const struct vk_graphics_pipeline_state *state,
1624                      enum mesa_vk_graphics_state_groups group)
1625 {
1626    /* Render pass is a bit special, because it contains always-static state
1627     * (e.g. the view mask). It's never all dynamic.
1628     */
1629    if (group == MESA_VK_GRAPHICS_STATE_RENDER_PASS_BIT)
1630       return false;
1631 
1632    BITSET_DECLARE(group_state, MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX);
1633    BITSET_DECLARE(dynamic_state, MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX);
1634    get_dynamic_state_groups(group_state, group);
1635    BITSET_AND(dynamic_state, group_state, state->dynamic);
1636    return BITSET_EQUAL(dynamic_state, group_state);
1637 }
1638 
1639 VkResult
vk_graphics_pipeline_state_copy(const struct vk_device * device,struct vk_graphics_pipeline_state * state,const struct vk_graphics_pipeline_state * old_state,const VkAllocationCallbacks * alloc,VkSystemAllocationScope scope,void ** alloc_ptr_out)1640 vk_graphics_pipeline_state_copy(const struct vk_device *device,
1641                                 struct vk_graphics_pipeline_state *state,
1642                                 const struct vk_graphics_pipeline_state *old_state,
1643                                 const VkAllocationCallbacks *alloc,
1644                                 VkSystemAllocationScope scope,
1645                                 void **alloc_ptr_out)
1646 {
1647    vk_graphics_pipeline_state_validate(old_state);
1648 
1649    VK_MULTIALLOC(ma);
1650 
1651 #define ENSURE_STATE_IF_NEEDED(STATE, type, s) \
1652    struct type *new_##s = NULL; \
1653    if (old_state->s && !is_group_all_dynamic(state, STATE)) { \
1654       vk_multialloc_add(&ma, &new_##s, struct type, 1); \
1655    }
1656 
1657    FOREACH_STATE_GROUP(ENSURE_STATE_IF_NEEDED)
1658 
1659 #undef ENSURE_STATE_IF_NEEDED
1660 
1661    /* Sample locations are a bit special. */
1662    struct vk_sample_locations_state *new_sample_locations = NULL;
1663    if (old_state->ms && old_state->ms->sample_locations &&
1664        !BITSET_TEST(old_state->dynamic, MESA_VK_DYNAMIC_MS_SAMPLE_LOCATIONS)) {
1665       assert(old_state->ms->sample_locations);
1666       vk_multialloc_add(&ma, &new_sample_locations,
1667                         struct vk_sample_locations_state, 1);
1668    }
1669 
1670    if (ma.size > 0) {
1671       *alloc_ptr_out = vk_multialloc_alloc2(&ma, &device->alloc, alloc, scope);
1672       if (*alloc_ptr_out == NULL)
1673          return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
1674    }
1675 
1676    if (new_sample_locations) {
1677       *new_sample_locations = *old_state->ms->sample_locations;
1678    }
1679 
1680 #define COPY_STATE_IF_NEEDED(STATE, type, s) \
1681    if (new_##s) { \
1682       *new_##s = *old_state->s; \
1683    } \
1684    state->s = new_##s;
1685 
1686    FOREACH_STATE_GROUP(COPY_STATE_IF_NEEDED)
1687 
1688    if (new_ms) {
1689       new_ms->sample_locations = new_sample_locations;
1690    }
1691 
1692    state->shader_stages = old_state->shader_stages;
1693    BITSET_COPY(state->dynamic, old_state->dynamic);
1694 
1695 #undef COPY_STATE_IF_NEEDED
1696 
1697    vk_graphics_pipeline_state_validate(state);
1698    return VK_SUCCESS;
1699 }
1700 
1701 const struct vk_dynamic_graphics_state vk_default_dynamic_graphics_state = {
1702    .rs = {
1703       .line = {
1704          .width = 1.0f,
1705       },
1706    },
1707    .fsr = {
1708       .fragment_size = {1u, 1u},
1709       .combiner_ops = {
1710          VK_FRAGMENT_SHADING_RATE_COMBINER_OP_KEEP_KHR,
1711          VK_FRAGMENT_SHADING_RATE_COMBINER_OP_KEEP_KHR,
1712       },
1713    },
1714    .ds = {
1715       .depth = {
1716          .bounds_test = {
1717             .min = 0.0f,
1718             .max = 1.0f,
1719          },
1720       },
1721       .stencil = {
1722          .write_enable = true,
1723          .front = {
1724             .compare_mask = -1,
1725             .write_mask = -1,
1726          },
1727          .back = {
1728             .compare_mask = -1,
1729             .write_mask = -1,
1730          },
1731       },
1732    },
1733    .cb = {
1734       .color_write_enables = 0xffu,
1735       .attachment_count = MESA_VK_MAX_COLOR_ATTACHMENTS,
1736    },
1737 };
1738 
1739 void
vk_dynamic_graphics_state_init(struct vk_dynamic_graphics_state * dyn)1740 vk_dynamic_graphics_state_init(struct vk_dynamic_graphics_state *dyn)
1741 {
1742    *dyn = vk_default_dynamic_graphics_state;
1743 }
1744 
1745 void
vk_dynamic_graphics_state_clear(struct vk_dynamic_graphics_state * dyn)1746 vk_dynamic_graphics_state_clear(struct vk_dynamic_graphics_state *dyn)
1747 {
1748    struct vk_vertex_input_state *vi = dyn->vi;
1749    struct vk_sample_locations_state *sl = dyn->ms.sample_locations;
1750 
1751    *dyn = vk_default_dynamic_graphics_state;
1752 
1753    if (vi != NULL) {
1754       memset(vi, 0, sizeof(*vi));
1755       dyn->vi = vi;
1756    }
1757 
1758    if (sl != NULL) {
1759       memset(sl, 0, sizeof(*sl));
1760       dyn->ms.sample_locations = sl;
1761    }
1762 }
1763 
1764 void
vk_dynamic_graphics_state_fill(struct vk_dynamic_graphics_state * dyn,const struct vk_graphics_pipeline_state * p)1765 vk_dynamic_graphics_state_fill(struct vk_dynamic_graphics_state *dyn,
1766                                const struct vk_graphics_pipeline_state *p)
1767 {
1768    /* This funciton (and the individual vk_dynamic_graphics_state_init_*
1769     * functions it calls) are a bit sloppy.  Instead of checking every single
1770     * bit, we just copy everything and set the bits the right way at the end
1771     * based on what groups we actually had.
1772     */
1773    enum mesa_vk_graphics_state_groups groups = 0;
1774 
1775    BITSET_DECLARE(needed, MESA_VK_DYNAMIC_GRAPHICS_STATE_ENUM_MAX);
1776    BITSET_COPY(needed, p->dynamic);
1777    BITSET_NOT(needed);
1778 
1779    /* We only want to copy these if the driver has filled out the relevant
1780     * pointer in the dynamic state struct.  If not, they don't support them
1781     * as dynamic state and we should leave them alone.
1782     */
1783    if (dyn->vi == NULL)
1784       BITSET_CLEAR(needed, MESA_VK_DYNAMIC_VI);
1785    if (dyn->ms.sample_locations == NULL)
1786       BITSET_CLEAR(needed, MESA_VK_DYNAMIC_MS_SAMPLE_LOCATIONS);
1787 
1788 #define INIT_DYNAMIC_STATE(STATE, type, s) \
1789    if (p->s != NULL) { \
1790       vk_dynamic_graphics_state_init_##s(dyn, needed, p->s); \
1791       groups |= STATE; \
1792    }
1793 
1794    FOREACH_STATE_GROUP(INIT_DYNAMIC_STATE);
1795 
1796 #undef INIT_DYNAMIC_STATE
1797 
1798    get_dynamic_state_groups(dyn->set, groups);
1799 
1800    /* Vertex input state is always included in a complete pipeline. If p->vi
1801     * is NULL, that means that it has been precompiled by the driver, but we
1802     * should still track vi_bindings_valid.
1803     */
1804    BITSET_SET(dyn->set, MESA_VK_DYNAMIC_VI_BINDINGS_VALID);
1805 
1806    /* If the pipeline doesn't render any color attachments, we should still
1807     * keep track of the fact that it writes 0 attachments, even though none of
1808     * the other blend states will be initialized. Normally this would be
1809     * initialized with the other blend states.
1810     */
1811    if (!p->rp || !(p->rp->attachment_aspects & VK_IMAGE_ASPECT_COLOR_BIT)) {
1812       dyn->cb.attachment_count = 0;
1813       BITSET_SET(dyn->set, MESA_VK_DYNAMIC_CB_ATTACHMENT_COUNT);
1814    }
1815 
1816    /* Mask off all but the groups we actually found */
1817    BITSET_AND(dyn->set, dyn->set, needed);
1818 }
1819 
1820 #define SET_DYN_VALUE(dst, STATE, state, value) do {        \
1821    if (!BITSET_TEST((dst)->set, MESA_VK_DYNAMIC_##STATE) || \
1822        (dst)->state != (value)) {                           \
1823       (dst)->state = (value);                               \
1824       assert((dst)->state == (value));                      \
1825       BITSET_SET(dst->set, MESA_VK_DYNAMIC_##STATE);        \
1826       BITSET_SET(dst->dirty, MESA_VK_DYNAMIC_##STATE);      \
1827    }                                                        \
1828 } while(0)
1829 
1830 #define SET_DYN_BOOL(dst, STATE, state, value) \
1831    SET_DYN_VALUE(dst, STATE, state, (bool)value);
1832 
1833 #define SET_DYN_ARRAY(dst, STATE, state, start, count, src) do {  \
1834    assert(start + count <= ARRAY_SIZE((dst)->state));             \
1835    STATIC_ASSERT(sizeof(*(dst)->state) == sizeof(*(src)));        \
1836    const size_t __state_size = sizeof(*(dst)->state) * (count);   \
1837    if (!BITSET_TEST((dst)->set, MESA_VK_DYNAMIC_##STATE) ||       \
1838        memcmp((dst)->state + start, src, __state_size)) {         \
1839       memcpy((dst)->state + start, src, __state_size);            \
1840       BITSET_SET(dst->set, MESA_VK_DYNAMIC_##STATE);              \
1841       BITSET_SET(dst->dirty, MESA_VK_DYNAMIC_##STATE);            \
1842    }                                                              \
1843 } while(0)
1844 
1845 void
vk_dynamic_graphics_state_copy(struct vk_dynamic_graphics_state * dst,const struct vk_dynamic_graphics_state * src)1846 vk_dynamic_graphics_state_copy(struct vk_dynamic_graphics_state *dst,
1847                                const struct vk_dynamic_graphics_state *src)
1848 {
1849 #define IS_SET_IN_SRC(STATE) \
1850    BITSET_TEST(src->set, MESA_VK_DYNAMIC_##STATE)
1851 
1852 #define COPY_MEMBER(STATE, state) \
1853    SET_DYN_VALUE(dst, STATE, state, src->state)
1854 
1855 #define COPY_ARRAY(STATE, state, count) \
1856    SET_DYN_ARRAY(dst, STATE, state, 0, count, src->state)
1857 
1858 #define COPY_IF_SET(STATE, state) \
1859    if (IS_SET_IN_SRC(STATE)) SET_DYN_VALUE(dst, STATE, state, src->state)
1860 
1861    if (IS_SET_IN_SRC(VI)) {
1862       assert(dst->vi != NULL);
1863       COPY_MEMBER(VI, vi->bindings_valid);
1864       u_foreach_bit(b, src->vi->bindings_valid) {
1865          COPY_MEMBER(VI, vi->bindings[b].stride);
1866          COPY_MEMBER(VI, vi->bindings[b].input_rate);
1867          COPY_MEMBER(VI, vi->bindings[b].divisor);
1868       }
1869       COPY_MEMBER(VI, vi->attributes_valid);
1870       u_foreach_bit(a, src->vi->attributes_valid) {
1871          COPY_MEMBER(VI, vi->attributes[a].binding);
1872          COPY_MEMBER(VI, vi->attributes[a].format);
1873          COPY_MEMBER(VI, vi->attributes[a].offset);
1874       }
1875    }
1876 
1877    if (IS_SET_IN_SRC(VI_BINDINGS_VALID))
1878       COPY_MEMBER(VI_BINDINGS_VALID, vi_bindings_valid);
1879 
1880    if (IS_SET_IN_SRC(VI_BINDING_STRIDES)) {
1881       assert(IS_SET_IN_SRC(VI_BINDINGS_VALID));
1882       u_foreach_bit(a, src->vi_bindings_valid) {
1883          COPY_MEMBER(VI_BINDING_STRIDES, vi_binding_strides[a]);
1884       }
1885    }
1886 
1887    COPY_IF_SET(IA_PRIMITIVE_TOPOLOGY, ia.primitive_topology);
1888    COPY_IF_SET(IA_PRIMITIVE_RESTART_ENABLE, ia.primitive_restart_enable);
1889    COPY_IF_SET(TS_PATCH_CONTROL_POINTS, ts.patch_control_points);
1890    COPY_IF_SET(TS_DOMAIN_ORIGIN, ts.domain_origin);
1891 
1892    COPY_IF_SET(VP_VIEWPORT_COUNT, vp.viewport_count);
1893    if (IS_SET_IN_SRC(VP_VIEWPORTS)) {
1894       assert(IS_SET_IN_SRC(VP_VIEWPORT_COUNT));
1895       COPY_ARRAY(VP_VIEWPORTS, vp.viewports, src->vp.viewport_count);
1896    }
1897 
1898    COPY_IF_SET(VP_SCISSOR_COUNT, vp.scissor_count);
1899    if (IS_SET_IN_SRC(VP_SCISSORS)) {
1900       assert(IS_SET_IN_SRC(VP_SCISSOR_COUNT));
1901       COPY_ARRAY(VP_SCISSORS, vp.scissors, src->vp.scissor_count);
1902    }
1903 
1904    COPY_IF_SET(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE,
1905                vp.depth_clip_negative_one_to_one);
1906 
1907    COPY_IF_SET(DR_ENABLE, dr.enable);
1908    COPY_IF_SET(DR_MODE, dr.mode);
1909    if (IS_SET_IN_SRC(DR_RECTANGLES)) {
1910       COPY_MEMBER(DR_RECTANGLES, dr.rectangle_count);
1911       COPY_ARRAY(DR_RECTANGLES, dr.rectangles, src->dr.rectangle_count);
1912    }
1913 
1914    COPY_IF_SET(RS_RASTERIZER_DISCARD_ENABLE, rs.rasterizer_discard_enable);
1915    COPY_IF_SET(RS_DEPTH_CLAMP_ENABLE, rs.depth_clamp_enable);
1916    COPY_IF_SET(RS_DEPTH_CLIP_ENABLE, rs.depth_clip_enable);
1917    COPY_IF_SET(RS_POLYGON_MODE, rs.polygon_mode);
1918    COPY_IF_SET(RS_CULL_MODE, rs.cull_mode);
1919    COPY_IF_SET(RS_FRONT_FACE, rs.front_face);
1920    COPY_IF_SET(RS_CONSERVATIVE_MODE, rs.conservative_mode);
1921    COPY_IF_SET(RS_EXTRA_PRIMITIVE_OVERESTIMATION_SIZE,
1922                rs.extra_primitive_overestimation_size);
1923    COPY_IF_SET(RS_RASTERIZATION_ORDER_AMD, rs.rasterization_order_amd);
1924    COPY_IF_SET(RS_PROVOKING_VERTEX, rs.provoking_vertex);
1925    COPY_IF_SET(RS_RASTERIZATION_STREAM, rs.rasterization_stream);
1926    COPY_IF_SET(RS_DEPTH_BIAS_ENABLE, rs.depth_bias.enable);
1927    COPY_IF_SET(RS_DEPTH_BIAS_FACTORS, rs.depth_bias.constant);
1928    COPY_IF_SET(RS_DEPTH_BIAS_FACTORS, rs.depth_bias.clamp);
1929    COPY_IF_SET(RS_DEPTH_BIAS_FACTORS, rs.depth_bias.slope);
1930    COPY_IF_SET(RS_DEPTH_BIAS_FACTORS, rs.depth_bias.representation);
1931    COPY_IF_SET(RS_DEPTH_BIAS_FACTORS, rs.depth_bias.exact);
1932    COPY_IF_SET(RS_LINE_WIDTH, rs.line.width);
1933    COPY_IF_SET(RS_LINE_MODE, rs.line.mode);
1934    COPY_IF_SET(RS_LINE_STIPPLE_ENABLE, rs.line.stipple.enable);
1935    COPY_IF_SET(RS_LINE_STIPPLE, rs.line.stipple.factor);
1936    COPY_IF_SET(RS_LINE_STIPPLE, rs.line.stipple.pattern);
1937 
1938    COPY_IF_SET(FSR, fsr.fragment_size.width);
1939    COPY_IF_SET(FSR, fsr.fragment_size.height);
1940    COPY_IF_SET(FSR, fsr.combiner_ops[0]);
1941    COPY_IF_SET(FSR, fsr.combiner_ops[1]);
1942 
1943    COPY_IF_SET(MS_RASTERIZATION_SAMPLES, ms.rasterization_samples);
1944    COPY_IF_SET(MS_SAMPLE_MASK, ms.sample_mask);
1945    COPY_IF_SET(MS_ALPHA_TO_COVERAGE_ENABLE, ms.alpha_to_coverage_enable);
1946    COPY_IF_SET(MS_ALPHA_TO_ONE_ENABLE, ms.alpha_to_one_enable);
1947    COPY_IF_SET(MS_SAMPLE_LOCATIONS_ENABLE, ms.sample_locations_enable);
1948 
1949    if (IS_SET_IN_SRC(MS_SAMPLE_LOCATIONS)) {
1950       assert(dst->ms.sample_locations != NULL);
1951       COPY_MEMBER(MS_SAMPLE_LOCATIONS, ms.sample_locations->per_pixel);
1952       COPY_MEMBER(MS_SAMPLE_LOCATIONS, ms.sample_locations->grid_size.width);
1953       COPY_MEMBER(MS_SAMPLE_LOCATIONS, ms.sample_locations->grid_size.height);
1954       const uint32_t sl_count = src->ms.sample_locations->per_pixel *
1955                                 src->ms.sample_locations->grid_size.width *
1956                                 src->ms.sample_locations->grid_size.height;
1957       COPY_ARRAY(MS_SAMPLE_LOCATIONS, ms.sample_locations->locations, sl_count);
1958    }
1959 
1960    COPY_IF_SET(DS_DEPTH_TEST_ENABLE, ds.depth.test_enable);
1961    COPY_IF_SET(DS_DEPTH_WRITE_ENABLE, ds.depth.write_enable);
1962    COPY_IF_SET(DS_DEPTH_COMPARE_OP, ds.depth.compare_op);
1963    COPY_IF_SET(DS_DEPTH_BOUNDS_TEST_ENABLE, ds.depth.bounds_test.enable);
1964    if (IS_SET_IN_SRC(DS_DEPTH_BOUNDS_TEST_BOUNDS)) {
1965       COPY_MEMBER(DS_DEPTH_BOUNDS_TEST_BOUNDS, ds.depth.bounds_test.min);
1966       COPY_MEMBER(DS_DEPTH_BOUNDS_TEST_BOUNDS, ds.depth.bounds_test.max);
1967    }
1968 
1969    COPY_IF_SET(DS_STENCIL_TEST_ENABLE, ds.stencil.test_enable);
1970    if (IS_SET_IN_SRC(DS_STENCIL_OP)) {
1971       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.front.op.fail);
1972       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.front.op.pass);
1973       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.front.op.depth_fail);
1974       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.front.op.compare);
1975       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.back.op.fail);
1976       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.back.op.pass);
1977       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.back.op.depth_fail);
1978       COPY_MEMBER(DS_STENCIL_OP, ds.stencil.back.op.compare);
1979    }
1980    if (IS_SET_IN_SRC(DS_STENCIL_COMPARE_MASK)) {
1981       COPY_MEMBER(DS_STENCIL_COMPARE_MASK, ds.stencil.front.compare_mask);
1982       COPY_MEMBER(DS_STENCIL_COMPARE_MASK, ds.stencil.back.compare_mask);
1983    }
1984    if (IS_SET_IN_SRC(DS_STENCIL_WRITE_MASK)) {
1985       COPY_MEMBER(DS_STENCIL_WRITE_MASK, ds.stencil.front.write_mask);
1986       COPY_MEMBER(DS_STENCIL_WRITE_MASK, ds.stencil.back.write_mask);
1987    }
1988    if (IS_SET_IN_SRC(DS_STENCIL_REFERENCE)) {
1989       COPY_MEMBER(DS_STENCIL_REFERENCE, ds.stencil.front.reference);
1990       COPY_MEMBER(DS_STENCIL_REFERENCE, ds.stencil.back.reference);
1991    }
1992 
1993    COPY_IF_SET(CB_LOGIC_OP_ENABLE, cb.logic_op_enable);
1994    COPY_IF_SET(CB_LOGIC_OP, cb.logic_op);
1995    COPY_IF_SET(CB_ATTACHMENT_COUNT, cb.attachment_count);
1996    COPY_IF_SET(CB_COLOR_WRITE_ENABLES, cb.color_write_enables);
1997    if (IS_SET_IN_SRC(CB_BLEND_ENABLES)) {
1998       for (uint32_t a = 0; a < src->cb.attachment_count; a++)
1999          COPY_MEMBER(CB_BLEND_ENABLES, cb.attachments[a].blend_enable);
2000    }
2001    if (IS_SET_IN_SRC(CB_BLEND_EQUATIONS)) {
2002       for (uint32_t a = 0; a < src->cb.attachment_count; a++) {
2003          COPY_MEMBER(CB_BLEND_EQUATIONS,
2004                      cb.attachments[a].src_color_blend_factor);
2005          COPY_MEMBER(CB_BLEND_EQUATIONS,
2006                      cb.attachments[a].dst_color_blend_factor);
2007          COPY_MEMBER(CB_BLEND_EQUATIONS,
2008                      cb.attachments[a].src_alpha_blend_factor);
2009          COPY_MEMBER(CB_BLEND_EQUATIONS,
2010                      cb.attachments[a].dst_alpha_blend_factor);
2011          COPY_MEMBER(CB_BLEND_EQUATIONS, cb.attachments[a].color_blend_op);
2012          COPY_MEMBER(CB_BLEND_EQUATIONS, cb.attachments[a].alpha_blend_op);
2013       }
2014    }
2015    if (IS_SET_IN_SRC(CB_WRITE_MASKS)) {
2016       for (uint32_t a = 0; a < src->cb.attachment_count; a++)
2017          COPY_MEMBER(CB_WRITE_MASKS, cb.attachments[a].write_mask);
2018    }
2019    if (IS_SET_IN_SRC(CB_BLEND_CONSTANTS))
2020       COPY_ARRAY(CB_BLEND_CONSTANTS, cb.blend_constants, 4);
2021 
2022 #undef IS_SET_IN_SRC
2023 #undef MARK_DIRTY
2024 #undef COPY_MEMBER
2025 #undef COPY_ARRAY
2026 #undef COPY_IF_SET
2027 
2028    for (uint32_t w = 0; w < ARRAY_SIZE(dst->dirty); w++) {
2029       /* If it's in the source but isn't set in the destination at all, mark
2030        * it dirty.  It's possible that the default values just happen to equal
2031        * the value from src.
2032        */
2033       dst->dirty[w] |= src->set[w] & ~dst->set[w];
2034 
2035       /* Everything that was in the source is now in the destination */
2036       dst->set[w] |= src->set[w];
2037    }
2038 }
2039 
2040 void
vk_cmd_set_dynamic_graphics_state(struct vk_command_buffer * cmd,const struct vk_dynamic_graphics_state * state)2041 vk_cmd_set_dynamic_graphics_state(struct vk_command_buffer *cmd,
2042                                   const struct vk_dynamic_graphics_state *state)
2043 {
2044    vk_dynamic_graphics_state_copy(&cmd->dynamic_graphics_state, state);
2045 }
2046 
2047 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetVertexInputEXT(VkCommandBuffer commandBuffer,uint32_t vertexBindingDescriptionCount,const VkVertexInputBindingDescription2EXT * pVertexBindingDescriptions,uint32_t vertexAttributeDescriptionCount,const VkVertexInputAttributeDescription2EXT * pVertexAttributeDescriptions)2048 vk_common_CmdSetVertexInputEXT(VkCommandBuffer commandBuffer,
2049    uint32_t vertexBindingDescriptionCount,
2050    const VkVertexInputBindingDescription2EXT* pVertexBindingDescriptions,
2051    uint32_t vertexAttributeDescriptionCount,
2052    const VkVertexInputAttributeDescription2EXT* pVertexAttributeDescriptions)
2053 {
2054    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2055    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2056 
2057    uint32_t bindings_valid = 0;
2058    for (uint32_t i = 0; i < vertexBindingDescriptionCount; i++) {
2059       const VkVertexInputBindingDescription2EXT *desc =
2060          &pVertexBindingDescriptions[i];
2061 
2062       assert(desc->binding < MESA_VK_MAX_VERTEX_BINDINGS);
2063       assert(desc->stride <= MESA_VK_MAX_VERTEX_BINDING_STRIDE);
2064       assert(desc->inputRate <= UINT8_MAX);
2065 
2066       const uint32_t b = desc->binding;
2067       bindings_valid |= BITFIELD_BIT(b);
2068       dyn->vi->bindings[b].stride = desc->stride;
2069       dyn->vi->bindings[b].input_rate = desc->inputRate;
2070       dyn->vi->bindings[b].divisor = desc->divisor;
2071 
2072       /* Also set bindings_strides in case a driver is keying off that */
2073       dyn->vi_binding_strides[b] = desc->stride;
2074    }
2075 
2076    dyn->vi->bindings_valid = bindings_valid;
2077    SET_DYN_VALUE(dyn, VI_BINDINGS_VALID, vi_bindings_valid, bindings_valid);
2078 
2079    uint32_t attributes_valid = 0;
2080    for (uint32_t i = 0; i < vertexAttributeDescriptionCount; i++) {
2081       const VkVertexInputAttributeDescription2EXT *desc =
2082          &pVertexAttributeDescriptions[i];
2083 
2084       assert(desc->location < MESA_VK_MAX_VERTEX_ATTRIBUTES);
2085       assert(desc->binding < MESA_VK_MAX_VERTEX_BINDINGS);
2086       assert(bindings_valid & BITFIELD_BIT(desc->binding));
2087 
2088       const uint32_t a = desc->location;
2089       attributes_valid |= BITFIELD_BIT(a);
2090       dyn->vi->attributes[a].binding = desc->binding;
2091       dyn->vi->attributes[a].format = desc->format;
2092       dyn->vi->attributes[a].offset = desc->offset;
2093    }
2094    dyn->vi->attributes_valid = attributes_valid;
2095 
2096    BITSET_SET(dyn->set, MESA_VK_DYNAMIC_VI);
2097    BITSET_SET(dyn->set, MESA_VK_DYNAMIC_VI_BINDING_STRIDES);
2098    BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_VI);
2099    BITSET_SET(dyn->dirty, MESA_VK_DYNAMIC_VI_BINDING_STRIDES);
2100 }
2101 
2102 void
vk_cmd_set_vertex_binding_strides(struct vk_command_buffer * cmd,uint32_t first_binding,uint32_t binding_count,const VkDeviceSize * strides)2103 vk_cmd_set_vertex_binding_strides(struct vk_command_buffer *cmd,
2104                                   uint32_t first_binding,
2105                                   uint32_t binding_count,
2106                                   const VkDeviceSize *strides)
2107 {
2108    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2109 
2110    for (uint32_t i = 0; i < binding_count; i++) {
2111       SET_DYN_VALUE(dyn, VI_BINDING_STRIDES,
2112                     vi_binding_strides[first_binding + i], strides[i]);
2113    }
2114 }
2115 
2116 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetPrimitiveTopology(VkCommandBuffer commandBuffer,VkPrimitiveTopology primitiveTopology)2117 vk_common_CmdSetPrimitiveTopology(VkCommandBuffer commandBuffer,
2118                                   VkPrimitiveTopology primitiveTopology)
2119 {
2120    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2121    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2122 
2123    SET_DYN_VALUE(dyn, IA_PRIMITIVE_TOPOLOGY,
2124                  ia.primitive_topology, primitiveTopology);
2125 }
2126 
2127 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetPrimitiveRestartEnable(VkCommandBuffer commandBuffer,VkBool32 primitiveRestartEnable)2128 vk_common_CmdSetPrimitiveRestartEnable(VkCommandBuffer commandBuffer,
2129                                        VkBool32 primitiveRestartEnable)
2130 {
2131    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2132    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2133 
2134    SET_DYN_BOOL(dyn, IA_PRIMITIVE_RESTART_ENABLE,
2135                 ia.primitive_restart_enable, primitiveRestartEnable);
2136 }
2137 
2138 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetPatchControlPointsEXT(VkCommandBuffer commandBuffer,uint32_t patchControlPoints)2139 vk_common_CmdSetPatchControlPointsEXT(VkCommandBuffer commandBuffer,
2140                                       uint32_t patchControlPoints)
2141 {
2142    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2143    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2144 
2145    SET_DYN_VALUE(dyn, TS_PATCH_CONTROL_POINTS,
2146                  ts.patch_control_points, patchControlPoints);
2147 }
2148 
2149 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetTessellationDomainOriginEXT(VkCommandBuffer commandBuffer,VkTessellationDomainOrigin domainOrigin)2150 vk_common_CmdSetTessellationDomainOriginEXT(VkCommandBuffer commandBuffer,
2151                                             VkTessellationDomainOrigin domainOrigin)
2152 {
2153    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2154    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2155 
2156    SET_DYN_VALUE(dyn, TS_DOMAIN_ORIGIN, ts.domain_origin, domainOrigin);
2157 }
2158 
2159 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetViewport(VkCommandBuffer commandBuffer,uint32_t firstViewport,uint32_t viewportCount,const VkViewport * pViewports)2160 vk_common_CmdSetViewport(VkCommandBuffer commandBuffer,
2161                          uint32_t firstViewport,
2162                          uint32_t viewportCount,
2163                          const VkViewport *pViewports)
2164 {
2165    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2166    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2167 
2168    SET_DYN_ARRAY(dyn, VP_VIEWPORTS, vp.viewports,
2169                  firstViewport, viewportCount, pViewports);
2170 }
2171 
2172 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetViewportWithCount(VkCommandBuffer commandBuffer,uint32_t viewportCount,const VkViewport * pViewports)2173 vk_common_CmdSetViewportWithCount(VkCommandBuffer commandBuffer,
2174                                   uint32_t viewportCount,
2175                                   const VkViewport *pViewports)
2176 {
2177    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2178    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2179 
2180    SET_DYN_VALUE(dyn, VP_VIEWPORT_COUNT, vp.viewport_count, viewportCount);
2181    SET_DYN_ARRAY(dyn, VP_VIEWPORTS, vp.viewports, 0, viewportCount, pViewports);
2182 }
2183 
2184 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetScissor(VkCommandBuffer commandBuffer,uint32_t firstScissor,uint32_t scissorCount,const VkRect2D * pScissors)2185 vk_common_CmdSetScissor(VkCommandBuffer commandBuffer,
2186                         uint32_t firstScissor,
2187                         uint32_t scissorCount,
2188                         const VkRect2D *pScissors)
2189 {
2190    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2191    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2192 
2193    SET_DYN_ARRAY(dyn, VP_SCISSORS, vp.scissors,
2194                  firstScissor, scissorCount, pScissors);
2195 }
2196 
2197 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetScissorWithCount(VkCommandBuffer commandBuffer,uint32_t scissorCount,const VkRect2D * pScissors)2198 vk_common_CmdSetScissorWithCount(VkCommandBuffer commandBuffer,
2199                                  uint32_t scissorCount,
2200                                  const VkRect2D *pScissors)
2201 {
2202    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2203    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2204 
2205    SET_DYN_VALUE(dyn, VP_SCISSOR_COUNT, vp.scissor_count, scissorCount);
2206    SET_DYN_ARRAY(dyn, VP_SCISSORS, vp.scissors, 0, scissorCount, pScissors);
2207 }
2208 
2209 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthClipNegativeOneToOneEXT(VkCommandBuffer commandBuffer,VkBool32 negativeOneToOne)2210 vk_common_CmdSetDepthClipNegativeOneToOneEXT(VkCommandBuffer commandBuffer,
2211                                              VkBool32 negativeOneToOne)
2212 {
2213    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2214    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2215 
2216    SET_DYN_BOOL(dyn, VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE,
2217                 vp.depth_clip_negative_one_to_one, negativeOneToOne);
2218 }
2219 
2220 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDiscardRectangleEXT(VkCommandBuffer commandBuffer,uint32_t firstDiscardRectangle,uint32_t discardRectangleCount,const VkRect2D * pDiscardRectangles)2221 vk_common_CmdSetDiscardRectangleEXT(VkCommandBuffer commandBuffer,
2222                                     uint32_t firstDiscardRectangle,
2223                                     uint32_t discardRectangleCount,
2224                                     const VkRect2D *pDiscardRectangles)
2225 {
2226    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2227    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2228 
2229    SET_DYN_VALUE(dyn, DR_RECTANGLES, dr.rectangle_count, discardRectangleCount);
2230    SET_DYN_ARRAY(dyn, DR_RECTANGLES, dr.rectangles, firstDiscardRectangle,
2231                  discardRectangleCount, pDiscardRectangles);
2232 }
2233 
2234 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetRasterizerDiscardEnableEXT(VkCommandBuffer commandBuffer,VkBool32 rasterizerDiscardEnable)2235 vk_common_CmdSetRasterizerDiscardEnableEXT(VkCommandBuffer commandBuffer,
2236                                            VkBool32 rasterizerDiscardEnable)
2237 {
2238    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2239    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2240 
2241    SET_DYN_BOOL(dyn, RS_RASTERIZER_DISCARD_ENABLE,
2242                 rs.rasterizer_discard_enable, rasterizerDiscardEnable);
2243 }
2244 
2245 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthClampEnableEXT(VkCommandBuffer commandBuffer,VkBool32 depthClampEnable)2246 vk_common_CmdSetDepthClampEnableEXT(VkCommandBuffer commandBuffer,
2247                                     VkBool32 depthClampEnable)
2248 {
2249    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2250    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2251 
2252    SET_DYN_BOOL(dyn, RS_DEPTH_CLAMP_ENABLE,
2253                 rs.depth_clamp_enable, depthClampEnable);
2254 }
2255 
2256 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthClipEnableEXT(VkCommandBuffer commandBuffer,VkBool32 depthClipEnable)2257 vk_common_CmdSetDepthClipEnableEXT(VkCommandBuffer commandBuffer,
2258                                    VkBool32 depthClipEnable)
2259 {
2260    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2261    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2262 
2263    SET_DYN_VALUE(dyn, RS_DEPTH_CLIP_ENABLE, rs.depth_clip_enable,
2264                  depthClipEnable ? VK_MESA_DEPTH_CLIP_ENABLE_TRUE :
2265                                    VK_MESA_DEPTH_CLIP_ENABLE_FALSE);
2266 }
2267 
2268 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetPolygonModeEXT(VkCommandBuffer commandBuffer,VkPolygonMode polygonMode)2269 vk_common_CmdSetPolygonModeEXT(VkCommandBuffer commandBuffer,
2270                                VkPolygonMode polygonMode)
2271 {
2272    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2273    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2274 
2275    SET_DYN_VALUE(dyn, RS_POLYGON_MODE, rs.polygon_mode, polygonMode);
2276 }
2277 
2278 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetCullMode(VkCommandBuffer commandBuffer,VkCullModeFlags cullMode)2279 vk_common_CmdSetCullMode(VkCommandBuffer commandBuffer,
2280                          VkCullModeFlags cullMode)
2281 {
2282    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2283    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2284 
2285    SET_DYN_VALUE(dyn, RS_CULL_MODE, rs.cull_mode, cullMode);
2286 }
2287 
2288 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetFrontFace(VkCommandBuffer commandBuffer,VkFrontFace frontFace)2289 vk_common_CmdSetFrontFace(VkCommandBuffer commandBuffer,
2290                           VkFrontFace frontFace)
2291 {
2292    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2293    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2294 
2295    SET_DYN_VALUE(dyn, RS_FRONT_FACE, rs.front_face, frontFace);
2296 }
2297 
2298 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetConservativeRasterizationModeEXT(VkCommandBuffer commandBuffer,VkConservativeRasterizationModeEXT conservativeRasterizationMode)2299 vk_common_CmdSetConservativeRasterizationModeEXT(
2300    VkCommandBuffer commandBuffer,
2301    VkConservativeRasterizationModeEXT conservativeRasterizationMode)
2302 {
2303    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2304    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2305 
2306    SET_DYN_VALUE(dyn, RS_CONSERVATIVE_MODE, rs.conservative_mode,
2307                  conservativeRasterizationMode);
2308 }
2309 
2310 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetExtraPrimitiveOverestimationSizeEXT(VkCommandBuffer commandBuffer,float extraPrimitiveOverestimationSize)2311 vk_common_CmdSetExtraPrimitiveOverestimationSizeEXT(
2312     VkCommandBuffer commandBuffer,
2313     float extraPrimitiveOverestimationSize)
2314 {
2315    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2316    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2317 
2318    SET_DYN_VALUE(dyn, RS_EXTRA_PRIMITIVE_OVERESTIMATION_SIZE,
2319                  rs.extra_primitive_overestimation_size,
2320                  extraPrimitiveOverestimationSize);
2321 }
2322 
2323 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetProvokingVertexModeEXT(VkCommandBuffer commandBuffer,VkProvokingVertexModeEXT provokingVertexMode)2324 vk_common_CmdSetProvokingVertexModeEXT(VkCommandBuffer commandBuffer,
2325                                        VkProvokingVertexModeEXT provokingVertexMode)
2326 {
2327    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2328    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2329 
2330    SET_DYN_VALUE(dyn, RS_PROVOKING_VERTEX,
2331                  rs.provoking_vertex, provokingVertexMode);
2332 }
2333 
2334 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetRasterizationStreamEXT(VkCommandBuffer commandBuffer,uint32_t rasterizationStream)2335 vk_common_CmdSetRasterizationStreamEXT(VkCommandBuffer commandBuffer,
2336                                        uint32_t rasterizationStream)
2337 {
2338    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2339    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2340 
2341    SET_DYN_VALUE(dyn, RS_PROVOKING_VERTEX,
2342                  rs.rasterization_stream, rasterizationStream);
2343 }
2344 
2345 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthBiasEnable(VkCommandBuffer commandBuffer,VkBool32 depthBiasEnable)2346 vk_common_CmdSetDepthBiasEnable(VkCommandBuffer commandBuffer,
2347                                 VkBool32 depthBiasEnable)
2348 {
2349    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2350    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2351 
2352    SET_DYN_BOOL(dyn, RS_DEPTH_BIAS_ENABLE,
2353                 rs.depth_bias.enable, depthBiasEnable);
2354 }
2355 
2356 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthBias(VkCommandBuffer commandBuffer,float depthBiasConstantFactor,float depthBiasClamp,float depthBiasSlopeFactor)2357 vk_common_CmdSetDepthBias(VkCommandBuffer commandBuffer,
2358                           float depthBiasConstantFactor,
2359                           float depthBiasClamp,
2360                           float depthBiasSlopeFactor)
2361 {
2362    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2363 
2364    VkDepthBiasInfoEXT depth_bias_info = {
2365       .sType = VK_STRUCTURE_TYPE_DEPTH_BIAS_INFO_EXT,
2366       .depthBiasConstantFactor = depthBiasConstantFactor,
2367       .depthBiasClamp = depthBiasClamp,
2368       .depthBiasSlopeFactor = depthBiasSlopeFactor,
2369    };
2370 
2371    cmd->base.device->dispatch_table.CmdSetDepthBias2EXT(commandBuffer,
2372                                                         &depth_bias_info);
2373 }
2374 
2375 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetLineWidth(VkCommandBuffer commandBuffer,float lineWidth)2376 vk_common_CmdSetLineWidth(VkCommandBuffer commandBuffer,
2377                           float lineWidth)
2378 {
2379    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2380    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2381 
2382    SET_DYN_VALUE(dyn, RS_LINE_WIDTH, rs.line.width, lineWidth);
2383 }
2384 
2385 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetLineRasterizationModeEXT(VkCommandBuffer commandBuffer,VkLineRasterizationModeEXT lineRasterizationMode)2386 vk_common_CmdSetLineRasterizationModeEXT(VkCommandBuffer commandBuffer,
2387                                          VkLineRasterizationModeEXT lineRasterizationMode)
2388 {
2389    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2390    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2391 
2392    SET_DYN_VALUE(dyn, RS_LINE_MODE, rs.line.mode, lineRasterizationMode);
2393 }
2394 
2395 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetLineStippleEnableEXT(VkCommandBuffer commandBuffer,VkBool32 stippledLineEnable)2396 vk_common_CmdSetLineStippleEnableEXT(VkCommandBuffer commandBuffer,
2397                                      VkBool32 stippledLineEnable)
2398 {
2399    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2400    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2401 
2402    SET_DYN_BOOL(dyn, RS_LINE_STIPPLE_ENABLE,
2403                 rs.line.stipple.enable, stippledLineEnable);
2404 }
2405 
2406 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetLineStippleEXT(VkCommandBuffer commandBuffer,uint32_t lineStippleFactor,uint16_t lineStipplePattern)2407 vk_common_CmdSetLineStippleEXT(VkCommandBuffer commandBuffer,
2408                                uint32_t lineStippleFactor,
2409                                uint16_t lineStipplePattern)
2410 {
2411    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2412    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2413 
2414    SET_DYN_VALUE(dyn, RS_LINE_STIPPLE,
2415                  rs.line.stipple.factor, lineStippleFactor);
2416    SET_DYN_VALUE(dyn, RS_LINE_STIPPLE,
2417                  rs.line.stipple.pattern, lineStipplePattern);
2418 }
2419 
2420 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetFragmentShadingRateKHR(VkCommandBuffer commandBuffer,const VkExtent2D * pFragmentSize,const VkFragmentShadingRateCombinerOpKHR combinerOps[2])2421 vk_common_CmdSetFragmentShadingRateKHR(VkCommandBuffer commandBuffer,
2422    const VkExtent2D *pFragmentSize,
2423    const VkFragmentShadingRateCombinerOpKHR combinerOps[2])
2424 {
2425    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2426    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2427 
2428    SET_DYN_VALUE(dyn, FSR, fsr.fragment_size.width, pFragmentSize->width);
2429    SET_DYN_VALUE(dyn, FSR, fsr.fragment_size.height, pFragmentSize->height);
2430    SET_DYN_VALUE(dyn, FSR, fsr.combiner_ops[0], combinerOps[0]);
2431    SET_DYN_VALUE(dyn, FSR, fsr.combiner_ops[1], combinerOps[1]);
2432 }
2433 
2434 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetRasterizationSamplesEXT(VkCommandBuffer commandBuffer,VkSampleCountFlagBits rasterizationSamples)2435 vk_common_CmdSetRasterizationSamplesEXT(VkCommandBuffer commandBuffer,
2436                                         VkSampleCountFlagBits rasterizationSamples)
2437 {
2438    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2439    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2440 
2441    assert(rasterizationSamples <= MESA_VK_MAX_SAMPLES);
2442 
2443    SET_DYN_VALUE(dyn, MS_RASTERIZATION_SAMPLES,
2444                  ms.rasterization_samples, rasterizationSamples);
2445 }
2446 
2447 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetSampleMaskEXT(VkCommandBuffer commandBuffer,VkSampleCountFlagBits samples,const VkSampleMask * pSampleMask)2448 vk_common_CmdSetSampleMaskEXT(VkCommandBuffer commandBuffer,
2449                               VkSampleCountFlagBits samples,
2450                               const VkSampleMask *pSampleMask)
2451 {
2452    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2453    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2454 
2455    assert(samples <= MESA_VK_MAX_SAMPLES);
2456    VkSampleMask sample_mask = *pSampleMask & BITFIELD_MASK(MESA_VK_MAX_SAMPLES);
2457 
2458    SET_DYN_VALUE(dyn, MS_SAMPLE_MASK, ms.sample_mask, sample_mask);
2459 }
2460 
2461 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetAlphaToCoverageEnableEXT(VkCommandBuffer commandBuffer,VkBool32 alphaToCoverageEnable)2462 vk_common_CmdSetAlphaToCoverageEnableEXT(VkCommandBuffer commandBuffer,
2463                                          VkBool32 alphaToCoverageEnable)
2464 {
2465    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2466    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2467 
2468    SET_DYN_VALUE(dyn, MS_ALPHA_TO_COVERAGE_ENABLE,
2469                  ms.alpha_to_coverage_enable, alphaToCoverageEnable);
2470 }
2471 
2472 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetAlphaToOneEnableEXT(VkCommandBuffer commandBuffer,VkBool32 alphaToOneEnable)2473 vk_common_CmdSetAlphaToOneEnableEXT(VkCommandBuffer commandBuffer,
2474                                     VkBool32 alphaToOneEnable)
2475 {
2476    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2477    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2478 
2479    SET_DYN_VALUE(dyn, MS_ALPHA_TO_ONE_ENABLE,
2480                  ms.alpha_to_one_enable, alphaToOneEnable);
2481 }
2482 
2483 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetSampleLocationsEXT(VkCommandBuffer commandBuffer,const VkSampleLocationsInfoEXT * pSampleLocationsInfo)2484 vk_common_CmdSetSampleLocationsEXT(VkCommandBuffer commandBuffer,
2485                                    const VkSampleLocationsInfoEXT *pSampleLocationsInfo)
2486 {
2487    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2488    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2489 
2490    SET_DYN_VALUE(dyn, MS_SAMPLE_LOCATIONS,
2491                  ms.sample_locations->per_pixel,
2492                  pSampleLocationsInfo->sampleLocationsPerPixel);
2493    SET_DYN_VALUE(dyn, MS_SAMPLE_LOCATIONS,
2494                  ms.sample_locations->grid_size.width,
2495                  pSampleLocationsInfo->sampleLocationGridSize.width);
2496    SET_DYN_VALUE(dyn, MS_SAMPLE_LOCATIONS,
2497                  ms.sample_locations->grid_size.height,
2498                  pSampleLocationsInfo->sampleLocationGridSize.height);
2499 
2500    assert(pSampleLocationsInfo->sampleLocationsCount ==
2501           pSampleLocationsInfo->sampleLocationsPerPixel *
2502           pSampleLocationsInfo->sampleLocationGridSize.width *
2503           pSampleLocationsInfo->sampleLocationGridSize.height);
2504 
2505    assert(pSampleLocationsInfo->sampleLocationsCount <=
2506           MESA_VK_MAX_SAMPLE_LOCATIONS);
2507 
2508    SET_DYN_ARRAY(dyn, MS_SAMPLE_LOCATIONS,
2509                  ms.sample_locations->locations,
2510                  0, pSampleLocationsInfo->sampleLocationsCount,
2511                  pSampleLocationsInfo->pSampleLocations);
2512 }
2513 
2514 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetSampleLocationsEnableEXT(VkCommandBuffer commandBuffer,VkBool32 sampleLocationsEnable)2515 vk_common_CmdSetSampleLocationsEnableEXT(VkCommandBuffer commandBuffer,
2516                                          VkBool32 sampleLocationsEnable)
2517 {
2518    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2519    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2520 
2521    SET_DYN_BOOL(dyn, MS_SAMPLE_LOCATIONS_ENABLE,
2522                 ms.sample_locations_enable, sampleLocationsEnable);
2523 }
2524 
2525 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthTestEnable(VkCommandBuffer commandBuffer,VkBool32 depthTestEnable)2526 vk_common_CmdSetDepthTestEnable(VkCommandBuffer commandBuffer,
2527                                 VkBool32 depthTestEnable)
2528 {
2529    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2530    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2531 
2532    SET_DYN_BOOL(dyn, DS_DEPTH_TEST_ENABLE,
2533                 ds.depth.test_enable, depthTestEnable);
2534 }
2535 
2536 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthWriteEnable(VkCommandBuffer commandBuffer,VkBool32 depthWriteEnable)2537 vk_common_CmdSetDepthWriteEnable(VkCommandBuffer commandBuffer,
2538                                 VkBool32 depthWriteEnable)
2539 {
2540    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2541    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2542 
2543    SET_DYN_BOOL(dyn, DS_DEPTH_WRITE_ENABLE,
2544                 ds.depth.write_enable, depthWriteEnable);
2545 }
2546 
2547 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthCompareOp(VkCommandBuffer commandBuffer,VkCompareOp depthCompareOp)2548 vk_common_CmdSetDepthCompareOp(VkCommandBuffer commandBuffer,
2549                                VkCompareOp depthCompareOp)
2550 {
2551    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2552    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2553 
2554    SET_DYN_VALUE(dyn, DS_DEPTH_COMPARE_OP, ds.depth.compare_op,
2555                  depthCompareOp);
2556 }
2557 
2558 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthBoundsTestEnable(VkCommandBuffer commandBuffer,VkBool32 depthBoundsTestEnable)2559 vk_common_CmdSetDepthBoundsTestEnable(VkCommandBuffer commandBuffer,
2560                                       VkBool32 depthBoundsTestEnable)
2561 {
2562    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2563    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2564 
2565    SET_DYN_BOOL(dyn, DS_DEPTH_BOUNDS_TEST_ENABLE,
2566                 ds.depth.bounds_test.enable, depthBoundsTestEnable);
2567 }
2568 
2569 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthBounds(VkCommandBuffer commandBuffer,float minDepthBounds,float maxDepthBounds)2570 vk_common_CmdSetDepthBounds(VkCommandBuffer commandBuffer,
2571                             float minDepthBounds,
2572                             float maxDepthBounds)
2573 {
2574    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2575    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2576 
2577    SET_DYN_VALUE(dyn, DS_DEPTH_BOUNDS_TEST_BOUNDS,
2578                  ds.depth.bounds_test.min, minDepthBounds);
2579    SET_DYN_VALUE(dyn, DS_DEPTH_BOUNDS_TEST_BOUNDS,
2580                  ds.depth.bounds_test.max, maxDepthBounds);
2581 }
2582 
2583 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetStencilTestEnable(VkCommandBuffer commandBuffer,VkBool32 stencilTestEnable)2584 vk_common_CmdSetStencilTestEnable(VkCommandBuffer commandBuffer,
2585                                   VkBool32 stencilTestEnable)
2586 {
2587    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2588    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2589 
2590    SET_DYN_BOOL(dyn, DS_STENCIL_TEST_ENABLE,
2591                 ds.stencil.test_enable, stencilTestEnable);
2592 }
2593 
2594 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetStencilOp(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,VkStencilOp failOp,VkStencilOp passOp,VkStencilOp depthFailOp,VkCompareOp compareOp)2595 vk_common_CmdSetStencilOp(VkCommandBuffer commandBuffer,
2596                           VkStencilFaceFlags faceMask,
2597                           VkStencilOp failOp,
2598                           VkStencilOp passOp,
2599                           VkStencilOp depthFailOp,
2600                           VkCompareOp compareOp)
2601 {
2602    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2603    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2604 
2605    if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
2606       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.front.op.fail, failOp);
2607       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.front.op.pass, passOp);
2608       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.front.op.depth_fail, depthFailOp);
2609       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.front.op.compare, compareOp);
2610    }
2611 
2612    if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
2613       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.back.op.fail, failOp);
2614       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.back.op.pass, passOp);
2615       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.back.op.depth_fail, depthFailOp);
2616       SET_DYN_VALUE(dyn, DS_STENCIL_OP, ds.stencil.back.op.compare, compareOp);
2617    }
2618 }
2619 
2620 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetStencilCompareMask(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,uint32_t compareMask)2621 vk_common_CmdSetStencilCompareMask(VkCommandBuffer commandBuffer,
2622                                    VkStencilFaceFlags faceMask,
2623                                    uint32_t compareMask)
2624 {
2625    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2626    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2627 
2628    /* We assume 8-bit stencil always */
2629    STATIC_ASSERT(sizeof(dyn->ds.stencil.front.write_mask) == 1);
2630 
2631    if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
2632       SET_DYN_VALUE(dyn, DS_STENCIL_COMPARE_MASK,
2633                     ds.stencil.front.compare_mask, (uint8_t)compareMask);
2634    }
2635    if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
2636       SET_DYN_VALUE(dyn, DS_STENCIL_COMPARE_MASK,
2637                     ds.stencil.back.compare_mask, (uint8_t)compareMask);
2638    }
2639 }
2640 
2641 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetStencilWriteMask(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,uint32_t writeMask)2642 vk_common_CmdSetStencilWriteMask(VkCommandBuffer commandBuffer,
2643                                  VkStencilFaceFlags faceMask,
2644                                  uint32_t writeMask)
2645 {
2646    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2647    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2648 
2649    /* We assume 8-bit stencil always */
2650    STATIC_ASSERT(sizeof(dyn->ds.stencil.front.write_mask) == 1);
2651 
2652    if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
2653       SET_DYN_VALUE(dyn, DS_STENCIL_WRITE_MASK,
2654                     ds.stencil.front.write_mask, (uint8_t)writeMask);
2655    }
2656    if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
2657       SET_DYN_VALUE(dyn, DS_STENCIL_WRITE_MASK,
2658                     ds.stencil.back.write_mask, (uint8_t)writeMask);
2659    }
2660 }
2661 
2662 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetStencilReference(VkCommandBuffer commandBuffer,VkStencilFaceFlags faceMask,uint32_t reference)2663 vk_common_CmdSetStencilReference(VkCommandBuffer commandBuffer,
2664                                  VkStencilFaceFlags faceMask,
2665                                  uint32_t reference)
2666 {
2667    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2668    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2669 
2670    /* We assume 8-bit stencil always */
2671    STATIC_ASSERT(sizeof(dyn->ds.stencil.front.write_mask) == 1);
2672 
2673    if (faceMask & VK_STENCIL_FACE_FRONT_BIT) {
2674       SET_DYN_VALUE(dyn, DS_STENCIL_REFERENCE,
2675                     ds.stencil.front.reference, (uint8_t)reference);
2676    }
2677    if (faceMask & VK_STENCIL_FACE_BACK_BIT) {
2678       SET_DYN_VALUE(dyn, DS_STENCIL_REFERENCE,
2679                     ds.stencil.back.reference, (uint8_t)reference);
2680    }
2681 }
2682 
2683 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetLogicOpEnableEXT(VkCommandBuffer commandBuffer,VkBool32 logicOpEnable)2684 vk_common_CmdSetLogicOpEnableEXT(VkCommandBuffer commandBuffer,
2685                                  VkBool32 logicOpEnable)
2686 {
2687    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2688    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2689 
2690    SET_DYN_BOOL(dyn, CB_LOGIC_OP_ENABLE, cb.logic_op_enable, logicOpEnable);
2691 }
2692 
2693 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetLogicOpEXT(VkCommandBuffer commandBuffer,VkLogicOp logicOp)2694 vk_common_CmdSetLogicOpEXT(VkCommandBuffer commandBuffer,
2695                            VkLogicOp logicOp)
2696 {
2697    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2698    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2699 
2700    SET_DYN_VALUE(dyn, CB_LOGIC_OP, cb.logic_op, logicOp);
2701 }
2702 
2703 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetColorWriteEnableEXT(VkCommandBuffer commandBuffer,uint32_t attachmentCount,const VkBool32 * pColorWriteEnables)2704 vk_common_CmdSetColorWriteEnableEXT(VkCommandBuffer commandBuffer,
2705                                     uint32_t attachmentCount,
2706                                     const VkBool32 *pColorWriteEnables)
2707 {
2708    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2709    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2710 
2711    assert(attachmentCount <= MESA_VK_MAX_COLOR_ATTACHMENTS);
2712 
2713    uint8_t color_write_enables = 0;
2714    for (uint32_t a = 0; a < attachmentCount; a++) {
2715       if (pColorWriteEnables[a])
2716          color_write_enables |= BITFIELD_BIT(a);
2717    }
2718 
2719    SET_DYN_VALUE(dyn, CB_COLOR_WRITE_ENABLES,
2720                  cb.color_write_enables, color_write_enables);
2721 }
2722 
2723 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetColorBlendEnableEXT(VkCommandBuffer commandBuffer,uint32_t firstAttachment,uint32_t attachmentCount,const VkBool32 * pColorBlendEnables)2724 vk_common_CmdSetColorBlendEnableEXT(VkCommandBuffer commandBuffer,
2725                                     uint32_t firstAttachment,
2726                                     uint32_t attachmentCount,
2727                                     const VkBool32 *pColorBlendEnables)
2728 {
2729    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2730    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2731 
2732    for (uint32_t i = 0; i < attachmentCount; i++) {
2733       uint32_t a = firstAttachment + i;
2734       assert(a < ARRAY_SIZE(dyn->cb.attachments));
2735 
2736       SET_DYN_BOOL(dyn, CB_BLEND_ENABLES,
2737                    cb.attachments[a].blend_enable, pColorBlendEnables[i]);
2738    }
2739 }
2740 
2741 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetColorBlendEquationEXT(VkCommandBuffer commandBuffer,uint32_t firstAttachment,uint32_t attachmentCount,const VkColorBlendEquationEXT * pColorBlendEquations)2742 vk_common_CmdSetColorBlendEquationEXT(VkCommandBuffer commandBuffer,
2743                                       uint32_t firstAttachment,
2744                                       uint32_t attachmentCount,
2745                                       const VkColorBlendEquationEXT *pColorBlendEquations)
2746 {
2747    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2748    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2749 
2750    for (uint32_t i = 0; i < attachmentCount; i++) {
2751       uint32_t a = firstAttachment + i;
2752       assert(a < ARRAY_SIZE(dyn->cb.attachments));
2753 
2754       SET_DYN_VALUE(dyn, CB_BLEND_EQUATIONS,
2755                     cb.attachments[a].src_color_blend_factor,
2756                     pColorBlendEquations[i].srcColorBlendFactor);
2757 
2758       SET_DYN_VALUE(dyn, CB_BLEND_EQUATIONS,
2759                     cb.attachments[a].dst_color_blend_factor,
2760                     pColorBlendEquations[i].dstColorBlendFactor);
2761 
2762       SET_DYN_VALUE(dyn, CB_BLEND_EQUATIONS,
2763                     cb.attachments[a].color_blend_op,
2764                     pColorBlendEquations[i].colorBlendOp);
2765 
2766       SET_DYN_VALUE(dyn, CB_BLEND_EQUATIONS,
2767                     cb.attachments[a].src_alpha_blend_factor,
2768                     pColorBlendEquations[i].srcAlphaBlendFactor);
2769 
2770       SET_DYN_VALUE(dyn, CB_BLEND_EQUATIONS,
2771                     cb.attachments[a].dst_alpha_blend_factor,
2772                     pColorBlendEquations[i].dstAlphaBlendFactor);
2773 
2774       SET_DYN_VALUE(dyn, CB_BLEND_EQUATIONS,
2775                     cb.attachments[a].alpha_blend_op,
2776                     pColorBlendEquations[i].alphaBlendOp);
2777    }
2778 }
2779 
2780 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetColorWriteMaskEXT(VkCommandBuffer commandBuffer,uint32_t firstAttachment,uint32_t attachmentCount,const VkColorComponentFlags * pColorWriteMasks)2781 vk_common_CmdSetColorWriteMaskEXT(VkCommandBuffer commandBuffer,
2782                                   uint32_t firstAttachment,
2783                                   uint32_t attachmentCount,
2784                                   const VkColorComponentFlags *pColorWriteMasks)
2785 {
2786    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2787    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2788 
2789    for (uint32_t i = 0; i < attachmentCount; i++) {
2790       uint32_t a = firstAttachment + i;
2791       assert(a < ARRAY_SIZE(dyn->cb.attachments));
2792 
2793       SET_DYN_VALUE(dyn, CB_WRITE_MASKS,
2794                     cb.attachments[a].write_mask, pColorWriteMasks[i]);
2795    }
2796 }
2797 
2798 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetBlendConstants(VkCommandBuffer commandBuffer,const float blendConstants[4])2799 vk_common_CmdSetBlendConstants(VkCommandBuffer commandBuffer,
2800                                const float  blendConstants[4])
2801 {
2802    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2803    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2804 
2805    SET_DYN_ARRAY(dyn, CB_BLEND_CONSTANTS, cb.blend_constants,
2806                  0, 4, blendConstants);
2807 }
2808 
2809 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetColorBlendAdvancedEXT(VkCommandBuffer commandBuffer,uint32_t firstAttachment,uint32_t attachmentCount,const VkColorBlendAdvancedEXT * pColorBlendAdvanced)2810 vk_common_CmdSetColorBlendAdvancedEXT(VkCommandBuffer commandBuffer,
2811                                       uint32_t firstAttachment,
2812                                       uint32_t attachmentCount,
2813                                       const VkColorBlendAdvancedEXT* pColorBlendAdvanced)
2814 {
2815    unreachable("VK_EXT_blend_operation_advanced unsupported");
2816 }
2817 
2818 void
vk_cmd_set_cb_attachment_count(struct vk_command_buffer * cmd,uint32_t attachment_count)2819 vk_cmd_set_cb_attachment_count(struct vk_command_buffer *cmd,
2820                                uint32_t attachment_count)
2821 {
2822    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2823 
2824    SET_DYN_VALUE(dyn, CB_ATTACHMENT_COUNT, cb.attachment_count, attachment_count);
2825 }
2826 
2827 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDiscardRectangleEnableEXT(VkCommandBuffer commandBuffer,VkBool32 discardRectangleEnable)2828 vk_common_CmdSetDiscardRectangleEnableEXT(VkCommandBuffer commandBuffer,
2829                                           VkBool32 discardRectangleEnable)
2830 {
2831    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2832    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2833 
2834    SET_DYN_VALUE(dyn, DR_ENABLE, dr.enable, discardRectangleEnable);
2835 }
2836 
2837 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDiscardRectangleModeEXT(VkCommandBuffer commandBuffer,VkDiscardRectangleModeEXT discardRectangleMode)2838 vk_common_CmdSetDiscardRectangleModeEXT(VkCommandBuffer commandBuffer,
2839                                         VkDiscardRectangleModeEXT discardRectangleMode)
2840 {
2841    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2842    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2843 
2844    SET_DYN_VALUE(dyn, DR_MODE, dr.mode, discardRectangleMode);
2845 }
2846 
2847 VKAPI_ATTR void VKAPI_CALL
vk_common_CmdSetDepthBias2EXT(VkCommandBuffer commandBuffer,const VkDepthBiasInfoEXT * pDepthBiasInfo)2848 vk_common_CmdSetDepthBias2EXT(
2849     VkCommandBuffer                             commandBuffer,
2850     const VkDepthBiasInfoEXT*                   pDepthBiasInfo)
2851 {
2852    VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer);
2853    struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state;
2854 
2855    SET_DYN_VALUE(dyn, RS_DEPTH_BIAS_FACTORS,
2856                  rs.depth_bias.constant, pDepthBiasInfo->depthBiasConstantFactor);
2857    SET_DYN_VALUE(dyn, RS_DEPTH_BIAS_FACTORS,
2858                  rs.depth_bias.clamp, pDepthBiasInfo->depthBiasClamp);
2859    SET_DYN_VALUE(dyn, RS_DEPTH_BIAS_FACTORS,
2860                  rs.depth_bias.slope, pDepthBiasInfo->depthBiasSlopeFactor);
2861 
2862    /** From the Vulkan 1.3.254 spec:
2863     *
2864     *    "If pNext does not contain a VkDepthBiasRepresentationInfoEXT
2865     *     structure, then this command is equivalent to including a
2866     *     VkDepthBiasRepresentationInfoEXT with depthBiasExact set to VK_FALSE
2867     *     and depthBiasRepresentation set to
2868     *     VK_DEPTH_BIAS_REPRESENTATION_LEAST_REPRESENTABLE_VALUE_FORMAT_EXT."
2869     */
2870    const VkDepthBiasRepresentationInfoEXT *dbr_info =
2871       vk_find_struct_const(pDepthBiasInfo->pNext, DEPTH_BIAS_REPRESENTATION_INFO_EXT);
2872    if (dbr_info) {
2873       SET_DYN_VALUE(dyn, RS_DEPTH_BIAS_FACTORS,
2874                     rs.depth_bias.representation, dbr_info->depthBiasRepresentation);
2875       SET_DYN_VALUE(dyn, RS_DEPTH_BIAS_FACTORS,
2876                     rs.depth_bias.exact, dbr_info->depthBiasExact);
2877    } else {
2878       SET_DYN_VALUE(dyn, RS_DEPTH_BIAS_FACTORS,
2879                     rs.depth_bias.representation,
2880                     VK_DEPTH_BIAS_REPRESENTATION_LEAST_REPRESENTABLE_VALUE_FORMAT_EXT);
2881       SET_DYN_VALUE(dyn, RS_DEPTH_BIAS_FACTORS,
2882                     rs.depth_bias.exact, false);
2883    }
2884 }
2885 
2886 const char *
vk_dynamic_graphic_state_to_str(enum mesa_vk_dynamic_graphics_state state)2887 vk_dynamic_graphic_state_to_str(enum mesa_vk_dynamic_graphics_state state)
2888 {
2889 #define NAME(name) \
2890       case MESA_VK_DYNAMIC_##name: return #name
2891 
2892    switch (state) {
2893       NAME(VI);
2894       NAME(VI_BINDINGS_VALID);
2895       NAME(VI_BINDING_STRIDES);
2896       NAME(IA_PRIMITIVE_TOPOLOGY);
2897       NAME(IA_PRIMITIVE_RESTART_ENABLE);
2898       NAME(TS_PATCH_CONTROL_POINTS);
2899       NAME(TS_DOMAIN_ORIGIN);
2900       NAME(VP_VIEWPORT_COUNT);
2901       NAME(VP_VIEWPORTS);
2902       NAME(VP_SCISSOR_COUNT);
2903       NAME(VP_SCISSORS);
2904       NAME(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE);
2905       NAME(DR_RECTANGLES);
2906       NAME(DR_MODE);
2907       NAME(DR_ENABLE);
2908       NAME(RS_RASTERIZER_DISCARD_ENABLE);
2909       NAME(RS_DEPTH_CLAMP_ENABLE);
2910       NAME(RS_DEPTH_CLIP_ENABLE);
2911       NAME(RS_POLYGON_MODE);
2912       NAME(RS_CULL_MODE);
2913       NAME(RS_FRONT_FACE);
2914       NAME(RS_CONSERVATIVE_MODE);
2915       NAME(RS_EXTRA_PRIMITIVE_OVERESTIMATION_SIZE);
2916       NAME(RS_RASTERIZATION_ORDER_AMD);
2917       NAME(RS_PROVOKING_VERTEX);
2918       NAME(RS_RASTERIZATION_STREAM);
2919       NAME(RS_DEPTH_BIAS_ENABLE);
2920       NAME(RS_DEPTH_BIAS_FACTORS);
2921       NAME(RS_LINE_WIDTH);
2922       NAME(RS_LINE_MODE);
2923       NAME(RS_LINE_STIPPLE_ENABLE);
2924       NAME(RS_LINE_STIPPLE);
2925       NAME(FSR);
2926       NAME(MS_RASTERIZATION_SAMPLES);
2927       NAME(MS_SAMPLE_MASK);
2928       NAME(MS_ALPHA_TO_COVERAGE_ENABLE);
2929       NAME(MS_ALPHA_TO_ONE_ENABLE);
2930       NAME(MS_SAMPLE_LOCATIONS_ENABLE);
2931       NAME(MS_SAMPLE_LOCATIONS);
2932       NAME(DS_DEPTH_TEST_ENABLE);
2933       NAME(DS_DEPTH_WRITE_ENABLE);
2934       NAME(DS_DEPTH_COMPARE_OP);
2935       NAME(DS_DEPTH_BOUNDS_TEST_ENABLE);
2936       NAME(DS_DEPTH_BOUNDS_TEST_BOUNDS);
2937       NAME(DS_STENCIL_TEST_ENABLE);
2938       NAME(DS_STENCIL_OP);
2939       NAME(DS_STENCIL_COMPARE_MASK);
2940       NAME(DS_STENCIL_WRITE_MASK);
2941       NAME(DS_STENCIL_REFERENCE);
2942       NAME(CB_LOGIC_OP_ENABLE);
2943       NAME(CB_LOGIC_OP);
2944       NAME(CB_ATTACHMENT_COUNT);
2945       NAME(CB_COLOR_WRITE_ENABLES);
2946       NAME(CB_BLEND_ENABLES);
2947       NAME(CB_BLEND_EQUATIONS);
2948       NAME(CB_WRITE_MASKS);
2949       NAME(CB_BLEND_CONSTANTS);
2950       NAME(ATTACHMENT_FEEDBACK_LOOP_ENABLE);
2951    default: unreachable("Invalid state");
2952    }
2953 
2954 #undef NAME
2955 }
2956