summaryrefslogtreecommitdiff
path: root/src/imagination/vulkan/pvr_tex_state.c
blob: 4de0e9999d1296a313769453ba62b6d6bfccf565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright © 2022 Imagination Technologies Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdint.h>
#include <vulkan/vulkan.h>

#include "hwdef/rogue_hw_defs.h"
#include "pipe/p_defines.h"
#include "pvr_csb.h"
#include "pvr_device_info.h"
#include "pvr_formats.h"
#include "pvr_private.h"
#include "pvr_tex_state.h"
#include "util/macros.h"
#include "util/u_math.h"
#include "vk_format.h"
#include "vk_log.h"

static enum ROGUE_TEXSTATE_SWIZ pvr_get_hw_swizzle(VkComponentSwizzle comp,
                                                   enum pipe_swizzle swz)
{
   switch (swz) {
   case PIPE_SWIZZLE_0:
      return ROGUE_TEXSTATE_SWIZ_SRC_ZERO;
   case PIPE_SWIZZLE_1:
      return ROGUE_TEXSTATE_SWIZ_SRC_ONE;
   case PIPE_SWIZZLE_X:
      return ROGUE_TEXSTATE_SWIZ_SRCCHAN_0;
   case PIPE_SWIZZLE_Y:
      return ROGUE_TEXSTATE_SWIZ_SRCCHAN_1;
   case PIPE_SWIZZLE_Z:
      return ROGUE_TEXSTATE_SWIZ_SRCCHAN_2;
   case PIPE_SWIZZLE_W:
      return ROGUE_TEXSTATE_SWIZ_SRCCHAN_3;
   case PIPE_SWIZZLE_NONE:
      if (comp == VK_COMPONENT_SWIZZLE_A)
         return ROGUE_TEXSTATE_SWIZ_SRC_ONE;
      else
         return ROGUE_TEXSTATE_SWIZ_SRC_ZERO;
   default:
      unreachable("Unknown enum pipe_swizzle");
   };
}

