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_context *context,
74 const struct wined3d_stream_info *si, UINT NumVertexes, GLenum glPrimType,
75 const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
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 if (si->use_map & (1 << WINED3D_FFP_POSITION))
118 element = &si->elements[WINED3D_FFP_POSITION];
119 position = element->data + streamOffset[element->stream_idx];
122 if (si->use_map & (1 << WINED3D_FFP_NORMAL))
124 element = &si->elements[WINED3D_FFP_NORMAL];
125 normal = element->data + streamOffset[element->stream_idx];
132 num_untracked_materials = context->num_untracked_materials;
133 if (si->use_map & (1 << WINED3D_FFP_DIFFUSE))
135 element = &si->elements[WINED3D_FFP_DIFFUSE];
136 diffuse = element->data + streamOffset[element->stream_idx];
138 if (num_untracked_materials && element->format_desc->format != WINED3DFMT_A8R8G8B8)
139 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format_desc->format));
143 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
146 if (si->use_map & (1 << WINED3D_FFP_SPECULAR))
148 element = &si->elements[WINED3D_FFP_SPECULAR];
149 specular = element->data + streamOffset[element->stream_idx];
151 /* special case where the fog density is stored in the specular alpha channel */
152 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
153 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
154 || si->elements[WINED3D_FFP_POSITION].format_desc->format == WINED3DFMT_R32G32B32A32_FLOAT)
155 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
157 if (GL_SUPPORT(EXT_FOG_COORD))
159 if (element->format_desc->format == WINED3DFMT_A8R8G8B8) specular_fog = TRUE;
160 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format_desc->format));
168 /* TODO: Use the fog table code from old ddraw */
169 FIXME("Implement fog for transformed vertices in software\n");
175 else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
177 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
180 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
182 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
183 DWORD texture_idx = This->texUnitMap[textureNo];
185 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
187 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
191 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
193 if (texture_idx == WINED3D_UNMAPPED_STAGE) continue;
197 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
200 else if (coordIdx < 0)
202 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
206 if (si->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + coordIdx)))
208 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
209 texCoords[coordIdx] = element->data + streamOffset[element->stream_idx];
210 tex_mask |= (1 << textureNo);
214 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
215 if (GL_SUPPORT(ARB_MULTITEXTURE))
216 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
218 glTexCoord4f(0, 0, 0, 1);
222 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
223 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
226 /* For each primitive */
227 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
228 UINT texture, tmp_tex_mask;
229 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
230 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
233 /* For indexed data, we need to go a few more strides in */
234 if (idxData != NULL) {
236 /* Indexed so work out the number of strides to skip */
238 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
239 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
241 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
242 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
246 tmp_tex_mask = tex_mask;
247 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
253 if (!(tmp_tex_mask & 1)) continue;
255 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
256 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
258 texture_idx = This->texUnitMap[texture];
259 multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format_desc->emit_idx](
260 GL_TEXTURE0_ARB + texture_idx, ptr);
263 /* Diffuse -------------------------------- */
265 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
267 diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format_desc->emit_idx](ptrToCoords);
268 if (num_untracked_materials)
270 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
274 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
275 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
276 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
277 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
279 for (i = 0; i < num_untracked_materials; ++i)
281 glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
286 /* Specular ------------------------------- */
288 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
290 specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format_desc->emit_idx](ptrToCoords);
294 DWORD specularColor = *(const DWORD *)ptrToCoords;
295 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
299 /* Normal -------------------------------- */
300 if (normal != NULL) {
301 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
302 normal_funcs[si->elements[WINED3D_FFP_NORMAL].format_desc->emit_idx](ptrToCoords);
305 /* Position -------------------------------- */
307 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
308 position_funcs[si->elements[WINED3D_FFP_POSITION].format_desc->emit_idx](ptrToCoords);
311 /* For non indexed mode, step onto next parts */
312 if (idxData == NULL) {
318 checkGLcall("glEnd and previous calls");
321 /* GL locking is done by the caller */
322 static inline void send_attribute(IWineD3DDeviceImpl *This, WINED3DFORMAT format, const UINT index, const void *ptr)
326 case WINED3DFMT_R32_FLOAT:
327 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
329 case WINED3DFMT_R32G32_FLOAT:
330 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
332 case WINED3DFMT_R32G32B32_FLOAT:
333 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
335 case WINED3DFMT_R32G32B32A32_FLOAT:
336 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
339 case WINED3DFMT_R8G8B8A8_UINT:
340 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
342 case WINED3DFMT_A8R8G8B8:
343 if (GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA))
345 const DWORD *src = ptr;
346 DWORD c = *src & 0xff00ff00;
347 c |= (*src & 0xff0000) >> 16;
348 c |= (*src & 0xff) << 16;
349 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
352 /* else fallthrough */
353 case WINED3DFMT_R8G8B8A8_UNORM:
354 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
357 case WINED3DFMT_R16G16_SINT:
358 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
360 case WINED3DFMT_R16G16B16A16_SINT:
361 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
364 case WINED3DFMT_R16G16_SNORM:
366 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
367 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
370 case WINED3DFMT_R16G16_UNORM:
372 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
373 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
376 case WINED3DFMT_R16G16B16A16_SNORM:
377 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
379 case WINED3DFMT_R16G16B16A16_UNORM:
380 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
383 case WINED3DFMT_R10G10B10A2_UINT:
384 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
385 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
387 case WINED3DFMT_R10G10B10A2_SNORM:
388 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
389 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
392 case WINED3DFMT_R16G16_FLOAT:
393 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
394 * byte float according to the IEEE standard
396 if (GL_SUPPORT(NV_HALF_FLOAT)) {
397 /* Not supported by GL_ARB_half_float_vertex */
398 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
400 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
401 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
402 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
405 case WINED3DFMT_R16G16B16A16_FLOAT:
406 if (GL_SUPPORT(NV_HALF_FLOAT)) {
407 /* Not supported by GL_ARB_half_float_vertex */
408 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
410 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
411 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
412 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
413 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
414 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
419 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
424 /* GL locking is done by the caller */
425 static void drawStridedSlowVs(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT numberOfVertices,
426 GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
428 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
429 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
430 const WORD *pIdxBufS = NULL;
431 const DWORD *pIdxBufL = NULL;
434 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
438 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
439 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
440 * idxData will be != NULL
442 if(idxData == NULL) {
443 idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
446 if (idxSize == 2) pIdxBufS = idxData;
447 else pIdxBufL = idxData;
448 } else if (idxData) {
449 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
453 /* Start drawing in GL */
454 VTRACE(("glBegin(%x)\n", glPrimitiveType));
455 glBegin(glPrimitiveType);
457 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
458 if (idxData != NULL) {
460 /* Indexed so work out the number of strides to skip */
462 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
463 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
465 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
466 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
470 for (i = MAX_ATTRIBS - 1; i >= 0; i--)
472 if (!(si->use_map & (1 << i))) continue;
474 ptr = si->elements[i].data +
475 si->elements[i].stride * SkipnStrides +
476 stateblock->streamOffset[si->elements[i].stream_idx];
478 send_attribute(This, si->elements[i].format_desc->format, i, ptr);
486 /* GL locking is done by the caller */
487 static inline void drawStridedInstanced(IWineD3DDevice *iface, const struct wined3d_stream_info *si,
488 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex,
491 UINT numInstances = 0, i;
492 int numInstancedAttribs = 0, j;
493 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
494 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
495 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
498 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
499 * We don't support this for now
501 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
502 * But the StreamSourceFreq value has a different meaning in that situation.
504 FIXME("Non-indexed instanced drawing is not supported\n");
508 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
510 /* First, figure out how many instances we have to draw */
511 for(i = 0; i < MAX_STREAMS; i++) {
512 /* Look at the streams and take the first one which matches */
513 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
514 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
515 if(stateblock->streamFreq[i] == 0){
518 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
520 break; /* break, because only the first suitable value is interesting */
524 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
526 if (!(si->use_map & (1 << i))) continue;
528 if (stateblock->streamFlags[si->elements[i].stream_idx] & WINED3DSTREAMSOURCE_INSTANCEDATA)
530 instancedData[numInstancedAttribs] = i;
531 numInstancedAttribs++;
535 /* now draw numInstances instances :-) */
536 for(i = 0; i < numInstances; i++) {
537 /* Specify the instanced attributes using immediate mode calls */
538 for(j = 0; j < numInstancedAttribs; j++) {
539 const BYTE *ptr = si->elements[instancedData[j]].data +
540 si->elements[instancedData[j]].stride * i +
541 stateblock->streamOffset[si->elements[instancedData[j]].stream_idx];
542 if (si->elements[instancedData[j]].buffer_object)
544 struct wined3d_buffer *vb =
545 (struct wined3d_buffer *)stateblock->streamSource[si->elements[instancedData[j]].stream_idx];
546 ptr += (long) buffer_get_sysmem(vb);
549 send_attribute(This, si->elements[instancedData[j]].format_desc->format, instancedData[j], ptr);
552 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
553 (const char *)idxData+(idxSize * startIdx));
554 checkGLcall("glDrawElements");
558 static inline void remove_vbos(IWineD3DDeviceImpl *This, struct wined3d_stream_info *s)
562 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
564 struct wined3d_stream_info_element *e;
566 if (!(s->use_map & (1 << i))) continue;
569 if (e->buffer_object)
571 struct wined3d_buffer *vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
572 e->buffer_object = 0;
573 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
578 /* Routine common to the draw primitive and draw indexed primitive routines */
579 void drawPrimitive(IWineD3DDevice *iface, UINT index_count, UINT numberOfVertices,
580 UINT StartIdx, UINT idxSize, const void *idxData, UINT minIndex)
583 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
584 IWineD3DSurfaceImpl *target;
585 struct wined3d_context *context;
588 if (!index_count) return;
590 if (This->stateBlock->renderState[WINED3DRS_COLORWRITEENABLE])
592 /* Invalidate the back buffer memory so LockRect will read it the next time */
593 for (i = 0; i < GL_LIMITS(buffers); ++i)
595 target = (IWineD3DSurfaceImpl *)This->render_targets[i];
598 IWineD3DSurface_LoadLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, NULL);
599 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, TRUE);
604 /* Signals other modules that a drawing is in progress and the stateblock finalized */
605 This->isInDraw = TRUE;
607 context = ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
609 if (This->stencilBufferTarget) {
610 /* Note that this depends on the ActivateContext call above to set
611 * This->render_offscreen properly. We don't currently take the
612 * Z-compare function into account, but we could skip loading the
613 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
614 * that we never copy the stencil data.*/
615 DWORD location = context->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
616 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE]
617 || This->stateBlock->renderState[WINED3DRS_ZENABLE])
618 surface_load_ds_location(This->stencilBufferTarget, context, location);
619 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE])
620 surface_modify_ds_location(This->stencilBufferTarget, location);
623 /* Ok, we will be updating the screen from here onwards so grab the lock */
626 GLenum glPrimType = This->stateBlock->gl_primitive_type;
627 BOOL emulation = FALSE;
628 const struct wined3d_stream_info *stream_info = &This->strided_streams;
629 struct wined3d_stream_info stridedlcl;
631 if (!numberOfVertices) numberOfVertices = index_count;
633 if (!use_vs(This->stateBlock))
635 if (!This->strided_streams.position_transformed && context->num_untracked_materials
636 && This->stateBlock->renderState[WINED3DRS_LIGHTING])
640 FIXME("Using software emulation because not all material properties could be tracked\n");
643 TRACE("Using software emulation because not all material properties could be tracked\n");
647 else if (context->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE])
649 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
650 * to a float in the vertex buffer
654 FIXME("Using software emulation because manual fog coordinates are provided\n");
657 TRACE("Using software emulation because manual fog coordinates are provided\n");
663 stream_info = &stridedlcl;
664 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
665 remove_vbos(This, &stridedlcl);
669 if (This->useDrawStridedSlow || emulation) {
670 /* Immediate mode drawing */
671 if (use_vs(This->stateBlock))
675 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
678 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
680 drawStridedSlowVs(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
682 drawStridedSlow(iface, context, stream_info, index_count,
683 glPrimType, idxData, idxSize, minIndex, StartIdx);
685 } else if(This->instancedDraw) {
686 /* Instancing emulation with mixing immediate mode and arrays */
687 drawStridedInstanced(iface, &This->strided_streams, index_count,
688 glPrimType, idxData, idxSize, minIndex, StartIdx);
690 drawStridedFast(iface, glPrimType, minIndex, minIndex + numberOfVertices - 1,
691 index_count, idxSize, idxData, StartIdx);
695 /* Finished updating the screen, restore lock */
697 TRACE("Done all gl drawing\n");
700 #ifdef SHOW_FRAME_MAKEUP
702 static long int primCounter = 0;
703 /* NOTE: set primCounter to the value reported by drawprim
704 before you want to to write frame makeup to /tmp */
705 if (primCounter >= 0) {
706 WINED3DLOCKED_RECT r;
708 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
709 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
710 TRACE("Saving screenshot %s\n", buffer);
711 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
712 IWineD3DSurface_UnlockRect(This->render_targets[0]);
714 #ifdef SHOW_TEXTURE_MAKEUP
716 IWineD3DSurface *pSur;
718 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
719 if (This->stateBlock->textures[textureNo] != NULL) {
720 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
721 TRACE("Saving texture %s\n", buffer);
722 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
723 IWineD3DTexture_GetSurfaceLevel(This->stateBlock->textures[textureNo], 0, &pSur);
724 IWineD3DSurface_SaveSnapshot(pSur, buffer);
725 IWineD3DSurface_Release(pSur);
727 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
734 TRACE("drawprim #%ld\n", primCounter);
739 /* Control goes back to the device, stateblock values may change again */
740 This->isInDraw = FALSE;
743 static void normalize_normal(float *n) {
744 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
745 if (length == 0.0f) return;
746 length = sqrt(length);
747 n[0] = n[0] / length;
748 n[1] = n[1] / length;
749 n[2] = n[2] / length;
752 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
754 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
755 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
756 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
757 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
760 * To read back, the opengl feedback mode is used. This creates a problem because we want
761 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
762 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
763 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
764 * them to [-1.0;+1.0] and set the viewport up to scale them back.
766 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
767 * resulting colors back to the normals.
769 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
770 * does not restore it because normally a draw follows immediately afterwards. The caller is
771 * responsible of taking care that either the gl states are restored, or the context activated
772 * for drawing to reset the lastWasBlit flag.
774 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
775 struct WineD3DRectPatch *patch) {
776 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
777 float max_x = 0.0f, max_y = 0.0f, max_z = 0.0f, neg_z = 0.0f;
778 struct wined3d_stream_info stream_info;
779 struct wined3d_stream_info_element *e;
781 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
783 GLenum feedback_type;
786 /* Simply activate the context for blitting. This disables all the things we don't want and
787 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
788 * patch (as opposed to normal draws) will most likely need different changes anyway. */
789 ActivateContext(This, NULL, CTXUSAGE_BLIT);
791 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
794 device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
796 e = &stream_info.elements[WINED3D_FFP_POSITION];
797 if (e->buffer_object)
799 struct wined3d_buffer *vb;
800 vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
801 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
803 vtxStride = e->stride;
805 vtxStride * info->Stride * info->StartVertexOffsetHeight +
806 vtxStride * info->StartVertexOffsetWidth;
808 /* Not entirely sure about what happens with transformed vertices */
809 if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
811 if(vtxStride % sizeof(GLfloat)) {
812 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
813 * I don't see how the stride could not be a multiple of 4, but make sure
816 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
818 if(info->Basis != WINED3DBASIS_BEZIER) {
819 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
821 if(info->Degree != WINED3DDEGREE_CUBIC) {
822 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
825 /* First, get the boundary cube of the input data */
826 for(j = 0; j < info->Height; j++) {
827 for(i = 0; i < info->Width; i++) {
828 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
829 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
830 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
831 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
832 if(v[2] < neg_z) neg_z = v[2];
836 /* This needs some improvements in the vertex decl code */
837 FIXME("Cannot find data to generate. Only generating position and normals\n");
838 patch->has_normals = TRUE;
839 patch->has_texcoords = FALSE;
843 glMatrixMode(GL_PROJECTION);
844 checkGLcall("glMatrixMode(GL_PROJECTION)");
846 checkGLcall("glLoadIndentity()");
847 glScalef(1.0f / (max_x), 1.0f / (max_y), max_z == 0.0f ? 1.0f : 1.0f / (2.0f * max_z));
848 glTranslatef(0.0f, 0.0f, 0.5f);
849 checkGLcall("glScalef");
850 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
851 checkGLcall("glViewport");
853 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
854 * our feedback buffer parser
856 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
857 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
858 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
859 if(patch->has_normals) {
860 static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
861 static const GLfloat red[] = {1.0f, 0.0f, 0.0f, 0.0f};
862 static const GLfloat green[] = {0.0f, 1.0f, 0.0f, 0.0f};
863 static const GLfloat blue[] = {0.0f, 0.0f, 1.0f, 0.0f};
864 static const GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
865 glEnable(GL_LIGHTING);
866 checkGLcall("glEnable(GL_LIGHTING)");
867 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
868 checkGLcall("glLightModel for MODEL_AMBIENT");
869 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
871 for(i = 3; i < GL_LIMITS(lights); i++) {
872 glDisable(GL_LIGHT0 + i);
873 checkGLcall("glDisable(GL_LIGHT0 + i)");
874 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
877 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
878 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
879 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
880 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
881 glLightfv(GL_LIGHT0, GL_POSITION, red);
883 checkGLcall("Setting up light 1");
884 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
885 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
886 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
887 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
888 glLightfv(GL_LIGHT1, GL_POSITION, green);
890 checkGLcall("Setting up light 2");
891 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
892 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
893 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
894 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
895 glLightfv(GL_LIGHT2, GL_POSITION, blue);
897 checkGLcall("Setting up light 3");
899 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
900 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
901 glDisable(GL_COLOR_MATERIAL);
902 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
903 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
904 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
905 checkGLcall("Setting up materials");
908 /* Enable the needed maps.
909 * GL_MAP2_VERTEX_3 is needed for positional data.
910 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
911 * GL_MAP2_TEXTURE_COORD_4 for texture coords
913 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
914 out_vertex_size = 3 /* position */;
915 d3d_out_vertex_size = 3;
916 glEnable(GL_MAP2_VERTEX_3);
917 if(patch->has_normals && patch->has_texcoords) {
918 FIXME("Texcoords not handled yet\n");
919 feedback_type = GL_3D_COLOR_TEXTURE;
920 out_vertex_size += 8;
921 d3d_out_vertex_size += 7;
922 glEnable(GL_AUTO_NORMAL);
923 glEnable(GL_MAP2_TEXTURE_COORD_4);
924 } else if(patch->has_texcoords) {
925 FIXME("Texcoords not handled yet\n");
926 feedback_type = GL_3D_COLOR_TEXTURE;
927 out_vertex_size += 7;
928 d3d_out_vertex_size += 4;
929 glEnable(GL_MAP2_TEXTURE_COORD_4);
930 } else if(patch->has_normals) {
931 feedback_type = GL_3D_COLOR;
932 out_vertex_size += 4;
933 d3d_out_vertex_size += 3;
934 glEnable(GL_AUTO_NORMAL);
936 feedback_type = GL_3D;
938 checkGLcall("glEnable vertex attrib generation");
940 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
941 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
942 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
944 glMap2f(GL_MAP2_VERTEX_3,
945 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
946 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
947 (const GLfloat *)data);
948 checkGLcall("glMap2f");
949 if(patch->has_texcoords) {
950 glMap2f(GL_MAP2_TEXTURE_COORD_4,
951 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
952 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
953 (const GLfloat *)data);
954 checkGLcall("glMap2f");
956 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0f, 1.0f, ceilf(patch->numSegs[1]), 0.0f, 1.0f);
957 checkGLcall("glMapGrid2f");
959 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
960 checkGLcall("glFeedbackBuffer");
961 glRenderMode(GL_FEEDBACK);
963 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
964 checkGLcall("glEvalMesh2");
966 i = glRenderMode(GL_RENDER);
969 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
970 HeapFree(GetProcessHeap(), 0, feedbuffer);
971 return WINED3DERR_DRIVERINTERNALERROR;
972 } else if(i != buffer_size) {
974 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
975 HeapFree(GetProcessHeap(), 0, feedbuffer);
976 return WINED3DERR_DRIVERINTERNALERROR;
978 TRACE("Got %d elements as expected\n", i);
981 HeapFree(GetProcessHeap(), 0, patch->mem);
982 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
984 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
985 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
986 ERR("Unexpected token: %f\n", feedbuffer[j]);
989 if(feedbuffer[j + 1] != 3) {
990 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
993 /* Somehow there are different ideas about back / front facing, so fix up the
996 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
997 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
998 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 3 */
999 if(patch->has_normals) {
1000 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1001 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1002 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1004 i += d3d_out_vertex_size;
1006 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1007 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1008 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 2 */
1009 if(patch->has_normals) {
1010 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1011 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1012 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1014 i += d3d_out_vertex_size;
1016 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1017 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1018 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 1 */
1019 if(patch->has_normals) {
1020 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1021 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1022 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1024 i += d3d_out_vertex_size;
1027 if(patch->has_normals) {
1028 /* Now do the same with reverse light directions */
1029 static const GLfloat x[] = {-1.0f, 0.0f, 0.0f, 0.0f};
1030 static const GLfloat y[] = { 0.0f, -1.0f, 0.0f, 0.0f};
1031 static const GLfloat z[] = { 0.0f, 0.0f, -1.0f, 0.0f};
1032 glLightfv(GL_LIGHT0, GL_POSITION, x);
1033 glLightfv(GL_LIGHT1, GL_POSITION, y);
1034 glLightfv(GL_LIGHT2, GL_POSITION, z);
1035 checkGLcall("Setting up reverse light directions");
1037 glRenderMode(GL_FEEDBACK);
1038 checkGLcall("glRenderMode(GL_FEEDBACK)");
1039 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1040 checkGLcall("glEvalMesh2");
1041 i = glRenderMode(GL_RENDER);
1042 checkGLcall("glRenderMode(GL_RENDER)");
1045 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1046 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1047 ERR("Unexpected token: %f\n", feedbuffer[j]);
1050 if(feedbuffer[j + 1] != 3) {
1051 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1054 if(patch->mem[i + 3] == 0.0f)
1055 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1056 if(patch->mem[i + 4] == 0.0f)
1057 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1058 if(patch->mem[i + 5] == 0.0f)
1059 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1060 normalize_normal(patch->mem + i + 3);
1061 i += d3d_out_vertex_size;
1063 if(patch->mem[i + 3] == 0.0f)
1064 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1065 if(patch->mem[i + 4] == 0.0f)
1066 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1067 if(patch->mem[i + 5] == 0.0f)
1068 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1069 normalize_normal(patch->mem + i + 3);
1070 i += d3d_out_vertex_size;
1072 if(patch->mem[i + 3] == 0.0f)
1073 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1074 if(patch->mem[i + 4] == 0.0f)
1075 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1076 if(patch->mem[i + 5] == 0.0f)
1077 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1078 normalize_normal(patch->mem + i + 3);
1079 i += d3d_out_vertex_size;
1083 glDisable(GL_MAP2_VERTEX_3);
1084 glDisable(GL_AUTO_NORMAL);
1085 glDisable(GL_MAP2_NORMAL);
1086 glDisable(GL_MAP2_TEXTURE_COORD_4);
1087 checkGLcall("glDisable vertex attrib generation");
1090 HeapFree(GetProcessHeap(), 0, feedbuffer);
1092 vtxStride = 3 * sizeof(float);
1093 if(patch->has_normals) {
1094 vtxStride += 3 * sizeof(float);
1096 if(patch->has_texcoords) {
1097 vtxStride += 4 * sizeof(float);
1099 memset(&patch->strided, 0, sizeof(&patch->strided));
1100 patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1101 patch->strided.position.lpData = (BYTE *) patch->mem;
1102 patch->strided.position.dwStride = vtxStride;
1104 if(patch->has_normals) {
1105 patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1106 patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1107 patch->strided.normal.dwStride = vtxStride;
1109 if(patch->has_texcoords) {
1110 patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1111 patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1112 if(patch->has_normals) {
1113 patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1115 patch->strided.texCoords[0].dwStride = vtxStride;