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
10 * Copyright 2009 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
31 #define GLINFO_LOCATION This->adapter->gl_info
36 /* GL locking is done by the caller */
37 static void drawStridedFast(IWineD3DDevice *iface, GLenum primitive_type,
38 UINT count, UINT idx_size, const void *idx_data, UINT start_idx)
40 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
44 TRACE("(%p) : glElements(%x, %d, ...)\n", This, primitive_type, count);
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");
53 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, primitive_type, start_idx, count);
55 glDrawArrays(primitive_type, start_idx, count);
56 checkGLcall("glDrawArrays");
61 * Actually draw using the supplied information.
62 * Slower GL version which extracts info about each vertex in turn
65 /* GL locking is done by the caller */
66 static void drawStridedSlow(IWineD3DDevice *iface, const struct wined3d_context *context,
67 const struct wined3d_stream_info *si, UINT NumVertexes, GLenum glPrimType,
68 const void *idxData, UINT idxSize, UINT startIdx)
70 unsigned int textureNo = 0;
71 const WORD *pIdxBufS = NULL;
72 const DWORD *pIdxBufL = NULL;
74 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
75 const UINT *streamOffset = This->stateBlock->streamOffset;
76 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
77 BOOL pixelShader = use_ps(This->stateBlock);
78 BOOL specular_fog = FALSE;
79 UINT texture_stages = GL_LIMITS(texture_stages);
80 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
81 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
82 const struct wined3d_stream_info_element *element;
83 UINT num_untracked_materials;
86 TRACE("Using slow vertex array code\n");
88 /* Variable Initialization */
90 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
91 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
92 * idxData will be != NULL
95 idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
98 if (idxSize == 2) pIdxBufS = idxData;
99 else pIdxBufL = idxData;
100 } else if (idxData) {
101 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
105 /* Start drawing in GL */
106 VTRACE(("glBegin(%x)\n", glPrimType));
109 if (si->use_map & (1 << WINED3D_FFP_POSITION))
111 element = &si->elements[WINED3D_FFP_POSITION];
112 position = element->data + streamOffset[element->stream_idx];
115 if (si->use_map & (1 << WINED3D_FFP_NORMAL))
117 element = &si->elements[WINED3D_FFP_NORMAL];
118 normal = element->data + streamOffset[element->stream_idx];
125 num_untracked_materials = context->num_untracked_materials;
126 if (si->use_map & (1 << WINED3D_FFP_DIFFUSE))
128 element = &si->elements[WINED3D_FFP_DIFFUSE];
129 diffuse = element->data + streamOffset[element->stream_idx];
131 if (num_untracked_materials && element->format_desc->format != WINED3DFMT_A8R8G8B8)
132 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format_desc->format));
136 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
139 if (si->use_map & (1 << WINED3D_FFP_SPECULAR))
141 element = &si->elements[WINED3D_FFP_SPECULAR];
142 specular = element->data + streamOffset[element->stream_idx];
144 /* special case where the fog density is stored in the specular alpha channel */
145 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
146 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
147 || si->elements[WINED3D_FFP_POSITION].format_desc->format == WINED3DFMT_R32G32B32A32_FLOAT)
148 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
150 if (GL_SUPPORT(EXT_FOG_COORD))
152 if (element->format_desc->format == WINED3DFMT_A8R8G8B8) specular_fog = TRUE;
153 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format_desc->format));
161 /* TODO: Use the fog table code from old ddraw */
162 FIXME("Implement fog for transformed vertices in software\n");
168 else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
170 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
173 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
175 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
176 DWORD texture_idx = This->texUnitMap[textureNo];
178 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
180 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
184 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
186 if (texture_idx == WINED3D_UNMAPPED_STAGE) continue;
190 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
193 else if (coordIdx < 0)
195 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
199 if (si->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + coordIdx)))
201 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
202 texCoords[coordIdx] = element->data + streamOffset[element->stream_idx];
203 tex_mask |= (1 << textureNo);
207 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
208 if (GL_SUPPORT(ARB_MULTITEXTURE))
209 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
211 glTexCoord4f(0, 0, 0, 1);
215 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
216 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
219 /* For each primitive */
220 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
221 UINT texture, tmp_tex_mask;
222 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
223 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
226 /* For indexed data, we need to go a few more strides in */
227 if (idxData != NULL) {
229 /* Indexed so work out the number of strides to skip */
231 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
232 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
234 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
235 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
239 tmp_tex_mask = tex_mask;
240 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
246 if (!(tmp_tex_mask & 1)) continue;
248 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
249 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
251 texture_idx = This->texUnitMap[texture];
252 multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format_desc->emit_idx](
253 GL_TEXTURE0_ARB + texture_idx, ptr);
256 /* Diffuse -------------------------------- */
258 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
260 diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format_desc->emit_idx](ptrToCoords);
261 if (num_untracked_materials)
263 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
267 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
268 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
269 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
270 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
272 for (i = 0; i < num_untracked_materials; ++i)
274 glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
279 /* Specular ------------------------------- */
281 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
283 specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format_desc->emit_idx](ptrToCoords);
287 DWORD specularColor = *(const DWORD *)ptrToCoords;
288 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
292 /* Normal -------------------------------- */
293 if (normal != NULL) {
294 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
295 normal_funcs[si->elements[WINED3D_FFP_NORMAL].format_desc->emit_idx](ptrToCoords);
298 /* Position -------------------------------- */
300 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
301 position_funcs[si->elements[WINED3D_FFP_POSITION].format_desc->emit_idx](ptrToCoords);
304 /* For non indexed mode, step onto next parts */
305 if (idxData == NULL) {
311 checkGLcall("glEnd and previous calls");
314 /* GL locking is done by the caller */
315 static inline void send_attribute(IWineD3DDeviceImpl *This, WINED3DFORMAT format, const UINT index, const void *ptr)
319 case WINED3DFMT_R32_FLOAT:
320 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
322 case WINED3DFMT_R32G32_FLOAT:
323 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
325 case WINED3DFMT_R32G32B32_FLOAT:
326 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
328 case WINED3DFMT_R32G32B32A32_FLOAT:
329 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
332 case WINED3DFMT_R8G8B8A8_UINT:
333 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
335 case WINED3DFMT_A8R8G8B8:
336 if (GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA))
338 const DWORD *src = ptr;
339 DWORD c = *src & 0xff00ff00;
340 c |= (*src & 0xff0000) >> 16;
341 c |= (*src & 0xff) << 16;
342 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
345 /* else fallthrough */
346 case WINED3DFMT_R8G8B8A8_UNORM:
347 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
350 case WINED3DFMT_R16G16_SINT:
351 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
353 case WINED3DFMT_R16G16B16A16_SINT:
354 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
357 case WINED3DFMT_R16G16_SNORM:
359 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
360 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
363 case WINED3DFMT_R16G16_UNORM:
365 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
366 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
369 case WINED3DFMT_R16G16B16A16_SNORM:
370 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
372 case WINED3DFMT_R16G16B16A16_UNORM:
373 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
376 case WINED3DFMT_R10G10B10A2_UINT:
377 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
378 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
380 case WINED3DFMT_R10G10B10A2_SNORM:
381 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
382 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
385 case WINED3DFMT_R16G16_FLOAT:
386 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
387 * byte float according to the IEEE standard
389 if (GL_SUPPORT(NV_HALF_FLOAT)) {
390 /* Not supported by GL_ARB_half_float_vertex */
391 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
393 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
394 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
395 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
398 case WINED3DFMT_R16G16B16A16_FLOAT:
399 if (GL_SUPPORT(NV_HALF_FLOAT)) {
400 /* Not supported by GL_ARB_half_float_vertex */
401 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
403 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
404 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
405 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
406 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
407 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
412 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
417 /* GL locking is done by the caller */
418 static void drawStridedSlowVs(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT numberOfVertices,
419 GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT startIdx)
421 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
422 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
423 const WORD *pIdxBufS = NULL;
424 const DWORD *pIdxBufL = NULL;
427 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
431 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
432 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
433 * idxData will be != NULL
435 if(idxData == NULL) {
436 idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
439 if (idxSize == 2) pIdxBufS = idxData;
440 else pIdxBufL = idxData;
441 } else if (idxData) {
442 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
446 /* Start drawing in GL */
447 VTRACE(("glBegin(%x)\n", glPrimitiveType));
448 glBegin(glPrimitiveType);
450 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
451 if (idxData != NULL) {
453 /* Indexed so work out the number of strides to skip */
455 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
456 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
458 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
459 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
463 for (i = MAX_ATTRIBS - 1; i >= 0; i--)
465 if (!(si->use_map & (1 << i))) continue;
467 ptr = si->elements[i].data +
468 si->elements[i].stride * SkipnStrides +
469 stateblock->streamOffset[si->elements[i].stream_idx];
471 send_attribute(This, si->elements[i].format_desc->format, i, ptr);
479 /* GL locking is done by the caller */
480 static inline void drawStridedInstanced(IWineD3DDevice *iface, const struct wined3d_stream_info *si,
481 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, UINT idxSize,
484 UINT numInstances = 0, i;
485 int numInstancedAttribs = 0, j;
486 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
487 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
488 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
491 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
492 * We don't support this for now
494 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
495 * But the StreamSourceFreq value has a different meaning in that situation.
497 FIXME("Non-indexed instanced drawing is not supported\n");
501 TRACE("(%p) : glElements(%x, %d, ...)\n", This, glPrimitiveType, numberOfVertices);
503 /* First, figure out how many instances we have to draw */
504 for(i = 0; i < MAX_STREAMS; i++) {
505 /* Look at the streams and take the first one which matches */
506 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
507 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
508 if(stateblock->streamFreq[i] == 0){
511 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
513 break; /* break, because only the first suitable value is interesting */
517 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
519 if (!(si->use_map & (1 << i))) continue;
521 if (stateblock->streamFlags[si->elements[i].stream_idx] & WINED3DSTREAMSOURCE_INSTANCEDATA)
523 instancedData[numInstancedAttribs] = i;
524 numInstancedAttribs++;
528 /* now draw numInstances instances :-) */
529 for(i = 0; i < numInstances; i++) {
530 /* Specify the instanced attributes using immediate mode calls */
531 for(j = 0; j < numInstancedAttribs; j++) {
532 const BYTE *ptr = si->elements[instancedData[j]].data +
533 si->elements[instancedData[j]].stride * i +
534 stateblock->streamOffset[si->elements[instancedData[j]].stream_idx];
535 if (si->elements[instancedData[j]].buffer_object)
537 struct wined3d_buffer *vb =
538 (struct wined3d_buffer *)stateblock->streamSource[si->elements[instancedData[j]].stream_idx];
539 ptr += (long) buffer_get_sysmem(vb);
542 send_attribute(This, si->elements[instancedData[j]].format_desc->format, instancedData[j], ptr);
545 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
546 (const char *)idxData+(idxSize * startIdx));
547 checkGLcall("glDrawElements");
551 static inline void remove_vbos(IWineD3DDeviceImpl *This, struct wined3d_stream_info *s)
555 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
557 struct wined3d_stream_info_element *e;
559 if (!(s->use_map & (1 << i))) continue;
562 if (e->buffer_object)
564 struct wined3d_buffer *vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
565 e->buffer_object = 0;
566 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
571 /* Routine common to the draw primitive and draw indexed primitive routines */
572 void drawPrimitive(IWineD3DDevice *iface, UINT index_count, UINT StartIdx, UINT idxSize, const void *idxData)
575 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
576 IWineD3DSurfaceImpl *target;
577 struct wined3d_context *context;
580 if (!index_count) return;
582 if (This->stateBlock->renderState[WINED3DRS_COLORWRITEENABLE])
584 /* Invalidate the back buffer memory so LockRect will read it the next time */
585 for (i = 0; i < GL_LIMITS(buffers); ++i)
587 target = (IWineD3DSurfaceImpl *)This->render_targets[i];
590 IWineD3DSurface_LoadLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, NULL);
591 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, TRUE);
596 /* Signals other modules that a drawing is in progress and the stateblock finalized */
597 This->isInDraw = TRUE;
599 context = ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
601 if (This->stencilBufferTarget) {
602 /* Note that this depends on the ActivateContext call above to set
603 * This->render_offscreen properly. We don't currently take the
604 * Z-compare function into account, but we could skip loading the
605 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
606 * that we never copy the stencil data.*/
607 DWORD location = context->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
608 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE]
609 || This->stateBlock->renderState[WINED3DRS_ZENABLE])
610 surface_load_ds_location(This->stencilBufferTarget, context, location);
611 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE])
612 surface_modify_ds_location(This->stencilBufferTarget, location);
615 /* Ok, we will be updating the screen from here onwards so grab the lock */
618 GLenum glPrimType = This->stateBlock->gl_primitive_type;
619 BOOL emulation = FALSE;
620 const struct wined3d_stream_info *stream_info = &This->strided_streams;
621 struct wined3d_stream_info stridedlcl;
623 if (!use_vs(This->stateBlock))
625 if (!This->strided_streams.position_transformed && context->num_untracked_materials
626 && This->stateBlock->renderState[WINED3DRS_LIGHTING])
630 FIXME("Using software emulation because not all material properties could be tracked\n");
633 TRACE("Using software emulation because not all material properties could be tracked\n");
637 else if (context->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE])
639 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
640 * to a float in the vertex buffer
644 FIXME("Using software emulation because manual fog coordinates are provided\n");
647 TRACE("Using software emulation because manual fog coordinates are provided\n");
653 stream_info = &stridedlcl;
654 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
655 remove_vbos(This, &stridedlcl);
659 if (This->useDrawStridedSlow || emulation) {
660 /* Immediate mode drawing */
661 if (use_vs(This->stateBlock))
665 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
668 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
670 drawStridedSlowVs(iface, stream_info, index_count, glPrimType, idxData, idxSize, StartIdx);
672 drawStridedSlow(iface, context, stream_info, index_count,
673 glPrimType, idxData, idxSize, StartIdx);
675 } else if(This->instancedDraw) {
676 /* Instancing emulation with mixing immediate mode and arrays */
677 drawStridedInstanced(iface, &This->strided_streams, index_count,
678 glPrimType, idxData, idxSize, StartIdx);
680 drawStridedFast(iface, glPrimType, index_count, idxSize, idxData, StartIdx);
684 /* Finished updating the screen, restore lock */
686 TRACE("Done all gl drawing\n");
689 #ifdef SHOW_FRAME_MAKEUP
691 static long int primCounter = 0;
692 /* NOTE: set primCounter to the value reported by drawprim
693 before you want to to write frame makeup to /tmp */
694 if (primCounter >= 0) {
695 WINED3DLOCKED_RECT r;
697 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
698 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
699 TRACE("Saving screenshot %s\n", buffer);
700 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
701 IWineD3DSurface_UnlockRect(This->render_targets[0]);
703 #ifdef SHOW_TEXTURE_MAKEUP
705 IWineD3DSurface *pSur;
707 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
708 if (This->stateBlock->textures[textureNo] != NULL) {
709 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
710 TRACE("Saving texture %s\n", buffer);
711 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
712 IWineD3DTexture_GetSurfaceLevel(This->stateBlock->textures[textureNo], 0, &pSur);
713 IWineD3DSurface_SaveSnapshot(pSur, buffer);
714 IWineD3DSurface_Release(pSur);
716 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
723 TRACE("drawprim #%ld\n", primCounter);
728 /* Control goes back to the device, stateblock values may change again */
729 This->isInDraw = FALSE;
732 static void normalize_normal(float *n) {
733 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
734 if (length == 0.0f) return;
735 length = sqrt(length);
736 n[0] = n[0] / length;
737 n[1] = n[1] / length;
738 n[2] = n[2] / length;
741 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
743 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
744 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
745 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
746 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
749 * To read back, the opengl feedback mode is used. This creates a problem because we want
750 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
751 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
752 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
753 * them to [-1.0;+1.0] and set the viewport up to scale them back.
755 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
756 * resulting colors back to the normals.
758 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
759 * does not restore it because normally a draw follows immediately afterwards. The caller is
760 * responsible of taking care that either the gl states are restored, or the context activated
761 * for drawing to reset the lastWasBlit flag.
763 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
764 struct WineD3DRectPatch *patch) {
765 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
766 float max_x = 0.0f, max_y = 0.0f, max_z = 0.0f, neg_z = 0.0f;
767 struct wined3d_stream_info stream_info;
768 struct wined3d_stream_info_element *e;
770 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
772 GLenum feedback_type;
775 /* Simply activate the context for blitting. This disables all the things we don't want and
776 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
777 * patch (as opposed to normal draws) will most likely need different changes anyway. */
778 ActivateContext(This, NULL, CTXUSAGE_BLIT);
780 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
783 device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
785 e = &stream_info.elements[WINED3D_FFP_POSITION];
786 if (e->buffer_object)
788 struct wined3d_buffer *vb;
789 vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
790 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
792 vtxStride = e->stride;
794 vtxStride * info->Stride * info->StartVertexOffsetHeight +
795 vtxStride * info->StartVertexOffsetWidth;
797 /* Not entirely sure about what happens with transformed vertices */
798 if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
800 if(vtxStride % sizeof(GLfloat)) {
801 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
802 * I don't see how the stride could not be a multiple of 4, but make sure
805 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
807 if(info->Basis != WINED3DBASIS_BEZIER) {
808 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
810 if(info->Degree != WINED3DDEGREE_CUBIC) {
811 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
814 /* First, get the boundary cube of the input data */
815 for(j = 0; j < info->Height; j++) {
816 for(i = 0; i < info->Width; i++) {
817 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
818 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
819 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
820 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
821 if(v[2] < neg_z) neg_z = v[2];
825 /* This needs some improvements in the vertex decl code */
826 FIXME("Cannot find data to generate. Only generating position and normals\n");
827 patch->has_normals = TRUE;
828 patch->has_texcoords = FALSE;
832 glMatrixMode(GL_PROJECTION);
833 checkGLcall("glMatrixMode(GL_PROJECTION)");
835 checkGLcall("glLoadIndentity()");
836 glScalef(1.0f / (max_x), 1.0f / (max_y), max_z == 0.0f ? 1.0f : 1.0f / (2.0f * max_z));
837 glTranslatef(0.0f, 0.0f, 0.5f);
838 checkGLcall("glScalef");
839 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
840 checkGLcall("glViewport");
842 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
843 * our feedback buffer parser
845 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
846 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
847 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
848 if(patch->has_normals) {
849 static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
850 static const GLfloat red[] = {1.0f, 0.0f, 0.0f, 0.0f};
851 static const GLfloat green[] = {0.0f, 1.0f, 0.0f, 0.0f};
852 static const GLfloat blue[] = {0.0f, 0.0f, 1.0f, 0.0f};
853 static const GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
854 glEnable(GL_LIGHTING);
855 checkGLcall("glEnable(GL_LIGHTING)");
856 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
857 checkGLcall("glLightModel for MODEL_AMBIENT");
858 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
860 for(i = 3; i < GL_LIMITS(lights); i++) {
861 glDisable(GL_LIGHT0 + i);
862 checkGLcall("glDisable(GL_LIGHT0 + i)");
863 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
866 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
867 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
868 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
869 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
870 glLightfv(GL_LIGHT0, GL_POSITION, red);
872 checkGLcall("Setting up light 1");
873 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
874 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
875 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
876 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
877 glLightfv(GL_LIGHT1, GL_POSITION, green);
879 checkGLcall("Setting up light 2");
880 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
881 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
882 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
883 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
884 glLightfv(GL_LIGHT2, GL_POSITION, blue);
886 checkGLcall("Setting up light 3");
888 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
889 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
890 glDisable(GL_COLOR_MATERIAL);
891 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
892 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
893 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
894 checkGLcall("Setting up materials");
897 /* Enable the needed maps.
898 * GL_MAP2_VERTEX_3 is needed for positional data.
899 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
900 * GL_MAP2_TEXTURE_COORD_4 for texture coords
902 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
903 out_vertex_size = 3 /* position */;
904 d3d_out_vertex_size = 3;
905 glEnable(GL_MAP2_VERTEX_3);
906 if(patch->has_normals && patch->has_texcoords) {
907 FIXME("Texcoords not handled yet\n");
908 feedback_type = GL_3D_COLOR_TEXTURE;
909 out_vertex_size += 8;
910 d3d_out_vertex_size += 7;
911 glEnable(GL_AUTO_NORMAL);
912 glEnable(GL_MAP2_TEXTURE_COORD_4);
913 } else if(patch->has_texcoords) {
914 FIXME("Texcoords not handled yet\n");
915 feedback_type = GL_3D_COLOR_TEXTURE;
916 out_vertex_size += 7;
917 d3d_out_vertex_size += 4;
918 glEnable(GL_MAP2_TEXTURE_COORD_4);
919 } else if(patch->has_normals) {
920 feedback_type = GL_3D_COLOR;
921 out_vertex_size += 4;
922 d3d_out_vertex_size += 3;
923 glEnable(GL_AUTO_NORMAL);
925 feedback_type = GL_3D;
927 checkGLcall("glEnable vertex attrib generation");
929 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
930 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
931 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
933 glMap2f(GL_MAP2_VERTEX_3,
934 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
935 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
936 (const GLfloat *)data);
937 checkGLcall("glMap2f");
938 if(patch->has_texcoords) {
939 glMap2f(GL_MAP2_TEXTURE_COORD_4,
940 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
941 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
942 (const GLfloat *)data);
943 checkGLcall("glMap2f");
945 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0f, 1.0f, ceilf(patch->numSegs[1]), 0.0f, 1.0f);
946 checkGLcall("glMapGrid2f");
948 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
949 checkGLcall("glFeedbackBuffer");
950 glRenderMode(GL_FEEDBACK);
952 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
953 checkGLcall("glEvalMesh2");
955 i = glRenderMode(GL_RENDER);
958 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
959 HeapFree(GetProcessHeap(), 0, feedbuffer);
960 return WINED3DERR_DRIVERINTERNALERROR;
961 } else if(i != buffer_size) {
963 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
964 HeapFree(GetProcessHeap(), 0, feedbuffer);
965 return WINED3DERR_DRIVERINTERNALERROR;
967 TRACE("Got %d elements as expected\n", i);
970 HeapFree(GetProcessHeap(), 0, patch->mem);
971 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
973 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
974 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
975 ERR("Unexpected token: %f\n", feedbuffer[j]);
978 if(feedbuffer[j + 1] != 3) {
979 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
982 /* Somehow there are different ideas about back / front facing, so fix up the
985 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
986 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
987 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 3 */
988 if(patch->has_normals) {
989 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
990 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
991 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
993 i += d3d_out_vertex_size;
995 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
996 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
997 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 2 */
998 if(patch->has_normals) {
999 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1000 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1001 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1003 i += d3d_out_vertex_size;
1005 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1006 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1007 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 1 */
1008 if(patch->has_normals) {
1009 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1010 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1011 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1013 i += d3d_out_vertex_size;
1016 if(patch->has_normals) {
1017 /* Now do the same with reverse light directions */
1018 static const GLfloat x[] = {-1.0f, 0.0f, 0.0f, 0.0f};
1019 static const GLfloat y[] = { 0.0f, -1.0f, 0.0f, 0.0f};
1020 static const GLfloat z[] = { 0.0f, 0.0f, -1.0f, 0.0f};
1021 glLightfv(GL_LIGHT0, GL_POSITION, x);
1022 glLightfv(GL_LIGHT1, GL_POSITION, y);
1023 glLightfv(GL_LIGHT2, GL_POSITION, z);
1024 checkGLcall("Setting up reverse light directions");
1026 glRenderMode(GL_FEEDBACK);
1027 checkGLcall("glRenderMode(GL_FEEDBACK)");
1028 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1029 checkGLcall("glEvalMesh2");
1030 i = glRenderMode(GL_RENDER);
1031 checkGLcall("glRenderMode(GL_RENDER)");
1034 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1035 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1036 ERR("Unexpected token: %f\n", feedbuffer[j]);
1039 if(feedbuffer[j + 1] != 3) {
1040 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1043 if(patch->mem[i + 3] == 0.0f)
1044 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1045 if(patch->mem[i + 4] == 0.0f)
1046 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1047 if(patch->mem[i + 5] == 0.0f)
1048 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1049 normalize_normal(patch->mem + i + 3);
1050 i += d3d_out_vertex_size;
1052 if(patch->mem[i + 3] == 0.0f)
1053 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1054 if(patch->mem[i + 4] == 0.0f)
1055 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1056 if(patch->mem[i + 5] == 0.0f)
1057 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1058 normalize_normal(patch->mem + i + 3);
1059 i += d3d_out_vertex_size;
1061 if(patch->mem[i + 3] == 0.0f)
1062 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1063 if(patch->mem[i + 4] == 0.0f)
1064 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1065 if(patch->mem[i + 5] == 0.0f)
1066 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1067 normalize_normal(patch->mem + i + 3);
1068 i += d3d_out_vertex_size;
1072 glDisable(GL_MAP2_VERTEX_3);
1073 glDisable(GL_AUTO_NORMAL);
1074 glDisable(GL_MAP2_NORMAL);
1075 glDisable(GL_MAP2_TEXTURE_COORD_4);
1076 checkGLcall("glDisable vertex attrib generation");
1079 HeapFree(GetProcessHeap(), 0, feedbuffer);
1081 vtxStride = 3 * sizeof(float);
1082 if(patch->has_normals) {
1083 vtxStride += 3 * sizeof(float);
1085 if(patch->has_texcoords) {
1086 vtxStride += 4 * sizeof(float);
1088 memset(&patch->strided, 0, sizeof(&patch->strided));
1089 patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1090 patch->strided.position.lpData = (BYTE *) patch->mem;
1091 patch->strided.position.dwStride = vtxStride;
1093 if(patch->has_normals) {
1094 patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1095 patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1096 patch->strided.normal.dwStride = vtxStride;
1098 if(patch->has_texcoords) {
1099 patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1100 patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1101 if(patch->has_normals) {
1102 patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1104 patch->strided.texCoords[0].dwStride = vtxStride;