Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/freedreno/a6xx/fd6_image.c
4574 views
1
/*
2
* Copyright (C) 2017 Rob Clark <[email protected]>
3
* Copyright © 2018 Google, Inc.
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
* and/or sell copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
14
* Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
* SOFTWARE.
23
*
24
* Authors:
25
* Rob Clark <[email protected]>
26
*/
27
28
#include "pipe/p_state.h"
29
30
#include "freedreno_resource.h"
31
#include "freedreno_state.h"
32
33
#include "fd6_format.h"
34
#include "fd6_image.h"
35
#include "fd6_resource.h"
36
#include "fd6_texture.h"
37
38
struct fd6_image {
39
struct pipe_resource *prsc;
40
enum pipe_format pfmt;
41
enum a6xx_tex_type type;
42
bool srgb;
43
uint32_t cpp;
44
uint32_t level;
45
uint32_t width;
46
uint32_t height;
47
uint32_t depth;
48
uint32_t pitch;
49
uint32_t array_pitch;
50
struct fd_bo *bo;
51
uint32_t ubwc_offset;
52
uint32_t offset;
53
bool buffer;
54
};
55
56
static void
57
translate_image(struct fd6_image *img, const struct pipe_image_view *pimg)
58
{
59
enum pipe_format format = pimg->format;
60
struct pipe_resource *prsc = pimg->resource;
61
struct fd_resource *rsc = fd_resource(prsc);
62
63
if (!prsc) {
64
memset(img, 0, sizeof(*img));
65
return;
66
}
67
68
img->prsc = prsc;
69
img->pfmt = format;
70
img->type = fd6_tex_type(prsc->target);
71
img->srgb = util_format_is_srgb(format);
72
img->cpp = rsc->layout.cpp;
73
img->bo = rsc->bo;
74
75
/* Treat cube textures as 2d-array: */
76
if (img->type == A6XX_TEX_CUBE)
77
img->type = A6XX_TEX_2D;
78
79
if (prsc->target == PIPE_BUFFER) {
80
img->buffer = true;
81
img->ubwc_offset = 0; /* not valid for buffers */
82
img->offset = pimg->u.buf.offset;
83
img->pitch = 0;
84
img->array_pitch = 0;
85
86
/* size is encoded with low 15b in WIDTH and high bits in
87
* HEIGHT, in units of elements:
88
*/
89
unsigned sz = pimg->u.buf.size / util_format_get_blocksize(format);
90
img->width = sz & MASK(15);
91
img->height = sz >> 15;
92
img->depth = 0;
93
img->level = 0;
94
} else {
95
img->buffer = false;
96
97
unsigned lvl = pimg->u.tex.level;
98
unsigned layers = pimg->u.tex.last_layer - pimg->u.tex.first_layer + 1;
99
100
img->ubwc_offset =
101
fd_resource_ubwc_offset(rsc, lvl, pimg->u.tex.first_layer);
102
img->offset = fd_resource_offset(rsc, lvl, pimg->u.tex.first_layer);
103
img->pitch = fd_resource_pitch(rsc, lvl);
104
105
switch (prsc->target) {
106
case PIPE_TEXTURE_RECT:
107
case PIPE_TEXTURE_1D:
108
case PIPE_TEXTURE_2D:
109
img->array_pitch = rsc->layout.layer_size;
110
img->depth = 1;
111
break;
112
case PIPE_TEXTURE_1D_ARRAY:
113
case PIPE_TEXTURE_2D_ARRAY:
114
case PIPE_TEXTURE_CUBE:
115
case PIPE_TEXTURE_CUBE_ARRAY:
116
img->array_pitch = rsc->layout.layer_size;
117
// TODO the CUBE/CUBE_ARRAY might need to be layers/6 for tex state,
118
// but empirically for ibo state it shouldn't be divided.
119
img->depth = layers;
120
break;
121
case PIPE_TEXTURE_3D:
122
img->array_pitch = fd_resource_slice(rsc, lvl)->size0;
123
img->depth = u_minify(prsc->depth0, lvl);
124
break;
125
default:
126
break;
127
}
128
129
img->level = lvl;
130
img->width = u_minify(prsc->width0, lvl);
131
img->height = u_minify(prsc->height0, lvl);
132
}
133
}
134
135
static void
136
translate_buf(struct fd6_image *img, const struct pipe_shader_buffer *pimg)
137
{
138
enum pipe_format format = PIPE_FORMAT_R32_UINT;
139
struct pipe_resource *prsc = pimg->buffer;
140
struct fd_resource *rsc = fd_resource(prsc);
141
142
if (!prsc) {
143
memset(img, 0, sizeof(*img));
144
return;
145
}
146
147
img->prsc = prsc;
148
img->pfmt = format;
149
img->type = fd6_tex_type(prsc->target);
150
img->srgb = util_format_is_srgb(format);
151
img->cpp = rsc->layout.cpp;
152
img->bo = rsc->bo;
153
img->buffer = true;
154
155
img->ubwc_offset = 0; /* not valid for buffers */
156
img->offset = pimg->buffer_offset;
157
img->pitch = 0;
158
img->array_pitch = 0;
159
img->level = 0;
160
161
/* size is encoded with low 15b in WIDTH and high bits in HEIGHT,
162
* in units of elements:
163
*/
164
unsigned sz = pimg->buffer_size / 4;
165
img->width = sz & MASK(15);
166
img->height = sz >> 15;
167
img->depth = 0;
168
}
169
170
static void
171
emit_image_tex(struct fd_ringbuffer *ring, struct fd6_image *img)
172
{
173
struct fd_resource *rsc = fd_resource(img->prsc);
174
bool ubwc_enabled = fd_resource_ubwc_enabled(rsc, img->level);
175
176
OUT_RING(ring,
177
fd6_tex_const_0(img->prsc, img->level, img->pfmt, PIPE_SWIZZLE_X,
178
PIPE_SWIZZLE_Y, PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W));
179
OUT_RING(ring, A6XX_TEX_CONST_1_WIDTH(img->width) |
180
A6XX_TEX_CONST_1_HEIGHT(img->height));
181
OUT_RING(ring,
182
COND(img->buffer, A6XX_TEX_CONST_2_UNK4 | A6XX_TEX_CONST_2_UNK31) |
183
A6XX_TEX_CONST_2_TYPE(img->type) |
184
A6XX_TEX_CONST_2_PITCH(img->pitch));
185
OUT_RING(ring, A6XX_TEX_CONST_3_ARRAY_PITCH(img->array_pitch) |
186
COND(ubwc_enabled, A6XX_TEX_CONST_3_FLAG) |
187
COND(rsc->layout.tile_all, A6XX_TEX_CONST_3_TILE_ALL));
188
if (img->bo) {
189
OUT_RELOC(ring, img->bo, img->offset,
190
(uint64_t)A6XX_TEX_CONST_5_DEPTH(img->depth) << 32, 0);
191
} else {
192
OUT_RING(ring, 0x00000000);
193
OUT_RING(ring, A6XX_TEX_CONST_5_DEPTH(img->depth));
194
}
195
196
OUT_RING(ring, 0x00000000); /* texconst6 */
197
198
if (ubwc_enabled) {
199
uint32_t block_width, block_height;
200
fdl6_get_ubwc_blockwidth(&rsc->layout, &block_width, &block_height);
201
202
OUT_RELOC(ring, rsc->bo, img->ubwc_offset, 0, 0);
203
OUT_RING(ring, A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(
204
rsc->layout.ubwc_layer_size >> 2));
205
OUT_RING(ring, A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(
206
fdl_ubwc_pitch(&rsc->layout, img->level)) |
207
A6XX_TEX_CONST_10_FLAG_BUFFER_LOGW(util_logbase2_ceil(
208
DIV_ROUND_UP(img->width, block_width))) |
209
A6XX_TEX_CONST_10_FLAG_BUFFER_LOGH(util_logbase2_ceil(
210
DIV_ROUND_UP(img->height, block_height))));
211
} else {
212
OUT_RING(ring, 0x00000000); /* texconst7 */
213
OUT_RING(ring, 0x00000000); /* texconst8 */
214
OUT_RING(ring, 0x00000000); /* texconst9 */
215
OUT_RING(ring, 0x00000000); /* texconst10 */
216
}
217
218
OUT_RING(ring, 0x00000000); /* texconst11 */
219
OUT_RING(ring, 0x00000000); /* texconst12 */
220
OUT_RING(ring, 0x00000000); /* texconst13 */
221
OUT_RING(ring, 0x00000000); /* texconst14 */
222
OUT_RING(ring, 0x00000000); /* texconst15 */
223
}
224
225
void
226
fd6_emit_image_tex(struct fd_ringbuffer *ring,
227
const struct pipe_image_view *pimg)
228
{
229
struct fd6_image img;
230
translate_image(&img, pimg);
231
emit_image_tex(ring, &img);
232
}
233
234
void
235
fd6_emit_ssbo_tex(struct fd_ringbuffer *ring,
236
const struct pipe_shader_buffer *pbuf)
237
{
238
struct fd6_image img;
239
translate_buf(&img, pbuf);
240
emit_image_tex(ring, &img);
241
}
242
243
static void
244
emit_image_ssbo(struct fd_ringbuffer *ring, struct fd6_image *img)
245
{
246
/* If the SSBO isn't present (becasue gallium doesn't pack atomic
247
* counters), zero-fill the slot.
248
*/
249
if (!img->prsc) {
250
for (int i = 0; i < 16; i++)
251
OUT_RING(ring, 0);
252
return;
253
}
254
255
struct fd_resource *rsc = fd_resource(img->prsc);
256
enum a6xx_tile_mode tile_mode = fd_resource_tile_mode(img->prsc, img->level);
257
bool ubwc_enabled = fd_resource_ubwc_enabled(rsc, img->level);
258
259
OUT_RING(ring, A6XX_IBO_0_FMT(fd6_pipe2tex(img->pfmt)) |
260
A6XX_IBO_0_TILE_MODE(tile_mode));
261
OUT_RING(ring,
262
A6XX_IBO_1_WIDTH(img->width) | A6XX_IBO_1_HEIGHT(img->height));
263
OUT_RING(ring, A6XX_IBO_2_PITCH(img->pitch) |
264
COND(img->buffer, A6XX_IBO_2_UNK4 | A6XX_IBO_2_UNK31) |
265
A6XX_IBO_2_TYPE(img->type));
266
OUT_RING(ring, A6XX_IBO_3_ARRAY_PITCH(img->array_pitch) |
267
COND(ubwc_enabled, A6XX_IBO_3_FLAG | A6XX_IBO_3_UNK27));
268
if (img->bo) {
269
OUT_RELOC(ring, img->bo, img->offset,
270
(uint64_t)A6XX_IBO_5_DEPTH(img->depth) << 32, 0);
271
} else {
272
OUT_RING(ring, 0x00000000);
273
OUT_RING(ring, A6XX_IBO_5_DEPTH(img->depth));
274
}
275
OUT_RING(ring, 0x00000000);
276
277
if (ubwc_enabled) {
278
OUT_RELOC(ring, rsc->bo, img->ubwc_offset, 0, 0);
279
OUT_RING(ring, A6XX_IBO_9_FLAG_BUFFER_ARRAY_PITCH(
280
rsc->layout.ubwc_layer_size >> 2));
281
OUT_RING(ring, A6XX_IBO_10_FLAG_BUFFER_PITCH(
282
fdl_ubwc_pitch(&rsc->layout, img->level)));
283
} else {
284
OUT_RING(ring, 0x00000000);
285
OUT_RING(ring, 0x00000000);
286
OUT_RING(ring, 0x00000000);
287
OUT_RING(ring, 0x00000000);
288
}
289
290
OUT_RING(ring, 0x00000000);
291
OUT_RING(ring, 0x00000000);
292
OUT_RING(ring, 0x00000000);
293
OUT_RING(ring, 0x00000000);
294
OUT_RING(ring, 0x00000000);
295
}
296
297
/* Build combined image/SSBO "IBO" state, returns ownership of state reference */
298
struct fd_ringbuffer *
299
fd6_build_ibo_state(struct fd_context *ctx, const struct ir3_shader_variant *v,
300
enum pipe_shader_type shader)
301
{
302
struct fd_shaderbuf_stateobj *bufso = &ctx->shaderbuf[shader];
303
struct fd_shaderimg_stateobj *imgso = &ctx->shaderimg[shader];
304
305
struct fd_ringbuffer *state = fd_submit_new_ringbuffer(
306
ctx->batch->submit,
307
(v->shader->nir->info.num_ssbos + v->shader->nir->info.num_images) * 16 *
308
4,
309
FD_RINGBUFFER_STREAMING);
310
311
assert(shader == PIPE_SHADER_COMPUTE || shader == PIPE_SHADER_FRAGMENT);
312
313
for (unsigned i = 0; i < v->shader->nir->info.num_ssbos; i++) {
314
struct fd6_image img;
315
translate_buf(&img, &bufso->sb[i]);
316
emit_image_ssbo(state, &img);
317
}
318
319
for (unsigned i = 0; i < v->shader->nir->info.num_images; i++) {
320
struct fd6_image img;
321
translate_image(&img, &imgso->si[i]);
322
emit_image_ssbo(state, &img);
323
}
324
325
return state;
326
}
327
328
static void
329
fd6_set_shader_images(struct pipe_context *pctx, enum pipe_shader_type shader,
330
unsigned start, unsigned count,
331
unsigned unbind_num_trailing_slots,
332
const struct pipe_image_view *images) in_dt
333
{
334
struct fd_context *ctx = fd_context(pctx);
335
struct fd_shaderimg_stateobj *so = &ctx->shaderimg[shader];
336
337
fd_set_shader_images(pctx, shader, start, count, unbind_num_trailing_slots,
338
images);
339
340
if (!images)
341
return;
342
343
for (unsigned i = 0; i < count; i++) {
344
unsigned n = i + start;
345
struct pipe_image_view *buf = &so->si[n];
346
347
if (!buf->resource)
348
continue;
349
350
fd6_validate_format(ctx, fd_resource(buf->resource), buf->format);
351
}
352
}
353
354
void
355
fd6_image_init(struct pipe_context *pctx)
356
{
357
pctx->set_shader_images = fd6_set_shader_images;
358
}
359
360