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 #include "gen8_pack.h"
33 #include "gen9_pack.h"
35 #include "genX_state_util.h"
38 genX(fill_buffer_surface_state
)(void *state
, enum isl_format format
,
39 uint32_t offset
, uint32_t range
, uint32_t stride
)
41 uint32_t num_elements
= range
/ stride
;
43 struct GENX(RENDER_SURFACE_STATE
) surface_state
= {
44 .SurfaceType
= SURFTYPE_BUFFER
,
45 .SurfaceArray
= false,
46 .SurfaceFormat
= format
,
47 .SurfaceVerticalAlignment
= VALIGN4
,
48 .SurfaceHorizontalAlignment
= HALIGN4
,
50 .SamplerL2BypassModeDisable
= true,
51 .RenderCacheReadWriteMode
= WriteOnlyCache
,
52 .MemoryObjectControlState
= GENX(MOCS
),
53 .Height
= ((num_elements
- 1) >> 7) & 0x3fff,
54 .Width
= (num_elements
- 1) & 0x7f,
55 .Depth
= ((num_elements
- 1) >> 21) & 0x3f,
56 .SurfacePitch
= stride
- 1,
57 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
58 .ShaderChannelSelectRed
= SCS_RED
,
59 .ShaderChannelSelectGreen
= SCS_GREEN
,
60 .ShaderChannelSelectBlue
= SCS_BLUE
,
61 .ShaderChannelSelectAlpha
= SCS_ALPHA
,
62 /* FIXME: We assume that the image must be bound at this time. */
63 .SurfaceBaseAddress
= { NULL
, offset
},
66 GENX(RENDER_SURFACE_STATE_pack
)(NULL
, state
, &surface_state
);
69 static const uint8_t anv_halign
[] = {
75 static const uint8_t anv_valign
[] = {
81 static struct anv_state
82 alloc_surface_state(struct anv_device
*device
,
83 struct anv_cmd_buffer
*cmd_buffer
)
86 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
88 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
93 * Get the values to pack into RENDER_SUFFACE_STATE.SurfaceHorizontalAlignment
94 * and SurfaceVerticalAlignment.
97 get_halign_valign(const struct isl_surf
*surf
, uint32_t *halign
, uint32_t *valign
)
100 if (isl_tiling_is_std_y(surf
->tiling
) ||
101 surf
->dim_layout
== ISL_DIM_LAYOUT_GEN9_1D
) {
102 /* The hardware ignores the alignment values. Anyway, the surface's
103 * true alignment is likely outside the enum range of HALIGN* and
109 /* In Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in units
110 * of surface elements (not pixels nor samples). For compressed formats,
111 * a "surface element" is defined as a compression block. For example,
112 * if SurfaceVerticalAlignment is VALIGN_4 and SurfaceFormat is an ETC2
113 * format (ETC2 has a block height of 4), then the vertical alignment is
114 * 4 compression blocks or, equivalently, 16 pixels.
116 struct isl_extent3d image_align_el
117 = isl_surf_get_image_alignment_el(surf
);
119 *halign
= anv_halign
[image_align_el
.width
];
120 *valign
= anv_valign
[image_align_el
.height
];
123 /* Pre-Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in
124 * units of surface samples. For example, if SurfaceVerticalAlignment
125 * is VALIGN_4 and the surface is singlesampled, then for any surface
126 * format (compressed or not) the vertical alignment is
129 struct isl_extent3d image_align_sa
130 = isl_surf_get_image_alignment_sa(surf
);
132 *halign
= anv_halign
[image_align_sa
.width
];
133 *valign
= anv_valign
[image_align_sa
.height
];
138 get_qpitch(const struct isl_surf
*surf
)
142 unreachable(!"bad isl_surf_dim");
143 case ISL_SURF_DIM_1D
:
145 /* QPitch is usually expressed as rows of surface elements (where
146 * a surface element is an compression block or a single surface
147 * sample). Skylake 1D is an outlier.
149 * From the Skylake BSpec >> Memory Views >> Common Surface
150 * Formats >> Surface Layout and Tiling >> 1D Surfaces:
152 * Surface QPitch specifies the distance in pixels between array
155 return isl_surf_get_array_pitch_el(surf
);
157 return isl_surf_get_array_pitch_el_rows(surf
);
159 case ISL_SURF_DIM_2D
:
160 case ISL_SURF_DIM_3D
:
161 return isl_surf_get_array_pitch_el_rows(surf
);
166 genX(image_view_init
)(struct anv_image_view
*iview
,
167 struct anv_device
*device
,
168 const VkImageViewCreateInfo
* pCreateInfo
,
169 struct anv_cmd_buffer
*cmd_buffer
)
171 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
173 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
175 struct anv_surface
*surface
=
176 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
178 uint32_t depth
= 1; /* RENDER_SURFACE_STATE::Depth */
179 uint32_t rt_view_extent
= 1; /* RENDER_SURFACE_STATE::RenderTargetViewExtent */
181 switch (image
->type
) {
182 case VK_IMAGE_TYPE_1D
:
183 case VK_IMAGE_TYPE_2D
:
184 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:
186 * For SURFTYPE_1D, 2D, and CUBE: The range of this field is reduced
187 * by one for each increase from zero of Minimum Array Element. For
188 * example, if Minimum Array Element is set to 1024 on a 2D surface,
189 * the range of this field is reduced to [0,1023].
191 depth
= range
->layerCount
;
193 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:
195 * For Render Target and Typed Dataport 1D and 2D Surfaces:
196 * This field must be set to the same value as the Depth field.
198 rt_view_extent
= depth
;
200 case VK_IMAGE_TYPE_3D
:
201 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:
203 * If the volume texture is MIP-mapped, this field specifies the
204 * depth of the base MIP level.
206 depth
= image
->extent
.depth
;
208 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:
210 * For Render Target and Typed Dataport 3D Surfaces: This field
211 * indicates the extent of the accessible 'R' coordinates minus 1 on
212 * the LOD currently being rendered to.
214 rt_view_extent
= iview
->extent
.depth
;
217 unreachable(!"bad VkImageType");
220 static const uint8_t isl_to_gen_tiling
[] = {
221 [ISL_TILING_LINEAR
] = LINEAR
,
222 [ISL_TILING_X
] = XMAJOR
,
223 [ISL_TILING_Y0
] = YMAJOR
,
224 [ISL_TILING_Yf
] = YMAJOR
,
225 [ISL_TILING_Ys
] = YMAJOR
,
226 [ISL_TILING_W
] = WMAJOR
,
229 uint32_t halign
, valign
;
230 get_halign_valign(&surface
->isl
, &halign
, &valign
);
232 struct GENX(RENDER_SURFACE_STATE
) surface_state
= {
233 .SurfaceType
= anv_surftype(image
, pCreateInfo
->viewType
, false),
234 .SurfaceArray
= image
->array_size
> 1,
235 .SurfaceFormat
= iview
->format
,
236 .SurfaceVerticalAlignment
= valign
,
237 .SurfaceHorizontalAlignment
= halign
,
238 .TileMode
= isl_to_gen_tiling
[surface
->isl
.tiling
],
239 .VerticalLineStride
= 0,
240 .VerticalLineStrideOffset
= 0,
241 .SamplerL2BypassModeDisable
= true,
242 .RenderCacheReadWriteMode
= WriteOnlyCache
,
243 .MemoryObjectControlState
= GENX(MOCS
),
245 /* The driver sets BaseMipLevel in SAMPLER_STATE, not here in
246 * RENDER_SURFACE_STATE. The Broadwell PRM says "it is illegal to have
247 * both Base Mip Level fields nonzero".
251 .SurfaceQPitch
= get_qpitch(&surface
->isl
) >> 2,
252 .Height
= image
->extent
.height
- 1,
253 .Width
= image
->extent
.width
- 1,
255 .SurfacePitch
= surface
->isl
.row_pitch
- 1,
256 .RenderTargetViewExtent
= rt_view_extent
- 1,
257 .MinimumArrayElement
= range
->baseArrayLayer
,
258 .NumberofMultisamples
= MULTISAMPLECOUNT_1
,
262 .MIPCountLOD
= 0, /* TEMPLATE */
263 .SurfaceMinLOD
= 0, /* TEMPLATE */
265 .AuxiliarySurfaceMode
= AUX_NONE
,
267 .GreenClearColor
= 0,
269 .AlphaClearColor
= 0,
270 .ShaderChannelSelectRed
= vk_to_gen_swizzle(pCreateInfo
->components
.r
,
271 VK_COMPONENT_SWIZZLE_R
),
272 .ShaderChannelSelectGreen
= vk_to_gen_swizzle(pCreateInfo
->components
.g
,
273 VK_COMPONENT_SWIZZLE_G
),
274 .ShaderChannelSelectBlue
= vk_to_gen_swizzle(pCreateInfo
->components
.b
,
275 VK_COMPONENT_SWIZZLE_B
),
276 .ShaderChannelSelectAlpha
= vk_to_gen_swizzle(pCreateInfo
->components
.a
,
277 VK_COMPONENT_SWIZZLE_A
),
278 .ResourceMinLOD
= 0.0,
279 .SurfaceBaseAddress
= { NULL
, iview
->offset
},
282 if (image
->needs_nonrt_surface_state
) {
283 iview
->nonrt_surface_state
=
284 alloc_surface_state(device
, cmd_buffer
);
286 /* For non render target surfaces, the hardware interprets field
287 * MIPCount/LOD as MIPCount. The range of levels accessible by the
288 * sampler engine is [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
290 surface_state
.SurfaceMinLOD
= range
->baseMipLevel
;
291 surface_state
.MIPCountLOD
= MAX2(range
->levelCount
, 1) - 1;
293 GENX(RENDER_SURFACE_STATE_pack
)(NULL
, iview
->nonrt_surface_state
.map
,
295 if (!device
->info
.has_llc
)
296 anv_state_clflush(iview
->nonrt_surface_state
);
299 if (image
->needs_color_rt_surface_state
) {
300 iview
->color_rt_surface_state
=
301 alloc_surface_state(device
, cmd_buffer
);
303 /* For render target surfaces, the hardware interprets field
304 * MIPCount/LOD as LOD. The Broadwell PRM says:
306 * MIPCountLOD defines the LOD that will be rendered into.
307 * SurfaceMinLOD is ignored.
309 surface_state
.MIPCountLOD
= range
->baseMipLevel
;
310 surface_state
.SurfaceMinLOD
= 0;
312 GENX(RENDER_SURFACE_STATE_pack
)(NULL
, iview
->color_rt_surface_state
.map
,
314 if (!device
->info
.has_llc
)
315 anv_state_clflush(iview
->color_rt_surface_state
);
318 if (image
->needs_storage_surface_state
) {
319 iview
->storage_surface_state
=
320 alloc_surface_state(device
, cmd_buffer
);
322 surface_state
.SurfaceType
=
323 anv_surftype(image
, pCreateInfo
->viewType
, true),
325 surface_state
.SurfaceFormat
=
326 isl_lower_storage_image_format(&device
->isl_dev
, iview
->format
);
328 surface_state
.SurfaceMinLOD
= range
->baseMipLevel
;
329 surface_state
.MIPCountLOD
= MAX2(range
->levelCount
, 1) - 1;
331 GENX(RENDER_SURFACE_STATE_pack
)(NULL
, iview
->storage_surface_state
.map
,
336 VkResult
genX(CreateSampler
)(
338 const VkSamplerCreateInfo
* pCreateInfo
,
339 const VkAllocationCallbacks
* pAllocator
,
342 ANV_FROM_HANDLE(anv_device
, device
, _device
);
343 struct anv_sampler
*sampler
;
344 uint32_t mag_filter
, min_filter
, max_anisotropy
;
346 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
348 sampler
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*sampler
), 8,
349 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
351 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
353 if (pCreateInfo
->maxAnisotropy
> 1) {
354 mag_filter
= MAPFILTER_ANISOTROPIC
;
355 min_filter
= MAPFILTER_ANISOTROPIC
;
356 max_anisotropy
= (pCreateInfo
->maxAnisotropy
- 2) / 2;
358 mag_filter
= vk_to_gen_tex_filter
[pCreateInfo
->magFilter
];
359 min_filter
= vk_to_gen_tex_filter
[pCreateInfo
->minFilter
];
360 max_anisotropy
= RATIO21
;
363 struct GENX(SAMPLER_STATE
) sampler_state
= {
364 .SamplerDisable
= false,
365 .TextureBorderColorMode
= DX10OGL
,
366 .LODPreClampMode
= 0,
370 .MipModeFilter
= vk_to_gen_mipmap_mode
[pCreateInfo
->mipmapMode
],
371 .MagModeFilter
= mag_filter
,
372 .MinModeFilter
= min_filter
,
373 .TextureLODBias
= anv_clamp_f(pCreateInfo
->mipLodBias
, -16, 15.996),
374 .AnisotropicAlgorithm
= EWAApproximation
,
375 .MinLOD
= anv_clamp_f(pCreateInfo
->minLod
, 0, 14),
376 .MaxLOD
= anv_clamp_f(pCreateInfo
->maxLod
, 0, 14),
377 .ChromaKeyEnable
= 0,
380 .ShadowFunction
= vk_to_gen_compare_op
[pCreateInfo
->compareOp
],
381 .CubeSurfaceControlMode
= 0,
383 .IndirectStatePointer
=
384 device
->border_colors
.offset
+
385 pCreateInfo
->borderColor
* sizeof(float) * 4,
387 .LODClampMagnificationMode
= MIPNONE
,
388 .MaximumAnisotropy
= max_anisotropy
,
389 .RAddressMinFilterRoundingEnable
= 0,
390 .RAddressMagFilterRoundingEnable
= 0,
391 .VAddressMinFilterRoundingEnable
= 0,
392 .VAddressMagFilterRoundingEnable
= 0,
393 .UAddressMinFilterRoundingEnable
= 0,
394 .UAddressMagFilterRoundingEnable
= 0,
395 .TrilinearFilterQuality
= 0,
396 .NonnormalizedCoordinateEnable
= pCreateInfo
->unnormalizedCoordinates
,
397 .TCXAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressModeU
],
398 .TCYAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressModeV
],
399 .TCZAddressControlMode
= vk_to_gen_tex_address
[pCreateInfo
->addressModeW
],
402 GENX(SAMPLER_STATE_pack
)(NULL
, sampler
->state
, &sampler_state
);
404 *pSampler
= anv_sampler_to_handle(sampler
);