1/*
2 * Copyright © 2021 Raspberry Pi Ltd
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 "v3dv_private.h"
25#include "broadcom/common/v3d_macros.h"
26#include "broadcom/cle/v3dx_pack.h"
27#include "broadcom/compiler/v3d_compiler.h"
28
29/*
30 * Packs and ensure bo for the shader state (the latter can be temporal).
31 */
32static void
33pack_texture_shader_state_helper(struct v3dv_device *device,
34                                 struct v3dv_image_view *image_view,
35                                 bool for_cube_map_array_storage)
36{
37   assert(!for_cube_map_array_storage ||
38          image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY);
39   const uint32_t index = for_cube_map_array_storage ? 1 : 0;
40
41   assert(image_view->vk.image);
42   const struct v3dv_image *image = (struct v3dv_image *) image_view->vk.image;
43
44   assert(image->vk.samples == VK_SAMPLE_COUNT_1_BIT ||
45          image->vk.samples == VK_SAMPLE_COUNT_4_BIT);
46   const uint32_t msaa_scale = image->vk.samples == VK_SAMPLE_COUNT_1_BIT ? 1 : 2;
47
48   v3dvx_pack(image_view->texture_shader_state[index], TEXTURE_SHADER_STATE, tex) {
49
50      tex.level_0_is_strictly_uif =
51         (image->slices[0].tiling == V3D_TILING_UIF_XOR ||
52          image->slices[0].tiling == V3D_TILING_UIF_NO_XOR);
53
54      tex.level_0_xor_enable = (image->slices[0].tiling == V3D_TILING_UIF_XOR);
55
56      if (tex.level_0_is_strictly_uif)
57         tex.level_0_ub_pad = image->slices[0].ub_pad;
58
59      /* FIXME: v3d never sets uif_xor_disable, but uses it on the following
60       * check so let's set the default value
61       */
62      tex.uif_xor_disable = false;
63      if (tex.uif_xor_disable ||
64          tex.level_0_is_strictly_uif) {
65         tex.extended = true;
66      }
67
68      tex.base_level = image_view->vk.base_mip_level;
69      tex.max_level = image_view->vk.base_mip_level +
70                      image_view->vk.level_count - 1;
71
72      tex.swizzle_r = v3d_translate_pipe_swizzle(image_view->swizzle[0]);
73      tex.swizzle_g = v3d_translate_pipe_swizzle(image_view->swizzle[1]);
74      tex.swizzle_b = v3d_translate_pipe_swizzle(image_view->swizzle[2]);
75      tex.swizzle_a = v3d_translate_pipe_swizzle(image_view->swizzle[3]);
76
77      tex.reverse_standard_border_color = image_view->channel_reverse;
78
79      tex.texture_type = image_view->format->tex_type;
80
81      if (image->vk.image_type == VK_IMAGE_TYPE_3D) {
82         tex.image_depth = image->vk.extent.depth;
83      } else {
84         tex.image_depth = image_view->vk.layer_count;
85      }
86
87      /* Empirical testing with CTS shows that when we are sampling from cube
88       * arrays we want to set image depth to layers / 6, but not when doing
89       * image load/store.
90       */
91      if (image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY &&
92          !for_cube_map_array_storage) {
93         assert(tex.image_depth % 6 == 0);
94         tex.image_depth /= 6;
95      }
96
97      tex.image_height = image->vk.extent.height * msaa_scale;
98      tex.image_width = image->vk.extent.width * msaa_scale;
99
100      /* On 4.x, the height of a 1D texture is redefined to be the
101       * upper 14 bits of the width (which is only usable with txf).
102       */
103      if (image->vk.image_type == VK_IMAGE_TYPE_1D) {
104         tex.image_height = tex.image_width >> 14;
105      }
106      tex.image_width &= (1 << 14) - 1;
107      tex.image_height &= (1 << 14) - 1;
108
109      tex.array_stride_64_byte_aligned = image->cube_map_stride / 64;
110
111      tex.srgb = vk_format_is_srgb(image_view->vk.view_format);
112
113      /* At this point we don't have the job. That's the reason the first
114       * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
115       * add the bo to the job. This also means that we need to add manually
116       * the image bo to the job using the texture.
117       */
118      const uint32_t base_offset =
119         image->mem->bo->offset +
120         v3dv_layer_offset(image, 0, image_view->vk.base_array_layer);
121      tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
122   }
123}
124
125void
126v3dX(pack_texture_shader_state)(struct v3dv_device *device,
127                                struct v3dv_image_view *iview)
128{
129   pack_texture_shader_state_helper(device, iview, false);
130   if (iview->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
131      pack_texture_shader_state_helper(device, iview, true);
132}
133
134void
135v3dX(pack_texture_shader_state_from_buffer_view)(struct v3dv_device *device,
136                                                 struct v3dv_buffer_view *buffer_view)
137{
138   assert(buffer_view->buffer);
139   const struct v3dv_buffer *buffer = buffer_view->buffer;
140
141   v3dvx_pack(buffer_view->texture_shader_state, TEXTURE_SHADER_STATE, tex) {
142      tex.swizzle_r = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_X);
143      tex.swizzle_g = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_Y);
144      tex.swizzle_b = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_Z);
145      tex.swizzle_a = v3d_translate_pipe_swizzle(PIPE_SWIZZLE_W);
146
147      tex.image_depth = 1;
148
149      /* On 4.x, the height of a 1D texture is redefined to be the upper 14
150       * bits of the width (which is only usable with txf) (or in other words,
151       * we are providing a 28 bit field for size, but split on the usual
152       * 14bit height/width).
153       */
154      tex.image_width = buffer_view->num_elements;
155      tex.image_height = tex.image_width >> 14;
156      tex.image_width &= (1 << 14) - 1;
157      tex.image_height &= (1 << 14) - 1;
158
159      tex.texture_type = buffer_view->format->tex_type;
160      tex.srgb = vk_format_is_srgb(buffer_view->vk_format);
161
162      /* At this point we don't have the job. That's the reason the first
163       * parameter is NULL, to avoid a crash when cl_pack_emit_reloc tries to
164       * add the bo to the job. This also means that we need to add manually
165       * the image bo to the job using the texture.
166       */
167      const uint32_t base_offset =
168         buffer->mem->bo->offset +
169         buffer->mem_offset +
170         buffer_view->offset;
171
172      tex.texture_base_pointer = v3dv_cl_address(NULL, base_offset);
173   }
174}
175