Merge commit 'origin/gallium-0.1'
[mesa.git] / src / mesa / drivers / dri / savage / savagerender.c
1 /*
2 * Copyright 2005 Felix Kuehling
3 * All rights reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
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.
23 */
24
25 /*
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.
29 */
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"
35
36 #include "tnl/t_context.h"
37
38 #include "savagecontext.h"
39 #include "savagetris.h"
40 #include "savagestate.h"
41 #include "savageioctl.h"
42
43 /*
44 * Standard render tab for Savage4 and smooth shading on Savage3D
45 */
46 #define HAVE_POINTS 0
47 #define HAVE_LINES 0
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
54 #define HAVE_QUADS 0
55 #define HAVE_QUAD_STRIPS 0
56
57 #define HAVE_ELTS 1
58
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); \
63 switch (prim) { \
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; \
67 } \
68 } while (0)
69 #define FLUSH() savageFlushElts(imesa), savageFlushVertices(imesa)
70
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)
75
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 )
80
81 #define ELTS_VARS( buf ) GLushort *dest = buf, firstElt = imesa->firstElt
82 #define ELT_INIT( prim ) INIT(prim)
83
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)
93
94 #define ALLOC_ELTS(nr) savageAllocElts(imesa, nr)
95 #define EMIT_ELT(offset, x) do { \
96 (dest)[offset] = (GLushort) ((x)+firstElt); \
97 } while (0)
98 #define EMIT_TWO_ELTS(offset, x, y) do { \
99 *(GLuint *)(dest + offset) = (((y)+firstElt) << 16) | \
100 ((x)+firstElt); \
101 } while (0)
102
103 #define INCR_ELTS( nr ) dest += nr
104 #define ELTPTR dest
105 #define RELEASE_ELT_VERTS() \
106 savageReleaseIndexedVerts(imesa)
107
108 #define EMIT_INDEXED_VERTS( ctx, start, count ) do { \
109 GLuint *buf = savageAllocIndexedVerts(imesa, count-start); \
110 EMIT_VERTS(ctx, start, count-start, buf); \
111 } while (0)
112
113 #define TAG(x) savage_##x
114 #include "tnl_dd/t_dd_dmatmp.h"
115
116 /*
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.
122 */
123 #undef HAVE_TRI_STRIPS
124 #undef HAVE_TRI_FANS
125 #define HAVE_TRI_STRIPS 0
126 #define HAVE_TRI_FANS 0
127
128 #undef INIT
129 #define INIT( prim ) do { \
130 if (0) fprintf(stderr, "%s\n", __FUNCTION__); \
131 savageFlushVertices(imesa); \
132 imesa->HwPrim = SAVAGE_PRIM_TRILIST_201; \
133 } while(0)
134
135 #undef TAG
136 #define TAG(x) savage_flat_##x##_s3d
137 #include "tnl_dd/t_dd_dmatmp.h"
138
139
140 /**********************************************************************/
141 /* Render pipeline stage */
142 /**********************************************************************/
143
144 static GLboolean savage_run_render( GLcontext *ctx,
145 struct tnl_pipeline_stage *stage )
146 {
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;
151 GLboolean valid;
152 GLuint i;
153
154 if (savageHaveIndexedVerts(imesa))
155 savageReleaseIndexedVerts(imesa);
156
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 );
162 } else {
163 tab = savage_render_tab_verts;
164 tab_elts = savage_render_tab_elts;
165 valid = savage_validate_render( ctx, VB );
166 }
167
168 /* Don't handle clipping or vertex manipulations.
169 */
170 if (imesa->RenderIndex != 0 || !valid) {
171 return GL_TRUE;
172 }
173
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.
177 */
178 if (imesa->RenderIndex != 0 || imesa->Fallback != 0) {
179 return GL_TRUE;
180 }
181
182 /* setup for hardware culling */
183 imesa->raster_primitive = GL_TRIANGLES;
184 imesa->new_state |= SAVAGE_NEW_CULL;
185
186 /* update and emit state */
187 savageDDUpdateHwState(ctx);
188 savageEmitChangedState(imesa);
189
190 if (VB->Elts) {
191 tab = tab_elts;
192 if (!savageHaveIndexedVerts(imesa)) {
193 if (VB->Count > GET_SUBSEQUENT_VB_MAX_VERTS())
194 return GL_TRUE;
195 EMIT_INDEXED_VERTS(ctx, 0, VB->Count);
196 }
197 }
198
199 for (i = 0 ; i < VB->PrimitiveCount ; i++)
200 {
201 GLuint prim = _tnl_translate_prim(&VB->Primitive[i]);
202 GLuint start = VB->Primitive[i].start;
203 GLuint length = VB->Primitive[i].count;
204
205 if (length)
206 tab[prim & PRIM_MODE_MASK]( ctx, start, start+length, prim);
207 }
208
209 tnl->Driver.Render.Finish( ctx );
210
211 return GL_FALSE; /* finished the pipe */
212 }
213
214 struct tnl_pipeline_stage _savage_render_stage =
215 {
216 "savage render",
217 NULL,
218 NULL,
219 NULL,
220 NULL,
221 savage_run_render /* run */
222 };
223
224
225 /**********************************************************************/
226 /* Pipeline stage for texture coordinate normalization */
227 /**********************************************************************/
228 struct texnorm_stage_data {
229 GLboolean active;
230 GLvector4f texcoord[MAX_TEXTURE_UNITS];
231 };
232
233 #define TEXNORM_STAGE_DATA(stage) ((struct texnorm_stage_data *)stage->privatePtr)
234
235
236 static GLboolean run_texnorm_stage( GLcontext *ctx,
237 struct tnl_pipeline_stage *stage )
238 {
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;
243 GLuint i;
244
245 if (imesa->Fallback || !store->active)
246 return GL_TRUE;
247
248 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
249 const GLbitfield reallyEnabled = ctx->Texture.Unit[i]._ReallyEnabled;
250 if (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;
258 GLint j;
259
260 if (!ctx->Texture.Unit[i]._ReallyEnabled ||
261 VB->TexCoordPtr[i]->size == 4)
262 /* Never try to normalize homogenous tex coords! */
263 continue;
264
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);
273 }
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;
280 out[j][1] = in[1];
281 in = (GLfloat *)((GLubyte *)in + instride);
282 }
283 } else {
284 for (j = 0; j < VB->Count; ++j) {
285 out[j][0] = in[0] + correctionS;
286 in = (GLfloat *)((GLubyte *)in + instride);
287 }
288 }
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) {
293 out[j][0] = in[0];
294 out[j][1] = in[1] + correctionT;
295 in = (GLfloat *)((GLubyte *)in + instride);
296 }
297 }
298
299 if (normalizeS || normalizeT)
300 VB->AttribPtr[VERT_ATTRIB_TEX0+i] = VB->TexCoordPtr[i] = &store->texcoord[i];
301 }
302 }
303
304 return GL_TRUE;
305 }
306
307 /* Called the first time stage->run() is invoked.
308 */
309 static GLboolean alloc_texnorm_data( GLcontext *ctx,
310 struct tnl_pipeline_stage *stage )
311 {
312 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
313 struct texnorm_stage_data *store;
314 GLuint i;
315
316 stage->privatePtr = CALLOC(sizeof(*store));
317 store = TEXNORM_STAGE_DATA(stage);
318 if (!store)
319 return GL_FALSE;
320
321 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
322 _mesa_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 );
323
324 return GL_TRUE;
325 }
326
327 static void validate_texnorm( GLcontext *ctx,
328 struct tnl_pipeline_stage *stage )
329 {
330 struct texnorm_stage_data *store = TEXNORM_STAGE_DATA(stage);
331 GLuint flags = 0;
332
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;
338
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;
344
345 store->active = (flags != 0);
346 }
347
348 static void free_texnorm_data( struct tnl_pipeline_stage *stage )
349 {
350 struct texnorm_stage_data *store = TEXNORM_STAGE_DATA(stage);
351 GLuint i;
352
353 if (store) {
354 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++)
355 if (store->texcoord[i].data)
356 _mesa_vector4f_free( &store->texcoord[i] );
357 FREE( store );
358 stage->privatePtr = 0;
359 }
360 }
361
362 struct tnl_pipeline_stage _savage_texnorm_stage =
363 {
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 */
368 validate_texnorm,
369 run_texnorm_stage
370 };