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