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-2008 Stefan Dösinger for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION This->adapter->gl_info
36 extern IWineD3DVertexShaderImpl* VertexShaders[64];
37 extern IWineD3DVertexDeclarationImpl* VertexShaderDeclarations[64];
38 extern IWineD3DPixelShaderImpl* PixelShaders[64];
40 #undef GL_VERSION_1_4 /* To be fixed, caused by mesa headers */
43 /* Issues the glBegin call for gl given the primitive type and count */
44 static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
48 DWORD NumVertexes = NumPrimitives;
50 switch (PrimitiveType) {
51 case WINED3DPT_POINTLIST:
53 *primType = GL_POINTS;
54 NumVertexes = NumPrimitives;
57 case WINED3DPT_LINELIST:
60 NumVertexes = NumPrimitives * 2;
63 case WINED3DPT_LINESTRIP:
64 TRACE("LINE_STRIP\n");
65 *primType = GL_LINE_STRIP;
66 NumVertexes = NumPrimitives + 1;
69 case WINED3DPT_TRIANGLELIST:
71 *primType = GL_TRIANGLES;
72 NumVertexes = NumPrimitives * 3;
75 case WINED3DPT_TRIANGLESTRIP:
76 TRACE("TRIANGLE_STRIP\n");
77 *primType = GL_TRIANGLE_STRIP;
78 NumVertexes = NumPrimitives + 2;
81 case WINED3DPT_TRIANGLEFAN:
82 TRACE("TRIANGLE_FAN\n");
83 *primType = GL_TRIANGLE_FAN;
84 NumVertexes = NumPrimitives + 2;
88 FIXME("Unhandled primitive\n");
89 *primType = GL_POINTS;
95 static BOOL fixed_get_input(
96 BYTE usage, BYTE usage_idx,
97 unsigned int* regnum) {
101 /* Those positions must have the order in the
102 * named part of the strided data */
104 if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 0)
106 else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
108 else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
110 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
112 else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
114 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
116 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
118 else if (usage == WINED3DDECLUSAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
119 *regnum = 7 + usage_idx;
120 else if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 1)
121 *regnum = 7 + WINED3DDP_MAXTEXCOORD;
122 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 1)
123 *regnum = 8 + WINED3DDP_MAXTEXCOORD;
124 else if (usage == WINED3DDECLUSAGE_TANGENT && usage_idx == 0)
125 *regnum = 9 + WINED3DDP_MAXTEXCOORD;
126 else if (usage == WINED3DDECLUSAGE_BINORMAL && usage_idx == 0)
127 *regnum = 10 + WINED3DDP_MAXTEXCOORD;
128 else if (usage == WINED3DDECLUSAGE_TESSFACTOR && usage_idx == 0)
129 *regnum = 11 + WINED3DDP_MAXTEXCOORD;
130 else if (usage == WINED3DDECLUSAGE_FOG && usage_idx == 0)
131 *regnum = 12 + WINED3DDP_MAXTEXCOORD;
132 else if (usage == WINED3DDECLUSAGE_DEPTH && usage_idx == 0)
133 *regnum = 13 + WINED3DDP_MAXTEXCOORD;
134 else if (usage == WINED3DDECLUSAGE_SAMPLE && usage_idx == 0)
135 *regnum = 14 + WINED3DDP_MAXTEXCOORD;
138 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
139 debug_d3ddeclusage(usage), usage_idx);
145 void primitiveDeclarationConvertToStridedData(
146 IWineD3DDevice *iface,
147 BOOL useVertexShaderFunction,
148 WineDirect3DVertexStridedData *strided,
151 /* We need to deal with frequency data!*/
154 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
155 IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
157 WINED3DVERTEXELEMENT *element;
159 DWORD numPreloadStreams = This->stateBlock->streamIsUP ? 0 : vertexDeclaration->num_streams;
160 DWORD *streams = vertexDeclaration->streams;
162 /* Check for transformed vertices, disable vertex shader if present */
163 strided->u.s.position_transformed = vertexDeclaration->position_transformed;
164 if(vertexDeclaration->position_transformed) {
165 useVertexShaderFunction = FALSE;
168 /* Translate the declaration into strided data */
169 for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
174 element = vertexDeclaration->pDeclarationWine + i;
175 TRACE("%p Element %p (%d of %d)\n", vertexDeclaration->pDeclarationWine,
176 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
178 if (This->stateBlock->streamSource[element->Stream] == NULL)
181 stride = This->stateBlock->streamStride[element->Stream];
182 if (This->stateBlock->streamIsUP) {
183 TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
185 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
187 TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
188 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
190 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
191 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
192 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
193 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
194 * not, drawStridedSlow is needed, including a vertex buffer path.
196 if(This->stateBlock->loadBaseVertexIndex < 0) {
197 WARN("loadBaseVertexIndex is < 0 (%d), not using vbos\n", This->stateBlock->loadBaseVertexIndex);
199 data = ((IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[element->Stream])->resource.allocatedMemory;
200 if((UINT_PTR)data < -This->stateBlock->loadBaseVertexIndex * stride) {
201 FIXME("System memory vertex data load offset is negative!\n");
206 if( streamVBO != 0) *fixup = TRUE;
207 else if(*fixup && !useVertexShaderFunction &&
208 (element->Usage == WINED3DDECLUSAGE_COLOR ||
209 element->Usage == WINED3DDECLUSAGE_POSITIONT)) {
210 /* This may be bad with the fixed function pipeline */
211 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
215 data += element->Offset;
217 TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
219 if (useVertexShaderFunction)
220 stride_used = vshader_get_input(This->stateBlock->vertexShader,
221 element->Usage, element->UsageIndex, &idx);
223 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
226 TRACE("Loaded %s array %u [usage=%s, usage_idx=%u, "
227 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
228 useVertexShaderFunction? "shader": "fixed function", idx,
229 debug_d3ddeclusage(element->Usage), element->UsageIndex,
230 element->Stream, element->Offset, stride, debug_d3ddecltype(element->Type), streamVBO);
232 strided->u.input[idx].lpData = data;
233 strided->u.input[idx].dwType = element->Type;
234 strided->u.input[idx].dwStride = stride;
235 strided->u.input[idx].VBO = streamVBO;
236 strided->u.input[idx].streamNo = element->Stream;
239 /* Now call PreLoad on all the vertex buffers. In the very rare case
240 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
241 * The vertex buffer can now use the strided structure in the device instead of finding its
244 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
247 for(i=0; i < numPreloadStreams; i++) {
248 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
250 IWineD3DVertexBuffer_PreLoad(vb);
255 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
256 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
257 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
259 if (idxSize != 0 /* This crashes sometimes!*/) {
260 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
261 idxData = idxData == (void *)-1 ? NULL : idxData;
263 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
264 (const char *)idxData+(idxSize * startIdx));
265 checkGLcall("glDrawElements");
266 #else /* using drawRangeElements may be faster */
268 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
269 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
270 (const char *)idxData+(idxSize * startIdx));
271 checkGLcall("glDrawRangeElements");
276 /* Note first is now zero as we shuffled along earlier */
277 TRACE("(%p) : glDrawArrays(%x, 0, %d)\n", This, glPrimitiveType, numberOfVertices);
278 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
279 checkGLcall("glDrawArrays");
287 * Actually draw using the supplied information.
288 * Slower GL version which extracts info about each vertex in turn
291 static void drawStridedSlow(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd,
292 UINT NumVertexes, GLenum glPrimType,
293 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
295 unsigned int textureNo = 0;
296 const WORD *pIdxBufS = NULL;
297 const DWORD *pIdxBufL = NULL;
299 DWORD diffuseColor = 0xFFFFFFFF; /* Diffuse Color */
300 DWORD specularColor = 0; /* Specular Color */
301 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
302 UINT *streamOffset = This->stateBlock->streamOffset;
303 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
304 BOOL pixelShader = use_ps(This);
306 BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
307 BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
309 TRACE("Using slow vertex array code\n");
311 /* Variable Initialization */
313 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
314 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
315 * idxData will be != NULL
317 if(idxData == NULL) {
318 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
321 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
322 else pIdxBufL = (const DWORD *) idxData;
325 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
326 * to the strided Data in the device and might be needed intact on the next draw
328 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
329 if(sd->u.s.texCoords[textureNo].lpData) {
330 texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
332 texCoords[textureNo] = NULL;
335 if(sd->u.s.diffuse.lpData) {
336 diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
338 if(sd->u.s.specular.lpData) {
339 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
341 if(sd->u.s.normal.lpData) {
342 normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
344 if(sd->u.s.position.lpData) {
345 position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
348 /* The texture coordinate types are not so easy to map into a common function signature - we're
349 * not using the vector functions here
351 if(FIXME_ON(d3d_draw)) {
352 for (textureNo = 0; textureNo < GL_LIMITS(textures); ++textureNo) {
353 DWORD type = sd->u.s.texCoords[textureNo].dwType;
354 if (sd->u.s.texCoords[textureNo].lpData &&
355 type != WINED3DDECLTYPE_FLOAT1 &&
356 type != WINED3DDECLTYPE_FLOAT2 &&
357 type != WINED3DDECLTYPE_FLOAT3 &&
358 type != WINED3DDECLTYPE_FLOAT4) {
359 FIXME("Implement fixed function texture coordinates from %s\n", debug_d3ddecltype(type));
362 if(specular && This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
363 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position_transformed )&&
364 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
365 if(GL_SUPPORT(EXT_FOG_COORD) && sd->u.s.specular.dwType != WINED3DDECLTYPE_D3DCOLOR) {
366 FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
369 if(This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR) {
370 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
374 /* Start drawing in GL */
375 VTRACE(("glBegin(%x)\n", glPrimType));
378 /* Default settings for data that is not passed */
379 if (sd->u.s.normal.lpData == NULL) {
382 if(sd->u.s.diffuse.lpData == NULL) {
383 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
385 if(sd->u.s.specular.lpData == NULL) {
386 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
387 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
391 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
392 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
395 /* For each primitive */
396 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
398 /* Initialize diffuse color */
399 diffuseColor = 0xFFFFFFFF;
401 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
402 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
405 /* For indexed data, we need to go a few more strides in */
406 if (idxData != NULL) {
408 /* Indexed so work out the number of strides to skip */
410 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
411 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
413 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
414 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
418 /* Texture coords --------------------------- */
419 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
421 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
422 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
426 /* Query tex coords */
427 if (This->stateBlock->textures[textureNo] != NULL || pixelShader) {
429 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
430 int texture_idx = This->texUnitMap[textureNo];
431 float *ptrToCoords = NULL;
432 float s = 0.0, t = 0.0, r = 0.0, q = 0.0;
435 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
437 } else if (coordIdx < 0) {
438 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
442 ptrToCoords = (float *)(texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride));
443 if (texCoords[coordIdx] == NULL) {
444 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
445 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
446 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
448 glTexCoord4f(0, 0, 0, 1);
452 int coordsToUse = sd->u.s.texCoords[coordIdx].dwType + 1; /* 0 == WINED3DDECLTYPE_FLOAT1 etc */
454 if (texture_idx == -1) continue;
456 /* The coords to supply depend completely on the fvf / vertex shader */
457 switch (coordsToUse) {
458 case 4: q = ptrToCoords[3]; /* drop through */
459 case 3: r = ptrToCoords[2]; /* drop through */
460 case 2: t = ptrToCoords[1]; /* drop through */
461 case 1: s = ptrToCoords[0];
464 switch (coordsToUse) { /* Supply the provided texture coords */
465 case WINED3DTTFF_COUNT1:
466 VTRACE(("tex:%d, s=%f\n", textureNo, s));
467 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
468 GL_EXTCALL(glMultiTexCoord1fARB(GL_TEXTURE0_ARB + texture_idx, s));
473 case WINED3DTTFF_COUNT2:
474 VTRACE(("tex:%d, s=%f, t=%f\n", textureNo, s, t));
475 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
476 GL_EXTCALL(glMultiTexCoord2fARB(GL_TEXTURE0_ARB + texture_idx, s, t));
481 case WINED3DTTFF_COUNT3:
482 VTRACE(("tex:%d, s=%f, t=%f, r=%f\n", textureNo, s, t, r));
483 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
484 GL_EXTCALL(glMultiTexCoord3fARB(GL_TEXTURE0_ARB + texture_idx, s, t, r));
486 glTexCoord3f(s, t, r);
489 case WINED3DTTFF_COUNT4:
490 VTRACE(("tex:%d, s=%f, t=%f, r=%f, q=%f\n", textureNo, s, t, r, q));
491 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
492 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, s, t, r, q));
494 glTexCoord4f(s, t, r, q);
498 FIXME("Should not get here as coordsToUse is two bits only (%x)!\n", coordsToUse);
502 } /* End of textures */
504 /* Diffuse -------------------------------- */
506 DWORD *ptrToCoords = (DWORD *)(diffuse + (SkipnStrides * sd->u.s.diffuse.dwStride));
508 diffuse_funcs[sd->u.s.diffuse.dwType]((void *) ptrToCoords);
509 if(This->activeContext->num_untracked_materials) {
513 diffuseColor = ptrToCoords[0];
514 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
515 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
516 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
517 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
519 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
520 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
525 /* Specular ------------------------------- */
527 DWORD *ptrToCoords = (DWORD *)(specular + (SkipnStrides * sd->u.s.specular.dwStride));
529 /* special case where the fog density is stored in the specular alpha channel */
530 if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
531 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
532 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
533 if(GL_SUPPORT(EXT_FOG_COORD)) {
534 specularColor = ptrToCoords[0];
535 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
537 static BOOL warned = FALSE;
539 /* TODO: Use the fog table code from old ddraw */
540 FIXME("Implement fog for transformed vertices in software\n");
546 specular_funcs[sd->u.s.specular.dwType]((void *) ptrToCoords);
549 /* Normal -------------------------------- */
550 if (normal != NULL) {
551 float *ptrToCoords = (float *)(normal + (SkipnStrides * sd->u.s.normal.dwStride));
552 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
555 /* Position -------------------------------- */
557 float *ptrToCoords = (float *)(position + (SkipnStrides * sd->u.s.position.dwStride));
558 position_funcs[sd->u.s.position.dwType](ptrToCoords);
561 /* For non indexed mode, step onto next parts */
562 if (idxData == NULL) {
568 checkGLcall("glEnd and previous calls");
571 static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
573 case WINED3DDECLTYPE_FLOAT1:
574 GL_EXTCALL(glVertexAttrib1fvARB(index, (float *) ptr));
576 case WINED3DDECLTYPE_FLOAT2:
577 GL_EXTCALL(glVertexAttrib2fvARB(index, (float *) ptr));
579 case WINED3DDECLTYPE_FLOAT3:
580 GL_EXTCALL(glVertexAttrib3fvARB(index, (float *) ptr));
582 case WINED3DDECLTYPE_FLOAT4:
583 GL_EXTCALL(glVertexAttrib4fvARB(index, (float *) ptr));
586 case WINED3DDECLTYPE_UBYTE4:
587 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
589 case WINED3DDECLTYPE_UBYTE4N:
590 case WINED3DDECLTYPE_D3DCOLOR:
591 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
594 case WINED3DDECLTYPE_SHORT2:
595 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
597 case WINED3DDECLTYPE_SHORT4:
598 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
601 case WINED3DDECLTYPE_SHORT2N:
603 GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
604 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
607 case WINED3DDECLTYPE_USHORT2N:
609 GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
610 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
613 case WINED3DDECLTYPE_SHORT4N:
614 GL_EXTCALL(glVertexAttrib4NsvARB(index, (GLshort *) ptr));
616 case WINED3DDECLTYPE_USHORT4N:
617 GL_EXTCALL(glVertexAttrib4NusvARB(index, (GLushort *) ptr));
620 case WINED3DDECLTYPE_UDEC3:
621 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
622 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
624 case WINED3DDECLTYPE_DEC3N:
625 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
626 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
629 case WINED3DDECLTYPE_FLOAT16_2:
630 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
631 * byte float according to the IEEE standard
633 if (GL_SUPPORT(NV_HALF_FLOAT)) {
634 GL_EXTCALL(glVertexAttrib2hvNV(index, (GLhalfNV *)ptr));
636 float x = float_16_to_32(((unsigned short *) ptr) + 0);
637 float y = float_16_to_32(((unsigned short *) ptr) + 1);
638 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
641 case WINED3DDECLTYPE_FLOAT16_4:
642 if (GL_SUPPORT(NV_HALF_FLOAT)) {
643 GL_EXTCALL(glVertexAttrib4hvNV(index, (GLhalfNV *)ptr));
645 float x = float_16_to_32(((unsigned short *) ptr) + 0);
646 float y = float_16_to_32(((unsigned short *) ptr) + 1);
647 float z = float_16_to_32(((unsigned short *) ptr) + 2);
648 float w = float_16_to_32(((unsigned short *) ptr) + 3);
649 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
653 case WINED3DDECLTYPE_UNUSED:
655 ERR("Unexpected attribute declaration: %d\n", type);
660 static void drawStridedSlowVs(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
661 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx,
664 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
665 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
666 const WORD *pIdxBufS = NULL;
667 const DWORD *pIdxBufL = NULL;
670 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
674 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
675 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
676 * idxData will be != NULL
678 if(idxData == NULL) {
679 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
682 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
683 else pIdxBufL = (const DWORD *) idxData;
686 /* Start drawing in GL */
687 VTRACE(("glBegin(%x)\n", glPrimType));
688 glBegin(glPrimitiveType);
690 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
691 if (idxData != NULL) {
693 /* Indexed so work out the number of strides to skip */
695 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
696 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
698 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
699 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
703 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
704 if(!sd->u.input[i].lpData) continue;
706 ptr = sd->u.input[i].lpData +
707 sd->u.input[i].dwStride * SkipnStrides +
708 stateblock->streamOffset[sd->u.input[i].streamNo];
710 send_attribute(This, sd->u.input[i].dwType, i, ptr);
718 static void depth_blt(IWineD3DDevice *iface, GLuint texture) {
719 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
720 GLint old_binding = 0;
722 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
724 glDisable(GL_CULL_FACE);
726 glDisable(GL_ALPHA_TEST);
727 glDisable(GL_SCISSOR_TEST);
728 glDisable(GL_STENCIL_TEST);
729 glEnable(GL_DEPTH_TEST);
730 glDepthFunc(GL_ALWAYS);
731 glBlendFunc(GL_ZERO, GL_ONE);
733 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
734 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
735 glBindTexture(GL_TEXTURE_2D, texture);
736 glEnable(GL_TEXTURE_2D);
738 This->shader_backend->shader_select_depth_blt(iface);
740 glBegin(GL_TRIANGLE_STRIP);
741 glVertex2f(-1.0f, -1.0f);
742 glVertex2f(1.0f, -1.0f);
743 glVertex2f(-1.0f, 1.0f);
744 glVertex2f(1.0f, 1.0f);
747 glBindTexture(GL_TEXTURE_2D, old_binding);
751 /* Reselect the old shaders. There doesn't seem to be any glPushAttrib bit for arb shaders,
752 * and this seems easier and more efficient than providing the shader backend with a private
753 * storage to read and restore the old shader settings
755 This->shader_backend->shader_select(iface, use_ps(This), use_vs(This));
758 void depth_copy(IWineD3DDevice *iface) {
759 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
760 IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *)This->auto_depth_stencil_buffer;
762 /* Only copy the depth buffer if there is one. */
763 if (!depth_stencil) return;
765 /* TODO: Make this work for modes other than FBO */
766 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
768 if (depth_stencil->current_renderbuffer) {
769 FIXME("Not supported with fixed up depth stencil\n");
773 if (This->render_offscreen) {
774 GLint old_binding = 0;
776 TRACE("Copying onscreen depth buffer to offscreen surface\n");
778 if (!This->depth_blt_texture) {
779 glGenTextures(1, &This->depth_blt_texture);
782 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
783 * directly on the FBO texture. That's because we need to flip. */
784 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
785 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
786 glBindTexture(GL_TEXTURE_2D, This->depth_blt_texture);
787 glCopyTexImage2D(depth_stencil->glDescription.target,
788 depth_stencil->glDescription.level,
789 depth_stencil->glDescription.glFormatInternal,
792 depth_stencil->currentDesc.Width,
793 depth_stencil->currentDesc.Height,
795 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
796 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
797 glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
798 glBindTexture(GL_TEXTURE_2D, old_binding);
800 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, This->fbo));
801 checkGLcall("glBindFramebuffer()");
802 depth_blt(iface, This->depth_blt_texture);
803 checkGLcall("depth_blt");
805 TRACE("Copying offscreen surface to onscreen depth buffer\n");
807 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
808 checkGLcall("glBindFramebuffer()");
809 depth_blt(iface, depth_stencil->glDescription.textureName);
810 checkGLcall("depth_blt");
814 static inline void drawStridedInstanced(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
815 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
816 ULONG startIdx, ULONG startVertex) {
817 UINT numInstances = 0;
818 int numInstancedAttribs = 0, i, j;
819 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
820 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
821 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
824 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
825 * We don't support this for now
827 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
828 * But the StreamSourceFreq value has a different meaning in that situation.
830 FIXME("Non-indexed instanced drawing is not supported\n");
834 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
835 idxData = idxData == (void *)-1 ? NULL : idxData;
837 /* First, figure out how many instances we have to draw */
838 for(i = 0; i < MAX_STREAMS; i++) {
839 /* Look at the streams and take the first one which matches */
840 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
841 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
842 if(stateblock->streamFreq[i] == 0){
845 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
847 break; /* break, because only the first suitable value is interesting */
851 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
852 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
853 instancedData[numInstancedAttribs] = i;
854 numInstancedAttribs++;
858 /* now draw numInstances instances :-) */
859 for(i = 0; i < numInstances; i++) {
860 /* Specify the instanced attributes using immediate mode calls */
861 for(j = 0; j < numInstancedAttribs; j++) {
862 BYTE *ptr = sd->u.input[instancedData[j]].lpData +
863 sd->u.input[instancedData[j]].dwStride * i +
864 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
865 if(sd->u.input[instancedData[j]].VBO) {
866 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
867 ptr += (long) vb->resource.allocatedMemory;
870 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
873 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
874 (const char *)idxData+(idxSize * startIdx));
875 checkGLcall("glDrawElements");
879 static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
881 IWineD3DVertexBufferImpl *vb;
883 if(s->u.s.position.VBO) {
884 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
885 s->u.s.position.VBO = 0;
886 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
888 if(s->u.s.blendWeights.VBO) {
889 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
890 s->u.s.blendWeights.VBO = 0;
891 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
893 if(s->u.s.blendMatrixIndices.VBO) {
894 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
895 s->u.s.blendMatrixIndices.VBO = 0;
896 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
898 if(s->u.s.normal.VBO) {
899 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
900 s->u.s.normal.VBO = 0;
901 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
903 if(s->u.s.pSize.VBO) {
904 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
905 s->u.s.pSize.VBO = 0;
906 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
908 if(s->u.s.diffuse.VBO) {
909 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
910 s->u.s.diffuse.VBO = 0;
911 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
913 if(s->u.s.specular.VBO) {
914 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
915 s->u.s.specular.VBO = 0;
916 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
918 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
919 if(s->u.s.texCoords[i].VBO) {
920 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
921 s->u.s.texCoords[i].VBO = 0;
922 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
925 if(s->u.s.position2.VBO) {
926 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
927 s->u.s.position2.VBO = 0;
928 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
930 if(s->u.s.normal2.VBO) {
931 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
932 s->u.s.normal2.VBO = 0;
933 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
935 if(s->u.s.tangent.VBO) {
936 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
937 s->u.s.tangent.VBO = 0;
938 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
940 if(s->u.s.binormal.VBO) {
941 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
942 s->u.s.binormal.VBO = 0;
943 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
945 if(s->u.s.tessFactor.VBO) {
946 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
947 s->u.s.tessFactor.VBO = 0;
948 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
951 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
953 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
955 if(s->u.s.depth.VBO) {
956 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
957 s->u.s.depth.VBO = 0;
958 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
960 if(s->u.s.sample.VBO) {
961 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
962 s->u.s.sample.VBO = 0;
963 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
967 /* Routine common to the draw primitive and draw indexed primitive routines */
968 void drawPrimitive(IWineD3DDevice *iface,
972 long StartVertexIndex,
973 UINT numberOfVertices,
979 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
980 IWineD3DSwapChain *swapchain;
981 IWineD3DBaseTexture *texture = NULL;
982 IWineD3DSurfaceImpl *target;
985 if (NumPrimitives == 0) return;
987 /* Invalidate the back buffer memory so LockRect will read it the next time */
988 for(i = 0; i < GL_LIMITS(buffers); i++) {
989 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
991 /* TODO: Only do all that if we're going to change anything
992 * Texture container dirtification does not work quite right yet
994 if(target /*&& target->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)*/) {
999 IWineD3DSurface_GetContainer((IWineD3DSurface *) target, &IID_IWineD3DSwapChain, (void **)&swapchain);
1001 /* Need the surface in the drawable! */
1002 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
1004 /* TODO: Move fbo logic to ModifyLocation */
1005 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
1007 /* Onscreen target. Invalidate system memory copy and texture copy */
1008 IWineD3DSwapChain_Release(swapchain);
1009 } else if(wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
1010 /* Non-FBO target: Invalidate system copy, texture copy and dirtify the container */
1011 /* TODO: Move container dirtification to ModifyLocation */
1012 IWineD3DSurface_GetContainer((IWineD3DSurface *) target, &IID_IWineD3DBaseTexture, (void **)&texture);
1015 IWineD3DBaseTexture_SetDirty(texture, TRUE);
1016 IWineD3DTexture_Release(texture);
1019 /* FBO offscreen target. Texture == Drawable */
1020 target->Flags |= SFLAG_INTEXTURE;
1023 /* Must be an fbo render target */
1024 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
1025 target->Flags |= SFLAG_INTEXTURE;
1030 /* Signals other modules that a drawing is in progress and the stateblock finalized */
1031 This->isInDraw = TRUE;
1033 /* Ok, we will be updating the screen from here onwards so grab the lock */
1035 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1037 apply_fbo_state(iface);
1041 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
1044 if (This->depth_copy_state == WINED3D_DCS_COPY) {
1047 This->depth_copy_state = WINED3D_DCS_INITIAL;
1051 BOOL emulation = FALSE;
1052 WineDirect3DVertexStridedData *strided = &This->strided_streams;
1053 WineDirect3DVertexStridedData stridedlcl;
1054 /* Ok, Work out which primitive is requested and how many vertexes that
1056 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
1057 if (numberOfVertices == 0 )
1058 numberOfVertices = calculatedNumberOfindices;
1061 if(!This->strided_streams.u.s.position_transformed && This->activeContext->num_untracked_materials &&
1062 This->stateBlock->renderState[WINED3DRS_LIGHTING]) {
1063 static BOOL first = TRUE;
1065 FIXME("Using software emulation because not all material properties could be tracked\n");
1068 TRACE("Using software emulation because not all material properties could be tracked\n");
1072 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
1073 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
1074 * to a float in the vertex buffer
1076 static BOOL first = TRUE;
1078 FIXME("Using software emulation because manual fog coordinates are provided\n");
1081 TRACE("Using software emulation because manual fog coordinates are provided\n");
1087 strided = &stridedlcl;
1088 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
1089 remove_vbos(This, &stridedlcl);
1093 if (This->useDrawStridedSlow || emulation) {
1094 /* Immediate mode drawing */
1096 static BOOL first = TRUE;
1098 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
1101 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
1103 drawStridedSlowVs(iface, strided, calculatedNumberOfindices, glPrimType,
1104 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1106 drawStridedSlow(iface, strided, calculatedNumberOfindices,
1107 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1109 } else if(This->instancedDraw) {
1110 /* Instancing emulation with mixing immediate mode and arrays */
1111 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
1112 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1114 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
1115 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1119 /* Finished updating the screen, restore lock */
1121 TRACE("Done all gl drawing\n");
1124 #ifdef SHOW_FRAME_MAKEUP
1126 static long int primCounter = 0;
1127 /* NOTE: set primCounter to the value reported by drawprim
1128 before you want to to write frame makeup to /tmp */
1129 if (primCounter >= 0) {
1130 WINED3DLOCKED_RECT r;
1132 IWineD3DSurface_LockRect(This->renderTarget, &r, NULL, WINED3DLOCK_READONLY);
1133 sprintf(buffer, "/tmp/backbuffer_%d.tga", primCounter);
1134 TRACE("Saving screenshot %s\n", buffer);
1135 IWineD3DSurface_SaveSnapshot(This->renderTarget, buffer);
1136 IWineD3DSurface_UnlockRect(This->renderTarget);
1138 #ifdef SHOW_TEXTURE_MAKEUP
1140 IWineD3DSurface *pSur;
1142 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
1143 if (This->stateBlock->textures[textureNo] != NULL) {
1144 sprintf(buffer, "/tmp/texture_%p_%d_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
1145 TRACE("Saving texture %s\n", buffer);
1146 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
1147 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
1148 IWineD3DSurface_SaveSnapshot(pSur, buffer);
1149 IWineD3DSurface_Release(pSur);
1151 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
1158 TRACE("drawprim #%d\n", primCounter);
1163 /* Control goes back to the device, stateblock values may change again */
1164 This->isInDraw = FALSE;
1167 static void normalize_normal(float *n) {
1168 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
1169 if(length == 0.0) return;
1170 length = sqrt(length);
1171 n[0] = n[0] / length;
1172 n[1] = n[1] / length;
1173 n[2] = n[2] / length;
1176 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
1178 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
1179 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1180 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
1181 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1184 * To read back, the opengl feedback mode is used. This creates a proplem because we want
1185 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1186 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1187 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1188 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1190 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1191 * resulting colors back to the normals.
1193 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1194 * does not restore it because normally a draw follows immediately afterwards. The caller is
1195 * responsible of taking care that either the gl states are restored, or the context activated
1196 * for drawing to reset the lastWasBlit flag.
1198 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1199 struct WineD3DRectPatch *patch) {
1200 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1201 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1202 WineDirect3DVertexStridedData strided;
1204 WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1206 GLenum feedback_type;
1207 GLfloat *feedbuffer;
1209 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1212 memset(&strided, 0, sizeof(strided));
1213 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1214 if(strided.u.s.position.VBO) {
1215 IWineD3DVertexBufferImpl *vb;
1216 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1217 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1218 (unsigned long) vb->resource.allocatedMemory);
1220 vtxStride = strided.u.s.position.dwStride;
1221 data = strided.u.s.position.lpData +
1222 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1223 vtxStride * info->StartVertexOffsetWidth;
1225 /* Not entirely sure about what happens with transformed vertices */
1226 if(strided.u.s.position_transformed) {
1227 FIXME("Transformed position in rectpatch generation\n");
1229 if(vtxStride % sizeof(GLfloat)) {
1230 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1231 * I don't see how the stride could not be a multiple of 4, but make sure
1234 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1236 if(info->Basis != WINED3DBASIS_BEZIER) {
1237 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1239 if(info->Degree != WINED3DDEGREE_CUBIC) {
1240 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1243 /* First, get the boundary cube of the input data */
1244 for(j = 0; j < info->Height; j++) {
1245 for(i = 0; i < info->Width; i++) {
1246 float *v = (float *) (data + vtxStride * i + vtxStride * info->Stride * j);
1247 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1248 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1249 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1250 if(v[2] < neg_z) neg_z = v[2];
1254 /* This needs some improvements in the vertex decl code */
1255 FIXME("Cannot find data to generate. Only generating position and normals\n");
1256 patch->has_normals = TRUE;
1257 patch->has_texcoords = FALSE;
1259 /* Simply activate the context for blitting. This disables all the things we don't want and
1260 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1261 * patch (as opposed to normal draws) will most likely need different changes anyway
1263 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1266 glMatrixMode(GL_PROJECTION);
1267 checkGLcall("glMatrixMode(GL_PROJECTION)");
1269 checkGLcall("glLoadIndentity()");
1270 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1271 glTranslatef(0, 0, 0.5);
1272 checkGLcall("glScalef");
1273 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1274 checkGLcall("glViewport");
1276 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1277 * our feedback buffer parser
1279 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1280 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1281 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1282 if(patch->has_normals) {
1283 float black[4] = {0, 0, 0, 0};
1284 float red[4] = {1, 0, 0, 0};
1285 float green[4] = {0, 1, 0, 0};
1286 float blue[4] = {0, 0, 1, 0};
1287 float white[4] = {1, 1, 1, 1};
1288 glEnable(GL_LIGHTING);
1289 checkGLcall("glEnable(GL_LIGHTING)");
1290 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1291 checkGLcall("glLightModel for MODEL_AMBIENT");
1292 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1294 for(i = 3; i < GL_LIMITS(lights); i++) {
1295 glDisable(GL_LIGHT0 + i);
1296 checkGLcall("glDisable(GL_LIGHT0 + i)");
1297 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1300 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1301 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1302 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1303 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1304 glLightfv(GL_LIGHT0, GL_POSITION, red);
1305 glEnable(GL_LIGHT0);
1306 checkGLcall("Setting up light 1\n");
1307 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1308 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1309 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1310 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1311 glLightfv(GL_LIGHT1, GL_POSITION, green);
1312 glEnable(GL_LIGHT1);
1313 checkGLcall("Setting up light 2\n");
1314 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1315 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1316 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1317 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1318 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1319 glEnable(GL_LIGHT2);
1320 checkGLcall("Setting up light 3\n");
1322 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1323 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1324 glDisable(GL_COLOR_MATERIAL);
1325 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1326 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1327 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1328 checkGLcall("Setting up materials\n");
1331 /* Enable the needed maps.
1332 * GL_MAP2_VERTEX_3 is needed for positional data.
1333 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1334 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1336 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1337 out_vertex_size = 3 /* position */;
1338 d3d_out_vertex_size = 3;
1339 glEnable(GL_MAP2_VERTEX_3);
1340 if(patch->has_normals && patch->has_texcoords) {
1341 FIXME("Texcoords not handled yet\n");
1342 feedback_type = GL_3D_COLOR_TEXTURE;
1343 out_vertex_size += 8;
1344 d3d_out_vertex_size += 7;
1345 glEnable(GL_AUTO_NORMAL);
1346 glEnable(GL_MAP2_TEXTURE_COORD_4);
1347 } else if(patch->has_texcoords) {
1348 FIXME("Texcoords not handled yet\n");
1349 feedback_type = GL_3D_COLOR_TEXTURE;
1350 out_vertex_size += 7;
1351 d3d_out_vertex_size += 4;
1352 glEnable(GL_MAP2_TEXTURE_COORD_4);
1353 } else if(patch->has_normals) {
1354 feedback_type = GL_3D_COLOR;
1355 out_vertex_size += 4;
1356 d3d_out_vertex_size += 3;
1357 glEnable(GL_AUTO_NORMAL);
1359 feedback_type = GL_3D;
1361 checkGLcall("glEnable vertex attrib generation");
1363 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1364 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1365 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1367 glMap2f(GL_MAP2_VERTEX_3,
1368 0, 1, vtxStride / sizeof(float), info->Width,
1369 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1371 checkGLcall("glMap2f");
1372 if(patch->has_texcoords) {
1373 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1374 0, 1, vtxStride / sizeof(float), info->Width,
1375 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1377 checkGLcall("glMap2f");
1379 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1380 checkGLcall("glMapGrid2f");
1382 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1383 checkGLcall("glFeedbackBuffer");
1384 glRenderMode(GL_FEEDBACK);
1386 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1387 checkGLcall("glEvalMesh2\n");
1389 i = glRenderMode(GL_RENDER);
1392 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1394 HeapFree(GetProcessHeap(), 0, feedbuffer);
1395 return WINED3DERR_DRIVERINTERNALERROR;
1396 } else if(i != buffer_size) {
1398 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1400 HeapFree(GetProcessHeap(), 0, feedbuffer);
1401 return WINED3DERR_DRIVERINTERNALERROR;
1403 TRACE("Got %d elements as expected\n", i);
1406 HeapFree(GetProcessHeap(), 0, patch->mem);
1407 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1409 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1410 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1411 ERR("Unexpected token: %f\n", feedbuffer[j]);
1414 if(feedbuffer[j + 1] != 3) {
1415 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1418 /* Somehow there are different ideas about back / front facing, so fix up the
1421 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1422 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1423 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1424 if(patch->has_normals) {
1425 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1426 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1427 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1429 i += d3d_out_vertex_size;
1431 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1432 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1433 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1434 if(patch->has_normals) {
1435 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1436 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1437 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1439 i += d3d_out_vertex_size;
1441 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1442 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1443 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1444 if(patch->has_normals) {
1445 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1446 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1447 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1449 i += d3d_out_vertex_size;
1452 if(patch->has_normals) {
1453 /* Now do the same with reverse light directions */
1454 float x[4] = {-1, 0, 0, 0};
1455 float y[4] = { 0, -1, 0, 0};
1456 float z[4] = { 0, 0, -1, 0};
1457 glLightfv(GL_LIGHT0, GL_POSITION, x);
1458 glLightfv(GL_LIGHT1, GL_POSITION, y);
1459 glLightfv(GL_LIGHT2, GL_POSITION, z);
1460 checkGLcall("Setting up reverse light directions\n");
1462 glRenderMode(GL_FEEDBACK);
1463 checkGLcall("glRenderMode(GL_FEEDBACK)");
1464 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1465 checkGLcall("glEvalMesh2\n");
1466 i = glRenderMode(GL_RENDER);
1467 checkGLcall("glRenderMode(GL_RENDER)");
1470 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1471 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1472 ERR("Unexpected token: %f\n", feedbuffer[j]);
1475 if(feedbuffer[j + 1] != 3) {
1476 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1479 if(patch->mem[i + 3] == 0.0)
1480 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1481 if(patch->mem[i + 4] == 0.0)
1482 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1483 if(patch->mem[i + 5] == 0.0)
1484 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1485 normalize_normal(patch->mem + i + 3);
1486 i += d3d_out_vertex_size;
1488 if(patch->mem[i + 3] == 0.0)
1489 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1490 if(patch->mem[i + 4] == 0.0)
1491 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1492 if(patch->mem[i + 5] == 0.0)
1493 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1494 normalize_normal(patch->mem + i + 3);
1495 i += d3d_out_vertex_size;
1497 if(patch->mem[i + 3] == 0.0)
1498 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1499 if(patch->mem[i + 4] == 0.0)
1500 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1501 if(patch->mem[i + 5] == 0.0)
1502 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1503 normalize_normal(patch->mem + i + 3);
1504 i += d3d_out_vertex_size;
1508 glDisable(GL_MAP2_VERTEX_3);
1509 glDisable(GL_AUTO_NORMAL);
1510 glDisable(GL_MAP2_NORMAL);
1511 glDisable(GL_MAP2_TEXTURE_COORD_4);
1512 checkGLcall("glDisable vertex attrib generation");
1515 HeapFree(GetProcessHeap(), 0, feedbuffer);
1517 vtxStride = 3 * sizeof(float);
1518 if(patch->has_normals) {
1519 vtxStride += 3 * sizeof(float);
1521 if(patch->has_texcoords) {
1522 vtxStride += 4 * sizeof(float);
1524 memset(&patch->strided, 0, sizeof(&patch->strided));
1525 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1526 patch->strided.u.s.position.dwStride = vtxStride;
1527 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1528 patch->strided.u.s.position.streamNo = 255;
1530 if(patch->has_normals) {
1531 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1532 patch->strided.u.s.normal.dwStride = vtxStride;
1533 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1534 patch->strided.u.s.normal.streamNo = 255;
1536 if(patch->has_texcoords) {
1537 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1538 if(patch->has_normals) {
1539 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1541 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1542 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1543 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1544 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1547 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;