secur32/tests: Use importlib for functions available since Windows XP.
[wine.git] / dlls / wined3d / device.c
blob3a24e2987ab64b17b4e32c1b4d000cbd55ec5ee9
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 void device_switch_onscreen_ds(struct wined3d_device *device,
202 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
204 if (device->onscreen_depth_stencil)
206 surface_load_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
208 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
209 device->onscreen_depth_stencil->ds_current_size.cx,
210 device->onscreen_depth_stencil->ds_current_size.cy);
211 wined3d_texture_decref(device->onscreen_depth_stencil->container);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_texture_incref(device->onscreen_depth_stencil->container);
217 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
219 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
220 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
222 /* partial draw rect */
223 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
224 return FALSE;
226 /* partial clear rect */
227 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
228 || clear_rect->right < width || clear_rect->bottom < height))
229 return FALSE;
231 return TRUE;
234 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
235 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
237 struct wined3d_texture_sub_resource *sub_resource = surface_get_sub_resource(ds);
238 RECT current_rect, r;
240 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
242 /* Depth buffer was discarded, make it entirely current in its new location since
243 * there is no other place where we would get data anyway. */
244 SetRect(out_rect, 0, 0,
245 wined3d_texture_get_level_width(ds->container, ds->texture_level),
246 wined3d_texture_get_level_height(ds->container, ds->texture_level));
247 return;
250 if (sub_resource->locations & location)
251 SetRect(&current_rect, 0, 0,
252 ds->ds_current_size.cx,
253 ds->ds_current_size.cy);
254 else
255 SetRectEmpty(&current_rect);
257 IntersectRect(&r, draw_rect, &current_rect);
258 if (EqualRect(&r, draw_rect))
260 /* current_rect ⊇ draw_rect, modify only. */
261 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
262 return;
265 if (EqualRect(&r, &current_rect))
267 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
269 if (!rect_count)
271 /* Full clear, modify only. */
272 *out_rect = *draw_rect;
273 return;
276 IntersectRect(&r, draw_rect, clear_rect);
277 if (EqualRect(&r, draw_rect))
279 /* clear_rect ⊇ draw_rect, modify only. */
280 *out_rect = *draw_rect;
281 return;
285 /* Full load. */
286 surface_load_location(ds, context, location);
287 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
290 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
291 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
292 float depth, DWORD stencil)
294 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
295 struct wined3d_surface *target = rtv ? wined3d_rendertarget_view_get_surface(rtv) : NULL;
296 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
297 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
298 const struct wined3d_state *state = &device->state;
299 const struct wined3d_gl_info *gl_info;
300 UINT drawable_width, drawable_height;
301 struct wined3d_color corrected_color;
302 struct wined3d_context *context;
303 GLbitfield clear_mask = 0;
304 BOOL render_offscreen;
305 unsigned int i;
306 RECT ds_rect = {0};
308 context = context_acquire(device, target);
309 if (!context->valid)
311 context_release(context);
312 WARN("Invalid context, skipping clear.\n");
313 return;
315 gl_info = context->gl_info;
317 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
318 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
319 * for the cleared parts, and the untouched parts.
321 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
322 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
323 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
324 * checking all this if the dest surface is in the drawable anyway. */
325 for (i = 0; i < rt_count; ++i)
327 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
329 if (rtv && rtv->format->id != WINED3DFMT_NULL)
331 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
333 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
334 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
335 else
336 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
340 if (target)
342 render_offscreen = context->render_offscreen;
343 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
345 else
347 render_offscreen = TRUE;
348 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
349 depth_stencil->texture_level);
350 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
351 depth_stencil->texture_level);
354 if (depth_stencil && render_offscreen)
355 wined3d_texture_prepare_location(depth_stencil->container,
356 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
358 if (flags & WINED3DCLEAR_ZBUFFER)
360 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
362 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
363 device_switch_onscreen_ds(device, context, depth_stencil);
364 prepare_ds_clear(depth_stencil, context, location,
365 draw_rect, rect_count, clear_rect, &ds_rect);
368 if (!context_apply_clear_state(context, state, rt_count, fb))
370 context_release(context);
371 WARN("Failed to apply clear state, skipping clear.\n");
372 return;
375 /* Only set the values up once, as they are not changing. */
376 if (flags & WINED3DCLEAR_STENCIL)
378 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
380 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
383 gl_info->gl_ops.gl.p_glStencilMask(~0U);
384 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
385 gl_info->gl_ops.gl.p_glClearStencil(stencil);
386 checkGLcall("glClearStencil");
387 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
390 if (flags & WINED3DCLEAR_ZBUFFER)
392 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
394 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
396 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
397 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
398 gl_info->gl_ops.gl.p_glClearDepth(depth);
399 checkGLcall("glClearDepth");
400 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
403 if (flags & WINED3DCLEAR_TARGET)
405 for (i = 0; i < rt_count; ++i)
407 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
408 struct wined3d_texture *texture;
410 if (!rtv)
411 continue;
413 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
415 FIXME("Not supported on buffer resources.\n");
416 continue;
419 texture = texture_from_resource(rtv->resource);
420 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
421 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
424 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
426 if (rt_count > 1)
427 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
428 "support, this might cause graphical issues.\n");
430 corrected_color.r = color->r < wined3d_srgb_const1[0]
431 ? color->r * wined3d_srgb_const0[3]
432 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
433 - wined3d_srgb_const0[2];
434 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
435 corrected_color.g = color->g < wined3d_srgb_const1[0]
436 ? color->g * wined3d_srgb_const0[3]
437 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
438 - wined3d_srgb_const0[2];
439 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
440 corrected_color.b = color->b < wined3d_srgb_const1[0]
441 ? color->b * wined3d_srgb_const0[3]
442 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
443 - wined3d_srgb_const0[2];
444 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
445 color = &corrected_color;
448 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
449 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
450 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
451 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
452 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
453 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
454 checkGLcall("glClearColor");
455 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
458 if (!rect_count)
460 if (render_offscreen)
462 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
463 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
465 else
467 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
468 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
470 checkGLcall("glScissor");
471 gl_info->gl_ops.gl.p_glClear(clear_mask);
472 checkGLcall("glClear");
474 else
476 RECT current_rect;
478 /* Now process each rect in turn. */
479 for (i = 0; i < rect_count; ++i)
481 /* Note that GL uses lower left, width/height. */
482 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
484 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
485 wine_dbgstr_rect(&clear_rect[i]),
486 wine_dbgstr_rect(&current_rect));
488 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
489 * The rectangle is not cleared, no error is returned, but further rectangles are
490 * still cleared if they are valid. */
491 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
493 TRACE("Rectangle with negative dimensions, ignoring.\n");
494 continue;
497 if (render_offscreen)
499 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
500 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
502 else
504 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
505 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
507 checkGLcall("glScissor");
509 gl_info->gl_ops.gl.p_glClear(clear_mask);
510 checkGLcall("glClear");
514 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
515 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
516 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
518 context_release(context);
521 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
523 ULONG refcount = InterlockedIncrement(&device->ref);
525 TRACE("%p increasing refcount to %u.\n", device, refcount);
527 return refcount;
530 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
532 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
534 ERR("Leftover sampler %p.\n", sampler);
537 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
539 ULONG refcount = InterlockedDecrement(&device->ref);
541 TRACE("%p decreasing refcount to %u.\n", device, refcount);
543 if (!refcount)
545 UINT i;
547 wined3d_cs_destroy(device->cs);
549 if (device->recording && wined3d_stateblock_decref(device->recording))
550 ERR("Something's still holding the recording stateblock.\n");
551 device->recording = NULL;
553 state_cleanup(&device->state);
555 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
557 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
558 device->multistate_funcs[i] = NULL;
561 if (!list_empty(&device->resources))
563 struct wined3d_resource *resource;
565 ERR("Device released with resources still bound.\n");
567 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
569 ERR("Leftover resource %p with type %s (%#x).\n",
570 resource, debug_d3dresourcetype(resource->type), resource->type);
574 if (device->contexts)
575 ERR("Context array not freed!\n");
576 if (device->hardwareCursor)
577 DestroyCursor(device->hardwareCursor);
578 device->hardwareCursor = 0;
580 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
582 wined3d_decref(device->wined3d);
583 device->wined3d = NULL;
584 HeapFree(GetProcessHeap(), 0, device);
585 TRACE("Freed device %p.\n", device);
588 return refcount;
591 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
593 TRACE("device %p.\n", device);
595 return device->swapchain_count;
598 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
600 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
602 if (swapchain_idx >= device->swapchain_count)
604 WARN("swapchain_idx %u >= swapchain_count %u.\n",
605 swapchain_idx, device->swapchain_count);
606 return NULL;
609 return device->swapchains[swapchain_idx];
612 static void device_load_logo(struct wined3d_device *device, const char *filename)
614 struct wined3d_color_key color_key;
615 struct wined3d_resource_desc desc;
616 HBITMAP hbm;
617 BITMAP bm;
618 HRESULT hr;
619 HDC dcb = NULL, dcs = NULL;
621 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
622 if(hbm)
624 GetObjectA(hbm, sizeof(BITMAP), &bm);
625 dcb = CreateCompatibleDC(NULL);
626 if(!dcb) goto out;
627 SelectObject(dcb, hbm);
629 else
631 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
632 * couldn't be loaded
634 memset(&bm, 0, sizeof(bm));
635 bm.bmWidth = 32;
636 bm.bmHeight = 32;
639 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
640 desc.format = WINED3DFMT_B5G6R5_UNORM;
641 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
642 desc.multisample_quality = 0;
643 desc.usage = WINED3DUSAGE_DYNAMIC;
644 desc.pool = WINED3D_POOL_DEFAULT;
645 desc.width = bm.bmWidth;
646 desc.height = bm.bmHeight;
647 desc.depth = 1;
648 desc.size = 0;
649 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
650 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
652 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
653 goto out;
656 if (dcb)
658 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
659 goto out;
660 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
661 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
663 color_key.color_space_low_value = 0;
664 color_key.color_space_high_value = 0;
665 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
667 else
669 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
670 const RECT rect = {0, 0, desc.width, desc.height};
671 struct wined3d_surface *surface;
673 /* Fill the surface with a white color to show that wined3d is there */
674 surface = device->logo_texture->sub_resources[0].u.surface;
675 surface_color_fill(surface, &rect, &c);
678 out:
679 if (dcb) DeleteDC(dcb);
680 if (hbm) DeleteObject(hbm);
683 /* Context activation is done by the caller. */
684 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
686 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
687 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
688 unsigned int i;
689 DWORD color;
691 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
692 color = 0x000000ff;
693 else
694 color = 0x00000000;
696 /* Under DirectX you can sample even if no texture is bound, whereas
697 * OpenGL will only allow that when a valid texture is bound.
698 * We emulate this by creating dummy textures and binding them
699 * to each texture stage when the currently set D3D texture is NULL. */
700 context_active_texture(context, gl_info, 0);
702 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d);
703 checkGLcall("glGenTextures");
704 TRACE("Dummy 2D texture given name %u.\n", device->dummy_textures.tex_2d);
706 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
707 checkGLcall("glBindTexture");
709 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
710 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
711 checkGLcall("glTexImage2D");
713 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
715 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_rect);
716 checkGLcall("glGenTextures");
717 TRACE("Dummy rectangle texture given name %u.\n", device->dummy_textures.tex_rect);
719 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
720 checkGLcall("glBindTexture");
722 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
723 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
724 checkGLcall("glTexImage2D");
727 if (gl_info->supported[EXT_TEXTURE3D])
729 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_3d);
730 checkGLcall("glGenTextures");
731 TRACE("Dummy 3D texture given name %u.\n", device->dummy_textures.tex_3d);
733 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
734 checkGLcall("glBindTexture");
736 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
737 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
738 checkGLcall("glTexImage3D");
741 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
743 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube);
744 checkGLcall("glGenTextures");
745 TRACE("Dummy cube texture given name %u.\n", device->dummy_textures.tex_cube);
747 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
748 checkGLcall("glBindTexture");
750 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
752 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
753 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
754 checkGLcall("glTexImage2D");
758 if (gl_info->supported[EXT_TEXTURE_ARRAY])
760 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d_array);
761 checkGLcall("glGenTextures");
762 TRACE("Dummy 2D array texture given name %u.\n", device->dummy_textures.tex_2d_array);
764 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
765 checkGLcall("glBindTexture");
767 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
768 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
769 checkGLcall("glTexImage3D");
772 context_bind_dummy_textures(device, context);
775 /* Context activation is done by the caller. */
776 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
778 const struct wined3d_gl_info *gl_info = context->gl_info;
780 if (gl_info->supported[EXT_TEXTURE_ARRAY])
781 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d_array);
783 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
784 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube);
786 if (gl_info->supported[EXT_TEXTURE3D])
787 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_3d);
789 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
790 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_rect);
792 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d);
794 checkGLcall("Delete dummy textures");
796 memset(&device->dummy_textures, 0, sizeof(device->dummy_textures));
799 /* Context activation is done by the caller. */
800 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
802 const struct wined3d_gl_info *gl_info = context->gl_info;
804 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
806 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
807 * instructions allow access to resources without using samplers.
808 * In GLSL, resources are always accessed through sampler or image variables. The default
809 * sampler object is used to emulate the direct resource access when there is no sampler state
810 * to use.
812 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
813 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
814 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
815 checkGLcall("Create default sampler");
817 /* In D3D10+, a NULL sampler maps to the default sampler state. */
818 GL_EXTCALL(glGenSamplers(1, &device->null_sampler));
819 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
820 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
821 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
822 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
823 checkGLcall("Create null sampler");
825 else
827 device->default_sampler = 0;
828 device->null_sampler = 0;
832 /* Context activation is done by the caller. */
833 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
835 const struct wined3d_gl_info *gl_info = context->gl_info;
837 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
839 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
840 GL_EXTCALL(glDeleteSamplers(1, &device->null_sampler));
841 checkGLcall("glDeleteSamplers");
844 device->default_sampler = 0;
845 device->null_sampler = 0;
848 static LONG fullscreen_style(LONG style)
850 /* Make sure the window is managed, otherwise we won't get keyboard input. */
851 style |= WS_POPUP | WS_SYSMENU;
852 style &= ~(WS_CAPTION | WS_THICKFRAME);
854 return style;
857 static LONG fullscreen_exstyle(LONG exstyle)
859 /* Filter out window decorations. */
860 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
862 return exstyle;
865 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
867 BOOL filter_messages;
868 LONG style, exstyle;
870 TRACE("Setting up window %p for fullscreen mode.\n", window);
872 if (device->style || device->exStyle)
874 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
875 window, device->style, device->exStyle);
878 device->style = GetWindowLongW(window, GWL_STYLE);
879 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
881 style = fullscreen_style(device->style);
882 exstyle = fullscreen_exstyle(device->exStyle);
884 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
885 device->style, device->exStyle, style, exstyle);
887 filter_messages = device->filter_messages;
888 device->filter_messages = TRUE;
890 SetWindowLongW(window, GWL_STYLE, style);
891 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
892 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
894 device->filter_messages = filter_messages;
897 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
898 const RECT *window_rect)
900 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
901 BOOL filter_messages;
902 LONG style, exstyle;
903 RECT rect = {0};
905 if (!device->style && !device->exStyle)
906 return;
908 style = GetWindowLongW(window, GWL_STYLE);
909 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
911 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
912 * application, and we want to ignore them in the test below, since it's
913 * not the application's fault that they changed. Additionally, we want to
914 * preserve the current status of these flags (i.e. don't restore them) to
915 * more closely emulate the behavior of Direct3D, which leaves these flags
916 * alone when returning to windowed mode. */
917 device->style ^= (device->style ^ style) & WS_VISIBLE;
918 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
920 TRACE("Restoring window style of window %p to %08x, %08x.\n",
921 window, device->style, device->exStyle);
923 filter_messages = device->filter_messages;
924 device->filter_messages = TRUE;
926 /* Only restore the style if the application didn't modify it during the
927 * fullscreen phase. Some applications change it before calling Reset()
928 * when switching between windowed and fullscreen modes (HL2), some
929 * depend on the original style (Eve Online). */
930 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
932 SetWindowLongW(window, GWL_STYLE, device->style);
933 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
936 if (window_rect)
937 rect = *window_rect;
938 else
939 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
940 SetWindowPos(window, 0, rect.left, rect.top,
941 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
943 device->filter_messages = filter_messages;
945 /* Delete the old values. */
946 device->style = 0;
947 device->exStyle = 0;
950 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
952 TRACE("device %p, window %p.\n", device, window);
954 if (!wined3d_register_window(window, device))
956 ERR("Failed to register window %p.\n", window);
957 return E_FAIL;
960 InterlockedExchangePointer((void **)&device->focus_window, window);
961 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
963 return WINED3D_OK;
966 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
968 TRACE("device %p.\n", device);
970 if (device->focus_window) wined3d_unregister_window(device->focus_window);
971 InterlockedExchangePointer((void **)&device->focus_window, NULL);
974 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
976 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
977 unsigned int i;
979 if (device->fb.render_targets)
981 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
983 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
985 if (device->back_buffer_view)
986 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
989 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
990 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
993 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
994 struct wined3d_swapchain_desc *swapchain_desc)
996 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
997 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
998 struct wined3d_swapchain *swapchain = NULL;
999 struct wined3d_context *context;
1000 DWORD clear_flags = 0;
1001 HRESULT hr;
1003 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1005 if (device->d3d_initialized)
1006 return WINED3DERR_INVALIDCALL;
1007 if (device->wined3d->flags & WINED3D_NO3D)
1008 return WINED3DERR_INVALIDCALL;
1010 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
1011 return E_OUTOFMEMORY;
1013 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1014 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1016 TRACE("Shader private data couldn't be allocated\n");
1017 goto err_out;
1019 if (FAILED(hr = device->blitter->alloc_private(device)))
1021 TRACE("Blitter private data couldn't be allocated\n");
1022 goto err_out;
1025 /* Setup the implicit swapchain. This also initializes a context. */
1026 TRACE("Creating implicit swapchain\n");
1027 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1028 swapchain_desc, &swapchain);
1029 if (FAILED(hr))
1031 WARN("Failed to create implicit swapchain\n");
1032 goto err_out;
1035 if (swapchain_desc->backbuffer_count)
1037 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1038 struct wined3d_view_desc view_desc;
1040 view_desc.format_id = back_buffer->format->id;
1041 view_desc.flags = 0;
1042 view_desc.u.texture.level_idx = 0;
1043 view_desc.u.texture.level_count = 1;
1044 view_desc.u.texture.layer_idx = 0;
1045 view_desc.u.texture.layer_count = 1;
1046 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1047 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1049 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1050 goto err_out;
1054 device->swapchain_count = 1;
1055 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1057 ERR("Out of memory!\n");
1058 goto err_out;
1060 device->swapchains[0] = swapchain;
1061 device_init_swapchain_state(device, swapchain);
1063 context = context_acquire(device, NULL);
1065 create_dummy_textures(device, context);
1066 create_default_samplers(device, context);
1068 device->contexts[0]->last_was_rhw = 0;
1070 TRACE("All defaults now set up, leaving 3D init.\n");
1072 context_release(context);
1074 /* Clear the screen */
1075 if (swapchain->back_buffers && swapchain->back_buffers[0])
1076 clear_flags |= WINED3DCLEAR_TARGET;
1077 if (swapchain_desc->enable_auto_depth_stencil)
1078 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1079 if (clear_flags)
1080 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1082 device->d3d_initialized = TRUE;
1084 if (wined3d_settings.logo)
1085 device_load_logo(device, wined3d_settings.logo);
1086 return WINED3D_OK;
1088 err_out:
1089 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1090 HeapFree(GetProcessHeap(), 0, device->swapchains);
1091 device->swapchain_count = 0;
1092 if (device->back_buffer_view)
1093 wined3d_rendertarget_view_decref(device->back_buffer_view);
1094 if (swapchain)
1095 wined3d_swapchain_decref(swapchain);
1096 if (device->blit_priv)
1097 device->blitter->free_private(device);
1098 if (device->shader_priv)
1099 device->shader_backend->shader_free_private(device);
1101 return hr;
1104 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1105 struct wined3d_swapchain_desc *swapchain_desc)
1107 struct wined3d_swapchain *swapchain = NULL;
1108 HRESULT hr;
1110 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1112 /* Setup the implicit swapchain */
1113 TRACE("Creating implicit swapchain\n");
1114 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1115 swapchain_desc, &swapchain);
1116 if (FAILED(hr))
1118 WARN("Failed to create implicit swapchain\n");
1119 goto err_out;
1122 device->swapchain_count = 1;
1123 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1125 ERR("Out of memory!\n");
1126 goto err_out;
1128 device->swapchains[0] = swapchain;
1129 return WINED3D_OK;
1131 err_out:
1132 wined3d_swapchain_decref(swapchain);
1133 return hr;
1136 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1138 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1140 wined3d_sampler_decref(sampler);
1143 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1145 struct wined3d_resource *resource, *cursor;
1146 const struct wined3d_gl_info *gl_info;
1147 struct wined3d_context *context;
1148 struct wined3d_surface *surface;
1149 UINT i;
1151 TRACE("device %p.\n", device);
1153 if (!device->d3d_initialized)
1154 return WINED3DERR_INVALIDCALL;
1156 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1157 * it was created. Thus make sure a context is active for the glDelete* calls
1159 context = context_acquire(device, NULL);
1160 gl_info = context->gl_info;
1162 if (device->logo_texture)
1163 wined3d_texture_decref(device->logo_texture);
1164 if (device->cursor_texture)
1165 wined3d_texture_decref(device->cursor_texture);
1167 state_unbind_resources(&device->state);
1169 /* Unload resources */
1170 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1172 TRACE("Unloading resource %p.\n", resource);
1173 wined3d_cs_emit_unload_resource(device->cs, resource);
1176 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1178 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1179 * private data, it might contain opengl pointers
1181 if (device->depth_blt_texture)
1183 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1184 device->depth_blt_texture = 0;
1187 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1188 device->blitter->free_private(device);
1189 device->shader_backend->shader_free_private(device);
1190 destroy_dummy_textures(device, context);
1191 destroy_default_samplers(device, context);
1193 context_release(context);
1195 /* Release the buffers (with sanity checks) */
1196 if (device->onscreen_depth_stencil)
1198 surface = device->onscreen_depth_stencil;
1199 device->onscreen_depth_stencil = NULL;
1200 wined3d_texture_decref(surface->container);
1203 if (device->fb.depth_stencil)
1205 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1207 TRACE("Releasing depth/stencil view %p.\n", view);
1209 device->fb.depth_stencil = NULL;
1210 wined3d_rendertarget_view_decref(view);
1213 if (device->auto_depth_stencil_view)
1215 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1217 device->auto_depth_stencil_view = NULL;
1218 if (wined3d_rendertarget_view_decref(view))
1219 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1222 for (i = 0; i < gl_info->limits.buffers; ++i)
1224 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1226 if (device->back_buffer_view)
1228 wined3d_rendertarget_view_decref(device->back_buffer_view);
1229 device->back_buffer_view = NULL;
1232 for (i = 0; i < device->swapchain_count; ++i)
1234 TRACE("Releasing the implicit swapchain %u.\n", i);
1235 if (wined3d_swapchain_decref(device->swapchains[i]))
1236 FIXME("Something's still holding the implicit swapchain.\n");
1239 HeapFree(GetProcessHeap(), 0, device->swapchains);
1240 device->swapchains = NULL;
1241 device->swapchain_count = 0;
1243 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1244 device->fb.render_targets = NULL;
1246 device->d3d_initialized = FALSE;
1248 return WINED3D_OK;
1251 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1253 unsigned int i;
1255 for (i = 0; i < device->swapchain_count; ++i)
1257 TRACE("Releasing the implicit swapchain %u.\n", i);
1258 if (wined3d_swapchain_decref(device->swapchains[i]))
1259 FIXME("Something's still holding the implicit swapchain.\n");
1262 HeapFree(GetProcessHeap(), 0, device->swapchains);
1263 device->swapchains = NULL;
1264 device->swapchain_count = 0;
1265 return WINED3D_OK;
1268 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1269 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1270 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1272 * There is no way to deactivate thread safety once it is enabled.
1274 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1276 TRACE("device %p.\n", device);
1278 /* For now just store the flag (needed in case of ddraw). */
1279 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1282 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1284 TRACE("device %p.\n", device);
1286 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1287 wine_dbgstr_longlong(device->adapter->vram_bytes),
1288 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1289 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1291 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1294 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1295 struct wined3d_buffer *buffer, UINT offset)
1297 struct wined3d_stream_output *stream;
1298 struct wined3d_buffer *prev_buffer;
1300 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1302 if (idx >= MAX_STREAM_OUT)
1304 WARN("Invalid stream output %u.\n", idx);
1305 return;
1308 stream = &device->update_state->stream_output[idx];
1309 prev_buffer = stream->buffer;
1311 if (buffer)
1312 wined3d_buffer_incref(buffer);
1313 stream->buffer = buffer;
1314 stream->offset = offset;
1315 if (!device->recording)
1316 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1317 if (prev_buffer)
1318 wined3d_buffer_decref(prev_buffer);
1321 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1322 UINT idx, UINT *offset)
1324 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1326 if (idx >= MAX_STREAM_OUT)
1328 WARN("Invalid stream output %u.\n", idx);
1329 return NULL;
1332 if (offset)
1333 *offset = device->state.stream_output[idx].offset;
1334 return device->state.stream_output[idx].buffer;
1337 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1338 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1340 struct wined3d_stream_state *stream;
1341 struct wined3d_buffer *prev_buffer;
1343 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1344 device, stream_idx, buffer, offset, stride);
1346 if (stream_idx >= MAX_STREAMS)
1348 WARN("Stream index %u out of range.\n", stream_idx);
1349 return WINED3DERR_INVALIDCALL;
1351 else if (offset & 0x3)
1353 WARN("Offset %u is not 4 byte aligned.\n", offset);
1354 return WINED3DERR_INVALIDCALL;
1357 stream = &device->update_state->streams[stream_idx];
1358 prev_buffer = stream->buffer;
1360 if (device->recording)
1361 device->recording->changed.streamSource |= 1u << stream_idx;
1363 if (prev_buffer == buffer
1364 && stream->stride == stride
1365 && stream->offset == offset)
1367 TRACE("Application is setting the old values over, nothing to do.\n");
1368 return WINED3D_OK;
1371 stream->buffer = buffer;
1372 if (buffer)
1374 stream->stride = stride;
1375 stream->offset = offset;
1376 wined3d_buffer_incref(buffer);
1379 if (!device->recording)
1380 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1381 if (prev_buffer)
1382 wined3d_buffer_decref(prev_buffer);
1384 return WINED3D_OK;
1387 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1388 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1390 const struct wined3d_stream_state *stream;
1392 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1393 device, stream_idx, buffer, offset, stride);
1395 if (stream_idx >= MAX_STREAMS)
1397 WARN("Stream index %u out of range.\n", stream_idx);
1398 return WINED3DERR_INVALIDCALL;
1401 stream = &device->state.streams[stream_idx];
1402 *buffer = stream->buffer;
1403 if (offset)
1404 *offset = stream->offset;
1405 *stride = stream->stride;
1407 return WINED3D_OK;
1410 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1412 struct wined3d_stream_state *stream;
1413 UINT old_flags, old_freq;
1415 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1417 /* Verify input. At least in d3d9 this is invalid. */
1418 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1420 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1421 return WINED3DERR_INVALIDCALL;
1423 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1425 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1426 return WINED3DERR_INVALIDCALL;
1428 if (!divider)
1430 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1431 return WINED3DERR_INVALIDCALL;
1434 stream = &device->update_state->streams[stream_idx];
1435 old_flags = stream->flags;
1436 old_freq = stream->frequency;
1438 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1439 stream->frequency = divider & 0x7fffff;
1441 if (device->recording)
1442 device->recording->changed.streamFreq |= 1u << stream_idx;
1443 else if (stream->frequency != old_freq || stream->flags != old_flags)
1444 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1446 return WINED3D_OK;
1449 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1450 UINT stream_idx, UINT *divider)
1452 const struct wined3d_stream_state *stream;
1454 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1456 stream = &device->state.streams[stream_idx];
1457 *divider = stream->flags | stream->frequency;
1459 TRACE("Returning %#x.\n", *divider);
1461 return WINED3D_OK;
1464 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1465 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1467 TRACE("device %p, state %s, matrix %p.\n",
1468 device, debug_d3dtstype(d3dts), matrix);
1469 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1470 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1471 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1472 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1474 /* Handle recording of state blocks. */
1475 if (device->recording)
1477 TRACE("Recording... not performing anything.\n");
1478 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1479 device->update_state->transforms[d3dts] = *matrix;
1480 return;
1483 /* If the new matrix is the same as the current one,
1484 * we cut off any further processing. this seems to be a reasonable
1485 * optimization because as was noticed, some apps (warcraft3 for example)
1486 * tend towards setting the same matrix repeatedly for some reason.
1488 * From here on we assume that the new matrix is different, wherever it matters. */
1489 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1491 TRACE("The application is setting the same matrix over again.\n");
1492 return;
1495 device->state.transforms[d3dts] = *matrix;
1496 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1499 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1500 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1502 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1504 *matrix = device->state.transforms[state];
1507 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1508 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1510 const struct wined3d_matrix *mat;
1511 struct wined3d_matrix temp;
1513 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1515 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1516 * below means it will be recorded in a state block change, but it
1517 * works regardless where it is recorded.
1518 * If this is found to be wrong, change to StateBlock. */
1519 if (state > HIGHEST_TRANSFORMSTATE)
1521 WARN("Unhandled transform state %#x.\n", state);
1522 return;
1525 mat = &device->update_state->transforms[state];
1526 multiply_matrix(&temp, mat, matrix);
1528 /* Apply change via set transform - will reapply to eg. lights this way. */
1529 wined3d_device_set_transform(device, state, &temp);
1532 /* Note lights are real special cases. Although the device caps state only
1533 * e.g. 8 are supported, you can reference any indexes you want as long as
1534 * that number max are enabled at any one point in time. Therefore since the
1535 * indices can be anything, we need a hashmap of them. However, this causes
1536 * stateblock problems. When capturing the state block, I duplicate the
1537 * hashmap, but when recording, just build a chain pretty much of commands to
1538 * be replayed. */
1539 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1540 UINT light_idx, const struct wined3d_light *light)
1542 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1543 struct wined3d_light_info *object = NULL;
1544 struct list *e;
1545 float rho;
1547 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1549 /* Check the parameter range. Need for speed most wanted sets junk lights
1550 * which confuse the GL driver. */
1551 if (!light)
1552 return WINED3DERR_INVALIDCALL;
1554 switch (light->type)
1556 case WINED3D_LIGHT_POINT:
1557 case WINED3D_LIGHT_SPOT:
1558 case WINED3D_LIGHT_GLSPOT:
1559 /* Incorrect attenuation values can cause the gl driver to crash.
1560 * Happens with Need for speed most wanted. */
1561 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1563 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1564 return WINED3DERR_INVALIDCALL;
1566 break;
1568 case WINED3D_LIGHT_DIRECTIONAL:
1569 case WINED3D_LIGHT_PARALLELPOINT:
1570 /* Ignores attenuation */
1571 break;
1573 default:
1574 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1575 return WINED3DERR_INVALIDCALL;
1578 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1580 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1581 if (object->OriginalIndex == light_idx)
1582 break;
1583 object = NULL;
1586 if (!object)
1588 TRACE("Adding new light\n");
1589 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1590 if (!object)
1591 return E_OUTOFMEMORY;
1593 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1594 object->glIndex = -1;
1595 object->OriginalIndex = light_idx;
1598 /* Initialize the object. */
1599 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1600 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1601 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1602 light_idx, light->type, debug_color(&light->diffuse),
1603 debug_color(&light->specular), debug_color(&light->ambient),
1604 light->position.x, light->position.y, light->position.z,
1605 light->direction.x, light->direction.y, light->direction.z,
1606 light->range, light->falloff, light->theta, light->phi);
1608 /* Update the live definitions if the light is currently assigned a glIndex. */
1609 if (object->glIndex != -1 && !device->recording)
1611 if (object->OriginalParms.type != light->type)
1612 device_invalidate_state(device, STATE_LIGHT_TYPE);
1613 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1616 /* Save away the information. */
1617 object->OriginalParms = *light;
1619 switch (light->type)
1621 case WINED3D_LIGHT_POINT:
1622 /* Position */
1623 object->position.x = light->position.x;
1624 object->position.y = light->position.y;
1625 object->position.z = light->position.z;
1626 object->position.w = 1.0f;
1627 object->cutoff = 180.0f;
1628 /* FIXME: Range */
1629 break;
1631 case WINED3D_LIGHT_DIRECTIONAL:
1632 /* Direction */
1633 object->direction.x = -light->direction.x;
1634 object->direction.y = -light->direction.y;
1635 object->direction.z = -light->direction.z;
1636 object->direction.w = 0.0f;
1637 object->exponent = 0.0f;
1638 object->cutoff = 180.0f;
1639 break;
1641 case WINED3D_LIGHT_SPOT:
1642 /* Position */
1643 object->position.x = light->position.x;
1644 object->position.y = light->position.y;
1645 object->position.z = light->position.z;
1646 object->position.w = 1.0f;
1648 /* Direction */
1649 object->direction.x = light->direction.x;
1650 object->direction.y = light->direction.y;
1651 object->direction.z = light->direction.z;
1652 object->direction.w = 0.0f;
1654 /* opengl-ish and d3d-ish spot lights use too different models
1655 * for the light "intensity" as a function of the angle towards
1656 * the main light direction, so we only can approximate very
1657 * roughly. However, spot lights are rather rarely used in games
1658 * (if ever used at all). Furthermore if still used, probably
1659 * nobody pays attention to such details. */
1660 if (!light->falloff)
1662 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1663 * equations have the falloff resp. exponent parameter as an
1664 * exponent, so the spot light lighting will always be 1.0 for
1665 * both of them, and we don't have to care for the rest of the
1666 * rather complex calculation. */
1667 object->exponent = 0.0f;
1669 else
1671 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1672 if (rho < 0.0001f)
1673 rho = 0.0001f;
1674 object->exponent = -0.3f / logf(cosf(rho / 2));
1677 if (object->exponent > 128.0f)
1678 object->exponent = 128.0f;
1680 object->cutoff = (float)(light->phi * 90 / M_PI);
1681 /* FIXME: Range */
1682 break;
1684 case WINED3D_LIGHT_PARALLELPOINT:
1685 object->position.x = light->position.x;
1686 object->position.y = light->position.y;
1687 object->position.z = light->position.z;
1688 object->position.w = 1.0f;
1689 break;
1691 default:
1692 FIXME("Unrecognized light type %#x.\n", light->type);
1695 return WINED3D_OK;
1698 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1699 UINT light_idx, struct wined3d_light *light)
1701 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1702 struct wined3d_light_info *light_info = NULL;
1703 struct list *e;
1705 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1707 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1709 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1710 if (light_info->OriginalIndex == light_idx)
1711 break;
1712 light_info = NULL;
1715 if (!light_info)
1717 TRACE("Light information requested but light not defined\n");
1718 return WINED3DERR_INVALIDCALL;
1721 *light = light_info->OriginalParms;
1722 return WINED3D_OK;
1725 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1727 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1728 struct wined3d_light_info *light_info = NULL;
1729 struct list *e;
1731 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1733 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1735 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1736 if (light_info->OriginalIndex == light_idx)
1737 break;
1738 light_info = NULL;
1740 TRACE("Found light %p.\n", light_info);
1742 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1743 if (!light_info)
1745 TRACE("Light enabled requested but light not defined, so defining one!\n");
1746 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1748 /* Search for it again! Should be fairly quick as near head of list. */
1749 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1751 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1752 if (light_info->OriginalIndex == light_idx)
1753 break;
1754 light_info = NULL;
1756 if (!light_info)
1758 FIXME("Adding default lights has failed dismally\n");
1759 return WINED3DERR_INVALIDCALL;
1763 if (!enable)
1765 if (light_info->glIndex != -1)
1767 if (!device->recording)
1769 device_invalidate_state(device, STATE_LIGHT_TYPE);
1770 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1773 device->update_state->lights[light_info->glIndex] = NULL;
1774 light_info->glIndex = -1;
1776 else
1778 TRACE("Light already disabled, nothing to do\n");
1780 light_info->enabled = FALSE;
1782 else
1784 light_info->enabled = TRUE;
1785 if (light_info->glIndex != -1)
1787 TRACE("Nothing to do as light was enabled\n");
1789 else
1791 unsigned int i;
1792 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1793 /* Find a free GL light. */
1794 for (i = 0; i < gl_info->limits.lights; ++i)
1796 if (!device->update_state->lights[i])
1798 device->update_state->lights[i] = light_info;
1799 light_info->glIndex = i;
1800 break;
1803 if (light_info->glIndex == -1)
1805 /* Our tests show that Windows returns D3D_OK in this situation, even with
1806 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1807 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1808 * as well for those lights.
1810 * TODO: Test how this affects rendering. */
1811 WARN("Too many concurrently active lights\n");
1812 return WINED3D_OK;
1815 /* i == light_info->glIndex */
1816 if (!device->recording)
1818 device_invalidate_state(device, STATE_LIGHT_TYPE);
1819 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1824 return WINED3D_OK;
1827 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1829 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1830 struct wined3d_light_info *light_info = NULL;
1831 struct list *e;
1833 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1835 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1837 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1838 if (light_info->OriginalIndex == light_idx)
1839 break;
1840 light_info = NULL;
1843 if (!light_info)
1845 TRACE("Light enabled state requested but light not defined.\n");
1846 return WINED3DERR_INVALIDCALL;
1848 /* true is 128 according to SetLightEnable */
1849 *enable = light_info->enabled ? 128 : 0;
1850 return WINED3D_OK;
1853 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1854 UINT plane_idx, const struct wined3d_vec4 *plane)
1856 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1858 /* Validate plane_idx. */
1859 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1861 TRACE("Application has requested clipplane this device doesn't support.\n");
1862 return WINED3DERR_INVALIDCALL;
1865 if (device->recording)
1866 device->recording->changed.clipplane |= 1u << plane_idx;
1868 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1870 TRACE("Application is setting old values over, nothing to do.\n");
1871 return WINED3D_OK;
1874 device->update_state->clip_planes[plane_idx] = *plane;
1876 if (!device->recording)
1877 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1879 return WINED3D_OK;
1882 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1883 UINT plane_idx, struct wined3d_vec4 *plane)
1885 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1887 /* Validate plane_idx. */
1888 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1890 TRACE("Application has requested clipplane this device doesn't support.\n");
1891 return WINED3DERR_INVALIDCALL;
1894 *plane = device->state.clip_planes[plane_idx];
1896 return WINED3D_OK;
1899 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1900 const struct wined3d_clip_status *clip_status)
1902 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1904 if (!clip_status)
1905 return WINED3DERR_INVALIDCALL;
1907 return WINED3D_OK;
1910 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1911 struct wined3d_clip_status *clip_status)
1913 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1915 if (!clip_status)
1916 return WINED3DERR_INVALIDCALL;
1918 return WINED3D_OK;
1921 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1923 TRACE("device %p, material %p.\n", device, material);
1925 device->update_state->material = *material;
1927 if (device->recording)
1928 device->recording->changed.material = TRUE;
1929 else
1930 wined3d_cs_emit_set_material(device->cs, material);
1933 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1935 TRACE("device %p, material %p.\n", device, material);
1937 *material = device->state.material;
1939 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1940 TRACE("ambient %s\n", debug_color(&material->ambient));
1941 TRACE("specular %s\n", debug_color(&material->specular));
1942 TRACE("emissive %s\n", debug_color(&material->emissive));
1943 TRACE("power %.8e.\n", material->power);
1946 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1947 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1949 enum wined3d_format_id prev_format;
1950 struct wined3d_buffer *prev_buffer;
1951 unsigned int prev_offset;
1953 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1954 device, buffer, debug_d3dformat(format_id), offset);
1956 prev_buffer = device->update_state->index_buffer;
1957 prev_format = device->update_state->index_format;
1958 prev_offset = device->update_state->index_offset;
1960 device->update_state->index_buffer = buffer;
1961 device->update_state->index_format = format_id;
1962 device->update_state->index_offset = offset;
1964 if (device->recording)
1965 device->recording->changed.indices = TRUE;
1967 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1968 return;
1970 if (buffer)
1971 wined3d_buffer_incref(buffer);
1972 if (!device->recording)
1973 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1974 if (prev_buffer)
1975 wined3d_buffer_decref(prev_buffer);
1978 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1979 enum wined3d_format_id *format, unsigned int *offset)
1981 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1983 *format = device->state.index_format;
1984 if (offset)
1985 *offset = device->state.index_offset;
1986 return device->state.index_buffer;
1989 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1991 TRACE("device %p, base_index %d.\n", device, base_index);
1993 device->update_state->base_vertex_index = base_index;
1996 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1998 TRACE("device %p.\n", device);
2000 return device->state.base_vertex_index;
2003 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2005 TRACE("device %p, viewport %p.\n", device, viewport);
2006 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2007 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2009 device->update_state->viewport = *viewport;
2011 /* Handle recording of state blocks */
2012 if (device->recording)
2014 TRACE("Recording... not performing anything\n");
2015 device->recording->changed.viewport = TRUE;
2016 return;
2019 wined3d_cs_emit_set_viewport(device->cs, viewport);
2022 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2024 TRACE("device %p, viewport %p.\n", device, viewport);
2026 *viewport = device->state.viewport;
2029 static void resolve_depth_buffer(struct wined3d_state *state)
2031 struct wined3d_texture *dst_texture = state->textures[0];
2032 struct wined3d_rendertarget_view *src_view;
2033 RECT src_rect, dst_rect;
2035 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2036 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2037 return;
2039 if (!(src_view = state->fb->depth_stencil))
2040 return;
2041 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2043 FIXME("Not supported on buffer resources.\n");
2044 return;
2047 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2048 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2049 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
2050 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2053 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2054 struct wined3d_rasterizer_state *rasterizer_state)
2056 struct wined3d_rasterizer_state *prev;
2058 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2060 prev = device->update_state->rasterizer_state;
2061 if (prev == rasterizer_state)
2062 return;
2064 if (rasterizer_state)
2065 wined3d_rasterizer_state_incref(rasterizer_state);
2066 device->update_state->rasterizer_state = rasterizer_state;
2067 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2068 if (prev)
2069 wined3d_rasterizer_state_decref(prev);
2072 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2074 TRACE("device %p.\n", device);
2076 return device->state.rasterizer_state;
2079 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2080 enum wined3d_render_state state, DWORD value)
2082 DWORD old_value;
2084 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2086 if (state > WINEHIGHEST_RENDER_STATE)
2088 WARN("Unhandled render state %#x.\n", state);
2089 return;
2092 old_value = device->state.render_states[state];
2093 device->update_state->render_states[state] = value;
2095 /* Handle recording of state blocks. */
2096 if (device->recording)
2098 TRACE("Recording... not performing anything.\n");
2099 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2100 return;
2103 /* Compared here and not before the assignment to allow proper stateblock recording. */
2104 if (value == old_value)
2105 TRACE("Application is setting the old value over, nothing to do.\n");
2106 else
2107 wined3d_cs_emit_set_render_state(device->cs, state, value);
2109 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2111 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2112 resolve_depth_buffer(&device->state);
2116 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2118 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2120 return device->state.render_states[state];
2123 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2124 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2126 DWORD old_value;
2128 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2129 device, sampler_idx, debug_d3dsamplerstate(state), value);
2131 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2132 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2134 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2136 WARN("Invalid sampler %u.\n", sampler_idx);
2137 return; /* Windows accepts overflowing this array ... we do not. */
2140 old_value = device->state.sampler_states[sampler_idx][state];
2141 device->update_state->sampler_states[sampler_idx][state] = value;
2143 /* Handle recording of state blocks. */
2144 if (device->recording)
2146 TRACE("Recording... not performing anything.\n");
2147 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2148 return;
2151 if (old_value == value)
2153 TRACE("Application is setting the old value over, nothing to do.\n");
2154 return;
2157 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2160 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2161 UINT sampler_idx, enum wined3d_sampler_state state)
2163 TRACE("device %p, sampler_idx %u, state %s.\n",
2164 device, sampler_idx, debug_d3dsamplerstate(state));
2166 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2167 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2169 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2171 WARN("Invalid sampler %u.\n", sampler_idx);
2172 return 0; /* Windows accepts overflowing this array ... we do not. */
2175 return device->state.sampler_states[sampler_idx][state];
2178 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2180 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2182 if (device->recording)
2183 device->recording->changed.scissorRect = TRUE;
2185 if (EqualRect(&device->update_state->scissor_rect, rect))
2187 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2188 return;
2190 CopyRect(&device->update_state->scissor_rect, rect);
2192 if (device->recording)
2194 TRACE("Recording... not performing anything.\n");
2195 return;
2198 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2201 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2203 TRACE("device %p, rect %p.\n", device, rect);
2205 *rect = device->state.scissor_rect;
2206 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2209 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2210 struct wined3d_vertex_declaration *declaration)
2212 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2214 TRACE("device %p, declaration %p.\n", device, declaration);
2216 if (device->recording)
2217 device->recording->changed.vertexDecl = TRUE;
2219 if (declaration == prev)
2220 return;
2222 if (declaration)
2223 wined3d_vertex_declaration_incref(declaration);
2224 device->update_state->vertex_declaration = declaration;
2225 if (!device->recording)
2226 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2227 if (prev)
2228 wined3d_vertex_declaration_decref(prev);
2231 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2233 TRACE("device %p.\n", device);
2235 return device->state.vertex_declaration;
2238 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2240 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2242 TRACE("device %p, shader %p.\n", device, shader);
2244 if (device->recording)
2245 device->recording->changed.vertexShader = TRUE;
2247 if (shader == prev)
2248 return;
2250 if (shader)
2251 wined3d_shader_incref(shader);
2252 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2253 if (!device->recording)
2254 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2255 if (prev)
2256 wined3d_shader_decref(prev);
2259 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2261 TRACE("device %p.\n", device);
2263 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2266 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2267 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2269 struct wined3d_buffer *prev;
2271 if (idx >= MAX_CONSTANT_BUFFERS)
2273 WARN("Invalid constant buffer index %u.\n", idx);
2274 return;
2277 prev = device->update_state->cb[type][idx];
2278 if (buffer == prev)
2279 return;
2281 if (buffer)
2282 wined3d_buffer_incref(buffer);
2283 device->update_state->cb[type][idx] = buffer;
2284 if (!device->recording)
2285 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2286 if (prev)
2287 wined3d_buffer_decref(prev);
2290 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2292 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2294 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2297 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2299 TRACE("device %p, idx %u.\n", device, idx);
2301 if (idx >= MAX_CONSTANT_BUFFERS)
2303 WARN("Invalid constant buffer index %u.\n", idx);
2304 return NULL;
2307 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2310 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2311 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2313 struct wined3d_shader_resource_view *prev;
2315 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2317 WARN("Invalid view index %u.\n", idx);
2318 return;
2321 prev = device->update_state->shader_resource_view[type][idx];
2322 if (view == prev)
2323 return;
2325 if (view)
2326 wined3d_shader_resource_view_incref(view);
2327 device->update_state->shader_resource_view[type][idx] = view;
2328 if (!device->recording)
2329 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2330 if (prev)
2331 wined3d_shader_resource_view_decref(prev);
2334 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2335 UINT idx, struct wined3d_shader_resource_view *view)
2337 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2339 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2342 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2343 UINT idx)
2345 TRACE("device %p, idx %u.\n", device, idx);
2347 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2349 WARN("Invalid view index %u.\n", idx);
2350 return NULL;
2353 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2356 static void wined3d_device_set_sampler(struct wined3d_device *device,
2357 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2359 struct wined3d_sampler *prev;
2361 if (idx >= MAX_SAMPLER_OBJECTS)
2363 WARN("Invalid sampler index %u.\n", idx);
2364 return;
2367 prev = device->update_state->sampler[type][idx];
2368 if (sampler == prev)
2369 return;
2371 if (sampler)
2372 wined3d_sampler_incref(sampler);
2373 device->update_state->sampler[type][idx] = sampler;
2374 if (!device->recording)
2375 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2376 if (prev)
2377 wined3d_sampler_decref(prev);
2380 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2382 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2384 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2387 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2389 TRACE("device %p, idx %u.\n", device, idx);
2391 if (idx >= MAX_SAMPLER_OBJECTS)
2393 WARN("Invalid sampler index %u.\n", idx);
2394 return NULL;
2397 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2400 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2401 unsigned int start_idx, unsigned int count, const BOOL *constants)
2403 unsigned int i;
2405 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2406 device, start_idx, count, constants);
2408 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2409 return WINED3DERR_INVALIDCALL;
2411 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2412 count = WINED3D_MAX_CONSTS_B - start_idx;
2413 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2414 if (TRACE_ON(d3d))
2416 for (i = 0; i < count; ++i)
2417 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2420 if (device->recording)
2422 for (i = start_idx; i < count + start_idx; ++i)
2423 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2425 else
2427 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2430 return WINED3D_OK;
2433 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2434 unsigned int start_idx, unsigned int count, BOOL *constants)
2436 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2437 device, start_idx, count, constants);
2439 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2440 return WINED3DERR_INVALIDCALL;
2442 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2443 count = WINED3D_MAX_CONSTS_B - start_idx;
2444 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2446 return WINED3D_OK;
2449 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2450 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2452 unsigned int i;
2454 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2455 device, start_idx, count, constants);
2457 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2458 return WINED3DERR_INVALIDCALL;
2460 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2461 count = WINED3D_MAX_CONSTS_I - start_idx;
2462 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2463 if (TRACE_ON(d3d))
2465 for (i = 0; i < count; ++i)
2466 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2469 if (device->recording)
2471 for (i = start_idx; i < count + start_idx; ++i)
2472 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2474 else
2476 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2479 return WINED3D_OK;
2482 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2483 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2485 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2486 device, start_idx, count, constants);
2488 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2489 return WINED3DERR_INVALIDCALL;
2491 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2492 count = WINED3D_MAX_CONSTS_I - start_idx;
2493 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2494 return WINED3D_OK;
2497 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2498 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2500 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2501 unsigned int i;
2503 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2504 device, start_idx, count, constants);
2506 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2507 || count > d3d_info->limits.vs_uniform_count - start_idx)
2508 return WINED3DERR_INVALIDCALL;
2510 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2511 if (TRACE_ON(d3d))
2513 for (i = 0; i < count; ++i)
2514 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2517 if (device->recording)
2518 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2519 count * sizeof(*device->recording->changed.vs_consts_f));
2520 else
2521 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2523 return WINED3D_OK;
2526 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2527 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2529 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2531 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2532 device, start_idx, count, constants);
2534 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2535 || count > d3d_info->limits.vs_uniform_count - start_idx)
2536 return WINED3DERR_INVALIDCALL;
2538 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2540 return WINED3D_OK;
2543 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2545 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2547 TRACE("device %p, shader %p.\n", device, shader);
2549 if (device->recording)
2550 device->recording->changed.pixelShader = TRUE;
2552 if (shader == prev)
2553 return;
2555 if (shader)
2556 wined3d_shader_incref(shader);
2557 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2558 if (!device->recording)
2559 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2560 if (prev)
2561 wined3d_shader_decref(prev);
2564 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2566 TRACE("device %p.\n", device);
2568 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2571 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2573 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2575 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2578 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2580 TRACE("device %p, idx %u.\n", device, idx);
2582 if (idx >= MAX_CONSTANT_BUFFERS)
2584 WARN("Invalid constant buffer index %u.\n", idx);
2585 return NULL;
2588 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2591 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2592 UINT idx, struct wined3d_shader_resource_view *view)
2594 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2596 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2599 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2600 UINT idx)
2602 TRACE("device %p, idx %u.\n", device, idx);
2604 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2606 WARN("Invalid view index %u.\n", idx);
2607 return NULL;
2610 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2613 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2615 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2617 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2620 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2622 TRACE("device %p, idx %u.\n", device, idx);
2624 if (idx >= MAX_SAMPLER_OBJECTS)
2626 WARN("Invalid sampler index %u.\n", idx);
2627 return NULL;
2630 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2633 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2634 unsigned int start_idx, unsigned int count, const BOOL *constants)
2636 unsigned int i;
2638 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2639 device, start_idx, count, constants);
2641 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2642 return WINED3DERR_INVALIDCALL;
2644 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2645 count = WINED3D_MAX_CONSTS_B - start_idx;
2646 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2647 if (TRACE_ON(d3d))
2649 for (i = 0; i < count; ++i)
2650 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2653 if (device->recording)
2655 for (i = start_idx; i < count + start_idx; ++i)
2656 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2658 else
2660 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2663 return WINED3D_OK;
2666 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2667 unsigned int start_idx, unsigned int count, BOOL *constants)
2669 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2670 device, start_idx, count, constants);
2672 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2673 return WINED3DERR_INVALIDCALL;
2675 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2676 count = WINED3D_MAX_CONSTS_B - start_idx;
2677 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2679 return WINED3D_OK;
2682 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2683 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2685 unsigned int i;
2687 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2688 device, start_idx, count, constants);
2690 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2691 return WINED3DERR_INVALIDCALL;
2693 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2694 count = WINED3D_MAX_CONSTS_I - start_idx;
2695 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2696 if (TRACE_ON(d3d))
2698 for (i = 0; i < count; ++i)
2699 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2702 if (device->recording)
2704 for (i = start_idx; i < count + start_idx; ++i)
2705 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2707 else
2709 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2712 return WINED3D_OK;
2715 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2716 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2718 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2719 device, start_idx, count, constants);
2721 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2722 return WINED3DERR_INVALIDCALL;
2724 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2725 count = WINED3D_MAX_CONSTS_I - start_idx;
2726 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2728 return WINED3D_OK;
2731 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2732 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2734 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2735 unsigned int i;
2737 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2738 device, start_idx, count, constants);
2740 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2741 || count > d3d_info->limits.ps_uniform_count - start_idx)
2742 return WINED3DERR_INVALIDCALL;
2744 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2745 if (TRACE_ON(d3d))
2747 for (i = 0; i < count; ++i)
2748 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2751 if (device->recording)
2752 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2753 count * sizeof(*device->recording->changed.ps_consts_f));
2754 else
2755 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2757 return WINED3D_OK;
2760 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2761 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2763 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2765 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2766 device, start_idx, count, constants);
2768 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2769 || count > d3d_info->limits.ps_uniform_count - start_idx)
2770 return WINED3DERR_INVALIDCALL;
2772 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2774 return WINED3D_OK;
2777 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2779 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2781 TRACE("device %p, shader %p.\n", device, shader);
2783 if (device->recording || shader == prev)
2784 return;
2785 if (shader)
2786 wined3d_shader_incref(shader);
2787 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2788 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2789 if (prev)
2790 wined3d_shader_decref(prev);
2793 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2795 TRACE("device %p.\n", device);
2797 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2800 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2802 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2804 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2807 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2809 TRACE("device %p, idx %u.\n", device, idx);
2811 if (idx >= MAX_CONSTANT_BUFFERS)
2813 WARN("Invalid constant buffer index %u.\n", idx);
2814 return NULL;
2817 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2820 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2821 UINT idx, struct wined3d_shader_resource_view *view)
2823 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2825 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2828 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2829 UINT idx)
2831 TRACE("device %p, idx %u.\n", device, idx);
2833 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2835 WARN("Invalid view index %u.\n", idx);
2836 return NULL;
2839 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2842 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2844 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2846 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2849 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2851 TRACE("device %p, idx %u.\n", device, idx);
2853 if (idx >= MAX_SAMPLER_OBJECTS)
2855 WARN("Invalid sampler index %u.\n", idx);
2856 return NULL;
2859 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2862 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2863 unsigned int idx, struct wined3d_unordered_access_view *uav)
2865 struct wined3d_unordered_access_view *prev;
2867 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2869 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2871 WARN("Invalid UAV index %u.\n", idx);
2872 return;
2875 prev = device->update_state->unordered_access_view[idx];
2876 if (uav == prev)
2877 return;
2879 if (uav)
2880 wined3d_unordered_access_view_incref(uav);
2881 device->update_state->unordered_access_view[idx] = uav;
2882 if (!device->recording)
2883 wined3d_cs_emit_set_unordered_access_view(device->cs, idx, uav);
2884 if (prev)
2885 wined3d_unordered_access_view_decref(prev);
2888 /* Context activation is done by the caller. */
2889 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2890 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2891 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2892 DWORD DestFVF)
2894 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2895 struct wined3d_map_desc map_desc;
2896 struct wined3d_box box = {0};
2897 struct wined3d_viewport vp;
2898 UINT vertex_size;
2899 unsigned int i;
2900 BYTE *dest_ptr;
2901 BOOL doClip;
2902 DWORD numTextures;
2903 HRESULT hr;
2905 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2907 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2910 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2912 ERR("Source has no position mask\n");
2913 return WINED3DERR_INVALIDCALL;
2916 if (device->state.render_states[WINED3D_RS_CLIPPING])
2918 static BOOL warned = FALSE;
2920 * The clipping code is not quite correct. Some things need
2921 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2922 * so disable clipping for now.
2923 * (The graphics in Half-Life are broken, and my processvertices
2924 * test crashes with IDirect3DDevice3)
2925 doClip = TRUE;
2927 doClip = FALSE;
2928 if(!warned) {
2929 warned = TRUE;
2930 FIXME("Clipping is broken and disabled for now\n");
2933 else
2934 doClip = FALSE;
2936 vertex_size = get_flexible_vertex_size(DestFVF);
2937 box.left = dwDestIndex * vertex_size;
2938 box.right = box.left + dwCount * vertex_size;
2939 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2941 WARN("Failed to map buffer, hr %#x.\n", hr);
2942 return hr;
2944 dest_ptr = map_desc.data;
2946 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2947 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2948 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2950 TRACE("View mat:\n");
2951 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2952 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2953 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2954 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2956 TRACE("Proj mat:\n");
2957 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2958 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2959 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2960 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2962 TRACE("World mat:\n");
2963 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2964 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2965 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2966 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2968 /* Get the viewport */
2969 wined3d_device_get_viewport(device, &vp);
2970 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2971 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2973 multiply_matrix(&mat,&view_mat,&world_mat);
2974 multiply_matrix(&mat,&proj_mat,&mat);
2976 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2978 for (i = 0; i < dwCount; i+= 1) {
2979 unsigned int tex_index;
2981 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2982 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2983 /* The position first */
2984 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2985 const float *p = (const float *)(element->data.addr + i * element->stride);
2986 float x, y, z, rhw;
2987 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2989 /* Multiplication with world, view and projection matrix. */
2990 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2991 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2992 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2993 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2995 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2997 /* WARNING: The following things are taken from d3d7 and were not yet checked
2998 * against d3d8 or d3d9!
3001 /* Clipping conditions: From msdn
3003 * A vertex is clipped if it does not match the following requirements
3004 * -rhw < x <= rhw
3005 * -rhw < y <= rhw
3006 * 0 < z <= rhw
3007 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3009 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3010 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3014 if( !doClip ||
3015 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3016 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3017 ( rhw > eps ) ) ) {
3019 /* "Normal" viewport transformation (not clipped)
3020 * 1) The values are divided by rhw
3021 * 2) The y axis is negative, so multiply it with -1
3022 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3023 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3024 * 4) Multiply x with Width/2 and add Width/2
3025 * 5) The same for the height
3026 * 6) Add the viewpoint X and Y to the 2D coordinates and
3027 * The minimum Z value to z
3028 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3030 * Well, basically it's simply a linear transformation into viewport
3031 * coordinates
3034 x /= rhw;
3035 y /= rhw;
3036 z /= rhw;
3038 y *= -1;
3040 x *= vp.width / 2;
3041 y *= vp.height / 2;
3042 z *= vp.max_z - vp.min_z;
3044 x += vp.width / 2 + vp.x;
3045 y += vp.height / 2 + vp.y;
3046 z += vp.min_z;
3048 rhw = 1 / rhw;
3049 } else {
3050 /* That vertex got clipped
3051 * Contrary to OpenGL it is not dropped completely, it just
3052 * undergoes a different calculation.
3054 TRACE("Vertex got clipped\n");
3055 x += rhw;
3056 y += rhw;
3058 x /= 2;
3059 y /= 2;
3061 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3062 * outside of the main vertex buffer memory. That needs some more
3063 * investigation...
3067 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3070 ( (float *) dest_ptr)[0] = x;
3071 ( (float *) dest_ptr)[1] = y;
3072 ( (float *) dest_ptr)[2] = z;
3073 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3075 dest_ptr += 3 * sizeof(float);
3077 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3078 dest_ptr += sizeof(float);
3081 if (DestFVF & WINED3DFVF_PSIZE)
3082 dest_ptr += sizeof(DWORD);
3084 if (DestFVF & WINED3DFVF_NORMAL)
3086 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3087 const float *normal = (const float *)(element->data.addr + i * element->stride);
3088 /* AFAIK this should go into the lighting information */
3089 FIXME("Didn't expect the destination to have a normal\n");
3090 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3093 if (DestFVF & WINED3DFVF_DIFFUSE)
3095 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3096 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3097 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3099 static BOOL warned = FALSE;
3101 if(!warned) {
3102 ERR("No diffuse color in source, but destination has one\n");
3103 warned = TRUE;
3106 *( (DWORD *) dest_ptr) = 0xffffffff;
3107 dest_ptr += sizeof(DWORD);
3109 else
3111 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3115 if (DestFVF & WINED3DFVF_SPECULAR)
3117 /* What's the color value in the feedback buffer? */
3118 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3119 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3120 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3122 static BOOL warned = FALSE;
3124 if(!warned) {
3125 ERR("No specular color in source, but destination has one\n");
3126 warned = TRUE;
3129 *(DWORD *)dest_ptr = 0xff000000;
3130 dest_ptr += sizeof(DWORD);
3132 else
3134 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3138 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3140 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3141 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3142 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3144 ERR("No source texture, but destination requests one\n");
3145 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3147 else
3149 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3154 wined3d_resource_unmap(&dest->resource, 0);
3156 return WINED3D_OK;
3158 #undef copy_and_next
3160 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3161 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3162 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3164 struct wined3d_state *state = &device->state;
3165 struct wined3d_stream_info stream_info;
3166 const struct wined3d_gl_info *gl_info;
3167 struct wined3d_context *context;
3168 struct wined3d_shader *vs;
3169 unsigned int i;
3170 HRESULT hr;
3171 WORD map;
3173 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3174 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3175 device, src_start_idx, dst_idx, vertex_count,
3176 dst_buffer, declaration, flags, dst_fvf);
3178 if (declaration)
3179 FIXME("Output vertex declaration not implemented yet.\n");
3181 /* Need any context to write to the vbo. */
3182 context = context_acquire(device, NULL);
3183 gl_info = context->gl_info;
3185 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3186 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3187 context_stream_info_from_declaration(context, state, &stream_info);
3188 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3190 /* We can't convert FROM a VBO, and vertex buffers used to source into
3191 * process_vertices() are unlikely to ever be used for drawing. Release
3192 * VBOs in those buffers and fix up the stream_info structure.
3194 * Also apply the start index. */
3195 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3197 struct wined3d_stream_info_element *e;
3198 struct wined3d_buffer *buffer;
3200 if (!(map & 1))
3201 continue;
3203 e = &stream_info.elements[i];
3204 buffer = state->streams[e->stream_idx].buffer;
3205 e->data.buffer_object = 0;
3206 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3207 if (buffer->buffer_object)
3209 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3210 buffer->buffer_object = 0;
3211 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
3213 if (e->data.addr)
3214 e->data.addr += e->stride * src_start_idx;
3217 hr = process_vertices_strided(device, dst_idx, vertex_count,
3218 &stream_info, dst_buffer, flags, dst_fvf);
3220 context_release(context);
3222 return hr;
3225 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3226 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3228 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3229 DWORD old_value;
3231 TRACE("device %p, stage %u, state %s, value %#x.\n",
3232 device, stage, debug_d3dtexturestate(state), value);
3234 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3236 WARN("Invalid state %#x passed.\n", state);
3237 return;
3240 if (stage >= d3d_info->limits.ffp_blend_stages)
3242 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3243 stage, d3d_info->limits.ffp_blend_stages - 1);
3244 return;
3247 old_value = device->update_state->texture_states[stage][state];
3248 device->update_state->texture_states[stage][state] = value;
3250 if (device->recording)
3252 TRACE("Recording... not performing anything.\n");
3253 device->recording->changed.textureState[stage] |= 1u << state;
3254 return;
3257 /* Checked after the assignments to allow proper stateblock recording. */
3258 if (old_value == value)
3260 TRACE("Application is setting the old value over, nothing to do.\n");
3261 return;
3264 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3267 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3268 UINT stage, enum wined3d_texture_stage_state state)
3270 TRACE("device %p, stage %u, state %s.\n",
3271 device, stage, debug_d3dtexturestate(state));
3273 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3275 WARN("Invalid state %#x passed.\n", state);
3276 return 0;
3279 return device->state.texture_states[stage][state];
3282 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3283 UINT stage, struct wined3d_texture *texture)
3285 struct wined3d_texture *prev;
3287 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3289 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3290 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3292 /* Windows accepts overflowing this array... we do not. */
3293 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3295 WARN("Ignoring invalid stage %u.\n", stage);
3296 return WINED3D_OK;
3299 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3301 WARN("Rejecting attempt to set scratch texture.\n");
3302 return WINED3DERR_INVALIDCALL;
3305 if (device->recording)
3306 device->recording->changed.textures |= 1u << stage;
3308 prev = device->update_state->textures[stage];
3309 TRACE("Previous texture %p.\n", prev);
3311 if (texture == prev)
3313 TRACE("App is setting the same texture again, nothing to do.\n");
3314 return WINED3D_OK;
3317 TRACE("Setting new texture to %p.\n", texture);
3318 device->update_state->textures[stage] = texture;
3320 if (texture)
3321 wined3d_texture_incref(texture);
3322 if (!device->recording)
3323 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3324 if (prev)
3325 wined3d_texture_decref(prev);
3327 return WINED3D_OK;
3330 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3332 TRACE("device %p, stage %u.\n", device, stage);
3334 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3335 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3337 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3339 WARN("Ignoring invalid stage %u.\n", stage);
3340 return NULL; /* Windows accepts overflowing this array ... we do not. */
3343 return device->state.textures[stage];
3346 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3348 TRACE("device %p, caps %p.\n", device, caps);
3350 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3351 device->create_parms.device_type, caps);
3354 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3355 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3357 struct wined3d_swapchain *swapchain;
3359 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3360 device, swapchain_idx, mode, rotation);
3362 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3363 return WINED3DERR_INVALIDCALL;
3365 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3368 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3370 struct wined3d_stateblock *stateblock;
3371 HRESULT hr;
3373 TRACE("device %p.\n", device);
3375 if (device->recording)
3376 return WINED3DERR_INVALIDCALL;
3378 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3379 if (FAILED(hr))
3380 return hr;
3382 device->recording = stateblock;
3383 device->update_state = &stateblock->state;
3385 TRACE("Recording stateblock %p.\n", stateblock);
3387 return WINED3D_OK;
3390 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3391 struct wined3d_stateblock **stateblock)
3393 struct wined3d_stateblock *object = device->recording;
3395 TRACE("device %p, stateblock %p.\n", device, stateblock);
3397 if (!device->recording)
3399 WARN("Not recording.\n");
3400 *stateblock = NULL;
3401 return WINED3DERR_INVALIDCALL;
3404 stateblock_init_contained_states(object);
3406 *stateblock = object;
3407 device->recording = NULL;
3408 device->update_state = &device->state;
3410 TRACE("Returning stateblock %p.\n", *stateblock);
3412 return WINED3D_OK;
3415 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3417 /* At the moment we have no need for any functionality at the beginning
3418 * of a scene. */
3419 TRACE("device %p.\n", device);
3421 if (device->inScene)
3423 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3424 return WINED3DERR_INVALIDCALL;
3426 device->inScene = TRUE;
3427 return WINED3D_OK;
3430 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3432 struct wined3d_context *context;
3434 TRACE("device %p.\n", device);
3436 if (!device->inScene)
3438 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3439 return WINED3DERR_INVALIDCALL;
3442 context = context_acquire(device, NULL);
3443 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3444 context->gl_info->gl_ops.gl.p_glFlush();
3445 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3446 * fails. */
3447 context_release(context);
3449 device->inScene = FALSE;
3450 return WINED3D_OK;
3453 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3454 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3456 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3457 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3459 if (!rect_count && rects)
3461 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3462 return WINED3D_OK;
3465 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3467 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3468 if (!ds)
3470 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3471 /* TODO: What about depth stencil buffers without stencil bits? */
3472 return WINED3DERR_INVALIDCALL;
3474 else if (flags & WINED3DCLEAR_TARGET)
3476 if (ds->width < device->fb.render_targets[0]->width
3477 || ds->height < device->fb.render_targets[0]->height)
3479 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3480 return WINED3D_OK;
3485 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3487 return WINED3D_OK;
3490 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3491 struct wined3d_query *predicate, BOOL value)
3493 struct wined3d_query *prev;
3495 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3497 prev = device->update_state->predicate;
3498 if (predicate)
3500 FIXME("Predicated rendering not implemented.\n");
3501 wined3d_query_incref(predicate);
3503 device->update_state->predicate = predicate;
3504 device->update_state->predicate_value = value;
3505 if (!device->recording)
3506 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3507 if (prev)
3508 wined3d_query_decref(prev);
3511 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3513 TRACE("device %p, value %p.\n", device, value);
3515 *value = device->state.predicate_value;
3516 return device->state.predicate;
3519 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3520 enum wined3d_primitive_type primitive_type)
3522 GLenum gl_primitive_type, prev;
3524 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3526 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3527 prev = device->update_state->gl_primitive_type;
3528 device->update_state->gl_primitive_type = gl_primitive_type;
3529 if (device->recording)
3530 device->recording->changed.primitive_type = TRUE;
3531 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3532 device_invalidate_state(device, STATE_POINT_ENABLE);
3535 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3536 enum wined3d_primitive_type *primitive_type)
3538 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3540 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3542 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3545 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3547 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3549 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, 0, 0, FALSE);
3551 return WINED3D_OK;
3554 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3555 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3557 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3558 device, start_vertex, vertex_count, start_instance, instance_count);
3560 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3563 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3565 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3567 if (!device->state.index_buffer)
3569 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3570 * without an index buffer set. (The first time at least...)
3571 * D3D8 simply dies, but I doubt it can do much harm to return
3572 * D3DERR_INVALIDCALL there as well. */
3573 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3574 return WINED3DERR_INVALIDCALL;
3577 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3579 return WINED3D_OK;
3582 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3583 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3585 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3586 device, start_idx, index_count, start_instance, instance_count);
3588 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index,
3589 start_idx, index_count, start_instance, instance_count, TRUE);
3592 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3593 struct wined3d_texture *src_texture, unsigned int src_level,
3594 struct wined3d_texture *dst_texture, unsigned int level_count)
3596 struct wined3d_const_bo_address data;
3597 struct wined3d_context *context;
3598 struct wined3d_map_desc src;
3599 HRESULT hr = WINED3D_OK;
3600 unsigned int i;
3602 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3603 device, src_texture, src_level, dst_texture, level_count);
3605 if (src_texture->resource.format != dst_texture->resource.format)
3607 WARN("Source and destination formats do not match.\n");
3608 return WINED3DERR_INVALIDCALL;
3611 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3612 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3613 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3615 WARN("Source and destination dimensions do not match.\n");
3616 return WINED3DERR_INVALIDCALL;
3619 context = context_acquire(device, NULL);
3621 /* Only a prepare, since we're uploading entire volumes. */
3622 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3623 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3625 for (i = 0; i < level_count; ++i)
3627 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3628 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3629 goto done;
3631 data.buffer_object = 0;
3632 data.addr = src.data;
3633 wined3d_texture_upload_data(dst_texture, i, context, NULL, &data, src.row_pitch, src.slice_pitch);
3634 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3636 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3637 goto done;
3640 done:
3641 context_release(context);
3642 return hr;
3645 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3646 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3648 unsigned int src_size, dst_size, src_skip_levels = 0;
3649 unsigned int layer_count, level_count, i, j;
3650 enum wined3d_resource_type type;
3651 HRESULT hr;
3652 struct wined3d_context *context;
3654 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3656 /* Verify that the source and destination textures are non-NULL. */
3657 if (!src_texture || !dst_texture)
3659 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3660 return WINED3DERR_INVALIDCALL;
3663 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3665 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3666 return WINED3DERR_INVALIDCALL;
3668 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3670 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3671 return WINED3DERR_INVALIDCALL;
3674 /* Verify that the source and destination textures are the same type. */
3675 type = src_texture->resource.type;
3676 if (dst_texture->resource.type != type)
3678 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3679 return WINED3DERR_INVALIDCALL;
3682 layer_count = src_texture->layer_count;
3683 if (layer_count != dst_texture->layer_count)
3685 WARN("Source and destination have different layer counts.\n");
3686 return WINED3DERR_INVALIDCALL;
3689 level_count = min(wined3d_texture_get_level_count(src_texture),
3690 wined3d_texture_get_level_count(dst_texture));
3692 src_size = max(src_texture->resource.width, src_texture->resource.height);
3693 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3694 if (type == WINED3D_RTYPE_TEXTURE_3D)
3696 src_size = max(src_size, src_texture->resource.depth);
3697 dst_size = max(dst_size, dst_texture->resource.depth);
3699 while (src_size > dst_size)
3701 src_size >>= 1;
3702 ++src_skip_levels;
3705 /* Make sure that the destination texture is loaded. */
3706 context = context_acquire(device, NULL);
3707 wined3d_texture_load(dst_texture, context, FALSE);
3708 context_release(context);
3710 /* Update every surface level of the texture. */
3711 switch (type)
3713 case WINED3D_RTYPE_TEXTURE_2D:
3715 unsigned int src_levels = src_texture->level_count;
3716 unsigned int dst_levels = dst_texture->level_count;
3717 struct wined3d_surface *src_surface;
3718 struct wined3d_surface *dst_surface;
3720 for (i = 0; i < layer_count; ++i)
3722 for (j = 0; j < level_count; ++j)
3724 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3725 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3726 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3728 WARN("Failed to update surface, hr %#x.\n", hr);
3729 return hr;
3733 return WINED3D_OK;
3736 case WINED3D_RTYPE_TEXTURE_3D:
3737 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3738 src_texture, src_skip_levels, dst_texture, level_count)))
3739 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3740 return hr;
3742 default:
3743 FIXME("Unsupported texture type %#x.\n", type);
3744 return WINED3DERR_INVALIDCALL;
3748 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3750 const struct wined3d_state *state = &device->state;
3751 struct wined3d_texture *texture;
3752 DWORD i;
3754 TRACE("device %p, num_passes %p.\n", device, num_passes);
3756 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3758 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3760 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3761 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3763 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3765 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3766 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3769 texture = state->textures[i];
3770 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3772 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3774 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3775 return E_FAIL;
3777 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3779 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3780 return E_FAIL;
3782 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3783 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3785 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3786 return E_FAIL;
3790 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3791 || state->render_states[WINED3D_RS_STENCILENABLE])
3793 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3794 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3796 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3798 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3799 return WINED3DERR_CONFLICTINGRENDERSTATE;
3803 /* return a sensible default */
3804 *num_passes = 1;
3806 TRACE("returning D3D_OK\n");
3807 return WINED3D_OK;
3810 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3812 static BOOL warned;
3814 TRACE("device %p, software %#x.\n", device, software);
3816 if (!warned)
3818 FIXME("device %p, software %#x stub!\n", device, software);
3819 warned = TRUE;
3822 device->softwareVertexProcessing = software;
3825 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3827 static BOOL warned;
3829 TRACE("device %p.\n", device);
3831 if (!warned)
3833 TRACE("device %p stub!\n", device);
3834 warned = TRUE;
3837 return device->softwareVertexProcessing;
3840 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3841 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3843 struct wined3d_swapchain *swapchain;
3845 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3846 device, swapchain_idx, raster_status);
3848 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3849 return WINED3DERR_INVALIDCALL;
3851 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3854 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3856 static BOOL warned;
3858 TRACE("device %p, segments %.8e.\n", device, segments);
3860 if (segments != 0.0f)
3862 if (!warned)
3864 FIXME("device %p, segments %.8e stub!\n", device, segments);
3865 warned = TRUE;
3869 return WINED3D_OK;
3872 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3874 static BOOL warned;
3876 TRACE("device %p.\n", device);
3878 if (!warned)
3880 FIXME("device %p stub!\n", device);
3881 warned = TRUE;
3884 return 0.0f;
3887 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3888 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3890 struct wined3d_texture *dst_texture, *src_texture;
3891 RECT dst_rect, src_rect;
3892 unsigned int i, j;
3893 HRESULT hr;
3895 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3897 if (src_resource == dst_resource)
3899 WARN("Source and destination are the same resource.\n");
3900 return;
3903 if (src_resource->type != dst_resource->type)
3905 WARN("Resource types (%s / %s) don't match.\n",
3906 debug_d3dresourcetype(dst_resource->type),
3907 debug_d3dresourcetype(src_resource->type));
3908 return;
3911 if (src_resource->width != dst_resource->width
3912 || src_resource->height != dst_resource->height
3913 || src_resource->depth != dst_resource->depth)
3915 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3916 dst_resource->width, dst_resource->height, dst_resource->depth,
3917 src_resource->width, src_resource->height, src_resource->depth);
3918 return;
3921 if (src_resource->format->id != dst_resource->format->id)
3923 WARN("Resource formats (%s / %s) don't match.\n",
3924 debug_d3dformat(dst_resource->format->id),
3925 debug_d3dformat(src_resource->format->id));
3926 return;
3929 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3931 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3932 buffer_from_resource(src_resource), 0,
3933 dst_resource->size)))
3934 ERR("Failed to copy buffer, hr %#x.\n", hr);
3935 return;
3938 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3940 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3941 return;
3944 dst_texture = texture_from_resource(dst_resource);
3945 src_texture = texture_from_resource(src_resource);
3947 if (src_texture->layer_count != dst_texture->layer_count
3948 || src_texture->level_count != dst_texture->level_count)
3950 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3951 dst_texture->layer_count, dst_texture->level_count,
3952 src_texture->layer_count, src_texture->level_count);
3953 return;
3956 for (i = 0; i < dst_texture->level_count; ++i)
3958 SetRect(&dst_rect, 0, 0,
3959 wined3d_texture_get_level_width(dst_texture, i),
3960 wined3d_texture_get_level_height(dst_texture, i));
3961 SetRect(&src_rect, 0, 0,
3962 wined3d_texture_get_level_width(src_texture, i),
3963 wined3d_texture_get_level_height(dst_texture, i));
3964 for (j = 0; j < dst_texture->layer_count; ++j)
3966 unsigned int idx = j * dst_texture->level_count + i;
3968 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3969 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3970 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3975 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3976 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3977 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3978 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3980 struct wined3d_texture *dst_texture, *src_texture;
3981 RECT dst_rect, src_rect;
3982 HRESULT hr;
3984 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3985 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3986 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3987 src_resource, src_sub_resource_idx, debug_box(src_box));
3989 if (src_box && (src_box->left >= src_box->right
3990 || src_box->top >= src_box->bottom
3991 || src_box->front >= src_box->back))
3993 WARN("Invalid box %s specified.\n", debug_box(src_box));
3994 return WINED3DERR_INVALIDCALL;
3997 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3999 WARN("Source and destination are the same sub-resource.\n");
4000 return WINED3DERR_INVALIDCALL;
4003 if (src_resource->type != dst_resource->type)
4005 WARN("Resource types (%s / %s) don't match.\n",
4006 debug_d3dresourcetype(dst_resource->type),
4007 debug_d3dresourcetype(src_resource->type));
4008 return WINED3DERR_INVALIDCALL;
4011 if (src_resource->format->id != dst_resource->format->id)
4013 WARN("Resource formats (%s / %s) don't match.\n",
4014 debug_d3dformat(dst_resource->format->id),
4015 debug_d3dformat(src_resource->format->id));
4016 return WINED3DERR_INVALIDCALL;
4019 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4021 unsigned int src_offset, size;
4023 if (dst_sub_resource_idx)
4025 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4026 return WINED3DERR_INVALIDCALL;
4028 if (src_sub_resource_idx)
4030 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4031 return WINED3DERR_INVALIDCALL;
4034 if (src_box)
4036 src_offset = src_box->left;
4037 size = src_box->right - src_box->left;
4039 else
4041 src_offset = 0;
4042 size = src_resource->size;
4045 if (src_offset > src_resource->size
4046 || size > src_resource->size - src_offset
4047 || dst_x > dst_resource->size
4048 || size > dst_resource->size - dst_x)
4050 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4051 dst_x, src_offset, size);
4052 return WINED3DERR_INVALIDCALL;
4055 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4056 buffer_from_resource(src_resource), src_offset, size);
4059 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4061 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4062 return WINED3DERR_INVALIDCALL;
4065 dst_texture = texture_from_resource(dst_resource);
4066 src_texture = texture_from_resource(src_resource);
4068 if (src_box)
4070 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4072 else
4074 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4076 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4077 wined3d_texture_get_level_height(src_texture, level));
4080 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4081 dst_y + (src_rect.bottom - src_rect.top));
4083 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4084 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4085 WARN("Failed to blit, hr %#x.\n", hr);
4087 return hr;
4090 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4091 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4092 unsigned int depth_pitch)
4094 unsigned int width, height, depth, level;
4095 struct wined3d_const_bo_address addr;
4096 struct wined3d_context *context;
4097 struct wined3d_texture *texture;
4099 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4100 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4102 if (resource->type == WINED3D_RTYPE_BUFFER)
4104 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4105 HRESULT hr;
4107 if (sub_resource_idx > 0)
4109 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4110 return;
4113 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4114 WARN("Failed to update buffer data, hr %#x.\n", hr);
4116 return;
4119 if (resource->type != WINED3D_RTYPE_TEXTURE_2D && resource->type != WINED3D_RTYPE_TEXTURE_3D)
4121 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4122 return;
4125 texture = texture_from_resource(resource);
4126 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4128 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4129 return;
4132 level = sub_resource_idx % texture->level_count;
4133 width = wined3d_texture_get_level_width(texture, level);
4134 height = wined3d_texture_get_level_height(texture, level);
4135 depth = wined3d_texture_get_level_depth(texture, level);
4137 if (box && (box->left >= box->right || box->right > width
4138 || box->top >= box->bottom || box->bottom > height
4139 || box->front >= box->back || box->back > depth))
4141 WARN("Invalid box %s specified.\n", debug_box(box));
4142 return;
4145 addr.buffer_object = 0;
4146 addr.addr = data;
4148 context = context_acquire(resource->device, NULL);
4150 /* Only load the sub-resource for partial updates. */
4151 if (!box || (!box->left && !box->top && !box->front
4152 && box->right == width && box->bottom == height && box->back == depth))
4153 wined3d_texture_prepare_texture(texture, context, FALSE);
4154 else
4155 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4156 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4158 wined3d_texture_upload_data(texture, sub_resource_idx, context, box, &addr, row_pitch, depth_pitch);
4160 context_release(context);
4162 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4163 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4166 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4167 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4168 const struct wined3d_color *color, float depth, DWORD stencil)
4170 const struct blit_shader *blitter;
4171 struct wined3d_resource *resource;
4172 enum wined3d_blit_op blit_op;
4173 RECT r;
4175 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4176 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4178 if (!flags)
4179 return WINED3D_OK;
4181 resource = view->resource;
4182 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4184 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4185 return WINED3DERR_INVALIDCALL;
4188 if (view->depth > 1)
4190 FIXME("Layered clears not implemented.\n");
4191 return WINED3DERR_INVALIDCALL;
4194 if (!rect)
4196 SetRect(&r, 0, 0, view->width, view->height);
4197 rect = &r;
4200 if (flags & WINED3DCLEAR_TARGET)
4201 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4202 else
4203 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4205 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4206 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4208 FIXME("No blitter is capable of performing the requested fill operation.\n");
4209 return WINED3DERR_INVALIDCALL;
4212 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4213 return blitter->color_fill(device, view, rect, color);
4214 else
4215 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4218 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4219 unsigned int view_idx)
4221 TRACE("device %p, view_idx %u.\n", device, view_idx);
4223 if (view_idx >= device->adapter->gl_info.limits.buffers)
4225 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4226 return NULL;
4229 return device->fb.render_targets[view_idx];
4232 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4234 TRACE("device %p.\n", device);
4236 return device->fb.depth_stencil;
4239 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4240 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4242 struct wined3d_rendertarget_view *prev;
4244 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4245 device, view_idx, view, set_viewport);
4247 if (view_idx >= device->adapter->gl_info.limits.buffers)
4249 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4250 return WINED3DERR_INVALIDCALL;
4253 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4255 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4256 return WINED3DERR_INVALIDCALL;
4259 /* Set the viewport and scissor rectangles, if requested. Tests show that
4260 * stateblock recording is ignored, the change goes directly into the
4261 * primary stateblock. */
4262 if (!view_idx && set_viewport)
4264 struct wined3d_state *state = &device->state;
4266 state->viewport.x = 0;
4267 state->viewport.y = 0;
4268 state->viewport.width = view->width;
4269 state->viewport.height = view->height;
4270 state->viewport.min_z = 0.0f;
4271 state->viewport.max_z = 1.0f;
4272 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4274 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4275 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4279 prev = device->fb.render_targets[view_idx];
4280 if (view == prev)
4281 return WINED3D_OK;
4283 if (view)
4284 wined3d_rendertarget_view_incref(view);
4285 device->fb.render_targets[view_idx] = view;
4286 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4287 /* Release after the assignment, to prevent device_resource_released()
4288 * from seeing the surface as still in use. */
4289 if (prev)
4290 wined3d_rendertarget_view_decref(prev);
4292 return WINED3D_OK;
4295 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4297 struct wined3d_rendertarget_view *prev;
4299 TRACE("device %p, view %p.\n", device, view);
4301 prev = device->fb.depth_stencil;
4302 if (prev == view)
4304 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4305 return;
4308 if ((device->fb.depth_stencil = view))
4309 wined3d_rendertarget_view_incref(view);
4310 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4311 if (prev)
4312 wined3d_rendertarget_view_decref(prev);
4315 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4316 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4318 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4319 struct wined3d_sub_resource_data data;
4320 struct wined3d_resource_desc desc;
4321 struct wined3d_map_desc map_desc;
4322 struct wined3d_texture *texture;
4323 HRESULT hr;
4325 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4327 ERR("Failed to map source texture.\n");
4328 return NULL;
4331 data.data = map_desc.data;
4332 data.row_pitch = map_desc.row_pitch;
4333 data.slice_pitch = map_desc.slice_pitch;
4335 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4336 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4337 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4338 desc.multisample_quality = 0;
4339 desc.usage = WINED3DUSAGE_DYNAMIC;
4340 desc.pool = WINED3D_POOL_DEFAULT;
4341 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4342 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4343 desc.depth = 1;
4344 desc.size = 0;
4346 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4347 &data, NULL, &wined3d_null_parent_ops, &texture);
4348 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4349 if (FAILED(hr))
4351 ERR("Failed to create cursor texture.\n");
4352 return NULL;
4355 return texture;
4358 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4359 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4361 unsigned int texture_level = sub_resource_idx % texture->level_count;
4362 unsigned int cursor_width, cursor_height;
4363 struct wined3d_display_mode mode;
4364 struct wined3d_map_desc map_desc;
4365 HRESULT hr;
4367 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4368 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4370 if (sub_resource_idx >= texture->level_count * texture->layer_count
4371 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4372 return WINED3DERR_INVALIDCALL;
4374 if (device->cursor_texture)
4376 wined3d_texture_decref(device->cursor_texture);
4377 device->cursor_texture = NULL;
4380 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4382 WARN("Texture %p has invalid format %s.\n",
4383 texture, debug_d3dformat(texture->resource.format->id));
4384 return WINED3DERR_INVALIDCALL;
4387 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4389 ERR("Failed to get display mode, hr %#x.\n", hr);
4390 return WINED3DERR_INVALIDCALL;
4393 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4394 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4395 if (cursor_width > mode.width || cursor_height > mode.height)
4397 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4398 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4399 return WINED3DERR_INVALIDCALL;
4402 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4404 /* Do not store the surface's pointer because the application may
4405 * release it after setting the cursor image. Windows doesn't
4406 * addref the set surface, so we can't do this either without
4407 * creating circular refcount dependencies. */
4408 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4410 ERR("Failed to create cursor texture.\n");
4411 return WINED3DERR_INVALIDCALL;
4414 if (cursor_width == 32 && cursor_height == 32)
4416 UINT mask_size = cursor_width * cursor_height / 8;
4417 ICONINFO cursor_info;
4418 DWORD *mask_bits;
4419 HCURSOR cursor;
4421 /* 32-bit user32 cursors ignore the alpha channel if it's all
4422 * zeroes, and use the mask instead. Fill the mask with all ones
4423 * to ensure we still get a fully transparent cursor. */
4424 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4425 return E_OUTOFMEMORY;
4426 memset(mask_bits, 0xff, mask_size);
4428 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4429 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4430 cursor_info.fIcon = FALSE;
4431 cursor_info.xHotspot = x_hotspot;
4432 cursor_info.yHotspot = y_hotspot;
4433 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4434 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4435 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4437 /* Create our cursor and clean up. */
4438 cursor = CreateIconIndirect(&cursor_info);
4439 if (cursor_info.hbmMask)
4440 DeleteObject(cursor_info.hbmMask);
4441 if (cursor_info.hbmColor)
4442 DeleteObject(cursor_info.hbmColor);
4443 if (device->hardwareCursor)
4444 DestroyCursor(device->hardwareCursor);
4445 device->hardwareCursor = cursor;
4446 if (device->bCursorVisible)
4447 SetCursor(cursor);
4449 HeapFree(GetProcessHeap(), 0, mask_bits);
4452 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4453 device->cursorWidth = cursor_width;
4454 device->cursorHeight = cursor_height;
4455 device->xHotSpot = x_hotspot;
4456 device->yHotSpot = y_hotspot;
4458 return WINED3D_OK;
4461 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4462 int x_screen_space, int y_screen_space, DWORD flags)
4464 TRACE("device %p, x %d, y %d, flags %#x.\n",
4465 device, x_screen_space, y_screen_space, flags);
4467 device->xScreenSpace = x_screen_space;
4468 device->yScreenSpace = y_screen_space;
4470 if (device->hardwareCursor)
4472 POINT pt;
4474 GetCursorPos( &pt );
4475 if (x_screen_space == pt.x && y_screen_space == pt.y)
4476 return;
4477 SetCursorPos( x_screen_space, y_screen_space );
4479 /* Switch to the software cursor if position diverges from the hardware one. */
4480 GetCursorPos( &pt );
4481 if (x_screen_space != pt.x || y_screen_space != pt.y)
4483 if (device->bCursorVisible) SetCursor( NULL );
4484 DestroyCursor( device->hardwareCursor );
4485 device->hardwareCursor = 0;
4490 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4492 BOOL oldVisible = device->bCursorVisible;
4494 TRACE("device %p, show %#x.\n", device, show);
4497 * When ShowCursor is first called it should make the cursor appear at the OS's last
4498 * known cursor position.
4500 if (show && !oldVisible)
4502 POINT pt;
4503 GetCursorPos(&pt);
4504 device->xScreenSpace = pt.x;
4505 device->yScreenSpace = pt.y;
4508 if (device->hardwareCursor)
4510 device->bCursorVisible = show;
4511 if (show)
4512 SetCursor(device->hardwareCursor);
4513 else
4514 SetCursor(NULL);
4516 else if (device->cursor_texture)
4518 device->bCursorVisible = show;
4521 return oldVisible;
4524 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4526 struct wined3d_resource *resource, *cursor;
4528 TRACE("device %p.\n", device);
4530 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4532 TRACE("Checking resource %p for eviction.\n", resource);
4534 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4536 TRACE("Evicting %p.\n", resource);
4537 wined3d_cs_emit_unload_resource(device->cs, resource);
4542 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4544 struct wined3d_resource *resource, *cursor;
4545 const struct wined3d_gl_info *gl_info;
4546 struct wined3d_context *context;
4547 struct wined3d_shader *shader;
4549 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4551 TRACE("Unloading resource %p.\n", resource);
4552 wined3d_cs_emit_unload_resource(device->cs, resource);
4555 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4557 device->shader_backend->shader_destroy(shader);
4560 context = context_acquire(device, NULL);
4561 gl_info = context->gl_info;
4563 if (device->depth_blt_texture)
4565 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4566 device->depth_blt_texture = 0;
4569 device->blitter->free_private(device);
4570 device->shader_backend->shader_free_private(device);
4571 destroy_dummy_textures(device, context);
4572 destroy_default_samplers(device, context);
4574 context_release(context);
4576 while (device->context_count)
4578 if (device->contexts[0]->swapchain)
4579 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4580 else
4581 context_destroy(device, device->contexts[0]);
4584 HeapFree(GetProcessHeap(), 0, swapchain->context);
4585 swapchain->context = NULL;
4588 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4590 struct wined3d_context *context;
4591 struct wined3d_texture *target;
4592 HRESULT hr;
4594 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4595 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4597 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4598 return hr;
4601 if (FAILED(hr = device->blitter->alloc_private(device)))
4603 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4604 device->shader_backend->shader_free_private(device);
4605 return hr;
4608 /* Recreate the primary swapchain's context */
4609 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4610 if (!swapchain->context)
4612 ERR("Failed to allocate memory for swapchain context array.\n");
4613 device->blitter->free_private(device);
4614 device->shader_backend->shader_free_private(device);
4615 return E_OUTOFMEMORY;
4618 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4619 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4621 WARN("Failed to create context.\n");
4622 device->blitter->free_private(device);
4623 device->shader_backend->shader_free_private(device);
4624 HeapFree(GetProcessHeap(), 0, swapchain->context);
4625 return E_FAIL;
4628 swapchain->context[0] = context;
4629 swapchain->num_contexts = 1;
4630 create_dummy_textures(device, context);
4631 create_default_samplers(device, context);
4632 context_release(context);
4634 return WINED3D_OK;
4637 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4638 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4639 wined3d_device_reset_cb callback, BOOL reset_state)
4641 struct wined3d_resource *resource, *cursor;
4642 struct wined3d_swapchain *swapchain;
4643 struct wined3d_view_desc view_desc;
4644 BOOL backbuffer_resized;
4645 HRESULT hr = WINED3D_OK;
4646 unsigned int i;
4648 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4649 device, swapchain_desc, mode, callback, reset_state);
4651 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4653 ERR("Failed to get the first implicit swapchain.\n");
4654 return WINED3DERR_INVALIDCALL;
4657 if (reset_state)
4659 if (device->logo_texture)
4661 wined3d_texture_decref(device->logo_texture);
4662 device->logo_texture = NULL;
4664 if (device->cursor_texture)
4666 wined3d_texture_decref(device->cursor_texture);
4667 device->cursor_texture = NULL;
4669 state_unbind_resources(&device->state);
4672 if (device->fb.render_targets)
4674 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4676 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4679 wined3d_device_set_depth_stencil_view(device, NULL);
4681 if (device->onscreen_depth_stencil)
4683 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4684 device->onscreen_depth_stencil = NULL;
4687 if (reset_state)
4689 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4691 TRACE("Enumerating resource %p.\n", resource);
4692 if (FAILED(hr = callback(resource)))
4693 return hr;
4697 TRACE("New params:\n");
4698 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4699 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4700 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4701 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4702 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4703 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4704 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4705 TRACE("device_window %p\n", swapchain_desc->device_window);
4706 TRACE("windowed %#x\n", swapchain_desc->windowed);
4707 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4708 if (swapchain_desc->enable_auto_depth_stencil)
4709 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4710 TRACE("flags %#x\n", swapchain_desc->flags);
4711 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4712 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4713 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4715 /* No special treatment of these parameters. Just store them */
4716 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4717 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4718 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4719 swapchain->desc.flags = swapchain_desc->flags;
4720 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4721 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4722 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4724 if (swapchain_desc->device_window
4725 && swapchain_desc->device_window != swapchain->desc.device_window)
4727 TRACE("Changing the device window from %p to %p.\n",
4728 swapchain->desc.device_window, swapchain_desc->device_window);
4729 swapchain->desc.device_window = swapchain_desc->device_window;
4730 swapchain->device_window = swapchain_desc->device_window;
4731 wined3d_swapchain_set_window(swapchain, NULL);
4734 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4735 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4737 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4738 || swapchain->reapply_mode || mode
4739 || (!swapchain_desc->windowed && backbuffer_resized))
4741 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4742 return hr;
4744 else if (!swapchain_desc->windowed)
4746 DWORD style = device->style;
4747 DWORD exStyle = device->exStyle;
4748 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4749 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4750 * Reset to clear up their mess. Guild Wars also loses the device during that.
4752 device->style = 0;
4753 device->exStyle = 0;
4754 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4755 swapchain_desc->backbuffer_width,
4756 swapchain_desc->backbuffer_height);
4757 device->style = style;
4758 device->exStyle = exStyle;
4761 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4762 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4763 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4764 return hr;
4766 if (device->auto_depth_stencil_view)
4768 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4769 device->auto_depth_stencil_view = NULL;
4771 if (swapchain->desc.enable_auto_depth_stencil)
4773 struct wined3d_resource_desc texture_desc;
4774 struct wined3d_texture *texture;
4776 TRACE("Creating the depth stencil buffer.\n");
4778 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4779 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4780 texture_desc.multisample_type = swapchain->desc.multisample_type;
4781 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4782 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4783 texture_desc.pool = WINED3D_POOL_DEFAULT;
4784 texture_desc.width = swapchain->desc.backbuffer_width;
4785 texture_desc.height = swapchain->desc.backbuffer_height;
4786 texture_desc.depth = 1;
4787 texture_desc.size = 0;
4789 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4790 device->device_parent, &texture_desc, &texture)))
4792 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4793 return WINED3DERR_INVALIDCALL;
4796 view_desc.format_id = texture->resource.format->id;
4797 view_desc.flags = 0;
4798 view_desc.u.texture.level_idx = 0;
4799 view_desc.u.texture.level_count = 1;
4800 view_desc.u.texture.layer_idx = 0;
4801 view_desc.u.texture.layer_count = 1;
4802 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4803 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4804 wined3d_texture_decref(texture);
4805 if (FAILED(hr))
4807 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4808 return hr;
4811 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4814 if (device->back_buffer_view)
4816 wined3d_rendertarget_view_decref(device->back_buffer_view);
4817 device->back_buffer_view = NULL;
4819 if (swapchain->desc.backbuffer_count)
4821 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4823 view_desc.format_id = back_buffer->format->id;
4824 view_desc.flags = 0;
4825 view_desc.u.texture.level_idx = 0;
4826 view_desc.u.texture.level_count = 1;
4827 view_desc.u.texture.layer_idx = 0;
4828 view_desc.u.texture.layer_count = 1;
4829 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4830 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4832 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4833 return hr;
4837 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4839 if (reset_state)
4841 TRACE("Resetting stateblock.\n");
4842 if (device->recording)
4844 wined3d_stateblock_decref(device->recording);
4845 device->recording = NULL;
4847 wined3d_cs_emit_reset_state(device->cs);
4848 state_cleanup(&device->state);
4850 if (device->d3d_initialized)
4851 delete_opengl_contexts(device, swapchain);
4853 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4854 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4855 device->update_state = &device->state;
4857 device_init_swapchain_state(device, swapchain);
4859 else if (device->back_buffer_view)
4861 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4862 struct wined3d_state *state = &device->state;
4864 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4866 /* Note the min_z / max_z is not reset. */
4867 state->viewport.x = 0;
4868 state->viewport.y = 0;
4869 state->viewport.width = view->width;
4870 state->viewport.height = view->height;
4871 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4873 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4874 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4877 if (device->d3d_initialized)
4879 if (reset_state)
4880 hr = create_primary_opengl_context(device, swapchain);
4881 swapchain_update_swap_interval(swapchain);
4884 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4885 * first use
4887 return hr;
4890 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4892 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4894 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4896 return WINED3D_OK;
4900 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4901 struct wined3d_device_creation_parameters *parameters)
4903 TRACE("device %p, parameters %p.\n", device, parameters);
4905 *parameters = device->create_parms;
4908 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4910 TRACE("device %p.\n", device);
4912 return device->wined3d;
4915 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4916 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4918 struct wined3d_swapchain *swapchain;
4920 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4921 device, swapchain_idx, flags, ramp);
4923 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4924 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4927 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4928 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4930 struct wined3d_swapchain *swapchain;
4932 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4933 device, swapchain_idx, ramp);
4935 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4936 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4939 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4941 TRACE("device %p, resource %p.\n", device, resource);
4943 list_add_head(&device->resources, &resource->resource_list_entry);
4946 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4948 TRACE("device %p, resource %p.\n", device, resource);
4950 list_remove(&resource->resource_list_entry);
4953 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4955 enum wined3d_resource_type type = resource->type;
4956 struct wined3d_rendertarget_view *rtv;
4957 unsigned int i;
4959 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4961 if (device->d3d_initialized)
4963 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4965 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4966 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4969 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4970 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4973 switch (type)
4975 case WINED3D_RTYPE_TEXTURE_2D:
4976 case WINED3D_RTYPE_TEXTURE_3D:
4977 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4979 struct wined3d_texture *texture = texture_from_resource(resource);
4981 if (device->state.textures[i] == texture)
4983 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4984 device->state.textures[i] = NULL;
4987 if (device->recording && device->update_state->textures[i] == texture)
4989 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4990 texture, device->recording, i);
4991 device->update_state->textures[i] = NULL;
4994 break;
4996 case WINED3D_RTYPE_BUFFER:
4998 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5000 for (i = 0; i < MAX_STREAMS; ++i)
5002 if (device->state.streams[i].buffer == buffer)
5004 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
5005 device->state.streams[i].buffer = NULL;
5008 if (device->recording && device->update_state->streams[i].buffer == buffer)
5010 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5011 buffer, device->recording, i);
5012 device->update_state->streams[i].buffer = NULL;
5016 if (device->state.index_buffer == buffer)
5018 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5019 device->state.index_buffer = NULL;
5022 if (device->recording && device->update_state->index_buffer == buffer)
5024 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5025 buffer, device->recording);
5026 device->update_state->index_buffer = NULL;
5029 break;
5031 default:
5032 break;
5035 /* Remove the resource from the resourceStore */
5036 device_resource_remove(device, resource);
5038 TRACE("Resource released.\n");
5041 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5043 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5045 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5048 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5049 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5050 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5052 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5053 const struct fragment_pipeline *fragment_pipeline;
5054 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5055 unsigned int i;
5056 HRESULT hr;
5058 device->ref = 1;
5059 device->wined3d = wined3d;
5060 wined3d_incref(device->wined3d);
5061 device->adapter = wined3d->adapter_count ? adapter : NULL;
5062 device->device_parent = device_parent;
5063 list_init(&device->resources);
5064 list_init(&device->shaders);
5065 device->surface_alignment = surface_alignment;
5067 /* Save the creation parameters. */
5068 device->create_parms.adapter_idx = adapter_idx;
5069 device->create_parms.device_type = device_type;
5070 device->create_parms.focus_window = focus_window;
5071 device->create_parms.flags = flags;
5073 device->shader_backend = adapter->shader_backend;
5075 vertex_pipeline = adapter->vertex_pipe;
5077 fragment_pipeline = adapter->fragment_pipe;
5079 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5081 if (vertex_pipeline->vp_states && fragment_pipeline->states
5082 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5083 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5084 fragment_pipeline, misc_state_template)))
5086 ERR("Failed to compile state table, hr %#x.\n", hr);
5087 wine_rb_destroy(&device->samplers, NULL, NULL);
5088 wined3d_decref(device->wined3d);
5089 return hr;
5092 device->blitter = adapter->blitter;
5094 state_init(&device->state, &device->fb, &adapter->gl_info,
5095 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5096 device->update_state = &device->state;
5098 if (!(device->cs = wined3d_cs_create(device)))
5100 WARN("Failed to create command stream.\n");
5101 state_cleanup(&device->state);
5102 hr = E_FAIL;
5103 goto err;
5106 return WINED3D_OK;
5108 err:
5109 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5111 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5113 wine_rb_destroy(&device->samplers, NULL, NULL);
5114 wined3d_decref(device->wined3d);
5115 return hr;
5119 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5121 DWORD rep = device->StateTable[state].representative;
5122 struct wined3d_context *context;
5123 DWORD idx;
5124 BYTE shift;
5125 UINT i;
5127 for (i = 0; i < device->context_count; ++i)
5129 context = device->contexts[i];
5130 if(isStateDirty(context, rep)) continue;
5132 context->dirtyArray[context->numDirtyEntries++] = rep;
5133 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5134 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5135 context->isStateDirty[idx] |= (1u << shift);
5139 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5140 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5142 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5144 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5145 window, message, wparam, lparam);
5146 if (unicode)
5147 return DefWindowProcW(window, message, wparam, lparam);
5148 else
5149 return DefWindowProcA(window, message, wparam, lparam);
5152 if (message == WM_DESTROY)
5154 TRACE("unregister window %p.\n", window);
5155 wined3d_unregister_window(window);
5157 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5158 ERR("Window %p is not the focus window for device %p.\n", window, device);
5160 else if (message == WM_DISPLAYCHANGE)
5162 device->device_parent->ops->mode_changed(device->device_parent);
5164 else if (message == WM_ACTIVATEAPP)
5166 UINT i;
5168 for (i = 0; i < device->swapchain_count; i++)
5169 wined3d_swapchain_activate(device->swapchains[i], wparam);
5171 device->device_parent->ops->activate(device->device_parent, wparam);
5173 else if (message == WM_SYSCOMMAND)
5175 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5177 if (unicode)
5178 DefWindowProcW(window, message, wparam, lparam);
5179 else
5180 DefWindowProcA(window, message, wparam, lparam);
5184 if (unicode)
5185 return CallWindowProcW(proc, window, message, wparam, lparam);
5186 else
5187 return CallWindowProcA(proc, window, message, wparam, lparam);