Merge branch 'nouveau-import'
[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 vtx->attr[j].inputsize != fp->attr[j].size ||
51 vtx->attr[j].vertoffset != fp->attr[j].offset)
52 return GL_FALSE;
53
54 if (fp->match_strides) {
55 if (vtx->vertex_size != fp->vertex_size)
56 return GL_FALSE;
57
58 for (j = 0; j < vtx->attr_count; j++)
59 if (vtx->attr[j].inputstride != fp->attr[j].stride)
60 return GL_FALSE;
61 }
62
63 return GL_TRUE;
64 }
65
66 static GLboolean search_fastpath_emit( struct tnl_clipspace *vtx )
67 {
68 struct tnl_clipspace_fastpath *fp = vtx->fastpath;
69
70 for ( ; fp ; fp = fp->next) {
71 if (match_fastpath(vtx, fp)) {
72 vtx->emit = fp->func;
73 return GL_TRUE;
74 }
75 }
76
77 return GL_FALSE;
78 }
79
80 void _tnl_register_fastpath( struct tnl_clipspace *vtx,
81 GLboolean match_strides )
82 {
83 struct tnl_clipspace_fastpath *fastpath = CALLOC_STRUCT(tnl_clipspace_fastpath);
84 GLuint i;
85
86 fastpath->vertex_size = vtx->vertex_size;
87 fastpath->attr_count = vtx->attr_count;
88 fastpath->match_strides = match_strides;
89 fastpath->func = vtx->emit;
90 fastpath->attr = (struct tnl_attr_type *)
91 _mesa_malloc(vtx->attr_count * sizeof(fastpath->attr[0]));
92
93 for (i = 0; i < vtx->attr_count; i++) {
94 fastpath->attr[i].format = vtx->attr[i].format;
95 fastpath->attr[i].stride = vtx->attr[i].inputstride;
96 fastpath->attr[i].size = vtx->attr[i].inputsize;
97 fastpath->attr[i].offset = vtx->attr[i].vertoffset;
98 }
99
100 fastpath->next = vtx->fastpath;
101 vtx->fastpath = fastpath;
102 }
103
104
105
106 /***********************************************************************
107 * Build codegen functions or return generic ones:
108 */
109 static void choose_emit_func( GLcontext *ctx, GLuint count, GLubyte *dest)
110 {
111 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
112 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
113 struct tnl_clipspace_attr *a = vtx->attr;
114 const GLuint attr_count = vtx->attr_count;
115 GLuint j;
116
117 for (j = 0; j < attr_count; j++) {
118 GLvector4f *vptr = VB->AttribPtr[a[j].attrib];
119 a[j].inputstride = vptr->stride;
120 a[j].inputsize = vptr->size;
121 a[j].emit = a[j].insert[vptr->size - 1]; /* not always used */
122 }
123
124 vtx->emit = NULL;
125
126 /* Does this match an existing (hardwired, codegen or known-bad)
127 * fastpath?
128 */
129 if (search_fastpath_emit(vtx)) {
130 /* Use this result. If it is null, then it is already known
131 * that the current state will fail for codegen and there is no
132 * point trying again.
133 */
134 }
135 else if (vtx->codegen_emit) {
136 vtx->codegen_emit(ctx);
137 }
138
139 if (!vtx->emit) {
140 _tnl_generate_hardwired_emit(ctx);
141 }
142
143 /* Otherwise use the generic version:
144 */
145 if (!vtx->emit)
146 vtx->emit = _tnl_generic_emit;
147
148 vtx->emit( ctx, count, dest );
149 }
150
151
152
153 static void choose_interp_func( GLcontext *ctx,
154 GLfloat t,
155 GLuint edst, GLuint eout, GLuint ein,
156 GLboolean force_boundary )
157 {
158 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
159
160 if (vtx->need_extras &&
161 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
162 vtx->interp = _tnl_generic_interp_extras;
163 } else {
164 vtx->interp = _tnl_generic_interp;
165 }
166
167 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
168 }
169
170
171 static void choose_copy_pv_func( GLcontext *ctx, GLuint edst, GLuint esrc )
172 {
173 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
174
175 if (vtx->need_extras &&
176 (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
177 vtx->copy_pv = _tnl_generic_copy_pv_extras;
178 } else {
179 vtx->copy_pv = _tnl_generic_copy_pv;
180 }
181
182 vtx->copy_pv( ctx, edst, esrc );
183 }
184
185
186 /***********************************************************************
187 * Public entrypoints, mostly dispatch to the above:
188 */
189
190
191 /* Interpolate between two vertices to produce a third:
192 */
193 void _tnl_interp( GLcontext *ctx,
194 GLfloat t,
195 GLuint edst, GLuint eout, GLuint ein,
196 GLboolean force_boundary )
197 {
198 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
199 vtx->interp( ctx, t, edst, eout, ein, force_boundary );
200 }
201
202 /* Copy colors from one vertex to another:
203 */
204 void _tnl_copy_pv( GLcontext *ctx, GLuint edst, GLuint esrc )
205 {
206 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
207 vtx->copy_pv( ctx, edst, esrc );
208 }
209
210
211 /* Extract a named attribute from a hardware vertex. Will have to
212 * reverse any viewport transformation, swizzling or other conversions
213 * which may have been applied:
214 */
215 void _tnl_get_attr( GLcontext *ctx, const void *vin,
216 GLenum attr, GLfloat *dest )
217 {
218 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
219 const struct tnl_clipspace_attr *a = vtx->attr;
220 const GLuint attr_count = vtx->attr_count;
221 GLuint j;
222
223 for (j = 0; j < attr_count; j++) {
224 if (a[j].attrib == attr) {
225 a[j].extract( &a[j], dest, (GLubyte *)vin + a[j].vertoffset );
226 return;
227 }
228 }
229
230 /* Else return the value from ctx->Current.
231 */
232 _mesa_memcpy( dest, ctx->Current.Attrib[attr], 4*sizeof(GLfloat));
233 }
234
235
236 /* Complementary operation to the above.
237 */
238 void _tnl_set_attr( GLcontext *ctx, void *vout,
239 GLenum attr, const GLfloat *src )
240 {
241 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
242 const struct tnl_clipspace_attr *a = vtx->attr;
243 const GLuint attr_count = vtx->attr_count;
244 GLuint j;
245
246 for (j = 0; j < attr_count; j++) {
247 if (a[j].attrib == attr) {
248 a[j].insert[4-1]( &a[j], (GLubyte *)vout + a[j].vertoffset, src );
249 return;
250 }
251 }
252 }
253
254
255 void *_tnl_get_vertex( GLcontext *ctx, GLuint nr )
256 {
257 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
258
259 return vtx->vertex_buf + nr * vtx->vertex_size;
260 }
261
262 void _tnl_invalidate_vertex_state( GLcontext *ctx, GLuint new_state )
263 {
264 if (new_state & (_DD_NEW_TRI_LIGHT_TWOSIDE|_DD_NEW_TRI_UNFILLED) ) {
265 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
266 vtx->new_inputs = ~0;
267 vtx->interp = choose_interp_func;
268 vtx->copy_pv = choose_copy_pv_func;
269 }
270 }
271
272 static void invalidate_funcs( struct tnl_clipspace *vtx )
273 {
274 vtx->emit = choose_emit_func;
275 vtx->interp = choose_interp_func;
276 vtx->copy_pv = choose_copy_pv_func;
277 vtx->new_inputs = ~0;
278 }
279
280 GLuint _tnl_install_attrs( GLcontext *ctx, const struct tnl_attr_map *map,
281 GLuint nr, const GLfloat *vp,
282 GLuint unpacked_size )
283 {
284 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
285 GLuint offset = 0;
286 GLuint i, j;
287
288 assert(nr < _TNL_ATTRIB_MAX);
289 assert(nr == 0 || map[0].attrib == VERT_ATTRIB_POS);
290
291 vtx->new_inputs = ~0;
292 vtx->need_viewport = GL_FALSE;
293
294 if (vp) {
295 vtx->need_viewport = GL_TRUE;
296 }
297
298 for (j = 0, i = 0; i < nr; i++) {
299 const GLuint format = map[i].format;
300 if (format == EMIT_PAD) {
301 if (DBG)
302 _mesa_printf("%d: pad %d, offset %d\n", i,
303 map[i].offset, offset);
304
305 offset += map[i].offset;
306
307 }
308 else {
309 GLuint tmpoffset;
310
311 if (unpacked_size)
312 tmpoffset = map[i].offset;
313 else
314 tmpoffset = offset;
315
316 if (vtx->attr_count != j ||
317 vtx->attr[j].attrib != map[i].attrib ||
318 vtx->attr[j].format != format ||
319 vtx->attr[j].vertoffset != tmpoffset) {
320 invalidate_funcs(vtx);
321
322 vtx->attr[j].attrib = map[i].attrib;
323 vtx->attr[j].format = format;
324 vtx->attr[j].vp = vp;
325 vtx->attr[j].insert = _tnl_format_info[format].insert;
326 vtx->attr[j].extract = _tnl_format_info[format].extract;
327 vtx->attr[j].vertattrsize = _tnl_format_info[format].attrsize;
328 vtx->attr[j].vertoffset = tmpoffset;
329 }
330
331
332 if (DBG)
333 _mesa_printf("%d: %s, vp %p, offset %d\n", i,
334 _tnl_format_info[format].name, (void *)vp,
335 vtx->attr[j].vertoffset);
336
337 offset += _tnl_format_info[format].attrsize;
338 j++;
339 }
340 }
341
342 vtx->attr_count = j;
343
344 if (unpacked_size)
345 vtx->vertex_size = unpacked_size;
346 else
347 vtx->vertex_size = offset;
348
349 assert(vtx->vertex_size <= vtx->max_vertex_size);
350 return vtx->vertex_size;
351 }
352
353
354
355 void _tnl_invalidate_vertices( GLcontext *ctx, GLuint newinputs )
356 {
357 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
358 vtx->new_inputs |= newinputs;
359 }
360
361
362 /* This event has broader use beyond this file - will move elsewhere
363 * and probably invoke a driver callback.
364 */
365 void _tnl_notify_pipeline_output_change( GLcontext *ctx )
366 {
367 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
368 invalidate_funcs(vtx);
369 }
370
371 static void update_input_ptrs( GLcontext *ctx, GLuint start )
372 {
373 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
374 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
375 struct tnl_clipspace_attr *a = vtx->attr;
376 const GLuint count = vtx->attr_count;
377 GLuint j;
378
379 for (j = 0; j < count; j++) {
380 GLvector4f *vptr = VB->AttribPtr[a[j].attrib];
381
382 if (vtx->emit != choose_emit_func) {
383 assert(a[j].inputstride == vptr->stride);
384 assert(a[j].inputsize == vptr->size);
385 }
386
387 a[j].inputptr = ((GLubyte *)vptr->data) + start * vptr->stride;
388 }
389
390 if (a->vp) {
391 vtx->vp_scale[0] = a->vp[MAT_SX];
392 vtx->vp_scale[1] = a->vp[MAT_SY];
393 vtx->vp_scale[2] = a->vp[MAT_SZ];
394 vtx->vp_scale[3] = 1.0;
395 vtx->vp_xlate[0] = a->vp[MAT_TX];
396 vtx->vp_xlate[1] = a->vp[MAT_TY];
397 vtx->vp_xlate[2] = a->vp[MAT_TZ];
398 vtx->vp_xlate[3] = 0.0;
399 }
400 }
401
402
403 void _tnl_build_vertices( GLcontext *ctx,
404 GLuint start,
405 GLuint end,
406 GLuint newinputs )
407 {
408 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
409 update_input_ptrs( ctx, start );
410 vtx->emit( ctx, end - start,
411 (GLubyte *)(vtx->vertex_buf +
412 start * vtx->vertex_size));
413 }
414
415 /* Emit VB vertices start..end to dest. Note that VB vertex at
416 * postion start will be emitted to dest at position zero.
417 */
418 void *_tnl_emit_vertices_to_buffer( GLcontext *ctx,
419 GLuint start,
420 GLuint end,
421 void *dest )
422 {
423 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
424
425 update_input_ptrs(ctx, start);
426
427 /* Note: dest should not be adjusted for non-zero 'start' values:
428 */
429 vtx->emit( ctx, end - start, (GLubyte*) dest );
430 return (void *)((GLubyte *)dest + vtx->vertex_size * (end - start));
431 }
432
433
434 void _tnl_init_vertices( GLcontext *ctx,
435 GLuint vb_size,
436 GLuint max_vertex_size )
437 {
438 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
439
440 _tnl_install_attrs( ctx, NULL, 0, NULL, 0 );
441
442 vtx->need_extras = GL_TRUE;
443 if (max_vertex_size > vtx->max_vertex_size) {
444 _tnl_free_vertices( ctx );
445 vtx->max_vertex_size = max_vertex_size;
446 vtx->vertex_buf = (GLubyte *)ALIGN_CALLOC(vb_size * max_vertex_size, 32 );
447 invalidate_funcs(vtx);
448 }
449
450 switch(CHAN_TYPE) {
451 case GL_UNSIGNED_BYTE:
452 vtx->chan_scale[0] = 255.0;
453 vtx->chan_scale[1] = 255.0;
454 vtx->chan_scale[2] = 255.0;
455 vtx->chan_scale[3] = 255.0;
456 break;
457 case GL_UNSIGNED_SHORT:
458 vtx->chan_scale[0] = 65535.0;
459 vtx->chan_scale[1] = 65535.0;
460 vtx->chan_scale[2] = 65535.0;
461 vtx->chan_scale[3] = 65535.0;
462 break;
463 default:
464 vtx->chan_scale[0] = 1.0;
465 vtx->chan_scale[1] = 1.0;
466 vtx->chan_scale[2] = 1.0;
467 vtx->chan_scale[3] = 1.0;
468 break;
469 }
470
471 vtx->identity[0] = 0.0;
472 vtx->identity[1] = 0.0;
473 vtx->identity[2] = 0.0;
474 vtx->identity[3] = 1.0;
475
476 vtx->codegen_emit = NULL;
477
478 #ifdef USE_SSE_ASM
479 if (!_mesa_getenv("MESA_NO_CODEGEN"))
480 vtx->codegen_emit = _tnl_generate_sse_emit;
481 #endif
482 }
483
484
485 void _tnl_free_vertices( GLcontext *ctx )
486 {
487 struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx);
488 struct tnl_clipspace_fastpath *fp, *tmp;
489
490 if (vtx->vertex_buf) {
491 ALIGN_FREE(vtx->vertex_buf);
492 vtx->vertex_buf = NULL;
493 }
494
495 for (fp = vtx->fastpath ; fp ; fp = tmp) {
496 tmp = fp->next;
497 FREE(fp->attr);
498
499 /* KW: At the moment, fp->func is constrained to be allocated by
500 * _mesa_exec_alloc(), as the hardwired fastpaths in
501 * t_vertex_generic.c are handled specially. It would be nice
502 * to unify them, but this probably won't change until this
503 * module gets another overhaul.
504 */
505 _mesa_exec_free((void *) fp->func);
506 FREE(fp);
507 }
508
509 vtx->fastpath = NULL;
510 }