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>
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/colormac.h"
32 #include "t_context.h"
37 /* Build and manage clipspace/ndc/window vertices.
40 static GLboolean
match_fastpath( struct tnl_clipspace
*vtx
,
41 const struct tnl_clipspace_fastpath
*fp
)
45 if (vtx
->attr_count
!= fp
->attr_count
)
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
)
54 if (fp
->match_strides
) {
55 if (vtx
->vertex_size
!= fp
->vertex_size
)
58 for (j
= 0; j
< vtx
->attr_count
; j
++)
59 if (vtx
->attr
[j
].inputstride
!= fp
->attr
[j
].stride
)
66 static GLboolean
search_fastpath_emit( struct tnl_clipspace
*vtx
)
68 struct tnl_clipspace_fastpath
*fp
= vtx
->fastpath
;
70 for ( ; fp
; fp
= fp
->next
) {
71 if (match_fastpath(vtx
, fp
)) {
80 void _tnl_register_fastpath( struct tnl_clipspace
*vtx
,
81 GLboolean match_strides
)
83 struct tnl_clipspace_fastpath
*fastpath
= CALLOC_STRUCT(tnl_clipspace_fastpath
);
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]));
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
;
100 fastpath
->next
= vtx
->fastpath
;
101 vtx
->fastpath
= fastpath
;
106 /***********************************************************************
107 * Build codegen functions or return generic ones:
109 static void choose_emit_func( GLcontext
*ctx
, GLuint count
, GLubyte
*dest
)
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
;
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 */
126 /* Does this match an existing (hardwired, codegen or known-bad)
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.
135 else if (vtx
->codegen_emit
) {
136 vtx
->codegen_emit(ctx
);
140 _tnl_generate_hardwired_emit(ctx
);
143 /* Otherwise use the generic version:
146 vtx
->emit
= _tnl_generic_emit
;
148 vtx
->emit( ctx
, count
, dest
);
153 static void choose_interp_func( GLcontext
*ctx
,
155 GLuint edst
, GLuint eout
, GLuint ein
,
156 GLboolean force_boundary
)
158 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
160 if (vtx
->need_extras
&&
161 (ctx
->_TriangleCaps
& (DD_TRI_LIGHT_TWOSIDE
|DD_TRI_UNFILLED
))) {
162 vtx
->interp
= _tnl_generic_interp_extras
;
164 vtx
->interp
= _tnl_generic_interp
;
167 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
171 static void choose_copy_pv_func( GLcontext
*ctx
, GLuint edst
, GLuint esrc
)
173 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
175 if (vtx
->need_extras
&&
176 (ctx
->_TriangleCaps
& (DD_TRI_LIGHT_TWOSIDE
|DD_TRI_UNFILLED
))) {
177 vtx
->copy_pv
= _tnl_generic_copy_pv_extras
;
179 vtx
->copy_pv
= _tnl_generic_copy_pv
;
182 vtx
->copy_pv( ctx
, edst
, esrc
);
186 /***********************************************************************
187 * Public entrypoints, mostly dispatch to the above:
191 /* Interpolate between two vertices to produce a third:
193 void _tnl_interp( GLcontext
*ctx
,
195 GLuint edst
, GLuint eout
, GLuint ein
,
196 GLboolean force_boundary
)
198 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
199 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
202 /* Copy colors from one vertex to another:
204 void _tnl_copy_pv( GLcontext
*ctx
, GLuint edst
, GLuint esrc
)
206 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
207 vtx
->copy_pv( ctx
, edst
, esrc
);
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:
215 void _tnl_get_attr( GLcontext
*ctx
, const void *vin
,
216 GLenum attr
, GLfloat
*dest
)
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
;
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
);
230 /* Else return the value from ctx->Current.
232 if (attr
== _TNL_ATTRIB_POINTSIZE
) {
233 /* If the hardware vertex doesn't have point size then use size from
234 * GLcontext. XXX this will be wrong if drawing attenuated points!
236 dest
[0] = ctx
->Point
.Size
;
239 _mesa_memcpy( dest
, ctx
->Current
.Attrib
[attr
], 4*sizeof(GLfloat
));
244 /* Complementary operation to the above.
246 void _tnl_set_attr( GLcontext
*ctx
, void *vout
,
247 GLenum attr
, const GLfloat
*src
)
249 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
250 const struct tnl_clipspace_attr
*a
= vtx
->attr
;
251 const GLuint attr_count
= vtx
->attr_count
;
254 for (j
= 0; j
< attr_count
; j
++) {
255 if (a
[j
].attrib
== attr
) {
256 a
[j
].insert
[4-1]( &a
[j
], (GLubyte
*)vout
+ a
[j
].vertoffset
, src
);
263 void *_tnl_get_vertex( GLcontext
*ctx
, GLuint nr
)
265 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
267 return vtx
->vertex_buf
+ nr
* vtx
->vertex_size
;
270 void _tnl_invalidate_vertex_state( GLcontext
*ctx
, GLuint new_state
)
272 if (new_state
& (_DD_NEW_TRI_LIGHT_TWOSIDE
|_DD_NEW_TRI_UNFILLED
) ) {
273 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
274 vtx
->new_inputs
= ~0;
275 vtx
->interp
= choose_interp_func
;
276 vtx
->copy_pv
= choose_copy_pv_func
;
280 static void invalidate_funcs( struct tnl_clipspace
*vtx
)
282 vtx
->emit
= choose_emit_func
;
283 vtx
->interp
= choose_interp_func
;
284 vtx
->copy_pv
= choose_copy_pv_func
;
285 vtx
->new_inputs
= ~0;
288 GLuint
_tnl_install_attrs( GLcontext
*ctx
, const struct tnl_attr_map
*map
,
289 GLuint nr
, const GLfloat
*vp
,
290 GLuint unpacked_size
)
292 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
296 assert(nr
< _TNL_ATTRIB_MAX
);
297 assert(nr
== 0 || map
[0].attrib
== VERT_ATTRIB_POS
);
299 vtx
->new_inputs
= ~0;
300 vtx
->need_viewport
= GL_FALSE
;
303 vtx
->need_viewport
= GL_TRUE
;
306 for (j
= 0, i
= 0; i
< nr
; i
++) {
307 const GLuint format
= map
[i
].format
;
308 if (format
== EMIT_PAD
) {
310 _mesa_printf("%d: pad %d, offset %d\n", i
,
311 map
[i
].offset
, offset
);
313 offset
+= map
[i
].offset
;
320 tmpoffset
= map
[i
].offset
;
324 if (vtx
->attr_count
!= j
||
325 vtx
->attr
[j
].attrib
!= map
[i
].attrib
||
326 vtx
->attr
[j
].format
!= format
||
327 vtx
->attr
[j
].vertoffset
!= tmpoffset
) {
328 invalidate_funcs(vtx
);
330 vtx
->attr
[j
].attrib
= map
[i
].attrib
;
331 vtx
->attr
[j
].format
= format
;
332 vtx
->attr
[j
].vp
= vp
;
333 vtx
->attr
[j
].insert
= _tnl_format_info
[format
].insert
;
334 vtx
->attr
[j
].extract
= _tnl_format_info
[format
].extract
;
335 vtx
->attr
[j
].vertattrsize
= _tnl_format_info
[format
].attrsize
;
336 vtx
->attr
[j
].vertoffset
= tmpoffset
;
341 _mesa_printf("%d: %s, vp %p, offset %d\n", i
,
342 _tnl_format_info
[format
].name
, (void *)vp
,
343 vtx
->attr
[j
].vertoffset
);
345 offset
+= _tnl_format_info
[format
].attrsize
;
353 vtx
->vertex_size
= unpacked_size
;
355 vtx
->vertex_size
= offset
;
357 assert(vtx
->vertex_size
<= vtx
->max_vertex_size
);
358 return vtx
->vertex_size
;
363 void _tnl_invalidate_vertices( GLcontext
*ctx
, GLuint newinputs
)
365 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
366 vtx
->new_inputs
|= newinputs
;
370 /* This event has broader use beyond this file - will move elsewhere
371 * and probably invoke a driver callback.
373 void _tnl_notify_pipeline_output_change( GLcontext
*ctx
)
375 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
376 invalidate_funcs(vtx
);
380 static void adjust_input_ptrs( GLcontext
*ctx
, GLint diff
)
382 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
383 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
384 struct tnl_clipspace_attr
*a
= vtx
->attr
;
385 const GLuint count
= vtx
->attr_count
;
389 for (j
=0; j
<count
; ++j
) {
390 register GLvector4f
*vptr
= VB
->AttribPtr
[a
->attrib
];
391 (a
++)->inputptr
+= diff
*vptr
->stride
;
395 static void update_input_ptrs( GLcontext
*ctx
, GLuint start
)
397 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
398 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
399 struct tnl_clipspace_attr
*a
= vtx
->attr
;
400 const GLuint count
= vtx
->attr_count
;
403 for (j
= 0; j
< count
; j
++) {
404 GLvector4f
*vptr
= VB
->AttribPtr
[a
[j
].attrib
];
406 if (vtx
->emit
!= choose_emit_func
) {
407 assert(a
[j
].inputstride
== vptr
->stride
);
408 assert(a
[j
].inputsize
== vptr
->size
);
411 a
[j
].inputptr
= ((GLubyte
*)vptr
->data
) + start
* vptr
->stride
;
415 vtx
->vp_scale
[0] = a
->vp
[MAT_SX
];
416 vtx
->vp_scale
[1] = a
->vp
[MAT_SY
];
417 vtx
->vp_scale
[2] = a
->vp
[MAT_SZ
];
418 vtx
->vp_scale
[3] = 1.0;
419 vtx
->vp_xlate
[0] = a
->vp
[MAT_TX
];
420 vtx
->vp_xlate
[1] = a
->vp
[MAT_TY
];
421 vtx
->vp_xlate
[2] = a
->vp
[MAT_TZ
];
422 vtx
->vp_xlate
[3] = 0.0;
427 void _tnl_build_vertices( GLcontext
*ctx
,
432 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
433 update_input_ptrs( ctx
, start
);
434 vtx
->emit( ctx
, end
- start
,
435 (GLubyte
*)(vtx
->vertex_buf
+
436 start
* vtx
->vertex_size
));
439 /* Emit VB vertices start..end to dest. Note that VB vertex at
440 * postion start will be emitted to dest at position zero.
442 void *_tnl_emit_vertices_to_buffer( GLcontext
*ctx
,
447 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
449 update_input_ptrs(ctx
, start
);
450 /* Note: dest should not be adjusted for non-zero 'start' values:
452 vtx
->emit( ctx
, end
- start
, (GLubyte
*) dest
);
453 return (void *)((GLubyte
*)dest
+ vtx
->vertex_size
* (end
- start
));
456 /* Emit indexed VB vertices start..end to dest. Note that VB vertex at
457 * postion start will be emitted to dest at position zero.
460 void *_tnl_emit_indexed_vertices_to_buffer( GLcontext
*ctx
,
466 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
468 GLubyte
*cdest
= dest
;
470 update_input_ptrs(ctx
, oldIndex
= elts
[start
++]);
471 vtx
->emit( ctx
, 1, cdest
);
472 cdest
+= vtx
->vertex_size
;
474 for (; start
< end
; ++start
) {
475 adjust_input_ptrs(ctx
, elts
[start
] - oldIndex
);
476 oldIndex
= elts
[start
];
477 vtx
->emit( ctx
, 1, cdest
);
478 cdest
+= vtx
->vertex_size
;
481 return (void *) cdest
;
485 void _tnl_init_vertices( GLcontext
*ctx
,
487 GLuint max_vertex_size
)
489 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
491 _tnl_install_attrs( ctx
, NULL
, 0, NULL
, 0 );
493 vtx
->need_extras
= GL_TRUE
;
494 if (max_vertex_size
> vtx
->max_vertex_size
) {
495 _tnl_free_vertices( ctx
);
496 vtx
->max_vertex_size
= max_vertex_size
;
497 vtx
->vertex_buf
= (GLubyte
*)ALIGN_CALLOC(vb_size
* max_vertex_size
, 32 );
498 invalidate_funcs(vtx
);
502 case GL_UNSIGNED_BYTE
:
503 vtx
->chan_scale
[0] = 255.0;
504 vtx
->chan_scale
[1] = 255.0;
505 vtx
->chan_scale
[2] = 255.0;
506 vtx
->chan_scale
[3] = 255.0;
508 case GL_UNSIGNED_SHORT
:
509 vtx
->chan_scale
[0] = 65535.0;
510 vtx
->chan_scale
[1] = 65535.0;
511 vtx
->chan_scale
[2] = 65535.0;
512 vtx
->chan_scale
[3] = 65535.0;
515 vtx
->chan_scale
[0] = 1.0;
516 vtx
->chan_scale
[1] = 1.0;
517 vtx
->chan_scale
[2] = 1.0;
518 vtx
->chan_scale
[3] = 1.0;
522 vtx
->identity
[0] = 0.0;
523 vtx
->identity
[1] = 0.0;
524 vtx
->identity
[2] = 0.0;
525 vtx
->identity
[3] = 1.0;
527 vtx
->codegen_emit
= NULL
;
530 if (!_mesa_getenv("MESA_NO_CODEGEN"))
531 vtx
->codegen_emit
= _tnl_generate_sse_emit
;
536 void _tnl_free_vertices( GLcontext
*ctx
)
538 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
540 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
541 struct tnl_clipspace_fastpath
*fp
, *tmp
;
543 if (vtx
->vertex_buf
) {
544 ALIGN_FREE(vtx
->vertex_buf
);
545 vtx
->vertex_buf
= NULL
;
548 for (fp
= vtx
->fastpath
; fp
; fp
= tmp
) {
552 /* KW: At the moment, fp->func is constrained to be allocated by
553 * _mesa_exec_alloc(), as the hardwired fastpaths in
554 * t_vertex_generic.c are handled specially. It would be nice
555 * to unify them, but this probably won't change until this
556 * module gets another overhaul.
558 _mesa_exec_free((void *) fp
->func
);
562 vtx
->fastpath
= NULL
;