anv: Always emit at least one vertex element
[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 "vk_util.h"
32 #include "vk_format_info.h"
33
34 static uint32_t
35 vertex_element_comp_control(enum isl_format format, unsigned comp)
36 {
37 uint8_t bits;
38 switch (comp) {
39 case 0: bits = isl_format_layouts[format].channels.r.bits; break;
40 case 1: bits = isl_format_layouts[format].channels.g.bits; break;
41 case 2: bits = isl_format_layouts[format].channels.b.bits; break;
42 case 3: bits = isl_format_layouts[format].channels.a.bits; break;
43 default: unreachable("Invalid component");
44 }
45
46 /*
47 * Take in account hardware restrictions when dealing with 64-bit floats.
48 *
49 * From Broadwell spec, command reference structures, page 586:
50 * "When SourceElementFormat is set to one of the *64*_PASSTHRU formats,
51 * 64-bit components are stored * in the URB without any conversion. In
52 * this case, vertex elements must be written as 128 or 256 bits, with
53 * VFCOMP_STORE_0 being used to pad the output as required. E.g., if
54 * R64_PASSTHRU is used to copy a 64-bit Red component into the URB,
55 * Component 1 must be specified as VFCOMP_STORE_0 (with Components 2,3
56 * set to VFCOMP_NOSTORE) in order to output a 128-bit vertex element, or
57 * Components 1-3 must be specified as VFCOMP_STORE_0 in order to output
58 * a 256-bit vertex element. Likewise, use of R64G64B64_PASSTHRU requires
59 * Component 3 to be specified as VFCOMP_STORE_0 in order to output a
60 * 256-bit vertex element."
61 */
62 if (bits) {
63 return VFCOMP_STORE_SRC;
64 } else if (comp >= 2 &&
65 !isl_format_layouts[format].channels.b.bits &&
66 isl_format_layouts[format].channels.r.type == ISL_RAW) {
67 /* When emitting 64-bit attributes, we need to write either 128 or 256
68 * bit chunks, using VFCOMP_NOSTORE when not writing the chunk, and
69 * VFCOMP_STORE_0 to pad the written chunk */
70 return VFCOMP_NOSTORE;
71 } else if (comp < 3 ||
72 isl_format_layouts[format].channels.r.type == ISL_RAW) {
73 /* Note we need to pad with value 0, not 1, due hardware restrictions
74 * (see comment above) */
75 return VFCOMP_STORE_0;
76 } else if (isl_format_layouts[format].channels.r.type == ISL_UINT ||
77 isl_format_layouts[format].channels.r.type == ISL_SINT) {
78 assert(comp == 3);
79 return VFCOMP_STORE_1_INT;
80 } else {
81 assert(comp == 3);
82 return VFCOMP_STORE_1_FP;
83 }
84 }
85
86 static void
87 emit_vertex_input(struct anv_pipeline *pipeline,
88 const VkPipelineVertexInputStateCreateInfo *info)
89 {
90 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
91
92 /* Pull inputs_read out of the VS prog data */
93 const uint64_t inputs_read = vs_prog_data->inputs_read;
94 const uint64_t double_inputs_read =
95 vs_prog_data->double_inputs_read & inputs_read;
96 assert((inputs_read & ((1 << VERT_ATTRIB_GENERIC0) - 1)) == 0);
97 const uint32_t elements = inputs_read >> VERT_ATTRIB_GENERIC0;
98 const uint32_t elements_double = double_inputs_read >> VERT_ATTRIB_GENERIC0;
99 const bool needs_svgs_elem = vs_prog_data->uses_vertexid ||
100 vs_prog_data->uses_instanceid ||
101 vs_prog_data->uses_firstvertex ||
102 vs_prog_data->uses_baseinstance;
103
104 uint32_t elem_count = __builtin_popcount(elements) -
105 __builtin_popcount(elements_double) / 2;
106
107 const uint32_t total_elems =
108 MAX2(1, elem_count + needs_svgs_elem + vs_prog_data->uses_drawid);
109
110 uint32_t *p;
111
112 const uint32_t num_dwords = 1 + total_elems * 2;
113 p = anv_batch_emitn(&pipeline->batch, num_dwords,
114 GENX(3DSTATE_VERTEX_ELEMENTS));
115 if (!p)
116 return;
117
118 for (uint32_t i = 0; i < total_elems; i++) {
119 /* The SKL docs for VERTEX_ELEMENT_STATE say:
120 *
121 * "All elements must be valid from Element[0] to the last valid
122 * element. (I.e. if Element[2] is valid then Element[1] and
123 * Element[0] must also be valid)."
124 *
125 * The SKL docs for 3D_Vertex_Component_Control say:
126 *
127 * "Don't store this component. (Not valid for Component 0, but can
128 * be used for Component 1-3)."
129 *
130 * So we can't just leave a vertex element blank and hope for the best.
131 * We have to tell the VF hardware to put something in it; so we just
132 * store a bunch of zero.
133 *
134 * TODO: Compact vertex elements so we never end up with holes.
135 */
136 struct GENX(VERTEX_ELEMENT_STATE) element = {
137 .Valid = true,
138 .Component0Control = VFCOMP_STORE_0,
139 .Component1Control = VFCOMP_STORE_0,
140 .Component2Control = VFCOMP_STORE_0,
141 .Component3Control = VFCOMP_STORE_0,
142 };
143 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + i * 2], &element);
144 }
145
146 for (uint32_t i = 0; i < info->vertexAttributeDescriptionCount; i++) {
147 const VkVertexInputAttributeDescription *desc =
148 &info->pVertexAttributeDescriptions[i];
149 enum isl_format format = anv_get_isl_format(&pipeline->device->info,
150 desc->format,
151 VK_IMAGE_ASPECT_COLOR_BIT,
152 VK_IMAGE_TILING_LINEAR);
153
154 assert(desc->binding < MAX_VBS);
155
156 if ((elements & (1 << desc->location)) == 0)
157 continue; /* Binding unused */
158
159 uint32_t slot =
160 __builtin_popcount(elements & ((1 << desc->location) - 1)) -
161 DIV_ROUND_UP(__builtin_popcount(elements_double &
162 ((1 << desc->location) -1)), 2);
163
164 struct GENX(VERTEX_ELEMENT_STATE) element = {
165 .VertexBufferIndex = desc->binding,
166 .Valid = true,
167 .SourceElementFormat = format,
168 .EdgeFlagEnable = false,
169 .SourceElementOffset = desc->offset,
170 .Component0Control = vertex_element_comp_control(format, 0),
171 .Component1Control = vertex_element_comp_control(format, 1),
172 .Component2Control = vertex_element_comp_control(format, 2),
173 .Component3Control = vertex_element_comp_control(format, 3),
174 };
175 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + slot * 2], &element);
176
177 #if GEN_GEN >= 8
178 /* On Broadwell and later, we have a separate VF_INSTANCING packet
179 * that controls instancing. On Haswell and prior, that's part of
180 * VERTEX_BUFFER_STATE which we emit later.
181 */
182 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_INSTANCING), vfi) {
183 vfi.InstancingEnable = pipeline->vb[desc->binding].instanced;
184 vfi.VertexElementIndex = slot;
185 vfi.InstanceDataStepRate =
186 pipeline->vb[desc->binding].instance_divisor;
187 }
188 #endif
189 }
190
191 const uint32_t id_slot = elem_count;
192 if (needs_svgs_elem) {
193 /* From the Broadwell PRM for the 3D_Vertex_Component_Control enum:
194 * "Within a VERTEX_ELEMENT_STATE structure, if a Component
195 * Control field is set to something other than VFCOMP_STORE_SRC,
196 * no higher-numbered Component Control fields may be set to
197 * VFCOMP_STORE_SRC"
198 *
199 * This means, that if we have BaseInstance, we need BaseVertex as
200 * well. Just do all or nothing.
201 */
202 uint32_t base_ctrl = (vs_prog_data->uses_firstvertex ||
203 vs_prog_data->uses_baseinstance) ?
204 VFCOMP_STORE_SRC : VFCOMP_STORE_0;
205
206 struct GENX(VERTEX_ELEMENT_STATE) element = {
207 .VertexBufferIndex = ANV_SVGS_VB_INDEX,
208 .Valid = true,
209 .SourceElementFormat = ISL_FORMAT_R32G32_UINT,
210 .Component0Control = base_ctrl,
211 .Component1Control = base_ctrl,
212 #if GEN_GEN >= 8
213 .Component2Control = VFCOMP_STORE_0,
214 .Component3Control = VFCOMP_STORE_0,
215 #else
216 .Component2Control = VFCOMP_STORE_VID,
217 .Component3Control = VFCOMP_STORE_IID,
218 #endif
219 };
220 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + id_slot * 2], &element);
221 }
222
223 #if GEN_GEN >= 8
224 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_SGVS), sgvs) {
225 sgvs.VertexIDEnable = vs_prog_data->uses_vertexid;
226 sgvs.VertexIDComponentNumber = 2;
227 sgvs.VertexIDElementOffset = id_slot;
228 sgvs.InstanceIDEnable = vs_prog_data->uses_instanceid;
229 sgvs.InstanceIDComponentNumber = 3;
230 sgvs.InstanceIDElementOffset = id_slot;
231 }
232 #endif
233
234 const uint32_t drawid_slot = elem_count + needs_svgs_elem;
235 if (vs_prog_data->uses_drawid) {
236 struct GENX(VERTEX_ELEMENT_STATE) element = {
237 .VertexBufferIndex = ANV_DRAWID_VB_INDEX,
238 .Valid = true,
239 .SourceElementFormat = ISL_FORMAT_R32_UINT,
240 .Component0Control = VFCOMP_STORE_SRC,
241 .Component1Control = VFCOMP_STORE_0,
242 .Component2Control = VFCOMP_STORE_0,
243 .Component3Control = VFCOMP_STORE_0,
244 };
245 GENX(VERTEX_ELEMENT_STATE_pack)(NULL,
246 &p[1 + drawid_slot * 2],
247 &element);
248
249 #if GEN_GEN >= 8
250 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_INSTANCING), vfi) {
251 vfi.VertexElementIndex = drawid_slot;
252 }
253 #endif
254 }
255 }
256
257 void
258 genX(emit_urb_setup)(struct anv_device *device, struct anv_batch *batch,
259 const struct gen_l3_config *l3_config,
260 VkShaderStageFlags active_stages,
261 const unsigned entry_size[4])
262 {
263 const struct gen_device_info *devinfo = &device->info;
264 #if GEN_IS_HASWELL
265 const unsigned push_constant_kb = devinfo->gt == 3 ? 32 : 16;
266 #else
267 const unsigned push_constant_kb = GEN_GEN >= 8 ? 32 : 16;
268 #endif
269
270 const unsigned urb_size_kb = gen_get_l3_config_urb_size(devinfo, l3_config);
271
272 unsigned entries[4];
273 unsigned start[4];
274 gen_get_urb_config(devinfo,
275 1024 * push_constant_kb, 1024 * urb_size_kb,
276 active_stages &
277 VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT,
278 active_stages & VK_SHADER_STAGE_GEOMETRY_BIT,
279 entry_size, entries, start);
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 = (struct anv_address) { &device->workaround_bo, 0 };
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_pipeline *pipeline)
309 {
310 unsigned entry_size[4];
311 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
312 const struct brw_vue_prog_data *prog_data =
313 !anv_pipeline_has_stage(pipeline, i) ? NULL :
314 (const struct brw_vue_prog_data *) pipeline->shaders[i]->prog_data;
315
316 entry_size[i] = prog_data ? prog_data->urb_entry_size : 1;
317 }
318
319 genX(emit_urb_setup)(pipeline->device, &pipeline->batch,
320 pipeline->urb.l3_config,
321 pipeline->active_stages, entry_size);
322 }
323
324 static void
325 emit_3dstate_sbe(struct anv_pipeline *pipeline)
326 {
327 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
328
329 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
330 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SBE), sbe);
331 #if GEN_GEN >= 8
332 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SBE_SWIZ), sbe);
333 #endif
334 return;
335 }
336
337 const struct brw_vue_map *fs_input_map =
338 &anv_pipeline_get_last_vue_prog_data(pipeline)->vue_map;
339
340 struct GENX(3DSTATE_SBE) sbe = {
341 GENX(3DSTATE_SBE_header),
342 .AttributeSwizzleEnable = true,
343 .PointSpriteTextureCoordinateOrigin = UPPERLEFT,
344 .NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs,
345 .ConstantInterpolationEnable = wm_prog_data->flat_inputs,
346 };
347
348 #if GEN_GEN >= 9
349 for (unsigned i = 0; i < 32; i++)
350 sbe.AttributeActiveComponentFormat[i] = ACF_XYZW;
351 #endif
352
353 #if GEN_GEN >= 8
354 /* On Broadwell, they broke 3DSTATE_SBE into two packets */
355 struct GENX(3DSTATE_SBE_SWIZ) swiz = {
356 GENX(3DSTATE_SBE_SWIZ_header),
357 };
358 #else
359 # define swiz sbe
360 #endif
361
362 /* Skip the VUE header and position slots by default */
363 unsigned urb_entry_read_offset = 1;
364 int max_source_attr = 0;
365 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
366 int input_index = wm_prog_data->urb_setup[attr];
367
368 if (input_index < 0)
369 continue;
370
371 /* gl_Layer is stored in the VUE header */
372 if (attr == VARYING_SLOT_LAYER) {
373 urb_entry_read_offset = 0;
374 continue;
375 }
376
377 if (attr == VARYING_SLOT_PNTC) {
378 sbe.PointSpriteTextureCoordinateEnable = 1 << input_index;
379 continue;
380 }
381
382 const int slot = fs_input_map->varying_to_slot[attr];
383
384 if (input_index >= 16)
385 continue;
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 } else {
401 /* We have to subtract two slots to accout for the URB entry output
402 * read offset in the VS and GS stages.
403 */
404 const int source_attr = slot - 2 * urb_entry_read_offset;
405 assert(source_attr >= 0 && source_attr < 32);
406 max_source_attr = MAX2(max_source_attr, source_attr);
407 swiz.Attribute[input_index].SourceAttribute = source_attr;
408 }
409 }
410
411 sbe.VertexURBEntryReadOffset = urb_entry_read_offset;
412 sbe.VertexURBEntryReadLength = DIV_ROUND_UP(max_source_attr + 1, 2);
413 #if GEN_GEN >= 8
414 sbe.ForceVertexURBEntryReadOffset = true;
415 sbe.ForceVertexURBEntryReadLength = true;
416 #endif
417
418 uint32_t *dw = anv_batch_emit_dwords(&pipeline->batch,
419 GENX(3DSTATE_SBE_length));
420 if (!dw)
421 return;
422 GENX(3DSTATE_SBE_pack)(&pipeline->batch, dw, &sbe);
423
424 #if GEN_GEN >= 8
425 dw = anv_batch_emit_dwords(&pipeline->batch, GENX(3DSTATE_SBE_SWIZ_length));
426 if (!dw)
427 return;
428 GENX(3DSTATE_SBE_SWIZ_pack)(&pipeline->batch, dw, &swiz);
429 #endif
430 }
431
432 static const uint32_t vk_to_gen_cullmode[] = {
433 [VK_CULL_MODE_NONE] = CULLMODE_NONE,
434 [VK_CULL_MODE_FRONT_BIT] = CULLMODE_FRONT,
435 [VK_CULL_MODE_BACK_BIT] = CULLMODE_BACK,
436 [VK_CULL_MODE_FRONT_AND_BACK] = CULLMODE_BOTH
437 };
438
439 static const uint32_t vk_to_gen_fillmode[] = {
440 [VK_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
441 [VK_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
442 [VK_POLYGON_MODE_POINT] = FILL_MODE_POINT,
443 };
444
445 static const uint32_t vk_to_gen_front_face[] = {
446 [VK_FRONT_FACE_COUNTER_CLOCKWISE] = 1,
447 [VK_FRONT_FACE_CLOCKWISE] = 0
448 };
449
450 static void
451 emit_rs_state(struct anv_pipeline *pipeline,
452 const VkPipelineRasterizationStateCreateInfo *rs_info,
453 const VkPipelineMultisampleStateCreateInfo *ms_info,
454 const struct anv_render_pass *pass,
455 const struct anv_subpass *subpass)
456 {
457 struct GENX(3DSTATE_SF) sf = {
458 GENX(3DSTATE_SF_header),
459 };
460
461 sf.ViewportTransformEnable = true;
462 sf.StatisticsEnable = true;
463 sf.TriangleStripListProvokingVertexSelect = 0;
464 sf.LineStripListProvokingVertexSelect = 0;
465 sf.TriangleFanProvokingVertexSelect = 1;
466
467 const struct brw_vue_prog_data *last_vue_prog_data =
468 anv_pipeline_get_last_vue_prog_data(pipeline);
469
470 if (last_vue_prog_data->vue_map.slots_valid & VARYING_BIT_PSIZ) {
471 sf.PointWidthSource = Vertex;
472 } else {
473 sf.PointWidthSource = State;
474 sf.PointWidth = 1.0;
475 }
476
477 #if GEN_GEN >= 8
478 struct GENX(3DSTATE_RASTER) raster = {
479 GENX(3DSTATE_RASTER_header),
480 };
481 #else
482 # define raster sf
483 #endif
484
485 /* For details on 3DSTATE_RASTER multisample state, see the BSpec table
486 * "Multisample Modes State".
487 */
488 #if GEN_GEN >= 8
489 raster.DXMultisampleRasterizationEnable = true;
490 /* NOTE: 3DSTATE_RASTER::ForcedSampleCount affects the BDW and SKL PMA fix
491 * computations. If we ever set this bit to a different value, they will
492 * need to be updated accordingly.
493 */
494 raster.ForcedSampleCount = FSC_NUMRASTSAMPLES_0;
495 raster.ForceMultisampling = false;
496 #else
497 raster.MultisampleRasterizationMode =
498 (ms_info && ms_info->rasterizationSamples > 1) ?
499 MSRASTMODE_ON_PATTERN : MSRASTMODE_OFF_PIXEL;
500 #endif
501
502 raster.FrontWinding = vk_to_gen_front_face[rs_info->frontFace];
503 raster.CullMode = vk_to_gen_cullmode[rs_info->cullMode];
504 raster.FrontFaceFillMode = vk_to_gen_fillmode[rs_info->polygonMode];
505 raster.BackFaceFillMode = vk_to_gen_fillmode[rs_info->polygonMode];
506 raster.ScissorRectangleEnable = true;
507
508 #if GEN_GEN >= 9
509 /* GEN9+ splits ViewportZClipTestEnable into near and far enable bits */
510 raster.ViewportZFarClipTestEnable = !pipeline->depth_clamp_enable;
511 raster.ViewportZNearClipTestEnable = !pipeline->depth_clamp_enable;
512 #elif GEN_GEN >= 8
513 raster.ViewportZClipTestEnable = !pipeline->depth_clamp_enable;
514 #endif
515
516 raster.GlobalDepthOffsetEnableSolid = rs_info->depthBiasEnable;
517 raster.GlobalDepthOffsetEnableWireframe = rs_info->depthBiasEnable;
518 raster.GlobalDepthOffsetEnablePoint = rs_info->depthBiasEnable;
519
520 #if GEN_GEN == 7
521 /* Gen7 requires that we provide the depth format in 3DSTATE_SF so that it
522 * can get the depth offsets correct.
523 */
524 if (subpass->depth_stencil_attachment) {
525 VkFormat vk_format =
526 pass->attachments[subpass->depth_stencil_attachment->attachment].format;
527 assert(vk_format_is_depth_or_stencil(vk_format));
528 if (vk_format_aspects(vk_format) & VK_IMAGE_ASPECT_DEPTH_BIT) {
529 enum isl_format isl_format =
530 anv_get_isl_format(&pipeline->device->info, vk_format,
531 VK_IMAGE_ASPECT_DEPTH_BIT,
532 VK_IMAGE_TILING_OPTIMAL);
533 sf.DepthBufferSurfaceFormat =
534 isl_format_get_depth_format(isl_format, false);
535 }
536 }
537 #endif
538
539 #if GEN_GEN >= 8
540 GENX(3DSTATE_SF_pack)(NULL, pipeline->gen8.sf, &sf);
541 GENX(3DSTATE_RASTER_pack)(NULL, pipeline->gen8.raster, &raster);
542 #else
543 # undef raster
544 GENX(3DSTATE_SF_pack)(NULL, &pipeline->gen7.sf, &sf);
545 #endif
546 }
547
548 static void
549 emit_ms_state(struct anv_pipeline *pipeline,
550 const VkPipelineMultisampleStateCreateInfo *info)
551 {
552 uint32_t samples = 1;
553 uint32_t log2_samples = 0;
554
555 /* From the Vulkan 1.0 spec:
556 * If pSampleMask is NULL, it is treated as if the mask has all bits
557 * enabled, i.e. no coverage is removed from fragments.
558 *
559 * 3DSTATE_SAMPLE_MASK.SampleMask is 16 bits.
560 */
561 #if GEN_GEN >= 8
562 uint32_t sample_mask = 0xffff;
563 #else
564 uint32_t sample_mask = 0xff;
565 #endif
566
567 if (info) {
568 samples = info->rasterizationSamples;
569 log2_samples = __builtin_ffs(samples) - 1;
570 }
571
572 if (info && info->pSampleMask)
573 sample_mask &= info->pSampleMask[0];
574
575 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_MULTISAMPLE), ms) {
576 ms.NumberofMultisamples = log2_samples;
577
578 ms.PixelLocation = CENTER;
579 #if GEN_GEN >= 8
580 /* The PRM says that this bit is valid only for DX9:
581 *
582 * SW can choose to set this bit only for DX9 API. DX10/OGL API's
583 * should not have any effect by setting or not setting this bit.
584 */
585 ms.PixelPositionOffsetEnable = false;
586 #else
587
588 switch (samples) {
589 case 1:
590 GEN_SAMPLE_POS_1X(ms.Sample);
591 break;
592 case 2:
593 GEN_SAMPLE_POS_2X(ms.Sample);
594 break;
595 case 4:
596 GEN_SAMPLE_POS_4X(ms.Sample);
597 break;
598 case 8:
599 GEN_SAMPLE_POS_8X(ms.Sample);
600 break;
601 default:
602 break;
603 }
604 #endif
605 }
606
607 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SAMPLE_MASK), sm) {
608 sm.SampleMask = sample_mask;
609 }
610 }
611
612 static const uint32_t vk_to_gen_logic_op[] = {
613 [VK_LOGIC_OP_COPY] = LOGICOP_COPY,
614 [VK_LOGIC_OP_CLEAR] = LOGICOP_CLEAR,
615 [VK_LOGIC_OP_AND] = LOGICOP_AND,
616 [VK_LOGIC_OP_AND_REVERSE] = LOGICOP_AND_REVERSE,
617 [VK_LOGIC_OP_AND_INVERTED] = LOGICOP_AND_INVERTED,
618 [VK_LOGIC_OP_NO_OP] = LOGICOP_NOOP,
619 [VK_LOGIC_OP_XOR] = LOGICOP_XOR,
620 [VK_LOGIC_OP_OR] = LOGICOP_OR,
621 [VK_LOGIC_OP_NOR] = LOGICOP_NOR,
622 [VK_LOGIC_OP_EQUIVALENT] = LOGICOP_EQUIV,
623 [VK_LOGIC_OP_INVERT] = LOGICOP_INVERT,
624 [VK_LOGIC_OP_OR_REVERSE] = LOGICOP_OR_REVERSE,
625 [VK_LOGIC_OP_COPY_INVERTED] = LOGICOP_COPY_INVERTED,
626 [VK_LOGIC_OP_OR_INVERTED] = LOGICOP_OR_INVERTED,
627 [VK_LOGIC_OP_NAND] = LOGICOP_NAND,
628 [VK_LOGIC_OP_SET] = LOGICOP_SET,
629 };
630
631 static const uint32_t vk_to_gen_blend[] = {
632 [VK_BLEND_FACTOR_ZERO] = BLENDFACTOR_ZERO,
633 [VK_BLEND_FACTOR_ONE] = BLENDFACTOR_ONE,
634 [VK_BLEND_FACTOR_SRC_COLOR] = BLENDFACTOR_SRC_COLOR,
635 [VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR] = BLENDFACTOR_INV_SRC_COLOR,
636 [VK_BLEND_FACTOR_DST_COLOR] = BLENDFACTOR_DST_COLOR,
637 [VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR] = BLENDFACTOR_INV_DST_COLOR,
638 [VK_BLEND_FACTOR_SRC_ALPHA] = BLENDFACTOR_SRC_ALPHA,
639 [VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA] = BLENDFACTOR_INV_SRC_ALPHA,
640 [VK_BLEND_FACTOR_DST_ALPHA] = BLENDFACTOR_DST_ALPHA,
641 [VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA] = BLENDFACTOR_INV_DST_ALPHA,
642 [VK_BLEND_FACTOR_CONSTANT_COLOR] = BLENDFACTOR_CONST_COLOR,
643 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR]= BLENDFACTOR_INV_CONST_COLOR,
644 [VK_BLEND_FACTOR_CONSTANT_ALPHA] = BLENDFACTOR_CONST_ALPHA,
645 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA]= BLENDFACTOR_INV_CONST_ALPHA,
646 [VK_BLEND_FACTOR_SRC_ALPHA_SATURATE] = BLENDFACTOR_SRC_ALPHA_SATURATE,
647 [VK_BLEND_FACTOR_SRC1_COLOR] = BLENDFACTOR_SRC1_COLOR,
648 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR] = BLENDFACTOR_INV_SRC1_COLOR,
649 [VK_BLEND_FACTOR_SRC1_ALPHA] = BLENDFACTOR_SRC1_ALPHA,
650 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA] = BLENDFACTOR_INV_SRC1_ALPHA,
651 };
652
653 static const uint32_t vk_to_gen_blend_op[] = {
654 [VK_BLEND_OP_ADD] = BLENDFUNCTION_ADD,
655 [VK_BLEND_OP_SUBTRACT] = BLENDFUNCTION_SUBTRACT,
656 [VK_BLEND_OP_REVERSE_SUBTRACT] = BLENDFUNCTION_REVERSE_SUBTRACT,
657 [VK_BLEND_OP_MIN] = BLENDFUNCTION_MIN,
658 [VK_BLEND_OP_MAX] = BLENDFUNCTION_MAX,
659 };
660
661 static const uint32_t vk_to_gen_compare_op[] = {
662 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
663 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
664 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
665 [VK_COMPARE_OP_LESS_OR_EQUAL] = PREFILTEROPLEQUAL,
666 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
667 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
668 [VK_COMPARE_OP_GREATER_OR_EQUAL] = PREFILTEROPGEQUAL,
669 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
670 };
671
672 static const uint32_t vk_to_gen_stencil_op[] = {
673 [VK_STENCIL_OP_KEEP] = STENCILOP_KEEP,
674 [VK_STENCIL_OP_ZERO] = STENCILOP_ZERO,
675 [VK_STENCIL_OP_REPLACE] = STENCILOP_REPLACE,
676 [VK_STENCIL_OP_INCREMENT_AND_CLAMP] = STENCILOP_INCRSAT,
677 [VK_STENCIL_OP_DECREMENT_AND_CLAMP] = STENCILOP_DECRSAT,
678 [VK_STENCIL_OP_INVERT] = STENCILOP_INVERT,
679 [VK_STENCIL_OP_INCREMENT_AND_WRAP] = STENCILOP_INCR,
680 [VK_STENCIL_OP_DECREMENT_AND_WRAP] = STENCILOP_DECR,
681 };
682
683 /* This function sanitizes the VkStencilOpState by looking at the compare ops
684 * and trying to determine whether or not a given stencil op can ever actually
685 * occur. Stencil ops which can never occur are set to VK_STENCIL_OP_KEEP.
686 * This function returns true if, after sanitation, any of the stencil ops are
687 * set to something other than VK_STENCIL_OP_KEEP.
688 */
689 static bool
690 sanitize_stencil_face(VkStencilOpState *face,
691 VkCompareOp depthCompareOp)
692 {
693 /* If compareOp is ALWAYS then the stencil test will never fail and failOp
694 * will never happen. Set failOp to KEEP in this case.
695 */
696 if (face->compareOp == VK_COMPARE_OP_ALWAYS)
697 face->failOp = VK_STENCIL_OP_KEEP;
698
699 /* If compareOp is NEVER or depthCompareOp is NEVER then one of the depth
700 * or stencil tests will fail and passOp will never happen.
701 */
702 if (face->compareOp == VK_COMPARE_OP_NEVER ||
703 depthCompareOp == VK_COMPARE_OP_NEVER)
704 face->passOp = VK_STENCIL_OP_KEEP;
705
706 /* If compareOp is NEVER or depthCompareOp is ALWAYS then either the
707 * stencil test will fail or the depth test will pass. In either case,
708 * depthFailOp will never happen.
709 */
710 if (face->compareOp == VK_COMPARE_OP_NEVER ||
711 depthCompareOp == VK_COMPARE_OP_ALWAYS)
712 face->depthFailOp = VK_STENCIL_OP_KEEP;
713
714 return face->failOp != VK_STENCIL_OP_KEEP ||
715 face->depthFailOp != VK_STENCIL_OP_KEEP ||
716 face->passOp != VK_STENCIL_OP_KEEP;
717 }
718
719 /* Intel hardware is fairly sensitive to whether or not depth/stencil writes
720 * are enabled. In the presence of discards, it's fairly easy to get into the
721 * non-promoted case which means a fairly big performance hit. From the Iron
722 * Lake PRM, Vol 2, pt. 1, section 8.4.3.2, "Early Depth Test Cases":
723 *
724 * "Non-promoted depth (N) is active whenever the depth test can be done
725 * early but it cannot determine whether or not to write source depth to
726 * the depth buffer, therefore the depth write must be performed post pixel
727 * shader. This includes cases where the pixel shader can kill pixels,
728 * including via sampler chroma key, as well as cases where the alpha test
729 * function is enabled, which kills pixels based on a programmable alpha
730 * test. In this case, even if the depth test fails, the pixel cannot be
731 * killed if a stencil write is indicated. Whether or not the stencil write
732 * happens depends on whether or not the pixel is killed later. In these
733 * cases if stencil test fails and stencil writes are off, the pixels can
734 * also be killed early. If stencil writes are enabled, the pixels must be
735 * treated as Computed depth (described above)."
736 *
737 * The same thing as mentioned in the stencil case can happen in the depth
738 * case as well if it thinks it writes depth but, thanks to the depth test
739 * being GL_EQUAL, the write doesn't actually matter. A little extra work
740 * up-front to try and disable depth and stencil writes can make a big
741 * difference.
742 *
743 * Unfortunately, the way depth and stencil testing is specified, there are
744 * many case where, regardless of depth/stencil writes being enabled, nothing
745 * actually gets written due to some other bit of state being set. This
746 * function attempts to "sanitize" the depth stencil state and disable writes
747 * and sometimes even testing whenever possible.
748 */
749 static void
750 sanitize_ds_state(VkPipelineDepthStencilStateCreateInfo *state,
751 bool *stencilWriteEnable,
752 VkImageAspectFlags ds_aspects)
753 {
754 *stencilWriteEnable = state->stencilTestEnable;
755
756 /* If the depth test is disabled, we won't be writing anything. Make sure we
757 * treat the test as always passing later on as well.
758 *
759 * Also, the Vulkan spec requires that if either depth or stencil is not
760 * present, the pipeline is to act as if the test silently passes. In that
761 * case we won't write either.
762 */
763 if (!state->depthTestEnable || !(ds_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
764 state->depthWriteEnable = false;
765 state->depthCompareOp = VK_COMPARE_OP_ALWAYS;
766 }
767
768 if (!(ds_aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
769 *stencilWriteEnable = false;
770 state->front.compareOp = VK_COMPARE_OP_ALWAYS;
771 state->back.compareOp = VK_COMPARE_OP_ALWAYS;
772 }
773
774 /* If the stencil test is enabled and always fails, then we will never get
775 * to the depth test so we can just disable the depth test entirely.
776 */
777 if (state->stencilTestEnable &&
778 state->front.compareOp == VK_COMPARE_OP_NEVER &&
779 state->back.compareOp == VK_COMPARE_OP_NEVER) {
780 state->depthTestEnable = false;
781 state->depthWriteEnable = false;
782 }
783
784 /* If depthCompareOp is EQUAL then the value we would be writing to the
785 * depth buffer is the same as the value that's already there so there's no
786 * point in writing it.
787 */
788 if (state->depthCompareOp == VK_COMPARE_OP_EQUAL)
789 state->depthWriteEnable = false;
790
791 /* If the stencil ops are such that we don't actually ever modify the
792 * stencil buffer, we should disable writes.
793 */
794 if (!sanitize_stencil_face(&state->front, state->depthCompareOp) &&
795 !sanitize_stencil_face(&state->back, state->depthCompareOp))
796 *stencilWriteEnable = false;
797
798 /* If the depth test always passes and we never write out depth, that's the
799 * same as if the depth test is disabled entirely.
800 */
801 if (state->depthCompareOp == VK_COMPARE_OP_ALWAYS &&
802 !state->depthWriteEnable)
803 state->depthTestEnable = false;
804
805 /* If the stencil test always passes and we never write out stencil, that's
806 * the same as if the stencil test is disabled entirely.
807 */
808 if (state->front.compareOp == VK_COMPARE_OP_ALWAYS &&
809 state->back.compareOp == VK_COMPARE_OP_ALWAYS &&
810 !*stencilWriteEnable)
811 state->stencilTestEnable = false;
812 }
813
814 static void
815 emit_ds_state(struct anv_pipeline *pipeline,
816 const VkPipelineDepthStencilStateCreateInfo *pCreateInfo,
817 const struct anv_render_pass *pass,
818 const struct anv_subpass *subpass)
819 {
820 #if GEN_GEN == 7
821 # define depth_stencil_dw pipeline->gen7.depth_stencil_state
822 #elif GEN_GEN == 8
823 # define depth_stencil_dw pipeline->gen8.wm_depth_stencil
824 #else
825 # define depth_stencil_dw pipeline->gen9.wm_depth_stencil
826 #endif
827
828 if (pCreateInfo == NULL) {
829 /* We're going to OR this together with the dynamic state. We need
830 * to make sure it's initialized to something useful.
831 */
832 pipeline->writes_stencil = false;
833 pipeline->stencil_test_enable = false;
834 pipeline->writes_depth = false;
835 pipeline->depth_test_enable = false;
836 memset(depth_stencil_dw, 0, sizeof(depth_stencil_dw));
837 return;
838 }
839
840 VkImageAspectFlags ds_aspects = 0;
841 if (subpass->depth_stencil_attachment) {
842 VkFormat depth_stencil_format =
843 pass->attachments[subpass->depth_stencil_attachment->attachment].format;
844 ds_aspects = vk_format_aspects(depth_stencil_format);
845 }
846
847 VkPipelineDepthStencilStateCreateInfo info = *pCreateInfo;
848 sanitize_ds_state(&info, &pipeline->writes_stencil, ds_aspects);
849 pipeline->stencil_test_enable = info.stencilTestEnable;
850 pipeline->writes_depth = info.depthWriteEnable;
851 pipeline->depth_test_enable = info.depthTestEnable;
852
853 /* VkBool32 depthBoundsTestEnable; // optional (depth_bounds_test) */
854
855 #if GEN_GEN <= 7
856 struct GENX(DEPTH_STENCIL_STATE) depth_stencil = {
857 #else
858 struct GENX(3DSTATE_WM_DEPTH_STENCIL) depth_stencil = {
859 #endif
860 .DepthTestEnable = info.depthTestEnable,
861 .DepthBufferWriteEnable = info.depthWriteEnable,
862 .DepthTestFunction = vk_to_gen_compare_op[info.depthCompareOp],
863 .DoubleSidedStencilEnable = true,
864
865 .StencilTestEnable = info.stencilTestEnable,
866 .StencilFailOp = vk_to_gen_stencil_op[info.front.failOp],
867 .StencilPassDepthPassOp = vk_to_gen_stencil_op[info.front.passOp],
868 .StencilPassDepthFailOp = vk_to_gen_stencil_op[info.front.depthFailOp],
869 .StencilTestFunction = vk_to_gen_compare_op[info.front.compareOp],
870 .BackfaceStencilFailOp = vk_to_gen_stencil_op[info.back.failOp],
871 .BackfaceStencilPassDepthPassOp = vk_to_gen_stencil_op[info.back.passOp],
872 .BackfaceStencilPassDepthFailOp =vk_to_gen_stencil_op[info.back.depthFailOp],
873 .BackfaceStencilTestFunction = vk_to_gen_compare_op[info.back.compareOp],
874 };
875
876 #if GEN_GEN <= 7
877 GENX(DEPTH_STENCIL_STATE_pack)(NULL, depth_stencil_dw, &depth_stencil);
878 #else
879 GENX(3DSTATE_WM_DEPTH_STENCIL_pack)(NULL, depth_stencil_dw, &depth_stencil);
880 #endif
881 }
882
883 MAYBE_UNUSED static bool
884 is_dual_src_blend_factor(VkBlendFactor factor)
885 {
886 return factor == VK_BLEND_FACTOR_SRC1_COLOR ||
887 factor == VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR ||
888 factor == VK_BLEND_FACTOR_SRC1_ALPHA ||
889 factor == VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA;
890 }
891
892 static void
893 emit_cb_state(struct anv_pipeline *pipeline,
894 const VkPipelineColorBlendStateCreateInfo *info,
895 const VkPipelineMultisampleStateCreateInfo *ms_info)
896 {
897 struct anv_device *device = pipeline->device;
898 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
899
900 struct GENX(BLEND_STATE) blend_state = {
901 #if GEN_GEN >= 8
902 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
903 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
904 #endif
905 };
906
907 uint32_t surface_count = 0;
908 struct anv_pipeline_bind_map *map;
909 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
910 map = &pipeline->shaders[MESA_SHADER_FRAGMENT]->bind_map;
911 surface_count = map->surface_count;
912 }
913
914 const uint32_t num_dwords = GENX(BLEND_STATE_length) +
915 GENX(BLEND_STATE_ENTRY_length) * surface_count;
916 pipeline->blend_state =
917 anv_state_pool_alloc(&device->dynamic_state_pool, num_dwords * 4, 64);
918
919 bool has_writeable_rt = false;
920 uint32_t *state_pos = pipeline->blend_state.map;
921 state_pos += GENX(BLEND_STATE_length);
922 #if GEN_GEN >= 8
923 struct GENX(BLEND_STATE_ENTRY) bs0 = { 0 };
924 #endif
925 for (unsigned i = 0; i < surface_count; i++) {
926 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[i];
927
928 /* All color attachments are at the beginning of the binding table */
929 if (binding->set != ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS)
930 break;
931
932 /* We can have at most 8 attachments */
933 assert(i < 8);
934
935 if (info == NULL || binding->index >= info->attachmentCount) {
936 /* Default everything to disabled */
937 struct GENX(BLEND_STATE_ENTRY) entry = {
938 .WriteDisableAlpha = true,
939 .WriteDisableRed = true,
940 .WriteDisableGreen = true,
941 .WriteDisableBlue = true,
942 };
943 GENX(BLEND_STATE_ENTRY_pack)(NULL, state_pos, &entry);
944 state_pos += GENX(BLEND_STATE_ENTRY_length);
945 continue;
946 }
947
948 assert(binding->binding == 0);
949 const VkPipelineColorBlendAttachmentState *a =
950 &info->pAttachments[binding->index];
951
952 struct GENX(BLEND_STATE_ENTRY) entry = {
953 #if GEN_GEN < 8
954 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
955 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
956 #endif
957 .LogicOpEnable = info->logicOpEnable,
958 .LogicOpFunction = vk_to_gen_logic_op[info->logicOp],
959 .ColorBufferBlendEnable = a->blendEnable,
960 .ColorClampRange = COLORCLAMP_RTFORMAT,
961 .PreBlendColorClampEnable = true,
962 .PostBlendColorClampEnable = true,
963 .SourceBlendFactor = vk_to_gen_blend[a->srcColorBlendFactor],
964 .DestinationBlendFactor = vk_to_gen_blend[a->dstColorBlendFactor],
965 .ColorBlendFunction = vk_to_gen_blend_op[a->colorBlendOp],
966 .SourceAlphaBlendFactor = vk_to_gen_blend[a->srcAlphaBlendFactor],
967 .DestinationAlphaBlendFactor = vk_to_gen_blend[a->dstAlphaBlendFactor],
968 .AlphaBlendFunction = vk_to_gen_blend_op[a->alphaBlendOp],
969 .WriteDisableAlpha = !(a->colorWriteMask & VK_COLOR_COMPONENT_A_BIT),
970 .WriteDisableRed = !(a->colorWriteMask & VK_COLOR_COMPONENT_R_BIT),
971 .WriteDisableGreen = !(a->colorWriteMask & VK_COLOR_COMPONENT_G_BIT),
972 .WriteDisableBlue = !(a->colorWriteMask & VK_COLOR_COMPONENT_B_BIT),
973 };
974
975 if (a->srcColorBlendFactor != a->srcAlphaBlendFactor ||
976 a->dstColorBlendFactor != a->dstAlphaBlendFactor ||
977 a->colorBlendOp != a->alphaBlendOp) {
978 #if GEN_GEN >= 8
979 blend_state.IndependentAlphaBlendEnable = true;
980 #else
981 entry.IndependentAlphaBlendEnable = true;
982 #endif
983 }
984
985 /* The Dual Source Blending documentation says:
986 *
987 * "If SRC1 is included in a src/dst blend factor and
988 * a DualSource RT Write message is not used, results
989 * are UNDEFINED. (This reflects the same restriction in DX APIs,
990 * where undefined results are produced if “o1” is not written
991 * by a PS – there are no default values defined)."
992 *
993 * There is no way to gracefully fix this undefined situation
994 * so we just disable the blending to prevent possible issues.
995 */
996 if (!wm_prog_data->dual_src_blend &&
997 (is_dual_src_blend_factor(a->srcColorBlendFactor) ||
998 is_dual_src_blend_factor(a->dstColorBlendFactor) ||
999 is_dual_src_blend_factor(a->srcAlphaBlendFactor) ||
1000 is_dual_src_blend_factor(a->dstAlphaBlendFactor))) {
1001 vk_debug_report(&device->instance->debug_report_callbacks,
1002 VK_DEBUG_REPORT_WARNING_BIT_EXT,
1003 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
1004 (uint64_t)(uintptr_t)device,
1005 0, 0, "anv",
1006 "Enabled dual-src blend factors without writing both targets "
1007 "in the shader. Disabling blending to avoid GPU hangs.");
1008 entry.ColorBufferBlendEnable = false;
1009 }
1010
1011 if (a->colorWriteMask != 0)
1012 has_writeable_rt = true;
1013
1014 /* Our hardware applies the blend factor prior to the blend function
1015 * regardless of what function is used. Technically, this means the
1016 * hardware can do MORE than GL or Vulkan specify. However, it also
1017 * means that, for MIN and MAX, we have to stomp the blend factor to
1018 * ONE to make it a no-op.
1019 */
1020 if (a->colorBlendOp == VK_BLEND_OP_MIN ||
1021 a->colorBlendOp == VK_BLEND_OP_MAX) {
1022 entry.SourceBlendFactor = BLENDFACTOR_ONE;
1023 entry.DestinationBlendFactor = BLENDFACTOR_ONE;
1024 }
1025 if (a->alphaBlendOp == VK_BLEND_OP_MIN ||
1026 a->alphaBlendOp == VK_BLEND_OP_MAX) {
1027 entry.SourceAlphaBlendFactor = BLENDFACTOR_ONE;
1028 entry.DestinationAlphaBlendFactor = BLENDFACTOR_ONE;
1029 }
1030 GENX(BLEND_STATE_ENTRY_pack)(NULL, state_pos, &entry);
1031 state_pos += GENX(BLEND_STATE_ENTRY_length);
1032 #if GEN_GEN >= 8
1033 if (i == 0)
1034 bs0 = entry;
1035 #endif
1036 }
1037
1038 #if GEN_GEN >= 8
1039 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS_BLEND), blend) {
1040 blend.AlphaToCoverageEnable = blend_state.AlphaToCoverageEnable;
1041 blend.HasWriteableRT = has_writeable_rt;
1042 blend.ColorBufferBlendEnable = bs0.ColorBufferBlendEnable;
1043 blend.SourceAlphaBlendFactor = bs0.SourceAlphaBlendFactor;
1044 blend.DestinationAlphaBlendFactor = bs0.DestinationAlphaBlendFactor;
1045 blend.SourceBlendFactor = bs0.SourceBlendFactor;
1046 blend.DestinationBlendFactor = bs0.DestinationBlendFactor;
1047 blend.AlphaTestEnable = false;
1048 blend.IndependentAlphaBlendEnable =
1049 blend_state.IndependentAlphaBlendEnable;
1050 }
1051 #else
1052 (void)has_writeable_rt;
1053 #endif
1054
1055 GENX(BLEND_STATE_pack)(NULL, pipeline->blend_state.map, &blend_state);
1056
1057 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_BLEND_STATE_POINTERS), bsp) {
1058 bsp.BlendStatePointer = pipeline->blend_state.offset;
1059 #if GEN_GEN >= 8
1060 bsp.BlendStatePointerValid = true;
1061 #endif
1062 }
1063 }
1064
1065 static void
1066 emit_3dstate_clip(struct anv_pipeline *pipeline,
1067 const VkPipelineViewportStateCreateInfo *vp_info,
1068 const VkPipelineRasterizationStateCreateInfo *rs_info)
1069 {
1070 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1071 (void) wm_prog_data;
1072 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_CLIP), clip) {
1073 clip.ClipEnable = true;
1074 clip.StatisticsEnable = true;
1075 clip.EarlyCullEnable = true;
1076 clip.APIMode = APIMODE_D3D,
1077 clip.ViewportXYClipTestEnable = true;
1078
1079 clip.ClipMode = CLIPMODE_NORMAL;
1080
1081 clip.TriangleStripListProvokingVertexSelect = 0;
1082 clip.LineStripListProvokingVertexSelect = 0;
1083 clip.TriangleFanProvokingVertexSelect = 1;
1084
1085 clip.MinimumPointWidth = 0.125;
1086 clip.MaximumPointWidth = 255.875;
1087
1088 const struct brw_vue_prog_data *last =
1089 anv_pipeline_get_last_vue_prog_data(pipeline);
1090
1091 /* From the Vulkan 1.0.45 spec:
1092 *
1093 * "If the last active vertex processing stage shader entry point's
1094 * interface does not include a variable decorated with
1095 * ViewportIndex, then the first viewport is used."
1096 */
1097 if (vp_info && (last->vue_map.slots_valid & VARYING_BIT_VIEWPORT)) {
1098 clip.MaximumVPIndex = vp_info->viewportCount - 1;
1099 } else {
1100 clip.MaximumVPIndex = 0;
1101 }
1102
1103 /* From the Vulkan 1.0.45 spec:
1104 *
1105 * "If the last active vertex processing stage shader entry point's
1106 * interface does not include a variable decorated with Layer, then
1107 * the first layer is used."
1108 */
1109 clip.ForceZeroRTAIndexEnable =
1110 !(last->vue_map.slots_valid & VARYING_BIT_LAYER);
1111
1112 #if GEN_GEN == 7
1113 clip.FrontWinding = vk_to_gen_front_face[rs_info->frontFace];
1114 clip.CullMode = vk_to_gen_cullmode[rs_info->cullMode];
1115 clip.ViewportZClipTestEnable = !pipeline->depth_clamp_enable;
1116 clip.UserClipDistanceClipTestEnableBitmask = last->clip_distance_mask;
1117 clip.UserClipDistanceCullTestEnableBitmask = last->cull_distance_mask;
1118 #else
1119 clip.NonPerspectiveBarycentricEnable = wm_prog_data ?
1120 (wm_prog_data->barycentric_interp_modes &
1121 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS) != 0 : 0;
1122 #endif
1123 }
1124 }
1125
1126 static void
1127 emit_3dstate_streamout(struct anv_pipeline *pipeline,
1128 const VkPipelineRasterizationStateCreateInfo *rs_info)
1129 {
1130 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_STREAMOUT), so) {
1131 so.RenderingDisable = rs_info->rasterizerDiscardEnable;
1132 }
1133 }
1134
1135 static uint32_t
1136 get_sampler_count(const struct anv_shader_bin *bin)
1137 {
1138 uint32_t count_by_4 = DIV_ROUND_UP(bin->bind_map.sampler_count, 4);
1139
1140 /* We can potentially have way more than 32 samplers and that's ok.
1141 * However, the 3DSTATE_XS packets only have 3 bits to specify how
1142 * many to pre-fetch and all values above 4 are marked reserved.
1143 */
1144 return MIN2(count_by_4, 4);
1145 }
1146
1147 static uint32_t
1148 get_binding_table_entry_count(const struct anv_shader_bin *bin)
1149 {
1150 return DIV_ROUND_UP(bin->bind_map.surface_count, 32);
1151 }
1152
1153 static struct anv_address
1154 get_scratch_address(struct anv_pipeline *pipeline,
1155 gl_shader_stage stage,
1156 const struct anv_shader_bin *bin)
1157 {
1158 return (struct anv_address) {
1159 .bo = anv_scratch_pool_alloc(pipeline->device,
1160 &pipeline->device->scratch_pool,
1161 stage, bin->prog_data->total_scratch),
1162 .offset = 0,
1163 };
1164 }
1165
1166 static uint32_t
1167 get_scratch_space(const struct anv_shader_bin *bin)
1168 {
1169 return ffs(bin->prog_data->total_scratch / 2048);
1170 }
1171
1172 static void
1173 emit_3dstate_vs(struct anv_pipeline *pipeline)
1174 {
1175 const struct gen_device_info *devinfo = &pipeline->device->info;
1176 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
1177 const struct anv_shader_bin *vs_bin =
1178 pipeline->shaders[MESA_SHADER_VERTEX];
1179
1180 assert(anv_pipeline_has_stage(pipeline, MESA_SHADER_VERTEX));
1181
1182 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS), vs) {
1183 vs.Enable = true;
1184 vs.StatisticsEnable = true;
1185 vs.KernelStartPointer = vs_bin->kernel.offset;
1186 #if GEN_GEN >= 8
1187 vs.SIMD8DispatchEnable =
1188 vs_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8;
1189 #endif
1190
1191 assert(!vs_prog_data->base.base.use_alt_mode);
1192 #if GEN_GEN < 11
1193 vs.SingleVertexDispatch = false;
1194 #endif
1195 vs.VectorMaskEnable = false;
1196 /* WA_1606682166:
1197 * Incorrect TDL's SSP address shift in SARB for 16:6 & 18:8 modes.
1198 * Disable the Sampler state prefetch functionality in the SARB by
1199 * programming 0xB000[30] to '1'.
1200 */
1201 vs.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(vs_bin);
1202 /* Gen 11 workarounds table #2056 WABTPPrefetchDisable suggests to
1203 * disable prefetching of binding tables on A0 and B0 steppings.
1204 * TODO: Revisit this WA on newer steppings.
1205 */
1206 vs.BindingTableEntryCount = GEN_GEN == 11 ? 0 : get_binding_table_entry_count(vs_bin);
1207 vs.FloatingPointMode = IEEE754;
1208 vs.IllegalOpcodeExceptionEnable = false;
1209 vs.SoftwareExceptionEnable = false;
1210 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
1211
1212 if (GEN_GEN == 9 && devinfo->gt == 4 &&
1213 anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL)) {
1214 /* On Sky Lake GT4, we have experienced some hangs related to the VS
1215 * cache and tessellation. It is unknown exactly what is happening
1216 * but the Haswell docs for the "VS Reference Count Full Force Miss
1217 * Enable" field of the "Thread Mode" register refer to a HSW bug in
1218 * which the VUE handle reference count would overflow resulting in
1219 * internal reference counting bugs. My (Jason's) best guess is that
1220 * this bug cropped back up on SKL GT4 when we suddenly had more
1221 * threads in play than any previous gen9 hardware.
1222 *
1223 * What we do know for sure is that setting this bit when
1224 * tessellation shaders are in use fixes a GPU hang in Batman: Arkham
1225 * City when playing with DXVK (https://bugs.freedesktop.org/107280).
1226 * Disabling the vertex cache with tessellation shaders should only
1227 * have a minor performance impact as the tessellation shaders are
1228 * likely generating and processing far more geometry than the vertex
1229 * stage.
1230 */
1231 vs.VertexCacheDisable = true;
1232 }
1233
1234 vs.VertexURBEntryReadLength = vs_prog_data->base.urb_read_length;
1235 vs.VertexURBEntryReadOffset = 0;
1236 vs.DispatchGRFStartRegisterForURBData =
1237 vs_prog_data->base.base.dispatch_grf_start_reg;
1238
1239 #if GEN_GEN >= 8
1240 vs.UserClipDistanceClipTestEnableBitmask =
1241 vs_prog_data->base.clip_distance_mask;
1242 vs.UserClipDistanceCullTestEnableBitmask =
1243 vs_prog_data->base.cull_distance_mask;
1244 #endif
1245
1246 vs.PerThreadScratchSpace = get_scratch_space(vs_bin);
1247 vs.ScratchSpaceBasePointer =
1248 get_scratch_address(pipeline, MESA_SHADER_VERTEX, vs_bin);
1249 }
1250 }
1251
1252 static void
1253 emit_3dstate_hs_te_ds(struct anv_pipeline *pipeline,
1254 const VkPipelineTessellationStateCreateInfo *tess_info)
1255 {
1256 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL)) {
1257 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_HS), hs);
1258 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_TE), te);
1259 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_DS), ds);
1260 return;
1261 }
1262
1263 const struct gen_device_info *devinfo = &pipeline->device->info;
1264 const struct anv_shader_bin *tcs_bin =
1265 pipeline->shaders[MESA_SHADER_TESS_CTRL];
1266 const struct anv_shader_bin *tes_bin =
1267 pipeline->shaders[MESA_SHADER_TESS_EVAL];
1268
1269 const struct brw_tcs_prog_data *tcs_prog_data = get_tcs_prog_data(pipeline);
1270 const struct brw_tes_prog_data *tes_prog_data = get_tes_prog_data(pipeline);
1271
1272 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_HS), hs) {
1273 hs.Enable = true;
1274 hs.StatisticsEnable = true;
1275 hs.KernelStartPointer = tcs_bin->kernel.offset;
1276 /* WA_1606682166 */
1277 hs.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(tcs_bin);
1278 /* Gen 11 workarounds table #2056 WABTPPrefetchDisable */
1279 hs.BindingTableEntryCount = GEN_GEN == 11 ? 0 : get_binding_table_entry_count(tcs_bin);
1280 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
1281 hs.IncludeVertexHandles = true;
1282 hs.InstanceCount = tcs_prog_data->instances - 1;
1283
1284 hs.VertexURBEntryReadLength = 0;
1285 hs.VertexURBEntryReadOffset = 0;
1286 hs.DispatchGRFStartRegisterForURBData =
1287 tcs_prog_data->base.base.dispatch_grf_start_reg;
1288
1289 hs.PerThreadScratchSpace = get_scratch_space(tcs_bin);
1290 hs.ScratchSpaceBasePointer =
1291 get_scratch_address(pipeline, MESA_SHADER_TESS_CTRL, tcs_bin);
1292 }
1293
1294 const VkPipelineTessellationDomainOriginStateCreateInfo *domain_origin_state =
1295 tess_info ? vk_find_struct_const(tess_info, PIPELINE_TESSELLATION_DOMAIN_ORIGIN_STATE_CREATE_INFO) : NULL;
1296
1297 VkTessellationDomainOrigin uv_origin =
1298 domain_origin_state ? domain_origin_state->domainOrigin :
1299 VK_TESSELLATION_DOMAIN_ORIGIN_UPPER_LEFT;
1300
1301 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_TE), te) {
1302 te.Partitioning = tes_prog_data->partitioning;
1303
1304 if (uv_origin == VK_TESSELLATION_DOMAIN_ORIGIN_LOWER_LEFT) {
1305 te.OutputTopology = tes_prog_data->output_topology;
1306 } else {
1307 /* When the origin is upper-left, we have to flip the winding order */
1308 if (tes_prog_data->output_topology == OUTPUT_TRI_CCW) {
1309 te.OutputTopology = OUTPUT_TRI_CW;
1310 } else if (tes_prog_data->output_topology == OUTPUT_TRI_CW) {
1311 te.OutputTopology = OUTPUT_TRI_CCW;
1312 } else {
1313 te.OutputTopology = tes_prog_data->output_topology;
1314 }
1315 }
1316
1317 te.TEDomain = tes_prog_data->domain;
1318 te.TEEnable = true;
1319 te.MaximumTessellationFactorOdd = 63.0;
1320 te.MaximumTessellationFactorNotOdd = 64.0;
1321 }
1322
1323 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_DS), ds) {
1324 ds.Enable = true;
1325 ds.StatisticsEnable = true;
1326 ds.KernelStartPointer = tes_bin->kernel.offset;
1327 /* WA_1606682166 */
1328 ds.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(tes_bin);
1329 /* Gen 11 workarounds table #2056 WABTPPrefetchDisable */
1330 ds.BindingTableEntryCount = GEN_GEN == 11 ? 0 : get_binding_table_entry_count(tes_bin);
1331 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
1332
1333 ds.ComputeWCoordinateEnable =
1334 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
1335
1336 ds.PatchURBEntryReadLength = tes_prog_data->base.urb_read_length;
1337 ds.PatchURBEntryReadOffset = 0;
1338 ds.DispatchGRFStartRegisterForURBData =
1339 tes_prog_data->base.base.dispatch_grf_start_reg;
1340
1341 #if GEN_GEN >= 8
1342 #if GEN_GEN < 11
1343 ds.DispatchMode =
1344 tes_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8 ?
1345 DISPATCH_MODE_SIMD8_SINGLE_PATCH :
1346 DISPATCH_MODE_SIMD4X2;
1347 #else
1348 assert(tes_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8);
1349 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
1350 #endif
1351
1352 ds.UserClipDistanceClipTestEnableBitmask =
1353 tes_prog_data->base.clip_distance_mask;
1354 ds.UserClipDistanceCullTestEnableBitmask =
1355 tes_prog_data->base.cull_distance_mask;
1356 #endif
1357
1358 ds.PerThreadScratchSpace = get_scratch_space(tes_bin);
1359 ds.ScratchSpaceBasePointer =
1360 get_scratch_address(pipeline, MESA_SHADER_TESS_EVAL, tes_bin);
1361 }
1362 }
1363
1364 static void
1365 emit_3dstate_gs(struct anv_pipeline *pipeline)
1366 {
1367 const struct gen_device_info *devinfo = &pipeline->device->info;
1368 const struct anv_shader_bin *gs_bin =
1369 pipeline->shaders[MESA_SHADER_GEOMETRY];
1370
1371 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY)) {
1372 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), gs);
1373 return;
1374 }
1375
1376 const struct brw_gs_prog_data *gs_prog_data = get_gs_prog_data(pipeline);
1377
1378 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), gs) {
1379 gs.Enable = true;
1380 gs.StatisticsEnable = true;
1381 gs.KernelStartPointer = gs_bin->kernel.offset;
1382 gs.DispatchMode = gs_prog_data->base.dispatch_mode;
1383
1384 gs.SingleProgramFlow = false;
1385 gs.VectorMaskEnable = false;
1386 /* WA_1606682166 */
1387 gs.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(gs_bin);
1388 /* Gen 11 workarounds table #2056 WABTPPrefetchDisable */
1389 gs.BindingTableEntryCount = GEN_GEN == 11 ? 0 : get_binding_table_entry_count(gs_bin);
1390 gs.IncludeVertexHandles = gs_prog_data->base.include_vue_handles;
1391 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
1392
1393 if (GEN_GEN == 8) {
1394 /* Broadwell is weird. It needs us to divide by 2. */
1395 gs.MaximumNumberofThreads = devinfo->max_gs_threads / 2 - 1;
1396 } else {
1397 gs.MaximumNumberofThreads = devinfo->max_gs_threads - 1;
1398 }
1399
1400 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
1401 gs.OutputTopology = gs_prog_data->output_topology;
1402 gs.VertexURBEntryReadLength = gs_prog_data->base.urb_read_length;
1403 gs.ControlDataFormat = gs_prog_data->control_data_format;
1404 gs.ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords;
1405 gs.InstanceControl = MAX2(gs_prog_data->invocations, 1) - 1;
1406 gs.ReorderMode = TRAILING;
1407
1408 #if GEN_GEN >= 8
1409 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
1410 gs.StaticOutput = gs_prog_data->static_vertex_count >= 0;
1411 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count >= 0 ?
1412 gs_prog_data->static_vertex_count : 0;
1413 #endif
1414
1415 gs.VertexURBEntryReadOffset = 0;
1416 gs.VertexURBEntryReadLength = gs_prog_data->base.urb_read_length;
1417 gs.DispatchGRFStartRegisterForURBData =
1418 gs_prog_data->base.base.dispatch_grf_start_reg;
1419
1420 #if GEN_GEN >= 8
1421 gs.UserClipDistanceClipTestEnableBitmask =
1422 gs_prog_data->base.clip_distance_mask;
1423 gs.UserClipDistanceCullTestEnableBitmask =
1424 gs_prog_data->base.cull_distance_mask;
1425 #endif
1426
1427 gs.PerThreadScratchSpace = get_scratch_space(gs_bin);
1428 gs.ScratchSpaceBasePointer =
1429 get_scratch_address(pipeline, MESA_SHADER_GEOMETRY, gs_bin);
1430 }
1431 }
1432
1433 static bool
1434 has_color_buffer_write_enabled(const struct anv_pipeline *pipeline,
1435 const VkPipelineColorBlendStateCreateInfo *blend)
1436 {
1437 const struct anv_shader_bin *shader_bin =
1438 pipeline->shaders[MESA_SHADER_FRAGMENT];
1439 if (!shader_bin)
1440 return false;
1441
1442 const struct anv_pipeline_bind_map *bind_map = &shader_bin->bind_map;
1443 for (int i = 0; i < bind_map->surface_count; i++) {
1444 struct anv_pipeline_binding *binding = &bind_map->surface_to_descriptor[i];
1445
1446 if (binding->set != ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS)
1447 continue;
1448
1449 if (binding->index == UINT32_MAX)
1450 continue;
1451
1452 if (blend && blend->pAttachments[binding->index].colorWriteMask != 0)
1453 return true;
1454 }
1455
1456 return false;
1457 }
1458
1459 static void
1460 emit_3dstate_wm(struct anv_pipeline *pipeline, struct anv_subpass *subpass,
1461 const VkPipelineColorBlendStateCreateInfo *blend,
1462 const VkPipelineMultisampleStateCreateInfo *multisample)
1463 {
1464 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1465
1466 MAYBE_UNUSED uint32_t samples =
1467 multisample ? multisample->rasterizationSamples : 1;
1468
1469 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM), wm) {
1470 wm.StatisticsEnable = true;
1471 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
1472 wm.LineAntialiasingRegionWidth = _10pixels;
1473 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1474
1475 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1476 if (wm_prog_data->early_fragment_tests) {
1477 wm.EarlyDepthStencilControl = EDSC_PREPS;
1478 } else if (wm_prog_data->has_side_effects) {
1479 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
1480 } else {
1481 wm.EarlyDepthStencilControl = EDSC_NORMAL;
1482 }
1483
1484 #if GEN_GEN >= 8
1485 /* Gen8 hardware tries to compute ThreadDispatchEnable for us but
1486 * doesn't take into account KillPixels when no depth or stencil
1487 * writes are enabled. In order for occlusion queries to work
1488 * correctly with no attachments, we need to force-enable PS thread
1489 * dispatch.
1490 *
1491 * The BDW docs are pretty clear that that this bit isn't validated
1492 * and probably shouldn't be used in production:
1493 *
1494 * "This must always be set to Normal. This field should not be
1495 * tested for functional validation."
1496 *
1497 * Unfortunately, however, the other mechanism we have for doing this
1498 * is 3DSTATE_PS_EXTRA::PixelShaderHasUAV which causes hangs on BDW.
1499 * Given two bad options, we choose the one which works.
1500 */
1501 if ((wm_prog_data->has_side_effects || wm_prog_data->uses_kill) &&
1502 !has_color_buffer_write_enabled(pipeline, blend))
1503 wm.ForceThreadDispatchEnable = ForceON;
1504 #endif
1505
1506 wm.BarycentricInterpolationMode =
1507 wm_prog_data->barycentric_interp_modes;
1508
1509 #if GEN_GEN < 8
1510 wm.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
1511 wm.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
1512 wm.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
1513 wm.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
1514
1515 /* If the subpass has a depth or stencil self-dependency, then we
1516 * need to force the hardware to do the depth/stencil write *after*
1517 * fragment shader execution. Otherwise, the writes may hit memory
1518 * before we get around to fetching from the input attachment and we
1519 * may get the depth or stencil value from the current draw rather
1520 * than the previous one.
1521 */
1522 wm.PixelShaderKillsPixel = subpass->has_ds_self_dep ||
1523 wm_prog_data->uses_kill;
1524
1525 if (wm.PixelShaderComputedDepthMode != PSCDEPTH_OFF ||
1526 wm_prog_data->has_side_effects ||
1527 wm.PixelShaderKillsPixel ||
1528 has_color_buffer_write_enabled(pipeline, blend))
1529 wm.ThreadDispatchEnable = true;
1530
1531 if (samples > 1) {
1532 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
1533 if (wm_prog_data->persample_dispatch) {
1534 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1535 } else {
1536 wm.MultisampleDispatchMode = MSDISPMODE_PERPIXEL;
1537 }
1538 } else {
1539 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
1540 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1541 }
1542 #endif
1543 }
1544 }
1545 }
1546
1547 static void
1548 emit_3dstate_ps(struct anv_pipeline *pipeline,
1549 const VkPipelineColorBlendStateCreateInfo *blend,
1550 const VkPipelineMultisampleStateCreateInfo *multisample)
1551 {
1552 MAYBE_UNUSED const struct gen_device_info *devinfo = &pipeline->device->info;
1553 const struct anv_shader_bin *fs_bin =
1554 pipeline->shaders[MESA_SHADER_FRAGMENT];
1555
1556 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1557 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS), ps) {
1558 #if GEN_GEN == 7
1559 /* Even if no fragments are ever dispatched, gen7 hardware hangs if
1560 * we don't at least set the maximum number of threads.
1561 */
1562 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
1563 #endif
1564 }
1565 return;
1566 }
1567
1568 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1569
1570 #if GEN_GEN < 8
1571 /* The hardware wedges if you have this bit set but don't turn on any dual
1572 * source blend factors.
1573 */
1574 bool dual_src_blend = false;
1575 if (wm_prog_data->dual_src_blend && blend) {
1576 for (uint32_t i = 0; i < blend->attachmentCount; i++) {
1577 const VkPipelineColorBlendAttachmentState *bstate =
1578 &blend->pAttachments[i];
1579
1580 if (bstate->blendEnable &&
1581 (is_dual_src_blend_factor(bstate->srcColorBlendFactor) ||
1582 is_dual_src_blend_factor(bstate->dstColorBlendFactor) ||
1583 is_dual_src_blend_factor(bstate->srcAlphaBlendFactor) ||
1584 is_dual_src_blend_factor(bstate->dstAlphaBlendFactor))) {
1585 dual_src_blend = true;
1586 break;
1587 }
1588 }
1589 }
1590 #endif
1591
1592 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS), ps) {
1593 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
1594 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
1595 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
1596
1597 /* From the Sky Lake PRM 3DSTATE_PS::32 Pixel Dispatch Enable:
1598 *
1599 * "When NUM_MULTISAMPLES = 16 or FORCE_SAMPLE_COUNT = 16, SIMD32
1600 * Dispatch must not be enabled for PER_PIXEL dispatch mode."
1601 *
1602 * Since 16x MSAA is first introduced on SKL, we don't need to apply
1603 * the workaround on any older hardware.
1604 */
1605 if (GEN_GEN >= 9 && !wm_prog_data->persample_dispatch &&
1606 multisample && multisample->rasterizationSamples == 16) {
1607 assert(ps._8PixelDispatchEnable || ps._16PixelDispatchEnable);
1608 ps._32PixelDispatchEnable = false;
1609 }
1610
1611 ps.KernelStartPointer0 = fs_bin->kernel.offset +
1612 brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
1613 ps.KernelStartPointer1 = fs_bin->kernel.offset +
1614 brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
1615 ps.KernelStartPointer2 = fs_bin->kernel.offset +
1616 brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
1617
1618 ps.SingleProgramFlow = false;
1619 ps.VectorMaskEnable = true;
1620 /* WA_1606682166 */
1621 ps.SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(fs_bin);
1622 /* Gen 11 workarounds table #2056 WABTPPrefetchDisable */
1623 ps.BindingTableEntryCount = GEN_GEN == 11 ? 0 : get_binding_table_entry_count(fs_bin);
1624 ps.PushConstantEnable = wm_prog_data->base.nr_params > 0 ||
1625 wm_prog_data->base.ubo_ranges[0].length;
1626 ps.PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
1627 POSOFFSET_SAMPLE: POSOFFSET_NONE;
1628 #if GEN_GEN < 8
1629 ps.AttributeEnable = wm_prog_data->num_varying_inputs > 0;
1630 ps.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
1631 ps.DualSourceBlendEnable = dual_src_blend;
1632 #endif
1633
1634 #if GEN_IS_HASWELL
1635 /* Haswell requires the sample mask to be set in this packet as well
1636 * as in 3DSTATE_SAMPLE_MASK; the values should match.
1637 */
1638 ps.SampleMask = 0xff;
1639 #endif
1640
1641 #if GEN_GEN >= 9
1642 ps.MaximumNumberofThreadsPerPSD = 64 - 1;
1643 #elif GEN_GEN >= 8
1644 ps.MaximumNumberofThreadsPerPSD = 64 - 2;
1645 #else
1646 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
1647 #endif
1648
1649 ps.DispatchGRFStartRegisterForConstantSetupData0 =
1650 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
1651 ps.DispatchGRFStartRegisterForConstantSetupData1 =
1652 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
1653 ps.DispatchGRFStartRegisterForConstantSetupData2 =
1654 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
1655
1656 ps.PerThreadScratchSpace = get_scratch_space(fs_bin);
1657 ps.ScratchSpaceBasePointer =
1658 get_scratch_address(pipeline, MESA_SHADER_FRAGMENT, fs_bin);
1659 }
1660 }
1661
1662 #if GEN_GEN >= 8
1663 static void
1664 emit_3dstate_ps_extra(struct anv_pipeline *pipeline,
1665 struct anv_subpass *subpass,
1666 const VkPipelineColorBlendStateCreateInfo *blend)
1667 {
1668 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1669
1670 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1671 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS_EXTRA), ps);
1672 return;
1673 }
1674
1675 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS_EXTRA), ps) {
1676 ps.PixelShaderValid = true;
1677 ps.AttributeEnable = wm_prog_data->num_varying_inputs > 0;
1678 ps.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
1679 ps.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
1680 ps.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
1681 ps.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
1682 ps.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
1683
1684 /* If the subpass has a depth or stencil self-dependency, then we need
1685 * to force the hardware to do the depth/stencil write *after* fragment
1686 * shader execution. Otherwise, the writes may hit memory before we get
1687 * around to fetching from the input attachment and we may get the depth
1688 * or stencil value from the current draw rather than the previous one.
1689 */
1690 ps.PixelShaderKillsPixel = subpass->has_ds_self_dep ||
1691 wm_prog_data->uses_kill;
1692
1693 #if GEN_GEN >= 9
1694 ps.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
1695 ps.PixelShaderPullsBary = wm_prog_data->pulls_bary;
1696
1697 ps.InputCoverageMaskState = ICMS_NONE;
1698 if (wm_prog_data->uses_sample_mask) {
1699 if (wm_prog_data->post_depth_coverage)
1700 ps.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
1701 else
1702 ps.InputCoverageMaskState = ICMS_INNER_CONSERVATIVE;
1703 }
1704 #else
1705 ps.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
1706 #endif
1707 }
1708 }
1709
1710 static void
1711 emit_3dstate_vf_topology(struct anv_pipeline *pipeline)
1712 {
1713 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_TOPOLOGY), vft) {
1714 vft.PrimitiveTopologyType = pipeline->topology;
1715 }
1716 }
1717 #endif
1718
1719 static void
1720 emit_3dstate_vf_statistics(struct anv_pipeline *pipeline)
1721 {
1722 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_STATISTICS), vfs) {
1723 vfs.StatisticsEnable = true;
1724 }
1725 }
1726
1727 static void
1728 compute_kill_pixel(struct anv_pipeline *pipeline,
1729 const VkPipelineMultisampleStateCreateInfo *ms_info,
1730 const struct anv_subpass *subpass)
1731 {
1732 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1733 pipeline->kill_pixel = false;
1734 return;
1735 }
1736
1737 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1738
1739 /* This computes the KillPixel portion of the computation for whether or
1740 * not we want to enable the PMA fix on gen8 or gen9. It's given by this
1741 * chunk of the giant formula:
1742 *
1743 * (3DSTATE_PS_EXTRA::PixelShaderKillsPixels ||
1744 * 3DSTATE_PS_EXTRA::oMask Present to RenderTarget ||
1745 * 3DSTATE_PS_BLEND::AlphaToCoverageEnable ||
1746 * 3DSTATE_PS_BLEND::AlphaTestEnable ||
1747 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable)
1748 *
1749 * 3DSTATE_WM_CHROMAKEY::ChromaKeyKillEnable is always false and so is
1750 * 3DSTATE_PS_BLEND::AlphaTestEnable since Vulkan doesn't have a concept
1751 * of an alpha test.
1752 */
1753 pipeline->kill_pixel =
1754 subpass->has_ds_self_dep || wm_prog_data->uses_kill ||
1755 wm_prog_data->uses_omask ||
1756 (ms_info && ms_info->alphaToCoverageEnable);
1757 }
1758
1759 static VkResult
1760 genX(graphics_pipeline_create)(
1761 VkDevice _device,
1762 struct anv_pipeline_cache * cache,
1763 const VkGraphicsPipelineCreateInfo* pCreateInfo,
1764 const VkAllocationCallbacks* pAllocator,
1765 VkPipeline* pPipeline)
1766 {
1767 ANV_FROM_HANDLE(anv_device, device, _device);
1768 ANV_FROM_HANDLE(anv_render_pass, pass, pCreateInfo->renderPass);
1769 struct anv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1770 struct anv_pipeline *pipeline;
1771 VkResult result;
1772
1773 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
1774
1775 /* Use the default pipeline cache if none is specified */
1776 if (cache == NULL && device->instance->pipeline_cache_enabled)
1777 cache = &device->default_pipeline_cache;
1778
1779 pipeline = vk_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
1780 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1781 if (pipeline == NULL)
1782 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1783
1784 result = anv_pipeline_init(pipeline, device, cache,
1785 pCreateInfo, pAllocator);
1786 if (result != VK_SUCCESS) {
1787 vk_free2(&device->alloc, pAllocator, pipeline);
1788 return result;
1789 }
1790
1791 assert(pCreateInfo->pVertexInputState);
1792 emit_vertex_input(pipeline, pCreateInfo->pVertexInputState);
1793 assert(pCreateInfo->pRasterizationState);
1794 emit_rs_state(pipeline, pCreateInfo->pRasterizationState,
1795 pCreateInfo->pMultisampleState, pass, subpass);
1796 emit_ms_state(pipeline, pCreateInfo->pMultisampleState);
1797 emit_ds_state(pipeline, pCreateInfo->pDepthStencilState, pass, subpass);
1798 emit_cb_state(pipeline, pCreateInfo->pColorBlendState,
1799 pCreateInfo->pMultisampleState);
1800 compute_kill_pixel(pipeline, pCreateInfo->pMultisampleState, subpass);
1801
1802 emit_urb_setup(pipeline);
1803
1804 emit_3dstate_clip(pipeline, pCreateInfo->pViewportState,
1805 pCreateInfo->pRasterizationState);
1806 emit_3dstate_streamout(pipeline, pCreateInfo->pRasterizationState);
1807
1808 #if 0
1809 /* From gen7_vs_state.c */
1810
1811 /**
1812 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
1813 * Geometry > Geometry Shader > State:
1814 *
1815 * "Note: Because of corruption in IVB:GT2, software needs to flush the
1816 * whole fixed function pipeline when the GS enable changes value in
1817 * the 3DSTATE_GS."
1818 *
1819 * The hardware architects have clarified that in this context "flush the
1820 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
1821 * Stall" bit set.
1822 */
1823 if (!device->info.is_haswell && !device->info.is_baytrail)
1824 gen7_emit_vs_workaround_flush(brw);
1825 #endif
1826
1827 emit_3dstate_vs(pipeline);
1828 emit_3dstate_hs_te_ds(pipeline, pCreateInfo->pTessellationState);
1829 emit_3dstate_gs(pipeline);
1830 emit_3dstate_sbe(pipeline);
1831 emit_3dstate_wm(pipeline, subpass, pCreateInfo->pColorBlendState,
1832 pCreateInfo->pMultisampleState);
1833 emit_3dstate_ps(pipeline, pCreateInfo->pColorBlendState,
1834 pCreateInfo->pMultisampleState);
1835 #if GEN_GEN >= 8
1836 emit_3dstate_ps_extra(pipeline, subpass, pCreateInfo->pColorBlendState);
1837 emit_3dstate_vf_topology(pipeline);
1838 #endif
1839 emit_3dstate_vf_statistics(pipeline);
1840
1841 *pPipeline = anv_pipeline_to_handle(pipeline);
1842
1843 return pipeline->batch.status;
1844 }
1845
1846 static VkResult
1847 compute_pipeline_create(
1848 VkDevice _device,
1849 struct anv_pipeline_cache * cache,
1850 const VkComputePipelineCreateInfo* pCreateInfo,
1851 const VkAllocationCallbacks* pAllocator,
1852 VkPipeline* pPipeline)
1853 {
1854 ANV_FROM_HANDLE(anv_device, device, _device);
1855 const struct anv_physical_device *physical_device =
1856 &device->instance->physicalDevice;
1857 const struct gen_device_info *devinfo = &physical_device->info;
1858 struct anv_pipeline *pipeline;
1859 VkResult result;
1860
1861 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO);
1862
1863 /* Use the default pipeline cache if none is specified */
1864 if (cache == NULL && device->instance->pipeline_cache_enabled)
1865 cache = &device->default_pipeline_cache;
1866
1867 pipeline = vk_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
1868 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1869 if (pipeline == NULL)
1870 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1871
1872 pipeline->device = device;
1873
1874 pipeline->blend_state.map = NULL;
1875
1876 result = anv_reloc_list_init(&pipeline->batch_relocs,
1877 pAllocator ? pAllocator : &device->alloc);
1878 if (result != VK_SUCCESS) {
1879 vk_free2(&device->alloc, pAllocator, pipeline);
1880 return result;
1881 }
1882 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
1883 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
1884 pipeline->batch.relocs = &pipeline->batch_relocs;
1885 pipeline->batch.status = VK_SUCCESS;
1886
1887 /* When we free the pipeline, we detect stages based on the NULL status
1888 * of various prog_data pointers. Make them NULL by default.
1889 */
1890 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
1891
1892 pipeline->needs_data_cache = false;
1893
1894 assert(pCreateInfo->stage.stage == VK_SHADER_STAGE_COMPUTE_BIT);
1895 pipeline->active_stages |= VK_SHADER_STAGE_COMPUTE_BIT;
1896 ANV_FROM_HANDLE(anv_shader_module, module, pCreateInfo->stage.module);
1897 result = anv_pipeline_compile_cs(pipeline, cache, pCreateInfo, module,
1898 pCreateInfo->stage.pName,
1899 pCreateInfo->stage.pSpecializationInfo);
1900 if (result != VK_SUCCESS) {
1901 vk_free2(&device->alloc, pAllocator, pipeline);
1902 return result;
1903 }
1904
1905 const struct brw_cs_prog_data *cs_prog_data = get_cs_prog_data(pipeline);
1906
1907 anv_pipeline_setup_l3_config(pipeline, cs_prog_data->base.total_shared > 0);
1908
1909 uint32_t group_size = cs_prog_data->local_size[0] *
1910 cs_prog_data->local_size[1] * cs_prog_data->local_size[2];
1911 uint32_t remainder = group_size & (cs_prog_data->simd_size - 1);
1912
1913 if (remainder > 0)
1914 pipeline->cs_right_mask = ~0u >> (32 - remainder);
1915 else
1916 pipeline->cs_right_mask = ~0u >> (32 - cs_prog_data->simd_size);
1917
1918 const uint32_t vfe_curbe_allocation =
1919 ALIGN(cs_prog_data->push.per_thread.regs * cs_prog_data->threads +
1920 cs_prog_data->push.cross_thread.regs, 2);
1921
1922 const uint32_t subslices = MAX2(physical_device->subslice_total, 1);
1923
1924 const struct anv_shader_bin *cs_bin =
1925 pipeline->shaders[MESA_SHADER_COMPUTE];
1926
1927 anv_batch_emit(&pipeline->batch, GENX(MEDIA_VFE_STATE), vfe) {
1928 #if GEN_GEN > 7
1929 vfe.StackSize = 0;
1930 #else
1931 vfe.GPGPUMode = true;
1932 #endif
1933 vfe.MaximumNumberofThreads =
1934 devinfo->max_cs_threads * subslices - 1;
1935 vfe.NumberofURBEntries = GEN_GEN <= 7 ? 0 : 2;
1936 #if GEN_GEN < 11
1937 vfe.ResetGatewayTimer = true;
1938 #endif
1939 #if GEN_GEN <= 8
1940 vfe.BypassGatewayControl = true;
1941 #endif
1942 vfe.URBEntryAllocationSize = GEN_GEN <= 7 ? 0 : 2;
1943 vfe.CURBEAllocationSize = vfe_curbe_allocation;
1944
1945 vfe.PerThreadScratchSpace = get_scratch_space(cs_bin);
1946 vfe.ScratchSpaceBasePointer =
1947 get_scratch_address(pipeline, MESA_SHADER_COMPUTE, cs_bin);
1948 }
1949
1950 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
1951 .KernelStartPointer = cs_bin->kernel.offset,
1952 /* WA_1606682166 */
1953 .SamplerCount = GEN_GEN == 11 ? 0 : get_sampler_count(cs_bin),
1954 /* Gen 11 workarounds table #2056 WABTPPrefetchDisable
1955 *
1956 * We add 1 because the CS indirect parameters buffer isn't accounted
1957 * for in bind_map.surface_count.
1958 */
1959 .BindingTableEntryCount = GEN_GEN == 11 ? 0 : 1 + MIN2(cs_bin->bind_map.surface_count, 30),
1960 .BarrierEnable = cs_prog_data->uses_barrier,
1961 .SharedLocalMemorySize =
1962 encode_slm_size(GEN_GEN, cs_prog_data->base.total_shared),
1963
1964 #if !GEN_IS_HASWELL
1965 .ConstantURBEntryReadOffset = 0,
1966 #endif
1967 .ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs,
1968 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1969 .CrossThreadConstantDataReadLength =
1970 cs_prog_data->push.cross_thread.regs,
1971 #endif
1972
1973 .NumberofThreadsinGPGPUThreadGroup = cs_prog_data->threads,
1974 };
1975 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL,
1976 pipeline->interface_descriptor_data,
1977 &desc);
1978
1979 *pPipeline = anv_pipeline_to_handle(pipeline);
1980
1981 return pipeline->batch.status;
1982 }
1983
1984 VkResult genX(CreateGraphicsPipelines)(
1985 VkDevice _device,
1986 VkPipelineCache pipelineCache,
1987 uint32_t count,
1988 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1989 const VkAllocationCallbacks* pAllocator,
1990 VkPipeline* pPipelines)
1991 {
1992 ANV_FROM_HANDLE(anv_pipeline_cache, pipeline_cache, pipelineCache);
1993
1994 VkResult result = VK_SUCCESS;
1995
1996 unsigned i;
1997 for (i = 0; i < count; i++) {
1998 result = genX(graphics_pipeline_create)(_device,
1999 pipeline_cache,
2000 &pCreateInfos[i],
2001 pAllocator, &pPipelines[i]);
2002
2003 /* Bail out on the first error as it is not obvious what error should be
2004 * report upon 2 different failures. */
2005 if (result != VK_SUCCESS)
2006 break;
2007 }
2008
2009 for (; i < count; i++)
2010 pPipelines[i] = VK_NULL_HANDLE;
2011
2012 return result;
2013 }
2014
2015 VkResult genX(CreateComputePipelines)(
2016 VkDevice _device,
2017 VkPipelineCache pipelineCache,
2018 uint32_t count,
2019 const VkComputePipelineCreateInfo* pCreateInfos,
2020 const VkAllocationCallbacks* pAllocator,
2021 VkPipeline* pPipelines)
2022 {
2023 ANV_FROM_HANDLE(anv_pipeline_cache, pipeline_cache, pipelineCache);
2024
2025 VkResult result = VK_SUCCESS;
2026
2027 unsigned i;
2028 for (i = 0; i < count; i++) {
2029 result = compute_pipeline_create(_device, pipeline_cache,
2030 &pCreateInfos[i],
2031 pAllocator, &pPipelines[i]);
2032
2033 /* Bail out on the first error as it is not obvious what error should be
2034 * report upon 2 different failures. */
2035 if (result != VK_SUCCESS)
2036 break;
2037 }
2038
2039 for (; i < count; i++)
2040 pPipelines[i] = VK_NULL_HANDLE;
2041
2042 return result;
2043 }