anv/pipeline: Silently pass tests if depth or stencil is missing
[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->use_rectlist),
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->use_rectlist),
58
59 /* uint32_t MultisampleRasterizationMode; */
60 /* bool LastPixelEnable; */
61
62 .TriangleStripListProvokingVertexSelect = 0,
63 .LineStripListProvokingVertexSelect = 0,
64 .TriangleFanProvokingVertexSelect = 1,
65
66 /* uint32_t AALineDistanceMode; */
67 /* uint32_t VertexSubPixelPrecisionSelect; */
68 .UsePointWidthState = false,
69 .PointWidth = 1.0,
70 .GlobalDepthOffsetEnableSolid = info->depthBiasEnable,
71 .GlobalDepthOffsetEnableWireframe = info->depthBiasEnable,
72 .GlobalDepthOffsetEnablePoint = info->depthBiasEnable,
73 };
74
75 GENX(3DSTATE_SF_pack)(NULL, &pipeline->gen7.sf, &sf);
76 }
77
78 static void
79 gen7_emit_cb_state(struct anv_pipeline *pipeline,
80 const VkPipelineColorBlendStateCreateInfo *info,
81 const VkPipelineMultisampleStateCreateInfo *ms_info)
82 {
83 struct anv_device *device = pipeline->device;
84
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);
94 } else {
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,
99
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),
116 };
117
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.
123 */
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;
128 }
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;
133 }
134
135 pipeline->blend_state = anv_state_pool_alloc(&device->dynamic_state_pool,
136 GENX(BLEND_STATE_length) * 4,
137 64);
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);
141 }
142
143 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_BLEND_STATE_POINTERS), bsp) {
144 bsp.BlendStatePointer = pipeline->blend_state.offset;
145 }
146 }
147
148 VkResult
149 genX(graphics_pipeline_create)(
150 VkDevice _device,
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)
156 {
157 ANV_FROM_HANDLE(anv_device, device, _device);
158 ANV_FROM_HANDLE(anv_render_pass, pass, pCreateInfo->renderPass);
159 struct anv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
160 struct anv_pipeline *pipeline;
161 VkResult result;
162
163 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
164
165 pipeline = anv_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
166 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
167 if (pipeline == NULL)
168 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
169
170 result = anv_pipeline_init(pipeline, device, cache,
171 pCreateInfo, extra, pAllocator);
172 if (result != VK_SUCCESS) {
173 anv_free2(&device->alloc, pAllocator, pipeline);
174 return result;
175 }
176
177 assert(pCreateInfo->pVertexInputState);
178 emit_vertex_input(pipeline, pCreateInfo->pVertexInputState, extra);
179
180 assert(pCreateInfo->pRasterizationState);
181 gen7_emit_rs_state(pipeline, pCreateInfo->pRasterizationState, extra);
182
183 emit_ds_state(pipeline, pCreateInfo->pDepthStencilState, pass, subpass);
184
185 gen7_emit_cb_state(pipeline, pCreateInfo->pColorBlendState,
186 pCreateInfo->pMultisampleState);
187
188 emit_urb_setup(pipeline);
189
190 const VkPipelineRasterizationStateCreateInfo *rs_info =
191 pCreateInfo->pRasterizationState;
192
193 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_CLIP), clip) {
194 clip.FrontWinding = vk_to_gen_front_face[rs_info->frontFace],
195 clip.CullMode = vk_to_gen_cullmode[rs_info->cullMode],
196 clip.ClipEnable = !(extra && extra->use_rectlist),
197 clip.APIMode = APIMODE_OGL,
198 clip.ViewportXYClipTestEnable = true,
199 clip.ClipMode = CLIPMODE_NORMAL,
200
201 clip.TriangleStripListProvokingVertexSelect = 0,
202 clip.LineStripListProvokingVertexSelect = 0,
203 clip.TriangleFanProvokingVertexSelect = 1,
204
205 clip.MinimumPointWidth = 0.125,
206 clip.MaximumPointWidth = 255.875,
207 clip.MaximumVPIndex = pCreateInfo->pViewportState->viewportCount - 1;
208 }
209
210 if (pCreateInfo->pMultisampleState &&
211 pCreateInfo->pMultisampleState->rasterizationSamples > 1)
212 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
213
214 uint32_t samples = 1;
215 uint32_t log2_samples = __builtin_ffs(samples) - 1;
216
217 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_MULTISAMPLE), ms) {
218 ms.PixelLocation = PIXLOC_CENTER;
219 ms.NumberofMultisamples = log2_samples;
220 }
221
222 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SAMPLE_MASK), sm) {
223 sm.SampleMask = 0xff;
224 }
225
226 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
227
228 #if 0
229 /* From gen7_vs_state.c */
230
231 /**
232 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
233 * Geometry > Geometry Shader > State:
234 *
235 * "Note: Because of corruption in IVB:GT2, software needs to flush the
236 * whole fixed function pipeline when the GS enable changes value in
237 * the 3DSTATE_GS."
238 *
239 * The hardware architects have clarified that in this context "flush the
240 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
241 * Stall" bit set.
242 */
243 if (!brw->is_haswell && !brw->is_baytrail)
244 gen7_emit_vs_workaround_flush(brw);
245 #endif
246
247 if (pipeline->vs_vec4 == NO_KERNEL || (extra && extra->disable_vs))
248 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS), vs);
249 else
250 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS), vs) {
251 vs.KernelStartPointer = pipeline->vs_vec4;
252 vs.ScratchSpaceBaseOffset = pipeline->scratch_start[MESA_SHADER_VERTEX];
253 vs.PerThreadScratchSpace = scratch_space(&vs_prog_data->base.base);
254
255 vs.DispatchGRFStartRegisterforURBData =
256 vs_prog_data->base.base.dispatch_grf_start_reg;
257
258 vs.VertexURBEntryReadLength = vs_prog_data->base.urb_read_length;
259 vs.VertexURBEntryReadOffset = 0;
260 vs.MaximumNumberofThreads = device->info.max_vs_threads - 1;
261 vs.StatisticsEnable = true;
262 vs.VSFunctionEnable = true;
263 }
264
265 const struct brw_gs_prog_data *gs_prog_data = get_gs_prog_data(pipeline);
266
267 if (pipeline->gs_kernel == NO_KERNEL || (extra && extra->disable_vs)) {
268 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), gs);
269 } else {
270 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), gs) {
271 gs.KernelStartPointer = pipeline->gs_kernel;
272 gs.ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_GEOMETRY];
273 gs.PerThreadScratchSpace = scratch_space(&gs_prog_data->base.base);
274
275 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
276 gs.OutputTopology = gs_prog_data->output_topology;
277 gs.VertexURBEntryReadLength = gs_prog_data->base.urb_read_length;
278 gs.IncludeVertexHandles = gs_prog_data->base.include_vue_handles;
279
280 gs.DispatchGRFStartRegisterforURBData =
281 gs_prog_data->base.base.dispatch_grf_start_reg;
282
283 gs.MaximumNumberofThreads = device->info.max_gs_threads - 1;
284 /* This in the next dword on HSW. */
285 gs.ControlDataFormat = gs_prog_data->control_data_format;
286 gs.ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords;
287 gs.InstanceControl = MAX2(gs_prog_data->invocations, 1) - 1;
288 gs.DispatchMode = gs_prog_data->base.dispatch_mode;
289 gs.GSStatisticsEnable = true;
290 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
291 # if (GEN_IS_HASWELL)
292 gs.ReorderMode = REORDER_TRAILING;
293 # else
294 gs.ReorderEnable = true;
295 # endif
296 gs.GSEnable = true;
297 }
298 }
299
300 if (pipeline->ps_ksp0 == NO_KERNEL) {
301 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SBE), sbe);
302
303 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM), wm) {
304 wm.StatisticsEnable = true;
305 wm.ThreadDispatchEnable = false;
306 wm.LineEndCapAntialiasingRegionWidth = 0; /* 0.5 pixels */
307 wm.LineAntialiasingRegionWidth = 1; /* 1.0 pixels */
308 wm.EarlyDepthStencilControl = EDSC_NORMAL;
309 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
310 }
311
312 /* Even if no fragments are ever dispatched, the hardware hangs if we
313 * don't at least set the maximum number of threads.
314 */
315 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS), ps) {
316 ps.MaximumNumberofThreads = device->info.max_wm_threads - 1;
317 }
318 } else {
319 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
320 if (wm_prog_data->urb_setup[VARYING_SLOT_BFC0] != -1 ||
321 wm_prog_data->urb_setup[VARYING_SLOT_BFC1] != -1)
322 anv_finishme("two-sided color needs sbe swizzling setup");
323 if (wm_prog_data->urb_setup[VARYING_SLOT_PRIMITIVE_ID] != -1)
324 anv_finishme("primitive_id needs sbe swizzling setup");
325
326 emit_3dstate_sbe(pipeline);
327
328 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS), ps) {
329 ps.KernelStartPointer0 = pipeline->ps_ksp0;
330 ps.ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_FRAGMENT];
331 ps.PerThreadScratchSpace = scratch_space(&wm_prog_data->base);
332 ps.MaximumNumberofThreads = device->info.max_wm_threads - 1;
333 ps.PushConstantEnable = wm_prog_data->base.nr_params > 0;
334 ps.AttributeEnable = wm_prog_data->num_varying_inputs > 0;
335 ps.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
336
337 ps.RenderTargetFastClearEnable = false;
338 ps.DualSourceBlendEnable = false;
339 ps.RenderTargetResolveEnable = false;
340
341 ps.PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
342 POSOFFSET_SAMPLE : POSOFFSET_NONE;
343
344 ps._32PixelDispatchEnable = false;
345 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
346 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
347
348 ps.DispatchGRFStartRegisterforConstantSetupData0 =
349 wm_prog_data->base.dispatch_grf_start_reg,
350 ps.DispatchGRFStartRegisterforConstantSetupData1 = 0,
351 ps.DispatchGRFStartRegisterforConstantSetupData2 =
352 wm_prog_data->dispatch_grf_start_reg_2,
353
354 /* Haswell requires the sample mask to be set in this packet as well as
355 * in 3DSTATE_SAMPLE_MASK; the values should match. */
356 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
357
358 ps.KernelStartPointer1 = 0;
359 ps.KernelStartPointer2 = pipeline->ps_ksp0 + wm_prog_data->prog_offset_2;
360 }
361
362 /* FIXME-GEN7: This needs a lot more work, cf gen7 upload_wm_state(). */
363 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM), wm) {
364 wm.StatisticsEnable = true;
365 wm.ThreadDispatchEnable = true;
366 wm.LineEndCapAntialiasingRegionWidth = 0; /* 0.5 pixels */
367 wm.LineAntialiasingRegionWidth = 1; /* 1.0 pixels */
368 wm.EarlyDepthStencilControl = EDSC_NORMAL;
369 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
370 wm.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
371 wm.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
372 wm.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
373 wm.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
374 wm.BarycentricInterpolationMode = wm_prog_data->barycentric_interp_modes;
375 }
376 }
377
378 *pPipeline = anv_pipeline_to_handle(pipeline);
379
380 return VK_SUCCESS;
381 }