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
30 #include "anv_private.h"
32 VkResult
gen8_CreateDynamicRasterState(
34 const VkDynamicRasterStateCreateInfo
* pCreateInfo
,
35 VkDynamicRasterState
* pState
)
37 ANV_FROM_HANDLE(anv_device
, device
, _device
);
38 struct anv_dynamic_rs_state
*state
;
40 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO
);
42 state
= anv_device_alloc(device
, sizeof(*state
), 8,
43 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
45 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
47 struct GEN8_3DSTATE_SF sf
= {
48 GEN8_3DSTATE_SF_header
,
49 .LineWidth
= pCreateInfo
->lineWidth
,
52 GEN8_3DSTATE_SF_pack(NULL
, state
->gen8
.sf
, &sf
);
54 bool enable_bias
= pCreateInfo
->depthBias
!= 0.0f
||
55 pCreateInfo
->slopeScaledDepthBias
!= 0.0f
;
56 struct GEN8_3DSTATE_RASTER raster
= {
57 .GlobalDepthOffsetEnableSolid
= enable_bias
,
58 .GlobalDepthOffsetEnableWireframe
= enable_bias
,
59 .GlobalDepthOffsetEnablePoint
= enable_bias
,
60 .GlobalDepthOffsetConstant
= pCreateInfo
->depthBias
,
61 .GlobalDepthOffsetScale
= pCreateInfo
->slopeScaledDepthBias
,
62 .GlobalDepthOffsetClamp
= pCreateInfo
->depthBiasClamp
65 GEN8_3DSTATE_RASTER_pack(NULL
, state
->gen8
.raster
, &raster
);
67 *pState
= anv_dynamic_rs_state_to_handle(state
);
73 gen8_fill_buffer_surface_state(void *state
, const struct anv_format
*format
,
74 uint32_t offset
, uint32_t range
)
76 /* This assumes RGBA float format. */
78 uint32_t num_elements
= range
/ stride
;
80 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
81 .SurfaceType
= SURFTYPE_BUFFER
,
82 .SurfaceArray
= false,
83 .SurfaceFormat
= format
->surface_format
,
84 .SurfaceVerticalAlignment
= VALIGN4
,
85 .SurfaceHorizontalAlignment
= HALIGN4
,
87 .SamplerL2BypassModeDisable
= true,
88 .RenderCacheReadWriteMode
= WriteOnlyCache
,
89 .MemoryObjectControlState
= GEN8_MOCS
,
90 .Height
= (num_elements
>> 7) & 0x3fff,
91 .Width
= num_elements
& 0x7f,
92 .Depth
= (num_elements
>> 21) & 0x3f,
93 .SurfacePitch
= stride
- 1,
94 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
95 .ShaderChannelSelectRed
= SCS_RED
,
96 .ShaderChannelSelectGreen
= SCS_GREEN
,
97 .ShaderChannelSelectBlue
= SCS_BLUE
,
98 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
99 /* FIXME: We assume that the image must be bound at this time. */
100 .SurfaceBaseAddress
= { NULL
, offset
},
103 GEN8_RENDER_SURFACE_STATE_pack(NULL
, state
, &surface_state
);
106 VkResult
gen8_CreateBufferView(
108 const VkBufferViewCreateInfo
* pCreateInfo
,
111 ANV_FROM_HANDLE(anv_device
, device
, _device
);
112 struct anv_buffer_view
*view
;
115 result
= anv_buffer_view_create(device
, pCreateInfo
, &view
);
116 if (result
!= VK_SUCCESS
)
119 const struct anv_format
*format
=
120 anv_format_for_vk_format(pCreateInfo
->format
);
122 gen8_fill_buffer_surface_state(view
->view
.surface_state
.map
, format
,
123 view
->view
.offset
, pCreateInfo
->range
);
125 *pView
= anv_buffer_view_to_handle(view
);
130 static const uint8_t anv_halign
[] = {
136 static const uint8_t anv_valign
[] = {
143 gen8_image_view_init(struct anv_image_view
*iview
,
144 struct anv_device
*device
,
145 const VkImageViewCreateInfo
* pCreateInfo
,
146 struct anv_cmd_buffer
*cmd_buffer
)
148 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
150 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
151 struct anv_surface_view
*view
= &iview
->view
;
152 struct anv_surface
*surface
=
153 anv_image_get_surface_for_aspect(image
, range
->aspect
);
155 const struct anv_format
*format_info
=
156 anv_format_for_vk_format(pCreateInfo
->format
);
158 const struct anv_image_view_info
*view_type_info
=
159 anv_image_view_info_for_vk_image_view_type(pCreateInfo
->viewType
);
161 if (pCreateInfo
->viewType
!= VK_IMAGE_VIEW_TYPE_2D
)
162 anv_finishme("non-2D image views");
164 view
->bo
= image
->bo
;
165 view
->offset
= image
->offset
+ surface
->offset
;
166 view
->format
= format_info
;
168 iview
->extent
= (VkExtent3D
) {
169 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
170 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
171 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
175 if (range
->arraySize
> 1) {
176 depth
= range
->arraySize
;
177 } else if (image
->extent
.depth
> 1) {
178 depth
= image
->extent
.depth
;
181 static const uint32_t vk_to_gen_swizzle
[] = {
182 [VK_CHANNEL_SWIZZLE_ZERO
] = SCS_ZERO
,
183 [VK_CHANNEL_SWIZZLE_ONE
] = SCS_ONE
,
184 [VK_CHANNEL_SWIZZLE_R
] = SCS_RED
,
185 [VK_CHANNEL_SWIZZLE_G
] = SCS_GREEN
,
186 [VK_CHANNEL_SWIZZLE_B
] = SCS_BLUE
,
187 [VK_CHANNEL_SWIZZLE_A
] = SCS_ALPHA
190 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
191 .SurfaceType
= view_type_info
->surface_type
,
192 .SurfaceArray
= image
->array_size
> 1,
193 .SurfaceFormat
= format_info
->surface_format
,
194 .SurfaceVerticalAlignment
= anv_valign
[surface
->v_align
],
195 .SurfaceHorizontalAlignment
= anv_halign
[surface
->h_align
],
196 .TileMode
= surface
->tile_mode
,
197 .VerticalLineStride
= 0,
198 .VerticalLineStrideOffset
= 0,
199 .SamplerL2BypassModeDisable
= true,
200 .RenderCacheReadWriteMode
= WriteOnlyCache
,
201 .MemoryObjectControlState
= GEN8_MOCS
,
203 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
204 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
205 * both Base Mip Level fields nonzero".
209 .SurfaceQPitch
= surface
->qpitch
>> 2,
210 .Height
= image
->extent
.height
- 1,
211 .Width
= image
->extent
.width
- 1,
213 .SurfacePitch
= surface
->stride
- 1,
214 .MinimumArrayElement
= range
->baseArraySlice
,
215 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
219 /* For sampler surfaces, the hardware interprets field MIPCount/LOD as
220 * MIPCount. The range of levels accessible by the sampler engine is
221 * [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
223 .MIPCountLOD
= range
->mipLevels
- 1,
224 .SurfaceMinLOD
= range
->baseMipLevel
,
226 .AuxiliarySurfaceMode
= AUX_NONE
,
228 .GreenClearColor
= 0,
230 .AlphaClearColor
= 0,
231 .ShaderChannelSelectRed
= vk_to_gen_swizzle
[pCreateInfo
->channels
.r
],
232 .ShaderChannelSelectGreen
= vk_to_gen_swizzle
[pCreateInfo
->channels
.g
],
233 .ShaderChannelSelectBlue
= vk_to_gen_swizzle
[pCreateInfo
->channels
.b
],
234 .ShaderChannelSelectAlpha
= vk_to_gen_swizzle
[pCreateInfo
->channels
.a
],
235 .ResourceMinLOD
= 0.0,
236 .SurfaceBaseAddress
= { NULL
, view
->offset
},
240 view
->surface_state
=
241 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
243 view
->surface_state
=
244 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
247 GEN8_RENDER_SURFACE_STATE_pack(NULL
, view
->surface_state
.map
, &surface_state
);
251 gen8_color_attachment_view_init(struct anv_color_attachment_view
*aview
,
252 struct anv_device
*device
,
253 const VkAttachmentViewCreateInfo
* pCreateInfo
,
254 struct anv_cmd_buffer
*cmd_buffer
)
256 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
257 struct anv_surface_view
*view
= &aview
->view
;
258 struct anv_surface
*surface
=
259 anv_image_get_surface_for_color_attachment(image
);
260 const struct anv_format
*format_info
=
261 anv_format_for_vk_format(pCreateInfo
->format
);
263 aview
->base
.attachment_type
= ANV_ATTACHMENT_VIEW_TYPE_COLOR
;
265 anv_assert(pCreateInfo
->arraySize
> 0);
266 anv_assert(pCreateInfo
->mipLevel
< image
->levels
);
267 anv_assert(pCreateInfo
->baseArraySlice
+ pCreateInfo
->arraySize
<= image
->array_size
);
269 view
->bo
= image
->bo
;
270 view
->offset
= image
->offset
+ surface
->offset
;
271 view
->format
= anv_format_for_vk_format(pCreateInfo
->format
);
273 aview
->base
.extent
= (VkExtent3D
) {
274 .width
= anv_minify(image
->extent
.width
, pCreateInfo
->mipLevel
),
275 .height
= anv_minify(image
->extent
.height
, pCreateInfo
->mipLevel
),
276 .depth
= anv_minify(image
->extent
.depth
, pCreateInfo
->mipLevel
),
280 if (pCreateInfo
->arraySize
> 1) {
281 depth
= pCreateInfo
->arraySize
;
282 } else if (image
->extent
.depth
> 1) {
283 depth
= image
->extent
.depth
;
287 view
->surface_state
=
288 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
290 view
->surface_state
=
291 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
294 struct GEN8_RENDER_SURFACE_STATE surface_state
= {
295 .SurfaceType
= SURFTYPE_2D
,
296 .SurfaceArray
= image
->array_size
> 1,
297 .SurfaceFormat
= format_info
->surface_format
,
298 .SurfaceVerticalAlignment
= anv_valign
[surface
->v_align
],
299 .SurfaceHorizontalAlignment
= anv_halign
[surface
->h_align
],
300 .TileMode
= surface
->tile_mode
,
301 .VerticalLineStride
= 0,
302 .VerticalLineStrideOffset
= 0,
303 .SamplerL2BypassModeDisable
= true,
304 .RenderCacheReadWriteMode
= WriteOnlyCache
,
305 .MemoryObjectControlState
= GEN8_MOCS
,
307 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
308 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
309 * both Base Mip Level fields nonzero".
313 .SurfaceQPitch
= surface
->qpitch
>> 2,
314 .Height
= image
->extent
.height
- 1,
315 .Width
= image
->extent
.width
- 1,
317 .SurfacePitch
= surface
->stride
- 1,
318 .MinimumArrayElement
= pCreateInfo
->baseArraySlice
,
319 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
323 /* For render target surfaces, the hardware interprets field MIPCount/LOD as
324 * LOD. The Broadwell PRM says:
326 * MIPCountLOD defines the LOD that will be rendered into.
327 * SurfaceMinLOD is ignored.
330 .MIPCountLOD
= pCreateInfo
->mipLevel
,
332 .AuxiliarySurfaceMode
= AUX_NONE
,
334 .GreenClearColor
= 0,
336 .AlphaClearColor
= 0,
337 .ShaderChannelSelectRed
= SCS_RED
,
338 .ShaderChannelSelectGreen
= SCS_GREEN
,
339 .ShaderChannelSelectBlue
= SCS_BLUE
,
340 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
341 .ResourceMinLOD
= 0.0,
342 .SurfaceBaseAddress
= { NULL
, view
->offset
},
345 GEN8_RENDER_SURFACE_STATE_pack(NULL
, view
->surface_state
.map
, &surface_state
);
348 VkResult
gen8_CreateSampler(
350 const VkSamplerCreateInfo
* pCreateInfo
,
353 ANV_FROM_HANDLE(anv_device
, device
, _device
);
354 struct anv_sampler
*sampler
;
355 uint32_t mag_filter
, min_filter
, max_anisotropy
;
357 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
359 sampler
= anv_device_alloc(device
, sizeof(*sampler
), 8,
360 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
362 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
364 static const uint32_t vk_to_gen_tex_filter
[] = {
365 [VK_TEX_FILTER_NEAREST
] = MAPFILTER_NEAREST
,
366 [VK_TEX_FILTER_LINEAR
] = MAPFILTER_LINEAR
369 static const uint32_t vk_to_gen_mipmap_mode
[] = {
370 [VK_TEX_MIPMAP_MODE_BASE
] = MIPFILTER_NONE
,
371 [VK_TEX_MIPMAP_MODE_NEAREST
] = MIPFILTER_NEAREST
,
372 [VK_TEX_MIPMAP_MODE_LINEAR
] = MIPFILTER_LINEAR
375 static const uint32_t vk_to_gen_tex_address
[] = {
376 [VK_TEX_ADDRESS_WRAP
] = TCM_WRAP
,
377 [VK_TEX_ADDRESS_MIRROR
] = TCM_MIRROR
,
378 [VK_TEX_ADDRESS_CLAMP
] = TCM_CLAMP
,
379 [VK_TEX_ADDRESS_MIRROR_ONCE
] = TCM_MIRROR_ONCE
,
380 [VK_TEX_ADDRESS_CLAMP_BORDER
] = TCM_CLAMP_BORDER
,
383 static const uint32_t vk_to_gen_compare_op
[] = {
384 [VK_COMPARE_OP_NEVER
] = PREFILTEROPNEVER
,
385 [VK_COMPARE_OP_LESS
] = PREFILTEROPLESS
,
386 [VK_COMPARE_OP_EQUAL
] = PREFILTEROPEQUAL
,
387 [VK_COMPARE_OP_LESS_EQUAL
] = PREFILTEROPLEQUAL
,
388 [VK_COMPARE_OP_GREATER
] = PREFILTEROPGREATER
,
389 [VK_COMPARE_OP_NOT_EQUAL
] = PREFILTEROPNOTEQUAL
,
390 [VK_COMPARE_OP_GREATER_EQUAL
] = PREFILTEROPGEQUAL
,
391 [VK_COMPARE_OP_ALWAYS
] = PREFILTEROPALWAYS
,
394 if (pCreateInfo
->maxAnisotropy
> 1) {
395 mag_filter
= MAPFILTER_ANISOTROPIC
;
396 min_filter
= MAPFILTER_ANISOTROPIC
;
397 max_anisotropy
= (pCreateInfo
->maxAnisotropy
- 2) / 2;
399 mag_filter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
];
400 min_filter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
];
401 max_anisotropy
= RATIO21
;
404 struct GEN8_SAMPLER_STATE sampler_state
= {
405 .SamplerDisable
= false,
406 .TextureBorderColorMode
= DX10OGL
,
407 .LODPreClampMode
= 0,
409 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipMode
],
410 .MagModeFilter
= mag_filter
,
411 .MinModeFilter
= min_filter
,
412 .TextureLODBias
= pCreateInfo
->mipLodBias
* 256,
413 .AnisotropicAlgorithm
= EWAApproximation
,
414 .MinLOD
= pCreateInfo
->minLod
,
415 .MaxLOD
= pCreateInfo
->maxLod
,
416 .ChromaKeyEnable
= 0,
419 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
420 .CubeSurfaceControlMode
= 0,
422 .IndirectStatePointer
=
423 device
->border_colors
.offset
+
424 pCreateInfo
->borderColor
* sizeof(float) * 4,
426 .LODClampMagnificationMode
= MIPNONE
,
427 .MaximumAnisotropy
= max_anisotropy
,
428 .RAddressMinFilterRoundingEnable
= 0,
429 .RAddressMagFilterRoundingEnable
= 0,
430 .VAddressMinFilterRoundingEnable
= 0,
431 .VAddressMagFilterRoundingEnable
= 0,
432 .UAddressMinFilterRoundingEnable
= 0,
433 .UAddressMagFilterRoundingEnable
= 0,
434 .TrilinearFilterQuality
= 0,
435 .NonnormalizedCoordinateEnable
= 0,
436 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressU
],
437 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressV
],
438 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressW
],
441 GEN8_SAMPLER_STATE_pack(NULL
, sampler
->state
, &sampler_state
);
443 *pSampler
= anv_sampler_to_handle(sampler
);
448 VkResult
gen8_CreateDynamicDepthStencilState(
450 const VkDynamicDepthStencilStateCreateInfo
* pCreateInfo
,
451 VkDynamicDepthStencilState
* pState
)
453 ANV_FROM_HANDLE(anv_device
, device
, _device
);
454 struct anv_dynamic_ds_state
*state
;
456 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO
);
458 state
= anv_device_alloc(device
, sizeof(*state
), 8,
459 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
461 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
463 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil
= {
464 GEN8_3DSTATE_WM_DEPTH_STENCIL_header
,
466 /* Is this what we need to do? */
467 .StencilBufferWriteEnable
= pCreateInfo
->stencilWriteMask
!= 0,
469 .StencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
470 .StencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
472 .BackfaceStencilTestMask
= pCreateInfo
->stencilReadMask
& 0xff,
473 .BackfaceStencilWriteMask
= pCreateInfo
->stencilWriteMask
& 0xff,
476 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL
, state
->gen8
.wm_depth_stencil
,
479 struct GEN8_COLOR_CALC_STATE color_calc_state
= {
480 .StencilReferenceValue
= pCreateInfo
->stencilFrontRef
,
481 .BackFaceStencilReferenceValue
= pCreateInfo
->stencilBackRef
484 GEN8_COLOR_CALC_STATE_pack(NULL
, state
->gen8
.color_calc_state
, &color_calc_state
);
486 *pState
= anv_dynamic_ds_state_to_handle(state
);