2 * Copyright © 2015 Intel Corporation
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:
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
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
24 #include "anv_private.h"
26 #include "genxml/gen_macros.h"
27 #include "genxml/genX_pack.h"
29 #include "common/gen_l3_config.h"
30 #include "common/gen_sample_positions.h"
31 #include "vk_format_info.h"
34 vertex_element_comp_control(enum isl_format format
, unsigned 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");
46 return VFCOMP_STORE_SRC
;
47 } else if (comp
< 3) {
48 return VFCOMP_STORE_0
;
49 } else if (isl_format_layouts
[format
].channels
.r
.type
== ISL_UINT
||
50 isl_format_layouts
[format
].channels
.r
.type
== ISL_SINT
) {
52 return VFCOMP_STORE_1_INT
;
55 return VFCOMP_STORE_1_FP
;
60 emit_vertex_input(struct anv_pipeline
*pipeline
,
61 const VkPipelineVertexInputStateCreateInfo
*info
)
63 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
65 /* Pull inputs_read out of the VS prog data */
66 const uint64_t inputs_read
= vs_prog_data
->inputs_read
;
67 assert((inputs_read
& ((1 << VERT_ATTRIB_GENERIC0
) - 1)) == 0);
68 const uint32_t elements
= inputs_read
>> VERT_ATTRIB_GENERIC0
;
71 /* On BDW+, we only need to allocate space for base ids. Setting up
72 * the actual vertex and instance id is a separate packet.
74 const bool needs_svgs_elem
= vs_prog_data
->uses_basevertex
||
75 vs_prog_data
->uses_baseinstance
;
77 /* On Haswell and prior, vertex and instance id are created by using the
78 * ComponentControl fields, so we need an element for any of them.
80 const bool needs_svgs_elem
= vs_prog_data
->uses_vertexid
||
81 vs_prog_data
->uses_instanceid
||
82 vs_prog_data
->uses_basevertex
||
83 vs_prog_data
->uses_baseinstance
;
86 uint32_t elem_count
= __builtin_popcount(elements
) + needs_svgs_elem
;
92 const uint32_t num_dwords
= 1 + elem_count
* 2;
93 p
= anv_batch_emitn(&pipeline
->batch
, num_dwords
,
94 GENX(3DSTATE_VERTEX_ELEMENTS
));
95 memset(p
+ 1, 0, (num_dwords
- 1) * 4);
97 for (uint32_t i
= 0; i
< info
->vertexAttributeDescriptionCount
; i
++) {
98 const VkVertexInputAttributeDescription
*desc
=
99 &info
->pVertexAttributeDescriptions
[i
];
100 enum isl_format format
= anv_get_isl_format(&pipeline
->device
->info
,
102 VK_IMAGE_ASPECT_COLOR_BIT
,
103 VK_IMAGE_TILING_LINEAR
);
105 assert(desc
->binding
< 32);
107 if ((elements
& (1 << desc
->location
)) == 0)
108 continue; /* Binding unused */
110 uint32_t slot
= __builtin_popcount(elements
& ((1 << desc
->location
) - 1));
112 struct GENX(VERTEX_ELEMENT_STATE
) element
= {
113 .VertexBufferIndex
= desc
->binding
,
115 .SourceElementFormat
= format
,
116 .EdgeFlagEnable
= false,
117 .SourceElementOffset
= desc
->offset
,
118 .Component0Control
= vertex_element_comp_control(format
, 0),
119 .Component1Control
= vertex_element_comp_control(format
, 1),
120 .Component2Control
= vertex_element_comp_control(format
, 2),
121 .Component3Control
= vertex_element_comp_control(format
, 3),
123 GENX(VERTEX_ELEMENT_STATE_pack
)(NULL
, &p
[1 + slot
* 2], &element
);
126 /* On Broadwell and later, we have a separate VF_INSTANCING packet
127 * that controls instancing. On Haswell and prior, that's part of
128 * VERTEX_BUFFER_STATE which we emit later.
130 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_VF_INSTANCING
), vfi
) {
131 vfi
.InstancingEnable
= pipeline
->instancing_enable
[desc
->binding
];
132 vfi
.VertexElementIndex
= slot
;
133 /* Vulkan so far doesn't have an instance divisor, so
134 * this is always 1 (ignored if not instancing). */
135 vfi
.InstanceDataStepRate
= 1;
140 const uint32_t id_slot
= __builtin_popcount(elements
);
141 if (needs_svgs_elem
) {
142 /* From the Broadwell PRM for the 3D_Vertex_Component_Control enum:
143 * "Within a VERTEX_ELEMENT_STATE structure, if a Component
144 * Control field is set to something other than VFCOMP_STORE_SRC,
145 * no higher-numbered Component Control fields may be set to
148 * This means, that if we have BaseInstance, we need BaseVertex as
149 * well. Just do all or nothing.
151 uint32_t base_ctrl
= (vs_prog_data
->uses_basevertex
||
152 vs_prog_data
->uses_baseinstance
) ?
153 VFCOMP_STORE_SRC
: VFCOMP_STORE_0
;
155 struct GENX(VERTEX_ELEMENT_STATE
) element
= {
156 .VertexBufferIndex
= 32, /* Reserved for this */
158 .SourceElementFormat
= ISL_FORMAT_R32G32_UINT
,
159 .Component0Control
= base_ctrl
,
160 .Component1Control
= base_ctrl
,
162 .Component2Control
= VFCOMP_STORE_0
,
163 .Component3Control
= VFCOMP_STORE_0
,
165 .Component2Control
= VFCOMP_STORE_VID
,
166 .Component3Control
= VFCOMP_STORE_IID
,
169 GENX(VERTEX_ELEMENT_STATE_pack
)(NULL
, &p
[1 + id_slot
* 2], &element
);
173 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_VF_SGVS
), sgvs
) {
174 sgvs
.VertexIDEnable
= vs_prog_data
->uses_vertexid
;
175 sgvs
.VertexIDComponentNumber
= 2;
176 sgvs
.VertexIDElementOffset
= id_slot
;
177 sgvs
.InstanceIDEnable
= vs_prog_data
->uses_instanceid
;
178 sgvs
.InstanceIDComponentNumber
= 3;
179 sgvs
.InstanceIDElementOffset
= id_slot
;
185 genX(emit_urb_setup
)(struct anv_device
*device
, struct anv_batch
*batch
,
186 VkShaderStageFlags active_stages
,
187 unsigned vs_size
, unsigned gs_size
,
188 const struct gen_l3_config
*l3_config
)
190 if (!(active_stages
& VK_SHADER_STAGE_VERTEX_BIT
))
193 if (!(active_stages
& VK_SHADER_STAGE_GEOMETRY_BIT
))
196 unsigned vs_entry_size_bytes
= vs_size
* 64;
197 unsigned gs_entry_size_bytes
= gs_size
* 64;
199 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
201 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
202 * Allocation Size is less than 9 512-bit URB entries.
204 * Similar text exists for GS.
206 unsigned vs_granularity
= (vs_size
< 9) ? 8 : 1;
207 unsigned gs_granularity
= (gs_size
< 9) ? 8 : 1;
209 /* URB allocations must be done in 8k chunks. */
210 unsigned chunk_size_bytes
= 8192;
212 /* Determine the size of the URB in chunks. */
213 const unsigned total_urb_size
=
214 gen_get_l3_config_urb_size(&device
->info
, l3_config
);
215 const unsigned urb_chunks
= total_urb_size
* 1024 / chunk_size_bytes
;
217 /* Reserve space for push constants */
218 unsigned push_constant_kb
;
219 if (device
->info
.gen
>= 8)
220 push_constant_kb
= 32;
221 else if (device
->info
.is_haswell
)
222 push_constant_kb
= device
->info
.gt
== 3 ? 32 : 16;
224 push_constant_kb
= 16;
226 unsigned push_constant_bytes
= push_constant_kb
* 1024;
227 unsigned push_constant_chunks
=
228 push_constant_bytes
/ chunk_size_bytes
;
230 /* Initially, assign each stage the minimum amount of URB space it needs,
231 * and make a note of how much additional space it "wants" (the amount of
232 * additional space it could actually make use of).
235 /* VS has a lower limit on the number of URB entries */
237 ALIGN(device
->info
.urb
.min_vs_entries
* vs_entry_size_bytes
,
238 chunk_size_bytes
) / chunk_size_bytes
;
240 ALIGN(device
->info
.urb
.max_vs_entries
* vs_entry_size_bytes
,
241 chunk_size_bytes
) / chunk_size_bytes
- vs_chunks
;
243 unsigned gs_chunks
= 0;
244 unsigned gs_wants
= 0;
245 if (active_stages
& VK_SHADER_STAGE_GEOMETRY_BIT
) {
246 /* There are two constraints on the minimum amount of URB space we can
249 * (1) We need room for at least 2 URB entries, since we always operate
250 * the GS in DUAL_OBJECT mode.
252 * (2) We can't allocate less than nr_gs_entries_granularity.
254 gs_chunks
= ALIGN(MAX2(gs_granularity
, 2) * gs_entry_size_bytes
,
255 chunk_size_bytes
) / chunk_size_bytes
;
257 ALIGN(device
->info
.urb
.max_gs_entries
* gs_entry_size_bytes
,
258 chunk_size_bytes
) / chunk_size_bytes
- gs_chunks
;
261 /* There should always be enough URB space to satisfy the minimum
262 * requirements of each stage.
264 unsigned total_needs
= push_constant_chunks
+ vs_chunks
+ gs_chunks
;
265 assert(total_needs
<= urb_chunks
);
267 /* Mete out remaining space (if any) in proportion to "wants". */
268 unsigned total_wants
= vs_wants
+ gs_wants
;
269 unsigned remaining_space
= urb_chunks
- total_needs
;
270 if (remaining_space
> total_wants
)
271 remaining_space
= total_wants
;
272 if (remaining_space
> 0) {
273 unsigned vs_additional
= (unsigned)
274 round(vs_wants
* (((double) remaining_space
) / total_wants
));
275 vs_chunks
+= vs_additional
;
276 remaining_space
-= vs_additional
;
277 gs_chunks
+= remaining_space
;
280 /* Sanity check that we haven't over-allocated. */
281 assert(push_constant_chunks
+ vs_chunks
+ gs_chunks
<= urb_chunks
);
283 /* Finally, compute the number of entries that can fit in the space
284 * allocated to each stage.
286 unsigned nr_vs_entries
= vs_chunks
* chunk_size_bytes
/ vs_entry_size_bytes
;
287 unsigned nr_gs_entries
= gs_chunks
* chunk_size_bytes
/ gs_entry_size_bytes
;
289 /* Since we rounded up when computing *_wants, this may be slightly more
290 * than the maximum allowed amount, so correct for that.
292 nr_vs_entries
= MIN2(nr_vs_entries
, device
->info
.urb
.max_vs_entries
);
293 nr_gs_entries
= MIN2(nr_gs_entries
, device
->info
.urb
.max_gs_entries
);
295 /* Ensure that we program a multiple of the granularity. */
296 nr_vs_entries
= ROUND_DOWN_TO(nr_vs_entries
, vs_granularity
);
297 nr_gs_entries
= ROUND_DOWN_TO(nr_gs_entries
, gs_granularity
);
299 /* Finally, sanity check to make sure we have at least the minimum number
300 * of entries needed for each stage.
302 assert(nr_vs_entries
>= device
->info
.urb
.min_vs_entries
);
303 if (active_stages
& VK_SHADER_STAGE_GEOMETRY_BIT
)
304 assert(nr_gs_entries
>= 2);
306 #if GEN_GEN == 7 && !GEN_IS_HASWELL
307 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
309 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth stall
310 * needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
311 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
312 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL
313 * needs to be sent before any combination of VS associated 3DSTATE."
315 anv_batch_emit(batch
, GEN7_PIPE_CONTROL
, pc
) {
316 pc
.DepthStallEnable
= true;
317 pc
.PostSyncOperation
= WriteImmediateData
;
318 pc
.Address
= (struct anv_address
) { &device
->workaround_bo
, 0 };
322 /* Lay out the URB in the following order:
327 anv_batch_emit(batch
, GENX(3DSTATE_URB_VS
), urb
) {
328 urb
.VSURBStartingAddress
= push_constant_chunks
;
329 urb
.VSURBEntryAllocationSize
= vs_size
- 1;
330 urb
.VSNumberofURBEntries
= nr_vs_entries
;
333 anv_batch_emit(batch
, GENX(3DSTATE_URB_HS
), urb
) {
334 urb
.HSURBStartingAddress
= push_constant_chunks
;
337 anv_batch_emit(batch
, GENX(3DSTATE_URB_DS
), urb
) {
338 urb
.DSURBStartingAddress
= push_constant_chunks
;
341 anv_batch_emit(batch
, GENX(3DSTATE_URB_GS
), urb
) {
342 urb
.GSURBStartingAddress
= push_constant_chunks
+ vs_chunks
;
343 urb
.GSURBEntryAllocationSize
= gs_size
- 1;
344 urb
.GSNumberofURBEntries
= nr_gs_entries
;
349 emit_urb_setup(struct anv_pipeline
*pipeline
)
351 unsigned vs_entry_size
=
352 (pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
) ?
353 get_vs_prog_data(pipeline
)->base
.urb_entry_size
: 0;
354 unsigned gs_entry_size
=
355 (pipeline
->active_stages
& VK_SHADER_STAGE_GEOMETRY_BIT
) ?
356 get_gs_prog_data(pipeline
)->base
.urb_entry_size
: 0;
358 genX(emit_urb_setup
)(pipeline
->device
, &pipeline
->batch
,
359 pipeline
->active_stages
, vs_entry_size
, gs_entry_size
,
360 pipeline
->urb
.l3_config
);
364 emit_3dstate_sbe(struct anv_pipeline
*pipeline
)
366 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
367 const struct brw_gs_prog_data
*gs_prog_data
= get_gs_prog_data(pipeline
);
368 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
369 const struct brw_vue_map
*fs_input_map
;
371 if (!anv_pipeline_has_stage(pipeline
, MESA_SHADER_FRAGMENT
)) {
372 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_SBE
), sbe
);
374 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_SBE_SWIZ
), sbe
);
380 fs_input_map
= &gs_prog_data
->base
.vue_map
;
382 fs_input_map
= &vs_prog_data
->base
.vue_map
;
384 struct GENX(3DSTATE_SBE
) sbe
= {
385 GENX(3DSTATE_SBE_header
),
386 .AttributeSwizzleEnable
= true,
387 .PointSpriteTextureCoordinateOrigin
= UPPERLEFT
,
388 .NumberofSFOutputAttributes
= wm_prog_data
->num_varying_inputs
,
389 .ConstantInterpolationEnable
= wm_prog_data
->flat_inputs
,
393 for (unsigned i
= 0; i
< 32; i
++)
394 sbe
.AttributeActiveComponentFormat
[i
] = ACF_XYZW
;
398 /* On Broadwell, they broke 3DSTATE_SBE into two packets */
399 struct GENX(3DSTATE_SBE_SWIZ
) swiz
= {
400 GENX(3DSTATE_SBE_SWIZ_header
),
406 int max_source_attr
= 0;
407 for (int attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
408 int input_index
= wm_prog_data
->urb_setup
[attr
];
413 if (attr
== VARYING_SLOT_PNTC
) {
414 sbe
.PointSpriteTextureCoordinateEnable
= 1 << input_index
;
418 const int slot
= fs_input_map
->varying_to_slot
[attr
];
420 if (input_index
>= 16)
424 /* This attribute does not exist in the VUE--that means that the
425 * vertex shader did not write to it. It could be that it's a
426 * regular varying read by the fragment shader but not written by
427 * the vertex shader or it's gl_PrimitiveID. In the first case the
428 * value is undefined, in the second it needs to be
431 swiz
.Attribute
[input_index
].ConstantSource
= PRIM_ID
;
432 swiz
.Attribute
[input_index
].ComponentOverrideX
= true;
433 swiz
.Attribute
[input_index
].ComponentOverrideY
= true;
434 swiz
.Attribute
[input_index
].ComponentOverrideZ
= true;
435 swiz
.Attribute
[input_index
].ComponentOverrideW
= true;
438 const int source_attr
= slot
- 2;
439 max_source_attr
= MAX2(max_source_attr
, source_attr
);
440 /* We have to subtract two slots to accout for the URB entry output
441 * read offset in the VS and GS stages.
443 swiz
.Attribute
[input_index
].SourceAttribute
= source_attr
;
447 sbe
.VertexURBEntryReadOffset
= 1; /* Skip the VUE header and position slots */
448 sbe
.VertexURBEntryReadLength
= DIV_ROUND_UP(max_source_attr
+ 1, 2);
450 uint32_t *dw
= anv_batch_emit_dwords(&pipeline
->batch
,
451 GENX(3DSTATE_SBE_length
));
452 GENX(3DSTATE_SBE_pack
)(&pipeline
->batch
, dw
, &sbe
);
455 dw
= anv_batch_emit_dwords(&pipeline
->batch
, GENX(3DSTATE_SBE_SWIZ_length
));
456 GENX(3DSTATE_SBE_SWIZ_pack
)(&pipeline
->batch
, dw
, &swiz
);
460 static const uint32_t vk_to_gen_cullmode
[] = {
461 [VK_CULL_MODE_NONE
] = CULLMODE_NONE
,
462 [VK_CULL_MODE_FRONT_BIT
] = CULLMODE_FRONT
,
463 [VK_CULL_MODE_BACK_BIT
] = CULLMODE_BACK
,
464 [VK_CULL_MODE_FRONT_AND_BACK
] = CULLMODE_BOTH
467 static const uint32_t vk_to_gen_fillmode
[] = {
468 [VK_POLYGON_MODE_FILL
] = FILL_MODE_SOLID
,
469 [VK_POLYGON_MODE_LINE
] = FILL_MODE_WIREFRAME
,
470 [VK_POLYGON_MODE_POINT
] = FILL_MODE_POINT
,
473 static const uint32_t vk_to_gen_front_face
[] = {
474 [VK_FRONT_FACE_COUNTER_CLOCKWISE
] = 1,
475 [VK_FRONT_FACE_CLOCKWISE
] = 0
479 emit_rs_state(struct anv_pipeline
*pipeline
,
480 const VkPipelineRasterizationStateCreateInfo
*rs_info
,
481 const VkPipelineMultisampleStateCreateInfo
*ms_info
,
482 const struct anv_render_pass
*pass
,
483 const struct anv_subpass
*subpass
)
485 struct GENX(3DSTATE_SF
) sf
= {
486 GENX(3DSTATE_SF_header
),
489 sf
.ViewportTransformEnable
= true;
490 sf
.StatisticsEnable
= true;
491 sf
.TriangleStripListProvokingVertexSelect
= 0;
492 sf
.LineStripListProvokingVertexSelect
= 0;
493 sf
.TriangleFanProvokingVertexSelect
= 1;
494 sf
.PointWidthSource
= Vertex
;
498 struct GENX(3DSTATE_RASTER
) raster
= {
499 GENX(3DSTATE_RASTER_header
),
505 /* For details on 3DSTATE_RASTER multisample state, see the BSpec table
506 * "Multisample Modes State".
509 raster
.DXMultisampleRasterizationEnable
= true;
510 raster
.ForcedSampleCount
= FSC_NUMRASTSAMPLES_0
;
511 raster
.ForceMultisampling
= false;
513 raster
.MultisampleRasterizationMode
=
514 (ms_info
&& ms_info
->rasterizationSamples
> 1) ?
515 MSRASTMODE_ON_PATTERN
: MSRASTMODE_OFF_PIXEL
;
518 raster
.FrontWinding
= vk_to_gen_front_face
[rs_info
->frontFace
];
519 raster
.CullMode
= vk_to_gen_cullmode
[rs_info
->cullMode
];
520 raster
.FrontFaceFillMode
= vk_to_gen_fillmode
[rs_info
->polygonMode
];
521 raster
.BackFaceFillMode
= vk_to_gen_fillmode
[rs_info
->polygonMode
];
522 raster
.ScissorRectangleEnable
= true;
525 /* GEN9+ splits ViewportZClipTestEnable into near and far enable bits */
526 raster
.ViewportZFarClipTestEnable
= !pipeline
->depth_clamp_enable
;
527 raster
.ViewportZNearClipTestEnable
= !pipeline
->depth_clamp_enable
;
529 raster
.ViewportZClipTestEnable
= !pipeline
->depth_clamp_enable
;
532 raster
.GlobalDepthOffsetEnableSolid
= rs_info
->depthBiasEnable
;
533 raster
.GlobalDepthOffsetEnableWireframe
= rs_info
->depthBiasEnable
;
534 raster
.GlobalDepthOffsetEnablePoint
= rs_info
->depthBiasEnable
;
537 /* Gen7 requires that we provide the depth format in 3DSTATE_SF so that it
538 * can get the depth offsets correct.
540 if (subpass
->depth_stencil_attachment
< pass
->attachment_count
) {
542 pass
->attachments
[subpass
->depth_stencil_attachment
].format
;
543 assert(vk_format_is_depth_or_stencil(vk_format
));
544 if (vk_format_aspects(vk_format
) & VK_IMAGE_ASPECT_DEPTH_BIT
) {
545 enum isl_format isl_format
=
546 anv_get_isl_format(&pipeline
->device
->info
, vk_format
,
547 VK_IMAGE_ASPECT_DEPTH_BIT
,
548 VK_IMAGE_TILING_OPTIMAL
);
549 sf
.DepthBufferSurfaceFormat
=
550 isl_format_get_depth_format(isl_format
, false);
556 GENX(3DSTATE_SF_pack
)(NULL
, pipeline
->gen8
.sf
, &sf
);
557 GENX(3DSTATE_RASTER_pack
)(NULL
, pipeline
->gen8
.raster
, &raster
);
560 GENX(3DSTATE_SF_pack
)(NULL
, &pipeline
->gen7
.sf
, &sf
);
565 emit_ms_state(struct anv_pipeline
*pipeline
,
566 const VkPipelineMultisampleStateCreateInfo
*info
)
568 uint32_t samples
= 1;
569 uint32_t log2_samples
= 0;
571 /* From the Vulkan 1.0 spec:
572 * If pSampleMask is NULL, it is treated as if the mask has all bits
573 * enabled, i.e. no coverage is removed from fragments.
575 * 3DSTATE_SAMPLE_MASK.SampleMask is 16 bits.
578 uint32_t sample_mask
= 0xffff;
580 uint32_t sample_mask
= 0xff;
584 samples
= info
->rasterizationSamples
;
585 log2_samples
= __builtin_ffs(samples
) - 1;
588 if (info
&& info
->pSampleMask
)
589 sample_mask
&= info
->pSampleMask
[0];
591 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_MULTISAMPLE
), ms
) {
592 ms
.NumberofMultisamples
= log2_samples
;
595 /* The PRM says that this bit is valid only for DX9:
597 * SW can choose to set this bit only for DX9 API. DX10/OGL API's
598 * should not have any effect by setting or not setting this bit.
600 ms
.PixelPositionOffsetEnable
= false;
601 ms
.PixelLocation
= CENTER
;
603 ms
.PixelLocation
= PIXLOC_CENTER
;
607 GEN_SAMPLE_POS_1X(ms
.Sample
);
610 GEN_SAMPLE_POS_2X(ms
.Sample
);
613 GEN_SAMPLE_POS_4X(ms
.Sample
);
616 GEN_SAMPLE_POS_8X(ms
.Sample
);
624 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_SAMPLE_MASK
), sm
) {
625 sm
.SampleMask
= sample_mask
;
629 static const uint32_t vk_to_gen_logic_op
[] = {
630 [VK_LOGIC_OP_COPY
] = LOGICOP_COPY
,
631 [VK_LOGIC_OP_CLEAR
] = LOGICOP_CLEAR
,
632 [VK_LOGIC_OP_AND
] = LOGICOP_AND
,
633 [VK_LOGIC_OP_AND_REVERSE
] = LOGICOP_AND_REVERSE
,
634 [VK_LOGIC_OP_AND_INVERTED
] = LOGICOP_AND_INVERTED
,
635 [VK_LOGIC_OP_NO_OP
] = LOGICOP_NOOP
,
636 [VK_LOGIC_OP_XOR
] = LOGICOP_XOR
,
637 [VK_LOGIC_OP_OR
] = LOGICOP_OR
,
638 [VK_LOGIC_OP_NOR
] = LOGICOP_NOR
,
639 [VK_LOGIC_OP_EQUIVALENT
] = LOGICOP_EQUIV
,
640 [VK_LOGIC_OP_INVERT
] = LOGICOP_INVERT
,
641 [VK_LOGIC_OP_OR_REVERSE
] = LOGICOP_OR_REVERSE
,
642 [VK_LOGIC_OP_COPY_INVERTED
] = LOGICOP_COPY_INVERTED
,
643 [VK_LOGIC_OP_OR_INVERTED
] = LOGICOP_OR_INVERTED
,
644 [VK_LOGIC_OP_NAND
] = LOGICOP_NAND
,
645 [VK_LOGIC_OP_SET
] = LOGICOP_SET
,
648 static const uint32_t vk_to_gen_blend
[] = {
649 [VK_BLEND_FACTOR_ZERO
] = BLENDFACTOR_ZERO
,
650 [VK_BLEND_FACTOR_ONE
] = BLENDFACTOR_ONE
,
651 [VK_BLEND_FACTOR_SRC_COLOR
] = BLENDFACTOR_SRC_COLOR
,
652 [VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR
] = BLENDFACTOR_INV_SRC_COLOR
,
653 [VK_BLEND_FACTOR_DST_COLOR
] = BLENDFACTOR_DST_COLOR
,
654 [VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR
] = BLENDFACTOR_INV_DST_COLOR
,
655 [VK_BLEND_FACTOR_SRC_ALPHA
] = BLENDFACTOR_SRC_ALPHA
,
656 [VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
] = BLENDFACTOR_INV_SRC_ALPHA
,
657 [VK_BLEND_FACTOR_DST_ALPHA
] = BLENDFACTOR_DST_ALPHA
,
658 [VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA
] = BLENDFACTOR_INV_DST_ALPHA
,
659 [VK_BLEND_FACTOR_CONSTANT_COLOR
] = BLENDFACTOR_CONST_COLOR
,
660 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
]= BLENDFACTOR_INV_CONST_COLOR
,
661 [VK_BLEND_FACTOR_CONSTANT_ALPHA
] = BLENDFACTOR_CONST_ALPHA
,
662 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
]= BLENDFACTOR_INV_CONST_ALPHA
,
663 [VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
] = BLENDFACTOR_SRC_ALPHA_SATURATE
,
664 [VK_BLEND_FACTOR_SRC1_COLOR
] = BLENDFACTOR_SRC1_COLOR
,
665 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR
] = BLENDFACTOR_INV_SRC1_COLOR
,
666 [VK_BLEND_FACTOR_SRC1_ALPHA
] = BLENDFACTOR_SRC1_ALPHA
,
667 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA
] = BLENDFACTOR_INV_SRC1_ALPHA
,
670 static const uint32_t vk_to_gen_blend_op
[] = {
671 [VK_BLEND_OP_ADD
] = BLENDFUNCTION_ADD
,
672 [VK_BLEND_OP_SUBTRACT
] = BLENDFUNCTION_SUBTRACT
,
673 [VK_BLEND_OP_REVERSE_SUBTRACT
] = BLENDFUNCTION_REVERSE_SUBTRACT
,
674 [VK_BLEND_OP_MIN
] = BLENDFUNCTION_MIN
,
675 [VK_BLEND_OP_MAX
] = BLENDFUNCTION_MAX
,
678 static const uint32_t vk_to_gen_compare_op
[] = {
679 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
680 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
681 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
682 [VK_COMPARE_OP_LESS_OR_EQUAL
] = PREFILTEROPLEQUAL
,
683 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
684 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
685 [VK_COMPARE_OP_GREATER_OR_EQUAL
] = PREFILTEROPGEQUAL
,
686 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
689 static const uint32_t vk_to_gen_stencil_op
[] = {
690 [VK_STENCIL_OP_KEEP
] = STENCILOP_KEEP
,
691 [VK_STENCIL_OP_ZERO
] = STENCILOP_ZERO
,
692 [VK_STENCIL_OP_REPLACE
] = STENCILOP_REPLACE
,
693 [VK_STENCIL_OP_INCREMENT_AND_CLAMP
] = STENCILOP_INCRSAT
,
694 [VK_STENCIL_OP_DECREMENT_AND_CLAMP
] = STENCILOP_DECRSAT
,
695 [VK_STENCIL_OP_INVERT
] = STENCILOP_INVERT
,
696 [VK_STENCIL_OP_INCREMENT_AND_WRAP
] = STENCILOP_INCR
,
697 [VK_STENCIL_OP_DECREMENT_AND_WRAP
] = STENCILOP_DECR
,
701 emit_ds_state(struct anv_pipeline
*pipeline
,
702 const VkPipelineDepthStencilStateCreateInfo
*info
,
703 const struct anv_render_pass
*pass
,
704 const struct anv_subpass
*subpass
)
707 # define depth_stencil_dw pipeline->gen7.depth_stencil_state
709 # define depth_stencil_dw pipeline->gen8.wm_depth_stencil
711 # define depth_stencil_dw pipeline->gen9.wm_depth_stencil
715 /* We're going to OR this together with the dynamic state. We need
716 * to make sure it's initialized to something useful.
718 memset(depth_stencil_dw
, 0, sizeof(depth_stencil_dw
));
722 /* VkBool32 depthBoundsTestEnable; // optional (depth_bounds_test) */
725 struct GENX(DEPTH_STENCIL_STATE
) depth_stencil
= {
727 struct GENX(3DSTATE_WM_DEPTH_STENCIL
) depth_stencil
= {
729 .DepthTestEnable
= info
->depthTestEnable
,
730 .DepthBufferWriteEnable
= info
->depthWriteEnable
,
731 .DepthTestFunction
= vk_to_gen_compare_op
[info
->depthCompareOp
],
732 .DoubleSidedStencilEnable
= true,
734 .StencilTestEnable
= info
->stencilTestEnable
,
735 .StencilBufferWriteEnable
= info
->stencilTestEnable
,
736 .StencilFailOp
= vk_to_gen_stencil_op
[info
->front
.failOp
],
737 .StencilPassDepthPassOp
= vk_to_gen_stencil_op
[info
->front
.passOp
],
738 .StencilPassDepthFailOp
= vk_to_gen_stencil_op
[info
->front
.depthFailOp
],
739 .StencilTestFunction
= vk_to_gen_compare_op
[info
->front
.compareOp
],
740 .BackfaceStencilFailOp
= vk_to_gen_stencil_op
[info
->back
.failOp
],
741 .BackfaceStencilPassDepthPassOp
= vk_to_gen_stencil_op
[info
->back
.passOp
],
742 .BackfaceStencilPassDepthFailOp
=vk_to_gen_stencil_op
[info
->back
.depthFailOp
],
743 .BackfaceStencilTestFunction
= vk_to_gen_compare_op
[info
->back
.compareOp
],
746 VkImageAspectFlags aspects
= 0;
747 if (subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
) {
748 VkFormat depth_stencil_format
=
749 pass
->attachments
[subpass
->depth_stencil_attachment
].format
;
750 aspects
= vk_format_aspects(depth_stencil_format
);
753 /* The Vulkan spec requires that if either depth or stencil is not present,
754 * the pipeline is to act as if the test silently passes.
756 if (!(aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
757 depth_stencil
.DepthBufferWriteEnable
= false;
758 depth_stencil
.DepthTestFunction
= PREFILTEROPALWAYS
;
761 if (!(aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
)) {
762 depth_stencil
.StencilBufferWriteEnable
= false;
763 depth_stencil
.StencilTestFunction
= PREFILTEROPALWAYS
;
764 depth_stencil
.BackfaceStencilTestFunction
= PREFILTEROPALWAYS
;
767 /* From the Broadwell PRM:
769 * "If Depth_Test_Enable = 1 AND Depth_Test_func = EQUAL, the
770 * Depth_Write_Enable must be set to 0."
772 if (info
->depthTestEnable
&& info
->depthCompareOp
== VK_COMPARE_OP_EQUAL
)
773 depth_stencil
.DepthBufferWriteEnable
= false;
776 GENX(DEPTH_STENCIL_STATE_pack
)(NULL
, depth_stencil_dw
, &depth_stencil
);
778 GENX(3DSTATE_WM_DEPTH_STENCIL_pack
)(NULL
, depth_stencil_dw
, &depth_stencil
);
783 emit_cb_state(struct anv_pipeline
*pipeline
,
784 const VkPipelineColorBlendStateCreateInfo
*info
,
785 const VkPipelineMultisampleStateCreateInfo
*ms_info
)
787 struct anv_device
*device
= pipeline
->device
;
789 const uint32_t num_dwords
= GENX(BLEND_STATE_length
);
790 pipeline
->blend_state
=
791 anv_state_pool_alloc(&device
->dynamic_state_pool
, num_dwords
* 4, 64);
793 struct GENX(BLEND_STATE
) blend_state
= {
795 .AlphaToCoverageEnable
= ms_info
&& ms_info
->alphaToCoverageEnable
,
796 .AlphaToOneEnable
= ms_info
&& ms_info
->alphaToOneEnable
,
798 /* Make sure it gets zeroed */
799 .Entry
= { { 0, }, },
803 /* Default everything to disabled */
804 for (uint32_t i
= 0; i
< 8; i
++) {
805 blend_state
.Entry
[i
].WriteDisableAlpha
= true;
806 blend_state
.Entry
[i
].WriteDisableRed
= true;
807 blend_state
.Entry
[i
].WriteDisableGreen
= true;
808 blend_state
.Entry
[i
].WriteDisableBlue
= true;
811 uint32_t surface_count
= 0;
812 struct anv_pipeline_bind_map
*map
;
813 if (anv_pipeline_has_stage(pipeline
, MESA_SHADER_FRAGMENT
)) {
814 map
= &pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->bind_map
;
815 surface_count
= map
->surface_count
;
818 bool has_writeable_rt
= false;
819 for (unsigned i
= 0; i
< surface_count
; i
++) {
820 struct anv_pipeline_binding
*binding
= &map
->surface_to_descriptor
[i
];
822 /* All color attachments are at the beginning of the binding table */
823 if (binding
->set
!= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
)
826 /* We can have at most 8 attachments */
829 if (binding
->index
>= info
->attachmentCount
)
832 assert(binding
->binding
== 0);
833 const VkPipelineColorBlendAttachmentState
*a
=
834 &info
->pAttachments
[binding
->index
];
836 blend_state
.Entry
[i
] = (struct GENX(BLEND_STATE_ENTRY
)) {
838 .AlphaToCoverageEnable
= ms_info
&& ms_info
->alphaToCoverageEnable
,
839 .AlphaToOneEnable
= ms_info
&& ms_info
->alphaToOneEnable
,
841 .LogicOpEnable
= info
->logicOpEnable
,
842 .LogicOpFunction
= vk_to_gen_logic_op
[info
->logicOp
],
843 .ColorBufferBlendEnable
= a
->blendEnable
,
844 .ColorClampRange
= COLORCLAMP_RTFORMAT
,
845 .PreBlendColorClampEnable
= true,
846 .PostBlendColorClampEnable
= true,
847 .SourceBlendFactor
= vk_to_gen_blend
[a
->srcColorBlendFactor
],
848 .DestinationBlendFactor
= vk_to_gen_blend
[a
->dstColorBlendFactor
],
849 .ColorBlendFunction
= vk_to_gen_blend_op
[a
->colorBlendOp
],
850 .SourceAlphaBlendFactor
= vk_to_gen_blend
[a
->srcAlphaBlendFactor
],
851 .DestinationAlphaBlendFactor
= vk_to_gen_blend
[a
->dstAlphaBlendFactor
],
852 .AlphaBlendFunction
= vk_to_gen_blend_op
[a
->alphaBlendOp
],
853 .WriteDisableAlpha
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_A_BIT
),
854 .WriteDisableRed
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_R_BIT
),
855 .WriteDisableGreen
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_G_BIT
),
856 .WriteDisableBlue
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_B_BIT
),
859 if (a
->srcColorBlendFactor
!= a
->srcAlphaBlendFactor
||
860 a
->dstColorBlendFactor
!= a
->dstAlphaBlendFactor
||
861 a
->colorBlendOp
!= a
->alphaBlendOp
) {
863 blend_state
.IndependentAlphaBlendEnable
= true;
865 blend_state
.Entry
[i
].IndependentAlphaBlendEnable
= true;
869 if (a
->colorWriteMask
!= 0)
870 has_writeable_rt
= true;
872 /* Our hardware applies the blend factor prior to the blend function
873 * regardless of what function is used. Technically, this means the
874 * hardware can do MORE than GL or Vulkan specify. However, it also
875 * means that, for MIN and MAX, we have to stomp the blend factor to
876 * ONE to make it a no-op.
878 if (a
->colorBlendOp
== VK_BLEND_OP_MIN
||
879 a
->colorBlendOp
== VK_BLEND_OP_MAX
) {
880 blend_state
.Entry
[i
].SourceBlendFactor
= BLENDFACTOR_ONE
;
881 blend_state
.Entry
[i
].DestinationBlendFactor
= BLENDFACTOR_ONE
;
883 if (a
->alphaBlendOp
== VK_BLEND_OP_MIN
||
884 a
->alphaBlendOp
== VK_BLEND_OP_MAX
) {
885 blend_state
.Entry
[i
].SourceAlphaBlendFactor
= BLENDFACTOR_ONE
;
886 blend_state
.Entry
[i
].DestinationAlphaBlendFactor
= BLENDFACTOR_ONE
;
891 struct GENX(BLEND_STATE_ENTRY
) *bs0
= &blend_state
.Entry
[0];
892 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_PS_BLEND
), blend
) {
893 blend
.AlphaToCoverageEnable
= blend_state
.AlphaToCoverageEnable
;
894 blend
.HasWriteableRT
= has_writeable_rt
;
895 blend
.ColorBufferBlendEnable
= bs0
->ColorBufferBlendEnable
;
896 blend
.SourceAlphaBlendFactor
= bs0
->SourceAlphaBlendFactor
;
897 blend
.DestinationAlphaBlendFactor
= bs0
->DestinationAlphaBlendFactor
;
898 blend
.SourceBlendFactor
= bs0
->SourceBlendFactor
;
899 blend
.DestinationBlendFactor
= bs0
->DestinationBlendFactor
;
900 blend
.AlphaTestEnable
= false;
901 blend
.IndependentAlphaBlendEnable
=
902 blend_state
.IndependentAlphaBlendEnable
;
905 (void)has_writeable_rt
;
908 GENX(BLEND_STATE_pack
)(NULL
, pipeline
->blend_state
.map
, &blend_state
);
909 if (!device
->info
.has_llc
)
910 anv_state_clflush(pipeline
->blend_state
);
912 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_BLEND_STATE_POINTERS
), bsp
) {
913 bsp
.BlendStatePointer
= pipeline
->blend_state
.offset
;
915 bsp
.BlendStatePointerValid
= true;
921 emit_3dstate_clip(struct anv_pipeline
*pipeline
,
922 const VkPipelineViewportStateCreateInfo
*vp_info
,
923 const VkPipelineRasterizationStateCreateInfo
*rs_info
)
925 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
927 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_CLIP
), clip
) {
928 clip
.ClipEnable
= true;
929 clip
.EarlyCullEnable
= true;
930 clip
.APIMode
= APIMODE_D3D
,
931 clip
.ViewportXYClipTestEnable
= true;
933 clip
.ClipMode
= CLIPMODE_NORMAL
;
935 clip
.TriangleStripListProvokingVertexSelect
= 0;
936 clip
.LineStripListProvokingVertexSelect
= 0;
937 clip
.TriangleFanProvokingVertexSelect
= 1;
939 clip
.MinimumPointWidth
= 0.125;
940 clip
.MaximumPointWidth
= 255.875;
941 clip
.MaximumVPIndex
= (vp_info
? vp_info
->viewportCount
: 1) - 1;
944 clip
.FrontWinding
= vk_to_gen_front_face
[rs_info
->frontFace
];
945 clip
.CullMode
= vk_to_gen_cullmode
[rs_info
->cullMode
];
946 clip
.ViewportZClipTestEnable
= !pipeline
->depth_clamp_enable
;
948 clip
.NonPerspectiveBarycentricEnable
= wm_prog_data
?
949 (wm_prog_data
->barycentric_interp_modes
& 0x38) != 0 : 0;
955 emit_3dstate_streamout(struct anv_pipeline
*pipeline
,
956 const VkPipelineRasterizationStateCreateInfo
*rs_info
)
958 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_STREAMOUT
), so
) {
959 so
.RenderingDisable
= rs_info
->rasterizerDiscardEnable
;
963 static inline uint32_t
964 get_sampler_count(const struct anv_shader_bin
*bin
)
966 return DIV_ROUND_UP(bin
->bind_map
.sampler_count
, 4);
969 static inline uint32_t
970 get_binding_table_entry_count(const struct anv_shader_bin
*bin
)
972 return DIV_ROUND_UP(bin
->bind_map
.surface_count
, 32);
975 static inline struct anv_address
976 get_scratch_address(struct anv_pipeline
*pipeline
,
977 gl_shader_stage stage
,
978 const struct anv_shader_bin
*bin
)
980 return (struct anv_address
) {
981 .bo
= anv_scratch_pool_alloc(pipeline
->device
,
982 &pipeline
->device
->scratch_pool
,
983 stage
, bin
->prog_data
->total_scratch
),
988 static inline uint32_t
989 get_scratch_space(const struct anv_shader_bin
*bin
)
991 return ffs(bin
->prog_data
->total_scratch
/ 2048);
994 static inline uint32_t
995 get_urb_output_offset()
997 /* Skip the VUE header and position slots */
1001 static inline uint32_t
1002 get_urb_output_length(const struct anv_shader_bin
*bin
)
1004 const struct brw_vue_prog_data
*prog_data
=
1005 (const struct brw_vue_prog_data
*)bin
->prog_data
;
1007 return (prog_data
->vue_map
.num_slots
+ 1) / 2 - get_urb_output_offset();
1011 emit_3dstate_vs(struct anv_pipeline
*pipeline
)
1013 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
1014 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
1015 const struct anv_shader_bin
*vs_bin
=
1016 pipeline
->shaders
[MESA_SHADER_VERTEX
];
1018 assert(anv_pipeline_has_stage(pipeline
, MESA_SHADER_VERTEX
));
1020 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_VS
), vs
) {
1021 vs
.FunctionEnable
= true;
1022 vs
.StatisticsEnable
= true;
1023 vs
.KernelStartPointer
= vs_bin
->kernel
.offset
;
1025 vs
.SIMD8DispatchEnable
=
1026 vs_prog_data
->base
.dispatch_mode
== DISPATCH_MODE_SIMD8
;
1029 assert(!vs_prog_data
->base
.base
.use_alt_mode
);
1030 vs
.SingleVertexDispatch
= false;
1031 vs
.VectorMaskEnable
= false;
1032 vs
.SamplerCount
= get_sampler_count(vs_bin
);
1033 vs
.BindingTableEntryCount
= get_binding_table_entry_count(vs_bin
);
1034 vs
.FloatingPointMode
= IEEE754
;
1035 vs
.IllegalOpcodeExceptionEnable
= false;
1036 vs
.SoftwareExceptionEnable
= false;
1037 vs
.MaximumNumberofThreads
= devinfo
->max_vs_threads
- 1;
1038 vs
.VertexCacheDisable
= false;
1040 vs
.VertexURBEntryReadLength
= vs_prog_data
->base
.urb_read_length
;
1041 vs
.VertexURBEntryReadOffset
= 0;
1042 vs
.DispatchGRFStartRegisterForURBData
=
1043 vs_prog_data
->base
.base
.dispatch_grf_start_reg
;
1046 vs
.VertexURBEntryOutputReadOffset
= get_urb_output_offset();
1047 vs
.VertexURBEntryOutputLength
= get_urb_output_length(vs_bin
);
1050 vs
.UserClipDistanceClipTestEnableBitmask
= 0;
1051 vs
.UserClipDistanceCullTestEnableBitmask
= 0;
1054 vs
.PerThreadScratchSpace
= get_scratch_space(vs_bin
);
1055 vs
.ScratchSpaceBasePointer
=
1056 get_scratch_address(pipeline
, MESA_SHADER_VERTEX
, vs_bin
);
1061 emit_3dstate_gs(struct anv_pipeline
*pipeline
)
1063 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
1064 const struct anv_shader_bin
*gs_bin
=
1065 pipeline
->shaders
[MESA_SHADER_GEOMETRY
];
1067 if (!anv_pipeline_has_stage(pipeline
, MESA_SHADER_GEOMETRY
)) {
1068 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_GS
), gs
);
1072 const struct brw_gs_prog_data
*gs_prog_data
= get_gs_prog_data(pipeline
);
1074 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_GS
), gs
) {
1075 gs
.FunctionEnable
= true;
1076 gs
.StatisticsEnable
= true;
1077 gs
.KernelStartPointer
= gs_bin
->kernel
.offset
;
1078 gs
.DispatchMode
= gs_prog_data
->base
.dispatch_mode
;
1080 gs
.SingleProgramFlow
= false;
1081 gs
.VectorMaskEnable
= false;
1082 gs
.SamplerCount
= get_sampler_count(gs_bin
);
1083 gs
.BindingTableEntryCount
= get_binding_table_entry_count(gs_bin
);
1084 gs
.IncludeVertexHandles
= gs_prog_data
->base
.include_vue_handles
;
1085 gs
.IncludePrimitiveID
= gs_prog_data
->include_primitive_id
;
1088 /* Broadwell is weird. It needs us to divide by 2. */
1089 gs
.MaximumNumberofThreads
= devinfo
->max_gs_threads
/ 2 - 1;
1091 gs
.MaximumNumberofThreads
= devinfo
->max_gs_threads
- 1;
1094 gs
.OutputVertexSize
= gs_prog_data
->output_vertex_size_hwords
* 2 - 1;
1095 gs
.OutputTopology
= gs_prog_data
->output_topology
;
1096 gs
.VertexURBEntryReadLength
= gs_prog_data
->base
.urb_read_length
;
1097 gs
.ControlDataFormat
= gs_prog_data
->control_data_format
;
1098 gs
.ControlDataHeaderSize
= gs_prog_data
->control_data_header_size_hwords
;
1099 gs
.InstanceControl
= MAX2(gs_prog_data
->invocations
, 1) - 1;
1100 #if GEN_GEN >= 8 || GEN_IS_HASWELL
1101 gs
.ReorderMode
= TRAILING
;
1103 gs
.ReorderEnable
= true;
1107 gs
.ExpectedVertexCount
= gs_prog_data
->vertices_in
;
1108 gs
.StaticOutput
= gs_prog_data
->static_vertex_count
>= 0;
1109 gs
.StaticOutputVertexCount
= gs_prog_data
->static_vertex_count
>= 0 ?
1110 gs_prog_data
->static_vertex_count
: 0;
1113 gs
.VertexURBEntryReadOffset
= 0;
1114 gs
.VertexURBEntryReadLength
= gs_prog_data
->base
.urb_read_length
;
1115 gs
.DispatchGRFStartRegisterForURBData
=
1116 gs_prog_data
->base
.base
.dispatch_grf_start_reg
;
1119 gs
.VertexURBEntryOutputReadOffset
= get_urb_output_offset();
1120 gs
.VertexURBEntryOutputLength
= get_urb_output_length(gs_bin
);
1123 gs
.UserClipDistanceClipTestEnableBitmask
= 0;
1124 gs
.UserClipDistanceCullTestEnableBitmask
= 0;
1127 gs
.PerThreadScratchSpace
= get_scratch_space(gs_bin
);
1128 gs
.ScratchSpaceBasePointer
=
1129 get_scratch_address(pipeline
, MESA_SHADER_GEOMETRY
, gs_bin
);
1134 emit_3dstate_wm(struct anv_pipeline
*pipeline
,
1135 const VkPipelineMultisampleStateCreateInfo
*multisample
)
1137 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
1139 MAYBE_UNUSED
uint32_t samples
=
1140 multisample
? multisample
->rasterizationSamples
: 1;
1142 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_WM
), wm
) {
1143 wm
.StatisticsEnable
= true;
1144 wm
.LineEndCapAntialiasingRegionWidth
= _05pixels
;
1145 wm
.LineAntialiasingRegionWidth
= _10pixels
;
1146 wm
.PointRasterizationRule
= RASTRULE_UPPER_RIGHT
;
1148 if (anv_pipeline_has_stage(pipeline
, MESA_SHADER_FRAGMENT
)) {
1149 if (wm_prog_data
->early_fragment_tests
) {
1150 wm
.EarlyDepthStencilControl
= EDSC_PREPS
;
1151 } else if (wm_prog_data
->has_side_effects
) {
1152 wm
.EarlyDepthStencilControl
= EDSC_PSEXEC
;
1154 wm
.EarlyDepthStencilControl
= EDSC_NORMAL
;
1157 wm
.BarycentricInterpolationMode
=
1158 wm_prog_data
->barycentric_interp_modes
;
1161 /* FIXME: This needs a lot more work, cf gen7 upload_wm_state(). */
1162 wm
.ThreadDispatchEnable
= true;
1164 wm
.PixelShaderKillsPixel
= wm_prog_data
->uses_kill
;
1165 wm
.PixelShaderComputedDepthMode
= wm_prog_data
->computed_depth_mode
;
1166 wm
.PixelShaderUsesSourceDepth
= wm_prog_data
->uses_src_depth
;
1167 wm
.PixelShaderUsesSourceW
= wm_prog_data
->uses_src_w
;
1168 wm
.PixelShaderUsesInputCoverageMask
= wm_prog_data
->uses_sample_mask
;
1171 wm
.MultisampleRasterizationMode
= MSRASTMODE_ON_PATTERN
;
1172 if (wm_prog_data
->persample_dispatch
) {
1173 wm
.MultisampleDispatchMode
= MSDISPMODE_PERSAMPLE
;
1175 wm
.MultisampleDispatchMode
= MSDISPMODE_PERPIXEL
;
1178 wm
.MultisampleRasterizationMode
= MSRASTMODE_OFF_PIXEL
;
1179 wm
.MultisampleDispatchMode
= MSDISPMODE_PERSAMPLE
;
1187 emit_3dstate_ps(struct anv_pipeline
*pipeline
)
1189 MAYBE_UNUSED
const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
1190 const struct anv_shader_bin
*fs_bin
=
1191 pipeline
->shaders
[MESA_SHADER_FRAGMENT
];
1193 if (!anv_pipeline_has_stage(pipeline
, MESA_SHADER_FRAGMENT
)) {
1194 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_PS
), ps
) {
1196 /* Even if no fragments are ever dispatched, gen7 hardware hangs if
1197 * we don't at least set the maximum number of threads.
1199 ps
.MaximumNumberofThreads
= devinfo
->max_wm_threads
- 1;
1205 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
1207 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_PS
), ps
) {
1208 ps
.KernelStartPointer0
= fs_bin
->kernel
.offset
;
1209 ps
.KernelStartPointer1
= 0;
1210 ps
.KernelStartPointer2
= fs_bin
->kernel
.offset
+
1211 wm_prog_data
->prog_offset_2
;
1212 ps
._8PixelDispatchEnable
= wm_prog_data
->dispatch_8
;
1213 ps
._16PixelDispatchEnable
= wm_prog_data
->dispatch_16
;
1214 ps
._32PixelDispatchEnable
= false;
1216 ps
.SingleProgramFlow
= false;
1217 ps
.VectorMaskEnable
= true;
1218 ps
.SamplerCount
= get_sampler_count(fs_bin
);
1219 ps
.BindingTableEntryCount
= get_binding_table_entry_count(fs_bin
);
1220 ps
.PushConstantEnable
= wm_prog_data
->base
.nr_params
> 0;
1221 ps
.PositionXYOffsetSelect
= wm_prog_data
->uses_pos_offset
?
1222 POSOFFSET_SAMPLE
: POSOFFSET_NONE
;
1224 ps
.AttributeEnable
= wm_prog_data
->num_varying_inputs
> 0;
1225 ps
.oMaskPresenttoRenderTarget
= wm_prog_data
->uses_omask
;
1226 ps
.DualSourceBlendEnable
= wm_prog_data
->dual_src_blend
;
1230 /* Haswell requires the sample mask to be set in this packet as well
1231 * as in 3DSTATE_SAMPLE_MASK; the values should match.
1233 ps
.SampleMask
= 0xff;
1237 ps
.MaximumNumberofThreadsPerPSD
= 64 - 1;
1239 ps
.MaximumNumberofThreadsPerPSD
= 64 - 2;
1241 ps
.MaximumNumberofThreads
= devinfo
->max_wm_threads
- 1;
1244 ps
.DispatchGRFStartRegisterForConstantSetupData0
=
1245 wm_prog_data
->base
.dispatch_grf_start_reg
;
1246 ps
.DispatchGRFStartRegisterForConstantSetupData1
= 0;
1247 ps
.DispatchGRFStartRegisterForConstantSetupData2
=
1248 wm_prog_data
->dispatch_grf_start_reg_2
;
1250 ps
.PerThreadScratchSpace
= get_scratch_space(fs_bin
);
1251 ps
.ScratchSpaceBasePointer
=
1252 get_scratch_address(pipeline
, MESA_SHADER_FRAGMENT
, fs_bin
);
1258 emit_3dstate_ps_extra(struct anv_pipeline
*pipeline
)
1260 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
1262 if (!anv_pipeline_has_stage(pipeline
, MESA_SHADER_FRAGMENT
)) {
1263 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_PS_EXTRA
), ps
);
1267 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_PS_EXTRA
), ps
) {
1268 ps
.PixelShaderValid
= true;
1269 ps
.AttributeEnable
= wm_prog_data
->num_varying_inputs
> 0;
1270 ps
.oMaskPresenttoRenderTarget
= wm_prog_data
->uses_omask
;
1271 ps
.PixelShaderIsPerSample
= wm_prog_data
->persample_dispatch
;
1272 ps
.PixelShaderKillsPixel
= wm_prog_data
->uses_kill
;
1273 ps
.PixelShaderComputedDepthMode
= wm_prog_data
->computed_depth_mode
;
1274 ps
.PixelShaderUsesSourceDepth
= wm_prog_data
->uses_src_depth
;
1275 ps
.PixelShaderUsesSourceW
= wm_prog_data
->uses_src_w
;
1278 ps
.PixelShaderPullsBary
= wm_prog_data
->pulls_bary
;
1279 ps
.InputCoverageMaskState
= wm_prog_data
->uses_sample_mask
?
1280 ICMS_INNER_CONSERVATIVE
: ICMS_NONE
;
1282 ps
.PixelShaderUsesInputCoverageMask
= wm_prog_data
->uses_sample_mask
;
1288 emit_3dstate_vf_topology(struct anv_pipeline
*pipeline
)
1290 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_VF_TOPOLOGY
), vft
) {
1291 vft
.PrimitiveTopologyType
= pipeline
->topology
;
1297 genX(graphics_pipeline_create
)(
1299 struct anv_pipeline_cache
* cache
,
1300 const VkGraphicsPipelineCreateInfo
* pCreateInfo
,
1301 const VkAllocationCallbacks
* pAllocator
,
1302 VkPipeline
* pPipeline
)
1304 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1305 ANV_FROM_HANDLE(anv_render_pass
, pass
, pCreateInfo
->renderPass
);
1306 struct anv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
1307 struct anv_pipeline
*pipeline
;
1310 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
1312 pipeline
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
1313 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1314 if (pipeline
== NULL
)
1315 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1317 result
= anv_pipeline_init(pipeline
, device
, cache
,
1318 pCreateInfo
, pAllocator
);
1319 if (result
!= VK_SUCCESS
) {
1320 vk_free2(&device
->alloc
, pAllocator
, pipeline
);
1324 assert(pCreateInfo
->pVertexInputState
);
1325 emit_vertex_input(pipeline
, pCreateInfo
->pVertexInputState
);
1326 assert(pCreateInfo
->pRasterizationState
);
1327 emit_rs_state(pipeline
, pCreateInfo
->pRasterizationState
,
1328 pCreateInfo
->pMultisampleState
, pass
, subpass
);
1329 emit_ms_state(pipeline
, pCreateInfo
->pMultisampleState
);
1330 emit_ds_state(pipeline
, pCreateInfo
->pDepthStencilState
, pass
, subpass
);
1331 emit_cb_state(pipeline
, pCreateInfo
->pColorBlendState
,
1332 pCreateInfo
->pMultisampleState
);
1334 emit_urb_setup(pipeline
);
1336 emit_3dstate_clip(pipeline
, pCreateInfo
->pViewportState
,
1337 pCreateInfo
->pRasterizationState
);
1338 emit_3dstate_streamout(pipeline
, pCreateInfo
->pRasterizationState
);
1341 /* From gen7_vs_state.c */
1344 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
1345 * Geometry > Geometry Shader > State:
1347 * "Note: Because of corruption in IVB:GT2, software needs to flush the
1348 * whole fixed function pipeline when the GS enable changes value in
1351 * The hardware architects have clarified that in this context "flush the
1352 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
1355 if (!brw
->is_haswell
&& !brw
->is_baytrail
)
1356 gen7_emit_vs_workaround_flush(brw
);
1359 emit_3dstate_vs(pipeline
);
1360 emit_3dstate_gs(pipeline
);
1361 emit_3dstate_sbe(pipeline
);
1362 emit_3dstate_wm(pipeline
, pCreateInfo
->pMultisampleState
);
1363 emit_3dstate_ps(pipeline
);
1365 emit_3dstate_ps_extra(pipeline
);
1366 emit_3dstate_vf_topology(pipeline
);
1369 *pPipeline
= anv_pipeline_to_handle(pipeline
);
1375 compute_pipeline_create(
1377 struct anv_pipeline_cache
* cache
,
1378 const VkComputePipelineCreateInfo
* pCreateInfo
,
1379 const VkAllocationCallbacks
* pAllocator
,
1380 VkPipeline
* pPipeline
)
1382 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1383 const struct anv_physical_device
*physical_device
=
1384 &device
->instance
->physicalDevice
;
1385 const struct gen_device_info
*devinfo
= &physical_device
->info
;
1386 struct anv_pipeline
*pipeline
;
1389 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO
);
1391 pipeline
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
1392 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1393 if (pipeline
== NULL
)
1394 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1396 pipeline
->device
= device
;
1397 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
1399 pipeline
->blend_state
.map
= NULL
;
1401 result
= anv_reloc_list_init(&pipeline
->batch_relocs
,
1402 pAllocator
? pAllocator
: &device
->alloc
);
1403 if (result
!= VK_SUCCESS
) {
1404 vk_free2(&device
->alloc
, pAllocator
, pipeline
);
1407 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
1408 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
1409 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
1411 /* When we free the pipeline, we detect stages based on the NULL status
1412 * of various prog_data pointers. Make them NULL by default.
1414 memset(pipeline
->shaders
, 0, sizeof(pipeline
->shaders
));
1416 pipeline
->active_stages
= 0;
1418 pipeline
->needs_data_cache
= false;
1420 assert(pCreateInfo
->stage
.stage
== VK_SHADER_STAGE_COMPUTE_BIT
);
1421 ANV_FROM_HANDLE(anv_shader_module
, module
, pCreateInfo
->stage
.module
);
1422 result
= anv_pipeline_compile_cs(pipeline
, cache
, pCreateInfo
, module
,
1423 pCreateInfo
->stage
.pName
,
1424 pCreateInfo
->stage
.pSpecializationInfo
);
1425 if (result
!= VK_SUCCESS
) {
1426 vk_free2(&device
->alloc
, pAllocator
, pipeline
);
1430 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
1432 anv_pipeline_setup_l3_config(pipeline
, cs_prog_data
->base
.total_shared
> 0);
1434 uint32_t group_size
= cs_prog_data
->local_size
[0] *
1435 cs_prog_data
->local_size
[1] * cs_prog_data
->local_size
[2];
1436 uint32_t remainder
= group_size
& (cs_prog_data
->simd_size
- 1);
1439 pipeline
->cs_right_mask
= ~0u >> (32 - remainder
);
1441 pipeline
->cs_right_mask
= ~0u >> (32 - cs_prog_data
->simd_size
);
1443 const uint32_t vfe_curbe_allocation
=
1444 ALIGN(cs_prog_data
->push
.per_thread
.regs
* cs_prog_data
->threads
+
1445 cs_prog_data
->push
.cross_thread
.regs
, 2);
1447 const uint32_t subslices
= MAX2(physical_device
->subslice_total
, 1);
1449 anv_batch_emit(&pipeline
->batch
, GENX(MEDIA_VFE_STATE
), vfe
) {
1450 vfe
.ScratchSpaceBasePointer
= (struct anv_address
) {
1451 .bo
= anv_scratch_pool_alloc(device
, &device
->scratch_pool
,
1452 MESA_SHADER_COMPUTE
,
1453 cs_prog_data
->base
.total_scratch
),
1456 vfe
.PerThreadScratchSpace
= ffs(cs_prog_data
->base
.total_scratch
/ 2048);
1460 vfe
.GPGPUMode
= true;
1462 vfe
.MaximumNumberofThreads
=
1463 devinfo
->max_cs_threads
* subslices
- 1;
1464 vfe
.NumberofURBEntries
= GEN_GEN
<= 7 ? 0 : 2;
1465 vfe
.ResetGatewayTimer
= true;
1467 vfe
.BypassGatewayControl
= true;
1469 vfe
.URBEntryAllocationSize
= GEN_GEN
<= 7 ? 0 : 2;
1470 vfe
.CURBEAllocationSize
= vfe_curbe_allocation
;
1473 *pPipeline
= anv_pipeline_to_handle(pipeline
);
1478 VkResult
genX(CreateGraphicsPipelines
)(
1480 VkPipelineCache pipelineCache
,
1482 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
1483 const VkAllocationCallbacks
* pAllocator
,
1484 VkPipeline
* pPipelines
)
1486 ANV_FROM_HANDLE(anv_pipeline_cache
, pipeline_cache
, pipelineCache
);
1488 VkResult result
= VK_SUCCESS
;
1491 for (; i
< count
; i
++) {
1492 result
= genX(graphics_pipeline_create
)(_device
,
1495 pAllocator
, &pPipelines
[i
]);
1496 if (result
!= VK_SUCCESS
) {
1497 for (unsigned j
= 0; j
< i
; j
++) {
1498 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);
1508 VkResult
genX(CreateComputePipelines
)(
1510 VkPipelineCache pipelineCache
,
1512 const VkComputePipelineCreateInfo
* pCreateInfos
,
1513 const VkAllocationCallbacks
* pAllocator
,
1514 VkPipeline
* pPipelines
)
1516 ANV_FROM_HANDLE(anv_pipeline_cache
, pipeline_cache
, pipelineCache
);
1518 VkResult result
= VK_SUCCESS
;
1521 for (; i
< count
; i
++) {
1522 result
= compute_pipeline_create(_device
, pipeline_cache
,
1524 pAllocator
, &pPipelines
[i
]);
1525 if (result
!= VK_SUCCESS
) {
1526 for (unsigned j
= 0; j
< i
; j
++) {
1527 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);