summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/nvc0/nvc0_push2.c
blob: 3f9359cf25f13f3ffa7f3b35daf7d263096e0966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

#include "pipe/p_context.h"
#include "pipe/p_state.h"
#include "util/u_inlines.h"
#include "util/u_format.h"
#include "translate/translate.h"

#include "nvc0_context.h"
#include "nvc0_resource.h"

#include "nvc0_3d.xml.h"

struct push_context {
   struct nvc0_context *nvc0;

   uint vertex_size;

   void *idxbuf;
   uint idxsize;

   float edgeflag;
   int edgeflag_input;

   struct {
      void *map;
      void (*push)(struct nouveau_channel *, void *);
      uint32_t stride;
      uint32_t divisor;
      uint32_t step;
   } attr[32];
   int num_attrs;
};

static void
emit_b32_1(struct nouveau_channel *chan, void *data)
{
   uint32_t *v = data;

   OUT_RING(chan, v[0]);
}

static void
emit_b32_2(struct nouveau_channel *chan, void *data)
{
   uint32_t *v = data;

   OUT_RING(chan, v[0]);
   OUT_RING(chan, v[1]);
}

static void
emit_b32_3(struct nouveau_channel *chan, void *data)
{
   uint32_t *v = data;

   OUT_RING(chan, v[0]);
   OUT_RING(chan, v[1]);
   OUT_RING(chan, v[2]);
}

static void
emit_b32_4(struct nouveau_channel *chan, void *data)
{
   uint32_t *v = data;

   OUT_RING(chan, v[0]);
   OUT_RING(chan, v[1]);
   OUT_RING(chan, v[2]);
   OUT_RING(chan, v[3]);
}

static void
emit_b16_1(struct nouveau_channel *chan, void *data)
{
   uint16_t *v = data;

   OUT_RING(chan, v[0]);
}

static void
emit_b16_3(struct nouveau_channel *chan, void *data)
{
   uint16_t *v = data;

   OUT_RING(chan, (v[1] << 16) | v[0]);
   OUT_RING(chan, v[2]);
}

static void
emit_b08_1(struct nouveau_channel *chan, void *data)
{
   uint8_t *v = data;

   OUT_RING(chan, v[0]);
}

static void
emit_b08_3(struct nouveau_channel *chan, void *data)
{
   uint8_t *v = data;

   OUT_RING(chan, (v[2] << 16) | (v[1] << 8) | v[0]);
}

static void
emit_b64_1(struct nouveau_channel *chan, void *data)
{
   double *v = data;

   OUT_RINGf(chan, v[0]);
}

static void
emit_b64_2(struct nouveau_channel *chan, void *data)
{
   double *v = data;

   OUT_RINGf(chan, v[0]);
   OUT_RINGf(chan, v[1]);
}

static void
emit_b64_3(struct nouveau_channel *chan, void *data)
{
   double *v = data;

   OUT_RINGf(chan, v[0]);
   OUT_RINGf(chan, v[1]);
   OUT_RINGf(chan, v[2]);
}

static void
emit_b64_4(struct nouveau_channel *chan, void *data)
{
   double *v = data;

   OUT_RINGf(chan, v[0]);
   OUT_RINGf(chan, v[1]);
   OUT_RINGf(chan, v[2]);
   OUT_RINGf(chan, v[3]);   
}

static INLINE void
emit_vertex(struct push_context *ctx, unsigned n)
{
   struct nouveau_channel *chan = ctx->nvc0->screen->base.channel;
   int i;

   if (ctx->edgeflag_input < 32) {
      /* TODO */
   }

   BEGIN_RING_NI(chan, RING_3D(VERTEX_DATA), ctx->vertex_size);
   for (i = 0; i < ctx->num_attrs; ++i)
      ctx->attr[i].push(chan,
                        (uint8_t *)ctx->attr[i].map + n * ctx->attr[i].stride);
}

static void
emit_edgeflag(struct push_context *ctx, boolean enabled)
{
   struct nouveau_channel *chan = ctx->nvc0->screen->base.channel;
   
   INLIN_RING(chan, RING_3D(EDGEFLAG_ENABLE), enabled);
}

static void
emit_elt08(struct push_context *ctx, unsigned start, unsigned count)
{
   uint8_t *idxbuf = ctx->idxbuf;

   while (count--)
      emit_vertex(ctx, idxbuf[start++]);
}

static void
emit_elt16(struct push_context *ctx, unsigned start, unsigned count)
{
   uint16_t *idxbuf = ctx->idxbuf;

   while (count--)
      emit_vertex(ctx, idxbuf[start++]);
}

static void
emit_elt32(struct push_context *ctx, unsigned start, unsigned count)
{
   uint32_t *idxbuf = ctx->idxbuf;

   while (count--)
      emit_vertex(ctx, idxbuf[start++]);
}

static void
emit_seq(struct push_context *ctx, unsigned start, unsigned count)
{
   while (count--)
      emit_vertex(ctx, start++);
}

#define NVC0_PRIM_GL_CASE(n) \
   case PIPE_PRIM_##n: return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n

