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
;
92 case WINED3D_PT_PATCH
:
96 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
97 case WINED3D_PT_UNDEFINED
:
102 static enum wined3d_primitive_type
d3d_primitive_type_from_gl(GLenum primitive_type
)
104 switch (primitive_type
)
107 return WINED3D_PT_POINTLIST
;
110 return WINED3D_PT_LINELIST
;
113 return WINED3D_PT_LINESTRIP
;
116 return WINED3D_PT_TRIANGLELIST
;
118 case GL_TRIANGLE_STRIP
:
119 return WINED3D_PT_TRIANGLESTRIP
;
121 case GL_TRIANGLE_FAN
:
122 return WINED3D_PT_TRIANGLEFAN
;
124 case GL_LINES_ADJACENCY_ARB
:
125 return WINED3D_PT_LINELIST_ADJ
;
127 case GL_LINE_STRIP_ADJACENCY_ARB
:
128 return WINED3D_PT_LINESTRIP_ADJ
;
130 case GL_TRIANGLES_ADJACENCY_ARB
:
131 return WINED3D_PT_TRIANGLELIST_ADJ
;
133 case GL_TRIANGLE_STRIP_ADJACENCY_ARB
:
134 return WINED3D_PT_TRIANGLESTRIP_ADJ
;
137 return WINED3D_PT_PATCH
;
140 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
142 return WINED3D_PT_UNDEFINED
;
146 BOOL
device_context_add(struct wined3d_device
*device
, struct wined3d_context
*context
)
148 struct wined3d_context
**new_array
;
150 TRACE("Adding context %p.\n", context
);
152 if (!device
->contexts
) new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
153 else new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
,
154 sizeof(*new_array
) * (device
->context_count
+ 1));
158 ERR("Failed to grow the context array.\n");
162 new_array
[device
->context_count
++] = context
;
163 device
->contexts
= new_array
;
167 void device_context_remove(struct wined3d_device
*device
, struct wined3d_context
*context
)
169 struct wined3d_context
**new_array
;
173 TRACE("Removing context %p.\n", context
);
175 for (i
= 0; i
< device
->context_count
; ++i
)
177 if (device
->contexts
[i
] == context
)
186 ERR("Context %p doesn't exist in context array.\n", context
);
190 if (!--device
->context_count
)
192 HeapFree(GetProcessHeap(), 0, device
->contexts
);
193 device
->contexts
= NULL
;
197 memmove(&device
->contexts
[i
], &device
->contexts
[i
+ 1], (device
->context_count
- i
) * sizeof(*device
->contexts
));
198 new_array
= HeapReAlloc(GetProcessHeap(), 0, device
->contexts
, device
->context_count
* sizeof(*device
->contexts
));
201 ERR("Failed to shrink context array. Oh well.\n");
205 device
->contexts
= new_array
;
208 static BOOL
is_full_clear(const struct wined3d_surface
*target
, const RECT
*draw_rect
, const RECT
*clear_rect
)
210 unsigned int height
= wined3d_texture_get_level_height(target
->container
, target
->texture_level
);
211 unsigned int width
= wined3d_texture_get_level_width(target
->container
, target
->texture_level
);
213 /* partial draw rect */
214 if (draw_rect
->left
|| draw_rect
->top
|| draw_rect
->right
< width
|| draw_rect
->bottom
< height
)
217 /* partial clear rect */
218 if (clear_rect
&& (clear_rect
->left
> 0 || clear_rect
->top
> 0
219 || clear_rect
->right
< width
|| clear_rect
->bottom
< height
))
225 void device_clear_render_targets(struct wined3d_device
*device
, UINT rt_count
, const struct wined3d_fb_state
*fb
,
226 UINT rect_count
, const RECT
*clear_rect
, const RECT
*draw_rect
, DWORD flags
, const struct wined3d_color
*color
,
227 float depth
, DWORD stencil
)
229 struct wined3d_rendertarget_view
*rtv
= rt_count
? fb
->render_targets
[0] : NULL
;
230 struct wined3d_surface
*target
= rtv
? wined3d_rendertarget_view_get_surface(rtv
) : NULL
;
231 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
232 struct wined3d_surface
*depth_stencil
= dsv
? wined3d_rendertarget_view_get_surface(dsv
) : NULL
;
233 const struct wined3d_state
*state
= &device
->cs
->state
;
234 const struct wined3d_gl_info
*gl_info
;
235 UINT drawable_width
, drawable_height
;
236 struct wined3d_color corrected_color
;
237 struct wined3d_context
*context
;
238 GLbitfield clear_mask
= 0;
239 BOOL render_offscreen
;
243 context
= context_acquire(device
, target
->container
, rtv
->sub_resource_idx
);
245 context
= context_acquire(device
, NULL
, 0);
248 context_release(context
);
249 WARN("Invalid context, skipping clear.\n");
252 gl_info
= context
->gl_info
;
254 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
255 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
256 * for the cleared parts, and the untouched parts.
258 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
259 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
260 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
261 * checking all this if the dest surface is in the drawable anyway. */
262 for (i
= 0; i
< rt_count
; ++i
)
264 struct wined3d_rendertarget_view
*rtv
= fb
->render_targets
[i
];
266 if (rtv
&& rtv
->format
->id
!= WINED3DFMT_NULL
)
268 struct wined3d_texture
*rt
= wined3d_texture_from_resource(rtv
->resource
);
270 if (flags
& WINED3DCLEAR_TARGET
&& !is_full_clear(target
, draw_rect
, rect_count
? clear_rect
: NULL
))
271 wined3d_texture_load_location(rt
, rtv
->sub_resource_idx
, context
, rtv
->resource
->draw_binding
);
273 wined3d_texture_prepare_location(rt
, rtv
->sub_resource_idx
, context
, rtv
->resource
->draw_binding
);
279 render_offscreen
= context
->render_offscreen
;
280 wined3d_rendertarget_view_get_drawable_size(rtv
, context
, &drawable_width
, &drawable_height
);
284 render_offscreen
= TRUE
;
285 drawable_width
= wined3d_texture_get_level_pow2_width(depth_stencil
->container
,
286 depth_stencil
->texture_level
);
287 drawable_height
= wined3d_texture_get_level_pow2_height(depth_stencil
->container
,
288 depth_stencil
->texture_level
);
291 if (depth_stencil
&& render_offscreen
)
292 wined3d_texture_prepare_location(depth_stencil
->container
,
293 dsv
->sub_resource_idx
, context
, dsv
->resource
->draw_binding
);
295 if (flags
& WINED3DCLEAR_ZBUFFER
)
297 DWORD location
= render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
299 wined3d_texture_load_location(depth_stencil
->container
,
300 dsv
->sub_resource_idx
, context
, location
);
303 if (!context_apply_clear_state(context
, state
, rt_count
, fb
))
305 context_release(context
);
306 WARN("Failed to apply clear state, skipping clear.\n");
310 /* Only set the values up once, as they are not changing. */
311 if (flags
& WINED3DCLEAR_STENCIL
)
313 if (gl_info
->supported
[EXT_STENCIL_TWO_SIDE
])
315 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT
);
316 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE
));
318 gl_info
->gl_ops
.gl
.p_glStencilMask(~0U);
319 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK
));
320 gl_info
->gl_ops
.gl
.p_glClearStencil(stencil
);
321 checkGLcall("glClearStencil");
322 clear_mask
= clear_mask
| GL_STENCIL_BUFFER_BIT
;
325 if (flags
& WINED3DCLEAR_ZBUFFER
)
327 DWORD location
= render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
329 wined3d_texture_validate_location(depth_stencil
->container
, dsv
->sub_resource_idx
, location
);
330 wined3d_texture_invalidate_location(depth_stencil
->container
, dsv
->sub_resource_idx
, ~location
);
332 gl_info
->gl_ops
.gl
.p_glDepthMask(GL_TRUE
);
333 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZWRITEENABLE
));
334 gl_info
->gl_ops
.gl
.p_glClearDepth(depth
);
335 checkGLcall("glClearDepth");
336 clear_mask
= clear_mask
| GL_DEPTH_BUFFER_BIT
;
339 if (flags
& WINED3DCLEAR_TARGET
)
341 for (i
= 0; i
< rt_count
; ++i
)
343 struct wined3d_rendertarget_view
*rtv
= fb
->render_targets
[i
];
344 struct wined3d_texture
*texture
;
349 if (rtv
->resource
->type
== WINED3D_RTYPE_BUFFER
)
351 FIXME("Not supported on buffer resources.\n");
355 texture
= texture_from_resource(rtv
->resource
);
356 wined3d_texture_validate_location(texture
, rtv
->sub_resource_idx
, rtv
->resource
->draw_binding
);
357 wined3d_texture_invalidate_location(texture
, rtv
->sub_resource_idx
, ~rtv
->resource
->draw_binding
);
360 if (!gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
] && needs_srgb_write(context
, state
, fb
))
363 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
364 "support, this might cause graphical issues.\n");
366 corrected_color
.r
= color
->r
< wined3d_srgb_const1
[0]
367 ? color
->r
* wined3d_srgb_const0
[3]
368 : pow(color
->r
, wined3d_srgb_const0
[0]) * wined3d_srgb_const0
[1]
369 - wined3d_srgb_const0
[2];
370 corrected_color
.r
= min(max(corrected_color
.r
, 0.0f
), 1.0f
);
371 corrected_color
.g
= color
->g
< wined3d_srgb_const1
[0]
372 ? color
->g
* wined3d_srgb_const0
[3]
373 : pow(color
->g
, wined3d_srgb_const0
[0]) * wined3d_srgb_const0
[1]
374 - wined3d_srgb_const0
[2];
375 corrected_color
.g
= min(max(corrected_color
.g
, 0.0f
), 1.0f
);
376 corrected_color
.b
= color
->b
< wined3d_srgb_const1
[0]
377 ? color
->b
* wined3d_srgb_const0
[3]
378 : pow(color
->b
, wined3d_srgb_const0
[0]) * wined3d_srgb_const0
[1]
379 - wined3d_srgb_const0
[2];
380 corrected_color
.b
= min(max(corrected_color
.b
, 0.0f
), 1.0f
);
381 color
= &corrected_color
;
384 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
385 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
386 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
387 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
388 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
389 gl_info
->gl_ops
.gl
.p_glClearColor(color
->r
, color
->g
, color
->b
, color
->a
);
390 checkGLcall("glClearColor");
391 clear_mask
= clear_mask
| GL_COLOR_BUFFER_BIT
;
396 if (render_offscreen
)
398 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, draw_rect
->top
,
399 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
403 gl_info
->gl_ops
.gl
.p_glScissor(draw_rect
->left
, drawable_height
- draw_rect
->bottom
,
404 draw_rect
->right
- draw_rect
->left
, draw_rect
->bottom
- draw_rect
->top
);
406 checkGLcall("glScissor");
407 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
408 checkGLcall("glClear");
414 /* Now process each rect in turn. */
415 for (i
= 0; i
< rect_count
; ++i
)
417 /* Note that GL uses lower left, width/height. */
418 IntersectRect(¤t_rect
, draw_rect
, &clear_rect
[i
]);
420 TRACE("clear_rect[%u] %s, current_rect %s.\n", i
,
421 wine_dbgstr_rect(&clear_rect
[i
]),
422 wine_dbgstr_rect(¤t_rect
));
424 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
425 * The rectangle is not cleared, no error is returned, but further rectangles are
426 * still cleared if they are valid. */
427 if (current_rect
.left
> current_rect
.right
|| current_rect
.top
> current_rect
.bottom
)
429 TRACE("Rectangle with negative dimensions, ignoring.\n");
433 if (render_offscreen
)
435 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, current_rect
.top
,
436 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
440 gl_info
->gl_ops
.gl
.p_glScissor(current_rect
.left
, drawable_height
- current_rect
.bottom
,
441 current_rect
.right
- current_rect
.left
, current_rect
.bottom
- current_rect
.top
);
443 checkGLcall("glScissor");
445 gl_info
->gl_ops
.gl
.p_glClear(clear_mask
);
446 checkGLcall("glClear");
450 if (wined3d_settings
.strict_draw_ordering
|| (flags
& WINED3DCLEAR_TARGET
451 && target
->container
->swapchain
&& target
->container
->swapchain
->front_buffer
== target
->container
))
452 gl_info
->gl_ops
.gl
.p_glFlush(); /* Flush to ensure ordering across contexts. */
454 context_release(context
);
457 ULONG CDECL
wined3d_device_incref(struct wined3d_device
*device
)
459 ULONG refcount
= InterlockedIncrement(&device
->ref
);
461 TRACE("%p increasing refcount to %u.\n", device
, refcount
);
466 static void device_leftover_sampler(struct wine_rb_entry
*entry
, void *context
)
468 struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
470 ERR("Leftover sampler %p.\n", sampler
);
473 ULONG CDECL
wined3d_device_decref(struct wined3d_device
*device
)
475 ULONG refcount
= InterlockedDecrement(&device
->ref
);
477 TRACE("%p decreasing refcount to %u.\n", device
, refcount
);
483 wined3d_cs_destroy(device
->cs
);
485 if (device
->recording
&& wined3d_stateblock_decref(device
->recording
))
486 ERR("Something's still holding the recording stateblock.\n");
487 device
->recording
= NULL
;
489 state_cleanup(&device
->state
);
491 for (i
= 0; i
< ARRAY_SIZE(device
->multistate_funcs
); ++i
)
493 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
494 device
->multistate_funcs
[i
] = NULL
;
497 if (!list_empty(&device
->resources
))
499 struct wined3d_resource
*resource
;
501 ERR("Device released with resources still bound.\n");
503 LIST_FOR_EACH_ENTRY(resource
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
505 ERR("Leftover resource %p with type %s (%#x).\n",
506 resource
, debug_d3dresourcetype(resource
->type
), resource
->type
);
510 if (device
->contexts
)
511 ERR("Context array not freed!\n");
512 if (device
->hardwareCursor
)
513 DestroyCursor(device
->hardwareCursor
);
514 device
->hardwareCursor
= 0;
516 wine_rb_destroy(&device
->samplers
, device_leftover_sampler
, NULL
);
518 wined3d_decref(device
->wined3d
);
519 device
->wined3d
= NULL
;
520 HeapFree(GetProcessHeap(), 0, device
);
521 TRACE("Freed device %p.\n", device
);
527 UINT CDECL
wined3d_device_get_swapchain_count(const struct wined3d_device
*device
)
529 TRACE("device %p.\n", device
);
531 return device
->swapchain_count
;
534 struct wined3d_swapchain
* CDECL
wined3d_device_get_swapchain(const struct wined3d_device
*device
, UINT swapchain_idx
)
536 TRACE("device %p, swapchain_idx %u.\n", device
, swapchain_idx
);
538 if (swapchain_idx
>= device
->swapchain_count
)
540 WARN("swapchain_idx %u >= swapchain_count %u.\n",
541 swapchain_idx
, device
->swapchain_count
);
545 return device
->swapchains
[swapchain_idx
];
548 static void device_load_logo(struct wined3d_device
*device
, const char *filename
)
550 struct wined3d_color_key color_key
;
551 struct wined3d_resource_desc desc
;
555 HDC dcb
= NULL
, dcs
= NULL
;
557 if (!(hbm
= LoadImageA(NULL
, filename
, IMAGE_BITMAP
, 0, 0, LR_LOADFROMFILE
| LR_CREATEDIBSECTION
)))
559 ERR_(winediag
)("Failed to load logo %s.\n", wine_dbgstr_a(filename
));
562 GetObjectA(hbm
, sizeof(BITMAP
), &bm
);
564 if (!(dcb
= CreateCompatibleDC(NULL
)))
566 SelectObject(dcb
, hbm
);
568 desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
569 desc
.format
= WINED3DFMT_B5G6R5_UNORM
;
570 desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
571 desc
.multisample_quality
= 0;
572 desc
.usage
= WINED3DUSAGE_DYNAMIC
;
573 desc
.pool
= WINED3D_POOL_DEFAULT
;
574 desc
.width
= bm
.bmWidth
;
575 desc
.height
= bm
.bmHeight
;
578 if (FAILED(hr
= wined3d_texture_create(device
, &desc
, 1, 1,
579 WINED3D_TEXTURE_CREATE_MAPPABLE
| WINED3D_TEXTURE_CREATE_GET_DC
,
580 NULL
, NULL
, &wined3d_null_parent_ops
, &device
->logo_texture
)))
582 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr
);
586 if (FAILED(hr
= wined3d_texture_get_dc(device
->logo_texture
, 0, &dcs
)))
588 wined3d_texture_decref(device
->logo_texture
);
589 device
->logo_texture
= NULL
;
592 BitBlt(dcs
, 0, 0, bm
.bmWidth
, bm
.bmHeight
, dcb
, 0, 0, SRCCOPY
);
593 wined3d_texture_release_dc(device
->logo_texture
, 0, dcs
);
595 color_key
.color_space_low_value
= 0;
596 color_key
.color_space_high_value
= 0;
597 wined3d_texture_set_color_key(device
->logo_texture
, WINED3D_CKEY_SRC_BLT
, &color_key
);
600 if (dcb
) DeleteDC(dcb
);
601 if (hbm
) DeleteObject(hbm
);
604 /* Context activation is done by the caller. */
605 static void create_dummy_textures(struct wined3d_device
*device
, struct wined3d_context
*context
)
607 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
608 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
612 if (d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR
)
617 /* Under DirectX you can sample even if no texture is bound, whereas
618 * OpenGL will only allow that when a valid texture is bound.
619 * We emulate this by creating dummy textures and binding them
620 * to each texture stage when the currently set D3D texture is NULL. */
621 context_active_texture(context
, gl_info
, 0);
623 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_2d
);
624 checkGLcall("glGenTextures");
625 TRACE("Dummy 2D texture given name %u.\n", device
->dummy_textures
.tex_2d
);
627 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_textures
.tex_2d
);
628 checkGLcall("glBindTexture");
630 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA8
, 1, 1, 0,
631 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
632 checkGLcall("glTexImage2D");
634 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
636 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_rect
);
637 checkGLcall("glGenTextures");
638 TRACE("Dummy rectangle texture given name %u.\n", device
->dummy_textures
.tex_rect
);
640 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_textures
.tex_rect
);
641 checkGLcall("glBindTexture");
643 gl_info
->gl_ops
.gl
.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA8
, 1, 1, 0,
644 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
645 checkGLcall("glTexImage2D");
648 if (gl_info
->supported
[EXT_TEXTURE3D
])
650 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_3d
);
651 checkGLcall("glGenTextures");
652 TRACE("Dummy 3D texture given name %u.\n", device
->dummy_textures
.tex_3d
);
654 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_textures
.tex_3d
);
655 checkGLcall("glBindTexture");
657 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D
, 0, GL_RGBA8
, 1, 1, 1, 0,
658 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
));
659 checkGLcall("glTexImage3D");
662 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
664 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_cube
);
665 checkGLcall("glGenTextures");
666 TRACE("Dummy cube texture given name %u.\n", device
->dummy_textures
.tex_cube
);
668 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_textures
.tex_cube
);
669 checkGLcall("glBindTexture");
671 for (i
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
; i
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
; ++i
)
673 gl_info
->gl_ops
.gl
.p_glTexImage2D(i
, 0, GL_RGBA8
, 1, 1, 0,
674 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
);
675 checkGLcall("glTexImage2D");
679 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
681 DWORD cube_array_data
[6];
683 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_cube_array
);
684 checkGLcall("glGenTextures");
685 TRACE("Dummy cube array texture given name %u.\n", device
->dummy_textures
.tex_cube_array
);
687 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, device
->dummy_textures
.tex_cube_array
);
688 checkGLcall("glBindTexture");
690 for (i
= 0; i
< ARRAY_SIZE(cube_array_data
); ++i
)
691 cube_array_data
[i
] = color
;
692 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY
, 0, GL_RGBA8
, 1, 1, 6, 0,
693 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, cube_array_data
));
694 checkGLcall("glTexImage3D");
697 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
699 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_2d_array
);
700 checkGLcall("glGenTextures");
701 TRACE("Dummy 2D array texture given name %u.\n", device
->dummy_textures
.tex_2d_array
);
703 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_textures
.tex_2d_array
);
704 checkGLcall("glBindTexture");
706 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY
, 0, GL_RGBA8
, 1, 1, 1, 0,
707 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, &color
));
708 checkGLcall("glTexImage3D");
711 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
715 GL_EXTCALL(glGenBuffers(1, &buffer
));
716 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER
, buffer
));
717 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER
, sizeof(color
), &color
, GL_STATIC_DRAW
));
718 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER
, 0));
719 checkGLcall("Create buffer object");
721 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &device
->dummy_textures
.tex_buffer
);
722 checkGLcall("glGenTextures");
723 TRACE("Dummy buffer texture given name %u.\n", device
->dummy_textures
.tex_buffer
);
725 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, device
->dummy_textures
.tex_buffer
);
726 checkGLcall("glBindTexture");
727 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER
, GL_RGBA8
, buffer
));
728 checkGLcall("glTexBuffer");
730 GL_EXTCALL(glDeleteBuffers(1, &buffer
));
731 checkGLcall("glDeleteBuffers");
734 context_bind_dummy_textures(device
, context
);
737 /* Context activation is done by the caller. */
738 static void destroy_dummy_textures(struct wined3d_device
*device
, struct wined3d_context
*context
)
740 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
742 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
743 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_buffer
);
745 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
746 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_2d_array
);
748 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
749 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_cube_array
);
751 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
752 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_cube
);
754 if (gl_info
->supported
[EXT_TEXTURE3D
])
755 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_3d
);
757 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
758 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_rect
);
760 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &device
->dummy_textures
.tex_2d
);
762 checkGLcall("Delete dummy textures");
764 memset(&device
->dummy_textures
, 0, sizeof(device
->dummy_textures
));
767 /* Context activation is done by the caller. */
768 static void create_default_samplers(struct wined3d_device
*device
, struct wined3d_context
*context
)
770 struct wined3d_sampler_desc desc
;
773 desc
.address_u
= WINED3D_TADDRESS_WRAP
;
774 desc
.address_v
= WINED3D_TADDRESS_WRAP
;
775 desc
.address_w
= WINED3D_TADDRESS_WRAP
;
776 memset(desc
.border_color
, 0, sizeof(desc
.border_color
));
777 desc
.mag_filter
= WINED3D_TEXF_POINT
;
778 desc
.min_filter
= WINED3D_TEXF_POINT
;
779 desc
.mip_filter
= WINED3D_TEXF_NONE
;
780 desc
.lod_bias
= 0.0f
;
781 desc
.min_lod
= -1000.0f
;
782 desc
.max_lod
= 1000.0f
;
783 desc
.mip_base_level
= 0;
784 desc
.max_anisotropy
= 1;
785 desc
.compare
= FALSE
;
786 desc
.comparison_func
= WINED3D_CMP_NEVER
;
787 desc
.srgb_decode
= TRUE
;
789 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
790 * instructions allow access to resources without using samplers.
791 * In GLSL, resources are always accessed through sampler or image variables. The default
792 * sampler object is used to emulate the direct resource access when there is no sampler state
795 if (FAILED(hr
= wined3d_sampler_create(device
, &desc
, NULL
, &wined3d_null_parent_ops
, &device
->default_sampler
)))
797 ERR("Failed to create default sampler, hr %#x.\n", hr
);
798 device
->default_sampler
= NULL
;
801 /* In D3D10+, a NULL sampler maps to the default sampler state. */
802 desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
803 desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
804 desc
.address_w
= WINED3D_TADDRESS_CLAMP
;
805 desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
806 desc
.min_filter
= WINED3D_TEXF_LINEAR
;
807 desc
.mip_filter
= WINED3D_TEXF_LINEAR
;
808 if (FAILED(hr
= wined3d_sampler_create(device
, &desc
, NULL
, &wined3d_null_parent_ops
, &device
->null_sampler
)))
810 ERR("Failed to create null sampler, hr %#x.\n", hr
);
811 device
->null_sampler
= NULL
;
815 /* Context activation is done by the caller. */
816 static void destroy_default_samplers(struct wined3d_device
*device
, struct wined3d_context
*context
)
818 wined3d_sampler_decref(device
->default_sampler
);
819 device
->default_sampler
= NULL
;
820 wined3d_sampler_decref(device
->null_sampler
);
821 device
->null_sampler
= NULL
;
824 static LONG
fullscreen_style(LONG style
)
826 /* Make sure the window is managed, otherwise we won't get keyboard input. */
827 style
|= WS_POPUP
| WS_SYSMENU
;
828 style
&= ~(WS_CAPTION
| WS_THICKFRAME
);
833 static LONG
fullscreen_exstyle(LONG exstyle
)
835 /* Filter out window decorations. */
836 exstyle
&= ~(WS_EX_WINDOWEDGE
| WS_EX_CLIENTEDGE
);
841 void CDECL
wined3d_device_setup_fullscreen_window(struct wined3d_device
*device
, HWND window
, UINT w
, UINT h
)
843 BOOL filter_messages
;
846 TRACE("Setting up window %p for fullscreen mode.\n", window
);
848 if (device
->style
|| device
->exStyle
)
850 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
851 window
, device
->style
, device
->exStyle
);
854 device
->style
= GetWindowLongW(window
, GWL_STYLE
);
855 device
->exStyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
857 style
= fullscreen_style(device
->style
);
858 exstyle
= fullscreen_exstyle(device
->exStyle
);
860 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
861 device
->style
, device
->exStyle
, style
, exstyle
);
863 filter_messages
= device
->filter_messages
;
864 device
->filter_messages
= TRUE
;
866 SetWindowLongW(window
, GWL_STYLE
, style
);
867 SetWindowLongW(window
, GWL_EXSTYLE
, exstyle
);
868 SetWindowPos(window
, HWND_TOPMOST
, 0, 0, w
, h
, SWP_FRAMECHANGED
| SWP_SHOWWINDOW
| SWP_NOACTIVATE
);
870 device
->filter_messages
= filter_messages
;
873 void CDECL
wined3d_device_restore_fullscreen_window(struct wined3d_device
*device
, HWND window
,
874 const RECT
*window_rect
)
876 unsigned int window_pos_flags
= SWP_FRAMECHANGED
| SWP_NOZORDER
| SWP_NOACTIVATE
;
877 BOOL filter_messages
;
881 if (!device
->style
&& !device
->exStyle
)
884 style
= GetWindowLongW(window
, GWL_STYLE
);
885 exstyle
= GetWindowLongW(window
, GWL_EXSTYLE
);
887 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
888 * application, and we want to ignore them in the test below, since it's
889 * not the application's fault that they changed. Additionally, we want to
890 * preserve the current status of these flags (i.e. don't restore them) to
891 * more closely emulate the behavior of Direct3D, which leaves these flags
892 * alone when returning to windowed mode. */
893 device
->style
^= (device
->style
^ style
) & WS_VISIBLE
;
894 device
->exStyle
^= (device
->exStyle
^ exstyle
) & WS_EX_TOPMOST
;
896 TRACE("Restoring window style of window %p to %08x, %08x.\n",
897 window
, device
->style
, device
->exStyle
);
899 filter_messages
= device
->filter_messages
;
900 device
->filter_messages
= TRUE
;
902 /* Only restore the style if the application didn't modify it during the
903 * fullscreen phase. Some applications change it before calling Reset()
904 * when switching between windowed and fullscreen modes (HL2), some
905 * depend on the original style (Eve Online). */
906 if (style
== fullscreen_style(device
->style
) && exstyle
== fullscreen_exstyle(device
->exStyle
))
908 SetWindowLongW(window
, GWL_STYLE
, device
->style
);
909 SetWindowLongW(window
, GWL_EXSTYLE
, device
->exStyle
);
915 window_pos_flags
|= (SWP_NOMOVE
| SWP_NOSIZE
);
916 SetWindowPos(window
, 0, rect
.left
, rect
.top
,
917 rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, window_pos_flags
);
919 device
->filter_messages
= filter_messages
;
921 /* Delete the old values. */
926 HRESULT CDECL
wined3d_device_acquire_focus_window(struct wined3d_device
*device
, HWND window
)
928 TRACE("device %p, window %p.\n", device
, window
);
930 if (!wined3d_register_window(window
, device
))
932 ERR("Failed to register window %p.\n", window
);
936 InterlockedExchangePointer((void **)&device
->focus_window
, window
);
937 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
);
942 void CDECL
wined3d_device_release_focus_window(struct wined3d_device
*device
)
944 TRACE("device %p.\n", device
);
946 if (device
->focus_window
) wined3d_unregister_window(device
->focus_window
);
947 InterlockedExchangePointer((void **)&device
->focus_window
, NULL
);
950 static void device_init_swapchain_state(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
952 BOOL ds_enable
= swapchain
->desc
.enable_auto_depth_stencil
;
955 if (device
->fb
.render_targets
)
957 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
959 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
961 if (device
->back_buffer_view
)
962 wined3d_device_set_rendertarget_view(device
, 0, device
->back_buffer_view
, TRUE
);
965 wined3d_device_set_depth_stencil_view(device
, ds_enable
? device
->auto_depth_stencil_view
: NULL
);
968 static void wined3d_device_delete_opengl_contexts_cs(void *object
)
970 struct wined3d_resource
*resource
, *cursor
;
971 struct wined3d_device
*device
= object
;
972 struct wined3d_context
*context
;
973 struct wined3d_shader
*shader
;
975 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
977 TRACE("Unloading resource %p.\n", resource
);
978 wined3d_cs_emit_unload_resource(device
->cs
, resource
);
981 LIST_FOR_EACH_ENTRY(shader
, &device
->shaders
, struct wined3d_shader
, shader_list_entry
)
983 device
->shader_backend
->shader_destroy(shader
);
986 context
= context_acquire(device
, NULL
, 0);
987 device
->blitter
->ops
->blitter_destroy(device
->blitter
, context
);
988 device
->shader_backend
->shader_free_private(device
);
989 destroy_dummy_textures(device
, context
);
990 destroy_default_samplers(device
, context
);
991 context_release(context
);
993 while (device
->context_count
)
995 if (device
->contexts
[0]->swapchain
)
996 swapchain_destroy_contexts(device
->contexts
[0]->swapchain
);
998 context_destroy(device
, device
->contexts
[0]);
1002 static void wined3d_device_delete_opengl_contexts(struct wined3d_device
*device
)
1004 wined3d_cs_destroy_object(device
->cs
, wined3d_device_delete_opengl_contexts_cs
, device
);
1005 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1008 static void wined3d_device_create_primary_opengl_context_cs(void *object
)
1010 struct wined3d_device
*device
= object
;
1011 struct wined3d_swapchain
*swapchain
;
1012 struct wined3d_context
*context
;
1013 struct wined3d_texture
*target
;
1016 if (FAILED(hr
= device
->shader_backend
->shader_alloc_private(device
,
1017 device
->adapter
->vertex_pipe
, device
->adapter
->fragment_pipe
)))
1019 ERR("Failed to allocate shader private data, hr %#x.\n", hr
);
1023 if (!(device
->blitter
= wined3d_cpu_blitter_create()))
1025 ERR("Failed to create CPU blitter.\n");
1026 device
->shader_backend
->shader_free_private(device
);
1029 wined3d_ffp_blitter_create(&device
->blitter
, &device
->adapter
->gl_info
);
1030 wined3d_arbfp_blitter_create(&device
->blitter
, device
);
1031 wined3d_fbo_blitter_create(&device
->blitter
, &device
->adapter
->gl_info
);
1032 wined3d_raw_blitter_create(&device
->blitter
, &device
->adapter
->gl_info
);
1034 swapchain
= device
->swapchains
[0];
1035 target
= swapchain
->back_buffers
? swapchain
->back_buffers
[0] : swapchain
->front_buffer
;
1036 context
= context_acquire(device
, target
, 0);
1037 create_dummy_textures(device
, context
);
1038 create_default_samplers(device
, context
);
1039 context_release(context
);
1042 static HRESULT
wined3d_device_create_primary_opengl_context(struct wined3d_device
*device
)
1044 wined3d_cs_init_object(device
->cs
, wined3d_device_create_primary_opengl_context_cs
, device
);
1045 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1046 if (!device
->swapchains
[0]->num_contexts
)
1052 HRESULT CDECL
wined3d_device_init_3d(struct wined3d_device
*device
,
1053 struct wined3d_swapchain_desc
*swapchain_desc
)
1055 static const struct wined3d_color black
= {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1056 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1057 struct wined3d_swapchain
*swapchain
= NULL
;
1058 DWORD clear_flags
= 0;
1061 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1063 if (device
->d3d_initialized
)
1064 return WINED3DERR_INVALIDCALL
;
1065 if (device
->wined3d
->flags
& WINED3D_NO3D
)
1066 return WINED3DERR_INVALIDCALL
;
1068 if (!(device
->fb
.render_targets
= wined3d_calloc(gl_info
->limits
.buffers
, sizeof(*device
->fb
.render_targets
))))
1069 return E_OUTOFMEMORY
;
1071 /* Setup the implicit swapchain. This also initializes a context. */
1072 TRACE("Creating implicit swapchain\n");
1073 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1074 swapchain_desc
, &swapchain
);
1077 WARN("Failed to create implicit swapchain\n");
1081 if (swapchain_desc
->backbuffer_count
)
1083 struct wined3d_resource
*back_buffer
= &swapchain
->back_buffers
[0]->resource
;
1084 struct wined3d_view_desc view_desc
;
1086 view_desc
.format_id
= back_buffer
->format
->id
;
1087 view_desc
.flags
= 0;
1088 view_desc
.u
.texture
.level_idx
= 0;
1089 view_desc
.u
.texture
.level_count
= 1;
1090 view_desc
.u
.texture
.layer_idx
= 0;
1091 view_desc
.u
.texture
.layer_count
= 1;
1092 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
, back_buffer
,
1093 NULL
, &wined3d_null_parent_ops
, &device
->back_buffer_view
)))
1095 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1100 device
->swapchain_count
= 1;
1101 if (!(device
->swapchains
= wined3d_calloc(device
->swapchain_count
, sizeof(*device
->swapchains
))))
1103 ERR("Out of memory!\n");
1106 device
->swapchains
[0] = swapchain
;
1108 if (FAILED(hr
= wined3d_device_create_primary_opengl_context(device
)))
1110 device_init_swapchain_state(device
, swapchain
);
1112 device
->contexts
[0]->last_was_rhw
= 0;
1114 TRACE("All defaults now set up, leaving 3D init.\n");
1116 /* Clear the screen */
1117 if (swapchain
->back_buffers
&& swapchain
->back_buffers
[0])
1118 clear_flags
|= WINED3DCLEAR_TARGET
;
1119 if (swapchain_desc
->enable_auto_depth_stencil
)
1120 clear_flags
|= WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
;
1122 wined3d_device_clear(device
, 0, NULL
, clear_flags
, &black
, 1.0f
, 0);
1124 device
->d3d_initialized
= TRUE
;
1126 if (wined3d_settings
.logo
)
1127 device_load_logo(device
, wined3d_settings
.logo
);
1131 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1132 device
->swapchain_count
= 0;
1133 if (device
->back_buffer_view
)
1134 wined3d_rendertarget_view_decref(device
->back_buffer_view
);
1136 wined3d_swapchain_decref(swapchain
);
1137 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1142 HRESULT CDECL
wined3d_device_init_gdi(struct wined3d_device
*device
,
1143 struct wined3d_swapchain_desc
*swapchain_desc
)
1145 struct wined3d_swapchain
*swapchain
= NULL
;
1148 TRACE("device %p, swapchain_desc %p.\n", device
, swapchain_desc
);
1150 /* Setup the implicit swapchain */
1151 TRACE("Creating implicit swapchain\n");
1152 hr
= device
->device_parent
->ops
->create_swapchain(device
->device_parent
,
1153 swapchain_desc
, &swapchain
);
1156 WARN("Failed to create implicit swapchain\n");
1160 device
->swapchain_count
= 1;
1161 if (!(device
->swapchains
= wined3d_calloc(device
->swapchain_count
, sizeof(*device
->swapchains
))))
1163 ERR("Out of memory!\n");
1166 device
->swapchains
[0] = swapchain
;
1168 if (!(device
->blitter
= wined3d_cpu_blitter_create()))
1170 ERR("Failed to create CPU blitter.\n");
1171 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1172 device
->swapchain_count
= 0;
1179 wined3d_swapchain_decref(swapchain
);
1183 static void device_free_sampler(struct wine_rb_entry
*entry
, void *context
)
1185 struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
1187 wined3d_sampler_decref(sampler
);
1190 HRESULT CDECL
wined3d_device_uninit_3d(struct wined3d_device
*device
)
1194 TRACE("device %p.\n", device
);
1196 if (!device
->d3d_initialized
)
1197 return WINED3DERR_INVALIDCALL
;
1199 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1201 if (device
->logo_texture
)
1202 wined3d_texture_decref(device
->logo_texture
);
1203 if (device
->cursor_texture
)
1204 wined3d_texture_decref(device
->cursor_texture
);
1206 state_unbind_resources(&device
->state
);
1208 wine_rb_clear(&device
->samplers
, device_free_sampler
, NULL
);
1210 wined3d_device_delete_opengl_contexts(device
);
1212 if (device
->fb
.depth_stencil
)
1214 struct wined3d_rendertarget_view
*view
= device
->fb
.depth_stencil
;
1216 TRACE("Releasing depth/stencil view %p.\n", view
);
1218 device
->fb
.depth_stencil
= NULL
;
1219 wined3d_rendertarget_view_decref(view
);
1222 if (device
->auto_depth_stencil_view
)
1224 struct wined3d_rendertarget_view
*view
= device
->auto_depth_stencil_view
;
1226 device
->auto_depth_stencil_view
= NULL
;
1227 if (wined3d_rendertarget_view_decref(view
))
1228 ERR("Something's still holding the auto depth/stencil view (%p).\n", view
);
1231 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
1233 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
1235 if (device
->back_buffer_view
)
1237 wined3d_rendertarget_view_decref(device
->back_buffer_view
);
1238 device
->back_buffer_view
= NULL
;
1241 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1243 TRACE("Releasing the implicit swapchain %u.\n", i
);
1244 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1245 FIXME("Something's still holding the implicit swapchain.\n");
1248 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1249 device
->swapchains
= NULL
;
1250 device
->swapchain_count
= 0;
1252 HeapFree(GetProcessHeap(), 0, device
->fb
.render_targets
);
1253 device
->fb
.render_targets
= NULL
;
1255 device
->d3d_initialized
= FALSE
;
1260 HRESULT CDECL
wined3d_device_uninit_gdi(struct wined3d_device
*device
)
1264 device
->blitter
->ops
->blitter_destroy(device
->blitter
, NULL
);
1266 for (i
= 0; i
< device
->swapchain_count
; ++i
)
1268 TRACE("Releasing the implicit swapchain %u.\n", i
);
1269 if (wined3d_swapchain_decref(device
->swapchains
[i
]))
1270 FIXME("Something's still holding the implicit swapchain.\n");
1273 HeapFree(GetProcessHeap(), 0, device
->swapchains
);
1274 device
->swapchains
= NULL
;
1275 device
->swapchain_count
= 0;
1279 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1280 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1281 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1283 * There is no way to deactivate thread safety once it is enabled.
1285 void CDECL
wined3d_device_set_multithreaded(struct wined3d_device
*device
)
1287 TRACE("device %p.\n", device
);
1289 /* For now just store the flag (needed in case of ddraw). */
1290 device
->create_parms
.flags
|= WINED3DCREATE_MULTITHREADED
;
1293 UINT CDECL
wined3d_device_get_available_texture_mem(const struct wined3d_device
*device
)
1295 TRACE("device %p.\n", device
);
1297 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1298 wine_dbgstr_longlong(device
->adapter
->vram_bytes
),
1299 wine_dbgstr_longlong(device
->adapter
->vram_bytes_used
),
1300 wine_dbgstr_longlong(device
->adapter
->vram_bytes
- device
->adapter
->vram_bytes_used
));
1302 return min(UINT_MAX
, device
->adapter
->vram_bytes
- device
->adapter
->vram_bytes_used
);
1305 void CDECL
wined3d_device_set_stream_output(struct wined3d_device
*device
, UINT idx
,
1306 struct wined3d_buffer
*buffer
, UINT offset
)
1308 struct wined3d_stream_output
*stream
;
1309 struct wined3d_buffer
*prev_buffer
;
1311 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device
, idx
, buffer
, offset
);
1313 if (idx
>= WINED3D_MAX_STREAM_OUTPUT_BUFFERS
)
1315 WARN("Invalid stream output %u.\n", idx
);
1319 stream
= &device
->update_state
->stream_output
[idx
];
1320 prev_buffer
= stream
->buffer
;
1323 wined3d_buffer_incref(buffer
);
1324 stream
->buffer
= buffer
;
1325 stream
->offset
= offset
;
1326 if (!device
->recording
)
1327 wined3d_cs_emit_set_stream_output(device
->cs
, idx
, buffer
, offset
);
1329 wined3d_buffer_decref(prev_buffer
);
1332 struct wined3d_buffer
* CDECL
wined3d_device_get_stream_output(struct wined3d_device
*device
,
1333 UINT idx
, UINT
*offset
)
1335 TRACE("device %p, idx %u, offset %p.\n", device
, idx
, offset
);
1337 if (idx
>= WINED3D_MAX_STREAM_OUTPUT_BUFFERS
)
1339 WARN("Invalid stream output %u.\n", idx
);
1344 *offset
= device
->state
.stream_output
[idx
].offset
;
1345 return device
->state
.stream_output
[idx
].buffer
;
1348 HRESULT CDECL
wined3d_device_set_stream_source(struct wined3d_device
*device
, UINT stream_idx
,
1349 struct wined3d_buffer
*buffer
, UINT offset
, UINT stride
)
1351 struct wined3d_stream_state
*stream
;
1352 struct wined3d_buffer
*prev_buffer
;
1354 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1355 device
, stream_idx
, buffer
, offset
, stride
);
1357 if (stream_idx
>= MAX_STREAMS
)
1359 WARN("Stream index %u out of range.\n", stream_idx
);
1360 return WINED3DERR_INVALIDCALL
;
1362 else if (offset
& 0x3)
1364 WARN("Offset %u is not 4 byte aligned.\n", offset
);
1365 return WINED3DERR_INVALIDCALL
;
1368 stream
= &device
->update_state
->streams
[stream_idx
];
1369 prev_buffer
= stream
->buffer
;
1371 if (device
->recording
)
1372 device
->recording
->changed
.streamSource
|= 1u << stream_idx
;
1374 if (prev_buffer
== buffer
1375 && stream
->stride
== stride
1376 && stream
->offset
== offset
)
1378 TRACE("Application is setting the old values over, nothing to do.\n");
1382 stream
->buffer
= buffer
;
1385 stream
->stride
= stride
;
1386 stream
->offset
= offset
;
1387 wined3d_buffer_incref(buffer
);
1390 if (!device
->recording
)
1391 wined3d_cs_emit_set_stream_source(device
->cs
, stream_idx
, buffer
, offset
, stride
);
1393 wined3d_buffer_decref(prev_buffer
);
1398 HRESULT CDECL
wined3d_device_get_stream_source(const struct wined3d_device
*device
,
1399 UINT stream_idx
, struct wined3d_buffer
**buffer
, UINT
*offset
, UINT
*stride
)
1401 const struct wined3d_stream_state
*stream
;
1403 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1404 device
, stream_idx
, buffer
, offset
, stride
);
1406 if (stream_idx
>= MAX_STREAMS
)
1408 WARN("Stream index %u out of range.\n", stream_idx
);
1409 return WINED3DERR_INVALIDCALL
;
1412 stream
= &device
->state
.streams
[stream_idx
];
1413 *buffer
= stream
->buffer
;
1415 *offset
= stream
->offset
;
1416 *stride
= stream
->stride
;
1421 HRESULT CDECL
wined3d_device_set_stream_source_freq(struct wined3d_device
*device
, UINT stream_idx
, UINT divider
)
1423 struct wined3d_stream_state
*stream
;
1424 UINT old_flags
, old_freq
;
1426 TRACE("device %p, stream_idx %u, divider %#x.\n", device
, stream_idx
, divider
);
1428 /* Verify input. At least in d3d9 this is invalid. */
1429 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && (divider
& WINED3DSTREAMSOURCE_INDEXEDDATA
))
1431 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1432 return WINED3DERR_INVALIDCALL
;
1434 if ((divider
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && !stream_idx
)
1436 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1437 return WINED3DERR_INVALIDCALL
;
1441 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1442 return WINED3DERR_INVALIDCALL
;
1445 stream
= &device
->update_state
->streams
[stream_idx
];
1446 old_flags
= stream
->flags
;
1447 old_freq
= stream
->frequency
;
1449 stream
->flags
= divider
& (WINED3DSTREAMSOURCE_INSTANCEDATA
| WINED3DSTREAMSOURCE_INDEXEDDATA
);
1450 stream
->frequency
= divider
& 0x7fffff;
1452 if (device
->recording
)
1453 device
->recording
->changed
.streamFreq
|= 1u << stream_idx
;
1454 else if (stream
->frequency
!= old_freq
|| stream
->flags
!= old_flags
)
1455 wined3d_cs_emit_set_stream_source_freq(device
->cs
, stream_idx
, stream
->frequency
, stream
->flags
);
1460 HRESULT CDECL
wined3d_device_get_stream_source_freq(const struct wined3d_device
*device
,
1461 UINT stream_idx
, UINT
*divider
)
1463 const struct wined3d_stream_state
*stream
;
1465 TRACE("device %p, stream_idx %u, divider %p.\n", device
, stream_idx
, divider
);
1467 stream
= &device
->state
.streams
[stream_idx
];
1468 *divider
= stream
->flags
| stream
->frequency
;
1470 TRACE("Returning %#x.\n", *divider
);
1475 void CDECL
wined3d_device_set_transform(struct wined3d_device
*device
,
1476 enum wined3d_transform_state d3dts
, const struct wined3d_matrix
*matrix
)
1478 TRACE("device %p, state %s, matrix %p.\n",
1479 device
, debug_d3dtstype(d3dts
), matrix
);
1480 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_11
, matrix
->_12
, matrix
->_13
, matrix
->_14
);
1481 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_21
, matrix
->_22
, matrix
->_23
, matrix
->_24
);
1482 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_31
, matrix
->_32
, matrix
->_33
, matrix
->_34
);
1483 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_41
, matrix
->_42
, matrix
->_43
, matrix
->_44
);
1485 /* Handle recording of state blocks. */
1486 if (device
->recording
)
1488 TRACE("Recording... not performing anything.\n");
1489 device
->recording
->changed
.transform
[d3dts
>> 5] |= 1u << (d3dts
& 0x1f);
1490 device
->update_state
->transforms
[d3dts
] = *matrix
;
1494 /* If the new matrix is the same as the current one,
1495 * we cut off any further processing. this seems to be a reasonable
1496 * optimization because as was noticed, some apps (warcraft3 for example)
1497 * tend towards setting the same matrix repeatedly for some reason.
1499 * From here on we assume that the new matrix is different, wherever it matters. */
1500 if (!memcmp(&device
->state
.transforms
[d3dts
], matrix
, sizeof(*matrix
)))
1502 TRACE("The application is setting the same matrix over again.\n");
1506 device
->state
.transforms
[d3dts
] = *matrix
;
1507 wined3d_cs_emit_set_transform(device
->cs
, d3dts
, matrix
);
1510 void CDECL
wined3d_device_get_transform(const struct wined3d_device
*device
,
1511 enum wined3d_transform_state state
, struct wined3d_matrix
*matrix
)
1513 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1515 *matrix
= device
->state
.transforms
[state
];
1518 void CDECL
wined3d_device_multiply_transform(struct wined3d_device
*device
,
1519 enum wined3d_transform_state state
, const struct wined3d_matrix
*matrix
)
1521 const struct wined3d_matrix
*mat
;
1522 struct wined3d_matrix temp
;
1524 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1526 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1527 * below means it will be recorded in a state block change, but it
1528 * works regardless where it is recorded.
1529 * If this is found to be wrong, change to StateBlock. */
1530 if (state
> HIGHEST_TRANSFORMSTATE
)
1532 WARN("Unhandled transform state %#x.\n", state
);
1536 mat
= &device
->update_state
->transforms
[state
];
1537 multiply_matrix(&temp
, mat
, matrix
);
1539 /* Apply change via set transform - will reapply to eg. lights this way. */
1540 wined3d_device_set_transform(device
, state
, &temp
);
1543 /* Note lights are real special cases. Although the device caps state only
1544 * e.g. 8 are supported, you can reference any indexes you want as long as
1545 * that number max are enabled at any one point in time. Therefore since the
1546 * indices can be anything, we need a hashmap of them. However, this causes
1547 * stateblock problems. When capturing the state block, I duplicate the
1548 * hashmap, but when recording, just build a chain pretty much of commands to
1550 HRESULT CDECL
wined3d_device_set_light(struct wined3d_device
*device
,
1551 UINT light_idx
, const struct wined3d_light
*light
)
1553 UINT hash_idx
= LIGHTMAP_HASHFUNC(light_idx
);
1554 struct wined3d_light_info
*object
= NULL
;
1557 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1559 /* Check the parameter range. Need for speed most wanted sets junk lights
1560 * which confuse the GL driver. */
1562 return WINED3DERR_INVALIDCALL
;
1564 switch (light
->type
)
1566 case WINED3D_LIGHT_POINT
:
1567 case WINED3D_LIGHT_SPOT
:
1568 case WINED3D_LIGHT_GLSPOT
:
1569 /* Incorrect attenuation values can cause the gl driver to crash.
1570 * Happens with Need for speed most wanted. */
1571 if (light
->attenuation0
< 0.0f
|| light
->attenuation1
< 0.0f
|| light
->attenuation2
< 0.0f
)
1573 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1574 return WINED3DERR_INVALIDCALL
;
1578 case WINED3D_LIGHT_DIRECTIONAL
:
1579 case WINED3D_LIGHT_PARALLELPOINT
:
1580 /* Ignores attenuation */
1584 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1585 return WINED3DERR_INVALIDCALL
;
1588 if (!(object
= wined3d_state_get_light(device
->update_state
, light_idx
)))
1590 TRACE("Adding new light\n");
1591 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
1593 return E_OUTOFMEMORY
;
1595 list_add_head(&device
->update_state
->light_map
[hash_idx
], &object
->entry
);
1596 object
->glIndex
= -1;
1597 object
->OriginalIndex
= light_idx
;
1600 /* Initialize the object. */
1601 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1602 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1603 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1604 light_idx
, light
->type
, debug_color(&light
->diffuse
),
1605 debug_color(&light
->specular
), debug_color(&light
->ambient
),
1606 light
->position
.x
, light
->position
.y
, light
->position
.z
,
1607 light
->direction
.x
, light
->direction
.y
, light
->direction
.z
,
1608 light
->range
, light
->falloff
, light
->theta
, light
->phi
);
1610 /* Save away the information. */
1611 object
->OriginalParms
= *light
;
1613 switch (light
->type
)
1615 case WINED3D_LIGHT_POINT
:
1617 object
->position
.x
= light
->position
.x
;
1618 object
->position
.y
= light
->position
.y
;
1619 object
->position
.z
= light
->position
.z
;
1620 object
->position
.w
= 1.0f
;
1621 object
->cutoff
= 180.0f
;
1625 case WINED3D_LIGHT_DIRECTIONAL
:
1627 object
->direction
.x
= -light
->direction
.x
;
1628 object
->direction
.y
= -light
->direction
.y
;
1629 object
->direction
.z
= -light
->direction
.z
;
1630 object
->direction
.w
= 0.0f
;
1631 object
->exponent
= 0.0f
;
1632 object
->cutoff
= 180.0f
;
1635 case WINED3D_LIGHT_SPOT
:
1637 object
->position
.x
= light
->position
.x
;
1638 object
->position
.y
= light
->position
.y
;
1639 object
->position
.z
= light
->position
.z
;
1640 object
->position
.w
= 1.0f
;
1643 object
->direction
.x
= light
->direction
.x
;
1644 object
->direction
.y
= light
->direction
.y
;
1645 object
->direction
.z
= light
->direction
.z
;
1646 object
->direction
.w
= 0.0f
;
1648 /* opengl-ish and d3d-ish spot lights use too different models
1649 * for the light "intensity" as a function of the angle towards
1650 * the main light direction, so we only can approximate very
1651 * roughly. However, spot lights are rather rarely used in games
1652 * (if ever used at all). Furthermore if still used, probably
1653 * nobody pays attention to such details. */
1654 if (!light
->falloff
)
1656 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1657 * equations have the falloff resp. exponent parameter as an
1658 * exponent, so the spot light lighting will always be 1.0 for
1659 * both of them, and we don't have to care for the rest of the
1660 * rather complex calculation. */
1661 object
->exponent
= 0.0f
;
1665 rho
= light
->theta
+ (light
->phi
- light
->theta
) / (2 * light
->falloff
);
1668 object
->exponent
= -0.3f
/ logf(cosf(rho
/ 2));
1671 if (object
->exponent
> 128.0f
)
1672 object
->exponent
= 128.0f
;
1674 object
->cutoff
= (float)(light
->phi
* 90 / M_PI
);
1678 case WINED3D_LIGHT_PARALLELPOINT
:
1679 object
->position
.x
= light
->position
.x
;
1680 object
->position
.y
= light
->position
.y
;
1681 object
->position
.z
= light
->position
.z
;
1682 object
->position
.w
= 1.0f
;
1686 FIXME("Unrecognized light type %#x.\n", light
->type
);
1689 if (!device
->recording
)
1690 wined3d_cs_emit_set_light(device
->cs
, object
);
1695 HRESULT CDECL
wined3d_device_get_light(const struct wined3d_device
*device
,
1696 UINT light_idx
, struct wined3d_light
*light
)
1698 struct wined3d_light_info
*light_info
;
1700 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1702 if (!(light_info
= wined3d_state_get_light(&device
->state
, light_idx
)))
1704 TRACE("Light information requested but light not defined\n");
1705 return WINED3DERR_INVALIDCALL
;
1708 *light
= light_info
->OriginalParms
;
1712 HRESULT CDECL
wined3d_device_set_light_enable(struct wined3d_device
*device
, UINT light_idx
, BOOL enable
)
1714 struct wined3d_light_info
*light_info
;
1716 TRACE("device %p, light_idx %u, enable %#x.\n", device
, light_idx
, enable
);
1718 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1719 if (!(light_info
= wined3d_state_get_light(device
->update_state
, light_idx
)))
1721 TRACE("Light enabled requested but light not defined, so defining one!\n");
1722 wined3d_device_set_light(device
, light_idx
, &WINED3D_default_light
);
1724 if (!(light_info
= wined3d_state_get_light(device
->update_state
, light_idx
)))
1726 FIXME("Adding default lights has failed dismally\n");
1727 return WINED3DERR_INVALIDCALL
;
1731 wined3d_state_enable_light(device
->update_state
, &device
->adapter
->d3d_info
, light_info
, enable
);
1732 if (!device
->recording
)
1733 wined3d_cs_emit_set_light_enable(device
->cs
, light_idx
, enable
);
1738 HRESULT CDECL
wined3d_device_get_light_enable(const struct wined3d_device
*device
, UINT light_idx
, BOOL
*enable
)
1740 struct wined3d_light_info
*light_info
;
1742 TRACE("device %p, light_idx %u, enable %p.\n", device
, light_idx
, enable
);
1744 if (!(light_info
= wined3d_state_get_light(&device
->state
, light_idx
)))
1746 TRACE("Light enabled state requested but light not defined.\n");
1747 return WINED3DERR_INVALIDCALL
;
1749 /* true is 128 according to SetLightEnable */
1750 *enable
= light_info
->enabled
? 128 : 0;
1754 HRESULT CDECL
wined3d_device_set_clip_plane(struct wined3d_device
*device
,
1755 UINT plane_idx
, const struct wined3d_vec4
*plane
)
1757 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
1759 if (plane_idx
>= device
->adapter
->gl_info
.limits
.user_clip_distances
)
1761 TRACE("Application has requested clipplane this device doesn't support.\n");
1762 return WINED3DERR_INVALIDCALL
;
1765 if (device
->recording
)
1766 device
->recording
->changed
.clipplane
|= 1u << plane_idx
;
1768 if (!memcmp(&device
->update_state
->clip_planes
[plane_idx
], plane
, sizeof(*plane
)))
1770 TRACE("Application is setting old values over, nothing to do.\n");
1774 device
->update_state
->clip_planes
[plane_idx
] = *plane
;
1776 if (!device
->recording
)
1777 wined3d_cs_emit_set_clip_plane(device
->cs
, plane_idx
, plane
);
1782 HRESULT CDECL
wined3d_device_get_clip_plane(const struct wined3d_device
*device
,
1783 UINT plane_idx
, struct wined3d_vec4
*plane
)
1785 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
1787 if (plane_idx
>= device
->adapter
->gl_info
.limits
.user_clip_distances
)
1789 TRACE("Application has requested clipplane this device doesn't support.\n");
1790 return WINED3DERR_INVALIDCALL
;
1793 *plane
= device
->state
.clip_planes
[plane_idx
];
1798 HRESULT CDECL
wined3d_device_set_clip_status(struct wined3d_device
*device
,
1799 const struct wined3d_clip_status
*clip_status
)
1801 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
1804 return WINED3DERR_INVALIDCALL
;
1809 HRESULT CDECL
wined3d_device_get_clip_status(const struct wined3d_device
*device
,
1810 struct wined3d_clip_status
*clip_status
)
1812 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
1815 return WINED3DERR_INVALIDCALL
;
1820 void CDECL
wined3d_device_set_material(struct wined3d_device
*device
, const struct wined3d_material
*material
)
1822 TRACE("device %p, material %p.\n", device
, material
);
1824 device
->update_state
->material
= *material
;
1826 if (device
->recording
)
1827 device
->recording
->changed
.material
= TRUE
;
1829 wined3d_cs_emit_set_material(device
->cs
, material
);
1832 void CDECL
wined3d_device_get_material(const struct wined3d_device
*device
, struct wined3d_material
*material
)
1834 TRACE("device %p, material %p.\n", device
, material
);
1836 *material
= device
->state
.material
;
1838 TRACE("diffuse %s\n", debug_color(&material
->diffuse
));
1839 TRACE("ambient %s\n", debug_color(&material
->ambient
));
1840 TRACE("specular %s\n", debug_color(&material
->specular
));
1841 TRACE("emissive %s\n", debug_color(&material
->emissive
));
1842 TRACE("power %.8e.\n", material
->power
);
1845 void CDECL
wined3d_device_set_index_buffer(struct wined3d_device
*device
,
1846 struct wined3d_buffer
*buffer
, enum wined3d_format_id format_id
, unsigned int offset
)
1848 enum wined3d_format_id prev_format
;
1849 struct wined3d_buffer
*prev_buffer
;
1850 unsigned int prev_offset
;
1852 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1853 device
, buffer
, debug_d3dformat(format_id
), offset
);
1855 prev_buffer
= device
->update_state
->index_buffer
;
1856 prev_format
= device
->update_state
->index_format
;
1857 prev_offset
= device
->update_state
->index_offset
;
1859 device
->update_state
->index_buffer
= buffer
;
1860 device
->update_state
->index_format
= format_id
;
1861 device
->update_state
->index_offset
= offset
;
1863 if (device
->recording
)
1864 device
->recording
->changed
.indices
= TRUE
;
1866 if (prev_buffer
== buffer
&& prev_format
== format_id
&& prev_offset
== offset
)
1870 wined3d_buffer_incref(buffer
);
1871 if (!device
->recording
)
1872 wined3d_cs_emit_set_index_buffer(device
->cs
, buffer
, format_id
, offset
);
1874 wined3d_buffer_decref(prev_buffer
);
1877 struct wined3d_buffer
* CDECL
wined3d_device_get_index_buffer(const struct wined3d_device
*device
,
1878 enum wined3d_format_id
*format
, unsigned int *offset
)
1880 TRACE("device %p, format %p, offset %p.\n", device
, format
, offset
);
1882 *format
= device
->state
.index_format
;
1884 *offset
= device
->state
.index_offset
;
1885 return device
->state
.index_buffer
;
1888 void CDECL
wined3d_device_set_base_vertex_index(struct wined3d_device
*device
, INT base_index
)
1890 TRACE("device %p, base_index %d.\n", device
, base_index
);
1892 device
->update_state
->base_vertex_index
= base_index
;
1895 INT CDECL
wined3d_device_get_base_vertex_index(const struct wined3d_device
*device
)
1897 TRACE("device %p.\n", device
);
1899 return device
->state
.base_vertex_index
;
1902 void CDECL
wined3d_device_set_viewport(struct wined3d_device
*device
, const struct wined3d_viewport
*viewport
)
1904 TRACE("device %p, viewport %p.\n", device
, viewport
);
1905 TRACE("x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n",
1906 viewport
->x
, viewport
->y
, viewport
->width
, viewport
->height
, viewport
->min_z
, viewport
->max_z
);
1908 device
->update_state
->viewport
= *viewport
;
1910 /* Handle recording of state blocks */
1911 if (device
->recording
)
1913 TRACE("Recording... not performing anything\n");
1914 device
->recording
->changed
.viewport
= TRUE
;
1918 wined3d_cs_emit_set_viewport(device
->cs
, viewport
);
1921 void CDECL
wined3d_device_get_viewport(const struct wined3d_device
*device
, struct wined3d_viewport
*viewport
)
1923 TRACE("device %p, viewport %p.\n", device
, viewport
);
1925 *viewport
= device
->state
.viewport
;
1928 static void resolve_depth_buffer(struct wined3d_state
*state
)
1930 struct wined3d_texture
*dst_texture
= state
->textures
[0];
1931 struct wined3d_rendertarget_view
*src_view
;
1932 RECT src_rect
, dst_rect
;
1934 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
1935 || !(dst_texture
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
))
1938 if (!(src_view
= state
->fb
->depth_stencil
))
1940 if (src_view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
1942 FIXME("Not supported on buffer resources.\n");
1946 SetRect(&dst_rect
, 0, 0, dst_texture
->resource
.width
, dst_texture
->resource
.height
);
1947 SetRect(&src_rect
, 0, 0, src_view
->width
, src_view
->height
);
1948 wined3d_texture_blt(dst_texture
, 0, &dst_rect
, texture_from_resource(src_view
->resource
),
1949 src_view
->sub_resource_idx
, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
1952 void CDECL
wined3d_device_set_rasterizer_state(struct wined3d_device
*device
,
1953 struct wined3d_rasterizer_state
*rasterizer_state
)
1955 struct wined3d_rasterizer_state
*prev
;
1957 TRACE("device %p, rasterizer_state %p.\n", device
, rasterizer_state
);
1959 prev
= device
->update_state
->rasterizer_state
;
1960 if (prev
== rasterizer_state
)
1963 if (rasterizer_state
)
1964 wined3d_rasterizer_state_incref(rasterizer_state
);
1965 device
->update_state
->rasterizer_state
= rasterizer_state
;
1966 wined3d_cs_emit_set_rasterizer_state(device
->cs
, rasterizer_state
);
1968 wined3d_rasterizer_state_decref(prev
);
1971 struct wined3d_rasterizer_state
* CDECL
wined3d_device_get_rasterizer_state(struct wined3d_device
*device
)
1973 TRACE("device %p.\n", device
);
1975 return device
->state
.rasterizer_state
;
1978 void CDECL
wined3d_device_set_render_state(struct wined3d_device
*device
,
1979 enum wined3d_render_state state
, DWORD value
)
1983 TRACE("device %p, state %s (%#x), value %#x.\n", device
, debug_d3drenderstate(state
), state
, value
);
1985 if (state
> WINEHIGHEST_RENDER_STATE
)
1987 WARN("Unhandled render state %#x.\n", state
);
1991 old_value
= device
->state
.render_states
[state
];
1992 device
->update_state
->render_states
[state
] = value
;
1994 /* Handle recording of state blocks. */
1995 if (device
->recording
)
1997 TRACE("Recording... not performing anything.\n");
1998 device
->recording
->changed
.renderState
[state
>> 5] |= 1u << (state
& 0x1f);
2002 /* Compared here and not before the assignment to allow proper stateblock recording. */
2003 if (value
== old_value
)
2004 TRACE("Application is setting the old value over, nothing to do.\n");
2006 wined3d_cs_emit_set_render_state(device
->cs
, state
, value
);
2008 if (state
== WINED3D_RS_POINTSIZE
&& value
== WINED3D_RESZ_CODE
)
2010 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2011 resolve_depth_buffer(&device
->state
);
2015 DWORD CDECL
wined3d_device_get_render_state(const struct wined3d_device
*device
, enum wined3d_render_state state
)
2017 TRACE("device %p, state %s (%#x).\n", device
, debug_d3drenderstate(state
), state
);
2019 return device
->state
.render_states
[state
];
2022 void CDECL
wined3d_device_set_sampler_state(struct wined3d_device
*device
,
2023 UINT sampler_idx
, enum wined3d_sampler_state state
, DWORD value
)
2027 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2028 device
, sampler_idx
, debug_d3dsamplerstate(state
), value
);
2030 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2031 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2033 if (sampler_idx
>= ARRAY_SIZE(device
->state
.sampler_states
))
2035 WARN("Invalid sampler %u.\n", sampler_idx
);
2036 return; /* Windows accepts overflowing this array ... we do not. */
2039 old_value
= device
->state
.sampler_states
[sampler_idx
][state
];
2040 device
->update_state
->sampler_states
[sampler_idx
][state
] = value
;
2042 /* Handle recording of state blocks. */
2043 if (device
->recording
)
2045 TRACE("Recording... not performing anything.\n");
2046 device
->recording
->changed
.samplerState
[sampler_idx
] |= 1u << state
;
2050 if (old_value
== value
)
2052 TRACE("Application is setting the old value over, nothing to do.\n");
2056 wined3d_cs_emit_set_sampler_state(device
->cs
, sampler_idx
, state
, value
);
2059 DWORD CDECL
wined3d_device_get_sampler_state(const struct wined3d_device
*device
,
2060 UINT sampler_idx
, enum wined3d_sampler_state state
)
2062 TRACE("device %p, sampler_idx %u, state %s.\n",
2063 device
, sampler_idx
, debug_d3dsamplerstate(state
));
2065 if (sampler_idx
>= WINED3DVERTEXTEXTURESAMPLER0
&& sampler_idx
<= WINED3DVERTEXTEXTURESAMPLER3
)
2066 sampler_idx
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
2068 if (sampler_idx
>= ARRAY_SIZE(device
->state
.sampler_states
))
2070 WARN("Invalid sampler %u.\n", sampler_idx
);
2071 return 0; /* Windows accepts overflowing this array ... we do not. */
2074 return device
->state
.sampler_states
[sampler_idx
][state
];
2077 void CDECL
wined3d_device_set_scissor_rect(struct wined3d_device
*device
, const RECT
*rect
)
2079 TRACE("device %p, rect %s.\n", device
, wine_dbgstr_rect(rect
));
2081 if (device
->recording
)
2082 device
->recording
->changed
.scissorRect
= TRUE
;
2084 if (EqualRect(&device
->update_state
->scissor_rect
, rect
))
2086 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2089 CopyRect(&device
->update_state
->scissor_rect
, rect
);
2091 if (device
->recording
)
2093 TRACE("Recording... not performing anything.\n");
2097 wined3d_cs_emit_set_scissor_rect(device
->cs
, rect
);
2100 void CDECL
wined3d_device_get_scissor_rect(const struct wined3d_device
*device
, RECT
*rect
)
2102 TRACE("device %p, rect %p.\n", device
, rect
);
2104 *rect
= device
->state
.scissor_rect
;
2105 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect
));
2108 void CDECL
wined3d_device_set_vertex_declaration(struct wined3d_device
*device
,
2109 struct wined3d_vertex_declaration
*declaration
)
2111 struct wined3d_vertex_declaration
*prev
= device
->update_state
->vertex_declaration
;
2113 TRACE("device %p, declaration %p.\n", device
, declaration
);
2115 if (device
->recording
)
2116 device
->recording
->changed
.vertexDecl
= TRUE
;
2118 if (declaration
== prev
)
2122 wined3d_vertex_declaration_incref(declaration
);
2123 device
->update_state
->vertex_declaration
= declaration
;
2124 if (!device
->recording
)
2125 wined3d_cs_emit_set_vertex_declaration(device
->cs
, declaration
);
2127 wined3d_vertex_declaration_decref(prev
);
2130 struct wined3d_vertex_declaration
* CDECL
wined3d_device_get_vertex_declaration(const struct wined3d_device
*device
)
2132 TRACE("device %p.\n", device
);
2134 return device
->state
.vertex_declaration
;
2137 void CDECL
wined3d_device_set_vertex_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2139 struct wined3d_shader
*prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
2141 TRACE("device %p, shader %p.\n", device
, shader
);
2143 if (device
->recording
)
2144 device
->recording
->changed
.vertexShader
= TRUE
;
2150 wined3d_shader_incref(shader
);
2151 device
->update_state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = shader
;
2152 if (!device
->recording
)
2153 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_VERTEX
, shader
);
2155 wined3d_shader_decref(prev
);
2158 struct wined3d_shader
* CDECL
wined3d_device_get_vertex_shader(const struct wined3d_device
*device
)
2160 TRACE("device %p.\n", device
);
2162 return device
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
];
2165 static void wined3d_device_set_constant_buffer(struct wined3d_device
*device
,
2166 enum wined3d_shader_type type
, UINT idx
, struct wined3d_buffer
*buffer
)
2168 struct wined3d_buffer
*prev
;
2170 if (idx
>= MAX_CONSTANT_BUFFERS
)
2172 WARN("Invalid constant buffer index %u.\n", idx
);
2176 prev
= device
->update_state
->cb
[type
][idx
];
2181 wined3d_buffer_incref(buffer
);
2182 device
->update_state
->cb
[type
][idx
] = buffer
;
2183 if (!device
->recording
)
2184 wined3d_cs_emit_set_constant_buffer(device
->cs
, type
, idx
, buffer
);
2186 wined3d_buffer_decref(prev
);
2189 void CDECL
wined3d_device_set_vs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2191 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2193 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, buffer
);
2196 static struct wined3d_buffer
*wined3d_device_get_constant_buffer(const struct wined3d_device
*device
,
2197 enum wined3d_shader_type shader_type
, unsigned int idx
)
2199 if (idx
>= MAX_CONSTANT_BUFFERS
)
2201 WARN("Invalid constant buffer index %u.\n", idx
);
2205 return device
->state
.cb
[shader_type
][idx
];
2208 struct wined3d_buffer
* CDECL
wined3d_device_get_vs_cb(const struct wined3d_device
*device
, UINT idx
)
2210 TRACE("device %p, idx %u.\n", device
, idx
);
2212 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2215 static void wined3d_device_set_shader_resource_view(struct wined3d_device
*device
,
2216 enum wined3d_shader_type type
, UINT idx
, struct wined3d_shader_resource_view
*view
)
2218 struct wined3d_shader_resource_view
*prev
;
2220 if (idx
>= MAX_SHADER_RESOURCE_VIEWS
)
2222 WARN("Invalid view index %u.\n", idx
);
2226 prev
= device
->update_state
->shader_resource_view
[type
][idx
];
2231 wined3d_shader_resource_view_incref(view
);
2232 device
->update_state
->shader_resource_view
[type
][idx
] = view
;
2233 if (!device
->recording
)
2234 wined3d_cs_emit_set_shader_resource_view(device
->cs
, type
, idx
, view
);
2236 wined3d_shader_resource_view_decref(prev
);
2239 void CDECL
wined3d_device_set_vs_resource_view(struct wined3d_device
*device
,
2240 UINT idx
, struct wined3d_shader_resource_view
*view
)
2242 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2244 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, view
);
2247 static struct wined3d_shader_resource_view
*wined3d_device_get_shader_resource_view(
2248 const struct wined3d_device
*device
, enum wined3d_shader_type shader_type
, unsigned int idx
)
2250 if (idx
>= MAX_SHADER_RESOURCE_VIEWS
)
2252 WARN("Invalid view index %u.\n", idx
);
2256 return device
->state
.shader_resource_view
[shader_type
][idx
];
2259 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_vs_resource_view(const struct wined3d_device
*device
,
2262 TRACE("device %p, idx %u.\n", device
, idx
);
2264 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2267 static void wined3d_device_set_sampler(struct wined3d_device
*device
,
2268 enum wined3d_shader_type type
, UINT idx
, struct wined3d_sampler
*sampler
)
2270 struct wined3d_sampler
*prev
;
2272 if (idx
>= MAX_SAMPLER_OBJECTS
)
2274 WARN("Invalid sampler index %u.\n", idx
);
2278 prev
= device
->update_state
->sampler
[type
][idx
];
2279 if (sampler
== prev
)
2283 wined3d_sampler_incref(sampler
);
2284 device
->update_state
->sampler
[type
][idx
] = sampler
;
2285 if (!device
->recording
)
2286 wined3d_cs_emit_set_sampler(device
->cs
, type
, idx
, sampler
);
2288 wined3d_sampler_decref(prev
);
2291 void CDECL
wined3d_device_set_vs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2293 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2295 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, sampler
);
2298 static struct wined3d_sampler
*wined3d_device_get_sampler(const struct wined3d_device
*device
,
2299 enum wined3d_shader_type shader_type
, unsigned int idx
)
2301 if (idx
>= MAX_SAMPLER_OBJECTS
)
2303 WARN("Invalid sampler index %u.\n", idx
);
2307 return device
->state
.sampler
[shader_type
][idx
];
2310 struct wined3d_sampler
* CDECL
wined3d_device_get_vs_sampler(const struct wined3d_device
*device
, UINT idx
)
2312 TRACE("device %p, idx %u.\n", device
, idx
);
2314 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2317 HRESULT CDECL
wined3d_device_set_vs_consts_b(struct wined3d_device
*device
,
2318 unsigned int start_idx
, unsigned int count
, const BOOL
*constants
)
2322 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2323 device
, start_idx
, count
, constants
);
2325 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2326 return WINED3DERR_INVALIDCALL
;
2328 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2329 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2330 memcpy(&device
->update_state
->vs_consts_b
[start_idx
], constants
, count
* sizeof(*constants
));
2333 for (i
= 0; i
< count
; ++i
)
2334 TRACE("Set BOOL constant %u to %#x.\n", start_idx
+ i
, constants
[i
]);
2337 if (device
->recording
)
2339 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2340 device
->recording
->changed
.vertexShaderConstantsB
|= (1u << i
);
2344 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_B
, start_idx
, count
, constants
);
2350 HRESULT CDECL
wined3d_device_get_vs_consts_b(const struct wined3d_device
*device
,
2351 unsigned int start_idx
, unsigned int count
, BOOL
*constants
)
2353 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2354 device
, start_idx
, count
, constants
);
2356 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2357 return WINED3DERR_INVALIDCALL
;
2359 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2360 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2361 memcpy(constants
, &device
->state
.vs_consts_b
[start_idx
], count
* sizeof(*constants
));
2366 HRESULT CDECL
wined3d_device_set_vs_consts_i(struct wined3d_device
*device
,
2367 unsigned int start_idx
, unsigned int count
, const struct wined3d_ivec4
*constants
)
2371 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2372 device
, start_idx
, count
, constants
);
2374 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2375 return WINED3DERR_INVALIDCALL
;
2377 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2378 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2379 memcpy(&device
->update_state
->vs_consts_i
[start_idx
], constants
, count
* sizeof(*constants
));
2382 for (i
= 0; i
< count
; ++i
)
2383 TRACE("Set ivec4 constant %u to %s.\n", start_idx
+ i
, debug_ivec4(&constants
[i
]));
2386 if (device
->recording
)
2388 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2389 device
->recording
->changed
.vertexShaderConstantsI
|= (1u << i
);
2393 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_I
, start_idx
, count
, constants
);
2399 HRESULT CDECL
wined3d_device_get_vs_consts_i(const struct wined3d_device
*device
,
2400 unsigned int start_idx
, unsigned int count
, struct wined3d_ivec4
*constants
)
2402 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2403 device
, start_idx
, count
, constants
);
2405 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2406 return WINED3DERR_INVALIDCALL
;
2408 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2409 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2410 memcpy(constants
, &device
->state
.vs_consts_i
[start_idx
], count
* sizeof(*constants
));
2414 HRESULT CDECL
wined3d_device_set_vs_consts_f(struct wined3d_device
*device
,
2415 unsigned int start_idx
, unsigned int count
, const struct wined3d_vec4
*constants
)
2417 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2420 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2421 device
, start_idx
, count
, constants
);
2423 if (!constants
|| start_idx
>= d3d_info
->limits
.vs_uniform_count
2424 || count
> d3d_info
->limits
.vs_uniform_count
- start_idx
)
2425 return WINED3DERR_INVALIDCALL
;
2427 memcpy(&device
->update_state
->vs_consts_f
[start_idx
], constants
, count
* sizeof(*constants
));
2430 for (i
= 0; i
< count
; ++i
)
2431 TRACE("Set vec4 constant %u to %s.\n", start_idx
+ i
, debug_vec4(&constants
[i
]));
2434 if (device
->recording
)
2435 memset(&device
->recording
->changed
.vs_consts_f
[start_idx
], 1,
2436 count
* sizeof(*device
->recording
->changed
.vs_consts_f
));
2438 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_F
, start_idx
, count
, constants
);
2443 HRESULT CDECL
wined3d_device_get_vs_consts_f(const struct wined3d_device
*device
,
2444 unsigned int start_idx
, unsigned int count
, struct wined3d_vec4
*constants
)
2446 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2448 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2449 device
, start_idx
, count
, constants
);
2451 if (!constants
|| start_idx
>= d3d_info
->limits
.vs_uniform_count
2452 || count
> d3d_info
->limits
.vs_uniform_count
- start_idx
)
2453 return WINED3DERR_INVALIDCALL
;
2455 memcpy(constants
, &device
->state
.vs_consts_f
[start_idx
], count
* sizeof(*constants
));
2460 void CDECL
wined3d_device_set_pixel_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2462 struct wined3d_shader
*prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2464 TRACE("device %p, shader %p.\n", device
, shader
);
2466 if (device
->recording
)
2467 device
->recording
->changed
.pixelShader
= TRUE
;
2473 wined3d_shader_incref(shader
);
2474 device
->update_state
->shader
[WINED3D_SHADER_TYPE_PIXEL
] = shader
;
2475 if (!device
->recording
)
2476 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_PIXEL
, shader
);
2478 wined3d_shader_decref(prev
);
2481 struct wined3d_shader
* CDECL
wined3d_device_get_pixel_shader(const struct wined3d_device
*device
)
2483 TRACE("device %p.\n", device
);
2485 return device
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
];
2488 void CDECL
wined3d_device_set_ps_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2490 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2492 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, buffer
);
2495 struct wined3d_buffer
* CDECL
wined3d_device_get_ps_cb(const struct wined3d_device
*device
, UINT idx
)
2497 TRACE("device %p, idx %u.\n", device
, idx
);
2499 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2502 void CDECL
wined3d_device_set_ps_resource_view(struct wined3d_device
*device
,
2503 UINT idx
, struct wined3d_shader_resource_view
*view
)
2505 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2507 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, view
);
2510 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_ps_resource_view(const struct wined3d_device
*device
,
2513 TRACE("device %p, idx %u.\n", device
, idx
);
2515 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2518 void CDECL
wined3d_device_set_ps_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2520 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2522 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, sampler
);
2525 struct wined3d_sampler
* CDECL
wined3d_device_get_ps_sampler(const struct wined3d_device
*device
, UINT idx
)
2527 TRACE("device %p, idx %u.\n", device
, idx
);
2529 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2532 HRESULT CDECL
wined3d_device_set_ps_consts_b(struct wined3d_device
*device
,
2533 unsigned int start_idx
, unsigned int count
, const BOOL
*constants
)
2537 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2538 device
, start_idx
, count
, constants
);
2540 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2541 return WINED3DERR_INVALIDCALL
;
2543 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2544 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2545 memcpy(&device
->update_state
->ps_consts_b
[start_idx
], constants
, count
* sizeof(*constants
));
2548 for (i
= 0; i
< count
; ++i
)
2549 TRACE("Set BOOL constant %u to %#x.\n", start_idx
+ i
, constants
[i
]);
2552 if (device
->recording
)
2554 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2555 device
->recording
->changed
.pixelShaderConstantsB
|= (1u << i
);
2559 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_B
, start_idx
, count
, constants
);
2565 HRESULT CDECL
wined3d_device_get_ps_consts_b(const struct wined3d_device
*device
,
2566 unsigned int start_idx
, unsigned int count
, BOOL
*constants
)
2568 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2569 device
, start_idx
, count
, constants
);
2571 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_B
)
2572 return WINED3DERR_INVALIDCALL
;
2574 if (count
> WINED3D_MAX_CONSTS_B
- start_idx
)
2575 count
= WINED3D_MAX_CONSTS_B
- start_idx
;
2576 memcpy(constants
, &device
->state
.ps_consts_b
[start_idx
], count
* sizeof(*constants
));
2581 HRESULT CDECL
wined3d_device_set_ps_consts_i(struct wined3d_device
*device
,
2582 unsigned int start_idx
, unsigned int count
, const struct wined3d_ivec4
*constants
)
2586 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2587 device
, start_idx
, count
, constants
);
2589 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2590 return WINED3DERR_INVALIDCALL
;
2592 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2593 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2594 memcpy(&device
->update_state
->ps_consts_i
[start_idx
], constants
, count
* sizeof(*constants
));
2597 for (i
= 0; i
< count
; ++i
)
2598 TRACE("Set ivec4 constant %u to %s.\n", start_idx
+ i
, debug_ivec4(&constants
[i
]));
2601 if (device
->recording
)
2603 for (i
= start_idx
; i
< count
+ start_idx
; ++i
)
2604 device
->recording
->changed
.pixelShaderConstantsI
|= (1u << i
);
2608 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_I
, start_idx
, count
, constants
);
2614 HRESULT CDECL
wined3d_device_get_ps_consts_i(const struct wined3d_device
*device
,
2615 unsigned int start_idx
, unsigned int count
, struct wined3d_ivec4
*constants
)
2617 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2618 device
, start_idx
, count
, constants
);
2620 if (!constants
|| start_idx
>= WINED3D_MAX_CONSTS_I
)
2621 return WINED3DERR_INVALIDCALL
;
2623 if (count
> WINED3D_MAX_CONSTS_I
- start_idx
)
2624 count
= WINED3D_MAX_CONSTS_I
- start_idx
;
2625 memcpy(constants
, &device
->state
.ps_consts_i
[start_idx
], count
* sizeof(*constants
));
2630 HRESULT CDECL
wined3d_device_set_ps_consts_f(struct wined3d_device
*device
,
2631 unsigned int start_idx
, unsigned int count
, const struct wined3d_vec4
*constants
)
2633 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2636 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2637 device
, start_idx
, count
, constants
);
2639 if (!constants
|| start_idx
>= d3d_info
->limits
.ps_uniform_count
2640 || count
> d3d_info
->limits
.ps_uniform_count
- start_idx
)
2641 return WINED3DERR_INVALIDCALL
;
2643 memcpy(&device
->update_state
->ps_consts_f
[start_idx
], constants
, count
* sizeof(*constants
));
2646 for (i
= 0; i
< count
; ++i
)
2647 TRACE("Set vec4 constant %u to %s.\n", start_idx
+ i
, debug_vec4(&constants
[i
]));
2650 if (device
->recording
)
2651 memset(&device
->recording
->changed
.ps_consts_f
[start_idx
], 1,
2652 count
* sizeof(*device
->recording
->changed
.ps_consts_f
));
2654 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_F
, start_idx
, count
, constants
);
2659 HRESULT CDECL
wined3d_device_get_ps_consts_f(const struct wined3d_device
*device
,
2660 unsigned int start_idx
, unsigned int count
, struct wined3d_vec4
*constants
)
2662 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
2664 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2665 device
, start_idx
, count
, constants
);
2667 if (!constants
|| start_idx
>= d3d_info
->limits
.ps_uniform_count
2668 || count
> d3d_info
->limits
.ps_uniform_count
- start_idx
)
2669 return WINED3DERR_INVALIDCALL
;
2671 memcpy(constants
, &device
->state
.ps_consts_f
[start_idx
], count
* sizeof(*constants
));
2676 void CDECL
wined3d_device_set_hull_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2678 struct wined3d_shader
*prev
;
2680 TRACE("device %p, shader %p.\n", device
, shader
);
2682 prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_HULL
];
2686 wined3d_shader_incref(shader
);
2687 device
->update_state
->shader
[WINED3D_SHADER_TYPE_HULL
] = shader
;
2688 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_HULL
, shader
);
2690 wined3d_shader_decref(prev
);
2693 struct wined3d_shader
* CDECL
wined3d_device_get_hull_shader(const struct wined3d_device
*device
)
2695 TRACE("device %p.\n", device
);
2697 return device
->state
.shader
[WINED3D_SHADER_TYPE_HULL
];
2700 void CDECL
wined3d_device_set_hs_cb(struct wined3d_device
*device
, unsigned int idx
, struct wined3d_buffer
*buffer
)
2702 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2704 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_HULL
, idx
, buffer
);
2707 struct wined3d_buffer
* CDECL
wined3d_device_get_hs_cb(const struct wined3d_device
*device
, unsigned int idx
)
2709 TRACE("device %p, idx %u.\n", device
, idx
);
2711 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_HULL
, idx
);
2714 void CDECL
wined3d_device_set_hs_resource_view(struct wined3d_device
*device
,
2715 unsigned int idx
, struct wined3d_shader_resource_view
*view
)
2717 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2719 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_HULL
, idx
, view
);
2722 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_hs_resource_view(const struct wined3d_device
*device
,
2725 TRACE("device %p, idx %u.\n", device
, idx
);
2727 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_HULL
, idx
);
2730 void CDECL
wined3d_device_set_hs_sampler(struct wined3d_device
*device
,
2731 unsigned int idx
, struct wined3d_sampler
*sampler
)
2733 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2735 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_HULL
, idx
, sampler
);
2738 struct wined3d_sampler
* CDECL
wined3d_device_get_hs_sampler(const struct wined3d_device
*device
, unsigned int idx
)
2740 TRACE("device %p, idx %u.\n", device
, idx
);
2742 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_HULL
, idx
);
2745 void CDECL
wined3d_device_set_domain_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2747 struct wined3d_shader
*prev
;
2749 TRACE("device %p, shader %p.\n", device
, shader
);
2751 prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_DOMAIN
];
2755 wined3d_shader_incref(shader
);
2756 device
->update_state
->shader
[WINED3D_SHADER_TYPE_DOMAIN
] = shader
;
2757 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_DOMAIN
, shader
);
2759 wined3d_shader_decref(prev
);
2762 struct wined3d_shader
* CDECL
wined3d_device_get_domain_shader(const struct wined3d_device
*device
)
2764 TRACE("device %p.\n", device
);
2766 return device
->state
.shader
[WINED3D_SHADER_TYPE_DOMAIN
];
2769 void CDECL
wined3d_device_set_ds_cb(struct wined3d_device
*device
, unsigned int idx
, struct wined3d_buffer
*buffer
)
2771 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2773 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
, buffer
);
2776 struct wined3d_buffer
* CDECL
wined3d_device_get_ds_cb(const struct wined3d_device
*device
, unsigned int idx
)
2778 TRACE("device %p, idx %u.\n", device
, idx
);
2780 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
);
2783 void CDECL
wined3d_device_set_ds_resource_view(struct wined3d_device
*device
,
2784 unsigned int idx
, struct wined3d_shader_resource_view
*view
)
2786 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2788 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
, view
);
2791 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_ds_resource_view(const struct wined3d_device
*device
,
2794 TRACE("device %p, idx %u.\n", device
, idx
);
2796 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
);
2799 void CDECL
wined3d_device_set_ds_sampler(struct wined3d_device
*device
,
2800 unsigned int idx
, struct wined3d_sampler
*sampler
)
2802 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2804 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
, sampler
);
2807 struct wined3d_sampler
* CDECL
wined3d_device_get_ds_sampler(const struct wined3d_device
*device
, unsigned int idx
)
2809 TRACE("device %p, idx %u.\n", device
, idx
);
2811 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
);
2814 void CDECL
wined3d_device_set_geometry_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2816 struct wined3d_shader
*prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2818 TRACE("device %p, shader %p.\n", device
, shader
);
2820 if (device
->recording
|| shader
== prev
)
2823 wined3d_shader_incref(shader
);
2824 device
->update_state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
] = shader
;
2825 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_GEOMETRY
, shader
);
2827 wined3d_shader_decref(prev
);
2830 struct wined3d_shader
* CDECL
wined3d_device_get_geometry_shader(const struct wined3d_device
*device
)
2832 TRACE("device %p.\n", device
);
2834 return device
->state
.shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2837 void CDECL
wined3d_device_set_gs_cb(struct wined3d_device
*device
, UINT idx
, struct wined3d_buffer
*buffer
)
2839 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2841 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, buffer
);
2844 struct wined3d_buffer
* CDECL
wined3d_device_get_gs_cb(const struct wined3d_device
*device
, UINT idx
)
2846 TRACE("device %p, idx %u.\n", device
, idx
);
2848 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2851 void CDECL
wined3d_device_set_gs_resource_view(struct wined3d_device
*device
,
2852 UINT idx
, struct wined3d_shader_resource_view
*view
)
2854 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2856 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, view
);
2859 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_gs_resource_view(const struct wined3d_device
*device
,
2862 TRACE("device %p, idx %u.\n", device
, idx
);
2864 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2867 void CDECL
wined3d_device_set_gs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2869 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2871 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, sampler
);
2874 struct wined3d_sampler
* CDECL
wined3d_device_get_gs_sampler(const struct wined3d_device
*device
, UINT idx
)
2876 TRACE("device %p, idx %u.\n", device
, idx
);
2878 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2881 void CDECL
wined3d_device_set_compute_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2883 struct wined3d_shader
*prev
;
2885 TRACE("device %p, shader %p.\n", device
, shader
);
2887 prev
= device
->update_state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
];
2888 if (device
->recording
|| shader
== prev
)
2891 wined3d_shader_incref(shader
);
2892 device
->update_state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
] = shader
;
2893 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_COMPUTE
, shader
);
2895 wined3d_shader_decref(prev
);
2898 struct wined3d_shader
* CDECL
wined3d_device_get_compute_shader(const struct wined3d_device
*device
)
2900 TRACE("device %p.\n", device
);
2902 return device
->state
.shader
[WINED3D_SHADER_TYPE_COMPUTE
];
2905 void CDECL
wined3d_device_set_cs_cb(struct wined3d_device
*device
, unsigned int idx
, struct wined3d_buffer
*buffer
)
2907 TRACE("device %p, idx %u, buffer %p.\n", device
, idx
, buffer
);
2909 wined3d_device_set_constant_buffer(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, buffer
);
2912 struct wined3d_buffer
* CDECL
wined3d_device_get_cs_cb(const struct wined3d_device
*device
, unsigned int idx
)
2914 TRACE("device %p, idx %u.\n", device
, idx
);
2916 return wined3d_device_get_constant_buffer(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2919 void CDECL
wined3d_device_set_cs_resource_view(struct wined3d_device
*device
,
2920 unsigned int idx
, struct wined3d_shader_resource_view
*view
)
2922 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2924 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, view
);
2927 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_cs_resource_view(const struct wined3d_device
*device
,
2930 TRACE("device %p, idx %u.\n", device
, idx
);
2932 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2935 void CDECL
wined3d_device_set_cs_sampler(struct wined3d_device
*device
,
2936 unsigned int idx
, struct wined3d_sampler
*sampler
)
2938 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2940 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, sampler
);
2943 struct wined3d_sampler
* CDECL
wined3d_device_get_cs_sampler(const struct wined3d_device
*device
, unsigned int idx
)
2945 TRACE("device %p, idx %u.\n", device
, idx
);
2947 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2950 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device
*device
,
2951 enum wined3d_pipeline pipeline
, unsigned int idx
, struct wined3d_unordered_access_view
*uav
,
2952 unsigned int initial_count
)
2954 struct wined3d_unordered_access_view
*prev
;
2956 if (idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
2958 WARN("Invalid UAV index %u.\n", idx
);
2962 prev
= device
->update_state
->unordered_access_view
[pipeline
][idx
];
2963 if (uav
== prev
&& initial_count
== ~0u)
2967 wined3d_unordered_access_view_incref(uav
);
2968 device
->update_state
->unordered_access_view
[pipeline
][idx
] = uav
;
2969 if (!device
->recording
)
2970 wined3d_cs_emit_set_unordered_access_view(device
->cs
, pipeline
, idx
, uav
, initial_count
);
2972 wined3d_unordered_access_view_decref(prev
);
2975 static struct wined3d_unordered_access_view
*wined3d_device_get_pipeline_unordered_access_view(
2976 const struct wined3d_device
*device
, enum wined3d_pipeline pipeline
, unsigned int idx
)
2978 if (idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
2980 WARN("Invalid UAV index %u.\n", idx
);
2984 return device
->state
.unordered_access_view
[pipeline
][idx
];
2987 void CDECL
wined3d_device_set_cs_uav(struct wined3d_device
*device
, unsigned int idx
,
2988 struct wined3d_unordered_access_view
*uav
, unsigned int initial_count
)
2990 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device
, idx
, uav
, initial_count
);
2992 wined3d_device_set_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_COMPUTE
, idx
, uav
, initial_count
);
2995 struct wined3d_unordered_access_view
* CDECL
wined3d_device_get_cs_uav(const struct wined3d_device
*device
,
2998 TRACE("device %p, idx %u.\n", device
, idx
);
3000 return wined3d_device_get_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_COMPUTE
, idx
);
3003 void CDECL
wined3d_device_set_unordered_access_view(struct wined3d_device
*device
,
3004 unsigned int idx
, struct wined3d_unordered_access_view
*uav
, unsigned int initial_count
)
3006 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device
, idx
, uav
, initial_count
);
3008 wined3d_device_set_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_GRAPHICS
, idx
, uav
, initial_count
);
3011 struct wined3d_unordered_access_view
* CDECL
wined3d_device_get_unordered_access_view(
3012 const struct wined3d_device
*device
, unsigned int idx
)
3014 TRACE("device %p, idx %u.\n", device
, idx
);
3016 return wined3d_device_get_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_GRAPHICS
, idx
);
3019 /* Context activation is done by the caller. */
3020 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3021 static HRESULT
process_vertices_strided(const struct wined3d_device
*device
, DWORD dwDestIndex
, DWORD dwCount
,
3022 const struct wined3d_stream_info
*stream_info
, struct wined3d_buffer
*dest
, DWORD flags
,
3025 struct wined3d_matrix mat
, proj_mat
, view_mat
, world_mat
;
3026 struct wined3d_map_desc map_desc
;
3027 struct wined3d_box box
= {0};
3028 struct wined3d_viewport vp
;
3036 if (stream_info
->use_map
& (1u << WINED3D_FFP_NORMAL
))
3038 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3041 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_POSITION
)))
3043 ERR("Source has no position mask\n");
3044 return WINED3DERR_INVALIDCALL
;
3047 if (device
->state
.render_states
[WINED3D_RS_CLIPPING
])
3049 static BOOL warned
= FALSE
;
3051 * The clipping code is not quite correct. Some things need
3052 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3053 * so disable clipping for now.
3054 * (The graphics in Half-Life are broken, and my processvertices
3055 * test crashes with IDirect3DDevice3)
3061 FIXME("Clipping is broken and disabled for now\n");
3067 vertex_size
= get_flexible_vertex_size(DestFVF
);
3068 box
.left
= dwDestIndex
* vertex_size
;
3069 box
.right
= box
.left
+ dwCount
* vertex_size
;
3070 if (FAILED(hr
= wined3d_resource_map(&dest
->resource
, 0, &map_desc
, &box
, 0)))
3072 WARN("Failed to map buffer, hr %#x.\n", hr
);
3075 dest_ptr
= map_desc
.data
;
3077 wined3d_device_get_transform(device
, WINED3D_TS_VIEW
, &view_mat
);
3078 wined3d_device_get_transform(device
, WINED3D_TS_PROJECTION
, &proj_mat
);
3079 wined3d_device_get_transform(device
, WINED3D_TS_WORLD_MATRIX(0), &world_mat
);
3081 TRACE("View mat:\n");
3082 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._11
, view_mat
._12
, view_mat
._13
, view_mat
._14
);
3083 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._21
, view_mat
._22
, view_mat
._23
, view_mat
._24
);
3084 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._31
, view_mat
._32
, view_mat
._33
, view_mat
._34
);
3085 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._41
, view_mat
._42
, view_mat
._43
, view_mat
._44
);
3087 TRACE("Proj mat:\n");
3088 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._11
, proj_mat
._12
, proj_mat
._13
, proj_mat
._14
);
3089 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._21
, proj_mat
._22
, proj_mat
._23
, proj_mat
._24
);
3090 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._31
, proj_mat
._32
, proj_mat
._33
, proj_mat
._34
);
3091 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._41
, proj_mat
._42
, proj_mat
._43
, proj_mat
._44
);
3093 TRACE("World mat:\n");
3094 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._11
, world_mat
._12
, world_mat
._13
, world_mat
._14
);
3095 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._21
, world_mat
._22
, world_mat
._23
, world_mat
._24
);
3096 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._31
, world_mat
._32
, world_mat
._33
, world_mat
._34
);
3097 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._41
, world_mat
._42
, world_mat
._43
, world_mat
._44
);
3099 /* Get the viewport */
3100 wined3d_device_get_viewport(device
, &vp
);
3101 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3102 vp
.x
, vp
.y
, vp
.width
, vp
.height
, vp
.min_z
, vp
.max_z
);
3104 multiply_matrix(&mat
,&view_mat
,&world_mat
);
3105 multiply_matrix(&mat
,&proj_mat
,&mat
);
3107 numTextures
= (DestFVF
& WINED3DFVF_TEXCOUNT_MASK
) >> WINED3DFVF_TEXCOUNT_SHIFT
;
3109 for (i
= 0; i
< dwCount
; i
+= 1) {
3110 unsigned int tex_index
;
3112 if ( ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZ
) ||
3113 ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
) ) {
3114 /* The position first */
3115 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_POSITION
];
3116 const float *p
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3118 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p
[0], p
[1], p
[2]);
3120 /* Multiplication with world, view and projection matrix. */
3121 x
= (p
[0] * mat
._11
) + (p
[1] * mat
._21
) + (p
[2] * mat
._31
) + mat
._41
;
3122 y
= (p
[0] * mat
._12
) + (p
[1] * mat
._22
) + (p
[2] * mat
._32
) + mat
._42
;
3123 z
= (p
[0] * mat
._13
) + (p
[1] * mat
._23
) + (p
[2] * mat
._33
) + mat
._43
;
3124 rhw
= (p
[0] * mat
._14
) + (p
[1] * mat
._24
) + (p
[2] * mat
._34
) + mat
._44
;
3126 TRACE("x=%f y=%f z=%f rhw=%f\n", x
, y
, z
, rhw
);
3128 /* WARNING: The following things are taken from d3d7 and were not yet checked
3129 * against d3d8 or d3d9!
3132 /* Clipping conditions: From msdn
3134 * A vertex is clipped if it does not match the following requirements
3138 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3140 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3141 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3146 ( (-rhw
-eps
< x
) && (-rhw
-eps
< y
) && ( -eps
< z
) &&
3147 (x
<= rhw
+ eps
) && (y
<= rhw
+ eps
) && (z
<= rhw
+ eps
) &&
3150 /* "Normal" viewport transformation (not clipped)
3151 * 1) The values are divided by rhw
3152 * 2) The y axis is negative, so multiply it with -1
3153 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3154 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3155 * 4) Multiply x with Width/2 and add Width/2
3156 * 5) The same for the height
3157 * 6) Add the viewpoint X and Y to the 2D coordinates and
3158 * The minimum Z value to z
3159 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3161 * Well, basically it's simply a linear transformation into viewport
3173 z
*= vp
.max_z
- vp
.min_z
;
3175 x
+= vp
.width
/ 2 + vp
.x
;
3176 y
+= vp
.height
/ 2 + vp
.y
;
3181 /* That vertex got clipped
3182 * Contrary to OpenGL it is not dropped completely, it just
3183 * undergoes a different calculation.
3185 TRACE("Vertex got clipped\n");
3192 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3193 * outside of the main vertex buffer memory. That needs some more
3198 TRACE("Writing (%f %f %f) %f\n", x
, y
, z
, rhw
);
3201 ( (float *) dest_ptr
)[0] = x
;
3202 ( (float *) dest_ptr
)[1] = y
;
3203 ( (float *) dest_ptr
)[2] = z
;
3204 ( (float *) dest_ptr
)[3] = rhw
; /* SIC, see ddraw test! */
3206 dest_ptr
+= 3 * sizeof(float);
3208 if ((DestFVF
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
)
3209 dest_ptr
+= sizeof(float);
3212 if (DestFVF
& WINED3DFVF_PSIZE
)
3213 dest_ptr
+= sizeof(DWORD
);
3215 if (DestFVF
& WINED3DFVF_NORMAL
)
3217 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_NORMAL
];
3218 const float *normal
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3219 /* AFAIK this should go into the lighting information */
3220 FIXME("Didn't expect the destination to have a normal\n");
3221 copy_and_next(dest_ptr
, normal
, 3 * sizeof(float));
3224 if (DestFVF
& WINED3DFVF_DIFFUSE
)
3226 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_DIFFUSE
];
3227 const DWORD
*color_d
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3228 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_DIFFUSE
)))
3230 static BOOL warned
= FALSE
;
3233 ERR("No diffuse color in source, but destination has one\n");
3237 *( (DWORD
*) dest_ptr
) = 0xffffffff;
3238 dest_ptr
+= sizeof(DWORD
);
3242 copy_and_next(dest_ptr
, color_d
, sizeof(DWORD
));
3246 if (DestFVF
& WINED3DFVF_SPECULAR
)
3248 /* What's the color value in the feedback buffer? */
3249 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_SPECULAR
];
3250 const DWORD
*color_s
= (const DWORD
*)(element
->data
.addr
+ i
* element
->stride
);
3251 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_SPECULAR
)))
3253 static BOOL warned
= FALSE
;
3256 ERR("No specular color in source, but destination has one\n");
3260 *(DWORD
*)dest_ptr
= 0xff000000;
3261 dest_ptr
+= sizeof(DWORD
);
3265 copy_and_next(dest_ptr
, color_s
, sizeof(DWORD
));
3269 for (tex_index
= 0; tex_index
< numTextures
; ++tex_index
)
3271 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_TEXCOORD0
+ tex_index
];
3272 const float *tex_coord
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3273 if (!(stream_info
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ tex_index
))))
3275 ERR("No source texture, but destination requests one\n");
3276 dest_ptr
+= GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float);
3280 copy_and_next(dest_ptr
, tex_coord
, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF
, tex_index
) * sizeof(float));
3285 wined3d_resource_unmap(&dest
->resource
, 0);
3289 #undef copy_and_next
3291 HRESULT CDECL
wined3d_device_process_vertices(struct wined3d_device
*device
,
3292 UINT src_start_idx
, UINT dst_idx
, UINT vertex_count
, struct wined3d_buffer
*dst_buffer
,
3293 const struct wined3d_vertex_declaration
*declaration
, DWORD flags
, DWORD dst_fvf
)
3295 struct wined3d_state
*state
= &device
->state
;
3296 struct wined3d_stream_info stream_info
;
3297 struct wined3d_resource
*resource
;
3298 struct wined3d_box box
= {0};
3299 struct wined3d_shader
*vs
;
3304 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3305 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3306 device
, src_start_idx
, dst_idx
, vertex_count
,
3307 dst_buffer
, declaration
, flags
, dst_fvf
);
3310 FIXME("Output vertex declaration not implemented yet.\n");
3312 vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3313 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = NULL
;
3314 wined3d_stream_info_from_declaration(&stream_info
, state
, &device
->adapter
->gl_info
, &device
->adapter
->d3d_info
);
3315 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = vs
;
3317 /* We can't convert FROM a VBO, and vertex buffers used to source into
3318 * process_vertices() are unlikely to ever be used for drawing. Release
3319 * VBOs in those buffers and fix up the stream_info structure.
3321 * Also apply the start index. */
3322 for (i
= 0, map
= stream_info
.use_map
; map
; map
>>= 1, ++i
)
3324 struct wined3d_stream_info_element
*e
;
3325 struct wined3d_map_desc map_desc
;
3330 e
= &stream_info
.elements
[i
];
3331 resource
= &state
->streams
[e
->stream_idx
].buffer
->resource
;
3332 box
.left
= src_start_idx
* e
->stride
;
3333 box
.right
= box
.left
+ vertex_count
* e
->stride
;
3334 if (FAILED(wined3d_resource_map(resource
, 0, &map_desc
, &box
, WINED3D_MAP_READONLY
)))
3335 ERR("Failed to map resource.\n");
3336 e
->data
.buffer_object
= 0;
3337 e
->data
.addr
+= (ULONG_PTR
)map_desc
.data
;
3340 hr
= process_vertices_strided(device
, dst_idx
, vertex_count
,
3341 &stream_info
, dst_buffer
, flags
, dst_fvf
);
3343 for (i
= 0, map
= stream_info
.use_map
; map
; map
>>= 1, ++i
)
3348 resource
= &state
->streams
[stream_info
.elements
[i
].stream_idx
].buffer
->resource
;
3349 if (FAILED(wined3d_resource_unmap(resource
, 0)))
3350 ERR("Failed to unmap resource.\n");
3356 void CDECL
wined3d_device_set_texture_stage_state(struct wined3d_device
*device
,
3357 UINT stage
, enum wined3d_texture_stage_state state
, DWORD value
)
3359 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3362 TRACE("device %p, stage %u, state %s, value %#x.\n",
3363 device
, stage
, debug_d3dtexturestate(state
), value
);
3365 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3367 WARN("Invalid state %#x passed.\n", state
);
3371 if (stage
>= d3d_info
->limits
.ffp_blend_stages
)
3373 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3374 stage
, d3d_info
->limits
.ffp_blend_stages
- 1);
3378 old_value
= device
->update_state
->texture_states
[stage
][state
];
3379 device
->update_state
->texture_states
[stage
][state
] = value
;
3381 if (device
->recording
)
3383 TRACE("Recording... not performing anything.\n");
3384 device
->recording
->changed
.textureState
[stage
] |= 1u << state
;
3388 /* Checked after the assignments to allow proper stateblock recording. */
3389 if (old_value
== value
)
3391 TRACE("Application is setting the old value over, nothing to do.\n");
3395 wined3d_cs_emit_set_texture_state(device
->cs
, stage
, state
, value
);
3398 DWORD CDECL
wined3d_device_get_texture_stage_state(const struct wined3d_device
*device
,
3399 UINT stage
, enum wined3d_texture_stage_state state
)
3401 TRACE("device %p, stage %u, state %s.\n",
3402 device
, stage
, debug_d3dtexturestate(state
));
3404 if (state
> WINED3D_HIGHEST_TEXTURE_STATE
)
3406 WARN("Invalid state %#x passed.\n", state
);
3410 return device
->state
.texture_states
[stage
][state
];
3413 HRESULT CDECL
wined3d_device_set_texture(struct wined3d_device
*device
,
3414 UINT stage
, struct wined3d_texture
*texture
)
3416 struct wined3d_texture
*prev
;
3418 TRACE("device %p, stage %u, texture %p.\n", device
, stage
, texture
);
3420 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3421 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3423 /* Windows accepts overflowing this array... we do not. */
3424 if (stage
>= ARRAY_SIZE(device
->state
.textures
))
3426 WARN("Ignoring invalid stage %u.\n", stage
);
3430 if (texture
&& texture
->resource
.usage
& WINED3DUSAGE_SCRATCH
)
3432 WARN("Rejecting attempt to set scratch texture.\n");
3433 return WINED3DERR_INVALIDCALL
;
3436 if (device
->recording
)
3437 device
->recording
->changed
.textures
|= 1u << stage
;
3439 prev
= device
->update_state
->textures
[stage
];
3440 TRACE("Previous texture %p.\n", prev
);
3442 if (texture
== prev
)
3444 TRACE("App is setting the same texture again, nothing to do.\n");
3448 TRACE("Setting new texture to %p.\n", texture
);
3449 device
->update_state
->textures
[stage
] = texture
;
3452 wined3d_texture_incref(texture
);
3453 if (!device
->recording
)
3454 wined3d_cs_emit_set_texture(device
->cs
, stage
, texture
);
3456 wined3d_texture_decref(prev
);
3461 struct wined3d_texture
* CDECL
wined3d_device_get_texture(const struct wined3d_device
*device
, UINT stage
)
3463 TRACE("device %p, stage %u.\n", device
, stage
);
3465 if (stage
>= WINED3DVERTEXTEXTURESAMPLER0
&& stage
<= WINED3DVERTEXTEXTURESAMPLER3
)
3466 stage
-= (WINED3DVERTEXTEXTURESAMPLER0
- MAX_FRAGMENT_SAMPLERS
);
3468 if (stage
>= ARRAY_SIZE(device
->state
.textures
))
3470 WARN("Ignoring invalid stage %u.\n", stage
);
3471 return NULL
; /* Windows accepts overflowing this array ... we do not. */
3474 return device
->state
.textures
[stage
];
3477 HRESULT CDECL
wined3d_device_get_device_caps(const struct wined3d_device
*device
, WINED3DCAPS
*caps
)
3479 TRACE("device %p, caps %p.\n", device
, caps
);
3481 return wined3d_get_device_caps(device
->wined3d
, device
->adapter
->ordinal
,
3482 device
->create_parms
.device_type
, caps
);
3485 HRESULT CDECL
wined3d_device_get_display_mode(const struct wined3d_device
*device
, UINT swapchain_idx
,
3486 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
3488 struct wined3d_swapchain
*swapchain
;
3490 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3491 device
, swapchain_idx
, mode
, rotation
);
3493 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3494 return WINED3DERR_INVALIDCALL
;
3496 return wined3d_swapchain_get_display_mode(swapchain
, mode
, rotation
);
3499 HRESULT CDECL
wined3d_device_begin_stateblock(struct wined3d_device
*device
)
3501 struct wined3d_stateblock
*stateblock
;
3504 TRACE("device %p.\n", device
);
3506 if (device
->recording
)
3507 return WINED3DERR_INVALIDCALL
;
3509 hr
= wined3d_stateblock_create(device
, WINED3D_SBT_RECORDED
, &stateblock
);
3513 device
->recording
= stateblock
;
3514 device
->update_state
= &stateblock
->state
;
3516 TRACE("Recording stateblock %p.\n", stateblock
);
3521 HRESULT CDECL
wined3d_device_end_stateblock(struct wined3d_device
*device
,
3522 struct wined3d_stateblock
**stateblock
)
3524 struct wined3d_stateblock
*object
= device
->recording
;
3526 TRACE("device %p, stateblock %p.\n", device
, stateblock
);
3528 if (!device
->recording
)
3530 WARN("Not recording.\n");
3532 return WINED3DERR_INVALIDCALL
;
3535 stateblock_init_contained_states(object
);
3537 *stateblock
= object
;
3538 device
->recording
= NULL
;
3539 device
->update_state
= &device
->state
;
3541 TRACE("Returning stateblock %p.\n", *stateblock
);
3546 HRESULT CDECL
wined3d_device_begin_scene(struct wined3d_device
*device
)
3548 /* At the moment we have no need for any functionality at the beginning
3550 TRACE("device %p.\n", device
);
3552 if (device
->inScene
)
3554 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3555 return WINED3DERR_INVALIDCALL
;
3557 device
->inScene
= TRUE
;
3561 HRESULT CDECL
wined3d_device_end_scene(struct wined3d_device
*device
)
3563 TRACE("device %p.\n", device
);
3565 if (!device
->inScene
)
3567 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3568 return WINED3DERR_INVALIDCALL
;
3571 wined3d_cs_emit_flush(device
->cs
);
3573 device
->inScene
= FALSE
;
3577 HRESULT CDECL
wined3d_device_clear(struct wined3d_device
*device
, DWORD rect_count
,
3578 const RECT
*rects
, DWORD flags
, const struct wined3d_color
*color
, float depth
, DWORD stencil
)
3580 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3581 device
, rect_count
, rects
, flags
, debug_color(color
), depth
, stencil
);
3583 if (!rect_count
&& rects
)
3585 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects
);
3589 if (flags
& (WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
))
3591 struct wined3d_rendertarget_view
*ds
= device
->fb
.depth_stencil
;
3594 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3595 /* TODO: What about depth stencil buffers without stencil bits? */
3596 return WINED3DERR_INVALIDCALL
;
3598 else if (flags
& WINED3DCLEAR_TARGET
)
3600 if (ds
->width
< device
->fb
.render_targets
[0]->width
3601 || ds
->height
< device
->fb
.render_targets
[0]->height
)
3603 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3609 wined3d_cs_emit_clear(device
->cs
, rect_count
, rects
, flags
, color
, depth
, stencil
);
3614 void CDECL
wined3d_device_set_predication(struct wined3d_device
*device
,
3615 struct wined3d_query
*predicate
, BOOL value
)
3617 struct wined3d_query
*prev
;
3619 TRACE("device %p, predicate %p, value %#x.\n", device
, predicate
, value
);
3621 prev
= device
->update_state
->predicate
;
3624 FIXME("Predicated rendering not implemented.\n");
3625 wined3d_query_incref(predicate
);
3627 device
->update_state
->predicate
= predicate
;
3628 device
->update_state
->predicate_value
= value
;
3629 if (!device
->recording
)
3630 wined3d_cs_emit_set_predication(device
->cs
, predicate
, value
);
3632 wined3d_query_decref(prev
);
3635 struct wined3d_query
* CDECL
wined3d_device_get_predication(struct wined3d_device
*device
, BOOL
*value
)
3637 TRACE("device %p, value %p.\n", device
, value
);
3640 *value
= device
->state
.predicate_value
;
3641 return device
->state
.predicate
;
3644 void CDECL
wined3d_device_dispatch_compute(struct wined3d_device
*device
,
3645 unsigned int group_count_x
, unsigned int group_count_y
, unsigned int group_count_z
)
3647 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3648 device
, group_count_x
, group_count_y
, group_count_z
);
3650 wined3d_cs_emit_dispatch(device
->cs
, group_count_x
, group_count_y
, group_count_z
);
3653 void CDECL
wined3d_device_dispatch_compute_indirect(struct wined3d_device
*device
,
3654 struct wined3d_buffer
*buffer
, unsigned int offset
)
3656 TRACE("device %p, buffer %p, offset %u.\n", device
, buffer
, offset
);
3658 wined3d_cs_emit_dispatch_indirect(device
->cs
, buffer
, offset
);
3661 void CDECL
wined3d_device_set_primitive_type(struct wined3d_device
*device
,
3662 enum wined3d_primitive_type primitive_type
, unsigned int patch_vertex_count
)
3664 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
3665 device
, debug_d3dprimitivetype(primitive_type
), patch_vertex_count
);
3667 device
->state
.gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
3668 device
->state
.gl_patch_vertices
= patch_vertex_count
;
3671 void CDECL
wined3d_device_get_primitive_type(const struct wined3d_device
*device
,
3672 enum wined3d_primitive_type
*primitive_type
, unsigned int *patch_vertex_count
)
3674 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
3675 device
, primitive_type
, patch_vertex_count
);
3677 *primitive_type
= d3d_primitive_type_from_gl(device
->state
.gl_primitive_type
);
3678 if (patch_vertex_count
)
3679 *patch_vertex_count
= device
->state
.gl_patch_vertices
;
3681 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type
));
3684 HRESULT CDECL
wined3d_device_draw_primitive(struct wined3d_device
*device
, UINT start_vertex
, UINT vertex_count
)
3686 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device
, start_vertex
, vertex_count
);
3688 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
, device
->state
.gl_patch_vertices
,
3689 0, start_vertex
, vertex_count
, 0, 0, FALSE
);
3694 void CDECL
wined3d_device_draw_primitive_instanced(struct wined3d_device
*device
,
3695 UINT start_vertex
, UINT vertex_count
, UINT start_instance
, UINT instance_count
)
3697 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3698 device
, start_vertex
, vertex_count
, start_instance
, instance_count
);
3700 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
, device
->state
.gl_patch_vertices
,
3701 0, start_vertex
, vertex_count
, start_instance
, instance_count
, FALSE
);
3704 void CDECL
wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device
*device
,
3705 struct wined3d_buffer
*buffer
, unsigned int offset
)
3707 TRACE("device %p, buffer %p, offset %u.\n", device
, buffer
, offset
);
3709 wined3d_cs_emit_draw_indirect(device
->cs
, device
->state
.gl_primitive_type
, device
->state
.gl_patch_vertices
,
3710 buffer
, offset
, FALSE
);
3713 HRESULT CDECL
wined3d_device_draw_indexed_primitive(struct wined3d_device
*device
, UINT start_idx
, UINT index_count
)
3715 TRACE("device %p, start_idx %u, index_count %u.\n", device
, start_idx
, index_count
);
3717 if (!device
->state
.index_buffer
)
3719 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3720 * without an index buffer set. (The first time at least...)
3721 * D3D8 simply dies, but I doubt it can do much harm to return
3722 * D3DERR_INVALIDCALL there as well. */
3723 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3724 return WINED3DERR_INVALIDCALL
;
3727 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
, device
->state
.gl_patch_vertices
,
3728 device
->state
.base_vertex_index
, start_idx
, index_count
, 0, 0, TRUE
);
3733 void CDECL
wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device
*device
,
3734 UINT start_idx
, UINT index_count
, UINT start_instance
, UINT instance_count
)
3736 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3737 device
, start_idx
, index_count
, start_instance
, instance_count
);
3739 wined3d_cs_emit_draw(device
->cs
, device
->state
.gl_primitive_type
, device
->state
.gl_patch_vertices
,
3740 device
->state
.base_vertex_index
, start_idx
, index_count
, start_instance
, instance_count
, TRUE
);
3743 void CDECL
wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device
*device
,
3744 struct wined3d_buffer
*buffer
, unsigned int offset
)
3746 TRACE("device %p, buffer %p, offset %u.\n", device
, buffer
, offset
);
3748 wined3d_cs_emit_draw_indirect(device
->cs
, device
->state
.gl_primitive_type
, device
->state
.gl_patch_vertices
,
3749 buffer
, offset
, TRUE
);
3752 HRESULT CDECL
wined3d_device_update_texture(struct wined3d_device
*device
,
3753 struct wined3d_texture
*src_texture
, struct wined3d_texture
*dst_texture
)
3755 unsigned int src_size
, dst_size
, src_skip_levels
= 0;
3756 unsigned int src_level_count
, dst_level_count
;
3757 unsigned int layer_count
, level_count
, i
, j
;
3758 unsigned int width
, height
, depth
;
3759 enum wined3d_resource_type type
;
3760 struct wined3d_box box
;
3762 TRACE("device %p, src_texture %p, dst_texture %p.\n", device
, src_texture
, dst_texture
);
3764 /* Verify that the source and destination textures are non-NULL. */
3765 if (!src_texture
|| !dst_texture
)
3767 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3768 return WINED3DERR_INVALIDCALL
;
3771 if (src_texture
->resource
.access
& WINED3D_RESOURCE_ACCESS_GPU
3772 || src_texture
->resource
.usage
& WINED3DUSAGE_SCRATCH
)
3774 WARN("Source resource is GPU accessible or a scratch resource.\n");
3775 return WINED3DERR_INVALIDCALL
;
3777 if (dst_texture
->resource
.access
& WINED3D_RESOURCE_ACCESS_CPU
)
3779 WARN("Destination resource is CPU accessible.\n");
3780 return WINED3DERR_INVALIDCALL
;
3783 /* Verify that the source and destination textures are the same type. */
3784 type
= src_texture
->resource
.type
;
3785 if (dst_texture
->resource
.type
!= type
)
3787 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3788 return WINED3DERR_INVALIDCALL
;
3791 layer_count
= src_texture
->layer_count
;
3792 if (layer_count
!= dst_texture
->layer_count
)
3794 WARN("Source and destination have different layer counts.\n");
3795 return WINED3DERR_INVALIDCALL
;
3798 if (src_texture
->resource
.format
!= dst_texture
->resource
.format
)
3800 WARN("Source and destination formats do not match.\n");
3801 return WINED3DERR_INVALIDCALL
;
3804 src_level_count
= src_texture
->level_count
;
3805 dst_level_count
= dst_texture
->level_count
;
3806 level_count
= min(src_level_count
, dst_level_count
);
3808 src_size
= max(src_texture
->resource
.width
, src_texture
->resource
.height
);
3809 dst_size
= max(dst_texture
->resource
.width
, dst_texture
->resource
.height
);
3810 if (type
== WINED3D_RTYPE_TEXTURE_3D
)
3812 src_size
= max(src_size
, src_texture
->resource
.depth
);
3813 dst_size
= max(dst_size
, dst_texture
->resource
.depth
);
3815 while (src_size
> dst_size
)
3821 if (wined3d_texture_get_level_width(src_texture
, src_skip_levels
) != dst_texture
->resource
.width
3822 || wined3d_texture_get_level_height(src_texture
, src_skip_levels
) != dst_texture
->resource
.height
3823 || wined3d_texture_get_level_depth(src_texture
, src_skip_levels
) != dst_texture
->resource
.depth
)
3825 WARN("Source and destination dimensions do not match.\n");
3826 return WINED3DERR_INVALIDCALL
;
3829 /* Update every surface level of the texture. */
3830 for (i
= 0; i
< level_count
; ++i
)
3832 width
= wined3d_texture_get_level_width(dst_texture
, i
);
3833 height
= wined3d_texture_get_level_height(dst_texture
, i
);
3834 depth
= wined3d_texture_get_level_depth(dst_texture
, i
);
3835 wined3d_box_set(&box
, 0, 0, width
, height
, 0, depth
);
3837 for (j
= 0; j
< layer_count
; ++j
)
3839 wined3d_cs_emit_blt_sub_resource(device
->cs
,
3840 &dst_texture
->resource
, j
* dst_level_count
+ i
, &box
,
3841 &src_texture
->resource
, j
* src_level_count
+ i
+ src_skip_levels
, &box
,
3842 0, NULL
, WINED3D_TEXF_POINT
);
3849 HRESULT CDECL
wined3d_device_validate_device(const struct wined3d_device
*device
, DWORD
*num_passes
)
3851 const struct wined3d_state
*state
= &device
->state
;
3852 struct wined3d_texture
*texture
;
3855 TRACE("device %p, num_passes %p.\n", device
, num_passes
);
3857 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
3859 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] == WINED3D_TEXF_NONE
)
3861 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
3862 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
3864 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] == WINED3D_TEXF_NONE
)
3866 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
3867 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
3870 texture
= state
->textures
[i
];
3871 if (!texture
|| texture
->resource
.format_flags
& WINED3DFMT_FLAG_FILTERING
) continue;
3873 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] != WINED3D_TEXF_POINT
)
3875 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i
);
3878 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] != WINED3D_TEXF_POINT
)
3880 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i
);
3883 if (state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_NONE
3884 && state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_POINT
)
3886 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i
);
3891 if (state
->render_states
[WINED3D_RS_ZENABLE
] || state
->render_states
[WINED3D_RS_ZWRITEENABLE
]
3892 || state
->render_states
[WINED3D_RS_STENCILENABLE
])
3894 struct wined3d_rendertarget_view
*rt
= device
->fb
.render_targets
[0];
3895 struct wined3d_rendertarget_view
*ds
= device
->fb
.depth_stencil
;
3897 if (ds
&& rt
&& (ds
->width
< rt
->width
|| ds
->height
< rt
->height
))
3899 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3900 return WINED3DERR_CONFLICTINGRENDERSTATE
;
3904 /* return a sensible default */
3907 TRACE("returning D3D_OK\n");
3911 void CDECL
wined3d_device_set_software_vertex_processing(struct wined3d_device
*device
, BOOL software
)
3915 TRACE("device %p, software %#x.\n", device
, software
);
3919 FIXME("device %p, software %#x stub!\n", device
, software
);
3923 device
->softwareVertexProcessing
= software
;
3926 BOOL CDECL
wined3d_device_get_software_vertex_processing(const struct wined3d_device
*device
)
3930 TRACE("device %p.\n", device
);
3934 TRACE("device %p stub!\n", device
);
3938 return device
->softwareVertexProcessing
;
3941 HRESULT CDECL
wined3d_device_get_raster_status(const struct wined3d_device
*device
,
3942 UINT swapchain_idx
, struct wined3d_raster_status
*raster_status
)
3944 struct wined3d_swapchain
*swapchain
;
3946 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3947 device
, swapchain_idx
, raster_status
);
3949 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3950 return WINED3DERR_INVALIDCALL
;
3952 return wined3d_swapchain_get_raster_status(swapchain
, raster_status
);
3955 HRESULT CDECL
wined3d_device_set_npatch_mode(struct wined3d_device
*device
, float segments
)
3959 TRACE("device %p, segments %.8e.\n", device
, segments
);
3961 if (segments
!= 0.0f
)
3965 FIXME("device %p, segments %.8e stub!\n", device
, segments
);
3973 float CDECL
wined3d_device_get_npatch_mode(const struct wined3d_device
*device
)
3977 TRACE("device %p.\n", device
);
3981 FIXME("device %p stub!\n", device
);
3988 void CDECL
wined3d_device_copy_uav_counter(struct wined3d_device
*device
,
3989 struct wined3d_buffer
*dst_buffer
, unsigned int offset
, struct wined3d_unordered_access_view
*uav
)
3991 TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
3992 device
, dst_buffer
, offset
, uav
);
3994 wined3d_cs_emit_copy_uav_counter(device
->cs
, dst_buffer
, offset
, uav
);
3997 void CDECL
wined3d_device_copy_resource(struct wined3d_device
*device
,
3998 struct wined3d_resource
*dst_resource
, struct wined3d_resource
*src_resource
)
4000 struct wined3d_texture
*dst_texture
, *src_texture
;
4001 struct wined3d_box box
;
4004 TRACE("device %p, dst_resource %p, src_resource %p.\n", device
, dst_resource
, src_resource
);
4006 if (src_resource
== dst_resource
)
4008 WARN("Source and destination are the same resource.\n");
4012 if (src_resource
->type
!= dst_resource
->type
)
4014 WARN("Resource types (%s / %s) don't match.\n",
4015 debug_d3dresourcetype(dst_resource
->type
),
4016 debug_d3dresourcetype(src_resource
->type
));
4020 if (src_resource
->width
!= dst_resource
->width
4021 || src_resource
->height
!= dst_resource
->height
4022 || src_resource
->depth
!= dst_resource
->depth
)
4024 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4025 dst_resource
->width
, dst_resource
->height
, dst_resource
->depth
,
4026 src_resource
->width
, src_resource
->height
, src_resource
->depth
);
4030 if (src_resource
->format
->typeless_id
!= dst_resource
->format
->typeless_id
4031 || (!src_resource
->format
->typeless_id
&& src_resource
->format
->id
!= dst_resource
->format
->id
))
4033 WARN("Resource formats %s and %s are incompatible.\n",
4034 debug_d3dformat(dst_resource
->format
->id
),
4035 debug_d3dformat(src_resource
->format
->id
));
4039 if (dst_resource
->type
== WINED3D_RTYPE_BUFFER
)
4041 wined3d_box_set(&box
, 0, 0, src_resource
->size
, 1, 0, 1);
4042 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, 0, &box
,
4043 src_resource
, 0, &box
, WINED3D_BLT_RAW
, NULL
, WINED3D_TEXF_POINT
);
4047 dst_texture
= texture_from_resource(dst_resource
);
4048 src_texture
= texture_from_resource(src_resource
);
4050 if (src_texture
->layer_count
!= dst_texture
->layer_count
4051 || src_texture
->level_count
!= dst_texture
->level_count
)
4053 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4054 dst_texture
->layer_count
, dst_texture
->level_count
,
4055 src_texture
->layer_count
, src_texture
->level_count
);
4059 for (i
= 0; i
< dst_texture
->level_count
; ++i
)
4061 wined3d_box_set(&box
, 0, 0,
4062 wined3d_texture_get_level_width(dst_texture
, i
),
4063 wined3d_texture_get_level_height(dst_texture
, i
),
4064 0, wined3d_texture_get_level_depth(dst_texture
, i
));
4065 for (j
= 0; j
< dst_texture
->layer_count
; ++j
)
4067 unsigned int idx
= j
* dst_texture
->level_count
+ i
;
4069 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, idx
, &box
,
4070 src_resource
, idx
, &box
, WINED3D_BLT_RAW
, NULL
, WINED3D_TEXF_POINT
);
4075 HRESULT CDECL
wined3d_device_copy_sub_resource_region(struct wined3d_device
*device
,
4076 struct wined3d_resource
*dst_resource
, unsigned int dst_sub_resource_idx
, unsigned int dst_x
,
4077 unsigned int dst_y
, unsigned int dst_z
, struct wined3d_resource
*src_resource
,
4078 unsigned int src_sub_resource_idx
, const struct wined3d_box
*src_box
)
4080 struct wined3d_box dst_box
, b
;
4082 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4083 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
4084 device
, dst_resource
, dst_sub_resource_idx
, dst_x
, dst_y
, dst_z
,
4085 src_resource
, src_sub_resource_idx
, debug_box(src_box
));
4087 if (src_resource
== dst_resource
&& src_sub_resource_idx
== dst_sub_resource_idx
)
4089 WARN("Source and destination are the same sub-resource.\n");
4090 return WINED3DERR_INVALIDCALL
;
4093 if (src_resource
->type
!= dst_resource
->type
)
4095 WARN("Resource types (%s / %s) don't match.\n",
4096 debug_d3dresourcetype(dst_resource
->type
),
4097 debug_d3dresourcetype(src_resource
->type
));
4098 return WINED3DERR_INVALIDCALL
;
4101 if (src_resource
->format
->typeless_id
!= dst_resource
->format
->typeless_id
4102 || (!src_resource
->format
->typeless_id
&& src_resource
->format
->id
!= dst_resource
->format
->id
))
4104 WARN("Resource formats %s and %s are incompatible.\n",
4105 debug_d3dformat(dst_resource
->format
->id
),
4106 debug_d3dformat(src_resource
->format
->id
));
4107 return WINED3DERR_INVALIDCALL
;
4110 if (dst_resource
->type
== WINED3D_RTYPE_BUFFER
)
4112 if (dst_sub_resource_idx
)
4114 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx
);
4115 return WINED3DERR_INVALIDCALL
;
4118 if (src_sub_resource_idx
)
4120 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx
);
4121 return WINED3DERR_INVALIDCALL
;
4128 dst_w
= dst_resource
->size
- dst_x
;
4129 wined3d_box_set(&b
, 0, 0, min(src_resource
->size
, dst_w
), 1, 0, 1);
4132 else if ((src_box
->left
>= src_box
->right
4133 || src_box
->top
>= src_box
->bottom
4134 || src_box
->front
>= src_box
->back
))
4136 WARN("Invalid box %s specified.\n", debug_box(src_box
));
4137 return WINED3DERR_INVALIDCALL
;
4140 if (src_box
->right
> src_resource
->size
|| dst_x
>= dst_resource
->size
4141 || src_box
->right
- src_box
->left
> dst_resource
->size
- dst_x
)
4143 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4144 dst_x
, src_box
->left
, src_box
->right
- src_box
->left
);
4145 return WINED3DERR_INVALIDCALL
;
4148 wined3d_box_set(&dst_box
, dst_x
, 0, dst_x
+ (src_box
->right
- src_box
->left
), 1, 0, 1);
4150 else if (dst_resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
4152 struct wined3d_texture
*dst_texture
= texture_from_resource(dst_resource
);
4153 struct wined3d_texture
*src_texture
= texture_from_resource(src_resource
);
4154 unsigned int src_level
= src_sub_resource_idx
% src_texture
->level_count
;
4156 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
)
4158 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx
);
4159 return WINED3DERR_INVALIDCALL
;
4162 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
)
4164 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx
);
4165 return WINED3DERR_INVALIDCALL
;
4168 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
)
4170 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx
);
4171 return WINED3DERR_INVALIDCALL
;
4174 if (src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
4176 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx
);
4177 return WINED3DERR_INVALIDCALL
;
4182 unsigned int src_w
, src_h
, dst_w
, dst_h
, dst_level
;
4184 src_w
= wined3d_texture_get_level_width(src_texture
, src_level
);
4185 src_h
= wined3d_texture_get_level_height(src_texture
, src_level
);
4187 dst_level
= dst_sub_resource_idx
% dst_texture
->level_count
;
4188 dst_w
= wined3d_texture_get_level_width(dst_texture
, dst_level
) - dst_x
;
4189 dst_h
= wined3d_texture_get_level_height(dst_texture
, dst_level
) - dst_y
;
4191 wined3d_box_set(&b
, 0, 0, min(src_w
, dst_w
), min(src_h
, dst_h
), 0, 1);
4194 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture
, src_level
, src_box
)))
4196 WARN("Invalid source box %s.\n", debug_box(src_box
));
4197 return WINED3DERR_INVALIDCALL
;
4200 wined3d_box_set(&dst_box
, dst_x
, dst_y
, dst_x
+ (src_box
->right
- src_box
->left
),
4201 dst_y
+ (src_box
->bottom
- src_box
->top
), 0, 1);
4202 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture
,
4203 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
4205 WARN("Invalid destination box %s.\n", debug_box(&dst_box
));
4206 return WINED3DERR_INVALIDCALL
;
4211 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource
->type
));
4212 return WINED3DERR_INVALIDCALL
;
4215 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, dst_sub_resource_idx
, &dst_box
,
4216 src_resource
, src_sub_resource_idx
, src_box
, WINED3D_BLT_RAW
, NULL
, WINED3D_TEXF_POINT
);
4221 void CDECL
wined3d_device_update_sub_resource(struct wined3d_device
*device
, struct wined3d_resource
*resource
,
4222 unsigned int sub_resource_idx
, const struct wined3d_box
*box
, const void *data
, unsigned int row_pitch
,
4223 unsigned int depth_pitch
)
4225 unsigned int width
, height
, depth
;
4226 struct wined3d_box b
;
4228 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4229 device
, resource
, sub_resource_idx
, debug_box(box
), data
, row_pitch
, depth_pitch
);
4231 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
4233 if (sub_resource_idx
> 0)
4235 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
4239 width
= resource
->size
;
4243 else if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
|| resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
4245 struct wined3d_texture
*texture
= texture_from_resource(resource
);
4248 if (sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
4250 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
4254 level
= sub_resource_idx
% texture
->level_count
;
4255 width
= wined3d_texture_get_level_width(texture
, level
);
4256 height
= wined3d_texture_get_level_height(texture
, level
);
4257 depth
= wined3d_texture_get_level_depth(texture
, level
);
4261 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
4267 wined3d_box_set(&b
, 0, 0, width
, height
, 0, depth
);
4270 else if (box
->left
>= box
->right
|| box
->right
> width
4271 || box
->top
>= box
->bottom
|| box
->bottom
> height
4272 || box
->front
>= box
->back
|| box
->back
> depth
)
4274 WARN("Invalid box %s specified.\n", debug_box(box
));
4278 wined3d_resource_wait_idle(resource
);
4280 wined3d_cs_emit_update_sub_resource(device
->cs
, resource
, sub_resource_idx
, box
, data
, row_pitch
, depth_pitch
);
4283 HRESULT CDECL
wined3d_device_clear_rendertarget_view(struct wined3d_device
*device
,
4284 struct wined3d_rendertarget_view
*view
, const RECT
*rect
, DWORD flags
,
4285 const struct wined3d_color
*color
, float depth
, DWORD stencil
)
4287 struct wined3d_resource
*resource
;
4290 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4291 device
, view
, wine_dbgstr_rect(rect
), flags
, debug_color(color
), depth
, stencil
);
4296 resource
= view
->resource
;
4297 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
4299 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
4300 return WINED3DERR_INVALIDCALL
;
4303 if (view
->layer_count
> 1)
4305 FIXME("Layered clears not implemented.\n");
4306 return WINED3DERR_INVALIDCALL
;
4311 SetRect(&r
, 0, 0, view
->width
, view
->height
);
4316 struct wined3d_box b
= {rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1};
4317 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
4320 if (FAILED(hr
= wined3d_texture_check_box_dimensions(texture
,
4321 view
->sub_resource_idx
% texture
->level_count
, &b
)))
4325 wined3d_cs_emit_clear_rendertarget_view(device
->cs
, view
, rect
, flags
, color
, depth
, stencil
);
4330 void CDECL
wined3d_device_clear_unordered_access_view_uint(struct wined3d_device
*device
,
4331 struct wined3d_unordered_access_view
*view
, const struct wined3d_uvec4
*clear_value
)
4333 TRACE("device %p, view %p, clear_value %s.\n", device
, view
, debug_uvec4(clear_value
));
4335 wined3d_cs_emit_clear_unordered_access_view_uint(device
->cs
, view
, clear_value
);
4338 struct wined3d_rendertarget_view
* CDECL
wined3d_device_get_rendertarget_view(const struct wined3d_device
*device
,
4339 unsigned int view_idx
)
4341 TRACE("device %p, view_idx %u.\n", device
, view_idx
);
4343 if (view_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4345 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4349 return device
->fb
.render_targets
[view_idx
];
4352 struct wined3d_rendertarget_view
* CDECL
wined3d_device_get_depth_stencil_view(const struct wined3d_device
*device
)
4354 TRACE("device %p.\n", device
);
4356 return device
->fb
.depth_stencil
;
4359 HRESULT CDECL
wined3d_device_set_rendertarget_view(struct wined3d_device
*device
,
4360 unsigned int view_idx
, struct wined3d_rendertarget_view
*view
, BOOL set_viewport
)
4362 struct wined3d_rendertarget_view
*prev
;
4364 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4365 device
, view_idx
, view
, set_viewport
);
4367 if (view_idx
>= device
->adapter
->gl_info
.limits
.buffers
)
4369 WARN("Only %u render targets are supported.\n", device
->adapter
->gl_info
.limits
.buffers
);
4370 return WINED3DERR_INVALIDCALL
;
4373 if (view
&& !(view
->resource
->usage
& WINED3DUSAGE_RENDERTARGET
))
4375 WARN("View resource %p doesn't have render target usage.\n", view
->resource
);
4376 return WINED3DERR_INVALIDCALL
;
4379 /* Set the viewport and scissor rectangles, if requested. Tests show that
4380 * stateblock recording is ignored, the change goes directly into the
4381 * primary stateblock. */
4382 if (!view_idx
&& set_viewport
)
4384 struct wined3d_state
*state
= &device
->state
;
4386 state
->viewport
.x
= 0;
4387 state
->viewport
.y
= 0;
4388 state
->viewport
.width
= view
->width
;
4389 state
->viewport
.height
= view
->height
;
4390 state
->viewport
.min_z
= 0.0f
;
4391 state
->viewport
.max_z
= 1.0f
;
4392 wined3d_cs_emit_set_viewport(device
->cs
, &state
->viewport
);
4394 SetRect(&state
->scissor_rect
, 0, 0, view
->width
, view
->height
);
4395 wined3d_cs_emit_set_scissor_rect(device
->cs
, &state
->scissor_rect
);
4399 prev
= device
->fb
.render_targets
[view_idx
];
4404 wined3d_rendertarget_view_incref(view
);
4405 device
->fb
.render_targets
[view_idx
] = view
;
4406 wined3d_cs_emit_set_rendertarget_view(device
->cs
, view_idx
, view
);
4407 /* Release after the assignment, to prevent device_resource_released()
4408 * from seeing the surface as still in use. */
4410 wined3d_rendertarget_view_decref(prev
);
4415 void CDECL
wined3d_device_set_depth_stencil_view(struct wined3d_device
*device
, struct wined3d_rendertarget_view
*view
)
4417 struct wined3d_rendertarget_view
*prev
;
4419 TRACE("device %p, view %p.\n", device
, view
);
4421 prev
= device
->fb
.depth_stencil
;
4424 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4428 if ((device
->fb
.depth_stencil
= view
))
4429 wined3d_rendertarget_view_incref(view
);
4430 wined3d_cs_emit_set_depth_stencil_view(device
->cs
, view
);
4432 wined3d_rendertarget_view_decref(prev
);
4435 static struct wined3d_texture
*wined3d_device_create_cursor_texture(struct wined3d_device
*device
,
4436 struct wined3d_texture
*cursor_image
, unsigned int sub_resource_idx
)
4438 unsigned int texture_level
= sub_resource_idx
% cursor_image
->level_count
;
4439 struct wined3d_sub_resource_data data
;
4440 struct wined3d_resource_desc desc
;
4441 struct wined3d_map_desc map_desc
;
4442 struct wined3d_texture
*texture
;
4445 if (FAILED(wined3d_resource_map(&cursor_image
->resource
, sub_resource_idx
, &map_desc
, NULL
, WINED3D_MAP_READONLY
)))
4447 ERR("Failed to map source texture.\n");
4451 data
.data
= map_desc
.data
;
4452 data
.row_pitch
= map_desc
.row_pitch
;
4453 data
.slice_pitch
= map_desc
.slice_pitch
;
4455 desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
4456 desc
.format
= WINED3DFMT_B8G8R8A8_UNORM
;
4457 desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
4458 desc
.multisample_quality
= 0;
4459 desc
.usage
= WINED3DUSAGE_DYNAMIC
;
4460 desc
.pool
= WINED3D_POOL_DEFAULT
;
4461 desc
.width
= wined3d_texture_get_level_width(cursor_image
, texture_level
);
4462 desc
.height
= wined3d_texture_get_level_height(cursor_image
, texture_level
);
4466 hr
= wined3d_texture_create(device
, &desc
, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE
,
4467 &data
, NULL
, &wined3d_null_parent_ops
, &texture
);
4468 wined3d_resource_unmap(&cursor_image
->resource
, sub_resource_idx
);
4471 ERR("Failed to create cursor texture.\n");
4478 HRESULT CDECL
wined3d_device_set_cursor_properties(struct wined3d_device
*device
,
4479 UINT x_hotspot
, UINT y_hotspot
, struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4481 unsigned int texture_level
= sub_resource_idx
% texture
->level_count
;
4482 unsigned int cursor_width
, cursor_height
;
4483 struct wined3d_display_mode mode
;
4484 struct wined3d_map_desc map_desc
;
4487 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4488 device
, x_hotspot
, y_hotspot
, texture
, sub_resource_idx
);
4490 if (sub_resource_idx
>= texture
->level_count
* texture
->layer_count
4491 || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
4492 return WINED3DERR_INVALIDCALL
;
4494 if (device
->cursor_texture
)
4496 wined3d_texture_decref(device
->cursor_texture
);
4497 device
->cursor_texture
= NULL
;
4500 if (texture
->resource
.format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4502 WARN("Texture %p has invalid format %s.\n",
4503 texture
, debug_d3dformat(texture
->resource
.format
->id
));
4504 return WINED3DERR_INVALIDCALL
;
4507 if (FAILED(hr
= wined3d_get_adapter_display_mode(device
->wined3d
, device
->adapter
->ordinal
, &mode
, NULL
)))
4509 ERR("Failed to get display mode, hr %#x.\n", hr
);
4510 return WINED3DERR_INVALIDCALL
;
4513 cursor_width
= wined3d_texture_get_level_width(texture
, texture_level
);
4514 cursor_height
= wined3d_texture_get_level_height(texture
, texture_level
);
4515 if (cursor_width
> mode
.width
|| cursor_height
> mode
.height
)
4517 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4518 texture
, sub_resource_idx
, cursor_width
, cursor_height
, mode
.width
, mode
.height
);
4519 return WINED3DERR_INVALIDCALL
;
4522 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4524 /* Do not store the surface's pointer because the application may
4525 * release it after setting the cursor image. Windows doesn't
4526 * addref the set surface, so we can't do this either without
4527 * creating circular refcount dependencies. */
4528 if (!(device
->cursor_texture
= wined3d_device_create_cursor_texture(device
, texture
, sub_resource_idx
)))
4530 ERR("Failed to create cursor texture.\n");
4531 return WINED3DERR_INVALIDCALL
;
4534 if (cursor_width
== 32 && cursor_height
== 32)
4536 UINT mask_size
= cursor_width
* cursor_height
/ 8;
4537 ICONINFO cursor_info
;
4541 /* 32-bit user32 cursors ignore the alpha channel if it's all
4542 * zeroes, and use the mask instead. Fill the mask with all ones
4543 * to ensure we still get a fully transparent cursor. */
4544 if (!(mask_bits
= HeapAlloc(GetProcessHeap(), 0, mask_size
)))
4545 return E_OUTOFMEMORY
;
4546 memset(mask_bits
, 0xff, mask_size
);
4548 wined3d_resource_map(&texture
->resource
, sub_resource_idx
, &map_desc
, NULL
,
4549 WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
);
4550 cursor_info
.fIcon
= FALSE
;
4551 cursor_info
.xHotspot
= x_hotspot
;
4552 cursor_info
.yHotspot
= y_hotspot
;
4553 cursor_info
.hbmMask
= CreateBitmap(cursor_width
, cursor_height
, 1, 1, mask_bits
);
4554 cursor_info
.hbmColor
= CreateBitmap(cursor_width
, cursor_height
, 1, 32, map_desc
.data
);
4555 wined3d_resource_unmap(&texture
->resource
, sub_resource_idx
);
4557 /* Create our cursor and clean up. */
4558 cursor
= CreateIconIndirect(&cursor_info
);
4559 if (cursor_info
.hbmMask
)
4560 DeleteObject(cursor_info
.hbmMask
);
4561 if (cursor_info
.hbmColor
)
4562 DeleteObject(cursor_info
.hbmColor
);
4563 if (device
->hardwareCursor
)
4564 DestroyCursor(device
->hardwareCursor
);
4565 device
->hardwareCursor
= cursor
;
4566 if (device
->bCursorVisible
)
4569 HeapFree(GetProcessHeap(), 0, mask_bits
);
4572 TRACE("New cursor dimensions are %ux%u.\n", cursor_width
, cursor_height
);
4573 device
->cursorWidth
= cursor_width
;
4574 device
->cursorHeight
= cursor_height
;
4575 device
->xHotSpot
= x_hotspot
;
4576 device
->yHotSpot
= y_hotspot
;
4581 void CDECL
wined3d_device_set_cursor_position(struct wined3d_device
*device
,
4582 int x_screen_space
, int y_screen_space
, DWORD flags
)
4584 TRACE("device %p, x %d, y %d, flags %#x.\n",
4585 device
, x_screen_space
, y_screen_space
, flags
);
4587 device
->xScreenSpace
= x_screen_space
;
4588 device
->yScreenSpace
= y_screen_space
;
4590 if (device
->hardwareCursor
)
4594 GetCursorPos( &pt
);
4595 if (x_screen_space
== pt
.x
&& y_screen_space
== pt
.y
)
4597 SetCursorPos( x_screen_space
, y_screen_space
);
4599 /* Switch to the software cursor if position diverges from the hardware one. */
4600 GetCursorPos( &pt
);
4601 if (x_screen_space
!= pt
.x
|| y_screen_space
!= pt
.y
)
4603 if (device
->bCursorVisible
) SetCursor( NULL
);
4604 DestroyCursor( device
->hardwareCursor
);
4605 device
->hardwareCursor
= 0;
4610 BOOL CDECL
wined3d_device_show_cursor(struct wined3d_device
*device
, BOOL show
)
4612 BOOL oldVisible
= device
->bCursorVisible
;
4614 TRACE("device %p, show %#x.\n", device
, show
);
4617 * When ShowCursor is first called it should make the cursor appear at the OS's last
4618 * known cursor position.
4620 if (show
&& !oldVisible
)
4624 device
->xScreenSpace
= pt
.x
;
4625 device
->yScreenSpace
= pt
.y
;
4628 if (device
->hardwareCursor
)
4630 device
->bCursorVisible
= show
;
4632 SetCursor(device
->hardwareCursor
);
4636 else if (device
->cursor_texture
)
4638 device
->bCursorVisible
= show
;
4644 void CDECL
wined3d_device_evict_managed_resources(struct wined3d_device
*device
)
4646 struct wined3d_resource
*resource
, *cursor
;
4648 TRACE("device %p.\n", device
);
4650 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4652 TRACE("Checking resource %p for eviction.\n", resource
);
4654 if (wined3d_resource_access_is_managed(resource
->access
) && !resource
->map_count
)
4656 TRACE("Evicting %p.\n", resource
);
4657 wined3d_cs_emit_unload_resource(device
->cs
, resource
);
4662 HRESULT CDECL
wined3d_device_reset(struct wined3d_device
*device
,
4663 const struct wined3d_swapchain_desc
*swapchain_desc
, const struct wined3d_display_mode
*mode
,
4664 wined3d_device_reset_cb callback
, BOOL reset_state
)
4666 struct wined3d_resource
*resource
, *cursor
;
4667 struct wined3d_swapchain
*swapchain
;
4668 struct wined3d_view_desc view_desc
;
4669 BOOL backbuffer_resized
;
4670 HRESULT hr
= WINED3D_OK
;
4673 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4674 device
, swapchain_desc
, mode
, callback
, reset_state
);
4676 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
4678 if (!(swapchain
= wined3d_device_get_swapchain(device
, 0)))
4680 ERR("Failed to get the first implicit swapchain.\n");
4681 return WINED3DERR_INVALIDCALL
;
4686 if (device
->logo_texture
)
4688 wined3d_texture_decref(device
->logo_texture
);
4689 device
->logo_texture
= NULL
;
4691 if (device
->cursor_texture
)
4693 wined3d_texture_decref(device
->cursor_texture
);
4694 device
->cursor_texture
= NULL
;
4696 state_unbind_resources(&device
->state
);
4699 if (device
->fb
.render_targets
)
4701 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
4703 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
4706 wined3d_device_set_depth_stencil_view(device
, NULL
);
4710 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
4712 TRACE("Enumerating resource %p.\n", resource
);
4713 if (FAILED(hr
= callback(resource
)))
4718 TRACE("New params:\n");
4719 TRACE("backbuffer_width %u\n", swapchain_desc
->backbuffer_width
);
4720 TRACE("backbuffer_height %u\n", swapchain_desc
->backbuffer_height
);
4721 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc
->backbuffer_format
));
4722 TRACE("backbuffer_count %u\n", swapchain_desc
->backbuffer_count
);
4723 TRACE("multisample_type %#x\n", swapchain_desc
->multisample_type
);
4724 TRACE("multisample_quality %u\n", swapchain_desc
->multisample_quality
);
4725 TRACE("swap_effect %#x\n", swapchain_desc
->swap_effect
);
4726 TRACE("device_window %p\n", swapchain_desc
->device_window
);
4727 TRACE("windowed %#x\n", swapchain_desc
->windowed
);
4728 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc
->enable_auto_depth_stencil
);
4729 if (swapchain_desc
->enable_auto_depth_stencil
)
4730 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc
->auto_depth_stencil_format
));
4731 TRACE("flags %#x\n", swapchain_desc
->flags
);
4732 TRACE("refresh_rate %u\n", swapchain_desc
->refresh_rate
);
4733 TRACE("swap_interval %u\n", swapchain_desc
->swap_interval
);
4734 TRACE("auto_restore_display_mode %#x\n", swapchain_desc
->auto_restore_display_mode
);
4736 if (swapchain_desc
->backbuffer_usage
!= WINED3DUSAGE_RENDERTARGET
)
4737 FIXME("Got unexpected backbuffer usage %#x.\n", swapchain_desc
->backbuffer_usage
);
4739 /* No special treatment of these parameters. Just store them */
4740 swapchain
->desc
.swap_effect
= swapchain_desc
->swap_effect
;
4741 swapchain
->desc
.enable_auto_depth_stencil
= swapchain_desc
->enable_auto_depth_stencil
;
4742 swapchain
->desc
.auto_depth_stencil_format
= swapchain_desc
->auto_depth_stencil_format
;
4743 swapchain
->desc
.flags
= swapchain_desc
->flags
;
4744 swapchain
->desc
.refresh_rate
= swapchain_desc
->refresh_rate
;
4745 swapchain
->desc
.swap_interval
= swapchain_desc
->swap_interval
;
4746 swapchain
->desc
.auto_restore_display_mode
= swapchain_desc
->auto_restore_display_mode
;
4748 if (swapchain_desc
->device_window
4749 && swapchain_desc
->device_window
!= swapchain
->desc
.device_window
)
4751 TRACE("Changing the device window from %p to %p.\n",
4752 swapchain
->desc
.device_window
, swapchain_desc
->device_window
);
4753 swapchain
->desc
.device_window
= swapchain_desc
->device_window
;
4754 swapchain
->device_window
= swapchain_desc
->device_window
;
4755 wined3d_swapchain_set_window(swapchain
, NULL
);
4758 backbuffer_resized
= swapchain_desc
->backbuffer_width
!= swapchain
->desc
.backbuffer_width
4759 || swapchain_desc
->backbuffer_height
!= swapchain
->desc
.backbuffer_height
;
4761 if (!swapchain_desc
->windowed
!= !swapchain
->desc
.windowed
4762 || swapchain
->reapply_mode
|| mode
4763 || (!swapchain_desc
->windowed
&& backbuffer_resized
))
4765 if (FAILED(hr
= wined3d_swapchain_set_fullscreen(swapchain
, swapchain_desc
, mode
)))
4768 else if (!swapchain_desc
->windowed
)
4770 DWORD style
= device
->style
;
4771 DWORD exStyle
= device
->exStyle
;
4772 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4773 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4774 * Reset to clear up their mess. Guild Wars also loses the device during that.
4777 device
->exStyle
= 0;
4778 wined3d_device_setup_fullscreen_window(device
, swapchain
->device_window
,
4779 swapchain_desc
->backbuffer_width
,
4780 swapchain_desc
->backbuffer_height
);
4781 device
->style
= style
;
4782 device
->exStyle
= exStyle
;
4785 if (FAILED(hr
= wined3d_swapchain_resize_buffers(swapchain
, swapchain_desc
->backbuffer_count
,
4786 swapchain_desc
->backbuffer_width
, swapchain_desc
->backbuffer_height
, swapchain_desc
->backbuffer_format
,
4787 swapchain_desc
->multisample_type
, swapchain_desc
->multisample_quality
)))
4790 if (device
->auto_depth_stencil_view
)
4792 wined3d_rendertarget_view_decref(device
->auto_depth_stencil_view
);
4793 device
->auto_depth_stencil_view
= NULL
;
4795 if (swapchain
->desc
.enable_auto_depth_stencil
)
4797 struct wined3d_resource_desc texture_desc
;
4798 struct wined3d_texture
*texture
;
4801 TRACE("Creating the depth stencil buffer.\n");
4803 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
4804 texture_desc
.format
= swapchain
->desc
.auto_depth_stencil_format
;
4805 texture_desc
.multisample_type
= swapchain
->desc
.multisample_type
;
4806 texture_desc
.multisample_quality
= swapchain
->desc
.multisample_quality
;
4807 texture_desc
.usage
= WINED3DUSAGE_DEPTHSTENCIL
;
4808 texture_desc
.pool
= WINED3D_POOL_DEFAULT
;
4809 texture_desc
.width
= swapchain
->desc
.backbuffer_width
;
4810 texture_desc
.height
= swapchain
->desc
.backbuffer_height
;
4811 texture_desc
.depth
= 1;
4812 texture_desc
.size
= 0;
4814 if (swapchain_desc
->flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
4815 flags
|= WINED3D_TEXTURE_CREATE_GET_DC
;
4817 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
4818 device
->device_parent
, &texture_desc
, flags
, &texture
)))
4820 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
4821 return WINED3DERR_INVALIDCALL
;
4824 view_desc
.format_id
= texture
->resource
.format
->id
;
4825 view_desc
.flags
= 0;
4826 view_desc
.u
.texture
.level_idx
= 0;
4827 view_desc
.u
.texture
.level_count
= 1;
4828 view_desc
.u
.texture
.layer_idx
= 0;
4829 view_desc
.u
.texture
.layer_count
= 1;
4830 hr
= wined3d_rendertarget_view_create(&view_desc
, &texture
->resource
,
4831 NULL
, &wined3d_null_parent_ops
, &device
->auto_depth_stencil_view
);
4832 wined3d_texture_decref(texture
);
4835 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
4839 wined3d_device_set_depth_stencil_view(device
, device
->auto_depth_stencil_view
);
4842 if (device
->back_buffer_view
)
4844 wined3d_rendertarget_view_decref(device
->back_buffer_view
);
4845 device
->back_buffer_view
= NULL
;
4847 if (swapchain
->desc
.backbuffer_count
)
4849 struct wined3d_resource
*back_buffer
= &swapchain
->back_buffers
[0]->resource
;
4851 view_desc
.format_id
= back_buffer
->format
->id
;
4852 view_desc
.flags
= 0;
4853 view_desc
.u
.texture
.level_idx
= 0;
4854 view_desc
.u
.texture
.level_count
= 1;
4855 view_desc
.u
.texture
.layer_idx
= 0;
4856 view_desc
.u
.texture
.layer_count
= 1;
4857 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
, back_buffer
,
4858 NULL
, &wined3d_null_parent_ops
, &device
->back_buffer_view
)))
4860 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
4865 wine_rb_clear(&device
->samplers
, device_free_sampler
, NULL
);
4869 TRACE("Resetting stateblock.\n");
4870 if (device
->recording
)
4872 wined3d_stateblock_decref(device
->recording
);
4873 device
->recording
= NULL
;
4875 wined3d_cs_emit_reset_state(device
->cs
);
4876 state_cleanup(&device
->state
);
4878 if (device
->d3d_initialized
)
4879 wined3d_device_delete_opengl_contexts(device
);
4881 memset(&device
->state
, 0, sizeof(device
->state
));
4882 state_init(&device
->state
, &device
->fb
, &device
->adapter
->gl_info
,
4883 &device
->adapter
->d3d_info
, WINED3D_STATE_INIT_DEFAULT
);
4884 device
->update_state
= &device
->state
;
4886 device_init_swapchain_state(device
, swapchain
);
4887 if (wined3d_settings
.logo
)
4888 device_load_logo(device
, wined3d_settings
.logo
);
4890 else if (device
->back_buffer_view
)
4892 struct wined3d_rendertarget_view
*view
= device
->back_buffer_view
;
4893 struct wined3d_state
*state
= &device
->state
;
4895 wined3d_device_set_rendertarget_view(device
, 0, view
, FALSE
);
4897 /* Note the min_z / max_z is not reset. */
4898 state
->viewport
.x
= 0;
4899 state
->viewport
.y
= 0;
4900 state
->viewport
.width
= view
->width
;
4901 state
->viewport
.height
= view
->height
;
4902 wined3d_cs_emit_set_viewport(device
->cs
, &state
->viewport
);
4904 SetRect(&state
->scissor_rect
, 0, 0, view
->width
, view
->height
);
4905 wined3d_cs_emit_set_scissor_rect(device
->cs
, &state
->scissor_rect
);
4908 if (device
->d3d_initialized
)
4911 hr
= wined3d_device_create_primary_opengl_context(device
);
4912 swapchain_update_swap_interval(swapchain
);
4915 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4921 HRESULT CDECL
wined3d_device_set_dialog_box_mode(struct wined3d_device
*device
, BOOL enable_dialogs
)
4923 TRACE("device %p, enable_dialogs %#x.\n", device
, enable_dialogs
);
4925 if (!enable_dialogs
) FIXME("Dialogs cannot be disabled yet.\n");
4931 void CDECL
wined3d_device_get_creation_parameters(const struct wined3d_device
*device
,
4932 struct wined3d_device_creation_parameters
*parameters
)
4934 TRACE("device %p, parameters %p.\n", device
, parameters
);
4936 *parameters
= device
->create_parms
;
4939 struct wined3d
* CDECL
wined3d_device_get_wined3d(const struct wined3d_device
*device
)
4941 TRACE("device %p.\n", device
);
4943 return device
->wined3d
;
4946 void CDECL
wined3d_device_set_gamma_ramp(const struct wined3d_device
*device
,
4947 UINT swapchain_idx
, DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
4949 struct wined3d_swapchain
*swapchain
;
4951 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4952 device
, swapchain_idx
, flags
, ramp
);
4954 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4955 wined3d_swapchain_set_gamma_ramp(swapchain
, flags
, ramp
);
4958 void CDECL
wined3d_device_get_gamma_ramp(const struct wined3d_device
*device
,
4959 UINT swapchain_idx
, struct wined3d_gamma_ramp
*ramp
)
4961 struct wined3d_swapchain
*swapchain
;
4963 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4964 device
, swapchain_idx
, ramp
);
4966 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4967 wined3d_swapchain_get_gamma_ramp(swapchain
, ramp
);
4970 void device_resource_add(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
4972 TRACE("device %p, resource %p.\n", device
, resource
);
4974 wined3d_not_from_cs(device
->cs
);
4976 list_add_head(&device
->resources
, &resource
->resource_list_entry
);
4979 static void device_resource_remove(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
4981 TRACE("device %p, resource %p.\n", device
, resource
);
4983 wined3d_not_from_cs(device
->cs
);
4985 list_remove(&resource
->resource_list_entry
);
4988 void device_resource_released(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
4990 enum wined3d_resource_type type
= resource
->type
;
4991 struct wined3d_rendertarget_view
*rtv
;
4994 TRACE("device %p, resource %p, type %s.\n", device
, resource
, debug_d3dresourcetype(type
));
4996 if (device
->d3d_initialized
)
4998 for (i
= 0; i
< device
->adapter
->gl_info
.limits
.buffers
; ++i
)
5000 if ((rtv
= device
->fb
.render_targets
[i
]) && rtv
->resource
== resource
)
5001 ERR("Resource %p is still in use as render target %u.\n", resource
, i
);
5004 if ((rtv
= device
->fb
.depth_stencil
) && rtv
->resource
== resource
)
5005 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource
);
5010 case WINED3D_RTYPE_TEXTURE_2D
:
5011 case WINED3D_RTYPE_TEXTURE_3D
:
5012 for (i
= 0; i
< MAX_COMBINED_SAMPLERS
; ++i
)
5014 struct wined3d_texture
*texture
= texture_from_resource(resource
);
5016 if (device
->state
.textures
[i
] == texture
)
5018 ERR("Texture %p is still in use, stage %u.\n", texture
, i
);
5019 device
->state
.textures
[i
] = NULL
;
5022 if (device
->recording
&& device
->update_state
->textures
[i
] == texture
)
5024 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
5025 texture
, device
->recording
, i
);
5026 device
->update_state
->textures
[i
] = NULL
;
5031 case WINED3D_RTYPE_BUFFER
:
5033 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
5035 for (i
= 0; i
< MAX_STREAMS
; ++i
)
5037 if (device
->state
.streams
[i
].buffer
== buffer
)
5039 ERR("Buffer %p is still in use, stream %u.\n", buffer
, i
);
5040 device
->state
.streams
[i
].buffer
= NULL
;
5043 if (device
->recording
&& device
->update_state
->streams
[i
].buffer
== buffer
)
5045 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5046 buffer
, device
->recording
, i
);
5047 device
->update_state
->streams
[i
].buffer
= NULL
;
5051 if (device
->state
.index_buffer
== buffer
)
5053 ERR("Buffer %p is still in use as index buffer.\n", buffer
);
5054 device
->state
.index_buffer
= NULL
;
5057 if (device
->recording
&& device
->update_state
->index_buffer
== buffer
)
5059 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5060 buffer
, device
->recording
);
5061 device
->update_state
->index_buffer
= NULL
;
5070 /* Remove the resource from the resourceStore */
5071 device_resource_remove(device
, resource
);
5073 TRACE("Resource released.\n");
5076 static int wined3d_sampler_compare(const void *key
, const struct wine_rb_entry
*entry
)
5078 const struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
5080 return memcmp(&sampler
->desc
, key
, sizeof(sampler
->desc
));
5083 HRESULT
device_init(struct wined3d_device
*device
, struct wined3d
*wined3d
,
5084 UINT adapter_idx
, enum wined3d_device_type device_type
, HWND focus_window
, DWORD flags
,
5085 BYTE surface_alignment
, struct wined3d_device_parent
*device_parent
)
5087 struct wined3d_adapter
*adapter
= &wined3d
->adapters
[adapter_idx
];
5088 const struct fragment_pipeline
*fragment_pipeline
;
5089 const struct wined3d_vertex_pipe_ops
*vertex_pipeline
;
5094 device
->wined3d
= wined3d
;
5095 wined3d_incref(device
->wined3d
);
5096 device
->adapter
= wined3d
->adapter_count
? adapter
: NULL
;
5097 device
->device_parent
= device_parent
;
5098 list_init(&device
->resources
);
5099 list_init(&device
->shaders
);
5100 device
->surface_alignment
= surface_alignment
;
5102 /* Save the creation parameters. */
5103 device
->create_parms
.adapter_idx
= adapter_idx
;
5104 device
->create_parms
.device_type
= device_type
;
5105 device
->create_parms
.focus_window
= focus_window
;
5106 device
->create_parms
.flags
= flags
;
5108 device
->shader_backend
= adapter
->shader_backend
;
5110 vertex_pipeline
= adapter
->vertex_pipe
;
5112 fragment_pipeline
= adapter
->fragment_pipe
;
5114 wine_rb_init(&device
->samplers
, wined3d_sampler_compare
);
5116 if (vertex_pipeline
->vp_states
&& fragment_pipeline
->states
5117 && FAILED(hr
= compile_state_table(device
->StateTable
, device
->multistate_funcs
,
5118 &adapter
->gl_info
, &adapter
->d3d_info
, vertex_pipeline
,
5119 fragment_pipeline
, misc_state_template
)))
5121 ERR("Failed to compile state table, hr %#x.\n", hr
);
5122 wine_rb_destroy(&device
->samplers
, NULL
, NULL
);
5123 wined3d_decref(device
->wined3d
);
5127 state_init(&device
->state
, &device
->fb
, &adapter
->gl_info
,
5128 &adapter
->d3d_info
, WINED3D_STATE_INIT_DEFAULT
);
5129 device
->update_state
= &device
->state
;
5131 if (!(device
->cs
= wined3d_cs_create(device
)))
5133 WARN("Failed to create command stream.\n");
5134 state_cleanup(&device
->state
);
5142 for (i
= 0; i
< ARRAY_SIZE(device
->multistate_funcs
); ++i
)
5144 HeapFree(GetProcessHeap(), 0, device
->multistate_funcs
[i
]);
5146 wine_rb_destroy(&device
->samplers
, NULL
, NULL
);
5147 wined3d_decref(device
->wined3d
);
5151 void device_invalidate_state(const struct wined3d_device
*device
, DWORD state
)
5153 DWORD rep
= device
->StateTable
[state
].representative
;
5154 struct wined3d_context
*context
;
5159 wined3d_from_cs(device
->cs
);
5161 if (STATE_IS_COMPUTE(state
))
5163 for (i
= 0; i
< device
->context_count
; ++i
)
5164 context_invalidate_compute_state(device
->contexts
[i
], state
);
5168 for (i
= 0; i
< device
->context_count
; ++i
)
5170 context
= device
->contexts
[i
];
5171 if(isStateDirty(context
, rep
)) continue;
5173 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
5174 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
5175 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
5176 context
->isStateDirty
[idx
] |= (1u << shift
);
5180 LRESULT
device_process_message(struct wined3d_device
*device
, HWND window
, BOOL unicode
,
5181 UINT message
, WPARAM wparam
, LPARAM lparam
, WNDPROC proc
)
5183 if (device
->filter_messages
&& message
!= WM_DISPLAYCHANGE
)
5185 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5186 window
, message
, wparam
, lparam
);
5188 return DefWindowProcW(window
, message
, wparam
, lparam
);
5190 return DefWindowProcA(window
, message
, wparam
, lparam
);
5193 if (message
== WM_DESTROY
)
5195 TRACE("unregister window %p.\n", window
);
5196 wined3d_unregister_window(window
);
5198 if (InterlockedCompareExchangePointer((void **)&device
->focus_window
, NULL
, window
) != window
)
5199 ERR("Window %p is not the focus window for device %p.\n", window
, device
);
5201 else if (message
== WM_DISPLAYCHANGE
)
5203 device
->device_parent
->ops
->mode_changed(device
->device_parent
);
5205 else if (message
== WM_ACTIVATEAPP
)
5209 for (i
= 0; i
< device
->swapchain_count
; i
++)
5210 wined3d_swapchain_activate(device
->swapchains
[i
], wparam
);
5212 device
->device_parent
->ops
->activate(device
->device_parent
, wparam
);
5214 else if (message
== WM_SYSCOMMAND
)
5216 if (wparam
== SC_RESTORE
&& device
->wined3d
->flags
& WINED3D_HANDLE_RESTORE
)
5219 DefWindowProcW(window
, message
, wparam
, lparam
);
5221 DefWindowProcA(window
, message
, wparam
, lparam
);
5226 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
5228 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);