• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_helpers.h"
32 #include "util/format/u_format.h"
33 #include "util/u_viewport.h"
34 
35 #include "freedreno_log.h"
36 #include "freedreno_resource.h"
37 #include "freedreno_state.h"
38 #include "freedreno_query_hw.h"
39 #include "common/freedreno_guardband.h"
40 
41 #include "fd6_emit.h"
42 #include "fd6_blend.h"
43 #include "fd6_const.h"
44 #include "fd6_context.h"
45 #include "fd6_image.h"
46 #include "fd6_pack.h"
47 #include "fd6_program.h"
48 #include "fd6_rasterizer.h"
49 #include "fd6_texture.h"
50 #include "fd6_format.h"
51 #include "fd6_zsa.h"
52 
53 /* Border color layout is diff from a4xx/a5xx.. if it turns out to be
54  * the same as a6xx then move this somewhere common ;-)
55  *
56  * Entry layout looks like (total size, 0x60 bytes):
57  */
58 
59 struct PACKED bcolor_entry {
60 	uint32_t fp32[4];
61 	uint16_t ui16[4];
62 	int16_t  si16[4];
63 	uint16_t fp16[4];
64 	uint16_t rgb565;
65 	uint16_t rgb5a1;
66 	uint16_t rgba4;
67 	uint8_t __pad0[2];
68 	uint8_t  ui8[4];
69 	int8_t   si8[4];
70 	uint32_t rgb10a2;
71 	uint32_t z24; /* also s8? */
72 	uint16_t srgb[4];      /* appears to duplicate fp16[], but clamped, used for srgb */
73 	uint8_t  __pad1[56];
74 };
75 
76 #define FD6_BORDER_COLOR_SIZE        sizeof(struct bcolor_entry)
77 #define FD6_BORDER_COLOR_UPLOAD_SIZE (2 * PIPE_MAX_SAMPLERS * FD6_BORDER_COLOR_SIZE)
78 
79 static void
setup_border_colors(struct fd_texture_stateobj * tex,struct bcolor_entry * entries)80 setup_border_colors(struct fd_texture_stateobj *tex, struct bcolor_entry *entries)
81 {
82 	unsigned i, j;
83 	STATIC_ASSERT(sizeof(struct bcolor_entry) == FD6_BORDER_COLOR_SIZE);
84 
85 	for (i = 0; i < tex->num_samplers; i++) {
86 		struct bcolor_entry *e = &entries[i];
87 		struct pipe_sampler_state *sampler = tex->samplers[i];
88 		union pipe_color_union *bc;
89 
90 		if (!sampler)
91 			continue;
92 
93 		bc = &sampler->border_color;
94 
95 		/*
96 		 * XXX HACK ALERT XXX
97 		 *
98 		 * The border colors need to be swizzled in a particular
99 		 * format-dependent order. Even though samplers don't know about
100 		 * formats, we can assume that with a GL state tracker, there's a
101 		 * 1:1 correspondence between sampler and texture. Take advantage
102 		 * of that knowledge.
103 		 */
104 		if ((i >= tex->num_textures) || !tex->textures[i])
105 			continue;
106 
107 		struct pipe_sampler_view *view = tex->textures[i];
108 		enum pipe_format format = view->format;
109 		const struct util_format_description *desc =
110 				util_format_description(format);
111 
112 		e->rgb565 = 0;
113 		e->rgb5a1 = 0;
114 		e->rgba4 = 0;
115 		e->rgb10a2 = 0;
116 		e->z24 = 0;
117 
118 		unsigned char swiz[4];
119 
120 		fd6_tex_swiz(format, swiz,
121 				view->swizzle_r, view->swizzle_g,
122 				view->swizzle_b, view->swizzle_a);
123 
124 		for (j = 0; j < 4; j++) {
125 			int c = swiz[j];
126 			int cd = c;
127 
128 			/*
129 			 * HACK: for PIPE_FORMAT_X24S8_UINT we end up w/ the
130 			 * stencil border color value in bc->ui[0] but according
131 			 * to desc->swizzle and desc->channel, the .x/.w component
132 			 * is NONE and the stencil value is in the y component.
133 			 * Meanwhile the hardware wants this in the .w component
134 			 * for x24s8 and the .x component for x32_s8x24.
135 			 */
136 			if ((format == PIPE_FORMAT_X24S8_UINT) ||
137 					(format == PIPE_FORMAT_X32_S8X24_UINT)) {
138 				if (j == 0) {
139 					c = 1;
140 					cd = (format == PIPE_FORMAT_X32_S8X24_UINT) ? 0 : 3;
141 				} else {
142 					continue;
143 				}
144 			}
145 
146 			if (c >= 4)
147 				continue;
148 
149 			if (desc->channel[c].pure_integer) {
150 				uint16_t clamped;
151 				switch (desc->channel[c].size) {
152 				case 2:
153 					assert(desc->channel[c].type == UTIL_FORMAT_TYPE_UNSIGNED);
154 					clamped = CLAMP(bc->ui[j], 0, 0x3);
155 					break;
156 				case 8:
157 					if (desc->channel[c].type == UTIL_FORMAT_TYPE_SIGNED)
158 						clamped = CLAMP(bc->i[j], -128, 127);
159 					else
160 						clamped = CLAMP(bc->ui[j], 0, 255);
161 					break;
162 				case 10:
163 					assert(desc->channel[c].type == UTIL_FORMAT_TYPE_UNSIGNED);
164 					clamped = CLAMP(bc->ui[j], 0, 0x3ff);
165 					break;
166 				case 16:
167 					if (desc->channel[c].type == UTIL_FORMAT_TYPE_SIGNED)
168 						clamped = CLAMP(bc->i[j], -32768, 32767);
169 					else
170 						clamped = CLAMP(bc->ui[j], 0, 65535);
171 					break;
172 				default:
173 					assert(!"Unexpected bit size");
174 				case 32:
175 					clamped = 0;
176 					break;
177 				}
178 				e->fp32[cd] = bc->ui[j];
179 				e->fp16[cd] = clamped;
180 			} else {
181 				float f = bc->f[j];
182 				float f_u = CLAMP(f, 0, 1);
183 				float f_s = CLAMP(f, -1, 1);
184 
185 				e->fp32[c] = fui(f);
186 				e->fp16[c] = _mesa_float_to_half(f);
187 				e->srgb[c] = _mesa_float_to_half(f_u);
188 				e->ui16[c] = f_u * 0xffff;
189 				e->si16[c] = f_s * 0x7fff;
190 				e->ui8[c]  = f_u * 0xff;
191 				e->si8[c]  = f_s * 0x7f;
192 				if (c == 1)
193 					e->rgb565 |= (int)(f_u * 0x3f) << 5;
194 				else if (c < 3)
195 					e->rgb565 |= (int)(f_u * 0x1f) << (c ? 11 : 0);
196 				if (c == 3)
197 					e->rgb5a1 |= (f_u > 0.5) ? 0x8000 : 0;
198 				else
199 					e->rgb5a1 |= (int)(f_u * 0x1f) << (c * 5);
200 				if (c == 3)
201 					e->rgb10a2 |= (int)(f_u * 0x3) << 30;
202 				else
203 					e->rgb10a2 |= (int)(f_u * 0x3ff) << (c * 10);
204 				e->rgba4 |= (int)(f_u * 0xf) << (c * 4);
205 				if (c == 0)
206 					e->z24 = f_u * 0xffffff;
207 			}
208 		}
209 
210 #ifdef DEBUG
211 		memset(&e->__pad0, 0, sizeof(e->__pad0));
212 		memset(&e->__pad1, 0, sizeof(e->__pad1));
213 #endif
214 	}
215 }
216 
217 static void
emit_border_color(struct fd_context * ctx,struct fd_ringbuffer * ring)218 emit_border_color(struct fd_context *ctx, struct fd_ringbuffer *ring)
219 {
220 	struct fd6_context *fd6_ctx = fd6_context(ctx);
221 	struct bcolor_entry *entries;
222 	unsigned off;
223 	void *ptr;
224 
225 	STATIC_ASSERT(sizeof(struct bcolor_entry) == FD6_BORDER_COLOR_SIZE);
226 
227 	u_upload_alloc(fd6_ctx->border_color_uploader,
228 			0, FD6_BORDER_COLOR_UPLOAD_SIZE,
229 			FD6_BORDER_COLOR_UPLOAD_SIZE, &off,
230 			&fd6_ctx->border_color_buf,
231 			&ptr);
232 
233 	entries = ptr;
234 
235 	setup_border_colors(&ctx->tex[PIPE_SHADER_VERTEX], &entries[0]);
236 	setup_border_colors(&ctx->tex[PIPE_SHADER_FRAGMENT],
237 			&entries[ctx->tex[PIPE_SHADER_VERTEX].num_samplers]);
238 
239 	OUT_PKT4(ring, REG_A6XX_SP_TP_BORDER_COLOR_BASE_ADDR_LO, 2);
240 	OUT_RELOC(ring, fd_resource(fd6_ctx->border_color_buf)->bo, off, 0, 0);
241 
242 	u_upload_unmap(fd6_ctx->border_color_uploader);
243 }
244 
245 static void
fd6_emit_fb_tex(struct fd_ringbuffer * state,struct fd_context * ctx)246 fd6_emit_fb_tex(struct fd_ringbuffer *state, struct fd_context *ctx)
247 {
248 	struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
249 	struct pipe_surface *psurf = pfb->cbufs[0];
250 	struct fd_resource *rsc = fd_resource(psurf->texture);
251 
252 	uint32_t texconst0 = fd6_tex_const_0(psurf->texture, psurf->u.tex.level,
253 			psurf->format, PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y,
254 			PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W);
255 
256 	/* always TILE6_2 mode in GMEM.. which also means no swap: */
257 	texconst0 &= ~(A6XX_TEX_CONST_0_SWAP__MASK | A6XX_TEX_CONST_0_TILE_MODE__MASK);
258 	texconst0 |= A6XX_TEX_CONST_0_TILE_MODE(TILE6_2);
259 
260 	OUT_RING(state, texconst0);
261 	OUT_RING(state, A6XX_TEX_CONST_1_WIDTH(pfb->width) |
262 			A6XX_TEX_CONST_1_HEIGHT(pfb->height));
263 	OUT_RINGP(state, A6XX_TEX_CONST_2_TYPE(A6XX_TEX_2D),
264 			&ctx->batch->fb_read_patches);
265 	OUT_RING(state, A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size));
266 
267 	OUT_RING(state, A6XX_TEX_CONST_4_BASE_LO(ctx->screen->gmem_base));
268 	OUT_RING(state, A6XX_TEX_CONST_5_BASE_HI(ctx->screen->gmem_base >> 32) |
269 			A6XX_TEX_CONST_5_DEPTH(1));
270 	OUT_RING(state, 0);   /* texconst6 */
271 	OUT_RING(state, 0);   /* texconst7 */
272 	OUT_RING(state, 0);   /* texconst8 */
273 	OUT_RING(state, 0);   /* texconst9 */
274 	OUT_RING(state, 0);   /* texconst10 */
275 	OUT_RING(state, 0);   /* texconst11 */
276 	OUT_RING(state, 0);
277 	OUT_RING(state, 0);
278 	OUT_RING(state, 0);
279 	OUT_RING(state, 0);
280 }
281 
282 bool
fd6_emit_textures(struct fd_pipe * pipe,struct fd_ringbuffer * ring,enum pipe_shader_type type,struct fd_texture_stateobj * tex,unsigned bcolor_offset,const struct ir3_shader_variant * v,struct fd_context * ctx)283 fd6_emit_textures(struct fd_pipe *pipe, struct fd_ringbuffer *ring,
284 		enum pipe_shader_type type, struct fd_texture_stateobj *tex,
285 		unsigned bcolor_offset,
286 		/* can be NULL if no image/SSBO/fb state to merge in: */
287 		const struct ir3_shader_variant *v, struct fd_context *ctx)
288 {
289 	bool needs_border = false;
290 	unsigned opcode, tex_samp_reg, tex_const_reg, tex_count_reg;
291 	enum a6xx_state_block sb;
292 
293 	switch (type) {
294 	case PIPE_SHADER_VERTEX:
295 		sb = SB6_VS_TEX;
296 		opcode = CP_LOAD_STATE6_GEOM;
297 		tex_samp_reg = REG_A6XX_SP_VS_TEX_SAMP_LO;
298 		tex_const_reg = REG_A6XX_SP_VS_TEX_CONST_LO;
299 		tex_count_reg = REG_A6XX_SP_VS_TEX_COUNT;
300 		break;
301 	case PIPE_SHADER_TESS_CTRL:
302 		sb = SB6_HS_TEX;
303 		opcode = CP_LOAD_STATE6_GEOM;
304 		tex_samp_reg = REG_A6XX_SP_HS_TEX_SAMP_LO;
305 		tex_const_reg = REG_A6XX_SP_HS_TEX_CONST_LO;
306 		tex_count_reg = REG_A6XX_SP_HS_TEX_COUNT;
307 		break;
308 	case PIPE_SHADER_TESS_EVAL:
309 		sb = SB6_DS_TEX;
310 		opcode = CP_LOAD_STATE6_GEOM;
311 		tex_samp_reg = REG_A6XX_SP_DS_TEX_SAMP_LO;
312 		tex_const_reg = REG_A6XX_SP_DS_TEX_CONST_LO;
313 		tex_count_reg = REG_A6XX_SP_DS_TEX_COUNT;
314 		break;
315 	case PIPE_SHADER_GEOMETRY:
316 		sb = SB6_GS_TEX;
317 		opcode = CP_LOAD_STATE6_GEOM;
318 		tex_samp_reg = REG_A6XX_SP_GS_TEX_SAMP_LO;
319 		tex_const_reg = REG_A6XX_SP_GS_TEX_CONST_LO;
320 		tex_count_reg = REG_A6XX_SP_GS_TEX_COUNT;
321 		break;
322 	case PIPE_SHADER_FRAGMENT:
323 		sb = SB6_FS_TEX;
324 		opcode = CP_LOAD_STATE6_FRAG;
325 		tex_samp_reg = REG_A6XX_SP_FS_TEX_SAMP_LO;
326 		tex_const_reg = REG_A6XX_SP_FS_TEX_CONST_LO;
327 		tex_count_reg = REG_A6XX_SP_FS_TEX_COUNT;
328 		break;
329 	case PIPE_SHADER_COMPUTE:
330 		sb = SB6_CS_TEX;
331 		opcode = CP_LOAD_STATE6_FRAG;
332 		tex_samp_reg = REG_A6XX_SP_CS_TEX_SAMP_LO;
333 		tex_const_reg = REG_A6XX_SP_CS_TEX_CONST_LO;
334 		tex_count_reg = REG_A6XX_SP_CS_TEX_COUNT;
335 		break;
336 	default:
337 		unreachable("bad state block");
338 	}
339 
340 	if (tex->num_samplers > 0) {
341 		struct fd_ringbuffer *state =
342 			fd_ringbuffer_new_object(pipe, tex->num_samplers * 4 * 4);
343 		for (unsigned i = 0; i < tex->num_samplers; i++) {
344 			static const struct fd6_sampler_stateobj dummy_sampler = {};
345 			const struct fd6_sampler_stateobj *sampler = tex->samplers[i] ?
346 				fd6_sampler_stateobj(tex->samplers[i]) : &dummy_sampler;
347 			OUT_RING(state, sampler->texsamp0);
348 			OUT_RING(state, sampler->texsamp1);
349 			OUT_RING(state, sampler->texsamp2 |
350 				A6XX_TEX_SAMP_2_BCOLOR(i + bcolor_offset));
351 			OUT_RING(state, sampler->texsamp3);
352 			needs_border |= sampler->needs_border;
353 		}
354 
355 		/* output sampler state: */
356 		OUT_PKT7(ring, opcode, 3);
357 		OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
358 			CP_LOAD_STATE6_0_STATE_TYPE(ST6_SHADER) |
359 			CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
360 			CP_LOAD_STATE6_0_STATE_BLOCK(sb) |
361 			CP_LOAD_STATE6_0_NUM_UNIT(tex->num_samplers));
362 		OUT_RB(ring, state); /* SRC_ADDR_LO/HI */
363 
364 		OUT_PKT4(ring, tex_samp_reg, 2);
365 		OUT_RB(ring, state); /* SRC_ADDR_LO/HI */
366 
367 		fd_ringbuffer_del(state);
368 	}
369 
370 	unsigned num_merged_textures = tex->num_textures;
371 	unsigned num_textures = tex->num_textures;
372 	if (v) {
373 		num_merged_textures += v->image_mapping.num_tex;
374 
375 		if (v->fb_read)
376 			num_merged_textures++;
377 
378 		/* There could be more bound textures than what the shader uses.
379 		 * Which isn't known at shader compile time.  So in the case we
380 		 * are merging tex state, only emit the textures that the shader
381 		 * uses (since the image/SSBO related tex state comes immediately
382 		 * after)
383 		 */
384 		num_textures = v->image_mapping.tex_base;
385 	}
386 
387 	if (num_merged_textures > 0) {
388 		struct fd_ringbuffer *state =
389 			fd_ringbuffer_new_object(pipe, num_merged_textures * 16 * 4);
390 		for (unsigned i = 0; i < num_textures; i++) {
391 			static const struct fd6_pipe_sampler_view dummy_view = {};
392 			const struct fd6_pipe_sampler_view *view = tex->textures[i] ?
393 				fd6_pipe_sampler_view(tex->textures[i]) : &dummy_view;
394 
395 			OUT_RING(state, view->texconst0);
396 			OUT_RING(state, view->texconst1);
397 			OUT_RING(state, view->texconst2);
398 			OUT_RING(state, view->texconst3);
399 
400 			if (view->ptr1) {
401 				OUT_RELOC(state, view->ptr1->bo, view->offset1,
402 					(uint64_t)view->texconst5 << 32, 0);
403 			} else {
404 				OUT_RING(state, 0x00000000);
405 				OUT_RING(state, view->texconst5);
406 			}
407 
408 			OUT_RING(state, view->texconst6);
409 
410 			if (view->ptr2) {
411 				OUT_RELOC(state, view->ptr2->bo, view->offset2, 0, 0);
412 			} else {
413 				OUT_RING(state, 0);
414 				OUT_RING(state, 0);
415 			}
416 
417 			OUT_RING(state, view->texconst9);
418 			OUT_RING(state, view->texconst10);
419 			OUT_RING(state, view->texconst11);
420 			OUT_RING(state, 0);
421 			OUT_RING(state, 0);
422 			OUT_RING(state, 0);
423 			OUT_RING(state, 0);
424 		}
425 
426 		if (v) {
427 			const struct ir3_ibo_mapping *mapping = &v->image_mapping;
428 			struct fd_shaderbuf_stateobj *buf = &ctx->shaderbuf[type];
429 			struct fd_shaderimg_stateobj *img = &ctx->shaderimg[type];
430 
431 			for (unsigned i = 0; i < mapping->num_tex; i++) {
432 				unsigned idx = mapping->tex_to_image[i];
433 				if (idx & IBO_SSBO) {
434 					fd6_emit_ssbo_tex(state, &buf->sb[idx & ~IBO_SSBO]);
435 				} else {
436 					fd6_emit_image_tex(state, &img->si[idx]);
437 				}
438 			}
439 
440 			if (v->fb_read) {
441 				fd6_emit_fb_tex(state, ctx);
442 			}
443 		}
444 
445 		/* emit texture state: */
446 		OUT_PKT7(ring, opcode, 3);
447 		OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
448 			CP_LOAD_STATE6_0_STATE_TYPE(ST6_CONSTANTS) |
449 			CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
450 			CP_LOAD_STATE6_0_STATE_BLOCK(sb) |
451 			CP_LOAD_STATE6_0_NUM_UNIT(num_merged_textures));
452 		OUT_RB(ring, state); /* SRC_ADDR_LO/HI */
453 
454 		OUT_PKT4(ring, tex_const_reg, 2);
455 		OUT_RB(ring, state); /* SRC_ADDR_LO/HI */
456 
457 		fd_ringbuffer_del(state);
458 	}
459 
460 	OUT_PKT4(ring, tex_count_reg, 1);
461 	OUT_RING(ring, num_merged_textures);
462 
463 	return needs_border;
464 }
465 
466 /* Emits combined texture state, which also includes any Image/SSBO
467  * related texture state merged in (because we must have all texture
468  * state for a given stage in a single buffer).  In the fast-path, if
469  * we don't need to merge in any image/ssbo related texture state, we
470  * just use cached texture stateobj.  Otherwise we generate a single-
471  * use stateobj.
472  *
473  * TODO Is there some sane way we can still use cached texture stateobj
474  * with image/ssbo in use?
475  *
476  * returns whether border_color is required:
477  */
478 static bool
fd6_emit_combined_textures(struct fd_ringbuffer * ring,struct fd6_emit * emit,enum pipe_shader_type type,const struct ir3_shader_variant * v)479 fd6_emit_combined_textures(struct fd_ringbuffer *ring, struct fd6_emit *emit,
480 		enum pipe_shader_type type, const struct ir3_shader_variant *v)
481 {
482 	struct fd_context *ctx = emit->ctx;
483 	bool needs_border = false;
484 
485 	static const struct {
486 		enum fd6_state_id state_id;
487 		unsigned enable_mask;
488 	} s[PIPE_SHADER_TYPES] = {
489 		[PIPE_SHADER_VERTEX]    = { FD6_GROUP_VS_TEX, ENABLE_ALL },
490 		[PIPE_SHADER_TESS_CTRL]  = { FD6_GROUP_HS_TEX, ENABLE_ALL },
491 		[PIPE_SHADER_TESS_EVAL]  = { FD6_GROUP_DS_TEX, ENABLE_ALL },
492 		[PIPE_SHADER_GEOMETRY]  = { FD6_GROUP_GS_TEX, ENABLE_ALL },
493 		[PIPE_SHADER_FRAGMENT]  = { FD6_GROUP_FS_TEX, ENABLE_DRAW },
494 	};
495 
496 	debug_assert(s[type].state_id);
497 
498 	if (!v->image_mapping.num_tex && !v->fb_read) {
499 		/* in the fast-path, when we don't have to mix in any image/SSBO
500 		 * related texture state, we can just lookup the stateobj and
501 		 * re-emit that:
502 		 *
503 		 * Also, framebuffer-read is a slow-path because an extra
504 		 * texture needs to be inserted.
505 		 *
506 		 * TODO we can probably simmplify things if we also treated
507 		 * border_color as a slow-path.. this way the tex state key
508 		 * wouldn't depend on bcolor_offset.. but fb_read might rather
509 		 * be *somehow* a fast-path if we eventually used it for PLS.
510 		 * I suppose there would be no harm in just *always* inserting
511 		 * an fb_read texture?
512 		 */
513 		if ((ctx->dirty_shader[type] & FD_DIRTY_SHADER_TEX) &&
514 				ctx->tex[type].num_textures > 0) {
515 			struct fd6_texture_state *tex = fd6_texture_state(ctx,
516 					type, &ctx->tex[type]);
517 
518 			needs_border |= tex->needs_border;
519 
520 			fd6_emit_add_group(emit, tex->stateobj, s[type].state_id,
521 					s[type].enable_mask);
522 		}
523 	} else {
524 		/* In the slow-path, create a one-shot texture state object
525 		 * if either TEX|PROG|SSBO|IMAGE state is dirty:
526 		 */
527 		if ((ctx->dirty_shader[type] &
528 				(FD_DIRTY_SHADER_TEX | FD_DIRTY_SHADER_PROG |
529 				 FD_DIRTY_SHADER_IMAGE | FD_DIRTY_SHADER_SSBO)) ||
530 				v->fb_read) {
531 			struct fd_texture_stateobj *tex = &ctx->tex[type];
532 			struct fd_ringbuffer *stateobj =
533 				fd_submit_new_ringbuffer(ctx->batch->submit,
534 					0x1000, FD_RINGBUFFER_STREAMING);
535 			unsigned bcolor_offset =
536 				fd6_border_color_offset(ctx, type, tex);
537 
538 			needs_border |= fd6_emit_textures(ctx->pipe, stateobj, type, tex,
539 					bcolor_offset, v, ctx);
540 
541 			fd6_emit_take_group(emit, stateobj, s[type].state_id,
542 					s[type].enable_mask);
543 		}
544 	}
545 
546 	return needs_border;
547 }
548 
549 static struct fd_ringbuffer *
build_vbo_state(struct fd6_emit * emit)550 build_vbo_state(struct fd6_emit *emit)
551 {
552 	const struct fd_vertex_state *vtx = emit->vtx;
553 
554 	struct fd_ringbuffer *ring = fd_submit_new_ringbuffer(emit->ctx->batch->submit,
555 			4 * (1 + vtx->vertexbuf.count * 4), FD_RINGBUFFER_STREAMING);
556 
557 	OUT_PKT4(ring, REG_A6XX_VFD_FETCH(0), 4 * vtx->vertexbuf.count);
558 	for (int32_t j = 0; j < vtx->vertexbuf.count; j++) {
559 		const struct pipe_vertex_buffer *vb = &vtx->vertexbuf.vb[j];
560 		struct fd_resource *rsc = fd_resource(vb->buffer.resource);
561 		if (rsc == NULL) {
562 			OUT_RING(ring, 0);
563 			OUT_RING(ring, 0);
564 			OUT_RING(ring, 0);
565 			OUT_RING(ring, 0);
566 		} else {
567 			uint32_t off = vb->buffer_offset;
568 			uint32_t size = fd_bo_size(rsc->bo) - off;
569 
570 			OUT_RELOC(ring, rsc->bo, off, 0, 0);
571 			OUT_RING(ring, size);           /* VFD_FETCH[j].SIZE */
572 			OUT_RING(ring, vb->stride);     /* VFD_FETCH[j].STRIDE */
573 		}
574 	}
575 
576 	return ring;
577 }
578 
579 static enum a6xx_ztest_mode
compute_ztest_mode(struct fd6_emit * emit,bool lrz_valid)580 compute_ztest_mode(struct fd6_emit *emit, bool lrz_valid)
581 {
582 	struct fd_context *ctx =  emit->ctx;
583 	struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
584 	struct fd6_zsa_stateobj *zsa = fd6_zsa_stateobj(ctx->zsa);
585 	const struct ir3_shader_variant *fs = emit->fs;
586 
587 	if (fs->shader->nir->info.fs.early_fragment_tests)
588 		return A6XX_EARLY_Z;
589 
590 	if (fs->no_earlyz || fs->writes_pos || !zsa->base.depth.enabled) {
591 		return A6XX_LATE_Z;
592 	} else if ((fs->has_kill || zsa->alpha_test) &&
593 			(zsa->base.depth.writemask || !pfb->zsbuf)) {
594 		/* Slightly odd, but seems like the hw wants us to select
595 		 * LATE_Z mode if there is no depth buffer + discard.  Either
596 		 * that, or when occlusion query is enabled.  See:
597 		 *
598 		 * dEQP-GLES31.functional.fbo.no_attachments.*
599 		 */
600 		return lrz_valid ? A6XX_EARLY_LRZ_LATE_Z : A6XX_LATE_Z;
601 	} else {
602 		return A6XX_EARLY_Z;
603 	}
604 }
605 
606 /**
607  * Calculate normalized LRZ state based on zsa/prog/blend state, updating
608  * the zsbuf's lrz state as necessary to detect the cases where we need
609  * to invalidate lrz.
610  */
611 static struct fd6_lrz_state
compute_lrz_state(struct fd6_emit * emit,bool binning_pass)612 compute_lrz_state(struct fd6_emit *emit, bool binning_pass)
613 {
614 	struct fd_context *ctx = emit->ctx;
615 	struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
616 	const struct ir3_shader_variant *fs = emit->fs;
617 	struct fd6_lrz_state lrz;
618 
619 	if (!pfb->zsbuf) {
620 		memset(&lrz, 0, sizeof(lrz));
621 		if (!binning_pass) {
622 			lrz.z_mode = compute_ztest_mode(emit, false);
623 		}
624 		return lrz;
625 	}
626 
627 	struct fd6_blend_stateobj *blend = fd6_blend_stateobj(ctx->blend);
628 	struct fd6_zsa_stateobj *zsa = fd6_zsa_stateobj(ctx->zsa);
629 	struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
630 
631 	lrz = zsa->lrz;
632 
633 	/* normalize lrz state: */
634 	if (blend->reads_dest || fs->writes_pos || fs->no_earlyz || fs->has_kill) {
635 		lrz.write = false;
636 		if (binning_pass)
637 			lrz.enable = false;
638 	}
639 
640 	/* if we change depthfunc direction, bail out on using LRZ.  The
641 	 * LRZ buffer encodes a min/max depth value per block, but if
642 	 * we switch from GT/GE <-> LT/LE, those values cannot be
643 	 * interpreted properly.
644 	 */
645 	if (zsa->base.depth.enabled &&
646 			(rsc->lrz_direction != FD_LRZ_UNKNOWN) &&
647 			(rsc->lrz_direction != lrz.direction)) {
648 		rsc->lrz_valid = false;
649 	}
650 
651 	if (zsa->invalidate_lrz || !rsc->lrz_valid) {
652 		rsc->lrz_valid = false;
653 		memset(&lrz, 0, sizeof(lrz));
654 	}
655 
656 	if (fs->no_earlyz || fs->writes_pos) {
657 		lrz.enable = false;
658 		lrz.write = false;
659 		lrz.test = false;
660 	}
661 
662 	if (!binning_pass) {
663 		lrz.z_mode = compute_ztest_mode(emit, rsc->lrz_valid);
664 	}
665 
666 	/* Once we start writing to the real depth buffer, we lock in the
667 	 * direction for LRZ.. if we have to skip a LRZ write for any
668 	 * reason, it is still safe to have LRZ until there is a direction
669 	 * reversal.  Prior to the reversal, since we disabled LRZ writes
670 	 * in the "unsafe" cases, this just means that the LRZ test may
671 	 * not early-discard some things that end up not passing a later
672 	 * test (ie. be overly concervative).  But once you have a reversal
673 	 * of direction, it is possible to increase/decrease the z value
674 	 * to the point where the overly-conservative test is incorrect.
675 	 */
676 	if (zsa->base.depth.writemask) {
677 		rsc->lrz_direction = lrz.direction;
678 	}
679 
680 	return lrz;
681 }
682 
683 static struct fd_ringbuffer *
build_lrz(struct fd6_emit * emit,bool binning_pass)684 build_lrz(struct fd6_emit *emit, bool binning_pass)
685 {
686 	struct fd_context *ctx = emit->ctx;
687 	struct fd6_context *fd6_ctx = fd6_context(ctx);
688 	struct fd6_lrz_state lrz =
689 			compute_lrz_state(emit, binning_pass);
690 
691 	/* If the LRZ state has not changed, we can skip the emit: */
692 	if (!ctx->last.dirty &&
693 			!memcmp(&fd6_ctx->last.lrz[binning_pass], &lrz, sizeof(lrz)))
694 		return NULL;
695 
696 	fd6_ctx->last.lrz[binning_pass] = lrz;
697 
698 	struct fd_ringbuffer *ring = fd_submit_new_ringbuffer(ctx->batch->submit,
699 			8*4, FD_RINGBUFFER_STREAMING);
700 
701 	OUT_REG(ring, A6XX_GRAS_LRZ_CNTL(
702 			.enable        = lrz.enable,
703 			.lrz_write     = lrz.write,
704 			.greater       = lrz.direction == FD_LRZ_GREATER,
705 			.z_test_enable = lrz.test,
706 		));
707 	OUT_REG(ring, A6XX_RB_LRZ_CNTL(
708 			.enable = lrz.enable,
709 		));
710 
711 	OUT_REG(ring, A6XX_RB_DEPTH_PLANE_CNTL(
712 			.z_mode = lrz.z_mode,
713 		));
714 
715 	OUT_REG(ring, A6XX_GRAS_SU_DEPTH_PLANE_CNTL(
716 			.z_mode = lrz.z_mode,
717 		));
718 
719 	return ring;
720 }
721 
722 static void
fd6_emit_streamout(struct fd_ringbuffer * ring,struct fd6_emit * emit,struct ir3_stream_output_info * info)723 fd6_emit_streamout(struct fd_ringbuffer *ring, struct fd6_emit *emit, struct ir3_stream_output_info *info)
724 {
725 	struct fd_context *ctx = emit->ctx;
726 	const struct fd6_program_state *prog = fd6_emit_get_prog(emit);
727 	struct fd_streamout_stateobj *so = &ctx->streamout;
728 
729 	emit->streamout_mask = 0;
730 
731 	for (unsigned i = 0; i < so->num_targets; i++) {
732 		struct pipe_stream_output_target *target = so->targets[i];
733 
734 		if (!target)
735 			continue;
736 
737 		OUT_PKT4(ring, REG_A6XX_VPC_SO_BUFFER_BASE_LO(i), 3);
738 		/* VPC_SO[i].BUFFER_BASE_LO: */
739 		OUT_RELOC(ring, fd_resource(target->buffer)->bo, target->buffer_offset, 0, 0);
740 		OUT_RING(ring, target->buffer_size - target->buffer_offset);
741 
742 		if (so->reset & (1 << i)) {
743 			unsigned offset = (so->offsets[i] * info->stride[i] * 4);
744 			OUT_PKT4(ring, REG_A6XX_VPC_SO_BUFFER_OFFSET(i), 1);
745 			OUT_RING(ring, offset);
746 		} else {
747 			OUT_PKT7(ring, CP_MEM_TO_REG, 3);
748 			OUT_RING(ring, CP_MEM_TO_REG_0_REG(REG_A6XX_VPC_SO_BUFFER_OFFSET(i)) |
749 					CP_MEM_TO_REG_0_SHIFT_BY_2 | CP_MEM_TO_REG_0_UNK31 |
750 					CP_MEM_TO_REG_0_CNT(0));
751 			OUT_RELOC(ring, control_ptr(fd6_context(ctx), flush_base[i].offset));
752 		}
753 
754 		OUT_PKT4(ring, REG_A6XX_VPC_SO_FLUSH_BASE_LO(i), 2);
755 		OUT_RELOC(ring, control_ptr(fd6_context(ctx), flush_base[i]));
756 
757 		so->reset &= ~(1 << i);
758 
759 		emit->streamout_mask |= (1 << i);
760 	}
761 
762 	if (emit->streamout_mask) {
763 		fd6_emit_add_group(emit, prog->streamout_stateobj, FD6_GROUP_SO, ENABLE_ALL);
764 	} else {
765 		/* If we transition from a draw with streamout to one without, turn
766 		 * off streamout.
767 		 */
768 		if (ctx->last.streamout_mask != 0) {
769 			struct fd_ringbuffer *obj = fd_submit_new_ringbuffer(emit->ctx->batch->submit,
770 					5 * 4, FD_RINGBUFFER_STREAMING);
771 
772 			OUT_PKT7(obj, CP_CONTEXT_REG_BUNCH, 4);
773 			OUT_RING(obj, REG_A6XX_VPC_SO_CNTL);
774 			OUT_RING(obj, 0);
775 			OUT_RING(obj, REG_A6XX_VPC_SO_STREAM_CNTL);
776 			OUT_RING(obj, 0);
777 
778 			fd6_emit_take_group(emit, obj, FD6_GROUP_SO, ENABLE_ALL);
779 		}
780 	}
781 
782 	ctx->last.streamout_mask = emit->streamout_mask;
783 }
784 
785 void
fd6_emit_state(struct fd_ringbuffer * ring,struct fd6_emit * emit)786 fd6_emit_state(struct fd_ringbuffer *ring, struct fd6_emit *emit)
787 {
788 	struct fd_context *ctx = emit->ctx;
789 	struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
790 	const struct fd6_program_state *prog = fd6_emit_get_prog(emit);
791 	const struct ir3_shader_variant *vs = emit->vs;
792 	const struct ir3_shader_variant *hs = emit->hs;
793 	const struct ir3_shader_variant *ds = emit->ds;
794 	const struct ir3_shader_variant *gs = emit->gs;
795 	const struct ir3_shader_variant *fs = emit->fs;
796 	const enum fd_dirty_3d_state dirty = emit->dirty;
797 	bool needs_border = false;
798 
799 	emit_marker6(ring, 5);
800 
801 	/* NOTE: we track fb_read differently than _BLEND_ENABLED since
802 	 * we might at some point decide to do sysmem in some cases when
803 	 * blend is enabled:
804 	 */
805 	if (fs->fb_read)
806 		ctx->batch->gmem_reason |= FD_GMEM_FB_READ;
807 
808 	if (emit->dirty & FD_DIRTY_VTXSTATE) {
809 		struct fd6_vertex_stateobj *vtx = fd6_vertex_stateobj(ctx->vtx.vtx);
810 
811 		fd6_emit_add_group(emit, vtx->stateobj, FD6_GROUP_VTXSTATE, ENABLE_ALL);
812 	}
813 
814 	if (emit->dirty & FD_DIRTY_VTXBUF) {
815 		struct fd_ringbuffer *state;
816 
817 		state = build_vbo_state(emit);
818 		fd6_emit_take_group(emit, state, FD6_GROUP_VBO, ENABLE_ALL);
819 	}
820 
821 	if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_RASTERIZER)) {
822 		struct fd_ringbuffer *state =
823 			fd6_zsa_state(ctx,
824 					util_format_is_pure_integer(pipe_surface_format(pfb->cbufs[0])),
825 					fd_depth_clamp_enabled(ctx));
826 
827 		fd6_emit_add_group(emit, state, FD6_GROUP_ZSA, ENABLE_ALL);
828 	}
829 
830 	if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_BLEND | FD_DIRTY_PROG)) {
831 		struct fd_ringbuffer *state;
832 
833 		state = build_lrz(emit, false);
834 		if (state) {
835 			fd6_emit_take_group(emit, state, FD6_GROUP_LRZ, ENABLE_DRAW);
836 		}
837 
838 		state = build_lrz(emit, true);
839 		if (state) {
840 			fd6_emit_take_group(emit, state,
841 					FD6_GROUP_LRZ_BINNING, CP_SET_DRAW_STATE__0_BINNING);
842 		}
843 	}
844 
845 	if (dirty & FD_DIRTY_STENCIL_REF) {
846 		struct pipe_stencil_ref *sr = &ctx->stencil_ref;
847 
848 		OUT_PKT4(ring, REG_A6XX_RB_STENCILREF, 1);
849 		OUT_RING(ring, A6XX_RB_STENCILREF_REF(sr->ref_value[0]) |
850 				A6XX_RB_STENCILREF_BFREF(sr->ref_value[1]));
851 	}
852 
853 	/* NOTE: scissor enabled bit is part of rasterizer state, but
854 	 * fd_rasterizer_state_bind() will mark scissor dirty if needed:
855 	 */
856 	if (dirty & FD_DIRTY_SCISSOR) {
857 		struct fd_ringbuffer *ring = fd_submit_new_ringbuffer(
858 				emit->ctx->batch->submit, 3*4, FD_RINGBUFFER_STREAMING);
859 		struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
860 
861 		OUT_REG(ring,
862 				A6XX_GRAS_SC_SCREEN_SCISSOR_TL(0,
863 					.x = scissor->minx,
864 					.y = scissor->miny
865 				),
866 				A6XX_GRAS_SC_SCREEN_SCISSOR_BR(0,
867 					.x = MAX2(scissor->maxx, 1) - 1,
868 					.y = MAX2(scissor->maxy, 1) - 1
869 				)
870 			);
871 
872 		fd6_emit_take_group(emit, ring, FD6_GROUP_SCISSOR, ENABLE_ALL);
873 
874 		ctx->batch->max_scissor.minx = MIN2(ctx->batch->max_scissor.minx, scissor->minx);
875 		ctx->batch->max_scissor.miny = MIN2(ctx->batch->max_scissor.miny, scissor->miny);
876 		ctx->batch->max_scissor.maxx = MAX2(ctx->batch->max_scissor.maxx, scissor->maxx);
877 		ctx->batch->max_scissor.maxy = MAX2(ctx->batch->max_scissor.maxy, scissor->maxy);
878 	}
879 
880 	if (dirty & FD_DIRTY_VIEWPORT) {
881 		struct pipe_scissor_state *scissor = &ctx->viewport_scissor;
882 
883 		OUT_REG(ring,
884 				A6XX_GRAS_CL_VPORT_XOFFSET(0, ctx->viewport.translate[0]),
885 				A6XX_GRAS_CL_VPORT_XSCALE(0, ctx->viewport.scale[0]),
886 				A6XX_GRAS_CL_VPORT_YOFFSET(0, ctx->viewport.translate[1]),
887 				A6XX_GRAS_CL_VPORT_YSCALE(0, ctx->viewport.scale[1]),
888 				A6XX_GRAS_CL_VPORT_ZOFFSET(0, ctx->viewport.translate[2]),
889 				A6XX_GRAS_CL_VPORT_ZSCALE(0, ctx->viewport.scale[2])
890 			);
891 
892 		OUT_REG(ring,
893 				A6XX_GRAS_SC_VIEWPORT_SCISSOR_TL(0,
894 					.x = scissor->minx,
895 					.y = scissor->miny
896 				),
897 				A6XX_GRAS_SC_VIEWPORT_SCISSOR_BR(0,
898 					.x = MAX2(scissor->maxx, 1) - 1,
899 					.y = MAX2(scissor->maxy, 1) - 1
900 				)
901 			);
902 
903 		unsigned guardband_x =
904 			fd_calc_guardband(ctx->viewport.translate[0], ctx->viewport.scale[0],
905 							  false);
906 		unsigned guardband_y =
907 			fd_calc_guardband(ctx->viewport.translate[1], ctx->viewport.scale[1],
908 							  false);
909 
910 		OUT_REG(ring, A6XX_GRAS_CL_GUARDBAND_CLIP_ADJ(
911 					.horz = guardband_x,
912 					.vert = guardband_y
913 				)
914 			);
915 	}
916 
917 	/* The clamp ranges are only used when the rasterizer wants depth
918 	 * clamping.
919 	 */
920 	if ((dirty & (FD_DIRTY_VIEWPORT | FD_DIRTY_RASTERIZER)) &&
921 			fd_depth_clamp_enabled(ctx)) {
922 		float zmin, zmax;
923 		util_viewport_zmin_zmax(&ctx->viewport, ctx->rasterizer->clip_halfz,
924 				&zmin, &zmax);
925 
926 		OUT_REG(ring,
927 				A6XX_GRAS_CL_Z_CLAMP_MIN(0, zmin),
928 				A6XX_GRAS_CL_Z_CLAMP_MAX(0, zmax));
929 
930 		OUT_REG(ring,
931 				A6XX_RB_Z_CLAMP_MIN(zmin),
932 				A6XX_RB_Z_CLAMP_MAX(zmax));
933 	}
934 
935 	if (dirty & FD_DIRTY_PROG) {
936 		fd6_emit_add_group(emit, prog->config_stateobj, FD6_GROUP_PROG_CONFIG, ENABLE_ALL);
937 		fd6_emit_add_group(emit, prog->stateobj, FD6_GROUP_PROG, ENABLE_DRAW);
938 		fd6_emit_add_group(emit, prog->binning_stateobj,
939 				FD6_GROUP_PROG_BINNING, CP_SET_DRAW_STATE__0_BINNING);
940 
941 		/* emit remaining streaming program state, ie. what depends on
942 		 * other emit state, so cannot be pre-baked.
943 		 */
944 		struct fd_ringbuffer *streaming = fd6_program_interp_state(emit);
945 
946 		fd6_emit_take_group(emit, streaming, FD6_GROUP_PROG_INTERP, ENABLE_DRAW);
947 	}
948 
949 	if (dirty & FD_DIRTY_RASTERIZER) {
950 		struct fd_ringbuffer *stateobj =
951 			fd6_rasterizer_state(ctx, emit->primitive_restart);
952 		fd6_emit_add_group(emit, stateobj,
953 						   FD6_GROUP_RASTERIZER, ENABLE_ALL);
954 	}
955 
956 	if (dirty & (FD_DIRTY_FRAMEBUFFER | FD_DIRTY_RASTERIZER_DISCARD | FD_DIRTY_PROG)) {
957 		struct fd_ringbuffer *ring = fd_submit_new_ringbuffer(
958 				emit->ctx->batch->submit, 5 * 4, FD_RINGBUFFER_STREAMING);
959 
960 		unsigned nr = pfb->nr_cbufs;
961 
962 		if (ctx->rasterizer->rasterizer_discard)
963 			nr = 0;
964 
965 		OUT_PKT4(ring, REG_A6XX_RB_FS_OUTPUT_CNTL0, 2);
966 		OUT_RING(ring, COND(fs->writes_pos, A6XX_RB_FS_OUTPUT_CNTL0_FRAG_WRITES_Z) |
967 				COND(fs->writes_smask && pfb->samples > 1,
968 						A6XX_RB_FS_OUTPUT_CNTL0_FRAG_WRITES_SAMPMASK));
969 		OUT_RING(ring, A6XX_RB_FS_OUTPUT_CNTL1_MRT(nr));
970 
971 		OUT_PKT4(ring, REG_A6XX_SP_FS_OUTPUT_CNTL1, 1);
972 		OUT_RING(ring, A6XX_SP_FS_OUTPUT_CNTL1_MRT(nr));
973 
974 		fd6_emit_take_group(emit, ring, FD6_GROUP_PROG_FB_RAST, ENABLE_DRAW);
975 	}
976 
977 	fd6_emit_consts(emit);
978 
979 	struct ir3_stream_output_info *info = &fd6_last_shader(prog)->shader->stream_output;
980 	if (info->num_outputs)
981 		fd6_emit_streamout(ring, emit, info);
982 
983 	if (dirty & (FD_DIRTY_BLEND | FD_DIRTY_SAMPLE_MASK)) {
984 		struct fd6_blend_variant *blend = fd6_blend_variant(ctx->blend,
985 				pfb->samples, ctx->sample_mask);
986 		fd6_emit_add_group(emit, blend->stateobj, FD6_GROUP_BLEND, ENABLE_DRAW);
987 	}
988 
989 	if (dirty & FD_DIRTY_BLEND_COLOR) {
990 		struct pipe_blend_color *bcolor = &ctx->blend_color;
991 		struct fd_ringbuffer *ring = fd_submit_new_ringbuffer(
992 				emit->ctx->batch->submit, 5*4, FD_RINGBUFFER_STREAMING);
993 
994 		OUT_REG(ring,
995 				A6XX_RB_BLEND_RED_F32(bcolor->color[0]),
996 				A6XX_RB_BLEND_GREEN_F32(bcolor->color[1]),
997 				A6XX_RB_BLEND_BLUE_F32(bcolor->color[2]),
998 				A6XX_RB_BLEND_ALPHA_F32(bcolor->color[3])
999 			);
1000 
1001 		fd6_emit_take_group(emit, ring, FD6_GROUP_BLEND_COLOR, ENABLE_DRAW);
1002 	}
1003 
1004 	needs_border |= fd6_emit_combined_textures(ring, emit, PIPE_SHADER_VERTEX, vs);
1005 	if (hs) {
1006 		needs_border |= fd6_emit_combined_textures(ring, emit, PIPE_SHADER_TESS_CTRL, hs);
1007 		needs_border |= fd6_emit_combined_textures(ring, emit, PIPE_SHADER_TESS_EVAL, ds);
1008 	}
1009 	if (gs) {
1010 		needs_border |= fd6_emit_combined_textures(ring, emit, PIPE_SHADER_GEOMETRY, gs);
1011 	}
1012 	needs_border |= fd6_emit_combined_textures(ring, emit, PIPE_SHADER_FRAGMENT, fs);
1013 
1014 	if (needs_border)
1015 		emit_border_color(ctx, ring);
1016 
1017 	if (hs) {
1018 		debug_assert(ir3_shader_nibo(hs) == 0);
1019 		debug_assert(ir3_shader_nibo(ds) == 0);
1020 	}
1021 	if (gs) {
1022 		debug_assert(ir3_shader_nibo(gs) == 0);
1023 	}
1024 
1025 #define DIRTY_IBO (FD_DIRTY_SHADER_SSBO | FD_DIRTY_SHADER_IMAGE | \
1026 				   FD_DIRTY_SHADER_PROG)
1027 	if (ctx->dirty_shader[PIPE_SHADER_FRAGMENT] & DIRTY_IBO) {
1028 		struct fd_ringbuffer *state =
1029 			fd6_build_ibo_state(ctx, fs, PIPE_SHADER_FRAGMENT);
1030 		struct fd_ringbuffer *obj = fd_submit_new_ringbuffer(
1031 			ctx->batch->submit, 0x100, FD_RINGBUFFER_STREAMING);
1032 
1033 		OUT_PKT7(obj, CP_LOAD_STATE6, 3);
1034 		OUT_RING(obj, CP_LOAD_STATE6_0_DST_OFF(0) |
1035 			CP_LOAD_STATE6_0_STATE_TYPE(ST6_SHADER) |
1036 			CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
1037 			CP_LOAD_STATE6_0_STATE_BLOCK(SB6_IBO) |
1038 			CP_LOAD_STATE6_0_NUM_UNIT(ir3_shader_nibo(fs)));
1039 		OUT_RB(obj, state);
1040 
1041 		OUT_PKT4(obj, REG_A6XX_SP_IBO_LO, 2);
1042 		OUT_RB(obj, state);
1043 
1044 		/* TODO if we used CP_SET_DRAW_STATE for compute shaders, we could
1045 		 * de-duplicate this from program->config_stateobj
1046 		 */
1047 		OUT_PKT4(obj, REG_A6XX_SP_IBO_COUNT, 1);
1048 		OUT_RING(obj, ir3_shader_nibo(fs));
1049 
1050 		fd6_emit_ibo_consts(emit, fs, PIPE_SHADER_FRAGMENT, ring);
1051 
1052 		fd6_emit_take_group(emit, obj, FD6_GROUP_IBO, ENABLE_DRAW);
1053 		fd_ringbuffer_del(state);
1054 	}
1055 
1056 	if (emit->num_groups > 0) {
1057 		OUT_PKT7(ring, CP_SET_DRAW_STATE, 3 * emit->num_groups);
1058 		for (unsigned i = 0; i < emit->num_groups; i++) {
1059 			struct fd6_state_group *g = &emit->groups[i];
1060 			unsigned n = g->stateobj ?
1061 				fd_ringbuffer_size(g->stateobj) / 4 : 0;
1062 
1063 			debug_assert((g->enable_mask & ~ENABLE_ALL) == 0);
1064 
1065 			if (n == 0) {
1066 				OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(0) |
1067 						CP_SET_DRAW_STATE__0_DISABLE |
1068 						g->enable_mask |
1069 						CP_SET_DRAW_STATE__0_GROUP_ID(g->group_id));
1070 				OUT_RING(ring, 0x00000000);
1071 				OUT_RING(ring, 0x00000000);
1072 			} else {
1073 				OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(n) |
1074 						g->enable_mask |
1075 						CP_SET_DRAW_STATE__0_GROUP_ID(g->group_id));
1076 				OUT_RB(ring, g->stateobj);
1077 			}
1078 
1079 			if (g->stateobj)
1080 				fd_ringbuffer_del(g->stateobj);
1081 		}
1082 		emit->num_groups = 0;
1083 	}
1084 }
1085 
1086 void
fd6_emit_cs_state(struct fd_context * ctx,struct fd_ringbuffer * ring,struct ir3_shader_variant * cp)1087 fd6_emit_cs_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
1088 		struct ir3_shader_variant *cp)
1089 {
1090 	enum fd_dirty_shader_state dirty = ctx->dirty_shader[PIPE_SHADER_COMPUTE];
1091 
1092 	if (dirty & (FD_DIRTY_SHADER_TEX | FD_DIRTY_SHADER_PROG |
1093 			 FD_DIRTY_SHADER_IMAGE | FD_DIRTY_SHADER_SSBO)) {
1094 		struct fd_texture_stateobj *tex = &ctx->tex[PIPE_SHADER_COMPUTE];
1095 		unsigned bcolor_offset = fd6_border_color_offset(ctx, PIPE_SHADER_COMPUTE, tex);
1096 
1097 		bool needs_border = fd6_emit_textures(ctx->pipe, ring, PIPE_SHADER_COMPUTE, tex,
1098 				bcolor_offset, cp, ctx);
1099 
1100 		if (needs_border)
1101 			emit_border_color(ctx, ring);
1102 
1103 		OUT_PKT4(ring, REG_A6XX_SP_VS_TEX_COUNT, 1);
1104 		OUT_RING(ring, 0);
1105 
1106 		OUT_PKT4(ring, REG_A6XX_SP_HS_TEX_COUNT, 1);
1107 		OUT_RING(ring, 0);
1108 
1109 		OUT_PKT4(ring, REG_A6XX_SP_DS_TEX_COUNT, 1);
1110 		OUT_RING(ring, 0);
1111 
1112 		OUT_PKT4(ring, REG_A6XX_SP_GS_TEX_COUNT, 1);
1113 		OUT_RING(ring, 0);
1114 
1115 		OUT_PKT4(ring, REG_A6XX_SP_FS_TEX_COUNT, 1);
1116 		OUT_RING(ring, 0);
1117 	}
1118 
1119 	if (dirty & (FD_DIRTY_SHADER_SSBO | FD_DIRTY_SHADER_IMAGE)) {
1120 		struct fd_ringbuffer *state =
1121 			fd6_build_ibo_state(ctx, cp, PIPE_SHADER_COMPUTE);
1122 
1123 		OUT_PKT7(ring, CP_LOAD_STATE6_FRAG, 3);
1124 		OUT_RING(ring, CP_LOAD_STATE6_0_DST_OFF(0) |
1125 			CP_LOAD_STATE6_0_STATE_TYPE(ST6_IBO) |
1126 			CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
1127 			CP_LOAD_STATE6_0_STATE_BLOCK(SB6_CS_SHADER) |
1128 			CP_LOAD_STATE6_0_NUM_UNIT(ir3_shader_nibo(cp)));
1129 		OUT_RB(ring, state);
1130 
1131 		OUT_PKT4(ring, REG_A6XX_SP_CS_IBO_LO, 2);
1132 		OUT_RB(ring, state);
1133 
1134 		OUT_PKT4(ring, REG_A6XX_SP_CS_IBO_COUNT, 1);
1135 		OUT_RING(ring, ir3_shader_nibo(cp));
1136 
1137 		fd_ringbuffer_del(state);
1138 	}
1139 }
1140 
1141 
1142 /* emit setup at begin of new cmdstream buffer (don't rely on previous
1143  * state, there could have been a context switch between ioctls):
1144  */
1145 void
fd6_emit_restore(struct fd_batch * batch,struct fd_ringbuffer * ring)1146 fd6_emit_restore(struct fd_batch *batch, struct fd_ringbuffer *ring)
1147 {
1148 	//struct fd_context *ctx = batch->ctx;
1149 
1150 	fd_log(batch, "START RESTORE");
1151 
1152 	fd6_cache_inv(batch, ring);
1153 
1154 	OUT_REG(ring, A6XX_HLSQ_INVALIDATE_CMD(
1155 			.vs_state = true,
1156 			.hs_state = true,
1157 			.ds_state = true,
1158 			.gs_state = true,
1159 			.fs_state = true,
1160 			.cs_state = true,
1161 			.gfx_ibo = true,
1162 			.cs_ibo = true,
1163 			.gfx_shared_const = true,
1164 			.cs_shared_const = true,
1165 			.gfx_bindless = 0x1f,
1166 			.cs_bindless = 0x1f
1167 		));
1168 
1169 	OUT_WFI5(ring);
1170 
1171 	WRITE(REG_A6XX_RB_UNKNOWN_8E04, 0x0);
1172 	WRITE(REG_A6XX_SP_UNKNOWN_AE04, 0x8);
1173 	WRITE(REG_A6XX_SP_UNKNOWN_AE00, 0);
1174 	WRITE(REG_A6XX_SP_UNKNOWN_AE0F, 0x3f);
1175 	WRITE(REG_A6XX_SP_UNKNOWN_B605, 0x44);
1176 	WRITE(REG_A6XX_SP_UNKNOWN_B600, 0x100000);
1177 	WRITE(REG_A6XX_HLSQ_UNKNOWN_BE00, 0x80);
1178 	WRITE(REG_A6XX_HLSQ_UNKNOWN_BE01, 0);
1179 
1180 	WRITE(REG_A6XX_VPC_UNKNOWN_9600, 0);
1181 	WRITE(REG_A6XX_GRAS_UNKNOWN_8600, 0x880);
1182 	WRITE(REG_A6XX_HLSQ_UNKNOWN_BE04, 0x80000);
1183 	WRITE(REG_A6XX_SP_UNKNOWN_AE03, 0x1430);
1184 	WRITE(REG_A6XX_SP_IBO_COUNT, 0);
1185 	WRITE(REG_A6XX_SP_UNKNOWN_B182, 0);
1186 	WRITE(REG_A6XX_HLSQ_SHARED_CONSTS, 0);
1187 	WRITE(REG_A6XX_UCHE_UNKNOWN_0E12, 0x3200000);
1188 	WRITE(REG_A6XX_UCHE_CLIENT_PF, 4);
1189 	WRITE(REG_A6XX_RB_UNKNOWN_8E01, 0x1);
1190 	WRITE(REG_A6XX_SP_MODE_CONTROL, A6XX_SP_MODE_CONTROL_CONSTANT_DEMOTION_ENABLE | 4);
1191 	WRITE(REG_A6XX_VFD_ADD_OFFSET, A6XX_VFD_ADD_OFFSET_VERTEX);
1192 	WRITE(REG_A6XX_RB_UNKNOWN_8811, 0x00000010);
1193 	WRITE(REG_A6XX_PC_MODE_CNTL, 0x1f);
1194 
1195 	WRITE(REG_A6XX_GRAS_UNKNOWN_8101, 0);
1196 	WRITE(REG_A6XX_GRAS_SAMPLE_CNTL, 0);
1197 	WRITE(REG_A6XX_GRAS_UNKNOWN_8110, 0x2);
1198 
1199 	WRITE(REG_A6XX_RB_UNKNOWN_8818, 0);
1200 	WRITE(REG_A6XX_RB_UNKNOWN_8819, 0);
1201 	WRITE(REG_A6XX_RB_UNKNOWN_881A, 0);
1202 	WRITE(REG_A6XX_RB_UNKNOWN_881B, 0);
1203 	WRITE(REG_A6XX_RB_UNKNOWN_881C, 0);
1204 	WRITE(REG_A6XX_RB_UNKNOWN_881D, 0);
1205 	WRITE(REG_A6XX_RB_UNKNOWN_881E, 0);
1206 	WRITE(REG_A6XX_RB_UNKNOWN_88F0, 0);
1207 
1208 	WRITE(REG_A6XX_VPC_POINT_COORD_INVERT,
1209 		  A6XX_VPC_POINT_COORD_INVERT(0).value);
1210 	WRITE(REG_A6XX_VPC_UNKNOWN_9300, 0);
1211 
1212 	WRITE(REG_A6XX_VPC_SO_DISABLE, A6XX_VPC_SO_DISABLE(true).value);
1213 
1214 	WRITE(REG_A6XX_PC_RASTER_CNTL, 0);
1215 
1216 	WRITE(REG_A6XX_PC_MULTIVIEW_CNTL, 0);
1217 
1218 	WRITE(REG_A6XX_SP_UNKNOWN_A81B, 0);
1219 
1220 	WRITE(REG_A6XX_SP_UNKNOWN_B183, 0);
1221 
1222 	WRITE(REG_A6XX_GRAS_UNKNOWN_8099, 0);
1223 	WRITE(REG_A6XX_GRAS_VS_LAYER_CNTL, 0);
1224 	WRITE(REG_A6XX_GRAS_UNKNOWN_80A0, 2);
1225 	WRITE(REG_A6XX_GRAS_UNKNOWN_80AF, 0);
1226 	WRITE(REG_A6XX_VPC_UNKNOWN_9210, 0);
1227 	WRITE(REG_A6XX_VPC_UNKNOWN_9211, 0);
1228 	WRITE(REG_A6XX_VPC_UNKNOWN_9602, 0);
1229 	WRITE(REG_A6XX_PC_UNKNOWN_9E72, 0);
1230 	WRITE(REG_A6XX_SP_TP_SAMPLE_CONFIG, 0);
1231 	/* NOTE blob seems to (mostly?) use 0xb2 for SP_TP_UNKNOWN_B309
1232 	 * but this seems to kill texture gather offsets.
1233 	 */
1234 	WRITE(REG_A6XX_SP_TP_UNKNOWN_B309, 0xa2);
1235 	WRITE(REG_A6XX_RB_SAMPLE_CONFIG, 0);
1236 	WRITE(REG_A6XX_GRAS_SAMPLE_CONFIG, 0);
1237 	WRITE(REG_A6XX_RB_Z_BOUNDS_MIN, 0);
1238 	WRITE(REG_A6XX_RB_Z_BOUNDS_MAX, 0);
1239 	WRITE(REG_A6XX_HLSQ_CONTROL_5_REG, 0xfc);
1240 
1241 	emit_marker6(ring, 7);
1242 
1243 	OUT_PKT4(ring, REG_A6XX_VFD_MODE_CNTL, 1);
1244 	OUT_RING(ring, 0x00000000);   /* VFD_MODE_CNTL */
1245 
1246 	WRITE(REG_A6XX_VFD_MULTIVIEW_CNTL, 0);
1247 
1248 	OUT_PKT4(ring, REG_A6XX_PC_MODE_CNTL, 1);
1249 	OUT_RING(ring, 0x0000001f);   /* PC_MODE_CNTL */
1250 
1251 	/* we don't use this yet.. probably best to disable.. */
1252 	OUT_PKT7(ring, CP_SET_DRAW_STATE, 3);
1253 	OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(0) |
1254 			CP_SET_DRAW_STATE__0_DISABLE_ALL_GROUPS |
1255 			CP_SET_DRAW_STATE__0_GROUP_ID(0));
1256 	OUT_RING(ring, CP_SET_DRAW_STATE__1_ADDR_LO(0));
1257 	OUT_RING(ring, CP_SET_DRAW_STATE__2_ADDR_HI(0));
1258 
1259 	OUT_PKT4(ring, REG_A6XX_VPC_SO_STREAM_CNTL, 1);
1260 	OUT_RING(ring, 0x00000000);   /* VPC_SO_STREAM_CNTL */
1261 
1262 	OUT_PKT4(ring, REG_A6XX_GRAS_LRZ_CNTL, 1);
1263 	OUT_RING(ring, 0x00000000);
1264 
1265 	OUT_PKT4(ring, REG_A6XX_RB_LRZ_CNTL, 1);
1266 	OUT_RING(ring, 0x00000000);
1267 
1268 	fd_log(batch, "END RESTORE");
1269 }
1270 
1271 static void
fd6_mem_to_mem(struct fd_ringbuffer * ring,struct pipe_resource * dst,unsigned dst_off,struct pipe_resource * src,unsigned src_off,unsigned sizedwords)1272 fd6_mem_to_mem(struct fd_ringbuffer *ring, struct pipe_resource *dst,
1273 		unsigned dst_off, struct pipe_resource *src, unsigned src_off,
1274 		unsigned sizedwords)
1275 {
1276 	struct fd_bo *src_bo = fd_resource(src)->bo;
1277 	struct fd_bo *dst_bo = fd_resource(dst)->bo;
1278 	unsigned i;
1279 
1280 	for (i = 0; i < sizedwords; i++) {
1281 		OUT_PKT7(ring, CP_MEM_TO_MEM, 5);
1282 		OUT_RING(ring, 0x00000000);
1283 		OUT_RELOC(ring, dst_bo, dst_off, 0, 0);
1284 		OUT_RELOC(ring, src_bo, src_off, 0, 0);
1285 
1286 		dst_off += 4;
1287 		src_off += 4;
1288 	}
1289 }
1290 
1291 /* this is *almost* the same as fd6_cache_flush().. which I guess
1292  * could be re-worked to be something a bit more generic w/ param
1293  * indicating what needs to be flushed..  although that would mean
1294  * figuring out which events trigger what state to flush..
1295  */
1296 static void
fd6_framebuffer_barrier(struct fd_context * ctx)1297 fd6_framebuffer_barrier(struct fd_context *ctx)
1298 {
1299 	struct fd6_context *fd6_ctx = fd6_context(ctx);
1300 	struct fd_batch *batch = ctx->batch;
1301 	struct fd_ringbuffer *ring = batch->draw;
1302 	unsigned seqno;
1303 
1304 	seqno = fd6_event_write(batch, ring, RB_DONE_TS, true);
1305 
1306 	OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
1307 	OUT_RING(ring, CP_WAIT_REG_MEM_0_FUNCTION(WRITE_EQ) |
1308 		       CP_WAIT_REG_MEM_0_POLL_MEMORY);
1309 	OUT_RELOC(ring, control_ptr(fd6_ctx, seqno));
1310 	OUT_RING(ring, CP_WAIT_REG_MEM_3_REF(seqno));
1311 	OUT_RING(ring, CP_WAIT_REG_MEM_4_MASK(~0));
1312 	OUT_RING(ring, CP_WAIT_REG_MEM_5_DELAY_LOOP_CYCLES(16));
1313 
1314 	fd6_event_write(batch, ring, PC_CCU_FLUSH_COLOR_TS, true);
1315 	fd6_event_write(batch, ring, PC_CCU_FLUSH_DEPTH_TS, true);
1316 
1317 	seqno = fd6_event_write(batch, ring, CACHE_FLUSH_TS, true);
1318 
1319 	fd6_event_write(batch, ring, 0x31, false);
1320 
1321 	OUT_PKT7(ring, CP_WAIT_MEM_GTE, 4);
1322 	OUT_RING(ring, CP_WAIT_MEM_GTE_0_RESERVED(0));
1323 	OUT_RELOC(ring, control_ptr(fd6_ctx, seqno));
1324 	OUT_RING(ring, CP_WAIT_MEM_GTE_3_REF(seqno));
1325 }
1326 
1327 void
fd6_emit_init_screen(struct pipe_screen * pscreen)1328 fd6_emit_init_screen(struct pipe_screen *pscreen)
1329 {
1330 	struct fd_screen *screen = fd_screen(pscreen);
1331 	screen->emit_ib = fd6_emit_ib;
1332 	screen->mem_to_mem = fd6_mem_to_mem;
1333 }
1334 
1335 void
fd6_emit_init(struct pipe_context * pctx)1336 fd6_emit_init(struct pipe_context *pctx)
1337 {
1338 	struct fd_context *ctx = fd_context(pctx);
1339 	ctx->framebuffer_barrier = fd6_framebuffer_barrier;
1340 }
1341