1 /* $Id: gld_vb_mesa_render_dx9.c,v 1.2 2004/07/01 13:14:07 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keithw@valinux.com>
32 * Render whole vertex buffers, including projection of vertices from
33 * clip space and clipping of primitives.
35 * This file makes calls to project vertices and to the point, line
36 * and triangle rasterizers via the function pointers:
38 * context->Driver.Render.*
43 //---------------------------------------------------------------------------
45 #include "dglcontext.h"
49 //---------------------------------------------------------------------------
57 #include "math/m_matrix.h"
58 #include "math/m_xform.h"
60 #include "tnl/t_pipeline.h"
62 /**********************************************************************/
63 /* Clip single primitives */
64 /**********************************************************************/
68 #define NEGATIVE(x) (GET_FLOAT_BITS(x) & (1<<31))
69 #define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
71 #define NEGATIVE(x) (x < 0)
72 #define DIFFERENT_SIGNS(x,y) (x * y <= 0 && x - y != 0)
73 /* Could just use (x*y<0) except for the flatshading requirements.
74 * Maybe there's a better way?
79 #define W(i) coord[i][3]
80 #define Z(i) coord[i][2]
81 #define Y(i) coord[i][1]
82 #define X(i) coord[i][0]
85 #include "tnl/t_vb_cliptmp.h"
89 /**********************************************************************/
90 /* Clip and render whole begin/end objects */
91 /**********************************************************************/
93 #define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
94 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
95 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
98 /* Vertices, with the possibility of clipping.
100 #define RENDER_POINTS( start, count ) \
101 tnl->Driver.Render.Points( ctx, start, count )
103 #define RENDER_LINE( v1, v2 ) \
105 GLubyte c1 = mask[v1], c2 = mask[v2]; \
106 GLubyte ormask = c1|c2; \
108 LineFunc( ctx, v1, v2 ); \
109 else if (!(c1 & c2 & 0x3f)) \
110 clip_line_4( ctx, v1, v2, ormask ); \
113 #define RENDER_TRI( v1, v2, v3 ) \
115 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
116 GLubyte ormask = c1|c2|c3; \
118 TriangleFunc( ctx, v1, v2, v3 ); \
119 else if (!(c1 & c2 & c3 & 0x3f)) \
120 clip_tri_4( ctx, v1, v2, v3, ormask ); \
123 #define RENDER_QUAD( v1, v2, v3, v4 ) \
125 GLubyte c1 = mask[v1], c2 = mask[v2]; \
126 GLubyte c3 = mask[v3], c4 = mask[v4]; \
127 GLubyte ormask = c1|c2|c3|c4; \
129 QuadFunc( ctx, v1, v2, v3, v4 ); \
130 else if (!(c1 & c2 & c3 & c4 & 0x3f)) \
131 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
136 TNLcontext *tnl = TNL_CONTEXT(ctx); \
137 struct vertex_buffer *VB = &tnl->vb; \
138 const GLuint * const elt = VB->Elts; \
139 const GLubyte *mask = VB->ClipMask; \
140 const GLuint sz = VB->ClipPtr->size; \
141 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
142 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
143 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
144 const GLboolean stipple = ctx->Line.StippleFlag; \
145 (void) (LineFunc && TriangleFunc && QuadFunc); \
146 (void) elt; (void) mask; (void) sz; (void) stipple;
148 #define TAG(x) clip_##x##_verts
149 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
150 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
151 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
152 #define PRESERVE_VB_DEFS
153 #include "tnl/t_vb_rendertmp.h"
157 /* Elts, with the possibility of clipping.
161 #define ELT(x) elt[x]
162 #define TAG(x) clip_##x##_elts
163 #include "tnl/t_vb_rendertmp.h"
165 /* TODO: do this for all primitives, verts and elts:
167 static void clip_elt_triangles( GLcontext
*ctx
,
172 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
173 tnl_render_func render_tris
= tnl
->Driver
.Render
.PrimTabElts
[GL_TRIANGLES
];
174 struct vertex_buffer
*VB
= &tnl
->vb
;
175 const GLuint
* const elt
= VB
->Elts
;
176 GLubyte
*mask
= VB
->ClipMask
;
177 GLuint last
= count
-2;
181 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
183 for (j
=start
; j
< last
; j
+=3 ) {
184 GLubyte c1
= mask
[elt
[j
]];
185 GLubyte c2
= mask
[elt
[j
+1]];
186 GLubyte c3
= mask
[elt
[j
+2]];
187 GLubyte ormask
= c1
|c2
|c3
;
190 render_tris( ctx
, start
, j
, 0 );
191 if (!(c1
&c2
&c3
&0x3f))
192 clip_tri_4( ctx
, elt
[j
], elt
[j
+1], elt
[j
+2], ormask
);
198 render_tris( ctx
, start
, j
, 0 );
201 /**********************************************************************/
202 /* Render whole begin/end objects */
203 /**********************************************************************/
205 #define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
206 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
207 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
210 /* Vertices, no clipping.
212 #define RENDER_POINTS( start, count ) \
213 tnl->Driver.Render.Points( ctx, start, count )
215 #define RENDER_LINE( v1, v2 ) \
216 LineFunc( ctx, v1, v2 )
218 #define RENDER_TRI( v1, v2, v3 ) \
219 TriangleFunc( ctx, v1, v2, v3 )
221 #define RENDER_QUAD( v1, v2, v3, v4 ) \
222 QuadFunc( ctx, v1, v2, v3, v4 )
224 #define TAG(x) _gld_tnl_##x##_verts
227 TNLcontext *tnl = TNL_CONTEXT(ctx); \
228 struct vertex_buffer *VB = &tnl->vb; \
229 const GLuint * const elt = VB->Elts; \
230 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
231 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
232 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
233 (void) (LineFunc && TriangleFunc && QuadFunc); \
236 #define RESET_STIPPLE tnl->Driver.Render.ResetLineStipple( ctx )
237 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
238 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
239 #define RENDER_TAB_QUALIFIER
240 #define PRESERVE_VB_DEFS
241 #include "tnl/t_vb_rendertmp.h"
244 /* Elts, no clipping.
247 #define TAG(x) _gld_tnl_##x##_elts
248 #define ELT(x) elt[x]
249 #include "tnl/t_vb_rendertmp.h"
252 /**********************************************************************/
253 /* Helper functions for drivers */
254 /**********************************************************************/
256 void _tnl_RenderClippedPolygon( GLcontext *ctx, const GLuint *elts, GLuint n )
258 TNLcontext *tnl = TNL_CONTEXT(ctx);
259 struct vertex_buffer *VB = &tnl->vb;
260 GLuint *tmp = VB->Elts;
262 VB->Elts = (GLuint *)elts;
263 tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
267 void _tnl_RenderClippedLine( GLcontext *ctx, GLuint ii, GLuint jj )
269 TNLcontext *tnl = TNL_CONTEXT(ctx);
270 tnl->Driver.Render.Line( ctx, ii, jj );
275 /**********************************************************************/
276 /* Clip and render whole vertex buffers */
277 /**********************************************************************/
279 tnl_points_func _gldSetupPoints
[4] = {
285 tnl_line_func _gldSetupLine
[4] = {
287 gld_Line2DSmooth_DX9
,
289 gld_Line2DSmooth_DX9
,
291 tnl_triangle_func _gldSetupTriangle
[4] = {
292 gld_Triangle2DFlat_DX9
,
293 gld_Triangle2DSmooth_DX9
,
294 gld_Triangle2DFlatExtras_DX9
,
295 gld_Triangle2DSmoothExtras_DX9
297 tnl_quad_func _gldSetupQuad
[4] = {
299 gld_Quad2DSmooth_DX9
,
300 gld_Quad2DFlatExtras_DX9
,
301 gld_Quad2DSmoothExtras_DX9
304 //---------------------------------------------------------------------------
306 static GLboolean
_gld_mesa_render_stage_run(
308 struct gl_pipeline_stage
*stage
)
310 GLD_context
*gldCtx
= GLD_GET_CONTEXT(ctx
);
311 GLD_driver_dx9
*gld
= GLD_GET_DX9_DRIVER(gldCtx
);
313 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
314 struct vertex_buffer
*VB
= &tnl
->vb
;
315 GLuint new_inputs
= stage
->changed_inputs
;
316 tnl_render_func
*tab
;
320 /* Allow the drivers to lock before projected verts are built so
321 * that window coordinates are guarenteed not to change before
324 ASSERT(tnl
->Driver
.Render
.Start
);
326 tnl
->Driver
.Render
.Start( ctx
);
328 // NOTE: Setting D3DRS_SOFTWAREVERTEXPROCESSING for a mixed-mode device resets
329 // stream, indices and shader to default values of NULL or 0.
330 /* if ((ctx->_TriangleCaps & DD_TRI_LIGHT_TWOSIDE) &&
331 gld->VStwosidelight.hShader &&
334 IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_SOFTWAREVERTEXPROCESSING, !gld->VStwosidelight.bHardware);
335 _GLD_DX9_DEV(SetVertexShader(gld->pDev, gld->VStwosidelight.hShader));
336 gldPB = &gld->PBtwosidelight;
337 tnl->Driver.Render.Points = gld_Points2DTwoside_DX9;
338 if (ctx->_TriangleCaps & DD_FLATSHADE) {
339 tnl->Driver.Render.Line = gld_Line2DFlatTwoside_DX9;
340 tnl->Driver.Render.Triangle = gld_Triangle2DFlatTwoside_DX9;
341 tnl->Driver.Render.Quad = gld_Quad2DFlatTwoside_DX9;
343 tnl->Driver.Render.Line = gld_Line2DSmoothTwoside_DX9;
344 tnl->Driver.Render.Triangle = gld_Triangle2DSmoothTwoside_DX9;
345 tnl->Driver.Render.Quad = gld_Quad2DSmoothTwoside_DX9;
348 // IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_SOFTWAREVERTEXPROCESSING, TRUE);
349 IDirect3DDevice9_SetSoftwareVertexProcessing(gld
->pDev
, TRUE
);
351 _GLD_DX9_DEV(SetVertexShader(gld
->pDev
, NULL
));
352 _GLD_DX9_DEV(SetFVF(gld
->pDev
, gldPB
->dwFVF
));
353 tnl
->Driver
.Render
.Points
= _gldSetupPoints
[gld
->iSetupFunc
];
354 tnl
->Driver
.Render
.Line
= _gldSetupLine
[gld
->iSetupFunc
];
355 tnl
->Driver
.Render
.Triangle
= _gldSetupTriangle
[gld
->iSetupFunc
];
356 tnl
->Driver
.Render
.Quad
= _gldSetupQuad
[gld
->iSetupFunc
];
359 _GLD_DX9_VB(Lock(gldPB
->pVB
, 0, 0, &gldPB
->pPoints
, D3DLOCK_DISCARD
));
360 gldPB
->nPoints
= gldPB
->nLines
= gldPB
->nTriangles
= 0;
361 // Allocate primitive pointers
362 // gldPB->pPoints is always first
363 gldPB
->pLines
= gldPB
->pPoints
+ (gldPB
->dwStride
* gldPB
->iFirstLine
);
364 gldPB
->pTriangles
= gldPB
->pPoints
+ (gldPB
->dwStride
* gldPB
->iFirstTriangle
);
366 ASSERT(tnl
->Driver
.Render
.BuildVertices
);
367 ASSERT(tnl
->Driver
.Render
.PrimitiveNotify
);
368 ASSERT(tnl
->Driver
.Render
.Points
);
369 ASSERT(tnl
->Driver
.Render
.Line
);
370 ASSERT(tnl
->Driver
.Render
.Triangle
);
371 ASSERT(tnl
->Driver
.Render
.Quad
);
372 ASSERT(tnl
->Driver
.Render
.ResetLineStipple
);
373 ASSERT(tnl
->Driver
.Render
.Interp
);
374 ASSERT(tnl
->Driver
.Render
.CopyPV
);
375 ASSERT(tnl
->Driver
.Render
.ClippedLine
);
376 ASSERT(tnl
->Driver
.Render
.ClippedPolygon
);
377 ASSERT(tnl
->Driver
.Render
.Finish
);
379 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, new_inputs
);
381 if (VB
->ClipOrMask
) {
382 tab
= VB
->Elts
? clip_render_tab_elts
: clip_render_tab_verts
;
383 clip_render_tab_elts
[GL_TRIANGLES
] = clip_elt_triangles
;
387 tnl
->Driver
.Render
.PrimTabElts
:
388 tnl
->Driver
.Render
.PrimTabVerts
);
392 GLuint i
, length
, flags
= 0;
393 for (i
= 0 ; !(flags
& PRIM_LAST
) ; i
+= length
) {
394 flags
= VB
->Primitive
[i
];
395 length
= VB
->PrimitiveLength
[i
];
396 ASSERT(length
|| (flags
& PRIM_LAST
));
397 ASSERT((flags
& PRIM_MODE_MASK
) <= GL_POLYGON
+1);
399 tab
[flags
& PRIM_MODE_MASK
]( ctx
, i
, i
+ length
, flags
);
401 } while (tnl
->Driver
.Render
.Multipass
&&
402 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
405 // tnl->Driver.Render.Finish( ctx );
407 _GLD_DX9_VB(Unlock(gldPB
->pVB
));
409 _GLD_DX9_DEV(SetStreamSource(gld
->pDev
, 0, gldPB
->pVB
, 0, gldPB
->dwStride
));
411 if (gldPB
->nPoints
) {
412 _GLD_DX9_DEV(DrawPrimitive(gld
->pDev
, D3DPT_POINTLIST
, 0, gldPB
->nPoints
));
417 _GLD_DX9_DEV(DrawPrimitive(gld
->pDev
, D3DPT_LINELIST
, gldPB
->iFirstLine
, gldPB
->nLines
));
421 if (gldPB
->nTriangles
) {
422 _GLD_DX9_DEV(DrawPrimitive(gld
->pDev
, D3DPT_TRIANGLELIST
, gldPB
->iFirstTriangle
, gldPB
->nTriangles
));
423 gldPB
->nTriangles
= 0;
426 return GL_FALSE
; /* finished the pipe */
430 /**********************************************************************/
431 /* Render pipeline stage */
432 /**********************************************************************/
436 /* Quite a bit of work involved in finding out the inputs for the
439 static void _gld_mesa_render_stage_check(
441 struct gl_pipeline_stage
*stage
)
443 GLuint inputs
= VERT_BIT_CLIP
;
446 if (ctx
->Visual
.rgbMode
) {
447 inputs
|= VERT_BIT_COLOR0
;
449 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
450 inputs
|= VERT_BIT_COLOR1
; //VERT_BIT_SPEC_RGB;
452 //if (ctx->Texture._ReallyEnabled) {
453 for (i
=0; i
<ctx
->Const
.MaxTextureUnits
; i
++) {
454 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
)
455 inputs
|= VERT_BIT_TEX(i
);
459 inputs
|= VERT_BIT_INDEX
;
462 if (ctx
->Point
._Attenuated
)
463 inputs
|= VERT_BIT_POINT_SIZE
;
465 /* How do drivers turn this off?
467 if (ctx
->Fog
.Enabled
)
468 inputs
|= VERT_BIT_FOG
; // VERT_FOG_COORD;
470 if (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
)
471 inputs
|= VERT_BIT_EDGEFLAG
;
473 if (ctx
->RenderMode
==GL_FEEDBACK
)
474 inputs
|= VERT_BITS_TEX_ANY
;
476 stage
->inputs
= inputs
;
479 //---------------------------------------------------------------------------
482 static void _gld_mesa_render_stage_dtr(
483 struct gl_pipeline_stage
*stage
)
487 //---------------------------------------------------------------------------
489 const struct gl_pipeline_stage _gld_mesa_render_stage
=
491 "gld_mesa_render_stage",
493 _DD_NEW_SEPARATE_SPECULAR
|
499 _DD_NEW_TRI_UNFILLED
|
500 _NEW_RENDERMODE
), // re-check (new inputs, interp function)
501 0, /* re-run (always runs) */
502 GL_TRUE
, /* active */
503 0, 0, /* inputs (set in check_render), outputs */
504 0, 0, /* changed_inputs, private */
505 _gld_mesa_render_stage_dtr
, /* destructor */
506 _gld_mesa_render_stage_check
, /* check */
507 _gld_mesa_render_stage_run
/* run */
510 //---------------------------------------------------------------------------