nvc0: import nvc0 gallium driver
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_push.c
1
2 #include "pipe/p_context.h"
3 #include "pipe/p_state.h"
4 #include "util/u_inlines.h"
5 #include "util/u_format.h"
6 #include "translate/translate.h"
7
8 #include "nvc0_context.h"
9 #include "nvc0_resource.h"
10
11 #include "nvc0_3d.xml.h"
12
13 struct push_context {
14 struct nouveau_channel *chan;
15
16 void *idxbuf;
17 int32_t idxbias;
18
19 float edgeflag;
20 int edgeflat_attr;
21
22 uint32_t vertex_size;
23 uint32_t packet_vertex_limit;
24
25 struct translate *translate;
26 };
27
28 static void
29 emit_vertices_i08(struct push_context *ctx, unsigned start, unsigned count)
30 {
31 uint8_t *elts = (uint8_t *)ctx->idxbuf + start;
32
33 while (count) {
34 unsigned push = MIN2(count, ctx->packet_vertex_limit);
35 unsigned size = ctx->vertex_size * push;
36
37 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
38
39 ctx->translate->run_elts8(ctx->translate, elts, push, 0, ctx->chan->cur);
40 ctx->chan->cur += size;
41 count -= push;
42 elts += push;
43 }
44 }
45
46 static void
47 emit_vertices_i16(struct push_context *ctx, unsigned start, unsigned count)
48 {
49 uint16_t *elts = (uint16_t *)ctx->idxbuf + start;
50
51 while (count) {
52 unsigned push = MIN2(count, ctx->packet_vertex_limit);
53 unsigned size = ctx->vertex_size * push;
54
55 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
56
57 ctx->translate->run_elts16(ctx->translate, elts, push, 0, ctx->chan->cur);
58 ctx->chan->cur += size;
59 count -= push;
60 elts += push;
61 }
62 }
63
64 static void
65 emit_vertices_i32(struct push_context *ctx, unsigned start, unsigned count)
66 {
67 uint32_t *elts = (uint32_t *)ctx->idxbuf + start;
68
69 while (count) {
70 unsigned push = MIN2(count, ctx->packet_vertex_limit);
71 unsigned size = ctx->vertex_size * push;
72
73 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
74
75 ctx->translate->run_elts(ctx->translate, elts, push, 0, ctx->chan->cur);
76 ctx->chan->cur += size;
77 count -= push;
78 elts += push;
79 }
80 }
81
82 static void
83 emit_vertices_seq(struct push_context *ctx, unsigned start, unsigned count)
84 {
85 while (count) {
86 unsigned push = MIN2(count, ctx->packet_vertex_limit);
87 unsigned size = ctx->vertex_size * push;
88
89 BEGIN_RING_NI(ctx->chan, RING_3D(VERTEX_DATA), size);
90
91 ctx->translate->run(ctx->translate, start, push, 0, ctx->chan->cur);
92 ctx->chan->cur += size;
93 count -= push;
94 start += push;
95 }
96 }
97
98
99 #define NVC0_PRIM_GL_CASE(n) \
100 case PIPE_PRIM_##n: return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
101
102 static INLINE unsigned
103 nvc0_prim_gl(unsigned prim)
104 {
105 switch (prim) {
106 NVC0_PRIM_GL_CASE(POINTS);
107 NVC0_PRIM_GL_CASE(LINES);
108 NVC0_PRIM_GL_CASE(LINE_LOOP);
109 NVC0_PRIM_GL_CASE(LINE_STRIP);
110 NVC0_PRIM_GL_CASE(TRIANGLES);
111 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP);
112 NVC0_PRIM_GL_CASE(TRIANGLE_FAN);
113 NVC0_PRIM_GL_CASE(QUADS);
114 NVC0_PRIM_GL_CASE(QUAD_STRIP);
115 NVC0_PRIM_GL_CASE(POLYGON);
116 NVC0_PRIM_GL_CASE(LINES_ADJACENCY);
117 NVC0_PRIM_GL_CASE(LINE_STRIP_ADJACENCY);
118 NVC0_PRIM_GL_CASE(TRIANGLES_ADJACENCY);
119 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY);
120 /*
121 NVC0_PRIM_GL_CASE(PATCHES); */
122 default:
123 return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS;
124 break;
125 }
126 }
127
128 void
129 nvc0_push_vbo(struct nvc0_context *nvc0, const struct pipe_draw_info *info)
130 {
131 struct push_context ctx;
132 struct pipe_transfer *transfer = NULL;
133 unsigned i, index_size;
134 unsigned prim = nvc0_prim_gl(info->mode);
135 unsigned inst = info->instance_count;
136
137 ctx.chan = nvc0->screen->base.channel;
138 ctx.translate = nvc0->vertex->translate;
139 ctx.packet_vertex_limit = nvc0->vertex->vtx_per_packet_max;
140 ctx.vertex_size = nvc0->vertex->vtx_size;
141
142 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
143 uint8_t *data;
144 struct pipe_vertex_buffer *vb = &nvc0->vtxbuf[i];
145 struct nvc0_resource *res = nvc0_resource(vb->buffer);
146
147 if (nouveau_bo_map(res->bo, NOUVEAU_BO_RD))
148 return;
149 data = (uint8_t *)res->bo->map + vb->buffer_offset;
150 if (info->indexed)
151 data += info->index_bias * vb->stride;
152
153 ctx.translate->set_buffer(ctx.translate, i, data, vb->stride, ~0);
154 }
155
156 if (info->indexed) {
157 ctx.idxbuf = pipe_buffer_map(&nvc0->pipe, nvc0->idxbuf.buffer,
158 PIPE_TRANSFER_READ, &transfer);
159 if (!ctx.idxbuf)
160 return;
161 index_size = nvc0->idxbuf.index_size;
162 } else {
163 ctx.idxbuf = NULL;
164 index_size = 0;
165 }
166
167 while (inst--) {
168 BEGIN_RING(ctx.chan, RING_3D(VERTEX_BEGIN_GL), 1);
169 OUT_RING (ctx.chan, prim);
170 switch (index_size) {
171 case 0:
172 emit_vertices_seq(&ctx, info->start, info->count);
173 break;
174 case 1:
175 emit_vertices_i08(&ctx, info->start, info->count);
176 break;
177 case 2:
178 emit_vertices_i16(&ctx, info->start, info->count);
179 break;
180 case 4:
181 emit_vertices_i32(&ctx, info->start, info->count);
182 break;
183 default:
184 assert(0);
185 break;
186 }
187 INLIN_RING(ctx.chan, RING_3D(VERTEX_END_GL), 0);
188
189 prim |= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT;
190 }
191
192 if (info->indexed)
193 pipe_buffer_unmap(&nvc0->pipe, nvc0->idxbuf.buffer, transfer);
194
195 for (i = 0; i < nvc0->num_vtxbufs; ++i) {
196 struct nvc0_resource *res = nvc0_resource(nvc0->vtxbuf[i].buffer);
197
198 if (res->bo)
199 nouveau_bo_unmap(res->bo);
200 }
201 }