anv/pipeline: Respect pRasterizationState->depthBiasEnable
[mesa.git] / src / intel / vulkan / gen7_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 <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "genxml/gen_macros.h"
33 #include "genxml/genX_pack.h"
34
35 #include "genX_pipeline_util.h"
36
37 static void
38 gen7_emit_rs_state(struct anv_pipeline *pipeline,
39 const VkPipelineRasterizationStateCreateInfo *info,
40 const struct anv_graphics_pipeline_create_info *extra)
41 {
42 struct GENX(3DSTATE_SF) sf = {
43 GENX(3DSTATE_SF_header),
44
45 /* LegacyGlobalDepthBiasEnable */
46
47 .StatisticsEnable = true,
48 .FrontFaceFillMode = vk_to_gen_fillmode[info->polygonMode],
49 .BackFaceFillMode = vk_to_gen_fillmode[info->polygonMode],
50 .ViewTransformEnable = !(extra && extra->disable_viewport),
51 .FrontWinding = vk_to_gen_front_face[info->frontFace],
52 /* bool AntiAliasingEnable; */
53
54 .CullMode = vk_to_gen_cullmode[info->cullMode],
55
56 /* uint32_t LineEndCapAntialiasingRegionWidth; */
57 .ScissorRectangleEnable = !(extra && extra->disable_scissor),
58
59 /* uint32_t MultisampleRasterizationMode; */
60 /* bool LastPixelEnable; */
61
62 .TriangleStripListProvokingVertexSelect = 0,
63 .LineStripListProvokingVertexSelect = 0,
64 .TriangleFanProvokingVertexSelect = 0,
65
66 /* uint32_t AALineDistanceMode; */
67 /* uint32_t VertexSubPixelPrecisionSelect; */
68 .UsePointWidthState = !pipeline->writes_point_size,
69 .PointWidth = 1.0,
70 .GlobalDepthOffsetConstant = info->depthBiasEnable,
71 .GlobalDepthOffsetScale = info->depthBiasEnable,
72 .GlobalDepthOffsetClamp = info->depthBiasEnable,
73 };
74
75 GENX(3DSTATE_SF_pack)(NULL, &pipeline->gen7.sf, &sf);
76 }
77
78 static void
79 gen7_emit_ds_state(struct anv_pipeline *pipeline,
80 const VkPipelineDepthStencilStateCreateInfo *info)
81 {
82 if (info == NULL) {
83 /* We're going to OR this together with the dynamic state. We need
84 * to make sure it's initialized to something useful.
85 */
86 memset(pipeline->gen7.depth_stencil_state, 0,
87 sizeof(pipeline->gen7.depth_stencil_state));
88 return;
89 }
90
91 struct GENX(DEPTH_STENCIL_STATE) state = {
92 .DepthTestEnable = info->depthTestEnable,
93 .DepthBufferWriteEnable = info->depthWriteEnable,
94 .DepthTestFunction = vk_to_gen_compare_op[info->depthCompareOp],
95 .DoubleSidedStencilEnable = true,
96
97 .StencilTestEnable = info->stencilTestEnable,
98 .StencilFailOp = vk_to_gen_stencil_op[info->front.failOp],
99 .StencilPassDepthPassOp = vk_to_gen_stencil_op[info->front.passOp],
100 .StencilPassDepthFailOp = vk_to_gen_stencil_op[info->front.depthFailOp],
101 .StencilTestFunction = vk_to_gen_compare_op[info->front.compareOp],
102
103 .BackfaceStencilFailOp = vk_to_gen_stencil_op[info->back.failOp],
104 .BackfaceStencilPassDepthPassOp = vk_to_gen_stencil_op[info->back.passOp],
105 .BackfaceStencilPassDepthFailOp = vk_to_gen_stencil_op[info->back.depthFailOp],
106 .BackFaceStencilTestFunction = vk_to_gen_compare_op[info->back.compareOp],
107 };
108
109 GENX(DEPTH_STENCIL_STATE_pack)(NULL, &pipeline->gen7.depth_stencil_state, &state);
110 }
111
112 static void
113 gen7_emit_cb_state(struct anv_pipeline *pipeline,
114 const VkPipelineColorBlendStateCreateInfo *info,
115 const VkPipelineMultisampleStateCreateInfo *ms_info)
116 {
117 struct anv_device *device = pipeline->device;
118
119 if (info == NULL || info->attachmentCount == 0) {
120 pipeline->blend_state =
121 anv_state_pool_emit(&device->dynamic_state_pool,
122 GENX(BLEND_STATE), 64,
123 .ColorBufferBlendEnable = false,
124 .WriteDisableAlpha = true,
125 .WriteDisableRed = true,
126 .WriteDisableGreen = true,
127 .WriteDisableBlue = true);
128 } else {
129 const VkPipelineColorBlendAttachmentState *a = &info->pAttachments[0];
130 struct GENX(BLEND_STATE) blend = {
131 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
132 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
133
134 .LogicOpEnable = info->logicOpEnable,
135 .LogicOpFunction = vk_to_gen_logic_op[info->logicOp],
136 .ColorBufferBlendEnable = a->blendEnable,
137 .ColorClampRange = COLORCLAMP_RTFORMAT,
138 .PreBlendColorClampEnable = true,
139 .PostBlendColorClampEnable = true,
140 .SourceBlendFactor = vk_to_gen_blend[a->srcColorBlendFactor],
141 .DestinationBlendFactor = vk_to_gen_blend[a->dstColorBlendFactor],
142 .ColorBlendFunction = vk_to_gen_blend_op[a->colorBlendOp],
143 .SourceAlphaBlendFactor = vk_to_gen_blend[a->srcAlphaBlendFactor],
144 .DestinationAlphaBlendFactor = vk_to_gen_blend[a->dstAlphaBlendFactor],
145 .AlphaBlendFunction = vk_to_gen_blend_op[a->alphaBlendOp],
146 .WriteDisableAlpha = !(a->colorWriteMask & VK_COLOR_COMPONENT_A_BIT),
147 .WriteDisableRed = !(a->colorWriteMask & VK_COLOR_COMPONENT_R_BIT),
148 .WriteDisableGreen = !(a->colorWriteMask & VK_COLOR_COMPONENT_G_BIT),
149 .WriteDisableBlue = !(a->colorWriteMask & VK_COLOR_COMPONENT_B_BIT),
150 };
151
152 /* Our hardware applies the blend factor prior to the blend function
153 * regardless of what function is used. Technically, this means the
154 * hardware can do MORE than GL or Vulkan specify. However, it also
155 * means that, for MIN and MAX, we have to stomp the blend factor to
156 * ONE to make it a no-op.
157 */
158 if (a->colorBlendOp == VK_BLEND_OP_MIN ||
159 a->colorBlendOp == VK_BLEND_OP_MAX) {
160 blend.SourceBlendFactor = BLENDFACTOR_ONE;
161 blend.DestinationBlendFactor = BLENDFACTOR_ONE;
162 }
163 if (a->alphaBlendOp == VK_BLEND_OP_MIN ||
164 a->alphaBlendOp == VK_BLEND_OP_MAX) {
165 blend.SourceAlphaBlendFactor = BLENDFACTOR_ONE;
166 blend.DestinationAlphaBlendFactor = BLENDFACTOR_ONE;
167 }
168
169 pipeline->blend_state = anv_state_pool_alloc(&device->dynamic_state_pool,
170 GENX(BLEND_STATE_length) * 4,
171 64);
172 GENX(BLEND_STATE_pack)(NULL, pipeline->blend_state.map, &blend);
173 if (pipeline->device->info.has_llc)
174 anv_state_clflush(pipeline->blend_state);
175 }
176
177 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_BLEND_STATE_POINTERS),
178 .BlendStatePointer = pipeline->blend_state.offset);
179 }
180
181 VkResult
182 genX(graphics_pipeline_create)(
183 VkDevice _device,
184 struct anv_pipeline_cache * cache,
185 const VkGraphicsPipelineCreateInfo* pCreateInfo,
186 const struct anv_graphics_pipeline_create_info *extra,
187 const VkAllocationCallbacks* pAllocator,
188 VkPipeline* pPipeline)
189 {
190 ANV_FROM_HANDLE(anv_device, device, _device);
191 struct anv_pipeline *pipeline;
192 VkResult result;
193
194 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
195
196 pipeline = anv_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
197 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
198 if (pipeline == NULL)
199 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
200
201 result = anv_pipeline_init(pipeline, device, cache,
202 pCreateInfo, extra, pAllocator);
203 if (result != VK_SUCCESS) {
204 anv_free2(&device->alloc, pAllocator, pipeline);
205 return result;
206 }
207
208 assert(pCreateInfo->pVertexInputState);
209 emit_vertex_input(pipeline, pCreateInfo->pVertexInputState, extra);
210
211 assert(pCreateInfo->pRasterizationState);
212 gen7_emit_rs_state(pipeline, pCreateInfo->pRasterizationState, extra);
213
214 gen7_emit_ds_state(pipeline, pCreateInfo->pDepthStencilState);
215
216 gen7_emit_cb_state(pipeline, pCreateInfo->pColorBlendState,
217 pCreateInfo->pMultisampleState);
218
219 emit_urb_setup(pipeline);
220
221 const VkPipelineRasterizationStateCreateInfo *rs_info =
222 pCreateInfo->pRasterizationState;
223
224 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_CLIP),
225 .FrontWinding = vk_to_gen_front_face[rs_info->frontFace],
226 .CullMode = vk_to_gen_cullmode[rs_info->cullMode],
227 .ClipEnable = true,
228 .APIMode = APIMODE_OGL,
229 .ViewportXYClipTestEnable = !(extra && extra->disable_viewport),
230 .ClipMode = CLIPMODE_NORMAL,
231 .TriangleStripListProvokingVertexSelect = 0,
232 .LineStripListProvokingVertexSelect = 0,
233 .TriangleFanProvokingVertexSelect = 0,
234 .MinimumPointWidth = 0.125,
235 .MaximumPointWidth = 255.875,
236 .MaximumVPIndex = pCreateInfo->pViewportState->viewportCount - 1);
237
238 if (pCreateInfo->pMultisampleState &&
239 pCreateInfo->pMultisampleState->rasterizationSamples > 1)
240 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
241
242 uint32_t samples = 1;
243 uint32_t log2_samples = __builtin_ffs(samples) - 1;
244
245 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_MULTISAMPLE),
246 .PixelLocation = PIXLOC_CENTER,
247 .NumberofMultisamples = log2_samples);
248
249 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SAMPLE_MASK),
250 .SampleMask = 0xff);
251
252 const struct brw_vue_prog_data *vue_prog_data = &pipeline->vs_prog_data.base;
253
254 #if 0
255 /* From gen7_vs_state.c */
256
257 /**
258 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
259 * Geometry > Geometry Shader > State:
260 *
261 * "Note: Because of corruption in IVB:GT2, software needs to flush the
262 * whole fixed function pipeline when the GS enable changes value in
263 * the 3DSTATE_GS."
264 *
265 * The hardware architects have clarified that in this context "flush the
266 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
267 * Stall" bit set.
268 */
269 if (!brw->is_haswell && !brw->is_baytrail)
270 gen7_emit_vs_workaround_flush(brw);
271 #endif
272
273 if (pipeline->vs_vec4 == NO_KERNEL || (extra && extra->disable_vs))
274 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS), .VSFunctionEnable = false);
275 else
276 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS),
277 .KernelStartPointer = pipeline->vs_vec4,
278 .ScratchSpaceBaseOffset = pipeline->scratch_start[MESA_SHADER_VERTEX],
279 .PerThreadScratchSpace = scratch_space(&vue_prog_data->base),
280
281 .DispatchGRFStartRegisterforURBData =
282 vue_prog_data->base.dispatch_grf_start_reg,
283 .VertexURBEntryReadLength = vue_prog_data->urb_read_length,
284 .VertexURBEntryReadOffset = 0,
285
286 .MaximumNumberofThreads = device->info.max_vs_threads - 1,
287 .StatisticsEnable = true,
288 .VSFunctionEnable = true);
289
290 const struct brw_gs_prog_data *gs_prog_data = &pipeline->gs_prog_data;
291
292 if (pipeline->gs_kernel == NO_KERNEL || (extra && extra->disable_vs)) {
293 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), .GSEnable = false);
294 } else {
295 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS),
296 .KernelStartPointer = pipeline->gs_kernel,
297 .ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_GEOMETRY],
298 .PerThreadScratchSpace = scratch_space(&gs_prog_data->base.base),
299
300 .OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1,
301 .OutputTopology = gs_prog_data->output_topology,
302 .VertexURBEntryReadLength = gs_prog_data->base.urb_read_length,
303 .IncludeVertexHandles = gs_prog_data->base.include_vue_handles,
304 .DispatchGRFStartRegisterforURBData =
305 gs_prog_data->base.base.dispatch_grf_start_reg,
306
307 .MaximumNumberofThreads = device->info.max_gs_threads - 1,
308 /* This in the next dword on HSW. */
309 .ControlDataFormat = gs_prog_data->control_data_format,
310 .ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords,
311 .InstanceControl = MAX2(gs_prog_data->invocations, 1) - 1,
312 .DispatchMode = gs_prog_data->base.dispatch_mode,
313 .GSStatisticsEnable = true,
314 .IncludePrimitiveID = gs_prog_data->include_primitive_id,
315 # if (GEN_IS_HASWELL)
316 .ReorderMode = REORDER_TRAILING,
317 # else
318 .ReorderEnable = true,
319 # endif
320 .GSEnable = true);
321 }
322
323 if (pipeline->ps_ksp0 == NO_KERNEL) {
324 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SBE));
325
326 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM),
327 .StatisticsEnable = true,
328 .ThreadDispatchEnable = false,
329 .LineEndCapAntialiasingRegionWidth = 0, /* 0.5 pixels */
330 .LineAntialiasingRegionWidth = 1, /* 1.0 pixels */
331 .EarlyDepthStencilControl = EDSC_NORMAL,
332 .PointRasterizationRule = RASTRULE_UPPER_RIGHT);
333
334 /* Even if no fragments are ever dispatched, the hardware hangs if we
335 * don't at least set the maximum number of threads.
336 */
337 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS),
338 .MaximumNumberofThreads = device->info.max_wm_threads - 1);
339 } else {
340 const struct brw_wm_prog_data *wm_prog_data = &pipeline->wm_prog_data;
341 if (wm_prog_data->urb_setup[VARYING_SLOT_BFC0] != -1 ||
342 wm_prog_data->urb_setup[VARYING_SLOT_BFC1] != -1)
343 anv_finishme("two-sided color needs sbe swizzling setup");
344 if (wm_prog_data->urb_setup[VARYING_SLOT_PRIMITIVE_ID] != -1)
345 anv_finishme("primitive_id needs sbe swizzling setup");
346
347 emit_3dstate_sbe(pipeline);
348
349 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS),
350 .KernelStartPointer0 = pipeline->ps_ksp0,
351 .ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_FRAGMENT],
352 .PerThreadScratchSpace = scratch_space(&wm_prog_data->base),
353
354 .MaximumNumberofThreads = device->info.max_wm_threads - 1,
355 .PushConstantEnable = wm_prog_data->base.nr_params > 0,
356 .AttributeEnable = wm_prog_data->num_varying_inputs > 0,
357 .oMaskPresenttoRenderTarget = wm_prog_data->uses_omask,
358
359 .RenderTargetFastClearEnable = false,
360 .DualSourceBlendEnable = false,
361 .RenderTargetResolveEnable = false,
362
363 .PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
364 POSOFFSET_SAMPLE : POSOFFSET_NONE,
365
366 ._32PixelDispatchEnable = false,
367 ._16PixelDispatchEnable = pipeline->ps_simd16 != NO_KERNEL,
368 ._8PixelDispatchEnable = pipeline->ps_simd8 != NO_KERNEL,
369
370 .DispatchGRFStartRegisterforConstantSetupData0 = pipeline->ps_grf_start0,
371 .DispatchGRFStartRegisterforConstantSetupData1 = 0,
372 .DispatchGRFStartRegisterforConstantSetupData2 = pipeline->ps_grf_start2,
373
374 #if 0
375 /* Haswell requires the sample mask to be set in this packet as well as
376 * in 3DSTATE_SAMPLE_MASK; the values should match. */
377 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
378 #endif
379
380 .KernelStartPointer1 = 0,
381 .KernelStartPointer2 = pipeline->ps_ksp2);
382
383 /* FIXME-GEN7: This needs a lot more work, cf gen7 upload_wm_state(). */
384 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM),
385 .StatisticsEnable = true,
386 .ThreadDispatchEnable = true,
387 .LineEndCapAntialiasingRegionWidth = 0, /* 0.5 pixels */
388 .LineAntialiasingRegionWidth = 1, /* 1.0 pixels */
389 .EarlyDepthStencilControl = EDSC_NORMAL,
390 .PointRasterizationRule = RASTRULE_UPPER_RIGHT,
391 .PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode,
392 .PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth,
393 .PixelShaderUsesSourceW = wm_prog_data->uses_src_w,
394 .PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask,
395 .BarycentricInterpolationMode = wm_prog_data->barycentric_interp_modes);
396 }
397
398 *pPipeline = anv_pipeline_to_handle(pipeline);
399
400 return VK_SUCCESS;
401 }