~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/gallium/drivers/etnaviv/etnaviv_context.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

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