2 * Copyright © 2015 Intel Corporation
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:
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
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
34 VkResult
anv_CreateShaderModule(
36 const VkShaderModuleCreateInfo
* pCreateInfo
,
37 VkShader
* pShaderModule
)
39 ANV_FROM_HANDLE(anv_device
, device
, _device
);
40 struct anv_shader_module
*module
;
42 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
43 assert(pCreateInfo
->flags
== 0);
45 module
= anv_device_alloc(device
, sizeof(*module
) + pCreateInfo
->codeSize
, 8,
46 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
48 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
50 module
->size
= pCreateInfo
->codeSize
;
51 memcpy(module
->data
, pCreateInfo
->pCode
, module
->size
);
53 *pShaderModule
= anv_shader_module_to_handle(module
);
58 VkResult
anv_DestroyShaderModule(
60 VkShaderModule _module
)
62 ANV_FROM_HANDLE(anv_device
, device
, _device
);
63 ANV_FROM_HANDLE(anv_shader_module
, module
, _module
);
65 anv_device_free(device
, module
);
70 VkResult
anv_CreateShader(
72 const VkShaderCreateInfo
* pCreateInfo
,
75 ANV_FROM_HANDLE(anv_device
, device
, _device
);
76 ANV_FROM_HANDLE(anv_shader_module
, module
, pCreateInfo
->module
);
77 struct anv_shader
*shader
;
79 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
);
80 assert(pCreateInfo
->flags
== 0);
82 size_t name_len
= strlen(pCreateInfo
->pName
);
84 if (strcmp(pCreateInfo
->pName
, "main") != 0) {
85 anv_finishme("Multiple shaders per module not really supported");
88 shader
= anv_device_alloc(device
, sizeof(*shader
) + name_len
+ 1, 8,
89 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
91 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
93 shader
->module
= module
;
94 memcpy(shader
->entrypoint
, pCreateInfo
->pName
, name_len
+ 1);
96 *pShader
= anv_shader_to_handle(shader
);
101 VkResult
anv_DestroyShader(
105 ANV_FROM_HANDLE(anv_device
, device
, _device
);
106 ANV_FROM_HANDLE(anv_shader
, shader
, _shader
);
108 anv_device_free(device
, shader
);
114 VkResult
anv_CreatePipelineCache(
116 const VkPipelineCacheCreateInfo
* pCreateInfo
,
117 VkPipelineCache
* pPipelineCache
)
121 stub_return(VK_SUCCESS
);
124 VkResult
anv_DestroyPipelineCache(
126 VkPipelineCache _cache
)
128 /* VkPipelineCache is a dummy object. */
132 size_t anv_GetPipelineCacheSize(
134 VkPipelineCache pipelineCache
)
139 VkResult
anv_GetPipelineCacheData(
141 VkPipelineCache pipelineCache
,
144 stub_return(VK_UNSUPPORTED
);
147 VkResult
anv_MergePipelineCaches(
149 VkPipelineCache destCache
,
150 uint32_t srcCacheCount
,
151 const VkPipelineCache
* pSrcCaches
)
153 stub_return(VK_UNSUPPORTED
);
156 // Pipeline functions
159 emit_vertex_input(struct anv_pipeline
*pipeline
,
160 const VkPipelineVertexInputStateCreateInfo
*info
)
162 const uint32_t num_dwords
= 1 + info
->attributeCount
* 2;
164 bool instancing_enable
[32];
166 pipeline
->vb_used
= 0;
167 for (uint32_t i
= 0; i
< info
->bindingCount
; i
++) {
168 const VkVertexInputBindingDescription
*desc
=
169 &info
->pVertexBindingDescriptions
[i
];
171 pipeline
->vb_used
|= 1 << desc
->binding
;
172 pipeline
->binding_stride
[desc
->binding
] = desc
->strideInBytes
;
174 /* Step rate is programmed per vertex element (attribute), not
175 * binding. Set up a map of which bindings step per instance, for
176 * reference by vertex element setup. */
177 switch (desc
->stepRate
) {
179 case VK_VERTEX_INPUT_STEP_RATE_VERTEX
:
180 instancing_enable
[desc
->binding
] = false;
182 case VK_VERTEX_INPUT_STEP_RATE_INSTANCE
:
183 instancing_enable
[desc
->binding
] = true;
188 p
= anv_batch_emitn(&pipeline
->batch
, num_dwords
,
189 GEN8_3DSTATE_VERTEX_ELEMENTS
);
191 for (uint32_t i
= 0; i
< info
->attributeCount
; i
++) {
192 const VkVertexInputAttributeDescription
*desc
=
193 &info
->pVertexAttributeDescriptions
[i
];
194 const struct anv_format
*format
= anv_format_for_vk_format(desc
->format
);
196 struct GEN8_VERTEX_ELEMENT_STATE element
= {
197 .VertexBufferIndex
= desc
->binding
,
199 .SourceElementFormat
= format
->surface_format
,
200 .EdgeFlagEnable
= false,
201 .SourceElementOffset
= desc
->offsetInBytes
,
202 .Component0Control
= VFCOMP_STORE_SRC
,
203 .Component1Control
= format
->num_channels
>= 2 ? VFCOMP_STORE_SRC
: VFCOMP_STORE_0
,
204 .Component2Control
= format
->num_channels
>= 3 ? VFCOMP_STORE_SRC
: VFCOMP_STORE_0
,
205 .Component3Control
= format
->num_channels
>= 4 ? VFCOMP_STORE_SRC
: VFCOMP_STORE_1_FP
207 GEN8_VERTEX_ELEMENT_STATE_pack(NULL
, &p
[1 + i
* 2], &element
);
209 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_VF_INSTANCING
,
210 .InstancingEnable
= instancing_enable
[desc
->binding
],
211 .VertexElementIndex
= i
,
212 /* Vulkan so far doesn't have an instance divisor, so
213 * this is always 1 (ignored if not instancing). */
214 .InstanceDataStepRate
= 1);
217 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_VF_SGVS
,
218 .VertexIDEnable
= pipeline
->vs_prog_data
.uses_vertexid
,
219 .VertexIDComponentNumber
= 2,
220 .VertexIDElementOffset
= info
->bindingCount
,
221 .InstanceIDEnable
= pipeline
->vs_prog_data
.uses_instanceid
,
222 .InstanceIDComponentNumber
= 3,
223 .InstanceIDElementOffset
= info
->bindingCount
);
227 emit_ia_state(struct anv_pipeline
*pipeline
,
228 const VkPipelineIaStateCreateInfo
*info
,
229 const struct anv_pipeline_create_info
*extra
)
231 static const uint32_t vk_to_gen_primitive_type
[] = {
232 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
233 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
234 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
235 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
236 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
237 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
238 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_ADJ
] = _3DPRIM_LINELIST_ADJ
,
239 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_ADJ
] = _3DPRIM_LINESTRIP_ADJ
,
240 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_ADJ
] = _3DPRIM_TRILIST_ADJ
,
241 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_ADJ
] = _3DPRIM_TRISTRIP_ADJ
,
242 [VK_PRIMITIVE_TOPOLOGY_PATCH
] = _3DPRIM_PATCHLIST_1
244 uint32_t topology
= vk_to_gen_primitive_type
[info
->topology
];
246 if (extra
&& extra
->use_rectlist
)
247 topology
= _3DPRIM_RECTLIST
;
249 struct GEN8_3DSTATE_VF vf
= {
250 GEN8_3DSTATE_VF_header
,
251 .IndexedDrawCutIndexEnable
= info
->primitiveRestartEnable
,
253 GEN8_3DSTATE_VF_pack(NULL
, pipeline
->state_vf
, &vf
);
255 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_VF_TOPOLOGY
,
256 .PrimitiveTopologyType
= topology
);
260 emit_rs_state(struct anv_pipeline
*pipeline
,
261 const VkPipelineRsStateCreateInfo
*info
,
262 const struct anv_pipeline_create_info
*extra
)
264 static const uint32_t vk_to_gen_cullmode
[] = {
265 [VK_CULL_MODE_NONE
] = CULLMODE_NONE
,
266 [VK_CULL_MODE_FRONT
] = CULLMODE_FRONT
,
267 [VK_CULL_MODE_BACK
] = CULLMODE_BACK
,
268 [VK_CULL_MODE_FRONT_AND_BACK
] = CULLMODE_BOTH
271 static const uint32_t vk_to_gen_fillmode
[] = {
272 [VK_FILL_MODE_POINTS
] = RASTER_POINT
,
273 [VK_FILL_MODE_WIREFRAME
] = RASTER_WIREFRAME
,
274 [VK_FILL_MODE_SOLID
] = RASTER_SOLID
277 static const uint32_t vk_to_gen_front_face
[] = {
278 [VK_FRONT_FACE_CCW
] = CounterClockwise
,
279 [VK_FRONT_FACE_CW
] = Clockwise
282 struct GEN8_3DSTATE_SF sf
= {
283 GEN8_3DSTATE_SF_header
,
284 .ViewportTransformEnable
= !(extra
&& extra
->disable_viewport
),
285 .TriangleStripListProvokingVertexSelect
= 0,
286 .LineStripListProvokingVertexSelect
= 0,
287 .TriangleFanProvokingVertexSelect
= 0,
288 .PointWidthSource
= pipeline
->writes_point_size
? Vertex
: State
,
292 /* FINISHME: VkBool32 rasterizerDiscardEnable; */
294 GEN8_3DSTATE_SF_pack(NULL
, pipeline
->state_sf
, &sf
);
296 struct GEN8_3DSTATE_RASTER raster
= {
297 GEN8_3DSTATE_RASTER_header
,
298 .FrontWinding
= vk_to_gen_front_face
[info
->frontFace
],
299 .CullMode
= vk_to_gen_cullmode
[info
->cullMode
],
300 .FrontFaceFillMode
= vk_to_gen_fillmode
[info
->fillMode
],
301 .BackFaceFillMode
= vk_to_gen_fillmode
[info
->fillMode
],
302 .ScissorRectangleEnable
= !(extra
&& extra
->disable_scissor
),
303 .ViewportZClipTestEnable
= info
->depthClipEnable
306 GEN8_3DSTATE_RASTER_pack(NULL
, pipeline
->state_raster
, &raster
);
308 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_SBE
,
309 .ForceVertexURBEntryReadLength
= false,
310 .ForceVertexURBEntryReadOffset
= false,
311 .PointSpriteTextureCoordinateOrigin
= UPPERLEFT
,
312 .NumberofSFOutputAttributes
=
313 pipeline
->wm_prog_data
.num_varying_inputs
);
318 emit_cb_state(struct anv_pipeline
*pipeline
,
319 const VkPipelineCbStateCreateInfo
*info
)
321 struct anv_device
*device
= pipeline
->device
;
323 static const uint32_t vk_to_gen_logic_op
[] = {
324 [VK_LOGIC_OP_COPY
] = LOGICOP_COPY
,
325 [VK_LOGIC_OP_CLEAR
] = LOGICOP_CLEAR
,
326 [VK_LOGIC_OP_AND
] = LOGICOP_AND
,
327 [VK_LOGIC_OP_AND_REVERSE
] = LOGICOP_AND_REVERSE
,
328 [VK_LOGIC_OP_AND_INVERTED
] = LOGICOP_AND_INVERTED
,
329 [VK_LOGIC_OP_NOOP
] = LOGICOP_NOOP
,
330 [VK_LOGIC_OP_XOR
] = LOGICOP_XOR
,
331 [VK_LOGIC_OP_OR
] = LOGICOP_OR
,
332 [VK_LOGIC_OP_NOR
] = LOGICOP_NOR
,
333 [VK_LOGIC_OP_EQUIV
] = LOGICOP_EQUIV
,
334 [VK_LOGIC_OP_INVERT
] = LOGICOP_INVERT
,
335 [VK_LOGIC_OP_OR_REVERSE
] = LOGICOP_OR_REVERSE
,
336 [VK_LOGIC_OP_COPY_INVERTED
] = LOGICOP_COPY_INVERTED
,
337 [VK_LOGIC_OP_OR_INVERTED
] = LOGICOP_OR_INVERTED
,
338 [VK_LOGIC_OP_NAND
] = LOGICOP_NAND
,
339 [VK_LOGIC_OP_SET
] = LOGICOP_SET
,
342 static const uint32_t vk_to_gen_blend
[] = {
343 [VK_BLEND_ZERO
] = BLENDFACTOR_ZERO
,
344 [VK_BLEND_ONE
] = BLENDFACTOR_ONE
,
345 [VK_BLEND_SRC_COLOR
] = BLENDFACTOR_SRC_COLOR
,
346 [VK_BLEND_ONE_MINUS_SRC_COLOR
] = BLENDFACTOR_INV_SRC_COLOR
,
347 [VK_BLEND_DEST_COLOR
] = BLENDFACTOR_DST_COLOR
,
348 [VK_BLEND_ONE_MINUS_DEST_COLOR
] = BLENDFACTOR_INV_DST_COLOR
,
349 [VK_BLEND_SRC_ALPHA
] = BLENDFACTOR_SRC_ALPHA
,
350 [VK_BLEND_ONE_MINUS_SRC_ALPHA
] = BLENDFACTOR_INV_SRC_ALPHA
,
351 [VK_BLEND_DEST_ALPHA
] = BLENDFACTOR_DST_ALPHA
,
352 [VK_BLEND_ONE_MINUS_DEST_ALPHA
] = BLENDFACTOR_INV_DST_ALPHA
,
353 [VK_BLEND_CONSTANT_COLOR
] = BLENDFACTOR_CONST_COLOR
,
354 [VK_BLEND_ONE_MINUS_CONSTANT_COLOR
] = BLENDFACTOR_INV_CONST_COLOR
,
355 [VK_BLEND_CONSTANT_ALPHA
] = BLENDFACTOR_CONST_ALPHA
,
356 [VK_BLEND_ONE_MINUS_CONSTANT_ALPHA
] = BLENDFACTOR_INV_CONST_ALPHA
,
357 [VK_BLEND_SRC_ALPHA_SATURATE
] = BLENDFACTOR_SRC_ALPHA_SATURATE
,
358 [VK_BLEND_SRC1_COLOR
] = BLENDFACTOR_SRC1_COLOR
,
359 [VK_BLEND_ONE_MINUS_SRC1_COLOR
] = BLENDFACTOR_INV_SRC1_COLOR
,
360 [VK_BLEND_SRC1_ALPHA
] = BLENDFACTOR_SRC1_ALPHA
,
361 [VK_BLEND_ONE_MINUS_SRC1_ALPHA
] = BLENDFACTOR_INV_SRC1_ALPHA
,
364 static const uint32_t vk_to_gen_blend_op
[] = {
365 [VK_BLEND_OP_ADD
] = BLENDFUNCTION_ADD
,
366 [VK_BLEND_OP_SUBTRACT
] = BLENDFUNCTION_SUBTRACT
,
367 [VK_BLEND_OP_REVERSE_SUBTRACT
] = BLENDFUNCTION_REVERSE_SUBTRACT
,
368 [VK_BLEND_OP_MIN
] = BLENDFUNCTION_MIN
,
369 [VK_BLEND_OP_MAX
] = BLENDFUNCTION_MAX
,
372 uint32_t num_dwords
= 1 + info
->attachmentCount
* 2;
373 pipeline
->blend_state
=
374 anv_state_pool_alloc(&device
->dynamic_state_pool
, num_dwords
* 4, 64);
376 struct GEN8_BLEND_STATE blend_state
= {
377 .AlphaToCoverageEnable
= info
->alphaToCoverageEnable
,
380 uint32_t *state
= pipeline
->blend_state
.map
;
381 GEN8_BLEND_STATE_pack(NULL
, state
, &blend_state
);
383 for (uint32_t i
= 0; i
< info
->attachmentCount
; i
++) {
384 const VkPipelineCbAttachmentState
*a
= &info
->pAttachments
[i
];
386 struct GEN8_BLEND_STATE_ENTRY entry
= {
387 .LogicOpEnable
= info
->logicOpEnable
,
388 .LogicOpFunction
= vk_to_gen_logic_op
[info
->logicOp
],
389 .ColorBufferBlendEnable
= a
->blendEnable
,
390 .PreBlendSourceOnlyClampEnable
= false,
391 .PreBlendColorClampEnable
= false,
392 .PostBlendColorClampEnable
= false,
393 .SourceBlendFactor
= vk_to_gen_blend
[a
->srcBlendColor
],
394 .DestinationBlendFactor
= vk_to_gen_blend
[a
->destBlendColor
],
395 .ColorBlendFunction
= vk_to_gen_blend_op
[a
->blendOpColor
],
396 .SourceAlphaBlendFactor
= vk_to_gen_blend
[a
->srcBlendAlpha
],
397 .DestinationAlphaBlendFactor
= vk_to_gen_blend
[a
->destBlendAlpha
],
398 .AlphaBlendFunction
= vk_to_gen_blend_op
[a
->blendOpAlpha
],
399 .WriteDisableAlpha
= !(a
->channelWriteMask
& VK_CHANNEL_A_BIT
),
400 .WriteDisableRed
= !(a
->channelWriteMask
& VK_CHANNEL_R_BIT
),
401 .WriteDisableGreen
= !(a
->channelWriteMask
& VK_CHANNEL_G_BIT
),
402 .WriteDisableBlue
= !(a
->channelWriteMask
& VK_CHANNEL_B_BIT
),
405 GEN8_BLEND_STATE_ENTRY_pack(NULL
, state
+ i
* 2 + 1, &entry
);
408 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_BLEND_STATE_POINTERS
,
409 .BlendStatePointer
= pipeline
->blend_state
.offset
,
410 .BlendStatePointerValid
= true);
413 static const uint32_t vk_to_gen_compare_op
[] = {
414 [VK_COMPARE_OP_NEVER
] = COMPAREFUNCTION_NEVER
,
415 [VK_COMPARE_OP_LESS
] = COMPAREFUNCTION_LESS
,
416 [VK_COMPARE_OP_EQUAL
] = COMPAREFUNCTION_EQUAL
,
417 [VK_COMPARE_OP_LESS_EQUAL
] = COMPAREFUNCTION_LEQUAL
,
418 [VK_COMPARE_OP_GREATER
] = COMPAREFUNCTION_GREATER
,
419 [VK_COMPARE_OP_NOT_EQUAL
] = COMPAREFUNCTION_NOTEQUAL
,
420 [VK_COMPARE_OP_GREATER_EQUAL
] = COMPAREFUNCTION_GEQUAL
,
421 [VK_COMPARE_OP_ALWAYS
] = COMPAREFUNCTION_ALWAYS
,
424 static const uint32_t vk_to_gen_stencil_op
[] = {
425 [VK_STENCIL_OP_KEEP
] = 0,
426 [VK_STENCIL_OP_ZERO
] = 0,
427 [VK_STENCIL_OP_REPLACE
] = 0,
428 [VK_STENCIL_OP_INC_CLAMP
] = 0,
429 [VK_STENCIL_OP_DEC_CLAMP
] = 0,
430 [VK_STENCIL_OP_INVERT
] = 0,
431 [VK_STENCIL_OP_INC_WRAP
] = 0,
432 [VK_STENCIL_OP_DEC_WRAP
] = 0
436 emit_ds_state(struct anv_pipeline
*pipeline
,
437 const VkPipelineDsStateCreateInfo
*info
)
440 /* We're going to OR this together with the dynamic state. We need
441 * to make sure it's initialized to something useful.
443 memset(pipeline
->state_wm_depth_stencil
, 0,
444 sizeof(pipeline
->state_wm_depth_stencil
));
448 /* VkBool32 depthBoundsEnable; // optional (depth_bounds_test) */
450 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
451 .DepthTestEnable
= info
->depthTestEnable
,
452 .DepthBufferWriteEnable
= info
->depthWriteEnable
,
453 .DepthTestFunction
= vk_to_gen_compare_op
[info
->depthCompareOp
],
454 .DoubleSidedStencilEnable
= true,
456 .StencilTestEnable
= info
->stencilTestEnable
,
457 .StencilFailOp
= vk_to_gen_stencil_op
[info
->front
.stencilFailOp
],
458 .StencilPassDepthPassOp
= vk_to_gen_stencil_op
[info
->front
.stencilPassOp
],
459 .StencilPassDepthFailOp
= vk_to_gen_stencil_op
[info
->front
.stencilDepthFailOp
],
460 .StencilTestFunction
= vk_to_gen_compare_op
[info
->front
.stencilCompareOp
],
461 .BackfaceStencilFailOp
= vk_to_gen_stencil_op
[info
->back
.stencilFailOp
],
462 .BackfaceStencilPassDepthPassOp
= vk_to_gen_stencil_op
[info
->back
.stencilPassOp
],
463 .BackfaceStencilPassDepthFailOp
=vk_to_gen_stencil_op
[info
->back
.stencilDepthFailOp
],
464 .BackfaceStencilTestFunction
= vk_to_gen_compare_op
[info
->back
.stencilCompareOp
],
467 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, pipeline
->state_wm_depth_stencil
, &wm_depth_stencil
);
471 anv_pipeline_destroy(struct anv_device
*device
,
472 struct anv_object
*object
,
473 VkObjectType obj_type
)
475 struct anv_pipeline
*pipeline
= (struct anv_pipeline
*) object
;
477 assert(obj_type
== VK_OBJECT_TYPE_PIPELINE
);
479 anv_DestroyPipeline(anv_device_to_handle(device
),
480 anv_pipeline_to_handle(pipeline
));
486 const VkGraphicsPipelineCreateInfo
* pCreateInfo
,
487 const struct anv_pipeline_create_info
* extra
,
488 VkPipeline
* pPipeline
)
490 ANV_FROM_HANDLE(anv_device
, device
, _device
);
491 struct anv_pipeline
*pipeline
;
493 uint32_t offset
, length
;
495 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
497 pipeline
= anv_device_alloc(device
, sizeof(*pipeline
), 8,
498 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
499 if (pipeline
== NULL
)
500 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
502 pipeline
->base
.destructor
= anv_pipeline_destroy
;
503 pipeline
->device
= device
;
504 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
505 memset(pipeline
->shaders
, 0, sizeof(pipeline
->shaders
));
507 result
= anv_reloc_list_init(&pipeline
->batch
.relocs
, device
);
508 if (result
!= VK_SUCCESS
) {
509 anv_device_free(device
, pipeline
);
512 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
513 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
515 anv_state_stream_init(&pipeline
->program_stream
,
516 &device
->instruction_block_pool
);
518 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
519 pipeline
->shaders
[pCreateInfo
->pStages
[i
].stage
] =
520 anv_shader_from_handle(pCreateInfo
->pStages
[i
].shader
);
523 if (pCreateInfo
->pTessState
)
524 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESS_STATE_CREATE_INFO");
525 if (pCreateInfo
->pVpState
)
526 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO");
527 if (pCreateInfo
->pMsState
)
528 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO");
530 pipeline
->use_repclear
= extra
&& extra
->use_repclear
;
532 anv_compiler_run(device
->compiler
, pipeline
);
534 /* FIXME: The compiler dead-codes FS inputs when we don't have a VS, so we
535 * hard code this to num_attributes - 2. This is because the attributes
536 * include VUE header and position, which aren't counted as varying
538 if (pipeline
->vs_simd8
== NO_KERNEL
) {
539 pipeline
->wm_prog_data
.num_varying_inputs
=
540 pCreateInfo
->pVertexInputState
->attributeCount
- 2;
543 assert(pCreateInfo
->pVertexInputState
);
544 emit_vertex_input(pipeline
, pCreateInfo
->pVertexInputState
);
545 assert(pCreateInfo
->pIaState
);
546 emit_ia_state(pipeline
, pCreateInfo
->pIaState
, extra
);
547 assert(pCreateInfo
->pRsState
);
548 emit_rs_state(pipeline
, pCreateInfo
->pRsState
, extra
);
549 emit_ds_state(pipeline
, pCreateInfo
->pDsState
);
550 emit_cb_state(pipeline
, pCreateInfo
->pCbState
);
552 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_VF_STATISTICS
,
553 .StatisticsEnable
= true);
554 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_HS
, .Enable
= false);
555 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_TE
, .TEEnable
= false);
556 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_DS
, .FunctionEnable
= false);
557 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_STREAMOUT
, .SOFunctionEnable
= false);
559 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_VS
,
560 .ConstantBufferOffset
= 0,
561 .ConstantBufferSize
= 4);
562 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_GS
,
563 .ConstantBufferOffset
= 4,
564 .ConstantBufferSize
= 4);
565 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_PUSH_CONSTANT_ALLOC_PS
,
566 .ConstantBufferOffset
= 8,
567 .ConstantBufferSize
= 4);
569 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_WM_CHROMAKEY
,
570 .ChromaKeyKillEnable
= false);
571 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_SBE_SWIZ
);
572 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_AA_LINE_PARAMETERS
);
574 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_CLIP
,
576 .ViewportXYClipTestEnable
= !(extra
&& extra
->disable_viewport
),
577 .MinimumPointWidth
= 0.125,
578 .MaximumPointWidth
= 255.875);
580 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_WM
,
581 .StatisticsEnable
= true,
582 .LineEndCapAntialiasingRegionWidth
= _05pixels
,
583 .LineAntialiasingRegionWidth
= _10pixels
,
584 .EarlyDepthStencilControl
= NORMAL
,
585 .ForceThreadDispatchEnable
= NORMAL
,
586 .PointRasterizationRule
= RASTRULE_UPPER_RIGHT
,
587 .BarycentricInterpolationMode
=
588 pipeline
->wm_prog_data
.barycentric_interp_modes
);
590 uint32_t samples
= 1;
591 uint32_t log2_samples
= __builtin_ffs(samples
) - 1;
592 bool enable_sampling
= samples
> 1 ? true : false;
594 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_MULTISAMPLE
,
595 .PixelPositionOffsetEnable
= enable_sampling
,
596 .PixelLocation
= CENTER
,
597 .NumberofMultisamples
= log2_samples
);
599 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_SAMPLE_MASK
,
600 .SampleMask
= 0xffff);
602 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_URB_VS
,
603 .VSURBStartingAddress
= pipeline
->urb
.vs_start
,
604 .VSURBEntryAllocationSize
= pipeline
->urb
.vs_size
- 1,
605 .VSNumberofURBEntries
= pipeline
->urb
.nr_vs_entries
);
607 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_URB_GS
,
608 .GSURBStartingAddress
= pipeline
->urb
.gs_start
,
609 .GSURBEntryAllocationSize
= pipeline
->urb
.gs_size
- 1,
610 .GSNumberofURBEntries
= pipeline
->urb
.nr_gs_entries
);
612 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_URB_HS
,
613 .HSURBStartingAddress
= pipeline
->urb
.vs_start
,
614 .HSURBEntryAllocationSize
= 0,
615 .HSNumberofURBEntries
= 0);
617 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_URB_DS
,
618 .DSURBStartingAddress
= pipeline
->urb
.vs_start
,
619 .DSURBEntryAllocationSize
= 0,
620 .DSNumberofURBEntries
= 0);
622 const struct brw_gs_prog_data
*gs_prog_data
= &pipeline
->gs_prog_data
;
624 length
= (gs_prog_data
->base
.vue_map
.num_slots
+ 1) / 2 - offset
;
626 if (pipeline
->gs_vec4
== NO_KERNEL
)
627 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_GS
, .Enable
= false);
629 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_GS
,
630 .SingleProgramFlow
= false,
631 .KernelStartPointer
= pipeline
->gs_vec4
,
632 .VectorMaskEnable
= Vmask
,
634 .BindingTableEntryCount
= 0,
635 .ExpectedVertexCount
= pipeline
->gs_vertex_count
,
637 .ScratchSpaceBasePointer
= pipeline
->scratch_start
[VK_SHADER_STAGE_GEOMETRY
],
638 .PerThreadScratchSpace
= ffs(gs_prog_data
->base
.base
.total_scratch
/ 2048),
640 .OutputVertexSize
= gs_prog_data
->output_vertex_size_hwords
* 2 - 1,
641 .OutputTopology
= gs_prog_data
->output_topology
,
642 .VertexURBEntryReadLength
= gs_prog_data
->base
.urb_read_length
,
643 .DispatchGRFStartRegisterForURBData
=
644 gs_prog_data
->base
.base
.dispatch_grf_start_reg
,
646 .MaximumNumberofThreads
= device
->info
.max_gs_threads
,
647 .ControlDataHeaderSize
= gs_prog_data
->control_data_header_size_hwords
,
648 //pipeline->gs_prog_data.dispatch_mode |
649 .StatisticsEnable
= true,
650 .IncludePrimitiveID
= gs_prog_data
->include_primitive_id
,
651 .ReorderMode
= TRAILING
,
654 .ControlDataFormat
= gs_prog_data
->control_data_format
,
656 /* FIXME: mesa sets this based on ctx->Transform.ClipPlanesEnabled:
657 * UserClipDistanceClipTestEnableBitmask_3DSTATE_GS(v)
658 * UserClipDistanceCullTestEnableBitmask(v)
661 .VertexURBEntryOutputReadOffset
= offset
,
662 .VertexURBEntryOutputLength
= length
);
664 const struct brw_vue_prog_data
*vue_prog_data
= &pipeline
->vs_prog_data
.base
;
665 /* Skip the VUE header and position slots */
667 length
= (vue_prog_data
->vue_map
.num_slots
+ 1) / 2 - offset
;
669 if (pipeline
->vs_simd8
== NO_KERNEL
|| (extra
&& extra
->disable_vs
))
670 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_VS
,
671 .FunctionEnable
= false,
672 .VertexURBEntryOutputReadOffset
= 1,
673 /* Even if VS is disabled, SBE still gets the amount of
674 * vertex data to read from this field. We use attribute
675 * count - 1, as we don't count the VUE header here. */
676 .VertexURBEntryOutputLength
=
677 DIV_ROUND_UP(pCreateInfo
->pVertexInputState
->attributeCount
- 1, 2));
679 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_VS
,
680 .KernelStartPointer
= pipeline
->vs_simd8
,
681 .SingleVertexDispatch
= Multiple
,
682 .VectorMaskEnable
= Dmask
,
684 .BindingTableEntryCount
=
685 vue_prog_data
->base
.binding_table
.size_bytes
/ 4,
686 .ThreadDispatchPriority
= Normal
,
687 .FloatingPointMode
= IEEE754
,
688 .IllegalOpcodeExceptionEnable
= false,
689 .AccessesUAV
= false,
690 .SoftwareExceptionEnable
= false,
692 .ScratchSpaceBasePointer
= pipeline
->scratch_start
[VK_SHADER_STAGE_VERTEX
],
693 .PerThreadScratchSpace
= ffs(vue_prog_data
->base
.total_scratch
/ 2048),
695 .DispatchGRFStartRegisterForURBData
=
696 vue_prog_data
->base
.dispatch_grf_start_reg
,
697 .VertexURBEntryReadLength
= vue_prog_data
->urb_read_length
,
698 .VertexURBEntryReadOffset
= 0,
700 .MaximumNumberofThreads
= device
->info
.max_vs_threads
- 1,
701 .StatisticsEnable
= false,
702 .SIMD8DispatchEnable
= true,
703 .VertexCacheDisable
= false,
704 .FunctionEnable
= true,
706 .VertexURBEntryOutputReadOffset
= offset
,
707 .VertexURBEntryOutputLength
= length
,
708 .UserClipDistanceClipTestEnableBitmask
= 0,
709 .UserClipDistanceCullTestEnableBitmask
= 0);
711 const struct brw_wm_prog_data
*wm_prog_data
= &pipeline
->wm_prog_data
;
712 uint32_t ksp0
, ksp2
, grf_start0
, grf_start2
;
716 if (pipeline
->ps_simd8
!= NO_KERNEL
) {
717 ksp0
= pipeline
->ps_simd8
;
718 grf_start0
= wm_prog_data
->base
.dispatch_grf_start_reg
;
719 if (pipeline
->ps_simd16
!= NO_KERNEL
) {
720 ksp2
= pipeline
->ps_simd16
;
721 grf_start2
= wm_prog_data
->dispatch_grf_start_reg_16
;
723 } else if (pipeline
->ps_simd16
!= NO_KERNEL
) {
724 ksp0
= pipeline
->ps_simd16
;
725 grf_start0
= wm_prog_data
->dispatch_grf_start_reg_16
;
727 unreachable("no ps shader");
730 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_PS
,
731 .KernelStartPointer0
= ksp0
,
733 .SingleProgramFlow
= false,
734 .VectorMaskEnable
= true,
737 .ScratchSpaceBasePointer
= pipeline
->scratch_start
[VK_SHADER_STAGE_FRAGMENT
],
738 .PerThreadScratchSpace
= ffs(wm_prog_data
->base
.total_scratch
/ 2048),
740 .MaximumNumberofThreadsPerPSD
= 64 - 2,
741 .PositionXYOffsetSelect
= wm_prog_data
->uses_pos_offset
?
742 POSOFFSET_SAMPLE
: POSOFFSET_NONE
,
743 .PushConstantEnable
= wm_prog_data
->base
.nr_params
> 0,
744 ._8PixelDispatchEnable
= pipeline
->ps_simd8
!= NO_KERNEL
,
745 ._16PixelDispatchEnable
= pipeline
->ps_simd16
!= NO_KERNEL
,
746 ._32PixelDispatchEnable
= false,
748 .DispatchGRFStartRegisterForConstantSetupData0
= grf_start0
,
749 .DispatchGRFStartRegisterForConstantSetupData1
= 0,
750 .DispatchGRFStartRegisterForConstantSetupData2
= grf_start2
,
752 .KernelStartPointer1
= 0,
753 .KernelStartPointer2
= ksp2
);
755 bool per_sample_ps
= false;
756 anv_batch_emit(&pipeline
->batch
, GEN8_3DSTATE_PS_EXTRA
,
757 .PixelShaderValid
= true,
758 .PixelShaderKillsPixel
= wm_prog_data
->uses_kill
,
759 .PixelShaderComputedDepthMode
= wm_prog_data
->computed_depth_mode
,
760 .AttributeEnable
= wm_prog_data
->num_varying_inputs
> 0,
761 .oMaskPresenttoRenderTarget
= wm_prog_data
->uses_omask
,
762 .PixelShaderIsPerSample
= per_sample_ps
);
764 *pPipeline
= anv_pipeline_to_handle(pipeline
);
769 VkResult
anv_DestroyPipeline(
771 VkPipeline _pipeline
)
773 ANV_FROM_HANDLE(anv_device
, device
, _device
);
774 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
776 anv_compiler_free(pipeline
);
777 anv_reloc_list_finish(&pipeline
->batch
.relocs
, pipeline
->device
);
778 anv_state_stream_finish(&pipeline
->program_stream
);
779 anv_state_pool_free(&device
->dynamic_state_pool
, pipeline
->blend_state
);
780 anv_device_free(pipeline
->device
, pipeline
);
785 VkResult
anv_CreateGraphicsPipelines(
787 VkPipelineCache pipelineCache
,
789 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
790 VkPipeline
* pPipelines
)
792 ANV_FROM_HANDLE(anv_device
, device
, _device
);
793 VkResult result
= VK_SUCCESS
;
796 for (; i
< count
; i
++) {
797 result
= anv_pipeline_create(_device
, &pCreateInfos
[i
],
798 NULL
, &pPipelines
[i
]);
799 if (result
!= VK_SUCCESS
) {
800 for (unsigned j
= 0; j
< i
; j
++) {
801 anv_pipeline_destroy(device
, (struct anv_object
*)pPipelines
[j
],
802 VK_OBJECT_TYPE_PIPELINE
);
812 static VkResult
anv_compute_pipeline_create(
814 const VkComputePipelineCreateInfo
* pCreateInfo
,
815 VkPipeline
* pPipeline
)
817 ANV_FROM_HANDLE(anv_device
, device
, _device
);
818 struct anv_pipeline
*pipeline
;
821 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO
);
823 pipeline
= anv_device_alloc(device
, sizeof(*pipeline
), 8,
824 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
825 if (pipeline
== NULL
)
826 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
828 pipeline
->base
.destructor
= anv_pipeline_destroy
;
829 pipeline
->device
= device
;
830 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
832 result
= anv_reloc_list_init(&pipeline
->batch
.relocs
, device
);
833 if (result
!= VK_SUCCESS
) {
834 anv_device_free(device
, pipeline
);
837 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
838 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
840 anv_state_stream_init(&pipeline
->program_stream
,
841 &device
->instruction_block_pool
);
843 memset(pipeline
->shaders
, 0, sizeof(pipeline
->shaders
));
845 pipeline
->shaders
[VK_SHADER_STAGE_COMPUTE
] =
846 anv_shader_from_handle(pCreateInfo
->cs
.shader
);
848 pipeline
->use_repclear
= false;
850 anv_compiler_run(device
->compiler
, pipeline
);
852 const struct brw_cs_prog_data
*cs_prog_data
= &pipeline
->cs_prog_data
;
854 anv_batch_emit(&pipeline
->batch
, GEN8_MEDIA_VFE_STATE
,
855 .ScratchSpaceBasePointer
= pipeline
->scratch_start
[VK_SHADER_STAGE_FRAGMENT
],
856 .PerThreadScratchSpace
= ffs(cs_prog_data
->base
.total_scratch
/ 2048),
857 .ScratchSpaceBasePointerHigh
= 0,
860 .MaximumNumberofThreads
= device
->info
.max_cs_threads
- 1,
861 .NumberofURBEntries
= 2,
862 .ResetGatewayTimer
= true,
863 .BypassGatewayControl
= true,
864 .URBEntryAllocationSize
= 2,
865 .CURBEAllocationSize
= 0);
867 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
868 uint32_t group_size
= prog_data
->local_size
[0] *
869 prog_data
->local_size
[1] * prog_data
->local_size
[2];
870 pipeline
->cs_thread_width_max
= DIV_ROUND_UP(group_size
, prog_data
->simd_size
);
871 uint32_t remainder
= group_size
& (prog_data
->simd_size
- 1);
874 pipeline
->cs_right_mask
= ~0u >> (32 - remainder
);
876 pipeline
->cs_right_mask
= ~0u >> (32 - prog_data
->simd_size
);
879 *pPipeline
= anv_pipeline_to_handle(pipeline
);
884 VkResult
anv_CreateComputePipelines(
886 VkPipelineCache pipelineCache
,
888 const VkComputePipelineCreateInfo
* pCreateInfos
,
889 VkPipeline
* pPipelines
)
891 ANV_FROM_HANDLE(anv_device
, device
, _device
);
892 VkResult result
= VK_SUCCESS
;
895 for (; i
< count
; i
++) {
896 result
= anv_compute_pipeline_create(_device
, &pCreateInfos
[i
],
898 if (result
!= VK_SUCCESS
) {
899 for (unsigned j
= 0; j
< i
; j
++) {
900 anv_pipeline_destroy(device
, (struct anv_object
*)pPipelines
[j
],
901 VK_OBJECT_TYPE_PIPELINE
);
911 // Pipeline layout functions
913 VkResult
anv_CreatePipelineLayout(
915 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
916 VkPipelineLayout
* pPipelineLayout
)
918 ANV_FROM_HANDLE(anv_device
, device
, _device
);
919 struct anv_pipeline_layout
*layout
;
921 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
923 layout
= anv_device_alloc(device
, sizeof(*layout
), 8,
924 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
926 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
928 layout
->num_sets
= pCreateInfo
->descriptorSetCount
;
930 uint32_t surface_start
[VK_SHADER_STAGE_NUM
] = { 0, };
931 uint32_t sampler_start
[VK_SHADER_STAGE_NUM
] = { 0, };
933 for (uint32_t s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++) {
934 layout
->stage
[s
].surface_count
= 0;
935 layout
->stage
[s
].sampler_count
= 0;
938 for (uint32_t i
= 0; i
< pCreateInfo
->descriptorSetCount
; i
++) {
939 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
940 pCreateInfo
->pSetLayouts
[i
]);
942 layout
->set
[i
].layout
= set_layout
;
943 for (uint32_t s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++) {
944 layout
->set
[i
].surface_start
[s
] = surface_start
[s
];
945 surface_start
[s
] += set_layout
->stage
[s
].surface_count
;
946 layout
->set
[i
].sampler_start
[s
] = sampler_start
[s
];
947 sampler_start
[s
] += set_layout
->stage
[s
].sampler_count
;
949 layout
->stage
[s
].surface_count
+= set_layout
->stage
[s
].surface_count
;
950 layout
->stage
[s
].sampler_count
+= set_layout
->stage
[s
].sampler_count
;
954 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
959 VkResult
anv_DestroyPipelineLayout(
961 VkPipelineLayout _pipelineLayout
)
963 ANV_FROM_HANDLE(anv_device
, device
, _device
);
964 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
966 anv_device_free(device
, pipeline_layout
);