1 /*
2  * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
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 <robclark@freedesktop.org>
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 #define FDL6_TEX_CONST_DWORDS 16
39 
40 struct fd6_image {
41    struct pipe_resource *prsc;
42    enum pipe_format pfmt;
43    enum a6xx_tex_type type;
44    bool srgb;
45    uint32_t cpp;
46    uint32_t level;
47    uint32_t width;
48    uint32_t height;
49    uint32_t depth;
50    uint32_t pitch;
51    uint32_t array_pitch;
52    struct fd_bo *bo;
53    uint32_t ubwc_offset;
54    uint32_t offset;
55    bool buffer;
56 };
57 
58 static void
translate_image(struct fd6_image * img,const struct pipe_image_view * pimg)59 translate_image(struct fd6_image *img, const struct pipe_image_view *pimg)
60 {
61    enum pipe_format format = pimg->format;
62    struct pipe_resource *prsc = pimg->resource;
63    struct fd_resource *rsc = fd_resource(prsc);
64 
65    if (!prsc) {
66       memset(img, 0, sizeof(*img));
67       return;
68    }
69 
70    img->prsc = prsc;
71    img->pfmt = format;
72    img->type = fd6_tex_type(prsc->target);
73    img->srgb = util_format_is_srgb(format);
74    img->cpp = rsc->layout.cpp;
75    img->bo = rsc->bo;
76 
77    /* Treat cube textures as 2d-array: */
78    if (img->type == A6XX_TEX_CUBE)
79       img->type = A6XX_TEX_2D;
80 
81    if (prsc->target == PIPE_BUFFER) {
82       img->buffer = true;
83       img->ubwc_offset = 0; /* not valid for buffers */
84       img->offset = pimg->u.buf.offset;
85       img->pitch = 0;
86       img->array_pitch = 0;
87 
88       /* size is encoded with low 15b in WIDTH and high bits in
89        * HEIGHT, in units of elements:
90        */
91       unsigned sz = pimg->u.buf.size / util_format_get_blocksize(format);
92       img->width = sz & MASK(15);
93       img->height = sz >> 15;
94       img->depth = 0;
95       img->level = 0;
96    } else {
97       img->buffer = false;
98 
99       unsigned lvl = pimg->u.tex.level;
100       unsigned layers = pimg->u.tex.last_layer - pimg->u.tex.first_layer + 1;
101 
102       img->ubwc_offset =
103          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 = fd_resource_pitch(rsc, lvl);
106 
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;
112          img->depth = 1;
113          break;
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.
121          img->depth = layers;
122          break;
123       case PIPE_TEXTURE_3D:
124          img->array_pitch = fd_resource_slice(rsc, lvl)->size0;
125          img->depth = u_minify(prsc->depth0, lvl);
126          break;
127       default:
128          break;
129       }
130 
131       img->level = lvl;
132       img->width = u_minify(prsc->width0, lvl);
133       img->height = u_minify(prsc->height0, lvl);
134    }
135 }
136 
137 static void
translate_buf(struct fd6_image * img,const struct pipe_shader_buffer * pimg)138 translate_buf(struct fd6_image *img, const struct pipe_shader_buffer *pimg)
139 {
140    struct pipe_resource *prsc = pimg->buffer;
141    struct fd_resource *rsc = fd_resource(prsc);
142 
143    if (!prsc) {
144       memset(img, 0, sizeof(*img));
145       return;
146    }
147 
148    const struct fd_dev_info *dev_info = fd_screen(prsc->screen)->info;
149    enum pipe_format format = dev_info->a6xx.storage_16bit
150                                 ? PIPE_FORMAT_R16_UINT
151                                 : PIPE_FORMAT_R32_UINT;
152 
153    img->prsc = prsc;
154    img->pfmt = format;
155    img->type = fd6_tex_type(prsc->target);
156    img->srgb = util_format_is_srgb(format);
157    img->cpp = rsc->layout.cpp;
158    img->bo = rsc->bo;
159    img->buffer = true;
160 
161    img->ubwc_offset = 0; /* not valid for buffers */
162    img->offset = pimg->buffer_offset;
163    img->pitch = 0;
164    img->array_pitch = 0;
165    img->level = 0;
166 
167    /* size is encoded with low 15b in WIDTH and high bits in HEIGHT,
168     * in units of elements:
169     */
170    unsigned sz = pimg->buffer_size / (dev_info->a6xx.storage_16bit ? 2 : 4);
171    img->width = sz & MASK(15);
172    img->height = sz >> 15;
173    img->depth = 0;
174 }
175 
176 static void
emit_image_tex(struct fd_ringbuffer * ring,struct fd6_image * img)177 emit_image_tex(struct fd_ringbuffer *ring, struct fd6_image *img)
178 {
179    if (!img->prsc) {
180       for (int i = 0; i < FDL6_TEX_CONST_DWORDS; i++)
181          OUT_RING(ring, 0);
182       return;
183    }
184 
185    struct fd_resource *rsc = fd_resource(img->prsc);
186    bool ubwc_enabled = fd_resource_ubwc_enabled(rsc, img->level);
187 
188    OUT_RING(ring,
189             fd6_tex_const_0(img->prsc, img->level, img->pfmt, PIPE_SWIZZLE_X,
190                             PIPE_SWIZZLE_Y, PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W));
191    OUT_RING(ring, A6XX_TEX_CONST_1_WIDTH(img->width) |
192                      A6XX_TEX_CONST_1_HEIGHT(img->height));
193    OUT_RING(ring,
194             COND(img->buffer, A6XX_TEX_CONST_2_UNK4 | A6XX_TEX_CONST_2_UNK31) |
195                A6XX_TEX_CONST_2_TYPE(img->type) |
196                A6XX_TEX_CONST_2_PITCH(img->pitch));
197    OUT_RING(ring, A6XX_TEX_CONST_3_ARRAY_PITCH(img->array_pitch) |
198                      COND(ubwc_enabled, A6XX_TEX_CONST_3_FLAG) |
199                      COND(rsc->layout.tile_all, A6XX_TEX_CONST_3_TILE_ALL));
200    if (img->bo) {
201       OUT_RELOC(ring, img->bo, img->offset,
202                 (uint64_t)A6XX_TEX_CONST_5_DEPTH(img->depth) << 32, 0);
203    } else {
204       OUT_RING(ring, 0x00000000);
205       OUT_RING(ring, A6XX_TEX_CONST_5_DEPTH(img->depth));
206    }
207 
208    OUT_RING(ring, 0x00000000); /* texconst6 */
209 
210    if (ubwc_enabled) {
211       uint32_t block_width, block_height;
212       fdl6_get_ubwc_blockwidth(&rsc->layout, &block_width, &block_height);
213 
214       OUT_RELOC(ring, rsc->bo, img->ubwc_offset, 0, 0);
215       OUT_RING(ring, A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(
216                         rsc->layout.ubwc_layer_size >> 2));
217       OUT_RING(ring, A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(
218                         fdl_ubwc_pitch(&rsc->layout, img->level)) |
219                         A6XX_TEX_CONST_10_FLAG_BUFFER_LOGW(util_logbase2_ceil(
220                            DIV_ROUND_UP(img->width, block_width))) |
221                         A6XX_TEX_CONST_10_FLAG_BUFFER_LOGH(util_logbase2_ceil(
222                            DIV_ROUND_UP(img->height, block_height))));
223    } else {
224       OUT_RING(ring, 0x00000000); /* texconst7 */
225       OUT_RING(ring, 0x00000000); /* texconst8 */
226       OUT_RING(ring, 0x00000000); /* texconst9 */
227       OUT_RING(ring, 0x00000000); /* texconst10 */
228    }
229 
230    OUT_RING(ring, 0x00000000); /* texconst11 */
231    OUT_RING(ring, 0x00000000); /* texconst12 */
232    OUT_RING(ring, 0x00000000); /* texconst13 */
233    OUT_RING(ring, 0x00000000); /* texconst14 */
234    OUT_RING(ring, 0x00000000); /* texconst15 */
235 }
236 
237 void
fd6_emit_image_tex(struct fd_ringbuffer * ring,const struct pipe_image_view * pimg)238 fd6_emit_image_tex(struct fd_ringbuffer *ring,
239                    const struct pipe_image_view *pimg)
240 {
241    struct fd6_image img;
242    translate_image(&img, pimg);
243    emit_image_tex(ring, &img);
244 }
245 
246 void
fd6_emit_ssbo_tex(struct fd_ringbuffer * ring,const struct pipe_shader_buffer * pbuf)247 fd6_emit_ssbo_tex(struct fd_ringbuffer *ring,
248                   const struct pipe_shader_buffer *pbuf)
249 {
250    struct fd6_image img;
251    translate_buf(&img, pbuf);
252    emit_image_tex(ring, &img);
253 }
254 
255 static void
emit_image_ssbo(struct fd_ringbuffer * ring,struct fd6_image * img)256 emit_image_ssbo(struct fd_ringbuffer *ring, struct fd6_image *img)
257 {
258    /* If the SSBO isn't present (becasue gallium doesn't pack atomic
259     * counters), zero-fill the slot.
260     */
261    if (!img->prsc) {
262       for (int i = 0; i < 16; i++)
263          OUT_RING(ring, 0);
264       return;
265    }
266 
267    struct fd_resource *rsc = fd_resource(img->prsc);
268    enum a6xx_tile_mode tile_mode = fd_resource_tile_mode(img->prsc, img->level);
269    bool ubwc_enabled = fd_resource_ubwc_enabled(rsc, img->level);
270 
271    OUT_RING(ring, A6XX_IBO_0_FMT(fd6_texture_format(img->pfmt, rsc->layout.tile_mode)) |
272                      A6XX_IBO_0_TILE_MODE(tile_mode));
273    OUT_RING(ring,
274             A6XX_IBO_1_WIDTH(img->width) | A6XX_IBO_1_HEIGHT(img->height));
275    OUT_RING(ring, A6XX_IBO_2_PITCH(img->pitch) |
276                      COND(img->buffer, A6XX_IBO_2_UNK4 | A6XX_IBO_2_UNK31) |
277                      A6XX_IBO_2_TYPE(img->type));
278    OUT_RING(ring, A6XX_IBO_3_ARRAY_PITCH(img->array_pitch) |
279                      COND(ubwc_enabled, A6XX_IBO_3_FLAG | A6XX_IBO_3_UNK27));
280    if (img->bo) {
281       OUT_RELOC(ring, img->bo, img->offset,
282                 (uint64_t)A6XX_IBO_5_DEPTH(img->depth) << 32, 0);
283    } else {
284       OUT_RING(ring, 0x00000000);
285       OUT_RING(ring, A6XX_IBO_5_DEPTH(img->depth));
286    }
287    OUT_RING(ring, 0x00000000);
288 
289    if (ubwc_enabled) {
290       OUT_RELOC(ring, rsc->bo, img->ubwc_offset, 0, 0);
291       OUT_RING(ring, A6XX_IBO_9_FLAG_BUFFER_ARRAY_PITCH(
292                         rsc->layout.ubwc_layer_size >> 2));
293       OUT_RING(ring, A6XX_IBO_10_FLAG_BUFFER_PITCH(
294                         fdl_ubwc_pitch(&rsc->layout, img->level)));
295    } else {
296       OUT_RING(ring, 0x00000000);
297       OUT_RING(ring, 0x00000000);
298       OUT_RING(ring, 0x00000000);
299       OUT_RING(ring, 0x00000000);
300    }
301 
302    OUT_RING(ring, 0x00000000);
303    OUT_RING(ring, 0x00000000);
304    OUT_RING(ring, 0x00000000);
305    OUT_RING(ring, 0x00000000);
306    OUT_RING(ring, 0x00000000);
307 }
308 
309 /* Build combined image/SSBO "IBO" state, returns ownership of state reference */
310 struct fd_ringbuffer *
fd6_build_ibo_state(struct fd_context * ctx,const struct ir3_shader_variant * v,enum pipe_shader_type shader)311 fd6_build_ibo_state(struct fd_context *ctx, const struct ir3_shader_variant *v,
312                     enum pipe_shader_type shader)
313 {
314    struct fd_shaderbuf_stateobj *bufso = &ctx->shaderbuf[shader];
315    struct fd_shaderimg_stateobj *imgso = &ctx->shaderimg[shader];
316 
317    struct fd_ringbuffer *state = fd_submit_new_ringbuffer(
318       ctx->batch->submit,
319       (v->shader->nir->info.num_ssbos + v->shader->nir->info.num_images) * 16 *
320          4,
321       FD_RINGBUFFER_STREAMING);
322 
323    assert(shader == PIPE_SHADER_COMPUTE || shader == PIPE_SHADER_FRAGMENT);
324 
325    for (unsigned i = 0; i < v->shader->nir->info.num_ssbos; i++) {
326       struct fd6_image img;
327       translate_buf(&img, &bufso->sb[i]);
328       emit_image_ssbo(state, &img);
329    }
330 
331    for (unsigned i = 0; i < v->shader->nir->info.num_images; i++) {
332       struct fd6_image img;
333       translate_image(&img, &imgso->si[i]);
334       emit_image_ssbo(state, &img);
335    }
336 
337    return state;
338 }
339 
340 static void
fd6_set_shader_images(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned count,unsigned unbind_num_trailing_slots,const struct pipe_image_view * images)341 fd6_set_shader_images(struct pipe_context *pctx, enum pipe_shader_type shader,
342                       unsigned start, unsigned count,
343                       unsigned unbind_num_trailing_slots,
344                       const struct pipe_image_view *images) in_dt
345 {
346    struct fd_context *ctx = fd_context(pctx);
347    struct fd_shaderimg_stateobj *so = &ctx->shaderimg[shader];
348 
349    fd_set_shader_images(pctx, shader, start, count, unbind_num_trailing_slots,
350                         images);
351 
352    if (!images)
353       return;
354 
355    for (unsigned i = 0; i < count; i++) {
356       unsigned n = i + start;
357       struct pipe_image_view *buf = &so->si[n];
358 
359       if (!buf->resource)
360          continue;
361 
362       fd6_validate_format(ctx, fd_resource(buf->resource), buf->format);
363    }
364 }
365 
366 void
fd6_image_init(struct pipe_context * pctx)367 fd6_image_init(struct pipe_context *pctx)
368 {
369    pctx->set_shader_images = fd6_set_shader_images;
370 }
371