push a6a7df41ae272b3b1bf5fcfb544267c1e96afa99
[wine/hacks.git] / dlls / wined3d / drawprim.c
blob2a935723d728f3fd544bd4d2c14e638bfa66efb9
1 /*
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, 2008 Henri Verbeet
9 * Copyright 2007-2008 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
26 #include "config.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION This->adapter->gl_info
32 #include <stdio.h>
33 #include <math.h>
35 /* Issues the glBegin call for gl given the primitive type and count */
36 static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
37 DWORD NumPrimitives,
38 GLenum *primType)
40 DWORD NumVertexes = NumPrimitives;
42 switch (PrimitiveType) {
43 case WINED3DPT_POINTLIST:
44 TRACE("POINTS\n");
45 *primType = GL_POINTS;
46 NumVertexes = NumPrimitives;
47 break;
49 case WINED3DPT_LINELIST:
50 TRACE("LINES\n");
51 *primType = GL_LINES;
52 NumVertexes = NumPrimitives * 2;
53 break;
55 case WINED3DPT_LINESTRIP:
56 TRACE("LINE_STRIP\n");
57 *primType = GL_LINE_STRIP;
58 NumVertexes = NumPrimitives + 1;
59 break;
61 case WINED3DPT_TRIANGLELIST:
62 TRACE("TRIANGLES\n");
63 *primType = GL_TRIANGLES;
64 NumVertexes = NumPrimitives * 3;
65 break;
67 case WINED3DPT_TRIANGLESTRIP:
68 TRACE("TRIANGLE_STRIP\n");
69 *primType = GL_TRIANGLE_STRIP;
70 NumVertexes = NumPrimitives + 2;
71 break;
73 case WINED3DPT_TRIANGLEFAN:
74 TRACE("TRIANGLE_FAN\n");
75 *primType = GL_TRIANGLE_FAN;
76 NumVertexes = NumPrimitives + 2;
77 break;
79 default:
80 FIXME("Unhandled primitive\n");
81 *primType = GL_POINTS;
82 break;
84 return NumVertexes;
87 static BOOL fixed_get_input(
88 BYTE usage, BYTE usage_idx,
89 unsigned int* regnum) {
91 *regnum = -1;
93 /* Those positions must have the order in the
94 * named part of the strided data */
96 if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 0)
97 *regnum = 0;
98 else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
99 *regnum = 1;
100 else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
101 *regnum = 2;
102 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
103 *regnum = 3;
104 else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
105 *regnum = 4;
106 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
107 *regnum = 5;
108 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
109 *regnum = 6;
110 else if (usage == WINED3DDECLUSAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
111 *regnum = 7 + usage_idx;
112 else if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 1)
113 *regnum = 7 + WINED3DDP_MAXTEXCOORD;
114 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 1)
115 *regnum = 8 + WINED3DDP_MAXTEXCOORD;
116 else if (usage == WINED3DDECLUSAGE_TANGENT && usage_idx == 0)
117 *regnum = 9 + WINED3DDP_MAXTEXCOORD;
118 else if (usage == WINED3DDECLUSAGE_BINORMAL && usage_idx == 0)
119 *regnum = 10 + WINED3DDP_MAXTEXCOORD;
120 else if (usage == WINED3DDECLUSAGE_TESSFACTOR && usage_idx == 0)
121 *regnum = 11 + WINED3DDP_MAXTEXCOORD;
122 else if (usage == WINED3DDECLUSAGE_FOG && usage_idx == 0)
123 *regnum = 12 + WINED3DDP_MAXTEXCOORD;
124 else if (usage == WINED3DDECLUSAGE_DEPTH && usage_idx == 0)
125 *regnum = 13 + WINED3DDP_MAXTEXCOORD;
126 else if (usage == WINED3DDECLUSAGE_SAMPLE && usage_idx == 0)
127 *regnum = 14 + WINED3DDP_MAXTEXCOORD;
129 if (*regnum == -1) {
130 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
131 debug_d3ddeclusage(usage), usage_idx);
132 return FALSE;
134 return TRUE;
137 void primitiveDeclarationConvertToStridedData(
138 IWineD3DDevice *iface,
139 BOOL useVertexShaderFunction,
140 WineDirect3DVertexStridedData *strided,
141 BOOL *fixup) {
143 /* We need to deal with frequency data!*/
145 BYTE *data = NULL;
146 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
147 IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
148 int i;
149 WINED3DVERTEXELEMENT *element;
150 DWORD stride;
151 DWORD numPreloadStreams = This->stateBlock->streamIsUP ? 0 : vertexDeclaration->num_streams;
152 DWORD *streams = vertexDeclaration->streams;
154 /* Check for transformed vertices, disable vertex shader if present */
155 strided->u.s.position_transformed = vertexDeclaration->position_transformed;
156 if(vertexDeclaration->position_transformed) {
157 useVertexShaderFunction = FALSE;
160 /* Translate the declaration into strided data */
161 for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
162 GLint streamVBO = 0;
163 BOOL stride_used;
164 unsigned int idx;
166 element = vertexDeclaration->pDeclarationWine + i;
167 TRACE("%p Element %p (%d of %d)\n", vertexDeclaration->pDeclarationWine,
168 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
170 if (This->stateBlock->streamSource[element->Stream] == NULL)
171 continue;
173 stride = This->stateBlock->streamStride[element->Stream];
174 if (This->stateBlock->streamIsUP) {
175 TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
176 streamVBO = 0;
177 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
178 } else {
179 TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
180 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
182 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
183 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
184 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
185 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
186 * not, drawStridedSlow is needed, including a vertex buffer path.
188 if(This->stateBlock->loadBaseVertexIndex < 0) {
189 WARN("loadBaseVertexIndex is < 0 (%d), not using vbos\n", This->stateBlock->loadBaseVertexIndex);
190 streamVBO = 0;
191 data = ((IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[element->Stream])->resource.allocatedMemory;
192 if((UINT_PTR)data < -This->stateBlock->loadBaseVertexIndex * stride) {
193 FIXME("System memory vertex data load offset is negative!\n");
197 if(fixup) {
198 if( streamVBO != 0) *fixup = TRUE;
199 else if(*fixup && !useVertexShaderFunction &&
200 (element->Usage == WINED3DDECLUSAGE_COLOR ||
201 element->Usage == WINED3DDECLUSAGE_POSITIONT)) {
202 static BOOL warned = FALSE;
203 if(!warned) {
204 /* This may be bad with the fixed function pipeline */
205 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
206 warned = TRUE;
211 data += element->Offset;
213 TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
215 if (useVertexShaderFunction)
216 stride_used = vshader_get_input(This->stateBlock->vertexShader,
217 element->Usage, element->UsageIndex, &idx);
218 else
219 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
221 if (stride_used) {
222 TRACE("Load %s array %u [usage=%s, usage_idx=%u, "
223 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
224 useVertexShaderFunction? "shader": "fixed function", idx,
225 debug_d3ddeclusage(element->Usage), element->UsageIndex,
226 element->Stream, element->Offset, stride, debug_d3ddecltype(element->Type), streamVBO);
228 if (!useVertexShaderFunction && !vertexDeclaration->ffp_valid[i]) {
229 WARN("Skipping unsupported fixed function element of type %s and usage %s\n",
230 debug_d3ddecltype(element->Type), debug_d3ddeclusage(element->Usage));
231 } else {
232 strided->u.input[idx].lpData = data;
233 strided->u.input[idx].dwType = element->Type;
234 strided->u.input[idx].dwStride = stride;
235 strided->u.input[idx].VBO = streamVBO;
236 strided->u.input[idx].streamNo = element->Stream;
240 /* Now call PreLoad on all the vertex buffers. In the very rare case
241 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
242 * The vertex buffer can now use the strided structure in the device instead of finding its
243 * own again.
245 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
246 * once in there.
248 for(i=0; i < numPreloadStreams; i++) {
249 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
250 if(vb) {
251 IWineD3DVertexBuffer_PreLoad(vb);
256 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
257 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
258 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
260 if (idxSize != 0 /* This crashes sometimes!*/) {
261 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
262 idxData = idxData == (void *)-1 ? NULL : idxData;
263 #if 1
264 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
265 (const char *)idxData+(idxSize * startIdx));
266 checkGLcall("glDrawElements");
267 #else /* using drawRangeElements may be faster */
269 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
270 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
271 (const char *)idxData+(idxSize * startIdx));
272 checkGLcall("glDrawRangeElements");
273 #endif
275 } else {
276 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, glPrimitiveType, startVertex, numberOfVertices);
277 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
278 checkGLcall("glDrawArrays");
281 return;
285 * Actually draw using the supplied information.
286 * Slower GL version which extracts info about each vertex in turn
289 static void drawStridedSlow(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd,
290 UINT NumVertexes, GLenum glPrimType,
291 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
293 unsigned int textureNo = 0;
294 const WORD *pIdxBufS = NULL;
295 const DWORD *pIdxBufL = NULL;
296 LONG vx_index;
297 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
298 UINT *streamOffset = This->stateBlock->streamOffset;
299 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
300 BOOL pixelShader = use_ps(This);
302 BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
303 BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
305 TRACE("Using slow vertex array code\n");
307 /* Variable Initialization */
308 if (idxSize != 0) {
309 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
310 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
311 * idxData will be != NULL
313 if(idxData == NULL) {
314 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
317 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
318 else pIdxBufL = (const DWORD *) idxData;
319 } else if (idxData) {
320 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
321 return;
324 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
325 * to the strided Data in the device and might be needed intact on the next draw
327 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
328 if(sd->u.s.texCoords[textureNo].lpData) {
329 texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
330 } else {
331 texCoords[textureNo] = NULL;
334 if(sd->u.s.diffuse.lpData) {
335 diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
337 if(sd->u.s.specular.lpData) {
338 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
340 if(sd->u.s.normal.lpData) {
341 normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
343 if(sd->u.s.position.lpData) {
344 position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
347 if(FIXME_ON(d3d_draw)) {
348 if(specular && This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
349 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position_transformed )&&
350 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
351 if(GL_SUPPORT(EXT_FOG_COORD) && sd->u.s.specular.dwType != WINED3DDECLTYPE_D3DCOLOR) {
352 FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
355 if(This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR) {
356 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
360 /* Start drawing in GL */
361 VTRACE(("glBegin(%x)\n", glPrimType));
362 glBegin(glPrimType);
364 /* Default settings for data that is not passed */
365 if (sd->u.s.normal.lpData == NULL) {
366 glNormal3f(0, 0, 0);
368 if(sd->u.s.diffuse.lpData == NULL) {
369 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
371 if(sd->u.s.specular.lpData == NULL) {
372 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
373 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
377 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
378 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
381 /* For each primitive */
382 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
383 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
384 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
387 /* For indexed data, we need to go a few more strides in */
388 if (idxData != NULL) {
390 /* Indexed so work out the number of strides to skip */
391 if (idxSize == 2) {
392 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
393 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
394 } else {
395 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
396 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
400 /* Texture coords --------------------------- */
401 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
403 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
404 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
405 continue ;
408 /* Query tex coords */
409 if (This->stateBlock->textures[textureNo] != NULL || pixelShader) {
410 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
411 int texture_idx = This->texUnitMap[textureNo];
412 void *ptrToCoords;
414 if (coordIdx > 7) {
415 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
416 continue;
417 } else if (coordIdx < 0) {
418 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
419 continue;
422 if (texture_idx == -1) continue;
424 if (texCoords[coordIdx] == NULL) {
425 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
426 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
427 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
428 } else {
429 glTexCoord4f(0, 0, 0, 1);
431 continue;
434 ptrToCoords = texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride);
435 if (GL_SUPPORT(ARB_MULTITEXTURE))
436 multi_texcoord_funcs[sd->u.s.texCoords[coordIdx].dwType](GL_TEXTURE0_ARB + texture_idx, ptrToCoords);
437 else
438 texcoord_funcs[sd->u.s.texCoords[coordIdx].dwType](ptrToCoords);
440 } /* End of textures */
442 /* Diffuse -------------------------------- */
443 if (diffuse) {
444 DWORD *ptrToCoords = (DWORD *)(diffuse + (SkipnStrides * sd->u.s.diffuse.dwStride));
446 diffuse_funcs[sd->u.s.diffuse.dwType]((void *) ptrToCoords);
447 if(This->activeContext->num_untracked_materials) {
448 DWORD diffuseColor = ptrToCoords[0];
449 unsigned char i;
450 float color[4];
452 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
453 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
454 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
455 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
457 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
458 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
463 /* Specular ------------------------------- */
464 if (specular) {
465 DWORD *ptrToCoords = (DWORD *)(specular + (SkipnStrides * sd->u.s.specular.dwStride));
467 /* special case where the fog density is stored in the specular alpha channel */
468 if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
469 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
470 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
471 if(GL_SUPPORT(EXT_FOG_COORD)) {
472 DWORD specularColor = ptrToCoords[0];
473 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
474 } else {
475 static BOOL warned = FALSE;
476 if(!warned) {
477 /* TODO: Use the fog table code from old ddraw */
478 FIXME("Implement fog for transformed vertices in software\n");
479 warned = TRUE;
484 specular_funcs[sd->u.s.specular.dwType]((void *) ptrToCoords);
487 /* Normal -------------------------------- */
488 if (normal != NULL) {
489 float *ptrToCoords = (float *)(normal + (SkipnStrides * sd->u.s.normal.dwStride));
490 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
493 /* Position -------------------------------- */
494 if (position) {
495 float *ptrToCoords = (float *)(position + (SkipnStrides * sd->u.s.position.dwStride));
496 position_funcs[sd->u.s.position.dwType](ptrToCoords);
499 /* For non indexed mode, step onto next parts */
500 if (idxData == NULL) {
501 ++SkipnStrides;
505 glEnd();
506 checkGLcall("glEnd and previous calls");
509 static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
510 switch(type) {
511 case WINED3DDECLTYPE_FLOAT1:
512 GL_EXTCALL(glVertexAttrib1fvARB(index, (float *) ptr));
513 break;
514 case WINED3DDECLTYPE_FLOAT2:
515 GL_EXTCALL(glVertexAttrib2fvARB(index, (float *) ptr));
516 break;
517 case WINED3DDECLTYPE_FLOAT3:
518 GL_EXTCALL(glVertexAttrib3fvARB(index, (float *) ptr));
519 break;
520 case WINED3DDECLTYPE_FLOAT4:
521 GL_EXTCALL(glVertexAttrib4fvARB(index, (float *) ptr));
522 break;
524 case WINED3DDECLTYPE_UBYTE4:
525 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
526 break;
527 case WINED3DDECLTYPE_UBYTE4N:
528 case WINED3DDECLTYPE_D3DCOLOR:
529 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
530 break;
532 case WINED3DDECLTYPE_SHORT2:
533 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
534 break;
535 case WINED3DDECLTYPE_SHORT4:
536 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
537 break;
539 case WINED3DDECLTYPE_SHORT2N:
541 GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
542 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
543 break;
545 case WINED3DDECLTYPE_USHORT2N:
547 GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
548 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
549 break;
551 case WINED3DDECLTYPE_SHORT4N:
552 GL_EXTCALL(glVertexAttrib4NsvARB(index, (GLshort *) ptr));
553 break;
554 case WINED3DDECLTYPE_USHORT4N:
555 GL_EXTCALL(glVertexAttrib4NusvARB(index, (GLushort *) ptr));
556 break;
558 case WINED3DDECLTYPE_UDEC3:
559 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
560 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
561 break;
562 case WINED3DDECLTYPE_DEC3N:
563 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
564 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
565 break;
567 case WINED3DDECLTYPE_FLOAT16_2:
568 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
569 * byte float according to the IEEE standard
571 if (GL_SUPPORT(NV_HALF_FLOAT)) {
572 GL_EXTCALL(glVertexAttrib2hvNV(index, (GLhalfNV *)ptr));
573 } else {
574 float x = float_16_to_32(((unsigned short *) ptr) + 0);
575 float y = float_16_to_32(((unsigned short *) ptr) + 1);
576 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
578 break;
579 case WINED3DDECLTYPE_FLOAT16_4:
580 if (GL_SUPPORT(NV_HALF_FLOAT)) {
581 GL_EXTCALL(glVertexAttrib4hvNV(index, (GLhalfNV *)ptr));
582 } else {
583 float x = float_16_to_32(((unsigned short *) ptr) + 0);
584 float y = float_16_to_32(((unsigned short *) ptr) + 1);
585 float z = float_16_to_32(((unsigned short *) ptr) + 2);
586 float w = float_16_to_32(((unsigned short *) ptr) + 3);
587 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
589 break;
591 case WINED3DDECLTYPE_UNUSED:
592 default:
593 ERR("Unexpected attribute declaration: %d\n", type);
594 break;
598 static void drawStridedSlowVs(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
599 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx,
600 ULONG startVertex) {
602 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
603 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
604 const WORD *pIdxBufS = NULL;
605 const DWORD *pIdxBufL = NULL;
606 LONG vx_index;
607 int i;
608 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
609 BYTE *ptr;
611 if (idxSize != 0) {
612 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
613 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
614 * idxData will be != NULL
616 if(idxData == NULL) {
617 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
620 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
621 else pIdxBufL = (const DWORD *) idxData;
624 /* Start drawing in GL */
625 VTRACE(("glBegin(%x)\n", glPrimitiveType));
626 glBegin(glPrimitiveType);
628 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
629 if (idxData != NULL) {
631 /* Indexed so work out the number of strides to skip */
632 if (idxSize == 2) {
633 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
634 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
635 } else {
636 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
637 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
641 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
642 if(!sd->u.input[i].lpData) continue;
644 ptr = sd->u.input[i].lpData +
645 sd->u.input[i].dwStride * SkipnStrides +
646 stateblock->streamOffset[sd->u.input[i].streamNo];
648 send_attribute(This, sd->u.input[i].dwType, i, ptr);
650 SkipnStrides++;
653 glEnd();
656 void depth_blt(IWineD3DDevice *iface, GLuint texture, GLsizei w, GLsizei h) {
657 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
658 GLint old_binding = 0;
660 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
662 glDisable(GL_CULL_FACE);
663 glEnable(GL_BLEND);
664 glDisable(GL_ALPHA_TEST);
665 glDisable(GL_SCISSOR_TEST);
666 glDisable(GL_STENCIL_TEST);
667 glEnable(GL_DEPTH_TEST);
668 glDepthFunc(GL_ALWAYS);
669 glDepthMask(GL_TRUE);
670 glBlendFunc(GL_ZERO, GL_ONE);
671 glViewport(0, 0, w, h);
673 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
674 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
675 glBindTexture(GL_TEXTURE_2D, texture);
677 This->shader_backend->shader_select_depth_blt(iface);
679 glBegin(GL_TRIANGLE_STRIP);
680 glVertex2f(-1.0f, -1.0f);
681 glVertex2f(1.0f, -1.0f);
682 glVertex2f(-1.0f, 1.0f);
683 glVertex2f(1.0f, 1.0f);
684 glEnd();
686 glBindTexture(GL_TEXTURE_2D, old_binding);
688 glPopAttrib();
690 This->shader_backend->shader_deselect_depth_blt(iface);
693 static inline void drawStridedInstanced(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
694 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
695 ULONG startIdx, ULONG startVertex) {
696 UINT numInstances = 0;
697 int numInstancedAttribs = 0, i, j;
698 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
699 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
700 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
702 if (idxSize == 0) {
703 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
704 * We don't support this for now
706 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
707 * But the StreamSourceFreq value has a different meaning in that situation.
709 FIXME("Non-indexed instanced drawing is not supported\n");
710 return;
713 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
714 idxData = idxData == (void *)-1 ? NULL : idxData;
716 /* First, figure out how many instances we have to draw */
717 for(i = 0; i < MAX_STREAMS; i++) {
718 /* Look at the streams and take the first one which matches */
719 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
720 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
721 if(stateblock->streamFreq[i] == 0){
722 numInstances = 1;
723 } else {
724 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
726 break; /* break, because only the first suitable value is interesting */
730 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
731 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
732 instancedData[numInstancedAttribs] = i;
733 numInstancedAttribs++;
737 /* now draw numInstances instances :-) */
738 for(i = 0; i < numInstances; i++) {
739 /* Specify the instanced attributes using immediate mode calls */
740 for(j = 0; j < numInstancedAttribs; j++) {
741 BYTE *ptr = sd->u.input[instancedData[j]].lpData +
742 sd->u.input[instancedData[j]].dwStride * i +
743 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
744 if(sd->u.input[instancedData[j]].VBO) {
745 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
746 ptr += (long) vb->resource.allocatedMemory;
749 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
752 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
753 (const char *)idxData+(idxSize * startIdx));
754 checkGLcall("glDrawElements");
758 static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
759 unsigned char i;
760 IWineD3DVertexBufferImpl *vb;
762 if(s->u.s.position.VBO) {
763 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
764 s->u.s.position.VBO = 0;
765 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
767 if(s->u.s.blendWeights.VBO) {
768 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
769 s->u.s.blendWeights.VBO = 0;
770 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
772 if(s->u.s.blendMatrixIndices.VBO) {
773 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
774 s->u.s.blendMatrixIndices.VBO = 0;
775 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
777 if(s->u.s.normal.VBO) {
778 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
779 s->u.s.normal.VBO = 0;
780 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
782 if(s->u.s.pSize.VBO) {
783 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
784 s->u.s.pSize.VBO = 0;
785 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
787 if(s->u.s.diffuse.VBO) {
788 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
789 s->u.s.diffuse.VBO = 0;
790 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
792 if(s->u.s.specular.VBO) {
793 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
794 s->u.s.specular.VBO = 0;
795 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
797 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
798 if(s->u.s.texCoords[i].VBO) {
799 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
800 s->u.s.texCoords[i].VBO = 0;
801 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
804 if(s->u.s.position2.VBO) {
805 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
806 s->u.s.position2.VBO = 0;
807 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
809 if(s->u.s.normal2.VBO) {
810 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
811 s->u.s.normal2.VBO = 0;
812 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
814 if(s->u.s.tangent.VBO) {
815 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
816 s->u.s.tangent.VBO = 0;
817 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
819 if(s->u.s.binormal.VBO) {
820 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
821 s->u.s.binormal.VBO = 0;
822 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
824 if(s->u.s.tessFactor.VBO) {
825 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
826 s->u.s.tessFactor.VBO = 0;
827 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
829 if(s->u.s.fog.VBO) {
830 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
831 s->u.s.fog.VBO = 0;
832 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
834 if(s->u.s.depth.VBO) {
835 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
836 s->u.s.depth.VBO = 0;
837 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
839 if(s->u.s.sample.VBO) {
840 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
841 s->u.s.sample.VBO = 0;
842 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
846 /* Routine common to the draw primitive and draw indexed primitive routines */
847 void drawPrimitive(IWineD3DDevice *iface,
848 int PrimitiveType,
849 long NumPrimitives,
850 /* for Indexed: */
851 long StartVertexIndex,
852 UINT numberOfVertices,
853 long StartIdx,
854 short idxSize,
855 const void *idxData,
856 int minIndex) {
858 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
859 IWineD3DSurfaceImpl *target;
860 int i;
862 if (NumPrimitives == 0) return;
864 /* Invalidate the back buffer memory so LockRect will read it the next time */
865 for(i = 0; i < GL_LIMITS(buffers); i++) {
866 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
867 if (target) {
868 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
869 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
873 /* Signals other modules that a drawing is in progress and the stateblock finalized */
874 This->isInDraw = TRUE;
876 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
878 if (This->stencilBufferTarget) {
879 /* Note that this depends on the ActivateContext call above to set
880 * This->render_offscreen properly */
881 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
882 surface_load_ds_location(This->stencilBufferTarget, location);
883 surface_modify_ds_location(This->stencilBufferTarget, location);
886 /* Ok, we will be updating the screen from here onwards so grab the lock */
887 ENTER_GL();
889 GLenum glPrimType;
890 BOOL emulation = FALSE;
891 WineDirect3DVertexStridedData *strided = &This->strided_streams;
892 WineDirect3DVertexStridedData stridedlcl;
893 /* Ok, Work out which primitive is requested and how many vertexes that
894 will be */
895 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
896 if (numberOfVertices == 0 )
897 numberOfVertices = calculatedNumberOfindices;
899 if(!use_vs(This)) {
900 if(!This->strided_streams.u.s.position_transformed && This->activeContext->num_untracked_materials &&
901 This->stateBlock->renderState[WINED3DRS_LIGHTING]) {
902 static BOOL first = TRUE;
903 if(first) {
904 FIXME("Using software emulation because not all material properties could be tracked\n");
905 first = FALSE;
906 } else {
907 TRACE("Using software emulation because not all material properties could be tracked\n");
909 emulation = TRUE;
911 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
912 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
913 * to a float in the vertex buffer
915 static BOOL first = TRUE;
916 if(first) {
917 FIXME("Using software emulation because manual fog coordinates are provided\n");
918 first = FALSE;
919 } else {
920 TRACE("Using software emulation because manual fog coordinates are provided\n");
922 emulation = TRUE;
925 if(emulation) {
926 strided = &stridedlcl;
927 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
928 remove_vbos(This, &stridedlcl);
932 if (This->useDrawStridedSlow || emulation) {
933 /* Immediate mode drawing */
934 if(use_vs(This)) {
935 static BOOL first = TRUE;
936 if(first) {
937 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
938 first = FALSE;
939 } else {
940 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
942 drawStridedSlowVs(iface, strided, calculatedNumberOfindices, glPrimType,
943 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
944 } else {
945 drawStridedSlow(iface, strided, calculatedNumberOfindices,
946 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
948 } else if(This->instancedDraw) {
949 /* Instancing emulation with mixing immediate mode and arrays */
950 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
951 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
952 } else {
953 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
954 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
958 /* Finished updating the screen, restore lock */
959 LEAVE_GL();
960 TRACE("Done all gl drawing\n");
962 /* Diagnostics */
963 #ifdef SHOW_FRAME_MAKEUP
965 static long int primCounter = 0;
966 /* NOTE: set primCounter to the value reported by drawprim
967 before you want to to write frame makeup to /tmp */
968 if (primCounter >= 0) {
969 WINED3DLOCKED_RECT r;
970 char buffer[80];
971 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
972 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
973 TRACE("Saving screenshot %s\n", buffer);
974 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
975 IWineD3DSurface_UnlockRect(This->render_targets[0]);
977 #ifdef SHOW_TEXTURE_MAKEUP
979 IWineD3DSurface *pSur;
980 int textureNo;
981 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
982 if (This->stateBlock->textures[textureNo] != NULL) {
983 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
984 TRACE("Saving texture %s\n", buffer);
985 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
986 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
987 IWineD3DSurface_SaveSnapshot(pSur, buffer);
988 IWineD3DSurface_Release(pSur);
989 } else {
990 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
995 #endif
997 TRACE("drawprim #%ld\n", primCounter);
998 ++primCounter;
1000 #else
1001 #ifdef SHOW_FRAME_MAKEUP_ONKEY
1003 /* NOTE: set primCounter to the value reported by drawprim
1004 before you want to to write frame makeup to /tmp */
1005 if ( GetKeyState(VK_F12) ) {
1006 static long int primCounter = 0;
1007 WINED3DLOCKED_RECT r;
1008 char buffer[80];
1009 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
1010 sprintf(buffer, "/tmp/wine/backbuffer_%03ld.tga", primCounter);
1011 /*TRACE("Saving screenshot %s\n", buffer);*/
1012 TRACE("Saving drawprim %03ld\n", primCounter);
1013 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
1014 IWineD3DSurface_UnlockRect(This->render_targets[0]);
1017 IWineD3DSurface *pSur;
1018 int textureNo;
1019 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
1020 if (This->stateBlock->textures[textureNo] != NULL) {
1021 sprintf(buffer, "/tmp/wine/texture_%03ld_%02d_%p.tga", primCounter, textureNo, This->stateBlock->textures[textureNo]);
1022 /*TRACE("Saving texture %s\n", buffer);*/
1023 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
1024 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
1025 IWineD3DSurface_SaveSnapshot(pSur, buffer);
1026 IWineD3DSurface_Release(pSur);
1027 } else {
1028 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
1033 ++primCounter;
1034 if (primCounter > 50) exit(0);
1036 TRACE("drawprim #%d\n", primCounter);
1037 ++primCounter;
1039 #endif
1040 #endif
1042 /* Control goes back to the device, stateblock values may change again */
1043 This->isInDraw = FALSE;
1046 static void normalize_normal(float *n) {
1047 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
1048 if(length == 0.0) return;
1049 length = sqrt(length);
1050 n[0] = n[0] / length;
1051 n[1] = n[1] / length;
1052 n[2] = n[2] / length;
1055 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
1057 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
1058 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1059 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
1060 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1061 * in drawprim.
1063 * To read back, the opengl feedback mode is used. This creates a problem because we want
1064 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1065 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1066 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1067 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1069 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1070 * resulting colors back to the normals.
1072 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1073 * does not restore it because normally a draw follows immediately afterwards. The caller is
1074 * responsible of taking care that either the gl states are restored, or the context activated
1075 * for drawing to reset the lastWasBlit flag.
1077 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1078 struct WineD3DRectPatch *patch) {
1079 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1080 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1081 WineDirect3DVertexStridedData strided;
1082 BYTE *data;
1083 WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1084 DWORD vtxStride;
1085 GLenum feedback_type;
1086 GLfloat *feedbuffer;
1088 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1089 * Beware of vbos
1091 memset(&strided, 0, sizeof(strided));
1092 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1093 if(strided.u.s.position.VBO) {
1094 IWineD3DVertexBufferImpl *vb;
1095 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1096 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1097 (unsigned long) vb->resource.allocatedMemory);
1099 vtxStride = strided.u.s.position.dwStride;
1100 data = strided.u.s.position.lpData +
1101 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1102 vtxStride * info->StartVertexOffsetWidth;
1104 /* Not entirely sure about what happens with transformed vertices */
1105 if(strided.u.s.position_transformed) {
1106 FIXME("Transformed position in rectpatch generation\n");
1108 if(vtxStride % sizeof(GLfloat)) {
1109 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1110 * I don't see how the stride could not be a multiple of 4, but make sure
1111 * to check it
1113 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1115 if(info->Basis != WINED3DBASIS_BEZIER) {
1116 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1118 if(info->Degree != WINED3DDEGREE_CUBIC) {
1119 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1122 /* First, get the boundary cube of the input data */
1123 for(j = 0; j < info->Height; j++) {
1124 for(i = 0; i < info->Width; i++) {
1125 float *v = (float *) (data + vtxStride * i + vtxStride * info->Stride * j);
1126 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1127 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1128 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1129 if(v[2] < neg_z) neg_z = v[2];
1133 /* This needs some improvements in the vertex decl code */
1134 FIXME("Cannot find data to generate. Only generating position and normals\n");
1135 patch->has_normals = TRUE;
1136 patch->has_texcoords = FALSE;
1138 /* Simply activate the context for blitting. This disables all the things we don't want and
1139 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1140 * patch (as opposed to normal draws) will most likely need different changes anyway
1142 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1143 ENTER_GL();
1145 glMatrixMode(GL_PROJECTION);
1146 checkGLcall("glMatrixMode(GL_PROJECTION)");
1147 glLoadIdentity();
1148 checkGLcall("glLoadIndentity()");
1149 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1150 glTranslatef(0, 0, 0.5);
1151 checkGLcall("glScalef");
1152 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1153 checkGLcall("glViewport");
1155 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1156 * our feedback buffer parser
1158 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1159 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1160 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1161 if(patch->has_normals) {
1162 float black[4] = {0, 0, 0, 0};
1163 float red[4] = {1, 0, 0, 0};
1164 float green[4] = {0, 1, 0, 0};
1165 float blue[4] = {0, 0, 1, 0};
1166 float white[4] = {1, 1, 1, 1};
1167 glEnable(GL_LIGHTING);
1168 checkGLcall("glEnable(GL_LIGHTING)");
1169 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1170 checkGLcall("glLightModel for MODEL_AMBIENT");
1171 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1173 for(i = 3; i < GL_LIMITS(lights); i++) {
1174 glDisable(GL_LIGHT0 + i);
1175 checkGLcall("glDisable(GL_LIGHT0 + i)");
1176 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1179 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1180 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1181 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1182 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1183 glLightfv(GL_LIGHT0, GL_POSITION, red);
1184 glEnable(GL_LIGHT0);
1185 checkGLcall("Setting up light 1\n");
1186 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1187 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1188 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1189 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1190 glLightfv(GL_LIGHT1, GL_POSITION, green);
1191 glEnable(GL_LIGHT1);
1192 checkGLcall("Setting up light 2\n");
1193 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1194 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1195 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1196 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1197 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1198 glEnable(GL_LIGHT2);
1199 checkGLcall("Setting up light 3\n");
1201 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1202 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1203 glDisable(GL_COLOR_MATERIAL);
1204 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1205 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1206 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1207 checkGLcall("Setting up materials\n");
1210 /* Enable the needed maps.
1211 * GL_MAP2_VERTEX_3 is needed for positional data.
1212 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1213 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1215 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1216 out_vertex_size = 3 /* position */;
1217 d3d_out_vertex_size = 3;
1218 glEnable(GL_MAP2_VERTEX_3);
1219 if(patch->has_normals && patch->has_texcoords) {
1220 FIXME("Texcoords not handled yet\n");
1221 feedback_type = GL_3D_COLOR_TEXTURE;
1222 out_vertex_size += 8;
1223 d3d_out_vertex_size += 7;
1224 glEnable(GL_AUTO_NORMAL);
1225 glEnable(GL_MAP2_TEXTURE_COORD_4);
1226 } else if(patch->has_texcoords) {
1227 FIXME("Texcoords not handled yet\n");
1228 feedback_type = GL_3D_COLOR_TEXTURE;
1229 out_vertex_size += 7;
1230 d3d_out_vertex_size += 4;
1231 glEnable(GL_MAP2_TEXTURE_COORD_4);
1232 } else if(patch->has_normals) {
1233 feedback_type = GL_3D_COLOR;
1234 out_vertex_size += 4;
1235 d3d_out_vertex_size += 3;
1236 glEnable(GL_AUTO_NORMAL);
1237 } else {
1238 feedback_type = GL_3D;
1240 checkGLcall("glEnable vertex attrib generation");
1242 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1243 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1244 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1246 glMap2f(GL_MAP2_VERTEX_3,
1247 0, 1, vtxStride / sizeof(float), info->Width,
1248 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1249 (float *) data);
1250 checkGLcall("glMap2f");
1251 if(patch->has_texcoords) {
1252 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1253 0, 1, vtxStride / sizeof(float), info->Width,
1254 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1255 (float *) data);
1256 checkGLcall("glMap2f");
1258 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1259 checkGLcall("glMapGrid2f");
1261 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1262 checkGLcall("glFeedbackBuffer");
1263 glRenderMode(GL_FEEDBACK);
1265 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1266 checkGLcall("glEvalMesh2\n");
1268 i = glRenderMode(GL_RENDER);
1269 if(i == -1) {
1270 LEAVE_GL();
1271 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1272 Sleep(10000);
1273 HeapFree(GetProcessHeap(), 0, feedbuffer);
1274 return WINED3DERR_DRIVERINTERNALERROR;
1275 } else if(i != buffer_size) {
1276 LEAVE_GL();
1277 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1278 Sleep(10000);
1279 HeapFree(GetProcessHeap(), 0, feedbuffer);
1280 return WINED3DERR_DRIVERINTERNALERROR;
1281 } else {
1282 TRACE("Got %d elements as expected\n", i);
1285 HeapFree(GetProcessHeap(), 0, patch->mem);
1286 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1287 i = 0;
1288 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1289 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1290 ERR("Unexpected token: %f\n", feedbuffer[j]);
1291 continue;
1293 if(feedbuffer[j + 1] != 3) {
1294 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1295 continue;
1297 /* Somehow there are different ideas about back / front facing, so fix up the
1298 * vertex order
1300 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1301 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1302 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1303 if(patch->has_normals) {
1304 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1305 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1306 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1308 i += d3d_out_vertex_size;
1310 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1311 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1312 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1313 if(patch->has_normals) {
1314 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1315 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1316 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1318 i += d3d_out_vertex_size;
1320 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1321 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1322 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1323 if(patch->has_normals) {
1324 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1325 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1326 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1328 i += d3d_out_vertex_size;
1331 if(patch->has_normals) {
1332 /* Now do the same with reverse light directions */
1333 float x[4] = {-1, 0, 0, 0};
1334 float y[4] = { 0, -1, 0, 0};
1335 float z[4] = { 0, 0, -1, 0};
1336 glLightfv(GL_LIGHT0, GL_POSITION, x);
1337 glLightfv(GL_LIGHT1, GL_POSITION, y);
1338 glLightfv(GL_LIGHT2, GL_POSITION, z);
1339 checkGLcall("Setting up reverse light directions\n");
1341 glRenderMode(GL_FEEDBACK);
1342 checkGLcall("glRenderMode(GL_FEEDBACK)");
1343 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1344 checkGLcall("glEvalMesh2\n");
1345 i = glRenderMode(GL_RENDER);
1346 checkGLcall("glRenderMode(GL_RENDER)");
1348 i = 0;
1349 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1350 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1351 ERR("Unexpected token: %f\n", feedbuffer[j]);
1352 continue;
1354 if(feedbuffer[j + 1] != 3) {
1355 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1356 continue;
1358 if(patch->mem[i + 3] == 0.0)
1359 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1360 if(patch->mem[i + 4] == 0.0)
1361 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1362 if(patch->mem[i + 5] == 0.0)
1363 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1364 normalize_normal(patch->mem + i + 3);
1365 i += d3d_out_vertex_size;
1367 if(patch->mem[i + 3] == 0.0)
1368 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1369 if(patch->mem[i + 4] == 0.0)
1370 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1371 if(patch->mem[i + 5] == 0.0)
1372 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1373 normalize_normal(patch->mem + i + 3);
1374 i += d3d_out_vertex_size;
1376 if(patch->mem[i + 3] == 0.0)
1377 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1378 if(patch->mem[i + 4] == 0.0)
1379 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1380 if(patch->mem[i + 5] == 0.0)
1381 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1382 normalize_normal(patch->mem + i + 3);
1383 i += d3d_out_vertex_size;
1387 glDisable(GL_MAP2_VERTEX_3);
1388 glDisable(GL_AUTO_NORMAL);
1389 glDisable(GL_MAP2_NORMAL);
1390 glDisable(GL_MAP2_TEXTURE_COORD_4);
1391 checkGLcall("glDisable vertex attrib generation");
1392 LEAVE_GL();
1394 HeapFree(GetProcessHeap(), 0, feedbuffer);
1396 vtxStride = 3 * sizeof(float);
1397 if(patch->has_normals) {
1398 vtxStride += 3 * sizeof(float);
1400 if(patch->has_texcoords) {
1401 vtxStride += 4 * sizeof(float);
1403 memset(&patch->strided, 0, sizeof(&patch->strided));
1404 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1405 patch->strided.u.s.position.dwStride = vtxStride;
1406 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1407 patch->strided.u.s.position.streamNo = 255;
1409 if(patch->has_normals) {
1410 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1411 patch->strided.u.s.normal.dwStride = vtxStride;
1412 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1413 patch->strided.u.s.normal.streamNo = 255;
1415 if(patch->has_texcoords) {
1416 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1417 if(patch->has_normals) {
1418 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1420 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1421 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1422 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1423 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1424 * application.
1426 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;
1429 return WINED3D_OK;