freedreno/a3xx: handle first/last level properly
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_emit.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_helpers.h"
33 #include "util/u_format.h"
34
35 #include "freedreno_resource.h"
36
37 #include "fd3_emit.h"
38 #include "fd3_blend.h"
39 #include "fd3_context.h"
40 #include "fd3_program.h"
41 #include "fd3_rasterizer.h"
42 #include "fd3_texture.h"
43 #include "fd3_util.h"
44 #include "fd3_zsa.h"
45
46 /* regid: base const register
47 * prsc or dwords: buffer containing constant values
48 * sizedwords: size of const value buffer
49 */
50 void
51 fd3_emit_constant(struct fd_ringbuffer *ring,
52 enum adreno_state_block sb,
53 uint32_t regid, uint32_t offset, uint32_t sizedwords,
54 const uint32_t *dwords, struct pipe_resource *prsc)
55 {
56 uint32_t i, sz;
57 enum adreno_state_src src;
58
59 if (prsc) {
60 sz = 0;
61 src = SS_INDIRECT;
62 } else {
63 sz = sizedwords;
64 src = SS_DIRECT;
65 }
66
67 OUT_PKT3(ring, CP_LOAD_STATE, 2 + sz);
68 OUT_RING(ring, CP_LOAD_STATE_0_DST_OFF(regid/2) |
69 CP_LOAD_STATE_0_STATE_SRC(src) |
70 CP_LOAD_STATE_0_STATE_BLOCK(sb) |
71 CP_LOAD_STATE_0_NUM_UNIT(sizedwords/2));
72 if (prsc) {
73 struct fd_bo *bo = fd_resource(prsc)->bo;
74 OUT_RELOC(ring, bo, offset,
75 CP_LOAD_STATE_1_STATE_TYPE(ST_CONSTANTS), 0);
76 } else {
77 OUT_RING(ring, CP_LOAD_STATE_1_EXT_SRC_ADDR(0) |
78 CP_LOAD_STATE_1_STATE_TYPE(ST_CONSTANTS));
79 dwords = (uint32_t *)&((uint8_t *)dwords)[offset];
80 }
81 for (i = 0; i < sz; i++) {
82 OUT_RING(ring, dwords[i]);
83 }
84 }
85
86 static void
87 emit_constants(struct fd_ringbuffer *ring,
88 enum adreno_state_block sb,
89 struct fd_constbuf_stateobj *constbuf,
90 struct ir3_shader_variant *shader)
91 {
92 uint32_t enabled_mask = constbuf->enabled_mask;
93 uint32_t first_immediate;
94 uint32_t base = 0;
95 unsigned i;
96
97 // XXX TODO only emit dirty consts.. but we need to keep track if
98 // they are clobbered by a clear, gmem2mem, or mem2gmem..
99 constbuf->dirty_mask = enabled_mask;
100
101 /* in particular, with binning shader and a unneeded consts no
102 * longer referenced, we could end up w/ constlen that is smaller
103 * than first_immediate. In that case truncate the user consts
104 * early to avoid HLSQ lockup caused by writing too many consts
105 */
106 first_immediate = MIN2(shader->first_immediate, shader->constlen);
107
108 /* emit user constants: */
109 while (enabled_mask) {
110 unsigned index = ffs(enabled_mask) - 1;
111 struct pipe_constant_buffer *cb = &constbuf->cb[index];
112 unsigned size = align(cb->buffer_size, 4) / 4; /* size in dwords */
113
114 // I expect that size should be a multiple of vec4's:
115 assert(size == align(size, 4));
116
117 /* gallium could leave const buffers bound above what the
118 * current shader uses.. don't let that confuse us.
119 */
120 if (base >= (4 * first_immediate))
121 break;
122
123 if (constbuf->dirty_mask & (1 << index)) {
124 /* and even if the start of the const buffer is before
125 * first_immediate, the end may not be:
126 */
127 size = MIN2(size, (4 * first_immediate) - base);
128 fd3_emit_constant(ring, sb, base,
129 cb->buffer_offset, size,
130 cb->user_buffer, cb->buffer);
131 constbuf->dirty_mask &= ~(1 << index);
132 }
133
134 base += size;
135 enabled_mask &= ~(1 << index);
136 }
137
138 /* emit shader immediates: */
139 if (shader) {
140 for (i = 0; i < shader->immediates_count; i++) {
141 base = 4 * (shader->first_immediate + i);
142 if (base >= (4 * shader->constlen))
143 break;
144 fd3_emit_constant(ring, sb, base,
145 0, 4, shader->immediates[i].val, NULL);
146 }
147 }
148 }
149
150 #define VERT_TEX_OFF 0
151 #define FRAG_TEX_OFF 16
152 #define BASETABLE_SZ A3XX_MAX_MIP_LEVELS
153
154 static void
155 emit_textures(struct fd_ringbuffer *ring,
156 enum adreno_state_block sb,
157 struct fd_texture_stateobj *tex)
158 {
159 static const unsigned tex_off[] = {
160 [SB_VERT_TEX] = VERT_TEX_OFF,
161 [SB_FRAG_TEX] = FRAG_TEX_OFF,
162 };
163 static const enum adreno_state_block mipaddr[] = {
164 [SB_VERT_TEX] = SB_VERT_MIPADDR,
165 [SB_FRAG_TEX] = SB_FRAG_MIPADDR,
166 };
167 unsigned i, j;
168
169 if (tex->num_samplers > 0) {
170 /* output sampler state: */
171 OUT_PKT3(ring, CP_LOAD_STATE, 2 + (2 * tex->num_samplers));
172 OUT_RING(ring, CP_LOAD_STATE_0_DST_OFF(tex_off[sb]) |
173 CP_LOAD_STATE_0_STATE_SRC(SS_DIRECT) |
174 CP_LOAD_STATE_0_STATE_BLOCK(sb) |
175 CP_LOAD_STATE_0_NUM_UNIT(tex->num_samplers));
176 OUT_RING(ring, CP_LOAD_STATE_1_STATE_TYPE(ST_SHADER) |
177 CP_LOAD_STATE_1_EXT_SRC_ADDR(0));
178 for (i = 0; i < tex->num_samplers; i++) {
179 static const struct fd3_sampler_stateobj dummy_sampler = {};
180 const struct fd3_sampler_stateobj *sampler = tex->samplers[i] ?
181 fd3_sampler_stateobj(tex->samplers[i]) :
182 &dummy_sampler;
183 OUT_RING(ring, sampler->texsamp0);
184 OUT_RING(ring, sampler->texsamp1);
185 }
186 }
187
188 if (tex->num_textures > 0) {
189 /* emit texture state: */
190 OUT_PKT3(ring, CP_LOAD_STATE, 2 + (4 * tex->num_textures));
191 OUT_RING(ring, CP_LOAD_STATE_0_DST_OFF(tex_off[sb]) |
192 CP_LOAD_STATE_0_STATE_SRC(SS_DIRECT) |
193 CP_LOAD_STATE_0_STATE_BLOCK(sb) |
194 CP_LOAD_STATE_0_NUM_UNIT(tex->num_textures));
195 OUT_RING(ring, CP_LOAD_STATE_1_STATE_TYPE(ST_CONSTANTS) |
196 CP_LOAD_STATE_1_EXT_SRC_ADDR(0));
197 for (i = 0; i < tex->num_textures; i++) {
198 static const struct fd3_pipe_sampler_view dummy_view = {};
199 const struct fd3_pipe_sampler_view *view = tex->textures[i] ?
200 fd3_pipe_sampler_view(tex->textures[i]) :
201 &dummy_view;
202 OUT_RING(ring, view->texconst0);
203 OUT_RING(ring, view->texconst1);
204 OUT_RING(ring, view->texconst2 |
205 A3XX_TEX_CONST_2_INDX(BASETABLE_SZ * i));
206 OUT_RING(ring, view->texconst3);
207 }
208
209 /* emit mipaddrs: */
210 OUT_PKT3(ring, CP_LOAD_STATE, 2 + (BASETABLE_SZ * tex->num_textures));
211 OUT_RING(ring, CP_LOAD_STATE_0_DST_OFF(BASETABLE_SZ * tex_off[sb]) |
212 CP_LOAD_STATE_0_STATE_SRC(SS_DIRECT) |
213 CP_LOAD_STATE_0_STATE_BLOCK(mipaddr[sb]) |
214 CP_LOAD_STATE_0_NUM_UNIT(BASETABLE_SZ * tex->num_textures));
215 OUT_RING(ring, CP_LOAD_STATE_1_STATE_TYPE(ST_CONSTANTS) |
216 CP_LOAD_STATE_1_EXT_SRC_ADDR(0));
217 for (i = 0; i < tex->num_textures; i++) {
218 static const struct fd3_pipe_sampler_view dummy_view = {
219 .base.u.tex.first_level = 1,
220 };
221 const struct fd3_pipe_sampler_view *view = tex->textures[i] ?
222 fd3_pipe_sampler_view(tex->textures[i]) :
223 &dummy_view;
224 struct fd_resource *rsc = view->tex_resource;
225 unsigned start = view->base.u.tex.first_level;
226 unsigned end = view->base.u.tex.last_level;
227
228 for (j = 0; j < (end - start + 1); j++) {
229 struct fd_resource_slice *slice =
230 fd_resource_slice(rsc, j + start);
231 OUT_RELOC(ring, rsc->bo, slice->offset, 0, 0);
232 }
233
234 /* pad the remaining entries w/ null: */
235 for (; j < BASETABLE_SZ; j++) {
236 OUT_RING(ring, 0x00000000);
237 }
238 }
239 }
240 }
241
242 /* emit texture state for mem->gmem restore operation.. eventually it would
243 * be good to get rid of this and use normal CSO/etc state for more of these
244 * special cases, but for now the compiler is not sufficient..
245 */
246 void
247 fd3_emit_gmem_restore_tex(struct fd_ringbuffer *ring, struct pipe_surface *psurf)
248 {
249 struct fd_resource *rsc = fd_resource(psurf->texture);
250 enum pipe_format format = fd3_gmem_restore_format(psurf->format);
251
252 /* output sampler state: */
253 OUT_PKT3(ring, CP_LOAD_STATE, 4);
254 OUT_RING(ring, CP_LOAD_STATE_0_DST_OFF(FRAG_TEX_OFF) |
255 CP_LOAD_STATE_0_STATE_SRC(SS_DIRECT) |
256 CP_LOAD_STATE_0_STATE_BLOCK(SB_FRAG_TEX) |
257 CP_LOAD_STATE_0_NUM_UNIT(1));
258 OUT_RING(ring, CP_LOAD_STATE_1_STATE_TYPE(ST_SHADER) |
259 CP_LOAD_STATE_1_EXT_SRC_ADDR(0));
260 OUT_RING(ring, A3XX_TEX_SAMP_0_XY_MAG(A3XX_TEX_NEAREST) |
261 A3XX_TEX_SAMP_0_XY_MIN(A3XX_TEX_NEAREST) |
262 A3XX_TEX_SAMP_0_WRAP_S(A3XX_TEX_CLAMP_TO_EDGE) |
263 A3XX_TEX_SAMP_0_WRAP_T(A3XX_TEX_CLAMP_TO_EDGE) |
264 A3XX_TEX_SAMP_0_WRAP_R(A3XX_TEX_REPEAT));
265 OUT_RING(ring, 0x00000000);
266
267 /* emit texture state: */
268 OUT_PKT3(ring, CP_LOAD_STATE, 6);
269 OUT_RING(ring, CP_LOAD_STATE_0_DST_OFF(FRAG_TEX_OFF) |
270 CP_LOAD_STATE_0_STATE_SRC(SS_DIRECT) |
271 CP_LOAD_STATE_0_STATE_BLOCK(SB_FRAG_TEX) |
272 CP_LOAD_STATE_0_NUM_UNIT(1));
273 OUT_RING(ring, CP_LOAD_STATE_1_STATE_TYPE(ST_CONSTANTS) |
274 CP_LOAD_STATE_1_EXT_SRC_ADDR(0));
275 OUT_RING(ring, A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(psurf->format)) |
276 A3XX_TEX_CONST_0_TYPE(A3XX_TEX_2D) |
277 fd3_tex_swiz(format, PIPE_SWIZZLE_RED, PIPE_SWIZZLE_GREEN,
278 PIPE_SWIZZLE_BLUE, PIPE_SWIZZLE_ALPHA));
279 OUT_RING(ring, A3XX_TEX_CONST_1_FETCHSIZE(TFETCH_DISABLE) |
280 A3XX_TEX_CONST_1_WIDTH(psurf->width) |
281 A3XX_TEX_CONST_1_HEIGHT(psurf->height));
282 OUT_RING(ring, A3XX_TEX_CONST_2_PITCH(rsc->slices[0].pitch * rsc->cpp) |
283 A3XX_TEX_CONST_2_INDX(0));
284 OUT_RING(ring, 0x00000000);
285
286 /* emit mipaddrs: */
287 OUT_PKT3(ring, CP_LOAD_STATE, 3);
288 OUT_RING(ring, CP_LOAD_STATE_0_DST_OFF(BASETABLE_SZ * FRAG_TEX_OFF) |
289 CP_LOAD_STATE_0_STATE_SRC(SS_DIRECT) |
290 CP_LOAD_STATE_0_STATE_BLOCK(SB_FRAG_MIPADDR) |
291 CP_LOAD_STATE_0_NUM_UNIT(1));
292 OUT_RING(ring, CP_LOAD_STATE_1_STATE_TYPE(ST_CONSTANTS) |
293 CP_LOAD_STATE_1_EXT_SRC_ADDR(0));
294 OUT_RELOC(ring, rsc->bo, 0, 0, 0);
295 }
296
297 void
298 fd3_emit_vertex_bufs(struct fd_ringbuffer *ring,
299 struct ir3_shader_variant *vp,
300 struct fd3_vertex_buf *vbufs, uint32_t n)
301 {
302 uint32_t i, j, last = 0;
303 uint32_t total_in = 0;
304
305 n = MIN2(n, vp->inputs_count);
306
307 for (i = 0; i < n; i++)
308 if (vp->inputs[i].compmask)
309 last = i;
310
311 for (i = 0, j = 0; i <= last; i++) {
312 if (vp->inputs[i].compmask) {
313 struct pipe_resource *prsc = vbufs[i].prsc;
314 struct fd_resource *rsc = fd_resource(prsc);
315 enum pipe_format pfmt = vbufs[i].format;
316 enum a3xx_vtx_fmt fmt = fd3_pipe2vtx(pfmt);
317 bool switchnext = (i != last);
318 uint32_t fs = util_format_get_blocksize(pfmt);
319
320 debug_assert(fmt != ~0);
321
322 OUT_PKT0(ring, REG_A3XX_VFD_FETCH(j), 2);
323 OUT_RING(ring, A3XX_VFD_FETCH_INSTR_0_FETCHSIZE(fs - 1) |
324 A3XX_VFD_FETCH_INSTR_0_BUFSTRIDE(vbufs[i].stride) |
325 COND(switchnext, A3XX_VFD_FETCH_INSTR_0_SWITCHNEXT) |
326 A3XX_VFD_FETCH_INSTR_0_INDEXCODE(j) |
327 A3XX_VFD_FETCH_INSTR_0_STEPRATE(1));
328 OUT_RELOC(ring, rsc->bo, vbufs[i].offset, 0, 0);
329
330 OUT_PKT0(ring, REG_A3XX_VFD_DECODE_INSTR(j), 1);
331 OUT_RING(ring, A3XX_VFD_DECODE_INSTR_CONSTFILL |
332 A3XX_VFD_DECODE_INSTR_WRITEMASK(vp->inputs[i].compmask) |
333 A3XX_VFD_DECODE_INSTR_FORMAT(fmt) |
334 A3XX_VFD_DECODE_INSTR_SWAP(fd3_pipe2swap(pfmt)) |
335 A3XX_VFD_DECODE_INSTR_REGID(vp->inputs[i].regid) |
336 A3XX_VFD_DECODE_INSTR_SHIFTCNT(fs) |
337 A3XX_VFD_DECODE_INSTR_LASTCOMPVALID |
338 COND(switchnext, A3XX_VFD_DECODE_INSTR_SWITCHNEXT));
339
340 total_in += vp->inputs[i].ncomp;
341 j++;
342 }
343 }
344
345 OUT_PKT0(ring, REG_A3XX_VFD_CONTROL_0, 2);
346 OUT_RING(ring, A3XX_VFD_CONTROL_0_TOTALATTRTOVS(total_in) |
347 A3XX_VFD_CONTROL_0_PACKETSIZE(2) |
348 A3XX_VFD_CONTROL_0_STRMDECINSTRCNT(j) |
349 A3XX_VFD_CONTROL_0_STRMFETCHINSTRCNT(j));
350 OUT_RING(ring, A3XX_VFD_CONTROL_1_MAXSTORAGE(1) | // XXX
351 A3XX_VFD_CONTROL_1_REGID4VTX(regid(63,0)) |
352 A3XX_VFD_CONTROL_1_REGID4INST(regid(63,0)));
353 }
354
355 void
356 fd3_emit_state(struct fd_context *ctx, struct fd_ringbuffer *ring,
357 struct fd_program_stateobj *prog, uint32_t dirty,
358 struct ir3_shader_key key)
359 {
360 struct ir3_shader_variant *vp;
361 struct ir3_shader_variant *fp;
362
363 fp = fd3_shader_variant(prog->fp, key);
364 vp = fd3_shader_variant(prog->vp, key);
365
366 emit_marker(ring, 5);
367
368 if (dirty & FD_DIRTY_SAMPLE_MASK) {
369 OUT_PKT0(ring, REG_A3XX_RB_MSAA_CONTROL, 1);
370 OUT_RING(ring, A3XX_RB_MSAA_CONTROL_DISABLE |
371 A3XX_RB_MSAA_CONTROL_SAMPLES(MSAA_ONE) |
372 A3XX_RB_MSAA_CONTROL_SAMPLE_MASK(ctx->sample_mask));
373 }
374
375 if ((dirty & (FD_DIRTY_ZSA | FD_DIRTY_PROG)) && !key.binning_pass) {
376 uint32_t val = fd3_zsa_stateobj(ctx->zsa)->rb_render_control;
377
378 val |= COND(fp->frag_face, A3XX_RB_RENDER_CONTROL_FACENESS);
379 val |= COND(fp->frag_coord, A3XX_RB_RENDER_CONTROL_XCOORD |
380 A3XX_RB_RENDER_CONTROL_YCOORD |
381 A3XX_RB_RENDER_CONTROL_ZCOORD |
382 A3XX_RB_RENDER_CONTROL_WCOORD);
383
384 /* I suppose if we needed to (which I don't *think* we need
385 * to), we could emit this for binning pass too. But we
386 * would need to keep a different patch-list for binning
387 * vs render pass.
388 */
389
390 OUT_PKT0(ring, REG_A3XX_RB_RENDER_CONTROL, 1);
391 OUT_RINGP(ring, val, &fd3_context(ctx)->rbrc_patches);
392 }
393
394 if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_STENCIL_REF)) {
395 struct fd3_zsa_stateobj *zsa = fd3_zsa_stateobj(ctx->zsa);
396 struct pipe_stencil_ref *sr = &ctx->stencil_ref;
397
398 OUT_PKT0(ring, REG_A3XX_RB_ALPHA_REF, 1);
399 OUT_RING(ring, zsa->rb_alpha_ref);
400
401 OUT_PKT0(ring, REG_A3XX_RB_STENCIL_CONTROL, 1);
402 OUT_RING(ring, zsa->rb_stencil_control);
403
404 OUT_PKT0(ring, REG_A3XX_RB_STENCILREFMASK, 2);
405 OUT_RING(ring, zsa->rb_stencilrefmask |
406 A3XX_RB_STENCILREFMASK_STENCILREF(sr->ref_value[0]));
407 OUT_RING(ring, zsa->rb_stencilrefmask_bf |
408 A3XX_RB_STENCILREFMASK_BF_STENCILREF(sr->ref_value[1]));
409 }
410
411 if (dirty & (FD_DIRTY_ZSA | FD_DIRTY_PROG)) {
412 uint32_t val = fd3_zsa_stateobj(ctx->zsa)->rb_depth_control;
413 if (fp->writes_pos) {
414 val |= A3XX_RB_DEPTH_CONTROL_FRAG_WRITES_Z;
415 val |= A3XX_RB_DEPTH_CONTROL_EARLY_Z_DISABLE;
416 }
417 OUT_PKT0(ring, REG_A3XX_RB_DEPTH_CONTROL, 1);
418 OUT_RING(ring, val);
419 }
420
421 if (dirty & FD_DIRTY_RASTERIZER) {
422 struct fd3_rasterizer_stateobj *rasterizer =
423 fd3_rasterizer_stateobj(ctx->rasterizer);
424
425 OUT_PKT0(ring, REG_A3XX_GRAS_SU_MODE_CONTROL, 1);
426 OUT_RING(ring, rasterizer->gras_su_mode_control);
427
428 OUT_PKT0(ring, REG_A3XX_GRAS_SU_POINT_MINMAX, 2);
429 OUT_RING(ring, rasterizer->gras_su_point_minmax);
430 OUT_RING(ring, rasterizer->gras_su_point_size);
431
432 OUT_PKT0(ring, REG_A3XX_GRAS_SU_POLY_OFFSET_SCALE, 2);
433 OUT_RING(ring, rasterizer->gras_su_poly_offset_scale);
434 OUT_RING(ring, rasterizer->gras_su_poly_offset_offset);
435 }
436
437 if (dirty & (FD_DIRTY_RASTERIZER | FD_DIRTY_PROG)) {
438 uint32_t val = fd3_rasterizer_stateobj(ctx->rasterizer)
439 ->gras_cl_clip_cntl;
440 val |= COND(fp->writes_pos, A3XX_GRAS_CL_CLIP_CNTL_ZCLIP_DISABLE);
441 val |= COND(fp->frag_coord, A3XX_GRAS_CL_CLIP_CNTL_ZCOORD |
442 A3XX_GRAS_CL_CLIP_CNTL_WCOORD);
443 OUT_PKT0(ring, REG_A3XX_GRAS_CL_CLIP_CNTL, 1);
444 OUT_RING(ring, val);
445 }
446
447 if (dirty & (FD_DIRTY_RASTERIZER | FD_DIRTY_PROG)) {
448 uint32_t val = fd3_rasterizer_stateobj(ctx->rasterizer)
449 ->pc_prim_vtx_cntl;
450
451 if (!key.binning_pass) {
452 uint32_t stride_in_vpc = align(fp->total_in, 4) / 4;
453 if (stride_in_vpc > 0)
454 stride_in_vpc = MAX2(stride_in_vpc, 2);
455 val |= A3XX_PC_PRIM_VTX_CNTL_STRIDE_IN_VPC(stride_in_vpc);
456 }
457
458 val |= COND(vp->writes_psize, A3XX_PC_PRIM_VTX_CNTL_PSIZE);
459
460 OUT_PKT0(ring, REG_A3XX_PC_PRIM_VTX_CNTL, 1);
461 OUT_RING(ring, val);
462 }
463
464 if (dirty & FD_DIRTY_SCISSOR) {
465 struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
466
467 OUT_PKT0(ring, REG_A3XX_GRAS_SC_WINDOW_SCISSOR_TL, 2);
468 OUT_RING(ring, A3XX_GRAS_SC_WINDOW_SCISSOR_TL_X(scissor->minx) |
469 A3XX_GRAS_SC_WINDOW_SCISSOR_TL_Y(scissor->miny));
470 OUT_RING(ring, A3XX_GRAS_SC_WINDOW_SCISSOR_BR_X(scissor->maxx - 1) |
471 A3XX_GRAS_SC_WINDOW_SCISSOR_BR_Y(scissor->maxy - 1));
472
473 ctx->max_scissor.minx = MIN2(ctx->max_scissor.minx, scissor->minx);
474 ctx->max_scissor.miny = MIN2(ctx->max_scissor.miny, scissor->miny);
475 ctx->max_scissor.maxx = MAX2(ctx->max_scissor.maxx, scissor->maxx);
476 ctx->max_scissor.maxy = MAX2(ctx->max_scissor.maxy, scissor->maxy);
477 }
478
479 if (dirty & FD_DIRTY_VIEWPORT) {
480 fd_wfi(ctx, ring);
481 OUT_PKT0(ring, REG_A3XX_GRAS_CL_VPORT_XOFFSET, 6);
482 OUT_RING(ring, A3XX_GRAS_CL_VPORT_XOFFSET(ctx->viewport.translate[0] - 0.5));
483 OUT_RING(ring, A3XX_GRAS_CL_VPORT_XSCALE(ctx->viewport.scale[0]));
484 OUT_RING(ring, A3XX_GRAS_CL_VPORT_YOFFSET(ctx->viewport.translate[1] - 0.5));
485 OUT_RING(ring, A3XX_GRAS_CL_VPORT_YSCALE(ctx->viewport.scale[1]));
486 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZOFFSET(ctx->viewport.translate[2]));
487 OUT_RING(ring, A3XX_GRAS_CL_VPORT_ZSCALE(ctx->viewport.scale[2]));
488 }
489
490 if (dirty & FD_DIRTY_PROG) {
491 fd3_program_emit(ring, prog, key);
492 }
493
494 /* TODO we should not need this or fd_wfi() before emit_constants():
495 */
496 OUT_PKT3(ring, CP_EVENT_WRITE, 1);
497 OUT_RING(ring, HLSQ_FLUSH);
498
499 if ((dirty & (FD_DIRTY_PROG | FD_DIRTY_CONSTBUF)) &&
500 /* evil hack to deal sanely with clear path: */
501 (prog == &ctx->prog)) {
502 fd_wfi(ctx, ring);
503 emit_constants(ring, SB_VERT_SHADER,
504 &ctx->constbuf[PIPE_SHADER_VERTEX],
505 (prog->dirty & FD_SHADER_DIRTY_VP) ? vp : NULL);
506 if (!key.binning_pass) {
507 emit_constants(ring, SB_FRAG_SHADER,
508 &ctx->constbuf[PIPE_SHADER_FRAGMENT],
509 (prog->dirty & FD_SHADER_DIRTY_FP) ? fp : NULL);
510 }
511 }
512
513 if ((dirty & FD_DIRTY_BLEND) && ctx->blend) {
514 struct fd3_blend_stateobj *blend = fd3_blend_stateobj(ctx->blend);
515 uint32_t i;
516
517 for (i = 0; i < ARRAY_SIZE(blend->rb_mrt); i++) {
518 OUT_PKT0(ring, REG_A3XX_RB_MRT_CONTROL(i), 1);
519 OUT_RING(ring, blend->rb_mrt[i].control);
520
521 OUT_PKT0(ring, REG_A3XX_RB_MRT_BLEND_CONTROL(i), 1);
522 OUT_RING(ring, blend->rb_mrt[i].blend_control);
523 }
524 }
525
526 if (dirty & FD_DIRTY_BLEND_COLOR) {
527 struct pipe_blend_color *bcolor = &ctx->blend_color;
528 OUT_PKT0(ring, REG_A3XX_RB_BLEND_RED, 4);
529 OUT_RING(ring, A3XX_RB_BLEND_RED_UINT(bcolor->color[0] * 255.0) |
530 A3XX_RB_BLEND_RED_FLOAT(bcolor->color[0]));
531 OUT_RING(ring, A3XX_RB_BLEND_GREEN_UINT(bcolor->color[1] * 255.0) |
532 A3XX_RB_BLEND_GREEN_FLOAT(bcolor->color[1]));
533 OUT_RING(ring, A3XX_RB_BLEND_BLUE_UINT(bcolor->color[2] * 255.0) |
534 A3XX_RB_BLEND_BLUE_FLOAT(bcolor->color[2]));
535 OUT_RING(ring, A3XX_RB_BLEND_ALPHA_UINT(bcolor->color[3] * 255.0) |
536 A3XX_RB_BLEND_ALPHA_FLOAT(bcolor->color[3]));
537 }
538
539 if (dirty & (FD_DIRTY_VERTTEX | FD_DIRTY_FRAGTEX))
540 fd_wfi(ctx, ring);
541
542 if (dirty & FD_DIRTY_VERTTEX) {
543 if (vp->has_samp)
544 emit_textures(ring, SB_VERT_TEX, &ctx->verttex);
545 else
546 dirty &= ~FD_DIRTY_VERTTEX;
547 }
548
549 if (dirty & FD_DIRTY_FRAGTEX) {
550 if (fp->has_samp)
551 emit_textures(ring, SB_FRAG_TEX, &ctx->fragtex);
552 else
553 dirty &= ~FD_DIRTY_FRAGTEX;
554 }
555
556 ctx->dirty &= ~dirty;
557 }
558
559 /* emit setup at begin of new cmdstream buffer (don't rely on previous
560 * state, there could have been a context switch between ioctls):
561 */
562 void
563 fd3_emit_restore(struct fd_context *ctx)
564 {
565 struct fd3_context *fd3_ctx = fd3_context(ctx);
566 struct fd_ringbuffer *ring = ctx->ring;
567 int i;
568
569 if (ctx->screen->gpu_id == 320) {
570 OUT_PKT3(ring, CP_REG_RMW, 3);
571 OUT_RING(ring, REG_A3XX_RBBM_CLOCK_CTL);
572 OUT_RING(ring, 0xfffcffff);
573 OUT_RING(ring, 0x00000000);
574 }
575
576 fd_wfi(ctx, ring);
577 OUT_PKT3(ring, CP_INVALIDATE_STATE, 1);
578 OUT_RING(ring, 0x00007fff);
579
580 OUT_PKT0(ring, REG_A3XX_SP_VS_PVT_MEM_PARAM_REG, 3);
581 OUT_RING(ring, 0x08000001); /* SP_VS_PVT_MEM_CTRL_REG */
582 OUT_RELOC(ring, fd3_ctx->vs_pvt_mem, 0,0,0); /* SP_VS_PVT_MEM_ADDR_REG */
583 OUT_RING(ring, 0x00000000); /* SP_VS_PVT_MEM_SIZE_REG */
584
585 OUT_PKT0(ring, REG_A3XX_SP_FS_PVT_MEM_PARAM_REG, 3);
586 OUT_RING(ring, 0x08000001); /* SP_FS_PVT_MEM_CTRL_REG */
587 OUT_RELOC(ring, fd3_ctx->fs_pvt_mem, 0,0,0); /* SP_FS_PVT_MEM_ADDR_REG */
588 OUT_RING(ring, 0x00000000); /* SP_FS_PVT_MEM_SIZE_REG */
589
590 OUT_PKT0(ring, REG_A3XX_PC_VERTEX_REUSE_BLOCK_CNTL, 1);
591 OUT_RING(ring, 0x0000000b); /* PC_VERTEX_REUSE_BLOCK_CNTL */
592
593 OUT_PKT0(ring, REG_A3XX_GRAS_SC_CONTROL, 1);
594 OUT_RING(ring, A3XX_GRAS_SC_CONTROL_RENDER_MODE(RB_RENDERING_PASS) |
595 A3XX_GRAS_SC_CONTROL_MSAA_SAMPLES(MSAA_ONE) |
596 A3XX_GRAS_SC_CONTROL_RASTER_MODE(0));
597
598 OUT_PKT0(ring, REG_A3XX_RB_MSAA_CONTROL, 2);
599 OUT_RING(ring, A3XX_RB_MSAA_CONTROL_DISABLE |
600 A3XX_RB_MSAA_CONTROL_SAMPLES(MSAA_ONE) |
601 A3XX_RB_MSAA_CONTROL_SAMPLE_MASK(0xffff));
602 OUT_RING(ring, 0x00000000); /* RB_ALPHA_REF */
603
604 OUT_PKT0(ring, REG_A3XX_GRAS_CL_GB_CLIP_ADJ, 1);
605 OUT_RING(ring, A3XX_GRAS_CL_GB_CLIP_ADJ_HORZ(0) |
606 A3XX_GRAS_CL_GB_CLIP_ADJ_VERT(0));
607
608 OUT_PKT0(ring, REG_A3XX_GRAS_TSE_DEBUG_ECO, 1);
609 OUT_RING(ring, 0x00000001); /* GRAS_TSE_DEBUG_ECO */
610
611 OUT_PKT0(ring, REG_A3XX_TPL1_TP_VS_TEX_OFFSET, 1);
612 OUT_RING(ring, A3XX_TPL1_TP_VS_TEX_OFFSET_SAMPLEROFFSET(VERT_TEX_OFF) |
613 A3XX_TPL1_TP_VS_TEX_OFFSET_MEMOBJOFFSET(VERT_TEX_OFF) |
614 A3XX_TPL1_TP_VS_TEX_OFFSET_BASETABLEPTR(BASETABLE_SZ * VERT_TEX_OFF));
615
616 OUT_PKT0(ring, REG_A3XX_TPL1_TP_FS_TEX_OFFSET, 1);
617 OUT_RING(ring, A3XX_TPL1_TP_FS_TEX_OFFSET_SAMPLEROFFSET(FRAG_TEX_OFF) |
618 A3XX_TPL1_TP_FS_TEX_OFFSET_MEMOBJOFFSET(FRAG_TEX_OFF) |
619 A3XX_TPL1_TP_FS_TEX_OFFSET_BASETABLEPTR(BASETABLE_SZ * FRAG_TEX_OFF));
620
621 OUT_PKT0(ring, REG_A3XX_VPC_VARY_CYLWRAP_ENABLE_0, 2);
622 OUT_RING(ring, 0x00000000); /* VPC_VARY_CYLWRAP_ENABLE_0 */
623 OUT_RING(ring, 0x00000000); /* VPC_VARY_CYLWRAP_ENABLE_1 */
624
625 OUT_PKT0(ring, REG_A3XX_UNKNOWN_0E43, 1);
626 OUT_RING(ring, 0x00000001); /* UNKNOWN_0E43 */
627
628 OUT_PKT0(ring, REG_A3XX_UNKNOWN_0F03, 1);
629 OUT_RING(ring, 0x00000001); /* UNKNOWN_0F03 */
630
631 OUT_PKT0(ring, REG_A3XX_UNKNOWN_0EE0, 1);
632 OUT_RING(ring, 0x00000003); /* UNKNOWN_0EE0 */
633
634 OUT_PKT0(ring, REG_A3XX_UNKNOWN_0C3D, 1);
635 OUT_RING(ring, 0x00000001); /* UNKNOWN_0C3D */
636
637 OUT_PKT0(ring, REG_A3XX_HLSQ_PERFCOUNTER0_SELECT, 1);
638 OUT_RING(ring, 0x00000000); /* HLSQ_PERFCOUNTER0_SELECT */
639
640 OUT_PKT0(ring, REG_A3XX_HLSQ_CONST_VSPRESV_RANGE_REG, 2);
641 OUT_RING(ring, A3XX_HLSQ_CONST_VSPRESV_RANGE_REG_STARTENTRY(0) |
642 A3XX_HLSQ_CONST_VSPRESV_RANGE_REG_ENDENTRY(0));
643 OUT_RING(ring, A3XX_HLSQ_CONST_FSPRESV_RANGE_REG_STARTENTRY(0) |
644 A3XX_HLSQ_CONST_FSPRESV_RANGE_REG_ENDENTRY(0));
645
646 OUT_PKT0(ring, REG_A3XX_UCHE_CACHE_INVALIDATE0_REG, 2);
647 OUT_RING(ring, A3XX_UCHE_CACHE_INVALIDATE0_REG_ADDR(0));
648 OUT_RING(ring, A3XX_UCHE_CACHE_INVALIDATE1_REG_ADDR(0) |
649 A3XX_UCHE_CACHE_INVALIDATE1_REG_OPCODE(INVALIDATE) |
650 A3XX_UCHE_CACHE_INVALIDATE1_REG_ENTIRE_CACHE);
651
652 OUT_PKT0(ring, REG_A3XX_GRAS_CL_CLIP_CNTL, 1);
653 OUT_RING(ring, 0x00000000); /* GRAS_CL_CLIP_CNTL */
654
655 OUT_PKT0(ring, REG_A3XX_GRAS_SU_POINT_MINMAX, 2);
656 OUT_RING(ring, 0xffc00010); /* GRAS_SU_POINT_MINMAX */
657 OUT_RING(ring, 0x00000008); /* GRAS_SU_POINT_SIZE */
658
659 OUT_PKT0(ring, REG_A3XX_PC_RESTART_INDEX, 1);
660 OUT_RING(ring, 0xffffffff); /* PC_RESTART_INDEX */
661
662 OUT_PKT0(ring, REG_A3XX_RB_WINDOW_OFFSET, 1);
663 OUT_RING(ring, A3XX_RB_WINDOW_OFFSET_X(0) |
664 A3XX_RB_WINDOW_OFFSET_Y(0));
665
666 OUT_PKT0(ring, REG_A3XX_RB_BLEND_RED, 4);
667 OUT_RING(ring, A3XX_RB_BLEND_RED_UINT(0) |
668 A3XX_RB_BLEND_RED_FLOAT(0.0));
669 OUT_RING(ring, A3XX_RB_BLEND_GREEN_UINT(0) |
670 A3XX_RB_BLEND_GREEN_FLOAT(0.0));
671 OUT_RING(ring, A3XX_RB_BLEND_BLUE_UINT(0) |
672 A3XX_RB_BLEND_BLUE_FLOAT(0.0));
673 OUT_RING(ring, A3XX_RB_BLEND_ALPHA_UINT(0xff) |
674 A3XX_RB_BLEND_ALPHA_FLOAT(1.0));
675
676 for (i = 0; i < 6; i++) {
677 OUT_PKT0(ring, REG_A3XX_GRAS_CL_USER_PLANE(i), 4);
678 OUT_RING(ring, 0x00000000); /* GRAS_CL_USER_PLANE[i].X */
679 OUT_RING(ring, 0x00000000); /* GRAS_CL_USER_PLANE[i].Y */
680 OUT_RING(ring, 0x00000000); /* GRAS_CL_USER_PLANE[i].Z */
681 OUT_RING(ring, 0x00000000); /* GRAS_CL_USER_PLANE[i].W */
682 }
683
684 OUT_PKT0(ring, REG_A3XX_PC_VSTREAM_CONTROL, 1);
685 OUT_RING(ring, 0x00000000);
686
687 fd_event_write(ctx, ring, CACHE_FLUSH);
688
689 if (is_a3xx_p0(ctx->screen)) {
690 OUT_PKT3(ring, CP_DRAW_INDX, 3);
691 OUT_RING(ring, 0x00000000);
692 OUT_RING(ring, DRAW(1, DI_SRC_SEL_AUTO_INDEX,
693 INDEX_SIZE_IGN, IGNORE_VISIBILITY));
694 OUT_RING(ring, 0); /* NumIndices */
695 }
696
697 OUT_PKT3(ring, CP_NOP, 4);
698 OUT_RING(ring, 0x00000000);
699 OUT_RING(ring, 0x00000000);
700 OUT_RING(ring, 0x00000000);
701 OUT_RING(ring, 0x00000000);
702
703 fd_wfi(ctx, ring);
704
705 ctx->needs_rb_fbd = true;
706 }