VirtualBox

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

Last change on this file since 22652 was 22496, checked in by vboxsync, 15 years ago

crOpenGL: update wine to 1.1.27 and better fix for depthstencil surface refcounting

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