2 * Copyright 2003 VMware, 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 * VMWARE 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@vmware.com>
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "swrast/s_chan.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 if (fastpath
== NULL
) {
87 _mesa_error_no_memory(__func__
);
91 fastpath
->vertex_size
= vtx
->vertex_size
;
92 fastpath
->attr_count
= vtx
->attr_count
;
93 fastpath
->match_strides
= match_strides
;
94 fastpath
->func
= vtx
->emit
;
95 fastpath
->attr
= malloc(vtx
->attr_count
* sizeof(fastpath
->attr
[0]));
97 if (fastpath
->attr
== NULL
) {
99 _mesa_error_no_memory(__func__
);
103 for (i
= 0; i
< vtx
->attr_count
; i
++) {
104 fastpath
->attr
[i
].format
= vtx
->attr
[i
].format
;
105 fastpath
->attr
[i
].stride
= vtx
->attr
[i
].inputstride
;
106 fastpath
->attr
[i
].size
= vtx
->attr
[i
].inputsize
;
107 fastpath
->attr
[i
].offset
= vtx
->attr
[i
].vertoffset
;
110 fastpath
->next
= vtx
->fastpath
;
111 vtx
->fastpath
= fastpath
;
116 /***********************************************************************
117 * Build codegen functions or return generic ones:
119 static void choose_emit_func( struct gl_context
*ctx
, GLuint count
, GLubyte
*dest
)
121 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
122 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
123 struct tnl_clipspace_attr
*a
= vtx
->attr
;
124 const GLuint attr_count
= vtx
->attr_count
;
127 for (j
= 0; j
< attr_count
; j
++) {
128 GLvector4f
*vptr
= VB
->AttribPtr
[a
[j
].attrib
];
129 a
[j
].inputstride
= vptr
->stride
;
130 a
[j
].inputsize
= vptr
->size
;
131 a
[j
].emit
= a
[j
].insert
[vptr
->size
- 1]; /* not always used */
136 /* Does this match an existing (hardwired, codegen or known-bad)
139 if (search_fastpath_emit(vtx
)) {
140 /* Use this result. If it is null, then it is already known
141 * that the current state will fail for codegen and there is no
142 * point trying again.
145 else if (vtx
->codegen_emit
) {
146 vtx
->codegen_emit(ctx
);
150 _tnl_generate_hardwired_emit(ctx
);
153 /* Otherwise use the generic version:
156 vtx
->emit
= _tnl_generic_emit
;
158 vtx
->emit( ctx
, count
, dest
);
163 static void choose_interp_func( struct gl_context
*ctx
,
165 GLuint edst
, GLuint eout
, GLuint ein
,
166 GLboolean force_boundary
)
168 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
169 GLboolean unfilled
= (ctx
->Polygon
.FrontMode
!= GL_FILL
||
170 ctx
->Polygon
.BackMode
!= GL_FILL
);
171 GLboolean twosided
= ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
;
173 if (vtx
->need_extras
&& (twosided
|| unfilled
)) {
174 vtx
->interp
= _tnl_generic_interp_extras
;
176 vtx
->interp
= _tnl_generic_interp
;
179 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
183 static void choose_copy_pv_func( struct gl_context
*ctx
, GLuint edst
, GLuint esrc
)
185 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
186 GLboolean unfilled
= (ctx
->Polygon
.FrontMode
!= GL_FILL
||
187 ctx
->Polygon
.BackMode
!= GL_FILL
);
189 GLboolean twosided
= ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
;
191 if (vtx
->need_extras
&& (twosided
|| unfilled
)) {
192 vtx
->copy_pv
= _tnl_generic_copy_pv_extras
;
194 vtx
->copy_pv
= _tnl_generic_copy_pv
;
197 vtx
->copy_pv( ctx
, edst
, esrc
);
201 /***********************************************************************
202 * Public entrypoints, mostly dispatch to the above:
206 /* Interpolate between two vertices to produce a third:
208 void _tnl_interp( struct gl_context
*ctx
,
210 GLuint edst
, GLuint eout
, GLuint ein
,
211 GLboolean force_boundary
)
213 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
214 vtx
->interp( ctx
, t
, edst
, eout
, ein
, force_boundary
);
217 /* Copy colors from one vertex to another:
219 void _tnl_copy_pv( struct gl_context
*ctx
, GLuint edst
, GLuint esrc
)
221 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
222 vtx
->copy_pv( ctx
, edst
, esrc
);
226 /* Extract a named attribute from a hardware vertex. Will have to
227 * reverse any viewport transformation, swizzling or other conversions
228 * which may have been applied:
230 void _tnl_get_attr( struct gl_context
*ctx
, const void *vin
,
231 GLenum attr
, GLfloat
*dest
)
233 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
234 const struct tnl_clipspace_attr
*a
= vtx
->attr
;
235 const GLuint attr_count
= vtx
->attr_count
;
238 for (j
= 0; j
< attr_count
; j
++) {
239 if (a
[j
].attrib
== attr
) {
240 a
[j
].extract( &a
[j
], dest
, (GLubyte
*)vin
+ a
[j
].vertoffset
);
245 /* Else return the value from ctx->Current.
247 if (attr
== _TNL_ATTRIB_POINTSIZE
) {
248 /* If the hardware vertex doesn't have point size then use size from
249 * struct gl_context. XXX this will be wrong if drawing attenuated points!
251 dest
[0] = ctx
->Point
.Size
;
254 memcpy( dest
, ctx
->Current
.Attrib
[attr
], 4*sizeof(GLfloat
));
259 /* Complementary operation to the above.
261 void _tnl_set_attr( struct gl_context
*ctx
, void *vout
,
262 GLenum attr
, const GLfloat
*src
)
264 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
265 const struct tnl_clipspace_attr
*a
= vtx
->attr
;
266 const GLuint attr_count
= vtx
->attr_count
;
269 for (j
= 0; j
< attr_count
; j
++) {
270 if (a
[j
].attrib
== attr
) {
271 a
[j
].insert
[4-1]( &a
[j
], (GLubyte
*)vout
+ a
[j
].vertoffset
, src
);
278 void *_tnl_get_vertex( struct gl_context
*ctx
, GLuint nr
)
280 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
282 return vtx
->vertex_buf
+ nr
* vtx
->vertex_size
;
285 void _tnl_invalidate_vertex_state( struct gl_context
*ctx
, GLuint new_state
)
287 /* if two-sided lighting changes or filled/unfilled polygon state changes */
288 if (new_state
& (_NEW_LIGHT
| _NEW_POLYGON
) ) {
289 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
290 vtx
->new_inputs
= ~0;
291 vtx
->interp
= choose_interp_func
;
292 vtx
->copy_pv
= choose_copy_pv_func
;
296 static void invalidate_funcs( struct tnl_clipspace
*vtx
)
298 vtx
->emit
= choose_emit_func
;
299 vtx
->interp
= choose_interp_func
;
300 vtx
->copy_pv
= choose_copy_pv_func
;
301 vtx
->new_inputs
= ~0;
304 GLuint
_tnl_install_attrs( struct gl_context
*ctx
, const struct tnl_attr_map
*map
,
305 GLuint nr
, const GLfloat
*vp
,
306 GLuint unpacked_size
)
308 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
312 assert(nr
< _TNL_ATTRIB_MAX
);
313 assert(nr
== 0 || map
[0].attrib
== VERT_ATTRIB_POS
);
315 vtx
->new_inputs
= ~0;
316 vtx
->need_viewport
= GL_FALSE
;
319 vtx
->need_viewport
= GL_TRUE
;
322 for (j
= 0, i
= 0; i
< nr
; i
++) {
323 const GLuint format
= map
[i
].format
;
324 if (format
== EMIT_PAD
) {
326 printf("%d: pad %d, offset %d\n", i
,
327 map
[i
].offset
, offset
);
329 offset
+= map
[i
].offset
;
336 tmpoffset
= map
[i
].offset
;
340 if (vtx
->attr_count
!= j
||
341 vtx
->attr
[j
].attrib
!= map
[i
].attrib
||
342 vtx
->attr
[j
].format
!= format
||
343 vtx
->attr
[j
].vertoffset
!= tmpoffset
) {
344 invalidate_funcs(vtx
);
346 vtx
->attr
[j
].attrib
= map
[i
].attrib
;
347 vtx
->attr
[j
].format
= format
;
348 vtx
->attr
[j
].vp
= vp
;
349 vtx
->attr
[j
].insert
= _tnl_format_info
[format
].insert
;
350 vtx
->attr
[j
].extract
= _tnl_format_info
[format
].extract
;
351 vtx
->attr
[j
].vertattrsize
= _tnl_format_info
[format
].attrsize
;
352 vtx
->attr
[j
].vertoffset
= tmpoffset
;
357 printf("%d: %s, vp %p, offset %d\n", i
,
358 _tnl_format_info
[format
].name
, (void *)vp
,
359 vtx
->attr
[j
].vertoffset
);
361 offset
+= _tnl_format_info
[format
].attrsize
;
369 vtx
->vertex_size
= unpacked_size
;
371 vtx
->vertex_size
= offset
;
373 assert(vtx
->vertex_size
<= vtx
->max_vertex_size
);
374 return vtx
->vertex_size
;
379 void _tnl_invalidate_vertices( struct gl_context
*ctx
, GLuint newinputs
)
381 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
382 vtx
->new_inputs
|= newinputs
;
386 /* This event has broader use beyond this file - will move elsewhere
387 * and probably invoke a driver callback.
389 void _tnl_notify_pipeline_output_change( struct gl_context
*ctx
)
391 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
392 invalidate_funcs(vtx
);
396 static void adjust_input_ptrs( struct gl_context
*ctx
, GLint diff
)
398 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
399 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
400 struct tnl_clipspace_attr
*a
= vtx
->attr
;
401 const GLuint count
= vtx
->attr_count
;
405 for (j
=0; j
<count
; ++j
) {
406 register GLvector4f
*vptr
= VB
->AttribPtr
[a
->attrib
];
407 (a
++)->inputptr
+= diff
*vptr
->stride
;
411 static void update_input_ptrs( struct gl_context
*ctx
, GLuint start
)
413 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
414 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
415 struct tnl_clipspace_attr
*a
= vtx
->attr
;
416 const GLuint count
= vtx
->attr_count
;
419 for (j
= 0; j
< count
; j
++) {
420 GLvector4f
*vptr
= VB
->AttribPtr
[a
[j
].attrib
];
422 if (vtx
->emit
!= choose_emit_func
) {
423 assert(a
[j
].inputstride
== vptr
->stride
);
424 assert(a
[j
].inputsize
== vptr
->size
);
427 a
[j
].inputptr
= ((GLubyte
*)vptr
->data
) + start
* vptr
->stride
;
431 vtx
->vp_scale
[0] = a
->vp
[MAT_SX
];
432 vtx
->vp_scale
[1] = a
->vp
[MAT_SY
];
433 vtx
->vp_scale
[2] = a
->vp
[MAT_SZ
];
434 vtx
->vp_scale
[3] = 1.0;
435 vtx
->vp_xlate
[0] = a
->vp
[MAT_TX
];
436 vtx
->vp_xlate
[1] = a
->vp
[MAT_TY
];
437 vtx
->vp_xlate
[2] = a
->vp
[MAT_TZ
];
438 vtx
->vp_xlate
[3] = 0.0;
443 void _tnl_build_vertices( struct gl_context
*ctx
,
448 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
449 update_input_ptrs( ctx
, start
);
450 vtx
->emit( ctx
, end
- start
,
451 (GLubyte
*)(vtx
->vertex_buf
+
452 start
* vtx
->vertex_size
));
455 /* Emit VB vertices start..end to dest. Note that VB vertex at
456 * postion start will be emitted to dest at position zero.
458 void *_tnl_emit_vertices_to_buffer( struct gl_context
*ctx
,
463 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
465 update_input_ptrs(ctx
, start
);
466 /* Note: dest should not be adjusted for non-zero 'start' values:
468 vtx
->emit( ctx
, end
- start
, (GLubyte
*) dest
);
469 return (void *)((GLubyte
*)dest
+ vtx
->vertex_size
* (end
- start
));
472 /* Emit indexed VB vertices start..end to dest. Note that VB vertex at
473 * postion start will be emitted to dest at position zero.
476 void *_tnl_emit_indexed_vertices_to_buffer( struct gl_context
*ctx
,
482 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
484 GLubyte
*cdest
= dest
;
486 update_input_ptrs(ctx
, oldIndex
= elts
[start
++]);
487 vtx
->emit( ctx
, 1, cdest
);
488 cdest
+= vtx
->vertex_size
;
490 for (; start
< end
; ++start
) {
491 adjust_input_ptrs(ctx
, elts
[start
] - oldIndex
);
492 oldIndex
= elts
[start
];
493 vtx
->emit( ctx
, 1, cdest
);
494 cdest
+= vtx
->vertex_size
;
497 return (void *) cdest
;
501 void _tnl_init_vertices( struct gl_context
*ctx
,
503 GLuint max_vertex_size
)
505 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
507 _tnl_install_attrs( ctx
, NULL
, 0, NULL
, 0 );
509 vtx
->need_extras
= GL_TRUE
;
510 if (max_vertex_size
> vtx
->max_vertex_size
) {
511 _tnl_free_vertices( ctx
);
512 vtx
->max_vertex_size
= max_vertex_size
;
513 vtx
->vertex_buf
= _mesa_align_calloc(vb_size
* max_vertex_size
, 32 );
514 invalidate_funcs(vtx
);
518 case GL_UNSIGNED_BYTE
:
519 vtx
->chan_scale
[0] = 255.0;
520 vtx
->chan_scale
[1] = 255.0;
521 vtx
->chan_scale
[2] = 255.0;
522 vtx
->chan_scale
[3] = 255.0;
524 case GL_UNSIGNED_SHORT
:
525 vtx
->chan_scale
[0] = 65535.0;
526 vtx
->chan_scale
[1] = 65535.0;
527 vtx
->chan_scale
[2] = 65535.0;
528 vtx
->chan_scale
[3] = 65535.0;
531 vtx
->chan_scale
[0] = 1.0;
532 vtx
->chan_scale
[1] = 1.0;
533 vtx
->chan_scale
[2] = 1.0;
534 vtx
->chan_scale
[3] = 1.0;
538 vtx
->identity
[0] = 0.0;
539 vtx
->identity
[1] = 0.0;
540 vtx
->identity
[2] = 0.0;
541 vtx
->identity
[3] = 1.0;
543 vtx
->codegen_emit
= NULL
;
546 if (!getenv("MESA_NO_CODEGEN"))
547 vtx
->codegen_emit
= _tnl_generate_sse_emit
;
552 void _tnl_free_vertices( struct gl_context
*ctx
)
554 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
556 struct tnl_clipspace
*vtx
= GET_VERTEX_STATE(ctx
);
557 struct tnl_clipspace_fastpath
*fp
, *tmp
;
559 _mesa_align_free(vtx
->vertex_buf
);
560 vtx
->vertex_buf
= NULL
;
562 for (fp
= vtx
->fastpath
; fp
; fp
= tmp
) {
566 /* KW: At the moment, fp->func is constrained to be allocated by
567 * _mesa_exec_alloc(), as the hardwired fastpaths in
568 * t_vertex_generic.c are handled specially. It would be nice
569 * to unify them, but this probably won't change until this
570 * module gets another overhaul.
572 _mesa_exec_free((void *) fp
->func
);
576 vtx
->fastpath
= NULL
;