2 * WINED3D draw functions
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
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.
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.
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
28 #include "wine/port.h"
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw
);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
38 /* Context activation is done by the caller. */
39 static void drawStridedFast(const struct wined3d_gl_info
*gl_info
, GLenum primitive_type
, UINT count
, UINT idx_size
,
40 const void *idx_data
, UINT start_idx
, INT base_vertex_index
, UINT start_instance
, UINT instance_count
)
44 GLenum idxtype
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
47 if (!gl_info
->supported
[ARB_DRAW_INSTANCED
] && !gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
49 FIXME("Instanced drawing not supported.\n");
54 FIXME("Start instance (%u) not supported.\n", start_instance
);
55 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
57 GL_EXTCALL(glDrawElementsInstancedBaseVertex(primitive_type
, count
, idxtype
,
58 (const char *)idx_data
+ (idx_size
* start_idx
), instance_count
, base_vertex_index
));
59 checkGLcall("glDrawElementsInstancedBaseVertex");
63 GL_EXTCALL(glDrawElementsInstancedARB(primitive_type
, count
, idxtype
,
64 (const char *)idx_data
+ (idx_size
* start_idx
), instance_count
));
65 checkGLcall("glDrawElementsInstancedARB");
69 else if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
71 GL_EXTCALL(glDrawElementsBaseVertex(primitive_type
, count
, idxtype
,
72 (const char *)idx_data
+ (idx_size
* start_idx
), base_vertex_index
));
73 checkGLcall("glDrawElementsBaseVertex");
77 gl_info
->gl_ops
.gl
.p_glDrawElements(primitive_type
, count
,
78 idxtype
, (const char *)idx_data
+ (idx_size
* start_idx
));
79 checkGLcall("glDrawElements");
84 gl_info
->gl_ops
.gl
.p_glDrawArrays(primitive_type
, start_idx
, count
);
85 checkGLcall("glDrawArrays");
90 * Actually draw using the supplied information.
91 * Slower GL version which extracts info about each vertex in turn
94 /* Context activation is done by the caller. */
95 static void drawStridedSlow(const struct wined3d_device
*device
, struct wined3d_context
*context
,
96 const struct wined3d_stream_info
*si
, UINT NumVertexes
, GLenum glPrimType
,
97 const void *idxData
, UINT idxSize
, UINT startIdx
)
99 unsigned int textureNo
= 0;
100 const WORD
*pIdxBufS
= NULL
;
101 const DWORD
*pIdxBufL
= NULL
;
103 const struct wined3d_state
*state
= &device
->state
;
104 LONG SkipnStrides
= startIdx
;
105 BOOL pixelShader
= use_ps(state
);
106 BOOL specular_fog
= FALSE
;
107 const BYTE
*texCoords
[WINED3DDP_MAXTEXCOORD
];
108 const BYTE
*diffuse
= NULL
, *specular
= NULL
, *normal
= NULL
, *position
= NULL
;
109 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
110 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
111 const struct wined3d_ffp_attrib_ops
*ops
= &d3d_info
->ffp_attrib_ops
;
112 UINT texture_stages
= d3d_info
->limits
.ffp_blend_stages
;
113 const struct wined3d_stream_info_element
*element
;
114 UINT num_untracked_materials
;
117 TRACE_(d3d_perf
)("Using slow vertex array code\n");
119 /* Variable Initialization */
122 /* Immediate mode drawing can't make use of indices in a vbo - get the
123 * data from the index buffer. If the index buffer has no vbo (not
124 * supported or other reason), or with user pointer drawing idxData
125 * will be non-NULL. */
127 idxData
= buffer_get_sysmem(state
->index_buffer
, context
);
129 if (idxSize
== 2) pIdxBufS
= idxData
;
130 else pIdxBufL
= idxData
;
131 } else if (idxData
) {
132 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
136 /* Start drawing in GL */
137 gl_info
->gl_ops
.gl
.p_glBegin(glPrimType
);
139 if (si
->use_map
& (1 << WINED3D_FFP_POSITION
))
141 element
= &si
->elements
[WINED3D_FFP_POSITION
];
142 position
= element
->data
.addr
;
145 if (si
->use_map
& (1 << WINED3D_FFP_NORMAL
))
147 element
= &si
->elements
[WINED3D_FFP_NORMAL
];
148 normal
= element
->data
.addr
;
152 gl_info
->gl_ops
.gl
.p_glNormal3f(0, 0, 0);
155 num_untracked_materials
= context
->num_untracked_materials
;
156 if (si
->use_map
& (1 << WINED3D_FFP_DIFFUSE
))
158 element
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
159 diffuse
= element
->data
.addr
;
161 if (num_untracked_materials
&& element
->format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
162 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element
->format
->id
));
166 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
169 if (si
->use_map
& (1 << WINED3D_FFP_SPECULAR
))
171 element
= &si
->elements
[WINED3D_FFP_SPECULAR
];
172 specular
= element
->data
.addr
;
174 /* special case where the fog density is stored in the specular alpha channel */
175 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
176 && (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
177 || si
->elements
[WINED3D_FFP_POSITION
].format
->id
== WINED3DFMT_R32G32B32A32_FLOAT
)
178 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
180 if (gl_info
->supported
[EXT_FOG_COORD
])
182 if (element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
) specular_fog
= TRUE
;
183 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element
->format
->id
));
191 /* TODO: Use the fog table code from old ddraw */
192 FIXME("Implement fog for transformed vertices in software\n");
198 else if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
200 GL_EXTCALL(glSecondaryColor3fEXT
)(0, 0, 0);
203 for (textureNo
= 0; textureNo
< texture_stages
; ++textureNo
)
205 int coordIdx
= state
->texture_states
[textureNo
][WINED3D_TSS_TEXCOORD_INDEX
];
206 DWORD texture_idx
= context
->tex_unit_map
[textureNo
];
208 if (!gl_info
->supported
[ARB_MULTITEXTURE
] && textureNo
> 0)
210 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
214 if (!pixelShader
&& !state
->textures
[textureNo
]) continue;
216 if (texture_idx
== WINED3D_UNMAPPED_STAGE
) continue;
220 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo
);
223 else if (coordIdx
< 0)
225 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo
, coordIdx
);
229 if (si
->use_map
& (1 << (WINED3D_FFP_TEXCOORD0
+ coordIdx
)))
231 element
= &si
->elements
[WINED3D_FFP_TEXCOORD0
+ coordIdx
];
232 texCoords
[coordIdx
] = element
->data
.addr
;
233 tex_mask
|= (1 << textureNo
);
237 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo
);
238 if (gl_info
->supported
[ARB_MULTITEXTURE
])
239 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_idx
, 0, 0, 0, 1));
241 gl_info
->gl_ops
.gl
.p_glTexCoord4f(0, 0, 0, 1);
245 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
246 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
249 /* For each primitive */
250 for (vx_index
= 0; vx_index
< NumVertexes
; ++vx_index
) {
251 UINT texture
, tmp_tex_mask
;
252 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
253 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
256 /* For indexed data, we need to go a few more strides in */
259 /* Indexed so work out the number of strides to skip */
261 SkipnStrides
= pIdxBufS
[startIdx
+ vx_index
] + state
->base_vertex_index
;
263 SkipnStrides
= pIdxBufL
[startIdx
+ vx_index
] + state
->base_vertex_index
;
266 tmp_tex_mask
= tex_mask
;
267 for (texture
= 0; tmp_tex_mask
; tmp_tex_mask
>>= 1, ++texture
)
273 if (!(tmp_tex_mask
& 1)) continue;
275 coord_idx
= state
->texture_states
[texture
][WINED3D_TSS_TEXCOORD_INDEX
];
276 ptr
= texCoords
[coord_idx
] + (SkipnStrides
* si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].stride
);
278 texture_idx
= context
->tex_unit_map
[texture
];
279 ops
->texcoord
[si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].format
->emit_idx
](
280 GL_TEXTURE0_ARB
+ texture_idx
, ptr
);
283 /* Diffuse -------------------------------- */
286 const void *ptrToCoords
= diffuse
+ SkipnStrides
* si
->elements
[WINED3D_FFP_DIFFUSE
].stride
;
287 ops
->diffuse
[si
->elements
[WINED3D_FFP_DIFFUSE
].format
->emit_idx
](ptrToCoords
);
289 if (num_untracked_materials
)
291 DWORD diffuseColor
= ((const DWORD
*)ptrToCoords
)[0];
295 color
[0] = D3DCOLOR_B_R(diffuseColor
) / 255.0f
;
296 color
[1] = D3DCOLOR_B_G(diffuseColor
) / 255.0f
;
297 color
[2] = D3DCOLOR_B_B(diffuseColor
) / 255.0f
;
298 color
[3] = D3DCOLOR_B_A(diffuseColor
) / 255.0f
;
300 for (i
= 0; i
< num_untracked_materials
; ++i
)
302 gl_info
->gl_ops
.gl
.p_glMaterialfv(GL_FRONT_AND_BACK
, context
->untracked_materials
[i
], color
);
307 /* Specular ------------------------------- */
310 const void *ptrToCoords
= specular
+ SkipnStrides
* si
->elements
[WINED3D_FFP_SPECULAR
].stride
;
311 ops
->specular
[si
->elements
[WINED3D_FFP_SPECULAR
].format
->emit_idx
](ptrToCoords
);
315 DWORD specularColor
= *(const DWORD
*)ptrToCoords
;
316 GL_EXTCALL(glFogCoordfEXT((float) (specularColor
>> 24)));
320 /* Normal -------------------------------- */
323 const void *ptrToCoords
= normal
+ SkipnStrides
* si
->elements
[WINED3D_FFP_NORMAL
].stride
;
324 ops
->normal
[si
->elements
[WINED3D_FFP_NORMAL
].format
->emit_idx
](ptrToCoords
);
327 /* Position -------------------------------- */
330 const void *ptrToCoords
= position
+ SkipnStrides
* si
->elements
[WINED3D_FFP_POSITION
].stride
;
331 ops
->position
[si
->elements
[WINED3D_FFP_POSITION
].format
->emit_idx
](ptrToCoords
);
334 /* For non indexed mode, step onto next parts */
335 if (!idxData
) ++SkipnStrides
;
338 gl_info
->gl_ops
.gl
.p_glEnd();
339 checkGLcall("glEnd and previous calls");
342 /* Context activation is done by the caller. */
343 static inline void send_attribute(const struct wined3d_gl_info
*gl_info
,
344 enum wined3d_format_id format
, const UINT index
, const void *ptr
)
348 case WINED3DFMT_R32_FLOAT
:
349 GL_EXTCALL(glVertexAttrib1fvARB(index
, ptr
));
351 case WINED3DFMT_R32G32_FLOAT
:
352 GL_EXTCALL(glVertexAttrib2fvARB(index
, ptr
));
354 case WINED3DFMT_R32G32B32_FLOAT
:
355 GL_EXTCALL(glVertexAttrib3fvARB(index
, ptr
));
357 case WINED3DFMT_R32G32B32A32_FLOAT
:
358 GL_EXTCALL(glVertexAttrib4fvARB(index
, ptr
));
361 case WINED3DFMT_R8G8B8A8_UINT
:
362 GL_EXTCALL(glVertexAttrib4ubvARB(index
, ptr
));
364 case WINED3DFMT_B8G8R8A8_UNORM
:
365 if (gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
])
367 const DWORD
*src
= ptr
;
368 DWORD c
= *src
& 0xff00ff00;
369 c
|= (*src
& 0xff0000) >> 16;
370 c
|= (*src
& 0xff) << 16;
371 GL_EXTCALL(glVertexAttrib4NubvARB(index
, (GLubyte
*)&c
));
374 /* else fallthrough */
375 case WINED3DFMT_R8G8B8A8_UNORM
:
376 GL_EXTCALL(glVertexAttrib4NubvARB(index
, ptr
));
379 case WINED3DFMT_R16G16_SINT
:
380 GL_EXTCALL(glVertexAttrib4svARB(index
, ptr
));
382 case WINED3DFMT_R16G16B16A16_SINT
:
383 GL_EXTCALL(glVertexAttrib4svARB(index
, ptr
));
386 case WINED3DFMT_R16G16_SNORM
:
388 GLshort s
[4] = {((const GLshort
*)ptr
)[0], ((const GLshort
*)ptr
)[1], 0, 1};
389 GL_EXTCALL(glVertexAttrib4NsvARB(index
, s
));
392 case WINED3DFMT_R16G16_UNORM
:
394 GLushort s
[4] = {((const GLushort
*)ptr
)[0], ((const GLushort
*)ptr
)[1], 0, 1};
395 GL_EXTCALL(glVertexAttrib4NusvARB(index
, s
));
398 case WINED3DFMT_R16G16B16A16_SNORM
:
399 GL_EXTCALL(glVertexAttrib4NsvARB(index
, ptr
));
401 case WINED3DFMT_R16G16B16A16_UNORM
:
402 GL_EXTCALL(glVertexAttrib4NusvARB(index
, ptr
));
405 case WINED3DFMT_R10G10B10A2_UINT
:
406 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
407 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
409 case WINED3DFMT_R10G10B10A2_SNORM
:
410 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
411 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
414 case WINED3DFMT_R16G16_FLOAT
:
415 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
416 * byte float according to the IEEE standard
418 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
420 /* Not supported by GL_ARB_half_float_vertex */
421 GL_EXTCALL(glVertexAttrib2hvNV(index
, ptr
));
425 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
426 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
427 GL_EXTCALL(glVertexAttrib2fARB(index
, x
, y
));
430 case WINED3DFMT_R16G16B16A16_FLOAT
:
431 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
433 /* Not supported by GL_ARB_half_float_vertex */
434 GL_EXTCALL(glVertexAttrib4hvNV(index
, ptr
));
438 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
439 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
440 float z
= float_16_to_32(((const unsigned short *)ptr
) + 2);
441 float w
= float_16_to_32(((const unsigned short *)ptr
) + 3);
442 GL_EXTCALL(glVertexAttrib4fARB(index
, x
, y
, z
, w
));
447 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format
));
452 /* Context activation is done by the caller. */
453 static void drawStridedSlowVs(struct wined3d_context
*context
, const struct wined3d_state
*state
,
454 const struct wined3d_stream_info
*si
, UINT numberOfVertices
, GLenum glPrimitiveType
,
455 const void *idxData
, UINT idxSize
, UINT startIdx
)
457 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
458 LONG SkipnStrides
= startIdx
+ state
->load_base_vertex_index
;
459 const DWORD
*pIdxBufL
= NULL
;
460 const WORD
*pIdxBufS
= NULL
;
467 /* Immediate mode drawing can't make use of indices in a vbo - get the
468 * data from the index buffer. If the index buffer has no vbo (not
469 * supported or other reason), or with user pointer drawing idxData
470 * will be non-NULL. */
472 idxData
= buffer_get_sysmem(state
->index_buffer
, context
);
474 if (idxSize
== 2) pIdxBufS
= idxData
;
475 else pIdxBufL
= idxData
;
476 } else if (idxData
) {
477 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
481 /* Start drawing in GL */
482 gl_info
->gl_ops
.gl
.p_glBegin(glPrimitiveType
);
484 for (vx_index
= 0; vx_index
< numberOfVertices
; ++vx_index
)
488 /* Indexed so work out the number of strides to skip */
490 SkipnStrides
= pIdxBufS
[startIdx
+ vx_index
] + state
->load_base_vertex_index
;
492 SkipnStrides
= pIdxBufL
[startIdx
+ vx_index
] + state
->load_base_vertex_index
;
495 for (i
= MAX_ATTRIBS
- 1; i
>= 0; i
--)
497 if (!(si
->use_map
& (1 << i
))) continue;
499 ptr
= si
->elements
[i
].data
.addr
+ si
->elements
[i
].stride
* SkipnStrides
;
501 send_attribute(gl_info
, si
->elements
[i
].format
->id
, i
, ptr
);
506 gl_info
->gl_ops
.gl
.p_glEnd();
509 /* Context activation is done by the caller. */
510 static void drawStridedInstanced(struct wined3d_context
*context
, const struct wined3d_state
*state
,
511 const struct wined3d_stream_info
*si
, UINT numberOfVertices
, GLenum glPrimitiveType
,
512 const void *idxData
, UINT idxSize
, UINT startIdx
, UINT base_vertex_index
, UINT instance_count
)
514 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
515 int numInstancedAttribs
= 0, j
;
516 UINT instancedData
[sizeof(si
->elements
) / sizeof(*si
->elements
) /* 16 */];
517 GLenum idxtype
= idxSize
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
522 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
523 * We don't support this for now
525 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
526 * But the StreamSourceFreq value has a different meaning in that situation.
528 FIXME("Non-indexed instanced drawing is not supported\n");
532 for (i
= 0; i
< sizeof(si
->elements
) / sizeof(*si
->elements
); ++i
)
534 if (!(si
->use_map
& (1 << i
))) continue;
536 if (state
->streams
[si
->elements
[i
].stream_idx
].flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
538 instancedData
[numInstancedAttribs
] = i
;
539 numInstancedAttribs
++;
543 for (i
= 0; i
< instance_count
; ++i
)
545 /* Specify the instanced attributes using immediate mode calls */
546 for(j
= 0; j
< numInstancedAttribs
; j
++) {
547 const BYTE
*ptr
= si
->elements
[instancedData
[j
]].data
.addr
548 + si
->elements
[instancedData
[j
]].stride
* i
;
549 if (si
->elements
[instancedData
[j
]].data
.buffer_object
)
551 struct wined3d_buffer
*vb
= state
->streams
[si
->elements
[instancedData
[j
]].stream_idx
].buffer
;
552 ptr
+= (ULONG_PTR
)buffer_get_sysmem(vb
, context
);
555 send_attribute(gl_info
, si
->elements
[instancedData
[j
]].format
->id
, instancedData
[j
], ptr
);
558 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
560 GL_EXTCALL(glDrawElementsBaseVertex(glPrimitiveType
, numberOfVertices
, idxtype
,
561 (const char *)idxData
+(idxSize
* startIdx
), base_vertex_index
));
562 checkGLcall("glDrawElementsBaseVertex");
566 gl_info
->gl_ops
.gl
.p_glDrawElements(glPrimitiveType
, numberOfVertices
, idxtype
,
567 (const char *)idxData
+ (idxSize
* startIdx
));
568 checkGLcall("glDrawElements");
573 static void remove_vbos(struct wined3d_context
*context
,
574 const struct wined3d_state
*state
, struct wined3d_stream_info
*s
)
578 for (i
= 0; i
< (sizeof(s
->elements
) / sizeof(*s
->elements
)); ++i
)
580 struct wined3d_stream_info_element
*e
;
582 if (!(s
->use_map
& (1 << i
))) continue;
585 if (e
->data
.buffer_object
)
587 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
588 e
->data
.buffer_object
= 0;
589 e
->data
.addr
= (BYTE
*)((ULONG_PTR
)e
->data
.addr
+ (ULONG_PTR
)buffer_get_sysmem(vb
, context
));
594 /* Routine common to the draw primitive and draw indexed primitive routines */
595 void draw_primitive(struct wined3d_device
*device
, UINT start_idx
, UINT index_count
,
596 UINT start_instance
, UINT instance_count
, BOOL indexed
)
598 const struct wined3d_state
*state
= &device
->state
;
599 const struct wined3d_stream_info
*stream_info
;
600 struct wined3d_event_query
*ib_query
= NULL
;
601 struct wined3d_stream_info si_emulated
;
602 const struct wined3d_gl_info
*gl_info
;
603 struct wined3d_context
*context
;
604 BOOL emulation
= FALSE
;
605 const void *idx_data
= NULL
;
609 if (!index_count
) return;
611 if (state
->render_states
[WINED3D_RS_COLORWRITEENABLE
])
613 /* Invalidate the back buffer memory so LockRect will read it the next time */
614 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
616 struct wined3d_surface
*target
= device
->fb
.render_targets
[i
];
619 surface_load_location(target
, target
->draw_binding
);
620 surface_invalidate_location(target
, ~target
->draw_binding
);
625 context
= context_acquire(device
, device
->fb
.render_targets
[0]);
628 context_release(context
);
629 WARN("Invalid context, skipping draw.\n");
632 gl_info
= context
->gl_info
;
634 if (device
->fb
.depth_stencil
)
636 /* Note that this depends on the context_acquire() call above to set
637 * context->render_offscreen properly. We don't currently take the
638 * Z-compare function into account, but we could skip loading the
639 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
640 * that we never copy the stencil data.*/
641 DWORD location
= context
->render_offscreen
?
642 device
->fb
.depth_stencil
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
643 if (state
->render_states
[WINED3D_RS_ZWRITEENABLE
] || state
->render_states
[WINED3D_RS_ZENABLE
])
645 struct wined3d_surface
*ds
= device
->fb
.depth_stencil
;
646 RECT current_rect
, draw_rect
, r
;
648 if (!context
->render_offscreen
&& ds
!= device
->onscreen_depth_stencil
)
649 device_switch_onscreen_ds(device
, context
, ds
);
651 if (ds
->locations
& location
)
652 SetRect(¤t_rect
, 0, 0, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
654 SetRectEmpty(¤t_rect
);
656 wined3d_get_draw_rect(state
, &draw_rect
);
658 IntersectRect(&r
, &draw_rect
, ¤t_rect
);
659 if (!EqualRect(&r
, &draw_rect
))
660 surface_load_ds_location(ds
, context
, location
);
664 if (!context_apply_draw_state(context
, device
))
666 context_release(context
);
667 WARN("Unable to apply draw state, skipping draw.\n");
671 if (device
->fb
.depth_stencil
&& state
->render_states
[WINED3D_RS_ZWRITEENABLE
])
673 struct wined3d_surface
*ds
= device
->fb
.depth_stencil
;
674 DWORD location
= context
->render_offscreen
? ds
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
676 surface_modify_ds_location(ds
, location
, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
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
)
685 FIXME("Point sprite coordinate origin switching not supported.\n");
688 stream_info
= &context
->stream_info
;
689 if (context
->instance_count
)
690 instance_count
= context
->instance_count
;
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
.heap_memory
;
699 ib_query
= index_buffer
->query
;
703 if (state
->index_format
== WINED3DFMT_R16_UINT
)
711 if (!stream_info
->position_transformed
&& context
->num_untracked_materials
712 && state
->render_states
[WINED3D_RS_LIGHTING
])
717 FIXME("Using software emulation because not all material properties could be tracked.\n");
719 WARN_(d3d_perf
)("Using software emulation because not all material properties could be tracked.\n");
722 else if (context
->fog_coord
&& state
->render_states
[WINED3D_RS_FOGENABLE
])
726 /* Either write a pipeline replacement shader or convert the
727 * specular alpha from unsigned byte to a float in the vertex
730 FIXME("Using software emulation because manual fog coordinates are provided.\n");
732 WARN_(d3d_perf
)("Using software emulation because manual fog coordinates are provided.\n");
738 si_emulated
= context
->stream_info
;
739 remove_vbos(context
, state
, &si_emulated
);
740 stream_info
= &si_emulated
;
744 if (context
->use_immediate_mode_draw
|| emulation
)
746 /* Immediate mode drawing. */
752 FIXME("Using immediate mode with vertex shaders for half float emulation.\n");
754 WARN_(d3d_perf
)("Using immediate mode with vertex shaders for half float emulation.\n");
756 drawStridedSlowVs(context
, state
, stream_info
, index_count
,
757 state
->gl_primitive_type
, idx_data
, idx_size
, start_idx
);
761 drawStridedSlow(device
, context
, stream_info
, index_count
,
762 state
->gl_primitive_type
, idx_data
, idx_size
, start_idx
);
765 else if (!gl_info
->supported
[ARB_INSTANCED_ARRAYS
] && instance_count
)
767 /* Instancing emulation by mixing immediate mode and arrays. */
768 drawStridedInstanced(context
, state
, stream_info
, index_count
, state
->gl_primitive_type
,
769 idx_data
, idx_size
, start_idx
, state
->base_vertex_index
, instance_count
);
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
);
778 wined3d_event_query_issue(ib_query
, device
);
779 for (i
= 0; i
< context
->num_buffer_queries
; ++i
)
781 wined3d_event_query_issue(context
->buffer_queries
[i
], device
);
784 if (wined3d_settings
.strict_draw_ordering
)
785 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
787 context_release(context
);
789 TRACE("Done all gl drawing\n");