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_acc.h"
38 #include "etnaviv_rasterizer.h"
39 #include "etnaviv_resource.h"
40 #include "etnaviv_screen.h"
41 #include "etnaviv_shader.h"
42 #include "etnaviv_state.h"
43 #include "etnaviv_surface.h"
44 #include "etnaviv_texture.h"
45 #include "etnaviv_transfer.h"
46 #include "etnaviv_translate.h"
47 #include "etnaviv_zsa.h"
48
49 #include "pipe/p_context.h"
50 #include "pipe/p_state.h"
51 #include "util/hash_table.h"
52 #include "util/u_blitter.h"
53 #include "util/u_helpers.h"
54 #include "util/u_memory.h"
55 #include "util/u_prim.h"
56 #include "util/u_upload_mgr.h"
57
58 #include "hw/common.xml.h"
59
60 static inline void
etna_emit_nop_with_data(struct etna_cmd_stream * stream,uint32_t value)61 etna_emit_nop_with_data(struct etna_cmd_stream *stream, uint32_t value)
62 {
63 etna_cmd_stream_emit(stream, VIV_FE_NOP_HEADER_OP_NOP);
64 etna_cmd_stream_emit(stream, value);
65 }
66
67 static void
etna_emit_string_marker(struct pipe_context * pctx,const char * string,int len)68 etna_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
69 {
70 struct etna_context *ctx = etna_context(pctx);
71 struct etna_cmd_stream *stream = ctx->stream;
72 const uint32_t *buf = (const void *)string;
73
74 etna_cmd_stream_reserve(stream, len * 2);
75
76 while (len >= 4) {
77 etna_emit_nop_with_data(stream, *buf);
78 buf++;
79 len -= 4;
80 }
81
82 /* copy remainder bytes without reading past end of input string */
83 if (len > 0) {
84 uint32_t w = 0;
85 memcpy(&w, buf, len);
86 etna_emit_nop_with_data(stream, w);
87 }
88 }
89
90 static void
etna_context_destroy(struct pipe_context * pctx)91 etna_context_destroy(struct pipe_context *pctx)
92 {
93 struct etna_context *ctx = etna_context(pctx);
94
95 mtx_lock(&ctx->lock);
96
97 if (ctx->used_resources_read) {
98
99 /*
100 * There should be no resources tracked in the context when it's being
101 * destroyed. Be sure there are none to avoid memory leaks on buggy
102 * programs.
103 */
104 set_foreach(ctx->used_resources_read, entry) {
105 struct etna_resource *rsc = (struct etna_resource *)entry->key;
106
107 mtx_lock(&rsc->lock);
108 _mesa_set_remove_key(rsc->pending_ctx, ctx);
109 mtx_unlock(&rsc->lock);
110 }
111 _mesa_set_destroy(ctx->used_resources_read, NULL);
112
113 }
114 if (ctx->used_resources_write) {
115
116 /*
117 * There should be no resources tracked in the context when it's being
118 * destroyed. Be sure there are none to avoid memory leaks on buggy
119 * programs.
120 */
121 set_foreach(ctx->used_resources_write, entry) {
122 struct etna_resource *rsc = (struct etna_resource *)entry->key;
123
124 mtx_lock(&rsc->lock);
125 _mesa_set_remove_key(rsc->pending_ctx, ctx);
126 mtx_unlock(&rsc->lock);
127 }
128 _mesa_set_destroy(ctx->used_resources_write, NULL);
129
130 }
131 mtx_unlock(&ctx->lock);
132
133 if (ctx->dummy_desc_bo)
134 etna_bo_del(ctx->dummy_desc_bo);
135
136 if (ctx->dummy_rt)
137 etna_bo_del(ctx->dummy_rt);
138
139 util_copy_framebuffer_state(&ctx->framebuffer_s, NULL);
140
141 if (ctx->primconvert)
142 util_primconvert_destroy(ctx->primconvert);
143
144 if (ctx->blitter)
145 util_blitter_destroy(ctx->blitter);
146
147 if (pctx->stream_uploader)
148 u_upload_destroy(pctx->stream_uploader);
149
150 if (ctx->stream)
151 etna_cmd_stream_del(ctx->stream);
152
153 slab_destroy_child(&ctx->transfer_pool);
154
155 if (ctx->in_fence_fd != -1)
156 close(ctx->in_fence_fd);
157
158 mtx_destroy(&ctx->lock);
159
160 FREE(pctx);
161 }
162
163 /* Update render state where needed based on draw operation */
164 static void
etna_update_state_for_draw(struct etna_context * ctx,const struct pipe_draw_info * info)165 etna_update_state_for_draw(struct etna_context *ctx, const struct pipe_draw_info *info)
166 {
167 /* Handle primitive restart:
168 * - If not an indexed draw, we don't care about the state of the primitive restart bit.
169 * - Otherwise, set the bit in INDEX_STREAM_CONTROL in the index buffer state
170 * accordingly
171 * - If the value of the INDEX_STREAM_CONTROL register changed due to this, or
172 * primitive restart is enabled and the restart index changed, mark the index
173 * buffer state as dirty
174 */
175
176 if (info->index_size) {
177 uint32_t new_control = ctx->index_buffer.FE_INDEX_STREAM_CONTROL;
178
179 if (info->primitive_restart)
180 new_control |= VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
181 else
182 new_control &= ~VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
183
184 if (ctx->index_buffer.FE_INDEX_STREAM_CONTROL != new_control ||
185 (info->primitive_restart && ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX != info->restart_index)) {
186 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = new_control;
187 ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX = info->restart_index;
188 ctx->dirty |= ETNA_DIRTY_INDEX_BUFFER;
189 }
190 }
191 }
192
193 static bool
etna_get_vs(struct etna_context * ctx,struct etna_shader_key key)194 etna_get_vs(struct etna_context *ctx, struct etna_shader_key key)
195 {
196 const struct etna_shader_variant *old = ctx->shader.vs;
197
198 ctx->shader.vs = etna_shader_variant(ctx->shader.bind_vs, key, &ctx->debug);
199
200 if (!ctx->shader.vs)
201 return false;
202
203 if (old != ctx->shader.vs)
204 ctx->dirty |= ETNA_DIRTY_SHADER;
205
206 return true;
207 }
208
209 static bool
etna_get_fs(struct etna_context * ctx,struct etna_shader_key key)210 etna_get_fs(struct etna_context *ctx, struct etna_shader_key key)
211 {
212 const struct etna_shader_variant *old = ctx->shader.fs;
213
214 ctx->shader.fs = etna_shader_variant(ctx->shader.bind_fs, key, &ctx->debug);
215
216 if (!ctx->shader.fs)
217 return false;
218
219 if (old != ctx->shader.fs)
220 ctx->dirty |= ETNA_DIRTY_SHADER;
221
222 return true;
223 }
224
225 static void
etna_draw_vbo(struct pipe_context * pctx,const struct pipe_draw_info * info)226 etna_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
227 {
228 struct etna_context *ctx = etna_context(pctx);
229 struct etna_screen *screen = ctx->screen;
230 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
231 uint32_t draw_mode;
232 unsigned i;
233
234 if (!info->count_from_stream_output && !info->indirect &&
235 !info->primitive_restart &&
236 !u_trim_pipe_prim(info->mode, (unsigned*)&info->count))
237 return;
238
239 if (ctx->vertex_elements == NULL || ctx->vertex_elements->num_elements == 0)
240 return; /* Nothing to do */
241
242 if (!(ctx->prim_hwsupport & (1 << info->mode))) {
243 struct primconvert_context *primconvert = ctx->primconvert;
244 util_primconvert_save_rasterizer_state(primconvert, ctx->rasterizer);
245 util_primconvert_draw_vbo(primconvert, info);
246 return;
247 }
248
249 int prims = u_decomposed_prims_for_vertices(info->mode, info->count);
250 if (unlikely(prims <= 0)) {
251 DBG("Invalid draw primitive mode=%i or no primitives to be drawn", info->mode);
252 return;
253 }
254
255 draw_mode = translate_draw_mode(info->mode);
256 if (draw_mode == ETNA_NO_MATCH) {
257 BUG("Unsupported draw mode");
258 return;
259 }
260
261 /* Upload a user index buffer. */
262 unsigned index_offset = 0;
263 struct pipe_resource *indexbuf = NULL;
264
265 if (info->index_size) {
266 indexbuf = info->has_user_indices ? NULL : info->index.resource;
267 if (info->has_user_indices &&
268 !util_upload_index_buffer(pctx, info, &indexbuf, &index_offset, 4)) {
269 BUG("Index buffer upload failed.");
270 return;
271 }
272 /* Add start to index offset, when rendering indexed */
273 index_offset += info->start * info->index_size;
274
275 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo = etna_resource(indexbuf)->bo;
276 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.offset = index_offset;
277 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.flags = ETNA_RELOC_READ;
278 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = translate_index_size(info->index_size);
279
280 if (!ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo) {
281 BUG("Unsupported or no index buffer");
282 return;
283 }
284 } else {
285 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo = 0;
286 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.offset = 0;
287 ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.flags = 0;
288 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = 0;
289 }
290 ctx->dirty |= ETNA_DIRTY_INDEX_BUFFER;
291
292 struct etna_shader_key key = {
293 .front_ccw = ctx->rasterizer->front_ccw,
294 };
295
296 if (pfb->cbufs[0])
297 key.frag_rb_swap = !!translate_pe_format_rb_swap(pfb->cbufs[0]->format);
298
299 if (!etna_get_vs(ctx, key) || !etna_get_fs(ctx, key)) {
300 BUG("compiled shaders are not okay");
301 return;
302 }
303
304 /* Update any derived state */
305 if (!etna_state_update(ctx))
306 return;
307
308 mtx_lock(&ctx->lock);
309
310 /*
311 * Figure out the buffers/features we need:
312 */
313 if (etna_depth_enabled(ctx))
314 resource_written(ctx, pfb->zsbuf->texture);
315
316 if (etna_stencil_enabled(ctx))
317 resource_written(ctx, pfb->zsbuf->texture);
318
319 for (i = 0; i < pfb->nr_cbufs; i++) {
320 struct pipe_resource *surf;
321
322 if (!pfb->cbufs[i])
323 continue;
324
325 surf = pfb->cbufs[i]->texture;
326 resource_written(ctx, surf);
327 }
328
329 /* Mark constant buffers as being read */
330 foreach_bit(i, ctx->constant_buffer[PIPE_SHADER_VERTEX].enabled_mask)
331 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_VERTEX].cb[i].buffer);
332
333 foreach_bit(i, ctx->constant_buffer[PIPE_SHADER_FRAGMENT].enabled_mask)
334 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_FRAGMENT].cb[i].buffer);
335
336 /* Mark VBOs as being read */
337 foreach_bit(i, ctx->vertex_buffer.enabled_mask) {
338 assert(!ctx->vertex_buffer.vb[i].is_user_buffer);
339 resource_read(ctx, ctx->vertex_buffer.vb[i].buffer.resource);
340 }
341
342 /* Mark index buffer as being read */
343 resource_read(ctx, indexbuf);
344
345 /* Mark textures as being read */
346 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
347 if (ctx->sampler_view[i]) {
348 resource_read(ctx, ctx->sampler_view[i]->texture);
349
350 /* if texture was modified since the last update,
351 * we need to clear the texture cache and possibly
352 * resolve/update ts
353 */
354 etna_update_sampler_source(ctx->sampler_view[i], i);
355 }
356 }
357
358 ctx->stats.prims_generated += u_reduced_prims_for_vertices(info->mode, info->count);
359 ctx->stats.draw_calls++;
360
361 /* Update state for this draw operation */
362 etna_update_state_for_draw(ctx, info);
363
364 /* First, sync state, then emit DRAW_PRIMITIVES or DRAW_INDEXED_PRIMITIVES */
365 etna_emit_state(ctx);
366
367 if (screen->specs.halti >= 2) {
368 /* On HALTI2+ (GC3000 and higher) only use instanced drawing commands, as the blob does */
369 etna_draw_instanced(ctx->stream, info->index_size, draw_mode, info->instance_count,
370 info->count, info->index_size ? info->index_bias : info->start);
371 } else {
372 if (info->index_size)
373 etna_draw_indexed_primitives(ctx->stream, draw_mode, 0, prims, info->index_bias);
374 else
375 etna_draw_primitives(ctx->stream, draw_mode, info->start, prims);
376 }
377
378 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL)) {
379 /* Stall the FE after every draw operation. This allows better
380 * debug of GPU hang conditions, as the FE will indicate which
381 * draw op has caused the hang. */
382 etna_stall(ctx->stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
383 }
384 mtx_unlock(&ctx->lock);
385
386 if (DBG_ENABLED(ETNA_DBG_FLUSH_ALL))
387 pctx->flush(pctx, NULL, 0);
388
389 if (ctx->framebuffer_s.cbufs[0])
390 etna_resource(ctx->framebuffer_s.cbufs[0]->texture)->seqno++;
391 if (ctx->framebuffer_s.zsbuf)
392 etna_resource(ctx->framebuffer_s.zsbuf->texture)->seqno++;
393 if (info->index_size && indexbuf != info->index.resource)
394 pipe_resource_reference(&indexbuf, NULL);
395 }
396
397 static void
etna_reset_gpu_state(struct etna_context * ctx)398 etna_reset_gpu_state(struct etna_context *ctx)
399 {
400 struct etna_cmd_stream *stream = ctx->stream;
401 struct etna_screen *screen = ctx->screen;
402
403 etna_set_state(stream, VIVS_GL_API_MODE, VIVS_GL_API_MODE_OPENGL);
404 etna_set_state(stream, VIVS_GL_VERTEX_ELEMENT_CONFIG, 0x00000001);
405 etna_set_state(stream, VIVS_PA_W_CLIP_LIMIT, 0x34000001);
406 etna_set_state(stream, VIVS_PA_FLAGS, 0x00000000); /* blob sets ZCONVERT_BYPASS on GC3000+, this messes up z for us */
407 etna_set_state(stream, VIVS_PA_VIEWPORT_UNK00A80, 0x38a01404);
408 etna_set_state(stream, VIVS_PA_VIEWPORT_UNK00A84, fui(8192.0));
409 etna_set_state(stream, VIVS_PA_ZFARCLIPPING, 0x00000000);
410 etna_set_state(stream, VIVS_RA_HDEPTH_CONTROL, 0x00007000);
411 etna_set_state(stream, VIVS_PS_CONTROL_EXT, 0x00000000);
412
413 /* There is no HALTI0 specific state */
414 if (screen->specs.halti >= 1) { /* Only on HALTI1+ */
415 etna_set_state(stream, VIVS_VS_HALTI1_UNK00884, 0x00000808);
416 }
417 if (screen->specs.halti >= 2) { /* Only on HALTI2+ */
418 etna_set_state(stream, VIVS_RA_UNK00E0C, 0x00000000);
419 }
420 if (screen->specs.halti >= 3) { /* Only on HALTI3+ */
421 etna_set_state(stream, VIVS_PS_HALTI3_UNK0103C, 0x76543210);
422 }
423 if (screen->specs.halti >= 4) { /* Only on HALTI4+ */
424 etna_set_state(stream, VIVS_PS_MSAA_CONFIG, 0x6fffffff & 0xf70fffff & 0xfff6ffff &
425 0xffff6fff & 0xfffff6ff & 0xffffff7f);
426 etna_set_state(stream, VIVS_PE_HALTI4_UNK014C0, 0x00000000);
427 }
428 if (screen->specs.halti >= 5) { /* Only on HALTI5+ */
429 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_UNK14C40, 0x00000001);
430 etna_set_state(stream, VIVS_FE_HALTI5_UNK007D8, 0x00000002);
431 etna_set_state(stream, VIVS_PS_SAMPLER_BASE, 0x00000000);
432 etna_set_state(stream, VIVS_VS_SAMPLER_BASE, 0x00000020);
433 etna_set_state(stream, VIVS_SH_CONFIG, VIVS_SH_CONFIG_RTNE_ROUNDING);
434 } else { /* Only on pre-HALTI5 */
435 etna_set_state(stream, VIVS_GL_UNK03838, 0x00000000);
436 etna_set_state(stream, VIVS_GL_UNK03854, 0x00000000);
437 }
438
439 if (!screen->specs.use_blt) {
440 /* Enable SINGLE_BUFFER for resolve, if supported */
441 etna_set_state(stream, VIVS_RS_SINGLE_BUFFER, COND(screen->specs.single_buffer, VIVS_RS_SINGLE_BUFFER_ENABLE));
442 }
443
444 if (screen->specs.halti >= 5) {
445 /* TXDESC cache flush - do this once at the beginning, as texture
446 * descriptors are only written by the CPU once, then patched by the kernel
447 * before command stream submission. It does not need flushing if the
448 * referenced image data changes.
449 */
450 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_FLUSH, 0);
451 etna_set_state(stream, VIVS_GL_FLUSH_CACHE,
452 VIVS_GL_FLUSH_CACHE_DESCRIPTOR_UNK12 |
453 VIVS_GL_FLUSH_CACHE_DESCRIPTOR_UNK13);
454
455 /* Icache invalidate (should do this on shader change?) */
456 etna_set_state(stream, VIVS_VS_ICACHE_INVALIDATE,
457 VIVS_VS_ICACHE_INVALIDATE_UNK0 | VIVS_VS_ICACHE_INVALIDATE_UNK1 |
458 VIVS_VS_ICACHE_INVALIDATE_UNK2 | VIVS_VS_ICACHE_INVALIDATE_UNK3 |
459 VIVS_VS_ICACHE_INVALIDATE_UNK4);
460 }
461
462 ctx->dirty = ~0L;
463 ctx->dirty_sampler_views = ~0L;
464 }
465
466 static void
etna_flush(struct pipe_context * pctx,struct pipe_fence_handle ** fence,enum pipe_flush_flags flags)467 etna_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
468 enum pipe_flush_flags flags)
469 {
470 struct etna_context *ctx = etna_context(pctx);
471 int out_fence_fd = -1;
472
473 mtx_lock(&ctx->lock);
474
475 list_for_each_entry(struct etna_acc_query, aq, &ctx->active_acc_queries, node)
476 etna_acc_query_suspend(aq, ctx);
477
478 etna_cmd_stream_flush(ctx->stream, ctx->in_fence_fd,
479 (flags & PIPE_FLUSH_FENCE_FD) ? &out_fence_fd : NULL);
480
481 list_for_each_entry(struct etna_acc_query, aq, &ctx->active_acc_queries, node)
482 etna_acc_query_resume(aq, ctx);
483
484 if (fence)
485 *fence = etna_fence_create(pctx, out_fence_fd);
486
487 /*
488 * Go through all _resources_ pending in this _context_ and mark them as
489 * not pending in this _context_ anymore, since they were just flushed.
490 */
491 set_foreach(ctx->used_resources_read, entry) {
492 struct etna_resource *rsc = (struct etna_resource *)entry->key;
493 struct pipe_resource *referenced = &rsc->base;
494
495 mtx_lock(&rsc->lock);
496
497 _mesa_set_remove_key(rsc->pending_ctx, ctx);
498
499 /* if resource has no pending ctx's reset its status */
500 if (_mesa_set_next_entry(rsc->pending_ctx, NULL) == NULL)
501 rsc->status &= ~ETNA_PENDING_READ;
502
503 mtx_unlock(&rsc->lock);
504
505 pipe_resource_reference(&referenced, NULL);
506 }
507 _mesa_set_clear(ctx->used_resources_read, NULL);
508
509 set_foreach(ctx->used_resources_write, entry) {
510 struct etna_resource *rsc = (struct etna_resource *)entry->key;
511 struct pipe_resource *referenced = &rsc->base;
512
513 mtx_lock(&rsc->lock);
514 _mesa_set_remove_key(rsc->pending_ctx, ctx);
515
516 /* if resource has no pending ctx's reset its status */
517 if (_mesa_set_next_entry(rsc->pending_ctx, NULL) == NULL)
518 rsc->status &= ~ETNA_PENDING_WRITE;
519 mtx_unlock(&rsc->lock);
520
521 pipe_resource_reference(&referenced, NULL);
522 }
523 _mesa_set_clear(ctx->used_resources_write, NULL);
524
525 etna_reset_gpu_state(ctx);
526 mtx_unlock(&ctx->lock);
527 }
528
529 static void
etna_context_force_flush(struct etna_cmd_stream * stream,void * priv)530 etna_context_force_flush(struct etna_cmd_stream *stream, void *priv)
531 {
532 struct pipe_context *pctx = priv;
533
534 pctx->flush(pctx, NULL, 0);
535
536 }
537
538 static void
etna_set_debug_callback(struct pipe_context * pctx,const struct pipe_debug_callback * cb)539 etna_set_debug_callback(struct pipe_context *pctx,
540 const struct pipe_debug_callback *cb)
541 {
542 struct etna_context *ctx = etna_context(pctx);
543
544 if (cb)
545 ctx->debug = *cb;
546 else
547 memset(&ctx->debug, 0, sizeof(ctx->debug));
548 }
549
550 struct pipe_context *
etna_context_create(struct pipe_screen * pscreen,void * priv,unsigned flags)551 etna_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
552 {
553 struct etna_context *ctx = CALLOC_STRUCT(etna_context);
554 struct etna_screen *screen;
555 struct pipe_context *pctx;
556
557 if (ctx == NULL)
558 return NULL;
559
560 pctx = &ctx->base;
561 pctx->priv = ctx;
562 pctx->screen = pscreen;
563 pctx->stream_uploader = u_upload_create_default(pctx);
564 if (!pctx->stream_uploader)
565 goto fail;
566 pctx->const_uploader = pctx->stream_uploader;
567
568 screen = etna_screen(pscreen);
569 ctx->stream = etna_cmd_stream_new(screen->pipe, 0x2000,
570 &etna_context_force_flush, pctx);
571 if (ctx->stream == NULL)
572 goto fail;
573
574 ctx->used_resources_read = _mesa_set_create(NULL, _mesa_hash_pointer,
575 _mesa_key_pointer_equal);
576 if (!ctx->used_resources_read)
577 goto fail;
578
579 ctx->used_resources_write = _mesa_set_create(NULL, _mesa_hash_pointer,
580 _mesa_key_pointer_equal);
581 if (!ctx->used_resources_write)
582 goto fail;
583
584 mtx_init(&ctx->lock, mtx_recursive);
585
586 /* context ctxate setup */
587 ctx->screen = screen;
588 /* need some sane default in case gallium frontends don't set some state: */
589 ctx->sample_mask = 0xffff;
590
591 /* Set sensible defaults for state */
592 etna_reset_gpu_state(ctx);
593
594 ctx->in_fence_fd = -1;
595
596 pctx->destroy = etna_context_destroy;
597 pctx->draw_vbo = etna_draw_vbo;
598 pctx->flush = etna_flush;
599 pctx->set_debug_callback = etna_set_debug_callback;
600 pctx->create_fence_fd = etna_create_fence_fd;
601 pctx->fence_server_sync = etna_fence_server_sync;
602 pctx->emit_string_marker = etna_emit_string_marker;
603
604 /* creation of compile states */
605 pctx->create_blend_state = etna_blend_state_create;
606 pctx->create_rasterizer_state = etna_rasterizer_state_create;
607 pctx->create_depth_stencil_alpha_state = etna_zsa_state_create;
608
609 etna_clear_blit_init(pctx);
610 etna_query_context_init(pctx);
611 etna_state_init(pctx);
612 etna_surface_init(pctx);
613 etna_shader_init(pctx);
614 etna_texture_init(pctx);
615 etna_transfer_init(pctx);
616
617 ctx->blitter = util_blitter_create(pctx);
618 if (!ctx->blitter)
619 goto fail;
620
621 /* Generate the bitmask of supported draw primitives. */
622 ctx->prim_hwsupport = 1 << PIPE_PRIM_POINTS |
623 1 << PIPE_PRIM_LINES |
624 1 << PIPE_PRIM_LINE_STRIP |
625 1 << PIPE_PRIM_TRIANGLES |
626 1 << PIPE_PRIM_TRIANGLE_FAN;
627
628 /* TODO: The bug relates only to indexed draws, but here we signal
629 * that there is no support for triangle strips at all. This should
630 * be refined.
631 */
632 if (VIV_FEATURE(ctx->screen, chipMinorFeatures2, BUG_FIXES8))
633 ctx->prim_hwsupport |= 1 << PIPE_PRIM_TRIANGLE_STRIP;
634
635 if (VIV_FEATURE(ctx->screen, chipMinorFeatures2, LINE_LOOP))
636 ctx->prim_hwsupport |= 1 << PIPE_PRIM_LINE_LOOP;
637
638 ctx->primconvert = util_primconvert_create(pctx, ctx->prim_hwsupport);
639 if (!ctx->primconvert)
640 goto fail;
641
642 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
643 list_inithead(&ctx->active_acc_queries);
644
645 /* create dummy RT buffer, used when rendering with no color buffer */
646 ctx->dummy_rt = etna_bo_new(ctx->screen->dev, 64 * 64 * 4,
647 DRM_ETNA_GEM_CACHE_WC);
648 if (!ctx->dummy_rt)
649 goto fail;
650
651 ctx->dummy_rt_reloc.bo = ctx->dummy_rt;
652 ctx->dummy_rt_reloc.offset = 0;
653 ctx->dummy_rt_reloc.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
654
655 if (screen->specs.halti >= 5) {
656 /* Create an empty dummy texture descriptor */
657 ctx->dummy_desc_bo = etna_bo_new(ctx->screen->dev, 0x100, DRM_ETNA_GEM_CACHE_WC);
658 if (!ctx->dummy_desc_bo)
659 goto fail;
660 uint32_t *buf = etna_bo_map(ctx->dummy_desc_bo);
661 etna_bo_cpu_prep(ctx->dummy_desc_bo, DRM_ETNA_PREP_WRITE);
662 memset(buf, 0, 0x100);
663 etna_bo_cpu_fini(ctx->dummy_desc_bo);
664 ctx->DUMMY_DESC_ADDR.bo = ctx->dummy_desc_bo;
665 ctx->DUMMY_DESC_ADDR.offset = 0;
666 ctx->DUMMY_DESC_ADDR.flags = ETNA_RELOC_READ;
667 }
668
669 return pctx;
670
671 fail:
672 pctx->destroy(pctx);
673
674 return NULL;
675 }
676