2 * Copyright (c) 2012-2015 Etnaviv Project
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 * Christian Gmeiner <christian.gmeiner@gmail.com>
28 #include "etnaviv_state.h"
30 #include "hw/common.xml.h"
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 "util/u_framebuffer.h"
43 #include "util/u_helpers.h"
44 #include "util/u_inlines.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47 #include "util/u_upload_mgr.h"
50 etna_set_stencil_ref(struct pipe_context
*pctx
, const struct pipe_stencil_ref
*sr
)
52 struct etna_context
*ctx
= etna_context(pctx
);
53 struct compiled_stencil_ref
*cs
= &ctx
->stencil_ref
;
55 ctx
->stencil_ref_s
= *sr
;
57 for (unsigned i
= 0; i
< 2; i
++) {
58 cs
->PE_STENCIL_CONFIG
[i
] =
59 VIVS_PE_STENCIL_CONFIG_REF_FRONT(sr
->ref_value
[i
]);
60 cs
->PE_STENCIL_CONFIG_EXT
[i
] =
61 VIVS_PE_STENCIL_CONFIG_EXT_REF_BACK(sr
->ref_value
[!i
]);
63 ctx
->dirty
|= ETNA_DIRTY_STENCIL_REF
;
67 etna_set_clip_state(struct pipe_context
*pctx
, const struct pipe_clip_state
*pcs
)
73 etna_set_sample_mask(struct pipe_context
*pctx
, unsigned sample_mask
)
75 struct etna_context
*ctx
= etna_context(pctx
);
77 ctx
->sample_mask
= sample_mask
;
78 ctx
->dirty
|= ETNA_DIRTY_SAMPLE_MASK
;
82 etna_set_constant_buffer(struct pipe_context
*pctx
,
83 enum pipe_shader_type shader
, uint index
,
84 const struct pipe_constant_buffer
*cb
)
86 struct etna_context
*ctx
= etna_context(pctx
);
87 struct etna_constbuf_state
*so
= &ctx
->constant_buffer
[shader
];
89 assert(index
< ETNA_MAX_CONST_BUF
);
91 util_copy_constant_buffer(&so
->cb
[index
], cb
);
93 /* Note that the gallium frontends can unbind constant buffers by
94 * passing NULL here. */
95 if (unlikely(!cb
|| (!cb
->buffer
&& !cb
->user_buffer
))) {
96 so
->enabled_mask
&= ~(1 << index
);
100 assert(index
!= 0 || cb
->user_buffer
!= NULL
);
103 struct pipe_constant_buffer
*cb
= &so
->cb
[index
];
104 u_upload_data(pctx
->const_uploader
, 0, cb
->buffer_size
, 16, cb
->user_buffer
, &cb
->buffer_offset
, &cb
->buffer
);
107 so
->enabled_mask
|= 1 << index
;
108 ctx
->dirty
|= ETNA_DIRTY_CONSTBUF
;
112 etna_update_render_resource(struct pipe_context
*pctx
, struct etna_resource
*base
)
114 struct etna_resource
*to
= base
, *from
= base
;
116 if (base
->texture
&& etna_resource_newer(etna_resource(base
->texture
), base
))
117 from
= etna_resource(base
->texture
);
120 to
= etna_resource(base
->render
);
122 if ((to
!= from
) && etna_resource_older(to
, from
)) {
123 etna_copy_resource(pctx
, &to
->base
, &from
->base
, 0, base
->base
.last_level
);
124 to
->seqno
= from
->seqno
;
129 etna_set_framebuffer_state(struct pipe_context
*pctx
,
130 const struct pipe_framebuffer_state
*fb
)
132 struct etna_context
*ctx
= etna_context(pctx
);
133 struct etna_screen
*screen
= ctx
->screen
;
134 struct compiled_framebuffer_state
*cs
= &ctx
->framebuffer
;
135 int nr_samples_color
= -1;
136 int nr_samples_depth
= -1;
138 /* Set up TS as well. Warning: this state is used by both the RS and PE */
139 uint32_t ts_mem_config
= 0;
140 uint32_t pe_mem_config
= 0;
141 uint32_t pe_logic_op
= 0;
143 if (fb
->nr_cbufs
> 0) { /* at least one color buffer? */
144 struct etna_surface
*cbuf
= etna_surface(fb
->cbufs
[0]);
145 struct etna_resource
*res
= etna_resource(cbuf
->base
.texture
);
146 bool color_supertiled
= (res
->layout
& ETNA_LAYOUT_BIT_SUPER
) != 0;
147 uint32_t fmt
= translate_pe_format(cbuf
->base
.format
);
149 assert(res
->layout
& ETNA_LAYOUT_BIT_TILE
); /* Cannot render to linear surfaces */
150 etna_update_render_resource(pctx
, etna_resource(cbuf
->prsc
));
152 if (fmt
>= PE_FORMAT_R16F
)
153 cs
->PE_COLOR_FORMAT
= VIVS_PE_COLOR_FORMAT_FORMAT_EXT(fmt
) |
154 VIVS_PE_COLOR_FORMAT_FORMAT_MASK
;
156 cs
->PE_COLOR_FORMAT
= VIVS_PE_COLOR_FORMAT_FORMAT(fmt
);
158 cs
->PE_COLOR_FORMAT
|=
159 VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK
|
160 VIVS_PE_COLOR_FORMAT_OVERWRITE
|
161 COND(color_supertiled
, VIVS_PE_COLOR_FORMAT_SUPER_TILED
) |
162 COND(color_supertiled
&& screen
->specs
.halti
>= 5, VIVS_PE_COLOR_FORMAT_SUPER_TILED_NEW
);
163 /* VIVS_PE_COLOR_FORMAT_COMPONENTS() and
164 * VIVS_PE_COLOR_FORMAT_OVERWRITE comes from blend_state
165 * but only if we set the bits above. */
166 /* merged with depth_stencil_alpha */
167 if ((cbuf
->surf
.offset
& 63) ||
168 (((cbuf
->surf
.stride
* 4) & 63) && cbuf
->surf
.height
> 4)) {
169 /* XXX Must make temporary surface here.
170 * Need the same mechanism on gc2000 when we want to do mipmap
172 * rendering to levels > 1 due to multitiled / tiled conversion. */
173 BUG("Alignment error, trying to render to offset %08x with tile "
175 cbuf
->surf
.offset
, cbuf
->surf
.stride
* 4);
178 if (screen
->specs
.pixel_pipes
== 1) {
179 cs
->PE_COLOR_ADDR
= cbuf
->reloc
[0];
180 cs
->PE_COLOR_ADDR
.flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
182 /* Rendered textures must always be multi-tiled, or single-buffer mode must be supported */
183 assert((res
->layout
& ETNA_LAYOUT_BIT_MULTI
) || screen
->specs
.single_buffer
);
184 for (int i
= 0; i
< screen
->specs
.pixel_pipes
; i
++) {
185 cs
->PE_PIPE_COLOR_ADDR
[i
] = cbuf
->reloc
[i
];
186 cs
->PE_PIPE_COLOR_ADDR
[i
].flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
189 cs
->PE_COLOR_STRIDE
= cbuf
->surf
.stride
;
191 if (cbuf
->surf
.ts_size
) {
192 cs
->TS_COLOR_CLEAR_VALUE
= cbuf
->level
->clear_value
;
193 cs
->TS_COLOR_CLEAR_VALUE_EXT
= cbuf
->level
->clear_value
>> 32;
195 cs
->TS_COLOR_STATUS_BASE
= cbuf
->ts_reloc
;
196 cs
->TS_COLOR_STATUS_BASE
.flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
198 cs
->TS_COLOR_SURFACE_BASE
= cbuf
->reloc
[0];
199 cs
->TS_COLOR_SURFACE_BASE
.flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
201 pe_mem_config
|= VIVS_PE_MEM_CONFIG_COLOR_TS_MODE(cbuf
->level
->ts_mode
);
203 if (cbuf
->level
->ts_compress_fmt
>= 0) {
204 /* overwrite bit breaks v1/v2 compression */
205 if (!screen
->specs
.v4_compression
)
206 cs
->PE_COLOR_FORMAT
&= ~VIVS_PE_COLOR_FORMAT_OVERWRITE
;
209 VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION
|
210 VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION_FORMAT(cbuf
->level
->ts_compress_fmt
);
214 nr_samples_color
= cbuf
->base
.texture
->nr_samples
;
216 if (util_format_is_srgb(cbuf
->base
.format
))
217 pe_logic_op
|= VIVS_PE_LOGIC_OP_SRGB
;
219 cs
->PS_CONTROL
= COND(util_format_is_unorm(cbuf
->base
.format
), VIVS_PS_CONTROL_SATURATE_RT0
);
221 VIVS_PS_CONTROL_EXT_OUTPUT_MODE0(translate_output_mode(cbuf
->base
.format
, screen
->specs
.halti
>= 5));
223 /* Clearing VIVS_PE_COLOR_FORMAT_COMPONENTS__MASK and
224 * VIVS_PE_COLOR_FORMAT_OVERWRITE prevents us from overwriting the
226 cs
->PE_COLOR_FORMAT
= VIVS_PE_COLOR_FORMAT_OVERWRITE
;
227 cs
->PE_COLOR_STRIDE
= 0;
228 cs
->TS_COLOR_STATUS_BASE
.bo
= NULL
;
229 cs
->TS_COLOR_SURFACE_BASE
.bo
= NULL
;
231 cs
->PE_COLOR_ADDR
= ctx
->dummy_rt_reloc
;
232 for (int i
= 0; i
< screen
->specs
.pixel_pipes
; i
++)
233 cs
->PE_PIPE_COLOR_ADDR
[i
] = ctx
->dummy_rt_reloc
;
236 if (fb
->zsbuf
!= NULL
) {
237 struct etna_surface
*zsbuf
= etna_surface(fb
->zsbuf
);
238 struct etna_resource
*res
= etna_resource(zsbuf
->base
.texture
);
240 etna_update_render_resource(pctx
, etna_resource(zsbuf
->prsc
));
242 assert(res
->layout
&ETNA_LAYOUT_BIT_TILE
); /* Cannot render to linear surfaces */
244 uint32_t depth_format
= translate_depth_format(zsbuf
->base
.format
);
245 unsigned depth_bits
=
246 depth_format
== VIVS_PE_DEPTH_CONFIG_DEPTH_FORMAT_D16
? 16 : 24;
247 bool depth_supertiled
= (res
->layout
& ETNA_LAYOUT_BIT_SUPER
) != 0;
249 cs
->PE_DEPTH_CONFIG
=
251 COND(depth_supertiled
, VIVS_PE_DEPTH_CONFIG_SUPER_TILED
) |
252 VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_Z
|
253 VIVS_PE_DEPTH_CONFIG_UNK18
| /* something to do with clipping? */
254 COND(screen
->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 */
256 /* VIVS_PE_DEPTH_CONFIG_ONLY_DEPTH */
257 /* merged with depth_stencil_alpha */
259 if (screen
->specs
.pixel_pipes
== 1) {
260 cs
->PE_DEPTH_ADDR
= zsbuf
->reloc
[0];
261 cs
->PE_DEPTH_ADDR
.flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
263 for (int i
= 0; i
< screen
->specs
.pixel_pipes
; i
++) {
264 cs
->PE_PIPE_DEPTH_ADDR
[i
] = zsbuf
->reloc
[i
];
265 cs
->PE_PIPE_DEPTH_ADDR
[i
].flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
269 cs
->PE_DEPTH_STRIDE
= zsbuf
->surf
.stride
;
270 cs
->PE_HDEPTH_CONTROL
= VIVS_PE_HDEPTH_CONTROL_FORMAT_DISABLED
;
271 cs
->PE_DEPTH_NORMALIZE
= fui(exp2f(depth_bits
) - 1.0f
);
273 if (zsbuf
->surf
.ts_size
) {
274 cs
->TS_DEPTH_CLEAR_VALUE
= zsbuf
->level
->clear_value
;
276 cs
->TS_DEPTH_STATUS_BASE
= zsbuf
->ts_reloc
;
277 cs
->TS_DEPTH_STATUS_BASE
.flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
279 cs
->TS_DEPTH_SURFACE_BASE
= zsbuf
->reloc
[0];
280 cs
->TS_DEPTH_SURFACE_BASE
.flags
= ETNA_RELOC_READ
| ETNA_RELOC_WRITE
;
282 pe_mem_config
|= VIVS_PE_MEM_CONFIG_DEPTH_TS_MODE(zsbuf
->level
->ts_mode
);
284 if (zsbuf
->level
->ts_compress_fmt
>= 0) {
286 VIVS_TS_MEM_CONFIG_DEPTH_COMPRESSION
|
287 COND(zsbuf
->level
->ts_compress_fmt
== COMPRESSION_FORMAT_D24S8
,
288 VIVS_TS_MEM_CONFIG_STENCIL_ENABLE
);
292 ts_mem_config
|= COND(depth_bits
== 16, VIVS_TS_MEM_CONFIG_DEPTH_16BPP
);
294 nr_samples_depth
= zsbuf
->base
.texture
->nr_samples
;
296 cs
->PE_DEPTH_CONFIG
= VIVS_PE_DEPTH_CONFIG_DEPTH_MODE_NONE
;
297 cs
->PE_DEPTH_ADDR
.bo
= NULL
;
298 cs
->PE_DEPTH_STRIDE
= 0;
299 cs
->TS_DEPTH_STATUS_BASE
.bo
= NULL
;
300 cs
->TS_DEPTH_SURFACE_BASE
.bo
= NULL
;
302 for (int i
= 0; i
< ETNA_MAX_PIXELPIPES
; i
++)
303 cs
->PE_PIPE_DEPTH_ADDR
[i
].bo
= NULL
;
307 if (nr_samples_depth
!= -1 && nr_samples_color
!= -1 &&
308 nr_samples_depth
!= nr_samples_color
) {
309 BUG("Number of samples in color and depth texture must match (%i and %i respectively)",
310 nr_samples_color
, nr_samples_depth
);
313 switch (MAX2(nr_samples_depth
, nr_samples_color
)) {
315 case 1: /* Are 0 and 1 samples allowed? */
316 cs
->GL_MULTI_SAMPLE_CONFIG
=
317 VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_NONE
;
318 cs
->msaa_mode
= false;
321 cs
->GL_MULTI_SAMPLE_CONFIG
= VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_2X
;
322 cs
->msaa_mode
= true; /* Add input to PS */
323 cs
->RA_MULTISAMPLE_UNK00E04
= 0x0;
324 cs
->RA_MULTISAMPLE_UNK00E10
[0] = 0x0000aa22;
325 cs
->RA_CENTROID_TABLE
[0] = 0x66aa2288;
326 cs
->RA_CENTROID_TABLE
[1] = 0x88558800;
327 cs
->RA_CENTROID_TABLE
[2] = 0x88881100;
328 cs
->RA_CENTROID_TABLE
[3] = 0x33888800;
331 cs
->GL_MULTI_SAMPLE_CONFIG
= VIVS_GL_MULTI_SAMPLE_CONFIG_MSAA_SAMPLES_4X
;
332 cs
->msaa_mode
= true; /* Add input to PS */
333 cs
->RA_MULTISAMPLE_UNK00E04
= 0x0;
334 cs
->RA_MULTISAMPLE_UNK00E10
[0] = 0xeaa26e26;
335 cs
->RA_MULTISAMPLE_UNK00E10
[1] = 0xe6ae622a;
336 cs
->RA_MULTISAMPLE_UNK00E10
[2] = 0xaaa22a22;
337 cs
->RA_CENTROID_TABLE
[0] = 0x4a6e2688;
338 cs
->RA_CENTROID_TABLE
[1] = 0x888888a2;
339 cs
->RA_CENTROID_TABLE
[2] = 0x888888ea;
340 cs
->RA_CENTROID_TABLE
[3] = 0x888888c6;
341 cs
->RA_CENTROID_TABLE
[4] = 0x46622a88;
342 cs
->RA_CENTROID_TABLE
[5] = 0x888888ae;
343 cs
->RA_CENTROID_TABLE
[6] = 0x888888e6;
344 cs
->RA_CENTROID_TABLE
[7] = 0x888888ca;
345 cs
->RA_CENTROID_TABLE
[8] = 0x262a2288;
346 cs
->RA_CENTROID_TABLE
[9] = 0x886688a2;
347 cs
->RA_CENTROID_TABLE
[10] = 0x888866aa;
348 cs
->RA_CENTROID_TABLE
[11] = 0x668888a6;
352 cs
->TS_MEM_CONFIG
= ts_mem_config
;
353 cs
->PE_MEM_CONFIG
= pe_mem_config
;
355 /* Single buffer setup. There is only one switch for this, not a separate
356 * one per color buffer / depth buffer. To keep the logic simple always use
357 * single buffer when this feature is available.
358 * note: the blob will use 2 in some situations, figure out why?
360 pe_logic_op
|= VIVS_PE_LOGIC_OP_SINGLE_BUFFER(screen
->specs
.single_buffer
? 3 : 0);
361 cs
->PE_LOGIC_OP
= pe_logic_op
;
363 /* keep copy of original structure */
364 util_copy_framebuffer_state(&ctx
->framebuffer_s
, fb
);
365 ctx
->dirty
|= ETNA_DIRTY_FRAMEBUFFER
| ETNA_DIRTY_DERIVE_TS
;
369 etna_set_polygon_stipple(struct pipe_context
*pctx
,
370 const struct pipe_poly_stipple
*stipple
)
376 etna_set_scissor_states(struct pipe_context
*pctx
, unsigned start_slot
,
377 unsigned num_scissors
, const struct pipe_scissor_state
*ss
)
379 struct etna_context
*ctx
= etna_context(pctx
);
380 assert(ss
->minx
<= ss
->maxx
);
381 assert(ss
->miny
<= ss
->maxy
);
384 ctx
->dirty
|= ETNA_DIRTY_SCISSOR
;
388 etna_set_viewport_states(struct pipe_context
*pctx
, unsigned start_slot
,
389 unsigned num_scissors
, const struct pipe_viewport_state
*vs
)
391 struct etna_context
*ctx
= etna_context(pctx
);
392 struct compiled_viewport_state
*cs
= &ctx
->viewport
;
394 ctx
->viewport_s
= *vs
;
396 * For Vivante GPU, viewport z transformation is 0..1 to 0..1 instead of
398 * scaling and translation to 0..1 already happened, so remove that
400 * z' = (z * 2 - 1) * scale + translate
401 * = z * (2 * scale) + (translate - scale)
404 * translate' = translate - scale
407 /* must be fixp as v4 state deltas assume it is */
408 cs
->PA_VIEWPORT_SCALE_X
= etna_f32_to_fixp16(vs
->scale
[0]);
409 cs
->PA_VIEWPORT_SCALE_Y
= etna_f32_to_fixp16(vs
->scale
[1]);
410 cs
->PA_VIEWPORT_SCALE_Z
= fui(vs
->scale
[2] * 2.0f
);
411 cs
->PA_VIEWPORT_OFFSET_X
= etna_f32_to_fixp16(vs
->translate
[0]);
412 cs
->PA_VIEWPORT_OFFSET_Y
= etna_f32_to_fixp16(vs
->translate
[1]);
413 cs
->PA_VIEWPORT_OFFSET_Z
= fui(vs
->translate
[2] - vs
->scale
[2]);
415 /* Compute scissor rectangle (fixp) from viewport.
416 * Make sure left is always < right and top always < bottom.
418 cs
->SE_SCISSOR_LEFT
= MAX2(vs
->translate
[0] - fabsf(vs
->scale
[0]), 0.0f
);
419 cs
->SE_SCISSOR_TOP
= MAX2(vs
->translate
[1] - fabsf(vs
->scale
[1]), 0.0f
);
420 cs
->SE_SCISSOR_RIGHT
= ceilf(MAX2(vs
->translate
[0] + fabsf(vs
->scale
[0]), 0.0f
));
421 cs
->SE_SCISSOR_BOTTOM
= ceilf(MAX2(vs
->translate
[1] + fabsf(vs
->scale
[1]), 0.0f
));
423 cs
->PE_DEPTH_NEAR
= fui(0.0); /* not affected if depth mode is Z (as in GL) */
424 cs
->PE_DEPTH_FAR
= fui(1.0);
425 ctx
->dirty
|= ETNA_DIRTY_VIEWPORT
;
429 etna_set_vertex_buffers(struct pipe_context
*pctx
, unsigned start_slot
,
430 unsigned num_buffers
, const struct pipe_vertex_buffer
*vb
)
432 struct etna_context
*ctx
= etna_context(pctx
);
433 struct etna_vertexbuf_state
*so
= &ctx
->vertex_buffer
;
435 util_set_vertex_buffers_mask(so
->vb
, &so
->enabled_mask
, vb
, start_slot
, num_buffers
);
436 so
->count
= util_last_bit(so
->enabled_mask
);
438 for (unsigned idx
= start_slot
; idx
< start_slot
+ num_buffers
; ++idx
) {
439 struct compiled_set_vertex_buffer
*cs
= &so
->cvb
[idx
];
440 struct pipe_vertex_buffer
*vbi
= &so
->vb
[idx
];
442 assert(!vbi
->is_user_buffer
); /* XXX support user_buffer using
445 if (vbi
->buffer
.resource
) { /* GPU buffer */
446 cs
->FE_VERTEX_STREAM_BASE_ADDR
.bo
= etna_resource(vbi
->buffer
.resource
)->bo
;
447 cs
->FE_VERTEX_STREAM_BASE_ADDR
.offset
= vbi
->buffer_offset
;
448 cs
->FE_VERTEX_STREAM_BASE_ADDR
.flags
= ETNA_RELOC_READ
;
449 cs
->FE_VERTEX_STREAM_CONTROL
=
450 FE_VERTEX_STREAM_CONTROL_VERTEX_STRIDE(vbi
->stride
);
452 cs
->FE_VERTEX_STREAM_BASE_ADDR
.bo
= NULL
;
453 cs
->FE_VERTEX_STREAM_CONTROL
= 0;
457 ctx
->dirty
|= ETNA_DIRTY_VERTEX_BUFFERS
;
461 etna_blend_state_bind(struct pipe_context
*pctx
, void *bs
)
463 struct etna_context
*ctx
= etna_context(pctx
);
466 ctx
->dirty
|= ETNA_DIRTY_BLEND
;
470 etna_blend_state_delete(struct pipe_context
*pctx
, void *bs
)
476 etna_rasterizer_state_bind(struct pipe_context
*pctx
, void *rs
)
478 struct etna_context
*ctx
= etna_context(pctx
);
480 ctx
->rasterizer
= rs
;
481 ctx
->dirty
|= ETNA_DIRTY_RASTERIZER
;
485 etna_rasterizer_state_delete(struct pipe_context
*pctx
, void *rs
)
491 etna_zsa_state_bind(struct pipe_context
*pctx
, void *zs
)
493 struct etna_context
*ctx
= etna_context(pctx
);
496 ctx
->dirty
|= ETNA_DIRTY_ZSA
;
500 etna_zsa_state_delete(struct pipe_context
*pctx
, void *zs
)
505 /** Create vertex element states, which define a layout for fetching
506 * vertices for rendering.
509 etna_vertex_elements_state_create(struct pipe_context
*pctx
,
510 unsigned num_elements
, const struct pipe_vertex_element
*elements
)
512 struct etna_context
*ctx
= etna_context(pctx
);
513 struct etna_screen
*screen
= ctx
->screen
;
514 struct compiled_vertex_elements_state
*cs
= CALLOC_STRUCT(compiled_vertex_elements_state
);
519 if (num_elements
> screen
->specs
.vertex_max_elements
) {
520 BUG("number of elements (%u) exceeds chip maximum (%u)", num_elements
,
521 screen
->specs
.vertex_max_elements
);
525 /* XXX could minimize number of consecutive stretches here by sorting, and
526 * permuting the inputs in shader or does Mesa do this already? */
528 cs
->num_elements
= num_elements
;
530 unsigned start_offset
= 0; /* start of current consecutive stretch */
531 bool nonconsecutive
= true; /* previous value of nonconsecutive */
532 uint32_t buffer_mask
= 0; /* mask of buffer_idx already seen */
534 for (unsigned idx
= 0; idx
< num_elements
; ++idx
) {
535 unsigned buffer_idx
= elements
[idx
].vertex_buffer_index
;
536 unsigned element_size
= util_format_get_blocksize(elements
[idx
].src_format
);
537 unsigned end_offset
= elements
[idx
].src_offset
+ element_size
;
538 uint32_t format_type
, normalize
;
541 start_offset
= elements
[idx
].src_offset
;
543 /* guaranteed by PIPE_CAP_MAX_VERTEX_BUFFERS */
544 assert(buffer_idx
< screen
->specs
.stream_count
);
546 /* maximum vertex size is 256 bytes */
547 assert(element_size
!= 0 && (end_offset
- start_offset
) < 256);
549 /* check whether next element is consecutive to this one */
550 nonconsecutive
= (idx
== (num_elements
- 1)) ||
551 elements
[idx
+ 1].vertex_buffer_index
!= buffer_idx
||
552 end_offset
!= elements
[idx
+ 1].src_offset
;
554 format_type
= translate_vertex_format_type(elements
[idx
].src_format
);
555 normalize
= translate_vertex_format_normalize(elements
[idx
].src_format
);
557 assert(format_type
!= ETNA_NO_MATCH
);
558 assert(normalize
!= ETNA_NO_MATCH
);
560 if (screen
->specs
.halti
< 5) {
561 cs
->FE_VERTEX_ELEMENT_CONFIG
[idx
] =
562 COND(nonconsecutive
, VIVS_FE_VERTEX_ELEMENT_CONFIG_NONCONSECUTIVE
) |
564 VIVS_FE_VERTEX_ELEMENT_CONFIG_NUM(util_format_get_nr_components(elements
[idx
].src_format
)) |
565 normalize
| VIVS_FE_VERTEX_ELEMENT_CONFIG_ENDIAN(ENDIAN_MODE_NO_SWAP
) |
566 VIVS_FE_VERTEX_ELEMENT_CONFIG_STREAM(buffer_idx
) |
567 VIVS_FE_VERTEX_ELEMENT_CONFIG_START(elements
[idx
].src_offset
) |
568 VIVS_FE_VERTEX_ELEMENT_CONFIG_END(end_offset
- start_offset
);
569 } else { /* HALTI5 spread vertex attrib config over two registers */
570 cs
->NFE_GENERIC_ATTRIB_CONFIG0
[idx
] =
572 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_NUM(util_format_get_nr_components(elements
[idx
].src_format
)) |
573 normalize
| VIVS_NFE_GENERIC_ATTRIB_CONFIG0_ENDIAN(ENDIAN_MODE_NO_SWAP
) |
574 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_STREAM(buffer_idx
) |
575 VIVS_NFE_GENERIC_ATTRIB_CONFIG0_START(elements
[idx
].src_offset
);
576 cs
->NFE_GENERIC_ATTRIB_CONFIG1
[idx
] =
577 COND(nonconsecutive
, VIVS_NFE_GENERIC_ATTRIB_CONFIG1_NONCONSECUTIVE
) |
578 VIVS_NFE_GENERIC_ATTRIB_CONFIG1_END(end_offset
- start_offset
);
581 if (util_format_is_pure_integer(elements
[idx
].src_format
))
582 cs
->NFE_GENERIC_ATTRIB_SCALE
[idx
] = 1;
584 cs
->NFE_GENERIC_ATTRIB_SCALE
[idx
] = fui(1.0f
);
586 /* instance_divisor is part of elements state but should be the same for all buffers */
587 if (buffer_mask
& 1 << buffer_idx
)
588 assert(cs
->NFE_VERTEX_STREAMS_VERTEX_DIVISOR
[buffer_idx
] == elements
[idx
].instance_divisor
);
590 cs
->NFE_VERTEX_STREAMS_VERTEX_DIVISOR
[buffer_idx
] = elements
[idx
].instance_divisor
;
592 buffer_mask
|= 1 << buffer_idx
;
593 cs
->num_buffers
= MAX2(cs
->num_buffers
, buffer_idx
+ 1);
600 etna_vertex_elements_state_delete(struct pipe_context
*pctx
, void *ve
)
606 etna_vertex_elements_state_bind(struct pipe_context
*pctx
, void *ve
)
608 struct etna_context
*ctx
= etna_context(pctx
);
610 ctx
->vertex_elements
= ve
;
611 ctx
->dirty
|= ETNA_DIRTY_VERTEX_ELEMENTS
;
615 etna_update_ts_config(struct etna_context
*ctx
)
617 uint32_t new_ts_config
= ctx
->framebuffer
.TS_MEM_CONFIG
;
619 if (ctx
->framebuffer_s
.nr_cbufs
> 0) {
620 struct etna_surface
*c_surf
= etna_surface(ctx
->framebuffer_s
.cbufs
[0]);
622 if(c_surf
->level
->ts_size
&& c_surf
->level
->ts_valid
) {
623 new_ts_config
|= VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR
;
625 new_ts_config
&= ~VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR
;
629 if (ctx
->framebuffer_s
.zsbuf
) {
630 struct etna_surface
*zs_surf
= etna_surface(ctx
->framebuffer_s
.zsbuf
);
632 if(zs_surf
->level
->ts_size
&& zs_surf
->level
->ts_valid
) {
633 new_ts_config
|= VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR
;
635 new_ts_config
&= ~VIVS_TS_MEM_CONFIG_DEPTH_FAST_CLEAR
;
639 if (new_ts_config
!= ctx
->framebuffer
.TS_MEM_CONFIG
||
640 (ctx
->dirty
& ETNA_DIRTY_FRAMEBUFFER
)) {
641 ctx
->framebuffer
.TS_MEM_CONFIG
= new_ts_config
;
642 ctx
->dirty
|= ETNA_DIRTY_TS
;
645 ctx
->dirty
&= ~ETNA_DIRTY_DERIVE_TS
;
651 etna_update_clipping(struct etna_context
*ctx
)
653 const struct etna_rasterizer_state
*rasterizer
= etna_rasterizer_state(ctx
->rasterizer
);
654 const struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer_s
;
656 /* clip framebuffer against viewport */
657 uint32_t scissor_left
= ctx
->viewport
.SE_SCISSOR_LEFT
;
658 uint32_t scissor_top
= ctx
->viewport
.SE_SCISSOR_TOP
;
659 uint32_t scissor_right
= MIN2(fb
->width
, ctx
->viewport
.SE_SCISSOR_RIGHT
);
660 uint32_t scissor_bottom
= MIN2(fb
->height
, ctx
->viewport
.SE_SCISSOR_BOTTOM
);
662 /* clip against scissor */
663 if (rasterizer
->scissor
) {
664 scissor_left
= MAX2(ctx
->scissor
.minx
, scissor_left
);
665 scissor_top
= MAX2(ctx
->scissor
.miny
, scissor_top
);
666 scissor_right
= MIN2(ctx
->scissor
.maxx
, scissor_right
);
667 scissor_bottom
= MIN2(ctx
->scissor
.maxy
, scissor_bottom
);
670 ctx
->clipping
.minx
= scissor_left
;
671 ctx
->clipping
.miny
= scissor_top
;
672 ctx
->clipping
.maxx
= scissor_right
;
673 ctx
->clipping
.maxy
= scissor_bottom
;
675 ctx
->dirty
|= ETNA_DIRTY_SCISSOR_CLIP
;
680 struct etna_state_updater
{
681 bool (*update
)(struct etna_context
*ctx
);
685 static const struct etna_state_updater etna_state_updates
[] = {
687 etna_shader_update_vertex
, ETNA_DIRTY_SHADER
| ETNA_DIRTY_VERTEX_ELEMENTS
,
690 etna_shader_link
, ETNA_DIRTY_SHADER
,
693 etna_update_blend
, ETNA_DIRTY_BLEND
| ETNA_DIRTY_FRAMEBUFFER
696 etna_update_blend_color
, ETNA_DIRTY_BLEND_COLOR
| ETNA_DIRTY_FRAMEBUFFER
,
699 etna_update_ts_config
, ETNA_DIRTY_DERIVE_TS
,
702 etna_update_clipping
, ETNA_DIRTY_SCISSOR
| ETNA_DIRTY_FRAMEBUFFER
|
703 ETNA_DIRTY_RASTERIZER
| ETNA_DIRTY_VIEWPORT
,
708 etna_state_update(struct etna_context
*ctx
)
710 for (unsigned int i
= 0; i
< ARRAY_SIZE(etna_state_updates
); i
++)
711 if (ctx
->dirty
& etna_state_updates
[i
].dirty
)
712 if (!etna_state_updates
[i
].update(ctx
))
719 etna_state_init(struct pipe_context
*pctx
)
721 pctx
->set_blend_color
= etna_set_blend_color
;
722 pctx
->set_stencil_ref
= etna_set_stencil_ref
;
723 pctx
->set_clip_state
= etna_set_clip_state
;
724 pctx
->set_sample_mask
= etna_set_sample_mask
;
725 pctx
->set_constant_buffer
= etna_set_constant_buffer
;
726 pctx
->set_framebuffer_state
= etna_set_framebuffer_state
;
727 pctx
->set_polygon_stipple
= etna_set_polygon_stipple
;
728 pctx
->set_scissor_states
= etna_set_scissor_states
;
729 pctx
->set_viewport_states
= etna_set_viewport_states
;
731 pctx
->set_vertex_buffers
= etna_set_vertex_buffers
;
733 pctx
->bind_blend_state
= etna_blend_state_bind
;
734 pctx
->delete_blend_state
= etna_blend_state_delete
;
736 pctx
->bind_rasterizer_state
= etna_rasterizer_state_bind
;
737 pctx
->delete_rasterizer_state
= etna_rasterizer_state_delete
;
739 pctx
->bind_depth_stencil_alpha_state
= etna_zsa_state_bind
;
740 pctx
->delete_depth_stencil_alpha_state
= etna_zsa_state_delete
;
742 pctx
->create_vertex_elements_state
= etna_vertex_elements_state_create
;
743 pctx
->delete_vertex_elements_state
= etna_vertex_elements_state_delete
;
744 pctx
->bind_vertex_elements_state
= etna_vertex_elements_state_bind
;