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 #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
= MALLOC(vtx
->attr_count
* sizeof(fastpath
->attr
[0]));
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
;
99 fastpath
->next
= vtx
->fastpath
;
100 vtx
->fastpath
= fastpath
;
105 /***********************************************************************
106 * Build codegen functions or return generic ones:
108 static void choose_emit_func( GLcontext
*ctx
, GLuint count
, GLubyte
*dest
)
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
;
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 */
125 /* Does this match an existing (hardwired, codegen or known-bad)
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.
134 else if (vtx
->codegen_emit
) {
135 vtx
->codegen_emit(ctx
);
139 _tnl_generate_hardwired_emit(ctx
);
142 /* Otherwise use the generic version:
145 vtx
->emit
= _tnl_generic_emit
;
147 vtx
->emit( ctx
, count
, dest
);
152 static void choose_interp_func( GLcontext
*ctx
,
154 GLuint edst
, GLuint eout
, GLuint ein
,
155 GLboolean force_boundary
)
157 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
159 if (vtx
->need_extras
&&
160 (ctx
->_TriangleCaps
& (DD_TRI_LIGHT_TWOSIDE
|DD_TRI_UNFILLED
))) {
161 vtx
->interp
= _tnl_generic_interp_extras
;
163 vtx
->interp
= _tnl_generic_interp
;
166 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
170 static void choose_copy_pv_func( GLcontext
*ctx
, GLuint edst
, GLuint esrc
)
172 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
174 if (vtx
->need_extras
&&
175 (ctx
->_TriangleCaps
& (DD_TRI_LIGHT_TWOSIDE
|DD_TRI_UNFILLED
))) {
176 vtx
->copy_pv
= _tnl_generic_copy_pv_extras
;
178 vtx
->copy_pv
= _tnl_generic_copy_pv
;
181 vtx
->copy_pv( ctx
, edst
, esrc
);
185 /***********************************************************************
186 * Public entrypoints, mostly dispatch to the above:
190 /* Interpolate between two vertices to produce a third:
192 void _tnl_interp( GLcontext
*ctx
,
194 GLuint edst
, GLuint eout
, GLuint ein
,
195 GLboolean force_boundary
)
197 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
198 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
201 /* Copy colors from one vertex to another:
203 void _tnl_copy_pv( GLcontext
*ctx
, GLuint edst
, GLuint esrc
)
205 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
206 vtx
->copy_pv( ctx
, edst
, esrc
);
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:
214 void _tnl_get_attr( GLcontext
*ctx
, const void *vin
,
215 GLenum attr
, GLfloat
*dest
)
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
;
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
);
229 /* Else return the value from ctx->Current.
231 _mesa_memcpy( dest
, ctx
->Current
.Attrib
[attr
], 4*sizeof(GLfloat
));
235 /* Complementary operation to the above.
237 void _tnl_set_attr( GLcontext
*ctx
, void *vout
,
238 GLenum attr
, const GLfloat
*src
)
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
;
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
);
254 void *_tnl_get_vertex( GLcontext
*ctx
, GLuint nr
)
256 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
258 return vtx
->vertex_buf
+ nr
* vtx
->vertex_size
;
261 void _tnl_invalidate_vertex_state( GLcontext
*ctx
, GLuint new_state
)
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
;
271 static void invalidate_funcs( struct tnl_clipspace
*vtx
)
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;
279 GLuint
_tnl_install_attrs( GLcontext
*ctx
, const struct tnl_attr_map
*map
,
280 GLuint nr
, const GLfloat
*vp
,
281 GLuint unpacked_size
)
283 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
287 assert(nr
< _TNL_ATTRIB_MAX
);
288 assert(nr
== 0 || map
[0].attrib
== VERT_ATTRIB_POS
);
290 vtx
->new_inputs
= ~0;
291 vtx
->need_viewport
= GL_FALSE
;
294 vtx
->need_viewport
= GL_TRUE
;
297 for (j
= 0, i
= 0; i
< nr
; i
++) {
298 const GLuint format
= map
[i
].format
;
299 if (format
== EMIT_PAD
) {
301 _mesa_printf("%d: pad %d, offset %d\n", i
,
302 map
[i
].offset
, offset
);
304 offset
+= map
[i
].offset
;
311 tmpoffset
= map
[i
].offset
;
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
);
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
;
332 _mesa_printf("%d: %s, vp %p, offset %d\n", i
,
333 _tnl_format_info
[format
].name
, (void *)vp
,
334 vtx
->attr
[j
].vertoffset
);
336 offset
+= _tnl_format_info
[format
].attrsize
;
344 vtx
->vertex_size
= unpacked_size
;
346 vtx
->vertex_size
= offset
;
348 assert(vtx
->vertex_size
<= vtx
->max_vertex_size
);
349 return vtx
->vertex_size
;
354 void _tnl_invalidate_vertices( GLcontext
*ctx
, GLuint newinputs
)
356 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
357 vtx
->new_inputs
|= newinputs
;
361 /* This event has broader use beyond this file - will move elsewhere
362 * and probably invoke a driver callback.
364 void _tnl_notify_pipeline_output_change( GLcontext
*ctx
)
366 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
367 invalidate_funcs(vtx
);
370 static void update_input_ptrs( GLcontext
*ctx
, GLuint start
)
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
;
378 for (j
= 0; j
< count
; j
++) {
379 GLvector4f
*vptr
= VB
->AttribPtr
[a
[j
].attrib
];
381 if (vtx
->emit
!= choose_emit_func
) {
382 assert(a
[j
].inputstride
== vptr
->stride
);
383 assert(a
[j
].inputsize
== vptr
->size
);
386 a
[j
].inputptr
= ((GLubyte
*)vptr
->data
) + start
* vptr
->stride
;
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;
402 void _tnl_build_vertices( GLcontext
*ctx
,
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
));
414 /* Emit VB vertices start..end to dest. Note that VB vertex at
415 * postion start will be emitted to dest at position zero.
417 void *_tnl_emit_vertices_to_buffer( GLcontext
*ctx
,
422 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
424 update_input_ptrs(ctx
, start
);
426 /* Note: dest should not be adjusted for non-zero 'start' values:
428 vtx
->emit( ctx
, end
- start
, dest
);
429 return (void *)((GLubyte
*)dest
+ vtx
->vertex_size
* (end
- start
));
433 void _tnl_init_vertices( GLcontext
*ctx
,
435 GLuint max_vertex_size
)
437 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
439 _tnl_install_attrs( ctx
, NULL
, 0, NULL
, 0 );
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
);
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;
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;
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;
470 vtx
->identity
[0] = 0.0;
471 vtx
->identity
[1] = 0.0;
472 vtx
->identity
[2] = 0.0;
473 vtx
->identity
[3] = 1.0;
475 vtx
->codegen_emit
= NULL
;
478 if (!_mesa_getenv("MESA_NO_CODEGEN"))
479 vtx
->codegen_emit
= _tnl_generate_sse_emit
;
484 void _tnl_free_vertices( GLcontext
*ctx
)
486 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
487 struct tnl_clipspace_fastpath
*fp
, *tmp
;
489 if (vtx
->vertex_buf
) {
490 ALIGN_FREE(vtx
->vertex_buf
);
491 vtx
->vertex_buf
= NULL
;
494 for (fp
= vtx
->fastpath
; fp
; fp
= tmp
) {
497 FREE((void *)fp
->func
);
501 vtx
->fastpath
= NULL
;