nv50_push.c 9.78 KB
Newer Older
1

2 3 4
#include "pipe/p_context.h"
#include "pipe/p_state.h"
#include "util/u_inlines.h"
5
#include "util/format/u_format.h"
6
#include "translate/translate.h"
7

8 9
#include "nv50/nv50_context.h"
#include "nv50/nv50_resource.h"
10

11
#include "nv50/nv50_3d.xml.h"
12

13
struct push_context {
14
   struct nouveau_pushbuf *push;
15

16
   const void *idxbuf;
17 18 19 20

   float edgeflag;
   int edgeflag_attr;

21 22 23 24 25
   uint32_t vertex_words;
   uint32_t packet_vertex_limit;

   struct translate *translate;

26
   bool primitive_restart;
27 28 29 30

   bool need_vertex_id;
   int32_t index_bias;

31 32
   uint32_t prim;
   uint32_t restart_index;
33
   uint32_t start_instance;
34
   uint32_t instance_id;
35 36
};

37
static inline unsigned
38
prim_restart_search_i08(uint8_t *elts, unsigned push, uint8_t index)
39
{
40 41 42 43 44
   unsigned i;
   for (i = 0; i < push; ++i)
      if (elts[i] == index)
         break;
   return i;
45 46
}

47
static inline unsigned
48
prim_restart_search_i16(uint16_t *elts, unsigned push, uint16_t index)
49
{
50 51 52 53 54
   unsigned i;
   for (i = 0; i < push; ++i)
      if (elts[i] == index)
         break;
   return i;
55 56
}

57
static inline unsigned
58
prim_restart_search_i32(uint32_t *elts, unsigned push, uint32_t index)
59
{
60 61 62 63 64
   unsigned i;
   for (i = 0; i < push; ++i)
      if (elts[i] == index)
         break;
   return i;
65 66 67
}

static void
68
emit_vertices_i08(struct push_context *ctx, unsigned start, unsigned count)
69
{
70
   uint8_t *elts = (uint8_t *)ctx->idxbuf + start;
71

72 73 74
   while (count) {
      unsigned push = MIN2(count, ctx->packet_vertex_limit);
      unsigned size, nr;
75

76 77 78
      nr = push;
      if (ctx->primitive_restart)
         nr = prim_restart_search_i08(elts, push, ctx->restart_index);
79

80
      size = ctx->vertex_words * nr;
81

82 83 84 85 86
      if (unlikely(ctx->need_vertex_id)) {
         BEGIN_NV04(ctx->push, NV84_3D(VERTEX_ID_BASE), 1);
         PUSH_DATA (ctx->push, *elts + ctx->index_bias);
      }

87
      BEGIN_NI04(ctx->push, NV50_3D(VERTEX_DATA), size);
88

89 90
      ctx->translate->run_elts8(ctx->translate, elts, nr,
                                ctx->start_instance, ctx->instance_id,
91
                                ctx->push->cur);
92

93
      ctx->push->cur += size;
94 95
      count -= nr;
      elts += nr;
96

97 98 99
      if (nr != push) {
         count--;
         elts++;
100 101
         BEGIN_NV04(ctx->push, NV50_3D(VB_ELEMENT_U32), 1);
         PUSH_DATA (ctx->push, ctx->restart_index);
102 103
      }
   }
104 105 106
}

static void
107
emit_vertices_i16(struct push_context *ctx, unsigned start, unsigned count)
108
{
109
   uint16_t *elts = (uint16_t *)ctx->idxbuf + start;
110

111 112 113
   while (count) {
      unsigned push = MIN2(count, ctx->packet_vertex_limit);
      unsigned size, nr;
114

115 116 117
      nr = push;
      if (ctx->primitive_restart)
         nr = prim_restart_search_i16(elts, push, ctx->restart_index);
118

119
      size = ctx->vertex_words * nr;
120

121 122 123 124 125
      if (unlikely(ctx->need_vertex_id)) {
         BEGIN_NV04(ctx->push, NV84_3D(VERTEX_ID_BASE), 1);
         PUSH_DATA (ctx->push, *elts + ctx->index_bias);
      }

126
      BEGIN_NI04(ctx->push, NV50_3D(VERTEX_DATA), size);
127

128 129
      ctx->translate->run_elts16(ctx->translate, elts, nr,
                                 ctx->start_instance, ctx->instance_id,
130
                                 ctx->push->cur);
131

132
      ctx->push->cur += size;
133 134
      count -= nr;
      elts += nr;
135

136 137 138
      if (nr != push) {
         count--;
         elts++;
139 140
         BEGIN_NV04(ctx->push, NV50_3D(VB_ELEMENT_U32), 1);
         PUSH_DATA (ctx->push, ctx->restart_index);
141 142
      }
   }
143 144
}

