wined3d: Store vertex shader integer constants as wined3d_ivec4 structures.
[wine.git] / dlls / wined3d / device.c
blobab87956a5c8e9fd54a7abbb940f7649ac1f6aff9
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_surface *target = rt_count ? wined3d_rendertarget_view_get_surface(fb->render_targets[0]) : NULL;
295 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
296 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
297 const struct wined3d_state *state = &device->state;
298 const struct wined3d_gl_info *gl_info;
299 UINT drawable_width, drawable_height;
300 struct wined3d_color corrected_color;
301 struct wined3d_context *context;
302 GLbitfield clear_mask = 0;
303 BOOL render_offscreen;
304 unsigned int i;
305 RECT ds_rect = {0};
307 context = context_acquire(device, target);
308 if (!context->valid)
310 context_release(context);
311 WARN("Invalid context, skipping clear.\n");
312 return;
314 gl_info = context->gl_info;
316 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
317 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
318 * for the cleared parts, and the untouched parts.
320 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
321 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
322 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
323 * checking all this if the dest surface is in the drawable anyway. */
324 for (i = 0; i < rt_count; ++i)
326 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
327 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(rtv);
329 if (rt && rtv->format->id != WINED3DFMT_NULL)
331 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
332 surface_load_location(rt, context, rtv->resource->draw_binding);
333 else
334 wined3d_texture_prepare_location(rt->container, rtv->sub_resource_idx,
335 context, rtv->resource->draw_binding);
339 if (target)
341 render_offscreen = context->render_offscreen;
342 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
344 else
346 render_offscreen = TRUE;
347 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
348 depth_stencil->texture_level);
349 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
350 depth_stencil->texture_level);
353 if (depth_stencil && render_offscreen)
354 wined3d_texture_prepare_location(depth_stencil->container,
355 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
357 if (flags & WINED3DCLEAR_ZBUFFER)
359 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
361 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
362 device_switch_onscreen_ds(device, context, depth_stencil);
363 prepare_ds_clear(depth_stencil, context, location,
364 draw_rect, rect_count, clear_rect, &ds_rect);
367 if (!context_apply_clear_state(context, state, rt_count, fb))
369 context_release(context);
370 WARN("Failed to apply clear state, skipping clear.\n");
371 return;
374 /* Only set the values up once, as they are not changing. */
375 if (flags & WINED3DCLEAR_STENCIL)
377 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
379 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
380 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
382 gl_info->gl_ops.gl.p_glStencilMask(~0U);
383 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
384 gl_info->gl_ops.gl.p_glClearStencil(stencil);
385 checkGLcall("glClearStencil");
386 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
389 if (flags & WINED3DCLEAR_ZBUFFER)
391 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
393 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
395 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
396 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
397 gl_info->gl_ops.gl.p_glClearDepth(depth);
398 checkGLcall("glClearDepth");
399 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
402 if (flags & WINED3DCLEAR_TARGET)
404 for (i = 0; i < rt_count; ++i)
406 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
407 struct wined3d_texture *texture;
409 if (!rtv)
410 continue;
412 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
414 FIXME("Not supported on buffer resources.\n");
415 continue;
418 texture = texture_from_resource(rtv->resource);
419 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
420 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
423 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
425 if (rt_count > 1)
426 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
427 "support, this might cause graphical issues.\n");
429 corrected_color.r = color->r < wined3d_srgb_const1[0]
430 ? color->r * wined3d_srgb_const0[3]
431 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
432 - wined3d_srgb_const0[2];
433 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
434 corrected_color.g = color->g < wined3d_srgb_const1[0]
435 ? color->g * wined3d_srgb_const0[3]
436 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
437 - wined3d_srgb_const0[2];
438 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
439 corrected_color.b = color->b < wined3d_srgb_const1[0]
440 ? color->b * wined3d_srgb_const0[3]
441 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
442 - wined3d_srgb_const0[2];
443 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
444 color = &corrected_color;
447 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
448 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
449 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
450 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
451 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
452 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
453 checkGLcall("glClearColor");
454 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
457 if (!rect_count)
459 if (render_offscreen)
461 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
462 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
464 else
466 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
467 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
469 checkGLcall("glScissor");
470 gl_info->gl_ops.gl.p_glClear(clear_mask);
471 checkGLcall("glClear");
473 else
475 RECT current_rect;
477 /* Now process each rect in turn. */
478 for (i = 0; i < rect_count; ++i)
480 /* Note that GL uses lower left, width/height. */
481 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
483 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
484 wine_dbgstr_rect(&clear_rect[i]),
485 wine_dbgstr_rect(&current_rect));
487 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
488 * The rectangle is not cleared, no error is returned, but further rectangles are
489 * still cleared if they are valid. */
490 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
492 TRACE("Rectangle with negative dimensions, ignoring.\n");
493 continue;
496 if (render_offscreen)
498 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
499 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
501 else
503 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
504 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
506 checkGLcall("glScissor");
508 gl_info->gl_ops.gl.p_glClear(clear_mask);
509 checkGLcall("glClear");
513 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
514 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
515 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
517 context_release(context);
520 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
522 ULONG refcount = InterlockedIncrement(&device->ref);
524 TRACE("%p increasing refcount to %u.\n", device, refcount);
526 return refcount;
529 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
531 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
533 ERR("Leftover sampler %p.\n", sampler);
536 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
538 ULONG refcount = InterlockedDecrement(&device->ref);
540 TRACE("%p decreasing refcount to %u.\n", device, refcount);
542 if (!refcount)
544 UINT i;
546 wined3d_cs_destroy(device->cs);
548 if (device->recording && wined3d_stateblock_decref(device->recording))
549 FIXME("Something's still holding the recording stateblock.\n");
550 device->recording = NULL;
552 state_cleanup(&device->state);
554 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
556 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
557 device->multistate_funcs[i] = NULL;
560 if (!list_empty(&device->resources))
562 struct wined3d_resource *resource;
564 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
566 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
568 FIXME("Leftover resource %p with type %s (%#x).\n",
569 resource, debug_d3dresourcetype(resource->type), resource->type);
573 if (device->contexts)
574 ERR("Context array not freed!\n");
575 if (device->hardwareCursor)
576 DestroyCursor(device->hardwareCursor);
577 device->hardwareCursor = 0;
579 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
581 wined3d_decref(device->wined3d);
582 device->wined3d = NULL;
583 HeapFree(GetProcessHeap(), 0, device);
584 TRACE("Freed device %p.\n", device);
587 return refcount;
590 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
592 TRACE("device %p.\n", device);
594 return device->swapchain_count;
597 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
599 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
601 if (swapchain_idx >= device->swapchain_count)
603 WARN("swapchain_idx %u >= swapchain_count %u.\n",
604 swapchain_idx, device->swapchain_count);
605 return NULL;
608 return device->swapchains[swapchain_idx];
611 static void device_load_logo(struct wined3d_device *device, const char *filename)
613 struct wined3d_color_key color_key;
614 struct wined3d_resource_desc desc;
615 HBITMAP hbm;
616 BITMAP bm;
617 HRESULT hr;
618 HDC dcb = NULL, dcs = NULL;
620 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
621 if(hbm)
623 GetObjectA(hbm, sizeof(BITMAP), &bm);
624 dcb = CreateCompatibleDC(NULL);
625 if(!dcb) goto out;
626 SelectObject(dcb, hbm);
628 else
630 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
631 * couldn't be loaded
633 memset(&bm, 0, sizeof(bm));
634 bm.bmWidth = 32;
635 bm.bmHeight = 32;
638 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
639 desc.format = WINED3DFMT_B5G6R5_UNORM;
640 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
641 desc.multisample_quality = 0;
642 desc.usage = WINED3DUSAGE_DYNAMIC;
643 desc.pool = WINED3D_POOL_DEFAULT;
644 desc.width = bm.bmWidth;
645 desc.height = bm.bmHeight;
646 desc.depth = 1;
647 desc.size = 0;
648 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
649 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
651 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
652 goto out;
655 if (dcb)
657 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
658 goto out;
659 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
660 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
662 color_key.color_space_low_value = 0;
663 color_key.color_space_high_value = 0;
664 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
666 else
668 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
669 const RECT rect = {0, 0, desc.width, desc.height};
670 struct wined3d_surface *surface;
672 /* Fill the surface with a white color to show that wined3d is there */
673 surface = device->logo_texture->sub_resources[0].u.surface;
674 surface_color_fill(surface, &rect, &c);
677 out:
678 if (dcb) DeleteDC(dcb);
679 if (hbm) DeleteObject(hbm);
682 /* Context activation is done by the caller. */
683 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
685 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
686 unsigned int i, j, count;
687 /* Under DirectX you can sample even if no texture is bound, whereas
688 * OpenGL will only allow that when a valid texture is bound.
689 * We emulate this by creating dummy textures and binding them
690 * to each texture stage when the currently set D3D texture is NULL. */
692 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
693 for (i = 0; i < count; ++i)
695 static const DWORD d3d10_color = 0x00000000;
696 static const DWORD color = 0x000000ff;
698 /* Make appropriate texture active */
699 context_active_texture(context, gl_info, i);
701 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
702 checkGLcall("glGenTextures");
703 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
705 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
706 checkGLcall("glBindTexture");
708 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
709 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
710 checkGLcall("glTexImage2D");
712 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
714 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
715 checkGLcall("glGenTextures");
716 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
718 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
719 checkGLcall("glBindTexture");
721 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
722 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
723 checkGLcall("glTexImage2D");
726 if (gl_info->supported[EXT_TEXTURE3D])
728 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
729 checkGLcall("glGenTextures");
730 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
732 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
733 checkGLcall("glBindTexture");
735 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
736 checkGLcall("glTexImage3D");
739 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
741 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
742 checkGLcall("glGenTextures");
743 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
745 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
746 checkGLcall("glBindTexture");
748 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
750 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
751 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
752 checkGLcall("glTexImage2D");
756 if (gl_info->supported[EXT_TEXTURE_ARRAY])
758 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d_array[i]);
759 checkGLcall("glGenTextures");
760 TRACE("Dummy 2D array texture %u given name %u.\n", i, device->dummy_texture_2d_array[i]);
762 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_texture_2d_array[i]);
763 checkGLcall("glBindTexture");
765 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
766 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &d3d10_color));
767 checkGLcall("glTexImage3D");
772 /* Context activation is done by the caller. */
773 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
775 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
777 if (gl_info->supported[EXT_TEXTURE_ARRAY])
779 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d_array);
780 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d_array)");
783 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
785 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
786 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
789 if (gl_info->supported[EXT_TEXTURE3D])
791 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
792 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
795 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
797 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
798 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
801 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
802 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
804 memset(device->dummy_texture_2d_array, 0, count * sizeof(*device->dummy_texture_2d_array));
805 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
806 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
807 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
808 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
811 /* Context activation is done by the caller. */
812 static void create_default_sampler(struct wined3d_device *device)
814 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
817 * In SM4+ shaders there is a separation between resources and samplers. Some of shader
818 * instructions allow to access resources without using samplers.
819 * In GLSL resources are always accessed through sampler or image variables. The default
820 * sampler object is used to emulate the direct resource access when there is no sampler state
821 * to use.
824 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
826 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
827 checkGLcall("glGenSamplers");
828 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
829 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
830 checkGLcall("glSamplerParameteri");
832 else
834 device->default_sampler = 0;
838 /* Context activation is done by the caller. */
839 static void destroy_default_sampler(struct wined3d_device *device)
841 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
843 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
845 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
846 checkGLcall("glDeleteSamplers");
849 device->default_sampler = 0;
852 static LONG fullscreen_style(LONG style)
854 /* Make sure the window is managed, otherwise we won't get keyboard input. */
855 style |= WS_POPUP | WS_SYSMENU;
856 style &= ~(WS_CAPTION | WS_THICKFRAME);
858 return style;
861 static LONG fullscreen_exstyle(LONG exstyle)
863 /* Filter out window decorations. */
864 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
866 return exstyle;
869 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
871 BOOL filter_messages;
872 LONG style, exstyle;
874 TRACE("Setting up window %p for fullscreen mode.\n", window);
876 if (device->style || device->exStyle)
878 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
879 window, device->style, device->exStyle);
882 device->style = GetWindowLongW(window, GWL_STYLE);
883 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
885 style = fullscreen_style(device->style);
886 exstyle = fullscreen_exstyle(device->exStyle);
888 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
889 device->style, device->exStyle, style, exstyle);
891 filter_messages = device->filter_messages;
892 device->filter_messages = TRUE;
894 SetWindowLongW(window, GWL_STYLE, style);
895 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
896 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
898 device->filter_messages = filter_messages;
901 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
903 BOOL filter_messages;
904 LONG style, exstyle;
906 if (!device->style && !device->exStyle) 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);
935 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
937 device->filter_messages = filter_messages;
939 /* Delete the old values. */
940 device->style = 0;
941 device->exStyle = 0;
944 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
946 TRACE("device %p, window %p.\n", device, window);
948 if (!wined3d_register_window(window, device))
950 ERR("Failed to register window %p.\n", window);
951 return E_FAIL;
954 InterlockedExchangePointer((void **)&device->focus_window, window);
955 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
957 return WINED3D_OK;
960 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
962 TRACE("device %p.\n", device);
964 if (device->focus_window) wined3d_unregister_window(device->focus_window);
965 InterlockedExchangePointer((void **)&device->focus_window, NULL);
968 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
970 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
971 unsigned int i;
973 if (device->fb.render_targets)
975 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
977 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
979 if (device->back_buffer_view)
980 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
983 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
984 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
987 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
988 struct wined3d_swapchain_desc *swapchain_desc)
990 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
991 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
992 struct wined3d_swapchain *swapchain = NULL;
993 struct wined3d_context *context;
994 DWORD clear_flags = 0;
995 HRESULT hr;
997 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
999 if (device->d3d_initialized)
1000 return WINED3DERR_INVALIDCALL;
1001 if (device->wined3d->flags & WINED3D_NO3D)
1002 return WINED3DERR_INVALIDCALL;
1004 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1005 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1007 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1008 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1010 TRACE("Shader private data couldn't be allocated\n");
1011 goto err_out;
1013 if (FAILED(hr = device->blitter->alloc_private(device)))
1015 TRACE("Blitter private data couldn't be allocated\n");
1016 goto err_out;
1019 /* Setup the implicit swapchain. This also initializes a context. */
1020 TRACE("Creating implicit swapchain\n");
1021 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1022 swapchain_desc, &swapchain);
1023 if (FAILED(hr))
1025 WARN("Failed to create implicit swapchain\n");
1026 goto err_out;
1029 if (swapchain_desc->backbuffer_count)
1031 struct wined3d_rendertarget_view_desc view_desc;
1033 view_desc.format_id = swapchain_desc->backbuffer_format;
1034 view_desc.u.texture.level_idx = 0;
1035 view_desc.u.texture.layer_idx = 0;
1036 view_desc.u.texture.layer_count = 1;
1037 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
1038 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1040 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1041 goto err_out;
1045 device->swapchain_count = 1;
1046 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1047 if (!device->swapchains)
1049 ERR("Out of memory!\n");
1050 goto err_out;
1052 device->swapchains[0] = swapchain;
1053 device_init_swapchain_state(device, swapchain);
1055 context = context_acquire(device, swapchain->front_buffer->sub_resources[0].u.surface);
1057 create_dummy_textures(device, context);
1058 create_default_sampler(device);
1060 device->contexts[0]->last_was_rhw = 0;
1062 TRACE("All defaults now set up, leaving 3D init.\n");
1064 context_release(context);
1066 /* Clear the screen */
1067 if (swapchain->back_buffers && swapchain->back_buffers[0])
1068 clear_flags |= WINED3DCLEAR_TARGET;
1069 if (swapchain_desc->enable_auto_depth_stencil)
1070 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1071 if (clear_flags)
1072 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1074 device->d3d_initialized = TRUE;
1076 if (wined3d_settings.logo)
1077 device_load_logo(device, wined3d_settings.logo);
1078 return WINED3D_OK;
1080 err_out:
1081 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1082 HeapFree(GetProcessHeap(), 0, device->swapchains);
1083 device->swapchain_count = 0;
1084 if (device->back_buffer_view)
1085 wined3d_rendertarget_view_decref(device->back_buffer_view);
1086 if (swapchain)
1087 wined3d_swapchain_decref(swapchain);
1088 if (device->blit_priv)
1089 device->blitter->free_private(device);
1090 if (device->shader_priv)
1091 device->shader_backend->shader_free_private(device);
1093 return hr;
1096 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1097 struct wined3d_swapchain_desc *swapchain_desc)
1099 struct wined3d_swapchain *swapchain = NULL;
1100 HRESULT hr;
1102 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1104 /* Setup the implicit swapchain */
1105 TRACE("Creating implicit swapchain\n");
1106 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1107 swapchain_desc, &swapchain);
1108 if (FAILED(hr))
1110 WARN("Failed to create implicit swapchain\n");
1111 goto err_out;
1114 device->swapchain_count = 1;
1115 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1116 if (!device->swapchains)
1118 ERR("Out of memory!\n");
1119 goto err_out;
1121 device->swapchains[0] = swapchain;
1122 return WINED3D_OK;
1124 err_out:
1125 wined3d_swapchain_decref(swapchain);
1126 return hr;
1129 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1131 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1133 wined3d_sampler_decref(sampler);
1136 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1138 struct wined3d_resource *resource, *cursor;
1139 const struct wined3d_gl_info *gl_info;
1140 struct wined3d_context *context;
1141 struct wined3d_surface *surface;
1142 UINT i;
1144 TRACE("device %p.\n", device);
1146 if (!device->d3d_initialized)
1147 return WINED3DERR_INVALIDCALL;
1149 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1150 * it was created. Thus make sure a context is active for the glDelete* calls
1152 context = context_acquire(device, NULL);
1153 gl_info = context->gl_info;
1155 if (device->logo_texture)
1156 wined3d_texture_decref(device->logo_texture);
1157 if (device->cursor_texture)
1158 wined3d_texture_decref(device->cursor_texture);
1160 state_unbind_resources(&device->state);
1162 /* Unload resources */
1163 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1165 TRACE("Unloading resource %p.\n", resource);
1166 resource->resource_ops->resource_unload(resource);
1169 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1171 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1172 * private data, it might contain opengl pointers
1174 if (device->depth_blt_texture)
1176 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1177 device->depth_blt_texture = 0;
1180 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1181 device->blitter->free_private(device);
1182 device->shader_backend->shader_free_private(device);
1183 destroy_dummy_textures(device, gl_info);
1184 destroy_default_sampler(device);
1186 /* Release the context again as soon as possible. In particular,
1187 * releasing the render target views below may release the last reference
1188 * to the swapchain associated with this context, which in turn will
1189 * destroy the context. */
1190 context_release(context);
1192 /* Release the buffers (with sanity checks)*/
1193 if (device->onscreen_depth_stencil)
1195 surface = device->onscreen_depth_stencil;
1196 device->onscreen_depth_stencil = NULL;
1197 wined3d_texture_decref(surface->container);
1200 if (device->fb.depth_stencil)
1202 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1204 TRACE("Releasing depth/stencil view %p.\n", view);
1206 device->fb.depth_stencil = NULL;
1207 wined3d_rendertarget_view_decref(view);
1210 if (device->auto_depth_stencil_view)
1212 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1214 device->auto_depth_stencil_view = NULL;
1215 if (wined3d_rendertarget_view_decref(view))
1216 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1219 for (i = 0; i < gl_info->limits.buffers; ++i)
1221 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1223 if (device->back_buffer_view)
1225 wined3d_rendertarget_view_decref(device->back_buffer_view);
1226 device->back_buffer_view = NULL;
1229 for (i = 0; i < device->swapchain_count; ++i)
1231 TRACE("Releasing the implicit swapchain %u.\n", i);
1232 if (wined3d_swapchain_decref(device->swapchains[i]))
1233 FIXME("Something's still holding the implicit swapchain.\n");
1236 HeapFree(GetProcessHeap(), 0, device->swapchains);
1237 device->swapchains = NULL;
1238 device->swapchain_count = 0;
1240 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1241 device->fb.render_targets = NULL;
1243 device->d3d_initialized = FALSE;
1245 return WINED3D_OK;
1248 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1250 unsigned int i;
1252 for (i = 0; i < device->swapchain_count; ++i)
1254 TRACE("Releasing the implicit swapchain %u.\n", i);
1255 if (wined3d_swapchain_decref(device->swapchains[i]))
1256 FIXME("Something's still holding the implicit swapchain.\n");
1259 HeapFree(GetProcessHeap(), 0, device->swapchains);
1260 device->swapchains = NULL;
1261 device->swapchain_count = 0;
1262 return WINED3D_OK;
1265 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1266 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1267 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1269 * There is no way to deactivate thread safety once it is enabled.
1271 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1273 TRACE("device %p.\n", device);
1275 /* For now just store the flag (needed in case of ddraw). */
1276 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1279 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1281 TRACE("device %p.\n", device);
1283 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1284 wine_dbgstr_longlong(device->adapter->vram_bytes),
1285 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1286 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1288 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1291 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1292 struct wined3d_buffer *buffer, UINT offset)
1294 struct wined3d_stream_output *stream;
1295 struct wined3d_buffer *prev_buffer;
1297 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1299 if (idx >= MAX_STREAM_OUT)
1301 WARN("Invalid stream output %u.\n", idx);
1302 return;
1305 stream = &device->update_state->stream_output[idx];
1306 prev_buffer = stream->buffer;
1308 if (buffer)
1309 wined3d_buffer_incref(buffer);
1310 stream->buffer = buffer;
1311 stream->offset = offset;
1312 if (!device->recording)
1313 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1314 if (prev_buffer)
1315 wined3d_buffer_decref(prev_buffer);
1318 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1319 UINT idx, UINT *offset)
1321 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1323 if (idx >= MAX_STREAM_OUT)
1325 WARN("Invalid stream output %u.\n", idx);
1326 return NULL;
1329 if (offset)
1330 *offset = device->state.stream_output[idx].offset;
1331 return device->state.stream_output[idx].buffer;
1334 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1335 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1337 struct wined3d_stream_state *stream;
1338 struct wined3d_buffer *prev_buffer;
1340 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1341 device, stream_idx, buffer, offset, stride);
1343 if (stream_idx >= MAX_STREAMS)
1345 WARN("Stream index %u out of range.\n", stream_idx);
1346 return WINED3DERR_INVALIDCALL;
1348 else if (offset & 0x3)
1350 WARN("Offset %u is not 4 byte aligned.\n", offset);
1351 return WINED3DERR_INVALIDCALL;
1354 stream = &device->update_state->streams[stream_idx];
1355 prev_buffer = stream->buffer;
1357 if (device->recording)
1358 device->recording->changed.streamSource |= 1u << stream_idx;
1360 if (prev_buffer == buffer
1361 && stream->stride == stride
1362 && stream->offset == offset)
1364 TRACE("Application is setting the old values over, nothing to do.\n");
1365 return WINED3D_OK;
1368 stream->buffer = buffer;
1369 if (buffer)
1371 stream->stride = stride;
1372 stream->offset = offset;
1373 wined3d_buffer_incref(buffer);
1376 if (!device->recording)
1377 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1378 if (prev_buffer)
1379 wined3d_buffer_decref(prev_buffer);
1381 return WINED3D_OK;
1384 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1385 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1387 const struct wined3d_stream_state *stream;
1389 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1390 device, stream_idx, buffer, offset, stride);
1392 if (stream_idx >= MAX_STREAMS)
1394 WARN("Stream index %u out of range.\n", stream_idx);
1395 return WINED3DERR_INVALIDCALL;
1398 stream = &device->state.streams[stream_idx];
1399 *buffer = stream->buffer;
1400 if (offset)
1401 *offset = stream->offset;
1402 *stride = stream->stride;
1404 return WINED3D_OK;
1407 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1409 struct wined3d_stream_state *stream;
1410 UINT old_flags, old_freq;
1412 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1414 /* Verify input. At least in d3d9 this is invalid. */
1415 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1417 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1418 return WINED3DERR_INVALIDCALL;
1420 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1422 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1423 return WINED3DERR_INVALIDCALL;
1425 if (!divider)
1427 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1428 return WINED3DERR_INVALIDCALL;
1431 stream = &device->update_state->streams[stream_idx];
1432 old_flags = stream->flags;
1433 old_freq = stream->frequency;
1435 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1436 stream->frequency = divider & 0x7fffff;
1438 if (device->recording)
1439 device->recording->changed.streamFreq |= 1u << stream_idx;
1440 else if (stream->frequency != old_freq || stream->flags != old_flags)
1441 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1443 return WINED3D_OK;
1446 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1447 UINT stream_idx, UINT *divider)
1449 const struct wined3d_stream_state *stream;
1451 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1453 stream = &device->state.streams[stream_idx];
1454 *divider = stream->flags | stream->frequency;
1456 TRACE("Returning %#x.\n", *divider);
1458 return WINED3D_OK;
1461 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1462 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1464 TRACE("device %p, state %s, matrix %p.\n",
1465 device, debug_d3dtstype(d3dts), matrix);
1466 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1467 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1468 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1469 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1471 /* Handle recording of state blocks. */
1472 if (device->recording)
1474 TRACE("Recording... not performing anything.\n");
1475 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1476 device->update_state->transforms[d3dts] = *matrix;
1477 return;
1480 /* If the new matrix is the same as the current one,
1481 * we cut off any further processing. this seems to be a reasonable
1482 * optimization because as was noticed, some apps (warcraft3 for example)
1483 * tend towards setting the same matrix repeatedly for some reason.
1485 * From here on we assume that the new matrix is different, wherever it matters. */
1486 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1488 TRACE("The application is setting the same matrix over again.\n");
1489 return;
1492 device->state.transforms[d3dts] = *matrix;
1493 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1496 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1497 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1499 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1501 *matrix = device->state.transforms[state];
1504 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1505 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1507 const struct wined3d_matrix *mat;
1508 struct wined3d_matrix temp;
1510 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1512 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1513 * below means it will be recorded in a state block change, but it
1514 * works regardless where it is recorded.
1515 * If this is found to be wrong, change to StateBlock. */
1516 if (state > HIGHEST_TRANSFORMSTATE)
1518 WARN("Unhandled transform state %#x.\n", state);
1519 return;
1522 mat = &device->update_state->transforms[state];
1523 multiply_matrix(&temp, mat, matrix);
1525 /* Apply change via set transform - will reapply to eg. lights this way. */
1526 wined3d_device_set_transform(device, state, &temp);
1529 /* Note lights are real special cases. Although the device caps state only
1530 * e.g. 8 are supported, you can reference any indexes you want as long as
1531 * that number max are enabled at any one point in time. Therefore since the
1532 * indices can be anything, we need a hashmap of them. However, this causes
1533 * stateblock problems. When capturing the state block, I duplicate the
1534 * hashmap, but when recording, just build a chain pretty much of commands to
1535 * be replayed. */
1536 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1537 UINT light_idx, const struct wined3d_light *light)
1539 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1540 struct wined3d_light_info *object = NULL;
1541 struct list *e;
1542 float rho;
1544 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1546 /* Check the parameter range. Need for speed most wanted sets junk lights
1547 * which confuse the GL driver. */
1548 if (!light)
1549 return WINED3DERR_INVALIDCALL;
1551 switch (light->type)
1553 case WINED3D_LIGHT_POINT:
1554 case WINED3D_LIGHT_SPOT:
1555 case WINED3D_LIGHT_GLSPOT:
1556 /* Incorrect attenuation values can cause the gl driver to crash.
1557 * Happens with Need for speed most wanted. */
1558 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1560 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1561 return WINED3DERR_INVALIDCALL;
1563 break;
1565 case WINED3D_LIGHT_DIRECTIONAL:
1566 case WINED3D_LIGHT_PARALLELPOINT:
1567 /* Ignores attenuation */
1568 break;
1570 default:
1571 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1572 return WINED3DERR_INVALIDCALL;
1575 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1577 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1578 if (object->OriginalIndex == light_idx)
1579 break;
1580 object = NULL;
1583 if (!object)
1585 TRACE("Adding new light\n");
1586 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1587 if (!object)
1588 return E_OUTOFMEMORY;
1590 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1591 object->glIndex = -1;
1592 object->OriginalIndex = light_idx;
1595 /* Initialize the object. */
1596 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1597 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1598 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1599 light_idx, light->type, debug_color(&light->diffuse),
1600 debug_color(&light->specular), debug_color(&light->ambient),
1601 light->position.x, light->position.y, light->position.z,
1602 light->direction.x, light->direction.y, light->direction.z,
1603 light->range, light->falloff, light->theta, light->phi);
1605 /* Update the live definitions if the light is currently assigned a glIndex. */
1606 if (object->glIndex != -1 && !device->recording)
1608 if (object->OriginalParms.type != light->type)
1609 device_invalidate_state(device, STATE_LIGHT_TYPE);
1610 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1613 /* Save away the information. */
1614 object->OriginalParms = *light;
1616 switch (light->type)
1618 case WINED3D_LIGHT_POINT:
1619 /* Position */
1620 object->position.x = light->position.x;
1621 object->position.y = light->position.y;
1622 object->position.z = light->position.z;
1623 object->position.w = 1.0f;
1624 object->cutoff = 180.0f;
1625 /* FIXME: Range */
1626 break;
1628 case WINED3D_LIGHT_DIRECTIONAL:
1629 /* Direction */
1630 object->direction.x = -light->direction.x;
1631 object->direction.y = -light->direction.y;
1632 object->direction.z = -light->direction.z;
1633 object->direction.w = 0.0f;
1634 object->exponent = 0.0f;
1635 object->cutoff = 180.0f;
1636 break;
1638 case WINED3D_LIGHT_SPOT:
1639 /* Position */
1640 object->position.x = light->position.x;
1641 object->position.y = light->position.y;
1642 object->position.z = light->position.z;
1643 object->position.w = 1.0f;
1645 /* Direction */
1646 object->direction.x = light->direction.x;
1647 object->direction.y = light->direction.y;
1648 object->direction.z = light->direction.z;
1649 object->direction.w = 0.0f;
1651 /* opengl-ish and d3d-ish spot lights use too different models
1652 * for the light "intensity" as a function of the angle towards
1653 * the main light direction, so we only can approximate very
1654 * roughly. However, spot lights are rather rarely used in games
1655 * (if ever used at all). Furthermore if still used, probably
1656 * nobody pays attention to such details. */
1657 if (!light->falloff)
1659 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1660 * equations have the falloff resp. exponent parameter as an
1661 * exponent, so the spot light lighting will always be 1.0 for
1662 * both of them, and we don't have to care for the rest of the
1663 * rather complex calculation. */
1664 object->exponent = 0.0f;
1666 else
1668 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1669 if (rho < 0.0001f)
1670 rho = 0.0001f;
1671 object->exponent = -0.3f / logf(cosf(rho / 2));
1674 if (object->exponent > 128.0f)
1675 object->exponent = 128.0f;
1677 object->cutoff = (float)(light->phi * 90 / M_PI);
1678 /* FIXME: Range */
1679 break;
1681 case WINED3D_LIGHT_PARALLELPOINT:
1682 object->position.x = light->position.x;
1683 object->position.y = light->position.y;
1684 object->position.z = light->position.z;
1685 object->position.w = 1.0f;
1686 break;
1688 default:
1689 FIXME("Unrecognized light type %#x.\n", light->type);
1692 return WINED3D_OK;
1695 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1696 UINT light_idx, struct wined3d_light *light)
1698 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1699 struct wined3d_light_info *light_info = NULL;
1700 struct list *e;
1702 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1704 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1706 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1707 if (light_info->OriginalIndex == light_idx)
1708 break;
1709 light_info = NULL;
1712 if (!light_info)
1714 TRACE("Light information requested but light not defined\n");
1715 return WINED3DERR_INVALIDCALL;
1718 *light = light_info->OriginalParms;
1719 return WINED3D_OK;
1722 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1724 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1725 struct wined3d_light_info *light_info = NULL;
1726 struct list *e;
1728 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1730 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1732 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1733 if (light_info->OriginalIndex == light_idx)
1734 break;
1735 light_info = NULL;
1737 TRACE("Found light %p.\n", light_info);
1739 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1740 if (!light_info)
1742 TRACE("Light enabled requested but light not defined, so defining one!\n");
1743 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1745 /* Search for it again! Should be fairly quick as near head of list. */
1746 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1748 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1749 if (light_info->OriginalIndex == light_idx)
1750 break;
1751 light_info = NULL;
1753 if (!light_info)
1755 FIXME("Adding default lights has failed dismally\n");
1756 return WINED3DERR_INVALIDCALL;
1760 if (!enable)
1762 if (light_info->glIndex != -1)
1764 if (!device->recording)
1766 device_invalidate_state(device, STATE_LIGHT_TYPE);
1767 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1770 device->update_state->lights[light_info->glIndex] = NULL;
1771 light_info->glIndex = -1;
1773 else
1775 TRACE("Light already disabled, nothing to do\n");
1777 light_info->enabled = FALSE;
1779 else
1781 light_info->enabled = TRUE;
1782 if (light_info->glIndex != -1)
1784 TRACE("Nothing to do as light was enabled\n");
1786 else
1788 unsigned int i;
1789 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1790 /* Find a free GL light. */
1791 for (i = 0; i < gl_info->limits.lights; ++i)
1793 if (!device->update_state->lights[i])
1795 device->update_state->lights[i] = light_info;
1796 light_info->glIndex = i;
1797 break;
1800 if (light_info->glIndex == -1)
1802 /* Our tests show that Windows returns D3D_OK in this situation, even with
1803 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1804 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1805 * as well for those lights.
1807 * TODO: Test how this affects rendering. */
1808 WARN("Too many concurrently active lights\n");
1809 return WINED3D_OK;
1812 /* i == light_info->glIndex */
1813 if (!device->recording)
1815 device_invalidate_state(device, STATE_LIGHT_TYPE);
1816 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1821 return WINED3D_OK;
1824 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1826 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1827 struct wined3d_light_info *light_info = NULL;
1828 struct list *e;
1830 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1832 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1834 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1835 if (light_info->OriginalIndex == light_idx)
1836 break;
1837 light_info = NULL;
1840 if (!light_info)
1842 TRACE("Light enabled state requested but light not defined.\n");
1843 return WINED3DERR_INVALIDCALL;
1845 /* true is 128 according to SetLightEnable */
1846 *enable = light_info->enabled ? 128 : 0;
1847 return WINED3D_OK;
1850 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1851 UINT plane_idx, const struct wined3d_vec4 *plane)
1853 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1855 /* Validate plane_idx. */
1856 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1858 TRACE("Application has requested clipplane this device doesn't support.\n");
1859 return WINED3DERR_INVALIDCALL;
1862 if (device->recording)
1863 device->recording->changed.clipplane |= 1u << plane_idx;
1865 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1867 TRACE("Application is setting old values over, nothing to do.\n");
1868 return WINED3D_OK;
1871 device->update_state->clip_planes[plane_idx] = *plane;
1873 if (!device->recording)
1874 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1876 return WINED3D_OK;
1879 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1880 UINT plane_idx, struct wined3d_vec4 *plane)
1882 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1884 /* Validate plane_idx. */
1885 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1887 TRACE("Application has requested clipplane this device doesn't support.\n");
1888 return WINED3DERR_INVALIDCALL;
1891 *plane = device->state.clip_planes[plane_idx];
1893 return WINED3D_OK;
1896 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1897 const struct wined3d_clip_status *clip_status)
1899 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1901 if (!clip_status)
1902 return WINED3DERR_INVALIDCALL;
1904 return WINED3D_OK;
1907 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1908 struct wined3d_clip_status *clip_status)
1910 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1912 if (!clip_status)
1913 return WINED3DERR_INVALIDCALL;
1915 return WINED3D_OK;
1918 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1920 TRACE("device %p, material %p.\n", device, material);
1922 device->update_state->material = *material;
1924 if (device->recording)
1925 device->recording->changed.material = TRUE;
1926 else
1927 wined3d_cs_emit_set_material(device->cs, material);
1930 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1932 TRACE("device %p, material %p.\n", device, material);
1934 *material = device->state.material;
1936 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1937 TRACE("ambient %s\n", debug_color(&material->ambient));
1938 TRACE("specular %s\n", debug_color(&material->specular));
1939 TRACE("emissive %s\n", debug_color(&material->emissive));
1940 TRACE("power %.8e.\n", material->power);
1943 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1944 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1946 enum wined3d_format_id prev_format;
1947 struct wined3d_buffer *prev_buffer;
1949 TRACE("device %p, buffer %p, format %s.\n",
1950 device, buffer, debug_d3dformat(format_id));
1952 prev_buffer = device->update_state->index_buffer;
1953 prev_format = device->update_state->index_format;
1955 device->update_state->index_buffer = buffer;
1956 device->update_state->index_format = format_id;
1958 if (device->recording)
1959 device->recording->changed.indices = TRUE;
1961 if (prev_buffer == buffer && prev_format == format_id)
1962 return;
1964 if (buffer)
1965 wined3d_buffer_incref(buffer);
1966 if (!device->recording)
1967 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1968 if (prev_buffer)
1969 wined3d_buffer_decref(prev_buffer);
1972 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1973 enum wined3d_format_id *format)
1975 TRACE("device %p, format %p.\n", device, format);
1977 *format = device->state.index_format;
1978 return device->state.index_buffer;
1981 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1983 TRACE("device %p, base_index %d.\n", device, base_index);
1985 device->update_state->base_vertex_index = base_index;
1988 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1990 TRACE("device %p.\n", device);
1992 return device->state.base_vertex_index;
1995 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1997 TRACE("device %p, viewport %p.\n", device, viewport);
1998 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1999 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2001 device->update_state->viewport = *viewport;
2003 /* Handle recording of state blocks */
2004 if (device->recording)
2006 TRACE("Recording... not performing anything\n");
2007 device->recording->changed.viewport = TRUE;
2008 return;
2011 wined3d_cs_emit_set_viewport(device->cs, viewport);
2014 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2016 TRACE("device %p, viewport %p.\n", device, viewport);
2018 *viewport = device->state.viewport;
2021 static void resolve_depth_buffer(struct wined3d_state *state)
2023 struct wined3d_texture *dst_texture = state->textures[0];
2024 struct wined3d_rendertarget_view *src_view;
2025 RECT src_rect, dst_rect;
2027 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2028 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2029 return;
2031 if (!(src_view = state->fb->depth_stencil))
2032 return;
2033 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2035 FIXME("Not supported on buffer resources.\n");
2036 return;
2039 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2040 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2041 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
2042 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2045 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2046 enum wined3d_render_state state, DWORD value)
2048 DWORD old_value;
2050 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2052 if (state > WINEHIGHEST_RENDER_STATE)
2054 WARN("Unhandled render state %#x.\n", state);
2055 return;
2058 old_value = device->state.render_states[state];
2059 device->update_state->render_states[state] = value;
2061 /* Handle recording of state blocks. */
2062 if (device->recording)
2064 TRACE("Recording... not performing anything.\n");
2065 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2066 return;
2069 /* Compared here and not before the assignment to allow proper stateblock recording. */
2070 if (value == old_value)
2071 TRACE("Application is setting the old value over, nothing to do.\n");
2072 else
2073 wined3d_cs_emit_set_render_state(device->cs, state, value);
2075 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2077 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2078 resolve_depth_buffer(&device->state);
2082 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2084 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2086 return device->state.render_states[state];
2089 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2090 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2092 DWORD old_value;
2094 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2095 device, sampler_idx, debug_d3dsamplerstate(state), value);
2097 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2098 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2100 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2102 WARN("Invalid sampler %u.\n", sampler_idx);
2103 return; /* Windows accepts overflowing this array ... we do not. */
2106 old_value = device->state.sampler_states[sampler_idx][state];
2107 device->update_state->sampler_states[sampler_idx][state] = value;
2109 /* Handle recording of state blocks. */
2110 if (device->recording)
2112 TRACE("Recording... not performing anything.\n");
2113 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2114 return;
2117 if (old_value == value)
2119 TRACE("Application is setting the old value over, nothing to do.\n");
2120 return;
2123 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2126 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2127 UINT sampler_idx, enum wined3d_sampler_state state)
2129 TRACE("device %p, sampler_idx %u, state %s.\n",
2130 device, sampler_idx, debug_d3dsamplerstate(state));
2132 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2133 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2135 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2137 WARN("Invalid sampler %u.\n", sampler_idx);
2138 return 0; /* Windows accepts overflowing this array ... we do not. */
2141 return device->state.sampler_states[sampler_idx][state];
2144 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2146 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2148 if (device->recording)
2149 device->recording->changed.scissorRect = TRUE;
2151 if (EqualRect(&device->update_state->scissor_rect, rect))
2153 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2154 return;
2156 CopyRect(&device->update_state->scissor_rect, rect);
2158 if (device->recording)
2160 TRACE("Recording... not performing anything.\n");
2161 return;
2164 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2167 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2169 TRACE("device %p, rect %p.\n", device, rect);
2171 *rect = device->state.scissor_rect;
2172 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2175 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2176 struct wined3d_vertex_declaration *declaration)
2178 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2180 TRACE("device %p, declaration %p.\n", device, declaration);
2182 if (device->recording)
2183 device->recording->changed.vertexDecl = TRUE;
2185 if (declaration == prev)
2186 return;
2188 if (declaration)
2189 wined3d_vertex_declaration_incref(declaration);
2190 device->update_state->vertex_declaration = declaration;
2191 if (!device->recording)
2192 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2193 if (prev)
2194 wined3d_vertex_declaration_decref(prev);
2197 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2199 TRACE("device %p.\n", device);
2201 return device->state.vertex_declaration;
2204 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2206 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2208 TRACE("device %p, shader %p.\n", device, shader);
2210 if (device->recording)
2211 device->recording->changed.vertexShader = TRUE;
2213 if (shader == prev)
2214 return;
2216 if (shader)
2217 wined3d_shader_incref(shader);
2218 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2219 if (!device->recording)
2220 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2221 if (prev)
2222 wined3d_shader_decref(prev);
2225 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2227 TRACE("device %p.\n", device);
2229 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2232 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2233 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2235 struct wined3d_buffer *prev;
2237 if (idx >= MAX_CONSTANT_BUFFERS)
2239 WARN("Invalid constant buffer index %u.\n", idx);
2240 return;
2243 prev = device->update_state->cb[type][idx];
2244 if (buffer == prev)
2245 return;
2247 if (buffer)
2248 wined3d_buffer_incref(buffer);
2249 device->update_state->cb[type][idx] = buffer;
2250 if (!device->recording)
2251 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2252 if (prev)
2253 wined3d_buffer_decref(prev);
2256 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2258 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2260 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2263 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2265 TRACE("device %p, idx %u.\n", device, idx);
2267 if (idx >= MAX_CONSTANT_BUFFERS)
2269 WARN("Invalid constant buffer index %u.\n", idx);
2270 return NULL;
2273 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2276 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2277 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2279 struct wined3d_shader_resource_view *prev;
2281 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2283 WARN("Invalid view index %u.\n", idx);
2284 return;
2287 prev = device->update_state->shader_resource_view[type][idx];
2288 if (view == prev)
2289 return;
2291 if (view)
2292 wined3d_shader_resource_view_incref(view);
2293 device->update_state->shader_resource_view[type][idx] = view;
2294 if (!device->recording)
2295 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2296 if (prev)
2297 wined3d_shader_resource_view_decref(prev);
2300 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2301 UINT idx, struct wined3d_shader_resource_view *view)
2303 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2305 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2308 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2309 UINT idx)
2311 TRACE("device %p, idx %u.\n", device, idx);
2313 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2315 WARN("Invalid view index %u.\n", idx);
2316 return NULL;
2319 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2322 static void wined3d_device_set_sampler(struct wined3d_device *device,
2323 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2325 struct wined3d_sampler *prev;
2327 if (idx >= MAX_SAMPLER_OBJECTS)
2329 WARN("Invalid sampler index %u.\n", idx);
2330 return;
2333 prev = device->update_state->sampler[type][idx];
2334 if (sampler == prev)
2335 return;
2337 if (sampler)
2338 wined3d_sampler_incref(sampler);
2339 device->update_state->sampler[type][idx] = sampler;
2340 if (!device->recording)
2341 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2342 if (prev)
2343 wined3d_sampler_decref(prev);
2346 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2348 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2350 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2353 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2355 TRACE("device %p, idx %u.\n", device, idx);
2357 if (idx >= MAX_SAMPLER_OBJECTS)
2359 WARN("Invalid sampler index %u.\n", idx);
2360 return NULL;
2363 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2366 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2368 UINT i;
2370 for (i = 0; i < device->context_count; ++i)
2372 device->contexts[i]->constant_update_mask |= mask;
2376 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2377 UINT start_register, const BOOL *constants, UINT bool_count)
2379 UINT count = min(bool_count, MAX_CONST_B - start_register);
2380 UINT i;
2382 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2383 device, start_register, constants, bool_count);
2385 if (!constants || start_register >= MAX_CONST_B)
2386 return WINED3DERR_INVALIDCALL;
2388 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2389 for (i = 0; i < count; ++i)
2390 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2392 if (device->recording)
2394 for (i = start_register; i < count + start_register; ++i)
2395 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2397 else
2399 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2402 return WINED3D_OK;
2405 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2406 UINT start_register, BOOL *constants, UINT bool_count)
2408 UINT count = min(bool_count, MAX_CONST_B - start_register);
2410 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2411 device, start_register, constants, bool_count);
2413 if (!constants || start_register >= MAX_CONST_B)
2414 return WINED3DERR_INVALIDCALL;
2416 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2418 return WINED3D_OK;
2421 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2422 UINT start_register, const int *constants, UINT vector4i_count)
2424 UINT count = min(vector4i_count, WINED3D_MAX_CONSTS_I - start_register);
2425 UINT i;
2427 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2428 device, start_register, constants, vector4i_count);
2430 if (!constants || start_register >= WINED3D_MAX_CONSTS_I)
2431 return WINED3DERR_INVALIDCALL;
2433 memcpy(&device->update_state->vs_consts_i[start_register], constants, count * sizeof(int) * 4);
2434 for (i = 0; i < count; ++i)
2435 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2436 constants[i * 4], constants[i * 4 + 1],
2437 constants[i * 4 + 2], constants[i * 4 + 3]);
2439 if (device->recording)
2441 for (i = start_register; i < count + start_register; ++i)
2442 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2444 else
2446 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2449 return WINED3D_OK;
2452 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2453 UINT start_register, int *constants, UINT vector4i_count)
2455 UINT count = min(vector4i_count, WINED3D_MAX_CONSTS_I - start_register);
2457 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2458 device, start_register, constants, vector4i_count);
2460 if (!constants || start_register >= WINED3D_MAX_CONSTS_I)
2461 return WINED3DERR_INVALIDCALL;
2463 memcpy(constants, &device->state.vs_consts_i[start_register], count * sizeof(int) * 4);
2464 return WINED3D_OK;
2467 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2468 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2470 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2471 unsigned int i;
2473 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2474 device, start_idx, count, constants);
2476 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2477 || count > d3d_info->limits.vs_uniform_count - start_idx)
2478 return WINED3DERR_INVALIDCALL;
2480 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2481 if (TRACE_ON(d3d))
2483 for (i = 0; i < count; ++i)
2484 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2487 if (device->recording)
2488 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2489 count * sizeof(*device->recording->changed.vs_consts_f));
2490 else
2491 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
2493 return WINED3D_OK;
2496 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2497 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2499 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2501 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2502 device, start_idx, count, constants);
2504 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2505 || count > d3d_info->limits.vs_uniform_count - start_idx)
2506 return WINED3DERR_INVALIDCALL;
2508 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2510 return WINED3D_OK;
2513 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2515 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2517 TRACE("device %p, shader %p.\n", device, shader);
2519 if (device->recording)
2520 device->recording->changed.pixelShader = TRUE;
2522 if (shader == prev)
2523 return;
2525 if (shader)
2526 wined3d_shader_incref(shader);
2527 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2528 if (!device->recording)
2529 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2530 if (prev)
2531 wined3d_shader_decref(prev);
2534 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2536 TRACE("device %p.\n", device);
2538 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2541 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2543 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2545 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2548 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2550 TRACE("device %p, idx %u.\n", device, idx);
2552 if (idx >= MAX_CONSTANT_BUFFERS)
2554 WARN("Invalid constant buffer index %u.\n", idx);
2555 return NULL;
2558 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2561 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2562 UINT idx, struct wined3d_shader_resource_view *view)
2564 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2566 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2569 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2570 UINT idx)
2572 TRACE("device %p, idx %u.\n", device, idx);
2574 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2576 WARN("Invalid view index %u.\n", idx);
2577 return NULL;
2580 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2583 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2585 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2587 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2590 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2592 TRACE("device %p, idx %u.\n", device, idx);
2594 if (idx >= MAX_SAMPLER_OBJECTS)
2596 WARN("Invalid sampler index %u.\n", idx);
2597 return NULL;
2600 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2603 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2604 UINT start_register, const BOOL *constants, UINT bool_count)
2606 UINT count = min(bool_count, MAX_CONST_B - start_register);
2607 UINT i;
2609 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2610 device, start_register, constants, bool_count);
2612 if (!constants || start_register >= MAX_CONST_B)
2613 return WINED3DERR_INVALIDCALL;
2615 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2616 for (i = 0; i < count; ++i)
2617 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2619 if (device->recording)
2621 for (i = start_register; i < count + start_register; ++i)
2622 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2624 else
2626 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2629 return WINED3D_OK;
2632 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2633 UINT start_register, BOOL *constants, UINT bool_count)
2635 UINT count = min(bool_count, MAX_CONST_B - start_register);
2637 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2638 device, start_register, constants, bool_count);
2640 if (!constants || start_register >= MAX_CONST_B)
2641 return WINED3DERR_INVALIDCALL;
2643 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2645 return WINED3D_OK;
2648 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2649 UINT start_register, const int *constants, UINT vector4i_count)
2651 UINT count = min(vector4i_count, WINED3D_MAX_CONSTS_I - start_register);
2652 UINT i;
2654 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2655 device, start_register, constants, vector4i_count);
2657 if (!constants || start_register >= WINED3D_MAX_CONSTS_I)
2658 return WINED3DERR_INVALIDCALL;
2660 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2661 for (i = 0; i < count; ++i)
2662 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2663 constants[i * 4], constants[i * 4 + 1],
2664 constants[i * 4 + 2], constants[i * 4 + 3]);
2666 if (device->recording)
2668 for (i = start_register; i < count + start_register; ++i)
2669 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2671 else
2673 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2676 return WINED3D_OK;
2679 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2680 UINT start_register, int *constants, UINT vector4i_count)
2682 UINT count = min(vector4i_count, WINED3D_MAX_CONSTS_I - start_register);
2684 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2685 device, start_register, constants, vector4i_count);
2687 if (!constants || start_register >= WINED3D_MAX_CONSTS_I)
2688 return WINED3DERR_INVALIDCALL;
2690 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2692 return WINED3D_OK;
2695 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2696 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2698 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2699 unsigned int i;
2701 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2702 device, start_idx, count, constants);
2704 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2705 || count > d3d_info->limits.ps_uniform_count - start_idx)
2706 return WINED3DERR_INVALIDCALL;
2708 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2709 if (TRACE_ON(d3d))
2711 for (i = 0; i < count; ++i)
2712 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2715 if (device->recording)
2716 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2717 count * sizeof(*device->recording->changed.ps_consts_f));
2718 else
2719 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
2721 return WINED3D_OK;
2724 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2725 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2727 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2729 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2730 device, start_idx, count, constants);
2732 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2733 || count > d3d_info->limits.ps_uniform_count - start_idx)
2734 return WINED3DERR_INVALIDCALL;
2736 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2738 return WINED3D_OK;
2741 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2743 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2745 TRACE("device %p, shader %p.\n", device, shader);
2747 if (device->recording || shader == prev)
2748 return;
2749 if (shader)
2750 wined3d_shader_incref(shader);
2751 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2752 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2753 if (prev)
2754 wined3d_shader_decref(prev);
2757 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2759 TRACE("device %p.\n", device);
2761 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2764 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2766 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2768 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2771 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2773 TRACE("device %p, idx %u.\n", device, idx);
2775 if (idx >= MAX_CONSTANT_BUFFERS)
2777 WARN("Invalid constant buffer index %u.\n", idx);
2778 return NULL;
2781 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2784 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2785 UINT idx, struct wined3d_shader_resource_view *view)
2787 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2789 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2792 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2793 UINT idx)
2795 TRACE("device %p, idx %u.\n", device, idx);
2797 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2799 WARN("Invalid view index %u.\n", idx);
2800 return NULL;
2803 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2806 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2808 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2810 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2813 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2815 TRACE("device %p, idx %u.\n", device, idx);
2817 if (idx >= MAX_SAMPLER_OBJECTS)
2819 WARN("Invalid sampler index %u.\n", idx);
2820 return NULL;
2823 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2826 /* Context activation is done by the caller. */
2827 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2828 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2829 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2830 DWORD DestFVF)
2832 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2833 struct wined3d_viewport vp;
2834 UINT vertex_size;
2835 unsigned int i;
2836 BYTE *dest_ptr;
2837 BOOL doClip;
2838 DWORD numTextures;
2839 HRESULT hr;
2841 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2843 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2846 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2848 ERR("Source has no position mask\n");
2849 return WINED3DERR_INVALIDCALL;
2852 if (device->state.render_states[WINED3D_RS_CLIPPING])
2854 static BOOL warned = FALSE;
2856 * The clipping code is not quite correct. Some things need
2857 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2858 * so disable clipping for now.
2859 * (The graphics in Half-Life are broken, and my processvertices
2860 * test crashes with IDirect3DDevice3)
2861 doClip = TRUE;
2863 doClip = FALSE;
2864 if(!warned) {
2865 warned = TRUE;
2866 FIXME("Clipping is broken and disabled for now\n");
2869 else
2870 doClip = FALSE;
2872 vertex_size = get_flexible_vertex_size(DestFVF);
2873 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2875 WARN("Failed to map buffer, hr %#x.\n", hr);
2876 return hr;
2879 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2880 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2881 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2883 TRACE("View mat:\n");
2884 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2885 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2886 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2887 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2889 TRACE("Proj mat:\n");
2890 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2891 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2892 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2893 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2895 TRACE("World mat:\n");
2896 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2897 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2898 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2899 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2901 /* Get the viewport */
2902 wined3d_device_get_viewport(device, &vp);
2903 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2904 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2906 multiply_matrix(&mat,&view_mat,&world_mat);
2907 multiply_matrix(&mat,&proj_mat,&mat);
2909 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2911 for (i = 0; i < dwCount; i+= 1) {
2912 unsigned int tex_index;
2914 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2915 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2916 /* The position first */
2917 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2918 const float *p = (const float *)(element->data.addr + i * element->stride);
2919 float x, y, z, rhw;
2920 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2922 /* Multiplication with world, view and projection matrix. */
2923 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2924 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2925 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2926 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2928 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2930 /* WARNING: The following things are taken from d3d7 and were not yet checked
2931 * against d3d8 or d3d9!
2934 /* Clipping conditions: From msdn
2936 * A vertex is clipped if it does not match the following requirements
2937 * -rhw < x <= rhw
2938 * -rhw < y <= rhw
2939 * 0 < z <= rhw
2940 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2942 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2943 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2947 if( !doClip ||
2948 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2949 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2950 ( rhw > eps ) ) ) {
2952 /* "Normal" viewport transformation (not clipped)
2953 * 1) The values are divided by rhw
2954 * 2) The y axis is negative, so multiply it with -1
2955 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2956 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2957 * 4) Multiply x with Width/2 and add Width/2
2958 * 5) The same for the height
2959 * 6) Add the viewpoint X and Y to the 2D coordinates and
2960 * The minimum Z value to z
2961 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2963 * Well, basically it's simply a linear transformation into viewport
2964 * coordinates
2967 x /= rhw;
2968 y /= rhw;
2969 z /= rhw;
2971 y *= -1;
2973 x *= vp.width / 2;
2974 y *= vp.height / 2;
2975 z *= vp.max_z - vp.min_z;
2977 x += vp.width / 2 + vp.x;
2978 y += vp.height / 2 + vp.y;
2979 z += vp.min_z;
2981 rhw = 1 / rhw;
2982 } else {
2983 /* That vertex got clipped
2984 * Contrary to OpenGL it is not dropped completely, it just
2985 * undergoes a different calculation.
2987 TRACE("Vertex got clipped\n");
2988 x += rhw;
2989 y += rhw;
2991 x /= 2;
2992 y /= 2;
2994 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2995 * outside of the main vertex buffer memory. That needs some more
2996 * investigation...
3000 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3003 ( (float *) dest_ptr)[0] = x;
3004 ( (float *) dest_ptr)[1] = y;
3005 ( (float *) dest_ptr)[2] = z;
3006 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3008 dest_ptr += 3 * sizeof(float);
3010 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3011 dest_ptr += sizeof(float);
3014 if (DestFVF & WINED3DFVF_PSIZE)
3015 dest_ptr += sizeof(DWORD);
3017 if (DestFVF & WINED3DFVF_NORMAL)
3019 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3020 const float *normal = (const float *)(element->data.addr + i * element->stride);
3021 /* AFAIK this should go into the lighting information */
3022 FIXME("Didn't expect the destination to have a normal\n");
3023 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3026 if (DestFVF & WINED3DFVF_DIFFUSE)
3028 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3029 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3030 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3032 static BOOL warned = FALSE;
3034 if(!warned) {
3035 ERR("No diffuse color in source, but destination has one\n");
3036 warned = TRUE;
3039 *( (DWORD *) dest_ptr) = 0xffffffff;
3040 dest_ptr += sizeof(DWORD);
3042 else
3044 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3048 if (DestFVF & WINED3DFVF_SPECULAR)
3050 /* What's the color value in the feedback buffer? */
3051 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3052 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3053 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3055 static BOOL warned = FALSE;
3057 if(!warned) {
3058 ERR("No specular color in source, but destination has one\n");
3059 warned = TRUE;
3062 *(DWORD *)dest_ptr = 0xff000000;
3063 dest_ptr += sizeof(DWORD);
3065 else
3067 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3071 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3073 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3074 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3075 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3077 ERR("No source texture, but destination requests one\n");
3078 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3080 else
3082 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3087 wined3d_buffer_unmap(dest);
3089 return WINED3D_OK;
3091 #undef copy_and_next
3093 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3094 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3095 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3097 struct wined3d_state *state = &device->state;
3098 struct wined3d_stream_info stream_info;
3099 const struct wined3d_gl_info *gl_info;
3100 struct wined3d_context *context;
3101 struct wined3d_shader *vs;
3102 unsigned int i;
3103 HRESULT hr;
3104 WORD map;
3106 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3107 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3108 device, src_start_idx, dst_idx, vertex_count,
3109 dst_buffer, declaration, flags, dst_fvf);
3111 if (declaration)
3112 FIXME("Output vertex declaration not implemented yet.\n");
3114 /* Need any context to write to the vbo. */
3115 context = context_acquire(device, NULL);
3116 gl_info = context->gl_info;
3118 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3119 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3120 context_stream_info_from_declaration(context, state, &stream_info);
3121 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3123 /* We can't convert FROM a VBO, and vertex buffers used to source into
3124 * process_vertices() are unlikely to ever be used for drawing. Release
3125 * VBOs in those buffers and fix up the stream_info structure.
3127 * Also apply the start index. */
3128 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3130 struct wined3d_stream_info_element *e;
3131 struct wined3d_buffer *buffer;
3133 if (!(map & 1))
3134 continue;
3136 e = &stream_info.elements[i];
3137 buffer = state->streams[e->stream_idx].buffer;
3138 e->data.buffer_object = 0;
3139 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3140 if (buffer->buffer_object)
3142 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3143 buffer->buffer_object = 0;
3145 if (e->data.addr)
3146 e->data.addr += e->stride * src_start_idx;
3149 hr = process_vertices_strided(device, dst_idx, vertex_count,
3150 &stream_info, dst_buffer, flags, dst_fvf);
3152 context_release(context);
3154 return hr;
3157 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3158 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3160 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3161 DWORD old_value;
3163 TRACE("device %p, stage %u, state %s, value %#x.\n",
3164 device, stage, debug_d3dtexturestate(state), value);
3166 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3168 WARN("Invalid state %#x passed.\n", state);
3169 return;
3172 if (stage >= d3d_info->limits.ffp_blend_stages)
3174 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3175 stage, d3d_info->limits.ffp_blend_stages - 1);
3176 return;
3179 old_value = device->update_state->texture_states[stage][state];
3180 device->update_state->texture_states[stage][state] = value;
3182 if (device->recording)
3184 TRACE("Recording... not performing anything.\n");
3185 device->recording->changed.textureState[stage] |= 1u << state;
3186 return;
3189 /* Checked after the assignments to allow proper stateblock recording. */
3190 if (old_value == value)
3192 TRACE("Application is setting the old value over, nothing to do.\n");
3193 return;
3196 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3199 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3200 UINT stage, enum wined3d_texture_stage_state state)
3202 TRACE("device %p, stage %u, state %s.\n",
3203 device, stage, debug_d3dtexturestate(state));
3205 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3207 WARN("Invalid state %#x passed.\n", state);
3208 return 0;
3211 return device->state.texture_states[stage][state];
3214 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3215 UINT stage, struct wined3d_texture *texture)
3217 struct wined3d_texture *prev;
3219 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3221 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3222 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3224 /* Windows accepts overflowing this array... we do not. */
3225 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3227 WARN("Ignoring invalid stage %u.\n", stage);
3228 return WINED3D_OK;
3231 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3233 WARN("Rejecting attempt to set scratch texture.\n");
3234 return WINED3DERR_INVALIDCALL;
3237 if (device->recording)
3238 device->recording->changed.textures |= 1u << stage;
3240 prev = device->update_state->textures[stage];
3241 TRACE("Previous texture %p.\n", prev);
3243 if (texture == prev)
3245 TRACE("App is setting the same texture again, nothing to do.\n");
3246 return WINED3D_OK;
3249 TRACE("Setting new texture to %p.\n", texture);
3250 device->update_state->textures[stage] = texture;
3252 if (texture)
3253 wined3d_texture_incref(texture);
3254 if (!device->recording)
3255 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3256 if (prev)
3257 wined3d_texture_decref(prev);
3259 return WINED3D_OK;
3262 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3264 TRACE("device %p, stage %u.\n", device, stage);
3266 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3267 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3269 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3271 WARN("Ignoring invalid stage %u.\n", stage);
3272 return NULL; /* Windows accepts overflowing this array ... we do not. */
3275 return device->state.textures[stage];
3278 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3280 TRACE("device %p, caps %p.\n", device, caps);
3282 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3283 device->create_parms.device_type, caps);
3286 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3287 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3289 struct wined3d_swapchain *swapchain;
3291 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3292 device, swapchain_idx, mode, rotation);
3294 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3295 return WINED3DERR_INVALIDCALL;
3297 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3300 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3302 struct wined3d_stateblock *stateblock;
3303 HRESULT hr;
3305 TRACE("device %p.\n", device);
3307 if (device->recording)
3308 return WINED3DERR_INVALIDCALL;
3310 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3311 if (FAILED(hr))
3312 return hr;
3314 device->recording = stateblock;
3315 device->update_state = &stateblock->state;
3317 TRACE("Recording stateblock %p.\n", stateblock);
3319 return WINED3D_OK;
3322 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3323 struct wined3d_stateblock **stateblock)
3325 struct wined3d_stateblock *object = device->recording;
3327 TRACE("device %p, stateblock %p.\n", device, stateblock);
3329 if (!device->recording)
3331 WARN("Not recording.\n");
3332 *stateblock = NULL;
3333 return WINED3DERR_INVALIDCALL;
3336 stateblock_init_contained_states(object);
3338 *stateblock = object;
3339 device->recording = NULL;
3340 device->update_state = &device->state;
3342 TRACE("Returning stateblock %p.\n", *stateblock);
3344 return WINED3D_OK;
3347 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3349 /* At the moment we have no need for any functionality at the beginning
3350 * of a scene. */
3351 TRACE("device %p.\n", device);
3353 if (device->inScene)
3355 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3356 return WINED3DERR_INVALIDCALL;
3358 device->inScene = TRUE;
3359 return WINED3D_OK;
3362 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3364 struct wined3d_context *context;
3366 TRACE("device %p.\n", device);
3368 if (!device->inScene)
3370 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3371 return WINED3DERR_INVALIDCALL;
3374 context = context_acquire(device, NULL);
3375 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3376 context->gl_info->gl_ops.gl.p_glFlush();
3377 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3378 * fails. */
3379 context_release(context);
3381 device->inScene = FALSE;
3382 return WINED3D_OK;
3385 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3386 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3388 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3389 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3391 if (!rect_count && rects)
3393 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3394 return WINED3D_OK;
3397 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3399 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3400 if (!ds)
3402 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3403 /* TODO: What about depth stencil buffers without stencil bits? */
3404 return WINED3DERR_INVALIDCALL;
3406 else if (flags & WINED3DCLEAR_TARGET)
3408 if (ds->width < device->fb.render_targets[0]->width
3409 || ds->height < device->fb.render_targets[0]->height)
3411 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3412 return WINED3D_OK;
3417 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3419 return WINED3D_OK;
3422 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3423 struct wined3d_query *predicate, BOOL value)
3425 struct wined3d_query *prev;
3427 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3429 prev = device->update_state->predicate;
3430 if (predicate)
3432 FIXME("Predicated rendering not implemented.\n");
3433 wined3d_query_incref(predicate);
3435 device->update_state->predicate = predicate;
3436 device->update_state->predicate_value = value;
3437 if (!device->recording)
3438 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3439 if (prev)
3440 wined3d_query_decref(prev);
3443 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3445 TRACE("device %p, value %p.\n", device, value);
3447 *value = device->state.predicate_value;
3448 return device->state.predicate;
3451 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3452 enum wined3d_primitive_type primitive_type)
3454 GLenum gl_primitive_type, prev;
3456 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3458 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3459 prev = device->update_state->gl_primitive_type;
3460 device->update_state->gl_primitive_type = gl_primitive_type;
3461 if (device->recording)
3462 device->recording->changed.primitive_type = TRUE;
3463 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3464 device_invalidate_state(device, STATE_POINT_ENABLE);
3467 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3468 enum wined3d_primitive_type *primitive_type)
3470 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3472 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3474 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3477 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3479 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3481 if (!device->state.vertex_declaration)
3483 WARN("Called without a valid vertex declaration set.\n");
3484 return WINED3DERR_INVALIDCALL;
3487 if (device->state.load_base_vertex_index)
3489 device->state.load_base_vertex_index = 0;
3490 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3493 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3495 return WINED3D_OK;
3498 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3499 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3501 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3502 device, start_vertex, vertex_count, start_instance, instance_count);
3504 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3507 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3509 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3511 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3513 if (!device->state.index_buffer)
3515 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3516 * without an index buffer set. (The first time at least...)
3517 * D3D8 simply dies, but I doubt it can do much harm to return
3518 * D3DERR_INVALIDCALL there as well. */
3519 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3520 return WINED3DERR_INVALIDCALL;
3523 if (!device->state.vertex_declaration)
3525 WARN("Called without a valid vertex declaration set.\n");
3526 return WINED3DERR_INVALIDCALL;
3529 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3530 device->state.load_base_vertex_index != device->state.base_vertex_index)
3532 device->state.load_base_vertex_index = device->state.base_vertex_index;
3533 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3536 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3538 return WINED3D_OK;
3541 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3542 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3544 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3545 device, start_idx, index_count, start_instance, instance_count);
3547 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3550 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3551 struct wined3d_texture *src_texture, unsigned int src_level,
3552 struct wined3d_texture *dst_texture, unsigned int level_count)
3554 struct wined3d_const_bo_address data;
3555 struct wined3d_context *context;
3556 struct wined3d_map_desc src;
3557 HRESULT hr = WINED3D_OK;
3558 unsigned int i;
3560 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3561 device, src_texture, src_level, dst_texture, level_count);
3563 if (src_texture->resource.format != dst_texture->resource.format)
3565 WARN("Source and destination formats do not match.\n");
3566 return WINED3DERR_INVALIDCALL;
3569 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3570 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3571 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3573 WARN("Source and destination dimensions do not match.\n");
3574 return WINED3DERR_INVALIDCALL;
3577 context = context_acquire(device, NULL);
3579 /* Only a prepare, since we're uploading entire volumes. */
3580 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3581 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3583 for (i = 0; i < level_count; ++i)
3585 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3586 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3587 goto done;
3589 data.buffer_object = 0;
3590 data.addr = src.data;
3591 wined3d_volume_upload_data(dst_texture, i, context, &data);
3592 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3594 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3595 goto done;
3598 done:
3599 context_release(context);
3600 return hr;
3603 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3604 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3606 unsigned int src_size, dst_size, src_skip_levels = 0;
3607 unsigned int layer_count, level_count, i, j;
3608 enum wined3d_resource_type type;
3609 HRESULT hr;
3610 struct wined3d_context *context;
3612 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3614 /* Verify that the source and destination textures are non-NULL. */
3615 if (!src_texture || !dst_texture)
3617 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3618 return WINED3DERR_INVALIDCALL;
3621 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3623 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3624 return WINED3DERR_INVALIDCALL;
3626 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3628 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3629 return WINED3DERR_INVALIDCALL;
3632 /* Verify that the source and destination textures are the same type. */
3633 type = src_texture->resource.type;
3634 if (dst_texture->resource.type != type)
3636 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3637 return WINED3DERR_INVALIDCALL;
3640 layer_count = src_texture->layer_count;
3641 if (layer_count != dst_texture->layer_count)
3643 WARN("Source and destination have different layer counts.\n");
3644 return WINED3DERR_INVALIDCALL;
3647 level_count = min(wined3d_texture_get_level_count(src_texture),
3648 wined3d_texture_get_level_count(dst_texture));
3650 src_size = max(src_texture->resource.width, src_texture->resource.height);
3651 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3652 if (type == WINED3D_RTYPE_TEXTURE_3D)
3654 src_size = max(src_size, src_texture->resource.depth);
3655 dst_size = max(dst_size, dst_texture->resource.depth);
3657 while (src_size > dst_size)
3659 src_size >>= 1;
3660 ++src_skip_levels;
3663 /* Make sure that the destination texture is loaded. */
3664 context = context_acquire(device, NULL);
3665 wined3d_texture_load(dst_texture, context, FALSE);
3666 context_release(context);
3668 /* Update every surface level of the texture. */
3669 switch (type)
3671 case WINED3D_RTYPE_TEXTURE_2D:
3673 unsigned int src_levels = src_texture->level_count;
3674 unsigned int dst_levels = dst_texture->level_count;
3675 struct wined3d_surface *src_surface;
3676 struct wined3d_surface *dst_surface;
3678 for (i = 0; i < layer_count; ++i)
3680 for (j = 0; j < level_count; ++j)
3682 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3683 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3684 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3686 WARN("Failed to update surface, hr %#x.\n", hr);
3687 return hr;
3691 return WINED3D_OK;
3694 case WINED3D_RTYPE_TEXTURE_3D:
3695 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3696 src_texture, src_skip_levels, dst_texture, level_count)))
3697 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3698 return hr;
3700 default:
3701 FIXME("Unsupported texture type %#x.\n", type);
3702 return WINED3DERR_INVALIDCALL;
3706 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3708 const struct wined3d_state *state = &device->state;
3709 struct wined3d_texture *texture;
3710 DWORD i;
3712 TRACE("device %p, num_passes %p.\n", device, num_passes);
3714 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3716 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3718 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3719 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3721 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3723 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3724 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3727 texture = state->textures[i];
3728 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3730 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3732 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3733 return E_FAIL;
3735 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3737 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3738 return E_FAIL;
3740 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3741 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3743 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3744 return E_FAIL;
3748 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3749 || state->render_states[WINED3D_RS_STENCILENABLE])
3751 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3752 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3754 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3756 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3757 return WINED3DERR_CONFLICTINGRENDERSTATE;
3761 /* return a sensible default */
3762 *num_passes = 1;
3764 TRACE("returning D3D_OK\n");
3765 return WINED3D_OK;
3768 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3770 static BOOL warned;
3772 TRACE("device %p, software %#x.\n", device, software);
3774 if (!warned)
3776 FIXME("device %p, software %#x stub!\n", device, software);
3777 warned = TRUE;
3780 device->softwareVertexProcessing = software;
3783 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3785 static BOOL warned;
3787 TRACE("device %p.\n", device);
3789 if (!warned)
3791 TRACE("device %p stub!\n", device);
3792 warned = TRUE;
3795 return device->softwareVertexProcessing;
3798 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3799 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3801 struct wined3d_swapchain *swapchain;
3803 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3804 device, swapchain_idx, raster_status);
3806 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3807 return WINED3DERR_INVALIDCALL;
3809 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3812 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3814 static BOOL warned;
3816 TRACE("device %p, segments %.8e.\n", device, segments);
3818 if (segments != 0.0f)
3820 if (!warned)
3822 FIXME("device %p, segments %.8e stub!\n", device, segments);
3823 warned = TRUE;
3827 return WINED3D_OK;
3830 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3832 static BOOL warned;
3834 TRACE("device %p.\n", device);
3836 if (!warned)
3838 FIXME("device %p stub!\n", device);
3839 warned = TRUE;
3842 return 0.0f;
3845 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3846 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3848 struct wined3d_texture *dst_texture, *src_texture;
3849 RECT dst_rect, src_rect;
3850 unsigned int i, j;
3851 HRESULT hr;
3853 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3855 if (src_resource == dst_resource)
3857 WARN("Source and destination are the same resource.\n");
3858 return;
3861 if (src_resource->type != dst_resource->type)
3863 WARN("Resource types (%s / %s) don't match.\n",
3864 debug_d3dresourcetype(dst_resource->type),
3865 debug_d3dresourcetype(src_resource->type));
3866 return;
3869 if (src_resource->width != dst_resource->width
3870 || src_resource->height != dst_resource->height
3871 || src_resource->depth != dst_resource->depth)
3873 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3874 dst_resource->width, dst_resource->height, dst_resource->depth,
3875 src_resource->width, src_resource->height, src_resource->depth);
3876 return;
3879 if (src_resource->format->id != dst_resource->format->id)
3881 WARN("Resource formats (%s / %s) don't match.\n",
3882 debug_d3dformat(dst_resource->format->id),
3883 debug_d3dformat(src_resource->format->id));
3884 return;
3887 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3889 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3890 buffer_from_resource(src_resource), 0,
3891 dst_resource->size)))
3892 ERR("Failed to copy buffer, hr %#x.\n", hr);
3893 return;
3896 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3898 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3899 return;
3902 dst_texture = texture_from_resource(dst_resource);
3903 src_texture = texture_from_resource(src_resource);
3905 if (src_texture->layer_count != dst_texture->layer_count
3906 || src_texture->level_count != dst_texture->level_count)
3908 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3909 dst_texture->layer_count, dst_texture->level_count,
3910 src_texture->layer_count, src_texture->level_count);
3911 return;
3914 for (i = 0; i < dst_texture->level_count; ++i)
3916 SetRect(&dst_rect, 0, 0,
3917 wined3d_texture_get_level_width(dst_texture, i),
3918 wined3d_texture_get_level_height(dst_texture, i));
3919 SetRect(&src_rect, 0, 0,
3920 wined3d_texture_get_level_width(src_texture, i),
3921 wined3d_texture_get_level_height(dst_texture, i));
3922 for (j = 0; j < dst_texture->layer_count; ++j)
3924 unsigned int idx = j * dst_texture->level_count + i;
3926 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3927 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3928 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3933 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3934 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3935 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3936 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3938 struct wined3d_texture *dst_texture, *src_texture;
3939 RECT dst_rect, src_rect;
3940 HRESULT hr;
3942 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3943 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3944 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3945 src_resource, src_sub_resource_idx, debug_box(src_box));
3947 if (src_box && (src_box->left >= src_box->right
3948 || src_box->top >= src_box->bottom
3949 || src_box->front >= src_box->back))
3951 WARN("Invalid box %s specified.\n", debug_box(src_box));
3952 return WINED3DERR_INVALIDCALL;
3955 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3957 WARN("Source and destination are the same sub-resource.\n");
3958 return WINED3DERR_INVALIDCALL;
3961 if (src_resource->type != dst_resource->type)
3963 WARN("Resource types (%s / %s) don't match.\n",
3964 debug_d3dresourcetype(dst_resource->type),
3965 debug_d3dresourcetype(src_resource->type));
3966 return WINED3DERR_INVALIDCALL;
3969 if (src_resource->format->id != dst_resource->format->id)
3971 WARN("Resource formats (%s / %s) don't match.\n",
3972 debug_d3dformat(dst_resource->format->id),
3973 debug_d3dformat(src_resource->format->id));
3974 return WINED3DERR_INVALIDCALL;
3977 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3979 unsigned int src_offset, size;
3981 if (dst_sub_resource_idx)
3983 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3984 return WINED3DERR_INVALIDCALL;
3986 if (src_sub_resource_idx)
3988 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3989 return WINED3DERR_INVALIDCALL;
3992 if (src_box)
3994 src_offset = src_box->left;
3995 size = src_box->right - src_box->left;
3997 else
3999 src_offset = 0;
4000 size = src_resource->size;
4003 if (src_offset > src_resource->size
4004 || size > src_resource->size - src_offset
4005 || dst_x > dst_resource->size
4006 || size > dst_resource->size - dst_x)
4008 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4009 dst_x, src_offset, size);
4010 return WINED3DERR_INVALIDCALL;
4013 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4014 buffer_from_resource(src_resource), src_offset, size);
4017 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4019 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4020 return WINED3DERR_INVALIDCALL;
4023 dst_texture = texture_from_resource(dst_resource);
4024 src_texture = texture_from_resource(src_resource);
4026 if (src_box)
4028 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4030 else
4032 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4034 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4035 wined3d_texture_get_level_height(src_texture, level));
4038 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4039 dst_y + (src_rect.bottom - src_rect.top));
4041 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4042 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4043 WARN("Failed to blit, hr %#x.\n", hr);
4045 return hr;
4048 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4049 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4050 unsigned int depth_pitch)
4052 struct wined3d_texture_sub_resource *sub_resource;
4053 const struct wined3d_gl_info *gl_info;
4054 struct wined3d_const_bo_address addr;
4055 unsigned int width, height, level;
4056 struct wined3d_context *context;
4057 struct wined3d_texture *texture;
4058 struct wined3d_surface *surface;
4059 POINT dst_point;
4060 RECT src_rect;
4062 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4063 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4065 if (resource->type == WINED3D_RTYPE_BUFFER)
4067 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4068 HRESULT hr;
4070 if (sub_resource_idx > 0)
4072 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4073 return;
4076 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4077 WARN("Failed to update buffer data, hr %#x.\n", hr);
4079 return;
4082 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4084 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4085 return;
4088 texture = texture_from_resource(resource);
4089 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4091 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4092 return;
4094 surface = sub_resource->u.surface;
4096 level = sub_resource_idx % texture->level_count;
4097 width = wined3d_texture_get_level_width(texture, level);
4098 height = wined3d_texture_get_level_height(texture, level);
4100 src_rect.left = 0;
4101 src_rect.top = 0;
4102 if (box)
4104 if (box->left >= box->right || box->right > width
4105 || box->top >= box->bottom || box->bottom > height
4106 || box->front >= box->back)
4108 WARN("Invalid box %s specified.\n", debug_box(box));
4109 return;
4112 src_rect.right = box->right - box->left;
4113 src_rect.bottom = box->bottom - box->top;
4114 dst_point.x = box->left;
4115 dst_point.y = box->top;
4117 else
4119 src_rect.right = width;
4120 src_rect.bottom = height;
4121 dst_point.x = 0;
4122 dst_point.y = 0;
4125 addr.buffer_object = 0;
4126 addr.addr = data;
4128 context = context_acquire(resource->device, NULL);
4129 gl_info = context->gl_info;
4131 /* Only load the surface for partial updates. */
4132 if (!dst_point.x && !dst_point.y && src_rect.right == width && src_rect.bottom == height)
4133 wined3d_texture_prepare_texture(texture, context, FALSE);
4134 else
4135 surface_load_location(surface, context, WINED3D_LOCATION_TEXTURE_RGB);
4136 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4138 wined3d_surface_upload_data(surface, gl_info, resource->format,
4139 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4141 context_release(context);
4143 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4144 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4147 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4148 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4149 const struct wined3d_color *color, float depth, DWORD stencil)
4151 const struct blit_shader *blitter;
4152 struct wined3d_resource *resource;
4153 enum wined3d_blit_op blit_op;
4154 RECT r;
4156 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4157 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4159 if (!flags)
4160 return WINED3D_OK;
4162 resource = view->resource;
4163 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4165 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4166 return WINED3DERR_INVALIDCALL;
4169 if (view->depth > 1)
4171 FIXME("Layered clears not implemented.\n");
4172 return WINED3DERR_INVALIDCALL;
4175 if (!rect)
4177 SetRect(&r, 0, 0, view->width, view->height);
4178 rect = &r;
4181 if (flags & WINED3DCLEAR_TARGET)
4182 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4183 else
4184 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4186 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4187 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4189 FIXME("No blitter is capable of performing the requested fill operation.\n");
4190 return WINED3DERR_INVALIDCALL;
4193 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4194 return blitter->color_fill(device, view, rect, color);
4195 else
4196 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4199 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4200 unsigned int view_idx)
4202 TRACE("device %p, view_idx %u.\n", device, view_idx);
4204 if (view_idx >= device->adapter->gl_info.limits.buffers)
4206 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4207 return NULL;
4210 return device->fb.render_targets[view_idx];
4213 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4215 TRACE("device %p.\n", device);
4217 return device->fb.depth_stencil;
4220 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4221 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4223 struct wined3d_rendertarget_view *prev;
4225 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4226 device, view_idx, view, set_viewport);
4228 if (view_idx >= device->adapter->gl_info.limits.buffers)
4230 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4231 return WINED3DERR_INVALIDCALL;
4234 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4236 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4237 return WINED3DERR_INVALIDCALL;
4240 /* Set the viewport and scissor rectangles, if requested. Tests show that
4241 * stateblock recording is ignored, the change goes directly into the
4242 * primary stateblock. */
4243 if (!view_idx && set_viewport)
4245 struct wined3d_state *state = &device->state;
4247 state->viewport.x = 0;
4248 state->viewport.y = 0;
4249 state->viewport.width = view->width;
4250 state->viewport.height = view->height;
4251 state->viewport.min_z = 0.0f;
4252 state->viewport.max_z = 1.0f;
4253 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4255 state->scissor_rect.top = 0;
4256 state->scissor_rect.left = 0;
4257 state->scissor_rect.right = view->width;
4258 state->scissor_rect.bottom = view->height;
4259 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4263 prev = device->fb.render_targets[view_idx];
4264 if (view == prev)
4265 return WINED3D_OK;
4267 if (view)
4268 wined3d_rendertarget_view_incref(view);
4269 device->fb.render_targets[view_idx] = view;
4270 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4271 /* Release after the assignment, to prevent device_resource_released()
4272 * from seeing the surface as still in use. */
4273 if (prev)
4274 wined3d_rendertarget_view_decref(prev);
4276 return WINED3D_OK;
4279 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4281 struct wined3d_rendertarget_view *prev;
4283 TRACE("device %p, view %p.\n", device, view);
4285 prev = device->fb.depth_stencil;
4286 if (prev == view)
4288 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4289 return;
4292 if ((device->fb.depth_stencil = view))
4293 wined3d_rendertarget_view_incref(view);
4294 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4295 if (prev)
4296 wined3d_rendertarget_view_decref(prev);
4299 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4300 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4302 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4303 struct wined3d_sub_resource_data data;
4304 struct wined3d_resource_desc desc;
4305 struct wined3d_map_desc map_desc;
4306 struct wined3d_texture *texture;
4307 HRESULT hr;
4309 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4311 ERR("Failed to map source texture.\n");
4312 return NULL;
4315 data.data = map_desc.data;
4316 data.row_pitch = map_desc.row_pitch;
4317 data.slice_pitch = map_desc.slice_pitch;
4319 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4320 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4321 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4322 desc.multisample_quality = 0;
4323 desc.usage = WINED3DUSAGE_DYNAMIC;
4324 desc.pool = WINED3D_POOL_DEFAULT;
4325 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4326 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4327 desc.depth = 1;
4328 desc.size = 0;
4330 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4331 &data, NULL, &wined3d_null_parent_ops, &texture);
4332 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4333 if (FAILED(hr))
4335 ERR("Failed to create cursor texture.\n");
4336 return NULL;
4339 return texture;
4342 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4343 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4345 unsigned int texture_level = sub_resource_idx % texture->level_count;
4346 unsigned int cursor_width, cursor_height;
4347 struct wined3d_display_mode mode;
4348 struct wined3d_map_desc map_desc;
4349 HRESULT hr;
4351 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4352 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4354 if (sub_resource_idx >= texture->level_count * texture->layer_count
4355 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4356 return WINED3DERR_INVALIDCALL;
4358 if (device->cursor_texture)
4360 wined3d_texture_decref(device->cursor_texture);
4361 device->cursor_texture = NULL;
4364 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4366 WARN("Texture %p has invalid format %s.\n",
4367 texture, debug_d3dformat(texture->resource.format->id));
4368 return WINED3DERR_INVALIDCALL;
4371 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4373 ERR("Failed to get display mode, hr %#x.\n", hr);
4374 return WINED3DERR_INVALIDCALL;
4377 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4378 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4379 if (cursor_width > mode.width || cursor_height > mode.height)
4381 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4382 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4383 return WINED3DERR_INVALIDCALL;
4386 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4388 /* Do not store the surface's pointer because the application may
4389 * release it after setting the cursor image. Windows doesn't
4390 * addref the set surface, so we can't do this either without
4391 * creating circular refcount dependencies. */
4392 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4394 ERR("Failed to create cursor texture.\n");
4395 return WINED3DERR_INVALIDCALL;
4398 if (cursor_width == 32 && cursor_height == 32)
4400 UINT mask_size = cursor_width * cursor_height / 8;
4401 ICONINFO cursor_info;
4402 DWORD *mask_bits;
4403 HCURSOR cursor;
4405 /* 32-bit user32 cursors ignore the alpha channel if it's all
4406 * zeroes, and use the mask instead. Fill the mask with all ones
4407 * to ensure we still get a fully transparent cursor. */
4408 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4409 return E_OUTOFMEMORY;
4410 memset(mask_bits, 0xff, mask_size);
4412 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4413 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4414 cursor_info.fIcon = FALSE;
4415 cursor_info.xHotspot = x_hotspot;
4416 cursor_info.yHotspot = y_hotspot;
4417 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4418 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4419 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4421 /* Create our cursor and clean up. */
4422 cursor = CreateIconIndirect(&cursor_info);
4423 if (cursor_info.hbmMask)
4424 DeleteObject(cursor_info.hbmMask);
4425 if (cursor_info.hbmColor)
4426 DeleteObject(cursor_info.hbmColor);
4427 if (device->hardwareCursor)
4428 DestroyCursor(device->hardwareCursor);
4429 device->hardwareCursor = cursor;
4430 if (device->bCursorVisible)
4431 SetCursor(cursor);
4433 HeapFree(GetProcessHeap(), 0, mask_bits);
4436 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4437 device->cursorWidth = cursor_width;
4438 device->cursorHeight = cursor_height;
4439 device->xHotSpot = x_hotspot;
4440 device->yHotSpot = y_hotspot;
4442 return WINED3D_OK;
4445 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4446 int x_screen_space, int y_screen_space, DWORD flags)
4448 TRACE("device %p, x %d, y %d, flags %#x.\n",
4449 device, x_screen_space, y_screen_space, flags);
4451 device->xScreenSpace = x_screen_space;
4452 device->yScreenSpace = y_screen_space;
4454 if (device->hardwareCursor)
4456 POINT pt;
4458 GetCursorPos( &pt );
4459 if (x_screen_space == pt.x && y_screen_space == pt.y)
4460 return;
4461 SetCursorPos( x_screen_space, y_screen_space );
4463 /* Switch to the software cursor if position diverges from the hardware one. */
4464 GetCursorPos( &pt );
4465 if (x_screen_space != pt.x || y_screen_space != pt.y)
4467 if (device->bCursorVisible) SetCursor( NULL );
4468 DestroyCursor( device->hardwareCursor );
4469 device->hardwareCursor = 0;
4474 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4476 BOOL oldVisible = device->bCursorVisible;
4478 TRACE("device %p, show %#x.\n", device, show);
4481 * When ShowCursor is first called it should make the cursor appear at the OS's last
4482 * known cursor position.
4484 if (show && !oldVisible)
4486 POINT pt;
4487 GetCursorPos(&pt);
4488 device->xScreenSpace = pt.x;
4489 device->yScreenSpace = pt.y;
4492 if (device->hardwareCursor)
4494 device->bCursorVisible = show;
4495 if (show)
4496 SetCursor(device->hardwareCursor);
4497 else
4498 SetCursor(NULL);
4500 else if (device->cursor_texture)
4502 device->bCursorVisible = show;
4505 return oldVisible;
4508 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4510 struct wined3d_resource *resource, *cursor;
4512 TRACE("device %p.\n", device);
4514 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4516 TRACE("Checking resource %p for eviction.\n", resource);
4518 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4520 TRACE("Evicting %p.\n", resource);
4521 resource->resource_ops->resource_unload(resource);
4525 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4526 device_invalidate_state(device, STATE_STREAMSRC);
4529 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4531 struct wined3d_resource *resource, *cursor;
4532 const struct wined3d_gl_info *gl_info;
4533 struct wined3d_context *context;
4534 struct wined3d_shader *shader;
4536 context = context_acquire(device, NULL);
4537 gl_info = context->gl_info;
4539 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4541 TRACE("Unloading resource %p.\n", resource);
4542 resource->resource_ops->resource_unload(resource);
4545 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4547 device->shader_backend->shader_destroy(shader);
4550 if (device->depth_blt_texture)
4552 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4553 device->depth_blt_texture = 0;
4556 device->blitter->free_private(device);
4557 device->shader_backend->shader_free_private(device);
4558 destroy_dummy_textures(device, gl_info);
4559 destroy_default_sampler(device);
4561 context_release(context);
4563 while (device->context_count)
4565 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4568 HeapFree(GetProcessHeap(), 0, swapchain->context);
4569 swapchain->context = NULL;
4572 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4574 struct wined3d_context *context;
4575 struct wined3d_texture *target;
4576 HRESULT hr;
4578 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4579 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4581 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4582 return hr;
4585 if (FAILED(hr = device->blitter->alloc_private(device)))
4587 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4588 device->shader_backend->shader_free_private(device);
4589 return hr;
4592 /* Recreate the primary swapchain's context */
4593 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4594 if (!swapchain->context)
4596 ERR("Failed to allocate memory for swapchain context array.\n");
4597 device->blitter->free_private(device);
4598 device->shader_backend->shader_free_private(device);
4599 return E_OUTOFMEMORY;
4602 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4603 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4605 WARN("Failed to create context.\n");
4606 device->blitter->free_private(device);
4607 device->shader_backend->shader_free_private(device);
4608 HeapFree(GetProcessHeap(), 0, swapchain->context);
4609 return E_FAIL;
4612 swapchain->context[0] = context;
4613 swapchain->num_contexts = 1;
4614 create_dummy_textures(device, context);
4615 create_default_sampler(device);
4616 context_release(context);
4618 return WINED3D_OK;
4621 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4622 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4623 wined3d_device_reset_cb callback, BOOL reset_state)
4625 struct wined3d_rendertarget_view_desc view_desc;
4626 struct wined3d_resource *resource, *cursor;
4627 struct wined3d_swapchain *swapchain;
4628 struct wined3d_display_mode m;
4629 BOOL DisplayModeChanged;
4630 HRESULT hr = WINED3D_OK;
4631 unsigned int i;
4633 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4634 device, swapchain_desc, mode, callback, reset_state);
4636 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4638 ERR("Failed to get the first implicit swapchain.\n");
4639 return WINED3DERR_INVALIDCALL;
4641 DisplayModeChanged = swapchain->reapply_mode;
4643 if (reset_state)
4645 if (device->logo_texture)
4647 wined3d_texture_decref(device->logo_texture);
4648 device->logo_texture = NULL;
4650 if (device->cursor_texture)
4652 wined3d_texture_decref(device->cursor_texture);
4653 device->cursor_texture = NULL;
4655 state_unbind_resources(&device->state);
4658 if (device->fb.render_targets)
4660 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4662 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4665 wined3d_device_set_depth_stencil_view(device, NULL);
4667 if (device->onscreen_depth_stencil)
4669 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4670 device->onscreen_depth_stencil = NULL;
4673 if (reset_state)
4675 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4677 TRACE("Enumerating resource %p.\n", resource);
4678 if (FAILED(hr = callback(resource)))
4679 return hr;
4683 TRACE("New params:\n");
4684 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4685 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4686 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4687 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4688 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4689 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4690 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4691 TRACE("device_window %p\n", swapchain_desc->device_window);
4692 TRACE("windowed %#x\n", swapchain_desc->windowed);
4693 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4694 if (swapchain_desc->enable_auto_depth_stencil)
4695 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4696 TRACE("flags %#x\n", swapchain_desc->flags);
4697 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4698 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4699 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4701 /* No special treatment of these parameters. Just store them */
4702 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4703 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4704 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4705 swapchain->desc.flags = swapchain_desc->flags;
4706 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4707 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4708 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4710 if (swapchain_desc->device_window
4711 && swapchain_desc->device_window != swapchain->desc.device_window)
4713 TRACE("Changing the device window from %p to %p.\n",
4714 swapchain->desc.device_window, swapchain_desc->device_window);
4715 swapchain->desc.device_window = swapchain_desc->device_window;
4716 swapchain->device_window = swapchain_desc->device_window;
4717 wined3d_swapchain_set_window(swapchain, NULL);
4720 if (mode)
4722 DisplayModeChanged = TRUE;
4723 m = *mode;
4725 else if (swapchain_desc->windowed)
4727 m = swapchain->original_mode;
4729 else
4731 m.width = swapchain_desc->backbuffer_width;
4732 m.height = swapchain_desc->backbuffer_height;
4733 m.refresh_rate = swapchain_desc->refresh_rate;
4734 m.format_id = swapchain_desc->backbuffer_format;
4735 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4737 if ((m.width != swapchain->desc.backbuffer_width
4738 || m.height != swapchain->desc.backbuffer_height))
4739 DisplayModeChanged = TRUE;
4742 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4743 || DisplayModeChanged)
4745 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4747 WARN("Failed to set display mode, hr %#x.\n", hr);
4748 return WINED3DERR_INVALIDCALL;
4751 if (!swapchain_desc->windowed)
4753 if (swapchain->desc.windowed)
4755 HWND focus_window = device->create_parms.focus_window;
4756 if (!focus_window)
4757 focus_window = swapchain_desc->device_window;
4758 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4760 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4761 return hr;
4764 /* switch from windowed to fs */
4765 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4766 swapchain_desc->backbuffer_width,
4767 swapchain_desc->backbuffer_height);
4769 else
4771 /* Fullscreen -> fullscreen mode change */
4772 MoveWindow(swapchain->device_window, 0, 0,
4773 swapchain_desc->backbuffer_width,
4774 swapchain_desc->backbuffer_height,
4775 TRUE);
4777 swapchain->d3d_mode = m;
4779 else if (!swapchain->desc.windowed)
4781 /* Fullscreen -> windowed switch */
4782 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4783 wined3d_device_release_focus_window(device);
4785 swapchain->desc.windowed = swapchain_desc->windowed;
4787 else if (!swapchain_desc->windowed)
4789 DWORD style = device->style;
4790 DWORD exStyle = device->exStyle;
4791 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4792 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4793 * Reset to clear up their mess. Guild Wars also loses the device during that.
4795 device->style = 0;
4796 device->exStyle = 0;
4797 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4798 swapchain_desc->backbuffer_width,
4799 swapchain_desc->backbuffer_height);
4800 device->style = style;
4801 device->exStyle = exStyle;
4804 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4805 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4806 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4807 return hr;
4809 if (device->auto_depth_stencil_view)
4811 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4812 device->auto_depth_stencil_view = NULL;
4814 if (swapchain->desc.enable_auto_depth_stencil)
4816 struct wined3d_resource_desc texture_desc;
4817 struct wined3d_texture *texture;
4819 TRACE("Creating the depth stencil buffer\n");
4821 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4822 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4823 texture_desc.multisample_type = swapchain->desc.multisample_type;
4824 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4825 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4826 texture_desc.pool = WINED3D_POOL_DEFAULT;
4827 texture_desc.width = swapchain->desc.backbuffer_width;
4828 texture_desc.height = swapchain->desc.backbuffer_height;
4829 texture_desc.depth = 1;
4830 texture_desc.size = 0;
4832 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4833 device->device_parent, &texture_desc, &texture)))
4835 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4836 return WINED3DERR_INVALIDCALL;
4839 view_desc.format_id = texture->resource.format->id;
4840 view_desc.u.texture.level_idx = 0;
4841 view_desc.u.texture.layer_idx = 0;
4842 view_desc.u.texture.layer_count = 1;
4843 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4844 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4845 wined3d_texture_decref(texture);
4846 if (FAILED(hr))
4848 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4849 return hr;
4852 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4855 if (device->back_buffer_view)
4857 wined3d_rendertarget_view_decref(device->back_buffer_view);
4858 device->back_buffer_view = NULL;
4860 if (swapchain->desc.backbuffer_count)
4862 view_desc.format_id = swapchain_desc->backbuffer_format;
4863 view_desc.u.texture.level_idx = 0;
4864 view_desc.u.texture.layer_idx = 0;
4865 view_desc.u.texture.layer_count = 1;
4866 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
4867 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4869 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4870 return hr;
4874 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4876 if (reset_state)
4878 TRACE("Resetting stateblock.\n");
4879 if (device->recording)
4881 wined3d_stateblock_decref(device->recording);
4882 device->recording = NULL;
4884 wined3d_cs_emit_reset_state(device->cs);
4885 state_cleanup(&device->state);
4887 if (device->d3d_initialized)
4888 delete_opengl_contexts(device, swapchain);
4890 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4891 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4892 device->update_state = &device->state;
4894 device_init_swapchain_state(device, swapchain);
4896 else if (device->back_buffer_view)
4898 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4899 struct wined3d_state *state = &device->state;
4901 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4903 /* Note the min_z / max_z is not reset. */
4904 state->viewport.x = 0;
4905 state->viewport.y = 0;
4906 state->viewport.width = view->width;
4907 state->viewport.height = view->height;
4908 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4910 state->scissor_rect.top = 0;
4911 state->scissor_rect.left = 0;
4912 state->scissor_rect.right = view->width;
4913 state->scissor_rect.bottom = view->height;
4914 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4917 if (device->d3d_initialized)
4919 if (reset_state)
4920 hr = create_primary_opengl_context(device, swapchain);
4921 swapchain_update_swap_interval(swapchain);
4924 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4925 * first use
4927 return hr;
4930 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4932 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4934 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4936 return WINED3D_OK;
4940 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4941 struct wined3d_device_creation_parameters *parameters)
4943 TRACE("device %p, parameters %p.\n", device, parameters);
4945 *parameters = device->create_parms;
4948 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4950 TRACE("device %p.\n", device);
4952 return device->wined3d;
4955 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4956 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4958 struct wined3d_swapchain *swapchain;
4960 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4961 device, swapchain_idx, flags, ramp);
4963 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4964 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4967 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4968 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4970 struct wined3d_swapchain *swapchain;
4972 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4973 device, swapchain_idx, ramp);
4975 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4976 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4979 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4981 TRACE("device %p, resource %p.\n", device, resource);
4983 list_add_head(&device->resources, &resource->resource_list_entry);
4986 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4988 TRACE("device %p, resource %p.\n", device, resource);
4990 list_remove(&resource->resource_list_entry);
4993 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4995 enum wined3d_resource_type type = resource->type;
4996 struct wined3d_rendertarget_view *rtv;
4997 unsigned int i;
4999 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5001 context_resource_released(device, resource, type);
5003 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5005 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
5006 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5009 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5010 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5012 switch (type)
5014 case WINED3D_RTYPE_TEXTURE_2D:
5015 case WINED3D_RTYPE_TEXTURE_3D:
5016 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5018 struct wined3d_texture *texture = texture_from_resource(resource);
5020 if (device->state.textures[i] == texture)
5022 ERR("Texture %p is still in use, stage %u.\n", texture, i);
5023 device->state.textures[i] = NULL;
5026 if (device->recording && device->update_state->textures[i] == texture)
5028 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
5029 texture, device->recording, i);
5030 device->update_state->textures[i] = NULL;
5033 break;
5035 case WINED3D_RTYPE_BUFFER:
5037 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5039 for (i = 0; i < MAX_STREAMS; ++i)
5041 if (device->state.streams[i].buffer == buffer)
5043 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
5044 device->state.streams[i].buffer = NULL;
5047 if (device->recording && device->update_state->streams[i].buffer == buffer)
5049 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5050 buffer, device->recording, i);
5051 device->update_state->streams[i].buffer = NULL;
5055 if (device->state.index_buffer == buffer)
5057 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5058 device->state.index_buffer = NULL;
5061 if (device->recording && device->update_state->index_buffer == buffer)
5063 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5064 buffer, device->recording);
5065 device->update_state->index_buffer = NULL;
5068 break;
5070 default:
5071 break;
5074 /* Remove the resource from the resourceStore */
5075 device_resource_remove(device, resource);
5077 TRACE("Resource released.\n");
5080 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5082 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5084 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5087 static const struct wine_rb_functions wined3d_sampler_rb_functions =
5089 wined3d_rb_alloc,
5090 wined3d_rb_realloc,
5091 wined3d_rb_free,
5092 wined3d_sampler_compare,
5095 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5096 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5097 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5099 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5100 const struct fragment_pipeline *fragment_pipeline;
5101 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5102 unsigned int i;
5103 HRESULT hr;
5105 device->ref = 1;
5106 device->wined3d = wined3d;
5107 wined3d_incref(device->wined3d);
5108 device->adapter = wined3d->adapter_count ? adapter : NULL;
5109 device->device_parent = device_parent;
5110 list_init(&device->resources);
5111 list_init(&device->shaders);
5112 device->surface_alignment = surface_alignment;
5114 /* Save the creation parameters. */
5115 device->create_parms.adapter_idx = adapter_idx;
5116 device->create_parms.device_type = device_type;
5117 device->create_parms.focus_window = focus_window;
5118 device->create_parms.flags = flags;
5120 device->shader_backend = adapter->shader_backend;
5122 vertex_pipeline = adapter->vertex_pipe;
5124 fragment_pipeline = adapter->fragment_pipe;
5126 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
5128 ERR("Failed to initialize sampler rbtree.\n");
5129 return E_OUTOFMEMORY;
5132 if (vertex_pipeline->vp_states && fragment_pipeline->states
5133 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5134 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5135 fragment_pipeline, misc_state_template)))
5137 ERR("Failed to compile state table, hr %#x.\n", hr);
5138 wine_rb_destroy(&device->samplers, NULL, NULL);
5139 wined3d_decref(device->wined3d);
5140 return hr;
5143 device->blitter = adapter->blitter;
5145 state_init(&device->state, &device->fb, &adapter->gl_info,
5146 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5147 device->update_state = &device->state;
5149 if (!(device->cs = wined3d_cs_create(device)))
5151 WARN("Failed to create command stream.\n");
5152 state_cleanup(&device->state);
5153 hr = E_FAIL;
5154 goto err;
5157 return WINED3D_OK;
5159 err:
5160 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5162 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5164 wine_rb_destroy(&device->samplers, NULL, NULL);
5165 wined3d_decref(device->wined3d);
5166 return hr;
5170 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5172 DWORD rep = device->StateTable[state].representative;
5173 struct wined3d_context *context;
5174 DWORD idx;
5175 BYTE shift;
5176 UINT i;
5178 for (i = 0; i < device->context_count; ++i)
5180 context = device->contexts[i];
5181 if(isStateDirty(context, rep)) continue;
5183 context->dirtyArray[context->numDirtyEntries++] = rep;
5184 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5185 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5186 context->isStateDirty[idx] |= (1u << shift);
5190 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5191 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5193 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5195 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5196 window, message, wparam, lparam);
5197 if (unicode)
5198 return DefWindowProcW(window, message, wparam, lparam);
5199 else
5200 return DefWindowProcA(window, message, wparam, lparam);
5203 if (message == WM_DESTROY)
5205 TRACE("unregister window %p.\n", window);
5206 wined3d_unregister_window(window);
5208 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5209 ERR("Window %p is not the focus window for device %p.\n", window, device);
5211 else if (message == WM_DISPLAYCHANGE)
5213 device->device_parent->ops->mode_changed(device->device_parent);
5215 else if (message == WM_ACTIVATEAPP)
5217 UINT i;
5219 for (i = 0; i < device->swapchain_count; i++)
5220 wined3d_swapchain_activate(device->swapchains[i], wparam);
5222 device->device_parent->ops->activate(device->device_parent, wparam);
5224 else if (message == WM_SYSCOMMAND)
5226 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5228 if (unicode)
5229 DefWindowProcW(window, message, wparam, lparam);
5230 else
5231 DefWindowProcA(window, message, wparam, lparam);
5235 if (unicode)
5236 return CallWindowProcW(proc, window, message, wparam, lparam);
5237 else
5238 return CallWindowProcA(proc, window, message, wparam, lparam);