etnaviv: use surface format directly
[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_query_hw.h"
38 #include "etnaviv_rasterizer.h"
39 #include "etnaviv_screen.h"
40 #include "etnaviv_shader.h"
41 #include "etnaviv_state.h"
42 #include "etnaviv_surface.h"
43 #include "etnaviv_texture.h"
44 #include "etnaviv_transfer.h"
45 #include "etnaviv_translate.h"
46 #include "etnaviv_zsa.h"
47
48 #include "pipe/p_context.h"
49 #include "pipe/p_state.h"
50 #include "util/u_blitter.h"
51 #include "util/u_helpers.h"
52 #include "util/u_memory.h"
53 #include "util/u_prim.h"
54 #include "util/u_upload_mgr.h"
55
56 #include "hw/common.xml.h"
57
58 static void
59 etna_context_destroy(struct pipe_context *pctx)
60 {
61 struct etna_context *ctx = etna_context(pctx);
62
63 if (ctx->dummy_rt)
64 etna_bo_del(ctx->dummy_rt);
65
66 if (ctx->primconvert)
67 util_primconvert_destroy(ctx->primconvert);
68
69 if (ctx->blitter)
70 util_blitter_destroy(ctx->blitter);
71
72 if (pctx->stream_uploader)
73 u_upload_destroy(pctx->stream_uploader);
74
75 if (ctx->stream)
76 etna_cmd_stream_del(ctx->stream);
77
78 slab_destroy_child(&ctx->transfer_pool);
79
80 if (ctx->in_fence_fd != -1)
81 close(ctx->in_fence_fd);
82
83 FREE(pctx);
84 }
85
86 /* Update render state where needed based on draw operation */
87 static void
88 etna_update_state_for_draw(struct etna_context *ctx, const struct pipe_draw_info *info)
89 {
90 /* Handle primitive restart:
91 * - If not an indexed draw, we don't care about the state of the primitive restart bit.
92 * - Otherwise, set the bit in INDEX_STREAM_CONTROL in the index buffer state
93 * accordingly
94 * - If the value of the INDEX_STREAM_CONTROL register changed due to this, or
95 * primitive restart is enabled and the restart index changed, mark the index
96 * buffer state as dirty
97 */
98
99 if (info->index_size) {
100 uint32_t new_control = ctx->index_buffer.FE_INDEX_STREAM_CONTROL;
101
102 if (info->primitive_restart)
103 new_control |= VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
104 else
105 new_control &= ~VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
106
107 if (ctx->index_buffer.FE_INDEX_STREAM_CONTROL != new_control ||
108 (info->primitive_restart && ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX != info->restart_index)) {
109 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = new_control;
110 ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX = info->restart_index;
111 ctx->dirty |= ETNA_DIRTY_INDEX_BUFFER;
112 }
113 }
114 }
115
116 static bool
117 etna_get_vs(struct etna_context *ctx, struct etna_shader_key key)
118 {
119 const struct etna_shader_variant *old = ctx->shader.vs;
120
121 ctx->shader.vs = etna_shader_variant(ctx->shader.bind_vs, key, &ctx->debug);
122
123 if (!ctx->shader.vs)
124 return false;
125
126 if (old != ctx->shader.vs)
127 ctx->dirty |= ETNA_DIRTY_SHADER;
128
129 return true;
130 }
131
132 static bool
133 etna_get_fs(struct etna_context *ctx, struct etna_shader_key key)
134 {
135 const struct etna_shader_variant *old = ctx->shader.fs;
136
137 ctx->shader.fs = etna_shader_variant(ctx->shader.bind_fs, key, &ctx->debug);
138
139 if (!ctx->shader.fs)
140 return false;
141
142 if (old != ctx->shader.fs)
143 ctx->dirty |= ETNA_DIRTY_SHADER;
144
145 return true;
146 }
147
148 static void
149 etna_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
150 {
151 struct etna_context *ctx = etna_context(pctx);
152 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
153 uint32_t draw_mode;
154 unsigned i;
155
156 if (!info->count_from_stream_output && !info->indirect &&
157 !info->primitive_restart &&
158 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
159 return;
160
161 if (ctx->vertex_elements == NULL || ctx->vertex_elements->num_elements == 0)
162 return; /* Nothing to do */
163
164 if (!(ctx->prim_hwsupport & (1 << info->mode))) {
165 struct primconvert_context *primconvert = ctx->primconvert;
166 util_primconvert_save_rasterizer_state(primconvert, ctx->rasterizer);
167 util_primconvert_draw_vbo(primconvert, info);
168 return;
169 }
170
171 int prims = u_decomposed_prims_for_vertices(info->mode, info->count);
172 if (unlikely(prims <= 0)) {
173 DBG("Invalid draw primitive mode=%i or no primitives to be drawn", info->mode);
174 return;
175 }
176
177 draw_mode = translate_draw_mode(info->mode);
178 if (draw_mode == ETNA_NO_MATCH) {
179 BUG("Unsupported draw mode");
180 return;
181 }
182
183 /* Upload a user index buffer. */
184 unsigned index_offset = 0;
185 struct pipe_resource *indexbuf = NULL;
186
187 if (info->index_size) {
188 indexbuf = info->has_user_indices ? NULL : info->index.resource;
189 if (info->has_user_indices &&
190 !util_upload_index_buffer(pctx, info, &indexbuf, &index_offset)) {
191 BUG("Index buffer upload failed.");
192 return;
193 }
194 /* Add start to index offset, when rendering indexed */
195 index_offset += info->start * info->index_size;
196
197 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo = etna_resource(indexbuf)->bo;
198 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.offset = index_offset;
199 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.flags = ETNA_RELOC_READ;
200 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = translate_index_size(info->index_size);
201
202 if (!ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo) {
203 BUG("Unsupported or no index buffer");
204 return;
205 }
206 } else {
207 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo = 0;
208 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.offset = 0;
209 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.flags = 0;
210 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = 0;
211 }
212 ctx->dirty |= ETNA_DIRTY_INDEX_BUFFER;
213
214 struct etna_shader_key key = {};
215 if (pfb->cbufs[0])
216 key.frag_rb_swap = !!translate_rs_format_rb_swap(pfb->cbufs[0]->format);
217
218 if (!etna_get_vs(ctx, key) || !etna_get_fs(ctx, key)) {
219 BUG("compiled shaders are not okay");
220 return;
221 }
222
223 /* Update any derived state */
224 if (!etna_state_update(ctx))
225 return;
226
227 /*
228 * Figure out the buffers/features we need:
229 */
230 if (etna_depth_enabled(ctx))
231 resource_written(ctx, pfb->zsbuf->texture);
232
233 if (etna_stencil_enabled(ctx))
234 resource_written(ctx, pfb->zsbuf->texture);
235
236 for (i = 0; i < pfb->nr_cbufs; i++) {
237 struct pipe_resource *surf;
238
239 if (!pfb->cbufs[i])
240 continue;
241
242 surf = pfb->cbufs[i]->texture;
243 resource_written(ctx, surf);
244 }
245
246 /* Mark constant buffers as being read */
247 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_VERTEX].buffer);
248 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_FRAGMENT].buffer);
249
250 /* Mark VBOs as being read */
251 for (i = 0; i < ctx->vertex_buffer.count; i++) {
252 assert(!ctx->vertex_buffer.vb[i].is_user_buffer);
253 resource_read(ctx, ctx->vertex_buffer.vb[i].buffer.resource);
254 }
255
256 /* Mark index buffer as being read */
257 resource_read(ctx, indexbuf);
258
259 /* Mark textures as being read */
260 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
261 if (ctx->sampler_view[i])
262 resource_read(ctx, ctx->sampler_view[i]->texture);
263
264 list_for_each_entry(struct etna_hw_query, hq, &ctx->active_hw_queries, node)
265 resource_written(ctx, hq->prsc);
266
267 ctx->stats.prims_emitted += u_reduced_prims_for_vertices(info->mode, info->count);
268 ctx->stats.draw_calls++;
269
270 /* Update state for this draw operation */
271 etna_update_state_for_draw(ctx, info);
272
273 /* First, sync state, then emit DRAW_PRIMITIVES or DRAW_INDEXED_PRIMITIVES */
274 etna_emit_state(ctx);
275
276 if (ctx->specs.halti >= 2) {
277 /* On HALTI2+ (GC3000 and higher) only use instanced drawing commands, as the blob does */
278 etna_draw_instanced(ctx->stream, info->index_size, draw_mode, 1,
279 info->count, info->index_size ? info->index_bias : info->start);
280 } else {
281 if (info->index_size)
282 etna_draw_indexed_primitives(ctx->stream, draw_mode, 0, prims, info->index_bias);
283 else
284 etna_draw_primitives(ctx->stream, draw_mode, info->start, prims);
285 }
286
287 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL)) {
288 /* Stall the FE after every draw operation. This allows better
289 * debug of GPU hang conditions, as the FE will indicate which
290 * draw op has caused the hang. */
291 etna_stall(ctx->stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
292 }
293
294 if (DBG_ENABLED(ETNA_DBG_FLUSH_ALL))
295 pctx->flush(pctx, NULL, 0);
296
297 if (ctx->framebuffer.cbuf)
298 etna_resource(ctx->framebuffer.cbuf->texture)->seqno++;
299 if (ctx->framebuffer.zsbuf)
300 etna_resource(ctx->framebuffer.zsbuf->texture)->seqno++;
301 if (info->index_size && indexbuf != info->index.resource)
302 pipe_resource_reference(&indexbuf, NULL);
303 }
304
305 static void
306 etna_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
307 enum pipe_flush_flags flags)
308 {
309 struct etna_context *ctx = etna_context(pctx);
310 int out_fence_fd = -1;
311
312 list_for_each_entry(struct etna_hw_query, hq, &ctx->active_hw_queries, node)
313 etna_hw_query_suspend(hq, ctx);
314
315 etna_cmd_stream_flush2(ctx->stream, ctx->in_fence_fd,
316 (flags & PIPE_FLUSH_FENCE_FD) ? &out_fence_fd :
317 NULL);
318
319 list_for_each_entry(struct etna_hw_query, hq, &ctx->active_hw_queries, node)
320 etna_hw_query_resume(hq, ctx);
321
322 if (fence)
323 *fence = etna_fence_create(pctx, out_fence_fd);
324 }
325
326 static void
327 etna_cmd_stream_reset_notify(struct etna_cmd_stream *stream, void *priv)
328 {
329 struct etna_context *ctx = priv;
330 struct etna_resource *rsc, *rsc_tmp;
331
332 etna_set_state(stream, VIVS_GL_API_MODE, VIVS_GL_API_MODE_OPENGL);
333 etna_set_state(stream, VIVS_GL_VERTEX_ELEMENT_CONFIG, 0x00000001);
334 /* blob sets this to 0x40000031 on GC7000, seems to make no difference,
335 * but keep it in mind if depth behaves strangely. */
336 etna_set_state(stream, VIVS_RA_EARLY_DEPTH, 0x00000031);
337 etna_set_state(stream, VIVS_PA_W_CLIP_LIMIT, 0x34000001);
338 etna_set_state(stream, VIVS_PA_FLAGS, 0x00000000); /* blob sets ZCONVERT_BYPASS on GC3000+, this messes up z for us */
339 etna_set_state(stream, VIVS_PA_VIEWPORT_UNK00A80, 0x38a01404);
340 etna_set_state(stream, VIVS_PA_VIEWPORT_UNK00A84, fui(8192.0));
341 etna_set_state(stream, VIVS_PA_ZFARCLIPPING, 0x00000000);
342 etna_set_state(stream, VIVS_PE_ALPHA_COLOR_EXT0, 0x00000000);
343 etna_set_state(stream, VIVS_PE_ALPHA_COLOR_EXT1, 0x00000000);
344 etna_set_state(stream, VIVS_RA_HDEPTH_CONTROL, 0x00007000);
345 etna_set_state(stream, VIVS_PE_STENCIL_CONFIG_EXT2, 0x00000000);
346 etna_set_state(stream, VIVS_PS_CONTROL_EXT, 0x00000000);
347
348 /* There is no HALTI0 specific state */
349 if (ctx->specs.halti >= 1) { /* Only on HALTI1+ */
350 etna_set_state(stream, VIVS_VS_HALTI1_UNK00884, 0x00000808);
351 }
352 if (ctx->specs.halti >= 2) { /* Only on HALTI2+ */
353 etna_set_state(stream, VIVS_RA_UNK00E0C, 0x00000000);
354 }
355 if (ctx->specs.halti >= 3) { /* Only on HALTI3+ */
356 etna_set_state(stream, VIVS_PE_MEM_CONFIG, 0x00000000); /* TODO: cache modes */
357 etna_set_state(stream, VIVS_PS_HALTI3_UNK0103C, 0x76543210);
358 }
359 if (ctx->specs.halti >= 4) { /* Only on HALTI4+ */
360 etna_set_state(stream, VIVS_PS_MSAA_CONFIG, 0x6fffffff & 0xf70fffff & 0xfff6ffff &
361 0xffff6fff & 0xfffff6ff & 0xffffff7f);
362 etna_set_state(stream, VIVS_PE_HALTI4_UNK014C0, 0x00000000);
363 }
364 if (ctx->specs.halti >= 5) { /* Only on HALTI5+ */
365 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_UNK14C40, 0x00000001);
366 etna_set_state(stream, VIVS_FE_HALTI5_UNK007D8, 0x00000002);
367 etna_set_state(stream, VIVS_FE_HALTI5_UNK007C4, 0x00000000);
368 etna_set_state(stream, VIVS_PS_SAMPLER_BASE, 0x00000000);
369 etna_set_state(stream, VIVS_VS_SAMPLER_BASE, 0x00000020);
370 etna_set_state(stream, VIVS_SH_CONFIG, VIVS_SH_CONFIG_RTNE_ROUNDING);
371 } else { /* Only on pre-HALTI5 */
372 etna_set_state(stream, VIVS_GL_UNK03834, 0x00000000);
373 etna_set_state(stream, VIVS_GL_UNK03838, 0x00000000);
374 etna_set_state(stream, VIVS_GL_UNK03854, 0x00000000);
375 }
376
377 if (!ctx->specs.use_blt) {
378 /* Enable SINGLE_BUFFER for resolve, if supported */
379 etna_set_state(stream, VIVS_RS_SINGLE_BUFFER, COND(ctx->specs.single_buffer, VIVS_RS_SINGLE_BUFFER_ENABLE));
380 }
381
382 ctx->dirty = ~0L;
383 ctx->dirty_sampler_views = ~0L;
384
385 /* go through all the used resources and clear their status flag */
386 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &ctx->used_resources, list)
387 {
388 debug_assert(rsc->status != 0);
389 rsc->status = 0;
390 rsc->pending_ctx = NULL;
391 list_delinit(&rsc->list);
392 }
393
394 assert(LIST_IS_EMPTY(&ctx->used_resources));
395 }
396
397 static void
398 etna_set_debug_callback(struct pipe_context *pctx,
399 const struct pipe_debug_callback *cb)
400 {
401 struct etna_context *ctx = etna_context(pctx);
402
403 if (cb)
404 ctx->debug = *cb;
405 else
406 memset(&ctx->debug, 0, sizeof(ctx->debug));
407 }
408
409 struct pipe_context *
410 etna_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
411 {
412 struct etna_context *ctx = CALLOC_STRUCT(etna_context);
413 struct etna_screen *screen;
414 struct pipe_context *pctx;
415
416 if (ctx == NULL)
417 return NULL;
418
419 pctx = &ctx->base;
420 pctx->priv = ctx;
421 pctx->screen = pscreen;
422 pctx->stream_uploader = u_upload_create_default(pctx);
423 if (!pctx->stream_uploader)
424 goto fail;
425 pctx->const_uploader = pctx->stream_uploader;
426
427 screen = etna_screen(pscreen);
428 ctx->stream = etna_cmd_stream_new(screen->pipe, 0x2000, &etna_cmd_stream_reset_notify, ctx);
429 if (ctx->stream == NULL)
430 goto fail;
431
432 /* context ctxate setup */
433 ctx->specs = screen->specs;
434 ctx->screen = screen;
435 /* need some sane default in case state tracker doesn't set some state: */
436 ctx->sample_mask = 0xffff;
437
438 list_inithead(&ctx->used_resources);
439
440 /* Set sensible defaults for state */
441 etna_cmd_stream_reset_notify(ctx->stream, ctx);
442
443 ctx->in_fence_fd = -1;
444
445 pctx->destroy = etna_context_destroy;
446 pctx->draw_vbo = etna_draw_vbo;
447 pctx->flush = etna_flush;
448 pctx->set_debug_callback = etna_set_debug_callback;
449 pctx->create_fence_fd = etna_create_fence_fd;
450 pctx->fence_server_sync = etna_fence_server_sync;
451
452 /* creation of compile states */
453 pctx->create_blend_state = etna_blend_state_create;
454 pctx->create_rasterizer_state = etna_rasterizer_state_create;
455 pctx->create_depth_stencil_alpha_state = etna_zsa_state_create;
456
457 etna_clear_blit_init(pctx);
458 etna_query_context_init(pctx);
459 etna_state_init(pctx);
460 etna_surface_init(pctx);
461 etna_shader_init(pctx);
462 etna_texture_init(pctx);
463 etna_transfer_init(pctx);
464
465 ctx->blitter = util_blitter_create(pctx);
466 if (!ctx->blitter)
467 goto fail;
468
469 /* Generate the bitmask of supported draw primitives. */
470 ctx->prim_hwsupport = 1 << PIPE_PRIM_POINTS |
471 1 << PIPE_PRIM_LINES |
472 1 << PIPE_PRIM_LINE_STRIP |
473 1 << PIPE_PRIM_TRIANGLES |
474 1 << PIPE_PRIM_TRIANGLE_STRIP |
475 1 << PIPE_PRIM_TRIANGLE_FAN;
476
477 if (VIV_FEATURE(ctx->screen, chipMinorFeatures2, LINE_LOOP))
478 ctx->prim_hwsupport |= 1 << PIPE_PRIM_LINE_LOOP;
479
480 ctx->primconvert = util_primconvert_create(pctx, ctx->prim_hwsupport);
481 if (!ctx->primconvert)
482 goto fail;
483
484 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
485 list_inithead(&ctx->active_hw_queries);
486
487 /* create dummy RT buffer, used when rendering with no color buffer */
488 ctx->dummy_rt = etna_bo_new(ctx->screen->dev, 64 * 64 * 4,
489 DRM_ETNA_GEM_CACHE_WC);
490 if (!ctx->dummy_rt)
491 goto fail;
492
493 ctx->dummy_rt_reloc.bo = ctx->dummy_rt;
494 ctx->dummy_rt_reloc.offset = 0;
495 ctx->dummy_rt_reloc.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
496
497 return pctx;
498
499 fail:
500 pctx->destroy(pctx);
501
502 return NULL;
503 }