wined3d: Instancing emulation.
[wine/multimedia.git] / dlls / wined3d / drawprim.c
blob1d2a66ed2f8572f66649461b7fa5652244ec06b7
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 Henri Verbeet
9 * Copyright 2007 Stefan Dösinger for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION ((IWineD3DImpl *)(This->wineD3D))->gl_info
32 #include <stdio.h>
34 #if 0 /* TODO */
35 extern IWineD3DVertexShaderImpl* VertexShaders[64];
36 extern IWineD3DVertexDeclarationImpl* VertexShaderDeclarations[64];
37 extern IWineD3DPixelShaderImpl* PixelShaders[64];
39 #undef GL_VERSION_1_4 /* To be fixed, caused by mesa headers */
40 #endif
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)
47 DWORD NumVertexes = NumPrimitives;
49 switch (PrimitiveType) {
50 case WINED3DPT_POINTLIST:
51 TRACE("POINTS\n");
52 *primType = GL_POINTS;
53 NumVertexes = NumPrimitives;
54 break;
56 case WINED3DPT_LINELIST:
57 TRACE("LINES\n");
58 *primType = GL_LINES;
59 NumVertexes = NumPrimitives * 2;
60 break;
62 case WINED3DPT_LINESTRIP:
63 TRACE("LINE_STRIP\n");
64 *primType = GL_LINE_STRIP;
65 NumVertexes = NumPrimitives + 1;
66 break;
68 case WINED3DPT_TRIANGLELIST:
69 TRACE("TRIANGLES\n");
70 *primType = GL_TRIANGLES;
71 NumVertexes = NumPrimitives * 3;
72 break;
74 case WINED3DPT_TRIANGLESTRIP:
75 TRACE("TRIANGLE_STRIP\n");
76 *primType = GL_TRIANGLE_STRIP;
77 NumVertexes = NumPrimitives + 2;
78 break;
80 case WINED3DPT_TRIANGLEFAN:
81 TRACE("TRIANGLE_FAN\n");
82 *primType = GL_TRIANGLE_FAN;
83 NumVertexes = NumPrimitives + 2;
84 break;
86 default:
87 FIXME("Unhandled primitive\n");
88 *primType = GL_POINTS;
89 break;
91 return NumVertexes;
94 static BOOL fixed_get_input(
95 BYTE usage, BYTE usage_idx,
96 unsigned int* regnum) {
98 *regnum = -1;
100 /* Those positions must have the order in the
101 * named part of the strided data */
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;
136 if (*regnum < 0) {
137 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
138 debug_d3ddeclusage(usage), usage_idx);
139 return FALSE;
141 return TRUE;
144 void primitiveDeclarationConvertToStridedData(
145 IWineD3DDevice *iface,
146 BOOL useVertexShaderFunction,
147 WineDirect3DVertexStridedData *strided,
148 BOOL *fixup) {
150 /* We need to deal with frequency data!*/
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;
162 memset(isPreLoaded, 0, sizeof(isPreLoaded));
164 /* Translate the declaration into strided data */
165 for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
166 GLint streamVBO = 0;
167 BOOL stride_used;
168 unsigned int idx;
170 element = vertexDeclaration->pDeclarationWine + i;
171 TRACE("%p Element %p (%d of %d)\n", vertexDeclaration->pDeclarationWine,
172 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
174 if (This->stateBlock->streamSource[element->Stream] == NULL)
175 continue;
177 if (This->stateBlock->streamIsUP) {
178 TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
179 streamVBO = 0;
180 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
181 if(fixup && *fixup) FIXME("Missing fixed and unfixed vertices, expect graphics glitches\n");
182 } else {
183 TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
184 if(!isPreLoaded[element->Stream]) {
185 preLoadStreams[numPreloadStreams] = element->Stream;
186 numPreloadStreams++;
187 isPreLoaded[element->Stream] = 1;
189 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
190 if(fixup) {
191 if( streamVBO != 0) *fixup = TRUE;
192 else if(*fixup) FIXME("Missing fixed and unfixed vertices, expect graphics glitches\n");
195 stride = This->stateBlock->streamStride[element->Stream];
196 data += element->Offset;
197 reg = element->Reg;
199 TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
201 if (useVertexShaderFunction)
202 stride_used = vshader_get_input(This->stateBlock->vertexShader,
203 element->Usage, element->UsageIndex, &idx);
204 else
205 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
207 if (stride_used) {
208 TRACE("Loaded %s array %u [usage=%s, usage_idx=%u, "
209 "stream=%u, offset=%u, stride=%u, VBO=%u]\n",
210 useVertexShaderFunction? "shader": "fixed function", idx,
211 debug_d3ddeclusage(element->Usage), element->UsageIndex,
212 element->Stream, element->Offset, stride, streamVBO);
214 strided->u.input[idx].lpData = data;
215 strided->u.input[idx].dwType = element->Type;
216 strided->u.input[idx].dwStride = stride;
217 strided->u.input[idx].VBO = streamVBO;
218 strided->u.input[idx].streamNo = element->Stream;
219 if (!useVertexShaderFunction) {
220 if (element->Usage == WINED3DDECLUSAGE_POSITION)
221 strided->u.s.position_transformed = FALSE;
222 else if (element->Usage == WINED3DDECLUSAGE_POSITIONT)
223 strided->u.s.position_transformed = TRUE;
227 /* Now call PreLoad on all the vertex buffers. In the very rare case
228 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
229 * The vertex buffer can now use the strided structure in the device instead of finding its
230 * own again.
232 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
233 * once in there.
235 for(i=0; i < numPreloadStreams; i++) {
236 IWineD3DVertexBuffer_PreLoad(This->stateBlock->streamSource[preLoadStreams[i]]);
240 void primitiveConvertFVFtoOffset(DWORD thisFVF, DWORD stride, BYTE *data, WineDirect3DVertexStridedData *strided, GLint streamVBO, UINT streamNo) {
241 int numBlends;
242 int numTextures;
243 int textureNo;
244 int coordIdxInfo = 0x00; /* Information on number of coords supplied */
245 int numCoords[8]; /* Holding place for WINED3DFVF_TEXTUREFORMATx */
247 /* Either 3 or 4 floats depending on the FVF */
248 /* FIXME: Can blending data be in a different stream to the position data?
249 and if so using the fixed pipeline how do we handle it */
250 if (thisFVF & WINED3DFVF_POSITION_MASK) {
251 strided->u.s.position.lpData = data;
252 strided->u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
253 strided->u.s.position.dwStride = stride;
254 strided->u.s.position.VBO = streamVBO;
255 strided->u.s.position.streamNo = streamNo;
256 data += 3 * sizeof(float);
257 if (thisFVF & WINED3DFVF_XYZRHW) {
258 strided->u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
259 strided->u.s.position_transformed = TRUE;
260 data += sizeof(float);
261 } else
262 strided->u.s.position_transformed = FALSE;
265 /* Blending is numBlends * FLOATs followed by a DWORD for UBYTE4 */
266 /** do we have to Check This->stateBlock->renderState[D3DRS_INDEXEDVERTEXBLENDENABLE] ? */
267 numBlends = 1 + (((thisFVF & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
268 if(thisFVF & WINED3DFVF_LASTBETA_UBYTE4) numBlends--;
270 if ((thisFVF & WINED3DFVF_XYZB5 ) > WINED3DFVF_XYZRHW) {
271 TRACE("Setting blend Weights to %p\n", data);
272 strided->u.s.blendWeights.lpData = data;
273 strided->u.s.blendWeights.dwType = WINED3DDECLTYPE_FLOAT1 + numBlends - 1;
274 strided->u.s.blendWeights.dwStride = stride;
275 strided->u.s.blendWeights.VBO = streamVBO;
276 strided->u.s.blendWeights.streamNo = streamNo;
277 data += numBlends * sizeof(FLOAT);
279 if (thisFVF & WINED3DFVF_LASTBETA_UBYTE4) {
280 strided->u.s.blendMatrixIndices.lpData = data;
281 strided->u.s.blendMatrixIndices.dwType = WINED3DDECLTYPE_UBYTE4;
282 strided->u.s.blendMatrixIndices.dwStride= stride;
283 strided->u.s.blendMatrixIndices.VBO = streamVBO;
284 strided->u.s.blendMatrixIndices.streamNo= streamNo;
285 data += sizeof(DWORD);
289 /* Normal is always 3 floats */
290 if (thisFVF & WINED3DFVF_NORMAL) {
291 strided->u.s.normal.lpData = data;
292 strided->u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
293 strided->u.s.normal.dwStride = stride;
294 strided->u.s.normal.VBO = streamVBO;
295 strided->u.s.normal.streamNo = streamNo;
296 data += 3 * sizeof(FLOAT);
299 /* Pointsize is a single float */
300 if (thisFVF & WINED3DFVF_PSIZE) {
301 strided->u.s.pSize.lpData = data;
302 strided->u.s.pSize.dwType = WINED3DDECLTYPE_FLOAT1;
303 strided->u.s.pSize.dwStride = stride;
304 strided->u.s.pSize.VBO = streamVBO;
305 strided->u.s.pSize.streamNo = streamNo;
306 data += sizeof(FLOAT);
309 /* Diffuse is 4 unsigned bytes */
310 if (thisFVF & WINED3DFVF_DIFFUSE) {
311 strided->u.s.diffuse.lpData = data;
312 strided->u.s.diffuse.dwType = WINED3DDECLTYPE_SHORT4;
313 strided->u.s.diffuse.dwStride = stride;
314 strided->u.s.diffuse.VBO = streamVBO;
315 strided->u.s.diffuse.streamNo = streamNo;
316 data += sizeof(DWORD);
319 /* Specular is 4 unsigned bytes */
320 if (thisFVF & WINED3DFVF_SPECULAR) {
321 strided->u.s.specular.lpData = data;
322 strided->u.s.specular.dwType = WINED3DDECLTYPE_SHORT4;
323 strided->u.s.specular.dwStride = stride;
324 strided->u.s.specular.VBO = streamVBO;
325 strided->u.s.specular.streamNo = streamNo;
326 data += sizeof(DWORD);
329 /* Texture coords */
330 numTextures = (thisFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
331 coordIdxInfo = (thisFVF & 0x00FF0000) >> 16; /* 16 is from definition of WINED3DFVF_TEXCOORDSIZE1, and is 8 (0-7 stages) * 2bits long */
333 /* numTextures indicates the number of texture coordinates supplied */
334 /* However, the first set may not be for stage 0 texture - it all */
335 /* depends on WINED3DTSS_TEXCOORDINDEX. */
336 /* The number of bytes for each coordinate set is based off */
337 /* WINED3DFVF_TEXCOORDSIZEn, which are the bottom 2 bits */
339 /* So, for each supplied texture extract the coords */
340 for (textureNo = 0; textureNo < numTextures; ++textureNo) {
342 strided->u.s.texCoords[textureNo].lpData = data;
343 strided->u.s.texCoords[textureNo].dwType = WINED3DDECLTYPE_FLOAT1;
344 strided->u.s.texCoords[textureNo].dwStride = stride;
345 strided->u.s.texCoords[textureNo].VBO = streamVBO;
346 strided->u.s.texCoords[textureNo].streamNo = streamNo;
347 numCoords[textureNo] = coordIdxInfo & 0x03;
349 /* Always one set */
350 data += sizeof(float);
351 if (numCoords[textureNo] != WINED3DFVF_TEXTUREFORMAT1) {
352 strided->u.s.texCoords[textureNo].dwType = WINED3DDECLTYPE_FLOAT2;
353 data += sizeof(float);
354 if (numCoords[textureNo] != WINED3DFVF_TEXTUREFORMAT2) {
355 strided->u.s.texCoords[textureNo].dwType = WINED3DDECLTYPE_FLOAT3;
356 data += sizeof(float);
357 if (numCoords[textureNo] != WINED3DFVF_TEXTUREFORMAT3) {
358 strided->u.s.texCoords[textureNo].dwType = WINED3DDECLTYPE_FLOAT4;
359 data += sizeof(float);
363 coordIdxInfo = coordIdxInfo >> 2; /* Drop bottom two bits */
367 void primitiveConvertToStridedData(IWineD3DDevice *iface, WineDirect3DVertexStridedData *strided, BOOL *fixup) {
369 short LoopThroughTo = 0;
370 short nStream;
371 GLint streamVBO = 0;
372 DWORD preLoadStreams[MAX_STREAMS], numPreloadStreams = 0;
374 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
376 /* OK, Now to setup the data locations
377 For the non-created vertex shaders, the VertexShader var holds the real
378 FVF and only stream 0 matters
379 For the created vertex shaders, there is an FVF per stream */
380 if (!This->stateBlock->streamIsUP && !(This->stateBlock->vertexShader == NULL)) {
381 LoopThroughTo = MAX_STREAMS;
382 } else {
383 LoopThroughTo = 1;
386 /* Work through stream by stream */
387 for (nStream=0; nStream<LoopThroughTo; ++nStream) {
388 DWORD stride = This->stateBlock->streamStride[nStream];
389 BYTE *data = NULL;
390 DWORD thisFVF = 0;
392 /* Skip empty streams */
393 if (This->stateBlock->streamSource[nStream] == NULL) continue;
395 /* Retrieve appropriate FVF */
396 if (LoopThroughTo == 1) { /* Use FVF, not vertex shader */
397 thisFVF = This->stateBlock->fvf;
398 /* Handle memory passed directly as well as vertex buffers */
399 if (This->stateBlock->streamIsUP) {
400 streamVBO = 0;
401 data = (BYTE *)This->stateBlock->streamSource[nStream];
402 } else {
403 /* The for loop should iterate through here only once per stream, so we don't need magic to prevent double loading
404 * buffers
406 preLoadStreams[numPreloadStreams] = nStream;
407 numPreloadStreams++;
408 /* GetMemory binds the VBO */
409 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[nStream], 0, &streamVBO);
410 if(fixup) {
411 if(streamVBO != 0 ) *fixup = TRUE;
414 } else {
415 #if 0 /* TODO: Vertex shader support */
416 thisFVF = This->stateBlock->vertexShaderDecl->fvf[nStream];
417 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[nStream], 0);
418 #endif
420 VTRACE(("FVF for stream %d is %lx\n", nStream, thisFVF));
421 if (thisFVF == 0) continue;
423 /* Now convert the stream into pointers */
424 primitiveConvertFVFtoOffset(thisFVF, stride, data, strided, streamVBO, nStream);
426 /* Now call PreLoad on all the vertex buffers. In the very rare case
427 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
428 * The vertex buffer can now use the strided structure in the device instead of finding its
429 * own again.
431 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
432 * once in there.
434 for(nStream=0; nStream < numPreloadStreams; nStream++) {
435 IWineD3DVertexBuffer_PreLoad(This->stateBlock->streamSource[preLoadStreams[nStream]]);
439 #if 0 /* TODO: Software Shaders */
440 /* Draw a single vertex using this information */
441 static void draw_vertex(IWineD3DDevice *iface, /* interface */
442 BOOL isXYZ, float x, float y, float z, float rhw, /* xyzn position*/
443 BOOL isNormal, float nx, float ny, float nz, /* normal */
444 BOOL isDiffuse, float *dRGBA, /* 1st colors */
445 BOOL isSpecular, float *sRGB, /* 2ndry colors */
446 BOOL isPtSize, float ptSize, /* pointSize */
447 WINED3DVECTOR_4 *texcoords, int *numcoords) /* texture info */
449 unsigned int textureNo;
450 float s, t, r, q;
451 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
453 /* Diffuse -------------------------------- */
454 if (isDiffuse) {
455 glColor4fv(dRGBA);
456 VTRACE(("glColor4f: r,g,b,a=%f,%f,%f,%f\n", dRGBA[0], dRGBA[1], dRGBA[2], dRGBA[3]));
459 /* Specular Colour ------------------------------------------*/
460 if (isSpecular) {
461 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
462 GL_EXTCALL(glSecondaryColor3fvEXT(sRGB));
463 VTRACE(("glSecondaryColor4f: r,g,b=%f,%f,%f\n", sRGB[0], sRGB[1], sRGB[2]));
464 } else {
465 VTRACE(("Specular color extensions not supplied\n"));
469 /* Normal -------------------------------- */
470 if (isNormal) {
471 VTRACE(("glNormal:nx,ny,nz=%f,%f,%f\n", nx,ny,nz));
472 glNormal3f(nx, ny, nz);
475 /* Point Size ----------------------------------------------*/
476 if (isPtSize) {
478 /* no such functionality in the fixed function GL pipeline */
479 FIXME("Cannot change ptSize here in openGl\n");
482 /* Texture coords --------------------------- */
483 for (textureNo = 0; textureNo < GL_LIMITS(textures); ++textureNo) {
485 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
486 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
487 continue ;
490 /* Query tex coords */
491 if (This->stateBlock->textures[textureNo] != NULL) {
493 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
494 if (coordIdx >= MAX_TEXTURES) {
495 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
496 continue;
497 } else if (numcoords[coordIdx] == 0) {
498 TRACE("tex: %d - Skipping tex coords, as no data supplied or no coords supplied\n", textureNo);
499 continue;
500 } else {
502 /* Initialize vars */
503 s = 0.0f;
504 t = 0.0f;
505 r = 0.0f;
506 q = 0.0f;
508 switch (numcoords[coordIdx]) {
509 case 4: q = texcoords[coordIdx].w; /* drop through */
510 case 3: r = texcoords[coordIdx].z; /* drop through */
511 case 2: t = texcoords[coordIdx].y; /* drop through */
512 case 1: s = texcoords[coordIdx].x;
515 switch (numcoords[coordIdx]) { /* Supply the provided texture coords */
516 case WINED3DTTFF_COUNT1:
517 VTRACE(("tex:%d, s=%f\n", textureNo, s));
518 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
519 GLMULTITEXCOORD1F(textureNo, s);
520 } else {
521 glTexCoord1f(s);
523 break;
524 case WINED3DTTFF_COUNT2:
525 VTRACE(("tex:%d, s=%f, t=%f\n", textureNo, s, t));
526 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
527 GLMULTITEXCOORD2F(textureNo, s, t);
528 } else {
529 glTexCoord2f(s, t);
531 break;
532 case WINED3DTTFF_COUNT3:
533 VTRACE(("tex:%d, s=%f, t=%f, r=%f\n", textureNo, s, t, r));
534 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
535 GLMULTITEXCOORD3F(textureNo, s, t, r);
536 } else {
537 glTexCoord3f(s, t, r);
539 break;
540 case WINED3DTTFF_COUNT4:
541 VTRACE(("tex:%d, s=%f, t=%f, r=%f, q=%f\n", textureNo, s, t, r, q));
542 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
543 GLMULTITEXCOORD4F(textureNo, s, t, r, q);
544 } else {
545 glTexCoord4f(s, t, r, q);
547 break;
548 default:
549 FIXME("Should not get here as numCoords should be 0->4 (%x)!\n", numcoords[coordIdx]);
553 } /* End of textures */
555 /* Position -------------------------------- */
556 if (isXYZ) {
557 if (1.0f == rhw || rhw < 0.00001f) {
558 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f\n", x,y,z));
559 glVertex3f(x, y, z);
560 } else {
561 /* Cannot optimize by dividing through by rhw as rhw is required
562 later for perspective in the GL pipeline for vertex shaders */
563 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f / rhw=%f\n", x,y,z,rhw));
564 glVertex4f(x,y,z,rhw);
568 #endif /* TODO: Software shaders */
570 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
571 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
572 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
574 if (idxData != NULL /* This crashes sometimes!*/) {
575 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
576 idxData = idxData == (void *)-1 ? NULL : idxData;
577 #if 1
578 #if 0
579 glIndexPointer(idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idxSize, startIdx);
580 glEnableClientState(GL_INDEX_ARRAY);
581 #endif
582 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
583 (const char *)idxData+(idxSize * startIdx));
584 #else /* using drawRangeElements may be faster */
586 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
587 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
588 (const char *)idxData+(idxSize * startIdx));
589 #endif
590 checkGLcall("glDrawRangeElements");
592 } else {
594 /* Note first is now zero as we shuffled along earlier */
595 TRACE("(%p) : glDrawArrays(%x, 0, %d)\n", This, glPrimitiveType, numberOfVertices);
596 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
597 checkGLcall("glDrawArrays");
601 return;
605 * Actually draw using the supplied information.
606 * Slower GL version which extracts info about each vertex in turn
609 static void drawStridedSlow(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd,
610 UINT NumVertexes, GLenum glPrimType,
611 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
613 unsigned int textureNo = 0;
614 unsigned int texture_idx = 0;
615 const short *pIdxBufS = NULL;
616 const long *pIdxBufL = NULL;
617 LONG vx_index;
618 float x = 0.0f, y = 0.0f, z = 0.0f; /* x,y,z coordinates */
619 float rhw = 0.0f; /* rhw */
620 DWORD diffuseColor = 0xFFFFFFFF; /* Diffuse Color */
621 DWORD specularColor = 0; /* Specular Color */
622 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
623 UINT *streamOffset = This->stateBlock->streamOffset;
624 LONG SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
626 BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
627 BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
629 TRACE("Using slow vertex array code\n");
631 /* Variable Initialization */
632 if (idxData != NULL) {
633 if (idxSize == 2) pIdxBufS = (const short *) idxData;
634 else pIdxBufL = (const long *) idxData;
637 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
638 * to the strided Data in the device and might be needed intact on the next draw
640 for (textureNo = 0, texture_idx = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
641 if(sd->u.s.texCoords[textureNo].lpData) {
642 texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
643 } else {
644 texCoords[textureNo] = NULL;
647 if(sd->u.s.diffuse.lpData) {
648 diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
650 if(sd->u.s.specular.lpData) {
651 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
653 if(sd->u.s.normal.lpData) {
654 normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
656 if(sd->u.s.position.lpData) {
657 position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
660 /* Start drawing in GL */
661 VTRACE(("glBegin(%x)\n", glPrimType));
662 glBegin(glPrimType);
664 /* Default settings for data that is not passed */
665 if (sd->u.s.normal.lpData == NULL) {
666 glNormal3f(0, 0, 1);
668 if(sd->u.s.diffuse.lpData != NULL) {
669 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
671 if(sd->u.s.specular.lpData != NULL) {
672 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
673 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
677 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
678 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
681 /* For each primitive */
682 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
684 /* Initialize diffuse color */
685 diffuseColor = 0xFFFFFFFF;
687 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
688 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
691 /* For indexed data, we need to go a few more strides in */
692 if (idxData != NULL) {
694 /* Indexed so work out the number of strides to skip */
695 if (idxSize == 2) {
696 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
697 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
698 } else {
699 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
700 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
704 /* Texture coords --------------------------- */
705 for (textureNo = 0, texture_idx = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
707 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
708 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
709 continue ;
712 /* Query tex coords */
713 if (This->stateBlock->textures[textureNo] != NULL) {
715 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
716 float *ptrToCoords = NULL;
717 float s = 0.0, t = 0.0, r = 0.0, q = 0.0;
719 if (coordIdx > 7) {
720 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
721 ++texture_idx;
722 continue;
723 } else if (coordIdx < 0) {
724 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
725 ++texture_idx;
726 continue;
729 ptrToCoords = (float *)(texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride));
730 if (texCoords[coordIdx] == NULL) {
731 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
732 ++texture_idx;
733 continue;
734 } else {
736 int coordsToUse = sd->u.s.texCoords[coordIdx].dwType + 1; /* 0 == WINED3DDECLTYPE_FLOAT1 etc */
738 /* The coords to supply depend completely on the fvf / vertex shader */
739 switch (coordsToUse) {
740 case 4: q = ptrToCoords[3]; /* drop through */
741 case 3: r = ptrToCoords[2]; /* drop through */
742 case 2: t = ptrToCoords[1]; /* drop through */
743 case 1: s = ptrToCoords[0];
746 /* Projected is more 'fun' - Move the last coord to the 'q'
747 parameter (see comments under WINED3DTSS_TEXTURETRANSFORMFLAGS */
748 if ((This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] != WINED3DTTFF_DISABLE) &&
749 (This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED)) {
751 if (This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
752 switch (coordsToUse) {
753 case 0: /* Drop Through */
754 case 1:
755 FIXME("WINED3DTTFF_PROJECTED but only zero or one coordinate?\n");
756 break;
757 case 2:
758 q = t;
759 t = 0.0;
760 coordsToUse = 4;
761 break;
762 case 3:
763 q = r;
764 r = 0.0;
765 coordsToUse = 4;
766 break;
767 case 4: /* Nop here */
768 break;
769 default:
770 FIXME("Unexpected WINED3DTSS_TEXTURETRANSFORMFLAGS value of %d\n",
771 This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED);
776 switch (coordsToUse) { /* Supply the provided texture coords */
777 case WINED3DTTFF_COUNT1:
778 VTRACE(("tex:%d, s=%f\n", textureNo, s));
779 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
780 GL_EXTCALL(glMultiTexCoord1fARB(texture_idx, s));
781 } else {
782 glTexCoord1f(s);
784 break;
785 case WINED3DTTFF_COUNT2:
786 VTRACE(("tex:%d, s=%f, t=%f\n", textureNo, s, t));
787 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
788 GL_EXTCALL(glMultiTexCoord2fARB(texture_idx, s, t));
789 } else {
790 glTexCoord2f(s, t);
792 break;
793 case WINED3DTTFF_COUNT3:
794 VTRACE(("tex:%d, s=%f, t=%f, r=%f\n", textureNo, s, t, r));
795 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
796 GL_EXTCALL(glMultiTexCoord3fARB(texture_idx, s, t, r));
797 } else {
798 glTexCoord3f(s, t, r);
800 break;
801 case WINED3DTTFF_COUNT4:
802 VTRACE(("tex:%d, s=%f, t=%f, r=%f, q=%f\n", textureNo, s, t, r, q));
803 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
804 GL_EXTCALL(glMultiTexCoord4fARB(texture_idx, s, t, r, q));
805 } else {
806 glTexCoord4f(s, t, r, q);
808 break;
809 default:
810 FIXME("Should not get here as coordsToUse is two bits only (%x)!\n", coordsToUse);
814 if (/*!GL_SUPPORT(NV_REGISTER_COMBINERS) || This->stateBlock->textures[textureNo]*/TRUE) ++texture_idx;
815 } /* End of textures */
817 /* Diffuse -------------------------------- */
818 if (diffuse) {
819 DWORD *ptrToCoords = (DWORD *)(diffuse + (SkipnStrides * sd->u.s.diffuse.dwStride));
820 diffuseColor = ptrToCoords[0];
821 VTRACE(("diffuseColor=%lx\n", diffuseColor));
823 glColor4ub(D3DCOLOR_B_R(diffuseColor),
824 D3DCOLOR_B_G(diffuseColor),
825 D3DCOLOR_B_B(diffuseColor),
826 D3DCOLOR_B_A(diffuseColor));
827 VTRACE(("glColor4ub: r,g,b,a=%lu,%lu,%lu,%lu\n",
828 D3DCOLOR_B_R(diffuseColor),
829 D3DCOLOR_B_G(diffuseColor),
830 D3DCOLOR_B_B(diffuseColor),
831 D3DCOLOR_B_A(diffuseColor)));
834 /* Specular ------------------------------- */
835 if (specular) {
836 DWORD *ptrToCoords = (DWORD *)(specular + (SkipnStrides * sd->u.s.specular.dwStride));
837 specularColor = ptrToCoords[0];
838 VTRACE(("specularColor=%lx\n", specularColor));
840 /* special case where the fog density is stored in the diffuse alpha channel */
841 if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
842 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
843 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
844 if(GL_SUPPORT(EXT_FOG_COORD)) {
845 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
846 } else {
847 static BOOL warned = FALSE;
848 if(!warned) {
849 /* TODO: Use the fog table code from old ddraw */
850 FIXME("Implement fog for transformed vertices in software\n");
851 warned = TRUE;
856 VTRACE(("glSecondaryColor4ub: r,g,b=%lu,%lu,%lu\n",
857 D3DCOLOR_B_R(specularColor),
858 D3DCOLOR_B_G(specularColor),
859 D3DCOLOR_B_B(specularColor)));
860 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
861 GL_EXTCALL(glSecondaryColor3ubEXT)(
862 D3DCOLOR_B_R(specularColor),
863 D3DCOLOR_B_G(specularColor),
864 D3DCOLOR_B_B(specularColor));
865 } else {
866 /* Do not worry if specular colour missing and disable request */
867 VTRACE(("Specular color extensions not supplied\n"));
871 /* Normal -------------------------------- */
872 if (normal != NULL) {
873 float *ptrToCoords = (float *)(normal + (SkipnStrides * sd->u.s.normal.dwStride));
875 VTRACE(("glNormal:nx,ny,nz=%f,%f,%f\n", ptrToCoords[0], ptrToCoords[1], ptrToCoords[2]));
876 glNormal3f(ptrToCoords[0], ptrToCoords[1], ptrToCoords[2]);
879 /* Position -------------------------------- */
880 if (position) {
881 float *ptrToCoords = (float *)(position + (SkipnStrides * sd->u.s.position.dwStride));
882 x = ptrToCoords[0];
883 y = ptrToCoords[1];
884 z = ptrToCoords[2];
885 rhw = 1.0;
886 VTRACE(("x,y,z=%f,%f,%f\n", x,y,z));
888 /* RHW follows, only if transformed, ie 4 floats were provided */
889 if (sd->u.s.position_transformed) {
890 rhw = ptrToCoords[3];
891 VTRACE(("rhw=%f\n", rhw));
894 if (1.0f == rhw || ((rhw < eps) && (rhw > -eps))) {
895 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f\n", x,y,z));
896 glVertex3f(x, y, z);
897 } else {
898 GLfloat w = 1.0 / rhw;
899 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f / rhw=%f\n", x,y,z,rhw));
900 glVertex4f(x*w, y*w, z*w, w);
904 /* For non indexed mode, step onto next parts */
905 if (idxData == NULL) {
906 ++SkipnStrides;
910 glEnd();
911 checkGLcall("glEnd and previous calls");
914 #if 0 /* TODO: Software/Hardware vertex blending support */
916 * Draw with emulated vertex shaders
917 * Note: strided data is uninitialized, as we need to pass the vertex
918 * shader directly as ordering irs yet
920 void drawStridedSoftwareVS(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd,
921 int PrimitiveType, ULONG NumPrimitives,
922 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx) {
924 unsigned int textureNo = 0;
925 GLenum glPrimType = GL_POINTS;
926 int NumVertexes = NumPrimitives;
927 const short *pIdxBufS = NULL;
928 const long *pIdxBufL = NULL;
929 LONG SkipnStrides = 0;
930 LONG vx_index;
931 float x = 0.0f, y = 0.0f, z = 0.0f; /* x,y,z coordinates */
932 float rhw = 0.0f; /* rhw */
933 float ptSize = 0.0f; /* Point size */
934 D3DVECTOR_4 texcoords[8]; /* Texture Coords */
935 int numcoords[8]; /* Number of coords */
936 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
938 IDirect3DVertexShaderImpl* vertexShader = NULL;
940 TRACE("Using slow software vertex shader code\n");
942 /* Variable Initialization */
943 if (idxData != NULL) {
944 if (idxSize == 2) pIdxBufS = (const short *) idxData;
945 else pIdxBufL = (const long *) idxData;
948 /* Ok, Work out which primitive is requested and how many vertexes that will be */
949 NumVertexes = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
951 /* Retrieve the VS information */
952 vertexShader = (IWineD3DVertexShaderImp *)This->stateBlock->VertexShader;
954 /* Start drawing in GL */
955 VTRACE(("glBegin(%x)\n", glPrimType));
956 glBegin(glPrimType);
958 /* For each primitive */
959 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
961 /* For indexed data, we need to go a few more strides in */
962 if (idxData != NULL) {
964 /* Indexed so work out the number of strides to skip */
965 if (idxSize == 2) {
966 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
967 SkipnStrides = pIdxBufS[startIdx+vx_index];
968 } else {
969 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
970 SkipnStrides = pIdxBufL[startIdx+vx_index];
974 /* Fill the vertex shader input */
975 IDirect3DDeviceImpl_FillVertexShaderInputSW(This, vertexShader, SkipnStrides);
977 /* Initialize the output fields to the same defaults as it would normally have */
978 memset(&vertexShader->output, 0, sizeof(VSHADEROUTPUTDATA8));
979 vertexShader->output.oD[0].x = 1.0;
980 vertexShader->output.oD[0].y = 1.0;
981 vertexShader->output.oD[0].z = 1.0;
982 vertexShader->output.oD[0].w = 1.0;
984 /* Now execute the vertex shader */
985 IDirect3DVertexShaderImpl_ExecuteSW(vertexShader, &vertexShader->input, &vertexShader->output);
988 TRACE_VECTOR(vertexShader->output.oPos);
989 TRACE_VECTOR(vertexShader->output.oD[0]);
990 TRACE_VECTOR(vertexShader->output.oD[1]);
991 TRACE_VECTOR(vertexShader->output.oT[0]);
992 TRACE_VECTOR(vertexShader->output.oT[1]);
993 TRACE_VECTOR(vertexShader->input.V[0]);
994 TRACE_VECTOR(vertexShader->data->C[0]);
995 TRACE_VECTOR(vertexShader->data->C[1]);
996 TRACE_VECTOR(vertexShader->data->C[2]);
997 TRACE_VECTOR(vertexShader->data->C[3]);
998 TRACE_VECTOR(vertexShader->data->C[4]);
999 TRACE_VECTOR(vertexShader->data->C[5]);
1000 TRACE_VECTOR(vertexShader->data->C[6]);
1001 TRACE_VECTOR(vertexShader->data->C[7]);
1004 /* Extract out the output */
1005 /* FIXME: Fog coords? */
1006 x = vertexShader->output.oPos.x;
1007 y = vertexShader->output.oPos.y;
1008 z = vertexShader->output.oPos.z;
1009 rhw = vertexShader->output.oPos.w;
1010 ptSize = vertexShader->output.oPts.x; /* Fixme - Is this right? */
1012 /** Update textures coords using vertexShader->output.oT[0->7] */
1013 memset(texcoords, 0x00, sizeof(texcoords));
1014 memset(numcoords, 0x00, sizeof(numcoords));
1015 for (textureNo = 0; textureNo < GL_LIMITS(textures); ++textureNo) {
1016 if (This->stateBlock->textures[textureNo] != NULL) {
1017 texcoords[textureNo].x = vertexShader->output.oT[textureNo].x;
1018 texcoords[textureNo].y = vertexShader->output.oT[textureNo].y;
1019 texcoords[textureNo].z = vertexShader->output.oT[textureNo].z;
1020 texcoords[textureNo].w = vertexShader->output.oT[textureNo].w;
1021 if (This->stateBlock->texture_state[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] != WINED3DTTFF_DISABLE) {
1022 numcoords[textureNo] = This->stateBlock->texture_state[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & ~WINED3DTTFF_PROJECTED;
1023 } else {
1024 switch (IDirect3DBaseTexture8Impl_GetType((LPDIRECT3DBASETEXTURE8) This->stateBlock->textures[textureNo])) {
1025 case WINED3DRTYPE_TEXTURE: numcoords[textureNo] = 2; break;
1026 case WINED3DRTYPE_VOLUMETEXTURE: numcoords[textureNo] = 3; break;
1027 default: numcoords[textureNo] = 4;
1030 } else {
1031 numcoords[textureNo] = 0;
1035 /* Draw using this information */
1036 draw_vertex(iface,
1037 TRUE, x, y, z, rhw,
1038 TRUE, 0.0f, 0.0f, 1.0f,
1039 TRUE, (float*) &vertexShader->output.oD[0],
1040 TRUE, (float*) &vertexShader->output.oD[1],
1041 FALSE, ptSize, /* FIXME: Change back when supported */
1042 texcoords, numcoords);
1044 /* For non indexed mode, step onto next parts */
1045 if (idxData == NULL) {
1046 ++SkipnStrides;
1049 } /* for each vertex */
1051 glEnd();
1052 checkGLcall("glEnd and previous calls");
1055 #endif
1057 static void check_fbo_status(IWineD3DDevice *iface) {
1058 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1060 GLenum status = GL_EXTCALL(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
1061 switch(status) {
1062 case GL_FRAMEBUFFER_COMPLETE_EXT: TRACE("FBO complete.\n"); break;
1063 default: TRACE("FBO status %#x.\n", status); break;
1067 static void depth_blt(IWineD3DDevice *iface, GLuint texture) {
1068 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1069 GLint old_binding = 0;
1071 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
1073 glDisable(GL_CULL_FACE);
1074 glDisable(GL_BLEND);
1075 glDisable(GL_ALPHA_TEST);
1076 glDisable(GL_STENCIL_TEST);
1077 glEnable(GL_DEPTH_TEST);
1078 glDepthFunc(GL_ALWAYS);
1080 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
1081 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
1082 glBindTexture(GL_TEXTURE_2D, texture);
1083 glEnable(GL_TEXTURE_2D);
1085 This->shader_backend->shader_select_depth_blt(iface);
1087 glBegin(GL_TRIANGLE_STRIP);
1088 glVertex2f(-1.0f, -1.0f);
1089 glVertex2f(1.0f, -1.0f);
1090 glVertex2f(-1.0f, 1.0f);
1091 glVertex2f(1.0f, 1.0f);
1092 glEnd();
1094 glBindTexture(GL_TEXTURE_2D, old_binding);
1096 glPopAttrib();
1098 /* Reselect the old shaders. There doesn't seem to be any glPushAttrib bit for arb shaders,
1099 * and this seems easier and more efficient than providing the shader backend with a private
1100 * storage to read and restore the old shader settings
1102 This->shader_backend->shader_select(iface,
1103 This->stateBlock->pixelShader && ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.function,
1104 This->stateBlock->vertexShader && ((IWineD3DVertexShaderImpl *)This->stateBlock->vertexShader)->baseShader.function);
1107 static void depth_copy(IWineD3DDevice *iface) {
1108 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1109 IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *)This->depthStencilBuffer;
1111 /* Only copy the depth buffer if there is one. */
1112 if (!depth_stencil) return;
1114 /* TODO: Make this work for modes other than FBO */
1115 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
1117 if (This->render_offscreen) {
1118 static GLuint tmp_texture = 0;
1119 GLint old_binding = 0;
1121 TRACE("Copying onscreen depth buffer to offscreen surface\n");
1123 if (!tmp_texture) {
1124 glGenTextures(1, &tmp_texture);
1127 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
1128 * directly on the FBO texture. That's because we need to flip. */
1129 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
1130 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
1131 glBindTexture(GL_TEXTURE_2D, tmp_texture);
1132 glCopyTexImage2D(depth_stencil->glDescription.target,
1133 depth_stencil->glDescription.level,
1134 depth_stencil->glDescription.glFormatInternal,
1137 depth_stencil->currentDesc.Width,
1138 depth_stencil->currentDesc.Height,
1140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1142 glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
1143 glBindTexture(GL_TEXTURE_2D, old_binding);
1145 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, This->fbo));
1146 checkGLcall("glBindFramebuffer()");
1147 depth_blt(iface, tmp_texture);
1148 checkGLcall("depth_blt");
1149 } else {
1150 TRACE("Copying offscreen surface to onscreen depth buffer\n");
1152 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
1153 checkGLcall("glBindFramebuffer()");
1154 depth_blt(iface, depth_stencil->glDescription.textureName);
1155 checkGLcall("depth_blt");
1159 inline void drawStridedInstanced(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
1160 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
1161 ULONG startIdx, ULONG startVertex) {
1162 UINT numInstances = 0;
1163 int numInstancedAttribs = 0, i, j;
1164 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
1165 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
1166 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
1168 if (idxData == NULL) {
1169 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
1170 * We don't support this for now
1172 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
1173 * But the StreamSourceFreq value has a different meaning in that situation.
1175 FIXME("Non-indexed instanced drawing is not supported\n");
1176 return;
1179 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
1180 idxData = idxData == (void *)-1 ? NULL : idxData;
1182 /* First, figure out how many instances we have to draw */
1183 for(i = 0; i < MAX_STREAMS; i++) {
1184 /* Look at all non-instanced streams */
1185 if(!(stateblock->streamFlags[i] & D3DSTREAMSOURCE_INSTANCEDATA) &&
1186 stateblock->streamSource[i]) {
1187 int inst = stateblock->streamFreq[i];
1189 if(numInstances && inst != numInstances) {
1190 ERR("Two streams specify a different number of instances. Got %d, new is %d\n", numInstances, inst);
1192 numInstances = inst;
1196 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
1197 if(stateblock->streamFlags[sd->u.input[i].streamNo] & D3DSTREAMSOURCE_INSTANCEDATA) {
1198 instancedData[numInstancedAttribs] = i;
1199 numInstancedAttribs++;
1203 /* now draw numInstances instances :-) */
1204 for(i = 0; i < numInstances; i++) {
1205 /* Specify the instanced attributes using immediate mode calls */
1206 for(j = 0; j < numInstancedAttribs; j++) {
1207 BYTE *ptr = sd->u.input[instancedData[j]].lpData +
1208 sd->u.input[instancedData[j]].dwStride * i +
1209 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
1210 if(sd->u.input[instancedData[j]].VBO) {
1211 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
1212 ptr += (long) vb->resource.allocatedMemory;
1215 switch(sd->u.input[instancedData[j]].dwType) {
1216 case WINED3DDECLTYPE_FLOAT1:
1217 GL_EXTCALL(glVertexAttrib1fvARB(instancedData[j], (float *) ptr));
1218 break;
1219 case WINED3DDECLTYPE_FLOAT2:
1220 GL_EXTCALL(glVertexAttrib2fvARB(instancedData[j], (float *) ptr));
1221 break;
1222 case WINED3DDECLTYPE_FLOAT3:
1223 GL_EXTCALL(glVertexAttrib3fvARB(instancedData[j], (float *) ptr));
1224 break;
1225 case WINED3DDECLTYPE_FLOAT4:
1226 GL_EXTCALL(glVertexAttrib4fvARB(instancedData[j], (float *) ptr));
1227 break;
1229 case WINED3DDECLTYPE_UBYTE4:
1230 GL_EXTCALL(glVertexAttrib4NubvARB(instancedData[j], ptr));
1231 break;
1232 case WINED3DDECLTYPE_UBYTE4N:
1233 case WINED3DDECLTYPE_D3DCOLOR:
1234 GL_EXTCALL(glVertexAttrib4NubvARB(instancedData[j], ptr));
1235 break;
1237 case WINED3DDECLTYPE_SHORT2:
1238 GL_EXTCALL(glVertexAttrib4svARB(instancedData[j], (GLshort *) ptr));
1239 break;
1240 case WINED3DDECLTYPE_SHORT4:
1241 GL_EXTCALL(glVertexAttrib4svARB(instancedData[j], (GLshort *) ptr));
1242 break;
1244 case WINED3DDECLTYPE_SHORT2N:
1246 GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
1247 GL_EXTCALL(glVertexAttrib4NsvARB(instancedData[j], s));
1248 break;
1250 case WINED3DDECLTYPE_USHORT2N:
1252 GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
1253 GL_EXTCALL(glVertexAttrib4NusvARB(instancedData[j], s));
1254 break;
1256 case WINED3DDECLTYPE_SHORT4N:
1257 GL_EXTCALL(glVertexAttrib4NsvARB(instancedData[j], (GLshort *) ptr));
1258 break;
1259 case WINED3DDECLTYPE_USHORT4N:
1260 GL_EXTCALL(glVertexAttrib4NusvARB(instancedData[j], (GLushort *) ptr));
1261 break;
1263 case WINED3DDECLTYPE_UDEC3:
1264 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
1265 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
1266 break;
1267 case WINED3DDECLTYPE_DEC3N:
1268 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
1269 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
1270 break;
1272 case WINED3DDECLTYPE_FLOAT16_2:
1273 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
1274 * byte float according to the IEEE standard
1276 FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_2\n");
1277 break;
1278 case WINED3DDECLTYPE_FLOAT16_4:
1279 FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_4\n");
1280 break;
1282 case WINED3DDECLTYPE_UNUSED:
1283 default:
1284 ERR("Unexpected declaration in instanced attributes\n");
1285 break;
1289 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
1290 (const char *)idxData+(idxSize * startIdx));
1291 checkGLcall("glDrawElements");
1295 /* Routine common to the draw primitive and draw indexed primitive routines */
1296 void drawPrimitive(IWineD3DDevice *iface,
1297 int PrimitiveType,
1298 long NumPrimitives,
1299 /* for Indexed: */
1300 long StartVertexIndex,
1301 UINT numberOfVertices,
1302 long StartIdx,
1303 short idxSize,
1304 const void *idxData,
1305 int minIndex) {
1307 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1308 int i;
1310 /* Signals other modules that a drawing is in progress and the stateblock finalized */
1311 This->isInDraw = TRUE;
1313 /* Invalidate the back buffer memory so LockRect will read it the next time */
1314 for(i = 0; i < GL_LIMITS(buffers); i++) {
1315 if(This->render_targets[i]) {
1316 ((IWineD3DSurfaceImpl *) This->render_targets[i])->Flags |= SFLAG_GLDIRTY;
1320 /* Ok, we will be updating the screen from here onwards so grab the lock */
1321 ENTER_GL();
1323 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
1325 if (TRACE_ON(d3d_draw) && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1326 check_fbo_status(iface);
1329 if (This->depth_copy_state == WINED3D_DCS_COPY) {
1330 depth_copy(iface);
1332 This->depth_copy_state = WINED3D_DCS_INITIAL;
1335 GLenum glPrimType;
1336 /* Ok, Work out which primitive is requested and how many vertexes that
1337 will be */
1338 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
1339 if (numberOfVertices == 0 )
1340 numberOfVertices = calculatedNumberOfindices;
1342 if (This->useDrawStridedSlow) {
1343 /* Immediate mode drawing */
1344 drawStridedSlow(iface, &This->strided_streams, calculatedNumberOfindices,
1345 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1346 } else if(This->instancedDraw) {
1347 /* Instancing emulation with mixing immediate mode and arrays */
1348 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
1349 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1350 } else {
1351 /* Simple array draw call */
1352 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
1353 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1357 /* Finshed updating the screen, restore lock */
1358 LEAVE_GL();
1359 TRACE("Done all gl drawing\n");
1361 /* Diagnostics */
1362 #ifdef SHOW_FRAME_MAKEUP
1364 static long int primCounter = 0;
1365 /* NOTE: set primCounter to the value reported by drawprim
1366 before you want to to write frame makeup to /tmp */
1367 if (primCounter >= 0) {
1368 WINED3DLOCKED_RECT r;
1369 char buffer[80];
1370 IWineD3DSurface_LockRect(This->renderTarget, &r, NULL, WINED3DLOCK_READONLY);
1371 sprintf(buffer, "/tmp/backbuffer_%d.tga", primCounter);
1372 TRACE("Saving screenshot %s\n", buffer);
1373 IWineD3DSurface_SaveSnapshot(This->renderTarget, buffer);
1374 IWineD3DSurface_UnlockRect(This->renderTarget);
1376 #ifdef SHOW_TEXTURE_MAKEUP
1378 IWineD3DSurface *pSur;
1379 int textureNo;
1380 for (textureNo = 0; textureNo < GL_LIMITS(textures); ++textureNo) {
1381 if (This->stateBlock->textures[textureNo] != NULL) {
1382 sprintf(buffer, "/tmp/texture_%p_%d_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
1383 TRACE("Saving texture %s\n", buffer);
1384 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
1385 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
1386 IWineD3DSurface_SaveSnapshot(pSur, buffer);
1387 IWineD3DSurface_Release(pSur);
1388 } else {
1389 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
1394 #endif
1396 TRACE("drawprim #%d\n", primCounter);
1397 ++primCounter;
1399 #endif
1401 /* Control goes back to the device, stateblock values may change again */
1402 This->isInDraw = FALSE;