VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/drawprim.c@ 41283

Last change on this file since 41283 was 39130, checked in by vboxsync, 13 years ago

wddm: fix image inversion on Mac; debugging stuff

  • Property svn:eol-style set to native
File size: 46.3 KB
Line 
1/*
2 * WINED3D draw functions
3 *
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
11 *
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.
16 *
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.
21 *
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
25 */
26
27/*
28 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
29 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
30 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
31 * a choice of LGPL license versions is made available with the language indicating
32 * that LGPLv2 or any later version may be used, or where a choice of which version
33 * of the LGPL is applied is otherwise unspecified.
34 */
35
36#include "config.h"
37#include "wined3d_private.h"
38
39#if defined(VBOX_WITH_WDDM)
40#include "../VBox/VBoxDbgGl.h"
41#endif
42
43WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
44#define GLINFO_LOCATION This->adapter->gl_info
45
46#include <stdio.h>
47#include <math.h>
48
49#define ceilf ceil
50
51/* GL locking is done by the caller */
52static void drawStridedFast(IWineD3DDevice *iface, GLenum primitive_type,
53 UINT count, UINT idx_size, const void *idx_data, UINT start_idx)
54{
55 if (idx_size)
56 {
57 TRACE("(%p) : glElements(%x, %d, ...)\n", iface, primitive_type, count);
58
59 glDrawElements(primitive_type, count,
60 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
61 (const char *)idx_data + (idx_size * start_idx));
62 checkGLcall("glDrawElements");
63 }
64 else
65 {
66 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", iface, primitive_type, start_idx, count);
67
68 glDrawArrays(primitive_type, start_idx, count);
69 checkGLcall("glDrawArrays");
70 }
71}
72
73/*
74 * Actually draw using the supplied information.
75 * Slower GL version which extracts info about each vertex in turn
76 */
77
78/* GL locking is done by the caller */
79static void drawStridedSlow(IWineD3DDevice *iface, const struct wined3d_context *context,
80 const struct wined3d_stream_info *si, UINT NumVertexes, GLenum glPrimType,
81 const void *idxData, UINT idxSize, UINT startIdx)
82{
83 unsigned int textureNo = 0;
84 const WORD *pIdxBufS = NULL;
85 const DWORD *pIdxBufL = NULL;
86 UINT vx_index;
87 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
88 const UINT *streamOffset = This->stateBlock->streamOffset;
89 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
90 BOOL pixelShader = use_ps(This->stateBlock);
91 BOOL specular_fog = FALSE;
92 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
93 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
94 const struct wined3d_gl_info *gl_info = context->gl_info;
95 UINT texture_stages = gl_info->limits.texture_stages;
96 const struct wined3d_stream_info_element *element;
97 UINT num_untracked_materials;
98 DWORD tex_mask = 0;
99
100 TRACE("Using slow vertex array code\n");
101
102 /* Variable Initialization */
103 if (idxSize != 0) {
104 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
105 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
106 * idxData will be != NULL
107 */
108 if(idxData == NULL) {
109 idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
110 }
111
112 if (idxSize == 2) pIdxBufS = idxData;
113 else pIdxBufL = idxData;
114 } else if (idxData) {
115 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
116 return;
117 }
118
119 /* Start drawing in GL */
120 glBegin(glPrimType);
121
122 if (si->use_map & (1 << WINED3D_FFP_POSITION))
123 {
124 element = &si->elements[WINED3D_FFP_POSITION];
125 position = element->data + streamOffset[element->stream_idx];
126 }
127
128 if (si->use_map & (1 << WINED3D_FFP_NORMAL))
129 {
130 element = &si->elements[WINED3D_FFP_NORMAL];
131 normal = element->data + streamOffset[element->stream_idx];
132 }
133 else
134 {
135 glNormal3f(0, 0, 0);
136 }
137
138 num_untracked_materials = context->num_untracked_materials;
139 if (si->use_map & (1 << WINED3D_FFP_DIFFUSE))
140 {
141 element = &si->elements[WINED3D_FFP_DIFFUSE];
142 diffuse = element->data + streamOffset[element->stream_idx];
143
144 if (num_untracked_materials && element->format_desc->format != WINED3DFMT_B8G8R8A8_UNORM)
145 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format_desc->format));
146 }
147 else
148 {
149 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
150 }
151
152 if (si->use_map & (1 << WINED3D_FFP_SPECULAR))
153 {
154 element = &si->elements[WINED3D_FFP_SPECULAR];
155 specular = element->data + streamOffset[element->stream_idx];
156
157 /* special case where the fog density is stored in the specular alpha channel */
158 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
159 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
160 || si->elements[WINED3D_FFP_POSITION].format_desc->format == WINED3DFMT_R32G32B32A32_FLOAT)
161 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
162 {
163 if (gl_info->supported[EXT_FOG_COORD])
164 {
165 if (element->format_desc->format == WINED3DFMT_B8G8R8A8_UNORM) specular_fog = TRUE;
166 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format_desc->format));
167 }
168 else
169 {
170 static BOOL warned;
171
172 if (!warned)
173 {
174 /* TODO: Use the fog table code from old ddraw */
175 FIXME("Implement fog for transformed vertices in software\n");
176 warned = TRUE;
177 }
178 }
179 }
180 }
181 else if (gl_info->supported[EXT_SECONDARY_COLOR])
182 {
183 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
184 }
185
186 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
187 {
188 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
189 DWORD texture_idx = This->texUnitMap[textureNo];
190
191 if (!gl_info->supported[ARB_MULTITEXTURE] && textureNo > 0)
192 {
193 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
194 continue;
195 }
196
197 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
198
199 if (texture_idx == WINED3D_UNMAPPED_STAGE) continue;
200
201 if (coordIdx > 7)
202 {
203 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
204 continue;
205 }
206 else if (coordIdx < 0)
207 {
208 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
209 continue;
210 }
211
212 if (si->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + coordIdx)))
213 {
214 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
215 texCoords[coordIdx] = element->data + streamOffset[element->stream_idx];
216 tex_mask |= (1 << textureNo);
217 }
218 else
219 {
220 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
221 if (gl_info->supported[ARB_MULTITEXTURE])
222 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
223 else
224 glTexCoord4f(0, 0, 0, 1);
225 }
226 }
227
228 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
229 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
230 */
231
232 /* For each primitive */
233 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
234 UINT texture, tmp_tex_mask;
235 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
236 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
237 */
238
239 /* For indexed data, we need to go a few more strides in */
240 if (idxData != NULL) {
241
242 /* Indexed so work out the number of strides to skip */
243 if (idxSize == 2)
244 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
245 else
246 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
247 }
248
249 tmp_tex_mask = tex_mask;
250 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
251 {
252 int coord_idx;
253 const void *ptr;
254 DWORD texture_idx;
255
256 if (!(tmp_tex_mask & 1)) continue;
257
258 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
259 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
260
261 texture_idx = This->texUnitMap[texture];
262 multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format_desc->emit_idx](
263 GL_TEXTURE0_ARB + texture_idx, ptr);
264 }
265
266 /* Diffuse -------------------------------- */
267 if (diffuse) {
268 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
269
270 diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format_desc->emit_idx](ptrToCoords);
271 if (num_untracked_materials)
272 {
273 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
274 unsigned char i;
275 float color[4];
276
277 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
278 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
279 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
280 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
281
282 for (i = 0; i < num_untracked_materials; ++i)
283 {
284 glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
285 }
286 }
287 }
288
289 /* Specular ------------------------------- */
290 if (specular) {
291 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
292
293 specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format_desc->emit_idx](ptrToCoords);
294
295 if (specular_fog)
296 {
297 DWORD specularColor = *(const DWORD *)ptrToCoords;
298 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
299 }
300 }
301
302 /* Normal -------------------------------- */
303 if (normal != NULL) {
304 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
305 normal_funcs[si->elements[WINED3D_FFP_NORMAL].format_desc->emit_idx](ptrToCoords);
306 }
307
308 /* Position -------------------------------- */
309 if (position) {
310 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
311 position_funcs[si->elements[WINED3D_FFP_POSITION].format_desc->emit_idx](ptrToCoords);
312 }
313
314 /* For non indexed mode, step onto next parts */
315 if (idxData == NULL) {
316 ++SkipnStrides;
317 }
318 }
319
320 glEnd();
321 checkGLcall("glEnd and previous calls");
322}
323
324/* GL locking is done by the caller */
325static inline void send_attribute(IWineD3DDeviceImpl *This, WINED3DFORMAT format, const UINT index, const void *ptr)
326{
327 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
328
329 switch(format)
330 {
331 case WINED3DFMT_R32_FLOAT:
332 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
333 break;
334 case WINED3DFMT_R32G32_FLOAT:
335 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
336 break;
337 case WINED3DFMT_R32G32B32_FLOAT:
338 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
339 break;
340 case WINED3DFMT_R32G32B32A32_FLOAT:
341 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
342 break;
343
344 case WINED3DFMT_R8G8B8A8_UINT:
345 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
346 break;
347 case WINED3DFMT_B8G8R8A8_UNORM:
348 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
349 {
350 const DWORD *src = ptr;
351 DWORD c = *src & 0xff00ff00;
352 c |= (*src & 0xff0000) >> 16;
353 c |= (*src & 0xff) << 16;
354 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
355 break;
356 }
357 /* else fallthrough */
358 case WINED3DFMT_R8G8B8A8_UNORM:
359 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
360 break;
361
362 case WINED3DFMT_R16G16_SINT:
363 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
364 break;
365 case WINED3DFMT_R16G16B16A16_SINT:
366 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
367 break;
368
369 case WINED3DFMT_R16G16_SNORM:
370 {
371 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
372 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
373 break;
374 }
375 case WINED3DFMT_R16G16_UNORM:
376 {
377 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
378 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
379 break;
380 }
381 case WINED3DFMT_R16G16B16A16_SNORM:
382 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
383 break;
384 case WINED3DFMT_R16G16B16A16_UNORM:
385 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
386 break;
387
388 case WINED3DFMT_R10G10B10A2_UINT:
389 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
390 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
391 break;
392 case WINED3DFMT_R10G10B10A2_SNORM:
393 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
394 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
395 break;
396
397 case WINED3DFMT_R16G16_FLOAT:
398 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
399 * byte float according to the IEEE standard
400 */
401 if (gl_info->supported[NV_HALF_FLOAT])
402 {
403 /* Not supported by GL_ARB_half_float_vertex */
404 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
405 }
406 else
407 {
408 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
409 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
410 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
411 }
412 break;
413 case WINED3DFMT_R16G16B16A16_FLOAT:
414 if (gl_info->supported[NV_HALF_FLOAT])
415 {
416 /* Not supported by GL_ARB_half_float_vertex */
417 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
418 }
419 else
420 {
421 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
422 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
423 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
424 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
425 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
426 }
427 break;
428
429 default:
430 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
431 break;
432 }
433}
434
435/* GL locking is done by the caller */
436static void drawStridedSlowVs(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT numberOfVertices,
437 GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT startIdx)
438{
439 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
440 long SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
441 const WORD *pIdxBufS = NULL;
442 const DWORD *pIdxBufL = NULL;
443 UINT vx_index;
444 int i;
445 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
446 const BYTE *ptr;
447
448 if (idxSize != 0) {
449 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
450 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
451 * idxData will be != NULL
452 */
453 if(idxData == NULL) {
454 idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
455 }
456
457 if (idxSize == 2) pIdxBufS = idxData;
458 else pIdxBufL = idxData;
459 } else if (idxData) {
460 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
461 return;
462 }
463
464 /* Start drawing in GL */
465 glBegin(glPrimitiveType);
466
467 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
468 if (idxData != NULL) {
469
470 /* Indexed so work out the number of strides to skip */
471 if (idxSize == 2)
472 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
473 else
474 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
475 }
476
477 for (i = MAX_ATTRIBS - 1; i >= 0; i--)
478 {
479 if (!(si->use_map & (1 << i))) continue;
480
481 ptr = si->elements[i].data +
482 si->elements[i].stride * SkipnStrides +
483 stateblock->streamOffset[si->elements[i].stream_idx];
484
485 send_attribute(This, si->elements[i].format_desc->format, i, ptr);
486 }
487 SkipnStrides++;
488 }
489
490 glEnd();
491}
492
493/* GL locking is done by the caller */
494static inline void drawStridedInstanced(IWineD3DDevice *iface, const struct wined3d_stream_info *si,
495 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, UINT idxSize,
496 UINT startIdx)
497{
498 UINT numInstances = 0, i;
499 int numInstancedAttribs = 0, j;
500 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
501 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
502 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
503
504 if (idxSize == 0) {
505 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
506 * We don't support this for now
507 *
508 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
509 * But the StreamSourceFreq value has a different meaning in that situation.
510 */
511 FIXME("Non-indexed instanced drawing is not supported\n");
512 return;
513 }
514
515 TRACE("(%p) : glElements(%x, %d, ...)\n", This, glPrimitiveType, numberOfVertices);
516
517 /* First, figure out how many instances we have to draw */
518 for(i = 0; i < MAX_STREAMS; i++) {
519 /* Look at the streams and take the first one which matches */
520 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
521 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
522 if(stateblock->streamFreq[i] == 0){
523 numInstances = 1;
524 } else {
525 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
526 }
527 break; /* break, because only the first suitable value is interesting */
528 }
529 }
530
531 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
532 {
533 if (!(si->use_map & (1 << i))) continue;
534
535 if (stateblock->streamFlags[si->elements[i].stream_idx] & WINED3DSTREAMSOURCE_INSTANCEDATA)
536 {
537 instancedData[numInstancedAttribs] = i;
538 numInstancedAttribs++;
539 }
540 }
541
542 /* now draw numInstances instances :-) */
543 for(i = 0; i < numInstances; i++) {
544 /* Specify the instanced attributes using immediate mode calls */
545 for(j = 0; j < numInstancedAttribs; j++) {
546 const BYTE *ptr = si->elements[instancedData[j]].data +
547 si->elements[instancedData[j]].stride * i +
548 stateblock->streamOffset[si->elements[instancedData[j]].stream_idx];
549 if (si->elements[instancedData[j]].buffer_object)
550 {
551 struct wined3d_buffer *vb =
552 (struct wined3d_buffer *)stateblock->streamSource[si->elements[instancedData[j]].stream_idx];
553 ptr += (long) buffer_get_sysmem(vb);
554 }
555
556 send_attribute(This, si->elements[instancedData[j]].format_desc->format, instancedData[j], ptr);
557 }
558
559 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
560 (const char *)idxData+(idxSize * startIdx));
561 checkGLcall("glDrawElements");
562 }
563}
564
565static inline void remove_vbos(IWineD3DDeviceImpl *This, struct wined3d_stream_info *s)
566{
567 unsigned int i;
568
569 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
570 {
571 struct wined3d_stream_info_element *e;
572
573 if (!(s->use_map & (1 << i))) continue;
574
575 e = &s->elements[i];
576 if (e->buffer_object)
577 {
578 struct wined3d_buffer *vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
579 e->buffer_object = 0;
580 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
581 }
582 }
583}
584
585/* Routine common to the draw primitive and draw indexed primitive routines */
586void drawPrimitive(IWineD3DDevice *iface, UINT index_count, UINT StartIdx, UINT idxSize, const void *idxData)
587{
588
589 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
590 IWineD3DSurfaceImpl *target;
591 struct wined3d_context *context;
592 unsigned int i;
593
594 if (!index_count) return;
595
596 if (This->stateBlock->renderState[WINED3DRS_COLORWRITEENABLE])
597 {
598 /* Invalidate the back buffer memory so LockRect will read it the next time */
599 for (i = 0; i < This->adapter->gl_info.limits.buffers; ++i)
600 {
601 target = (IWineD3DSurfaceImpl *)This->render_targets[i];
602 if (target)
603 {
604 IWineD3DSurface_LoadLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, NULL);
605 IWineD3DSurface_ModifyLocation((IWineD3DSurface *)target, SFLAG_INDRAWABLE, TRUE);
606 }
607 }
608 }
609
610 /* Signals other modules that a drawing is in progress and the stateblock finalized */
611 This->isInDraw = TRUE;
612
613 context = context_acquire(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
614 if (!context->valid)
615 {
616 context_release(context);
617 WARN("Invalid context, skipping draw.\n");
618 return;
619 }
620
621#if defined(VBOX_WITH_WDDM)
622 DBGL_CHECK_DRAWPRIM(context->gl_info, This);
623#endif
624
625 if (This->stencilBufferTarget) {
626 /* Note that this depends on the context_acquire() call above to set
627 * This->render_offscreen properly. We don't currently take the
628 * Z-compare function into account, but we could skip loading the
629 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
630 * that we never copy the stencil data.*/
631 DWORD location = context->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
632 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE]
633 || This->stateBlock->renderState[WINED3DRS_ZENABLE])
634 surface_load_ds_location(This->stencilBufferTarget, context, location);
635 if (This->stateBlock->renderState[WINED3DRS_ZWRITEENABLE])
636 surface_modify_ds_location(This->stencilBufferTarget, location);
637 }
638
639 /* Ok, we will be updating the screen from here onwards so grab the lock */
640 ENTER_GL();
641 {
642 GLenum glPrimType = This->stateBlock->gl_primitive_type;
643 BOOL emulation = FALSE;
644 const struct wined3d_stream_info *stream_info = &This->strided_streams;
645 struct wined3d_stream_info stridedlcl;
646
647 if (!use_vs(This->stateBlock))
648 {
649 if (!This->strided_streams.position_transformed && context->num_untracked_materials
650 && This->stateBlock->renderState[WINED3DRS_LIGHTING])
651 {
652 static BOOL warned;
653 if (!warned) {
654 FIXME("Using software emulation because not all material properties could be tracked\n");
655 warned = TRUE;
656 } else {
657 TRACE("Using software emulation because not all material properties could be tracked\n");
658 }
659 emulation = TRUE;
660 }
661 else if (context->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE])
662 {
663 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
664 * to a float in the vertex buffer
665 */
666 static BOOL warned;
667 if (!warned) {
668 FIXME("Using software emulation because manual fog coordinates are provided\n");
669 warned = TRUE;
670 } else {
671 TRACE("Using software emulation because manual fog coordinates are provided\n");
672 }
673 emulation = TRUE;
674 }
675
676 if(emulation) {
677 stream_info = &stridedlcl;
678 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
679 remove_vbos(This, &stridedlcl);
680 }
681 }
682
683 if (This->useDrawStridedSlow || emulation) {
684 /* Immediate mode drawing */
685 if (use_vs(This->stateBlock))
686 {
687 static BOOL warned;
688 if (!warned) {
689 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
690 warned = TRUE;
691 } else {
692 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
693 }
694 drawStridedSlowVs(iface, stream_info, index_count, glPrimType, idxData, idxSize, StartIdx);
695 } else {
696 drawStridedSlow(iface, context, stream_info, index_count,
697 glPrimType, idxData, idxSize, StartIdx);
698 }
699 } else if(This->instancedDraw) {
700 /* Instancing emulation with mixing immediate mode and arrays */
701 drawStridedInstanced(iface, &This->strided_streams, index_count,
702 glPrimType, idxData, idxSize, StartIdx);
703 } else {
704 drawStridedFast(iface, glPrimType, index_count, idxSize, idxData, StartIdx);
705 }
706 }
707
708 /* Finished updating the screen, restore lock */
709 LEAVE_GL();
710
711 for(i = 0; i < This->num_buffer_queries; i++)
712 {
713 wined3d_event_query_issue(This->buffer_queries[i], This);
714 }
715
716 if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
717
718 context_release(context);
719
720 TRACE("Done all gl drawing\n");
721
722 /* Diagnostics */
723#ifdef SHOW_FRAME_MAKEUP
724 {
725 static long int primCounter = 0;
726 /* NOTE: set primCounter to the value reported by drawprim
727 before you want to to write frame makeup to /tmp */
728 if (primCounter >= 0) {
729 WINED3DLOCKED_RECT r;
730 char buffer[80];
731 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
732 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
733 TRACE("Saving screenshot %s\n", buffer);
734 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
735 IWineD3DSurface_UnlockRect(This->render_targets[0]);
736
737#ifdef SHOW_TEXTURE_MAKEUP
738 {
739 IWineD3DSurface *pSur;
740 int textureNo;
741 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
742 if (This->stateBlock->textures[textureNo] != NULL) {
743 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
744 TRACE("Saving texture %s\n", buffer);
745 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
746 IWineD3DTexture_GetSurfaceLevel(This->stateBlock->textures[textureNo], 0, &pSur);
747 IWineD3DSurface_SaveSnapshot(pSur, buffer);
748 IWineD3DSurface_Release(pSur);
749 } else {
750 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
751 }
752 }
753 }
754 }
755#endif
756 }
757 TRACE("drawprim #%ld\n", primCounter);
758 ++primCounter;
759 }
760#endif
761
762 /* Control goes back to the device, stateblock values may change again */
763 This->isInDraw = FALSE;
764}
765
766static void normalize_normal(float *n) {
767 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
768 if (length == 0.0f) return;
769 length = sqrt(length);
770 n[0] = n[0] / length;
771 n[1] = n[1] / length;
772 n[2] = n[2] / length;
773}
774
775/* Tesselates a high order rectangular patch into single triangles using gl evaluators
776 *
777 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
778 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
779 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
780 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
781 * in drawprim.
782 *
783 * To read back, the opengl feedback mode is used. This creates a problem because we want
784 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
785 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
786 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
787 * them to [-1.0;+1.0] and set the viewport up to scale them back.
788 *
789 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
790 * resulting colors back to the normals.
791 *
792 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
793 * does not restore it because normally a draw follows immediately afterwards. The caller is
794 * responsible of taking care that either the gl states are restored, or the context activated
795 * for drawing to reset the lastWasBlit flag.
796 */
797HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
798 struct WineD3DRectPatch *patch) {
799 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
800 float max_x = 0.0f, max_y = 0.0f, max_z = 0.0f, neg_z = 0.0f;
801 struct wined3d_stream_info stream_info;
802 struct wined3d_stream_info_element *e;
803 struct wined3d_context *context;
804 const BYTE *data;
805 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
806 DWORD vtxStride;
807 GLenum feedback_type;
808 GLfloat *feedbuffer;
809
810 /* Simply activate the context for blitting. This disables all the things we don't want and
811 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
812 * patch (as opposed to normal draws) will most likely need different changes anyway. */
813 context = context_acquire(This, NULL, CTXUSAGE_BLIT);
814
815 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
816 * Beware of vbos
817 */
818 device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
819
820 e = &stream_info.elements[WINED3D_FFP_POSITION];
821 if (e->buffer_object)
822 {
823 struct wined3d_buffer *vb;
824 vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
825 e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
826 }
827 vtxStride = e->stride;
828 data = e->data +
829 vtxStride * info->Stride * info->StartVertexOffsetHeight +
830 vtxStride * info->StartVertexOffsetWidth;
831
832 /* Not entirely sure about what happens with transformed vertices */
833 if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
834
835 if(vtxStride % sizeof(GLfloat)) {
836 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
837 * I don't see how the stride could not be a multiple of 4, but make sure
838 * to check it
839 */
840 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
841 }
842 if(info->Basis != WINED3DBASIS_BEZIER) {
843 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
844 }
845 if(info->Degree != WINED3DDEGREE_CUBIC) {
846 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
847 }
848
849 /* First, get the boundary cube of the input data */
850 for(j = 0; j < info->Height; j++) {
851 for(i = 0; i < info->Width; i++) {
852 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
853 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
854 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
855 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
856 if(v[2] < neg_z) neg_z = v[2];
857 }
858 }
859
860 /* This needs some improvements in the vertex decl code */
861 FIXME("Cannot find data to generate. Only generating position and normals\n");
862 patch->has_normals = TRUE;
863 patch->has_texcoords = FALSE;
864
865 ENTER_GL();
866
867 glMatrixMode(GL_PROJECTION);
868 checkGLcall("glMatrixMode(GL_PROJECTION)");
869 glLoadIdentity();
870 checkGLcall("glLoadIndentity()");
871 glScalef(1.0f / (max_x), 1.0f / (max_y), max_z == 0.0f ? 1.0f : 1.0f / (2.0f * max_z));
872 glTranslatef(0.0f, 0.0f, 0.5f);
873 checkGLcall("glScalef");
874 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
875 checkGLcall("glViewport");
876
877 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
878 * our feedback buffer parser
879 */
880 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
881 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
882 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
883 if(patch->has_normals) {
884 static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
885 static const GLfloat red[] = {1.0f, 0.0f, 0.0f, 0.0f};
886 static const GLfloat green[] = {0.0f, 1.0f, 0.0f, 0.0f};
887 static const GLfloat blue[] = {0.0f, 0.0f, 1.0f, 0.0f};
888 static const GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
889 glEnable(GL_LIGHTING);
890 checkGLcall("glEnable(GL_LIGHTING)");
891 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
892 checkGLcall("glLightModel for MODEL_AMBIENT");
893 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
894
895 for (i = 3; i < context->gl_info->limits.lights; ++i)
896 {
897 glDisable(GL_LIGHT0 + i);
898 checkGLcall("glDisable(GL_LIGHT0 + i)");
899 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
900 }
901
902 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
903 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
904 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
905 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
906 glLightfv(GL_LIGHT0, GL_POSITION, red);
907 glEnable(GL_LIGHT0);
908 checkGLcall("Setting up light 1");
909 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
910 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
911 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
912 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
913 glLightfv(GL_LIGHT1, GL_POSITION, green);
914 glEnable(GL_LIGHT1);
915 checkGLcall("Setting up light 2");
916 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
917 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
918 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
919 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
920 glLightfv(GL_LIGHT2, GL_POSITION, blue);
921 glEnable(GL_LIGHT2);
922 checkGLcall("Setting up light 3");
923
924 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
925 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
926 glDisable(GL_COLOR_MATERIAL);
927 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
928 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
929 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
930 checkGLcall("Setting up materials");
931 }
932
933 /* Enable the needed maps.
934 * GL_MAP2_VERTEX_3 is needed for positional data.
935 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
936 * GL_MAP2_TEXTURE_COORD_4 for texture coords
937 */
938 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
939 out_vertex_size = 3 /* position */;
940 d3d_out_vertex_size = 3;
941 glEnable(GL_MAP2_VERTEX_3);
942 if(patch->has_normals && patch->has_texcoords) {
943 FIXME("Texcoords not handled yet\n");
944 feedback_type = GL_3D_COLOR_TEXTURE;
945 out_vertex_size += 8;
946 d3d_out_vertex_size += 7;
947 glEnable(GL_AUTO_NORMAL);
948 glEnable(GL_MAP2_TEXTURE_COORD_4);
949 } else if(patch->has_texcoords) {
950 FIXME("Texcoords not handled yet\n");
951 feedback_type = GL_3D_COLOR_TEXTURE;
952 out_vertex_size += 7;
953 d3d_out_vertex_size += 4;
954 glEnable(GL_MAP2_TEXTURE_COORD_4);
955 } else if(patch->has_normals) {
956 feedback_type = GL_3D_COLOR;
957 out_vertex_size += 4;
958 d3d_out_vertex_size += 3;
959 glEnable(GL_AUTO_NORMAL);
960 } else {
961 feedback_type = GL_3D;
962 }
963 checkGLcall("glEnable vertex attrib generation");
964
965 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
966 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
967 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
968
969 glMap2f(GL_MAP2_VERTEX_3,
970 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
971 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
972 (const GLfloat *)data);
973 checkGLcall("glMap2f");
974 if(patch->has_texcoords) {
975 glMap2f(GL_MAP2_TEXTURE_COORD_4,
976 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
977 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
978 (const GLfloat *)data);
979 checkGLcall("glMap2f");
980 }
981 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0f, 1.0f, ceilf(patch->numSegs[1]), 0.0f, 1.0f);
982 checkGLcall("glMapGrid2f");
983
984 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
985 checkGLcall("glFeedbackBuffer");
986 glRenderMode(GL_FEEDBACK);
987
988 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
989 checkGLcall("glEvalMesh2");
990
991 i = glRenderMode(GL_RENDER);
992 if(i == -1) {
993 LEAVE_GL();
994 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
995 HeapFree(GetProcessHeap(), 0, feedbuffer);
996 context_release(context);
997 return WINED3DERR_DRIVERINTERNALERROR;
998 } else if(i != buffer_size) {
999 LEAVE_GL();
1000 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1001 HeapFree(GetProcessHeap(), 0, feedbuffer);
1002 context_release(context);
1003 return WINED3DERR_DRIVERINTERNALERROR;
1004 } else {
1005 TRACE("Got %d elements as expected\n", i);
1006 }
1007
1008 HeapFree(GetProcessHeap(), 0, patch->mem);
1009 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1010 i = 0;
1011 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1012 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1013 ERR("Unexpected token: %f\n", feedbuffer[j]);
1014 continue;
1015 }
1016 if(feedbuffer[j + 1] != 3) {
1017 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1018 continue;
1019 }
1020 /* Somehow there are different ideas about back / front facing, so fix up the
1021 * vertex order
1022 */
1023 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1024 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1025 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 3 */
1026 if(patch->has_normals) {
1027 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1028 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1029 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1030 }
1031 i += d3d_out_vertex_size;
1032
1033 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1034 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1035 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 2 */
1036 if(patch->has_normals) {
1037 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1038 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1039 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1040 }
1041 i += d3d_out_vertex_size;
1042
1043 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1044 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1045 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 1 */
1046 if(patch->has_normals) {
1047 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1048 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1049 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1050 }
1051 i += d3d_out_vertex_size;
1052 }
1053
1054 if(patch->has_normals) {
1055 /* Now do the same with reverse light directions */
1056 static const GLfloat x[] = {-1.0f, 0.0f, 0.0f, 0.0f};
1057 static const GLfloat y[] = { 0.0f, -1.0f, 0.0f, 0.0f};
1058 static const GLfloat z[] = { 0.0f, 0.0f, -1.0f, 0.0f};
1059 glLightfv(GL_LIGHT0, GL_POSITION, x);
1060 glLightfv(GL_LIGHT1, GL_POSITION, y);
1061 glLightfv(GL_LIGHT2, GL_POSITION, z);
1062 checkGLcall("Setting up reverse light directions");
1063
1064 glRenderMode(GL_FEEDBACK);
1065 checkGLcall("glRenderMode(GL_FEEDBACK)");
1066 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1067 checkGLcall("glEvalMesh2");
1068 i = glRenderMode(GL_RENDER);
1069 checkGLcall("glRenderMode(GL_RENDER)");
1070
1071 i = 0;
1072 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1073 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1074 ERR("Unexpected token: %f\n", feedbuffer[j]);
1075 continue;
1076 }
1077 if(feedbuffer[j + 1] != 3) {
1078 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1079 continue;
1080 }
1081 if(patch->mem[i + 3] == 0.0f)
1082 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1083 if(patch->mem[i + 4] == 0.0f)
1084 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1085 if(patch->mem[i + 5] == 0.0f)
1086 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1087 normalize_normal(patch->mem + i + 3);
1088 i += d3d_out_vertex_size;
1089
1090 if(patch->mem[i + 3] == 0.0f)
1091 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1092 if(patch->mem[i + 4] == 0.0f)
1093 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1094 if(patch->mem[i + 5] == 0.0f)
1095 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1096 normalize_normal(patch->mem + i + 3);
1097 i += d3d_out_vertex_size;
1098
1099 if(patch->mem[i + 3] == 0.0f)
1100 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1101 if(patch->mem[i + 4] == 0.0f)
1102 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1103 if(patch->mem[i + 5] == 0.0f)
1104 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1105 normalize_normal(patch->mem + i + 3);
1106 i += d3d_out_vertex_size;
1107 }
1108 }
1109
1110 glDisable(GL_MAP2_VERTEX_3);
1111 glDisable(GL_AUTO_NORMAL);
1112 glDisable(GL_MAP2_NORMAL);
1113 glDisable(GL_MAP2_TEXTURE_COORD_4);
1114 checkGLcall("glDisable vertex attrib generation");
1115 LEAVE_GL();
1116
1117 context_release(context);
1118
1119 HeapFree(GetProcessHeap(), 0, feedbuffer);
1120
1121 vtxStride = 3 * sizeof(float);
1122 if(patch->has_normals) {
1123 vtxStride += 3 * sizeof(float);
1124 }
1125 if(patch->has_texcoords) {
1126 vtxStride += 4 * sizeof(float);
1127 }
1128 memset(&patch->strided, 0, sizeof(patch->strided));
1129 patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1130 patch->strided.position.lpData = (BYTE *) patch->mem;
1131 patch->strided.position.dwStride = vtxStride;
1132
1133 if(patch->has_normals) {
1134 patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1135 patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1136 patch->strided.normal.dwStride = vtxStride;
1137 }
1138 if(patch->has_texcoords) {
1139 patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1140 patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1141 if(patch->has_normals) {
1142 patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1143 }
1144 patch->strided.texCoords[0].dwStride = vtxStride;
1145 }
1146
1147 return WINED3D_OK;
1148}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette