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