2 * WINED3D draw functions
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006 Henri Verbeet
9 * Copyright 2007 Stefan Dösinger for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw
);
30 #define GLINFO_LOCATION This->adapter->gl_info
36 extern IWineD3DVertexShaderImpl
* VertexShaders
[64];
37 extern IWineD3DVertexDeclarationImpl
* VertexShaderDeclarations
[64];
38 extern IWineD3DPixelShaderImpl
* PixelShaders
[64];
40 #undef GL_VERSION_1_4 /* To be fixed, caused by mesa headers */
43 /* Issues the glBegin call for gl given the primitive type and count */
44 static DWORD
primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType
,
48 DWORD NumVertexes
= NumPrimitives
;
50 switch (PrimitiveType
) {
51 case WINED3DPT_POINTLIST
:
53 *primType
= GL_POINTS
;
54 NumVertexes
= NumPrimitives
;
57 case WINED3DPT_LINELIST
:
60 NumVertexes
= NumPrimitives
* 2;
63 case WINED3DPT_LINESTRIP
:
64 TRACE("LINE_STRIP\n");
65 *primType
= GL_LINE_STRIP
;
66 NumVertexes
= NumPrimitives
+ 1;
69 case WINED3DPT_TRIANGLELIST
:
71 *primType
= GL_TRIANGLES
;
72 NumVertexes
= NumPrimitives
* 3;
75 case WINED3DPT_TRIANGLESTRIP
:
76 TRACE("TRIANGLE_STRIP\n");
77 *primType
= GL_TRIANGLE_STRIP
;
78 NumVertexes
= NumPrimitives
+ 2;
81 case WINED3DPT_TRIANGLEFAN
:
82 TRACE("TRIANGLE_FAN\n");
83 *primType
= GL_TRIANGLE_FAN
;
84 NumVertexes
= NumPrimitives
+ 2;
88 FIXME("Unhandled primitive\n");
89 *primType
= GL_POINTS
;
95 static BOOL
fixed_get_input(
96 BYTE usage
, BYTE usage_idx
,
97 unsigned int* regnum
) {
101 /* Those positions must have the order in the
102 * named part of the strided data */
104 if ((usage
== WINED3DDECLUSAGE_POSITION
|| usage
== WINED3DDECLUSAGE_POSITIONT
) && usage_idx
== 0)
106 else if (usage
== WINED3DDECLUSAGE_BLENDWEIGHT
&& usage_idx
== 0)
108 else if (usage
== WINED3DDECLUSAGE_BLENDINDICES
&& usage_idx
== 0)
110 else if (usage
== WINED3DDECLUSAGE_NORMAL
&& usage_idx
== 0)
112 else if (usage
== WINED3DDECLUSAGE_PSIZE
&& usage_idx
== 0)
114 else if (usage
== WINED3DDECLUSAGE_COLOR
&& usage_idx
== 0)
116 else if (usage
== WINED3DDECLUSAGE_COLOR
&& usage_idx
== 1)
118 else if (usage
== WINED3DDECLUSAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
119 *regnum
= 7 + usage_idx
;
120 else if ((usage
== WINED3DDECLUSAGE_POSITION
|| usage
== WINED3DDECLUSAGE_POSITIONT
) && usage_idx
== 1)
121 *regnum
= 7 + WINED3DDP_MAXTEXCOORD
;
122 else if (usage
== WINED3DDECLUSAGE_NORMAL
&& usage_idx
== 1)
123 *regnum
= 8 + WINED3DDP_MAXTEXCOORD
;
124 else if (usage
== WINED3DDECLUSAGE_TANGENT
&& usage_idx
== 0)
125 *regnum
= 9 + WINED3DDP_MAXTEXCOORD
;
126 else if (usage
== WINED3DDECLUSAGE_BINORMAL
&& usage_idx
== 0)
127 *regnum
= 10 + WINED3DDP_MAXTEXCOORD
;
128 else if (usage
== WINED3DDECLUSAGE_TESSFACTOR
&& usage_idx
== 0)
129 *regnum
= 11 + WINED3DDP_MAXTEXCOORD
;
130 else if (usage
== WINED3DDECLUSAGE_FOG
&& usage_idx
== 0)
131 *regnum
= 12 + WINED3DDP_MAXTEXCOORD
;
132 else if (usage
== WINED3DDECLUSAGE_DEPTH
&& usage_idx
== 0)
133 *regnum
= 13 + WINED3DDP_MAXTEXCOORD
;
134 else if (usage
== WINED3DDECLUSAGE_SAMPLE
&& usage_idx
== 0)
135 *regnum
= 14 + WINED3DDP_MAXTEXCOORD
;
138 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
139 debug_d3ddeclusage(usage
), usage_idx
);
145 void primitiveDeclarationConvertToStridedData(
146 IWineD3DDevice
*iface
,
147 BOOL useVertexShaderFunction
,
148 WineDirect3DVertexStridedData
*strided
,
151 /* We need to deal with frequency data!*/
154 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
155 IWineD3DVertexDeclarationImpl
* vertexDeclaration
= (IWineD3DVertexDeclarationImpl
*)This
->stateBlock
->vertexDecl
;
157 WINED3DVERTEXELEMENT
*element
;
160 DWORD numPreloadStreams
= This
->stateBlock
->streamIsUP
? 0 : vertexDeclaration
->num_streams
;
161 DWORD
*streams
= vertexDeclaration
->streams
;
163 /* Check for transformed vertices, disable vertex shader if present */
164 strided
->u
.s
.position_transformed
= vertexDeclaration
->position_transformed
;
165 if(vertexDeclaration
->position_transformed
) {
166 useVertexShaderFunction
= FALSE
;
169 /* Translate the declaration into strided data */
170 for (i
= 0 ; i
< vertexDeclaration
->declarationWNumElements
- 1; ++i
) {
175 element
= vertexDeclaration
->pDeclarationWine
+ i
;
176 TRACE("%p Element %p (%d of %d)\n", vertexDeclaration
->pDeclarationWine
,
177 element
, i
+ 1, vertexDeclaration
->declarationWNumElements
- 1);
179 if (This
->stateBlock
->streamSource
[element
->Stream
] == NULL
)
182 stride
= This
->stateBlock
->streamStride
[element
->Stream
];
183 if (This
->stateBlock
->streamIsUP
) {
184 TRACE("Stream is up %d, %p\n", element
->Stream
, This
->stateBlock
->streamSource
[element
->Stream
]);
186 data
= (BYTE
*)This
->stateBlock
->streamSource
[element
->Stream
];
188 TRACE("Stream isn't up %d, %p\n", element
->Stream
, This
->stateBlock
->streamSource
[element
->Stream
]);
189 data
= IWineD3DVertexBufferImpl_GetMemory(This
->stateBlock
->streamSource
[element
->Stream
], 0, &streamVBO
);
191 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
192 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
193 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
194 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
195 * not, drawStridedSlow is needed, including a vertex buffer path.
197 if(This
->stateBlock
->loadBaseVertexIndex
< 0) {
198 WARN("loadBaseVertexIndex is < 0 (%d), not using vbos\n", This
->stateBlock
->loadBaseVertexIndex
);
200 data
= ((IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[element
->Stream
])->resource
.allocatedMemory
;
201 if((UINT_PTR
)data
< -This
->stateBlock
->loadBaseVertexIndex
* stride
) {
202 FIXME("System memory vertex data load offset is negative!\n");
204 } else if(vertexDeclaration
->half_float_conv_needed
) {
205 WARN("Half float vertex data used, but GL_NV_half_float is not supported. Not using vbos\n");
207 data
= ((IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[element
->Stream
])->resource
.allocatedMemory
;
211 if( streamVBO
!= 0) *fixup
= TRUE
;
212 else if(*fixup
&& !useVertexShaderFunction
&&
213 (element
->Usage
== WINED3DDECLUSAGE_COLOR
||
214 element
->Usage
== WINED3DDECLUSAGE_POSITIONT
)) {
215 /* This may be bad with the fixed function pipeline */
216 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
220 data
+= element
->Offset
;
223 TRACE("Offset %d Stream %d UsageIndex %d\n", element
->Offset
, element
->Stream
, element
->UsageIndex
);
225 if (useVertexShaderFunction
)
226 stride_used
= vshader_get_input(This
->stateBlock
->vertexShader
,
227 element
->Usage
, element
->UsageIndex
, &idx
);
229 stride_used
= fixed_get_input(element
->Usage
, element
->UsageIndex
, &idx
);
232 TRACE("Loaded %s array %u [usage=%s, usage_idx=%u, "
233 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
234 useVertexShaderFunction
? "shader": "fixed function", idx
,
235 debug_d3ddeclusage(element
->Usage
), element
->UsageIndex
,
236 element
->Stream
, element
->Offset
, stride
, debug_d3ddecltype(element
->Type
), streamVBO
);
238 strided
->u
.input
[idx
].lpData
= data
;
239 strided
->u
.input
[idx
].dwType
= element
->Type
;
240 strided
->u
.input
[idx
].dwStride
= stride
;
241 strided
->u
.input
[idx
].VBO
= streamVBO
;
242 strided
->u
.input
[idx
].streamNo
= element
->Stream
;
245 /* Now call PreLoad on all the vertex buffers. In the very rare case
246 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
247 * The vertex buffer can now use the strided structure in the device instead of finding its
250 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
253 for(i
=0; i
< numPreloadStreams
; i
++) {
254 IWineD3DVertexBuffer
*vb
= This
->stateBlock
->streamSource
[streams
[i
]];
256 IWineD3DVertexBuffer_PreLoad(vb
);
261 static void drawStridedFast(IWineD3DDevice
*iface
,UINT numberOfVertices
, GLenum glPrimitiveType
,
262 const void *idxData
, short idxSize
, ULONG minIndex
, ULONG startIdx
, ULONG startVertex
) {
263 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
265 if (idxSize
!= 0 /* This crashes sometimes!*/) {
266 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This
, glPrimitiveType
, numberOfVertices
, minIndex
);
267 idxData
= idxData
== (void *)-1 ? NULL
: idxData
;
269 glDrawElements(glPrimitiveType
, numberOfVertices
, idxSize
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
,
270 (const char *)idxData
+(idxSize
* startIdx
));
271 checkGLcall("glDrawElements");
272 #else /* using drawRangeElements may be faster */
274 glDrawRangeElements(glPrimitiveType
, minIndex
, minIndex
+ numberOfVertices
- 1, numberOfVertices
,
275 idxSize
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
,
276 (const char *)idxData
+(idxSize
* startIdx
));
277 checkGLcall("glDrawRangeElements");
282 /* Note first is now zero as we shuffled along earlier */
283 TRACE("(%p) : glDrawArrays(%x, 0, %d)\n", This
, glPrimitiveType
, numberOfVertices
);
284 glDrawArrays(glPrimitiveType
, startVertex
, numberOfVertices
);
285 checkGLcall("glDrawArrays");
293 * Actually draw using the supplied information.
294 * Slower GL version which extracts info about each vertex in turn
297 static void drawStridedSlow(IWineD3DDevice
*iface
, WineDirect3DVertexStridedData
*sd
,
298 UINT NumVertexes
, GLenum glPrimType
,
299 const void *idxData
, short idxSize
, ULONG minIndex
, ULONG startIdx
, ULONG startVertex
) {
301 unsigned int textureNo
= 0;
302 const WORD
*pIdxBufS
= NULL
;
303 const DWORD
*pIdxBufL
= NULL
;
305 DWORD diffuseColor
= 0xFFFFFFFF; /* Diffuse Color */
306 DWORD specularColor
= 0; /* Specular Color */
307 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
308 UINT
*streamOffset
= This
->stateBlock
->streamOffset
;
309 long SkipnStrides
= startVertex
+ This
->stateBlock
->loadBaseVertexIndex
;
310 BOOL pixelShader
= use_ps(This
);
312 BYTE
*texCoords
[WINED3DDP_MAXTEXCOORD
];
313 BYTE
*diffuse
= NULL
, *specular
= NULL
, *normal
= NULL
, *position
= NULL
;
315 TRACE("Using slow vertex array code\n");
317 /* Variable Initialization */
319 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
320 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
321 * idxData will be != NULL
323 if(idxData
== NULL
) {
324 idxData
= ((IWineD3DIndexBufferImpl
*) This
->stateBlock
->pIndexData
)->resource
.allocatedMemory
;
327 if (idxSize
== 2) pIdxBufS
= (const WORD
*) idxData
;
328 else pIdxBufL
= (const DWORD
*) idxData
;
331 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
332 * to the strided Data in the device and might be needed intact on the next draw
334 for (textureNo
= 0; textureNo
< GL_LIMITS(texture_stages
); ++textureNo
) {
335 if(sd
->u
.s
.texCoords
[textureNo
].lpData
) {
336 texCoords
[textureNo
] = sd
->u
.s
.texCoords
[textureNo
].lpData
+ streamOffset
[sd
->u
.s
.texCoords
[textureNo
].streamNo
];
338 texCoords
[textureNo
] = NULL
;
341 if(sd
->u
.s
.diffuse
.lpData
) {
342 diffuse
= sd
->u
.s
.diffuse
.lpData
+ streamOffset
[sd
->u
.s
.diffuse
.streamNo
];
344 if(sd
->u
.s
.specular
.lpData
) {
345 specular
= sd
->u
.s
.specular
.lpData
+ streamOffset
[sd
->u
.s
.specular
.streamNo
];
347 if(sd
->u
.s
.normal
.lpData
) {
348 normal
= sd
->u
.s
.normal
.lpData
+ streamOffset
[sd
->u
.s
.normal
.streamNo
];
350 if(sd
->u
.s
.position
.lpData
) {
351 position
= sd
->u
.s
.position
.lpData
+ streamOffset
[sd
->u
.s
.position
.streamNo
];
354 /* The texture coordinate types are not so easy to map into a common function signature - we're
355 * not using the vector functions here
357 if(FIXME_ON(d3d_draw
)) {
358 for (textureNo
= 0; textureNo
< GL_LIMITS(textures
); ++textureNo
) {
359 DWORD type
= sd
->u
.s
.texCoords
[textureNo
].dwType
;
360 if (sd
->u
.s
.texCoords
[textureNo
].lpData
&&
361 type
!= WINED3DDECLTYPE_FLOAT1
&&
362 type
!= WINED3DDECLTYPE_FLOAT2
&&
363 type
!= WINED3DDECLTYPE_FLOAT3
&&
364 type
!= WINED3DDECLTYPE_FLOAT4
) {
365 FIXME("Implement fixed function texture coordinates from %s\n", debug_d3ddecltype(type
));
368 if(specular
&& This
->stateBlock
->renderState
[WINED3DRS_FOGENABLE
] &&
369 (This
->stateBlock
->renderState
[WINED3DRS_FOGVERTEXMODE
] == WINED3DFOG_NONE
|| sd
->u
.s
.position_transformed
)&&
370 This
->stateBlock
->renderState
[WINED3DRS_FOGTABLEMODE
] == WINED3DFOG_NONE
) {
371 if(GL_SUPPORT(EXT_FOG_COORD
) && sd
->u
.s
.specular
.dwType
!= WINED3DDECLTYPE_D3DCOLOR
) {
372 FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd
->u
.s
.specular
.dwType
));
375 if(This
->activeContext
->num_untracked_materials
&& sd
->u
.s
.diffuse
.dwType
!= WINED3DDECLTYPE_D3DCOLOR
) {
376 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd
->u
.s
.diffuse
.dwType
));
380 /* Start drawing in GL */
381 VTRACE(("glBegin(%x)\n", glPrimType
));
384 /* Default settings for data that is not passed */
385 if (sd
->u
.s
.normal
.lpData
== NULL
) {
388 if(sd
->u
.s
.diffuse
.lpData
== NULL
) {
389 glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
391 if(sd
->u
.s
.specular
.lpData
== NULL
) {
392 if (GL_SUPPORT(EXT_SECONDARY_COLOR
)) {
393 GL_EXTCALL(glSecondaryColor3fEXT
)(0, 0, 0);
397 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
398 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
401 /* For each primitive */
402 for (vx_index
= 0; vx_index
< NumVertexes
; ++vx_index
) {
404 /* Initialize diffuse color */
405 diffuseColor
= 0xFFFFFFFF;
407 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
408 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
411 /* For indexed data, we need to go a few more strides in */
412 if (idxData
!= NULL
) {
414 /* Indexed so work out the number of strides to skip */
416 VTRACE(("Idx for vertex %d = %d\n", vx_index
, pIdxBufS
[startIdx
+vx_index
]));
417 SkipnStrides
= pIdxBufS
[startIdx
+ vx_index
] + This
->stateBlock
->loadBaseVertexIndex
;
419 VTRACE(("Idx for vertex %d = %d\n", vx_index
, pIdxBufL
[startIdx
+vx_index
]));
420 SkipnStrides
= pIdxBufL
[startIdx
+ vx_index
] + This
->stateBlock
->loadBaseVertexIndex
;
424 /* Texture coords --------------------------- */
425 for (textureNo
= 0; textureNo
< GL_LIMITS(texture_stages
); ++textureNo
) {
427 if (!GL_SUPPORT(ARB_MULTITEXTURE
) && textureNo
> 0) {
428 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
432 /* Query tex coords */
433 if (This
->stateBlock
->textures
[textureNo
] != NULL
|| pixelShader
) {
435 int coordIdx
= This
->stateBlock
->textureState
[textureNo
][WINED3DTSS_TEXCOORDINDEX
];
436 int texture_idx
= This
->texUnitMap
[textureNo
];
437 float *ptrToCoords
= NULL
;
438 float s
= 0.0, t
= 0.0, r
= 0.0, q
= 0.0;
441 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo
));
443 } else if (coordIdx
< 0) {
444 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo
, coordIdx
);
448 ptrToCoords
= (float *)(texCoords
[coordIdx
] + (SkipnStrides
* sd
->u
.s
.texCoords
[coordIdx
].dwStride
));
449 if (texCoords
[coordIdx
] == NULL
) {
450 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo
);
451 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
452 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_idx
, 0, 0, 0, 1));
454 glTexCoord4f(0, 0, 0, 1);
458 int coordsToUse
= sd
->u
.s
.texCoords
[coordIdx
].dwType
+ 1; /* 0 == WINED3DDECLTYPE_FLOAT1 etc */
460 if (texture_idx
== -1) continue;
462 /* The coords to supply depend completely on the fvf / vertex shader */
463 switch (coordsToUse
) {
464 case 4: q
= ptrToCoords
[3]; /* drop through */
465 case 3: r
= ptrToCoords
[2]; /* drop through */
466 case 2: t
= ptrToCoords
[1]; /* drop through */
467 case 1: s
= ptrToCoords
[0];
470 switch (coordsToUse
) { /* Supply the provided texture coords */
471 case WINED3DTTFF_COUNT1
:
472 VTRACE(("tex:%d, s=%f\n", textureNo
, s
));
473 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
474 GL_EXTCALL(glMultiTexCoord1fARB(GL_TEXTURE0_ARB
+ texture_idx
, s
));
479 case WINED3DTTFF_COUNT2
:
480 VTRACE(("tex:%d, s=%f, t=%f\n", textureNo
, s
, t
));
481 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
482 GL_EXTCALL(glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ texture_idx
, s
, t
));
487 case WINED3DTTFF_COUNT3
:
488 VTRACE(("tex:%d, s=%f, t=%f, r=%f\n", textureNo
, s
, t
, r
));
489 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
490 GL_EXTCALL(glMultiTexCoord3fARB(GL_TEXTURE0_ARB
+ texture_idx
, s
, t
, r
));
492 glTexCoord3f(s
, t
, r
);
495 case WINED3DTTFF_COUNT4
:
496 VTRACE(("tex:%d, s=%f, t=%f, r=%f, q=%f\n", textureNo
, s
, t
, r
, q
));
497 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
498 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_idx
, s
, t
, r
, q
));
500 glTexCoord4f(s
, t
, r
, q
);
504 FIXME("Should not get here as coordsToUse is two bits only (%x)!\n", coordsToUse
);
508 } /* End of textures */
510 /* Diffuse -------------------------------- */
512 DWORD
*ptrToCoords
= (DWORD
*)(diffuse
+ (SkipnStrides
* sd
->u
.s
.diffuse
.dwStride
));
514 diffuse_funcs
[sd
->u
.s
.diffuse
.dwType
]((void *) ptrToCoords
);
515 if(This
->activeContext
->num_untracked_materials
) {
519 diffuseColor
= ptrToCoords
[0];
520 color
[0] = D3DCOLOR_B_R(diffuseColor
) / 255.0;
521 color
[1] = D3DCOLOR_B_G(diffuseColor
) / 255.0;
522 color
[2] = D3DCOLOR_B_B(diffuseColor
) / 255.0;
523 color
[3] = D3DCOLOR_B_A(diffuseColor
) / 255.0;
525 for(i
= 0; i
< This
->activeContext
->num_untracked_materials
; i
++) {
526 glMaterialfv(GL_FRONT_AND_BACK
, This
->activeContext
->untracked_materials
[i
], color
);
531 /* Specular ------------------------------- */
533 DWORD
*ptrToCoords
= (DWORD
*)(specular
+ (SkipnStrides
* sd
->u
.s
.specular
.dwStride
));
535 /* special case where the fog density is stored in the specular alpha channel */
536 if(This
->stateBlock
->renderState
[WINED3DRS_FOGENABLE
] &&
537 (This
->stateBlock
->renderState
[WINED3DRS_FOGVERTEXMODE
] == WINED3DFOG_NONE
|| sd
->u
.s
.position
.dwType
== WINED3DDECLTYPE_FLOAT4
)&&
538 This
->stateBlock
->renderState
[WINED3DRS_FOGTABLEMODE
] == WINED3DFOG_NONE
) {
539 if(GL_SUPPORT(EXT_FOG_COORD
)) {
540 specularColor
= ptrToCoords
[0];
541 GL_EXTCALL(glFogCoordfEXT(specularColor
>> 24));
543 static BOOL warned
= FALSE
;
545 /* TODO: Use the fog table code from old ddraw */
546 FIXME("Implement fog for transformed vertices in software\n");
552 specular_funcs
[sd
->u
.s
.specular
.dwType
]((void *) ptrToCoords
);
555 /* Normal -------------------------------- */
556 if (normal
!= NULL
) {
557 float *ptrToCoords
= (float *)(normal
+ (SkipnStrides
* sd
->u
.s
.normal
.dwStride
));
558 normal_funcs
[sd
->u
.s
.normal
.dwType
](ptrToCoords
);
561 /* Position -------------------------------- */
563 float *ptrToCoords
= (float *)(position
+ (SkipnStrides
* sd
->u
.s
.position
.dwStride
));
564 position_funcs
[sd
->u
.s
.position
.dwType
](ptrToCoords
);
567 /* For non indexed mode, step onto next parts */
568 if (idxData
== NULL
) {
574 checkGLcall("glEnd and previous calls");
577 /* See GL_NV_half_float for reference */
578 static inline float float_16_to_32(const unsigned short *in
) {
579 const unsigned short s
= ((*in
) & 0x8000);
580 const unsigned short e
= ((*in
) & 0x7C00) >> 10;
581 const unsigned short m
= (*in
) & 0x3FF;
582 const float sgn
= (s
? -1.0 : 1.0);
585 if(m
== 0) return sgn
* 0.0; /* +0.0 or -0.0 */
586 else return sgn
* pow(2, -14.0) * ( (float) m
/ 1024.0);
588 return sgn
* pow(2, (float) e
-15.0) * (1.0 + ((float) m
/ 1024.0));
590 if(m
== 0) return sgn
/ 0.0; /* +INF / -INF */
591 else return 0.0 / 0.0; /* NAN */
595 static inline void send_attribute(IWineD3DDeviceImpl
*This
, const DWORD type
, const UINT index
, const void *ptr
) {
597 case WINED3DDECLTYPE_FLOAT1
:
598 GL_EXTCALL(glVertexAttrib1fvARB(index
, (float *) ptr
));
600 case WINED3DDECLTYPE_FLOAT2
:
601 GL_EXTCALL(glVertexAttrib2fvARB(index
, (float *) ptr
));
603 case WINED3DDECLTYPE_FLOAT3
:
604 GL_EXTCALL(glVertexAttrib3fvARB(index
, (float *) ptr
));
606 case WINED3DDECLTYPE_FLOAT4
:
607 GL_EXTCALL(glVertexAttrib4fvARB(index
, (float *) ptr
));
610 case WINED3DDECLTYPE_UBYTE4
:
611 GL_EXTCALL(glVertexAttrib4ubvARB(index
, ptr
));
613 case WINED3DDECLTYPE_UBYTE4N
:
614 case WINED3DDECLTYPE_D3DCOLOR
:
615 GL_EXTCALL(glVertexAttrib4NubvARB(index
, ptr
));
618 case WINED3DDECLTYPE_SHORT2
:
619 GL_EXTCALL(glVertexAttrib4svARB(index
, (GLshort
*) ptr
));
621 case WINED3DDECLTYPE_SHORT4
:
622 GL_EXTCALL(glVertexAttrib4svARB(index
, (GLshort
*) ptr
));
625 case WINED3DDECLTYPE_SHORT2N
:
627 GLshort s
[4] = {((short *) ptr
)[0], ((short *) ptr
)[1], 0, 1};
628 GL_EXTCALL(glVertexAttrib4NsvARB(index
, s
));
631 case WINED3DDECLTYPE_USHORT2N
:
633 GLushort s
[4] = {((unsigned short *) ptr
)[0], ((unsigned short *) ptr
)[1], 0, 1};
634 GL_EXTCALL(glVertexAttrib4NusvARB(index
, s
));
637 case WINED3DDECLTYPE_SHORT4N
:
638 GL_EXTCALL(glVertexAttrib4NsvARB(index
, (GLshort
*) ptr
));
640 case WINED3DDECLTYPE_USHORT4N
:
641 GL_EXTCALL(glVertexAttrib4NusvARB(index
, (GLushort
*) ptr
));
644 case WINED3DDECLTYPE_UDEC3
:
645 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
646 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
648 case WINED3DDECLTYPE_DEC3N
:
649 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
650 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
653 case WINED3DDECLTYPE_FLOAT16_2
:
654 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
655 * byte float according to the IEEE standard
657 if (GL_SUPPORT(NV_HALF_FLOAT
)) {
658 GL_EXTCALL(glVertexAttrib2hvNV(index
, (GLhalfNV
*)ptr
));
660 float x
= float_16_to_32(((unsigned short *) ptr
) + 0);
661 float y
= float_16_to_32(((unsigned short *) ptr
) + 1);
662 GL_EXTCALL(glVertexAttrib2fARB(index
, x
, y
));
665 case WINED3DDECLTYPE_FLOAT16_4
:
666 if (GL_SUPPORT(NV_HALF_FLOAT
)) {
667 GL_EXTCALL(glVertexAttrib4hvNV(index
, (GLhalfNV
*)ptr
));
669 float x
= float_16_to_32(((unsigned short *) ptr
) + 0);
670 float y
= float_16_to_32(((unsigned short *) ptr
) + 1);
671 float z
= float_16_to_32(((unsigned short *) ptr
) + 2);
672 float w
= float_16_to_32(((unsigned short *) ptr
) + 3);
673 GL_EXTCALL(glVertexAttrib4fARB(index
, x
, y
, z
, w
));
677 case WINED3DDECLTYPE_UNUSED
:
679 ERR("Unexpected attribute declaration: %d\n", type
);
684 static void drawStridedSlowVs(IWineD3DDevice
*iface
, WineDirect3DVertexStridedData
*sd
, UINT numberOfVertices
,
685 GLenum glPrimitiveType
, const void *idxData
, short idxSize
, ULONG minIndex
, ULONG startIdx
,
688 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
689 long SkipnStrides
= startVertex
+ This
->stateBlock
->loadBaseVertexIndex
;
690 const WORD
*pIdxBufS
= NULL
;
691 const DWORD
*pIdxBufL
= NULL
;
694 IWineD3DStateBlockImpl
*stateblock
= (IWineD3DStateBlockImpl
*) This
->stateBlock
;
698 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
699 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
700 * idxData will be != NULL
702 if(idxData
== NULL
) {
703 idxData
= ((IWineD3DIndexBufferImpl
*) stateblock
->pIndexData
)->resource
.allocatedMemory
;
706 if (idxSize
== 2) pIdxBufS
= (const WORD
*) idxData
;
707 else pIdxBufL
= (const DWORD
*) idxData
;
710 /* Start drawing in GL */
711 VTRACE(("glBegin(%x)\n", glPrimType
));
712 glBegin(glPrimitiveType
);
714 for (vx_index
= 0; vx_index
< numberOfVertices
; ++vx_index
) {
715 if (idxData
!= NULL
) {
717 /* Indexed so work out the number of strides to skip */
719 VTRACE(("Idx for vertex %d = %d\n", vx_index
, pIdxBufS
[startIdx
+vx_index
]));
720 SkipnStrides
= pIdxBufS
[startIdx
+ vx_index
] + stateblock
->loadBaseVertexIndex
;
722 VTRACE(("Idx for vertex %d = %d\n", vx_index
, pIdxBufL
[startIdx
+vx_index
]));
723 SkipnStrides
= pIdxBufL
[startIdx
+ vx_index
] + stateblock
->loadBaseVertexIndex
;
727 for(i
= MAX_ATTRIBS
- 1; i
>= 0; i
--) {
728 if(!sd
->u
.input
[i
].lpData
) continue;
730 ptr
= sd
->u
.input
[i
].lpData
+
731 sd
->u
.input
[i
].dwStride
* SkipnStrides
+
732 stateblock
->streamOffset
[sd
->u
.input
[i
].streamNo
];
734 send_attribute(This
, sd
->u
.input
[i
].dwType
, i
, ptr
);
742 static void depth_blt(IWineD3DDevice
*iface
, GLuint texture
) {
743 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
744 GLint old_binding
= 0;
746 glPushAttrib(GL_ENABLE_BIT
| GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT
);
748 glDisable(GL_CULL_FACE
);
750 glDisable(GL_ALPHA_TEST
);
751 glDisable(GL_SCISSOR_TEST
);
752 glDisable(GL_STENCIL_TEST
);
753 glEnable(GL_DEPTH_TEST
);
754 glDepthFunc(GL_ALWAYS
);
755 glBlendFunc(GL_ZERO
, GL_ONE
);
757 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
758 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
759 glBindTexture(GL_TEXTURE_2D
, texture
);
760 glEnable(GL_TEXTURE_2D
);
762 This
->shader_backend
->shader_select_depth_blt(iface
);
764 glBegin(GL_TRIANGLE_STRIP
);
765 glVertex2f(-1.0f
, -1.0f
);
766 glVertex2f(1.0f
, -1.0f
);
767 glVertex2f(-1.0f
, 1.0f
);
768 glVertex2f(1.0f
, 1.0f
);
771 glBindTexture(GL_TEXTURE_2D
, old_binding
);
775 /* Reselect the old shaders. There doesn't seem to be any glPushAttrib bit for arb shaders,
776 * and this seems easier and more efficient than providing the shader backend with a private
777 * storage to read and restore the old shader settings
779 This
->shader_backend
->shader_select(iface
, use_ps(This
), use_vs(This
));
782 static void depth_copy(IWineD3DDevice
*iface
) {
783 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
784 IWineD3DSurfaceImpl
*depth_stencil
= (IWineD3DSurfaceImpl
*)This
->auto_depth_stencil_buffer
;
786 /* Only copy the depth buffer if there is one. */
787 if (!depth_stencil
) return;
789 /* TODO: Make this work for modes other than FBO */
790 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return;
792 if (depth_stencil
->current_renderbuffer
) {
793 FIXME("Not supported with fixed up depth stencil\n");
797 if (This
->render_offscreen
) {
798 static GLuint tmp_texture
= 0;
799 GLint old_binding
= 0;
801 TRACE("Copying onscreen depth buffer to offscreen surface\n");
804 glGenTextures(1, &tmp_texture
);
807 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
808 * directly on the FBO texture. That's because we need to flip. */
809 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0));
810 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
811 glBindTexture(GL_TEXTURE_2D
, tmp_texture
);
812 glCopyTexImage2D(depth_stencil
->glDescription
.target
,
813 depth_stencil
->glDescription
.level
,
814 depth_stencil
->glDescription
.glFormatInternal
,
817 depth_stencil
->currentDesc
.Width
,
818 depth_stencil
->currentDesc
.Height
,
820 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
821 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
822 glTexParameteri(GL_TEXTURE_2D
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_LUMINANCE
);
823 glBindTexture(GL_TEXTURE_2D
, old_binding
);
825 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, This
->fbo
));
826 checkGLcall("glBindFramebuffer()");
827 depth_blt(iface
, tmp_texture
);
828 checkGLcall("depth_blt");
830 TRACE("Copying offscreen surface to onscreen depth buffer\n");
832 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0));
833 checkGLcall("glBindFramebuffer()");
834 depth_blt(iface
, depth_stencil
->glDescription
.textureName
);
835 checkGLcall("depth_blt");
839 static inline void drawStridedInstanced(IWineD3DDevice
*iface
, WineDirect3DVertexStridedData
*sd
, UINT numberOfVertices
,
840 GLenum glPrimitiveType
, const void *idxData
, short idxSize
, ULONG minIndex
,
841 ULONG startIdx
, ULONG startVertex
) {
842 UINT numInstances
= 0;
843 int numInstancedAttribs
= 0, i
, j
;
844 UINT instancedData
[sizeof(sd
->u
.input
) / sizeof(sd
->u
.input
[0]) /* 16 */];
845 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*) iface
;
846 IWineD3DStateBlockImpl
*stateblock
= This
->stateBlock
;
849 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
850 * We don't support this for now
852 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
853 * But the StreamSourceFreq value has a different meaning in that situation.
855 FIXME("Non-indexed instanced drawing is not supported\n");
859 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This
, glPrimitiveType
, numberOfVertices
, minIndex
);
860 idxData
= idxData
== (void *)-1 ? NULL
: idxData
;
862 /* First, figure out how many instances we have to draw */
863 for(i
= 0; i
< MAX_STREAMS
; i
++) {
864 /* Look at all non-instanced streams */
865 if(!(stateblock
->streamFlags
[i
] & WINED3DSTREAMSOURCE_INSTANCEDATA
) &&
866 stateblock
->streamSource
[i
]) {
867 int inst
= stateblock
->streamFreq
[i
];
869 if(numInstances
&& inst
!= numInstances
) {
870 ERR("Two streams specify a different number of instances. Got %d, new is %d\n", numInstances
, inst
);
876 for(i
= 0; i
< sizeof(sd
->u
.input
) / sizeof(sd
->u
.input
[0]); i
++) {
877 if(stateblock
->streamFlags
[sd
->u
.input
[i
].streamNo
] & WINED3DSTREAMSOURCE_INSTANCEDATA
) {
878 instancedData
[numInstancedAttribs
] = i
;
879 numInstancedAttribs
++;
883 /* now draw numInstances instances :-) */
884 for(i
= 0; i
< numInstances
; i
++) {
885 /* Specify the instanced attributes using immediate mode calls */
886 for(j
= 0; j
< numInstancedAttribs
; j
++) {
887 BYTE
*ptr
= sd
->u
.input
[instancedData
[j
]].lpData
+
888 sd
->u
.input
[instancedData
[j
]].dwStride
* i
+
889 stateblock
->streamOffset
[sd
->u
.input
[instancedData
[j
]].streamNo
];
890 if(sd
->u
.input
[instancedData
[j
]].VBO
) {
891 IWineD3DVertexBufferImpl
*vb
= (IWineD3DVertexBufferImpl
*) stateblock
->streamSource
[sd
->u
.input
[instancedData
[j
]].streamNo
];
892 ptr
+= (long) vb
->resource
.allocatedMemory
;
895 send_attribute(This
, sd
->u
.input
[instancedData
[j
]].dwType
, instancedData
[j
], ptr
);
898 glDrawElements(glPrimitiveType
, numberOfVertices
, idxSize
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
,
899 (const char *)idxData
+(idxSize
* startIdx
));
900 checkGLcall("glDrawElements");
904 static inline void remove_vbos(IWineD3DDeviceImpl
*This
, WineDirect3DVertexStridedData
*s
) {
906 IWineD3DVertexBufferImpl
*vb
;
908 if(s
->u
.s
.position
.VBO
) {
909 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.position
.streamNo
];
910 s
->u
.s
.position
.VBO
= 0;
911 s
->u
.s
.position
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.position
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
913 if(s
->u
.s
.blendWeights
.VBO
) {
914 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.blendWeights
.streamNo
];
915 s
->u
.s
.blendWeights
.VBO
= 0;
916 s
->u
.s
.blendWeights
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.blendWeights
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
918 if(s
->u
.s
.blendMatrixIndices
.VBO
) {
919 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.blendMatrixIndices
.streamNo
];
920 s
->u
.s
.blendMatrixIndices
.VBO
= 0;
921 s
->u
.s
.blendMatrixIndices
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.blendMatrixIndices
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
923 if(s
->u
.s
.normal
.VBO
) {
924 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.normal
.streamNo
];
925 s
->u
.s
.normal
.VBO
= 0;
926 s
->u
.s
.normal
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.normal
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
928 if(s
->u
.s
.pSize
.VBO
) {
929 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.pSize
.streamNo
];
930 s
->u
.s
.pSize
.VBO
= 0;
931 s
->u
.s
.pSize
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.pSize
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
933 if(s
->u
.s
.diffuse
.VBO
) {
934 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.diffuse
.streamNo
];
935 s
->u
.s
.diffuse
.VBO
= 0;
936 s
->u
.s
.diffuse
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.diffuse
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
938 if(s
->u
.s
.specular
.VBO
) {
939 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.specular
.streamNo
];
940 s
->u
.s
.specular
.VBO
= 0;
941 s
->u
.s
.specular
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.specular
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
943 for(i
= 0; i
< WINED3DDP_MAXTEXCOORD
; i
++) {
944 if(s
->u
.s
.texCoords
[i
].VBO
) {
945 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.texCoords
[i
].streamNo
];
946 s
->u
.s
.texCoords
[i
].VBO
= 0;
947 s
->u
.s
.texCoords
[i
].lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.texCoords
[i
].lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
950 if(s
->u
.s
.position2
.VBO
) {
951 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.position2
.streamNo
];
952 s
->u
.s
.position2
.VBO
= 0;
953 s
->u
.s
.position2
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.position2
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
955 if(s
->u
.s
.normal2
.VBO
) {
956 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.normal2
.streamNo
];
957 s
->u
.s
.normal2
.VBO
= 0;
958 s
->u
.s
.normal2
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.normal2
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
960 if(s
->u
.s
.tangent
.VBO
) {
961 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.tangent
.streamNo
];
962 s
->u
.s
.tangent
.VBO
= 0;
963 s
->u
.s
.tangent
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.tangent
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
965 if(s
->u
.s
.binormal
.VBO
) {
966 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.binormal
.streamNo
];
967 s
->u
.s
.binormal
.VBO
= 0;
968 s
->u
.s
.binormal
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.binormal
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
970 if(s
->u
.s
.tessFactor
.VBO
) {
971 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.tessFactor
.streamNo
];
972 s
->u
.s
.tessFactor
.VBO
= 0;
973 s
->u
.s
.tessFactor
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.tessFactor
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
976 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.fog
.streamNo
];
978 s
->u
.s
.fog
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.fog
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
980 if(s
->u
.s
.depth
.VBO
) {
981 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.depth
.streamNo
];
982 s
->u
.s
.depth
.VBO
= 0;
983 s
->u
.s
.depth
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.depth
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
985 if(s
->u
.s
.sample
.VBO
) {
986 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[s
->u
.s
.sample
.streamNo
];
987 s
->u
.s
.sample
.VBO
= 0;
988 s
->u
.s
.sample
.lpData
= (BYTE
*) ((unsigned long) s
->u
.s
.sample
.lpData
+ (unsigned long) vb
->resource
.allocatedMemory
);
992 /* Routine common to the draw primitive and draw indexed primitive routines */
993 void drawPrimitive(IWineD3DDevice
*iface
,
997 long StartVertexIndex
,
998 UINT numberOfVertices
,
1001 const void *idxData
,
1004 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
1005 IWineD3DSwapChain
*swapchain
;
1006 IWineD3DBaseTexture
*texture
= NULL
;
1007 IWineD3DSurfaceImpl
*target
;
1010 /* Signals other modules that a drawing is in progress and the stateblock finalized */
1011 This
->isInDraw
= TRUE
;
1013 /* Invalidate the back buffer memory so LockRect will read it the next time */
1014 for(i
= 0; i
< GL_LIMITS(buffers
); i
++) {
1015 target
= (IWineD3DSurfaceImpl
*) This
->render_targets
[i
];
1017 /* TODO: Only do all that if we're going to change anything
1018 * Texture container dirtification does not work quite right yet
1020 if(target
/*&& target->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)*/) {
1025 IWineD3DSurface_GetContainer((IWineD3DSurface
*) target
, &IID_IWineD3DSwapChain
, (void **)&swapchain
);
1027 /* Need the surface in the drawable! */
1028 IWineD3DSurface_LoadLocation((IWineD3DSurface
*) target
, SFLAG_INDRAWABLE
, NULL
);
1030 /* TODO: Move fbo logic to ModifyLocation */
1031 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*) target
, SFLAG_INDRAWABLE
, TRUE
);
1033 /* Onscreen target. Invalidate system memory copy and texture copy */
1034 IWineD3DSwapChain_Release(swapchain
);
1035 } else if(wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) {
1036 /* Non-FBO target: Invalidate system copy, texture copy and dirtify the container */
1037 /* TODO: Move container dirtification to ModifyLocation */
1038 IWineD3DSurface_GetContainer((IWineD3DSurface
*) target
, &IID_IWineD3DBaseTexture
, (void **)&texture
);
1041 IWineD3DBaseTexture_SetDirty(texture
, TRUE
);
1042 IWineD3DTexture_Release(texture
);
1045 /* FBO offscreen target. Texture == Drawable */
1046 target
->Flags
|= SFLAG_INTEXTURE
;
1049 /* Must be an fbo render target */
1050 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*) target
, SFLAG_INDRAWABLE
, TRUE
);
1051 target
->Flags
|= SFLAG_INTEXTURE
;
1056 /* Ok, we will be updating the screen from here onwards so grab the lock */
1058 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
) {
1060 apply_fbo_state(iface
);
1064 ActivateContext(This
, This
->render_targets
[0], CTXUSAGE_DRAWPRIM
);
1067 if (This
->depth_copy_state
== WINED3D_DCS_COPY
) {
1070 This
->depth_copy_state
= WINED3D_DCS_INITIAL
;
1074 BOOL emulation
= FALSE
;
1075 WineDirect3DVertexStridedData
*strided
= &This
->strided_streams
;
1076 WineDirect3DVertexStridedData stridedlcl
;
1077 /* Ok, Work out which primitive is requested and how many vertexes that
1079 UINT calculatedNumberOfindices
= primitiveToGl(PrimitiveType
, NumPrimitives
, &glPrimType
);
1080 if (numberOfVertices
== 0 )
1081 numberOfVertices
= calculatedNumberOfindices
;
1084 if(!This
->strided_streams
.u
.s
.position_transformed
&& This
->activeContext
->num_untracked_materials
&&
1085 This
->stateBlock
->renderState
[WINED3DRS_LIGHTING
]) {
1086 FIXME("Using software emulation because not all material properties could be tracked\n");
1089 else if(This
->activeContext
->fog_coord
&& This
->stateBlock
->renderState
[WINED3DRS_FOGENABLE
]) {
1090 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
1091 * to a float in the vertex buffer
1093 FIXME("Using software emulation because manual fog coordinates are provided\n");
1098 strided
= &stridedlcl
;
1099 memcpy(&stridedlcl
, &This
->strided_streams
, sizeof(stridedlcl
));
1100 remove_vbos(This
, &stridedlcl
);
1104 if (This
->useDrawStridedSlow
|| emulation
) {
1105 /* Immediate mode drawing */
1106 drawStridedSlow(iface
, strided
, calculatedNumberOfindices
,
1107 glPrimType
, idxData
, idxSize
, minIndex
, StartIdx
, StartVertexIndex
);
1108 } else if(This
->instancedDraw
) {
1109 /* Instancing emulation with mixing immediate mode and arrays */
1110 drawStridedInstanced(iface
, &This
->strided_streams
, calculatedNumberOfindices
, glPrimType
,
1111 idxData
, idxSize
, minIndex
, StartIdx
, StartVertexIndex
);
1112 } else if(!((IWineD3DVertexDeclarationImpl
*) This
->stateBlock
->vertexDecl
)->half_float_conv_needed
) {
1113 drawStridedFast(iface
, calculatedNumberOfindices
, glPrimType
,
1114 idxData
, idxSize
, minIndex
, StartIdx
, StartVertexIndex
);
1116 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
1117 drawStridedSlowVs(iface
, strided
, calculatedNumberOfindices
, glPrimType
,
1118 idxData
, idxSize
, minIndex
, StartIdx
, StartVertexIndex
);
1122 /* Finshed updating the screen, restore lock */
1124 TRACE("Done all gl drawing\n");
1127 #ifdef SHOW_FRAME_MAKEUP
1129 static long int primCounter
= 0;
1130 /* NOTE: set primCounter to the value reported by drawprim
1131 before you want to to write frame makeup to /tmp */
1132 if (primCounter
>= 0) {
1133 WINED3DLOCKED_RECT r
;
1135 IWineD3DSurface_LockRect(This
->renderTarget
, &r
, NULL
, WINED3DLOCK_READONLY
);
1136 sprintf(buffer
, "/tmp/backbuffer_%d.tga", primCounter
);
1137 TRACE("Saving screenshot %s\n", buffer
);
1138 IWineD3DSurface_SaveSnapshot(This
->renderTarget
, buffer
);
1139 IWineD3DSurface_UnlockRect(This
->renderTarget
);
1141 #ifdef SHOW_TEXTURE_MAKEUP
1143 IWineD3DSurface
*pSur
;
1145 for (textureNo
= 0; textureNo
< MAX_COMBINED_SAMPLERS
; ++textureNo
) {
1146 if (This
->stateBlock
->textures
[textureNo
] != NULL
) {
1147 sprintf(buffer
, "/tmp/texture_%p_%d_%d.tga", This
->stateBlock
->textures
[textureNo
], primCounter
, textureNo
);
1148 TRACE("Saving texture %s\n", buffer
);
1149 if (IWineD3DBaseTexture_GetType(This
->stateBlock
->textures
[textureNo
]) == WINED3DRTYPE_TEXTURE
) {
1150 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture
*)This
->stateBlock
->textures
[textureNo
], 0, &pSur
);
1151 IWineD3DSurface_SaveSnapshot(pSur
, buffer
);
1152 IWineD3DSurface_Release(pSur
);
1154 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This
->stateBlock
->textures
[textureNo
]));
1161 TRACE("drawprim #%d\n", primCounter
);
1166 /* Control goes back to the device, stateblock values may change again */
1167 This
->isInDraw
= FALSE
;
1170 static void normalize_normal(float *n
) {
1171 float length
= n
[0] * n
[0] + n
[1] * n
[1] + n
[2] * n
[2];
1172 if(length
== 0.0) return;
1173 length
= sqrt(length
);
1174 n
[0] = n
[0] / length
;
1175 n
[1] = n
[1] / length
;
1176 n
[2] = n
[2] / length
;
1179 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
1181 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
1182 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1183 * to chache the patches in a vertex buffer. But more importantly, gl can't bind generated
1184 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1187 * To read back, the opengl feedback mode is used. This creates a proplem because we want
1188 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1189 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1190 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1191 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1193 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1194 * resulting colors back to the normals.
1196 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1197 * does not restore it because normally a draw follows immediately afterwards. The caller is
1198 * responsible of taking care that either the gl states are restored, or the context activated
1199 * for drawing to reset the lastWasBlit flag.
1201 HRESULT
tesselate_rectpatch(IWineD3DDeviceImpl
*This
,
1202 struct WineD3DRectPatch
*patch
) {
1203 unsigned int i
, j
, num_quads
, out_vertex_size
, buffer_size
, d3d_out_vertex_size
;
1204 float max_x
= 0.0, max_y
= 0.0, max_z
= 0.0, neg_z
= 0.0;
1205 WineDirect3DVertexStridedData strided
;
1207 WINED3DRECTPATCH_INFO
*info
= &patch
->RectPatchInfo
;
1209 GLenum feedback_type
;
1210 GLfloat
*feedbuffer
;
1212 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1215 memset(&strided
, 0, sizeof(strided
));
1216 primitiveDeclarationConvertToStridedData((IWineD3DDevice
*) This
, FALSE
, &strided
, NULL
);
1217 if(strided
.u
.s
.position
.VBO
) {
1218 IWineD3DVertexBufferImpl
*vb
;
1219 vb
= (IWineD3DVertexBufferImpl
*) This
->stateBlock
->streamSource
[strided
.u
.s
.position
.streamNo
];
1220 strided
.u
.s
.position
.lpData
= (BYTE
*) ((unsigned long) strided
.u
.s
.position
.lpData
+
1221 (unsigned long) vb
->resource
.allocatedMemory
);
1223 vtxStride
= strided
.u
.s
.position
.dwStride
;
1224 data
= strided
.u
.s
.position
.lpData
+
1225 vtxStride
* info
->Stride
* info
->StartVertexOffsetHeight
+
1226 vtxStride
* info
->StartVertexOffsetWidth
;
1228 /* Not entirely sure about what happens with transformed vertices */
1229 if(strided
.u
.s
.position_transformed
) {
1230 FIXME("Transformed position in rectpatch generation\n");
1232 if(vtxStride
% sizeof(GLfloat
)) {
1233 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1234 * I don't see how the stride could not be a multiple of 4, but make sure
1237 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1239 if(info
->Basis
!= WINED3DBASIS_BEZIER
) {
1240 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info
->Basis
));
1242 if(info
->Degree
!= WINED3DDEGREE_CUBIC
) {
1243 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info
->Degree
));
1246 /* First, get the boundary cube of the input data */
1247 for(j
= 0; j
< info
->Height
; j
++) {
1248 for(i
= 0; i
< info
->Width
; i
++) {
1249 float *v
= (float *) (data
+ vtxStride
* i
+ vtxStride
* info
->Stride
* j
);
1250 if(fabs(v
[0]) > max_x
) max_x
= fabs(v
[0]);
1251 if(fabs(v
[1]) > max_y
) max_y
= fabs(v
[1]);
1252 if(fabs(v
[2]) > max_z
) max_z
= fabs(v
[2]);
1253 if(v
[2] < neg_z
) neg_z
= v
[2];
1257 /* This needs some improvements in the vertex decl code */
1258 FIXME("Cannot find data to generate. Only generating position and normals\n");
1259 patch
->has_normals
= TRUE
;
1260 patch
->has_texcoords
= FALSE
;
1262 /* Simply activate the context for blitting. This disables all the things we don't want and
1263 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1264 * patch (as opposed to normal draws) will most likely need different changes anyway
1266 ActivateContext(This
, This
->lastActiveRenderTarget
, CTXUSAGE_BLIT
);
1269 glMatrixMode(GL_PROJECTION
);
1270 checkGLcall("glMatrixMode(GL_PROJECTION)");
1272 checkGLcall("glLoadIndentity()");
1273 glScalef(1 / (max_x
) , 1 / (max_y
), max_z
== 0 ? 1 : 1 / ( 2 * max_z
));
1274 glTranslatef(0, 0, 0.5);
1275 checkGLcall("glScalef");
1276 glViewport(-max_x
, -max_y
, 2 * (max_x
), 2 * (max_y
));
1277 checkGLcall("glViewport");
1279 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1280 * our feedback buffer parser
1282 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
1283 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1284 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_RENDER(WINED3DRS_FILLMODE
));
1285 if(patch
->has_normals
) {
1286 float black
[4] = {0, 0, 0, 0};
1287 float red
[4] = {1, 0, 0, 0};
1288 float green
[4] = {0, 1, 0, 0};
1289 float blue
[4] = {0, 0, 1, 0};
1290 float white
[4] = {1, 1, 1, 1};
1291 glEnable(GL_LIGHTING
);
1292 checkGLcall("glEnable(GL_LIGHTING)");
1293 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, black
);
1294 checkGLcall("glLightModel for MODEL_AMBIENT");
1295 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_RENDER(WINED3DRS_AMBIENT
));
1297 for(i
= 3; i
< GL_LIMITS(lights
); i
++) {
1298 glDisable(GL_LIGHT0
+ i
);
1299 checkGLcall("glDisable(GL_LIGHT0 + i)");
1300 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_ACTIVELIGHT(i
));
1303 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_ACTIVELIGHT(0));
1304 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, red
);
1305 glLightfv(GL_LIGHT0
, GL_SPECULAR
, black
);
1306 glLightfv(GL_LIGHT0
, GL_AMBIENT
, black
);
1307 glLightfv(GL_LIGHT0
, GL_POSITION
, red
);
1308 glEnable(GL_LIGHT0
);
1309 checkGLcall("Setting up light 1\n");
1310 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_ACTIVELIGHT(1));
1311 glLightfv(GL_LIGHT1
, GL_DIFFUSE
, green
);
1312 glLightfv(GL_LIGHT1
, GL_SPECULAR
, black
);
1313 glLightfv(GL_LIGHT1
, GL_AMBIENT
, black
);
1314 glLightfv(GL_LIGHT1
, GL_POSITION
, green
);
1315 glEnable(GL_LIGHT1
);
1316 checkGLcall("Setting up light 2\n");
1317 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_ACTIVELIGHT(2));
1318 glLightfv(GL_LIGHT2
, GL_DIFFUSE
, blue
);
1319 glLightfv(GL_LIGHT2
, GL_SPECULAR
, black
);
1320 glLightfv(GL_LIGHT2
, GL_AMBIENT
, black
);
1321 glLightfv(GL_LIGHT2
, GL_POSITION
, blue
);
1322 glEnable(GL_LIGHT2
);
1323 checkGLcall("Setting up light 3\n");
1325 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_MATERIAL
);
1326 IWineD3DDeviceImpl_MarkStateDirty(This
, STATE_RENDER(WINED3DRS_COLORVERTEX
));
1327 glDisable(GL_COLOR_MATERIAL
);
1328 glMaterialfv(GL_FRONT_AND_BACK
, GL_EMISSION
, black
);
1329 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, black
);
1330 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, white
);
1331 checkGLcall("Setting up materials\n");
1334 /* Enable the needed maps.
1335 * GL_MAP2_VERTEX_3 is needed for positional data.
1336 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1337 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1339 num_quads
= ceilf(patch
->numSegs
[0]) * ceilf(patch
->numSegs
[1]);
1340 out_vertex_size
= 3 /* position */;
1341 d3d_out_vertex_size
= 3;
1342 glEnable(GL_MAP2_VERTEX_3
);
1343 if(patch
->has_normals
&& patch
->has_texcoords
) {
1344 FIXME("Texcoords not handled yet\n");
1345 feedback_type
= GL_3D_COLOR_TEXTURE
;
1346 out_vertex_size
+= 8;
1347 d3d_out_vertex_size
+= 7;
1348 glEnable(GL_AUTO_NORMAL
);
1349 glEnable(GL_MAP2_TEXTURE_COORD_4
);
1350 } else if(patch
->has_texcoords
) {
1351 FIXME("Texcoords not handled yet\n");
1352 feedback_type
= GL_3D_COLOR_TEXTURE
;
1353 out_vertex_size
+= 7;
1354 d3d_out_vertex_size
+= 4;
1355 glEnable(GL_MAP2_TEXTURE_COORD_4
);
1356 } else if(patch
->has_normals
) {
1357 feedback_type
= GL_3D_COLOR
;
1358 out_vertex_size
+= 4;
1359 d3d_out_vertex_size
+= 3;
1360 glEnable(GL_AUTO_NORMAL
);
1362 feedback_type
= GL_3D
;
1364 checkGLcall("glEnable vertex attrib generation");
1366 buffer_size
= num_quads
* out_vertex_size
* 2 /* triangle list */ * 3 /* verts per tri */
1367 + 4 * num_quads
/* 2 triangle markers per quad + num verts in tri */;
1368 feedbuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buffer_size
* sizeof(float) * 8);
1370 glMap2f(GL_MAP2_VERTEX_3
,
1371 0, 1, vtxStride
/ sizeof(float), info
->Width
,
1372 0, 1, info
->Stride
* vtxStride
/ sizeof(float), info
->Height
,
1374 checkGLcall("glMap2f");
1375 if(patch
->has_texcoords
) {
1376 glMap2f(GL_MAP2_TEXTURE_COORD_4
,
1377 0, 1, vtxStride
/ sizeof(float), info
->Width
,
1378 0, 1, info
->Stride
* vtxStride
/ sizeof(float), info
->Height
,
1380 checkGLcall("glMap2f");
1382 glMapGrid2f(ceilf(patch
->numSegs
[0]), 0.0, 1.0, ceilf(patch
->numSegs
[1]), 0.0, 1.0);
1383 checkGLcall("glMapGrid2f");
1385 glFeedbackBuffer(buffer_size
* 2, feedback_type
, feedbuffer
);
1386 checkGLcall("glFeedbackBuffer");
1387 glRenderMode(GL_FEEDBACK
);
1389 glEvalMesh2(GL_FILL
, 0, ceilf(patch
->numSegs
[0]), 0, ceilf(patch
->numSegs
[1]));
1390 checkGLcall("glEvalMesh2\n");
1392 i
= glRenderMode(GL_RENDER
);
1394 ERR("Feedback failed. Expected %d elements back\n", buffer_size
);
1396 HeapFree(GetProcessHeap(), 0, feedbuffer
);
1397 return WINED3DERR_DRIVERINTERNALERROR
;
1398 } else if(i
!= buffer_size
) {
1399 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size
, i
);
1401 HeapFree(GetProcessHeap(), 0, feedbuffer
);
1402 return WINED3DERR_DRIVERINTERNALERROR
;
1404 TRACE("Got %d elements as expected\n", i
);
1407 HeapFree(GetProcessHeap(), 0, patch
->mem
);
1408 patch
->mem
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, num_quads
* 6 * d3d_out_vertex_size
* sizeof(float) * 8);
1410 for(j
= 0; j
< buffer_size
; j
+= (3 /* num verts */ * out_vertex_size
+ 2 /* tri marker */)) {
1411 if(feedbuffer
[j
] != GL_POLYGON_TOKEN
) {
1412 ERR("Unexpected token: %f\n", feedbuffer
[j
]);
1415 if(feedbuffer
[j
+ 1] != 3) {
1416 ERR("Unexpected polygon: %f corners\n", feedbuffer
[j
+ 1]);
1419 /* Somehow there are different ideas about back / front facing, so fix up the
1422 patch
->mem
[i
+ 0] = feedbuffer
[j
+ out_vertex_size
* 2 + 2]; /* x, triangle 2 */
1423 patch
->mem
[i
+ 1] = feedbuffer
[j
+ out_vertex_size
* 2 + 3]; /* y, triangle 2 */
1424 patch
->mem
[i
+ 2] = (feedbuffer
[j
+ out_vertex_size
* 2 + 4] - 0.5) * 4 * max_z
; /* z, triangle 3 */
1425 if(patch
->has_normals
) {
1426 patch
->mem
[i
+ 3] = feedbuffer
[j
+ out_vertex_size
* 2 + 5];
1427 patch
->mem
[i
+ 4] = feedbuffer
[j
+ out_vertex_size
* 2 + 6];
1428 patch
->mem
[i
+ 5] = feedbuffer
[j
+ out_vertex_size
* 2 + 7];
1430 i
+= d3d_out_vertex_size
;
1432 patch
->mem
[i
+ 0] = feedbuffer
[j
+ out_vertex_size
* 1 + 2]; /* x, triangle 2 */
1433 patch
->mem
[i
+ 1] = feedbuffer
[j
+ out_vertex_size
* 1 + 3]; /* y, triangle 2 */
1434 patch
->mem
[i
+ 2] = (feedbuffer
[j
+ out_vertex_size
* 1 + 4] - 0.5) * 4 * max_z
; /* z, triangle 2 */
1435 if(patch
->has_normals
) {
1436 patch
->mem
[i
+ 3] = feedbuffer
[j
+ out_vertex_size
* 1 + 5];
1437 patch
->mem
[i
+ 4] = feedbuffer
[j
+ out_vertex_size
* 1 + 6];
1438 patch
->mem
[i
+ 5] = feedbuffer
[j
+ out_vertex_size
* 1 + 7];
1440 i
+= d3d_out_vertex_size
;
1442 patch
->mem
[i
+ 0] = feedbuffer
[j
+ out_vertex_size
* 0 + 2]; /* x, triangle 1 */
1443 patch
->mem
[i
+ 1] = feedbuffer
[j
+ out_vertex_size
* 0 + 3]; /* y, triangle 1 */
1444 patch
->mem
[i
+ 2] = (feedbuffer
[j
+ out_vertex_size
* 0 + 4] - 0.5) * 4 * max_z
; /* z, triangle 1 */
1445 if(patch
->has_normals
) {
1446 patch
->mem
[i
+ 3] = feedbuffer
[j
+ out_vertex_size
* 0 + 5];
1447 patch
->mem
[i
+ 4] = feedbuffer
[j
+ out_vertex_size
* 0 + 6];
1448 patch
->mem
[i
+ 5] = feedbuffer
[j
+ out_vertex_size
* 0 + 7];
1450 i
+= d3d_out_vertex_size
;
1453 if(patch
->has_normals
) {
1454 /* Now do the same with reverse light directions */
1455 float x
[4] = {-1, 0, 0, 0};
1456 float y
[4] = { 0, -1, 0, 0};
1457 float z
[4] = { 0, 0, -1, 0};
1458 glLightfv(GL_LIGHT0
, GL_POSITION
, x
);
1459 glLightfv(GL_LIGHT1
, GL_POSITION
, y
);
1460 glLightfv(GL_LIGHT2
, GL_POSITION
, z
);
1461 checkGLcall("Setting up reverse light directions\n");
1463 glRenderMode(GL_FEEDBACK
);
1464 checkGLcall("glRenderMode(GL_FEEDBACK)");
1465 glEvalMesh2(GL_FILL
, 0, ceilf(patch
->numSegs
[0]), 0, ceilf(patch
->numSegs
[1]));
1466 checkGLcall("glEvalMesh2\n");
1467 i
= glRenderMode(GL_RENDER
);
1468 checkGLcall("glRenderMode(GL_RENDER)");
1471 for(j
= 0; j
< buffer_size
; j
+= (3 /* num verts */ * out_vertex_size
+ 2 /* tri marker */)) {
1472 if(feedbuffer
[j
] != GL_POLYGON_TOKEN
) {
1473 ERR("Unexpected token: %f\n", feedbuffer
[j
]);
1476 if(feedbuffer
[j
+ 1] != 3) {
1477 ERR("Unexpected polygon: %f corners\n", feedbuffer
[j
+ 1]);
1480 if(patch
->mem
[i
+ 3] == 0.0)
1481 patch
->mem
[i
+ 3] = -feedbuffer
[j
+ out_vertex_size
* 2 + 5];
1482 if(patch
->mem
[i
+ 4] == 0.0)
1483 patch
->mem
[i
+ 4] = -feedbuffer
[j
+ out_vertex_size
* 2 + 6];
1484 if(patch
->mem
[i
+ 5] == 0.0)
1485 patch
->mem
[i
+ 5] = -feedbuffer
[j
+ out_vertex_size
* 2 + 7];
1486 normalize_normal(patch
->mem
+ i
+ 3);
1487 i
+= d3d_out_vertex_size
;
1489 if(patch
->mem
[i
+ 3] == 0.0)
1490 patch
->mem
[i
+ 3] = -feedbuffer
[j
+ out_vertex_size
* 1 + 5];
1491 if(patch
->mem
[i
+ 4] == 0.0)
1492 patch
->mem
[i
+ 4] = -feedbuffer
[j
+ out_vertex_size
* 1 + 6];
1493 if(patch
->mem
[i
+ 5] == 0.0)
1494 patch
->mem
[i
+ 5] = -feedbuffer
[j
+ out_vertex_size
* 1 + 7];
1495 normalize_normal(patch
->mem
+ i
+ 3);
1496 i
+= d3d_out_vertex_size
;
1498 if(patch
->mem
[i
+ 3] == 0.0)
1499 patch
->mem
[i
+ 3] = -feedbuffer
[j
+ out_vertex_size
* 0 + 5];
1500 if(patch
->mem
[i
+ 4] == 0.0)
1501 patch
->mem
[i
+ 4] = -feedbuffer
[j
+ out_vertex_size
* 0 + 6];
1502 if(patch
->mem
[i
+ 5] == 0.0)
1503 patch
->mem
[i
+ 5] = -feedbuffer
[j
+ out_vertex_size
* 0 + 7];
1504 normalize_normal(patch
->mem
+ i
+ 3);
1505 i
+= d3d_out_vertex_size
;
1509 glDisable(GL_MAP2_VERTEX_3
);
1510 glDisable(GL_AUTO_NORMAL
);
1511 glDisable(GL_MAP2_NORMAL
);
1512 glDisable(GL_MAP2_TEXTURE_COORD_4
);
1513 checkGLcall("glDisable vertex attrib generation");
1516 HeapFree(GetProcessHeap(), 0, feedbuffer
);
1518 vtxStride
= 3 * sizeof(float);
1519 if(patch
->has_normals
) {
1520 vtxStride
+= 3 * sizeof(float);
1522 if(patch
->has_texcoords
) {
1523 vtxStride
+= 4 * sizeof(float);
1525 memset(&patch
->strided
, 0, sizeof(&patch
->strided
));
1526 patch
->strided
.u
.s
.position
.lpData
= (BYTE
*) patch
->mem
;
1527 patch
->strided
.u
.s
.position
.dwStride
= vtxStride
;
1528 patch
->strided
.u
.s
.position
.dwType
= WINED3DDECLTYPE_FLOAT3
;
1529 patch
->strided
.u
.s
.position
.streamNo
= 255;
1531 if(patch
->has_normals
) {
1532 patch
->strided
.u
.s
.normal
.lpData
= (BYTE
*) patch
->mem
+ 3 * sizeof(float) /* pos */;
1533 patch
->strided
.u
.s
.normal
.dwStride
= vtxStride
;
1534 patch
->strided
.u
.s
.normal
.dwType
= WINED3DDECLTYPE_FLOAT3
;
1535 patch
->strided
.u
.s
.normal
.streamNo
= 255;
1537 if(patch
->has_texcoords
) {
1538 patch
->strided
.u
.s
.texCoords
[0].lpData
= (BYTE
*) patch
->mem
+ 3 * sizeof(float) /* pos */;
1539 if(patch
->has_normals
) {
1540 patch
->strided
.u
.s
.texCoords
[0].lpData
+= 3 * sizeof(float);
1542 patch
->strided
.u
.s
.texCoords
[0].dwStride
= vtxStride
;
1543 patch
->strided
.u
.s
.texCoords
[0].dwType
= WINED3DDECLTYPE_FLOAT4
;
1544 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1545 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1548 patch
->strided
.u
.s
.texCoords
[0].streamNo
= MAX_STREAMS
;