wined3d: Simplify handling of swizzled attributes.
[wine/multimedia.git] / dlls / wined3d / drawprim.c
blob70ba80b1af3fce7b3c563f1493d683f9fdc5447b
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 (element->Type == WINED3DDECLTYPE_D3DCOLOR) strided->swizzle_map |= 1 << idx;
239 strided->use_map |= 1 << idx;
243 /* Now call PreLoad on all the vertex buffers. In the very rare case
244 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
245 * The vertex buffer can now use the strided structure in the device instead of finding its
246 * own again.
248 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
249 * once in there.
251 for(i=0; i < numPreloadStreams; i++) {
252 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
253 if(vb) {
254 IWineD3DVertexBuffer_PreLoad(vb);
259 static void drawStridedFast(IWineD3DDevice *iface, GLenum primitive_type,
260 UINT min_vertex_idx, UINT max_vertex_idx, UINT count, short idx_size,
261 const void *idx_data, UINT start_idx)
263 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
265 if (idx_size)
267 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, primitive_type, count, min_vertex_idx);
269 #if 1
270 glDrawElements(primitive_type, count,
271 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
272 (const char *)idx_data + (idx_size * start_idx));
273 checkGLcall("glDrawElements");
274 #else
275 glDrawRangeElements(primitive_type, min_vertex_idx, max_vertex_idx, count,
276 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
277 (const char *)idx_data + (idx_size * start_idx));
278 checkGLcall("glDrawRangeElements");
279 #endif
281 else
283 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, primitive_type, start_idx, count);
285 glDrawArrays(primitive_type, start_idx, count);
286 checkGLcall("glDrawArrays");
291 * Actually draw using the supplied information.
292 * Slower GL version which extracts info about each vertex in turn
295 static void drawStridedSlow(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT NumVertexes,
296 GLenum glPrimType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx)
298 unsigned int textureNo = 0;
299 const WORD *pIdxBufS = NULL;
300 const DWORD *pIdxBufL = NULL;
301 ULONG vx_index;
302 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
303 const UINT *streamOffset = This->stateBlock->streamOffset;
304 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
305 BOOL pixelShader = use_ps(This->stateBlock);
306 BOOL specular_fog = FALSE;
307 UINT texture_stages = GL_LIMITS(texture_stages);
308 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
309 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
310 DWORD tex_mask = 0;
312 TRACE("Using slow vertex array code\n");
314 /* Variable Initialization */
315 if (idxSize != 0) {
316 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
317 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
318 * idxData will be != NULL
320 if(idxData == NULL) {
321 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
324 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
325 else pIdxBufL = (const DWORD *) idxData;
326 } else if (idxData) {
327 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
328 return;
331 /* Start drawing in GL */
332 VTRACE(("glBegin(%x)\n", glPrimType));
333 glBegin(glPrimType);
335 if (sd->u.s.position.lpData) position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
337 if (sd->u.s.normal.lpData) normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
338 else glNormal3f(0, 0, 0);
340 if (sd->u.s.diffuse.lpData) diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
341 else glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
342 if (This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR)
343 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
345 if (sd->u.s.specular.lpData)
347 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
349 /* special case where the fog density is stored in the specular alpha channel */
350 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
351 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
352 || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4)
353 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
355 if (GL_SUPPORT(EXT_FOG_COORD))
357 if (sd->u.s.specular.dwType == WINED3DDECLTYPE_D3DCOLOR) specular_fog = TRUE;
358 else FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
360 else
362 static BOOL warned;
364 if (!warned)
366 /* TODO: Use the fog table code from old ddraw */
367 FIXME("Implement fog for transformed vertices in software\n");
368 warned = TRUE;
373 else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
375 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
378 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
380 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
381 int texture_idx = This->texUnitMap[textureNo];
383 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
385 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
386 continue;
389 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
391 if (texture_idx == -1) continue;
393 if (coordIdx > 7)
395 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
396 continue;
398 else if (coordIdx < 0)
400 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
401 continue;
404 if(sd->u.s.texCoords[coordIdx].lpData)
406 texCoords[coordIdx] =
407 sd->u.s.texCoords[coordIdx].lpData + streamOffset[sd->u.s.texCoords[coordIdx].streamNo];
408 tex_mask |= (1 << textureNo);
410 else
412 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
413 if (GL_SUPPORT(ARB_MULTITEXTURE))
414 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
415 else
416 glTexCoord4f(0, 0, 0, 1);
420 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
421 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
424 /* For each primitive */
425 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
426 UINT texture, tmp_tex_mask;
427 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
428 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
431 /* For indexed data, we need to go a few more strides in */
432 if (idxData != NULL) {
434 /* Indexed so work out the number of strides to skip */
435 if (idxSize == 2) {
436 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
437 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
438 } else {
439 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
440 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
444 tmp_tex_mask = tex_mask;
445 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
447 int coord_idx;
448 const void *ptr;
449 int texture_idx;
451 if (!(tmp_tex_mask & 1)) continue;
453 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
454 ptr = texCoords[coord_idx] + (SkipnStrides * sd->u.s.texCoords[coord_idx].dwStride);
456 texture_idx = This->texUnitMap[texture];
457 multi_texcoord_funcs[sd->u.s.texCoords[coord_idx].dwType](GL_TEXTURE0_ARB + texture_idx, ptr);
460 /* Diffuse -------------------------------- */
461 if (diffuse) {
462 const void *ptrToCoords = diffuse + SkipnStrides * sd->u.s.diffuse.dwStride;
464 diffuse_funcs[sd->u.s.diffuse.dwType](ptrToCoords);
465 if(This->activeContext->num_untracked_materials) {
466 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
467 unsigned char i;
468 float color[4];
470 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
471 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
472 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
473 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
475 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
476 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
481 /* Specular ------------------------------- */
482 if (specular) {
483 const void *ptrToCoords = specular + SkipnStrides * sd->u.s.specular.dwStride;
485 specular_funcs[sd->u.s.specular.dwType](ptrToCoords);
487 if (specular_fog)
489 DWORD specularColor = *(const DWORD *)ptrToCoords;
490 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
494 /* Normal -------------------------------- */
495 if (normal != NULL) {
496 const void *ptrToCoords = normal + SkipnStrides * sd->u.s.normal.dwStride;
497 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
500 /* Position -------------------------------- */
501 if (position) {
502 const void *ptrToCoords = position + SkipnStrides * sd->u.s.position.dwStride;
503 position_funcs[sd->u.s.position.dwType](ptrToCoords);
506 /* For non indexed mode, step onto next parts */
507 if (idxData == NULL) {
508 ++SkipnStrides;
512 glEnd();
513 checkGLcall("glEnd and previous calls");
516 static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
517 switch(type) {
518 case WINED3DDECLTYPE_FLOAT1:
519 GL_EXTCALL(glVertexAttrib1fvARB(index, (const float *)ptr));
520 break;
521 case WINED3DDECLTYPE_FLOAT2:
522 GL_EXTCALL(glVertexAttrib2fvARB(index, (const float *)ptr));
523 break;
524 case WINED3DDECLTYPE_FLOAT3:
525 GL_EXTCALL(glVertexAttrib3fvARB(index, (const float *)ptr));
526 break;
527 case WINED3DDECLTYPE_FLOAT4:
528 GL_EXTCALL(glVertexAttrib4fvARB(index, (const float *)ptr));
529 break;
531 case WINED3DDECLTYPE_UBYTE4:
532 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
533 break;
534 case WINED3DDECLTYPE_UBYTE4N:
535 case WINED3DDECLTYPE_D3DCOLOR:
536 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
537 break;
539 case WINED3DDECLTYPE_SHORT2:
540 GL_EXTCALL(glVertexAttrib4svARB(index, (const GLshort *)ptr));
541 break;
542 case WINED3DDECLTYPE_SHORT4:
543 GL_EXTCALL(glVertexAttrib4svARB(index, (const GLshort *)ptr));
544 break;
546 case WINED3DDECLTYPE_SHORT2N:
548 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
549 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
550 break;
552 case WINED3DDECLTYPE_USHORT2N:
554 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
555 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
556 break;
558 case WINED3DDECLTYPE_SHORT4N:
559 GL_EXTCALL(glVertexAttrib4NsvARB(index, (const GLshort *)ptr));
560 break;
561 case WINED3DDECLTYPE_USHORT4N:
562 GL_EXTCALL(glVertexAttrib4NusvARB(index, (const GLushort *)ptr));
563 break;
565 case WINED3DDECLTYPE_UDEC3:
566 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
567 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
568 break;
569 case WINED3DDECLTYPE_DEC3N:
570 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
571 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
572 break;
574 case WINED3DDECLTYPE_FLOAT16_2:
575 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
576 * byte float according to the IEEE standard
578 if (GL_SUPPORT(NV_HALF_FLOAT)) {
579 GL_EXTCALL(glVertexAttrib2hvNV(index, (const GLhalfNV *)ptr));
580 } else {
581 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
582 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
583 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
585 break;
586 case WINED3DDECLTYPE_FLOAT16_4:
587 if (GL_SUPPORT(NV_HALF_FLOAT)) {
588 GL_EXTCALL(glVertexAttrib4hvNV(index, (const GLhalfNV *)ptr));
589 } else {
590 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
591 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
592 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
593 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
594 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
596 break;
598 case WINED3DDECLTYPE_UNUSED:
599 default:
600 ERR("Unexpected attribute declaration: %d\n", type);
601 break;
605 static void drawStridedSlowVs(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
606 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx)
608 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
609 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
610 const WORD *pIdxBufS = NULL;
611 const DWORD *pIdxBufL = NULL;
612 ULONG vx_index;
613 int i;
614 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
615 const BYTE *ptr;
617 if (idxSize != 0) {
618 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
619 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
620 * idxData will be != NULL
622 if(idxData == NULL) {
623 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
626 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
627 else pIdxBufL = (const DWORD *) idxData;
628 } else if (idxData) {
629 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
630 return;
633 /* Start drawing in GL */
634 VTRACE(("glBegin(%x)\n", glPrimitiveType));
635 glBegin(glPrimitiveType);
637 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
638 if (idxData != NULL) {
640 /* Indexed so work out the number of strides to skip */
641 if (idxSize == 2) {
642 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
643 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
644 } else {
645 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
646 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
650 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
651 if(!sd->u.input[i].lpData) continue;
653 ptr = sd->u.input[i].lpData +
654 sd->u.input[i].dwStride * SkipnStrides +
655 stateblock->streamOffset[sd->u.input[i].streamNo];
657 send_attribute(This, sd->u.input[i].dwType, i, ptr);
659 SkipnStrides++;
662 glEnd();
665 static inline void drawStridedInstanced(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd,
666 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
667 ULONG startIdx)
669 UINT numInstances = 0, i;
670 int numInstancedAttribs = 0, j;
671 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
672 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
673 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
675 if (idxSize == 0) {
676 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
677 * We don't support this for now
679 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
680 * But the StreamSourceFreq value has a different meaning in that situation.
682 FIXME("Non-indexed instanced drawing is not supported\n");
683 return;
686 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
687 idxData = idxData == (void *)-1 ? NULL : idxData;
689 /* First, figure out how many instances we have to draw */
690 for(i = 0; i < MAX_STREAMS; i++) {
691 /* Look at the streams and take the first one which matches */
692 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
693 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
694 if(stateblock->streamFreq[i] == 0){
695 numInstances = 1;
696 } else {
697 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
699 break; /* break, because only the first suitable value is interesting */
703 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
704 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
705 instancedData[numInstancedAttribs] = i;
706 numInstancedAttribs++;
710 /* now draw numInstances instances :-) */
711 for(i = 0; i < numInstances; i++) {
712 /* Specify the instanced attributes using immediate mode calls */
713 for(j = 0; j < numInstancedAttribs; j++) {
714 const BYTE *ptr = sd->u.input[instancedData[j]].lpData +
715 sd->u.input[instancedData[j]].dwStride * i +
716 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
717 if(sd->u.input[instancedData[j]].VBO) {
718 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
719 ptr += (long) vb->resource.allocatedMemory;
722 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
725 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
726 (const char *)idxData+(idxSize * startIdx));
727 checkGLcall("glDrawElements");
731 static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
732 unsigned char i;
733 IWineD3DVertexBufferImpl *vb;
735 if(s->u.s.position.VBO) {
736 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
737 s->u.s.position.VBO = 0;
738 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
740 if(s->u.s.blendWeights.VBO) {
741 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
742 s->u.s.blendWeights.VBO = 0;
743 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
745 if(s->u.s.blendMatrixIndices.VBO) {
746 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
747 s->u.s.blendMatrixIndices.VBO = 0;
748 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
750 if(s->u.s.normal.VBO) {
751 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
752 s->u.s.normal.VBO = 0;
753 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
755 if(s->u.s.pSize.VBO) {
756 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
757 s->u.s.pSize.VBO = 0;
758 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
760 if(s->u.s.diffuse.VBO) {
761 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
762 s->u.s.diffuse.VBO = 0;
763 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
765 if(s->u.s.specular.VBO) {
766 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
767 s->u.s.specular.VBO = 0;
768 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
770 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
771 if(s->u.s.texCoords[i].VBO) {
772 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
773 s->u.s.texCoords[i].VBO = 0;
774 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
777 if(s->u.s.position2.VBO) {
778 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
779 s->u.s.position2.VBO = 0;
780 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
782 if(s->u.s.normal2.VBO) {
783 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
784 s->u.s.normal2.VBO = 0;
785 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
787 if(s->u.s.tangent.VBO) {
788 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
789 s->u.s.tangent.VBO = 0;
790 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
792 if(s->u.s.binormal.VBO) {
793 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
794 s->u.s.binormal.VBO = 0;
795 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
797 if(s->u.s.tessFactor.VBO) {
798 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
799 s->u.s.tessFactor.VBO = 0;
800 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
802 if(s->u.s.fog.VBO) {
803 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
804 s->u.s.fog.VBO = 0;
805 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
807 if(s->u.s.depth.VBO) {
808 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
809 s->u.s.depth.VBO = 0;
810 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
812 if(s->u.s.sample.VBO) {
813 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
814 s->u.s.sample.VBO = 0;
815 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
819 /* Routine common to the draw primitive and draw indexed primitive routines */
820 void drawPrimitive(IWineD3DDevice *iface, int PrimitiveType, long NumPrimitives,
821 UINT numberOfVertices, long StartIdx, short idxSize, const void *idxData, int minIndex)
824 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
825 IWineD3DSurfaceImpl *target;
826 unsigned int i;
828 if (NumPrimitives == 0) return;
830 /* Invalidate the back buffer memory so LockRect will read it the next time */
831 for(i = 0; i < GL_LIMITS(buffers); i++) {
832 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
833 if (target) {
834 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
835 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
839 /* Signals other modules that a drawing is in progress and the stateblock finalized */
840 This->isInDraw = TRUE;
842 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
844 if (This->stencilBufferTarget) {
845 /* Note that this depends on the ActivateContext call above to set
846 * This->render_offscreen properly */
847 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
848 surface_load_ds_location(This->stencilBufferTarget, location);
849 surface_modify_ds_location(This->stencilBufferTarget, location);
852 /* Ok, we will be updating the screen from here onwards so grab the lock */
853 ENTER_GL();
855 GLenum glPrimType;
856 BOOL emulation = FALSE;
857 const WineDirect3DVertexStridedData *strided = &This->strided_streams;
858 WineDirect3DVertexStridedData stridedlcl;
859 /* Ok, Work out which primitive is requested and how many vertexes that
860 will be */
861 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
862 if (numberOfVertices == 0 )
863 numberOfVertices = calculatedNumberOfindices;
865 if (!use_vs(This->stateBlock))
867 if (!This->strided_streams.position_transformed && This->activeContext->num_untracked_materials
868 && This->stateBlock->renderState[WINED3DRS_LIGHTING])
870 static BOOL warned;
871 if (!warned) {
872 FIXME("Using software emulation because not all material properties could be tracked\n");
873 warned = TRUE;
874 } else {
875 TRACE("Using software emulation because not all material properties could be tracked\n");
877 emulation = TRUE;
879 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
880 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
881 * to a float in the vertex buffer
883 static BOOL warned;
884 if (!warned) {
885 FIXME("Using software emulation because manual fog coordinates are provided\n");
886 warned = TRUE;
887 } else {
888 TRACE("Using software emulation because manual fog coordinates are provided\n");
890 emulation = TRUE;
893 if(emulation) {
894 strided = &stridedlcl;
895 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
896 remove_vbos(This, &stridedlcl);
900 if (This->useDrawStridedSlow || emulation) {
901 /* Immediate mode drawing */
902 if (use_vs(This->stateBlock))
904 static BOOL warned;
905 if (!warned) {
906 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
907 warned = TRUE;
908 } else {
909 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
911 drawStridedSlowVs(iface, strided, calculatedNumberOfindices,
912 glPrimType, idxData, idxSize, minIndex, StartIdx);
913 } else {
914 drawStridedSlow(iface, strided, calculatedNumberOfindices,
915 glPrimType, idxData, idxSize, minIndex, StartIdx);
917 } else if(This->instancedDraw) {
918 /* Instancing emulation with mixing immediate mode and arrays */
919 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
920 idxData, idxSize, minIndex, StartIdx);
921 } else {
922 drawStridedFast(iface, glPrimType, minIndex, minIndex + numberOfVertices - 1,
923 calculatedNumberOfindices, idxSize, idxData, StartIdx);
927 /* Finished updating the screen, restore lock */
928 LEAVE_GL();
929 TRACE("Done all gl drawing\n");
931 /* Diagnostics */
932 #ifdef SHOW_FRAME_MAKEUP
934 static long int primCounter = 0;
935 /* NOTE: set primCounter to the value reported by drawprim
936 before you want to to write frame makeup to /tmp */
937 if (primCounter >= 0) {
938 WINED3DLOCKED_RECT r;
939 char buffer[80];
940 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
941 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
942 TRACE("Saving screenshot %s\n", buffer);
943 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
944 IWineD3DSurface_UnlockRect(This->render_targets[0]);
946 #ifdef SHOW_TEXTURE_MAKEUP
948 IWineD3DSurface *pSur;
949 int textureNo;
950 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
951 if (This->stateBlock->textures[textureNo] != NULL) {
952 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
953 TRACE("Saving texture %s\n", buffer);
954 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
955 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
956 IWineD3DSurface_SaveSnapshot(pSur, buffer);
957 IWineD3DSurface_Release(pSur);
958 } else {
959 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
964 #endif
966 TRACE("drawprim #%ld\n", primCounter);
967 ++primCounter;
969 #endif
971 /* Control goes back to the device, stateblock values may change again */
972 This->isInDraw = FALSE;
975 static void normalize_normal(float *n) {
976 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
977 if(length == 0.0) return;
978 length = sqrt(length);
979 n[0] = n[0] / length;
980 n[1] = n[1] / length;
981 n[2] = n[2] / length;
984 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
986 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
987 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
988 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
989 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
990 * in drawprim.
992 * To read back, the opengl feedback mode is used. This creates a problem because we want
993 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
994 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
995 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
996 * them to [-1.0;+1.0] and set the viewport up to scale them back.
998 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
999 * resulting colors back to the normals.
1001 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1002 * does not restore it because normally a draw follows immediately afterwards. The caller is
1003 * responsible of taking care that either the gl states are restored, or the context activated
1004 * for drawing to reset the lastWasBlit flag.
1006 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1007 struct WineD3DRectPatch *patch) {
1008 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1009 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1010 WineDirect3DVertexStridedData strided;
1011 const BYTE *data;
1012 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1013 DWORD vtxStride;
1014 GLenum feedback_type;
1015 GLfloat *feedbuffer;
1017 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1018 * Beware of vbos
1020 memset(&strided, 0, sizeof(strided));
1021 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1022 if(strided.u.s.position.VBO) {
1023 IWineD3DVertexBufferImpl *vb;
1024 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1025 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1026 (unsigned long) vb->resource.allocatedMemory);
1028 vtxStride = strided.u.s.position.dwStride;
1029 data = strided.u.s.position.lpData +
1030 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1031 vtxStride * info->StartVertexOffsetWidth;
1033 /* Not entirely sure about what happens with transformed vertices */
1034 if (strided.position_transformed) FIXME("Transformed position in rectpatch generation\n");
1036 if(vtxStride % sizeof(GLfloat)) {
1037 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1038 * I don't see how the stride could not be a multiple of 4, but make sure
1039 * to check it
1041 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1043 if(info->Basis != WINED3DBASIS_BEZIER) {
1044 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1046 if(info->Degree != WINED3DDEGREE_CUBIC) {
1047 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1050 /* First, get the boundary cube of the input data */
1051 for(j = 0; j < info->Height; j++) {
1052 for(i = 0; i < info->Width; i++) {
1053 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
1054 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1055 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1056 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1057 if(v[2] < neg_z) neg_z = v[2];
1061 /* This needs some improvements in the vertex decl code */
1062 FIXME("Cannot find data to generate. Only generating position and normals\n");
1063 patch->has_normals = TRUE;
1064 patch->has_texcoords = FALSE;
1066 /* Simply activate the context for blitting. This disables all the things we don't want and
1067 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1068 * patch (as opposed to normal draws) will most likely need different changes anyway
1070 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1071 ENTER_GL();
1073 glMatrixMode(GL_PROJECTION);
1074 checkGLcall("glMatrixMode(GL_PROJECTION)");
1075 glLoadIdentity();
1076 checkGLcall("glLoadIndentity()");
1077 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1078 glTranslatef(0, 0, 0.5);
1079 checkGLcall("glScalef");
1080 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1081 checkGLcall("glViewport");
1083 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1084 * our feedback buffer parser
1086 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1087 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1088 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1089 if(patch->has_normals) {
1090 static const GLfloat black[] = {0, 0, 0, 0};
1091 static const GLfloat red[] = {1, 0, 0, 0};
1092 static const GLfloat green[] = {0, 1, 0, 0};
1093 static const GLfloat blue[] = {0, 0, 1, 0};
1094 static const GLfloat white[] = {1, 1, 1, 1};
1095 glEnable(GL_LIGHTING);
1096 checkGLcall("glEnable(GL_LIGHTING)");
1097 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1098 checkGLcall("glLightModel for MODEL_AMBIENT");
1099 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1101 for(i = 3; i < GL_LIMITS(lights); i++) {
1102 glDisable(GL_LIGHT0 + i);
1103 checkGLcall("glDisable(GL_LIGHT0 + i)");
1104 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1107 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1108 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1109 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1110 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1111 glLightfv(GL_LIGHT0, GL_POSITION, red);
1112 glEnable(GL_LIGHT0);
1113 checkGLcall("Setting up light 1\n");
1114 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1115 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1116 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1117 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1118 glLightfv(GL_LIGHT1, GL_POSITION, green);
1119 glEnable(GL_LIGHT1);
1120 checkGLcall("Setting up light 2\n");
1121 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1122 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1123 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1124 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1125 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1126 glEnable(GL_LIGHT2);
1127 checkGLcall("Setting up light 3\n");
1129 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1130 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1131 glDisable(GL_COLOR_MATERIAL);
1132 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1133 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1134 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1135 checkGLcall("Setting up materials\n");
1138 /* Enable the needed maps.
1139 * GL_MAP2_VERTEX_3 is needed for positional data.
1140 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1141 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1143 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1144 out_vertex_size = 3 /* position */;
1145 d3d_out_vertex_size = 3;
1146 glEnable(GL_MAP2_VERTEX_3);
1147 if(patch->has_normals && patch->has_texcoords) {
1148 FIXME("Texcoords not handled yet\n");
1149 feedback_type = GL_3D_COLOR_TEXTURE;
1150 out_vertex_size += 8;
1151 d3d_out_vertex_size += 7;
1152 glEnable(GL_AUTO_NORMAL);
1153 glEnable(GL_MAP2_TEXTURE_COORD_4);
1154 } else if(patch->has_texcoords) {
1155 FIXME("Texcoords not handled yet\n");
1156 feedback_type = GL_3D_COLOR_TEXTURE;
1157 out_vertex_size += 7;
1158 d3d_out_vertex_size += 4;
1159 glEnable(GL_MAP2_TEXTURE_COORD_4);
1160 } else if(patch->has_normals) {
1161 feedback_type = GL_3D_COLOR;
1162 out_vertex_size += 4;
1163 d3d_out_vertex_size += 3;
1164 glEnable(GL_AUTO_NORMAL);
1165 } else {
1166 feedback_type = GL_3D;
1168 checkGLcall("glEnable vertex attrib generation");
1170 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1171 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1172 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1174 glMap2f(GL_MAP2_VERTEX_3,
1175 0, 1, vtxStride / sizeof(float), info->Width,
1176 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1177 (const GLfloat *)data);
1178 checkGLcall("glMap2f");
1179 if(patch->has_texcoords) {
1180 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1181 0, 1, vtxStride / sizeof(float), info->Width,
1182 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1183 (const GLfloat *)data);
1184 checkGLcall("glMap2f");
1186 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1187 checkGLcall("glMapGrid2f");
1189 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1190 checkGLcall("glFeedbackBuffer");
1191 glRenderMode(GL_FEEDBACK);
1193 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1194 checkGLcall("glEvalMesh2\n");
1196 i = glRenderMode(GL_RENDER);
1197 if(i == -1) {
1198 LEAVE_GL();
1199 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1200 Sleep(10000);
1201 HeapFree(GetProcessHeap(), 0, feedbuffer);
1202 return WINED3DERR_DRIVERINTERNALERROR;
1203 } else if(i != buffer_size) {
1204 LEAVE_GL();
1205 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1206 Sleep(10000);
1207 HeapFree(GetProcessHeap(), 0, feedbuffer);
1208 return WINED3DERR_DRIVERINTERNALERROR;
1209 } else {
1210 TRACE("Got %d elements as expected\n", i);
1213 HeapFree(GetProcessHeap(), 0, patch->mem);
1214 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1215 i = 0;
1216 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1217 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1218 ERR("Unexpected token: %f\n", feedbuffer[j]);
1219 continue;
1221 if(feedbuffer[j + 1] != 3) {
1222 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1223 continue;
1225 /* Somehow there are different ideas about back / front facing, so fix up the
1226 * vertex order
1228 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1229 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1230 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1231 if(patch->has_normals) {
1232 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1233 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1234 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1236 i += d3d_out_vertex_size;
1238 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1239 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1240 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1241 if(patch->has_normals) {
1242 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1243 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1244 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1246 i += d3d_out_vertex_size;
1248 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1249 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1250 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1251 if(patch->has_normals) {
1252 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1253 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1254 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1256 i += d3d_out_vertex_size;
1259 if(patch->has_normals) {
1260 /* Now do the same with reverse light directions */
1261 static const GLfloat x[] = {-1, 0, 0, 0};
1262 static const GLfloat y[] = { 0, -1, 0, 0};
1263 static const GLfloat z[] = { 0, 0, -1, 0};
1264 glLightfv(GL_LIGHT0, GL_POSITION, x);
1265 glLightfv(GL_LIGHT1, GL_POSITION, y);
1266 glLightfv(GL_LIGHT2, GL_POSITION, z);
1267 checkGLcall("Setting up reverse light directions\n");
1269 glRenderMode(GL_FEEDBACK);
1270 checkGLcall("glRenderMode(GL_FEEDBACK)");
1271 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1272 checkGLcall("glEvalMesh2\n");
1273 i = glRenderMode(GL_RENDER);
1274 checkGLcall("glRenderMode(GL_RENDER)");
1276 i = 0;
1277 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1278 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1279 ERR("Unexpected token: %f\n", feedbuffer[j]);
1280 continue;
1282 if(feedbuffer[j + 1] != 3) {
1283 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1284 continue;
1286 if(patch->mem[i + 3] == 0.0)
1287 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1288 if(patch->mem[i + 4] == 0.0)
1289 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1290 if(patch->mem[i + 5] == 0.0)
1291 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1292 normalize_normal(patch->mem + i + 3);
1293 i += d3d_out_vertex_size;
1295 if(patch->mem[i + 3] == 0.0)
1296 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1297 if(patch->mem[i + 4] == 0.0)
1298 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1299 if(patch->mem[i + 5] == 0.0)
1300 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1301 normalize_normal(patch->mem + i + 3);
1302 i += d3d_out_vertex_size;
1304 if(patch->mem[i + 3] == 0.0)
1305 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1306 if(patch->mem[i + 4] == 0.0)
1307 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1308 if(patch->mem[i + 5] == 0.0)
1309 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1310 normalize_normal(patch->mem + i + 3);
1311 i += d3d_out_vertex_size;
1315 glDisable(GL_MAP2_VERTEX_3);
1316 glDisable(GL_AUTO_NORMAL);
1317 glDisable(GL_MAP2_NORMAL);
1318 glDisable(GL_MAP2_TEXTURE_COORD_4);
1319 checkGLcall("glDisable vertex attrib generation");
1320 LEAVE_GL();
1322 HeapFree(GetProcessHeap(), 0, feedbuffer);
1324 vtxStride = 3 * sizeof(float);
1325 if(patch->has_normals) {
1326 vtxStride += 3 * sizeof(float);
1328 if(patch->has_texcoords) {
1329 vtxStride += 4 * sizeof(float);
1331 memset(&patch->strided, 0, sizeof(&patch->strided));
1332 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1333 patch->strided.u.s.position.dwStride = vtxStride;
1334 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1335 patch->strided.u.s.position.streamNo = 255;
1337 if(patch->has_normals) {
1338 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1339 patch->strided.u.s.normal.dwStride = vtxStride;
1340 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1341 patch->strided.u.s.normal.streamNo = 255;
1343 if(patch->has_texcoords) {
1344 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1345 if(patch->has_normals) {
1346 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1348 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1349 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1350 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1351 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1352 * application.
1354 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;
1357 return WINED3D_OK;