2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 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"
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
40 /* Define the default light parameters as specified by MSDN. */
41 const struct wined3d_light WINED3D_default_light
=
43 WINED3D_LIGHT_DIRECTIONAL
, /* Type */
44 { 1.0f
, 1.0f
, 1.0f
, 0.0f
}, /* Diffuse r,g,b,a */
45 { 0.0f
, 0.0f
, 0.0f
, 0.0f
}, /* Specular r,g,b,a */
46 { 0.0f
, 0.0f
, 0.0f
, 0.0f
}, /* Ambient r,g,b,a, */
47 { 0.0f
, 0.0f
, 0.0f
}, /* Position x,y,z */
48 { 0.0f
, 0.0f
, 1.0f
}, /* Direction x,y,z */
51 0.0f
, 0.0f
, 0.0f
, /* Attenuation 0,1,2 */
56 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
57 * actually have the same values in GL and D3D. */
58 GLenum
gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
60 switch(primitive_type
)
62 case WINED3D_PT_POINTLIST
:
65 case WINED3D_PT_LINELIST
:
68 case WINED3D_PT_LINESTRIP
:
71 case WINED3D_PT_TRIANGLELIST
:
74 case WINED3D_PT_TRIANGLESTRIP
:
75 return GL_TRIANGLE_STRIP
;
77 case WINED3D_PT_TRIANGLEFAN
:
78 return GL_TRIANGLE_FAN
;
80 case WINED3D_PT_LINELIST_ADJ
:
81 return GL_LINES_ADJACENCY_ARB
;
83 case WINED3D_PT_LINESTRIP_ADJ
:
84 return GL_LINE_STRIP_ADJACENCY_ARB
;
86 case WINED3D_PT_TRIANGLELIST_ADJ
:
87 return GL_TRIANGLES_ADJACENCY_ARB
;
89 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
90 return GL_TRIANGLE_STRIP_ADJACENCY_ARB
;
93 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type
));
98 static enum wined3d_primitive_type
d3d_primitive_type_from_gl(GLenum primitive_type
)
100 switch(primitive_type
)
103 return WINED3D_PT_POINTLIST
;
106 return WINED3D_PT_LINELIST
;
109 return WINED3D_PT_LINESTRIP
;
112 return WINED3D_PT_TRIANGLELIST
;
114 case GL_TRIANGLE_STRIP
:
115 return WINED3D_PT_TRIANGLESTRIP
;
117 case GL_TRIANGLE_FAN
:
118 return WINED3D_PT_TRIANGLEFAN
;
120 case GL_LINES_ADJACENCY_ARB
:
121 return WINED3D_PT_LINELIST_ADJ
;
123 case GL_LINE_STRIP_ADJACENCY_ARB
:
124 return WINED3D_PT_LINESTRIP_ADJ
;
126 case GL_TRIANGLES_ADJACENCY_ARB
:
127 return WINED3D_PT_TRIANGLELIST_ADJ
;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB
:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ
;
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type
));
134 return WINED3D_PT_UNDEFINED
;
138 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
140 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
141 *regnum
= WINED3D_FFP_POSITION
;
142 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
143 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
144 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
145 *regnum
= WINED3D_FFP_BLENDINDICES
;
146 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
147 *regnum
= WINED3D_FFP_NORMAL
;
148 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
149 *regnum
= WINED3D_FFP_PSIZE
;
150 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
151 *regnum
= WINED3D_FFP_DIFFUSE
;
152 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
153 *regnum
= WINED3D_FFP_SPECULAR
;
154 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
155 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
158 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n", debug_d3ddeclusage(usage
), usage_idx
);
166 /* Context activation is done by the caller. */
167 static void device_stream_info_from_declaration(struct wined3d_device
*device
, struct wined3d_stream_info
*stream_info
)
169 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
170 /* We need to deal with frequency data! */
171 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
176 stream_info
->use_map
= 0;
177 stream_info
->swizzle_map
= 0;
178 stream_info
->all_vbo
= 1;
180 /* Check for transformed vertices, disable vertex shader if present. */
181 stream_info
->position_transformed
= declaration
->position_transformed
;
182 use_vshader
= state
->vertex_shader
&& !declaration
->position_transformed
;
184 /* Translate the declaration into strided data. */
185 for (i
= 0; i
< declaration
->element_count
; ++i
)
187 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
188 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
189 struct wined3d_buffer
*buffer
= stream
->buffer
;
190 struct wined3d_bo_address data
;
195 TRACE("%p Element %p (%u of %u)\n", declaration
->elements
,
196 element
, i
+ 1, declaration
->element_count
);
198 if (!buffer
) continue;
200 stride
= stream
->stride
;
202 TRACE("Stream %u, buffer %p.\n", element
->input_slot
, buffer
);
203 buffer_get_memory(buffer
, &device
->adapter
->gl_info
, &data
);
205 /* We can't use VBOs if the base vertex index is negative. OpenGL
206 * doesn't accept negative offsets (or rather offsets bigger than the
207 * VBO, because the pointer is unsigned), so use system memory
208 * sources. In most sane cases the pointer - offset will still be > 0,
209 * otherwise it will wrap around to some big value. Hope that with the
210 * indices, the driver wraps it back internally. If not,
211 * drawStridedSlow() is needed, including a vertex buffer path. */
212 if (state
->load_base_vertex_index
< 0)
214 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n", state
->load_base_vertex_index
);
215 data
.buffer_object
= 0;
216 data
.addr
= buffer_get_sysmem(buffer
, &device
->adapter
->gl_info
);
217 if ((UINT_PTR
)data
.addr
< -state
->load_base_vertex_index
* stride
)
218 FIXME("System memory vertex data load offset is negative!\n");
220 data
.addr
+= element
->offset
;
222 TRACE("offset %u input_slot %u usage_idx %d\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
226 if (element
->output_slot
== ~0U)
228 /* TODO: Assuming vertexdeclarations are usually used with the
229 * same or a similar shader, it might be worth it to store the
230 * last used output slot and try that one first. */
231 stride_used
= vshader_get_input(state
->vertex_shader
,
232 element
->usage
, element
->usage_idx
, &idx
);
236 idx
= element
->output_slot
;
242 if (!element
->ffp_valid
)
244 WARN("Skipping unsupported fixed function element of format %s and usage %s\n",
245 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
250 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
256 TRACE("Load %s array %u [usage %s, usage_idx %u, "
257 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u]\n",
258 use_vshader
? "shader": "fixed function", idx
,
259 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
260 element
->offset
, stride
, debug_d3dformat(element
->format
->id
), data
.buffer_object
);
262 data
.addr
+= stream
->offset
;
264 stream_info
->elements
[idx
].format
= element
->format
;
265 stream_info
->elements
[idx
].data
= data
;
266 stream_info
->elements
[idx
].stride
= stride
;
267 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
269 if (!device
->adapter
->gl_info
.supported
[ARB_VERTEX_ARRAY_BGRA
]
270 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
272 stream_info
->swizzle_map
|= 1 << idx
;
274 stream_info
->use_map
|= 1 << idx
;
278 /* Preload the vertex buffers. */
279 device
->num_buffer_queries
= 0;
280 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
282 struct wined3d_stream_info_element
*element
;
283 struct wined3d_buffer
*buffer
;
288 element
= &stream_info
->elements
[i
];
289 buffer
= state
->streams
[element
->stream_idx
].buffer
;
290 wined3d_buffer_preload(buffer
);
292 /* If the preload dropped the buffer object, update the stream info. */
293 if (buffer
->buffer_object
!= element
->data
.buffer_object
)
295 element
->data
.buffer_object
= 0;
296 element
->data
.addr
= buffer_get_sysmem(buffer
, &device
->adapter
->gl_info
) + (ptrdiff_t)element
->data
.addr
;
299 if (!buffer
->buffer_object
)
300 stream_info
->all_vbo
= 0;
303 device
->buffer_queries
[device
->num_buffer_queries
++] = buffer
->query
;
307 /* Context activation is done by the caller. */
308 void device_update_stream_info(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
)
310 struct wined3d_stream_info
*stream_info
= &device
->stream_info
;
311 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
312 DWORD prev_all_vbo
= stream_info
->all_vbo
;
314 TRACE("============================= Vertex Declaration =============================\n");
315 device_stream_info_from_declaration(device
, stream_info
);
317 if (state
->vertex_shader
&& !stream_info
->position_transformed
)
319 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
321 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
322 device
->useDrawStridedSlow
= TRUE
;
326 device
->useDrawStridedSlow
= FALSE
;
331 WORD slow_mask
= (1 << WINED3D_FFP_PSIZE
);
332 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
333 & ((1 << WINED3D_FFP_DIFFUSE
) | (1 << WINED3D_FFP_SPECULAR
));
335 if (((stream_info
->position_transformed
&& !device
->adapter
->d3d_info
.xyzrhw
)
336 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
337 device
->useDrawStridedSlow
= TRUE
;
339 device
->useDrawStridedSlow
= FALSE
;
342 if (prev_all_vbo
!= stream_info
->all_vbo
)
343 device_invalidate_state(device
, STATE_INDEXBUFFER
);
346 static void device_preload_texture(const struct wined3d_state
*state
, unsigned int idx
)
348 struct wined3d_texture
*texture
;
349 enum WINED3DSRGB srgb
;
351 if (!(texture
= state
->textures
[idx
])) return;
352 srgb
= state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
] ? SRGB_SRGB
: SRGB_RGB
;
353 texture
->texture_ops
->texture_preload(texture
, srgb
);
356 void device_preload_textures(const struct wined3d_device
*device
)
358 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
363 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
365 if (state
->vertex_shader
->reg_maps
.sampler_type
[i
])
366 device_preload_texture(state
, MAX_FRAGMENT_SAMPLERS
+ i
);
372 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
374 if (state
->pixel_shader
->reg_maps
.sampler_type
[i
])
375 device_preload_texture(state
, i
);
380 WORD ffu_map
= device
->fixed_function_usage_map
;
382 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
385 device_preload_texture(state
, i
);
390 BOOL
device_context_add(struct wined3d_device
*device
, struct wined3d_context
*context
)
392 struct wined3d_context
**new_array
;
394 TRACE("Adding context %p.\n", context
);
396 if (!device
->contexts
) new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
397 else new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
,
398 sizeof(*new_array
) * (device
->context_count
+ 1));
402 ERR("Failed to grow the context array.\n");
406 new_array
[device
->context_count
++] = context
;
407 device
->contexts
= new_array
;
411 void device_context_remove(struct wined3d_device
*device
, struct wined3d_context
*context
)
413 struct wined3d_context
**new_array
;
417 TRACE("Removing context %p.\n", context
);
419 for (i
= 0; i
< device
->context_count
; ++i
)
421 if (device
->contexts
[i
] == context
)
430 ERR("Context %p doesn't exist in context array.\n", context
);
434 if (!--device
->context_count
)
436 HeapFree(GetProcessHeap(), 0, device
->contexts
);
437 device
->contexts
= NULL
;
441 memmove(&device
->contexts
[i
], &device
->contexts
[i
+ 1], (device
->context_count
- i
) * sizeof(*device
->contexts
));
442 new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
, device
->context_count
* sizeof(*device
->contexts
));
445 ERR("Failed to shrink context array. Oh well.\n");
449 device
->contexts
= new_array
;
452 /* Do not call while under the GL lock. */
453 void device_switch_onscreen_ds(struct wined3d_device
*device
,
454 struct wined3d_context
*context
, struct wined3d_surface
*depth_stencil
)
456 if (device
->onscreen_depth_stencil
)
458 surface_load_ds_location(device
->onscreen_depth_stencil
, context
, SFLAG_INTEXTURE
);
460 surface_modify_ds_location(device
->onscreen_depth_stencil
, SFLAG_INTEXTURE
,
461 device
->onscreen_depth_stencil
->ds_current_size
.cx
,
462 device
->onscreen_depth_stencil
->ds_current_size
.cy
);
463 wined3d_surface_decref(device
->onscreen_depth_stencil
);
465 device
->onscreen_depth_stencil
= depth_stencil
;
466 wined3d_surface_incref(device
->onscreen_depth_stencil
);
469 static BOOL
is_full_clear(const struct wined3d_surface
*target
, const RECT
*draw_rect
, const RECT
*clear_rect
)
471 /* partial draw rect */
472 if (draw_rect
->left
|| draw_rect
->top
473 || draw_rect
->right
< target
->resource
.width
474 || draw_rect
->bottom
< target
->resource
.height
)
477 /* partial clear rect */
478 if (clear_rect
&& (clear_rect
->left
> 0 || clear_rect
->top
> 0
479 || clear_rect
->right
< target
->resource
.width
480 || clear_rect
->bottom
< target
->resource
.height
))
486 static void prepare_ds_clear(struct wined3d_surface
*ds
, struct wined3d_context
*context
,
487 DWORD location
, const RECT
*draw_rect
, UINT rect_count
, const RECT
*clear_rect
, RECT
*out_rect
)
489 RECT current_rect
, r
;
491 if (ds
->flags
& SFLAG_DISCARDED
)
493 /* Depth buffer was discarded, make it entirely current in its new location since
494 * there is no other place where we would get data anyway. */
495 SetRect(out_rect
, 0, 0, ds
->resource
.width
, ds
->resource
.height
);
499 if (ds
->flags
& location
)
500 SetRect(¤t_rect
, 0, 0,
501 ds
->ds_current_size
.cx
,
502 ds
->ds_current_size
.cy
);
504 SetRectEmpty(¤t_rect
);
506 IntersectRect(&r
, draw_rect
, ¤t_rect
);
507 if (EqualRect(&r
, draw_rect
))
509 /* current_rect ⊇ draw_rect, modify only. */
510 SetRect(out_rect
, 0, 0, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
514 if (EqualRect(&r
, ¤t_rect
))
516 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
520 /* Full clear, modify only. */
521 *out_rect
= *draw_rect
;
525 IntersectRect(&r
, draw_rect
, clear_rect
);
526 if (EqualRect(&r
, draw_rect
))
528 /* clear_rect ⊇ draw_rect, modify only. */
529 *out_rect
= *draw_rect
;
535 surface_load_ds_location(ds
, context
, location
);
536 SetRect(out_rect
, 0, 0, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
539 /* Do not call while under the GL lock. */
540 void device_clear_render_targets(struct wined3d_device
*device
, UINT rt_count
, const struct wined3d_fb_state
*fb
,
541 UINT rect_count
, const RECT
*rects
, const RECT
*draw_rect
, DWORD flags
, const struct wined3d_color
*color
,
542 float depth
, DWORD stencil
)
544 const RECT
*clear_rect
= (rect_count
> 0 && rects
) ? (const RECT
*)rects
: NULL
;
545 struct wined3d_surface
*target
= rt_count
? fb
->render_targets
[0] : NULL
;
546 const struct wined3d_gl_info
*gl_info
;
547 UINT drawable_width
, drawable_height
;
548 struct wined3d_context
*context
;
549 GLbitfield clear_mask
= 0;
550 BOOL render_offscreen
;
554 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
555 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
556 * for the cleared parts, and the untouched parts.
558 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
559 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
560 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
561 * checking all this if the dest surface is in the drawable anyway. */
562 if (flags
& WINED3DCLEAR_TARGET
&& !is_full_clear(target
, draw_rect
, clear_rect
))
564 for (i
= 0; i
< rt_count
; ++i
)
566 struct wined3d_surface
*rt
= fb
->render_targets
[i
];
568 surface_load_location(rt
, rt
->draw_binding
, NULL
);
572 context
= context_acquire(device
, target
);
575 context_release(context
);
576 WARN("Invalid context, skipping clear.\n");
579 gl_info
= context
->gl_info
;
583 render_offscreen
= context
->render_offscreen
;
584 target
->get_drawable_size(context
, &drawable_width
, &drawable_height
);
588 render_offscreen
= TRUE
;
589 drawable_width
= fb
->depth_stencil
->pow2Width
;
590 drawable_height
= fb
->depth_stencil
->pow2Height
;
593 if (flags
& WINED3DCLEAR_ZBUFFER
)
595 DWORD location
= render_offscreen
? fb
->depth_stencil
->draw_binding
: SFLAG_INDRAWABLE
;
597 if (!render_offscreen
&& fb
->depth_stencil
!= device
->onscreen_depth_stencil
)
598 device_switch_onscreen_ds(device
, context
, fb
->depth_stencil
);
599 prepare_ds_clear(fb
->depth_stencil
, context
, location
,
600 draw_rect
, rect_count
, clear_rect
, &ds_rect
);
603 if (!context_apply_clear_state(context
, device
, rt_count
, fb
))
605 context_release(context
);
606 WARN("Failed to apply clear state, skipping clear.\n");
610 /* Only set the values up once, as they are not changing. */
611 if (flags
& WINED3DCLEAR_STENCIL
)
613 if (gl_info
->supported
[EXT_STENCIL_TWO_SIDE
])
615 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT
);
616 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE
));
618 gl_info
->gl_ops
.gl
.p_glStencilMask(~0U);
619 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK
));
620 gl_info
->gl_ops
.gl
.p_glClearStencil(stencil
);
621 checkGLcall("glClearStencil");
622 clear_mask
= clear_mask
| GL_STENCIL_BUFFER_BIT
;
625 if (flags
& WINED3DCLEAR_ZBUFFER
)
627 DWORD location
= render_offscreen
? fb
->depth_stencil
->draw_binding
: SFLAG_INDRAWABLE
;
629 surface_modify_ds_location(fb
->depth_stencil
, location
, ds_rect
.right
, ds_rect
.bottom
);
631 gl_info
->gl_ops
.gl
.p_glDepthMask(GL_TRUE
);
632 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZWRITEENABLE
));
633 gl_info
->gl_ops
.gl
.p_glClearDepth(depth
);
634 checkGLcall("glClearDepth");
635 clear_mask
= clear_mask
| GL_DEPTH_BUFFER_BIT
;
638 if (flags
& WINED3DCLEAR_TARGET
)
640 for (i
= 0; i
< rt_count
; ++i
)
642 struct wined3d_surface
*rt
= fb
->render_targets
[i
];
645 surface_modify_location(rt
, rt
->draw_binding
, TRUE
);
648 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
649 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
650 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
651 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
652 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
653 gl_info
->gl_ops
.gl
.p_glClearColor(color
->r
, color
->g
, color
->b
, color
->a
);
654 checkGLcall("glClearColor");
655 clear_mask
= clear_mask
| GL_COLOR_BUFFER_BIT
;
660 if (render_offscreen
)
662 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, draw_rect
->top
,
663 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
667 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, drawable_height
- draw_rect
->bottom
,
668 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
670 checkGLcall("glScissor");
671 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
672 checkGLcall("glClear");
678 /* Now process each rect in turn. */
679 for (i
= 0; i
< rect_count
; ++i
)
681 /* Note that GL uses lower left, width/height. */
682 IntersectRect(¤t_rect
, draw_rect
, &clear_rect
[i
]);
684 TRACE("clear_rect[%u] %s, current_rect %s.\n", i
,
685 wine_dbgstr_rect(&clear_rect
[i
]),
686 wine_dbgstr_rect(¤t_rect
));
688 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
689 * The rectangle is not cleared, no error is returned, but further rectangles are
690 * still cleared if they are valid. */
691 if (current_rect
.left
> current_rect
.right
|| current_rect
.top
> current_rect
.bottom
)
693 TRACE("Rectangle with negative dimensions, ignoring.\n");
697 if (render_offscreen
)
699 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, current_rect
.top
,
700 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
704 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, drawable_height
- current_rect
.bottom
,
705 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
707 checkGLcall("glScissor");
709 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
710 checkGLcall("glClear");
714 if (wined3d_settings
.strict_draw_ordering
|| (flags
& WINED3DCLEAR_TARGET
715 && target
->swapchain
&& target
->swapchain
->front_buffer
== target
))
716 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
718 context_release(context
);
721 ULONG CDECL
wined3d_device_incref(struct wined3d_device
*device
)
723 ULONG refcount
= InterlockedIncrement(&device
->ref
);
725 TRACE("%p increasing refcount to %u.\n", device
, refcount
);
730 ULONG CDECL
wined3d_device_decref(struct wined3d_device
*device
)
732 ULONG refcount
= InterlockedDecrement(&device
->ref
);
734 TRACE("%p decreasing refcount to %u.\n", device
, refcount
);
738 struct wined3d_stateblock
*stateblock
;
741 if (wined3d_stateblock_decref(device
->updateStateBlock
)
742 && device
->updateStateBlock
!= device
->stateBlock
)
743 FIXME("Something's still holding the update stateblock.\n");
744 device
->updateStateBlock
= NULL
;
746 stateblock
= device
->stateBlock
;
747 device
->stateBlock
= NULL
;
748 if (wined3d_stateblock_decref(stateblock
))
749 FIXME("Something's still holding the stateblock.\n");
751 for (i
= 0; i
< sizeof(device
->multistate_funcs
) / sizeof(device
->multistate_funcs
[0]); ++i
)
753 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
754 device
->multistate_funcs
[i
] = NULL
;
757 if (!list_empty(&device
->resources
))
759 struct wined3d_resource
*resource
;
761 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
763 LIST_FOR_EACH_ENTRY(resource
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
765 FIXME("Leftover resource %p with type %s (%#x).\n",
766 resource
, debug_d3dresourcetype(resource
->type
), resource
->type
);
770 if (device
->contexts
)
771 ERR("Context array not freed!\n");
772 if (device
->hardwareCursor
)
773 DestroyCursor(device
->hardwareCursor
);
774 device
->hardwareCursor
= 0;
776 wined3d_decref(device
->wined3d
);
777 device
->wined3d
= NULL
;
778 HeapFree(GetProcessHeap(), 0, device
);
779 TRACE("Freed device %p.\n", device
);
785 UINT CDECL
wined3d_device_get_swapchain_count(const struct wined3d_device
*device
)
787 TRACE("device %p.\n", device
);
789 return device
->swapchain_count
;
792 struct wined3d_swapchain
* CDECL
wined3d_device_get_swapchain(const struct wined3d_device
*device
, UINT swapchain_idx
)
794 TRACE("device %p, swapchain_idx %u.\n", device
, swapchain_idx
);
796 if (swapchain_idx
>= device
->swapchain_count
)
798 WARN("swapchain_idx %u >= swapchain_count %u.\n",
799 swapchain_idx
, device
->swapchain_count
);
803 return device
->swapchains
[swapchain_idx
];
806 static void device_load_logo(struct wined3d_device
*device
, const char *filename
)
808 struct wined3d_color_key color_key
;
812 HDC dcb
= NULL
, dcs
= NULL
;
814 hbm
= LoadImageA(NULL
, filename
, IMAGE_BITMAP
, 0, 0, LR_LOADFROMFILE
| LR_CREATEDIBSECTION
);
817 GetObjectA(hbm
, sizeof(BITMAP
), &bm
);
818 dcb
= CreateCompatibleDC(NULL
);
820 SelectObject(dcb
, hbm
);
824 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
827 memset(&bm
, 0, sizeof(bm
));
832 hr
= wined3d_surface_create(device
, bm
.bmWidth
, bm
.bmHeight
, WINED3DFMT_B5G6R5_UNORM
, 0,
833 WINED3D_POOL_SYSTEM_MEM
, WINED3D_MULTISAMPLE_NONE
, 0, WINED3D_SURFACE_MAPPABLE
,
834 NULL
, &wined3d_null_parent_ops
, &device
->logo_surface
);
837 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr
);
843 if (FAILED(hr
= wined3d_surface_getdc(device
->logo_surface
, &dcs
)))
845 BitBlt(dcs
, 0, 0, bm
.bmWidth
, bm
.bmHeight
, dcb
, 0, 0, SRCCOPY
);
846 wined3d_surface_releasedc(device
->logo_surface
, dcs
);
848 color_key
.color_space_low_value
= 0;
849 color_key
.color_space_high_value
= 0;
850 wined3d_surface_set_color_key(device
->logo_surface
, WINEDDCKEY_SRCBLT
, &color_key
);
854 const struct wined3d_color c
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
855 /* Fill the surface with a white color to show that wined3d is there */
856 wined3d_device_color_fill(device
, device
->logo_surface
, NULL
, &c
);
860 if (dcb
) DeleteDC(dcb
);
861 if (hbm
) DeleteObject(hbm
);
864 /* Context activation is done by the caller. */
865 static void create_dummy_textures(struct wined3d_device
*device
, struct wined3d_context
*context
)
867 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
868 unsigned int i
, j
, count
;
869 /* Under DirectX you can sample even if no texture is bound, whereas
870 * OpenGL will only allow that when a valid texture is bound.
871 * We emulate this by creating dummy textures and binding them
872 * to each texture stage when the currently set D3D texture is NULL. */
874 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
876 /* The dummy texture does not have client storage backing */
877 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
878 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
881 count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
882 for (i
= 0; i
< count
; ++i
)
884 DWORD color
= 0x000000ff;
886 /* Make appropriate texture active */
887 context_active_texture(context
, gl_info
, i
);
889 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_2d
[i
]);
890 checkGLcall("glGenTextures");
891 TRACE("Dummy 2D texture %u given name %u.\n", i
, device
->dummy_texture_2d
[i
]);
893 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
894 checkGLcall("glBindTexture");
896 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA8
, 1, 1, 0,
897 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
898 checkGLcall("glTexImage2D");
900 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
902 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_rect
[i
]);
903 checkGLcall("glGenTextures");
904 TRACE("Dummy rectangle texture %u given name %u.\n", i
, device
->dummy_texture_rect
[i
]);
906 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
907 checkGLcall("glBindTexture");
909 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA8
, 1, 1, 0,
910 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
911 checkGLcall("glTexImage2D");
914 if (gl_info
->supported
[EXT_TEXTURE3D
])
916 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_3d
[i
]);
917 checkGLcall("glGenTextures");
918 TRACE("Dummy 3D texture %u given name %u.\n", i
, device
->dummy_texture_3d
[i
]);
920 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
921 checkGLcall("glBindTexture");
923 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D
, 0, GL_RGBA8
, 1, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
));
924 checkGLcall("glTexImage3D");
927 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
929 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_cube
[i
]);
930 checkGLcall("glGenTextures");
931 TRACE("Dummy cube texture %u given name %u.\n", i
, device
->dummy_texture_cube
[i
]);
933 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
934 checkGLcall("glBindTexture");
936 for (j
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
; j
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
; ++j
)
938 gl_info
->gl_ops
.gl
.p_glTexImage2D(j
, 0, GL_RGBA8
, 1, 1, 0,
939 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
940 checkGLcall("glTexImage2D");
945 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
947 /* Re-enable because if supported it is enabled by default */
948 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
949 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
953 /* Context activation is done by the caller. */
954 static void destroy_dummy_textures(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
)
956 unsigned int count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
958 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
960 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_cube
);
961 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
964 if (gl_info
->supported
[EXT_TEXTURE3D
])
966 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_3d
);
967 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
970 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
972 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_rect
);
973 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
976 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_2d
);
977 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
979 memset(device
->dummy_texture_cube
, 0, count
* sizeof(*device
->dummy_texture_cube
));
980 memset(device
->dummy_texture_3d
, 0, count
* sizeof(*device
->dummy_texture_3d
));
981 memset(device
->dummy_texture_rect
, 0, count
* sizeof(*device
->dummy_texture_rect
));
982 memset(device
->dummy_texture_2d
, 0, count
* sizeof(*device
->dummy_texture_2d
));
985 static LONG
fullscreen_style(LONG style
)
987 /* Make sure the window is managed, otherwise we won't get keyboard input. */
988 style
|= WS_POPUP
| WS_SYSMENU
;
989 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
994 static LONG
fullscreen_exstyle(LONG exstyle
)
996 /* Filter out window decorations. */
997 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
1002 void CDECL
wined3d_device_setup_fullscreen_window(struct wined3d_device
*device
, HWND window
, UINT w
, UINT h
)
1004 BOOL filter_messages
;
1005 LONG style
, exstyle
;
1007 TRACE("Setting up window %p for fullscreen mode.\n", window
);
1009 if (device
->style
|| device
->exStyle
)
1011 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1012 window
, device
->style
, device
->exStyle
);
1015 device
->style
= GetWindowLongW(window
, GWL_STYLE
);
1016 device
->exStyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
1018 style
= fullscreen_style(device
->style
);
1019 exstyle
= fullscreen_exstyle(device
->exStyle
);
1021 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1022 device
->style
, device
->exStyle
, style
, exstyle
);
1024 filter_messages
= device
->filter_messages
;
1025 device
->filter_messages
= TRUE
;
1027 SetWindowLongW(window
, GWL_STYLE
, style
);
1028 SetWindowLongW(window
, GWL_EXSTYLE
, exstyle
);
1029 SetWindowPos(window
, HWND_TOPMOST
, 0, 0, w
, h
, SWP_FRAMECHANGED
| SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
1031 device
->filter_messages
= filter_messages
;
1034 void CDECL
wined3d_device_restore_fullscreen_window(struct wined3d_device
*device
, HWND window
)
1036 BOOL filter_messages
;
1037 LONG style
, exstyle
;
1039 if (!device
->style
&& !device
->exStyle
) return;
1041 style
= GetWindowLongW(window
, GWL_STYLE
);
1042 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
1044 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
1045 * application, and we want to ignore them in the test below, since it's
1046 * not the application's fault that they changed. Additionally, we want to
1047 * preserve the current status of these flags (i.e. don't restore them) to
1048 * more closely emulate the behavior of Direct3D, which leaves these flags
1049 * alone when returning to windowed mode. */
1050 device
->style
^= (device
->style
^ style
) & WS_VISIBLE
;
1051 device
->exStyle
^= (device
->exStyle
^ exstyle
) & WS_EX_TOPMOST
;
1053 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1054 window
, device
->style
, device
->exStyle
);
1056 filter_messages
= device
->filter_messages
;
1057 device
->filter_messages
= TRUE
;
1059 /* Only restore the style if the application didn't modify it during the
1060 * fullscreen phase. Some applications change it before calling Reset()
1061 * when switching between windowed and fullscreen modes (HL2), some
1062 * depend on the original style (Eve Online). */
1063 if (style
== fullscreen_style(device
->style
) && exstyle
== fullscreen_exstyle(device
->exStyle
))
1065 SetWindowLongW(window
, GWL_STYLE
, device
->style
);
1066 SetWindowLongW(window
, GWL_EXSTYLE
, device
->exStyle
);
1068 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
| SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
1070 device
->filter_messages
= filter_messages
;
1072 /* Delete the old values. */
1074 device
->exStyle
= 0;
1077 HRESULT CDECL
wined3d_device_acquire_focus_window(struct wined3d_device
*device
, HWND window
)
1079 TRACE("device %p, window %p.\n", device
, window
);
1081 if (!wined3d_register_window(window
, device
))
1083 ERR("Failed to register window %p.\n", window
);
1087 InterlockedExchangePointer((void **)&device
->focus_window
, window
);
1088 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
);
1093 void CDECL
wined3d_device_release_focus_window(struct wined3d_device
*device
)
1095 TRACE("device %p.\n", device
);
1097 if (device
->focus_window
) wined3d_unregister_window(device
->focus_window
);
1098 InterlockedExchangePointer((void **)&device
->focus_window
, NULL
);
1101 HRESULT CDECL
wined3d_device_init_3d(struct wined3d_device
*device
,
1102 struct wined3d_swapchain_desc
*swapchain_desc
)
1104 static const struct wined3d_color black
= {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1105 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1106 struct wined3d_swapchain
*swapchain
= NULL
;
1107 struct wined3d_context
*context
;
1111 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1113 if (device
->d3d_initialized
)
1114 return WINED3DERR_INVALIDCALL
;
1115 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1116 return WINED3DERR_INVALIDCALL
;
1118 device
->fb
.render_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1119 sizeof(*device
->fb
.render_targets
) * gl_info
->limits
.buffers
);
1121 /* Initialize the texture unit mapping to a 1:1 mapping */
1122 for (state
= 0; state
< MAX_COMBINED_SAMPLERS
; ++state
)
1124 if (state
< gl_info
->limits
.fragment_samplers
)
1126 device
->texUnitMap
[state
] = state
;
1127 device
->rev_tex_unit_map
[state
] = state
;
1131 device
->texUnitMap
[state
] = WINED3D_UNMAPPED_STAGE
;
1132 device
->rev_tex_unit_map
[state
] = WINED3D_UNMAPPED_STAGE
;
1136 if (FAILED(hr
= device
->shader_backend
->shader_alloc_private(device
,
1137 device
->adapter
->vertex_pipe
, device
->adapter
->fragment_pipe
)))
1139 TRACE("Shader private data couldn't be allocated\n");
1142 if (FAILED(hr
= device
->blitter
->alloc_private(device
)))
1144 TRACE("Blitter private data couldn't be allocated\n");
1148 /* Setup the implicit swapchain. This also initializes a context. */
1149 TRACE("Creating implicit swapchain\n");
1150 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1151 swapchain_desc
, &swapchain
);
1154 WARN("Failed to create implicit swapchain\n");
1158 device
->swapchain_count
= 1;
1159 device
->swapchains
= HeapAlloc(GetProcessHeap(), 0, device
->swapchain_count
* sizeof(*device
->swapchains
));
1160 if (!device
->swapchains
)
1162 ERR("Out of memory!\n");
1165 device
->swapchains
[0] = swapchain
;
1167 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0])
1169 TRACE("Setting rendertarget to %p.\n", swapchain
->back_buffers
);
1170 device
->fb
.render_targets
[0] = swapchain
->back_buffers
[0];
1174 TRACE("Setting rendertarget to %p.\n", swapchain
->front_buffer
);
1175 device
->fb
.render_targets
[0] = swapchain
->front_buffer
;
1177 wined3d_surface_incref(device
->fb
.render_targets
[0]);
1179 /* Depth Stencil support */
1180 device
->fb
.depth_stencil
= device
->auto_depth_stencil
;
1181 if (device
->fb
.depth_stencil
)
1182 wined3d_surface_incref(device
->fb
.depth_stencil
);
1184 /* Set up some starting GL setup */
1186 /* Setup all the devices defaults */
1187 stateblock_init_default_state(device
->stateBlock
);
1189 context
= context_acquire(device
, swapchain
->front_buffer
);
1191 create_dummy_textures(device
, context
);
1193 device
->contexts
[0]->last_was_rhw
= 0;
1195 switch (wined3d_settings
.offscreen_rendering_mode
)
1198 device
->offscreenBuffer
= GL_COLOR_ATTACHMENT0
;
1201 case ORM_BACKBUFFER
:
1203 if (context_get_current()->aux_buffers
> 0)
1205 TRACE("Using auxiliary buffer for offscreen rendering\n");
1206 device
->offscreenBuffer
= GL_AUX0
;
1210 TRACE("Using back buffer for offscreen rendering\n");
1211 device
->offscreenBuffer
= GL_BACK
;
1216 TRACE("All defaults now set up, leaving 3D init.\n");
1218 context_release(context
);
1220 /* Clear the screen */
1221 wined3d_device_clear(device
, 0, NULL
, WINED3DCLEAR_TARGET
1222 | (swapchain_desc
->enable_auto_depth_stencil
? WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
: 0),
1225 device
->d3d_initialized
= TRUE
;
1227 if (wined3d_settings
.logo
)
1228 device_load_logo(device
, wined3d_settings
.logo
);
1232 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1233 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1234 device
->swapchain_count
= 0;
1236 wined3d_swapchain_decref(swapchain
);
1237 if (device
->blit_priv
)
1238 device
->blitter
->free_private(device
);
1239 if (device
->shader_priv
)
1240 device
->shader_backend
->shader_free_private(device
);
1245 HRESULT CDECL
wined3d_device_init_gdi(struct wined3d_device
*device
,
1246 struct wined3d_swapchain_desc
*swapchain_desc
)
1248 struct wined3d_swapchain
*swapchain
= NULL
;
1251 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1253 /* Setup the implicit swapchain */
1254 TRACE("Creating implicit swapchain\n");
1255 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1256 swapchain_desc
, &swapchain
);
1259 WARN("Failed to create implicit swapchain\n");
1263 device
->swapchain_count
= 1;
1264 device
->swapchains
= HeapAlloc(GetProcessHeap(), 0, device
->swapchain_count
* sizeof(*device
->swapchains
));
1265 if (!device
->swapchains
)
1267 ERR("Out of memory!\n");
1270 device
->swapchains
[0] = swapchain
;
1274 wined3d_swapchain_decref(swapchain
);
1278 HRESULT CDECL
wined3d_device_uninit_3d(struct wined3d_device
*device
)
1280 struct wined3d_resource
*resource
, *cursor
;
1281 const struct wined3d_gl_info
*gl_info
;
1282 struct wined3d_context
*context
;
1283 struct wined3d_surface
*surface
;
1286 TRACE("device %p.\n", device
);
1288 if (!device
->d3d_initialized
)
1289 return WINED3DERR_INVALIDCALL
;
1291 /* Force making the context current again, to verify it is still valid
1292 * (workaround for broken drivers) */
1293 context_set_current(NULL
);
1294 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1295 * it was created. Thus make sure a context is active for the glDelete* calls
1297 context
= context_acquire(device
, NULL
);
1298 gl_info
= context
->gl_info
;
1300 if (device
->logo_surface
)
1301 wined3d_surface_decref(device
->logo_surface
);
1303 stateblock_unbind_resources(device
->stateBlock
);
1305 /* Unload resources */
1306 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
1308 TRACE("Unloading resource %p.\n", resource
);
1310 resource
->resource_ops
->resource_unload(resource
);
1313 /* Delete the mouse cursor texture */
1314 if (device
->cursorTexture
)
1316 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->cursorTexture
);
1317 device
->cursorTexture
= 0;
1320 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1321 * private data, it might contain opengl pointers
1323 if (device
->depth_blt_texture
)
1325 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->depth_blt_texture
);
1326 device
->depth_blt_texture
= 0;
1329 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1330 device
->blitter
->free_private(device
);
1331 device
->shader_backend
->shader_free_private(device
);
1333 /* Release the buffers (with sanity checks)*/
1334 if (device
->onscreen_depth_stencil
)
1336 surface
= device
->onscreen_depth_stencil
;
1337 device
->onscreen_depth_stencil
= NULL
;
1338 wined3d_surface_decref(surface
);
1341 if (device
->fb
.depth_stencil
)
1343 surface
= device
->fb
.depth_stencil
;
1345 TRACE("Releasing depth/stencil buffer %p.\n", surface
);
1347 device
->fb
.depth_stencil
= NULL
;
1348 wined3d_surface_decref(surface
);
1351 if (device
->auto_depth_stencil
)
1353 surface
= device
->auto_depth_stencil
;
1354 device
->auto_depth_stencil
= NULL
;
1355 if (wined3d_surface_decref(surface
))
1356 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface
);
1359 for (i
= 1; i
< gl_info
->limits
.buffers
; ++i
)
1361 wined3d_device_set_render_target(device
, i
, NULL
, FALSE
);
1364 surface
= device
->fb
.render_targets
[0];
1365 TRACE("Setting rendertarget 0 to NULL\n");
1366 device
->fb
.render_targets
[0] = NULL
;
1367 TRACE("Releasing the render target at %p\n", surface
);
1368 wined3d_surface_decref(surface
);
1370 context_release(context
);
1372 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1374 TRACE("Releasing the implicit swapchain %u.\n", i
);
1375 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1376 FIXME("Something's still holding the implicit swapchain.\n");
1379 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1380 device
->swapchains
= NULL
;
1381 device
->swapchain_count
= 0;
1383 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1384 device
->fb
.render_targets
= NULL
;
1386 device
->d3d_initialized
= FALSE
;
1391 HRESULT CDECL
wined3d_device_uninit_gdi(struct wined3d_device
*device
)
1395 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1397 TRACE("Releasing the implicit swapchain %u.\n", i
);
1398 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1399 FIXME("Something's still holding the implicit swapchain.\n");
1402 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1403 device
->swapchains
= NULL
;
1404 device
->swapchain_count
= 0;
1408 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1409 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1410 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1412 * There is no way to deactivate thread safety once it is enabled.
1414 void CDECL
wined3d_device_set_multithreaded(struct wined3d_device
*device
)
1416 TRACE("device %p.\n", device
);
1418 /* For now just store the flag (needed in case of ddraw). */
1419 device
->create_parms
.flags
|= WINED3DCREATE_MULTITHREADED
;
1422 UINT CDECL
wined3d_device_get_available_texture_mem(const struct wined3d_device
*device
)
1424 TRACE("device %p.\n", device
);
1426 TRACE("Emulating %d MB, returning %d MB left.\n",
1427 device
->adapter
->TextureRam
/ (1024 * 1024),
1428 (device
->adapter
->TextureRam
- device
->adapter
->UsedTextureRam
) / (1024 * 1024));
1430 return device
->adapter
->TextureRam
- device
->adapter
->UsedTextureRam
;
1433 void CDECL
wined3d_device_set_stream_output(struct wined3d_device
*device
, UINT idx
,
1434 struct wined3d_buffer
*buffer
, UINT offset
)
1436 struct wined3d_buffer
*prev_buffer
;
1438 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device
, idx
, buffer
, offset
);
1440 if (idx
>= MAX_STREAM_OUT
)
1442 WARN("Invalid stream output %u.\n", idx
);
1446 prev_buffer
= device
->updateStateBlock
->state
.stream_output
[idx
].buffer
;
1447 device
->updateStateBlock
->state
.stream_output
[idx
].buffer
= buffer
;
1448 device
->updateStateBlock
->state
.stream_output
[idx
].offset
= offset
;
1450 if (device
->isRecordingState
)
1453 wined3d_buffer_incref(buffer
);
1455 wined3d_buffer_decref(prev_buffer
);
1459 if (prev_buffer
!= buffer
)
1463 InterlockedIncrement(&buffer
->resource
.bind_count
);
1464 wined3d_buffer_incref(buffer
);
1468 InterlockedDecrement(&prev_buffer
->resource
.bind_count
);
1469 wined3d_buffer_decref(prev_buffer
);
1474 struct wined3d_buffer
* CDECL
wined3d_device_get_stream_output(struct wined3d_device
*device
,
1475 UINT idx
, UINT
*offset
)
1477 TRACE("device %p, idx %u, offset %p.\n", device
, idx
, offset
);
1479 if (idx
>= MAX_STREAM_OUT
)
1481 WARN("Invalid stream output %u.\n", idx
);
1485 *offset
= device
->stateBlock
->state
.stream_output
[idx
].offset
;
1486 return device
->stateBlock
->state
.stream_output
[idx
].buffer
;
1489 HRESULT CDECL
wined3d_device_set_stream_source(struct wined3d_device
*device
, UINT stream_idx
,
1490 struct wined3d_buffer
*buffer
, UINT offset
, UINT stride
)
1492 struct wined3d_stream_state
*stream
;
1493 struct wined3d_buffer
*prev_buffer
;
1495 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1496 device
, stream_idx
, buffer
, offset
, stride
);
1498 if (stream_idx
>= MAX_STREAMS
)
1500 WARN("Stream index %u out of range.\n", stream_idx
);
1501 return WINED3DERR_INVALIDCALL
;
1503 else if (offset
& 0x3)
1505 WARN("Offset %u is not 4 byte aligned.\n", offset
);
1506 return WINED3DERR_INVALIDCALL
;
1509 stream
= &device
->updateStateBlock
->state
.streams
[stream_idx
];
1510 prev_buffer
= stream
->buffer
;
1512 device
->updateStateBlock
->changed
.streamSource
|= 1 << stream_idx
;
1514 if (prev_buffer
== buffer
1515 && stream
->stride
== stride
1516 && stream
->offset
== offset
)
1518 TRACE("Application is setting the old values over, nothing to do.\n");
1522 stream
->buffer
= buffer
;
1525 stream
->stride
= stride
;
1526 stream
->offset
= offset
;
1529 /* Handle recording of state blocks. */
1530 if (device
->isRecordingState
)
1532 TRACE("Recording... not performing anything.\n");
1534 wined3d_buffer_incref(buffer
);
1536 wined3d_buffer_decref(prev_buffer
);
1542 InterlockedIncrement(&buffer
->resource
.bind_count
);
1543 wined3d_buffer_incref(buffer
);
1547 InterlockedDecrement(&prev_buffer
->resource
.bind_count
);
1548 wined3d_buffer_decref(prev_buffer
);
1551 device_invalidate_state(device
, STATE_STREAMSRC
);
1556 HRESULT CDECL
wined3d_device_get_stream_source(const struct wined3d_device
*device
,
1557 UINT stream_idx
, struct wined3d_buffer
**buffer
, UINT
*offset
, UINT
*stride
)
1559 struct wined3d_stream_state
*stream
;
1561 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1562 device
, stream_idx
, buffer
, offset
, stride
);
1564 if (stream_idx
>= MAX_STREAMS
)
1566 WARN("Stream index %u out of range.\n", stream_idx
);
1567 return WINED3DERR_INVALIDCALL
;
1570 stream
= &device
->stateBlock
->state
.streams
[stream_idx
];
1571 *buffer
= stream
->buffer
;
1573 wined3d_buffer_incref(*buffer
);
1575 *offset
= stream
->offset
;
1576 *stride
= stream
->stride
;
1581 HRESULT CDECL
wined3d_device_set_stream_source_freq(struct wined3d_device
*device
, UINT stream_idx
, UINT divider
)
1583 struct wined3d_stream_state
*stream
;
1584 UINT old_flags
, old_freq
;
1586 TRACE("device %p, stream_idx %u, divider %#x.\n", device
, stream_idx
, divider
);
1588 /* Verify input. At least in d3d9 this is invalid. */
1589 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && (divider
& WINED3DSTREAMSOURCE_INDEXEDDATA
))
1591 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1592 return WINED3DERR_INVALIDCALL
;
1594 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && !stream_idx
)
1596 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1597 return WINED3DERR_INVALIDCALL
;
1601 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1602 return WINED3DERR_INVALIDCALL
;
1605 stream
= &device
->updateStateBlock
->state
.streams
[stream_idx
];
1606 old_flags
= stream
->flags
;
1607 old_freq
= stream
->frequency
;
1609 stream
->flags
= divider
& (WINED3DSTREAMSOURCE_INSTANCEDATA
| WINED3DSTREAMSOURCE_INDEXEDDATA
);
1610 stream
->frequency
= divider
& 0x7fffff;
1612 device
->updateStateBlock
->changed
.streamFreq
|= 1 << stream_idx
;
1614 if (stream
->frequency
!= old_freq
|| stream
->flags
!= old_flags
)
1615 device_invalidate_state(device
, STATE_STREAMSRC
);
1620 HRESULT CDECL
wined3d_device_get_stream_source_freq(const struct wined3d_device
*device
,
1621 UINT stream_idx
, UINT
*divider
)
1623 struct wined3d_stream_state
*stream
;
1625 TRACE("device %p, stream_idx %u, divider %p.\n", device
, stream_idx
, divider
);
1627 stream
= &device
->updateStateBlock
->state
.streams
[stream_idx
];
1628 *divider
= stream
->flags
| stream
->frequency
;
1630 TRACE("Returning %#x.\n", *divider
);
1635 void CDECL
wined3d_device_set_transform(struct wined3d_device
*device
,
1636 enum wined3d_transform_state d3dts
, const struct wined3d_matrix
*matrix
)
1638 TRACE("device %p, state %s, matrix %p.\n",
1639 device
, debug_d3dtstype(d3dts
), matrix
);
1640 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._11
, matrix
->u
.s
._12
, matrix
->u
.s
._13
, matrix
->u
.s
._14
);
1641 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._21
, matrix
->u
.s
._22
, matrix
->u
.s
._23
, matrix
->u
.s
._24
);
1642 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._31
, matrix
->u
.s
._32
, matrix
->u
.s
._33
, matrix
->u
.s
._34
);
1643 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._41
, matrix
->u
.s
._42
, matrix
->u
.s
._43
, matrix
->u
.s
._44
);
1645 /* Handle recording of state blocks. */
1646 if (device
->isRecordingState
)
1648 TRACE("Recording... not performing anything.\n");
1649 device
->updateStateBlock
->changed
.transform
[d3dts
>> 5] |= 1 << (d3dts
& 0x1f);
1650 device
->updateStateBlock
->state
.transforms
[d3dts
] = *matrix
;
1654 /* If the new matrix is the same as the current one,
1655 * we cut off any further processing. this seems to be a reasonable
1656 * optimization because as was noticed, some apps (warcraft3 for example)
1657 * tend towards setting the same matrix repeatedly for some reason.
1659 * From here on we assume that the new matrix is different, wherever it matters. */
1660 if (!memcmp(&device
->stateBlock
->state
.transforms
[d3dts
].u
.m
[0][0], matrix
, sizeof(*matrix
)))
1662 TRACE("The application is setting the same matrix over again.\n");
1666 device
->stateBlock
->state
.transforms
[d3dts
] = *matrix
;
1668 if (d3dts
< WINED3D_TS_WORLD_MATRIX(device
->adapter
->gl_info
.limits
.blends
))
1669 device_invalidate_state(device
, STATE_TRANSFORM(d3dts
));
1672 void CDECL
wined3d_device_get_transform(const struct wined3d_device
*device
,
1673 enum wined3d_transform_state state
, struct wined3d_matrix
*matrix
)
1675 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1677 *matrix
= device
->stateBlock
->state
.transforms
[state
];
1680 void CDECL
wined3d_device_multiply_transform(struct wined3d_device
*device
,
1681 enum wined3d_transform_state state
, const struct wined3d_matrix
*matrix
)
1683 const struct wined3d_matrix
*mat
;
1684 struct wined3d_matrix temp
;
1686 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1688 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1689 * below means it will be recorded in a state block change, but it
1690 * works regardless where it is recorded.
1691 * If this is found to be wrong, change to StateBlock. */
1692 if (state
> HIGHEST_TRANSFORMSTATE
)
1694 WARN("Unhandled transform state %#x.\n", state
);
1698 mat
= &device
->updateStateBlock
->state
.transforms
[state
];
1699 multiply_matrix(&temp
, mat
, matrix
);
1701 /* Apply change via set transform - will reapply to eg. lights this way. */
1702 wined3d_device_set_transform(device
, state
, &temp
);
1705 /* Note lights are real special cases. Although the device caps state only
1706 * e.g. 8 are supported, you can reference any indexes you want as long as
1707 * that number max are enabled at any one point in time. Therefore since the
1708 * indices can be anything, we need a hashmap of them. However, this causes
1709 * stateblock problems. When capturing the state block, I duplicate the
1710 * hashmap, but when recording, just build a chain pretty much of commands to
1712 HRESULT CDECL
wined3d_device_set_light(struct wined3d_device
*device
,
1713 UINT light_idx
, const struct wined3d_light
*light
)
1715 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1716 struct wined3d_light_info
*object
= NULL
;
1720 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1722 /* Check the parameter range. Need for speed most wanted sets junk lights
1723 * which confuse the GL driver. */
1725 return WINED3DERR_INVALIDCALL
;
1727 switch (light
->type
)
1729 case WINED3D_LIGHT_POINT
:
1730 case WINED3D_LIGHT_SPOT
:
1731 case WINED3D_LIGHT_PARALLELPOINT
:
1732 case WINED3D_LIGHT_GLSPOT
:
1733 /* Incorrect attenuation values can cause the gl driver to crash.
1734 * Happens with Need for speed most wanted. */
1735 if (light
->attenuation0
< 0.0f
|| light
->attenuation1
< 0.0f
|| light
->attenuation2
< 0.0f
)
1737 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1738 return WINED3DERR_INVALIDCALL
;
1742 case WINED3D_LIGHT_DIRECTIONAL
:
1743 /* Ignores attenuation */
1747 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1748 return WINED3DERR_INVALIDCALL
;
1751 LIST_FOR_EACH(e
, &device
->updateStateBlock
->state
.light_map
[hash_idx
])
1753 object
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1754 if (object
->OriginalIndex
== light_idx
)
1761 TRACE("Adding new light\n");
1762 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1764 return E_OUTOFMEMORY
;
1766 list_add_head(&device
->updateStateBlock
->state
.light_map
[hash_idx
], &object
->entry
);
1767 object
->glIndex
= -1;
1768 object
->OriginalIndex
= light_idx
;
1771 /* Initialize the object. */
1772 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1773 light_idx
, light
->type
,
1774 light
->diffuse
.r
, light
->diffuse
.g
, light
->diffuse
.b
, light
->diffuse
.a
,
1775 light
->specular
.r
, light
->specular
.g
, light
->specular
.b
, light
->specular
.a
,
1776 light
->ambient
.r
, light
->ambient
.g
, light
->ambient
.b
, light
->ambient
.a
);
1777 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light
->position
.x
, light
->position
.y
, light
->position
.z
,
1778 light
->direction
.x
, light
->direction
.y
, light
->direction
.z
);
1779 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1780 light
->range
, light
->falloff
, light
->theta
, light
->phi
);
1782 /* Update the live definitions if the light is currently assigned a glIndex. */
1783 if (object
->glIndex
!= -1 && !device
->isRecordingState
)
1785 if (object
->OriginalParms
.type
!= light
->type
)
1786 device_invalidate_state(device
, STATE_LIGHT_TYPE
);
1787 device_invalidate_state(device
, STATE_ACTIVELIGHT(object
->glIndex
));
1790 /* Save away the information. */
1791 object
->OriginalParms
= *light
;
1793 switch (light
->type
)
1795 case WINED3D_LIGHT_POINT
:
1797 object
->lightPosn
[0] = light
->position
.x
;
1798 object
->lightPosn
[1] = light
->position
.y
;
1799 object
->lightPosn
[2] = light
->position
.z
;
1800 object
->lightPosn
[3] = 1.0f
;
1801 object
->cutoff
= 180.0f
;
1805 case WINED3D_LIGHT_DIRECTIONAL
:
1807 object
->lightPosn
[0] = -light
->direction
.x
;
1808 object
->lightPosn
[1] = -light
->direction
.y
;
1809 object
->lightPosn
[2] = -light
->direction
.z
;
1810 object
->lightPosn
[3] = 0.0f
;
1811 object
->exponent
= 0.0f
;
1812 object
->cutoff
= 180.0f
;
1815 case WINED3D_LIGHT_SPOT
:
1817 object
->lightPosn
[0] = light
->position
.x
;
1818 object
->lightPosn
[1] = light
->position
.y
;
1819 object
->lightPosn
[2] = light
->position
.z
;
1820 object
->lightPosn
[3] = 1.0f
;
1823 object
->lightDirn
[0] = light
->direction
.x
;
1824 object
->lightDirn
[1] = light
->direction
.y
;
1825 object
->lightDirn
[2] = light
->direction
.z
;
1826 object
->lightDirn
[3] = 1.0f
;
1828 /* opengl-ish and d3d-ish spot lights use too different models
1829 * for the light "intensity" as a function of the angle towards
1830 * the main light direction, so we only can approximate very
1831 * roughly. However, spot lights are rather rarely used in games
1832 * (if ever used at all). Furthermore if still used, probably
1833 * nobody pays attention to such details. */
1834 if (!light
->falloff
)
1836 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1837 * equations have the falloff resp. exponent parameter as an
1838 * exponent, so the spot light lighting will always be 1.0 for
1839 * both of them, and we don't have to care for the rest of the
1840 * rather complex calculation. */
1841 object
->exponent
= 0.0f
;
1845 rho
= light
->theta
+ (light
->phi
- light
->theta
) / (2 * light
->falloff
);
1848 object
->exponent
= -0.3f
/ logf(cosf(rho
/ 2));
1851 if (object
->exponent
> 128.0f
)
1852 object
->exponent
= 128.0f
;
1854 object
->cutoff
= (float)(light
->phi
* 90 / M_PI
);
1859 FIXME("Unrecognized light type %#x.\n", light
->type
);
1865 HRESULT CDECL
wined3d_device_get_light(const struct wined3d_device
*device
,
1866 UINT light_idx
, struct wined3d_light
*light
)
1868 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1869 struct wined3d_light_info
*light_info
= NULL
;
1872 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1874 LIST_FOR_EACH(e
, &device
->stateBlock
->state
.light_map
[hash_idx
])
1876 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1877 if (light_info
->OriginalIndex
== light_idx
)
1884 TRACE("Light information requested but light not defined\n");
1885 return WINED3DERR_INVALIDCALL
;
1888 *light
= light_info
->OriginalParms
;
1892 HRESULT CDECL
wined3d_device_set_light_enable(struct wined3d_device
*device
, UINT light_idx
, BOOL enable
)
1894 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1895 struct wined3d_light_info
*light_info
= NULL
;
1898 TRACE("device %p, light_idx %u, enable %#x.\n", device
, light_idx
, enable
);
1900 LIST_FOR_EACH(e
, &device
->updateStateBlock
->state
.light_map
[hash_idx
])
1902 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1903 if (light_info
->OriginalIndex
== light_idx
)
1907 TRACE("Found light %p.\n", light_info
);
1909 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1912 TRACE("Light enabled requested but light not defined, so defining one!\n");
1913 wined3d_device_set_light(device
, light_idx
, &WINED3D_default_light
);
1915 /* Search for it again! Should be fairly quick as near head of list. */
1916 LIST_FOR_EACH(e
, &device
->updateStateBlock
->state
.light_map
[hash_idx
])
1918 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1919 if (light_info
->OriginalIndex
== light_idx
)
1925 FIXME("Adding default lights has failed dismally\n");
1926 return WINED3DERR_INVALIDCALL
;
1932 if (light_info
->glIndex
!= -1)
1934 if (!device
->isRecordingState
)
1936 device_invalidate_state(device
, STATE_LIGHT_TYPE
);
1937 device_invalidate_state(device
, STATE_ACTIVELIGHT(light_info
->glIndex
));
1940 device
->updateStateBlock
->state
.lights
[light_info
->glIndex
] = NULL
;
1941 light_info
->glIndex
= -1;
1945 TRACE("Light already disabled, nothing to do\n");
1947 light_info
->enabled
= FALSE
;
1951 light_info
->enabled
= TRUE
;
1952 if (light_info
->glIndex
!= -1)
1954 TRACE("Nothing to do as light was enabled\n");
1959 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1960 /* Find a free GL light. */
1961 for (i
= 0; i
< gl_info
->limits
.lights
; ++i
)
1963 if (!device
->updateStateBlock
->state
.lights
[i
])
1965 device
->updateStateBlock
->state
.lights
[i
] = light_info
;
1966 light_info
->glIndex
= i
;
1970 if (light_info
->glIndex
== -1)
1972 /* Our tests show that Windows returns D3D_OK in this situation, even with
1973 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1974 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1975 * as well for those lights.
1977 * TODO: Test how this affects rendering. */
1978 WARN("Too many concurrently active lights\n");
1982 /* i == light_info->glIndex */
1983 if (!device
->isRecordingState
)
1985 device_invalidate_state(device
, STATE_LIGHT_TYPE
);
1986 device_invalidate_state(device
, STATE_ACTIVELIGHT(i
));
1994 HRESULT CDECL
wined3d_device_get_light_enable(const struct wined3d_device
*device
, UINT light_idx
, BOOL
*enable
)
1996 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1997 struct wined3d_light_info
*light_info
= NULL
;
2000 TRACE("device %p, light_idx %u, enable %p.\n", device
, light_idx
, enable
);
2002 LIST_FOR_EACH(e
, &device
->stateBlock
->state
.light_map
[hash_idx
])
2004 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
2005 if (light_info
->OriginalIndex
== light_idx
)
2012 TRACE("Light enabled state requested but light not defined.\n");
2013 return WINED3DERR_INVALIDCALL
;
2015 /* true is 128 according to SetLightEnable */
2016 *enable
= light_info
->enabled
? 128 : 0;
2020 HRESULT CDECL
wined3d_device_set_clip_plane(struct wined3d_device
*device
,
2021 UINT plane_idx
, const struct wined3d_vec4
*plane
)
2023 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
2025 /* Validate plane_idx. */
2026 if (plane_idx
>= device
->adapter
->gl_info
.limits
.clipplanes
)
2028 TRACE("Application has requested clipplane this device doesn't support.\n");
2029 return WINED3DERR_INVALIDCALL
;
2032 device
->updateStateBlock
->changed
.clipplane
|= 1 << plane_idx
;
2034 if (!memcmp(&device
->updateStateBlock
->state
.clip_planes
[plane_idx
], plane
, sizeof(*plane
)))
2036 TRACE("Application is setting old values over, nothing to do.\n");
2040 device
->updateStateBlock
->state
.clip_planes
[plane_idx
] = *plane
;
2042 /* Handle recording of state blocks. */
2043 if (device
->isRecordingState
)
2045 TRACE("Recording... not performing anything.\n");
2049 device_invalidate_state(device
, STATE_CLIPPLANE(plane_idx
));
2054 HRESULT CDECL
wined3d_device_get_clip_plane(const struct wined3d_device
*device
,
2055 UINT plane_idx
, struct wined3d_vec4
*plane
)
2057 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
2059 /* Validate plane_idx. */
2060 if (plane_idx
>= device
->adapter
->gl_info
.limits
.clipplanes
)
2062 TRACE("Application has requested clipplane this device doesn't support.\n");
2063 return WINED3DERR_INVALIDCALL
;
2066 *plane
= device
->stateBlock
->state
.clip_planes
[plane_idx
];
2071 HRESULT CDECL
wined3d_device_set_clip_status(struct wined3d_device
*device
,
2072 const struct wined3d_clip_status
*clip_status
)
2074 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
2077 return WINED3DERR_INVALIDCALL
;
2082 HRESULT CDECL
wined3d_device_get_clip_status(const struct wined3d_device
*device
,
2083 struct wined3d_clip_status
*clip_status
)
2085 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
2088 return WINED3DERR_INVALIDCALL
;
2093 void CDECL
wined3d_device_set_material(struct wined3d_device
*device
, const struct wined3d_material
*material
)
2095 TRACE("device %p, material %p.\n", device
, material
);
2097 device
->updateStateBlock
->changed
.material
= TRUE
;
2098 device
->updateStateBlock
->state
.material
= *material
;
2100 /* Handle recording of state blocks */
2101 if (device
->isRecordingState
)
2103 TRACE("Recording... not performing anything.\n");
2107 device_invalidate_state(device
, STATE_MATERIAL
);
2110 void CDECL
wined3d_device_get_material(const struct wined3d_device
*device
, struct wined3d_material
*material
)
2112 TRACE("device %p, material %p.\n", device
, material
);
2114 *material
= device
->updateStateBlock
->state
.material
;
2116 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2117 material
->diffuse
.r
, material
->diffuse
.g
,
2118 material
->diffuse
.b
, material
->diffuse
.a
);
2119 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2120 material
->ambient
.r
, material
->ambient
.g
,
2121 material
->ambient
.b
, material
->ambient
.a
);
2122 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2123 material
->specular
.r
, material
->specular
.g
,
2124 material
->specular
.b
, material
->specular
.a
);
2125 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2126 material
->emissive
.r
, material
->emissive
.g
,
2127 material
->emissive
.b
, material
->emissive
.a
);
2128 TRACE("power %.8e.\n", material
->power
);
2131 void CDECL
wined3d_device_set_index_buffer(struct wined3d_device
*device
,
2132 struct wined3d_buffer
*buffer
, enum wined3d_format_id format_id
)
2134 struct wined3d_buffer
*prev_buffer
;
2136 TRACE("device %p, buffer %p, format %s.\n",
2137 device
, buffer
, debug_d3dformat(format_id
));
2139 prev_buffer
= device
->updateStateBlock
->state
.index_buffer
;
2141 device
->updateStateBlock
->changed
.indices
= TRUE
;
2142 device
->updateStateBlock
->state
.index_buffer
= buffer
;
2143 device
->updateStateBlock
->state
.index_format
= format_id
;
2145 /* Handle recording of state blocks. */
2146 if (device
->isRecordingState
)
2148 TRACE("Recording... not performing anything.\n");
2150 wined3d_buffer_incref(buffer
);
2152 wined3d_buffer_decref(prev_buffer
);
2156 if (prev_buffer
!= buffer
)
2158 device_invalidate_state(device
, STATE_INDEXBUFFER
);
2161 InterlockedIncrement(&buffer
->resource
.bind_count
);
2162 wined3d_buffer_incref(buffer
);
2166 InterlockedDecrement(&prev_buffer
->resource
.bind_count
);
2167 wined3d_buffer_decref(prev_buffer
);
2172 struct wined3d_buffer
* CDECL
wined3d_device_get_index_buffer(const struct wined3d_device
*device
,
2173 enum wined3d_format_id
*format
)
2175 TRACE("device %p, format %p.\n", device
, format
);
2177 *format
= device
->stateBlock
->state
.index_format
;
2178 return device
->stateBlock
->state
.index_buffer
;
2181 void CDECL
wined3d_device_set_base_vertex_index(struct wined3d_device
*device
, INT base_index
)
2183 TRACE("device %p, base_index %d.\n", device
, base_index
);
2185 device
->updateStateBlock
->state
.base_vertex_index
= base_index
;
2188 INT CDECL
wined3d_device_get_base_vertex_index(const struct wined3d_device
*device
)
2190 TRACE("device %p.\n", device
);
2192 return device
->stateBlock
->state
.base_vertex_index
;
2195 void CDECL
wined3d_device_set_viewport(struct wined3d_device
*device
, const struct wined3d_viewport
*viewport
)
2197 TRACE("device %p, viewport %p.\n", device
, viewport
);
2198 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2199 viewport
->x
, viewport
->y
, viewport
->width
, viewport
->height
, viewport
->min_z
, viewport
->max_z
);
2201 device
->updateStateBlock
->changed
.viewport
= TRUE
;
2202 device
->updateStateBlock
->state
.viewport
= *viewport
;
2204 /* Handle recording of state blocks */
2205 if (device
->isRecordingState
)
2207 TRACE("Recording... not performing anything\n");
2211 device_invalidate_state(device
, STATE_VIEWPORT
);
2214 void CDECL
wined3d_device_get_viewport(const struct wined3d_device
*device
, struct wined3d_viewport
*viewport
)
2216 TRACE("device %p, viewport %p.\n", device
, viewport
);
2218 *viewport
= device
->stateBlock
->state
.viewport
;
2221 static void resolve_depth_buffer(struct wined3d_state
*state
)
2223 struct wined3d_texture
*texture
= state
->textures
[0];
2224 struct wined3d_surface
*depth_stencil
, *surface
;
2226 if (!texture
|| texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE
2227 || !(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_DEPTH
))
2229 surface
= surface_from_resource(texture
->sub_resources
[0]);
2230 depth_stencil
= state
->fb
->depth_stencil
;
2234 wined3d_surface_blt(surface
, NULL
, depth_stencil
, NULL
, 0, NULL
, WINED3D_TEXF_POINT
);
2237 void CDECL
wined3d_device_set_render_state(struct wined3d_device
*device
,
2238 enum wined3d_render_state state
, DWORD value
)
2240 DWORD old_value
= device
->stateBlock
->state
.render_states
[state
];
2242 TRACE("device %p, state %s (%#x), value %#x.\n", device
, debug_d3drenderstate(state
), state
, value
);
2244 device
->updateStateBlock
->changed
.renderState
[state
>> 5] |= 1 << (state
& 0x1f);
2245 device
->updateStateBlock
->state
.render_states
[state
] = value
;
2247 /* Handle recording of state blocks. */
2248 if (device
->isRecordingState
)
2250 TRACE("Recording... not performing anything.\n");
2254 /* Compared here and not before the assignment to allow proper stateblock recording. */
2255 if (value
== old_value
)
2256 TRACE("Application is setting the old value over, nothing to do.\n");
2258 device_invalidate_state(device
, STATE_RENDER(state
));
2260 if (state
== WINED3D_RS_POINTSIZE
&& value
== WINED3D_RESZ_CODE
)
2262 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2263 resolve_depth_buffer(&device
->stateBlock
->state
);
2267 DWORD CDECL
wined3d_device_get_render_state(const struct wined3d_device
*device
, enum wined3d_render_state state
)
2269 TRACE("device %p, state %s (%#x).\n", device
, debug_d3drenderstate(state
), state
);
2271 return device
->stateBlock
->state
.render_states
[state
];
2274 void CDECL
wined3d_device_set_sampler_state(struct wined3d_device
*device
,
2275 UINT sampler_idx
, enum wined3d_sampler_state state
, DWORD value
)
2279 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2280 device
, sampler_idx
, debug_d3dsamplerstate(state
), value
);
2282 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2283 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2285 if (sampler_idx
>= sizeof(device
->stateBlock
->state
.sampler_states
)
2286 / sizeof(*device
->stateBlock
->state
.sampler_states
))
2288 WARN("Invalid sampler %u.\n", sampler_idx
);
2289 return; /* Windows accepts overflowing this array ... we do not. */
2292 old_value
= device
->stateBlock
->state
.sampler_states
[sampler_idx
][state
];
2293 device
->updateStateBlock
->state
.sampler_states
[sampler_idx
][state
] = value
;
2294 device
->updateStateBlock
->changed
.samplerState
[sampler_idx
] |= 1 << state
;
2296 /* Handle recording of state blocks. */
2297 if (device
->isRecordingState
)
2299 TRACE("Recording... not performing anything.\n");
2303 if (old_value
== value
)
2305 TRACE("Application is setting the old value over, nothing to do.\n");
2309 device_invalidate_state(device
, STATE_SAMPLER(sampler_idx
));
2312 DWORD CDECL
wined3d_device_get_sampler_state(const struct wined3d_device
*device
,
2313 UINT sampler_idx
, enum wined3d_sampler_state state
)
2315 TRACE("device %p, sampler_idx %u, state %s.\n",
2316 device
, sampler_idx
, debug_d3dsamplerstate(state
));
2318 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2319 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2321 if (sampler_idx
>= sizeof(device
->stateBlock
->state
.sampler_states
)
2322 / sizeof(*device
->stateBlock
->state
.sampler_states
))
2324 WARN("Invalid sampler %u.\n", sampler_idx
);
2325 return 0; /* Windows accepts overflowing this array ... we do not. */
2328 return device
->stateBlock
->state
.sampler_states
[sampler_idx
][state
];
2331 void CDECL
wined3d_device_set_scissor_rect(struct wined3d_device
*device
, const RECT
*rect
)
2333 TRACE("device %p, rect %s.\n", device
, wine_dbgstr_rect(rect
));
2335 device
->updateStateBlock
->changed
.scissorRect
= TRUE
;
2336 if (EqualRect(&device
->updateStateBlock
->state
.scissor_rect
, rect
))
2338 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2341 CopyRect(&device
->updateStateBlock
->state
.scissor_rect
, rect
);
2343 if (device
->isRecordingState
)
2345 TRACE("Recording... not performing anything.\n");
2349 device_invalidate_state(device
, STATE_SCISSORRECT
);
2352 void CDECL
wined3d_device_get_scissor_rect(const struct wined3d_device
*device
, RECT
*rect
)
2354 TRACE("device %p, rect %p.\n", device
, rect
);
2356 *rect
= device
->updateStateBlock
->state
.scissor_rect
;
2357 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect
));
2360 void CDECL
wined3d_device_set_vertex_declaration(struct wined3d_device
*device
,
2361 struct wined3d_vertex_declaration
*declaration
)
2363 struct wined3d_vertex_declaration
*prev
= device
->updateStateBlock
->state
.vertex_declaration
;
2365 TRACE("device %p, declaration %p.\n", device
, declaration
);
2368 wined3d_vertex_declaration_incref(declaration
);
2370 wined3d_vertex_declaration_decref(prev
);
2372 device
->updateStateBlock
->state
.vertex_declaration
= declaration
;
2373 device
->updateStateBlock
->changed
.vertexDecl
= TRUE
;
2375 if (device
->isRecordingState
)
2377 TRACE("Recording... not performing anything.\n");
2381 if (declaration
== prev
)
2383 /* Checked after the assignment to allow proper stateblock recording. */
2384 TRACE("Application is setting the old declaration over, nothing to do.\n");
2388 device_invalidate_state(device
, STATE_VDECL
);
2391 struct wined3d_vertex_declaration
* CDECL
wined3d_device_get_vertex_declaration(const struct wined3d_device
*device
)
2393 TRACE("device %p.\n", device
);
2395 return device
->stateBlock
->state
.vertex_declaration
;
2398 void CDECL
wined3d_device_set_vertex_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2400 struct wined3d_shader
*prev
= device
->updateStateBlock
->state
.vertex_shader
;
2402 TRACE("device %p, shader %p.\n", device
, shader
);
2405 wined3d_shader_incref(shader
);
2407 wined3d_shader_decref(prev
);
2409 device
->updateStateBlock
->state
.vertex_shader
= shader
;
2410 device
->updateStateBlock
->changed
.vertexShader
= TRUE
;
2412 if (device
->isRecordingState
)
2414 TRACE("Recording... not performing anything.\n");
2420 TRACE("Application is setting the old shader over, nothing to do.\n");
2424 device_invalidate_state(device
, STATE_VSHADER
);
2427 struct wined3d_shader
* CDECL
wined3d_device_get_vertex_shader(const struct wined3d_device
*device
)
2429 TRACE("device %p.\n", device
);
2431 return device
->stateBlock
->state
.vertex_shader
;
2434 void CDECL
wined3d_device_set_vs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2436 struct wined3d_buffer
*prev
;
2438 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2440 if (idx
>= MAX_CONSTANT_BUFFERS
)
2442 WARN("Invalid constant buffer index %u.\n", idx
);
2446 prev
= device
->updateStateBlock
->state
.vs_cb
[idx
];
2447 device
->updateStateBlock
->state
.vs_cb
[idx
] = buffer
;
2449 if (device
->isRecordingState
)
2452 wined3d_buffer_incref(buffer
);
2454 wined3d_buffer_decref(prev
);
2462 InterlockedIncrement(&buffer
->resource
.bind_count
);
2463 wined3d_buffer_incref(buffer
);
2467 InterlockedDecrement(&prev
->resource
.bind_count
);
2468 wined3d_buffer_decref(prev
);
2473 struct wined3d_buffer
* CDECL
wined3d_device_get_vs_cb(const struct wined3d_device
*device
, UINT idx
)
2475 TRACE("device %p, idx %u.\n", device
, idx
);
2477 if (idx
>= MAX_CONSTANT_BUFFERS
)
2479 WARN("Invalid constant buffer index %u.\n", idx
);
2483 return device
->stateBlock
->state
.vs_cb
[idx
];
2486 void CDECL
wined3d_device_set_vs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2488 struct wined3d_sampler
*prev
;
2490 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2492 if (idx
>= MAX_SAMPLER_OBJECTS
)
2494 WARN("Invalid sampler index %u.\n", idx
);
2498 prev
= device
->updateStateBlock
->state
.vs_sampler
[idx
];
2499 device
->updateStateBlock
->state
.vs_sampler
[idx
] = sampler
;
2502 wined3d_sampler_incref(sampler
);
2504 wined3d_sampler_decref(prev
);
2507 struct wined3d_sampler
* CDECL
wined3d_device_get_vs_sampler(const struct wined3d_device
*device
, UINT idx
)
2509 TRACE("device %p, idx %u.\n", device
, idx
);
2511 if (idx
>= MAX_SAMPLER_OBJECTS
)
2513 WARN("Invalid sampler index %u.\n", idx
);
2517 return device
->stateBlock
->state
.vs_sampler
[idx
];
2520 HRESULT CDECL
wined3d_device_set_vs_consts_b(struct wined3d_device
*device
,
2521 UINT start_register
, const BOOL
*constants
, UINT bool_count
)
2523 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
2526 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2527 device
, start_register
, constants
, bool_count
);
2529 if (!constants
|| start_register
>= MAX_CONST_B
)
2530 return WINED3DERR_INVALIDCALL
;
2532 memcpy(&device
->updateStateBlock
->state
.vs_consts_b
[start_register
], constants
, count
* sizeof(BOOL
));
2533 for (i
= 0; i
< count
; ++i
)
2534 TRACE("Set BOOL constant %u to %s.\n", start_register
+ i
, constants
[i
] ? "true" : "false");
2536 for (i
= start_register
; i
< count
+ start_register
; ++i
)
2537 device
->updateStateBlock
->changed
.vertexShaderConstantsB
|= (1 << i
);
2539 if (!device
->isRecordingState
)
2540 device_invalidate_state(device
, STATE_VERTEXSHADERCONSTANT
);
2545 HRESULT CDECL
wined3d_device_get_vs_consts_b(const struct wined3d_device
*device
,
2546 UINT start_register
, BOOL
*constants
, UINT bool_count
)
2548 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
2550 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2551 device
, start_register
, constants
, bool_count
);
2553 if (!constants
|| start_register
>= MAX_CONST_B
)
2554 return WINED3DERR_INVALIDCALL
;
2556 memcpy(constants
, &device
->stateBlock
->state
.vs_consts_b
[start_register
], count
* sizeof(BOOL
));
2561 HRESULT CDECL
wined3d_device_set_vs_consts_i(struct wined3d_device
*device
,
2562 UINT start_register
, const int *constants
, UINT vector4i_count
)
2564 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
2567 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2568 device
, start_register
, constants
, vector4i_count
);
2570 if (!constants
|| start_register
>= MAX_CONST_I
)
2571 return WINED3DERR_INVALIDCALL
;
2573 memcpy(&device
->updateStateBlock
->state
.vs_consts_i
[start_register
* 4], constants
, count
* sizeof(int) * 4);
2574 for (i
= 0; i
< count
; ++i
)
2575 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register
+ i
,
2576 constants
[i
* 4], constants
[i
* 4 + 1],
2577 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
2579 for (i
= start_register
; i
< count
+ start_register
; ++i
)
2580 device
->updateStateBlock
->changed
.vertexShaderConstantsI
|= (1 << i
);
2582 if (!device
->isRecordingState
)
2583 device_invalidate_state(device
, STATE_VERTEXSHADERCONSTANT
);
2588 HRESULT CDECL
wined3d_device_get_vs_consts_i(const struct wined3d_device
*device
,
2589 UINT start_register
, int *constants
, UINT vector4i_count
)
2591 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
2593 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2594 device
, start_register
, constants
, vector4i_count
);
2596 if (!constants
|| start_register
>= MAX_CONST_I
)
2597 return WINED3DERR_INVALIDCALL
;
2599 memcpy(constants
, &device
->stateBlock
->state
.vs_consts_i
[start_register
* 4], count
* sizeof(int) * 4);
2603 HRESULT CDECL
wined3d_device_set_vs_consts_f(struct wined3d_device
*device
,
2604 UINT start_register
, const float *constants
, UINT vector4f_count
)
2607 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2609 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2610 device
, start_register
, constants
, vector4f_count
);
2612 /* Specifically test start_register > limit to catch MAX_UINT overflows
2613 * when adding start_register + vector4f_count. */
2615 || start_register
+ vector4f_count
> d3d_info
->limits
.vs_uniform_count
2616 || start_register
> d3d_info
->limits
.vs_uniform_count
)
2617 return WINED3DERR_INVALIDCALL
;
2619 memcpy(&device
->updateStateBlock
->state
.vs_consts_f
[start_register
* 4],
2620 constants
, vector4f_count
* sizeof(float) * 4);
2623 for (i
= 0; i
< vector4f_count
; ++i
)
2624 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register
+ i
,
2625 constants
[i
* 4], constants
[i
* 4 + 1],
2626 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
2629 if (!device
->isRecordingState
)
2631 device
->shader_backend
->shader_update_float_vertex_constants(device
, start_register
, vector4f_count
);
2632 device_invalidate_state(device
, STATE_VERTEXSHADERCONSTANT
);
2635 memset(device
->updateStateBlock
->changed
.vertexShaderConstantsF
+ start_register
, 1,
2636 sizeof(*device
->updateStateBlock
->changed
.vertexShaderConstantsF
) * vector4f_count
);
2641 HRESULT CDECL
wined3d_device_get_vs_consts_f(const struct wined3d_device
*device
,
2642 UINT start_register
, float *constants
, UINT vector4f_count
)
2644 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2645 int count
= min(vector4f_count
, d3d_info
->limits
.vs_uniform_count
- start_register
);
2647 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2648 device
, start_register
, constants
, vector4f_count
);
2650 if (!constants
|| count
< 0)
2651 return WINED3DERR_INVALIDCALL
;
2653 memcpy(constants
, &device
->stateBlock
->state
.vs_consts_f
[start_register
* 4], count
* sizeof(float) * 4);
2658 static void device_invalidate_texture_stage(const struct wined3d_device
*device
, DWORD stage
)
2662 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2664 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, i
));
2668 static void device_map_stage(struct wined3d_device
*device
, DWORD stage
, DWORD unit
)
2670 DWORD i
= device
->rev_tex_unit_map
[unit
];
2671 DWORD j
= device
->texUnitMap
[stage
];
2673 device
->texUnitMap
[stage
] = unit
;
2674 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2675 device
->texUnitMap
[i
] = WINED3D_UNMAPPED_STAGE
;
2677 device
->rev_tex_unit_map
[unit
] = stage
;
2678 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2679 device
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2682 static void device_update_fixed_function_usage_map(struct wined3d_device
*device
)
2686 device
->fixed_function_usage_map
= 0;
2687 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2689 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2690 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2691 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2692 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2693 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2694 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2695 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2696 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2697 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2699 /* Not used, and disable higher stages. */
2700 if (color_op
== WINED3D_TOP_DISABLE
)
2703 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2704 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2705 || ((color_arg3
== WINED3DTA_TEXTURE
)
2706 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2707 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2708 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2709 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2710 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2711 device
->fixed_function_usage_map
|= (1 << i
);
2713 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2714 && i
< MAX_TEXTURES
- 1)
2715 device
->fixed_function_usage_map
|= (1 << (i
+ 1));
2719 static void device_map_fixed_function_samplers(struct wined3d_device
*device
, const struct wined3d_d3d_info
*d3d_info
)
2721 unsigned int i
, tex
;
2724 device_update_fixed_function_usage_map(device
);
2725 ffu_map
= device
->fixed_function_usage_map
;
2727 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2728 || device
->stateBlock
->state
.lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2730 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2732 if (!(ffu_map
& 1)) continue;
2734 if (device
->texUnitMap
[i
] != i
)
2736 device_map_stage(device
, i
, i
);
2737 device_invalidate_state(device
, STATE_SAMPLER(i
));
2738 device_invalidate_texture_stage(device
, i
);
2744 /* Now work out the mapping */
2746 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2748 if (!(ffu_map
& 1)) continue;
2750 if (device
->texUnitMap
[i
] != tex
)
2752 device_map_stage(device
, i
, tex
);
2753 device_invalidate_state(device
, STATE_SAMPLER(i
));
2754 device_invalidate_texture_stage(device
, i
);
2761 static void device_map_psamplers(struct wined3d_device
*device
, const struct wined3d_d3d_info
*d3d_info
)
2763 const enum wined3d_sampler_texture_type
*sampler_type
=
2764 device
->stateBlock
->state
.pixel_shader
->reg_maps
.sampler_type
;
2767 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2769 if (sampler_type
[i
] && device
->texUnitMap
[i
] != i
)
2771 device_map_stage(device
, i
, i
);
2772 device_invalidate_state(device
, STATE_SAMPLER(i
));
2773 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2774 device_invalidate_texture_stage(device
, i
);
2779 static BOOL
device_unit_free_for_vs(const struct wined3d_device
*device
,
2780 const enum wined3d_sampler_texture_type
*pshader_sampler_tokens
,
2781 const enum wined3d_sampler_texture_type
*vshader_sampler_tokens
, DWORD unit
)
2783 DWORD current_mapping
= device
->rev_tex_unit_map
[unit
];
2785 /* Not currently used */
2786 if (current_mapping
== WINED3D_UNMAPPED_STAGE
) return TRUE
;
2788 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
) {
2789 /* Used by a fragment sampler */
2791 if (!pshader_sampler_tokens
) {
2792 /* No pixel shader, check fixed function */
2793 return current_mapping
>= MAX_TEXTURES
|| !(device
->fixed_function_usage_map
& (1 << current_mapping
));
2796 /* Pixel shader, check the shader's sampler map */
2797 return !pshader_sampler_tokens
[current_mapping
];
2800 /* Used by a vertex sampler */
2801 return !vshader_sampler_tokens
[current_mapping
- MAX_FRAGMENT_SAMPLERS
];
2804 static void device_map_vsamplers(struct wined3d_device
*device
, BOOL ps
, const struct wined3d_gl_info
*gl_info
)
2806 const enum wined3d_sampler_texture_type
*vshader_sampler_type
=
2807 device
->stateBlock
->state
.vertex_shader
->reg_maps
.sampler_type
;
2808 const enum wined3d_sampler_texture_type
*pshader_sampler_type
= NULL
;
2809 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2814 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2815 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2816 pshader_sampler_type
= device
->stateBlock
->state
.pixel_shader
->reg_maps
.sampler_type
;
2819 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
2820 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2821 if (vshader_sampler_type
[i
])
2823 if (device
->texUnitMap
[vsampler_idx
] != WINED3D_UNMAPPED_STAGE
)
2825 /* Already mapped somewhere */
2831 if (device_unit_free_for_vs(device
, pshader_sampler_type
, vshader_sampler_type
, start
))
2833 device_map_stage(device
, vsampler_idx
, start
);
2834 device_invalidate_state(device
, STATE_SAMPLER(vsampler_idx
));
2846 void device_update_tex_unit_map(struct wined3d_device
*device
)
2848 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2849 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2850 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2851 BOOL vs
= use_vs(state
);
2852 BOOL ps
= use_ps(state
);
2855 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2856 * that would be really messy and require shader recompilation
2857 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2858 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2861 device_map_psamplers(device
, d3d_info
);
2863 device_map_fixed_function_samplers(device
, d3d_info
);
2866 device_map_vsamplers(device
, ps
, gl_info
);
2869 void CDECL
wined3d_device_set_pixel_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2871 struct wined3d_shader
*prev
= device
->updateStateBlock
->state
.pixel_shader
;
2873 TRACE("device %p, shader %p.\n", device
, shader
);
2876 wined3d_shader_incref(shader
);
2878 wined3d_shader_decref(prev
);
2880 device
->updateStateBlock
->state
.pixel_shader
= shader
;
2881 device
->updateStateBlock
->changed
.pixelShader
= TRUE
;
2883 if (device
->isRecordingState
)
2885 TRACE("Recording... not performing anything.\n");
2891 TRACE("Application is setting the old shader over, nothing to do.\n");
2895 device_invalidate_state(device
, STATE_PIXELSHADER
);
2898 struct wined3d_shader
* CDECL
wined3d_device_get_pixel_shader(const struct wined3d_device
*device
)
2900 TRACE("device %p.\n", device
);
2902 return device
->stateBlock
->state
.pixel_shader
;
2905 void CDECL
wined3d_device_set_ps_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2907 struct wined3d_buffer
*prev
;
2909 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2911 if (idx
>= MAX_CONSTANT_BUFFERS
)
2913 WARN("Invalid constant buffer index %u.\n", idx
);
2917 prev
= device
->updateStateBlock
->state
.ps_cb
[idx
];
2918 device
->updateStateBlock
->state
.ps_cb
[idx
] = buffer
;
2920 if (device
->isRecordingState
)
2923 wined3d_buffer_incref(buffer
);
2925 wined3d_buffer_decref(prev
);
2933 InterlockedIncrement(&buffer
->resource
.bind_count
);
2934 wined3d_buffer_incref(buffer
);
2938 InterlockedDecrement(&prev
->resource
.bind_count
);
2939 wined3d_buffer_decref(prev
);
2944 struct wined3d_buffer
* CDECL
wined3d_device_get_ps_cb(const struct wined3d_device
*device
, UINT idx
)
2946 TRACE("device %p, idx %u.\n", device
, idx
);
2948 if (idx
>= MAX_CONSTANT_BUFFERS
)
2950 WARN("Invalid constant buffer index %u.\n", idx
);
2954 return device
->stateBlock
->state
.ps_cb
[idx
];
2957 void CDECL
wined3d_device_set_ps_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2959 struct wined3d_sampler
*prev
;
2961 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2963 if (idx
>= MAX_SAMPLER_OBJECTS
)
2965 WARN("Invalid sampler index %u.\n", idx
);
2969 prev
= device
->updateStateBlock
->state
.ps_sampler
[idx
];
2970 device
->updateStateBlock
->state
.ps_sampler
[idx
] = sampler
;
2973 wined3d_sampler_incref(sampler
);
2975 wined3d_sampler_decref(prev
);
2978 struct wined3d_sampler
* CDECL
wined3d_device_get_ps_sampler(const struct wined3d_device
*device
, UINT idx
)
2980 TRACE("device %p, idx %u.\n", device
, idx
);
2982 if (idx
>= MAX_SAMPLER_OBJECTS
)
2984 WARN("Invalid sampler index %u.\n", idx
);
2988 return device
->stateBlock
->state
.ps_sampler
[idx
];
2991 HRESULT CDECL
wined3d_device_set_ps_consts_b(struct wined3d_device
*device
,
2992 UINT start_register
, const BOOL
*constants
, UINT bool_count
)
2994 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
2997 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2998 device
, start_register
, constants
, bool_count
);
3000 if (!constants
|| start_register
>= MAX_CONST_B
)
3001 return WINED3DERR_INVALIDCALL
;
3003 memcpy(&device
->updateStateBlock
->state
.ps_consts_b
[start_register
], constants
, count
* sizeof(BOOL
));
3004 for (i
= 0; i
< count
; ++i
)
3005 TRACE("Set BOOL constant %u to %s.\n", start_register
+ i
, constants
[i
] ? "true" : "false");
3007 for (i
= start_register
; i
< count
+ start_register
; ++i
)
3008 device
->updateStateBlock
->changed
.pixelShaderConstantsB
|= (1 << i
);
3010 if (!device
->isRecordingState
)
3011 device_invalidate_state(device
, STATE_PIXELSHADERCONSTANT
);
3016 HRESULT CDECL
wined3d_device_get_ps_consts_b(const struct wined3d_device
*device
,
3017 UINT start_register
, BOOL
*constants
, UINT bool_count
)
3019 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
3021 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3022 device
, start_register
, constants
, bool_count
);
3024 if (!constants
|| start_register
>= MAX_CONST_B
)
3025 return WINED3DERR_INVALIDCALL
;
3027 memcpy(constants
, &device
->stateBlock
->state
.ps_consts_b
[start_register
], count
* sizeof(BOOL
));
3032 HRESULT CDECL
wined3d_device_set_ps_consts_i(struct wined3d_device
*device
,
3033 UINT start_register
, const int *constants
, UINT vector4i_count
)
3035 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
3038 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3039 device
, start_register
, constants
, vector4i_count
);
3041 if (!constants
|| start_register
>= MAX_CONST_I
)
3042 return WINED3DERR_INVALIDCALL
;
3044 memcpy(&device
->updateStateBlock
->state
.ps_consts_i
[start_register
* 4], constants
, count
* sizeof(int) * 4);
3045 for (i
= 0; i
< count
; ++i
)
3046 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register
+ i
,
3047 constants
[i
* 4], constants
[i
* 4 + 1],
3048 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
3050 for (i
= start_register
; i
< count
+ start_register
; ++i
)
3051 device
->updateStateBlock
->changed
.pixelShaderConstantsI
|= (1 << i
);
3053 if (!device
->isRecordingState
)
3054 device_invalidate_state(device
, STATE_PIXELSHADERCONSTANT
);
3059 HRESULT CDECL
wined3d_device_get_ps_consts_i(const struct wined3d_device
*device
,
3060 UINT start_register
, int *constants
, UINT vector4i_count
)
3062 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
3064 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3065 device
, start_register
, constants
, vector4i_count
);
3067 if (!constants
|| start_register
>= MAX_CONST_I
)
3068 return WINED3DERR_INVALIDCALL
;
3070 memcpy(constants
, &device
->stateBlock
->state
.ps_consts_i
[start_register
* 4], count
* sizeof(int) * 4);
3075 HRESULT CDECL
wined3d_device_set_ps_consts_f(struct wined3d_device
*device
,
3076 UINT start_register
, const float *constants
, UINT vector4f_count
)
3079 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3081 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3082 device
, start_register
, constants
, vector4f_count
);
3084 /* Specifically test start_register > limit to catch MAX_UINT overflows
3085 * when adding start_register + vector4f_count. */
3087 || start_register
+ vector4f_count
> d3d_info
->limits
.ps_uniform_count
3088 || start_register
> d3d_info
->limits
.ps_uniform_count
)
3089 return WINED3DERR_INVALIDCALL
;
3091 memcpy(&device
->updateStateBlock
->state
.ps_consts_f
[start_register
* 4],
3092 constants
, vector4f_count
* sizeof(float) * 4);
3095 for (i
= 0; i
< vector4f_count
; ++i
)
3096 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register
+ i
,
3097 constants
[i
* 4], constants
[i
* 4 + 1],
3098 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
3101 if (!device
->isRecordingState
)
3103 device
->shader_backend
->shader_update_float_pixel_constants(device
, start_register
, vector4f_count
);
3104 device_invalidate_state(device
, STATE_PIXELSHADERCONSTANT
);
3107 memset(device
->updateStateBlock
->changed
.pixelShaderConstantsF
+ start_register
, 1,
3108 sizeof(*device
->updateStateBlock
->changed
.pixelShaderConstantsF
) * vector4f_count
);
3113 HRESULT CDECL
wined3d_device_get_ps_consts_f(const struct wined3d_device
*device
,
3114 UINT start_register
, float *constants
, UINT vector4f_count
)
3116 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3117 int count
= min(vector4f_count
, d3d_info
->limits
.ps_uniform_count
- start_register
);
3119 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3120 device
, start_register
, constants
, vector4f_count
);
3122 if (!constants
|| count
< 0)
3123 return WINED3DERR_INVALIDCALL
;
3125 memcpy(constants
, &device
->stateBlock
->state
.ps_consts_f
[start_register
* 4], count
* sizeof(float) * 4);
3130 void CDECL
wined3d_device_set_geometry_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
3132 struct wined3d_shader
*prev
= device
->updateStateBlock
->state
.geometry_shader
;
3134 TRACE("device %p, shader %p.\n", device
, shader
);
3137 wined3d_shader_incref(shader
);
3139 wined3d_shader_decref(prev
);
3141 device
->updateStateBlock
->state
.geometry_shader
= shader
;
3143 if (device
->isRecordingState
|| shader
== prev
)
3146 device_invalidate_state(device
, STATE_GEOMETRY_SHADER
);
3149 struct wined3d_shader
* CDECL
wined3d_device_get_geometry_shader(const struct wined3d_device
*device
)
3151 TRACE("device %p.\n", device
);
3153 return device
->stateBlock
->state
.geometry_shader
;
3156 void CDECL
wined3d_device_set_gs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
3158 struct wined3d_buffer
*prev
;
3160 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
3162 if (idx
>= MAX_CONSTANT_BUFFERS
)
3164 WARN("Invalid constant buffer index %u.\n", idx
);
3168 prev
= device
->updateStateBlock
->state
.gs_cb
[idx
];
3169 device
->updateStateBlock
->state
.gs_cb
[idx
] = buffer
;
3171 if (device
->isRecordingState
)
3174 wined3d_buffer_incref(buffer
);
3176 wined3d_buffer_decref(prev
);
3184 InterlockedIncrement(&buffer
->resource
.bind_count
);
3185 wined3d_buffer_incref(buffer
);
3189 InterlockedDecrement(&prev
->resource
.bind_count
);
3190 wined3d_buffer_decref(prev
);
3195 struct wined3d_buffer
* CDECL
wined3d_device_get_gs_cb(const struct wined3d_device
*device
, UINT idx
)
3197 TRACE("device %p, idx %u.\n", device
, idx
);
3199 if (idx
>= MAX_CONSTANT_BUFFERS
)
3201 WARN("Invalid constant buffer index %u.\n", idx
);
3205 return device
->stateBlock
->state
.gs_cb
[idx
];
3208 void CDECL
wined3d_device_set_gs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
3210 struct wined3d_sampler
*prev
;
3212 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
3214 if (idx
>= MAX_SAMPLER_OBJECTS
)
3216 WARN("Invalid sampler index %u.\n", idx
);
3220 prev
= device
->updateStateBlock
->state
.gs_sampler
[idx
];
3221 device
->updateStateBlock
->state
.gs_sampler
[idx
] = sampler
;
3224 wined3d_sampler_incref(sampler
);
3226 wined3d_sampler_decref(prev
);
3229 struct wined3d_sampler
* CDECL
wined3d_device_get_gs_sampler(const struct wined3d_device
*device
, UINT idx
)
3231 TRACE("device %p, idx %u.\n", device
, idx
);
3233 if (idx
>= MAX_SAMPLER_OBJECTS
)
3235 WARN("Invalid sampler index %u.\n", idx
);
3239 return device
->stateBlock
->state
.gs_sampler
[idx
];
3242 /* Context activation is done by the caller. */
3243 /* Do not call while under the GL lock. */
3244 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3245 static HRESULT
process_vertices_strided(const struct wined3d_device
*device
, DWORD dwDestIndex
, DWORD dwCount
,
3246 const struct wined3d_stream_info
*stream_info
, struct wined3d_buffer
*dest
, DWORD flags
,
3249 struct wined3d_matrix mat
, proj_mat
, view_mat
, world_mat
;
3250 struct wined3d_viewport vp
;
3258 if (stream_info
->use_map
& (1 << WINED3D_FFP_NORMAL
))
3260 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3263 if (!(stream_info
->use_map
& (1 << WINED3D_FFP_POSITION
)))
3265 ERR("Source has no position mask\n");
3266 return WINED3DERR_INVALIDCALL
;
3269 if (device
->stateBlock
->state
.render_states
[WINED3D_RS_CLIPPING
])
3271 static BOOL warned
= FALSE
;
3273 * The clipping code is not quite correct. Some things need
3274 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3275 * so disable clipping for now.
3276 * (The graphics in Half-Life are broken, and my processvertices
3277 * test crashes with IDirect3DDevice3)
3283 FIXME("Clipping is broken and disabled for now\n");
3289 vertex_size
= get_flexible_vertex_size(DestFVF
);
3290 if (FAILED(hr
= wined3d_buffer_map(dest
, dwDestIndex
* vertex_size
, dwCount
* vertex_size
, &dest_ptr
, 0)))
3292 WARN("Failed to map buffer, hr %#x.\n", hr
);
3296 wined3d_device_get_transform(device
, WINED3D_TS_VIEW
, &view_mat
);
3297 wined3d_device_get_transform(device
, WINED3D_TS_PROJECTION
, &proj_mat
);
3298 wined3d_device_get_transform(device
, WINED3D_TS_WORLD_MATRIX(0), &world_mat
);
3300 TRACE("View mat:\n");
3301 TRACE("%f %f %f %f\n", view_mat
.u
.s
._11
, view_mat
.u
.s
._12
, view_mat
.u
.s
._13
, view_mat
.u
.s
._14
);
3302 TRACE("%f %f %f %f\n", view_mat
.u
.s
._21
, view_mat
.u
.s
._22
, view_mat
.u
.s
._23
, view_mat
.u
.s
._24
);
3303 TRACE("%f %f %f %f\n", view_mat
.u
.s
._31
, view_mat
.u
.s
._32
, view_mat
.u
.s
._33
, view_mat
.u
.s
._34
);
3304 TRACE("%f %f %f %f\n", view_mat
.u
.s
._41
, view_mat
.u
.s
._42
, view_mat
.u
.s
._43
, view_mat
.u
.s
._44
);
3306 TRACE("Proj mat:\n");
3307 TRACE("%f %f %f %f\n", proj_mat
.u
.s
._11
, proj_mat
.u
.s
._12
, proj_mat
.u
.s
._13
, proj_mat
.u
.s
._14
);
3308 TRACE("%f %f %f %f\n", proj_mat
.u
.s
._21
, proj_mat
.u
.s
._22
, proj_mat
.u
.s
._23
, proj_mat
.u
.s
._24
);
3309 TRACE("%f %f %f %f\n", proj_mat
.u
.s
._31
, proj_mat
.u
.s
._32
, proj_mat
.u
.s
._33
, proj_mat
.u
.s
._34
);
3310 TRACE("%f %f %f %f\n", proj_mat
.u
.s
._41
, proj_mat
.u
.s
._42
, proj_mat
.u
.s
._43
, proj_mat
.u
.s
._44
);
3312 TRACE("World mat:\n");
3313 TRACE("%f %f %f %f\n", world_mat
.u
.s
._11
, world_mat
.u
.s
._12
, world_mat
.u
.s
._13
, world_mat
.u
.s
._14
);
3314 TRACE("%f %f %f %f\n", world_mat
.u
.s
._21
, world_mat
.u
.s
._22
, world_mat
.u
.s
._23
, world_mat
.u
.s
._24
);
3315 TRACE("%f %f %f %f\n", world_mat
.u
.s
._31
, world_mat
.u
.s
._32
, world_mat
.u
.s
._33
, world_mat
.u
.s
._34
);
3316 TRACE("%f %f %f %f\n", world_mat
.u
.s
._41
, world_mat
.u
.s
._42
, world_mat
.u
.s
._43
, world_mat
.u
.s
._44
);
3318 /* Get the viewport */
3319 wined3d_device_get_viewport(device
, &vp
);
3320 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3321 vp
.x
, vp
.y
, vp
.width
, vp
.height
, vp
.min_z
, vp
.max_z
);
3323 multiply_matrix(&mat
,&view_mat
,&world_mat
);
3324 multiply_matrix(&mat
,&proj_mat
,&mat
);
3326 numTextures
= (DestFVF
& WINED3DFVF_TEXCOUNT_MASK
) >> WINED3DFVF_TEXCOUNT_SHIFT
;
3328 for (i
= 0; i
< dwCount
; i
+= 1) {
3329 unsigned int tex_index
;
3331 if ( ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZ
) ||
3332 ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
) ) {
3333 /* The position first */
3334 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_POSITION
];
3335 const float *p
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3337 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p
[0], p
[1], p
[2]);
3339 /* Multiplication with world, view and projection matrix */
3340 x
= (p
[0] * mat
.u
.s
._11
) + (p
[1] * mat
.u
.s
._21
) + (p
[2] * mat
.u
.s
._31
) + (1.0f
* mat
.u
.s
._41
);
3341 y
= (p
[0] * mat
.u
.s
._12
) + (p
[1] * mat
.u
.s
._22
) + (p
[2] * mat
.u
.s
._32
) + (1.0f
* mat
.u
.s
._42
);
3342 z
= (p
[0] * mat
.u
.s
._13
) + (p
[1] * mat
.u
.s
._23
) + (p
[2] * mat
.u
.s
._33
) + (1.0f
* mat
.u
.s
._43
);
3343 rhw
= (p
[0] * mat
.u
.s
._14
) + (p
[1] * mat
.u
.s
._24
) + (p
[2] * mat
.u
.s
._34
) + (1.0f
* mat
.u
.s
._44
);
3345 TRACE("x=%f y=%f z=%f rhw=%f\n", x
, y
, z
, rhw
);
3347 /* WARNING: The following things are taken from d3d7 and were not yet checked
3348 * against d3d8 or d3d9!
3351 /* Clipping conditions: From msdn
3353 * A vertex is clipped if it does not match the following requirements
3357 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3359 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3360 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3365 ( (-rhw
-eps
< x
) && (-rhw
-eps
< y
) && ( -eps
< z
) &&
3366 (x
<= rhw
+ eps
) && (y
<= rhw
+ eps
) && (z
<= rhw
+ eps
) &&
3369 /* "Normal" viewport transformation (not clipped)
3370 * 1) The values are divided by rhw
3371 * 2) The y axis is negative, so multiply it with -1
3372 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3373 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3374 * 4) Multiply x with Width/2 and add Width/2
3375 * 5) The same for the height
3376 * 6) Add the viewpoint X and Y to the 2D coordinates and
3377 * The minimum Z value to z
3378 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3380 * Well, basically it's simply a linear transformation into viewport
3392 z
*= vp
.max_z
- vp
.min_z
;
3394 x
+= vp
.width
/ 2 + vp
.x
;
3395 y
+= vp
.height
/ 2 + vp
.y
;
3400 /* That vertex got clipped
3401 * Contrary to OpenGL it is not dropped completely, it just
3402 * undergoes a different calculation.
3404 TRACE("Vertex got clipped\n");
3411 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3412 * outside of the main vertex buffer memory. That needs some more
3417 TRACE("Writing (%f %f %f) %f\n", x
, y
, z
, rhw
);
3420 ( (float *) dest_ptr
)[0] = x
;
3421 ( (float *) dest_ptr
)[1] = y
;
3422 ( (float *) dest_ptr
)[2] = z
;
3423 ( (float *) dest_ptr
)[3] = rhw
; /* SIC, see ddraw test! */
3425 dest_ptr
+= 3 * sizeof(float);
3427 if ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
)
3428 dest_ptr
+= sizeof(float);
3431 if (DestFVF
& WINED3DFVF_PSIZE
)
3432 dest_ptr
+= sizeof(DWORD
);
3434 if (DestFVF
& WINED3DFVF_NORMAL
)
3436 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_NORMAL
];
3437 const float *normal
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3438 /* AFAIK this should go into the lighting information */
3439 FIXME("Didn't expect the destination to have a normal\n");
3440 copy_and_next(dest_ptr
, normal
, 3 * sizeof(float));
3443 if (DestFVF
& WINED3DFVF_DIFFUSE
)
3445 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_DIFFUSE
];
3446 const DWORD
*color_d
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3447 if (!(stream_info
->use_map
& (1 << WINED3D_FFP_DIFFUSE
)))
3449 static BOOL warned
= FALSE
;
3452 ERR("No diffuse color in source, but destination has one\n");
3456 *( (DWORD
*) dest_ptr
) = 0xffffffff;
3457 dest_ptr
+= sizeof(DWORD
);
3461 copy_and_next(dest_ptr
, color_d
, sizeof(DWORD
));
3465 if (DestFVF
& WINED3DFVF_SPECULAR
)
3467 /* What's the color value in the feedback buffer? */
3468 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_SPECULAR
];
3469 const DWORD
*color_s
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3470 if (!(stream_info
->use_map
& (1 << WINED3D_FFP_SPECULAR
)))
3472 static BOOL warned
= FALSE
;
3475 ERR("No specular color in source, but destination has one\n");
3479 *(DWORD
*)dest_ptr
= 0xff000000;
3480 dest_ptr
+= sizeof(DWORD
);
3484 copy_and_next(dest_ptr
, color_s
, sizeof(DWORD
));
3488 for (tex_index
= 0; tex_index
< numTextures
; ++tex_index
)
3490 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_TEXCOORD0
+ tex_index
];
3491 const float *tex_coord
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3492 if (!(stream_info
->use_map
& (1 << (WINED3D_FFP_TEXCOORD0
+ tex_index
))))
3494 ERR("No source texture, but destination requests one\n");
3495 dest_ptr
+= GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float);
3499 copy_and_next(dest_ptr
, tex_coord
, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float));
3504 wined3d_buffer_unmap(dest
);
3508 #undef copy_and_next
3510 /* Do not call while under the GL lock. */
3511 HRESULT CDECL
wined3d_device_process_vertices(struct wined3d_device
*device
,
3512 UINT src_start_idx
, UINT dst_idx
, UINT vertex_count
, struct wined3d_buffer
*dst_buffer
,
3513 const struct wined3d_vertex_declaration
*declaration
, DWORD flags
, DWORD dst_fvf
)
3515 struct wined3d_state
*state
= &device
->stateBlock
->state
;
3516 struct wined3d_stream_info stream_info
;
3517 const struct wined3d_gl_info
*gl_info
;
3518 struct wined3d_context
*context
;
3519 struct wined3d_shader
*vs
;
3523 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3524 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3525 device
, src_start_idx
, dst_idx
, vertex_count
,
3526 dst_buffer
, declaration
, flags
, dst_fvf
);
3529 FIXME("Output vertex declaration not implemented yet.\n");
3531 /* Need any context to write to the vbo. */
3532 context
= context_acquire(device
, NULL
);
3533 gl_info
= context
->gl_info
;
3535 vs
= state
->vertex_shader
;
3536 state
->vertex_shader
= NULL
;
3537 device_stream_info_from_declaration(device
, &stream_info
);
3538 state
->vertex_shader
= vs
;
3540 /* We can't convert FROM a VBO, and vertex buffers used to source into
3541 * process_vertices() are unlikely to ever be used for drawing. Release
3542 * VBOs in those buffers and fix up the stream_info structure.
3544 * Also apply the start index. */
3545 for (i
= 0; i
< (sizeof(stream_info
.elements
) / sizeof(*stream_info
.elements
)); ++i
)
3547 struct wined3d_stream_info_element
*e
;
3549 if (!(stream_info
.use_map
& (1 << i
)))
3552 e
= &stream_info
.elements
[i
];
3553 if (e
->data
.buffer_object
)
3555 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
3556 e
->data
.buffer_object
= 0;
3557 e
->data
.addr
= (BYTE
*)((ULONG_PTR
)e
->data
.addr
+ (ULONG_PTR
)buffer_get_sysmem(vb
, gl_info
));
3558 GL_EXTCALL(glDeleteBuffersARB(1, &vb
->buffer_object
));
3559 vb
->buffer_object
= 0;
3562 e
->data
.addr
+= e
->stride
* src_start_idx
;
3565 hr
= process_vertices_strided(device
, dst_idx
, vertex_count
,
3566 &stream_info
, dst_buffer
, flags
, dst_fvf
);
3568 context_release(context
);
3573 void CDECL
wined3d_device_set_texture_stage_state(struct wined3d_device
*device
,
3574 UINT stage
, enum wined3d_texture_stage_state state
, DWORD value
)
3576 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3579 TRACE("device %p, stage %u, state %s, value %#x.\n",
3580 device
, stage
, debug_d3dtexturestate(state
), value
);
3582 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3584 WARN("Invalid state %#x passed.\n", state
);
3588 if (stage
>= d3d_info
->limits
.ffp_blend_stages
)
3590 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3591 stage
, d3d_info
->limits
.ffp_blend_stages
- 1);
3595 old_value
= device
->updateStateBlock
->state
.texture_states
[stage
][state
];
3596 device
->updateStateBlock
->changed
.textureState
[stage
] |= 1 << state
;
3597 device
->updateStateBlock
->state
.texture_states
[stage
][state
] = value
;
3599 if (device
->isRecordingState
)
3601 TRACE("Recording... not performing anything.\n");
3605 /* Checked after the assignments to allow proper stateblock recording. */
3606 if (old_value
== value
)
3608 TRACE("Application is setting the old value over, nothing to do.\n");
3612 if (stage
> device
->stateBlock
->state
.lowest_disabled_stage
3613 && device
->StateTable
[STATE_TEXTURESTAGE(0, state
)].representative
3614 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
))
3616 /* Colorop change above lowest disabled stage? That won't change
3617 * anything in the GL setup. Changes in other states are important on
3618 * disabled stages too. */
3622 if (state
== WINED3D_TSS_COLOR_OP
)
3626 if (value
== WINED3D_TOP_DISABLE
&& old_value
!= WINED3D_TOP_DISABLE
)
3628 /* Previously enabled stage disabled now. Make sure to dirtify
3629 * all enabled stages above stage, they have to be disabled.
3631 * The current stage is dirtified below. */
3632 for (i
= stage
+ 1; i
< device
->stateBlock
->state
.lowest_disabled_stage
; ++i
)
3634 TRACE("Additionally dirtifying stage %u.\n", i
);
3635 device_invalidate_state(device
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3637 device
->stateBlock
->state
.lowest_disabled_stage
= stage
;
3638 TRACE("New lowest disabled: %u.\n", stage
);
3640 else if (value
!= WINED3D_TOP_DISABLE
&& old_value
== WINED3D_TOP_DISABLE
)
3642 /* Previously disabled stage enabled. Stages above it may need
3643 * enabling. Stage must be lowest_disabled_stage here, if it's
3644 * bigger success is returned above, and stages below the lowest
3645 * disabled stage can't be enabled (because they are enabled
3648 * Again stage stage doesn't need to be dirtified here, it is
3650 for (i
= stage
+ 1; i
< d3d_info
->limits
.ffp_blend_stages
; ++i
)
3652 if (device
->updateStateBlock
->state
.texture_states
[i
][WINED3D_TSS_COLOR_OP
] == WINED3D_TOP_DISABLE
)
3654 TRACE("Additionally dirtifying stage %u due to enable.\n", i
);
3655 device_invalidate_state(device
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3657 device
->stateBlock
->state
.lowest_disabled_stage
= i
;
3658 TRACE("New lowest disabled: %u.\n", i
);
3662 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, state
));
3665 DWORD CDECL
wined3d_device_get_texture_stage_state(const struct wined3d_device
*device
,
3666 UINT stage
, enum wined3d_texture_stage_state state
)
3668 TRACE("device %p, stage %u, state %s.\n",
3669 device
, stage
, debug_d3dtexturestate(state
));
3671 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3673 WARN("Invalid state %#x passed.\n", state
);
3677 return device
->updateStateBlock
->state
.texture_states
[stage
][state
];
3680 HRESULT CDECL
wined3d_device_set_texture(struct wined3d_device
*device
,
3681 UINT stage
, struct wined3d_texture
*texture
)
3683 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3684 struct wined3d_texture
*prev
;
3686 TRACE("device %p, stage %u, texture %p.\n", device
, stage
, texture
);
3688 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3689 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3691 /* Windows accepts overflowing this array... we do not. */
3692 if (stage
>= sizeof(device
->stateBlock
->state
.textures
) / sizeof(*device
->stateBlock
->state
.textures
))
3694 WARN("Ignoring invalid stage %u.\n", stage
);
3698 if (texture
&& texture
->resource
.pool
== WINED3D_POOL_SCRATCH
)
3700 WARN("Rejecting attempt to set scratch texture.\n");
3701 return WINED3DERR_INVALIDCALL
;
3704 device
->updateStateBlock
->changed
.textures
|= 1 << stage
;
3706 prev
= device
->updateStateBlock
->state
.textures
[stage
];
3707 TRACE("Previous texture %p.\n", prev
);
3709 if (texture
== prev
)
3711 TRACE("App is setting the same texture again, nothing to do.\n");
3715 TRACE("Setting new texture to %p.\n", texture
);
3716 device
->updateStateBlock
->state
.textures
[stage
] = texture
;
3718 if (device
->isRecordingState
)
3720 TRACE("Recording... not performing anything\n");
3722 if (texture
) wined3d_texture_incref(texture
);
3723 if (prev
) wined3d_texture_decref(prev
);
3730 LONG bind_count
= InterlockedIncrement(&texture
->resource
.bind_count
);
3732 wined3d_texture_incref(texture
);
3734 if (!prev
|| texture
->target
!= prev
->target
)
3735 device_invalidate_state(device
, STATE_PIXELSHADER
);
3737 if (!prev
&& stage
< d3d_info
->limits
.ffp_blend_stages
)
3739 /* The source arguments for color and alpha ops have different
3740 * meanings when a NULL texture is bound, so the COLOR_OP and
3741 * ALPHA_OP have to be dirtified. */
3742 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_COLOR_OP
));
3743 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_ALPHA_OP
));
3746 if (bind_count
== 1)
3747 texture
->sampler
= stage
;
3752 LONG bind_count
= InterlockedDecrement(&prev
->resource
.bind_count
);
3754 if (!texture
&& stage
< d3d_info
->limits
.ffp_blend_stages
)
3756 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_COLOR_OP
));
3757 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_ALPHA_OP
));
3760 if (bind_count
&& prev
->sampler
== stage
)
3764 /* Search for other stages the texture is bound to. Shouldn't
3765 * happen if applications bind textures to a single stage only. */
3766 TRACE("Searching for other stages the texture is bound to.\n");
3767 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
3769 if (device
->updateStateBlock
->state
.textures
[i
] == prev
)
3771 TRACE("Texture is also bound to stage %u.\n", i
);
3778 wined3d_texture_decref(prev
);
3781 device_invalidate_state(device
, STATE_SAMPLER(stage
));
3786 struct wined3d_texture
* CDECL
wined3d_device_get_texture(const struct wined3d_device
*device
, UINT stage
)
3788 TRACE("device %p, stage %u.\n", device
, stage
);
3790 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3791 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3793 if (stage
>= sizeof(device
->stateBlock
->state
.textures
) / sizeof(*device
->stateBlock
->state
.textures
))
3795 WARN("Ignoring invalid stage %u.\n", stage
);
3796 return NULL
; /* Windows accepts overflowing this array ... we do not. */
3799 return device
->stateBlock
->state
.textures
[stage
];
3802 HRESULT CDECL
wined3d_device_get_back_buffer(const struct wined3d_device
*device
, UINT swapchain_idx
,
3803 UINT backbuffer_idx
, enum wined3d_backbuffer_type backbuffer_type
, struct wined3d_surface
**backbuffer
)
3805 struct wined3d_swapchain
*swapchain
;
3807 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3808 device
, swapchain_idx
, backbuffer_idx
, backbuffer_type
, backbuffer
);
3810 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3811 return WINED3DERR_INVALIDCALL
;
3813 if (!(*backbuffer
= wined3d_swapchain_get_back_buffer(swapchain
, backbuffer_idx
, backbuffer_type
)))
3814 return WINED3DERR_INVALIDCALL
;
3818 HRESULT CDECL
wined3d_device_get_device_caps(const struct wined3d_device
*device
, WINED3DCAPS
*caps
)
3820 TRACE("device %p, caps %p.\n", device
, caps
);
3822 return wined3d_get_device_caps(device
->wined3d
, device
->adapter
->ordinal
,
3823 device
->create_parms
.device_type
, caps
);
3826 HRESULT CDECL
wined3d_device_get_display_mode(const struct wined3d_device
*device
, UINT swapchain_idx
,
3827 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
3829 struct wined3d_swapchain
*swapchain
;
3831 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3832 device
, swapchain_idx
, mode
, rotation
);
3834 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3835 return WINED3DERR_INVALIDCALL
;
3837 return wined3d_swapchain_get_display_mode(swapchain
, mode
, rotation
);
3840 HRESULT CDECL
wined3d_device_begin_stateblock(struct wined3d_device
*device
)
3842 struct wined3d_stateblock
*stateblock
;
3845 TRACE("device %p.\n", device
);
3847 if (device
->isRecordingState
)
3848 return WINED3DERR_INVALIDCALL
;
3850 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_RECORDED
, &stateblock
);
3854 wined3d_stateblock_decref(device
->updateStateBlock
);
3855 device
->updateStateBlock
= stateblock
;
3856 device
->isRecordingState
= TRUE
;
3858 TRACE("Recording stateblock %p.\n", stateblock
);
3863 HRESULT CDECL
wined3d_device_end_stateblock(struct wined3d_device
*device
,
3864 struct wined3d_stateblock
**stateblock
)
3866 struct wined3d_stateblock
*object
= device
->updateStateBlock
;
3868 TRACE("device %p, stateblock %p.\n", device
, stateblock
);
3870 if (!device
->isRecordingState
)
3872 WARN("Not recording.\n");
3874 return WINED3DERR_INVALIDCALL
;
3877 stateblock_init_contained_states(object
);
3879 *stateblock
= object
;
3880 device
->isRecordingState
= FALSE
;
3881 device
->updateStateBlock
= device
->stateBlock
;
3882 wined3d_stateblock_incref(device
->updateStateBlock
);
3884 TRACE("Returning stateblock %p.\n", *stateblock
);
3889 HRESULT CDECL
wined3d_device_begin_scene(struct wined3d_device
*device
)
3891 /* At the moment we have no need for any functionality at the beginning
3893 TRACE("device %p.\n", device
);
3895 if (device
->inScene
)
3897 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3898 return WINED3DERR_INVALIDCALL
;
3900 device
->inScene
= TRUE
;
3904 HRESULT CDECL
wined3d_device_end_scene(struct wined3d_device
*device
)
3906 struct wined3d_context
*context
;
3908 TRACE("device %p.\n", device
);
3910 if (!device
->inScene
)
3912 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3913 return WINED3DERR_INVALIDCALL
;
3916 context
= context_acquire(device
, NULL
);
3917 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3918 context
->gl_info
->gl_ops
.gl
.p_glFlush();
3919 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3921 context_release(context
);
3923 device
->inScene
= FALSE
;
3927 HRESULT CDECL
wined3d_device_present(const struct wined3d_device
*device
, const RECT
*src_rect
,
3928 const RECT
*dst_rect
, HWND dst_window_override
, const RGNDATA
*dirty_region
, DWORD flags
)
3932 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3933 device
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
3934 dst_window_override
, dirty_region
, flags
);
3936 for (i
= 0; i
< device
->swapchain_count
; ++i
)
3938 wined3d_swapchain_present(device
->swapchains
[i
], src_rect
,
3939 dst_rect
, dst_window_override
, dirty_region
, flags
);
3945 /* Do not call while under the GL lock. */
3946 HRESULT CDECL
wined3d_device_clear(struct wined3d_device
*device
, DWORD rect_count
,
3947 const RECT
*rects
, DWORD flags
, const struct wined3d_color
*color
, float depth
, DWORD stencil
)
3951 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3952 device
, rect_count
, rects
, flags
, color
->r
, color
->g
, color
->b
, color
->a
, depth
, stencil
);
3954 if (!rect_count
&& rects
)
3956 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects
);
3960 if (flags
& (WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
))
3962 struct wined3d_surface
*ds
= device
->fb
.depth_stencil
;
3965 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3966 /* TODO: What about depth stencil buffers without stencil bits? */
3967 return WINED3DERR_INVALIDCALL
;
3969 else if (flags
& WINED3DCLEAR_TARGET
)
3971 if (ds
->resource
.width
< device
->fb
.render_targets
[0]->resource
.width
3972 || ds
->resource
.height
< device
->fb
.render_targets
[0]->resource
.height
)
3974 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3980 wined3d_get_draw_rect(&device
->stateBlock
->state
, &draw_rect
);
3981 device_clear_render_targets(device
, device
->adapter
->gl_info
.limits
.buffers
,
3982 &device
->fb
, rect_count
, rects
, &draw_rect
, flags
, color
, depth
, stencil
);
3987 void CDECL
wined3d_device_set_primitive_type(struct wined3d_device
*device
,
3988 enum wined3d_primitive_type primitive_type
)
3990 GLenum gl_primitive_type
, prev
;
3992 TRACE("device %p, primitive_type %s\n", device
, debug_d3dprimitivetype(primitive_type
));
3994 device
->updateStateBlock
->changed
.primitive_type
= TRUE
;
3995 gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
3996 prev
= device
->updateStateBlock
->state
.gl_primitive_type
;
3997 device
->updateStateBlock
->state
.gl_primitive_type
= gl_primitive_type
;
3998 if (!device
->isRecordingState
&& gl_primitive_type
!= prev
3999 && (gl_primitive_type
== GL_POINTS
|| prev
== GL_POINTS
))
4000 device_invalidate_state(device
, STATE_POINT_SIZE_ENABLE
);
4003 void CDECL
wined3d_device_get_primitive_type(const struct wined3d_device
*device
,
4004 enum wined3d_primitive_type
*primitive_type
)
4006 TRACE("device %p, primitive_type %p\n", device
, primitive_type
);
4008 *primitive_type
= d3d_primitive_type_from_gl(device
->stateBlock
->state
.gl_primitive_type
);
4010 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type
));
4013 HRESULT CDECL
wined3d_device_draw_primitive(struct wined3d_device
*device
, UINT start_vertex
, UINT vertex_count
)
4015 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device
, start_vertex
, vertex_count
);
4017 if (!device
->stateBlock
->state
.vertex_declaration
)
4019 WARN("Called without a valid vertex declaration set.\n");
4020 return WINED3DERR_INVALIDCALL
;
4023 if (device
->stateBlock
->state
.load_base_vertex_index
)
4025 device
->stateBlock
->state
.load_base_vertex_index
= 0;
4026 device_invalidate_state(device
, STATE_BASEVERTEXINDEX
);
4029 /* Account for the loading offset due to index buffers. Instead of
4030 * reloading all sources correct it with the startvertex parameter. */
4031 draw_primitive(device
, start_vertex
, vertex_count
, 0, 0, FALSE
);
4035 HRESULT CDECL
wined3d_device_draw_indexed_primitive(struct wined3d_device
*device
, UINT start_idx
, UINT index_count
)
4037 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4039 TRACE("device %p, start_idx %u, index_count %u.\n", device
, start_idx
, index_count
);
4041 if (!device
->stateBlock
->state
.index_buffer
)
4043 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4044 * without an index buffer set. (The first time at least...)
4045 * D3D8 simply dies, but I doubt it can do much harm to return
4046 * D3DERR_INVALIDCALL there as well. */
4047 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4048 return WINED3DERR_INVALIDCALL
;
4051 if (!device
->stateBlock
->state
.vertex_declaration
)
4053 WARN("Called without a valid vertex declaration set.\n");
4054 return WINED3DERR_INVALIDCALL
;
4057 if (!gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
] &&
4058 device
->stateBlock
->state
.load_base_vertex_index
!= device
->stateBlock
->state
.base_vertex_index
)
4060 device
->stateBlock
->state
.load_base_vertex_index
= device
->stateBlock
->state
.base_vertex_index
;
4061 device_invalidate_state(device
, STATE_BASEVERTEXINDEX
);
4064 draw_primitive(device
, start_idx
, index_count
, 0, 0, TRUE
);
4069 void CDECL
wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device
*device
,
4070 UINT start_idx
, UINT index_count
, UINT start_instance
, UINT instance_count
)
4072 TRACE("device %p, start_idx %u, index_count %u.\n", device
, start_idx
, index_count
);
4074 draw_primitive(device
, start_idx
, index_count
, start_instance
, instance_count
, TRUE
);
4077 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4078 static HRESULT
device_update_volume(struct wined3d_device
*device
,
4079 struct wined3d_volume
*src_volume
, struct wined3d_volume
*dst_volume
)
4081 struct wined3d_map_desc src
;
4082 struct wined3d_map_desc dst
;
4085 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4086 device
, src_volume
, dst_volume
);
4088 /* TODO: Implement direct loading into the gl volume instead of using
4089 * memcpy and dirtification to improve loading performance. */
4090 if (FAILED(hr
= wined3d_volume_map(src_volume
, &src
, NULL
, WINED3D_MAP_READONLY
)))
4092 if (FAILED(hr
= wined3d_volume_map(dst_volume
, &dst
, NULL
, WINED3D_MAP_DISCARD
)))
4094 wined3d_volume_unmap(src_volume
);
4098 memcpy(dst
.data
, src
.data
, dst_volume
->resource
.size
);
4100 hr
= wined3d_volume_unmap(dst_volume
);
4102 wined3d_volume_unmap(src_volume
);
4104 hr
= wined3d_volume_unmap(src_volume
);
4109 HRESULT CDECL
wined3d_device_update_texture(struct wined3d_device
*device
,
4110 struct wined3d_texture
*src_texture
, struct wined3d_texture
*dst_texture
)
4112 enum wined3d_resource_type type
;
4113 unsigned int level_count
, i
;
4116 TRACE("device %p, src_texture %p, dst_texture %p.\n", device
, src_texture
, dst_texture
);
4118 /* Verify that the source and destination textures are non-NULL. */
4119 if (!src_texture
|| !dst_texture
)
4121 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4122 return WINED3DERR_INVALIDCALL
;
4125 if (src_texture
== dst_texture
)
4127 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4128 return WINED3DERR_INVALIDCALL
;
4131 /* Verify that the source and destination textures are the same type. */
4132 type
= src_texture
->resource
.type
;
4133 if (dst_texture
->resource
.type
!= type
)
4135 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4136 return WINED3DERR_INVALIDCALL
;
4139 /* Check that both textures have the identical numbers of levels. */
4140 level_count
= wined3d_texture_get_level_count(src_texture
);
4141 if (wined3d_texture_get_level_count(dst_texture
) != level_count
)
4143 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4144 return WINED3DERR_INVALIDCALL
;
4147 /* Make sure that the destination texture is loaded. */
4148 dst_texture
->texture_ops
->texture_preload(dst_texture
, SRGB_RGB
);
4150 /* Update every surface level of the texture. */
4153 case WINED3D_RTYPE_TEXTURE
:
4155 struct wined3d_surface
*src_surface
;
4156 struct wined3d_surface
*dst_surface
;
4158 for (i
= 0; i
< level_count
; ++i
)
4160 src_surface
= surface_from_resource(wined3d_texture_get_sub_resource(src_texture
, i
));
4161 dst_surface
= surface_from_resource(wined3d_texture_get_sub_resource(dst_texture
, i
));
4162 hr
= wined3d_device_update_surface(device
, src_surface
, NULL
, dst_surface
, NULL
);
4165 WARN("Failed to update surface, hr %#x.\n", hr
);
4172 case WINED3D_RTYPE_CUBE_TEXTURE
:
4174 struct wined3d_surface
*src_surface
;
4175 struct wined3d_surface
*dst_surface
;
4177 for (i
= 0; i
< level_count
* 6; ++i
)
4179 src_surface
= surface_from_resource(wined3d_texture_get_sub_resource(src_texture
, i
));
4180 dst_surface
= surface_from_resource(wined3d_texture_get_sub_resource(dst_texture
, i
));
4181 hr
= wined3d_device_update_surface(device
, src_surface
, NULL
, dst_surface
, NULL
);
4184 WARN("Failed to update surface, hr %#x.\n", hr
);
4191 case WINED3D_RTYPE_VOLUME_TEXTURE
:
4193 for (i
= 0; i
< level_count
; ++i
)
4195 hr
= device_update_volume(device
,
4196 volume_from_resource(wined3d_texture_get_sub_resource(src_texture
, i
)),
4197 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture
, i
)));
4200 WARN("Failed to update volume, hr %#x.\n", hr
);
4208 FIXME("Unsupported texture type %#x.\n", type
);
4209 return WINED3DERR_INVALIDCALL
;
4215 HRESULT CDECL
wined3d_device_get_front_buffer_data(const struct wined3d_device
*device
,
4216 UINT swapchain_idx
, struct wined3d_surface
*dst_surface
)
4218 struct wined3d_swapchain
*swapchain
;
4220 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device
, swapchain_idx
, dst_surface
);
4222 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4223 return WINED3DERR_INVALIDCALL
;
4225 return wined3d_swapchain_get_front_buffer_data(swapchain
, dst_surface
);
4228 HRESULT CDECL
wined3d_device_validate_device(const struct wined3d_device
*device
, DWORD
*num_passes
)
4230 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
4231 struct wined3d_texture
*texture
;
4234 TRACE("device %p, num_passes %p.\n", device
, num_passes
);
4236 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
4238 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] == WINED3D_TEXF_NONE
)
4240 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
4241 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
4243 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] == WINED3D_TEXF_NONE
)
4245 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
4246 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
4249 texture
= state
->textures
[i
];
4250 if (!texture
|| texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
) continue;
4252 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] != WINED3D_TEXF_POINT
)
4254 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i
);
4257 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] != WINED3D_TEXF_POINT
)
4259 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i
);
4262 if (state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_NONE
4263 && state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_POINT
)
4265 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i
);
4270 if (state
->render_states
[WINED3D_RS_ZENABLE
] || state
->render_states
[WINED3D_RS_ZWRITEENABLE
]
4271 || state
->render_states
[WINED3D_RS_STENCILENABLE
])
4273 struct wined3d_surface
*ds
= device
->fb
.depth_stencil
;
4274 struct wined3d_surface
*target
= device
->fb
.render_targets
[0];
4277 && (ds
->resource
.width
< target
->resource
.width
|| ds
->resource
.height
< target
->resource
.height
))
4279 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4280 return WINED3DERR_CONFLICTINGRENDERSTATE
;
4284 /* return a sensible default */
4287 TRACE("returning D3D_OK\n");
4291 void CDECL
wined3d_device_set_software_vertex_processing(struct wined3d_device
*device
, BOOL software
)
4295 TRACE("device %p, software %#x.\n", device
, software
);
4299 FIXME("device %p, software %#x stub!\n", device
, software
);
4303 device
->softwareVertexProcessing
= software
;
4306 BOOL CDECL
wined3d_device_get_software_vertex_processing(const struct wined3d_device
*device
)
4310 TRACE("device %p.\n", device
);
4314 TRACE("device %p stub!\n", device
);
4318 return device
->softwareVertexProcessing
;
4321 HRESULT CDECL
wined3d_device_get_raster_status(const struct wined3d_device
*device
,
4322 UINT swapchain_idx
, struct wined3d_raster_status
*raster_status
)
4324 struct wined3d_swapchain
*swapchain
;
4326 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4327 device
, swapchain_idx
, raster_status
);
4329 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4330 return WINED3DERR_INVALIDCALL
;
4332 return wined3d_swapchain_get_raster_status(swapchain
, raster_status
);
4335 HRESULT CDECL
wined3d_device_set_npatch_mode(struct wined3d_device
*device
, float segments
)
4339 TRACE("device %p, segments %.8e.\n", device
, segments
);
4341 if (segments
!= 0.0f
)
4345 FIXME("device %p, segments %.8e stub!\n", device
, segments
);
4353 float CDECL
wined3d_device_get_npatch_mode(const struct wined3d_device
*device
)
4357 TRACE("device %p.\n", device
);
4361 FIXME("device %p stub!\n", device
);
4368 HRESULT CDECL
wined3d_device_update_surface(struct wined3d_device
*device
,
4369 struct wined3d_surface
*src_surface
, const RECT
*src_rect
,
4370 struct wined3d_surface
*dst_surface
, const POINT
*dst_point
)
4372 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4373 device
, src_surface
, wine_dbgstr_rect(src_rect
),
4374 dst_surface
, wine_dbgstr_point(dst_point
));
4376 if (src_surface
->resource
.pool
!= WINED3D_POOL_SYSTEM_MEM
|| dst_surface
->resource
.pool
!= WINED3D_POOL_DEFAULT
)
4378 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4379 src_surface
, dst_surface
);
4380 return WINED3DERR_INVALIDCALL
;
4383 return surface_upload_from_surface(dst_surface
, dst_point
, src_surface
, src_rect
);
4386 /* Do not call while under the GL lock. */
4387 HRESULT CDECL
wined3d_device_color_fill(struct wined3d_device
*device
,
4388 struct wined3d_surface
*surface
, const RECT
*rect
, const struct wined3d_color
*color
)
4392 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4393 device
, surface
, wine_dbgstr_rect(rect
),
4394 color
->r
, color
->g
, color
->b
, color
->a
);
4396 if (surface
->resource
.pool
!= WINED3D_POOL_DEFAULT
&& surface
->resource
.pool
!= WINED3D_POOL_SYSTEM_MEM
)
4398 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface
->resource
.pool
));
4399 return WINED3DERR_INVALIDCALL
;
4404 SetRect(&r
, 0, 0, surface
->resource
.width
, surface
->resource
.height
);
4408 return surface_color_fill(surface
, rect
, color
);
4411 /* Do not call while under the GL lock. */
4412 void CDECL
wined3d_device_clear_rendertarget_view(struct wined3d_device
*device
,
4413 struct wined3d_rendertarget_view
*rendertarget_view
, const struct wined3d_color
*color
)
4415 struct wined3d_resource
*resource
;
4419 resource
= rendertarget_view
->resource
;
4420 if (resource
->type
!= WINED3D_RTYPE_SURFACE
)
4422 FIXME("Only supported on surface resources\n");
4426 SetRect(&rect
, 0, 0, resource
->width
, resource
->height
);
4427 hr
= surface_color_fill(surface_from_resource(resource
), &rect
, color
);
4428 if (FAILED(hr
)) ERR("Color fill failed, hr %#x.\n", hr
);
4431 struct wined3d_surface
* CDECL
wined3d_device_get_render_target(const struct wined3d_device
*device
,
4432 UINT render_target_idx
)
4434 TRACE("device %p, render_target_idx %u.\n", device
, render_target_idx
);
4436 if (render_target_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4438 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4442 return device
->fb
.render_targets
[render_target_idx
];
4445 struct wined3d_surface
* CDECL
wined3d_device_get_depth_stencil(const struct wined3d_device
*device
)
4447 TRACE("device %p.\n", device
);
4449 return device
->fb
.depth_stencil
;
4452 HRESULT CDECL
wined3d_device_set_render_target(struct wined3d_device
*device
,
4453 UINT render_target_idx
, struct wined3d_surface
*render_target
, BOOL set_viewport
)
4455 struct wined3d_surface
*prev
;
4457 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4458 device
, render_target_idx
, render_target
, set_viewport
);
4460 if (render_target_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4462 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4463 return WINED3DERR_INVALIDCALL
;
4466 /* Render target 0 can't be set to NULL. */
4467 if (!render_target
&& !render_target_idx
)
4469 WARN("Trying to set render target 0 to NULL.\n");
4470 return WINED3DERR_INVALIDCALL
;
4473 if (render_target
&& !(render_target
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
))
4475 FIXME("Surface %p doesn't have render target usage.\n", render_target
);
4476 return WINED3DERR_INVALIDCALL
;
4479 /* Set the viewport and scissor rectangles, if requested. Tests show that
4480 * stateblock recording is ignored, the change goes directly into the
4481 * primary stateblock. */
4482 if (!render_target_idx
&& set_viewport
)
4484 struct wined3d_state
*state
= &device
->stateBlock
->state
;
4486 state
->viewport
.x
= 0;
4487 state
->viewport
.y
= 0;
4488 state
->viewport
.width
= render_target
->resource
.width
;
4489 state
->viewport
.height
= render_target
->resource
.height
;
4490 state
->viewport
.min_z
= 0.0f
;
4491 state
->viewport
.max_z
= 1.0f
;
4492 device_invalidate_state(device
, STATE_VIEWPORT
);
4494 state
->scissor_rect
.top
= 0;
4495 state
->scissor_rect
.left
= 0;
4496 state
->scissor_rect
.right
= render_target
->resource
.width
;
4497 state
->scissor_rect
.bottom
= render_target
->resource
.height
;
4498 device_invalidate_state(device
, STATE_SCISSORRECT
);
4502 prev
= device
->fb
.render_targets
[render_target_idx
];
4503 if (render_target
== prev
)
4507 wined3d_surface_incref(render_target
);
4508 device
->fb
.render_targets
[render_target_idx
] = render_target
;
4509 /* Release after the assignment, to prevent device_resource_released()
4510 * from seeing the surface as still in use. */
4512 wined3d_surface_decref(prev
);
4514 device_invalidate_state(device
, STATE_FRAMEBUFFER
);
4519 void CDECL
wined3d_device_set_depth_stencil(struct wined3d_device
*device
, struct wined3d_surface
*depth_stencil
)
4521 struct wined3d_surface
*prev
= device
->fb
.depth_stencil
;
4523 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4524 device
, depth_stencil
, prev
);
4526 if (prev
== depth_stencil
)
4528 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4534 if (device
->swapchains
[0]->desc
.flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4535 || prev
->flags
& SFLAG_DISCARD
)
4537 surface_modify_ds_location(prev
, SFLAG_DISCARDED
,
4538 prev
->resource
.width
, prev
->resource
.height
);
4539 if (prev
== device
->onscreen_depth_stencil
)
4541 wined3d_surface_decref(device
->onscreen_depth_stencil
);
4542 device
->onscreen_depth_stencil
= NULL
;
4547 device
->fb
.depth_stencil
= depth_stencil
;
4549 wined3d_surface_incref(depth_stencil
);
4551 if (!prev
!= !depth_stencil
)
4553 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4554 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_ZENABLE
));
4555 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
4556 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK
));
4557 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_DEPTHBIAS
));
4559 else if (prev
&& prev
->resource
.format
->depth_size
!= depth_stencil
->resource
.format
->depth_size
)
4561 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_DEPTHBIAS
));
4564 wined3d_surface_decref(prev
);
4566 device_invalidate_state(device
, STATE_FRAMEBUFFER
);
4571 HRESULT CDECL
wined3d_device_set_cursor_properties(struct wined3d_device
*device
,
4572 UINT x_hotspot
, UINT y_hotspot
, struct wined3d_surface
*cursor_image
)
4574 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4575 device
, x_hotspot
, y_hotspot
, cursor_image
);
4577 /* some basic validation checks */
4578 if (device
->cursorTexture
)
4580 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4581 context
->gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->cursorTexture
);
4582 context_release(context
);
4583 device
->cursorTexture
= 0;
4588 struct wined3d_display_mode mode
;
4589 struct wined3d_map_desc map_desc
;
4592 /* MSDN: Cursor must be A8R8G8B8 */
4593 if (cursor_image
->resource
.format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4595 WARN("surface %p has an invalid format.\n", cursor_image
);
4596 return WINED3DERR_INVALIDCALL
;
4599 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, &mode
, NULL
)))
4601 ERR("Failed to get display mode, hr %#x.\n", hr
);
4602 return WINED3DERR_INVALIDCALL
;
4605 /* MSDN: Cursor must be smaller than the display mode */
4606 if (cursor_image
->resource
.width
> mode
.width
|| cursor_image
->resource
.height
> mode
.height
)
4608 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4609 cursor_image
, cursor_image
->resource
.width
, cursor_image
->resource
.height
,
4610 mode
.width
, mode
.height
);
4611 return WINED3DERR_INVALIDCALL
;
4614 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4616 /* Do not store the surface's pointer because the application may
4617 * release it after setting the cursor image. Windows doesn't
4618 * addref the set surface, so we can't do this either without
4619 * creating circular refcount dependencies. Copy out the gl texture
4621 device
->cursorWidth
= cursor_image
->resource
.width
;
4622 device
->cursorHeight
= cursor_image
->resource
.height
;
4623 if (SUCCEEDED(wined3d_surface_map(cursor_image
, &map_desc
, NULL
, WINED3D_MAP_READONLY
)))
4625 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4626 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
4627 struct wined3d_context
*context
;
4628 char *mem
, *bits
= map_desc
.data
;
4629 GLint intfmt
= format
->glInternal
;
4630 GLint gl_format
= format
->glFormat
;
4631 GLint type
= format
->glType
;
4632 INT height
= device
->cursorHeight
;
4633 INT width
= device
->cursorWidth
;
4634 INT bpp
= format
->byte_count
;
4637 /* Reformat the texture memory (pitch and width can be
4639 mem
= HeapAlloc(GetProcessHeap(), 0, width
* height
* bpp
);
4640 for (i
= 0; i
< height
; ++i
)
4641 memcpy(&mem
[width
* bpp
* i
], &bits
[map_desc
.row_pitch
* i
], width
* bpp
);
4642 wined3d_surface_unmap(cursor_image
);
4644 context
= context_acquire(device
, NULL
);
4646 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
4648 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
4649 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4652 invalidate_active_texture(device
, context
);
4653 /* Create a new cursor texture */
4654 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->cursorTexture
);
4655 checkGLcall("glGenTextures");
4656 context_bind_texture(context
, GL_TEXTURE_2D
, device
->cursorTexture
);
4657 /* Copy the bitmap memory into the cursor texture */
4658 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_2D
, 0, intfmt
, width
, height
, 0, gl_format
, type
, mem
);
4659 checkGLcall("glTexImage2D");
4660 HeapFree(GetProcessHeap(), 0, mem
);
4662 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
4664 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
4665 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4668 context_release(context
);
4672 FIXME("A cursor texture was not returned.\n");
4673 device
->cursorTexture
= 0;
4676 if (cursor_image
->resource
.width
== 32 && cursor_image
->resource
.height
== 32)
4678 UINT mask_size
= cursor_image
->resource
.width
* cursor_image
->resource
.height
/ 8;
4679 ICONINFO cursorInfo
;
4683 /* 32-bit user32 cursors ignore the alpha channel if it's all
4684 * zeroes, and use the mask instead. Fill the mask with all ones
4685 * to ensure we still get a fully transparent cursor. */
4686 maskBits
= HeapAlloc(GetProcessHeap(), 0, mask_size
);
4687 memset(maskBits
, 0xff, mask_size
);
4688 wined3d_surface_map(cursor_image
, &map_desc
, NULL
,
4689 WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
);
4690 TRACE("width: %u height: %u.\n", cursor_image
->resource
.width
, cursor_image
->resource
.height
);
4692 cursorInfo
.fIcon
= FALSE
;
4693 cursorInfo
.xHotspot
= x_hotspot
;
4694 cursorInfo
.yHotspot
= y_hotspot
;
4695 cursorInfo
.hbmMask
= CreateBitmap(cursor_image
->resource
.width
, cursor_image
->resource
.height
,
4697 cursorInfo
.hbmColor
= CreateBitmap(cursor_image
->resource
.width
, cursor_image
->resource
.height
,
4698 1, 32, map_desc
.data
);
4699 wined3d_surface_unmap(cursor_image
);
4700 /* Create our cursor and clean up. */
4701 cursor
= CreateIconIndirect(&cursorInfo
);
4702 if (cursorInfo
.hbmMask
) DeleteObject(cursorInfo
.hbmMask
);
4703 if (cursorInfo
.hbmColor
) DeleteObject(cursorInfo
.hbmColor
);
4704 if (device
->hardwareCursor
) DestroyCursor(device
->hardwareCursor
);
4705 device
->hardwareCursor
= cursor
;
4706 if (device
->bCursorVisible
) SetCursor( cursor
);
4707 HeapFree(GetProcessHeap(), 0, maskBits
);
4711 device
->xHotSpot
= x_hotspot
;
4712 device
->yHotSpot
= y_hotspot
;
4716 void CDECL
wined3d_device_set_cursor_position(struct wined3d_device
*device
,
4717 int x_screen_space
, int y_screen_space
, DWORD flags
)
4719 TRACE("device %p, x %d, y %d, flags %#x.\n",
4720 device
, x_screen_space
, y_screen_space
, flags
);
4722 device
->xScreenSpace
= x_screen_space
;
4723 device
->yScreenSpace
= y_screen_space
;
4725 if (device
->hardwareCursor
)
4729 GetCursorPos( &pt
);
4730 if (x_screen_space
== pt
.x
&& y_screen_space
== pt
.y
)
4732 SetCursorPos( x_screen_space
, y_screen_space
);
4734 /* Switch to the software cursor if position diverges from the hardware one. */
4735 GetCursorPos( &pt
);
4736 if (x_screen_space
!= pt
.x
|| y_screen_space
!= pt
.y
)
4738 if (device
->bCursorVisible
) SetCursor( NULL
);
4739 DestroyCursor( device
->hardwareCursor
);
4740 device
->hardwareCursor
= 0;
4745 BOOL CDECL
wined3d_device_show_cursor(struct wined3d_device
*device
, BOOL show
)
4747 BOOL oldVisible
= device
->bCursorVisible
;
4749 TRACE("device %p, show %#x.\n", device
, show
);
4752 * When ShowCursor is first called it should make the cursor appear at the OS's last
4753 * known cursor position.
4755 if (show
&& !oldVisible
)
4759 device
->xScreenSpace
= pt
.x
;
4760 device
->yScreenSpace
= pt
.y
;
4763 if (device
->hardwareCursor
)
4765 device
->bCursorVisible
= show
;
4767 SetCursor(device
->hardwareCursor
);
4773 if (device
->cursorTexture
)
4774 device
->bCursorVisible
= show
;
4780 void CDECL
wined3d_device_evict_managed_resources(struct wined3d_device
*device
)
4782 struct wined3d_resource
*resource
, *cursor
;
4784 TRACE("device %p.\n", device
);
4786 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4788 TRACE("Checking resource %p for eviction.\n", resource
);
4790 if (resource
->pool
== WINED3D_POOL_MANAGED
&& !resource
->map_count
)
4792 TRACE("Evicting %p.\n", resource
);
4793 resource
->resource_ops
->resource_unload(resource
);
4797 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4798 device_invalidate_state(device
, STATE_STREAMSRC
);
4801 /* Do not call while under the GL lock. */
4802 static void delete_opengl_contexts(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
4804 struct wined3d_resource
*resource
, *cursor
;
4805 const struct wined3d_gl_info
*gl_info
;
4806 struct wined3d_context
*context
;
4807 struct wined3d_shader
*shader
;
4809 context
= context_acquire(device
, NULL
);
4810 gl_info
= context
->gl_info
;
4812 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4814 TRACE("Unloading resource %p.\n", resource
);
4816 resource
->resource_ops
->resource_unload(resource
);
4819 LIST_FOR_EACH_ENTRY(shader
, &device
->shaders
, struct wined3d_shader
, shader_list_entry
)
4821 device
->shader_backend
->shader_destroy(shader
);
4824 if (device
->depth_blt_texture
)
4826 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->depth_blt_texture
);
4827 device
->depth_blt_texture
= 0;
4829 if (device
->cursorTexture
)
4831 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->cursorTexture
);
4832 device
->cursorTexture
= 0;
4835 device
->blitter
->free_private(device
);
4836 device
->shader_backend
->shader_free_private(device
);
4837 destroy_dummy_textures(device
, gl_info
);
4839 context_release(context
);
4841 while (device
->context_count
)
4843 swapchain_destroy_contexts(device
->contexts
[0]->swapchain
);
4846 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
4847 swapchain
->context
= NULL
;
4850 /* Do not call while under the GL lock. */
4851 static HRESULT
create_primary_opengl_context(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
4853 struct wined3d_context
*context
;
4854 struct wined3d_surface
*target
;
4857 if (FAILED(hr
= device
->shader_backend
->shader_alloc_private(device
,
4858 device
->adapter
->vertex_pipe
, device
->adapter
->fragment_pipe
)))
4860 ERR("Failed to allocate shader private data, hr %#x.\n", hr
);
4864 if (FAILED(hr
= device
->blitter
->alloc_private(device
)))
4866 ERR("Failed to allocate blitter private data, hr %#x.\n", hr
);
4867 device
->shader_backend
->shader_free_private(device
);
4871 /* Recreate the primary swapchain's context */
4872 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
4873 if (!swapchain
->context
)
4875 ERR("Failed to allocate memory for swapchain context array.\n");
4876 device
->blitter
->free_private(device
);
4877 device
->shader_backend
->shader_free_private(device
);
4878 return E_OUTOFMEMORY
;
4881 target
= swapchain
->back_buffers
? swapchain
->back_buffers
[0] : swapchain
->front_buffer
;
4882 if (!(context
= context_create(swapchain
, target
, swapchain
->ds_format
)))
4884 WARN("Failed to create context.\n");
4885 device
->blitter
->free_private(device
);
4886 device
->shader_backend
->shader_free_private(device
);
4887 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
4891 swapchain
->context
[0] = context
;
4892 swapchain
->num_contexts
= 1;
4893 create_dummy_textures(device
, context
);
4894 context_release(context
);
4899 /* Do not call while under the GL lock. */
4900 HRESULT CDECL
wined3d_device_reset(struct wined3d_device
*device
,
4901 const struct wined3d_swapchain_desc
*swapchain_desc
, const struct wined3d_display_mode
*mode
,
4902 wined3d_device_reset_cb callback
, BOOL reset_state
)
4904 struct wined3d_resource
*resource
, *cursor
;
4905 struct wined3d_swapchain
*swapchain
;
4906 struct wined3d_display_mode m
;
4907 BOOL DisplayModeChanged
= FALSE
;
4908 BOOL update_desc
= FALSE
;
4909 UINT backbuffer_width
= swapchain_desc
->backbuffer_width
;
4910 UINT backbuffer_height
= swapchain_desc
->backbuffer_height
;
4911 HRESULT hr
= WINED3D_OK
;
4914 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device
, swapchain_desc
, mode
, callback
);
4916 if (!(swapchain
= wined3d_device_get_swapchain(device
, 0)))
4918 ERR("Failed to get the first implicit swapchain.\n");
4919 return WINED3DERR_INVALIDCALL
;
4923 stateblock_unbind_resources(device
->stateBlock
);
4925 if (device
->fb
.render_targets
)
4927 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0])
4928 wined3d_device_set_render_target(device
, 0, swapchain
->back_buffers
[0], FALSE
);
4930 wined3d_device_set_render_target(device
, 0, swapchain
->front_buffer
, FALSE
);
4931 for (i
= 1; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
4933 wined3d_device_set_render_target(device
, i
, NULL
, FALSE
);
4936 wined3d_device_set_depth_stencil(device
, NULL
);
4938 if (device
->onscreen_depth_stencil
)
4940 wined3d_surface_decref(device
->onscreen_depth_stencil
);
4941 device
->onscreen_depth_stencil
= NULL
;
4946 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4948 TRACE("Enumerating resource %p.\n", resource
);
4949 if (FAILED(hr
= callback(resource
)))
4954 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4955 * on an existing gl context, so there's no real need for recreation.
4957 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4959 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4961 TRACE("New params:\n");
4962 TRACE("backbuffer_width %u\n", swapchain_desc
->backbuffer_width
);
4963 TRACE("backbuffer_height %u\n", swapchain_desc
->backbuffer_height
);
4964 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc
->backbuffer_format
));
4965 TRACE("backbuffer_count %u\n", swapchain_desc
->backbuffer_count
);
4966 TRACE("multisample_type %#x\n", swapchain_desc
->multisample_type
);
4967 TRACE("multisample_quality %u\n", swapchain_desc
->multisample_quality
);
4968 TRACE("swap_effect %#x\n", swapchain_desc
->swap_effect
);
4969 TRACE("device_window %p\n", swapchain_desc
->device_window
);
4970 TRACE("windowed %#x\n", swapchain_desc
->windowed
);
4971 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc
->enable_auto_depth_stencil
);
4972 if (swapchain_desc
->enable_auto_depth_stencil
)
4973 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc
->auto_depth_stencil_format
));
4974 TRACE("flags %#x\n", swapchain_desc
->flags
);
4975 TRACE("refresh_rate %u\n", swapchain_desc
->refresh_rate
);
4976 TRACE("swap_interval %u\n", swapchain_desc
->swap_interval
);
4977 TRACE("auto_restore_display_mode %#x\n", swapchain_desc
->auto_restore_display_mode
);
4979 /* No special treatment of these parameters. Just store them */
4980 swapchain
->desc
.swap_effect
= swapchain_desc
->swap_effect
;
4981 swapchain
->desc
.flags
= swapchain_desc
->flags
;
4982 swapchain
->desc
.swap_interval
= swapchain_desc
->swap_interval
;
4983 swapchain
->desc
.refresh_rate
= swapchain_desc
->refresh_rate
;
4985 /* What to do about these? */
4986 if (swapchain_desc
->backbuffer_count
4987 && swapchain_desc
->backbuffer_count
!= swapchain
->desc
.backbuffer_count
)
4988 FIXME("Cannot change the back buffer count yet.\n");
4990 if (swapchain_desc
->device_window
4991 && swapchain_desc
->device_window
!= swapchain
->desc
.device_window
)
4993 TRACE("Changing the device window from %p to %p.\n",
4994 swapchain
->desc
.device_window
, swapchain_desc
->device_window
);
4995 swapchain
->desc
.device_window
= swapchain_desc
->device_window
;
4996 swapchain
->device_window
= swapchain_desc
->device_window
;
4997 wined3d_swapchain_set_window(swapchain
, NULL
);
5000 if (swapchain_desc
->enable_auto_depth_stencil
&& !device
->auto_depth_stencil
)
5002 struct wined3d_resource_desc surface_desc
;
5004 TRACE("Creating the depth stencil buffer\n");
5006 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
5007 surface_desc
.format
= swapchain_desc
->auto_depth_stencil_format
;
5008 surface_desc
.multisample_type
= swapchain_desc
->multisample_type
;
5009 surface_desc
.multisample_quality
= swapchain_desc
->multisample_quality
;
5010 surface_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
5011 surface_desc
.pool
= WINED3D_POOL_DEFAULT
;
5012 surface_desc
.width
= swapchain_desc
->backbuffer_width
;
5013 surface_desc
.height
= swapchain_desc
->backbuffer_height
;
5014 surface_desc
.depth
= 1;
5015 surface_desc
.size
= 0;
5017 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
5018 device
->device_parent
, &surface_desc
, &device
->auto_depth_stencil
)))
5020 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr
);
5021 return WINED3DERR_INVALIDCALL
;
5025 /* Reset the depth stencil */
5026 if (swapchain_desc
->enable_auto_depth_stencil
)
5027 wined3d_device_set_depth_stencil(device
, device
->auto_depth_stencil
);
5031 DisplayModeChanged
= TRUE
;
5034 else if (swapchain_desc
->windowed
)
5036 m
.width
= swapchain
->orig_width
;
5037 m
.height
= swapchain
->orig_height
;
5039 m
.format_id
= swapchain
->desc
.backbuffer_format
;
5040 m
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
5044 m
.width
= swapchain_desc
->backbuffer_width
;
5045 m
.height
= swapchain_desc
->backbuffer_height
;
5046 m
.refresh_rate
= swapchain_desc
->refresh_rate
;
5047 m
.format_id
= swapchain_desc
->backbuffer_format
;
5048 m
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
5051 if (!backbuffer_width
|| !backbuffer_height
)
5053 /* The application is requesting that either the swapchain width or
5054 * height be set to the corresponding dimension in the window's
5059 if (!swapchain_desc
->windowed
)
5060 return WINED3DERR_INVALIDCALL
;
5062 if (!GetClientRect(swapchain
->device_window
, &client_rect
))
5064 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
5065 return WINED3DERR_INVALIDCALL
;
5068 if (!backbuffer_width
)
5069 backbuffer_width
= client_rect
.right
;
5071 if (!backbuffer_height
)
5072 backbuffer_height
= client_rect
.bottom
;
5075 if (backbuffer_width
!= swapchain
->desc
.backbuffer_width
5076 || backbuffer_height
!= swapchain
->desc
.backbuffer_height
)
5078 if (!swapchain_desc
->windowed
)
5079 DisplayModeChanged
= TRUE
;
5081 swapchain
->desc
.backbuffer_width
= backbuffer_width
;
5082 swapchain
->desc
.backbuffer_height
= backbuffer_height
;
5086 if (swapchain_desc
->backbuffer_format
!= WINED3DFMT_UNKNOWN
5087 && swapchain_desc
->backbuffer_format
!= swapchain
->desc
.backbuffer_format
)
5089 swapchain
->desc
.backbuffer_format
= swapchain_desc
->backbuffer_format
;
5093 if (swapchain_desc
->multisample_type
!= swapchain
->desc
.multisample_type
5094 || swapchain_desc
->multisample_quality
!= swapchain
->desc
.multisample_quality
)
5096 swapchain
->desc
.multisample_type
= swapchain_desc
->multisample_type
;
5097 swapchain
->desc
.multisample_quality
= swapchain_desc
->multisample_quality
;
5105 if (FAILED(hr
= wined3d_surface_update_desc(swapchain
->front_buffer
, swapchain
->desc
.backbuffer_width
,
5106 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
5107 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
)))
5110 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
5112 if (FAILED(hr
= wined3d_surface_update_desc(swapchain
->back_buffers
[i
], swapchain
->desc
.backbuffer_width
,
5113 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
5114 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
)))
5117 if (device
->auto_depth_stencil
)
5119 if (FAILED(hr
= wined3d_surface_update_desc(device
->auto_depth_stencil
, swapchain
->desc
.backbuffer_width
,
5120 swapchain
->desc
.backbuffer_height
, device
->auto_depth_stencil
->resource
.format
->id
,
5121 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
)))
5126 if (!swapchain_desc
->windowed
!= !swapchain
->desc
.windowed
5127 || DisplayModeChanged
)
5129 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, &m
)))
5131 WARN("Failed to set display mode, hr %#x.\n", hr
);
5132 return WINED3DERR_INVALIDCALL
;
5135 if (!swapchain_desc
->windowed
)
5137 if (swapchain
->desc
.windowed
)
5139 HWND focus_window
= device
->create_parms
.focus_window
;
5141 focus_window
= swapchain_desc
->device_window
;
5142 if (FAILED(hr
= wined3d_device_acquire_focus_window(device
, focus_window
)))
5144 ERR("Failed to acquire focus window, hr %#x.\n", hr
);
5148 /* switch from windowed to fs */
5149 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
,
5150 swapchain_desc
->backbuffer_width
,
5151 swapchain_desc
->backbuffer_height
);
5155 /* Fullscreen -> fullscreen mode change */
5156 MoveWindow(swapchain
->device_window
, 0, 0,
5157 swapchain_desc
->backbuffer_width
,
5158 swapchain_desc
->backbuffer_height
,
5162 else if (!swapchain
->desc
.windowed
)
5164 /* Fullscreen -> windowed switch */
5165 wined3d_device_restore_fullscreen_window(device
, swapchain
->device_window
);
5166 wined3d_device_release_focus_window(device
);
5168 swapchain
->desc
.windowed
= swapchain_desc
->windowed
;
5170 else if (!swapchain_desc
->windowed
)
5172 DWORD style
= device
->style
;
5173 DWORD exStyle
= device
->exStyle
;
5174 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5175 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5176 * Reset to clear up their mess. Guild Wars also loses the device during that.
5179 device
->exStyle
= 0;
5180 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
,
5181 swapchain_desc
->backbuffer_width
,
5182 swapchain_desc
->backbuffer_height
);
5183 device
->style
= style
;
5184 device
->exStyle
= exStyle
;
5189 TRACE("Resetting stateblock.\n");
5190 wined3d_stateblock_decref(device
->updateStateBlock
);
5191 wined3d_stateblock_decref(device
->stateBlock
);
5193 if (device
->d3d_initialized
)
5194 delete_opengl_contexts(device
, swapchain
);
5196 /* Note: No parent needed for initial internal stateblock */
5197 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_INIT
, &device
->stateBlock
);
5199 ERR("Resetting the stateblock failed with error %#x.\n", hr
);
5201 TRACE("Created stateblock %p.\n", device
->stateBlock
);
5202 device
->updateStateBlock
= device
->stateBlock
;
5203 wined3d_stateblock_incref(device
->updateStateBlock
);
5205 stateblock_init_default_state(device
->stateBlock
);
5209 struct wined3d_surface
*rt
= device
->fb
.render_targets
[0];
5210 struct wined3d_state
*state
= &device
->stateBlock
->state
;
5212 /* Note the min_z / max_z is not reset. */
5213 state
->viewport
.x
= 0;
5214 state
->viewport
.y
= 0;
5215 state
->viewport
.width
= rt
->resource
.width
;
5216 state
->viewport
.height
= rt
->resource
.height
;
5217 device_invalidate_state(device
, STATE_VIEWPORT
);
5219 state
->scissor_rect
.top
= 0;
5220 state
->scissor_rect
.left
= 0;
5221 state
->scissor_rect
.right
= rt
->resource
.width
;
5222 state
->scissor_rect
.bottom
= rt
->resource
.height
;
5223 device_invalidate_state(device
, STATE_SCISSORRECT
);
5226 swapchain_update_render_to_fbo(swapchain
);
5227 swapchain_update_draw_bindings(swapchain
);
5229 if (reset_state
&& device
->d3d_initialized
)
5230 hr
= create_primary_opengl_context(device
, swapchain
);
5232 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5238 HRESULT CDECL
wined3d_device_set_dialog_box_mode(struct wined3d_device
*device
, BOOL enable_dialogs
)
5240 TRACE("device %p, enable_dialogs %#x.\n", device
, enable_dialogs
);
5242 if (!enable_dialogs
) FIXME("Dialogs cannot be disabled yet.\n");
5248 void CDECL
wined3d_device_get_creation_parameters(const struct wined3d_device
*device
,
5249 struct wined3d_device_creation_parameters
*parameters
)
5251 TRACE("device %p, parameters %p.\n", device
, parameters
);
5253 *parameters
= device
->create_parms
;
5256 void CDECL
wined3d_device_set_gamma_ramp(const struct wined3d_device
*device
,
5257 UINT swapchain_idx
, DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
5259 struct wined3d_swapchain
*swapchain
;
5261 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5262 device
, swapchain_idx
, flags
, ramp
);
5264 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
5265 wined3d_swapchain_set_gamma_ramp(swapchain
, flags
, ramp
);
5268 void CDECL
wined3d_device_get_gamma_ramp(const struct wined3d_device
*device
,
5269 UINT swapchain_idx
, struct wined3d_gamma_ramp
*ramp
)
5271 struct wined3d_swapchain
*swapchain
;
5273 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5274 device
, swapchain_idx
, ramp
);
5276 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
5277 wined3d_swapchain_get_gamma_ramp(swapchain
, ramp
);
5280 void device_resource_add(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5282 TRACE("device %p, resource %p.\n", device
, resource
);
5284 list_add_head(&device
->resources
, &resource
->resource_list_entry
);
5287 static void device_resource_remove(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5289 TRACE("device %p, resource %p.\n", device
, resource
);
5291 list_remove(&resource
->resource_list_entry
);
5294 void device_resource_released(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5296 enum wined3d_resource_type type
= resource
->type
;
5299 TRACE("device %p, resource %p, type %s.\n", device
, resource
, debug_d3dresourcetype(type
));
5301 context_resource_released(device
, resource
, type
);
5305 case WINED3D_RTYPE_SURFACE
:
5307 struct wined3d_surface
*surface
= surface_from_resource(resource
);
5309 if (!device
->d3d_initialized
) break;
5311 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
5313 if (device
->fb
.render_targets
[i
] == surface
)
5315 ERR("Surface %p is still in use as render target %u.\n", surface
, i
);
5316 device
->fb
.render_targets
[i
] = NULL
;
5320 if (device
->fb
.depth_stencil
== surface
)
5322 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface
);
5323 device
->fb
.depth_stencil
= NULL
;
5328 case WINED3D_RTYPE_TEXTURE
:
5329 case WINED3D_RTYPE_CUBE_TEXTURE
:
5330 case WINED3D_RTYPE_VOLUME_TEXTURE
:
5331 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
5333 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
5335 if (device
->stateBlock
&& device
->stateBlock
->state
.textures
[i
] == texture
)
5337 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5338 texture
, device
->stateBlock
, i
);
5339 device
->stateBlock
->state
.textures
[i
] = NULL
;
5342 if (device
->updateStateBlock
!= device
->stateBlock
5343 && device
->updateStateBlock
->state
.textures
[i
] == texture
)
5345 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5346 texture
, device
->updateStateBlock
, i
);
5347 device
->updateStateBlock
->state
.textures
[i
] = NULL
;
5352 case WINED3D_RTYPE_BUFFER
:
5354 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
5356 for (i
= 0; i
< MAX_STREAMS
; ++i
)
5358 if (device
->stateBlock
&& device
->stateBlock
->state
.streams
[i
].buffer
== buffer
)
5360 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5361 buffer
, device
->stateBlock
, i
);
5362 device
->stateBlock
->state
.streams
[i
].buffer
= NULL
;
5365 if (device
->updateStateBlock
!= device
->stateBlock
5366 && device
->updateStateBlock
->state
.streams
[i
].buffer
== buffer
)
5368 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5369 buffer
, device
->updateStateBlock
, i
);
5370 device
->updateStateBlock
->state
.streams
[i
].buffer
= NULL
;
5375 if (device
->stateBlock
&& device
->stateBlock
->state
.index_buffer
== buffer
)
5377 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5378 buffer
, device
->stateBlock
);
5379 device
->stateBlock
->state
.index_buffer
= NULL
;
5382 if (device
->updateStateBlock
!= device
->stateBlock
5383 && device
->updateStateBlock
->state
.index_buffer
== buffer
)
5385 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5386 buffer
, device
->updateStateBlock
);
5387 device
->updateStateBlock
->state
.index_buffer
= NULL
;
5396 /* Remove the resource from the resourceStore */
5397 device_resource_remove(device
, resource
);
5399 TRACE("Resource released.\n");
5402 struct wined3d_surface
* CDECL
wined3d_device_get_surface_from_dc(const struct wined3d_device
*device
, HDC dc
)
5404 struct wined3d_resource
*resource
;
5406 TRACE("device %p, dc %p.\n", device
, dc
);
5411 LIST_FOR_EACH_ENTRY(resource
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
5413 if (resource
->type
== WINED3D_RTYPE_SURFACE
)
5415 struct wined3d_surface
*s
= surface_from_resource(resource
);
5419 TRACE("Found surface %p for dc %p.\n", s
, dc
);
5428 HRESULT
device_init(struct wined3d_device
*device
, struct wined3d
*wined3d
,
5429 UINT adapter_idx
, enum wined3d_device_type device_type
, HWND focus_window
, DWORD flags
,
5430 BYTE surface_alignment
, struct wined3d_device_parent
*device_parent
)
5432 struct wined3d_adapter
*adapter
= &wined3d
->adapters
[adapter_idx
];
5433 const struct fragment_pipeline
*fragment_pipeline
;
5434 const struct wined3d_vertex_pipe_ops
*vertex_pipeline
;
5439 device
->wined3d
= wined3d
;
5440 wined3d_incref(device
->wined3d
);
5441 device
->adapter
= wined3d
->adapter_count
? adapter
: NULL
;
5442 device
->device_parent
= device_parent
;
5443 list_init(&device
->resources
);
5444 list_init(&device
->shaders
);
5445 device
->surface_alignment
= surface_alignment
;
5447 /* Save the creation parameters. */
5448 device
->create_parms
.adapter_idx
= adapter_idx
;
5449 device
->create_parms
.device_type
= device_type
;
5450 device
->create_parms
.focus_window
= focus_window
;
5451 device
->create_parms
.flags
= flags
;
5453 device
->shader_backend
= adapter
->shader_backend
;
5455 vertex_pipeline
= adapter
->vertex_pipe
;
5457 fragment_pipeline
= adapter
->fragment_pipe
;
5459 if (vertex_pipeline
->vp_states
&& fragment_pipeline
->states
5460 && FAILED(hr
= compile_state_table(device
->StateTable
, device
->multistate_funcs
,
5461 &adapter
->gl_info
, &adapter
->d3d_info
, vertex_pipeline
,
5462 fragment_pipeline
, misc_state_template
)))
5464 ERR("Failed to compile state table, hr %#x.\n", hr
);
5465 wined3d_decref(device
->wined3d
);
5469 device
->blitter
= adapter
->blitter
;
5471 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_INIT
, &device
->stateBlock
);
5474 WARN("Failed to create stateblock.\n");
5475 for (i
= 0; i
< sizeof(device
->multistate_funcs
) / sizeof(device
->multistate_funcs
[0]); ++i
)
5477 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
5479 wined3d_decref(device
->wined3d
);
5483 TRACE("Created stateblock %p.\n", device
->stateBlock
);
5484 device
->updateStateBlock
= device
->stateBlock
;
5485 wined3d_stateblock_incref(device
->updateStateBlock
);
5491 void device_invalidate_state(const struct wined3d_device
*device
, DWORD state
)
5493 DWORD rep
= device
->StateTable
[state
].representative
;
5494 struct wined3d_context
*context
;
5499 for (i
= 0; i
< device
->context_count
; ++i
)
5501 context
= device
->contexts
[i
];
5502 if(isStateDirty(context
, rep
)) continue;
5504 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
5505 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
5506 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
5507 context
->isStateDirty
[idx
] |= (1 << shift
);
5511 void get_drawable_size_fbo(const struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
5513 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5514 *width
= context
->current_rt
->pow2Width
;
5515 *height
= context
->current_rt
->pow2Height
;
5518 void get_drawable_size_backbuffer(const struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
5520 const struct wined3d_swapchain
*swapchain
= context
->swapchain
;
5521 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5522 * current context's drawable, which is the size of the back buffer of the swapchain
5523 * the active context belongs to. */
5524 *width
= swapchain
->desc
.backbuffer_width
;
5525 *height
= swapchain
->desc
.backbuffer_height
;
5528 LRESULT
device_process_message(struct wined3d_device
*device
, HWND window
, BOOL unicode
,
5529 UINT message
, WPARAM wparam
, LPARAM lparam
, WNDPROC proc
)
5531 if (device
->filter_messages
)
5533 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5534 window
, message
, wparam
, lparam
);
5536 return DefWindowProcW(window
, message
, wparam
, lparam
);
5538 return DefWindowProcA(window
, message
, wparam
, lparam
);
5541 if (message
== WM_DESTROY
)
5543 TRACE("unregister window %p.\n", window
);
5544 wined3d_unregister_window(window
);
5546 if (InterlockedCompareExchangePointer((void **)&device
->focus_window
, NULL
, window
) != window
)
5547 ERR("Window %p is not the focus window for device %p.\n", window
, device
);
5549 else if (message
== WM_DISPLAYCHANGE
)
5551 device
->device_parent
->ops
->mode_changed(device
->device_parent
);
5555 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
5557 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);