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
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION This->adapter->gl_info
35 static void drawStridedFast(IWineD3DDevice *iface, GLenum primitive_type,
36 UINT min_vertex_idx, UINT max_vertex_idx, UINT count, UINT idx_size,
37 const void *idx_data, UINT start_idx)
39 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
43 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, primitive_type, count, min_vertex_idx);
46 glDrawElements(primitive_type, count,
47 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
48 (const char *)idx_data + (idx_size * start_idx));
49 checkGLcall("glDrawElements");
51 glDrawRangeElements(primitive_type, min_vertex_idx, max_vertex_idx, count,
52 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
53 (const char *)idx_data + (idx_size * start_idx));
54 checkGLcall("glDrawRangeElements");
59 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, primitive_type, start_idx, count);
61 glDrawArrays(primitive_type, start_idx, count);
62 checkGLcall("glDrawArrays");
67 * Actually draw using the supplied information.
68 * Slower GL version which extracts info about each vertex in turn
71 static void drawStridedSlow(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT NumVertexes,
72 GLenum glPrimType, const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
74 unsigned int textureNo = 0;
75 const WORD *pIdxBufS = NULL;
76 const DWORD *pIdxBufL = NULL;
78 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
79 const UINT *streamOffset = This->stateBlock->streamOffset;
80 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
81 BOOL pixelShader = use_ps(This->stateBlock);
82 BOOL specular_fog = FALSE;
83 UINT texture_stages = GL_LIMITS(texture_stages);
84 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
85 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
86 const struct wined3d_stream_info_element *element;
89 TRACE("Using slow vertex array code\n");
91 /* Variable Initialization */
93 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
94 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
95 * idxData will be != NULL
98 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
101 if (idxSize == 2) pIdxBufS = idxData;
102 else pIdxBufL = idxData;
103 } else if (idxData) {
104 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
108 /* Start drawing in GL */
109 VTRACE(("glBegin(%x)\n", glPrimType));
112 element = &si->elements[WINED3D_FFP_POSITION];
113 if (element->data) position = element->data + streamOffset[element->stream_idx];
115 element = &si->elements[WINED3D_FFP_NORMAL];
116 if (element->data) normal = element->data + streamOffset[element->stream_idx];
117 else glNormal3f(0, 0, 0);
119 element = &si->elements[WINED3D_FFP_DIFFUSE];
120 if (element->data) diffuse = element->data + streamOffset[element->stream_idx];
121 else glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
122 if (This->activeContext->num_untracked_materials && element->format_desc->format != WINED3DFMT_A8R8G8B8)
123 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format_desc->format));
125 element = &si->elements[WINED3D_FFP_SPECULAR];
128 specular = element->data + streamOffset[element->stream_idx];
130 /* special case where the fog density is stored in the specular alpha channel */
131 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
132 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
133 || si->elements[WINED3D_FFP_POSITION].format_desc->format == WINED3DFMT_R32G32B32A32_FLOAT)
134 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
136 if (GL_SUPPORT(EXT_FOG_COORD))
138 if (element->format_desc->format == WINED3DFMT_A8R8G8B8) specular_fog = TRUE;
139 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format_desc->format));
147 /* TODO: Use the fog table code from old ddraw */
148 FIXME("Implement fog for transformed vertices in software\n");
154 else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
156 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
159 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
161 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
162 int texture_idx = This->texUnitMap[textureNo];
164 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
166 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
170 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
172 if (texture_idx == -1) continue;
176 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
179 else if (coordIdx < 0)
181 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
185 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
188 texCoords[coordIdx] = element->data + streamOffset[element->stream_idx];
189 tex_mask |= (1 << textureNo);
193 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
194 if (GL_SUPPORT(ARB_MULTITEXTURE))
195 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
197 glTexCoord4f(0, 0, 0, 1);
201 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
202 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
205 /* For each primitive */
206 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
207 UINT texture, tmp_tex_mask;
208 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
209 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
212 /* For indexed data, we need to go a few more strides in */
213 if (idxData != NULL) {
215 /* Indexed so work out the number of strides to skip */
217 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
218 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
220 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
221 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
225 tmp_tex_mask = tex_mask;
226 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
232 if (!(tmp_tex_mask & 1)) continue;
234 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
235 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
237 texture_idx = This->texUnitMap[texture];
238 multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format_desc->emit_idx](
239 GL_TEXTURE0_ARB + texture_idx, ptr);
242 /* Diffuse -------------------------------- */
244 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
246 diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format_desc->emit_idx](ptrToCoords);
247 if(This->activeContext->num_untracked_materials) {
248 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
252 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
253 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
254 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
255 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
257 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
258 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
263 /* Specular ------------------------------- */
265 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
267 specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format_desc->emit_idx](ptrToCoords);
271 DWORD specularColor = *(const DWORD *)ptrToCoords;
272 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
276 /* Normal -------------------------------- */
277 if (normal != NULL) {
278 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
279 normal_funcs[si->elements[WINED3D_FFP_NORMAL].format_desc->emit_idx](ptrToCoords);
282 /* Position -------------------------------- */
284 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
285 position_funcs[si->elements[WINED3D_FFP_POSITION].format_desc->emit_idx](ptrToCoords);
288 /* For non indexed mode, step onto next parts */
289 if (idxData == NULL) {
295 checkGLcall("glEnd and previous calls");
298 static inline void send_attribute(IWineD3DDeviceImpl *This, WINED3DFORMAT format, const UINT index, const void *ptr)
302 case WINED3DFMT_R32_FLOAT:
303 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
305 case WINED3DFMT_R32G32_FLOAT:
306 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
308 case WINED3DFMT_R32G32B32_FLOAT:
309 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
311 case WINED3DFMT_R32G32B32A32_FLOAT:
312 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
315 case WINED3DFMT_R8G8B8A8_UINT:
316 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
318 case WINED3DFMT_A8R8G8B8:
319 if (GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA))
321 const DWORD *src = ptr;
322 DWORD c = *src & 0xff00ff00;
323 c |= (*src & 0xff0000) >> 16;
324 c |= (*src & 0xff) << 16;
325 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
328 /* else fallthrough */
329 case WINED3DFMT_R8G8B8A8_UNORM:
330 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
333 case WINED3DFMT_R16G16_SINT:
334 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
336 case WINED3DFMT_R16G16B16A16_SINT:
337 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
340 case WINED3DFMT_R16G16_SNORM:
342 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
343 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
346 case WINED3DFMT_R16G16_UNORM:
348 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
349 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
352 case WINED3DFMT_R16G16B16A16_SNORM:
353 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
355 case WINED3DFMT_R16G16B16A16_UNORM:
356 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
359 case WINED3DFMT_R10G10B10A2_UINT:
360 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
361 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
363 case WINED3DFMT_R10G10B10A2_SNORM:
364 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
365 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
368 case WINED3DFMT_R16G16_FLOAT:
369 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
370 * byte float according to the IEEE standard
372 if (GL_SUPPORT(NV_HALF_FLOAT)) {
373 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
375 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
376 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
377 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
380 case WINED3DFMT_R16G16B16A16_FLOAT:
381 if (GL_SUPPORT(NV_HALF_FLOAT)) {
382 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
384 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
385 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
386 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
387 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
388 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
393 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
398 static void drawStridedSlowVs(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT numberOfVertices,
399 GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
401 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
402 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
403 const WORD *pIdxBufS = NULL;
404 const DWORD *pIdxBufL = NULL;
407 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
411 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
412 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
413 * idxData will be != NULL
415 if(idxData == NULL) {
416 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
419 if (idxSize == 2) pIdxBufS = idxData;
420 else pIdxBufL = idxData;
421 } else if (idxData) {
422 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
426 /* Start drawing in GL */
427 VTRACE(("glBegin(%x)\n", glPrimitiveType));
428 glBegin(glPrimitiveType);
430 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
431 if (idxData != NULL) {
433 /* Indexed so work out the number of strides to skip */
435 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
436 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
438 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
439 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
443 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
444 if(!si->elements[i].data) continue;
446 ptr = si->elements[i].data +
447 si->elements[i].stride * SkipnStrides +
448 stateblock->streamOffset[si->elements[i].stream_idx];
450 send_attribute(This, si->elements[i].format_desc->format, i, ptr);
458 static inline void drawStridedInstanced(IWineD3DDevice *iface, const struct wined3d_stream_info *si,
459 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex,
462 UINT numInstances = 0, i;
463 int numInstancedAttribs = 0, j;
464 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
465 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
466 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
469 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
470 * We don't support this for now
472 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
473 * But the StreamSourceFreq value has a different meaning in that situation.
475 FIXME("Non-indexed instanced drawing is not supported\n");
479 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
481 /* First, figure out how many instances we have to draw */
482 for(i = 0; i < MAX_STREAMS; i++) {
483 /* Look at the streams and take the first one which matches */
484 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
485 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
486 if(stateblock->streamFreq[i] == 0){
489 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
491 break; /* break, because only the first suitable value is interesting */
495 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
497 if (stateblock->streamFlags[si->elements[i].stream_idx] & WINED3DSTREAMSOURCE_INSTANCEDATA)
499 instancedData[numInstancedAttribs] = i;
500 numInstancedAttribs++;
504 /* now draw numInstances instances :-) */
505 for(i = 0; i < numInstances; i++) {
506 /* Specify the instanced attributes using immediate mode calls */
507 for(j = 0; j < numInstancedAttribs; j++) {
508 const BYTE *ptr = si->elements[instancedData[j]].data +
509 si->elements[instancedData[j]].stride * i +
510 stateblock->streamOffset[si->elements[instancedData[j]].stream_idx];
511 if (si->elements[instancedData[j]].buffer_object)
513 struct wined3d_buffer *vb =
514 (struct wined3d_buffer *)stateblock->streamSource[si->elements[instancedData[j]].stream_idx];
515 ptr += (long) vb->resource.allocatedMemory;
518 send_attribute(This, si->elements[instancedData[j]].format_desc->format, instancedData[j], ptr);
521 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
522 (const char *)idxData+(idxSize * startIdx));
523 checkGLcall("glDrawElements");
527 static inline void remove_vbos(IWineD3DDeviceImpl *This, struct wined3d_stream_info *s)
531 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
533 struct wined3d_stream_info_element *e = &s->elements[i];
534 if (e->buffer_object)
536 struct wined3d_buffer *vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
537 e->buffer_object = 0;
538 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)vb->resource.allocatedMemory);
543 /* Routine common to the draw primitive and draw indexed primitive routines */
544 void drawPrimitive(IWineD3DDevice *iface, UINT index_count, UINT numberOfVertices,
545 UINT StartIdx, UINT idxSize, const void *idxData, UINT minIndex)
548 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
549 IWineD3DSurfaceImpl *target;
552 if (!index_count) return;
554 /* Invalidate the back buffer memory so LockRect will read it the next time */
555 for(i = 0; i < GL_LIMITS(buffers); i++) {
556 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
558 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
559 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
563 /* Signals other modules that a drawing is in progress and the stateblock finalized */
564 This->isInDraw = TRUE;
566 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
568 if (This->stencilBufferTarget) {
569 /* Note that this depends on the ActivateContext call above to set
570 * This->render_offscreen properly */
571 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
572 surface_load_ds_location(This->stencilBufferTarget, location);
573 surface_modify_ds_location(This->stencilBufferTarget, location);
576 /* Ok, we will be updating the screen from here onwards so grab the lock */
579 GLenum glPrimType = This->stateBlock->gl_primitive_type;
580 BOOL emulation = FALSE;
581 const struct wined3d_stream_info *stream_info = &This->strided_streams;
582 struct wined3d_stream_info stridedlcl;
584 if (!numberOfVertices) numberOfVertices = index_count;
586 if (!use_vs(This->stateBlock))
588 if (!This->strided_streams.position_transformed && This->activeContext->num_untracked_materials
589 && This->stateBlock->renderState[WINED3DRS_LIGHTING])
593 FIXME("Using software emulation because not all material properties could be tracked\n");
596 TRACE("Using software emulation because not all material properties could be tracked\n");
600 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
601 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
602 * to a float in the vertex buffer
606 FIXME("Using software emulation because manual fog coordinates are provided\n");
609 TRACE("Using software emulation because manual fog coordinates are provided\n");
615 stream_info = &stridedlcl;
616 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
617 remove_vbos(This, &stridedlcl);
621 if (This->useDrawStridedSlow || emulation) {
622 /* Immediate mode drawing */
623 if (use_vs(This->stateBlock))
627 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
630 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
632 drawStridedSlowVs(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
634 drawStridedSlow(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
636 } else if(This->instancedDraw) {
637 /* Instancing emulation with mixing immediate mode and arrays */
638 drawStridedInstanced(iface, &This->strided_streams, index_count,
639 glPrimType, idxData, idxSize, minIndex, StartIdx);
641 drawStridedFast(iface, glPrimType, minIndex, minIndex + numberOfVertices - 1,
642 index_count, idxSize, idxData, StartIdx);
646 /* Finished updating the screen, restore lock */
648 TRACE("Done all gl drawing\n");
651 #ifdef SHOW_FRAME_MAKEUP
653 static long int primCounter = 0;
654 /* NOTE: set primCounter to the value reported by drawprim
655 before you want to to write frame makeup to /tmp */
656 if (primCounter >= 0) {
657 WINED3DLOCKED_RECT r;
659 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
660 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
661 TRACE("Saving screenshot %s\n", buffer);
662 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
663 IWineD3DSurface_UnlockRect(This->render_targets[0]);
665 #ifdef SHOW_TEXTURE_MAKEUP
667 IWineD3DSurface *pSur;
669 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
670 if (This->stateBlock->textures[textureNo] != NULL) {
671 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
672 TRACE("Saving texture %s\n", buffer);
673 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
674 IWineD3DTexture_GetSurfaceLevel(This->stateBlock->textures[textureNo], 0, &pSur);
675 IWineD3DSurface_SaveSnapshot(pSur, buffer);
676 IWineD3DSurface_Release(pSur);
678 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
685 TRACE("drawprim #%ld\n", primCounter);
690 /* Control goes back to the device, stateblock values may change again */
691 This->isInDraw = FALSE;
694 static void normalize_normal(float *n) {
695 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
696 if(length == 0.0) return;
697 length = sqrt(length);
698 n[0] = n[0] / length;
699 n[1] = n[1] / length;
700 n[2] = n[2] / length;
703 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
705 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
706 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
707 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
708 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
711 * To read back, the opengl feedback mode is used. This creates a problem because we want
712 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
713 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
714 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
715 * them to [-1.0;+1.0] and set the viewport up to scale them back.
717 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
718 * resulting colors back to the normals.
720 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
721 * does not restore it because normally a draw follows immediately afterwards. The caller is
722 * responsible of taking care that either the gl states are restored, or the context activated
723 * for drawing to reset the lastWasBlit flag.
725 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
726 struct WineD3DRectPatch *patch) {
727 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
728 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
729 struct wined3d_stream_info stream_info;
730 struct wined3d_stream_info_element *e;
732 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
734 GLenum feedback_type;
737 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
740 device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
742 e = &stream_info.elements[WINED3D_FFP_POSITION];
743 if (e->buffer_object)
745 struct wined3d_buffer *vb;
746 vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
747 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)vb->resource.allocatedMemory);
749 vtxStride = e->stride;
751 vtxStride * info->Stride * info->StartVertexOffsetHeight +
752 vtxStride * info->StartVertexOffsetWidth;
754 /* Not entirely sure about what happens with transformed vertices */
755 if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
757 if(vtxStride % sizeof(GLfloat)) {
758 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
759 * I don't see how the stride could not be a multiple of 4, but make sure
762 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
764 if(info->Basis != WINED3DBASIS_BEZIER) {
765 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
767 if(info->Degree != WINED3DDEGREE_CUBIC) {
768 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
771 /* First, get the boundary cube of the input data */
772 for(j = 0; j < info->Height; j++) {
773 for(i = 0; i < info->Width; i++) {
774 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
775 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
776 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
777 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
778 if(v[2] < neg_z) neg_z = v[2];
782 /* This needs some improvements in the vertex decl code */
783 FIXME("Cannot find data to generate. Only generating position and normals\n");
784 patch->has_normals = TRUE;
785 patch->has_texcoords = FALSE;
787 /* Simply activate the context for blitting. This disables all the things we don't want and
788 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
789 * patch (as opposed to normal draws) will most likely need different changes anyway
791 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
794 glMatrixMode(GL_PROJECTION);
795 checkGLcall("glMatrixMode(GL_PROJECTION)");
797 checkGLcall("glLoadIndentity()");
798 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
799 glTranslatef(0, 0, 0.5);
800 checkGLcall("glScalef");
801 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
802 checkGLcall("glViewport");
804 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
805 * our feedback buffer parser
807 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
808 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
809 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
810 if(patch->has_normals) {
811 static const GLfloat black[] = {0, 0, 0, 0};
812 static const GLfloat red[] = {1, 0, 0, 0};
813 static const GLfloat green[] = {0, 1, 0, 0};
814 static const GLfloat blue[] = {0, 0, 1, 0};
815 static const GLfloat white[] = {1, 1, 1, 1};
816 glEnable(GL_LIGHTING);
817 checkGLcall("glEnable(GL_LIGHTING)");
818 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
819 checkGLcall("glLightModel for MODEL_AMBIENT");
820 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
822 for(i = 3; i < GL_LIMITS(lights); i++) {
823 glDisable(GL_LIGHT0 + i);
824 checkGLcall("glDisable(GL_LIGHT0 + i)");
825 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
828 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
829 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
830 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
831 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
832 glLightfv(GL_LIGHT0, GL_POSITION, red);
834 checkGLcall("Setting up light 1\n");
835 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
836 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
837 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
838 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
839 glLightfv(GL_LIGHT1, GL_POSITION, green);
841 checkGLcall("Setting up light 2\n");
842 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
843 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
844 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
845 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
846 glLightfv(GL_LIGHT2, GL_POSITION, blue);
848 checkGLcall("Setting up light 3\n");
850 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
851 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
852 glDisable(GL_COLOR_MATERIAL);
853 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
854 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
855 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
856 checkGLcall("Setting up materials\n");
859 /* Enable the needed maps.
860 * GL_MAP2_VERTEX_3 is needed for positional data.
861 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
862 * GL_MAP2_TEXTURE_COORD_4 for texture coords
864 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
865 out_vertex_size = 3 /* position */;
866 d3d_out_vertex_size = 3;
867 glEnable(GL_MAP2_VERTEX_3);
868 if(patch->has_normals && patch->has_texcoords) {
869 FIXME("Texcoords not handled yet\n");
870 feedback_type = GL_3D_COLOR_TEXTURE;
871 out_vertex_size += 8;
872 d3d_out_vertex_size += 7;
873 glEnable(GL_AUTO_NORMAL);
874 glEnable(GL_MAP2_TEXTURE_COORD_4);
875 } else if(patch->has_texcoords) {
876 FIXME("Texcoords not handled yet\n");
877 feedback_type = GL_3D_COLOR_TEXTURE;
878 out_vertex_size += 7;
879 d3d_out_vertex_size += 4;
880 glEnable(GL_MAP2_TEXTURE_COORD_4);
881 } else if(patch->has_normals) {
882 feedback_type = GL_3D_COLOR;
883 out_vertex_size += 4;
884 d3d_out_vertex_size += 3;
885 glEnable(GL_AUTO_NORMAL);
887 feedback_type = GL_3D;
889 checkGLcall("glEnable vertex attrib generation");
891 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
892 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
893 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
895 glMap2f(GL_MAP2_VERTEX_3,
896 0, 1, vtxStride / sizeof(float), info->Width,
897 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
898 (const GLfloat *)data);
899 checkGLcall("glMap2f");
900 if(patch->has_texcoords) {
901 glMap2f(GL_MAP2_TEXTURE_COORD_4,
902 0, 1, vtxStride / sizeof(float), info->Width,
903 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
904 (const GLfloat *)data);
905 checkGLcall("glMap2f");
907 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
908 checkGLcall("glMapGrid2f");
910 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
911 checkGLcall("glFeedbackBuffer");
912 glRenderMode(GL_FEEDBACK);
914 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
915 checkGLcall("glEvalMesh2\n");
917 i = glRenderMode(GL_RENDER);
920 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
922 HeapFree(GetProcessHeap(), 0, feedbuffer);
923 return WINED3DERR_DRIVERINTERNALERROR;
924 } else if(i != buffer_size) {
926 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
928 HeapFree(GetProcessHeap(), 0, feedbuffer);
929 return WINED3DERR_DRIVERINTERNALERROR;
931 TRACE("Got %d elements as expected\n", i);
934 HeapFree(GetProcessHeap(), 0, patch->mem);
935 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
937 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
938 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
939 ERR("Unexpected token: %f\n", feedbuffer[j]);
942 if(feedbuffer[j + 1] != 3) {
943 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
946 /* Somehow there are different ideas about back / front facing, so fix up the
949 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
950 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
951 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
952 if(patch->has_normals) {
953 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
954 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
955 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
957 i += d3d_out_vertex_size;
959 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
960 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
961 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
962 if(patch->has_normals) {
963 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
964 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
965 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
967 i += d3d_out_vertex_size;
969 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
970 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
971 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
972 if(patch->has_normals) {
973 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
974 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
975 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
977 i += d3d_out_vertex_size;
980 if(patch->has_normals) {
981 /* Now do the same with reverse light directions */
982 static const GLfloat x[] = {-1, 0, 0, 0};
983 static const GLfloat y[] = { 0, -1, 0, 0};
984 static const GLfloat z[] = { 0, 0, -1, 0};
985 glLightfv(GL_LIGHT0, GL_POSITION, x);
986 glLightfv(GL_LIGHT1, GL_POSITION, y);
987 glLightfv(GL_LIGHT2, GL_POSITION, z);
988 checkGLcall("Setting up reverse light directions\n");
990 glRenderMode(GL_FEEDBACK);
991 checkGLcall("glRenderMode(GL_FEEDBACK)");
992 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
993 checkGLcall("glEvalMesh2\n");
994 i = glRenderMode(GL_RENDER);
995 checkGLcall("glRenderMode(GL_RENDER)");
998 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
999 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1000 ERR("Unexpected token: %f\n", feedbuffer[j]);
1003 if(feedbuffer[j + 1] != 3) {
1004 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1007 if(patch->mem[i + 3] == 0.0)
1008 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1009 if(patch->mem[i + 4] == 0.0)
1010 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1011 if(patch->mem[i + 5] == 0.0)
1012 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1013 normalize_normal(patch->mem + i + 3);
1014 i += d3d_out_vertex_size;
1016 if(patch->mem[i + 3] == 0.0)
1017 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1018 if(patch->mem[i + 4] == 0.0)
1019 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1020 if(patch->mem[i + 5] == 0.0)
1021 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1022 normalize_normal(patch->mem + i + 3);
1023 i += d3d_out_vertex_size;
1025 if(patch->mem[i + 3] == 0.0)
1026 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1027 if(patch->mem[i + 4] == 0.0)
1028 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1029 if(patch->mem[i + 5] == 0.0)
1030 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1031 normalize_normal(patch->mem + i + 3);
1032 i += d3d_out_vertex_size;
1036 glDisable(GL_MAP2_VERTEX_3);
1037 glDisable(GL_AUTO_NORMAL);
1038 glDisable(GL_MAP2_NORMAL);
1039 glDisable(GL_MAP2_TEXTURE_COORD_4);
1040 checkGLcall("glDisable vertex attrib generation");
1043 HeapFree(GetProcessHeap(), 0, feedbuffer);
1045 vtxStride = 3 * sizeof(float);
1046 if(patch->has_normals) {
1047 vtxStride += 3 * sizeof(float);
1049 if(patch->has_texcoords) {
1050 vtxStride += 4 * sizeof(float);
1052 memset(&patch->strided, 0, sizeof(&patch->strided));
1053 patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1054 patch->strided.position.lpData = (BYTE *) patch->mem;
1055 patch->strided.position.dwStride = vtxStride;
1057 if(patch->has_normals) {
1058 patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1059 patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1060 patch->strided.normal.dwStride = vtxStride;
1062 if(patch->has_texcoords) {
1063 patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1064 patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1065 if(patch->has_normals) {
1066 patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1068 patch->strided.texCoords[0].dwStride = vtxStride;