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