anv/formats: Add a tiling parameter to get_isl_format
[mesa.git] / src / vulkan / gen8_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 "gen8_pack.h"
33 #include "gen9_pack.h"
34
35 #include "genX_pipeline_util.h"
36
37 static void
38 emit_vertex_input(struct anv_pipeline *pipeline,
39 const VkPipelineVertexInputStateCreateInfo *info,
40 const struct anv_graphics_pipeline_create_info *extra)
41 {
42 static_assert(ANV_GEN >= 8, "should be compiling this for gen < 8");
43
44 uint32_t elements;
45 if (extra && extra->disable_vs) {
46 /* If the VS is disabled, just assume the user knows what they're
47 * doing and apply the layout blindly. This can only come from
48 * meta, so this *should* be safe.
49 */
50 elements = 0;
51 for (uint32_t i = 0; i < info->vertexAttributeDescriptionCount; i++)
52 elements |= (1 << info->pVertexAttributeDescriptions[i].location);
53 } else {
54 /* Pull inputs_read out of the VS prog data */
55 uint64_t inputs_read = pipeline->vs_prog_data.inputs_read;
56 assert((inputs_read & ((1 << VERT_ATTRIB_GENERIC0) - 1)) == 0);
57 elements = inputs_read >> VERT_ATTRIB_GENERIC0;
58 }
59
60 const uint32_t num_dwords = 1 + __builtin_popcount(elements) * 2;
61
62 uint32_t *p;
63 if (elements != 0) {
64 p = anv_batch_emitn(&pipeline->batch, num_dwords,
65 GENX(3DSTATE_VERTEX_ELEMENTS));
66 memset(p + 1, 0, (num_dwords - 1) * 4);
67 }
68
69 for (uint32_t i = 0; i < info->vertexAttributeDescriptionCount; i++) {
70 const VkVertexInputAttributeDescription *desc =
71 &info->pVertexAttributeDescriptions[i];
72 enum isl_format format = anv_get_isl_format(desc->format,
73 VK_IMAGE_ASPECT_COLOR_BIT,
74 VK_IMAGE_TILING_LINEAR);
75
76 assert(desc->binding < 32);
77
78 if ((elements & (1 << desc->location)) == 0)
79 continue; /* Binding unused */
80
81 uint32_t slot = __builtin_popcount(elements & ((1 << desc->location) - 1));
82
83 struct GENX(VERTEX_ELEMENT_STATE) element = {
84 .VertexBufferIndex = desc->binding,
85 .Valid = true,
86 .SourceElementFormat = format,
87 .EdgeFlagEnable = false,
88 .SourceElementOffset = desc->offset,
89 .Component0Control = vertex_element_comp_control(format, 0),
90 .Component1Control = vertex_element_comp_control(format, 1),
91 .Component2Control = vertex_element_comp_control(format, 2),
92 .Component3Control = vertex_element_comp_control(format, 3),
93 };
94 GENX(VERTEX_ELEMENT_STATE_pack)(NULL, &p[1 + slot * 2], &element);
95
96 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_INSTANCING),
97 .InstancingEnable = pipeline->instancing_enable[desc->binding],
98 .VertexElementIndex = slot,
99 /* Vulkan so far doesn't have an instance divisor, so
100 * this is always 1 (ignored if not instancing). */
101 .InstanceDataStepRate = 1);
102 }
103
104 const uint32_t id_slot = __builtin_popcount(elements);
105 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_SGVS),
106 .VertexIDEnable = pipeline->vs_prog_data.uses_vertexid,
107 .VertexIDComponentNumber = 2,
108 .VertexIDElementOffset = id_slot,
109 .InstanceIDEnable = pipeline->vs_prog_data.uses_instanceid,
110 .InstanceIDComponentNumber = 3,
111 .InstanceIDElementOffset = id_slot);
112 }
113
114 static void
115 emit_ia_state(struct anv_pipeline *pipeline,
116 const VkPipelineInputAssemblyStateCreateInfo *info,
117 const struct anv_graphics_pipeline_create_info *extra)
118 {
119 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_TOPOLOGY),
120 .PrimitiveTopologyType = pipeline->topology);
121 }
122
123 static void
124 emit_rs_state(struct anv_pipeline *pipeline,
125 const VkPipelineRasterizationStateCreateInfo *info,
126 const struct anv_graphics_pipeline_create_info *extra)
127 {
128 struct GENX(3DSTATE_SF) sf = {
129 GENX(3DSTATE_SF_header),
130 .ViewportTransformEnable = !(extra && extra->disable_viewport),
131 .TriangleStripListProvokingVertexSelect = 0,
132 .LineStripListProvokingVertexSelect = 0,
133 .TriangleFanProvokingVertexSelect = 0,
134 .PointWidthSource = pipeline->writes_point_size ? Vertex : State,
135 .PointWidth = 1.0,
136 };
137
138 /* FINISHME: VkBool32 rasterizerDiscardEnable; */
139
140 GENX(3DSTATE_SF_pack)(NULL, pipeline->gen8.sf, &sf);
141
142 struct GENX(3DSTATE_RASTER) raster = {
143 GENX(3DSTATE_RASTER_header),
144 .FrontWinding = vk_to_gen_front_face[info->frontFace],
145 .CullMode = vk_to_gen_cullmode[info->cullMode],
146 .FrontFaceFillMode = vk_to_gen_fillmode[info->polygonMode],
147 .BackFaceFillMode = vk_to_gen_fillmode[info->polygonMode],
148 .ScissorRectangleEnable = !(extra && extra->disable_scissor),
149 #if ANV_GEN == 8
150 .ViewportZClipTestEnable = true,
151 #else
152 /* GEN9+ splits ViewportZClipTestEnable into near and far enable bits */
153 .ViewportZFarClipTestEnable = true,
154 .ViewportZNearClipTestEnable = true,
155 #endif
156 };
157
158 GENX(3DSTATE_RASTER_pack)(NULL, pipeline->gen8.raster, &raster);
159 }
160
161 static void
162 emit_cb_state(struct anv_pipeline *pipeline,
163 const VkPipelineColorBlendStateCreateInfo *info,
164 const VkPipelineMultisampleStateCreateInfo *ms_info)
165 {
166 struct anv_device *device = pipeline->device;
167
168 uint32_t num_dwords = GENX(BLEND_STATE_length);
169 pipeline->blend_state =
170 anv_state_pool_alloc(&device->dynamic_state_pool, num_dwords * 4, 64);
171
172 struct GENX(BLEND_STATE) blend_state = {
173 .AlphaToCoverageEnable = ms_info && ms_info->alphaToCoverageEnable,
174 .AlphaToOneEnable = ms_info && ms_info->alphaToOneEnable,
175 };
176
177 for (uint32_t i = 0; i < info->attachmentCount; i++) {
178 const VkPipelineColorBlendAttachmentState *a = &info->pAttachments[i];
179
180 if (a->srcColorBlendFactor != a->srcAlphaBlendFactor ||
181 a->dstColorBlendFactor != a->dstAlphaBlendFactor ||
182 a->colorBlendOp != a->alphaBlendOp) {
183 blend_state.IndependentAlphaBlendEnable = true;
184 }
185
186 blend_state.Entry[i] = (struct GENX(BLEND_STATE_ENTRY)) {
187 .LogicOpEnable = info->logicOpEnable,
188 .LogicOpFunction = vk_to_gen_logic_op[info->logicOp],
189 .ColorBufferBlendEnable = a->blendEnable,
190 .PreBlendSourceOnlyClampEnable = false,
191 .ColorClampRange = COLORCLAMP_RTFORMAT,
192 .PreBlendColorClampEnable = true,
193 .PostBlendColorClampEnable = true,
194 .SourceBlendFactor = vk_to_gen_blend[a->srcColorBlendFactor],
195 .DestinationBlendFactor = vk_to_gen_blend[a->dstColorBlendFactor],
196 .ColorBlendFunction = vk_to_gen_blend_op[a->colorBlendOp],
197 .SourceAlphaBlendFactor = vk_to_gen_blend[a->srcAlphaBlendFactor],
198 .DestinationAlphaBlendFactor = vk_to_gen_blend[a->dstAlphaBlendFactor],
199 .AlphaBlendFunction = vk_to_gen_blend_op[a->alphaBlendOp],
200 .WriteDisableAlpha = !(a->colorWriteMask & VK_COLOR_COMPONENT_A_BIT),
201 .WriteDisableRed = !(a->colorWriteMask & VK_COLOR_COMPONENT_R_BIT),
202 .WriteDisableGreen = !(a->colorWriteMask & VK_COLOR_COMPONENT_G_BIT),
203 .WriteDisableBlue = !(a->colorWriteMask & VK_COLOR_COMPONENT_B_BIT),
204 };
205
206 /* Our hardware applies the blend factor prior to the blend function
207 * regardless of what function is used. Technically, this means the
208 * hardware can do MORE than GL or Vulkan specify. However, it also
209 * means that, for MIN and MAX, we have to stomp the blend factor to
210 * ONE to make it a no-op.
211 */
212 if (a->colorBlendOp == VK_BLEND_OP_MIN ||
213 a->colorBlendOp == VK_BLEND_OP_MAX) {
214 blend_state.Entry[i].SourceBlendFactor = BLENDFACTOR_ONE;
215 blend_state.Entry[i].DestinationBlendFactor = BLENDFACTOR_ONE;
216 }
217 if (a->alphaBlendOp == VK_BLEND_OP_MIN ||
218 a->alphaBlendOp == VK_BLEND_OP_MAX) {
219 blend_state.Entry[i].SourceAlphaBlendFactor = BLENDFACTOR_ONE;
220 blend_state.Entry[i].DestinationAlphaBlendFactor = BLENDFACTOR_ONE;
221 }
222 }
223
224 GENX(BLEND_STATE_pack)(NULL, pipeline->blend_state.map, &blend_state);
225 if (!device->info.has_llc)
226 anv_state_clflush(pipeline->blend_state);
227
228 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_BLEND_STATE_POINTERS),
229 .BlendStatePointer = pipeline->blend_state.offset,
230 .BlendStatePointerValid = true);
231 }
232
233 static void
234 emit_ds_state(struct anv_pipeline *pipeline,
235 const VkPipelineDepthStencilStateCreateInfo *info)
236 {
237 uint32_t *dw = ANV_GEN == 8 ?
238 pipeline->gen8.wm_depth_stencil : pipeline->gen9.wm_depth_stencil;
239
240 if (info == NULL) {
241 /* We're going to OR this together with the dynamic state. We need
242 * to make sure it's initialized to something useful.
243 */
244 memset(pipeline->gen8.wm_depth_stencil, 0,
245 sizeof(pipeline->gen8.wm_depth_stencil));
246 memset(pipeline->gen9.wm_depth_stencil, 0,
247 sizeof(pipeline->gen9.wm_depth_stencil));
248 return;
249 }
250
251 /* VkBool32 depthBoundsTestEnable; // optional (depth_bounds_test) */
252
253 struct GENX(3DSTATE_WM_DEPTH_STENCIL) wm_depth_stencil = {
254 .DepthTestEnable = info->depthTestEnable,
255 .DepthBufferWriteEnable = info->depthWriteEnable,
256 .DepthTestFunction = vk_to_gen_compare_op[info->depthCompareOp],
257 .DoubleSidedStencilEnable = true,
258
259 .StencilTestEnable = info->stencilTestEnable,
260 .StencilFailOp = vk_to_gen_stencil_op[info->front.failOp],
261 .StencilPassDepthPassOp = vk_to_gen_stencil_op[info->front.passOp],
262 .StencilPassDepthFailOp = vk_to_gen_stencil_op[info->front.depthFailOp],
263 .StencilTestFunction = vk_to_gen_compare_op[info->front.compareOp],
264 .BackfaceStencilFailOp = vk_to_gen_stencil_op[info->back.failOp],
265 .BackfaceStencilPassDepthPassOp = vk_to_gen_stencil_op[info->back.passOp],
266 .BackfaceStencilPassDepthFailOp =vk_to_gen_stencil_op[info->back.depthFailOp],
267 .BackfaceStencilTestFunction = vk_to_gen_compare_op[info->back.compareOp],
268 };
269
270 GENX(3DSTATE_WM_DEPTH_STENCIL_pack)(NULL, dw, &wm_depth_stencil);
271 }
272
273 VkResult
274 genX(graphics_pipeline_create)(
275 VkDevice _device,
276 const VkGraphicsPipelineCreateInfo* pCreateInfo,
277 const struct anv_graphics_pipeline_create_info *extra,
278 const VkAllocationCallbacks* pAllocator,
279 VkPipeline* pPipeline)
280 {
281 ANV_FROM_HANDLE(anv_device, device, _device);
282 struct anv_pipeline *pipeline;
283 VkResult result;
284 uint32_t offset, length;
285
286 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
287
288 pipeline = anv_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
289 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
290 if (pipeline == NULL)
291 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
292
293 result = anv_pipeline_init(pipeline, device, pCreateInfo, extra, pAllocator);
294 if (result != VK_SUCCESS) {
295 anv_free2(&device->alloc, pAllocator, pipeline);
296 return result;
297 }
298
299 assert(pCreateInfo->pVertexInputState);
300 emit_vertex_input(pipeline, pCreateInfo->pVertexInputState, extra);
301 assert(pCreateInfo->pInputAssemblyState);
302 emit_ia_state(pipeline, pCreateInfo->pInputAssemblyState, extra);
303 assert(pCreateInfo->pRasterizationState);
304 emit_rs_state(pipeline, pCreateInfo->pRasterizationState, extra);
305 emit_ds_state(pipeline, pCreateInfo->pDepthStencilState);
306 emit_cb_state(pipeline, pCreateInfo->pColorBlendState,
307 pCreateInfo->pMultisampleState);
308
309 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VF_STATISTICS),
310 .StatisticsEnable = true);
311 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_HS), .Enable = false);
312 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_TE), .TEEnable = false);
313 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_DS), .FunctionEnable = false);
314 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_STREAMOUT), .SOFunctionEnable = false);
315
316 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS),
317 .ConstantBufferOffset = 0,
318 .ConstantBufferSize = 4);
319 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_GS),
320 .ConstantBufferOffset = 4,
321 .ConstantBufferSize = 4);
322 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_PS),
323 .ConstantBufferOffset = 8,
324 .ConstantBufferSize = 4);
325
326 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM_CHROMAKEY),
327 .ChromaKeyKillEnable = false);
328 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_AA_LINE_PARAMETERS));
329
330 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_CLIP),
331 .ClipEnable = true,
332 .ViewportXYClipTestEnable = !(extra && extra->disable_viewport),
333 .MinimumPointWidth = 0.125,
334 .MaximumPointWidth = 255.875);
335
336 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_WM),
337 .StatisticsEnable = true,
338 .LineEndCapAntialiasingRegionWidth = _05pixels,
339 .LineAntialiasingRegionWidth = _10pixels,
340 .EarlyDepthStencilControl = NORMAL,
341 .ForceThreadDispatchEnable = NORMAL,
342 .PointRasterizationRule = RASTRULE_UPPER_RIGHT,
343 .BarycentricInterpolationMode =
344 pipeline->wm_prog_data.barycentric_interp_modes);
345
346 uint32_t samples = 1;
347 uint32_t log2_samples = __builtin_ffs(samples) - 1;
348 bool enable_sampling = samples > 1 ? true : false;
349
350 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_MULTISAMPLE),
351 .PixelPositionOffsetEnable = enable_sampling,
352 .PixelLocation = CENTER,
353 .NumberofMultisamples = log2_samples);
354
355 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SAMPLE_MASK),
356 .SampleMask = 0xffff);
357
358 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_URB_VS),
359 .VSURBStartingAddress = pipeline->urb.vs_start,
360 .VSURBEntryAllocationSize = pipeline->urb.vs_size - 1,
361 .VSNumberofURBEntries = pipeline->urb.nr_vs_entries);
362
363 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_URB_GS),
364 .GSURBStartingAddress = pipeline->urb.gs_start,
365 .GSURBEntryAllocationSize = pipeline->urb.gs_size - 1,
366 .GSNumberofURBEntries = pipeline->urb.nr_gs_entries);
367
368 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_URB_HS),
369 .HSURBStartingAddress = pipeline->urb.vs_start,
370 .HSURBEntryAllocationSize = 0,
371 .HSNumberofURBEntries = 0);
372
373 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_URB_DS),
374 .DSURBStartingAddress = pipeline->urb.vs_start,
375 .DSURBEntryAllocationSize = 0,
376 .DSNumberofURBEntries = 0);
377
378 const struct brw_gs_prog_data *gs_prog_data = &pipeline->gs_prog_data;
379 offset = 1;
380 length = (gs_prog_data->base.vue_map.num_slots + 1) / 2 - offset;
381
382 if (pipeline->gs_kernel == NO_KERNEL)
383 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS), .Enable = false);
384 else
385 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_GS),
386 .SingleProgramFlow = false,
387 .KernelStartPointer = pipeline->gs_kernel,
388 .VectorMaskEnable = Dmask,
389 .SamplerCount = 0,
390 .BindingTableEntryCount = 0,
391 .ExpectedVertexCount = pipeline->gs_vertex_count,
392
393 .ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_GEOMETRY],
394 .PerThreadScratchSpace = ffs(gs_prog_data->base.base.total_scratch / 2048),
395
396 .OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1,
397 .OutputTopology = gs_prog_data->output_topology,
398 .VertexURBEntryReadLength = gs_prog_data->base.urb_read_length,
399 .IncludeVertexHandles = gs_prog_data->base.include_vue_handles,
400 .DispatchGRFStartRegisterForURBData =
401 gs_prog_data->base.base.dispatch_grf_start_reg,
402
403 .MaximumNumberofThreads = device->info.max_gs_threads / 2 - 1,
404 .ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords,
405 .DispatchMode = gs_prog_data->base.dispatch_mode,
406 .StatisticsEnable = true,
407 .IncludePrimitiveID = gs_prog_data->include_primitive_id,
408 .ReorderMode = TRAILING,
409 .Enable = true,
410
411 .ControlDataFormat = gs_prog_data->control_data_format,
412
413 .StaticOutput = gs_prog_data->static_vertex_count >= 0,
414 .StaticOutputVertexCount =
415 gs_prog_data->static_vertex_count >= 0 ?
416 gs_prog_data->static_vertex_count : 0,
417
418 /* FIXME: mesa sets this based on ctx->Transform.ClipPlanesEnabled:
419 * UserClipDistanceClipTestEnableBitmask_3DSTATE_GS(v)
420 * UserClipDistanceCullTestEnableBitmask(v)
421 */
422
423 .VertexURBEntryOutputReadOffset = offset,
424 .VertexURBEntryOutputLength = length);
425
426 const struct brw_vue_prog_data *vue_prog_data = &pipeline->vs_prog_data.base;
427 /* Skip the VUE header and position slots */
428 offset = 1;
429 length = (vue_prog_data->vue_map.num_slots + 1) / 2 - offset;
430
431 uint32_t vs_start = pipeline->vs_simd8 != NO_KERNEL ? pipeline->vs_simd8 :
432 pipeline->vs_vec4;
433
434 if (vs_start == NO_KERNEL || (extra && extra->disable_vs))
435 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS),
436 .FunctionEnable = false,
437 /* Even if VS is disabled, SBE still gets the amount of
438 * vertex data to read from this field. */
439 .VertexURBEntryOutputReadOffset = offset,
440 .VertexURBEntryOutputLength = length);
441 else
442 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_VS),
443 .KernelStartPointer = vs_start,
444 .SingleVertexDispatch = Multiple,
445 .VectorMaskEnable = Dmask,
446 .SamplerCount = 0,
447 .BindingTableEntryCount =
448 vue_prog_data->base.binding_table.size_bytes / 4,
449 .ThreadDispatchPriority = Normal,
450 .FloatingPointMode = IEEE754,
451 .IllegalOpcodeExceptionEnable = false,
452 .AccessesUAV = false,
453 .SoftwareExceptionEnable = false,
454
455 .ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_VERTEX],
456 .PerThreadScratchSpace = ffs(vue_prog_data->base.total_scratch / 2048),
457
458 .DispatchGRFStartRegisterForURBData =
459 vue_prog_data->base.dispatch_grf_start_reg,
460 .VertexURBEntryReadLength = vue_prog_data->urb_read_length,
461 .VertexURBEntryReadOffset = 0,
462
463 .MaximumNumberofThreads = device->info.max_vs_threads - 1,
464 .StatisticsEnable = false,
465 .SIMD8DispatchEnable = pipeline->vs_simd8 != NO_KERNEL,
466 .VertexCacheDisable = false,
467 .FunctionEnable = true,
468
469 .VertexURBEntryOutputReadOffset = offset,
470 .VertexURBEntryOutputLength = length,
471 .UserClipDistanceClipTestEnableBitmask = 0,
472 .UserClipDistanceCullTestEnableBitmask = 0);
473
474 const struct brw_wm_prog_data *wm_prog_data = &pipeline->wm_prog_data;
475
476 /* TODO: We should clean this up. Among other things, this is mostly
477 * shared with other gens.
478 */
479 const struct brw_vue_map *fs_input_map;
480 if (pipeline->gs_kernel == NO_KERNEL)
481 fs_input_map = &vue_prog_data->vue_map;
482 else
483 fs_input_map = &gs_prog_data->base.vue_map;
484
485 struct GENX(3DSTATE_SBE_SWIZ) swiz = {
486 GENX(3DSTATE_SBE_SWIZ_header),
487 };
488
489 int max_source_attr = 0;
490 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
491 int input_index = wm_prog_data->urb_setup[attr];
492
493 if (input_index < 0)
494 continue;
495
496 /* We have to subtract two slots to accout for the URB entry output
497 * read offset in the VS and GS stages.
498 */
499 int source_attr = fs_input_map->varying_to_slot[attr] - 2;
500 max_source_attr = MAX2(max_source_attr, source_attr);
501
502 if (input_index >= 16)
503 continue;
504
505 swiz.Attribute[input_index].SourceAttribute = source_attr;
506 }
507
508 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_SBE),
509 .AttributeSwizzleEnable = true,
510 .ForceVertexURBEntryReadLength = false,
511 .ForceVertexURBEntryReadOffset = false,
512 .VertexURBEntryReadLength = DIV_ROUND_UP(max_source_attr + 1, 2),
513 .PointSpriteTextureCoordinateOrigin = UPPERLEFT,
514 .NumberofSFOutputAttributes =
515 wm_prog_data->num_varying_inputs,
516
517 #if ANV_GEN >= 9
518 .Attribute0ActiveComponentFormat = ACF_XYZW,
519 .Attribute1ActiveComponentFormat = ACF_XYZW,
520 .Attribute2ActiveComponentFormat = ACF_XYZW,
521 .Attribute3ActiveComponentFormat = ACF_XYZW,
522 .Attribute4ActiveComponentFormat = ACF_XYZW,
523 .Attribute5ActiveComponentFormat = ACF_XYZW,
524 .Attribute6ActiveComponentFormat = ACF_XYZW,
525 .Attribute7ActiveComponentFormat = ACF_XYZW,
526 .Attribute8ActiveComponentFormat = ACF_XYZW,
527 .Attribute9ActiveComponentFormat = ACF_XYZW,
528 .Attribute10ActiveComponentFormat = ACF_XYZW,
529 .Attribute11ActiveComponentFormat = ACF_XYZW,
530 .Attribute12ActiveComponentFormat = ACF_XYZW,
531 .Attribute13ActiveComponentFormat = ACF_XYZW,
532 .Attribute14ActiveComponentFormat = ACF_XYZW,
533 .Attribute15ActiveComponentFormat = ACF_XYZW,
534 /* wow, much field, very attribute */
535 .Attribute16ActiveComponentFormat = ACF_XYZW,
536 .Attribute17ActiveComponentFormat = ACF_XYZW,
537 .Attribute18ActiveComponentFormat = ACF_XYZW,
538 .Attribute19ActiveComponentFormat = ACF_XYZW,
539 .Attribute20ActiveComponentFormat = ACF_XYZW,
540 .Attribute21ActiveComponentFormat = ACF_XYZW,
541 .Attribute22ActiveComponentFormat = ACF_XYZW,
542 .Attribute23ActiveComponentFormat = ACF_XYZW,
543 .Attribute24ActiveComponentFormat = ACF_XYZW,
544 .Attribute25ActiveComponentFormat = ACF_XYZW,
545 .Attribute26ActiveComponentFormat = ACF_XYZW,
546 .Attribute27ActiveComponentFormat = ACF_XYZW,
547 .Attribute28ActiveComponentFormat = ACF_XYZW,
548 .Attribute29ActiveComponentFormat = ACF_XYZW,
549 .Attribute28ActiveComponentFormat = ACF_XYZW,
550 .Attribute29ActiveComponentFormat = ACF_XYZW,
551 .Attribute30ActiveComponentFormat = ACF_XYZW,
552 #endif
553 );
554
555 uint32_t *dw = anv_batch_emit_dwords(&pipeline->batch,
556 GENX(3DSTATE_SBE_SWIZ_length));
557 GENX(3DSTATE_SBE_SWIZ_pack)(&pipeline->batch, dw, &swiz);
558
559 const int num_thread_bias = ANV_GEN == 8 ? 2 : 1;
560 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS),
561 .KernelStartPointer0 = pipeline->ps_ksp0,
562
563 .SingleProgramFlow = false,
564 .VectorMaskEnable = true,
565 .SamplerCount = 1,
566
567 .ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_FRAGMENT],
568 .PerThreadScratchSpace = ffs(wm_prog_data->base.total_scratch / 2048),
569
570 .MaximumNumberofThreadsPerPSD = 64 - num_thread_bias,
571 .PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
572 POSOFFSET_SAMPLE: POSOFFSET_NONE,
573 .PushConstantEnable = wm_prog_data->base.nr_params > 0,
574 ._8PixelDispatchEnable = pipeline->ps_simd8 != NO_KERNEL,
575 ._16PixelDispatchEnable = pipeline->ps_simd16 != NO_KERNEL,
576 ._32PixelDispatchEnable = false,
577
578 .DispatchGRFStartRegisterForConstantSetupData0 = pipeline->ps_grf_start0,
579 .DispatchGRFStartRegisterForConstantSetupData1 = 0,
580 .DispatchGRFStartRegisterForConstantSetupData2 = pipeline->ps_grf_start2,
581
582 .KernelStartPointer1 = 0,
583 .KernelStartPointer2 = pipeline->ps_ksp2);
584
585 bool per_sample_ps = false;
586 anv_batch_emit(&pipeline->batch, GENX(3DSTATE_PS_EXTRA),
587 .PixelShaderValid = true,
588 .PixelShaderKillsPixel = wm_prog_data->uses_kill,
589 .PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode,
590 .AttributeEnable = wm_prog_data->num_varying_inputs > 0,
591 .oMaskPresenttoRenderTarget = wm_prog_data->uses_omask,
592 .PixelShaderIsPerSample = per_sample_ps,
593 #if ANV_GEN >= 9
594 .PixelShaderPullsBary = wm_prog_data->pulls_bary,
595 .InputCoverageMaskState = ICMS_NONE
596 #endif
597 );
598
599 *pPipeline = anv_pipeline_to_handle(pipeline);
600
601 return VK_SUCCESS;
602 }
603
604 VkResult genX(compute_pipeline_create)(
605 VkDevice _device,
606 const VkComputePipelineCreateInfo* pCreateInfo,
607 const VkAllocationCallbacks* pAllocator,
608 VkPipeline* pPipeline)
609 {
610 ANV_FROM_HANDLE(anv_device, device, _device);
611 struct anv_pipeline *pipeline;
612 VkResult result;
613
614 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO);
615
616 pipeline = anv_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
617 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
618 if (pipeline == NULL)
619 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
620
621 pipeline->device = device;
622 pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
623
624 pipeline->blend_state.map = NULL;
625
626 result = anv_reloc_list_init(&pipeline->batch_relocs,
627 pAllocator ? pAllocator : &device->alloc);
628 if (result != VK_SUCCESS) {
629 anv_free2(&device->alloc, pAllocator, pipeline);
630 return result;
631 }
632 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
633 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
634 pipeline->batch.relocs = &pipeline->batch_relocs;
635
636 anv_state_stream_init(&pipeline->program_stream,
637 &device->instruction_block_pool);
638
639 /* When we free the pipeline, we detect stages based on the NULL status
640 * of various prog_data pointers. Make them NULL by default.
641 */
642 memset(pipeline->prog_data, 0, sizeof(pipeline->prog_data));
643 memset(pipeline->scratch_start, 0, sizeof(pipeline->scratch_start));
644
645 pipeline->vs_simd8 = NO_KERNEL;
646 pipeline->vs_vec4 = NO_KERNEL;
647 pipeline->gs_kernel = NO_KERNEL;
648
649 pipeline->active_stages = 0;
650 pipeline->total_scratch = 0;
651
652 assert(pCreateInfo->stage.stage == VK_SHADER_STAGE_COMPUTE_BIT);
653 ANV_FROM_HANDLE(anv_shader_module, module, pCreateInfo->stage.module);
654 anv_pipeline_compile_cs(pipeline, pCreateInfo, module,
655 pCreateInfo->stage.pName);
656
657 pipeline->use_repclear = false;
658
659 const struct brw_cs_prog_data *cs_prog_data = &pipeline->cs_prog_data;
660
661 anv_batch_emit(&pipeline->batch, GENX(MEDIA_VFE_STATE),
662 .ScratchSpaceBasePointer = pipeline->scratch_start[MESA_SHADER_COMPUTE],
663 .PerThreadScratchSpace = ffs(cs_prog_data->base.total_scratch / 2048),
664 .ScratchSpaceBasePointerHigh = 0,
665 .StackSize = 0,
666
667 .MaximumNumberofThreads = device->info.max_cs_threads - 1,
668 .NumberofURBEntries = 2,
669 .ResetGatewayTimer = true,
670 #if ANV_GEN == 8
671 .BypassGatewayControl = true,
672 #endif
673 .URBEntryAllocationSize = 2,
674 .CURBEAllocationSize = 0);
675
676 struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
677 uint32_t group_size = prog_data->local_size[0] *
678 prog_data->local_size[1] * prog_data->local_size[2];
679 pipeline->cs_thread_width_max = DIV_ROUND_UP(group_size, prog_data->simd_size);
680 uint32_t remainder = group_size & (prog_data->simd_size - 1);
681
682 if (remainder > 0)
683 pipeline->cs_right_mask = ~0u >> (32 - remainder);
684 else
685 pipeline->cs_right_mask = ~0u >> (32 - prog_data->simd_size);
686
687
688 *pPipeline = anv_pipeline_to_handle(pipeline);
689
690 return VK_SUCCESS;
691 }