wined3d: Add Filtering to IWineD3DSurface::Blt and handle it.
[wine] / dlls / wined3d / drawprim.c
1 /*
2  * WINED3D draw functions
3  *
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
10  *
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.
15  *
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.
20  *
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
24  */
25
26 #include "config.h"
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
31
32 #include <stdio.h>
33
34 #if 0 /* TODO */
35 extern IWineD3DVertexShaderImpl*            VertexShaders[64];
36 extern IWineD3DVertexDeclarationImpl*       VertexShaderDeclarations[64];
37 extern IWineD3DPixelShaderImpl*             PixelShaders[64];
38
39 #undef GL_VERSION_1_4 /* To be fixed, caused by mesa headers */
40 #endif
41
42 /* Issues the glBegin call for gl given the primitive type and count */
43 static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
44                     DWORD            NumPrimitives,
45                     GLenum          *primType)
46 {
47     DWORD   NumVertexes = NumPrimitives;
48
49     switch (PrimitiveType) {
50     case WINED3DPT_POINTLIST:
51         TRACE("POINTS\n");
52         *primType   = GL_POINTS;
53         NumVertexes = NumPrimitives;
54         break;
55
56     case WINED3DPT_LINELIST:
57         TRACE("LINES\n");
58         *primType   = GL_LINES;
59         NumVertexes = NumPrimitives * 2;
60         break;
61
62     case WINED3DPT_LINESTRIP:
63         TRACE("LINE_STRIP\n");
64         *primType   = GL_LINE_STRIP;
65         NumVertexes = NumPrimitives + 1;
66         break;
67
68     case WINED3DPT_TRIANGLELIST:
69         TRACE("TRIANGLES\n");
70         *primType   = GL_TRIANGLES;
71         NumVertexes = NumPrimitives * 3;
72         break;
73
74     case WINED3DPT_TRIANGLESTRIP:
75         TRACE("TRIANGLE_STRIP\n");
76         *primType   = GL_TRIANGLE_STRIP;
77         NumVertexes = NumPrimitives + 2;
78         break;
79
80     case WINED3DPT_TRIANGLEFAN:
81         TRACE("TRIANGLE_FAN\n");
82         *primType   = GL_TRIANGLE_FAN;
83         NumVertexes = NumPrimitives + 2;
84         break;
85
86     default:
87         FIXME("Unhandled primitive\n");
88         *primType    = GL_POINTS;
89         break;
90     }
91     return NumVertexes;
92 }
93
94 static BOOL fixed_get_input(
95     BYTE usage, BYTE usage_idx,
96     unsigned int* regnum) {
97
98     *regnum = -1;
99
100     /* Those positions must have the order in the
101      * named part of the strided data */
102
103     if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 0)
104         *regnum = 0;
105     else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
106         *regnum = 1;
107     else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
108         *regnum = 2;
109     else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
110         *regnum = 3;
111     else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
112         *regnum = 4;
113     else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
114         *regnum = 5;
115     else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
116         *regnum = 6;
117     else if (usage == WINED3DDECLUSAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
118         *regnum = 7 + usage_idx;
119     else if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 1)
120         *regnum = 7 + WINED3DDP_MAXTEXCOORD;
121     else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 1)
122         *regnum = 8 + WINED3DDP_MAXTEXCOORD;
123     else if (usage == WINED3DDECLUSAGE_TANGENT && usage_idx == 0)
124         *regnum = 9 + WINED3DDP_MAXTEXCOORD;
125     else if (usage == WINED3DDECLUSAGE_BINORMAL && usage_idx == 0)
126         *regnum = 10 + WINED3DDP_MAXTEXCOORD;
127     else if (usage == WINED3DDECLUSAGE_TESSFACTOR && usage_idx == 0)
128         *regnum = 11 + WINED3DDP_MAXTEXCOORD;
129     else if (usage == WINED3DDECLUSAGE_FOG && usage_idx == 0)
130         *regnum = 12 + WINED3DDP_MAXTEXCOORD;
131     else if (usage == WINED3DDECLUSAGE_DEPTH && usage_idx == 0)
132         *regnum = 13 + WINED3DDP_MAXTEXCOORD;
133     else if (usage == WINED3DDECLUSAGE_SAMPLE && usage_idx == 0)
134         *regnum = 14 + WINED3DDP_MAXTEXCOORD;
135
136     if (*regnum < 0) {
137         FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
138             debug_d3ddeclusage(usage), usage_idx);
139         return FALSE;
140     }
141     return TRUE;
142 }
143
144 void primitiveDeclarationConvertToStridedData(
145      IWineD3DDevice *iface,
146      BOOL useVertexShaderFunction,
147      WineDirect3DVertexStridedData *strided,
148      BOOL *fixup) {
149
150      /* We need to deal with frequency data!*/
151
152     BYTE  *data    = NULL;
153     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
154     IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
155     int i;
156     WINED3DVERTEXELEMENT *element;
157     DWORD stride;
158     int reg;
159     char isPreLoaded[MAX_STREAMS];
160     DWORD preLoadStreams[MAX_STREAMS], numPreloadStreams = 0;
161
162     memset(isPreLoaded, 0, sizeof(isPreLoaded));
163
164     /* Check for transformed vertices, disable vertex shader if present */
165     strided->u.s.position_transformed = FALSE;
166     for (i = 0; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
167         element = vertexDeclaration->pDeclarationWine + i;
168
169         if (element->Usage == WINED3DDECLUSAGE_POSITIONT) {
170             strided->u.s.position_transformed = TRUE;
171             useVertexShaderFunction = FALSE;
172         }
173     }
174
175     /* Translate the declaration into strided data */
176     for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
177         GLint streamVBO = 0;
178         BOOL stride_used;
179         unsigned int idx;
180
181         element = vertexDeclaration->pDeclarationWine + i;
182         TRACE("%p Element %p (%d of %d)\n", vertexDeclaration->pDeclarationWine,
183             element,  i + 1, vertexDeclaration->declarationWNumElements - 1);
184
185         if (This->stateBlock->streamSource[element->Stream] == NULL)
186             continue;
187
188         if (This->stateBlock->streamIsUP) {
189             TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
190             streamVBO = 0;
191             data    = (BYTE *)This->stateBlock->streamSource[element->Stream];
192         } else {
193             TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
194             if(!isPreLoaded[element->Stream]) {
195                 preLoadStreams[numPreloadStreams] = element->Stream;
196                 numPreloadStreams++;
197                 isPreLoaded[element->Stream] = 1;
198             }
199             data    = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
200             if(fixup) {
201                 if( streamVBO != 0) *fixup = TRUE;
202                 else if(*fixup && !useVertexShaderFunction) {
203                     /* This may be bad with the fixed function pipeline */
204                     FIXME("Missing fixed and unfixed vertices, expect graphics glitches\n");
205                 }
206             }
207         }
208         stride  = This->stateBlock->streamStride[element->Stream];
209         data += element->Offset;
210         reg = element->Reg;
211
212         TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
213
214         if (useVertexShaderFunction)
215             stride_used = vshader_get_input(This->stateBlock->vertexShader,
216                 element->Usage, element->UsageIndex, &idx);
217         else
218             stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
219
220         if (stride_used) {
221             TRACE("Loaded %s array %u [usage=%s, usage_idx=%u, "
222                     "stream=%u, offset=%u, stride=%u, VBO=%u]\n",
223                     useVertexShaderFunction? "shader": "fixed function", idx,
224                     debug_d3ddeclusage(element->Usage), element->UsageIndex,
225                     element->Stream, element->Offset, stride, streamVBO);
226
227             strided->u.input[idx].lpData = data;
228             strided->u.input[idx].dwType = element->Type;
229             strided->u.input[idx].dwStride = stride;
230             strided->u.input[idx].VBO = streamVBO;
231             strided->u.input[idx].streamNo = element->Stream;
232         }
233     }
234     /* Now call PreLoad on all the vertex buffers. In the very rare case
235      * that the buffers stopps converting PreLoad will dirtify the VDECL again.
236      * The vertex buffer can now use the strided structure in the device instead of finding its
237      * own again.
238      *
239      * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
240      * once in there.
241      */
242     for(i=0; i < numPreloadStreams; i++) {
243         IWineD3DVertexBuffer_PreLoad(This->stateBlock->streamSource[preLoadStreams[i]]);
244     }
245 }
246
247 void primitiveConvertFVFtoOffset(DWORD thisFVF, DWORD stride, BYTE *data, WineDirect3DVertexStridedData *strided, GLint streamVBO, UINT streamNo) {
248     int           numBlends;
249     int           numTextures;
250     int           textureNo;
251     int           coordIdxInfo = 0x00;    /* Information on number of coords supplied */
252     int           numCoords[8];           /* Holding place for WINED3DFVF_TEXTUREFORMATx  */
253
254     /* Either 3 or 4 floats depending on the FVF */
255     /* FIXME: Can blending data be in a different stream to the position data?
256           and if so using the fixed pipeline how do we handle it               */
257     if (thisFVF & WINED3DFVF_POSITION_MASK) {
258         strided->u.s.position.lpData    = data;
259         strided->u.s.position.dwType    = WINED3DDECLTYPE_FLOAT3;
260         strided->u.s.position.dwStride  = stride;
261         strided->u.s.position.VBO       = streamVBO;
262         strided->u.s.position.streamNo  = streamNo;
263         data += 3 * sizeof(float);
264         if (thisFVF & WINED3DFVF_XYZRHW) {
265             strided->u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
266             strided->u.s.position_transformed = TRUE;
267             data += sizeof(float);
268         } else
269             strided->u.s.position_transformed = FALSE;
270     }
271
272     /* Blending is numBlends * FLOATs followed by a DWORD for UBYTE4 */
273     /** do we have to Check This->stateBlock->renderState[D3DRS_INDEXEDVERTEXBLENDENABLE] ? */
274     numBlends = 1 + (((thisFVF & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
275     if(thisFVF & WINED3DFVF_LASTBETA_UBYTE4) numBlends--;
276
277     if ((thisFVF & WINED3DFVF_XYZB5 ) > WINED3DFVF_XYZRHW) {
278         TRACE("Setting blend Weights to %p\n", data);
279         strided->u.s.blendWeights.lpData    = data;
280         strided->u.s.blendWeights.dwType    = WINED3DDECLTYPE_FLOAT1 + numBlends - 1;
281         strided->u.s.blendWeights.dwStride  = stride;
282         strided->u.s.blendWeights.VBO       = streamVBO;
283         strided->u.s.blendWeights.streamNo  = streamNo;
284         data += numBlends * sizeof(FLOAT);
285
286         if (thisFVF & WINED3DFVF_LASTBETA_UBYTE4) {
287             strided->u.s.blendMatrixIndices.lpData = data;
288             strided->u.s.blendMatrixIndices.dwType  = WINED3DDECLTYPE_UBYTE4;
289             strided->u.s.blendMatrixIndices.dwStride= stride;
290             strided->u.s.blendMatrixIndices.VBO     = streamVBO;
291             strided->u.s.blendMatrixIndices.streamNo= streamNo;
292             data += sizeof(DWORD);
293         }
294     }
295
296     /* Normal is always 3 floats */
297     if (thisFVF & WINED3DFVF_NORMAL) {
298         strided->u.s.normal.lpData    = data;
299         strided->u.s.normal.dwType    = WINED3DDECLTYPE_FLOAT3;
300         strided->u.s.normal.dwStride  = stride;
301         strided->u.s.normal.VBO       = streamVBO;
302         strided->u.s.normal.streamNo  = streamNo;
303         data += 3 * sizeof(FLOAT);
304     }
305
306     /* Pointsize is a single float */
307     if (thisFVF & WINED3DFVF_PSIZE) {
308         strided->u.s.pSize.lpData    = data;
309         strided->u.s.pSize.dwType    = WINED3DDECLTYPE_FLOAT1;
310         strided->u.s.pSize.dwStride  = stride;
311         strided->u.s.pSize.VBO       = streamVBO;
312         strided->u.s.pSize.streamNo  = streamNo;
313         data += sizeof(FLOAT);
314     }
315
316     /* Diffuse is 4 unsigned bytes */
317     if (thisFVF & WINED3DFVF_DIFFUSE) {
318         strided->u.s.diffuse.lpData    = data;
319         strided->u.s.diffuse.dwType    = WINED3DDECLTYPE_SHORT4;
320         strided->u.s.diffuse.dwStride  = stride;
321         strided->u.s.diffuse.VBO       = streamVBO;
322         strided->u.s.diffuse.streamNo  = streamNo;
323         data += sizeof(DWORD);
324     }
325
326     /* Specular is 4 unsigned bytes */
327     if (thisFVF & WINED3DFVF_SPECULAR) {
328         strided->u.s.specular.lpData    = data;
329         strided->u.s.specular.dwType    = WINED3DDECLTYPE_SHORT4;
330         strided->u.s.specular.dwStride  = stride;
331         strided->u.s.specular.VBO       = streamVBO;
332         strided->u.s.specular.streamNo  = streamNo;
333         data += sizeof(DWORD);
334     }
335
336     /* Texture coords */
337     numTextures   = (thisFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
338     coordIdxInfo  = (thisFVF & 0x00FF0000) >> 16; /* 16 is from definition of WINED3DFVF_TEXCOORDSIZE1, and is 8 (0-7 stages) * 2bits long */
339
340     /* numTextures indicates the number of texture coordinates supplied */
341     /* However, the first set may not be for stage 0 texture - it all   */
342     /*   depends on WINED3DTSS_TEXCOORDINDEX.                           */
343     /* The number of bytes for each coordinate set is based off         */
344     /*   WINED3DFVF_TEXCOORDSIZEn, which are the bottom 2 bits              */
345
346     /* So, for each supplied texture extract the coords */
347     for (textureNo = 0; textureNo < numTextures; ++textureNo) {
348
349         strided->u.s.texCoords[textureNo].lpData    = data;
350         strided->u.s.texCoords[textureNo].dwType    = WINED3DDECLTYPE_FLOAT1;
351         strided->u.s.texCoords[textureNo].dwStride  = stride;
352         strided->u.s.texCoords[textureNo].VBO       = streamVBO;
353         strided->u.s.texCoords[textureNo].streamNo  = streamNo;
354         numCoords[textureNo] = coordIdxInfo & 0x03;
355
356         /* Always one set */
357         data += sizeof(float);
358         if (numCoords[textureNo] != WINED3DFVF_TEXTUREFORMAT1) {
359             strided->u.s.texCoords[textureNo].dwType = WINED3DDECLTYPE_FLOAT2;
360             data += sizeof(float);
361             if (numCoords[textureNo] != WINED3DFVF_TEXTUREFORMAT2) {
362                 strided->u.s.texCoords[textureNo].dwType = WINED3DDECLTYPE_FLOAT3;
363                 data += sizeof(float);
364                 if (numCoords[textureNo] != WINED3DFVF_TEXTUREFORMAT3) {
365                     strided->u.s.texCoords[textureNo].dwType = WINED3DDECLTYPE_FLOAT4;
366                     data += sizeof(float);
367                 }
368             }
369         }
370         coordIdxInfo = coordIdxInfo >> 2; /* Drop bottom two bits */
371     }
372 }
373
374 void primitiveConvertToStridedData(IWineD3DDevice *iface, WineDirect3DVertexStridedData *strided, BOOL *fixup) {
375     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
376     GLint         streamVBO = 0;
377     DWORD  stride  = This->stateBlock->streamStride[0];
378     BYTE  *data    = NULL;
379     DWORD  thisFVF = 0;
380
381     /* Retrieve appropriate FVF */
382     thisFVF = This->stateBlock->fvf;
383     /* Handle memory passed directly as well as vertex buffers */
384     if (This->stateBlock->streamIsUP) {
385         streamVBO = 0;
386         data    = (BYTE *)This->stateBlock->streamSource[0];
387     } else {
388         /* The for loop should iterate through here only once per stream, so we don't need magic to prevent double loading
389          * buffers
390          */
391         data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[0], 0, &streamVBO);
392         if(fixup) {
393             if(streamVBO != 0 ) *fixup = TRUE;
394         }
395     }
396     VTRACE(("FVF for stream 0 is %lx\n", thisFVF));
397
398     /* Now convert the stream into pointers */
399     primitiveConvertFVFtoOffset(thisFVF, stride, data, strided, streamVBO, 0);
400
401     /* Now call PreLoad on the vertex buffer. In the very rare case
402      * that the buffers stopps converting PreLoad will dirtify the VDECL again.
403      * The vertex buffer can now use the strided structure in the device instead of finding its
404      * own again.
405      */
406     if(!This->stateBlock->streamIsUP) {
407         IWineD3DVertexBuffer_PreLoad(This->stateBlock->streamSource[0]);
408     }
409 }
410
411 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
412                      const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
413     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
414
415     if (idxSize != 0 /* This crashes sometimes!*/) {
416         TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
417         idxData = idxData == (void *)-1 ? NULL : idxData;
418 #if 1
419         glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
420                      (const char *)idxData+(idxSize * startIdx));
421 #else /* using drawRangeElements may be faster */
422
423         glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
424                       idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
425                       (const char *)idxData+(idxSize * startIdx));
426 #endif
427         checkGLcall("glDrawRangeElements");
428
429     } else {
430
431         /* Note first is now zero as we shuffled along earlier */
432         TRACE("(%p) : glDrawArrays(%x, 0, %d)\n", This, glPrimitiveType, numberOfVertices);
433         glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
434         checkGLcall("glDrawArrays");
435
436     }
437
438     return;
439 }
440
441 /*
442  * Actually draw using the supplied information.
443  * Slower GL version which extracts info about each vertex in turn
444  */
445
446 static void drawStridedSlow(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd,
447                      UINT NumVertexes, GLenum glPrimType,
448                      const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
449
450     unsigned int               textureNo    = 0;
451     unsigned int               texture_idx  = 0;
452     const short               *pIdxBufS     = NULL;
453     const long                *pIdxBufL     = NULL;
454     LONG                       vx_index;
455     float x  = 0.0f, y  = 0.0f, z = 0.0f;  /* x,y,z coordinates          */
456     float rhw = 0.0f;                      /* rhw                        */
457     DWORD diffuseColor = 0xFFFFFFFF;       /* Diffuse Color              */
458     DWORD specularColor = 0;               /* Specular Color             */
459     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
460     UINT *streamOffset = This->stateBlock->streamOffset;
461     LONG                       SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
462
463     BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
464     BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
465
466     TRACE("Using slow vertex array code\n");
467
468     /* Variable Initialization */
469     if (idxSize != 0) {
470         /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
471          * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
472          * idxData will be != NULL
473          */
474         if(idxData == NULL) {
475             idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
476         }
477
478         if (idxSize == 2) pIdxBufS = (const short *) idxData;
479         else pIdxBufL = (const long *) idxData;
480     }
481
482     /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
483      * to the strided Data in the device and might be needed intact on the next draw
484      */
485     for (textureNo = 0, texture_idx = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
486         if(sd->u.s.texCoords[textureNo].lpData) {
487             texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
488         } else {
489             texCoords[textureNo] = NULL;
490         }
491     }
492     if(sd->u.s.diffuse.lpData) {
493         diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
494     }
495     if(sd->u.s.specular.lpData) {
496         specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
497     }
498     if(sd->u.s.normal.lpData) {
499         normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
500     }
501     if(sd->u.s.position.lpData) {
502         position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
503     }
504
505     /* Start drawing in GL */
506     VTRACE(("glBegin(%x)\n", glPrimType));
507     glBegin(glPrimType);
508
509     /* Default settings for data that is not passed */
510     if (sd->u.s.normal.lpData == NULL) {
511         glNormal3f(0, 0, 1);
512     }
513     if(sd->u.s.diffuse.lpData != NULL) {
514         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
515     }
516     if(sd->u.s.specular.lpData != NULL) {
517         if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
518             GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
519         }
520     }
521
522     /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
523      * Guess it's not necessary(we crash then anyway) and would only eat CPU time
524      */
525
526     /* For each primitive */
527     for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
528
529         /* Initialize diffuse color */
530         diffuseColor = 0xFFFFFFFF;
531
532         /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
533          * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
534          */
535
536         /* For indexed data, we need to go a few more strides in */
537         if (idxData != NULL) {
538
539             /* Indexed so work out the number of strides to skip */
540             if (idxSize == 2) {
541                 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
542                 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
543             } else {
544                 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
545                 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
546             }
547         }
548
549         /* Texture coords --------------------------- */
550         for (textureNo = 0, texture_idx = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
551
552             if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
553                 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
554                 continue ;
555             }
556
557             /* Query tex coords */
558             if (This->stateBlock->textures[textureNo] != NULL) {
559
560                 int    coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
561                 float *ptrToCoords = NULL;
562                 float  s = 0.0, t = 0.0, r = 0.0, q = 0.0;
563
564                 if (coordIdx > 7) {
565                     VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
566                     ++texture_idx;
567                     continue;
568                 } else if (coordIdx < 0) {
569                     FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
570                     ++texture_idx;
571                     continue;
572                 }
573
574                 ptrToCoords = (float *)(texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride));
575                 if (texCoords[coordIdx] == NULL) {
576                     TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
577                     ++texture_idx;
578                     continue;
579                 } else {
580
581                     int coordsToUse = sd->u.s.texCoords[coordIdx].dwType + 1; /* 0 == WINED3DDECLTYPE_FLOAT1 etc */
582
583                     /* The coords to supply depend completely on the fvf / vertex shader */
584                     switch (coordsToUse) {
585                     case 4: q = ptrToCoords[3]; /* drop through */
586                     case 3: r = ptrToCoords[2]; /* drop through */
587                     case 2: t = ptrToCoords[1]; /* drop through */
588                     case 1: s = ptrToCoords[0];
589                     }
590
591                     /* Projected is more 'fun' - Move the last coord to the 'q'
592                           parameter (see comments under WINED3DTSS_TEXTURETRANSFORMFLAGS */
593                     if ((This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] != WINED3DTTFF_DISABLE) &&
594                         (This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED)) {
595
596                         if (This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
597                             switch (coordsToUse) {
598                             case 0:  /* Drop Through */
599                             case 1:
600                                 FIXME("WINED3DTTFF_PROJECTED but only zero or one coordinate?\n");
601                                 break;
602                             case 2:
603                                 q = t;
604                                 t = 0.0;
605                                 coordsToUse = 4;
606                                 break;
607                             case 3:
608                                 q = r;
609                                 r = 0.0;
610                                 coordsToUse = 4;
611                                 break;
612                             case 4:  /* Nop here */
613                                 break;
614                             default:
615                                 FIXME("Unexpected WINED3DTSS_TEXTURETRANSFORMFLAGS value of %d\n",
616                                       This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED);
617                             }
618                         }
619                     }
620
621                     switch (coordsToUse) {   /* Supply the provided texture coords */
622                     case WINED3DTTFF_COUNT1:
623                         VTRACE(("tex:%d, s=%f\n", textureNo, s));
624                         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
625                             GL_EXTCALL(glMultiTexCoord1fARB(texture_idx, s));
626                         } else {
627                             glTexCoord1f(s);
628                         }
629                         break;
630                     case WINED3DTTFF_COUNT2:
631                         VTRACE(("tex:%d, s=%f, t=%f\n", textureNo, s, t));
632                         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
633                             GL_EXTCALL(glMultiTexCoord2fARB(texture_idx, s, t));
634                         } else {
635                             glTexCoord2f(s, t);
636                         }
637                         break;
638                     case WINED3DTTFF_COUNT3:
639                         VTRACE(("tex:%d, s=%f, t=%f, r=%f\n", textureNo, s, t, r));
640                         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
641                             GL_EXTCALL(glMultiTexCoord3fARB(texture_idx, s, t, r));
642                         } else {
643                             glTexCoord3f(s, t, r);
644                         }
645                         break;
646                     case WINED3DTTFF_COUNT4:
647                         VTRACE(("tex:%d, s=%f, t=%f, r=%f, q=%f\n", textureNo, s, t, r, q));
648                         if (GL_SUPPORT(ARB_MULTITEXTURE)) {
649                             GL_EXTCALL(glMultiTexCoord4fARB(texture_idx, s, t, r, q));
650                         } else {
651                             glTexCoord4f(s, t, r, q);
652                         }
653                         break;
654                     default:
655                         FIXME("Should not get here as coordsToUse is two bits only (%x)!\n", coordsToUse);
656                     }
657                 }
658             }
659             if (/*!GL_SUPPORT(NV_REGISTER_COMBINERS) || This->stateBlock->textures[textureNo]*/TRUE) ++texture_idx;
660         } /* End of textures */
661
662         /* Diffuse -------------------------------- */
663         if (diffuse) {
664             DWORD *ptrToCoords = (DWORD *)(diffuse + (SkipnStrides * sd->u.s.diffuse.dwStride));
665             diffuseColor = ptrToCoords[0];
666             VTRACE(("diffuseColor=%lx\n", diffuseColor));
667
668             glColor4ub(D3DCOLOR_B_R(diffuseColor),
669                      D3DCOLOR_B_G(diffuseColor),
670                      D3DCOLOR_B_B(diffuseColor),
671                      D3DCOLOR_B_A(diffuseColor));
672             VTRACE(("glColor4ub: r,g,b,a=%lu,%lu,%lu,%lu\n", 
673                     D3DCOLOR_B_R(diffuseColor),
674                     D3DCOLOR_B_G(diffuseColor),
675                     D3DCOLOR_B_B(diffuseColor),
676                     D3DCOLOR_B_A(diffuseColor)));
677         }
678
679         /* Specular ------------------------------- */
680         if (specular) {
681             DWORD *ptrToCoords = (DWORD *)(specular + (SkipnStrides * sd->u.s.specular.dwStride));
682             specularColor = ptrToCoords[0];
683             VTRACE(("specularColor=%lx\n", specularColor));
684
685             /* special case where the fog density is stored in the specular alpha channel */
686             if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
687               (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
688               This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
689                 if(GL_SUPPORT(EXT_FOG_COORD)) {
690                     GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
691                 } else {
692                     static BOOL warned = FALSE;
693                     if(!warned) {
694                         /* TODO: Use the fog table code from old ddraw */
695                         FIXME("Implement fog for transformed vertices in software\n");
696                         warned = TRUE;
697                     }
698                 }
699             }
700
701             VTRACE(("glSecondaryColor4ub: r,g,b=%lu,%lu,%lu\n", 
702                     D3DCOLOR_B_R(specularColor), 
703                     D3DCOLOR_B_G(specularColor), 
704                     D3DCOLOR_B_B(specularColor)));
705             if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
706                 GL_EXTCALL(glSecondaryColor3ubEXT)(
707                            D3DCOLOR_B_R(specularColor),
708                            D3DCOLOR_B_G(specularColor),
709                            D3DCOLOR_B_B(specularColor));
710             } else {
711                 /* Do not worry if specular colour missing and disable request */
712                 VTRACE(("Specular color extensions not supplied\n"));
713             }
714         }
715
716         /* Normal -------------------------------- */
717         if (normal != NULL) {
718             float *ptrToCoords = (float *)(normal + (SkipnStrides * sd->u.s.normal.dwStride));
719
720             VTRACE(("glNormal:nx,ny,nz=%f,%f,%f\n", ptrToCoords[0], ptrToCoords[1], ptrToCoords[2]));
721             glNormal3f(ptrToCoords[0], ptrToCoords[1], ptrToCoords[2]);
722         }
723
724         /* Position -------------------------------- */
725         if (position) {
726             float *ptrToCoords = (float *)(position + (SkipnStrides * sd->u.s.position.dwStride));
727             x = ptrToCoords[0];
728             y = ptrToCoords[1];
729             z = ptrToCoords[2];
730             rhw = 1.0;
731             VTRACE(("x,y,z=%f,%f,%f\n", x,y,z));
732
733             /* RHW follows, only if transformed, ie 4 floats were provided */
734             if (sd->u.s.position_transformed) {
735                 rhw = ptrToCoords[3];
736                 VTRACE(("rhw=%f\n", rhw));
737             }
738
739             if (1.0f == rhw || ((rhw < eps) && (rhw > -eps))) {
740                 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f\n", x,y,z));
741                 glVertex3f(x, y, z);
742             } else {
743                 GLfloat w = 1.0 / rhw;
744                 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f / rhw=%f\n", x,y,z,rhw));
745                 glVertex4f(x*w, y*w, z*w, w);
746             }
747         }
748
749         /* For non indexed mode, step onto next parts */
750         if (idxData == NULL) {
751             ++SkipnStrides;
752         }
753     }
754
755     glEnd();
756     checkGLcall("glEnd and previous calls");
757 }
758
759 static void check_fbo_status(IWineD3DDevice *iface) {
760     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
761
762     GLenum status = GL_EXTCALL(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
763     switch(status) {
764         case GL_FRAMEBUFFER_COMPLETE_EXT: TRACE("FBO complete.\n"); break;
765         default: TRACE("FBO status %#x.\n", status); break;
766     }
767 }
768
769 static void depth_blt(IWineD3DDevice *iface, GLuint texture) {
770     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
771     GLint old_binding = 0;
772
773     glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
774
775     glDisable(GL_CULL_FACE);
776     glDisable(GL_BLEND);
777     glDisable(GL_ALPHA_TEST);
778     glDisable(GL_STENCIL_TEST);
779     glEnable(GL_DEPTH_TEST);
780     glDepthFunc(GL_ALWAYS);
781
782     GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
783     glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
784     glBindTexture(GL_TEXTURE_2D, texture);
785     glEnable(GL_TEXTURE_2D);
786
787     This->shader_backend->shader_select_depth_blt(iface);
788
789     glBegin(GL_TRIANGLE_STRIP);
790     glVertex2f(-1.0f, -1.0f);
791     glVertex2f(1.0f, -1.0f);
792     glVertex2f(-1.0f, 1.0f);
793     glVertex2f(1.0f, 1.0f);
794     glEnd();
795
796     glBindTexture(GL_TEXTURE_2D, old_binding);
797
798     glPopAttrib();
799
800     /* Reselect the old shaders. There doesn't seem to be any glPushAttrib bit for arb shaders,
801      * and this seems easier and more efficient than providing the shader backend with a private
802      * storage to read and restore the old shader settings
803      */
804     This->shader_backend->shader_select(iface, use_ps(This), use_vs(This));
805 }
806
807 static void depth_copy(IWineD3DDevice *iface) {
808     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
809     IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *)This->depthStencilBuffer;
810
811     /* Only copy the depth buffer if there is one. */
812     if (!depth_stencil) return;
813
814     /* TODO: Make this work for modes other than FBO */
815     if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
816
817     if (This->render_offscreen) {
818         static GLuint tmp_texture = 0;
819         GLint old_binding = 0;
820
821         TRACE("Copying onscreen depth buffer to offscreen surface\n");
822
823         if (!tmp_texture) {
824             glGenTextures(1, &tmp_texture);
825         }
826
827         /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
828          * directly on the FBO texture. That's because we need to flip. */
829         GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
830         glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
831         glBindTexture(GL_TEXTURE_2D, tmp_texture);
832         glCopyTexImage2D(depth_stencil->glDescription.target,
833                 depth_stencil->glDescription.level,
834                 depth_stencil->glDescription.glFormatInternal,
835                 0,
836                 0,
837                 depth_stencil->currentDesc.Width,
838                 depth_stencil->currentDesc.Height,
839                 0);
840         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
841         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
842         glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
843         glBindTexture(GL_TEXTURE_2D, old_binding);
844
845         GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, This->fbo));
846         checkGLcall("glBindFramebuffer()");
847         depth_blt(iface, tmp_texture);
848         checkGLcall("depth_blt");
849     } else {
850         TRACE("Copying offscreen surface to onscreen depth buffer\n");
851
852         GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
853         checkGLcall("glBindFramebuffer()");
854         depth_blt(iface, depth_stencil->glDescription.textureName);
855         checkGLcall("depth_blt");
856     }
857 }
858
859 static inline void drawStridedInstanced(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
860                                  GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
861                                  ULONG startIdx, ULONG startVertex) {
862     UINT numInstances = 0;
863     int numInstancedAttribs = 0, i, j;
864     UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
865     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
866     IWineD3DStateBlockImpl *stateblock = This->stateBlock;
867
868     if (idxSize == 0) {
869         /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
870          * We don't support this for now
871          *
872          * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
873          * But the StreamSourceFreq value has a different meaning in that situation.
874          */
875         FIXME("Non-indexed instanced drawing is not supported\n");
876         return;
877     }
878
879     TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
880     idxData = idxData == (void *)-1 ? NULL : idxData;
881
882     /* First, figure out how many instances we have to draw */
883     for(i = 0; i < MAX_STREAMS; i++) {
884         /* Look at all non-instanced streams */
885         if(!(stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) &&
886            stateblock->streamSource[i]) {
887             int inst = stateblock->streamFreq[i];
888
889             if(numInstances && inst != numInstances) {
890                 ERR("Two streams specify a different number of instances. Got %d, new is %d\n", numInstances, inst);
891             }
892             numInstances = inst;
893         }
894     }
895
896     for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
897         if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
898             instancedData[numInstancedAttribs] = i;
899             numInstancedAttribs++;
900         }
901     }
902
903     /* now draw numInstances instances :-) */
904     for(i = 0; i < numInstances; i++) {
905         /* Specify the instanced attributes using immediate mode calls */
906         for(j = 0; j < numInstancedAttribs; j++) {
907             BYTE *ptr = sd->u.input[instancedData[j]].lpData +
908                         sd->u.input[instancedData[j]].dwStride * i +
909                         stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
910             if(sd->u.input[instancedData[j]].VBO) {
911                 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
912                 ptr += (long) vb->resource.allocatedMemory;
913             }
914
915             switch(sd->u.input[instancedData[j]].dwType) {
916                 case WINED3DDECLTYPE_FLOAT1:
917                     GL_EXTCALL(glVertexAttrib1fvARB(instancedData[j], (float *) ptr));
918                     break;
919                 case WINED3DDECLTYPE_FLOAT2:
920                     GL_EXTCALL(glVertexAttrib2fvARB(instancedData[j], (float *) ptr));
921                     break;
922                 case WINED3DDECLTYPE_FLOAT3:
923                     GL_EXTCALL(glVertexAttrib3fvARB(instancedData[j], (float *) ptr));
924                     break;
925                 case WINED3DDECLTYPE_FLOAT4:
926                     GL_EXTCALL(glVertexAttrib4fvARB(instancedData[j], (float *) ptr));
927                     break;
928
929                 case WINED3DDECLTYPE_UBYTE4:
930                     GL_EXTCALL(glVertexAttrib4NubvARB(instancedData[j], ptr));
931                     break;
932                 case WINED3DDECLTYPE_UBYTE4N:
933                 case WINED3DDECLTYPE_D3DCOLOR:
934                     GL_EXTCALL(glVertexAttrib4NubvARB(instancedData[j], ptr));
935                     break;
936
937                 case WINED3DDECLTYPE_SHORT2:
938                     GL_EXTCALL(glVertexAttrib4svARB(instancedData[j], (GLshort *) ptr));
939                     break;
940                 case WINED3DDECLTYPE_SHORT4:
941                     GL_EXTCALL(glVertexAttrib4svARB(instancedData[j], (GLshort *) ptr));
942                     break;
943
944                 case WINED3DDECLTYPE_SHORT2N:
945                 {
946                     GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
947                     GL_EXTCALL(glVertexAttrib4NsvARB(instancedData[j], s));
948                     break;
949                 }
950                 case WINED3DDECLTYPE_USHORT2N:
951                 {
952                     GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
953                     GL_EXTCALL(glVertexAttrib4NusvARB(instancedData[j], s));
954                     break;
955                 }
956                 case WINED3DDECLTYPE_SHORT4N:
957                     GL_EXTCALL(glVertexAttrib4NsvARB(instancedData[j], (GLshort *) ptr));
958                     break;
959                 case WINED3DDECLTYPE_USHORT4N:
960                     GL_EXTCALL(glVertexAttrib4NusvARB(instancedData[j], (GLushort *) ptr));
961                     break;
962
963                 case WINED3DDECLTYPE_UDEC3:
964                     FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
965                     /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
966                     break;
967                 case WINED3DDECLTYPE_DEC3N:
968                     FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
969                     /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
970                     break;
971
972                 case WINED3DDECLTYPE_FLOAT16_2:
973                     /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
974                      * byte float according to the IEEE standard
975                      */
976                     FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_2\n");
977                     break;
978                 case WINED3DDECLTYPE_FLOAT16_4:
979                     FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_4\n");
980                     break;
981
982                 case WINED3DDECLTYPE_UNUSED:
983                 default:
984                     ERR("Unexpected declaration in instanced attributes\n");
985                     break;
986             }
987         }
988
989         glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
990                     (const char *)idxData+(idxSize * startIdx));
991         checkGLcall("glDrawElements");
992     }
993 }
994
995 struct coords {
996     int x, y, z;
997 };
998
999 void blt_to_drawable(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *surface) {
1000     struct coords coords[4];
1001     int low_coord;
1002
1003     /* TODO: This could be supported for lazy unlocking */
1004     if(!(surface->Flags & SFLAG_INTEXTURE)) {
1005         /* It is ok at init to be nowhere */
1006         if(!(surface->Flags & SFLAG_INSYSMEM)) {
1007             ERR("Blitting surfaces from sysmem not supported yet\n");
1008         }
1009         return;
1010     }
1011
1012     ENTER_GL();
1013     ActivateContext(This, This->render_targets[0], CTXUSAGE_BLIT);
1014
1015     if(surface->glDescription.target == GL_TEXTURE_2D) {
1016         glBindTexture(GL_TEXTURE_2D, surface->glDescription.textureName);
1017         checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
1018
1019         coords[0].x = 0;    coords[0].y = 0;    coords[0].z = 0;
1020         coords[1].x = 0;    coords[1].y = 1;    coords[1].z = 0;
1021         coords[2].x = 1;    coords[2].y = 1;    coords[2].z = 0;
1022         coords[3].x = 1;    coords[3].y = 0;    coords[3].z = 0;
1023
1024         low_coord = 0;
1025     } else {
1026         /* Must be a cube map */
1027         glDisable(GL_TEXTURE_2D);
1028         checkGLcall("glDisable(GL_TEXTURE_2D)");
1029         glEnable(GL_TEXTURE_CUBE_MAP_ARB);
1030         checkGLcall("glEnable(surface->glDescription.target)");
1031         glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, surface->glDescription.textureName);
1032         checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
1033
1034         switch(surface->glDescription.target) {
1035             case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1036                 coords[0].x =  1;   coords[0].y = -1;   coords[0].z =  1;
1037                 coords[1].x =  1;   coords[1].y =  1;   coords[1].z =  1;
1038                 coords[2].x =  1;   coords[2].y =  1;   coords[2].z = -1;
1039                 coords[3].x =  1;   coords[3].y = -1;   coords[3].z = -1;
1040                 break;
1041
1042             case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1043                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z =  1;
1044                 coords[1].x = -1;   coords[1].y =  1;   coords[1].z =  1;
1045                 coords[2].x = -1;   coords[2].y =  1;   coords[2].z = -1;
1046                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z = -1;
1047                 break;
1048
1049             case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1050                 coords[0].x = -1;   coords[0].y =  1;   coords[0].z =  1;
1051                 coords[1].x =  1;   coords[1].y =  1;   coords[1].z =  1;
1052                 coords[2].x =  1;   coords[2].y =  1;   coords[2].z = -1;
1053                 coords[3].x = -1;   coords[3].y =  1;   coords[3].z = -1;
1054                 break;
1055
1056             case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1057                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z =  1;
1058                 coords[1].x =  1;   coords[1].y = -1;   coords[1].z =  1;
1059                 coords[2].x =  1;   coords[2].y = -1;   coords[2].z = -1;
1060                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z = -1;
1061                 break;
1062
1063             case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1064                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z =  1;
1065                 coords[1].x =  1;   coords[1].y = -1;   coords[1].z =  1;
1066                 coords[2].x =  1;   coords[2].y = -1;   coords[2].z =  1;
1067                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z =  1;
1068                 break;
1069
1070             case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1071                 coords[0].x = -1;   coords[0].y = -1;   coords[0].z = -1;
1072                 coords[1].x =  1;   coords[1].y = -1;   coords[1].z = -1;
1073                 coords[2].x =  1;   coords[2].y = -1;   coords[2].z = -1;
1074                 coords[3].x = -1;   coords[3].y = -1;   coords[3].z = -1;
1075
1076             default:
1077                 ERR("Unexpected texture target\n");
1078                 LEAVE_GL();
1079                 return;
1080         }
1081
1082         low_coord = -1;
1083     }
1084
1085     if(This->render_offscreen) {
1086         coords[0].y = coords[0].y == 1 ? low_coord : 1;
1087         coords[1].y = coords[1].y == 1 ? low_coord : 1;
1088         coords[2].y = coords[2].y == 1 ? low_coord : 1;
1089         coords[3].y = coords[3].y == 1 ? low_coord : 1;
1090     }
1091
1092     glBegin(GL_QUADS);
1093         glTexCoord3iv((GLint *) &coords[0]);
1094         glVertex2i(0, 0);
1095
1096         glTexCoord3iv((GLint *) &coords[1]);
1097         glVertex2i(0, surface->pow2Height);
1098
1099         glTexCoord3iv((GLint *) &coords[2]);
1100         glVertex2i(surface->pow2Width, surface->pow2Height);
1101
1102         glTexCoord3iv((GLint *) &coords[3]);
1103         glVertex2i(surface->pow2Width, 0);
1104     glEnd();
1105     checkGLcall("glEnd");
1106
1107     if(surface->glDescription.target != GL_TEXTURE_2D) {
1108         glEnable(GL_TEXTURE_2D);
1109         checkGLcall("glEnable(GL_TEXTURE_2D)");
1110         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1111         checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
1112     }
1113     LEAVE_GL();
1114 }
1115
1116 /* Routine common to the draw primitive and draw indexed primitive routines */
1117 void drawPrimitive(IWineD3DDevice *iface,
1118                    int PrimitiveType,
1119                    long NumPrimitives,
1120                    /* for Indexed: */
1121                    long  StartVertexIndex,
1122                    UINT  numberOfVertices,
1123                    long  StartIdx,
1124                    short idxSize,
1125                    const void *idxData,
1126                    int   minIndex) {
1127
1128     IWineD3DDeviceImpl           *This = (IWineD3DDeviceImpl *)iface;
1129     IWineD3DSwapChain            *swapchain;
1130     IWineD3DBaseTexture          *texture = NULL;
1131     IWineD3DSurfaceImpl          *target;
1132     int i;
1133
1134     /* Signals other modules that a drawing is in progress and the stateblock finalized */
1135     This->isInDraw = TRUE;
1136
1137     /* Invalidate the back buffer memory so LockRect will read it the next time */
1138     for(i = 0; i < GL_LIMITS(buffers); i++) {
1139         target = (IWineD3DSurfaceImpl *) This->render_targets[i];
1140
1141         /* TODO: Only do all that if we're going to change anything
1142          * Texture container dirtification does not work quite right yet
1143          */
1144         if(target /*&& target->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)*/) {
1145             swapchain = NULL;
1146             texture = NULL;
1147
1148             if(i == 0) {
1149                 IWineD3DSurface_GetContainer((IWineD3DSurface *) target, &IID_IWineD3DSwapChain, (void **)&swapchain);
1150
1151                 /* Need the surface in the drawable! */
1152                 if(!(target->Flags & SFLAG_INDRAWABLE) && (swapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
1153                     blt_to_drawable(This, target);
1154                 }
1155
1156                 if(swapchain) {
1157                     /* Onscreen target. Invalidate system memory copy and texture copy */
1158                     target->Flags &= ~(SFLAG_INSYSMEM | SFLAG_INTEXTURE);
1159                     target->Flags |= SFLAG_INDRAWABLE;
1160                     IWineD3DSwapChain_Release(swapchain);
1161                 } else if(wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
1162                     /* Non-FBO target: Invalidate system copy, texture copy and dirtify the container */
1163                     IWineD3DSurface_GetContainer((IWineD3DSurface *) target, &IID_IWineD3DBaseTexture, (void **)&texture);
1164
1165                     if(texture) {
1166                         IWineD3DBaseTexture_SetDirty(texture, TRUE);
1167                         IWineD3DTexture_Release(texture);
1168                     }
1169
1170                     target->Flags &= ~(SFLAG_INSYSMEM | SFLAG_INTEXTURE);
1171                     target->Flags |= SFLAG_INDRAWABLE;
1172                 } else {
1173                     /* FBO offscreen target. Invalidate system memory copy */
1174                     target->Flags &= ~SFLAG_INSYSMEM;
1175                 }
1176             } else {
1177                 /* Must be an fbo render target */
1178                 target->Flags &= ~SFLAG_INSYSMEM;
1179                 target->Flags |=  SFLAG_INTEXTURE;
1180             }
1181         }
1182     }
1183
1184     /* Ok, we will be updating the screen from here onwards so grab the lock */
1185     ENTER_GL();
1186
1187     ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
1188
1189     if (TRACE_ON(d3d_draw) && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1190         check_fbo_status(iface);
1191     }
1192
1193     if (This->depth_copy_state == WINED3D_DCS_COPY) {
1194         depth_copy(iface);
1195     }
1196     This->depth_copy_state = WINED3D_DCS_INITIAL;
1197
1198     {
1199         GLenum glPrimType;
1200         /* Ok, Work out which primitive is requested and how many vertexes that
1201            will be                                                              */
1202         UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
1203         if (numberOfVertices == 0 )
1204             numberOfVertices = calculatedNumberOfindices;
1205
1206         if (This->useDrawStridedSlow) {
1207             /* Immediate mode drawing */
1208             drawStridedSlow(iface, &This->strided_streams, calculatedNumberOfindices,
1209                             glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1210         } else if(This->instancedDraw) {
1211             /* Instancing emulation with mixing immediate mode and arrays */
1212             drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
1213                             idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1214         } else {
1215             /* Simple array draw call */
1216             drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
1217                             idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1218         }
1219     }
1220
1221     /* Finshed updating the screen, restore lock */
1222     LEAVE_GL();
1223     TRACE("Done all gl drawing\n");
1224
1225     /* Diagnostics */
1226 #ifdef SHOW_FRAME_MAKEUP
1227     {
1228         static long int primCounter = 0;
1229         /* NOTE: set primCounter to the value reported by drawprim 
1230            before you want to to write frame makeup to /tmp */
1231         if (primCounter >= 0) {
1232             WINED3DLOCKED_RECT r;
1233             char buffer[80];
1234             IWineD3DSurface_LockRect(This->renderTarget, &r, NULL, WINED3DLOCK_READONLY);
1235             sprintf(buffer, "/tmp/backbuffer_%d.tga", primCounter);
1236             TRACE("Saving screenshot %s\n", buffer);
1237             IWineD3DSurface_SaveSnapshot(This->renderTarget, buffer);
1238             IWineD3DSurface_UnlockRect(This->renderTarget);
1239
1240 #ifdef SHOW_TEXTURE_MAKEUP
1241            {
1242             IWineD3DSurface *pSur;
1243             int textureNo;
1244             for (textureNo = 0; textureNo < GL_LIMITS(textures); ++textureNo) {
1245                 if (This->stateBlock->textures[textureNo] != NULL) {
1246                     sprintf(buffer, "/tmp/texture_%p_%d_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
1247                     TRACE("Saving texture %s\n", buffer);
1248                     if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
1249                             IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
1250                             IWineD3DSurface_SaveSnapshot(pSur, buffer);
1251                             IWineD3DSurface_Release(pSur);
1252                     } else  {
1253                         FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
1254                     }
1255                 }
1256             }
1257            }
1258 #endif
1259         }
1260         TRACE("drawprim #%d\n", primCounter);
1261         ++primCounter;
1262     }
1263 #endif
1264
1265     /* Control goes back to the device, stateblock values may change again */
1266     This->isInDraw = FALSE;
1267 }