145
static void
146
emit_vertices_i32(struct push_context *ctx, unsigned start, unsigned count)
147
{
148
   uint32_t *elts = (uint32_t *)ctx->idxbuf + start;
149

150 151 152
   while (count) {
      unsigned push = MIN2(count, ctx->packet_vertex_limit);
      unsigned size, nr;
153

154 155 156
      nr = push;
      if (ctx->primitive_restart)
         nr = prim_restart_search_i32(elts, push, ctx->restart_index);
157

158
      size = ctx->vertex_words * nr;
159

160 161 162 163 164
      if (unlikely(ctx->need_vertex_id)) {
         BEGIN_NV04(ctx->push, NV84_3D(VERTEX_ID_BASE), 1);
         PUSH_DATA (ctx->push, *elts + ctx->index_bias);
      }

165
      BEGIN_NI04(ctx->push, NV50_3D(VERTEX_DATA), size);
166

167 168
      ctx->translate->run_elts(ctx->translate, elts, nr,
                               ctx->start_instance, ctx->instance_id,
169
                               ctx->push->cur);
170

171
      ctx->push->cur += size;
172 173
      count -= nr;
      elts += nr;
174

175 176 177
      if (nr != push) {
         count--;
         elts++;
178 179
         BEGIN_NV04(ctx->push, NV50_3D(VB_ELEMENT_U32), 1);
         PUSH_DATA (ctx->push, ctx->restart_index);
180 181
      }
   }
182 183
}

184
static void
185
emit_vertices_seq(struct push_context *ctx, unsigned start, unsigned count)
186
{
187 188
   uint32_t elts = 0;

189 190 191 192
   while (count) {
      unsigned push = MIN2(count, ctx->packet_vertex_limit);
      unsigned size = ctx->vertex_words * push;

193 194 195 196 197 198
      if (unlikely(ctx->need_vertex_id)) {
         /* For non-indexed draws, gl_VertexID goes up after each vertex. */
         BEGIN_NV04(ctx->push, NV84_3D(VERTEX_ID_BASE), 1);
         PUSH_DATA (ctx->push, elts++);
      }

199
      BEGIN_NI04(ctx->push, NV50_3D(VERTEX_DATA), size);
200

201 202
      ctx->translate->run(ctx->translate, start, push,
                          ctx->start_instance, ctx->instance_id,
203 204
                          ctx->push->cur);
      ctx->push->cur += size;
205 206 207
      count -= push;
      start += push;
   }
208 209
}

210 211 212 213

#define NV50_PRIM_GL_CASE(n) \
   case PIPE_PRIM_##n: return NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n

214
static inline unsigned
215
nv50_prim_gl(unsigned prim)
216
{
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
   switch (prim) {
   NV50_PRIM_GL_CASE(POINTS);
   NV50_PRIM_GL_CASE(LINES);
   NV50_PRIM_GL_CASE(LINE_LOOP);
   NV50_PRIM_GL_CASE(LINE_STRIP);
   NV50_PRIM_GL_CASE(TRIANGLES);
   NV50_PRIM_GL_CASE(TRIANGLE_STRIP);
   NV50_PRIM_GL_CASE(TRIANGLE_FAN);
   NV50_PRIM_GL_CASE(QUADS);
   NV50_PRIM_GL_CASE(QUAD_STRIP);
   NV50_PRIM_GL_CASE(POLYGON);
   NV50_PRIM_GL_CASE(LINES_ADJACENCY);
   NV50_PRIM_GL_CASE(LINE_STRIP_ADJACENCY);
   NV50_PRIM_GL_CASE(TRIANGLES_ADJACENCY);
   NV50_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY);
   /*
   NV50_PRIM_GL_CASE(PATCHES); */
   default:
      return NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS;
      break;
   }
238 239 240
}

