Merge branch 'nir-spirv' into vulkan
[mesa.git] / src / vulkan / gen8_state.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 "anv_private.h"
31
32 VkResult gen8_CreateDynamicRasterState(
33 VkDevice _device,
34 const VkDynamicRasterStateCreateInfo* pCreateInfo,
35 VkDynamicRasterState* pState)
36 {
37 ANV_FROM_HANDLE(anv_device, device, _device);
38 struct anv_dynamic_rs_state *state;
39
40 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO);
41
42 state = anv_device_alloc(device, sizeof(*state), 8,
43 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
44 if (state == NULL)
45 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
46
47 struct GEN8_3DSTATE_SF sf = {
48 GEN8_3DSTATE_SF_header,
49 .LineWidth = pCreateInfo->lineWidth,
50 };
51
52 GEN8_3DSTATE_SF_pack(NULL, state->gen8.sf, &sf);
53
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
63 };
64
65 GEN8_3DSTATE_RASTER_pack(NULL, state->gen8.raster, &raster);
66
67 *pState = anv_dynamic_rs_state_to_handle(state);
68
69 return VK_SUCCESS;
70 }
71
72 void
73 gen8_fill_buffer_surface_state(void *state, const struct anv_format *format,
74 uint32_t offset, uint32_t range)
75 {
76 /* This assumes RGBA float format. */
77 uint32_t stride = 4;
78 uint32_t num_elements = range / stride;
79
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,
86 .TileMode = LINEAR,
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 },
101 };
102
103 GEN8_RENDER_SURFACE_STATE_pack(NULL, state, &surface_state);
104 }
105
106 VkResult gen8_CreateBufferView(
107 VkDevice _device,
108 const VkBufferViewCreateInfo* pCreateInfo,
109 VkBufferView* pView)
110 {
111 ANV_FROM_HANDLE(anv_device, device, _device);
112 struct anv_buffer_view *view;
113 VkResult result;
114
115 result = anv_buffer_view_create(device, pCreateInfo, &view);
116 if (result != VK_SUCCESS)
117 return result;
118
119 const struct anv_format *format =
120 anv_format_for_vk_format(pCreateInfo->format);
121
122 gen8_fill_buffer_surface_state(view->view.surface_state.map, format,
123 view->view.offset, pCreateInfo->range);
124
125 *pView = anv_buffer_view_to_handle(view);
126
127 return VK_SUCCESS;
128 }
129
130 static const uint8_t anv_halign[] = {
131 [4] = HALIGN4,
132 [8] = HALIGN8,
133 [16] = HALIGN16,
134 };
135
136 static const uint8_t anv_valign[] = {
137 [4] = VALIGN4,
138 [8] = VALIGN8,
139 [16] = VALIGN16,
140 };
141
142 void
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)
147 {
148 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
149
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);
154
155 const struct anv_format *format_info =
156 anv_format_for_vk_format(pCreateInfo->format);
157
158 const struct anv_image_view_info *view_type_info =
159 anv_image_view_info_for_vk_image_view_type(pCreateInfo->viewType);
160
161 if (pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D)
162 anv_finishme("non-2D image views");
163
164 view->bo = image->bo;
165 view->offset = image->offset + surface->offset;
166 view->format = format_info;
167
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),
172 };
173
174 uint32_t depth = 1;
175 if (range->arraySize > 1) {
176 depth = range->arraySize;
177 } else if (image->extent.depth > 1) {
178 depth = image->extent.depth;
179 }
180
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
188 };
189
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,
202
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".
206 */
207 .BaseMipLevel = 0.0,
208
209 .SurfaceQPitch = surface->qpitch >> 2,
210 .Height = image->extent.height - 1,
211 .Width = image->extent.width - 1,
212 .Depth = depth - 1,
213 .SurfacePitch = surface->stride - 1,
214 .MinimumArrayElement = range->baseArraySlice,
215 .NumberofMultisamples = MULTISAMPLECOUNT_1,
216 .XOffset = 0,
217 .YOffset = 0,
218
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].
222 */
223 .MIPCountLOD = range->mipLevels - 1,
224 .SurfaceMinLOD = range->baseMipLevel,
225
226 .AuxiliarySurfaceMode = AUX_NONE,
227 .RedClearColor = 0,
228 .GreenClearColor = 0,
229 .BlueClearColor = 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 },
237 };
238
239 if (cmd_buffer) {
240 view->surface_state =
241 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
242 } else {
243 view->surface_state =
244 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
245 }
246
247 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
248 }
249
250 void
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)
255 {
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);
262
263 aview->base.attachment_type = ANV_ATTACHMENT_VIEW_TYPE_COLOR;
264
265 anv_assert(pCreateInfo->arraySize > 0);
266 anv_assert(pCreateInfo->mipLevel < image->levels);
267 anv_assert(pCreateInfo->baseArraySlice + pCreateInfo->arraySize <= image->array_size);
268
269 view->bo = image->bo;
270 view->offset = image->offset + surface->offset;
271 view->format = anv_format_for_vk_format(pCreateInfo->format);
272
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),
277 };
278
279 uint32_t depth = 1;
280 if (pCreateInfo->arraySize > 1) {
281 depth = pCreateInfo->arraySize;
282 } else if (image->extent.depth > 1) {
283 depth = image->extent.depth;
284 }
285
286 if (cmd_buffer) {
287 view->surface_state =
288 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
289 } else {
290 view->surface_state =
291 anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
292 }
293
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,
306
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".
310 */
311 .BaseMipLevel = 0.0,
312
313 .SurfaceQPitch = surface->qpitch >> 2,
314 .Height = image->extent.height - 1,
315 .Width = image->extent.width - 1,
316 .Depth = depth - 1,
317 .SurfacePitch = surface->stride - 1,
318 .MinimumArrayElement = pCreateInfo->baseArraySlice,
319 .NumberofMultisamples = MULTISAMPLECOUNT_1,
320 .XOffset = 0,
321 .YOffset = 0,
322
323 /* For render target surfaces, the hardware interprets field MIPCount/LOD as
324 * LOD. The Broadwell PRM says:
325 *
326 * MIPCountLOD defines the LOD that will be rendered into.
327 * SurfaceMinLOD is ignored.
328 */
329 .SurfaceMinLOD = 0,
330 .MIPCountLOD = pCreateInfo->mipLevel,
331
332 .AuxiliarySurfaceMode = AUX_NONE,
333 .RedClearColor = 0,
334 .GreenClearColor = 0,
335 .BlueClearColor = 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 },
343 };
344
345 GEN8_RENDER_SURFACE_STATE_pack(NULL, view->surface_state.map, &surface_state);
346 }
347
348 VkResult gen8_CreateSampler(
349 VkDevice _device,
350 const VkSamplerCreateInfo* pCreateInfo,
351 VkSampler* pSampler)
352 {
353 ANV_FROM_HANDLE(anv_device, device, _device);
354 struct anv_sampler *sampler;
355 uint32_t mag_filter, min_filter, max_anisotropy;
356
357 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
358
359 sampler = anv_device_alloc(device, sizeof(*sampler), 8,
360 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
361 if (!sampler)
362 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
363
364 static const uint32_t vk_to_gen_tex_filter[] = {
365 [VK_TEX_FILTER_NEAREST] = MAPFILTER_NEAREST,
366 [VK_TEX_FILTER_LINEAR] = MAPFILTER_LINEAR
367 };
368
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
373 };
374
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,
381 };
382
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,
392 };
393
394 if (pCreateInfo->maxAnisotropy > 1) {
395 mag_filter = MAPFILTER_ANISOTROPIC;
396 min_filter = MAPFILTER_ANISOTROPIC;
397 max_anisotropy = (pCreateInfo->maxAnisotropy - 2) / 2;
398 } else {
399 mag_filter = vk_to_gen_tex_filter[pCreateInfo->magFilter];
400 min_filter = vk_to_gen_tex_filter[pCreateInfo->minFilter];
401 max_anisotropy = RATIO21;
402 }
403
404 struct GEN8_SAMPLER_STATE sampler_state = {
405 .SamplerDisable = false,
406 .TextureBorderColorMode = DX10OGL,
407 .LODPreClampMode = 0,
408 .BaseMipLevel = 0.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,
417 .ChromaKeyIndex = 0,
418 .ChromaKeyMode = 0,
419 .ShadowFunction = vk_to_gen_compare_op[pCreateInfo->compareOp],
420 .CubeSurfaceControlMode = 0,
421
422 .IndirectStatePointer =
423 device->border_colors.offset +
424 pCreateInfo->borderColor * sizeof(float) * 4,
425
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],
439 };
440
441 GEN8_SAMPLER_STATE_pack(NULL, sampler->state, &sampler_state);
442
443 *pSampler = anv_sampler_to_handle(sampler);
444
445 return VK_SUCCESS;
446 }
447
448 VkResult gen8_CreateDynamicDepthStencilState(
449 VkDevice _device,
450 const VkDynamicDepthStencilStateCreateInfo* pCreateInfo,
451 VkDynamicDepthStencilState* pState)
452 {
453 ANV_FROM_HANDLE(anv_device, device, _device);
454 struct anv_dynamic_ds_state *state;
455
456 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO);
457
458 state = anv_device_alloc(device, sizeof(*state), 8,
459 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
460 if (state == NULL)
461 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
462
463 struct GEN8_3DSTATE_WM_DEPTH_STENCIL wm_depth_stencil = {
464 GEN8_3DSTATE_WM_DEPTH_STENCIL_header,
465
466 /* Is this what we need to do? */
467 .StencilBufferWriteEnable = pCreateInfo->stencilWriteMask != 0,
468
469 .StencilTestMask = pCreateInfo->stencilReadMask & 0xff,
470 .StencilWriteMask = pCreateInfo->stencilWriteMask & 0xff,
471
472 .BackfaceStencilTestMask = pCreateInfo->stencilReadMask & 0xff,
473 .BackfaceStencilWriteMask = pCreateInfo->stencilWriteMask & 0xff,
474 };
475
476 GEN8_3DSTATE_WM_DEPTH_STENCIL_pack(NULL, state->gen8.wm_depth_stencil,
477 &wm_depth_stencil);
478
479 struct GEN8_COLOR_CALC_STATE color_calc_state = {
480 .StencilReferenceValue = pCreateInfo->stencilFrontRef,
481 .BackFaceStencilReferenceValue = pCreateInfo->stencilBackRef
482 };
483
484 GEN8_COLOR_CALC_STATE_pack(NULL, state->gen8.color_calc_state, &color_calc_state);
485
486 *pState = anv_dynamic_ds_state_to_handle(state);
487
488 return VK_SUCCESS;
489 }