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