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