void
241
nv50_push_vbo(struct nv50_context *nv50, const struct pipe_draw_info *info)
242 243
{
   struct push_context ctx;
244
   unsigned i, index_size;
245 246
   unsigned inst_count = info->instance_count;
   unsigned vert_count = info->count;
247
   bool apply_bias = info->index_size && info->index_bias;
248

249
   ctx.push = nv50->base.pushbuf;
250
   ctx.translate = nv50->vertex->translate;
251 252 253 254

   ctx.need_vertex_id = nv50->screen->base.class_3d >= NV84_3D_CLASS &&
      nv50->vertprog->vp.need_vertex_id && (nv50->vertex->num_elements < 32);
   ctx.index_bias = info->index_bias;
255
   ctx.instance_id = 0;
256 257 258 259

   /* For indexed draws, gl_VertexID must be emitted for every vertex. */
   ctx.packet_vertex_limit =
      ctx.need_vertex_id ? 1 : nv50->vertex->packet_vertex_limit;
260 261
   ctx.vertex_words = nv50->vertex->vertex_size;

262
   assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
263
   for (i = 0; i < nv50->num_vtxbufs; ++i) {
264 265
      const struct pipe_vertex_buffer *vb = &nv50->vtxbuf[i];
      const uint8_t *data;
266

267 268 269 270
      if (unlikely(!vb->is_user_buffer)) {
         if (!vb->buffer.resource)
            continue;

271
         data = nouveau_resource_map_offset(&nv50->base,
272
            nv04_resource(vb->buffer.resource), vb->buffer_offset, NOUVEAU_BO_RD);
273
      } else
274
         data = vb->buffer.user;
275 276

      if (apply_bias && likely(!(nv50->vertex->instance_bufs & (1 << i))))
277
         data += (ptrdiff_t)info->index_bias * vb->stride;
278 279 280 281

      ctx.translate->set_buffer(ctx.translate, i, data, vb->stride, ~0);
   }

282 283
   if (info->index_size) {
      if (!info->has_user_indices) {
284
         ctx.idxbuf = nouveau_resource_map_offset(&nv50->base,
285
            nv04_resource(info->index.resource), 0, NOUVEAU_BO_RD);
286
      } else {
287
         ctx.idxbuf = info->index.user;
288
      }
289
      if (!ctx.idxbuf)
290
         return;
291
      index_size = info->index_size;
292 293 294
      ctx.primitive_restart = info->primitive_restart;
      ctx.restart_index = info->restart_index;
   } else {
295
      if (unlikely(info->indirect && info->indirect->count_from_stream_output)) {
296 297
         struct pipe_context *pipe = &nv50->base.pipe;
         struct nv50_so_target *targ;
298
         targ = nv50_so_target(info->indirect->count_from_stream_output);
299 300 301 302
         if (!targ->pq) {
            NOUVEAU_ERR("draw_stream_output not supported on pre-NVA0 cards\n");
            return;
         }
303
         pipe->get_query_result(pipe, targ->pq, true, (void *)&vert_count);
304 305
         vert_count /= targ->stride;
      }
306 307
      ctx.idxbuf = NULL;
      index_size = 0;
308
      ctx.primitive_restart = false;
309 310 311
      ctx.restart_index = 0;
   }

312
   ctx.start_instance = info->start_instance;
313
   ctx.prim = nv50_prim_gl(info->mode);
314

315
   if (info->primitive_restart) {
316 317 318
      BEGIN_NV04(ctx.push, NV50_3D(PRIM_RESTART_ENABLE), 2);
      PUSH_DATA (ctx.push, 1);
      PUSH_DATA (ctx.push, info->restart_index);
319 320
   } else
   if (nv50->state.prim_restart) {
321 322
      BEGIN_NV04(ctx.push, NV50_3D(PRIM_RESTART_ENABLE), 1);
      PUSH_DATA (ctx.push, 0);
323 324 325
   }
   nv50->state.prim_restart = info->primitive_restart;

326
   while (inst_count--) {
327 328
      BEGIN_NV04(ctx.push, NV50_3D(VERTEX_BEGIN_GL), 1);
      PUSH_DATA (ctx.push, ctx.prim);
329 330
      switch (index_size) {
      case 0:
331
         emit_vertices_seq(&ctx, info->start, vert_count);
332
         break;
333
      case 1:
334
         emit_vertices_i08(&ctx, info->start, vert_count);
335
         break;
336
      case 2:
337
         emit_vertices_i16(&ctx, info->start, vert_count);
338 339
         break;
      case 4:
340
         emit_vertices_i32(&ctx, info->start, vert_count);
341 342 343
         break;
      default:
         assert(0);
344
         break;
345
      }
346 347
      BEGIN_NV04(ctx.push, NV50_3D(VERTEX_END_GL), 1);
      PUSH_DATA (ctx.push, 0);
348

349 350
      ctx.instance_id++;
      ctx.prim |= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
351
   }
352 353 354 355 356 357

   if (unlikely(ctx.need_vertex_id)) {
      /* Reset gl_VertexID to prevent future indexed draws to be confused. */
      BEGIN_NV04(ctx.push, NV84_3D(VERTEX_ID_BASE), 1);
      PUSH_DATA (ctx.push, nv50->state.index_bias);
   }
358
}