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(winediag
);
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
));
94 case WINED3D_PT_UNDEFINED
:
99 static enum wined3d_primitive_type
d3d_primitive_type_from_gl(GLenum primitive_type
)
101 switch (primitive_type
)
104 return WINED3D_PT_POINTLIST
;
107 return WINED3D_PT_LINELIST
;
110 return WINED3D_PT_LINESTRIP
;
113 return WINED3D_PT_TRIANGLELIST
;
115 case GL_TRIANGLE_STRIP
:
116 return WINED3D_PT_TRIANGLESTRIP
;
118 case GL_TRIANGLE_FAN
:
119 return WINED3D_PT_TRIANGLEFAN
;
121 case GL_LINES_ADJACENCY_ARB
:
122 return WINED3D_PT_LINELIST_ADJ
;
124 case GL_LINE_STRIP_ADJACENCY_ARB
:
125 return WINED3D_PT_LINESTRIP_ADJ
;
127 case GL_TRIANGLES_ADJACENCY_ARB
:
128 return WINED3D_PT_TRIANGLELIST_ADJ
;
130 case GL_TRIANGLE_STRIP_ADJACENCY_ARB
:
131 return WINED3D_PT_TRIANGLESTRIP_ADJ
;
134 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
136 return WINED3D_PT_UNDEFINED
;
140 BOOL
device_context_add(struct wined3d_device
*device
, struct wined3d_context
*context
)
142 struct wined3d_context
**new_array
;
144 TRACE("Adding context %p.\n", context
);
146 if (!device
->contexts
) new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
147 else new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
,
148 sizeof(*new_array
) * (device
->context_count
+ 1));
152 ERR("Failed to grow the context array.\n");
156 new_array
[device
->context_count
++] = context
;
157 device
->contexts
= new_array
;
161 void device_context_remove(struct wined3d_device
*device
, struct wined3d_context
*context
)
163 struct wined3d_context
**new_array
;
167 TRACE("Removing context %p.\n", context
);
169 for (i
= 0; i
< device
->context_count
; ++i
)
171 if (device
->contexts
[i
] == context
)
180 ERR("Context %p doesn't exist in context array.\n", context
);
184 if (!--device
->context_count
)
186 HeapFree(GetProcessHeap(), 0, device
->contexts
);
187 device
->contexts
= NULL
;
191 memmove(&device
->contexts
[i
], &device
->contexts
[i
+ 1], (device
->context_count
- i
) * sizeof(*device
->contexts
));
192 new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
, device
->context_count
* sizeof(*device
->contexts
));
195 ERR("Failed to shrink context array. Oh well.\n");
199 device
->contexts
= new_array
;
202 static BOOL
is_full_clear(const struct wined3d_surface
*target
, const RECT
*draw_rect
, const RECT
*clear_rect
)
204 unsigned int height
= wined3d_texture_get_level_height(target
->container
, target
->texture_level
);
205 unsigned int width
= wined3d_texture_get_level_width(target
->container
, target
->texture_level
);
207 /* partial draw rect */
208 if (draw_rect
->left
|| draw_rect
->top
|| draw_rect
->right
< width
|| draw_rect
->bottom
< height
)
211 /* partial clear rect */
212 if (clear_rect
&& (clear_rect
->left
> 0 || clear_rect
->top
> 0
213 || clear_rect
->right
< width
|| clear_rect
->bottom
< height
))
219 void device_clear_render_targets(struct wined3d_device
*device
, UINT rt_count
, const struct wined3d_fb_state
*fb
,
220 UINT rect_count
, const RECT
*clear_rect
, const RECT
*draw_rect
, DWORD flags
, const struct wined3d_color
*color
,
221 float depth
, DWORD stencil
)
223 struct wined3d_rendertarget_view
*rtv
= rt_count
? fb
->render_targets
[0] : NULL
;
224 struct wined3d_surface
*target
= rtv
? wined3d_rendertarget_view_get_surface(rtv
) : NULL
;
225 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
226 struct wined3d_surface
*depth_stencil
= dsv
? wined3d_rendertarget_view_get_surface(dsv
) : NULL
;
227 const struct wined3d_state
*state
= &device
->cs
->state
;
228 const struct wined3d_gl_info
*gl_info
;
229 UINT drawable_width
, drawable_height
;
230 struct wined3d_color corrected_color
;
231 struct wined3d_context
*context
;
232 GLbitfield clear_mask
= 0;
233 BOOL render_offscreen
;
237 context
= context_acquire(device
, target
->container
, rtv
->sub_resource_idx
);
239 context
= context_acquire(device
, NULL
, 0);
242 context_release(context
);
243 WARN("Invalid context, skipping clear.\n");
246 gl_info
= context
->gl_info
;
248 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
249 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
250 * for the cleared parts, and the untouched parts.
252 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
253 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
254 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
255 * checking all this if the dest surface is in the drawable anyway. */
256 for (i
= 0; i
< rt_count
; ++i
)
258 struct wined3d_rendertarget_view
*rtv
= fb
->render_targets
[i
];
260 if (rtv
&& rtv
->format
->id
!= WINED3DFMT_NULL
)
262 struct wined3d_texture
*rt
= wined3d_texture_from_resource(rtv
->resource
);
264 if (flags
& WINED3DCLEAR_TARGET
&& !is_full_clear(target
, draw_rect
, rect_count
? clear_rect
: NULL
))
265 wined3d_texture_load_location(rt
, rtv
->sub_resource_idx
, context
, rtv
->resource
->draw_binding
);
267 wined3d_texture_prepare_location(rt
, rtv
->sub_resource_idx
, context
, rtv
->resource
->draw_binding
);
273 render_offscreen
= context
->render_offscreen
;
274 wined3d_rendertarget_view_get_drawable_size(rtv
, context
, &drawable_width
, &drawable_height
);
278 render_offscreen
= TRUE
;
279 drawable_width
= wined3d_texture_get_level_pow2_width(depth_stencil
->container
,
280 depth_stencil
->texture_level
);
281 drawable_height
= wined3d_texture_get_level_pow2_height(depth_stencil
->container
,
282 depth_stencil
->texture_level
);
285 if (depth_stencil
&& render_offscreen
)
286 wined3d_texture_prepare_location(depth_stencil
->container
,
287 dsv
->sub_resource_idx
, context
, dsv
->resource
->draw_binding
);
289 if (flags
& WINED3DCLEAR_ZBUFFER
)
291 DWORD location
= render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
293 wined3d_texture_load_location(depth_stencil
->container
,
294 dsv
->sub_resource_idx
, context
, location
);
297 if (!context_apply_clear_state(context
, state
, rt_count
, fb
))
299 context_release(context
);
300 WARN("Failed to apply clear state, skipping clear.\n");
304 /* Only set the values up once, as they are not changing. */
305 if (flags
& WINED3DCLEAR_STENCIL
)
307 if (gl_info
->supported
[EXT_STENCIL_TWO_SIDE
])
309 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT
);
310 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE
));
312 gl_info
->gl_ops
.gl
.p_glStencilMask(~0U);
313 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK
));
314 gl_info
->gl_ops
.gl
.p_glClearStencil(stencil
);
315 checkGLcall("glClearStencil");
316 clear_mask
= clear_mask
| GL_STENCIL_BUFFER_BIT
;
319 if (flags
& WINED3DCLEAR_ZBUFFER
)
321 DWORD location
= render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
323 wined3d_texture_validate_location(depth_stencil
->container
, dsv
->sub_resource_idx
, location
);
324 wined3d_texture_invalidate_location(depth_stencil
->container
, dsv
->sub_resource_idx
, ~location
);
326 gl_info
->gl_ops
.gl
.p_glDepthMask(GL_TRUE
);
327 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZWRITEENABLE
));
328 gl_info
->gl_ops
.gl
.p_glClearDepth(depth
);
329 checkGLcall("glClearDepth");
330 clear_mask
= clear_mask
| GL_DEPTH_BUFFER_BIT
;
333 if (flags
& WINED3DCLEAR_TARGET
)
335 for (i
= 0; i
< rt_count
; ++i
)
337 struct wined3d_rendertarget_view
*rtv
= fb
->render_targets
[i
];
338 struct wined3d_texture
*texture
;
343 if (rtv
->resource
->type
== WINED3D_RTYPE_BUFFER
)
345 FIXME("Not supported on buffer resources.\n");
349 texture
= texture_from_resource(rtv
->resource
);
350 wined3d_texture_validate_location(texture
, rtv
->sub_resource_idx
, rtv
->resource
->draw_binding
);
351 wined3d_texture_invalidate_location(texture
, rtv
->sub_resource_idx
, ~rtv
->resource
->draw_binding
);
354 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && needs_srgb_write(context
, state
, fb
))
357 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
358 "support, this might cause graphical issues.\n");
360 corrected_color
.r
= color
->r
< wined3d_srgb_const1
[0]
361 ? color
->r
* wined3d_srgb_const0
[3]
362 : pow(color
->r
, wined3d_srgb_const0
[0]) * wined3d_srgb_const0
[1]
363 - wined3d_srgb_const0
[2];
364 corrected_color
.r
= min(max(corrected_color
.r
, 0.0f
), 1.0f
);
365 corrected_color
.g
= color
->g
< wined3d_srgb_const1
[0]
366 ? color
->g
* wined3d_srgb_const0
[3]
367 : pow(color
->g
, wined3d_srgb_const0
[0]) * wined3d_srgb_const0
[1]
368 - wined3d_srgb_const0
[2];
369 corrected_color
.g
= min(max(corrected_color
.g
, 0.0f
), 1.0f
);
370 corrected_color
.b
= color
->b
< wined3d_srgb_const1
[0]
371 ? color
->b
* wined3d_srgb_const0
[3]
372 : pow(color
->b
, wined3d_srgb_const0
[0]) * wined3d_srgb_const0
[1]
373 - wined3d_srgb_const0
[2];
374 corrected_color
.b
= min(max(corrected_color
.b
, 0.0f
), 1.0f
);
375 color
= &corrected_color
;
378 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
379 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
380 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
381 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
382 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
383 gl_info
->gl_ops
.gl
.p_glClearColor(color
->r
, color
->g
, color
->b
, color
->a
);
384 checkGLcall("glClearColor");
385 clear_mask
= clear_mask
| GL_COLOR_BUFFER_BIT
;
390 if (render_offscreen
)
392 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, draw_rect
->top
,
393 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
397 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, drawable_height
- draw_rect
->bottom
,
398 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
400 checkGLcall("glScissor");
401 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
402 checkGLcall("glClear");
408 /* Now process each rect in turn. */
409 for (i
= 0; i
< rect_count
; ++i
)
411 /* Note that GL uses lower left, width/height. */
412 IntersectRect(¤t_rect
, draw_rect
, &clear_rect
[i
]);
414 TRACE("clear_rect[%u] %s, current_rect %s.\n", i
,
415 wine_dbgstr_rect(&clear_rect
[i
]),
416 wine_dbgstr_rect(¤t_rect
));
418 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
419 * The rectangle is not cleared, no error is returned, but further rectangles are
420 * still cleared if they are valid. */
421 if (current_rect
.left
> current_rect
.right
|| current_rect
.top
> current_rect
.bottom
)
423 TRACE("Rectangle with negative dimensions, ignoring.\n");
427 if (render_offscreen
)
429 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, current_rect
.top
,
430 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
434 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, drawable_height
- current_rect
.bottom
,
435 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
437 checkGLcall("glScissor");
439 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
440 checkGLcall("glClear");
444 if (wined3d_settings
.strict_draw_ordering
|| (flags
& WINED3DCLEAR_TARGET
445 && target
->container
->swapchain
&& target
->container
->swapchain
->front_buffer
== target
->container
))
446 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
448 context_release(context
);
451 ULONG CDECL
wined3d_device_incref(struct wined3d_device
*device
)
453 ULONG refcount
= InterlockedIncrement(&device
->ref
);
455 TRACE("%p increasing refcount to %u.\n", device
, refcount
);
460 static void device_leftover_sampler(struct wine_rb_entry
*entry
, void *context
)
462 struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
464 ERR("Leftover sampler %p.\n", sampler
);
467 ULONG CDECL
wined3d_device_decref(struct wined3d_device
*device
)
469 ULONG refcount
= InterlockedDecrement(&device
->ref
);
471 TRACE("%p decreasing refcount to %u.\n", device
, refcount
);
477 wined3d_cs_destroy(device
->cs
);
479 if (device
->recording
&& wined3d_stateblock_decref(device
->recording
))
480 ERR("Something's still holding the recording stateblock.\n");
481 device
->recording
= NULL
;
483 state_cleanup(&device
->state
);
485 for (i
= 0; i
< sizeof(device
->multistate_funcs
) / sizeof(device
->multistate_funcs
[0]); ++i
)
487 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
488 device
->multistate_funcs
[i
] = NULL
;
491 if (!list_empty(&device
->resources
))
493 struct wined3d_resource
*resource
;
495 ERR("Device released with resources still bound.\n");
497 LIST_FOR_EACH_ENTRY(resource
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
499 ERR("Leftover resource %p with type %s (%#x).\n",
500 resource
, debug_d3dresourcetype(resource
->type
), resource
->type
);
504 if (device
->contexts
)
505 ERR("Context array not freed!\n");
506 if (device
->hardwareCursor
)
507 DestroyCursor(device
->hardwareCursor
);
508 device
->hardwareCursor
= 0;
510 wine_rb_destroy(&device
->samplers
, device_leftover_sampler
, NULL
);
512 wined3d_decref(device
->wined3d
);
513 device
->wined3d
= NULL
;
514 HeapFree(GetProcessHeap(), 0, device
);
515 TRACE("Freed device %p.\n", device
);
521 UINT CDECL
wined3d_device_get_swapchain_count(const struct wined3d_device
*device
)
523 TRACE("device %p.\n", device
);
525 return device
->swapchain_count
;
528 struct wined3d_swapchain
* CDECL
wined3d_device_get_swapchain(const struct wined3d_device
*device
, UINT swapchain_idx
)
530 TRACE("device %p, swapchain_idx %u.\n", device
, swapchain_idx
);
532 if (swapchain_idx
>= device
->swapchain_count
)
534 WARN("swapchain_idx %u >= swapchain_count %u.\n",
535 swapchain_idx
, device
->swapchain_count
);
539 return device
->swapchains
[swapchain_idx
];
542 static void device_load_logo(struct wined3d_device
*device
, const char *filename
)
544 struct wined3d_color_key color_key
;
545 struct wined3d_resource_desc desc
;
549 HDC dcb
= NULL
, dcs
= NULL
;
551 if (!(hbm
= LoadImageA(NULL
, filename
, IMAGE_BITMAP
, 0, 0, LR_LOADFROMFILE
| LR_CREATEDIBSECTION
)))
553 ERR_(winediag
)("Failed to load logo %s.\n", wine_dbgstr_a(filename
));
556 GetObjectA(hbm
, sizeof(BITMAP
), &bm
);
558 if (!(dcb
= CreateCompatibleDC(NULL
)))
560 SelectObject(dcb
, hbm
);
562 desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
563 desc
.format
= WINED3DFMT_B5G6R5_UNORM
;
564 desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
565 desc
.multisample_quality
= 0;
566 desc
.usage
= WINED3DUSAGE_DYNAMIC
;
567 desc
.pool
= WINED3D_POOL_DEFAULT
;
568 desc
.width
= bm
.bmWidth
;
569 desc
.height
= bm
.bmHeight
;
572 if (FAILED(hr
= wined3d_texture_create(device
, &desc
, 1, 1,
573 WINED3D_TEXTURE_CREATE_MAPPABLE
| WINED3D_TEXTURE_CREATE_GET_DC
,
574 NULL
, NULL
, &wined3d_null_parent_ops
, &device
->logo_texture
)))
576 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr
);
580 if (FAILED(hr
= wined3d_texture_get_dc(device
->logo_texture
, 0, &dcs
)))
582 wined3d_texture_decref(device
->logo_texture
);
583 device
->logo_texture
= NULL
;
586 BitBlt(dcs
, 0, 0, bm
.bmWidth
, bm
.bmHeight
, dcb
, 0, 0, SRCCOPY
);
587 wined3d_texture_release_dc(device
->logo_texture
, 0, dcs
);
589 color_key
.color_space_low_value
= 0;
590 color_key
.color_space_high_value
= 0;
591 wined3d_texture_set_color_key(device
->logo_texture
, WINED3D_CKEY_SRC_BLT
, &color_key
);
594 if (dcb
) DeleteDC(dcb
);
595 if (hbm
) DeleteObject(hbm
);
598 /* Context activation is done by the caller. */
599 static void create_dummy_textures(struct wined3d_device
*device
, struct wined3d_context
*context
)
601 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
602 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
606 if (d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR
)
611 /* Under DirectX you can sample even if no texture is bound, whereas
612 * OpenGL will only allow that when a valid texture is bound.
613 * We emulate this by creating dummy textures and binding them
614 * to each texture stage when the currently set D3D texture is NULL. */
615 context_active_texture(context
, gl_info
, 0);
617 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_2d
);
618 checkGLcall("glGenTextures");
619 TRACE("Dummy 2D texture given name %u.\n", device
->dummy_textures
.tex_2d
);
621 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_textures
.tex_2d
);
622 checkGLcall("glBindTexture");
624 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA8
, 1, 1, 0,
625 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
626 checkGLcall("glTexImage2D");
628 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
630 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_rect
);
631 checkGLcall("glGenTextures");
632 TRACE("Dummy rectangle texture given name %u.\n", device
->dummy_textures
.tex_rect
);
634 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_textures
.tex_rect
);
635 checkGLcall("glBindTexture");
637 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA8
, 1, 1, 0,
638 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
639 checkGLcall("glTexImage2D");
642 if (gl_info
->supported
[EXT_TEXTURE3D
])
644 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_3d
);
645 checkGLcall("glGenTextures");
646 TRACE("Dummy 3D texture given name %u.\n", device
->dummy_textures
.tex_3d
);
648 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_textures
.tex_3d
);
649 checkGLcall("glBindTexture");
651 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, 0, GL_RGBA8
, 1, 1, 1, 0,
652 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
));
653 checkGLcall("glTexImage3D");
656 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
658 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_cube
);
659 checkGLcall("glGenTextures");
660 TRACE("Dummy cube texture given name %u.\n", device
->dummy_textures
.tex_cube
);
662 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_textures
.tex_cube
);
663 checkGLcall("glBindTexture");
665 for (i
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
; i
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
; ++i
)
667 gl_info
->gl_ops
.gl
.p_glTexImage2D(i
, 0, GL_RGBA8
, 1, 1, 0,
668 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
669 checkGLcall("glTexImage2D");
673 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
675 DWORD cube_array_data
[6];
677 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_cube_array
);
678 checkGLcall("glGenTextures");
679 TRACE("Dummy cube array texture given name %u.\n", device
->dummy_textures
.tex_cube_array
);
681 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, device
->dummy_textures
.tex_cube_array
);
682 checkGLcall("glBindTexture");
684 for (i
= 0; i
< ARRAY_SIZE(cube_array_data
); ++i
)
685 cube_array_data
[i
] = color
;
686 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY
, 0, GL_RGBA8
, 1, 1, 6, 0,
687 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, cube_array_data
));
688 checkGLcall("glTexImage3D");
691 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
693 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_2d_array
);
694 checkGLcall("glGenTextures");
695 TRACE("Dummy 2D array texture given name %u.\n", device
->dummy_textures
.tex_2d_array
);
697 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_textures
.tex_2d_array
);
698 checkGLcall("glBindTexture");
700 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY
, 0, GL_RGBA8
, 1, 1, 1, 0,
701 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
));
702 checkGLcall("glTexImage3D");
705 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
709 GL_EXTCALL(glGenBuffers(1, &buffer
));
710 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER
, buffer
));
711 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER
, sizeof(color
), &color
, GL_STATIC_DRAW
));
712 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER
, 0));
713 checkGLcall("Create buffer object");
715 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_buffer
);
716 checkGLcall("glGenTextures");
717 TRACE("Dummy buffer texture given name %u.\n", device
->dummy_textures
.tex_buffer
);
719 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, device
->dummy_textures
.tex_buffer
);
720 checkGLcall("glBindTexture");
721 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER
, GL_RGBA8
, buffer
));
722 checkGLcall("glTexBuffer");
724 GL_EXTCALL(glDeleteBuffers(1, &buffer
));
725 checkGLcall("glDeleteBuffers");
728 context_bind_dummy_textures(device
, context
);
731 /* Context activation is done by the caller. */
732 static void destroy_dummy_textures(struct wined3d_device
*device
, struct wined3d_context
*context
)
734 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
736 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
737 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_buffer
);
739 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
740 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_2d_array
);
742 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
743 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_cube_array
);
745 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
746 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_cube
);
748 if (gl_info
->supported
[EXT_TEXTURE3D
])
749 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_3d
);
751 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
752 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_rect
);
754 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_2d
);
756 checkGLcall("Delete dummy textures");
758 memset(&device
->dummy_textures
, 0, sizeof(device
->dummy_textures
));
761 /* Context activation is done by the caller. */
762 static void create_default_samplers(struct wined3d_device
*device
, struct wined3d_context
*context
)
764 struct wined3d_sampler_desc desc
;
767 desc
.address_u
= WINED3D_TADDRESS_WRAP
;
768 desc
.address_v
= WINED3D_TADDRESS_WRAP
;
769 desc
.address_w
= WINED3D_TADDRESS_WRAP
;
770 memset(desc
.border_color
, 0, sizeof(desc
.border_color
));
771 desc
.mag_filter
= WINED3D_TEXF_POINT
;
772 desc
.min_filter
= WINED3D_TEXF_POINT
;
773 desc
.mip_filter
= WINED3D_TEXF_NONE
;
774 desc
.lod_bias
= 0.0f
;
775 desc
.min_lod
= -1000.0f
;
776 desc
.max_lod
= 1000.0f
;
777 desc
.mip_base_level
= 0;
778 desc
.max_anisotropy
= 1;
779 desc
.compare
= FALSE
;
780 desc
.comparison_func
= WINED3D_CMP_NEVER
;
781 desc
.srgb_decode
= TRUE
;
783 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
784 * instructions allow access to resources without using samplers.
785 * In GLSL, resources are always accessed through sampler or image variables. The default
786 * sampler object is used to emulate the direct resource access when there is no sampler state
789 if (FAILED(hr
= wined3d_sampler_create(device
, &desc
, NULL
, &device
->default_sampler
)))
791 ERR("Failed to create default sampler, hr %#x.\n", hr
);
792 device
->default_sampler
= NULL
;
795 /* In D3D10+, a NULL sampler maps to the default sampler state. */
796 desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
797 desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
798 desc
.address_w
= WINED3D_TADDRESS_CLAMP
;
799 desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
800 desc
.min_filter
= WINED3D_TEXF_LINEAR
;
801 desc
.mip_filter
= WINED3D_TEXF_LINEAR
;
802 if (FAILED(hr
= wined3d_sampler_create(device
, &desc
, NULL
, &device
->null_sampler
)))
804 ERR("Failed to create null sampler, hr %#x.\n", hr
);
805 device
->null_sampler
= NULL
;
809 /* Context activation is done by the caller. */
810 static void destroy_default_samplers(struct wined3d_device
*device
, struct wined3d_context
*context
)
812 wined3d_sampler_decref(device
->default_sampler
);
813 device
->default_sampler
= NULL
;
814 wined3d_sampler_decref(device
->null_sampler
);
815 device
->null_sampler
= NULL
;
818 static LONG
fullscreen_style(LONG style
)
820 /* Make sure the window is managed, otherwise we won't get keyboard input. */
821 style
|= WS_POPUP
| WS_SYSMENU
;
822 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
827 static LONG
fullscreen_exstyle(LONG exstyle
)
829 /* Filter out window decorations. */
830 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
835 void CDECL
wined3d_device_setup_fullscreen_window(struct wined3d_device
*device
, HWND window
, UINT w
, UINT h
)
837 BOOL filter_messages
;
840 TRACE("Setting up window %p for fullscreen mode.\n", window
);
842 if (device
->style
|| device
->exStyle
)
844 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
845 window
, device
->style
, device
->exStyle
);
848 device
->style
= GetWindowLongW(window
, GWL_STYLE
);
849 device
->exStyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
851 style
= fullscreen_style(device
->style
);
852 exstyle
= fullscreen_exstyle(device
->exStyle
);
854 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
855 device
->style
, device
->exStyle
, style
, exstyle
);
857 filter_messages
= device
->filter_messages
;
858 device
->filter_messages
= TRUE
;
860 SetWindowLongW(window
, GWL_STYLE
, style
);
861 SetWindowLongW(window
, GWL_EXSTYLE
, exstyle
);
862 SetWindowPos(window
, HWND_TOPMOST
, 0, 0, w
, h
, SWP_FRAMECHANGED
| SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
864 device
->filter_messages
= filter_messages
;
867 void CDECL
wined3d_device_restore_fullscreen_window(struct wined3d_device
*device
, HWND window
,
868 const RECT
*window_rect
)
870 unsigned int window_pos_flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
871 BOOL filter_messages
;
875 if (!device
->style
&& !device
->exStyle
)
878 style
= GetWindowLongW(window
, GWL_STYLE
);
879 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
881 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
882 * application, and we want to ignore them in the test below, since it's
883 * not the application's fault that they changed. Additionally, we want to
884 * preserve the current status of these flags (i.e. don't restore them) to
885 * more closely emulate the behavior of Direct3D, which leaves these flags
886 * alone when returning to windowed mode. */
887 device
->style
^= (device
->style
^ style
) & WS_VISIBLE
;
888 device
->exStyle
^= (device
->exStyle
^ exstyle
) & WS_EX_TOPMOST
;
890 TRACE("Restoring window style of window %p to %08x, %08x.\n",
891 window
, device
->style
, device
->exStyle
);
893 filter_messages
= device
->filter_messages
;
894 device
->filter_messages
= TRUE
;
896 /* Only restore the style if the application didn't modify it during the
897 * fullscreen phase. Some applications change it before calling Reset()
898 * when switching between windowed and fullscreen modes (HL2), some
899 * depend on the original style (Eve Online). */
900 if (style
== fullscreen_style(device
->style
) && exstyle
== fullscreen_exstyle(device
->exStyle
))
902 SetWindowLongW(window
, GWL_STYLE
, device
->style
);
903 SetWindowLongW(window
, GWL_EXSTYLE
, device
->exStyle
);
909 window_pos_flags
|= (SWP_NOMOVE
| SWP_NOSIZE
);
910 SetWindowPos(window
, 0, rect
.left
, rect
.top
,
911 rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, window_pos_flags
);
913 device
->filter_messages
= filter_messages
;
915 /* Delete the old values. */
920 HRESULT CDECL
wined3d_device_acquire_focus_window(struct wined3d_device
*device
, HWND window
)
922 TRACE("device %p, window %p.\n", device
, window
);
924 if (!wined3d_register_window(window
, device
))
926 ERR("Failed to register window %p.\n", window
);
930 InterlockedExchangePointer((void **)&device
->focus_window
, window
);
931 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
);
936 void CDECL
wined3d_device_release_focus_window(struct wined3d_device
*device
)
938 TRACE("device %p.\n", device
);
940 if (device
->focus_window
) wined3d_unregister_window(device
->focus_window
);
941 InterlockedExchangePointer((void **)&device
->focus_window
, NULL
);
944 static void device_init_swapchain_state(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
946 BOOL ds_enable
= !!swapchain
->desc
.enable_auto_depth_stencil
;
949 if (device
->fb
.render_targets
)
951 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
953 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
955 if (device
->back_buffer_view
)
956 wined3d_device_set_rendertarget_view(device
, 0, device
->back_buffer_view
, TRUE
);
959 wined3d_device_set_depth_stencil_view(device
, ds_enable
? device
->auto_depth_stencil_view
: NULL
);
960 wined3d_device_set_render_state(device
, WINED3D_RS_ZENABLE
, ds_enable
);
963 static void wined3d_device_delete_opengl_contexts_cs(void *object
)
965 struct wined3d_resource
*resource
, *cursor
;
966 struct wined3d_device
*device
= object
;
967 struct wined3d_context
*context
;
968 struct wined3d_shader
*shader
;
970 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
972 TRACE("Unloading resource %p.\n", resource
);
973 wined3d_cs_emit_unload_resource(device
->cs
, resource
);
976 LIST_FOR_EACH_ENTRY(shader
, &device
->shaders
, struct wined3d_shader
, shader_list_entry
)
978 device
->shader_backend
->shader_destroy(shader
);
981 context
= context_acquire(device
, NULL
, 0);
982 device
->blitter
->free_private(device
);
983 device
->shader_backend
->shader_free_private(device
);
984 destroy_dummy_textures(device
, context
);
985 destroy_default_samplers(device
, context
);
986 context_release(context
);
988 while (device
->context_count
)
990 if (device
->contexts
[0]->swapchain
)
991 swapchain_destroy_contexts(device
->contexts
[0]->swapchain
);
993 context_destroy(device
, device
->contexts
[0]);
997 static void wined3d_device_delete_opengl_contexts(struct wined3d_device
*device
)
999 wined3d_cs_destroy_object(device
->cs
, wined3d_device_delete_opengl_contexts_cs
, device
);
1002 static void wined3d_device_create_primary_opengl_context_cs(void *object
)
1004 struct wined3d_device
*device
= object
;
1005 struct wined3d_swapchain
*swapchain
;
1006 struct wined3d_context
*context
;
1007 struct wined3d_texture
*target
;
1010 if (FAILED(hr
= device
->shader_backend
->shader_alloc_private(device
,
1011 device
->adapter
->vertex_pipe
, device
->adapter
->fragment_pipe
)))
1013 ERR("Failed to allocate shader private data, hr %#x.\n", hr
);
1017 if (FAILED(hr
= device
->blitter
->alloc_private(device
)))
1019 ERR("Failed to allocate blitter private data, hr %#x.\n", hr
);
1020 device
->shader_backend
->shader_free_private(device
);
1024 swapchain
= device
->swapchains
[0];
1025 target
= swapchain
->back_buffers
? swapchain
->back_buffers
[0] : swapchain
->front_buffer
;
1026 context
= context_acquire(device
, target
, 0);
1027 create_dummy_textures(device
, context
);
1028 create_default_samplers(device
, context
);
1029 context_release(context
);
1032 static HRESULT
wined3d_device_create_primary_opengl_context(struct wined3d_device
*device
)
1034 wined3d_cs_init_object(device
->cs
, wined3d_device_create_primary_opengl_context_cs
, device
);
1035 if (!device
->swapchains
[0]->num_contexts
)
1041 HRESULT CDECL
wined3d_device_init_3d(struct wined3d_device
*device
,
1042 struct wined3d_swapchain_desc
*swapchain_desc
)
1044 static const struct wined3d_color black
= {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1045 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1046 struct wined3d_swapchain
*swapchain
= NULL
;
1047 DWORD clear_flags
= 0;
1050 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1052 if (device
->d3d_initialized
)
1053 return WINED3DERR_INVALIDCALL
;
1054 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1055 return WINED3DERR_INVALIDCALL
;
1057 if (!(device
->fb
.render_targets
= wined3d_calloc(gl_info
->limits
.buffers
, sizeof(*device
->fb
.render_targets
))))
1058 return E_OUTOFMEMORY
;
1060 /* Setup the implicit swapchain. This also initializes a context. */
1061 TRACE("Creating implicit swapchain\n");
1062 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1063 swapchain_desc
, &swapchain
);
1066 WARN("Failed to create implicit swapchain\n");
1070 if (swapchain_desc
->backbuffer_count
)
1072 struct wined3d_resource
*back_buffer
= &swapchain
->back_buffers
[0]->resource
;
1073 struct wined3d_view_desc view_desc
;
1075 view_desc
.format_id
= back_buffer
->format
->id
;
1076 view_desc
.flags
= 0;
1077 view_desc
.u
.texture
.level_idx
= 0;
1078 view_desc
.u
.texture
.level_count
= 1;
1079 view_desc
.u
.texture
.layer_idx
= 0;
1080 view_desc
.u
.texture
.layer_count
= 1;
1081 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
, back_buffer
,
1082 NULL
, &wined3d_null_parent_ops
, &device
->back_buffer_view
)))
1084 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1089 device
->swapchain_count
= 1;
1090 if (!(device
->swapchains
= wined3d_calloc(device
->swapchain_count
, sizeof(*device
->swapchains
))))
1092 ERR("Out of memory!\n");
1095 device
->swapchains
[0] = swapchain
;
1097 if (FAILED(hr
= wined3d_device_create_primary_opengl_context(device
)))
1099 device_init_swapchain_state(device
, swapchain
);
1101 device
->contexts
[0]->last_was_rhw
= 0;
1103 TRACE("All defaults now set up, leaving 3D init.\n");
1105 /* Clear the screen */
1106 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0])
1107 clear_flags
|= WINED3DCLEAR_TARGET
;
1108 if (swapchain_desc
->enable_auto_depth_stencil
)
1109 clear_flags
|= WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
;
1111 wined3d_device_clear(device
, 0, NULL
, clear_flags
, &black
, 1.0f
, 0);
1113 device
->d3d_initialized
= TRUE
;
1115 if (wined3d_settings
.logo
)
1116 device_load_logo(device
, wined3d_settings
.logo
);
1120 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1121 device
->swapchain_count
= 0;
1122 if (device
->back_buffer_view
)
1123 wined3d_rendertarget_view_decref(device
->back_buffer_view
);
1125 wined3d_swapchain_decref(swapchain
);
1126 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1131 HRESULT CDECL
wined3d_device_init_gdi(struct wined3d_device
*device
,
1132 struct wined3d_swapchain_desc
*swapchain_desc
)
1134 struct wined3d_swapchain
*swapchain
= NULL
;
1137 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1139 /* Setup the implicit swapchain */
1140 TRACE("Creating implicit swapchain\n");
1141 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1142 swapchain_desc
, &swapchain
);
1145 WARN("Failed to create implicit swapchain\n");
1149 device
->swapchain_count
= 1;
1150 if (!(device
->swapchains
= wined3d_calloc(device
->swapchain_count
, sizeof(*device
->swapchains
))))
1152 ERR("Out of memory!\n");
1155 device
->swapchains
[0] = swapchain
;
1159 wined3d_swapchain_decref(swapchain
);
1163 static void device_free_sampler(struct wine_rb_entry
*entry
, void *context
)
1165 struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
1167 wined3d_sampler_decref(sampler
);
1170 HRESULT CDECL
wined3d_device_uninit_3d(struct wined3d_device
*device
)
1174 TRACE("device %p.\n", device
);
1176 if (!device
->d3d_initialized
)
1177 return WINED3DERR_INVALIDCALL
;
1179 if (device
->logo_texture
)
1180 wined3d_texture_decref(device
->logo_texture
);
1181 if (device
->cursor_texture
)
1182 wined3d_texture_decref(device
->cursor_texture
);
1184 state_unbind_resources(&device
->state
);
1186 wine_rb_clear(&device
->samplers
, device_free_sampler
, NULL
);
1188 wined3d_device_delete_opengl_contexts(device
);
1190 if (device
->fb
.depth_stencil
)
1192 struct wined3d_rendertarget_view
*view
= device
->fb
.depth_stencil
;
1194 TRACE("Releasing depth/stencil view %p.\n", view
);
1196 device
->fb
.depth_stencil
= NULL
;
1197 wined3d_rendertarget_view_decref(view
);
1200 if (device
->auto_depth_stencil_view
)
1202 struct wined3d_rendertarget_view
*view
= device
->auto_depth_stencil_view
;
1204 device
->auto_depth_stencil_view
= NULL
;
1205 if (wined3d_rendertarget_view_decref(view
))
1206 ERR("Something's still holding the auto depth/stencil view (%p).\n", view
);
1209 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
1211 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
1213 if (device
->back_buffer_view
)
1215 wined3d_rendertarget_view_decref(device
->back_buffer_view
);
1216 device
->back_buffer_view
= NULL
;
1219 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1221 TRACE("Releasing the implicit swapchain %u.\n", i
);
1222 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1223 FIXME("Something's still holding the implicit swapchain.\n");
1226 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1227 device
->swapchains
= NULL
;
1228 device
->swapchain_count
= 0;
1230 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1231 device
->fb
.render_targets
= NULL
;
1233 device
->d3d_initialized
= FALSE
;
1238 HRESULT CDECL
wined3d_device_uninit_gdi(struct wined3d_device
*device
)
1242 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1244 TRACE("Releasing the implicit swapchain %u.\n", i
);
1245 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1246 FIXME("Something's still holding the implicit swapchain.\n");
1249 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1250 device
->swapchains
= NULL
;
1251 device
->swapchain_count
= 0;
1255 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1256 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1257 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1259 * There is no way to deactivate thread safety once it is enabled.
1261 void CDECL
wined3d_device_set_multithreaded(struct wined3d_device
*device
)
1263 TRACE("device %p.\n", device
);
1265 /* For now just store the flag (needed in case of ddraw). */
1266 device
->create_parms
.flags
|= WINED3DCREATE_MULTITHREADED
;
1269 UINT CDECL
wined3d_device_get_available_texture_mem(const struct wined3d_device
*device
)
1271 TRACE("device %p.\n", device
);
1273 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1274 wine_dbgstr_longlong(device
->adapter
->vram_bytes
),
1275 wine_dbgstr_longlong(device
->adapter
->vram_bytes_used
),
1276 wine_dbgstr_longlong(device
->adapter
->vram_bytes
- device
->adapter
->vram_bytes_used
));
1278 return min(UINT_MAX
, device
->adapter
->vram_bytes
- device
->adapter
->vram_bytes_used
);
1281 void CDECL
wined3d_device_set_stream_output(struct wined3d_device
*device
, UINT idx
,
1282 struct wined3d_buffer
*buffer
, UINT offset
)
1284 struct wined3d_stream_output
*stream
;
1285 struct wined3d_buffer
*prev_buffer
;
1287 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device
, idx
, buffer
, offset
);
1289 if (idx
>= WINED3D_MAX_STREAM_OUTPUT_BUFFERS
)
1291 WARN("Invalid stream output %u.\n", idx
);
1295 stream
= &device
->update_state
->stream_output
[idx
];
1296 prev_buffer
= stream
->buffer
;
1299 wined3d_buffer_incref(buffer
);
1300 stream
->buffer
= buffer
;
1301 stream
->offset
= offset
;
1302 if (!device
->recording
)
1303 wined3d_cs_emit_set_stream_output(device
->cs
, idx
, buffer
, offset
);
1305 wined3d_buffer_decref(prev_buffer
);
1308 struct wined3d_buffer
* CDECL
wined3d_device_get_stream_output(struct wined3d_device
*device
,
1309 UINT idx
, UINT
*offset
)
1311 TRACE("device %p, idx %u, offset %p.\n", device
, idx
, offset
);
1313 if (idx
>= WINED3D_MAX_STREAM_OUTPUT_BUFFERS
)
1315 WARN("Invalid stream output %u.\n", idx
);
1320 *offset
= device
->state
.stream_output
[idx
].offset
;
1321 return device
->state
.stream_output
[idx
].buffer
;
1324 HRESULT CDECL
wined3d_device_set_stream_source(struct wined3d_device
*device
, UINT stream_idx
,
1325 struct wined3d_buffer
*buffer
, UINT offset
, UINT stride
)
1327 struct wined3d_stream_state
*stream
;
1328 struct wined3d_buffer
*prev_buffer
;
1330 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1331 device
, stream_idx
, buffer
, offset
, stride
);
1333 if (stream_idx
>= MAX_STREAMS
)
1335 WARN("Stream index %u out of range.\n", stream_idx
);
1336 return WINED3DERR_INVALIDCALL
;
1338 else if (offset
& 0x3)
1340 WARN("Offset %u is not 4 byte aligned.\n", offset
);
1341 return WINED3DERR_INVALIDCALL
;
1344 stream
= &device
->update_state
->streams
[stream_idx
];
1345 prev_buffer
= stream
->buffer
;
1347 if (device
->recording
)
1348 device
->recording
->changed
.streamSource
|= 1u << stream_idx
;
1350 if (prev_buffer
== buffer
1351 && stream
->stride
== stride
1352 && stream
->offset
== offset
)
1354 TRACE("Application is setting the old values over, nothing to do.\n");
1358 stream
->buffer
= buffer
;
1361 stream
->stride
= stride
;
1362 stream
->offset
= offset
;
1363 wined3d_buffer_incref(buffer
);
1366 if (!device
->recording
)
1367 wined3d_cs_emit_set_stream_source(device
->cs
, stream_idx
, buffer
, offset
, stride
);
1369 wined3d_buffer_decref(prev_buffer
);
1374 HRESULT CDECL
wined3d_device_get_stream_source(const struct wined3d_device
*device
,
1375 UINT stream_idx
, struct wined3d_buffer
**buffer
, UINT
*offset
, UINT
*stride
)
1377 const struct wined3d_stream_state
*stream
;
1379 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1380 device
, stream_idx
, buffer
, offset
, stride
);
1382 if (stream_idx
>= MAX_STREAMS
)
1384 WARN("Stream index %u out of range.\n", stream_idx
);
1385 return WINED3DERR_INVALIDCALL
;
1388 stream
= &device
->state
.streams
[stream_idx
];
1389 *buffer
= stream
->buffer
;
1391 *offset
= stream
->offset
;
1392 *stride
= stream
->stride
;
1397 HRESULT CDECL
wined3d_device_set_stream_source_freq(struct wined3d_device
*device
, UINT stream_idx
, UINT divider
)
1399 struct wined3d_stream_state
*stream
;
1400 UINT old_flags
, old_freq
;
1402 TRACE("device %p, stream_idx %u, divider %#x.\n", device
, stream_idx
, divider
);
1404 /* Verify input. At least in d3d9 this is invalid. */
1405 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && (divider
& WINED3DSTREAMSOURCE_INDEXEDDATA
))
1407 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1408 return WINED3DERR_INVALIDCALL
;
1410 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && !stream_idx
)
1412 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1413 return WINED3DERR_INVALIDCALL
;
1417 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1418 return WINED3DERR_INVALIDCALL
;
1421 stream
= &device
->update_state
->streams
[stream_idx
];
1422 old_flags
= stream
->flags
;
1423 old_freq
= stream
->frequency
;
1425 stream
->flags
= divider
& (WINED3DSTREAMSOURCE_INSTANCEDATA
| WINED3DSTREAMSOURCE_INDEXEDDATA
);
1426 stream
->frequency
= divider
& 0x7fffff;
1428 if (device
->recording
)
1429 device
->recording
->changed
.streamFreq
|= 1u << stream_idx
;
1430 else if (stream
->frequency
!= old_freq
|| stream
->flags
!= old_flags
)
1431 wined3d_cs_emit_set_stream_source_freq(device
->cs
, stream_idx
, stream
->frequency
, stream
->flags
);
1436 HRESULT CDECL
wined3d_device_get_stream_source_freq(const struct wined3d_device
*device
,
1437 UINT stream_idx
, UINT
*divider
)
1439 const struct wined3d_stream_state
*stream
;
1441 TRACE("device %p, stream_idx %u, divider %p.\n", device
, stream_idx
, divider
);
1443 stream
= &device
->state
.streams
[stream_idx
];
1444 *divider
= stream
->flags
| stream
->frequency
;
1446 TRACE("Returning %#x.\n", *divider
);
1451 void CDECL
wined3d_device_set_transform(struct wined3d_device
*device
,
1452 enum wined3d_transform_state d3dts
, const struct wined3d_matrix
*matrix
)
1454 TRACE("device %p, state %s, matrix %p.\n",
1455 device
, debug_d3dtstype(d3dts
), matrix
);
1456 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_11
, matrix
->_12
, matrix
->_13
, matrix
->_14
);
1457 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_21
, matrix
->_22
, matrix
->_23
, matrix
->_24
);
1458 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_31
, matrix
->_32
, matrix
->_33
, matrix
->_34
);
1459 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_41
, matrix
->_42
, matrix
->_43
, matrix
->_44
);
1461 /* Handle recording of state blocks. */
1462 if (device
->recording
)
1464 TRACE("Recording... not performing anything.\n");
1465 device
->recording
->changed
.transform
[d3dts
>> 5] |= 1u << (d3dts
& 0x1f);
1466 device
->update_state
->transforms
[d3dts
] = *matrix
;
1470 /* If the new matrix is the same as the current one,
1471 * we cut off any further processing. this seems to be a reasonable
1472 * optimization because as was noticed, some apps (warcraft3 for example)
1473 * tend towards setting the same matrix repeatedly for some reason.
1475 * From here on we assume that the new matrix is different, wherever it matters. */
1476 if (!memcmp(&device
->state
.transforms
[d3dts
], matrix
, sizeof(*matrix
)))
1478 TRACE("The application is setting the same matrix over again.\n");
1482 device
->state
.transforms
[d3dts
] = *matrix
;
1483 wined3d_cs_emit_set_transform(device
->cs
, d3dts
, matrix
);
1486 void CDECL
wined3d_device_get_transform(const struct wined3d_device
*device
,
1487 enum wined3d_transform_state state
, struct wined3d_matrix
*matrix
)
1489 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1491 *matrix
= device
->state
.transforms
[state
];
1494 void CDECL
wined3d_device_multiply_transform(struct wined3d_device
*device
,
1495 enum wined3d_transform_state state
, const struct wined3d_matrix
*matrix
)
1497 const struct wined3d_matrix
*mat
;
1498 struct wined3d_matrix temp
;
1500 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1502 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1503 * below means it will be recorded in a state block change, but it
1504 * works regardless where it is recorded.
1505 * If this is found to be wrong, change to StateBlock. */
1506 if (state
> HIGHEST_TRANSFORMSTATE
)
1508 WARN("Unhandled transform state %#x.\n", state
);
1512 mat
= &device
->update_state
->transforms
[state
];
1513 multiply_matrix(&temp
, mat
, matrix
);
1515 /* Apply change via set transform - will reapply to eg. lights this way. */
1516 wined3d_device_set_transform(device
, state
, &temp
);
1519 /* Note lights are real special cases. Although the device caps state only
1520 * e.g. 8 are supported, you can reference any indexes you want as long as
1521 * that number max are enabled at any one point in time. Therefore since the
1522 * indices can be anything, we need a hashmap of them. However, this causes
1523 * stateblock problems. When capturing the state block, I duplicate the
1524 * hashmap, but when recording, just build a chain pretty much of commands to
1526 HRESULT CDECL
wined3d_device_set_light(struct wined3d_device
*device
,
1527 UINT light_idx
, const struct wined3d_light
*light
)
1529 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1530 struct wined3d_light_info
*object
= NULL
;
1533 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1535 /* Check the parameter range. Need for speed most wanted sets junk lights
1536 * which confuse the GL driver. */
1538 return WINED3DERR_INVALIDCALL
;
1540 switch (light
->type
)
1542 case WINED3D_LIGHT_POINT
:
1543 case WINED3D_LIGHT_SPOT
:
1544 case WINED3D_LIGHT_GLSPOT
:
1545 /* Incorrect attenuation values can cause the gl driver to crash.
1546 * Happens with Need for speed most wanted. */
1547 if (light
->attenuation0
< 0.0f
|| light
->attenuation1
< 0.0f
|| light
->attenuation2
< 0.0f
)
1549 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1550 return WINED3DERR_INVALIDCALL
;
1554 case WINED3D_LIGHT_DIRECTIONAL
:
1555 case WINED3D_LIGHT_PARALLELPOINT
:
1556 /* Ignores attenuation */
1560 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1561 return WINED3DERR_INVALIDCALL
;
1564 if (!(object
= wined3d_state_get_light(device
->update_state
, light_idx
)))
1566 TRACE("Adding new light\n");
1567 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1569 return E_OUTOFMEMORY
;
1571 list_add_head(&device
->update_state
->light_map
[hash_idx
], &object
->entry
);
1572 object
->glIndex
= -1;
1573 object
->OriginalIndex
= light_idx
;
1576 /* Initialize the object. */
1577 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1578 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1579 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1580 light_idx
, light
->type
, debug_color(&light
->diffuse
),
1581 debug_color(&light
->specular
), debug_color(&light
->ambient
),
1582 light
->position
.x
, light
->position
.y
, light
->position
.z
,
1583 light
->direction
.x
, light
->direction
.y
, light
->direction
.z
,
1584 light
->range
, light
->falloff
, light
->theta
, light
->phi
);
1586 /* Save away the information. */
1587 object
->OriginalParms
= *light
;
1589 switch (light
->type
)
1591 case WINED3D_LIGHT_POINT
:
1593 object
->position
.x
= light
->position
.x
;
1594 object
->position
.y
= light
->position
.y
;
1595 object
->position
.z
= light
->position
.z
;
1596 object
->position
.w
= 1.0f
;
1597 object
->cutoff
= 180.0f
;
1601 case WINED3D_LIGHT_DIRECTIONAL
:
1603 object
->direction
.x
= -light
->direction
.x
;
1604 object
->direction
.y
= -light
->direction
.y
;
1605 object
->direction
.z
= -light
->direction
.z
;
1606 object
->direction
.w
= 0.0f
;
1607 object
->exponent
= 0.0f
;
1608 object
->cutoff
= 180.0f
;
1611 case WINED3D_LIGHT_SPOT
:
1613 object
->position
.x
= light
->position
.x
;
1614 object
->position
.y
= light
->position
.y
;
1615 object
->position
.z
= light
->position
.z
;
1616 object
->position
.w
= 1.0f
;
1619 object
->direction
.x
= light
->direction
.x
;
1620 object
->direction
.y
= light
->direction
.y
;
1621 object
->direction
.z
= light
->direction
.z
;
1622 object
->direction
.w
= 0.0f
;
1624 /* opengl-ish and d3d-ish spot lights use too different models
1625 * for the light "intensity" as a function of the angle towards
1626 * the main light direction, so we only can approximate very
1627 * roughly. However, spot lights are rather rarely used in games
1628 * (if ever used at all). Furthermore if still used, probably
1629 * nobody pays attention to such details. */
1630 if (!light
->falloff
)
1632 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1633 * equations have the falloff resp. exponent parameter as an
1634 * exponent, so the spot light lighting will always be 1.0 for
1635 * both of them, and we don't have to care for the rest of the
1636 * rather complex calculation. */
1637 object
->exponent
= 0.0f
;
1641 rho
= light
->theta
+ (light
->phi
- light
->theta
) / (2 * light
->falloff
);
1644 object
->exponent
= -0.3f
/ logf(cosf(rho
/ 2));
1647 if (object
->exponent
> 128.0f
)
1648 object
->exponent
= 128.0f
;
1650 object
->cutoff
= (float)(light
->phi
* 90 / M_PI
);
1654 case WINED3D_LIGHT_PARALLELPOINT
:
1655 object
->position
.x
= light
->position
.x
;
1656 object
->position
.y
= light
->position
.y
;
1657 object
->position
.z
= light
->position
.z
;
1658 object
->position
.w
= 1.0f
;
1662 FIXME("Unrecognized light type %#x.\n", light
->type
);
1665 if (!device
->recording
)
1666 wined3d_cs_emit_set_light(device
->cs
, object
);
1671 HRESULT CDECL
wined3d_device_get_light(const struct wined3d_device
*device
,
1672 UINT light_idx
, struct wined3d_light
*light
)
1674 struct wined3d_light_info
*light_info
;
1676 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1678 if (!(light_info
= wined3d_state_get_light(&device
->state
, light_idx
)))
1680 TRACE("Light information requested but light not defined\n");
1681 return WINED3DERR_INVALIDCALL
;
1684 *light
= light_info
->OriginalParms
;
1688 HRESULT CDECL
wined3d_device_set_light_enable(struct wined3d_device
*device
, UINT light_idx
, BOOL enable
)
1690 struct wined3d_light_info
*light_info
;
1692 TRACE("device %p, light_idx %u, enable %#x.\n", device
, light_idx
, enable
);
1694 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1695 if (!(light_info
= wined3d_state_get_light(device
->update_state
, light_idx
)))
1697 TRACE("Light enabled requested but light not defined, so defining one!\n");
1698 wined3d_device_set_light(device
, light_idx
, &WINED3D_default_light
);
1700 if (!(light_info
= wined3d_state_get_light(device
->update_state
, light_idx
)))
1702 FIXME("Adding default lights has failed dismally\n");
1703 return WINED3DERR_INVALIDCALL
;
1707 wined3d_state_enable_light(device
->update_state
, &device
->adapter
->d3d_info
, light_info
, enable
);
1708 if (!device
->recording
)
1709 wined3d_cs_emit_set_light_enable(device
->cs
, light_idx
, enable
);
1714 HRESULT CDECL
wined3d_device_get_light_enable(const struct wined3d_device
*device
, UINT light_idx
, BOOL
*enable
)
1716 struct wined3d_light_info
*light_info
;
1718 TRACE("device %p, light_idx %u, enable %p.\n", device
, light_idx
, enable
);
1720 if (!(light_info
= wined3d_state_get_light(&device
->state
, light_idx
)))
1722 TRACE("Light enabled state requested but light not defined.\n");
1723 return WINED3DERR_INVALIDCALL
;
1725 /* true is 128 according to SetLightEnable */
1726 *enable
= light_info
->enabled
? 128 : 0;
1730 HRESULT CDECL
wined3d_device_set_clip_plane(struct wined3d_device
*device
,
1731 UINT plane_idx
, const struct wined3d_vec4
*plane
)
1733 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
1735 /* Validate plane_idx. */
1736 if (plane_idx
>= device
->adapter
->gl_info
.limits
.user_clip_distances
)
1738 TRACE("Application has requested clipplane this device doesn't support.\n");
1739 return WINED3DERR_INVALIDCALL
;
1742 if (device
->recording
)
1743 device
->recording
->changed
.clipplane
|= 1u << plane_idx
;
1745 if (!memcmp(&device
->update_state
->clip_planes
[plane_idx
], plane
, sizeof(*plane
)))
1747 TRACE("Application is setting old values over, nothing to do.\n");
1751 device
->update_state
->clip_planes
[plane_idx
] = *plane
;
1753 if (!device
->recording
)
1754 wined3d_cs_emit_set_clip_plane(device
->cs
, plane_idx
, plane
);
1759 HRESULT CDECL
wined3d_device_get_clip_plane(const struct wined3d_device
*device
,
1760 UINT plane_idx
, struct wined3d_vec4
*plane
)
1762 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
1764 /* Validate plane_idx. */
1765 if (plane_idx
>= device
->adapter
->gl_info
.limits
.user_clip_distances
)
1767 TRACE("Application has requested clipplane this device doesn't support.\n");
1768 return WINED3DERR_INVALIDCALL
;
1771 *plane
= device
->state
.clip_planes
[plane_idx
];
1776 HRESULT CDECL
wined3d_device_set_clip_status(struct wined3d_device
*device
,
1777 const struct wined3d_clip_status
*clip_status
)
1779 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
1782 return WINED3DERR_INVALIDCALL
;
1787 HRESULT CDECL
wined3d_device_get_clip_status(const struct wined3d_device
*device
,
1788 struct wined3d_clip_status
*clip_status
)
1790 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
1793 return WINED3DERR_INVALIDCALL
;
1798 void CDECL
wined3d_device_set_material(struct wined3d_device
*device
, const struct wined3d_material
*material
)
1800 TRACE("device %p, material %p.\n", device
, material
);
1802 device
->update_state
->material
= *material
;
1804 if (device
->recording
)
1805 device
->recording
->changed
.material
= TRUE
;
1807 wined3d_cs_emit_set_material(device
->cs
, material
);
1810 void CDECL
wined3d_device_get_material(const struct wined3d_device
*device
, struct wined3d_material
*material
)
1812 TRACE("device %p, material %p.\n", device
, material
);
1814 *material
= device
->state
.material
;
1816 TRACE("diffuse %s\n", debug_color(&material
->diffuse
));
1817 TRACE("ambient %s\n", debug_color(&material
->ambient
));
1818 TRACE("specular %s\n", debug_color(&material
->specular
));
1819 TRACE("emissive %s\n", debug_color(&material
->emissive
));
1820 TRACE("power %.8e.\n", material
->power
);
1823 void CDECL
wined3d_device_set_index_buffer(struct wined3d_device
*device
,
1824 struct wined3d_buffer
*buffer
, enum wined3d_format_id format_id
, unsigned int offset
)
1826 enum wined3d_format_id prev_format
;
1827 struct wined3d_buffer
*prev_buffer
;
1828 unsigned int prev_offset
;
1830 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1831 device
, buffer
, debug_d3dformat(format_id
), offset
);
1833 prev_buffer
= device
->update_state
->index_buffer
;
1834 prev_format
= device
->update_state
->index_format
;
1835 prev_offset
= device
->update_state
->index_offset
;
1837 device
->update_state
->index_buffer
= buffer
;
1838 device
->update_state
->index_format
= format_id
;
1839 device
->update_state
->index_offset
= offset
;
1841 if (device
->recording
)
1842 device
->recording
->changed
.indices
= TRUE
;
1844 if (prev_buffer
== buffer
&& prev_format
== format_id
&& prev_offset
== offset
)
1848 wined3d_buffer_incref(buffer
);
1849 if (!device
->recording
)
1850 wined3d_cs_emit_set_index_buffer(device
->cs
, buffer
, format_id
, offset
);
1852 wined3d_buffer_decref(prev_buffer
);
1855 struct wined3d_buffer
* CDECL
wined3d_device_get_index_buffer(const struct wined3d_device
*device
,
1856 enum wined3d_format_id
*format
, unsigned int *offset
)
1858 TRACE("device %p, format %p, offset %p.\n", device
, format
, offset
);
1860 *format
= device
->state
.index_format
;
1862 *offset
= device
->state
.index_offset
;
1863 return device
->state
.index_buffer
;
1866 void CDECL
wined3d_device_set_base_vertex_index(struct wined3d_device
*device
, INT base_index
)
1868 TRACE("device %p, base_index %d.\n", device
, base_index
);
1870 device
->update_state
->base_vertex_index
= base_index
;
1873 INT CDECL
wined3d_device_get_base_vertex_index(const struct wined3d_device
*device
)
1875 TRACE("device %p.\n", device
);
1877 return device
->state
.base_vertex_index
;
1880 void CDECL
wined3d_device_set_viewport(struct wined3d_device
*device
, const struct wined3d_viewport
*viewport
)
1882 TRACE("device %p, viewport %p.\n", device
, viewport
);
1883 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1884 viewport
->x
, viewport
->y
, viewport
->width
, viewport
->height
, viewport
->min_z
, viewport
->max_z
);
1886 device
->update_state
->viewport
= *viewport
;
1888 /* Handle recording of state blocks */
1889 if (device
->recording
)
1891 TRACE("Recording... not performing anything\n");
1892 device
->recording
->changed
.viewport
= TRUE
;
1896 wined3d_cs_emit_set_viewport(device
->cs
, viewport
);
1899 void CDECL
wined3d_device_get_viewport(const struct wined3d_device
*device
, struct wined3d_viewport
*viewport
)
1901 TRACE("device %p, viewport %p.\n", device
, viewport
);
1903 *viewport
= device
->state
.viewport
;
1906 static void resolve_depth_buffer(struct wined3d_state
*state
)
1908 struct wined3d_texture
*dst_texture
= state
->textures
[0];
1909 struct wined3d_rendertarget_view
*src_view
;
1910 RECT src_rect
, dst_rect
;
1912 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1913 || !(dst_texture
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
))
1916 if (!(src_view
= state
->fb
->depth_stencil
))
1918 if (src_view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
1920 FIXME("Not supported on buffer resources.\n");
1924 SetRect(&dst_rect
, 0, 0, dst_texture
->resource
.width
, dst_texture
->resource
.height
);
1925 SetRect(&src_rect
, 0, 0, src_view
->width
, src_view
->height
);
1926 wined3d_texture_blt(dst_texture
, 0, &dst_rect
, texture_from_resource(src_view
->resource
),
1927 src_view
->sub_resource_idx
, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
1930 void CDECL
wined3d_device_set_rasterizer_state(struct wined3d_device
*device
,
1931 struct wined3d_rasterizer_state
*rasterizer_state
)
1933 struct wined3d_rasterizer_state
*prev
;
1935 TRACE("device %p, rasterizer_state %p.\n", device
, rasterizer_state
);
1937 prev
= device
->update_state
->rasterizer_state
;
1938 if (prev
== rasterizer_state
)
1941 if (rasterizer_state
)
1942 wined3d_rasterizer_state_incref(rasterizer_state
);
1943 device
->update_state
->rasterizer_state
= rasterizer_state
;
1944 wined3d_cs_emit_set_rasterizer_state(device
->cs
, rasterizer_state
);
1946 wined3d_rasterizer_state_decref(prev
);
1949 struct wined3d_rasterizer_state
* CDECL
wined3d_device_get_rasterizer_state(struct wined3d_device
*device
)
1951 TRACE("device %p.\n", device
);
1953 return device
->state
.rasterizer_state
;
1956 void CDECL
wined3d_device_set_render_state(struct wined3d_device
*device
,
1957 enum wined3d_render_state state
, DWORD value
)
1961 TRACE("device %p, state %s (%#x), value %#x.\n", device
, debug_d3drenderstate(state
), state
, value
);
1963 if (state
> WINEHIGHEST_RENDER_STATE
)
1965 WARN("Unhandled render state %#x.\n", state
);
1969 old_value
= device
->state
.render_states
[state
];
1970 device
->update_state
->render_states
[state
] = value
;
1972 /* Handle recording of state blocks. */
1973 if (device
->recording
)
1975 TRACE("Recording... not performing anything.\n");
1976 device
->recording
->changed
.renderState
[state
>> 5] |= 1u << (state
& 0x1f);
1980 /* Compared here and not before the assignment to allow proper stateblock recording. */
1981 if (value
== old_value
)
1982 TRACE("Application is setting the old value over, nothing to do.\n");
1984 wined3d_cs_emit_set_render_state(device
->cs
, state
, value
);
1986 if (state
== WINED3D_RS_POINTSIZE
&& value
== WINED3D_RESZ_CODE
)
1988 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1989 resolve_depth_buffer(&device
->state
);
1993 DWORD CDECL
wined3d_device_get_render_state(const struct wined3d_device
*device
, enum wined3d_render_state state
)
1995 TRACE("device %p, state %s (%#x).\n", device
, debug_d3drenderstate(state
), state
);
1997 return device
->state
.render_states
[state
];
2000 void CDECL
wined3d_device_set_sampler_state(struct wined3d_device
*device
,
2001 UINT sampler_idx
, enum wined3d_sampler_state state
, DWORD value
)
2005 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2006 device
, sampler_idx
, debug_d3dsamplerstate(state
), value
);
2008 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2009 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2011 if (sampler_idx
>= sizeof(device
->state
.sampler_states
) / sizeof(*device
->state
.sampler_states
))
2013 WARN("Invalid sampler %u.\n", sampler_idx
);
2014 return; /* Windows accepts overflowing this array ... we do not. */
2017 old_value
= device
->state
.sampler_states
[sampler_idx
][state
];
2018 device
->update_state
->sampler_states
[sampler_idx
][state
] = value
;
2020 /* Handle recording of state blocks. */
2021 if (device
->recording
)
2023 TRACE("Recording... not performing anything.\n");
2024 device
->recording
->changed
.samplerState
[sampler_idx
] |= 1u << state
;
2028 if (old_value
== value
)
2030 TRACE("Application is setting the old value over, nothing to do.\n");
2034 wined3d_cs_emit_set_sampler_state(device
->cs
, sampler_idx
, state
, value
);
2037 DWORD CDECL
wined3d_device_get_sampler_state(const struct wined3d_device
*device
,
2038 UINT sampler_idx
, enum wined3d_sampler_state state
)
2040 TRACE("device %p, sampler_idx %u, state %s.\n",
2041 device
, sampler_idx
, debug_d3dsamplerstate(state
));
2043 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2044 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2046 if (sampler_idx
>= sizeof(device
->state
.sampler_states
) / sizeof(*device
->state
.sampler_states
))
2048 WARN("Invalid sampler %u.\n", sampler_idx
);
2049 return 0; /* Windows accepts overflowing this array ... we do not. */
2052 return device
->state
.sampler_states
[sampler_idx
][state
];
2055 void CDECL
wined3d_device_set_scissor_rect(struct wined3d_device
*device
, const RECT
*rect
)
2057 TRACE("device %p, rect %s.\n", device
, wine_dbgstr_rect(rect
));
2059 if (device
->recording
)
2060 device
->recording
->changed
.scissorRect
= TRUE
;
2062 if (EqualRect(&device
->update_state
->scissor_rect
, rect
))
2064 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2067 CopyRect(&device
->update_state
->scissor_rect
, rect
);
2069 if (device
->recording
)
2071 TRACE("Recording... not performing anything.\n");
2075 wined3d_cs_emit_set_scissor_rect(device
->cs
, rect
);
2078 void CDECL
wined3d_device_get_scissor_rect(const struct wined3d_device
*device
, RECT
*rect
)
2080 TRACE("device %p, rect %p.\n", device
, rect
);
2082 *rect
= device
->state
.scissor_rect
;
2083 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect
));
2086 void CDECL
wined3d_device_set_vertex_declaration(struct wined3d_device
*device
,
2087 struct wined3d_vertex_declaration
*declaration
)
2089 struct wined3d_vertex_declaration
*prev
= device
->update_state
->vertex_declaration
;
2091 TRACE("device %p, declaration %p.\n", device
, declaration
);
2093 if (device
->recording
)
2094 device
->recording
->changed
.vertexDecl
= TRUE
;
2096 if (declaration
== prev
)
2100 wined3d_vertex_declaration_incref(declaration
);
2101 device
->update_state
->vertex_declaration
= declaration
;
2102 if (!device
->recording
)
2103 wined3d_cs_emit_set_vertex_declaration(device
->cs
, declaration
);
2105 wined3d_vertex_declaration_decref(prev
);
2108 struct wined3d_vertex_declaration
* CDECL
wined3d_device_get_vertex_declaration(const struct wined3d_device
*device
)
2110 TRACE("device %p.\n", device
);
2112 return device
->state
.vertex_declaration
;
2115 void CDECL
wined3d_device_set_vertex_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2117 struct wined3d_shader
*prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
2119 TRACE("device %p, shader %p.\n", device
, shader
);
2121 if (device
->recording
)
2122 device
->recording
->changed
.vertexShader
= TRUE
;
2128 wined3d_shader_incref(shader
);
2129 device
->update_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = shader
;
2130 if (!device
->recording
)
2131 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_VERTEX
, shader
);
2133 wined3d_shader_decref(prev
);
2136 struct wined3d_shader
* CDECL
wined3d_device_get_vertex_shader(const struct wined3d_device
*device
)
2138 TRACE("device %p.\n", device
);
2140 return device
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
];
2143 static void wined3d_device_set_constant_buffer(struct wined3d_device
*device
,
2144 enum wined3d_shader_type type
, UINT idx
, struct wined3d_buffer
*buffer
)
2146 struct wined3d_buffer
*prev
;
2148 if (idx
>= MAX_CONSTANT_BUFFERS
)
2150 WARN("Invalid constant buffer index %u.\n", idx
);
2154 prev
= device
->update_state
->cb
[type
][idx
];
2159 wined3d_buffer_incref(buffer
);
2160 device
->update_state
->cb
[type
][idx
] = buffer
;
2161 if (!device
->recording
)
2162 wined3d_cs_emit_set_constant_buffer(device
->cs
, type
, idx
, buffer
);
2164 wined3d_buffer_decref(prev
);
2167 void CDECL
wined3d_device_set_vs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2169 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2171 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, buffer
);
2174 static struct wined3d_buffer
*wined3d_device_get_constant_buffer(const struct wined3d_device
*device
,
2175 enum wined3d_shader_type shader_type
, unsigned int idx
)
2177 if (idx
>= MAX_CONSTANT_BUFFERS
)
2179 WARN("Invalid constant buffer index %u.\n", idx
);
2183 return device
->state
.cb
[shader_type
][idx
];
2186 struct wined3d_buffer
* CDECL
wined3d_device_get_vs_cb(const struct wined3d_device
*device
, UINT idx
)
2188 TRACE("device %p, idx %u.\n", device
, idx
);
2190 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2193 static void wined3d_device_set_shader_resource_view(struct wined3d_device
*device
,
2194 enum wined3d_shader_type type
, UINT idx
, struct wined3d_shader_resource_view
*view
)
2196 struct wined3d_shader_resource_view
*prev
;
2198 if (idx
>= MAX_SHADER_RESOURCE_VIEWS
)
2200 WARN("Invalid view index %u.\n", idx
);
2204 prev
= device
->update_state
->shader_resource_view
[type
][idx
];
2209 wined3d_shader_resource_view_incref(view
);
2210 device
->update_state
->shader_resource_view
[type
][idx
] = view
;
2211 if (!device
->recording
)
2212 wined3d_cs_emit_set_shader_resource_view(device
->cs
, type
, idx
, view
);
2214 wined3d_shader_resource_view_decref(prev
);
2217 void CDECL
wined3d_device_set_vs_resource_view(struct wined3d_device
*device
,
2218 UINT idx
, struct wined3d_shader_resource_view
*view
)
2220 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2222 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, view
);
2225 static struct wined3d_shader_resource_view
*wined3d_device_get_shader_resource_view(
2226 const struct wined3d_device
*device
, enum wined3d_shader_type shader_type
, unsigned int idx
)
2228 if (idx
>= MAX_SHADER_RESOURCE_VIEWS
)
2230 WARN("Invalid view index %u.\n", idx
);
2234 return device
->state
.shader_resource_view
[shader_type
][idx
];
2237 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_vs_resource_view(const struct wined3d_device
*device
,
2240 TRACE("device %p, idx %u.\n", device
, idx
);
2242 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2245 static void wined3d_device_set_sampler(struct wined3d_device
*device
,
2246 enum wined3d_shader_type type
, UINT idx
, struct wined3d_sampler
*sampler
)
2248 struct wined3d_sampler
*prev
;
2250 if (idx
>= MAX_SAMPLER_OBJECTS
)
2252 WARN("Invalid sampler index %u.\n", idx
);
2256 prev
= device
->update_state
->sampler
[type
][idx
];
2257 if (sampler
== prev
)
2261 wined3d_sampler_incref(sampler
);
2262 device
->update_state
->sampler
[type
][idx
] = sampler
;
2263 if (!device
->recording
)
2264 wined3d_cs_emit_set_sampler(device
->cs
, type
, idx
, sampler
);
2266 wined3d_sampler_decref(prev
);
2269 void CDECL
wined3d_device_set_vs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2271 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2273 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, sampler
);
2276 static struct wined3d_sampler
*wined3d_device_get_sampler(const struct wined3d_device
*device
,
2277 enum wined3d_shader_type shader_type
, unsigned int idx
)
2279 if (idx
>= MAX_SAMPLER_OBJECTS
)
2281 WARN("Invalid sampler index %u.\n", idx
);
2285 return device
->state
.sampler
[shader_type
][idx
];
2288 struct wined3d_sampler
* CDECL
wined3d_device_get_vs_sampler(const struct wined3d_device
*device
, UINT idx
)
2290 TRACE("device %p, idx %u.\n", device
, idx
);
2292 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2295 HRESULT CDECL
wined3d_device_set_vs_consts_b(struct wined3d_device
*device
,
2296 unsigned int start_idx
, unsigned int count
, const BOOL
*constants
)
2300 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2301 device
, start_idx
, count
, constants
);
2303 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2304 return WINED3DERR_INVALIDCALL
;
2306 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2307 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2308 memcpy(&device
->update_state
->vs_consts_b
[start_idx
], constants
, count
* sizeof(*constants
));
2311 for (i
= 0; i
< count
; ++i
)
2312 TRACE("Set BOOL constant %u to %#x.\n", start_idx
+ i
, constants
[i
]);
2315 if (device
->recording
)
2317 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2318 device
->recording
->changed
.vertexShaderConstantsB
|= (1u << i
);
2322 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_B
, start_idx
, count
, constants
);
2328 HRESULT CDECL
wined3d_device_get_vs_consts_b(const struct wined3d_device
*device
,
2329 unsigned int start_idx
, unsigned int count
, BOOL
*constants
)
2331 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2332 device
, start_idx
, count
, constants
);
2334 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2335 return WINED3DERR_INVALIDCALL
;
2337 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2338 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2339 memcpy(constants
, &device
->state
.vs_consts_b
[start_idx
], count
* sizeof(*constants
));
2344 HRESULT CDECL
wined3d_device_set_vs_consts_i(struct wined3d_device
*device
,
2345 unsigned int start_idx
, unsigned int count
, const struct wined3d_ivec4
*constants
)
2349 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2350 device
, start_idx
, count
, constants
);
2352 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2353 return WINED3DERR_INVALIDCALL
;
2355 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2356 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2357 memcpy(&device
->update_state
->vs_consts_i
[start_idx
], constants
, count
* sizeof(*constants
));
2360 for (i
= 0; i
< count
; ++i
)
2361 TRACE("Set ivec4 constant %u to %s.\n", start_idx
+ i
, debug_ivec4(&constants
[i
]));
2364 if (device
->recording
)
2366 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2367 device
->recording
->changed
.vertexShaderConstantsI
|= (1u << i
);
2371 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_I
, start_idx
, count
, constants
);
2377 HRESULT CDECL
wined3d_device_get_vs_consts_i(const struct wined3d_device
*device
,
2378 unsigned int start_idx
, unsigned int count
, struct wined3d_ivec4
*constants
)
2380 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2381 device
, start_idx
, count
, constants
);
2383 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2384 return WINED3DERR_INVALIDCALL
;
2386 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2387 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2388 memcpy(constants
, &device
->state
.vs_consts_i
[start_idx
], count
* sizeof(*constants
));
2392 HRESULT CDECL
wined3d_device_set_vs_consts_f(struct wined3d_device
*device
,
2393 unsigned int start_idx
, unsigned int count
, const struct wined3d_vec4
*constants
)
2395 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2398 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2399 device
, start_idx
, count
, constants
);
2401 if (!constants
|| start_idx
>= d3d_info
->limits
.vs_uniform_count
2402 || count
> d3d_info
->limits
.vs_uniform_count
- start_idx
)
2403 return WINED3DERR_INVALIDCALL
;
2405 memcpy(&device
->update_state
->vs_consts_f
[start_idx
], constants
, count
* sizeof(*constants
));
2408 for (i
= 0; i
< count
; ++i
)
2409 TRACE("Set vec4 constant %u to %s.\n", start_idx
+ i
, debug_vec4(&constants
[i
]));
2412 if (device
->recording
)
2413 memset(&device
->recording
->changed
.vs_consts_f
[start_idx
], 1,
2414 count
* sizeof(*device
->recording
->changed
.vs_consts_f
));
2416 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_F
, start_idx
, count
, constants
);
2421 HRESULT CDECL
wined3d_device_get_vs_consts_f(const struct wined3d_device
*device
,
2422 unsigned int start_idx
, unsigned int count
, struct wined3d_vec4
*constants
)
2424 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2426 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2427 device
, start_idx
, count
, constants
);
2429 if (!constants
|| start_idx
>= d3d_info
->limits
.vs_uniform_count
2430 || count
> d3d_info
->limits
.vs_uniform_count
- start_idx
)
2431 return WINED3DERR_INVALIDCALL
;
2433 memcpy(constants
, &device
->state
.vs_consts_f
[start_idx
], count
* sizeof(*constants
));
2438 void CDECL
wined3d_device_set_pixel_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2440 struct wined3d_shader
*prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2442 TRACE("device %p, shader %p.\n", device
, shader
);
2444 if (device
->recording
)
2445 device
->recording
->changed
.pixelShader
= TRUE
;
2451 wined3d_shader_incref(shader
);
2452 device
->update_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
] = shader
;
2453 if (!device
->recording
)
2454 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_PIXEL
, shader
);
2456 wined3d_shader_decref(prev
);
2459 struct wined3d_shader
* CDECL
wined3d_device_get_pixel_shader(const struct wined3d_device
*device
)
2461 TRACE("device %p.\n", device
);
2463 return device
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
];
2466 void CDECL
wined3d_device_set_ps_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2468 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2470 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, buffer
);
2473 struct wined3d_buffer
* CDECL
wined3d_device_get_ps_cb(const struct wined3d_device
*device
, UINT idx
)
2475 TRACE("device %p, idx %u.\n", device
, idx
);
2477 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2480 void CDECL
wined3d_device_set_ps_resource_view(struct wined3d_device
*device
,
2481 UINT idx
, struct wined3d_shader_resource_view
*view
)
2483 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2485 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, view
);
2488 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_ps_resource_view(const struct wined3d_device
*device
,
2491 TRACE("device %p, idx %u.\n", device
, idx
);
2493 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2496 void CDECL
wined3d_device_set_ps_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2498 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2500 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, sampler
);
2503 struct wined3d_sampler
* CDECL
wined3d_device_get_ps_sampler(const struct wined3d_device
*device
, UINT idx
)
2505 TRACE("device %p, idx %u.\n", device
, idx
);
2507 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2510 HRESULT CDECL
wined3d_device_set_ps_consts_b(struct wined3d_device
*device
,
2511 unsigned int start_idx
, unsigned int count
, const BOOL
*constants
)
2515 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2516 device
, start_idx
, count
, constants
);
2518 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2519 return WINED3DERR_INVALIDCALL
;
2521 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2522 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2523 memcpy(&device
->update_state
->ps_consts_b
[start_idx
], constants
, count
* sizeof(*constants
));
2526 for (i
= 0; i
< count
; ++i
)
2527 TRACE("Set BOOL constant %u to %#x.\n", start_idx
+ i
, constants
[i
]);
2530 if (device
->recording
)
2532 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2533 device
->recording
->changed
.pixelShaderConstantsB
|= (1u << i
);
2537 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_B
, start_idx
, count
, constants
);
2543 HRESULT CDECL
wined3d_device_get_ps_consts_b(const struct wined3d_device
*device
,
2544 unsigned int start_idx
, unsigned int count
, BOOL
*constants
)
2546 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2547 device
, start_idx
, count
, constants
);
2549 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2550 return WINED3DERR_INVALIDCALL
;
2552 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2553 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2554 memcpy(constants
, &device
->state
.ps_consts_b
[start_idx
], count
* sizeof(*constants
));
2559 HRESULT CDECL
wined3d_device_set_ps_consts_i(struct wined3d_device
*device
,
2560 unsigned int start_idx
, unsigned int count
, const struct wined3d_ivec4
*constants
)
2564 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2565 device
, start_idx
, count
, constants
);
2567 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2568 return WINED3DERR_INVALIDCALL
;
2570 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2571 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2572 memcpy(&device
->update_state
->ps_consts_i
[start_idx
], constants
, count
* sizeof(*constants
));
2575 for (i
= 0; i
< count
; ++i
)
2576 TRACE("Set ivec4 constant %u to %s.\n", start_idx
+ i
, debug_ivec4(&constants
[i
]));
2579 if (device
->recording
)
2581 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2582 device
->recording
->changed
.pixelShaderConstantsI
|= (1u << i
);
2586 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_I
, start_idx
, count
, constants
);
2592 HRESULT CDECL
wined3d_device_get_ps_consts_i(const struct wined3d_device
*device
,
2593 unsigned int start_idx
, unsigned int count
, struct wined3d_ivec4
*constants
)
2595 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2596 device
, start_idx
, count
, constants
);
2598 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2599 return WINED3DERR_INVALIDCALL
;
2601 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2602 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2603 memcpy(constants
, &device
->state
.ps_consts_i
[start_idx
], count
* sizeof(*constants
));
2608 HRESULT CDECL
wined3d_device_set_ps_consts_f(struct wined3d_device
*device
,
2609 unsigned int start_idx
, unsigned int count
, const struct wined3d_vec4
*constants
)
2611 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2614 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2615 device
, start_idx
, count
, constants
);
2617 if (!constants
|| start_idx
>= d3d_info
->limits
.ps_uniform_count
2618 || count
> d3d_info
->limits
.ps_uniform_count
- start_idx
)
2619 return WINED3DERR_INVALIDCALL
;
2621 memcpy(&device
->update_state
->ps_consts_f
[start_idx
], constants
, count
* sizeof(*constants
));
2624 for (i
= 0; i
< count
; ++i
)
2625 TRACE("Set vec4 constant %u to %s.\n", start_idx
+ i
, debug_vec4(&constants
[i
]));
2628 if (device
->recording
)
2629 memset(&device
->recording
->changed
.ps_consts_f
[start_idx
], 1,
2630 count
* sizeof(*device
->recording
->changed
.ps_consts_f
));
2632 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_F
, start_idx
, count
, constants
);
2637 HRESULT CDECL
wined3d_device_get_ps_consts_f(const struct wined3d_device
*device
,
2638 unsigned int start_idx
, unsigned int count
, struct wined3d_vec4
*constants
)
2640 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2642 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2643 device
, start_idx
, count
, constants
);
2645 if (!constants
|| start_idx
>= d3d_info
->limits
.ps_uniform_count
2646 || count
> d3d_info
->limits
.ps_uniform_count
- start_idx
)
2647 return WINED3DERR_INVALIDCALL
;
2649 memcpy(constants
, &device
->state
.ps_consts_f
[start_idx
], count
* sizeof(*constants
));
2654 void CDECL
wined3d_device_set_geometry_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2656 struct wined3d_shader
*prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2658 TRACE("device %p, shader %p.\n", device
, shader
);
2660 if (device
->recording
|| shader
== prev
)
2663 wined3d_shader_incref(shader
);
2664 device
->update_state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
] = shader
;
2665 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_GEOMETRY
, shader
);
2667 wined3d_shader_decref(prev
);
2670 struct wined3d_shader
* CDECL
wined3d_device_get_geometry_shader(const struct wined3d_device
*device
)
2672 TRACE("device %p.\n", device
);
2674 return device
->state
.shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2677 void CDECL
wined3d_device_set_gs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2679 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2681 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, buffer
);
2684 struct wined3d_buffer
* CDECL
wined3d_device_get_gs_cb(const struct wined3d_device
*device
, UINT idx
)
2686 TRACE("device %p, idx %u.\n", device
, idx
);
2688 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2691 void CDECL
wined3d_device_set_gs_resource_view(struct wined3d_device
*device
,
2692 UINT idx
, struct wined3d_shader_resource_view
*view
)
2694 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2696 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, view
);
2699 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_gs_resource_view(const struct wined3d_device
*device
,
2702 TRACE("device %p, idx %u.\n", device
, idx
);
2704 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2707 void CDECL
wined3d_device_set_gs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2709 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2711 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, sampler
);
2714 struct wined3d_sampler
* CDECL
wined3d_device_get_gs_sampler(const struct wined3d_device
*device
, UINT idx
)
2716 TRACE("device %p, idx %u.\n", device
, idx
);
2718 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2721 void CDECL
wined3d_device_set_compute_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2723 struct wined3d_shader
*prev
;
2725 TRACE("device %p, shader %p.\n", device
, shader
);
2727 prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
];
2728 if (device
->recording
|| shader
== prev
)
2731 wined3d_shader_incref(shader
);
2732 device
->update_state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
] = shader
;
2733 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_COMPUTE
, shader
);
2735 wined3d_shader_decref(prev
);
2738 struct wined3d_shader
* CDECL
wined3d_device_get_compute_shader(const struct wined3d_device
*device
)
2740 TRACE("device %p.\n", device
);
2742 return device
->state
.shader
[WINED3D_SHADER_TYPE_COMPUTE
];
2745 void CDECL
wined3d_device_set_cs_cb(struct wined3d_device
*device
, unsigned int idx
, struct wined3d_buffer
*buffer
)
2747 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2749 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, buffer
);
2752 struct wined3d_buffer
* CDECL
wined3d_device_get_cs_cb(const struct wined3d_device
*device
, unsigned int idx
)
2754 TRACE("device %p, idx %u.\n", device
, idx
);
2756 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2759 void CDECL
wined3d_device_set_cs_resource_view(struct wined3d_device
*device
,
2760 unsigned int idx
, struct wined3d_shader_resource_view
*view
)
2762 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2764 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, view
);
2767 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_cs_resource_view(const struct wined3d_device
*device
,
2770 TRACE("device %p, idx %u.\n", device
, idx
);
2772 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2775 void CDECL
wined3d_device_set_cs_sampler(struct wined3d_device
*device
,
2776 unsigned int idx
, struct wined3d_sampler
*sampler
)
2778 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2780 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, sampler
);
2783 struct wined3d_sampler
* CDECL
wined3d_device_get_cs_sampler(const struct wined3d_device
*device
, unsigned int idx
)
2785 TRACE("device %p, idx %u.\n", device
, idx
);
2787 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2790 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device
*device
,
2791 enum wined3d_pipeline pipeline
, unsigned int idx
, struct wined3d_unordered_access_view
*uav
)
2793 struct wined3d_unordered_access_view
*prev
;
2795 if (idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
2797 WARN("Invalid UAV index %u.\n", idx
);
2801 prev
= device
->update_state
->unordered_access_view
[pipeline
][idx
];
2806 wined3d_unordered_access_view_incref(uav
);
2807 device
->update_state
->unordered_access_view
[pipeline
][idx
] = uav
;
2808 if (!device
->recording
)
2809 wined3d_cs_emit_set_unordered_access_view(device
->cs
, pipeline
, idx
, uav
);
2811 wined3d_unordered_access_view_decref(prev
);
2814 void CDECL
wined3d_device_set_cs_uav(struct wined3d_device
*device
, unsigned int idx
,
2815 struct wined3d_unordered_access_view
*uav
)
2817 TRACE("device %p, idx %u, uav %p.\n", device
, idx
, uav
);
2819 wined3d_device_set_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_COMPUTE
, idx
, uav
);
2822 void CDECL
wined3d_device_set_unordered_access_view(struct wined3d_device
*device
,
2823 unsigned int idx
, struct wined3d_unordered_access_view
*uav
)
2825 TRACE("device %p, idx %u, uav %p.\n", device
, idx
, uav
);
2827 wined3d_device_set_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_GRAPHICS
, idx
, uav
);
2830 /* Context activation is done by the caller. */
2831 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2832 static HRESULT
process_vertices_strided(const struct wined3d_device
*device
, DWORD dwDestIndex
, DWORD dwCount
,
2833 const struct wined3d_stream_info
*stream_info
, struct wined3d_buffer
*dest
, DWORD flags
,
2836 struct wined3d_matrix mat
, proj_mat
, view_mat
, world_mat
;
2837 struct wined3d_map_desc map_desc
;
2838 struct wined3d_box box
= {0};
2839 struct wined3d_viewport vp
;
2847 if (stream_info
->use_map
& (1u << WINED3D_FFP_NORMAL
))
2849 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2852 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_POSITION
)))
2854 ERR("Source has no position mask\n");
2855 return WINED3DERR_INVALIDCALL
;
2858 if (device
->state
.render_states
[WINED3D_RS_CLIPPING
])
2860 static BOOL warned
= FALSE
;
2862 * The clipping code is not quite correct. Some things need
2863 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2864 * so disable clipping for now.
2865 * (The graphics in Half-Life are broken, and my processvertices
2866 * test crashes with IDirect3DDevice3)
2872 FIXME("Clipping is broken and disabled for now\n");
2878 vertex_size
= get_flexible_vertex_size(DestFVF
);
2879 box
.left
= dwDestIndex
* vertex_size
;
2880 box
.right
= box
.left
+ dwCount
* vertex_size
;
2881 if (FAILED(hr
= wined3d_resource_map(&dest
->resource
, 0, &map_desc
, &box
, 0)))
2883 WARN("Failed to map buffer, hr %#x.\n", hr
);
2886 dest_ptr
= map_desc
.data
;
2888 wined3d_device_get_transform(device
, WINED3D_TS_VIEW
, &view_mat
);
2889 wined3d_device_get_transform(device
, WINED3D_TS_PROJECTION
, &proj_mat
);
2890 wined3d_device_get_transform(device
, WINED3D_TS_WORLD_MATRIX(0), &world_mat
);
2892 TRACE("View mat:\n");
2893 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._11
, view_mat
._12
, view_mat
._13
, view_mat
._14
);
2894 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._21
, view_mat
._22
, view_mat
._23
, view_mat
._24
);
2895 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._31
, view_mat
._32
, view_mat
._33
, view_mat
._34
);
2896 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._41
, view_mat
._42
, view_mat
._43
, view_mat
._44
);
2898 TRACE("Proj mat:\n");
2899 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._11
, proj_mat
._12
, proj_mat
._13
, proj_mat
._14
);
2900 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._21
, proj_mat
._22
, proj_mat
._23
, proj_mat
._24
);
2901 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._31
, proj_mat
._32
, proj_mat
._33
, proj_mat
._34
);
2902 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._41
, proj_mat
._42
, proj_mat
._43
, proj_mat
._44
);
2904 TRACE("World mat:\n");
2905 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._11
, world_mat
._12
, world_mat
._13
, world_mat
._14
);
2906 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._21
, world_mat
._22
, world_mat
._23
, world_mat
._24
);
2907 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._31
, world_mat
._32
, world_mat
._33
, world_mat
._34
);
2908 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._41
, world_mat
._42
, world_mat
._43
, world_mat
._44
);
2910 /* Get the viewport */
2911 wined3d_device_get_viewport(device
, &vp
);
2912 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2913 vp
.x
, vp
.y
, vp
.width
, vp
.height
, vp
.min_z
, vp
.max_z
);
2915 multiply_matrix(&mat
,&view_mat
,&world_mat
);
2916 multiply_matrix(&mat
,&proj_mat
,&mat
);
2918 numTextures
= (DestFVF
& WINED3DFVF_TEXCOUNT_MASK
) >> WINED3DFVF_TEXCOUNT_SHIFT
;
2920 for (i
= 0; i
< dwCount
; i
+= 1) {
2921 unsigned int tex_index
;
2923 if ( ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZ
) ||
2924 ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
) ) {
2925 /* The position first */
2926 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_POSITION
];
2927 const float *p
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
2929 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p
[0], p
[1], p
[2]);
2931 /* Multiplication with world, view and projection matrix. */
2932 x
= (p
[0] * mat
._11
) + (p
[1] * mat
._21
) + (p
[2] * mat
._31
) + mat
._41
;
2933 y
= (p
[0] * mat
._12
) + (p
[1] * mat
._22
) + (p
[2] * mat
._32
) + mat
._42
;
2934 z
= (p
[0] * mat
._13
) + (p
[1] * mat
._23
) + (p
[2] * mat
._33
) + mat
._43
;
2935 rhw
= (p
[0] * mat
._14
) + (p
[1] * mat
._24
) + (p
[2] * mat
._34
) + mat
._44
;
2937 TRACE("x=%f y=%f z=%f rhw=%f\n", x
, y
, z
, rhw
);
2939 /* WARNING: The following things are taken from d3d7 and were not yet checked
2940 * against d3d8 or d3d9!
2943 /* Clipping conditions: From msdn
2945 * A vertex is clipped if it does not match the following requirements
2949 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2951 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2952 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2957 ( (-rhw
-eps
< x
) && (-rhw
-eps
< y
) && ( -eps
< z
) &&
2958 (x
<= rhw
+ eps
) && (y
<= rhw
+ eps
) && (z
<= rhw
+ eps
) &&
2961 /* "Normal" viewport transformation (not clipped)
2962 * 1) The values are divided by rhw
2963 * 2) The y axis is negative, so multiply it with -1
2964 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2965 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2966 * 4) Multiply x with Width/2 and add Width/2
2967 * 5) The same for the height
2968 * 6) Add the viewpoint X and Y to the 2D coordinates and
2969 * The minimum Z value to z
2970 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2972 * Well, basically it's simply a linear transformation into viewport
2984 z
*= vp
.max_z
- vp
.min_z
;
2986 x
+= vp
.width
/ 2 + vp
.x
;
2987 y
+= vp
.height
/ 2 + vp
.y
;
2992 /* That vertex got clipped
2993 * Contrary to OpenGL it is not dropped completely, it just
2994 * undergoes a different calculation.
2996 TRACE("Vertex got clipped\n");
3003 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3004 * outside of the main vertex buffer memory. That needs some more
3009 TRACE("Writing (%f %f %f) %f\n", x
, y
, z
, rhw
);
3012 ( (float *) dest_ptr
)[0] = x
;
3013 ( (float *) dest_ptr
)[1] = y
;
3014 ( (float *) dest_ptr
)[2] = z
;
3015 ( (float *) dest_ptr
)[3] = rhw
; /* SIC, see ddraw test! */
3017 dest_ptr
+= 3 * sizeof(float);
3019 if ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
)
3020 dest_ptr
+= sizeof(float);
3023 if (DestFVF
& WINED3DFVF_PSIZE
)
3024 dest_ptr
+= sizeof(DWORD
);
3026 if (DestFVF
& WINED3DFVF_NORMAL
)
3028 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_NORMAL
];
3029 const float *normal
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3030 /* AFAIK this should go into the lighting information */
3031 FIXME("Didn't expect the destination to have a normal\n");
3032 copy_and_next(dest_ptr
, normal
, 3 * sizeof(float));
3035 if (DestFVF
& WINED3DFVF_DIFFUSE
)
3037 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_DIFFUSE
];
3038 const DWORD
*color_d
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3039 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_DIFFUSE
)))
3041 static BOOL warned
= FALSE
;
3044 ERR("No diffuse color in source, but destination has one\n");
3048 *( (DWORD
*) dest_ptr
) = 0xffffffff;
3049 dest_ptr
+= sizeof(DWORD
);
3053 copy_and_next(dest_ptr
, color_d
, sizeof(DWORD
));
3057 if (DestFVF
& WINED3DFVF_SPECULAR
)
3059 /* What's the color value in the feedback buffer? */
3060 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_SPECULAR
];
3061 const DWORD
*color_s
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3062 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_SPECULAR
)))
3064 static BOOL warned
= FALSE
;
3067 ERR("No specular color in source, but destination has one\n");
3071 *(DWORD
*)dest_ptr
= 0xff000000;
3072 dest_ptr
+= sizeof(DWORD
);
3076 copy_and_next(dest_ptr
, color_s
, sizeof(DWORD
));
3080 for (tex_index
= 0; tex_index
< numTextures
; ++tex_index
)
3082 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_TEXCOORD0
+ tex_index
];
3083 const float *tex_coord
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3084 if (!(stream_info
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ tex_index
))))
3086 ERR("No source texture, but destination requests one\n");
3087 dest_ptr
+= GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float);
3091 copy_and_next(dest_ptr
, tex_coord
, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float));
3096 wined3d_resource_unmap(&dest
->resource
, 0);
3100 #undef copy_and_next
3102 HRESULT CDECL
wined3d_device_process_vertices(struct wined3d_device
*device
,
3103 UINT src_start_idx
, UINT dst_idx
, UINT vertex_count
, struct wined3d_buffer
*dst_buffer
,
3104 const struct wined3d_vertex_declaration
*declaration
, DWORD flags
, DWORD dst_fvf
)
3106 struct wined3d_state
*state
= &device
->state
;
3107 struct wined3d_stream_info stream_info
;
3108 struct wined3d_resource
*resource
;
3109 struct wined3d_box box
= {0};
3110 struct wined3d_shader
*vs
;
3115 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3116 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3117 device
, src_start_idx
, dst_idx
, vertex_count
,
3118 dst_buffer
, declaration
, flags
, dst_fvf
);
3121 FIXME("Output vertex declaration not implemented yet.\n");
3123 vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3124 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = NULL
;
3125 wined3d_stream_info_from_declaration(&stream_info
, state
, &device
->adapter
->gl_info
, &device
->adapter
->d3d_info
);
3126 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = vs
;
3128 /* We can't convert FROM a VBO, and vertex buffers used to source into
3129 * process_vertices() are unlikely to ever be used for drawing. Release
3130 * VBOs in those buffers and fix up the stream_info structure.
3132 * Also apply the start index. */
3133 for (i
= 0, map
= stream_info
.use_map
; map
; map
>>= 1, ++i
)
3135 struct wined3d_stream_info_element
*e
;
3136 struct wined3d_map_desc map_desc
;
3141 e
= &stream_info
.elements
[i
];
3142 resource
= &state
->streams
[e
->stream_idx
].buffer
->resource
;
3143 box
.left
= src_start_idx
* e
->stride
;
3144 box
.right
= box
.left
+ vertex_count
* e
->stride
;
3145 if (FAILED(wined3d_resource_map(resource
, 0, &map_desc
, &box
, WINED3D_MAP_READONLY
)))
3146 ERR("Failed to map resource.\n");
3147 e
->data
.buffer_object
= 0;
3148 e
->data
.addr
+= (ULONG_PTR
)map_desc
.data
;
3151 hr
= process_vertices_strided(device
, dst_idx
, vertex_count
,
3152 &stream_info
, dst_buffer
, flags
, dst_fvf
);
3154 for (i
= 0, map
= stream_info
.use_map
; map
; map
>>= 1, ++i
)
3159 resource
= &state
->streams
[stream_info
.elements
[i
].stream_idx
].buffer
->resource
;
3160 if (FAILED(wined3d_resource_unmap(resource
, 0)))
3161 ERR("Failed to unmap resource.\n");
3167 void CDECL
wined3d_device_set_texture_stage_state(struct wined3d_device
*device
,
3168 UINT stage
, enum wined3d_texture_stage_state state
, DWORD value
)
3170 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3173 TRACE("device %p, stage %u, state %s, value %#x.\n",
3174 device
, stage
, debug_d3dtexturestate(state
), value
);
3176 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3178 WARN("Invalid state %#x passed.\n", state
);
3182 if (stage
>= d3d_info
->limits
.ffp_blend_stages
)
3184 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3185 stage
, d3d_info
->limits
.ffp_blend_stages
- 1);
3189 old_value
= device
->update_state
->texture_states
[stage
][state
];
3190 device
->update_state
->texture_states
[stage
][state
] = value
;
3192 if (device
->recording
)
3194 TRACE("Recording... not performing anything.\n");
3195 device
->recording
->changed
.textureState
[stage
] |= 1u << state
;
3199 /* Checked after the assignments to allow proper stateblock recording. */
3200 if (old_value
== value
)
3202 TRACE("Application is setting the old value over, nothing to do.\n");
3206 wined3d_cs_emit_set_texture_state(device
->cs
, stage
, state
, value
);
3209 DWORD CDECL
wined3d_device_get_texture_stage_state(const struct wined3d_device
*device
,
3210 UINT stage
, enum wined3d_texture_stage_state state
)
3212 TRACE("device %p, stage %u, state %s.\n",
3213 device
, stage
, debug_d3dtexturestate(state
));
3215 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3217 WARN("Invalid state %#x passed.\n", state
);
3221 return device
->state
.texture_states
[stage
][state
];
3224 HRESULT CDECL
wined3d_device_set_texture(struct wined3d_device
*device
,
3225 UINT stage
, struct wined3d_texture
*texture
)
3227 struct wined3d_texture
*prev
;
3229 TRACE("device %p, stage %u, texture %p.\n", device
, stage
, texture
);
3231 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3232 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3234 /* Windows accepts overflowing this array... we do not. */
3235 if (stage
>= sizeof(device
->state
.textures
) / sizeof(*device
->state
.textures
))
3237 WARN("Ignoring invalid stage %u.\n", stage
);
3241 if (texture
&& texture
->resource
.pool
== WINED3D_POOL_SCRATCH
)
3243 WARN("Rejecting attempt to set scratch texture.\n");
3244 return WINED3DERR_INVALIDCALL
;
3247 if (device
->recording
)
3248 device
->recording
->changed
.textures
|= 1u << stage
;
3250 prev
= device
->update_state
->textures
[stage
];
3251 TRACE("Previous texture %p.\n", prev
);
3253 if (texture
== prev
)
3255 TRACE("App is setting the same texture again, nothing to do.\n");
3259 TRACE("Setting new texture to %p.\n", texture
);
3260 device
->update_state
->textures
[stage
] = texture
;
3263 wined3d_texture_incref(texture
);
3264 if (!device
->recording
)
3265 wined3d_cs_emit_set_texture(device
->cs
, stage
, texture
);
3267 wined3d_texture_decref(prev
);
3272 struct wined3d_texture
* CDECL
wined3d_device_get_texture(const struct wined3d_device
*device
, UINT stage
)
3274 TRACE("device %p, stage %u.\n", device
, stage
);
3276 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3277 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3279 if (stage
>= sizeof(device
->state
.textures
) / sizeof(*device
->state
.textures
))
3281 WARN("Ignoring invalid stage %u.\n", stage
);
3282 return NULL
; /* Windows accepts overflowing this array ... we do not. */
3285 return device
->state
.textures
[stage
];
3288 HRESULT CDECL
wined3d_device_get_device_caps(const struct wined3d_device
*device
, WINED3DCAPS
*caps
)
3290 TRACE("device %p, caps %p.\n", device
, caps
);
3292 return wined3d_get_device_caps(device
->wined3d
, device
->adapter
->ordinal
,
3293 device
->create_parms
.device_type
, caps
);
3296 HRESULT CDECL
wined3d_device_get_display_mode(const struct wined3d_device
*device
, UINT swapchain_idx
,
3297 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
3299 struct wined3d_swapchain
*swapchain
;
3301 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3302 device
, swapchain_idx
, mode
, rotation
);
3304 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3305 return WINED3DERR_INVALIDCALL
;
3307 return wined3d_swapchain_get_display_mode(swapchain
, mode
, rotation
);
3310 HRESULT CDECL
wined3d_device_begin_stateblock(struct wined3d_device
*device
)
3312 struct wined3d_stateblock
*stateblock
;
3315 TRACE("device %p.\n", device
);
3317 if (device
->recording
)
3318 return WINED3DERR_INVALIDCALL
;
3320 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_RECORDED
, &stateblock
);
3324 device
->recording
= stateblock
;
3325 device
->update_state
= &stateblock
->state
;
3327 TRACE("Recording stateblock %p.\n", stateblock
);
3332 HRESULT CDECL
wined3d_device_end_stateblock(struct wined3d_device
*device
,
3333 struct wined3d_stateblock
**stateblock
)
3335 struct wined3d_stateblock
*object
= device
->recording
;
3337 TRACE("device %p, stateblock %p.\n", device
, stateblock
);
3339 if (!device
->recording
)
3341 WARN("Not recording.\n");
3343 return WINED3DERR_INVALIDCALL
;
3346 stateblock_init_contained_states(object
);
3348 *stateblock
= object
;
3349 device
->recording
= NULL
;
3350 device
->update_state
= &device
->state
;
3352 TRACE("Returning stateblock %p.\n", *stateblock
);
3357 HRESULT CDECL
wined3d_device_begin_scene(struct wined3d_device
*device
)
3359 /* At the moment we have no need for any functionality at the beginning
3361 TRACE("device %p.\n", device
);
3363 if (device
->inScene
)
3365 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3366 return WINED3DERR_INVALIDCALL
;
3368 device
->inScene
= TRUE
;
3372 HRESULT CDECL
wined3d_device_end_scene(struct wined3d_device
*device
)
3374 struct wined3d_context
*context
;
3376 TRACE("device %p.\n", device
);
3378 if (!device
->inScene
)
3380 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3381 return WINED3DERR_INVALIDCALL
;
3384 context
= context_acquire(device
, NULL
, 0);
3385 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3386 context
->gl_info
->gl_ops
.gl
.p_glFlush();
3387 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3389 context_release(context
);
3391 device
->inScene
= FALSE
;
3395 HRESULT CDECL
wined3d_device_clear(struct wined3d_device
*device
, DWORD rect_count
,
3396 const RECT
*rects
, DWORD flags
, const struct wined3d_color
*color
, float depth
, DWORD stencil
)
3398 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3399 device
, rect_count
, rects
, flags
, debug_color(color
), depth
, stencil
);
3401 if (!rect_count
&& rects
)
3403 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects
);
3407 if (flags
& (WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
))
3409 struct wined3d_rendertarget_view
*ds
= device
->fb
.depth_stencil
;
3412 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3413 /* TODO: What about depth stencil buffers without stencil bits? */
3414 return WINED3DERR_INVALIDCALL
;
3416 else if (flags
& WINED3DCLEAR_TARGET
)
3418 if (ds
->width
< device
->fb
.render_targets
[0]->width
3419 || ds
->height
< device
->fb
.render_targets
[0]->height
)
3421 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3427 wined3d_cs_emit_clear(device
->cs
, rect_count
, rects
, flags
, color
, depth
, stencil
);
3432 void CDECL
wined3d_device_set_predication(struct wined3d_device
*device
,
3433 struct wined3d_query
*predicate
, BOOL value
)
3435 struct wined3d_query
*prev
;
3437 TRACE("device %p, predicate %p, value %#x.\n", device
, predicate
, value
);
3439 prev
= device
->update_state
->predicate
;
3442 FIXME("Predicated rendering not implemented.\n");
3443 wined3d_query_incref(predicate
);
3445 device
->update_state
->predicate
= predicate
;
3446 device
->update_state
->predicate_value
= value
;
3447 if (!device
->recording
)
3448 wined3d_cs_emit_set_predication(device
->cs
, predicate
, value
);
3450 wined3d_query_decref(prev
);
3453 struct wined3d_query
* CDECL
wined3d_device_get_predication(struct wined3d_device
*device
, BOOL
*value
)
3455 TRACE("device %p, value %p.\n", device
, value
);
3457 *value
= device
->state
.predicate_value
;
3458 return device
->state
.predicate
;
3461 void CDECL
wined3d_device_dispatch_compute(struct wined3d_device
*device
,
3462 unsigned int group_count_x
, unsigned int group_count_y
, unsigned int group_count_z
)
3464 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3465 device
, group_count_x
, group_count_y
, group_count_z
);
3467 wined3d_cs_emit_dispatch(device
->cs
, group_count_x
, group_count_y
, group_count_z
);
3470 void CDECL
wined3d_device_set_primitive_type(struct wined3d_device
*device
,
3471 enum wined3d_primitive_type primitive_type
)
3473 TRACE("device %p, primitive_type %s\n", device
, debug_d3dprimitivetype(primitive_type
));
3475 device
->state
.gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
3478 void CDECL
wined3d_device_get_primitive_type(const struct wined3d_device
*device
,
3479 enum wined3d_primitive_type
*primitive_type
)
3481 TRACE("device %p, primitive_type %p\n", device
, primitive_type
);
3483 *primitive_type
= d3d_primitive_type_from_gl(device
->state
.gl_primitive_type
);
3485 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type
));
3488 HRESULT CDECL
wined3d_device_draw_primitive(struct wined3d_device
*device
, UINT start_vertex
, UINT vertex_count
)
3490 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device
, start_vertex
, vertex_count
);
3492 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
, 0,
3493 start_vertex
, vertex_count
, 0, 0, FALSE
);
3498 void CDECL
wined3d_device_draw_primitive_instanced(struct wined3d_device
*device
,
3499 UINT start_vertex
, UINT vertex_count
, UINT start_instance
, UINT instance_count
)
3501 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3502 device
, start_vertex
, vertex_count
, start_instance
, instance_count
);
3504 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
, 0,
3505 start_vertex
, vertex_count
, start_instance
, instance_count
, FALSE
);
3508 HRESULT CDECL
wined3d_device_draw_indexed_primitive(struct wined3d_device
*device
, UINT start_idx
, UINT index_count
)
3510 TRACE("device %p, start_idx %u, index_count %u.\n", device
, start_idx
, index_count
);
3512 if (!device
->state
.index_buffer
)
3514 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3515 * without an index buffer set. (The first time at least...)
3516 * D3D8 simply dies, but I doubt it can do much harm to return
3517 * D3DERR_INVALIDCALL there as well. */
3518 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3519 return WINED3DERR_INVALIDCALL
;
3522 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
,
3523 device
->state
.base_vertex_index
, start_idx
, index_count
, 0, 0, TRUE
);
3528 void CDECL
wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device
*device
,
3529 UINT start_idx
, UINT index_count
, UINT start_instance
, UINT instance_count
)
3531 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3532 device
, start_idx
, index_count
, start_instance
, instance_count
);
3534 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
, device
->state
.base_vertex_index
,
3535 start_idx
, index_count
, start_instance
, instance_count
, TRUE
);
3538 static HRESULT
wined3d_device_update_texture_3d(struct wined3d_device
*device
,
3539 struct wined3d_texture
*src_texture
, unsigned int src_level
,
3540 struct wined3d_texture
*dst_texture
, unsigned int level_count
)
3542 struct wined3d_const_bo_address data
;
3543 struct wined3d_context
*context
;
3544 struct wined3d_map_desc src
;
3545 HRESULT hr
= WINED3D_OK
;
3548 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3549 device
, src_texture
, src_level
, dst_texture
, level_count
);
3551 if (src_texture
->resource
.format
!= dst_texture
->resource
.format
)
3553 WARN("Source and destination formats do not match.\n");
3554 return WINED3DERR_INVALIDCALL
;
3557 if (wined3d_texture_get_level_width(src_texture
, src_level
) != dst_texture
->resource
.width
3558 || wined3d_texture_get_level_height(src_texture
, src_level
) != dst_texture
->resource
.height
3559 || wined3d_texture_get_level_depth(src_texture
, src_level
) != dst_texture
->resource
.depth
)
3561 WARN("Source and destination dimensions do not match.\n");
3562 return WINED3DERR_INVALIDCALL
;
3565 context
= context_acquire(device
, NULL
, 0);
3567 /* Only a prepare, since we're uploading entire volumes. */
3568 wined3d_texture_prepare_texture(dst_texture
, context
, FALSE
);
3569 wined3d_texture_bind_and_dirtify(dst_texture
, context
, FALSE
);
3571 for (i
= 0; i
< level_count
; ++i
)
3573 if (FAILED(hr
= wined3d_resource_map(&src_texture
->resource
,
3574 src_level
+ i
, &src
, NULL
, WINED3D_MAP_READONLY
)))
3577 data
.buffer_object
= 0;
3578 data
.addr
= src
.data
;
3579 wined3d_texture_upload_data(dst_texture
, i
, context
, NULL
, &data
, src
.row_pitch
, src
.slice_pitch
);
3580 wined3d_texture_invalidate_location(dst_texture
, i
, ~WINED3D_LOCATION_TEXTURE_RGB
);
3582 if (FAILED(hr
= wined3d_resource_unmap(&src_texture
->resource
, src_level
+ i
)))
3587 context_release(context
);
3591 HRESULT CDECL
wined3d_device_update_texture(struct wined3d_device
*device
,
3592 struct wined3d_texture
*src_texture
, struct wined3d_texture
*dst_texture
)
3594 unsigned int src_size
, dst_size
, src_skip_levels
= 0;
3595 unsigned int layer_count
, level_count
, i
, j
;
3596 enum wined3d_resource_type type
;
3598 struct wined3d_context
*context
;
3600 TRACE("device %p, src_texture %p, dst_texture %p.\n", device
, src_texture
, dst_texture
);
3602 /* Verify that the source and destination textures are non-NULL. */
3603 if (!src_texture
|| !dst_texture
)
3605 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3606 return WINED3DERR_INVALIDCALL
;
3609 if (src_texture
->resource
.pool
!= WINED3D_POOL_SYSTEM_MEM
)
3611 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3612 return WINED3DERR_INVALIDCALL
;
3614 if (dst_texture
->resource
.pool
!= WINED3D_POOL_DEFAULT
)
3616 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3617 return WINED3DERR_INVALIDCALL
;
3620 /* Verify that the source and destination textures are the same type. */
3621 type
= src_texture
->resource
.type
;
3622 if (dst_texture
->resource
.type
!= type
)
3624 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3625 return WINED3DERR_INVALIDCALL
;
3628 layer_count
= src_texture
->layer_count
;
3629 if (layer_count
!= dst_texture
->layer_count
)
3631 WARN("Source and destination have different layer counts.\n");
3632 return WINED3DERR_INVALIDCALL
;
3635 level_count
= min(wined3d_texture_get_level_count(src_texture
),
3636 wined3d_texture_get_level_count(dst_texture
));
3638 src_size
= max(src_texture
->resource
.width
, src_texture
->resource
.height
);
3639 dst_size
= max(dst_texture
->resource
.width
, dst_texture
->resource
.height
);
3640 if (type
== WINED3D_RTYPE_TEXTURE_3D
)
3642 src_size
= max(src_size
, src_texture
->resource
.depth
);
3643 dst_size
= max(dst_size
, dst_texture
->resource
.depth
);
3645 while (src_size
> dst_size
)
3651 /* Make sure that the destination texture is loaded. */
3652 context
= context_acquire(device
, NULL
, 0);
3653 wined3d_texture_load(dst_texture
, context
, FALSE
);
3654 context_release(context
);
3656 /* Update every surface level of the texture. */
3659 case WINED3D_RTYPE_TEXTURE_2D
:
3661 unsigned int src_levels
= src_texture
->level_count
;
3662 unsigned int dst_levels
= dst_texture
->level_count
;
3663 struct wined3d_surface
*src_surface
;
3664 struct wined3d_surface
*dst_surface
;
3666 for (i
= 0; i
< layer_count
; ++i
)
3668 for (j
= 0; j
< level_count
; ++j
)
3670 src_surface
= src_texture
->sub_resources
[i
* src_levels
+ j
+ src_skip_levels
].u
.surface
;
3671 dst_surface
= dst_texture
->sub_resources
[i
* dst_levels
+ j
].u
.surface
;
3672 if (FAILED(hr
= surface_upload_from_surface(dst_surface
, NULL
, src_surface
, NULL
)))
3674 WARN("Failed to update surface, hr %#x.\n", hr
);
3682 case WINED3D_RTYPE_TEXTURE_3D
:
3683 if (FAILED(hr
= wined3d_device_update_texture_3d(device
,
3684 src_texture
, src_skip_levels
, dst_texture
, level_count
)))
3685 WARN("Failed to update 3D texture, hr %#x.\n", hr
);
3689 FIXME("Unsupported texture type %#x.\n", type
);
3690 return WINED3DERR_INVALIDCALL
;
3694 HRESULT CDECL
wined3d_device_validate_device(const struct wined3d_device
*device
, DWORD
*num_passes
)
3696 const struct wined3d_state
*state
= &device
->state
;
3697 struct wined3d_texture
*texture
;
3700 TRACE("device %p, num_passes %p.\n", device
, num_passes
);
3702 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
3704 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] == WINED3D_TEXF_NONE
)
3706 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
3707 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
3709 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] == WINED3D_TEXF_NONE
)
3711 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
3712 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
3715 texture
= state
->textures
[i
];
3716 if (!texture
|| texture
->resource
.format_flags
& WINED3DFMT_FLAG_FILTERING
) continue;
3718 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] != WINED3D_TEXF_POINT
)
3720 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i
);
3723 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] != WINED3D_TEXF_POINT
)
3725 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i
);
3728 if (state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_NONE
3729 && state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_POINT
)
3731 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i
);
3736 if (state
->render_states
[WINED3D_RS_ZENABLE
] || state
->render_states
[WINED3D_RS_ZWRITEENABLE
]
3737 || state
->render_states
[WINED3D_RS_STENCILENABLE
])
3739 struct wined3d_rendertarget_view
*rt
= device
->fb
.render_targets
[0];
3740 struct wined3d_rendertarget_view
*ds
= device
->fb
.depth_stencil
;
3742 if (ds
&& rt
&& (ds
->width
< rt
->width
|| ds
->height
< rt
->height
))
3744 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3745 return WINED3DERR_CONFLICTINGRENDERSTATE
;
3749 /* return a sensible default */
3752 TRACE("returning D3D_OK\n");
3756 void CDECL
wined3d_device_set_software_vertex_processing(struct wined3d_device
*device
, BOOL software
)
3760 TRACE("device %p, software %#x.\n", device
, software
);
3764 FIXME("device %p, software %#x stub!\n", device
, software
);
3768 device
->softwareVertexProcessing
= software
;
3771 BOOL CDECL
wined3d_device_get_software_vertex_processing(const struct wined3d_device
*device
)
3775 TRACE("device %p.\n", device
);
3779 TRACE("device %p stub!\n", device
);
3783 return device
->softwareVertexProcessing
;
3786 HRESULT CDECL
wined3d_device_get_raster_status(const struct wined3d_device
*device
,
3787 UINT swapchain_idx
, struct wined3d_raster_status
*raster_status
)
3789 struct wined3d_swapchain
*swapchain
;
3791 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3792 device
, swapchain_idx
, raster_status
);
3794 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3795 return WINED3DERR_INVALIDCALL
;
3797 return wined3d_swapchain_get_raster_status(swapchain
, raster_status
);
3800 HRESULT CDECL
wined3d_device_set_npatch_mode(struct wined3d_device
*device
, float segments
)
3804 TRACE("device %p, segments %.8e.\n", device
, segments
);
3806 if (segments
!= 0.0f
)
3810 FIXME("device %p, segments %.8e stub!\n", device
, segments
);
3818 float CDECL
wined3d_device_get_npatch_mode(const struct wined3d_device
*device
)
3822 TRACE("device %p.\n", device
);
3826 FIXME("device %p stub!\n", device
);
3833 void CDECL
wined3d_device_copy_resource(struct wined3d_device
*device
,
3834 struct wined3d_resource
*dst_resource
, struct wined3d_resource
*src_resource
)
3836 struct wined3d_texture
*dst_texture
, *src_texture
;
3837 struct wined3d_box box
;
3840 TRACE("device %p, dst_resource %p, src_resource %p.\n", device
, dst_resource
, src_resource
);
3842 if (src_resource
== dst_resource
)
3844 WARN("Source and destination are the same resource.\n");
3848 if (src_resource
->type
!= dst_resource
->type
)
3850 WARN("Resource types (%s / %s) don't match.\n",
3851 debug_d3dresourcetype(dst_resource
->type
),
3852 debug_d3dresourcetype(src_resource
->type
));
3856 if (src_resource
->width
!= dst_resource
->width
3857 || src_resource
->height
!= dst_resource
->height
3858 || src_resource
->depth
!= dst_resource
->depth
)
3860 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3861 dst_resource
->width
, dst_resource
->height
, dst_resource
->depth
,
3862 src_resource
->width
, src_resource
->height
, src_resource
->depth
);
3866 if (src_resource
->format
->id
!= dst_resource
->format
->id
)
3868 WARN("Resource formats (%s / %s) don't match.\n",
3869 debug_d3dformat(dst_resource
->format
->id
),
3870 debug_d3dformat(src_resource
->format
->id
));
3874 if (dst_resource
->type
== WINED3D_RTYPE_BUFFER
)
3878 box
.right
= src_resource
->size
;
3883 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, 0, &box
,
3884 src_resource
, 0, &box
, 0, NULL
, WINED3D_TEXF_POINT
);
3888 if (dst_resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
3890 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource
->type
));
3894 dst_texture
= texture_from_resource(dst_resource
);
3895 src_texture
= texture_from_resource(src_resource
);
3897 if (src_texture
->layer_count
!= dst_texture
->layer_count
3898 || src_texture
->level_count
!= dst_texture
->level_count
)
3900 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3901 dst_texture
->layer_count
, dst_texture
->level_count
,
3902 src_texture
->layer_count
, src_texture
->level_count
);
3906 for (i
= 0; i
< dst_texture
->level_count
; ++i
)
3910 box
.right
= wined3d_texture_get_level_width(dst_texture
, i
);
3911 box
.bottom
= wined3d_texture_get_level_height(dst_texture
, i
);
3913 box
.back
= wined3d_texture_get_level_depth(dst_texture
, i
);
3915 for (j
= 0; j
< dst_texture
->layer_count
; ++j
)
3917 unsigned int idx
= j
* dst_texture
->level_count
+ i
;
3919 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, idx
, &box
,
3920 src_resource
, idx
, &box
, 0, NULL
, WINED3D_TEXF_POINT
);
3925 HRESULT CDECL
wined3d_device_copy_sub_resource_region(struct wined3d_device
*device
,
3926 struct wined3d_resource
*dst_resource
, unsigned int dst_sub_resource_idx
, unsigned int dst_x
,
3927 unsigned int dst_y
, unsigned int dst_z
, struct wined3d_resource
*src_resource
,
3928 unsigned int src_sub_resource_idx
, const struct wined3d_box
*src_box
)
3930 struct wined3d_box dst_box
, b
;
3932 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3933 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3934 device
, dst_resource
, dst_sub_resource_idx
, dst_x
, dst_y
, dst_z
,
3935 src_resource
, src_sub_resource_idx
, debug_box(src_box
));
3937 if (src_resource
== dst_resource
&& src_sub_resource_idx
== dst_sub_resource_idx
)
3939 WARN("Source and destination are the same sub-resource.\n");
3940 return WINED3DERR_INVALIDCALL
;
3943 if (src_resource
->type
!= dst_resource
->type
)
3945 WARN("Resource types (%s / %s) don't match.\n",
3946 debug_d3dresourcetype(dst_resource
->type
),
3947 debug_d3dresourcetype(src_resource
->type
));
3948 return WINED3DERR_INVALIDCALL
;
3951 if (src_resource
->format
->id
!= dst_resource
->format
->id
)
3953 WARN("Resource formats (%s / %s) don't match.\n",
3954 debug_d3dformat(dst_resource
->format
->id
),
3955 debug_d3dformat(src_resource
->format
->id
));
3956 return WINED3DERR_INVALIDCALL
;
3959 if (dst_resource
->type
== WINED3D_RTYPE_BUFFER
)
3961 if (dst_sub_resource_idx
)
3963 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx
);
3964 return WINED3DERR_INVALIDCALL
;
3967 if (src_sub_resource_idx
)
3969 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx
);
3970 return WINED3DERR_INVALIDCALL
;
3977 b
.right
= src_resource
->size
;
3983 else if ((src_box
->left
>= src_box
->right
3984 || src_box
->top
>= src_box
->bottom
3985 || src_box
->front
>= src_box
->back
))
3987 WARN("Invalid box %s specified.\n", debug_box(src_box
));
3988 return WINED3DERR_INVALIDCALL
;
3991 if (src_box
->right
> src_resource
->size
|| dst_x
>= dst_resource
->size
3992 || src_box
->right
- src_box
->left
> dst_resource
->size
- dst_x
)
3994 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
3995 dst_x
, src_box
->left
, src_box
->right
- src_box
->left
);
3996 return WINED3DERR_INVALIDCALL
;
3999 dst_box
.left
= dst_x
;
4001 dst_box
.right
= dst_x
+ (src_box
->right
- src_box
->left
);
4006 else if (dst_resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
4008 struct wined3d_texture
*dst_texture
= texture_from_resource(dst_resource
);
4009 struct wined3d_texture
*src_texture
= texture_from_resource(src_resource
);
4010 unsigned int src_level
= src_sub_resource_idx
% src_texture
->level_count
;
4012 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
)
4014 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx
);
4015 return WINED3DERR_INVALIDCALL
;
4018 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
)
4020 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx
);
4021 return WINED3DERR_INVALIDCALL
;
4024 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
)
4026 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx
);
4027 return WINED3DERR_INVALIDCALL
;
4030 if (src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
4032 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx
);
4033 return WINED3DERR_INVALIDCALL
;
4041 b
.right
= wined3d_texture_get_level_width(src_texture
, src_level
);
4042 b
.bottom
= wined3d_texture_get_level_height(src_texture
, src_level
);
4047 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture
, src_level
, src_box
)))
4049 WARN("Invalid source box %s.\n", debug_box(src_box
));
4050 return WINED3DERR_INVALIDCALL
;
4053 dst_box
.left
= dst_x
;
4054 dst_box
.top
= dst_y
;
4055 dst_box
.right
= dst_x
+ (src_box
->right
- src_box
->left
);
4056 dst_box
.bottom
= dst_y
+ (src_box
->bottom
- src_box
->top
);
4059 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture
,
4060 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
4062 WARN("Invalid destination box %s.\n", debug_box(&dst_box
));
4063 return WINED3DERR_INVALIDCALL
;
4068 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource
->type
));
4069 return WINED3DERR_INVALIDCALL
;
4072 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, dst_sub_resource_idx
, &dst_box
,
4073 src_resource
, src_sub_resource_idx
, src_box
, 0, NULL
, WINED3D_TEXF_POINT
);
4078 void CDECL
wined3d_device_update_sub_resource(struct wined3d_device
*device
, struct wined3d_resource
*resource
,
4079 unsigned int sub_resource_idx
, const struct wined3d_box
*box
, const void *data
, unsigned int row_pitch
,
4080 unsigned int depth_pitch
)
4082 unsigned int width
, height
, depth
;
4083 struct wined3d_box b
;
4085 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4086 device
, resource
, sub_resource_idx
, debug_box(box
), data
, row_pitch
, depth_pitch
);
4088 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
4090 if (sub_resource_idx
> 0)
4092 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
4096 width
= resource
->size
;
4100 else if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
|| resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
4102 struct wined3d_texture
*texture
= texture_from_resource(resource
);
4105 if (sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
4107 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
4111 level
= sub_resource_idx
% texture
->level_count
;
4112 width
= wined3d_texture_get_level_width(texture
, level
);
4113 height
= wined3d_texture_get_level_height(texture
, level
);
4114 depth
= wined3d_texture_get_level_depth(texture
, level
);
4118 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
4132 else if (box
->left
>= box
->right
|| box
->right
> width
4133 || box
->top
>= box
->bottom
|| box
->bottom
> height
4134 || box
->front
>= box
->back
|| box
->back
> depth
)
4136 WARN("Invalid box %s specified.\n", debug_box(box
));
4140 wined3d_cs_emit_update_sub_resource(device
->cs
, resource
, sub_resource_idx
, box
, data
, row_pitch
, depth_pitch
);
4143 HRESULT CDECL
wined3d_device_clear_rendertarget_view(struct wined3d_device
*device
,
4144 struct wined3d_rendertarget_view
*view
, const RECT
*rect
, DWORD flags
,
4145 const struct wined3d_color
*color
, float depth
, DWORD stencil
)
4147 const struct wined3d_blitter_ops
*blitter
;
4148 struct wined3d_resource
*resource
;
4149 enum wined3d_blit_op blit_op
;
4152 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4153 device
, view
, wine_dbgstr_rect(rect
), flags
, debug_color(color
), depth
, stencil
);
4158 resource
= view
->resource
;
4159 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
4161 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
4162 return WINED3DERR_INVALIDCALL
;
4165 if (view
->depth
> 1)
4167 FIXME("Layered clears not implemented.\n");
4168 return WINED3DERR_INVALIDCALL
;
4173 SetRect(&r
, 0, 0, view
->width
, view
->height
);
4178 struct wined3d_box b
= {rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1};
4179 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
4182 if (FAILED(hr
= wined3d_texture_check_box_dimensions(texture
,
4183 view
->sub_resource_idx
% texture
->level_count
, &b
)))
4187 if (flags
& WINED3DCLEAR_TARGET
)
4188 blit_op
= WINED3D_BLIT_OP_COLOR_FILL
;
4190 blit_op
= WINED3D_BLIT_OP_DEPTH_FILL
;
4192 if (!(blitter
= wined3d_select_blitter(&device
->adapter
->gl_info
, &device
->adapter
->d3d_info
,
4193 blit_op
, NULL
, 0, 0, NULL
, rect
, resource
->usage
, resource
->pool
, resource
->format
)))
4195 FIXME("No blitter is capable of performing the requested fill operation.\n");
4196 return WINED3DERR_INVALIDCALL
;
4199 blitter
->blitter_clear(device
, view
, rect
, flags
, color
, depth
, stencil
);
4204 struct wined3d_rendertarget_view
* CDECL
wined3d_device_get_rendertarget_view(const struct wined3d_device
*device
,
4205 unsigned int view_idx
)
4207 TRACE("device %p, view_idx %u.\n", device
, view_idx
);
4209 if (view_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4211 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4215 return device
->fb
.render_targets
[view_idx
];
4218 struct wined3d_rendertarget_view
* CDECL
wined3d_device_get_depth_stencil_view(const struct wined3d_device
*device
)
4220 TRACE("device %p.\n", device
);
4222 return device
->fb
.depth_stencil
;
4225 HRESULT CDECL
wined3d_device_set_rendertarget_view(struct wined3d_device
*device
,
4226 unsigned int view_idx
, struct wined3d_rendertarget_view
*view
, BOOL set_viewport
)
4228 struct wined3d_rendertarget_view
*prev
;
4230 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4231 device
, view_idx
, view
, set_viewport
);
4233 if (view_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4235 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4236 return WINED3DERR_INVALIDCALL
;
4239 if (view
&& !(view
->resource
->usage
& WINED3DUSAGE_RENDERTARGET
))
4241 WARN("View resource %p doesn't have render target usage.\n", view
->resource
);
4242 return WINED3DERR_INVALIDCALL
;
4245 /* Set the viewport and scissor rectangles, if requested. Tests show that
4246 * stateblock recording is ignored, the change goes directly into the
4247 * primary stateblock. */
4248 if (!view_idx
&& set_viewport
)
4250 struct wined3d_state
*state
= &device
->state
;
4252 state
->viewport
.x
= 0;
4253 state
->viewport
.y
= 0;
4254 state
->viewport
.width
= view
->width
;
4255 state
->viewport
.height
= view
->height
;
4256 state
->viewport
.min_z
= 0.0f
;
4257 state
->viewport
.max_z
= 1.0f
;
4258 wined3d_cs_emit_set_viewport(device
->cs
, &state
->viewport
);
4260 SetRect(&state
->scissor_rect
, 0, 0, view
->width
, view
->height
);
4261 wined3d_cs_emit_set_scissor_rect(device
->cs
, &state
->scissor_rect
);
4265 prev
= device
->fb
.render_targets
[view_idx
];
4270 wined3d_rendertarget_view_incref(view
);
4271 device
->fb
.render_targets
[view_idx
] = view
;
4272 wined3d_cs_emit_set_rendertarget_view(device
->cs
, view_idx
, view
);
4273 /* Release after the assignment, to prevent device_resource_released()
4274 * from seeing the surface as still in use. */
4276 wined3d_rendertarget_view_decref(prev
);
4281 void CDECL
wined3d_device_set_depth_stencil_view(struct wined3d_device
*device
, struct wined3d_rendertarget_view
*view
)
4283 struct wined3d_rendertarget_view
*prev
;
4285 TRACE("device %p, view %p.\n", device
, view
);
4287 prev
= device
->fb
.depth_stencil
;
4290 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4294 if ((device
->fb
.depth_stencil
= view
))
4295 wined3d_rendertarget_view_incref(view
);
4296 wined3d_cs_emit_set_depth_stencil_view(device
->cs
, view
);
4298 wined3d_rendertarget_view_decref(prev
);
4301 static struct wined3d_texture
*wined3d_device_create_cursor_texture(struct wined3d_device
*device
,
4302 struct wined3d_texture
*cursor_image
, unsigned int sub_resource_idx
)
4304 unsigned int texture_level
= sub_resource_idx
% cursor_image
->level_count
;
4305 struct wined3d_sub_resource_data data
;
4306 struct wined3d_resource_desc desc
;
4307 struct wined3d_map_desc map_desc
;
4308 struct wined3d_texture
*texture
;
4311 if (FAILED(wined3d_resource_map(&cursor_image
->resource
, sub_resource_idx
, &map_desc
, NULL
, WINED3D_MAP_READONLY
)))
4313 ERR("Failed to map source texture.\n");
4317 data
.data
= map_desc
.data
;
4318 data
.row_pitch
= map_desc
.row_pitch
;
4319 data
.slice_pitch
= map_desc
.slice_pitch
;
4321 desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
4322 desc
.format
= WINED3DFMT_B8G8R8A8_UNORM
;
4323 desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
4324 desc
.multisample_quality
= 0;
4325 desc
.usage
= WINED3DUSAGE_DYNAMIC
;
4326 desc
.pool
= WINED3D_POOL_DEFAULT
;
4327 desc
.width
= wined3d_texture_get_level_width(cursor_image
, texture_level
);
4328 desc
.height
= wined3d_texture_get_level_height(cursor_image
, texture_level
);
4332 hr
= wined3d_texture_create(device
, &desc
, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE
,
4333 &data
, NULL
, &wined3d_null_parent_ops
, &texture
);
4334 wined3d_resource_unmap(&cursor_image
->resource
, sub_resource_idx
);
4337 ERR("Failed to create cursor texture.\n");
4344 HRESULT CDECL
wined3d_device_set_cursor_properties(struct wined3d_device
*device
,
4345 UINT x_hotspot
, UINT y_hotspot
, struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4347 unsigned int texture_level
= sub_resource_idx
% texture
->level_count
;
4348 unsigned int cursor_width
, cursor_height
;
4349 struct wined3d_display_mode mode
;
4350 struct wined3d_map_desc map_desc
;
4353 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4354 device
, x_hotspot
, y_hotspot
, texture
, sub_resource_idx
);
4356 if (sub_resource_idx
>= texture
->level_count
* texture
->layer_count
4357 || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
4358 return WINED3DERR_INVALIDCALL
;
4360 if (device
->cursor_texture
)
4362 wined3d_texture_decref(device
->cursor_texture
);
4363 device
->cursor_texture
= NULL
;
4366 if (texture
->resource
.format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4368 WARN("Texture %p has invalid format %s.\n",
4369 texture
, debug_d3dformat(texture
->resource
.format
->id
));
4370 return WINED3DERR_INVALIDCALL
;
4373 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, &mode
, NULL
)))
4375 ERR("Failed to get display mode, hr %#x.\n", hr
);
4376 return WINED3DERR_INVALIDCALL
;
4379 cursor_width
= wined3d_texture_get_level_width(texture
, texture_level
);
4380 cursor_height
= wined3d_texture_get_level_height(texture
, texture_level
);
4381 if (cursor_width
> mode
.width
|| cursor_height
> mode
.height
)
4383 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4384 texture
, sub_resource_idx
, cursor_width
, cursor_height
, mode
.width
, mode
.height
);
4385 return WINED3DERR_INVALIDCALL
;
4388 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4390 /* Do not store the surface's pointer because the application may
4391 * release it after setting the cursor image. Windows doesn't
4392 * addref the set surface, so we can't do this either without
4393 * creating circular refcount dependencies. */
4394 if (!(device
->cursor_texture
= wined3d_device_create_cursor_texture(device
, texture
, sub_resource_idx
)))
4396 ERR("Failed to create cursor texture.\n");
4397 return WINED3DERR_INVALIDCALL
;
4400 if (cursor_width
== 32 && cursor_height
== 32)
4402 UINT mask_size
= cursor_width
* cursor_height
/ 8;
4403 ICONINFO cursor_info
;
4407 /* 32-bit user32 cursors ignore the alpha channel if it's all
4408 * zeroes, and use the mask instead. Fill the mask with all ones
4409 * to ensure we still get a fully transparent cursor. */
4410 if (!(mask_bits
= HeapAlloc(GetProcessHeap(), 0, mask_size
)))
4411 return E_OUTOFMEMORY
;
4412 memset(mask_bits
, 0xff, mask_size
);
4414 wined3d_resource_map(&texture
->resource
, sub_resource_idx
, &map_desc
, NULL
,
4415 WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
);
4416 cursor_info
.fIcon
= FALSE
;
4417 cursor_info
.xHotspot
= x_hotspot
;
4418 cursor_info
.yHotspot
= y_hotspot
;
4419 cursor_info
.hbmMask
= CreateBitmap(cursor_width
, cursor_height
, 1, 1, mask_bits
);
4420 cursor_info
.hbmColor
= CreateBitmap(cursor_width
, cursor_height
, 1, 32, map_desc
.data
);
4421 wined3d_resource_unmap(&texture
->resource
, sub_resource_idx
);
4423 /* Create our cursor and clean up. */
4424 cursor
= CreateIconIndirect(&cursor_info
);
4425 if (cursor_info
.hbmMask
)
4426 DeleteObject(cursor_info
.hbmMask
);
4427 if (cursor_info
.hbmColor
)
4428 DeleteObject(cursor_info
.hbmColor
);
4429 if (device
->hardwareCursor
)
4430 DestroyCursor(device
->hardwareCursor
);
4431 device
->hardwareCursor
= cursor
;
4432 if (device
->bCursorVisible
)
4435 HeapFree(GetProcessHeap(), 0, mask_bits
);
4438 TRACE("New cursor dimensions are %ux%u.\n", cursor_width
, cursor_height
);
4439 device
->cursorWidth
= cursor_width
;
4440 device
->cursorHeight
= cursor_height
;
4441 device
->xHotSpot
= x_hotspot
;
4442 device
->yHotSpot
= y_hotspot
;
4447 void CDECL
wined3d_device_set_cursor_position(struct wined3d_device
*device
,
4448 int x_screen_space
, int y_screen_space
, DWORD flags
)
4450 TRACE("device %p, x %d, y %d, flags %#x.\n",
4451 device
, x_screen_space
, y_screen_space
, flags
);
4453 device
->xScreenSpace
= x_screen_space
;
4454 device
->yScreenSpace
= y_screen_space
;
4456 if (device
->hardwareCursor
)
4460 GetCursorPos( &pt
);
4461 if (x_screen_space
== pt
.x
&& y_screen_space
== pt
.y
)
4463 SetCursorPos( x_screen_space
, y_screen_space
);
4465 /* Switch to the software cursor if position diverges from the hardware one. */
4466 GetCursorPos( &pt
);
4467 if (x_screen_space
!= pt
.x
|| y_screen_space
!= pt
.y
)
4469 if (device
->bCursorVisible
) SetCursor( NULL
);
4470 DestroyCursor( device
->hardwareCursor
);
4471 device
->hardwareCursor
= 0;
4476 BOOL CDECL
wined3d_device_show_cursor(struct wined3d_device
*device
, BOOL show
)
4478 BOOL oldVisible
= device
->bCursorVisible
;
4480 TRACE("device %p, show %#x.\n", device
, show
);
4483 * When ShowCursor is first called it should make the cursor appear at the OS's last
4484 * known cursor position.
4486 if (show
&& !oldVisible
)
4490 device
->xScreenSpace
= pt
.x
;
4491 device
->yScreenSpace
= pt
.y
;
4494 if (device
->hardwareCursor
)
4496 device
->bCursorVisible
= show
;
4498 SetCursor(device
->hardwareCursor
);
4502 else if (device
->cursor_texture
)
4504 device
->bCursorVisible
= show
;
4510 void CDECL
wined3d_device_evict_managed_resources(struct wined3d_device
*device
)
4512 struct wined3d_resource
*resource
, *cursor
;
4514 TRACE("device %p.\n", device
);
4516 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4518 TRACE("Checking resource %p for eviction.\n", resource
);
4520 if (resource
->pool
== WINED3D_POOL_MANAGED
&& !resource
->map_count
)
4522 TRACE("Evicting %p.\n", resource
);
4523 wined3d_cs_emit_unload_resource(device
->cs
, resource
);
4528 HRESULT CDECL
wined3d_device_reset(struct wined3d_device
*device
,
4529 const struct wined3d_swapchain_desc
*swapchain_desc
, const struct wined3d_display_mode
*mode
,
4530 wined3d_device_reset_cb callback
, BOOL reset_state
)
4532 struct wined3d_resource
*resource
, *cursor
;
4533 struct wined3d_swapchain
*swapchain
;
4534 struct wined3d_view_desc view_desc
;
4535 BOOL backbuffer_resized
;
4536 HRESULT hr
= WINED3D_OK
;
4539 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4540 device
, swapchain_desc
, mode
, callback
, reset_state
);
4542 if (!(swapchain
= wined3d_device_get_swapchain(device
, 0)))
4544 ERR("Failed to get the first implicit swapchain.\n");
4545 return WINED3DERR_INVALIDCALL
;
4550 if (device
->logo_texture
)
4552 wined3d_texture_decref(device
->logo_texture
);
4553 device
->logo_texture
= NULL
;
4555 if (device
->cursor_texture
)
4557 wined3d_texture_decref(device
->cursor_texture
);
4558 device
->cursor_texture
= NULL
;
4560 state_unbind_resources(&device
->state
);
4563 if (device
->fb
.render_targets
)
4565 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
4567 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
4570 wined3d_device_set_depth_stencil_view(device
, NULL
);
4574 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4576 TRACE("Enumerating resource %p.\n", resource
);
4577 if (FAILED(hr
= callback(resource
)))
4582 TRACE("New params:\n");
4583 TRACE("backbuffer_width %u\n", swapchain_desc
->backbuffer_width
);
4584 TRACE("backbuffer_height %u\n", swapchain_desc
->backbuffer_height
);
4585 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc
->backbuffer_format
));
4586 TRACE("backbuffer_count %u\n", swapchain_desc
->backbuffer_count
);
4587 TRACE("multisample_type %#x\n", swapchain_desc
->multisample_type
);
4588 TRACE("multisample_quality %u\n", swapchain_desc
->multisample_quality
);
4589 TRACE("swap_effect %#x\n", swapchain_desc
->swap_effect
);
4590 TRACE("device_window %p\n", swapchain_desc
->device_window
);
4591 TRACE("windowed %#x\n", swapchain_desc
->windowed
);
4592 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc
->enable_auto_depth_stencil
);
4593 if (swapchain_desc
->enable_auto_depth_stencil
)
4594 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc
->auto_depth_stencil_format
));
4595 TRACE("flags %#x\n", swapchain_desc
->flags
);
4596 TRACE("refresh_rate %u\n", swapchain_desc
->refresh_rate
);
4597 TRACE("swap_interval %u\n", swapchain_desc
->swap_interval
);
4598 TRACE("auto_restore_display_mode %#x\n", swapchain_desc
->auto_restore_display_mode
);
4600 /* No special treatment of these parameters. Just store them */
4601 swapchain
->desc
.swap_effect
= swapchain_desc
->swap_effect
;
4602 swapchain
->desc
.enable_auto_depth_stencil
= swapchain_desc
->enable_auto_depth_stencil
;
4603 swapchain
->desc
.auto_depth_stencil_format
= swapchain_desc
->auto_depth_stencil_format
;
4604 swapchain
->desc
.flags
= swapchain_desc
->flags
;
4605 swapchain
->desc
.refresh_rate
= swapchain_desc
->refresh_rate
;
4606 swapchain
->desc
.swap_interval
= swapchain_desc
->swap_interval
;
4607 swapchain
->desc
.auto_restore_display_mode
= swapchain_desc
->auto_restore_display_mode
;
4609 if (swapchain_desc
->device_window
4610 && swapchain_desc
->device_window
!= swapchain
->desc
.device_window
)
4612 TRACE("Changing the device window from %p to %p.\n",
4613 swapchain
->desc
.device_window
, swapchain_desc
->device_window
);
4614 swapchain
->desc
.device_window
= swapchain_desc
->device_window
;
4615 swapchain
->device_window
= swapchain_desc
->device_window
;
4616 wined3d_swapchain_set_window(swapchain
, NULL
);
4619 backbuffer_resized
= swapchain_desc
->backbuffer_width
!= swapchain
->desc
.backbuffer_width
4620 || swapchain_desc
->backbuffer_height
!= swapchain
->desc
.backbuffer_height
;
4622 if (!swapchain_desc
->windowed
!= !swapchain
->desc
.windowed
4623 || swapchain
->reapply_mode
|| mode
4624 || (!swapchain_desc
->windowed
&& backbuffer_resized
))
4626 if (FAILED(hr
= wined3d_swapchain_set_fullscreen(swapchain
, swapchain_desc
, mode
)))
4629 else if (!swapchain_desc
->windowed
)
4631 DWORD style
= device
->style
;
4632 DWORD exStyle
= device
->exStyle
;
4633 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4634 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4635 * Reset to clear up their mess. Guild Wars also loses the device during that.
4638 device
->exStyle
= 0;
4639 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
,
4640 swapchain_desc
->backbuffer_width
,
4641 swapchain_desc
->backbuffer_height
);
4642 device
->style
= style
;
4643 device
->exStyle
= exStyle
;
4646 if (FAILED(hr
= wined3d_swapchain_resize_buffers(swapchain
, swapchain_desc
->backbuffer_count
,
4647 swapchain_desc
->backbuffer_width
, swapchain_desc
->backbuffer_height
, swapchain_desc
->backbuffer_format
,
4648 swapchain_desc
->multisample_type
, swapchain_desc
->multisample_quality
)))
4651 if (device
->auto_depth_stencil_view
)
4653 wined3d_rendertarget_view_decref(device
->auto_depth_stencil_view
);
4654 device
->auto_depth_stencil_view
= NULL
;
4656 if (swapchain
->desc
.enable_auto_depth_stencil
)
4658 struct wined3d_resource_desc texture_desc
;
4659 struct wined3d_texture
*texture
;
4662 TRACE("Creating the depth stencil buffer.\n");
4664 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
4665 texture_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
4666 texture_desc
.multisample_type
= swapchain
->desc
.multisample_type
;
4667 texture_desc
.multisample_quality
= swapchain
->desc
.multisample_quality
;
4668 texture_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
4669 texture_desc
.pool
= WINED3D_POOL_DEFAULT
;
4670 texture_desc
.width
= swapchain
->desc
.backbuffer_width
;
4671 texture_desc
.height
= swapchain
->desc
.backbuffer_height
;
4672 texture_desc
.depth
= 1;
4673 texture_desc
.size
= 0;
4675 if (swapchain_desc
->flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
4676 flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
4678 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
4679 device
->device_parent
, &texture_desc
, flags
, &texture
)))
4681 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
4682 return WINED3DERR_INVALIDCALL
;
4685 view_desc
.format_id
= texture
->resource
.format
->id
;
4686 view_desc
.flags
= 0;
4687 view_desc
.u
.texture
.level_idx
= 0;
4688 view_desc
.u
.texture
.level_count
= 1;
4689 view_desc
.u
.texture
.layer_idx
= 0;
4690 view_desc
.u
.texture
.layer_count
= 1;
4691 hr
= wined3d_rendertarget_view_create(&view_desc
, &texture
->resource
,
4692 NULL
, &wined3d_null_parent_ops
, &device
->auto_depth_stencil_view
);
4693 wined3d_texture_decref(texture
);
4696 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
4700 wined3d_device_set_depth_stencil_view(device
, device
->auto_depth_stencil_view
);
4703 if (device
->back_buffer_view
)
4705 wined3d_rendertarget_view_decref(device
->back_buffer_view
);
4706 device
->back_buffer_view
= NULL
;
4708 if (swapchain
->desc
.backbuffer_count
)
4710 struct wined3d_resource
*back_buffer
= &swapchain
->back_buffers
[0]->resource
;
4712 view_desc
.format_id
= back_buffer
->format
->id
;
4713 view_desc
.flags
= 0;
4714 view_desc
.u
.texture
.level_idx
= 0;
4715 view_desc
.u
.texture
.level_count
= 1;
4716 view_desc
.u
.texture
.layer_idx
= 0;
4717 view_desc
.u
.texture
.layer_count
= 1;
4718 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
, back_buffer
,
4719 NULL
, &wined3d_null_parent_ops
, &device
->back_buffer_view
)))
4721 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
4726 wine_rb_clear(&device
->samplers
, device_free_sampler
, NULL
);
4730 TRACE("Resetting stateblock.\n");
4731 if (device
->recording
)
4733 wined3d_stateblock_decref(device
->recording
);
4734 device
->recording
= NULL
;
4736 wined3d_cs_emit_reset_state(device
->cs
);
4737 state_cleanup(&device
->state
);
4739 if (device
->d3d_initialized
)
4740 wined3d_device_delete_opengl_contexts(device
);
4742 memset(&device
->state
, 0, sizeof(device
->state
));
4743 state_init(&device
->state
, &device
->fb
, &device
->adapter
->gl_info
,
4744 &device
->adapter
->d3d_info
, WINED3D_STATE_INIT_DEFAULT
);
4745 device
->update_state
= &device
->state
;
4747 device_init_swapchain_state(device
, swapchain
);
4748 if (wined3d_settings
.logo
)
4749 device_load_logo(device
, wined3d_settings
.logo
);
4751 else if (device
->back_buffer_view
)
4753 struct wined3d_rendertarget_view
*view
= device
->back_buffer_view
;
4754 struct wined3d_state
*state
= &device
->state
;
4756 wined3d_device_set_rendertarget_view(device
, 0, view
, FALSE
);
4758 /* Note the min_z / max_z is not reset. */
4759 state
->viewport
.x
= 0;
4760 state
->viewport
.y
= 0;
4761 state
->viewport
.width
= view
->width
;
4762 state
->viewport
.height
= view
->height
;
4763 wined3d_cs_emit_set_viewport(device
->cs
, &state
->viewport
);
4765 SetRect(&state
->scissor_rect
, 0, 0, view
->width
, view
->height
);
4766 wined3d_cs_emit_set_scissor_rect(device
->cs
, &state
->scissor_rect
);
4769 if (device
->d3d_initialized
)
4772 hr
= wined3d_device_create_primary_opengl_context(device
);
4773 swapchain_update_swap_interval(swapchain
);
4776 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4782 HRESULT CDECL
wined3d_device_set_dialog_box_mode(struct wined3d_device
*device
, BOOL enable_dialogs
)
4784 TRACE("device %p, enable_dialogs %#x.\n", device
, enable_dialogs
);
4786 if (!enable_dialogs
) FIXME("Dialogs cannot be disabled yet.\n");
4792 void CDECL
wined3d_device_get_creation_parameters(const struct wined3d_device
*device
,
4793 struct wined3d_device_creation_parameters
*parameters
)
4795 TRACE("device %p, parameters %p.\n", device
, parameters
);
4797 *parameters
= device
->create_parms
;
4800 struct wined3d
* CDECL
wined3d_device_get_wined3d(const struct wined3d_device
*device
)
4802 TRACE("device %p.\n", device
);
4804 return device
->wined3d
;
4807 void CDECL
wined3d_device_set_gamma_ramp(const struct wined3d_device
*device
,
4808 UINT swapchain_idx
, DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
4810 struct wined3d_swapchain
*swapchain
;
4812 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4813 device
, swapchain_idx
, flags
, ramp
);
4815 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4816 wined3d_swapchain_set_gamma_ramp(swapchain
, flags
, ramp
);
4819 void CDECL
wined3d_device_get_gamma_ramp(const struct wined3d_device
*device
,
4820 UINT swapchain_idx
, struct wined3d_gamma_ramp
*ramp
)
4822 struct wined3d_swapchain
*swapchain
;
4824 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4825 device
, swapchain_idx
, ramp
);
4827 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4828 wined3d_swapchain_get_gamma_ramp(swapchain
, ramp
);
4831 void device_resource_add(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
4833 TRACE("device %p, resource %p.\n", device
, resource
);
4835 list_add_head(&device
->resources
, &resource
->resource_list_entry
);
4838 static void device_resource_remove(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
4840 TRACE("device %p, resource %p.\n", device
, resource
);
4842 list_remove(&resource
->resource_list_entry
);
4845 void device_resource_released(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
4847 enum wined3d_resource_type type
= resource
->type
;
4848 struct wined3d_rendertarget_view
*rtv
;
4851 TRACE("device %p, resource %p, type %s.\n", device
, resource
, debug_d3dresourcetype(type
));
4853 if (device
->d3d_initialized
)
4855 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
4857 if ((rtv
= device
->fb
.render_targets
[i
]) && rtv
->resource
== resource
)
4858 ERR("Resource %p is still in use as render target %u.\n", resource
, i
);
4861 if ((rtv
= device
->fb
.depth_stencil
) && rtv
->resource
== resource
)
4862 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource
);
4867 case WINED3D_RTYPE_TEXTURE_2D
:
4868 case WINED3D_RTYPE_TEXTURE_3D
:
4869 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
4871 struct wined3d_texture
*texture
= texture_from_resource(resource
);
4873 if (device
->state
.textures
[i
] == texture
)
4875 ERR("Texture %p is still in use, stage %u.\n", texture
, i
);
4876 device
->state
.textures
[i
] = NULL
;
4879 if (device
->recording
&& device
->update_state
->textures
[i
] == texture
)
4881 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4882 texture
, device
->recording
, i
);
4883 device
->update_state
->textures
[i
] = NULL
;
4888 case WINED3D_RTYPE_BUFFER
:
4890 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
4892 for (i
= 0; i
< MAX_STREAMS
; ++i
)
4894 if (device
->state
.streams
[i
].buffer
== buffer
)
4896 ERR("Buffer %p is still in use, stream %u.\n", buffer
, i
);
4897 device
->state
.streams
[i
].buffer
= NULL
;
4900 if (device
->recording
&& device
->update_state
->streams
[i
].buffer
== buffer
)
4902 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4903 buffer
, device
->recording
, i
);
4904 device
->update_state
->streams
[i
].buffer
= NULL
;
4908 if (device
->state
.index_buffer
== buffer
)
4910 ERR("Buffer %p is still in use as index buffer.\n", buffer
);
4911 device
->state
.index_buffer
= NULL
;
4914 if (device
->recording
&& device
->update_state
->index_buffer
== buffer
)
4916 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4917 buffer
, device
->recording
);
4918 device
->update_state
->index_buffer
= NULL
;
4927 /* Remove the resource from the resourceStore */
4928 device_resource_remove(device
, resource
);
4930 TRACE("Resource released.\n");
4933 static int wined3d_sampler_compare(const void *key
, const struct wine_rb_entry
*entry
)
4935 const struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
4937 return memcmp(&sampler
->desc
, key
, sizeof(sampler
->desc
));
4940 HRESULT
device_init(struct wined3d_device
*device
, struct wined3d
*wined3d
,
4941 UINT adapter_idx
, enum wined3d_device_type device_type
, HWND focus_window
, DWORD flags
,
4942 BYTE surface_alignment
, struct wined3d_device_parent
*device_parent
)
4944 struct wined3d_adapter
*adapter
= &wined3d
->adapters
[adapter_idx
];
4945 const struct fragment_pipeline
*fragment_pipeline
;
4946 const struct wined3d_vertex_pipe_ops
*vertex_pipeline
;
4951 device
->wined3d
= wined3d
;
4952 wined3d_incref(device
->wined3d
);
4953 device
->adapter
= wined3d
->adapter_count
? adapter
: NULL
;
4954 device
->device_parent
= device_parent
;
4955 list_init(&device
->resources
);
4956 list_init(&device
->shaders
);
4957 device
->surface_alignment
= surface_alignment
;
4959 /* Save the creation parameters. */
4960 device
->create_parms
.adapter_idx
= adapter_idx
;
4961 device
->create_parms
.device_type
= device_type
;
4962 device
->create_parms
.focus_window
= focus_window
;
4963 device
->create_parms
.flags
= flags
;
4965 device
->shader_backend
= adapter
->shader_backend
;
4967 vertex_pipeline
= adapter
->vertex_pipe
;
4969 fragment_pipeline
= adapter
->fragment_pipe
;
4971 wine_rb_init(&device
->samplers
, wined3d_sampler_compare
);
4973 if (vertex_pipeline
->vp_states
&& fragment_pipeline
->states
4974 && FAILED(hr
= compile_state_table(device
->StateTable
, device
->multistate_funcs
,
4975 &adapter
->gl_info
, &adapter
->d3d_info
, vertex_pipeline
,
4976 fragment_pipeline
, misc_state_template
)))
4978 ERR("Failed to compile state table, hr %#x.\n", hr
);
4979 wine_rb_destroy(&device
->samplers
, NULL
, NULL
);
4980 wined3d_decref(device
->wined3d
);
4984 device
->blitter
= adapter
->blitter
;
4986 state_init(&device
->state
, &device
->fb
, &adapter
->gl_info
,
4987 &adapter
->d3d_info
, WINED3D_STATE_INIT_DEFAULT
);
4988 device
->update_state
= &device
->state
;
4990 if (!(device
->cs
= wined3d_cs_create(device
)))
4992 WARN("Failed to create command stream.\n");
4993 state_cleanup(&device
->state
);
5001 for (i
= 0; i
< sizeof(device
->multistate_funcs
) / sizeof(device
->multistate_funcs
[0]); ++i
)
5003 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
5005 wine_rb_destroy(&device
->samplers
, NULL
, NULL
);
5006 wined3d_decref(device
->wined3d
);
5010 void device_invalidate_state(const struct wined3d_device
*device
, DWORD state
)
5012 DWORD rep
= device
->StateTable
[state
].representative
;
5013 struct wined3d_context
*context
;
5018 if (STATE_IS_COMPUTE(state
))
5020 for (i
= 0; i
< device
->context_count
; ++i
)
5021 context_invalidate_compute_state(device
->contexts
[i
], state
);
5025 for (i
= 0; i
< device
->context_count
; ++i
)
5027 context
= device
->contexts
[i
];
5028 if(isStateDirty(context
, rep
)) continue;
5030 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
5031 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
5032 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
5033 context
->isStateDirty
[idx
] |= (1u << shift
);
5037 LRESULT
device_process_message(struct wined3d_device
*device
, HWND window
, BOOL unicode
,
5038 UINT message
, WPARAM wparam
, LPARAM lparam
, WNDPROC proc
)
5040 if (device
->filter_messages
&& message
!= WM_DISPLAYCHANGE
)
5042 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5043 window
, message
, wparam
, lparam
);
5045 return DefWindowProcW(window
, message
, wparam
, lparam
);
5047 return DefWindowProcA(window
, message
, wparam
, lparam
);
5050 if (message
== WM_DESTROY
)
5052 TRACE("unregister window %p.\n", window
);
5053 wined3d_unregister_window(window
);
5055 if (InterlockedCompareExchangePointer((void **)&device
->focus_window
, NULL
, window
) != window
)
5056 ERR("Window %p is not the focus window for device %p.\n", window
, device
);
5058 else if (message
== WM_DISPLAYCHANGE
)
5060 device
->device_parent
->ops
->mode_changed(device
->device_parent
);
5062 else if (message
== WM_ACTIVATEAPP
)
5066 for (i
= 0; i
< device
->swapchain_count
; i
++)
5067 wined3d_swapchain_activate(device
->swapchains
[i
], wparam
);
5069 device
->device_parent
->ops
->activate(device
->device_parent
, wparam
);
5071 else if (message
== WM_SYSCOMMAND
)
5073 if (wparam
== SC_RESTORE
&& device
->wined3d
->flags
& WINED3D_HANDLE_RESTORE
)
5076 DefWindowProcW(window
, message
, wparam
, lparam
);
5078 DefWindowProcA(window
, message
, wparam
, lparam
);
5083 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
5085 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);