blob: e4cf552f90d3bef36efa83b62d56fe8e16a8a26c [file] [log] [blame]
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -07001/*
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
Jason Ekstrandde54b4b2015-11-16 12:29:07 -080032#include "gen7_pack.h"
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080033#include "gen75_pack.h"
Jason Ekstrandde54b4b2015-11-16 12:29:07 -080034
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080035GENX_FUNC(GEN7, GEN75) void
36genX(fill_buffer_surface_state)(void *state, const struct anv_format *format,
37 uint32_t offset, uint32_t range,
38 uint32_t stride)
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070039{
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070040 uint32_t num_elements = range / stride;
41
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080042 struct GENX(RENDER_SURFACE_STATE) surface_state = {
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070043 .SurfaceType = SURFTYPE_BUFFER,
44 .SurfaceFormat = format->surface_format,
45 .SurfaceVerticalAlignment = VALIGN_4,
46 .SurfaceHorizontalAlignment = HALIGN_4,
47 .TiledSurface = false,
Kristian Høgsberg Kristensen647a6022015-08-25 15:58:21 -070048 .RenderCacheReadWriteMode = false,
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080049 .SurfaceObjectControlState = GENX(MOCS),
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070050 .Height = (num_elements >> 7) & 0x3fff,
51 .Width = num_elements & 0x7f,
52 .Depth = (num_elements >> 21) & 0x3f,
53 .SurfacePitch = stride - 1,
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080054# if (ANV_IS_HASWELL)
55 .ShaderChannelSelectR = SCS_RED,
56 .ShaderChannelSelectG = SCS_GREEN,
57 .ShaderChannelSelectB = SCS_BLUE,
58 .ShaderChannelSelectA = SCS_ALPHA,
59# endif
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070060 .SurfaceBaseAddress = { NULL, offset },
61 };
62
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080063 GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &surface_state);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070064}
65
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070066static const uint32_t vk_to_gen_tex_filter[] = {
Jason Ekstrande8f22942015-11-30 16:21:38 -080067 [VK_FILTER_NEAREST] = MAPFILTER_NEAREST,
68 [VK_FILTER_LINEAR] = MAPFILTER_LINEAR
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070069};
70
71static const uint32_t vk_to_gen_mipmap_mode[] = {
Jason Ekstrande8f22942015-11-30 16:21:38 -080072 [VK_SAMPLER_MIPMAP_MODE_BASE] = MIPFILTER_NONE,
73 [VK_SAMPLER_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
74 [VK_SAMPLER_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070075};
76
77static const uint32_t vk_to_gen_tex_address[] = {
Jason Ekstrande8f22942015-11-30 16:21:38 -080078 [VK_SAMPLER_ADDRESS_MODE_REPEAT] = TCM_WRAP,
79 [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = TCM_MIRROR,
80 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE] = TCM_CLAMP,
81 [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
82 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070083};
84
85static const uint32_t vk_to_gen_compare_op[] = {
86 [VK_COMPARE_OP_NEVER] = PREFILTEROPNEVER,
87 [VK_COMPARE_OP_LESS] = PREFILTEROPLESS,
88 [VK_COMPARE_OP_EQUAL] = PREFILTEROPEQUAL,
Jason Ekstrand73ef7d42015-11-30 13:49:28 -080089 [VK_COMPARE_OP_LESS_OR_EQUAL] = PREFILTEROPLEQUAL,
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070090 [VK_COMPARE_OP_GREATER] = PREFILTEROPGREATER,
91 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPNOTEQUAL,
Jason Ekstrand73ef7d42015-11-30 13:49:28 -080092 [VK_COMPARE_OP_GREATER_OR_EQUAL] = PREFILTEROPGEQUAL,
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -070093 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPALWAYS,
94};
95
Chad Versace24de3d42015-10-06 19:11:58 -070096static struct anv_state
Jason Ekstrandf0390bc2015-11-17 07:07:02 -080097alloc_surface_state(struct anv_device *device,
98 struct anv_cmd_buffer *cmd_buffer)
Chad Versace24de3d42015-10-06 19:11:58 -070099{
100 if (cmd_buffer) {
Jason Ekstrand6dc4cad2015-10-15 13:45:07 -0700101 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
Chad Versace24de3d42015-10-06 19:11:58 -0700102 } else {
103 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
104 }
105}
106
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800107VkResult genX(CreateSampler)(
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700108 VkDevice _device,
109 const VkSamplerCreateInfo* pCreateInfo,
110 VkSampler* pSampler)
111{
112 ANV_FROM_HANDLE(anv_device, device, _device);
113 struct anv_sampler *sampler;
114 uint32_t mag_filter, min_filter, max_anisotropy;
115
116 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
117
118 sampler = anv_device_alloc(device, sizeof(*sampler), 8,
119 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
120 if (!sampler)
121 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
122
123 if (pCreateInfo->maxAnisotropy > 1) {
124 mag_filter = MAPFILTER_ANISOTROPIC;
125 min_filter = MAPFILTER_ANISOTROPIC;
126 max_anisotropy = (pCreateInfo->maxAnisotropy - 2) / 2;
127 } else {
128 mag_filter = vk_to_gen_tex_filter[pCreateInfo->magFilter];
129 min_filter = vk_to_gen_tex_filter[pCreateInfo->minFilter];
130 max_anisotropy = RATIO21;
131 }
132
133 struct GEN7_SAMPLER_STATE sampler_state = {
134 .SamplerDisable = false,
135 .TextureBorderColorMode = DX10OGL,
136 .BaseMipLevel = 0.0,
Jason Ekstrande8f22942015-11-30 16:21:38 -0800137 .MipModeFilter = vk_to_gen_mipmap_mode[pCreateInfo->mipmapMode],
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700138 .MagModeFilter = mag_filter,
139 .MinModeFilter = min_filter,
140 .TextureLODBias = pCreateInfo->mipLodBias * 256,
141 .AnisotropicAlgorithm = EWAApproximation,
142 .MinLOD = pCreateInfo->minLod,
143 .MaxLOD = pCreateInfo->maxLod,
144 .ChromaKeyEnable = 0,
145 .ChromaKeyIndex = 0,
146 .ChromaKeyMode = 0,
147 .ShadowFunction = vk_to_gen_compare_op[pCreateInfo->compareOp],
148 .CubeSurfaceControlMode = 0,
149
150 .BorderColorPointer =
151 device->border_colors.offset +
152 pCreateInfo->borderColor * sizeof(float) * 4,
153
154 .MaximumAnisotropy = max_anisotropy,
155 .RAddressMinFilterRoundingEnable = 0,
156 .RAddressMagFilterRoundingEnable = 0,
157 .VAddressMinFilterRoundingEnable = 0,
158 .VAddressMagFilterRoundingEnable = 0,
159 .UAddressMinFilterRoundingEnable = 0,
160 .UAddressMagFilterRoundingEnable = 0,
161 .TrilinearFilterQuality = 0,
Jason Ekstrand57f50032015-10-05 20:17:24 -0700162 .NonnormalizedCoordinateEnable = pCreateInfo->unnormalizedCoordinates,
Jason Ekstrandf7c35192015-10-05 20:15:06 -0700163 .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeU],
164 .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeV],
165 .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeW],
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700166 };
167
168 GEN7_SAMPLER_STATE_pack(NULL, sampler->state, &sampler_state);
169
170 *pSampler = anv_sampler_to_handle(sampler);
171
172 return VK_SUCCESS;
173}
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700174
175static const uint8_t anv_halign[] = {
176 [4] = HALIGN_4,
177 [8] = HALIGN_8,
178};
179
180static const uint8_t anv_valign[] = {
181 [2] = VALIGN_2,
182 [4] = VALIGN_4,
183};
184
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800185static const uint32_t vk_to_gen_swizzle_map[] = {
186 [VK_COMPONENT_SWIZZLE_ZERO] = SCS_ZERO,
187 [VK_COMPONENT_SWIZZLE_ONE] = SCS_ONE,
188 [VK_COMPONENT_SWIZZLE_R] = SCS_RED,
189 [VK_COMPONENT_SWIZZLE_G] = SCS_GREEN,
190 [VK_COMPONENT_SWIZZLE_B] = SCS_BLUE,
191 [VK_COMPONENT_SWIZZLE_A] = SCS_ALPHA
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800192};
193
Jason Ekstrand2c77b0c2015-11-30 13:29:49 -0800194static inline uint32_t
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800195vk_to_gen_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component)
196{
197 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
198 return vk_to_gen_swizzle_map[component];
199 else
200 return vk_to_gen_swizzle_map[swizzle];
201}
202
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800203GENX_FUNC(GEN7, GEN75) void
204genX(image_view_init)(struct anv_image_view *iview,
205 struct anv_device *device,
206 const VkImageViewCreateInfo* pCreateInfo,
207 struct anv_cmd_buffer *cmd_buffer)
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700208{
209 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
210
211 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
Chad Versace24de3d42015-10-06 19:11:58 -0700212
Chad Versace941b48e2015-08-28 07:08:58 -0700213 struct anv_surface *surface =
Chad Versace7a089bd2015-10-05 06:48:14 -0700214 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700215
216 const struct anv_format *format =
217 anv_format_for_vk_format(pCreateInfo->format);
218
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700219 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
220 anv_finishme("non-2D image views");
221
Chad Versace24de3d42015-10-06 19:11:58 -0700222 iview->image = image;
Chad Versacef0f4dfa2015-10-05 16:24:53 -0700223 iview->bo = image->bo;
224 iview->offset = image->offset + surface->offset;
225 iview->format = anv_format_for_vk_format(pCreateInfo->format);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700226
227 iview->extent = (VkExtent3D) {
228 .width = anv_minify(image->extent.width, range->baseMipLevel),
229 .height = anv_minify(image->extent.height, range->baseMipLevel),
230 .depth = anv_minify(image->extent.depth, range->baseMipLevel),
231 };
232
233 uint32_t depth = 1;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800234 if (range->layerCount > 1) {
235 depth = range->layerCount;
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700236 } else if (image->extent.depth > 1) {
237 depth = image->extent.depth;
238 }
239
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800240 struct GENX(RENDER_SURFACE_STATE) surface_state = {
Chad Versace24de3d42015-10-06 19:11:58 -0700241 .SurfaceType = image->surface_type,
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700242 .SurfaceArray = image->array_size > 1,
243 .SurfaceFormat = format->surface_format,
244 .SurfaceVerticalAlignment = anv_valign[surface->v_align],
245 .SurfaceHorizontalAlignment = anv_halign[surface->h_align],
Kristian Høgsberg Kristensen6a1098b2015-08-25 10:48:43 -0700246
247 /* From bspec (DevSNB, DevIVB): "Set Tile Walk to TILEWALK_XMAJOR if
248 * Tiled Surface is False."
249 */
Chad Versaceba467462015-11-13 10:24:57 -0800250 .TiledSurface = surface->tiling != ISL_TILING_LINEAR,
251 .TileWalk = surface->tiling == ISL_TILING_Y ?
252 TILEWALK_YMAJOR : TILEWALK_XMAJOR,
Kristian Høgsberg Kristensen6a1098b2015-08-25 10:48:43 -0700253
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700254 .VerticalLineStride = 0,
255 .VerticalLineStrideOffset = 0,
Chad Versace24de3d42015-10-06 19:11:58 -0700256
257 .RenderCacheReadWriteMode = 0, /* TEMPLATE */
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700258
259 .Height = image->extent.height - 1,
260 .Width = image->extent.width - 1,
261 .Depth = depth - 1,
262 .SurfacePitch = surface->stride - 1,
Jason Ekstrand1e4263b2015-10-06 10:27:50 -0700263 .MinimumArrayElement = range->baseArrayLayer,
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700264 .NumberofMultisamples = MULTISAMPLECOUNT_1,
265 .XOffset = 0,
266 .YOffset = 0,
267
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800268 .SurfaceObjectControlState = GENX(MOCS),
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700269
Chad Versace24de3d42015-10-06 19:11:58 -0700270 .MIPCountLOD = 0, /* TEMPLATE */
271 .SurfaceMinLOD = 0, /* TEMPLATE */
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700272
273 .MCSEnable = false,
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800274# if (ANV_IS_HASWELL)
Jason Ekstranda53f23d2015-11-30 13:06:12 -0800275 .ShaderChannelSelectR = vk_to_gen_swizzle(pCreateInfo->components.r,
276 VK_COMPONENT_SWIZZLE_R),
277 .ShaderChannelSelectG = vk_to_gen_swizzle(pCreateInfo->components.g,
278 VK_COMPONENT_SWIZZLE_G),
279 .ShaderChannelSelectB = vk_to_gen_swizzle(pCreateInfo->components.b,
280 VK_COMPONENT_SWIZZLE_B),
281 .ShaderChannelSelectA = vk_to_gen_swizzle(pCreateInfo->components.a,
282 VK_COMPONENT_SWIZZLE_A),
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800283# else /* XXX: Seriously? */
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700284 .RedClearColor = 0,
285 .GreenClearColor = 0,
286 .BlueClearColor = 0,
287 .AlphaClearColor = 0,
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800288# endif
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700289 .ResourceMinLOD = 0.0,
Chad Versacef0f4dfa2015-10-05 16:24:53 -0700290 .SurfaceBaseAddress = { NULL, iview->offset },
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700291 };
292
Chad Versace24de3d42015-10-06 19:11:58 -0700293 if (image->needs_nonrt_surface_state) {
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800294 iview->nonrt_surface_state = alloc_surface_state(device, cmd_buffer);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700295
Chad Versace24de3d42015-10-06 19:11:58 -0700296 surface_state.RenderCacheReadWriteMode = false;
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700297
Chad Versace24de3d42015-10-06 19:11:58 -0700298 /* For non render target surfaces, the hardware interprets field
299 * MIPCount/LOD as MIPCount. The range of levels accessible by the
300 * sampler engine is [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
Kristian Høgsberg Kristensen6a1098b2015-08-25 10:48:43 -0700301 */
Chad Versace24de3d42015-10-06 19:11:58 -0700302 surface_state.SurfaceMinLOD = range->baseMipLevel;
Jason Ekstrand299f8f12015-12-01 12:52:56 -0800303 surface_state.MIPCountLOD = range->levelCount - 1;
Kristian Høgsberg Kristensen6a1098b2015-08-25 10:48:43 -0700304
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800305 GENX(RENDER_SURFACE_STATE_pack)(NULL, iview->nonrt_surface_state.map,
306 &surface_state);
Chad Versace24de3d42015-10-06 19:11:58 -0700307 }
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700308
Chad Versace24de3d42015-10-06 19:11:58 -0700309 if (image->needs_color_rt_surface_state) {
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800310 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700311
Jason Ekstrandde54b4b2015-11-16 12:29:07 -0800312 surface_state.RenderCacheReadWriteMode = 0; /* Write only */
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700313
314 /* For render target surfaces, the hardware interprets field MIPCount/LOD as
315 * LOD. The Broadwell PRM says:
316 *
317 * MIPCountLOD defines the LOD that will be rendered into.
318 * SurfaceMinLOD is ignored.
319 */
Chad Versace24de3d42015-10-06 19:11:58 -0700320 surface_state.MIPCountLOD = range->baseMipLevel;
321 surface_state.SurfaceMinLOD = 0;
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700322
Jason Ekstrandf0390bc2015-11-17 07:07:02 -0800323 GENX(RENDER_SURFACE_STATE_pack)(NULL, iview->color_rt_surface_state.map,
324 &surface_state);
Chad Versace24de3d42015-10-06 19:11:58 -0700325 }
Kristian Høgsberg Kristensenf1455ff2015-08-20 22:59:19 -0700326}