gallium: fold u_trim_pipe_prim call from st/mesa to drivers
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_context.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_context.h"
29
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"
46
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"
54
55 #include "hw/common.xml.h"
56
57 static void
58 etna_context_destroy(struct pipe_context *pctx)
59 {
60 struct etna_context *ctx = etna_context(pctx);
61
62 if (ctx->primconvert)
63 util_primconvert_destroy(ctx->primconvert);
64
65 if (ctx->blitter)
66 util_blitter_destroy(ctx->blitter);
67
68 if (pctx->stream_uploader)
69 u_upload_destroy(pctx->stream_uploader);
70
71 if (ctx->stream)
72 etna_cmd_stream_del(ctx->stream);
73
74 slab_destroy_child(&ctx->transfer_pool);
75
76 if (ctx->in_fence_fd != -1)
77 close(ctx->in_fence_fd);
78
79 FREE(pctx);
80 }
81
82 /* Update render state where needed based on draw operation */
83 static void
84 etna_update_state_for_draw(struct etna_context *ctx, const struct pipe_draw_info *info)
85 {
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
89 * accordingly
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
93 */
94
95 if (info->indexed) {
96 uint32_t new_control = ctx->index_buffer.FE_INDEX_STREAM_CONTROL;
97
98 if (info->primitive_restart)
99 new_control |= VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
100 else
101 new_control &= ~VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
102
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;
108 }
109 }
110 }
111
112 static bool
113 etna_get_vs(struct etna_context *ctx, struct etna_shader_key key)
114 {
115 const struct etna_shader_variant *old = ctx->shader.vs;
116
117 ctx->shader.vs = etna_shader_variant(ctx->shader.bind_vs, key, &ctx->debug);
118
119 if (!ctx->shader.vs)
120 return false;
121
122 if (old != ctx->shader.vs)
123 ctx->dirty |= ETNA_DIRTY_SHADER;
124
125 return true;
126 }
127
128 static bool
129 etna_get_fs(struct etna_context *ctx, struct etna_shader_key key)
130 {
131 const struct etna_shader_variant *old = ctx->shader.fs;
132
133 ctx->shader.fs = etna_shader_variant(ctx->shader.bind_fs, key, &ctx->debug);
134
135 if (!ctx->shader.fs)
136 return false;
137
138 if (old != ctx->shader.fs)
139 ctx->dirty |= ETNA_DIRTY_SHADER;
140
141 return true;
142 }
143
144 static void
145 etna_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
146 {
147 struct etna_context *ctx = etna_context(pctx);
148 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
149 uint32_t draw_mode;
150 unsigned i;
151
152 if (!info->count_from_stream_output && !info->indirect &&
153 !info->primitive_restart &&
154 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
155 return;
156
157 if (ctx->vertex_elements == NULL || ctx->vertex_elements->num_elements == 0)
158 return; /* Nothing to do */
159
160 if (!(ctx->prim_hwsupport & (1 << info->mode))) {
161 struct primconvert_context *primconvert = ctx->primconvert;
162 util_primconvert_save_index_buffer(primconvert, &ctx->index_buffer.ib);
163 util_primconvert_save_rasterizer_state(primconvert, ctx->rasterizer);
164 util_primconvert_draw_vbo(primconvert, info);
165 return;
166 }
167
168 int prims = u_decomposed_prims_for_vertices(info->mode, info->count);
169 if (unlikely(prims <= 0)) {
170 DBG("Invalid draw primitive mode=%i or no primitives to be drawn", info->mode);
171 return;
172 }
173
174 draw_mode = translate_draw_mode(info->mode);
175 if (draw_mode == ETNA_NO_MATCH) {
176 BUG("Unsupported draw mode");
177 return;
178 }
179
180 /* Upload a user index buffer. */
181 struct pipe_index_buffer ibuffer_saved = {};
182 if (info->indexed && ctx->index_buffer.ib.user_buffer &&
183 !util_save_and_upload_index_buffer(pctx, info, &ctx->index_buffer.ib,
184 &ibuffer_saved)) {
185 BUG("Index buffer upload failed.");
186 return;
187 }
188
189 if (info->indexed && !ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo) {
190 BUG("Unsupported or no index buffer");
191 return;
192 }
193
194 struct etna_shader_key key = {};
195 struct etna_surface *cbuf = etna_surface(pfb->cbufs[0]);
196
197 if (cbuf) {
198 struct etna_resource *res = etna_resource(cbuf->base.texture);
199
200 key.frag_rb_swap = !!translate_rs_format_rb_swap(res->base.format);
201 }
202
203 if (!etna_get_vs(ctx, key) || !etna_get_fs(ctx, key)) {
204 BUG("compiled shaders are not okay");
205 return;
206 }
207
208 /* Update any derived state */
209 if (!etna_state_update(ctx))
210 return;
211
212 /*
213 * Figure out the buffers/features we need:
214 */
215 if (etna_depth_enabled(ctx))
216 resource_written(ctx, pfb->zsbuf->texture);
217
218 if (etna_stencil_enabled(ctx))
219 resource_written(ctx, pfb->zsbuf->texture);
220
221 for (i = 0; i < pfb->nr_cbufs; i++) {
222 struct pipe_resource *surf;
223
224 if (!pfb->cbufs[i])
225 continue;
226
227 surf = pfb->cbufs[i]->texture;
228 resource_written(ctx, surf);
229 }
230
231 /* Mark constant buffers as being read */
232 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_VERTEX].buffer);
233 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_FRAGMENT].buffer);
234
235 /* Mark VBOs as being read */
236 for (i = 0; i < ctx->vertex_buffer.count; i++) {
237 assert(!ctx->vertex_buffer.vb[i].user_buffer);
238 resource_read(ctx, ctx->vertex_buffer.vb[i].buffer);
239 }
240
241 /* Mark index buffer as being read */
242 resource_read(ctx, ctx->index_buffer.ib.buffer);
243
244 /* Mark textures as being read */
245 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
246 if (ctx->sampler_view[i])
247 resource_read(ctx, ctx->sampler_view[i]->texture);
248
249 ctx->stats.prims_emitted += u_reduced_prims_for_vertices(info->mode, info->count);
250 ctx->stats.draw_calls++;
251
252 /* Update state for this draw operation */
253 etna_update_state_for_draw(ctx, info);
254
255 /* First, sync state, then emit DRAW_PRIMITIVES or DRAW_INDEXED_PRIMITIVES */
256 etna_emit_state(ctx);
257
258 if (info->indexed)
259 etna_draw_indexed_primitives(ctx->stream, draw_mode, info->start, prims, info->index_bias);
260 else
261 etna_draw_primitives(ctx->stream, draw_mode, info->start, prims);
262
263 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL)) {
264 /* Stall the FE after every draw operation. This allows better
265 * debug of GPU hang conditions, as the FE will indicate which
266 * draw op has caused the hang. */
267 etna_stall(ctx->stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
268 }
269
270 if (DBG_ENABLED(ETNA_DBG_FLUSH_ALL))
271 pctx->flush(pctx, NULL, 0);
272
273 if (ctx->framebuffer.cbuf)
274 etna_resource(ctx->framebuffer.cbuf->texture)->seqno++;
275 if (ctx->framebuffer.zsbuf)
276 etna_resource(ctx->framebuffer.zsbuf->texture)->seqno++;
277 if (info->indexed && ibuffer_saved.user_buffer)
278 pctx->set_index_buffer(pctx, &ibuffer_saved);
279 }
280
281 static void
282 etna_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
283 enum pipe_flush_flags flags)
284 {
285 struct etna_context *ctx = etna_context(pctx);
286 int out_fence_fd = -1;
287
288 etna_cmd_stream_flush2(ctx->stream, ctx->in_fence_fd,
289 (flags & PIPE_FLUSH_FENCE_FD) ? &out_fence_fd :
290 NULL);
291
292 if (fence)
293 *fence = etna_fence_create(pctx, out_fence_fd);
294 }
295
296 static void
297 etna_cmd_stream_reset_notify(struct etna_cmd_stream *stream, void *priv)
298 {
299 struct etna_context *ctx = priv;
300 struct etna_resource *rsc, *rsc_tmp;
301
302 etna_set_state(stream, VIVS_GL_API_MODE, VIVS_GL_API_MODE_OPENGL);
303 etna_set_state(stream, VIVS_GL_VERTEX_ELEMENT_CONFIG, 0x00000001);
304 etna_set_state(stream, VIVS_RA_EARLY_DEPTH, 0x00000031);
305 etna_set_state(stream, VIVS_PA_W_CLIP_LIMIT, 0x34000001);
306
307 /* Enable SINGLE_BUFFER for resolve, if supported */
308 etna_set_state(stream, VIVS_RS_SINGLE_BUFFER, COND(ctx->specs.single_buffer, VIVS_RS_SINGLE_BUFFER_ENABLE));
309
310 ctx->dirty = ~0L;
311
312 /* go through all the used resources and clear their status flag */
313 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &ctx->used_resources, list)
314 {
315 debug_assert(rsc->status != 0);
316 rsc->status = 0;
317 rsc->pending_ctx = NULL;
318 list_delinit(&rsc->list);
319 }
320
321 assert(LIST_IS_EMPTY(&ctx->used_resources));
322 }
323
324 static void
325 etna_set_debug_callback(struct pipe_context *pctx,
326 const struct pipe_debug_callback *cb)
327 {
328 struct etna_context *ctx = etna_context(pctx);
329
330 if (cb)
331 ctx->debug = *cb;
332 else
333 memset(&ctx->debug, 0, sizeof(ctx->debug));
334 }
335
336 struct pipe_context *
337 etna_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
338 {
339 struct etna_context *ctx = CALLOC_STRUCT(etna_context);
340 struct etna_screen *screen;
341 struct pipe_context *pctx;
342
343 if (ctx == NULL)
344 return NULL;
345
346 pctx = &ctx->base;
347 pctx->priv = ctx;
348 pctx->screen = pscreen;
349 pctx->stream_uploader = u_upload_create_default(pctx);
350 if (!pctx->stream_uploader)
351 goto fail;
352 pctx->const_uploader = pctx->stream_uploader;
353
354 screen = etna_screen(pscreen);
355 ctx->stream = etna_cmd_stream_new(screen->pipe, 0x2000, &etna_cmd_stream_reset_notify, ctx);
356 if (ctx->stream == NULL)
357 goto fail;
358
359 /* context ctxate setup */
360 ctx->specs = screen->specs;
361 ctx->screen = screen;
362 /* need some sane default in case state tracker doesn't set some state: */
363 ctx->sample_mask = 0xffff;
364
365 list_inithead(&ctx->used_resources);
366
367 /* Set sensible defaults for state */
368 etna_cmd_stream_reset_notify(ctx->stream, ctx);
369
370 ctx->in_fence_fd = -1;
371
372 pctx->destroy = etna_context_destroy;
373 pctx->draw_vbo = etna_draw_vbo;
374 pctx->flush = etna_flush;
375 pctx->set_debug_callback = etna_set_debug_callback;
376 pctx->create_fence_fd = etna_create_fence_fd;
377 pctx->fence_server_sync = etna_fence_server_sync;
378
379 /* creation of compile states */
380 pctx->create_blend_state = etna_blend_state_create;
381 pctx->create_rasterizer_state = etna_rasterizer_state_create;
382 pctx->create_depth_stencil_alpha_state = etna_zsa_state_create;
383
384 etna_clear_blit_init(pctx);
385 etna_query_context_init(pctx);
386 etna_state_init(pctx);
387 etna_surface_init(pctx);
388 etna_shader_init(pctx);
389 etna_texture_init(pctx);
390 etna_transfer_init(pctx);
391
392 ctx->blitter = util_blitter_create(pctx);
393 if (!ctx->blitter)
394 goto fail;
395
396 /* Generate the bitmask of supported draw primitives. */
397 ctx->prim_hwsupport = 1 << PIPE_PRIM_POINTS |
398 1 << PIPE_PRIM_LINES |
399 1 << PIPE_PRIM_LINE_STRIP |
400 1 << PIPE_PRIM_TRIANGLES |
401 1 << PIPE_PRIM_TRIANGLE_STRIP |
402 1 << PIPE_PRIM_TRIANGLE_FAN;
403
404 if (VIV_FEATURE(ctx->screen, chipMinorFeatures2, LINE_LOOP))
405 ctx->prim_hwsupport |= 1 << PIPE_PRIM_LINE_LOOP;
406
407 ctx->primconvert = util_primconvert_create(pctx, ctx->prim_hwsupport);
408 if (!ctx->primconvert)
409 goto fail;
410
411 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
412
413 return pctx;
414
415 fail:
416 pctx->destroy(pctx);
417
418 return NULL;
419 }