Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/broadcom/vulkan/v3dvx_device.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
26
#include "broadcom/common/v3d_macros.h"
27
#include "broadcom/cle/v3dx_pack.h"
28
#include "broadcom/compiler/v3d_compiler.h"
29
#include "vk_format_info.h"
30
#include "util/u_pack_color.h"
31
32
static const enum V3DX(Wrap_Mode) vk_to_v3d_wrap_mode[] = {
33
[VK_SAMPLER_ADDRESS_MODE_REPEAT] = V3D_WRAP_MODE_REPEAT,
34
[VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = V3D_WRAP_MODE_MIRROR,
35
[VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE] = V3D_WRAP_MODE_CLAMP,
36
[VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = V3D_WRAP_MODE_MIRROR_ONCE,
37
[VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = V3D_WRAP_MODE_BORDER,
38
};
39
40
static const enum V3DX(Compare_Function)
41
vk_to_v3d_compare_func[] = {
42
[VK_COMPARE_OP_NEVER] = V3D_COMPARE_FUNC_NEVER,
43
[VK_COMPARE_OP_LESS] = V3D_COMPARE_FUNC_LESS,
44
[VK_COMPARE_OP_EQUAL] = V3D_COMPARE_FUNC_EQUAL,
45
[VK_COMPARE_OP_LESS_OR_EQUAL] = V3D_COMPARE_FUNC_LEQUAL,
46
[VK_COMPARE_OP_GREATER] = V3D_COMPARE_FUNC_GREATER,
47
[VK_COMPARE_OP_NOT_EQUAL] = V3D_COMPARE_FUNC_NOTEQUAL,
48
[VK_COMPARE_OP_GREATER_OR_EQUAL] = V3D_COMPARE_FUNC_GEQUAL,
49
[VK_COMPARE_OP_ALWAYS] = V3D_COMPARE_FUNC_ALWAYS,
50
};
51
52
void
53
v3dX(pack_sampler_state)(struct v3dv_sampler *sampler,
54
const VkSamplerCreateInfo *pCreateInfo)
55
{
56
enum V3DX(Border_Color_Mode) border_color_mode;
57
58
/* For now we only support the preset Vulkan border color modes. If we
59
* want to implement VK_EXT_custom_border_color in the future we would have
60
* to use V3D_BORDER_COLOR_FOLLOWS, and fill up border_color_word_[0/1/2/3]
61
* SAMPLER_STATE.
62
*/
63
switch (pCreateInfo->borderColor) {
64
case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
65
case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
66
border_color_mode = V3D_BORDER_COLOR_0000;
67
break;
68
case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
69
case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
70
border_color_mode = V3D_BORDER_COLOR_0001;
71
break;
72
case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
73
case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
74
border_color_mode = V3D_BORDER_COLOR_1111;
75
break;
76
default:
77
unreachable("Unknown border color");
78
break;
79
}
80
81
/* For some texture formats, when clamping to transparent black border the
82
* CTS expects alpha to be set to 1 instead of 0, but the border color mode
83
* will take priority over the texture state swizzle, so the only way to
84
* fix that is to apply a swizzle in the shader. Here we keep track of
85
* whether we are activating that mode and we will decide if we need to
86
* activate the texture swizzle lowering in the shader key at compile time
87
* depending on the actual texture format.
88
*/
89
if ((pCreateInfo->addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
90
pCreateInfo->addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER ||
91
pCreateInfo->addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER) &&
92
border_color_mode == V3D_BORDER_COLOR_0000) {
93
sampler->clamp_to_transparent_black_border = true;
94
}
95
96
v3dvx_pack(sampler->sampler_state, SAMPLER_STATE, s) {
97
if (pCreateInfo->anisotropyEnable) {
98
s.anisotropy_enable = true;
99
if (pCreateInfo->maxAnisotropy > 8)
100
s.maximum_anisotropy = 3;
101
else if (pCreateInfo->maxAnisotropy > 4)
102
s.maximum_anisotropy = 2;
103
else if (pCreateInfo->maxAnisotropy > 2)
104
s.maximum_anisotropy = 1;
105
}
106
107
s.border_color_mode = border_color_mode;
108
109
s.wrap_i_border = false; /* Also hardcoded on v3d */
110
s.wrap_s = vk_to_v3d_wrap_mode[pCreateInfo->addressModeU];
111
s.wrap_t = vk_to_v3d_wrap_mode[pCreateInfo->addressModeV];
112
s.wrap_r = vk_to_v3d_wrap_mode[pCreateInfo->addressModeW];
113
s.fixed_bias = pCreateInfo->mipLodBias;
114
s.max_level_of_detail = MIN2(MAX2(0, pCreateInfo->maxLod), 15);
115
s.min_level_of_detail = MIN2(MAX2(0, pCreateInfo->minLod), 15);
116
s.srgb_disable = 0; /* Not even set by v3d */
117
s.depth_compare_function =
118
vk_to_v3d_compare_func[pCreateInfo->compareEnable ?
119
pCreateInfo->compareOp : VK_COMPARE_OP_NEVER];
120
s.mip_filter_nearest = pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_NEAREST;
121
s.min_filter_nearest = pCreateInfo->minFilter == VK_FILTER_NEAREST;
122
s.mag_filter_nearest = pCreateInfo->magFilter == VK_FILTER_NEAREST;
123
}
124
}
125
126
/**
127
* This computes the maximum bpp used by any of the render targets used by
128
* a particular subpass and checks if any of those render targets are
129
* multisampled. If we don't have a subpass (when we are not inside a
130
* render pass), then we assume that all framebuffer attachments are used.
131
*/
132
void
133
v3dX(framebuffer_compute_internal_bpp_msaa)(
134
const struct v3dv_framebuffer *framebuffer,
135
const struct v3dv_subpass *subpass,
136
uint8_t *max_bpp,
137
bool *msaa)
138
{
139
STATIC_ASSERT(RENDER_TARGET_MAXIMUM_32BPP == 0);
140
*max_bpp = RENDER_TARGET_MAXIMUM_32BPP;
141
*msaa = false;
142
143
if (subpass) {
144
for (uint32_t i = 0; i < subpass->color_count; i++) {
145
uint32_t att_idx = subpass->color_attachments[i].attachment;
146
if (att_idx == VK_ATTACHMENT_UNUSED)
147
continue;
148
149
const struct v3dv_image_view *att = framebuffer->attachments[att_idx];
150
assert(att);
151
152
if (att->aspects & VK_IMAGE_ASPECT_COLOR_BIT)
153
*max_bpp = MAX2(*max_bpp, att->internal_bpp);
154
155
if (att->image->samples > VK_SAMPLE_COUNT_1_BIT)
156
*msaa = true;
157
}
158
159
if (!*msaa && subpass->ds_attachment.attachment != VK_ATTACHMENT_UNUSED) {
160
const struct v3dv_image_view *att =
161
framebuffer->attachments[subpass->ds_attachment.attachment];
162
assert(att);
163
164
if (att->image->samples > VK_SAMPLE_COUNT_1_BIT)
165
*msaa = true;
166
}
167
168
return;
169
}
170
171
assert(framebuffer->attachment_count <= 4);
172
for (uint32_t i = 0; i < framebuffer->attachment_count; i++) {
173
const struct v3dv_image_view *att = framebuffer->attachments[i];
174
assert(att);
175
176
if (att->aspects & VK_IMAGE_ASPECT_COLOR_BIT)
177
*max_bpp = MAX2(*max_bpp, att->internal_bpp);
178
179
if (att->image->samples > VK_SAMPLE_COUNT_1_BIT)
180
*msaa = true;
181
}
182
183
return;
184
}
185
186
uint32_t
187
v3dX(zs_buffer_from_aspect_bits)(VkImageAspectFlags aspects)
188
{
189
const VkImageAspectFlags zs_aspects =
190
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
191
const VkImageAspectFlags filtered_aspects = aspects & zs_aspects;
192
193
if (filtered_aspects == zs_aspects)
194
return ZSTENCIL;
195
else if (filtered_aspects == VK_IMAGE_ASPECT_DEPTH_BIT)
196
return Z;
197
else if (filtered_aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
198
return STENCIL;
199
else
200
return NONE;
201
}
202
203
void
204
v3dX(get_hw_clear_color)(const VkClearColorValue *color,
205
uint32_t internal_type,
206
uint32_t internal_size,
207
uint32_t *hw_color)
208
{
209
union util_color uc;
210
switch (internal_type) {
211
case V3D_INTERNAL_TYPE_8:
212
util_pack_color(color->float32, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
213
memcpy(hw_color, uc.ui, internal_size);
214
break;
215
case V3D_INTERNAL_TYPE_8I:
216
case V3D_INTERNAL_TYPE_8UI:
217
hw_color[0] = ((color->uint32[0] & 0xff) |
218
(color->uint32[1] & 0xff) << 8 |
219
(color->uint32[2] & 0xff) << 16 |
220
(color->uint32[3] & 0xff) << 24);
221
break;
222
case V3D_INTERNAL_TYPE_16F:
223
util_pack_color(color->float32, PIPE_FORMAT_R16G16B16A16_FLOAT, &uc);
224
memcpy(hw_color, uc.ui, internal_size);
225
break;
226
case V3D_INTERNAL_TYPE_16I:
227
case V3D_INTERNAL_TYPE_16UI:
228
hw_color[0] = ((color->uint32[0] & 0xffff) | color->uint32[1] << 16);
229
hw_color[1] = ((color->uint32[2] & 0xffff) | color->uint32[3] << 16);
230
break;
231
case V3D_INTERNAL_TYPE_32F:
232
case V3D_INTERNAL_TYPE_32I:
233
case V3D_INTERNAL_TYPE_32UI:
234
memcpy(hw_color, color->uint32, internal_size);
235
break;
236
}
237
}
238
239
#ifdef DEBUG
240
void
241
v3dX(device_check_prepacked_sizes)(void)
242
{
243
STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
244
cl_packet_length(SAMPLER_STATE));
245
STATIC_ASSERT(V3DV_TEXTURE_SHADER_STATE_LENGTH >=
246
cl_packet_length(TEXTURE_SHADER_STATE));
247
STATIC_ASSERT(V3DV_SAMPLER_STATE_LENGTH >=
248
cl_packet_length(SAMPLER_STATE));
249
STATIC_ASSERT(V3DV_BLEND_CFG_LENGTH>=
250
cl_packet_length(BLEND_CFG));
251
STATIC_ASSERT(V3DV_CFG_BITS_LENGTH>=
252
cl_packet_length(CFG_BITS));
253
STATIC_ASSERT(V3DV_GL_SHADER_STATE_RECORD_LENGTH >=
254
cl_packet_length(GL_SHADER_STATE_RECORD));
255
STATIC_ASSERT(V3DV_VCM_CACHE_SIZE_LENGTH>=
256
cl_packet_length(VCM_CACHE_SIZE));
257
STATIC_ASSERT(V3DV_GL_SHADER_STATE_ATTRIBUTE_RECORD_LENGTH >=
258
cl_packet_length(GL_SHADER_STATE_ATTRIBUTE_RECORD));
259
STATIC_ASSERT(V3DV_STENCIL_CFG_LENGTH >=
260
cl_packet_length(STENCIL_CFG));
261
}
262
#endif
263
264