vk: Quiet a few warnings
[mesa.git] / src / vulkan / 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 "private.h"
31
32 // Shader functions
33
34 VkResult anv_CreateShader(
35 VkDevice _device,
36 const VkShaderCreateInfo* pCreateInfo,
37 VkShader* pShader)
38 {
39 struct anv_device *device = (struct anv_device *) _device;
40 struct anv_shader *shader;
41
42 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_CREATE_INFO);
43
44 shader = anv_device_alloc(device, sizeof(*shader) + pCreateInfo->codeSize, 8,
45 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
46 if (shader == NULL)
47 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
48
49 shader->size = pCreateInfo->codeSize;
50 memcpy(shader->data, pCreateInfo->pCode, shader->size);
51
52 *pShader = (VkShader) shader;
53
54 return VK_SUCCESS;
55 }
56
57 // Pipeline functions
58
59 static void
60 emit_vertex_input(struct anv_pipeline *pipeline, VkPipelineVertexInputCreateInfo *info)
61 {
62 const uint32_t num_dwords = 1 + info->attributeCount * 2;
63 uint32_t *p;
64 bool instancing_enable[32];
65
66 pipeline->vb_used = 0;
67 for (uint32_t i = 0; i < info->bindingCount; i++) {
68 const VkVertexInputBindingDescription *desc =
69 &info->pVertexBindingDescriptions[i];
70
71 pipeline->vb_used |= 1 << desc->binding;
72 pipeline->binding_stride[desc->binding] = desc->strideInBytes;
73
74 /* Step rate is programmed per vertex element (attribute), not
75 * binding. Set up a map of which bindings step per instance, for
76 * reference by vertex element setup. */
77 switch (desc->stepRate) {
78 default:
79 case VK_VERTEX_INPUT_STEP_RATE_VERTEX:
80 instancing_enable[desc->binding] = false;
81 break;
82 case VK_VERTEX_INPUT_STEP_RATE_INSTANCE:
83 instancing_enable[desc->binding] = true;
84 break;
85 }
86 }
87
88 p = anv_batch_emitn(&pipeline->batch, num_dwords,
89 GEN8_3DSTATE_VERTEX_ELEMENTS);
90
91 for (uint32_t i = 0; i < info->attributeCount; i++) {
92 const VkVertexInputAttributeDescription *desc =
93 &info->pVertexAttributeDescriptions[i];
94 const struct anv_format *format = anv_format_for_vk_format(desc->format);
95
96 struct GEN8_VERTEX_ELEMENT_STATE element = {
97 .VertexBufferIndex = desc->binding,
98 .Valid = true,
99 .SourceElementFormat = format->format,
100 .EdgeFlagEnable = false,
101 .SourceElementOffset = desc->offsetInBytes,
102 .Component0Control = VFCOMP_STORE_SRC,
103 .Component1Control = format->channels >= 2 ? VFCOMP_STORE_SRC : VFCOMP_STORE_0,
104 .Component2Control = format->channels >= 3 ? VFCOMP_STORE_SRC : VFCOMP_STORE_0,
105 .Component3Control = format->channels >= 4 ? VFCOMP_STORE_SRC : VFCOMP_STORE_1_FP
106 };
107 GEN8_VERTEX_ELEMENT_STATE_pack(NULL, &p[1 + i * 2], &element);
108
109 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF_INSTANCING,
110 .InstancingEnable = instancing_enable[desc->binding],
111 .VertexElementIndex = i,
112 /* Vulkan so far doesn't have an instance divisor, so
113 * this is always 1 (ignored if not instancing). */
114 .InstanceDataStepRate = 1);
115 }
116
117 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF_SGVS,
118 .VertexIDEnable = pipeline->vs_prog_data.uses_vertexid,
119 .VertexIDComponentNumber = 2,
120 .VertexIDElementOffset = info->bindingCount,
121 .InstanceIDEnable = pipeline->vs_prog_data.uses_instanceid,
122 .InstanceIDComponentNumber = 3,
123 .InstanceIDElementOffset = info->bindingCount);
124 }
125
126 static void
127 emit_ia_state(struct anv_pipeline *pipeline,
128 VkPipelineIaStateCreateInfo *info,
129 const struct anv_pipeline_create_info *extra)
130 {
131 static const uint32_t vk_to_gen_primitive_type[] = {
132 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = _3DPRIM_POINTLIST,
133 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = _3DPRIM_LINELIST,
134 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = _3DPRIM_LINESTRIP,
135 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = _3DPRIM_TRILIST,
136 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
137 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
138 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_ADJ] = _3DPRIM_LINELIST_ADJ,
139 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_ADJ] = _3DPRIM_LISTSTRIP_ADJ,
140 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_ADJ] = _3DPRIM_TRILIST_ADJ,
141 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_ADJ] = _3DPRIM_TRISTRIP_ADJ,
142 [VK_PRIMITIVE_TOPOLOGY_PATCH] = _3DPRIM_PATCHLIST_1
143 };
144 uint32_t topology = vk_to_gen_primitive_type[info->topology];
145
146 if (extra && extra->use_rectlist)
147 topology = _3DPRIM_RECTLIST;
148
149 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF,
150 .IndexedDrawCutIndexEnable = info->primitiveRestartEnable,
151 .CutIndex = info->primitiveRestartIndex);
152 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VF_TOPOLOGY,
153 .PrimitiveTopologyType = topology);
154 }
155
156 static void
157 emit_rs_state(struct anv_pipeline *pipeline, VkPipelineRsStateCreateInfo *info,
158 const struct anv_pipeline_create_info *extra)
159 {
160 static const uint32_t vk_to_gen_cullmode[] = {
161 [VK_CULL_MODE_NONE] = CULLMODE_NONE,
162 [VK_CULL_MODE_FRONT] = CULLMODE_FRONT,
163 [VK_CULL_MODE_BACK] = CULLMODE_BACK,
164 [VK_CULL_MODE_FRONT_AND_BACK] = CULLMODE_BOTH
165 };
166
167 static const uint32_t vk_to_gen_fillmode[] = {
168 [VK_FILL_MODE_POINTS] = RASTER_POINT,
169 [VK_FILL_MODE_WIREFRAME] = RASTER_WIREFRAME,
170 [VK_FILL_MODE_SOLID] = RASTER_SOLID
171 };
172
173 static const uint32_t vk_to_gen_front_face[] = {
174 [VK_FRONT_FACE_CCW] = CounterClockwise,
175 [VK_FRONT_FACE_CW] = Clockwise
176 };
177
178 static const uint32_t vk_to_gen_coordinate_origin[] = {
179 [VK_COORDINATE_ORIGIN_UPPER_LEFT] = UPPERLEFT,
180 [VK_COORDINATE_ORIGIN_LOWER_LEFT] = LOWERLEFT
181 };
182
183 struct GEN8_3DSTATE_SF sf = {
184 GEN8_3DSTATE_SF_header,
185 .ViewportTransformEnable = !(extra && extra->disable_viewport),
186 .TriangleStripListProvokingVertexSelect =
187 info->provokingVertex == VK_PROVOKING_VERTEX_FIRST ? 0 : 2,
188 .LineStripListProvokingVertexSelect =
189 info->provokingVertex == VK_PROVOKING_VERTEX_FIRST ? 0 : 1,
190 .TriangleFanProvokingVertexSelect =
191 info->provokingVertex == VK_PROVOKING_VERTEX_FIRST ? 0 : 2,
192 .PointWidthSource = info->programPointSize ? Vertex : State,
193 };
194
195 /* FINISHME: bool32_t rasterizerDiscardEnable; */
196
197 GEN8_3DSTATE_SF_pack(NULL, pipeline->state_sf, &sf);
198
199 struct GEN8_3DSTATE_RASTER raster = {
200 GEN8_3DSTATE_RASTER_header,
201 .FrontWinding = vk_to_gen_front_face[info->frontFace],
202 .CullMode = vk_to_gen_cullmode[info->cullMode],
203 .FrontFaceFillMode = vk_to_gen_fillmode[info->fillMode],
204 .BackFaceFillMode = vk_to_gen_fillmode[info->fillMode],
205 .ScissorRectangleEnable = !(extra && extra->disable_scissor),
206 .ViewportZClipTestEnable = info->depthClipEnable
207 };
208
209 GEN8_3DSTATE_RASTER_pack(NULL, pipeline->state_raster, &raster);
210
211 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_SBE,
212 .ForceVertexURBEntryReadLength = false,
213 .ForceVertexURBEntryReadOffset = false,
214 .PointSpriteTextureCoordinateOrigin =
215 vk_to_gen_coordinate_origin[info->pointOrigin],
216 .NumberofSFOutputAttributes =
217 pipeline->wm_prog_data.num_varying_inputs);
218
219 }
220
221 static void
222 emit_cb_state(struct anv_pipeline *pipeline, VkPipelineCbStateCreateInfo *info)
223 {
224 struct anv_device *device = pipeline->device;
225
226 static const uint32_t vk_to_gen_logic_op[] = {
227 [VK_LOGIC_OP_COPY] = LOGICOP_COPY,
228 [VK_LOGIC_OP_CLEAR] = LOGICOP_CLEAR,
229 [VK_LOGIC_OP_AND] = LOGICOP_AND,
230 [VK_LOGIC_OP_AND_REVERSE] = LOGICOP_AND_REVERSE,
231 [VK_LOGIC_OP_AND_INVERTED] = LOGICOP_AND_INVERTED,
232 [VK_LOGIC_OP_NOOP] = LOGICOP_NOOP,
233 [VK_LOGIC_OP_XOR] = LOGICOP_XOR,
234 [VK_LOGIC_OP_OR] = LOGICOP_OR,
235 [VK_LOGIC_OP_NOR] = LOGICOP_NOR,
236 [VK_LOGIC_OP_EQUIV] = LOGICOP_EQUIV,
237 [VK_LOGIC_OP_INVERT] = LOGICOP_INVERT,
238 [VK_LOGIC_OP_OR_REVERSE] = LOGICOP_OR_REVERSE,
239 [VK_LOGIC_OP_COPY_INVERTED] = LOGICOP_COPY_INVERTED,
240 [VK_LOGIC_OP_OR_INVERTED] = LOGICOP_OR_INVERTED,
241 [VK_LOGIC_OP_NAND] = LOGICOP_NAND,
242 [VK_LOGIC_OP_SET] = LOGICOP_SET,
243 };
244
245 static const uint32_t vk_to_gen_blend[] = {
246 [VK_BLEND_ZERO] = BLENDFACTOR_ZERO,
247 [VK_BLEND_ONE] = BLENDFACTOR_ONE,
248 [VK_BLEND_SRC_COLOR] = BLENDFACTOR_SRC_COLOR,
249 [VK_BLEND_ONE_MINUS_SRC_COLOR] = BLENDFACTOR_INV_SRC_COLOR,
250 [VK_BLEND_DEST_COLOR] = BLENDFACTOR_DST_COLOR,
251 [VK_BLEND_ONE_MINUS_DEST_COLOR] = BLENDFACTOR_INV_DST_COLOR,
252 [VK_BLEND_SRC_ALPHA] = BLENDFACTOR_SRC_ALPHA,
253 [VK_BLEND_ONE_MINUS_SRC_ALPHA] = BLENDFACTOR_INV_SRC_ALPHA,
254 [VK_BLEND_DEST_ALPHA] = BLENDFACTOR_DST_ALPHA,
255 [VK_BLEND_ONE_MINUS_DEST_ALPHA] = BLENDFACTOR_INV_DST_ALPHA,
256 [VK_BLEND_CONSTANT_COLOR] = BLENDFACTOR_CONST_COLOR,
257 [VK_BLEND_ONE_MINUS_CONSTANT_COLOR] = BLENDFACTOR_INV_CONST_COLOR,
258 [VK_BLEND_CONSTANT_ALPHA] = BLENDFACTOR_CONST_ALPHA,
259 [VK_BLEND_ONE_MINUS_CONSTANT_ALPHA] = BLENDFACTOR_INV_CONST_ALPHA,
260 [VK_BLEND_SRC_ALPHA_SATURATE] = BLENDFACTOR_SRC_ALPHA_SATURATE,
261 [VK_BLEND_SRC1_COLOR] = BLENDFACTOR_SRC1_COLOR,
262 [VK_BLEND_ONE_MINUS_SRC1_COLOR] = BLENDFACTOR_INV_SRC1_COLOR,
263 [VK_BLEND_SRC1_ALPHA] = BLENDFACTOR_SRC1_ALPHA,
264 [VK_BLEND_ONE_MINUS_SRC1_ALPHA] = BLENDFACTOR_INV_SRC1_ALPHA,
265 };
266
267 static const uint32_t vk_to_gen_blend_op[] = {
268 [VK_BLEND_OP_ADD] = BLENDFUNCTION_ADD,
269 [VK_BLEND_OP_SUBTRACT] = BLENDFUNCTION_SUBTRACT,
270 [VK_BLEND_OP_REVERSE_SUBTRACT] = BLENDFUNCTION_REVERSE_SUBTRACT,
271 [VK_BLEND_OP_MIN] = BLENDFUNCTION_MIN,
272 [VK_BLEND_OP_MAX] = BLENDFUNCTION_MAX,
273 };
274
275 uint32_t num_dwords = 1 + info->attachmentCount * 2;
276 pipeline->blend_state =
277 anv_state_pool_alloc(&device->dynamic_state_pool, num_dwords * 4, 64);
278
279 struct GEN8_BLEND_STATE blend_state = {
280 .AlphaToCoverageEnable = info->alphaToCoverageEnable,
281 };
282
283 uint32_t *state = pipeline->blend_state.map;
284 GEN8_BLEND_STATE_pack(NULL, state, &blend_state);
285
286 for (uint32_t i = 0; i < info->attachmentCount; i++) {
287 const VkPipelineCbAttachmentState *a = &info->pAttachments[i];
288
289 struct GEN8_BLEND_STATE_ENTRY entry = {
290 .LogicOpEnable = info->logicOpEnable,
291 .LogicOpFunction = vk_to_gen_logic_op[info->logicOp],
292 .ColorBufferBlendEnable = a->blendEnable,
293 .PreBlendSourceOnlyClampEnable = false,
294 .PreBlendColorClampEnable = false,
295 .PostBlendColorClampEnable = false,
296 .SourceBlendFactor = vk_to_gen_blend[a->srcBlendColor],
297 .DestinationBlendFactor = vk_to_gen_blend[a->destBlendColor],
298 .ColorBlendFunction = vk_to_gen_blend_op[a->blendOpColor],
299 .SourceAlphaBlendFactor = vk_to_gen_blend[a->srcBlendAlpha],
300 .DestinationAlphaBlendFactor = vk_to_gen_blend[a->destBlendAlpha],
301 .AlphaBlendFunction = vk_to_gen_blend_op[a->blendOpAlpha],
302 .WriteDisableAlpha = !(a->channelWriteMask & VK_CHANNEL_A_BIT),
303 .WriteDisableRed = !(a->channelWriteMask & VK_CHANNEL_R_BIT),
304 .WriteDisableGreen = !(a->channelWriteMask & VK_CHANNEL_G_BIT),
305 .WriteDisableBlue = !(a->channelWriteMask & VK_CHANNEL_B_BIT),
306 };
307
308 GEN8_BLEND_STATE_ENTRY_pack(NULL, state + i * 2 + 1, &entry);
309 }
310
311 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_BLEND_STATE_POINTERS,
312 .BlendStatePointer = pipeline->blend_state.offset,
313 .BlendStatePointerValid = true);
314 }
315
316 static const uint32_t vk_to_gen_compare_op[] = {
317 [VK_COMPARE_OP_NEVER] = COMPAREFUNCTION_NEVER,
318 [VK_COMPARE_OP_LESS] = COMPAREFUNCTION_LESS,
319 [VK_COMPARE_OP_EQUAL] = COMPAREFUNCTION_EQUAL,
320 [VK_COMPARE_OP_LESS_EQUAL] = COMPAREFUNCTION_LEQUAL,
321 [VK_COMPARE_OP_GREATER] = COMPAREFUNCTION_GREATER,
322 [VK_COMPARE_OP_NOT_EQUAL] = COMPAREFUNCTION_NOTEQUAL,
323 [VK_COMPARE_OP_GREATER_EQUAL] = COMPAREFUNCTION_GEQUAL,
324 [VK_COMPARE_OP_ALWAYS] = COMPAREFUNCTION_ALWAYS,
325 };
326
327 static const uint32_t vk_to_gen_stencil_op[] = {
328 [VK_STENCIL_OP_KEEP] = 0,
329 [VK_STENCIL_OP_ZERO] = 0,
330 [VK_STENCIL_OP_REPLACE] = 0,
331 [VK_STENCIL_OP_INC_CLAMP] = 0,
332 [VK_STENCIL_OP_DEC_CLAMP] = 0,
333 [VK_STENCIL_OP_INVERT] = 0,
334 [VK_STENCIL_OP_INC_WRAP] = 0,
335 [VK_STENCIL_OP_DEC_WRAP] = 0
336 };
337
338 static void
339 emit_ds_state(struct anv_pipeline *pipeline, VkPipelineDsStateCreateInfo *info)
340 {
341 /* bool32_t depthBoundsEnable; // optional (depth_bounds_test) */
342
343 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil = {
344 .DepthTestEnable = info->depthTestEnable,
345 .DepthBufferWriteEnable = info->depthWriteEnable,
346 .DepthTestFunction = vk_to_gen_compare_op[info->depthCompareOp],
347 .DoubleSidedStencilEnable = true,
348
349 .StencilTestEnable = info->stencilTestEnable,
350 .StencilFailOp = vk_to_gen_stencil_op[info->front.stencilFailOp],
351 .StencilPassDepthPassOp = vk_to_gen_stencil_op[info->front.stencilPassOp],
352 .StencilPassDepthFailOp = vk_to_gen_stencil_op[info->front.stencilDepthFailOp],
353 .StencilTestFunction = vk_to_gen_compare_op[info->front.stencilCompareOp],
354 .BackfaceStencilFailOp = vk_to_gen_stencil_op[info->back.stencilFailOp],
355 .BackfaceStencilPassDepthPassOp = vk_to_gen_stencil_op[info->back.stencilPassOp],
356 .BackfaceStencilPassDepthFailOp =vk_to_gen_stencil_op[info->back.stencilDepthFailOp],
357 .BackfaceStencilTestFunction = vk_to_gen_compare_op[info->back.stencilCompareOp],
358 };
359
360 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL, pipeline->state_wm_depth_stencil, &wm_depth_stencil);
361 }
362
363 VkResult anv_CreateGraphicsPipeline(
364 VkDevice device,
365 const VkGraphicsPipelineCreateInfo* pCreateInfo,
366 VkPipeline* pPipeline)
367 {
368 return anv_pipeline_create(device, pCreateInfo, NULL, pPipeline);
369 }
370
371 static void
372 anv_pipeline_destroy(struct anv_device *device,
373 struct anv_object *object,
374 VkObjectType obj_type)
375 {
376 struct anv_pipeline *pipeline = (struct anv_pipeline*) object;
377
378 assert(obj_type == VK_OBJECT_TYPE_PIPELINE);
379
380 anv_compiler_free(pipeline);
381 anv_reloc_list_finish(&pipeline->batch.relocs, pipeline->device);
382 anv_state_stream_finish(&pipeline->program_stream);
383 anv_state_pool_free(&device->dynamic_state_pool, pipeline->blend_state);
384 anv_device_free(pipeline->device, pipeline);
385 }
386
387 VkResult
388 anv_pipeline_create(
389 VkDevice _device,
390 const VkGraphicsPipelineCreateInfo* pCreateInfo,
391 const struct anv_pipeline_create_info * extra,
392 VkPipeline* pPipeline)
393 {
394 struct anv_device *device = (struct anv_device *) _device;
395 struct anv_pipeline *pipeline;
396 const struct anv_common *common;
397 VkPipelineShaderStageCreateInfo *shader_create_info;
398 VkPipelineIaStateCreateInfo *ia_info = NULL;
399 VkPipelineRsStateCreateInfo *rs_info = NULL;
400 VkPipelineDsStateCreateInfo *ds_info = NULL;
401 VkPipelineCbStateCreateInfo *cb_info = NULL;
402 VkPipelineVertexInputCreateInfo *vi_info = NULL;
403 VkResult result;
404 uint32_t offset, length;
405
406 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
407
408 pipeline = anv_device_alloc(device, sizeof(*pipeline), 8,
409 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
410 if (pipeline == NULL)
411 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
412
413 pipeline->base.destructor = anv_pipeline_destroy;
414 pipeline->device = device;
415 pipeline->layout = (struct anv_pipeline_layout *) pCreateInfo->layout;
416 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
417
418 result = anv_reloc_list_init(&pipeline->batch.relocs, device);
419 if (result != VK_SUCCESS) {
420 anv_device_free(device, pipeline);
421 return result;
422 }
423 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
424 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
425
426 anv_state_stream_init(&pipeline->program_stream,
427 &device->instruction_block_pool);
428
429 for (common = pCreateInfo->pNext; common; common = common->pNext) {
430 switch (common->sType) {
431 case VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO:
432 vi_info = (VkPipelineVertexInputCreateInfo *) common;
433 break;
434 case VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO:
435 ia_info = (VkPipelineIaStateCreateInfo *) common;
436 break;
437 case VK_STRUCTURE_TYPE_PIPELINE_TESS_STATE_CREATE_INFO:
438 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESS_STATE_CREATE_INFO");
439 break;
440 case VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO:
441 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO");
442 break;
443 case VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO:
444 rs_info = (VkPipelineRsStateCreateInfo *) common;
445 break;
446 case VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO:
447 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO");
448 break;
449 case VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO:
450 cb_info = (VkPipelineCbStateCreateInfo *) common;
451 break;
452 case VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO:
453 ds_info = (VkPipelineDsStateCreateInfo *) common;
454 break;
455 case VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO:
456 shader_create_info = (VkPipelineShaderStageCreateInfo *) common;
457 pipeline->shaders[shader_create_info->shader.stage] =
458 (struct anv_shader *) shader_create_info->shader.shader;
459 break;
460 default:
461 break;
462 }
463 }
464
465 pipeline->use_repclear = extra && extra->use_repclear;
466
467 anv_compiler_run(device->compiler, pipeline);
468
469 /* FIXME: The compiler dead-codes FS inputs when we don't have a VS, so we
470 * hard code this to num_attributes - 2. This is because the attributes
471 * include VUE header and position, which aren't counted as varying
472 * inputs. */
473 if (pipeline->vs_simd8 == NO_KERNEL)
474 pipeline->wm_prog_data.num_varying_inputs = vi_info->attributeCount - 2;
475
476 assert(vi_info);
477 emit_vertex_input(pipeline, vi_info);
478 assert(ia_info);
479 emit_ia_state(pipeline, ia_info, extra);
480 assert(rs_info);
481 emit_rs_state(pipeline, rs_info, extra);
482 /* ds_info is optional if we're not using depth or stencil buffers, ps is
483 * optional for depth-only rendering. */
484 if (ds_info)
485 emit_ds_state(pipeline, ds_info);
486
487 emit_cb_state(pipeline, cb_info);
488
489 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_CLIP,
490 .ClipEnable = true,
491 .ViewportXYClipTestEnable = !(extra && extra->disable_viewport));
492
493 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_WM,
494 .StatisticsEnable = true,
495 .LineEndCapAntialiasingRegionWidth = _05pixels,
496 .LineAntialiasingRegionWidth = _10pixels,
497 .EarlyDepthStencilControl = NORMAL,
498 .ForceThreadDispatchEnable = NORMAL,
499 .PointRasterizationRule = RASTRULE_UPPER_RIGHT,
500 .BarycentricInterpolationMode =
501 pipeline->wm_prog_data.barycentric_interp_modes);
502
503 uint32_t samples = 1;
504 uint32_t log2_samples = __builtin_ffs(samples) - 1;
505 bool enable_sampling = samples > 1 ? true : false;
506
507 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_MULTISAMPLE,
508 .PixelPositionOffsetEnable = enable_sampling,
509 .PixelLocation = CENTER,
510 .NumberofMultisamples = log2_samples);
511
512 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_VS,
513 .VSURBStartingAddress = pipeline->urb.vs_start,
514 .VSURBEntryAllocationSize = pipeline->urb.vs_size - 1,
515 .VSNumberofURBEntries = pipeline->urb.nr_vs_entries);
516
517 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_GS,
518 .GSURBStartingAddress = pipeline->urb.gs_start,
519 .GSURBEntryAllocationSize = pipeline->urb.gs_size - 1,
520 .GSNumberofURBEntries = pipeline->urb.nr_gs_entries);
521
522 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_HS,
523 .HSURBStartingAddress = pipeline->urb.vs_start,
524 .HSURBEntryAllocationSize = 0,
525 .HSNumberofURBEntries = 0);
526
527 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_URB_DS,
528 .DSURBStartingAddress = pipeline->urb.vs_start,
529 .DSURBEntryAllocationSize = 0,
530 .DSNumberofURBEntries = 0);
531
532 const struct brw_gs_prog_data *gs_prog_data = &pipeline->gs_prog_data;
533 offset = 1;
534 length = (gs_prog_data->base.vue_map.num_slots + 1) / 2 - offset;
535
536 if (pipeline->gs_vec4 == NO_KERNEL)
537 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_GS, .Enable = false);
538 else
539 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_GS,
540 .SingleProgramFlow = false,
541 .KernelStartPointer = pipeline->gs_vec4,
542 .VectorMaskEnable = Vmask,
543 .SamplerCount = 0,
544 .BindingTableEntryCount = 0,
545 .ExpectedVertexCount = pipeline->gs_vertex_count,
546
547 .PerThreadScratchSpace = 0,
548 .ScratchSpaceBasePointer = 0,
549
550 .OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1,
551 .OutputTopology = gs_prog_data->output_topology,
552 .VertexURBEntryReadLength = gs_prog_data->base.urb_read_length,
553 .DispatchGRFStartRegisterForURBData =
554 gs_prog_data->base.base.dispatch_grf_start_reg,
555
556 .MaximumNumberofThreads = device->info.max_gs_threads,
557 .ControlDataHeaderSize = gs_prog_data->control_data_header_size_hwords,
558 //pipeline->gs_prog_data.dispatch_mode |
559 .StatisticsEnable = true,
560 .IncludePrimitiveID = gs_prog_data->include_primitive_id,
561 .ReorderMode = TRAILING,
562 .Enable = true,
563
564 .ControlDataFormat = gs_prog_data->control_data_format,
565
566 /* FIXME: mesa sets this based on ctx->Transform.ClipPlanesEnabled:
567 * UserClipDistanceClipTestEnableBitmask_3DSTATE_GS(v)
568 * UserClipDistanceCullTestEnableBitmask(v)
569 */
570
571 .VertexURBEntryOutputReadOffset = offset,
572 .VertexURBEntryOutputLength = length);
573
574 //trp_generate_blend_hw_cmds(batch, pipeline);
575
576 const struct brw_vue_prog_data *vue_prog_data = &pipeline->vs_prog_data.base;
577 /* Skip the VUE header and position slots */
578 offset = 1;
579 length = (vue_prog_data->vue_map.num_slots + 1) / 2 - offset;
580
581 if (pipeline->vs_simd8 == NO_KERNEL || (extra && extra->disable_vs))
582 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VS,
583 .FunctionEnable = false,
584 .VertexURBEntryOutputReadOffset = 1,
585 /* Even if VS is disabled, SBE still gets the amount of
586 * vertex data to read from this field. We use attribute
587 * count - 1, as we don't count the VUE header here. */
588 .VertexURBEntryOutputLength =
589 DIV_ROUND_UP(vi_info->attributeCount - 1, 2));
590 else
591 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_VS,
592 .KernelStartPointer = pipeline->vs_simd8,
593 .SingleVertexDispatch = Multiple,
594 .VectorMaskEnable = Dmask,
595 .SamplerCount = 0,
596 .BindingTableEntryCount =
597 vue_prog_data->base.binding_table.size_bytes / 4,
598 .ThreadDispatchPriority = Normal,
599 .FloatingPointMode = IEEE754,
600 .IllegalOpcodeExceptionEnable = false,
601 .AccessesUAV = false,
602 .SoftwareExceptionEnable = false,
603
604 /* FIXME: pointer needs to be assigned outside as it aliases
605 * PerThreadScratchSpace.
606 */
607 .ScratchSpaceBasePointer = 0,
608 .PerThreadScratchSpace = 0,
609
610 .DispatchGRFStartRegisterForURBData =
611 vue_prog_data->base.dispatch_grf_start_reg,
612 .VertexURBEntryReadLength = vue_prog_data->urb_read_length,
613 .VertexURBEntryReadOffset = 0,
614
615 .MaximumNumberofThreads = device->info.max_vs_threads - 1,
616 .StatisticsEnable = false,
617 .SIMD8DispatchEnable = true,
618 .VertexCacheDisable = ia_info->disableVertexReuse,
619 .FunctionEnable = true,
620
621 .VertexURBEntryOutputReadOffset = offset,
622 .VertexURBEntryOutputLength = length,
623 .UserClipDistanceClipTestEnableBitmask = 0,
624 .UserClipDistanceCullTestEnableBitmask = 0);
625
626 const struct brw_wm_prog_data *wm_prog_data = &pipeline->wm_prog_data;
627 uint32_t ksp0, ksp2, grf_start0, grf_start2;
628
629 ksp2 = 0;
630 grf_start2 = 0;
631 if (pipeline->ps_simd8 != NO_KERNEL) {
632 ksp0 = pipeline->ps_simd8;
633 grf_start0 = wm_prog_data->base.dispatch_grf_start_reg;
634 if (pipeline->ps_simd16 != NO_KERNEL) {
635 ksp2 = pipeline->ps_simd16;
636 grf_start2 = wm_prog_data->dispatch_grf_start_reg_16;
637 }
638 } else if (pipeline->ps_simd16 != NO_KERNEL) {
639 ksp0 = pipeline->ps_simd16;
640 grf_start0 = wm_prog_data->dispatch_grf_start_reg_16;
641 } else {
642 unreachable("no ps shader");
643 }
644
645 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_PS,
646 .KernelStartPointer0 = ksp0,
647
648 .SingleProgramFlow = false,
649 .VectorMaskEnable = true,
650 .SamplerCount = 1,
651
652 .ScratchSpaceBasePointer = 0,
653 .PerThreadScratchSpace = 0,
654
655 .MaximumNumberofThreadsPerPSD = 64 - 2,
656 .PositionXYOffsetSelect = wm_prog_data->uses_pos_offset ?
657 POSOFFSET_SAMPLE: POSOFFSET_NONE,
658 .PushConstantEnable = wm_prog_data->base.nr_params > 0,
659 ._8PixelDispatchEnable = pipeline->ps_simd8 != NO_KERNEL,
660 ._16PixelDispatchEnable = pipeline->ps_simd16 != NO_KERNEL,
661 ._32PixelDispatchEnable = false,
662
663 .DispatchGRFStartRegisterForConstantSetupData0 = grf_start0,
664 .DispatchGRFStartRegisterForConstantSetupData1 = 0,
665 .DispatchGRFStartRegisterForConstantSetupData2 = grf_start2,
666
667 .KernelStartPointer1 = 0,
668 .KernelStartPointer2 = ksp2);
669
670 bool per_sample_ps = false;
671 anv_batch_emit(&pipeline->batch, GEN8_3DSTATE_PS_EXTRA,
672 .PixelShaderValid = true,
673 .PixelShaderKillsPixel = wm_prog_data->uses_kill,
674 .PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode,
675 .AttributeEnable = wm_prog_data->num_varying_inputs > 0,
676 .oMaskPresenttoRenderTarget = wm_prog_data->uses_omask,
677 .PixelShaderIsPerSample = per_sample_ps);
678
679 *pPipeline = (VkPipeline) pipeline;
680
681 return VK_SUCCESS;
682 }
683
684 VkResult anv_CreateGraphicsPipelineDerivative(
685 VkDevice device,
686 const VkGraphicsPipelineCreateInfo* pCreateInfo,
687 VkPipeline basePipeline,
688 VkPipeline* pPipeline)
689 {
690 stub_return(VK_UNSUPPORTED);
691 }
692
693 VkResult anv_CreateComputePipeline(
694 VkDevice device,
695 const VkComputePipelineCreateInfo* pCreateInfo,
696 VkPipeline* pPipeline)
697 {
698 stub_return(VK_UNSUPPORTED);
699 }
700
701 VkResult anv_StorePipeline(
702 VkDevice device,
703 VkPipeline pipeline,
704 size_t* pDataSize,
705 void* pData)
706 {
707 stub_return(VK_UNSUPPORTED);
708 }
709
710 VkResult anv_LoadPipeline(
711 VkDevice device,
712 size_t dataSize,
713 const void* pData,
714 VkPipeline* pPipeline)
715 {
716 stub_return(VK_UNSUPPORTED);
717 }
718
719 VkResult anv_LoadPipelineDerivative(
720 VkDevice device,
721 size_t dataSize,
722 const void* pData,
723 VkPipeline basePipeline,
724 VkPipeline* pPipeline)
725 {
726 stub_return(VK_UNSUPPORTED);
727 }
728
729 // Pipeline layout functions
730
731 VkResult anv_CreatePipelineLayout(
732 VkDevice _device,
733 const VkPipelineLayoutCreateInfo* pCreateInfo,
734 VkPipelineLayout* pPipelineLayout)
735 {
736 struct anv_device *device = (struct anv_device *) _device;
737 struct anv_pipeline_layout *layout;
738
739 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
740
741 layout = anv_device_alloc(device, sizeof(*layout), 8,
742 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
743 if (layout == NULL)
744 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
745
746 layout->num_sets = pCreateInfo->descriptorSetCount;
747
748 uint32_t surface_start[VK_NUM_SHADER_STAGE] = { 0, };
749 uint32_t sampler_start[VK_NUM_SHADER_STAGE] = { 0, };
750
751 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
752 layout->stage[s].surface_count = 0;
753 layout->stage[s].sampler_count = 0;
754 }
755
756 for (uint32_t i = 0; i < pCreateInfo->descriptorSetCount; i++) {
757 struct anv_descriptor_set_layout *set_layout =
758 (struct anv_descriptor_set_layout *) pCreateInfo->pSetLayouts[i];
759
760 layout->set[i].layout = set_layout;
761 for (uint32_t s = 0; s < VK_NUM_SHADER_STAGE; s++) {
762 layout->set[i].surface_start[s] = surface_start[s];
763 surface_start[s] += set_layout->stage[s].surface_count;
764 layout->set[i].sampler_start[s] = sampler_start[s];
765 sampler_start[s] += set_layout->stage[s].sampler_count;
766
767 layout->stage[s].surface_count += set_layout->stage[s].surface_count;
768 layout->stage[s].sampler_count += set_layout->stage[s].sampler_count;
769 }
770 }
771
772 *pPipelineLayout = (VkPipelineLayout) layout;
773
774 return VK_SUCCESS;
775 }