9cf963a9335cf49a1f07faf3dac6a75f5b427d09
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_swtnl_t.c
1 /*
2 * Copyright (C) 2009-2010 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "tnl/t_context.h"
28 #include "tnl/t_pipeline.h"
29 #include "tnl/t_vertex.h"
30
31 #define SWTNL_VBO_SIZE 65536
32
33 static enum tnl_attr_format
34 swtnl_get_format(int type, int fields) {
35 switch (type) {
36 case GL_FLOAT:
37 switch (fields){
38 case 1:
39 return EMIT_1F;
40 case 2:
41 return EMIT_2F;
42 case 3:
43 return EMIT_3F;
44 case 4:
45 return EMIT_4F;
46 default:
47 assert(0);
48 }
49 case GL_UNSIGNED_BYTE:
50 switch (fields) {
51 case 4:
52 return EMIT_4UB_4F_RGBA;
53 default:
54 assert(0);
55 }
56 default:
57 assert(0);
58 }
59 }
60
61 static struct swtnl_attr_info {
62 int type;
63 int fields;
64 } swtnl_attrs[VERT_ATTRIB_MAX] = {
65 [VERT_ATTRIB_POS] = {
66 .type = GL_FLOAT,
67 .fields = 4,
68 },
69 [VERT_ATTRIB_NORMAL] = {
70 .type = GL_FLOAT,
71 .fields = -1,
72 },
73 [VERT_ATTRIB_COLOR0] = {
74 .type = GL_UNSIGNED_BYTE,
75 .fields = 4,
76 },
77 [VERT_ATTRIB_COLOR1] = {
78 .type = GL_UNSIGNED_BYTE,
79 .fields = 4,
80 },
81 [VERT_ATTRIB_FOG] = {
82 .type = GL_FLOAT,
83 .fields = 1,
84 },
85 [VERT_ATTRIB_TEX0] = {
86 .type = GL_FLOAT,
87 .fields = -1,
88 },
89 [VERT_ATTRIB_TEX1] = {
90 .type = GL_FLOAT,
91 .fields = -1,
92 },
93 [VERT_ATTRIB_TEX2] = {
94 .type = GL_FLOAT,
95 .fields = -1,
96 },
97 [VERT_ATTRIB_TEX3] = {
98 .type = GL_FLOAT,
99 .fields = -1,
100 },
101 };
102
103 static void
104 swtnl_choose_attrs(struct gl_context *ctx)
105 {
106 struct nouveau_render_state *render = to_render_state(ctx);
107 TNLcontext *tnl = TNL_CONTEXT(ctx);
108 struct tnl_clipspace *vtx = &tnl->clipspace;
109 static struct tnl_attr_map map[NUM_VERTEX_ATTRS];
110 int fields, attr, i, n = 0;
111
112 render->mode = VBO;
113 render->attr_count = NUM_VERTEX_ATTRS;
114
115 /* We always want non Ndc coords format */
116 tnl->vb.AttribPtr[VERT_ATTRIB_POS] = tnl->vb.ClipPtr;
117
118 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
119 struct nouveau_attr_info *ha = &TAG(vertex_attrs)[i];
120 struct swtnl_attr_info *sa = &swtnl_attrs[i];
121 struct nouveau_array_state *a = &render->attrs[i];
122
123 if (!sa->fields)
124 continue; /* Unsupported attribute. */
125
126 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, i)) {
127 if (sa->fields > 0)
128 fields = sa->fields;
129 else
130 fields = tnl->vb.AttribPtr[i]->size;
131
132 map[n++] = (struct tnl_attr_map) {
133 .attrib = i,
134 .format = swtnl_get_format(sa->type, fields),
135 };
136
137 render->map[ha->vbo_index] = i;
138 a->attr = i;
139 a->fields = fields;
140 a->type = sa->type;
141 }
142 }
143
144 _tnl_install_attrs(ctx, map, n, NULL, 0);
145
146 FOR_EACH_BOUND_ATTR(render, i, attr)
147 render->attrs[attr].stride = vtx->vertex_size;
148
149 TAG(render_set_format)(ctx);
150 }
151
152 static void
153 swtnl_alloc_vertices(struct gl_context *ctx)
154 {
155 struct nouveau_swtnl_state *swtnl = &to_render_state(ctx)->swtnl;
156
157 nouveau_bo_ref(NULL, &swtnl->vbo);
158 swtnl->buf = nouveau_get_scratch(ctx, SWTNL_VBO_SIZE, &swtnl->vbo,
159 &swtnl->offset);
160 swtnl->vertex_count = 0;
161 }
162
163 static void
164 swtnl_bind_vertices(struct gl_context *ctx)
165 {
166 struct nouveau_render_state *render = to_render_state(ctx);
167 struct nouveau_swtnl_state *swtnl = &render->swtnl;
168 int i;
169
170 for (i = 0; i < render->attr_count; i++) {
171 int attr = render->map[i];
172
173 if (attr >= 0)
174 nouveau_bo_ref(swtnl->vbo,
175 &render->attrs[attr].bo);
176 }
177
178 TAG(render_bind_vertices)(ctx);
179 }
180
181 static void
182 swtnl_unbind_vertices(struct gl_context *ctx)
183 {
184 struct nouveau_render_state *render = to_render_state(ctx);
185 int i, attr;
186
187 FOR_EACH_BOUND_ATTR(render, i, attr) {
188 nouveau_bo_ref(NULL, &render->attrs[attr].bo);
189 render->map[i] = -1;
190 }
191
192 render->attr_count = 0;
193 }
194
195 static void
196 swtnl_flush_vertices(struct gl_context *ctx)
197 {
198 struct nouveau_channel *chan = context_chan(ctx);
199 struct nouveau_swtnl_state *swtnl = &to_render_state(ctx)->swtnl;
200 unsigned push, start = 0, count = swtnl->vertex_count;
201 RENDER_LOCALS(ctx);
202
203 swtnl_bind_vertices(ctx);
204
205 while (count) {
206 push = get_max_vertices(ctx, NULL, AVAIL_RING(chan));
207 push = MIN2(push / 12 * 12, count);
208 count -= push;
209
210 if (!push) {
211 FIRE_RING(chan);
212 continue;
213 }
214
215 BATCH_BEGIN(nvgl_primitive(swtnl->primitive));
216 EMIT_VBO(L, ctx, start, 0, push);
217 BATCH_END();
218
219 FIRE_RING(chan);
220 }
221
222 swtnl_alloc_vertices(ctx);
223 }
224
225 /* TnL renderer entry points */
226
227 static void
228 swtnl_start(struct gl_context *ctx)
229 {
230 swtnl_choose_attrs(ctx);
231 }
232
233 static void
234 swtnl_finish(struct gl_context *ctx)
235 {
236 swtnl_flush_vertices(ctx);
237 swtnl_unbind_vertices(ctx);
238 }
239
240 static void
241 swtnl_primitive(struct gl_context *ctx, GLenum mode)
242 {
243 }
244
245 static void
246 swtnl_reset_stipple(struct gl_context *ctx)
247 {
248 }
249
250 /* Primitive rendering */
251
252 #define BEGIN_PRIMITIVE(p, n) \
253 struct nouveau_swtnl_state *swtnl = &to_render_state(ctx)->swtnl; \
254 int vertex_len = TNL_CONTEXT(ctx)->clipspace.vertex_size; \
255 \
256 if (swtnl->vertex_count + (n) > SWTNL_VBO_SIZE/vertex_len \
257 || (swtnl->vertex_count && swtnl->primitive != p)) \
258 swtnl_flush_vertices(ctx); \
259 \
260 swtnl->primitive = p;
261
262 #define OUT_VERTEX(i) do { \
263 memcpy(swtnl->buf + swtnl->vertex_count * vertex_len, \
264 _tnl_get_vertex(ctx, (i)), vertex_len); \
265 swtnl->vertex_count++; \
266 } while (0)
267
268 static void
269 swtnl_points(struct gl_context *ctx, GLuint first, GLuint last)
270 {
271 int i, count;
272
273 while (first < last) {
274 BEGIN_PRIMITIVE(GL_POINTS, last - first);
275
276 count = MIN2(SWTNL_VBO_SIZE / vertex_len, last - first);
277 for (i = 0; i < count; i++)
278 OUT_VERTEX(first + i);
279
280 first += count;
281 }
282 }
283
284 static void
285 swtnl_line(struct gl_context *ctx, GLuint v1, GLuint v2)
286 {
287 BEGIN_PRIMITIVE(GL_LINES, 2);
288 OUT_VERTEX(v1);
289 OUT_VERTEX(v2);
290 }
291
292 static void
293 swtnl_triangle(struct gl_context *ctx, GLuint v1, GLuint v2, GLuint v3)
294 {
295 BEGIN_PRIMITIVE(GL_TRIANGLES, 3);
296 OUT_VERTEX(v1);
297 OUT_VERTEX(v2);
298 OUT_VERTEX(v3);
299 }
300
301 static void
302 swtnl_quad(struct gl_context *ctx, GLuint v1, GLuint v2, GLuint v3, GLuint v4)
303 {
304 BEGIN_PRIMITIVE(GL_QUADS, 4);
305 OUT_VERTEX(v1);
306 OUT_VERTEX(v2);
307 OUT_VERTEX(v3);
308 OUT_VERTEX(v4);
309 }
310
311 /* TnL initialization. */
312 static void
313 TAG(swtnl_init)(struct gl_context *ctx)
314 {
315 TNLcontext *tnl = TNL_CONTEXT(ctx);
316
317 tnl->Driver.RunPipeline = _tnl_run_pipeline;
318 tnl->Driver.Render.Interp = _tnl_interp;
319 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
320 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
321 tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine;
322 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
323
324 tnl->Driver.Render.Start = swtnl_start;
325 tnl->Driver.Render.Finish = swtnl_finish;
326 tnl->Driver.Render.PrimitiveNotify = swtnl_primitive;
327 tnl->Driver.Render.ResetLineStipple = swtnl_reset_stipple;
328
329 tnl->Driver.Render.Points = swtnl_points;
330 tnl->Driver.Render.Line = swtnl_line;
331 tnl->Driver.Render.Triangle = swtnl_triangle;
332 tnl->Driver.Render.Quad = swtnl_quad;
333
334 _tnl_init_vertices(ctx, tnl->vb.Size,
335 NUM_VERTEX_ATTRS * 4 * sizeof(GLfloat));
336 _tnl_need_projected_coords(ctx, GL_FALSE);
337 _tnl_allow_vertex_fog(ctx, GL_FALSE);
338 _tnl_wakeup(ctx);
339
340 swtnl_alloc_vertices(ctx);
341 }
342
343 static void
344 TAG(swtnl_destroy)(struct gl_context *ctx)
345 {
346 nouveau_bo_ref(NULL, &to_render_state(ctx)->swtnl.vbo);
347 }