freedreno/a6xx: move SSBO/image consts to IBO stateobj
[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 unsigned offset = (so->offsets[i] * info->stride[i] * 4) +
716 target->buffer_offset;
717
718 OUT_PKT4(ring, REG_A6XX_VPC_SO_BUFFER_BASE_LO(i), 3);
719 /* VPC_SO[i].BUFFER_BASE_LO: */
720 OUT_RELOCW(ring, fd_resource(target->buffer)->bo, 0, 0, 0);
721 OUT_RING(ring, target->buffer_size + offset);
722
723 OUT_PKT4(ring, REG_A6XX_VPC_SO_BUFFER_OFFSET(i), 3);
724 OUT_RING(ring, offset);
725 /* VPC_SO[i].FLUSH_BASE_LO/HI: */
726 // TODO just give hw a dummy addr for now.. we should
727 // be using this an then CP_MEM_TO_REG to set the
728 // VPC_SO[i].BUFFER_OFFSET for the next draw..
729 OUT_RELOCW(ring, control_ptr(fd6_context(ctx), flush_base));
730
731 emit->streamout_mask |= (1 << i);
732 }
733
734 if (emit->streamout_mask) {
735 const struct fd6_streamout_state *tf = &prog->tf;
736
737 OUT_PKT7(ring, CP_CONTEXT_REG_BUNCH, 12 + (2 * tf->prog_count));
738 OUT_RING(ring, REG_A6XX_VPC_SO_BUF_CNTL);
739 OUT_RING(ring, tf->vpc_so_buf_cntl);
740 OUT_RING(ring, REG_A6XX_VPC_SO_NCOMP(0));
741 OUT_RING(ring, tf->ncomp[0]);
742 OUT_RING(ring, REG_A6XX_VPC_SO_NCOMP(1));
743 OUT_RING(ring, tf->ncomp[1]);
744 OUT_RING(ring, REG_A6XX_VPC_SO_NCOMP(2));
745 OUT_RING(ring, tf->ncomp[2]);
746 OUT_RING(ring, REG_A6XX_VPC_SO_NCOMP(3));
747 OUT_RING(ring, tf->ncomp[3]);
748 OUT_RING(ring, REG_A6XX_VPC_SO_CNTL);
749 OUT_RING(ring, A6XX_VPC_SO_CNTL_ENABLE);
750 for (unsigned i = 0; i < tf->prog_count; i++) {
751 OUT_RING(ring, REG_A6XX_VPC_SO_PROG);
752 OUT_RING(ring, tf->prog[i]);
753 }
754
755 OUT_PKT4(ring, REG_A6XX_VPC_SO_OVERRIDE, 1);
756 OUT_RING(ring, 0x0);
757 } else {
758 OUT_PKT7(ring, CP_CONTEXT_REG_BUNCH, 4);
759 OUT_RING(ring, REG_A6XX_VPC_SO_CNTL);
760 OUT_RING(ring, 0);
761 OUT_RING(ring, REG_A6XX_VPC_SO_BUF_CNTL);
762 OUT_RING(ring, 0);
763
764 OUT_PKT4(ring, REG_A6XX_VPC_SO_OVERRIDE, 1);
765 OUT_RING(ring, A6XX_VPC_SO_OVERRIDE_SO_DISABLE);
766 }
767
768 }
769
770 void
771 fd6_emit_state(struct fd_ringbuffer *ring, struct fd6_emit *emit)
772 {
773 struct fd_context *ctx = emit->ctx;
774 struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
775 const struct fd6_program_state *prog = fd6_emit_get_prog(emit);
776 const struct ir3_shader_variant *vp = emit->vs;
777 const struct ir3_shader_variant *fp = emit->fs;
778 const enum fd_dirty_3d_state dirty = emit->dirty;
779 bool needs_border = false;
780
781 emit_marker6(ring, 5);
782
783 /* NOTE: we track fb_read differently than _BLEND_ENABLED since
784 * we might at some point decide to do sysmem in some cases when
785 * blend is enabled:
786 */
787 if (fp->fb_read)
788 ctx->batch->gmem_reason |= FD_GMEM_FB_READ;
789
790 if (emit->dirty & (FD_DIRTY_VTXBUF | FD_DIRTY_VTXSTATE)) {
791 struct fd_ringbuffer *state;
792
793 state = build_vbo_state(emit, emit->vs);
794 fd6_emit_take_group(emit, state, FD6_GROUP_VBO, 0x7);
795 }
796
797 if (dirty & FD_DIRTY_ZSA) {
798 struct fd6_zsa_stateobj *zsa = fd6_zsa_stateobj(ctx->zsa);
799
800 if (util_format_is_pure_integer(pipe_surface_format(pfb->cbufs[0])))
801 fd6_emit_add_group(emit, zsa->stateobj_no_alpha, FD6_GROUP_ZSA, 0x7);
802 else
803 fd6_emit_add_group(emit, zsa->stateobj, FD6_GROUP_ZSA, 0x7);
804 }
805
806 if ((dirty & (FD_DIRTY_ZSA | FD_DIRTY_PROG)) && pfb->zsbuf) {
807 struct fd_ringbuffer *state;
808
809 state = build_lrz(emit, false);
810 fd6_emit_take_group(emit, state, FD6_GROUP_LRZ, 0x6);
811
812 state = build_lrz(emit, true);
813 fd6_emit_take_group(emit, state, FD6_GROUP_LRZ_BINNING, 0x1);
814 }
815
816 if (dirty & FD_DIRTY_STENCIL_REF) {
817 struct pipe_stencil_ref *sr = &ctx->stencil_ref;
818
819 OUT_PKT4(ring, REG_A6XX_RB_STENCILREF, 1);
820 OUT_RING(ring, A6XX_RB_STENCILREF_REF(sr->ref_value[0]) |
821 A6XX_RB_STENCILREF_BFREF(sr->ref_value[1]));
822 }
823
824 /* NOTE: scissor enabled bit is part of rasterizer state: */
825 if (dirty & (FD_DIRTY_SCISSOR | FD_DIRTY_RASTERIZER)) {
826 struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
827
828 OUT_PKT4(ring, REG_A6XX_GRAS_SC_SCREEN_SCISSOR_TL_0, 2);
829 OUT_RING(ring, A6XX_GRAS_SC_SCREEN_SCISSOR_TL_0_X(scissor->minx) |
830 A6XX_GRAS_SC_SCREEN_SCISSOR_TL_0_Y(scissor->miny));
831 OUT_RING(ring, A6XX_GRAS_SC_SCREEN_SCISSOR_TL_0_X(scissor->maxx - 1) |
832 A6XX_GRAS_SC_SCREEN_SCISSOR_TL_0_Y(scissor->maxy - 1));
833
834 ctx->batch->max_scissor.minx = MIN2(ctx->batch->max_scissor.minx, scissor->minx);
835 ctx->batch->max_scissor.miny = MIN2(ctx->batch->max_scissor.miny, scissor->miny);
836 ctx->batch->max_scissor.maxx = MAX2(ctx->batch->max_scissor.maxx, scissor->maxx);
837 ctx->batch->max_scissor.maxy = MAX2(ctx->batch->max_scissor.maxy, scissor->maxy);
838 }
839
840 if (dirty & FD_DIRTY_VIEWPORT) {
841 struct pipe_scissor_state *scissor = &ctx->viewport_scissor;
842
843 OUT_PKT4(ring, REG_A6XX_GRAS_CL_VPORT_XOFFSET_0, 6);
844 OUT_RING(ring, A6XX_GRAS_CL_VPORT_XOFFSET_0(ctx->viewport.translate[0]));
845 OUT_RING(ring, A6XX_GRAS_CL_VPORT_XSCALE_0(ctx->viewport.scale[0]));
846 OUT_RING(ring, A6XX_GRAS_CL_VPORT_YOFFSET_0(ctx->viewport.translate[1]));
847 OUT_RING(ring, A6XX_GRAS_CL_VPORT_YSCALE_0(ctx->viewport.scale[1]));
848 OUT_RING(ring, A6XX_GRAS_CL_VPORT_ZOFFSET_0(ctx->viewport.translate[2]));
849 OUT_RING(ring, A6XX_GRAS_CL_VPORT_ZSCALE_0(ctx->viewport.scale[2]));
850
851 OUT_PKT4(ring, REG_A6XX_GRAS_SC_VIEWPORT_SCISSOR_TL_0, 2);
852 OUT_RING(ring, A6XX_GRAS_SC_VIEWPORT_SCISSOR_TL_0_X(scissor->minx) |
853 A6XX_GRAS_SC_VIEWPORT_SCISSOR_TL_0_Y(scissor->miny));
854 OUT_RING(ring, A6XX_GRAS_SC_VIEWPORT_SCISSOR_TL_0_X(scissor->maxx - 1) |
855 A6XX_GRAS_SC_VIEWPORT_SCISSOR_TL_0_Y(scissor->maxy - 1));
856
857 unsigned guardband_x = fd_calc_guardband(scissor->maxx - scissor->minx);
858 unsigned guardband_y = fd_calc_guardband(scissor->maxy - scissor->miny);
859
860 OUT_PKT4(ring, REG_A6XX_GRAS_CL_GUARDBAND_CLIP_ADJ, 1);
861 OUT_RING(ring, A6XX_GRAS_CL_GUARDBAND_CLIP_ADJ_HORZ(guardband_x) |
862 A6XX_GRAS_CL_GUARDBAND_CLIP_ADJ_VERT(guardband_y));
863 }
864
865 if (dirty & FD_DIRTY_PROG) {
866 fd6_emit_add_group(emit, prog->config_stateobj, FD6_GROUP_PROG_CONFIG, 0x7);
867 fd6_emit_add_group(emit, prog->stateobj, FD6_GROUP_PROG, 0x6);
868 fd6_emit_add_group(emit, prog->binning_stateobj,
869 FD6_GROUP_PROG_BINNING, 0x1);
870
871 /* emit remaining non-stateobj program state, ie. what depends
872 * on other emit state, so cannot be pre-baked. This could
873 * be moved to a separate stateobj which is dynamically
874 * created.
875 */
876 fd6_program_emit(ring, emit);
877 }
878
879 if (dirty & FD_DIRTY_RASTERIZER) {
880 struct fd6_rasterizer_stateobj *rasterizer =
881 fd6_rasterizer_stateobj(ctx->rasterizer);
882 fd6_emit_add_group(emit, rasterizer->stateobj,
883 FD6_GROUP_RASTERIZER, 0x7);
884 }
885
886 /* Since the primitive restart state is not part of a tracked object, we
887 * re-emit this register every time.
888 */
889 if (emit->info && ctx->rasterizer) {
890 struct fd6_rasterizer_stateobj *rasterizer =
891 fd6_rasterizer_stateobj(ctx->rasterizer);
892 OUT_PKT4(ring, REG_A6XX_PC_UNKNOWN_9806, 1);
893 OUT_RING(ring, 0);
894 OUT_PKT4(ring, REG_A6XX_PC_UNKNOWN_9990, 1);
895 OUT_RING(ring, 0);
896 OUT_PKT4(ring, REG_A6XX_VFD_UNKNOWN_A008, 1);
897 OUT_RING(ring, 0);
898
899 OUT_PKT4(ring, REG_A6XX_PC_PRIMITIVE_CNTL_0, 1);
900 OUT_RING(ring, rasterizer->pc_primitive_cntl |
901 COND(emit->info->primitive_restart && emit->info->index_size,
902 A6XX_PC_PRIMITIVE_CNTL_0_PRIMITIVE_RESTART));
903 }
904
905 if (dirty & (FD_DIRTY_FRAMEBUFFER | FD_DIRTY_RASTERIZER | FD_DIRTY_PROG)) {
906 unsigned nr = pfb->nr_cbufs;
907
908 if (ctx->rasterizer->rasterizer_discard)
909 nr = 0;
910
911 OUT_PKT4(ring, REG_A6XX_RB_FS_OUTPUT_CNTL0, 2);
912 OUT_RING(ring, COND(fp->writes_pos, A6XX_RB_FS_OUTPUT_CNTL0_FRAG_WRITES_Z) |
913 COND(fp->writes_smask && pfb->samples > 1,
914 A6XX_RB_FS_OUTPUT_CNTL0_FRAG_WRITES_SAMPMASK));
915 OUT_RING(ring, A6XX_RB_FS_OUTPUT_CNTL1_MRT(nr));
916
917 OUT_PKT4(ring, REG_A6XX_SP_FS_OUTPUT_CNTL1, 1);
918 OUT_RING(ring, A6XX_SP_FS_OUTPUT_CNTL1_MRT(nr));
919 }
920
921 #define DIRTY_CONST (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_CONST)
922
923 if (ctx->dirty_shader[PIPE_SHADER_VERTEX] & DIRTY_CONST) {
924 struct fd_ringbuffer *vsconstobj = fd_submit_new_ringbuffer(
925 ctx->batch->submit, 0x1000, FD_RINGBUFFER_STREAMING);
926
927 ir3_emit_user_consts(ctx->screen, vp, vsconstobj,
928 &ctx->constbuf[PIPE_SHADER_VERTEX]);
929 ir3_emit_ubos(ctx->screen, vp, vsconstobj,
930 &ctx->constbuf[PIPE_SHADER_VERTEX]);
931
932 fd6_emit_take_group(emit, vsconstobj, FD6_GROUP_VS_CONST, 0x7);
933 }
934
935 if (ctx->dirty_shader[PIPE_SHADER_FRAGMENT] & DIRTY_CONST) {
936 struct fd_ringbuffer *fsconstobj = fd_submit_new_ringbuffer(
937 ctx->batch->submit, 0x1000, FD_RINGBUFFER_STREAMING);
938
939 ir3_emit_user_consts(ctx->screen, fp, fsconstobj,
940 &ctx->constbuf[PIPE_SHADER_FRAGMENT]);
941 ir3_emit_ubos(ctx->screen, fp, fsconstobj,
942 &ctx->constbuf[PIPE_SHADER_FRAGMENT]);
943
944 fd6_emit_take_group(emit, fsconstobj, FD6_GROUP_FS_CONST, 0x6);
945 }
946
947 /* if driver-params are needed, emit each time: */
948 if (ir3_needs_vs_driver_params(vp)) {
949 struct fd_ringbuffer *dpconstobj = fd_submit_new_ringbuffer(
950 ctx->batch->submit, IR3_DP_VS_COUNT * 4, FD_RINGBUFFER_STREAMING);
951 ir3_emit_vs_driver_params(vp, dpconstobj, ctx, emit->info);
952 fd6_emit_take_group(emit, dpconstobj, FD6_GROUP_VS_DRIVER_PARAMS, 0x7);
953 } else {
954 fd6_emit_take_group(emit, NULL, FD6_GROUP_VS_DRIVER_PARAMS, 0x7);
955 }
956
957 struct ir3_stream_output_info *info = &vp->shader->stream_output;
958 if (info->num_outputs)
959 fd6_emit_streamout(ring, emit, info);
960
961 if (dirty & FD_DIRTY_BLEND) {
962 struct fd6_blend_stateobj *blend = fd6_blend_stateobj(ctx->blend);
963 uint32_t i;
964
965 for (i = 0; i < pfb->nr_cbufs; i++) {
966 enum pipe_format format = pipe_surface_format(pfb->cbufs[i]);
967 bool is_int = util_format_is_pure_integer(format);
968 bool has_alpha = util_format_has_alpha(format);
969 uint32_t control = blend->rb_mrt[i].control;
970 uint32_t blend_control = blend->rb_mrt[i].blend_control_alpha;
971
972 if (is_int) {
973 control &= A6XX_RB_MRT_CONTROL_COMPONENT_ENABLE__MASK;
974 control |= A6XX_RB_MRT_CONTROL_ROP_CODE(ROP_COPY);
975 }
976
977 if (has_alpha) {
978 blend_control |= blend->rb_mrt[i].blend_control_rgb;
979 } else {
980 blend_control |= blend->rb_mrt[i].blend_control_no_alpha_rgb;
981 control &= ~A6XX_RB_MRT_CONTROL_BLEND2;
982 }
983
984 OUT_PKT4(ring, REG_A6XX_RB_MRT_CONTROL(i), 1);
985 OUT_RING(ring, control);
986
987 OUT_PKT4(ring, REG_A6XX_RB_MRT_BLEND_CONTROL(i), 1);
988 OUT_RING(ring, blend_control);
989 }
990
991 OUT_PKT4(ring, REG_A6XX_RB_DITHER_CNTL, 1);
992 OUT_RING(ring, blend->rb_dither_cntl);
993
994 OUT_PKT4(ring, REG_A6XX_SP_BLEND_CNTL, 1);
995 OUT_RING(ring, blend->sp_blend_cntl);
996 }
997
998 if (dirty & (FD_DIRTY_BLEND | FD_DIRTY_SAMPLE_MASK)) {
999 struct fd6_blend_stateobj *blend = fd6_blend_stateobj(ctx->blend);
1000
1001 OUT_PKT4(ring, REG_A6XX_RB_BLEND_CNTL, 1);
1002 OUT_RING(ring, blend->rb_blend_cntl |
1003 A6XX_RB_BLEND_CNTL_SAMPLE_MASK(ctx->sample_mask));
1004 }
1005
1006 if (dirty & FD_DIRTY_BLEND_COLOR) {
1007 struct pipe_blend_color *bcolor = &ctx->blend_color;
1008
1009 OUT_PKT4(ring, REG_A6XX_RB_BLEND_RED_F32, 4);
1010 OUT_RING(ring, A6XX_RB_BLEND_RED_F32(bcolor->color[0]));
1011 OUT_RING(ring, A6XX_RB_BLEND_GREEN_F32(bcolor->color[1]));
1012 OUT_RING(ring, A6XX_RB_BLEND_BLUE_F32(bcolor->color[2]));
1013 OUT_RING(ring, A6XX_RB_BLEND_ALPHA_F32(bcolor->color[3]));
1014 }
1015
1016 needs_border |= fd6_emit_combined_textures(ring, emit, PIPE_SHADER_VERTEX, vp);
1017 needs_border |= fd6_emit_combined_textures(ring, emit, PIPE_SHADER_FRAGMENT, fp);
1018
1019 if (needs_border)
1020 emit_border_color(ctx, ring);
1021
1022 #define DIRTY_IBO (FD_DIRTY_SHADER_SSBO | FD_DIRTY_SHADER_IMAGE | \
1023 FD_DIRTY_SHADER_PROG)
1024 if (ctx->dirty_shader[PIPE_SHADER_FRAGMENT] & DIRTY_IBO) {
1025 struct fd_ringbuffer *state =
1026 fd6_build_ibo_state(ctx, fp, PIPE_SHADER_FRAGMENT);
1027 struct fd_ringbuffer *obj = fd_submit_new_ringbuffer(
1028 ctx->batch->submit, 0x100, FD_RINGBUFFER_STREAMING);
1029 const struct ir3_ibo_mapping *mapping = &fp->image_mapping;
1030
1031 OUT_PKT7(obj, CP_LOAD_STATE6, 3);
1032 OUT_RING(obj, CP_LOAD_STATE6_0_DST_OFF(0) |
1033 CP_LOAD_STATE6_0_STATE_TYPE(ST6_SHADER) |
1034 CP_LOAD_STATE6_0_STATE_SRC(SS6_INDIRECT) |
1035 CP_LOAD_STATE6_0_STATE_BLOCK(SB6_IBO) |
1036 CP_LOAD_STATE6_0_NUM_UNIT(mapping->num_ibo));
1037 OUT_RB(obj, state);
1038
1039 OUT_PKT4(obj, REG_A6XX_SP_IBO_LO, 2);
1040 OUT_RB(obj, state);
1041
1042 /* TODO if we used CP_SET_DRAW_STATE for compute shaders, we could
1043 * de-duplicate this from program->config_stateobj
1044 */
1045 OUT_PKT4(obj, REG_A6XX_SP_IBO_COUNT, 1);
1046 OUT_RING(obj, mapping->num_ibo);
1047
1048 ir3_emit_ssbo_sizes(ctx->screen, fp, obj,
1049 &ctx->shaderbuf[PIPE_SHADER_FRAGMENT]);
1050 ir3_emit_image_dims(ctx->screen, fp, obj,
1051 &ctx->shaderimg[PIPE_SHADER_FRAGMENT]);
1052
1053 fd6_emit_take_group(emit, obj, FD6_GROUP_IBO, 0x6);
1054 fd_ringbuffer_del(state);
1055 }
1056
1057 if (emit->num_groups > 0) {
1058 OUT_PKT7(ring, CP_SET_DRAW_STATE, 3 * emit->num_groups);
1059 for (unsigned i = 0; i < emit->num_groups; i++) {
1060 struct fd6_state_group *g = &emit->groups[i];
1061 unsigned n = g->stateobj ?
1062 fd_ringbuffer_size(g->stateobj) / 4 : 0;
1063
1064 if (n == 0) {
1065 OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(0) |
1066 CP_SET_DRAW_STATE__0_DISABLE |
1067 CP_SET_DRAW_STATE__0_ENABLE_MASK(g->enable_mask) |
1068 CP_SET_DRAW_STATE__0_GROUP_ID(g->group_id));
1069 OUT_RING(ring, 0x00000000);
1070 OUT_RING(ring, 0x00000000);
1071 } else {
1072 OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(n) |
1073 CP_SET_DRAW_STATE__0_ENABLE_MASK(g->enable_mask) |
1074 CP_SET_DRAW_STATE__0_GROUP_ID(g->group_id));
1075 OUT_RB(ring, g->stateobj);
1076 }
1077
1078 if (g->stateobj)
1079 fd_ringbuffer_del(g->stateobj);
1080 }
1081 emit->num_groups = 0;
1082 }
1083 }
1084
1085 void
1086 fd6_emit_cs_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
1087 struct ir3_shader_variant *cp)
1088 {
1089 enum fd_dirty_shader_state dirty = ctx->dirty_shader[PIPE_SHADER_COMPUTE];
1090
1091 if (dirty & (FD_DIRTY_SHADER_TEX | FD_DIRTY_SHADER_PROG |
1092 FD_DIRTY_SHADER_IMAGE | FD_DIRTY_SHADER_SSBO)) {
1093 struct fd_texture_stateobj *tex = &ctx->tex[PIPE_SHADER_COMPUTE];
1094 unsigned bcolor_offset = fd6_border_color_offset(ctx, PIPE_SHADER_COMPUTE, tex);
1095
1096 bool needs_border = fd6_emit_textures(ctx->pipe, ring, PIPE_SHADER_COMPUTE, tex,
1097 bcolor_offset, cp, ctx);
1098
1099 if (needs_border)
1100 emit_border_color(ctx, ring);
1101
1102 OUT_PKT4(ring, REG_A6XX_SP_VS_TEX_COUNT, 1);
1103 OUT_RING(ring, 0);
1104
1105 OUT_PKT4(ring, REG_A6XX_SP_HS_TEX_COUNT, 1);
1106 OUT_RING(ring, 0);
1107
1108 OUT_PKT4(ring, REG_A6XX_SP_DS_TEX_COUNT, 1);
1109 OUT_RING(ring, 0);
1110
1111 OUT_PKT4(ring, REG_A6XX_SP_GS_TEX_COUNT, 1);
1112 OUT_RING(ring, 0);
1113
1114 OUT_PKT4(ring, REG_A6XX_SP_FS_TEX_COUNT, 1);
1115 OUT_RING(ring, 0);
1116 }
1117
1118 if (dirty & (FD_DIRTY_SHADER_SSBO | FD_DIRTY_SHADER_IMAGE)) {
1119 struct fd_ringbuffer *state =
1120 fd6_build_ibo_state(ctx, cp, PIPE_SHADER_COMPUTE);
1121 const struct ir3_ibo_mapping *mapping = &cp->image_mapping;
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(mapping->num_ibo));
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, mapping->num_ibo);
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
1146 fd6_emit_restore(struct fd_batch *batch, struct fd_ringbuffer *ring)
1147 {
1148 //struct fd_context *ctx = batch->ctx;
1149
1150 fd6_cache_inv(batch, ring);
1151
1152 OUT_PKT4(ring, REG_A6XX_HLSQ_UPDATE_CNTL, 1);
1153 OUT_RING(ring, 0xfffff);
1154
1155 /*
1156 t7 opcode: CP_PERFCOUNTER_ACTION (50) (4 dwords)
1157 0000000500024048: 70d08003 00000000 001c5000 00000005
1158 t7 opcode: CP_PERFCOUNTER_ACTION (50) (4 dwords)
1159 0000000500024058: 70d08003 00000010 001c7000 00000005
1160
1161 t7 opcode: CP_WAIT_FOR_IDLE (26) (1 dwords)
1162 0000000500024068: 70268000
1163 */
1164
1165 OUT_WFI5(ring);
1166
1167 WRITE(REG_A6XX_RB_CCU_CNTL, 0x7c400004);
1168 WRITE(REG_A6XX_RB_UNKNOWN_8E04, 0x00100000);
1169 WRITE(REG_A6XX_SP_UNKNOWN_AE04, 0x8);
1170 WRITE(REG_A6XX_SP_UNKNOWN_AE00, 0);
1171 WRITE(REG_A6XX_SP_UNKNOWN_AE0F, 0x3f);
1172 WRITE(REG_A6XX_SP_UNKNOWN_B605, 0x44);
1173 WRITE(REG_A6XX_SP_UNKNOWN_B600, 0x100000);
1174 WRITE(REG_A6XX_HLSQ_UNKNOWN_BE00, 0x80);
1175 WRITE(REG_A6XX_HLSQ_UNKNOWN_BE01, 0);
1176
1177 WRITE(REG_A6XX_VPC_UNKNOWN_9600, 0);
1178 WRITE(REG_A6XX_GRAS_UNKNOWN_8600, 0x880);
1179 WRITE(REG_A6XX_HLSQ_UNKNOWN_BE04, 0x80000);
1180 WRITE(REG_A6XX_SP_UNKNOWN_AE03, 0x1430);
1181 WRITE(REG_A6XX_SP_IBO_COUNT, 0);
1182 WRITE(REG_A6XX_SP_UNKNOWN_B182, 0);
1183 WRITE(REG_A6XX_HLSQ_UNKNOWN_BB11, 0);
1184 WRITE(REG_A6XX_UCHE_UNKNOWN_0E12, 0x3200000);
1185 WRITE(REG_A6XX_UCHE_CLIENT_PF, 4);
1186 WRITE(REG_A6XX_RB_UNKNOWN_8E01, 0x1);
1187 WRITE(REG_A6XX_SP_UNKNOWN_AB00, 0x5);
1188 WRITE(REG_A6XX_VFD_UNKNOWN_A009, 0x00000001);
1189 WRITE(REG_A6XX_RB_UNKNOWN_8811, 0x00000010);
1190 WRITE(REG_A6XX_PC_MODE_CNTL, 0x1f);
1191
1192 OUT_PKT4(ring, REG_A6XX_RB_SRGB_CNTL, 1);
1193 OUT_RING(ring, 0);
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_RENDER_CONTROL0, 0x401);
1200 WRITE(REG_A6XX_RB_RENDER_CONTROL1, 0);
1201 WRITE(REG_A6XX_RB_FS_OUTPUT_CNTL0, 0);
1202 WRITE(REG_A6XX_RB_SAMPLE_CNTL, 0);
1203 WRITE(REG_A6XX_RB_UNKNOWN_8818, 0);
1204 WRITE(REG_A6XX_RB_UNKNOWN_8819, 0);
1205 WRITE(REG_A6XX_RB_UNKNOWN_881A, 0);
1206 WRITE(REG_A6XX_RB_UNKNOWN_881B, 0);
1207 WRITE(REG_A6XX_RB_UNKNOWN_881C, 0);
1208 WRITE(REG_A6XX_RB_UNKNOWN_881D, 0);
1209 WRITE(REG_A6XX_RB_UNKNOWN_881E, 0);
1210 WRITE(REG_A6XX_RB_UNKNOWN_88F0, 0);
1211
1212 WRITE(REG_A6XX_VPC_UNKNOWN_9101, 0xffff00);
1213 WRITE(REG_A6XX_VPC_UNKNOWN_9107, 0);
1214
1215 WRITE(REG_A6XX_VPC_UNKNOWN_9236,
1216 A6XX_VPC_UNKNOWN_9236_POINT_COORD_INVERT(0));
1217 WRITE(REG_A6XX_VPC_UNKNOWN_9300, 0);
1218
1219 WRITE(REG_A6XX_VPC_SO_OVERRIDE, A6XX_VPC_SO_OVERRIDE_SO_DISABLE);
1220
1221 WRITE(REG_A6XX_PC_UNKNOWN_9801, 0);
1222 WRITE(REG_A6XX_PC_UNKNOWN_9806, 0);
1223 WRITE(REG_A6XX_PC_UNKNOWN_9980, 0);
1224
1225 WRITE(REG_A6XX_PC_UNKNOWN_9B06, 0);
1226 WRITE(REG_A6XX_PC_UNKNOWN_9B06, 0);
1227
1228 WRITE(REG_A6XX_SP_UNKNOWN_A81B, 0);
1229
1230 WRITE(REG_A6XX_SP_UNKNOWN_B183, 0);
1231
1232 WRITE(REG_A6XX_GRAS_UNKNOWN_8099, 0);
1233 WRITE(REG_A6XX_GRAS_UNKNOWN_809B, 0);
1234 WRITE(REG_A6XX_GRAS_UNKNOWN_80A0, 2);
1235 WRITE(REG_A6XX_GRAS_UNKNOWN_80AF, 0);
1236 WRITE(REG_A6XX_VPC_UNKNOWN_9210, 0);
1237 WRITE(REG_A6XX_VPC_UNKNOWN_9211, 0);
1238 WRITE(REG_A6XX_VPC_UNKNOWN_9602, 0);
1239 WRITE(REG_A6XX_PC_UNKNOWN_9981, 0x3);
1240 WRITE(REG_A6XX_PC_UNKNOWN_9E72, 0);
1241 WRITE(REG_A6XX_VPC_UNKNOWN_9108, 0x3);
1242 WRITE(REG_A6XX_SP_TP_UNKNOWN_B304, 0);
1243 /* NOTE blob seems to (mostly?) use 0xb2 for SP_TP_UNKNOWN_B309
1244 * but this seems to kill texture gather offsets.
1245 */
1246 WRITE(REG_A6XX_SP_TP_UNKNOWN_B309, 0xa2);
1247 WRITE(REG_A6XX_RB_UNKNOWN_8804, 0);
1248 WRITE(REG_A6XX_GRAS_UNKNOWN_80A4, 0);
1249 WRITE(REG_A6XX_GRAS_UNKNOWN_80A5, 0);
1250 WRITE(REG_A6XX_GRAS_UNKNOWN_80A6, 0);
1251 WRITE(REG_A6XX_RB_UNKNOWN_8805, 0);
1252 WRITE(REG_A6XX_RB_UNKNOWN_8806, 0);
1253 WRITE(REG_A6XX_RB_UNKNOWN_8878, 0);
1254 WRITE(REG_A6XX_RB_UNKNOWN_8879, 0);
1255 WRITE(REG_A6XX_HLSQ_CONTROL_5_REG, 0xfc);
1256
1257 emit_marker6(ring, 7);
1258
1259 OUT_PKT4(ring, REG_A6XX_VFD_MODE_CNTL, 1);
1260 OUT_RING(ring, 0x00000000); /* VFD_MODE_CNTL */
1261
1262 WRITE(REG_A6XX_VFD_UNKNOWN_A008, 0);
1263
1264 OUT_PKT4(ring, REG_A6XX_PC_MODE_CNTL, 1);
1265 OUT_RING(ring, 0x0000001f); /* PC_MODE_CNTL */
1266
1267 /* we don't use this yet.. probably best to disable.. */
1268 OUT_PKT7(ring, CP_SET_DRAW_STATE, 3);
1269 OUT_RING(ring, CP_SET_DRAW_STATE__0_COUNT(0) |
1270 CP_SET_DRAW_STATE__0_DISABLE_ALL_GROUPS |
1271 CP_SET_DRAW_STATE__0_GROUP_ID(0));
1272 OUT_RING(ring, CP_SET_DRAW_STATE__1_ADDR_LO(0));
1273 OUT_RING(ring, CP_SET_DRAW_STATE__2_ADDR_HI(0));
1274
1275 OUT_PKT4(ring, REG_A6XX_VPC_SO_BUF_CNTL, 1);
1276 OUT_RING(ring, 0x00000000); /* VPC_SO_BUF_CNTL */
1277
1278 OUT_PKT4(ring, REG_A6XX_SP_HS_CTRL_REG0, 1);
1279 OUT_RING(ring, 0x00000000);
1280
1281 OUT_PKT4(ring, REG_A6XX_SP_GS_CTRL_REG0, 1);
1282 OUT_RING(ring, 0x00000000);
1283
1284 OUT_PKT4(ring, REG_A6XX_GRAS_LRZ_CNTL, 1);
1285 OUT_RING(ring, 0x00000000);
1286
1287 OUT_PKT4(ring, REG_A6XX_RB_LRZ_CNTL, 1);
1288 OUT_RING(ring, 0x00000000);
1289 }
1290
1291 static void
1292 fd6_mem_to_mem(struct fd_ringbuffer *ring, struct pipe_resource *dst,
1293 unsigned dst_off, struct pipe_resource *src, unsigned src_off,
1294 unsigned sizedwords)
1295 {
1296 struct fd_bo *src_bo = fd_resource(src)->bo;
1297 struct fd_bo *dst_bo = fd_resource(dst)->bo;
1298 unsigned i;
1299
1300 for (i = 0; i < sizedwords; i++) {
1301 OUT_PKT7(ring, CP_MEM_TO_MEM, 5);
1302 OUT_RING(ring, 0x00000000);
1303 OUT_RELOCW(ring, dst_bo, dst_off, 0, 0);
1304 OUT_RELOC (ring, src_bo, src_off, 0, 0);
1305
1306 dst_off += 4;
1307 src_off += 4;
1308 }
1309 }
1310
1311 /* this is *almost* the same as fd6_cache_flush().. which I guess
1312 * could be re-worked to be something a bit more generic w/ param
1313 * indicating what needs to be flushed.. although that would mean
1314 * figuring out which events trigger what state to flush..
1315 */
1316 static void
1317 fd6_framebuffer_barrier(struct fd_context *ctx)
1318 {
1319 struct fd6_context *fd6_ctx = fd6_context(ctx);
1320 struct fd_batch *batch = ctx->batch;
1321 struct fd_ringbuffer *ring = batch->draw;
1322 unsigned seqno;
1323
1324 seqno = fd6_event_write(batch, ring, CACHE_FLUSH_AND_INV_EVENT, true);
1325
1326 OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
1327 OUT_RING(ring, 0x00000013);
1328 OUT_RELOC(ring, control_ptr(fd6_ctx, seqno));
1329 OUT_RING(ring, seqno);
1330 OUT_RING(ring, 0xffffffff);
1331 OUT_RING(ring, 0x00000010);
1332
1333 fd6_event_write(batch, ring, UNK_1D, true);
1334 fd6_event_write(batch, ring, UNK_1C, true);
1335
1336 seqno = fd6_event_write(batch, ring, CACHE_FLUSH_TS, true);
1337
1338 fd6_event_write(batch, ring, 0x31, false);
1339
1340 OUT_PKT7(ring, CP_UNK_A6XX_14, 4);
1341 OUT_RING(ring, 0x00000000);
1342 OUT_RELOC(ring, control_ptr(fd6_ctx, seqno));
1343 OUT_RING(ring, seqno);
1344 }
1345
1346 void
1347 fd6_emit_init_screen(struct pipe_screen *pscreen)
1348 {
1349 struct fd_screen *screen = fd_screen(pscreen);
1350 screen->emit_const = fd6_emit_const;
1351 screen->emit_const_bo = fd6_emit_const_bo;
1352 screen->emit_ib = fd6_emit_ib;
1353 screen->mem_to_mem = fd6_mem_to_mem;
1354 }
1355
1356 void
1357 fd6_emit_init(struct pipe_context *pctx)
1358 {
1359 struct fd_context *ctx = fd_context(pctx);
1360 ctx->framebuffer_barrier = fd6_framebuffer_barrier;
1361 }