2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
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:
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
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
25 * Rob Clark <robclark@freedesktop.org>
28 #include "pipe/p_state.h"
30 #include "freedreno_resource.h"
31 #include "freedreno_state.h"
33 #include "fd6_image.h"
34 #include "fd6_format.h"
35 #include "fd6_resource.h"
36 #include "fd6_texture.h"
39 struct pipe_resource
*prsc
;
40 enum pipe_format pfmt
;
42 enum a6xx_tex_fetchsize fetchsize
;
43 enum a6xx_tex_type type
;
58 static void translate_image(struct fd6_image
*img
, const struct pipe_image_view
*pimg
)
60 enum pipe_format format
= pimg
->format
;
61 struct pipe_resource
*prsc
= pimg
->resource
;
62 struct fd_resource
*rsc
= fd_resource(prsc
);
65 memset(img
, 0, sizeof(*img
));
71 img
->fmt
= fd6_pipe2tex(format
);
72 img
->fetchsize
= fd6_pipe2fetchsize(format
);
73 img
->type
= fd6_tex_type(prsc
->target
);
74 img
->srgb
= util_format_is_srgb(format
);
75 img
->cpp
= rsc
->layout
.cpp
;
78 /* Treat cube textures as 2d-array: */
79 if (img
->type
== A6XX_TEX_CUBE
)
80 img
->type
= A6XX_TEX_2D
;
82 if (prsc
->target
== PIPE_BUFFER
) {
84 img
->ubwc_offset
= 0; /* not valid for buffers */
85 img
->offset
= pimg
->u
.buf
.offset
;
89 /* size is encoded with low 15b in WIDTH and high bits in
90 * HEIGHT, in units of elements:
92 unsigned sz
= prsc
->width0
;
93 img
->width
= sz
& MASK(15);
94 img
->height
= sz
>> 15;
99 unsigned lvl
= pimg
->u
.tex
.level
;
100 struct fdl_slice
*slice
= fd_resource_slice(rsc
, lvl
);
101 unsigned layers
= pimg
->u
.tex
.last_layer
- pimg
->u
.tex
.first_layer
+ 1;
103 img
->ubwc_offset
= fd_resource_ubwc_offset(rsc
, lvl
, pimg
->u
.tex
.first_layer
);
104 img
->offset
= fd_resource_offset(rsc
, lvl
, pimg
->u
.tex
.first_layer
);
105 img
->pitch
= slice
->pitch
;
107 switch (prsc
->target
) {
108 case PIPE_TEXTURE_RECT
:
109 case PIPE_TEXTURE_1D
:
110 case PIPE_TEXTURE_2D
:
111 img
->array_pitch
= rsc
->layout
.layer_size
;
114 case PIPE_TEXTURE_1D_ARRAY
:
115 case PIPE_TEXTURE_2D_ARRAY
:
116 case PIPE_TEXTURE_CUBE
:
117 case PIPE_TEXTURE_CUBE_ARRAY
:
118 img
->array_pitch
= rsc
->layout
.layer_size
;
119 // TODO the CUBE/CUBE_ARRAY might need to be layers/6 for tex state,
120 // but empirically for ibo state it shouldn't be divided.
123 case PIPE_TEXTURE_3D
:
124 img
->array_pitch
= slice
->size0
;
125 img
->depth
= u_minify(prsc
->depth0
, lvl
);
132 img
->width
= u_minify(prsc
->width0
, lvl
);
133 img
->height
= u_minify(prsc
->height0
, lvl
);
137 static void translate_buf(struct fd6_image
*img
, const struct pipe_shader_buffer
*pimg
)
139 enum pipe_format format
= PIPE_FORMAT_R32_UINT
;
140 struct pipe_resource
*prsc
= pimg
->buffer
;
141 struct fd_resource
*rsc
= fd_resource(prsc
);
144 memset(img
, 0, sizeof(*img
));
150 img
->fmt
= fd6_pipe2tex(format
);
151 img
->fetchsize
= fd6_pipe2fetchsize(format
);
152 img
->type
= fd6_tex_type(prsc
->target
);
153 img
->srgb
= util_format_is_srgb(format
);
154 img
->cpp
= rsc
->layout
.cpp
;
158 img
->ubwc_offset
= 0; /* not valid for buffers */
159 img
->offset
= pimg
->buffer_offset
;
161 img
->array_pitch
= 0;
163 /* size is encoded with low 15b in WIDTH and high bits in HEIGHT,
164 * in units of elements:
166 unsigned sz
= pimg
->buffer_size
/ 4;
167 img
->width
= sz
& MASK(15);
168 img
->height
= sz
>> 15;
172 static void emit_image_tex(struct fd_ringbuffer
*ring
, struct fd6_image
*img
)
174 struct fd_resource
*rsc
= fd_resource(img
->prsc
);
175 bool ubwc_enabled
= fd_resource_ubwc_enabled(rsc
, img
->level
);
177 OUT_RING(ring
, fd6_tex_const_0(img
->prsc
, img
->level
, img
->pfmt
,
178 PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
,
179 PIPE_SWIZZLE_Z
, PIPE_SWIZZLE_W
));
180 OUT_RING(ring
, A6XX_TEX_CONST_1_WIDTH(img
->width
) |
181 A6XX_TEX_CONST_1_HEIGHT(img
->height
));
182 OUT_RING(ring
, A6XX_TEX_CONST_2_FETCHSIZE(img
->fetchsize
) |
183 COND(img
->buffer
, A6XX_TEX_CONST_2_UNK4
| A6XX_TEX_CONST_2_UNK31
) |
184 A6XX_TEX_CONST_2_TYPE(img
->type
) |
185 A6XX_TEX_CONST_2_PITCH(img
->pitch
));
186 OUT_RING(ring
, A6XX_TEX_CONST_3_ARRAY_PITCH(img
->array_pitch
) |
187 COND(ubwc_enabled
, A6XX_TEX_CONST_3_FLAG
| A6XX_TEX_CONST_3_TILE_ALL
));
189 OUT_RELOC(ring
, img
->bo
, img
->offset
,
190 (uint64_t)A6XX_TEX_CONST_5_DEPTH(img
->depth
) << 32, 0);
192 OUT_RING(ring
, 0x00000000);
193 OUT_RING(ring
, A6XX_TEX_CONST_5_DEPTH(img
->depth
));
196 OUT_RING(ring
, 0x00000000); /* texconst6 */
199 struct fdl_slice
*ubwc_slice
= &rsc
->layout
.ubwc_slices
[img
->level
];
201 uint32_t block_width
, block_height
;
202 fdl6_get_ubwc_blockwidth(&rsc
->layout
, &block_width
, &block_height
);
204 OUT_RELOC(ring
, rsc
->bo
, img
->ubwc_offset
, 0, 0);
205 OUT_RING(ring
, A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(rsc
->layout
.ubwc_layer_size
>> 2));
207 A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(ubwc_slice
->pitch
) |
208 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGW(util_logbase2_ceil(DIV_ROUND_UP(img
->width
, block_width
))) |
209 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGH(util_logbase2_ceil(DIV_ROUND_UP(img
->height
, block_height
))));
211 OUT_RING(ring
, 0x00000000); /* texconst7 */
212 OUT_RING(ring
, 0x00000000); /* texconst8 */
213 OUT_RING(ring
, 0x00000000); /* texconst9 */
214 OUT_RING(ring
, 0x00000000); /* texconst10 */
217 OUT_RING(ring
, 0x00000000); /* texconst11 */
218 OUT_RING(ring
, 0x00000000); /* texconst12 */
219 OUT_RING(ring
, 0x00000000); /* texconst13 */
220 OUT_RING(ring
, 0x00000000); /* texconst14 */
221 OUT_RING(ring
, 0x00000000); /* texconst15 */
225 fd6_emit_image_tex(struct fd_ringbuffer
*ring
, const struct pipe_image_view
*pimg
)
227 struct fd6_image img
;
228 translate_image(&img
, pimg
);
229 emit_image_tex(ring
, &img
);
233 fd6_emit_ssbo_tex(struct fd_ringbuffer
*ring
, const struct pipe_shader_buffer
*pbuf
)
235 struct fd6_image img
;
236 translate_buf(&img
, pbuf
);
237 emit_image_tex(ring
, &img
);
240 static void emit_image_ssbo(struct fd_ringbuffer
*ring
, struct fd6_image
*img
)
242 /* If the SSBO isn't present (becasue gallium doesn't pack atomic
243 * counters), zero-fill the slot.
246 for (int i
= 0; i
< 16; i
++)
251 struct fd_resource
*rsc
= fd_resource(img
->prsc
);
252 enum a6xx_tile_mode tile_mode
= fd_resource_tile_mode(img
->prsc
, img
->level
);
253 bool ubwc_enabled
= fd_resource_ubwc_enabled(rsc
, img
->level
);
255 OUT_RING(ring
, A6XX_IBO_0_FMT(img
->fmt
) |
256 A6XX_IBO_0_TILE_MODE(tile_mode
));
257 OUT_RING(ring
, A6XX_IBO_1_WIDTH(img
->width
) |
258 A6XX_IBO_1_HEIGHT(img
->height
));
259 OUT_RING(ring
, A6XX_IBO_2_PITCH(img
->pitch
) |
260 COND(img
->buffer
, A6XX_IBO_2_UNK4
| A6XX_IBO_2_UNK31
) |
261 A6XX_IBO_2_TYPE(img
->type
));
262 OUT_RING(ring
, A6XX_IBO_3_ARRAY_PITCH(img
->array_pitch
) |
263 COND(ubwc_enabled
, A6XX_IBO_3_FLAG
| A6XX_IBO_3_UNK27
));
265 OUT_RELOCW(ring
, img
->bo
, img
->offset
,
266 (uint64_t)A6XX_IBO_5_DEPTH(img
->depth
) << 32, 0);
268 OUT_RING(ring
, 0x00000000);
269 OUT_RING(ring
, A6XX_IBO_5_DEPTH(img
->depth
));
271 OUT_RING(ring
, 0x00000000);
274 struct fdl_slice
*ubwc_slice
= &rsc
->layout
.ubwc_slices
[img
->level
];
275 OUT_RELOCW(ring
, rsc
->bo
, img
->ubwc_offset
, 0, 0);
276 OUT_RING(ring
, A6XX_IBO_9_FLAG_BUFFER_ARRAY_PITCH(rsc
->layout
.ubwc_layer_size
>> 2));
277 OUT_RING(ring
, A6XX_IBO_10_FLAG_BUFFER_PITCH(ubwc_slice
->pitch
));
279 OUT_RING(ring
, 0x00000000);
280 OUT_RING(ring
, 0x00000000);
281 OUT_RING(ring
, 0x00000000);
282 OUT_RING(ring
, 0x00000000);
285 OUT_RING(ring
, 0x00000000);
286 OUT_RING(ring
, 0x00000000);
287 OUT_RING(ring
, 0x00000000);
288 OUT_RING(ring
, 0x00000000);
289 OUT_RING(ring
, 0x00000000);
292 /* Build combined image/SSBO "IBO" state, returns ownership of state reference */
293 struct fd_ringbuffer
*
294 fd6_build_ibo_state(struct fd_context
*ctx
, const struct ir3_shader_variant
*v
,
295 enum pipe_shader_type shader
)
297 struct fd_shaderbuf_stateobj
*bufso
= &ctx
->shaderbuf
[shader
];
298 struct fd_shaderimg_stateobj
*imgso
= &ctx
->shaderimg
[shader
];
300 struct fd_ringbuffer
*state
=
301 fd_submit_new_ringbuffer(ctx
->batch
->submit
,
302 (v
->shader
->nir
->info
.num_ssbos
+
303 v
->shader
->nir
->info
.num_images
) * 16 * 4,
304 FD_RINGBUFFER_STREAMING
);
306 assert(shader
== PIPE_SHADER_COMPUTE
|| shader
== PIPE_SHADER_FRAGMENT
);
308 for (unsigned i
= 0; i
< v
->shader
->nir
->info
.num_ssbos
; i
++) {
309 struct fd6_image img
;
310 translate_buf(&img
, &bufso
->sb
[i
]);
311 emit_image_ssbo(state
, &img
);
314 for (unsigned i
= 0; i
< v
->shader
->nir
->info
.num_images
; i
++) {
315 struct fd6_image img
;
316 translate_image(&img
, &imgso
->si
[i
]);
317 emit_image_ssbo(state
, &img
);
323 static void fd6_set_shader_images(struct pipe_context
*pctx
,
324 enum pipe_shader_type shader
,
325 unsigned start
, unsigned count
,
326 const struct pipe_image_view
*images
)
328 struct fd_context
*ctx
= fd_context(pctx
);
329 struct fd_shaderimg_stateobj
*so
= &ctx
->shaderimg
[shader
];
331 fd_set_shader_images(pctx
, shader
, start
, count
, images
);
336 for (unsigned i
= 0; i
< count
; i
++) {
337 unsigned n
= i
+ start
;
338 struct pipe_image_view
*buf
= &so
->si
[n
];
343 fd6_validate_format(ctx
, fd_resource(buf
->resource
), buf
->format
);
348 fd6_image_init(struct pipe_context
*pctx
)
350 pctx
->set_shader_images
= fd6_set_shader_images
;