VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/drawprim.c@ 51270

Last change on this file since 51270 was 51270, checked in by vboxsync, 11 years ago

wine: update to 1.6.2

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.6 KB
Line 
1/*
2 * WINED3D draw functions
3 *
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006, 2008 Henri Verbeet
9 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10 * Copyright 2009 Henri Verbeet for CodeWeavers
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27/*
28 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
29 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
30 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
31 * a choice of LGPL license versions is made available with the language indicating
32 * that LGPLv2 or any later version may be used, or where a choice of which version
33 * of the LGPL is applied is otherwise unspecified.
34 */
35
36#include "config.h"
37#include "wine/port.h"
38
39#include "wined3d_private.h"
40
41WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
42WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
43
44#include <stdio.h>
45#include <math.h>
46
47#ifdef DEBUG_misha
48#include "../VBox/VBoxDbgGl.h"
49#endif
50
51#ifdef VBOX_WITH_WINE_FIXES
52# define ceilf ceil
53#endif
54
55/* Context activation is done by the caller. */
56static void drawStridedFast(const struct wined3d_gl_info *gl_info, GLenum primitive_type, UINT count, UINT idx_size,
57 const void *idx_data, UINT start_idx, INT base_vertex_index, UINT start_instance, UINT instance_count)
58{
59 if (idx_size)
60 {
61 GLenum idxtype = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
62 if (instance_count)
63 {
64 if (!gl_info->supported[ARB_DRAW_INSTANCED] && !gl_info->supported[ARB_INSTANCED_ARRAYS])
65 {
66 FIXME("Instanced drawing not supported.\n");
67 }
68 else
69 {
70 if (start_instance)
71 FIXME("Start instance (%u) not supported.\n", start_instance);
72 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
73 {
74 GL_EXTCALL(glDrawElementsInstancedBaseVertex(primitive_type, count, idxtype,
75 (const char *)idx_data + (idx_size * start_idx), instance_count, base_vertex_index));
76 checkGLcall("glDrawElementsInstancedBaseVertex");
77 }
78 else
79 {
80 GL_EXTCALL(glDrawElementsInstancedARB(primitive_type, count, idxtype,
81 (const char *)idx_data + (idx_size * start_idx), instance_count));
82 checkGLcall("glDrawElementsInstancedARB");
83 }
84 }
85 }
86 else if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
87 {
88 GL_EXTCALL(glDrawElementsBaseVertex(primitive_type, count, idxtype,
89 (const char *)idx_data + (idx_size * start_idx), base_vertex_index));
90 checkGLcall("glDrawElementsBaseVertex");
91 }
92 else
93 {
94 gl_info->gl_ops.gl.p_glDrawElements(primitive_type, count,
95 idxtype, (const char *)idx_data + (idx_size * start_idx));
96 checkGLcall("glDrawElements");
97 }
98 }
99 else
100 {
101 gl_info->gl_ops.gl.p_glDrawArrays(primitive_type, start_idx, count);
102 checkGLcall("glDrawArrays");
103 }
104}
105
106/*
107 * Actually draw using the supplied information.
108 * Slower GL version which extracts info about each vertex in turn
109 */
110
111/* Context activation is done by the caller. */
112static void drawStridedSlow(const struct wined3d_device *device, const struct wined3d_context *context,
113 const struct wined3d_stream_info *si, UINT NumVertexes, GLenum glPrimType,
114 const void *idxData, UINT idxSize, UINT startIdx)
115{
116 unsigned int textureNo = 0;
117 const WORD *pIdxBufS = NULL;
118 const DWORD *pIdxBufL = NULL;
119 UINT vx_index;
120 const struct wined3d_state *state = &device->stateBlock->state;
121 LONG SkipnStrides = startIdx;
122 BOOL pixelShader = use_ps(state);
123 BOOL specular_fog = FALSE;
124 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
125 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
126 const struct wined3d_gl_info *gl_info = context->gl_info;
127 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
128 UINT texture_stages = d3d_info->limits.ffp_blend_stages;
129 const struct wined3d_stream_info_element *element;
130 UINT num_untracked_materials;
131 DWORD tex_mask = 0;
132
133 TRACE_(d3d_perf)("Using slow vertex array code\n");
134
135 /* Variable Initialization */
136 if (idxSize)
137 {
138 /* Immediate mode drawing can't make use of indices in a vbo - get the
139 * data from the index buffer. If the index buffer has no vbo (not
140 * supported or other reason), or with user pointer drawing idxData
141 * will be non-NULL. */
142 if (!idxData)
143 idxData = buffer_get_sysmem(state->index_buffer, gl_info);
144
145 if (idxSize == 2) pIdxBufS = idxData;
146 else pIdxBufL = idxData;
147 } else if (idxData) {
148 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
149 return;
150 }
151
152 /* Start drawing in GL */
153 gl_info->gl_ops.gl.p_glBegin(glPrimType);
154
155 if (si->use_map & (1 << WINED3D_FFP_POSITION))
156 {
157 element = &si->elements[WINED3D_FFP_POSITION];
158 position = element->data.addr;
159 }
160
161 if (si->use_map & (1 << WINED3D_FFP_NORMAL))
162 {
163 element = &si->elements[WINED3D_FFP_NORMAL];
164 normal = element->data.addr;
165 }
166 else
167 {
168 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
169 }
170
171 num_untracked_materials = context->num_untracked_materials;
172 if (si->use_map & (1 << WINED3D_FFP_DIFFUSE))
173 {
174 element = &si->elements[WINED3D_FFP_DIFFUSE];
175 diffuse = element->data.addr;
176
177 if (num_untracked_materials && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
178 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format->id));
179 }
180 else
181 {
182 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
183 }
184
185 if (si->use_map & (1 << WINED3D_FFP_SPECULAR))
186 {
187 element = &si->elements[WINED3D_FFP_SPECULAR];
188 specular = element->data.addr;
189
190 /* special case where the fog density is stored in the specular alpha channel */
191 if (state->render_states[WINED3D_RS_FOGENABLE]
192 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
193 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
194 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
195 {
196 if (gl_info->supported[EXT_FOG_COORD])
197 {
198 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM) specular_fog = TRUE;
199 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format->id));
200 }
201 else
202 {
203 static BOOL warned;
204
205 if (!warned)
206 {
207 /* TODO: Use the fog table code from old ddraw */
208 FIXME("Implement fog for transformed vertices in software\n");
209 warned = TRUE;
210 }
211 }
212 }
213 }
214 else if (gl_info->supported[EXT_SECONDARY_COLOR])
215 {
216 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
217 }
218
219 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
220 {
221 int coordIdx = state->texture_states[textureNo][WINED3D_TSS_TEXCOORD_INDEX];
222 DWORD texture_idx = device->texUnitMap[textureNo];
223
224 if (!gl_info->supported[ARB_MULTITEXTURE] && textureNo > 0)
225 {
226 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
227 continue;
228 }
229
230 if (!pixelShader && !state->textures[textureNo]) continue;
231
232 if (texture_idx == WINED3D_UNMAPPED_STAGE) continue;
233
234 if (coordIdx > 7)
235 {
236 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
237 continue;
238 }
239 else if (coordIdx < 0)
240 {
241 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
242 continue;
243 }
244
245 if (si->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + coordIdx)))
246 {
247 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
248 texCoords[coordIdx] = element->data.addr;
249 tex_mask |= (1 << textureNo);
250 }
251 else
252 {
253 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
254 if (gl_info->supported[ARB_MULTITEXTURE])
255 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
256 else
257 gl_info->gl_ops.gl.p_glTexCoord4f(0, 0, 0, 1);
258 }
259 }
260
261 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
262 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
263 */
264
265 /* For each primitive */
266 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
267 UINT texture, tmp_tex_mask;
268 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
269 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
270 */
271
272 /* For indexed data, we need to go a few more strides in */
273 if (idxData)
274 {
275 /* Indexed so work out the number of strides to skip */
276 if (idxSize == 2)
277 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->base_vertex_index;
278 else
279 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->base_vertex_index;
280 }
281
282 tmp_tex_mask = tex_mask;
283 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
284 {
285 int coord_idx;
286 const void *ptr;
287 DWORD texture_idx;
288
289 if (!(tmp_tex_mask & 1)) continue;
290
291 coord_idx = state->texture_states[texture][WINED3D_TSS_TEXCOORD_INDEX];
292 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
293
294 texture_idx = device->texUnitMap[texture];
295 multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
296 GL_TEXTURE0_ARB + texture_idx, ptr);
297 }
298
299 /* Diffuse -------------------------------- */
300 if (diffuse) {
301 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
302
303 diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptrToCoords);
304 if (num_untracked_materials)
305 {
306 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
307 unsigned char i;
308 float color[4];
309
310 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
311 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
312 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
313 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
314
315 for (i = 0; i < num_untracked_materials; ++i)
316 {
317 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
318 }
319 }
320 }
321
322 /* Specular ------------------------------- */
323 if (specular) {
324 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
325
326 specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptrToCoords);
327
328 if (specular_fog)
329 {
330 DWORD specularColor = *(const DWORD *)ptrToCoords;
331 GL_EXTCALL(glFogCoordfEXT((float) (specularColor >> 24)));
332 }
333 }
334
335 /* Normal -------------------------------- */
336 if (normal)
337 {
338 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
339 normal_funcs[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptrToCoords);
340 }
341
342 /* Position -------------------------------- */
343 if (position) {
344 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
345 position_funcs[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptrToCoords);
346 }
347
348 /* For non indexed mode, step onto next parts */
349 if (!idxData) ++SkipnStrides;
350 }
351
352 gl_info->gl_ops.gl.p_glEnd();
353 checkGLcall("glEnd and previous calls");
354}
355
356/* Context activation is done by the caller. */
357static inline void send_attribute(const struct wined3d_gl_info *gl_info,
358 enum wined3d_format_id format, const UINT index, const void *ptr)
359{
360 switch(format)
361 {
362 case WINED3DFMT_R32_FLOAT:
363 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
364 break;
365 case WINED3DFMT_R32G32_FLOAT:
366 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
367 break;
368 case WINED3DFMT_R32G32B32_FLOAT:
369 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
370 break;
371 case WINED3DFMT_R32G32B32A32_FLOAT:
372 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
373 break;
374
375 case WINED3DFMT_R8G8B8A8_UINT:
376 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
377 break;
378 case WINED3DFMT_B8G8R8A8_UNORM:
379 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
380 {
381 const DWORD *src = ptr;
382 DWORD c = *src & 0xff00ff00;
383 c |= (*src & 0xff0000) >> 16;
384 c |= (*src & 0xff) << 16;
385 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
386 break;
387 }
388 /* else fallthrough */
389 case WINED3DFMT_R8G8B8A8_UNORM:
390 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
391 break;
392
393 case WINED3DFMT_R16G16_SINT:
394 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
395 break;
396 case WINED3DFMT_R16G16B16A16_SINT:
397 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
398 break;
399
400 case WINED3DFMT_R16G16_SNORM:
401 {
402 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
403 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
404 break;
405 }
406 case WINED3DFMT_R16G16_UNORM:
407 {
408 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
409 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
410 break;
411 }
412 case WINED3DFMT_R16G16B16A16_SNORM:
413 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
414 break;
415 case WINED3DFMT_R16G16B16A16_UNORM:
416 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
417 break;
418
419 case WINED3DFMT_R10G10B10A2_UINT:
420 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
421 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
422 break;
423 case WINED3DFMT_R10G10B10A2_SNORM:
424 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
425 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
426 break;
427
428 case WINED3DFMT_R16G16_FLOAT:
429 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
430 * byte float according to the IEEE standard
431 */
432 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
433 {
434 /* Not supported by GL_ARB_half_float_vertex */
435 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
436 }
437 else
438 {
439 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
440 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
441 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
442 }
443 break;
444 case WINED3DFMT_R16G16B16A16_FLOAT:
445 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
446 {
447 /* Not supported by GL_ARB_half_float_vertex */
448 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
449 }
450 else
451 {
452 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
453 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
454 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
455 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
456 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
457 }
458 break;
459
460 default:
461 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
462 break;
463 }
464}
465
466/* Context activation is done by the caller. */
467static void drawStridedSlowVs(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
468 const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
469 const void *idxData, UINT idxSize, UINT startIdx)
470{
471 LONG SkipnStrides = startIdx + state->load_base_vertex_index;
472 const DWORD *pIdxBufL = NULL;
473 const WORD *pIdxBufS = NULL;
474 UINT vx_index;
475 int i;
476 const BYTE *ptr;
477
478 if (idxSize)
479 {
480 /* Immediate mode drawing can't make use of indices in a vbo - get the
481 * data from the index buffer. If the index buffer has no vbo (not
482 * supported or other reason), or with user pointer drawing idxData
483 * will be non-NULL. */
484 if (!idxData)
485 idxData = buffer_get_sysmem(state->index_buffer, gl_info);
486
487 if (idxSize == 2) pIdxBufS = idxData;
488 else pIdxBufL = idxData;
489 } else if (idxData) {
490 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
491 return;
492 }
493
494 /* Start drawing in GL */
495 gl_info->gl_ops.gl.p_glBegin(glPrimitiveType);
496
497 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index)
498 {
499 if (idxData)
500 {
501 /* Indexed so work out the number of strides to skip */
502 if (idxSize == 2)
503 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->load_base_vertex_index;
504 else
505 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->load_base_vertex_index;
506 }
507
508 for (i = MAX_ATTRIBS - 1; i >= 0; i--)
509 {
510 if (!(si->use_map & (1 << i)))
511 {
512#ifdef VBOX_WITH_WINE_FIX_ZEROVERTATTR
513 if (i == 0)
514 {
515# ifdef DEBUG_misha
516 ERR("Test it!\n");
517# endif
518 GL_EXTCALL(glVertexAttrib4fARB(0, 0.0f, 0.0f, 0.0f, 0.0f));
519 }
520#endif
521 continue;
522 }
523
524 ptr = si->elements[i].data.addr + si->elements[i].stride * SkipnStrides;
525
526 send_attribute(gl_info, si->elements[i].format->id, i, ptr);
527 }
528 SkipnStrides++;
529 }
530
531 gl_info->gl_ops.gl.p_glEnd();
532}
533
534/* Context activation is done by the caller. */
535static void drawStridedInstanced(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
536 const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
537 const void *idxData, UINT idxSize, UINT startIdx, UINT base_vertex_index, UINT instance_count)
538{
539 int numInstancedAttribs = 0, j;
540 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
541 GLenum idxtype = idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
542 UINT i;
543
544 if (!idxSize)
545 {
546 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
547 * We don't support this for now
548 *
549 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
550 * But the StreamSourceFreq value has a different meaning in that situation.
551 */
552 FIXME("Non-indexed instanced drawing is not supported\n");
553 return;
554 }
555
556 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
557 {
558 if (!(si->use_map & (1 << i))) continue;
559
560 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
561 {
562 instancedData[numInstancedAttribs] = i;
563 numInstancedAttribs++;
564 }
565 }
566
567 for (i = 0; i < instance_count; ++i)
568 {
569 /* Specify the instanced attributes using immediate mode calls */
570 for(j = 0; j < numInstancedAttribs; j++) {
571 const BYTE *ptr = si->elements[instancedData[j]].data.addr
572 + si->elements[instancedData[j]].stride * i;
573 if (si->elements[instancedData[j]].data.buffer_object)
574 {
575 struct wined3d_buffer *vb = state->streams[si->elements[instancedData[j]].stream_idx].buffer;
576 ptr += (ULONG_PTR)buffer_get_sysmem(vb, gl_info);
577 }
578
579 send_attribute(gl_info, si->elements[instancedData[j]].format->id, instancedData[j], ptr);
580 }
581
582 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
583 {
584 GL_EXTCALL(glDrawElementsBaseVertex(glPrimitiveType, numberOfVertices, idxtype,
585 (const char *)idxData+(idxSize * startIdx), base_vertex_index));
586 checkGLcall("glDrawElementsBaseVertex");
587 }
588 else
589 {
590 gl_info->gl_ops.gl.p_glDrawElements(glPrimitiveType, numberOfVertices, idxtype,
591 (const char *)idxData + (idxSize * startIdx));
592 checkGLcall("glDrawElements");
593 }
594 }
595}
596
597static void remove_vbos(const struct wined3d_gl_info *gl_info,
598 const struct wined3d_state *state, struct wined3d_stream_info *s)
599{
600 unsigned int i;
601
602 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
603 {
604 struct wined3d_stream_info_element *e;
605
606 if (!(s->use_map & (1 << i))) continue;
607
608 e = &s->elements[i];
609 if (e->data.buffer_object)
610 {
611 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
612 e->data.buffer_object = 0;
613 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
614 }
615 }
616}
617
618/* Routine common to the draw primitive and draw indexed primitive routines */
619void draw_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count,
620 UINT start_instance, UINT instance_count, BOOL indexed)
621{
622 const struct wined3d_state *state = &device->stateBlock->state;
623 const struct wined3d_stream_info *stream_info;
624 struct wined3d_event_query *ib_query = NULL;
625 struct wined3d_stream_info si_emulated;
626 const struct wined3d_gl_info *gl_info;
627 struct wined3d_context *context;
628 BOOL emulation = FALSE;
629 const void *idx_data = NULL;
630 UINT idx_size = 0;
631 unsigned int i;
632
633 if (!index_count) return;
634
635 if (state->render_states[WINED3D_RS_COLORWRITEENABLE])
636 {
637 /* Invalidate the back buffer memory so LockRect will read it the next time */
638 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
639 {
640 struct wined3d_surface *target = device->fb.render_targets[i];
641 if (target)
642 {
643 surface_load_location(target, target->draw_binding, NULL);
644 surface_modify_location(target, target->draw_binding, TRUE);
645 }
646 }
647 }
648
649 /* Signals other modules that a drawing is in progress and the stateblock finalized */
650 device->isInDraw = TRUE;
651
652 context = context_acquire(device, device->fb.render_targets[0]);
653 if (!context->valid)
654 {
655 context_release(context);
656 WARN("Invalid context, skipping draw.\n");
657 return;
658 }
659 gl_info = context->gl_info;
660
661 if (device->fb.depth_stencil)
662 {
663 /* Note that this depends on the context_acquire() call above to set
664 * context->render_offscreen properly. We don't currently take the
665 * Z-compare function into account, but we could skip loading the
666 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
667 * that we never copy the stencil data.*/
668 DWORD location = context->render_offscreen ? device->fb.depth_stencil->draw_binding : SFLAG_INDRAWABLE;
669 if (state->render_states[WINED3D_RS_ZWRITEENABLE] || state->render_states[WINED3D_RS_ZENABLE])
670 {
671 struct wined3d_surface *ds = device->fb.depth_stencil;
672 RECT current_rect, draw_rect, r;
673
674 if (!context->render_offscreen && ds != device->onscreen_depth_stencil)
675 device_switch_onscreen_ds(device, context, ds);
676
677 if (ds->flags & location)
678 SetRect(&current_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
679 else
680 SetRectEmpty(&current_rect);
681
682 wined3d_get_draw_rect(state, &draw_rect);
683
684 IntersectRect(&r, &draw_rect, &current_rect);
685 if (!EqualRect(&r, &draw_rect))
686 surface_load_ds_location(ds, context, location);
687 }
688 }
689
690#ifdef VBOX_WITH_WINE_FIX_ZEROVERTATTR
691 Assert(device->czvDrawVertices == 0);
692 device->czvDrawVertices = index_count;
693#endif
694
695 if (!context_apply_draw_state(context, device))
696 {
697 context_release(context);
698 WARN("Unable to apply draw state, skipping draw.\n");
699#ifdef VBOX_WITH_WINE_FIX_ZEROVERTATTR
700 device->czvDrawVertices = 0;
701#endif
702 return;
703 }
704
705#ifdef VBOX_WITH_WINE_FIX_ZEROVERTATTR
706 device->czvDrawVertices = 0;
707#endif
708
709
710#ifdef DEBUG_misha
711 DBGL_CHECK_DRAWPRIM(context->gl_info, device);
712#endif
713
714 if (device->fb.depth_stencil && state->render_states[WINED3D_RS_ZWRITEENABLE])
715 {
716 struct wined3d_surface *ds = device->fb.depth_stencil;
717 DWORD location = context->render_offscreen ? ds->draw_binding : SFLAG_INDRAWABLE;
718
719 surface_modify_ds_location(ds, location, ds->ds_current_size.cx, ds->ds_current_size.cy);
720 }
721
722 if ((!gl_info->supported[WINED3D_GL_VERSION_2_0]
723 || !gl_info->supported[NV_POINT_SPRITE])
724 && context->render_offscreen
725 && state->render_states[WINED3D_RS_POINTSPRITEENABLE]
726 && state->gl_primitive_type == GL_POINTS)
727 {
728 FIXME("Point sprite coordinate origin switching not supported.\n");
729 }
730
731 stream_info = &device->stream_info;
732 if (device->instance_count)
733 instance_count = device->instance_count;
734
735 if (indexed)
736 {
737 struct wined3d_buffer *index_buffer = state->index_buffer;
738 if (!index_buffer->buffer_object || !stream_info->all_vbo)
739 idx_data = index_buffer->resource.allocatedMemory;
740 else
741 {
742 ib_query = index_buffer->query;
743 idx_data = NULL;
744 }
745
746 if (state->index_format == WINED3DFMT_R16_UINT)
747 idx_size = 2;
748 else
749 idx_size = 4;
750 }
751
752 if (!use_vs(state))
753 {
754 if (!stream_info->position_transformed && context->num_untracked_materials
755 && state->render_states[WINED3D_RS_LIGHTING])
756 {
757 static BOOL warned;
758
759 if (!warned++)
760 FIXME("Using software emulation because not all material properties could be tracked.\n");
761 else
762 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
763 emulation = TRUE;
764 }
765 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
766 {
767 static BOOL warned;
768
769 /* Either write a pipeline replacement shader or convert the
770 * specular alpha from unsigned byte to a float in the vertex
771 * buffer. */
772 if (!warned++)
773 FIXME("Using software emulation because manual fog coordinates are provided.\n");
774 else
775 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
776 emulation = TRUE;
777 }
778
779 if (emulation)
780 {
781 si_emulated = device->stream_info;
782 remove_vbos(gl_info, state, &si_emulated);
783 stream_info = &si_emulated;
784 }
785 }
786
787 if (device->useDrawStridedSlow || emulation)
788 {
789 /* Immediate mode drawing. */
790 if (use_vs(state))
791 {
792 static BOOL warned;
793
794 if (!warned++)
795 FIXME("Using immediate mode with vertex shaders for half float emulation.\n");
796 else
797 WARN_(d3d_perf)("Using immediate mode with vertex shaders for half float emulation.\n");
798
799 drawStridedSlowVs(gl_info, state, stream_info, index_count,
800 state->gl_primitive_type, idx_data, idx_size, start_idx);
801 }
802 else
803 {
804 drawStridedSlow(device, context, stream_info, index_count,
805 state->gl_primitive_type, idx_data, idx_size, start_idx);
806 }
807 }
808 else if (!gl_info->supported[ARB_INSTANCED_ARRAYS] && instance_count)
809 {
810 /* Instancing emulation by mixing immediate mode and arrays. */
811 drawStridedInstanced(gl_info, state, stream_info, index_count, state->gl_primitive_type,
812 idx_data, idx_size, start_idx, state->base_vertex_index, instance_count);
813 }
814 else
815 {
816 drawStridedFast(gl_info, state->gl_primitive_type, index_count, idx_size, idx_data,
817 start_idx, state->base_vertex_index, start_instance, instance_count);
818 }
819
820 if (ib_query)
821 wined3d_event_query_issue(ib_query, device);
822 for (i = 0; i < device->num_buffer_queries; ++i)
823 {
824 wined3d_event_query_issue(device->buffer_queries[i], device);
825 }
826
827 if (wined3d_settings.strict_draw_ordering)
828 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
829
830 context_release(context);
831
832 TRACE("Done all gl drawing\n");
833
834 /* Control goes back to the device, stateblock values may change again */
835 device->isInDraw = FALSE;
836}
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