xref: /aosp_15_r20/external/mesa3d/src/broadcom/vulkan/v3dvx_image.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2021 Raspberry Pi Ltd
3*61046927SAndroid Build Coastguard Worker  *
4*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker  *
11*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*61046927SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*61046927SAndroid Build Coastguard Worker  * Software.
14*61046927SAndroid Build Coastguard Worker  *
15*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*61046927SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*61046927SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*61046927SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*61046927SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*61046927SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*61046927SAndroid Build Coastguard Worker  * IN THE SOFTWARE.
22*61046927SAndroid Build Coastguard Worker  */
23*61046927SAndroid Build Coastguard Worker 
24*61046927SAndroid Build Coastguard Worker #include "v3dv_private.h"
25*61046927SAndroid Build Coastguard Worker #include "broadcom/common/v3d_macros.h"
26*61046927SAndroid Build Coastguard Worker #include "broadcom/cle/v3dx_pack.h"
27*61046927SAndroid Build Coastguard Worker #include "broadcom/compiler/v3d_compiler.h"
28*61046927SAndroid Build Coastguard Worker 
29*61046927SAndroid Build Coastguard Worker /*
30*61046927SAndroid Build Coastguard Worker  * Packs and ensure bo for the shader state (the latter can be temporal).
31*61046927SAndroid Build Coastguard Worker  */
32*61046927SAndroid Build Coastguard Worker static void
pack_texture_shader_state_helper(struct v3dv_device * device,struct v3dv_image_view * image_view,bool for_cube_map_array_storage)33*61046927SAndroid Build Coastguard Worker pack_texture_shader_state_helper(struct v3dv_device *device,
34*61046927SAndroid Build Coastguard Worker                                  struct v3dv_image_view *image_view,
35*61046927SAndroid Build Coastguard Worker                                  bool for_cube_map_array_storage)
36*61046927SAndroid Build Coastguard Worker {
37*61046927SAndroid Build Coastguard Worker    assert(!for_cube_map_array_storage ||
38*61046927SAndroid Build Coastguard Worker           image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY);
39*61046927SAndroid Build Coastguard Worker    const uint32_t index = for_cube_map_array_storage ? 1 : 0;
40*61046927SAndroid Build Coastguard Worker 
41*61046927SAndroid Build Coastguard Worker    assert(image_view->vk.image);
42*61046927SAndroid Build Coastguard Worker    const struct v3dv_image *image = (struct v3dv_image *) image_view->vk.image;
43*61046927SAndroid Build Coastguard Worker 
44*61046927SAndroid Build Coastguard Worker    assert(image->vk.samples == VK_SAMPLE_COUNT_1_BIT ||
45*61046927SAndroid Build Coastguard Worker           image->vk.samples == VK_SAMPLE_COUNT_4_BIT);
46*61046927SAndroid Build Coastguard Worker    const uint32_t msaa_scale = image->vk.samples == VK_SAMPLE_COUNT_1_BIT ? 1 : 2;
47*61046927SAndroid Build Coastguard Worker 
48*61046927SAndroid Build Coastguard Worker    for (uint8_t plane = 0; plane < image_view->plane_count; plane++) {
49*61046927SAndroid Build Coastguard Worker       uint8_t iplane = image_view->planes[plane].image_plane;
50*61046927SAndroid Build Coastguard Worker       v3dvx_pack(image_view->planes[plane].texture_shader_state[index], TEXTURE_SHADER_STATE, tex) {
51*61046927SAndroid Build Coastguard Worker 
52*61046927SAndroid Build Coastguard Worker          tex.level_0_is_strictly_uif =
53*61046927SAndroid Build Coastguard Worker             (image->planes[iplane].slices[0].tiling == V3D_TILING_UIF_XOR ||
54*61046927SAndroid Build Coastguard Worker              image->planes[iplane].slices[0].tiling == V3D_TILING_UIF_NO_XOR);
55*61046927SAndroid Build Coastguard Worker 
56*61046927SAndroid Build Coastguard Worker          tex.level_0_xor_enable = (image->planes[iplane].slices[0].tiling == V3D_TILING_UIF_XOR);
57*61046927SAndroid Build Coastguard Worker 
58*61046927SAndroid Build Coastguard Worker          if (tex.level_0_is_strictly_uif)
59*61046927SAndroid Build Coastguard Worker             tex.level_0_ub_pad = image->planes[iplane].slices[0].ub_pad;
60*61046927SAndroid Build Coastguard Worker 
61*61046927SAndroid Build Coastguard Worker          /* FIXME: v3d never sets uif_xor_disable, but uses it on the following
62*61046927SAndroid Build Coastguard Worker           * check so let's set the default value
63*61046927SAndroid Build Coastguard Worker           */
64*61046927SAndroid Build Coastguard Worker          tex.uif_xor_disable = false;
65*61046927SAndroid Build Coastguard Worker          if (tex.uif_xor_disable ||
66*61046927SAndroid Build Coastguard Worker              tex.level_0_is_strictly_uif) {
67*61046927SAndroid Build Coastguard Worker             tex.extended = true;
68*61046927SAndroid Build Coastguard Worker          }
69*61046927SAndroid Build Coastguard Worker 
70*61046927SAndroid Build Coastguard Worker          tex.base_level = image_view->vk.base_mip_level;
71*61046927SAndroid Build Coastguard Worker          tex.max_level = image_view->vk.base_mip_level +
72*61046927SAndroid Build Coastguard Worker                          image_view->vk.level_count - 1;
73*61046927SAndroid Build Coastguard Worker 
74*61046927SAndroid Build Coastguard Worker          tex.swizzle_r = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[0]);
75*61046927SAndroid Build Coastguard Worker          tex.swizzle_g = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[1]);
76*61046927SAndroid Build Coastguard Worker          tex.swizzle_b = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[2]);
77*61046927SAndroid Build Coastguard Worker          tex.swizzle_a = v3d_translate_pipe_swizzle(image_view->planes[plane].swizzle[3]);
78*61046927SAndroid Build Coastguard Worker 
79*61046927SAndroid Build Coastguard Worker          tex.texture_type = image_view->format->planes[plane].tex_type;
80*61046927SAndroid Build Coastguard Worker 
81*61046927SAndroid Build Coastguard Worker          if (image->vk.image_type == VK_IMAGE_TYPE_3D) {
82*61046927SAndroid Build Coastguard Worker             tex.image_depth = image->vk.extent.depth;
83*61046927SAndroid Build Coastguard Worker          } else {
84*61046927SAndroid Build Coastguard Worker             tex.image_depth = image_view->vk.layer_count;
85*61046927SAndroid Build Coastguard Worker          }
86*61046927SAndroid Build Coastguard Worker 
87*61046927SAndroid Build Coastguard Worker          /* Empirical testing with CTS shows that when we are sampling from cube
88*61046927SAndroid Build Coastguard Worker           * arrays we want to set image depth to layers / 6, but not when doing
89*61046927SAndroid Build Coastguard Worker           * image load/store.
90*61046927SAndroid Build Coastguard Worker           */
91*61046927SAndroid Build Coastguard Worker          if (image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY &&
92*61046927SAndroid Build Coastguard Worker              !for_cube_map_array_storage) {
93*61046927SAndroid Build Coastguard Worker             assert(tex.image_depth % 6 == 0);
94*61046927SAndroid Build Coastguard Worker             tex.image_depth /= 6;
95*61046927SAndroid Build Coastguard Worker          }
96*61046927SAndroid Build Coastguard Worker 
97*61046927SAndroid Build Coastguard Worker          tex.image_height = image->planes[iplane].height * msaa_scale;
98*61046927SAndroid Build Coastguard Worker          tex.image_width = image->planes[iplane].width * msaa_scale;
99*61046927SAndroid Build Coastguard Worker 
100*61046927SAndroid Build Coastguard Worker          /* On 4.x, the height of a 1D texture is redefined to be the
101*61046927SAndroid Build Coastguard Worker           * upper 14 bits of the width (which is only usable with txf).
102*61046927SAndroid Build Coastguard Worker           */
103*61046927SAndroid Build Coastguard Worker          if (image->vk.image_type == VK_IMAGE_TYPE_1D)
104*61046927SAndroid Build Coastguard Worker             tex.image_height = tex.image_width >> 14;
105*61046927SAndroid Build Coastguard Worker 
106*61046927SAndroid Build Coastguard Worker          tex.image_width &= (1 << 14) - 1;
107*61046927SAndroid Build Coastguard Worker          tex.image_height &= (1 << 14) - 1;
108*61046927SAndroid Build Coastguard Worker 
109*61046927SAndroid Build Coastguard Worker          tex.array_stride_64_byte_aligned = image->planes[iplane].cube_map_stride / 64;
110*61046927SAndroid Build Coastguard Worker 
111*61046927SAndroid Build Coastguard Worker          /* At this point we don't have the job. That's the reason the first
112*61046927SAndroid Build Coastguard Worker           * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
113*61046927SAndroid Build Coastguard Worker           * add the bo to the job. This also means that we need to add manually
114*61046927SAndroid Build Coastguard Worker           * the image bo to the job using the texture.
115*61046927SAndroid Build Coastguard Worker           */
116*61046927SAndroid Build Coastguard Worker          const uint32_t base_offset =
117*61046927SAndroid Build Coastguard Worker             image->planes[iplane].mem->bo->offset +
118*61046927SAndroid Build Coastguard Worker             v3dv_layer_offset(image, 0, image_view->vk.base_array_layer,
119*61046927SAndroid Build Coastguard Worker                               iplane);
120*61046927SAndroid Build Coastguard Worker          tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
121*61046927SAndroid Build Coastguard Worker 
122*61046927SAndroid Build Coastguard Worker          bool is_srgb = vk_format_is_srgb(image_view->vk.format);
123*61046927SAndroid Build Coastguard Worker 
124*61046927SAndroid Build Coastguard Worker          /* V3D 4.x doesn't have the reverse and swap_r/b bits, so we compose
125*61046927SAndroid Build Coastguard Worker           * the reverse and/or swap_r/b swizzle from the format table with the
126*61046927SAndroid Build Coastguard Worker           * image view swizzle. This, however, doesn't work for border colors,
127*61046927SAndroid Build Coastguard Worker           * for that there is the reverse_standard_border_color.
128*61046927SAndroid Build Coastguard Worker           *
129*61046927SAndroid Build Coastguard Worker           * In v3d 7.x, however, there is no reverse_standard_border_color bit,
130*61046927SAndroid Build Coastguard Worker           * since the reverse and swap_r/b bits also affect border colors. It is
131*61046927SAndroid Build Coastguard Worker           * because of this that we absolutely need to use these bits with
132*61046927SAndroid Build Coastguard Worker           * reversed and swpaped formats, since that's the only way to ensure
133*61046927SAndroid Build Coastguard Worker           * correct border colors. In that case we don't want to program the
134*61046927SAndroid Build Coastguard Worker           * swizzle to the composition of the format swizzle and the view
135*61046927SAndroid Build Coastguard Worker           * swizzle like we do in v3d 4.x, since the format swizzle is applied
136*61046927SAndroid Build Coastguard Worker           * via the reverse and swap_r/b bits.
137*61046927SAndroid Build Coastguard Worker           */
138*61046927SAndroid Build Coastguard Worker #if V3D_VERSION == 42
139*61046927SAndroid Build Coastguard Worker          tex.srgb = is_srgb;
140*61046927SAndroid Build Coastguard Worker          tex.reverse_standard_border_color =
141*61046927SAndroid Build Coastguard Worker             image_view->planes[plane].channel_reverse;
142*61046927SAndroid Build Coastguard Worker #endif
143*61046927SAndroid Build Coastguard Worker #if V3D_VERSION >= 71
144*61046927SAndroid Build Coastguard Worker          tex.transfer_func = is_srgb ? TRANSFER_FUNC_SRGB : TRANSFER_FUNC_NONE;
145*61046927SAndroid Build Coastguard Worker 
146*61046927SAndroid Build Coastguard Worker          tex.reverse = image_view->planes[plane].channel_reverse;
147*61046927SAndroid Build Coastguard Worker          tex.r_b_swap = image_view->planes[plane].swap_rb;
148*61046927SAndroid Build Coastguard Worker 
149*61046927SAndroid Build Coastguard Worker          if (tex.reverse || tex.r_b_swap) {
150*61046927SAndroid Build Coastguard Worker             tex.swizzle_r =
151*61046927SAndroid Build Coastguard Worker                v3d_translate_pipe_swizzle(image_view->view_swizzle[0]);
152*61046927SAndroid Build Coastguard Worker             tex.swizzle_g =
153*61046927SAndroid Build Coastguard Worker                v3d_translate_pipe_swizzle(image_view->view_swizzle[1]);
154*61046927SAndroid Build Coastguard Worker             tex.swizzle_b =
155*61046927SAndroid Build Coastguard Worker                v3d_translate_pipe_swizzle(image_view->view_swizzle[2]);
156*61046927SAndroid Build Coastguard Worker             tex.swizzle_a =
157*61046927SAndroid Build Coastguard Worker                v3d_translate_pipe_swizzle(image_view->view_swizzle[3]);
158*61046927SAndroid Build Coastguard Worker          }
159*61046927SAndroid Build Coastguard Worker 
160*61046927SAndroid Build Coastguard Worker          tex.chroma_offset_x = 1;
161*61046927SAndroid Build Coastguard Worker          tex.chroma_offset_y = 1;
162*61046927SAndroid Build Coastguard Worker          /* See comment in XML field definition for rationale of the shifts */
163*61046927SAndroid Build Coastguard Worker          tex.texture_base_pointer_cb = base_offset >> 6;
164*61046927SAndroid Build Coastguard Worker          tex.texture_base_pointer_cr = base_offset >> 6;
165*61046927SAndroid Build Coastguard Worker #endif
166*61046927SAndroid Build Coastguard Worker       }
167*61046927SAndroid Build Coastguard Worker    }
168*61046927SAndroid Build Coastguard Worker }
169*61046927SAndroid Build Coastguard Worker 
170*61046927SAndroid Build Coastguard Worker void
v3dX(pack_texture_shader_state)171*61046927SAndroid Build Coastguard Worker v3dX(pack_texture_shader_state)(struct v3dv_device *device,
172*61046927SAndroid Build Coastguard Worker                                 struct v3dv_image_view *iview)
173*61046927SAndroid Build Coastguard Worker {
174*61046927SAndroid Build Coastguard Worker    pack_texture_shader_state_helper(device, iview, false);
175*61046927SAndroid Build Coastguard Worker    if (iview->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
176*61046927SAndroid Build Coastguard Worker       pack_texture_shader_state_helper(device, iview, true);
177*61046927SAndroid Build Coastguard Worker }
178*61046927SAndroid Build Coastguard Worker 
179*61046927SAndroid Build Coastguard Worker void
v3dX(pack_texture_shader_state_from_buffer_view)180*61046927SAndroid Build Coastguard Worker v3dX(pack_texture_shader_state_from_buffer_view)(struct v3dv_device *device,
181*61046927SAndroid Build Coastguard Worker                                                  struct v3dv_buffer_view *buffer_view)
182*61046927SAndroid Build Coastguard Worker {
183*61046927SAndroid Build Coastguard Worker    assert(buffer_view->buffer);
184*61046927SAndroid Build Coastguard Worker    const struct v3dv_buffer *buffer = buffer_view->buffer;
185*61046927SAndroid Build Coastguard Worker 
186*61046927SAndroid Build Coastguard Worker    v3dvx_pack(buffer_view->texture_shader_state, TEXTURE_SHADER_STATE, tex) {
187*61046927SAndroid Build Coastguard Worker       tex.swizzle_r =
188*61046927SAndroid Build Coastguard Worker          v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[0]);
189*61046927SAndroid Build Coastguard Worker       tex.swizzle_g =
190*61046927SAndroid Build Coastguard Worker          v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[1]);
191*61046927SAndroid Build Coastguard Worker       tex.swizzle_b =
192*61046927SAndroid Build Coastguard Worker          v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[2]);
193*61046927SAndroid Build Coastguard Worker       tex.swizzle_a =
194*61046927SAndroid Build Coastguard Worker          v3d_translate_pipe_swizzle(buffer_view->format->planes[0].swizzle[3]);
195*61046927SAndroid Build Coastguard Worker 
196*61046927SAndroid Build Coastguard Worker       tex.image_depth = 1;
197*61046927SAndroid Build Coastguard Worker 
198*61046927SAndroid Build Coastguard Worker       /* On 4.x, the height of a 1D texture is redefined to be the upper 14
199*61046927SAndroid Build Coastguard Worker        * bits of the width (which is only usable with txf) (or in other words,
200*61046927SAndroid Build Coastguard Worker        * we are providing a 28 bit field for size, but split on the usual
201*61046927SAndroid Build Coastguard Worker        * 14bit height/width).
202*61046927SAndroid Build Coastguard Worker        */
203*61046927SAndroid Build Coastguard Worker       tex.image_width = buffer_view->num_elements;
204*61046927SAndroid Build Coastguard Worker       tex.image_height = tex.image_width >> 14;
205*61046927SAndroid Build Coastguard Worker       tex.image_width &= (1 << 14) - 1;
206*61046927SAndroid Build Coastguard Worker       tex.image_height &= (1 << 14) - 1;
207*61046927SAndroid Build Coastguard Worker 
208*61046927SAndroid Build Coastguard Worker       assert(buffer_view->format->plane_count == 1);
209*61046927SAndroid Build Coastguard Worker       tex.texture_type = buffer_view->format->planes[0].tex_type;
210*61046927SAndroid Build Coastguard Worker 
211*61046927SAndroid Build Coastguard Worker       bool is_srgb = vk_format_is_srgb(buffer_view->vk_format);
212*61046927SAndroid Build Coastguard Worker #if V3D_VERSION == 42
213*61046927SAndroid Build Coastguard Worker       tex.srgb = is_srgb;
214*61046927SAndroid Build Coastguard Worker #endif
215*61046927SAndroid Build Coastguard Worker #if V3D_VERSION >= 71
216*61046927SAndroid Build Coastguard Worker       tex.transfer_func = is_srgb ? TRANSFER_FUNC_SRGB : TRANSFER_FUNC_NONE;
217*61046927SAndroid Build Coastguard Worker #endif
218*61046927SAndroid Build Coastguard Worker 
219*61046927SAndroid Build Coastguard Worker       /* At this point we don't have the job. That's the reason the first
220*61046927SAndroid Build Coastguard Worker        * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
221*61046927SAndroid Build Coastguard Worker        * add the bo to the job. This also means that we need to add manually
222*61046927SAndroid Build Coastguard Worker        * the image bo to the job using the texture.
223*61046927SAndroid Build Coastguard Worker        */
224*61046927SAndroid Build Coastguard Worker       const uint32_t base_offset =
225*61046927SAndroid Build Coastguard Worker          buffer->mem->bo->offset +
226*61046927SAndroid Build Coastguard Worker          buffer->mem_offset +
227*61046927SAndroid Build Coastguard Worker          buffer_view->offset;
228*61046927SAndroid Build Coastguard Worker 
229*61046927SAndroid Build Coastguard Worker       tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
230*61046927SAndroid Build Coastguard Worker 
231*61046927SAndroid Build Coastguard Worker #if V3D_VERSION >= 71
232*61046927SAndroid Build Coastguard Worker       tex.chroma_offset_x = 1;
233*61046927SAndroid Build Coastguard Worker       tex.chroma_offset_y = 1;
234*61046927SAndroid Build Coastguard Worker       /* See comment in XML field definition for rationale of the shifts */
235*61046927SAndroid Build Coastguard Worker       tex.texture_base_pointer_cb = base_offset >> 6;
236*61046927SAndroid Build Coastguard Worker       tex.texture_base_pointer_cr = base_offset >> 6;
237*61046927SAndroid Build Coastguard Worker #endif
238*61046927SAndroid Build Coastguard Worker    }
239*61046927SAndroid Build Coastguard Worker }
240