2 * Copyright 2003 Tungsten Graphics, inc.
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:
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
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.
25 * Keith Whitwell <keithw@tungstengraphics.com>
32 /* Build and manage clipspace/ndc/window vertices.
35 static GLboolean
match_fastpath( struct tnl_clipspace
*vtx
,
36 const struct tnl_clipspace_fastpath
*fp
)
40 if (vtx
->attr_count
!= fp
->attr_count
)
43 for (j
= 0; j
< vtx
->attr_count
; j
++)
44 if (vtx
->attr
[j
].format
!= fp
->attr
[j
].format
||
45 vtx
->attr
[j
].inputsize
!= fp
->attr
[j
].size
||
46 vtx
->attr
[j
].vertoffset
!= fp
->attr
[j
].offset
)
49 if (fp
->match_strides
) {
50 if (vtx
->vertex_size
!= fp
->vertex_size
)
53 for (j
= 0; j
< vtx
->attr_count
; j
++)
54 if (vtx
->attr
[j
].inputstride
!= fp
->attr
[j
].stride
)
61 static GLboolean
search_fastpath_emit( struct tnl_clipspace
*vtx
)
63 struct tnl_clipspace_fastpath
*fp
= vtx
->fastpath
;
65 for ( ; fp
; fp
= fp
->next
) {
66 if (match_fastpath(vtx
, fp
)) {
75 void _tnl_register_fastpath( struct tnl_clipspace
*vtx
,
76 GLboolean match_strides
)
78 struct tnl_clipspace_fastpath
*fastpath
= CALLOC_STRUCT(tnl_clipspace_fastpath
);
81 fastpath
->vertex_size
= vtx
->vertex_size
;
82 fastpath
->attr_count
= vtx
->attr_count
;
83 fastpath
->match_strides
= match_strides
;
84 fastpath
->func
= vtx
->emit
;
85 fastpath
->attr
= (struct tnl_attr_type
*)
86 malloc(vtx
->attr_count
* sizeof(fastpath
->attr
[0]));
88 for (i
= 0; i
< vtx
->attr_count
; i
++) {
89 fastpath
->attr
[i
].format
= vtx
->attr
[i
].format
;
90 fastpath
->attr
[i
].stride
= vtx
->attr
[i
].inputstride
;
91 fastpath
->attr
[i
].size
= vtx
->attr
[i
].inputsize
;
92 fastpath
->attr
[i
].offset
= vtx
->attr
[i
].vertoffset
;
95 fastpath
->next
= vtx
->fastpath
;
96 vtx
->fastpath
= fastpath
;
101 /***********************************************************************
102 * Build codegen functions or return generic ones:
104 static void choose_emit_func( struct gl_context
*ctx
, GLuint count
, GLubyte
*dest
)
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
;
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 */
121 /* Does this match an existing (hardwired, codegen or known-bad)
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.
130 else if (vtx
->codegen_emit
) {
131 vtx
->codegen_emit(ctx
);
135 _tnl_generate_hardwired_emit(ctx
);
138 /* Otherwise use the generic version:
141 vtx
->emit
= _tnl_generic_emit
;
143 vtx
->emit( ctx
, count
, dest
);
148 static void choose_interp_func( struct gl_context
*ctx
,
150 GLuint edst
, GLuint eout
, GLuint ein
,
151 GLboolean force_boundary
)
153 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
155 if (vtx
->need_extras
&&
156 (ctx
->_TriangleCaps
& (DD_TRI_LIGHT_TWOSIDE
|DD_TRI_UNFILLED
))) {
157 vtx
->interp
= _tnl_generic_interp_extras
;
159 vtx
->interp
= _tnl_generic_interp
;
162 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
166 static void choose_copy_pv_func( struct gl_context
*ctx
, GLuint edst
, GLuint esrc
)
168 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
170 if (vtx
->need_extras
&&
171 (ctx
->_TriangleCaps
& (DD_TRI_LIGHT_TWOSIDE
|DD_TRI_UNFILLED
))) {
172 vtx
->copy_pv
= _tnl_generic_copy_pv_extras
;
174 vtx
->copy_pv
= _tnl_generic_copy_pv
;
177 vtx
->copy_pv( ctx
, edst
, esrc
);
181 /***********************************************************************
182 * Public entrypoints, mostly dispatch to the above:
186 /* Interpolate between two vertices to produce a third:
188 void _tnl_interp( struct gl_context
*ctx
,
190 GLuint edst
, GLuint eout
, GLuint ein
,
191 GLboolean force_boundary
)
193 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
194 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
197 /* Copy colors from one vertex to another:
199 void _tnl_copy_pv( struct gl_context
*ctx
, GLuint edst
, GLuint esrc
)
201 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
202 vtx
->copy_pv( ctx
, edst
, esrc
);
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:
210 void _tnl_get_attr( struct gl_context
*ctx
, const void *vin
,
211 GLenum attr
, GLfloat
*dest
)
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
;
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
);
225 /* Else return the value from ctx->Current.
227 if (attr
== _TNL_ATTRIB_POINTSIZE
) {
228 /* If the hardware vertex doesn't have point size then use size from
229 * struct gl_context. XXX this will be wrong if drawing attenuated points!
231 dest
[0] = ctx
->Point
.Size
;
234 memcpy( dest
, ctx
->Current
.Attrib
[attr
], 4*sizeof(GLfloat
));
239 /* Complementary operation to the above.
241 void _tnl_set_attr( struct gl_context
*ctx
, void *vout
,
242 GLenum attr
, const GLfloat
*src
)
244 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
245 const struct tnl_clipspace_attr
*a
= vtx
->attr
;
246 const GLuint attr_count
= vtx
->attr_count
;
249 for (j
= 0; j
< attr_count
; j
++) {
250 if (a
[j
].attrib
== attr
) {
251 a
[j
].insert
[4-1]( &a
[j
], (GLubyte
*)vout
+ a
[j
].vertoffset
, src
);
258 void *_tnl_get_vertex( struct gl_context
*ctx
, GLuint nr
)
260 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
262 return vtx
->vertex_buf
+ nr
* vtx
->vertex_size
;
265 void _tnl_invalidate_vertex_state( struct gl_context
*ctx
, GLuint new_state
)
267 if (new_state
& (_DD_NEW_TRI_LIGHT_TWOSIDE
|_DD_NEW_TRI_UNFILLED
) ) {
268 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
269 vtx
->new_inputs
= ~0;
270 vtx
->interp
= choose_interp_func
;
271 vtx
->copy_pv
= choose_copy_pv_func
;
275 static void invalidate_funcs( struct tnl_clipspace
*vtx
)
277 vtx
->emit
= choose_emit_func
;
278 vtx
->interp
= choose_interp_func
;
279 vtx
->copy_pv
= choose_copy_pv_func
;
280 vtx
->new_inputs
= ~0;
283 GLuint
_tnl_install_attrs( struct gl_context
*ctx
, const struct tnl_attr_map
*map
,
284 GLuint nr
, const GLfloat
*vp
,
285 GLuint unpacked_size
)
287 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
291 assert(nr
< _TNL_ATTRIB_MAX
);
292 assert(nr
== 0 || map
[0].attrib
== VERT_ATTRIB_POS
);
294 vtx
->new_inputs
= ~0;
295 vtx
->need_viewport
= GL_FALSE
;
298 vtx
->need_viewport
= GL_TRUE
;
301 for (j
= 0, i
= 0; i
< nr
; i
++) {
302 const GLuint format
= map
[i
].format
;
303 if (format
== EMIT_PAD
) {
305 printf("%d: pad %d, offset %d\n", i
,
306 map
[i
].offset
, offset
);
308 offset
+= map
[i
].offset
;
315 tmpoffset
= map
[i
].offset
;
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
);
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
;
336 printf("%d: %s, vp %p, offset %d\n", i
,
337 _tnl_format_info
[format
].name
, (void *)vp
,
338 vtx
->attr
[j
].vertoffset
);
340 offset
+= _tnl_format_info
[format
].attrsize
;
348 vtx
->vertex_size
= unpacked_size
;
350 vtx
->vertex_size
= offset
;
352 assert(vtx
->vertex_size
<= vtx
->max_vertex_size
);
353 return vtx
->vertex_size
;
358 void _tnl_invalidate_vertices( struct gl_context
*ctx
, GLuint newinputs
)
360 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
361 vtx
->new_inputs
|= newinputs
;
365 /* This event has broader use beyond this file - will move elsewhere
366 * and probably invoke a driver callback.
368 void _tnl_notify_pipeline_output_change( struct gl_context
*ctx
)
370 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
371 invalidate_funcs(vtx
);
375 static void adjust_input_ptrs( struct gl_context
*ctx
, GLint diff
)
377 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
378 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
379 struct tnl_clipspace_attr
*a
= vtx
->attr
;
380 const GLuint count
= vtx
->attr_count
;
384 for (j
=0; j
<count
; ++j
) {
385 register GLvector4f
*vptr
= VB
->AttribPtr
[a
->attrib
];
386 (a
++)->inputptr
+= diff
*vptr
->stride
;
390 static void update_input_ptrs( struct gl_context
*ctx
, GLuint start
)
392 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
393 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
394 struct tnl_clipspace_attr
*a
= vtx
->attr
;
395 const GLuint count
= vtx
->attr_count
;
398 for (j
= 0; j
< count
; j
++) {
399 GLvector4f
*vptr
= VB
->AttribPtr
[a
[j
].attrib
];
401 if (vtx
->emit
!= choose_emit_func
) {
402 assert(a
[j
].inputstride
== vptr
->stride
);
403 assert(a
[j
].inputsize
== vptr
->size
);
406 a
[j
].inputptr
= ((GLubyte
*)vptr
->data
) + start
* vptr
->stride
;
410 vtx
->vp_scale
[0] = a
->vp
[MAT_SX
];
411 vtx
->vp_scale
[1] = a
->vp
[MAT_SY
];
412 vtx
->vp_scale
[2] = a
->vp
[MAT_SZ
];
413 vtx
->vp_scale
[3] = 1.0;
414 vtx
->vp_xlate
[0] = a
->vp
[MAT_TX
];
415 vtx
->vp_xlate
[1] = a
->vp
[MAT_TY
];
416 vtx
->vp_xlate
[2] = a
->vp
[MAT_TZ
];
417 vtx
->vp_xlate
[3] = 0.0;
422 void _tnl_build_vertices( struct gl_context
*ctx
,
427 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
428 update_input_ptrs( ctx
, start
);
429 vtx
->emit( ctx
, end
- start
,
430 (GLubyte
*)(vtx
->vertex_buf
+
431 start
* vtx
->vertex_size
));
434 /* Emit VB vertices start..end to dest. Note that VB vertex at
435 * position start will be emitted to dest at position zero.
437 void *_tnl_emit_vertices_to_buffer( struct gl_context
*ctx
,
442 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
444 update_input_ptrs(ctx
, start
);
445 /* Note: dest should not be adjusted for non-zero 'start' values:
447 vtx
->emit( ctx
, end
- start
, (GLubyte
*) dest
);
448 return (void *)((GLubyte
*)dest
+ vtx
->vertex_size
* (end
- start
));
451 /* Emit indexed VB vertices start..end to dest. Note that VB vertex at
452 * position start will be emitted to dest at position zero.
455 void *_tnl_emit_indexed_vertices_to_buffer( struct gl_context
*ctx
,
461 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
463 GLubyte
*cdest
= dest
;
465 update_input_ptrs(ctx
, oldIndex
= elts
[start
++]);
466 vtx
->emit( ctx
, 1, cdest
);
467 cdest
+= vtx
->vertex_size
;
469 for (; start
< end
; ++start
) {
470 adjust_input_ptrs(ctx
, elts
[start
] - oldIndex
);
471 oldIndex
= elts
[start
];
472 vtx
->emit( ctx
, 1, cdest
);
473 cdest
+= vtx
->vertex_size
;
476 return (void *) cdest
;
480 void _tnl_init_vertices( struct gl_context
*ctx
,
482 GLuint max_vertex_size
)
484 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
486 _tnl_install_attrs( ctx
, NULL
, 0, NULL
, 0 );
488 vtx
->need_extras
= GL_TRUE
;
489 if (max_vertex_size
> vtx
->max_vertex_size
) {
490 _tnl_free_vertices( ctx
);
491 vtx
->max_vertex_size
= max_vertex_size
;
492 vtx
->vertex_buf
= (GLubyte
*)_mesa_align_calloc(vb_size
* max_vertex_size
, 32 );
493 invalidate_funcs(vtx
);
497 case GL_UNSIGNED_BYTE
:
498 vtx
->chan_scale
[0] = 255.0;
499 vtx
->chan_scale
[1] = 255.0;
500 vtx
->chan_scale
[2] = 255.0;
501 vtx
->chan_scale
[3] = 255.0;
503 case GL_UNSIGNED_SHORT
:
504 vtx
->chan_scale
[0] = 65535.0;
505 vtx
->chan_scale
[1] = 65535.0;
506 vtx
->chan_scale
[2] = 65535.0;
507 vtx
->chan_scale
[3] = 65535.0;
510 vtx
->chan_scale
[0] = 1.0;
511 vtx
->chan_scale
[1] = 1.0;
512 vtx
->chan_scale
[2] = 1.0;
513 vtx
->chan_scale
[3] = 1.0;
517 vtx
->identity
[0] = 0.0;
518 vtx
->identity
[1] = 0.0;
519 vtx
->identity
[2] = 0.0;
520 vtx
->identity
[3] = 1.0;
522 vtx
->codegen_emit
= NULL
;
525 if (!_mesa_getenv("MESA_NO_CODEGEN"))
526 vtx
->codegen_emit
= _tnl_generate_sse_emit
;
531 void _tnl_free_vertices( struct gl_context
*ctx
)
533 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
535 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
536 struct tnl_clipspace_fastpath
*fp
, *tmp
;
538 if (vtx
->vertex_buf
) {
539 _mesa_align_free(vtx
->vertex_buf
);
540 vtx
->vertex_buf
= NULL
;
543 for (fp
= vtx
->fastpath
; fp
; fp
= tmp
) {
547 /* KW: At the moment, fp->func is constrained to be allocated by
548 * _mesa_exec_alloc(), as the hardwired fastpaths in
549 * t_vertex_generic.c are handled specially. It would be nice
550 * to unify them, but this probably won't change until this
551 * module gets another overhaul.
553 _mesa_exec_free((void *) fp
->func
);
557 vtx
->fastpath
= NULL
;