a63350993fefea090a71d332938f39b46b15c2af
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_state.c
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_shader.h"
37 #include "etnaviv_surface.h"
38 #include "etnaviv_translate.h"
39 #include "etnaviv_util.h"
40 #include "util/u_framebuffer.h"
41 #include "util/u_helpers.h"
42 #include "util/u_inlines.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
45
46 static void
47 etna_set_stencil_ref(struct pipe_context *pctx, const struct pipe_stencil_ref *sr)
48 {
49 struct etna_context *ctx = etna_context(pctx);
50 struct compiled_stencil_ref *cs = &ctx->stencil_ref;
51
52 ctx->stencil_ref_s = *sr;
53
54 cs->PE_STENCIL_CONFIG = VIVS_PE_STENCIL_CONFIG_REF_FRONT(sr->ref_value[0]);
55 /* rest of bits weaved in from depth_stencil_alpha */
56 cs->PE_STENCIL_CONFIG_EXT =
57 VIVS_PE_STENCIL_CONFIG_EXT_REF_BACK(sr->ref_value[0]);
58 ctx->dirty |= ETNA_DIRTY_STENCIL_REF;
59 }
60
61 static void
62 etna_set_clip_state(struct pipe_context *pctx, const struct pipe_clip_state *pcs)
63 {
64 /* NOOP */
65 }
66
67 static void
68 etna_set_sample_mask(struct pipe_context *pctx, unsigned sample_mask)
69 {
70 struct etna_context *ctx = etna_context(pctx);
71
72 ctx->sample_mask = sample_mask;
73 ctx->dirty |= ETNA_DIRTY_SAMPLE_MASK;
74 }
75
76 static void
77 etna_set_constant_buffer(struct pipe_context *pctx,
78 enum pipe_shader_type shader, uint index,
79 const struct pipe_constant_buffer *cb)
80 {
81 struct etna_context *ctx = etna_context(pctx);
82
83 if (unlikely(index > 0)) {
84 DBG("Unhandled buffer index %i", index);
85 return;
86 }
87
88
89 util_copy_constant_buffer(&ctx->constant_buffer[shader], cb);
90
91 /* Note that the state tracker can unbind constant buffers by
92 * passing NULL here. */
93 if (unlikely(!cb || (!cb->buffer && !cb->user_buffer)))
94 return;
95
96 /* there is no support for ARB_uniform_buffer_object */
97 assert(cb->buffer == NULL && cb->user_buffer != NULL);
98
99 ctx->dirty |= ETNA_DIRTY_CONSTBUF;
100 }
101
102 static void
103 etna_update_render_resource(struct pipe_context *pctx, struct pipe_resource *pres)
104 {
105 struct etna_resource *res = etna_resource(pres);
106
107 if (res->texture && etna_resource_older(res, etna_resource(res->texture))) {
108 /* The render buffer is older than the texture buffer. Copy it over. */
109 etna_copy_resource(pctx, pres, res->texture, 0, pres->last_level);
110 res->seqno = etna_resource(res->texture)->seqno;
111 }
112 }
113
114 static void
115 etna_set_framebuffer_state(struct pipe_context *pctx,
116 const struct pipe_framebuffer_state *sv)
117 {
118 struct etna_context *ctx = etna_context(pctx);
119 struct compiled_framebuffer_state *cs = &ctx->framebuffer;
120 int nr_samples_color = -1;
121 int nr_samples_depth = -1;
122
123 /* Set up TS as well. Warning: this state is used by both the RS and PE */
124 uint32_t ts_mem_config = 0;
125 uint32_t pe_mem_config = 0;
126
127 if (sv->nr_cbufs > 0) { /* at least one color buffer? */
128 struct etna_surface *cbuf = etna_surface(sv->cbufs[0]);
129 struct etna_resource *res = etna_resource(cbuf->base.texture);
130 bool color_supertiled = (res->layout & ETNA_LAYOUT_BIT_SUPER) != 0;
131
132 assert(res->layout & ETNA_LAYOUT_BIT_TILE); /* Cannot render to linear surfaces */
133 etna_update_render_resource(pctx, cbuf->base.texture);
134
135 cs->PE_COLOR_FORMAT =
136 VIVS_PE_COLOR_FORMAT_FORMAT(translate_rs_format(cbuf->base.format)) |
137 VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK |
138 VIVS_PE_COLOR_FORMAT_OVERWRITE |
139 COND(color_supertiled, VIVS_PE_COLOR_FORMAT_SUPER_TILED) |
140 COND(color_supertiled && ctx->specs.halti >= 5, VIVS_PE_COLOR_FORMAT_SUPER_TILED_NEW);
141 /* VIVS_PE_COLOR_FORMAT_COMPONENTS() and
142 * VIVS_PE_COLOR_FORMAT_OVERWRITE comes from blend_state
143 * but only if we set the bits above. */
144 /* merged with depth_stencil_alpha */
145 if ((cbuf->surf.offset & 63) ||
146 (((cbuf->surf.stride * 4) & 63) && cbuf->surf.height > 4)) {
147 /* XXX Must make temporary surface here.
148 * Need the same mechanism on gc2000 when we want to do mipmap
149 * generation by
150 * rendering to levels > 1 due to multitiled / tiled conversion. */
151 BUG("Alignment error, trying to render to offset %08x with tile "
152 "stride %i",
153 cbuf->surf.offset, cbuf->surf.stride * 4);
154 }
155
156 if (ctx->specs.pixel_pipes == 1) {
157 cs->PE_COLOR_ADDR = cbuf->reloc[0];
158 cs->PE_COLOR_ADDR.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
159 } else {
160 /* Rendered textures must always be multi-tiled, or single-buffer mode must be supported */
161 assert((res->layout & ETNA_LAYOUT_BIT_MULTI) || ctx->specs.single_buffer);
162 for (int i = 0; i < ctx->specs.pixel_pipes; i++) {
163 cs->PE_PIPE_COLOR_ADDR[i] = cbuf->reloc[i];
164 cs->PE_PIPE_COLOR_ADDR[i].flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
165 }
166 }
167 cs->PE_COLOR_STRIDE = cbuf->surf.stride;
168
169 if (cbuf->surf.ts_size) {
170 cs->TS_COLOR_CLEAR_VALUE = cbuf->level->clear_value;
171
172 cs->TS_COLOR_STATUS_BASE = cbuf->ts_reloc;
173 cs->TS_COLOR_STATUS_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
174
175 cs->TS_COLOR_SURFACE_BASE = cbuf->reloc[0];
176 cs->TS_COLOR_SURFACE_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
177
178 pe_mem_config |= VIVS_PE_MEM_CONFIG_COLOR_TS_MODE(cbuf->level->ts_mode);
179 }
180
181 /* MSAA */
182 if (cbuf->base.texture->nr_samples > 1)
183 ts_mem_config |=
184 VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION | translate_msaa_format(cbuf->base.format);
185
186 nr_samples_color = cbuf->base.texture->nr_samples;
187 } else {
188 /* Clearing VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK and
189 * VIVS_PE_COLOR_FORMAT_OVERWRITE prevents us from overwriting the
190 * color target */
191 cs->PE_COLOR_FORMAT = VIVS_PE_COLOR_FORMAT_OVERWRITE;
192 cs->PE_COLOR_STRIDE = 0;
193 cs->TS_COLOR_STATUS_BASE.bo = NULL;
194 cs->TS_COLOR_SURFACE_BASE.bo = NULL;
195
196 cs->PE_COLOR_ADDR = ctx->dummy_rt_reloc;
197 for (int i = 0; i < ctx->specs.pixel_pipes; i++)
198 cs->PE_PIPE_COLOR_ADDR[i] = ctx->dummy_rt_reloc;
199 }
200
201 if (sv->zsbuf != NULL) {
202 struct etna_surface *zsbuf = etna_surface(sv->zsbuf);
203 struct etna_resource *res = etna_resource(zsbuf->base.texture);
204
205 etna_update_render_resource(pctx, zsbuf->base.texture);
206
207 assert(res->layout &ETNA_LAYOUT_BIT_TILE); /* Cannot render to linear surfaces */
208
209 uint32_t depth_format = translate_depth_format(zsbuf->base.format);
210 unsigned depth_bits =
211 depth_format == VIVS_PE_DEPTH_CONFIG_DEPTH_FORMAT_D16 ? 16 : 24;
212 bool depth_supertiled = (res->layout & ETNA_LAYOUT_BIT_SUPER) != 0;
213
214 cs->PE_DEPTH_CONFIG =
215 depth_format |
216 COND(depth_supertiled, VIVS_PE_DEPTH_CONFIG_SUPER_TILED) |
217 VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_Z |
218 COND(ctx->specs.halti >= 5, VIVS_PE_DEPTH_CONFIG_DISABLE_ZS) /* Needs to be enabled on GC7000, otherwise depth writes hang w/ TS - apparently it does something else now */
219 ;
220 /* VIVS_PE_DEPTH_CONFIG_ONLY_DEPTH */
221 /* merged with depth_stencil_alpha */
222
223 if (ctx->specs.pixel_pipes == 1) {
224 cs->PE_DEPTH_ADDR = zsbuf->reloc[0];
225 cs->PE_DEPTH_ADDR.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
226 } else {
227 for (int i = 0; i < ctx->specs.pixel_pipes; i++) {
228 cs->PE_PIPE_DEPTH_ADDR[i] = zsbuf->reloc[i];
229 cs->PE_PIPE_DEPTH_ADDR[i].flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
230 }
231 }
232
233 cs->PE_DEPTH_STRIDE = zsbuf->surf.stride;
234 cs->PE_HDEPTH_CONTROL = VIVS_PE_HDEPTH_CONTROL_FORMAT_DISABLED;
235 cs->PE_DEPTH_NORMALIZE = fui(exp2f(depth_bits) - 1.0f);
236
237 if (zsbuf->surf.ts_size) {
238 cs->TS_DEPTH_CLEAR_VALUE = zsbuf->level->clear_value;
239
240 cs->TS_DEPTH_STATUS_BASE = zsbuf->ts_reloc;
241 cs->TS_DEPTH_STATUS_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
242
243 cs->TS_DEPTH_SURFACE_BASE = zsbuf->reloc[0];
244 cs->TS_DEPTH_SURFACE_BASE.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
245
246 pe_mem_config |= VIVS_PE_MEM_CONFIG_DEPTH_TS_MODE(zsbuf->level->ts_mode);
247 }
248
249 ts_mem_config |= COND(depth_bits == 16, VIVS_TS_MEM_CONFIG_DEPTH_16BPP);
250
251 /* MSAA */
252 if (zsbuf->base.texture->nr_samples > 1)
253 /* XXX VIVS_TS_MEM_CONFIG_DEPTH_COMPRESSION;
254 * Disable without MSAA for now, as it causes corruption in glquake. */
255 ts_mem_config |= VIVS_TS_MEM_CONFIG_DEPTH_COMPRESSION;
256
257 nr_samples_depth = zsbuf->base.texture->nr_samples;
258 } else {
259 cs->PE_DEPTH_CONFIG = VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_NONE;
260 cs->PE_DEPTH_ADDR.bo = NULL;
261 cs->PE_DEPTH_STRIDE = 0;
262 cs->TS_DEPTH_STATUS_BASE.bo = NULL;
263 cs->TS_DEPTH_SURFACE_BASE.bo = NULL;
264
265 for (int i = 0; i < ETNA_MAX_PIXELPIPES; i++)
266 cs->PE_PIPE_DEPTH_ADDR[i].bo = NULL;
267 }
268
269 /* MSAA setup */
270 if (nr_samples_depth != -1 && nr_samples_color != -1 &&
271 nr_samples_depth != nr_samples_color) {
272 BUG("Number of samples in color and depth texture must match (%i and %i respectively)",
273 nr_samples_color, nr_samples_depth);
274 }
275
276 switch (MAX2(nr_samples_depth, nr_samples_color)) {
277 case 0:
278 case 1: /* Are 0 and 1 samples allowed? */
279 cs->GL_MULTI_SAMPLE_CONFIG =
280 VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_NONE;
281 cs->msaa_mode = false;
282 break;
283 case 2:
284 cs->GL_MULTI_SAMPLE_CONFIG = VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_2X;
285 cs->msaa_mode = true; /* Add input to PS */
286 cs->RA_MULTISAMPLE_UNK00E04 = 0x0;
287 cs->RA_MULTISAMPLE_UNK00E10[0] = 0x0000aa22;
288 cs->RA_CENTROID_TABLE[0] = 0x66aa2288;
289 cs->RA_CENTROID_TABLE[1] = 0x88558800;
290 cs->RA_CENTROID_TABLE[2] = 0x88881100;
291 cs->RA_CENTROID_TABLE[3] = 0x33888800;
292 break;
293 case 4:
294 cs->GL_MULTI_SAMPLE_CONFIG = VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_4X;
295 cs->msaa_mode = true; /* Add input to PS */
296 cs->RA_MULTISAMPLE_UNK00E04 = 0x0;
297 cs->RA_MULTISAMPLE_UNK00E10[0] = 0xeaa26e26;
298 cs->RA_MULTISAMPLE_UNK00E10[1] = 0xe6ae622a;
299 cs->RA_MULTISAMPLE_UNK00E10[2] = 0xaaa22a22;
300 cs->RA_CENTROID_TABLE[0] = 0x4a6e2688;
301 cs->RA_CENTROID_TABLE[1] = 0x888888a2;
302 cs->RA_CENTROID_TABLE[2] = 0x888888ea;
303 cs->RA_CENTROID_TABLE[3] = 0x888888c6;
304 cs->RA_CENTROID_TABLE[4] = 0x46622a88;
305 cs->RA_CENTROID_TABLE[5] = 0x888888ae;
306 cs->RA_CENTROID_TABLE[6] = 0x888888e6;
307 cs->RA_CENTROID_TABLE[7] = 0x888888ca;
308 cs->RA_CENTROID_TABLE[8] = 0x262a2288;
309 cs->RA_CENTROID_TABLE[9] = 0x886688a2;
310 cs->RA_CENTROID_TABLE[10] = 0x888866aa;
311 cs->RA_CENTROID_TABLE[11] = 0x668888a6;
312 break;
313 }
314
315 /* Scissor setup */
316 cs->SE_SCISSOR_LEFT = 0; /* affected by rasterizer and scissor state as well */
317 cs->SE_SCISSOR_TOP = 0;
318 cs->SE_SCISSOR_RIGHT = (sv->width << 16) + ETNA_SE_SCISSOR_MARGIN_RIGHT;
319 cs->SE_SCISSOR_BOTTOM = (sv->height << 16) + ETNA_SE_SCISSOR_MARGIN_BOTTOM;
320 cs->SE_CLIP_RIGHT = (sv->width << 16) + ETNA_SE_CLIP_MARGIN_RIGHT;
321 cs->SE_CLIP_BOTTOM = (sv->height << 16) + ETNA_SE_CLIP_MARGIN_BOTTOM;
322
323 cs->TS_MEM_CONFIG = ts_mem_config;
324 cs->PE_MEM_CONFIG = pe_mem_config;
325
326 /* Single buffer setup. There is only one switch for this, not a separate
327 * one per color buffer / depth buffer. To keep the logic simple always use
328 * single buffer when this feature is available.
329 */
330 cs->PE_LOGIC_OP = VIVS_PE_LOGIC_OP_SINGLE_BUFFER(ctx->specs.single_buffer ? 3 : 0);
331
332 /* keep copy of original structure */
333 util_copy_framebuffer_state(&ctx->framebuffer_s, sv);
334 ctx->dirty |= ETNA_DIRTY_FRAMEBUFFER | ETNA_DIRTY_DERIVE_TS;
335 }
336
337 static void
338 etna_set_polygon_stipple(struct pipe_context *pctx,
339 const struct pipe_poly_stipple *stipple)
340 {
341 /* NOP */
342 }
343
344 static void
345 etna_set_scissor_states(struct pipe_context *pctx, unsigned start_slot,
346 unsigned num_scissors, const struct pipe_scissor_state *ss)
347 {
348 struct etna_context *ctx = etna_context(pctx);
349 struct compiled_scissor_state *cs = &ctx->scissor;
350 assert(ss->minx <= ss->maxx);
351 assert(ss->miny <= ss->maxy);
352
353 /* note that this state is only used when rasterizer_state->scissor is on */
354 ctx->scissor_s = *ss;
355 cs->SE_SCISSOR_LEFT = (ss->minx << 16);
356 cs->SE_SCISSOR_TOP = (ss->miny << 16);
357 cs->SE_SCISSOR_RIGHT = (ss->maxx << 16) + ETNA_SE_SCISSOR_MARGIN_RIGHT;
358 cs->SE_SCISSOR_BOTTOM = (ss->maxy << 16) + ETNA_SE_SCISSOR_MARGIN_BOTTOM;
359 cs->SE_CLIP_RIGHT = (ss->maxx << 16) + ETNA_SE_CLIP_MARGIN_RIGHT;
360 cs->SE_CLIP_BOTTOM = (ss->maxy << 16) + ETNA_SE_CLIP_MARGIN_BOTTOM;
361
362 ctx->dirty |= ETNA_DIRTY_SCISSOR;
363 }
364
365 static void
366 etna_set_viewport_states(struct pipe_context *pctx, unsigned start_slot,
367 unsigned num_scissors, const struct pipe_viewport_state *vs)
368 {
369 struct etna_context *ctx = etna_context(pctx);
370 struct compiled_viewport_state *cs = &ctx->viewport;
371
372 ctx->viewport_s = *vs;
373 /**
374 * For Vivante GPU, viewport z transformation is 0..1 to 0..1 instead of
375 * -1..1 to 0..1.
376 * scaling and translation to 0..1 already happened, so remove that
377 *
378 * z' = (z * 2 - 1) * scale + translate
379 * = z * (2 * scale) + (translate - scale)
380 *
381 * scale' = 2 * scale
382 * translate' = translate - scale
383 */
384
385 /* must be fixp as v4 state deltas assume it is */
386 cs->PA_VIEWPORT_SCALE_X = etna_f32_to_fixp16(vs->scale[0]);
387 cs->PA_VIEWPORT_SCALE_Y = etna_f32_to_fixp16(vs->scale[1]);
388 cs->PA_VIEWPORT_SCALE_Z = fui(vs->scale[2] * 2.0f);
389 cs->PA_VIEWPORT_OFFSET_X = etna_f32_to_fixp16(vs->translate[0]);
390 cs->PA_VIEWPORT_OFFSET_Y = etna_f32_to_fixp16(vs->translate[1]);
391 cs->PA_VIEWPORT_OFFSET_Z = fui(vs->translate[2] - vs->scale[2]);
392
393 /* Compute scissor rectangle (fixp) from viewport.
394 * Make sure left is always < right and top always < bottom.
395 */
396 cs->SE_SCISSOR_LEFT = etna_f32_to_fixp16(MAX2(vs->translate[0] - fabsf(vs->scale[0]), 0.0f));
397 cs->SE_SCISSOR_TOP = etna_f32_to_fixp16(MAX2(vs->translate[1] - fabsf(vs->scale[1]), 0.0f));
398 uint32_t right_fixp = etna_f32_to_fixp16(MAX2(vs->translate[0] + fabsf(vs->scale[0]), 0.0f));
399 uint32_t bottom_fixp = etna_f32_to_fixp16(MAX2(vs->translate[1] + fabsf(vs->scale[1]), 0.0f));
400 cs->SE_SCISSOR_RIGHT = right_fixp + ETNA_SE_SCISSOR_MARGIN_RIGHT;
401 cs->SE_SCISSOR_BOTTOM = bottom_fixp + ETNA_SE_SCISSOR_MARGIN_BOTTOM;
402 cs->SE_CLIP_RIGHT = right_fixp + ETNA_SE_CLIP_MARGIN_RIGHT;
403 cs->SE_CLIP_BOTTOM = bottom_fixp + ETNA_SE_CLIP_MARGIN_BOTTOM;
404
405 cs->PE_DEPTH_NEAR = fui(0.0); /* not affected if depth mode is Z (as in GL) */
406 cs->PE_DEPTH_FAR = fui(1.0);
407 ctx->dirty |= ETNA_DIRTY_VIEWPORT;
408 }
409
410 static void
411 etna_set_vertex_buffers(struct pipe_context *pctx, unsigned start_slot,
412 unsigned num_buffers, const struct pipe_vertex_buffer *vb)
413 {
414 struct etna_context *ctx = etna_context(pctx);
415 struct etna_vertexbuf_state *so = &ctx->vertex_buffer;
416
417 util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, vb, start_slot, num_buffers);
418 so->count = util_last_bit(so->enabled_mask);
419
420 for (unsigned idx = start_slot; idx < start_slot + num_buffers; ++idx) {
421 struct compiled_set_vertex_buffer *cs = &so->cvb[idx];
422 struct pipe_vertex_buffer *vbi = &so->vb[idx];
423
424 assert(!vbi->is_user_buffer); /* XXX support user_buffer using
425 etna_usermem_map */
426
427 if (vbi->buffer.resource) { /* GPU buffer */
428 cs->FE_VERTEX_STREAM_BASE_ADDR.bo = etna_resource(vbi->buffer.resource)->bo;
429 cs->FE_VERTEX_STREAM_BASE_ADDR.offset = vbi->buffer_offset;
430 cs->FE_VERTEX_STREAM_BASE_ADDR.flags = ETNA_RELOC_READ;
431 cs->FE_VERTEX_STREAM_CONTROL =
432 FE_VERTEX_STREAM_CONTROL_VERTEX_STRIDE(vbi->stride);
433 } else {
434 cs->FE_VERTEX_STREAM_BASE_ADDR.bo = NULL;
435 cs->FE_VERTEX_STREAM_CONTROL = 0;
436 }
437 }
438
439 ctx->dirty |= ETNA_DIRTY_VERTEX_BUFFERS;
440 }
441
442 static void
443 etna_blend_state_bind(struct pipe_context *pctx, void *bs)
444 {
445 struct etna_context *ctx = etna_context(pctx);
446
447 ctx->blend = bs;
448 ctx->dirty |= ETNA_DIRTY_BLEND;
449 }
450
451 static void
452 etna_blend_state_delete(struct pipe_context *pctx, void *bs)
453 {
454 FREE(bs);
455 }
456
457 static void
458 etna_rasterizer_state_bind(struct pipe_context *pctx, void *rs)
459 {
460 struct etna_context *ctx = etna_context(pctx);
461
462 ctx->rasterizer = rs;
463 ctx->dirty |= ETNA_DIRTY_RASTERIZER;
464 }
465
466 static void
467 etna_rasterizer_state_delete(struct pipe_context *pctx, void *rs)
468 {
469 FREE(rs);
470 }
471
472 static void
473 etna_zsa_state_bind(struct pipe_context *pctx, void *zs)
474 {
475 struct etna_context *ctx = etna_context(pctx);
476
477 ctx->zsa = zs;
478 ctx->dirty |= ETNA_DIRTY_ZSA;
479 }
480
481 static void
482 etna_zsa_state_delete(struct pipe_context *pctx, void *zs)
483 {
484 FREE(zs);
485 }
486
487 /** Create vertex element states, which define a layout for fetching
488 * vertices for rendering.
489 */
490 static void *
491 etna_vertex_elements_state_create(struct pipe_context *pctx,
492 unsigned num_elements, const struct pipe_vertex_element *elements)
493 {
494 struct etna_context *ctx = etna_context(pctx);
495 struct compiled_vertex_elements_state *cs = CALLOC_STRUCT(compiled_vertex_elements_state);
496
497 if (!cs)
498 return NULL;
499
500 if (num_elements > ctx->specs.vertex_max_elements) {
501 BUG("number of elements (%u) exceeds chip maximum (%u)", num_elements,
502 ctx->specs.vertex_max_elements);
503 return NULL;
504 }
505
506 /* XXX could minimize number of consecutive stretches here by sorting, and
507 * permuting the inputs in shader or does Mesa do this already? */
508
509 /* Check that vertex element binding is compatible with hardware; thus
510 * elements[idx].vertex_buffer_index are < stream_count. If not, the binding
511 * uses more streams than is supported, and u_vbuf should have done some
512 * reorganization for compatibility. */
513
514 /* TODO: does mesa this for us? */
515 bool incompatible = false;
516 for (unsigned idx = 0; idx < num_elements; ++idx) {
517 if (elements[idx].vertex_buffer_index >= ctx->specs.stream_count || elements[idx].instance_divisor > 0)
518 incompatible = true;
519 }
520
521 cs->num_elements = num_elements;
522 if (incompatible || num_elements == 0) {
523 DBG("Error: zero vertex elements, or more vertex buffers used than supported");
524 FREE(cs);
525 return NULL;
526 }
527
528 unsigned start_offset = 0; /* start of current consecutive stretch */
529 bool nonconsecutive = true; /* previous value of nonconsecutive */
530
531 for (unsigned idx = 0; idx < num_elements; ++idx) {
532 unsigned element_size = util_format_get_blocksize(elements[idx].src_format);
533 unsigned end_offset = elements[idx].src_offset + element_size;
534 uint32_t format_type, normalize;
535
536 if (nonconsecutive)
537 start_offset = elements[idx].src_offset;
538
539 /* maximum vertex size is 256 bytes */
540 assert(element_size != 0 && end_offset <= 256);
541
542 /* check whether next element is consecutive to this one */
543 nonconsecutive = (idx == (num_elements - 1)) ||
544 elements[idx + 1].vertex_buffer_index != elements[idx].vertex_buffer_index ||
545 end_offset != elements[idx + 1].src_offset;
546
547 format_type = translate_vertex_format_type(elements[idx].src_format);
548 normalize = translate_vertex_format_normalize(elements[idx].src_format);
549
550 assert(format_type != ETNA_NO_MATCH);
551 assert(normalize != ETNA_NO_MATCH);
552
553 if (ctx->specs.halti < 5) {
554 cs->FE_VERTEX_ELEMENT_CONFIG[idx] =
555 COND(nonconsecutive, VIVS_FE_VERTEX_ELEMENT_CONFIG_NONCONSECUTIVE) |
556 format_type |
557 VIVS_FE_VERTEX_ELEMENT_CONFIG_NUM(util_format_get_nr_components(elements[idx].src_format)) |
558 normalize | VIVS_FE_VERTEX_ELEMENT_CONFIG_ENDIAN(ENDIAN_MODE_NO_SWAP) |
559 VIVS_FE_VERTEX_ELEMENT_CONFIG_STREAM(elements[idx].vertex_buffer_index) |
560 VIVS_FE_VERTEX_ELEMENT_CONFIG_START(elements[idx].src_offset) |
561 VIVS_FE_VERTEX_ELEMENT_CONFIG_END(end_offset - start_offset);
562 } else { /* HALTI5 spread vertex attrib config over two registers */
563 cs->NFE_GENERIC_ATTRIB_CONFIG0[idx] =
564 format_type |
565 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_NUM(util_format_get_nr_components(elements[idx].src_format)) |
566 normalize | VIVS_NFE_GENERIC_ATTRIB_CONFIG0_ENDIAN(ENDIAN_MODE_NO_SWAP) |
567 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_STREAM(elements[idx].vertex_buffer_index) |
568 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_START(elements[idx].src_offset);
569 cs->NFE_GENERIC_ATTRIB_CONFIG1[idx] =
570 COND(nonconsecutive, VIVS_NFE_GENERIC_ATTRIB_CONFIG1_NONCONSECUTIVE) |
571 VIVS_NFE_GENERIC_ATTRIB_CONFIG1_END(end_offset - start_offset);
572 }
573 cs->NFE_GENERIC_ATTRIB_SCALE[idx] = 0x3f800000; /* 1 for integer, 1.0 for float */
574 }
575
576 return cs;
577 }
578
579 static void
580 etna_vertex_elements_state_delete(struct pipe_context *pctx, void *ve)
581 {
582 FREE(ve);
583 }
584
585 static void
586 etna_vertex_elements_state_bind(struct pipe_context *pctx, void *ve)
587 {
588 struct etna_context *ctx = etna_context(pctx);
589
590 ctx->vertex_elements = ve;
591 ctx->dirty |= ETNA_DIRTY_VERTEX_ELEMENTS;
592 }
593
594 static bool
595 etna_update_ts_config(struct etna_context *ctx)
596 {
597 uint32_t new_ts_config = ctx->framebuffer.TS_MEM_CONFIG;
598
599 if (ctx->framebuffer_s.nr_cbufs > 0) {
600 struct etna_surface *c_surf = etna_surface(ctx->framebuffer_s.cbufs[0]);
601
602 if(c_surf->level->ts_size && c_surf->level->ts_valid) {
603 new_ts_config |= VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR;
604 } else {
605 new_ts_config &= ~VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR;
606 }
607 }
608
609 if (ctx->framebuffer_s.zsbuf) {
610 struct etna_surface *zs_surf = etna_surface(ctx->framebuffer_s.zsbuf);
611
612 if(zs_surf->level->ts_size && zs_surf->level->ts_valid) {
613 new_ts_config |= VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR;
614 } else {
615 new_ts_config &= ~VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR;
616 }
617 }
618
619 if (new_ts_config != ctx->framebuffer.TS_MEM_CONFIG ||
620 (ctx->dirty & ETNA_DIRTY_FRAMEBUFFER)) {
621 ctx->framebuffer.TS_MEM_CONFIG = new_ts_config;
622 ctx->dirty |= ETNA_DIRTY_TS;
623 }
624
625 ctx->dirty &= ~ETNA_DIRTY_DERIVE_TS;
626
627 return true;
628 }
629
630 struct etna_state_updater {
631 bool (*update)(struct etna_context *ctx);
632 uint32_t dirty;
633 };
634
635 static const struct etna_state_updater etna_state_updates[] = {
636 {
637 etna_shader_update_vertex, ETNA_DIRTY_SHADER | ETNA_DIRTY_VERTEX_ELEMENTS,
638 },
639 {
640 etna_shader_link, ETNA_DIRTY_SHADER,
641 },
642 {
643 etna_update_blend, ETNA_DIRTY_BLEND | ETNA_DIRTY_FRAMEBUFFER
644 },
645 {
646 etna_update_blend_color, ETNA_DIRTY_BLEND_COLOR | ETNA_DIRTY_FRAMEBUFFER,
647 },
648 {
649 etna_update_ts_config, ETNA_DIRTY_DERIVE_TS,
650 }
651 };
652
653 bool
654 etna_state_update(struct etna_context *ctx)
655 {
656 for (unsigned int i = 0; i < ARRAY_SIZE(etna_state_updates); i++)
657 if (ctx->dirty & etna_state_updates[i].dirty)
658 if (!etna_state_updates[i].update(ctx))
659 return false;
660
661 return true;
662 }
663
664 void
665 etna_state_init(struct pipe_context *pctx)
666 {
667 pctx->set_blend_color = etna_set_blend_color;
668 pctx->set_stencil_ref = etna_set_stencil_ref;
669 pctx->set_clip_state = etna_set_clip_state;
670 pctx->set_sample_mask = etna_set_sample_mask;
671 pctx->set_constant_buffer = etna_set_constant_buffer;
672 pctx->set_framebuffer_state = etna_set_framebuffer_state;
673 pctx->set_polygon_stipple = etna_set_polygon_stipple;
674 pctx->set_scissor_states = etna_set_scissor_states;
675 pctx->set_viewport_states = etna_set_viewport_states;
676
677 pctx->set_vertex_buffers = etna_set_vertex_buffers;
678
679 pctx->bind_blend_state = etna_blend_state_bind;
680 pctx->delete_blend_state = etna_blend_state_delete;
681
682 pctx->bind_rasterizer_state = etna_rasterizer_state_bind;
683 pctx->delete_rasterizer_state = etna_rasterizer_state_delete;
684
685 pctx->bind_depth_stencil_alpha_state = etna_zsa_state_bind;
686 pctx->delete_depth_stencil_alpha_state = etna_zsa_state_delete;
687
688 pctx->create_vertex_elements_state = etna_vertex_elements_state_create;
689 pctx->delete_vertex_elements_state = etna_vertex_elements_state_delete;
690 pctx->bind_vertex_elements_state = etna_vertex_elements_state_bind;
691 }