1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 * Christian Gmeiner <christian.gmeiner@gmail.com>
26 */
27
28 #include "etnaviv_state.h"
29
30 #include "hw/common.xml.h"
31
32 #include "etnaviv_blend.h"
33 #include "etnaviv_clear_blit.h"
34 #include "etnaviv_context.h"
35 #include "etnaviv_format.h"
36 #include "etnaviv_rasterizer.h"
37 #include "etnaviv_screen.h"
38 #include "etnaviv_shader.h"
39 #include "etnaviv_surface.h"
40 #include "etnaviv_translate.h"
41 #include "etnaviv_util.h"
42 #include "etnaviv_zsa.h"
43 #include "util/u_framebuffer.h"
44 #include "util/u_helpers.h"
45 #include "util/u_inlines.h"
46 #include "util/u_math.h"
47 #include "util/u_memory.h"
48 #include "util/u_upload_mgr.h"
49
50 static void
etna_set_stencil_ref(struct pipe_context * pctx,const struct pipe_stencil_ref sr)51 etna_set_stencil_ref(struct pipe_context *pctx, const struct pipe_stencil_ref sr)
52 {
53 struct etna_context *ctx = etna_context(pctx);
54 struct compiled_stencil_ref *cs = &ctx->stencil_ref;
55
56 ctx->stencil_ref_s = sr;
57
58 for (unsigned i = 0; i < 2; i++) {
59 cs->PE_STENCIL_CONFIG[i] =
60 VIVS_PE_STENCIL_CONFIG_REF_FRONT(sr.ref_value[i]);
61 cs->PE_STENCIL_CONFIG_EXT[i] =
62 VIVS_PE_STENCIL_CONFIG_EXT_REF_BACK(sr.ref_value[!i]);
63 }
64 ctx->dirty |= ETNA_DIRTY_STENCIL_REF;
65 }
66
67 static void
etna_set_clip_state(struct pipe_context * pctx,const struct pipe_clip_state * pcs)68 etna_set_clip_state(struct pipe_context *pctx, const struct pipe_clip_state *pcs)
69 {
70 /* NOOP */
71 }
72
73 static void
etna_set_sample_mask(struct pipe_context * pctx,unsigned sample_mask)74 etna_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask)
75 {
76 struct etna_context *ctx = etna_context(pctx);
77
78 ctx->sample_mask = sample_mask;
79 ctx->dirty |= ETNA_DIRTY_SAMPLE_MASK;
80 }
81
82 static void
etna_set_constant_buffer(struct pipe_context * pctx,enum pipe_shader_type shader,uint index,bool take_ownership,const struct pipe_constant_buffer * cb)83 etna_set_constant_buffer(struct pipe_context *pctx,
84 enum pipe_shader_type shader, uint index, bool take_ownership,
85 const struct pipe_constant_buffer *cb)
86 {
87 struct etna_context *ctx = etna_context(pctx);
88 struct etna_constbuf_state *so = &ctx->constant_buffer[shader];
89
90 assert(index < ETNA_MAX_CONST_BUF);
91
92 util_copy_constant_buffer(&so->cb[index], cb, take_ownership);
93
94 /* Note that the gallium frontends can unbind constant buffers by
95 * passing NULL here. */
96 if (unlikely(!cb || (!cb->buffer && !cb->user_buffer))) {
97 so->enabled_mask &= ~(1 << index);
98 return;
99 }
100
101 assert(index != 0 || cb->user_buffer != NULL);
102
103 if (!cb->buffer) {
104 struct pipe_constant_buffer *cb = &so->cb[index];
105 u_upload_data(pctx->const_uploader, 0, cb->buffer_size, 16, cb->user_buffer, &cb->buffer_offset, &cb->buffer);
106 }
107
108 so->enabled_mask |= 1 << index;
109 ctx->dirty |= ETNA_DIRTY_CONSTBUF;
110 }
111
112 static void
etna_update_render_surface(struct pipe_context * pctx,struct etna_surface * surf)113 etna_update_render_surface(struct pipe_context *pctx, struct etna_surface *surf)
114 {
115 struct etna_resource *base = etna_resource(surf->prsc);
116 struct etna_resource *to = base, *from = base;
117 unsigned level = surf->base.u.tex.level;
118
119 if (base->texture &&
120 etna_resource_level_newer(&etna_resource(base->texture)->levels[level],
121 surf->level))
122 from = etna_resource(base->texture);
123
124 if (base->render)
125 to = etna_resource(base->render);
126
127 if ((to != from) &&
128 etna_resource_level_older(&to->levels[level], &from->levels[level]))
129 etna_copy_resource(pctx, &to->base, &from->base, level, level);
130 }
131
132 static void
etna_set_framebuffer_state(struct pipe_context * pctx,const struct pipe_framebuffer_state * fb)133 etna_set_framebuffer_state(struct pipe_context *pctx,
134 const struct pipe_framebuffer_state *fb)
135 {
136 struct etna_context *ctx = etna_context(pctx);
137 struct etna_screen *screen = ctx->screen;
138 struct compiled_framebuffer_state *cs = &ctx->framebuffer;
139 int nr_samples_color = -1;
140 int nr_samples_depth = -1;
141 bool target_16bpp = false;
142 bool target_linear = false;
143
144 /* Set up TS as well. Warning: this state is used by both the RS and PE */
145 uint32_t ts_mem_config = 0;
146 uint32_t pe_mem_config = 0;
147 uint32_t pe_logic_op = 0;
148
149 if (fb->nr_cbufs > 0) { /* at least one color buffer? */
150 struct etna_surface *cbuf = etna_surface(fb->cbufs[0]);
151 struct etna_resource *res = etna_resource(cbuf->base.texture);
152 bool color_supertiled = (res->layout & ETNA_LAYOUT_BIT_SUPER) != 0;
153 uint32_t fmt = translate_pe_format(cbuf->base.format);
154
155 assert((res->layout & ETNA_LAYOUT_BIT_TILE) ||
156 VIV_FEATURE(screen, chipMinorFeatures2, LINEAR_PE));
157 etna_update_render_surface(pctx, cbuf);
158
159 if (res->layout == ETNA_LAYOUT_LINEAR)
160 target_linear = true;
161
162 if (fmt >= PE_FORMAT_R16F)
163 cs->PE_COLOR_FORMAT = VIVS_PE_COLOR_FORMAT_FORMAT_EXT(fmt) |
164 VIVS_PE_COLOR_FORMAT_FORMAT_MASK;
165 else
166 cs->PE_COLOR_FORMAT = VIVS_PE_COLOR_FORMAT_FORMAT(fmt);
167
168 if (util_format_get_blocksize(cbuf->base.format) <= 2)
169 target_16bpp = true;
170
171 cs->PE_COLOR_FORMAT |=
172 VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK |
173 COND(color_supertiled, VIVS_PE_COLOR_FORMAT_SUPER_TILED);
174
175 nr_samples_color = cbuf->base.texture->nr_samples;
176 if (nr_samples_color <= 1)
177 cs->PE_COLOR_FORMAT |= VIVS_PE_COLOR_FORMAT_OVERWRITE;
178
179 if (VIV_FEATURE(screen, chipMinorFeatures6, CACHE128B256BPERLINE))
180 cs->PE_COLOR_FORMAT |= COND(color_supertiled, VIVS_PE_COLOR_FORMAT_SUPER_TILED_NEW);
181 /* VIVS_PE_COLOR_FORMAT_COMPONENTS() and
182 * VIVS_PE_COLOR_FORMAT_OVERWRITE comes from blend_state
183 * but only if we set the bits above. */
184 /* merged with depth_stencil_alpha */
185 if ((cbuf->offset & 63) ||
186 (((cbuf->level->stride * 4) & 63) && cbuf->level->height > 4)) {
187 /* XXX Must make temporary surface here.
188 * Need the same mechanism on gc2000 when we want to do mipmap
189 * generation by
190 * rendering to levels > 1 due to multitiled / tiled conversion. */
191 BUG("Alignment error, trying to render to offset %08x with tile "
192 "stride %i",
193 cbuf->offset, cbuf->level->stride * 4);
194 }
195
196 if (screen->specs.halti >= 0 && screen->model != 0x880) {
197 /* Rendertargets on GPUs with more than a single pixel pipe must always
198 * be multi-tiled, or single-buffer mode must be supported */
199 assert(screen->specs.pixel_pipes == 1 ||
200 (res->layout & ETNA_LAYOUT_BIT_MULTI) || screen->specs.single_buffer);
201 for (int i = 0; i < screen->specs.pixel_pipes; i++) {
202 cs->PE_PIPE_COLOR_ADDR[i] = cbuf->reloc[i];
203 cs->PE_PIPE_COLOR_ADDR[i].flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
204 }
205 } else {
206 cs->PE_COLOR_ADDR = cbuf->reloc[0];
207 cs->PE_COLOR_ADDR.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
208 }
209
210 cs->PE_COLOR_STRIDE = cbuf->level->stride;
211
212 if (cbuf->level->ts_size) {
213 cs->TS_COLOR_CLEAR_VALUE = cbuf->level->clear_value;
214 cs->TS_COLOR_CLEAR_VALUE_EXT = cbuf->level->clear_value >> 32;
215
216 cs->TS_COLOR_STATUS_BASE = cbuf->ts_reloc;
217 cs->TS_COLOR_STATUS_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
218
219 cs->TS_COLOR_SURFACE_BASE = cbuf->reloc[0];
220 cs->TS_COLOR_SURFACE_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
221
222 pe_mem_config |= VIVS_PE_MEM_CONFIG_COLOR_TS_MODE(cbuf->level->ts_mode);
223
224 if (cbuf->level->ts_compress_fmt >= 0) {
225 /* overwrite bit breaks v1/v2 compression */
226 if (!screen->specs.v4_compression)
227 cs->PE_COLOR_FORMAT &= ~VIVS_PE_COLOR_FORMAT_OVERWRITE;
228
229 ts_mem_config |=
230 VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION |
231 VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION_FORMAT(cbuf->level->ts_compress_fmt);
232 }
233 }
234
235 if (util_format_is_srgb(cbuf->base.format))
236 pe_logic_op |= VIVS_PE_LOGIC_OP_SRGB;
237
238 cs->PS_CONTROL = COND(util_format_is_unorm(cbuf->base.format), VIVS_PS_CONTROL_SATURATE_RT0);
239 cs->PS_CONTROL_EXT =
240 VIVS_PS_CONTROL_EXT_OUTPUT_MODE0(translate_output_mode(cbuf->base.format, screen->specs.halti >= 5));
241 } else {
242 /* Clearing VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK and
243 * VIVS_PE_COLOR_FORMAT_OVERWRITE prevents us from overwriting the
244 * color target */
245 cs->PE_COLOR_FORMAT = VIVS_PE_COLOR_FORMAT_OVERWRITE;
246 cs->PE_COLOR_STRIDE = 0;
247 cs->TS_COLOR_STATUS_BASE.bo = NULL;
248 cs->TS_COLOR_SURFACE_BASE.bo = NULL;
249
250 cs->PE_COLOR_ADDR = screen->dummy_rt_reloc;
251 for (int i = 0; i < screen->specs.pixel_pipes; i++)
252 cs->PE_PIPE_COLOR_ADDR[i] = screen->dummy_rt_reloc;
253 }
254
255 if (fb->zsbuf != NULL) {
256 struct etna_surface *zsbuf = etna_surface(fb->zsbuf);
257 struct etna_resource *res = etna_resource(zsbuf->base.texture);
258
259 etna_update_render_surface(pctx, zsbuf);
260
261 assert(res->layout &ETNA_LAYOUT_BIT_TILE); /* Cannot render to linear surfaces */
262
263 uint32_t depth_format = translate_depth_format(zsbuf->base.format);
264 unsigned depth_bits =
265 depth_format == VIVS_PE_DEPTH_CONFIG_DEPTH_FORMAT_D16 ? 16 : 24;
266 bool depth_supertiled = (res->layout & ETNA_LAYOUT_BIT_SUPER) != 0;
267
268 if (depth_bits == 16)
269 target_16bpp = true;
270
271 cs->PE_DEPTH_CONFIG =
272 depth_format |
273 COND(depth_supertiled, VIVS_PE_DEPTH_CONFIG_SUPER_TILED) |
274 VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_Z |
275 VIVS_PE_DEPTH_CONFIG_UNK18; /* something to do with clipping? */
276 /* VIVS_PE_DEPTH_CONFIG_ONLY_DEPTH */
277 /* merged with depth_stencil_alpha */
278
279 if (screen->specs.halti >= 0 && screen->model != 0x880) {
280 for (int i = 0; i < screen->specs.pixel_pipes; i++) {
281 cs->PE_PIPE_DEPTH_ADDR[i] = zsbuf->reloc[i];
282 cs->PE_PIPE_DEPTH_ADDR[i].flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
283 }
284 } else {
285 cs->PE_DEPTH_ADDR = zsbuf->reloc[0];
286 cs->PE_DEPTH_ADDR.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
287 }
288
289 cs->PE_DEPTH_STRIDE = zsbuf->level->stride;
290 cs->PE_HDEPTH_CONTROL = VIVS_PE_HDEPTH_CONTROL_FORMAT_DISABLED;
291 cs->PE_DEPTH_NORMALIZE = fui(exp2f(depth_bits) - 1.0f);
292
293 if (zsbuf->level->ts_size) {
294 cs->TS_DEPTH_CLEAR_VALUE = zsbuf->level->clear_value;
295
296 cs->TS_DEPTH_STATUS_BASE = zsbuf->ts_reloc;
297 cs->TS_DEPTH_STATUS_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
298
299 cs->TS_DEPTH_SURFACE_BASE = zsbuf->reloc[0];
300 cs->TS_DEPTH_SURFACE_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
301
302 pe_mem_config |= VIVS_PE_MEM_CONFIG_DEPTH_TS_MODE(zsbuf->level->ts_mode);
303
304 if (zsbuf->level->ts_compress_fmt >= 0) {
305 ts_mem_config |=
306 VIVS_TS_MEM_CONFIG_DEPTH_COMPRESSION |
307 COND(zsbuf->level->ts_compress_fmt == COMPRESSION_FORMAT_D24S8,
308 VIVS_TS_MEM_CONFIG_STENCIL_ENABLE);
309 }
310 }
311
312 ts_mem_config |= COND(depth_bits == 16, VIVS_TS_MEM_CONFIG_DEPTH_16BPP);
313
314 nr_samples_depth = zsbuf->base.texture->nr_samples;
315 } else {
316 cs->PE_DEPTH_CONFIG = VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_NONE;
317 cs->PE_DEPTH_ADDR.bo = NULL;
318 cs->PE_DEPTH_STRIDE = 0;
319 cs->TS_DEPTH_STATUS_BASE.bo = NULL;
320 cs->TS_DEPTH_SURFACE_BASE.bo = NULL;
321
322 for (int i = 0; i < ETNA_MAX_PIXELPIPES; i++)
323 cs->PE_PIPE_DEPTH_ADDR[i].bo = NULL;
324 }
325
326 /* MSAA setup */
327 if (nr_samples_depth != -1 && nr_samples_color != -1 &&
328 nr_samples_depth != nr_samples_color) {
329 BUG("Number of samples in color and depth texture must match (%i and %i respectively)",
330 nr_samples_color, nr_samples_depth);
331 }
332
333 switch (MAX2(nr_samples_depth, nr_samples_color)) {
334 case 0:
335 case 1: /* Are 0 and 1 samples allowed? */
336 cs->GL_MULTI_SAMPLE_CONFIG =
337 VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_NONE;
338 cs->msaa_mode = false;
339 break;
340 case 2:
341 cs->GL_MULTI_SAMPLE_CONFIG = VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_2X;
342 cs->msaa_mode = true; /* Add input to PS */
343 cs->RA_MULTISAMPLE_UNK00E04 = 0x0;
344 cs->RA_MULTISAMPLE_UNK00E10[0] = 0x0000aa22;
345 cs->RA_CENTROID_TABLE[0] = 0x66aa2288;
346 cs->RA_CENTROID_TABLE[1] = 0x88558800;
347 cs->RA_CENTROID_TABLE[2] = 0x88881100;
348 cs->RA_CENTROID_TABLE[3] = 0x33888800;
349 break;
350 case 4:
351 cs->GL_MULTI_SAMPLE_CONFIG = VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_4X;
352 cs->msaa_mode = true; /* Add input to PS */
353 cs->RA_MULTISAMPLE_UNK00E04 = 0x0;
354 cs->RA_MULTISAMPLE_UNK00E10[0] = 0xeaa26e26;
355 cs->RA_MULTISAMPLE_UNK00E10[1] = 0xe6ae622a;
356 cs->RA_MULTISAMPLE_UNK00E10[2] = 0xaaa22a22;
357 cs->RA_CENTROID_TABLE[0] = 0x4a6e2688;
358 cs->RA_CENTROID_TABLE[1] = 0x888888a2;
359 cs->RA_CENTROID_TABLE[2] = 0x888888ea;
360 cs->RA_CENTROID_TABLE[3] = 0x888888c6;
361 cs->RA_CENTROID_TABLE[4] = 0x46622a88;
362 cs->RA_CENTROID_TABLE[5] = 0x888888ae;
363 cs->RA_CENTROID_TABLE[6] = 0x888888e6;
364 cs->RA_CENTROID_TABLE[7] = 0x888888ca;
365 cs->RA_CENTROID_TABLE[8] = 0x262a2288;
366 cs->RA_CENTROID_TABLE[9] = 0x886688a2;
367 cs->RA_CENTROID_TABLE[10] = 0x888866aa;
368 cs->RA_CENTROID_TABLE[11] = 0x668888a6;
369 if (VIV_FEATURE(screen, chipMinorFeatures4, SMALL_MSAA))
370 pe_logic_op |= VIVS_PE_LOGIC_OP_UNK24(0x5);
371 break;
372 }
373
374 cs->TS_MEM_CONFIG = ts_mem_config;
375 cs->PE_MEM_CONFIG = pe_mem_config;
376
377 /* Single buffer setup. There is only one switch for this, not a separate
378 * one per color buffer / depth buffer. To keep the logic simple always use
379 * single buffer when this feature is available.
380 */
381 if (unlikely(target_linear))
382 pe_logic_op |= VIVS_PE_LOGIC_OP_SINGLE_BUFFER(1);
383 else if (screen->specs.single_buffer)
384 pe_logic_op |= VIVS_PE_LOGIC_OP_SINGLE_BUFFER(target_16bpp ? 3 : 2);
385 cs->PE_LOGIC_OP = pe_logic_op;
386
387 /* keep copy of original structure */
388 util_copy_framebuffer_state(&ctx->framebuffer_s, fb);
389 ctx->dirty |= ETNA_DIRTY_FRAMEBUFFER | ETNA_DIRTY_DERIVE_TS;
390 }
391
392 static void
etna_set_polygon_stipple(struct pipe_context * pctx,const struct pipe_poly_stipple * stipple)393 etna_set_polygon_stipple(struct pipe_context *pctx,
394 const struct pipe_poly_stipple *stipple)
395 {
396 /* NOP */
397 }
398
399 static void
etna_set_scissor_states(struct pipe_context * pctx,unsigned start_slot,unsigned num_scissors,const struct pipe_scissor_state * ss)400 etna_set_scissor_states(struct pipe_context *pctx, unsigned start_slot,
401 unsigned num_scissors, const struct pipe_scissor_state *ss)
402 {
403 struct etna_context *ctx = etna_context(pctx);
404 assert(ss->minx <= ss->maxx);
405 assert(ss->miny <= ss->maxy);
406
407 ctx->scissor = *ss;
408 ctx->dirty |= ETNA_DIRTY_SCISSOR;
409 }
410
411 static void
etna_set_viewport_states(struct pipe_context * pctx,unsigned start_slot,unsigned num_scissors,const struct pipe_viewport_state * vs)412 etna_set_viewport_states(struct pipe_context *pctx, unsigned start_slot,
413 unsigned num_scissors, const struct pipe_viewport_state *vs)
414 {
415 struct etna_context *ctx = etna_context(pctx);
416 struct compiled_viewport_state *cs = &ctx->viewport;
417
418 ctx->viewport_s = *vs;
419 /**
420 * For Vivante GPU, viewport z transformation is 0..1 to 0..1 instead of
421 * -1..1 to 0..1.
422 * scaling and translation to 0..1 already happened, so remove that
423 *
424 * z' = (z * 2 - 1) * scale + translate
425 * = z * (2 * scale) + (translate - scale)
426 *
427 * scale' = 2 * scale
428 * translate' = translate - scale
429 */
430
431 /* must be fixp as v4 state deltas assume it is */
432 cs->PA_VIEWPORT_SCALE_X = etna_f32_to_fixp16(vs->scale[0]);
433 cs->PA_VIEWPORT_SCALE_Y = etna_f32_to_fixp16(vs->scale[1]);
434 cs->PA_VIEWPORT_SCALE_Z = fui(vs->scale[2] * 2.0f);
435 cs->PA_VIEWPORT_OFFSET_X = etna_f32_to_fixp16(vs->translate[0]);
436 cs->PA_VIEWPORT_OFFSET_Y = etna_f32_to_fixp16(vs->translate[1]);
437 cs->PA_VIEWPORT_OFFSET_Z = fui(vs->translate[2] - vs->scale[2]);
438
439 /* Compute scissor rectangle (fixp) from viewport.
440 * Make sure left is always < right and top always < bottom.
441 */
442 cs->SE_SCISSOR_LEFT = MAX2(vs->translate[0] - fabsf(vs->scale[0]), 0.0f);
443 cs->SE_SCISSOR_TOP = MAX2(vs->translate[1] - fabsf(vs->scale[1]), 0.0f);
444 cs->SE_SCISSOR_RIGHT = ceilf(MAX2(vs->translate[0] + fabsf(vs->scale[0]), 0.0f));
445 cs->SE_SCISSOR_BOTTOM = ceilf(MAX2(vs->translate[1] + fabsf(vs->scale[1]), 0.0f));
446
447 cs->PE_DEPTH_NEAR = fui(0.0); /* not affected if depth mode is Z (as in GL) */
448 cs->PE_DEPTH_FAR = fui(1.0);
449 ctx->dirty |= ETNA_DIRTY_VIEWPORT;
450 }
451
452 static void
etna_set_vertex_buffers(struct pipe_context * pctx,unsigned num_buffers,const struct pipe_vertex_buffer * vb)453 etna_set_vertex_buffers(struct pipe_context *pctx, unsigned num_buffers,
454 const struct pipe_vertex_buffer *vb)
455 {
456 struct etna_context *ctx = etna_context(pctx);
457 struct etna_vertexbuf_state *so = &ctx->vertex_buffer;
458
459 util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb, num_buffers,
460 true);
461 so->count = util_last_bit(so->enabled_mask);
462
463 for (unsigned idx = 0; idx < num_buffers; ++idx) {
464 struct compiled_set_vertex_buffer *cs = &so->cvb[idx];
465 struct pipe_vertex_buffer *vbi = &so->vb[idx];
466
467 assert(!vbi->is_user_buffer); /* XXX support user_buffer using
468 etna_usermem_map */
469
470 if (vbi->buffer.resource) { /* GPU buffer */
471 cs->FE_VERTEX_STREAM_BASE_ADDR.bo = etna_resource(vbi->buffer.resource)->bo;
472 cs->FE_VERTEX_STREAM_BASE_ADDR.offset = vbi->buffer_offset;
473 cs->FE_VERTEX_STREAM_BASE_ADDR.flags = ETNA_RELOC_READ;
474 } else {
475 cs->FE_VERTEX_STREAM_BASE_ADDR.bo = NULL;
476 }
477 }
478
479 ctx->dirty |= ETNA_DIRTY_VERTEX_BUFFERS;
480 }
481
482 static void
etna_blend_state_bind(struct pipe_context * pctx,void * bs)483 etna_blend_state_bind(struct pipe_context *pctx, void *bs)
484 {
485 struct etna_context *ctx = etna_context(pctx);
486
487 ctx->blend = bs;
488 ctx->dirty |= ETNA_DIRTY_BLEND;
489 }
490
491 static void
etna_blend_state_delete(struct pipe_context * pctx,void * bs)492 etna_blend_state_delete(struct pipe_context *pctx, void *bs)
493 {
494 FREE(bs);
495 }
496
497 static void
etna_rasterizer_state_bind(struct pipe_context * pctx,void * rs)498 etna_rasterizer_state_bind(struct pipe_context *pctx, void *rs)
499 {
500 struct etna_context *ctx = etna_context(pctx);
501
502 ctx->rasterizer = rs;
503 ctx->dirty |= ETNA_DIRTY_RASTERIZER;
504 }
505
506 static void
etna_rasterizer_state_delete(struct pipe_context * pctx,void * rs)507 etna_rasterizer_state_delete(struct pipe_context *pctx, void *rs)
508 {
509 FREE(rs);
510 }
511
512 static void
etna_zsa_state_bind(struct pipe_context * pctx,void * zs)513 etna_zsa_state_bind(struct pipe_context *pctx, void *zs)
514 {
515 struct etna_context *ctx = etna_context(pctx);
516
517 ctx->zsa = zs;
518 ctx->dirty |= ETNA_DIRTY_ZSA;
519 }
520
521 static void
etna_zsa_state_delete(struct pipe_context * pctx,void * zs)522 etna_zsa_state_delete(struct pipe_context *pctx, void *zs)
523 {
524 FREE(zs);
525 }
526
527 /** Create vertex element states, which define a layout for fetching
528 * vertices for rendering.
529 */
530 static void *
etna_vertex_elements_state_create(struct pipe_context * pctx,unsigned num_elements,const struct pipe_vertex_element * elements)531 etna_vertex_elements_state_create(struct pipe_context *pctx,
532 unsigned num_elements, const struct pipe_vertex_element *elements)
533 {
534 struct etna_context *ctx = etna_context(pctx);
535 struct etna_screen *screen = ctx->screen;
536 struct compiled_vertex_elements_state *cs = CALLOC_STRUCT(compiled_vertex_elements_state);
537
538 if (!cs)
539 return NULL;
540
541 if (num_elements > screen->specs.vertex_max_elements) {
542 BUG("number of elements (%u) exceeds chip maximum (%u)", num_elements,
543 screen->specs.vertex_max_elements);
544 FREE(cs);
545 return NULL;
546 }
547
548 /* XXX could minimize number of consecutive stretches here by sorting, and
549 * permuting the inputs in shader or does Mesa do this already? */
550
551 cs->num_elements = num_elements;
552
553 unsigned start_offset = 0; /* start of current consecutive stretch */
554 bool nonconsecutive = true; /* previous value of nonconsecutive */
555 uint32_t buffer_mask = 0; /* mask of buffer_idx already seen */
556
557 for (unsigned idx = 0; idx < num_elements; ++idx) {
558 unsigned buffer_idx = elements[idx].vertex_buffer_index;
559 unsigned element_size = util_format_get_blocksize(elements[idx].src_format);
560 unsigned end_offset = elements[idx].src_offset + element_size;
561 uint32_t format_type, normalize;
562
563 if (nonconsecutive)
564 start_offset = elements[idx].src_offset;
565
566 /* guaranteed by PIPE_CAP_MAX_VERTEX_BUFFERS */
567 assert(buffer_idx < screen->specs.stream_count);
568
569 /* maximum vertex size is 256 bytes */
570 assert(element_size != 0 && (end_offset - start_offset) < 256);
571
572 /* check whether next element is consecutive to this one */
573 nonconsecutive = (idx == (num_elements - 1)) ||
574 elements[idx + 1].vertex_buffer_index != buffer_idx ||
575 end_offset != elements[idx + 1].src_offset;
576
577 format_type = translate_vertex_format_type(elements[idx].src_format);
578 normalize = translate_vertex_format_normalize(elements[idx].src_format);
579
580 assert(format_type != ETNA_NO_MATCH);
581 assert(normalize != ETNA_NO_MATCH);
582
583 if (screen->specs.halti < 5) {
584 cs->FE_VERTEX_ELEMENT_CONFIG[idx] =
585 COND(nonconsecutive, VIVS_FE_VERTEX_ELEMENT_CONFIG_NONCONSECUTIVE) |
586 format_type |
587 VIVS_FE_VERTEX_ELEMENT_CONFIG_NUM(util_format_get_nr_components(elements[idx].src_format)) |
588 normalize | VIVS_FE_VERTEX_ELEMENT_CONFIG_ENDIAN(ENDIAN_MODE_NO_SWAP) |
589 VIVS_FE_VERTEX_ELEMENT_CONFIG_STREAM(buffer_idx) |
590 VIVS_FE_VERTEX_ELEMENT_CONFIG_START(elements[idx].src_offset) |
591 VIVS_FE_VERTEX_ELEMENT_CONFIG_END(end_offset - start_offset);
592 } else { /* HALTI5 spread vertex attrib config over two registers */
593 cs->NFE_GENERIC_ATTRIB_CONFIG0[idx] =
594 format_type |
595 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_NUM(util_format_get_nr_components(elements[idx].src_format)) |
596 normalize | VIVS_NFE_GENERIC_ATTRIB_CONFIG0_ENDIAN(ENDIAN_MODE_NO_SWAP) |
597 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_STREAM(buffer_idx) |
598 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_START(elements[idx].src_offset);
599 cs->NFE_GENERIC_ATTRIB_CONFIG1[idx] =
600 COND(nonconsecutive, VIVS_NFE_GENERIC_ATTRIB_CONFIG1_NONCONSECUTIVE) |
601 VIVS_NFE_GENERIC_ATTRIB_CONFIG1_END(end_offset - start_offset);
602 }
603 cs->FE_VERTEX_STREAM_CONTROL[buffer_idx] =
604 FE_VERTEX_STREAM_CONTROL_VERTEX_STRIDE(elements[idx].src_stride);
605
606 if (util_format_is_pure_integer(elements[idx].src_format))
607 cs->NFE_GENERIC_ATTRIB_SCALE[idx] = 1;
608 else
609 cs->NFE_GENERIC_ATTRIB_SCALE[idx] = fui(1.0f);
610
611 /* instance_divisor is part of elements state but should be the same for all buffers */
612 if (buffer_mask & 1 << buffer_idx)
613 assert(cs->NFE_VERTEX_STREAMS_VERTEX_DIVISOR[buffer_idx] == elements[idx].instance_divisor);
614 else
615 cs->NFE_VERTEX_STREAMS_VERTEX_DIVISOR[buffer_idx] = elements[idx].instance_divisor;
616
617 buffer_mask |= 1 << buffer_idx;
618 cs->num_buffers = MAX2(cs->num_buffers, buffer_idx + 1);
619 }
620
621 return cs;
622 }
623
624 static void
etna_vertex_elements_state_delete(struct pipe_context * pctx,void * ve)625 etna_vertex_elements_state_delete(struct pipe_context *pctx, void *ve)
626 {
627 FREE(ve);
628 }
629
630 static void
etna_vertex_elements_state_bind(struct pipe_context * pctx,void * ve)631 etna_vertex_elements_state_bind(struct pipe_context *pctx, void *ve)
632 {
633 struct etna_context *ctx = etna_context(pctx);
634
635 ctx->vertex_elements = ve;
636 ctx->dirty |= ETNA_DIRTY_VERTEX_ELEMENTS;
637 }
638
639 static void
etna_set_stream_output_targets(struct pipe_context * pctx,unsigned num_targets,struct pipe_stream_output_target ** targets,const unsigned * offsets)640 etna_set_stream_output_targets(struct pipe_context *pctx,
641 unsigned num_targets, struct pipe_stream_output_target **targets,
642 const unsigned *offsets)
643 {
644 /* stub */
645 }
646
647 static bool
etna_update_ts_config(struct etna_context * ctx)648 etna_update_ts_config(struct etna_context *ctx)
649 {
650 uint32_t new_ts_config = ctx->framebuffer.TS_MEM_CONFIG;
651
652 if (ctx->framebuffer_s.nr_cbufs > 0) {
653 struct etna_surface *c_surf = etna_surface(ctx->framebuffer_s.cbufs[0]);
654
655 if (etna_resource_level_ts_valid(c_surf->level)) {
656 new_ts_config |= VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR;
657 } else {
658 new_ts_config &= ~VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR;
659 }
660 }
661
662 if (ctx->framebuffer_s.zsbuf) {
663 struct etna_surface *zs_surf = etna_surface(ctx->framebuffer_s.zsbuf);
664
665 if (etna_resource_level_ts_valid(zs_surf->level)) {
666 new_ts_config |= VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR;
667 } else {
668 new_ts_config &= ~VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR;
669 }
670 }
671
672 if (new_ts_config != ctx->framebuffer.TS_MEM_CONFIG ||
673 (ctx->dirty & ETNA_DIRTY_FRAMEBUFFER)) {
674 ctx->framebuffer.TS_MEM_CONFIG = new_ts_config;
675 ctx->dirty |= ETNA_DIRTY_TS;
676 }
677
678 ctx->dirty &= ~ETNA_DIRTY_DERIVE_TS;
679
680 return true;
681 }
682
683 static bool
etna_update_clipping(struct etna_context * ctx)684 etna_update_clipping(struct etna_context *ctx)
685 {
686 const struct etna_rasterizer_state *rasterizer = etna_rasterizer_state(ctx->rasterizer);
687 const struct pipe_framebuffer_state *fb = &ctx->framebuffer_s;
688
689 /* clip framebuffer against viewport */
690 uint32_t scissor_left = ctx->viewport.SE_SCISSOR_LEFT;
691 uint32_t scissor_top = ctx->viewport.SE_SCISSOR_TOP;
692 uint32_t scissor_right = MIN2(fb->width, ctx->viewport.SE_SCISSOR_RIGHT);
693 uint32_t scissor_bottom = MIN2(fb->height, ctx->viewport.SE_SCISSOR_BOTTOM);
694
695 /* clip against scissor */
696 if (rasterizer->scissor) {
697 scissor_left = MAX2(ctx->scissor.minx, scissor_left);
698 scissor_top = MAX2(ctx->scissor.miny, scissor_top);
699 scissor_right = MIN2(ctx->scissor.maxx, scissor_right);
700 scissor_bottom = MIN2(ctx->scissor.maxy, scissor_bottom);
701 }
702
703 ctx->clipping.minx = scissor_left;
704 ctx->clipping.miny = scissor_top;
705 ctx->clipping.maxx = scissor_right;
706 ctx->clipping.maxy = scissor_bottom;
707
708 ctx->dirty |= ETNA_DIRTY_SCISSOR_CLIP;
709
710 return true;
711 }
712
713 static bool
etna_update_zsa(struct etna_context * ctx)714 etna_update_zsa(struct etna_context *ctx)
715 {
716 struct compiled_shader_state *shader_state = &ctx->shader_state;
717 struct pipe_depth_stencil_alpha_state *zsa_state = ctx->zsa;
718 struct etna_zsa_state *zsa = etna_zsa_state(zsa_state);
719 struct etna_screen *screen = ctx->screen;
720 uint32_t new_pe_depth, new_ra_depth;
721 bool early_z_allowed = !VIV_FEATURE(screen, chipFeatures, NO_EARLY_Z);
722 bool late_zs = false, early_zs = false,
723 late_z_test = false, early_z_test = false;
724
725 /* Linear PE breaks the combination of early test with late write, as it
726 * seems RA and PE disagree about the buffer layout in this mode. Fall back
727 * to late Z always even though early Z write might be possible, as we don't
728 * know if any other draws to the same surface require late Z write.
729 */
730 if (ctx->framebuffer_s.nr_cbufs > 0) {
731 struct etna_surface *cbuf = etna_surface(ctx->framebuffer_s.cbufs[0]);
732 struct etna_resource *res = etna_resource(cbuf->base.texture);
733
734 if (res->layout == ETNA_LAYOUT_LINEAR)
735 early_z_allowed = false;
736 }
737
738 if (zsa->z_write_enabled || zsa->stencil_enabled) {
739 if (VIV_FEATURE(screen, chipMinorFeatures5, RA_WRITE_DEPTH) &&
740 early_z_allowed &&
741 !zsa_state->alpha_enabled &&
742 !shader_state->writes_z &&
743 !shader_state->uses_discard)
744 early_zs = true;
745 else
746 late_zs = true;
747 }
748
749 if (zsa->z_test_enabled) {
750 if (early_z_allowed &&
751 (!zsa->stencil_modified || early_zs) &&
752 !shader_state->writes_z)
753 early_z_test = true;
754 else
755 late_z_test = true;
756 }
757
758 new_pe_depth = VIVS_PE_DEPTH_CONFIG_DEPTH_FUNC(zsa->z_test_enabled ?
759 /* compare funcs have 1 to 1 mapping */
760 zsa_state->depth_func : PIPE_FUNC_ALWAYS) |
761 COND(zsa->z_write_enabled, VIVS_PE_DEPTH_CONFIG_WRITE_ENABLE) |
762 COND(early_z_test, VIVS_PE_DEPTH_CONFIG_EARLY_Z) |
763 COND(!late_zs && !late_z_test,
764 VIVS_PE_DEPTH_CONFIG_DISABLE_ZS);
765
766 /* blob sets this to 0x40000031 on GC7000, seems to make no difference,
767 * but keep it in mind if depth behaves strangely. */
768 new_ra_depth = 0x0000030 |
769 COND(early_z_test, VIVS_RA_EARLY_DEPTH_TEST_ENABLE);
770
771 if (VIV_FEATURE(screen, chipMinorFeatures5, RA_WRITE_DEPTH)) {
772 if (!early_zs)
773 new_ra_depth |= VIVS_RA_EARLY_DEPTH_WRITE_DISABLE;
774 /* The new early hierarchical test seems to only work properly if depth
775 * is also written from the early stage.
776 */
777 if (late_z_test || (early_z_test && late_zs))
778 new_ra_depth |= VIVS_RA_EARLY_DEPTH_HDEPTH_DISABLE;
779
780 if (ctx->framebuffer_s.nr_cbufs > 0) {
781 struct pipe_resource *res = ctx->framebuffer_s.cbufs[0]->texture;
782
783 if ((late_z_test || late_zs) && res->nr_samples > 1)
784 new_ra_depth |= VIVS_RA_EARLY_DEPTH_LATE_DEPTH_MSAA;
785 }
786 }
787
788 if (new_pe_depth != zsa->PE_DEPTH_CONFIG ||
789 new_ra_depth != zsa->RA_DEPTH_CONFIG)
790 ctx->dirty |= ETNA_DIRTY_ZSA;
791
792 zsa->PE_DEPTH_CONFIG = new_pe_depth;
793 zsa->RA_DEPTH_CONFIG = new_ra_depth;
794
795 return true;
796 }
797
798 static bool
etna_record_flush_resources(struct etna_context * ctx)799 etna_record_flush_resources(struct etna_context *ctx)
800 {
801 struct pipe_framebuffer_state *fb = &ctx->framebuffer_s;
802
803 if (fb->nr_cbufs > 0) {
804 struct etna_surface *surf = etna_surface(fb->cbufs[0]);
805 struct etna_resource *rsc = etna_resource(surf->prsc);
806
807 if (rsc->shared && !rsc->explicit_flush)
808 etna_context_add_flush_resource(ctx, surf->prsc);
809 }
810
811 return true;
812 }
813
814 struct etna_state_updater {
815 bool (*update)(struct etna_context *ctx);
816 uint32_t dirty;
817 };
818
819 static const struct etna_state_updater etna_state_updates[] = {
820 {
821 etna_shader_update_vertex, ETNA_DIRTY_SHADER | ETNA_DIRTY_VERTEX_ELEMENTS,
822 },
823 {
824 etna_shader_link, ETNA_DIRTY_SHADER,
825 },
826 {
827 etna_update_blend, ETNA_DIRTY_BLEND | ETNA_DIRTY_FRAMEBUFFER
828 },
829 {
830 etna_update_blend_color, ETNA_DIRTY_BLEND_COLOR | ETNA_DIRTY_FRAMEBUFFER,
831 },
832 {
833 etna_update_ts_config, ETNA_DIRTY_DERIVE_TS,
834 },
835 {
836 etna_update_clipping, ETNA_DIRTY_SCISSOR | ETNA_DIRTY_FRAMEBUFFER |
837 ETNA_DIRTY_RASTERIZER | ETNA_DIRTY_VIEWPORT,
838 },
839 {
840 etna_update_zsa, ETNA_DIRTY_ZSA | ETNA_DIRTY_SHADER |
841 ETNA_DIRTY_FRAMEBUFFER,
842 },
843 {
844 etna_record_flush_resources, ETNA_DIRTY_FRAMEBUFFER,
845 }
846 };
847
848 bool
etna_state_update(struct etna_context * ctx)849 etna_state_update(struct etna_context *ctx)
850 {
851 for (unsigned int i = 0; i < ARRAY_SIZE(etna_state_updates); i++)
852 if (ctx->dirty & etna_state_updates[i].dirty)
853 if (!etna_state_updates[i].update(ctx))
854 return false;
855
856 return true;
857 }
858
859 void
etna_state_init(struct pipe_context * pctx)860 etna_state_init(struct pipe_context *pctx)
861 {
862 pctx->set_blend_color = etna_set_blend_color;
863 pctx->set_stencil_ref = etna_set_stencil_ref;
864 pctx->set_clip_state = etna_set_clip_state;
865 pctx->set_sample_mask = etna_set_sample_mask;
866 pctx->set_constant_buffer = etna_set_constant_buffer;
867 pctx->set_framebuffer_state = etna_set_framebuffer_state;
868 pctx->set_polygon_stipple = etna_set_polygon_stipple;
869 pctx->set_scissor_states = etna_set_scissor_states;
870 pctx->set_viewport_states = etna_set_viewport_states;
871
872 pctx->set_vertex_buffers = etna_set_vertex_buffers;
873
874 pctx->bind_blend_state = etna_blend_state_bind;
875 pctx->delete_blend_state = etna_blend_state_delete;
876
877 pctx->bind_rasterizer_state = etna_rasterizer_state_bind;
878 pctx->delete_rasterizer_state = etna_rasterizer_state_delete;
879
880 pctx->bind_depth_stencil_alpha_state = etna_zsa_state_bind;
881 pctx->delete_depth_stencil_alpha_state = etna_zsa_state_delete;
882
883 pctx->create_vertex_elements_state = etna_vertex_elements_state_create;
884 pctx->delete_vertex_elements_state = etna_vertex_elements_state_delete;
885 pctx->bind_vertex_elements_state = etna_vertex_elements_state_bind;
886
887 pctx->set_stream_output_targets = etna_set_stream_output_targets;
888 }
889