Merge branch 'wip/i965-separate-sampler-tex' into vulkan
[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 static void
33 emit_vertex_input(struct anv_pipeline *pipeline,
34 const VkPipelineVertexInputStateCreateInfo *info)
35 {
36 const uint32_t num_dwords = 1 + info->attributeCount * 2;
37 uint32_t *p;
38
39 if (info->attributeCount > 0) {
40 p = anv_batch_emitn(&pipeline->batch, num_dwords,
41 GEN8_3DSTATE_VERTEX_ELEMENTS);
42 }
43
44 for (uint32_t i = 0; i < info->attributeCount; i++) {
45 const VkVertexInputAttributeDescription *desc =
46 &info->pVertexAttributeDescriptions[i];
47 const struct anv_format *format = anv_format_for_vk_format(desc->format);
48
49 struct GEN8_VERTEX_ELEMENT_STATE element = {
50 .VertexBufferIndex = desc->binding,
51 .Valid = true,
52 .SourceElementFormat = format->surface_format,
53 .EdgeFlagEnable = false,
54 .SourceElementOffset = desc->offsetInBytes,
55 .Component0Control = VFCOMP_STORE_SRC,
56 .Component1Control = format->num_channels >= 2 ? VFCOMP_STORE_SRC : VFCOMP_STORE_0,
57 .Component2Control = format->num_channels >= 3 ? VFCOMP_STORE_SRC : VFCOMP_STORE_0,
58 .Component3Control = format->num_channels >= 4 ? VFCOMP_STORE_SRC : VFCOMP_STORE_1_FP
59 };
60 GEN8_VERTEX_ELEMENT_STATE_pack(NULL, &p[1 + i * 2], &element);
61
62 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF_INSTANCING,
63 .InstancingEnable = pipeline->instancing_enable[desc->binding],
64 .VertexElementIndex = i,
65 /* Vulkan so far doesn't have an instance divisor, so
66 * this is always 1 (ignored if not instancing). */
67 .InstanceDataStepRate = 1);
68 }
69
70 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF_SGVS,
71 .VertexIDEnable = pipeline->vs_prog_data.uses_vertexid,
72 .VertexIDComponentNumber = 2,
73 .VertexIDElementOffset = info->bindingCount,
74 .InstanceIDEnable = pipeline->vs_prog_data.uses_instanceid,
75 .InstanceIDComponentNumber = 3,
76 .InstanceIDElementOffset = info->bindingCount);
77 }
78
79 static void
80 emit_ia_state(struct anv_pipeline *pipeline,
81 const VkPipelineInputAssemblyStateCreateInfo *info,
82 const struct anv_graphics_pipeline_create_info *extra)
83 {
84 struct GEN8_3DSTATE_VF vf = {
85 GEN8_3DSTATE_VF_header,
86 .IndexedDrawCutIndexEnable = pipeline->primitive_restart
87 };
88 GEN8_3DSTATE_VF_pack(NULL, pipeline->gen8.vf, &vf);
89
90 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF_TOPOLOGY,
91 .PrimitiveTopologyType = pipeline->topology);
92 }
93
94 static void
95 emit_rs_state(struct anv_pipeline *pipeline,
96 const VkPipelineRasterStateCreateInfo *info,
97 const struct anv_graphics_pipeline_create_info *extra)
98 {
99 static const uint32_t vk_to_gen_cullmode[] = {
100 [VK_CULL_MODE_NONE] = CULLMODE_NONE,
101 [VK_CULL_MODE_FRONT] = CULLMODE_FRONT,
102 [VK_CULL_MODE_BACK] = CULLMODE_BACK,
103 [VK_CULL_MODE_FRONT_AND_BACK] = CULLMODE_BOTH
104 };
105
106 static const uint32_t vk_to_gen_fillmode[] = {
107 [VK_FILL_MODE_POINTS] = RASTER_POINT,
108 [VK_FILL_MODE_WIREFRAME] = RASTER_WIREFRAME,
109 [VK_FILL_MODE_SOLID] = RASTER_SOLID
110 };
111
112 static const uint32_t vk_to_gen_front_face[] = {
113 [VK_FRONT_FACE_CCW] = CounterClockwise,
114 [VK_FRONT_FACE_CW] = Clockwise
115 };
116
117 struct GEN8_3DSTATE_SF sf = {
118 GEN8_3DSTATE_SF_header,
119 .ViewportTransformEnable = !(extra && extra->disable_viewport),
120 .TriangleStripListProvokingVertexSelect = 0,
121 .LineStripListProvokingVertexSelect = 0,
122 .TriangleFanProvokingVertexSelect = 0,
123 .PointWidthSource = pipeline->writes_point_size ? Vertex : State,
124 .PointWidth = 1.0,
125 };
126
127 /* FINISHME: VkBool32 rasterizerDiscardEnable; */
128
129 GEN8_3DSTATE_SF_pack(NULL, pipeline->gen8.sf, &sf);
130
131 struct GEN8_3DSTATE_RASTER raster = {
132 GEN8_3DSTATE_RASTER_header,
133 .FrontWinding = vk_to_gen_front_face[info->frontFace],
134 .CullMode = vk_to_gen_cullmode[info->cullMode],
135 .FrontFaceFillMode = vk_to_gen_fillmode[info->fillMode],
136 .BackFaceFillMode = vk_to_gen_fillmode[info->fillMode],
137 .ScissorRectangleEnable = !(extra && extra->disable_scissor),
138 .ViewportZClipTestEnable = info->depthClipEnable
139 };
140
141 GEN8_3DSTATE_RASTER_pack(NULL, pipeline->gen8.raster, &raster);
142 }
143
144 static void
145 emit_cb_state(struct anv_pipeline *pipeline,
146 const VkPipelineColorBlendStateCreateInfo *info)
147 {
148 struct anv_device *device = pipeline->device;
149
150 static const uint32_t vk_to_gen_logic_op[] = {
151 [VK_LOGIC_OP_COPY] = LOGICOP_COPY,
152 [VK_LOGIC_OP_CLEAR] = LOGICOP_CLEAR,
153 [VK_LOGIC_OP_AND] = LOGICOP_AND,
154 [VK_LOGIC_OP_AND_REVERSE] = LOGICOP_AND_REVERSE,
155 [VK_LOGIC_OP_AND_INVERTED] = LOGICOP_AND_INVERTED,
156 [VK_LOGIC_OP_NOOP] = LOGICOP_NOOP,
157 [VK_LOGIC_OP_XOR] = LOGICOP_XOR,
158 [VK_LOGIC_OP_OR] = LOGICOP_OR,
159 [VK_LOGIC_OP_NOR] = LOGICOP_NOR,
160 [VK_LOGIC_OP_EQUIV] = LOGICOP_EQUIV,
161 [VK_LOGIC_OP_INVERT] = LOGICOP_INVERT,
162 [VK_LOGIC_OP_OR_REVERSE] = LOGICOP_OR_REVERSE,
163 [VK_LOGIC_OP_COPY_INVERTED] = LOGICOP_COPY_INVERTED,
164 [VK_LOGIC_OP_OR_INVERTED] = LOGICOP_OR_INVERTED,
165 [VK_LOGIC_OP_NAND] = LOGICOP_NAND,
166 [VK_LOGIC_OP_SET] = LOGICOP_SET,
167 };
168
169 static const uint32_t vk_to_gen_blend[] = {
170 [VK_BLEND_ZERO] = BLENDFACTOR_ZERO,
171 [VK_BLEND_ONE] = BLENDFACTOR_ONE,
172 [VK_BLEND_SRC_COLOR] = BLENDFACTOR_SRC_COLOR,
173 [VK_BLEND_ONE_MINUS_SRC_COLOR] = BLENDFACTOR_INV_SRC_COLOR,
174 [VK_BLEND_DEST_COLOR] = BLENDFACTOR_DST_COLOR,
175 [VK_BLEND_ONE_MINUS_DEST_COLOR] = BLENDFACTOR_INV_DST_COLOR,
176 [VK_BLEND_SRC_ALPHA] = BLENDFACTOR_SRC_ALPHA,
177 [VK_BLEND_ONE_MINUS_SRC_ALPHA] = BLENDFACTOR_INV_SRC_ALPHA,
178 [VK_BLEND_DEST_ALPHA] = BLENDFACTOR_DST_ALPHA,
179 [VK_BLEND_ONE_MINUS_DEST_ALPHA] = BLENDFACTOR_INV_DST_ALPHA,
180 [VK_BLEND_CONSTANT_COLOR] = BLENDFACTOR_CONST_COLOR,
181 [VK_BLEND_ONE_MINUS_CONSTANT_COLOR] = BLENDFACTOR_INV_CONST_COLOR,
182 [VK_BLEND_CONSTANT_ALPHA] = BLENDFACTOR_CONST_ALPHA,
183 [VK_BLEND_ONE_MINUS_CONSTANT_ALPHA] = BLENDFACTOR_INV_CONST_ALPHA,
184 [VK_BLEND_SRC_ALPHA_SATURATE] = BLENDFACTOR_SRC_ALPHA_SATURATE,
185 [VK_BLEND_SRC1_COLOR] = BLENDFACTOR_SRC1_COLOR,
186 [VK_BLEND_ONE_MINUS_SRC1_COLOR] = BLENDFACTOR_INV_SRC1_COLOR,
187 [VK_BLEND_SRC1_ALPHA] = BLENDFACTOR_SRC1_ALPHA,
188 [VK_BLEND_ONE_MINUS_SRC1_ALPHA] = BLENDFACTOR_INV_SRC1_ALPHA,
189 };
190
191 static const uint32_t vk_to_gen_blend_op[] = {
192 [VK_BLEND_OP_ADD] = BLENDFUNCTION_ADD,
193 [VK_BLEND_OP_SUBTRACT] = BLENDFUNCTION_SUBTRACT,
194 [VK_BLEND_OP_REVERSE_SUBTRACT] = BLENDFUNCTION_REVERSE_SUBTRACT,
195 [VK_BLEND_OP_MIN] = BLENDFUNCTION_MIN,
196 [VK_BLEND_OP_MAX] = BLENDFUNCTION_MAX,
197 };
198
199 uint32_t num_dwords = GEN8_BLEND_STATE_length;
200 pipeline->blend_state =
201 anv_state_pool_alloc(&device->dynamic_state_pool, num_dwords * 4, 64);
202
203 struct GEN8_BLEND_STATE blend_state = {
204 .AlphaToCoverageEnable = info->alphaToCoverageEnable,
205 };
206
207 for (uint32_t i = 0; i < info->attachmentCount; i++) {
208 const VkPipelineColorBlendAttachmentState *a = &info->pAttachments[i];
209
210 blend_state.Entry[i] = (struct GEN8_BLEND_STATE_ENTRY) {
211 .LogicOpEnable = info->logicOpEnable,
212 .LogicOpFunction = vk_to_gen_logic_op[info->logicOp],
213 .ColorBufferBlendEnable = a->blendEnable,
214 .PreBlendSourceOnlyClampEnable = false,
215 .PreBlendColorClampEnable = false,
216 .PostBlendColorClampEnable = false,
217 .SourceBlendFactor = vk_to_gen_blend[a->srcBlendColor],
218 .DestinationBlendFactor = vk_to_gen_blend[a->destBlendColor],
219 .ColorBlendFunction = vk_to_gen_blend_op[a->blendOpColor],
220 .SourceAlphaBlendFactor = vk_to_gen_blend[a->srcBlendAlpha],
221 .DestinationAlphaBlendFactor = vk_to_gen_blend[a->destBlendAlpha],
222 .AlphaBlendFunction = vk_to_gen_blend_op[a->blendOpAlpha],
223 .WriteDisableAlpha = !(a->channelWriteMask & VK_CHANNEL_A_BIT),
224 .WriteDisableRed = !(a->channelWriteMask & VK_CHANNEL_R_BIT),
225 .WriteDisableGreen = !(a->channelWriteMask & VK_CHANNEL_G_BIT),
226 .WriteDisableBlue = !(a->channelWriteMask & VK_CHANNEL_B_BIT),
227 };
228 }
229
230 GEN8_BLEND_STATE_pack(NULL, pipeline->blend_state.map, &blend_state);
231
232 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_BLEND_STATE_POINTERS,
233 .BlendStatePointer = pipeline->blend_state.offset,
234 .BlendStatePointerValid = true);
235 }
236
237 static const uint32_t vk_to_gen_compare_op[] = {
238 [VK_COMPARE_OP_NEVER] = COMPAREFUNCTION_NEVER,
239 [VK_COMPARE_OP_LESS] = COMPAREFUNCTION_LESS,
240 [VK_COMPARE_OP_EQUAL] = COMPAREFUNCTION_EQUAL,
241 [VK_COMPARE_OP_LESS_EQUAL] = COMPAREFUNCTION_LEQUAL,
242 [VK_COMPARE_OP_GREATER] = COMPAREFUNCTION_GREATER,
243 [VK_COMPARE_OP_NOT_EQUAL] = COMPAREFUNCTION_NOTEQUAL,
244 [VK_COMPARE_OP_GREATER_EQUAL] = COMPAREFUNCTION_GEQUAL,
245 [VK_COMPARE_OP_ALWAYS] = COMPAREFUNCTION_ALWAYS,
246 };
247
248 static const uint32_t vk_to_gen_stencil_op[] = {
249 [VK_STENCIL_OP_KEEP] = STENCILOP_KEEP,
250 [VK_STENCIL_OP_ZERO] = STENCILOP_ZERO,
251 [VK_STENCIL_OP_REPLACE] = STENCILOP_REPLACE,
252 [VK_STENCIL_OP_INC_CLAMP] = STENCILOP_INCRSAT,
253 [VK_STENCIL_OP_DEC_CLAMP] = STENCILOP_DECRSAT,
254 [VK_STENCIL_OP_INVERT] = STENCILOP_INVERT,
255 [VK_STENCIL_OP_INC_WRAP] = STENCILOP_INCR,
256 [VK_STENCIL_OP_DEC_WRAP] = STENCILOP_DECR,
257 };
258
259 static void
260 emit_ds_state(struct anv_pipeline *pipeline,
261 const VkPipelineDepthStencilStateCreateInfo *info)
262 {
263 if (info == NULL) {
264 /* We're going to OR this together with the dynamic state. We need
265 * to make sure it's initialized to something useful.
266 */
267 memset(pipeline->gen8.wm_depth_stencil, 0,
268 sizeof(pipeline->gen8.wm_depth_stencil));
269 return;
270 }
271
272 /* VkBool32 depthBoundsTestEnable; // optional (depth_bounds_test) */
273
274 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil = {
275 .DepthTestEnable = info->depthTestEnable,
276 .DepthBufferWriteEnable = info->depthWriteEnable,
277 .DepthTestFunction = vk_to_gen_compare_op[info->depthCompareOp],
278 .DoubleSidedStencilEnable = true,
279
280 .StencilTestEnable = info->stencilTestEnable,
281 .StencilFailOp = vk_to_gen_stencil_op[info->front.stencilFailOp],
282 .StencilPassDepthPassOp = vk_to_gen_stencil_op[info->front.stencilPassOp],
283 .StencilPassDepthFailOp = vk_to_gen_stencil_op[info->front.stencilDepthFailOp],
284 .StencilTestFunction = vk_to_gen_compare_op[info->front.stencilCompareOp],
285 .BackfaceStencilFailOp = vk_to_gen_stencil_op[info->back.stencilFailOp],
286 .BackfaceStencilPassDepthPassOp = vk_to_gen_stencil_op[info->back.stencilPassOp],
287 .BackfaceStencilPassDepthFailOp =vk_to_gen_stencil_op[info->back.stencilDepthFailOp],
288 .BackfaceStencilTestFunction = vk_to_gen_compare_op[info->back.stencilCompareOp],
289 };
290
291 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL, pipeline->gen8.wm_depth_stencil, &wm_depth_stencil);
292 }
293
294 VkResult
295 gen8_graphics_pipeline_create(
296 VkDevice _device,
297 const VkGraphicsPipelineCreateInfo* pCreateInfo,
298 const struct anv_graphics_pipeline_create_info *extra,
299 VkPipeline* pPipeline)
300 {
301 ANV_FROM_HANDLE(anv_device, device, _device);
302 struct anv_pipeline *pipeline;
303 VkResult result;
304 uint32_t offset, length;
305
306 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
307
308 pipeline = anv_device_alloc(device, sizeof(*pipeline), 8,
309 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
310 if (pipeline == NULL)
311 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
312
313 result = anv_pipeline_init(pipeline, device, pCreateInfo, extra);
314 if (result != VK_SUCCESS)
315 return result;
316
317 /* FIXME: The compiler dead-codes FS inputs when we don't have a VS, so we
318 * hard code this to num_attributes - 2. This is because the attributes
319 * include VUE header and position, which aren't counted as varying
320 * inputs. */
321 if (pipeline->vs_simd8 == NO_KERNEL) {
322 pipeline->wm_prog_data.num_varying_inputs =
323 pCreateInfo->pVertexInputState->attributeCount - 2;
324 }
325
326 assert(pCreateInfo->pVertexInputState);
327 emit_vertex_input(pipeline, pCreateInfo->pVertexInputState);
328 assert(pCreateInfo->pInputAssemblyState);
329 emit_ia_state(pipeline, pCreateInfo->pInputAssemblyState, extra);
330 assert(pCreateInfo->pRasterState);
331 emit_rs_state(pipeline, pCreateInfo->pRasterState, extra);
332 emit_ds_state(pipeline, pCreateInfo->pDepthStencilState);
333 emit_cb_state(pipeline, pCreateInfo->pColorBlendState);
334
335 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF_STATISTICS,
336 .StatisticsEnable = true);
337 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_HS, .Enable = false);
338 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_TE, .TEEnable = false);
339 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_DS, .FunctionEnable = false);
340 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_STREAMOUT, .SOFunctionEnable = false);
341
342 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS,
343 .ConstantBufferOffset = 0,
344 .ConstantBufferSize = 4);
345 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS,
346 .ConstantBufferOffset = 4,
347 .ConstantBufferSize = 4);
348 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS,
349 .ConstantBufferOffset = 8,
350 .ConstantBufferSize = 4);
351
352 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_WM_CHROMAKEY,
353 .ChromaKeyKillEnable = false);
354 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_AA_LINE_PARAMETERS);
355
356 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_CLIP,
357 .ClipEnable = true,
358 .ViewportXYClipTestEnable = !(extra && extra->disable_viewport),
359 .MinimumPointWidth = 0.125,
360 .MaximumPointWidth = 255.875);
361
362 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_WM,
363 .StatisticsEnable = true,
364 .LineEndCapAntialiasingRegionWidth = _05pixels,
365 .LineAntialiasingRegionWidth = _10pixels,
366 .EarlyDepthStencilControl = NORMAL,
367 .ForceThreadDispatchEnable = NORMAL,
368 .PointRasterizationRule = RASTRULE_UPPER_RIGHT,
369 .BarycentricInterpolationMode =
370 pipeline->wm_prog_data.barycentric_interp_modes);
371
372 uint32_t samples = 1;
373 uint32_t log2_samples = __builtin_ffs(samples) - 1;
374 bool enable_sampling = samples > 1 ? true : false;
375
376 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_MULTISAMPLE,
377 .PixelPositionOffsetEnable = enable_sampling,
378 .PixelLocation = CENTER,
379 .NumberofMultisamples = log2_samples);
380
381 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_SAMPLE_MASK,
382 .SampleMask = 0xffff);
383
384 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_VS,
385 .VSURBStartingAddress = pipeline->urb.vs_start,
386 .VSURBEntryAllocationSize = pipeline->urb.vs_size - 1,
387 .VSNumberofURBEntries = pipeline->urb.nr_vs_entries);
388
389 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_GS,
390 .GSURBStartingAddress = pipeline->urb.gs_start,
391 .GSURBEntryAllocationSize = pipeline->urb.gs_size - 1,
392 .GSNumberofURBEntries = pipeline->urb.nr_gs_entries);
393
394 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_HS,
395 .HSURBStartingAddress = pipeline->urb.vs_start,
396 .HSURBEntryAllocationSize = 0,
397 .HSNumberofURBEntries = 0);
398
399 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_DS,
400 .DSURBStartingAddress = pipeline->urb.vs_start,
401 .DSURBEntryAllocationSize = 0,
402 .DSNumberofURBEntries = 0);
403
404 const struct brw_gs_prog_data *gs_prog_data = &pipeline->gs_prog_data;
405 offset = 1;
406 length = (gs_prog_data->base.vue_map.num_slots + 1) / 2 - offset;
407
408 if (pipeline->gs_vec4 == NO_KERNEL)
409 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_GS, .Enable = false);
410 else
411 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_GS,
412 .SingleProgramFlow = false,
413 .KernelStartPointer = pipeline->gs_vec4,
414 .VectorMaskEnable = Dmask,
415 .SamplerCount = 0,
416 .BindingTableEntryCount = 0,
417 .ExpectedVertexCount = pipeline->gs_vertex_count,
418
419 .ScratchSpaceBasePointer = pipeline->scratch_start[VK_SHADER_STAGE_GEOMETRY],
420 .PerThreadScratchSpace = ffs(gs_prog_data->base.base.total_scratch / 2048),
421
422 .OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1,
423 .OutputTopology = gs_prog_data->output_topology,
424 .VertexURBEntryReadLength = gs_prog_data->base.urb_read_length,
425 .DispatchGRFStartRegisterForURBData =
426 gs_prog_data->base.base.dispatch_grf_start_reg,
427
428 .MaximumNumberofThreads = device->info.max_gs_threads / 2 - 1,
429 .ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords,
430 .DispatchMode = gs_prog_data->base.dispatch_mode,
431 .StatisticsEnable = true,
432 .IncludePrimitiveID = gs_prog_data->include_primitive_id,
433 .ReorderMode = TRAILING,
434 .Enable = true,
435
436 .ControlDataFormat = gs_prog_data->control_data_format,
437
438 .StaticOutput = gs_prog_data->static_vertex_count >= 0,
439 .StaticOutputVertexCount =
440 gs_prog_data->static_vertex_count >= 0 ?
441 gs_prog_data->static_vertex_count : 0,
442
443 /* FIXME: mesa sets this based on ctx->Transform.ClipPlanesEnabled:
444 * UserClipDistanceClipTestEnableBitmask_3DSTATE_GS(v)
445 * UserClipDistanceCullTestEnableBitmask(v)
446 */
447
448 .VertexURBEntryOutputReadOffset = offset,
449 .VertexURBEntryOutputLength = length);
450
451 const struct brw_vue_prog_data *vue_prog_data = &pipeline->vs_prog_data.base;
452 /* Skip the VUE header and position slots */
453 offset = 1;
454 length = (vue_prog_data->vue_map.num_slots + 1) / 2 - offset;
455
456 if (pipeline->vs_simd8 == NO_KERNEL || (extra && extra->disable_vs))
457 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VS,
458 .FunctionEnable = false,
459 /* Even if VS is disabled, SBE still gets the amount of
460 * vertex data to read from this field. */
461 .VertexURBEntryOutputReadOffset = offset,
462 .VertexURBEntryOutputLength = length);
463 else
464 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VS,
465 .KernelStartPointer = pipeline->vs_simd8,
466 .SingleVertexDispatch = Multiple,
467 .VectorMaskEnable = Dmask,
468 .SamplerCount = 0,
469 .BindingTableEntryCount =
470 vue_prog_data->base.binding_table.size_bytes / 4,
471 .ThreadDispatchPriority = Normal,
472 .FloatingPointMode = IEEE754,
473 .IllegalOpcodeExceptionEnable = false,
474 .AccessesUAV = false,
475 .SoftwareExceptionEnable = false,
476
477 .ScratchSpaceBasePointer = pipeline->scratch_start[VK_SHADER_STAGE_VERTEX],
478 .PerThreadScratchSpace = ffs(vue_prog_data->base.total_scratch / 2048),
479
480 .DispatchGRFStartRegisterForURBData =
481 vue_prog_data->base.dispatch_grf_start_reg,
482 .VertexURBEntryReadLength = vue_prog_data->urb_read_length,
483 .VertexURBEntryReadOffset = 0,
484
485 .MaximumNumberofThreads = device->info.max_vs_threads - 1,
486 .StatisticsEnable = false,
487 .SIMD8DispatchEnable = true,
488 .VertexCacheDisable = false,
489 .FunctionEnable = true,
490
491 .VertexURBEntryOutputReadOffset = offset,
492 .VertexURBEntryOutputLength = length,
493 .UserClipDistanceClipTestEnableBitmask = 0,
494 .UserClipDistanceCullTestEnableBitmask = 0);
495
496 const struct brw_wm_prog_data *wm_prog_data = &pipeline->wm_prog_data;
497
498 /* TODO: We should clean this up. Among other things, this is mostly
499 * shared with other gens.
500 */
501 const struct brw_vue_map *fs_input_map;
502 if (pipeline->gs_vec4 == NO_KERNEL)
503 fs_input_map = &vue_prog_data->vue_map;
504 else
505 fs_input_map = &gs_prog_data->base.vue_map;
506
507 struct GEN8_3DSTATE_SBE_SWIZ swiz = {
508 GEN8_3DSTATE_SBE_SWIZ_header,
509 };
510
511 int max_source_attr = 0;
512 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
513 int input_index = wm_prog_data->urb_setup[attr];
514
515 if (input_index < 0)
516 continue;
517
518 /* We have to subtract two slots to accout for the URB entry output
519 * read offset in the VS and GS stages.
520 */
521 int source_attr = fs_input_map->varying_to_slot[attr] - 2;
522 max_source_attr = MAX2(max_source_attr, source_attr);
523
524 if (input_index >= 16)
525 continue;
526
527 swiz.Attribute[input_index].SourceAttribute = source_attr;
528 }
529
530 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_SBE,
531 .AttributeSwizzleEnable = true,
532 .ForceVertexURBEntryReadLength = false,
533 .ForceVertexURBEntryReadOffset = false,
534 .VertexURBEntryReadLength = DIV_ROUND_UP(max_source_attr + 1, 2),
535 .PointSpriteTextureCoordinateOrigin = UPPERLEFT,
536 .NumberofSFOutputAttributes =
537 wm_prog_data->num_varying_inputs);
538
539 uint32_t *dw = anv_batch_emit_dwords(&pipeline->batch,
540 GEN8_3DSTATE_SBE_SWIZ_length);
541 GEN8_3DSTATE_SBE_SWIZ_pack(&pipeline->batch, dw, &swiz);
542
543 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_PS,
544 .KernelStartPointer0 = pipeline->ps_ksp0,
545
546 .SingleProgramFlow = false,
547 .VectorMaskEnable = true,
548 .SamplerCount = 1,
549
550 .ScratchSpaceBasePointer = pipeline->scratch_start[VK_SHADER_STAGE_FRAGMENT],
551 .PerThreadScratchSpace = ffs(wm_prog_data->base.total_scratch / 2048),
552
553 .MaximumNumberofThreadsPerPSD = 64 - 2,
554 .PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
555 POSOFFSET_SAMPLE: POSOFFSET_NONE,
556 .PushConstantEnable = wm_prog_data->base.nr_params > 0,
557 ._8PixelDispatchEnable = pipeline->ps_simd8 != NO_KERNEL,
558 ._16PixelDispatchEnable = pipeline->ps_simd16 != NO_KERNEL,
559 ._32PixelDispatchEnable = false,
560
561 .DispatchGRFStartRegisterForConstantSetupData0 = pipeline->ps_grf_start0,
562 .DispatchGRFStartRegisterForConstantSetupData1 = 0,
563 .DispatchGRFStartRegisterForConstantSetupData2 = pipeline->ps_grf_start2,
564
565 .KernelStartPointer1 = 0,
566 .KernelStartPointer2 = pipeline->ps_ksp2);
567
568 bool per_sample_ps = false;
569 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_PS_EXTRA,
570 .PixelShaderValid = true,
571 .PixelShaderKillsPixel = wm_prog_data->uses_kill,
572 .PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode,
573 .AttributeEnable = wm_prog_data->num_varying_inputs > 0,
574 .oMaskPresenttoRenderTarget = wm_prog_data->uses_omask,
575 .PixelShaderIsPerSample = per_sample_ps);
576
577 *pPipeline = anv_pipeline_to_handle(pipeline);
578
579 return VK_SUCCESS;
580 }
581
582 VkResult gen8_compute_pipeline_create(
583 VkDevice _device,
584 const VkComputePipelineCreateInfo* pCreateInfo,
585 VkPipeline* pPipeline)
586 {
587 ANV_FROM_HANDLE(anv_device, device, _device);
588 struct anv_pipeline *pipeline;
589 VkResult result;
590
591 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO);
592
593 pipeline = anv_device_alloc(device, sizeof(*pipeline), 8,
594 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
595 if (pipeline == NULL)
596 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
597
598 pipeline->device = device;
599 pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
600
601 pipeline->blend_state.map = NULL;
602
603 result = anv_reloc_list_init(&pipeline->batch_relocs, device);
604 if (result != VK_SUCCESS) {
605 anv_device_free(device, pipeline);
606 return result;
607 }
608 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
609 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
610 pipeline->batch.relocs = &pipeline->batch_relocs;
611
612 anv_state_stream_init(&pipeline->program_stream,
613 &device->instruction_block_pool);
614
615 /* When we free the pipeline, we detect stages based on the NULL status
616 * of various prog_data pointers. Make them NULL by default.
617 */
618 memset(pipeline->prog_data, 0, sizeof(pipeline->prog_data));
619 memset(pipeline->scratch_start, 0, sizeof(pipeline->scratch_start));
620
621 pipeline->vs_simd8 = NO_KERNEL;
622 pipeline->vs_vec4 = NO_KERNEL;
623 pipeline->gs_vec4 = NO_KERNEL;
624
625 pipeline->active_stages = 0;
626 pipeline->total_scratch = 0;
627
628 assert(pCreateInfo->stage.stage == VK_SHADER_STAGE_COMPUTE);
629 ANV_FROM_HANDLE(anv_shader, shader, pCreateInfo->stage.shader);
630 anv_pipeline_compile_cs(pipeline, pCreateInfo, shader);
631
632 pipeline->use_repclear = false;
633
634 const struct brw_cs_prog_data *cs_prog_data = &pipeline->cs_prog_data;
635
636 anv_batch_emit(&pipeline->batch, GEN8_MEDIA_VFE_STATE,
637 .ScratchSpaceBasePointer = pipeline->scratch_start[VK_SHADER_STAGE_COMPUTE],
638 .PerThreadScratchSpace = ffs(cs_prog_data->base.total_scratch / 2048),
639 .ScratchSpaceBasePointerHigh = 0,
640 .StackSize = 0,
641
642 .MaximumNumberofThreads = device->info.max_cs_threads - 1,
643 .NumberofURBEntries = 2,
644 .ResetGatewayTimer = true,
645 .BypassGatewayControl = true,
646 .URBEntryAllocationSize = 2,
647 .CURBEAllocationSize = 0);
648
649 struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
650 uint32_t group_size = prog_data->local_size[0] *
651 prog_data->local_size[1] * prog_data->local_size[2];
652 pipeline->cs_thread_width_max = DIV_ROUND_UP(group_size, prog_data->simd_size);
653 uint32_t remainder = group_size & (prog_data->simd_size - 1);
654
655 if (remainder > 0)
656 pipeline->cs_right_mask = ~0u >> (32 - remainder);
657 else
658 pipeline->cs_right_mask = ~0u >> (32 - prog_data->simd_size);
659
660
661 *pPipeline = anv_pipeline_to_handle(pipeline);
662
663 return VK_SUCCESS;
664 }