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 /* GL locking is done by the caller */
36 static void drawStridedFast(IWineD3DDevice *iface, GLenum primitive_type,
37 UINT min_vertex_idx, UINT max_vertex_idx, UINT count, UINT idx_size,
38 const void *idx_data, UINT start_idx)
40 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
44 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, primitive_type, count, min_vertex_idx);
47 glDrawElements(primitive_type, count,
48 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
49 (const char *)idx_data + (idx_size * start_idx));
50 checkGLcall("glDrawElements");
52 glDrawRangeElements(primitive_type, min_vertex_idx, max_vertex_idx, count,
53 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
54 (const char *)idx_data + (idx_size * start_idx));
55 checkGLcall("glDrawRangeElements");
60 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, primitive_type, start_idx, count);
62 glDrawArrays(primitive_type, start_idx, count);
63 checkGLcall("glDrawArrays");
68 * Actually draw using the supplied information.
69 * Slower GL version which extracts info about each vertex in turn
72 /* GL locking is done by the caller */
73 static void drawStridedSlow(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT NumVertexes,
74 GLenum glPrimType, const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
76 struct wined3d_context *context = context_get_current();
77 unsigned int textureNo = 0;
78 const WORD *pIdxBufS = NULL;
79 const DWORD *pIdxBufL = NULL;
81 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
82 const UINT *streamOffset = This->stateBlock->streamOffset;
83 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
84 BOOL pixelShader = use_ps(This->stateBlock);
85 BOOL specular_fog = FALSE;
86 UINT texture_stages = GL_LIMITS(texture_stages);
87 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
88 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
89 const struct wined3d_stream_info_element *element;
90 UINT num_untracked_materials;
93 TRACE("Using slow vertex array code\n");
95 /* Variable Initialization */
97 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
98 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
99 * idxData will be != NULL
101 if(idxData == NULL) {
102 idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
105 if (idxSize == 2) pIdxBufS = idxData;
106 else pIdxBufL = idxData;
107 } else if (idxData) {
108 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
112 /* Start drawing in GL */
113 VTRACE(("glBegin(%x)\n", glPrimType));
116 element = &si->elements[WINED3D_FFP_POSITION];
117 if (element->data) position = element->data + streamOffset[element->stream_idx];
119 element = &si->elements[WINED3D_FFP_NORMAL];
120 if (element->data) normal = element->data + streamOffset[element->stream_idx];
121 else glNormal3f(0, 0, 0);
123 element = &si->elements[WINED3D_FFP_DIFFUSE];
124 if (element->data) diffuse = element->data + streamOffset[element->stream_idx];
125 else glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
126 num_untracked_materials = context->num_untracked_materials;
127 if (num_untracked_materials && element->format_desc->format != WINED3DFMT_A8R8G8B8)
128 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format_desc->format));
130 element = &si->elements[WINED3D_FFP_SPECULAR];
133 specular = element->data + streamOffset[element->stream_idx];
135 /* special case where the fog density is stored in the specular alpha channel */
136 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
137 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
138 || si->elements[WINED3D_FFP_POSITION].format_desc->format == WINED3DFMT_R32G32B32A32_FLOAT)
139 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
141 if (GL_SUPPORT(EXT_FOG_COORD))
143 if (element->format_desc->format == WINED3DFMT_A8R8G8B8) specular_fog = TRUE;
144 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format_desc->format));
152 /* TODO: Use the fog table code from old ddraw */
153 FIXME("Implement fog for transformed vertices in software\n");
159 else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
161 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
164 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
166 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
167 int texture_idx = This->texUnitMap[textureNo];
169 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
171 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
175 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
177 if (texture_idx == -1) continue;
181 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
184 else if (coordIdx < 0)
186 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
190 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
193 texCoords[coordIdx] = element->data + streamOffset[element->stream_idx];
194 tex_mask |= (1 << textureNo);
198 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
199 if (GL_SUPPORT(ARB_MULTITEXTURE))
200 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
202 glTexCoord4f(0, 0, 0, 1);
206 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
207 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
210 /* For each primitive */
211 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
212 UINT texture, tmp_tex_mask;
213 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
214 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
217 /* For indexed data, we need to go a few more strides in */
218 if (idxData != NULL) {
220 /* Indexed so work out the number of strides to skip */
222 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
223 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
225 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
226 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
230 tmp_tex_mask = tex_mask;
231 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
237 if (!(tmp_tex_mask & 1)) continue;
239 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
240 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
242 texture_idx = This->texUnitMap[texture];
243 multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format_desc->emit_idx](
244 GL_TEXTURE0_ARB + texture_idx, ptr);
247 /* Diffuse -------------------------------- */
249 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
251 diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format_desc->emit_idx](ptrToCoords);
252 if (num_untracked_materials)
254 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
258 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
259 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
260 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
261 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
263 for (i = 0; i < num_untracked_materials; ++i)
265 glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
270 /* Specular ------------------------------- */
272 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
274 specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format_desc->emit_idx](ptrToCoords);
278 DWORD specularColor = *(const DWORD *)ptrToCoords;
279 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
283 /* Normal -------------------------------- */
284 if (normal != NULL) {
285 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
286 normal_funcs[si->elements[WINED3D_FFP_NORMAL].format_desc->emit_idx](ptrToCoords);
289 /* Position -------------------------------- */
291 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
292 position_funcs[si->elements[WINED3D_FFP_POSITION].format_desc->emit_idx](ptrToCoords);
295 /* For non indexed mode, step onto next parts */
296 if (idxData == NULL) {
302 checkGLcall("glEnd and previous calls");
305 /* GL locking is done by the caller */
306 static inline void send_attribute(IWineD3DDeviceImpl *This, WINED3DFORMAT format, const UINT index, const void *ptr)
310 case WINED3DFMT_R32_FLOAT:
311 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
313 case WINED3DFMT_R32G32_FLOAT:
314 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
316 case WINED3DFMT_R32G32B32_FLOAT:
317 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
319 case WINED3DFMT_R32G32B32A32_FLOAT:
320 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
323 case WINED3DFMT_R8G8B8A8_UINT:
324 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
326 case WINED3DFMT_A8R8G8B8:
327 if (GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA))
329 const DWORD *src = ptr;
330 DWORD c = *src & 0xff00ff00;
331 c |= (*src & 0xff0000) >> 16;
332 c |= (*src & 0xff) << 16;
333 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
336 /* else fallthrough */
337 case WINED3DFMT_R8G8B8A8_UNORM:
338 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
341 case WINED3DFMT_R16G16_SINT:
342 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
344 case WINED3DFMT_R16G16B16A16_SINT:
345 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
348 case WINED3DFMT_R16G16_SNORM:
350 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
351 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
354 case WINED3DFMT_R16G16_UNORM:
356 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
357 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
360 case WINED3DFMT_R16G16B16A16_SNORM:
361 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
363 case WINED3DFMT_R16G16B16A16_UNORM:
364 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
367 case WINED3DFMT_R10G10B10A2_UINT:
368 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
369 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
371 case WINED3DFMT_R10G10B10A2_SNORM:
372 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
373 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
376 case WINED3DFMT_R16G16_FLOAT:
377 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
378 * byte float according to the IEEE standard
380 if (GL_SUPPORT(NV_HALF_FLOAT)) {
381 /* Not supported by GL_ARB_half_float_vertex */
382 GL_EXTCALL(glVertexAttrib2hvNV(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 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
389 case WINED3DFMT_R16G16B16A16_FLOAT:
390 if (GL_SUPPORT(NV_HALF_FLOAT)) {
391 /* Not supported by GL_ARB_half_float_vertex */
392 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
394 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
395 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
396 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
397 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
398 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
403 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
408 /* GL locking is done by the caller */
409 static void drawStridedSlowVs(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT numberOfVertices,
410 GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
412 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
413 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
414 const WORD *pIdxBufS = NULL;
415 const DWORD *pIdxBufL = NULL;
418 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
422 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
423 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
424 * idxData will be != NULL
426 if(idxData == NULL) {
427 idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
430 if (idxSize == 2) pIdxBufS = idxData;
431 else pIdxBufL = idxData;
432 } else if (idxData) {
433 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
437 /* Start drawing in GL */
438 VTRACE(("glBegin(%x)\n", glPrimitiveType));
439 glBegin(glPrimitiveType);
441 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
442 if (idxData != NULL) {
444 /* Indexed so work out the number of strides to skip */
446 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
447 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
449 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
450 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
454 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
455 if(!si->elements[i].data) continue;
457 ptr = si->elements[i].data +
458 si->elements[i].stride * SkipnStrides +
459 stateblock->streamOffset[si->elements[i].stream_idx];
461 send_attribute(This, si->elements[i].format_desc->format, i, ptr);
469 /* GL locking is done by the caller */
470 static inline void drawStridedInstanced(IWineD3DDevice *iface, const struct wined3d_stream_info *si,
471 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex,
474 UINT numInstances = 0, i;
475 int numInstancedAttribs = 0, j;
476 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
477 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
478 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
481 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
482 * We don't support this for now
484 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
485 * But the StreamSourceFreq value has a different meaning in that situation.
487 FIXME("Non-indexed instanced drawing is not supported\n");
491 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
493 /* First, figure out how many instances we have to draw */
494 for(i = 0; i < MAX_STREAMS; i++) {
495 /* Look at the streams and take the first one which matches */
496 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
497 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
498 if(stateblock->streamFreq[i] == 0){
501 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
503 break; /* break, because only the first suitable value is interesting */
507 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
509 if (stateblock->streamFlags[si->elements[i].stream_idx] & WINED3DSTREAMSOURCE_INSTANCEDATA)
511 instancedData[numInstancedAttribs] = i;
512 numInstancedAttribs++;
516 /* now draw numInstances instances :-) */
517 for(i = 0; i < numInstances; i++) {
518 /* Specify the instanced attributes using immediate mode calls */
519 for(j = 0; j < numInstancedAttribs; j++) {
520 const BYTE *ptr = si->elements[instancedData[j]].data +
521 si->elements[instancedData[j]].stride * i +
522 stateblock->streamOffset[si->elements[instancedData[j]].stream_idx];
523 if (si->elements[instancedData[j]].buffer_object)
525 struct wined3d_buffer *vb =
526 (struct wined3d_buffer *)stateblock->streamSource[si->elements[instancedData[j]].stream_idx];
527 ptr += (long) buffer_get_sysmem(vb);
530 send_attribute(This, si->elements[instancedData[j]].format_desc->format, instancedData[j], ptr);
533 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
534 (const char *)idxData+(idxSize * startIdx));
535 checkGLcall("glDrawElements");
539 static inline void remove_vbos(IWineD3DDeviceImpl *This, struct wined3d_stream_info *s)
543 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
545 struct wined3d_stream_info_element *e = &s->elements[i];
546 if (e->buffer_object)
548 struct wined3d_buffer *vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
549 e->buffer_object = 0;
550 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
555 /* Routine common to the draw primitive and draw indexed primitive routines */
556 void drawPrimitive(IWineD3DDevice *iface, UINT index_count, UINT numberOfVertices,
557 UINT StartIdx, UINT idxSize, const void *idxData, UINT minIndex)
560 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
561 IWineD3DSurfaceImpl *target;
562 struct wined3d_context *context;
565 if (!index_count) return;
567 if (This->stateBlock->renderState[WINED3DRS_COLORWRITEENABLE])
569 /* Invalidate the back buffer memory so LockRect will read it the next time */
570 for (i = 0; i < GL_LIMITS(buffers); ++i)
572 target = (IWineD3DSurfaceImpl *)This->render_targets[i];
575 IWineD3DSurface_LoadLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, NULL);
576 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, TRUE);
581 /* Signals other modules that a drawing is in progress and the stateblock finalized */
582 This->isInDraw = TRUE;
584 context = ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
586 if (This->stencilBufferTarget) {
587 /* Note that this depends on the ActivateContext call above to set
588 * This->render_offscreen properly. We don't currently take the
589 * Z-compare function into account, but we could skip loading the
590 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
591 * that we never copy the stencil data.*/
592 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
593 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE]
594 || This->stateBlock->renderState[WINED3DRS_ZENABLE])
595 surface_load_ds_location(This->stencilBufferTarget, context, location);
596 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE])
597 surface_modify_ds_location(This->stencilBufferTarget, location);
600 /* Ok, we will be updating the screen from here onwards so grab the lock */
603 GLenum glPrimType = This->stateBlock->gl_primitive_type;
604 BOOL emulation = FALSE;
605 const struct wined3d_stream_info *stream_info = &This->strided_streams;
606 struct wined3d_stream_info stridedlcl;
608 if (!numberOfVertices) numberOfVertices = index_count;
610 if (!use_vs(This->stateBlock))
612 if (!This->strided_streams.position_transformed && context->num_untracked_materials
613 && This->stateBlock->renderState[WINED3DRS_LIGHTING])
617 FIXME("Using software emulation because not all material properties could be tracked\n");
620 TRACE("Using software emulation because not all material properties could be tracked\n");
624 else if (context->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE])
626 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
627 * to a float in the vertex buffer
631 FIXME("Using software emulation because manual fog coordinates are provided\n");
634 TRACE("Using software emulation because manual fog coordinates are provided\n");
640 stream_info = &stridedlcl;
641 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
642 remove_vbos(This, &stridedlcl);
646 if (This->useDrawStridedSlow || emulation) {
647 /* Immediate mode drawing */
648 if (use_vs(This->stateBlock))
652 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
655 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
657 drawStridedSlowVs(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
659 drawStridedSlow(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
661 } else if(This->instancedDraw) {
662 /* Instancing emulation with mixing immediate mode and arrays */
663 drawStridedInstanced(iface, &This->strided_streams, index_count,
664 glPrimType, idxData, idxSize, minIndex, StartIdx);
666 drawStridedFast(iface, glPrimType, minIndex, minIndex + numberOfVertices - 1,
667 index_count, idxSize, idxData, StartIdx);
671 /* Finished updating the screen, restore lock */
673 TRACE("Done all gl drawing\n");
676 #ifdef SHOW_FRAME_MAKEUP
678 static long int primCounter = 0;
679 /* NOTE: set primCounter to the value reported by drawprim
680 before you want to to write frame makeup to /tmp */
681 if (primCounter >= 0) {
682 WINED3DLOCKED_RECT r;
684 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
685 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
686 TRACE("Saving screenshot %s\n", buffer);
687 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
688 IWineD3DSurface_UnlockRect(This->render_targets[0]);
690 #ifdef SHOW_TEXTURE_MAKEUP
692 IWineD3DSurface *pSur;
694 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
695 if (This->stateBlock->textures[textureNo] != NULL) {
696 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
697 TRACE("Saving texture %s\n", buffer);
698 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
699 IWineD3DTexture_GetSurfaceLevel(This->stateBlock->textures[textureNo], 0, &pSur);
700 IWineD3DSurface_SaveSnapshot(pSur, buffer);
701 IWineD3DSurface_Release(pSur);
703 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
710 TRACE("drawprim #%ld\n", primCounter);
715 /* Control goes back to the device, stateblock values may change again */
716 This->isInDraw = FALSE;
719 static void normalize_normal(float *n) {
720 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
721 if (length == 0.0f) return;
722 length = sqrt(length);
723 n[0] = n[0] / length;
724 n[1] = n[1] / length;
725 n[2] = n[2] / length;
728 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
730 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
731 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
732 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
733 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
736 * To read back, the opengl feedback mode is used. This creates a problem because we want
737 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
738 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
739 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
740 * them to [-1.0;+1.0] and set the viewport up to scale them back.
742 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
743 * resulting colors back to the normals.
745 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
746 * does not restore it because normally a draw follows immediately afterwards. The caller is
747 * responsible of taking care that either the gl states are restored, or the context activated
748 * for drawing to reset the lastWasBlit flag.
750 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
751 struct WineD3DRectPatch *patch) {
752 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
753 float max_x = 0.0f, max_y = 0.0f, max_z = 0.0f, neg_z = 0.0f;
754 struct wined3d_stream_info stream_info;
755 struct wined3d_stream_info_element *e;
757 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
759 GLenum feedback_type;
762 /* Simply activate the context for blitting. This disables all the things we don't want and
763 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
764 * patch (as opposed to normal draws) will most likely need different changes anyway. */
765 ActivateContext(This, NULL, CTXUSAGE_BLIT);
767 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
770 device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
772 e = &stream_info.elements[WINED3D_FFP_POSITION];
773 if (e->buffer_object)
775 struct wined3d_buffer *vb;
776 vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
777 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
779 vtxStride = e->stride;
781 vtxStride * info->Stride * info->StartVertexOffsetHeight +
782 vtxStride * info->StartVertexOffsetWidth;
784 /* Not entirely sure about what happens with transformed vertices */
785 if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
787 if(vtxStride % sizeof(GLfloat)) {
788 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
789 * I don't see how the stride could not be a multiple of 4, but make sure
792 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
794 if(info->Basis != WINED3DBASIS_BEZIER) {
795 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
797 if(info->Degree != WINED3DDEGREE_CUBIC) {
798 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
801 /* First, get the boundary cube of the input data */
802 for(j = 0; j < info->Height; j++) {
803 for(i = 0; i < info->Width; i++) {
804 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
805 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
806 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
807 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
808 if(v[2] < neg_z) neg_z = v[2];
812 /* This needs some improvements in the vertex decl code */
813 FIXME("Cannot find data to generate. Only generating position and normals\n");
814 patch->has_normals = TRUE;
815 patch->has_texcoords = FALSE;
819 glMatrixMode(GL_PROJECTION);
820 checkGLcall("glMatrixMode(GL_PROJECTION)");
822 checkGLcall("glLoadIndentity()");
823 glScalef(1.0f / (max_x), 1.0f / (max_y), max_z == 0.0f ? 1.0f : 1.0f / (2.0f * max_z));
824 glTranslatef(0.0f, 0.0f, 0.5f);
825 checkGLcall("glScalef");
826 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
827 checkGLcall("glViewport");
829 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
830 * our feedback buffer parser
832 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
833 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
834 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
835 if(patch->has_normals) {
836 static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
837 static const GLfloat red[] = {1.0f, 0.0f, 0.0f, 0.0f};
838 static const GLfloat green[] = {0.0f, 1.0f, 0.0f, 0.0f};
839 static const GLfloat blue[] = {0.0f, 0.0f, 1.0f, 0.0f};
840 static const GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
841 glEnable(GL_LIGHTING);
842 checkGLcall("glEnable(GL_LIGHTING)");
843 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
844 checkGLcall("glLightModel for MODEL_AMBIENT");
845 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
847 for(i = 3; i < GL_LIMITS(lights); i++) {
848 glDisable(GL_LIGHT0 + i);
849 checkGLcall("glDisable(GL_LIGHT0 + i)");
850 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
853 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
854 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
855 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
856 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
857 glLightfv(GL_LIGHT0, GL_POSITION, red);
859 checkGLcall("Setting up light 1");
860 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
861 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
862 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
863 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
864 glLightfv(GL_LIGHT1, GL_POSITION, green);
866 checkGLcall("Setting up light 2");
867 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
868 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
869 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
870 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
871 glLightfv(GL_LIGHT2, GL_POSITION, blue);
873 checkGLcall("Setting up light 3");
875 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
876 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
877 glDisable(GL_COLOR_MATERIAL);
878 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
879 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
880 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
881 checkGLcall("Setting up materials");
884 /* Enable the needed maps.
885 * GL_MAP2_VERTEX_3 is needed for positional data.
886 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
887 * GL_MAP2_TEXTURE_COORD_4 for texture coords
889 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
890 out_vertex_size = 3 /* position */;
891 d3d_out_vertex_size = 3;
892 glEnable(GL_MAP2_VERTEX_3);
893 if(patch->has_normals && patch->has_texcoords) {
894 FIXME("Texcoords not handled yet\n");
895 feedback_type = GL_3D_COLOR_TEXTURE;
896 out_vertex_size += 8;
897 d3d_out_vertex_size += 7;
898 glEnable(GL_AUTO_NORMAL);
899 glEnable(GL_MAP2_TEXTURE_COORD_4);
900 } else if(patch->has_texcoords) {
901 FIXME("Texcoords not handled yet\n");
902 feedback_type = GL_3D_COLOR_TEXTURE;
903 out_vertex_size += 7;
904 d3d_out_vertex_size += 4;
905 glEnable(GL_MAP2_TEXTURE_COORD_4);
906 } else if(patch->has_normals) {
907 feedback_type = GL_3D_COLOR;
908 out_vertex_size += 4;
909 d3d_out_vertex_size += 3;
910 glEnable(GL_AUTO_NORMAL);
912 feedback_type = GL_3D;
914 checkGLcall("glEnable vertex attrib generation");
916 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
917 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
918 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
920 glMap2f(GL_MAP2_VERTEX_3,
921 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
922 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
923 (const GLfloat *)data);
924 checkGLcall("glMap2f");
925 if(patch->has_texcoords) {
926 glMap2f(GL_MAP2_TEXTURE_COORD_4,
927 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
928 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
929 (const GLfloat *)data);
930 checkGLcall("glMap2f");
932 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0f, 1.0f, ceilf(patch->numSegs[1]), 0.0f, 1.0f);
933 checkGLcall("glMapGrid2f");
935 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
936 checkGLcall("glFeedbackBuffer");
937 glRenderMode(GL_FEEDBACK);
939 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
940 checkGLcall("glEvalMesh2");
942 i = glRenderMode(GL_RENDER);
945 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
946 HeapFree(GetProcessHeap(), 0, feedbuffer);
947 return WINED3DERR_DRIVERINTERNALERROR;
948 } else if(i != buffer_size) {
950 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
951 HeapFree(GetProcessHeap(), 0, feedbuffer);
952 return WINED3DERR_DRIVERINTERNALERROR;
954 TRACE("Got %d elements as expected\n", i);
957 HeapFree(GetProcessHeap(), 0, patch->mem);
958 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
960 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
961 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
962 ERR("Unexpected token: %f\n", feedbuffer[j]);
965 if(feedbuffer[j + 1] != 3) {
966 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
969 /* Somehow there are different ideas about back / front facing, so fix up the
972 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
973 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
974 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 3 */
975 if(patch->has_normals) {
976 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
977 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
978 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
980 i += d3d_out_vertex_size;
982 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
983 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
984 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 2 */
985 if(patch->has_normals) {
986 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
987 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
988 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
990 i += d3d_out_vertex_size;
992 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
993 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
994 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 1 */
995 if(patch->has_normals) {
996 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
997 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
998 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1000 i += d3d_out_vertex_size;
1003 if(patch->has_normals) {
1004 /* Now do the same with reverse light directions */
1005 static const GLfloat x[] = {-1.0f, 0.0f, 0.0f, 0.0f};
1006 static const GLfloat y[] = { 0.0f, -1.0f, 0.0f, 0.0f};
1007 static const GLfloat z[] = { 0.0f, 0.0f, -1.0f, 0.0f};
1008 glLightfv(GL_LIGHT0, GL_POSITION, x);
1009 glLightfv(GL_LIGHT1, GL_POSITION, y);
1010 glLightfv(GL_LIGHT2, GL_POSITION, z);
1011 checkGLcall("Setting up reverse light directions");
1013 glRenderMode(GL_FEEDBACK);
1014 checkGLcall("glRenderMode(GL_FEEDBACK)");
1015 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1016 checkGLcall("glEvalMesh2");
1017 i = glRenderMode(GL_RENDER);
1018 checkGLcall("glRenderMode(GL_RENDER)");
1021 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1022 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1023 ERR("Unexpected token: %f\n", feedbuffer[j]);
1026 if(feedbuffer[j + 1] != 3) {
1027 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1030 if(patch->mem[i + 3] == 0.0f)
1031 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1032 if(patch->mem[i + 4] == 0.0f)
1033 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1034 if(patch->mem[i + 5] == 0.0f)
1035 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1036 normalize_normal(patch->mem + i + 3);
1037 i += d3d_out_vertex_size;
1039 if(patch->mem[i + 3] == 0.0f)
1040 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1041 if(patch->mem[i + 4] == 0.0f)
1042 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1043 if(patch->mem[i + 5] == 0.0f)
1044 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1045 normalize_normal(patch->mem + i + 3);
1046 i += d3d_out_vertex_size;
1048 if(patch->mem[i + 3] == 0.0f)
1049 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1050 if(patch->mem[i + 4] == 0.0f)
1051 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1052 if(patch->mem[i + 5] == 0.0f)
1053 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1054 normalize_normal(patch->mem + i + 3);
1055 i += d3d_out_vertex_size;
1059 glDisable(GL_MAP2_VERTEX_3);
1060 glDisable(GL_AUTO_NORMAL);
1061 glDisable(GL_MAP2_NORMAL);
1062 glDisable(GL_MAP2_TEXTURE_COORD_4);
1063 checkGLcall("glDisable vertex attrib generation");
1066 HeapFree(GetProcessHeap(), 0, feedbuffer);
1068 vtxStride = 3 * sizeof(float);
1069 if(patch->has_normals) {
1070 vtxStride += 3 * sizeof(float);
1072 if(patch->has_texcoords) {
1073 vtxStride += 4 * sizeof(float);
1075 memset(&patch->strided, 0, sizeof(&patch->strided));
1076 patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1077 patch->strided.position.lpData = (BYTE *) patch->mem;
1078 patch->strided.position.dwStride = vtxStride;
1080 if(patch->has_normals) {
1081 patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1082 patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1083 patch->strided.normal.dwStride = vtxStride;
1085 if(patch->has_texcoords) {
1086 patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1087 patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1088 if(patch->has_normals) {
1089 patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1091 patch->strided.texCoords[0].dwStride = vtxStride;