static INLINE unsigned
nvc0_prim_gl(unsigned prim)
{
   switch (prim) {
   NVC0_PRIM_GL_CASE(POINTS);
   NVC0_PRIM_GL_CASE(LINES);
   NVC0_PRIM_GL_CASE(LINE_LOOP);
   NVC0_PRIM_GL_CASE(LINE_STRIP);
   NVC0_PRIM_GL_CASE(TRIANGLES);
   NVC0_PRIM_GL_CASE(TRIANGLE_STRIP);
   NVC0_PRIM_GL_CASE(TRIANGLE_FAN);
   NVC0_PRIM_GL_CASE(QUADS);
   NVC0_PRIM_GL_CASE(QUAD_STRIP);
   NVC0_PRIM_GL_CASE(POLYGON);
   NVC0_PRIM_GL_CASE(LINES_ADJACENCY);
   NVC0_PRIM_GL_CASE(LINE_STRIP_ADJACENCY);
   NVC0_PRIM_GL_CASE(TRIANGLES_ADJACENCY);
   NVC0_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY);
   /*
   NVC0_PRIM_GL_CASE(PATCHES); */
   default:
      return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS;
      break;
   }
}

void
nvc0_push_vbo2(struct nvc0_context *nvc0, const struct pipe_draw_info *info)
{
   struct push_context ctx;
   unsigned i, n;
   unsigned inst = info->instance_count;
   unsigned prim = nvc0_prim_gl(info->mode);

   ctx.nvc0 = nvc0;
   ctx.vertex_size = nvc0->vertex->vtx_size;
   ctx.idxbuf = NULL;
   ctx.num_attrs = 0;
   ctx.edgeflag = 0.5f;
   ctx.edgeflag_input = 32;

   for (i = 0; i < nvc0->vertex->num_elements; ++i) {
      struct pipe_vertex_element *ve = &nvc0->vertex->element[i].pipe;
      struct pipe_vertex_buffer *vb = &nvc0->vtxbuf[ve->vertex_buffer_index];
      struct nouveau_bo *bo = nvc0_resource(vb->buffer)->bo;
      unsigned nr_components;

      if (!(nvc0->vbo_fifo & (1 << i)))
         continue;
      n = ctx.num_attrs++;

      if (nouveau_bo_map(bo, NOUVEAU_BO_RD))
         return;
      ctx.attr[n].map = (uint8_t *)bo->map + vb->buffer_offset + ve->src_offset;

      nouveau_bo_unmap(bo);

      ctx.attr[n].stride = vb->stride;
      ctx.attr[n].divisor = ve->instance_divisor;

      nr_components = util_format_get_nr_components(ve->src_format);
      switch (util_format_get_component_bits(ve->src_format,
                                             UTIL_FORMAT_COLORSPACE_RGB, 0)) {
      case 8:
         switch (nr_components) {
         case 1: ctx.attr[n].push = emit_b08_1; break;
         case 2: ctx.attr[n].push = emit_b16_1; break;
         case 3: ctx.attr[n].push = emit_b08_3; break;
         case 4: ctx.attr[n].push = emit_b32_1; break;
         }
         break;
      case 16:
         switch (nr_components) {
         case 1: ctx.attr[n].push = emit_b16_1; break;
         case 2: ctx.attr[n].push = emit_b32_1; break;
         case 3: ctx.attr[n].push = emit_b16_3; break;
         case 4: ctx.attr[n].push = emit_b32_2; break;
         }
         break;
      case 32:
         switch (nr_components) {
         case 1: ctx.attr[n].push = emit_b32_1; break;
         case 2: ctx.attr[n].push = emit_b32_2; break;
         case 3: ctx.attr[n].push = emit_b32_3; break;
         case 4: ctx.attr[n].push = emit_b32_4; break;
         }
         break;
      default:
         assert(0);
         break;
      }
   }

   if (info->indexed) {
      struct nvc0_resource *res = nvc0_resource(nvc0->idxbuf.buffer);
      if (!res || nouveau_bo_map(res->bo, NOUVEAU_BO_RD))
         return;
      ctx.idxbuf = res->bo->map;
      nouveau_bo_unmap(res->bo);
      ctx.idxsize = nvc0->idxbuf.index_size;
   } else {
      ctx.idxsize = 0;
   }

   while (inst--) {
      BEGIN_RING(nvc0->screen->base.channel, RING_3D(VERTEX_BEGIN_GL), 1);
      OUT_RING  (nvc0->screen->base.channel, prim);
      switch (ctx.idxsize) {
      case 0:
         emit_seq(&ctx, info->start, info->count);
         break;
      case 1:
         emit_elt08(&ctx, info->start, info->count);
         break;
      case 2:
         emit_elt16(&ctx, info->start, info->count);
         break;
      case 4:
         emit_elt32(&ctx, info->start, info->count);
         break;
      }
      INLIN_RING(nvc0->screen->base.channel, RING_3D(VERTEX_END_GL), 0);

      prim |= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
   }
}