Rename the various function types in t_context.h to include a tnl_ prefix.
[mesa.git] / src / mesa / drivers / windows / gldirect / dx9 / gld_vb_mesa_render_dx9.c
1 /* $Id: gld_vb_mesa_render_dx9.c,v 1.2 2004/07/01 13:14:07 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30
31 /*
32 * Render whole vertex buffers, including projection of vertices from
33 * clip space and clipping of primitives.
34 *
35 * This file makes calls to project vertices and to the point, line
36 * and triangle rasterizers via the function pointers:
37 *
38 * context->Driver.Render.*
39 *
40 */
41
42
43 //---------------------------------------------------------------------------
44
45 #include "dglcontext.h"
46 #include "ddlog.h"
47 #include "gld_dx9.h"
48
49 //---------------------------------------------------------------------------
50
51 #include "glheader.h"
52 #include "context.h"
53 #include "macros.h"
54 #include "mtypes.h"
55 #include "mmath.h"
56
57 #include "math/m_matrix.h"
58 #include "math/m_xform.h"
59
60 #include "tnl/t_pipeline.h"
61
62 /**********************************************************************/
63 /* Clip single primitives */
64 /**********************************************************************/
65
66
67 #if defined(USE_IEEE)
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))
70 #else
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?
75 */
76 #endif
77
78
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]
83 #define SIZE 4
84 #define TAG(x) x##_4
85 #include "tnl/t_vb_cliptmp.h"
86
87
88
89 /**********************************************************************/
90 /* Clip and render whole begin/end objects */
91 /**********************************************************************/
92
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
96
97
98 /* Vertices, with the possibility of clipping.
99 */
100 #define RENDER_POINTS( start, count ) \
101 tnl->Driver.Render.Points( ctx, start, count )
102
103 #define RENDER_LINE( v1, v2 ) \
104 do { \
105 GLubyte c1 = mask[v1], c2 = mask[v2]; \
106 GLubyte ormask = c1|c2; \
107 if (!ormask) \
108 LineFunc( ctx, v1, v2 ); \
109 else if (!(c1 & c2 & 0x3f)) \
110 clip_line_4( ctx, v1, v2, ormask ); \
111 } while (0)
112
113 #define RENDER_TRI( v1, v2, v3 ) \
114 do { \
115 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
116 GLubyte ormask = c1|c2|c3; \
117 if (!ormask) \
118 TriangleFunc( ctx, v1, v2, v3 ); \
119 else if (!(c1 & c2 & c3 & 0x3f)) \
120 clip_tri_4( ctx, v1, v2, v3, ormask ); \
121 } while (0)
122
123 #define RENDER_QUAD( v1, v2, v3, v4 ) \
124 do { \
125 GLubyte c1 = mask[v1], c2 = mask[v2]; \
126 GLubyte c3 = mask[v3], c4 = mask[v4]; \
127 GLubyte ormask = c1|c2|c3|c4; \
128 if (!ormask) \
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 ); \
132 } while (0)
133
134
135 #define LOCAL_VARS \
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;
147
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"
154
155
156
157 /* Elts, with the possibility of clipping.
158 */
159 #undef ELT
160 #undef TAG
161 #define ELT(x) elt[x]
162 #define TAG(x) clip_##x##_elts
163 #include "tnl/t_vb_rendertmp.h"
164
165 /* TODO: do this for all primitives, verts and elts:
166 */
167 static void clip_elt_triangles( GLcontext *ctx,
168 GLuint start,
169 GLuint count,
170 GLuint flags )
171 {
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;
178 GLuint j;
179 (void) flags;
180
181 tnl->Driver.Render.PrimitiveNotify( ctx, GL_TRIANGLES );
182
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;
188 if (ormask) {
189 if (start < j)
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 );
193 start = j+3;
194 }
195 }
196
197 if (start < j)
198 render_tris( ctx, start, j, 0 );
199 }
200
201 /**********************************************************************/
202 /* Render whole begin/end objects */
203 /**********************************************************************/
204
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
208
209
210 /* Vertices, no clipping.
211 */
212 #define RENDER_POINTS( start, count ) \
213 tnl->Driver.Render.Points( ctx, start, count )
214
215 #define RENDER_LINE( v1, v2 ) \
216 LineFunc( ctx, v1, v2 )
217
218 #define RENDER_TRI( v1, v2, v3 ) \
219 TriangleFunc( ctx, v1, v2, v3 )
220
221 #define RENDER_QUAD( v1, v2, v3, v4 ) \
222 QuadFunc( ctx, v1, v2, v3, v4 )
223
224 #define TAG(x) _gld_tnl_##x##_verts
225
226 #define LOCAL_VARS \
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); \
234 (void) elt;
235
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"
242
243
244 /* Elts, no clipping.
245 */
246 #undef ELT
247 #define TAG(x) _gld_tnl_##x##_elts
248 #define ELT(x) elt[x]
249 #include "tnl/t_vb_rendertmp.h"
250
251
252 /**********************************************************************/
253 /* Helper functions for drivers */
254 /**********************************************************************/
255 /*
256 void _tnl_RenderClippedPolygon( GLcontext *ctx, const GLuint *elts, GLuint n )
257 {
258 TNLcontext *tnl = TNL_CONTEXT(ctx);
259 struct vertex_buffer *VB = &tnl->vb;
260 GLuint *tmp = VB->Elts;
261
262 VB->Elts = (GLuint *)elts;
263 tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
264 VB->Elts = tmp;
265 }
266
267 void _tnl_RenderClippedLine( GLcontext *ctx, GLuint ii, GLuint jj )
268 {
269 TNLcontext *tnl = TNL_CONTEXT(ctx);
270 tnl->Driver.Render.Line( ctx, ii, jj );
271 }
272 */
273
274
275 /**********************************************************************/
276 /* Clip and render whole vertex buffers */
277 /**********************************************************************/
278
279 tnl_points_func _gldSetupPoints[4] = {
280 gld_Points2D_DX9,
281 gld_Points2D_DX9,
282 gld_Points2D_DX9,
283 gld_Points2D_DX9
284 };
285 tnl_line_func _gldSetupLine[4] = {
286 gld_Line2DFlat_DX9,
287 gld_Line2DSmooth_DX9,
288 gld_Line2DFlat_DX9,
289 gld_Line2DSmooth_DX9,
290 };
291 tnl_triangle_func _gldSetupTriangle[4] = {
292 gld_Triangle2DFlat_DX9,
293 gld_Triangle2DSmooth_DX9,
294 gld_Triangle2DFlatExtras_DX9,
295 gld_Triangle2DSmoothExtras_DX9
296 };
297 tnl_quad_func _gldSetupQuad[4] = {
298 gld_Quad2DFlat_DX9,
299 gld_Quad2DSmooth_DX9,
300 gld_Quad2DFlatExtras_DX9,
301 gld_Quad2DSmoothExtras_DX9
302 };
303
304 //---------------------------------------------------------------------------
305
306 static GLboolean _gld_mesa_render_stage_run(
307 GLcontext *ctx,
308 struct gl_pipeline_stage *stage)
309 {
310 GLD_context *gldCtx = GLD_GET_CONTEXT(ctx);
311 GLD_driver_dx9 *gld = GLD_GET_DX9_DRIVER(gldCtx);
312
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;
317 GLint pass = 0;
318 GLD_pb_dx9 *gldPB;
319
320 /* Allow the drivers to lock before projected verts are built so
321 * that window coordinates are guarenteed not to change before
322 * rendering.
323 */
324 ASSERT(tnl->Driver.Render.Start);
325
326 tnl->Driver.Render.Start( ctx );
327
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 &&
332 !ctx->Fog.Enabled)
333 {
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;
342 } else {
343 tnl->Driver.Render.Line = gld_Line2DSmoothTwoside_DX9;
344 tnl->Driver.Render.Triangle = gld_Triangle2DSmoothTwoside_DX9;
345 tnl->Driver.Render.Quad = gld_Quad2DSmoothTwoside_DX9;
346 }
347 } else {*/
348 // IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_SOFTWAREVERTEXPROCESSING, TRUE);
349 IDirect3DDevice9_SetSoftwareVertexProcessing(gld->pDev, TRUE);
350 gldPB = &gld->PB2d;
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];
357 // }
358
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);
365
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);
378
379 tnl->Driver.Render.BuildVertices( ctx, 0, VB->Count, new_inputs );
380
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;
384 }
385 else {
386 tab = (VB->Elts ?
387 tnl->Driver.Render.PrimTabElts :
388 tnl->Driver.Render.PrimTabVerts);
389 }
390
391 do {
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);
398 if (length)
399 tab[flags & PRIM_MODE_MASK]( ctx, i, i + length, flags );
400 }
401 } while (tnl->Driver.Render.Multipass &&
402 tnl->Driver.Render.Multipass( ctx, ++pass ));
403
404
405 // tnl->Driver.Render.Finish( ctx );
406
407 _GLD_DX9_VB(Unlock(gldPB->pVB));
408
409 _GLD_DX9_DEV(SetStreamSource(gld->pDev, 0, gldPB->pVB, 0, gldPB->dwStride));
410
411 if (gldPB->nPoints) {
412 _GLD_DX9_DEV(DrawPrimitive(gld->pDev, D3DPT_POINTLIST, 0, gldPB->nPoints));
413 gldPB->nPoints = 0;
414 }
415
416 if (gldPB->nLines) {
417 _GLD_DX9_DEV(DrawPrimitive(gld->pDev, D3DPT_LINELIST, gldPB->iFirstLine, gldPB->nLines));
418 gldPB->nLines = 0;
419 }
420
421 if (gldPB->nTriangles) {
422 _GLD_DX9_DEV(DrawPrimitive(gld->pDev, D3DPT_TRIANGLELIST, gldPB->iFirstTriangle, gldPB->nTriangles));
423 gldPB->nTriangles = 0;
424 }
425
426 return GL_FALSE; /* finished the pipe */
427 }
428
429
430 /**********************************************************************/
431 /* Render pipeline stage */
432 /**********************************************************************/
433
434
435
436 /* Quite a bit of work involved in finding out the inputs for the
437 * render stage.
438 */
439 static void _gld_mesa_render_stage_check(
440 GLcontext *ctx,
441 struct gl_pipeline_stage *stage)
442 {
443 GLuint inputs = VERT_BIT_CLIP;
444 GLuint i;
445
446 if (ctx->Visual.rgbMode) {
447 inputs |= VERT_BIT_COLOR0;
448
449 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
450 inputs |= VERT_BIT_COLOR1; //VERT_BIT_SPEC_RGB;
451
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);
456 }
457 //}
458 } else {
459 inputs |= VERT_BIT_INDEX;
460 }
461
462 if (ctx->Point._Attenuated)
463 inputs |= VERT_BIT_POINT_SIZE;
464
465 /* How do drivers turn this off?
466 */
467 if (ctx->Fog.Enabled)
468 inputs |= VERT_BIT_FOG; // VERT_FOG_COORD;
469
470 if (ctx->_TriangleCaps & DD_TRI_UNFILLED)
471 inputs |= VERT_BIT_EDGEFLAG;
472
473 if (ctx->RenderMode==GL_FEEDBACK)
474 inputs |= VERT_BITS_TEX_ANY;
475
476 stage->inputs = inputs;
477 }
478
479 //---------------------------------------------------------------------------
480
481 // Destructor
482 static void _gld_mesa_render_stage_dtr(
483 struct gl_pipeline_stage *stage)
484 {
485 }
486
487 //---------------------------------------------------------------------------
488
489 const struct gl_pipeline_stage _gld_mesa_render_stage =
490 {
491 "gld_mesa_render_stage",
492 (_NEW_BUFFERS |
493 _DD_NEW_SEPARATE_SPECULAR |
494 _DD_NEW_FLATSHADE |
495 _NEW_TEXTURE|
496 _NEW_LIGHT|
497 _NEW_POINT|
498 _NEW_FOG|
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 */
508 };
509
510 //---------------------------------------------------------------------------