1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Raspberry Pi Ltd
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "v3dv_private.h"
25bf215546Sopenharmony_ci#include "broadcom/common/v3d_macros.h"
26bf215546Sopenharmony_ci#include "broadcom/cle/v3dx_pack.h"
27bf215546Sopenharmony_ci#include "broadcom/compiler/v3d_compiler.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/*
30bf215546Sopenharmony_ci * Packs and ensure bo for the shader state (the latter can be temporal).
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_cistatic void
33bf215546Sopenharmony_cipack_texture_shader_state_helper(struct v3dv_device *device,
34bf215546Sopenharmony_ci                                 struct v3dv_image_view *image_view,
35bf215546Sopenharmony_ci                                 bool for_cube_map_array_storage)
36bf215546Sopenharmony_ci{
37bf215546Sopenharmony_ci   assert(!for_cube_map_array_storage ||
38bf215546Sopenharmony_ci          image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY);
39bf215546Sopenharmony_ci   const uint32_t index = for_cube_map_array_storage ? 1 : 0;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   assert(image_view->vk.image);
42bf215546Sopenharmony_ci   const struct v3dv_image *image = (struct v3dv_image *) image_view->vk.image;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   assert(image->vk.samples == VK_SAMPLE_COUNT_1_BIT ||
45bf215546Sopenharmony_ci          image->vk.samples == VK_SAMPLE_COUNT_4_BIT);
46bf215546Sopenharmony_ci   const uint32_t msaa_scale = image->vk.samples == VK_SAMPLE_COUNT_1_BIT ? 1 : 2;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   v3dvx_pack(image_view->texture_shader_state[index], TEXTURE_SHADER_STATE, tex) {
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci      tex.level_0_is_strictly_uif =
51bf215546Sopenharmony_ci         (image->slices[0].tiling == V3D_TILING_UIF_XOR ||
52bf215546Sopenharmony_ci          image->slices[0].tiling == V3D_TILING_UIF_NO_XOR);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci      tex.level_0_xor_enable = (image->slices[0].tiling == V3D_TILING_UIF_XOR);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci      if (tex.level_0_is_strictly_uif)
57bf215546Sopenharmony_ci         tex.level_0_ub_pad = image->slices[0].ub_pad;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci      /* FIXME: v3d never sets uif_xor_disable, but uses it on the following
60bf215546Sopenharmony_ci       * check so let's set the default value
61bf215546Sopenharmony_ci       */
62bf215546Sopenharmony_ci      tex.uif_xor_disable = false;
63bf215546Sopenharmony_ci      if (tex.uif_xor_disable ||
64bf215546Sopenharmony_ci          tex.level_0_is_strictly_uif) {
65bf215546Sopenharmony_ci         tex.extended = true;
66bf215546Sopenharmony_ci      }
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci      tex.base_level = image_view->vk.base_mip_level;
69bf215546Sopenharmony_ci      tex.max_level = image_view->vk.base_mip_level +
70bf215546Sopenharmony_ci                      image_view->vk.level_count - 1;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci      tex.swizzle_r = v3d_translate_pipe_swizzle(image_view->swizzle[0]);
73bf215546Sopenharmony_ci      tex.swizzle_g = v3d_translate_pipe_swizzle(image_view->swizzle[1]);
74bf215546Sopenharmony_ci      tex.swizzle_b = v3d_translate_pipe_swizzle(image_view->swizzle[2]);
75bf215546Sopenharmony_ci      tex.swizzle_a = v3d_translate_pipe_swizzle(image_view->swizzle[3]);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci      tex.reverse_standard_border_color = image_view->channel_reverse;
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci      tex.texture_type = image_view->format->tex_type;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci      if (image->vk.image_type == VK_IMAGE_TYPE_3D) {
82bf215546Sopenharmony_ci         tex.image_depth = image->vk.extent.depth;
83bf215546Sopenharmony_ci      } else {
84bf215546Sopenharmony_ci         tex.image_depth = image_view->vk.layer_count;
85bf215546Sopenharmony_ci      }
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci      /* Empirical testing with CTS shows that when we are sampling from cube
88bf215546Sopenharmony_ci       * arrays we want to set image depth to layers / 6, but not when doing
89bf215546Sopenharmony_ci       * image load/store.
90bf215546Sopenharmony_ci       */
91bf215546Sopenharmony_ci      if (image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY &&
92bf215546Sopenharmony_ci          !for_cube_map_array_storage) {
93bf215546Sopenharmony_ci         assert(tex.image_depth % 6 == 0);
94bf215546Sopenharmony_ci         tex.image_depth /= 6;
95bf215546Sopenharmony_ci      }
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci      tex.image_height = image->vk.extent.height * msaa_scale;
98bf215546Sopenharmony_ci      tex.image_width = image->vk.extent.width * msaa_scale;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci      /* On 4.x, the height of a 1D texture is redefined to be the
101bf215546Sopenharmony_ci       * upper 14 bits of the width (which is only usable with txf).
102bf215546Sopenharmony_ci       */
103bf215546Sopenharmony_ci      if (image->vk.image_type == VK_IMAGE_TYPE_1D) {
104bf215546Sopenharmony_ci         tex.image_height = tex.image_width >> 14;
105bf215546Sopenharmony_ci      }
106bf215546Sopenharmony_ci      tex.image_width &= (1 << 14) - 1;
107bf215546Sopenharmony_ci      tex.image_height &= (1 << 14) - 1;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci      tex.array_stride_64_byte_aligned = image->cube_map_stride / 64;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci      tex.srgb = vk_format_is_srgb(image_view->vk.view_format);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci      /* At this point we don't have the job. That's the reason the first
114bf215546Sopenharmony_ci       * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
115bf215546Sopenharmony_ci       * add the bo to the job. This also means that we need to add manually
116bf215546Sopenharmony_ci       * the image bo to the job using the texture.
117bf215546Sopenharmony_ci       */
118bf215546Sopenharmony_ci      const uint32_t base_offset =
119bf215546Sopenharmony_ci         image->mem->bo->offset +
120bf215546Sopenharmony_ci         v3dv_layer_offset(image, 0, image_view->vk.base_array_layer);
121bf215546Sopenharmony_ci      tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
122bf215546Sopenharmony_ci   }
123bf215546Sopenharmony_ci}
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_civoid
126bf215546Sopenharmony_civ3dX(pack_texture_shader_state)(struct v3dv_device *device,
127bf215546Sopenharmony_ci                                struct v3dv_image_view *iview)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   pack_texture_shader_state_helper(device, iview, false);
130bf215546Sopenharmony_ci   if (iview->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
131bf215546Sopenharmony_ci      pack_texture_shader_state_helper(device, iview, true);
132bf215546Sopenharmony_ci}
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_civoid
135bf215546Sopenharmony_civ3dX(pack_texture_shader_state_from_buffer_view)(struct v3dv_device *device,
136bf215546Sopenharmony_ci                                                 struct v3dv_buffer_view *buffer_view)
137bf215546Sopenharmony_ci{
138bf215546Sopenharmony_ci   assert(buffer_view->buffer);
139bf215546Sopenharmony_ci   const struct v3dv_buffer *buffer = buffer_view->buffer;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   v3dvx_pack(buffer_view->texture_shader_state, TEXTURE_SHADER_STATE, tex) {
142bf215546Sopenharmony_ci      tex.swizzle_r = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_X);
143bf215546Sopenharmony_ci      tex.swizzle_g = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_Y);
144bf215546Sopenharmony_ci      tex.swizzle_b = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_Z);
145bf215546Sopenharmony_ci      tex.swizzle_a = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_W);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci      tex.image_depth = 1;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci      /* On 4.x, the height of a 1D texture is redefined to be the upper 14
150bf215546Sopenharmony_ci       * bits of the width (which is only usable with txf) (or in other words,
151bf215546Sopenharmony_ci       * we are providing a 28 bit field for size, but split on the usual
152bf215546Sopenharmony_ci       * 14bit height/width).
153bf215546Sopenharmony_ci       */
154bf215546Sopenharmony_ci      tex.image_width = buffer_view->num_elements;
155bf215546Sopenharmony_ci      tex.image_height = tex.image_width >> 14;
156bf215546Sopenharmony_ci      tex.image_width &= (1 << 14) - 1;
157bf215546Sopenharmony_ci      tex.image_height &= (1 << 14) - 1;
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci      tex.texture_type = buffer_view->format->tex_type;
160bf215546Sopenharmony_ci      tex.srgb = vk_format_is_srgb(buffer_view->vk_format);
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci      /* At this point we don't have the job. That's the reason the first
163bf215546Sopenharmony_ci       * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
164bf215546Sopenharmony_ci       * add the bo to the job. This also means that we need to add manually
165bf215546Sopenharmony_ci       * the image bo to the job using the texture.
166bf215546Sopenharmony_ci       */
167bf215546Sopenharmony_ci      const uint32_t base_offset =
168bf215546Sopenharmony_ci         buffer->mem->bo->offset +
169bf215546Sopenharmony_ci         buffer->mem_offset +
170bf215546Sopenharmony_ci         buffer_view->offset;
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci      tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
173bf215546Sopenharmony_ci   }
174bf215546Sopenharmony_ci}
175