push 2f3ca95c4974ba229fa47d638b3044f50788f3bd
[wine/hacks.git] / dlls / wined3d / drawprim.c
blobc9a8f4a26a3f3113616d0914ebc4c62578829591
1 /*
2 * WINED3D draw functions
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006, 2008 Henri Verbeet
9 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION This->adapter->gl_info
32 #include <stdio.h>
33 #include <math.h>
35 /* Issues the glBegin call for gl given the primitive type and count */
36 static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
37 DWORD NumPrimitives,
38 GLenum *primType)
40 DWORD NumVertexes = NumPrimitives;
42 switch (PrimitiveType) {
43 case WINED3DPT_POINTLIST:
44 TRACE("POINTS\n");
45 *primType = GL_POINTS;
46 NumVertexes = NumPrimitives;
47 break;
49 case WINED3DPT_LINELIST:
50 TRACE("LINES\n");
51 *primType = GL_LINES;
52 NumVertexes = NumPrimitives * 2;
53 break;
55 case WINED3DPT_LINESTRIP:
56 TRACE("LINE_STRIP\n");
57 *primType = GL_LINE_STRIP;
58 NumVertexes = NumPrimitives + 1;
59 break;
61 case WINED3DPT_TRIANGLELIST:
62 TRACE("TRIANGLES\n");
63 *primType = GL_TRIANGLES;
64 NumVertexes = NumPrimitives * 3;
65 break;
67 case WINED3DPT_TRIANGLESTRIP:
68 TRACE("TRIANGLE_STRIP\n");
69 *primType = GL_TRIANGLE_STRIP;
70 NumVertexes = NumPrimitives + 2;
71 break;
73 case WINED3DPT_TRIANGLEFAN:
74 TRACE("TRIANGLE_FAN\n");
75 *primType = GL_TRIANGLE_FAN;
76 NumVertexes = NumPrimitives + 2;
77 break;
79 default:
80 FIXME("Unhandled primitive\n");
81 *primType = GL_POINTS;
82 break;
84 return NumVertexes;
87 static BOOL fixed_get_input(
88 BYTE usage, BYTE usage_idx,
89 unsigned int* regnum) {
91 *regnum = -1;
93 /* Those positions must have the order in the
94 * named part of the strided data */
96 if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 0)
97 *regnum = 0;
98 else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
99 *regnum = 1;
100 else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
101 *regnum = 2;
102 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
103 *regnum = 3;
104 else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
105 *regnum = 4;
106 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
107 *regnum = 5;
108 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
109 *regnum = 6;
110 else if (usage == WINED3DDECLUSAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
111 *regnum = 7 + usage_idx;
112 else if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 1)
113 *regnum = 7 + WINED3DDP_MAXTEXCOORD;
114 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 1)
115 *regnum = 8 + WINED3DDP_MAXTEXCOORD;
116 else if (usage == WINED3DDECLUSAGE_TANGENT && usage_idx == 0)
117 *regnum = 9 + WINED3DDP_MAXTEXCOORD;
118 else if (usage == WINED3DDECLUSAGE_BINORMAL && usage_idx == 0)
119 *regnum = 10 + WINED3DDP_MAXTEXCOORD;
120 else if (usage == WINED3DDECLUSAGE_TESSFACTOR && usage_idx == 0)
121 *regnum = 11 + WINED3DDP_MAXTEXCOORD;
122 else if (usage == WINED3DDECLUSAGE_FOG && usage_idx == 0)
123 *regnum = 12 + WINED3DDP_MAXTEXCOORD;
124 else if (usage == WINED3DDECLUSAGE_DEPTH && usage_idx == 0)
125 *regnum = 13 + WINED3DDP_MAXTEXCOORD;
126 else if (usage == WINED3DDECLUSAGE_SAMPLE && usage_idx == 0)
127 *regnum = 14 + WINED3DDP_MAXTEXCOORD;
129 if (*regnum == -1) {
130 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
131 debug_d3ddeclusage(usage), usage_idx);
132 return FALSE;
134 return TRUE;
137 void primitiveDeclarationConvertToStridedData(
138 IWineD3DDevice *iface,
139 BOOL useVertexShaderFunction,
140 WineDirect3DVertexStridedData *strided,
141 BOOL *fixup) {
143 /* We need to deal with frequency data!*/
145 const BYTE *data = NULL;
146 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
147 IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
148 unsigned int i;
149 const WINED3DVERTEXELEMENT *element;
150 DWORD stride;
151 DWORD numPreloadStreams = This->stateBlock->streamIsUP ? 0 : vertexDeclaration->num_streams;
152 const DWORD *streams = vertexDeclaration->streams;
154 /* Check for transformed vertices, disable vertex shader if present */
155 strided->position_transformed = vertexDeclaration->position_transformed;
156 if(vertexDeclaration->position_transformed) {
157 useVertexShaderFunction = FALSE;
160 /* Translate the declaration into strided data */
161 strided->swizzle_map = 0;
162 for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
163 GLint streamVBO = 0;
164 BOOL stride_used;
165 unsigned int idx;
167 element = vertexDeclaration->pDeclarationWine + i;
168 TRACE("%p Element %p (%u of %u)\n", vertexDeclaration->pDeclarationWine,
169 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
171 if (This->stateBlock->streamSource[element->Stream] == NULL)
172 continue;
174 stride = This->stateBlock->streamStride[element->Stream];
175 if (This->stateBlock->streamIsUP) {
176 TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
177 streamVBO = 0;
178 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
179 } else {
180 TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
181 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
183 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
184 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
185 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
186 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
187 * not, drawStridedSlow is needed, including a vertex buffer path.
189 if(This->stateBlock->loadBaseVertexIndex < 0) {
190 WARN("loadBaseVertexIndex is < 0 (%d), not using vbos\n", This->stateBlock->loadBaseVertexIndex);
191 streamVBO = 0;
192 data = ((IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[element->Stream])->resource.allocatedMemory;
193 if((UINT_PTR)data < -This->stateBlock->loadBaseVertexIndex * stride) {
194 FIXME("System memory vertex data load offset is negative!\n");
198 if(fixup) {
199 if( streamVBO != 0) *fixup = TRUE;
200 else if(*fixup && !useVertexShaderFunction &&
201 (element->Usage == WINED3DDECLUSAGE_COLOR ||
202 element->Usage == WINED3DDECLUSAGE_POSITIONT)) {
203 static BOOL warned = FALSE;
204 if(!warned) {
205 /* This may be bad with the fixed function pipeline */
206 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
207 warned = TRUE;
212 data += element->Offset;
214 TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
216 if (useVertexShaderFunction)
217 stride_used = vshader_get_input(This->stateBlock->vertexShader,
218 element->Usage, element->UsageIndex, &idx);
219 else
220 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
222 if (stride_used) {
223 TRACE("Load %s array %u [usage=%s, usage_idx=%u, "
224 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
225 useVertexShaderFunction? "shader": "fixed function", idx,
226 debug_d3ddeclusage(element->Usage), element->UsageIndex,
227 element->Stream, element->Offset, stride, debug_d3ddecltype(element->Type), streamVBO);
229 if (!useVertexShaderFunction && !vertexDeclaration->ffp_valid[i]) {
230 WARN("Skipping unsupported fixed function element of type %s and usage %s\n",
231 debug_d3ddecltype(element->Type), debug_d3ddeclusage(element->Usage));
232 } else {
233 strided->u.input[idx].lpData = data;
234 strided->u.input[idx].dwType = element->Type;
235 strided->u.input[idx].dwStride = stride;
236 strided->u.input[idx].VBO = streamVBO;
237 strided->u.input[idx].streamNo = element->Stream;
238 if (!GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA) && element->Type == WINED3DDECLTYPE_D3DCOLOR)
240 strided->swizzle_map |= 1 << idx;
242 strided->use_map |= 1 << idx;
246 /* Now call PreLoad on all the vertex buffers. In the very rare case
247 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
248 * The vertex buffer can now use the strided structure in the device instead of finding its
249 * own again.
251 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
252 * once in there.
254 for(i=0; i < numPreloadStreams; i++) {
255 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
256 if(vb) {
257 IWineD3DVertexBuffer_PreLoad(vb);
262 static void drawStridedFast(IWineD3DDevice *iface, GLenum primitive_type,
263 UINT min_vertex_idx, UINT max_vertex_idx, UINT count, short idx_size,
264 const void *idx_data, UINT start_idx)
266 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
268 if (idx_size)
270 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, primitive_type, count, min_vertex_idx);
272 #if 1
273 glDrawElements(primitive_type, count,
274 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
275 (const char *)idx_data + (idx_size * start_idx));
276 checkGLcall("glDrawElements");
277 #else
278 glDrawRangeElements(primitive_type, min_vertex_idx, max_vertex_idx, count,
279 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
280 (const char *)idx_data + (idx_size * start_idx));
281 checkGLcall("glDrawRangeElements");
282 #endif
284 else
286 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, primitive_type, start_idx, count);
288 glDrawArrays(primitive_type, start_idx, count);
289 checkGLcall("glDrawArrays");
294 * Actually draw using the supplied information.
295 * Slower GL version which extracts info about each vertex in turn
298 static void drawStridedSlow(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT NumVertexes,
299 GLenum glPrimType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx)
301 unsigned int textureNo = 0;
302 const WORD *pIdxBufS = NULL;
303 const DWORD *pIdxBufL = NULL;
304 ULONG vx_index;
305 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
306 const UINT *streamOffset = This->stateBlock->streamOffset;
307 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
308 BOOL pixelShader = use_ps(This->stateBlock);
309 BOOL specular_fog = FALSE;
310 UINT texture_stages = GL_LIMITS(texture_stages);
311 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
312 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
313 DWORD tex_mask = 0;
315 TRACE("Using slow vertex array code\n");
317 /* Variable Initialization */
318 if (idxSize != 0) {
319 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
320 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
321 * idxData will be != NULL
323 if(idxData == NULL) {
324 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
327 if (idxSize == 2) pIdxBufS = idxData;
328 else pIdxBufL = idxData;
329 } else if (idxData) {
330 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
331 return;
334 /* Start drawing in GL */
335 VTRACE(("glBegin(%x)\n", glPrimType));
336 glBegin(glPrimType);
338 if (sd->u.s.position.lpData) position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
340 if (sd->u.s.normal.lpData) normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
341 else glNormal3f(0, 0, 0);
343 if (sd->u.s.diffuse.lpData) diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
344 else glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
345 if (This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR)
346 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
348 if (sd->u.s.specular.lpData)
350 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
352 /* special case where the fog density is stored in the specular alpha channel */
353 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
354 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
355 || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4)
356 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
358 if (GL_SUPPORT(EXT_FOG_COORD))
360 if (sd->u.s.specular.dwType == WINED3DDECLTYPE_D3DCOLOR) specular_fog = TRUE;
361 else FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
363 else
365 static BOOL warned;
367 if (!warned)
369 /* TODO: Use the fog table code from old ddraw */
370 FIXME("Implement fog for transformed vertices in software\n");
371 warned = TRUE;
376 else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
378 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
381 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
383 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
384 int texture_idx = This->texUnitMap[textureNo];
386 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
388 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
389 continue;
392 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
394 if (texture_idx == -1) continue;
396 if (coordIdx > 7)
398 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
399 continue;
401 else if (coordIdx < 0)
403 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
404 continue;
407 if(sd->u.s.texCoords[coordIdx].lpData)
409 texCoords[coordIdx] =
410 sd->u.s.texCoords[coordIdx].lpData + streamOffset[sd->u.s.texCoords[coordIdx].streamNo];
411 tex_mask |= (1 << textureNo);
413 else
415 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
416 if (GL_SUPPORT(ARB_MULTITEXTURE))
417 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
418 else
419 glTexCoord4f(0, 0, 0, 1);
423 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
424 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
427 /* For each primitive */
428 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
429 UINT texture, tmp_tex_mask;
430 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
431 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
434 /* For indexed data, we need to go a few more strides in */
435 if (idxData != NULL) {
437 /* Indexed so work out the number of strides to skip */
438 if (idxSize == 2) {
439 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
440 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
441 } else {
442 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
443 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
447 tmp_tex_mask = tex_mask;
448 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
450 int coord_idx;
451 const void *ptr;
452 int texture_idx;
454 if (!(tmp_tex_mask & 1)) continue;
456 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
457 ptr = texCoords[coord_idx] + (SkipnStrides * sd->u.s.texCoords[coord_idx].dwStride);
459 texture_idx = This->texUnitMap[texture];
460 multi_texcoord_funcs[sd->u.s.texCoords[coord_idx].dwType](GL_TEXTURE0_ARB + texture_idx, ptr);
463 /* Diffuse -------------------------------- */
464 if (diffuse) {
465 const void *ptrToCoords = diffuse + SkipnStrides * sd->u.s.diffuse.dwStride;
467 diffuse_funcs[sd->u.s.diffuse.dwType](ptrToCoords);
468 if(This->activeContext->num_untracked_materials) {
469 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
470 unsigned char i;
471 float color[4];
473 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
474 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
475 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
476 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
478 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
479 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
484 /* Specular ------------------------------- */
485 if (specular) {
486 const void *ptrToCoords = specular + SkipnStrides * sd->u.s.specular.dwStride;
488 specular_funcs[sd->u.s.specular.dwType](ptrToCoords);
490 if (specular_fog)
492 DWORD specularColor = *(const DWORD *)ptrToCoords;
493 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
497 /* Normal -------------------------------- */
498 if (normal != NULL) {
499 const void *ptrToCoords = normal + SkipnStrides * sd->u.s.normal.dwStride;
500 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
503 /* Position -------------------------------- */
504 if (position) {
505 const void *ptrToCoords = position + SkipnStrides * sd->u.s.position.dwStride;
506 position_funcs[sd->u.s.position.dwType](ptrToCoords);
509 /* For non indexed mode, step onto next parts */
510 if (idxData == NULL) {
511 ++SkipnStrides;
515 glEnd();
516 checkGLcall("glEnd and previous calls");
519 static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
520 switch(type) {
521 case WINED3DDECLTYPE_FLOAT1:
522 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
523 break;
524 case WINED3DDECLTYPE_FLOAT2:
525 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
526 break;
527 case WINED3DDECLTYPE_FLOAT3:
528 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
529 break;
530 case WINED3DDECLTYPE_FLOAT4:
531 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
532 break;
534 case WINED3DDECLTYPE_UBYTE4:
535 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
536 break;
537 case WINED3DDECLTYPE_D3DCOLOR:
538 if (GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA))
540 const DWORD *src = ptr;
541 DWORD c = *src & 0xff00ff00;
542 c |= (*src & 0xff0000) >> 16;
543 c |= (*src & 0xff) << 16;
544 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
545 break;
547 /* else fallthrough */
548 case WINED3DDECLTYPE_UBYTE4N:
549 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
550 break;
552 case WINED3DDECLTYPE_SHORT2:
553 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
554 break;
555 case WINED3DDECLTYPE_SHORT4:
556 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
557 break;
559 case WINED3DDECLTYPE_SHORT2N:
561 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
562 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
563 break;
565 case WINED3DDECLTYPE_USHORT2N:
567 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
568 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
569 break;
571 case WINED3DDECLTYPE_SHORT4N:
572 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
573 break;
574 case WINED3DDECLTYPE_USHORT4N:
575 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
576 break;
578 case WINED3DDECLTYPE_UDEC3:
579 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
580 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
581 break;
582 case WINED3DDECLTYPE_DEC3N:
583 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
584 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
585 break;
587 case WINED3DDECLTYPE_FLOAT16_2:
588 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
589 * byte float according to the IEEE standard
591 if (GL_SUPPORT(NV_HALF_FLOAT)) {
592 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
593 } else {
594 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
595 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
596 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
598 break;
599 case WINED3DDECLTYPE_FLOAT16_4:
600 if (GL_SUPPORT(NV_HALF_FLOAT)) {
601 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
602 } else {
603 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
604 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
605 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
606 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
607 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
609 break;
611 case WINED3DDECLTYPE_UNUSED:
612 default:
613 ERR("Unexpected attribute declaration: %d\n", type);
614 break;
618 static void drawStridedSlowVs(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
619 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx)
621 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
622 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
623 const WORD *pIdxBufS = NULL;
624 const DWORD *pIdxBufL = NULL;
625 ULONG vx_index;
626 int i;
627 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
628 const BYTE *ptr;
630 if (idxSize != 0) {
631 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
632 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
633 * idxData will be != NULL
635 if(idxData == NULL) {
636 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
639 if (idxSize == 2) pIdxBufS = idxData;
640 else pIdxBufL = idxData;
641 } else if (idxData) {
642 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
643 return;
646 /* Start drawing in GL */
647 VTRACE(("glBegin(%x)\n", glPrimitiveType));
648 glBegin(glPrimitiveType);
650 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
651 if (idxData != NULL) {
653 /* Indexed so work out the number of strides to skip */
654 if (idxSize == 2) {
655 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
656 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
657 } else {
658 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
659 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
663 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
664 if(!sd->u.input[i].lpData) continue;
666 ptr = sd->u.input[i].lpData +
667 sd->u.input[i].dwStride * SkipnStrides +
668 stateblock->streamOffset[sd->u.input[i].streamNo];
670 send_attribute(This, sd->u.input[i].dwType, i, ptr);
672 SkipnStrides++;
675 glEnd();
678 static inline void drawStridedInstanced(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd,
679 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
680 ULONG startIdx)
682 UINT numInstances = 0, i;
683 int numInstancedAttribs = 0, j;
684 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
685 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
686 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
688 if (idxSize == 0) {
689 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
690 * We don't support this for now
692 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
693 * But the StreamSourceFreq value has a different meaning in that situation.
695 FIXME("Non-indexed instanced drawing is not supported\n");
696 return;
699 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
701 /* First, figure out how many instances we have to draw */
702 for(i = 0; i < MAX_STREAMS; i++) {
703 /* Look at the streams and take the first one which matches */
704 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
705 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
706 if(stateblock->streamFreq[i] == 0){
707 numInstances = 1;
708 } else {
709 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
711 break; /* break, because only the first suitable value is interesting */
715 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
716 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
717 instancedData[numInstancedAttribs] = i;
718 numInstancedAttribs++;
722 /* now draw numInstances instances :-) */
723 for(i = 0; i < numInstances; i++) {
724 /* Specify the instanced attributes using immediate mode calls */
725 for(j = 0; j < numInstancedAttribs; j++) {
726 const BYTE *ptr = sd->u.input[instancedData[j]].lpData +
727 sd->u.input[instancedData[j]].dwStride * i +
728 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
729 if(sd->u.input[instancedData[j]].VBO) {
730 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
731 ptr += (long) vb->resource.allocatedMemory;
734 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
737 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
738 (const char *)idxData+(idxSize * startIdx));
739 checkGLcall("glDrawElements");
743 static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
744 unsigned char i;
745 IWineD3DVertexBufferImpl *vb;
747 if(s->u.s.position.VBO) {
748 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
749 s->u.s.position.VBO = 0;
750 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
752 if(s->u.s.blendWeights.VBO) {
753 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
754 s->u.s.blendWeights.VBO = 0;
755 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
757 if(s->u.s.blendMatrixIndices.VBO) {
758 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
759 s->u.s.blendMatrixIndices.VBO = 0;
760 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
762 if(s->u.s.normal.VBO) {
763 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
764 s->u.s.normal.VBO = 0;
765 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
767 if(s->u.s.pSize.VBO) {
768 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
769 s->u.s.pSize.VBO = 0;
770 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
772 if(s->u.s.diffuse.VBO) {
773 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
774 s->u.s.diffuse.VBO = 0;
775 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
777 if(s->u.s.specular.VBO) {
778 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
779 s->u.s.specular.VBO = 0;
780 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
782 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
783 if(s->u.s.texCoords[i].VBO) {
784 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
785 s->u.s.texCoords[i].VBO = 0;
786 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
789 if(s->u.s.position2.VBO) {
790 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
791 s->u.s.position2.VBO = 0;
792 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
794 if(s->u.s.normal2.VBO) {
795 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
796 s->u.s.normal2.VBO = 0;
797 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
799 if(s->u.s.tangent.VBO) {
800 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
801 s->u.s.tangent.VBO = 0;
802 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
804 if(s->u.s.binormal.VBO) {
805 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
806 s->u.s.binormal.VBO = 0;
807 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
809 if(s->u.s.tessFactor.VBO) {
810 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
811 s->u.s.tessFactor.VBO = 0;
812 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
814 if(s->u.s.fog.VBO) {
815 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
816 s->u.s.fog.VBO = 0;
817 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
819 if(s->u.s.depth.VBO) {
820 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
821 s->u.s.depth.VBO = 0;
822 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
824 if(s->u.s.sample.VBO) {
825 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
826 s->u.s.sample.VBO = 0;
827 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
831 /* Routine common to the draw primitive and draw indexed primitive routines */
832 void drawPrimitive(IWineD3DDevice *iface, int PrimitiveType, long NumPrimitives,
833 UINT numberOfVertices, long StartIdx, short idxSize, const void *idxData, int minIndex)
836 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
837 IWineD3DSurfaceImpl *target;
838 unsigned int i;
840 if (NumPrimitives == 0) return;
842 /* Invalidate the back buffer memory so LockRect will read it the next time */
843 for(i = 0; i < GL_LIMITS(buffers); i++) {
844 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
845 if (target) {
846 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
847 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
851 /* Signals other modules that a drawing is in progress and the stateblock finalized */
852 This->isInDraw = TRUE;
854 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
856 if (This->stencilBufferTarget) {
857 /* Note that this depends on the ActivateContext call above to set
858 * This->render_offscreen properly */
859 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
860 surface_load_ds_location(This->stencilBufferTarget, location);
861 surface_modify_ds_location(This->stencilBufferTarget, location);
864 /* Ok, we will be updating the screen from here onwards so grab the lock */
865 ENTER_GL();
867 GLenum glPrimType;
868 BOOL emulation = FALSE;
869 const WineDirect3DVertexStridedData *strided = &This->strided_streams;
870 WineDirect3DVertexStridedData stridedlcl;
871 /* Ok, Work out which primitive is requested and how many vertexes that
872 will be */
873 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
874 if (numberOfVertices == 0 )
875 numberOfVertices = calculatedNumberOfindices;
877 if (!use_vs(This->stateBlock))
879 if (!This->strided_streams.position_transformed && This->activeContext->num_untracked_materials
880 && This->stateBlock->renderState[WINED3DRS_LIGHTING])
882 static BOOL warned;
883 if (!warned) {
884 FIXME("Using software emulation because not all material properties could be tracked\n");
885 warned = TRUE;
886 } else {
887 TRACE("Using software emulation because not all material properties could be tracked\n");
889 emulation = TRUE;
891 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
892 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
893 * to a float in the vertex buffer
895 static BOOL warned;
896 if (!warned) {
897 FIXME("Using software emulation because manual fog coordinates are provided\n");
898 warned = TRUE;
899 } else {
900 TRACE("Using software emulation because manual fog coordinates are provided\n");
902 emulation = TRUE;
905 if(emulation) {
906 strided = &stridedlcl;
907 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
908 remove_vbos(This, &stridedlcl);
912 if (This->useDrawStridedSlow || emulation) {
913 /* Immediate mode drawing */
914 if (use_vs(This->stateBlock))
916 static BOOL warned;
917 if (!warned) {
918 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
919 warned = TRUE;
920 } else {
921 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
923 drawStridedSlowVs(iface, strided, calculatedNumberOfindices,
924 glPrimType, idxData, idxSize, minIndex, StartIdx);
925 } else {
926 drawStridedSlow(iface, strided, calculatedNumberOfindices,
927 glPrimType, idxData, idxSize, minIndex, StartIdx);
929 } else if(This->instancedDraw) {
930 /* Instancing emulation with mixing immediate mode and arrays */
931 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
932 idxData, idxSize, minIndex, StartIdx);
933 } else {
934 drawStridedFast(iface, glPrimType, minIndex, minIndex + numberOfVertices - 1,
935 calculatedNumberOfindices, idxSize, idxData, StartIdx);
939 /* Finished updating the screen, restore lock */
940 LEAVE_GL();
941 TRACE("Done all gl drawing\n");
943 /* Diagnostics */
944 #ifdef SHOW_FRAME_MAKEUP
946 static long int primCounter = 0;
947 /* NOTE: set primCounter to the value reported by drawprim
948 before you want to to write frame makeup to /tmp */
949 if (primCounter >= 0) {
950 WINED3DLOCKED_RECT r;
951 char buffer[80];
952 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
953 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
954 TRACE("Saving screenshot %s\n", buffer);
955 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
956 IWineD3DSurface_UnlockRect(This->render_targets[0]);
958 #ifdef SHOW_TEXTURE_MAKEUP
960 IWineD3DSurface *pSur;
961 int textureNo;
962 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
963 if (This->stateBlock->textures[textureNo] != NULL) {
964 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
965 TRACE("Saving texture %s\n", buffer);
966 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
967 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
968 IWineD3DSurface_SaveSnapshot(pSur, buffer);
969 IWineD3DSurface_Release(pSur);
970 } else {
971 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
976 #endif
978 TRACE("drawprim #%ld\n", primCounter);
979 ++primCounter;
981 #endif
983 /* Control goes back to the device, stateblock values may change again */
984 This->isInDraw = FALSE;
987 static void normalize_normal(float *n) {
988 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
989 if(length == 0.0) return;
990 length = sqrt(length);
991 n[0] = n[0] / length;
992 n[1] = n[1] / length;
993 n[2] = n[2] / length;
996 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
998 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
999 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1000 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
1001 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1002 * in drawprim.
1004 * To read back, the opengl feedback mode is used. This creates a problem because we want
1005 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1006 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1007 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1008 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1010 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1011 * resulting colors back to the normals.
1013 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1014 * does not restore it because normally a draw follows immediately afterwards. The caller is
1015 * responsible of taking care that either the gl states are restored, or the context activated
1016 * for drawing to reset the lastWasBlit flag.
1018 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1019 struct WineD3DRectPatch *patch) {
1020 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1021 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1022 WineDirect3DVertexStridedData strided;
1023 const BYTE *data;
1024 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1025 DWORD vtxStride;
1026 GLenum feedback_type;
1027 GLfloat *feedbuffer;
1029 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1030 * Beware of vbos
1032 memset(&strided, 0, sizeof(strided));
1033 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1034 if(strided.u.s.position.VBO) {
1035 IWineD3DVertexBufferImpl *vb;
1036 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1037 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1038 (unsigned long) vb->resource.allocatedMemory);
1040 vtxStride = strided.u.s.position.dwStride;
1041 data = strided.u.s.position.lpData +
1042 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1043 vtxStride * info->StartVertexOffsetWidth;
1045 /* Not entirely sure about what happens with transformed vertices */
1046 if (strided.position_transformed) FIXME("Transformed position in rectpatch generation\n");
1048 if(vtxStride % sizeof(GLfloat)) {
1049 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1050 * I don't see how the stride could not be a multiple of 4, but make sure
1051 * to check it
1053 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1055 if(info->Basis != WINED3DBASIS_BEZIER) {
1056 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1058 if(info->Degree != WINED3DDEGREE_CUBIC) {
1059 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1062 /* First, get the boundary cube of the input data */
1063 for(j = 0; j < info->Height; j++) {
1064 for(i = 0; i < info->Width; i++) {
1065 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
1066 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1067 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1068 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1069 if(v[2] < neg_z) neg_z = v[2];
1073 /* This needs some improvements in the vertex decl code */
1074 FIXME("Cannot find data to generate. Only generating position and normals\n");
1075 patch->has_normals = TRUE;
1076 patch->has_texcoords = FALSE;
1078 /* Simply activate the context for blitting. This disables all the things we don't want and
1079 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1080 * patch (as opposed to normal draws) will most likely need different changes anyway
1082 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1083 ENTER_GL();
1085 glMatrixMode(GL_PROJECTION);
1086 checkGLcall("glMatrixMode(GL_PROJECTION)");
1087 glLoadIdentity();
1088 checkGLcall("glLoadIndentity()");
1089 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1090 glTranslatef(0, 0, 0.5);
1091 checkGLcall("glScalef");
1092 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1093 checkGLcall("glViewport");
1095 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1096 * our feedback buffer parser
1098 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1099 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1100 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1101 if(patch->has_normals) {
1102 static const GLfloat black[] = {0, 0, 0, 0};
1103 static const GLfloat red[] = {1, 0, 0, 0};
1104 static const GLfloat green[] = {0, 1, 0, 0};
1105 static const GLfloat blue[] = {0, 0, 1, 0};
1106 static const GLfloat white[] = {1, 1, 1, 1};
1107 glEnable(GL_LIGHTING);
1108 checkGLcall("glEnable(GL_LIGHTING)");
1109 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1110 checkGLcall("glLightModel for MODEL_AMBIENT");
1111 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1113 for(i = 3; i < GL_LIMITS(lights); i++) {
1114 glDisable(GL_LIGHT0 + i);
1115 checkGLcall("glDisable(GL_LIGHT0 + i)");
1116 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1119 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1120 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1121 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1122 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1123 glLightfv(GL_LIGHT0, GL_POSITION, red);
1124 glEnable(GL_LIGHT0);
1125 checkGLcall("Setting up light 1\n");
1126 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1127 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1128 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1129 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1130 glLightfv(GL_LIGHT1, GL_POSITION, green);
1131 glEnable(GL_LIGHT1);
1132 checkGLcall("Setting up light 2\n");
1133 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1134 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1135 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1136 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1137 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1138 glEnable(GL_LIGHT2);
1139 checkGLcall("Setting up light 3\n");
1141 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1142 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1143 glDisable(GL_COLOR_MATERIAL);
1144 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1145 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1146 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1147 checkGLcall("Setting up materials\n");
1150 /* Enable the needed maps.
1151 * GL_MAP2_VERTEX_3 is needed for positional data.
1152 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1153 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1155 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1156 out_vertex_size = 3 /* position */;
1157 d3d_out_vertex_size = 3;
1158 glEnable(GL_MAP2_VERTEX_3);
1159 if(patch->has_normals && patch->has_texcoords) {
1160 FIXME("Texcoords not handled yet\n");
1161 feedback_type = GL_3D_COLOR_TEXTURE;
1162 out_vertex_size += 8;
1163 d3d_out_vertex_size += 7;
1164 glEnable(GL_AUTO_NORMAL);
1165 glEnable(GL_MAP2_TEXTURE_COORD_4);
1166 } else if(patch->has_texcoords) {
1167 FIXME("Texcoords not handled yet\n");
1168 feedback_type = GL_3D_COLOR_TEXTURE;
1169 out_vertex_size += 7;
1170 d3d_out_vertex_size += 4;
1171 glEnable(GL_MAP2_TEXTURE_COORD_4);
1172 } else if(patch->has_normals) {
1173 feedback_type = GL_3D_COLOR;
1174 out_vertex_size += 4;
1175 d3d_out_vertex_size += 3;
1176 glEnable(GL_AUTO_NORMAL);
1177 } else {
1178 feedback_type = GL_3D;
1180 checkGLcall("glEnable vertex attrib generation");
1182 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1183 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1184 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1186 glMap2f(GL_MAP2_VERTEX_3,
1187 0, 1, vtxStride / sizeof(float), info->Width,
1188 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1189 (const GLfloat *)data);
1190 checkGLcall("glMap2f");
1191 if(patch->has_texcoords) {
1192 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1193 0, 1, vtxStride / sizeof(float), info->Width,
1194 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1195 (const GLfloat *)data);
1196 checkGLcall("glMap2f");
1198 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1199 checkGLcall("glMapGrid2f");
1201 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1202 checkGLcall("glFeedbackBuffer");
1203 glRenderMode(GL_FEEDBACK);
1205 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1206 checkGLcall("glEvalMesh2\n");
1208 i = glRenderMode(GL_RENDER);
1209 if(i == -1) {
1210 LEAVE_GL();
1211 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1212 Sleep(10000);
1213 HeapFree(GetProcessHeap(), 0, feedbuffer);
1214 return WINED3DERR_DRIVERINTERNALERROR;
1215 } else if(i != buffer_size) {
1216 LEAVE_GL();
1217 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1218 Sleep(10000);
1219 HeapFree(GetProcessHeap(), 0, feedbuffer);
1220 return WINED3DERR_DRIVERINTERNALERROR;
1221 } else {
1222 TRACE("Got %d elements as expected\n", i);
1225 HeapFree(GetProcessHeap(), 0, patch->mem);
1226 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1227 i = 0;
1228 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1229 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1230 ERR("Unexpected token: %f\n", feedbuffer[j]);
1231 continue;
1233 if(feedbuffer[j + 1] != 3) {
1234 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1235 continue;
1237 /* Somehow there are different ideas about back / front facing, so fix up the
1238 * vertex order
1240 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1241 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1242 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1243 if(patch->has_normals) {
1244 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1245 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1246 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1248 i += d3d_out_vertex_size;
1250 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1251 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1252 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1253 if(patch->has_normals) {
1254 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1255 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1256 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1258 i += d3d_out_vertex_size;
1260 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1261 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1262 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1263 if(patch->has_normals) {
1264 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1265 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1266 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1268 i += d3d_out_vertex_size;
1271 if(patch->has_normals) {
1272 /* Now do the same with reverse light directions */
1273 static const GLfloat x[] = {-1, 0, 0, 0};
1274 static const GLfloat y[] = { 0, -1, 0, 0};
1275 static const GLfloat z[] = { 0, 0, -1, 0};
1276 glLightfv(GL_LIGHT0, GL_POSITION, x);
1277 glLightfv(GL_LIGHT1, GL_POSITION, y);
1278 glLightfv(GL_LIGHT2, GL_POSITION, z);
1279 checkGLcall("Setting up reverse light directions\n");
1281 glRenderMode(GL_FEEDBACK);
1282 checkGLcall("glRenderMode(GL_FEEDBACK)");
1283 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1284 checkGLcall("glEvalMesh2\n");
1285 i = glRenderMode(GL_RENDER);
1286 checkGLcall("glRenderMode(GL_RENDER)");
1288 i = 0;
1289 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1290 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1291 ERR("Unexpected token: %f\n", feedbuffer[j]);
1292 continue;
1294 if(feedbuffer[j + 1] != 3) {
1295 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1296 continue;
1298 if(patch->mem[i + 3] == 0.0)
1299 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1300 if(patch->mem[i + 4] == 0.0)
1301 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1302 if(patch->mem[i + 5] == 0.0)
1303 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1304 normalize_normal(patch->mem + i + 3);
1305 i += d3d_out_vertex_size;
1307 if(patch->mem[i + 3] == 0.0)
1308 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1309 if(patch->mem[i + 4] == 0.0)
1310 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1311 if(patch->mem[i + 5] == 0.0)
1312 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1313 normalize_normal(patch->mem + i + 3);
1314 i += d3d_out_vertex_size;
1316 if(patch->mem[i + 3] == 0.0)
1317 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1318 if(patch->mem[i + 4] == 0.0)
1319 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1320 if(patch->mem[i + 5] == 0.0)
1321 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1322 normalize_normal(patch->mem + i + 3);
1323 i += d3d_out_vertex_size;
1327 glDisable(GL_MAP2_VERTEX_3);
1328 glDisable(GL_AUTO_NORMAL);
1329 glDisable(GL_MAP2_NORMAL);
1330 glDisable(GL_MAP2_TEXTURE_COORD_4);
1331 checkGLcall("glDisable vertex attrib generation");
1332 LEAVE_GL();
1334 HeapFree(GetProcessHeap(), 0, feedbuffer);
1336 vtxStride = 3 * sizeof(float);
1337 if(patch->has_normals) {
1338 vtxStride += 3 * sizeof(float);
1340 if(patch->has_texcoords) {
1341 vtxStride += 4 * sizeof(float);
1343 memset(&patch->strided, 0, sizeof(&patch->strided));
1344 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1345 patch->strided.u.s.position.dwStride = vtxStride;
1346 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1347 patch->strided.u.s.position.streamNo = 255;
1349 if(patch->has_normals) {
1350 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1351 patch->strided.u.s.normal.dwStride = vtxStride;
1352 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1353 patch->strided.u.s.normal.streamNo = 255;
1355 if(patch->has_texcoords) {
1356 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1357 if(patch->has_normals) {
1358 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1360 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1361 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1362 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1363 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1364 * application.
1366 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;
1369 return WINED3D_OK;