VkResult
pvr_pack_tex_state(struct pvr_device *device,
                   const struct pvr_texture_state_info *info,
                   uint64_t state[static const ROGUE_NUM_TEXSTATE_IMAGE_WORDS])
{
   const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
   enum pvr_memlayout mem_layout;
   VkImageViewType iview_type;

   if (info->type == VK_IMAGE_VIEW_TYPE_1D &&
       info->mem_layout == PVR_MEMLAYOUT_LINEAR) {
      /* Change the memory layout to twiddled as there isn't a TEXSTATE_TEXTYPE
       * for 1D linear and 1D twiddled is equivalent.
       */
      mem_layout = PVR_MEMLAYOUT_TWIDDLED;
   } else {
      mem_layout = info->mem_layout;
   }

   if (info->is_cube && info->tex_state_type != PVR_TEXTURE_STATE_SAMPLE)
      iview_type = VK_IMAGE_VIEW_TYPE_2D;
   else
      iview_type = info->type;

   pvr_csb_pack (&state[0], TEXSTATE_IMAGE_WORD0, word0) {
      if (mem_layout == PVR_MEMLAYOUT_LINEAR) {
         switch (iview_type) {
         case VK_IMAGE_VIEW_TYPE_2D:
         case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
            word0.textype = PVRX(TEXSTATE_TEXTYPE_STRIDE);
            break;

         default:
            return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
         }
      } else if (mem_layout == PVR_MEMLAYOUT_TWIDDLED) {
         switch (iview_type) {
         case VK_IMAGE_VIEW_TYPE_1D:
         case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
            word0.textype = PVRX(TEXSTATE_TEXTYPE_1D);
            break;

         case VK_IMAGE_VIEW_TYPE_2D:
         case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
            word0.textype = PVRX(TEXSTATE_TEXTYPE_2D);
            break;

         case VK_IMAGE_VIEW_TYPE_CUBE:
         case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
            word0.textype = PVRX(TEXSTATE_TEXTYPE_CUBE);
            break;

         default:
            return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
         }
      } else if (mem_layout == PVR_MEMLAYOUT_3DTWIDDLED) {
         switch (iview_type) {
         case VK_IMAGE_VIEW_TYPE_3D:
            word0.textype = PVRX(TEXSTATE_TEXTYPE_3D);
            break;

         default:
            return vk_error(device, VK_ERROR_FORMAT_NOT_SUPPORTED);
         }
      } else {
         unreachable("Unknown memory layout");
      }

      /* When sampling from a combined D/S image, the TPU will default to only
       * the depth aspect.
       * The driver must select the correct single aspect format when sampling
       * to avoid this.
       */
      word0.texformat =
         pvr_get_tex_format_aspect(info->format, info->aspect_mask);
      word0.smpcnt = util_logbase2(info->sample_count);
      word0.swiz0 =
         pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_R, info->swizzle[0]);
      word0.swiz1 =
         pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_G, info->swizzle[1]);
      word0.swiz2 =
         pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_B, info->swizzle[2]);
      word0.swiz3 =
         pvr_get_hw_swizzle(VK_COMPONENT_SWIZZLE_A, info->swizzle[3]);

      /* Gamma */
      if (vk_format_is_srgb(info->format)) {
         /* Gamma for 2 Component Formats has to be handled differently. */
         if (vk_format_get_nr_components(info->format) == 2) {
            /* Enable Gamma only for Channel 0 if Channel 1 is an Alpha
             * Channel.
             */
            if (vk_format_has_alpha(info->format)) {
               word0.twocomp_gamma = PVRX(TEXSTATE_TWOCOMP_GAMMA_R);
            } else {
               /* Otherwise Enable Gamma for both the Channels. */
               word0.twocomp_gamma = PVRX(TEXSTATE_TWOCOMP_GAMMA_RG);

               /* If Channel 0 happens to be the Alpha Channel, the
                * ALPHA_MSB bit would not be set thereby disabling Gamma
                * for Channel 0.
                */
            }
         } else {
            word0.gamma = PVRX(TEXSTATE_GAMMA_ON);
         }
      }

      word0.width = info->extent.width - 1;
      if (iview_type != VK_IMAGE_VIEW_TYPE_1D &&
          iview_type != VK_IMAGE_VIEW_TYPE_1D_ARRAY)
         word0.height = info->extent.height - 1;
   }

   if (mem_layout == PVR_MEMLAYOUT_LINEAR) {
      pvr_csb_pack (&state[1], TEXSTATE_STRIDE_IMAGE_WORD1, word1) {
         assert(info->stride > 0U);
         word1.stride = info->stride - 1U;
         word1.num_mip_levels = info->mip_levels;
         word1.mipmaps_present = info->mipmaps_present;

         word1.texaddr = PVR_DEV_ADDR_OFFSET(info->addr, info->offset);

         if (vk_format_is_alpha_on_msb(info->format))
            word1.alpha_msb = true;

         if (!PVR_HAS_FEATURE(dev_info, tpu_extended_integer_lookup) &&
             !PVR_HAS_FEATURE(dev_info, tpu_image_state_v2)) {
            if (info->flags & PVR_TEXFLAGS_INDEX_LOOKUP ||
                info->flags & PVR_TEXFLAGS_BUFFER)
               word1.index_lookup = true;
         }

         if (info->flags & PVR_TEXFLAGS_BUFFER)
            word1.mipmaps_present = false;

         if (PVR_HAS_FEATURE(dev_info, tpu_image_state_v2) &&
             vk_format_is_compressed(info->format))
            word1.tpu_image_state_v2_compression_mode =
               PVRX(TEXSTATE_COMPRESSION_MODE_TPU);
      }
   } else {
      pvr_csb_pack (&state[1], TEXSTATE_IMAGE_WORD1, word1) {
         word1.num_mip_levels = info->mip_levels;
         word1.mipmaps_present = info->mipmaps_present;
         word1.baselevel = info->base_level;

         if (iview_type == VK_IMAGE_VIEW_TYPE_3D) {
            if (info->extent.depth > 0)
               word1.depth = info->extent.depth - 1;
         } else if (PVR_HAS_FEATURE(dev_info, tpu_array_textures)) {
            uint32_t array_layers = info->array_size;

            if (iview_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
               array_layers /= 6;

            if (array_layers > 0)
               word1.depth = array_layers - 1;
         }

         word1.texaddr = PVR_DEV_ADDR_OFFSET(info->addr, info->offset);

         if (!PVR_HAS_FEATURE(dev_info, tpu_extended_integer_lookup) &&
             !PVR_HAS_FEATURE(dev_info, tpu_image_state_v2)) {
            if (info->flags & PVR_TEXFLAGS_INDEX_LOOKUP ||
                info->flags & PVR_TEXFLAGS_BUFFER)
               word1.index_lookup = true;
         }

         if (info->flags & PVR_TEXFLAGS_BUFFER)
            word1.mipmaps_present = false;

         if (info->flags & PVR_TEXFLAGS_BORDER)
            word1.border = true;

         if (vk_format_is_alpha_on_msb(info->format))
            word1.alpha_msb = true;

         if (PVR_HAS_FEATURE(dev_info, tpu_image_state_v2) &&
             vk_format_is_compressed(info->format))
            word1.tpu_image_state_v2_compression_mode =
               PVRX(TEXSTATE_COMPRESSION_MODE_TPU);
      }
   }

   return VK_SUCCESS;
}