dxgi: Added support for DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE.
[wine.git] / dlls / wined3d / device.c
blob002eed30865b91961b0afb544855f3e4d0f5376c
1 /*
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
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
203 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
204 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
206 /* partial draw rect */
207 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
208 return FALSE;
210 /* partial clear rect */
211 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
212 || clear_rect->right < width || clear_rect->bottom < height))
213 return FALSE;
215 return TRUE;
218 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
219 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
220 float depth, DWORD stencil)
222 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
223 struct wined3d_surface *target = rtv ? wined3d_rendertarget_view_get_surface(rtv) : NULL;
224 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
225 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
226 const struct wined3d_state *state = &device->state;
227 const struct wined3d_gl_info *gl_info;
228 UINT drawable_width, drawable_height;
229 struct wined3d_color corrected_color;
230 struct wined3d_context *context;
231 GLbitfield clear_mask = 0;
232 BOOL render_offscreen;
233 unsigned int i;
235 context = context_acquire(device, target);
236 if (!context->valid)
238 context_release(context);
239 WARN("Invalid context, skipping clear.\n");
240 return;
242 gl_info = context->gl_info;
244 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
245 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
246 * for the cleared parts, and the untouched parts.
248 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
249 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
250 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
251 * checking all this if the dest surface is in the drawable anyway. */
252 for (i = 0; i < rt_count; ++i)
254 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
256 if (rtv && rtv->format->id != WINED3DFMT_NULL)
258 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
260 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
261 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
262 else
263 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
267 if (target)
269 render_offscreen = context->render_offscreen;
270 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
272 else
274 render_offscreen = TRUE;
275 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
276 depth_stencil->texture_level);
277 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
278 depth_stencil->texture_level);
281 if (depth_stencil && render_offscreen)
282 wined3d_texture_prepare_location(depth_stencil->container,
283 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
285 if (flags & WINED3DCLEAR_ZBUFFER)
287 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
289 surface_load_location(depth_stencil, context, location);
292 if (!context_apply_clear_state(context, state, rt_count, fb))
294 context_release(context);
295 WARN("Failed to apply clear state, skipping clear.\n");
296 return;
299 /* Only set the values up once, as they are not changing. */
300 if (flags & WINED3DCLEAR_STENCIL)
302 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
304 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
305 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
307 gl_info->gl_ops.gl.p_glStencilMask(~0U);
308 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
309 gl_info->gl_ops.gl.p_glClearStencil(stencil);
310 checkGLcall("glClearStencil");
311 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
314 if (flags & WINED3DCLEAR_ZBUFFER)
316 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
318 wined3d_texture_validate_location(depth_stencil->container, dsv->sub_resource_idx, location);
319 wined3d_texture_invalidate_location(depth_stencil->container, dsv->sub_resource_idx, ~location);
321 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
322 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
323 gl_info->gl_ops.gl.p_glClearDepth(depth);
324 checkGLcall("glClearDepth");
325 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
328 if (flags & WINED3DCLEAR_TARGET)
330 for (i = 0; i < rt_count; ++i)
332 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
333 struct wined3d_texture *texture;
335 if (!rtv)
336 continue;
338 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
340 FIXME("Not supported on buffer resources.\n");
341 continue;
344 texture = texture_from_resource(rtv->resource);
345 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
346 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
349 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
351 if (rt_count > 1)
352 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
353 "support, this might cause graphical issues.\n");
355 corrected_color.r = color->r < wined3d_srgb_const1[0]
356 ? color->r * wined3d_srgb_const0[3]
357 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
358 - wined3d_srgb_const0[2];
359 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
360 corrected_color.g = color->g < wined3d_srgb_const1[0]
361 ? color->g * wined3d_srgb_const0[3]
362 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
363 - wined3d_srgb_const0[2];
364 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
365 corrected_color.b = color->b < wined3d_srgb_const1[0]
366 ? color->b * wined3d_srgb_const0[3]
367 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
368 - wined3d_srgb_const0[2];
369 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
370 color = &corrected_color;
373 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
374 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
375 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
376 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
377 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
378 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
379 checkGLcall("glClearColor");
380 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
383 if (!rect_count)
385 if (render_offscreen)
387 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
388 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
390 else
392 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
393 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
395 checkGLcall("glScissor");
396 gl_info->gl_ops.gl.p_glClear(clear_mask);
397 checkGLcall("glClear");
399 else
401 RECT current_rect;
403 /* Now process each rect in turn. */
404 for (i = 0; i < rect_count; ++i)
406 /* Note that GL uses lower left, width/height. */
407 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
409 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
410 wine_dbgstr_rect(&clear_rect[i]),
411 wine_dbgstr_rect(&current_rect));
413 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
414 * The rectangle is not cleared, no error is returned, but further rectangles are
415 * still cleared if they are valid. */
416 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
418 TRACE("Rectangle with negative dimensions, ignoring.\n");
419 continue;
422 if (render_offscreen)
424 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
425 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
427 else
429 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
430 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
432 checkGLcall("glScissor");
434 gl_info->gl_ops.gl.p_glClear(clear_mask);
435 checkGLcall("glClear");
439 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
440 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
441 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
443 context_release(context);
446 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
448 ULONG refcount = InterlockedIncrement(&device->ref);
450 TRACE("%p increasing refcount to %u.\n", device, refcount);
452 return refcount;
455 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
457 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
459 ERR("Leftover sampler %p.\n", sampler);
462 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
464 ULONG refcount = InterlockedDecrement(&device->ref);
466 TRACE("%p decreasing refcount to %u.\n", device, refcount);
468 if (!refcount)
470 UINT i;
472 wined3d_cs_destroy(device->cs);
474 if (device->recording && wined3d_stateblock_decref(device->recording))
475 ERR("Something's still holding the recording stateblock.\n");
476 device->recording = NULL;
478 state_cleanup(&device->state);
480 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
482 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
483 device->multistate_funcs[i] = NULL;
486 if (!list_empty(&device->resources))
488 struct wined3d_resource *resource;
490 ERR("Device released with resources still bound.\n");
492 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
494 ERR("Leftover resource %p with type %s (%#x).\n",
495 resource, debug_d3dresourcetype(resource->type), resource->type);
499 if (device->contexts)
500 ERR("Context array not freed!\n");
501 if (device->hardwareCursor)
502 DestroyCursor(device->hardwareCursor);
503 device->hardwareCursor = 0;
505 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
507 wined3d_decref(device->wined3d);
508 device->wined3d = NULL;
509 HeapFree(GetProcessHeap(), 0, device);
510 TRACE("Freed device %p.\n", device);
513 return refcount;
516 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
518 TRACE("device %p.\n", device);
520 return device->swapchain_count;
523 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
525 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
527 if (swapchain_idx >= device->swapchain_count)
529 WARN("swapchain_idx %u >= swapchain_count %u.\n",
530 swapchain_idx, device->swapchain_count);
531 return NULL;
534 return device->swapchains[swapchain_idx];
537 static void device_load_logo(struct wined3d_device *device, const char *filename)
539 struct wined3d_color_key color_key;
540 struct wined3d_resource_desc desc;
541 HBITMAP hbm;
542 BITMAP bm;
543 HRESULT hr;
544 HDC dcb = NULL, dcs = NULL;
546 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
547 if(hbm)
549 GetObjectA(hbm, sizeof(BITMAP), &bm);
550 dcb = CreateCompatibleDC(NULL);
551 if(!dcb) goto out;
552 SelectObject(dcb, hbm);
554 else
556 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
557 * couldn't be loaded
559 memset(&bm, 0, sizeof(bm));
560 bm.bmWidth = 32;
561 bm.bmHeight = 32;
564 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
565 desc.format = WINED3DFMT_B5G6R5_UNORM;
566 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
567 desc.multisample_quality = 0;
568 desc.usage = WINED3DUSAGE_DYNAMIC;
569 desc.pool = WINED3D_POOL_DEFAULT;
570 desc.width = bm.bmWidth;
571 desc.height = bm.bmHeight;
572 desc.depth = 1;
573 desc.size = 0;
574 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
575 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
577 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
578 goto out;
581 if (dcb)
583 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
584 goto out;
585 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
586 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
588 color_key.color_space_low_value = 0;
589 color_key.color_space_high_value = 0;
590 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
592 else
594 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
595 const RECT rect = {0, 0, desc.width, desc.height};
596 struct wined3d_surface *surface;
598 /* Fill the surface with a white color to show that wined3d is there */
599 surface = device->logo_texture->sub_resources[0].u.surface;
600 surface_color_fill(surface, &rect, &c);
603 out:
604 if (dcb) DeleteDC(dcb);
605 if (hbm) DeleteObject(hbm);
608 /* Context activation is done by the caller. */
609 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
611 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
612 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
613 unsigned int i;
614 DWORD color;
616 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
617 color = 0x000000ff;
618 else
619 color = 0x00000000;
621 /* Under DirectX you can sample even if no texture is bound, whereas
622 * OpenGL will only allow that when a valid texture is bound.
623 * We emulate this by creating dummy textures and binding them
624 * to each texture stage when the currently set D3D texture is NULL. */
625 context_active_texture(context, gl_info, 0);
627 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d);
628 checkGLcall("glGenTextures");
629 TRACE("Dummy 2D texture given name %u.\n", device->dummy_textures.tex_2d);
631 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
632 checkGLcall("glBindTexture");
634 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
635 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
636 checkGLcall("glTexImage2D");
638 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
640 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_rect);
641 checkGLcall("glGenTextures");
642 TRACE("Dummy rectangle texture given name %u.\n", device->dummy_textures.tex_rect);
644 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
645 checkGLcall("glBindTexture");
647 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
648 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
649 checkGLcall("glTexImage2D");
652 if (gl_info->supported[EXT_TEXTURE3D])
654 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_3d);
655 checkGLcall("glGenTextures");
656 TRACE("Dummy 3D texture given name %u.\n", device->dummy_textures.tex_3d);
658 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
659 checkGLcall("glBindTexture");
661 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
662 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
663 checkGLcall("glTexImage3D");
666 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
668 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube);
669 checkGLcall("glGenTextures");
670 TRACE("Dummy cube texture given name %u.\n", device->dummy_textures.tex_cube);
672 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
673 checkGLcall("glBindTexture");
675 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
677 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
678 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
679 checkGLcall("glTexImage2D");
683 if (gl_info->supported[EXT_TEXTURE_ARRAY])
685 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d_array);
686 checkGLcall("glGenTextures");
687 TRACE("Dummy 2D array texture given name %u.\n", device->dummy_textures.tex_2d_array);
689 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
690 checkGLcall("glBindTexture");
692 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
693 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
694 checkGLcall("glTexImage3D");
697 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
699 GLuint buffer;
701 GL_EXTCALL(glGenBuffers(1, &buffer));
702 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
703 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
704 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
705 checkGLcall("Create buffer object");
707 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_buffer);
708 checkGLcall("glGenTextures");
709 TRACE("Dummy buffer texture given name %u.\n", device->dummy_textures.tex_buffer);
711 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, device->dummy_textures.tex_buffer);
712 checkGLcall("glBindTexture");
713 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
714 checkGLcall("glTexBuffer");
716 GL_EXTCALL(glDeleteBuffers(1, &buffer));
717 checkGLcall("glDeleteBuffers");
720 context_bind_dummy_textures(device, context);
723 /* Context activation is done by the caller. */
724 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
726 const struct wined3d_gl_info *gl_info = context->gl_info;
728 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
729 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_buffer);
731 if (gl_info->supported[EXT_TEXTURE_ARRAY])
732 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d_array);
734 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
735 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube);
737 if (gl_info->supported[EXT_TEXTURE3D])
738 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_3d);
740 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
741 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_rect);
743 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d);
745 checkGLcall("Delete dummy textures");
747 memset(&device->dummy_textures, 0, sizeof(device->dummy_textures));
750 /* Context activation is done by the caller. */
751 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
753 const struct wined3d_gl_info *gl_info = context->gl_info;
755 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
757 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
758 * instructions allow access to resources without using samplers.
759 * In GLSL, resources are always accessed through sampler or image variables. The default
760 * sampler object is used to emulate the direct resource access when there is no sampler state
761 * to use.
763 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
764 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
765 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
766 checkGLcall("Create default sampler");
768 /* In D3D10+, a NULL sampler maps to the default sampler state. */
769 GL_EXTCALL(glGenSamplers(1, &device->null_sampler));
770 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
771 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
772 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
773 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
774 checkGLcall("Create null sampler");
776 else
778 device->default_sampler = 0;
779 device->null_sampler = 0;
783 /* Context activation is done by the caller. */
784 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
786 const struct wined3d_gl_info *gl_info = context->gl_info;
788 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
790 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
791 GL_EXTCALL(glDeleteSamplers(1, &device->null_sampler));
792 checkGLcall("glDeleteSamplers");
795 device->default_sampler = 0;
796 device->null_sampler = 0;
799 static LONG fullscreen_style(LONG style)
801 /* Make sure the window is managed, otherwise we won't get keyboard input. */
802 style |= WS_POPUP | WS_SYSMENU;
803 style &= ~(WS_CAPTION | WS_THICKFRAME);
805 return style;
808 static LONG fullscreen_exstyle(LONG exstyle)
810 /* Filter out window decorations. */
811 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
813 return exstyle;
816 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
818 BOOL filter_messages;
819 LONG style, exstyle;
821 TRACE("Setting up window %p for fullscreen mode.\n", window);
823 if (device->style || device->exStyle)
825 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
826 window, device->style, device->exStyle);
829 device->style = GetWindowLongW(window, GWL_STYLE);
830 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
832 style = fullscreen_style(device->style);
833 exstyle = fullscreen_exstyle(device->exStyle);
835 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
836 device->style, device->exStyle, style, exstyle);
838 filter_messages = device->filter_messages;
839 device->filter_messages = TRUE;
841 SetWindowLongW(window, GWL_STYLE, style);
842 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
843 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
845 device->filter_messages = filter_messages;
848 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
849 const RECT *window_rect)
851 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
852 BOOL filter_messages;
853 LONG style, exstyle;
854 RECT rect = {0};
856 if (!device->style && !device->exStyle)
857 return;
859 style = GetWindowLongW(window, GWL_STYLE);
860 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
862 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
863 * application, and we want to ignore them in the test below, since it's
864 * not the application's fault that they changed. Additionally, we want to
865 * preserve the current status of these flags (i.e. don't restore them) to
866 * more closely emulate the behavior of Direct3D, which leaves these flags
867 * alone when returning to windowed mode. */
868 device->style ^= (device->style ^ style) & WS_VISIBLE;
869 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
871 TRACE("Restoring window style of window %p to %08x, %08x.\n",
872 window, device->style, device->exStyle);
874 filter_messages = device->filter_messages;
875 device->filter_messages = TRUE;
877 /* Only restore the style if the application didn't modify it during the
878 * fullscreen phase. Some applications change it before calling Reset()
879 * when switching between windowed and fullscreen modes (HL2), some
880 * depend on the original style (Eve Online). */
881 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
883 SetWindowLongW(window, GWL_STYLE, device->style);
884 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
887 if (window_rect)
888 rect = *window_rect;
889 else
890 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
891 SetWindowPos(window, 0, rect.left, rect.top,
892 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
894 device->filter_messages = filter_messages;
896 /* Delete the old values. */
897 device->style = 0;
898 device->exStyle = 0;
901 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
903 TRACE("device %p, window %p.\n", device, window);
905 if (!wined3d_register_window(window, device))
907 ERR("Failed to register window %p.\n", window);
908 return E_FAIL;
911 InterlockedExchangePointer((void **)&device->focus_window, window);
912 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
914 return WINED3D_OK;
917 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
919 TRACE("device %p.\n", device);
921 if (device->focus_window) wined3d_unregister_window(device->focus_window);
922 InterlockedExchangePointer((void **)&device->focus_window, NULL);
925 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
927 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
928 unsigned int i;
930 if (device->fb.render_targets)
932 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
934 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
936 if (device->back_buffer_view)
937 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
940 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
941 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
944 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
945 struct wined3d_swapchain_desc *swapchain_desc)
947 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
948 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
949 struct wined3d_swapchain *swapchain = NULL;
950 struct wined3d_context *context;
951 DWORD clear_flags = 0;
952 HRESULT hr;
954 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
956 if (device->d3d_initialized)
957 return WINED3DERR_INVALIDCALL;
958 if (device->wined3d->flags & WINED3D_NO3D)
959 return WINED3DERR_INVALIDCALL;
961 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
962 return E_OUTOFMEMORY;
964 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
965 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
967 TRACE("Shader private data couldn't be allocated\n");
968 goto err_out;
970 if (FAILED(hr = device->blitter->alloc_private(device)))
972 TRACE("Blitter private data couldn't be allocated\n");
973 goto err_out;
976 /* Setup the implicit swapchain. This also initializes a context. */
977 TRACE("Creating implicit swapchain\n");
978 hr = device->device_parent->ops->create_swapchain(device->device_parent,
979 swapchain_desc, &swapchain);
980 if (FAILED(hr))
982 WARN("Failed to create implicit swapchain\n");
983 goto err_out;
986 if (swapchain_desc->backbuffer_count)
988 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
989 struct wined3d_view_desc view_desc;
991 view_desc.format_id = back_buffer->format->id;
992 view_desc.flags = 0;
993 view_desc.u.texture.level_idx = 0;
994 view_desc.u.texture.level_count = 1;
995 view_desc.u.texture.layer_idx = 0;
996 view_desc.u.texture.layer_count = 1;
997 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
998 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1000 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1001 goto err_out;
1005 device->swapchain_count = 1;
1006 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1008 ERR("Out of memory!\n");
1009 goto err_out;
1011 device->swapchains[0] = swapchain;
1012 device_init_swapchain_state(device, swapchain);
1014 context = context_acquire(device, NULL);
1016 create_dummy_textures(device, context);
1017 create_default_samplers(device, context);
1019 device->contexts[0]->last_was_rhw = 0;
1021 TRACE("All defaults now set up, leaving 3D init.\n");
1023 context_release(context);
1025 /* Clear the screen */
1026 if (swapchain->back_buffers && swapchain->back_buffers[0])
1027 clear_flags |= WINED3DCLEAR_TARGET;
1028 if (swapchain_desc->enable_auto_depth_stencil)
1029 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1030 if (clear_flags)
1031 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1033 device->d3d_initialized = TRUE;
1035 if (wined3d_settings.logo)
1036 device_load_logo(device, wined3d_settings.logo);
1037 return WINED3D_OK;
1039 err_out:
1040 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1041 HeapFree(GetProcessHeap(), 0, device->swapchains);
1042 device->swapchain_count = 0;
1043 if (device->back_buffer_view)
1044 wined3d_rendertarget_view_decref(device->back_buffer_view);
1045 if (swapchain)
1046 wined3d_swapchain_decref(swapchain);
1047 if (device->blit_priv)
1048 device->blitter->free_private(device);
1049 if (device->shader_priv)
1050 device->shader_backend->shader_free_private(device);
1052 return hr;
1055 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1056 struct wined3d_swapchain_desc *swapchain_desc)
1058 struct wined3d_swapchain *swapchain = NULL;
1059 HRESULT hr;
1061 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1063 /* Setup the implicit swapchain */
1064 TRACE("Creating implicit swapchain\n");
1065 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1066 swapchain_desc, &swapchain);
1067 if (FAILED(hr))
1069 WARN("Failed to create implicit swapchain\n");
1070 goto err_out;
1073 device->swapchain_count = 1;
1074 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1076 ERR("Out of memory!\n");
1077 goto err_out;
1079 device->swapchains[0] = swapchain;
1080 return WINED3D_OK;
1082 err_out:
1083 wined3d_swapchain_decref(swapchain);
1084 return hr;
1087 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1089 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1091 wined3d_sampler_decref(sampler);
1094 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1096 struct wined3d_resource *resource, *cursor;
1097 const struct wined3d_gl_info *gl_info;
1098 struct wined3d_context *context;
1099 UINT i;
1101 TRACE("device %p.\n", device);
1103 if (!device->d3d_initialized)
1104 return WINED3DERR_INVALIDCALL;
1106 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1107 * it was created. Thus make sure a context is active for the glDelete* calls
1109 context = context_acquire(device, NULL);
1110 gl_info = context->gl_info;
1112 if (device->logo_texture)
1113 wined3d_texture_decref(device->logo_texture);
1114 if (device->cursor_texture)
1115 wined3d_texture_decref(device->cursor_texture);
1117 state_unbind_resources(&device->state);
1119 /* Unload resources */
1120 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1122 TRACE("Unloading resource %p.\n", resource);
1123 wined3d_cs_emit_unload_resource(device->cs, resource);
1126 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1128 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1129 * private data, it might contain opengl pointers
1131 if (device->depth_blt_texture)
1133 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1134 device->depth_blt_texture = 0;
1137 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1138 device->blitter->free_private(device);
1139 device->shader_backend->shader_free_private(device);
1140 destroy_dummy_textures(device, context);
1141 destroy_default_samplers(device, context);
1143 context_release(context);
1145 if (device->fb.depth_stencil)
1147 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1149 TRACE("Releasing depth/stencil view %p.\n", view);
1151 device->fb.depth_stencil = NULL;
1152 wined3d_rendertarget_view_decref(view);
1155 if (device->auto_depth_stencil_view)
1157 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1159 device->auto_depth_stencil_view = NULL;
1160 if (wined3d_rendertarget_view_decref(view))
1161 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1164 for (i = 0; i < gl_info->limits.buffers; ++i)
1166 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1168 if (device->back_buffer_view)
1170 wined3d_rendertarget_view_decref(device->back_buffer_view);
1171 device->back_buffer_view = NULL;
1174 for (i = 0; i < device->swapchain_count; ++i)
1176 TRACE("Releasing the implicit swapchain %u.\n", i);
1177 if (wined3d_swapchain_decref(device->swapchains[i]))
1178 FIXME("Something's still holding the implicit swapchain.\n");
1181 HeapFree(GetProcessHeap(), 0, device->swapchains);
1182 device->swapchains = NULL;
1183 device->swapchain_count = 0;
1185 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1186 device->fb.render_targets = NULL;
1188 device->d3d_initialized = FALSE;
1190 return WINED3D_OK;
1193 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1195 unsigned int i;
1197 for (i = 0; i < device->swapchain_count; ++i)
1199 TRACE("Releasing the implicit swapchain %u.\n", i);
1200 if (wined3d_swapchain_decref(device->swapchains[i]))
1201 FIXME("Something's still holding the implicit swapchain.\n");
1204 HeapFree(GetProcessHeap(), 0, device->swapchains);
1205 device->swapchains = NULL;
1206 device->swapchain_count = 0;
1207 return WINED3D_OK;
1210 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1211 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1212 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1214 * There is no way to deactivate thread safety once it is enabled.
1216 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1218 TRACE("device %p.\n", device);
1220 /* For now just store the flag (needed in case of ddraw). */
1221 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1224 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1226 TRACE("device %p.\n", device);
1228 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1229 wine_dbgstr_longlong(device->adapter->vram_bytes),
1230 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1231 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1233 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1236 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1237 struct wined3d_buffer *buffer, UINT offset)
1239 struct wined3d_stream_output *stream;
1240 struct wined3d_buffer *prev_buffer;
1242 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1244 if (idx >= MAX_STREAM_OUT)
1246 WARN("Invalid stream output %u.\n", idx);
1247 return;
1250 stream = &device->update_state->stream_output[idx];
1251 prev_buffer = stream->buffer;
1253 if (buffer)
1254 wined3d_buffer_incref(buffer);
1255 stream->buffer = buffer;
1256 stream->offset = offset;
1257 if (!device->recording)
1258 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1259 if (prev_buffer)
1260 wined3d_buffer_decref(prev_buffer);
1263 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1264 UINT idx, UINT *offset)
1266 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1268 if (idx >= MAX_STREAM_OUT)
1270 WARN("Invalid stream output %u.\n", idx);
1271 return NULL;
1274 if (offset)
1275 *offset = device->state.stream_output[idx].offset;
1276 return device->state.stream_output[idx].buffer;
1279 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1280 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1282 struct wined3d_stream_state *stream;
1283 struct wined3d_buffer *prev_buffer;
1285 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1286 device, stream_idx, buffer, offset, stride);
1288 if (stream_idx >= MAX_STREAMS)
1290 WARN("Stream index %u out of range.\n", stream_idx);
1291 return WINED3DERR_INVALIDCALL;
1293 else if (offset & 0x3)
1295 WARN("Offset %u is not 4 byte aligned.\n", offset);
1296 return WINED3DERR_INVALIDCALL;
1299 stream = &device->update_state->streams[stream_idx];
1300 prev_buffer = stream->buffer;
1302 if (device->recording)
1303 device->recording->changed.streamSource |= 1u << stream_idx;
1305 if (prev_buffer == buffer
1306 && stream->stride == stride
1307 && stream->offset == offset)
1309 TRACE("Application is setting the old values over, nothing to do.\n");
1310 return WINED3D_OK;
1313 stream->buffer = buffer;
1314 if (buffer)
1316 stream->stride = stride;
1317 stream->offset = offset;
1318 wined3d_buffer_incref(buffer);
1321 if (!device->recording)
1322 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1323 if (prev_buffer)
1324 wined3d_buffer_decref(prev_buffer);
1326 return WINED3D_OK;
1329 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1330 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1332 const struct wined3d_stream_state *stream;
1334 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1335 device, stream_idx, buffer, offset, stride);
1337 if (stream_idx >= MAX_STREAMS)
1339 WARN("Stream index %u out of range.\n", stream_idx);
1340 return WINED3DERR_INVALIDCALL;
1343 stream = &device->state.streams[stream_idx];
1344 *buffer = stream->buffer;
1345 if (offset)
1346 *offset = stream->offset;
1347 *stride = stream->stride;
1349 return WINED3D_OK;
1352 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1354 struct wined3d_stream_state *stream;
1355 UINT old_flags, old_freq;
1357 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1359 /* Verify input. At least in d3d9 this is invalid. */
1360 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1362 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1363 return WINED3DERR_INVALIDCALL;
1365 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1367 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1368 return WINED3DERR_INVALIDCALL;
1370 if (!divider)
1372 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1373 return WINED3DERR_INVALIDCALL;
1376 stream = &device->update_state->streams[stream_idx];
1377 old_flags = stream->flags;
1378 old_freq = stream->frequency;
1380 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1381 stream->frequency = divider & 0x7fffff;
1383 if (device->recording)
1384 device->recording->changed.streamFreq |= 1u << stream_idx;
1385 else if (stream->frequency != old_freq || stream->flags != old_flags)
1386 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1388 return WINED3D_OK;
1391 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1392 UINT stream_idx, UINT *divider)
1394 const struct wined3d_stream_state *stream;
1396 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1398 stream = &device->state.streams[stream_idx];
1399 *divider = stream->flags | stream->frequency;
1401 TRACE("Returning %#x.\n", *divider);
1403 return WINED3D_OK;
1406 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1407 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1409 TRACE("device %p, state %s, matrix %p.\n",
1410 device, debug_d3dtstype(d3dts), matrix);
1411 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1412 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1413 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1414 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1416 /* Handle recording of state blocks. */
1417 if (device->recording)
1419 TRACE("Recording... not performing anything.\n");
1420 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1421 device->update_state->transforms[d3dts] = *matrix;
1422 return;
1425 /* If the new matrix is the same as the current one,
1426 * we cut off any further processing. this seems to be a reasonable
1427 * optimization because as was noticed, some apps (warcraft3 for example)
1428 * tend towards setting the same matrix repeatedly for some reason.
1430 * From here on we assume that the new matrix is different, wherever it matters. */
1431 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1433 TRACE("The application is setting the same matrix over again.\n");
1434 return;
1437 device->state.transforms[d3dts] = *matrix;
1438 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1441 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1442 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1444 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1446 *matrix = device->state.transforms[state];
1449 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1450 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1452 const struct wined3d_matrix *mat;
1453 struct wined3d_matrix temp;
1455 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1457 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1458 * below means it will be recorded in a state block change, but it
1459 * works regardless where it is recorded.
1460 * If this is found to be wrong, change to StateBlock. */
1461 if (state > HIGHEST_TRANSFORMSTATE)
1463 WARN("Unhandled transform state %#x.\n", state);
1464 return;
1467 mat = &device->update_state->transforms[state];
1468 multiply_matrix(&temp, mat, matrix);
1470 /* Apply change via set transform - will reapply to eg. lights this way. */
1471 wined3d_device_set_transform(device, state, &temp);
1474 /* Note lights are real special cases. Although the device caps state only
1475 * e.g. 8 are supported, you can reference any indexes you want as long as
1476 * that number max are enabled at any one point in time. Therefore since the
1477 * indices can be anything, we need a hashmap of them. However, this causes
1478 * stateblock problems. When capturing the state block, I duplicate the
1479 * hashmap, but when recording, just build a chain pretty much of commands to
1480 * be replayed. */
1481 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1482 UINT light_idx, const struct wined3d_light *light)
1484 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1485 struct wined3d_light_info *object = NULL;
1486 struct list *e;
1487 float rho;
1489 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1491 /* Check the parameter range. Need for speed most wanted sets junk lights
1492 * which confuse the GL driver. */
1493 if (!light)
1494 return WINED3DERR_INVALIDCALL;
1496 switch (light->type)
1498 case WINED3D_LIGHT_POINT:
1499 case WINED3D_LIGHT_SPOT:
1500 case WINED3D_LIGHT_GLSPOT:
1501 /* Incorrect attenuation values can cause the gl driver to crash.
1502 * Happens with Need for speed most wanted. */
1503 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1505 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1506 return WINED3DERR_INVALIDCALL;
1508 break;
1510 case WINED3D_LIGHT_DIRECTIONAL:
1511 case WINED3D_LIGHT_PARALLELPOINT:
1512 /* Ignores attenuation */
1513 break;
1515 default:
1516 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1517 return WINED3DERR_INVALIDCALL;
1520 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1522 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1523 if (object->OriginalIndex == light_idx)
1524 break;
1525 object = NULL;
1528 if (!object)
1530 TRACE("Adding new light\n");
1531 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1532 if (!object)
1533 return E_OUTOFMEMORY;
1535 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1536 object->glIndex = -1;
1537 object->OriginalIndex = light_idx;
1540 /* Initialize the object. */
1541 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1542 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1543 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1544 light_idx, light->type, debug_color(&light->diffuse),
1545 debug_color(&light->specular), debug_color(&light->ambient),
1546 light->position.x, light->position.y, light->position.z,
1547 light->direction.x, light->direction.y, light->direction.z,
1548 light->range, light->falloff, light->theta, light->phi);
1550 /* Update the live definitions if the light is currently assigned a glIndex. */
1551 if (object->glIndex != -1 && !device->recording)
1553 if (object->OriginalParms.type != light->type)
1554 device_invalidate_state(device, STATE_LIGHT_TYPE);
1555 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1558 /* Save away the information. */
1559 object->OriginalParms = *light;
1561 switch (light->type)
1563 case WINED3D_LIGHT_POINT:
1564 /* Position */
1565 object->position.x = light->position.x;
1566 object->position.y = light->position.y;
1567 object->position.z = light->position.z;
1568 object->position.w = 1.0f;
1569 object->cutoff = 180.0f;
1570 /* FIXME: Range */
1571 break;
1573 case WINED3D_LIGHT_DIRECTIONAL:
1574 /* Direction */
1575 object->direction.x = -light->direction.x;
1576 object->direction.y = -light->direction.y;
1577 object->direction.z = -light->direction.z;
1578 object->direction.w = 0.0f;
1579 object->exponent = 0.0f;
1580 object->cutoff = 180.0f;
1581 break;
1583 case WINED3D_LIGHT_SPOT:
1584 /* Position */
1585 object->position.x = light->position.x;
1586 object->position.y = light->position.y;
1587 object->position.z = light->position.z;
1588 object->position.w = 1.0f;
1590 /* Direction */
1591 object->direction.x = light->direction.x;
1592 object->direction.y = light->direction.y;
1593 object->direction.z = light->direction.z;
1594 object->direction.w = 0.0f;
1596 /* opengl-ish and d3d-ish spot lights use too different models
1597 * for the light "intensity" as a function of the angle towards
1598 * the main light direction, so we only can approximate very
1599 * roughly. However, spot lights are rather rarely used in games
1600 * (if ever used at all). Furthermore if still used, probably
1601 * nobody pays attention to such details. */
1602 if (!light->falloff)
1604 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1605 * equations have the falloff resp. exponent parameter as an
1606 * exponent, so the spot light lighting will always be 1.0 for
1607 * both of them, and we don't have to care for the rest of the
1608 * rather complex calculation. */
1609 object->exponent = 0.0f;
1611 else
1613 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1614 if (rho < 0.0001f)
1615 rho = 0.0001f;
1616 object->exponent = -0.3f / logf(cosf(rho / 2));
1619 if (object->exponent > 128.0f)
1620 object->exponent = 128.0f;
1622 object->cutoff = (float)(light->phi * 90 / M_PI);
1623 /* FIXME: Range */
1624 break;
1626 case WINED3D_LIGHT_PARALLELPOINT:
1627 object->position.x = light->position.x;
1628 object->position.y = light->position.y;
1629 object->position.z = light->position.z;
1630 object->position.w = 1.0f;
1631 break;
1633 default:
1634 FIXME("Unrecognized light type %#x.\n", light->type);
1637 return WINED3D_OK;
1640 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1641 UINT light_idx, struct wined3d_light *light)
1643 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1644 struct wined3d_light_info *light_info = NULL;
1645 struct list *e;
1647 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1649 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1651 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1652 if (light_info->OriginalIndex == light_idx)
1653 break;
1654 light_info = NULL;
1657 if (!light_info)
1659 TRACE("Light information requested but light not defined\n");
1660 return WINED3DERR_INVALIDCALL;
1663 *light = light_info->OriginalParms;
1664 return WINED3D_OK;
1667 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1669 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1670 struct wined3d_light_info *light_info = NULL;
1671 struct list *e;
1673 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1675 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1677 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1678 if (light_info->OriginalIndex == light_idx)
1679 break;
1680 light_info = NULL;
1682 TRACE("Found light %p.\n", light_info);
1684 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1685 if (!light_info)
1687 TRACE("Light enabled requested but light not defined, so defining one!\n");
1688 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1690 /* Search for it again! Should be fairly quick as near head of list. */
1691 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1693 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1694 if (light_info->OriginalIndex == light_idx)
1695 break;
1696 light_info = NULL;
1698 if (!light_info)
1700 FIXME("Adding default lights has failed dismally\n");
1701 return WINED3DERR_INVALIDCALL;
1705 if (!enable)
1707 if (light_info->glIndex != -1)
1709 if (!device->recording)
1711 device_invalidate_state(device, STATE_LIGHT_TYPE);
1712 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1715 device->update_state->lights[light_info->glIndex] = NULL;
1716 light_info->glIndex = -1;
1718 else
1720 TRACE("Light already disabled, nothing to do\n");
1722 light_info->enabled = FALSE;
1724 else
1726 light_info->enabled = TRUE;
1727 if (light_info->glIndex != -1)
1729 TRACE("Nothing to do as light was enabled\n");
1731 else
1733 unsigned int light_count = device->adapter->d3d_info.limits.active_light_count;
1734 unsigned int i;
1736 /* Find a free light. */
1737 for (i = 0; i < light_count; ++i)
1739 if (!device->update_state->lights[i])
1741 device->update_state->lights[i] = light_info;
1742 light_info->glIndex = i;
1743 break;
1746 if (light_info->glIndex == -1)
1748 /* Our tests show that Windows returns D3D_OK in this situation, even with
1749 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1750 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1751 * as well for those lights.
1753 * TODO: Test how this affects rendering. */
1754 WARN("Too many concurrently active lights\n");
1755 return WINED3D_OK;
1758 /* i == light_info->glIndex */
1759 if (!device->recording)
1761 device_invalidate_state(device, STATE_LIGHT_TYPE);
1762 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1767 return WINED3D_OK;
1770 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1772 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1773 struct wined3d_light_info *light_info = NULL;
1774 struct list *e;
1776 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1778 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1780 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1781 if (light_info->OriginalIndex == light_idx)
1782 break;
1783 light_info = NULL;
1786 if (!light_info)
1788 TRACE("Light enabled state requested but light not defined.\n");
1789 return WINED3DERR_INVALIDCALL;
1791 /* true is 128 according to SetLightEnable */
1792 *enable = light_info->enabled ? 128 : 0;
1793 return WINED3D_OK;
1796 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1797 UINT plane_idx, const struct wined3d_vec4 *plane)
1799 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1801 /* Validate plane_idx. */
1802 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1804 TRACE("Application has requested clipplane this device doesn't support.\n");
1805 return WINED3DERR_INVALIDCALL;
1808 if (device->recording)
1809 device->recording->changed.clipplane |= 1u << plane_idx;
1811 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1813 TRACE("Application is setting old values over, nothing to do.\n");
1814 return WINED3D_OK;
1817 device->update_state->clip_planes[plane_idx] = *plane;
1819 if (!device->recording)
1820 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1822 return WINED3D_OK;
1825 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1826 UINT plane_idx, struct wined3d_vec4 *plane)
1828 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1830 /* Validate plane_idx. */
1831 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1833 TRACE("Application has requested clipplane this device doesn't support.\n");
1834 return WINED3DERR_INVALIDCALL;
1837 *plane = device->state.clip_planes[plane_idx];
1839 return WINED3D_OK;
1842 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1843 const struct wined3d_clip_status *clip_status)
1845 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1847 if (!clip_status)
1848 return WINED3DERR_INVALIDCALL;
1850 return WINED3D_OK;
1853 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1854 struct wined3d_clip_status *clip_status)
1856 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1858 if (!clip_status)
1859 return WINED3DERR_INVALIDCALL;
1861 return WINED3D_OK;
1864 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1866 TRACE("device %p, material %p.\n", device, material);
1868 device->update_state->material = *material;
1870 if (device->recording)
1871 device->recording->changed.material = TRUE;
1872 else
1873 wined3d_cs_emit_set_material(device->cs, material);
1876 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1878 TRACE("device %p, material %p.\n", device, material);
1880 *material = device->state.material;
1882 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1883 TRACE("ambient %s\n", debug_color(&material->ambient));
1884 TRACE("specular %s\n", debug_color(&material->specular));
1885 TRACE("emissive %s\n", debug_color(&material->emissive));
1886 TRACE("power %.8e.\n", material->power);
1889 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1890 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1892 enum wined3d_format_id prev_format;
1893 struct wined3d_buffer *prev_buffer;
1894 unsigned int prev_offset;
1896 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1897 device, buffer, debug_d3dformat(format_id), offset);
1899 prev_buffer = device->update_state->index_buffer;
1900 prev_format = device->update_state->index_format;
1901 prev_offset = device->update_state->index_offset;
1903 device->update_state->index_buffer = buffer;
1904 device->update_state->index_format = format_id;
1905 device->update_state->index_offset = offset;
1907 if (device->recording)
1908 device->recording->changed.indices = TRUE;
1910 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1911 return;
1913 if (buffer)
1914 wined3d_buffer_incref(buffer);
1915 if (!device->recording)
1916 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1917 if (prev_buffer)
1918 wined3d_buffer_decref(prev_buffer);
1921 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1922 enum wined3d_format_id *format, unsigned int *offset)
1924 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1926 *format = device->state.index_format;
1927 if (offset)
1928 *offset = device->state.index_offset;
1929 return device->state.index_buffer;
1932 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1934 TRACE("device %p, base_index %d.\n", device, base_index);
1936 device->update_state->base_vertex_index = base_index;
1939 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1941 TRACE("device %p.\n", device);
1943 return device->state.base_vertex_index;
1946 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1948 TRACE("device %p, viewport %p.\n", device, viewport);
1949 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1950 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1952 device->update_state->viewport = *viewport;
1954 /* Handle recording of state blocks */
1955 if (device->recording)
1957 TRACE("Recording... not performing anything\n");
1958 device->recording->changed.viewport = TRUE;
1959 return;
1962 wined3d_cs_emit_set_viewport(device->cs, viewport);
1965 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1967 TRACE("device %p, viewport %p.\n", device, viewport);
1969 *viewport = device->state.viewport;
1972 static void resolve_depth_buffer(struct wined3d_state *state)
1974 struct wined3d_texture *dst_texture = state->textures[0];
1975 struct wined3d_rendertarget_view *src_view;
1976 RECT src_rect, dst_rect;
1978 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1979 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1980 return;
1982 if (!(src_view = state->fb->depth_stencil))
1983 return;
1984 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
1986 FIXME("Not supported on buffer resources.\n");
1987 return;
1990 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
1991 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
1992 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
1993 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1996 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1997 struct wined3d_rasterizer_state *rasterizer_state)
1999 struct wined3d_rasterizer_state *prev;
2001 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2003 prev = device->update_state->rasterizer_state;
2004 if (prev == rasterizer_state)
2005 return;
2007 if (rasterizer_state)
2008 wined3d_rasterizer_state_incref(rasterizer_state);
2009 device->update_state->rasterizer_state = rasterizer_state;
2010 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2011 if (prev)
2012 wined3d_rasterizer_state_decref(prev);
2015 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2017 TRACE("device %p.\n", device);
2019 return device->state.rasterizer_state;
2022 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2023 enum wined3d_render_state state, DWORD value)
2025 DWORD old_value;
2027 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2029 if (state > WINEHIGHEST_RENDER_STATE)
2031 WARN("Unhandled render state %#x.\n", state);
2032 return;
2035 old_value = device->state.render_states[state];
2036 device->update_state->render_states[state] = value;
2038 /* Handle recording of state blocks. */
2039 if (device->recording)
2041 TRACE("Recording... not performing anything.\n");
2042 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2043 return;
2046 /* Compared here and not before the assignment to allow proper stateblock recording. */
2047 if (value == old_value)
2048 TRACE("Application is setting the old value over, nothing to do.\n");
2049 else
2050 wined3d_cs_emit_set_render_state(device->cs, state, value);
2052 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2054 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2055 resolve_depth_buffer(&device->state);
2059 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2061 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2063 return device->state.render_states[state];
2066 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2067 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2069 DWORD old_value;
2071 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2072 device, sampler_idx, debug_d3dsamplerstate(state), value);
2074 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2075 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2077 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2079 WARN("Invalid sampler %u.\n", sampler_idx);
2080 return; /* Windows accepts overflowing this array ... we do not. */
2083 old_value = device->state.sampler_states[sampler_idx][state];
2084 device->update_state->sampler_states[sampler_idx][state] = value;
2086 /* Handle recording of state blocks. */
2087 if (device->recording)
2089 TRACE("Recording... not performing anything.\n");
2090 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2091 return;
2094 if (old_value == value)
2096 TRACE("Application is setting the old value over, nothing to do.\n");
2097 return;
2100 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2103 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2104 UINT sampler_idx, enum wined3d_sampler_state state)
2106 TRACE("device %p, sampler_idx %u, state %s.\n",
2107 device, sampler_idx, debug_d3dsamplerstate(state));
2109 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2110 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2112 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2114 WARN("Invalid sampler %u.\n", sampler_idx);
2115 return 0; /* Windows accepts overflowing this array ... we do not. */
2118 return device->state.sampler_states[sampler_idx][state];
2121 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2123 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2125 if (device->recording)
2126 device->recording->changed.scissorRect = TRUE;
2128 if (EqualRect(&device->update_state->scissor_rect, rect))
2130 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2131 return;
2133 CopyRect(&device->update_state->scissor_rect, rect);
2135 if (device->recording)
2137 TRACE("Recording... not performing anything.\n");
2138 return;
2141 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2144 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2146 TRACE("device %p, rect %p.\n", device, rect);
2148 *rect = device->state.scissor_rect;
2149 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2152 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2153 struct wined3d_vertex_declaration *declaration)
2155 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2157 TRACE("device %p, declaration %p.\n", device, declaration);
2159 if (device->recording)
2160 device->recording->changed.vertexDecl = TRUE;
2162 if (declaration == prev)
2163 return;
2165 if (declaration)
2166 wined3d_vertex_declaration_incref(declaration);
2167 device->update_state->vertex_declaration = declaration;
2168 if (!device->recording)
2169 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2170 if (prev)
2171 wined3d_vertex_declaration_decref(prev);
2174 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2176 TRACE("device %p.\n", device);
2178 return device->state.vertex_declaration;
2181 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2183 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2185 TRACE("device %p, shader %p.\n", device, shader);
2187 if (device->recording)
2188 device->recording->changed.vertexShader = TRUE;
2190 if (shader == prev)
2191 return;
2193 if (shader)
2194 wined3d_shader_incref(shader);
2195 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2196 if (!device->recording)
2197 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2198 if (prev)
2199 wined3d_shader_decref(prev);
2202 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2204 TRACE("device %p.\n", device);
2206 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2209 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2210 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2212 struct wined3d_buffer *prev;
2214 if (idx >= MAX_CONSTANT_BUFFERS)
2216 WARN("Invalid constant buffer index %u.\n", idx);
2217 return;
2220 prev = device->update_state->cb[type][idx];
2221 if (buffer == prev)
2222 return;
2224 if (buffer)
2225 wined3d_buffer_incref(buffer);
2226 device->update_state->cb[type][idx] = buffer;
2227 if (!device->recording)
2228 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2229 if (prev)
2230 wined3d_buffer_decref(prev);
2233 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2235 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2237 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2240 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2242 TRACE("device %p, idx %u.\n", device, idx);
2244 if (idx >= MAX_CONSTANT_BUFFERS)
2246 WARN("Invalid constant buffer index %u.\n", idx);
2247 return NULL;
2250 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2253 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2254 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2256 struct wined3d_shader_resource_view *prev;
2258 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2260 WARN("Invalid view index %u.\n", idx);
2261 return;
2264 prev = device->update_state->shader_resource_view[type][idx];
2265 if (view == prev)
2266 return;
2268 if (view)
2269 wined3d_shader_resource_view_incref(view);
2270 device->update_state->shader_resource_view[type][idx] = view;
2271 if (!device->recording)
2272 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2273 if (prev)
2274 wined3d_shader_resource_view_decref(prev);
2277 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2278 UINT idx, struct wined3d_shader_resource_view *view)
2280 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2282 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2285 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2286 UINT idx)
2288 TRACE("device %p, idx %u.\n", device, idx);
2290 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2292 WARN("Invalid view index %u.\n", idx);
2293 return NULL;
2296 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2299 static void wined3d_device_set_sampler(struct wined3d_device *device,
2300 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2302 struct wined3d_sampler *prev;
2304 if (idx >= MAX_SAMPLER_OBJECTS)
2306 WARN("Invalid sampler index %u.\n", idx);
2307 return;
2310 prev = device->update_state->sampler[type][idx];
2311 if (sampler == prev)
2312 return;
2314 if (sampler)
2315 wined3d_sampler_incref(sampler);
2316 device->update_state->sampler[type][idx] = sampler;
2317 if (!device->recording)
2318 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2319 if (prev)
2320 wined3d_sampler_decref(prev);
2323 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2325 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2327 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2330 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2332 TRACE("device %p, idx %u.\n", device, idx);
2334 if (idx >= MAX_SAMPLER_OBJECTS)
2336 WARN("Invalid sampler index %u.\n", idx);
2337 return NULL;
2340 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2343 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2344 unsigned int start_idx, unsigned int count, const BOOL *constants)
2346 unsigned int i;
2348 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2349 device, start_idx, count, constants);
2351 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2352 return WINED3DERR_INVALIDCALL;
2354 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2355 count = WINED3D_MAX_CONSTS_B - start_idx;
2356 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2357 if (TRACE_ON(d3d))
2359 for (i = 0; i < count; ++i)
2360 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2363 if (device->recording)
2365 for (i = start_idx; i < count + start_idx; ++i)
2366 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2368 else
2370 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2373 return WINED3D_OK;
2376 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2377 unsigned int start_idx, unsigned int count, BOOL *constants)
2379 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2380 device, start_idx, count, constants);
2382 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2383 return WINED3DERR_INVALIDCALL;
2385 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2386 count = WINED3D_MAX_CONSTS_B - start_idx;
2387 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2389 return WINED3D_OK;
2392 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2393 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2395 unsigned int i;
2397 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2398 device, start_idx, count, constants);
2400 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2401 return WINED3DERR_INVALIDCALL;
2403 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2404 count = WINED3D_MAX_CONSTS_I - start_idx;
2405 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2406 if (TRACE_ON(d3d))
2408 for (i = 0; i < count; ++i)
2409 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2412 if (device->recording)
2414 for (i = start_idx; i < count + start_idx; ++i)
2415 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2417 else
2419 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2422 return WINED3D_OK;
2425 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2426 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2428 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2429 device, start_idx, count, constants);
2431 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2432 return WINED3DERR_INVALIDCALL;
2434 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2435 count = WINED3D_MAX_CONSTS_I - start_idx;
2436 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2437 return WINED3D_OK;
2440 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2441 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2443 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2444 unsigned int i;
2446 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2447 device, start_idx, count, constants);
2449 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2450 || count > d3d_info->limits.vs_uniform_count - start_idx)
2451 return WINED3DERR_INVALIDCALL;
2453 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2454 if (TRACE_ON(d3d))
2456 for (i = 0; i < count; ++i)
2457 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2460 if (device->recording)
2461 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2462 count * sizeof(*device->recording->changed.vs_consts_f));
2463 else
2464 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2466 return WINED3D_OK;
2469 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2470 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2472 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2474 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2475 device, start_idx, count, constants);
2477 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2478 || count > d3d_info->limits.vs_uniform_count - start_idx)
2479 return WINED3DERR_INVALIDCALL;
2481 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2483 return WINED3D_OK;
2486 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2488 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2490 TRACE("device %p, shader %p.\n", device, shader);
2492 if (device->recording)
2493 device->recording->changed.pixelShader = TRUE;
2495 if (shader == prev)
2496 return;
2498 if (shader)
2499 wined3d_shader_incref(shader);
2500 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2501 if (!device->recording)
2502 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2503 if (prev)
2504 wined3d_shader_decref(prev);
2507 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2509 TRACE("device %p.\n", device);
2511 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2514 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2516 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2518 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2521 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2523 TRACE("device %p, idx %u.\n", device, idx);
2525 if (idx >= MAX_CONSTANT_BUFFERS)
2527 WARN("Invalid constant buffer index %u.\n", idx);
2528 return NULL;
2531 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2534 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2535 UINT idx, struct wined3d_shader_resource_view *view)
2537 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2539 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2542 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2543 UINT idx)
2545 TRACE("device %p, idx %u.\n", device, idx);
2547 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2549 WARN("Invalid view index %u.\n", idx);
2550 return NULL;
2553 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2556 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2558 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2560 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2563 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2565 TRACE("device %p, idx %u.\n", device, idx);
2567 if (idx >= MAX_SAMPLER_OBJECTS)
2569 WARN("Invalid sampler index %u.\n", idx);
2570 return NULL;
2573 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2576 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2577 unsigned int start_idx, unsigned int count, const BOOL *constants)
2579 unsigned int i;
2581 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2582 device, start_idx, count, constants);
2584 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2585 return WINED3DERR_INVALIDCALL;
2587 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2588 count = WINED3D_MAX_CONSTS_B - start_idx;
2589 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2590 if (TRACE_ON(d3d))
2592 for (i = 0; i < count; ++i)
2593 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2596 if (device->recording)
2598 for (i = start_idx; i < count + start_idx; ++i)
2599 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2601 else
2603 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2606 return WINED3D_OK;
2609 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2610 unsigned int start_idx, unsigned int count, BOOL *constants)
2612 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2613 device, start_idx, count, constants);
2615 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2616 return WINED3DERR_INVALIDCALL;
2618 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2619 count = WINED3D_MAX_CONSTS_B - start_idx;
2620 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2622 return WINED3D_OK;
2625 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2626 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2628 unsigned int i;
2630 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2631 device, start_idx, count, constants);
2633 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2634 return WINED3DERR_INVALIDCALL;
2636 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2637 count = WINED3D_MAX_CONSTS_I - start_idx;
2638 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2639 if (TRACE_ON(d3d))
2641 for (i = 0; i < count; ++i)
2642 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2645 if (device->recording)
2647 for (i = start_idx; i < count + start_idx; ++i)
2648 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2650 else
2652 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2655 return WINED3D_OK;
2658 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2659 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2661 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2662 device, start_idx, count, constants);
2664 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2665 return WINED3DERR_INVALIDCALL;
2667 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2668 count = WINED3D_MAX_CONSTS_I - start_idx;
2669 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2671 return WINED3D_OK;
2674 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2675 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2677 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2678 unsigned int i;
2680 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2681 device, start_idx, count, constants);
2683 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2684 || count > d3d_info->limits.ps_uniform_count - start_idx)
2685 return WINED3DERR_INVALIDCALL;
2687 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2688 if (TRACE_ON(d3d))
2690 for (i = 0; i < count; ++i)
2691 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2694 if (device->recording)
2695 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2696 count * sizeof(*device->recording->changed.ps_consts_f));
2697 else
2698 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2700 return WINED3D_OK;
2703 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2704 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2706 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2708 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2709 device, start_idx, count, constants);
2711 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2712 || count > d3d_info->limits.ps_uniform_count - start_idx)
2713 return WINED3DERR_INVALIDCALL;
2715 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2717 return WINED3D_OK;
2720 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2722 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2724 TRACE("device %p, shader %p.\n", device, shader);
2726 if (device->recording || shader == prev)
2727 return;
2728 if (shader)
2729 wined3d_shader_incref(shader);
2730 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2731 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2732 if (prev)
2733 wined3d_shader_decref(prev);
2736 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2738 TRACE("device %p.\n", device);
2740 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2743 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2745 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2747 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2750 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2752 TRACE("device %p, idx %u.\n", device, idx);
2754 if (idx >= MAX_CONSTANT_BUFFERS)
2756 WARN("Invalid constant buffer index %u.\n", idx);
2757 return NULL;
2760 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2763 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2764 UINT idx, struct wined3d_shader_resource_view *view)
2766 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2768 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2771 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2772 UINT idx)
2774 TRACE("device %p, idx %u.\n", device, idx);
2776 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2778 WARN("Invalid view index %u.\n", idx);
2779 return NULL;
2782 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2785 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2787 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2789 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2792 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2794 TRACE("device %p, idx %u.\n", device, idx);
2796 if (idx >= MAX_SAMPLER_OBJECTS)
2798 WARN("Invalid sampler index %u.\n", idx);
2799 return NULL;
2802 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2805 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2807 struct wined3d_shader *prev;
2809 TRACE("device %p, shader %p.\n", device, shader);
2811 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2812 if (device->recording || shader == prev)
2813 return;
2814 if (shader)
2815 wined3d_shader_incref(shader);
2816 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2817 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2818 if (prev)
2819 wined3d_shader_decref(prev);
2822 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2824 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2826 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2829 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2830 unsigned int idx, struct wined3d_unordered_access_view *uav)
2832 struct wined3d_unordered_access_view *prev;
2834 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2836 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2838 WARN("Invalid UAV index %u.\n", idx);
2839 return;
2842 prev = device->update_state->unordered_access_view[idx];
2843 if (uav == prev)
2844 return;
2846 if (uav)
2847 wined3d_unordered_access_view_incref(uav);
2848 device->update_state->unordered_access_view[idx] = uav;
2849 if (!device->recording)
2850 wined3d_cs_emit_set_unordered_access_view(device->cs, idx, uav);
2851 if (prev)
2852 wined3d_unordered_access_view_decref(prev);
2855 /* Context activation is done by the caller. */
2856 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2857 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2858 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2859 DWORD DestFVF)
2861 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2862 struct wined3d_map_desc map_desc;
2863 struct wined3d_box box = {0};
2864 struct wined3d_viewport vp;
2865 UINT vertex_size;
2866 unsigned int i;
2867 BYTE *dest_ptr;
2868 BOOL doClip;
2869 DWORD numTextures;
2870 HRESULT hr;
2872 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2874 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2877 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2879 ERR("Source has no position mask\n");
2880 return WINED3DERR_INVALIDCALL;
2883 if (device->state.render_states[WINED3D_RS_CLIPPING])
2885 static BOOL warned = FALSE;
2887 * The clipping code is not quite correct. Some things need
2888 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2889 * so disable clipping for now.
2890 * (The graphics in Half-Life are broken, and my processvertices
2891 * test crashes with IDirect3DDevice3)
2892 doClip = TRUE;
2894 doClip = FALSE;
2895 if(!warned) {
2896 warned = TRUE;
2897 FIXME("Clipping is broken and disabled for now\n");
2900 else
2901 doClip = FALSE;
2903 vertex_size = get_flexible_vertex_size(DestFVF);
2904 box.left = dwDestIndex * vertex_size;
2905 box.right = box.left + dwCount * vertex_size;
2906 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2908 WARN("Failed to map buffer, hr %#x.\n", hr);
2909 return hr;
2911 dest_ptr = map_desc.data;
2913 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2914 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2915 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2917 TRACE("View mat:\n");
2918 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2919 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2920 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2921 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2923 TRACE("Proj mat:\n");
2924 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2925 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2926 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2927 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2929 TRACE("World mat:\n");
2930 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2931 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2932 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2933 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2935 /* Get the viewport */
2936 wined3d_device_get_viewport(device, &vp);
2937 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2938 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2940 multiply_matrix(&mat,&view_mat,&world_mat);
2941 multiply_matrix(&mat,&proj_mat,&mat);
2943 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2945 for (i = 0; i < dwCount; i+= 1) {
2946 unsigned int tex_index;
2948 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2949 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2950 /* The position first */
2951 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2952 const float *p = (const float *)(element->data.addr + i * element->stride);
2953 float x, y, z, rhw;
2954 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2956 /* Multiplication with world, view and projection matrix. */
2957 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2958 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2959 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2960 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2962 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2964 /* WARNING: The following things are taken from d3d7 and were not yet checked
2965 * against d3d8 or d3d9!
2968 /* Clipping conditions: From msdn
2970 * A vertex is clipped if it does not match the following requirements
2971 * -rhw < x <= rhw
2972 * -rhw < y <= rhw
2973 * 0 < z <= rhw
2974 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2976 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2977 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2981 if( !doClip ||
2982 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2983 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2984 ( rhw > eps ) ) ) {
2986 /* "Normal" viewport transformation (not clipped)
2987 * 1) The values are divided by rhw
2988 * 2) The y axis is negative, so multiply it with -1
2989 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2990 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2991 * 4) Multiply x with Width/2 and add Width/2
2992 * 5) The same for the height
2993 * 6) Add the viewpoint X and Y to the 2D coordinates and
2994 * The minimum Z value to z
2995 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2997 * Well, basically it's simply a linear transformation into viewport
2998 * coordinates
3001 x /= rhw;
3002 y /= rhw;
3003 z /= rhw;
3005 y *= -1;
3007 x *= vp.width / 2;
3008 y *= vp.height / 2;
3009 z *= vp.max_z - vp.min_z;
3011 x += vp.width / 2 + vp.x;
3012 y += vp.height / 2 + vp.y;
3013 z += vp.min_z;
3015 rhw = 1 / rhw;
3016 } else {
3017 /* That vertex got clipped
3018 * Contrary to OpenGL it is not dropped completely, it just
3019 * undergoes a different calculation.
3021 TRACE("Vertex got clipped\n");
3022 x += rhw;
3023 y += rhw;
3025 x /= 2;
3026 y /= 2;
3028 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3029 * outside of the main vertex buffer memory. That needs some more
3030 * investigation...
3034 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3037 ( (float *) dest_ptr)[0] = x;
3038 ( (float *) dest_ptr)[1] = y;
3039 ( (float *) dest_ptr)[2] = z;
3040 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3042 dest_ptr += 3 * sizeof(float);
3044 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3045 dest_ptr += sizeof(float);
3048 if (DestFVF & WINED3DFVF_PSIZE)
3049 dest_ptr += sizeof(DWORD);
3051 if (DestFVF & WINED3DFVF_NORMAL)
3053 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3054 const float *normal = (const float *)(element->data.addr + i * element->stride);
3055 /* AFAIK this should go into the lighting information */
3056 FIXME("Didn't expect the destination to have a normal\n");
3057 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3060 if (DestFVF & WINED3DFVF_DIFFUSE)
3062 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3063 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3064 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3066 static BOOL warned = FALSE;
3068 if(!warned) {
3069 ERR("No diffuse color in source, but destination has one\n");
3070 warned = TRUE;
3073 *( (DWORD *) dest_ptr) = 0xffffffff;
3074 dest_ptr += sizeof(DWORD);
3076 else
3078 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3082 if (DestFVF & WINED3DFVF_SPECULAR)
3084 /* What's the color value in the feedback buffer? */
3085 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3086 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3087 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3089 static BOOL warned = FALSE;
3091 if(!warned) {
3092 ERR("No specular color in source, but destination has one\n");
3093 warned = TRUE;
3096 *(DWORD *)dest_ptr = 0xff000000;
3097 dest_ptr += sizeof(DWORD);
3099 else
3101 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3105 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3107 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3108 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3109 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3111 ERR("No source texture, but destination requests one\n");
3112 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3114 else
3116 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3121 wined3d_resource_unmap(&dest->resource, 0);
3123 return WINED3D_OK;
3125 #undef copy_and_next
3127 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3128 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3129 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3131 struct wined3d_state *state = &device->state;
3132 struct wined3d_stream_info stream_info;
3133 const struct wined3d_gl_info *gl_info;
3134 struct wined3d_context *context;
3135 struct wined3d_shader *vs;
3136 unsigned int i;
3137 HRESULT hr;
3138 WORD map;
3140 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3141 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3142 device, src_start_idx, dst_idx, vertex_count,
3143 dst_buffer, declaration, flags, dst_fvf);
3145 if (declaration)
3146 FIXME("Output vertex declaration not implemented yet.\n");
3148 /* Need any context to write to the vbo. */
3149 context = context_acquire(device, NULL);
3150 gl_info = context->gl_info;
3152 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3153 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3154 context_stream_info_from_declaration(context, state, &stream_info);
3155 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3157 /* We can't convert FROM a VBO, and vertex buffers used to source into
3158 * process_vertices() are unlikely to ever be used for drawing. Release
3159 * VBOs in those buffers and fix up the stream_info structure.
3161 * Also apply the start index. */
3162 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3164 struct wined3d_stream_info_element *e;
3165 struct wined3d_buffer *buffer;
3167 if (!(map & 1))
3168 continue;
3170 e = &stream_info.elements[i];
3171 buffer = state->streams[e->stream_idx].buffer;
3172 e->data.buffer_object = 0;
3173 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3174 if (buffer->buffer_object)
3176 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3177 buffer->buffer_object = 0;
3178 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
3180 if (e->data.addr)
3181 e->data.addr += e->stride * src_start_idx;
3184 hr = process_vertices_strided(device, dst_idx, vertex_count,
3185 &stream_info, dst_buffer, flags, dst_fvf);
3187 context_release(context);
3189 return hr;
3192 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3193 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3195 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3196 DWORD old_value;
3198 TRACE("device %p, stage %u, state %s, value %#x.\n",
3199 device, stage, debug_d3dtexturestate(state), value);
3201 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3203 WARN("Invalid state %#x passed.\n", state);
3204 return;
3207 if (stage >= d3d_info->limits.ffp_blend_stages)
3209 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3210 stage, d3d_info->limits.ffp_blend_stages - 1);
3211 return;
3214 old_value = device->update_state->texture_states[stage][state];
3215 device->update_state->texture_states[stage][state] = value;
3217 if (device->recording)
3219 TRACE("Recording... not performing anything.\n");
3220 device->recording->changed.textureState[stage] |= 1u << state;
3221 return;
3224 /* Checked after the assignments to allow proper stateblock recording. */
3225 if (old_value == value)
3227 TRACE("Application is setting the old value over, nothing to do.\n");
3228 return;
3231 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3234 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3235 UINT stage, enum wined3d_texture_stage_state state)
3237 TRACE("device %p, stage %u, state %s.\n",
3238 device, stage, debug_d3dtexturestate(state));
3240 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3242 WARN("Invalid state %#x passed.\n", state);
3243 return 0;
3246 return device->state.texture_states[stage][state];
3249 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3250 UINT stage, struct wined3d_texture *texture)
3252 struct wined3d_texture *prev;
3254 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3256 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3257 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3259 /* Windows accepts overflowing this array... we do not. */
3260 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3262 WARN("Ignoring invalid stage %u.\n", stage);
3263 return WINED3D_OK;
3266 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3268 WARN("Rejecting attempt to set scratch texture.\n");
3269 return WINED3DERR_INVALIDCALL;
3272 if (device->recording)
3273 device->recording->changed.textures |= 1u << stage;
3275 prev = device->update_state->textures[stage];
3276 TRACE("Previous texture %p.\n", prev);
3278 if (texture == prev)
3280 TRACE("App is setting the same texture again, nothing to do.\n");
3281 return WINED3D_OK;
3284 TRACE("Setting new texture to %p.\n", texture);
3285 device->update_state->textures[stage] = texture;
3287 if (texture)
3288 wined3d_texture_incref(texture);
3289 if (!device->recording)
3290 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3291 if (prev)
3292 wined3d_texture_decref(prev);
3294 return WINED3D_OK;
3297 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3299 TRACE("device %p, stage %u.\n", device, stage);
3301 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3302 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3304 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3306 WARN("Ignoring invalid stage %u.\n", stage);
3307 return NULL; /* Windows accepts overflowing this array ... we do not. */
3310 return device->state.textures[stage];
3313 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3315 TRACE("device %p, caps %p.\n", device, caps);
3317 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3318 device->create_parms.device_type, caps);
3321 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3322 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3324 struct wined3d_swapchain *swapchain;
3326 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3327 device, swapchain_idx, mode, rotation);
3329 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3330 return WINED3DERR_INVALIDCALL;
3332 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3335 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3337 struct wined3d_stateblock *stateblock;
3338 HRESULT hr;
3340 TRACE("device %p.\n", device);
3342 if (device->recording)
3343 return WINED3DERR_INVALIDCALL;
3345 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3346 if (FAILED(hr))
3347 return hr;
3349 device->recording = stateblock;
3350 device->update_state = &stateblock->state;
3352 TRACE("Recording stateblock %p.\n", stateblock);
3354 return WINED3D_OK;
3357 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3358 struct wined3d_stateblock **stateblock)
3360 struct wined3d_stateblock *object = device->recording;
3362 TRACE("device %p, stateblock %p.\n", device, stateblock);
3364 if (!device->recording)
3366 WARN("Not recording.\n");
3367 *stateblock = NULL;
3368 return WINED3DERR_INVALIDCALL;
3371 stateblock_init_contained_states(object);
3373 *stateblock = object;
3374 device->recording = NULL;
3375 device->update_state = &device->state;
3377 TRACE("Returning stateblock %p.\n", *stateblock);
3379 return WINED3D_OK;
3382 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3384 /* At the moment we have no need for any functionality at the beginning
3385 * of a scene. */
3386 TRACE("device %p.\n", device);
3388 if (device->inScene)
3390 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3391 return WINED3DERR_INVALIDCALL;
3393 device->inScene = TRUE;
3394 return WINED3D_OK;
3397 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3399 struct wined3d_context *context;
3401 TRACE("device %p.\n", device);
3403 if (!device->inScene)
3405 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3406 return WINED3DERR_INVALIDCALL;
3409 context = context_acquire(device, NULL);
3410 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3411 context->gl_info->gl_ops.gl.p_glFlush();
3412 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3413 * fails. */
3414 context_release(context);
3416 device->inScene = FALSE;
3417 return WINED3D_OK;
3420 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3421 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3423 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3424 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3426 if (!rect_count && rects)
3428 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3429 return WINED3D_OK;
3432 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3434 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3435 if (!ds)
3437 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3438 /* TODO: What about depth stencil buffers without stencil bits? */
3439 return WINED3DERR_INVALIDCALL;
3441 else if (flags & WINED3DCLEAR_TARGET)
3443 if (ds->width < device->fb.render_targets[0]->width
3444 || ds->height < device->fb.render_targets[0]->height)
3446 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3447 return WINED3D_OK;
3452 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3454 return WINED3D_OK;
3457 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3458 struct wined3d_query *predicate, BOOL value)
3460 struct wined3d_query *prev;
3462 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3464 prev = device->update_state->predicate;
3465 if (predicate)
3467 FIXME("Predicated rendering not implemented.\n");
3468 wined3d_query_incref(predicate);
3470 device->update_state->predicate = predicate;
3471 device->update_state->predicate_value = value;
3472 if (!device->recording)
3473 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3474 if (prev)
3475 wined3d_query_decref(prev);
3478 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3480 TRACE("device %p, value %p.\n", device, value);
3482 *value = device->state.predicate_value;
3483 return device->state.predicate;
3486 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3487 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3489 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3490 device, group_count_x, group_count_y, group_count_z);
3492 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3495 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3496 enum wined3d_primitive_type primitive_type)
3498 GLenum gl_primitive_type, prev;
3500 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3502 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3503 prev = device->update_state->gl_primitive_type;
3504 device->update_state->gl_primitive_type = gl_primitive_type;
3505 if (device->recording)
3506 device->recording->changed.primitive_type = TRUE;
3507 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3508 device_invalidate_state(device, STATE_POINT_ENABLE);
3511 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3512 enum wined3d_primitive_type *primitive_type)
3514 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3516 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3518 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3521 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3523 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3525 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, 0, 0, FALSE);
3527 return WINED3D_OK;
3530 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3531 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3533 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3534 device, start_vertex, vertex_count, start_instance, instance_count);
3536 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3539 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3541 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3543 if (!device->state.index_buffer)
3545 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3546 * without an index buffer set. (The first time at least...)
3547 * D3D8 simply dies, but I doubt it can do much harm to return
3548 * D3DERR_INVALIDCALL there as well. */
3549 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3550 return WINED3DERR_INVALIDCALL;
3553 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3555 return WINED3D_OK;
3558 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3559 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3561 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3562 device, start_idx, index_count, start_instance, instance_count);
3564 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index,
3565 start_idx, index_count, start_instance, instance_count, TRUE);
3568 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3569 struct wined3d_texture *src_texture, unsigned int src_level,
3570 struct wined3d_texture *dst_texture, unsigned int level_count)
3572 struct wined3d_const_bo_address data;
3573 struct wined3d_context *context;
3574 struct wined3d_map_desc src;
3575 HRESULT hr = WINED3D_OK;
3576 unsigned int i;
3578 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3579 device, src_texture, src_level, dst_texture, level_count);
3581 if (src_texture->resource.format != dst_texture->resource.format)
3583 WARN("Source and destination formats do not match.\n");
3584 return WINED3DERR_INVALIDCALL;
3587 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3588 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3589 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3591 WARN("Source and destination dimensions do not match.\n");
3592 return WINED3DERR_INVALIDCALL;
3595 context = context_acquire(device, NULL);
3597 /* Only a prepare, since we're uploading entire volumes. */
3598 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3599 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3601 for (i = 0; i < level_count; ++i)
3603 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3604 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3605 goto done;
3607 data.buffer_object = 0;
3608 data.addr = src.data;
3609 wined3d_texture_upload_data(dst_texture, i, context, NULL, &data, src.row_pitch, src.slice_pitch);
3610 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3612 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3613 goto done;
3616 done:
3617 context_release(context);
3618 return hr;
3621 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3622 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3624 unsigned int src_size, dst_size, src_skip_levels = 0;
3625 unsigned int layer_count, level_count, i, j;
3626 enum wined3d_resource_type type;
3627 HRESULT hr;
3628 struct wined3d_context *context;
3630 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3632 /* Verify that the source and destination textures are non-NULL. */
3633 if (!src_texture || !dst_texture)
3635 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3636 return WINED3DERR_INVALIDCALL;
3639 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3641 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3642 return WINED3DERR_INVALIDCALL;
3644 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3646 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3647 return WINED3DERR_INVALIDCALL;
3650 /* Verify that the source and destination textures are the same type. */
3651 type = src_texture->resource.type;
3652 if (dst_texture->resource.type != type)
3654 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3655 return WINED3DERR_INVALIDCALL;
3658 layer_count = src_texture->layer_count;
3659 if (layer_count != dst_texture->layer_count)
3661 WARN("Source and destination have different layer counts.\n");
3662 return WINED3DERR_INVALIDCALL;
3665 level_count = min(wined3d_texture_get_level_count(src_texture),
3666 wined3d_texture_get_level_count(dst_texture));
3668 src_size = max(src_texture->resource.width, src_texture->resource.height);
3669 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3670 if (type == WINED3D_RTYPE_TEXTURE_3D)
3672 src_size = max(src_size, src_texture->resource.depth);
3673 dst_size = max(dst_size, dst_texture->resource.depth);
3675 while (src_size > dst_size)
3677 src_size >>= 1;
3678 ++src_skip_levels;
3681 /* Make sure that the destination texture is loaded. */
3682 context = context_acquire(device, NULL);
3683 wined3d_texture_load(dst_texture, context, FALSE);
3684 context_release(context);
3686 /* Update every surface level of the texture. */
3687 switch (type)
3689 case WINED3D_RTYPE_TEXTURE_2D:
3691 unsigned int src_levels = src_texture->level_count;
3692 unsigned int dst_levels = dst_texture->level_count;
3693 struct wined3d_surface *src_surface;
3694 struct wined3d_surface *dst_surface;
3696 for (i = 0; i < layer_count; ++i)
3698 for (j = 0; j < level_count; ++j)
3700 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3701 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3702 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3704 WARN("Failed to update surface, hr %#x.\n", hr);
3705 return hr;
3709 return WINED3D_OK;
3712 case WINED3D_RTYPE_TEXTURE_3D:
3713 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3714 src_texture, src_skip_levels, dst_texture, level_count)))
3715 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3716 return hr;
3718 default:
3719 FIXME("Unsupported texture type %#x.\n", type);
3720 return WINED3DERR_INVALIDCALL;
3724 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3726 const struct wined3d_state *state = &device->state;
3727 struct wined3d_texture *texture;
3728 DWORD i;
3730 TRACE("device %p, num_passes %p.\n", device, num_passes);
3732 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3734 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3736 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3737 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3739 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3741 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3742 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3745 texture = state->textures[i];
3746 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3748 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3750 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3751 return E_FAIL;
3753 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3755 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3756 return E_FAIL;
3758 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3759 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3761 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3762 return E_FAIL;
3766 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3767 || state->render_states[WINED3D_RS_STENCILENABLE])
3769 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3770 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3772 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3774 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3775 return WINED3DERR_CONFLICTINGRENDERSTATE;
3779 /* return a sensible default */
3780 *num_passes = 1;
3782 TRACE("returning D3D_OK\n");
3783 return WINED3D_OK;
3786 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3788 static BOOL warned;
3790 TRACE("device %p, software %#x.\n", device, software);
3792 if (!warned)
3794 FIXME("device %p, software %#x stub!\n", device, software);
3795 warned = TRUE;
3798 device->softwareVertexProcessing = software;
3801 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3803 static BOOL warned;
3805 TRACE("device %p.\n", device);
3807 if (!warned)
3809 TRACE("device %p stub!\n", device);
3810 warned = TRUE;
3813 return device->softwareVertexProcessing;
3816 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3817 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3819 struct wined3d_swapchain *swapchain;
3821 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3822 device, swapchain_idx, raster_status);
3824 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3825 return WINED3DERR_INVALIDCALL;
3827 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3830 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3832 static BOOL warned;
3834 TRACE("device %p, segments %.8e.\n", device, segments);
3836 if (segments != 0.0f)
3838 if (!warned)
3840 FIXME("device %p, segments %.8e stub!\n", device, segments);
3841 warned = TRUE;
3845 return WINED3D_OK;
3848 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3850 static BOOL warned;
3852 TRACE("device %p.\n", device);
3854 if (!warned)
3856 FIXME("device %p stub!\n", device);
3857 warned = TRUE;
3860 return 0.0f;
3863 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3864 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3866 struct wined3d_texture *dst_texture, *src_texture;
3867 RECT dst_rect, src_rect;
3868 unsigned int i, j;
3869 HRESULT hr;
3871 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3873 if (src_resource == dst_resource)
3875 WARN("Source and destination are the same resource.\n");
3876 return;
3879 if (src_resource->type != dst_resource->type)
3881 WARN("Resource types (%s / %s) don't match.\n",
3882 debug_d3dresourcetype(dst_resource->type),
3883 debug_d3dresourcetype(src_resource->type));
3884 return;
3887 if (src_resource->width != dst_resource->width
3888 || src_resource->height != dst_resource->height
3889 || src_resource->depth != dst_resource->depth)
3891 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3892 dst_resource->width, dst_resource->height, dst_resource->depth,
3893 src_resource->width, src_resource->height, src_resource->depth);
3894 return;
3897 if (src_resource->format->id != dst_resource->format->id)
3899 WARN("Resource formats (%s / %s) don't match.\n",
3900 debug_d3dformat(dst_resource->format->id),
3901 debug_d3dformat(src_resource->format->id));
3902 return;
3905 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3907 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3908 buffer_from_resource(src_resource), 0,
3909 dst_resource->size)))
3910 ERR("Failed to copy buffer, hr %#x.\n", hr);
3911 return;
3914 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3916 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3917 return;
3920 dst_texture = texture_from_resource(dst_resource);
3921 src_texture = texture_from_resource(src_resource);
3923 if (src_texture->layer_count != dst_texture->layer_count
3924 || src_texture->level_count != dst_texture->level_count)
3926 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3927 dst_texture->layer_count, dst_texture->level_count,
3928 src_texture->layer_count, src_texture->level_count);
3929 return;
3932 for (i = 0; i < dst_texture->level_count; ++i)
3934 SetRect(&dst_rect, 0, 0,
3935 wined3d_texture_get_level_width(dst_texture, i),
3936 wined3d_texture_get_level_height(dst_texture, i));
3937 SetRect(&src_rect, 0, 0,
3938 wined3d_texture_get_level_width(src_texture, i),
3939 wined3d_texture_get_level_height(dst_texture, i));
3940 for (j = 0; j < dst_texture->layer_count; ++j)
3942 unsigned int idx = j * dst_texture->level_count + i;
3944 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3945 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3946 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3951 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3952 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3953 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3954 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3956 struct wined3d_texture *dst_texture, *src_texture;
3957 RECT dst_rect, src_rect;
3958 HRESULT hr;
3960 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3961 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3962 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3963 src_resource, src_sub_resource_idx, debug_box(src_box));
3965 if (src_box && (src_box->left >= src_box->right
3966 || src_box->top >= src_box->bottom
3967 || src_box->front >= src_box->back))
3969 WARN("Invalid box %s specified.\n", debug_box(src_box));
3970 return WINED3DERR_INVALIDCALL;
3973 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3975 WARN("Source and destination are the same sub-resource.\n");
3976 return WINED3DERR_INVALIDCALL;
3979 if (src_resource->type != dst_resource->type)
3981 WARN("Resource types (%s / %s) don't match.\n",
3982 debug_d3dresourcetype(dst_resource->type),
3983 debug_d3dresourcetype(src_resource->type));
3984 return WINED3DERR_INVALIDCALL;
3987 if (src_resource->format->id != dst_resource->format->id)
3989 WARN("Resource formats (%s / %s) don't match.\n",
3990 debug_d3dformat(dst_resource->format->id),
3991 debug_d3dformat(src_resource->format->id));
3992 return WINED3DERR_INVALIDCALL;
3995 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3997 unsigned int src_offset, size;
3999 if (dst_sub_resource_idx)
4001 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4002 return WINED3DERR_INVALIDCALL;
4004 if (src_sub_resource_idx)
4006 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4007 return WINED3DERR_INVALIDCALL;
4010 if (src_box)
4012 src_offset = src_box->left;
4013 size = src_box->right - src_box->left;
4015 else
4017 src_offset = 0;
4018 size = src_resource->size;
4021 if (src_offset > src_resource->size
4022 || size > src_resource->size - src_offset
4023 || dst_x > dst_resource->size
4024 || size > dst_resource->size - dst_x)
4026 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4027 dst_x, src_offset, size);
4028 return WINED3DERR_INVALIDCALL;
4031 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4032 buffer_from_resource(src_resource), src_offset, size);
4035 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4037 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4038 return WINED3DERR_INVALIDCALL;
4041 dst_texture = texture_from_resource(dst_resource);
4042 src_texture = texture_from_resource(src_resource);
4044 if (src_box)
4046 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4048 else
4050 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4052 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4053 wined3d_texture_get_level_height(src_texture, level));
4056 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4057 dst_y + (src_rect.bottom - src_rect.top));
4059 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4060 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4061 WARN("Failed to blit, hr %#x.\n", hr);
4063 return hr;
4066 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4067 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4068 unsigned int depth_pitch)
4070 unsigned int width, height, depth, level;
4071 struct wined3d_const_bo_address addr;
4072 struct wined3d_context *context;
4073 struct wined3d_texture *texture;
4075 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4076 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4078 if (resource->type == WINED3D_RTYPE_BUFFER)
4080 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4082 if (sub_resource_idx > 0)
4084 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4085 return;
4088 context = context_acquire(resource->device, NULL);
4089 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
4091 ERR("Failed to load buffer location.\n");
4092 return;
4095 wined3d_buffer_upload_data(buffer, context, box, data);
4096 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
4097 context_release(context);
4099 return;
4102 if (resource->type != WINED3D_RTYPE_TEXTURE_2D && resource->type != WINED3D_RTYPE_TEXTURE_3D)
4104 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4105 return;
4108 texture = texture_from_resource(resource);
4109 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4111 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4112 return;
4115 level = sub_resource_idx % texture->level_count;
4116 width = wined3d_texture_get_level_width(texture, level);
4117 height = wined3d_texture_get_level_height(texture, level);
4118 depth = wined3d_texture_get_level_depth(texture, level);
4120 if (box && (box->left >= box->right || box->right > width
4121 || box->top >= box->bottom || box->bottom > height
4122 || box->front >= box->back || box->back > depth))
4124 WARN("Invalid box %s specified.\n", debug_box(box));
4125 return;
4128 addr.buffer_object = 0;
4129 addr.addr = data;
4131 context = context_acquire(resource->device, NULL);
4133 /* Only load the sub-resource for partial updates. */
4134 if (!box || (!box->left && !box->top && !box->front
4135 && box->right == width && box->bottom == height && box->back == depth))
4136 wined3d_texture_prepare_texture(texture, context, FALSE);
4137 else
4138 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4139 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4141 wined3d_texture_upload_data(texture, sub_resource_idx, context, box, &addr, row_pitch, depth_pitch);
4143 context_release(context);
4145 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4146 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4149 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4150 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4151 const struct wined3d_color *color, float depth, DWORD stencil)
4153 const struct blit_shader *blitter;
4154 struct wined3d_resource *resource;
4155 enum wined3d_blit_op blit_op;
4156 RECT r;
4158 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4159 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4161 if (!flags)
4162 return WINED3D_OK;
4164 resource = view->resource;
4165 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4167 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4168 return WINED3DERR_INVALIDCALL;
4171 if (view->depth > 1)
4173 FIXME("Layered clears not implemented.\n");
4174 return WINED3DERR_INVALIDCALL;
4177 if (!rect)
4179 SetRect(&r, 0, 0, view->width, view->height);
4180 rect = &r;
4183 if (flags & WINED3DCLEAR_TARGET)
4184 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4185 else
4186 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4188 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4189 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4191 FIXME("No blitter is capable of performing the requested fill operation.\n");
4192 return WINED3DERR_INVALIDCALL;
4195 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4196 return blitter->color_fill(device, view, rect, color);
4197 else
4198 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4201 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4202 unsigned int view_idx)
4204 TRACE("device %p, view_idx %u.\n", device, view_idx);
4206 if (view_idx >= device->adapter->gl_info.limits.buffers)
4208 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4209 return NULL;
4212 return device->fb.render_targets[view_idx];
4215 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4217 TRACE("device %p.\n", device);
4219 return device->fb.depth_stencil;
4222 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4223 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4225 struct wined3d_rendertarget_view *prev;
4227 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4228 device, view_idx, view, set_viewport);
4230 if (view_idx >= device->adapter->gl_info.limits.buffers)
4232 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4233 return WINED3DERR_INVALIDCALL;
4236 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4238 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4239 return WINED3DERR_INVALIDCALL;
4242 /* Set the viewport and scissor rectangles, if requested. Tests show that
4243 * stateblock recording is ignored, the change goes directly into the
4244 * primary stateblock. */
4245 if (!view_idx && set_viewport)
4247 struct wined3d_state *state = &device->state;
4249 state->viewport.x = 0;
4250 state->viewport.y = 0;
4251 state->viewport.width = view->width;
4252 state->viewport.height = view->height;
4253 state->viewport.min_z = 0.0f;
4254 state->viewport.max_z = 1.0f;
4255 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4257 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4258 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4262 prev = device->fb.render_targets[view_idx];
4263 if (view == prev)
4264 return WINED3D_OK;
4266 if (view)
4267 wined3d_rendertarget_view_incref(view);
4268 device->fb.render_targets[view_idx] = view;
4269 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4270 /* Release after the assignment, to prevent device_resource_released()
4271 * from seeing the surface as still in use. */
4272 if (prev)
4273 wined3d_rendertarget_view_decref(prev);
4275 return WINED3D_OK;
4278 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4280 struct wined3d_rendertarget_view *prev;
4282 TRACE("device %p, view %p.\n", device, view);
4284 prev = device->fb.depth_stencil;
4285 if (prev == view)
4287 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4288 return;
4291 if ((device->fb.depth_stencil = view))
4292 wined3d_rendertarget_view_incref(view);
4293 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4294 if (prev)
4295 wined3d_rendertarget_view_decref(prev);
4298 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4299 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4301 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4302 struct wined3d_sub_resource_data data;
4303 struct wined3d_resource_desc desc;
4304 struct wined3d_map_desc map_desc;
4305 struct wined3d_texture *texture;
4306 HRESULT hr;
4308 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4310 ERR("Failed to map source texture.\n");
4311 return NULL;
4314 data.data = map_desc.data;
4315 data.row_pitch = map_desc.row_pitch;
4316 data.slice_pitch = map_desc.slice_pitch;
4318 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4319 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4320 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4321 desc.multisample_quality = 0;
4322 desc.usage = WINED3DUSAGE_DYNAMIC;
4323 desc.pool = WINED3D_POOL_DEFAULT;
4324 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4325 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4326 desc.depth = 1;
4327 desc.size = 0;
4329 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4330 &data, NULL, &wined3d_null_parent_ops, &texture);
4331 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4332 if (FAILED(hr))
4334 ERR("Failed to create cursor texture.\n");
4335 return NULL;
4338 return texture;
4341 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4342 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4344 unsigned int texture_level = sub_resource_idx % texture->level_count;
4345 unsigned int cursor_width, cursor_height;
4346 struct wined3d_display_mode mode;
4347 struct wined3d_map_desc map_desc;
4348 HRESULT hr;
4350 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4351 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4353 if (sub_resource_idx >= texture->level_count * texture->layer_count
4354 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4355 return WINED3DERR_INVALIDCALL;
4357 if (device->cursor_texture)
4359 wined3d_texture_decref(device->cursor_texture);
4360 device->cursor_texture = NULL;
4363 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4365 WARN("Texture %p has invalid format %s.\n",
4366 texture, debug_d3dformat(texture->resource.format->id));
4367 return WINED3DERR_INVALIDCALL;
4370 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4372 ERR("Failed to get display mode, hr %#x.\n", hr);
4373 return WINED3DERR_INVALIDCALL;
4376 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4377 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4378 if (cursor_width > mode.width || cursor_height > mode.height)
4380 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4381 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4382 return WINED3DERR_INVALIDCALL;
4385 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4387 /* Do not store the surface's pointer because the application may
4388 * release it after setting the cursor image. Windows doesn't
4389 * addref the set surface, so we can't do this either without
4390 * creating circular refcount dependencies. */
4391 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4393 ERR("Failed to create cursor texture.\n");
4394 return WINED3DERR_INVALIDCALL;
4397 if (cursor_width == 32 && cursor_height == 32)
4399 UINT mask_size = cursor_width * cursor_height / 8;
4400 ICONINFO cursor_info;
4401 DWORD *mask_bits;
4402 HCURSOR cursor;
4404 /* 32-bit user32 cursors ignore the alpha channel if it's all
4405 * zeroes, and use the mask instead. Fill the mask with all ones
4406 * to ensure we still get a fully transparent cursor. */
4407 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4408 return E_OUTOFMEMORY;
4409 memset(mask_bits, 0xff, mask_size);
4411 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4412 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4413 cursor_info.fIcon = FALSE;
4414 cursor_info.xHotspot = x_hotspot;
4415 cursor_info.yHotspot = y_hotspot;
4416 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4417 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4418 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4420 /* Create our cursor and clean up. */
4421 cursor = CreateIconIndirect(&cursor_info);
4422 if (cursor_info.hbmMask)
4423 DeleteObject(cursor_info.hbmMask);
4424 if (cursor_info.hbmColor)
4425 DeleteObject(cursor_info.hbmColor);
4426 if (device->hardwareCursor)
4427 DestroyCursor(device->hardwareCursor);
4428 device->hardwareCursor = cursor;
4429 if (device->bCursorVisible)
4430 SetCursor(cursor);
4432 HeapFree(GetProcessHeap(), 0, mask_bits);
4435 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4436 device->cursorWidth = cursor_width;
4437 device->cursorHeight = cursor_height;
4438 device->xHotSpot = x_hotspot;
4439 device->yHotSpot = y_hotspot;
4441 return WINED3D_OK;
4444 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4445 int x_screen_space, int y_screen_space, DWORD flags)
4447 TRACE("device %p, x %d, y %d, flags %#x.\n",
4448 device, x_screen_space, y_screen_space, flags);
4450 device->xScreenSpace = x_screen_space;
4451 device->yScreenSpace = y_screen_space;
4453 if (device->hardwareCursor)
4455 POINT pt;
4457 GetCursorPos( &pt );
4458 if (x_screen_space == pt.x && y_screen_space == pt.y)
4459 return;
4460 SetCursorPos( x_screen_space, y_screen_space );
4462 /* Switch to the software cursor if position diverges from the hardware one. */
4463 GetCursorPos( &pt );
4464 if (x_screen_space != pt.x || y_screen_space != pt.y)
4466 if (device->bCursorVisible) SetCursor( NULL );
4467 DestroyCursor( device->hardwareCursor );
4468 device->hardwareCursor = 0;
4473 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4475 BOOL oldVisible = device->bCursorVisible;
4477 TRACE("device %p, show %#x.\n", device, show);
4480 * When ShowCursor is first called it should make the cursor appear at the OS's last
4481 * known cursor position.
4483 if (show && !oldVisible)
4485 POINT pt;
4486 GetCursorPos(&pt);
4487 device->xScreenSpace = pt.x;
4488 device->yScreenSpace = pt.y;
4491 if (device->hardwareCursor)
4493 device->bCursorVisible = show;
4494 if (show)
4495 SetCursor(device->hardwareCursor);
4496 else
4497 SetCursor(NULL);
4499 else if (device->cursor_texture)
4501 device->bCursorVisible = show;
4504 return oldVisible;
4507 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4509 struct wined3d_resource *resource, *cursor;
4511 TRACE("device %p.\n", device);
4513 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4515 TRACE("Checking resource %p for eviction.\n", resource);
4517 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4519 TRACE("Evicting %p.\n", resource);
4520 wined3d_cs_emit_unload_resource(device->cs, resource);
4525 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4527 struct wined3d_resource *resource, *cursor;
4528 const struct wined3d_gl_info *gl_info;
4529 struct wined3d_context *context;
4530 struct wined3d_shader *shader;
4532 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4534 TRACE("Unloading resource %p.\n", resource);
4535 wined3d_cs_emit_unload_resource(device->cs, resource);
4538 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4540 device->shader_backend->shader_destroy(shader);
4543 context = context_acquire(device, NULL);
4544 gl_info = context->gl_info;
4546 if (device->depth_blt_texture)
4548 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4549 device->depth_blt_texture = 0;
4552 device->blitter->free_private(device);
4553 device->shader_backend->shader_free_private(device);
4554 destroy_dummy_textures(device, context);
4555 destroy_default_samplers(device, context);
4557 context_release(context);
4559 while (device->context_count)
4561 if (device->contexts[0]->swapchain)
4562 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4563 else
4564 context_destroy(device, device->contexts[0]);
4567 HeapFree(GetProcessHeap(), 0, swapchain->context);
4568 swapchain->context = NULL;
4571 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4573 struct wined3d_context *context;
4574 struct wined3d_texture *target;
4575 HRESULT hr;
4577 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4578 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4580 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4581 return hr;
4584 if (FAILED(hr = device->blitter->alloc_private(device)))
4586 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4587 device->shader_backend->shader_free_private(device);
4588 return hr;
4591 /* Recreate the primary swapchain's context */
4592 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4593 if (!swapchain->context)
4595 ERR("Failed to allocate memory for swapchain context array.\n");
4596 device->blitter->free_private(device);
4597 device->shader_backend->shader_free_private(device);
4598 return E_OUTOFMEMORY;
4601 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4602 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4604 WARN("Failed to create context.\n");
4605 device->blitter->free_private(device);
4606 device->shader_backend->shader_free_private(device);
4607 HeapFree(GetProcessHeap(), 0, swapchain->context);
4608 return E_FAIL;
4611 swapchain->context[0] = context;
4612 swapchain->num_contexts = 1;
4613 create_dummy_textures(device, context);
4614 create_default_samplers(device, context);
4615 context_release(context);
4617 return WINED3D_OK;
4620 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4621 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4622 wined3d_device_reset_cb callback, BOOL reset_state)
4624 struct wined3d_resource *resource, *cursor;
4625 struct wined3d_swapchain *swapchain;
4626 struct wined3d_view_desc view_desc;
4627 BOOL backbuffer_resized;
4628 HRESULT hr = WINED3D_OK;
4629 unsigned int i;
4631 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4632 device, swapchain_desc, mode, callback, reset_state);
4634 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4636 ERR("Failed to get the first implicit swapchain.\n");
4637 return WINED3DERR_INVALIDCALL;
4640 if (reset_state)
4642 if (device->logo_texture)
4644 wined3d_texture_decref(device->logo_texture);
4645 device->logo_texture = NULL;
4647 if (device->cursor_texture)
4649 wined3d_texture_decref(device->cursor_texture);
4650 device->cursor_texture = NULL;
4652 state_unbind_resources(&device->state);
4655 if (device->fb.render_targets)
4657 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4659 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4662 wined3d_device_set_depth_stencil_view(device, NULL);
4664 if (reset_state)
4666 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4668 TRACE("Enumerating resource %p.\n", resource);
4669 if (FAILED(hr = callback(resource)))
4670 return hr;
4674 TRACE("New params:\n");
4675 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4676 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4677 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4678 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4679 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4680 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4681 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4682 TRACE("device_window %p\n", swapchain_desc->device_window);
4683 TRACE("windowed %#x\n", swapchain_desc->windowed);
4684 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4685 if (swapchain_desc->enable_auto_depth_stencil)
4686 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4687 TRACE("flags %#x\n", swapchain_desc->flags);
4688 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4689 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4690 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4692 /* No special treatment of these parameters. Just store them */
4693 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4694 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4695 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4696 swapchain->desc.flags = swapchain_desc->flags;
4697 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4698 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4699 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4701 if (swapchain_desc->device_window
4702 && swapchain_desc->device_window != swapchain->desc.device_window)
4704 TRACE("Changing the device window from %p to %p.\n",
4705 swapchain->desc.device_window, swapchain_desc->device_window);
4706 swapchain->desc.device_window = swapchain_desc->device_window;
4707 swapchain->device_window = swapchain_desc->device_window;
4708 wined3d_swapchain_set_window(swapchain, NULL);
4711 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4712 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4714 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4715 || swapchain->reapply_mode || mode
4716 || (!swapchain_desc->windowed && backbuffer_resized))
4718 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4719 return hr;
4721 else if (!swapchain_desc->windowed)
4723 DWORD style = device->style;
4724 DWORD exStyle = device->exStyle;
4725 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4726 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4727 * Reset to clear up their mess. Guild Wars also loses the device during that.
4729 device->style = 0;
4730 device->exStyle = 0;
4731 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4732 swapchain_desc->backbuffer_width,
4733 swapchain_desc->backbuffer_height);
4734 device->style = style;
4735 device->exStyle = exStyle;
4738 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4739 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4740 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4741 return hr;
4743 if (device->auto_depth_stencil_view)
4745 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4746 device->auto_depth_stencil_view = NULL;
4748 if (swapchain->desc.enable_auto_depth_stencil)
4750 struct wined3d_resource_desc texture_desc;
4751 struct wined3d_texture *texture;
4752 DWORD flags = 0;
4754 TRACE("Creating the depth stencil buffer.\n");
4756 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4757 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4758 texture_desc.multisample_type = swapchain->desc.multisample_type;
4759 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4760 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4761 texture_desc.pool = WINED3D_POOL_DEFAULT;
4762 texture_desc.width = swapchain->desc.backbuffer_width;
4763 texture_desc.height = swapchain->desc.backbuffer_height;
4764 texture_desc.depth = 1;
4765 texture_desc.size = 0;
4767 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4768 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4770 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4771 device->device_parent, &texture_desc, flags, &texture)))
4773 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4774 return WINED3DERR_INVALIDCALL;
4777 view_desc.format_id = texture->resource.format->id;
4778 view_desc.flags = 0;
4779 view_desc.u.texture.level_idx = 0;
4780 view_desc.u.texture.level_count = 1;
4781 view_desc.u.texture.layer_idx = 0;
4782 view_desc.u.texture.layer_count = 1;
4783 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4784 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4785 wined3d_texture_decref(texture);
4786 if (FAILED(hr))
4788 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4789 return hr;
4792 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4795 if (device->back_buffer_view)
4797 wined3d_rendertarget_view_decref(device->back_buffer_view);
4798 device->back_buffer_view = NULL;
4800 if (swapchain->desc.backbuffer_count)
4802 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4804 view_desc.format_id = back_buffer->format->id;
4805 view_desc.flags = 0;
4806 view_desc.u.texture.level_idx = 0;
4807 view_desc.u.texture.level_count = 1;
4808 view_desc.u.texture.layer_idx = 0;
4809 view_desc.u.texture.layer_count = 1;
4810 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4811 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4813 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4814 return hr;
4818 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4820 if (reset_state)
4822 TRACE("Resetting stateblock.\n");
4823 if (device->recording)
4825 wined3d_stateblock_decref(device->recording);
4826 device->recording = NULL;
4828 wined3d_cs_emit_reset_state(device->cs);
4829 state_cleanup(&device->state);
4831 if (device->d3d_initialized)
4832 delete_opengl_contexts(device, swapchain);
4834 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4835 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4836 device->update_state = &device->state;
4838 device_init_swapchain_state(device, swapchain);
4840 else if (device->back_buffer_view)
4842 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4843 struct wined3d_state *state = &device->state;
4845 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4847 /* Note the min_z / max_z is not reset. */
4848 state->viewport.x = 0;
4849 state->viewport.y = 0;
4850 state->viewport.width = view->width;
4851 state->viewport.height = view->height;
4852 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4854 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4855 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4858 if (device->d3d_initialized)
4860 if (reset_state)
4861 hr = create_primary_opengl_context(device, swapchain);
4862 swapchain_update_swap_interval(swapchain);
4865 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4866 * first use
4868 return hr;
4871 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4873 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4875 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4877 return WINED3D_OK;
4881 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4882 struct wined3d_device_creation_parameters *parameters)
4884 TRACE("device %p, parameters %p.\n", device, parameters);
4886 *parameters = device->create_parms;
4889 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4891 TRACE("device %p.\n", device);
4893 return device->wined3d;
4896 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4897 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4899 struct wined3d_swapchain *swapchain;
4901 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4902 device, swapchain_idx, flags, ramp);
4904 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4905 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4908 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4909 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4911 struct wined3d_swapchain *swapchain;
4913 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4914 device, swapchain_idx, ramp);
4916 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4917 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4920 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4922 TRACE("device %p, resource %p.\n", device, resource);
4924 list_add_head(&device->resources, &resource->resource_list_entry);
4927 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4929 TRACE("device %p, resource %p.\n", device, resource);
4931 list_remove(&resource->resource_list_entry);
4934 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4936 enum wined3d_resource_type type = resource->type;
4937 struct wined3d_rendertarget_view *rtv;
4938 unsigned int i;
4940 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4942 if (device->d3d_initialized)
4944 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4946 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4947 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4950 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4951 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4954 switch (type)
4956 case WINED3D_RTYPE_TEXTURE_2D:
4957 case WINED3D_RTYPE_TEXTURE_3D:
4958 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4960 struct wined3d_texture *texture = texture_from_resource(resource);
4962 if (device->state.textures[i] == texture)
4964 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4965 device->state.textures[i] = NULL;
4968 if (device->recording && device->update_state->textures[i] == texture)
4970 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4971 texture, device->recording, i);
4972 device->update_state->textures[i] = NULL;
4975 break;
4977 case WINED3D_RTYPE_BUFFER:
4979 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4981 for (i = 0; i < MAX_STREAMS; ++i)
4983 if (device->state.streams[i].buffer == buffer)
4985 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4986 device->state.streams[i].buffer = NULL;
4989 if (device->recording && device->update_state->streams[i].buffer == buffer)
4991 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4992 buffer, device->recording, i);
4993 device->update_state->streams[i].buffer = NULL;
4997 if (device->state.index_buffer == buffer)
4999 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5000 device->state.index_buffer = NULL;
5003 if (device->recording && device->update_state->index_buffer == buffer)
5005 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5006 buffer, device->recording);
5007 device->update_state->index_buffer = NULL;
5010 break;
5012 default:
5013 break;
5016 /* Remove the resource from the resourceStore */
5017 device_resource_remove(device, resource);
5019 TRACE("Resource released.\n");
5022 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5024 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5026 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5029 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5030 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5031 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5033 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5034 const struct fragment_pipeline *fragment_pipeline;
5035 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5036 unsigned int i;
5037 HRESULT hr;
5039 device->ref = 1;
5040 device->wined3d = wined3d;
5041 wined3d_incref(device->wined3d);
5042 device->adapter = wined3d->adapter_count ? adapter : NULL;
5043 device->device_parent = device_parent;
5044 list_init(&device->resources);
5045 list_init(&device->shaders);
5046 device->surface_alignment = surface_alignment;
5048 /* Save the creation parameters. */
5049 device->create_parms.adapter_idx = adapter_idx;
5050 device->create_parms.device_type = device_type;
5051 device->create_parms.focus_window = focus_window;
5052 device->create_parms.flags = flags;
5054 device->shader_backend = adapter->shader_backend;
5056 vertex_pipeline = adapter->vertex_pipe;
5058 fragment_pipeline = adapter->fragment_pipe;
5060 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5062 if (vertex_pipeline->vp_states && fragment_pipeline->states
5063 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5064 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5065 fragment_pipeline, misc_state_template)))
5067 ERR("Failed to compile state table, hr %#x.\n", hr);
5068 wine_rb_destroy(&device->samplers, NULL, NULL);
5069 wined3d_decref(device->wined3d);
5070 return hr;
5073 device->blitter = adapter->blitter;
5075 state_init(&device->state, &device->fb, &adapter->gl_info,
5076 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5077 device->update_state = &device->state;
5079 if (!(device->cs = wined3d_cs_create(device)))
5081 WARN("Failed to create command stream.\n");
5082 state_cleanup(&device->state);
5083 hr = E_FAIL;
5084 goto err;
5087 return WINED3D_OK;
5089 err:
5090 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5092 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5094 wine_rb_destroy(&device->samplers, NULL, NULL);
5095 wined3d_decref(device->wined3d);
5096 return hr;
5099 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5101 DWORD rep = device->StateTable[state].representative;
5102 struct wined3d_context *context;
5103 DWORD idx;
5104 BYTE shift;
5105 UINT i;
5107 if (STATE_IS_COMPUTE(state))
5109 for (i = 0; i < device->context_count; ++i)
5110 context_invalidate_compute_state(device->contexts[i], state);
5111 return;
5114 for (i = 0; i < device->context_count; ++i)
5116 context = device->contexts[i];
5117 if(isStateDirty(context, rep)) continue;
5119 context->dirtyArray[context->numDirtyEntries++] = rep;
5120 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5121 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5122 context->isStateDirty[idx] |= (1u << shift);
5126 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5127 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5129 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5131 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5132 window, message, wparam, lparam);
5133 if (unicode)
5134 return DefWindowProcW(window, message, wparam, lparam);
5135 else
5136 return DefWindowProcA(window, message, wparam, lparam);
5139 if (message == WM_DESTROY)
5141 TRACE("unregister window %p.\n", window);
5142 wined3d_unregister_window(window);
5144 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5145 ERR("Window %p is not the focus window for device %p.\n", window, device);
5147 else if (message == WM_DISPLAYCHANGE)
5149 device->device_parent->ops->mode_changed(device->device_parent);
5151 else if (message == WM_ACTIVATEAPP)
5153 UINT i;
5155 for (i = 0; i < device->swapchain_count; i++)
5156 wined3d_swapchain_activate(device->swapchains[i], wparam);
5158 device->device_parent->ops->activate(device->device_parent, wparam);
5160 else if (message == WM_SYSCOMMAND)
5162 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5164 if (unicode)
5165 DefWindowProcW(window, message, wparam, lparam);
5166 else
5167 DefWindowProcA(window, message, wparam, lparam);
5171 if (unicode)
5172 return CallWindowProcW(proc, window, message, wparam, lparam);
5173 else
5174 return CallWindowProcA(proc, window, message, wparam, lparam);