d3dcompiler: Use an iface instead of a vtbl pointer in d3dcompiler_blob.
[wine/multimedia.git] / dlls / wined3d / drawprim.c
bloba3b0053ceea570a3ca7e7c99be9484d270aa49ea
1 /*
2 * WINED3D draw functions
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006, 2008 Henri Verbeet
9 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
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
27 #include "config.h"
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
32 #include <stdio.h>
33 #include <math.h>
35 /* GL locking is done by the caller */
36 static void drawStridedFast(GLenum primitive_type, UINT count, UINT idx_size, const void *idx_data, UINT start_idx)
38 if (idx_size)
40 glDrawElements(primitive_type, count,
41 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
42 (const char *)idx_data + (idx_size * start_idx));
43 checkGLcall("glDrawElements");
45 else
47 glDrawArrays(primitive_type, start_idx, count);
48 checkGLcall("glDrawArrays");
53 * Actually draw using the supplied information.
54 * Slower GL version which extracts info about each vertex in turn
57 /* GL locking is done by the caller */
58 static void drawStridedSlow(IWineD3DDeviceImpl *device, const struct wined3d_context *context,
59 const struct wined3d_stream_info *si, UINT NumVertexes, GLenum glPrimType,
60 const void *idxData, UINT idxSize, UINT startIdx)
62 unsigned int textureNo = 0;
63 const WORD *pIdxBufS = NULL;
64 const DWORD *pIdxBufL = NULL;
65 UINT vx_index;
66 const struct wined3d_state *state = &device->stateBlock->state;
67 const struct wined3d_stream_state *streams = state->streams;
68 LONG SkipnStrides = startIdx + state->load_base_vertex_index;
69 BOOL pixelShader = use_ps(state);
70 BOOL specular_fog = FALSE;
71 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
72 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
73 const struct wined3d_gl_info *gl_info = context->gl_info;
74 UINT texture_stages = gl_info->limits.texture_stages;
75 const struct wined3d_stream_info_element *element;
76 UINT num_untracked_materials;
77 DWORD tex_mask = 0;
79 TRACE("Using slow vertex array code\n");
81 /* Variable Initialization */
82 if (idxSize)
84 /* Immediate mode drawing can't make use of indices in a vbo - get the
85 * data from the index buffer. If the index buffer has no vbo (not
86 * supported or other reason), or with user pointer drawing idxData
87 * will be non-NULL. */
88 if (!idxData)
89 idxData = buffer_get_sysmem(state->index_buffer, gl_info);
91 if (idxSize == 2) pIdxBufS = idxData;
92 else pIdxBufL = idxData;
93 } else if (idxData) {
94 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
95 return;
98 /* Start drawing in GL */
99 glBegin(glPrimType);
101 if (si->use_map & (1 << WINED3D_FFP_POSITION))
103 element = &si->elements[WINED3D_FFP_POSITION];
104 position = element->data + streams[element->stream_idx].offset;
107 if (si->use_map & (1 << WINED3D_FFP_NORMAL))
109 element = &si->elements[WINED3D_FFP_NORMAL];
110 normal = element->data + streams[element->stream_idx].offset;
112 else
114 glNormal3f(0, 0, 0);
117 num_untracked_materials = context->num_untracked_materials;
118 if (si->use_map & (1 << WINED3D_FFP_DIFFUSE))
120 element = &si->elements[WINED3D_FFP_DIFFUSE];
121 diffuse = element->data + streams[element->stream_idx].offset;
123 if (num_untracked_materials && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
124 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format->id));
126 else
128 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
131 if (si->use_map & (1 << WINED3D_FFP_SPECULAR))
133 element = &si->elements[WINED3D_FFP_SPECULAR];
134 specular = element->data + streams[element->stream_idx].offset;
136 /* special case where the fog density is stored in the specular alpha channel */
137 if (state->render_states[WINED3DRS_FOGENABLE]
138 && (state->render_states[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
139 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
140 && state->render_states[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
142 if (gl_info->supported[EXT_FOG_COORD])
144 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM) specular_fog = TRUE;
145 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format->id));
147 else
149 static BOOL warned;
151 if (!warned)
153 /* TODO: Use the fog table code from old ddraw */
154 FIXME("Implement fog for transformed vertices in software\n");
155 warned = TRUE;
160 else if (gl_info->supported[EXT_SECONDARY_COLOR])
162 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
165 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
167 int coordIdx = state->texture_states[textureNo][WINED3DTSS_TEXCOORDINDEX];
168 DWORD texture_idx = device->texUnitMap[textureNo];
170 if (!gl_info->supported[ARB_MULTITEXTURE] && textureNo > 0)
172 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
173 continue;
176 if (!pixelShader && !state->textures[textureNo]) continue;
178 if (texture_idx == WINED3D_UNMAPPED_STAGE) continue;
180 if (coordIdx > 7)
182 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
183 continue;
185 else if (coordIdx < 0)
187 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
188 continue;
191 if (si->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + coordIdx)))
193 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
194 texCoords[coordIdx] = element->data + streams[element->stream_idx].offset;
195 tex_mask |= (1 << textureNo);
197 else
199 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
200 if (gl_info->supported[ARB_MULTITEXTURE])
201 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
202 else
203 glTexCoord4f(0, 0, 0, 1);
207 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
208 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
211 /* For each primitive */
212 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
213 UINT texture, tmp_tex_mask;
214 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
215 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
218 /* For indexed data, we need to go a few more strides in */
219 if (idxData)
221 /* Indexed so work out the number of strides to skip */
222 if (idxSize == 2)
223 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->load_base_vertex_index;
224 else
225 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->load_base_vertex_index;
228 tmp_tex_mask = tex_mask;
229 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
231 int coord_idx;
232 const void *ptr;
233 DWORD texture_idx;
235 if (!(tmp_tex_mask & 1)) continue;
237 coord_idx = state->texture_states[texture][WINED3DTSS_TEXCOORDINDEX];
238 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
240 texture_idx = device->texUnitMap[texture];
241 multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
242 GL_TEXTURE0_ARB + texture_idx, ptr);
245 /* Diffuse -------------------------------- */
246 if (diffuse) {
247 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
249 diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptrToCoords);
250 if (num_untracked_materials)
252 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
253 unsigned char i;
254 float color[4];
256 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
257 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
258 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
259 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
261 for (i = 0; i < num_untracked_materials; ++i)
263 glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
268 /* Specular ------------------------------- */
269 if (specular) {
270 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
272 specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptrToCoords);
274 if (specular_fog)
276 DWORD specularColor = *(const DWORD *)ptrToCoords;
277 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
281 /* Normal -------------------------------- */
282 if (normal)
284 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
285 normal_funcs[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptrToCoords);
288 /* Position -------------------------------- */
289 if (position) {
290 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
291 position_funcs[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptrToCoords);
294 /* For non indexed mode, step onto next parts */
295 if (!idxData) ++SkipnStrides;
298 glEnd();
299 checkGLcall("glEnd and previous calls");
302 /* GL locking is done by the caller */
303 static inline void send_attribute(const struct wined3d_gl_info *gl_info,
304 enum wined3d_format_id format, const UINT index, const void *ptr)
306 switch(format)
308 case WINED3DFMT_R32_FLOAT:
309 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
310 break;
311 case WINED3DFMT_R32G32_FLOAT:
312 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
313 break;
314 case WINED3DFMT_R32G32B32_FLOAT:
315 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
316 break;
317 case WINED3DFMT_R32G32B32A32_FLOAT:
318 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
319 break;
321 case WINED3DFMT_R8G8B8A8_UINT:
322 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
323 break;
324 case WINED3DFMT_B8G8R8A8_UNORM:
325 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
327 const DWORD *src = ptr;
328 DWORD c = *src & 0xff00ff00;
329 c |= (*src & 0xff0000) >> 16;
330 c |= (*src & 0xff) << 16;
331 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
332 break;
334 /* else fallthrough */
335 case WINED3DFMT_R8G8B8A8_UNORM:
336 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
337 break;
339 case WINED3DFMT_R16G16_SINT:
340 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
341 break;
342 case WINED3DFMT_R16G16B16A16_SINT:
343 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
344 break;
346 case WINED3DFMT_R16G16_SNORM:
348 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
349 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
350 break;
352 case WINED3DFMT_R16G16_UNORM:
354 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
355 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
356 break;
358 case WINED3DFMT_R16G16B16A16_SNORM:
359 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
360 break;
361 case WINED3DFMT_R16G16B16A16_UNORM:
362 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
363 break;
365 case WINED3DFMT_R10G10B10A2_UINT:
366 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
367 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
368 break;
369 case WINED3DFMT_R10G10B10A2_SNORM:
370 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
371 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
372 break;
374 case WINED3DFMT_R16G16_FLOAT:
375 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
376 * byte float according to the IEEE standard
378 if (gl_info->supported[NV_HALF_FLOAT])
380 /* Not supported by GL_ARB_half_float_vertex */
381 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
383 else
385 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
386 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
387 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
389 break;
390 case WINED3DFMT_R16G16B16A16_FLOAT:
391 if (gl_info->supported[NV_HALF_FLOAT])
393 /* Not supported by GL_ARB_half_float_vertex */
394 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
396 else
398 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
399 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
400 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
401 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
402 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
404 break;
406 default:
407 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
408 break;
412 /* GL locking is done by the caller */
413 static void drawStridedSlowVs(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
414 const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
415 const void *idxData, UINT idxSize, UINT startIdx)
417 LONG SkipnStrides = startIdx + state->load_base_vertex_index;
418 const DWORD *pIdxBufL = NULL;
419 const WORD *pIdxBufS = NULL;
420 UINT vx_index;
421 int i;
422 const BYTE *ptr;
424 if (idxSize)
426 /* Immediate mode drawing can't make use of indices in a vbo - get the
427 * data from the index buffer. If the index buffer has no vbo (not
428 * supported or other reason), or with user pointer drawing idxData
429 * will be non-NULL. */
430 if (!idxData)
431 idxData = buffer_get_sysmem(state->index_buffer, gl_info);
433 if (idxSize == 2) pIdxBufS = idxData;
434 else pIdxBufL = idxData;
435 } else if (idxData) {
436 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
437 return;
440 /* Start drawing in GL */
441 glBegin(glPrimitiveType);
443 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index)
445 if (idxData)
447 /* Indexed so work out the number of strides to skip */
448 if (idxSize == 2)
449 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->load_base_vertex_index;
450 else
451 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->load_base_vertex_index;
454 for (i = MAX_ATTRIBS - 1; i >= 0; i--)
456 if (!(si->use_map & (1 << i))) continue;
458 ptr = si->elements[i].data + si->elements[i].stride * SkipnStrides
459 + state->streams[si->elements[i].stream_idx].offset;
461 send_attribute(gl_info, si->elements[i].format->id, i, ptr);
463 SkipnStrides++;
466 glEnd();
469 /* GL locking is done by the caller */
470 static void drawStridedInstanced(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
471 const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
472 const void *idxData, UINT idxSize, UINT startIdx)
474 UINT numInstances = 0, i;
475 int numInstancedAttribs = 0, j;
476 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
478 if (!idxSize)
480 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
481 * We don't support this for now
483 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
484 * But the StreamSourceFreq value has a different meaning in that situation.
486 FIXME("Non-indexed instanced drawing is not supported\n");
487 return;
490 /* First, figure out how many instances we have to draw */
491 for (i = 0; i < MAX_STREAMS; ++i)
493 /* Look at the streams and take the first one which matches */
494 if (state->streams[i].buffer
495 && ((state->streams[i].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
496 || (state->streams[i].flags & WINED3DSTREAMSOURCE_INDEXEDDATA)))
498 /* Use the specified number of instances from the first matched
499 * stream. A streamFreq of 0 (with INSTANCEDATA or INDEXEDDATA)
500 * is handled as 1. See d3d9/tests/visual.c-> stream_test(). */
501 numInstances = state->streams[i].frequency ? state->streams[i].frequency : 1;
502 break;
506 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
508 if (!(si->use_map & (1 << i))) continue;
510 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
512 instancedData[numInstancedAttribs] = i;
513 numInstancedAttribs++;
517 /* now draw numInstances instances :-) */
518 for(i = 0; i < numInstances; i++) {
519 /* Specify the instanced attributes using immediate mode calls */
520 for(j = 0; j < numInstancedAttribs; j++) {
521 const BYTE *ptr = si->elements[instancedData[j]].data
522 + si->elements[instancedData[j]].stride * i
523 + state->streams[si->elements[instancedData[j]].stream_idx].offset;
524 if (si->elements[instancedData[j]].buffer_object)
526 struct wined3d_buffer *vb = state->streams[si->elements[instancedData[j]].stream_idx].buffer;
527 ptr += (ULONG_PTR)buffer_get_sysmem(vb, gl_info);
530 send_attribute(gl_info, si->elements[instancedData[j]].format->id, 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 void remove_vbos(const struct wined3d_gl_info *gl_info,
540 const struct wined3d_state *state, struct wined3d_stream_info *s)
542 unsigned int i;
544 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
546 struct wined3d_stream_info_element *e;
548 if (!(s->use_map & (1 << i))) continue;
550 e = &s->elements[i];
551 if (e->buffer_object)
553 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
554 e->buffer_object = 0;
555 e->data = (BYTE *)((ULONG_PTR)e->data + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
560 /* Routine common to the draw primitive and draw indexed primitive routines */
561 void drawPrimitive(IWineD3DDeviceImpl *device, UINT index_count, UINT StartIdx, UINT idxSize, const void *idxData)
563 const struct wined3d_state *state = &device->stateBlock->state;
564 struct wined3d_context *context;
565 unsigned int i;
567 if (!index_count) return;
569 if (state->render_states[WINED3DRS_COLORWRITEENABLE])
571 /* Invalidate the back buffer memory so LockRect will read it the next time */
572 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
574 IWineD3DSurfaceImpl *target = device->render_targets[i];
575 if (target)
577 surface_load_location(target, SFLAG_INDRAWABLE, NULL);
578 surface_modify_location(target, SFLAG_INDRAWABLE, TRUE);
583 /* Signals other modules that a drawing is in progress and the stateblock finalized */
584 device->isInDraw = TRUE;
586 context = context_acquire(device, device->render_targets[0]);
587 if (!context->valid)
589 context_release(context);
590 WARN("Invalid context, skipping draw.\n");
591 return;
594 context_apply_draw_state(context, device);
596 if (device->depth_stencil)
598 /* Note that this depends on the context_acquire() call above to set
599 * context->render_offscreen properly. We don't currently take the
600 * Z-compare function into account, but we could skip loading the
601 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
602 * that we never copy the stencil data.*/
603 DWORD location = context->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
604 if (state->render_states[WINED3DRS_ZWRITEENABLE] || state->render_states[WINED3DRS_ZENABLE])
606 IWineD3DSurfaceImpl *ds = device->depth_stencil;
607 RECT current_rect, draw_rect, r;
609 if (location == SFLAG_DS_ONSCREEN && ds != device->onscreen_depth_stencil)
610 device_switch_onscreen_ds(device, context, ds);
612 if (ds->flags & location)
613 SetRect(&current_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
614 else
615 SetRectEmpty(&current_rect);
617 device_get_draw_rect(device, &draw_rect);
619 IntersectRect(&r, &draw_rect, &current_rect);
620 if (!EqualRect(&r, &draw_rect))
621 surface_load_ds_location(ds, context, location);
623 if (state->render_states[WINED3DRS_ZWRITEENABLE])
625 surface_modify_ds_location(ds, location, ds->ds_current_size.cx, ds->ds_current_size.cy);
626 surface_modify_location(ds, SFLAG_INDRAWABLE, TRUE);
631 if ((!context->gl_info->supported[WINED3D_GL_VERSION_2_0]
632 || (!glPointParameteri && !context->gl_info->supported[NV_POINT_SPRITE]))
633 && context->render_offscreen
634 && state->render_states[WINED3DRS_POINTSPRITEENABLE]
635 && state->gl_primitive_type == GL_POINTS)
637 FIXME("Point sprite coordinate origin switching not supported.\n");
640 /* Ok, we will be updating the screen from here onwards so grab the lock */
641 ENTER_GL();
643 GLenum glPrimType = state->gl_primitive_type;
644 BOOL emulation = FALSE;
645 const struct wined3d_stream_info *stream_info = &device->strided_streams;
646 struct wined3d_stream_info stridedlcl;
648 if (!use_vs(state))
650 if (!stream_info->position_transformed && context->num_untracked_materials
651 && state->render_states[WINED3DRS_LIGHTING])
653 static BOOL warned;
654 if (!warned) {
655 FIXME("Using software emulation because not all material properties could be tracked\n");
656 warned = TRUE;
657 } else {
658 TRACE("Using software emulation because not all material properties could be tracked\n");
660 emulation = TRUE;
662 else if (context->fog_coord && state->render_states[WINED3DRS_FOGENABLE])
664 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
665 * to a float in the vertex buffer
667 static BOOL warned;
668 if (!warned) {
669 FIXME("Using software emulation because manual fog coordinates are provided\n");
670 warned = TRUE;
671 } else {
672 TRACE("Using software emulation because manual fog coordinates are provided\n");
674 emulation = TRUE;
677 if(emulation) {
678 stream_info = &stridedlcl;
679 memcpy(&stridedlcl, &device->strided_streams, sizeof(stridedlcl));
680 remove_vbos(context->gl_info, state, &stridedlcl);
684 if (device->useDrawStridedSlow || emulation)
686 /* Immediate mode drawing */
687 if (use_vs(state))
689 static BOOL warned;
690 if (!warned) {
691 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
692 warned = TRUE;
693 } else {
694 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
696 drawStridedSlowVs(context->gl_info, state, stream_info,
697 index_count, glPrimType, idxData, idxSize, StartIdx);
699 else
701 drawStridedSlow(device, context, stream_info, index_count,
702 glPrimType, idxData, idxSize, StartIdx);
705 else if (device->instancedDraw)
707 /* Instancing emulation with mixing immediate mode and arrays */
708 drawStridedInstanced(context->gl_info, state, stream_info,
709 index_count, glPrimType, idxData, idxSize, StartIdx);
711 else
713 drawStridedFast(glPrimType, index_count, idxSize, idxData, StartIdx);
717 /* Finished updating the screen, restore lock */
718 LEAVE_GL();
720 for(i = 0; i < device->num_buffer_queries; ++i)
722 wined3d_event_query_issue(device->buffer_queries[i], device);
725 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
727 context_release(context);
729 TRACE("Done all gl drawing\n");
731 /* Control goes back to the device, stateblock values may change again */
732 device->isInDraw = FALSE;
735 static void normalize_normal(float *n) {
736 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
737 if (length == 0.0f) return;
738 length = sqrtf(length);
739 n[0] = n[0] / length;
740 n[1] = n[1] / length;
741 n[2] = n[2] / length;
744 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
746 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
747 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
748 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
749 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
750 * in drawprim.
752 * To read back, the opengl feedback mode is used. This creates a problem because we want
753 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
754 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
755 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
756 * them to [-1.0;+1.0] and set the viewport up to scale them back.
758 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
759 * resulting colors back to the normals.
761 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
762 * does not restore it because normally a draw follows immediately afterwards. The caller is
763 * responsible of taking care that either the gl states are restored, or the context activated
764 * for drawing to reset the lastWasBlit flag.
766 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
767 struct WineD3DRectPatch *patch) {
768 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
769 float max_x = 0.0f, max_y = 0.0f, max_z = 0.0f, neg_z = 0.0f;
770 struct wined3d_stream_info stream_info;
771 struct wined3d_stream_info_element *e;
772 struct wined3d_context *context;
773 const BYTE *data;
774 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
775 DWORD vtxStride;
776 GLenum feedback_type;
777 GLfloat *feedbuffer;
779 /* Simply activate the context for blitting. This disables all the things we don't want and
780 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
781 * patch (as opposed to normal draws) will most likely need different changes anyway. */
782 context = context_acquire(This, NULL);
783 context_apply_blit_state(context, This);
785 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
786 * Beware of vbos
788 device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
790 e = &stream_info.elements[WINED3D_FFP_POSITION];
791 if (e->buffer_object)
793 struct wined3d_buffer *vb = This->stateBlock->state.streams[e->stream_idx].buffer;
794 e->data = (BYTE *)((ULONG_PTR)e->data + (ULONG_PTR)buffer_get_sysmem(vb, context->gl_info));
796 vtxStride = e->stride;
797 data = e->data +
798 vtxStride * info->Stride * info->StartVertexOffsetHeight +
799 vtxStride * info->StartVertexOffsetWidth;
801 /* Not entirely sure about what happens with transformed vertices */
802 if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
804 if(vtxStride % sizeof(GLfloat)) {
805 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
806 * I don't see how the stride could not be a multiple of 4, but make sure
807 * to check it
809 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
811 if(info->Basis != WINED3DBASIS_BEZIER) {
812 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
814 if(info->Degree != WINED3DDEGREE_CUBIC) {
815 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
818 /* First, get the boundary cube of the input data */
819 for(j = 0; j < info->Height; j++) {
820 for(i = 0; i < info->Width; i++) {
821 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
822 if(fabs(v[0]) > max_x) max_x = fabsf(v[0]);
823 if(fabs(v[1]) > max_y) max_y = fabsf(v[1]);
824 if(fabs(v[2]) > max_z) max_z = fabsf(v[2]);
825 if(v[2] < neg_z) neg_z = v[2];
829 /* This needs some improvements in the vertex decl code */
830 FIXME("Cannot find data to generate. Only generating position and normals\n");
831 patch->has_normals = TRUE;
832 patch->has_texcoords = FALSE;
834 ENTER_GL();
836 glMatrixMode(GL_PROJECTION);
837 checkGLcall("glMatrixMode(GL_PROJECTION)");
838 glLoadIdentity();
839 checkGLcall("glLoadIdentity()");
840 glScalef(1.0f / (max_x), 1.0f / (max_y), max_z == 0.0f ? 1.0f : 1.0f / (2.0f * max_z));
841 glTranslatef(0.0f, 0.0f, 0.5f);
842 checkGLcall("glScalef");
843 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
844 checkGLcall("glViewport");
846 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
847 * our feedback buffer parser
849 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
850 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
851 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
852 if(patch->has_normals) {
853 static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
854 static const GLfloat red[] = {1.0f, 0.0f, 0.0f, 0.0f};
855 static const GLfloat green[] = {0.0f, 1.0f, 0.0f, 0.0f};
856 static const GLfloat blue[] = {0.0f, 0.0f, 1.0f, 0.0f};
857 static const GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
858 glEnable(GL_LIGHTING);
859 checkGLcall("glEnable(GL_LIGHTING)");
860 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
861 checkGLcall("glLightModel for MODEL_AMBIENT");
862 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
864 for (i = 3; i < context->gl_info->limits.lights; ++i)
866 glDisable(GL_LIGHT0 + i);
867 checkGLcall("glDisable(GL_LIGHT0 + i)");
868 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
871 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
872 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
873 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
874 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
875 glLightfv(GL_LIGHT0, GL_POSITION, red);
876 glEnable(GL_LIGHT0);
877 checkGLcall("Setting up light 1");
878 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
879 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
880 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
881 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
882 glLightfv(GL_LIGHT1, GL_POSITION, green);
883 glEnable(GL_LIGHT1);
884 checkGLcall("Setting up light 2");
885 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
886 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
887 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
888 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
889 glLightfv(GL_LIGHT2, GL_POSITION, blue);
890 glEnable(GL_LIGHT2);
891 checkGLcall("Setting up light 3");
893 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
894 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
895 glDisable(GL_COLOR_MATERIAL);
896 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
897 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
898 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
899 checkGLcall("Setting up materials");
902 /* Enable the needed maps.
903 * GL_MAP2_VERTEX_3 is needed for positional data.
904 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
905 * GL_MAP2_TEXTURE_COORD_4 for texture coords
907 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
908 out_vertex_size = 3 /* position */;
909 d3d_out_vertex_size = 3;
910 glEnable(GL_MAP2_VERTEX_3);
911 if(patch->has_normals && patch->has_texcoords) {
912 FIXME("Texcoords not handled yet\n");
913 feedback_type = GL_3D_COLOR_TEXTURE;
914 out_vertex_size += 8;
915 d3d_out_vertex_size += 7;
916 glEnable(GL_AUTO_NORMAL);
917 glEnable(GL_MAP2_TEXTURE_COORD_4);
918 } else if(patch->has_texcoords) {
919 FIXME("Texcoords not handled yet\n");
920 feedback_type = GL_3D_COLOR_TEXTURE;
921 out_vertex_size += 7;
922 d3d_out_vertex_size += 4;
923 glEnable(GL_MAP2_TEXTURE_COORD_4);
924 } else if(patch->has_normals) {
925 feedback_type = GL_3D_COLOR;
926 out_vertex_size += 4;
927 d3d_out_vertex_size += 3;
928 glEnable(GL_AUTO_NORMAL);
929 } else {
930 feedback_type = GL_3D;
932 checkGLcall("glEnable vertex attrib generation");
934 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
935 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
936 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
938 glMap2f(GL_MAP2_VERTEX_3,
939 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
940 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
941 (const GLfloat *)data);
942 checkGLcall("glMap2f");
943 if(patch->has_texcoords) {
944 glMap2f(GL_MAP2_TEXTURE_COORD_4,
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");
950 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0f, 1.0f, ceilf(patch->numSegs[1]), 0.0f, 1.0f);
951 checkGLcall("glMapGrid2f");
953 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
954 checkGLcall("glFeedbackBuffer");
955 glRenderMode(GL_FEEDBACK);
957 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
958 checkGLcall("glEvalMesh2");
960 i = glRenderMode(GL_RENDER);
961 if(i == -1) {
962 LEAVE_GL();
963 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
964 HeapFree(GetProcessHeap(), 0, feedbuffer);
965 context_release(context);
966 return WINED3DERR_DRIVERINTERNALERROR;
967 } else if(i != buffer_size) {
968 LEAVE_GL();
969 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
970 HeapFree(GetProcessHeap(), 0, feedbuffer);
971 context_release(context);
972 return WINED3DERR_DRIVERINTERNALERROR;
973 } else {
974 TRACE("Got %d elements as expected\n", i);
977 HeapFree(GetProcessHeap(), 0, patch->mem);
978 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
979 i = 0;
980 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
981 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
982 ERR("Unexpected token: %f\n", feedbuffer[j]);
983 continue;
985 if(feedbuffer[j + 1] != 3) {
986 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
987 continue;
989 /* Somehow there are different ideas about back / front facing, so fix up the
990 * vertex order
992 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
993 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
994 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 3 */
995 if(patch->has_normals) {
996 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
997 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
998 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1000 i += d3d_out_vertex_size;
1002 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1003 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1004 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 2 */
1005 if(patch->has_normals) {
1006 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1007 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1008 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1010 i += d3d_out_vertex_size;
1012 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1013 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1014 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 1 */
1015 if(patch->has_normals) {
1016 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1017 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1018 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1020 i += d3d_out_vertex_size;
1023 if(patch->has_normals) {
1024 /* Now do the same with reverse light directions */
1025 static const GLfloat x[] = {-1.0f, 0.0f, 0.0f, 0.0f};
1026 static const GLfloat y[] = { 0.0f, -1.0f, 0.0f, 0.0f};
1027 static const GLfloat z[] = { 0.0f, 0.0f, -1.0f, 0.0f};
1028 glLightfv(GL_LIGHT0, GL_POSITION, x);
1029 glLightfv(GL_LIGHT1, GL_POSITION, y);
1030 glLightfv(GL_LIGHT2, GL_POSITION, z);
1031 checkGLcall("Setting up reverse light directions");
1033 glRenderMode(GL_FEEDBACK);
1034 checkGLcall("glRenderMode(GL_FEEDBACK)");
1035 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1036 checkGLcall("glEvalMesh2");
1037 i = glRenderMode(GL_RENDER);
1038 checkGLcall("glRenderMode(GL_RENDER)");
1040 i = 0;
1041 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1042 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1043 ERR("Unexpected token: %f\n", feedbuffer[j]);
1044 continue;
1046 if(feedbuffer[j + 1] != 3) {
1047 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1048 continue;
1050 if(patch->mem[i + 3] == 0.0f)
1051 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1052 if(patch->mem[i + 4] == 0.0f)
1053 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1054 if(patch->mem[i + 5] == 0.0f)
1055 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1056 normalize_normal(patch->mem + i + 3);
1057 i += d3d_out_vertex_size;
1059 if(patch->mem[i + 3] == 0.0f)
1060 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1061 if(patch->mem[i + 4] == 0.0f)
1062 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1063 if(patch->mem[i + 5] == 0.0f)
1064 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1065 normalize_normal(patch->mem + i + 3);
1066 i += d3d_out_vertex_size;
1068 if(patch->mem[i + 3] == 0.0f)
1069 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1070 if(patch->mem[i + 4] == 0.0f)
1071 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1072 if(patch->mem[i + 5] == 0.0f)
1073 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1074 normalize_normal(patch->mem + i + 3);
1075 i += d3d_out_vertex_size;
1079 glDisable(GL_MAP2_VERTEX_3);
1080 glDisable(GL_AUTO_NORMAL);
1081 glDisable(GL_MAP2_NORMAL);
1082 glDisable(GL_MAP2_TEXTURE_COORD_4);
1083 checkGLcall("glDisable vertex attrib generation");
1084 LEAVE_GL();
1086 context_release(context);
1088 HeapFree(GetProcessHeap(), 0, feedbuffer);
1090 vtxStride = 3 * sizeof(float);
1091 if(patch->has_normals) {
1092 vtxStride += 3 * sizeof(float);
1094 if(patch->has_texcoords) {
1095 vtxStride += 4 * sizeof(float);
1097 memset(&patch->strided, 0, sizeof(patch->strided));
1098 patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1099 patch->strided.position.lpData = (BYTE *) patch->mem;
1100 patch->strided.position.dwStride = vtxStride;
1102 if(patch->has_normals) {
1103 patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1104 patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1105 patch->strided.normal.dwStride = vtxStride;
1107 if(patch->has_texcoords) {
1108 patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1109 patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1110 if(patch->has_normals) {
1111 patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1113 patch->strided.texCoords[0].dwStride = vtxStride;
1116 return WINED3D_OK;