2 * Copyright 2005 Felix Kuehling
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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Render unclipped vertex buffers by emitting vertices directly to
27 * dma buffers. Use strip/fan hardware primitives where possible.
28 * Simulate missing primitives with indexed vertices.
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/macros.h"
33 #include "main/imports.h"
34 #include "main/mtypes.h"
36 #include "tnl/t_context.h"
38 #include "savagecontext.h"
39 #include "savagetris.h"
40 #include "savagestate.h"
41 #include "savageioctl.h"
44 * Standard render tab for Savage4 and smooth shading on Savage3D
48 #define HAVE_LINE_STRIPS 0
49 #define HAVE_TRIANGLES 1
50 #define HAVE_TRI_STRIPS 1
51 #define HAVE_TRI_STRIP_1 0
52 #define HAVE_TRI_FANS 1
53 #define HAVE_POLYGONS 0
55 #define HAVE_QUAD_STRIPS 0
59 #define LOCAL_VARS savageContextPtr imesa = SAVAGE_CONTEXT(ctx)
60 #define INIT( prim ) do { \
61 if (0) fprintf(stderr, "%s\n", __FUNCTION__); \
62 savageFlushVertices(imesa); \
64 case GL_TRIANGLES: imesa->HwPrim = SAVAGE_PRIM_TRILIST; break; \
65 case GL_TRIANGLE_STRIP: imesa->HwPrim = SAVAGE_PRIM_TRISTRIP; break; \
66 case GL_TRIANGLE_FAN: imesa->HwPrim = SAVAGE_PRIM_TRIFAN; break; \
69 #define FLUSH() savageFlushElts(imesa), savageFlushVertices(imesa)
71 #define GET_CURRENT_VB_MAX_VERTS() \
72 ((imesa->bufferSize/4 - imesa->vtxBuf->used) / imesa->HwVertexSize)
73 #define GET_SUBSEQUENT_VB_MAX_VERTS() \
74 (imesa->bufferSize/4 / imesa->HwVertexSize)
76 #define ALLOC_VERTS( nr ) \
77 savageAllocVtxBuf( imesa, (nr) * imesa->HwVertexSize )
78 #define EMIT_VERTS( ctx, j, nr, buf ) \
79 _tnl_emit_vertices_to_buffer(ctx, j, (j)+(nr), buf )
81 #define ELTS_VARS( buf ) GLushort *dest = buf, firstElt = imesa->firstElt
82 #define ELT_INIT( prim ) INIT(prim)
84 /* (size - used - 1 qword for drawing command) * 4 elts per qword */
85 #define GET_CURRENT_VB_MAX_ELTS() \
86 ((imesa->cmdBuf.size - (imesa->cmdBuf.write - imesa->cmdBuf.base) - 1)*4)
87 /* (size - space for initial state - 1 qword for drawing command) * 4 elts
88 * imesa is not defined in validate_render :( */
89 #define GET_SUBSEQUENT_VB_MAX_ELTS() \
90 ((SAVAGE_CONTEXT(ctx)->cmdBuf.size - \
91 (SAVAGE_CONTEXT(ctx)->cmdBuf.start - \
92 SAVAGE_CONTEXT(ctx)->cmdBuf.base) - 1)*4)
94 #define ALLOC_ELTS(nr) savageAllocElts(imesa, nr)
95 #define EMIT_ELT(offset, x) do { \
96 (dest)[offset] = (GLushort) ((x)+firstElt); \
98 #define EMIT_TWO_ELTS(offset, x, y) do { \
99 *(GLuint *)(dest + offset) = (((y)+firstElt) << 16) | \
103 #define INCR_ELTS( nr ) dest += nr
105 #define RELEASE_ELT_VERTS() \
106 savageReleaseIndexedVerts(imesa)
108 #define EMIT_INDEXED_VERTS( ctx, start, count ) do { \
109 GLuint *buf = savageAllocIndexedVerts(imesa, count-start); \
110 EMIT_VERTS(ctx, start, count-start, buf); \
113 #define TAG(x) savage_##x
114 #include "tnl_dd/t_dd_dmatmp.h"
117 * On Savage3D triangle fans and strips are broken with flat
118 * shading. With triangles it wants the color for flat shading in the
119 * first vertex! So we make another template instance which uses
120 * triangles only (with reordered vertices: SAVAGE_PRIM_TRILIST_201).
121 * The reordering is done by the DRM.
123 #undef HAVE_TRI_STRIPS
125 #define HAVE_TRI_STRIPS 0
126 #define HAVE_TRI_FANS 0
129 #define INIT( prim ) do { \
130 if (0) fprintf(stderr, "%s\n", __FUNCTION__); \
131 savageFlushVertices(imesa); \
132 imesa->HwPrim = SAVAGE_PRIM_TRILIST_201; \
136 #define TAG(x) savage_flat_##x##_s3d
137 #include "tnl_dd/t_dd_dmatmp.h"
140 /**********************************************************************/
141 /* Render pipeline stage */
142 /**********************************************************************/
144 static GLboolean
savage_run_render( GLcontext
*ctx
,
145 struct tnl_pipeline_stage
*stage
)
147 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
148 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
149 struct vertex_buffer
*VB
= &tnl
->vb
;
150 tnl_render_func
*tab
, *tab_elts
;
154 if (savageHaveIndexedVerts(imesa
))
155 savageReleaseIndexedVerts(imesa
);
157 if (imesa
->savageScreen
->chipset
< S3_SAVAGE4
&&
158 (ctx
->_TriangleCaps
& DD_FLATSHADE
)) {
159 tab
= savage_flat_render_tab_verts_s3d
;
160 tab_elts
= savage_flat_render_tab_elts_s3d
;
161 valid
= savage_flat_validate_render_s3d( ctx
, VB
);
163 tab
= savage_render_tab_verts
;
164 tab_elts
= savage_render_tab_elts
;
165 valid
= savage_validate_render( ctx
, VB
);
168 /* Don't handle clipping or vertex manipulations.
170 if (imesa
->RenderIndex
!= 0 || !valid
) {
174 tnl
->Driver
.Render
.Start( ctx
);
175 /* Check RenderIndex again. The ptexHack is detected late in RenderStart.
176 * Also check for ptex fallbacks detected late.
178 if (imesa
->RenderIndex
!= 0 || imesa
->Fallback
!= 0) {
182 /* setup for hardware culling */
183 imesa
->raster_primitive
= GL_TRIANGLES
;
184 imesa
->new_state
|= SAVAGE_NEW_CULL
;
186 /* update and emit state */
187 savageDDUpdateHwState(ctx
);
188 savageEmitChangedState(imesa
);
192 if (!savageHaveIndexedVerts(imesa
)) {
193 if (VB
->Count
> GET_SUBSEQUENT_VB_MAX_VERTS())
195 EMIT_INDEXED_VERTS(ctx
, 0, VB
->Count
);
199 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
201 GLuint prim
= _tnl_translate_prim(&VB
->Primitive
[i
]);
202 GLuint start
= VB
->Primitive
[i
].start
;
203 GLuint length
= VB
->Primitive
[i
].count
;
206 tab
[prim
& PRIM_MODE_MASK
]( ctx
, start
, start
+length
, prim
);
209 tnl
->Driver
.Render
.Finish( ctx
);
211 return GL_FALSE
; /* finished the pipe */
214 struct tnl_pipeline_stage _savage_render_stage
=
221 savage_run_render
/* run */
225 /**********************************************************************/
226 /* Pipeline stage for texture coordinate normalization */
227 /**********************************************************************/
228 struct texnorm_stage_data
{
230 GLvector4f texcoord
[MAX_TEXTURE_UNITS
];
233 #define TEXNORM_STAGE_DATA(stage) ((struct texnorm_stage_data *)stage->privatePtr)
236 static GLboolean
run_texnorm_stage( GLcontext
*ctx
,
237 struct tnl_pipeline_stage
*stage
)
239 struct texnorm_stage_data
*store
= TEXNORM_STAGE_DATA(stage
);
240 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
241 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
242 struct vertex_buffer
*VB
= &tnl
->vb
;
245 if (imesa
->Fallback
|| !store
->active
)
248 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
249 const GLbitfield reallyEnabled
= ctx
->Texture
.Unit
[i
]._ReallyEnabled
;
251 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
252 const GLboolean normalizeS
= (texObj
->WrapS
== GL_REPEAT
);
253 const GLboolean normalizeT
= (reallyEnabled
& TEXTURE_2D_BIT
) &&
254 (texObj
->WrapT
== GL_REPEAT
);
255 const GLfloat
*in
= (GLfloat
*)VB
->TexCoordPtr
[i
]->data
;
256 const GLint instride
= VB
->TexCoordPtr
[i
]->stride
;
257 GLfloat (*out
)[4] = store
->texcoord
[i
].data
;
260 if (!ctx
->Texture
.Unit
[i
]._ReallyEnabled
||
261 VB
->TexCoordPtr
[i
]->size
== 4)
262 /* Never try to normalize homogenous tex coords! */
265 if (normalizeS
&& normalizeT
) {
266 /* take first texcoords as rough estimate of mean value */
267 GLfloat correctionS
= -floor(in
[0]+0.5);
268 GLfloat correctionT
= -floor(in
[1]+0.5);
269 for (j
= 0; j
< VB
->Count
; ++j
) {
270 out
[j
][0] = in
[0] + correctionS
;
271 out
[j
][1] = in
[1] + correctionT
;
272 in
= (GLfloat
*)((GLubyte
*)in
+ instride
);
274 } else if (normalizeS
) {
275 /* take first texcoords as rough estimate of mean value */
276 GLfloat correctionS
= -floor(in
[0]+0.5);
277 if (reallyEnabled
& TEXTURE_2D_BIT
) {
278 for (j
= 0; j
< VB
->Count
; ++j
) {
279 out
[j
][0] = in
[0] + correctionS
;
281 in
= (GLfloat
*)((GLubyte
*)in
+ instride
);
284 for (j
= 0; j
< VB
->Count
; ++j
) {
285 out
[j
][0] = in
[0] + correctionS
;
286 in
= (GLfloat
*)((GLubyte
*)in
+ instride
);
289 } else if (normalizeT
) {
290 /* take first texcoords as rough estimate of mean value */
291 GLfloat correctionT
= -floor(in
[1]+0.5);
292 for (j
= 0; j
< VB
->Count
; ++j
) {
294 out
[j
][1] = in
[1] + correctionT
;
295 in
= (GLfloat
*)((GLubyte
*)in
+ instride
);
299 if (normalizeS
|| normalizeT
)
300 VB
->AttribPtr
[VERT_ATTRIB_TEX0
+i
] = VB
->TexCoordPtr
[i
] = &store
->texcoord
[i
];
307 /* Called the first time stage->run() is invoked.
309 static GLboolean
alloc_texnorm_data( GLcontext
*ctx
,
310 struct tnl_pipeline_stage
*stage
)
312 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
313 struct texnorm_stage_data
*store
;
316 stage
->privatePtr
= CALLOC(sizeof(*store
));
317 store
= TEXNORM_STAGE_DATA(stage
);
321 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
322 _mesa_vector4f_alloc( &store
->texcoord
[i
], 0, VB
->Size
, 32 );
327 static void validate_texnorm( GLcontext
*ctx
,
328 struct tnl_pipeline_stage
*stage
)
330 struct texnorm_stage_data
*store
= TEXNORM_STAGE_DATA(stage
);
333 if (((ctx
->Texture
.Unit
[0]._ReallyEnabled
& (TEXTURE_1D_BIT
|TEXTURE_2D_BIT
)) &&
334 (ctx
->Texture
.Unit
[0]._Current
->WrapS
== GL_REPEAT
)) ||
335 ((ctx
->Texture
.Unit
[0]._ReallyEnabled
& TEXTURE_2D_BIT
) &&
336 (ctx
->Texture
.Unit
[0]._Current
->WrapT
== GL_REPEAT
)))
337 flags
|= VERT_BIT_TEX0
;
339 if (((ctx
->Texture
.Unit
[1]._ReallyEnabled
& (TEXTURE_1D_BIT
|TEXTURE_2D_BIT
)) &&
340 (ctx
->Texture
.Unit
[1]._Current
->WrapS
== GL_REPEAT
)) ||
341 ((ctx
->Texture
.Unit
[1]._ReallyEnabled
& TEXTURE_2D_BIT
) &&
342 (ctx
->Texture
.Unit
[1]._Current
->WrapT
== GL_REPEAT
)))
343 flags
|= VERT_BIT_TEX1
;
345 store
->active
= (flags
!= 0);
348 static void free_texnorm_data( struct tnl_pipeline_stage
*stage
)
350 struct texnorm_stage_data
*store
= TEXNORM_STAGE_DATA(stage
);
354 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++)
355 if (store
->texcoord
[i
].data
)
356 _mesa_vector4f_free( &store
->texcoord
[i
] );
358 stage
->privatePtr
= 0;
362 struct tnl_pipeline_stage _savage_texnorm_stage
=
364 "savage texture coordinate normalization stage", /* name */
365 NULL
, /* private data */
366 alloc_texnorm_data
, /* run -- initially set to init */
367 free_texnorm_data
, /* destructor */