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_context.h"
30 #include "etnaviv_blend.h"
31 #include "etnaviv_clear_blit.h"
32 #include "etnaviv_compiler.h"
33 #include "etnaviv_debug.h"
34 #include "etnaviv_emit.h"
35 #include "etnaviv_fence.h"
36 #include "etnaviv_query.h"
37 #include "etnaviv_rasterizer.h"
38 #include "etnaviv_screen.h"
39 #include "etnaviv_shader.h"
40 #include "etnaviv_state.h"
41 #include "etnaviv_surface.h"
42 #include "etnaviv_texture.h"
43 #include "etnaviv_transfer.h"
44 #include "etnaviv_translate.h"
45 #include "etnaviv_zsa.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_state.h"
49 #include "util/u_blitter.h"
50 #include "util/u_helpers.h"
51 #include "util/u_memory.h"
52 #include "util/u_prim.h"
53 #include "util/u_upload_mgr.h"
55 #include "hw/common.xml.h"
58 etna_context_destroy(struct pipe_context
*pctx
)
60 struct etna_context
*ctx
= etna_context(pctx
);
63 util_primconvert_destroy(ctx
->primconvert
);
66 util_blitter_destroy(ctx
->blitter
);
68 if (pctx
->stream_uploader
)
69 u_upload_destroy(pctx
->stream_uploader
);
72 etna_cmd_stream_del(ctx
->stream
);
74 slab_destroy_child(&ctx
->transfer_pool
);
76 if (ctx
->in_fence_fd
!= -1)
77 close(ctx
->in_fence_fd
);
82 /* Update render state where needed based on draw operation */
84 etna_update_state_for_draw(struct etna_context
*ctx
, const struct pipe_draw_info
*info
)
86 /* Handle primitive restart:
87 * - If not an indexed draw, we don't care about the state of the primitive restart bit.
88 * - Otherwise, set the bit in INDEX_STREAM_CONTROL in the index buffer state
90 * - If the value of the INDEX_STREAM_CONTROL register changed due to this, or
91 * primitive restart is enabled and the restart index changed, mark the index
92 * buffer state as dirty
95 if (info
->index_size
) {
96 uint32_t new_control
= ctx
->index_buffer
.FE_INDEX_STREAM_CONTROL
;
98 if (info
->primitive_restart
)
99 new_control
|= VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART
;
101 new_control
&= ~VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART
;
103 if (ctx
->index_buffer
.FE_INDEX_STREAM_CONTROL
!= new_control
||
104 (info
->primitive_restart
&& ctx
->index_buffer
.FE_PRIMITIVE_RESTART_INDEX
!= info
->restart_index
)) {
105 ctx
->index_buffer
.FE_INDEX_STREAM_CONTROL
= new_control
;
106 ctx
->index_buffer
.FE_PRIMITIVE_RESTART_INDEX
= info
->restart_index
;
107 ctx
->dirty
|= ETNA_DIRTY_INDEX_BUFFER
;
113 etna_get_vs(struct etna_context
*ctx
, struct etna_shader_key key
)
115 const struct etna_shader_variant
*old
= ctx
->shader
.vs
;
117 ctx
->shader
.vs
= etna_shader_variant(ctx
->shader
.bind_vs
, key
, &ctx
->debug
);
122 if (old
!= ctx
->shader
.vs
)
123 ctx
->dirty
|= ETNA_DIRTY_SHADER
;
129 etna_get_fs(struct etna_context
*ctx
, struct etna_shader_key key
)
131 const struct etna_shader_variant
*old
= ctx
->shader
.fs
;
133 ctx
->shader
.fs
= etna_shader_variant(ctx
->shader
.bind_fs
, key
, &ctx
->debug
);
138 if (old
!= ctx
->shader
.fs
)
139 ctx
->dirty
|= ETNA_DIRTY_SHADER
;
145 etna_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
147 struct etna_context
*ctx
= etna_context(pctx
);
148 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer_s
;
152 if (!info
->count_from_stream_output
&& !info
->indirect
&&
153 !info
->primitive_restart
&&
154 !u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
))
157 if (ctx
->vertex_elements
== NULL
|| ctx
->vertex_elements
->num_elements
== 0)
158 return; /* Nothing to do */
160 if (!(ctx
->prim_hwsupport
& (1 << info
->mode
))) {
161 struct primconvert_context
*primconvert
= ctx
->primconvert
;
162 util_primconvert_save_rasterizer_state(primconvert
, ctx
->rasterizer
);
163 util_primconvert_draw_vbo(primconvert
, info
);
167 int prims
= u_decomposed_prims_for_vertices(info
->mode
, info
->count
);
168 if (unlikely(prims
<= 0)) {
169 DBG("Invalid draw primitive mode=%i or no primitives to be drawn", info
->mode
);
173 draw_mode
= translate_draw_mode(info
->mode
);
174 if (draw_mode
== ETNA_NO_MATCH
) {
175 BUG("Unsupported draw mode");
179 /* Upload a user index buffer. */
180 unsigned index_offset
= 0;
181 struct pipe_resource
*indexbuf
= info
->has_user_indices
? NULL
: info
->index
.resource
;
182 if (info
->index_size
&& info
->has_user_indices
&&
183 !util_upload_index_buffer(pctx
, info
, &indexbuf
, &index_offset
)) {
184 BUG("Index buffer upload failed.");
188 if (info
->index_size
&& indexbuf
) {
189 ctx
->index_buffer
.FE_INDEX_STREAM_BASE_ADDR
.bo
= etna_resource(indexbuf
)->bo
;
190 ctx
->index_buffer
.FE_INDEX_STREAM_BASE_ADDR
.offset
= index_offset
;
191 ctx
->index_buffer
.FE_INDEX_STREAM_BASE_ADDR
.flags
= ETNA_RELOC_READ
;
192 ctx
->index_buffer
.FE_INDEX_STREAM_CONTROL
= translate_index_size(info
->index_size
);
193 ctx
->dirty
|= ETNA_DIRTY_INDEX_BUFFER
;
196 if (info
->index_size
&& !ctx
->index_buffer
.FE_INDEX_STREAM_BASE_ADDR
.bo
) {
197 BUG("Unsupported or no index buffer");
201 struct etna_shader_key key
= {};
202 struct etna_surface
*cbuf
= etna_surface(pfb
->cbufs
[0]);
205 struct etna_resource
*res
= etna_resource(cbuf
->base
.texture
);
207 key
.frag_rb_swap
= !!translate_rs_format_rb_swap(res
->base
.format
);
210 if (!etna_get_vs(ctx
, key
) || !etna_get_fs(ctx
, key
)) {
211 BUG("compiled shaders are not okay");
215 /* Update any derived state */
216 if (!etna_state_update(ctx
))
220 * Figure out the buffers/features we need:
222 if (etna_depth_enabled(ctx
))
223 resource_written(ctx
, pfb
->zsbuf
->texture
);
225 if (etna_stencil_enabled(ctx
))
226 resource_written(ctx
, pfb
->zsbuf
->texture
);
228 for (i
= 0; i
< pfb
->nr_cbufs
; i
++) {
229 struct pipe_resource
*surf
;
234 surf
= pfb
->cbufs
[i
]->texture
;
235 resource_written(ctx
, surf
);
238 /* Mark constant buffers as being read */
239 resource_read(ctx
, ctx
->constant_buffer
[PIPE_SHADER_VERTEX
].buffer
);
240 resource_read(ctx
, ctx
->constant_buffer
[PIPE_SHADER_FRAGMENT
].buffer
);
242 /* Mark VBOs as being read */
243 for (i
= 0; i
< ctx
->vertex_buffer
.count
; i
++) {
244 assert(!ctx
->vertex_buffer
.vb
[i
].is_user_buffer
);
245 resource_read(ctx
, ctx
->vertex_buffer
.vb
[i
].buffer
.resource
);
248 /* Mark index buffer as being read */
249 resource_read(ctx
, indexbuf
);
251 /* Mark textures as being read */
252 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++)
253 if (ctx
->sampler_view
[i
])
254 resource_read(ctx
, ctx
->sampler_view
[i
]->texture
);
256 ctx
->stats
.prims_emitted
+= u_reduced_prims_for_vertices(info
->mode
, info
->count
);
257 ctx
->stats
.draw_calls
++;
259 /* Update state for this draw operation */
260 etna_update_state_for_draw(ctx
, info
);
262 /* First, sync state, then emit DRAW_PRIMITIVES or DRAW_INDEXED_PRIMITIVES */
263 etna_emit_state(ctx
);
265 if (info
->index_size
)
266 etna_draw_indexed_primitives(ctx
->stream
, draw_mode
, info
->start
, prims
, info
->index_bias
);
268 etna_draw_primitives(ctx
->stream
, draw_mode
, info
->start
, prims
);
270 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL
)) {
271 /* Stall the FE after every draw operation. This allows better
272 * debug of GPU hang conditions, as the FE will indicate which
273 * draw op has caused the hang. */
274 etna_stall(ctx
->stream
, SYNC_RECIPIENT_FE
, SYNC_RECIPIENT_PE
);
277 if (DBG_ENABLED(ETNA_DBG_FLUSH_ALL
))
278 pctx
->flush(pctx
, NULL
, 0);
280 if (ctx
->framebuffer
.cbuf
)
281 etna_resource(ctx
->framebuffer
.cbuf
->texture
)->seqno
++;
282 if (ctx
->framebuffer
.zsbuf
)
283 etna_resource(ctx
->framebuffer
.zsbuf
->texture
)->seqno
++;
284 if (info
->index_size
&& indexbuf
!= info
->index
.resource
)
285 pipe_resource_reference(&indexbuf
, NULL
);
289 etna_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
290 enum pipe_flush_flags flags
)
292 struct etna_context
*ctx
= etna_context(pctx
);
293 int out_fence_fd
= -1;
295 etna_cmd_stream_flush2(ctx
->stream
, ctx
->in_fence_fd
,
296 (flags
& PIPE_FLUSH_FENCE_FD
) ? &out_fence_fd
:
300 *fence
= etna_fence_create(pctx
, out_fence_fd
);
304 etna_cmd_stream_reset_notify(struct etna_cmd_stream
*stream
, void *priv
)
306 struct etna_context
*ctx
= priv
;
307 struct etna_resource
*rsc
, *rsc_tmp
;
309 etna_set_state(stream
, VIVS_GL_API_MODE
, VIVS_GL_API_MODE_OPENGL
);
310 etna_set_state(stream
, VIVS_GL_VERTEX_ELEMENT_CONFIG
, 0x00000001);
311 etna_set_state(stream
, VIVS_RA_EARLY_DEPTH
, 0x00000031);
312 etna_set_state(stream
, VIVS_PA_W_CLIP_LIMIT
, 0x34000001);
314 /* Enable SINGLE_BUFFER for resolve, if supported */
315 etna_set_state(stream
, VIVS_RS_SINGLE_BUFFER
, COND(ctx
->specs
.single_buffer
, VIVS_RS_SINGLE_BUFFER_ENABLE
));
319 /* go through all the used resources and clear their status flag */
320 LIST_FOR_EACH_ENTRY_SAFE(rsc
, rsc_tmp
, &ctx
->used_resources
, list
)
322 debug_assert(rsc
->status
!= 0);
324 rsc
->pending_ctx
= NULL
;
325 list_delinit(&rsc
->list
);
328 assert(LIST_IS_EMPTY(&ctx
->used_resources
));
332 etna_set_debug_callback(struct pipe_context
*pctx
,
333 const struct pipe_debug_callback
*cb
)
335 struct etna_context
*ctx
= etna_context(pctx
);
340 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
343 struct pipe_context
*
344 etna_context_create(struct pipe_screen
*pscreen
, void *priv
, unsigned flags
)
346 struct etna_context
*ctx
= CALLOC_STRUCT(etna_context
);
347 struct etna_screen
*screen
;
348 struct pipe_context
*pctx
;
355 pctx
->screen
= pscreen
;
356 pctx
->stream_uploader
= u_upload_create_default(pctx
);
357 if (!pctx
->stream_uploader
)
359 pctx
->const_uploader
= pctx
->stream_uploader
;
361 screen
= etna_screen(pscreen
);
362 ctx
->stream
= etna_cmd_stream_new(screen
->pipe
, 0x2000, &etna_cmd_stream_reset_notify
, ctx
);
363 if (ctx
->stream
== NULL
)
366 /* context ctxate setup */
367 ctx
->specs
= screen
->specs
;
368 ctx
->screen
= screen
;
369 /* need some sane default in case state tracker doesn't set some state: */
370 ctx
->sample_mask
= 0xffff;
372 list_inithead(&ctx
->used_resources
);
374 /* Set sensible defaults for state */
375 etna_cmd_stream_reset_notify(ctx
->stream
, ctx
);
377 ctx
->in_fence_fd
= -1;
379 pctx
->destroy
= etna_context_destroy
;
380 pctx
->draw_vbo
= etna_draw_vbo
;
381 pctx
->flush
= etna_flush
;
382 pctx
->set_debug_callback
= etna_set_debug_callback
;
383 pctx
->create_fence_fd
= etna_create_fence_fd
;
384 pctx
->fence_server_sync
= etna_fence_server_sync
;
386 /* creation of compile states */
387 pctx
->create_blend_state
= etna_blend_state_create
;
388 pctx
->create_rasterizer_state
= etna_rasterizer_state_create
;
389 pctx
->create_depth_stencil_alpha_state
= etna_zsa_state_create
;
391 etna_clear_blit_init(pctx
);
392 etna_query_context_init(pctx
);
393 etna_state_init(pctx
);
394 etna_surface_init(pctx
);
395 etna_shader_init(pctx
);
396 etna_texture_init(pctx
);
397 etna_transfer_init(pctx
);
399 ctx
->blitter
= util_blitter_create(pctx
);
403 /* Generate the bitmask of supported draw primitives. */
404 ctx
->prim_hwsupport
= 1 << PIPE_PRIM_POINTS
|
405 1 << PIPE_PRIM_LINES
|
406 1 << PIPE_PRIM_LINE_STRIP
|
407 1 << PIPE_PRIM_TRIANGLES
|
408 1 << PIPE_PRIM_TRIANGLE_STRIP
|
409 1 << PIPE_PRIM_TRIANGLE_FAN
;
411 if (VIV_FEATURE(ctx
->screen
, chipMinorFeatures2
, LINE_LOOP
))
412 ctx
->prim_hwsupport
|= 1 << PIPE_PRIM_LINE_LOOP
;
414 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->prim_hwsupport
);
415 if (!ctx
->primconvert
)
418 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);