anv: handle dynamic viewport count
[mesa.git] / src / intel / vulkan / genX_pipeline.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_private.h"
25
26 #include "genxml/gen_macros.h"
27 #include "genxml/genX_pack.h"
28
29 #include "common/gen_l3_config.h"
30 #include "common/gen_sample_positions.h"
31 #include "nir/nir_xfb_info.h"
32 #include "vk_util.h"
33 #include "vk_format_info.h"
34
35 static uint32_t
36 vertex_element_comp_control(enum isl_format format, unsigned comp)
37 {
38 uint8_t bits;
39 switch (comp) {
40 case 0: bits = isl_format_layouts[format].channels.r.bits; break;
41 case 1: bits = isl_format_layouts[format].channels.g.bits; break;
42 case 2: bits = isl_format_layouts[format].channels.b.bits; break;
43 case 3: bits = isl_format_layouts[format].channels.a.bits; break;
44 default: unreachable("Invalid component");
45 }
46
47 /*
48 * Take in account hardware restrictions when dealing with 64-bit floats.
49 *
50 * From Broadwell spec, command reference structures, page 586:
51 * "When SourceElementFormat is set to one of the *64*_PASSTHRU formats,
52 * 64-bit components are stored * in the URB without any conversion. In
53 * this case, vertex elements must be written as 128 or 256 bits, with
54 * VFCOMP_STORE_0 being used to pad the output as required. E.g., if
55 * R64_PASSTHRU is used to copy a 64-bit Red component into the URB,
56 * Component 1 must be specified as VFCOMP_STORE_0 (with Components 2,3
57 * set to VFCOMP_NOSTORE) in order to output a 128-bit vertex element, or
58 * Components 1-3 must be specified as VFCOMP_STORE_0 in order to output
59 * a 256-bit vertex element. Likewise, use of R64G64B64_PASSTHRU requires
60 * Component 3 to be specified as VFCOMP_STORE_0 in order to output a
61 * 256-bit vertex element."
62 */
63 if (bits) {
64 return VFCOMP_STORE_SRC;
65 } else if (comp >= 2 &&
66 !isl_format_layouts[format].channels.b.bits &&
67 isl_format_layouts[format].channels.r.type == ISL_RAW) {
68 /* When emitting 64-bit attributes, we need to write either 128 or 256
69 * bit chunks, using VFCOMP_NOSTORE when not writing the chunk, and
70 * VFCOMP_STORE_0 to pad the written chunk */
71 return VFCOMP_NOSTORE;
72 } else if (comp < 3 ||
73 isl_format_layouts[format].channels.r.type == ISL_RAW) {
74 /* Note we need to pad with value 0, not 1, due hardware restrictions
75 * (see comment above) */
76 return VFCOMP_STORE_0;
77 } else if (isl_format_layouts[format].channels.r.type == ISL_UINT ||
78 isl_format_layouts[format].channels.r.type == ISL_SINT) {
79 assert(comp == 3);
80 return VFCOMP_STORE_1_INT;
81 } else {
82 assert(comp == 3);
83 return VFCOMP_STORE_1_FP;
84 }
85 }
86
87 static void
88 emit_vertex_input(struct anv_graphics_pipeline *pipeline,
89 const VkPipelineVertexInputStateCreateInfo *info)
90 {
91 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
92
93 /* Pull inputs_read out of the VS prog data */
94 const uint64_t inputs_read = vs_prog_data->inputs_read;
95 const uint64_t double_inputs_read =
96 vs_prog_data->double_inputs_read & inputs_read;
97 assert((inputs_read & ((1 << VERT_ATTRIB_GENERIC0) - 1)) == 0);
98 const uint32_t elements = inputs_read >> VERT_ATTRIB_GENERIC0;
99 const uint32_t elements_double = double_inputs_read >> VERT_ATTRIB_GENERIC0;
100 const bool needs_svgs_elem = vs_prog_data->uses_vertexid ||
101 vs_prog_data->uses_instanceid ||
102 vs_prog_data->uses_firstvertex ||
103 vs_prog_data->uses_baseinstance;
104
105 uint32_t elem_count = __builtin_popcount(elements) -
106 __builtin_popcount(elements_double) / 2;
107
108 const uint32_t total_elems =
109 MAX2(1, elem_count + needs_svgs_elem + vs_prog_data->uses_drawid);
110
111 uint32_t *p;
112
113 const uint32_t num_dwords = 1 + total_elems * 2;
114 p = anv_batch_emitn(&pipeline->base.batch, num_dwords,
115 GENX(3DSTATE_VERTEX_ELEMENTS));
116 if (!p)
117 return;
118
119 for (uint32_t i = 0; i < total_elems; i++) {
120 /* The SKL docs for VERTEX_ELEMENT_STATE say:
121 *
122 * "All elements must be valid from Element[0] to the last valid
123 * element. (I.e. if Element[2] is valid then Element[1] and
124 * Element[0] must also be valid)."
125 *
126 * The SKL docs for 3D_Vertex_Component_Control say:
127 *
128 * "Don't store this component. (Not valid for Component 0, but can
129 * be used for Component 1-3)."
130 *
131 * So we can't just leave a vertex element blank and hope for the best.
132 * We have to tell the VF hardware to put something in it; so we just
133 * store a bunch of zero.
134 *
135 * TODO: Compact vertex elements so we never end up with holes.
136 */
137 struct GENX(VERTEX_ELEMENT_STATE) element = {
138 .Valid = true,
139 .Component0Control = VFCOMP_STORE_0,
140 .Component1Control = VFCOMP_STORE_0,
141 .Component2Control = VFCOMP_STORE_0,
142 .Component3Control = VFCOMP_STORE_0,
143 };
144 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + i * 2], &element);
145 }
146
147 for (uint32_t i = 0; i < info->vertexAttributeDescriptionCount; i++) {
148 const VkVertexInputAttributeDescription *desc =
149 &info->pVertexAttributeDescriptions[i];
150 enum isl_format format = anv_get_isl_format(&pipeline->base.device->info,
151 desc->format,
152 VK_IMAGE_ASPECT_COLOR_BIT,
153 VK_IMAGE_TILING_LINEAR);
154
155 assert(desc->binding < MAX_VBS);
156
157 if ((elements & (1 << desc->location)) == 0)
158 continue; /* Binding unused */
159
160 uint32_t slot =
161 __builtin_popcount(elements & ((1 << desc->location) - 1)) -
162 DIV_ROUND_UP(__builtin_popcount(elements_double &
163 ((1 << desc->location) -1)), 2);
164
165 struct GENX(VERTEX_ELEMENT_STATE) element = {
166 .VertexBufferIndex = desc->binding,
167 .Valid = true,
168 .SourceElementFormat = format,
169 .EdgeFlagEnable = false,
170 .SourceElementOffset = desc->offset,
171 .Component0Control = vertex_element_comp_control(format, 0),
172 .Component1Control = vertex_element_comp_control(format, 1),
173 .Component2Control = vertex_element_comp_control(format, 2),
174 .Component3Control = vertex_element_comp_control(format, 3),
175 };
176 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + slot * 2], &element);
177
178 #if GEN_GEN >= 8
179 /* On Broadwell and later, we have a separate VF_INSTANCING packet
180 * that controls instancing. On Haswell and prior, that's part of
181 * VERTEX_BUFFER_STATE which we emit later.
182 */
183 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_VF_INSTANCING), vfi) {
184 vfi.InstancingEnable = pipeline->vb[desc->binding].instanced;
185 vfi.VertexElementIndex = slot;
186 vfi.InstanceDataStepRate =
187 pipeline->vb[desc->binding].instance_divisor;
188 }
189 #endif
190 }
191
192 const uint32_t id_slot = elem_count;
193 if (needs_svgs_elem) {
194 /* From the Broadwell PRM for the 3D_Vertex_Component_Control enum:
195 * "Within a VERTEX_ELEMENT_STATE structure, if a Component
196 * Control field is set to something other than VFCOMP_STORE_SRC,
197 * no higher-numbered Component Control fields may be set to
198 * VFCOMP_STORE_SRC"
199 *
200 * This means, that if we have BaseInstance, we need BaseVertex as
201 * well. Just do all or nothing.
202 */
203 uint32_t base_ctrl = (vs_prog_data->uses_firstvertex ||
204 vs_prog_data->uses_baseinstance) ?
205 VFCOMP_STORE_SRC : VFCOMP_STORE_0;
206
207 struct GENX(VERTEX_ELEMENT_STATE) element = {
208 .VertexBufferIndex = ANV_SVGS_VB_INDEX,
209 .Valid = true,
210 .SourceElementFormat = ISL_FORMAT_R32G32_UINT,
211 .Component0Control = base_ctrl,
212 .Component1Control = base_ctrl,
213 #if GEN_GEN >= 8
214 .Component2Control = VFCOMP_STORE_0,
215 .Component3Control = VFCOMP_STORE_0,
216 #else
217 .Component2Control = VFCOMP_STORE_VID,
218 .Component3Control = VFCOMP_STORE_IID,
219 #endif
220 };
221 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + id_slot * 2], &element);
222
223 #if GEN_GEN >= 8
224 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_VF_INSTANCING), vfi) {
225 vfi.VertexElementIndex = id_slot;
226 }
227 #endif
228 }
229
230 #if GEN_GEN >= 8
231 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_VF_SGVS), sgvs) {
232 sgvs.VertexIDEnable = vs_prog_data->uses_vertexid;
233 sgvs.VertexIDComponentNumber = 2;
234 sgvs.VertexIDElementOffset = id_slot;
235 sgvs.InstanceIDEnable = vs_prog_data->uses_instanceid;
236 sgvs.InstanceIDComponentNumber = 3;
237 sgvs.InstanceIDElementOffset = id_slot;
238 }
239 #endif
240
241 const uint32_t drawid_slot = elem_count + needs_svgs_elem;
242 if (vs_prog_data->uses_drawid) {
243 struct GENX(VERTEX_ELEMENT_STATE) element = {
244 .VertexBufferIndex = ANV_DRAWID_VB_INDEX,
245 .Valid = true,
246 .SourceElementFormat = ISL_FORMAT_R32_UINT,
247 .Component0Control = VFCOMP_STORE_SRC,
248 .Component1Control = VFCOMP_STORE_0,
249 .Component2Control = VFCOMP_STORE_0,
250 .Component3Control = VFCOMP_STORE_0,
251 };
252 GENX(VERTEX_ELEMENT_STATE_pack)(NULL,
253 &p[1 + drawid_slot * 2],
254 &element);
255
256 #if GEN_GEN >= 8
257 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_VF_INSTANCING), vfi) {
258 vfi.VertexElementIndex = drawid_slot;
259 }
260 #endif
261 }
262 }
263
264 void
265 genX(emit_urb_setup)(struct anv_device *device, struct anv_batch *batch,
266 const struct gen_l3_config *l3_config,
267 VkShaderStageFlags active_stages,
268 const unsigned entry_size[4],
269 enum gen_urb_deref_block_size *deref_block_size)
270 {
271 const struct gen_device_info *devinfo = &device->info;
272
273 unsigned entries[4];
274 unsigned start[4];
275 gen_get_urb_config(devinfo, l3_config,
276 active_stages &
277 VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT,
278 active_stages & VK_SHADER_STAGE_GEOMETRY_BIT,
279 entry_size, entries, start, deref_block_size);
280
281 #if GEN_GEN == 7 && !GEN_IS_HASWELL
282 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
283 *
284 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth stall
285 * needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
286 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
287 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL
288 * needs to be sent before any combination of VS associated 3DSTATE."
289 */
290 anv_batch_emit(batch, GEN7_PIPE_CONTROL, pc) {
291 pc.DepthStallEnable = true;
292 pc.PostSyncOperation = WriteImmediateData;
293 pc.Address = device->workaround_address;
294 }
295 #endif
296
297 for (int i = 0; i <= MESA_SHADER_GEOMETRY; i++) {
298 anv_batch_emit(batch, GENX(3DSTATE_URB_VS), urb) {
299 urb._3DCommandSubOpcode += i;
300 urb.VSURBStartingAddress = start[i];
301 urb.VSURBEntryAllocationSize = entry_size[i] - 1;
302 urb.VSNumberofURBEntries = entries[i];
303 }
304 }
305 }
306
307 static void
308 emit_urb_setup(struct anv_graphics_pipeline *pipeline,
309 enum gen_urb_deref_block_size *deref_block_size)
310 {
311 unsigned entry_size[4];
312 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
313 const struct brw_vue_prog_data *prog_data =
314 !anv_pipeline_has_stage(pipeline, i) ? NULL :
315 (const struct brw_vue_prog_data *) pipeline->shaders[i]->prog_data;
316
317 entry_size[i] = prog_data ? prog_data->urb_entry_size : 1;
318 }
319
320 genX(emit_urb_setup)(pipeline->base.device, &pipeline->base.batch,
321 pipeline->base.l3_config,
322 pipeline->active_stages, entry_size,
323 deref_block_size);
324 }
325
326 static void
327 emit_3dstate_sbe(struct anv_graphics_pipeline *pipeline)
328 {
329 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
330
331 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
332 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_SBE), sbe);
333 #if GEN_GEN >= 8
334 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_SBE_SWIZ), sbe);
335 #endif
336 return;
337 }
338
339 const struct brw_vue_map *fs_input_map =
340 &anv_pipeline_get_last_vue_prog_data(pipeline)->vue_map;
341
342 struct GENX(3DSTATE_SBE) sbe = {
343 GENX(3DSTATE_SBE_header),
344 .AttributeSwizzleEnable = true,
345 .PointSpriteTextureCoordinateOrigin = UPPERLEFT,
346 .NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs,
347 .ConstantInterpolationEnable = wm_prog_data->flat_inputs,
348 };
349
350 #if GEN_GEN >= 9
351 for (unsigned i = 0; i < 32; i++)
352 sbe.AttributeActiveComponentFormat[i] = ACF_XYZW;
353 #endif
354
355 #if GEN_GEN >= 8
356 /* On Broadwell, they broke 3DSTATE_SBE into two packets */
357 struct GENX(3DSTATE_SBE_SWIZ) swiz = {
358 GENX(3DSTATE_SBE_SWIZ_header),
359 };
360 #else
361 # define swiz sbe
362 #endif
363
364 int first_slot = brw_compute_first_urb_slot_required(wm_prog_data->inputs,
365 fs_input_map);
366 assert(first_slot % 2 == 0);
367 unsigned urb_entry_read_offset = first_slot / 2;
368 int max_source_attr = 0;
369 for (uint8_t idx = 0; idx < wm_prog_data->urb_setup_attribs_count; idx++) {
370 uint8_t attr = wm_prog_data->urb_setup_attribs[idx];
371 int input_index = wm_prog_data->urb_setup[attr];
372
373 assert(0 <= input_index);
374
375 /* gl_Viewport and gl_Layer are stored in the VUE header */
376 if (attr == VARYING_SLOT_VIEWPORT || attr == VARYING_SLOT_LAYER) {
377 continue;
378 }
379
380 if (attr == VARYING_SLOT_PNTC) {
381 sbe.PointSpriteTextureCoordinateEnable = 1 << input_index;
382 continue;
383 }
384
385 const int slot = fs_input_map->varying_to_slot[attr];
386
387 if (slot == -1) {
388 /* This attribute does not exist in the VUE--that means that the
389 * vertex shader did not write to it. It could be that it's a
390 * regular varying read by the fragment shader but not written by
391 * the vertex shader or it's gl_PrimitiveID. In the first case the
392 * value is undefined, in the second it needs to be
393 * gl_PrimitiveID.
394 */
395 swiz.Attribute[input_index].ConstantSource = PRIM_ID;
396 swiz.Attribute[input_index].ComponentOverrideX = true;
397 swiz.Attribute[input_index].ComponentOverrideY = true;
398 swiz.Attribute[input_index].ComponentOverrideZ = true;
399 swiz.Attribute[input_index].ComponentOverrideW = true;
400 continue;
401 }
402
403 /* We have to subtract two slots to accout for the URB entry output
404 * read offset in the VS and GS stages.
405 */
406 const int source_attr = slot - 2 * urb_entry_read_offset;
407 assert(source_attr >= 0 && source_attr < 32);
408 max_source_attr = MAX2(max_source_attr, source_attr);
409 /* The hardware can only do overrides on 16 overrides at a time, and the
410 * other up to 16 have to be lined up so that the input index = the
411 * output index. We'll need to do some tweaking to make sure that's the
412 * case.
413 */
414 if (input_index < 16)
415 swiz.Attribute[input_index].SourceAttribute = source_attr;
416 else
417 assert(source_attr == input_index);
418 }
419
420 sbe.VertexURBEntryReadOffset = urb_entry_read_offset;
421 sbe.VertexURBEntryReadLength = DIV_ROUND_UP(max_source_attr + 1, 2);
422 #if GEN_GEN >= 8
423 sbe.ForceVertexURBEntryReadOffset = true;
424 sbe.ForceVertexURBEntryReadLength = true;
425 #endif
426
427 uint32_t *dw = anv_batch_emit_dwords(&pipeline->base.batch,
428 GENX(3DSTATE_SBE_length));
429 if (!dw)
430 return;
431 GENX(3DSTATE_SBE_pack)(&pipeline->base.batch, dw, &sbe);
432
433 #if GEN_GEN >= 8
434 dw = anv_batch_emit_dwords(&pipeline->base.batch, GENX(3DSTATE_SBE_SWIZ_length));
435 if (!dw)
436 return;
437 GENX(3DSTATE_SBE_SWIZ_pack)(&pipeline->base.batch, dw, &swiz);
438 #endif
439 }
440
441 static const uint32_t vk_to_gen_cullmode[] = {
442 [VK_CULL_MODE_NONE] = CULLMODE_NONE,
443 [VK_CULL_MODE_FRONT_BIT] = CULLMODE_FRONT,
444 [VK_CULL_MODE_BACK_BIT] = CULLMODE_BACK,
445 [VK_CULL_MODE_FRONT_AND_BACK] = CULLMODE_BOTH
446 };
447
448 static const uint32_t vk_to_gen_fillmode[] = {
449 [VK_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
450 [VK_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
451 [VK_POLYGON_MODE_POINT] = FILL_MODE_POINT,
452 };
453
454 static const uint32_t vk_to_gen_front_face[] = {
455 [VK_FRONT_FACE_COUNTER_CLOCKWISE] = 1,
456 [VK_FRONT_FACE_CLOCKWISE] = 0
457 };
458
459 static VkLineRasterizationModeEXT
460 vk_line_rasterization_mode(const VkPipelineRasterizationLineStateCreateInfoEXT *line_info,
461 const VkPipelineMultisampleStateCreateInfo *ms_info)
462 {
463 VkLineRasterizationModeEXT line_mode =
464 line_info ? line_info->lineRasterizationMode :
465 VK_LINE_RASTERIZATION_MODE_DEFAULT_EXT;
466
467 if (line_mode == VK_LINE_RASTERIZATION_MODE_DEFAULT_EXT) {
468 if (ms_info && ms_info->rasterizationSamples > 1) {
469 return VK_LINE_RASTERIZATION_MODE_RECTANGULAR_EXT;
470 } else {
471 return VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT;
472 }
473 }
474
475 return line_mode;
476 }
477
478 /** Returns the final polygon mode for rasterization
479 *
480 * This function takes into account polygon mode, primitive topology and the
481 * different shader stages which might generate their own type of primitives.
482 */
483 static VkPolygonMode
484 anv_raster_polygon_mode(struct anv_graphics_pipeline *pipeline,
485 const VkPipelineInputAssemblyStateCreateInfo *ia_info,
486 const VkPipelineRasterizationStateCreateInfo *rs_info)
487 {
488 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY)) {
489 switch (get_gs_prog_data(pipeline)->output_topology) {
490 case _3DPRIM_POINTLIST:
491 return VK_POLYGON_MODE_POINT;
492
493 case _3DPRIM_LINELIST:
494 case _3DPRIM_LINESTRIP:
495 case _3DPRIM_LINELOOP:
496 return VK_POLYGON_MODE_LINE;
497
498 case _3DPRIM_TRILIST:
499 case _3DPRIM_TRIFAN:
500 case _3DPRIM_TRISTRIP:
501 case _3DPRIM_RECTLIST:
502 case _3DPRIM_QUADLIST:
503 case _3DPRIM_QUADSTRIP:
504 case _3DPRIM_POLYGON:
505 return rs_info->polygonMode;
506 }
507 unreachable("Unsupported GS output topology");
508 } else if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL)) {
509 switch (get_tes_prog_data(pipeline)->output_topology) {
510 case BRW_TESS_OUTPUT_TOPOLOGY_POINT:
511 return VK_POLYGON_MODE_POINT;
512
513 case BRW_TESS_OUTPUT_TOPOLOGY_LINE:
514 return VK_POLYGON_MODE_LINE;
515
516 case BRW_TESS_OUTPUT_TOPOLOGY_TRI_CW:
517 case BRW_TESS_OUTPUT_TOPOLOGY_TRI_CCW:
518 return rs_info->polygonMode;
519 }
520 unreachable("Unsupported TCS output topology");
521 } else {
522 switch (ia_info->topology) {
523 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
524 return VK_POLYGON_MODE_POINT;
525
526 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
527 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
528 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
529 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
530 return VK_POLYGON_MODE_LINE;
531
532 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
533 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
534 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
535 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
536 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
537 return rs_info->polygonMode;
538
539 default:
540 unreachable("Unsupported primitive topology");
541 }
542 }
543 }
544
545 #if GEN_GEN <= 7
546 static uint32_t
547 gen7_ms_rast_mode(struct anv_graphics_pipeline *pipeline,
548 const VkPipelineInputAssemblyStateCreateInfo *ia_info,
549 const VkPipelineRasterizationStateCreateInfo *rs_info,
550 const VkPipelineMultisampleStateCreateInfo *ms_info)
551 {
552 const VkPipelineRasterizationLineStateCreateInfoEXT *line_info =
553 vk_find_struct_const(rs_info->pNext,
554 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
555
556 VkPolygonMode raster_mode =
557 anv_raster_polygon_mode(pipeline, ia_info, rs_info);
558 if (raster_mode == VK_POLYGON_MODE_LINE) {
559 switch (vk_line_rasterization_mode(line_info, ms_info)) {
560 case VK_LINE_RASTERIZATION_MODE_RECTANGULAR_EXT:
561 return MSRASTMODE_ON_PATTERN;
562
563 case VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT:
564 case VK_LINE_RASTERIZATION_MODE_RECTANGULAR_SMOOTH_EXT:
565 return MSRASTMODE_OFF_PIXEL;
566
567 default:
568 unreachable("Unsupported line rasterization mode");
569 }
570 } else {
571 return (ms_info && ms_info->rasterizationSamples > 1) ?
572 MSRASTMODE_ON_PATTERN : MSRASTMODE_OFF_PIXEL;
573 }
574 }
575 #endif
576
577 static void
578 emit_rs_state(struct anv_graphics_pipeline *pipeline,
579 const VkPipelineInputAssemblyStateCreateInfo *ia_info,
580 const VkPipelineRasterizationStateCreateInfo *rs_info,
581 const VkPipelineMultisampleStateCreateInfo *ms_info,
582 const VkPipelineRasterizationLineStateCreateInfoEXT *line_info,
583 const uint32_t dynamic_states,
584 const struct anv_render_pass *pass,
585 const struct anv_subpass *subpass,
586 enum gen_urb_deref_block_size urb_deref_block_size)
587 {
588 struct GENX(3DSTATE_SF) sf = {
589 GENX(3DSTATE_SF_header),
590 };
591
592 sf.ViewportTransformEnable = true;
593 sf.StatisticsEnable = true;
594 sf.TriangleStripListProvokingVertexSelect = 0;
595 sf.LineStripListProvokingVertexSelect = 0;
596 sf.TriangleFanProvokingVertexSelect = 1;
597 sf.VertexSubPixelPrecisionSelect = _8Bit;
598 sf.AALineDistanceMode = true;
599
600 #if GEN_IS_HASWELL
601 sf.LineStippleEnable = line_info && line_info->stippledLineEnable;
602 #endif
603
604 #if GEN_GEN >= 12
605 sf.DerefBlockSize = urb_deref_block_size;
606 #endif
607
608 const struct brw_vue_prog_data *last_vue_prog_data =
609 anv_pipeline_get_last_vue_prog_data(pipeline);
610
611 if (last_vue_prog_data->vue_map.slots_valid & VARYING_BIT_PSIZ) {
612 sf.PointWidthSource = Vertex;
613 } else {
614 sf.PointWidthSource = State;
615 sf.PointWidth = 1.0;
616 }
617
618 #if GEN_GEN >= 8
619 struct GENX(3DSTATE_RASTER) raster = {
620 GENX(3DSTATE_RASTER_header),
621 };
622 #else
623 # define raster sf
624 #endif
625
626 VkPolygonMode raster_mode =
627 anv_raster_polygon_mode(pipeline, ia_info, rs_info);
628 VkLineRasterizationModeEXT line_mode =
629 vk_line_rasterization_mode(line_info, ms_info);
630
631 /* For details on 3DSTATE_RASTER multisample state, see the BSpec table
632 * "Multisample Modes State".
633 */
634 #if GEN_GEN >= 8
635 if (raster_mode == VK_POLYGON_MODE_LINE) {
636 /* Unfortunately, configuring our line rasterization hardware on gen8
637 * and later is rather painful. Instead of giving us bits to tell the
638 * hardware what line mode to use like we had on gen7, we now have an
639 * arcane combination of API Mode and MSAA enable bits which do things
640 * in a table which are expected to magically put the hardware into the
641 * right mode for your API. Sadly, Vulkan isn't any of the APIs the
642 * hardware people thought of so nothing works the way you want it to.
643 *
644 * Look at the table titled "Multisample Rasterization Modes" in Vol 7
645 * of the Skylake PRM for more details.
646 */
647 switch (line_mode) {
648 case VK_LINE_RASTERIZATION_MODE_RECTANGULAR_EXT:
649 raster.APIMode = DX100;
650 raster.DXMultisampleRasterizationEnable = true;
651 break;
652
653 case VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT:
654 case VK_LINE_RASTERIZATION_MODE_RECTANGULAR_SMOOTH_EXT:
655 raster.APIMode = DX9OGL;
656 raster.DXMultisampleRasterizationEnable = false;
657 break;
658
659 default:
660 unreachable("Unsupported line rasterization mode");
661 }
662 } else {
663 raster.APIMode = DX100;
664 raster.DXMultisampleRasterizationEnable = true;
665 }
666
667 /* NOTE: 3DSTATE_RASTER::ForcedSampleCount affects the BDW and SKL PMA fix
668 * computations. If we ever set this bit to a different value, they will
669 * need to be updated accordingly.
670 */
671 raster.ForcedSampleCount = FSC_NUMRASTSAMPLES_0;
672 raster.ForceMultisampling = false;
673 #else
674 raster.MultisampleRasterizationMode =
675 gen7_ms_rast_mode(pipeline, ia_info, rs_info, ms_info);
676 #endif
677
678 if (raster_mode == VK_POLYGON_MODE_LINE &&
679 line_mode == VK_LINE_RASTERIZATION_MODE_RECTANGULAR_SMOOTH_EXT)
680 raster.AntialiasingEnable = true;
681
682 raster.FrontWinding =
683 dynamic_states & ANV_CMD_DIRTY_DYNAMIC_FRONT_FACE ?
684 0 : vk_to_gen_front_face[rs_info->frontFace];
685 raster.CullMode =
686 dynamic_states & ANV_CMD_DIRTY_DYNAMIC_CULL_MODE ?
687 0 : vk_to_gen_cullmode[rs_info->cullMode];
688
689 raster.FrontFaceFillMode = vk_to_gen_fillmode[rs_info->polygonMode];
690 raster.BackFaceFillMode = vk_to_gen_fillmode[rs_info->polygonMode];
691 raster.ScissorRectangleEnable = true;
692
693 #if GEN_GEN >= 9
694 /* GEN9+ splits ViewportZClipTestEnable into near and far enable bits */
695 raster.ViewportZFarClipTestEnable = pipeline->depth_clip_enable;
696 raster.ViewportZNearClipTestEnable = pipeline->depth_clip_enable;
697 #elif GEN_GEN >= 8
698 raster.ViewportZClipTestEnable = pipeline->depth_clip_enable;
699 #endif
700
701 raster.GlobalDepthOffsetEnableSolid = rs_info->depthBiasEnable;
702 raster.GlobalDepthOffsetEnableWireframe = rs_info->depthBiasEnable;
703 raster.GlobalDepthOffsetEnablePoint = rs_info->depthBiasEnable;
704
705 #if GEN_GEN == 7
706 /* Gen7 requires that we provide the depth format in 3DSTATE_SF so that it
707 * can get the depth offsets correct.
708 */
709 if (subpass->depth_stencil_attachment) {
710 VkFormat vk_format =
711 pass->attachments[subpass->depth_stencil_attachment->attachment].format;
712 assert(vk_format_is_depth_or_stencil(vk_format));
713 if (vk_format_aspects(vk_format) & VK_IMAGE_ASPECT_DEPTH_BIT) {
714 enum isl_format isl_format =
715 anv_get_isl_format(&pipeline->base.device->info, vk_format,
716 VK_IMAGE_ASPECT_DEPTH_BIT,
717 VK_IMAGE_TILING_OPTIMAL);
718 sf.DepthBufferSurfaceFormat =
719 isl_format_get_depth_format(isl_format, false);
720 }
721 }
722 #endif
723
724 #if GEN_GEN >= 8
725 GENX(3DSTATE_SF_pack)(NULL, pipeline->gen8.sf, &sf);
726 GENX(3DSTATE_RASTER_pack)(NULL, pipeline->gen8.raster, &raster);
727 #else
728 # undef raster
729 GENX(3DSTATE_SF_pack)(NULL, &pipeline->gen7.sf, &sf);
730 #endif
731 }
732
733 static void
734 emit_ms_state(struct anv_graphics_pipeline *pipeline,
735 const VkPipelineMultisampleStateCreateInfo *info)
736 {
737 uint32_t samples = 1;
738 uint32_t log2_samples = 0;
739
740 /* From the Vulkan 1.0 spec:
741 * If pSampleMask is NULL, it is treated as if the mask has all bits
742 * enabled, i.e. no coverage is removed from fragments.
743 *
744 * 3DSTATE_SAMPLE_MASK.SampleMask is 16 bits.
745 */
746 #if GEN_GEN >= 8
747 uint32_t sample_mask = 0xffff;
748 #else
749 uint32_t sample_mask = 0xff;
750 #endif
751
752 if (info) {
753 samples = info->rasterizationSamples;
754 log2_samples = __builtin_ffs(samples) - 1;
755 }
756
757 if (info && info->pSampleMask)
758 sample_mask &= info->pSampleMask[0];
759
760 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_MULTISAMPLE), ms) {
761 ms.NumberofMultisamples = log2_samples;
762
763 ms.PixelLocation = CENTER;
764 #if GEN_GEN >= 8
765 /* The PRM says that this bit is valid only for DX9:
766 *
767 * SW can choose to set this bit only for DX9 API. DX10/OGL API's
768 * should not have any effect by setting or not setting this bit.
769 */
770 ms.PixelPositionOffsetEnable = false;
771 #else
772
773 switch (samples) {
774 case 1:
775 GEN_SAMPLE_POS_1X(ms.Sample);
776 break;
777 case 2:
778 GEN_SAMPLE_POS_2X(ms.Sample);
779 break;
780 case 4:
781 GEN_SAMPLE_POS_4X(ms.Sample);
782 break;
783 case 8:
784 GEN_SAMPLE_POS_8X(ms.Sample);
785 break;
786 default:
787 break;
788 }
789 #endif
790 }
791
792 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_SAMPLE_MASK), sm) {
793 sm.SampleMask = sample_mask;
794 }
795 }
796
797 static const uint32_t vk_to_gen_logic_op[] = {
798 [VK_LOGIC_OP_COPY] = LOGICOP_COPY,
799 [VK_LOGIC_OP_CLEAR] = LOGICOP_CLEAR,
800 [VK_LOGIC_OP_AND] = LOGICOP_AND,
801 [VK_LOGIC_OP_AND_REVERSE] = LOGICOP_AND_REVERSE,
802 [VK_LOGIC_OP_AND_INVERTED] = LOGICOP_AND_INVERTED,
803 [VK_LOGIC_OP_NO_OP] = LOGICOP_NOOP,
804 [VK_LOGIC_OP_XOR] = LOGICOP_XOR,
805 [VK_LOGIC_OP_OR] = LOGICOP_OR,
806 [VK_LOGIC_OP_NOR] = LOGICOP_NOR,
807 [VK_LOGIC_OP_EQUIVALENT] = LOGICOP_EQUIV,
808 [VK_LOGIC_OP_INVERT] = LOGICOP_INVERT,
809 [VK_LOGIC_OP_OR_REVERSE] = LOGICOP_OR_REVERSE,
810 [VK_LOGIC_OP_COPY_INVERTED] = LOGICOP_COPY_INVERTED,
811 [VK_LOGIC_OP_OR_INVERTED] = LOGICOP_OR_INVERTED,
812 [VK_LOGIC_OP_NAND] = LOGICOP_NAND,
813 [VK_LOGIC_OP_SET] = LOGICOP_SET,
814 };
815
816 static const uint32_t vk_to_gen_blend[] = {
817 [VK_BLEND_FACTOR_ZERO] = BLENDFACTOR_ZERO,
818 [VK_BLEND_FACTOR_ONE] = BLENDFACTOR_ONE,
819 [VK_BLEND_FACTOR_SRC_COLOR] = BLENDFACTOR_SRC_COLOR,
820 [VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR] = BLENDFACTOR_INV_SRC_COLOR,
821 [VK_BLEND_FACTOR_DST_COLOR] = BLENDFACTOR_DST_COLOR,
822 [VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR] = BLENDFACTOR_INV_DST_COLOR,
823 [VK_BLEND_FACTOR_SRC_ALPHA] = BLENDFACTOR_SRC_ALPHA,
824 [VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA] = BLENDFACTOR_INV_SRC_ALPHA,
825 [VK_BLEND_FACTOR_DST_ALPHA] = BLENDFACTOR_DST_ALPHA,
826 [VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA] = BLENDFACTOR_INV_DST_ALPHA,
827 [VK_BLEND_FACTOR_CONSTANT_COLOR] = BLENDFACTOR_CONST_COLOR,
828 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR]= BLENDFACTOR_INV_CONST_COLOR,
829 [VK_BLEND_FACTOR_CONSTANT_ALPHA] = BLENDFACTOR_CONST_ALPHA,
830 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA]= BLENDFACTOR_INV_CONST_ALPHA,
831 [VK_BLEND_FACTOR_SRC_ALPHA_SATURATE] = BLENDFACTOR_SRC_ALPHA_SATURATE,
832 [VK_BLEND_FACTOR_SRC1_COLOR] = BLENDFACTOR_SRC1_COLOR,
833 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR] = BLENDFACTOR_INV_SRC1_COLOR,
834 [VK_BLEND_FACTOR_SRC1_ALPHA] = BLENDFACTOR_SRC1_ALPHA,
835 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA] = BLENDFACTOR_INV_SRC1_ALPHA,
836 };
837
838 static const uint32_t vk_to_gen_blend_op[] = {
839 [VK_BLEND_OP_ADD] = BLENDFUNCTION_ADD,
840 [VK_BLEND_OP_SUBTRACT] = BLENDFUNCTION_SUBTRACT,
841 [VK_BLEND_OP_REVERSE_SUBTRACT] = BLENDFUNCTION_REVERSE_SUBTRACT,
842 [VK_BLEND_OP_MIN] = BLENDFUNCTION_MIN,
843 [VK_BLEND_OP_MAX] = BLENDFUNCTION_MAX,
844 };
845
846 static const uint32_t vk_to_gen_compare_op[] = {
847 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
848 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
849 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
850 [VK_COMPARE_OP_LESS_OR_EQUAL] = PREFILTEROPLEQUAL,
851 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
852 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
853 [VK_COMPARE_OP_GREATER_OR_EQUAL] = PREFILTEROPGEQUAL,
854 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
855 };
856
857 static const uint32_t vk_to_gen_stencil_op[] = {
858 [VK_STENCIL_OP_KEEP] = STENCILOP_KEEP,
859 [VK_STENCIL_OP_ZERO] = STENCILOP_ZERO,
860 [VK_STENCIL_OP_REPLACE] = STENCILOP_REPLACE,
861 [VK_STENCIL_OP_INCREMENT_AND_CLAMP] = STENCILOP_INCRSAT,
862 [VK_STENCIL_OP_DECREMENT_AND_CLAMP] = STENCILOP_DECRSAT,
863 [VK_STENCIL_OP_INVERT] = STENCILOP_INVERT,
864 [VK_STENCIL_OP_INCREMENT_AND_WRAP] = STENCILOP_INCR,
865 [VK_STENCIL_OP_DECREMENT_AND_WRAP] = STENCILOP_DECR,
866 };
867
868 /* This function sanitizes the VkStencilOpState by looking at the compare ops
869 * and trying to determine whether or not a given stencil op can ever actually
870 * occur. Stencil ops which can never occur are set to VK_STENCIL_OP_KEEP.
871 * This function returns true if, after sanitation, any of the stencil ops are
872 * set to something other than VK_STENCIL_OP_KEEP.
873 */
874 static bool
875 sanitize_stencil_face(VkStencilOpState *face,
876 VkCompareOp depthCompareOp)
877 {
878 /* If compareOp is ALWAYS then the stencil test will never fail and failOp
879 * will never happen. Set failOp to KEEP in this case.
880 */
881 if (face->compareOp == VK_COMPARE_OP_ALWAYS)
882 face->failOp = VK_STENCIL_OP_KEEP;
883
884 /* If compareOp is NEVER or depthCompareOp is NEVER then one of the depth
885 * or stencil tests will fail and passOp will never happen.
886 */
887 if (face->compareOp == VK_COMPARE_OP_NEVER ||
888 depthCompareOp == VK_COMPARE_OP_NEVER)
889 face->passOp = VK_STENCIL_OP_KEEP;
890
891 /* If compareOp is NEVER or depthCompareOp is ALWAYS then either the
892 * stencil test will fail or the depth test will pass. In either case,
893 * depthFailOp will never happen.
894 */
895 if (face->compareOp == VK_COMPARE_OP_NEVER ||
896 depthCompareOp == VK_COMPARE_OP_ALWAYS)
897 face->depthFailOp = VK_STENCIL_OP_KEEP;
898
899 return face->failOp != VK_STENCIL_OP_KEEP ||
900 face->depthFailOp != VK_STENCIL_OP_KEEP ||
901 face->passOp != VK_STENCIL_OP_KEEP;
902 }
903
904 /* Intel hardware is fairly sensitive to whether or not depth/stencil writes
905 * are enabled. In the presence of discards, it's fairly easy to get into the
906 * non-promoted case which means a fairly big performance hit. From the Iron
907 * Lake PRM, Vol 2, pt. 1, section 8.4.3.2, "Early Depth Test Cases":
908 *
909 * "Non-promoted depth (N) is active whenever the depth test can be done
910 * early but it cannot determine whether or not to write source depth to
911 * the depth buffer, therefore the depth write must be performed post pixel
912 * shader. This includes cases where the pixel shader can kill pixels,
913 * including via sampler chroma key, as well as cases where the alpha test
914 * function is enabled, which kills pixels based on a programmable alpha
915 * test. In this case, even if the depth test fails, the pixel cannot be
916 * killed if a stencil write is indicated. Whether or not the stencil write
917 * happens depends on whether or not the pixel is killed later. In these
918 * cases if stencil test fails and stencil writes are off, the pixels can
919 * also be killed early. If stencil writes are enabled, the pixels must be
920 * treated as Computed depth (described above)."
921 *
922 * The same thing as mentioned in the stencil case can happen in the depth
923 * case as well if it thinks it writes depth but, thanks to the depth test
924 * being GL_EQUAL, the write doesn't actually matter. A little extra work
925 * up-front to try and disable depth and stencil writes can make a big
926 * difference.
927 *
928 * Unfortunately, the way depth and stencil testing is specified, there are
929 * many case where, regardless of depth/stencil writes being enabled, nothing
930 * actually gets written due to some other bit of state being set. This
931 * function attempts to "sanitize" the depth stencil state and disable writes
932 * and sometimes even testing whenever possible.
933 */
934 static void
935 sanitize_ds_state(VkPipelineDepthStencilStateCreateInfo *state,
936 bool *stencilWriteEnable,
937 VkImageAspectFlags ds_aspects)
938 {
939 *stencilWriteEnable = state->stencilTestEnable;
940
941 /* If the depth test is disabled, we won't be writing anything. Make sure we
942 * treat the test as always passing later on as well.
943 *
944 * Also, the Vulkan spec requires that if either depth or stencil is not
945 * present, the pipeline is to act as if the test silently passes. In that
946 * case we won't write either.
947 */
948 if (!state->depthTestEnable || !(ds_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
949 state->depthWriteEnable = false;
950 state->depthCompareOp = VK_COMPARE_OP_ALWAYS;
951 }
952
953 if (!(ds_aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
954 *stencilWriteEnable = false;
955 state->front.compareOp = VK_COMPARE_OP_ALWAYS;
956 state->back.compareOp = VK_COMPARE_OP_ALWAYS;
957 }
958
959 /* If the stencil test is enabled and always fails, then we will never get
960 * to the depth test so we can just disable the depth test entirely.
961 */
962 if (state->stencilTestEnable &&
963 state->front.compareOp == VK_COMPARE_OP_NEVER &&
964 state->back.compareOp == VK_COMPARE_OP_NEVER) {
965 state->depthTestEnable = false;
966 state->depthWriteEnable = false;
967 }
968
969 /* If depthCompareOp is EQUAL then the value we would be writing to the
970 * depth buffer is the same as the value that's already there so there's no
971 * point in writing it.
972 */
973 if (state->depthCompareOp == VK_COMPARE_OP_EQUAL)
974 state->depthWriteEnable = false;
975
976 /* If the stencil ops are such that we don't actually ever modify the
977 * stencil buffer, we should disable writes.
978 */
979 if (!sanitize_stencil_face(&state->front, state->depthCompareOp) &&
980 !sanitize_stencil_face(&state->back, state->depthCompareOp))
981 *stencilWriteEnable = false;
982
983 /* If the depth test always passes and we never write out depth, that's the
984 * same as if the depth test is disabled entirely.
985 */
986 if (state->depthCompareOp == VK_COMPARE_OP_ALWAYS &&
987 !state->depthWriteEnable)
988 state->depthTestEnable = false;
989
990 /* If the stencil test always passes and we never write out stencil, that's
991 * the same as if the stencil test is disabled entirely.
992 */
993 if (state->front.compareOp == VK_COMPARE_OP_ALWAYS &&
994 state->back.compareOp == VK_COMPARE_OP_ALWAYS &&
995 !*stencilWriteEnable)
996 state->stencilTestEnable = false;
997 }
998
999 static void
1000 emit_ds_state(struct anv_graphics_pipeline *pipeline,
1001 const VkPipelineDepthStencilStateCreateInfo *pCreateInfo,
1002 const uint32_t dynamic_states,
1003 const struct anv_render_pass *pass,
1004 const struct anv_subpass *subpass)
1005 {
1006 #if GEN_GEN == 7
1007 # define depth_stencil_dw pipeline->gen7.depth_stencil_state
1008 #elif GEN_GEN == 8
1009 # define depth_stencil_dw pipeline->gen8.wm_depth_stencil
1010 #else
1011 # define depth_stencil_dw pipeline->gen9.wm_depth_stencil
1012 #endif
1013
1014 if (pCreateInfo == NULL) {
1015 /* We're going to OR this together with the dynamic state. We need
1016 * to make sure it's initialized to something useful.
1017 */
1018 pipeline->writes_stencil = false;
1019 pipeline->stencil_test_enable = false;
1020 pipeline->writes_depth = false;
1021 pipeline->depth_test_enable = false;
1022 pipeline->depth_bounds_test_enable = false;
1023 memset(depth_stencil_dw, 0, sizeof(depth_stencil_dw));
1024 return;
1025 }
1026
1027 VkImageAspectFlags ds_aspects = 0;
1028 if (subpass->depth_stencil_attachment) {
1029 VkFormat depth_stencil_format =
1030 pass->attachments[subpass->depth_stencil_attachment->attachment].format;
1031 ds_aspects = vk_format_aspects(depth_stencil_format);
1032 }
1033
1034 VkPipelineDepthStencilStateCreateInfo info = *pCreateInfo;
1035 sanitize_ds_state(&info, &pipeline->writes_stencil, ds_aspects);
1036 pipeline->stencil_test_enable = info.stencilTestEnable;
1037 pipeline->writes_depth = info.depthWriteEnable;
1038 pipeline->depth_test_enable = info.depthTestEnable;
1039 pipeline->depth_bounds_test_enable = info.depthBoundsTestEnable;
1040
1041 bool dynamic_stencil_op =
1042 dynamic_states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_OP;
1043
1044 #if GEN_GEN <= 7
1045 struct GENX(DEPTH_STENCIL_STATE) depth_stencil = {
1046 #else
1047 struct GENX(3DSTATE_WM_DEPTH_STENCIL) depth_stencil = {
1048 #endif
1049 .DepthTestEnable =
1050 dynamic_states & ANV_CMD_DIRTY_DYNAMIC_DEPTH_TEST_ENABLE ?
1051 0 : info.depthTestEnable,
1052
1053 .DepthBufferWriteEnable =
1054 dynamic_states & ANV_CMD_DIRTY_DYNAMIC_DEPTH_WRITE_ENABLE ?
1055 0 : info.depthWriteEnable,
1056
1057 .DepthTestFunction =
1058 dynamic_states & ANV_CMD_DIRTY_DYNAMIC_DEPTH_COMPARE_OP ?
1059 0 : vk_to_gen_compare_op[info.depthCompareOp],
1060
1061 .DoubleSidedStencilEnable = true,
1062
1063 .StencilTestEnable =
1064 dynamic_states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_TEST_ENABLE ?
1065 0 : info.stencilTestEnable,
1066
1067 .StencilFailOp = vk_to_gen_stencil_op[info.front.failOp],
1068 .StencilPassDepthPassOp = vk_to_gen_stencil_op[info.front.passOp],
1069 .StencilPassDepthFailOp = vk_to_gen_stencil_op[info.front.depthFailOp],
1070 .StencilTestFunction = vk_to_gen_compare_op[info.front.compareOp],
1071 .BackfaceStencilFailOp = vk_to_gen_stencil_op[info.back.failOp],
1072 .BackfaceStencilPassDepthPassOp = vk_to_gen_stencil_op[info.back.passOp],
1073 .BackfaceStencilPassDepthFailOp =vk_to_gen_stencil_op[info.back.depthFailOp],
1074 .BackfaceStencilTestFunction = vk_to_gen_compare_op[info.back.compareOp],
1075 };
1076
1077 if (dynamic_stencil_op) {
1078 depth_stencil.StencilFailOp = 0;
1079 depth_stencil.StencilPassDepthPassOp = 0;
1080 depth_stencil.StencilPassDepthFailOp = 0;
1081 depth_stencil.StencilTestFunction = 0;
1082 depth_stencil.BackfaceStencilFailOp = 0;
1083 depth_stencil.BackfaceStencilPassDepthPassOp = 0;
1084 depth_stencil.BackfaceStencilPassDepthFailOp = 0;
1085 depth_stencil.BackfaceStencilTestFunction = 0;
1086 }
1087
1088 #if GEN_GEN <= 7
1089 GENX(DEPTH_STENCIL_STATE_pack)(NULL, depth_stencil_dw, &depth_stencil);
1090 #else
1091 GENX(3DSTATE_WM_DEPTH_STENCIL_pack)(NULL, depth_stencil_dw, &depth_stencil);
1092 #endif
1093 }
1094
1095 static bool
1096 is_dual_src_blend_factor(VkBlendFactor factor)
1097 {
1098 return factor == VK_BLEND_FACTOR_SRC1_COLOR ||
1099 factor == VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR ||
1100 factor == VK_BLEND_FACTOR_SRC1_ALPHA ||
1101 factor == VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA;
1102 }
1103
1104 static void
1105 emit_cb_state(struct anv_graphics_pipeline *pipeline,
1106 const VkPipelineColorBlendStateCreateInfo *info,
1107 const VkPipelineMultisampleStateCreateInfo *ms_info)
1108 {
1109 struct anv_device *device = pipeline->base.device;
1110 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1111
1112 struct GENX(BLEND_STATE) blend_state = {
1113 #if GEN_GEN >= 8
1114 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
1115 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
1116 #endif
1117 };
1118
1119 uint32_t surface_count = 0;
1120 struct anv_pipeline_bind_map *map;
1121 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1122 map = &pipeline->shaders[MESA_SHADER_FRAGMENT]->bind_map;
1123 surface_count = map->surface_count;
1124 }
1125
1126 const uint32_t num_dwords = GENX(BLEND_STATE_length) +
1127 GENX(BLEND_STATE_ENTRY_length) * surface_count;
1128 pipeline->blend_state =
1129 anv_state_pool_alloc(&device->dynamic_state_pool, num_dwords * 4, 64);
1130
1131 bool has_writeable_rt = false;
1132 uint32_t *state_pos = pipeline->blend_state.map;
1133 state_pos += GENX(BLEND_STATE_length);
1134 #if GEN_GEN >= 8
1135 struct GENX(BLEND_STATE_ENTRY) bs0 = { 0 };
1136 #endif
1137 for (unsigned i = 0; i < surface_count; i++) {
1138 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[i];
1139
1140 /* All color attachments are at the beginning of the binding table */
1141 if (binding->set != ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS)
1142 break;
1143
1144 /* We can have at most 8 attachments */
1145 assert(i < 8);
1146
1147 if (info == NULL || binding->index >= info->attachmentCount) {
1148 /* Default everything to disabled */
1149 struct GENX(BLEND_STATE_ENTRY) entry = {
1150 .WriteDisableAlpha = true,
1151 .WriteDisableRed = true,
1152 .WriteDisableGreen = true,
1153 .WriteDisableBlue = true,
1154 };
1155 GENX(BLEND_STATE_ENTRY_pack)(NULL, state_pos, &entry);
1156 state_pos += GENX(BLEND_STATE_ENTRY_length);
1157 continue;
1158 }
1159
1160 const VkPipelineColorBlendAttachmentState *a =
1161 &info->pAttachments[binding->index];
1162
1163 struct GENX(BLEND_STATE_ENTRY) entry = {
1164 #if GEN_GEN < 8
1165 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
1166 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
1167 #endif
1168 .LogicOpEnable = info->logicOpEnable,
1169 .LogicOpFunction = vk_to_gen_logic_op[info->logicOp],
1170 .ColorBufferBlendEnable = a->blendEnable,
1171 .ColorClampRange = COLORCLAMP_RTFORMAT,
1172 .PreBlendColorClampEnable = true,
1173 .PostBlendColorClampEnable = true,
1174 .SourceBlendFactor = vk_to_gen_blend[a->srcColorBlendFactor],
1175 .DestinationBlendFactor = vk_to_gen_blend[a->dstColorBlendFactor],
1176 .ColorBlendFunction = vk_to_gen_blend_op[a->colorBlendOp],
1177 .SourceAlphaBlendFactor = vk_to_gen_blend[a->srcAlphaBlendFactor],
1178 .DestinationAlphaBlendFactor = vk_to_gen_blend[a->dstAlphaBlendFactor],
1179 .AlphaBlendFunction = vk_to_gen_blend_op[a->alphaBlendOp],
1180 .WriteDisableAlpha = !(a->colorWriteMask & VK_COLOR_COMPONENT_A_BIT),
1181 .WriteDisableRed = !(a->colorWriteMask & VK_COLOR_COMPONENT_R_BIT),
1182 .WriteDisableGreen = !(a->colorWriteMask & VK_COLOR_COMPONENT_G_BIT),
1183 .WriteDisableBlue = !(a->colorWriteMask & VK_COLOR_COMPONENT_B_BIT),
1184 };
1185
1186 if (a->srcColorBlendFactor != a->srcAlphaBlendFactor ||
1187 a->dstColorBlendFactor != a->dstAlphaBlendFactor ||
1188 a->colorBlendOp != a->alphaBlendOp) {
1189 #if GEN_GEN >= 8
1190 blend_state.IndependentAlphaBlendEnable = true;
1191 #else
1192 entry.IndependentAlphaBlendEnable = true;
1193 #endif
1194 }
1195
1196 /* The Dual Source Blending documentation says:
1197 *
1198 * "If SRC1 is included in a src/dst blend factor and
1199 * a DualSource RT Write message is not used, results
1200 * are UNDEFINED. (This reflects the same restriction in DX APIs,
1201 * where undefined results are produced if “o1” is not written
1202 * by a PS – there are no default values defined)."
1203 *
1204 * There is no way to gracefully fix this undefined situation
1205 * so we just disable the blending to prevent possible issues.
1206 */
1207 if (!wm_prog_data->dual_src_blend &&
1208 (is_dual_src_blend_factor(a->srcColorBlendFactor) ||
1209 is_dual_src_blend_factor(a->dstColorBlendFactor) ||
1210 is_dual_src_blend_factor(a->srcAlphaBlendFactor) ||
1211 is_dual_src_blend_factor(a->dstAlphaBlendFactor))) {
1212 vk_debug_report(&device->physical->instance->debug_report_callbacks,
1213 VK_DEBUG_REPORT_WARNING_BIT_EXT,
1214 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
1215 (uint64_t)(uintptr_t)device,
1216 0, 0, "anv",
1217 "Enabled dual-src blend factors without writing both targets "
1218 "in the shader. Disabling blending to avoid GPU hangs.");
1219 entry.ColorBufferBlendEnable = false;
1220 }
1221
1222 if (a->colorWriteMask != 0)
1223 has_writeable_rt = true;
1224
1225 /* Our hardware applies the blend factor prior to the blend function
1226 * regardless of what function is used. Technically, this means the
1227 * hardware can do MORE than GL or Vulkan specify. However, it also
1228 * means that, for MIN and MAX, we have to stomp the blend factor to
1229 * ONE to make it a no-op.
1230 */
1231 if (a->colorBlendOp == VK_BLEND_OP_MIN ||
1232 a->colorBlendOp == VK_BLEND_OP_MAX) {
1233 entry.SourceBlendFactor = BLENDFACTOR_ONE;
1234 entry.DestinationBlendFactor = BLENDFACTOR_ONE;
1235 }
1236 if (a->alphaBlendOp == VK_BLEND_OP_MIN ||
1237 a->alphaBlendOp == VK_BLEND_OP_MAX) {
1238 entry.SourceAlphaBlendFactor = BLENDFACTOR_ONE;
1239 entry.DestinationAlphaBlendFactor = BLENDFACTOR_ONE;
1240 }
1241 GENX(BLEND_STATE_ENTRY_pack)(NULL, state_pos, &entry);
1242 state_pos += GENX(BLEND_STATE_ENTRY_length);
1243 #if GEN_GEN >= 8
1244 if (i == 0)
1245 bs0 = entry;
1246 #endif
1247 }
1248
1249 #if GEN_GEN >= 8
1250 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_PS_BLEND), blend) {
1251 blend.AlphaToCoverageEnable = blend_state.AlphaToCoverageEnable;
1252 blend.HasWriteableRT = has_writeable_rt;
1253 blend.ColorBufferBlendEnable = bs0.ColorBufferBlendEnable;
1254 blend.SourceAlphaBlendFactor = bs0.SourceAlphaBlendFactor;
1255 blend.DestinationAlphaBlendFactor = bs0.DestinationAlphaBlendFactor;
1256 blend.SourceBlendFactor = bs0.SourceBlendFactor;
1257 blend.DestinationBlendFactor = bs0.DestinationBlendFactor;
1258 blend.AlphaTestEnable = false;
1259 blend.IndependentAlphaBlendEnable =
1260 blend_state.IndependentAlphaBlendEnable;
1261 }
1262 #else
1263 (void)has_writeable_rt;
1264 #endif
1265
1266 GENX(BLEND_STATE_pack)(NULL, pipeline->blend_state.map, &blend_state);
1267
1268 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_BLEND_STATE_POINTERS), bsp) {
1269 bsp.BlendStatePointer = pipeline->blend_state.offset;
1270 #if GEN_GEN >= 8
1271 bsp.BlendStatePointerValid = true;
1272 #endif
1273 }
1274 }
1275
1276 static void
1277 emit_3dstate_clip(struct anv_graphics_pipeline *pipeline,
1278 const VkPipelineInputAssemblyStateCreateInfo *ia_info,
1279 const VkPipelineViewportStateCreateInfo *vp_info,
1280 const VkPipelineRasterizationStateCreateInfo *rs_info,
1281 const uint32_t dynamic_states)
1282 {
1283 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1284 (void) wm_prog_data;
1285
1286 struct GENX(3DSTATE_CLIP) clip = {
1287 GENX(3DSTATE_CLIP_header),
1288 };
1289
1290 clip.ClipEnable = true;
1291 clip.StatisticsEnable = true;
1292 clip.EarlyCullEnable = true;
1293 clip.APIMode = APIMODE_D3D;
1294 clip.GuardbandClipTestEnable = true;
1295
1296 /* Only enable the XY clip test when the final polygon rasterization
1297 * mode is VK_POLYGON_MODE_FILL. We want to leave it disabled for
1298 * points and lines so we get "pop-free" clipping.
1299 */
1300 VkPolygonMode raster_mode =
1301 anv_raster_polygon_mode(pipeline, ia_info, rs_info);
1302 clip.ViewportXYClipTestEnable = (raster_mode == VK_POLYGON_MODE_FILL);
1303
1304 #if GEN_GEN >= 8
1305 clip.VertexSubPixelPrecisionSelect = _8Bit;
1306 #endif
1307 clip.ClipMode = CLIPMODE_NORMAL;
1308
1309 clip.TriangleStripListProvokingVertexSelect = 0;
1310 clip.LineStripListProvokingVertexSelect = 0;
1311 clip.TriangleFanProvokingVertexSelect = 1;
1312
1313 clip.MinimumPointWidth = 0.125;
1314 clip.MaximumPointWidth = 255.875;
1315
1316 const struct brw_vue_prog_data *last =
1317 anv_pipeline_get_last_vue_prog_data(pipeline);
1318
1319 /* From the Vulkan 1.0.45 spec:
1320 *
1321 * "If the last active vertex processing stage shader entry point's
1322 * interface does not include a variable decorated with
1323 * ViewportIndex, then the first viewport is used."
1324 */
1325 if (vp_info && (last->vue_map.slots_valid & VARYING_BIT_VIEWPORT)) {
1326 clip.MaximumVPIndex = vp_info->viewportCount > 0 ?
1327 vp_info->viewportCount - 1 : 0;
1328 } else {
1329 clip.MaximumVPIndex = 0;
1330 }
1331
1332 /* From the Vulkan 1.0.45 spec:
1333 *
1334 * "If the last active vertex processing stage shader entry point's
1335 * interface does not include a variable decorated with Layer, then
1336 * the first layer is used."
1337 */
1338 clip.ForceZeroRTAIndexEnable =
1339 !(last->vue_map.slots_valid & VARYING_BIT_LAYER);
1340
1341 #if GEN_GEN == 7
1342 clip.FrontWinding = vk_to_gen_front_face[rs_info->frontFace];
1343 clip.CullMode = vk_to_gen_cullmode[rs_info->cullMode];
1344 clip.ViewportZClipTestEnable = pipeline->depth_clip_enable;
1345 clip.UserClipDistanceClipTestEnableBitmask = last->clip_distance_mask;
1346 clip.UserClipDistanceCullTestEnableBitmask = last->cull_distance_mask;
1347 #else
1348 clip.NonPerspectiveBarycentricEnable = wm_prog_data ?
1349 (wm_prog_data->barycentric_interp_modes &
1350 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS) != 0 : 0;
1351 #endif
1352
1353 GENX(3DSTATE_CLIP_pack)(NULL, pipeline->gen7.clip, &clip);
1354 }
1355
1356 static void
1357 emit_3dstate_streamout(struct anv_graphics_pipeline *pipeline,
1358 const VkPipelineRasterizationStateCreateInfo *rs_info)
1359 {
1360 #if GEN_GEN >= 8
1361 const struct brw_vue_prog_data *prog_data =
1362 anv_pipeline_get_last_vue_prog_data(pipeline);
1363 const struct brw_vue_map *vue_map = &prog_data->vue_map;
1364
1365 nir_xfb_info *xfb_info;
1366 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY))
1367 xfb_info = pipeline->shaders[MESA_SHADER_GEOMETRY]->xfb_info;
1368 else if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1369 xfb_info = pipeline->shaders[MESA_SHADER_TESS_EVAL]->xfb_info;
1370 else
1371 xfb_info = pipeline->shaders[MESA_SHADER_VERTEX]->xfb_info;
1372 #endif
1373
1374 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_STREAMOUT), so) {
1375 so.RenderingDisable = rs_info->rasterizerDiscardEnable;
1376
1377 #if GEN_GEN >= 8
1378 if (xfb_info) {
1379 so.SOFunctionEnable = true;
1380 so.SOStatisticsEnable = true;
1381
1382 const VkPipelineRasterizationStateStreamCreateInfoEXT *stream_info =
1383 vk_find_struct_const(rs_info, PIPELINE_RASTERIZATION_STATE_STREAM_CREATE_INFO_EXT);
1384 so.RenderStreamSelect = stream_info ?
1385 stream_info->rasterizationStream : 0;
1386
1387 so.Buffer0SurfacePitch = xfb_info->buffers[0].stride;
1388 so.Buffer1SurfacePitch = xfb_info->buffers[1].stride;
1389 so.Buffer2SurfacePitch = xfb_info->buffers[2].stride;
1390 so.Buffer3SurfacePitch = xfb_info->buffers[3].stride;
1391
1392 int urb_entry_read_offset = 0;
1393 int urb_entry_read_length =
1394 (prog_data->vue_map.num_slots + 1) / 2 - urb_entry_read_offset;
1395
1396 /* We always read the whole vertex. This could be reduced at some
1397 * point by reading less and offsetting the register index in the
1398 * SO_DECLs.
1399 */
1400 so.Stream0VertexReadOffset = urb_entry_read_offset;
1401 so.Stream0VertexReadLength = urb_entry_read_length - 1;
1402 so.Stream1VertexReadOffset = urb_entry_read_offset;
1403 so.Stream1VertexReadLength = urb_entry_read_length - 1;
1404 so.Stream2VertexReadOffset = urb_entry_read_offset;
1405 so.Stream2VertexReadLength = urb_entry_read_length - 1;
1406 so.Stream3VertexReadOffset = urb_entry_read_offset;
1407 so.Stream3VertexReadLength = urb_entry_read_length - 1;
1408 }
1409 #endif /* GEN_GEN >= 8 */
1410 }
1411
1412 #if GEN_GEN >= 8
1413 if (xfb_info) {
1414 struct GENX(SO_DECL) so_decl[MAX_XFB_STREAMS][128];
1415 int next_offset[MAX_XFB_BUFFERS] = {0, 0, 0, 0};
1416 int decls[MAX_XFB_STREAMS] = {0, 0, 0, 0};
1417
1418 memset(so_decl, 0, sizeof(so_decl));
1419
1420 for (unsigned i = 0; i < xfb_info->output_count; i++) {
1421 const nir_xfb_output_info *output = &xfb_info->outputs[i];
1422 unsigned buffer = output->buffer;
1423 unsigned stream = xfb_info->buffer_to_stream[buffer];
1424
1425 /* Our hardware is unusual in that it requires us to program SO_DECLs
1426 * for fake "hole" components, rather than simply taking the offset
1427 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
1428 * program as many size = 4 holes as we can, then a final hole to
1429 * accommodate the final 1, 2, or 3 remaining.
1430 */
1431 int hole_dwords = (output->offset - next_offset[buffer]) / 4;
1432 while (hole_dwords > 0) {
1433 so_decl[stream][decls[stream]++] = (struct GENX(SO_DECL)) {
1434 .HoleFlag = 1,
1435 .OutputBufferSlot = buffer,
1436 .ComponentMask = (1 << MIN2(hole_dwords, 4)) - 1,
1437 };
1438 hole_dwords -= 4;
1439 }
1440
1441 int varying = output->location;
1442 uint8_t component_mask = output->component_mask;
1443 /* VARYING_SLOT_PSIZ contains three scalar fields packed together:
1444 * - VARYING_SLOT_LAYER in VARYING_SLOT_PSIZ.y
1445 * - VARYING_SLOT_VIEWPORT in VARYING_SLOT_PSIZ.z
1446 * - VARYING_SLOT_PSIZ in VARYING_SLOT_PSIZ.w
1447 */
1448 if (varying == VARYING_SLOT_LAYER) {
1449 varying = VARYING_SLOT_PSIZ;
1450 component_mask = 1 << 1; // SO_DECL_COMPMASK_Y
1451 } else if (varying == VARYING_SLOT_VIEWPORT) {
1452 varying = VARYING_SLOT_PSIZ;
1453 component_mask = 1 << 2; // SO_DECL_COMPMASK_Z
1454 } else if (varying == VARYING_SLOT_PSIZ) {
1455 component_mask = 1 << 3; // SO_DECL_COMPMASK_W
1456 }
1457
1458 next_offset[buffer] = output->offset +
1459 __builtin_popcount(component_mask) * 4;
1460
1461 const int slot = vue_map->varying_to_slot[varying];
1462 if (slot < 0) {
1463 /* This can happen if the shader never writes to the varying.
1464 * Insert a hole instead of actual varying data.
1465 */
1466 so_decl[stream][decls[stream]++] = (struct GENX(SO_DECL)) {
1467 .HoleFlag = true,
1468 .OutputBufferSlot = buffer,
1469 .ComponentMask = component_mask,
1470 };
1471 } else {
1472 so_decl[stream][decls[stream]++] = (struct GENX(SO_DECL)) {
1473 .OutputBufferSlot = buffer,
1474 .RegisterIndex = slot,
1475 .ComponentMask = component_mask,
1476 };
1477 }
1478 }
1479
1480 int max_decls = 0;
1481 for (unsigned s = 0; s < MAX_XFB_STREAMS; s++)
1482 max_decls = MAX2(max_decls, decls[s]);
1483
1484 uint8_t sbs[MAX_XFB_STREAMS] = { };
1485 for (unsigned b = 0; b < MAX_XFB_BUFFERS; b++) {
1486 if (xfb_info->buffers_written & (1 << b))
1487 sbs[xfb_info->buffer_to_stream[b]] |= 1 << b;
1488 }
1489
1490 uint32_t *dw = anv_batch_emitn(&pipeline->base.batch, 3 + 2 * max_decls,
1491 GENX(3DSTATE_SO_DECL_LIST),
1492 .StreamtoBufferSelects0 = sbs[0],
1493 .StreamtoBufferSelects1 = sbs[1],
1494 .StreamtoBufferSelects2 = sbs[2],
1495 .StreamtoBufferSelects3 = sbs[3],
1496 .NumEntries0 = decls[0],
1497 .NumEntries1 = decls[1],
1498 .NumEntries2 = decls[2],
1499 .NumEntries3 = decls[3]);
1500
1501 for (int i = 0; i < max_decls; i++) {
1502 GENX(SO_DECL_ENTRY_pack)(NULL, dw + 3 + i * 2,
1503 &(struct GENX(SO_DECL_ENTRY)) {
1504 .Stream0Decl = so_decl[0][i],
1505 .Stream1Decl = so_decl[1][i],
1506 .Stream2Decl = so_decl[2][i],
1507 .Stream3Decl = so_decl[3][i],
1508 });
1509 }
1510 }
1511 #endif /* GEN_GEN >= 8 */
1512 }
1513
1514 static uint32_t
1515 get_sampler_count(const struct anv_shader_bin *bin)
1516 {
1517 uint32_t count_by_4 = DIV_ROUND_UP(bin->bind_map.sampler_count, 4);
1518
1519 /* We can potentially have way more than 32 samplers and that's ok.
1520 * However, the 3DSTATE_XS packets only have 3 bits to specify how
1521 * many to pre-fetch and all values above 4 are marked reserved.
1522 */
1523 return MIN2(count_by_4, 4);
1524 }
1525
1526 static uint32_t
1527 get_binding_table_entry_count(const struct anv_shader_bin *bin)
1528 {
1529 return DIV_ROUND_UP(bin->bind_map.surface_count, 32);
1530 }
1531
1532 static struct anv_address
1533 get_scratch_address(struct anv_pipeline *pipeline,
1534 gl_shader_stage stage,
1535 const struct anv_shader_bin *bin)
1536 {
1537 return (struct anv_address) {
1538 .bo = anv_scratch_pool_alloc(pipeline->device,
1539 &pipeline->device->scratch_pool,
1540 stage, bin->prog_data->total_scratch),
1541 .offset = 0,
1542 };
1543 }
1544
1545 static uint32_t
1546 get_scratch_space(const struct anv_shader_bin *bin)
1547 {
1548 return ffs(bin->prog_data->total_scratch / 2048);
1549 }
1550
1551 static void
1552 emit_3dstate_vs(struct anv_graphics_pipeline *pipeline)
1553 {
1554 const struct gen_device_info *devinfo = &pipeline->base.device->info;
1555 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1556 const struct anv_shader_bin *vs_bin =
1557 pipeline->shaders[MESA_SHADER_VERTEX];
1558
1559 assert(anv_pipeline_has_stage(pipeline, MESA_SHADER_VERTEX));
1560
1561 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_VS), vs) {
1562 vs.Enable = true;
1563 vs.StatisticsEnable = true;
1564 vs.KernelStartPointer = vs_bin->kernel.offset;
1565 #if GEN_GEN >= 8
1566 vs.SIMD8DispatchEnable =
1567 vs_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8;
1568 #endif
1569
1570 assert(!vs_prog_data->base.base.use_alt_mode);
1571 #if GEN_GEN < 11
1572 vs.SingleVertexDispatch = false;
1573 #endif
1574 vs.VectorMaskEnable = false;
1575 /* WA_1606682166:
1576 * Incorrect TDL's SSP address shift in SARB for 16:6 & 18:8 modes.
1577 * Disable the Sampler state prefetch functionality in the SARB by
1578 * programming 0xB000[30] to '1'.
1579 */
1580 vs.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(vs_bin);
1581 vs.BindingTableEntryCount = get_binding_table_entry_count(vs_bin);
1582 vs.FloatingPointMode = IEEE754;
1583 vs.IllegalOpcodeExceptionEnable = false;
1584 vs.SoftwareExceptionEnable = false;
1585 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
1586
1587 if (GEN_GEN == 9 && devinfo->gt == 4 &&
1588 anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL)) {
1589 /* On Sky Lake GT4, we have experienced some hangs related to the VS
1590 * cache and tessellation. It is unknown exactly what is happening
1591 * but the Haswell docs for the "VS Reference Count Full Force Miss
1592 * Enable" field of the "Thread Mode" register refer to a HSW bug in
1593 * which the VUE handle reference count would overflow resulting in
1594 * internal reference counting bugs. My (Jason's) best guess is that
1595 * this bug cropped back up on SKL GT4 when we suddenly had more
1596 * threads in play than any previous gen9 hardware.
1597 *
1598 * What we do know for sure is that setting this bit when
1599 * tessellation shaders are in use fixes a GPU hang in Batman: Arkham
1600 * City when playing with DXVK (https://bugs.freedesktop.org/107280).
1601 * Disabling the vertex cache with tessellation shaders should only
1602 * have a minor performance impact as the tessellation shaders are
1603 * likely generating and processing far more geometry than the vertex
1604 * stage.
1605 */
1606 vs.VertexCacheDisable = true;
1607 }
1608
1609 vs.VertexURBEntryReadLength = vs_prog_data->base.urb_read_length;
1610 vs.VertexURBEntryReadOffset = 0;
1611 vs.DispatchGRFStartRegisterForURBData =
1612 vs_prog_data->base.base.dispatch_grf_start_reg;
1613
1614 #if GEN_GEN >= 8
1615 vs.UserClipDistanceClipTestEnableBitmask =
1616 vs_prog_data->base.clip_distance_mask;
1617 vs.UserClipDistanceCullTestEnableBitmask =
1618 vs_prog_data->base.cull_distance_mask;
1619 #endif
1620
1621 vs.PerThreadScratchSpace = get_scratch_space(vs_bin);
1622 vs.ScratchSpaceBasePointer =
1623 get_scratch_address(&pipeline->base, MESA_SHADER_VERTEX, vs_bin);
1624 }
1625 }
1626
1627 static void
1628 emit_3dstate_hs_te_ds(struct anv_graphics_pipeline *pipeline,
1629 const VkPipelineTessellationStateCreateInfo *tess_info)
1630 {
1631 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL)) {
1632 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_HS), hs);
1633 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_TE), te);
1634 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_DS), ds);
1635 return;
1636 }
1637
1638 const struct gen_device_info *devinfo = &pipeline->base.device->info;
1639 const struct anv_shader_bin *tcs_bin =
1640 pipeline->shaders[MESA_SHADER_TESS_CTRL];
1641 const struct anv_shader_bin *tes_bin =
1642 pipeline->shaders[MESA_SHADER_TESS_EVAL];
1643
1644 const struct brw_tcs_prog_data *tcs_prog_data = get_tcs_prog_data(pipeline);
1645 const struct brw_tes_prog_data *tes_prog_data = get_tes_prog_data(pipeline);
1646
1647 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_HS), hs) {
1648 hs.Enable = true;
1649 hs.StatisticsEnable = true;
1650 hs.KernelStartPointer = tcs_bin->kernel.offset;
1651 /* WA_1606682166 */
1652 hs.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(tcs_bin);
1653 hs.BindingTableEntryCount = get_binding_table_entry_count(tcs_bin);
1654
1655 #if GEN_GEN >= 12
1656 /* GEN:BUG:1604578095:
1657 *
1658 * Hang occurs when the number of max threads is less than 2 times
1659 * the number of instance count. The number of max threads must be
1660 * more than 2 times the number of instance count.
1661 */
1662 assert((devinfo->max_tcs_threads / 2) > tcs_prog_data->instances);
1663 #endif
1664
1665 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
1666 hs.IncludeVertexHandles = true;
1667 hs.InstanceCount = tcs_prog_data->instances - 1;
1668
1669 hs.VertexURBEntryReadLength = 0;
1670 hs.VertexURBEntryReadOffset = 0;
1671 hs.DispatchGRFStartRegisterForURBData =
1672 tcs_prog_data->base.base.dispatch_grf_start_reg & 0x1f;
1673 #if GEN_GEN >= 12
1674 hs.DispatchGRFStartRegisterForURBData5 =
1675 tcs_prog_data->base.base.dispatch_grf_start_reg >> 5;
1676 #endif
1677
1678
1679 hs.PerThreadScratchSpace = get_scratch_space(tcs_bin);
1680 hs.ScratchSpaceBasePointer =
1681 get_scratch_address(&pipeline->base, MESA_SHADER_TESS_CTRL, tcs_bin);
1682
1683 #if GEN_GEN == 12
1684 /* Patch Count threshold specifies the maximum number of patches that
1685 * will be accumulated before a thread dispatch is forced.
1686 */
1687 hs.PatchCountThreshold = tcs_prog_data->patch_count_threshold;
1688 #endif
1689
1690 #if GEN_GEN >= 9
1691 hs.DispatchMode = tcs_prog_data->base.dispatch_mode;
1692 hs.IncludePrimitiveID = tcs_prog_data->include_primitive_id;
1693 #endif
1694 }
1695
1696 const VkPipelineTessellationDomainOriginStateCreateInfo *domain_origin_state =
1697 tess_info ? vk_find_struct_const(tess_info, PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO) : NULL;
1698
1699 VkTessellationDomainOrigin uv_origin =
1700 domain_origin_state ? domain_origin_state->domainOrigin :
1701 VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT;
1702
1703 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_TE), te) {
1704 te.Partitioning = tes_prog_data->partitioning;
1705
1706 if (uv_origin == VK_TESSELLATION_DOMAIN_ORIGIN_LOWER_LEFT) {
1707 te.OutputTopology = tes_prog_data->output_topology;
1708 } else {
1709 /* When the origin is upper-left, we have to flip the winding order */
1710 if (tes_prog_data->output_topology == OUTPUT_TRI_CCW) {
1711 te.OutputTopology = OUTPUT_TRI_CW;
1712 } else if (tes_prog_data->output_topology == OUTPUT_TRI_CW) {
1713 te.OutputTopology = OUTPUT_TRI_CCW;
1714 } else {
1715 te.OutputTopology = tes_prog_data->output_topology;
1716 }
1717 }
1718
1719 te.TEDomain = tes_prog_data->domain;
1720 te.TEEnable = true;
1721 te.MaximumTessellationFactorOdd = 63.0;
1722 te.MaximumTessellationFactorNotOdd = 64.0;
1723 }
1724
1725 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_DS), ds) {
1726 ds.Enable = true;
1727 ds.StatisticsEnable = true;
1728 ds.KernelStartPointer = tes_bin->kernel.offset;
1729 /* WA_1606682166 */
1730 ds.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(tes_bin);
1731 ds.BindingTableEntryCount = get_binding_table_entry_count(tes_bin);
1732 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
1733
1734 ds.ComputeWCoordinateEnable =
1735 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
1736
1737 ds.PatchURBEntryReadLength = tes_prog_data->base.urb_read_length;
1738 ds.PatchURBEntryReadOffset = 0;
1739 ds.DispatchGRFStartRegisterForURBData =
1740 tes_prog_data->base.base.dispatch_grf_start_reg;
1741
1742 #if GEN_GEN >= 8
1743 #if GEN_GEN < 11
1744 ds.DispatchMode =
1745 tes_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8 ?
1746 DISPATCH_MODE_SIMD8_SINGLE_PATCH :
1747 DISPATCH_MODE_SIMD4X2;
1748 #else
1749 assert(tes_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8);
1750 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
1751 #endif
1752
1753 ds.UserClipDistanceClipTestEnableBitmask =
1754 tes_prog_data->base.clip_distance_mask;
1755 ds.UserClipDistanceCullTestEnableBitmask =
1756 tes_prog_data->base.cull_distance_mask;
1757 #endif
1758
1759 ds.PerThreadScratchSpace = get_scratch_space(tes_bin);
1760 ds.ScratchSpaceBasePointer =
1761 get_scratch_address(&pipeline->base, MESA_SHADER_TESS_EVAL, tes_bin);
1762 }
1763 }
1764
1765 static void
1766 emit_3dstate_gs(struct anv_graphics_pipeline *pipeline)
1767 {
1768 const struct gen_device_info *devinfo = &pipeline->base.device->info;
1769 const struct anv_shader_bin *gs_bin =
1770 pipeline->shaders[MESA_SHADER_GEOMETRY];
1771
1772 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY)) {
1773 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_GS), gs);
1774 return;
1775 }
1776
1777 const struct brw_gs_prog_data *gs_prog_data = get_gs_prog_data(pipeline);
1778
1779 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_GS), gs) {
1780 gs.Enable = true;
1781 gs.StatisticsEnable = true;
1782 gs.KernelStartPointer = gs_bin->kernel.offset;
1783 gs.DispatchMode = gs_prog_data->base.dispatch_mode;
1784
1785 gs.SingleProgramFlow = false;
1786 gs.VectorMaskEnable = false;
1787 /* WA_1606682166 */
1788 gs.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(gs_bin);
1789 gs.BindingTableEntryCount = get_binding_table_entry_count(gs_bin);
1790 gs.IncludeVertexHandles = gs_prog_data->base.include_vue_handles;
1791 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
1792
1793 if (GEN_GEN == 8) {
1794 /* Broadwell is weird. It needs us to divide by 2. */
1795 gs.MaximumNumberofThreads = devinfo->max_gs_threads / 2 - 1;
1796 } else {
1797 gs.MaximumNumberofThreads = devinfo->max_gs_threads - 1;
1798 }
1799
1800 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
1801 gs.OutputTopology = gs_prog_data->output_topology;
1802 gs.VertexURBEntryReadLength = gs_prog_data->base.urb_read_length;
1803 gs.ControlDataFormat = gs_prog_data->control_data_format;
1804 gs.ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords;
1805 gs.InstanceControl = MAX2(gs_prog_data->invocations, 1) - 1;
1806 gs.ReorderMode = TRAILING;
1807
1808 #if GEN_GEN >= 8
1809 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
1810 gs.StaticOutput = gs_prog_data->static_vertex_count >= 0;
1811 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count >= 0 ?
1812 gs_prog_data->static_vertex_count : 0;
1813 #endif
1814
1815 gs.VertexURBEntryReadOffset = 0;
1816 gs.VertexURBEntryReadLength = gs_prog_data->base.urb_read_length;
1817 gs.DispatchGRFStartRegisterForURBData =
1818 gs_prog_data->base.base.dispatch_grf_start_reg;
1819
1820 #if GEN_GEN >= 8
1821 gs.UserClipDistanceClipTestEnableBitmask =
1822 gs_prog_data->base.clip_distance_mask;
1823 gs.UserClipDistanceCullTestEnableBitmask =
1824 gs_prog_data->base.cull_distance_mask;
1825 #endif
1826
1827 gs.PerThreadScratchSpace = get_scratch_space(gs_bin);
1828 gs.ScratchSpaceBasePointer =
1829 get_scratch_address(&pipeline->base, MESA_SHADER_GEOMETRY, gs_bin);
1830 }
1831 }
1832
1833 static bool
1834 has_color_buffer_write_enabled(const struct anv_graphics_pipeline *pipeline,
1835 const VkPipelineColorBlendStateCreateInfo *blend)
1836 {
1837 const struct anv_shader_bin *shader_bin =
1838 pipeline->shaders[MESA_SHADER_FRAGMENT];
1839 if (!shader_bin)
1840 return false;
1841
1842 const struct anv_pipeline_bind_map *bind_map = &shader_bin->bind_map;
1843 for (int i = 0; i < bind_map->surface_count; i++) {
1844 struct anv_pipeline_binding *binding = &bind_map->surface_to_descriptor[i];
1845
1846 if (binding->set != ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS)
1847 continue;
1848
1849 if (binding->index == UINT32_MAX)
1850 continue;
1851
1852 if (blend && blend->pAttachments[binding->index].colorWriteMask != 0)
1853 return true;
1854 }
1855
1856 return false;
1857 }
1858
1859 static void
1860 emit_3dstate_wm(struct anv_graphics_pipeline *pipeline, struct anv_subpass *subpass,
1861 const VkPipelineInputAssemblyStateCreateInfo *ia,
1862 const VkPipelineRasterizationStateCreateInfo *raster,
1863 const VkPipelineColorBlendStateCreateInfo *blend,
1864 const VkPipelineMultisampleStateCreateInfo *multisample,
1865 const VkPipelineRasterizationLineStateCreateInfoEXT *line)
1866 {
1867 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1868
1869 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_WM), wm) {
1870 wm.StatisticsEnable = true;
1871 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
1872 wm.LineAntialiasingRegionWidth = _10pixels;
1873 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1874
1875 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1876 if (wm_prog_data->early_fragment_tests) {
1877 wm.EarlyDepthStencilControl = EDSC_PREPS;
1878 } else if (wm_prog_data->has_side_effects) {
1879 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
1880 } else {
1881 wm.EarlyDepthStencilControl = EDSC_NORMAL;
1882 }
1883
1884 #if GEN_GEN >= 8
1885 /* Gen8 hardware tries to compute ThreadDispatchEnable for us but
1886 * doesn't take into account KillPixels when no depth or stencil
1887 * writes are enabled. In order for occlusion queries to work
1888 * correctly with no attachments, we need to force-enable PS thread
1889 * dispatch.
1890 *
1891 * The BDW docs are pretty clear that that this bit isn't validated
1892 * and probably shouldn't be used in production:
1893 *
1894 * "This must always be set to Normal. This field should not be
1895 * tested for functional validation."
1896 *
1897 * Unfortunately, however, the other mechanism we have for doing this
1898 * is 3DSTATE_PS_EXTRA::PixelShaderHasUAV which causes hangs on BDW.
1899 * Given two bad options, we choose the one which works.
1900 */
1901 if ((wm_prog_data->has_side_effects || wm_prog_data->uses_kill) &&
1902 !has_color_buffer_write_enabled(pipeline, blend))
1903 wm.ForceThreadDispatchEnable = ForceON;
1904 #endif
1905
1906 wm.BarycentricInterpolationMode =
1907 wm_prog_data->barycentric_interp_modes;
1908
1909 #if GEN_GEN < 8
1910 wm.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
1911 wm.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
1912 wm.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
1913 wm.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
1914
1915 /* If the subpass has a depth or stencil self-dependency, then we
1916 * need to force the hardware to do the depth/stencil write *after*
1917 * fragment shader execution. Otherwise, the writes may hit memory
1918 * before we get around to fetching from the input attachment and we
1919 * may get the depth or stencil value from the current draw rather
1920 * than the previous one.
1921 */
1922 wm.PixelShaderKillsPixel = subpass->has_ds_self_dep ||
1923 wm_prog_data->uses_kill;
1924
1925 if (wm.PixelShaderComputedDepthMode != PSCDEPTH_OFF ||
1926 wm_prog_data->has_side_effects ||
1927 wm.PixelShaderKillsPixel ||
1928 has_color_buffer_write_enabled(pipeline, blend))
1929 wm.ThreadDispatchEnable = true;
1930
1931 if (multisample && multisample->rasterizationSamples > 1) {
1932 if (wm_prog_data->persample_dispatch) {
1933 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1934 } else {
1935 wm.MultisampleDispatchMode = MSDISPMODE_PERPIXEL;
1936 }
1937 } else {
1938 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1939 }
1940 wm.MultisampleRasterizationMode =
1941 gen7_ms_rast_mode(pipeline, ia, raster, multisample);
1942 #endif
1943
1944 wm.LineStippleEnable = line && line->stippledLineEnable;
1945 }
1946 }
1947 }
1948
1949 static void
1950 emit_3dstate_ps(struct anv_graphics_pipeline *pipeline,
1951 const VkPipelineColorBlendStateCreateInfo *blend,
1952 const VkPipelineMultisampleStateCreateInfo *multisample)
1953 {
1954 UNUSED const struct gen_device_info *devinfo = &pipeline->base.device->info;
1955 const struct anv_shader_bin *fs_bin =
1956 pipeline->shaders[MESA_SHADER_FRAGMENT];
1957
1958 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1959 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_PS), ps) {
1960 #if GEN_GEN == 7
1961 /* Even if no fragments are ever dispatched, gen7 hardware hangs if
1962 * we don't at least set the maximum number of threads.
1963 */
1964 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
1965 #endif
1966 }
1967 return;
1968 }
1969
1970 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1971
1972 #if GEN_GEN < 8
1973 /* The hardware wedges if you have this bit set but don't turn on any dual
1974 * source blend factors.
1975 */
1976 bool dual_src_blend = false;
1977 if (wm_prog_data->dual_src_blend && blend) {
1978 for (uint32_t i = 0; i < blend->attachmentCount; i++) {
1979 const VkPipelineColorBlendAttachmentState *bstate =
1980 &blend->pAttachments[i];
1981
1982 if (bstate->blendEnable &&
1983 (is_dual_src_blend_factor(bstate->srcColorBlendFactor) ||
1984 is_dual_src_blend_factor(bstate->dstColorBlendFactor) ||
1985 is_dual_src_blend_factor(bstate->srcAlphaBlendFactor) ||
1986 is_dual_src_blend_factor(bstate->dstAlphaBlendFactor))) {
1987 dual_src_blend = true;
1988 break;
1989 }
1990 }
1991 }
1992 #endif
1993
1994 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_PS), ps) {
1995 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
1996 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
1997 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
1998
1999 /* From the Sky Lake PRM 3DSTATE_PS::32 Pixel Dispatch Enable:
2000 *
2001 * "When NUM_MULTISAMPLES = 16 or FORCE_SAMPLE_COUNT = 16, SIMD32
2002 * Dispatch must not be enabled for PER_PIXEL dispatch mode."
2003 *
2004 * Since 16x MSAA is first introduced on SKL, we don't need to apply
2005 * the workaround on any older hardware.
2006 */
2007 if (GEN_GEN >= 9 && !wm_prog_data->persample_dispatch &&
2008 multisample && multisample->rasterizationSamples == 16) {
2009 assert(ps._8PixelDispatchEnable || ps._16PixelDispatchEnable);
2010 ps._32PixelDispatchEnable = false;
2011 }
2012
2013 ps.KernelStartPointer0 = fs_bin->kernel.offset +
2014 brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
2015 ps.KernelStartPointer1 = fs_bin->kernel.offset +
2016 brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
2017 ps.KernelStartPointer2 = fs_bin->kernel.offset +
2018 brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
2019
2020 ps.SingleProgramFlow = false;
2021 ps.VectorMaskEnable = GEN_GEN >= 8;
2022 /* WA_1606682166 */
2023 ps.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(fs_bin);
2024 ps.BindingTableEntryCount = get_binding_table_entry_count(fs_bin);
2025 ps.PushConstantEnable = wm_prog_data->base.nr_params > 0 ||
2026 wm_prog_data->base.ubo_ranges[0].length;
2027 ps.PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
2028 POSOFFSET_SAMPLE: POSOFFSET_NONE;
2029 #if GEN_GEN < 8
2030 ps.AttributeEnable = wm_prog_data->num_varying_inputs > 0;
2031 ps.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
2032 ps.DualSourceBlendEnable = dual_src_blend;
2033 #endif
2034
2035 #if GEN_IS_HASWELL
2036 /* Haswell requires the sample mask to be set in this packet as well
2037 * as in 3DSTATE_SAMPLE_MASK; the values should match.
2038 */
2039 ps.SampleMask = 0xff;
2040 #endif
2041
2042 #if GEN_GEN >= 9
2043 ps.MaximumNumberofThreadsPerPSD = 64 - 1;
2044 #elif GEN_GEN >= 8
2045 ps.MaximumNumberofThreadsPerPSD = 64 - 2;
2046 #else
2047 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
2048 #endif
2049
2050 ps.DispatchGRFStartRegisterForConstantSetupData0 =
2051 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
2052 ps.DispatchGRFStartRegisterForConstantSetupData1 =
2053 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
2054 ps.DispatchGRFStartRegisterForConstantSetupData2 =
2055 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
2056
2057 ps.PerThreadScratchSpace = get_scratch_space(fs_bin);
2058 ps.ScratchSpaceBasePointer =
2059 get_scratch_address(&pipeline->base, MESA_SHADER_FRAGMENT, fs_bin);
2060 }
2061 }
2062
2063 #if GEN_GEN >= 8
2064 static void
2065 emit_3dstate_ps_extra(struct anv_graphics_pipeline *pipeline,
2066 struct anv_subpass *subpass)
2067 {
2068 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
2069
2070 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
2071 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_PS_EXTRA), ps);
2072 return;
2073 }
2074
2075 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_PS_EXTRA), ps) {
2076 ps.PixelShaderValid = true;
2077 ps.AttributeEnable = wm_prog_data->num_varying_inputs > 0;
2078 ps.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
2079 ps.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
2080 ps.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
2081 ps.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
2082 ps.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
2083
2084 /* If the subpass has a depth or stencil self-dependency, then we need
2085 * to force the hardware to do the depth/stencil write *after* fragment
2086 * shader execution. Otherwise, the writes may hit memory before we get
2087 * around to fetching from the input attachment and we may get the depth
2088 * or stencil value from the current draw rather than the previous one.
2089 */
2090 ps.PixelShaderKillsPixel = subpass->has_ds_self_dep ||
2091 wm_prog_data->uses_kill;
2092
2093 #if GEN_GEN >= 9
2094 ps.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
2095 ps.PixelShaderPullsBary = wm_prog_data->pulls_bary;
2096
2097 ps.InputCoverageMaskState = ICMS_NONE;
2098 if (wm_prog_data->uses_sample_mask) {
2099 if (wm_prog_data->post_depth_coverage)
2100 ps.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
2101 else
2102 ps.InputCoverageMaskState = ICMS_INNER_CONSERVATIVE;
2103 }
2104 #else
2105 ps.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
2106 #endif
2107 }
2108 }
2109
2110 static void
2111 emit_3dstate_vf_topology(struct anv_graphics_pipeline *pipeline)
2112 {
2113 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_VF_TOPOLOGY), vft) {
2114 vft.PrimitiveTopologyType = pipeline->topology;
2115 }
2116 }
2117 #endif
2118
2119 static void
2120 emit_3dstate_vf_statistics(struct anv_graphics_pipeline *pipeline)
2121 {
2122 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_VF_STATISTICS), vfs) {
2123 vfs.StatisticsEnable = true;
2124 }
2125 }
2126
2127 static void
2128 compute_kill_pixel(struct anv_graphics_pipeline *pipeline,
2129 const VkPipelineMultisampleStateCreateInfo *ms_info,
2130 const struct anv_subpass *subpass)
2131 {
2132 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
2133 pipeline->kill_pixel = false;
2134 return;
2135 }
2136
2137 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
2138
2139 /* This computes the KillPixel portion of the computation for whether or
2140 * not we want to enable the PMA fix on gen8 or gen9. It's given by this
2141 * chunk of the giant formula:
2142 *
2143 * (3DSTATE_PS_EXTRA::PixelShaderKillsPixels ||
2144 * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||
2145 * 3DSTATE_PS_BLEND::AlphaToCoverageEnable ||
2146 * 3DSTATE_PS_BLEND::AlphaTestEnable ||
2147 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable)
2148 *
2149 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable is always false and so is
2150 * 3DSTATE_PS_BLEND::AlphaTestEnable since Vulkan doesn't have a concept
2151 * of an alpha test.
2152 */
2153 pipeline->kill_pixel =
2154 subpass->has_ds_self_dep || wm_prog_data->uses_kill ||
2155 wm_prog_data->uses_omask ||
2156 (ms_info && ms_info->alphaToCoverageEnable);
2157 }
2158
2159 #if GEN_GEN == 12
2160 static void
2161 emit_3dstate_primitive_replication(struct anv_graphics_pipeline *pipeline)
2162 {
2163 if (!pipeline->use_primitive_replication) {
2164 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_PRIMITIVE_REPLICATION), pr);
2165 return;
2166 }
2167
2168 uint32_t view_mask = pipeline->subpass->view_mask;
2169 int view_count = util_bitcount(view_mask);
2170 assert(view_count > 1 && view_count <= MAX_VIEWS_FOR_PRIMITIVE_REPLICATION);
2171
2172 anv_batch_emit(&pipeline->base.batch, GENX(3DSTATE_PRIMITIVE_REPLICATION), pr) {
2173 pr.ReplicaMask = (1 << view_count) - 1;
2174 pr.ReplicationCount = view_count - 1;
2175
2176 int i = 0, view_index;
2177 for_each_bit(view_index, view_mask) {
2178 pr.RTAIOffset[i] = view_index;
2179 i++;
2180 }
2181 }
2182 }
2183 #endif
2184
2185 static VkResult
2186 genX(graphics_pipeline_create)(
2187 VkDevice _device,
2188 struct anv_pipeline_cache * cache,
2189 const VkGraphicsPipelineCreateInfo* pCreateInfo,
2190 const VkAllocationCallbacks* pAllocator,
2191 VkPipeline* pPipeline)
2192 {
2193 ANV_FROM_HANDLE(anv_device, device, _device);
2194 ANV_FROM_HANDLE(anv_render_pass, pass, pCreateInfo->renderPass);
2195 struct anv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
2196 struct anv_graphics_pipeline *pipeline;
2197 VkResult result;
2198
2199 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
2200
2201 /* Use the default pipeline cache if none is specified */
2202 if (cache == NULL && device->physical->instance->pipeline_cache_enabled)
2203 cache = &device->default_pipeline_cache;
2204
2205 pipeline = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
2206 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2207 if (pipeline == NULL)
2208 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2209
2210 result = anv_graphics_pipeline_init(pipeline, device, cache,
2211 pCreateInfo, pAllocator);
2212 if (result != VK_SUCCESS) {
2213 vk_free2(&device->vk.alloc, pAllocator, pipeline);
2214 if (result == VK_PIPELINE_COMPILE_REQUIRED_EXT)
2215 *pPipeline = VK_NULL_HANDLE;
2216 return result;
2217 }
2218
2219 /* If rasterization is not enabled, various CreateInfo structs must be
2220 * ignored.
2221 */
2222 const bool raster_enabled =
2223 !pCreateInfo->pRasterizationState->rasterizerDiscardEnable;
2224
2225 const VkPipelineViewportStateCreateInfo *vp_info =
2226 raster_enabled ? pCreateInfo->pViewportState : NULL;
2227
2228 const VkPipelineMultisampleStateCreateInfo *ms_info =
2229 raster_enabled ? pCreateInfo->pMultisampleState : NULL;
2230
2231 const VkPipelineDepthStencilStateCreateInfo *ds_info =
2232 raster_enabled ? pCreateInfo->pDepthStencilState : NULL;
2233
2234 const VkPipelineColorBlendStateCreateInfo *cb_info =
2235 raster_enabled ? pCreateInfo->pColorBlendState : NULL;
2236
2237 const VkPipelineRasterizationLineStateCreateInfoEXT *line_info =
2238 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
2239 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
2240
2241 /* Information on which states are considered dynamic. */
2242 const VkPipelineDynamicStateCreateInfo *dyn_info =
2243 pCreateInfo->pDynamicState;
2244 uint32_t dynamic_states = 0;
2245 if (dyn_info) {
2246 for (unsigned i = 0; i < dyn_info->dynamicStateCount; i++)
2247 dynamic_states |=
2248 anv_cmd_dirty_bit_for_vk_dynamic_state(dyn_info->pDynamicStates[i]);
2249 }
2250
2251 enum gen_urb_deref_block_size urb_deref_block_size;
2252 emit_urb_setup(pipeline, &urb_deref_block_size);
2253
2254 assert(pCreateInfo->pVertexInputState);
2255 emit_vertex_input(pipeline, pCreateInfo->pVertexInputState);
2256 assert(pCreateInfo->pRasterizationState);
2257 emit_rs_state(pipeline, pCreateInfo->pInputAssemblyState,
2258 pCreateInfo->pRasterizationState,
2259 ms_info, line_info, dynamic_states, pass, subpass,
2260 urb_deref_block_size);
2261 emit_ms_state(pipeline, ms_info);
2262 emit_ds_state(pipeline, ds_info, dynamic_states, pass, subpass);
2263 emit_cb_state(pipeline, cb_info, ms_info);
2264 compute_kill_pixel(pipeline, ms_info, subpass);
2265
2266 emit_3dstate_clip(pipeline,
2267 pCreateInfo->pInputAssemblyState,
2268 vp_info,
2269 pCreateInfo->pRasterizationState,
2270 dynamic_states);
2271 emit_3dstate_streamout(pipeline, pCreateInfo->pRasterizationState);
2272
2273 #if GEN_GEN == 12
2274 emit_3dstate_primitive_replication(pipeline);
2275 #endif
2276
2277 #if 0
2278 /* From gen7_vs_state.c */
2279
2280 /**
2281 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
2282 * Geometry > Geometry Shader > State:
2283 *
2284 * "Note: Because of corruption in IVB:GT2, software needs to flush the
2285 * whole fixed function pipeline when the GS enable changes value in
2286 * the 3DSTATE_GS."
2287 *
2288 * The hardware architects have clarified that in this context "flush the
2289 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
2290 * Stall" bit set.
2291 */
2292 if (!device->info.is_haswell && !device->info.is_baytrail)
2293 gen7_emit_vs_workaround_flush(brw);
2294 #endif
2295
2296 emit_3dstate_vs(pipeline);
2297 emit_3dstate_hs_te_ds(pipeline, pCreateInfo->pTessellationState);
2298 emit_3dstate_gs(pipeline);
2299 emit_3dstate_sbe(pipeline);
2300 emit_3dstate_wm(pipeline, subpass,
2301 pCreateInfo->pInputAssemblyState,
2302 pCreateInfo->pRasterizationState,
2303 cb_info, ms_info, line_info);
2304 emit_3dstate_ps(pipeline, cb_info, ms_info);
2305 #if GEN_GEN >= 8
2306 emit_3dstate_ps_extra(pipeline, subpass);
2307
2308 if (!(dynamic_states & ANV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY))
2309 emit_3dstate_vf_topology(pipeline);
2310 #endif
2311 emit_3dstate_vf_statistics(pipeline);
2312
2313 *pPipeline = anv_pipeline_to_handle(&pipeline->base);
2314
2315 return pipeline->base.batch.status;
2316 }
2317
2318 static void
2319 emit_media_cs_state(struct anv_compute_pipeline *pipeline,
2320 const struct anv_device *device)
2321 {
2322 const struct brw_cs_prog_data *cs_prog_data = get_cs_prog_data(pipeline);
2323
2324 anv_pipeline_setup_l3_config(&pipeline->base, cs_prog_data->base.total_shared > 0);
2325
2326 const struct anv_cs_parameters cs_params = anv_cs_parameters(pipeline);
2327
2328 pipeline->cs_right_mask = brw_cs_right_mask(cs_params.group_size, cs_params.simd_size);
2329
2330 const uint32_t vfe_curbe_allocation =
2331 ALIGN(cs_prog_data->push.per_thread.regs * cs_params.threads +
2332 cs_prog_data->push.cross_thread.regs, 2);
2333
2334 const uint32_t subslices = MAX2(device->physical->subslice_total, 1);
2335
2336 const struct anv_shader_bin *cs_bin = pipeline->cs;
2337 const struct gen_device_info *devinfo = &device->info;
2338
2339 anv_batch_emit(&pipeline->base.batch, GENX(MEDIA_VFE_STATE), vfe) {
2340 #if GEN_GEN > 7
2341 vfe.StackSize = 0;
2342 #else
2343 vfe.GPGPUMode = true;
2344 #endif
2345 vfe.MaximumNumberofThreads =
2346 devinfo->max_cs_threads * subslices - 1;
2347 vfe.NumberofURBEntries = GEN_GEN <= 7 ? 0 : 2;
2348 #if GEN_GEN < 11
2349 vfe.ResetGatewayTimer = true;
2350 #endif
2351 #if GEN_GEN <= 8
2352 vfe.BypassGatewayControl = true;
2353 #endif
2354 vfe.URBEntryAllocationSize = GEN_GEN <= 7 ? 0 : 2;
2355 vfe.CURBEAllocationSize = vfe_curbe_allocation;
2356
2357 if (cs_bin->prog_data->total_scratch) {
2358 if (GEN_GEN >= 8) {
2359 /* Broadwell's Per Thread Scratch Space is in the range [0, 11]
2360 * where 0 = 1k, 1 = 2k, 2 = 4k, ..., 11 = 2M.
2361 */
2362 vfe.PerThreadScratchSpace =
2363 ffs(cs_bin->prog_data->total_scratch) - 11;
2364 } else if (GEN_IS_HASWELL) {
2365 /* Haswell's Per Thread Scratch Space is in the range [0, 10]
2366 * where 0 = 2k, 1 = 4k, 2 = 8k, ..., 10 = 2M.
2367 */
2368 vfe.PerThreadScratchSpace =
2369 ffs(cs_bin->prog_data->total_scratch) - 12;
2370 } else {
2371 /* IVB and BYT use the range [0, 11] to mean [1kB, 12kB]
2372 * where 0 = 1kB, 1 = 2kB, 2 = 3kB, ..., 11 = 12kB.
2373 */
2374 vfe.PerThreadScratchSpace =
2375 cs_bin->prog_data->total_scratch / 1024 - 1;
2376 }
2377 vfe.ScratchSpaceBasePointer =
2378 get_scratch_address(&pipeline->base, MESA_SHADER_COMPUTE, cs_bin);
2379 }
2380 }
2381
2382 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
2383 .KernelStartPointer =
2384 cs_bin->kernel.offset +
2385 brw_cs_prog_data_prog_offset(cs_prog_data, cs_params.simd_size),
2386
2387 /* WA_1606682166 */
2388 .SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(cs_bin),
2389 /* We add 1 because the CS indirect parameters buffer isn't accounted
2390 * for in bind_map.surface_count.
2391 */
2392 .BindingTableEntryCount = 1 + MIN2(cs_bin->bind_map.surface_count, 30),
2393 .BarrierEnable = cs_prog_data->uses_barrier,
2394 .SharedLocalMemorySize =
2395 encode_slm_size(GEN_GEN, cs_prog_data->base.total_shared),
2396
2397 #if !GEN_IS_HASWELL
2398 .ConstantURBEntryReadOffset = 0,
2399 #endif
2400 .ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs,
2401 #if GEN_GEN >= 8 || GEN_IS_HASWELL
2402 .CrossThreadConstantDataReadLength =
2403 cs_prog_data->push.cross_thread.regs,
2404 #endif
2405 #if GEN_GEN >= 12
2406 /* TODO: Check if we are missing workarounds and enable mid-thread
2407 * preemption.
2408 *
2409 * We still have issues with mid-thread preemption (it was already
2410 * disabled by the kernel on gen11, due to missing workarounds). It's
2411 * possible that we are just missing some workarounds, and could enable
2412 * it later, but for now let's disable it to fix a GPU in compute in Car
2413 * Chase (and possibly more).
2414 */
2415 .ThreadPreemptionDisable = true,
2416 #endif
2417
2418 .NumberofThreadsinGPGPUThreadGroup = cs_params.threads,
2419 };
2420 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL,
2421 pipeline->interface_descriptor_data,
2422 &desc);
2423 }
2424
2425 static VkResult
2426 compute_pipeline_create(
2427 VkDevice _device,
2428 struct anv_pipeline_cache * cache,
2429 const VkComputePipelineCreateInfo* pCreateInfo,
2430 const VkAllocationCallbacks* pAllocator,
2431 VkPipeline* pPipeline)
2432 {
2433 ANV_FROM_HANDLE(anv_device, device, _device);
2434 struct anv_compute_pipeline *pipeline;
2435 VkResult result;
2436
2437 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO);
2438
2439 /* Use the default pipeline cache if none is specified */
2440 if (cache == NULL && device->physical->instance->pipeline_cache_enabled)
2441 cache = &device->default_pipeline_cache;
2442
2443 pipeline = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
2444 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2445 if (pipeline == NULL)
2446 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2447
2448 result = anv_pipeline_init(&pipeline->base, device,
2449 ANV_PIPELINE_COMPUTE, pCreateInfo->flags,
2450 pAllocator);
2451 if (result != VK_SUCCESS) {
2452 vk_free2(&device->vk.alloc, pAllocator, pipeline);
2453 return result;
2454 }
2455
2456 anv_batch_set_storage(&pipeline->base.batch, ANV_NULL_ADDRESS,
2457 pipeline->batch_data, sizeof(pipeline->batch_data));
2458
2459 pipeline->cs = NULL;
2460
2461 assert(pCreateInfo->stage.stage == VK_SHADER_STAGE_COMPUTE_BIT);
2462 ANV_FROM_HANDLE(anv_shader_module, module, pCreateInfo->stage.module);
2463 result = anv_pipeline_compile_cs(pipeline, cache, pCreateInfo, module,
2464 pCreateInfo->stage.pName,
2465 pCreateInfo->stage.pSpecializationInfo);
2466 if (result != VK_SUCCESS) {
2467 anv_pipeline_finish(&pipeline->base, device, pAllocator);
2468 vk_free2(&device->vk.alloc, pAllocator, pipeline);
2469 if (result == VK_PIPELINE_COMPILE_REQUIRED_EXT)
2470 *pPipeline = VK_NULL_HANDLE;
2471 return result;
2472 }
2473
2474 emit_media_cs_state(pipeline, device);
2475
2476 *pPipeline = anv_pipeline_to_handle(&pipeline->base);
2477
2478 return pipeline->base.batch.status;
2479 }
2480
2481 VkResult genX(CreateGraphicsPipelines)(
2482 VkDevice _device,
2483 VkPipelineCache pipelineCache,
2484 uint32_t count,
2485 const VkGraphicsPipelineCreateInfo* pCreateInfos,
2486 const VkAllocationCallbacks* pAllocator,
2487 VkPipeline* pPipelines)
2488 {
2489 ANV_FROM_HANDLE(anv_pipeline_cache, pipeline_cache, pipelineCache);
2490
2491 VkResult result = VK_SUCCESS;
2492
2493 unsigned i;
2494 for (i = 0; i < count; i++) {
2495 VkResult res = genX(graphics_pipeline_create)(_device,
2496 pipeline_cache,
2497 &pCreateInfos[i],
2498 pAllocator, &pPipelines[i]);
2499
2500 if (res == VK_SUCCESS)
2501 continue;
2502
2503 /* Bail out on the first error != VK_PIPELINE_COMPILE_REQUIRED_EX as it
2504 * is not obvious what error should be report upon 2 different failures.
2505 * */
2506 result = res;
2507 if (res != VK_PIPELINE_COMPILE_REQUIRED_EXT)
2508 break;
2509
2510 if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)
2511 break;
2512 }
2513
2514 for (; i < count; i++)
2515 pPipelines[i] = VK_NULL_HANDLE;
2516
2517 return result;
2518 }
2519
2520 VkResult genX(CreateComputePipelines)(
2521 VkDevice _device,
2522 VkPipelineCache pipelineCache,
2523 uint32_t count,
2524 const VkComputePipelineCreateInfo* pCreateInfos,
2525 const VkAllocationCallbacks* pAllocator,
2526 VkPipeline* pPipelines)
2527 {
2528 ANV_FROM_HANDLE(anv_pipeline_cache, pipeline_cache, pipelineCache);
2529
2530 VkResult result = VK_SUCCESS;
2531
2532 unsigned i;
2533 for (i = 0; i < count; i++) {
2534 VkResult res = compute_pipeline_create(_device, pipeline_cache,
2535 &pCreateInfos[i],
2536 pAllocator, &pPipelines[i]);
2537
2538 if (res == VK_SUCCESS)
2539 continue;
2540
2541 /* Bail out on the first error != VK_PIPELINE_COMPILE_REQUIRED_EX as it
2542 * is not obvious what error should be report upon 2 different failures.
2543 * */
2544 result = res;
2545 if (res != VK_PIPELINE_COMPILE_REQUIRED_EXT)
2546 break;
2547
2548 if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)
2549 break;
2550 }
2551
2552 for (; i < count; i++)
2553 pPipelines[i] = VK_NULL_HANDLE;
2554
2555 return result;
2556 }