ntdll/tests: Added more registry symlink tests.
[wine.git] / dlls / wined3d / device.c
blob9bf6042d79952d21a74285eb766c2b4b05ce1390
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;
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 context_active_texture(context, gl_info, 0);
701 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d);
702 checkGLcall("glGenTextures");
703 TRACE("Dummy 2D texture given name %u.\n", device->dummy_textures.tex_2d);
705 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
706 checkGLcall("glBindTexture");
708 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
709 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
710 checkGLcall("glTexImage2D");
712 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
714 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_rect);
715 checkGLcall("glGenTextures");
716 TRACE("Dummy rectangle texture given name %u.\n", device->dummy_textures.tex_rect);
718 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
719 checkGLcall("glBindTexture");
721 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
722 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
723 checkGLcall("glTexImage2D");
726 if (gl_info->supported[EXT_TEXTURE3D])
728 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_3d);
729 checkGLcall("glGenTextures");
730 TRACE("Dummy 3D texture given name %u.\n", device->dummy_textures.tex_3d);
732 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
733 checkGLcall("glBindTexture");
735 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
736 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
737 checkGLcall("glTexImage3D");
740 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
742 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube);
743 checkGLcall("glGenTextures");
744 TRACE("Dummy cube texture given name %u.\n", device->dummy_textures.tex_cube);
746 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
747 checkGLcall("glBindTexture");
749 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
751 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
752 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
753 checkGLcall("glTexImage2D");
757 if (gl_info->supported[EXT_TEXTURE_ARRAY])
759 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d_array);
760 checkGLcall("glGenTextures");
761 TRACE("Dummy 2D array texture given name %u.\n", device->dummy_textures.tex_2d_array);
763 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
764 checkGLcall("glBindTexture");
766 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
767 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
768 checkGLcall("glTexImage3D");
771 context_bind_dummy_textures(device, context);
774 /* Context activation is done by the caller. */
775 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
777 const struct wined3d_gl_info *gl_info = context->gl_info;
779 if (gl_info->supported[EXT_TEXTURE_ARRAY])
780 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d_array);
782 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
783 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube);
785 if (gl_info->supported[EXT_TEXTURE3D])
786 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_3d);
788 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
789 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_rect);
791 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d);
793 checkGLcall("Delete dummy textures");
795 memset(&device->dummy_textures, 0, sizeof(device->dummy_textures));
798 /* Context activation is done by the caller. */
799 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
801 const struct wined3d_gl_info *gl_info = context->gl_info;
803 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
805 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
806 * instructions allow access to resources without using samplers.
807 * In GLSL, resources are always accessed through sampler or image variables. The default
808 * sampler object is used to emulate the direct resource access when there is no sampler state
809 * to use.
811 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
812 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
813 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
814 checkGLcall("Create default sampler");
816 /* In D3D10+, a NULL sampler maps to the default sampler state. */
817 GL_EXTCALL(glGenSamplers(1, &device->null_sampler));
818 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
819 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
820 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
821 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
822 checkGLcall("Create null sampler");
824 else
826 device->default_sampler = 0;
827 device->null_sampler = 0;
831 /* Context activation is done by the caller. */
832 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
834 const struct wined3d_gl_info *gl_info = context->gl_info;
836 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
838 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
839 GL_EXTCALL(glDeleteSamplers(1, &device->null_sampler));
840 checkGLcall("glDeleteSamplers");
843 device->default_sampler = 0;
844 device->null_sampler = 0;
847 static LONG fullscreen_style(LONG style)
849 /* Make sure the window is managed, otherwise we won't get keyboard input. */
850 style |= WS_POPUP | WS_SYSMENU;
851 style &= ~(WS_CAPTION | WS_THICKFRAME);
853 return style;
856 static LONG fullscreen_exstyle(LONG exstyle)
858 /* Filter out window decorations. */
859 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
861 return exstyle;
864 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
866 BOOL filter_messages;
867 LONG style, exstyle;
869 TRACE("Setting up window %p for fullscreen mode.\n", window);
871 if (device->style || device->exStyle)
873 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
874 window, device->style, device->exStyle);
877 device->style = GetWindowLongW(window, GWL_STYLE);
878 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
880 style = fullscreen_style(device->style);
881 exstyle = fullscreen_exstyle(device->exStyle);
883 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
884 device->style, device->exStyle, style, exstyle);
886 filter_messages = device->filter_messages;
887 device->filter_messages = TRUE;
889 SetWindowLongW(window, GWL_STYLE, style);
890 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
891 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
893 device->filter_messages = filter_messages;
896 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
897 const RECT *window_rect)
899 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
900 BOOL filter_messages;
901 LONG style, exstyle;
902 RECT rect = {0};
904 if (!device->style && !device->exStyle)
905 return;
907 style = GetWindowLongW(window, GWL_STYLE);
908 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
910 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
911 * application, and we want to ignore them in the test below, since it's
912 * not the application's fault that they changed. Additionally, we want to
913 * preserve the current status of these flags (i.e. don't restore them) to
914 * more closely emulate the behavior of Direct3D, which leaves these flags
915 * alone when returning to windowed mode. */
916 device->style ^= (device->style ^ style) & WS_VISIBLE;
917 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
919 TRACE("Restoring window style of window %p to %08x, %08x.\n",
920 window, device->style, device->exStyle);
922 filter_messages = device->filter_messages;
923 device->filter_messages = TRUE;
925 /* Only restore the style if the application didn't modify it during the
926 * fullscreen phase. Some applications change it before calling Reset()
927 * when switching between windowed and fullscreen modes (HL2), some
928 * depend on the original style (Eve Online). */
929 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
931 SetWindowLongW(window, GWL_STYLE, device->style);
932 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
935 if (window_rect)
936 rect = *window_rect;
937 else
938 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
939 SetWindowPos(window, 0, rect.left, rect.top,
940 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
942 device->filter_messages = filter_messages;
944 /* Delete the old values. */
945 device->style = 0;
946 device->exStyle = 0;
949 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
951 TRACE("device %p, window %p.\n", device, window);
953 if (!wined3d_register_window(window, device))
955 ERR("Failed to register window %p.\n", window);
956 return E_FAIL;
959 InterlockedExchangePointer((void **)&device->focus_window, window);
960 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
962 return WINED3D_OK;
965 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
967 TRACE("device %p.\n", device);
969 if (device->focus_window) wined3d_unregister_window(device->focus_window);
970 InterlockedExchangePointer((void **)&device->focus_window, NULL);
973 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
975 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
976 unsigned int i;
978 if (device->fb.render_targets)
980 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
982 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
984 if (device->back_buffer_view)
985 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
988 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
989 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
992 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
993 struct wined3d_swapchain_desc *swapchain_desc)
995 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
996 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
997 struct wined3d_swapchain *swapchain = NULL;
998 struct wined3d_context *context;
999 DWORD clear_flags = 0;
1000 HRESULT hr;
1002 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1004 if (device->d3d_initialized)
1005 return WINED3DERR_INVALIDCALL;
1006 if (device->wined3d->flags & WINED3D_NO3D)
1007 return WINED3DERR_INVALIDCALL;
1009 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
1010 return E_OUTOFMEMORY;
1012 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1013 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1015 TRACE("Shader private data couldn't be allocated\n");
1016 goto err_out;
1018 if (FAILED(hr = device->blitter->alloc_private(device)))
1020 TRACE("Blitter private data couldn't be allocated\n");
1021 goto err_out;
1024 /* Setup the implicit swapchain. This also initializes a context. */
1025 TRACE("Creating implicit swapchain\n");
1026 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1027 swapchain_desc, &swapchain);
1028 if (FAILED(hr))
1030 WARN("Failed to create implicit swapchain\n");
1031 goto err_out;
1034 if (swapchain_desc->backbuffer_count)
1036 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1037 struct wined3d_rendertarget_view_desc view_desc;
1039 view_desc.format_id = back_buffer->format->id;
1040 view_desc.u.texture.level_idx = 0;
1041 view_desc.u.texture.layer_idx = 0;
1042 view_desc.u.texture.layer_count = 1;
1043 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1044 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1046 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1047 goto err_out;
1051 device->swapchain_count = 1;
1052 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1054 ERR("Out of memory!\n");
1055 goto err_out;
1057 device->swapchains[0] = swapchain;
1058 device_init_swapchain_state(device, swapchain);
1060 context = context_acquire(device, NULL);
1062 create_dummy_textures(device, context);
1063 create_default_samplers(device, context);
1065 device->contexts[0]->last_was_rhw = 0;
1067 TRACE("All defaults now set up, leaving 3D init.\n");
1069 context_release(context);
1071 /* Clear the screen */
1072 if (swapchain->back_buffers && swapchain->back_buffers[0])
1073 clear_flags |= WINED3DCLEAR_TARGET;
1074 if (swapchain_desc->enable_auto_depth_stencil)
1075 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1076 if (clear_flags)
1077 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1079 device->d3d_initialized = TRUE;
1081 if (wined3d_settings.logo)
1082 device_load_logo(device, wined3d_settings.logo);
1083 return WINED3D_OK;
1085 err_out:
1086 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1087 HeapFree(GetProcessHeap(), 0, device->swapchains);
1088 device->swapchain_count = 0;
1089 if (device->back_buffer_view)
1090 wined3d_rendertarget_view_decref(device->back_buffer_view);
1091 if (swapchain)
1092 wined3d_swapchain_decref(swapchain);
1093 if (device->blit_priv)
1094 device->blitter->free_private(device);
1095 if (device->shader_priv)
1096 device->shader_backend->shader_free_private(device);
1098 return hr;
1101 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1102 struct wined3d_swapchain_desc *swapchain_desc)
1104 struct wined3d_swapchain *swapchain = NULL;
1105 HRESULT hr;
1107 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1109 /* Setup the implicit swapchain */
1110 TRACE("Creating implicit swapchain\n");
1111 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1112 swapchain_desc, &swapchain);
1113 if (FAILED(hr))
1115 WARN("Failed to create implicit swapchain\n");
1116 goto err_out;
1119 device->swapchain_count = 1;
1120 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1122 ERR("Out of memory!\n");
1123 goto err_out;
1125 device->swapchains[0] = swapchain;
1126 return WINED3D_OK;
1128 err_out:
1129 wined3d_swapchain_decref(swapchain);
1130 return hr;
1133 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1135 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1137 wined3d_sampler_decref(sampler);
1140 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1142 struct wined3d_resource *resource, *cursor;
1143 const struct wined3d_gl_info *gl_info;
1144 struct wined3d_context *context;
1145 struct wined3d_surface *surface;
1146 UINT i;
1148 TRACE("device %p.\n", device);
1150 if (!device->d3d_initialized)
1151 return WINED3DERR_INVALIDCALL;
1153 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1154 * it was created. Thus make sure a context is active for the glDelete* calls
1156 context = context_acquire(device, NULL);
1157 gl_info = context->gl_info;
1159 if (device->logo_texture)
1160 wined3d_texture_decref(device->logo_texture);
1161 if (device->cursor_texture)
1162 wined3d_texture_decref(device->cursor_texture);
1164 state_unbind_resources(&device->state);
1166 /* Unload resources */
1167 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1169 TRACE("Unloading resource %p.\n", resource);
1170 wined3d_cs_emit_unload_resource(device->cs, resource);
1173 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1175 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1176 * private data, it might contain opengl pointers
1178 if (device->depth_blt_texture)
1180 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1181 device->depth_blt_texture = 0;
1184 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1185 device->blitter->free_private(device);
1186 device->shader_backend->shader_free_private(device);
1187 destroy_dummy_textures(device, context);
1188 destroy_default_samplers(device, context);
1190 context_release(context);
1192 /* Release the buffers (with sanity checks) */
1193 if (device->onscreen_depth_stencil)
1195 surface = device->onscreen_depth_stencil;
1196 device->onscreen_depth_stencil = NULL;
1197 wined3d_texture_decref(surface->container);
1200 if (device->fb.depth_stencil)
1202 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1204 TRACE("Releasing depth/stencil view %p.\n", view);
1206 device->fb.depth_stencil = NULL;
1207 wined3d_rendertarget_view_decref(view);
1210 if (device->auto_depth_stencil_view)
1212 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1214 device->auto_depth_stencil_view = NULL;
1215 if (wined3d_rendertarget_view_decref(view))
1216 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1219 for (i = 0; i < gl_info->limits.buffers; ++i)
1221 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1223 if (device->back_buffer_view)
1225 wined3d_rendertarget_view_decref(device->back_buffer_view);
1226 device->back_buffer_view = NULL;
1229 for (i = 0; i < device->swapchain_count; ++i)
1231 TRACE("Releasing the implicit swapchain %u.\n", i);
1232 if (wined3d_swapchain_decref(device->swapchains[i]))
1233 FIXME("Something's still holding the implicit swapchain.\n");
1236 HeapFree(GetProcessHeap(), 0, device->swapchains);
1237 device->swapchains = NULL;
1238 device->swapchain_count = 0;
1240 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1241 device->fb.render_targets = NULL;
1243 device->d3d_initialized = FALSE;
1245 return WINED3D_OK;
1248 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1250 unsigned int i;
1252 for (i = 0; i < device->swapchain_count; ++i)
1254 TRACE("Releasing the implicit swapchain %u.\n", i);
1255 if (wined3d_swapchain_decref(device->swapchains[i]))
1256 FIXME("Something's still holding the implicit swapchain.\n");
1259 HeapFree(GetProcessHeap(), 0, device->swapchains);
1260 device->swapchains = NULL;
1261 device->swapchain_count = 0;
1262 return WINED3D_OK;
1265 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1266 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1267 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1269 * There is no way to deactivate thread safety once it is enabled.
1271 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1273 TRACE("device %p.\n", device);
1275 /* For now just store the flag (needed in case of ddraw). */
1276 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1279 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1281 TRACE("device %p.\n", device);
1283 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1284 wine_dbgstr_longlong(device->adapter->vram_bytes),
1285 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1286 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1288 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1291 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1292 struct wined3d_buffer *buffer, UINT offset)
1294 struct wined3d_stream_output *stream;
1295 struct wined3d_buffer *prev_buffer;
1297 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1299 if (idx >= MAX_STREAM_OUT)
1301 WARN("Invalid stream output %u.\n", idx);
1302 return;
1305 stream = &device->update_state->stream_output[idx];
1306 prev_buffer = stream->buffer;
1308 if (buffer)
1309 wined3d_buffer_incref(buffer);
1310 stream->buffer = buffer;
1311 stream->offset = offset;
1312 if (!device->recording)
1313 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1314 if (prev_buffer)
1315 wined3d_buffer_decref(prev_buffer);
1318 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1319 UINT idx, UINT *offset)
1321 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1323 if (idx >= MAX_STREAM_OUT)
1325 WARN("Invalid stream output %u.\n", idx);
1326 return NULL;
1329 if (offset)
1330 *offset = device->state.stream_output[idx].offset;
1331 return device->state.stream_output[idx].buffer;
1334 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1335 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1337 struct wined3d_stream_state *stream;
1338 struct wined3d_buffer *prev_buffer;
1340 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1341 device, stream_idx, buffer, offset, stride);
1343 if (stream_idx >= MAX_STREAMS)
1345 WARN("Stream index %u out of range.\n", stream_idx);
1346 return WINED3DERR_INVALIDCALL;
1348 else if (offset & 0x3)
1350 WARN("Offset %u is not 4 byte aligned.\n", offset);
1351 return WINED3DERR_INVALIDCALL;
1354 stream = &device->update_state->streams[stream_idx];
1355 prev_buffer = stream->buffer;
1357 if (device->recording)
1358 device->recording->changed.streamSource |= 1u << stream_idx;
1360 if (prev_buffer == buffer
1361 && stream->stride == stride
1362 && stream->offset == offset)
1364 TRACE("Application is setting the old values over, nothing to do.\n");
1365 return WINED3D_OK;
1368 stream->buffer = buffer;
1369 if (buffer)
1371 stream->stride = stride;
1372 stream->offset = offset;
1373 wined3d_buffer_incref(buffer);
1376 if (!device->recording)
1377 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1378 if (prev_buffer)
1379 wined3d_buffer_decref(prev_buffer);
1381 return WINED3D_OK;
1384 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1385 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1387 const struct wined3d_stream_state *stream;
1389 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1390 device, stream_idx, buffer, offset, stride);
1392 if (stream_idx >= MAX_STREAMS)
1394 WARN("Stream index %u out of range.\n", stream_idx);
1395 return WINED3DERR_INVALIDCALL;
1398 stream = &device->state.streams[stream_idx];
1399 *buffer = stream->buffer;
1400 if (offset)
1401 *offset = stream->offset;
1402 *stride = stream->stride;
1404 return WINED3D_OK;
1407 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1409 struct wined3d_stream_state *stream;
1410 UINT old_flags, old_freq;
1412 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1414 /* Verify input. At least in d3d9 this is invalid. */
1415 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1417 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1418 return WINED3DERR_INVALIDCALL;
1420 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1422 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1423 return WINED3DERR_INVALIDCALL;
1425 if (!divider)
1427 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1428 return WINED3DERR_INVALIDCALL;
1431 stream = &device->update_state->streams[stream_idx];
1432 old_flags = stream->flags;
1433 old_freq = stream->frequency;
1435 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1436 stream->frequency = divider & 0x7fffff;
1438 if (device->recording)
1439 device->recording->changed.streamFreq |= 1u << stream_idx;
1440 else if (stream->frequency != old_freq || stream->flags != old_flags)
1441 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1443 return WINED3D_OK;
1446 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1447 UINT stream_idx, UINT *divider)
1449 const struct wined3d_stream_state *stream;
1451 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1453 stream = &device->state.streams[stream_idx];
1454 *divider = stream->flags | stream->frequency;
1456 TRACE("Returning %#x.\n", *divider);
1458 return WINED3D_OK;
1461 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1462 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1464 TRACE("device %p, state %s, matrix %p.\n",
1465 device, debug_d3dtstype(d3dts), matrix);
1466 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1467 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1468 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1469 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1471 /* Handle recording of state blocks. */
1472 if (device->recording)
1474 TRACE("Recording... not performing anything.\n");
1475 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1476 device->update_state->transforms[d3dts] = *matrix;
1477 return;
1480 /* If the new matrix is the same as the current one,
1481 * we cut off any further processing. this seems to be a reasonable
1482 * optimization because as was noticed, some apps (warcraft3 for example)
1483 * tend towards setting the same matrix repeatedly for some reason.
1485 * From here on we assume that the new matrix is different, wherever it matters. */
1486 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1488 TRACE("The application is setting the same matrix over again.\n");
1489 return;
1492 device->state.transforms[d3dts] = *matrix;
1493 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1496 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1497 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1499 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1501 *matrix = device->state.transforms[state];
1504 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1505 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1507 const struct wined3d_matrix *mat;
1508 struct wined3d_matrix temp;
1510 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1512 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1513 * below means it will be recorded in a state block change, but it
1514 * works regardless where it is recorded.
1515 * If this is found to be wrong, change to StateBlock. */
1516 if (state > HIGHEST_TRANSFORMSTATE)
1518 WARN("Unhandled transform state %#x.\n", state);
1519 return;
1522 mat = &device->update_state->transforms[state];
1523 multiply_matrix(&temp, mat, matrix);
1525 /* Apply change via set transform - will reapply to eg. lights this way. */
1526 wined3d_device_set_transform(device, state, &temp);
1529 /* Note lights are real special cases. Although the device caps state only
1530 * e.g. 8 are supported, you can reference any indexes you want as long as
1531 * that number max are enabled at any one point in time. Therefore since the
1532 * indices can be anything, we need a hashmap of them. However, this causes
1533 * stateblock problems. When capturing the state block, I duplicate the
1534 * hashmap, but when recording, just build a chain pretty much of commands to
1535 * be replayed. */
1536 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1537 UINT light_idx, const struct wined3d_light *light)
1539 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1540 struct wined3d_light_info *object = NULL;
1541 struct list *e;
1542 float rho;
1544 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1546 /* Check the parameter range. Need for speed most wanted sets junk lights
1547 * which confuse the GL driver. */
1548 if (!light)
1549 return WINED3DERR_INVALIDCALL;
1551 switch (light->type)
1553 case WINED3D_LIGHT_POINT:
1554 case WINED3D_LIGHT_SPOT:
1555 case WINED3D_LIGHT_GLSPOT:
1556 /* Incorrect attenuation values can cause the gl driver to crash.
1557 * Happens with Need for speed most wanted. */
1558 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1560 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1561 return WINED3DERR_INVALIDCALL;
1563 break;
1565 case WINED3D_LIGHT_DIRECTIONAL:
1566 case WINED3D_LIGHT_PARALLELPOINT:
1567 /* Ignores attenuation */
1568 break;
1570 default:
1571 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1572 return WINED3DERR_INVALIDCALL;
1575 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1577 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1578 if (object->OriginalIndex == light_idx)
1579 break;
1580 object = NULL;
1583 if (!object)
1585 TRACE("Adding new light\n");
1586 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1587 if (!object)
1588 return E_OUTOFMEMORY;
1590 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1591 object->glIndex = -1;
1592 object->OriginalIndex = light_idx;
1595 /* Initialize the object. */
1596 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1597 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1598 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1599 light_idx, light->type, debug_color(&light->diffuse),
1600 debug_color(&light->specular), debug_color(&light->ambient),
1601 light->position.x, light->position.y, light->position.z,
1602 light->direction.x, light->direction.y, light->direction.z,
1603 light->range, light->falloff, light->theta, light->phi);
1605 /* Update the live definitions if the light is currently assigned a glIndex. */
1606 if (object->glIndex != -1 && !device->recording)
1608 if (object->OriginalParms.type != light->type)
1609 device_invalidate_state(device, STATE_LIGHT_TYPE);
1610 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1613 /* Save away the information. */
1614 object->OriginalParms = *light;
1616 switch (light->type)
1618 case WINED3D_LIGHT_POINT:
1619 /* Position */
1620 object->position.x = light->position.x;
1621 object->position.y = light->position.y;
1622 object->position.z = light->position.z;
1623 object->position.w = 1.0f;
1624 object->cutoff = 180.0f;
1625 /* FIXME: Range */
1626 break;
1628 case WINED3D_LIGHT_DIRECTIONAL:
1629 /* Direction */
1630 object->direction.x = -light->direction.x;
1631 object->direction.y = -light->direction.y;
1632 object->direction.z = -light->direction.z;
1633 object->direction.w = 0.0f;
1634 object->exponent = 0.0f;
1635 object->cutoff = 180.0f;
1636 break;
1638 case WINED3D_LIGHT_SPOT:
1639 /* Position */
1640 object->position.x = light->position.x;
1641 object->position.y = light->position.y;
1642 object->position.z = light->position.z;
1643 object->position.w = 1.0f;
1645 /* Direction */
1646 object->direction.x = light->direction.x;
1647 object->direction.y = light->direction.y;
1648 object->direction.z = light->direction.z;
1649 object->direction.w = 0.0f;
1651 /* opengl-ish and d3d-ish spot lights use too different models
1652 * for the light "intensity" as a function of the angle towards
1653 * the main light direction, so we only can approximate very
1654 * roughly. However, spot lights are rather rarely used in games
1655 * (if ever used at all). Furthermore if still used, probably
1656 * nobody pays attention to such details. */
1657 if (!light->falloff)
1659 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1660 * equations have the falloff resp. exponent parameter as an
1661 * exponent, so the spot light lighting will always be 1.0 for
1662 * both of them, and we don't have to care for the rest of the
1663 * rather complex calculation. */
1664 object->exponent = 0.0f;
1666 else
1668 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1669 if (rho < 0.0001f)
1670 rho = 0.0001f;
1671 object->exponent = -0.3f / logf(cosf(rho / 2));
1674 if (object->exponent > 128.0f)
1675 object->exponent = 128.0f;
1677 object->cutoff = (float)(light->phi * 90 / M_PI);
1678 /* FIXME: Range */
1679 break;
1681 case WINED3D_LIGHT_PARALLELPOINT:
1682 object->position.x = light->position.x;
1683 object->position.y = light->position.y;
1684 object->position.z = light->position.z;
1685 object->position.w = 1.0f;
1686 break;
1688 default:
1689 FIXME("Unrecognized light type %#x.\n", light->type);
1692 return WINED3D_OK;
1695 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1696 UINT light_idx, struct wined3d_light *light)
1698 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1699 struct wined3d_light_info *light_info = NULL;
1700 struct list *e;
1702 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1704 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1706 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1707 if (light_info->OriginalIndex == light_idx)
1708 break;
1709 light_info = NULL;
1712 if (!light_info)
1714 TRACE("Light information requested but light not defined\n");
1715 return WINED3DERR_INVALIDCALL;
1718 *light = light_info->OriginalParms;
1719 return WINED3D_OK;
1722 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1724 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1725 struct wined3d_light_info *light_info = NULL;
1726 struct list *e;
1728 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1730 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1732 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1733 if (light_info->OriginalIndex == light_idx)
1734 break;
1735 light_info = NULL;
1737 TRACE("Found light %p.\n", light_info);
1739 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1740 if (!light_info)
1742 TRACE("Light enabled requested but light not defined, so defining one!\n");
1743 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1745 /* Search for it again! Should be fairly quick as near head of list. */
1746 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1748 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1749 if (light_info->OriginalIndex == light_idx)
1750 break;
1751 light_info = NULL;
1753 if (!light_info)
1755 FIXME("Adding default lights has failed dismally\n");
1756 return WINED3DERR_INVALIDCALL;
1760 if (!enable)
1762 if (light_info->glIndex != -1)
1764 if (!device->recording)
1766 device_invalidate_state(device, STATE_LIGHT_TYPE);
1767 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1770 device->update_state->lights[light_info->glIndex] = NULL;
1771 light_info->glIndex = -1;
1773 else
1775 TRACE("Light already disabled, nothing to do\n");
1777 light_info->enabled = FALSE;
1779 else
1781 light_info->enabled = TRUE;
1782 if (light_info->glIndex != -1)
1784 TRACE("Nothing to do as light was enabled\n");
1786 else
1788 unsigned int i;
1789 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1790 /* Find a free GL light. */
1791 for (i = 0; i < gl_info->limits.lights; ++i)
1793 if (!device->update_state->lights[i])
1795 device->update_state->lights[i] = light_info;
1796 light_info->glIndex = i;
1797 break;
1800 if (light_info->glIndex == -1)
1802 /* Our tests show that Windows returns D3D_OK in this situation, even with
1803 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1804 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1805 * as well for those lights.
1807 * TODO: Test how this affects rendering. */
1808 WARN("Too many concurrently active lights\n");
1809 return WINED3D_OK;
1812 /* i == light_info->glIndex */
1813 if (!device->recording)
1815 device_invalidate_state(device, STATE_LIGHT_TYPE);
1816 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1821 return WINED3D_OK;
1824 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1826 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1827 struct wined3d_light_info *light_info = NULL;
1828 struct list *e;
1830 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1832 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1834 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1835 if (light_info->OriginalIndex == light_idx)
1836 break;
1837 light_info = NULL;
1840 if (!light_info)
1842 TRACE("Light enabled state requested but light not defined.\n");
1843 return WINED3DERR_INVALIDCALL;
1845 /* true is 128 according to SetLightEnable */
1846 *enable = light_info->enabled ? 128 : 0;
1847 return WINED3D_OK;
1850 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1851 UINT plane_idx, const struct wined3d_vec4 *plane)
1853 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1855 /* Validate plane_idx. */
1856 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1858 TRACE("Application has requested clipplane this device doesn't support.\n");
1859 return WINED3DERR_INVALIDCALL;
1862 if (device->recording)
1863 device->recording->changed.clipplane |= 1u << plane_idx;
1865 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1867 TRACE("Application is setting old values over, nothing to do.\n");
1868 return WINED3D_OK;
1871 device->update_state->clip_planes[plane_idx] = *plane;
1873 if (!device->recording)
1874 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1876 return WINED3D_OK;
1879 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1880 UINT plane_idx, struct wined3d_vec4 *plane)
1882 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1884 /* Validate plane_idx. */
1885 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1887 TRACE("Application has requested clipplane this device doesn't support.\n");
1888 return WINED3DERR_INVALIDCALL;
1891 *plane = device->state.clip_planes[plane_idx];
1893 return WINED3D_OK;
1896 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1897 const struct wined3d_clip_status *clip_status)
1899 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1901 if (!clip_status)
1902 return WINED3DERR_INVALIDCALL;
1904 return WINED3D_OK;
1907 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1908 struct wined3d_clip_status *clip_status)
1910 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1912 if (!clip_status)
1913 return WINED3DERR_INVALIDCALL;
1915 return WINED3D_OK;
1918 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1920 TRACE("device %p, material %p.\n", device, material);
1922 device->update_state->material = *material;
1924 if (device->recording)
1925 device->recording->changed.material = TRUE;
1926 else
1927 wined3d_cs_emit_set_material(device->cs, material);
1930 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1932 TRACE("device %p, material %p.\n", device, material);
1934 *material = device->state.material;
1936 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1937 TRACE("ambient %s\n", debug_color(&material->ambient));
1938 TRACE("specular %s\n", debug_color(&material->specular));
1939 TRACE("emissive %s\n", debug_color(&material->emissive));
1940 TRACE("power %.8e.\n", material->power);
1943 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1944 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1946 enum wined3d_format_id prev_format;
1947 struct wined3d_buffer *prev_buffer;
1948 unsigned int prev_offset;
1950 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1951 device, buffer, debug_d3dformat(format_id), offset);
1953 prev_buffer = device->update_state->index_buffer;
1954 prev_format = device->update_state->index_format;
1955 prev_offset = device->update_state->index_offset;
1957 device->update_state->index_buffer = buffer;
1958 device->update_state->index_format = format_id;
1959 device->update_state->index_offset = offset;
1961 if (device->recording)
1962 device->recording->changed.indices = TRUE;
1964 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1965 return;
1967 if (buffer)
1968 wined3d_buffer_incref(buffer);
1969 if (!device->recording)
1970 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1971 if (prev_buffer)
1972 wined3d_buffer_decref(prev_buffer);
1975 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1976 enum wined3d_format_id *format, unsigned int *offset)
1978 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1980 *format = device->state.index_format;
1981 if (offset)
1982 *offset = device->state.index_offset;
1983 return device->state.index_buffer;
1986 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1988 TRACE("device %p, base_index %d.\n", device, base_index);
1990 device->update_state->base_vertex_index = base_index;
1993 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1995 TRACE("device %p.\n", device);
1997 return device->state.base_vertex_index;
2000 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2002 TRACE("device %p, viewport %p.\n", device, viewport);
2003 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2004 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2006 device->update_state->viewport = *viewport;
2008 /* Handle recording of state blocks */
2009 if (device->recording)
2011 TRACE("Recording... not performing anything\n");
2012 device->recording->changed.viewport = TRUE;
2013 return;
2016 wined3d_cs_emit_set_viewport(device->cs, viewport);
2019 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2021 TRACE("device %p, viewport %p.\n", device, viewport);
2023 *viewport = device->state.viewport;
2026 static void resolve_depth_buffer(struct wined3d_state *state)
2028 struct wined3d_texture *dst_texture = state->textures[0];
2029 struct wined3d_rendertarget_view *src_view;
2030 RECT src_rect, dst_rect;
2032 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2033 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2034 return;
2036 if (!(src_view = state->fb->depth_stencil))
2037 return;
2038 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2040 FIXME("Not supported on buffer resources.\n");
2041 return;
2044 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2045 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2046 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
2047 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2050 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2051 struct wined3d_rasterizer_state *rasterizer_state)
2053 struct wined3d_rasterizer_state *prev;
2055 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2057 prev = device->update_state->rasterizer_state;
2058 if (prev == rasterizer_state)
2059 return;
2061 if (rasterizer_state)
2062 wined3d_rasterizer_state_incref(rasterizer_state);
2063 device->update_state->rasterizer_state = rasterizer_state;
2064 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2065 if (prev)
2066 wined3d_rasterizer_state_decref(prev);
2069 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2071 TRACE("device %p.\n", device);
2073 return device->state.rasterizer_state;
2076 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2077 enum wined3d_render_state state, DWORD value)
2079 DWORD old_value;
2081 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2083 if (state > WINEHIGHEST_RENDER_STATE)
2085 WARN("Unhandled render state %#x.\n", state);
2086 return;
2089 old_value = device->state.render_states[state];
2090 device->update_state->render_states[state] = value;
2092 /* Handle recording of state blocks. */
2093 if (device->recording)
2095 TRACE("Recording... not performing anything.\n");
2096 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2097 return;
2100 /* Compared here and not before the assignment to allow proper stateblock recording. */
2101 if (value == old_value)
2102 TRACE("Application is setting the old value over, nothing to do.\n");
2103 else
2104 wined3d_cs_emit_set_render_state(device->cs, state, value);
2106 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2108 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2109 resolve_depth_buffer(&device->state);
2113 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2115 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2117 return device->state.render_states[state];
2120 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2121 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2123 DWORD old_value;
2125 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2126 device, sampler_idx, debug_d3dsamplerstate(state), value);
2128 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2129 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2131 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2133 WARN("Invalid sampler %u.\n", sampler_idx);
2134 return; /* Windows accepts overflowing this array ... we do not. */
2137 old_value = device->state.sampler_states[sampler_idx][state];
2138 device->update_state->sampler_states[sampler_idx][state] = value;
2140 /* Handle recording of state blocks. */
2141 if (device->recording)
2143 TRACE("Recording... not performing anything.\n");
2144 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2145 return;
2148 if (old_value == value)
2150 TRACE("Application is setting the old value over, nothing to do.\n");
2151 return;
2154 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2157 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2158 UINT sampler_idx, enum wined3d_sampler_state state)
2160 TRACE("device %p, sampler_idx %u, state %s.\n",
2161 device, sampler_idx, debug_d3dsamplerstate(state));
2163 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2164 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2166 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2168 WARN("Invalid sampler %u.\n", sampler_idx);
2169 return 0; /* Windows accepts overflowing this array ... we do not. */
2172 return device->state.sampler_states[sampler_idx][state];
2175 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2177 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2179 if (device->recording)
2180 device->recording->changed.scissorRect = TRUE;
2182 if (EqualRect(&device->update_state->scissor_rect, rect))
2184 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2185 return;
2187 CopyRect(&device->update_state->scissor_rect, rect);
2189 if (device->recording)
2191 TRACE("Recording... not performing anything.\n");
2192 return;
2195 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2198 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2200 TRACE("device %p, rect %p.\n", device, rect);
2202 *rect = device->state.scissor_rect;
2203 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2206 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2207 struct wined3d_vertex_declaration *declaration)
2209 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2211 TRACE("device %p, declaration %p.\n", device, declaration);
2213 if (device->recording)
2214 device->recording->changed.vertexDecl = TRUE;
2216 if (declaration == prev)
2217 return;
2219 if (declaration)
2220 wined3d_vertex_declaration_incref(declaration);
2221 device->update_state->vertex_declaration = declaration;
2222 if (!device->recording)
2223 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2224 if (prev)
2225 wined3d_vertex_declaration_decref(prev);
2228 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2230 TRACE("device %p.\n", device);
2232 return device->state.vertex_declaration;
2235 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2237 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2239 TRACE("device %p, shader %p.\n", device, shader);
2241 if (device->recording)
2242 device->recording->changed.vertexShader = TRUE;
2244 if (shader == prev)
2245 return;
2247 if (shader)
2248 wined3d_shader_incref(shader);
2249 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2250 if (!device->recording)
2251 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2252 if (prev)
2253 wined3d_shader_decref(prev);
2256 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2258 TRACE("device %p.\n", device);
2260 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2263 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2264 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2266 struct wined3d_buffer *prev;
2268 if (idx >= MAX_CONSTANT_BUFFERS)
2270 WARN("Invalid constant buffer index %u.\n", idx);
2271 return;
2274 prev = device->update_state->cb[type][idx];
2275 if (buffer == prev)
2276 return;
2278 if (buffer)
2279 wined3d_buffer_incref(buffer);
2280 device->update_state->cb[type][idx] = buffer;
2281 if (!device->recording)
2282 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2283 if (prev)
2284 wined3d_buffer_decref(prev);
2287 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2289 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2291 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2294 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2296 TRACE("device %p, idx %u.\n", device, idx);
2298 if (idx >= MAX_CONSTANT_BUFFERS)
2300 WARN("Invalid constant buffer index %u.\n", idx);
2301 return NULL;
2304 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2307 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2308 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2310 struct wined3d_shader_resource_view *prev;
2312 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2314 WARN("Invalid view index %u.\n", idx);
2315 return;
2318 prev = device->update_state->shader_resource_view[type][idx];
2319 if (view == prev)
2320 return;
2322 if (view)
2323 wined3d_shader_resource_view_incref(view);
2324 device->update_state->shader_resource_view[type][idx] = view;
2325 if (!device->recording)
2326 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2327 if (prev)
2328 wined3d_shader_resource_view_decref(prev);
2331 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2332 UINT idx, struct wined3d_shader_resource_view *view)
2334 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2336 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2339 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2340 UINT idx)
2342 TRACE("device %p, idx %u.\n", device, idx);
2344 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2346 WARN("Invalid view index %u.\n", idx);
2347 return NULL;
2350 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2353 static void wined3d_device_set_sampler(struct wined3d_device *device,
2354 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2356 struct wined3d_sampler *prev;
2358 if (idx >= MAX_SAMPLER_OBJECTS)
2360 WARN("Invalid sampler index %u.\n", idx);
2361 return;
2364 prev = device->update_state->sampler[type][idx];
2365 if (sampler == prev)
2366 return;
2368 if (sampler)
2369 wined3d_sampler_incref(sampler);
2370 device->update_state->sampler[type][idx] = sampler;
2371 if (!device->recording)
2372 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2373 if (prev)
2374 wined3d_sampler_decref(prev);
2377 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2379 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2381 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2384 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2386 TRACE("device %p, idx %u.\n", device, idx);
2388 if (idx >= MAX_SAMPLER_OBJECTS)
2390 WARN("Invalid sampler index %u.\n", idx);
2391 return NULL;
2394 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2397 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2398 unsigned int start_idx, unsigned int count, const BOOL *constants)
2400 unsigned int i;
2402 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2403 device, start_idx, count, constants);
2405 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2406 return WINED3DERR_INVALIDCALL;
2408 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2409 count = WINED3D_MAX_CONSTS_B - start_idx;
2410 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2411 if (TRACE_ON(d3d))
2413 for (i = 0; i < count; ++i)
2414 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2417 if (device->recording)
2419 for (i = start_idx; i < count + start_idx; ++i)
2420 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2422 else
2424 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2427 return WINED3D_OK;
2430 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2431 unsigned int start_idx, unsigned int count, BOOL *constants)
2433 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2434 device, start_idx, count, constants);
2436 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2437 return WINED3DERR_INVALIDCALL;
2439 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2440 count = WINED3D_MAX_CONSTS_B - start_idx;
2441 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2443 return WINED3D_OK;
2446 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2447 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2449 unsigned int i;
2451 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2452 device, start_idx, count, constants);
2454 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2455 return WINED3DERR_INVALIDCALL;
2457 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2458 count = WINED3D_MAX_CONSTS_I - start_idx;
2459 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2460 if (TRACE_ON(d3d))
2462 for (i = 0; i < count; ++i)
2463 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2466 if (device->recording)
2468 for (i = start_idx; i < count + start_idx; ++i)
2469 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2471 else
2473 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2476 return WINED3D_OK;
2479 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2480 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2482 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2483 device, start_idx, count, constants);
2485 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2486 return WINED3DERR_INVALIDCALL;
2488 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2489 count = WINED3D_MAX_CONSTS_I - start_idx;
2490 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2491 return WINED3D_OK;
2494 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2495 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2497 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2498 unsigned int i;
2500 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2501 device, start_idx, count, constants);
2503 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2504 || count > d3d_info->limits.vs_uniform_count - start_idx)
2505 return WINED3DERR_INVALIDCALL;
2507 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2508 if (TRACE_ON(d3d))
2510 for (i = 0; i < count; ++i)
2511 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2514 if (device->recording)
2515 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2516 count * sizeof(*device->recording->changed.vs_consts_f));
2517 else
2518 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2520 return WINED3D_OK;
2523 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2524 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2526 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2528 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2529 device, start_idx, count, constants);
2531 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2532 || count > d3d_info->limits.vs_uniform_count - start_idx)
2533 return WINED3DERR_INVALIDCALL;
2535 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2537 return WINED3D_OK;
2540 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2542 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2544 TRACE("device %p, shader %p.\n", device, shader);
2546 if (device->recording)
2547 device->recording->changed.pixelShader = TRUE;
2549 if (shader == prev)
2550 return;
2552 if (shader)
2553 wined3d_shader_incref(shader);
2554 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2555 if (!device->recording)
2556 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2557 if (prev)
2558 wined3d_shader_decref(prev);
2561 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2563 TRACE("device %p.\n", device);
2565 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2568 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2570 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2572 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2575 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2577 TRACE("device %p, idx %u.\n", device, idx);
2579 if (idx >= MAX_CONSTANT_BUFFERS)
2581 WARN("Invalid constant buffer index %u.\n", idx);
2582 return NULL;
2585 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2588 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2589 UINT idx, struct wined3d_shader_resource_view *view)
2591 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2593 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2596 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2597 UINT idx)
2599 TRACE("device %p, idx %u.\n", device, idx);
2601 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2603 WARN("Invalid view index %u.\n", idx);
2604 return NULL;
2607 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2610 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2612 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2614 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2617 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2619 TRACE("device %p, idx %u.\n", device, idx);
2621 if (idx >= MAX_SAMPLER_OBJECTS)
2623 WARN("Invalid sampler index %u.\n", idx);
2624 return NULL;
2627 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2630 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2631 unsigned int start_idx, unsigned int count, const BOOL *constants)
2633 unsigned int i;
2635 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2636 device, start_idx, count, constants);
2638 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2639 return WINED3DERR_INVALIDCALL;
2641 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2642 count = WINED3D_MAX_CONSTS_B - start_idx;
2643 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2644 if (TRACE_ON(d3d))
2646 for (i = 0; i < count; ++i)
2647 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2650 if (device->recording)
2652 for (i = start_idx; i < count + start_idx; ++i)
2653 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2655 else
2657 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2660 return WINED3D_OK;
2663 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2664 unsigned int start_idx, unsigned int count, BOOL *constants)
2666 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2667 device, start_idx, count, constants);
2669 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2670 return WINED3DERR_INVALIDCALL;
2672 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2673 count = WINED3D_MAX_CONSTS_B - start_idx;
2674 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2676 return WINED3D_OK;
2679 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2680 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2682 unsigned int i;
2684 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2685 device, start_idx, count, constants);
2687 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2688 return WINED3DERR_INVALIDCALL;
2690 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2691 count = WINED3D_MAX_CONSTS_I - start_idx;
2692 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2693 if (TRACE_ON(d3d))
2695 for (i = 0; i < count; ++i)
2696 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2699 if (device->recording)
2701 for (i = start_idx; i < count + start_idx; ++i)
2702 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2704 else
2706 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2709 return WINED3D_OK;
2712 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2713 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2715 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2716 device, start_idx, count, constants);
2718 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2719 return WINED3DERR_INVALIDCALL;
2721 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2722 count = WINED3D_MAX_CONSTS_I - start_idx;
2723 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2725 return WINED3D_OK;
2728 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2729 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2731 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2732 unsigned int i;
2734 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2735 device, start_idx, count, constants);
2737 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2738 || count > d3d_info->limits.ps_uniform_count - start_idx)
2739 return WINED3DERR_INVALIDCALL;
2741 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2742 if (TRACE_ON(d3d))
2744 for (i = 0; i < count; ++i)
2745 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2748 if (device->recording)
2749 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2750 count * sizeof(*device->recording->changed.ps_consts_f));
2751 else
2752 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2754 return WINED3D_OK;
2757 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2758 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2760 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2762 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2763 device, start_idx, count, constants);
2765 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2766 || count > d3d_info->limits.ps_uniform_count - start_idx)
2767 return WINED3DERR_INVALIDCALL;
2769 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2771 return WINED3D_OK;
2774 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2776 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2778 TRACE("device %p, shader %p.\n", device, shader);
2780 if (device->recording || shader == prev)
2781 return;
2782 if (shader)
2783 wined3d_shader_incref(shader);
2784 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2785 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2786 if (prev)
2787 wined3d_shader_decref(prev);
2790 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2792 TRACE("device %p.\n", device);
2794 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2797 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2799 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2801 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2804 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2806 TRACE("device %p, idx %u.\n", device, idx);
2808 if (idx >= MAX_CONSTANT_BUFFERS)
2810 WARN("Invalid constant buffer index %u.\n", idx);
2811 return NULL;
2814 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2817 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2818 UINT idx, struct wined3d_shader_resource_view *view)
2820 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2822 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2825 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2826 UINT idx)
2828 TRACE("device %p, idx %u.\n", device, idx);
2830 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2832 WARN("Invalid view index %u.\n", idx);
2833 return NULL;
2836 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2839 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2841 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2843 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2846 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2848 TRACE("device %p, idx %u.\n", device, idx);
2850 if (idx >= MAX_SAMPLER_OBJECTS)
2852 WARN("Invalid sampler index %u.\n", idx);
2853 return NULL;
2856 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2859 /* Context activation is done by the caller. */
2860 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2861 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2862 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2863 DWORD DestFVF)
2865 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2866 struct wined3d_map_desc map_desc;
2867 struct wined3d_box box = {0};
2868 struct wined3d_viewport vp;
2869 UINT vertex_size;
2870 unsigned int i;
2871 BYTE *dest_ptr;
2872 BOOL doClip;
2873 DWORD numTextures;
2874 HRESULT hr;
2876 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2878 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2881 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2883 ERR("Source has no position mask\n");
2884 return WINED3DERR_INVALIDCALL;
2887 if (device->state.render_states[WINED3D_RS_CLIPPING])
2889 static BOOL warned = FALSE;
2891 * The clipping code is not quite correct. Some things need
2892 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2893 * so disable clipping for now.
2894 * (The graphics in Half-Life are broken, and my processvertices
2895 * test crashes with IDirect3DDevice3)
2896 doClip = TRUE;
2898 doClip = FALSE;
2899 if(!warned) {
2900 warned = TRUE;
2901 FIXME("Clipping is broken and disabled for now\n");
2904 else
2905 doClip = FALSE;
2907 vertex_size = get_flexible_vertex_size(DestFVF);
2908 box.left = dwDestIndex * vertex_size;
2909 box.right = box.left + dwCount * vertex_size;
2910 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2912 WARN("Failed to map buffer, hr %#x.\n", hr);
2913 return hr;
2915 dest_ptr = map_desc.data;
2917 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2918 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2919 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2921 TRACE("View mat:\n");
2922 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2923 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2924 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2925 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2927 TRACE("Proj mat:\n");
2928 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2929 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2930 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2931 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2933 TRACE("World mat:\n");
2934 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2935 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2936 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2937 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2939 /* Get the viewport */
2940 wined3d_device_get_viewport(device, &vp);
2941 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2942 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2944 multiply_matrix(&mat,&view_mat,&world_mat);
2945 multiply_matrix(&mat,&proj_mat,&mat);
2947 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2949 for (i = 0; i < dwCount; i+= 1) {
2950 unsigned int tex_index;
2952 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2953 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2954 /* The position first */
2955 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2956 const float *p = (const float *)(element->data.addr + i * element->stride);
2957 float x, y, z, rhw;
2958 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2960 /* Multiplication with world, view and projection matrix. */
2961 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2962 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2963 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2964 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2966 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2968 /* WARNING: The following things are taken from d3d7 and were not yet checked
2969 * against d3d8 or d3d9!
2972 /* Clipping conditions: From msdn
2974 * A vertex is clipped if it does not match the following requirements
2975 * -rhw < x <= rhw
2976 * -rhw < y <= rhw
2977 * 0 < z <= rhw
2978 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2980 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2981 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2985 if( !doClip ||
2986 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2987 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2988 ( rhw > eps ) ) ) {
2990 /* "Normal" viewport transformation (not clipped)
2991 * 1) The values are divided by rhw
2992 * 2) The y axis is negative, so multiply it with -1
2993 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2994 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2995 * 4) Multiply x with Width/2 and add Width/2
2996 * 5) The same for the height
2997 * 6) Add the viewpoint X and Y to the 2D coordinates and
2998 * The minimum Z value to z
2999 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3001 * Well, basically it's simply a linear transformation into viewport
3002 * coordinates
3005 x /= rhw;
3006 y /= rhw;
3007 z /= rhw;
3009 y *= -1;
3011 x *= vp.width / 2;
3012 y *= vp.height / 2;
3013 z *= vp.max_z - vp.min_z;
3015 x += vp.width / 2 + vp.x;
3016 y += vp.height / 2 + vp.y;
3017 z += vp.min_z;
3019 rhw = 1 / rhw;
3020 } else {
3021 /* That vertex got clipped
3022 * Contrary to OpenGL it is not dropped completely, it just
3023 * undergoes a different calculation.
3025 TRACE("Vertex got clipped\n");
3026 x += rhw;
3027 y += rhw;
3029 x /= 2;
3030 y /= 2;
3032 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3033 * outside of the main vertex buffer memory. That needs some more
3034 * investigation...
3038 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3041 ( (float *) dest_ptr)[0] = x;
3042 ( (float *) dest_ptr)[1] = y;
3043 ( (float *) dest_ptr)[2] = z;
3044 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3046 dest_ptr += 3 * sizeof(float);
3048 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3049 dest_ptr += sizeof(float);
3052 if (DestFVF & WINED3DFVF_PSIZE)
3053 dest_ptr += sizeof(DWORD);
3055 if (DestFVF & WINED3DFVF_NORMAL)
3057 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3058 const float *normal = (const float *)(element->data.addr + i * element->stride);
3059 /* AFAIK this should go into the lighting information */
3060 FIXME("Didn't expect the destination to have a normal\n");
3061 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3064 if (DestFVF & WINED3DFVF_DIFFUSE)
3066 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3067 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3068 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3070 static BOOL warned = FALSE;
3072 if(!warned) {
3073 ERR("No diffuse color in source, but destination has one\n");
3074 warned = TRUE;
3077 *( (DWORD *) dest_ptr) = 0xffffffff;
3078 dest_ptr += sizeof(DWORD);
3080 else
3082 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3086 if (DestFVF & WINED3DFVF_SPECULAR)
3088 /* What's the color value in the feedback buffer? */
3089 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3090 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3091 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3093 static BOOL warned = FALSE;
3095 if(!warned) {
3096 ERR("No specular color in source, but destination has one\n");
3097 warned = TRUE;
3100 *(DWORD *)dest_ptr = 0xff000000;
3101 dest_ptr += sizeof(DWORD);
3103 else
3105 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3109 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3111 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3112 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3113 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3115 ERR("No source texture, but destination requests one\n");
3116 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3118 else
3120 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3125 wined3d_resource_unmap(&dest->resource, 0);
3127 return WINED3D_OK;
3129 #undef copy_and_next
3131 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3132 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3133 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3135 struct wined3d_state *state = &device->state;
3136 struct wined3d_stream_info stream_info;
3137 const struct wined3d_gl_info *gl_info;
3138 struct wined3d_context *context;
3139 struct wined3d_shader *vs;
3140 unsigned int i;
3141 HRESULT hr;
3142 WORD map;
3144 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3145 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3146 device, src_start_idx, dst_idx, vertex_count,
3147 dst_buffer, declaration, flags, dst_fvf);
3149 if (declaration)
3150 FIXME("Output vertex declaration not implemented yet.\n");
3152 /* Need any context to write to the vbo. */
3153 context = context_acquire(device, NULL);
3154 gl_info = context->gl_info;
3156 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3157 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3158 context_stream_info_from_declaration(context, state, &stream_info);
3159 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3161 /* We can't convert FROM a VBO, and vertex buffers used to source into
3162 * process_vertices() are unlikely to ever be used for drawing. Release
3163 * VBOs in those buffers and fix up the stream_info structure.
3165 * Also apply the start index. */
3166 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3168 struct wined3d_stream_info_element *e;
3169 struct wined3d_buffer *buffer;
3171 if (!(map & 1))
3172 continue;
3174 e = &stream_info.elements[i];
3175 buffer = state->streams[e->stream_idx].buffer;
3176 e->data.buffer_object = 0;
3177 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3178 if (buffer->buffer_object)
3180 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3181 buffer->buffer_object = 0;
3183 if (e->data.addr)
3184 e->data.addr += e->stride * src_start_idx;
3187 hr = process_vertices_strided(device, dst_idx, vertex_count,
3188 &stream_info, dst_buffer, flags, dst_fvf);
3190 context_release(context);
3192 return hr;
3195 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3196 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3198 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3199 DWORD old_value;
3201 TRACE("device %p, stage %u, state %s, value %#x.\n",
3202 device, stage, debug_d3dtexturestate(state), value);
3204 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3206 WARN("Invalid state %#x passed.\n", state);
3207 return;
3210 if (stage >= d3d_info->limits.ffp_blend_stages)
3212 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3213 stage, d3d_info->limits.ffp_blend_stages - 1);
3214 return;
3217 old_value = device->update_state->texture_states[stage][state];
3218 device->update_state->texture_states[stage][state] = value;
3220 if (device->recording)
3222 TRACE("Recording... not performing anything.\n");
3223 device->recording->changed.textureState[stage] |= 1u << state;
3224 return;
3227 /* Checked after the assignments to allow proper stateblock recording. */
3228 if (old_value == value)
3230 TRACE("Application is setting the old value over, nothing to do.\n");
3231 return;
3234 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3237 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3238 UINT stage, enum wined3d_texture_stage_state state)
3240 TRACE("device %p, stage %u, state %s.\n",
3241 device, stage, debug_d3dtexturestate(state));
3243 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3245 WARN("Invalid state %#x passed.\n", state);
3246 return 0;
3249 return device->state.texture_states[stage][state];
3252 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3253 UINT stage, struct wined3d_texture *texture)
3255 struct wined3d_texture *prev;
3257 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3259 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3260 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3262 /* Windows accepts overflowing this array... we do not. */
3263 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3265 WARN("Ignoring invalid stage %u.\n", stage);
3266 return WINED3D_OK;
3269 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3271 WARN("Rejecting attempt to set scratch texture.\n");
3272 return WINED3DERR_INVALIDCALL;
3275 if (device->recording)
3276 device->recording->changed.textures |= 1u << stage;
3278 prev = device->update_state->textures[stage];
3279 TRACE("Previous texture %p.\n", prev);
3281 if (texture == prev)
3283 TRACE("App is setting the same texture again, nothing to do.\n");
3284 return WINED3D_OK;
3287 TRACE("Setting new texture to %p.\n", texture);
3288 device->update_state->textures[stage] = texture;
3290 if (texture)
3291 wined3d_texture_incref(texture);
3292 if (!device->recording)
3293 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3294 if (prev)
3295 wined3d_texture_decref(prev);
3297 return WINED3D_OK;
3300 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3302 TRACE("device %p, stage %u.\n", device, stage);
3304 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3305 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3307 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3309 WARN("Ignoring invalid stage %u.\n", stage);
3310 return NULL; /* Windows accepts overflowing this array ... we do not. */
3313 return device->state.textures[stage];
3316 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3318 TRACE("device %p, caps %p.\n", device, caps);
3320 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3321 device->create_parms.device_type, caps);
3324 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3325 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3327 struct wined3d_swapchain *swapchain;
3329 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3330 device, swapchain_idx, mode, rotation);
3332 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3333 return WINED3DERR_INVALIDCALL;
3335 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3338 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3340 struct wined3d_stateblock *stateblock;
3341 HRESULT hr;
3343 TRACE("device %p.\n", device);
3345 if (device->recording)
3346 return WINED3DERR_INVALIDCALL;
3348 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3349 if (FAILED(hr))
3350 return hr;
3352 device->recording = stateblock;
3353 device->update_state = &stateblock->state;
3355 TRACE("Recording stateblock %p.\n", stateblock);
3357 return WINED3D_OK;
3360 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3361 struct wined3d_stateblock **stateblock)
3363 struct wined3d_stateblock *object = device->recording;
3365 TRACE("device %p, stateblock %p.\n", device, stateblock);
3367 if (!device->recording)
3369 WARN("Not recording.\n");
3370 *stateblock = NULL;
3371 return WINED3DERR_INVALIDCALL;
3374 stateblock_init_contained_states(object);
3376 *stateblock = object;
3377 device->recording = NULL;
3378 device->update_state = &device->state;
3380 TRACE("Returning stateblock %p.\n", *stateblock);
3382 return WINED3D_OK;
3385 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3387 /* At the moment we have no need for any functionality at the beginning
3388 * of a scene. */
3389 TRACE("device %p.\n", device);
3391 if (device->inScene)
3393 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3394 return WINED3DERR_INVALIDCALL;
3396 device->inScene = TRUE;
3397 return WINED3D_OK;
3400 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3402 struct wined3d_context *context;
3404 TRACE("device %p.\n", device);
3406 if (!device->inScene)
3408 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3409 return WINED3DERR_INVALIDCALL;
3412 context = context_acquire(device, NULL);
3413 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3414 context->gl_info->gl_ops.gl.p_glFlush();
3415 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3416 * fails. */
3417 context_release(context);
3419 device->inScene = FALSE;
3420 return WINED3D_OK;
3423 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3424 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3426 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3427 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3429 if (!rect_count && rects)
3431 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3432 return WINED3D_OK;
3435 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3437 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3438 if (!ds)
3440 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3441 /* TODO: What about depth stencil buffers without stencil bits? */
3442 return WINED3DERR_INVALIDCALL;
3444 else if (flags & WINED3DCLEAR_TARGET)
3446 if (ds->width < device->fb.render_targets[0]->width
3447 || ds->height < device->fb.render_targets[0]->height)
3449 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3450 return WINED3D_OK;
3455 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3457 return WINED3D_OK;
3460 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3461 struct wined3d_query *predicate, BOOL value)
3463 struct wined3d_query *prev;
3465 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3467 prev = device->update_state->predicate;
3468 if (predicate)
3470 FIXME("Predicated rendering not implemented.\n");
3471 wined3d_query_incref(predicate);
3473 device->update_state->predicate = predicate;
3474 device->update_state->predicate_value = value;
3475 if (!device->recording)
3476 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3477 if (prev)
3478 wined3d_query_decref(prev);
3481 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3483 TRACE("device %p, value %p.\n", device, value);
3485 *value = device->state.predicate_value;
3486 return device->state.predicate;
3489 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3490 enum wined3d_primitive_type primitive_type)
3492 GLenum gl_primitive_type, prev;
3494 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3496 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3497 prev = device->update_state->gl_primitive_type;
3498 device->update_state->gl_primitive_type = gl_primitive_type;
3499 if (device->recording)
3500 device->recording->changed.primitive_type = TRUE;
3501 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3502 device_invalidate_state(device, STATE_POINT_ENABLE);
3505 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3506 enum wined3d_primitive_type *primitive_type)
3508 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3510 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3512 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3515 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3517 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3519 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, 0, 0, FALSE);
3521 return WINED3D_OK;
3524 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3525 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3527 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3528 device, start_vertex, vertex_count, start_instance, instance_count);
3530 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3533 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3535 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3537 if (!device->state.index_buffer)
3539 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3540 * without an index buffer set. (The first time at least...)
3541 * D3D8 simply dies, but I doubt it can do much harm to return
3542 * D3DERR_INVALIDCALL there as well. */
3543 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3544 return WINED3DERR_INVALIDCALL;
3547 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3549 return WINED3D_OK;
3552 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3553 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3555 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3556 device, start_idx, index_count, start_instance, instance_count);
3558 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index,
3559 start_idx, index_count, start_instance, instance_count, TRUE);
3562 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3563 struct wined3d_texture *src_texture, unsigned int src_level,
3564 struct wined3d_texture *dst_texture, unsigned int level_count)
3566 struct wined3d_const_bo_address data;
3567 struct wined3d_context *context;
3568 struct wined3d_map_desc src;
3569 HRESULT hr = WINED3D_OK;
3570 unsigned int i;
3572 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3573 device, src_texture, src_level, dst_texture, level_count);
3575 if (src_texture->resource.format != dst_texture->resource.format)
3577 WARN("Source and destination formats do not match.\n");
3578 return WINED3DERR_INVALIDCALL;
3581 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3582 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3583 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3585 WARN("Source and destination dimensions do not match.\n");
3586 return WINED3DERR_INVALIDCALL;
3589 context = context_acquire(device, NULL);
3591 /* Only a prepare, since we're uploading entire volumes. */
3592 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3593 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3595 for (i = 0; i < level_count; ++i)
3597 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3598 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3599 goto done;
3601 data.buffer_object = 0;
3602 data.addr = src.data;
3603 wined3d_texture_upload_data(dst_texture, i, context, &data, src.row_pitch, src.slice_pitch);
3604 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3606 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3607 goto done;
3610 done:
3611 context_release(context);
3612 return hr;
3615 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3616 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3618 unsigned int src_size, dst_size, src_skip_levels = 0;
3619 unsigned int layer_count, level_count, i, j;
3620 enum wined3d_resource_type type;
3621 HRESULT hr;
3622 struct wined3d_context *context;
3624 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3626 /* Verify that the source and destination textures are non-NULL. */
3627 if (!src_texture || !dst_texture)
3629 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3630 return WINED3DERR_INVALIDCALL;
3633 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3635 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3636 return WINED3DERR_INVALIDCALL;
3638 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3640 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3641 return WINED3DERR_INVALIDCALL;
3644 /* Verify that the source and destination textures are the same type. */
3645 type = src_texture->resource.type;
3646 if (dst_texture->resource.type != type)
3648 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3649 return WINED3DERR_INVALIDCALL;
3652 layer_count = src_texture->layer_count;
3653 if (layer_count != dst_texture->layer_count)
3655 WARN("Source and destination have different layer counts.\n");
3656 return WINED3DERR_INVALIDCALL;
3659 level_count = min(wined3d_texture_get_level_count(src_texture),
3660 wined3d_texture_get_level_count(dst_texture));
3662 src_size = max(src_texture->resource.width, src_texture->resource.height);
3663 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3664 if (type == WINED3D_RTYPE_TEXTURE_3D)
3666 src_size = max(src_size, src_texture->resource.depth);
3667 dst_size = max(dst_size, dst_texture->resource.depth);
3669 while (src_size > dst_size)
3671 src_size >>= 1;
3672 ++src_skip_levels;
3675 /* Make sure that the destination texture is loaded. */
3676 context = context_acquire(device, NULL);
3677 wined3d_texture_load(dst_texture, context, FALSE);
3678 context_release(context);
3680 /* Update every surface level of the texture. */
3681 switch (type)
3683 case WINED3D_RTYPE_TEXTURE_2D:
3685 unsigned int src_levels = src_texture->level_count;
3686 unsigned int dst_levels = dst_texture->level_count;
3687 struct wined3d_surface *src_surface;
3688 struct wined3d_surface *dst_surface;
3690 for (i = 0; i < layer_count; ++i)
3692 for (j = 0; j < level_count; ++j)
3694 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3695 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3696 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3698 WARN("Failed to update surface, hr %#x.\n", hr);
3699 return hr;
3703 return WINED3D_OK;
3706 case WINED3D_RTYPE_TEXTURE_3D:
3707 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3708 src_texture, src_skip_levels, dst_texture, level_count)))
3709 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3710 return hr;
3712 default:
3713 FIXME("Unsupported texture type %#x.\n", type);
3714 return WINED3DERR_INVALIDCALL;
3718 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3720 const struct wined3d_state *state = &device->state;
3721 struct wined3d_texture *texture;
3722 DWORD i;
3724 TRACE("device %p, num_passes %p.\n", device, num_passes);
3726 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3728 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3730 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3731 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3733 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3735 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3736 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3739 texture = state->textures[i];
3740 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3742 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3744 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3745 return E_FAIL;
3747 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3749 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3750 return E_FAIL;
3752 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3753 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3755 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3756 return E_FAIL;
3760 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3761 || state->render_states[WINED3D_RS_STENCILENABLE])
3763 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3764 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3766 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3768 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3769 return WINED3DERR_CONFLICTINGRENDERSTATE;
3773 /* return a sensible default */
3774 *num_passes = 1;
3776 TRACE("returning D3D_OK\n");
3777 return WINED3D_OK;
3780 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3782 static BOOL warned;
3784 TRACE("device %p, software %#x.\n", device, software);
3786 if (!warned)
3788 FIXME("device %p, software %#x stub!\n", device, software);
3789 warned = TRUE;
3792 device->softwareVertexProcessing = software;
3795 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3797 static BOOL warned;
3799 TRACE("device %p.\n", device);
3801 if (!warned)
3803 TRACE("device %p stub!\n", device);
3804 warned = TRUE;
3807 return device->softwareVertexProcessing;
3810 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3811 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3813 struct wined3d_swapchain *swapchain;
3815 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3816 device, swapchain_idx, raster_status);
3818 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3819 return WINED3DERR_INVALIDCALL;
3821 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3824 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3826 static BOOL warned;
3828 TRACE("device %p, segments %.8e.\n", device, segments);
3830 if (segments != 0.0f)
3832 if (!warned)
3834 FIXME("device %p, segments %.8e stub!\n", device, segments);
3835 warned = TRUE;
3839 return WINED3D_OK;
3842 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3844 static BOOL warned;
3846 TRACE("device %p.\n", device);
3848 if (!warned)
3850 FIXME("device %p stub!\n", device);
3851 warned = TRUE;
3854 return 0.0f;
3857 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3858 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3860 struct wined3d_texture *dst_texture, *src_texture;
3861 RECT dst_rect, src_rect;
3862 unsigned int i, j;
3863 HRESULT hr;
3865 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3867 if (src_resource == dst_resource)
3869 WARN("Source and destination are the same resource.\n");
3870 return;
3873 if (src_resource->type != dst_resource->type)
3875 WARN("Resource types (%s / %s) don't match.\n",
3876 debug_d3dresourcetype(dst_resource->type),
3877 debug_d3dresourcetype(src_resource->type));
3878 return;
3881 if (src_resource->width != dst_resource->width
3882 || src_resource->height != dst_resource->height
3883 || src_resource->depth != dst_resource->depth)
3885 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3886 dst_resource->width, dst_resource->height, dst_resource->depth,
3887 src_resource->width, src_resource->height, src_resource->depth);
3888 return;
3891 if (src_resource->format->id != dst_resource->format->id)
3893 WARN("Resource formats (%s / %s) don't match.\n",
3894 debug_d3dformat(dst_resource->format->id),
3895 debug_d3dformat(src_resource->format->id));
3896 return;
3899 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3901 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3902 buffer_from_resource(src_resource), 0,
3903 dst_resource->size)))
3904 ERR("Failed to copy buffer, hr %#x.\n", hr);
3905 return;
3908 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3910 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3911 return;
3914 dst_texture = texture_from_resource(dst_resource);
3915 src_texture = texture_from_resource(src_resource);
3917 if (src_texture->layer_count != dst_texture->layer_count
3918 || src_texture->level_count != dst_texture->level_count)
3920 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3921 dst_texture->layer_count, dst_texture->level_count,
3922 src_texture->layer_count, src_texture->level_count);
3923 return;
3926 for (i = 0; i < dst_texture->level_count; ++i)
3928 SetRect(&dst_rect, 0, 0,
3929 wined3d_texture_get_level_width(dst_texture, i),
3930 wined3d_texture_get_level_height(dst_texture, i));
3931 SetRect(&src_rect, 0, 0,
3932 wined3d_texture_get_level_width(src_texture, i),
3933 wined3d_texture_get_level_height(dst_texture, i));
3934 for (j = 0; j < dst_texture->layer_count; ++j)
3936 unsigned int idx = j * dst_texture->level_count + i;
3938 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3939 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3940 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3945 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3946 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3947 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3948 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3950 struct wined3d_texture *dst_texture, *src_texture;
3951 RECT dst_rect, src_rect;
3952 HRESULT hr;
3954 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3955 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3956 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3957 src_resource, src_sub_resource_idx, debug_box(src_box));
3959 if (src_box && (src_box->left >= src_box->right
3960 || src_box->top >= src_box->bottom
3961 || src_box->front >= src_box->back))
3963 WARN("Invalid box %s specified.\n", debug_box(src_box));
3964 return WINED3DERR_INVALIDCALL;
3967 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3969 WARN("Source and destination are the same sub-resource.\n");
3970 return WINED3DERR_INVALIDCALL;
3973 if (src_resource->type != dst_resource->type)
3975 WARN("Resource types (%s / %s) don't match.\n",
3976 debug_d3dresourcetype(dst_resource->type),
3977 debug_d3dresourcetype(src_resource->type));
3978 return WINED3DERR_INVALIDCALL;
3981 if (src_resource->format->id != dst_resource->format->id)
3983 WARN("Resource formats (%s / %s) don't match.\n",
3984 debug_d3dformat(dst_resource->format->id),
3985 debug_d3dformat(src_resource->format->id));
3986 return WINED3DERR_INVALIDCALL;
3989 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3991 unsigned int src_offset, size;
3993 if (dst_sub_resource_idx)
3995 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3996 return WINED3DERR_INVALIDCALL;
3998 if (src_sub_resource_idx)
4000 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4001 return WINED3DERR_INVALIDCALL;
4004 if (src_box)
4006 src_offset = src_box->left;
4007 size = src_box->right - src_box->left;
4009 else
4011 src_offset = 0;
4012 size = src_resource->size;
4015 if (src_offset > src_resource->size
4016 || size > src_resource->size - src_offset
4017 || dst_x > dst_resource->size
4018 || size > dst_resource->size - dst_x)
4020 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4021 dst_x, src_offset, size);
4022 return WINED3DERR_INVALIDCALL;
4025 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4026 buffer_from_resource(src_resource), src_offset, size);
4029 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4031 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4032 return WINED3DERR_INVALIDCALL;
4035 dst_texture = texture_from_resource(dst_resource);
4036 src_texture = texture_from_resource(src_resource);
4038 if (src_box)
4040 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4042 else
4044 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4046 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4047 wined3d_texture_get_level_height(src_texture, level));
4050 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4051 dst_y + (src_rect.bottom - src_rect.top));
4053 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4054 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4055 WARN("Failed to blit, hr %#x.\n", hr);
4057 return hr;
4060 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4061 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4062 unsigned int depth_pitch)
4064 struct wined3d_texture_sub_resource *sub_resource;
4065 const struct wined3d_gl_info *gl_info;
4066 struct wined3d_const_bo_address addr;
4067 unsigned int width, height, level;
4068 struct wined3d_context *context;
4069 struct wined3d_texture *texture;
4070 struct wined3d_surface *surface;
4071 POINT dst_point;
4072 RECT src_rect;
4074 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4075 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4077 if (resource->type == WINED3D_RTYPE_BUFFER)
4079 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4080 HRESULT hr;
4082 if (sub_resource_idx > 0)
4084 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4085 return;
4088 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4089 WARN("Failed to update buffer data, hr %#x.\n", hr);
4091 return;
4094 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4096 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4097 return;
4100 texture = texture_from_resource(resource);
4101 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4103 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4104 return;
4106 surface = sub_resource->u.surface;
4108 level = sub_resource_idx % texture->level_count;
4109 width = wined3d_texture_get_level_width(texture, level);
4110 height = wined3d_texture_get_level_height(texture, level);
4112 src_rect.left = 0;
4113 src_rect.top = 0;
4114 if (box)
4116 if (box->left >= box->right || box->right > width
4117 || box->top >= box->bottom || box->bottom > height
4118 || box->front >= box->back)
4120 WARN("Invalid box %s specified.\n", debug_box(box));
4121 return;
4124 src_rect.right = box->right - box->left;
4125 src_rect.bottom = box->bottom - box->top;
4126 dst_point.x = box->left;
4127 dst_point.y = box->top;
4129 else
4131 src_rect.right = width;
4132 src_rect.bottom = height;
4133 dst_point.x = 0;
4134 dst_point.y = 0;
4137 addr.buffer_object = 0;
4138 addr.addr = data;
4140 context = context_acquire(resource->device, NULL);
4141 gl_info = context->gl_info;
4143 /* Only load the surface for partial updates. */
4144 if (!dst_point.x && !dst_point.y && src_rect.right == width && src_rect.bottom == height)
4145 wined3d_texture_prepare_texture(texture, context, FALSE);
4146 else
4147 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4148 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4150 wined3d_surface_upload_data(surface, gl_info, resource->format,
4151 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4153 context_release(context);
4155 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4156 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4159 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4160 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4161 const struct wined3d_color *color, float depth, DWORD stencil)
4163 const struct blit_shader *blitter;
4164 struct wined3d_resource *resource;
4165 enum wined3d_blit_op blit_op;
4166 RECT r;
4168 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4169 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4171 if (!flags)
4172 return WINED3D_OK;
4174 resource = view->resource;
4175 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4177 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4178 return WINED3DERR_INVALIDCALL;
4181 if (view->depth > 1)
4183 FIXME("Layered clears not implemented.\n");
4184 return WINED3DERR_INVALIDCALL;
4187 if (!rect)
4189 SetRect(&r, 0, 0, view->width, view->height);
4190 rect = &r;
4193 if (flags & WINED3DCLEAR_TARGET)
4194 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4195 else
4196 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4198 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4199 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4201 FIXME("No blitter is capable of performing the requested fill operation.\n");
4202 return WINED3DERR_INVALIDCALL;
4205 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4206 return blitter->color_fill(device, view, rect, color);
4207 else
4208 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4211 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4212 unsigned int view_idx)
4214 TRACE("device %p, view_idx %u.\n", device, view_idx);
4216 if (view_idx >= device->adapter->gl_info.limits.buffers)
4218 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4219 return NULL;
4222 return device->fb.render_targets[view_idx];
4225 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4227 TRACE("device %p.\n", device);
4229 return device->fb.depth_stencil;
4232 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4233 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4235 struct wined3d_rendertarget_view *prev;
4237 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4238 device, view_idx, view, set_viewport);
4240 if (view_idx >= device->adapter->gl_info.limits.buffers)
4242 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4243 return WINED3DERR_INVALIDCALL;
4246 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4248 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4249 return WINED3DERR_INVALIDCALL;
4252 /* Set the viewport and scissor rectangles, if requested. Tests show that
4253 * stateblock recording is ignored, the change goes directly into the
4254 * primary stateblock. */
4255 if (!view_idx && set_viewport)
4257 struct wined3d_state *state = &device->state;
4259 state->viewport.x = 0;
4260 state->viewport.y = 0;
4261 state->viewport.width = view->width;
4262 state->viewport.height = view->height;
4263 state->viewport.min_z = 0.0f;
4264 state->viewport.max_z = 1.0f;
4265 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4267 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4268 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4272 prev = device->fb.render_targets[view_idx];
4273 if (view == prev)
4274 return WINED3D_OK;
4276 if (view)
4277 wined3d_rendertarget_view_incref(view);
4278 device->fb.render_targets[view_idx] = view;
4279 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4280 /* Release after the assignment, to prevent device_resource_released()
4281 * from seeing the surface as still in use. */
4282 if (prev)
4283 wined3d_rendertarget_view_decref(prev);
4285 return WINED3D_OK;
4288 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4290 struct wined3d_rendertarget_view *prev;
4292 TRACE("device %p, view %p.\n", device, view);
4294 prev = device->fb.depth_stencil;
4295 if (prev == view)
4297 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4298 return;
4301 if ((device->fb.depth_stencil = view))
4302 wined3d_rendertarget_view_incref(view);
4303 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4304 if (prev)
4305 wined3d_rendertarget_view_decref(prev);
4308 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4309 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4311 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4312 struct wined3d_sub_resource_data data;
4313 struct wined3d_resource_desc desc;
4314 struct wined3d_map_desc map_desc;
4315 struct wined3d_texture *texture;
4316 HRESULT hr;
4318 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4320 ERR("Failed to map source texture.\n");
4321 return NULL;
4324 data.data = map_desc.data;
4325 data.row_pitch = map_desc.row_pitch;
4326 data.slice_pitch = map_desc.slice_pitch;
4328 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4329 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4330 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4331 desc.multisample_quality = 0;
4332 desc.usage = WINED3DUSAGE_DYNAMIC;
4333 desc.pool = WINED3D_POOL_DEFAULT;
4334 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4335 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4336 desc.depth = 1;
4337 desc.size = 0;
4339 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4340 &data, NULL, &wined3d_null_parent_ops, &texture);
4341 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4342 if (FAILED(hr))
4344 ERR("Failed to create cursor texture.\n");
4345 return NULL;
4348 return texture;
4351 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4352 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4354 unsigned int texture_level = sub_resource_idx % texture->level_count;
4355 unsigned int cursor_width, cursor_height;
4356 struct wined3d_display_mode mode;
4357 struct wined3d_map_desc map_desc;
4358 HRESULT hr;
4360 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4361 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4363 if (sub_resource_idx >= texture->level_count * texture->layer_count
4364 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4365 return WINED3DERR_INVALIDCALL;
4367 if (device->cursor_texture)
4369 wined3d_texture_decref(device->cursor_texture);
4370 device->cursor_texture = NULL;
4373 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4375 WARN("Texture %p has invalid format %s.\n",
4376 texture, debug_d3dformat(texture->resource.format->id));
4377 return WINED3DERR_INVALIDCALL;
4380 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4382 ERR("Failed to get display mode, hr %#x.\n", hr);
4383 return WINED3DERR_INVALIDCALL;
4386 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4387 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4388 if (cursor_width > mode.width || cursor_height > mode.height)
4390 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4391 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4392 return WINED3DERR_INVALIDCALL;
4395 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4397 /* Do not store the surface's pointer because the application may
4398 * release it after setting the cursor image. Windows doesn't
4399 * addref the set surface, so we can't do this either without
4400 * creating circular refcount dependencies. */
4401 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4403 ERR("Failed to create cursor texture.\n");
4404 return WINED3DERR_INVALIDCALL;
4407 if (cursor_width == 32 && cursor_height == 32)
4409 UINT mask_size = cursor_width * cursor_height / 8;
4410 ICONINFO cursor_info;
4411 DWORD *mask_bits;
4412 HCURSOR cursor;
4414 /* 32-bit user32 cursors ignore the alpha channel if it's all
4415 * zeroes, and use the mask instead. Fill the mask with all ones
4416 * to ensure we still get a fully transparent cursor. */
4417 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4418 return E_OUTOFMEMORY;
4419 memset(mask_bits, 0xff, mask_size);
4421 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4422 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4423 cursor_info.fIcon = FALSE;
4424 cursor_info.xHotspot = x_hotspot;
4425 cursor_info.yHotspot = y_hotspot;
4426 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4427 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4428 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4430 /* Create our cursor and clean up. */
4431 cursor = CreateIconIndirect(&cursor_info);
4432 if (cursor_info.hbmMask)
4433 DeleteObject(cursor_info.hbmMask);
4434 if (cursor_info.hbmColor)
4435 DeleteObject(cursor_info.hbmColor);
4436 if (device->hardwareCursor)
4437 DestroyCursor(device->hardwareCursor);
4438 device->hardwareCursor = cursor;
4439 if (device->bCursorVisible)
4440 SetCursor(cursor);
4442 HeapFree(GetProcessHeap(), 0, mask_bits);
4445 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4446 device->cursorWidth = cursor_width;
4447 device->cursorHeight = cursor_height;
4448 device->xHotSpot = x_hotspot;
4449 device->yHotSpot = y_hotspot;
4451 return WINED3D_OK;
4454 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4455 int x_screen_space, int y_screen_space, DWORD flags)
4457 TRACE("device %p, x %d, y %d, flags %#x.\n",
4458 device, x_screen_space, y_screen_space, flags);
4460 device->xScreenSpace = x_screen_space;
4461 device->yScreenSpace = y_screen_space;
4463 if (device->hardwareCursor)
4465 POINT pt;
4467 GetCursorPos( &pt );
4468 if (x_screen_space == pt.x && y_screen_space == pt.y)
4469 return;
4470 SetCursorPos( x_screen_space, y_screen_space );
4472 /* Switch to the software cursor if position diverges from the hardware one. */
4473 GetCursorPos( &pt );
4474 if (x_screen_space != pt.x || y_screen_space != pt.y)
4476 if (device->bCursorVisible) SetCursor( NULL );
4477 DestroyCursor( device->hardwareCursor );
4478 device->hardwareCursor = 0;
4483 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4485 BOOL oldVisible = device->bCursorVisible;
4487 TRACE("device %p, show %#x.\n", device, show);
4490 * When ShowCursor is first called it should make the cursor appear at the OS's last
4491 * known cursor position.
4493 if (show && !oldVisible)
4495 POINT pt;
4496 GetCursorPos(&pt);
4497 device->xScreenSpace = pt.x;
4498 device->yScreenSpace = pt.y;
4501 if (device->hardwareCursor)
4503 device->bCursorVisible = show;
4504 if (show)
4505 SetCursor(device->hardwareCursor);
4506 else
4507 SetCursor(NULL);
4509 else if (device->cursor_texture)
4511 device->bCursorVisible = show;
4514 return oldVisible;
4517 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4519 struct wined3d_resource *resource, *cursor;
4521 TRACE("device %p.\n", device);
4523 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4525 TRACE("Checking resource %p for eviction.\n", resource);
4527 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4529 TRACE("Evicting %p.\n", resource);
4530 wined3d_cs_emit_unload_resource(device->cs, resource);
4535 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4537 struct wined3d_resource *resource, *cursor;
4538 const struct wined3d_gl_info *gl_info;
4539 struct wined3d_context *context;
4540 struct wined3d_shader *shader;
4542 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4544 TRACE("Unloading resource %p.\n", resource);
4545 wined3d_cs_emit_unload_resource(device->cs, resource);
4548 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4550 device->shader_backend->shader_destroy(shader);
4553 context = context_acquire(device, NULL);
4554 gl_info = context->gl_info;
4556 if (device->depth_blt_texture)
4558 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4559 device->depth_blt_texture = 0;
4562 device->blitter->free_private(device);
4563 device->shader_backend->shader_free_private(device);
4564 destroy_dummy_textures(device, context);
4565 destroy_default_samplers(device, context);
4567 context_release(context);
4569 while (device->context_count)
4571 if (device->contexts[0]->swapchain)
4572 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4573 else
4574 context_destroy(device, device->contexts[0]);
4577 HeapFree(GetProcessHeap(), 0, swapchain->context);
4578 swapchain->context = NULL;
4581 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4583 struct wined3d_context *context;
4584 struct wined3d_texture *target;
4585 HRESULT hr;
4587 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4588 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4590 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4591 return hr;
4594 if (FAILED(hr = device->blitter->alloc_private(device)))
4596 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4597 device->shader_backend->shader_free_private(device);
4598 return hr;
4601 /* Recreate the primary swapchain's context */
4602 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4603 if (!swapchain->context)
4605 ERR("Failed to allocate memory for swapchain context array.\n");
4606 device->blitter->free_private(device);
4607 device->shader_backend->shader_free_private(device);
4608 return E_OUTOFMEMORY;
4611 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4612 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4614 WARN("Failed to create context.\n");
4615 device->blitter->free_private(device);
4616 device->shader_backend->shader_free_private(device);
4617 HeapFree(GetProcessHeap(), 0, swapchain->context);
4618 return E_FAIL;
4621 swapchain->context[0] = context;
4622 swapchain->num_contexts = 1;
4623 create_dummy_textures(device, context);
4624 create_default_samplers(device, context);
4625 context_release(context);
4627 return WINED3D_OK;
4630 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4631 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4632 wined3d_device_reset_cb callback, BOOL reset_state)
4634 struct wined3d_rendertarget_view_desc view_desc;
4635 struct wined3d_resource *resource, *cursor;
4636 struct wined3d_swapchain *swapchain;
4637 BOOL backbuffer_resized;
4638 HRESULT hr = WINED3D_OK;
4639 unsigned int i;
4641 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4642 device, swapchain_desc, mode, callback, reset_state);
4644 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4646 ERR("Failed to get the first implicit swapchain.\n");
4647 return WINED3DERR_INVALIDCALL;
4650 if (reset_state)
4652 if (device->logo_texture)
4654 wined3d_texture_decref(device->logo_texture);
4655 device->logo_texture = NULL;
4657 if (device->cursor_texture)
4659 wined3d_texture_decref(device->cursor_texture);
4660 device->cursor_texture = NULL;
4662 state_unbind_resources(&device->state);
4665 if (device->fb.render_targets)
4667 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4669 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4672 wined3d_device_set_depth_stencil_view(device, NULL);
4674 if (device->onscreen_depth_stencil)
4676 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4677 device->onscreen_depth_stencil = NULL;
4680 if (reset_state)
4682 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4684 TRACE("Enumerating resource %p.\n", resource);
4685 if (FAILED(hr = callback(resource)))
4686 return hr;
4690 TRACE("New params:\n");
4691 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4692 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4693 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4694 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4695 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4696 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4697 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4698 TRACE("device_window %p\n", swapchain_desc->device_window);
4699 TRACE("windowed %#x\n", swapchain_desc->windowed);
4700 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4701 if (swapchain_desc->enable_auto_depth_stencil)
4702 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4703 TRACE("flags %#x\n", swapchain_desc->flags);
4704 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4705 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4706 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4708 /* No special treatment of these parameters. Just store them */
4709 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4710 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4711 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4712 swapchain->desc.flags = swapchain_desc->flags;
4713 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4714 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4715 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4717 if (swapchain_desc->device_window
4718 && swapchain_desc->device_window != swapchain->desc.device_window)
4720 TRACE("Changing the device window from %p to %p.\n",
4721 swapchain->desc.device_window, swapchain_desc->device_window);
4722 swapchain->desc.device_window = swapchain_desc->device_window;
4723 swapchain->device_window = swapchain_desc->device_window;
4724 wined3d_swapchain_set_window(swapchain, NULL);
4727 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4728 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4730 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4731 || swapchain->reapply_mode || mode
4732 || (!swapchain_desc->windowed && backbuffer_resized))
4734 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4735 return hr;
4737 else if (!swapchain_desc->windowed)
4739 DWORD style = device->style;
4740 DWORD exStyle = device->exStyle;
4741 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4742 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4743 * Reset to clear up their mess. Guild Wars also loses the device during that.
4745 device->style = 0;
4746 device->exStyle = 0;
4747 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4748 swapchain_desc->backbuffer_width,
4749 swapchain_desc->backbuffer_height);
4750 device->style = style;
4751 device->exStyle = exStyle;
4754 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4755 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4756 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4757 return hr;
4759 if (device->auto_depth_stencil_view)
4761 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4762 device->auto_depth_stencil_view = NULL;
4764 if (swapchain->desc.enable_auto_depth_stencil)
4766 struct wined3d_resource_desc texture_desc;
4767 struct wined3d_texture *texture;
4769 TRACE("Creating the depth stencil buffer.\n");
4771 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4772 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4773 texture_desc.multisample_type = swapchain->desc.multisample_type;
4774 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4775 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4776 texture_desc.pool = WINED3D_POOL_DEFAULT;
4777 texture_desc.width = swapchain->desc.backbuffer_width;
4778 texture_desc.height = swapchain->desc.backbuffer_height;
4779 texture_desc.depth = 1;
4780 texture_desc.size = 0;
4782 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4783 device->device_parent, &texture_desc, &texture)))
4785 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4786 return WINED3DERR_INVALIDCALL;
4789 view_desc.format_id = texture->resource.format->id;
4790 view_desc.u.texture.level_idx = 0;
4791 view_desc.u.texture.layer_idx = 0;
4792 view_desc.u.texture.layer_count = 1;
4793 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4794 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4795 wined3d_texture_decref(texture);
4796 if (FAILED(hr))
4798 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4799 return hr;
4802 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4805 if (device->back_buffer_view)
4807 wined3d_rendertarget_view_decref(device->back_buffer_view);
4808 device->back_buffer_view = NULL;
4810 if (swapchain->desc.backbuffer_count)
4812 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4814 view_desc.format_id = back_buffer->format->id;
4815 view_desc.u.texture.level_idx = 0;
4816 view_desc.u.texture.layer_idx = 0;
4817 view_desc.u.texture.layer_count = 1;
4818 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4819 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4821 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4822 return hr;
4826 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4828 if (reset_state)
4830 TRACE("Resetting stateblock.\n");
4831 if (device->recording)
4833 wined3d_stateblock_decref(device->recording);
4834 device->recording = NULL;
4836 wined3d_cs_emit_reset_state(device->cs);
4837 state_cleanup(&device->state);
4839 if (device->d3d_initialized)
4840 delete_opengl_contexts(device, swapchain);
4842 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4843 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4844 device->update_state = &device->state;
4846 device_init_swapchain_state(device, swapchain);
4848 else if (device->back_buffer_view)
4850 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4851 struct wined3d_state *state = &device->state;
4853 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4855 /* Note the min_z / max_z is not reset. */
4856 state->viewport.x = 0;
4857 state->viewport.y = 0;
4858 state->viewport.width = view->width;
4859 state->viewport.height = view->height;
4860 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4862 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4863 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4866 if (device->d3d_initialized)
4868 if (reset_state)
4869 hr = create_primary_opengl_context(device, swapchain);
4870 swapchain_update_swap_interval(swapchain);
4873 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4874 * first use
4876 return hr;
4879 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4881 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4883 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4885 return WINED3D_OK;
4889 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4890 struct wined3d_device_creation_parameters *parameters)
4892 TRACE("device %p, parameters %p.\n", device, parameters);
4894 *parameters = device->create_parms;
4897 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4899 TRACE("device %p.\n", device);
4901 return device->wined3d;
4904 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4905 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4907 struct wined3d_swapchain *swapchain;
4909 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4910 device, swapchain_idx, flags, ramp);
4912 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4913 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4916 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4917 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4919 struct wined3d_swapchain *swapchain;
4921 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4922 device, swapchain_idx, ramp);
4924 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4925 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4928 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4930 TRACE("device %p, resource %p.\n", device, resource);
4932 list_add_head(&device->resources, &resource->resource_list_entry);
4935 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4937 TRACE("device %p, resource %p.\n", device, resource);
4939 list_remove(&resource->resource_list_entry);
4942 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4944 enum wined3d_resource_type type = resource->type;
4945 struct wined3d_rendertarget_view *rtv;
4946 unsigned int i;
4948 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4950 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4952 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4953 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4956 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4957 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4959 switch (type)
4961 case WINED3D_RTYPE_TEXTURE_2D:
4962 case WINED3D_RTYPE_TEXTURE_3D:
4963 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4965 struct wined3d_texture *texture = texture_from_resource(resource);
4967 if (device->state.textures[i] == texture)
4969 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4970 device->state.textures[i] = NULL;
4973 if (device->recording && device->update_state->textures[i] == texture)
4975 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4976 texture, device->recording, i);
4977 device->update_state->textures[i] = NULL;
4980 break;
4982 case WINED3D_RTYPE_BUFFER:
4984 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4986 for (i = 0; i < MAX_STREAMS; ++i)
4988 if (device->state.streams[i].buffer == buffer)
4990 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4991 device->state.streams[i].buffer = NULL;
4994 if (device->recording && device->update_state->streams[i].buffer == buffer)
4996 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4997 buffer, device->recording, i);
4998 device->update_state->streams[i].buffer = NULL;
5002 if (device->state.index_buffer == buffer)
5004 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5005 device->state.index_buffer = NULL;
5008 if (device->recording && device->update_state->index_buffer == buffer)
5010 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5011 buffer, device->recording);
5012 device->update_state->index_buffer = NULL;
5015 break;
5017 default:
5018 break;
5021 /* Remove the resource from the resourceStore */
5022 device_resource_remove(device, resource);
5024 TRACE("Resource released.\n");
5027 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5029 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5031 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5034 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5035 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5036 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5038 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5039 const struct fragment_pipeline *fragment_pipeline;
5040 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5041 unsigned int i;
5042 HRESULT hr;
5044 device->ref = 1;
5045 device->wined3d = wined3d;
5046 wined3d_incref(device->wined3d);
5047 device->adapter = wined3d->adapter_count ? adapter : NULL;
5048 device->device_parent = device_parent;
5049 list_init(&device->resources);
5050 list_init(&device->shaders);
5051 device->surface_alignment = surface_alignment;
5053 /* Save the creation parameters. */
5054 device->create_parms.adapter_idx = adapter_idx;
5055 device->create_parms.device_type = device_type;
5056 device->create_parms.focus_window = focus_window;
5057 device->create_parms.flags = flags;
5059 device->shader_backend = adapter->shader_backend;
5061 vertex_pipeline = adapter->vertex_pipe;
5063 fragment_pipeline = adapter->fragment_pipe;
5065 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5067 if (vertex_pipeline->vp_states && fragment_pipeline->states
5068 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5069 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5070 fragment_pipeline, misc_state_template)))
5072 ERR("Failed to compile state table, hr %#x.\n", hr);
5073 wine_rb_destroy(&device->samplers, NULL, NULL);
5074 wined3d_decref(device->wined3d);
5075 return hr;
5078 device->blitter = adapter->blitter;
5080 state_init(&device->state, &device->fb, &adapter->gl_info,
5081 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5082 device->update_state = &device->state;
5084 if (!(device->cs = wined3d_cs_create(device)))
5086 WARN("Failed to create command stream.\n");
5087 state_cleanup(&device->state);
5088 hr = E_FAIL;
5089 goto err;
5092 return WINED3D_OK;
5094 err:
5095 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5097 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5099 wine_rb_destroy(&device->samplers, NULL, NULL);
5100 wined3d_decref(device->wined3d);
5101 return hr;
5105 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5107 DWORD rep = device->StateTable[state].representative;
5108 struct wined3d_context *context;
5109 DWORD idx;
5110 BYTE shift;
5111 UINT i;
5113 for (i = 0; i < device->context_count; ++i)
5115 context = device->contexts[i];
5116 if(isStateDirty(context, rep)) continue;
5118 context->dirtyArray[context->numDirtyEntries++] = rep;
5119 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5120 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5121 context->isStateDirty[idx] |= (1u << shift);
5125 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5126 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5128 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5130 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5131 window, message, wparam, lparam);
5132 if (unicode)
5133 return DefWindowProcW(window, message, wparam, lparam);
5134 else
5135 return DefWindowProcA(window, message, wparam, lparam);
5138 if (message == WM_DESTROY)
5140 TRACE("unregister window %p.\n", window);
5141 wined3d_unregister_window(window);
5143 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5144 ERR("Window %p is not the focus window for device %p.\n", window, device);
5146 else if (message == WM_DISPLAYCHANGE)
5148 device->device_parent->ops->mode_changed(device->device_parent);
5150 else if (message == WM_ACTIVATEAPP)
5152 UINT i;
5154 for (i = 0; i < device->swapchain_count; i++)
5155 wined3d_swapchain_activate(device->swapchains[i], wparam);
5157 device->device_parent->ops->activate(device->device_parent, wparam);
5159 else if (message == WM_SYSCOMMAND)
5161 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5163 if (unicode)
5164 DefWindowProcW(window, message, wparam, lparam);
5165 else
5166 DefWindowProcA(window, message, wparam, lparam);
5170 if (unicode)
5171 return CallWindowProcW(proc, window, message, wparam, lparam);
5172 else
5173 return CallWindowProcA(proc, window, message, wparam, lparam);