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_memory.h"
51 #include "util/u_prim.h"
53 #include "hw/common.xml.h"
56 etna_context_destroy(struct pipe_context
*pctx
)
58 struct etna_context
*ctx
= etna_context(pctx
);
61 util_primconvert_destroy(ctx
->primconvert
);
64 util_blitter_destroy(ctx
->blitter
);
67 etna_cmd_stream_del(ctx
->stream
);
69 slab_destroy_child(&ctx
->transfer_pool
);
74 /* Update render state where needed based on draw operation */
76 etna_update_state_for_draw(struct etna_context
*ctx
, const struct pipe_draw_info
*info
)
78 /* Handle primitive restart:
79 * - If not an indexed draw, we don't care about the state of the primitive restart bit.
80 * - Otherwise, set the bit in INDEX_STREAM_CONTROL in the index buffer state
82 * - If the value of the INDEX_STREAM_CONTROL register changed due to this, or
83 * primitive restart is enabled and the restart index changed, mark the index
84 * buffer state as dirty
88 uint32_t new_control
= ctx
->index_buffer
.FE_INDEX_STREAM_CONTROL
;
90 if (info
->primitive_restart
)
91 new_control
|= VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART
;
93 new_control
&= ~VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART
;
95 if (ctx
->index_buffer
.FE_INDEX_STREAM_CONTROL
!= new_control
||
96 (info
->primitive_restart
&& ctx
->index_buffer
.FE_PRIMITIVE_RESTART_INDEX
!= info
->restart_index
)) {
97 ctx
->index_buffer
.FE_INDEX_STREAM_CONTROL
= new_control
;
98 ctx
->index_buffer
.FE_PRIMITIVE_RESTART_INDEX
= info
->restart_index
;
99 ctx
->dirty
|= ETNA_DIRTY_INDEX_BUFFER
;
106 etna_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
108 struct etna_context
*ctx
= etna_context(pctx
);
109 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer_s
;
113 if (ctx
->vertex_elements
== NULL
|| ctx
->vertex_elements
->num_elements
== 0)
114 return; /* Nothing to do */
116 if (!(ctx
->prim_hwsupport
& (1 << info
->mode
))) {
117 struct primconvert_context
*primconvert
= ctx
->primconvert
;
118 util_primconvert_save_index_buffer(primconvert
, &ctx
->index_buffer
.ib
);
119 util_primconvert_save_rasterizer_state(primconvert
, ctx
->rasterizer
);
120 util_primconvert_draw_vbo(primconvert
, info
);
124 int prims
= u_decomposed_prims_for_vertices(info
->mode
, info
->count
);
125 if (unlikely(prims
<= 0)) {
126 DBG("Invalid draw primitive mode=%i or no primitives to be drawn", info
->mode
);
130 draw_mode
= translate_draw_mode(info
->mode
);
131 if (draw_mode
== ETNA_NO_MATCH
) {
132 BUG("Unsupported draw mode");
136 if (info
->indexed
&& !ctx
->index_buffer
.FE_INDEX_STREAM_BASE_ADDR
.bo
) {
137 BUG("Unsupported or no index buffer");
141 /* Update any derived state */
142 if (!etna_state_update(ctx
))
146 * Figure out the buffers/features we need:
148 if (etna_depth_enabled(ctx
))
149 resource_written(ctx
, pfb
->zsbuf
->texture
);
151 if (etna_stencil_enabled(ctx
))
152 resource_written(ctx
, pfb
->zsbuf
->texture
);
154 for (i
= 0; i
< pfb
->nr_cbufs
; i
++) {
155 struct pipe_resource
*surf
;
160 surf
= pfb
->cbufs
[i
]->texture
;
161 resource_written(ctx
, surf
);
164 /* Mark constant buffers as being read */
165 resource_read(ctx
, ctx
->constant_buffer
[PIPE_SHADER_VERTEX
].buffer
);
166 resource_read(ctx
, ctx
->constant_buffer
[PIPE_SHADER_FRAGMENT
].buffer
);
168 /* Mark VBOs as being read */
169 for (i
= 0; i
< ctx
->vertex_buffer
.count
; i
++) {
170 assert(!ctx
->vertex_buffer
.vb
[i
].user_buffer
);
171 resource_read(ctx
, ctx
->vertex_buffer
.vb
[i
].buffer
);
174 /* Mark index buffer as being read */
175 resource_read(ctx
, ctx
->index_buffer
.ib
.buffer
);
177 /* Mark textures as being read */
178 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++)
179 if (ctx
->sampler_view
[i
])
180 resource_read(ctx
, ctx
->sampler_view
[i
]->texture
);
182 ctx
->stats
.prims_emitted
+= u_reduced_prims_for_vertices(info
->mode
, info
->count
);
183 ctx
->stats
.draw_calls
++;
185 /* Update state for this draw operation */
186 etna_update_state_for_draw(ctx
, info
);
188 /* First, sync state, then emit DRAW_PRIMITIVES or DRAW_INDEXED_PRIMITIVES */
189 etna_emit_state(ctx
);
192 etna_draw_indexed_primitives(ctx
->stream
, draw_mode
, info
->start
, prims
, info
->index_bias
);
194 etna_draw_primitives(ctx
->stream
, draw_mode
, info
->start
, prims
);
196 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL
)) {
197 /* Stall the FE after every draw operation. This allows better
198 * debug of GPU hang conditions, as the FE will indicate which
199 * draw op has caused the hang. */
200 etna_stall(ctx
->stream
, SYNC_RECIPIENT_FE
, SYNC_RECIPIENT_PE
);
203 if (DBG_ENABLED(ETNA_DBG_FLUSH_ALL
))
204 pctx
->flush(pctx
, NULL
, 0);
206 if (ctx
->framebuffer
.cbuf
)
207 etna_resource(ctx
->framebuffer
.cbuf
->texture
)->seqno
++;
208 if (ctx
->framebuffer
.zsbuf
)
209 etna_resource(ctx
->framebuffer
.zsbuf
->texture
)->seqno
++;
213 etna_flush(struct pipe_context
*pctx
, struct pipe_fence_handle
**fence
,
214 enum pipe_flush_flags flags
)
216 struct etna_context
*ctx
= etna_context(pctx
);
218 etna_cmd_stream_flush(ctx
->stream
);
221 *fence
= etna_fence_create(pctx
);
225 etna_cmd_stream_reset_notify(struct etna_cmd_stream
*stream
, void *priv
)
227 struct etna_context
*ctx
= priv
;
228 struct etna_resource
*rsc
, *rsc_tmp
;
230 etna_set_state(stream
, VIVS_GL_API_MODE
, VIVS_GL_API_MODE_OPENGL
);
231 etna_set_state(stream
, VIVS_GL_VERTEX_ELEMENT_CONFIG
, 0x00000001);
232 etna_set_state(stream
, VIVS_RA_EARLY_DEPTH
, 0x00000031);
233 etna_set_state(stream
, VIVS_PA_W_CLIP_LIMIT
, 0x34000001);
237 /* go through all the used resources and clear their status flag */
238 LIST_FOR_EACH_ENTRY_SAFE(rsc
, rsc_tmp
, &ctx
->used_resources
, list
)
240 debug_assert(rsc
->status
!= 0);
242 rsc
->pending_ctx
= NULL
;
243 list_delinit(&rsc
->list
);
246 assert(LIST_IS_EMPTY(&ctx
->used_resources
));
250 etna_set_debug_callback(struct pipe_context
*pctx
,
251 const struct pipe_debug_callback
*cb
)
253 struct etna_context
*ctx
= etna_context(pctx
);
258 memset(&ctx
->debug
, 0, sizeof(ctx
->debug
));
261 struct pipe_context
*
262 etna_context_create(struct pipe_screen
*pscreen
, void *priv
, unsigned flags
)
264 struct etna_context
*ctx
= CALLOC_STRUCT(etna_context
);
265 struct etna_screen
*screen
;
266 struct pipe_context
*pctx
= NULL
;
271 screen
= etna_screen(pscreen
);
272 ctx
->stream
= etna_cmd_stream_new(screen
->pipe
, 0x2000, &etna_cmd_stream_reset_notify
, ctx
);
273 if (ctx
->stream
== NULL
)
278 pctx
->screen
= pscreen
;
280 /* context ctxate setup */
281 ctx
->specs
= screen
->specs
;
282 ctx
->screen
= screen
;
283 /* need some sane default in case state tracker doesn't set some state: */
284 ctx
->sample_mask
= 0xffff;
286 list_inithead(&ctx
->used_resources
);
288 /* Set sensible defaults for state */
289 etna_cmd_stream_reset_notify(ctx
->stream
, ctx
);
291 pctx
->destroy
= etna_context_destroy
;
292 pctx
->draw_vbo
= etna_draw_vbo
;
293 pctx
->flush
= etna_flush
;
294 pctx
->set_debug_callback
= etna_set_debug_callback
;
296 /* creation of compile states */
297 pctx
->create_blend_state
= etna_blend_state_create
;
298 pctx
->create_rasterizer_state
= etna_rasterizer_state_create
;
299 pctx
->create_depth_stencil_alpha_state
= etna_zsa_state_create
;
301 etna_clear_blit_init(pctx
);
302 etna_query_context_init(pctx
);
303 etna_state_init(pctx
);
304 etna_surface_init(pctx
);
305 etna_shader_init(pctx
);
306 etna_texture_init(pctx
);
307 etna_transfer_init(pctx
);
309 ctx
->blitter
= util_blitter_create(pctx
);
313 /* Generate the bitmask of supported draw primitives. */
314 ctx
->prim_hwsupport
= 1 << PIPE_PRIM_POINTS
|
315 1 << PIPE_PRIM_LINES
|
316 1 << PIPE_PRIM_LINE_STRIP
|
317 1 << PIPE_PRIM_TRIANGLES
|
318 1 << PIPE_PRIM_TRIANGLE_STRIP
|
319 1 << PIPE_PRIM_TRIANGLE_FAN
;
321 if (VIV_FEATURE(ctx
->screen
, chipMinorFeatures2
, LINE_LOOP
))
322 ctx
->prim_hwsupport
|= 1 << PIPE_PRIM_LINE_LOOP
;
324 ctx
->primconvert
= util_primconvert_create(pctx
, ctx
->prim_hwsupport
);
325 if (!ctx
->primconvert
)
328 slab_create_child(&ctx
->transfer_pool
, &screen
->transfer_pool
);