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