anv: Emit 3DSTATE_HS/TE/DS packets.
[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_format_info.h"
32
33 static uint32_t
34 vertex_element_comp_control(enum isl_format format, unsigned comp)
35 {
36 uint8_t bits;
37 switch (comp) {
38 case 0: bits = isl_format_layouts[format].channels.r.bits; break;
39 case 1: bits = isl_format_layouts[format].channels.g.bits; break;
40 case 2: bits = isl_format_layouts[format].channels.b.bits; break;
41 case 3: bits = isl_format_layouts[format].channels.a.bits; break;
42 default: unreachable("Invalid component");
43 }
44
45 /*
46 * Take in account hardware restrictions when dealing with 64-bit floats.
47 *
48 * From Broadwell spec, command reference structures, page 586:
49 * "When SourceElementFormat is set to one of the *64*_PASSTHRU formats,
50 * 64-bit components are stored * in the URB without any conversion. In
51 * this case, vertex elements must be written as 128 or 256 bits, with
52 * VFCOMP_STORE_0 being used to pad the output as required. E.g., if
53 * R64_PASSTHRU is used to copy a 64-bit Red component into the URB,
54 * Component 1 must be specified as VFCOMP_STORE_0 (with Components 2,3
55 * set to VFCOMP_NOSTORE) in order to output a 128-bit vertex element, or
56 * Components 1-3 must be specified as VFCOMP_STORE_0 in order to output
57 * a 256-bit vertex element. Likewise, use of R64G64B64_PASSTHRU requires
58 * Component 3 to be specified as VFCOMP_STORE_0 in order to output a
59 * 256-bit vertex element."
60 */
61 if (bits) {
62 return VFCOMP_STORE_SRC;
63 } else if (comp >= 2 &&
64 !isl_format_layouts[format].channels.b.bits &&
65 isl_format_layouts[format].channels.r.type == ISL_RAW) {
66 /* When emitting 64-bit attributes, we need to write either 128 or 256
67 * bit chunks, using VFCOMP_NOSTORE when not writing the chunk, and
68 * VFCOMP_STORE_0 to pad the written chunk */
69 return VFCOMP_NOSTORE;
70 } else if (comp < 3 ||
71 isl_format_layouts[format].channels.r.type == ISL_RAW) {
72 /* Note we need to pad with value 0, not 1, due hardware restrictions
73 * (see comment above) */
74 return VFCOMP_STORE_0;
75 } else if (isl_format_layouts[format].channels.r.type == ISL_UINT ||
76 isl_format_layouts[format].channels.r.type == ISL_SINT) {
77 assert(comp == 3);
78 return VFCOMP_STORE_1_INT;
79 } else {
80 assert(comp == 3);
81 return VFCOMP_STORE_1_FP;
82 }
83 }
84
85 static void
86 emit_vertex_input(struct anv_pipeline *pipeline,
87 const VkPipelineVertexInputStateCreateInfo *info)
88 {
89 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
90
91 /* Pull inputs_read out of the VS prog data */
92 const uint64_t inputs_read = vs_prog_data->inputs_read;
93 const uint64_t double_inputs_read = vs_prog_data->double_inputs_read;
94 assert((inputs_read & ((1 << VERT_ATTRIB_GENERIC0) - 1)) == 0);
95 const uint32_t elements = inputs_read >> VERT_ATTRIB_GENERIC0;
96 const uint32_t elements_double = double_inputs_read >> VERT_ATTRIB_GENERIC0;
97
98 #if GEN_GEN >= 8
99 /* On BDW+, we only need to allocate space for base ids. Setting up
100 * the actual vertex and instance id is a separate packet.
101 */
102 const bool needs_svgs_elem = vs_prog_data->uses_basevertex ||
103 vs_prog_data->uses_baseinstance;
104 #else
105 /* On Haswell and prior, vertex and instance id are created by using the
106 * ComponentControl fields, so we need an element for any of them.
107 */
108 const bool needs_svgs_elem = vs_prog_data->uses_vertexid ||
109 vs_prog_data->uses_instanceid ||
110 vs_prog_data->uses_basevertex ||
111 vs_prog_data->uses_baseinstance;
112 #endif
113
114 uint32_t elem_count = __builtin_popcount(elements) -
115 __builtin_popcount(elements_double) / 2;
116
117 uint32_t total_elems = elem_count + needs_svgs_elem;
118 if (total_elems == 0)
119 return;
120
121 uint32_t *p;
122
123 const uint32_t num_dwords = 1 + total_elems * 2;
124 p = anv_batch_emitn(&pipeline->batch, num_dwords,
125 GENX(3DSTATE_VERTEX_ELEMENTS));
126 memset(p + 1, 0, (num_dwords - 1) * 4);
127
128 for (uint32_t i = 0; i < info->vertexAttributeDescriptionCount; i++) {
129 const VkVertexInputAttributeDescription *desc =
130 &info->pVertexAttributeDescriptions[i];
131 enum isl_format format = anv_get_isl_format(&pipeline->device->info,
132 desc->format,
133 VK_IMAGE_ASPECT_COLOR_BIT,
134 VK_IMAGE_TILING_LINEAR);
135
136 assert(desc->binding < 32);
137
138 if ((elements & (1 << desc->location)) == 0)
139 continue; /* Binding unused */
140
141 uint32_t slot =
142 __builtin_popcount(elements & ((1 << desc->location) - 1)) -
143 DIV_ROUND_UP(__builtin_popcount(elements_double &
144 ((1 << desc->location) -1)), 2);
145
146 struct GENX(VERTEX_ELEMENT_STATE) element = {
147 .VertexBufferIndex = desc->binding,
148 .Valid = true,
149 .SourceElementFormat = format,
150 .EdgeFlagEnable = false,
151 .SourceElementOffset = desc->offset,
152 .Component0Control = vertex_element_comp_control(format, 0),
153 .Component1Control = vertex_element_comp_control(format, 1),
154 .Component2Control = vertex_element_comp_control(format, 2),
155 .Component3Control = vertex_element_comp_control(format, 3),
156 };
157 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + slot * 2], &element);
158
159 #if GEN_GEN >= 8
160 /* On Broadwell and later, we have a separate VF_INSTANCING packet
161 * that controls instancing. On Haswell and prior, that's part of
162 * VERTEX_BUFFER_STATE which we emit later.
163 */
164 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_INSTANCING), vfi) {
165 vfi.InstancingEnable = pipeline->instancing_enable[desc->binding];
166 vfi.VertexElementIndex = slot;
167 /* Vulkan so far doesn't have an instance divisor, so
168 * this is always 1 (ignored if not instancing). */
169 vfi.InstanceDataStepRate = 1;
170 }
171 #endif
172 }
173
174 const uint32_t id_slot = elem_count;
175 if (needs_svgs_elem) {
176 /* From the Broadwell PRM for the 3D_Vertex_Component_Control enum:
177 * "Within a VERTEX_ELEMENT_STATE structure, if a Component
178 * Control field is set to something other than VFCOMP_STORE_SRC,
179 * no higher-numbered Component Control fields may be set to
180 * VFCOMP_STORE_SRC"
181 *
182 * This means, that if we have BaseInstance, we need BaseVertex as
183 * well. Just do all or nothing.
184 */
185 uint32_t base_ctrl = (vs_prog_data->uses_basevertex ||
186 vs_prog_data->uses_baseinstance) ?
187 VFCOMP_STORE_SRC : VFCOMP_STORE_0;
188
189 struct GENX(VERTEX_ELEMENT_STATE) element = {
190 .VertexBufferIndex = 32, /* Reserved for this */
191 .Valid = true,
192 .SourceElementFormat = ISL_FORMAT_R32G32_UINT,
193 .Component0Control = base_ctrl,
194 .Component1Control = base_ctrl,
195 #if GEN_GEN >= 8
196 .Component2Control = VFCOMP_STORE_0,
197 .Component3Control = VFCOMP_STORE_0,
198 #else
199 .Component2Control = VFCOMP_STORE_VID,
200 .Component3Control = VFCOMP_STORE_IID,
201 #endif
202 };
203 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + id_slot * 2], &element);
204 }
205
206 #if GEN_GEN >= 8
207 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_SGVS), sgvs) {
208 sgvs.VertexIDEnable = vs_prog_data->uses_vertexid;
209 sgvs.VertexIDComponentNumber = 2;
210 sgvs.VertexIDElementOffset = id_slot;
211 sgvs.InstanceIDEnable = vs_prog_data->uses_instanceid;
212 sgvs.InstanceIDComponentNumber = 3;
213 sgvs.InstanceIDElementOffset = id_slot;
214 }
215 #endif
216 }
217
218 void
219 genX(emit_urb_setup)(struct anv_device *device, struct anv_batch *batch,
220 const struct gen_l3_config *l3_config,
221 VkShaderStageFlags active_stages,
222 const unsigned entry_size[4])
223 {
224 const struct gen_device_info *devinfo = &device->info;
225 #if GEN_IS_HASWELL
226 const unsigned push_constant_kb = devinfo->gt == 3 ? 32 : 16;
227 #else
228 const unsigned push_constant_kb = GEN_GEN >= 8 ? 32 : 16;
229 #endif
230
231 const unsigned urb_size_kb = gen_get_l3_config_urb_size(devinfo, l3_config);
232
233 unsigned entries[4];
234 unsigned start[4];
235 gen_get_urb_config(devinfo,
236 1024 * push_constant_kb, 1024 * urb_size_kb,
237 active_stages &
238 VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT,
239 active_stages & VK_SHADER_STAGE_GEOMETRY_BIT,
240 entry_size, entries, start);
241
242 #if GEN_GEN == 7 && !GEN_IS_HASWELL
243 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
244 *
245 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth stall
246 * needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
247 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
248 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL
249 * needs to be sent before any combination of VS associated 3DSTATE."
250 */
251 anv_batch_emit(batch, GEN7_PIPE_CONTROL, pc) {
252 pc.DepthStallEnable = true;
253 pc.PostSyncOperation = WriteImmediateData;
254 pc.Address = (struct anv_address) { &device->workaround_bo, 0 };
255 }
256 #endif
257
258 for (int i = 0; i <= MESA_SHADER_GEOMETRY; i++) {
259 anv_batch_emit(batch, GENX(3DSTATE_URB_VS), urb) {
260 urb._3DCommandSubOpcode += i;
261 urb.VSURBStartingAddress = start[i];
262 urb.VSURBEntryAllocationSize = entry_size[i] - 1;
263 urb.VSNumberofURBEntries = entries[i];
264 }
265 }
266 }
267
268 static inline void
269 emit_urb_setup(struct anv_pipeline *pipeline)
270 {
271 unsigned entry_size[4];
272 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
273 const struct brw_vue_prog_data *prog_data =
274 !anv_pipeline_has_stage(pipeline, i) ? NULL :
275 (const struct brw_vue_prog_data *) pipeline->shaders[i]->prog_data;
276
277 entry_size[i] = prog_data ? prog_data->urb_entry_size : 1;
278 }
279
280 genX(emit_urb_setup)(pipeline->device, &pipeline->batch,
281 pipeline->urb.l3_config,
282 pipeline->active_stages, entry_size);
283 }
284
285 static void
286 emit_3dstate_sbe(struct anv_pipeline *pipeline)
287 {
288 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
289 const struct brw_vue_map *fs_input_map;
290
291 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
292 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SBE), sbe);
293 #if GEN_GEN >= 8
294 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SBE_SWIZ), sbe);
295 #endif
296 return;
297 }
298
299 fs_input_map = anv_pipeline_get_fs_input_map(pipeline);
300
301 struct GENX(3DSTATE_SBE) sbe = {
302 GENX(3DSTATE_SBE_header),
303 .AttributeSwizzleEnable = true,
304 .PointSpriteTextureCoordinateOrigin = UPPERLEFT,
305 .NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs,
306 .ConstantInterpolationEnable = wm_prog_data->flat_inputs,
307 };
308
309 #if GEN_GEN >= 9
310 for (unsigned i = 0; i < 32; i++)
311 sbe.AttributeActiveComponentFormat[i] = ACF_XYZW;
312 #endif
313
314 #if GEN_GEN >= 8
315 /* On Broadwell, they broke 3DSTATE_SBE into two packets */
316 struct GENX(3DSTATE_SBE_SWIZ) swiz = {
317 GENX(3DSTATE_SBE_SWIZ_header),
318 };
319 #else
320 # define swiz sbe
321 #endif
322
323 /* Skip the VUE header and position slots by default */
324 unsigned urb_entry_read_offset = 1;
325 int max_source_attr = 0;
326 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
327 int input_index = wm_prog_data->urb_setup[attr];
328
329 if (input_index < 0)
330 continue;
331
332 /* gl_Layer is stored in the VUE header */
333 if (attr == VARYING_SLOT_LAYER) {
334 urb_entry_read_offset = 0;
335 continue;
336 }
337
338 if (attr == VARYING_SLOT_PNTC) {
339 sbe.PointSpriteTextureCoordinateEnable = 1 << input_index;
340 continue;
341 }
342
343 const int slot = fs_input_map->varying_to_slot[attr];
344
345 if (input_index >= 16)
346 continue;
347
348 if (slot == -1) {
349 /* This attribute does not exist in the VUE--that means that the
350 * vertex shader did not write to it. It could be that it's a
351 * regular varying read by the fragment shader but not written by
352 * the vertex shader or it's gl_PrimitiveID. In the first case the
353 * value is undefined, in the second it needs to be
354 * gl_PrimitiveID.
355 */
356 swiz.Attribute[input_index].ConstantSource = PRIM_ID;
357 swiz.Attribute[input_index].ComponentOverrideX = true;
358 swiz.Attribute[input_index].ComponentOverrideY = true;
359 swiz.Attribute[input_index].ComponentOverrideZ = true;
360 swiz.Attribute[input_index].ComponentOverrideW = true;
361 } else {
362 /* We have to subtract two slots to accout for the URB entry output
363 * read offset in the VS and GS stages.
364 */
365 assert(slot >= 2);
366 const int source_attr = slot - 2 * urb_entry_read_offset;
367 max_source_attr = MAX2(max_source_attr, source_attr);
368 swiz.Attribute[input_index].SourceAttribute = source_attr;
369 }
370 }
371
372 sbe.VertexURBEntryReadOffset = urb_entry_read_offset;
373 sbe.VertexURBEntryReadLength = DIV_ROUND_UP(max_source_attr + 1, 2);
374 #if GEN_GEN >= 8
375 sbe.ForceVertexURBEntryReadOffset = true;
376 sbe.ForceVertexURBEntryReadLength = true;
377 #endif
378
379 uint32_t *dw = anv_batch_emit_dwords(&pipeline->batch,
380 GENX(3DSTATE_SBE_length));
381 GENX(3DSTATE_SBE_pack)(&pipeline->batch, dw, &sbe);
382
383 #if GEN_GEN >= 8
384 dw = anv_batch_emit_dwords(&pipeline->batch, GENX(3DSTATE_SBE_SWIZ_length));
385 GENX(3DSTATE_SBE_SWIZ_pack)(&pipeline->batch, dw, &swiz);
386 #endif
387 }
388
389 static const uint32_t vk_to_gen_cullmode[] = {
390 [VK_CULL_MODE_NONE] = CULLMODE_NONE,
391 [VK_CULL_MODE_FRONT_BIT] = CULLMODE_FRONT,
392 [VK_CULL_MODE_BACK_BIT] = CULLMODE_BACK,
393 [VK_CULL_MODE_FRONT_AND_BACK] = CULLMODE_BOTH
394 };
395
396 static const uint32_t vk_to_gen_fillmode[] = {
397 [VK_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
398 [VK_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
399 [VK_POLYGON_MODE_POINT] = FILL_MODE_POINT,
400 };
401
402 static const uint32_t vk_to_gen_front_face[] = {
403 [VK_FRONT_FACE_COUNTER_CLOCKWISE] = 1,
404 [VK_FRONT_FACE_CLOCKWISE] = 0
405 };
406
407 static void
408 emit_rs_state(struct anv_pipeline *pipeline,
409 const VkPipelineRasterizationStateCreateInfo *rs_info,
410 const VkPipelineMultisampleStateCreateInfo *ms_info,
411 const struct anv_render_pass *pass,
412 const struct anv_subpass *subpass)
413 {
414 struct GENX(3DSTATE_SF) sf = {
415 GENX(3DSTATE_SF_header),
416 };
417
418 sf.ViewportTransformEnable = true;
419 sf.StatisticsEnable = true;
420 sf.TriangleStripListProvokingVertexSelect = 0;
421 sf.LineStripListProvokingVertexSelect = 0;
422 sf.TriangleFanProvokingVertexSelect = 1;
423 sf.PointWidthSource = Vertex;
424 sf.PointWidth = 1.0;
425
426 #if GEN_GEN >= 8
427 struct GENX(3DSTATE_RASTER) raster = {
428 GENX(3DSTATE_RASTER_header),
429 };
430 #else
431 # define raster sf
432 #endif
433
434 /* For details on 3DSTATE_RASTER multisample state, see the BSpec table
435 * "Multisample Modes State".
436 */
437 #if GEN_GEN >= 8
438 raster.DXMultisampleRasterizationEnable = true;
439 raster.ForcedSampleCount = FSC_NUMRASTSAMPLES_0;
440 raster.ForceMultisampling = false;
441 #else
442 raster.MultisampleRasterizationMode =
443 (ms_info && ms_info->rasterizationSamples > 1) ?
444 MSRASTMODE_ON_PATTERN : MSRASTMODE_OFF_PIXEL;
445 #endif
446
447 raster.FrontWinding = vk_to_gen_front_face[rs_info->frontFace];
448 raster.CullMode = vk_to_gen_cullmode[rs_info->cullMode];
449 raster.FrontFaceFillMode = vk_to_gen_fillmode[rs_info->polygonMode];
450 raster.BackFaceFillMode = vk_to_gen_fillmode[rs_info->polygonMode];
451 raster.ScissorRectangleEnable = true;
452
453 #if GEN_GEN >= 9
454 /* GEN9+ splits ViewportZClipTestEnable into near and far enable bits */
455 raster.ViewportZFarClipTestEnable = !pipeline->depth_clamp_enable;
456 raster.ViewportZNearClipTestEnable = !pipeline->depth_clamp_enable;
457 #elif GEN_GEN >= 8
458 raster.ViewportZClipTestEnable = !pipeline->depth_clamp_enable;
459 #endif
460
461 raster.GlobalDepthOffsetEnableSolid = rs_info->depthBiasEnable;
462 raster.GlobalDepthOffsetEnableWireframe = rs_info->depthBiasEnable;
463 raster.GlobalDepthOffsetEnablePoint = rs_info->depthBiasEnable;
464
465 #if GEN_GEN == 7
466 /* Gen7 requires that we provide the depth format in 3DSTATE_SF so that it
467 * can get the depth offsets correct.
468 */
469 if (subpass->depth_stencil_attachment < pass->attachment_count) {
470 VkFormat vk_format =
471 pass->attachments[subpass->depth_stencil_attachment].format;
472 assert(vk_format_is_depth_or_stencil(vk_format));
473 if (vk_format_aspects(vk_format) & VK_IMAGE_ASPECT_DEPTH_BIT) {
474 enum isl_format isl_format =
475 anv_get_isl_format(&pipeline->device->info, vk_format,
476 VK_IMAGE_ASPECT_DEPTH_BIT,
477 VK_IMAGE_TILING_OPTIMAL);
478 sf.DepthBufferSurfaceFormat =
479 isl_format_get_depth_format(isl_format, false);
480 }
481 }
482 #endif
483
484 #if GEN_GEN >= 8
485 GENX(3DSTATE_SF_pack)(NULL, pipeline->gen8.sf, &sf);
486 GENX(3DSTATE_RASTER_pack)(NULL, pipeline->gen8.raster, &raster);
487 #else
488 # undef raster
489 GENX(3DSTATE_SF_pack)(NULL, &pipeline->gen7.sf, &sf);
490 #endif
491 }
492
493 static void
494 emit_ms_state(struct anv_pipeline *pipeline,
495 const VkPipelineMultisampleStateCreateInfo *info)
496 {
497 uint32_t samples = 1;
498 uint32_t log2_samples = 0;
499
500 /* From the Vulkan 1.0 spec:
501 * If pSampleMask is NULL, it is treated as if the mask has all bits
502 * enabled, i.e. no coverage is removed from fragments.
503 *
504 * 3DSTATE_SAMPLE_MASK.SampleMask is 16 bits.
505 */
506 #if GEN_GEN >= 8
507 uint32_t sample_mask = 0xffff;
508 #else
509 uint32_t sample_mask = 0xff;
510 #endif
511
512 if (info) {
513 samples = info->rasterizationSamples;
514 log2_samples = __builtin_ffs(samples) - 1;
515 }
516
517 if (info && info->pSampleMask)
518 sample_mask &= info->pSampleMask[0];
519
520 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_MULTISAMPLE), ms) {
521 ms.NumberofMultisamples = log2_samples;
522
523 #if GEN_GEN >= 8
524 /* The PRM says that this bit is valid only for DX9:
525 *
526 * SW can choose to set this bit only for DX9 API. DX10/OGL API's
527 * should not have any effect by setting or not setting this bit.
528 */
529 ms.PixelPositionOffsetEnable = false;
530 ms.PixelLocation = CENTER;
531 #else
532 ms.PixelLocation = PIXLOC_CENTER;
533
534 switch (samples) {
535 case 1:
536 GEN_SAMPLE_POS_1X(ms.Sample);
537 break;
538 case 2:
539 GEN_SAMPLE_POS_2X(ms.Sample);
540 break;
541 case 4:
542 GEN_SAMPLE_POS_4X(ms.Sample);
543 break;
544 case 8:
545 GEN_SAMPLE_POS_8X(ms.Sample);
546 break;
547 default:
548 break;
549 }
550 #endif
551 }
552
553 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SAMPLE_MASK), sm) {
554 sm.SampleMask = sample_mask;
555 }
556 }
557
558 static const uint32_t vk_to_gen_logic_op[] = {
559 [VK_LOGIC_OP_COPY] = LOGICOP_COPY,
560 [VK_LOGIC_OP_CLEAR] = LOGICOP_CLEAR,
561 [VK_LOGIC_OP_AND] = LOGICOP_AND,
562 [VK_LOGIC_OP_AND_REVERSE] = LOGICOP_AND_REVERSE,
563 [VK_LOGIC_OP_AND_INVERTED] = LOGICOP_AND_INVERTED,
564 [VK_LOGIC_OP_NO_OP] = LOGICOP_NOOP,
565 [VK_LOGIC_OP_XOR] = LOGICOP_XOR,
566 [VK_LOGIC_OP_OR] = LOGICOP_OR,
567 [VK_LOGIC_OP_NOR] = LOGICOP_NOR,
568 [VK_LOGIC_OP_EQUIVALENT] = LOGICOP_EQUIV,
569 [VK_LOGIC_OP_INVERT] = LOGICOP_INVERT,
570 [VK_LOGIC_OP_OR_REVERSE] = LOGICOP_OR_REVERSE,
571 [VK_LOGIC_OP_COPY_INVERTED] = LOGICOP_COPY_INVERTED,
572 [VK_LOGIC_OP_OR_INVERTED] = LOGICOP_OR_INVERTED,
573 [VK_LOGIC_OP_NAND] = LOGICOP_NAND,
574 [VK_LOGIC_OP_SET] = LOGICOP_SET,
575 };
576
577 static const uint32_t vk_to_gen_blend[] = {
578 [VK_BLEND_FACTOR_ZERO] = BLENDFACTOR_ZERO,
579 [VK_BLEND_FACTOR_ONE] = BLENDFACTOR_ONE,
580 [VK_BLEND_FACTOR_SRC_COLOR] = BLENDFACTOR_SRC_COLOR,
581 [VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR] = BLENDFACTOR_INV_SRC_COLOR,
582 [VK_BLEND_FACTOR_DST_COLOR] = BLENDFACTOR_DST_COLOR,
583 [VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR] = BLENDFACTOR_INV_DST_COLOR,
584 [VK_BLEND_FACTOR_SRC_ALPHA] = BLENDFACTOR_SRC_ALPHA,
585 [VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA] = BLENDFACTOR_INV_SRC_ALPHA,
586 [VK_BLEND_FACTOR_DST_ALPHA] = BLENDFACTOR_DST_ALPHA,
587 [VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA] = BLENDFACTOR_INV_DST_ALPHA,
588 [VK_BLEND_FACTOR_CONSTANT_COLOR] = BLENDFACTOR_CONST_COLOR,
589 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR]= BLENDFACTOR_INV_CONST_COLOR,
590 [VK_BLEND_FACTOR_CONSTANT_ALPHA] = BLENDFACTOR_CONST_ALPHA,
591 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA]= BLENDFACTOR_INV_CONST_ALPHA,
592 [VK_BLEND_FACTOR_SRC_ALPHA_SATURATE] = BLENDFACTOR_SRC_ALPHA_SATURATE,
593 [VK_BLEND_FACTOR_SRC1_COLOR] = BLENDFACTOR_SRC1_COLOR,
594 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR] = BLENDFACTOR_INV_SRC1_COLOR,
595 [VK_BLEND_FACTOR_SRC1_ALPHA] = BLENDFACTOR_SRC1_ALPHA,
596 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA] = BLENDFACTOR_INV_SRC1_ALPHA,
597 };
598
599 static const uint32_t vk_to_gen_blend_op[] = {
600 [VK_BLEND_OP_ADD] = BLENDFUNCTION_ADD,
601 [VK_BLEND_OP_SUBTRACT] = BLENDFUNCTION_SUBTRACT,
602 [VK_BLEND_OP_REVERSE_SUBTRACT] = BLENDFUNCTION_REVERSE_SUBTRACT,
603 [VK_BLEND_OP_MIN] = BLENDFUNCTION_MIN,
604 [VK_BLEND_OP_MAX] = BLENDFUNCTION_MAX,
605 };
606
607 static const uint32_t vk_to_gen_compare_op[] = {
608 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
609 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
610 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
611 [VK_COMPARE_OP_LESS_OR_EQUAL] = PREFILTEROPLEQUAL,
612 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
613 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
614 [VK_COMPARE_OP_GREATER_OR_EQUAL] = PREFILTEROPGEQUAL,
615 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
616 };
617
618 static const uint32_t vk_to_gen_stencil_op[] = {
619 [VK_STENCIL_OP_KEEP] = STENCILOP_KEEP,
620 [VK_STENCIL_OP_ZERO] = STENCILOP_ZERO,
621 [VK_STENCIL_OP_REPLACE] = STENCILOP_REPLACE,
622 [VK_STENCIL_OP_INCREMENT_AND_CLAMP] = STENCILOP_INCRSAT,
623 [VK_STENCIL_OP_DECREMENT_AND_CLAMP] = STENCILOP_DECRSAT,
624 [VK_STENCIL_OP_INVERT] = STENCILOP_INVERT,
625 [VK_STENCIL_OP_INCREMENT_AND_WRAP] = STENCILOP_INCR,
626 [VK_STENCIL_OP_DECREMENT_AND_WRAP] = STENCILOP_DECR,
627 };
628
629 static void
630 emit_ds_state(struct anv_pipeline *pipeline,
631 const VkPipelineDepthStencilStateCreateInfo *info,
632 const struct anv_render_pass *pass,
633 const struct anv_subpass *subpass)
634 {
635 #if GEN_GEN == 7
636 # define depth_stencil_dw pipeline->gen7.depth_stencil_state
637 #elif GEN_GEN == 8
638 # define depth_stencil_dw pipeline->gen8.wm_depth_stencil
639 #else
640 # define depth_stencil_dw pipeline->gen9.wm_depth_stencil
641 #endif
642
643 if (info == NULL) {
644 /* We're going to OR this together with the dynamic state. We need
645 * to make sure it's initialized to something useful.
646 */
647 memset(depth_stencil_dw, 0, sizeof(depth_stencil_dw));
648 return;
649 }
650
651 /* VkBool32 depthBoundsTestEnable; // optional (depth_bounds_test) */
652
653 #if GEN_GEN <= 7
654 struct GENX(DEPTH_STENCIL_STATE) depth_stencil = {
655 #else
656 struct GENX(3DSTATE_WM_DEPTH_STENCIL) depth_stencil = {
657 #endif
658 .DepthTestEnable = info->depthTestEnable,
659 .DepthBufferWriteEnable = info->depthWriteEnable,
660 .DepthTestFunction = vk_to_gen_compare_op[info->depthCompareOp],
661 .DoubleSidedStencilEnable = true,
662
663 .StencilTestEnable = info->stencilTestEnable,
664 .StencilBufferWriteEnable = info->stencilTestEnable,
665 .StencilFailOp = vk_to_gen_stencil_op[info->front.failOp],
666 .StencilPassDepthPassOp = vk_to_gen_stencil_op[info->front.passOp],
667 .StencilPassDepthFailOp = vk_to_gen_stencil_op[info->front.depthFailOp],
668 .StencilTestFunction = vk_to_gen_compare_op[info->front.compareOp],
669 .BackfaceStencilFailOp = vk_to_gen_stencil_op[info->back.failOp],
670 .BackfaceStencilPassDepthPassOp = vk_to_gen_stencil_op[info->back.passOp],
671 .BackfaceStencilPassDepthFailOp =vk_to_gen_stencil_op[info->back.depthFailOp],
672 .BackfaceStencilTestFunction = vk_to_gen_compare_op[info->back.compareOp],
673 };
674
675 VkImageAspectFlags aspects = 0;
676 if (subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED) {
677 VkFormat depth_stencil_format =
678 pass->attachments[subpass->depth_stencil_attachment].format;
679 aspects = vk_format_aspects(depth_stencil_format);
680 }
681
682 /* The Vulkan spec requires that if either depth or stencil is not present,
683 * the pipeline is to act as if the test silently passes.
684 */
685 if (!(aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) {
686 depth_stencil.DepthBufferWriteEnable = false;
687 depth_stencil.DepthTestFunction = PREFILTEROPALWAYS;
688 }
689
690 if (!(aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) {
691 depth_stencil.StencilBufferWriteEnable = false;
692 depth_stencil.StencilTestFunction = PREFILTEROPALWAYS;
693 depth_stencil.BackfaceStencilTestFunction = PREFILTEROPALWAYS;
694 }
695
696 /* From the Broadwell PRM:
697 *
698 * "If Depth_Test_Enable = 1 AND Depth_Test_func = EQUAL, the
699 * Depth_Write_Enable must be set to 0."
700 */
701 if (info->depthTestEnable && info->depthCompareOp == VK_COMPARE_OP_EQUAL)
702 depth_stencil.DepthBufferWriteEnable = false;
703
704 #if GEN_GEN <= 7
705 GENX(DEPTH_STENCIL_STATE_pack)(NULL, depth_stencil_dw, &depth_stencil);
706 #else
707 GENX(3DSTATE_WM_DEPTH_STENCIL_pack)(NULL, depth_stencil_dw, &depth_stencil);
708 #endif
709 }
710
711 static void
712 emit_cb_state(struct anv_pipeline *pipeline,
713 const VkPipelineColorBlendStateCreateInfo *info,
714 const VkPipelineMultisampleStateCreateInfo *ms_info)
715 {
716 struct anv_device *device = pipeline->device;
717
718 const uint32_t num_dwords = GENX(BLEND_STATE_length);
719 pipeline->blend_state =
720 anv_state_pool_alloc(&device->dynamic_state_pool, num_dwords * 4, 64);
721
722 struct GENX(BLEND_STATE) blend_state = {
723 #if GEN_GEN >= 8
724 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
725 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
726 #else
727 /* Make sure it gets zeroed */
728 .Entry = { { 0, }, },
729 #endif
730 };
731
732 /* Default everything to disabled */
733 for (uint32_t i = 0; i < 8; i++) {
734 blend_state.Entry[i].WriteDisableAlpha = true;
735 blend_state.Entry[i].WriteDisableRed = true;
736 blend_state.Entry[i].WriteDisableGreen = true;
737 blend_state.Entry[i].WriteDisableBlue = true;
738 }
739
740 uint32_t surface_count = 0;
741 struct anv_pipeline_bind_map *map;
742 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
743 map = &pipeline->shaders[MESA_SHADER_FRAGMENT]->bind_map;
744 surface_count = map->surface_count;
745 }
746
747 bool has_writeable_rt = false;
748 for (unsigned i = 0; i < surface_count; i++) {
749 struct anv_pipeline_binding *binding = &map->surface_to_descriptor[i];
750
751 /* All color attachments are at the beginning of the binding table */
752 if (binding->set != ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS)
753 break;
754
755 /* We can have at most 8 attachments */
756 assert(i < 8);
757
758 if (binding->index >= info->attachmentCount)
759 continue;
760
761 assert(binding->binding == 0);
762 const VkPipelineColorBlendAttachmentState *a =
763 &info->pAttachments[binding->index];
764
765 blend_state.Entry[i] = (struct GENX(BLEND_STATE_ENTRY)) {
766 #if GEN_GEN < 8
767 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
768 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
769 #endif
770 .LogicOpEnable = info->logicOpEnable,
771 .LogicOpFunction = vk_to_gen_logic_op[info->logicOp],
772 .ColorBufferBlendEnable = a->blendEnable,
773 .ColorClampRange = COLORCLAMP_RTFORMAT,
774 .PreBlendColorClampEnable = true,
775 .PostBlendColorClampEnable = true,
776 .SourceBlendFactor = vk_to_gen_blend[a->srcColorBlendFactor],
777 .DestinationBlendFactor = vk_to_gen_blend[a->dstColorBlendFactor],
778 .ColorBlendFunction = vk_to_gen_blend_op[a->colorBlendOp],
779 .SourceAlphaBlendFactor = vk_to_gen_blend[a->srcAlphaBlendFactor],
780 .DestinationAlphaBlendFactor = vk_to_gen_blend[a->dstAlphaBlendFactor],
781 .AlphaBlendFunction = vk_to_gen_blend_op[a->alphaBlendOp],
782 .WriteDisableAlpha = !(a->colorWriteMask & VK_COLOR_COMPONENT_A_BIT),
783 .WriteDisableRed = !(a->colorWriteMask & VK_COLOR_COMPONENT_R_BIT),
784 .WriteDisableGreen = !(a->colorWriteMask & VK_COLOR_COMPONENT_G_BIT),
785 .WriteDisableBlue = !(a->colorWriteMask & VK_COLOR_COMPONENT_B_BIT),
786 };
787
788 if (a->srcColorBlendFactor != a->srcAlphaBlendFactor ||
789 a->dstColorBlendFactor != a->dstAlphaBlendFactor ||
790 a->colorBlendOp != a->alphaBlendOp) {
791 #if GEN_GEN >= 8
792 blend_state.IndependentAlphaBlendEnable = true;
793 #else
794 blend_state.Entry[i].IndependentAlphaBlendEnable = true;
795 #endif
796 }
797
798 if (a->colorWriteMask != 0)
799 has_writeable_rt = true;
800
801 /* Our hardware applies the blend factor prior to the blend function
802 * regardless of what function is used. Technically, this means the
803 * hardware can do MORE than GL or Vulkan specify. However, it also
804 * means that, for MIN and MAX, we have to stomp the blend factor to
805 * ONE to make it a no-op.
806 */
807 if (a->colorBlendOp == VK_BLEND_OP_MIN ||
808 a->colorBlendOp == VK_BLEND_OP_MAX) {
809 blend_state.Entry[i].SourceBlendFactor = BLENDFACTOR_ONE;
810 blend_state.Entry[i].DestinationBlendFactor = BLENDFACTOR_ONE;
811 }
812 if (a->alphaBlendOp == VK_BLEND_OP_MIN ||
813 a->alphaBlendOp == VK_BLEND_OP_MAX) {
814 blend_state.Entry[i].SourceAlphaBlendFactor = BLENDFACTOR_ONE;
815 blend_state.Entry[i].DestinationAlphaBlendFactor = BLENDFACTOR_ONE;
816 }
817 }
818
819 #if GEN_GEN >= 8
820 struct GENX(BLEND_STATE_ENTRY) *bs0 = &blend_state.Entry[0];
821 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS_BLEND), blend) {
822 blend.AlphaToCoverageEnable = blend_state.AlphaToCoverageEnable;
823 blend.HasWriteableRT = has_writeable_rt;
824 blend.ColorBufferBlendEnable = bs0->ColorBufferBlendEnable;
825 blend.SourceAlphaBlendFactor = bs0->SourceAlphaBlendFactor;
826 blend.DestinationAlphaBlendFactor = bs0->DestinationAlphaBlendFactor;
827 blend.SourceBlendFactor = bs0->SourceBlendFactor;
828 blend.DestinationBlendFactor = bs0->DestinationBlendFactor;
829 blend.AlphaTestEnable = false;
830 blend.IndependentAlphaBlendEnable =
831 blend_state.IndependentAlphaBlendEnable;
832 }
833 #else
834 (void)has_writeable_rt;
835 #endif
836
837 GENX(BLEND_STATE_pack)(NULL, pipeline->blend_state.map, &blend_state);
838 if (!device->info.has_llc)
839 anv_state_clflush(pipeline->blend_state);
840
841 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_BLEND_STATE_POINTERS), bsp) {
842 bsp.BlendStatePointer = pipeline->blend_state.offset;
843 #if GEN_GEN >= 8
844 bsp.BlendStatePointerValid = true;
845 #endif
846 }
847 }
848
849 /**
850 * Get the brw_vue_prog_data for the last stage which outputs VUEs.
851 */
852 static inline struct brw_vue_prog_data *
853 get_last_vue_prog_data(struct anv_pipeline *pipeline)
854 {
855 for (int s = MESA_SHADER_GEOMETRY; s >= 0; s--) {
856 if (pipeline->shaders[s])
857 return (struct brw_vue_prog_data *) pipeline->shaders[s]->prog_data;
858 }
859 return NULL;
860 }
861
862 static void
863 emit_3dstate_clip(struct anv_pipeline *pipeline,
864 const VkPipelineViewportStateCreateInfo *vp_info,
865 const VkPipelineRasterizationStateCreateInfo *rs_info)
866 {
867 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
868 (void) wm_prog_data;
869 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_CLIP), clip) {
870 clip.ClipEnable = true;
871 clip.EarlyCullEnable = true;
872 clip.APIMode = APIMODE_D3D,
873 clip.ViewportXYClipTestEnable = true;
874
875 clip.ClipMode = CLIPMODE_NORMAL;
876
877 clip.TriangleStripListProvokingVertexSelect = 0;
878 clip.LineStripListProvokingVertexSelect = 0;
879 clip.TriangleFanProvokingVertexSelect = 1;
880
881 clip.MinimumPointWidth = 0.125;
882 clip.MaximumPointWidth = 255.875;
883 clip.MaximumVPIndex = (vp_info ? vp_info->viewportCount : 1) - 1;
884
885 #if GEN_GEN == 7
886 clip.FrontWinding = vk_to_gen_front_face[rs_info->frontFace];
887 clip.CullMode = vk_to_gen_cullmode[rs_info->cullMode];
888 clip.ViewportZClipTestEnable = !pipeline->depth_clamp_enable;
889 const struct brw_vue_prog_data *last = get_last_vue_prog_data(pipeline);
890 if (last) {
891 clip.UserClipDistanceClipTestEnableBitmask = last->clip_distance_mask;
892 clip.UserClipDistanceCullTestEnableBitmask = last->cull_distance_mask;
893 }
894 #else
895 clip.NonPerspectiveBarycentricEnable = wm_prog_data ?
896 (wm_prog_data->barycentric_interp_modes & 0x38) != 0 : 0;
897 #endif
898 }
899 }
900
901 static void
902 emit_3dstate_streamout(struct anv_pipeline *pipeline,
903 const VkPipelineRasterizationStateCreateInfo *rs_info)
904 {
905 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_STREAMOUT), so) {
906 so.RenderingDisable = rs_info->rasterizerDiscardEnable;
907 }
908 }
909
910 static inline uint32_t
911 get_sampler_count(const struct anv_shader_bin *bin)
912 {
913 return DIV_ROUND_UP(bin->bind_map.sampler_count, 4);
914 }
915
916 static inline uint32_t
917 get_binding_table_entry_count(const struct anv_shader_bin *bin)
918 {
919 return DIV_ROUND_UP(bin->bind_map.surface_count, 32);
920 }
921
922 static inline struct anv_address
923 get_scratch_address(struct anv_pipeline *pipeline,
924 gl_shader_stage stage,
925 const struct anv_shader_bin *bin)
926 {
927 return (struct anv_address) {
928 .bo = anv_scratch_pool_alloc(pipeline->device,
929 &pipeline->device->scratch_pool,
930 stage, bin->prog_data->total_scratch),
931 .offset = 0,
932 };
933 }
934
935 static inline uint32_t
936 get_scratch_space(const struct anv_shader_bin *bin)
937 {
938 return ffs(bin->prog_data->total_scratch / 2048);
939 }
940
941 static inline uint32_t
942 get_urb_output_offset()
943 {
944 /* Skip the VUE header and position slots */
945 return 1;
946 }
947
948 static inline uint32_t
949 get_urb_output_length(const struct anv_shader_bin *bin)
950 {
951 const struct brw_vue_prog_data *prog_data =
952 (const struct brw_vue_prog_data *)bin->prog_data;
953
954 return (prog_data->vue_map.num_slots + 1) / 2 - get_urb_output_offset();
955 }
956
957 static void
958 emit_3dstate_vs(struct anv_pipeline *pipeline)
959 {
960 const struct gen_device_info *devinfo = &pipeline->device->info;
961 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
962 const struct anv_shader_bin *vs_bin =
963 pipeline->shaders[MESA_SHADER_VERTEX];
964
965 assert(anv_pipeline_has_stage(pipeline, MESA_SHADER_VERTEX));
966
967 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS), vs) {
968 vs.FunctionEnable = true;
969 vs.StatisticsEnable = true;
970 vs.KernelStartPointer = vs_bin->kernel.offset;
971 #if GEN_GEN >= 8
972 vs.SIMD8DispatchEnable =
973 vs_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8;
974 #endif
975
976 assert(!vs_prog_data->base.base.use_alt_mode);
977 vs.SingleVertexDispatch = false;
978 vs.VectorMaskEnable = false;
979 vs.SamplerCount = get_sampler_count(vs_bin);
980 vs.BindingTableEntryCount = get_binding_table_entry_count(vs_bin);
981 vs.FloatingPointMode = IEEE754;
982 vs.IllegalOpcodeExceptionEnable = false;
983 vs.SoftwareExceptionEnable = false;
984 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
985 vs.VertexCacheDisable = false;
986
987 vs.VertexURBEntryReadLength = vs_prog_data->base.urb_read_length;
988 vs.VertexURBEntryReadOffset = 0;
989 vs.DispatchGRFStartRegisterForURBData =
990 vs_prog_data->base.base.dispatch_grf_start_reg;
991
992 #if GEN_GEN >= 8
993 vs.VertexURBEntryOutputReadOffset = get_urb_output_offset();
994 vs.VertexURBEntryOutputLength = get_urb_output_length(vs_bin);
995
996 vs.UserClipDistanceClipTestEnableBitmask =
997 vs_prog_data->base.clip_distance_mask;
998 vs.UserClipDistanceCullTestEnableBitmask =
999 vs_prog_data->base.cull_distance_mask;
1000 #endif
1001
1002 vs.PerThreadScratchSpace = get_scratch_space(vs_bin);
1003 vs.ScratchSpaceBasePointer =
1004 get_scratch_address(pipeline, MESA_SHADER_VERTEX, vs_bin);
1005 }
1006 }
1007
1008 static void
1009 emit_3dstate_hs_te_ds(struct anv_pipeline *pipeline)
1010 {
1011 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL)) {
1012 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_HS), hs);
1013 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_TE), te);
1014 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_DS), ds);
1015 return;
1016 }
1017
1018 const struct gen_device_info *devinfo = &pipeline->device->info;
1019 const struct anv_shader_bin *tcs_bin =
1020 pipeline->shaders[MESA_SHADER_TESS_CTRL];
1021 const struct anv_shader_bin *tes_bin =
1022 pipeline->shaders[MESA_SHADER_TESS_EVAL];
1023
1024 const struct brw_tcs_prog_data *tcs_prog_data = get_tcs_prog_data(pipeline);
1025 const struct brw_tes_prog_data *tes_prog_data = get_tes_prog_data(pipeline);
1026
1027 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_HS), hs) {
1028 hs.FunctionEnable = true;
1029 hs.StatisticsEnable = true;
1030 hs.KernelStartPointer = tcs_bin->kernel.offset;
1031
1032 hs.SamplerCount = get_sampler_count(tcs_bin);
1033 hs.BindingTableEntryCount = get_binding_table_entry_count(tcs_bin);
1034 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
1035 hs.IncludeVertexHandles = true;
1036 hs.InstanceCount = tcs_prog_data->instances - 1;
1037
1038 hs.VertexURBEntryReadLength = 0;
1039 hs.VertexURBEntryReadOffset = 0;
1040 hs.DispatchGRFStartRegisterForURBData =
1041 tcs_prog_data->base.base.dispatch_grf_start_reg;
1042
1043 hs.PerThreadScratchSpace = get_scratch_space(tcs_bin);
1044 hs.ScratchSpaceBasePointer =
1045 get_scratch_address(pipeline, MESA_SHADER_TESS_CTRL, tcs_bin);
1046 }
1047
1048 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_TE), te) {
1049 te.Partitioning = tes_prog_data->partitioning;
1050 te.OutputTopology = tes_prog_data->output_topology;
1051 te.TEDomain = tes_prog_data->domain;
1052 te.TEEnable = true;
1053 te.MaximumTessellationFactorOdd = 63.0;
1054 te.MaximumTessellationFactorNotOdd = 64.0;
1055 }
1056
1057 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_DS), ds) {
1058 ds.FunctionEnable = true;
1059 ds.StatisticsEnable = true;
1060 ds.KernelStartPointer = tes_bin->kernel.offset;
1061
1062 ds.SamplerCount = get_sampler_count(tes_bin);
1063 ds.BindingTableEntryCount = get_binding_table_entry_count(tes_bin);
1064 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
1065
1066 ds.ComputeWCoordinateEnable =
1067 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
1068
1069 ds.PatchURBEntryReadLength = tes_prog_data->base.urb_read_length;
1070 ds.PatchURBEntryReadOffset = 0;
1071 ds.DispatchGRFStartRegisterForURBData =
1072 tes_prog_data->base.base.dispatch_grf_start_reg;
1073
1074 #if GEN_GEN >= 8
1075 ds.VertexURBEntryOutputReadOffset = 1;
1076 ds.VertexURBEntryOutputLength =
1077 (tes_prog_data->base.vue_map.num_slots + 1) / 2 - 1;
1078
1079 ds.DispatchMode =
1080 tes_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8 ?
1081 DISPATCH_MODE_SIMD8_SINGLE_PATCH :
1082 DISPATCH_MODE_SIMD4X2;
1083
1084 ds.UserClipDistanceClipTestEnableBitmask =
1085 tes_prog_data->base.clip_distance_mask;
1086 ds.UserClipDistanceCullTestEnableBitmask =
1087 tes_prog_data->base.cull_distance_mask;
1088 #endif
1089
1090 ds.PerThreadScratchSpace = get_scratch_space(tes_bin);
1091 ds.ScratchSpaceBasePointer =
1092 get_scratch_address(pipeline, MESA_SHADER_TESS_EVAL, tes_bin);
1093 }
1094 }
1095
1096 static void
1097 emit_3dstate_gs(struct anv_pipeline *pipeline)
1098 {
1099 const struct gen_device_info *devinfo = &pipeline->device->info;
1100 const struct anv_shader_bin *gs_bin =
1101 pipeline->shaders[MESA_SHADER_GEOMETRY];
1102
1103 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_GEOMETRY)) {
1104 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), gs);
1105 return;
1106 }
1107
1108 const struct brw_gs_prog_data *gs_prog_data = get_gs_prog_data(pipeline);
1109
1110 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), gs) {
1111 gs.FunctionEnable = true;
1112 gs.StatisticsEnable = true;
1113 gs.KernelStartPointer = gs_bin->kernel.offset;
1114 gs.DispatchMode = gs_prog_data->base.dispatch_mode;
1115
1116 gs.SingleProgramFlow = false;
1117 gs.VectorMaskEnable = false;
1118 gs.SamplerCount = get_sampler_count(gs_bin);
1119 gs.BindingTableEntryCount = get_binding_table_entry_count(gs_bin);
1120 gs.IncludeVertexHandles = gs_prog_data->base.include_vue_handles;
1121 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
1122
1123 if (GEN_GEN == 8) {
1124 /* Broadwell is weird. It needs us to divide by 2. */
1125 gs.MaximumNumberofThreads = devinfo->max_gs_threads / 2 - 1;
1126 } else {
1127 gs.MaximumNumberofThreads = devinfo->max_gs_threads - 1;
1128 }
1129
1130 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
1131 gs.OutputTopology = gs_prog_data->output_topology;
1132 gs.VertexURBEntryReadLength = gs_prog_data->base.urb_read_length;
1133 gs.ControlDataFormat = gs_prog_data->control_data_format;
1134 gs.ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords;
1135 gs.InstanceControl = MAX2(gs_prog_data->invocations, 1) - 1;
1136 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1137 gs.ReorderMode = TRAILING;
1138 #else
1139 gs.ReorderEnable = true;
1140 #endif
1141
1142 #if GEN_GEN >= 8
1143 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
1144 gs.StaticOutput = gs_prog_data->static_vertex_count >= 0;
1145 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count >= 0 ?
1146 gs_prog_data->static_vertex_count : 0;
1147 #endif
1148
1149 gs.VertexURBEntryReadOffset = 0;
1150 gs.VertexURBEntryReadLength = gs_prog_data->base.urb_read_length;
1151 gs.DispatchGRFStartRegisterForURBData =
1152 gs_prog_data->base.base.dispatch_grf_start_reg;
1153
1154 #if GEN_GEN >= 8
1155 gs.VertexURBEntryOutputReadOffset = get_urb_output_offset();
1156 gs.VertexURBEntryOutputLength = get_urb_output_length(gs_bin);
1157
1158 gs.UserClipDistanceClipTestEnableBitmask =
1159 gs_prog_data->base.clip_distance_mask;
1160 gs.UserClipDistanceCullTestEnableBitmask =
1161 gs_prog_data->base.cull_distance_mask;
1162 #endif
1163
1164 gs.PerThreadScratchSpace = get_scratch_space(gs_bin);
1165 gs.ScratchSpaceBasePointer =
1166 get_scratch_address(pipeline, MESA_SHADER_GEOMETRY, gs_bin);
1167 }
1168 }
1169
1170 static void
1171 emit_3dstate_wm(struct anv_pipeline *pipeline, struct anv_subpass *subpass,
1172 const VkPipelineMultisampleStateCreateInfo *multisample)
1173 {
1174 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1175
1176 MAYBE_UNUSED uint32_t samples =
1177 multisample ? multisample->rasterizationSamples : 1;
1178
1179 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM), wm) {
1180 wm.StatisticsEnable = true;
1181 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
1182 wm.LineAntialiasingRegionWidth = _10pixels;
1183 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1184
1185 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1186 if (wm_prog_data->early_fragment_tests) {
1187 wm.EarlyDepthStencilControl = EDSC_PREPS;
1188 } else if (wm_prog_data->has_side_effects) {
1189 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
1190 } else {
1191 wm.EarlyDepthStencilControl = EDSC_NORMAL;
1192 }
1193
1194 wm.BarycentricInterpolationMode =
1195 wm_prog_data->barycentric_interp_modes;
1196
1197 #if GEN_GEN < 8
1198 /* FIXME: This needs a lot more work, cf gen7 upload_wm_state(). */
1199 wm.ThreadDispatchEnable = true;
1200
1201 wm.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
1202 wm.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
1203 wm.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
1204 wm.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
1205
1206 /* If the subpass has a depth or stencil self-dependency, then we
1207 * need to force the hardware to do the depth/stencil write *after*
1208 * fragment shader execution. Otherwise, the writes may hit memory
1209 * before we get around to fetching from the input attachment and we
1210 * may get the depth or stencil value from the current draw rather
1211 * than the previous one.
1212 */
1213 wm.PixelShaderKillsPixel = subpass->has_ds_self_dep ||
1214 wm_prog_data->uses_kill;
1215
1216 if (samples > 1) {
1217 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
1218 if (wm_prog_data->persample_dispatch) {
1219 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1220 } else {
1221 wm.MultisampleDispatchMode = MSDISPMODE_PERPIXEL;
1222 }
1223 } else {
1224 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
1225 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1226 }
1227 #endif
1228 }
1229 }
1230 }
1231
1232 static inline bool
1233 is_dual_src_blend_factor(VkBlendFactor factor)
1234 {
1235 return factor == VK_BLEND_FACTOR_SRC1_COLOR ||
1236 factor == VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR ||
1237 factor == VK_BLEND_FACTOR_SRC1_ALPHA ||
1238 factor == VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA;
1239 }
1240
1241 static void
1242 emit_3dstate_ps(struct anv_pipeline *pipeline,
1243 const VkPipelineColorBlendStateCreateInfo *blend)
1244 {
1245 MAYBE_UNUSED const struct gen_device_info *devinfo = &pipeline->device->info;
1246 const struct anv_shader_bin *fs_bin =
1247 pipeline->shaders[MESA_SHADER_FRAGMENT];
1248
1249 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1250 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS), ps) {
1251 #if GEN_GEN == 7
1252 /* Even if no fragments are ever dispatched, gen7 hardware hangs if
1253 * we don't at least set the maximum number of threads.
1254 */
1255 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
1256 #endif
1257 }
1258 return;
1259 }
1260
1261 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1262
1263 #if GEN_GEN < 8
1264 /* The hardware wedges if you have this bit set but don't turn on any dual
1265 * source blend factors.
1266 */
1267 bool dual_src_blend = false;
1268 if (wm_prog_data->dual_src_blend) {
1269 for (uint32_t i = 0; i < blend->attachmentCount; i++) {
1270 const VkPipelineColorBlendAttachmentState *bstate =
1271 &blend->pAttachments[i];
1272
1273 if (bstate->blendEnable &&
1274 (is_dual_src_blend_factor(bstate->srcColorBlendFactor) ||
1275 is_dual_src_blend_factor(bstate->dstColorBlendFactor) ||
1276 is_dual_src_blend_factor(bstate->srcAlphaBlendFactor) ||
1277 is_dual_src_blend_factor(bstate->dstAlphaBlendFactor))) {
1278 dual_src_blend = true;
1279 break;
1280 }
1281 }
1282 }
1283 #endif
1284
1285 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS), ps) {
1286 ps.KernelStartPointer0 = fs_bin->kernel.offset;
1287 ps.KernelStartPointer1 = 0;
1288 ps.KernelStartPointer2 = fs_bin->kernel.offset +
1289 wm_prog_data->prog_offset_2;
1290 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
1291 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
1292 ps._32PixelDispatchEnable = false;
1293
1294 ps.SingleProgramFlow = false;
1295 ps.VectorMaskEnable = true;
1296 ps.SamplerCount = get_sampler_count(fs_bin);
1297 ps.BindingTableEntryCount = get_binding_table_entry_count(fs_bin);
1298 ps.PushConstantEnable = wm_prog_data->base.nr_params > 0;
1299 ps.PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
1300 POSOFFSET_SAMPLE: POSOFFSET_NONE;
1301 #if GEN_GEN < 8
1302 ps.AttributeEnable = wm_prog_data->num_varying_inputs > 0;
1303 ps.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
1304 ps.DualSourceBlendEnable = dual_src_blend;
1305 #endif
1306
1307 #if GEN_IS_HASWELL
1308 /* Haswell requires the sample mask to be set in this packet as well
1309 * as in 3DSTATE_SAMPLE_MASK; the values should match.
1310 */
1311 ps.SampleMask = 0xff;
1312 #endif
1313
1314 #if GEN_GEN >= 9
1315 ps.MaximumNumberofThreadsPerPSD = 64 - 1;
1316 #elif GEN_GEN >= 8
1317 ps.MaximumNumberofThreadsPerPSD = 64 - 2;
1318 #else
1319 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
1320 #endif
1321
1322 ps.DispatchGRFStartRegisterForConstantSetupData0 =
1323 wm_prog_data->base.dispatch_grf_start_reg;
1324 ps.DispatchGRFStartRegisterForConstantSetupData1 = 0;
1325 ps.DispatchGRFStartRegisterForConstantSetupData2 =
1326 wm_prog_data->dispatch_grf_start_reg_2;
1327
1328 ps.PerThreadScratchSpace = get_scratch_space(fs_bin);
1329 ps.ScratchSpaceBasePointer =
1330 get_scratch_address(pipeline, MESA_SHADER_FRAGMENT, fs_bin);
1331 }
1332 }
1333
1334 #if GEN_GEN >= 8
1335 static void
1336 emit_3dstate_ps_extra(struct anv_pipeline *pipeline,
1337 struct anv_subpass *subpass)
1338 {
1339 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
1340
1341 if (!anv_pipeline_has_stage(pipeline, MESA_SHADER_FRAGMENT)) {
1342 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS_EXTRA), ps);
1343 return;
1344 }
1345
1346 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS_EXTRA), ps) {
1347 ps.PixelShaderValid = true;
1348 ps.AttributeEnable = wm_prog_data->num_varying_inputs > 0;
1349 ps.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
1350 ps.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
1351 ps.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
1352 ps.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
1353 ps.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
1354
1355 /* If the subpass has a depth or stencil self-dependency, then we need
1356 * to force the hardware to do the depth/stencil write *after* fragment
1357 * shader execution. Otherwise, the writes may hit memory before we get
1358 * around to fetching from the input attachment and we may get the depth
1359 * or stencil value from the current draw rather than the previous one.
1360 */
1361 ps.PixelShaderKillsPixel = subpass->has_ds_self_dep ||
1362 wm_prog_data->uses_kill;
1363
1364 #if GEN_GEN >= 9
1365 ps.PixelShaderPullsBary = wm_prog_data->pulls_bary;
1366 ps.InputCoverageMaskState = wm_prog_data->uses_sample_mask ?
1367 ICMS_INNER_CONSERVATIVE : ICMS_NONE;
1368 #else
1369 ps.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
1370 #endif
1371 }
1372 }
1373
1374 static void
1375 emit_3dstate_vf_topology(struct anv_pipeline *pipeline)
1376 {
1377 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_TOPOLOGY), vft) {
1378 vft.PrimitiveTopologyType = pipeline->topology;
1379 }
1380 }
1381 #endif
1382
1383 static VkResult
1384 genX(graphics_pipeline_create)(
1385 VkDevice _device,
1386 struct anv_pipeline_cache * cache,
1387 const VkGraphicsPipelineCreateInfo* pCreateInfo,
1388 const VkAllocationCallbacks* pAllocator,
1389 VkPipeline* pPipeline)
1390 {
1391 ANV_FROM_HANDLE(anv_device, device, _device);
1392 ANV_FROM_HANDLE(anv_render_pass, pass, pCreateInfo->renderPass);
1393 struct anv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1394 struct anv_pipeline *pipeline;
1395 VkResult result;
1396
1397 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
1398
1399 pipeline = vk_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
1400 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1401 if (pipeline == NULL)
1402 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1403
1404 result = anv_pipeline_init(pipeline, device, cache,
1405 pCreateInfo, pAllocator);
1406 if (result != VK_SUCCESS) {
1407 vk_free2(&device->alloc, pAllocator, pipeline);
1408 return result;
1409 }
1410
1411 assert(pCreateInfo->pVertexInputState);
1412 emit_vertex_input(pipeline, pCreateInfo->pVertexInputState);
1413 assert(pCreateInfo->pRasterizationState);
1414 emit_rs_state(pipeline, pCreateInfo->pRasterizationState,
1415 pCreateInfo->pMultisampleState, pass, subpass);
1416 emit_ms_state(pipeline, pCreateInfo->pMultisampleState);
1417 emit_ds_state(pipeline, pCreateInfo->pDepthStencilState, pass, subpass);
1418 emit_cb_state(pipeline, pCreateInfo->pColorBlendState,
1419 pCreateInfo->pMultisampleState);
1420
1421 emit_urb_setup(pipeline);
1422
1423 emit_3dstate_clip(pipeline, pCreateInfo->pViewportState,
1424 pCreateInfo->pRasterizationState);
1425 emit_3dstate_streamout(pipeline, pCreateInfo->pRasterizationState);
1426
1427 #if 0
1428 /* From gen7_vs_state.c */
1429
1430 /**
1431 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
1432 * Geometry > Geometry Shader > State:
1433 *
1434 * "Note: Because of corruption in IVB:GT2, software needs to flush the
1435 * whole fixed function pipeline when the GS enable changes value in
1436 * the 3DSTATE_GS."
1437 *
1438 * The hardware architects have clarified that in this context "flush the
1439 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
1440 * Stall" bit set.
1441 */
1442 if (!brw->is_haswell && !brw->is_baytrail)
1443 gen7_emit_vs_workaround_flush(brw);
1444 #endif
1445
1446 emit_3dstate_vs(pipeline);
1447 emit_3dstate_hs_te_ds(pipeline);
1448 emit_3dstate_gs(pipeline);
1449 emit_3dstate_sbe(pipeline);
1450 emit_3dstate_wm(pipeline, subpass, pCreateInfo->pMultisampleState);
1451 emit_3dstate_ps(pipeline, pCreateInfo->pColorBlendState);
1452 #if GEN_GEN >= 8
1453 emit_3dstate_ps_extra(pipeline, subpass);
1454 emit_3dstate_vf_topology(pipeline);
1455 #endif
1456
1457 *pPipeline = anv_pipeline_to_handle(pipeline);
1458
1459 return VK_SUCCESS;
1460 }
1461
1462 static VkResult
1463 compute_pipeline_create(
1464 VkDevice _device,
1465 struct anv_pipeline_cache * cache,
1466 const VkComputePipelineCreateInfo* pCreateInfo,
1467 const VkAllocationCallbacks* pAllocator,
1468 VkPipeline* pPipeline)
1469 {
1470 ANV_FROM_HANDLE(anv_device, device, _device);
1471 const struct anv_physical_device *physical_device =
1472 &device->instance->physicalDevice;
1473 const struct gen_device_info *devinfo = &physical_device->info;
1474 struct anv_pipeline *pipeline;
1475 VkResult result;
1476
1477 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO);
1478
1479 pipeline = vk_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
1480 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1481 if (pipeline == NULL)
1482 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1483
1484 pipeline->device = device;
1485 pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
1486
1487 pipeline->blend_state.map = NULL;
1488
1489 result = anv_reloc_list_init(&pipeline->batch_relocs,
1490 pAllocator ? pAllocator : &device->alloc);
1491 if (result != VK_SUCCESS) {
1492 vk_free2(&device->alloc, pAllocator, pipeline);
1493 return result;
1494 }
1495 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
1496 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
1497 pipeline->batch.relocs = &pipeline->batch_relocs;
1498
1499 /* When we free the pipeline, we detect stages based on the NULL status
1500 * of various prog_data pointers. Make them NULL by default.
1501 */
1502 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
1503
1504 pipeline->active_stages = 0;
1505
1506 pipeline->needs_data_cache = false;
1507
1508 assert(pCreateInfo->stage.stage == VK_SHADER_STAGE_COMPUTE_BIT);
1509 ANV_FROM_HANDLE(anv_shader_module, module, pCreateInfo->stage.module);
1510 result = anv_pipeline_compile_cs(pipeline, cache, pCreateInfo, module,
1511 pCreateInfo->stage.pName,
1512 pCreateInfo->stage.pSpecializationInfo);
1513 if (result != VK_SUCCESS) {
1514 vk_free2(&device->alloc, pAllocator, pipeline);
1515 return result;
1516 }
1517
1518 const struct brw_cs_prog_data *cs_prog_data = get_cs_prog_data(pipeline);
1519
1520 anv_pipeline_setup_l3_config(pipeline, cs_prog_data->base.total_shared > 0);
1521
1522 uint32_t group_size = cs_prog_data->local_size[0] *
1523 cs_prog_data->local_size[1] * cs_prog_data->local_size[2];
1524 uint32_t remainder = group_size & (cs_prog_data->simd_size - 1);
1525
1526 if (remainder > 0)
1527 pipeline->cs_right_mask = ~0u >> (32 - remainder);
1528 else
1529 pipeline->cs_right_mask = ~0u >> (32 - cs_prog_data->simd_size);
1530
1531 const uint32_t vfe_curbe_allocation =
1532 ALIGN(cs_prog_data->push.per_thread.regs * cs_prog_data->threads +
1533 cs_prog_data->push.cross_thread.regs, 2);
1534
1535 const uint32_t subslices = MAX2(physical_device->subslice_total, 1);
1536
1537 const struct anv_shader_bin *cs_bin =
1538 pipeline->shaders[MESA_SHADER_COMPUTE];
1539
1540 anv_batch_emit(&pipeline->batch, GENX(MEDIA_VFE_STATE), vfe) {
1541 #if GEN_GEN > 7
1542 vfe.StackSize = 0;
1543 #else
1544 vfe.GPGPUMode = true;
1545 #endif
1546 vfe.MaximumNumberofThreads =
1547 devinfo->max_cs_threads * subslices - 1;
1548 vfe.NumberofURBEntries = GEN_GEN <= 7 ? 0 : 2;
1549 vfe.ResetGatewayTimer = true;
1550 #if GEN_GEN <= 8
1551 vfe.BypassGatewayControl = true;
1552 #endif
1553 vfe.URBEntryAllocationSize = GEN_GEN <= 7 ? 0 : 2;
1554 vfe.CURBEAllocationSize = vfe_curbe_allocation;
1555
1556 vfe.PerThreadScratchSpace = get_scratch_space(cs_bin);
1557 vfe.ScratchSpaceBasePointer =
1558 get_scratch_address(pipeline, MESA_SHADER_COMPUTE, cs_bin);
1559 }
1560
1561 struct GENX(INTERFACE_DESCRIPTOR_DATA) desc = {
1562 .KernelStartPointer = cs_bin->kernel.offset,
1563
1564 .SamplerCount = get_sampler_count(cs_bin),
1565 .BindingTableEntryCount = get_binding_table_entry_count(cs_bin),
1566 .BarrierEnable = cs_prog_data->uses_barrier,
1567 .SharedLocalMemorySize =
1568 encode_slm_size(GEN_GEN, cs_prog_data->base.total_shared),
1569
1570 #if !GEN_IS_HASWELL
1571 .ConstantURBEntryReadOffset = 0,
1572 #endif
1573 .ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs,
1574 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1575 .CrossThreadConstantDataReadLength =
1576 cs_prog_data->push.cross_thread.regs,
1577 #endif
1578
1579 .NumberofThreadsinGPGPUThreadGroup = cs_prog_data->threads,
1580 };
1581 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(NULL,
1582 pipeline->interface_descriptor_data,
1583 &desc);
1584
1585 *pPipeline = anv_pipeline_to_handle(pipeline);
1586
1587 return VK_SUCCESS;
1588 }
1589
1590 VkResult genX(CreateGraphicsPipelines)(
1591 VkDevice _device,
1592 VkPipelineCache pipelineCache,
1593 uint32_t count,
1594 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1595 const VkAllocationCallbacks* pAllocator,
1596 VkPipeline* pPipelines)
1597 {
1598 ANV_FROM_HANDLE(anv_pipeline_cache, pipeline_cache, pipelineCache);
1599
1600 VkResult result = VK_SUCCESS;
1601
1602 unsigned i;
1603 for (i = 0; i < count; i++) {
1604 result = genX(graphics_pipeline_create)(_device,
1605 pipeline_cache,
1606 &pCreateInfos[i],
1607 pAllocator, &pPipelines[i]);
1608
1609 /* Bail out on the first error as it is not obvious what error should be
1610 * report upon 2 different failures. */
1611 if (result != VK_SUCCESS)
1612 break;
1613 }
1614
1615 for (; i < count; i++)
1616 pPipelines[i] = VK_NULL_HANDLE;
1617
1618 return result;
1619 }
1620
1621 VkResult genX(CreateComputePipelines)(
1622 VkDevice _device,
1623 VkPipelineCache pipelineCache,
1624 uint32_t count,
1625 const VkComputePipelineCreateInfo* pCreateInfos,
1626 const VkAllocationCallbacks* pAllocator,
1627 VkPipeline* pPipelines)
1628 {
1629 ANV_FROM_HANDLE(anv_pipeline_cache, pipeline_cache, pipelineCache);
1630
1631 VkResult result = VK_SUCCESS;
1632
1633 unsigned i;
1634 for (i = 0; i < count; i++) {
1635 result = compute_pipeline_create(_device, pipeline_cache,
1636 &pCreateInfos[i],
1637 pAllocator, &pPipelines[i]);
1638
1639 /* Bail out on the first error as it is not obvious what error should be
1640 * report upon 2 different failures. */
1641 if (result != VK_SUCCESS)
1642 break;
1643 }
1644
1645 for (; i < count; i++)
1646 pPipelines[i] = VK_NULL_HANDLE;
1647
1648 return result;
1649 }