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
);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
39 /* Context activation is done by the caller. */
40 static void draw_primitive_arrays(struct wined3d_context
*context
, const struct wined3d_state
*state
,
41 const void *idx_data
, unsigned int idx_size
, int base_vertex_idx
, unsigned int start_idx
,
42 unsigned int count
, unsigned int start_instance
, unsigned int instance_count
)
44 const struct wined3d_ffp_attrib_ops
*ops
= &context
->d3d_info
->ffp_attrib_ops
;
45 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
46 const struct wined3d_stream_info
*si
= &context
->stream_info
;
47 unsigned int instanced_elements
[ARRAY_SIZE(si
->elements
)];
48 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
49 unsigned int instanced_element_count
= 0;
56 gl_info
->gl_ops
.gl
.p_glDrawArrays(state
->gl_primitive_type
, start_idx
, count
);
57 checkGLcall("glDrawArrays");
61 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
63 GL_EXTCALL(glDrawElementsBaseVertex(state
->gl_primitive_type
, count
, idx_type
,
64 (const char *)idx_data
+ (idx_size
* start_idx
), base_vertex_idx
));
65 checkGLcall("glDrawElementsBaseVertex");
69 gl_info
->gl_ops
.gl
.p_glDrawElements(state
->gl_primitive_type
, count
,
70 idx_type
, (const char *)idx_data
+ (idx_size
* start_idx
));
71 checkGLcall("glDrawElements");
76 FIXME("Start instance (%u) not supported.\n", start_instance
);
78 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
82 GL_EXTCALL(glDrawArraysInstanced(state
->gl_primitive_type
, start_idx
, count
, instance_count
));
83 checkGLcall("glDrawArraysInstanced");
87 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
89 GL_EXTCALL(glDrawElementsInstancedBaseVertex(state
->gl_primitive_type
, count
, idx_type
,
90 (const char *)idx_data
+ (idx_size
* start_idx
), instance_count
, base_vertex_idx
));
91 checkGLcall("glDrawElementsInstancedBaseVertex");
95 GL_EXTCALL(glDrawElementsInstanced(state
->gl_primitive_type
, count
, idx_type
,
96 (const char *)idx_data
+ (idx_size
* start_idx
), instance_count
));
97 checkGLcall("glDrawElementsInstanced");
101 /* Instancing emulation by mixing immediate mode and arrays. */
103 /* This is a nasty thing. MSDN says no hardware supports this and
104 * applications have to use software vertex processing. We don't support
107 * Shouldn't be too hard to support with OpenGL, in theory just call
108 * glDrawArrays() instead of drawElements(). But the stream fequency value
109 * has a different meaning in that situation. */
112 FIXME("Non-indexed instanced drawing is not supported\n");
116 for (i
= 0; i
< ARRAY_SIZE(si
->elements
); ++i
)
118 if (!(si
->use_map
& (1u << i
)))
121 if (state
->streams
[si
->elements
[i
].stream_idx
].flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
122 instanced_elements
[instanced_element_count
++] = i
;
125 for (i
= 0; i
< instance_count
; ++i
)
127 /* Specify the instanced attributes using immediate mode calls. */
128 for (j
= 0; j
< instanced_element_count
; ++j
)
130 const struct wined3d_stream_info_element
*element
;
131 unsigned int element_idx
;
134 element_idx
= instanced_elements
[j
];
135 element
= &si
->elements
[element_idx
];
136 ptr
= element
->data
.addr
+ element
->stride
* i
;
137 if (element
->data
.buffer_object
)
138 ptr
+= (ULONG_PTR
)buffer_get_sysmem(state
->streams
[element
->stream_idx
].buffer
, context
);
139 ops
->generic
[element
->format
->emit_idx
](element_idx
, ptr
);
142 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
144 GL_EXTCALL(glDrawElementsBaseVertex(state
->gl_primitive_type
, count
, idx_type
,
145 (const char *)idx_data
+ (idx_size
* start_idx
), base_vertex_idx
));
146 checkGLcall("glDrawElementsBaseVertex");
150 gl_info
->gl_ops
.gl
.p_glDrawElements(state
->gl_primitive_type
, count
, idx_type
,
151 (const char *)idx_data
+ (idx_size
* start_idx
));
152 checkGLcall("glDrawElements");
157 static unsigned int get_stride_idx(const void *idx_data
, unsigned int idx_size
,
158 unsigned int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_idx
)
161 return start_idx
+ vertex_idx
;
163 return ((const WORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
164 return ((const DWORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
167 /* Context activation is done by the caller. */
168 static void draw_primitive_immediate_mode(struct wined3d_context
*context
, const struct wined3d_state
*state
,
169 const struct wined3d_stream_info
*si
, const void *idx_data
, unsigned int idx_size
,
170 int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_count
, unsigned int instance_count
)
172 const BYTE
*position
= NULL
, *normal
= NULL
, *diffuse
= NULL
, *specular
= NULL
;
173 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
174 unsigned int coord_idx
, stride_idx
, texture_idx
, vertex_idx
;
175 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
176 const struct wined3d_stream_info_element
*element
;
177 const BYTE
*tex_coords
[WINED3DDP_MAXTEXCOORD
];
178 unsigned int texture_unit
, texture_stages
;
179 const struct wined3d_ffp_attrib_ops
*ops
;
180 unsigned int untracked_material_count
;
181 unsigned int tex_mask
= 0;
182 BOOL specular_fog
= FALSE
;
183 BOOL ps
= use_ps(state
);
186 static unsigned int once
;
189 FIXME_(d3d_perf
)("Drawing using immediate mode.\n");
191 WARN_(d3d_perf
)("Drawing using immediate mode.\n");
193 if (!idx_size
&& idx_data
)
194 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
197 FIXME("Instancing not implemented.\n");
199 /* Immediate mode drawing can't make use of indices in a vbo - get the
200 * data from the index buffer. If the index buffer has no vbo (not
201 * supported or other reason), or with user pointer drawing idx_data
202 * will be non-NULL. */
203 if (idx_size
&& !idx_data
)
204 idx_data
= buffer_get_sysmem(state
->index_buffer
, context
);
206 ops
= &d3d_info
->ffp_attrib_ops
;
208 gl_info
->gl_ops
.gl
.p_glBegin(state
->gl_primitive_type
);
210 if (use_vs(state
) || d3d_info
->ffp_generic_attributes
)
212 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
214 unsigned int use_map
= si
->use_map
;
215 unsigned int element_idx
;
217 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
218 for (element_idx
= MAX_ATTRIBS
- 1; use_map
; use_map
&= ~(1u << element_idx
), --element_idx
)
220 if (!(use_map
& 1u << element_idx
))
223 ptr
= si
->elements
[element_idx
].data
.addr
+ si
->elements
[element_idx
].stride
* stride_idx
;
224 ops
->generic
[si
->elements
[element_idx
].format
->emit_idx
](element_idx
, ptr
);
228 gl_info
->gl_ops
.gl
.p_glEnd();
232 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
233 position
= si
->elements
[WINED3D_FFP_POSITION
].data
.addr
;
235 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
236 normal
= si
->elements
[WINED3D_FFP_NORMAL
].data
.addr
;
238 gl_info
->gl_ops
.gl
.p_glNormal3f(0.0f
, 0.0f
, 0.0f
);
240 untracked_material_count
= context
->num_untracked_materials
;
241 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
243 element
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
244 diffuse
= element
->data
.addr
;
246 if (untracked_material_count
&& element
->format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
247 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element
->format
->id
));
251 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
254 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
256 element
= &si
->elements
[WINED3D_FFP_SPECULAR
];
257 specular
= element
->data
.addr
;
259 /* Special case where the fog density is stored in the specular alpha channel. */
260 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
261 && (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
262 || si
->elements
[WINED3D_FFP_POSITION
].format
->id
== WINED3DFMT_R32G32B32A32_FLOAT
)
263 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
265 if (gl_info
->supported
[EXT_FOG_COORD
])
267 if (element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
270 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element
->format
->id
));
274 static unsigned int once
;
277 FIXME("Implement fog for transformed vertices in software.\n");
281 else if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
283 GL_EXTCALL(glSecondaryColor3fEXT
)(0.0f
, 0.0f
, 0.0f
);
286 texture_stages
= d3d_info
->limits
.ffp_blend_stages
;
287 for (texture_idx
= 0; texture_idx
< texture_stages
; ++texture_idx
)
289 if (!gl_info
->supported
[ARB_MULTITEXTURE
] && texture_idx
> 0)
291 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
295 if (!ps
&& !state
->textures
[texture_idx
])
298 texture_unit
= context
->tex_unit_map
[texture_idx
];
299 if (texture_unit
== WINED3D_UNMAPPED_STAGE
)
302 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
305 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx
, texture_idx
);
309 if (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))
311 tex_coords
[coord_idx
] = si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].data
.addr
;
312 tex_mask
|= (1u << texture_idx
);
316 TRACE("Setting default coordinates for texture %u.\n", texture_idx
);
317 if (gl_info
->supported
[ARB_MULTITEXTURE
])
318 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_unit
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
320 gl_info
->gl_ops
.gl
.p_glTexCoord4f(0.0f
, 0.0f
, 0.0f
, 1.0f
);
324 /* Blending data and point sizes are not supported by this function. They
325 * are not supported by the fixed function pipeline at all. A FIXME for
326 * them is printed after decoding the vertex declaration. */
327 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
329 unsigned int tmp_tex_mask
;
331 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
335 ptr
= normal
+ stride_idx
* si
->elements
[WINED3D_FFP_NORMAL
].stride
;
336 ops
->normal
[si
->elements
[WINED3D_FFP_NORMAL
].format
->emit_idx
](ptr
);
341 ptr
= diffuse
+ stride_idx
* si
->elements
[WINED3D_FFP_DIFFUSE
].stride
;
342 ops
->diffuse
[si
->elements
[WINED3D_FFP_DIFFUSE
].format
->emit_idx
](ptr
);
344 if (untracked_material_count
)
346 struct wined3d_color color
;
349 wined3d_color_from_d3dcolor(&color
, *(const DWORD
*)ptr
);
350 for (i
= 0; i
< untracked_material_count
; ++i
)
352 gl_info
->gl_ops
.gl
.p_glMaterialfv(GL_FRONT_AND_BACK
, context
->untracked_materials
[i
], &color
.r
);
359 ptr
= specular
+ stride_idx
* si
->elements
[WINED3D_FFP_SPECULAR
].stride
;
360 ops
->specular
[si
->elements
[WINED3D_FFP_SPECULAR
].format
->emit_idx
](ptr
);
363 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD
*)ptr
>> 24)));
366 tmp_tex_mask
= tex_mask
;
367 for (texture_idx
= 0; tmp_tex_mask
; tmp_tex_mask
>>= 1, ++texture_idx
)
369 if (!(tmp_tex_mask
& 1))
372 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
373 ptr
= tex_coords
[coord_idx
] + (stride_idx
* si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].stride
);
374 ops
->texcoord
[si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].format
->emit_idx
](
375 GL_TEXTURE0_ARB
+ context
->tex_unit_map
[texture_idx
], ptr
);
380 ptr
= position
+ stride_idx
* si
->elements
[WINED3D_FFP_POSITION
].stride
;
381 ops
->position
[si
->elements
[WINED3D_FFP_POSITION
].format
->emit_idx
](ptr
);
385 gl_info
->gl_ops
.gl
.p_glEnd();
386 checkGLcall("glEnd and previous calls");
389 static void remove_vbos(struct wined3d_context
*context
,
390 const struct wined3d_state
*state
, struct wined3d_stream_info
*s
)
394 for (i
= 0; i
< (sizeof(s
->elements
) / sizeof(*s
->elements
)); ++i
)
396 struct wined3d_stream_info_element
*e
;
398 if (!(s
->use_map
& (1u << i
))) continue;
401 if (e
->data
.buffer_object
)
403 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
404 e
->data
.buffer_object
= 0;
405 e
->data
.addr
= (BYTE
*)((ULONG_PTR
)e
->data
.addr
+ (ULONG_PTR
)buffer_get_sysmem(vb
, context
));
410 /* Routine common to the draw primitive and draw indexed primitive routines */
411 void draw_primitive(struct wined3d_device
*device
, const struct wined3d_state
*state
,
412 int base_vertex_idx
, unsigned int start_idx
, unsigned int index_count
,
413 unsigned int start_instance
, unsigned int instance_count
, BOOL indexed
)
415 const struct wined3d_fb_state
*fb
= state
->fb
;
416 const struct wined3d_stream_info
*stream_info
;
417 struct wined3d_event_query
*ib_query
= NULL
;
418 struct wined3d_stream_info si_emulated
;
419 struct wined3d_rendertarget_view
*dsv
;
420 const struct wined3d_gl_info
*gl_info
;
421 struct wined3d_context
*context
;
422 unsigned int i
, idx_size
= 0;
423 const void *idx_data
= NULL
;
424 BOOL emulation
= FALSE
;
429 context
= context_acquire(device
, wined3d_rendertarget_view_get_surface(fb
->render_targets
[0]));
432 context_release(context
);
433 WARN("Invalid context, skipping draw.\n");
436 gl_info
= context
->gl_info
;
438 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
440 struct wined3d_rendertarget_view
*rtv
= fb
->render_targets
[i
];
441 struct wined3d_texture
*rt
;
443 if (!rtv
|| rtv
->format
->id
== WINED3DFMT_NULL
)
446 rt
= wined3d_texture_from_resource(rtv
->resource
);
447 if (state
->render_states
[WINED3D_RS_COLORWRITEENABLE
])
449 wined3d_texture_load_location(rt
, rtv
->sub_resource_idx
, context
, rtv
->resource
->draw_binding
);
450 wined3d_texture_invalidate_location(rt
, rtv
->sub_resource_idx
, ~rtv
->resource
->draw_binding
);
454 wined3d_texture_prepare_location(rt
, rtv
->sub_resource_idx
, context
, rtv
->resource
->draw_binding
);
458 if ((dsv
= fb
->depth_stencil
))
460 /* Note that this depends on the context_acquire() call above to set
461 * context->render_offscreen properly. We don't currently take the
462 * Z-compare function into account, but we could skip loading the
463 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
464 * that we never copy the stencil data.*/
465 DWORD location
= context
->render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
466 struct wined3d_surface
*ds
= wined3d_rendertarget_view_get_surface(dsv
);
468 if (state
->render_states
[WINED3D_RS_ZWRITEENABLE
] || state
->render_states
[WINED3D_RS_ZENABLE
])
470 RECT current_rect
, draw_rect
, r
;
472 if (!context
->render_offscreen
&& ds
!= device
->onscreen_depth_stencil
)
473 device_switch_onscreen_ds(device
, context
, ds
);
475 if (surface_get_sub_resource(ds
)->locations
& location
)
476 SetRect(¤t_rect
, 0, 0, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
478 SetRectEmpty(¤t_rect
);
480 wined3d_get_draw_rect(state
, &draw_rect
);
482 IntersectRect(&r
, &draw_rect
, ¤t_rect
);
483 if (!EqualRect(&r
, &draw_rect
))
484 wined3d_texture_load_location(ds
->container
, dsv
->sub_resource_idx
, context
, location
);
486 wined3d_texture_prepare_location(ds
->container
, dsv
->sub_resource_idx
, context
, location
);
489 wined3d_texture_prepare_location(ds
->container
, dsv
->sub_resource_idx
, context
, location
);
492 if (!context_apply_draw_state(context
, device
, state
))
494 context_release(context
);
495 WARN("Unable to apply draw state, skipping draw.\n");
499 if (fb
->depth_stencil
&& state
->render_states
[WINED3D_RS_ZWRITEENABLE
])
501 struct wined3d_surface
*ds
= wined3d_rendertarget_view_get_surface(fb
->depth_stencil
);
502 DWORD location
= context
->render_offscreen
? ds
->container
->resource
.draw_binding
: WINED3D_LOCATION_DRAWABLE
;
504 surface_modify_ds_location(ds
, location
, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
507 if ((!gl_info
->supported
[WINED3D_GL_VERSION_2_0
]
508 || !gl_info
->supported
[NV_POINT_SPRITE
])
509 && context
->render_offscreen
510 && state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
]
511 && state
->gl_primitive_type
== GL_POINTS
)
513 FIXME("Point sprite coordinate origin switching not supported.\n");
516 stream_info
= &context
->stream_info
;
517 if (context
->instance_count
)
518 instance_count
= context
->instance_count
;
522 struct wined3d_buffer
*index_buffer
= state
->index_buffer
;
523 if (!index_buffer
->buffer_object
|| !stream_info
->all_vbo
)
525 idx_data
= index_buffer
->resource
.heap_memory
;
529 ib_query
= index_buffer
->query
;
532 idx_data
= (const BYTE
*)idx_data
+ state
->index_offset
;
534 if (state
->index_format
== WINED3DFMT_R16_UINT
)
542 if (!stream_info
->position_transformed
&& context
->num_untracked_materials
543 && state
->render_states
[WINED3D_RS_LIGHTING
])
548 FIXME("Using software emulation because not all material properties could be tracked.\n");
550 WARN_(d3d_perf
)("Using software emulation because not all material properties could be tracked.\n");
553 else if (context
->fog_coord
&& state
->render_states
[WINED3D_RS_FOGENABLE
])
557 /* Either write a pipeline replacement shader or convert the
558 * specular alpha from unsigned byte to a float in the vertex
561 FIXME("Using software emulation because manual fog coordinates are provided.\n");
563 WARN_(d3d_perf
)("Using software emulation because manual fog coordinates are provided.\n");
569 si_emulated
= context
->stream_info
;
570 remove_vbos(context
, state
, &si_emulated
);
571 stream_info
= &si_emulated
;
575 if (context
->use_immediate_mode_draw
|| emulation
)
576 draw_primitive_immediate_mode(context
, state
, stream_info
, idx_data
,
577 idx_size
, base_vertex_idx
, start_idx
, index_count
, instance_count
);
579 draw_primitive_arrays(context
, state
, idx_data
, idx_size
, base_vertex_idx
,
580 start_idx
, index_count
, start_instance
, instance_count
);
583 wined3d_event_query_issue(ib_query
, device
);
584 for (i
= 0; i
< context
->num_buffer_queries
; ++i
)
585 wined3d_event_query_issue(context
->buffer_queries
[i
], device
);
587 if (wined3d_settings
.strict_draw_ordering
)
588 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
590 context_release(context
);
592 TRACE("Done all gl drawing.\n");