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