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