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
|| (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
337 device
->useDrawStridedSlow
= TRUE
;
341 device
->useDrawStridedSlow
= FALSE
;
345 if (prev_all_vbo
!= stream_info
->all_vbo
)
346 device_invalidate_state(device
, STATE_INDEXBUFFER
);
349 static void device_preload_texture(const struct wined3d_state
*state
, unsigned int idx
)
351 struct wined3d_texture
*texture
;
352 enum WINED3DSRGB srgb
;
354 if (!(texture
= state
->textures
[idx
])) return;
355 srgb
= state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
] ? SRGB_SRGB
: SRGB_RGB
;
356 texture
->texture_ops
->texture_preload(texture
, srgb
);
359 void device_preload_textures(const struct wined3d_device
*device
)
361 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
366 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
368 if (state
->vertex_shader
->reg_maps
.sampler_type
[i
])
369 device_preload_texture(state
, MAX_FRAGMENT_SAMPLERS
+ i
);
375 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
377 if (state
->pixel_shader
->reg_maps
.sampler_type
[i
])
378 device_preload_texture(state
, i
);
383 WORD ffu_map
= device
->fixed_function_usage_map
;
385 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
388 device_preload_texture(state
, i
);
393 BOOL
device_context_add(struct wined3d_device
*device
, struct wined3d_context
*context
)
395 struct wined3d_context
**new_array
;
397 TRACE("Adding context %p.\n", context
);
399 if (!device
->contexts
) new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
400 else new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
,
401 sizeof(*new_array
) * (device
->context_count
+ 1));
405 ERR("Failed to grow the context array.\n");
409 new_array
[device
->context_count
++] = context
;
410 device
->contexts
= new_array
;
414 void device_context_remove(struct wined3d_device
*device
, struct wined3d_context
*context
)
416 struct wined3d_context
**new_array
;
420 TRACE("Removing context %p.\n", context
);
422 for (i
= 0; i
< device
->context_count
; ++i
)
424 if (device
->contexts
[i
] == context
)
433 ERR("Context %p doesn't exist in context array.\n", context
);
437 if (!--device
->context_count
)
439 HeapFree(GetProcessHeap(), 0, device
->contexts
);
440 device
->contexts
= NULL
;
444 memmove(&device
->contexts
[i
], &device
->contexts
[i
+ 1], (device
->context_count
- i
) * sizeof(*device
->contexts
));
445 new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
, device
->context_count
* sizeof(*device
->contexts
));
448 ERR("Failed to shrink context array. Oh well.\n");
452 device
->contexts
= new_array
;
455 /* Do not call while under the GL lock. */
456 void device_switch_onscreen_ds(struct wined3d_device
*device
,
457 struct wined3d_context
*context
, struct wined3d_surface
*depth_stencil
)
459 if (device
->onscreen_depth_stencil
)
461 surface_load_ds_location(device
->onscreen_depth_stencil
, context
, SFLAG_INTEXTURE
);
463 surface_modify_ds_location(device
->onscreen_depth_stencil
, SFLAG_INTEXTURE
,
464 device
->onscreen_depth_stencil
->ds_current_size
.cx
,
465 device
->onscreen_depth_stencil
->ds_current_size
.cy
);
466 wined3d_surface_decref(device
->onscreen_depth_stencil
);
468 device
->onscreen_depth_stencil
= depth_stencil
;
469 wined3d_surface_incref(device
->onscreen_depth_stencil
);
472 static BOOL
is_full_clear(const struct wined3d_surface
*target
, const RECT
*draw_rect
, const RECT
*clear_rect
)
474 /* partial draw rect */
475 if (draw_rect
->left
|| draw_rect
->top
476 || draw_rect
->right
< target
->resource
.width
477 || draw_rect
->bottom
< target
->resource
.height
)
480 /* partial clear rect */
481 if (clear_rect
&& (clear_rect
->left
> 0 || clear_rect
->top
> 0
482 || clear_rect
->right
< target
->resource
.width
483 || clear_rect
->bottom
< target
->resource
.height
))
489 static void prepare_ds_clear(struct wined3d_surface
*ds
, struct wined3d_context
*context
,
490 DWORD location
, const RECT
*draw_rect
, UINT rect_count
, const RECT
*clear_rect
, RECT
*out_rect
)
492 RECT current_rect
, r
;
494 if (ds
->flags
& SFLAG_DISCARDED
)
496 /* Depth buffer was discarded, make it entirely current in its new location since
497 * there is no other place where we would get data anyway. */
498 SetRect(out_rect
, 0, 0, ds
->resource
.width
, ds
->resource
.height
);
502 if (ds
->flags
& location
)
503 SetRect(¤t_rect
, 0, 0,
504 ds
->ds_current_size
.cx
,
505 ds
->ds_current_size
.cy
);
507 SetRectEmpty(¤t_rect
);
509 IntersectRect(&r
, draw_rect
, ¤t_rect
);
510 if (EqualRect(&r
, draw_rect
))
512 /* current_rect ⊇ draw_rect, modify only. */
513 SetRect(out_rect
, 0, 0, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
517 if (EqualRect(&r
, ¤t_rect
))
519 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
523 /* Full clear, modify only. */
524 *out_rect
= *draw_rect
;
528 IntersectRect(&r
, draw_rect
, clear_rect
);
529 if (EqualRect(&r
, draw_rect
))
531 /* clear_rect ⊇ draw_rect, modify only. */
532 *out_rect
= *draw_rect
;
538 surface_load_ds_location(ds
, context
, location
);
539 SetRect(out_rect
, 0, 0, ds
->ds_current_size
.cx
, ds
->ds_current_size
.cy
);
542 /* Do not call while under the GL lock. */
543 void device_clear_render_targets(struct wined3d_device
*device
, UINT rt_count
, const struct wined3d_fb_state
*fb
,
544 UINT rect_count
, const RECT
*rects
, const RECT
*draw_rect
, DWORD flags
, const struct wined3d_color
*color
,
545 float depth
, DWORD stencil
)
547 const RECT
*clear_rect
= (rect_count
> 0 && rects
) ? (const RECT
*)rects
: NULL
;
548 struct wined3d_surface
*target
= rt_count
? fb
->render_targets
[0] : NULL
;
549 const struct wined3d_gl_info
*gl_info
;
550 UINT drawable_width
, drawable_height
;
551 struct wined3d_context
*context
;
552 GLbitfield clear_mask
= 0;
553 BOOL render_offscreen
;
557 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
558 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
559 * for the cleared parts, and the untouched parts.
561 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
562 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
563 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
564 * checking all this if the dest surface is in the drawable anyway. */
565 if (flags
& WINED3DCLEAR_TARGET
&& !is_full_clear(target
, draw_rect
, clear_rect
))
567 for (i
= 0; i
< rt_count
; ++i
)
569 struct wined3d_surface
*rt
= fb
->render_targets
[i
];
571 surface_load_location(rt
, rt
->draw_binding
, NULL
);
575 context
= context_acquire(device
, target
);
578 context_release(context
);
579 WARN("Invalid context, skipping clear.\n");
582 gl_info
= context
->gl_info
;
586 render_offscreen
= context
->render_offscreen
;
587 target
->get_drawable_size(context
, &drawable_width
, &drawable_height
);
591 render_offscreen
= TRUE
;
592 drawable_width
= fb
->depth_stencil
->pow2Width
;
593 drawable_height
= fb
->depth_stencil
->pow2Height
;
596 if (flags
& WINED3DCLEAR_ZBUFFER
)
598 DWORD location
= render_offscreen
? fb
->depth_stencil
->draw_binding
: SFLAG_INDRAWABLE
;
600 if (!render_offscreen
&& fb
->depth_stencil
!= device
->onscreen_depth_stencil
)
601 device_switch_onscreen_ds(device
, context
, fb
->depth_stencil
);
602 prepare_ds_clear(fb
->depth_stencil
, context
, location
,
603 draw_rect
, rect_count
, clear_rect
, &ds_rect
);
606 if (!context_apply_clear_state(context
, device
, rt_count
, fb
))
608 context_release(context
);
609 WARN("Failed to apply clear state, skipping clear.\n");
613 /* Only set the values up once, as they are not changing. */
614 if (flags
& WINED3DCLEAR_STENCIL
)
616 if (gl_info
->supported
[EXT_STENCIL_TWO_SIDE
])
618 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT
);
619 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE
));
621 gl_info
->gl_ops
.gl
.p_glStencilMask(~0U);
622 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK
));
623 gl_info
->gl_ops
.gl
.p_glClearStencil(stencil
);
624 checkGLcall("glClearStencil");
625 clear_mask
= clear_mask
| GL_STENCIL_BUFFER_BIT
;
628 if (flags
& WINED3DCLEAR_ZBUFFER
)
630 DWORD location
= render_offscreen
? fb
->depth_stencil
->draw_binding
: SFLAG_INDRAWABLE
;
632 surface_modify_ds_location(fb
->depth_stencil
, location
, ds_rect
.right
, ds_rect
.bottom
);
634 gl_info
->gl_ops
.gl
.p_glDepthMask(GL_TRUE
);
635 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZWRITEENABLE
));
636 gl_info
->gl_ops
.gl
.p_glClearDepth(depth
);
637 checkGLcall("glClearDepth");
638 clear_mask
= clear_mask
| GL_DEPTH_BUFFER_BIT
;
641 if (flags
& WINED3DCLEAR_TARGET
)
643 for (i
= 0; i
< rt_count
; ++i
)
645 struct wined3d_surface
*rt
= fb
->render_targets
[i
];
648 surface_modify_location(rt
, rt
->draw_binding
, TRUE
);
651 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
652 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
653 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
654 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
655 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
656 gl_info
->gl_ops
.gl
.p_glClearColor(color
->r
, color
->g
, color
->b
, color
->a
);
657 checkGLcall("glClearColor");
658 clear_mask
= clear_mask
| GL_COLOR_BUFFER_BIT
;
663 if (render_offscreen
)
665 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, draw_rect
->top
,
666 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
670 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, drawable_height
- draw_rect
->bottom
,
671 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
673 checkGLcall("glScissor");
674 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
675 checkGLcall("glClear");
681 /* Now process each rect in turn. */
682 for (i
= 0; i
< rect_count
; ++i
)
684 /* Note that GL uses lower left, width/height. */
685 IntersectRect(¤t_rect
, draw_rect
, &clear_rect
[i
]);
687 TRACE("clear_rect[%u] %s, current_rect %s.\n", i
,
688 wine_dbgstr_rect(&clear_rect
[i
]),
689 wine_dbgstr_rect(¤t_rect
));
691 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
692 * The rectangle is not cleared, no error is returned, but further rectangles are
693 * still cleared if they are valid. */
694 if (current_rect
.left
> current_rect
.right
|| current_rect
.top
> current_rect
.bottom
)
696 TRACE("Rectangle with negative dimensions, ignoring.\n");
700 if (render_offscreen
)
702 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, current_rect
.top
,
703 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
707 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, drawable_height
- current_rect
.bottom
,
708 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
710 checkGLcall("glScissor");
712 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
713 checkGLcall("glClear");
717 if (wined3d_settings
.strict_draw_ordering
|| (flags
& WINED3DCLEAR_TARGET
718 && target
->swapchain
&& target
->swapchain
->front_buffer
== target
))
719 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
721 context_release(context
);
724 ULONG CDECL
wined3d_device_incref(struct wined3d_device
*device
)
726 ULONG refcount
= InterlockedIncrement(&device
->ref
);
728 TRACE("%p increasing refcount to %u.\n", device
, refcount
);
733 ULONG CDECL
wined3d_device_decref(struct wined3d_device
*device
)
735 ULONG refcount
= InterlockedDecrement(&device
->ref
);
737 TRACE("%p decreasing refcount to %u.\n", device
, refcount
);
741 struct wined3d_stateblock
*stateblock
;
744 if (wined3d_stateblock_decref(device
->updateStateBlock
)
745 && device
->updateStateBlock
!= device
->stateBlock
)
746 FIXME("Something's still holding the update stateblock.\n");
747 device
->updateStateBlock
= NULL
;
749 stateblock
= device
->stateBlock
;
750 device
->stateBlock
= NULL
;
751 if (wined3d_stateblock_decref(stateblock
))
752 FIXME("Something's still holding the stateblock.\n");
754 for (i
= 0; i
< sizeof(device
->multistate_funcs
) / sizeof(device
->multistate_funcs
[0]); ++i
)
756 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
757 device
->multistate_funcs
[i
] = NULL
;
760 if (!list_empty(&device
->resources
))
762 struct wined3d_resource
*resource
;
764 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
766 LIST_FOR_EACH_ENTRY(resource
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
768 FIXME("Leftover resource %p with type %s (%#x).\n",
769 resource
, debug_d3dresourcetype(resource
->type
), resource
->type
);
773 if (device
->contexts
)
774 ERR("Context array not freed!\n");
775 if (device
->hardwareCursor
)
776 DestroyCursor(device
->hardwareCursor
);
777 device
->hardwareCursor
= 0;
779 wined3d_decref(device
->wined3d
);
780 device
->wined3d
= NULL
;
781 HeapFree(GetProcessHeap(), 0, device
);
782 TRACE("Freed device %p.\n", device
);
788 UINT CDECL
wined3d_device_get_swapchain_count(const struct wined3d_device
*device
)
790 TRACE("device %p.\n", device
);
792 return device
->swapchain_count
;
795 struct wined3d_swapchain
* CDECL
wined3d_device_get_swapchain(const struct wined3d_device
*device
, UINT swapchain_idx
)
797 TRACE("device %p, swapchain_idx %u.\n", device
, swapchain_idx
);
799 if (swapchain_idx
>= device
->swapchain_count
)
801 WARN("swapchain_idx %u >= swapchain_count %u.\n",
802 swapchain_idx
, device
->swapchain_count
);
806 return device
->swapchains
[swapchain_idx
];
809 static void device_load_logo(struct wined3d_device
*device
, const char *filename
)
811 struct wined3d_color_key color_key
;
815 HDC dcb
= NULL
, dcs
= NULL
;
817 hbm
= LoadImageA(NULL
, filename
, IMAGE_BITMAP
, 0, 0, LR_LOADFROMFILE
| LR_CREATEDIBSECTION
);
820 GetObjectA(hbm
, sizeof(BITMAP
), &bm
);
821 dcb
= CreateCompatibleDC(NULL
);
823 SelectObject(dcb
, hbm
);
827 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
830 memset(&bm
, 0, sizeof(bm
));
835 hr
= wined3d_surface_create(device
, bm
.bmWidth
, bm
.bmHeight
, WINED3DFMT_B5G6R5_UNORM
, 0,
836 WINED3D_POOL_SYSTEM_MEM
, WINED3D_MULTISAMPLE_NONE
, 0, WINED3D_SURFACE_MAPPABLE
,
837 NULL
, &wined3d_null_parent_ops
, &device
->logo_surface
);
840 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr
);
846 if (FAILED(hr
= wined3d_surface_getdc(device
->logo_surface
, &dcs
)))
848 BitBlt(dcs
, 0, 0, bm
.bmWidth
, bm
.bmHeight
, dcb
, 0, 0, SRCCOPY
);
849 wined3d_surface_releasedc(device
->logo_surface
, dcs
);
851 color_key
.color_space_low_value
= 0;
852 color_key
.color_space_high_value
= 0;
853 wined3d_surface_set_color_key(device
->logo_surface
, WINEDDCKEY_SRCBLT
, &color_key
);
857 const struct wined3d_color c
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
858 /* Fill the surface with a white color to show that wined3d is there */
859 wined3d_device_color_fill(device
, device
->logo_surface
, NULL
, &c
);
863 if (dcb
) DeleteDC(dcb
);
864 if (hbm
) DeleteObject(hbm
);
867 /* Context activation is done by the caller. */
868 static void create_dummy_textures(struct wined3d_device
*device
, struct wined3d_context
*context
)
870 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
871 unsigned int i
, j
, count
;
872 /* Under DirectX you can sample even if no texture is bound, whereas
873 * OpenGL will only allow that when a valid texture is bound.
874 * We emulate this by creating dummy textures and binding them
875 * to each texture stage when the currently set D3D texture is NULL. */
877 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
879 /* The dummy texture does not have client storage backing */
880 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
881 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
884 count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
885 for (i
= 0; i
< count
; ++i
)
887 DWORD color
= 0x000000ff;
889 /* Make appropriate texture active */
890 context_active_texture(context
, gl_info
, i
);
892 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_2d
[i
]);
893 checkGLcall("glGenTextures");
894 TRACE("Dummy 2D texture %u given name %u.\n", i
, device
->dummy_texture_2d
[i
]);
896 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
897 checkGLcall("glBindTexture");
899 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA8
, 1, 1, 0,
900 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
901 checkGLcall("glTexImage2D");
903 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
905 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_rect
[i
]);
906 checkGLcall("glGenTextures");
907 TRACE("Dummy rectangle texture %u given name %u.\n", i
, device
->dummy_texture_rect
[i
]);
909 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
910 checkGLcall("glBindTexture");
912 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA8
, 1, 1, 0,
913 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
914 checkGLcall("glTexImage2D");
917 if (gl_info
->supported
[EXT_TEXTURE3D
])
919 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_3d
[i
]);
920 checkGLcall("glGenTextures");
921 TRACE("Dummy 3D texture %u given name %u.\n", i
, device
->dummy_texture_3d
[i
]);
923 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
924 checkGLcall("glBindTexture");
926 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D
, 0, GL_RGBA8
, 1, 1, 1, 0, GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
));
927 checkGLcall("glTexImage3D");
930 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
932 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_texture_cube
[i
]);
933 checkGLcall("glGenTextures");
934 TRACE("Dummy cube texture %u given name %u.\n", i
, device
->dummy_texture_cube
[i
]);
936 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
937 checkGLcall("glBindTexture");
939 for (j
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
; j
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
; ++j
)
941 gl_info
->gl_ops
.gl
.p_glTexImage2D(j
, 0, GL_RGBA8
, 1, 1, 0,
942 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
943 checkGLcall("glTexImage2D");
948 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
950 /* Re-enable because if supported it is enabled by default */
951 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
952 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
956 /* Context activation is done by the caller. */
957 static void destroy_dummy_textures(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
)
959 unsigned int count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
961 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
963 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_cube
);
964 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
967 if (gl_info
->supported
[EXT_TEXTURE3D
])
969 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_3d
);
970 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
973 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
975 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_rect
);
976 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
979 gl_info
->gl_ops
.gl
.p_glDeleteTextures(count
, device
->dummy_texture_2d
);
980 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
982 memset(device
->dummy_texture_cube
, 0, count
* sizeof(*device
->dummy_texture_cube
));
983 memset(device
->dummy_texture_3d
, 0, count
* sizeof(*device
->dummy_texture_3d
));
984 memset(device
->dummy_texture_rect
, 0, count
* sizeof(*device
->dummy_texture_rect
));
985 memset(device
->dummy_texture_2d
, 0, count
* sizeof(*device
->dummy_texture_2d
));
988 static LONG
fullscreen_style(LONG style
)
990 /* Make sure the window is managed, otherwise we won't get keyboard input. */
991 style
|= WS_POPUP
| WS_SYSMENU
;
992 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
997 static LONG
fullscreen_exstyle(LONG exstyle
)
999 /* Filter out window decorations. */
1000 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
1005 void CDECL
wined3d_device_setup_fullscreen_window(struct wined3d_device
*device
, HWND window
, UINT w
, UINT h
)
1007 BOOL filter_messages
;
1008 LONG style
, exstyle
;
1010 TRACE("Setting up window %p for fullscreen mode.\n", window
);
1012 if (device
->style
|| device
->exStyle
)
1014 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1015 window
, device
->style
, device
->exStyle
);
1018 device
->style
= GetWindowLongW(window
, GWL_STYLE
);
1019 device
->exStyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
1021 style
= fullscreen_style(device
->style
);
1022 exstyle
= fullscreen_exstyle(device
->exStyle
);
1024 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1025 device
->style
, device
->exStyle
, style
, exstyle
);
1027 filter_messages
= device
->filter_messages
;
1028 device
->filter_messages
= TRUE
;
1030 SetWindowLongW(window
, GWL_STYLE
, style
);
1031 SetWindowLongW(window
, GWL_EXSTYLE
, exstyle
);
1032 SetWindowPos(window
, HWND_TOPMOST
, 0, 0, w
, h
, SWP_FRAMECHANGED
| SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
1034 device
->filter_messages
= filter_messages
;
1037 void CDECL
wined3d_device_restore_fullscreen_window(struct wined3d_device
*device
, HWND window
)
1039 BOOL filter_messages
;
1040 LONG style
, exstyle
;
1042 if (!device
->style
&& !device
->exStyle
) return;
1044 style
= GetWindowLongW(window
, GWL_STYLE
);
1045 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
1047 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
1048 * application, and we want to ignore them in the test below, since it's
1049 * not the application's fault that they changed. Additionally, we want to
1050 * preserve the current status of these flags (i.e. don't restore them) to
1051 * more closely emulate the behavior of Direct3D, which leaves these flags
1052 * alone when returning to windowed mode. */
1053 device
->style
^= (device
->style
^ style
) & WS_VISIBLE
;
1054 device
->exStyle
^= (device
->exStyle
^ exstyle
) & WS_EX_TOPMOST
;
1056 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1057 window
, device
->style
, device
->exStyle
);
1059 filter_messages
= device
->filter_messages
;
1060 device
->filter_messages
= TRUE
;
1062 /* Only restore the style if the application didn't modify it during the
1063 * fullscreen phase. Some applications change it before calling Reset()
1064 * when switching between windowed and fullscreen modes (HL2), some
1065 * depend on the original style (Eve Online). */
1066 if (style
== fullscreen_style(device
->style
) && exstyle
== fullscreen_exstyle(device
->exStyle
))
1068 SetWindowLongW(window
, GWL_STYLE
, device
->style
);
1069 SetWindowLongW(window
, GWL_EXSTYLE
, device
->exStyle
);
1071 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
| SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
1073 device
->filter_messages
= filter_messages
;
1075 /* Delete the old values. */
1077 device
->exStyle
= 0;
1080 HRESULT CDECL
wined3d_device_acquire_focus_window(struct wined3d_device
*device
, HWND window
)
1082 TRACE("device %p, window %p.\n", device
, window
);
1084 if (!wined3d_register_window(window
, device
))
1086 ERR("Failed to register window %p.\n", window
);
1090 InterlockedExchangePointer((void **)&device
->focus_window
, window
);
1091 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
);
1096 void CDECL
wined3d_device_release_focus_window(struct wined3d_device
*device
)
1098 TRACE("device %p.\n", device
);
1100 if (device
->focus_window
) wined3d_unregister_window(device
->focus_window
);
1101 InterlockedExchangePointer((void **)&device
->focus_window
, NULL
);
1104 HRESULT CDECL
wined3d_device_init_3d(struct wined3d_device
*device
,
1105 struct wined3d_swapchain_desc
*swapchain_desc
)
1107 static const struct wined3d_color black
= {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1108 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1109 struct wined3d_swapchain
*swapchain
= NULL
;
1110 struct wined3d_context
*context
;
1114 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1116 if (device
->d3d_initialized
)
1117 return WINED3DERR_INVALIDCALL
;
1118 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1119 return WINED3DERR_INVALIDCALL
;
1121 device
->fb
.render_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1122 sizeof(*device
->fb
.render_targets
) * gl_info
->limits
.buffers
);
1124 /* Initialize the texture unit mapping to a 1:1 mapping */
1125 for (state
= 0; state
< MAX_COMBINED_SAMPLERS
; ++state
)
1127 if (state
< gl_info
->limits
.fragment_samplers
)
1129 device
->texUnitMap
[state
] = state
;
1130 device
->rev_tex_unit_map
[state
] = state
;
1134 device
->texUnitMap
[state
] = WINED3D_UNMAPPED_STAGE
;
1135 device
->rev_tex_unit_map
[state
] = WINED3D_UNMAPPED_STAGE
;
1139 if (FAILED(hr
= device
->shader_backend
->shader_alloc_private(device
,
1140 device
->adapter
->vertex_pipe
, device
->adapter
->fragment_pipe
)))
1142 TRACE("Shader private data couldn't be allocated\n");
1145 if (FAILED(hr
= device
->blitter
->alloc_private(device
)))
1147 TRACE("Blitter private data couldn't be allocated\n");
1151 /* Setup the implicit swapchain. This also initializes a context. */
1152 TRACE("Creating implicit swapchain\n");
1153 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1154 swapchain_desc
, &swapchain
);
1157 WARN("Failed to create implicit swapchain\n");
1161 device
->swapchain_count
= 1;
1162 device
->swapchains
= HeapAlloc(GetProcessHeap(), 0, device
->swapchain_count
* sizeof(*device
->swapchains
));
1163 if (!device
->swapchains
)
1165 ERR("Out of memory!\n");
1168 device
->swapchains
[0] = swapchain
;
1170 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0])
1172 TRACE("Setting rendertarget to %p.\n", swapchain
->back_buffers
);
1173 device
->fb
.render_targets
[0] = swapchain
->back_buffers
[0];
1177 TRACE("Setting rendertarget to %p.\n", swapchain
->front_buffer
);
1178 device
->fb
.render_targets
[0] = swapchain
->front_buffer
;
1180 wined3d_surface_incref(device
->fb
.render_targets
[0]);
1182 /* Depth Stencil support */
1183 device
->fb
.depth_stencil
= device
->auto_depth_stencil
;
1184 if (device
->fb
.depth_stencil
)
1185 wined3d_surface_incref(device
->fb
.depth_stencil
);
1187 /* Set up some starting GL setup */
1189 /* Setup all the devices defaults */
1190 stateblock_init_default_state(device
->stateBlock
);
1192 context
= context_acquire(device
, swapchain
->front_buffer
);
1194 create_dummy_textures(device
, context
);
1196 device
->contexts
[0]->last_was_rhw
= 0;
1198 switch (wined3d_settings
.offscreen_rendering_mode
)
1201 device
->offscreenBuffer
= GL_COLOR_ATTACHMENT0
;
1204 case ORM_BACKBUFFER
:
1206 if (context_get_current()->aux_buffers
> 0)
1208 TRACE("Using auxiliary buffer for offscreen rendering\n");
1209 device
->offscreenBuffer
= GL_AUX0
;
1213 TRACE("Using back buffer for offscreen rendering\n");
1214 device
->offscreenBuffer
= GL_BACK
;
1219 TRACE("All defaults now set up, leaving 3D init.\n");
1221 context_release(context
);
1223 /* Clear the screen */
1224 wined3d_device_clear(device
, 0, NULL
, WINED3DCLEAR_TARGET
1225 | (swapchain_desc
->enable_auto_depth_stencil
? WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
: 0),
1228 device
->d3d_initialized
= TRUE
;
1230 if (wined3d_settings
.logo
)
1231 device_load_logo(device
, wined3d_settings
.logo
);
1235 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1236 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1237 device
->swapchain_count
= 0;
1239 wined3d_swapchain_decref(swapchain
);
1240 if (device
->blit_priv
)
1241 device
->blitter
->free_private(device
);
1242 if (device
->shader_priv
)
1243 device
->shader_backend
->shader_free_private(device
);
1248 HRESULT CDECL
wined3d_device_init_gdi(struct wined3d_device
*device
,
1249 struct wined3d_swapchain_desc
*swapchain_desc
)
1251 struct wined3d_swapchain
*swapchain
= NULL
;
1254 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1256 /* Setup the implicit swapchain */
1257 TRACE("Creating implicit swapchain\n");
1258 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1259 swapchain_desc
, &swapchain
);
1262 WARN("Failed to create implicit swapchain\n");
1266 device
->swapchain_count
= 1;
1267 device
->swapchains
= HeapAlloc(GetProcessHeap(), 0, device
->swapchain_count
* sizeof(*device
->swapchains
));
1268 if (!device
->swapchains
)
1270 ERR("Out of memory!\n");
1273 device
->swapchains
[0] = swapchain
;
1277 wined3d_swapchain_decref(swapchain
);
1281 HRESULT CDECL
wined3d_device_uninit_3d(struct wined3d_device
*device
)
1283 struct wined3d_resource
*resource
, *cursor
;
1284 const struct wined3d_gl_info
*gl_info
;
1285 struct wined3d_context
*context
;
1286 struct wined3d_surface
*surface
;
1289 TRACE("device %p.\n", device
);
1291 if (!device
->d3d_initialized
)
1292 return WINED3DERR_INVALIDCALL
;
1294 /* Force making the context current again, to verify it is still valid
1295 * (workaround for broken drivers) */
1296 context_set_current(NULL
);
1297 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1298 * it was created. Thus make sure a context is active for the glDelete* calls
1300 context
= context_acquire(device
, NULL
);
1301 gl_info
= context
->gl_info
;
1303 if (device
->logo_surface
)
1304 wined3d_surface_decref(device
->logo_surface
);
1306 stateblock_unbind_resources(device
->stateBlock
);
1308 /* Unload resources */
1309 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
1311 TRACE("Unloading resource %p.\n", resource
);
1313 resource
->resource_ops
->resource_unload(resource
);
1316 /* Delete the mouse cursor texture */
1317 if (device
->cursorTexture
)
1319 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->cursorTexture
);
1320 device
->cursorTexture
= 0;
1323 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1324 * private data, it might contain opengl pointers
1326 if (device
->depth_blt_texture
)
1328 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->depth_blt_texture
);
1329 device
->depth_blt_texture
= 0;
1332 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1333 device
->blitter
->free_private(device
);
1334 device
->shader_backend
->shader_free_private(device
);
1336 /* Release the buffers (with sanity checks)*/
1337 if (device
->onscreen_depth_stencil
)
1339 surface
= device
->onscreen_depth_stencil
;
1340 device
->onscreen_depth_stencil
= NULL
;
1341 wined3d_surface_decref(surface
);
1344 if (device
->fb
.depth_stencil
)
1346 surface
= device
->fb
.depth_stencil
;
1348 TRACE("Releasing depth/stencil buffer %p.\n", surface
);
1350 device
->fb
.depth_stencil
= NULL
;
1351 wined3d_surface_decref(surface
);
1354 if (device
->auto_depth_stencil
)
1356 surface
= device
->auto_depth_stencil
;
1357 device
->auto_depth_stencil
= NULL
;
1358 if (wined3d_surface_decref(surface
))
1359 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface
);
1362 for (i
= 1; i
< gl_info
->limits
.buffers
; ++i
)
1364 wined3d_device_set_render_target(device
, i
, NULL
, FALSE
);
1367 surface
= device
->fb
.render_targets
[0];
1368 TRACE("Setting rendertarget 0 to NULL\n");
1369 device
->fb
.render_targets
[0] = NULL
;
1370 TRACE("Releasing the render target at %p\n", surface
);
1371 wined3d_surface_decref(surface
);
1373 context_release(context
);
1375 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1377 TRACE("Releasing the implicit swapchain %u.\n", i
);
1378 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1379 FIXME("Something's still holding the implicit swapchain.\n");
1382 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1383 device
->swapchains
= NULL
;
1384 device
->swapchain_count
= 0;
1386 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1387 device
->fb
.render_targets
= NULL
;
1389 device
->d3d_initialized
= FALSE
;
1394 HRESULT CDECL
wined3d_device_uninit_gdi(struct wined3d_device
*device
)
1398 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1400 TRACE("Releasing the implicit swapchain %u.\n", i
);
1401 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1402 FIXME("Something's still holding the implicit swapchain.\n");
1405 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1406 device
->swapchains
= NULL
;
1407 device
->swapchain_count
= 0;
1411 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1412 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1413 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1415 * There is no way to deactivate thread safety once it is enabled.
1417 void CDECL
wined3d_device_set_multithreaded(struct wined3d_device
*device
)
1419 TRACE("device %p.\n", device
);
1421 /* For now just store the flag (needed in case of ddraw). */
1422 device
->create_parms
.flags
|= WINED3DCREATE_MULTITHREADED
;
1425 UINT CDECL
wined3d_device_get_available_texture_mem(const struct wined3d_device
*device
)
1427 TRACE("device %p.\n", device
);
1429 TRACE("Emulating %d MB, returning %d MB left.\n",
1430 device
->adapter
->TextureRam
/ (1024 * 1024),
1431 (device
->adapter
->TextureRam
- device
->adapter
->UsedTextureRam
) / (1024 * 1024));
1433 return device
->adapter
->TextureRam
- device
->adapter
->UsedTextureRam
;
1436 void CDECL
wined3d_device_set_stream_output(struct wined3d_device
*device
, UINT idx
,
1437 struct wined3d_buffer
*buffer
, UINT offset
)
1439 struct wined3d_buffer
*prev_buffer
;
1441 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device
, idx
, buffer
, offset
);
1443 if (idx
>= MAX_STREAM_OUT
)
1445 WARN("Invalid stream output %u.\n", idx
);
1449 prev_buffer
= device
->updateStateBlock
->state
.stream_output
[idx
].buffer
;
1450 device
->updateStateBlock
->state
.stream_output
[idx
].buffer
= buffer
;
1451 device
->updateStateBlock
->state
.stream_output
[idx
].offset
= offset
;
1453 if (device
->isRecordingState
)
1456 wined3d_buffer_incref(buffer
);
1458 wined3d_buffer_decref(prev_buffer
);
1462 if (prev_buffer
!= buffer
)
1466 InterlockedIncrement(&buffer
->resource
.bind_count
);
1467 wined3d_buffer_incref(buffer
);
1471 InterlockedDecrement(&prev_buffer
->resource
.bind_count
);
1472 wined3d_buffer_decref(prev_buffer
);
1477 struct wined3d_buffer
* CDECL
wined3d_device_get_stream_output(struct wined3d_device
*device
,
1478 UINT idx
, UINT
*offset
)
1480 TRACE("device %p, idx %u, offset %p.\n", device
, idx
, offset
);
1482 if (idx
>= MAX_STREAM_OUT
)
1484 WARN("Invalid stream output %u.\n", idx
);
1488 *offset
= device
->stateBlock
->state
.stream_output
[idx
].offset
;
1489 return device
->stateBlock
->state
.stream_output
[idx
].buffer
;
1492 HRESULT CDECL
wined3d_device_set_stream_source(struct wined3d_device
*device
, UINT stream_idx
,
1493 struct wined3d_buffer
*buffer
, UINT offset
, UINT stride
)
1495 struct wined3d_stream_state
*stream
;
1496 struct wined3d_buffer
*prev_buffer
;
1498 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1499 device
, stream_idx
, buffer
, offset
, stride
);
1501 if (stream_idx
>= MAX_STREAMS
)
1503 WARN("Stream index %u out of range.\n", stream_idx
);
1504 return WINED3DERR_INVALIDCALL
;
1506 else if (offset
& 0x3)
1508 WARN("Offset %u is not 4 byte aligned.\n", offset
);
1509 return WINED3DERR_INVALIDCALL
;
1512 stream
= &device
->updateStateBlock
->state
.streams
[stream_idx
];
1513 prev_buffer
= stream
->buffer
;
1515 device
->updateStateBlock
->changed
.streamSource
|= 1 << stream_idx
;
1517 if (prev_buffer
== buffer
1518 && stream
->stride
== stride
1519 && stream
->offset
== offset
)
1521 TRACE("Application is setting the old values over, nothing to do.\n");
1525 stream
->buffer
= buffer
;
1528 stream
->stride
= stride
;
1529 stream
->offset
= offset
;
1532 /* Handle recording of state blocks. */
1533 if (device
->isRecordingState
)
1535 TRACE("Recording... not performing anything.\n");
1537 wined3d_buffer_incref(buffer
);
1539 wined3d_buffer_decref(prev_buffer
);
1545 InterlockedIncrement(&buffer
->resource
.bind_count
);
1546 wined3d_buffer_incref(buffer
);
1550 InterlockedDecrement(&prev_buffer
->resource
.bind_count
);
1551 wined3d_buffer_decref(prev_buffer
);
1554 device_invalidate_state(device
, STATE_STREAMSRC
);
1559 HRESULT CDECL
wined3d_device_get_stream_source(const struct wined3d_device
*device
,
1560 UINT stream_idx
, struct wined3d_buffer
**buffer
, UINT
*offset
, UINT
*stride
)
1562 struct wined3d_stream_state
*stream
;
1564 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1565 device
, stream_idx
, buffer
, offset
, stride
);
1567 if (stream_idx
>= MAX_STREAMS
)
1569 WARN("Stream index %u out of range.\n", stream_idx
);
1570 return WINED3DERR_INVALIDCALL
;
1573 stream
= &device
->stateBlock
->state
.streams
[stream_idx
];
1574 *buffer
= stream
->buffer
;
1576 wined3d_buffer_incref(*buffer
);
1578 *offset
= stream
->offset
;
1579 *stride
= stream
->stride
;
1584 HRESULT CDECL
wined3d_device_set_stream_source_freq(struct wined3d_device
*device
, UINT stream_idx
, UINT divider
)
1586 struct wined3d_stream_state
*stream
;
1587 UINT old_flags
, old_freq
;
1589 TRACE("device %p, stream_idx %u, divider %#x.\n", device
, stream_idx
, divider
);
1591 /* Verify input. At least in d3d9 this is invalid. */
1592 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && (divider
& WINED3DSTREAMSOURCE_INDEXEDDATA
))
1594 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1595 return WINED3DERR_INVALIDCALL
;
1597 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && !stream_idx
)
1599 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1600 return WINED3DERR_INVALIDCALL
;
1604 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1605 return WINED3DERR_INVALIDCALL
;
1608 stream
= &device
->updateStateBlock
->state
.streams
[stream_idx
];
1609 old_flags
= stream
->flags
;
1610 old_freq
= stream
->frequency
;
1612 stream
->flags
= divider
& (WINED3DSTREAMSOURCE_INSTANCEDATA
| WINED3DSTREAMSOURCE_INDEXEDDATA
);
1613 stream
->frequency
= divider
& 0x7fffff;
1615 device
->updateStateBlock
->changed
.streamFreq
|= 1 << stream_idx
;
1617 if (stream
->frequency
!= old_freq
|| stream
->flags
!= old_flags
)
1618 device_invalidate_state(device
, STATE_STREAMSRC
);
1623 HRESULT CDECL
wined3d_device_get_stream_source_freq(const struct wined3d_device
*device
,
1624 UINT stream_idx
, UINT
*divider
)
1626 struct wined3d_stream_state
*stream
;
1628 TRACE("device %p, stream_idx %u, divider %p.\n", device
, stream_idx
, divider
);
1630 stream
= &device
->updateStateBlock
->state
.streams
[stream_idx
];
1631 *divider
= stream
->flags
| stream
->frequency
;
1633 TRACE("Returning %#x.\n", *divider
);
1638 void CDECL
wined3d_device_set_transform(struct wined3d_device
*device
,
1639 enum wined3d_transform_state d3dts
, const struct wined3d_matrix
*matrix
)
1641 TRACE("device %p, state %s, matrix %p.\n",
1642 device
, debug_d3dtstype(d3dts
), matrix
);
1643 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._11
, matrix
->u
.s
._12
, matrix
->u
.s
._13
, matrix
->u
.s
._14
);
1644 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._21
, matrix
->u
.s
._22
, matrix
->u
.s
._23
, matrix
->u
.s
._24
);
1645 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._31
, matrix
->u
.s
._32
, matrix
->u
.s
._33
, matrix
->u
.s
._34
);
1646 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->u
.s
._41
, matrix
->u
.s
._42
, matrix
->u
.s
._43
, matrix
->u
.s
._44
);
1648 /* Handle recording of state blocks. */
1649 if (device
->isRecordingState
)
1651 TRACE("Recording... not performing anything.\n");
1652 device
->updateStateBlock
->changed
.transform
[d3dts
>> 5] |= 1 << (d3dts
& 0x1f);
1653 device
->updateStateBlock
->state
.transforms
[d3dts
] = *matrix
;
1657 /* If the new matrix is the same as the current one,
1658 * we cut off any further processing. this seems to be a reasonable
1659 * optimization because as was noticed, some apps (warcraft3 for example)
1660 * tend towards setting the same matrix repeatedly for some reason.
1662 * From here on we assume that the new matrix is different, wherever it matters. */
1663 if (!memcmp(&device
->stateBlock
->state
.transforms
[d3dts
].u
.m
[0][0], matrix
, sizeof(*matrix
)))
1665 TRACE("The application is setting the same matrix over again.\n");
1669 device
->stateBlock
->state
.transforms
[d3dts
] = *matrix
;
1671 if (d3dts
< WINED3D_TS_WORLD_MATRIX(device
->adapter
->gl_info
.limits
.blends
))
1672 device_invalidate_state(device
, STATE_TRANSFORM(d3dts
));
1675 void CDECL
wined3d_device_get_transform(const struct wined3d_device
*device
,
1676 enum wined3d_transform_state state
, struct wined3d_matrix
*matrix
)
1678 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1680 *matrix
= device
->stateBlock
->state
.transforms
[state
];
1683 void CDECL
wined3d_device_multiply_transform(struct wined3d_device
*device
,
1684 enum wined3d_transform_state state
, const struct wined3d_matrix
*matrix
)
1686 const struct wined3d_matrix
*mat
;
1687 struct wined3d_matrix temp
;
1689 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1691 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1692 * below means it will be recorded in a state block change, but it
1693 * works regardless where it is recorded.
1694 * If this is found to be wrong, change to StateBlock. */
1695 if (state
> HIGHEST_TRANSFORMSTATE
)
1697 WARN("Unhandled transform state %#x.\n", state
);
1701 mat
= &device
->updateStateBlock
->state
.transforms
[state
];
1702 multiply_matrix(&temp
, mat
, matrix
);
1704 /* Apply change via set transform - will reapply to eg. lights this way. */
1705 wined3d_device_set_transform(device
, state
, &temp
);
1708 /* Note lights are real special cases. Although the device caps state only
1709 * e.g. 8 are supported, you can reference any indexes you want as long as
1710 * that number max are enabled at any one point in time. Therefore since the
1711 * indices can be anything, we need a hashmap of them. However, this causes
1712 * stateblock problems. When capturing the state block, I duplicate the
1713 * hashmap, but when recording, just build a chain pretty much of commands to
1715 HRESULT CDECL
wined3d_device_set_light(struct wined3d_device
*device
,
1716 UINT light_idx
, const struct wined3d_light
*light
)
1718 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1719 struct wined3d_light_info
*object
= NULL
;
1723 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1725 /* Check the parameter range. Need for speed most wanted sets junk lights
1726 * which confuse the GL driver. */
1728 return WINED3DERR_INVALIDCALL
;
1730 switch (light
->type
)
1732 case WINED3D_LIGHT_POINT
:
1733 case WINED3D_LIGHT_SPOT
:
1734 case WINED3D_LIGHT_PARALLELPOINT
:
1735 case WINED3D_LIGHT_GLSPOT
:
1736 /* Incorrect attenuation values can cause the gl driver to crash.
1737 * Happens with Need for speed most wanted. */
1738 if (light
->attenuation0
< 0.0f
|| light
->attenuation1
< 0.0f
|| light
->attenuation2
< 0.0f
)
1740 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1741 return WINED3DERR_INVALIDCALL
;
1745 case WINED3D_LIGHT_DIRECTIONAL
:
1746 /* Ignores attenuation */
1750 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1751 return WINED3DERR_INVALIDCALL
;
1754 LIST_FOR_EACH(e
, &device
->updateStateBlock
->state
.light_map
[hash_idx
])
1756 object
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1757 if (object
->OriginalIndex
== light_idx
)
1764 TRACE("Adding new light\n");
1765 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1767 return E_OUTOFMEMORY
;
1769 list_add_head(&device
->updateStateBlock
->state
.light_map
[hash_idx
], &object
->entry
);
1770 object
->glIndex
= -1;
1771 object
->OriginalIndex
= light_idx
;
1774 /* Initialize the object. */
1775 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1776 light_idx
, light
->type
,
1777 light
->diffuse
.r
, light
->diffuse
.g
, light
->diffuse
.b
, light
->diffuse
.a
,
1778 light
->specular
.r
, light
->specular
.g
, light
->specular
.b
, light
->specular
.a
,
1779 light
->ambient
.r
, light
->ambient
.g
, light
->ambient
.b
, light
->ambient
.a
);
1780 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light
->position
.x
, light
->position
.y
, light
->position
.z
,
1781 light
->direction
.x
, light
->direction
.y
, light
->direction
.z
);
1782 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1783 light
->range
, light
->falloff
, light
->theta
, light
->phi
);
1785 /* Update the live definitions if the light is currently assigned a glIndex. */
1786 if (object
->glIndex
!= -1 && !device
->isRecordingState
)
1788 if (object
->OriginalParms
.type
!= light
->type
)
1789 device_invalidate_state(device
, STATE_LIGHT_TYPE
);
1790 device_invalidate_state(device
, STATE_ACTIVELIGHT(object
->glIndex
));
1793 /* Save away the information. */
1794 object
->OriginalParms
= *light
;
1796 switch (light
->type
)
1798 case WINED3D_LIGHT_POINT
:
1800 object
->lightPosn
[0] = light
->position
.x
;
1801 object
->lightPosn
[1] = light
->position
.y
;
1802 object
->lightPosn
[2] = light
->position
.z
;
1803 object
->lightPosn
[3] = 1.0f
;
1804 object
->cutoff
= 180.0f
;
1808 case WINED3D_LIGHT_DIRECTIONAL
:
1810 object
->lightPosn
[0] = -light
->direction
.x
;
1811 object
->lightPosn
[1] = -light
->direction
.y
;
1812 object
->lightPosn
[2] = -light
->direction
.z
;
1813 object
->lightPosn
[3] = 0.0f
;
1814 object
->exponent
= 0.0f
;
1815 object
->cutoff
= 180.0f
;
1818 case WINED3D_LIGHT_SPOT
:
1820 object
->lightPosn
[0] = light
->position
.x
;
1821 object
->lightPosn
[1] = light
->position
.y
;
1822 object
->lightPosn
[2] = light
->position
.z
;
1823 object
->lightPosn
[3] = 1.0f
;
1826 object
->lightDirn
[0] = light
->direction
.x
;
1827 object
->lightDirn
[1] = light
->direction
.y
;
1828 object
->lightDirn
[2] = light
->direction
.z
;
1829 object
->lightDirn
[3] = 1.0f
;
1831 /* opengl-ish and d3d-ish spot lights use too different models
1832 * for the light "intensity" as a function of the angle towards
1833 * the main light direction, so we only can approximate very
1834 * roughly. However, spot lights are rather rarely used in games
1835 * (if ever used at all). Furthermore if still used, probably
1836 * nobody pays attention to such details. */
1837 if (!light
->falloff
)
1839 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1840 * equations have the falloff resp. exponent parameter as an
1841 * exponent, so the spot light lighting will always be 1.0 for
1842 * both of them, and we don't have to care for the rest of the
1843 * rather complex calculation. */
1844 object
->exponent
= 0.0f
;
1848 rho
= light
->theta
+ (light
->phi
- light
->theta
) / (2 * light
->falloff
);
1851 object
->exponent
= -0.3f
/ logf(cosf(rho
/ 2));
1854 if (object
->exponent
> 128.0f
)
1855 object
->exponent
= 128.0f
;
1857 object
->cutoff
= (float)(light
->phi
* 90 / M_PI
);
1862 FIXME("Unrecognized light type %#x.\n", light
->type
);
1868 HRESULT CDECL
wined3d_device_get_light(const struct wined3d_device
*device
,
1869 UINT light_idx
, struct wined3d_light
*light
)
1871 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1872 struct wined3d_light_info
*light_info
= NULL
;
1875 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1877 LIST_FOR_EACH(e
, &device
->stateBlock
->state
.light_map
[hash_idx
])
1879 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1880 if (light_info
->OriginalIndex
== light_idx
)
1887 TRACE("Light information requested but light not defined\n");
1888 return WINED3DERR_INVALIDCALL
;
1891 *light
= light_info
->OriginalParms
;
1895 HRESULT CDECL
wined3d_device_set_light_enable(struct wined3d_device
*device
, UINT light_idx
, BOOL enable
)
1897 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1898 struct wined3d_light_info
*light_info
= NULL
;
1901 TRACE("device %p, light_idx %u, enable %#x.\n", device
, light_idx
, enable
);
1903 LIST_FOR_EACH(e
, &device
->updateStateBlock
->state
.light_map
[hash_idx
])
1905 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1906 if (light_info
->OriginalIndex
== light_idx
)
1910 TRACE("Found light %p.\n", light_info
);
1912 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1915 TRACE("Light enabled requested but light not defined, so defining one!\n");
1916 wined3d_device_set_light(device
, light_idx
, &WINED3D_default_light
);
1918 /* Search for it again! Should be fairly quick as near head of list. */
1919 LIST_FOR_EACH(e
, &device
->updateStateBlock
->state
.light_map
[hash_idx
])
1921 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
1922 if (light_info
->OriginalIndex
== light_idx
)
1928 FIXME("Adding default lights has failed dismally\n");
1929 return WINED3DERR_INVALIDCALL
;
1935 if (light_info
->glIndex
!= -1)
1937 if (!device
->isRecordingState
)
1939 device_invalidate_state(device
, STATE_LIGHT_TYPE
);
1940 device_invalidate_state(device
, STATE_ACTIVELIGHT(light_info
->glIndex
));
1943 device
->updateStateBlock
->state
.lights
[light_info
->glIndex
] = NULL
;
1944 light_info
->glIndex
= -1;
1948 TRACE("Light already disabled, nothing to do\n");
1950 light_info
->enabled
= FALSE
;
1954 light_info
->enabled
= TRUE
;
1955 if (light_info
->glIndex
!= -1)
1957 TRACE("Nothing to do as light was enabled\n");
1962 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1963 /* Find a free GL light. */
1964 for (i
= 0; i
< gl_info
->limits
.lights
; ++i
)
1966 if (!device
->updateStateBlock
->state
.lights
[i
])
1968 device
->updateStateBlock
->state
.lights
[i
] = light_info
;
1969 light_info
->glIndex
= i
;
1973 if (light_info
->glIndex
== -1)
1975 /* Our tests show that Windows returns D3D_OK in this situation, even with
1976 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1977 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1978 * as well for those lights.
1980 * TODO: Test how this affects rendering. */
1981 WARN("Too many concurrently active lights\n");
1985 /* i == light_info->glIndex */
1986 if (!device
->isRecordingState
)
1988 device_invalidate_state(device
, STATE_LIGHT_TYPE
);
1989 device_invalidate_state(device
, STATE_ACTIVELIGHT(i
));
1997 HRESULT CDECL
wined3d_device_get_light_enable(const struct wined3d_device
*device
, UINT light_idx
, BOOL
*enable
)
1999 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
2000 struct wined3d_light_info
*light_info
= NULL
;
2003 TRACE("device %p, light_idx %u, enable %p.\n", device
, light_idx
, enable
);
2005 LIST_FOR_EACH(e
, &device
->stateBlock
->state
.light_map
[hash_idx
])
2007 light_info
= LIST_ENTRY(e
, struct wined3d_light_info
, entry
);
2008 if (light_info
->OriginalIndex
== light_idx
)
2015 TRACE("Light enabled state requested but light not defined.\n");
2016 return WINED3DERR_INVALIDCALL
;
2018 /* true is 128 according to SetLightEnable */
2019 *enable
= light_info
->enabled
? 128 : 0;
2023 HRESULT CDECL
wined3d_device_set_clip_plane(struct wined3d_device
*device
,
2024 UINT plane_idx
, const struct wined3d_vec4
*plane
)
2026 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
2028 /* Validate plane_idx. */
2029 if (plane_idx
>= device
->adapter
->gl_info
.limits
.clipplanes
)
2031 TRACE("Application has requested clipplane this device doesn't support.\n");
2032 return WINED3DERR_INVALIDCALL
;
2035 device
->updateStateBlock
->changed
.clipplane
|= 1 << plane_idx
;
2037 if (!memcmp(&device
->updateStateBlock
->state
.clip_planes
[plane_idx
], plane
, sizeof(*plane
)))
2039 TRACE("Application is setting old values over, nothing to do.\n");
2043 device
->updateStateBlock
->state
.clip_planes
[plane_idx
] = *plane
;
2045 /* Handle recording of state blocks. */
2046 if (device
->isRecordingState
)
2048 TRACE("Recording... not performing anything.\n");
2052 device_invalidate_state(device
, STATE_CLIPPLANE(plane_idx
));
2057 HRESULT CDECL
wined3d_device_get_clip_plane(const struct wined3d_device
*device
,
2058 UINT plane_idx
, struct wined3d_vec4
*plane
)
2060 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
2062 /* Validate plane_idx. */
2063 if (plane_idx
>= device
->adapter
->gl_info
.limits
.clipplanes
)
2065 TRACE("Application has requested clipplane this device doesn't support.\n");
2066 return WINED3DERR_INVALIDCALL
;
2069 *plane
= device
->stateBlock
->state
.clip_planes
[plane_idx
];
2074 HRESULT CDECL
wined3d_device_set_clip_status(struct wined3d_device
*device
,
2075 const struct wined3d_clip_status
*clip_status
)
2077 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
2080 return WINED3DERR_INVALIDCALL
;
2085 HRESULT CDECL
wined3d_device_get_clip_status(const struct wined3d_device
*device
,
2086 struct wined3d_clip_status
*clip_status
)
2088 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
2091 return WINED3DERR_INVALIDCALL
;
2096 void CDECL
wined3d_device_set_material(struct wined3d_device
*device
, const struct wined3d_material
*material
)
2098 TRACE("device %p, material %p.\n", device
, material
);
2100 device
->updateStateBlock
->changed
.material
= TRUE
;
2101 device
->updateStateBlock
->state
.material
= *material
;
2103 /* Handle recording of state blocks */
2104 if (device
->isRecordingState
)
2106 TRACE("Recording... not performing anything.\n");
2110 device_invalidate_state(device
, STATE_MATERIAL
);
2113 void CDECL
wined3d_device_get_material(const struct wined3d_device
*device
, struct wined3d_material
*material
)
2115 TRACE("device %p, material %p.\n", device
, material
);
2117 *material
= device
->updateStateBlock
->state
.material
;
2119 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2120 material
->diffuse
.r
, material
->diffuse
.g
,
2121 material
->diffuse
.b
, material
->diffuse
.a
);
2122 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2123 material
->ambient
.r
, material
->ambient
.g
,
2124 material
->ambient
.b
, material
->ambient
.a
);
2125 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2126 material
->specular
.r
, material
->specular
.g
,
2127 material
->specular
.b
, material
->specular
.a
);
2128 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2129 material
->emissive
.r
, material
->emissive
.g
,
2130 material
->emissive
.b
, material
->emissive
.a
);
2131 TRACE("power %.8e.\n", material
->power
);
2134 void CDECL
wined3d_device_set_index_buffer(struct wined3d_device
*device
,
2135 struct wined3d_buffer
*buffer
, enum wined3d_format_id format_id
)
2137 struct wined3d_buffer
*prev_buffer
;
2139 TRACE("device %p, buffer %p, format %s.\n",
2140 device
, buffer
, debug_d3dformat(format_id
));
2142 prev_buffer
= device
->updateStateBlock
->state
.index_buffer
;
2144 device
->updateStateBlock
->changed
.indices
= TRUE
;
2145 device
->updateStateBlock
->state
.index_buffer
= buffer
;
2146 device
->updateStateBlock
->state
.index_format
= format_id
;
2148 /* Handle recording of state blocks. */
2149 if (device
->isRecordingState
)
2151 TRACE("Recording... not performing anything.\n");
2153 wined3d_buffer_incref(buffer
);
2155 wined3d_buffer_decref(prev_buffer
);
2159 if (prev_buffer
!= buffer
)
2161 device_invalidate_state(device
, STATE_INDEXBUFFER
);
2164 InterlockedIncrement(&buffer
->resource
.bind_count
);
2165 wined3d_buffer_incref(buffer
);
2169 InterlockedDecrement(&prev_buffer
->resource
.bind_count
);
2170 wined3d_buffer_decref(prev_buffer
);
2175 struct wined3d_buffer
* CDECL
wined3d_device_get_index_buffer(const struct wined3d_device
*device
,
2176 enum wined3d_format_id
*format
)
2178 TRACE("device %p, format %p.\n", device
, format
);
2180 *format
= device
->stateBlock
->state
.index_format
;
2181 return device
->stateBlock
->state
.index_buffer
;
2184 void CDECL
wined3d_device_set_base_vertex_index(struct wined3d_device
*device
, INT base_index
)
2186 TRACE("device %p, base_index %d.\n", device
, base_index
);
2188 device
->updateStateBlock
->state
.base_vertex_index
= base_index
;
2191 INT CDECL
wined3d_device_get_base_vertex_index(const struct wined3d_device
*device
)
2193 TRACE("device %p.\n", device
);
2195 return device
->stateBlock
->state
.base_vertex_index
;
2198 void CDECL
wined3d_device_set_viewport(struct wined3d_device
*device
, const struct wined3d_viewport
*viewport
)
2200 TRACE("device %p, viewport %p.\n", device
, viewport
);
2201 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2202 viewport
->x
, viewport
->y
, viewport
->width
, viewport
->height
, viewport
->min_z
, viewport
->max_z
);
2204 device
->updateStateBlock
->changed
.viewport
= TRUE
;
2205 device
->updateStateBlock
->state
.viewport
= *viewport
;
2207 /* Handle recording of state blocks */
2208 if (device
->isRecordingState
)
2210 TRACE("Recording... not performing anything\n");
2214 device_invalidate_state(device
, STATE_VIEWPORT
);
2217 void CDECL
wined3d_device_get_viewport(const struct wined3d_device
*device
, struct wined3d_viewport
*viewport
)
2219 TRACE("device %p, viewport %p.\n", device
, viewport
);
2221 *viewport
= device
->stateBlock
->state
.viewport
;
2224 static void resolve_depth_buffer(struct wined3d_state
*state
)
2226 struct wined3d_texture
*texture
= state
->textures
[0];
2227 struct wined3d_surface
*depth_stencil
, *surface
;
2229 if (!texture
|| texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE
2230 || !(texture
->resource
.format
->flags
& WINED3DFMT_FLAG_DEPTH
))
2232 surface
= surface_from_resource(texture
->sub_resources
[0]);
2233 depth_stencil
= state
->fb
->depth_stencil
;
2237 wined3d_surface_blt(surface
, NULL
, depth_stencil
, NULL
, 0, NULL
, WINED3D_TEXF_POINT
);
2240 void CDECL
wined3d_device_set_render_state(struct wined3d_device
*device
,
2241 enum wined3d_render_state state
, DWORD value
)
2243 DWORD old_value
= device
->stateBlock
->state
.render_states
[state
];
2245 TRACE("device %p, state %s (%#x), value %#x.\n", device
, debug_d3drenderstate(state
), state
, value
);
2247 device
->updateStateBlock
->changed
.renderState
[state
>> 5] |= 1 << (state
& 0x1f);
2248 device
->updateStateBlock
->state
.render_states
[state
] = value
;
2250 /* Handle recording of state blocks. */
2251 if (device
->isRecordingState
)
2253 TRACE("Recording... not performing anything.\n");
2257 /* Compared here and not before the assignment to allow proper stateblock recording. */
2258 if (value
== old_value
)
2259 TRACE("Application is setting the old value over, nothing to do.\n");
2261 device_invalidate_state(device
, STATE_RENDER(state
));
2263 if (state
== WINED3D_RS_POINTSIZE
&& value
== WINED3D_RESZ_CODE
)
2265 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2266 resolve_depth_buffer(&device
->stateBlock
->state
);
2270 DWORD CDECL
wined3d_device_get_render_state(const struct wined3d_device
*device
, enum wined3d_render_state state
)
2272 TRACE("device %p, state %s (%#x).\n", device
, debug_d3drenderstate(state
), state
);
2274 return device
->stateBlock
->state
.render_states
[state
];
2277 void CDECL
wined3d_device_set_sampler_state(struct wined3d_device
*device
,
2278 UINT sampler_idx
, enum wined3d_sampler_state state
, DWORD value
)
2282 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2283 device
, sampler_idx
, debug_d3dsamplerstate(state
), value
);
2285 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2286 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2288 if (sampler_idx
>= sizeof(device
->stateBlock
->state
.sampler_states
)
2289 / sizeof(*device
->stateBlock
->state
.sampler_states
))
2291 WARN("Invalid sampler %u.\n", sampler_idx
);
2292 return; /* Windows accepts overflowing this array ... we do not. */
2295 old_value
= device
->stateBlock
->state
.sampler_states
[sampler_idx
][state
];
2296 device
->updateStateBlock
->state
.sampler_states
[sampler_idx
][state
] = value
;
2297 device
->updateStateBlock
->changed
.samplerState
[sampler_idx
] |= 1 << state
;
2299 /* Handle recording of state blocks. */
2300 if (device
->isRecordingState
)
2302 TRACE("Recording... not performing anything.\n");
2306 if (old_value
== value
)
2308 TRACE("Application is setting the old value over, nothing to do.\n");
2312 device_invalidate_state(device
, STATE_SAMPLER(sampler_idx
));
2315 DWORD CDECL
wined3d_device_get_sampler_state(const struct wined3d_device
*device
,
2316 UINT sampler_idx
, enum wined3d_sampler_state state
)
2318 TRACE("device %p, sampler_idx %u, state %s.\n",
2319 device
, sampler_idx
, debug_d3dsamplerstate(state
));
2321 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2322 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2324 if (sampler_idx
>= sizeof(device
->stateBlock
->state
.sampler_states
)
2325 / sizeof(*device
->stateBlock
->state
.sampler_states
))
2327 WARN("Invalid sampler %u.\n", sampler_idx
);
2328 return 0; /* Windows accepts overflowing this array ... we do not. */
2331 return device
->stateBlock
->state
.sampler_states
[sampler_idx
][state
];
2334 void CDECL
wined3d_device_set_scissor_rect(struct wined3d_device
*device
, const RECT
*rect
)
2336 TRACE("device %p, rect %s.\n", device
, wine_dbgstr_rect(rect
));
2338 device
->updateStateBlock
->changed
.scissorRect
= TRUE
;
2339 if (EqualRect(&device
->updateStateBlock
->state
.scissor_rect
, rect
))
2341 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2344 CopyRect(&device
->updateStateBlock
->state
.scissor_rect
, rect
);
2346 if (device
->isRecordingState
)
2348 TRACE("Recording... not performing anything.\n");
2352 device_invalidate_state(device
, STATE_SCISSORRECT
);
2355 void CDECL
wined3d_device_get_scissor_rect(const struct wined3d_device
*device
, RECT
*rect
)
2357 TRACE("device %p, rect %p.\n", device
, rect
);
2359 *rect
= device
->updateStateBlock
->state
.scissor_rect
;
2360 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect
));
2363 void CDECL
wined3d_device_set_vertex_declaration(struct wined3d_device
*device
,
2364 struct wined3d_vertex_declaration
*declaration
)
2366 struct wined3d_vertex_declaration
*prev
= device
->updateStateBlock
->state
.vertex_declaration
;
2368 TRACE("device %p, declaration %p.\n", device
, declaration
);
2371 wined3d_vertex_declaration_incref(declaration
);
2373 wined3d_vertex_declaration_decref(prev
);
2375 device
->updateStateBlock
->state
.vertex_declaration
= declaration
;
2376 device
->updateStateBlock
->changed
.vertexDecl
= TRUE
;
2378 if (device
->isRecordingState
)
2380 TRACE("Recording... not performing anything.\n");
2384 if (declaration
== prev
)
2386 /* Checked after the assignment to allow proper stateblock recording. */
2387 TRACE("Application is setting the old declaration over, nothing to do.\n");
2391 device_invalidate_state(device
, STATE_VDECL
);
2394 struct wined3d_vertex_declaration
* CDECL
wined3d_device_get_vertex_declaration(const struct wined3d_device
*device
)
2396 TRACE("device %p.\n", device
);
2398 return device
->stateBlock
->state
.vertex_declaration
;
2401 void CDECL
wined3d_device_set_vertex_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2403 struct wined3d_shader
*prev
= device
->updateStateBlock
->state
.vertex_shader
;
2405 TRACE("device %p, shader %p.\n", device
, shader
);
2408 wined3d_shader_incref(shader
);
2410 wined3d_shader_decref(prev
);
2412 device
->updateStateBlock
->state
.vertex_shader
= shader
;
2413 device
->updateStateBlock
->changed
.vertexShader
= TRUE
;
2415 if (device
->isRecordingState
)
2417 TRACE("Recording... not performing anything.\n");
2423 TRACE("Application is setting the old shader over, nothing to do.\n");
2427 device_invalidate_state(device
, STATE_VSHADER
);
2430 struct wined3d_shader
* CDECL
wined3d_device_get_vertex_shader(const struct wined3d_device
*device
)
2432 TRACE("device %p.\n", device
);
2434 return device
->stateBlock
->state
.vertex_shader
;
2437 void CDECL
wined3d_device_set_vs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2439 struct wined3d_buffer
*prev
;
2441 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2443 if (idx
>= MAX_CONSTANT_BUFFERS
)
2445 WARN("Invalid constant buffer index %u.\n", idx
);
2449 prev
= device
->updateStateBlock
->state
.vs_cb
[idx
];
2450 device
->updateStateBlock
->state
.vs_cb
[idx
] = buffer
;
2452 if (device
->isRecordingState
)
2455 wined3d_buffer_incref(buffer
);
2457 wined3d_buffer_decref(prev
);
2465 InterlockedIncrement(&buffer
->resource
.bind_count
);
2466 wined3d_buffer_incref(buffer
);
2470 InterlockedDecrement(&prev
->resource
.bind_count
);
2471 wined3d_buffer_decref(prev
);
2476 struct wined3d_buffer
* CDECL
wined3d_device_get_vs_cb(const struct wined3d_device
*device
, UINT idx
)
2478 TRACE("device %p, idx %u.\n", device
, idx
);
2480 if (idx
>= MAX_CONSTANT_BUFFERS
)
2482 WARN("Invalid constant buffer index %u.\n", idx
);
2486 return device
->stateBlock
->state
.vs_cb
[idx
];
2489 void CDECL
wined3d_device_set_vs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2491 struct wined3d_sampler
*prev
;
2493 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2495 if (idx
>= MAX_SAMPLER_OBJECTS
)
2497 WARN("Invalid sampler index %u.\n", idx
);
2501 prev
= device
->updateStateBlock
->state
.vs_sampler
[idx
];
2502 device
->updateStateBlock
->state
.vs_sampler
[idx
] = sampler
;
2505 wined3d_sampler_incref(sampler
);
2507 wined3d_sampler_decref(prev
);
2510 struct wined3d_sampler
* CDECL
wined3d_device_get_vs_sampler(const struct wined3d_device
*device
, UINT idx
)
2512 TRACE("device %p, idx %u.\n", device
, idx
);
2514 if (idx
>= MAX_SAMPLER_OBJECTS
)
2516 WARN("Invalid sampler index %u.\n", idx
);
2520 return device
->stateBlock
->state
.vs_sampler
[idx
];
2523 HRESULT CDECL
wined3d_device_set_vs_consts_b(struct wined3d_device
*device
,
2524 UINT start_register
, const BOOL
*constants
, UINT bool_count
)
2526 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
2529 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2530 device
, start_register
, constants
, bool_count
);
2532 if (!constants
|| start_register
>= MAX_CONST_B
)
2533 return WINED3DERR_INVALIDCALL
;
2535 memcpy(&device
->updateStateBlock
->state
.vs_consts_b
[start_register
], constants
, count
* sizeof(BOOL
));
2536 for (i
= 0; i
< count
; ++i
)
2537 TRACE("Set BOOL constant %u to %s.\n", start_register
+ i
, constants
[i
] ? "true" : "false");
2539 for (i
= start_register
; i
< count
+ start_register
; ++i
)
2540 device
->updateStateBlock
->changed
.vertexShaderConstantsB
|= (1 << i
);
2542 if (!device
->isRecordingState
)
2543 device_invalidate_state(device
, STATE_VERTEXSHADERCONSTANT
);
2548 HRESULT CDECL
wined3d_device_get_vs_consts_b(const struct wined3d_device
*device
,
2549 UINT start_register
, BOOL
*constants
, UINT bool_count
)
2551 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
2553 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2554 device
, start_register
, constants
, bool_count
);
2556 if (!constants
|| start_register
>= MAX_CONST_B
)
2557 return WINED3DERR_INVALIDCALL
;
2559 memcpy(constants
, &device
->stateBlock
->state
.vs_consts_b
[start_register
], count
* sizeof(BOOL
));
2564 HRESULT CDECL
wined3d_device_set_vs_consts_i(struct wined3d_device
*device
,
2565 UINT start_register
, const int *constants
, UINT vector4i_count
)
2567 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
2570 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2571 device
, start_register
, constants
, vector4i_count
);
2573 if (!constants
|| start_register
>= MAX_CONST_I
)
2574 return WINED3DERR_INVALIDCALL
;
2576 memcpy(&device
->updateStateBlock
->state
.vs_consts_i
[start_register
* 4], constants
, count
* sizeof(int) * 4);
2577 for (i
= 0; i
< count
; ++i
)
2578 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register
+ i
,
2579 constants
[i
* 4], constants
[i
* 4 + 1],
2580 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
2582 for (i
= start_register
; i
< count
+ start_register
; ++i
)
2583 device
->updateStateBlock
->changed
.vertexShaderConstantsI
|= (1 << i
);
2585 if (!device
->isRecordingState
)
2586 device_invalidate_state(device
, STATE_VERTEXSHADERCONSTANT
);
2591 HRESULT CDECL
wined3d_device_get_vs_consts_i(const struct wined3d_device
*device
,
2592 UINT start_register
, int *constants
, UINT vector4i_count
)
2594 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
2596 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2597 device
, start_register
, constants
, vector4i_count
);
2599 if (!constants
|| start_register
>= MAX_CONST_I
)
2600 return WINED3DERR_INVALIDCALL
;
2602 memcpy(constants
, &device
->stateBlock
->state
.vs_consts_i
[start_register
* 4], count
* sizeof(int) * 4);
2606 HRESULT CDECL
wined3d_device_set_vs_consts_f(struct wined3d_device
*device
,
2607 UINT start_register
, const float *constants
, UINT vector4f_count
)
2610 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2612 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2613 device
, start_register
, constants
, vector4f_count
);
2615 /* Specifically test start_register > limit to catch MAX_UINT overflows
2616 * when adding start_register + vector4f_count. */
2618 || start_register
+ vector4f_count
> d3d_info
->limits
.vs_uniform_count
2619 || start_register
> d3d_info
->limits
.vs_uniform_count
)
2620 return WINED3DERR_INVALIDCALL
;
2622 memcpy(&device
->updateStateBlock
->state
.vs_consts_f
[start_register
* 4],
2623 constants
, vector4f_count
* sizeof(float) * 4);
2626 for (i
= 0; i
< vector4f_count
; ++i
)
2627 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register
+ i
,
2628 constants
[i
* 4], constants
[i
* 4 + 1],
2629 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
2632 if (!device
->isRecordingState
)
2634 device
->shader_backend
->shader_update_float_vertex_constants(device
, start_register
, vector4f_count
);
2635 device_invalidate_state(device
, STATE_VERTEXSHADERCONSTANT
);
2638 memset(device
->updateStateBlock
->changed
.vertexShaderConstantsF
+ start_register
, 1,
2639 sizeof(*device
->updateStateBlock
->changed
.vertexShaderConstantsF
) * vector4f_count
);
2644 HRESULT CDECL
wined3d_device_get_vs_consts_f(const struct wined3d_device
*device
,
2645 UINT start_register
, float *constants
, UINT vector4f_count
)
2647 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2648 int count
= min(vector4f_count
, d3d_info
->limits
.vs_uniform_count
- start_register
);
2650 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2651 device
, start_register
, constants
, vector4f_count
);
2653 if (!constants
|| count
< 0)
2654 return WINED3DERR_INVALIDCALL
;
2656 memcpy(constants
, &device
->stateBlock
->state
.vs_consts_f
[start_register
* 4], count
* sizeof(float) * 4);
2661 static void device_invalidate_texture_stage(const struct wined3d_device
*device
, DWORD stage
)
2665 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2667 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, i
));
2671 static void device_map_stage(struct wined3d_device
*device
, DWORD stage
, DWORD unit
)
2673 DWORD i
= device
->rev_tex_unit_map
[unit
];
2674 DWORD j
= device
->texUnitMap
[stage
];
2676 device
->texUnitMap
[stage
] = unit
;
2677 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2678 device
->texUnitMap
[i
] = WINED3D_UNMAPPED_STAGE
;
2680 device
->rev_tex_unit_map
[unit
] = stage
;
2681 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2682 device
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2685 static void device_update_fixed_function_usage_map(struct wined3d_device
*device
)
2689 device
->fixed_function_usage_map
= 0;
2690 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2692 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2693 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2694 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2695 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2696 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2697 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2698 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2699 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2700 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2702 /* Not used, and disable higher stages. */
2703 if (color_op
== WINED3D_TOP_DISABLE
)
2706 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2707 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2708 || ((color_arg3
== WINED3DTA_TEXTURE
)
2709 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2710 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2711 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2712 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2713 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2714 device
->fixed_function_usage_map
|= (1 << i
);
2716 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2717 && i
< MAX_TEXTURES
- 1)
2718 device
->fixed_function_usage_map
|= (1 << (i
+ 1));
2722 static void device_map_fixed_function_samplers(struct wined3d_device
*device
, const struct wined3d_d3d_info
*d3d_info
)
2724 unsigned int i
, tex
;
2727 device_update_fixed_function_usage_map(device
);
2728 ffu_map
= device
->fixed_function_usage_map
;
2730 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2731 || device
->stateBlock
->state
.lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2733 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2735 if (!(ffu_map
& 1)) continue;
2737 if (device
->texUnitMap
[i
] != i
)
2739 device_map_stage(device
, i
, i
);
2740 device_invalidate_state(device
, STATE_SAMPLER(i
));
2741 device_invalidate_texture_stage(device
, i
);
2747 /* Now work out the mapping */
2749 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2751 if (!(ffu_map
& 1)) continue;
2753 if (device
->texUnitMap
[i
] != tex
)
2755 device_map_stage(device
, i
, tex
);
2756 device_invalidate_state(device
, STATE_SAMPLER(i
));
2757 device_invalidate_texture_stage(device
, i
);
2764 static void device_map_psamplers(struct wined3d_device
*device
, const struct wined3d_d3d_info
*d3d_info
)
2766 const enum wined3d_sampler_texture_type
*sampler_type
=
2767 device
->stateBlock
->state
.pixel_shader
->reg_maps
.sampler_type
;
2770 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2772 if (sampler_type
[i
] && device
->texUnitMap
[i
] != i
)
2774 device_map_stage(device
, i
, i
);
2775 device_invalidate_state(device
, STATE_SAMPLER(i
));
2776 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2777 device_invalidate_texture_stage(device
, i
);
2782 static BOOL
device_unit_free_for_vs(const struct wined3d_device
*device
,
2783 const enum wined3d_sampler_texture_type
*pshader_sampler_tokens
,
2784 const enum wined3d_sampler_texture_type
*vshader_sampler_tokens
, DWORD unit
)
2786 DWORD current_mapping
= device
->rev_tex_unit_map
[unit
];
2788 /* Not currently used */
2789 if (current_mapping
== WINED3D_UNMAPPED_STAGE
) return TRUE
;
2791 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
) {
2792 /* Used by a fragment sampler */
2794 if (!pshader_sampler_tokens
) {
2795 /* No pixel shader, check fixed function */
2796 return current_mapping
>= MAX_TEXTURES
|| !(device
->fixed_function_usage_map
& (1 << current_mapping
));
2799 /* Pixel shader, check the shader's sampler map */
2800 return !pshader_sampler_tokens
[current_mapping
];
2803 /* Used by a vertex sampler */
2804 return !vshader_sampler_tokens
[current_mapping
- MAX_FRAGMENT_SAMPLERS
];
2807 static void device_map_vsamplers(struct wined3d_device
*device
, BOOL ps
, const struct wined3d_gl_info
*gl_info
)
2809 const enum wined3d_sampler_texture_type
*vshader_sampler_type
=
2810 device
->stateBlock
->state
.vertex_shader
->reg_maps
.sampler_type
;
2811 const enum wined3d_sampler_texture_type
*pshader_sampler_type
= NULL
;
2812 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2817 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2818 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2819 pshader_sampler_type
= device
->stateBlock
->state
.pixel_shader
->reg_maps
.sampler_type
;
2822 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
2823 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2824 if (vshader_sampler_type
[i
])
2826 if (device
->texUnitMap
[vsampler_idx
] != WINED3D_UNMAPPED_STAGE
)
2828 /* Already mapped somewhere */
2834 if (device_unit_free_for_vs(device
, pshader_sampler_type
, vshader_sampler_type
, start
))
2836 device_map_stage(device
, vsampler_idx
, start
);
2837 device_invalidate_state(device
, STATE_SAMPLER(vsampler_idx
));
2849 void device_update_tex_unit_map(struct wined3d_device
*device
)
2851 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2852 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2853 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2854 BOOL vs
= use_vs(state
);
2855 BOOL ps
= use_ps(state
);
2858 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2859 * that would be really messy and require shader recompilation
2860 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2861 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2864 device_map_psamplers(device
, d3d_info
);
2866 device_map_fixed_function_samplers(device
, d3d_info
);
2869 device_map_vsamplers(device
, ps
, gl_info
);
2872 void CDECL
wined3d_device_set_pixel_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2874 struct wined3d_shader
*prev
= device
->updateStateBlock
->state
.pixel_shader
;
2876 TRACE("device %p, shader %p.\n", device
, shader
);
2879 wined3d_shader_incref(shader
);
2881 wined3d_shader_decref(prev
);
2883 device
->updateStateBlock
->state
.pixel_shader
= shader
;
2884 device
->updateStateBlock
->changed
.pixelShader
= TRUE
;
2886 if (device
->isRecordingState
)
2888 TRACE("Recording... not performing anything.\n");
2894 TRACE("Application is setting the old shader over, nothing to do.\n");
2898 device_invalidate_state(device
, STATE_PIXELSHADER
);
2901 struct wined3d_shader
* CDECL
wined3d_device_get_pixel_shader(const struct wined3d_device
*device
)
2903 TRACE("device %p.\n", device
);
2905 return device
->stateBlock
->state
.pixel_shader
;
2908 void CDECL
wined3d_device_set_ps_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2910 struct wined3d_buffer
*prev
;
2912 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2914 if (idx
>= MAX_CONSTANT_BUFFERS
)
2916 WARN("Invalid constant buffer index %u.\n", idx
);
2920 prev
= device
->updateStateBlock
->state
.ps_cb
[idx
];
2921 device
->updateStateBlock
->state
.ps_cb
[idx
] = buffer
;
2923 if (device
->isRecordingState
)
2926 wined3d_buffer_incref(buffer
);
2928 wined3d_buffer_decref(prev
);
2936 InterlockedIncrement(&buffer
->resource
.bind_count
);
2937 wined3d_buffer_incref(buffer
);
2941 InterlockedDecrement(&prev
->resource
.bind_count
);
2942 wined3d_buffer_decref(prev
);
2947 struct wined3d_buffer
* CDECL
wined3d_device_get_ps_cb(const struct wined3d_device
*device
, UINT idx
)
2949 TRACE("device %p, idx %u.\n", device
, idx
);
2951 if (idx
>= MAX_CONSTANT_BUFFERS
)
2953 WARN("Invalid constant buffer index %u.\n", idx
);
2957 return device
->stateBlock
->state
.ps_cb
[idx
];
2960 void CDECL
wined3d_device_set_ps_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2962 struct wined3d_sampler
*prev
;
2964 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2966 if (idx
>= MAX_SAMPLER_OBJECTS
)
2968 WARN("Invalid sampler index %u.\n", idx
);
2972 prev
= device
->updateStateBlock
->state
.ps_sampler
[idx
];
2973 device
->updateStateBlock
->state
.ps_sampler
[idx
] = sampler
;
2976 wined3d_sampler_incref(sampler
);
2978 wined3d_sampler_decref(prev
);
2981 struct wined3d_sampler
* CDECL
wined3d_device_get_ps_sampler(const struct wined3d_device
*device
, UINT idx
)
2983 TRACE("device %p, idx %u.\n", device
, idx
);
2985 if (idx
>= MAX_SAMPLER_OBJECTS
)
2987 WARN("Invalid sampler index %u.\n", idx
);
2991 return device
->stateBlock
->state
.ps_sampler
[idx
];
2994 HRESULT CDECL
wined3d_device_set_ps_consts_b(struct wined3d_device
*device
,
2995 UINT start_register
, const BOOL
*constants
, UINT bool_count
)
2997 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
3000 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3001 device
, start_register
, constants
, bool_count
);
3003 if (!constants
|| start_register
>= MAX_CONST_B
)
3004 return WINED3DERR_INVALIDCALL
;
3006 memcpy(&device
->updateStateBlock
->state
.ps_consts_b
[start_register
], constants
, count
* sizeof(BOOL
));
3007 for (i
= 0; i
< count
; ++i
)
3008 TRACE("Set BOOL constant %u to %s.\n", start_register
+ i
, constants
[i
] ? "true" : "false");
3010 for (i
= start_register
; i
< count
+ start_register
; ++i
)
3011 device
->updateStateBlock
->changed
.pixelShaderConstantsB
|= (1 << i
);
3013 if (!device
->isRecordingState
)
3014 device_invalidate_state(device
, STATE_PIXELSHADERCONSTANT
);
3019 HRESULT CDECL
wined3d_device_get_ps_consts_b(const struct wined3d_device
*device
,
3020 UINT start_register
, BOOL
*constants
, UINT bool_count
)
3022 UINT count
= min(bool_count
, MAX_CONST_B
- start_register
);
3024 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3025 device
, start_register
, constants
, bool_count
);
3027 if (!constants
|| start_register
>= MAX_CONST_B
)
3028 return WINED3DERR_INVALIDCALL
;
3030 memcpy(constants
, &device
->stateBlock
->state
.ps_consts_b
[start_register
], count
* sizeof(BOOL
));
3035 HRESULT CDECL
wined3d_device_set_ps_consts_i(struct wined3d_device
*device
,
3036 UINT start_register
, const int *constants
, UINT vector4i_count
)
3038 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
3041 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3042 device
, start_register
, constants
, vector4i_count
);
3044 if (!constants
|| start_register
>= MAX_CONST_I
)
3045 return WINED3DERR_INVALIDCALL
;
3047 memcpy(&device
->updateStateBlock
->state
.ps_consts_i
[start_register
* 4], constants
, count
* sizeof(int) * 4);
3048 for (i
= 0; i
< count
; ++i
)
3049 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register
+ i
,
3050 constants
[i
* 4], constants
[i
* 4 + 1],
3051 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
3053 for (i
= start_register
; i
< count
+ start_register
; ++i
)
3054 device
->updateStateBlock
->changed
.pixelShaderConstantsI
|= (1 << i
);
3056 if (!device
->isRecordingState
)
3057 device_invalidate_state(device
, STATE_PIXELSHADERCONSTANT
);
3062 HRESULT CDECL
wined3d_device_get_ps_consts_i(const struct wined3d_device
*device
,
3063 UINT start_register
, int *constants
, UINT vector4i_count
)
3065 UINT count
= min(vector4i_count
, MAX_CONST_I
- start_register
);
3067 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3068 device
, start_register
, constants
, vector4i_count
);
3070 if (!constants
|| start_register
>= MAX_CONST_I
)
3071 return WINED3DERR_INVALIDCALL
;
3073 memcpy(constants
, &device
->stateBlock
->state
.ps_consts_i
[start_register
* 4], count
* sizeof(int) * 4);
3078 HRESULT CDECL
wined3d_device_set_ps_consts_f(struct wined3d_device
*device
,
3079 UINT start_register
, const float *constants
, UINT vector4f_count
)
3082 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3084 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3085 device
, start_register
, constants
, vector4f_count
);
3087 /* Specifically test start_register > limit to catch MAX_UINT overflows
3088 * when adding start_register + vector4f_count. */
3090 || start_register
+ vector4f_count
> d3d_info
->limits
.ps_uniform_count
3091 || start_register
> d3d_info
->limits
.ps_uniform_count
)
3092 return WINED3DERR_INVALIDCALL
;
3094 memcpy(&device
->updateStateBlock
->state
.ps_consts_f
[start_register
* 4],
3095 constants
, vector4f_count
* sizeof(float) * 4);
3098 for (i
= 0; i
< vector4f_count
; ++i
)
3099 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register
+ i
,
3100 constants
[i
* 4], constants
[i
* 4 + 1],
3101 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
3104 if (!device
->isRecordingState
)
3106 device
->shader_backend
->shader_update_float_pixel_constants(device
, start_register
, vector4f_count
);
3107 device_invalidate_state(device
, STATE_PIXELSHADERCONSTANT
);
3110 memset(device
->updateStateBlock
->changed
.pixelShaderConstantsF
+ start_register
, 1,
3111 sizeof(*device
->updateStateBlock
->changed
.pixelShaderConstantsF
) * vector4f_count
);
3116 HRESULT CDECL
wined3d_device_get_ps_consts_f(const struct wined3d_device
*device
,
3117 UINT start_register
, float *constants
, UINT vector4f_count
)
3119 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3120 int count
= min(vector4f_count
, d3d_info
->limits
.ps_uniform_count
- start_register
);
3122 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3123 device
, start_register
, constants
, vector4f_count
);
3125 if (!constants
|| count
< 0)
3126 return WINED3DERR_INVALIDCALL
;
3128 memcpy(constants
, &device
->stateBlock
->state
.ps_consts_f
[start_register
* 4], count
* sizeof(float) * 4);
3133 void CDECL
wined3d_device_set_geometry_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
3135 struct wined3d_shader
*prev
= device
->updateStateBlock
->state
.geometry_shader
;
3137 TRACE("device %p, shader %p.\n", device
, shader
);
3140 wined3d_shader_incref(shader
);
3142 wined3d_shader_decref(prev
);
3144 device
->updateStateBlock
->state
.geometry_shader
= shader
;
3146 if (device
->isRecordingState
|| shader
== prev
)
3149 device_invalidate_state(device
, STATE_GEOMETRY_SHADER
);
3152 struct wined3d_shader
* CDECL
wined3d_device_get_geometry_shader(const struct wined3d_device
*device
)
3154 TRACE("device %p.\n", device
);
3156 return device
->stateBlock
->state
.geometry_shader
;
3159 void CDECL
wined3d_device_set_gs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
3161 struct wined3d_buffer
*prev
;
3163 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
3165 if (idx
>= MAX_CONSTANT_BUFFERS
)
3167 WARN("Invalid constant buffer index %u.\n", idx
);
3171 prev
= device
->updateStateBlock
->state
.gs_cb
[idx
];
3172 device
->updateStateBlock
->state
.gs_cb
[idx
] = buffer
;
3174 if (device
->isRecordingState
)
3177 wined3d_buffer_incref(buffer
);
3179 wined3d_buffer_decref(prev
);
3187 InterlockedIncrement(&buffer
->resource
.bind_count
);
3188 wined3d_buffer_incref(buffer
);
3192 InterlockedDecrement(&prev
->resource
.bind_count
);
3193 wined3d_buffer_decref(prev
);
3198 struct wined3d_buffer
* CDECL
wined3d_device_get_gs_cb(const struct wined3d_device
*device
, UINT idx
)
3200 TRACE("device %p, idx %u.\n", device
, idx
);
3202 if (idx
>= MAX_CONSTANT_BUFFERS
)
3204 WARN("Invalid constant buffer index %u.\n", idx
);
3208 return device
->stateBlock
->state
.gs_cb
[idx
];
3211 void CDECL
wined3d_device_set_gs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
3213 struct wined3d_sampler
*prev
;
3215 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
3217 if (idx
>= MAX_SAMPLER_OBJECTS
)
3219 WARN("Invalid sampler index %u.\n", idx
);
3223 prev
= device
->updateStateBlock
->state
.gs_sampler
[idx
];
3224 device
->updateStateBlock
->state
.gs_sampler
[idx
] = sampler
;
3227 wined3d_sampler_incref(sampler
);
3229 wined3d_sampler_decref(prev
);
3232 struct wined3d_sampler
* CDECL
wined3d_device_get_gs_sampler(const struct wined3d_device
*device
, UINT idx
)
3234 TRACE("device %p, idx %u.\n", device
, idx
);
3236 if (idx
>= MAX_SAMPLER_OBJECTS
)
3238 WARN("Invalid sampler index %u.\n", idx
);
3242 return device
->stateBlock
->state
.gs_sampler
[idx
];
3245 /* Context activation is done by the caller. */
3246 /* Do not call while under the GL lock. */
3247 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3248 static HRESULT
process_vertices_strided(const struct wined3d_device
*device
, DWORD dwDestIndex
, DWORD dwCount
,
3249 const struct wined3d_stream_info
*stream_info
, struct wined3d_buffer
*dest
, DWORD flags
,
3252 struct wined3d_matrix mat
, proj_mat
, view_mat
, world_mat
;
3253 struct wined3d_viewport vp
;
3261 if (stream_info
->use_map
& (1 << WINED3D_FFP_NORMAL
))
3263 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3266 if (!(stream_info
->use_map
& (1 << WINED3D_FFP_POSITION
)))
3268 ERR("Source has no position mask\n");
3269 return WINED3DERR_INVALIDCALL
;
3272 if (device
->stateBlock
->state
.render_states
[WINED3D_RS_CLIPPING
])
3274 static BOOL warned
= FALSE
;
3276 * The clipping code is not quite correct. Some things need
3277 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3278 * so disable clipping for now.
3279 * (The graphics in Half-Life are broken, and my processvertices
3280 * test crashes with IDirect3DDevice3)
3286 FIXME("Clipping is broken and disabled for now\n");
3292 vertex_size
= get_flexible_vertex_size(DestFVF
);
3293 if (FAILED(hr
= wined3d_buffer_map(dest
, dwDestIndex
* vertex_size
, dwCount
* vertex_size
, &dest_ptr
, 0)))
3295 WARN("Failed to map buffer, hr %#x.\n", hr
);
3299 wined3d_device_get_transform(device
, WINED3D_TS_VIEW
, &view_mat
);
3300 wined3d_device_get_transform(device
, WINED3D_TS_PROJECTION
, &proj_mat
);
3301 wined3d_device_get_transform(device
, WINED3D_TS_WORLD_MATRIX(0), &world_mat
);
3303 TRACE("View mat:\n");
3304 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
);
3305 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
);
3306 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
);
3307 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
);
3309 TRACE("Proj mat:\n");
3310 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
);
3311 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
);
3312 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
);
3313 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
);
3315 TRACE("World mat:\n");
3316 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
);
3317 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
);
3318 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
);
3319 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
);
3321 /* Get the viewport */
3322 wined3d_device_get_viewport(device
, &vp
);
3323 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3324 vp
.x
, vp
.y
, vp
.width
, vp
.height
, vp
.min_z
, vp
.max_z
);
3326 multiply_matrix(&mat
,&view_mat
,&world_mat
);
3327 multiply_matrix(&mat
,&proj_mat
,&mat
);
3329 numTextures
= (DestFVF
& WINED3DFVF_TEXCOUNT_MASK
) >> WINED3DFVF_TEXCOUNT_SHIFT
;
3331 for (i
= 0; i
< dwCount
; i
+= 1) {
3332 unsigned int tex_index
;
3334 if ( ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZ
) ||
3335 ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
) ) {
3336 /* The position first */
3337 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_POSITION
];
3338 const float *p
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3340 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p
[0], p
[1], p
[2]);
3342 /* Multiplication with world, view and projection matrix */
3343 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
);
3344 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
);
3345 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
);
3346 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
);
3348 TRACE("x=%f y=%f z=%f rhw=%f\n", x
, y
, z
, rhw
);
3350 /* WARNING: The following things are taken from d3d7 and were not yet checked
3351 * against d3d8 or d3d9!
3354 /* Clipping conditions: From msdn
3356 * A vertex is clipped if it does not match the following requirements
3360 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3362 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3363 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3368 ( (-rhw
-eps
< x
) && (-rhw
-eps
< y
) && ( -eps
< z
) &&
3369 (x
<= rhw
+ eps
) && (y
<= rhw
+ eps
) && (z
<= rhw
+ eps
) &&
3372 /* "Normal" viewport transformation (not clipped)
3373 * 1) The values are divided by rhw
3374 * 2) The y axis is negative, so multiply it with -1
3375 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3376 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3377 * 4) Multiply x with Width/2 and add Width/2
3378 * 5) The same for the height
3379 * 6) Add the viewpoint X and Y to the 2D coordinates and
3380 * The minimum Z value to z
3381 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3383 * Well, basically it's simply a linear transformation into viewport
3395 z
*= vp
.max_z
- vp
.min_z
;
3397 x
+= vp
.width
/ 2 + vp
.x
;
3398 y
+= vp
.height
/ 2 + vp
.y
;
3403 /* That vertex got clipped
3404 * Contrary to OpenGL it is not dropped completely, it just
3405 * undergoes a different calculation.
3407 TRACE("Vertex got clipped\n");
3414 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3415 * outside of the main vertex buffer memory. That needs some more
3420 TRACE("Writing (%f %f %f) %f\n", x
, y
, z
, rhw
);
3423 ( (float *) dest_ptr
)[0] = x
;
3424 ( (float *) dest_ptr
)[1] = y
;
3425 ( (float *) dest_ptr
)[2] = z
;
3426 ( (float *) dest_ptr
)[3] = rhw
; /* SIC, see ddraw test! */
3428 dest_ptr
+= 3 * sizeof(float);
3430 if ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
)
3431 dest_ptr
+= sizeof(float);
3434 if (DestFVF
& WINED3DFVF_PSIZE
)
3435 dest_ptr
+= sizeof(DWORD
);
3437 if (DestFVF
& WINED3DFVF_NORMAL
)
3439 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_NORMAL
];
3440 const float *normal
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3441 /* AFAIK this should go into the lighting information */
3442 FIXME("Didn't expect the destination to have a normal\n");
3443 copy_and_next(dest_ptr
, normal
, 3 * sizeof(float));
3446 if (DestFVF
& WINED3DFVF_DIFFUSE
)
3448 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_DIFFUSE
];
3449 const DWORD
*color_d
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3450 if (!(stream_info
->use_map
& (1 << WINED3D_FFP_DIFFUSE
)))
3452 static BOOL warned
= FALSE
;
3455 ERR("No diffuse color in source, but destination has one\n");
3459 *( (DWORD
*) dest_ptr
) = 0xffffffff;
3460 dest_ptr
+= sizeof(DWORD
);
3464 copy_and_next(dest_ptr
, color_d
, sizeof(DWORD
));
3468 if (DestFVF
& WINED3DFVF_SPECULAR
)
3470 /* What's the color value in the feedback buffer? */
3471 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_SPECULAR
];
3472 const DWORD
*color_s
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3473 if (!(stream_info
->use_map
& (1 << WINED3D_FFP_SPECULAR
)))
3475 static BOOL warned
= FALSE
;
3478 ERR("No specular color in source, but destination has one\n");
3482 *(DWORD
*)dest_ptr
= 0xff000000;
3483 dest_ptr
+= sizeof(DWORD
);
3487 copy_and_next(dest_ptr
, color_s
, sizeof(DWORD
));
3491 for (tex_index
= 0; tex_index
< numTextures
; ++tex_index
)
3493 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_TEXCOORD0
+ tex_index
];
3494 const float *tex_coord
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3495 if (!(stream_info
->use_map
& (1 << (WINED3D_FFP_TEXCOORD0
+ tex_index
))))
3497 ERR("No source texture, but destination requests one\n");
3498 dest_ptr
+= GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float);
3502 copy_and_next(dest_ptr
, tex_coord
, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float));
3507 wined3d_buffer_unmap(dest
);
3511 #undef copy_and_next
3513 /* Do not call while under the GL lock. */
3514 HRESULT CDECL
wined3d_device_process_vertices(struct wined3d_device
*device
,
3515 UINT src_start_idx
, UINT dst_idx
, UINT vertex_count
, struct wined3d_buffer
*dst_buffer
,
3516 const struct wined3d_vertex_declaration
*declaration
, DWORD flags
, DWORD dst_fvf
)
3518 struct wined3d_state
*state
= &device
->stateBlock
->state
;
3519 struct wined3d_stream_info stream_info
;
3520 const struct wined3d_gl_info
*gl_info
;
3521 struct wined3d_context
*context
;
3522 struct wined3d_shader
*vs
;
3526 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3527 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3528 device
, src_start_idx
, dst_idx
, vertex_count
,
3529 dst_buffer
, declaration
, flags
, dst_fvf
);
3532 FIXME("Output vertex declaration not implemented yet.\n");
3534 /* Need any context to write to the vbo. */
3535 context
= context_acquire(device
, NULL
);
3536 gl_info
= context
->gl_info
;
3538 vs
= state
->vertex_shader
;
3539 state
->vertex_shader
= NULL
;
3540 device_stream_info_from_declaration(device
, &stream_info
);
3541 state
->vertex_shader
= vs
;
3543 /* We can't convert FROM a VBO, and vertex buffers used to source into
3544 * process_vertices() are unlikely to ever be used for drawing. Release
3545 * VBOs in those buffers and fix up the stream_info structure.
3547 * Also apply the start index. */
3548 for (i
= 0; i
< (sizeof(stream_info
.elements
) / sizeof(*stream_info
.elements
)); ++i
)
3550 struct wined3d_stream_info_element
*e
;
3552 if (!(stream_info
.use_map
& (1 << i
)))
3555 e
= &stream_info
.elements
[i
];
3556 if (e
->data
.buffer_object
)
3558 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
3559 e
->data
.buffer_object
= 0;
3560 e
->data
.addr
= (BYTE
*)((ULONG_PTR
)e
->data
.addr
+ (ULONG_PTR
)buffer_get_sysmem(vb
, gl_info
));
3561 GL_EXTCALL(glDeleteBuffersARB(1, &vb
->buffer_object
));
3562 vb
->buffer_object
= 0;
3565 e
->data
.addr
+= e
->stride
* src_start_idx
;
3568 hr
= process_vertices_strided(device
, dst_idx
, vertex_count
,
3569 &stream_info
, dst_buffer
, flags
, dst_fvf
);
3571 context_release(context
);
3576 void CDECL
wined3d_device_set_texture_stage_state(struct wined3d_device
*device
,
3577 UINT stage
, enum wined3d_texture_stage_state state
, DWORD value
)
3579 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3582 TRACE("device %p, stage %u, state %s, value %#x.\n",
3583 device
, stage
, debug_d3dtexturestate(state
), value
);
3585 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3587 WARN("Invalid state %#x passed.\n", state
);
3591 if (stage
>= d3d_info
->limits
.ffp_blend_stages
)
3593 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3594 stage
, d3d_info
->limits
.ffp_blend_stages
- 1);
3598 old_value
= device
->updateStateBlock
->state
.texture_states
[stage
][state
];
3599 device
->updateStateBlock
->changed
.textureState
[stage
] |= 1 << state
;
3600 device
->updateStateBlock
->state
.texture_states
[stage
][state
] = value
;
3602 if (device
->isRecordingState
)
3604 TRACE("Recording... not performing anything.\n");
3608 /* Checked after the assignments to allow proper stateblock recording. */
3609 if (old_value
== value
)
3611 TRACE("Application is setting the old value over, nothing to do.\n");
3615 if (stage
> device
->stateBlock
->state
.lowest_disabled_stage
3616 && device
->StateTable
[STATE_TEXTURESTAGE(0, state
)].representative
3617 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
))
3619 /* Colorop change above lowest disabled stage? That won't change
3620 * anything in the GL setup. Changes in other states are important on
3621 * disabled stages too. */
3625 if (state
== WINED3D_TSS_COLOR_OP
)
3629 if (value
== WINED3D_TOP_DISABLE
&& old_value
!= WINED3D_TOP_DISABLE
)
3631 /* Previously enabled stage disabled now. Make sure to dirtify
3632 * all enabled stages above stage, they have to be disabled.
3634 * The current stage is dirtified below. */
3635 for (i
= stage
+ 1; i
< device
->stateBlock
->state
.lowest_disabled_stage
; ++i
)
3637 TRACE("Additionally dirtifying stage %u.\n", i
);
3638 device_invalidate_state(device
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3640 device
->stateBlock
->state
.lowest_disabled_stage
= stage
;
3641 TRACE("New lowest disabled: %u.\n", stage
);
3643 else if (value
!= WINED3D_TOP_DISABLE
&& old_value
== WINED3D_TOP_DISABLE
)
3645 /* Previously disabled stage enabled. Stages above it may need
3646 * enabling. Stage must be lowest_disabled_stage here, if it's
3647 * bigger success is returned above, and stages below the lowest
3648 * disabled stage can't be enabled (because they are enabled
3651 * Again stage stage doesn't need to be dirtified here, it is
3653 for (i
= stage
+ 1; i
< d3d_info
->limits
.ffp_blend_stages
; ++i
)
3655 if (device
->updateStateBlock
->state
.texture_states
[i
][WINED3D_TSS_COLOR_OP
] == WINED3D_TOP_DISABLE
)
3657 TRACE("Additionally dirtifying stage %u due to enable.\n", i
);
3658 device_invalidate_state(device
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3660 device
->stateBlock
->state
.lowest_disabled_stage
= i
;
3661 TRACE("New lowest disabled: %u.\n", i
);
3665 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, state
));
3668 DWORD CDECL
wined3d_device_get_texture_stage_state(const struct wined3d_device
*device
,
3669 UINT stage
, enum wined3d_texture_stage_state state
)
3671 TRACE("device %p, stage %u, state %s.\n",
3672 device
, stage
, debug_d3dtexturestate(state
));
3674 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3676 WARN("Invalid state %#x passed.\n", state
);
3680 return device
->updateStateBlock
->state
.texture_states
[stage
][state
];
3683 HRESULT CDECL
wined3d_device_set_texture(struct wined3d_device
*device
,
3684 UINT stage
, struct wined3d_texture
*texture
)
3686 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3687 struct wined3d_texture
*prev
;
3689 TRACE("device %p, stage %u, texture %p.\n", device
, stage
, texture
);
3691 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3692 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3694 /* Windows accepts overflowing this array... we do not. */
3695 if (stage
>= sizeof(device
->stateBlock
->state
.textures
) / sizeof(*device
->stateBlock
->state
.textures
))
3697 WARN("Ignoring invalid stage %u.\n", stage
);
3701 if (texture
&& texture
->resource
.pool
== WINED3D_POOL_SCRATCH
)
3703 WARN("Rejecting attempt to set scratch texture.\n");
3704 return WINED3DERR_INVALIDCALL
;
3707 device
->updateStateBlock
->changed
.textures
|= 1 << stage
;
3709 prev
= device
->updateStateBlock
->state
.textures
[stage
];
3710 TRACE("Previous texture %p.\n", prev
);
3712 if (texture
== prev
)
3714 TRACE("App is setting the same texture again, nothing to do.\n");
3718 TRACE("Setting new texture to %p.\n", texture
);
3719 device
->updateStateBlock
->state
.textures
[stage
] = texture
;
3721 if (device
->isRecordingState
)
3723 TRACE("Recording... not performing anything\n");
3725 if (texture
) wined3d_texture_incref(texture
);
3726 if (prev
) wined3d_texture_decref(prev
);
3733 LONG bind_count
= InterlockedIncrement(&texture
->resource
.bind_count
);
3735 wined3d_texture_incref(texture
);
3737 if (!prev
|| texture
->target
!= prev
->target
)
3738 device_invalidate_state(device
, STATE_PIXELSHADER
);
3740 if (!prev
&& stage
< d3d_info
->limits
.ffp_blend_stages
)
3742 /* The source arguments for color and alpha ops have different
3743 * meanings when a NULL texture is bound, so the COLOR_OP and
3744 * ALPHA_OP have to be dirtified. */
3745 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_COLOR_OP
));
3746 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_ALPHA_OP
));
3749 if (bind_count
== 1)
3750 texture
->sampler
= stage
;
3755 LONG bind_count
= InterlockedDecrement(&prev
->resource
.bind_count
);
3757 if (!texture
&& stage
< d3d_info
->limits
.ffp_blend_stages
)
3759 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_COLOR_OP
));
3760 device_invalidate_state(device
, STATE_TEXTURESTAGE(stage
, WINED3D_TSS_ALPHA_OP
));
3763 if (bind_count
&& prev
->sampler
== stage
)
3767 /* Search for other stages the texture is bound to. Shouldn't
3768 * happen if applications bind textures to a single stage only. */
3769 TRACE("Searching for other stages the texture is bound to.\n");
3770 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
3772 if (device
->updateStateBlock
->state
.textures
[i
] == prev
)
3774 TRACE("Texture is also bound to stage %u.\n", i
);
3781 wined3d_texture_decref(prev
);
3784 device_invalidate_state(device
, STATE_SAMPLER(stage
));
3789 struct wined3d_texture
* CDECL
wined3d_device_get_texture(const struct wined3d_device
*device
, UINT stage
)
3791 TRACE("device %p, stage %u.\n", device
, stage
);
3793 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3794 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3796 if (stage
>= sizeof(device
->stateBlock
->state
.textures
) / sizeof(*device
->stateBlock
->state
.textures
))
3798 WARN("Ignoring invalid stage %u.\n", stage
);
3799 return NULL
; /* Windows accepts overflowing this array ... we do not. */
3802 return device
->stateBlock
->state
.textures
[stage
];
3805 HRESULT CDECL
wined3d_device_get_back_buffer(const struct wined3d_device
*device
, UINT swapchain_idx
,
3806 UINT backbuffer_idx
, enum wined3d_backbuffer_type backbuffer_type
, struct wined3d_surface
**backbuffer
)
3808 struct wined3d_swapchain
*swapchain
;
3810 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3811 device
, swapchain_idx
, backbuffer_idx
, backbuffer_type
, backbuffer
);
3813 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3814 return WINED3DERR_INVALIDCALL
;
3816 if (!(*backbuffer
= wined3d_swapchain_get_back_buffer(swapchain
, backbuffer_idx
, backbuffer_type
)))
3817 return WINED3DERR_INVALIDCALL
;
3821 HRESULT CDECL
wined3d_device_get_device_caps(const struct wined3d_device
*device
, WINED3DCAPS
*caps
)
3823 TRACE("device %p, caps %p.\n", device
, caps
);
3825 return wined3d_get_device_caps(device
->wined3d
, device
->adapter
->ordinal
,
3826 device
->create_parms
.device_type
, caps
);
3829 HRESULT CDECL
wined3d_device_get_display_mode(const struct wined3d_device
*device
, UINT swapchain_idx
,
3830 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
3832 struct wined3d_swapchain
*swapchain
;
3834 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3835 device
, swapchain_idx
, mode
, rotation
);
3837 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3838 return WINED3DERR_INVALIDCALL
;
3840 return wined3d_swapchain_get_display_mode(swapchain
, mode
, rotation
);
3843 HRESULT CDECL
wined3d_device_begin_stateblock(struct wined3d_device
*device
)
3845 struct wined3d_stateblock
*stateblock
;
3848 TRACE("device %p.\n", device
);
3850 if (device
->isRecordingState
)
3851 return WINED3DERR_INVALIDCALL
;
3853 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_RECORDED
, &stateblock
);
3857 wined3d_stateblock_decref(device
->updateStateBlock
);
3858 device
->updateStateBlock
= stateblock
;
3859 device
->isRecordingState
= TRUE
;
3861 TRACE("Recording stateblock %p.\n", stateblock
);
3866 HRESULT CDECL
wined3d_device_end_stateblock(struct wined3d_device
*device
,
3867 struct wined3d_stateblock
**stateblock
)
3869 struct wined3d_stateblock
*object
= device
->updateStateBlock
;
3871 TRACE("device %p, stateblock %p.\n", device
, stateblock
);
3873 if (!device
->isRecordingState
)
3875 WARN("Not recording.\n");
3877 return WINED3DERR_INVALIDCALL
;
3880 stateblock_init_contained_states(object
);
3882 *stateblock
= object
;
3883 device
->isRecordingState
= FALSE
;
3884 device
->updateStateBlock
= device
->stateBlock
;
3885 wined3d_stateblock_incref(device
->updateStateBlock
);
3887 TRACE("Returning stateblock %p.\n", *stateblock
);
3892 HRESULT CDECL
wined3d_device_begin_scene(struct wined3d_device
*device
)
3894 /* At the moment we have no need for any functionality at the beginning
3896 TRACE("device %p.\n", device
);
3898 if (device
->inScene
)
3900 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3901 return WINED3DERR_INVALIDCALL
;
3903 device
->inScene
= TRUE
;
3907 HRESULT CDECL
wined3d_device_end_scene(struct wined3d_device
*device
)
3909 struct wined3d_context
*context
;
3911 TRACE("device %p.\n", device
);
3913 if (!device
->inScene
)
3915 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3916 return WINED3DERR_INVALIDCALL
;
3919 context
= context_acquire(device
, NULL
);
3920 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3921 context
->gl_info
->gl_ops
.gl
.p_glFlush();
3922 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3924 context_release(context
);
3926 device
->inScene
= FALSE
;
3930 HRESULT CDECL
wined3d_device_present(const struct wined3d_device
*device
, const RECT
*src_rect
,
3931 const RECT
*dst_rect
, HWND dst_window_override
, const RGNDATA
*dirty_region
, DWORD flags
)
3935 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3936 device
, wine_dbgstr_rect(src_rect
), wine_dbgstr_rect(dst_rect
),
3937 dst_window_override
, dirty_region
, flags
);
3939 for (i
= 0; i
< device
->swapchain_count
; ++i
)
3941 wined3d_swapchain_present(device
->swapchains
[i
], src_rect
,
3942 dst_rect
, dst_window_override
, dirty_region
, flags
);
3948 /* Do not call while under the GL lock. */
3949 HRESULT CDECL
wined3d_device_clear(struct wined3d_device
*device
, DWORD rect_count
,
3950 const RECT
*rects
, DWORD flags
, const struct wined3d_color
*color
, float depth
, DWORD stencil
)
3954 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3955 device
, rect_count
, rects
, flags
, color
->r
, color
->g
, color
->b
, color
->a
, depth
, stencil
);
3957 if (!rect_count
&& rects
)
3959 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects
);
3963 if (flags
& (WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
))
3965 struct wined3d_surface
*ds
= device
->fb
.depth_stencil
;
3968 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3969 /* TODO: What about depth stencil buffers without stencil bits? */
3970 return WINED3DERR_INVALIDCALL
;
3972 else if (flags
& WINED3DCLEAR_TARGET
)
3974 if (ds
->resource
.width
< device
->fb
.render_targets
[0]->resource
.width
3975 || ds
->resource
.height
< device
->fb
.render_targets
[0]->resource
.height
)
3977 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3983 wined3d_get_draw_rect(&device
->stateBlock
->state
, &draw_rect
);
3984 device_clear_render_targets(device
, device
->adapter
->gl_info
.limits
.buffers
,
3985 &device
->fb
, rect_count
, rects
, &draw_rect
, flags
, color
, depth
, stencil
);
3990 void CDECL
wined3d_device_set_primitive_type(struct wined3d_device
*device
,
3991 enum wined3d_primitive_type primitive_type
)
3993 GLenum gl_primitive_type
, prev
;
3995 TRACE("device %p, primitive_type %s\n", device
, debug_d3dprimitivetype(primitive_type
));
3997 device
->updateStateBlock
->changed
.primitive_type
= TRUE
;
3998 gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
3999 prev
= device
->updateStateBlock
->state
.gl_primitive_type
;
4000 device
->updateStateBlock
->state
.gl_primitive_type
= gl_primitive_type
;
4001 if (!device
->isRecordingState
&& gl_primitive_type
!= prev
4002 && (gl_primitive_type
== GL_POINTS
|| prev
== GL_POINTS
))
4003 device_invalidate_state(device
, STATE_POINT_SIZE_ENABLE
);
4006 void CDECL
wined3d_device_get_primitive_type(const struct wined3d_device
*device
,
4007 enum wined3d_primitive_type
*primitive_type
)
4009 TRACE("device %p, primitive_type %p\n", device
, primitive_type
);
4011 *primitive_type
= d3d_primitive_type_from_gl(device
->stateBlock
->state
.gl_primitive_type
);
4013 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type
));
4016 HRESULT CDECL
wined3d_device_draw_primitive(struct wined3d_device
*device
, UINT start_vertex
, UINT vertex_count
)
4018 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device
, start_vertex
, vertex_count
);
4020 if (!device
->stateBlock
->state
.vertex_declaration
)
4022 WARN("Called without a valid vertex declaration set.\n");
4023 return WINED3DERR_INVALIDCALL
;
4026 if (device
->stateBlock
->state
.load_base_vertex_index
)
4028 device
->stateBlock
->state
.load_base_vertex_index
= 0;
4029 device_invalidate_state(device
, STATE_BASEVERTEXINDEX
);
4032 /* Account for the loading offset due to index buffers. Instead of
4033 * reloading all sources correct it with the startvertex parameter. */
4034 draw_primitive(device
, start_vertex
, vertex_count
, 0, 0, FALSE
);
4038 HRESULT CDECL
wined3d_device_draw_indexed_primitive(struct wined3d_device
*device
, UINT start_idx
, UINT index_count
)
4040 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4042 TRACE("device %p, start_idx %u, index_count %u.\n", device
, start_idx
, index_count
);
4044 if (!device
->stateBlock
->state
.index_buffer
)
4046 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4047 * without an index buffer set. (The first time at least...)
4048 * D3D8 simply dies, but I doubt it can do much harm to return
4049 * D3DERR_INVALIDCALL there as well. */
4050 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4051 return WINED3DERR_INVALIDCALL
;
4054 if (!device
->stateBlock
->state
.vertex_declaration
)
4056 WARN("Called without a valid vertex declaration set.\n");
4057 return WINED3DERR_INVALIDCALL
;
4060 if (!gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
] &&
4061 device
->stateBlock
->state
.load_base_vertex_index
!= device
->stateBlock
->state
.base_vertex_index
)
4063 device
->stateBlock
->state
.load_base_vertex_index
= device
->stateBlock
->state
.base_vertex_index
;
4064 device_invalidate_state(device
, STATE_BASEVERTEXINDEX
);
4067 draw_primitive(device
, start_idx
, index_count
, 0, 0, TRUE
);
4072 void CDECL
wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device
*device
,
4073 UINT start_idx
, UINT index_count
, UINT start_instance
, UINT instance_count
)
4075 TRACE("device %p, start_idx %u, index_count %u.\n", device
, start_idx
, index_count
);
4077 draw_primitive(device
, start_idx
, index_count
, start_instance
, instance_count
, TRUE
);
4080 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4081 static HRESULT
device_update_volume(struct wined3d_device
*device
,
4082 struct wined3d_volume
*src_volume
, struct wined3d_volume
*dst_volume
)
4084 struct wined3d_map_desc src
;
4085 struct wined3d_map_desc dst
;
4088 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4089 device
, src_volume
, dst_volume
);
4091 /* TODO: Implement direct loading into the gl volume instead of using
4092 * memcpy and dirtification to improve loading performance. */
4093 if (FAILED(hr
= wined3d_volume_map(src_volume
, &src
, NULL
, WINED3D_MAP_READONLY
)))
4095 if (FAILED(hr
= wined3d_volume_map(dst_volume
, &dst
, NULL
, WINED3D_MAP_DISCARD
)))
4097 wined3d_volume_unmap(src_volume
);
4101 memcpy(dst
.data
, src
.data
, dst_volume
->resource
.size
);
4103 hr
= wined3d_volume_unmap(dst_volume
);
4105 wined3d_volume_unmap(src_volume
);
4107 hr
= wined3d_volume_unmap(src_volume
);
4112 HRESULT CDECL
wined3d_device_update_texture(struct wined3d_device
*device
,
4113 struct wined3d_texture
*src_texture
, struct wined3d_texture
*dst_texture
)
4115 enum wined3d_resource_type type
;
4116 unsigned int level_count
, i
;
4119 TRACE("device %p, src_texture %p, dst_texture %p.\n", device
, src_texture
, dst_texture
);
4121 /* Verify that the source and destination textures are non-NULL. */
4122 if (!src_texture
|| !dst_texture
)
4124 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4125 return WINED3DERR_INVALIDCALL
;
4128 if (src_texture
== dst_texture
)
4130 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4131 return WINED3DERR_INVALIDCALL
;
4134 /* Verify that the source and destination textures are the same type. */
4135 type
= src_texture
->resource
.type
;
4136 if (dst_texture
->resource
.type
!= type
)
4138 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4139 return WINED3DERR_INVALIDCALL
;
4142 /* Check that both textures have the identical numbers of levels. */
4143 level_count
= wined3d_texture_get_level_count(src_texture
);
4144 if (wined3d_texture_get_level_count(dst_texture
) != level_count
)
4146 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4147 return WINED3DERR_INVALIDCALL
;
4150 /* Make sure that the destination texture is loaded. */
4151 dst_texture
->texture_ops
->texture_preload(dst_texture
, SRGB_RGB
);
4153 /* Update every surface level of the texture. */
4156 case WINED3D_RTYPE_TEXTURE
:
4158 struct wined3d_surface
*src_surface
;
4159 struct wined3d_surface
*dst_surface
;
4161 for (i
= 0; i
< level_count
; ++i
)
4163 src_surface
= surface_from_resource(wined3d_texture_get_sub_resource(src_texture
, i
));
4164 dst_surface
= surface_from_resource(wined3d_texture_get_sub_resource(dst_texture
, i
));
4165 hr
= wined3d_device_update_surface(device
, src_surface
, NULL
, dst_surface
, NULL
);
4168 WARN("Failed to update surface, hr %#x.\n", hr
);
4175 case WINED3D_RTYPE_CUBE_TEXTURE
:
4177 struct wined3d_surface
*src_surface
;
4178 struct wined3d_surface
*dst_surface
;
4180 for (i
= 0; i
< level_count
* 6; ++i
)
4182 src_surface
= surface_from_resource(wined3d_texture_get_sub_resource(src_texture
, i
));
4183 dst_surface
= surface_from_resource(wined3d_texture_get_sub_resource(dst_texture
, i
));
4184 hr
= wined3d_device_update_surface(device
, src_surface
, NULL
, dst_surface
, NULL
);
4187 WARN("Failed to update surface, hr %#x.\n", hr
);
4194 case WINED3D_RTYPE_VOLUME_TEXTURE
:
4196 for (i
= 0; i
< level_count
; ++i
)
4198 hr
= device_update_volume(device
,
4199 volume_from_resource(wined3d_texture_get_sub_resource(src_texture
, i
)),
4200 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture
, i
)));
4203 WARN("Failed to update volume, hr %#x.\n", hr
);
4211 FIXME("Unsupported texture type %#x.\n", type
);
4212 return WINED3DERR_INVALIDCALL
;
4218 HRESULT CDECL
wined3d_device_get_front_buffer_data(const struct wined3d_device
*device
,
4219 UINT swapchain_idx
, struct wined3d_surface
*dst_surface
)
4221 struct wined3d_swapchain
*swapchain
;
4223 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device
, swapchain_idx
, dst_surface
);
4225 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4226 return WINED3DERR_INVALIDCALL
;
4228 return wined3d_swapchain_get_front_buffer_data(swapchain
, dst_surface
);
4231 HRESULT CDECL
wined3d_device_validate_device(const struct wined3d_device
*device
, DWORD
*num_passes
)
4233 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
4234 struct wined3d_texture
*texture
;
4237 TRACE("device %p, num_passes %p.\n", device
, num_passes
);
4239 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
4241 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] == WINED3D_TEXF_NONE
)
4243 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
4244 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
4246 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] == WINED3D_TEXF_NONE
)
4248 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
4249 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
4252 texture
= state
->textures
[i
];
4253 if (!texture
|| texture
->resource
.format
->flags
& WINED3DFMT_FLAG_FILTERING
) continue;
4255 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] != WINED3D_TEXF_POINT
)
4257 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i
);
4260 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] != WINED3D_TEXF_POINT
)
4262 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i
);
4265 if (state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_NONE
4266 && state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_POINT
)
4268 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i
);
4273 if (state
->render_states
[WINED3D_RS_ZENABLE
] || state
->render_states
[WINED3D_RS_ZWRITEENABLE
]
4274 || state
->render_states
[WINED3D_RS_STENCILENABLE
])
4276 struct wined3d_surface
*ds
= device
->fb
.depth_stencil
;
4277 struct wined3d_surface
*target
= device
->fb
.render_targets
[0];
4280 && (ds
->resource
.width
< target
->resource
.width
|| ds
->resource
.height
< target
->resource
.height
))
4282 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4283 return WINED3DERR_CONFLICTINGRENDERSTATE
;
4287 /* return a sensible default */
4290 TRACE("returning D3D_OK\n");
4294 void CDECL
wined3d_device_set_software_vertex_processing(struct wined3d_device
*device
, BOOL software
)
4298 TRACE("device %p, software %#x.\n", device
, software
);
4302 FIXME("device %p, software %#x stub!\n", device
, software
);
4306 device
->softwareVertexProcessing
= software
;
4309 BOOL CDECL
wined3d_device_get_software_vertex_processing(const struct wined3d_device
*device
)
4313 TRACE("device %p.\n", device
);
4317 TRACE("device %p stub!\n", device
);
4321 return device
->softwareVertexProcessing
;
4324 HRESULT CDECL
wined3d_device_get_raster_status(const struct wined3d_device
*device
,
4325 UINT swapchain_idx
, struct wined3d_raster_status
*raster_status
)
4327 struct wined3d_swapchain
*swapchain
;
4329 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4330 device
, swapchain_idx
, raster_status
);
4332 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4333 return WINED3DERR_INVALIDCALL
;
4335 return wined3d_swapchain_get_raster_status(swapchain
, raster_status
);
4338 HRESULT CDECL
wined3d_device_set_npatch_mode(struct wined3d_device
*device
, float segments
)
4342 TRACE("device %p, segments %.8e.\n", device
, segments
);
4344 if (segments
!= 0.0f
)
4348 FIXME("device %p, segments %.8e stub!\n", device
, segments
);
4356 float CDECL
wined3d_device_get_npatch_mode(const struct wined3d_device
*device
)
4360 TRACE("device %p.\n", device
);
4364 FIXME("device %p stub!\n", device
);
4371 HRESULT CDECL
wined3d_device_update_surface(struct wined3d_device
*device
,
4372 struct wined3d_surface
*src_surface
, const RECT
*src_rect
,
4373 struct wined3d_surface
*dst_surface
, const POINT
*dst_point
)
4375 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4376 device
, src_surface
, wine_dbgstr_rect(src_rect
),
4377 dst_surface
, wine_dbgstr_point(dst_point
));
4379 if (src_surface
->resource
.pool
!= WINED3D_POOL_SYSTEM_MEM
|| dst_surface
->resource
.pool
!= WINED3D_POOL_DEFAULT
)
4381 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4382 src_surface
, dst_surface
);
4383 return WINED3DERR_INVALIDCALL
;
4386 return surface_upload_from_surface(dst_surface
, dst_point
, src_surface
, src_rect
);
4389 /* Do not call while under the GL lock. */
4390 HRESULT CDECL
wined3d_device_color_fill(struct wined3d_device
*device
,
4391 struct wined3d_surface
*surface
, const RECT
*rect
, const struct wined3d_color
*color
)
4395 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4396 device
, surface
, wine_dbgstr_rect(rect
),
4397 color
->r
, color
->g
, color
->b
, color
->a
);
4399 if (surface
->resource
.pool
!= WINED3D_POOL_DEFAULT
&& surface
->resource
.pool
!= WINED3D_POOL_SYSTEM_MEM
)
4401 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface
->resource
.pool
));
4402 return WINED3DERR_INVALIDCALL
;
4407 SetRect(&r
, 0, 0, surface
->resource
.width
, surface
->resource
.height
);
4411 return surface_color_fill(surface
, rect
, color
);
4414 /* Do not call while under the GL lock. */
4415 void CDECL
wined3d_device_clear_rendertarget_view(struct wined3d_device
*device
,
4416 struct wined3d_rendertarget_view
*rendertarget_view
, const struct wined3d_color
*color
)
4418 struct wined3d_resource
*resource
;
4422 resource
= rendertarget_view
->resource
;
4423 if (resource
->type
!= WINED3D_RTYPE_SURFACE
)
4425 FIXME("Only supported on surface resources\n");
4429 SetRect(&rect
, 0, 0, resource
->width
, resource
->height
);
4430 hr
= surface_color_fill(surface_from_resource(resource
), &rect
, color
);
4431 if (FAILED(hr
)) ERR("Color fill failed, hr %#x.\n", hr
);
4434 struct wined3d_surface
* CDECL
wined3d_device_get_render_target(const struct wined3d_device
*device
,
4435 UINT render_target_idx
)
4437 TRACE("device %p, render_target_idx %u.\n", device
, render_target_idx
);
4439 if (render_target_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4441 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4445 return device
->fb
.render_targets
[render_target_idx
];
4448 struct wined3d_surface
* CDECL
wined3d_device_get_depth_stencil(const struct wined3d_device
*device
)
4450 TRACE("device %p.\n", device
);
4452 return device
->fb
.depth_stencil
;
4455 HRESULT CDECL
wined3d_device_set_render_target(struct wined3d_device
*device
,
4456 UINT render_target_idx
, struct wined3d_surface
*render_target
, BOOL set_viewport
)
4458 struct wined3d_surface
*prev
;
4460 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4461 device
, render_target_idx
, render_target
, set_viewport
);
4463 if (render_target_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4465 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4466 return WINED3DERR_INVALIDCALL
;
4469 /* Render target 0 can't be set to NULL. */
4470 if (!render_target
&& !render_target_idx
)
4472 WARN("Trying to set render target 0 to NULL.\n");
4473 return WINED3DERR_INVALIDCALL
;
4476 if (render_target
&& !(render_target
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
))
4478 FIXME("Surface %p doesn't have render target usage.\n", render_target
);
4479 return WINED3DERR_INVALIDCALL
;
4482 /* Set the viewport and scissor rectangles, if requested. Tests show that
4483 * stateblock recording is ignored, the change goes directly into the
4484 * primary stateblock. */
4485 if (!render_target_idx
&& set_viewport
)
4487 struct wined3d_state
*state
= &device
->stateBlock
->state
;
4489 state
->viewport
.x
= 0;
4490 state
->viewport
.y
= 0;
4491 state
->viewport
.width
= render_target
->resource
.width
;
4492 state
->viewport
.height
= render_target
->resource
.height
;
4493 state
->viewport
.min_z
= 0.0f
;
4494 state
->viewport
.max_z
= 1.0f
;
4495 device_invalidate_state(device
, STATE_VIEWPORT
);
4497 state
->scissor_rect
.top
= 0;
4498 state
->scissor_rect
.left
= 0;
4499 state
->scissor_rect
.right
= render_target
->resource
.width
;
4500 state
->scissor_rect
.bottom
= render_target
->resource
.height
;
4501 device_invalidate_state(device
, STATE_SCISSORRECT
);
4505 prev
= device
->fb
.render_targets
[render_target_idx
];
4506 if (render_target
== prev
)
4510 wined3d_surface_incref(render_target
);
4511 device
->fb
.render_targets
[render_target_idx
] = render_target
;
4512 /* Release after the assignment, to prevent device_resource_released()
4513 * from seeing the surface as still in use. */
4515 wined3d_surface_decref(prev
);
4517 device_invalidate_state(device
, STATE_FRAMEBUFFER
);
4522 void CDECL
wined3d_device_set_depth_stencil(struct wined3d_device
*device
, struct wined3d_surface
*depth_stencil
)
4524 struct wined3d_surface
*prev
= device
->fb
.depth_stencil
;
4526 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4527 device
, depth_stencil
, prev
);
4529 if (prev
== depth_stencil
)
4531 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4537 if (device
->swapchains
[0]->desc
.flags
& WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4538 || prev
->flags
& SFLAG_DISCARD
)
4540 surface_modify_ds_location(prev
, SFLAG_DISCARDED
,
4541 prev
->resource
.width
, prev
->resource
.height
);
4542 if (prev
== device
->onscreen_depth_stencil
)
4544 wined3d_surface_decref(device
->onscreen_depth_stencil
);
4545 device
->onscreen_depth_stencil
= NULL
;
4550 device
->fb
.depth_stencil
= depth_stencil
;
4552 wined3d_surface_incref(depth_stencil
);
4554 if (!prev
!= !depth_stencil
)
4556 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4557 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_ZENABLE
));
4558 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
4559 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK
));
4560 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_DEPTHBIAS
));
4562 else if (prev
&& prev
->resource
.format
->depth_size
!= depth_stencil
->resource
.format
->depth_size
)
4564 device_invalidate_state(device
, STATE_RENDER(WINED3D_RS_DEPTHBIAS
));
4567 wined3d_surface_decref(prev
);
4569 device_invalidate_state(device
, STATE_FRAMEBUFFER
);
4574 HRESULT CDECL
wined3d_device_set_cursor_properties(struct wined3d_device
*device
,
4575 UINT x_hotspot
, UINT y_hotspot
, struct wined3d_surface
*cursor_image
)
4577 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4578 device
, x_hotspot
, y_hotspot
, cursor_image
);
4580 /* some basic validation checks */
4581 if (device
->cursorTexture
)
4583 struct wined3d_context
*context
= context_acquire(device
, NULL
);
4584 context
->gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->cursorTexture
);
4585 context_release(context
);
4586 device
->cursorTexture
= 0;
4591 struct wined3d_display_mode mode
;
4592 struct wined3d_map_desc map_desc
;
4595 /* MSDN: Cursor must be A8R8G8B8 */
4596 if (cursor_image
->resource
.format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4598 WARN("surface %p has an invalid format.\n", cursor_image
);
4599 return WINED3DERR_INVALIDCALL
;
4602 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, &mode
, NULL
)))
4604 ERR("Failed to get display mode, hr %#x.\n", hr
);
4605 return WINED3DERR_INVALIDCALL
;
4608 /* MSDN: Cursor must be smaller than the display mode */
4609 if (cursor_image
->resource
.width
> mode
.width
|| cursor_image
->resource
.height
> mode
.height
)
4611 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4612 cursor_image
, cursor_image
->resource
.width
, cursor_image
->resource
.height
,
4613 mode
.width
, mode
.height
);
4614 return WINED3DERR_INVALIDCALL
;
4617 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4619 /* Do not store the surface's pointer because the application may
4620 * release it after setting the cursor image. Windows doesn't
4621 * addref the set surface, so we can't do this either without
4622 * creating circular refcount dependencies. Copy out the gl texture
4624 device
->cursorWidth
= cursor_image
->resource
.width
;
4625 device
->cursorHeight
= cursor_image
->resource
.height
;
4626 if (SUCCEEDED(wined3d_surface_map(cursor_image
, &map_desc
, NULL
, WINED3D_MAP_READONLY
)))
4628 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4629 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
4630 struct wined3d_context
*context
;
4631 char *mem
, *bits
= map_desc
.data
;
4632 GLint intfmt
= format
->glInternal
;
4633 GLint gl_format
= format
->glFormat
;
4634 GLint type
= format
->glType
;
4635 INT height
= device
->cursorHeight
;
4636 INT width
= device
->cursorWidth
;
4637 INT bpp
= format
->byte_count
;
4640 /* Reformat the texture memory (pitch and width can be
4642 mem
= HeapAlloc(GetProcessHeap(), 0, width
* height
* bpp
);
4643 for (i
= 0; i
< height
; ++i
)
4644 memcpy(&mem
[width
* bpp
* i
], &bits
[map_desc
.row_pitch
* i
], width
* bpp
);
4645 wined3d_surface_unmap(cursor_image
);
4647 context
= context_acquire(device
, NULL
);
4649 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
4651 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
4652 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4655 invalidate_active_texture(device
, context
);
4656 /* Create a new cursor texture */
4657 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->cursorTexture
);
4658 checkGLcall("glGenTextures");
4659 context_bind_texture(context
, GL_TEXTURE_2D
, device
->cursorTexture
);
4660 /* Copy the bitmap memory into the cursor texture */
4661 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_2D
, 0, intfmt
, width
, height
, 0, gl_format
, type
, mem
);
4662 checkGLcall("glTexImage2D");
4663 HeapFree(GetProcessHeap(), 0, mem
);
4665 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
4667 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
4668 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4671 context_release(context
);
4675 FIXME("A cursor texture was not returned.\n");
4676 device
->cursorTexture
= 0;
4679 if (cursor_image
->resource
.width
== 32 && cursor_image
->resource
.height
== 32)
4681 UINT mask_size
= cursor_image
->resource
.width
* cursor_image
->resource
.height
/ 8;
4682 ICONINFO cursorInfo
;
4686 /* 32-bit user32 cursors ignore the alpha channel if it's all
4687 * zeroes, and use the mask instead. Fill the mask with all ones
4688 * to ensure we still get a fully transparent cursor. */
4689 maskBits
= HeapAlloc(GetProcessHeap(), 0, mask_size
);
4690 memset(maskBits
, 0xff, mask_size
);
4691 wined3d_surface_map(cursor_image
, &map_desc
, NULL
,
4692 WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
);
4693 TRACE("width: %u height: %u.\n", cursor_image
->resource
.width
, cursor_image
->resource
.height
);
4695 cursorInfo
.fIcon
= FALSE
;
4696 cursorInfo
.xHotspot
= x_hotspot
;
4697 cursorInfo
.yHotspot
= y_hotspot
;
4698 cursorInfo
.hbmMask
= CreateBitmap(cursor_image
->resource
.width
, cursor_image
->resource
.height
,
4700 cursorInfo
.hbmColor
= CreateBitmap(cursor_image
->resource
.width
, cursor_image
->resource
.height
,
4701 1, 32, map_desc
.data
);
4702 wined3d_surface_unmap(cursor_image
);
4703 /* Create our cursor and clean up. */
4704 cursor
= CreateIconIndirect(&cursorInfo
);
4705 if (cursorInfo
.hbmMask
) DeleteObject(cursorInfo
.hbmMask
);
4706 if (cursorInfo
.hbmColor
) DeleteObject(cursorInfo
.hbmColor
);
4707 if (device
->hardwareCursor
) DestroyCursor(device
->hardwareCursor
);
4708 device
->hardwareCursor
= cursor
;
4709 if (device
->bCursorVisible
) SetCursor( cursor
);
4710 HeapFree(GetProcessHeap(), 0, maskBits
);
4714 device
->xHotSpot
= x_hotspot
;
4715 device
->yHotSpot
= y_hotspot
;
4719 void CDECL
wined3d_device_set_cursor_position(struct wined3d_device
*device
,
4720 int x_screen_space
, int y_screen_space
, DWORD flags
)
4722 TRACE("device %p, x %d, y %d, flags %#x.\n",
4723 device
, x_screen_space
, y_screen_space
, flags
);
4725 device
->xScreenSpace
= x_screen_space
;
4726 device
->yScreenSpace
= y_screen_space
;
4728 if (device
->hardwareCursor
)
4732 GetCursorPos( &pt
);
4733 if (x_screen_space
== pt
.x
&& y_screen_space
== pt
.y
)
4735 SetCursorPos( x_screen_space
, y_screen_space
);
4737 /* Switch to the software cursor if position diverges from the hardware one. */
4738 GetCursorPos( &pt
);
4739 if (x_screen_space
!= pt
.x
|| y_screen_space
!= pt
.y
)
4741 if (device
->bCursorVisible
) SetCursor( NULL
);
4742 DestroyCursor( device
->hardwareCursor
);
4743 device
->hardwareCursor
= 0;
4748 BOOL CDECL
wined3d_device_show_cursor(struct wined3d_device
*device
, BOOL show
)
4750 BOOL oldVisible
= device
->bCursorVisible
;
4752 TRACE("device %p, show %#x.\n", device
, show
);
4755 * When ShowCursor is first called it should make the cursor appear at the OS's last
4756 * known cursor position.
4758 if (show
&& !oldVisible
)
4762 device
->xScreenSpace
= pt
.x
;
4763 device
->yScreenSpace
= pt
.y
;
4766 if (device
->hardwareCursor
)
4768 device
->bCursorVisible
= show
;
4770 SetCursor(device
->hardwareCursor
);
4776 if (device
->cursorTexture
)
4777 device
->bCursorVisible
= show
;
4783 void CDECL
wined3d_device_evict_managed_resources(struct wined3d_device
*device
)
4785 struct wined3d_resource
*resource
, *cursor
;
4787 TRACE("device %p.\n", device
);
4789 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4791 TRACE("Checking resource %p for eviction.\n", resource
);
4793 if (resource
->pool
== WINED3D_POOL_MANAGED
&& !resource
->map_count
)
4795 TRACE("Evicting %p.\n", resource
);
4796 resource
->resource_ops
->resource_unload(resource
);
4800 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4801 device_invalidate_state(device
, STATE_STREAMSRC
);
4804 /* Do not call while under the GL lock. */
4805 static void delete_opengl_contexts(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
4807 struct wined3d_resource
*resource
, *cursor
;
4808 const struct wined3d_gl_info
*gl_info
;
4809 struct wined3d_context
*context
;
4810 struct wined3d_shader
*shader
;
4812 context
= context_acquire(device
, NULL
);
4813 gl_info
= context
->gl_info
;
4815 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4817 TRACE("Unloading resource %p.\n", resource
);
4819 resource
->resource_ops
->resource_unload(resource
);
4822 LIST_FOR_EACH_ENTRY(shader
, &device
->shaders
, struct wined3d_shader
, shader_list_entry
)
4824 device
->shader_backend
->shader_destroy(shader
);
4827 if (device
->depth_blt_texture
)
4829 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->depth_blt_texture
);
4830 device
->depth_blt_texture
= 0;
4832 if (device
->cursorTexture
)
4834 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->cursorTexture
);
4835 device
->cursorTexture
= 0;
4838 device
->blitter
->free_private(device
);
4839 device
->shader_backend
->shader_free_private(device
);
4840 destroy_dummy_textures(device
, gl_info
);
4842 context_release(context
);
4844 while (device
->context_count
)
4846 swapchain_destroy_contexts(device
->contexts
[0]->swapchain
);
4849 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
4850 swapchain
->context
= NULL
;
4853 /* Do not call while under the GL lock. */
4854 static HRESULT
create_primary_opengl_context(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
4856 struct wined3d_context
*context
;
4857 struct wined3d_surface
*target
;
4860 if (FAILED(hr
= device
->shader_backend
->shader_alloc_private(device
,
4861 device
->adapter
->vertex_pipe
, device
->adapter
->fragment_pipe
)))
4863 ERR("Failed to allocate shader private data, hr %#x.\n", hr
);
4867 if (FAILED(hr
= device
->blitter
->alloc_private(device
)))
4869 ERR("Failed to allocate blitter private data, hr %#x.\n", hr
);
4870 device
->shader_backend
->shader_free_private(device
);
4874 /* Recreate the primary swapchain's context */
4875 swapchain
->context
= HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain
->context
));
4876 if (!swapchain
->context
)
4878 ERR("Failed to allocate memory for swapchain context array.\n");
4879 device
->blitter
->free_private(device
);
4880 device
->shader_backend
->shader_free_private(device
);
4881 return E_OUTOFMEMORY
;
4884 target
= swapchain
->back_buffers
? swapchain
->back_buffers
[0] : swapchain
->front_buffer
;
4885 if (!(context
= context_create(swapchain
, target
, swapchain
->ds_format
)))
4887 WARN("Failed to create context.\n");
4888 device
->blitter
->free_private(device
);
4889 device
->shader_backend
->shader_free_private(device
);
4890 HeapFree(GetProcessHeap(), 0, swapchain
->context
);
4894 swapchain
->context
[0] = context
;
4895 swapchain
->num_contexts
= 1;
4896 create_dummy_textures(device
, context
);
4897 context_release(context
);
4902 /* Do not call while under the GL lock. */
4903 HRESULT CDECL
wined3d_device_reset(struct wined3d_device
*device
,
4904 const struct wined3d_swapchain_desc
*swapchain_desc
, const struct wined3d_display_mode
*mode
,
4905 wined3d_device_reset_cb callback
, BOOL reset_state
)
4907 struct wined3d_resource
*resource
, *cursor
;
4908 struct wined3d_swapchain
*swapchain
;
4909 struct wined3d_display_mode m
;
4910 BOOL DisplayModeChanged
= FALSE
;
4911 BOOL update_desc
= FALSE
;
4912 UINT backbuffer_width
= swapchain_desc
->backbuffer_width
;
4913 UINT backbuffer_height
= swapchain_desc
->backbuffer_height
;
4914 HRESULT hr
= WINED3D_OK
;
4917 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device
, swapchain_desc
, mode
, callback
);
4919 if (!(swapchain
= wined3d_device_get_swapchain(device
, 0)))
4921 ERR("Failed to get the first implicit swapchain.\n");
4922 return WINED3DERR_INVALIDCALL
;
4926 stateblock_unbind_resources(device
->stateBlock
);
4928 if (device
->fb
.render_targets
)
4930 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0])
4931 wined3d_device_set_render_target(device
, 0, swapchain
->back_buffers
[0], FALSE
);
4933 wined3d_device_set_render_target(device
, 0, swapchain
->front_buffer
, FALSE
);
4934 for (i
= 1; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
4936 wined3d_device_set_render_target(device
, i
, NULL
, FALSE
);
4939 wined3d_device_set_depth_stencil(device
, NULL
);
4941 if (device
->onscreen_depth_stencil
)
4943 wined3d_surface_decref(device
->onscreen_depth_stencil
);
4944 device
->onscreen_depth_stencil
= NULL
;
4949 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4951 TRACE("Enumerating resource %p.\n", resource
);
4952 if (FAILED(hr
= callback(resource
)))
4957 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4958 * on an existing gl context, so there's no real need for recreation.
4960 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4962 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4964 TRACE("New params:\n");
4965 TRACE("backbuffer_width %u\n", swapchain_desc
->backbuffer_width
);
4966 TRACE("backbuffer_height %u\n", swapchain_desc
->backbuffer_height
);
4967 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc
->backbuffer_format
));
4968 TRACE("backbuffer_count %u\n", swapchain_desc
->backbuffer_count
);
4969 TRACE("multisample_type %#x\n", swapchain_desc
->multisample_type
);
4970 TRACE("multisample_quality %u\n", swapchain_desc
->multisample_quality
);
4971 TRACE("swap_effect %#x\n", swapchain_desc
->swap_effect
);
4972 TRACE("device_window %p\n", swapchain_desc
->device_window
);
4973 TRACE("windowed %#x\n", swapchain_desc
->windowed
);
4974 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc
->enable_auto_depth_stencil
);
4975 if (swapchain_desc
->enable_auto_depth_stencil
)
4976 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc
->auto_depth_stencil_format
));
4977 TRACE("flags %#x\n", swapchain_desc
->flags
);
4978 TRACE("refresh_rate %u\n", swapchain_desc
->refresh_rate
);
4979 TRACE("swap_interval %u\n", swapchain_desc
->swap_interval
);
4980 TRACE("auto_restore_display_mode %#x\n", swapchain_desc
->auto_restore_display_mode
);
4982 /* No special treatment of these parameters. Just store them */
4983 swapchain
->desc
.swap_effect
= swapchain_desc
->swap_effect
;
4984 swapchain
->desc
.flags
= swapchain_desc
->flags
;
4985 swapchain
->desc
.swap_interval
= swapchain_desc
->swap_interval
;
4986 swapchain
->desc
.refresh_rate
= swapchain_desc
->refresh_rate
;
4988 /* What to do about these? */
4989 if (swapchain_desc
->backbuffer_count
4990 && swapchain_desc
->backbuffer_count
!= swapchain
->desc
.backbuffer_count
)
4991 FIXME("Cannot change the back buffer count yet.\n");
4993 if (swapchain_desc
->device_window
4994 && swapchain_desc
->device_window
!= swapchain
->desc
.device_window
)
4996 TRACE("Changing the device window from %p to %p.\n",
4997 swapchain
->desc
.device_window
, swapchain_desc
->device_window
);
4998 swapchain
->desc
.device_window
= swapchain_desc
->device_window
;
4999 swapchain
->device_window
= swapchain_desc
->device_window
;
5000 wined3d_swapchain_set_window(swapchain
, NULL
);
5003 if (swapchain_desc
->enable_auto_depth_stencil
&& !device
->auto_depth_stencil
)
5005 struct wined3d_resource_desc surface_desc
;
5007 TRACE("Creating the depth stencil buffer\n");
5009 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
5010 surface_desc
.format
= swapchain_desc
->auto_depth_stencil_format
;
5011 surface_desc
.multisample_type
= swapchain_desc
->multisample_type
;
5012 surface_desc
.multisample_quality
= swapchain_desc
->multisample_quality
;
5013 surface_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
5014 surface_desc
.pool
= WINED3D_POOL_DEFAULT
;
5015 surface_desc
.width
= swapchain_desc
->backbuffer_width
;
5016 surface_desc
.height
= swapchain_desc
->backbuffer_height
;
5017 surface_desc
.depth
= 1;
5018 surface_desc
.size
= 0;
5020 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_surface(device
->device_parent
,
5021 device
->device_parent
, &surface_desc
, &device
->auto_depth_stencil
)))
5023 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr
);
5024 return WINED3DERR_INVALIDCALL
;
5028 /* Reset the depth stencil */
5029 if (swapchain_desc
->enable_auto_depth_stencil
)
5030 wined3d_device_set_depth_stencil(device
, device
->auto_depth_stencil
);
5034 DisplayModeChanged
= TRUE
;
5037 else if (swapchain_desc
->windowed
)
5039 m
.width
= swapchain
->orig_width
;
5040 m
.height
= swapchain
->orig_height
;
5042 m
.format_id
= swapchain
->desc
.backbuffer_format
;
5043 m
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
5047 m
.width
= swapchain_desc
->backbuffer_width
;
5048 m
.height
= swapchain_desc
->backbuffer_height
;
5049 m
.refresh_rate
= swapchain_desc
->refresh_rate
;
5050 m
.format_id
= swapchain_desc
->backbuffer_format
;
5051 m
.scanline_ordering
= WINED3D_SCANLINE_ORDERING_UNKNOWN
;
5054 if (!backbuffer_width
|| !backbuffer_height
)
5056 /* The application is requesting that either the swapchain width or
5057 * height be set to the corresponding dimension in the window's
5062 if (!swapchain_desc
->windowed
)
5063 return WINED3DERR_INVALIDCALL
;
5065 if (!GetClientRect(swapchain
->device_window
, &client_rect
))
5067 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
5068 return WINED3DERR_INVALIDCALL
;
5071 if (!backbuffer_width
)
5072 backbuffer_width
= client_rect
.right
;
5074 if (!backbuffer_height
)
5075 backbuffer_height
= client_rect
.bottom
;
5078 if (backbuffer_width
!= swapchain
->desc
.backbuffer_width
5079 || backbuffer_height
!= swapchain
->desc
.backbuffer_height
)
5081 if (!swapchain_desc
->windowed
)
5082 DisplayModeChanged
= TRUE
;
5084 swapchain
->desc
.backbuffer_width
= backbuffer_width
;
5085 swapchain
->desc
.backbuffer_height
= backbuffer_height
;
5089 if (swapchain_desc
->backbuffer_format
!= WINED3DFMT_UNKNOWN
5090 && swapchain_desc
->backbuffer_format
!= swapchain
->desc
.backbuffer_format
)
5092 swapchain
->desc
.backbuffer_format
= swapchain_desc
->backbuffer_format
;
5096 if (swapchain_desc
->multisample_type
!= swapchain
->desc
.multisample_type
5097 || swapchain_desc
->multisample_quality
!= swapchain
->desc
.multisample_quality
)
5099 swapchain
->desc
.multisample_type
= swapchain_desc
->multisample_type
;
5100 swapchain
->desc
.multisample_quality
= swapchain_desc
->multisample_quality
;
5108 if (FAILED(hr
= wined3d_surface_update_desc(swapchain
->front_buffer
, swapchain
->desc
.backbuffer_width
,
5109 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
5110 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
)))
5113 for (i
= 0; i
< swapchain
->desc
.backbuffer_count
; ++i
)
5115 if (FAILED(hr
= wined3d_surface_update_desc(swapchain
->back_buffers
[i
], swapchain
->desc
.backbuffer_width
,
5116 swapchain
->desc
.backbuffer_height
, swapchain
->desc
.backbuffer_format
,
5117 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
)))
5120 if (device
->auto_depth_stencil
)
5122 if (FAILED(hr
= wined3d_surface_update_desc(device
->auto_depth_stencil
, swapchain
->desc
.backbuffer_width
,
5123 swapchain
->desc
.backbuffer_height
, device
->auto_depth_stencil
->resource
.format
->id
,
5124 swapchain
->desc
.multisample_type
, swapchain
->desc
.multisample_quality
)))
5129 if (!swapchain_desc
->windowed
!= !swapchain
->desc
.windowed
5130 || DisplayModeChanged
)
5132 if (FAILED(hr
= wined3d_set_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, &m
)))
5134 WARN("Failed to set display mode, hr %#x.\n", hr
);
5135 return WINED3DERR_INVALIDCALL
;
5138 if (!swapchain_desc
->windowed
)
5140 if (swapchain
->desc
.windowed
)
5142 HWND focus_window
= device
->create_parms
.focus_window
;
5144 focus_window
= swapchain_desc
->device_window
;
5145 if (FAILED(hr
= wined3d_device_acquire_focus_window(device
, focus_window
)))
5147 ERR("Failed to acquire focus window, hr %#x.\n", hr
);
5151 /* switch from windowed to fs */
5152 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
,
5153 swapchain_desc
->backbuffer_width
,
5154 swapchain_desc
->backbuffer_height
);
5158 /* Fullscreen -> fullscreen mode change */
5159 MoveWindow(swapchain
->device_window
, 0, 0,
5160 swapchain_desc
->backbuffer_width
,
5161 swapchain_desc
->backbuffer_height
,
5165 else if (!swapchain
->desc
.windowed
)
5167 /* Fullscreen -> windowed switch */
5168 wined3d_device_restore_fullscreen_window(device
, swapchain
->device_window
);
5169 wined3d_device_release_focus_window(device
);
5171 swapchain
->desc
.windowed
= swapchain_desc
->windowed
;
5173 else if (!swapchain_desc
->windowed
)
5175 DWORD style
= device
->style
;
5176 DWORD exStyle
= device
->exStyle
;
5177 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5178 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5179 * Reset to clear up their mess. Guild Wars also loses the device during that.
5182 device
->exStyle
= 0;
5183 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
,
5184 swapchain_desc
->backbuffer_width
,
5185 swapchain_desc
->backbuffer_height
);
5186 device
->style
= style
;
5187 device
->exStyle
= exStyle
;
5192 TRACE("Resetting stateblock.\n");
5193 wined3d_stateblock_decref(device
->updateStateBlock
);
5194 wined3d_stateblock_decref(device
->stateBlock
);
5196 if (device
->d3d_initialized
)
5197 delete_opengl_contexts(device
, swapchain
);
5199 /* Note: No parent needed for initial internal stateblock */
5200 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_INIT
, &device
->stateBlock
);
5202 ERR("Resetting the stateblock failed with error %#x.\n", hr
);
5204 TRACE("Created stateblock %p.\n", device
->stateBlock
);
5205 device
->updateStateBlock
= device
->stateBlock
;
5206 wined3d_stateblock_incref(device
->updateStateBlock
);
5208 stateblock_init_default_state(device
->stateBlock
);
5212 struct wined3d_surface
*rt
= device
->fb
.render_targets
[0];
5213 struct wined3d_state
*state
= &device
->stateBlock
->state
;
5215 /* Note the min_z / max_z is not reset. */
5216 state
->viewport
.x
= 0;
5217 state
->viewport
.y
= 0;
5218 state
->viewport
.width
= rt
->resource
.width
;
5219 state
->viewport
.height
= rt
->resource
.height
;
5220 device_invalidate_state(device
, STATE_VIEWPORT
);
5222 state
->scissor_rect
.top
= 0;
5223 state
->scissor_rect
.left
= 0;
5224 state
->scissor_rect
.right
= rt
->resource
.width
;
5225 state
->scissor_rect
.bottom
= rt
->resource
.height
;
5226 device_invalidate_state(device
, STATE_SCISSORRECT
);
5229 swapchain_update_render_to_fbo(swapchain
);
5230 swapchain_update_draw_bindings(swapchain
);
5232 if (reset_state
&& device
->d3d_initialized
)
5233 hr
= create_primary_opengl_context(device
, swapchain
);
5235 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5241 HRESULT CDECL
wined3d_device_set_dialog_box_mode(struct wined3d_device
*device
, BOOL enable_dialogs
)
5243 TRACE("device %p, enable_dialogs %#x.\n", device
, enable_dialogs
);
5245 if (!enable_dialogs
) FIXME("Dialogs cannot be disabled yet.\n");
5251 void CDECL
wined3d_device_get_creation_parameters(const struct wined3d_device
*device
,
5252 struct wined3d_device_creation_parameters
*parameters
)
5254 TRACE("device %p, parameters %p.\n", device
, parameters
);
5256 *parameters
= device
->create_parms
;
5259 void CDECL
wined3d_device_set_gamma_ramp(const struct wined3d_device
*device
,
5260 UINT swapchain_idx
, DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
5262 struct wined3d_swapchain
*swapchain
;
5264 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5265 device
, swapchain_idx
, flags
, ramp
);
5267 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
5268 wined3d_swapchain_set_gamma_ramp(swapchain
, flags
, ramp
);
5271 void CDECL
wined3d_device_get_gamma_ramp(const struct wined3d_device
*device
,
5272 UINT swapchain_idx
, struct wined3d_gamma_ramp
*ramp
)
5274 struct wined3d_swapchain
*swapchain
;
5276 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5277 device
, swapchain_idx
, ramp
);
5279 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
5280 wined3d_swapchain_get_gamma_ramp(swapchain
, ramp
);
5283 void device_resource_add(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5285 TRACE("device %p, resource %p.\n", device
, resource
);
5287 list_add_head(&device
->resources
, &resource
->resource_list_entry
);
5290 static void device_resource_remove(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5292 TRACE("device %p, resource %p.\n", device
, resource
);
5294 list_remove(&resource
->resource_list_entry
);
5297 void device_resource_released(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5299 enum wined3d_resource_type type
= resource
->type
;
5302 TRACE("device %p, resource %p, type %s.\n", device
, resource
, debug_d3dresourcetype(type
));
5304 context_resource_released(device
, resource
, type
);
5308 case WINED3D_RTYPE_SURFACE
:
5310 struct wined3d_surface
*surface
= surface_from_resource(resource
);
5312 if (!device
->d3d_initialized
) break;
5314 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
5316 if (device
->fb
.render_targets
[i
] == surface
)
5318 ERR("Surface %p is still in use as render target %u.\n", surface
, i
);
5319 device
->fb
.render_targets
[i
] = NULL
;
5323 if (device
->fb
.depth_stencil
== surface
)
5325 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface
);
5326 device
->fb
.depth_stencil
= NULL
;
5331 case WINED3D_RTYPE_TEXTURE
:
5332 case WINED3D_RTYPE_CUBE_TEXTURE
:
5333 case WINED3D_RTYPE_VOLUME_TEXTURE
:
5334 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
5336 struct wined3d_texture
*texture
= wined3d_texture_from_resource(resource
);
5338 if (device
->stateBlock
&& device
->stateBlock
->state
.textures
[i
] == texture
)
5340 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5341 texture
, device
->stateBlock
, i
);
5342 device
->stateBlock
->state
.textures
[i
] = NULL
;
5345 if (device
->updateStateBlock
!= device
->stateBlock
5346 && device
->updateStateBlock
->state
.textures
[i
] == texture
)
5348 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5349 texture
, device
->updateStateBlock
, i
);
5350 device
->updateStateBlock
->state
.textures
[i
] = NULL
;
5355 case WINED3D_RTYPE_BUFFER
:
5357 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
5359 for (i
= 0; i
< MAX_STREAMS
; ++i
)
5361 if (device
->stateBlock
&& device
->stateBlock
->state
.streams
[i
].buffer
== buffer
)
5363 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5364 buffer
, device
->stateBlock
, i
);
5365 device
->stateBlock
->state
.streams
[i
].buffer
= NULL
;
5368 if (device
->updateStateBlock
!= device
->stateBlock
5369 && device
->updateStateBlock
->state
.streams
[i
].buffer
== buffer
)
5371 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5372 buffer
, device
->updateStateBlock
, i
);
5373 device
->updateStateBlock
->state
.streams
[i
].buffer
= NULL
;
5378 if (device
->stateBlock
&& device
->stateBlock
->state
.index_buffer
== buffer
)
5380 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5381 buffer
, device
->stateBlock
);
5382 device
->stateBlock
->state
.index_buffer
= NULL
;
5385 if (device
->updateStateBlock
!= device
->stateBlock
5386 && device
->updateStateBlock
->state
.index_buffer
== buffer
)
5388 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5389 buffer
, device
->updateStateBlock
);
5390 device
->updateStateBlock
->state
.index_buffer
= NULL
;
5399 /* Remove the resource from the resourceStore */
5400 device_resource_remove(device
, resource
);
5402 TRACE("Resource released.\n");
5405 struct wined3d_surface
* CDECL
wined3d_device_get_surface_from_dc(const struct wined3d_device
*device
, HDC dc
)
5407 struct wined3d_resource
*resource
;
5409 TRACE("device %p, dc %p.\n", device
, dc
);
5414 LIST_FOR_EACH_ENTRY(resource
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
5416 if (resource
->type
== WINED3D_RTYPE_SURFACE
)
5418 struct wined3d_surface
*s
= surface_from_resource(resource
);
5422 TRACE("Found surface %p for dc %p.\n", s
, dc
);
5431 HRESULT
device_init(struct wined3d_device
*device
, struct wined3d
*wined3d
,
5432 UINT adapter_idx
, enum wined3d_device_type device_type
, HWND focus_window
, DWORD flags
,
5433 BYTE surface_alignment
, struct wined3d_device_parent
*device_parent
)
5435 struct wined3d_adapter
*adapter
= &wined3d
->adapters
[adapter_idx
];
5436 const struct fragment_pipeline
*fragment_pipeline
;
5437 const struct wined3d_vertex_pipe_ops
*vertex_pipeline
;
5442 device
->wined3d
= wined3d
;
5443 wined3d_incref(device
->wined3d
);
5444 device
->adapter
= wined3d
->adapter_count
? adapter
: NULL
;
5445 device
->device_parent
= device_parent
;
5446 list_init(&device
->resources
);
5447 list_init(&device
->shaders
);
5448 device
->surface_alignment
= surface_alignment
;
5450 /* Save the creation parameters. */
5451 device
->create_parms
.adapter_idx
= adapter_idx
;
5452 device
->create_parms
.device_type
= device_type
;
5453 device
->create_parms
.focus_window
= focus_window
;
5454 device
->create_parms
.flags
= flags
;
5456 device
->shader_backend
= adapter
->shader_backend
;
5458 vertex_pipeline
= adapter
->vertex_pipe
;
5460 fragment_pipeline
= adapter
->fragment_pipe
;
5462 if (vertex_pipeline
->vp_states
&& fragment_pipeline
->states
5463 && FAILED(hr
= compile_state_table(device
->StateTable
, device
->multistate_funcs
,
5464 &adapter
->gl_info
, &adapter
->d3d_info
, vertex_pipeline
,
5465 fragment_pipeline
, misc_state_template
)))
5467 ERR("Failed to compile state table, hr %#x.\n", hr
);
5468 wined3d_decref(device
->wined3d
);
5472 device
->blitter
= adapter
->blitter
;
5474 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_INIT
, &device
->stateBlock
);
5477 WARN("Failed to create stateblock.\n");
5478 for (i
= 0; i
< sizeof(device
->multistate_funcs
) / sizeof(device
->multistate_funcs
[0]); ++i
)
5480 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
5482 wined3d_decref(device
->wined3d
);
5486 TRACE("Created stateblock %p.\n", device
->stateBlock
);
5487 device
->updateStateBlock
= device
->stateBlock
;
5488 wined3d_stateblock_incref(device
->updateStateBlock
);
5494 void device_invalidate_state(const struct wined3d_device
*device
, DWORD state
)
5496 DWORD rep
= device
->StateTable
[state
].representative
;
5497 struct wined3d_context
*context
;
5502 for (i
= 0; i
< device
->context_count
; ++i
)
5504 context
= device
->contexts
[i
];
5505 if(isStateDirty(context
, rep
)) continue;
5507 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
5508 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
5509 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
5510 context
->isStateDirty
[idx
] |= (1 << shift
);
5514 void get_drawable_size_fbo(const struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
5516 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5517 *width
= context
->current_rt
->pow2Width
;
5518 *height
= context
->current_rt
->pow2Height
;
5521 void get_drawable_size_backbuffer(const struct wined3d_context
*context
, UINT
*width
, UINT
*height
)
5523 const struct wined3d_swapchain
*swapchain
= context
->swapchain
;
5524 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5525 * current context's drawable, which is the size of the back buffer of the swapchain
5526 * the active context belongs to. */
5527 *width
= swapchain
->desc
.backbuffer_width
;
5528 *height
= swapchain
->desc
.backbuffer_height
;
5531 LRESULT
device_process_message(struct wined3d_device
*device
, HWND window
, BOOL unicode
,
5532 UINT message
, WPARAM wparam
, LPARAM lparam
, WNDPROC proc
)
5534 if (device
->filter_messages
)
5536 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5537 window
, message
, wparam
, lparam
);
5539 return DefWindowProcW(window
, message
, wparam
, lparam
);
5541 return DefWindowProcA(window
, message
, wparam
, lparam
);
5544 if (message
== WM_DESTROY
)
5546 TRACE("unregister window %p.\n", window
);
5547 wined3d_unregister_window(window
);
5549 if (InterlockedCompareExchangePointer((void **)&device
->focus_window
, NULL
, window
) != window
)
5550 ERR("Window %p is not the focus window for device %p.\n", window
, device
);
5552 else if (message
== WM_DISPLAYCHANGE
)
5554 device
->device_parent
->ops
->mode_changed(device
->device_parent
);
5558 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
5560 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);