Remove old t_vertex.c codegen infrastructure, tie in new code.
[mesa.git] / src / mesa / tnl / t_vertex.c
1 /*
2 * Copyright 2003 Tungsten Graphics, inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@tungstengraphics.com>
26 */
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "colormac.h"
31
32 #include "t_context.h"
33 #include "t_vertex.h"
34
35 #define DBG 0
36
37 /* Build and manage clipspace/ndc/window vertices.
38 */
39
40 static GLboolean match_fastpath( struct tnl_clipspace *vtx,
41 const struct tnl_clipspace_fastpath *fp)
42 {
43 GLuint j;
44
45 if (vtx->attr_count != fp->attr_count)
46 return GL_FALSE;
47
48 for (j = 0; j < vtx->attr_count; j++)
49 if (vtx->attr[j].format != fp->attr[j].format)
50 return GL_FALSE;
51
52 if (fp->match_strides) {
53 if (vtx->vertex_size != fp->vertex_size)
54 return GL_FALSE;
55
56 for (j = 0; j < vtx->attr_count; j++)
57 if (vtx->attr[j].inputstride != fp->attr[j].stride)
58 return GL_FALSE;
59 }
60
61 return GL_TRUE;
62 }
63
64 static GLboolean search_fastpath_emit( struct tnl_clipspace *vtx )
65 {
66 struct tnl_clipspace_fastpath *fp = vtx->fastpath;
67
68 for ( ; fp ; fp = fp->next) {
69 if (match_fastpath(vtx, fp)) {
70 vtx->emit = fp->func;
71 return GL_TRUE;
72 }
73 }
74
75 return GL_FALSE;
76 }
77
78 void _tnl_register_fastpath( struct tnl_clipspace *vtx,
79 GLboolean match_strides )
80 {
81 struct tnl_clipspace_fastpath *fastpath = CALLOC_STRUCT(tnl_clipspace_fastpath);
82 GLuint i;
83
84 fastpath->vertex_size = vtx->vertex_size;
85 fastpath->attr_count = vtx->attr_count;
86 fastpath->match_strides = match_strides;
87 fastpath->func = vtx->emit;
88 fastpath->attr = MALLOC(vtx->attr_count * sizeof(fastpath->attr[0]));
89
90 for (i = 0; i < vtx->attr_count; i++) {
91 fastpath->attr[i].format = vtx->attr[i].format;
92 fastpath->attr[i].stride = vtx->attr[i].inputstride;
93 }
94
95 fastpath->next = vtx->fastpath;
96 vtx->fastpath = fastpath;
97 }
98
99
100
101 /***********************************************************************
102 * Build codegen functions or return generic ones:
103 */
104 static void choose_emit_func( GLcontext *ctx, GLuint count, GLubyte *dest)
105 {
106 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
107 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
108 struct tnl_clipspace_attr *a = vtx->attr;
109 const GLuint attr_count = vtx->attr_count;
110 GLuint j;
111
112 for (j = 0; j < attr_count; j++) {
113 GLvector4f *vptr = VB->AttribPtr[a[j].attrib];
114 a[j].inputstride = vptr->stride;
115 a[j].inputsize = vptr->size;
116 a[j].emit = a[j].insert[vptr->size - 1]; /* not always used */
117 }
118
119 vtx->emit = NULL;
120
121 /* Does this match an existing (hardwired, codegen or known-bad)
122 * fastpath?
123 */
124 if (search_fastpath_emit(vtx)) {
125 /* Use this result. If it is null, then it is already known
126 * that the current state will fail for codegen and there is no
127 * point trying again.
128 */
129 }
130 else if (vtx->codegen_emit) {
131 vtx->codegen_emit(ctx);
132 }
133
134 if (!vtx->emit) {
135 _tnl_generate_hardwired_emit(ctx);
136 }
137
138 /* Otherwise use the generic version:
139 */
140 if (!vtx->emit)
141 vtx->emit = _tnl_generic_emit;
142
143 vtx->emit( ctx, count, dest );
144 }
145
146
147
148 static void choose_interp_func( GLcontext *ctx,
149 GLfloat t,
150 GLuint edst, GLuint eout, GLuint ein,
151 GLboolean force_boundary )
152 {
153 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
154
155 if (vtx->need_extras &&
156 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
157 vtx->interp = _tnl_generic_interp_extras;
158 } else {
159 vtx->interp = _tnl_generic_interp;
160 }
161
162 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
163 }
164
165
166 static void choose_copy_pv_func( GLcontext *ctx, GLuint edst, GLuint esrc )
167 {
168 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
169
170 if (vtx->need_extras &&
171 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
172 vtx->copy_pv = _tnl_generic_copy_pv_extras;
173 } else {
174 vtx->copy_pv = _tnl_generic_copy_pv;
175 }
176
177 vtx->copy_pv( ctx, edst, esrc );
178 }
179
180
181 /***********************************************************************
182 * Public entrypoints, mostly dispatch to the above:
183 */
184
185
186 /* Interpolate between two vertices to produce a third:
187 */
188 void _tnl_interp( GLcontext *ctx,
189 GLfloat t,
190 GLuint edst, GLuint eout, GLuint ein,
191 GLboolean force_boundary )
192 {
193 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
194 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
195 }
196
197 /* Copy colors from one vertex to another:
198 */
199 void _tnl_copy_pv( GLcontext *ctx, GLuint edst, GLuint esrc )
200 {
201 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
202 vtx->copy_pv( ctx, edst, esrc );
203 }
204
205
206 /* Extract a named attribute from a hardware vertex. Will have to
207 * reverse any viewport transformation, swizzling or other conversions
208 * which may have been applied:
209 */
210 void _tnl_get_attr( GLcontext *ctx, const void *vin,
211 GLenum attr, GLfloat *dest )
212 {
213 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
214 const struct tnl_clipspace_attr *a = vtx->attr;
215 const GLuint attr_count = vtx->attr_count;
216 GLuint j;
217
218 for (j = 0; j < attr_count; j++) {
219 if (a[j].attrib == attr) {
220 a[j].extract( &a[j], dest, (GLubyte *)vin + a[j].vertoffset );
221 return;
222 }
223 }
224
225 /* Else return the value from ctx->Current.
226 */
227 _mesa_memcpy( dest, ctx->Current.Attrib[attr], 4*sizeof(GLfloat));
228 }
229
230
231 /* Complementary operation to the above.
232 */
233 void _tnl_set_attr( GLcontext *ctx, void *vout,
234 GLenum attr, const GLfloat *src )
235 {
236 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
237 const struct tnl_clipspace_attr *a = vtx->attr;
238 const GLuint attr_count = vtx->attr_count;
239 GLuint j;
240
241 for (j = 0; j < attr_count; j++) {
242 if (a[j].attrib == attr) {
243 a[j].insert[4-1]( &a[j], (GLubyte *)vout + a[j].vertoffset, src );
244 return;
245 }
246 }
247 }
248
249
250 void *_tnl_get_vertex( GLcontext *ctx, GLuint nr )
251 {
252 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
253
254 return vtx->vertex_buf + nr * vtx->vertex_size;
255 }
256
257 void _tnl_invalidate_vertex_state( GLcontext *ctx, GLuint new_state )
258 {
259 if (new_state & (_DD_NEW_TRI_LIGHT_TWOSIDE|_DD_NEW_TRI_UNFILLED) ) {
260 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
261 vtx->new_inputs = ~0;
262 vtx->interp = choose_interp_func;
263 vtx->copy_pv = choose_copy_pv_func;
264 }
265 }
266
267 static void invalidate_funcs( struct tnl_clipspace *vtx )
268 {
269 vtx->emit = choose_emit_func;
270 vtx->interp = choose_interp_func;
271 vtx->copy_pv = choose_copy_pv_func;
272 vtx->new_inputs = ~0;
273 }
274
275 GLuint _tnl_install_attrs( GLcontext *ctx, const struct tnl_attr_map *map,
276 GLuint nr, const GLfloat *vp,
277 GLuint unpacked_size )
278 {
279 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
280 GLuint offset = 0;
281 GLuint i, j;
282
283 assert(nr < _TNL_ATTRIB_MAX);
284 assert(nr == 0 || map[0].attrib == VERT_ATTRIB_POS);
285
286 vtx->new_inputs = ~0;
287 vtx->need_viewport = GL_FALSE;
288
289 if (vp) {
290 vtx->need_viewport = GL_TRUE;
291 vtx->vp_scale[0] = vp[MAT_SX];
292 vtx->vp_scale[1] = vp[MAT_SY];
293 vtx->vp_scale[2] = vp[MAT_SZ];
294 vtx->vp_scale[3] = 1.0;
295 vtx->vp_xlate[0] = vp[MAT_TX];
296 vtx->vp_xlate[1] = vp[MAT_TY];
297 vtx->vp_xlate[2] = vp[MAT_TZ];
298 vtx->vp_xlate[3] = 0.0;
299 }
300
301 for (j = 0, i = 0; i < nr; i++) {
302 const GLuint format = map[i].format;
303 if (format == EMIT_PAD) {
304 if (DBG)
305 _mesa_printf("%d: pad %d, offset %d\n", i,
306 map[i].offset, offset);
307
308 offset += map[i].offset;
309
310 }
311 else {
312 GLuint tmpoffset;
313
314 if (unpacked_size)
315 tmpoffset = map[i].offset;
316 else
317 tmpoffset = offset;
318
319 if (vtx->attr_count != j ||
320 vtx->attr[j].attrib != map[i].attrib ||
321 vtx->attr[j].format != format ||
322 vtx->attr[j].vertoffset != tmpoffset) {
323 invalidate_funcs(vtx);
324
325 vtx->attr[j].attrib = map[i].attrib;
326 vtx->attr[j].format = format;
327 vtx->attr[j].vp = vp;
328 vtx->attr[j].insert = _tnl_format_info[format].insert;
329 vtx->attr[j].extract = _tnl_format_info[format].extract;
330 vtx->attr[j].vertattrsize = _tnl_format_info[format].attrsize;
331 vtx->attr[j].vertoffset = tmpoffset;
332 }
333
334
335 if (DBG)
336 _mesa_printf("%d: %s, vp %p, offset %d\n", i,
337 _tnl_format_info[format].name, (void *)vp,
338 vtx->attr[j].vertoffset);
339
340 offset += _tnl_format_info[format].attrsize;
341 j++;
342 }
343 }
344
345 vtx->attr_count = j;
346
347 if (unpacked_size)
348 vtx->vertex_size = unpacked_size;
349 else
350 vtx->vertex_size = offset;
351
352 assert(vtx->vertex_size <= vtx->max_vertex_size);
353 return vtx->vertex_size;
354 }
355
356
357
358 void _tnl_invalidate_vertices( GLcontext *ctx, GLuint newinputs )
359 {
360 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
361 vtx->new_inputs |= newinputs;
362 }
363
364
365 /* This event has broader use beyond this file - will move elsewhere
366 * and probably invoke a driver callback.
367 */
368 void _tnl_notify_pipeline_output_change( GLcontext *ctx )
369 {
370 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
371 invalidate_funcs(vtx);
372 }
373
374 static void update_input_ptrs( GLcontext *ctx, GLuint start )
375 {
376 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
377 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
378 struct tnl_clipspace_attr *a = vtx->attr;
379 const GLuint count = vtx->attr_count;
380 GLuint j;
381
382 for (j = 0; j < count; j++) {
383 GLvector4f *vptr = VB->AttribPtr[a[j].attrib];
384
385 if (vtx->emit != choose_emit_func) {
386 assert(a[j].inputstride == vptr->stride);
387 assert(a[j].inputsize == vptr->size);
388 }
389
390 a[j].inputptr = ((GLubyte *)vptr->data) + start * vptr->stride;
391 }
392 }
393
394
395 void _tnl_build_vertices( GLcontext *ctx,
396 GLuint start,
397 GLuint end,
398 GLuint newinputs )
399 {
400 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
401 update_input_ptrs( ctx, start );
402 vtx->emit( ctx, end - start,
403 (GLubyte *)(vtx->vertex_buf +
404 start * vtx->vertex_size));
405 }
406
407 /* Emit VB vertices start..end to dest. Note that VB vertex at
408 * postion start will be emitted to dest at position zero.
409 */
410 void *_tnl_emit_vertices_to_buffer( GLcontext *ctx,
411 GLuint start,
412 GLuint end,
413 void *dest )
414 {
415 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
416
417 update_input_ptrs(ctx, start);
418
419 /* Note: dest should not be adjusted for non-zero 'start' values:
420 */
421 vtx->emit( ctx, end - start, dest );
422 return (void *)((GLubyte *)dest + vtx->vertex_size * (end - start));
423 }
424
425
426 void _tnl_init_vertices( GLcontext *ctx,
427 GLuint vb_size,
428 GLuint max_vertex_size )
429 {
430 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
431
432 _tnl_install_attrs( ctx, NULL, 0, NULL, 0 );
433
434 vtx->need_extras = GL_TRUE;
435 if (max_vertex_size > vtx->max_vertex_size) {
436 _tnl_free_vertices( ctx );
437 vtx->max_vertex_size = max_vertex_size;
438 vtx->vertex_buf = (GLubyte *)ALIGN_CALLOC(vb_size * max_vertex_size, 32 );
439 invalidate_funcs(vtx);
440 }
441
442 switch(CHAN_TYPE) {
443 case GL_UNSIGNED_BYTE:
444 vtx->chan_scale[0] = 255.0;
445 vtx->chan_scale[1] = 255.0;
446 vtx->chan_scale[2] = 255.0;
447 vtx->chan_scale[3] = 255.0;
448 break;
449 case GL_UNSIGNED_SHORT:
450 vtx->chan_scale[0] = 65535.0;
451 vtx->chan_scale[1] = 65535.0;
452 vtx->chan_scale[2] = 65535.0;
453 vtx->chan_scale[3] = 65535.0;
454 break;
455 default:
456 vtx->chan_scale[0] = 1.0;
457 vtx->chan_scale[1] = 1.0;
458 vtx->chan_scale[2] = 1.0;
459 vtx->chan_scale[3] = 1.0;
460 break;
461 }
462
463 vtx->identity[0] = 0.0;
464 vtx->identity[1] = 0.0;
465 vtx->identity[2] = 0.0;
466 vtx->identity[3] = 1.0;
467
468 vtx->codegen_emit = NULL;
469
470 #ifdef __i386__
471 if (getenv("MESA_EXPERIMENTAL"))
472 vtx->codegen_emit = _tnl_generate_sse_emit;
473 #endif
474 }
475
476
477 void _tnl_free_vertices( GLcontext *ctx )
478 {
479 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
480 struct tnl_clipspace_fastpath *fp, *tmp;
481
482 if (vtx->vertex_buf) {
483 ALIGN_FREE(vtx->vertex_buf);
484 vtx->vertex_buf = NULL;
485 }
486
487 for (fp = vtx->fastpath ; fp ; fp = tmp) {
488 tmp = fp->next;
489 FREE(fp->attr);
490 FREE((void *)fp->func);
491 FREE(fp);
492 }
493
494 vtx->fastpath = NULL;
495 }