winex11: Remove support for owner-displayed clipboard formats.
[wine.git] / dlls / wined3d / device.c
blob2db1af87dcb94b44c16df62330a486f4b41488f2
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 void device_switch_onscreen_ds(struct wined3d_device *device,
202 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
204 if (device->onscreen_depth_stencil)
206 surface_load_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
208 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
209 device->onscreen_depth_stencil->ds_current_size.cx,
210 device->onscreen_depth_stencil->ds_current_size.cy);
211 wined3d_texture_decref(device->onscreen_depth_stencil->container);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_texture_incref(device->onscreen_depth_stencil->container);
217 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
219 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
220 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
222 /* partial draw rect */
223 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
224 return FALSE;
226 /* partial clear rect */
227 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
228 || clear_rect->right < width || clear_rect->bottom < height))
229 return FALSE;
231 return TRUE;
234 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
235 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
237 struct wined3d_texture_sub_resource *sub_resource = surface_get_sub_resource(ds);
238 RECT current_rect, r;
240 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
242 /* Depth buffer was discarded, make it entirely current in its new location since
243 * there is no other place where we would get data anyway. */
244 SetRect(out_rect, 0, 0,
245 wined3d_texture_get_level_width(ds->container, ds->texture_level),
246 wined3d_texture_get_level_height(ds->container, ds->texture_level));
247 return;
250 if (sub_resource->locations & location)
251 SetRect(&current_rect, 0, 0,
252 ds->ds_current_size.cx,
253 ds->ds_current_size.cy);
254 else
255 SetRectEmpty(&current_rect);
257 IntersectRect(&r, draw_rect, &current_rect);
258 if (EqualRect(&r, draw_rect))
260 /* current_rect ⊇ draw_rect, modify only. */
261 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
262 return;
265 if (EqualRect(&r, &current_rect))
267 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
269 if (!rect_count)
271 /* Full clear, modify only. */
272 *out_rect = *draw_rect;
273 return;
276 IntersectRect(&r, draw_rect, clear_rect);
277 if (EqualRect(&r, draw_rect))
279 /* clear_rect ⊇ draw_rect, modify only. */
280 *out_rect = *draw_rect;
281 return;
285 /* Full load. */
286 surface_load_location(ds, context, location);
287 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
290 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
291 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
292 float depth, DWORD stencil)
294 struct wined3d_surface *target = rt_count ? wined3d_rendertarget_view_get_surface(fb->render_targets[0]) : NULL;
295 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
296 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
297 const struct wined3d_state *state = &device->state;
298 const struct wined3d_gl_info *gl_info;
299 UINT drawable_width, drawable_height;
300 struct wined3d_color corrected_color;
301 struct wined3d_context *context;
302 GLbitfield clear_mask = 0;
303 BOOL render_offscreen;
304 unsigned int i;
305 RECT ds_rect = {0};
307 context = context_acquire(device, target);
308 if (!context->valid)
310 context_release(context);
311 WARN("Invalid context, skipping clear.\n");
312 return;
314 gl_info = context->gl_info;
316 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
317 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
318 * for the cleared parts, and the untouched parts.
320 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
321 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
322 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
323 * checking all this if the dest surface is in the drawable anyway. */
324 for (i = 0; i < rt_count; ++i)
326 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
328 if (rtv && rtv->format->id != WINED3DFMT_NULL)
330 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
332 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
333 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
334 else
335 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
339 if (target)
341 render_offscreen = context->render_offscreen;
342 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
344 else
346 render_offscreen = TRUE;
347 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
348 depth_stencil->texture_level);
349 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
350 depth_stencil->texture_level);
353 if (depth_stencil && render_offscreen)
354 wined3d_texture_prepare_location(depth_stencil->container,
355 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
357 if (flags & WINED3DCLEAR_ZBUFFER)
359 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
361 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
362 device_switch_onscreen_ds(device, context, depth_stencil);
363 prepare_ds_clear(depth_stencil, context, location,
364 draw_rect, rect_count, clear_rect, &ds_rect);
367 if (!context_apply_clear_state(context, state, rt_count, fb))
369 context_release(context);
370 WARN("Failed to apply clear state, skipping clear.\n");
371 return;
374 /* Only set the values up once, as they are not changing. */
375 if (flags & WINED3DCLEAR_STENCIL)
377 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
379 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
380 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
382 gl_info->gl_ops.gl.p_glStencilMask(~0U);
383 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
384 gl_info->gl_ops.gl.p_glClearStencil(stencil);
385 checkGLcall("glClearStencil");
386 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
389 if (flags & WINED3DCLEAR_ZBUFFER)
391 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
393 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
395 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
396 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
397 gl_info->gl_ops.gl.p_glClearDepth(depth);
398 checkGLcall("glClearDepth");
399 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
402 if (flags & WINED3DCLEAR_TARGET)
404 for (i = 0; i < rt_count; ++i)
406 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
407 struct wined3d_texture *texture;
409 if (!rtv)
410 continue;
412 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
414 FIXME("Not supported on buffer resources.\n");
415 continue;
418 texture = texture_from_resource(rtv->resource);
419 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
420 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
423 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
425 if (rt_count > 1)
426 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
427 "support, this might cause graphical issues.\n");
429 corrected_color.r = color->r < wined3d_srgb_const1[0]
430 ? color->r * wined3d_srgb_const0[3]
431 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
432 - wined3d_srgb_const0[2];
433 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
434 corrected_color.g = color->g < wined3d_srgb_const1[0]
435 ? color->g * wined3d_srgb_const0[3]
436 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
437 - wined3d_srgb_const0[2];
438 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
439 corrected_color.b = color->b < wined3d_srgb_const1[0]
440 ? color->b * wined3d_srgb_const0[3]
441 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
442 - wined3d_srgb_const0[2];
443 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
444 color = &corrected_color;
447 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
448 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
449 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
450 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
451 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
452 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
453 checkGLcall("glClearColor");
454 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
457 if (!rect_count)
459 if (render_offscreen)
461 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
462 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
464 else
466 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
467 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
469 checkGLcall("glScissor");
470 gl_info->gl_ops.gl.p_glClear(clear_mask);
471 checkGLcall("glClear");
473 else
475 RECT current_rect;
477 /* Now process each rect in turn. */
478 for (i = 0; i < rect_count; ++i)
480 /* Note that GL uses lower left, width/height. */
481 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
483 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
484 wine_dbgstr_rect(&clear_rect[i]),
485 wine_dbgstr_rect(&current_rect));
487 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
488 * The rectangle is not cleared, no error is returned, but further rectangles are
489 * still cleared if they are valid. */
490 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
492 TRACE("Rectangle with negative dimensions, ignoring.\n");
493 continue;
496 if (render_offscreen)
498 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
499 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
501 else
503 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
504 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
506 checkGLcall("glScissor");
508 gl_info->gl_ops.gl.p_glClear(clear_mask);
509 checkGLcall("glClear");
513 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
514 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
515 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
517 context_release(context);
520 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
522 ULONG refcount = InterlockedIncrement(&device->ref);
524 TRACE("%p increasing refcount to %u.\n", device, refcount);
526 return refcount;
529 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
531 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
533 ERR("Leftover sampler %p.\n", sampler);
536 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
538 ULONG refcount = InterlockedDecrement(&device->ref);
540 TRACE("%p decreasing refcount to %u.\n", device, refcount);
542 if (!refcount)
544 UINT i;
546 wined3d_cs_destroy(device->cs);
548 if (device->recording && wined3d_stateblock_decref(device->recording))
549 FIXME("Something's still holding the recording stateblock.\n");
550 device->recording = NULL;
552 state_cleanup(&device->state);
554 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
556 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
557 device->multistate_funcs[i] = NULL;
560 if (!list_empty(&device->resources))
562 struct wined3d_resource *resource;
564 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
566 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
568 FIXME("Leftover resource %p with type %s (%#x).\n",
569 resource, debug_d3dresourcetype(resource->type), resource->type);
573 if (device->contexts)
574 ERR("Context array not freed!\n");
575 if (device->hardwareCursor)
576 DestroyCursor(device->hardwareCursor);
577 device->hardwareCursor = 0;
579 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
581 wined3d_decref(device->wined3d);
582 device->wined3d = NULL;
583 HeapFree(GetProcessHeap(), 0, device);
584 TRACE("Freed device %p.\n", device);
587 return refcount;
590 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
592 TRACE("device %p.\n", device);
594 return device->swapchain_count;
597 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
599 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
601 if (swapchain_idx >= device->swapchain_count)
603 WARN("swapchain_idx %u >= swapchain_count %u.\n",
604 swapchain_idx, device->swapchain_count);
605 return NULL;
608 return device->swapchains[swapchain_idx];
611 static void device_load_logo(struct wined3d_device *device, const char *filename)
613 struct wined3d_color_key color_key;
614 struct wined3d_resource_desc desc;
615 HBITMAP hbm;
616 BITMAP bm;
617 HRESULT hr;
618 HDC dcb = NULL, dcs = NULL;
620 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
621 if(hbm)
623 GetObjectA(hbm, sizeof(BITMAP), &bm);
624 dcb = CreateCompatibleDC(NULL);
625 if(!dcb) goto out;
626 SelectObject(dcb, hbm);
628 else
630 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
631 * couldn't be loaded
633 memset(&bm, 0, sizeof(bm));
634 bm.bmWidth = 32;
635 bm.bmHeight = 32;
638 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
639 desc.format = WINED3DFMT_B5G6R5_UNORM;
640 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
641 desc.multisample_quality = 0;
642 desc.usage = WINED3DUSAGE_DYNAMIC;
643 desc.pool = WINED3D_POOL_DEFAULT;
644 desc.width = bm.bmWidth;
645 desc.height = bm.bmHeight;
646 desc.depth = 1;
647 desc.size = 0;
648 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
649 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
651 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
652 goto out;
655 if (dcb)
657 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
658 goto out;
659 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
660 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
662 color_key.color_space_low_value = 0;
663 color_key.color_space_high_value = 0;
664 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
666 else
668 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
669 const RECT rect = {0, 0, desc.width, desc.height};
670 struct wined3d_surface *surface;
672 /* Fill the surface with a white color to show that wined3d is there */
673 surface = device->logo_texture->sub_resources[0].u.surface;
674 surface_color_fill(surface, &rect, &c);
677 out:
678 if (dcb) DeleteDC(dcb);
679 if (hbm) DeleteObject(hbm);
682 /* Context activation is done by the caller. */
683 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
685 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
686 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
687 unsigned int i, j, count;
688 DWORD color;
690 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
691 color = 0x000000ff;
692 else
693 color = 0x00000000;
695 /* Under DirectX you can sample even if no texture is bound, whereas
696 * OpenGL will only allow that when a valid texture is bound.
697 * We emulate this by creating dummy textures and binding them
698 * to each texture stage when the currently set D3D texture is NULL. */
699 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
700 for (i = 0; i < count; ++i)
702 /* Make appropriate texture active */
703 context_active_texture(context, gl_info, i);
705 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
706 checkGLcall("glGenTextures");
707 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
709 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
710 checkGLcall("glBindTexture");
712 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
713 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
714 checkGLcall("glTexImage2D");
716 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
718 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
719 checkGLcall("glGenTextures");
720 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
722 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
723 checkGLcall("glBindTexture");
725 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
726 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
727 checkGLcall("glTexImage2D");
730 if (gl_info->supported[EXT_TEXTURE3D])
732 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
733 checkGLcall("glGenTextures");
734 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
736 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
737 checkGLcall("glBindTexture");
739 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
740 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
741 checkGLcall("glTexImage3D");
744 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
746 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
747 checkGLcall("glGenTextures");
748 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
750 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
751 checkGLcall("glBindTexture");
753 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
755 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
756 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
757 checkGLcall("glTexImage2D");
761 if (gl_info->supported[EXT_TEXTURE_ARRAY])
763 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d_array[i]);
764 checkGLcall("glGenTextures");
765 TRACE("Dummy 2D array texture %u given name %u.\n", i, device->dummy_texture_2d_array[i]);
767 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_texture_2d_array[i]);
768 checkGLcall("glBindTexture");
770 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
771 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
772 checkGLcall("glTexImage3D");
777 /* Context activation is done by the caller. */
778 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
780 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
782 if (gl_info->supported[EXT_TEXTURE_ARRAY])
784 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d_array);
785 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d_array)");
788 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
790 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
791 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
794 if (gl_info->supported[EXT_TEXTURE3D])
796 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
797 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
800 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
802 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
803 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
806 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
807 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
809 memset(device->dummy_texture_2d_array, 0, count * sizeof(*device->dummy_texture_2d_array));
810 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
811 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
812 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
813 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
816 /* Context activation is done by the caller. */
817 static void create_default_samplers(struct wined3d_device *device)
819 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
821 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
823 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
824 * instructions allow access to resources without using samplers.
825 * In GLSL, resources are always accessed through sampler or image variables. The default
826 * sampler object is used to emulate the direct resource access when there is no sampler state
827 * to use.
829 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
830 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
831 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
832 checkGLcall("Create default sampler");
834 /* In D3D10+, a NULL sampler maps to the default sampler state. */
835 GL_EXTCALL(glGenSamplers(1, &device->null_sampler));
836 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
837 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
838 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
839 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
840 checkGLcall("Create null sampler");
842 else
844 device->default_sampler = 0;
845 device->null_sampler = 0;
849 /* Context activation is done by the caller. */
850 static void destroy_default_samplers(struct wined3d_device *device)
852 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
854 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
856 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
857 GL_EXTCALL(glDeleteSamplers(1, &device->null_sampler));
858 checkGLcall("glDeleteSamplers");
861 device->default_sampler = 0;
862 device->null_sampler = 0;
865 static LONG fullscreen_style(LONG style)
867 /* Make sure the window is managed, otherwise we won't get keyboard input. */
868 style |= WS_POPUP | WS_SYSMENU;
869 style &= ~(WS_CAPTION | WS_THICKFRAME);
871 return style;
874 static LONG fullscreen_exstyle(LONG exstyle)
876 /* Filter out window decorations. */
877 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
879 return exstyle;
882 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
884 BOOL filter_messages;
885 LONG style, exstyle;
887 TRACE("Setting up window %p for fullscreen mode.\n", window);
889 if (device->style || device->exStyle)
891 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
892 window, device->style, device->exStyle);
895 device->style = GetWindowLongW(window, GWL_STYLE);
896 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
898 style = fullscreen_style(device->style);
899 exstyle = fullscreen_exstyle(device->exStyle);
901 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
902 device->style, device->exStyle, style, exstyle);
904 filter_messages = device->filter_messages;
905 device->filter_messages = TRUE;
907 SetWindowLongW(window, GWL_STYLE, style);
908 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
909 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
911 device->filter_messages = filter_messages;
914 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
915 const RECT *window_rect)
917 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
918 BOOL filter_messages;
919 LONG style, exstyle;
920 RECT rect = {0};
922 if (!device->style && !device->exStyle)
923 return;
925 style = GetWindowLongW(window, GWL_STYLE);
926 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
928 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
929 * application, and we want to ignore them in the test below, since it's
930 * not the application's fault that they changed. Additionally, we want to
931 * preserve the current status of these flags (i.e. don't restore them) to
932 * more closely emulate the behavior of Direct3D, which leaves these flags
933 * alone when returning to windowed mode. */
934 device->style ^= (device->style ^ style) & WS_VISIBLE;
935 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
937 TRACE("Restoring window style of window %p to %08x, %08x.\n",
938 window, device->style, device->exStyle);
940 filter_messages = device->filter_messages;
941 device->filter_messages = TRUE;
943 /* Only restore the style if the application didn't modify it during the
944 * fullscreen phase. Some applications change it before calling Reset()
945 * when switching between windowed and fullscreen modes (HL2), some
946 * depend on the original style (Eve Online). */
947 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
949 SetWindowLongW(window, GWL_STYLE, device->style);
950 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
953 if (window_rect)
954 rect = *window_rect;
955 else
956 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
957 SetWindowPos(window, 0, rect.left, rect.top,
958 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
960 device->filter_messages = filter_messages;
962 /* Delete the old values. */
963 device->style = 0;
964 device->exStyle = 0;
967 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
969 TRACE("device %p, window %p.\n", device, window);
971 if (!wined3d_register_window(window, device))
973 ERR("Failed to register window %p.\n", window);
974 return E_FAIL;
977 InterlockedExchangePointer((void **)&device->focus_window, window);
978 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
980 return WINED3D_OK;
983 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
985 TRACE("device %p.\n", device);
987 if (device->focus_window) wined3d_unregister_window(device->focus_window);
988 InterlockedExchangePointer((void **)&device->focus_window, NULL);
991 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
993 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
994 unsigned int i;
996 if (device->fb.render_targets)
998 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
1000 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1002 if (device->back_buffer_view)
1003 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
1006 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
1007 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
1010 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1011 struct wined3d_swapchain_desc *swapchain_desc)
1013 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1014 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1015 struct wined3d_swapchain *swapchain = NULL;
1016 struct wined3d_context *context;
1017 DWORD clear_flags = 0;
1018 HRESULT hr;
1020 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1022 if (device->d3d_initialized)
1023 return WINED3DERR_INVALIDCALL;
1024 if (device->wined3d->flags & WINED3D_NO3D)
1025 return WINED3DERR_INVALIDCALL;
1027 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
1028 return E_OUTOFMEMORY;
1030 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1031 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1033 TRACE("Shader private data couldn't be allocated\n");
1034 goto err_out;
1036 if (FAILED(hr = device->blitter->alloc_private(device)))
1038 TRACE("Blitter private data couldn't be allocated\n");
1039 goto err_out;
1042 /* Setup the implicit swapchain. This also initializes a context. */
1043 TRACE("Creating implicit swapchain\n");
1044 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1045 swapchain_desc, &swapchain);
1046 if (FAILED(hr))
1048 WARN("Failed to create implicit swapchain\n");
1049 goto err_out;
1052 if (swapchain_desc->backbuffer_count)
1054 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1055 struct wined3d_rendertarget_view_desc view_desc;
1057 view_desc.format_id = back_buffer->format->id;
1058 view_desc.u.texture.level_idx = 0;
1059 view_desc.u.texture.layer_idx = 0;
1060 view_desc.u.texture.layer_count = 1;
1061 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1062 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1064 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1065 goto err_out;
1069 device->swapchain_count = 1;
1070 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1072 ERR("Out of memory!\n");
1073 goto err_out;
1075 device->swapchains[0] = swapchain;
1076 device_init_swapchain_state(device, swapchain);
1078 context = context_acquire(device, NULL);
1080 create_dummy_textures(device, context);
1081 create_default_samplers(device);
1083 device->contexts[0]->last_was_rhw = 0;
1085 TRACE("All defaults now set up, leaving 3D init.\n");
1087 context_release(context);
1089 /* Clear the screen */
1090 if (swapchain->back_buffers && swapchain->back_buffers[0])
1091 clear_flags |= WINED3DCLEAR_TARGET;
1092 if (swapchain_desc->enable_auto_depth_stencil)
1093 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1094 if (clear_flags)
1095 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1097 device->d3d_initialized = TRUE;
1099 if (wined3d_settings.logo)
1100 device_load_logo(device, wined3d_settings.logo);
1101 return WINED3D_OK;
1103 err_out:
1104 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1105 HeapFree(GetProcessHeap(), 0, device->swapchains);
1106 device->swapchain_count = 0;
1107 if (device->back_buffer_view)
1108 wined3d_rendertarget_view_decref(device->back_buffer_view);
1109 if (swapchain)
1110 wined3d_swapchain_decref(swapchain);
1111 if (device->blit_priv)
1112 device->blitter->free_private(device);
1113 if (device->shader_priv)
1114 device->shader_backend->shader_free_private(device);
1116 return hr;
1119 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1120 struct wined3d_swapchain_desc *swapchain_desc)
1122 struct wined3d_swapchain *swapchain = NULL;
1123 HRESULT hr;
1125 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1127 /* Setup the implicit swapchain */
1128 TRACE("Creating implicit swapchain\n");
1129 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1130 swapchain_desc, &swapchain);
1131 if (FAILED(hr))
1133 WARN("Failed to create implicit swapchain\n");
1134 goto err_out;
1137 device->swapchain_count = 1;
1138 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1140 ERR("Out of memory!\n");
1141 goto err_out;
1143 device->swapchains[0] = swapchain;
1144 return WINED3D_OK;
1146 err_out:
1147 wined3d_swapchain_decref(swapchain);
1148 return hr;
1151 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1153 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1155 wined3d_sampler_decref(sampler);
1158 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1160 struct wined3d_resource *resource, *cursor;
1161 const struct wined3d_gl_info *gl_info;
1162 struct wined3d_context *context;
1163 struct wined3d_surface *surface;
1164 UINT i;
1166 TRACE("device %p.\n", device);
1168 if (!device->d3d_initialized)
1169 return WINED3DERR_INVALIDCALL;
1171 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1172 * it was created. Thus make sure a context is active for the glDelete* calls
1174 context = context_acquire(device, NULL);
1175 gl_info = context->gl_info;
1177 if (device->logo_texture)
1178 wined3d_texture_decref(device->logo_texture);
1179 if (device->cursor_texture)
1180 wined3d_texture_decref(device->cursor_texture);
1182 state_unbind_resources(&device->state);
1184 /* Unload resources */
1185 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1187 TRACE("Unloading resource %p.\n", resource);
1188 wined3d_cs_emit_unload_resource(device->cs, resource);
1191 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1193 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1194 * private data, it might contain opengl pointers
1196 if (device->depth_blt_texture)
1198 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1199 device->depth_blt_texture = 0;
1202 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1203 device->blitter->free_private(device);
1204 device->shader_backend->shader_free_private(device);
1205 destroy_dummy_textures(device, gl_info);
1206 destroy_default_samplers(device);
1208 /* Release the context again as soon as possible. In particular,
1209 * releasing the render target views below may release the last reference
1210 * to the swapchain associated with this context, which in turn will
1211 * destroy the context. */
1212 context_release(context);
1214 /* Release the buffers (with sanity checks)*/
1215 if (device->onscreen_depth_stencil)
1217 surface = device->onscreen_depth_stencil;
1218 device->onscreen_depth_stencil = NULL;
1219 wined3d_texture_decref(surface->container);
1222 if (device->fb.depth_stencil)
1224 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1226 TRACE("Releasing depth/stencil view %p.\n", view);
1228 device->fb.depth_stencil = NULL;
1229 wined3d_rendertarget_view_decref(view);
1232 if (device->auto_depth_stencil_view)
1234 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1236 device->auto_depth_stencil_view = NULL;
1237 if (wined3d_rendertarget_view_decref(view))
1238 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1241 for (i = 0; i < gl_info->limits.buffers; ++i)
1243 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1245 if (device->back_buffer_view)
1247 wined3d_rendertarget_view_decref(device->back_buffer_view);
1248 device->back_buffer_view = NULL;
1251 for (i = 0; i < device->swapchain_count; ++i)
1253 TRACE("Releasing the implicit swapchain %u.\n", i);
1254 if (wined3d_swapchain_decref(device->swapchains[i]))
1255 FIXME("Something's still holding the implicit swapchain.\n");
1258 HeapFree(GetProcessHeap(), 0, device->swapchains);
1259 device->swapchains = NULL;
1260 device->swapchain_count = 0;
1262 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1263 device->fb.render_targets = NULL;
1265 device->d3d_initialized = FALSE;
1267 return WINED3D_OK;
1270 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1272 unsigned int i;
1274 for (i = 0; i < device->swapchain_count; ++i)
1276 TRACE("Releasing the implicit swapchain %u.\n", i);
1277 if (wined3d_swapchain_decref(device->swapchains[i]))
1278 FIXME("Something's still holding the implicit swapchain.\n");
1281 HeapFree(GetProcessHeap(), 0, device->swapchains);
1282 device->swapchains = NULL;
1283 device->swapchain_count = 0;
1284 return WINED3D_OK;
1287 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1288 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1289 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1291 * There is no way to deactivate thread safety once it is enabled.
1293 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1295 TRACE("device %p.\n", device);
1297 /* For now just store the flag (needed in case of ddraw). */
1298 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1301 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1303 TRACE("device %p.\n", device);
1305 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1306 wine_dbgstr_longlong(device->adapter->vram_bytes),
1307 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1308 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1310 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1313 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1314 struct wined3d_buffer *buffer, UINT offset)
1316 struct wined3d_stream_output *stream;
1317 struct wined3d_buffer *prev_buffer;
1319 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1321 if (idx >= MAX_STREAM_OUT)
1323 WARN("Invalid stream output %u.\n", idx);
1324 return;
1327 stream = &device->update_state->stream_output[idx];
1328 prev_buffer = stream->buffer;
1330 if (buffer)
1331 wined3d_buffer_incref(buffer);
1332 stream->buffer = buffer;
1333 stream->offset = offset;
1334 if (!device->recording)
1335 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1336 if (prev_buffer)
1337 wined3d_buffer_decref(prev_buffer);
1340 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1341 UINT idx, UINT *offset)
1343 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1345 if (idx >= MAX_STREAM_OUT)
1347 WARN("Invalid stream output %u.\n", idx);
1348 return NULL;
1351 if (offset)
1352 *offset = device->state.stream_output[idx].offset;
1353 return device->state.stream_output[idx].buffer;
1356 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1357 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1359 struct wined3d_stream_state *stream;
1360 struct wined3d_buffer *prev_buffer;
1362 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1363 device, stream_idx, buffer, offset, stride);
1365 if (stream_idx >= MAX_STREAMS)
1367 WARN("Stream index %u out of range.\n", stream_idx);
1368 return WINED3DERR_INVALIDCALL;
1370 else if (offset & 0x3)
1372 WARN("Offset %u is not 4 byte aligned.\n", offset);
1373 return WINED3DERR_INVALIDCALL;
1376 stream = &device->update_state->streams[stream_idx];
1377 prev_buffer = stream->buffer;
1379 if (device->recording)
1380 device->recording->changed.streamSource |= 1u << stream_idx;
1382 if (prev_buffer == buffer
1383 && stream->stride == stride
1384 && stream->offset == offset)
1386 TRACE("Application is setting the old values over, nothing to do.\n");
1387 return WINED3D_OK;
1390 stream->buffer = buffer;
1391 if (buffer)
1393 stream->stride = stride;
1394 stream->offset = offset;
1395 wined3d_buffer_incref(buffer);
1398 if (!device->recording)
1399 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1400 if (prev_buffer)
1401 wined3d_buffer_decref(prev_buffer);
1403 return WINED3D_OK;
1406 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1407 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1409 const struct wined3d_stream_state *stream;
1411 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1412 device, stream_idx, buffer, offset, stride);
1414 if (stream_idx >= MAX_STREAMS)
1416 WARN("Stream index %u out of range.\n", stream_idx);
1417 return WINED3DERR_INVALIDCALL;
1420 stream = &device->state.streams[stream_idx];
1421 *buffer = stream->buffer;
1422 if (offset)
1423 *offset = stream->offset;
1424 *stride = stream->stride;
1426 return WINED3D_OK;
1429 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1431 struct wined3d_stream_state *stream;
1432 UINT old_flags, old_freq;
1434 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1436 /* Verify input. At least in d3d9 this is invalid. */
1437 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1439 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1440 return WINED3DERR_INVALIDCALL;
1442 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1444 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1445 return WINED3DERR_INVALIDCALL;
1447 if (!divider)
1449 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1450 return WINED3DERR_INVALIDCALL;
1453 stream = &device->update_state->streams[stream_idx];
1454 old_flags = stream->flags;
1455 old_freq = stream->frequency;
1457 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1458 stream->frequency = divider & 0x7fffff;
1460 if (device->recording)
1461 device->recording->changed.streamFreq |= 1u << stream_idx;
1462 else if (stream->frequency != old_freq || stream->flags != old_flags)
1463 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1465 return WINED3D_OK;
1468 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1469 UINT stream_idx, UINT *divider)
1471 const struct wined3d_stream_state *stream;
1473 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1475 stream = &device->state.streams[stream_idx];
1476 *divider = stream->flags | stream->frequency;
1478 TRACE("Returning %#x.\n", *divider);
1480 return WINED3D_OK;
1483 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1484 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1486 TRACE("device %p, state %s, matrix %p.\n",
1487 device, debug_d3dtstype(d3dts), matrix);
1488 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1489 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1490 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1491 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1493 /* Handle recording of state blocks. */
1494 if (device->recording)
1496 TRACE("Recording... not performing anything.\n");
1497 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1498 device->update_state->transforms[d3dts] = *matrix;
1499 return;
1502 /* If the new matrix is the same as the current one,
1503 * we cut off any further processing. this seems to be a reasonable
1504 * optimization because as was noticed, some apps (warcraft3 for example)
1505 * tend towards setting the same matrix repeatedly for some reason.
1507 * From here on we assume that the new matrix is different, wherever it matters. */
1508 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1510 TRACE("The application is setting the same matrix over again.\n");
1511 return;
1514 device->state.transforms[d3dts] = *matrix;
1515 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1518 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1519 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1521 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1523 *matrix = device->state.transforms[state];
1526 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1527 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1529 const struct wined3d_matrix *mat;
1530 struct wined3d_matrix temp;
1532 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1534 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1535 * below means it will be recorded in a state block change, but it
1536 * works regardless where it is recorded.
1537 * If this is found to be wrong, change to StateBlock. */
1538 if (state > HIGHEST_TRANSFORMSTATE)
1540 WARN("Unhandled transform state %#x.\n", state);
1541 return;
1544 mat = &device->update_state->transforms[state];
1545 multiply_matrix(&temp, mat, matrix);
1547 /* Apply change via set transform - will reapply to eg. lights this way. */
1548 wined3d_device_set_transform(device, state, &temp);
1551 /* Note lights are real special cases. Although the device caps state only
1552 * e.g. 8 are supported, you can reference any indexes you want as long as
1553 * that number max are enabled at any one point in time. Therefore since the
1554 * indices can be anything, we need a hashmap of them. However, this causes
1555 * stateblock problems. When capturing the state block, I duplicate the
1556 * hashmap, but when recording, just build a chain pretty much of commands to
1557 * be replayed. */
1558 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1559 UINT light_idx, const struct wined3d_light *light)
1561 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1562 struct wined3d_light_info *object = NULL;
1563 struct list *e;
1564 float rho;
1566 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1568 /* Check the parameter range. Need for speed most wanted sets junk lights
1569 * which confuse the GL driver. */
1570 if (!light)
1571 return WINED3DERR_INVALIDCALL;
1573 switch (light->type)
1575 case WINED3D_LIGHT_POINT:
1576 case WINED3D_LIGHT_SPOT:
1577 case WINED3D_LIGHT_GLSPOT:
1578 /* Incorrect attenuation values can cause the gl driver to crash.
1579 * Happens with Need for speed most wanted. */
1580 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1582 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1583 return WINED3DERR_INVALIDCALL;
1585 break;
1587 case WINED3D_LIGHT_DIRECTIONAL:
1588 case WINED3D_LIGHT_PARALLELPOINT:
1589 /* Ignores attenuation */
1590 break;
1592 default:
1593 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1594 return WINED3DERR_INVALIDCALL;
1597 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1599 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1600 if (object->OriginalIndex == light_idx)
1601 break;
1602 object = NULL;
1605 if (!object)
1607 TRACE("Adding new light\n");
1608 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1609 if (!object)
1610 return E_OUTOFMEMORY;
1612 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1613 object->glIndex = -1;
1614 object->OriginalIndex = light_idx;
1617 /* Initialize the object. */
1618 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1619 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1620 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1621 light_idx, light->type, debug_color(&light->diffuse),
1622 debug_color(&light->specular), debug_color(&light->ambient),
1623 light->position.x, light->position.y, light->position.z,
1624 light->direction.x, light->direction.y, light->direction.z,
1625 light->range, light->falloff, light->theta, light->phi);
1627 /* Update the live definitions if the light is currently assigned a glIndex. */
1628 if (object->glIndex != -1 && !device->recording)
1630 if (object->OriginalParms.type != light->type)
1631 device_invalidate_state(device, STATE_LIGHT_TYPE);
1632 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1635 /* Save away the information. */
1636 object->OriginalParms = *light;
1638 switch (light->type)
1640 case WINED3D_LIGHT_POINT:
1641 /* Position */
1642 object->position.x = light->position.x;
1643 object->position.y = light->position.y;
1644 object->position.z = light->position.z;
1645 object->position.w = 1.0f;
1646 object->cutoff = 180.0f;
1647 /* FIXME: Range */
1648 break;
1650 case WINED3D_LIGHT_DIRECTIONAL:
1651 /* Direction */
1652 object->direction.x = -light->direction.x;
1653 object->direction.y = -light->direction.y;
1654 object->direction.z = -light->direction.z;
1655 object->direction.w = 0.0f;
1656 object->exponent = 0.0f;
1657 object->cutoff = 180.0f;
1658 break;
1660 case WINED3D_LIGHT_SPOT:
1661 /* Position */
1662 object->position.x = light->position.x;
1663 object->position.y = light->position.y;
1664 object->position.z = light->position.z;
1665 object->position.w = 1.0f;
1667 /* Direction */
1668 object->direction.x = light->direction.x;
1669 object->direction.y = light->direction.y;
1670 object->direction.z = light->direction.z;
1671 object->direction.w = 0.0f;
1673 /* opengl-ish and d3d-ish spot lights use too different models
1674 * for the light "intensity" as a function of the angle towards
1675 * the main light direction, so we only can approximate very
1676 * roughly. However, spot lights are rather rarely used in games
1677 * (if ever used at all). Furthermore if still used, probably
1678 * nobody pays attention to such details. */
1679 if (!light->falloff)
1681 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1682 * equations have the falloff resp. exponent parameter as an
1683 * exponent, so the spot light lighting will always be 1.0 for
1684 * both of them, and we don't have to care for the rest of the
1685 * rather complex calculation. */
1686 object->exponent = 0.0f;
1688 else
1690 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1691 if (rho < 0.0001f)
1692 rho = 0.0001f;
1693 object->exponent = -0.3f / logf(cosf(rho / 2));
1696 if (object->exponent > 128.0f)
1697 object->exponent = 128.0f;
1699 object->cutoff = (float)(light->phi * 90 / M_PI);
1700 /* FIXME: Range */
1701 break;
1703 case WINED3D_LIGHT_PARALLELPOINT:
1704 object->position.x = light->position.x;
1705 object->position.y = light->position.y;
1706 object->position.z = light->position.z;
1707 object->position.w = 1.0f;
1708 break;
1710 default:
1711 FIXME("Unrecognized light type %#x.\n", light->type);
1714 return WINED3D_OK;
1717 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1718 UINT light_idx, struct wined3d_light *light)
1720 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1721 struct wined3d_light_info *light_info = NULL;
1722 struct list *e;
1724 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1726 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1728 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1729 if (light_info->OriginalIndex == light_idx)
1730 break;
1731 light_info = NULL;
1734 if (!light_info)
1736 TRACE("Light information requested but light not defined\n");
1737 return WINED3DERR_INVALIDCALL;
1740 *light = light_info->OriginalParms;
1741 return WINED3D_OK;
1744 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1746 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1747 struct wined3d_light_info *light_info = NULL;
1748 struct list *e;
1750 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1752 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1754 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1755 if (light_info->OriginalIndex == light_idx)
1756 break;
1757 light_info = NULL;
1759 TRACE("Found light %p.\n", light_info);
1761 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1762 if (!light_info)
1764 TRACE("Light enabled requested but light not defined, so defining one!\n");
1765 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1767 /* Search for it again! Should be fairly quick as near head of list. */
1768 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1770 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1771 if (light_info->OriginalIndex == light_idx)
1772 break;
1773 light_info = NULL;
1775 if (!light_info)
1777 FIXME("Adding default lights has failed dismally\n");
1778 return WINED3DERR_INVALIDCALL;
1782 if (!enable)
1784 if (light_info->glIndex != -1)
1786 if (!device->recording)
1788 device_invalidate_state(device, STATE_LIGHT_TYPE);
1789 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1792 device->update_state->lights[light_info->glIndex] = NULL;
1793 light_info->glIndex = -1;
1795 else
1797 TRACE("Light already disabled, nothing to do\n");
1799 light_info->enabled = FALSE;
1801 else
1803 light_info->enabled = TRUE;
1804 if (light_info->glIndex != -1)
1806 TRACE("Nothing to do as light was enabled\n");
1808 else
1810 unsigned int i;
1811 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1812 /* Find a free GL light. */
1813 for (i = 0; i < gl_info->limits.lights; ++i)
1815 if (!device->update_state->lights[i])
1817 device->update_state->lights[i] = light_info;
1818 light_info->glIndex = i;
1819 break;
1822 if (light_info->glIndex == -1)
1824 /* Our tests show that Windows returns D3D_OK in this situation, even with
1825 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1826 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1827 * as well for those lights.
1829 * TODO: Test how this affects rendering. */
1830 WARN("Too many concurrently active lights\n");
1831 return WINED3D_OK;
1834 /* i == light_info->glIndex */
1835 if (!device->recording)
1837 device_invalidate_state(device, STATE_LIGHT_TYPE);
1838 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1843 return WINED3D_OK;
1846 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1848 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1849 struct wined3d_light_info *light_info = NULL;
1850 struct list *e;
1852 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1854 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1856 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1857 if (light_info->OriginalIndex == light_idx)
1858 break;
1859 light_info = NULL;
1862 if (!light_info)
1864 TRACE("Light enabled state requested but light not defined.\n");
1865 return WINED3DERR_INVALIDCALL;
1867 /* true is 128 according to SetLightEnable */
1868 *enable = light_info->enabled ? 128 : 0;
1869 return WINED3D_OK;
1872 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1873 UINT plane_idx, const struct wined3d_vec4 *plane)
1875 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1877 /* Validate plane_idx. */
1878 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1880 TRACE("Application has requested clipplane this device doesn't support.\n");
1881 return WINED3DERR_INVALIDCALL;
1884 if (device->recording)
1885 device->recording->changed.clipplane |= 1u << plane_idx;
1887 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1889 TRACE("Application is setting old values over, nothing to do.\n");
1890 return WINED3D_OK;
1893 device->update_state->clip_planes[plane_idx] = *plane;
1895 if (!device->recording)
1896 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1898 return WINED3D_OK;
1901 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1902 UINT plane_idx, struct wined3d_vec4 *plane)
1904 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1906 /* Validate plane_idx. */
1907 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1909 TRACE("Application has requested clipplane this device doesn't support.\n");
1910 return WINED3DERR_INVALIDCALL;
1913 *plane = device->state.clip_planes[plane_idx];
1915 return WINED3D_OK;
1918 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1919 const struct wined3d_clip_status *clip_status)
1921 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1923 if (!clip_status)
1924 return WINED3DERR_INVALIDCALL;
1926 return WINED3D_OK;
1929 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1930 struct wined3d_clip_status *clip_status)
1932 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1934 if (!clip_status)
1935 return WINED3DERR_INVALIDCALL;
1937 return WINED3D_OK;
1940 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1942 TRACE("device %p, material %p.\n", device, material);
1944 device->update_state->material = *material;
1946 if (device->recording)
1947 device->recording->changed.material = TRUE;
1948 else
1949 wined3d_cs_emit_set_material(device->cs, material);
1952 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1954 TRACE("device %p, material %p.\n", device, material);
1956 *material = device->state.material;
1958 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1959 TRACE("ambient %s\n", debug_color(&material->ambient));
1960 TRACE("specular %s\n", debug_color(&material->specular));
1961 TRACE("emissive %s\n", debug_color(&material->emissive));
1962 TRACE("power %.8e.\n", material->power);
1965 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1966 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1968 enum wined3d_format_id prev_format;
1969 struct wined3d_buffer *prev_buffer;
1970 unsigned int prev_offset;
1972 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1973 device, buffer, debug_d3dformat(format_id), offset);
1975 prev_buffer = device->update_state->index_buffer;
1976 prev_format = device->update_state->index_format;
1977 prev_offset = device->update_state->index_offset;
1979 device->update_state->index_buffer = buffer;
1980 device->update_state->index_format = format_id;
1981 device->update_state->index_offset = offset;
1983 if (device->recording)
1984 device->recording->changed.indices = TRUE;
1986 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1987 return;
1989 if (buffer)
1990 wined3d_buffer_incref(buffer);
1991 if (!device->recording)
1992 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1993 if (prev_buffer)
1994 wined3d_buffer_decref(prev_buffer);
1997 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1998 enum wined3d_format_id *format, unsigned int *offset)
2000 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
2002 *format = device->state.index_format;
2003 if (offset)
2004 *offset = device->state.index_offset;
2005 return device->state.index_buffer;
2008 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2010 TRACE("device %p, base_index %d.\n", device, base_index);
2012 device->update_state->base_vertex_index = base_index;
2015 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2017 TRACE("device %p.\n", device);
2019 return device->state.base_vertex_index;
2022 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2024 TRACE("device %p, viewport %p.\n", device, viewport);
2025 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2026 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2028 device->update_state->viewport = *viewport;
2030 /* Handle recording of state blocks */
2031 if (device->recording)
2033 TRACE("Recording... not performing anything\n");
2034 device->recording->changed.viewport = TRUE;
2035 return;
2038 wined3d_cs_emit_set_viewport(device->cs, viewport);
2041 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2043 TRACE("device %p, viewport %p.\n", device, viewport);
2045 *viewport = device->state.viewport;
2048 static void resolve_depth_buffer(struct wined3d_state *state)
2050 struct wined3d_texture *dst_texture = state->textures[0];
2051 struct wined3d_rendertarget_view *src_view;
2052 RECT src_rect, dst_rect;
2054 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2055 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2056 return;
2058 if (!(src_view = state->fb->depth_stencil))
2059 return;
2060 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2062 FIXME("Not supported on buffer resources.\n");
2063 return;
2066 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2067 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2068 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
2069 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2072 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2073 struct wined3d_rasterizer_state *rasterizer_state)
2075 struct wined3d_rasterizer_state *prev;
2077 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2079 prev = device->update_state->rasterizer_state;
2080 if (prev == rasterizer_state)
2081 return;
2083 if (rasterizer_state)
2084 wined3d_rasterizer_state_incref(rasterizer_state);
2085 device->update_state->rasterizer_state = rasterizer_state;
2086 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2087 if (prev)
2088 wined3d_rasterizer_state_decref(prev);
2091 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2093 TRACE("device %p.\n", device);
2095 return device->state.rasterizer_state;
2098 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2099 enum wined3d_render_state state, DWORD value)
2101 DWORD old_value;
2103 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2105 if (state > WINEHIGHEST_RENDER_STATE)
2107 WARN("Unhandled render state %#x.\n", state);
2108 return;
2111 old_value = device->state.render_states[state];
2112 device->update_state->render_states[state] = value;
2114 /* Handle recording of state blocks. */
2115 if (device->recording)
2117 TRACE("Recording... not performing anything.\n");
2118 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2119 return;
2122 /* Compared here and not before the assignment to allow proper stateblock recording. */
2123 if (value == old_value)
2124 TRACE("Application is setting the old value over, nothing to do.\n");
2125 else
2126 wined3d_cs_emit_set_render_state(device->cs, state, value);
2128 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2130 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2131 resolve_depth_buffer(&device->state);
2135 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2137 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2139 return device->state.render_states[state];
2142 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2143 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2145 DWORD old_value;
2147 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2148 device, sampler_idx, debug_d3dsamplerstate(state), value);
2150 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2151 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2153 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2155 WARN("Invalid sampler %u.\n", sampler_idx);
2156 return; /* Windows accepts overflowing this array ... we do not. */
2159 old_value = device->state.sampler_states[sampler_idx][state];
2160 device->update_state->sampler_states[sampler_idx][state] = value;
2162 /* Handle recording of state blocks. */
2163 if (device->recording)
2165 TRACE("Recording... not performing anything.\n");
2166 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2167 return;
2170 if (old_value == value)
2172 TRACE("Application is setting the old value over, nothing to do.\n");
2173 return;
2176 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2179 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2180 UINT sampler_idx, enum wined3d_sampler_state state)
2182 TRACE("device %p, sampler_idx %u, state %s.\n",
2183 device, sampler_idx, debug_d3dsamplerstate(state));
2185 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2186 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2188 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2190 WARN("Invalid sampler %u.\n", sampler_idx);
2191 return 0; /* Windows accepts overflowing this array ... we do not. */
2194 return device->state.sampler_states[sampler_idx][state];
2197 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2199 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2201 if (device->recording)
2202 device->recording->changed.scissorRect = TRUE;
2204 if (EqualRect(&device->update_state->scissor_rect, rect))
2206 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2207 return;
2209 CopyRect(&device->update_state->scissor_rect, rect);
2211 if (device->recording)
2213 TRACE("Recording... not performing anything.\n");
2214 return;
2217 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2220 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2222 TRACE("device %p, rect %p.\n", device, rect);
2224 *rect = device->state.scissor_rect;
2225 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2228 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2229 struct wined3d_vertex_declaration *declaration)
2231 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2233 TRACE("device %p, declaration %p.\n", device, declaration);
2235 if (device->recording)
2236 device->recording->changed.vertexDecl = TRUE;
2238 if (declaration == prev)
2239 return;
2241 if (declaration)
2242 wined3d_vertex_declaration_incref(declaration);
2243 device->update_state->vertex_declaration = declaration;
2244 if (!device->recording)
2245 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2246 if (prev)
2247 wined3d_vertex_declaration_decref(prev);
2250 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2252 TRACE("device %p.\n", device);
2254 return device->state.vertex_declaration;
2257 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2259 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2261 TRACE("device %p, shader %p.\n", device, shader);
2263 if (device->recording)
2264 device->recording->changed.vertexShader = TRUE;
2266 if (shader == prev)
2267 return;
2269 if (shader)
2270 wined3d_shader_incref(shader);
2271 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2272 if (!device->recording)
2273 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2274 if (prev)
2275 wined3d_shader_decref(prev);
2278 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2280 TRACE("device %p.\n", device);
2282 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2285 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2286 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2288 struct wined3d_buffer *prev;
2290 if (idx >= MAX_CONSTANT_BUFFERS)
2292 WARN("Invalid constant buffer index %u.\n", idx);
2293 return;
2296 prev = device->update_state->cb[type][idx];
2297 if (buffer == prev)
2298 return;
2300 if (buffer)
2301 wined3d_buffer_incref(buffer);
2302 device->update_state->cb[type][idx] = buffer;
2303 if (!device->recording)
2304 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2305 if (prev)
2306 wined3d_buffer_decref(prev);
2309 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2311 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2313 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2316 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2318 TRACE("device %p, idx %u.\n", device, idx);
2320 if (idx >= MAX_CONSTANT_BUFFERS)
2322 WARN("Invalid constant buffer index %u.\n", idx);
2323 return NULL;
2326 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2329 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2330 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2332 struct wined3d_shader_resource_view *prev;
2334 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2336 WARN("Invalid view index %u.\n", idx);
2337 return;
2340 prev = device->update_state->shader_resource_view[type][idx];
2341 if (view == prev)
2342 return;
2344 if (view)
2345 wined3d_shader_resource_view_incref(view);
2346 device->update_state->shader_resource_view[type][idx] = view;
2347 if (!device->recording)
2348 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2349 if (prev)
2350 wined3d_shader_resource_view_decref(prev);
2353 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2354 UINT idx, struct wined3d_shader_resource_view *view)
2356 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2358 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2361 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2362 UINT idx)
2364 TRACE("device %p, idx %u.\n", device, idx);
2366 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2368 WARN("Invalid view index %u.\n", idx);
2369 return NULL;
2372 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2375 static void wined3d_device_set_sampler(struct wined3d_device *device,
2376 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2378 struct wined3d_sampler *prev;
2380 if (idx >= MAX_SAMPLER_OBJECTS)
2382 WARN("Invalid sampler index %u.\n", idx);
2383 return;
2386 prev = device->update_state->sampler[type][idx];
2387 if (sampler == prev)
2388 return;
2390 if (sampler)
2391 wined3d_sampler_incref(sampler);
2392 device->update_state->sampler[type][idx] = sampler;
2393 if (!device->recording)
2394 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2395 if (prev)
2396 wined3d_sampler_decref(prev);
2399 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2401 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2403 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2406 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2408 TRACE("device %p, idx %u.\n", device, idx);
2410 if (idx >= MAX_SAMPLER_OBJECTS)
2412 WARN("Invalid sampler index %u.\n", idx);
2413 return NULL;
2416 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2419 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2420 unsigned int start_idx, unsigned int count, const BOOL *constants)
2422 unsigned int i;
2424 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2425 device, start_idx, count, constants);
2427 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2428 return WINED3DERR_INVALIDCALL;
2430 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2431 count = WINED3D_MAX_CONSTS_B - start_idx;
2432 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2433 if (TRACE_ON(d3d))
2435 for (i = 0; i < count; ++i)
2436 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2439 if (device->recording)
2441 for (i = start_idx; i < count + start_idx; ++i)
2442 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2444 else
2446 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2449 return WINED3D_OK;
2452 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2453 unsigned int start_idx, unsigned int count, BOOL *constants)
2455 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2456 device, start_idx, count, constants);
2458 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2459 return WINED3DERR_INVALIDCALL;
2461 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2462 count = WINED3D_MAX_CONSTS_B - start_idx;
2463 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2465 return WINED3D_OK;
2468 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2469 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2471 unsigned int i;
2473 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2474 device, start_idx, count, constants);
2476 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2477 return WINED3DERR_INVALIDCALL;
2479 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2480 count = WINED3D_MAX_CONSTS_I - start_idx;
2481 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2482 if (TRACE_ON(d3d))
2484 for (i = 0; i < count; ++i)
2485 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2488 if (device->recording)
2490 for (i = start_idx; i < count + start_idx; ++i)
2491 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2493 else
2495 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2498 return WINED3D_OK;
2501 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2502 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2504 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2505 device, start_idx, count, constants);
2507 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2508 return WINED3DERR_INVALIDCALL;
2510 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2511 count = WINED3D_MAX_CONSTS_I - start_idx;
2512 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2513 return WINED3D_OK;
2516 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2517 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2519 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2520 unsigned int i;
2522 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2523 device, start_idx, count, constants);
2525 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2526 || count > d3d_info->limits.vs_uniform_count - start_idx)
2527 return WINED3DERR_INVALIDCALL;
2529 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2530 if (TRACE_ON(d3d))
2532 for (i = 0; i < count; ++i)
2533 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2536 if (device->recording)
2537 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2538 count * sizeof(*device->recording->changed.vs_consts_f));
2539 else
2540 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2542 return WINED3D_OK;
2545 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2546 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2548 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2550 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2551 device, start_idx, count, constants);
2553 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2554 || count > d3d_info->limits.vs_uniform_count - start_idx)
2555 return WINED3DERR_INVALIDCALL;
2557 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2559 return WINED3D_OK;
2562 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2564 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2566 TRACE("device %p, shader %p.\n", device, shader);
2568 if (device->recording)
2569 device->recording->changed.pixelShader = TRUE;
2571 if (shader == prev)
2572 return;
2574 if (shader)
2575 wined3d_shader_incref(shader);
2576 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2577 if (!device->recording)
2578 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2579 if (prev)
2580 wined3d_shader_decref(prev);
2583 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2585 TRACE("device %p.\n", device);
2587 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2590 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2592 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2594 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2597 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2599 TRACE("device %p, idx %u.\n", device, idx);
2601 if (idx >= MAX_CONSTANT_BUFFERS)
2603 WARN("Invalid constant buffer index %u.\n", idx);
2604 return NULL;
2607 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2610 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2611 UINT idx, struct wined3d_shader_resource_view *view)
2613 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2615 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2618 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2619 UINT idx)
2621 TRACE("device %p, idx %u.\n", device, idx);
2623 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2625 WARN("Invalid view index %u.\n", idx);
2626 return NULL;
2629 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2632 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2634 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2636 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2639 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2641 TRACE("device %p, idx %u.\n", device, idx);
2643 if (idx >= MAX_SAMPLER_OBJECTS)
2645 WARN("Invalid sampler index %u.\n", idx);
2646 return NULL;
2649 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2652 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2653 unsigned int start_idx, unsigned int count, const BOOL *constants)
2655 unsigned int i;
2657 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2658 device, start_idx, count, constants);
2660 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2661 return WINED3DERR_INVALIDCALL;
2663 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2664 count = WINED3D_MAX_CONSTS_B - start_idx;
2665 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2666 if (TRACE_ON(d3d))
2668 for (i = 0; i < count; ++i)
2669 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2672 if (device->recording)
2674 for (i = start_idx; i < count + start_idx; ++i)
2675 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2677 else
2679 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2682 return WINED3D_OK;
2685 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2686 unsigned int start_idx, unsigned int count, BOOL *constants)
2688 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2689 device, start_idx, count, constants);
2691 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2692 return WINED3DERR_INVALIDCALL;
2694 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2695 count = WINED3D_MAX_CONSTS_B - start_idx;
2696 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2698 return WINED3D_OK;
2701 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2702 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2704 unsigned int i;
2706 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2707 device, start_idx, count, constants);
2709 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2710 return WINED3DERR_INVALIDCALL;
2712 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2713 count = WINED3D_MAX_CONSTS_I - start_idx;
2714 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2715 if (TRACE_ON(d3d))
2717 for (i = 0; i < count; ++i)
2718 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2721 if (device->recording)
2723 for (i = start_idx; i < count + start_idx; ++i)
2724 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2726 else
2728 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2731 return WINED3D_OK;
2734 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2735 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2737 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2738 device, start_idx, count, constants);
2740 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2741 return WINED3DERR_INVALIDCALL;
2743 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2744 count = WINED3D_MAX_CONSTS_I - start_idx;
2745 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2747 return WINED3D_OK;
2750 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2751 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2753 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2754 unsigned int i;
2756 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2757 device, start_idx, count, constants);
2759 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2760 || count > d3d_info->limits.ps_uniform_count - start_idx)
2761 return WINED3DERR_INVALIDCALL;
2763 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2764 if (TRACE_ON(d3d))
2766 for (i = 0; i < count; ++i)
2767 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2770 if (device->recording)
2771 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2772 count * sizeof(*device->recording->changed.ps_consts_f));
2773 else
2774 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2776 return WINED3D_OK;
2779 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2780 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2782 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2784 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2785 device, start_idx, count, constants);
2787 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2788 || count > d3d_info->limits.ps_uniform_count - start_idx)
2789 return WINED3DERR_INVALIDCALL;
2791 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2793 return WINED3D_OK;
2796 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2798 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2800 TRACE("device %p, shader %p.\n", device, shader);
2802 if (device->recording || shader == prev)
2803 return;
2804 if (shader)
2805 wined3d_shader_incref(shader);
2806 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2807 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2808 if (prev)
2809 wined3d_shader_decref(prev);
2812 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2814 TRACE("device %p.\n", device);
2816 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2819 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2821 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2823 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2826 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2828 TRACE("device %p, idx %u.\n", device, idx);
2830 if (idx >= MAX_CONSTANT_BUFFERS)
2832 WARN("Invalid constant buffer index %u.\n", idx);
2833 return NULL;
2836 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2839 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2840 UINT idx, struct wined3d_shader_resource_view *view)
2842 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2844 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2847 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2848 UINT idx)
2850 TRACE("device %p, idx %u.\n", device, idx);
2852 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2854 WARN("Invalid view index %u.\n", idx);
2855 return NULL;
2858 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2861 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2863 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2865 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2868 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2870 TRACE("device %p, idx %u.\n", device, idx);
2872 if (idx >= MAX_SAMPLER_OBJECTS)
2874 WARN("Invalid sampler index %u.\n", idx);
2875 return NULL;
2878 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2881 /* Context activation is done by the caller. */
2882 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2883 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2884 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2885 DWORD DestFVF)
2887 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2888 struct wined3d_map_desc map_desc;
2889 struct wined3d_box box = {0};
2890 struct wined3d_viewport vp;
2891 UINT vertex_size;
2892 unsigned int i;
2893 BYTE *dest_ptr;
2894 BOOL doClip;
2895 DWORD numTextures;
2896 HRESULT hr;
2898 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2900 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2903 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2905 ERR("Source has no position mask\n");
2906 return WINED3DERR_INVALIDCALL;
2909 if (device->state.render_states[WINED3D_RS_CLIPPING])
2911 static BOOL warned = FALSE;
2913 * The clipping code is not quite correct. Some things need
2914 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2915 * so disable clipping for now.
2916 * (The graphics in Half-Life are broken, and my processvertices
2917 * test crashes with IDirect3DDevice3)
2918 doClip = TRUE;
2920 doClip = FALSE;
2921 if(!warned) {
2922 warned = TRUE;
2923 FIXME("Clipping is broken and disabled for now\n");
2926 else
2927 doClip = FALSE;
2929 vertex_size = get_flexible_vertex_size(DestFVF);
2930 box.left = dwDestIndex * vertex_size;
2931 box.right = box.left + dwCount * vertex_size;
2932 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2934 WARN("Failed to map buffer, hr %#x.\n", hr);
2935 return hr;
2937 dest_ptr = map_desc.data;
2939 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2940 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2941 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2943 TRACE("View mat:\n");
2944 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2945 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2946 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2947 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2949 TRACE("Proj mat:\n");
2950 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2951 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2952 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2953 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2955 TRACE("World mat:\n");
2956 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2957 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2958 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2959 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2961 /* Get the viewport */
2962 wined3d_device_get_viewport(device, &vp);
2963 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2964 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2966 multiply_matrix(&mat,&view_mat,&world_mat);
2967 multiply_matrix(&mat,&proj_mat,&mat);
2969 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2971 for (i = 0; i < dwCount; i+= 1) {
2972 unsigned int tex_index;
2974 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2975 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2976 /* The position first */
2977 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2978 const float *p = (const float *)(element->data.addr + i * element->stride);
2979 float x, y, z, rhw;
2980 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2982 /* Multiplication with world, view and projection matrix. */
2983 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2984 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2985 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2986 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2988 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2990 /* WARNING: The following things are taken from d3d7 and were not yet checked
2991 * against d3d8 or d3d9!
2994 /* Clipping conditions: From msdn
2996 * A vertex is clipped if it does not match the following requirements
2997 * -rhw < x <= rhw
2998 * -rhw < y <= rhw
2999 * 0 < z <= rhw
3000 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3002 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3003 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3007 if( !doClip ||
3008 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3009 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3010 ( rhw > eps ) ) ) {
3012 /* "Normal" viewport transformation (not clipped)
3013 * 1) The values are divided by rhw
3014 * 2) The y axis is negative, so multiply it with -1
3015 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3016 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3017 * 4) Multiply x with Width/2 and add Width/2
3018 * 5) The same for the height
3019 * 6) Add the viewpoint X and Y to the 2D coordinates and
3020 * The minimum Z value to z
3021 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3023 * Well, basically it's simply a linear transformation into viewport
3024 * coordinates
3027 x /= rhw;
3028 y /= rhw;
3029 z /= rhw;
3031 y *= -1;
3033 x *= vp.width / 2;
3034 y *= vp.height / 2;
3035 z *= vp.max_z - vp.min_z;
3037 x += vp.width / 2 + vp.x;
3038 y += vp.height / 2 + vp.y;
3039 z += vp.min_z;
3041 rhw = 1 / rhw;
3042 } else {
3043 /* That vertex got clipped
3044 * Contrary to OpenGL it is not dropped completely, it just
3045 * undergoes a different calculation.
3047 TRACE("Vertex got clipped\n");
3048 x += rhw;
3049 y += rhw;
3051 x /= 2;
3052 y /= 2;
3054 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3055 * outside of the main vertex buffer memory. That needs some more
3056 * investigation...
3060 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3063 ( (float *) dest_ptr)[0] = x;
3064 ( (float *) dest_ptr)[1] = y;
3065 ( (float *) dest_ptr)[2] = z;
3066 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3068 dest_ptr += 3 * sizeof(float);
3070 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3071 dest_ptr += sizeof(float);
3074 if (DestFVF & WINED3DFVF_PSIZE)
3075 dest_ptr += sizeof(DWORD);
3077 if (DestFVF & WINED3DFVF_NORMAL)
3079 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3080 const float *normal = (const float *)(element->data.addr + i * element->stride);
3081 /* AFAIK this should go into the lighting information */
3082 FIXME("Didn't expect the destination to have a normal\n");
3083 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3086 if (DestFVF & WINED3DFVF_DIFFUSE)
3088 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3089 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3090 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3092 static BOOL warned = FALSE;
3094 if(!warned) {
3095 ERR("No diffuse color in source, but destination has one\n");
3096 warned = TRUE;
3099 *( (DWORD *) dest_ptr) = 0xffffffff;
3100 dest_ptr += sizeof(DWORD);
3102 else
3104 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3108 if (DestFVF & WINED3DFVF_SPECULAR)
3110 /* What's the color value in the feedback buffer? */
3111 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3112 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3113 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3115 static BOOL warned = FALSE;
3117 if(!warned) {
3118 ERR("No specular color in source, but destination has one\n");
3119 warned = TRUE;
3122 *(DWORD *)dest_ptr = 0xff000000;
3123 dest_ptr += sizeof(DWORD);
3125 else
3127 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3131 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3133 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3134 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3135 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3137 ERR("No source texture, but destination requests one\n");
3138 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3140 else
3142 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3147 wined3d_resource_unmap(&dest->resource, 0);
3149 return WINED3D_OK;
3151 #undef copy_and_next
3153 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3154 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3155 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3157 struct wined3d_state *state = &device->state;
3158 struct wined3d_stream_info stream_info;
3159 const struct wined3d_gl_info *gl_info;
3160 struct wined3d_context *context;
3161 struct wined3d_shader *vs;
3162 unsigned int i;
3163 HRESULT hr;
3164 WORD map;
3166 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3167 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3168 device, src_start_idx, dst_idx, vertex_count,
3169 dst_buffer, declaration, flags, dst_fvf);
3171 if (declaration)
3172 FIXME("Output vertex declaration not implemented yet.\n");
3174 /* Need any context to write to the vbo. */
3175 context = context_acquire(device, NULL);
3176 gl_info = context->gl_info;
3178 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3179 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3180 context_stream_info_from_declaration(context, state, &stream_info);
3181 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3183 /* We can't convert FROM a VBO, and vertex buffers used to source into
3184 * process_vertices() are unlikely to ever be used for drawing. Release
3185 * VBOs in those buffers and fix up the stream_info structure.
3187 * Also apply the start index. */
3188 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3190 struct wined3d_stream_info_element *e;
3191 struct wined3d_buffer *buffer;
3193 if (!(map & 1))
3194 continue;
3196 e = &stream_info.elements[i];
3197 buffer = state->streams[e->stream_idx].buffer;
3198 e->data.buffer_object = 0;
3199 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3200 if (buffer->buffer_object)
3202 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3203 buffer->buffer_object = 0;
3205 if (e->data.addr)
3206 e->data.addr += e->stride * src_start_idx;
3209 hr = process_vertices_strided(device, dst_idx, vertex_count,
3210 &stream_info, dst_buffer, flags, dst_fvf);
3212 context_release(context);
3214 return hr;
3217 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3218 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3220 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3221 DWORD old_value;
3223 TRACE("device %p, stage %u, state %s, value %#x.\n",
3224 device, stage, debug_d3dtexturestate(state), value);
3226 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3228 WARN("Invalid state %#x passed.\n", state);
3229 return;
3232 if (stage >= d3d_info->limits.ffp_blend_stages)
3234 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3235 stage, d3d_info->limits.ffp_blend_stages - 1);
3236 return;
3239 old_value = device->update_state->texture_states[stage][state];
3240 device->update_state->texture_states[stage][state] = value;
3242 if (device->recording)
3244 TRACE("Recording... not performing anything.\n");
3245 device->recording->changed.textureState[stage] |= 1u << state;
3246 return;
3249 /* Checked after the assignments to allow proper stateblock recording. */
3250 if (old_value == value)
3252 TRACE("Application is setting the old value over, nothing to do.\n");
3253 return;
3256 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3259 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3260 UINT stage, enum wined3d_texture_stage_state state)
3262 TRACE("device %p, stage %u, state %s.\n",
3263 device, stage, debug_d3dtexturestate(state));
3265 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3267 WARN("Invalid state %#x passed.\n", state);
3268 return 0;
3271 return device->state.texture_states[stage][state];
3274 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3275 UINT stage, struct wined3d_texture *texture)
3277 struct wined3d_texture *prev;
3279 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3281 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3282 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3284 /* Windows accepts overflowing this array... we do not. */
3285 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3287 WARN("Ignoring invalid stage %u.\n", stage);
3288 return WINED3D_OK;
3291 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3293 WARN("Rejecting attempt to set scratch texture.\n");
3294 return WINED3DERR_INVALIDCALL;
3297 if (device->recording)
3298 device->recording->changed.textures |= 1u << stage;
3300 prev = device->update_state->textures[stage];
3301 TRACE("Previous texture %p.\n", prev);
3303 if (texture == prev)
3305 TRACE("App is setting the same texture again, nothing to do.\n");
3306 return WINED3D_OK;
3309 TRACE("Setting new texture to %p.\n", texture);
3310 device->update_state->textures[stage] = texture;
3312 if (texture)
3313 wined3d_texture_incref(texture);
3314 if (!device->recording)
3315 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3316 if (prev)
3317 wined3d_texture_decref(prev);
3319 return WINED3D_OK;
3322 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3324 TRACE("device %p, stage %u.\n", device, stage);
3326 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3327 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3329 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3331 WARN("Ignoring invalid stage %u.\n", stage);
3332 return NULL; /* Windows accepts overflowing this array ... we do not. */
3335 return device->state.textures[stage];
3338 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3340 TRACE("device %p, caps %p.\n", device, caps);
3342 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3343 device->create_parms.device_type, caps);
3346 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3347 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3349 struct wined3d_swapchain *swapchain;
3351 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3352 device, swapchain_idx, mode, rotation);
3354 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3355 return WINED3DERR_INVALIDCALL;
3357 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3360 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3362 struct wined3d_stateblock *stateblock;
3363 HRESULT hr;
3365 TRACE("device %p.\n", device);
3367 if (device->recording)
3368 return WINED3DERR_INVALIDCALL;
3370 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3371 if (FAILED(hr))
3372 return hr;
3374 device->recording = stateblock;
3375 device->update_state = &stateblock->state;
3377 TRACE("Recording stateblock %p.\n", stateblock);
3379 return WINED3D_OK;
3382 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3383 struct wined3d_stateblock **stateblock)
3385 struct wined3d_stateblock *object = device->recording;
3387 TRACE("device %p, stateblock %p.\n", device, stateblock);
3389 if (!device->recording)
3391 WARN("Not recording.\n");
3392 *stateblock = NULL;
3393 return WINED3DERR_INVALIDCALL;
3396 stateblock_init_contained_states(object);
3398 *stateblock = object;
3399 device->recording = NULL;
3400 device->update_state = &device->state;
3402 TRACE("Returning stateblock %p.\n", *stateblock);
3404 return WINED3D_OK;
3407 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3409 /* At the moment we have no need for any functionality at the beginning
3410 * of a scene. */
3411 TRACE("device %p.\n", device);
3413 if (device->inScene)
3415 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3416 return WINED3DERR_INVALIDCALL;
3418 device->inScene = TRUE;
3419 return WINED3D_OK;
3422 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3424 struct wined3d_context *context;
3426 TRACE("device %p.\n", device);
3428 if (!device->inScene)
3430 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3431 return WINED3DERR_INVALIDCALL;
3434 context = context_acquire(device, NULL);
3435 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3436 context->gl_info->gl_ops.gl.p_glFlush();
3437 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3438 * fails. */
3439 context_release(context);
3441 device->inScene = FALSE;
3442 return WINED3D_OK;
3445 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3446 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3448 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3449 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3451 if (!rect_count && rects)
3453 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3454 return WINED3D_OK;
3457 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3459 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3460 if (!ds)
3462 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3463 /* TODO: What about depth stencil buffers without stencil bits? */
3464 return WINED3DERR_INVALIDCALL;
3466 else if (flags & WINED3DCLEAR_TARGET)
3468 if (ds->width < device->fb.render_targets[0]->width
3469 || ds->height < device->fb.render_targets[0]->height)
3471 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3472 return WINED3D_OK;
3477 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3479 return WINED3D_OK;
3482 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3483 struct wined3d_query *predicate, BOOL value)
3485 struct wined3d_query *prev;
3487 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3489 prev = device->update_state->predicate;
3490 if (predicate)
3492 FIXME("Predicated rendering not implemented.\n");
3493 wined3d_query_incref(predicate);
3495 device->update_state->predicate = predicate;
3496 device->update_state->predicate_value = value;
3497 if (!device->recording)
3498 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3499 if (prev)
3500 wined3d_query_decref(prev);
3503 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3505 TRACE("device %p, value %p.\n", device, value);
3507 *value = device->state.predicate_value;
3508 return device->state.predicate;
3511 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3512 enum wined3d_primitive_type primitive_type)
3514 GLenum gl_primitive_type, prev;
3516 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3518 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3519 prev = device->update_state->gl_primitive_type;
3520 device->update_state->gl_primitive_type = gl_primitive_type;
3521 if (device->recording)
3522 device->recording->changed.primitive_type = TRUE;
3523 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3524 device_invalidate_state(device, STATE_POINT_ENABLE);
3527 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3528 enum wined3d_primitive_type *primitive_type)
3530 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3532 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3534 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3537 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3539 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3541 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, 0, 0, FALSE);
3543 return WINED3D_OK;
3546 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3547 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3549 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3550 device, start_vertex, vertex_count, start_instance, instance_count);
3552 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3555 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3557 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3559 if (!device->state.index_buffer)
3561 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3562 * without an index buffer set. (The first time at least...)
3563 * D3D8 simply dies, but I doubt it can do much harm to return
3564 * D3DERR_INVALIDCALL there as well. */
3565 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3566 return WINED3DERR_INVALIDCALL;
3569 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3571 return WINED3D_OK;
3574 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3575 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3577 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3578 device, start_idx, index_count, start_instance, instance_count);
3580 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index,
3581 start_idx, index_count, start_instance, instance_count, TRUE);
3584 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3585 struct wined3d_texture *src_texture, unsigned int src_level,
3586 struct wined3d_texture *dst_texture, unsigned int level_count)
3588 struct wined3d_const_bo_address data;
3589 struct wined3d_context *context;
3590 struct wined3d_map_desc src;
3591 HRESULT hr = WINED3D_OK;
3592 unsigned int i;
3594 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3595 device, src_texture, src_level, dst_texture, level_count);
3597 if (src_texture->resource.format != dst_texture->resource.format)
3599 WARN("Source and destination formats do not match.\n");
3600 return WINED3DERR_INVALIDCALL;
3603 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3604 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3605 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3607 WARN("Source and destination dimensions do not match.\n");
3608 return WINED3DERR_INVALIDCALL;
3611 context = context_acquire(device, NULL);
3613 /* Only a prepare, since we're uploading entire volumes. */
3614 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3615 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3617 for (i = 0; i < level_count; ++i)
3619 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3620 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3621 goto done;
3623 data.buffer_object = 0;
3624 data.addr = src.data;
3625 wined3d_texture_upload_data(dst_texture, i, context, &data, src.row_pitch, src.slice_pitch);
3626 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3628 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3629 goto done;
3632 done:
3633 context_release(context);
3634 return hr;
3637 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3638 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3640 unsigned int src_size, dst_size, src_skip_levels = 0;
3641 unsigned int layer_count, level_count, i, j;
3642 enum wined3d_resource_type type;
3643 HRESULT hr;
3644 struct wined3d_context *context;
3646 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3648 /* Verify that the source and destination textures are non-NULL. */
3649 if (!src_texture || !dst_texture)
3651 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3652 return WINED3DERR_INVALIDCALL;
3655 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3657 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3658 return WINED3DERR_INVALIDCALL;
3660 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3662 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3663 return WINED3DERR_INVALIDCALL;
3666 /* Verify that the source and destination textures are the same type. */
3667 type = src_texture->resource.type;
3668 if (dst_texture->resource.type != type)
3670 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3671 return WINED3DERR_INVALIDCALL;
3674 layer_count = src_texture->layer_count;
3675 if (layer_count != dst_texture->layer_count)
3677 WARN("Source and destination have different layer counts.\n");
3678 return WINED3DERR_INVALIDCALL;
3681 level_count = min(wined3d_texture_get_level_count(src_texture),
3682 wined3d_texture_get_level_count(dst_texture));
3684 src_size = max(src_texture->resource.width, src_texture->resource.height);
3685 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3686 if (type == WINED3D_RTYPE_TEXTURE_3D)
3688 src_size = max(src_size, src_texture->resource.depth);
3689 dst_size = max(dst_size, dst_texture->resource.depth);
3691 while (src_size > dst_size)
3693 src_size >>= 1;
3694 ++src_skip_levels;
3697 /* Make sure that the destination texture is loaded. */
3698 context = context_acquire(device, NULL);
3699 wined3d_texture_load(dst_texture, context, FALSE);
3700 context_release(context);
3702 /* Update every surface level of the texture. */
3703 switch (type)
3705 case WINED3D_RTYPE_TEXTURE_2D:
3707 unsigned int src_levels = src_texture->level_count;
3708 unsigned int dst_levels = dst_texture->level_count;
3709 struct wined3d_surface *src_surface;
3710 struct wined3d_surface *dst_surface;
3712 for (i = 0; i < layer_count; ++i)
3714 for (j = 0; j < level_count; ++j)
3716 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3717 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3718 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3720 WARN("Failed to update surface, hr %#x.\n", hr);
3721 return hr;
3725 return WINED3D_OK;
3728 case WINED3D_RTYPE_TEXTURE_3D:
3729 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3730 src_texture, src_skip_levels, dst_texture, level_count)))
3731 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3732 return hr;
3734 default:
3735 FIXME("Unsupported texture type %#x.\n", type);
3736 return WINED3DERR_INVALIDCALL;
3740 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3742 const struct wined3d_state *state = &device->state;
3743 struct wined3d_texture *texture;
3744 DWORD i;
3746 TRACE("device %p, num_passes %p.\n", device, num_passes);
3748 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3750 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3752 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3753 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3755 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3757 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3758 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3761 texture = state->textures[i];
3762 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3764 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3766 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3767 return E_FAIL;
3769 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3771 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3772 return E_FAIL;
3774 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3775 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3777 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3778 return E_FAIL;
3782 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3783 || state->render_states[WINED3D_RS_STENCILENABLE])
3785 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3786 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3788 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3790 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3791 return WINED3DERR_CONFLICTINGRENDERSTATE;
3795 /* return a sensible default */
3796 *num_passes = 1;
3798 TRACE("returning D3D_OK\n");
3799 return WINED3D_OK;
3802 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3804 static BOOL warned;
3806 TRACE("device %p, software %#x.\n", device, software);
3808 if (!warned)
3810 FIXME("device %p, software %#x stub!\n", device, software);
3811 warned = TRUE;
3814 device->softwareVertexProcessing = software;
3817 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3819 static BOOL warned;
3821 TRACE("device %p.\n", device);
3823 if (!warned)
3825 TRACE("device %p stub!\n", device);
3826 warned = TRUE;
3829 return device->softwareVertexProcessing;
3832 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3833 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3835 struct wined3d_swapchain *swapchain;
3837 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3838 device, swapchain_idx, raster_status);
3840 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3841 return WINED3DERR_INVALIDCALL;
3843 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3846 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3848 static BOOL warned;
3850 TRACE("device %p, segments %.8e.\n", device, segments);
3852 if (segments != 0.0f)
3854 if (!warned)
3856 FIXME("device %p, segments %.8e stub!\n", device, segments);
3857 warned = TRUE;
3861 return WINED3D_OK;
3864 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3866 static BOOL warned;
3868 TRACE("device %p.\n", device);
3870 if (!warned)
3872 FIXME("device %p stub!\n", device);
3873 warned = TRUE;
3876 return 0.0f;
3879 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3880 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3882 struct wined3d_texture *dst_texture, *src_texture;
3883 RECT dst_rect, src_rect;
3884 unsigned int i, j;
3885 HRESULT hr;
3887 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3889 if (src_resource == dst_resource)
3891 WARN("Source and destination are the same resource.\n");
3892 return;
3895 if (src_resource->type != dst_resource->type)
3897 WARN("Resource types (%s / %s) don't match.\n",
3898 debug_d3dresourcetype(dst_resource->type),
3899 debug_d3dresourcetype(src_resource->type));
3900 return;
3903 if (src_resource->width != dst_resource->width
3904 || src_resource->height != dst_resource->height
3905 || src_resource->depth != dst_resource->depth)
3907 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3908 dst_resource->width, dst_resource->height, dst_resource->depth,
3909 src_resource->width, src_resource->height, src_resource->depth);
3910 return;
3913 if (src_resource->format->id != dst_resource->format->id)
3915 WARN("Resource formats (%s / %s) don't match.\n",
3916 debug_d3dformat(dst_resource->format->id),
3917 debug_d3dformat(src_resource->format->id));
3918 return;
3921 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3923 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3924 buffer_from_resource(src_resource), 0,
3925 dst_resource->size)))
3926 ERR("Failed to copy buffer, hr %#x.\n", hr);
3927 return;
3930 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3932 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3933 return;
3936 dst_texture = texture_from_resource(dst_resource);
3937 src_texture = texture_from_resource(src_resource);
3939 if (src_texture->layer_count != dst_texture->layer_count
3940 || src_texture->level_count != dst_texture->level_count)
3942 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3943 dst_texture->layer_count, dst_texture->level_count,
3944 src_texture->layer_count, src_texture->level_count);
3945 return;
3948 for (i = 0; i < dst_texture->level_count; ++i)
3950 SetRect(&dst_rect, 0, 0,
3951 wined3d_texture_get_level_width(dst_texture, i),
3952 wined3d_texture_get_level_height(dst_texture, i));
3953 SetRect(&src_rect, 0, 0,
3954 wined3d_texture_get_level_width(src_texture, i),
3955 wined3d_texture_get_level_height(dst_texture, i));
3956 for (j = 0; j < dst_texture->layer_count; ++j)
3958 unsigned int idx = j * dst_texture->level_count + i;
3960 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3961 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3962 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3967 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3968 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3969 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3970 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3972 struct wined3d_texture *dst_texture, *src_texture;
3973 RECT dst_rect, src_rect;
3974 HRESULT hr;
3976 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3977 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3978 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3979 src_resource, src_sub_resource_idx, debug_box(src_box));
3981 if (src_box && (src_box->left >= src_box->right
3982 || src_box->top >= src_box->bottom
3983 || src_box->front >= src_box->back))
3985 WARN("Invalid box %s specified.\n", debug_box(src_box));
3986 return WINED3DERR_INVALIDCALL;
3989 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3991 WARN("Source and destination are the same sub-resource.\n");
3992 return WINED3DERR_INVALIDCALL;
3995 if (src_resource->type != dst_resource->type)
3997 WARN("Resource types (%s / %s) don't match.\n",
3998 debug_d3dresourcetype(dst_resource->type),
3999 debug_d3dresourcetype(src_resource->type));
4000 return WINED3DERR_INVALIDCALL;
4003 if (src_resource->format->id != dst_resource->format->id)
4005 WARN("Resource formats (%s / %s) don't match.\n",
4006 debug_d3dformat(dst_resource->format->id),
4007 debug_d3dformat(src_resource->format->id));
4008 return WINED3DERR_INVALIDCALL;
4011 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4013 unsigned int src_offset, size;
4015 if (dst_sub_resource_idx)
4017 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4018 return WINED3DERR_INVALIDCALL;
4020 if (src_sub_resource_idx)
4022 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4023 return WINED3DERR_INVALIDCALL;
4026 if (src_box)
4028 src_offset = src_box->left;
4029 size = src_box->right - src_box->left;
4031 else
4033 src_offset = 0;
4034 size = src_resource->size;
4037 if (src_offset > src_resource->size
4038 || size > src_resource->size - src_offset
4039 || dst_x > dst_resource->size
4040 || size > dst_resource->size - dst_x)
4042 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4043 dst_x, src_offset, size);
4044 return WINED3DERR_INVALIDCALL;
4047 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4048 buffer_from_resource(src_resource), src_offset, size);
4051 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4053 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4054 return WINED3DERR_INVALIDCALL;
4057 dst_texture = texture_from_resource(dst_resource);
4058 src_texture = texture_from_resource(src_resource);
4060 if (src_box)
4062 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4064 else
4066 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4068 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4069 wined3d_texture_get_level_height(src_texture, level));
4072 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4073 dst_y + (src_rect.bottom - src_rect.top));
4075 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4076 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4077 WARN("Failed to blit, hr %#x.\n", hr);
4079 return hr;
4082 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4083 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4084 unsigned int depth_pitch)
4086 struct wined3d_texture_sub_resource *sub_resource;
4087 const struct wined3d_gl_info *gl_info;
4088 struct wined3d_const_bo_address addr;
4089 unsigned int width, height, level;
4090 struct wined3d_context *context;
4091 struct wined3d_texture *texture;
4092 struct wined3d_surface *surface;
4093 POINT dst_point;
4094 RECT src_rect;
4096 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4097 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4099 if (resource->type == WINED3D_RTYPE_BUFFER)
4101 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4102 HRESULT hr;
4104 if (sub_resource_idx > 0)
4106 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4107 return;
4110 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4111 WARN("Failed to update buffer data, hr %#x.\n", hr);
4113 return;
4116 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4118 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4119 return;
4122 texture = texture_from_resource(resource);
4123 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4125 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4126 return;
4128 surface = sub_resource->u.surface;
4130 level = sub_resource_idx % texture->level_count;
4131 width = wined3d_texture_get_level_width(texture, level);
4132 height = wined3d_texture_get_level_height(texture, level);
4134 src_rect.left = 0;
4135 src_rect.top = 0;
4136 if (box)
4138 if (box->left >= box->right || box->right > width
4139 || box->top >= box->bottom || box->bottom > height
4140 || box->front >= box->back)
4142 WARN("Invalid box %s specified.\n", debug_box(box));
4143 return;
4146 src_rect.right = box->right - box->left;
4147 src_rect.bottom = box->bottom - box->top;
4148 dst_point.x = box->left;
4149 dst_point.y = box->top;
4151 else
4153 src_rect.right = width;
4154 src_rect.bottom = height;
4155 dst_point.x = 0;
4156 dst_point.y = 0;
4159 addr.buffer_object = 0;
4160 addr.addr = data;
4162 context = context_acquire(resource->device, NULL);
4163 gl_info = context->gl_info;
4165 /* Only load the surface for partial updates. */
4166 if (!dst_point.x && !dst_point.y && src_rect.right == width && src_rect.bottom == height)
4167 wined3d_texture_prepare_texture(texture, context, FALSE);
4168 else
4169 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4170 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4172 wined3d_surface_upload_data(surface, gl_info, resource->format,
4173 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4175 context_release(context);
4177 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4178 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4181 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4182 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4183 const struct wined3d_color *color, float depth, DWORD stencil)
4185 const struct blit_shader *blitter;
4186 struct wined3d_resource *resource;
4187 enum wined3d_blit_op blit_op;
4188 RECT r;
4190 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4191 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4193 if (!flags)
4194 return WINED3D_OK;
4196 resource = view->resource;
4197 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4199 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4200 return WINED3DERR_INVALIDCALL;
4203 if (view->depth > 1)
4205 FIXME("Layered clears not implemented.\n");
4206 return WINED3DERR_INVALIDCALL;
4209 if (!rect)
4211 SetRect(&r, 0, 0, view->width, view->height);
4212 rect = &r;
4215 if (flags & WINED3DCLEAR_TARGET)
4216 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4217 else
4218 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4220 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4221 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4223 FIXME("No blitter is capable of performing the requested fill operation.\n");
4224 return WINED3DERR_INVALIDCALL;
4227 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4228 return blitter->color_fill(device, view, rect, color);
4229 else
4230 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4233 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4234 unsigned int view_idx)
4236 TRACE("device %p, view_idx %u.\n", device, view_idx);
4238 if (view_idx >= device->adapter->gl_info.limits.buffers)
4240 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4241 return NULL;
4244 return device->fb.render_targets[view_idx];
4247 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4249 TRACE("device %p.\n", device);
4251 return device->fb.depth_stencil;
4254 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4255 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4257 struct wined3d_rendertarget_view *prev;
4259 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4260 device, view_idx, view, set_viewport);
4262 if (view_idx >= device->adapter->gl_info.limits.buffers)
4264 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4265 return WINED3DERR_INVALIDCALL;
4268 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4270 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4271 return WINED3DERR_INVALIDCALL;
4274 /* Set the viewport and scissor rectangles, if requested. Tests show that
4275 * stateblock recording is ignored, the change goes directly into the
4276 * primary stateblock. */
4277 if (!view_idx && set_viewport)
4279 struct wined3d_state *state = &device->state;
4281 state->viewport.x = 0;
4282 state->viewport.y = 0;
4283 state->viewport.width = view->width;
4284 state->viewport.height = view->height;
4285 state->viewport.min_z = 0.0f;
4286 state->viewport.max_z = 1.0f;
4287 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4289 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4290 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4294 prev = device->fb.render_targets[view_idx];
4295 if (view == prev)
4296 return WINED3D_OK;
4298 if (view)
4299 wined3d_rendertarget_view_incref(view);
4300 device->fb.render_targets[view_idx] = view;
4301 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4302 /* Release after the assignment, to prevent device_resource_released()
4303 * from seeing the surface as still in use. */
4304 if (prev)
4305 wined3d_rendertarget_view_decref(prev);
4307 return WINED3D_OK;
4310 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4312 struct wined3d_rendertarget_view *prev;
4314 TRACE("device %p, view %p.\n", device, view);
4316 prev = device->fb.depth_stencil;
4317 if (prev == view)
4319 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4320 return;
4323 if ((device->fb.depth_stencil = view))
4324 wined3d_rendertarget_view_incref(view);
4325 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4326 if (prev)
4327 wined3d_rendertarget_view_decref(prev);
4330 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4331 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4333 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4334 struct wined3d_sub_resource_data data;
4335 struct wined3d_resource_desc desc;
4336 struct wined3d_map_desc map_desc;
4337 struct wined3d_texture *texture;
4338 HRESULT hr;
4340 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4342 ERR("Failed to map source texture.\n");
4343 return NULL;
4346 data.data = map_desc.data;
4347 data.row_pitch = map_desc.row_pitch;
4348 data.slice_pitch = map_desc.slice_pitch;
4350 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4351 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4352 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4353 desc.multisample_quality = 0;
4354 desc.usage = WINED3DUSAGE_DYNAMIC;
4355 desc.pool = WINED3D_POOL_DEFAULT;
4356 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4357 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4358 desc.depth = 1;
4359 desc.size = 0;
4361 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4362 &data, NULL, &wined3d_null_parent_ops, &texture);
4363 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4364 if (FAILED(hr))
4366 ERR("Failed to create cursor texture.\n");
4367 return NULL;
4370 return texture;
4373 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4374 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4376 unsigned int texture_level = sub_resource_idx % texture->level_count;
4377 unsigned int cursor_width, cursor_height;
4378 struct wined3d_display_mode mode;
4379 struct wined3d_map_desc map_desc;
4380 HRESULT hr;
4382 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4383 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4385 if (sub_resource_idx >= texture->level_count * texture->layer_count
4386 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4387 return WINED3DERR_INVALIDCALL;
4389 if (device->cursor_texture)
4391 wined3d_texture_decref(device->cursor_texture);
4392 device->cursor_texture = NULL;
4395 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4397 WARN("Texture %p has invalid format %s.\n",
4398 texture, debug_d3dformat(texture->resource.format->id));
4399 return WINED3DERR_INVALIDCALL;
4402 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4404 ERR("Failed to get display mode, hr %#x.\n", hr);
4405 return WINED3DERR_INVALIDCALL;
4408 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4409 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4410 if (cursor_width > mode.width || cursor_height > mode.height)
4412 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4413 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4414 return WINED3DERR_INVALIDCALL;
4417 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4419 /* Do not store the surface's pointer because the application may
4420 * release it after setting the cursor image. Windows doesn't
4421 * addref the set surface, so we can't do this either without
4422 * creating circular refcount dependencies. */
4423 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4425 ERR("Failed to create cursor texture.\n");
4426 return WINED3DERR_INVALIDCALL;
4429 if (cursor_width == 32 && cursor_height == 32)
4431 UINT mask_size = cursor_width * cursor_height / 8;
4432 ICONINFO cursor_info;
4433 DWORD *mask_bits;
4434 HCURSOR cursor;
4436 /* 32-bit user32 cursors ignore the alpha channel if it's all
4437 * zeroes, and use the mask instead. Fill the mask with all ones
4438 * to ensure we still get a fully transparent cursor. */
4439 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4440 return E_OUTOFMEMORY;
4441 memset(mask_bits, 0xff, mask_size);
4443 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4444 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4445 cursor_info.fIcon = FALSE;
4446 cursor_info.xHotspot = x_hotspot;
4447 cursor_info.yHotspot = y_hotspot;
4448 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4449 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4450 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4452 /* Create our cursor and clean up. */
4453 cursor = CreateIconIndirect(&cursor_info);
4454 if (cursor_info.hbmMask)
4455 DeleteObject(cursor_info.hbmMask);
4456 if (cursor_info.hbmColor)
4457 DeleteObject(cursor_info.hbmColor);
4458 if (device->hardwareCursor)
4459 DestroyCursor(device->hardwareCursor);
4460 device->hardwareCursor = cursor;
4461 if (device->bCursorVisible)
4462 SetCursor(cursor);
4464 HeapFree(GetProcessHeap(), 0, mask_bits);
4467 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4468 device->cursorWidth = cursor_width;
4469 device->cursorHeight = cursor_height;
4470 device->xHotSpot = x_hotspot;
4471 device->yHotSpot = y_hotspot;
4473 return WINED3D_OK;
4476 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4477 int x_screen_space, int y_screen_space, DWORD flags)
4479 TRACE("device %p, x %d, y %d, flags %#x.\n",
4480 device, x_screen_space, y_screen_space, flags);
4482 device->xScreenSpace = x_screen_space;
4483 device->yScreenSpace = y_screen_space;
4485 if (device->hardwareCursor)
4487 POINT pt;
4489 GetCursorPos( &pt );
4490 if (x_screen_space == pt.x && y_screen_space == pt.y)
4491 return;
4492 SetCursorPos( x_screen_space, y_screen_space );
4494 /* Switch to the software cursor if position diverges from the hardware one. */
4495 GetCursorPos( &pt );
4496 if (x_screen_space != pt.x || y_screen_space != pt.y)
4498 if (device->bCursorVisible) SetCursor( NULL );
4499 DestroyCursor( device->hardwareCursor );
4500 device->hardwareCursor = 0;
4505 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4507 BOOL oldVisible = device->bCursorVisible;
4509 TRACE("device %p, show %#x.\n", device, show);
4512 * When ShowCursor is first called it should make the cursor appear at the OS's last
4513 * known cursor position.
4515 if (show && !oldVisible)
4517 POINT pt;
4518 GetCursorPos(&pt);
4519 device->xScreenSpace = pt.x;
4520 device->yScreenSpace = pt.y;
4523 if (device->hardwareCursor)
4525 device->bCursorVisible = show;
4526 if (show)
4527 SetCursor(device->hardwareCursor);
4528 else
4529 SetCursor(NULL);
4531 else if (device->cursor_texture)
4533 device->bCursorVisible = show;
4536 return oldVisible;
4539 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4541 struct wined3d_resource *resource, *cursor;
4543 TRACE("device %p.\n", device);
4545 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4547 TRACE("Checking resource %p for eviction.\n", resource);
4549 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4551 TRACE("Evicting %p.\n", resource);
4552 wined3d_cs_emit_unload_resource(device->cs, resource);
4557 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4559 struct wined3d_resource *resource, *cursor;
4560 const struct wined3d_gl_info *gl_info;
4561 struct wined3d_context *context;
4562 struct wined3d_shader *shader;
4564 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4566 TRACE("Unloading resource %p.\n", resource);
4567 wined3d_cs_emit_unload_resource(device->cs, resource);
4570 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4572 device->shader_backend->shader_destroy(shader);
4575 context = context_acquire(device, NULL);
4576 gl_info = context->gl_info;
4578 if (device->depth_blt_texture)
4580 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4581 device->depth_blt_texture = 0;
4584 device->blitter->free_private(device);
4585 device->shader_backend->shader_free_private(device);
4586 destroy_dummy_textures(device, gl_info);
4587 destroy_default_samplers(device);
4589 context_release(context);
4591 while (device->context_count)
4593 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4596 HeapFree(GetProcessHeap(), 0, swapchain->context);
4597 swapchain->context = NULL;
4600 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4602 struct wined3d_context *context;
4603 struct wined3d_texture *target;
4604 HRESULT hr;
4606 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4607 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4609 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4610 return hr;
4613 if (FAILED(hr = device->blitter->alloc_private(device)))
4615 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4616 device->shader_backend->shader_free_private(device);
4617 return hr;
4620 /* Recreate the primary swapchain's context */
4621 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4622 if (!swapchain->context)
4624 ERR("Failed to allocate memory for swapchain context array.\n");
4625 device->blitter->free_private(device);
4626 device->shader_backend->shader_free_private(device);
4627 return E_OUTOFMEMORY;
4630 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4631 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4633 WARN("Failed to create context.\n");
4634 device->blitter->free_private(device);
4635 device->shader_backend->shader_free_private(device);
4636 HeapFree(GetProcessHeap(), 0, swapchain->context);
4637 return E_FAIL;
4640 swapchain->context[0] = context;
4641 swapchain->num_contexts = 1;
4642 create_dummy_textures(device, context);
4643 create_default_samplers(device);
4644 context_release(context);
4646 return WINED3D_OK;
4649 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4650 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4651 wined3d_device_reset_cb callback, BOOL reset_state)
4653 struct wined3d_rendertarget_view_desc view_desc;
4654 struct wined3d_resource *resource, *cursor;
4655 struct wined3d_swapchain *swapchain;
4656 BOOL backbuffer_resized;
4657 HRESULT hr = WINED3D_OK;
4658 unsigned int i;
4660 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4661 device, swapchain_desc, mode, callback, reset_state);
4663 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4665 ERR("Failed to get the first implicit swapchain.\n");
4666 return WINED3DERR_INVALIDCALL;
4669 if (reset_state)
4671 if (device->logo_texture)
4673 wined3d_texture_decref(device->logo_texture);
4674 device->logo_texture = NULL;
4676 if (device->cursor_texture)
4678 wined3d_texture_decref(device->cursor_texture);
4679 device->cursor_texture = NULL;
4681 state_unbind_resources(&device->state);
4684 if (device->fb.render_targets)
4686 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4688 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4691 wined3d_device_set_depth_stencil_view(device, NULL);
4693 if (device->onscreen_depth_stencil)
4695 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4696 device->onscreen_depth_stencil = NULL;
4699 if (reset_state)
4701 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4703 TRACE("Enumerating resource %p.\n", resource);
4704 if (FAILED(hr = callback(resource)))
4705 return hr;
4709 TRACE("New params:\n");
4710 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4711 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4712 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4713 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4714 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4715 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4716 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4717 TRACE("device_window %p\n", swapchain_desc->device_window);
4718 TRACE("windowed %#x\n", swapchain_desc->windowed);
4719 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4720 if (swapchain_desc->enable_auto_depth_stencil)
4721 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4722 TRACE("flags %#x\n", swapchain_desc->flags);
4723 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4724 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4725 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4727 /* No special treatment of these parameters. Just store them */
4728 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4729 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4730 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4731 swapchain->desc.flags = swapchain_desc->flags;
4732 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4733 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4734 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4736 if (swapchain_desc->device_window
4737 && swapchain_desc->device_window != swapchain->desc.device_window)
4739 TRACE("Changing the device window from %p to %p.\n",
4740 swapchain->desc.device_window, swapchain_desc->device_window);
4741 swapchain->desc.device_window = swapchain_desc->device_window;
4742 swapchain->device_window = swapchain_desc->device_window;
4743 wined3d_swapchain_set_window(swapchain, NULL);
4746 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4747 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4749 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4750 || swapchain->reapply_mode || mode
4751 || (!swapchain_desc->windowed && backbuffer_resized))
4753 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4754 return hr;
4756 else if (!swapchain_desc->windowed)
4758 DWORD style = device->style;
4759 DWORD exStyle = device->exStyle;
4760 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4761 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4762 * Reset to clear up their mess. Guild Wars also loses the device during that.
4764 device->style = 0;
4765 device->exStyle = 0;
4766 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4767 swapchain_desc->backbuffer_width,
4768 swapchain_desc->backbuffer_height);
4769 device->style = style;
4770 device->exStyle = exStyle;
4773 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4774 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4775 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4776 return hr;
4778 if (device->auto_depth_stencil_view)
4780 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4781 device->auto_depth_stencil_view = NULL;
4783 if (swapchain->desc.enable_auto_depth_stencil)
4785 struct wined3d_resource_desc texture_desc;
4786 struct wined3d_texture *texture;
4788 TRACE("Creating the depth stencil buffer.\n");
4790 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4791 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4792 texture_desc.multisample_type = swapchain->desc.multisample_type;
4793 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4794 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4795 texture_desc.pool = WINED3D_POOL_DEFAULT;
4796 texture_desc.width = swapchain->desc.backbuffer_width;
4797 texture_desc.height = swapchain->desc.backbuffer_height;
4798 texture_desc.depth = 1;
4799 texture_desc.size = 0;
4801 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4802 device->device_parent, &texture_desc, &texture)))
4804 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4805 return WINED3DERR_INVALIDCALL;
4808 view_desc.format_id = texture->resource.format->id;
4809 view_desc.u.texture.level_idx = 0;
4810 view_desc.u.texture.layer_idx = 0;
4811 view_desc.u.texture.layer_count = 1;
4812 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4813 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4814 wined3d_texture_decref(texture);
4815 if (FAILED(hr))
4817 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4818 return hr;
4821 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4824 if (device->back_buffer_view)
4826 wined3d_rendertarget_view_decref(device->back_buffer_view);
4827 device->back_buffer_view = NULL;
4829 if (swapchain->desc.backbuffer_count)
4831 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4833 view_desc.format_id = back_buffer->format->id;
4834 view_desc.u.texture.level_idx = 0;
4835 view_desc.u.texture.layer_idx = 0;
4836 view_desc.u.texture.layer_count = 1;
4837 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4838 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4840 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4841 return hr;
4845 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4847 if (reset_state)
4849 TRACE("Resetting stateblock.\n");
4850 if (device->recording)
4852 wined3d_stateblock_decref(device->recording);
4853 device->recording = NULL;
4855 wined3d_cs_emit_reset_state(device->cs);
4856 state_cleanup(&device->state);
4858 if (device->d3d_initialized)
4859 delete_opengl_contexts(device, swapchain);
4861 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4862 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4863 device->update_state = &device->state;
4865 device_init_swapchain_state(device, swapchain);
4867 else if (device->back_buffer_view)
4869 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4870 struct wined3d_state *state = &device->state;
4872 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4874 /* Note the min_z / max_z is not reset. */
4875 state->viewport.x = 0;
4876 state->viewport.y = 0;
4877 state->viewport.width = view->width;
4878 state->viewport.height = view->height;
4879 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4881 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4882 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4885 if (device->d3d_initialized)
4887 if (reset_state)
4888 hr = create_primary_opengl_context(device, swapchain);
4889 swapchain_update_swap_interval(swapchain);
4892 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4893 * first use
4895 return hr;
4898 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4900 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4902 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4904 return WINED3D_OK;
4908 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4909 struct wined3d_device_creation_parameters *parameters)
4911 TRACE("device %p, parameters %p.\n", device, parameters);
4913 *parameters = device->create_parms;
4916 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4918 TRACE("device %p.\n", device);
4920 return device->wined3d;
4923 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4924 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4926 struct wined3d_swapchain *swapchain;
4928 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4929 device, swapchain_idx, flags, ramp);
4931 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4932 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4935 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4936 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4938 struct wined3d_swapchain *swapchain;
4940 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4941 device, swapchain_idx, ramp);
4943 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4944 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4947 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4949 TRACE("device %p, resource %p.\n", device, resource);
4951 list_add_head(&device->resources, &resource->resource_list_entry);
4954 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4956 TRACE("device %p, resource %p.\n", device, resource);
4958 list_remove(&resource->resource_list_entry);
4961 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4963 enum wined3d_resource_type type = resource->type;
4964 struct wined3d_rendertarget_view *rtv;
4965 unsigned int i;
4967 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4969 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4971 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4972 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4975 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4976 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4978 switch (type)
4980 case WINED3D_RTYPE_TEXTURE_2D:
4981 case WINED3D_RTYPE_TEXTURE_3D:
4982 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4984 struct wined3d_texture *texture = texture_from_resource(resource);
4986 if (device->state.textures[i] == texture)
4988 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4989 device->state.textures[i] = NULL;
4992 if (device->recording && device->update_state->textures[i] == texture)
4994 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4995 texture, device->recording, i);
4996 device->update_state->textures[i] = NULL;
4999 break;
5001 case WINED3D_RTYPE_BUFFER:
5003 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5005 for (i = 0; i < MAX_STREAMS; ++i)
5007 if (device->state.streams[i].buffer == buffer)
5009 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
5010 device->state.streams[i].buffer = NULL;
5013 if (device->recording && device->update_state->streams[i].buffer == buffer)
5015 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5016 buffer, device->recording, i);
5017 device->update_state->streams[i].buffer = NULL;
5021 if (device->state.index_buffer == buffer)
5023 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5024 device->state.index_buffer = NULL;
5027 if (device->recording && device->update_state->index_buffer == buffer)
5029 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5030 buffer, device->recording);
5031 device->update_state->index_buffer = NULL;
5034 break;
5036 default:
5037 break;
5040 /* Remove the resource from the resourceStore */
5041 device_resource_remove(device, resource);
5043 TRACE("Resource released.\n");
5046 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5048 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5050 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5053 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5054 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5055 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5057 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5058 const struct fragment_pipeline *fragment_pipeline;
5059 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5060 unsigned int i;
5061 HRESULT hr;
5063 device->ref = 1;
5064 device->wined3d = wined3d;
5065 wined3d_incref(device->wined3d);
5066 device->adapter = wined3d->adapter_count ? adapter : NULL;
5067 device->device_parent = device_parent;
5068 list_init(&device->resources);
5069 list_init(&device->shaders);
5070 device->surface_alignment = surface_alignment;
5072 /* Save the creation parameters. */
5073 device->create_parms.adapter_idx = adapter_idx;
5074 device->create_parms.device_type = device_type;
5075 device->create_parms.focus_window = focus_window;
5076 device->create_parms.flags = flags;
5078 device->shader_backend = adapter->shader_backend;
5080 vertex_pipeline = adapter->vertex_pipe;
5082 fragment_pipeline = adapter->fragment_pipe;
5084 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5086 if (vertex_pipeline->vp_states && fragment_pipeline->states
5087 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5088 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5089 fragment_pipeline, misc_state_template)))
5091 ERR("Failed to compile state table, hr %#x.\n", hr);
5092 wine_rb_destroy(&device->samplers, NULL, NULL);
5093 wined3d_decref(device->wined3d);
5094 return hr;
5097 device->blitter = adapter->blitter;
5099 state_init(&device->state, &device->fb, &adapter->gl_info,
5100 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5101 device->update_state = &device->state;
5103 if (!(device->cs = wined3d_cs_create(device)))
5105 WARN("Failed to create command stream.\n");
5106 state_cleanup(&device->state);
5107 hr = E_FAIL;
5108 goto err;
5111 return WINED3D_OK;
5113 err:
5114 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5116 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5118 wine_rb_destroy(&device->samplers, NULL, NULL);
5119 wined3d_decref(device->wined3d);
5120 return hr;
5124 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5126 DWORD rep = device->StateTable[state].representative;
5127 struct wined3d_context *context;
5128 DWORD idx;
5129 BYTE shift;
5130 UINT i;
5132 for (i = 0; i < device->context_count; ++i)
5134 context = device->contexts[i];
5135 if(isStateDirty(context, rep)) continue;
5137 context->dirtyArray[context->numDirtyEntries++] = rep;
5138 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5139 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5140 context->isStateDirty[idx] |= (1u << shift);
5144 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5145 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5147 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5149 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5150 window, message, wparam, lparam);
5151 if (unicode)
5152 return DefWindowProcW(window, message, wparam, lparam);
5153 else
5154 return DefWindowProcA(window, message, wparam, lparam);
5157 if (message == WM_DESTROY)
5159 TRACE("unregister window %p.\n", window);
5160 wined3d_unregister_window(window);
5162 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5163 ERR("Window %p is not the focus window for device %p.\n", window, device);
5165 else if (message == WM_DISPLAYCHANGE)
5167 device->device_parent->ops->mode_changed(device->device_parent);
5169 else if (message == WM_ACTIVATEAPP)
5171 UINT i;
5173 for (i = 0; i < device->swapchain_count; i++)
5174 wined3d_swapchain_activate(device->swapchains[i], wparam);
5176 device->device_parent->ops->activate(device->device_parent, wparam);
5178 else if (message == WM_SYSCOMMAND)
5180 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5182 if (unicode)
5183 DefWindowProcW(window, message, wparam, lparam);
5184 else
5185 DefWindowProcA(window, message, wparam, lparam);
5189 if (unicode)
5190 return CallWindowProcW(proc, window, message, wparam, lparam);
5191 else
5192 return CallWindowProcA(proc, window, message, wparam, lparam);