VirtualBox

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

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

wine/new: export

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