wined3d: Properly check for 3D/volume textures in wined3d_device_update_texture().
[wine.git] / dlls / wined3d / device.c
blobf00c90647961be7ad62fab25b59ea241c46612d4
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_surface *depth_stencil = fb->depth_stencil
296 ? wined3d_rendertarget_view_get_surface(fb->depth_stencil) : 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;
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_surface_prepare(rt, context, rtv->resource->draw_binding);
338 if (target)
340 render_offscreen = context->render_offscreen;
341 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
343 else
345 render_offscreen = TRUE;
346 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
347 depth_stencil->texture_level);
348 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
349 depth_stencil->texture_level);
352 if (depth_stencil && render_offscreen)
353 wined3d_surface_prepare(depth_stencil, context, depth_stencil->container->resource.draw_binding);
355 if (flags & WINED3DCLEAR_ZBUFFER)
357 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
359 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
360 device_switch_onscreen_ds(device, context, depth_stencil);
361 prepare_ds_clear(depth_stencil, context, location,
362 draw_rect, rect_count, clear_rect, &ds_rect);
365 if (!context_apply_clear_state(context, state, rt_count, fb))
367 context_release(context);
368 WARN("Failed to apply clear state, skipping clear.\n");
369 return;
372 /* Only set the values up once, as they are not changing. */
373 if (flags & WINED3DCLEAR_STENCIL)
375 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
377 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
378 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
380 gl_info->gl_ops.gl.p_glStencilMask(~0U);
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
382 gl_info->gl_ops.gl.p_glClearStencil(stencil);
383 checkGLcall("glClearStencil");
384 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
387 if (flags & WINED3DCLEAR_ZBUFFER)
389 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
391 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
393 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
394 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
395 gl_info->gl_ops.gl.p_glClearDepth(depth);
396 checkGLcall("glClearDepth");
397 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
400 if (flags & WINED3DCLEAR_TARGET)
402 for (i = 0; i < rt_count; ++i)
404 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
405 struct wined3d_texture *texture;
407 if (!rtv)
408 continue;
410 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
412 FIXME("Not supported on buffer resources.\n");
413 continue;
416 texture = wined3d_texture_from_resource(rtv->resource);
417 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
418 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
421 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
423 if (rt_count > 1)
424 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
425 "support, this might cause graphical issues.\n");
427 corrected_color.r = color->r < wined3d_srgb_const1[0]
428 ? color->r * wined3d_srgb_const0[3]
429 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
430 - wined3d_srgb_const0[2];
431 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
432 corrected_color.g = color->g < wined3d_srgb_const1[0]
433 ? color->g * wined3d_srgb_const0[3]
434 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
435 - wined3d_srgb_const0[2];
436 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
437 corrected_color.b = color->b < wined3d_srgb_const1[0]
438 ? color->b * wined3d_srgb_const0[3]
439 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
440 - wined3d_srgb_const0[2];
441 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
442 color = &corrected_color;
445 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
446 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
447 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
448 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
449 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
450 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
451 checkGLcall("glClearColor");
452 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
455 if (!rect_count)
457 if (render_offscreen)
459 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
460 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
462 else
464 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
465 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
467 checkGLcall("glScissor");
468 gl_info->gl_ops.gl.p_glClear(clear_mask);
469 checkGLcall("glClear");
471 else
473 RECT current_rect;
475 /* Now process each rect in turn. */
476 for (i = 0; i < rect_count; ++i)
478 /* Note that GL uses lower left, width/height. */
479 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
481 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
482 wine_dbgstr_rect(&clear_rect[i]),
483 wine_dbgstr_rect(&current_rect));
485 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
486 * The rectangle is not cleared, no error is returned, but further rectangles are
487 * still cleared if they are valid. */
488 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
490 TRACE("Rectangle with negative dimensions, ignoring.\n");
491 continue;
494 if (render_offscreen)
496 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
497 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
499 else
501 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
502 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
504 checkGLcall("glScissor");
506 gl_info->gl_ops.gl.p_glClear(clear_mask);
507 checkGLcall("glClear");
511 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
512 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
513 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
515 context_release(context);
518 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
520 ULONG refcount = InterlockedIncrement(&device->ref);
522 TRACE("%p increasing refcount to %u.\n", device, refcount);
524 return refcount;
527 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
529 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
531 ERR("Leftover sampler %p.\n", sampler);
534 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
536 ULONG refcount = InterlockedDecrement(&device->ref);
538 TRACE("%p decreasing refcount to %u.\n", device, refcount);
540 if (!refcount)
542 UINT i;
544 wined3d_cs_destroy(device->cs);
546 if (device->recording && wined3d_stateblock_decref(device->recording))
547 FIXME("Something's still holding the recording stateblock.\n");
548 device->recording = NULL;
550 state_cleanup(&device->state);
552 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
554 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
555 device->multistate_funcs[i] = NULL;
558 if (!list_empty(&device->resources))
560 struct wined3d_resource *resource;
562 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
564 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
566 FIXME("Leftover resource %p with type %s (%#x).\n",
567 resource, debug_d3dresourcetype(resource->type), resource->type);
571 if (device->contexts)
572 ERR("Context array not freed!\n");
573 if (device->hardwareCursor)
574 DestroyCursor(device->hardwareCursor);
575 device->hardwareCursor = 0;
577 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
579 wined3d_decref(device->wined3d);
580 device->wined3d = NULL;
581 HeapFree(GetProcessHeap(), 0, device);
582 TRACE("Freed device %p.\n", device);
585 return refcount;
588 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
590 TRACE("device %p.\n", device);
592 return device->swapchain_count;
595 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
597 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
599 if (swapchain_idx >= device->swapchain_count)
601 WARN("swapchain_idx %u >= swapchain_count %u.\n",
602 swapchain_idx, device->swapchain_count);
603 return NULL;
606 return device->swapchains[swapchain_idx];
609 static void device_load_logo(struct wined3d_device *device, const char *filename)
611 struct wined3d_color_key color_key;
612 struct wined3d_resource_desc desc;
613 HBITMAP hbm;
614 BITMAP bm;
615 HRESULT hr;
616 HDC dcb = NULL, dcs = NULL;
618 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
619 if(hbm)
621 GetObjectA(hbm, sizeof(BITMAP), &bm);
622 dcb = CreateCompatibleDC(NULL);
623 if(!dcb) goto out;
624 SelectObject(dcb, hbm);
626 else
628 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
629 * couldn't be loaded
631 memset(&bm, 0, sizeof(bm));
632 bm.bmWidth = 32;
633 bm.bmHeight = 32;
636 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
637 desc.format = WINED3DFMT_B5G6R5_UNORM;
638 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
639 desc.multisample_quality = 0;
640 desc.usage = WINED3DUSAGE_DYNAMIC;
641 desc.pool = WINED3D_POOL_DEFAULT;
642 desc.width = bm.bmWidth;
643 desc.height = bm.bmHeight;
644 desc.depth = 1;
645 desc.size = 0;
646 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
647 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
649 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
650 goto out;
653 if (dcb)
655 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
656 goto out;
657 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
658 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
660 color_key.color_space_low_value = 0;
661 color_key.color_space_high_value = 0;
662 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
664 else
666 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
667 const RECT rect = {0, 0, desc.width, desc.height};
668 struct wined3d_surface *surface;
670 /* Fill the surface with a white color to show that wined3d is there */
671 surface = device->logo_texture->sub_resources[0].u.surface;
672 surface_color_fill(surface, &rect, &c);
675 out:
676 if (dcb) DeleteDC(dcb);
677 if (hbm) DeleteObject(hbm);
680 /* Context activation is done by the caller. */
681 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
683 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
684 unsigned int i, j, count;
685 /* Under DirectX you can sample even if no texture is bound, whereas
686 * OpenGL will only allow that when a valid texture is bound.
687 * We emulate this by creating dummy textures and binding them
688 * to each texture stage when the currently set D3D texture is NULL. */
690 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
691 for (i = 0; i < count; ++i)
693 static const DWORD color = 0x000000ff;
695 /* Make appropriate texture active */
696 context_active_texture(context, gl_info, i);
698 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
699 checkGLcall("glGenTextures");
700 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
702 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
703 checkGLcall("glBindTexture");
705 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
706 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
707 checkGLcall("glTexImage2D");
709 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
711 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
712 checkGLcall("glGenTextures");
713 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
715 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
716 checkGLcall("glBindTexture");
718 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
719 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
720 checkGLcall("glTexImage2D");
723 if (gl_info->supported[EXT_TEXTURE3D])
725 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
726 checkGLcall("glGenTextures");
727 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
729 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
730 checkGLcall("glBindTexture");
732 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
733 checkGLcall("glTexImage3D");
736 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
738 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
739 checkGLcall("glGenTextures");
740 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
742 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
743 checkGLcall("glBindTexture");
745 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
747 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
748 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
749 checkGLcall("glTexImage2D");
755 /* Context activation is done by the caller. */
756 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
758 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
760 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
762 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
763 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
766 if (gl_info->supported[EXT_TEXTURE3D])
768 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
769 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
772 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
774 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
775 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
778 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
779 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
781 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
782 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
783 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
784 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
787 /* Context activation is done by the caller. */
788 static void create_default_sampler(struct wined3d_device *device)
790 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
793 * In SM4+ shaders there is a separation between resources and samplers. Some of shader
794 * instructions allow to access resources without using samplers.
795 * In GLSL resources are always accessed through sampler or image variables. The default
796 * sampler object is used to emulate the direct resource access when there is no sampler state
797 * to use.
800 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
802 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
803 checkGLcall("glGenSamplers");
804 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
805 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
806 checkGLcall("glSamplerParamteri");
808 else
810 device->default_sampler = 0;
814 /* Context activation is done by the caller. */
815 static void destroy_default_sampler(struct wined3d_device *device)
817 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
819 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
821 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
822 checkGLcall("glDeleteSamplers");
825 device->default_sampler = 0;
828 static LONG fullscreen_style(LONG style)
830 /* Make sure the window is managed, otherwise we won't get keyboard input. */
831 style |= WS_POPUP | WS_SYSMENU;
832 style &= ~(WS_CAPTION | WS_THICKFRAME);
834 return style;
837 static LONG fullscreen_exstyle(LONG exstyle)
839 /* Filter out window decorations. */
840 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
842 return exstyle;
845 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
847 BOOL filter_messages;
848 LONG style, exstyle;
850 TRACE("Setting up window %p for fullscreen mode.\n", window);
852 if (device->style || device->exStyle)
854 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
855 window, device->style, device->exStyle);
858 device->style = GetWindowLongW(window, GWL_STYLE);
859 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
861 style = fullscreen_style(device->style);
862 exstyle = fullscreen_exstyle(device->exStyle);
864 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
865 device->style, device->exStyle, style, exstyle);
867 filter_messages = device->filter_messages;
868 device->filter_messages = TRUE;
870 SetWindowLongW(window, GWL_STYLE, style);
871 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
872 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
874 device->filter_messages = filter_messages;
877 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
879 BOOL filter_messages;
880 LONG style, exstyle;
882 if (!device->style && !device->exStyle) return;
884 style = GetWindowLongW(window, GWL_STYLE);
885 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
887 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
888 * application, and we want to ignore them in the test below, since it's
889 * not the application's fault that they changed. Additionally, we want to
890 * preserve the current status of these flags (i.e. don't restore them) to
891 * more closely emulate the behavior of Direct3D, which leaves these flags
892 * alone when returning to windowed mode. */
893 device->style ^= (device->style ^ style) & WS_VISIBLE;
894 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
896 TRACE("Restoring window style of window %p to %08x, %08x.\n",
897 window, device->style, device->exStyle);
899 filter_messages = device->filter_messages;
900 device->filter_messages = TRUE;
902 /* Only restore the style if the application didn't modify it during the
903 * fullscreen phase. Some applications change it before calling Reset()
904 * when switching between windowed and fullscreen modes (HL2), some
905 * depend on the original style (Eve Online). */
906 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
908 SetWindowLongW(window, GWL_STYLE, device->style);
909 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
911 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
913 device->filter_messages = filter_messages;
915 /* Delete the old values. */
916 device->style = 0;
917 device->exStyle = 0;
920 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
922 TRACE("device %p, window %p.\n", device, window);
924 if (!wined3d_register_window(window, device))
926 ERR("Failed to register window %p.\n", window);
927 return E_FAIL;
930 InterlockedExchangePointer((void **)&device->focus_window, window);
931 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
933 return WINED3D_OK;
936 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
938 TRACE("device %p.\n", device);
940 if (device->focus_window) wined3d_unregister_window(device->focus_window);
941 InterlockedExchangePointer((void **)&device->focus_window, NULL);
944 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
946 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
947 unsigned int i;
949 if (device->fb.render_targets)
951 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
953 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
955 if (device->back_buffer_view)
956 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
959 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
960 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
963 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
964 struct wined3d_swapchain_desc *swapchain_desc)
966 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
967 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
968 struct wined3d_swapchain *swapchain = NULL;
969 struct wined3d_context *context;
970 DWORD clear_flags = 0;
971 HRESULT hr;
973 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
975 if (device->d3d_initialized)
976 return WINED3DERR_INVALIDCALL;
977 if (device->wined3d->flags & WINED3D_NO3D)
978 return WINED3DERR_INVALIDCALL;
980 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
981 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
983 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
984 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
986 TRACE("Shader private data couldn't be allocated\n");
987 goto err_out;
989 if (FAILED(hr = device->blitter->alloc_private(device)))
991 TRACE("Blitter private data couldn't be allocated\n");
992 goto err_out;
995 /* Setup the implicit swapchain. This also initializes a context. */
996 TRACE("Creating implicit swapchain\n");
997 hr = device->device_parent->ops->create_swapchain(device->device_parent,
998 swapchain_desc, &swapchain);
999 if (FAILED(hr))
1001 WARN("Failed to create implicit swapchain\n");
1002 goto err_out;
1005 if (swapchain_desc->backbuffer_count)
1007 struct wined3d_rendertarget_view_desc view_desc;
1009 view_desc.format_id = swapchain_desc->backbuffer_format;
1010 view_desc.u.texture.level_idx = 0;
1011 view_desc.u.texture.layer_idx = 0;
1012 view_desc.u.texture.layer_count = 1;
1013 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
1014 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1016 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1017 goto err_out;
1021 device->swapchain_count = 1;
1022 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1023 if (!device->swapchains)
1025 ERR("Out of memory!\n");
1026 goto err_out;
1028 device->swapchains[0] = swapchain;
1029 device_init_swapchain_state(device, swapchain);
1031 context = context_acquire(device, swapchain->front_buffer->sub_resources[0].u.surface);
1033 create_dummy_textures(device, context);
1034 create_default_sampler(device);
1036 device->contexts[0]->last_was_rhw = 0;
1038 TRACE("All defaults now set up, leaving 3D init.\n");
1040 context_release(context);
1042 /* Clear the screen */
1043 if (swapchain->back_buffers && swapchain->back_buffers[0])
1044 clear_flags |= WINED3DCLEAR_TARGET;
1045 if (swapchain_desc->enable_auto_depth_stencil)
1046 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1047 if (clear_flags)
1048 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1050 device->d3d_initialized = TRUE;
1052 if (wined3d_settings.logo)
1053 device_load_logo(device, wined3d_settings.logo);
1054 return WINED3D_OK;
1056 err_out:
1057 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1058 HeapFree(GetProcessHeap(), 0, device->swapchains);
1059 device->swapchain_count = 0;
1060 if (device->back_buffer_view)
1061 wined3d_rendertarget_view_decref(device->back_buffer_view);
1062 if (swapchain)
1063 wined3d_swapchain_decref(swapchain);
1064 if (device->blit_priv)
1065 device->blitter->free_private(device);
1066 if (device->shader_priv)
1067 device->shader_backend->shader_free_private(device);
1069 return hr;
1072 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1073 struct wined3d_swapchain_desc *swapchain_desc)
1075 struct wined3d_swapchain *swapchain = NULL;
1076 HRESULT hr;
1078 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1080 /* Setup the implicit swapchain */
1081 TRACE("Creating implicit swapchain\n");
1082 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1083 swapchain_desc, &swapchain);
1084 if (FAILED(hr))
1086 WARN("Failed to create implicit swapchain\n");
1087 goto err_out;
1090 device->swapchain_count = 1;
1091 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1092 if (!device->swapchains)
1094 ERR("Out of memory!\n");
1095 goto err_out;
1097 device->swapchains[0] = swapchain;
1098 return WINED3D_OK;
1100 err_out:
1101 wined3d_swapchain_decref(swapchain);
1102 return hr;
1105 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1107 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1109 wined3d_sampler_decref(sampler);
1112 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1114 struct wined3d_resource *resource, *cursor;
1115 const struct wined3d_gl_info *gl_info;
1116 struct wined3d_context *context;
1117 struct wined3d_surface *surface;
1118 UINT i;
1120 TRACE("device %p.\n", device);
1122 if (!device->d3d_initialized)
1123 return WINED3DERR_INVALIDCALL;
1125 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1126 * it was created. Thus make sure a context is active for the glDelete* calls
1128 context = context_acquire(device, NULL);
1129 gl_info = context->gl_info;
1131 if (device->logo_texture)
1132 wined3d_texture_decref(device->logo_texture);
1133 if (device->cursor_texture)
1134 wined3d_texture_decref(device->cursor_texture);
1136 state_unbind_resources(&device->state);
1138 /* Unload resources */
1139 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1141 if (resource->type == WINED3D_RTYPE_SURFACE || resource->type == WINED3D_RTYPE_VOLUME)
1142 continue;
1144 TRACE("Unloading resource %p.\n", resource);
1145 resource->resource_ops->resource_unload(resource);
1148 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1150 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1151 * private data, it might contain opengl pointers
1153 if (device->depth_blt_texture)
1155 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1156 device->depth_blt_texture = 0;
1159 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1160 device->blitter->free_private(device);
1161 device->shader_backend->shader_free_private(device);
1162 destroy_dummy_textures(device, gl_info);
1163 destroy_default_sampler(device);
1165 /* Release the context again as soon as possible. In particular,
1166 * releasing the render target views below may release the last reference
1167 * to the swapchain associated with this context, which in turn will
1168 * destroy the context. */
1169 context_release(context);
1171 /* Release the buffers (with sanity checks)*/
1172 if (device->onscreen_depth_stencil)
1174 surface = device->onscreen_depth_stencil;
1175 device->onscreen_depth_stencil = NULL;
1176 wined3d_texture_decref(surface->container);
1179 if (device->fb.depth_stencil)
1181 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1183 TRACE("Releasing depth/stencil view %p.\n", view);
1185 device->fb.depth_stencil = NULL;
1186 wined3d_rendertarget_view_decref(view);
1189 if (device->auto_depth_stencil_view)
1191 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1193 device->auto_depth_stencil_view = NULL;
1194 if (wined3d_rendertarget_view_decref(view))
1195 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1198 for (i = 0; i < gl_info->limits.buffers; ++i)
1200 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1202 if (device->back_buffer_view)
1204 wined3d_rendertarget_view_decref(device->back_buffer_view);
1205 device->back_buffer_view = NULL;
1208 for (i = 0; i < device->swapchain_count; ++i)
1210 TRACE("Releasing the implicit swapchain %u.\n", i);
1211 if (wined3d_swapchain_decref(device->swapchains[i]))
1212 FIXME("Something's still holding the implicit swapchain.\n");
1215 HeapFree(GetProcessHeap(), 0, device->swapchains);
1216 device->swapchains = NULL;
1217 device->swapchain_count = 0;
1219 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1220 device->fb.render_targets = NULL;
1222 device->d3d_initialized = FALSE;
1224 return WINED3D_OK;
1227 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1229 unsigned int i;
1231 for (i = 0; i < device->swapchain_count; ++i)
1233 TRACE("Releasing the implicit swapchain %u.\n", i);
1234 if (wined3d_swapchain_decref(device->swapchains[i]))
1235 FIXME("Something's still holding the implicit swapchain.\n");
1238 HeapFree(GetProcessHeap(), 0, device->swapchains);
1239 device->swapchains = NULL;
1240 device->swapchain_count = 0;
1241 return WINED3D_OK;
1244 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1245 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1246 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1248 * There is no way to deactivate thread safety once it is enabled.
1250 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1252 TRACE("device %p.\n", device);
1254 /* For now just store the flag (needed in case of ddraw). */
1255 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1258 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1260 TRACE("device %p.\n", device);
1262 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1263 wine_dbgstr_longlong(device->adapter->vram_bytes),
1264 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1265 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1267 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1270 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1271 struct wined3d_buffer *buffer, UINT offset)
1273 struct wined3d_stream_output *stream;
1274 struct wined3d_buffer *prev_buffer;
1276 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1278 if (idx >= MAX_STREAM_OUT)
1280 WARN("Invalid stream output %u.\n", idx);
1281 return;
1284 stream = &device->update_state->stream_output[idx];
1285 prev_buffer = stream->buffer;
1287 if (buffer)
1288 wined3d_buffer_incref(buffer);
1289 stream->buffer = buffer;
1290 stream->offset = offset;
1291 if (!device->recording)
1292 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1293 if (prev_buffer)
1294 wined3d_buffer_decref(prev_buffer);
1297 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1298 UINT idx, UINT *offset)
1300 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1302 if (idx >= MAX_STREAM_OUT)
1304 WARN("Invalid stream output %u.\n", idx);
1305 return NULL;
1308 if (offset)
1309 *offset = device->state.stream_output[idx].offset;
1310 return device->state.stream_output[idx].buffer;
1313 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1314 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1316 struct wined3d_stream_state *stream;
1317 struct wined3d_buffer *prev_buffer;
1319 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1320 device, stream_idx, buffer, offset, stride);
1322 if (stream_idx >= MAX_STREAMS)
1324 WARN("Stream index %u out of range.\n", stream_idx);
1325 return WINED3DERR_INVALIDCALL;
1327 else if (offset & 0x3)
1329 WARN("Offset %u is not 4 byte aligned.\n", offset);
1330 return WINED3DERR_INVALIDCALL;
1333 stream = &device->update_state->streams[stream_idx];
1334 prev_buffer = stream->buffer;
1336 if (device->recording)
1337 device->recording->changed.streamSource |= 1u << stream_idx;
1339 if (prev_buffer == buffer
1340 && stream->stride == stride
1341 && stream->offset == offset)
1343 TRACE("Application is setting the old values over, nothing to do.\n");
1344 return WINED3D_OK;
1347 stream->buffer = buffer;
1348 if (buffer)
1350 stream->stride = stride;
1351 stream->offset = offset;
1352 wined3d_buffer_incref(buffer);
1355 if (!device->recording)
1356 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1357 if (prev_buffer)
1358 wined3d_buffer_decref(prev_buffer);
1360 return WINED3D_OK;
1363 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1364 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1366 const struct wined3d_stream_state *stream;
1368 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1369 device, stream_idx, buffer, offset, stride);
1371 if (stream_idx >= MAX_STREAMS)
1373 WARN("Stream index %u out of range.\n", stream_idx);
1374 return WINED3DERR_INVALIDCALL;
1377 stream = &device->state.streams[stream_idx];
1378 *buffer = stream->buffer;
1379 if (offset)
1380 *offset = stream->offset;
1381 *stride = stream->stride;
1383 return WINED3D_OK;
1386 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1388 struct wined3d_stream_state *stream;
1389 UINT old_flags, old_freq;
1391 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1393 /* Verify input. At least in d3d9 this is invalid. */
1394 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1396 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1397 return WINED3DERR_INVALIDCALL;
1399 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1401 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1402 return WINED3DERR_INVALIDCALL;
1404 if (!divider)
1406 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1407 return WINED3DERR_INVALIDCALL;
1410 stream = &device->update_state->streams[stream_idx];
1411 old_flags = stream->flags;
1412 old_freq = stream->frequency;
1414 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1415 stream->frequency = divider & 0x7fffff;
1417 if (device->recording)
1418 device->recording->changed.streamFreq |= 1u << stream_idx;
1419 else if (stream->frequency != old_freq || stream->flags != old_flags)
1420 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1422 return WINED3D_OK;
1425 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1426 UINT stream_idx, UINT *divider)
1428 const struct wined3d_stream_state *stream;
1430 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1432 stream = &device->state.streams[stream_idx];
1433 *divider = stream->flags | stream->frequency;
1435 TRACE("Returning %#x.\n", *divider);
1437 return WINED3D_OK;
1440 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1441 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1443 TRACE("device %p, state %s, matrix %p.\n",
1444 device, debug_d3dtstype(d3dts), matrix);
1445 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1446 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1447 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1448 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1450 /* Handle recording of state blocks. */
1451 if (device->recording)
1453 TRACE("Recording... not performing anything.\n");
1454 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1455 device->update_state->transforms[d3dts] = *matrix;
1456 return;
1459 /* If the new matrix is the same as the current one,
1460 * we cut off any further processing. this seems to be a reasonable
1461 * optimization because as was noticed, some apps (warcraft3 for example)
1462 * tend towards setting the same matrix repeatedly for some reason.
1464 * From here on we assume that the new matrix is different, wherever it matters. */
1465 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1467 TRACE("The application is setting the same matrix over again.\n");
1468 return;
1471 device->state.transforms[d3dts] = *matrix;
1472 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1475 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1476 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1478 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1480 *matrix = device->state.transforms[state];
1483 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1484 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1486 const struct wined3d_matrix *mat;
1487 struct wined3d_matrix temp;
1489 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1491 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1492 * below means it will be recorded in a state block change, but it
1493 * works regardless where it is recorded.
1494 * If this is found to be wrong, change to StateBlock. */
1495 if (state > HIGHEST_TRANSFORMSTATE)
1497 WARN("Unhandled transform state %#x.\n", state);
1498 return;
1501 mat = &device->update_state->transforms[state];
1502 multiply_matrix(&temp, mat, matrix);
1504 /* Apply change via set transform - will reapply to eg. lights this way. */
1505 wined3d_device_set_transform(device, state, &temp);
1508 /* Note lights are real special cases. Although the device caps state only
1509 * e.g. 8 are supported, you can reference any indexes you want as long as
1510 * that number max are enabled at any one point in time. Therefore since the
1511 * indices can be anything, we need a hashmap of them. However, this causes
1512 * stateblock problems. When capturing the state block, I duplicate the
1513 * hashmap, but when recording, just build a chain pretty much of commands to
1514 * be replayed. */
1515 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1516 UINT light_idx, const struct wined3d_light *light)
1518 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1519 struct wined3d_light_info *object = NULL;
1520 struct list *e;
1521 float rho;
1523 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1525 /* Check the parameter range. Need for speed most wanted sets junk lights
1526 * which confuse the GL driver. */
1527 if (!light)
1528 return WINED3DERR_INVALIDCALL;
1530 switch (light->type)
1532 case WINED3D_LIGHT_POINT:
1533 case WINED3D_LIGHT_SPOT:
1534 case WINED3D_LIGHT_GLSPOT:
1535 /* Incorrect attenuation values can cause the gl driver to crash.
1536 * Happens with Need for speed most wanted. */
1537 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1539 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1540 return WINED3DERR_INVALIDCALL;
1542 break;
1544 case WINED3D_LIGHT_DIRECTIONAL:
1545 case WINED3D_LIGHT_PARALLELPOINT:
1546 /* Ignores attenuation */
1547 break;
1549 default:
1550 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1551 return WINED3DERR_INVALIDCALL;
1554 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1556 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1557 if (object->OriginalIndex == light_idx)
1558 break;
1559 object = NULL;
1562 if (!object)
1564 TRACE("Adding new light\n");
1565 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1566 if (!object)
1567 return E_OUTOFMEMORY;
1569 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1570 object->glIndex = -1;
1571 object->OriginalIndex = light_idx;
1574 /* Initialize the object. */
1575 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1576 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1577 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1578 light_idx, light->type, debug_color(&light->diffuse),
1579 debug_color(&light->specular), debug_color(&light->ambient),
1580 light->position.x, light->position.y, light->position.z,
1581 light->direction.x, light->direction.y, light->direction.z,
1582 light->range, light->falloff, light->theta, light->phi);
1584 /* Update the live definitions if the light is currently assigned a glIndex. */
1585 if (object->glIndex != -1 && !device->recording)
1587 if (object->OriginalParms.type != light->type)
1588 device_invalidate_state(device, STATE_LIGHT_TYPE);
1589 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1592 /* Save away the information. */
1593 object->OriginalParms = *light;
1595 switch (light->type)
1597 case WINED3D_LIGHT_POINT:
1598 /* Position */
1599 object->position.x = light->position.x;
1600 object->position.y = light->position.y;
1601 object->position.z = light->position.z;
1602 object->position.w = 1.0f;
1603 object->cutoff = 180.0f;
1604 /* FIXME: Range */
1605 break;
1607 case WINED3D_LIGHT_DIRECTIONAL:
1608 /* Direction */
1609 object->direction.x = -light->direction.x;
1610 object->direction.y = -light->direction.y;
1611 object->direction.z = -light->direction.z;
1612 object->direction.w = 0.0f;
1613 object->exponent = 0.0f;
1614 object->cutoff = 180.0f;
1615 break;
1617 case WINED3D_LIGHT_SPOT:
1618 /* Position */
1619 object->position.x = light->position.x;
1620 object->position.y = light->position.y;
1621 object->position.z = light->position.z;
1622 object->position.w = 1.0f;
1624 /* Direction */
1625 object->direction.x = light->direction.x;
1626 object->direction.y = light->direction.y;
1627 object->direction.z = light->direction.z;
1628 object->direction.w = 0.0f;
1630 /* opengl-ish and d3d-ish spot lights use too different models
1631 * for the light "intensity" as a function of the angle towards
1632 * the main light direction, so we only can approximate very
1633 * roughly. However, spot lights are rather rarely used in games
1634 * (if ever used at all). Furthermore if still used, probably
1635 * nobody pays attention to such details. */
1636 if (!light->falloff)
1638 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1639 * equations have the falloff resp. exponent parameter as an
1640 * exponent, so the spot light lighting will always be 1.0 for
1641 * both of them, and we don't have to care for the rest of the
1642 * rather complex calculation. */
1643 object->exponent = 0.0f;
1645 else
1647 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1648 if (rho < 0.0001f)
1649 rho = 0.0001f;
1650 object->exponent = -0.3f / logf(cosf(rho / 2));
1653 if (object->exponent > 128.0f)
1654 object->exponent = 128.0f;
1656 object->cutoff = (float)(light->phi * 90 / M_PI);
1657 /* FIXME: Range */
1658 break;
1660 case WINED3D_LIGHT_PARALLELPOINT:
1661 object->position.x = light->position.x;
1662 object->position.y = light->position.y;
1663 object->position.z = light->position.z;
1664 object->position.w = 1.0f;
1665 break;
1667 default:
1668 FIXME("Unrecognized light type %#x.\n", light->type);
1671 return WINED3D_OK;
1674 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1675 UINT light_idx, struct wined3d_light *light)
1677 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1678 struct wined3d_light_info *light_info = NULL;
1679 struct list *e;
1681 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1683 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1685 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1686 if (light_info->OriginalIndex == light_idx)
1687 break;
1688 light_info = NULL;
1691 if (!light_info)
1693 TRACE("Light information requested but light not defined\n");
1694 return WINED3DERR_INVALIDCALL;
1697 *light = light_info->OriginalParms;
1698 return WINED3D_OK;
1701 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1703 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1704 struct wined3d_light_info *light_info = NULL;
1705 struct list *e;
1707 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1709 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1711 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1712 if (light_info->OriginalIndex == light_idx)
1713 break;
1714 light_info = NULL;
1716 TRACE("Found light %p.\n", light_info);
1718 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1719 if (!light_info)
1721 TRACE("Light enabled requested but light not defined, so defining one!\n");
1722 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1724 /* Search for it again! Should be fairly quick as near head of list. */
1725 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1727 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1728 if (light_info->OriginalIndex == light_idx)
1729 break;
1730 light_info = NULL;
1732 if (!light_info)
1734 FIXME("Adding default lights has failed dismally\n");
1735 return WINED3DERR_INVALIDCALL;
1739 if (!enable)
1741 if (light_info->glIndex != -1)
1743 if (!device->recording)
1745 device_invalidate_state(device, STATE_LIGHT_TYPE);
1746 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1749 device->update_state->lights[light_info->glIndex] = NULL;
1750 light_info->glIndex = -1;
1752 else
1754 TRACE("Light already disabled, nothing to do\n");
1756 light_info->enabled = FALSE;
1758 else
1760 light_info->enabled = TRUE;
1761 if (light_info->glIndex != -1)
1763 TRACE("Nothing to do as light was enabled\n");
1765 else
1767 unsigned int i;
1768 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1769 /* Find a free GL light. */
1770 for (i = 0; i < gl_info->limits.lights; ++i)
1772 if (!device->update_state->lights[i])
1774 device->update_state->lights[i] = light_info;
1775 light_info->glIndex = i;
1776 break;
1779 if (light_info->glIndex == -1)
1781 /* Our tests show that Windows returns D3D_OK in this situation, even with
1782 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1783 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1784 * as well for those lights.
1786 * TODO: Test how this affects rendering. */
1787 WARN("Too many concurrently active lights\n");
1788 return WINED3D_OK;
1791 /* i == light_info->glIndex */
1792 if (!device->recording)
1794 device_invalidate_state(device, STATE_LIGHT_TYPE);
1795 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1800 return WINED3D_OK;
1803 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1805 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1806 struct wined3d_light_info *light_info = NULL;
1807 struct list *e;
1809 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1811 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1813 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1814 if (light_info->OriginalIndex == light_idx)
1815 break;
1816 light_info = NULL;
1819 if (!light_info)
1821 TRACE("Light enabled state requested but light not defined.\n");
1822 return WINED3DERR_INVALIDCALL;
1824 /* true is 128 according to SetLightEnable */
1825 *enable = light_info->enabled ? 128 : 0;
1826 return WINED3D_OK;
1829 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1830 UINT plane_idx, const struct wined3d_vec4 *plane)
1832 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1834 /* Validate plane_idx. */
1835 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1837 TRACE("Application has requested clipplane this device doesn't support.\n");
1838 return WINED3DERR_INVALIDCALL;
1841 if (device->recording)
1842 device->recording->changed.clipplane |= 1u << plane_idx;
1844 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1846 TRACE("Application is setting old values over, nothing to do.\n");
1847 return WINED3D_OK;
1850 device->update_state->clip_planes[plane_idx] = *plane;
1852 if (!device->recording)
1853 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1855 return WINED3D_OK;
1858 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1859 UINT plane_idx, struct wined3d_vec4 *plane)
1861 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1863 /* Validate plane_idx. */
1864 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1866 TRACE("Application has requested clipplane this device doesn't support.\n");
1867 return WINED3DERR_INVALIDCALL;
1870 *plane = device->state.clip_planes[plane_idx];
1872 return WINED3D_OK;
1875 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1876 const struct wined3d_clip_status *clip_status)
1878 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1880 if (!clip_status)
1881 return WINED3DERR_INVALIDCALL;
1883 return WINED3D_OK;
1886 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1887 struct wined3d_clip_status *clip_status)
1889 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1891 if (!clip_status)
1892 return WINED3DERR_INVALIDCALL;
1894 return WINED3D_OK;
1897 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1899 TRACE("device %p, material %p.\n", device, material);
1901 device->update_state->material = *material;
1903 if (device->recording)
1904 device->recording->changed.material = TRUE;
1905 else
1906 wined3d_cs_emit_set_material(device->cs, material);
1909 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1911 TRACE("device %p, material %p.\n", device, material);
1913 *material = device->state.material;
1915 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1916 TRACE("ambient %s\n", debug_color(&material->ambient));
1917 TRACE("specular %s\n", debug_color(&material->specular));
1918 TRACE("emissive %s\n", debug_color(&material->emissive));
1919 TRACE("power %.8e.\n", material->power);
1922 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1923 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1925 enum wined3d_format_id prev_format;
1926 struct wined3d_buffer *prev_buffer;
1928 TRACE("device %p, buffer %p, format %s.\n",
1929 device, buffer, debug_d3dformat(format_id));
1931 prev_buffer = device->update_state->index_buffer;
1932 prev_format = device->update_state->index_format;
1934 device->update_state->index_buffer = buffer;
1935 device->update_state->index_format = format_id;
1937 if (device->recording)
1938 device->recording->changed.indices = TRUE;
1940 if (prev_buffer == buffer && prev_format == format_id)
1941 return;
1943 if (buffer)
1944 wined3d_buffer_incref(buffer);
1945 if (!device->recording)
1946 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1947 if (prev_buffer)
1948 wined3d_buffer_decref(prev_buffer);
1951 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1952 enum wined3d_format_id *format)
1954 TRACE("device %p, format %p.\n", device, format);
1956 *format = device->state.index_format;
1957 return device->state.index_buffer;
1960 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1962 TRACE("device %p, base_index %d.\n", device, base_index);
1964 device->update_state->base_vertex_index = base_index;
1967 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1969 TRACE("device %p.\n", device);
1971 return device->state.base_vertex_index;
1974 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1976 TRACE("device %p, viewport %p.\n", device, viewport);
1977 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1978 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1980 device->update_state->viewport = *viewport;
1982 /* Handle recording of state blocks */
1983 if (device->recording)
1985 TRACE("Recording... not performing anything\n");
1986 device->recording->changed.viewport = TRUE;
1987 return;
1990 wined3d_cs_emit_set_viewport(device->cs, viewport);
1993 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1995 TRACE("device %p, viewport %p.\n", device, viewport);
1997 *viewport = device->state.viewport;
2000 static void resolve_depth_buffer(struct wined3d_state *state)
2002 struct wined3d_texture *dst_texture = state->textures[0];
2003 struct wined3d_rendertarget_view *src_view;
2004 RECT src_rect, dst_rect;
2006 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2007 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2008 return;
2010 if (!(src_view = state->fb->depth_stencil))
2011 return;
2012 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2014 FIXME("Not supported on buffer resources.\n");
2015 return;
2018 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2019 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2020 wined3d_texture_blt(dst_texture, 0, &dst_rect, wined3d_texture_from_resource(src_view->resource),
2021 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2024 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2025 enum wined3d_render_state state, DWORD value)
2027 DWORD old_value;
2029 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2031 if (state > WINEHIGHEST_RENDER_STATE)
2033 WARN("Unhandled render state %#x.\n", state);
2034 return;
2037 old_value = device->state.render_states[state];
2038 device->update_state->render_states[state] = value;
2040 /* Handle recording of state blocks. */
2041 if (device->recording)
2043 TRACE("Recording... not performing anything.\n");
2044 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2045 return;
2048 /* Compared here and not before the assignment to allow proper stateblock recording. */
2049 if (value == old_value)
2050 TRACE("Application is setting the old value over, nothing to do.\n");
2051 else
2052 wined3d_cs_emit_set_render_state(device->cs, state, value);
2054 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2056 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2057 resolve_depth_buffer(&device->state);
2061 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2063 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2065 return device->state.render_states[state];
2068 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2069 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2071 DWORD old_value;
2073 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2074 device, sampler_idx, debug_d3dsamplerstate(state), value);
2076 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2077 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2079 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2081 WARN("Invalid sampler %u.\n", sampler_idx);
2082 return; /* Windows accepts overflowing this array ... we do not. */
2085 old_value = device->state.sampler_states[sampler_idx][state];
2086 device->update_state->sampler_states[sampler_idx][state] = value;
2088 /* Handle recording of state blocks. */
2089 if (device->recording)
2091 TRACE("Recording... not performing anything.\n");
2092 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2093 return;
2096 if (old_value == value)
2098 TRACE("Application is setting the old value over, nothing to do.\n");
2099 return;
2102 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2105 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2106 UINT sampler_idx, enum wined3d_sampler_state state)
2108 TRACE("device %p, sampler_idx %u, state %s.\n",
2109 device, sampler_idx, debug_d3dsamplerstate(state));
2111 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2112 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2114 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2116 WARN("Invalid sampler %u.\n", sampler_idx);
2117 return 0; /* Windows accepts overflowing this array ... we do not. */
2120 return device->state.sampler_states[sampler_idx][state];
2123 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2125 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2127 if (device->recording)
2128 device->recording->changed.scissorRect = TRUE;
2130 if (EqualRect(&device->update_state->scissor_rect, rect))
2132 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2133 return;
2135 CopyRect(&device->update_state->scissor_rect, rect);
2137 if (device->recording)
2139 TRACE("Recording... not performing anything.\n");
2140 return;
2143 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2146 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2148 TRACE("device %p, rect %p.\n", device, rect);
2150 *rect = device->state.scissor_rect;
2151 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2154 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2155 struct wined3d_vertex_declaration *declaration)
2157 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2159 TRACE("device %p, declaration %p.\n", device, declaration);
2161 if (device->recording)
2162 device->recording->changed.vertexDecl = TRUE;
2164 if (declaration == prev)
2165 return;
2167 if (declaration)
2168 wined3d_vertex_declaration_incref(declaration);
2169 device->update_state->vertex_declaration = declaration;
2170 if (!device->recording)
2171 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2172 if (prev)
2173 wined3d_vertex_declaration_decref(prev);
2176 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2178 TRACE("device %p.\n", device);
2180 return device->state.vertex_declaration;
2183 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2185 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2187 TRACE("device %p, shader %p.\n", device, shader);
2189 if (device->recording)
2190 device->recording->changed.vertexShader = TRUE;
2192 if (shader == prev)
2193 return;
2195 if (shader)
2196 wined3d_shader_incref(shader);
2197 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2198 if (!device->recording)
2199 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2200 if (prev)
2201 wined3d_shader_decref(prev);
2204 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2206 TRACE("device %p.\n", device);
2208 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2211 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2212 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2214 struct wined3d_buffer *prev;
2216 if (idx >= MAX_CONSTANT_BUFFERS)
2218 WARN("Invalid constant buffer index %u.\n", idx);
2219 return;
2222 prev = device->update_state->cb[type][idx];
2223 if (buffer == prev)
2224 return;
2226 if (buffer)
2227 wined3d_buffer_incref(buffer);
2228 device->update_state->cb[type][idx] = buffer;
2229 if (!device->recording)
2230 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2231 if (prev)
2232 wined3d_buffer_decref(prev);
2235 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2237 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2239 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2242 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2244 TRACE("device %p, idx %u.\n", device, idx);
2246 if (idx >= MAX_CONSTANT_BUFFERS)
2248 WARN("Invalid constant buffer index %u.\n", idx);
2249 return NULL;
2252 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2255 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2256 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2258 struct wined3d_shader_resource_view *prev;
2260 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2262 WARN("Invalid view index %u.\n", idx);
2263 return;
2266 prev = device->update_state->shader_resource_view[type][idx];
2267 if (view == prev)
2268 return;
2270 if (view)
2271 wined3d_shader_resource_view_incref(view);
2272 device->update_state->shader_resource_view[type][idx] = view;
2273 if (!device->recording)
2274 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2275 if (prev)
2276 wined3d_shader_resource_view_decref(prev);
2279 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2280 UINT idx, struct wined3d_shader_resource_view *view)
2282 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2284 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2287 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2288 UINT idx)
2290 TRACE("device %p, idx %u.\n", device, idx);
2292 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2294 WARN("Invalid view index %u.\n", idx);
2295 return NULL;
2298 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2301 static void wined3d_device_set_sampler(struct wined3d_device *device,
2302 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2304 struct wined3d_sampler *prev;
2306 if (idx >= MAX_SAMPLER_OBJECTS)
2308 WARN("Invalid sampler index %u.\n", idx);
2309 return;
2312 prev = device->update_state->sampler[type][idx];
2313 if (sampler == prev)
2314 return;
2316 if (sampler)
2317 wined3d_sampler_incref(sampler);
2318 device->update_state->sampler[type][idx] = sampler;
2319 if (!device->recording)
2320 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2321 if (prev)
2322 wined3d_sampler_decref(prev);
2325 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2327 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2329 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2332 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2334 TRACE("device %p, idx %u.\n", device, idx);
2336 if (idx >= MAX_SAMPLER_OBJECTS)
2338 WARN("Invalid sampler index %u.\n", idx);
2339 return NULL;
2342 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2345 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2347 UINT i;
2349 for (i = 0; i < device->context_count; ++i)
2351 device->contexts[i]->constant_update_mask |= mask;
2355 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2356 UINT start_register, const BOOL *constants, UINT bool_count)
2358 UINT count = min(bool_count, MAX_CONST_B - start_register);
2359 UINT i;
2361 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2362 device, start_register, constants, bool_count);
2364 if (!constants || start_register >= MAX_CONST_B)
2365 return WINED3DERR_INVALIDCALL;
2367 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2368 for (i = 0; i < count; ++i)
2369 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2371 if (device->recording)
2373 for (i = start_register; i < count + start_register; ++i)
2374 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2376 else
2378 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2381 return WINED3D_OK;
2384 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2385 UINT start_register, BOOL *constants, UINT bool_count)
2387 UINT count = min(bool_count, MAX_CONST_B - start_register);
2389 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2390 device, start_register, constants, bool_count);
2392 if (!constants || start_register >= MAX_CONST_B)
2393 return WINED3DERR_INVALIDCALL;
2395 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2397 return WINED3D_OK;
2400 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2401 UINT start_register, const int *constants, UINT vector4i_count)
2403 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2404 UINT i;
2406 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2407 device, start_register, constants, vector4i_count);
2409 if (!constants || start_register >= MAX_CONST_I)
2410 return WINED3DERR_INVALIDCALL;
2412 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2413 for (i = 0; i < count; ++i)
2414 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2415 constants[i * 4], constants[i * 4 + 1],
2416 constants[i * 4 + 2], constants[i * 4 + 3]);
2418 if (device->recording)
2420 for (i = start_register; i < count + start_register; ++i)
2421 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2423 else
2425 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2428 return WINED3D_OK;
2431 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2432 UINT start_register, int *constants, UINT vector4i_count)
2434 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2436 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2437 device, start_register, constants, vector4i_count);
2439 if (!constants || start_register >= MAX_CONST_I)
2440 return WINED3DERR_INVALIDCALL;
2442 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2443 return WINED3D_OK;
2446 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2447 UINT start_register, const float *constants, UINT vector4f_count)
2449 UINT i;
2450 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2452 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2453 device, start_register, constants, vector4f_count);
2455 /* Specifically test start_register > limit to catch MAX_UINT overflows
2456 * when adding start_register + vector4f_count. */
2457 if (!constants
2458 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2459 || start_register > d3d_info->limits.vs_uniform_count)
2460 return WINED3DERR_INVALIDCALL;
2462 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2463 constants, vector4f_count * sizeof(float) * 4);
2464 if (TRACE_ON(d3d))
2466 for (i = 0; i < vector4f_count; ++i)
2467 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2468 constants[i * 4], constants[i * 4 + 1],
2469 constants[i * 4 + 2], constants[i * 4 + 3]);
2472 if (device->recording)
2473 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2474 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2475 else
2476 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2479 return WINED3D_OK;
2482 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2483 UINT start_register, float *constants, UINT vector4f_count)
2485 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2486 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2488 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2489 device, start_register, constants, vector4f_count);
2491 if (!constants || count < 0)
2492 return WINED3DERR_INVALIDCALL;
2494 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2496 return WINED3D_OK;
2499 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2501 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2503 TRACE("device %p, shader %p.\n", device, shader);
2505 if (device->recording)
2506 device->recording->changed.pixelShader = TRUE;
2508 if (shader == prev)
2509 return;
2511 if (shader)
2512 wined3d_shader_incref(shader);
2513 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2514 if (!device->recording)
2515 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2516 if (prev)
2517 wined3d_shader_decref(prev);
2520 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2522 TRACE("device %p.\n", device);
2524 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2527 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2529 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2531 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2534 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2536 TRACE("device %p, idx %u.\n", device, idx);
2538 if (idx >= MAX_CONSTANT_BUFFERS)
2540 WARN("Invalid constant buffer index %u.\n", idx);
2541 return NULL;
2544 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2547 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2548 UINT idx, struct wined3d_shader_resource_view *view)
2550 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2552 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2555 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2556 UINT idx)
2558 TRACE("device %p, idx %u.\n", device, idx);
2560 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2562 WARN("Invalid view index %u.\n", idx);
2563 return NULL;
2566 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2569 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2571 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2573 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2576 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2578 TRACE("device %p, idx %u.\n", device, idx);
2580 if (idx >= MAX_SAMPLER_OBJECTS)
2582 WARN("Invalid sampler index %u.\n", idx);
2583 return NULL;
2586 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2589 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2590 UINT start_register, const BOOL *constants, UINT bool_count)
2592 UINT count = min(bool_count, MAX_CONST_B - start_register);
2593 UINT i;
2595 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2596 device, start_register, constants, bool_count);
2598 if (!constants || start_register >= MAX_CONST_B)
2599 return WINED3DERR_INVALIDCALL;
2601 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2602 for (i = 0; i < count; ++i)
2603 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2605 if (device->recording)
2607 for (i = start_register; i < count + start_register; ++i)
2608 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2610 else
2612 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2615 return WINED3D_OK;
2618 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2619 UINT start_register, BOOL *constants, UINT bool_count)
2621 UINT count = min(bool_count, MAX_CONST_B - start_register);
2623 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2624 device, start_register, constants, bool_count);
2626 if (!constants || start_register >= MAX_CONST_B)
2627 return WINED3DERR_INVALIDCALL;
2629 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2631 return WINED3D_OK;
2634 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2635 UINT start_register, const int *constants, UINT vector4i_count)
2637 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2638 UINT i;
2640 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2641 device, start_register, constants, vector4i_count);
2643 if (!constants || start_register >= MAX_CONST_I)
2644 return WINED3DERR_INVALIDCALL;
2646 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2647 for (i = 0; i < count; ++i)
2648 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2649 constants[i * 4], constants[i * 4 + 1],
2650 constants[i * 4 + 2], constants[i * 4 + 3]);
2652 if (device->recording)
2654 for (i = start_register; i < count + start_register; ++i)
2655 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2657 else
2659 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2662 return WINED3D_OK;
2665 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2666 UINT start_register, int *constants, UINT vector4i_count)
2668 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2670 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2671 device, start_register, constants, vector4i_count);
2673 if (!constants || start_register >= MAX_CONST_I)
2674 return WINED3DERR_INVALIDCALL;
2676 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2678 return WINED3D_OK;
2681 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2682 UINT start_register, const float *constants, UINT vector4f_count)
2684 UINT i;
2685 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2687 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2688 device, start_register, constants, vector4f_count);
2690 /* Specifically test start_register > limit to catch MAX_UINT overflows
2691 * when adding start_register + vector4f_count. */
2692 if (!constants
2693 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2694 || start_register > d3d_info->limits.ps_uniform_count)
2695 return WINED3DERR_INVALIDCALL;
2697 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2698 constants, vector4f_count * sizeof(float) * 4);
2699 if (TRACE_ON(d3d))
2701 for (i = 0; i < vector4f_count; ++i)
2702 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2703 constants[i * 4], constants[i * 4 + 1],
2704 constants[i * 4 + 2], constants[i * 4 + 3]);
2707 if (device->recording)
2708 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2709 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2710 else
2711 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2713 return WINED3D_OK;
2716 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2717 UINT start_register, float *constants, UINT vector4f_count)
2719 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2720 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2722 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2723 device, start_register, constants, vector4f_count);
2725 if (!constants || count < 0)
2726 return WINED3DERR_INVALIDCALL;
2728 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2730 return WINED3D_OK;
2733 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2735 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2737 TRACE("device %p, shader %p.\n", device, shader);
2739 if (device->recording || shader == prev)
2740 return;
2741 if (shader)
2742 wined3d_shader_incref(shader);
2743 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2744 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2745 if (prev)
2746 wined3d_shader_decref(prev);
2749 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2751 TRACE("device %p.\n", device);
2753 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2756 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2758 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2760 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2763 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2765 TRACE("device %p, idx %u.\n", device, idx);
2767 if (idx >= MAX_CONSTANT_BUFFERS)
2769 WARN("Invalid constant buffer index %u.\n", idx);
2770 return NULL;
2773 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2776 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2777 UINT idx, struct wined3d_shader_resource_view *view)
2779 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2781 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2784 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2785 UINT idx)
2787 TRACE("device %p, idx %u.\n", device, idx);
2789 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2791 WARN("Invalid view index %u.\n", idx);
2792 return NULL;
2795 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2798 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2800 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2802 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2805 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2807 TRACE("device %p, idx %u.\n", device, idx);
2809 if (idx >= MAX_SAMPLER_OBJECTS)
2811 WARN("Invalid sampler index %u.\n", idx);
2812 return NULL;
2815 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2818 /* Context activation is done by the caller. */
2819 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2820 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2821 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2822 DWORD DestFVF)
2824 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2825 struct wined3d_viewport vp;
2826 UINT vertex_size;
2827 unsigned int i;
2828 BYTE *dest_ptr;
2829 BOOL doClip;
2830 DWORD numTextures;
2831 HRESULT hr;
2833 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2835 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2838 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2840 ERR("Source has no position mask\n");
2841 return WINED3DERR_INVALIDCALL;
2844 if (device->state.render_states[WINED3D_RS_CLIPPING])
2846 static BOOL warned = FALSE;
2848 * The clipping code is not quite correct. Some things need
2849 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2850 * so disable clipping for now.
2851 * (The graphics in Half-Life are broken, and my processvertices
2852 * test crashes with IDirect3DDevice3)
2853 doClip = TRUE;
2855 doClip = FALSE;
2856 if(!warned) {
2857 warned = TRUE;
2858 FIXME("Clipping is broken and disabled for now\n");
2861 else
2862 doClip = FALSE;
2864 vertex_size = get_flexible_vertex_size(DestFVF);
2865 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2867 WARN("Failed to map buffer, hr %#x.\n", hr);
2868 return hr;
2871 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2872 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2873 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2875 TRACE("View mat:\n");
2876 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2877 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2878 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2879 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2881 TRACE("Proj mat:\n");
2882 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2883 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2884 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2885 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2887 TRACE("World mat:\n");
2888 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2889 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2890 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2891 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2893 /* Get the viewport */
2894 wined3d_device_get_viewport(device, &vp);
2895 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2896 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2898 multiply_matrix(&mat,&view_mat,&world_mat);
2899 multiply_matrix(&mat,&proj_mat,&mat);
2901 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2903 for (i = 0; i < dwCount; i+= 1) {
2904 unsigned int tex_index;
2906 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2907 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2908 /* The position first */
2909 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2910 const float *p = (const float *)(element->data.addr + i * element->stride);
2911 float x, y, z, rhw;
2912 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2914 /* Multiplication with world, view and projection matrix. */
2915 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2916 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2917 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2918 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2920 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2922 /* WARNING: The following things are taken from d3d7 and were not yet checked
2923 * against d3d8 or d3d9!
2926 /* Clipping conditions: From msdn
2928 * A vertex is clipped if it does not match the following requirements
2929 * -rhw < x <= rhw
2930 * -rhw < y <= rhw
2931 * 0 < z <= rhw
2932 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2934 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2935 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2939 if( !doClip ||
2940 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2941 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2942 ( rhw > eps ) ) ) {
2944 /* "Normal" viewport transformation (not clipped)
2945 * 1) The values are divided by rhw
2946 * 2) The y axis is negative, so multiply it with -1
2947 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2948 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2949 * 4) Multiply x with Width/2 and add Width/2
2950 * 5) The same for the height
2951 * 6) Add the viewpoint X and Y to the 2D coordinates and
2952 * The minimum Z value to z
2953 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2955 * Well, basically it's simply a linear transformation into viewport
2956 * coordinates
2959 x /= rhw;
2960 y /= rhw;
2961 z /= rhw;
2963 y *= -1;
2965 x *= vp.width / 2;
2966 y *= vp.height / 2;
2967 z *= vp.max_z - vp.min_z;
2969 x += vp.width / 2 + vp.x;
2970 y += vp.height / 2 + vp.y;
2971 z += vp.min_z;
2973 rhw = 1 / rhw;
2974 } else {
2975 /* That vertex got clipped
2976 * Contrary to OpenGL it is not dropped completely, it just
2977 * undergoes a different calculation.
2979 TRACE("Vertex got clipped\n");
2980 x += rhw;
2981 y += rhw;
2983 x /= 2;
2984 y /= 2;
2986 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2987 * outside of the main vertex buffer memory. That needs some more
2988 * investigation...
2992 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2995 ( (float *) dest_ptr)[0] = x;
2996 ( (float *) dest_ptr)[1] = y;
2997 ( (float *) dest_ptr)[2] = z;
2998 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3000 dest_ptr += 3 * sizeof(float);
3002 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3003 dest_ptr += sizeof(float);
3006 if (DestFVF & WINED3DFVF_PSIZE)
3007 dest_ptr += sizeof(DWORD);
3009 if (DestFVF & WINED3DFVF_NORMAL)
3011 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3012 const float *normal = (const float *)(element->data.addr + i * element->stride);
3013 /* AFAIK this should go into the lighting information */
3014 FIXME("Didn't expect the destination to have a normal\n");
3015 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3018 if (DestFVF & WINED3DFVF_DIFFUSE)
3020 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3021 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3022 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3024 static BOOL warned = FALSE;
3026 if(!warned) {
3027 ERR("No diffuse color in source, but destination has one\n");
3028 warned = TRUE;
3031 *( (DWORD *) dest_ptr) = 0xffffffff;
3032 dest_ptr += sizeof(DWORD);
3034 else
3036 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3040 if (DestFVF & WINED3DFVF_SPECULAR)
3042 /* What's the color value in the feedback buffer? */
3043 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3044 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3045 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3047 static BOOL warned = FALSE;
3049 if(!warned) {
3050 ERR("No specular color in source, but destination has one\n");
3051 warned = TRUE;
3054 *(DWORD *)dest_ptr = 0xff000000;
3055 dest_ptr += sizeof(DWORD);
3057 else
3059 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3063 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3065 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3066 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3067 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3069 ERR("No source texture, but destination requests one\n");
3070 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3072 else
3074 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3079 wined3d_buffer_unmap(dest);
3081 return WINED3D_OK;
3083 #undef copy_and_next
3085 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3086 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3087 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3089 struct wined3d_state *state = &device->state;
3090 struct wined3d_stream_info stream_info;
3091 const struct wined3d_gl_info *gl_info;
3092 struct wined3d_context *context;
3093 struct wined3d_shader *vs;
3094 unsigned int i;
3095 HRESULT hr;
3096 WORD map;
3098 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3099 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3100 device, src_start_idx, dst_idx, vertex_count,
3101 dst_buffer, declaration, flags, dst_fvf);
3103 if (declaration)
3104 FIXME("Output vertex declaration not implemented yet.\n");
3106 /* Need any context to write to the vbo. */
3107 context = context_acquire(device, NULL);
3108 gl_info = context->gl_info;
3110 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3111 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3112 context_stream_info_from_declaration(context, state, &stream_info);
3113 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3115 /* We can't convert FROM a VBO, and vertex buffers used to source into
3116 * process_vertices() are unlikely to ever be used for drawing. Release
3117 * VBOs in those buffers and fix up the stream_info structure.
3119 * Also apply the start index. */
3120 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3122 struct wined3d_stream_info_element *e;
3123 struct wined3d_buffer *buffer;
3125 if (!(map & 1))
3126 continue;
3128 e = &stream_info.elements[i];
3129 buffer = state->streams[e->stream_idx].buffer;
3130 e->data.buffer_object = 0;
3131 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3132 if (buffer->buffer_object)
3134 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3135 buffer->buffer_object = 0;
3137 if (e->data.addr)
3138 e->data.addr += e->stride * src_start_idx;
3141 hr = process_vertices_strided(device, dst_idx, vertex_count,
3142 &stream_info, dst_buffer, flags, dst_fvf);
3144 context_release(context);
3146 return hr;
3149 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3150 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3152 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3153 DWORD old_value;
3155 TRACE("device %p, stage %u, state %s, value %#x.\n",
3156 device, stage, debug_d3dtexturestate(state), value);
3158 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3160 WARN("Invalid state %#x passed.\n", state);
3161 return;
3164 if (stage >= d3d_info->limits.ffp_blend_stages)
3166 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3167 stage, d3d_info->limits.ffp_blend_stages - 1);
3168 return;
3171 old_value = device->update_state->texture_states[stage][state];
3172 device->update_state->texture_states[stage][state] = value;
3174 if (device->recording)
3176 TRACE("Recording... not performing anything.\n");
3177 device->recording->changed.textureState[stage] |= 1u << state;
3178 return;
3181 /* Checked after the assignments to allow proper stateblock recording. */
3182 if (old_value == value)
3184 TRACE("Application is setting the old value over, nothing to do.\n");
3185 return;
3188 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3191 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3192 UINT stage, enum wined3d_texture_stage_state state)
3194 TRACE("device %p, stage %u, state %s.\n",
3195 device, stage, debug_d3dtexturestate(state));
3197 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3199 WARN("Invalid state %#x passed.\n", state);
3200 return 0;
3203 return device->state.texture_states[stage][state];
3206 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3207 UINT stage, struct wined3d_texture *texture)
3209 struct wined3d_texture *prev;
3211 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3213 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3214 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3216 /* Windows accepts overflowing this array... we do not. */
3217 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3219 WARN("Ignoring invalid stage %u.\n", stage);
3220 return WINED3D_OK;
3223 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3225 WARN("Rejecting attempt to set scratch texture.\n");
3226 return WINED3DERR_INVALIDCALL;
3229 if (device->recording)
3230 device->recording->changed.textures |= 1u << stage;
3232 prev = device->update_state->textures[stage];
3233 TRACE("Previous texture %p.\n", prev);
3235 if (texture == prev)
3237 TRACE("App is setting the same texture again, nothing to do.\n");
3238 return WINED3D_OK;
3241 TRACE("Setting new texture to %p.\n", texture);
3242 device->update_state->textures[stage] = texture;
3244 if (texture)
3245 wined3d_texture_incref(texture);
3246 if (!device->recording)
3247 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3248 if (prev)
3249 wined3d_texture_decref(prev);
3251 return WINED3D_OK;
3254 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3256 TRACE("device %p, stage %u.\n", device, stage);
3258 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3259 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3261 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3263 WARN("Ignoring invalid stage %u.\n", stage);
3264 return NULL; /* Windows accepts overflowing this array ... we do not. */
3267 return device->state.textures[stage];
3270 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3272 TRACE("device %p, caps %p.\n", device, caps);
3274 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3275 device->create_parms.device_type, caps);
3278 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3279 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3281 struct wined3d_swapchain *swapchain;
3283 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3284 device, swapchain_idx, mode, rotation);
3286 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3287 return WINED3DERR_INVALIDCALL;
3289 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3292 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3294 struct wined3d_stateblock *stateblock;
3295 HRESULT hr;
3297 TRACE("device %p.\n", device);
3299 if (device->recording)
3300 return WINED3DERR_INVALIDCALL;
3302 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3303 if (FAILED(hr))
3304 return hr;
3306 device->recording = stateblock;
3307 device->update_state = &stateblock->state;
3309 TRACE("Recording stateblock %p.\n", stateblock);
3311 return WINED3D_OK;
3314 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3315 struct wined3d_stateblock **stateblock)
3317 struct wined3d_stateblock *object = device->recording;
3319 TRACE("device %p, stateblock %p.\n", device, stateblock);
3321 if (!device->recording)
3323 WARN("Not recording.\n");
3324 *stateblock = NULL;
3325 return WINED3DERR_INVALIDCALL;
3328 stateblock_init_contained_states(object);
3330 *stateblock = object;
3331 device->recording = NULL;
3332 device->update_state = &device->state;
3334 TRACE("Returning stateblock %p.\n", *stateblock);
3336 return WINED3D_OK;
3339 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3341 /* At the moment we have no need for any functionality at the beginning
3342 * of a scene. */
3343 TRACE("device %p.\n", device);
3345 if (device->inScene)
3347 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3348 return WINED3DERR_INVALIDCALL;
3350 device->inScene = TRUE;
3351 return WINED3D_OK;
3354 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3356 struct wined3d_context *context;
3358 TRACE("device %p.\n", device);
3360 if (!device->inScene)
3362 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3363 return WINED3DERR_INVALIDCALL;
3366 context = context_acquire(device, NULL);
3367 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3368 context->gl_info->gl_ops.gl.p_glFlush();
3369 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3370 * fails. */
3371 context_release(context);
3373 device->inScene = FALSE;
3374 return WINED3D_OK;
3377 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3378 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3380 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3381 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3383 if (!rect_count && rects)
3385 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3386 return WINED3D_OK;
3389 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3391 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3392 if (!ds)
3394 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3395 /* TODO: What about depth stencil buffers without stencil bits? */
3396 return WINED3DERR_INVALIDCALL;
3398 else if (flags & WINED3DCLEAR_TARGET)
3400 if (ds->width < device->fb.render_targets[0]->width
3401 || ds->height < device->fb.render_targets[0]->height)
3403 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3404 return WINED3D_OK;
3409 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3411 return WINED3D_OK;
3414 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3415 struct wined3d_query *predicate, BOOL value)
3417 struct wined3d_query *prev;
3419 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3421 prev = device->update_state->predicate;
3422 if (predicate)
3424 FIXME("Predicated rendering not implemented.\n");
3425 wined3d_query_incref(predicate);
3427 device->update_state->predicate = predicate;
3428 device->update_state->predicate_value = value;
3429 if (!device->recording)
3430 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3431 if (prev)
3432 wined3d_query_decref(prev);
3435 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3437 TRACE("device %p, value %p.\n", device, value);
3439 *value = device->state.predicate_value;
3440 return device->state.predicate;
3443 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3444 enum wined3d_primitive_type primitive_type)
3446 GLenum gl_primitive_type, prev;
3448 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3450 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3451 prev = device->update_state->gl_primitive_type;
3452 device->update_state->gl_primitive_type = gl_primitive_type;
3453 if (device->recording)
3454 device->recording->changed.primitive_type = TRUE;
3455 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3456 device_invalidate_state(device, STATE_POINT_ENABLE);
3459 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3460 enum wined3d_primitive_type *primitive_type)
3462 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3464 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3466 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3469 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3471 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3473 if (!device->state.vertex_declaration)
3475 WARN("Called without a valid vertex declaration set.\n");
3476 return WINED3DERR_INVALIDCALL;
3479 if (device->state.load_base_vertex_index)
3481 device->state.load_base_vertex_index = 0;
3482 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3485 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3487 return WINED3D_OK;
3490 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3491 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3493 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3494 device, start_vertex, vertex_count, start_instance, instance_count);
3496 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3499 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3501 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3503 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3505 if (!device->state.index_buffer)
3507 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3508 * without an index buffer set. (The first time at least...)
3509 * D3D8 simply dies, but I doubt it can do much harm to return
3510 * D3DERR_INVALIDCALL there as well. */
3511 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3512 return WINED3DERR_INVALIDCALL;
3515 if (!device->state.vertex_declaration)
3517 WARN("Called without a valid vertex declaration set.\n");
3518 return WINED3DERR_INVALIDCALL;
3521 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3522 device->state.load_base_vertex_index != device->state.base_vertex_index)
3524 device->state.load_base_vertex_index = device->state.base_vertex_index;
3525 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3528 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3530 return WINED3D_OK;
3533 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3534 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3536 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3537 device, start_idx, index_count, start_instance, instance_count);
3539 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3542 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3543 struct wined3d_texture *src_texture, unsigned int src_level,
3544 struct wined3d_texture *dst_texture, unsigned int level_count)
3546 struct wined3d_const_bo_address data;
3547 struct wined3d_context *context;
3548 struct wined3d_map_desc src;
3549 HRESULT hr = WINED3D_OK;
3550 unsigned int i;
3552 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3553 device, src_texture, src_level, dst_texture, level_count);
3555 if (src_texture->resource.format != dst_texture->resource.format)
3557 WARN("Source and destination formats do not match.\n");
3558 return WINED3DERR_INVALIDCALL;
3561 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3562 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3563 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3565 WARN("Source and destination dimensions do not match.\n");
3566 return WINED3DERR_INVALIDCALL;
3569 context = context_acquire(device, NULL);
3571 /* Only a prepare, since we're uploading entire volumes. */
3572 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3573 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3575 for (i = 0; i < level_count; ++i)
3577 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3578 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3579 goto done;
3581 data.buffer_object = 0;
3582 data.addr = src.data;
3583 wined3d_volume_upload_data(dst_texture->sub_resources[i].u.volume, context, &data);
3584 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3586 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3587 goto done;
3590 done:
3591 context_release(context);
3592 return hr;
3595 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3596 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3598 unsigned int src_size, dst_size, src_skip_levels = 0;
3599 unsigned int layer_count, level_count, i, j;
3600 enum wined3d_resource_type type;
3601 HRESULT hr;
3602 struct wined3d_context *context;
3604 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3606 /* Verify that the source and destination textures are non-NULL. */
3607 if (!src_texture || !dst_texture)
3609 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3610 return WINED3DERR_INVALIDCALL;
3613 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3615 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3616 return WINED3DERR_INVALIDCALL;
3618 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3620 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3621 return WINED3DERR_INVALIDCALL;
3624 /* Verify that the source and destination textures are the same type. */
3625 type = src_texture->resource.type;
3626 if (dst_texture->resource.type != type)
3628 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3629 return WINED3DERR_INVALIDCALL;
3632 layer_count = src_texture->layer_count;
3633 if (layer_count != dst_texture->layer_count)
3635 WARN("Source and destination have different layer counts.\n");
3636 return WINED3DERR_INVALIDCALL;
3639 level_count = min(wined3d_texture_get_level_count(src_texture),
3640 wined3d_texture_get_level_count(dst_texture));
3642 src_size = max(src_texture->resource.width, src_texture->resource.height);
3643 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3644 if (type == WINED3D_RTYPE_TEXTURE_3D)
3646 src_size = max(src_size, src_texture->resource.depth);
3647 dst_size = max(dst_size, dst_texture->resource.depth);
3649 while (src_size > dst_size)
3651 src_size >>= 1;
3652 ++src_skip_levels;
3655 /* Make sure that the destination texture is loaded. */
3656 context = context_acquire(device, NULL);
3657 wined3d_texture_load(dst_texture, context, FALSE);
3658 context_release(context);
3660 /* Update every surface level of the texture. */
3661 switch (type)
3663 case WINED3D_RTYPE_TEXTURE_2D:
3665 unsigned int src_levels = src_texture->level_count;
3666 unsigned int dst_levels = dst_texture->level_count;
3667 struct wined3d_surface *src_surface;
3668 struct wined3d_surface *dst_surface;
3670 for (i = 0; i < layer_count; ++i)
3672 for (j = 0; j < level_count; ++j)
3674 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3675 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3676 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3678 WARN("Failed to update surface, hr %#x.\n", hr);
3679 return hr;
3683 return WINED3D_OK;
3686 case WINED3D_RTYPE_TEXTURE_3D:
3687 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3688 src_texture, src_skip_levels, dst_texture, level_count)))
3689 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3690 return hr;
3692 default:
3693 FIXME("Unsupported texture type %#x.\n", type);
3694 return WINED3DERR_INVALIDCALL;
3698 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3700 const struct wined3d_state *state = &device->state;
3701 struct wined3d_texture *texture;
3702 DWORD i;
3704 TRACE("device %p, num_passes %p.\n", device, num_passes);
3706 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3708 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3710 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3711 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3713 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3715 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3716 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3719 texture = state->textures[i];
3720 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3722 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3724 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3725 return E_FAIL;
3727 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3729 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3730 return E_FAIL;
3732 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3733 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3735 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3736 return E_FAIL;
3740 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3741 || state->render_states[WINED3D_RS_STENCILENABLE])
3743 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3744 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3746 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3748 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3749 return WINED3DERR_CONFLICTINGRENDERSTATE;
3753 /* return a sensible default */
3754 *num_passes = 1;
3756 TRACE("returning D3D_OK\n");
3757 return WINED3D_OK;
3760 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3762 static BOOL warned;
3764 TRACE("device %p, software %#x.\n", device, software);
3766 if (!warned)
3768 FIXME("device %p, software %#x stub!\n", device, software);
3769 warned = TRUE;
3772 device->softwareVertexProcessing = software;
3775 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3777 static BOOL warned;
3779 TRACE("device %p.\n", device);
3781 if (!warned)
3783 TRACE("device %p stub!\n", device);
3784 warned = TRUE;
3787 return device->softwareVertexProcessing;
3790 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3791 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3793 struct wined3d_swapchain *swapchain;
3795 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3796 device, swapchain_idx, raster_status);
3798 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3799 return WINED3DERR_INVALIDCALL;
3801 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3804 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3806 static BOOL warned;
3808 TRACE("device %p, segments %.8e.\n", device, segments);
3810 if (segments != 0.0f)
3812 if (!warned)
3814 FIXME("device %p, segments %.8e stub!\n", device, segments);
3815 warned = TRUE;
3819 return WINED3D_OK;
3822 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3824 static BOOL warned;
3826 TRACE("device %p.\n", device);
3828 if (!warned)
3830 FIXME("device %p stub!\n", device);
3831 warned = TRUE;
3834 return 0.0f;
3837 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3838 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3840 struct wined3d_texture *dst_texture, *src_texture;
3841 RECT dst_rect, src_rect;
3842 unsigned int i, j;
3843 HRESULT hr;
3845 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3847 if (src_resource == dst_resource)
3849 WARN("Source and destination are the same resource.\n");
3850 return;
3853 if (src_resource->type != dst_resource->type)
3855 WARN("Resource types (%s / %s) don't match.\n",
3856 debug_d3dresourcetype(dst_resource->type),
3857 debug_d3dresourcetype(src_resource->type));
3858 return;
3861 if (src_resource->width != dst_resource->width
3862 || src_resource->height != dst_resource->height
3863 || src_resource->depth != dst_resource->depth)
3865 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3866 dst_resource->width, dst_resource->height, dst_resource->depth,
3867 src_resource->width, src_resource->height, src_resource->depth);
3868 return;
3871 if (src_resource->format->id != dst_resource->format->id)
3873 WARN("Resource formats (%s / %s) don't match.\n",
3874 debug_d3dformat(dst_resource->format->id),
3875 debug_d3dformat(src_resource->format->id));
3876 return;
3879 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3881 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3882 buffer_from_resource(src_resource), 0,
3883 dst_resource->size)))
3884 ERR("Failed to copy buffer, hr %#x.\n", hr);
3885 return;
3888 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3890 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3891 return;
3894 dst_texture = wined3d_texture_from_resource(dst_resource);
3895 src_texture = wined3d_texture_from_resource(src_resource);
3897 if (src_texture->layer_count != dst_texture->layer_count
3898 || src_texture->level_count != dst_texture->level_count)
3900 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3901 dst_texture->layer_count, dst_texture->level_count,
3902 src_texture->layer_count, src_texture->level_count);
3903 return;
3906 for (i = 0; i < dst_texture->level_count; ++i)
3908 SetRect(&dst_rect, 0, 0,
3909 wined3d_texture_get_level_width(dst_texture, i),
3910 wined3d_texture_get_level_height(dst_texture, i));
3911 SetRect(&src_rect, 0, 0,
3912 wined3d_texture_get_level_width(src_texture, i),
3913 wined3d_texture_get_level_height(dst_texture, i));
3914 for (j = 0; j < dst_texture->layer_count; ++j)
3916 unsigned int idx = j * dst_texture->level_count + i;
3918 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3919 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3920 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3925 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3926 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3927 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3928 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3930 struct wined3d_texture *dst_texture, *src_texture;
3931 RECT dst_rect, src_rect;
3932 HRESULT hr;
3934 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3935 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3936 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3937 src_resource, src_sub_resource_idx, debug_box(src_box));
3939 if (src_box && (src_box->left >= src_box->right
3940 || src_box->top >= src_box->bottom
3941 || src_box->front >= src_box->back))
3943 WARN("Invalid box %s specified.\n", debug_box(src_box));
3944 return WINED3DERR_INVALIDCALL;
3947 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3949 WARN("Source and destination are the same sub-resource.\n");
3950 return WINED3DERR_INVALIDCALL;
3953 if (src_resource->type != dst_resource->type)
3955 WARN("Resource types (%s / %s) don't match.\n",
3956 debug_d3dresourcetype(dst_resource->type),
3957 debug_d3dresourcetype(src_resource->type));
3958 return WINED3DERR_INVALIDCALL;
3961 if (src_resource->format->id != dst_resource->format->id)
3963 WARN("Resource formats (%s / %s) don't match.\n",
3964 debug_d3dformat(dst_resource->format->id),
3965 debug_d3dformat(src_resource->format->id));
3966 return WINED3DERR_INVALIDCALL;
3969 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3971 unsigned int src_offset, size;
3973 if (dst_sub_resource_idx)
3975 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3976 return WINED3DERR_INVALIDCALL;
3978 if (src_sub_resource_idx)
3980 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3981 return WINED3DERR_INVALIDCALL;
3984 if (src_box)
3986 src_offset = src_box->left;
3987 size = src_box->right - src_box->left;
3989 else
3991 src_offset = 0;
3992 size = src_resource->size;
3995 if (src_offset > src_resource->size
3996 || size > src_resource->size - src_offset
3997 || dst_x > dst_resource->size
3998 || size > dst_resource->size - dst_x)
4000 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4001 dst_x, src_offset, size);
4002 return WINED3DERR_INVALIDCALL;
4005 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4006 buffer_from_resource(src_resource), src_offset, size);
4009 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4011 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4012 return WINED3DERR_INVALIDCALL;
4015 dst_texture = wined3d_texture_from_resource(dst_resource);
4016 src_texture = wined3d_texture_from_resource(src_resource);
4018 if (src_box)
4020 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4022 else
4024 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4026 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4027 wined3d_texture_get_level_height(src_texture, level));
4030 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4031 dst_y + (src_rect.bottom - src_rect.top));
4033 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4034 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4035 WARN("Failed to blit, hr %#x.\n", hr);
4037 return hr;
4040 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4041 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4042 unsigned int depth_pitch)
4044 struct wined3d_texture_sub_resource *sub_resource;
4045 const struct wined3d_gl_info *gl_info;
4046 struct wined3d_const_bo_address addr;
4047 unsigned int width, height, level;
4048 struct wined3d_context *context;
4049 struct wined3d_texture *texture;
4050 struct wined3d_surface *surface;
4051 POINT dst_point;
4052 RECT src_rect;
4054 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4055 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4057 if (resource->type == WINED3D_RTYPE_BUFFER)
4059 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4060 HRESULT hr;
4062 if (sub_resource_idx > 0)
4064 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4065 return;
4068 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4069 WARN("Failed to update buffer data, hr %#x.\n", hr);
4071 return;
4074 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4076 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4077 return;
4080 texture = wined3d_texture_from_resource(resource);
4081 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4083 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4084 return;
4086 surface = sub_resource->u.surface;
4088 level = sub_resource_idx % texture->level_count;
4089 width = wined3d_texture_get_level_width(texture, level);
4090 height = wined3d_texture_get_level_height(texture, level);
4092 src_rect.left = 0;
4093 src_rect.top = 0;
4094 if (box)
4096 if (box->left >= box->right || box->right > width
4097 || box->top >= box->bottom || box->bottom > height
4098 || box->front >= box->back)
4100 WARN("Invalid box %s specified.\n", debug_box(box));
4101 return;
4104 src_rect.right = box->right - box->left;
4105 src_rect.bottom = box->bottom - box->top;
4106 dst_point.x = box->left;
4107 dst_point.y = box->top;
4109 else
4111 src_rect.right = width;
4112 src_rect.bottom = height;
4113 dst_point.x = 0;
4114 dst_point.y = 0;
4117 addr.buffer_object = 0;
4118 addr.addr = data;
4120 context = context_acquire(resource->device, NULL);
4121 gl_info = context->gl_info;
4123 /* Only load the surface for partial updates. */
4124 if (!dst_point.x && !dst_point.y && src_rect.right == width && src_rect.bottom == height)
4125 wined3d_texture_prepare_texture(texture, context, FALSE);
4126 else
4127 surface_load_location(surface, context, WINED3D_LOCATION_TEXTURE_RGB);
4128 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4130 wined3d_surface_upload_data(surface, gl_info, resource->format,
4131 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4133 context_release(context);
4135 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4136 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4139 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4140 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4141 const struct wined3d_color *color, float depth, DWORD stencil)
4143 const struct blit_shader *blitter;
4144 struct wined3d_resource *resource;
4145 enum wined3d_blit_op blit_op;
4146 RECT r;
4148 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4149 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4151 if (!flags)
4152 return WINED3D_OK;
4154 resource = view->resource;
4155 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4157 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4158 return WINED3DERR_INVALIDCALL;
4161 if (view->depth > 1)
4163 FIXME("Layered clears not implemented.\n");
4164 return WINED3DERR_INVALIDCALL;
4167 if (!rect)
4169 SetRect(&r, 0, 0, view->width, view->height);
4170 rect = &r;
4173 if (flags & WINED3DCLEAR_TARGET)
4174 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4175 else
4176 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4178 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4179 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4181 FIXME("No blitter is capable of performing the requested fill operation.\n");
4182 return WINED3DERR_INVALIDCALL;
4185 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4186 return blitter->color_fill(device, view, rect, color);
4187 else
4188 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4191 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4192 unsigned int view_idx)
4194 TRACE("device %p, view_idx %u.\n", device, view_idx);
4196 if (view_idx >= device->adapter->gl_info.limits.buffers)
4198 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4199 return NULL;
4202 return device->fb.render_targets[view_idx];
4205 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4207 TRACE("device %p.\n", device);
4209 return device->fb.depth_stencil;
4212 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4213 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4215 struct wined3d_rendertarget_view *prev;
4217 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4218 device, view_idx, view, set_viewport);
4220 if (view_idx >= device->adapter->gl_info.limits.buffers)
4222 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4223 return WINED3DERR_INVALIDCALL;
4226 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4228 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4229 return WINED3DERR_INVALIDCALL;
4232 /* Set the viewport and scissor rectangles, if requested. Tests show that
4233 * stateblock recording is ignored, the change goes directly into the
4234 * primary stateblock. */
4235 if (!view_idx && set_viewport)
4237 struct wined3d_state *state = &device->state;
4239 state->viewport.x = 0;
4240 state->viewport.y = 0;
4241 state->viewport.width = view->width;
4242 state->viewport.height = view->height;
4243 state->viewport.min_z = 0.0f;
4244 state->viewport.max_z = 1.0f;
4245 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4247 state->scissor_rect.top = 0;
4248 state->scissor_rect.left = 0;
4249 state->scissor_rect.right = view->width;
4250 state->scissor_rect.bottom = view->height;
4251 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4255 prev = device->fb.render_targets[view_idx];
4256 if (view == prev)
4257 return WINED3D_OK;
4259 if (view)
4260 wined3d_rendertarget_view_incref(view);
4261 device->fb.render_targets[view_idx] = view;
4262 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4263 /* Release after the assignment, to prevent device_resource_released()
4264 * from seeing the surface as still in use. */
4265 if (prev)
4266 wined3d_rendertarget_view_decref(prev);
4268 return WINED3D_OK;
4271 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4273 struct wined3d_rendertarget_view *prev;
4275 TRACE("device %p, view %p.\n", device, view);
4277 prev = device->fb.depth_stencil;
4278 if (prev == view)
4280 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4281 return;
4284 if ((device->fb.depth_stencil = view))
4285 wined3d_rendertarget_view_incref(view);
4286 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4287 if (prev)
4288 wined3d_rendertarget_view_decref(prev);
4291 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4292 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4294 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4295 struct wined3d_sub_resource_data data;
4296 struct wined3d_resource_desc desc;
4297 struct wined3d_map_desc map_desc;
4298 struct wined3d_texture *texture;
4299 HRESULT hr;
4301 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4303 ERR("Failed to map source texture.\n");
4304 return NULL;
4307 data.data = map_desc.data;
4308 data.row_pitch = map_desc.row_pitch;
4309 data.slice_pitch = map_desc.slice_pitch;
4311 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4312 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4313 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4314 desc.multisample_quality = 0;
4315 desc.usage = WINED3DUSAGE_DYNAMIC;
4316 desc.pool = WINED3D_POOL_DEFAULT;
4317 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4318 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4319 desc.depth = 1;
4320 desc.size = 0;
4322 hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4323 &data, NULL, &wined3d_null_parent_ops, &texture);
4324 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4325 if (FAILED(hr))
4327 ERR("Failed to create cursor texture.\n");
4328 return NULL;
4331 return texture;
4334 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4335 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4337 unsigned int texture_level = sub_resource_idx % texture->level_count;
4338 unsigned int cursor_width, cursor_height;
4339 struct wined3d_display_mode mode;
4340 struct wined3d_map_desc map_desc;
4341 HRESULT hr;
4343 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4344 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4346 if (sub_resource_idx >= texture->level_count * texture->layer_count
4347 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4348 return WINED3DERR_INVALIDCALL;
4350 if (device->cursor_texture)
4352 wined3d_texture_decref(device->cursor_texture);
4353 device->cursor_texture = NULL;
4356 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4358 WARN("Texture %p has invalid format %s.\n",
4359 texture, debug_d3dformat(texture->resource.format->id));
4360 return WINED3DERR_INVALIDCALL;
4363 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4365 ERR("Failed to get display mode, hr %#x.\n", hr);
4366 return WINED3DERR_INVALIDCALL;
4369 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4370 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4371 if (cursor_width > mode.width || cursor_height > mode.height)
4373 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4374 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4375 return WINED3DERR_INVALIDCALL;
4378 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4380 /* Do not store the surface's pointer because the application may
4381 * release it after setting the cursor image. Windows doesn't
4382 * addref the set surface, so we can't do this either without
4383 * creating circular refcount dependencies. */
4384 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4386 ERR("Failed to create cursor texture.\n");
4387 return WINED3DERR_INVALIDCALL;
4390 if (cursor_width == 32 && cursor_height == 32)
4392 UINT mask_size = cursor_width * cursor_height / 8;
4393 ICONINFO cursor_info;
4394 DWORD *mask_bits;
4395 HCURSOR cursor;
4397 /* 32-bit user32 cursors ignore the alpha channel if it's all
4398 * zeroes, and use the mask instead. Fill the mask with all ones
4399 * to ensure we still get a fully transparent cursor. */
4400 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4401 return E_OUTOFMEMORY;
4402 memset(mask_bits, 0xff, mask_size);
4404 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4405 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4406 cursor_info.fIcon = FALSE;
4407 cursor_info.xHotspot = x_hotspot;
4408 cursor_info.yHotspot = y_hotspot;
4409 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4410 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4411 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4413 /* Create our cursor and clean up. */
4414 cursor = CreateIconIndirect(&cursor_info);
4415 if (cursor_info.hbmMask)
4416 DeleteObject(cursor_info.hbmMask);
4417 if (cursor_info.hbmColor)
4418 DeleteObject(cursor_info.hbmColor);
4419 if (device->hardwareCursor)
4420 DestroyCursor(device->hardwareCursor);
4421 device->hardwareCursor = cursor;
4422 if (device->bCursorVisible)
4423 SetCursor(cursor);
4425 HeapFree(GetProcessHeap(), 0, mask_bits);
4428 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4429 device->cursorWidth = cursor_width;
4430 device->cursorHeight = cursor_height;
4431 device->xHotSpot = x_hotspot;
4432 device->yHotSpot = y_hotspot;
4434 return WINED3D_OK;
4437 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4438 int x_screen_space, int y_screen_space, DWORD flags)
4440 TRACE("device %p, x %d, y %d, flags %#x.\n",
4441 device, x_screen_space, y_screen_space, flags);
4443 device->xScreenSpace = x_screen_space;
4444 device->yScreenSpace = y_screen_space;
4446 if (device->hardwareCursor)
4448 POINT pt;
4450 GetCursorPos( &pt );
4451 if (x_screen_space == pt.x && y_screen_space == pt.y)
4452 return;
4453 SetCursorPos( x_screen_space, y_screen_space );
4455 /* Switch to the software cursor if position diverges from the hardware one. */
4456 GetCursorPos( &pt );
4457 if (x_screen_space != pt.x || y_screen_space != pt.y)
4459 if (device->bCursorVisible) SetCursor( NULL );
4460 DestroyCursor( device->hardwareCursor );
4461 device->hardwareCursor = 0;
4466 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4468 BOOL oldVisible = device->bCursorVisible;
4470 TRACE("device %p, show %#x.\n", device, show);
4473 * When ShowCursor is first called it should make the cursor appear at the OS's last
4474 * known cursor position.
4476 if (show && !oldVisible)
4478 POINT pt;
4479 GetCursorPos(&pt);
4480 device->xScreenSpace = pt.x;
4481 device->yScreenSpace = pt.y;
4484 if (device->hardwareCursor)
4486 device->bCursorVisible = show;
4487 if (show)
4488 SetCursor(device->hardwareCursor);
4489 else
4490 SetCursor(NULL);
4492 else if (device->cursor_texture)
4494 device->bCursorVisible = show;
4497 return oldVisible;
4500 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4502 struct wined3d_resource *resource, *cursor;
4504 TRACE("device %p.\n", device);
4506 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4508 TRACE("Checking resource %p for eviction.\n", resource);
4510 /* These are handled by the texture they're part of. */
4511 if (resource->type == WINED3D_RTYPE_SURFACE || resource->type == WINED3D_RTYPE_VOLUME)
4512 continue;
4514 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4516 TRACE("Evicting %p.\n", resource);
4517 resource->resource_ops->resource_unload(resource);
4521 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4522 device_invalidate_state(device, STATE_STREAMSRC);
4525 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4527 struct wined3d_resource *resource, *cursor;
4528 const struct wined3d_gl_info *gl_info;
4529 struct wined3d_context *context;
4530 struct wined3d_shader *shader;
4532 context = context_acquire(device, NULL);
4533 gl_info = context->gl_info;
4535 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4537 if (resource->type == WINED3D_RTYPE_SURFACE || resource->type == WINED3D_RTYPE_VOLUME)
4538 continue;
4540 TRACE("Unloading resource %p.\n", resource);
4541 resource->resource_ops->resource_unload(resource);
4544 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4546 device->shader_backend->shader_destroy(shader);
4549 if (device->depth_blt_texture)
4551 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4552 device->depth_blt_texture = 0;
4555 device->blitter->free_private(device);
4556 device->shader_backend->shader_free_private(device);
4557 destroy_dummy_textures(device, gl_info);
4558 destroy_default_sampler(device);
4560 context_release(context);
4562 while (device->context_count)
4564 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4567 HeapFree(GetProcessHeap(), 0, swapchain->context);
4568 swapchain->context = NULL;
4571 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4573 struct wined3d_context *context;
4574 struct wined3d_texture *target;
4575 HRESULT hr;
4577 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4578 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4580 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4581 return hr;
4584 if (FAILED(hr = device->blitter->alloc_private(device)))
4586 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4587 device->shader_backend->shader_free_private(device);
4588 return hr;
4591 /* Recreate the primary swapchain's context */
4592 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4593 if (!swapchain->context)
4595 ERR("Failed to allocate memory for swapchain context array.\n");
4596 device->blitter->free_private(device);
4597 device->shader_backend->shader_free_private(device);
4598 return E_OUTOFMEMORY;
4601 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4602 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4604 WARN("Failed to create context.\n");
4605 device->blitter->free_private(device);
4606 device->shader_backend->shader_free_private(device);
4607 HeapFree(GetProcessHeap(), 0, swapchain->context);
4608 return E_FAIL;
4611 swapchain->context[0] = context;
4612 swapchain->num_contexts = 1;
4613 create_dummy_textures(device, context);
4614 create_default_sampler(device);
4615 context_release(context);
4617 return WINED3D_OK;
4620 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4621 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4622 wined3d_device_reset_cb callback, BOOL reset_state)
4624 struct wined3d_rendertarget_view_desc view_desc;
4625 struct wined3d_resource *resource, *cursor;
4626 struct wined3d_swapchain *swapchain;
4627 struct wined3d_display_mode m;
4628 BOOL DisplayModeChanged;
4629 HRESULT hr = WINED3D_OK;
4630 unsigned int i;
4632 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4633 device, swapchain_desc, mode, callback, reset_state);
4635 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4637 ERR("Failed to get the first implicit swapchain.\n");
4638 return WINED3DERR_INVALIDCALL;
4640 DisplayModeChanged = swapchain->reapply_mode;
4642 if (reset_state)
4644 if (device->logo_texture)
4646 wined3d_texture_decref(device->logo_texture);
4647 device->logo_texture = NULL;
4649 if (device->cursor_texture)
4651 wined3d_texture_decref(device->cursor_texture);
4652 device->cursor_texture = NULL;
4654 state_unbind_resources(&device->state);
4657 if (device->fb.render_targets)
4659 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4661 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4664 wined3d_device_set_depth_stencil_view(device, NULL);
4666 if (device->onscreen_depth_stencil)
4668 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4669 device->onscreen_depth_stencil = NULL;
4672 if (reset_state)
4674 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4676 TRACE("Enumerating resource %p.\n", resource);
4677 if (FAILED(hr = callback(resource)))
4678 return hr;
4682 TRACE("New params:\n");
4683 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4684 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4685 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4686 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4687 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4688 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4689 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4690 TRACE("device_window %p\n", swapchain_desc->device_window);
4691 TRACE("windowed %#x\n", swapchain_desc->windowed);
4692 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4693 if (swapchain_desc->enable_auto_depth_stencil)
4694 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4695 TRACE("flags %#x\n", swapchain_desc->flags);
4696 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4697 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4698 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4700 /* No special treatment of these parameters. Just store them */
4701 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4702 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4703 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4704 swapchain->desc.flags = swapchain_desc->flags;
4705 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4706 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4707 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4709 if (swapchain_desc->device_window
4710 && swapchain_desc->device_window != swapchain->desc.device_window)
4712 TRACE("Changing the device window from %p to %p.\n",
4713 swapchain->desc.device_window, swapchain_desc->device_window);
4714 swapchain->desc.device_window = swapchain_desc->device_window;
4715 swapchain->device_window = swapchain_desc->device_window;
4716 wined3d_swapchain_set_window(swapchain, NULL);
4719 if (mode)
4721 DisplayModeChanged = TRUE;
4722 m = *mode;
4724 else if (swapchain_desc->windowed)
4726 m = swapchain->original_mode;
4728 else
4730 m.width = swapchain_desc->backbuffer_width;
4731 m.height = swapchain_desc->backbuffer_height;
4732 m.refresh_rate = swapchain_desc->refresh_rate;
4733 m.format_id = swapchain_desc->backbuffer_format;
4734 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4736 if ((m.width != swapchain->desc.backbuffer_width
4737 || m.height != swapchain->desc.backbuffer_height))
4738 DisplayModeChanged = TRUE;
4741 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4742 || DisplayModeChanged)
4744 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4746 WARN("Failed to set display mode, hr %#x.\n", hr);
4747 return WINED3DERR_INVALIDCALL;
4750 if (!swapchain_desc->windowed)
4752 if (swapchain->desc.windowed)
4754 HWND focus_window = device->create_parms.focus_window;
4755 if (!focus_window)
4756 focus_window = swapchain_desc->device_window;
4757 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4759 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4760 return hr;
4763 /* switch from windowed to fs */
4764 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4765 swapchain_desc->backbuffer_width,
4766 swapchain_desc->backbuffer_height);
4768 else
4770 /* Fullscreen -> fullscreen mode change */
4771 MoveWindow(swapchain->device_window, 0, 0,
4772 swapchain_desc->backbuffer_width,
4773 swapchain_desc->backbuffer_height,
4774 TRUE);
4776 swapchain->d3d_mode = m;
4778 else if (!swapchain->desc.windowed)
4780 /* Fullscreen -> windowed switch */
4781 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4782 wined3d_device_release_focus_window(device);
4784 swapchain->desc.windowed = swapchain_desc->windowed;
4786 else if (!swapchain_desc->windowed)
4788 DWORD style = device->style;
4789 DWORD exStyle = device->exStyle;
4790 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4791 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4792 * Reset to clear up their mess. Guild Wars also loses the device during that.
4794 device->style = 0;
4795 device->exStyle = 0;
4796 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4797 swapchain_desc->backbuffer_width,
4798 swapchain_desc->backbuffer_height);
4799 device->style = style;
4800 device->exStyle = exStyle;
4803 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4804 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4805 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4806 return hr;
4808 if (device->auto_depth_stencil_view)
4810 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4811 device->auto_depth_stencil_view = NULL;
4813 if (swapchain->desc.enable_auto_depth_stencil)
4815 struct wined3d_resource_desc texture_desc;
4816 struct wined3d_texture *texture;
4818 TRACE("Creating the depth stencil buffer\n");
4820 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4821 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4822 texture_desc.multisample_type = swapchain->desc.multisample_type;
4823 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4824 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4825 texture_desc.pool = WINED3D_POOL_DEFAULT;
4826 texture_desc.width = swapchain->desc.backbuffer_width;
4827 texture_desc.height = swapchain->desc.backbuffer_height;
4828 texture_desc.depth = 1;
4829 texture_desc.size = 0;
4831 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4832 device->device_parent, &texture_desc, &texture)))
4834 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4835 return WINED3DERR_INVALIDCALL;
4838 view_desc.format_id = texture->resource.format->id;
4839 view_desc.u.texture.level_idx = 0;
4840 view_desc.u.texture.layer_idx = 0;
4841 view_desc.u.texture.layer_count = 1;
4842 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4843 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4844 wined3d_texture_decref(texture);
4845 if (FAILED(hr))
4847 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4848 return hr;
4851 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4854 if (device->back_buffer_view)
4856 wined3d_rendertarget_view_decref(device->back_buffer_view);
4857 device->back_buffer_view = NULL;
4859 if (swapchain->desc.backbuffer_count)
4861 view_desc.format_id = swapchain_desc->backbuffer_format;
4862 view_desc.u.texture.level_idx = 0;
4863 view_desc.u.texture.layer_idx = 0;
4864 view_desc.u.texture.layer_count = 1;
4865 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
4866 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4868 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4869 return hr;
4873 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4875 if (reset_state)
4877 TRACE("Resetting stateblock.\n");
4878 if (device->recording)
4880 wined3d_stateblock_decref(device->recording);
4881 device->recording = NULL;
4883 wined3d_cs_emit_reset_state(device->cs);
4884 state_cleanup(&device->state);
4886 if (device->d3d_initialized)
4887 delete_opengl_contexts(device, swapchain);
4889 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4890 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4891 ERR("Failed to initialize device state, hr %#x.\n", hr);
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 = wined3d_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 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
5146 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
5148 ERR("Failed to initialize device state, hr %#x.\n", hr);
5149 goto err;
5151 device->update_state = &device->state;
5153 if (!(device->cs = wined3d_cs_create(device)))
5155 WARN("Failed to create command stream.\n");
5156 state_cleanup(&device->state);
5157 hr = E_FAIL;
5158 goto err;
5161 return WINED3D_OK;
5163 err:
5164 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5166 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5168 wine_rb_destroy(&device->samplers, NULL, NULL);
5169 wined3d_decref(device->wined3d);
5170 return hr;
5174 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5176 DWORD rep = device->StateTable[state].representative;
5177 struct wined3d_context *context;
5178 DWORD idx;
5179 BYTE shift;
5180 UINT i;
5182 for (i = 0; i < device->context_count; ++i)
5184 context = device->contexts[i];
5185 if(isStateDirty(context, rep)) continue;
5187 context->dirtyArray[context->numDirtyEntries++] = rep;
5188 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5189 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5190 context->isStateDirty[idx] |= (1u << shift);
5194 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5195 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5197 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5199 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5200 window, message, wparam, lparam);
5201 if (unicode)
5202 return DefWindowProcW(window, message, wparam, lparam);
5203 else
5204 return DefWindowProcA(window, message, wparam, lparam);
5207 if (message == WM_DESTROY)
5209 TRACE("unregister window %p.\n", window);
5210 wined3d_unregister_window(window);
5212 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5213 ERR("Window %p is not the focus window for device %p.\n", window, device);
5215 else if (message == WM_DISPLAYCHANGE)
5217 device->device_parent->ops->mode_changed(device->device_parent);
5219 else if (message == WM_ACTIVATEAPP)
5221 UINT i;
5223 for (i = 0; i < device->swapchain_count; i++)
5224 wined3d_swapchain_activate(device->swapchains[i], wparam);
5226 device->device_parent->ops->activate(device->device_parent, wparam);
5228 else if (message == WM_SYSCOMMAND)
5230 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5232 if (unicode)
5233 DefWindowProcW(window, message, wparam, lparam);
5234 else
5235 DefWindowProcA(window, message, wparam, lparam);
5239 if (unicode)
5240 return CallWindowProcW(proc, window, message, wparam, lparam);
5241 else
5242 return CallWindowProcA(proc, window, message, wparam, lparam);