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
30 #include "anv_private.h"
32 #include "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
35 #include "genX_pipeline_util.h"
38 gen7_emit_rs_state(struct anv_pipeline
*pipeline
,
39 const VkPipelineRasterizationStateCreateInfo
*info
,
40 const struct anv_graphics_pipeline_create_info
*extra
)
42 struct GENX(3DSTATE_SF
) sf
= {
43 GENX(3DSTATE_SF_header
),
45 /* LegacyGlobalDepthBiasEnable */
47 .StatisticsEnable
= true,
48 .FrontFaceFillMode
= vk_to_gen_fillmode
[info
->polygonMode
],
49 .BackFaceFillMode
= vk_to_gen_fillmode
[info
->polygonMode
],
50 .ViewTransformEnable
= !(extra
&& extra
->use_rectlist
),
51 .FrontWinding
= vk_to_gen_front_face
[info
->frontFace
],
52 /* bool AntiAliasingEnable; */
54 .CullMode
= vk_to_gen_cullmode
[info
->cullMode
],
56 /* uint32_t LineEndCapAntialiasingRegionWidth; */
57 .ScissorRectangleEnable
= !(extra
&& extra
->use_rectlist
),
59 /* uint32_t MultisampleRasterizationMode; */
60 /* bool LastPixelEnable; */
62 .TriangleStripListProvokingVertexSelect
= 0,
63 .LineStripListProvokingVertexSelect
= 0,
64 .TriangleFanProvokingVertexSelect
= 1,
66 /* uint32_t AALineDistanceMode; */
67 /* uint32_t VertexSubPixelPrecisionSelect; */
68 .UsePointWidthState
= false,
70 .GlobalDepthOffsetEnableSolid
= info
->depthBiasEnable
,
71 .GlobalDepthOffsetEnableWireframe
= info
->depthBiasEnable
,
72 .GlobalDepthOffsetEnablePoint
= info
->depthBiasEnable
,
75 GENX(3DSTATE_SF_pack
)(NULL
, &pipeline
->gen7
.sf
, &sf
);
79 gen7_emit_cb_state(struct anv_pipeline
*pipeline
,
80 const VkPipelineColorBlendStateCreateInfo
*info
,
81 const VkPipelineMultisampleStateCreateInfo
*ms_info
)
83 struct anv_device
*device
= pipeline
->device
;
85 if (info
== NULL
|| info
->attachmentCount
== 0) {
86 pipeline
->blend_state
=
87 anv_state_pool_emit(&device
->dynamic_state_pool
,
88 GENX(BLEND_STATE
), 64,
89 .ColorBufferBlendEnable
= false,
90 .WriteDisableAlpha
= true,
91 .WriteDisableRed
= true,
92 .WriteDisableGreen
= true,
93 .WriteDisableBlue
= true);
95 const VkPipelineColorBlendAttachmentState
*a
= &info
->pAttachments
[0];
96 struct GENX(BLEND_STATE
) blend
= {
97 .AlphaToCoverageEnable
= ms_info
&& ms_info
->alphaToCoverageEnable
,
98 .AlphaToOneEnable
= ms_info
&& ms_info
->alphaToOneEnable
,
100 .LogicOpEnable
= info
->logicOpEnable
,
101 .LogicOpFunction
= vk_to_gen_logic_op
[info
->logicOp
],
102 .ColorBufferBlendEnable
= a
->blendEnable
,
103 .ColorClampRange
= COLORCLAMP_RTFORMAT
,
104 .PreBlendColorClampEnable
= true,
105 .PostBlendColorClampEnable
= true,
106 .SourceBlendFactor
= vk_to_gen_blend
[a
->srcColorBlendFactor
],
107 .DestinationBlendFactor
= vk_to_gen_blend
[a
->dstColorBlendFactor
],
108 .ColorBlendFunction
= vk_to_gen_blend_op
[a
->colorBlendOp
],
109 .SourceAlphaBlendFactor
= vk_to_gen_blend
[a
->srcAlphaBlendFactor
],
110 .DestinationAlphaBlendFactor
= vk_to_gen_blend
[a
->dstAlphaBlendFactor
],
111 .AlphaBlendFunction
= vk_to_gen_blend_op
[a
->alphaBlendOp
],
112 .WriteDisableAlpha
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_A_BIT
),
113 .WriteDisableRed
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_R_BIT
),
114 .WriteDisableGreen
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_G_BIT
),
115 .WriteDisableBlue
= !(a
->colorWriteMask
& VK_COLOR_COMPONENT_B_BIT
),
118 /* Our hardware applies the blend factor prior to the blend function
119 * regardless of what function is used. Technically, this means the
120 * hardware can do MORE than GL or Vulkan specify. However, it also
121 * means that, for MIN and MAX, we have to stomp the blend factor to
122 * ONE to make it a no-op.
124 if (a
->colorBlendOp
== VK_BLEND_OP_MIN
||
125 a
->colorBlendOp
== VK_BLEND_OP_MAX
) {
126 blend
.SourceBlendFactor
= BLENDFACTOR_ONE
;
127 blend
.DestinationBlendFactor
= BLENDFACTOR_ONE
;
129 if (a
->alphaBlendOp
== VK_BLEND_OP_MIN
||
130 a
->alphaBlendOp
== VK_BLEND_OP_MAX
) {
131 blend
.SourceAlphaBlendFactor
= BLENDFACTOR_ONE
;
132 blend
.DestinationAlphaBlendFactor
= BLENDFACTOR_ONE
;
135 pipeline
->blend_state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
136 GENX(BLEND_STATE_length
) * 4,
138 GENX(BLEND_STATE_pack
)(NULL
, pipeline
->blend_state
.map
, &blend
);
139 if (pipeline
->device
->info
.has_llc
)
140 anv_state_clflush(pipeline
->blend_state
);
143 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_BLEND_STATE_POINTERS
), bsp
) {
144 bsp
.BlendStatePointer
= pipeline
->blend_state
.offset
;
149 genX(graphics_pipeline_create
)(
151 struct anv_pipeline_cache
* cache
,
152 const VkGraphicsPipelineCreateInfo
* pCreateInfo
,
153 const struct anv_graphics_pipeline_create_info
*extra
,
154 const VkAllocationCallbacks
* pAllocator
,
155 VkPipeline
* pPipeline
)
157 ANV_FROM_HANDLE(anv_device
, device
, _device
);
158 struct anv_pipeline
*pipeline
;
161 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
163 pipeline
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*pipeline
), 8,
164 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
165 if (pipeline
== NULL
)
166 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
168 result
= anv_pipeline_init(pipeline
, device
, cache
,
169 pCreateInfo
, extra
, pAllocator
);
170 if (result
!= VK_SUCCESS
) {
171 anv_free2(&device
->alloc
, pAllocator
, pipeline
);
175 assert(pCreateInfo
->pVertexInputState
);
176 emit_vertex_input(pipeline
, pCreateInfo
->pVertexInputState
, extra
);
178 assert(pCreateInfo
->pRasterizationState
);
179 gen7_emit_rs_state(pipeline
, pCreateInfo
->pRasterizationState
, extra
);
181 emit_ds_state(pipeline
, pCreateInfo
->pDepthStencilState
);
183 gen7_emit_cb_state(pipeline
, pCreateInfo
->pColorBlendState
,
184 pCreateInfo
->pMultisampleState
);
186 emit_urb_setup(pipeline
);
188 const VkPipelineRasterizationStateCreateInfo
*rs_info
=
189 pCreateInfo
->pRasterizationState
;
191 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_CLIP
), clip
) {
192 clip
.FrontWinding
= vk_to_gen_front_face
[rs_info
->frontFace
],
193 clip
.CullMode
= vk_to_gen_cullmode
[rs_info
->cullMode
],
194 clip
.ClipEnable
= !(extra
&& extra
->use_rectlist
),
195 clip
.APIMode
= APIMODE_OGL
,
196 clip
.ViewportXYClipTestEnable
= true,
197 clip
.ClipMode
= CLIPMODE_NORMAL
,
199 clip
.TriangleStripListProvokingVertexSelect
= 0,
200 clip
.LineStripListProvokingVertexSelect
= 0,
201 clip
.TriangleFanProvokingVertexSelect
= 1,
203 clip
.MinimumPointWidth
= 0.125,
204 clip
.MaximumPointWidth
= 255.875,
205 clip
.MaximumVPIndex
= pCreateInfo
->pViewportState
->viewportCount
- 1;
208 if (pCreateInfo
->pMultisampleState
&&
209 pCreateInfo
->pMultisampleState
->rasterizationSamples
> 1)
210 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
212 uint32_t samples
= 1;
213 uint32_t log2_samples
= __builtin_ffs(samples
) - 1;
215 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_MULTISAMPLE
), ms
) {
216 ms
.PixelLocation
= PIXLOC_CENTER
;
217 ms
.NumberofMultisamples
= log2_samples
;
220 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_SAMPLE_MASK
), sm
) {
221 sm
.SampleMask
= 0xff;
224 const struct brw_vs_prog_data
*vs_prog_data
= get_vs_prog_data(pipeline
);
227 /* From gen7_vs_state.c */
230 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
231 * Geometry > Geometry Shader > State:
233 * "Note: Because of corruption in IVB:GT2, software needs to flush the
234 * whole fixed function pipeline when the GS enable changes value in
237 * The hardware architects have clarified that in this context "flush the
238 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
241 if (!brw
->is_haswell
&& !brw
->is_baytrail
)
242 gen7_emit_vs_workaround_flush(brw
);
245 if (pipeline
->vs_vec4
== NO_KERNEL
|| (extra
&& extra
->disable_vs
))
246 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_VS
), vs
);
248 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_VS
), vs
) {
249 vs
.KernelStartPointer
= pipeline
->vs_vec4
;
250 vs
.ScratchSpaceBaseOffset
= pipeline
->scratch_start
[MESA_SHADER_VERTEX
];
251 vs
.PerThreadScratchSpace
= scratch_space(&vs_prog_data
->base
.base
);
253 vs
.DispatchGRFStartRegisterforURBData
=
254 vs_prog_data
->base
.base
.dispatch_grf_start_reg
;
256 vs
.VertexURBEntryReadLength
= vs_prog_data
->base
.urb_read_length
;
257 vs
.VertexURBEntryReadOffset
= 0;
258 vs
.MaximumNumberofThreads
= device
->info
.max_vs_threads
- 1;
259 vs
.StatisticsEnable
= true;
260 vs
.VSFunctionEnable
= true;
263 const struct brw_gs_prog_data
*gs_prog_data
= get_gs_prog_data(pipeline
);
265 if (pipeline
->gs_kernel
== NO_KERNEL
|| (extra
&& extra
->disable_vs
)) {
266 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_GS
), gs
);
268 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_GS
), gs
) {
269 gs
.KernelStartPointer
= pipeline
->gs_kernel
;
270 gs
.ScratchSpaceBasePointer
= pipeline
->scratch_start
[MESA_SHADER_GEOMETRY
];
271 gs
.PerThreadScratchSpace
= scratch_space(&gs_prog_data
->base
.base
);
273 gs
.OutputVertexSize
= gs_prog_data
->output_vertex_size_hwords
* 2 - 1;
274 gs
.OutputTopology
= gs_prog_data
->output_topology
;
275 gs
.VertexURBEntryReadLength
= gs_prog_data
->base
.urb_read_length
;
276 gs
.IncludeVertexHandles
= gs_prog_data
->base
.include_vue_handles
;
278 gs
.DispatchGRFStartRegisterforURBData
=
279 gs_prog_data
->base
.base
.dispatch_grf_start_reg
;
281 gs
.MaximumNumberofThreads
= device
->info
.max_gs_threads
- 1;
282 /* This in the next dword on HSW. */
283 gs
.ControlDataFormat
= gs_prog_data
->control_data_format
;
284 gs
.ControlDataHeaderSize
= gs_prog_data
->control_data_header_size_hwords
;
285 gs
.InstanceControl
= MAX2(gs_prog_data
->invocations
, 1) - 1;
286 gs
.DispatchMode
= gs_prog_data
->base
.dispatch_mode
;
287 gs
.GSStatisticsEnable
= true;
288 gs
.IncludePrimitiveID
= gs_prog_data
->include_primitive_id
;
289 # if (GEN_IS_HASWELL)
290 gs
.ReorderMode
= REORDER_TRAILING
;
292 gs
.ReorderEnable
= true;
298 if (pipeline
->ps_ksp0
== NO_KERNEL
) {
299 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_SBE
), sbe
);
301 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_WM
), wm
) {
302 wm
.StatisticsEnable
= true;
303 wm
.ThreadDispatchEnable
= false;
304 wm
.LineEndCapAntialiasingRegionWidth
= 0; /* 0.5 pixels */
305 wm
.LineAntialiasingRegionWidth
= 1; /* 1.0 pixels */
306 wm
.EarlyDepthStencilControl
= EDSC_NORMAL
;
307 wm
.PointRasterizationRule
= RASTRULE_UPPER_RIGHT
;
310 /* Even if no fragments are ever dispatched, the hardware hangs if we
311 * don't at least set the maximum number of threads.
313 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_PS
), ps
) {
314 ps
.MaximumNumberofThreads
= device
->info
.max_wm_threads
- 1;
317 const struct brw_wm_prog_data
*wm_prog_data
= get_wm_prog_data(pipeline
);
318 if (wm_prog_data
->urb_setup
[VARYING_SLOT_BFC0
] != -1 ||
319 wm_prog_data
->urb_setup
[VARYING_SLOT_BFC1
] != -1)
320 anv_finishme("two-sided color needs sbe swizzling setup");
321 if (wm_prog_data
->urb_setup
[VARYING_SLOT_PRIMITIVE_ID
] != -1)
322 anv_finishme("primitive_id needs sbe swizzling setup");
324 emit_3dstate_sbe(pipeline
);
326 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_PS
), ps
) {
327 ps
.KernelStartPointer0
= pipeline
->ps_ksp0
;
328 ps
.ScratchSpaceBasePointer
= pipeline
->scratch_start
[MESA_SHADER_FRAGMENT
];
329 ps
.PerThreadScratchSpace
= scratch_space(&wm_prog_data
->base
);
330 ps
.MaximumNumberofThreads
= device
->info
.max_wm_threads
- 1;
331 ps
.PushConstantEnable
= wm_prog_data
->base
.nr_params
> 0;
332 ps
.AttributeEnable
= wm_prog_data
->num_varying_inputs
> 0;
333 ps
.oMaskPresenttoRenderTarget
= wm_prog_data
->uses_omask
;
335 ps
.RenderTargetFastClearEnable
= false;
336 ps
.DualSourceBlendEnable
= false;
337 ps
.RenderTargetResolveEnable
= false;
339 ps
.PositionXYOffsetSelect
= wm_prog_data
->uses_pos_offset
?
340 POSOFFSET_SAMPLE
: POSOFFSET_NONE
;
342 ps
._32PixelDispatchEnable
= false;
343 ps
._16PixelDispatchEnable
= wm_prog_data
->dispatch_16
;
344 ps
._8PixelDispatchEnable
= wm_prog_data
->dispatch_8
;
346 ps
.DispatchGRFStartRegisterforConstantSetupData0
=
347 wm_prog_data
->base
.dispatch_grf_start_reg
,
348 ps
.DispatchGRFStartRegisterforConstantSetupData1
= 0,
349 ps
.DispatchGRFStartRegisterforConstantSetupData2
=
350 wm_prog_data
->dispatch_grf_start_reg_2
,
352 /* Haswell requires the sample mask to be set in this packet as well as
353 * in 3DSTATE_SAMPLE_MASK; the values should match. */
354 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
356 ps
.KernelStartPointer1
= 0;
357 ps
.KernelStartPointer2
= pipeline
->ps_ksp0
+ wm_prog_data
->prog_offset_2
;
360 /* FIXME-GEN7: This needs a lot more work, cf gen7 upload_wm_state(). */
361 anv_batch_emit(&pipeline
->batch
, GENX(3DSTATE_WM
), wm
) {
362 wm
.StatisticsEnable
= true;
363 wm
.ThreadDispatchEnable
= true;
364 wm
.LineEndCapAntialiasingRegionWidth
= 0; /* 0.5 pixels */
365 wm
.LineAntialiasingRegionWidth
= 1; /* 1.0 pixels */
366 wm
.EarlyDepthStencilControl
= EDSC_NORMAL
;
367 wm
.PointRasterizationRule
= RASTRULE_UPPER_RIGHT
;
368 wm
.PixelShaderComputedDepthMode
= wm_prog_data
->computed_depth_mode
;
369 wm
.PixelShaderUsesSourceDepth
= wm_prog_data
->uses_src_depth
;
370 wm
.PixelShaderUsesSourceW
= wm_prog_data
->uses_src_w
;
371 wm
.PixelShaderUsesInputCoverageMask
= wm_prog_data
->uses_sample_mask
;
372 wm
.BarycentricInterpolationMode
= wm_prog_data
->barycentric_interp_modes
;
376 *pPipeline
= anv_pipeline_to_handle(pipeline
);