3
* Mesa 3-D graphics library
6
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8
* Permission is hereby granted, free of charge, to any person obtaining a
9
* copy of this software and associated documentation files (the "Software"),
10
* to deal in the Software without restriction, including without limitation
11
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
* and/or sell copies of the Software, and to permit persons to whom the
13
* Software is furnished to do so, subject to the following conditions:
15
* The above copyright notice and this permission notice shall be included
16
* in all copies or substantial portions of the Software.
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
* Keith Whitwell <keithw@valinux.com>
31
* Render whole vertex buffers, including projection of vertices from
32
* clip space and clipping of primitives.
34
* This file makes calls to project vertices and to the point, line
35
* and triangle rasterizers via the function pointers:
37
* context->Driver.Render.*
42
//---------------------------------------------------------------------------
44
#include "dglcontext.h"
48
//---------------------------------------------------------------------------
56
#include "math/m_matrix.h"
57
#include "math/m_xform.h"
59
#include "tnl/t_pipeline.h"
61
/**********************************************************************/
62
/* Clip single primitives */
63
/**********************************************************************/
67
#define NEGATIVE(x) (GET_FLOAT_BITS(x) & (1<<31))
68
//#define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
70
#define NEGATIVE(x) (x < 0)
71
//#define DIFFERENT_SIGNS(x,y) (x * y <= 0 && x - y != 0)
72
/* Could just use (x*y<0) except for the flatshading requirements.
73
* Maybe there's a better way?
78
#define W(i) coord[i][3]
79
#define Z(i) coord[i][2]
80
#define Y(i) coord[i][1]
81
#define X(i) coord[i][0]
84
#include "tnl/t_vb_cliptmp.h"
88
/**********************************************************************/
89
/* Clip and render whole begin/end objects */
90
/**********************************************************************/
92
#define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
93
#define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
94
#define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
97
/* Vertices, with the possibility of clipping.
99
#define RENDER_POINTS( start, count ) \
100
tnl->Driver.Render.Points( ctx, start, count )
102
#define RENDER_LINE( v1, v2 ) \
104
GLubyte c1 = mask[v1], c2 = mask[v2]; \
105
GLubyte ormask = c1|c2; \
107
LineFunc( ctx, v1, v2 ); \
108
else if (!(c1 & c2 & 0x3f)) \
109
clip_line_4( ctx, v1, v2, ormask ); \
112
#define RENDER_TRI( v1, v2, v3 ) \
114
GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
115
GLubyte ormask = c1|c2|c3; \
117
TriangleFunc( ctx, v1, v2, v3 ); \
118
else if (!(c1 & c2 & c3 & 0x3f)) \
119
clip_tri_4( ctx, v1, v2, v3, ormask ); \
122
#define RENDER_QUAD( v1, v2, v3, v4 ) \
124
GLubyte c1 = mask[v1], c2 = mask[v2]; \
125
GLubyte c3 = mask[v3], c4 = mask[v4]; \
126
GLubyte ormask = c1|c2|c3|c4; \
128
QuadFunc( ctx, v1, v2, v3, v4 ); \
129
else if (!(c1 & c2 & c3 & c4 & 0x3f)) \
130
clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
135
TNLcontext *tnl = TNL_CONTEXT(ctx); \
136
struct vertex_buffer *VB = &tnl->vb; \
137
const GLuint * const elt = VB->Elts; \
138
const GLubyte *mask = VB->ClipMask; \
139
const GLuint sz = VB->ClipPtr->size; \
140
const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
141
const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
142
const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
143
const GLboolean stipple = ctx->Line.StippleFlag; \
144
(void) (LineFunc && TriangleFunc && QuadFunc); \
145
(void) elt; (void) mask; (void) sz; (void) stipple;
147
#define TAG(x) clip_##x##_verts
148
#define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
149
#define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
150
#define PRESERVE_VB_DEFS
151
#include "tnl/t_vb_rendertmp.h"
155
/* Elts, with the possibility of clipping.
159
#define ELT(x) elt[x]
160
#define TAG(x) clip_##x##_elts
161
#include "tnl/t_vb_rendertmp.h"
163
/* TODO: do this for all primitives, verts and elts:
165
static void clip_elt_triangles( struct gl_context *ctx,
170
TNLcontext *tnl = TNL_CONTEXT(ctx);
171
tnl_render_func render_tris = tnl->Driver.Render.PrimTabElts[GL_TRIANGLES];
172
struct vertex_buffer *VB = &tnl->vb;
173
const GLuint * const elt = VB->Elts;
174
GLubyte *mask = VB->ClipMask;
175
GLuint last = count-2;
179
tnl->Driver.Render.PrimitiveNotify( ctx, GL_TRIANGLES );
181
for (j=start; j < last; j+=3 ) {
182
GLubyte c1 = mask[elt[j]];
183
GLubyte c2 = mask[elt[j+1]];
184
GLubyte c3 = mask[elt[j+2]];
185
GLubyte ormask = c1|c2|c3;
188
render_tris( ctx, start, j, 0 );
189
if (!(c1&c2&c3&0x3f))
190
clip_tri_4( ctx, elt[j], elt[j+1], elt[j+2], ormask );
196
render_tris( ctx, start, j, 0 );
199
/**********************************************************************/
200
/* Render whole begin/end objects */
201
/**********************************************************************/
203
#define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
204
#define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
205
#define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
208
/* Vertices, no clipping.
210
#define RENDER_POINTS( start, count ) \
211
tnl->Driver.Render.Points( ctx, start, count )
213
#define RENDER_LINE( v1, v2 ) \
214
LineFunc( ctx, v1, v2 )
216
#define RENDER_TRI( v1, v2, v3 ) \
217
TriangleFunc( ctx, v1, v2, v3 )
219
#define RENDER_QUAD( v1, v2, v3, v4 ) \
220
QuadFunc( ctx, v1, v2, v3, v4 )
222
#define TAG(x) _gld_tnl_##x##_verts
225
TNLcontext *tnl = TNL_CONTEXT(ctx); \
226
struct vertex_buffer *VB = &tnl->vb; \
227
const GLuint * const elt = VB->Elts; \
228
const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
229
const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
230
const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
231
(void) (LineFunc && TriangleFunc && QuadFunc); \
234
#define RESET_STIPPLE tnl->Driver.Render.ResetLineStipple( ctx )
235
#define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
236
#define RENDER_TAB_QUALIFIER
237
#define PRESERVE_VB_DEFS
238
#include "tnl/t_vb_rendertmp.h"
241
/* Elts, no clipping.
244
#define TAG(x) _gld_tnl_##x##_elts
245
#define ELT(x) elt[x]
246
#include "tnl/t_vb_rendertmp.h"
249
/**********************************************************************/
250
/* Helper functions for drivers */
251
/**********************************************************************/
253
void _tnl_RenderClippedPolygon( struct gl_context *ctx, const GLuint *elts, GLuint n )
255
TNLcontext *tnl = TNL_CONTEXT(ctx);
256
struct vertex_buffer *VB = &tnl->vb;
257
GLuint *tmp = VB->Elts;
259
VB->Elts = (GLuint *)elts;
260
tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
264
void _tnl_RenderClippedLine( struct gl_context *ctx, GLuint ii, GLuint jj )
266
TNLcontext *tnl = TNL_CONTEXT(ctx);
267
tnl->Driver.Render.Line( ctx, ii, jj );
272
/**********************************************************************/
273
/* Clip and render whole vertex buffers */
274
/**********************************************************************/
276
tnl_points_func _gldSetupPoints[4] = {
282
tnl_line_func _gldSetupLine[4] = {
284
gld_Line2DSmooth_DX9,
286
gld_Line2DSmooth_DX9,
288
tnl_triangle_func _gldSetupTriangle[4] = {
289
gld_Triangle2DFlat_DX9,
290
gld_Triangle2DSmooth_DX9,
291
gld_Triangle2DFlatExtras_DX9,
292
gld_Triangle2DSmoothExtras_DX9
294
tnl_quad_func _gldSetupQuad[4] = {
296
gld_Quad2DSmooth_DX9,
297
gld_Quad2DFlatExtras_DX9,
298
gld_Quad2DSmoothExtras_DX9
301
//---------------------------------------------------------------------------
303
static GLboolean _gld_mesa_render_stage_run(
304
struct gl_context *ctx,
305
struct tnl_pipeline_stage *stage)
307
GLD_context *gldCtx = GLD_GET_CONTEXT(ctx);
308
GLD_driver_dx9 *gld = GLD_GET_DX9_DRIVER(gldCtx);
310
TNLcontext *tnl = TNL_CONTEXT(ctx);
311
struct vertex_buffer *VB = &tnl->vb;
312
tnl_render_func *tab;
316
/* Allow the drivers to lock before projected verts are built so
317
* that window coordinates are guarenteed not to change before
320
ASSERT(tnl->Driver.Render.Start);
322
tnl->Driver.Render.Start( ctx );
324
// NOTE: Setting D3DRS_SOFTWAREVERTEXPROCESSING for a mixed-mode device resets
325
// stream, indices and shader to default values of NULL or 0.
326
/* if ((ctx->_TriangleCaps & DD_TRI_LIGHT_TWOSIDE) &&
327
gld->VStwosidelight.hShader &&
330
IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_SOFTWAREVERTEXPROCESSING, !gld->VStwosidelight.bHardware);
331
_GLD_DX9_DEV(SetVertexShader(gld->pDev, gld->VStwosidelight.hShader));
332
gldPB = &gld->PBtwosidelight;
333
tnl->Driver.Render.Points = gld_Points2DTwoside_DX9;
334
if (ctx->_TriangleCaps & DD_FLATSHADE) {
335
tnl->Driver.Render.Line = gld_Line2DFlatTwoside_DX9;
336
tnl->Driver.Render.Triangle = gld_Triangle2DFlatTwoside_DX9;
337
tnl->Driver.Render.Quad = gld_Quad2DFlatTwoside_DX9;
339
tnl->Driver.Render.Line = gld_Line2DSmoothTwoside_DX9;
340
tnl->Driver.Render.Triangle = gld_Triangle2DSmoothTwoside_DX9;
341
tnl->Driver.Render.Quad = gld_Quad2DSmoothTwoside_DX9;
344
// IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_SOFTWAREVERTEXPROCESSING, TRUE);
345
IDirect3DDevice9_SetSoftwareVertexProcessing(gld->pDev, TRUE);
347
_GLD_DX9_DEV(SetVertexShader(gld->pDev, NULL));
348
_GLD_DX9_DEV(SetFVF(gld->pDev, gldPB->dwFVF));
349
tnl->Driver.Render.Points = _gldSetupPoints[gld->iSetupFunc];
350
tnl->Driver.Render.Line = _gldSetupLine[gld->iSetupFunc];
351
tnl->Driver.Render.Triangle = _gldSetupTriangle[gld->iSetupFunc];
352
tnl->Driver.Render.Quad = _gldSetupQuad[gld->iSetupFunc];
355
_GLD_DX9_VB(Lock(gldPB->pVB, 0, 0, &gldPB->pPoints, D3DLOCK_DISCARD));
356
gldPB->nPoints = gldPB->nLines = gldPB->nTriangles = 0;
357
// Allocate primitive pointers
358
// gldPB->pPoints is always first
359
gldPB->pLines = gldPB->pPoints + (gldPB->dwStride * gldPB->iFirstLine);
360
gldPB->pTriangles = gldPB->pPoints + (gldPB->dwStride * gldPB->iFirstTriangle);
362
ASSERT(tnl->Driver.Render.BuildVertices);
363
ASSERT(tnl->Driver.Render.PrimitiveNotify);
364
ASSERT(tnl->Driver.Render.Points);
365
ASSERT(tnl->Driver.Render.Line);
366
ASSERT(tnl->Driver.Render.Triangle);
367
ASSERT(tnl->Driver.Render.Quad);
368
ASSERT(tnl->Driver.Render.ResetLineStipple);
369
ASSERT(tnl->Driver.Render.Interp);
370
ASSERT(tnl->Driver.Render.CopyPV);
371
ASSERT(tnl->Driver.Render.ClippedLine);
372
ASSERT(tnl->Driver.Render.ClippedPolygon);
373
ASSERT(tnl->Driver.Render.Finish);
375
tnl->Driver.Render.BuildVertices( ctx, 0, VB->Count, ~0 );
377
if (VB->ClipOrMask) {
378
tab = VB->Elts ? clip_render_tab_elts : clip_render_tab_verts;
379
clip_render_tab_elts[GL_TRIANGLES] = clip_elt_triangles;
383
tnl->Driver.Render.PrimTabElts :
384
tnl->Driver.Render.PrimTabVerts);
388
GLuint i, length, flags = 0;
389
for (i = 0 ; !(flags & PRIM_END) ; i += length) {
390
flags = VB->Primitive[i].mode;
391
length= VB->Primitive[i].count;
392
ASSERT(length || (flags & PRIM_END));
393
ASSERT((flags & PRIM_MODE_MASK) <= GL_POLYGON+1);
395
tab[flags & PRIM_MODE_MASK]( ctx, i, i + length, flags );
397
} while (tnl->Driver.Render.Multipass &&
398
tnl->Driver.Render.Multipass( ctx, ++pass ));
401
// tnl->Driver.Render.Finish( ctx );
403
_GLD_DX9_VB(Unlock(gldPB->pVB));
405
_GLD_DX9_DEV(SetStreamSource(gld->pDev, 0, gldPB->pVB, 0, gldPB->dwStride));
407
if (gldPB->nPoints) {
408
_GLD_DX9_DEV(DrawPrimitive(gld->pDev, D3DPT_POINTLIST, 0, gldPB->nPoints));
413
_GLD_DX9_DEV(DrawPrimitive(gld->pDev, D3DPT_LINELIST, gldPB->iFirstLine, gldPB->nLines));
417
if (gldPB->nTriangles) {
418
_GLD_DX9_DEV(DrawPrimitive(gld->pDev, D3DPT_TRIANGLELIST, gldPB->iFirstTriangle, gldPB->nTriangles));
419
gldPB->nTriangles = 0;
422
return GL_FALSE; /* finished the pipe */
426
/**********************************************************************/
427
/* Render pipeline stage */
428
/**********************************************************************/
433
const struct tnl_pipeline_stage _gld_mesa_render_stage =
435
"gld_mesa_render_stage",
440
_gld_mesa_render_stage_run /* run */
443
//---------------------------------------------------------------------------