wined3d: Explicitly pass a wined3d_state structure to context_apply_clear_state().
[wine.git] / dlls / wined3d / device.c
blob619ad1eaae26d65a86bbaa4f079778207b4ea75e
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 void device_switch_onscreen_ds(struct wined3d_device *device,
202 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
204 if (device->onscreen_depth_stencil)
206 surface_load_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
208 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
209 device->onscreen_depth_stencil->ds_current_size.cx,
210 device->onscreen_depth_stencil->ds_current_size.cy);
211 wined3d_texture_decref(device->onscreen_depth_stencil->container);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_texture_incref(device->onscreen_depth_stencil->container);
217 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
219 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
220 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
222 /* partial draw rect */
223 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
224 return FALSE;
226 /* partial clear rect */
227 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
228 || clear_rect->right < width || clear_rect->bottom < height))
229 return FALSE;
231 return TRUE;
234 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
235 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
237 struct wined3d_texture_sub_resource *sub_resource = surface_get_sub_resource(ds);
238 RECT current_rect, r;
240 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
242 /* Depth buffer was discarded, make it entirely current in its new location since
243 * there is no other place where we would get data anyway. */
244 SetRect(out_rect, 0, 0,
245 wined3d_texture_get_level_width(ds->container, ds->texture_level),
246 wined3d_texture_get_level_height(ds->container, ds->texture_level));
247 return;
250 if (sub_resource->locations & location)
251 SetRect(&current_rect, 0, 0,
252 ds->ds_current_size.cx,
253 ds->ds_current_size.cy);
254 else
255 SetRectEmpty(&current_rect);
257 IntersectRect(&r, draw_rect, &current_rect);
258 if (EqualRect(&r, draw_rect))
260 /* current_rect ⊇ draw_rect, modify only. */
261 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
262 return;
265 if (EqualRect(&r, &current_rect))
267 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
269 if (!rect_count)
271 /* Full clear, modify only. */
272 *out_rect = *draw_rect;
273 return;
276 IntersectRect(&r, draw_rect, clear_rect);
277 if (EqualRect(&r, draw_rect))
279 /* clear_rect ⊇ draw_rect, modify only. */
280 *out_rect = *draw_rect;
281 return;
285 /* Full load. */
286 surface_load_location(ds, context, location);
287 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
290 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
291 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
292 float depth, DWORD stencil)
294 struct wined3d_surface *target = rt_count ? wined3d_rendertarget_view_get_surface(fb->render_targets[0]) : NULL;
295 struct wined3d_surface *depth_stencil = fb->depth_stencil
296 ? wined3d_rendertarget_view_get_surface(fb->depth_stencil) : NULL;
297 const struct wined3d_state *state = &device->state;
298 const struct wined3d_gl_info *gl_info;
299 UINT drawable_width, drawable_height;
300 struct wined3d_color corrected_color;
301 struct wined3d_context *context;
302 GLbitfield clear_mask = 0;
303 BOOL render_offscreen;
304 unsigned int i;
305 RECT ds_rect;
307 context = context_acquire(device, target);
308 if (!context->valid)
310 context_release(context);
311 WARN("Invalid context, skipping clear.\n");
312 return;
314 gl_info = context->gl_info;
316 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
317 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
318 * for the cleared parts, and the untouched parts.
320 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
321 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
322 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
323 * checking all this if the dest surface is in the drawable anyway. */
324 for (i = 0; i < rt_count; ++i)
326 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
327 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(rtv);
329 if (rt && rtv->format->id != WINED3DFMT_NULL)
331 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
332 surface_load_location(rt, context, rtv->resource->draw_binding);
333 else
334 wined3d_surface_prepare(rt, context, rtv->resource->draw_binding);
338 if (target)
340 render_offscreen = context->render_offscreen;
341 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
343 else
345 render_offscreen = TRUE;
346 drawable_width = depth_stencil->pow2Width;
347 drawable_height = depth_stencil->pow2Height;
350 if (depth_stencil && render_offscreen)
351 wined3d_surface_prepare(depth_stencil, context, depth_stencil->container->resource.draw_binding);
353 if (flags & WINED3DCLEAR_ZBUFFER)
355 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
357 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
358 device_switch_onscreen_ds(device, context, depth_stencil);
359 prepare_ds_clear(depth_stencil, context, location,
360 draw_rect, rect_count, clear_rect, &ds_rect);
363 if (!context_apply_clear_state(context, state, rt_count, fb))
365 context_release(context);
366 WARN("Failed to apply clear state, skipping clear.\n");
367 return;
370 /* Only set the values up once, as they are not changing. */
371 if (flags & WINED3DCLEAR_STENCIL)
373 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
375 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
376 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
378 gl_info->gl_ops.gl.p_glStencilMask(~0U);
379 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
380 gl_info->gl_ops.gl.p_glClearStencil(stencil);
381 checkGLcall("glClearStencil");
382 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
385 if (flags & WINED3DCLEAR_ZBUFFER)
387 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
389 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
391 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
392 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
393 gl_info->gl_ops.gl.p_glClearDepth(depth);
394 checkGLcall("glClearDepth");
395 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
398 if (flags & WINED3DCLEAR_TARGET)
400 for (i = 0; i < rt_count; ++i)
402 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
403 struct wined3d_texture *texture;
405 if (!rtv)
406 continue;
408 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
410 FIXME("Not supported on buffer resources.\n");
411 continue;
414 texture = wined3d_texture_from_resource(rtv->resource);
415 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
416 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
419 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
421 if (rt_count > 1)
422 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
423 "support, this might cause graphical issues.\n");
425 corrected_color.r = color->r < wined3d_srgb_const1[0]
426 ? color->r * wined3d_srgb_const0[3]
427 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
428 - wined3d_srgb_const0[2];
429 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
430 corrected_color.g = color->g < wined3d_srgb_const1[0]
431 ? color->g * wined3d_srgb_const0[3]
432 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
433 - wined3d_srgb_const0[2];
434 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
435 corrected_color.b = color->b < wined3d_srgb_const1[0]
436 ? color->b * wined3d_srgb_const0[3]
437 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
438 - wined3d_srgb_const0[2];
439 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
440 color = &corrected_color;
443 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
444 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
445 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
446 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
447 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
448 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
449 checkGLcall("glClearColor");
450 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
453 if (!rect_count)
455 if (render_offscreen)
457 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
458 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
460 else
462 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
463 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
465 checkGLcall("glScissor");
466 gl_info->gl_ops.gl.p_glClear(clear_mask);
467 checkGLcall("glClear");
469 else
471 RECT current_rect;
473 /* Now process each rect in turn. */
474 for (i = 0; i < rect_count; ++i)
476 /* Note that GL uses lower left, width/height. */
477 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
479 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
480 wine_dbgstr_rect(&clear_rect[i]),
481 wine_dbgstr_rect(&current_rect));
483 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
484 * The rectangle is not cleared, no error is returned, but further rectangles are
485 * still cleared if they are valid. */
486 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
488 TRACE("Rectangle with negative dimensions, ignoring.\n");
489 continue;
492 if (render_offscreen)
494 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
495 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
497 else
499 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
500 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
502 checkGLcall("glScissor");
504 gl_info->gl_ops.gl.p_glClear(clear_mask);
505 checkGLcall("glClear");
509 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
510 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
511 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
513 context_release(context);
516 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
518 ULONG refcount = InterlockedIncrement(&device->ref);
520 TRACE("%p increasing refcount to %u.\n", device, refcount);
522 return refcount;
525 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
527 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
529 ERR("Leftover sampler %p.\n", sampler);
532 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
534 ULONG refcount = InterlockedDecrement(&device->ref);
536 TRACE("%p decreasing refcount to %u.\n", device, refcount);
538 if (!refcount)
540 UINT i;
542 wined3d_cs_destroy(device->cs);
544 if (device->recording && wined3d_stateblock_decref(device->recording))
545 FIXME("Something's still holding the recording stateblock.\n");
546 device->recording = NULL;
548 state_cleanup(&device->state);
550 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
552 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
553 device->multistate_funcs[i] = NULL;
556 if (!list_empty(&device->resources))
558 struct wined3d_resource *resource;
560 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
562 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
564 FIXME("Leftover resource %p with type %s (%#x).\n",
565 resource, debug_d3dresourcetype(resource->type), resource->type);
569 if (device->contexts)
570 ERR("Context array not freed!\n");
571 if (device->hardwareCursor)
572 DestroyCursor(device->hardwareCursor);
573 device->hardwareCursor = 0;
575 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
577 wined3d_decref(device->wined3d);
578 device->wined3d = NULL;
579 HeapFree(GetProcessHeap(), 0, device);
580 TRACE("Freed device %p.\n", device);
583 return refcount;
586 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
588 TRACE("device %p.\n", device);
590 return device->swapchain_count;
593 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
595 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
597 if (swapchain_idx >= device->swapchain_count)
599 WARN("swapchain_idx %u >= swapchain_count %u.\n",
600 swapchain_idx, device->swapchain_count);
601 return NULL;
604 return device->swapchains[swapchain_idx];
607 static void device_load_logo(struct wined3d_device *device, const char *filename)
609 struct wined3d_color_key color_key;
610 struct wined3d_resource_desc desc;
611 HBITMAP hbm;
612 BITMAP bm;
613 HRESULT hr;
614 HDC dcb = NULL, dcs = NULL;
616 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
617 if(hbm)
619 GetObjectA(hbm, sizeof(BITMAP), &bm);
620 dcb = CreateCompatibleDC(NULL);
621 if(!dcb) goto out;
622 SelectObject(dcb, hbm);
624 else
626 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
627 * couldn't be loaded
629 memset(&bm, 0, sizeof(bm));
630 bm.bmWidth = 32;
631 bm.bmHeight = 32;
634 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
635 desc.format = WINED3DFMT_B5G6R5_UNORM;
636 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
637 desc.multisample_quality = 0;
638 desc.usage = WINED3DUSAGE_DYNAMIC;
639 desc.pool = WINED3D_POOL_DEFAULT;
640 desc.width = bm.bmWidth;
641 desc.height = bm.bmHeight;
642 desc.depth = 1;
643 desc.size = 0;
644 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
645 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
647 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
648 goto out;
651 if (dcb)
653 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
654 goto out;
655 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
656 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
658 color_key.color_space_low_value = 0;
659 color_key.color_space_high_value = 0;
660 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
662 else
664 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
665 const RECT rect = {0, 0, desc.width, desc.height};
666 struct wined3d_surface *surface;
668 /* Fill the surface with a white color to show that wined3d is there */
669 surface = device->logo_texture->sub_resources[0].u.surface;
670 surface_color_fill(surface, &rect, &c);
673 out:
674 if (dcb) DeleteDC(dcb);
675 if (hbm) DeleteObject(hbm);
678 /* Context activation is done by the caller. */
679 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
681 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
682 unsigned int i, j, count;
683 /* Under DirectX you can sample even if no texture is bound, whereas
684 * OpenGL will only allow that when a valid texture is bound.
685 * We emulate this by creating dummy textures and binding them
686 * to each texture stage when the currently set D3D texture is NULL. */
688 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
689 for (i = 0; i < count; ++i)
691 static const DWORD color = 0x000000ff;
693 /* Make appropriate texture active */
694 context_active_texture(context, gl_info, i);
696 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
697 checkGLcall("glGenTextures");
698 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
700 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
701 checkGLcall("glBindTexture");
703 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
704 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
705 checkGLcall("glTexImage2D");
707 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
709 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
710 checkGLcall("glGenTextures");
711 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
713 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
714 checkGLcall("glBindTexture");
716 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
717 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
718 checkGLcall("glTexImage2D");
721 if (gl_info->supported[EXT_TEXTURE3D])
723 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
724 checkGLcall("glGenTextures");
725 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
727 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
728 checkGLcall("glBindTexture");
730 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
731 checkGLcall("glTexImage3D");
734 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
736 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
737 checkGLcall("glGenTextures");
738 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
740 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
741 checkGLcall("glBindTexture");
743 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
745 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
746 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
747 checkGLcall("glTexImage2D");
753 /* Context activation is done by the caller. */
754 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
756 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
758 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
760 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
761 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
764 if (gl_info->supported[EXT_TEXTURE3D])
766 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
767 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
770 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
772 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
773 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
776 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
777 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
779 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
780 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
781 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
782 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
785 /* Context activation is done by the caller. */
786 static void create_default_sampler(struct wined3d_device *device)
788 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
791 * In SM4+ shaders there is a separation between resources and samplers. Some of shader
792 * instructions allow to access resources without using samplers.
793 * In GLSL resources are always accessed through sampler or image variables. The default
794 * sampler object is used to emulate the direct resource access when there is no sampler state
795 * to use.
798 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
800 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
801 checkGLcall("glGenSamplers");
802 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
803 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
804 checkGLcall("glSamplerParamteri");
806 else
808 device->default_sampler = 0;
812 /* Context activation is done by the caller. */
813 static void destroy_default_sampler(struct wined3d_device *device)
815 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
817 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
819 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
820 checkGLcall("glDeleteSamplers");
823 device->default_sampler = 0;
826 static LONG fullscreen_style(LONG style)
828 /* Make sure the window is managed, otherwise we won't get keyboard input. */
829 style |= WS_POPUP | WS_SYSMENU;
830 style &= ~(WS_CAPTION | WS_THICKFRAME);
832 return style;
835 static LONG fullscreen_exstyle(LONG exstyle)
837 /* Filter out window decorations. */
838 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
840 return exstyle;
843 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
845 BOOL filter_messages;
846 LONG style, exstyle;
848 TRACE("Setting up window %p for fullscreen mode.\n", window);
850 if (device->style || device->exStyle)
852 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
853 window, device->style, device->exStyle);
856 device->style = GetWindowLongW(window, GWL_STYLE);
857 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
859 style = fullscreen_style(device->style);
860 exstyle = fullscreen_exstyle(device->exStyle);
862 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
863 device->style, device->exStyle, style, exstyle);
865 filter_messages = device->filter_messages;
866 device->filter_messages = TRUE;
868 SetWindowLongW(window, GWL_STYLE, style);
869 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
870 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
872 device->filter_messages = filter_messages;
875 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
877 BOOL filter_messages;
878 LONG style, exstyle;
880 if (!device->style && !device->exStyle) return;
882 style = GetWindowLongW(window, GWL_STYLE);
883 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
885 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
886 * application, and we want to ignore them in the test below, since it's
887 * not the application's fault that they changed. Additionally, we want to
888 * preserve the current status of these flags (i.e. don't restore them) to
889 * more closely emulate the behavior of Direct3D, which leaves these flags
890 * alone when returning to windowed mode. */
891 device->style ^= (device->style ^ style) & WS_VISIBLE;
892 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
894 TRACE("Restoring window style of window %p to %08x, %08x.\n",
895 window, device->style, device->exStyle);
897 filter_messages = device->filter_messages;
898 device->filter_messages = TRUE;
900 /* Only restore the style if the application didn't modify it during the
901 * fullscreen phase. Some applications change it before calling Reset()
902 * when switching between windowed and fullscreen modes (HL2), some
903 * depend on the original style (Eve Online). */
904 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
906 SetWindowLongW(window, GWL_STYLE, device->style);
907 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
909 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
911 device->filter_messages = filter_messages;
913 /* Delete the old values. */
914 device->style = 0;
915 device->exStyle = 0;
918 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
920 TRACE("device %p, window %p.\n", device, window);
922 if (!wined3d_register_window(window, device))
924 ERR("Failed to register window %p.\n", window);
925 return E_FAIL;
928 InterlockedExchangePointer((void **)&device->focus_window, window);
929 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
931 return WINED3D_OK;
934 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
936 TRACE("device %p.\n", device);
938 if (device->focus_window) wined3d_unregister_window(device->focus_window);
939 InterlockedExchangePointer((void **)&device->focus_window, NULL);
942 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
944 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
945 unsigned int i;
947 if (device->fb.render_targets)
949 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
951 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
953 if (device->back_buffer_view)
954 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
957 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
958 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
961 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
962 struct wined3d_swapchain_desc *swapchain_desc)
964 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
965 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
966 struct wined3d_swapchain *swapchain = NULL;
967 struct wined3d_context *context;
968 DWORD clear_flags = 0;
969 HRESULT hr;
971 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
973 if (device->d3d_initialized)
974 return WINED3DERR_INVALIDCALL;
975 if (device->wined3d->flags & WINED3D_NO3D)
976 return WINED3DERR_INVALIDCALL;
978 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
979 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
981 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
982 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
984 TRACE("Shader private data couldn't be allocated\n");
985 goto err_out;
987 if (FAILED(hr = device->blitter->alloc_private(device)))
989 TRACE("Blitter private data couldn't be allocated\n");
990 goto err_out;
993 /* Setup the implicit swapchain. This also initializes a context. */
994 TRACE("Creating implicit swapchain\n");
995 hr = device->device_parent->ops->create_swapchain(device->device_parent,
996 swapchain_desc, &swapchain);
997 if (FAILED(hr))
999 WARN("Failed to create implicit swapchain\n");
1000 goto err_out;
1003 if (swapchain_desc->backbuffer_count)
1005 struct wined3d_rendertarget_view_desc view_desc;
1007 view_desc.format_id = swapchain_desc->backbuffer_format;
1008 view_desc.u.texture.level_idx = 0;
1009 view_desc.u.texture.layer_idx = 0;
1010 view_desc.u.texture.layer_count = 1;
1011 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
1012 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1014 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1015 goto err_out;
1019 device->swapchain_count = 1;
1020 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1021 if (!device->swapchains)
1023 ERR("Out of memory!\n");
1024 goto err_out;
1026 device->swapchains[0] = swapchain;
1027 device_init_swapchain_state(device, swapchain);
1029 context = context_acquire(device, swapchain->front_buffer->sub_resources[0].u.surface);
1031 create_dummy_textures(device, context);
1032 create_default_sampler(device);
1034 device->contexts[0]->last_was_rhw = 0;
1036 TRACE("All defaults now set up, leaving 3D init.\n");
1038 context_release(context);
1040 /* Clear the screen */
1041 if (swapchain->back_buffers && swapchain->back_buffers[0])
1042 clear_flags |= WINED3DCLEAR_TARGET;
1043 if (swapchain_desc->enable_auto_depth_stencil)
1044 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1045 if (clear_flags)
1046 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1048 device->d3d_initialized = TRUE;
1050 if (wined3d_settings.logo)
1051 device_load_logo(device, wined3d_settings.logo);
1052 return WINED3D_OK;
1054 err_out:
1055 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1056 HeapFree(GetProcessHeap(), 0, device->swapchains);
1057 device->swapchain_count = 0;
1058 if (device->back_buffer_view)
1059 wined3d_rendertarget_view_decref(device->back_buffer_view);
1060 if (swapchain)
1061 wined3d_swapchain_decref(swapchain);
1062 if (device->blit_priv)
1063 device->blitter->free_private(device);
1064 if (device->shader_priv)
1065 device->shader_backend->shader_free_private(device);
1067 return hr;
1070 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1071 struct wined3d_swapchain_desc *swapchain_desc)
1073 struct wined3d_swapchain *swapchain = NULL;
1074 HRESULT hr;
1076 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1078 /* Setup the implicit swapchain */
1079 TRACE("Creating implicit swapchain\n");
1080 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1081 swapchain_desc, &swapchain);
1082 if (FAILED(hr))
1084 WARN("Failed to create implicit swapchain\n");
1085 goto err_out;
1088 device->swapchain_count = 1;
1089 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1090 if (!device->swapchains)
1092 ERR("Out of memory!\n");
1093 goto err_out;
1095 device->swapchains[0] = swapchain;
1096 return WINED3D_OK;
1098 err_out:
1099 wined3d_swapchain_decref(swapchain);
1100 return hr;
1103 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1105 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1107 wined3d_sampler_decref(sampler);
1110 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1112 struct wined3d_resource *resource, *cursor;
1113 const struct wined3d_gl_info *gl_info;
1114 struct wined3d_context *context;
1115 struct wined3d_surface *surface;
1116 UINT i;
1118 TRACE("device %p.\n", device);
1120 if (!device->d3d_initialized)
1121 return WINED3DERR_INVALIDCALL;
1123 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1124 * it was created. Thus make sure a context is active for the glDelete* calls
1126 context = context_acquire(device, NULL);
1127 gl_info = context->gl_info;
1129 if (device->logo_texture)
1130 wined3d_texture_decref(device->logo_texture);
1131 if (device->cursor_texture)
1132 wined3d_texture_decref(device->cursor_texture);
1134 state_unbind_resources(&device->state);
1136 /* Unload resources */
1137 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1139 TRACE("Unloading resource %p.\n", resource);
1141 resource->resource_ops->resource_unload(resource);
1144 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1146 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1147 * private data, it might contain opengl pointers
1149 if (device->depth_blt_texture)
1151 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1152 device->depth_blt_texture = 0;
1155 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1156 device->blitter->free_private(device);
1157 device->shader_backend->shader_free_private(device);
1158 destroy_dummy_textures(device, gl_info);
1159 destroy_default_sampler(device);
1161 /* Release the context again as soon as possible. In particular,
1162 * releasing the render target views below may release the last reference
1163 * to the swapchain associated with this context, which in turn will
1164 * destroy the context. */
1165 context_release(context);
1167 /* Release the buffers (with sanity checks)*/
1168 if (device->onscreen_depth_stencil)
1170 surface = device->onscreen_depth_stencil;
1171 device->onscreen_depth_stencil = NULL;
1172 wined3d_texture_decref(surface->container);
1175 if (device->fb.depth_stencil)
1177 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1179 TRACE("Releasing depth/stencil view %p.\n", view);
1181 device->fb.depth_stencil = NULL;
1182 wined3d_rendertarget_view_decref(view);
1185 if (device->auto_depth_stencil_view)
1187 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1189 device->auto_depth_stencil_view = NULL;
1190 if (wined3d_rendertarget_view_decref(view))
1191 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1194 for (i = 0; i < gl_info->limits.buffers; ++i)
1196 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1198 if (device->back_buffer_view)
1200 wined3d_rendertarget_view_decref(device->back_buffer_view);
1201 device->back_buffer_view = NULL;
1204 for (i = 0; i < device->swapchain_count; ++i)
1206 TRACE("Releasing the implicit swapchain %u.\n", i);
1207 if (wined3d_swapchain_decref(device->swapchains[i]))
1208 FIXME("Something's still holding the implicit swapchain.\n");
1211 HeapFree(GetProcessHeap(), 0, device->swapchains);
1212 device->swapchains = NULL;
1213 device->swapchain_count = 0;
1215 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1216 device->fb.render_targets = NULL;
1218 device->d3d_initialized = FALSE;
1220 return WINED3D_OK;
1223 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1225 unsigned int i;
1227 for (i = 0; i < device->swapchain_count; ++i)
1229 TRACE("Releasing the implicit swapchain %u.\n", i);
1230 if (wined3d_swapchain_decref(device->swapchains[i]))
1231 FIXME("Something's still holding the implicit swapchain.\n");
1234 HeapFree(GetProcessHeap(), 0, device->swapchains);
1235 device->swapchains = NULL;
1236 device->swapchain_count = 0;
1237 return WINED3D_OK;
1240 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1241 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1242 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1244 * There is no way to deactivate thread safety once it is enabled.
1246 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1248 TRACE("device %p.\n", device);
1250 /* For now just store the flag (needed in case of ddraw). */
1251 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1254 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1256 TRACE("device %p.\n", device);
1258 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1259 wine_dbgstr_longlong(device->adapter->vram_bytes),
1260 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1261 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1263 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1266 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1267 struct wined3d_buffer *buffer, UINT offset)
1269 struct wined3d_stream_output *stream;
1270 struct wined3d_buffer *prev_buffer;
1272 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1274 if (idx >= MAX_STREAM_OUT)
1276 WARN("Invalid stream output %u.\n", idx);
1277 return;
1280 stream = &device->update_state->stream_output[idx];
1281 prev_buffer = stream->buffer;
1283 if (buffer)
1284 wined3d_buffer_incref(buffer);
1285 stream->buffer = buffer;
1286 stream->offset = offset;
1287 if (!device->recording)
1288 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1289 if (prev_buffer)
1290 wined3d_buffer_decref(prev_buffer);
1293 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1294 UINT idx, UINT *offset)
1296 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1298 if (idx >= MAX_STREAM_OUT)
1300 WARN("Invalid stream output %u.\n", idx);
1301 return NULL;
1304 if (offset)
1305 *offset = device->state.stream_output[idx].offset;
1306 return device->state.stream_output[idx].buffer;
1309 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1310 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1312 struct wined3d_stream_state *stream;
1313 struct wined3d_buffer *prev_buffer;
1315 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1316 device, stream_idx, buffer, offset, stride);
1318 if (stream_idx >= MAX_STREAMS)
1320 WARN("Stream index %u out of range.\n", stream_idx);
1321 return WINED3DERR_INVALIDCALL;
1323 else if (offset & 0x3)
1325 WARN("Offset %u is not 4 byte aligned.\n", offset);
1326 return WINED3DERR_INVALIDCALL;
1329 stream = &device->update_state->streams[stream_idx];
1330 prev_buffer = stream->buffer;
1332 if (device->recording)
1333 device->recording->changed.streamSource |= 1u << stream_idx;
1335 if (prev_buffer == buffer
1336 && stream->stride == stride
1337 && stream->offset == offset)
1339 TRACE("Application is setting the old values over, nothing to do.\n");
1340 return WINED3D_OK;
1343 stream->buffer = buffer;
1344 if (buffer)
1346 stream->stride = stride;
1347 stream->offset = offset;
1348 wined3d_buffer_incref(buffer);
1351 if (!device->recording)
1352 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1353 if (prev_buffer)
1354 wined3d_buffer_decref(prev_buffer);
1356 return WINED3D_OK;
1359 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1360 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1362 const struct wined3d_stream_state *stream;
1364 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1365 device, stream_idx, buffer, offset, stride);
1367 if (stream_idx >= MAX_STREAMS)
1369 WARN("Stream index %u out of range.\n", stream_idx);
1370 return WINED3DERR_INVALIDCALL;
1373 stream = &device->state.streams[stream_idx];
1374 *buffer = stream->buffer;
1375 if (offset)
1376 *offset = stream->offset;
1377 *stride = stream->stride;
1379 return WINED3D_OK;
1382 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1384 struct wined3d_stream_state *stream;
1385 UINT old_flags, old_freq;
1387 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1389 /* Verify input. At least in d3d9 this is invalid. */
1390 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1392 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1393 return WINED3DERR_INVALIDCALL;
1395 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1397 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1398 return WINED3DERR_INVALIDCALL;
1400 if (!divider)
1402 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1403 return WINED3DERR_INVALIDCALL;
1406 stream = &device->update_state->streams[stream_idx];
1407 old_flags = stream->flags;
1408 old_freq = stream->frequency;
1410 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1411 stream->frequency = divider & 0x7fffff;
1413 if (device->recording)
1414 device->recording->changed.streamFreq |= 1u << stream_idx;
1415 else if (stream->frequency != old_freq || stream->flags != old_flags)
1416 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1418 return WINED3D_OK;
1421 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1422 UINT stream_idx, UINT *divider)
1424 const struct wined3d_stream_state *stream;
1426 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1428 stream = &device->state.streams[stream_idx];
1429 *divider = stream->flags | stream->frequency;
1431 TRACE("Returning %#x.\n", *divider);
1433 return WINED3D_OK;
1436 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1437 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1439 TRACE("device %p, state %s, matrix %p.\n",
1440 device, debug_d3dtstype(d3dts), matrix);
1441 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1442 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1443 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1444 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1446 /* Handle recording of state blocks. */
1447 if (device->recording)
1449 TRACE("Recording... not performing anything.\n");
1450 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1451 device->update_state->transforms[d3dts] = *matrix;
1452 return;
1455 /* If the new matrix is the same as the current one,
1456 * we cut off any further processing. this seems to be a reasonable
1457 * optimization because as was noticed, some apps (warcraft3 for example)
1458 * tend towards setting the same matrix repeatedly for some reason.
1460 * From here on we assume that the new matrix is different, wherever it matters. */
1461 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1463 TRACE("The application is setting the same matrix over again.\n");
1464 return;
1467 device->state.transforms[d3dts] = *matrix;
1468 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1471 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1472 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1474 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1476 *matrix = device->state.transforms[state];
1479 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1480 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1482 const struct wined3d_matrix *mat;
1483 struct wined3d_matrix temp;
1485 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1487 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1488 * below means it will be recorded in a state block change, but it
1489 * works regardless where it is recorded.
1490 * If this is found to be wrong, change to StateBlock. */
1491 if (state > HIGHEST_TRANSFORMSTATE)
1493 WARN("Unhandled transform state %#x.\n", state);
1494 return;
1497 mat = &device->update_state->transforms[state];
1498 multiply_matrix(&temp, mat, matrix);
1500 /* Apply change via set transform - will reapply to eg. lights this way. */
1501 wined3d_device_set_transform(device, state, &temp);
1504 /* Note lights are real special cases. Although the device caps state only
1505 * e.g. 8 are supported, you can reference any indexes you want as long as
1506 * that number max are enabled at any one point in time. Therefore since the
1507 * indices can be anything, we need a hashmap of them. However, this causes
1508 * stateblock problems. When capturing the state block, I duplicate the
1509 * hashmap, but when recording, just build a chain pretty much of commands to
1510 * be replayed. */
1511 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1512 UINT light_idx, const struct wined3d_light *light)
1514 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1515 struct wined3d_light_info *object = NULL;
1516 struct list *e;
1517 float rho;
1519 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1521 /* Check the parameter range. Need for speed most wanted sets junk lights
1522 * which confuse the GL driver. */
1523 if (!light)
1524 return WINED3DERR_INVALIDCALL;
1526 switch (light->type)
1528 case WINED3D_LIGHT_POINT:
1529 case WINED3D_LIGHT_SPOT:
1530 case WINED3D_LIGHT_GLSPOT:
1531 /* Incorrect attenuation values can cause the gl driver to crash.
1532 * Happens with Need for speed most wanted. */
1533 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1535 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1536 return WINED3DERR_INVALIDCALL;
1538 break;
1540 case WINED3D_LIGHT_DIRECTIONAL:
1541 case WINED3D_LIGHT_PARALLELPOINT:
1542 /* Ignores attenuation */
1543 break;
1545 default:
1546 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1547 return WINED3DERR_INVALIDCALL;
1550 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1552 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1553 if (object->OriginalIndex == light_idx)
1554 break;
1555 object = NULL;
1558 if (!object)
1560 TRACE("Adding new light\n");
1561 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1562 if (!object)
1563 return E_OUTOFMEMORY;
1565 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1566 object->glIndex = -1;
1567 object->OriginalIndex = light_idx;
1570 /* Initialize the object. */
1571 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1572 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1573 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1574 light_idx, light->type, debug_color(&light->diffuse),
1575 debug_color(&light->specular), debug_color(&light->ambient),
1576 light->position.x, light->position.y, light->position.z,
1577 light->direction.x, light->direction.y, light->direction.z,
1578 light->range, light->falloff, light->theta, light->phi);
1580 /* Update the live definitions if the light is currently assigned a glIndex. */
1581 if (object->glIndex != -1 && !device->recording)
1583 if (object->OriginalParms.type != light->type)
1584 device_invalidate_state(device, STATE_LIGHT_TYPE);
1585 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1588 /* Save away the information. */
1589 object->OriginalParms = *light;
1591 switch (light->type)
1593 case WINED3D_LIGHT_POINT:
1594 /* Position */
1595 object->position.x = light->position.x;
1596 object->position.y = light->position.y;
1597 object->position.z = light->position.z;
1598 object->position.w = 1.0f;
1599 object->cutoff = 180.0f;
1600 /* FIXME: Range */
1601 break;
1603 case WINED3D_LIGHT_DIRECTIONAL:
1604 /* Direction */
1605 object->direction.x = -light->direction.x;
1606 object->direction.y = -light->direction.y;
1607 object->direction.z = -light->direction.z;
1608 object->direction.w = 0.0f;
1609 object->exponent = 0.0f;
1610 object->cutoff = 180.0f;
1611 break;
1613 case WINED3D_LIGHT_SPOT:
1614 /* Position */
1615 object->position.x = light->position.x;
1616 object->position.y = light->position.y;
1617 object->position.z = light->position.z;
1618 object->position.w = 1.0f;
1620 /* Direction */
1621 object->direction.x = light->direction.x;
1622 object->direction.y = light->direction.y;
1623 object->direction.z = light->direction.z;
1624 object->direction.w = 0.0f;
1626 /* opengl-ish and d3d-ish spot lights use too different models
1627 * for the light "intensity" as a function of the angle towards
1628 * the main light direction, so we only can approximate very
1629 * roughly. However, spot lights are rather rarely used in games
1630 * (if ever used at all). Furthermore if still used, probably
1631 * nobody pays attention to such details. */
1632 if (!light->falloff)
1634 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1635 * equations have the falloff resp. exponent parameter as an
1636 * exponent, so the spot light lighting will always be 1.0 for
1637 * both of them, and we don't have to care for the rest of the
1638 * rather complex calculation. */
1639 object->exponent = 0.0f;
1641 else
1643 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1644 if (rho < 0.0001f)
1645 rho = 0.0001f;
1646 object->exponent = -0.3f / logf(cosf(rho / 2));
1649 if (object->exponent > 128.0f)
1650 object->exponent = 128.0f;
1652 object->cutoff = (float)(light->phi * 90 / M_PI);
1653 /* FIXME: Range */
1654 break;
1656 case WINED3D_LIGHT_PARALLELPOINT:
1657 object->position.x = light->position.x;
1658 object->position.y = light->position.y;
1659 object->position.z = light->position.z;
1660 object->position.w = 1.0f;
1661 break;
1663 default:
1664 FIXME("Unrecognized light type %#x.\n", light->type);
1667 return WINED3D_OK;
1670 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1671 UINT light_idx, struct wined3d_light *light)
1673 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1674 struct wined3d_light_info *light_info = NULL;
1675 struct list *e;
1677 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1679 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1681 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1682 if (light_info->OriginalIndex == light_idx)
1683 break;
1684 light_info = NULL;
1687 if (!light_info)
1689 TRACE("Light information requested but light not defined\n");
1690 return WINED3DERR_INVALIDCALL;
1693 *light = light_info->OriginalParms;
1694 return WINED3D_OK;
1697 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1699 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1700 struct wined3d_light_info *light_info = NULL;
1701 struct list *e;
1703 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1705 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1707 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1708 if (light_info->OriginalIndex == light_idx)
1709 break;
1710 light_info = NULL;
1712 TRACE("Found light %p.\n", light_info);
1714 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1715 if (!light_info)
1717 TRACE("Light enabled requested but light not defined, so defining one!\n");
1718 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1720 /* Search for it again! Should be fairly quick as near head of list. */
1721 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1723 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1724 if (light_info->OriginalIndex == light_idx)
1725 break;
1726 light_info = NULL;
1728 if (!light_info)
1730 FIXME("Adding default lights has failed dismally\n");
1731 return WINED3DERR_INVALIDCALL;
1735 if (!enable)
1737 if (light_info->glIndex != -1)
1739 if (!device->recording)
1741 device_invalidate_state(device, STATE_LIGHT_TYPE);
1742 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1745 device->update_state->lights[light_info->glIndex] = NULL;
1746 light_info->glIndex = -1;
1748 else
1750 TRACE("Light already disabled, nothing to do\n");
1752 light_info->enabled = FALSE;
1754 else
1756 light_info->enabled = TRUE;
1757 if (light_info->glIndex != -1)
1759 TRACE("Nothing to do as light was enabled\n");
1761 else
1763 unsigned int i;
1764 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1765 /* Find a free GL light. */
1766 for (i = 0; i < gl_info->limits.lights; ++i)
1768 if (!device->update_state->lights[i])
1770 device->update_state->lights[i] = light_info;
1771 light_info->glIndex = i;
1772 break;
1775 if (light_info->glIndex == -1)
1777 /* Our tests show that Windows returns D3D_OK in this situation, even with
1778 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1779 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1780 * as well for those lights.
1782 * TODO: Test how this affects rendering. */
1783 WARN("Too many concurrently active lights\n");
1784 return WINED3D_OK;
1787 /* i == light_info->glIndex */
1788 if (!device->recording)
1790 device_invalidate_state(device, STATE_LIGHT_TYPE);
1791 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1796 return WINED3D_OK;
1799 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1801 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1802 struct wined3d_light_info *light_info = NULL;
1803 struct list *e;
1805 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1807 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1809 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1810 if (light_info->OriginalIndex == light_idx)
1811 break;
1812 light_info = NULL;
1815 if (!light_info)
1817 TRACE("Light enabled state requested but light not defined.\n");
1818 return WINED3DERR_INVALIDCALL;
1820 /* true is 128 according to SetLightEnable */
1821 *enable = light_info->enabled ? 128 : 0;
1822 return WINED3D_OK;
1825 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1826 UINT plane_idx, const struct wined3d_vec4 *plane)
1828 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1830 /* Validate plane_idx. */
1831 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1833 TRACE("Application has requested clipplane this device doesn't support.\n");
1834 return WINED3DERR_INVALIDCALL;
1837 if (device->recording)
1838 device->recording->changed.clipplane |= 1u << plane_idx;
1840 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1842 TRACE("Application is setting old values over, nothing to do.\n");
1843 return WINED3D_OK;
1846 device->update_state->clip_planes[plane_idx] = *plane;
1848 if (!device->recording)
1849 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1851 return WINED3D_OK;
1854 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1855 UINT plane_idx, struct wined3d_vec4 *plane)
1857 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1859 /* Validate plane_idx. */
1860 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1862 TRACE("Application has requested clipplane this device doesn't support.\n");
1863 return WINED3DERR_INVALIDCALL;
1866 *plane = device->state.clip_planes[plane_idx];
1868 return WINED3D_OK;
1871 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1872 const struct wined3d_clip_status *clip_status)
1874 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1876 if (!clip_status)
1877 return WINED3DERR_INVALIDCALL;
1879 return WINED3D_OK;
1882 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1883 struct wined3d_clip_status *clip_status)
1885 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1887 if (!clip_status)
1888 return WINED3DERR_INVALIDCALL;
1890 return WINED3D_OK;
1893 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1895 TRACE("device %p, material %p.\n", device, material);
1897 device->update_state->material = *material;
1899 if (device->recording)
1900 device->recording->changed.material = TRUE;
1901 else
1902 wined3d_cs_emit_set_material(device->cs, material);
1905 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1907 TRACE("device %p, material %p.\n", device, material);
1909 *material = device->state.material;
1911 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1912 TRACE("ambient %s\n", debug_color(&material->ambient));
1913 TRACE("specular %s\n", debug_color(&material->specular));
1914 TRACE("emissive %s\n", debug_color(&material->emissive));
1915 TRACE("power %.8e.\n", material->power);
1918 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1919 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1921 enum wined3d_format_id prev_format;
1922 struct wined3d_buffer *prev_buffer;
1924 TRACE("device %p, buffer %p, format %s.\n",
1925 device, buffer, debug_d3dformat(format_id));
1927 prev_buffer = device->update_state->index_buffer;
1928 prev_format = device->update_state->index_format;
1930 device->update_state->index_buffer = buffer;
1931 device->update_state->index_format = format_id;
1933 if (device->recording)
1934 device->recording->changed.indices = TRUE;
1936 if (prev_buffer == buffer && prev_format == format_id)
1937 return;
1939 if (buffer)
1940 wined3d_buffer_incref(buffer);
1941 if (!device->recording)
1942 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1943 if (prev_buffer)
1944 wined3d_buffer_decref(prev_buffer);
1947 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1948 enum wined3d_format_id *format)
1950 TRACE("device %p, format %p.\n", device, format);
1952 *format = device->state.index_format;
1953 return device->state.index_buffer;
1956 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1958 TRACE("device %p, base_index %d.\n", device, base_index);
1960 device->update_state->base_vertex_index = base_index;
1963 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1965 TRACE("device %p.\n", device);
1967 return device->state.base_vertex_index;
1970 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1972 TRACE("device %p, viewport %p.\n", device, viewport);
1973 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1974 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1976 device->update_state->viewport = *viewport;
1978 /* Handle recording of state blocks */
1979 if (device->recording)
1981 TRACE("Recording... not performing anything\n");
1982 device->recording->changed.viewport = TRUE;
1983 return;
1986 wined3d_cs_emit_set_viewport(device->cs, viewport);
1989 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1991 TRACE("device %p, viewport %p.\n", device, viewport);
1993 *viewport = device->state.viewport;
1996 static void resolve_depth_buffer(struct wined3d_state *state)
1998 struct wined3d_texture *dst_texture = state->textures[0];
1999 struct wined3d_rendertarget_view *src_view;
2000 RECT src_rect, dst_rect;
2002 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2003 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2004 return;
2006 if (!(src_view = state->fb->depth_stencil))
2007 return;
2008 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2010 FIXME("Not supported on buffer resources.\n");
2011 return;
2014 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2015 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2016 wined3d_texture_blt(dst_texture, 0, &dst_rect, wined3d_texture_from_resource(src_view->resource),
2017 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2020 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2021 enum wined3d_render_state state, DWORD value)
2023 DWORD old_value;
2025 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2027 if (state > WINEHIGHEST_RENDER_STATE)
2029 WARN("Unhandled render state %#x.\n", state);
2030 return;
2033 old_value = device->state.render_states[state];
2034 device->update_state->render_states[state] = value;
2036 /* Handle recording of state blocks. */
2037 if (device->recording)
2039 TRACE("Recording... not performing anything.\n");
2040 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2041 return;
2044 /* Compared here and not before the assignment to allow proper stateblock recording. */
2045 if (value == old_value)
2046 TRACE("Application is setting the old value over, nothing to do.\n");
2047 else
2048 wined3d_cs_emit_set_render_state(device->cs, state, value);
2050 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2052 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2053 resolve_depth_buffer(&device->state);
2057 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2059 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2061 return device->state.render_states[state];
2064 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2065 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2067 DWORD old_value;
2069 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2070 device, sampler_idx, debug_d3dsamplerstate(state), value);
2072 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2073 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2075 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2077 WARN("Invalid sampler %u.\n", sampler_idx);
2078 return; /* Windows accepts overflowing this array ... we do not. */
2081 old_value = device->state.sampler_states[sampler_idx][state];
2082 device->update_state->sampler_states[sampler_idx][state] = value;
2084 /* Handle recording of state blocks. */
2085 if (device->recording)
2087 TRACE("Recording... not performing anything.\n");
2088 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2089 return;
2092 if (old_value == value)
2094 TRACE("Application is setting the old value over, nothing to do.\n");
2095 return;
2098 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2101 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2102 UINT sampler_idx, enum wined3d_sampler_state state)
2104 TRACE("device %p, sampler_idx %u, state %s.\n",
2105 device, sampler_idx, debug_d3dsamplerstate(state));
2107 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2108 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2110 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2112 WARN("Invalid sampler %u.\n", sampler_idx);
2113 return 0; /* Windows accepts overflowing this array ... we do not. */
2116 return device->state.sampler_states[sampler_idx][state];
2119 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2121 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2123 if (device->recording)
2124 device->recording->changed.scissorRect = TRUE;
2126 if (EqualRect(&device->update_state->scissor_rect, rect))
2128 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2129 return;
2131 CopyRect(&device->update_state->scissor_rect, rect);
2133 if (device->recording)
2135 TRACE("Recording... not performing anything.\n");
2136 return;
2139 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2142 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2144 TRACE("device %p, rect %p.\n", device, rect);
2146 *rect = device->state.scissor_rect;
2147 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2150 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2151 struct wined3d_vertex_declaration *declaration)
2153 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2155 TRACE("device %p, declaration %p.\n", device, declaration);
2157 if (device->recording)
2158 device->recording->changed.vertexDecl = TRUE;
2160 if (declaration == prev)
2161 return;
2163 if (declaration)
2164 wined3d_vertex_declaration_incref(declaration);
2165 device->update_state->vertex_declaration = declaration;
2166 if (!device->recording)
2167 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2168 if (prev)
2169 wined3d_vertex_declaration_decref(prev);
2172 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2174 TRACE("device %p.\n", device);
2176 return device->state.vertex_declaration;
2179 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2181 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2183 TRACE("device %p, shader %p.\n", device, shader);
2185 if (device->recording)
2186 device->recording->changed.vertexShader = TRUE;
2188 if (shader == prev)
2189 return;
2191 if (shader)
2192 wined3d_shader_incref(shader);
2193 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2194 if (!device->recording)
2195 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2196 if (prev)
2197 wined3d_shader_decref(prev);
2200 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2202 TRACE("device %p.\n", device);
2204 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2207 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2208 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2210 struct wined3d_buffer *prev;
2212 if (idx >= MAX_CONSTANT_BUFFERS)
2214 WARN("Invalid constant buffer index %u.\n", idx);
2215 return;
2218 prev = device->update_state->cb[type][idx];
2219 if (buffer == prev)
2220 return;
2222 if (buffer)
2223 wined3d_buffer_incref(buffer);
2224 device->update_state->cb[type][idx] = buffer;
2225 if (!device->recording)
2226 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2227 if (prev)
2228 wined3d_buffer_decref(prev);
2231 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2233 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2235 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2238 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2240 TRACE("device %p, idx %u.\n", device, idx);
2242 if (idx >= MAX_CONSTANT_BUFFERS)
2244 WARN("Invalid constant buffer index %u.\n", idx);
2245 return NULL;
2248 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2251 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2252 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2254 struct wined3d_shader_resource_view *prev;
2256 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2258 WARN("Invalid view index %u.\n", idx);
2259 return;
2262 prev = device->update_state->shader_resource_view[type][idx];
2263 if (view == prev)
2264 return;
2266 if (view)
2267 wined3d_shader_resource_view_incref(view);
2268 device->update_state->shader_resource_view[type][idx] = view;
2269 if (!device->recording)
2270 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2271 if (prev)
2272 wined3d_shader_resource_view_decref(prev);
2275 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2276 UINT idx, struct wined3d_shader_resource_view *view)
2278 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2280 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2283 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2284 UINT idx)
2286 TRACE("device %p, idx %u.\n", device, idx);
2288 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2290 WARN("Invalid view index %u.\n", idx);
2291 return NULL;
2294 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2297 static void wined3d_device_set_sampler(struct wined3d_device *device,
2298 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2300 struct wined3d_sampler *prev;
2302 if (idx >= MAX_SAMPLER_OBJECTS)
2304 WARN("Invalid sampler index %u.\n", idx);
2305 return;
2308 prev = device->update_state->sampler[type][idx];
2309 if (sampler == prev)
2310 return;
2312 if (sampler)
2313 wined3d_sampler_incref(sampler);
2314 device->update_state->sampler[type][idx] = sampler;
2315 if (!device->recording)
2316 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2317 if (prev)
2318 wined3d_sampler_decref(prev);
2321 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2323 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2325 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2328 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2330 TRACE("device %p, idx %u.\n", device, idx);
2332 if (idx >= MAX_SAMPLER_OBJECTS)
2334 WARN("Invalid sampler index %u.\n", idx);
2335 return NULL;
2338 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2341 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2343 UINT i;
2345 for (i = 0; i < device->context_count; ++i)
2347 device->contexts[i]->constant_update_mask |= mask;
2351 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2352 UINT start_register, const BOOL *constants, UINT bool_count)
2354 UINT count = min(bool_count, MAX_CONST_B - start_register);
2355 UINT i;
2357 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2358 device, start_register, constants, bool_count);
2360 if (!constants || start_register >= MAX_CONST_B)
2361 return WINED3DERR_INVALIDCALL;
2363 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2364 for (i = 0; i < count; ++i)
2365 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2367 if (device->recording)
2369 for (i = start_register; i < count + start_register; ++i)
2370 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2372 else
2374 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2377 return WINED3D_OK;
2380 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2381 UINT start_register, BOOL *constants, UINT bool_count)
2383 UINT count = min(bool_count, MAX_CONST_B - start_register);
2385 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2386 device, start_register, constants, bool_count);
2388 if (!constants || start_register >= MAX_CONST_B)
2389 return WINED3DERR_INVALIDCALL;
2391 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2393 return WINED3D_OK;
2396 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2397 UINT start_register, const int *constants, UINT vector4i_count)
2399 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2400 UINT i;
2402 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2403 device, start_register, constants, vector4i_count);
2405 if (!constants || start_register >= MAX_CONST_I)
2406 return WINED3DERR_INVALIDCALL;
2408 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2409 for (i = 0; i < count; ++i)
2410 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2411 constants[i * 4], constants[i * 4 + 1],
2412 constants[i * 4 + 2], constants[i * 4 + 3]);
2414 if (device->recording)
2416 for (i = start_register; i < count + start_register; ++i)
2417 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2419 else
2421 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2424 return WINED3D_OK;
2427 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2428 UINT start_register, int *constants, UINT vector4i_count)
2430 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2432 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2433 device, start_register, constants, vector4i_count);
2435 if (!constants || start_register >= MAX_CONST_I)
2436 return WINED3DERR_INVALIDCALL;
2438 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2439 return WINED3D_OK;
2442 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2443 UINT start_register, const float *constants, UINT vector4f_count)
2445 UINT i;
2446 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2448 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2449 device, start_register, constants, vector4f_count);
2451 /* Specifically test start_register > limit to catch MAX_UINT overflows
2452 * when adding start_register + vector4f_count. */
2453 if (!constants
2454 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2455 || start_register > d3d_info->limits.vs_uniform_count)
2456 return WINED3DERR_INVALIDCALL;
2458 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2459 constants, vector4f_count * sizeof(float) * 4);
2460 if (TRACE_ON(d3d))
2462 for (i = 0; i < vector4f_count; ++i)
2463 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2464 constants[i * 4], constants[i * 4 + 1],
2465 constants[i * 4 + 2], constants[i * 4 + 3]);
2468 if (device->recording)
2469 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2470 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2471 else
2472 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2475 return WINED3D_OK;
2478 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2479 UINT start_register, float *constants, UINT vector4f_count)
2481 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2482 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2484 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2485 device, start_register, constants, vector4f_count);
2487 if (!constants || count < 0)
2488 return WINED3DERR_INVALIDCALL;
2490 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2492 return WINED3D_OK;
2495 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2497 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2499 TRACE("device %p, shader %p.\n", device, shader);
2501 if (device->recording)
2502 device->recording->changed.pixelShader = TRUE;
2504 if (shader == prev)
2505 return;
2507 if (shader)
2508 wined3d_shader_incref(shader);
2509 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2510 if (!device->recording)
2511 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2512 if (prev)
2513 wined3d_shader_decref(prev);
2516 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2518 TRACE("device %p.\n", device);
2520 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2523 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2525 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2527 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2530 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2532 TRACE("device %p, idx %u.\n", device, idx);
2534 if (idx >= MAX_CONSTANT_BUFFERS)
2536 WARN("Invalid constant buffer index %u.\n", idx);
2537 return NULL;
2540 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2543 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2544 UINT idx, struct wined3d_shader_resource_view *view)
2546 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2548 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2551 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2552 UINT idx)
2554 TRACE("device %p, idx %u.\n", device, idx);
2556 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2558 WARN("Invalid view index %u.\n", idx);
2559 return NULL;
2562 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2565 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2567 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2569 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2572 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2574 TRACE("device %p, idx %u.\n", device, idx);
2576 if (idx >= MAX_SAMPLER_OBJECTS)
2578 WARN("Invalid sampler index %u.\n", idx);
2579 return NULL;
2582 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2585 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2586 UINT start_register, const BOOL *constants, UINT bool_count)
2588 UINT count = min(bool_count, MAX_CONST_B - start_register);
2589 UINT i;
2591 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2592 device, start_register, constants, bool_count);
2594 if (!constants || start_register >= MAX_CONST_B)
2595 return WINED3DERR_INVALIDCALL;
2597 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2598 for (i = 0; i < count; ++i)
2599 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2601 if (device->recording)
2603 for (i = start_register; i < count + start_register; ++i)
2604 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2606 else
2608 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2611 return WINED3D_OK;
2614 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2615 UINT start_register, BOOL *constants, UINT bool_count)
2617 UINT count = min(bool_count, MAX_CONST_B - start_register);
2619 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2620 device, start_register, constants, bool_count);
2622 if (!constants || start_register >= MAX_CONST_B)
2623 return WINED3DERR_INVALIDCALL;
2625 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2627 return WINED3D_OK;
2630 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2631 UINT start_register, const int *constants, UINT vector4i_count)
2633 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2634 UINT i;
2636 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2637 device, start_register, constants, vector4i_count);
2639 if (!constants || start_register >= MAX_CONST_I)
2640 return WINED3DERR_INVALIDCALL;
2642 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2643 for (i = 0; i < count; ++i)
2644 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2645 constants[i * 4], constants[i * 4 + 1],
2646 constants[i * 4 + 2], constants[i * 4 + 3]);
2648 if (device->recording)
2650 for (i = start_register; i < count + start_register; ++i)
2651 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2653 else
2655 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2658 return WINED3D_OK;
2661 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2662 UINT start_register, int *constants, UINT vector4i_count)
2664 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2666 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2667 device, start_register, constants, vector4i_count);
2669 if (!constants || start_register >= MAX_CONST_I)
2670 return WINED3DERR_INVALIDCALL;
2672 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2674 return WINED3D_OK;
2677 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2678 UINT start_register, const float *constants, UINT vector4f_count)
2680 UINT i;
2681 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2683 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2684 device, start_register, constants, vector4f_count);
2686 /* Specifically test start_register > limit to catch MAX_UINT overflows
2687 * when adding start_register + vector4f_count. */
2688 if (!constants
2689 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2690 || start_register > d3d_info->limits.ps_uniform_count)
2691 return WINED3DERR_INVALIDCALL;
2693 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2694 constants, vector4f_count * sizeof(float) * 4);
2695 if (TRACE_ON(d3d))
2697 for (i = 0; i < vector4f_count; ++i)
2698 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2699 constants[i * 4], constants[i * 4 + 1],
2700 constants[i * 4 + 2], constants[i * 4 + 3]);
2703 if (device->recording)
2704 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2705 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2706 else
2707 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2709 return WINED3D_OK;
2712 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2713 UINT start_register, float *constants, UINT vector4f_count)
2715 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2716 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2718 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2719 device, start_register, constants, vector4f_count);
2721 if (!constants || count < 0)
2722 return WINED3DERR_INVALIDCALL;
2724 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2726 return WINED3D_OK;
2729 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2731 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2733 TRACE("device %p, shader %p.\n", device, shader);
2735 if (device->recording || shader == prev)
2736 return;
2737 if (shader)
2738 wined3d_shader_incref(shader);
2739 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2740 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2741 if (prev)
2742 wined3d_shader_decref(prev);
2745 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2747 TRACE("device %p.\n", device);
2749 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2752 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2754 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2756 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2759 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2761 TRACE("device %p, idx %u.\n", device, idx);
2763 if (idx >= MAX_CONSTANT_BUFFERS)
2765 WARN("Invalid constant buffer index %u.\n", idx);
2766 return NULL;
2769 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2772 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2773 UINT idx, struct wined3d_shader_resource_view *view)
2775 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2777 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2780 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2781 UINT idx)
2783 TRACE("device %p, idx %u.\n", device, idx);
2785 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2787 WARN("Invalid view index %u.\n", idx);
2788 return NULL;
2791 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2794 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2796 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2798 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2801 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2803 TRACE("device %p, idx %u.\n", device, idx);
2805 if (idx >= MAX_SAMPLER_OBJECTS)
2807 WARN("Invalid sampler index %u.\n", idx);
2808 return NULL;
2811 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2814 /* Context activation is done by the caller. */
2815 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2816 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2817 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2818 DWORD DestFVF)
2820 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2821 struct wined3d_viewport vp;
2822 UINT vertex_size;
2823 unsigned int i;
2824 BYTE *dest_ptr;
2825 BOOL doClip;
2826 DWORD numTextures;
2827 HRESULT hr;
2829 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2831 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2834 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2836 ERR("Source has no position mask\n");
2837 return WINED3DERR_INVALIDCALL;
2840 if (device->state.render_states[WINED3D_RS_CLIPPING])
2842 static BOOL warned = FALSE;
2844 * The clipping code is not quite correct. Some things need
2845 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2846 * so disable clipping for now.
2847 * (The graphics in Half-Life are broken, and my processvertices
2848 * test crashes with IDirect3DDevice3)
2849 doClip = TRUE;
2851 doClip = FALSE;
2852 if(!warned) {
2853 warned = TRUE;
2854 FIXME("Clipping is broken and disabled for now\n");
2857 else
2858 doClip = FALSE;
2860 vertex_size = get_flexible_vertex_size(DestFVF);
2861 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2863 WARN("Failed to map buffer, hr %#x.\n", hr);
2864 return hr;
2867 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2868 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2869 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2871 TRACE("View mat:\n");
2872 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2873 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2874 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2875 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2877 TRACE("Proj mat:\n");
2878 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2879 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2880 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2881 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2883 TRACE("World mat:\n");
2884 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2885 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2886 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2887 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2889 /* Get the viewport */
2890 wined3d_device_get_viewport(device, &vp);
2891 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2892 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2894 multiply_matrix(&mat,&view_mat,&world_mat);
2895 multiply_matrix(&mat,&proj_mat,&mat);
2897 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2899 for (i = 0; i < dwCount; i+= 1) {
2900 unsigned int tex_index;
2902 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2903 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2904 /* The position first */
2905 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2906 const float *p = (const float *)(element->data.addr + i * element->stride);
2907 float x, y, z, rhw;
2908 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2910 /* Multiplication with world, view and projection matrix. */
2911 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2912 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2913 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2914 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2916 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2918 /* WARNING: The following things are taken from d3d7 and were not yet checked
2919 * against d3d8 or d3d9!
2922 /* Clipping conditions: From msdn
2924 * A vertex is clipped if it does not match the following requirements
2925 * -rhw < x <= rhw
2926 * -rhw < y <= rhw
2927 * 0 < z <= rhw
2928 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2930 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2931 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2935 if( !doClip ||
2936 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2937 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2938 ( rhw > eps ) ) ) {
2940 /* "Normal" viewport transformation (not clipped)
2941 * 1) The values are divided by rhw
2942 * 2) The y axis is negative, so multiply it with -1
2943 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2944 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2945 * 4) Multiply x with Width/2 and add Width/2
2946 * 5) The same for the height
2947 * 6) Add the viewpoint X and Y to the 2D coordinates and
2948 * The minimum Z value to z
2949 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2951 * Well, basically it's simply a linear transformation into viewport
2952 * coordinates
2955 x /= rhw;
2956 y /= rhw;
2957 z /= rhw;
2959 y *= -1;
2961 x *= vp.width / 2;
2962 y *= vp.height / 2;
2963 z *= vp.max_z - vp.min_z;
2965 x += vp.width / 2 + vp.x;
2966 y += vp.height / 2 + vp.y;
2967 z += vp.min_z;
2969 rhw = 1 / rhw;
2970 } else {
2971 /* That vertex got clipped
2972 * Contrary to OpenGL it is not dropped completely, it just
2973 * undergoes a different calculation.
2975 TRACE("Vertex got clipped\n");
2976 x += rhw;
2977 y += rhw;
2979 x /= 2;
2980 y /= 2;
2982 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2983 * outside of the main vertex buffer memory. That needs some more
2984 * investigation...
2988 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2991 ( (float *) dest_ptr)[0] = x;
2992 ( (float *) dest_ptr)[1] = y;
2993 ( (float *) dest_ptr)[2] = z;
2994 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2996 dest_ptr += 3 * sizeof(float);
2998 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2999 dest_ptr += sizeof(float);
3002 if (DestFVF & WINED3DFVF_PSIZE)
3003 dest_ptr += sizeof(DWORD);
3005 if (DestFVF & WINED3DFVF_NORMAL)
3007 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3008 const float *normal = (const float *)(element->data.addr + i * element->stride);
3009 /* AFAIK this should go into the lighting information */
3010 FIXME("Didn't expect the destination to have a normal\n");
3011 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3014 if (DestFVF & WINED3DFVF_DIFFUSE)
3016 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3017 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3018 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3020 static BOOL warned = FALSE;
3022 if(!warned) {
3023 ERR("No diffuse color in source, but destination has one\n");
3024 warned = TRUE;
3027 *( (DWORD *) dest_ptr) = 0xffffffff;
3028 dest_ptr += sizeof(DWORD);
3030 else
3032 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3036 if (DestFVF & WINED3DFVF_SPECULAR)
3038 /* What's the color value in the feedback buffer? */
3039 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3040 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3041 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3043 static BOOL warned = FALSE;
3045 if(!warned) {
3046 ERR("No specular color in source, but destination has one\n");
3047 warned = TRUE;
3050 *(DWORD *)dest_ptr = 0xff000000;
3051 dest_ptr += sizeof(DWORD);
3053 else
3055 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3059 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3061 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3062 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3063 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3065 ERR("No source texture, but destination requests one\n");
3066 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3068 else
3070 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3075 wined3d_buffer_unmap(dest);
3077 return WINED3D_OK;
3079 #undef copy_and_next
3081 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3082 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3083 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3085 struct wined3d_state *state = &device->state;
3086 struct wined3d_stream_info stream_info;
3087 const struct wined3d_gl_info *gl_info;
3088 struct wined3d_context *context;
3089 struct wined3d_shader *vs;
3090 unsigned int i;
3091 HRESULT hr;
3092 WORD map;
3094 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3095 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3096 device, src_start_idx, dst_idx, vertex_count,
3097 dst_buffer, declaration, flags, dst_fvf);
3099 if (declaration)
3100 FIXME("Output vertex declaration not implemented yet.\n");
3102 /* Need any context to write to the vbo. */
3103 context = context_acquire(device, NULL);
3104 gl_info = context->gl_info;
3106 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3107 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3108 context_stream_info_from_declaration(context, state, &stream_info);
3109 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3111 /* We can't convert FROM a VBO, and vertex buffers used to source into
3112 * process_vertices() are unlikely to ever be used for drawing. Release
3113 * VBOs in those buffers and fix up the stream_info structure.
3115 * Also apply the start index. */
3116 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3118 struct wined3d_stream_info_element *e;
3119 struct wined3d_buffer *buffer;
3121 if (!(map & 1))
3122 continue;
3124 e = &stream_info.elements[i];
3125 buffer = state->streams[e->stream_idx].buffer;
3126 e->data.buffer_object = 0;
3127 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3128 if (buffer->buffer_object)
3130 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3131 buffer->buffer_object = 0;
3133 if (e->data.addr)
3134 e->data.addr += e->stride * src_start_idx;
3137 hr = process_vertices_strided(device, dst_idx, vertex_count,
3138 &stream_info, dst_buffer, flags, dst_fvf);
3140 context_release(context);
3142 return hr;
3145 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3146 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3148 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3149 DWORD old_value;
3151 TRACE("device %p, stage %u, state %s, value %#x.\n",
3152 device, stage, debug_d3dtexturestate(state), value);
3154 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3156 WARN("Invalid state %#x passed.\n", state);
3157 return;
3160 if (stage >= d3d_info->limits.ffp_blend_stages)
3162 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3163 stage, d3d_info->limits.ffp_blend_stages - 1);
3164 return;
3167 old_value = device->update_state->texture_states[stage][state];
3168 device->update_state->texture_states[stage][state] = value;
3170 if (device->recording)
3172 TRACE("Recording... not performing anything.\n");
3173 device->recording->changed.textureState[stage] |= 1u << state;
3174 return;
3177 /* Checked after the assignments to allow proper stateblock recording. */
3178 if (old_value == value)
3180 TRACE("Application is setting the old value over, nothing to do.\n");
3181 return;
3184 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3187 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3188 UINT stage, enum wined3d_texture_stage_state state)
3190 TRACE("device %p, stage %u, state %s.\n",
3191 device, stage, debug_d3dtexturestate(state));
3193 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3195 WARN("Invalid state %#x passed.\n", state);
3196 return 0;
3199 return device->state.texture_states[stage][state];
3202 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3203 UINT stage, struct wined3d_texture *texture)
3205 struct wined3d_texture *prev;
3207 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3209 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3210 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3212 /* Windows accepts overflowing this array... we do not. */
3213 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3215 WARN("Ignoring invalid stage %u.\n", stage);
3216 return WINED3D_OK;
3219 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3221 WARN("Rejecting attempt to set scratch texture.\n");
3222 return WINED3DERR_INVALIDCALL;
3225 if (device->recording)
3226 device->recording->changed.textures |= 1u << stage;
3228 prev = device->update_state->textures[stage];
3229 TRACE("Previous texture %p.\n", prev);
3231 if (texture == prev)
3233 TRACE("App is setting the same texture again, nothing to do.\n");
3234 return WINED3D_OK;
3237 TRACE("Setting new texture to %p.\n", texture);
3238 device->update_state->textures[stage] = texture;
3240 if (texture)
3241 wined3d_texture_incref(texture);
3242 if (!device->recording)
3243 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3244 if (prev)
3245 wined3d_texture_decref(prev);
3247 return WINED3D_OK;
3250 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3252 TRACE("device %p, stage %u.\n", device, stage);
3254 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3255 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3257 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3259 WARN("Ignoring invalid stage %u.\n", stage);
3260 return NULL; /* Windows accepts overflowing this array ... we do not. */
3263 return device->state.textures[stage];
3266 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3268 TRACE("device %p, caps %p.\n", device, caps);
3270 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3271 device->create_parms.device_type, caps);
3274 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3275 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3277 struct wined3d_swapchain *swapchain;
3279 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3280 device, swapchain_idx, mode, rotation);
3282 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3283 return WINED3DERR_INVALIDCALL;
3285 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3288 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3290 struct wined3d_stateblock *stateblock;
3291 HRESULT hr;
3293 TRACE("device %p.\n", device);
3295 if (device->recording)
3296 return WINED3DERR_INVALIDCALL;
3298 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3299 if (FAILED(hr))
3300 return hr;
3302 device->recording = stateblock;
3303 device->update_state = &stateblock->state;
3305 TRACE("Recording stateblock %p.\n", stateblock);
3307 return WINED3D_OK;
3310 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3311 struct wined3d_stateblock **stateblock)
3313 struct wined3d_stateblock *object = device->recording;
3315 TRACE("device %p, stateblock %p.\n", device, stateblock);
3317 if (!device->recording)
3319 WARN("Not recording.\n");
3320 *stateblock = NULL;
3321 return WINED3DERR_INVALIDCALL;
3324 stateblock_init_contained_states(object);
3326 *stateblock = object;
3327 device->recording = NULL;
3328 device->update_state = &device->state;
3330 TRACE("Returning stateblock %p.\n", *stateblock);
3332 return WINED3D_OK;
3335 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3337 /* At the moment we have no need for any functionality at the beginning
3338 * of a scene. */
3339 TRACE("device %p.\n", device);
3341 if (device->inScene)
3343 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3344 return WINED3DERR_INVALIDCALL;
3346 device->inScene = TRUE;
3347 return WINED3D_OK;
3350 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3352 struct wined3d_context *context;
3354 TRACE("device %p.\n", device);
3356 if (!device->inScene)
3358 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3359 return WINED3DERR_INVALIDCALL;
3362 context = context_acquire(device, NULL);
3363 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3364 context->gl_info->gl_ops.gl.p_glFlush();
3365 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3366 * fails. */
3367 context_release(context);
3369 device->inScene = FALSE;
3370 return WINED3D_OK;
3373 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3374 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3376 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3377 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3379 if (!rect_count && rects)
3381 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3382 return WINED3D_OK;
3385 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3387 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3388 if (!ds)
3390 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3391 /* TODO: What about depth stencil buffers without stencil bits? */
3392 return WINED3DERR_INVALIDCALL;
3394 else if (flags & WINED3DCLEAR_TARGET)
3396 if (ds->width < device->fb.render_targets[0]->width
3397 || ds->height < device->fb.render_targets[0]->height)
3399 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3400 return WINED3D_OK;
3405 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3407 return WINED3D_OK;
3410 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3411 struct wined3d_query *predicate, BOOL value)
3413 struct wined3d_query *prev;
3415 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3417 prev = device->update_state->predicate;
3418 if (predicate)
3420 FIXME("Predicated rendering not implemented.\n");
3421 wined3d_query_incref(predicate);
3423 device->update_state->predicate = predicate;
3424 device->update_state->predicate_value = value;
3425 if (!device->recording)
3426 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3427 if (prev)
3428 wined3d_query_decref(prev);
3431 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3433 TRACE("device %p, value %p.\n", device, value);
3435 *value = device->state.predicate_value;
3436 return device->state.predicate;
3439 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3440 enum wined3d_primitive_type primitive_type)
3442 GLenum gl_primitive_type, prev;
3444 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3446 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3447 prev = device->update_state->gl_primitive_type;
3448 device->update_state->gl_primitive_type = gl_primitive_type;
3449 if (device->recording)
3450 device->recording->changed.primitive_type = TRUE;
3451 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3452 device_invalidate_state(device, STATE_POINT_ENABLE);
3455 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3456 enum wined3d_primitive_type *primitive_type)
3458 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3460 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3462 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3465 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3467 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3469 if (!device->state.vertex_declaration)
3471 WARN("Called without a valid vertex declaration set.\n");
3472 return WINED3DERR_INVALIDCALL;
3475 if (device->state.load_base_vertex_index)
3477 device->state.load_base_vertex_index = 0;
3478 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3481 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3483 return WINED3D_OK;
3486 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3487 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3489 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3490 device, start_vertex, vertex_count, start_instance, instance_count);
3492 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3495 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3497 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3499 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3501 if (!device->state.index_buffer)
3503 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3504 * without an index buffer set. (The first time at least...)
3505 * D3D8 simply dies, but I doubt it can do much harm to return
3506 * D3DERR_INVALIDCALL there as well. */
3507 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3508 return WINED3DERR_INVALIDCALL;
3511 if (!device->state.vertex_declaration)
3513 WARN("Called without a valid vertex declaration set.\n");
3514 return WINED3DERR_INVALIDCALL;
3517 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3518 device->state.load_base_vertex_index != device->state.base_vertex_index)
3520 device->state.load_base_vertex_index = device->state.base_vertex_index;
3521 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3524 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3526 return WINED3D_OK;
3529 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3530 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3532 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3533 device, start_idx, index_count, start_instance, instance_count);
3535 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3538 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3539 struct wined3d_texture *src_texture, unsigned int src_level,
3540 struct wined3d_texture *dst_texture, unsigned int level_count)
3542 struct wined3d_const_bo_address data;
3543 struct wined3d_context *context;
3544 struct wined3d_map_desc src;
3545 HRESULT hr = WINED3D_OK;
3546 unsigned int i;
3548 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3549 device, src_texture, src_level, dst_texture, level_count);
3551 if (src_texture->resource.format != dst_texture->resource.format)
3553 WARN("Source and destination formats do not match.\n");
3554 return WINED3DERR_INVALIDCALL;
3557 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3558 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3559 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3561 WARN("Source and destination dimensions do not match.\n");
3562 return WINED3DERR_INVALIDCALL;
3565 context = context_acquire(device, NULL);
3567 /* Only a prepare, since we're uploading entire volumes. */
3568 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3569 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3571 for (i = 0; i < level_count; ++i)
3573 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3574 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3575 goto done;
3577 data.buffer_object = 0;
3578 data.addr = src.data;
3579 wined3d_volume_upload_data(dst_texture->sub_resources[i].u.volume, context, &data);
3580 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3582 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3583 goto done;
3586 done:
3587 context_release(context);
3588 return hr;
3591 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3592 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3594 unsigned int src_size, dst_size, src_skip_levels = 0;
3595 unsigned int layer_count, level_count, i, j;
3596 enum wined3d_resource_type type;
3597 HRESULT hr;
3598 struct wined3d_context *context;
3600 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3602 /* Verify that the source and destination textures are non-NULL. */
3603 if (!src_texture || !dst_texture)
3605 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3606 return WINED3DERR_INVALIDCALL;
3609 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3611 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3612 return WINED3DERR_INVALIDCALL;
3614 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3616 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3617 return WINED3DERR_INVALIDCALL;
3620 /* Verify that the source and destination textures are the same type. */
3621 type = src_texture->resource.type;
3622 if (dst_texture->resource.type != type)
3624 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3625 return WINED3DERR_INVALIDCALL;
3628 layer_count = src_texture->layer_count;
3629 if (layer_count != dst_texture->layer_count)
3631 WARN("Source and destination have different layer counts.\n");
3632 return WINED3DERR_INVALIDCALL;
3635 level_count = min(wined3d_texture_get_level_count(src_texture),
3636 wined3d_texture_get_level_count(dst_texture));
3638 src_size = max(src_texture->resource.width, src_texture->resource.height);
3639 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3640 if (type == WINED3D_RTYPE_VOLUME)
3642 src_size = max(src_size, src_texture->resource.depth);
3643 dst_size = max(dst_size, dst_texture->resource.depth);
3645 while (src_size > dst_size)
3647 src_size >>= 1;
3648 ++src_skip_levels;
3651 /* Make sure that the destination texture is loaded. */
3652 context = context_acquire(device, NULL);
3653 wined3d_texture_load(dst_texture, context, FALSE);
3654 context_release(context);
3656 /* Update every surface level of the texture. */
3657 switch (type)
3659 case WINED3D_RTYPE_TEXTURE_2D:
3661 unsigned int src_levels = src_texture->level_count;
3662 unsigned int dst_levels = dst_texture->level_count;
3663 struct wined3d_surface *src_surface;
3664 struct wined3d_surface *dst_surface;
3666 for (i = 0; i < layer_count; ++i)
3668 for (j = 0; j < level_count; ++j)
3670 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3671 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3672 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3674 WARN("Failed to update surface, hr %#x.\n", hr);
3675 return hr;
3679 return WINED3D_OK;
3682 case WINED3D_RTYPE_TEXTURE_3D:
3683 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3684 src_texture, src_skip_levels, dst_texture, level_count)))
3685 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3686 return hr;
3688 default:
3689 FIXME("Unsupported texture type %#x.\n", type);
3690 return WINED3DERR_INVALIDCALL;
3694 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3696 const struct wined3d_state *state = &device->state;
3697 struct wined3d_texture *texture;
3698 DWORD i;
3700 TRACE("device %p, num_passes %p.\n", device, num_passes);
3702 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3704 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3706 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3707 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3709 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3711 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3712 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3715 texture = state->textures[i];
3716 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3718 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3720 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3721 return E_FAIL;
3723 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3725 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3726 return E_FAIL;
3728 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3729 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3731 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3732 return E_FAIL;
3736 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3737 || state->render_states[WINED3D_RS_STENCILENABLE])
3739 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3740 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3742 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3744 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3745 return WINED3DERR_CONFLICTINGRENDERSTATE;
3749 /* return a sensible default */
3750 *num_passes = 1;
3752 TRACE("returning D3D_OK\n");
3753 return WINED3D_OK;
3756 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3758 static BOOL warned;
3760 TRACE("device %p, software %#x.\n", device, software);
3762 if (!warned)
3764 FIXME("device %p, software %#x stub!\n", device, software);
3765 warned = TRUE;
3768 device->softwareVertexProcessing = software;
3771 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3773 static BOOL warned;
3775 TRACE("device %p.\n", device);
3777 if (!warned)
3779 TRACE("device %p stub!\n", device);
3780 warned = TRUE;
3783 return device->softwareVertexProcessing;
3786 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3787 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3789 struct wined3d_swapchain *swapchain;
3791 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3792 device, swapchain_idx, raster_status);
3794 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3795 return WINED3DERR_INVALIDCALL;
3797 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3800 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3802 static BOOL warned;
3804 TRACE("device %p, segments %.8e.\n", device, segments);
3806 if (segments != 0.0f)
3808 if (!warned)
3810 FIXME("device %p, segments %.8e stub!\n", device, segments);
3811 warned = TRUE;
3815 return WINED3D_OK;
3818 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3820 static BOOL warned;
3822 TRACE("device %p.\n", device);
3824 if (!warned)
3826 FIXME("device %p stub!\n", device);
3827 warned = TRUE;
3830 return 0.0f;
3833 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3834 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3836 struct wined3d_texture *dst_texture, *src_texture;
3837 RECT dst_rect, src_rect;
3838 unsigned int i, j;
3839 HRESULT hr;
3841 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3843 if (src_resource == dst_resource)
3845 WARN("Source and destination are the same resource.\n");
3846 return;
3849 if (src_resource->type != dst_resource->type)
3851 WARN("Resource types (%s / %s) don't match.\n",
3852 debug_d3dresourcetype(dst_resource->type),
3853 debug_d3dresourcetype(src_resource->type));
3854 return;
3857 if (src_resource->width != dst_resource->width
3858 || src_resource->height != dst_resource->height
3859 || src_resource->depth != dst_resource->depth)
3861 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3862 dst_resource->width, dst_resource->height, dst_resource->depth,
3863 src_resource->width, src_resource->height, src_resource->depth);
3864 return;
3867 if (src_resource->format->id != dst_resource->format->id)
3869 WARN("Resource formats (%s / %s) don't match.\n",
3870 debug_d3dformat(dst_resource->format->id),
3871 debug_d3dformat(src_resource->format->id));
3872 return;
3875 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3877 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3878 buffer_from_resource(src_resource), 0,
3879 dst_resource->size)))
3880 ERR("Failed to copy buffer, hr %#x.\n", hr);
3881 return;
3884 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3886 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3887 return;
3890 dst_texture = wined3d_texture_from_resource(dst_resource);
3891 src_texture = wined3d_texture_from_resource(src_resource);
3893 if (src_texture->layer_count != dst_texture->layer_count
3894 || src_texture->level_count != dst_texture->level_count)
3896 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3897 dst_texture->layer_count, dst_texture->level_count,
3898 src_texture->layer_count, src_texture->level_count);
3899 return;
3902 for (i = 0; i < dst_texture->level_count; ++i)
3904 SetRect(&dst_rect, 0, 0,
3905 wined3d_texture_get_level_width(dst_texture, i),
3906 wined3d_texture_get_level_height(dst_texture, i));
3907 SetRect(&src_rect, 0, 0,
3908 wined3d_texture_get_level_width(src_texture, i),
3909 wined3d_texture_get_level_height(dst_texture, i));
3910 for (j = 0; j < dst_texture->layer_count; ++j)
3912 unsigned int idx = j * dst_texture->level_count + i;
3914 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3915 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3916 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3921 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3922 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3923 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3924 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3926 struct wined3d_texture *dst_texture, *src_texture;
3927 RECT dst_rect, src_rect;
3928 HRESULT hr;
3930 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3931 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3932 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3933 src_resource, src_sub_resource_idx, debug_box(src_box));
3935 if (src_box && (src_box->left >= src_box->right
3936 || src_box->top >= src_box->bottom
3937 || src_box->front >= src_box->back))
3939 WARN("Invalid box %s specified.\n", debug_box(src_box));
3940 return WINED3DERR_INVALIDCALL;
3943 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3945 WARN("Source and destination are the same sub-resource.\n");
3946 return WINED3DERR_INVALIDCALL;
3949 if (src_resource->type != dst_resource->type)
3951 WARN("Resource types (%s / %s) don't match.\n",
3952 debug_d3dresourcetype(dst_resource->type),
3953 debug_d3dresourcetype(src_resource->type));
3954 return WINED3DERR_INVALIDCALL;
3957 if (src_resource->format->id != dst_resource->format->id)
3959 WARN("Resource formats (%s / %s) don't match.\n",
3960 debug_d3dformat(dst_resource->format->id),
3961 debug_d3dformat(src_resource->format->id));
3962 return WINED3DERR_INVALIDCALL;
3965 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3967 unsigned int src_offset, size;
3969 if (dst_sub_resource_idx)
3971 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3972 return WINED3DERR_INVALIDCALL;
3974 if (src_sub_resource_idx)
3976 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3977 return WINED3DERR_INVALIDCALL;
3980 if (src_box)
3982 src_offset = src_box->left;
3983 size = src_box->right - src_box->left;
3985 else
3987 src_offset = 0;
3988 size = src_resource->size;
3991 if (src_offset > src_resource->size
3992 || size > src_resource->size - src_offset
3993 || dst_x > dst_resource->size
3994 || size > dst_resource->size - dst_x)
3996 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
3997 dst_x, src_offset, size);
3998 return WINED3DERR_INVALIDCALL;
4001 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4002 buffer_from_resource(src_resource), src_offset, size);
4005 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4007 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4008 return WINED3DERR_INVALIDCALL;
4011 dst_texture = wined3d_texture_from_resource(dst_resource);
4012 src_texture = wined3d_texture_from_resource(src_resource);
4014 if (src_box)
4016 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4018 else
4020 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4022 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4023 wined3d_texture_get_level_height(src_texture, level));
4026 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4027 dst_y + (src_rect.bottom - src_rect.top));
4029 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4030 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4031 WARN("Failed to blit, hr %#x.\n", hr);
4033 return hr;
4036 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4037 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4038 unsigned int depth_pitch)
4040 struct wined3d_texture_sub_resource *sub_resource;
4041 const struct wined3d_gl_info *gl_info;
4042 struct wined3d_const_bo_address addr;
4043 unsigned int width, height, level;
4044 struct wined3d_context *context;
4045 struct wined3d_texture *texture;
4046 struct wined3d_surface *surface;
4047 POINT dst_point;
4048 RECT src_rect;
4050 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4051 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4053 if (resource->type == WINED3D_RTYPE_BUFFER)
4055 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4056 HRESULT hr;
4058 if (sub_resource_idx > 0)
4060 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4061 return;
4064 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4065 WARN("Failed to update buffer data, hr %#x.\n", hr);
4067 return;
4070 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4072 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4073 return;
4076 texture = wined3d_texture_from_resource(resource);
4077 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4079 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4080 return;
4082 surface = sub_resource->u.surface;
4084 level = sub_resource_idx % texture->level_count;
4085 width = wined3d_texture_get_level_width(texture, level);
4086 height = wined3d_texture_get_level_height(texture, level);
4088 src_rect.left = 0;
4089 src_rect.top = 0;
4090 if (box)
4092 if (box->left >= box->right || box->right > width
4093 || box->top >= box->bottom || box->bottom > height
4094 || box->front >= box->back)
4096 WARN("Invalid box %s specified.\n", debug_box(box));
4097 return;
4100 src_rect.right = box->right - box->left;
4101 src_rect.bottom = box->bottom - box->top;
4102 dst_point.x = box->left;
4103 dst_point.y = box->top;
4105 else
4107 src_rect.right = width;
4108 src_rect.bottom = height;
4109 dst_point.x = 0;
4110 dst_point.y = 0;
4113 addr.buffer_object = 0;
4114 addr.addr = data;
4116 context = context_acquire(resource->device, NULL);
4117 gl_info = context->gl_info;
4119 /* Only load the surface for partial updates. */
4120 if (!dst_point.x && !dst_point.y && src_rect.right == width && src_rect.bottom == height)
4121 wined3d_texture_prepare_texture(texture, context, FALSE);
4122 else
4123 surface_load_location(surface, context, WINED3D_LOCATION_TEXTURE_RGB);
4124 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4126 wined3d_surface_upload_data(surface, gl_info, resource->format,
4127 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4129 context_release(context);
4131 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4132 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4135 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4136 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4137 const struct wined3d_color *color, float depth, DWORD stencil)
4139 const struct blit_shader *blitter;
4140 struct wined3d_resource *resource;
4141 enum wined3d_blit_op blit_op;
4142 RECT r;
4144 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4145 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4147 if (!flags)
4148 return WINED3D_OK;
4150 resource = view->resource;
4151 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4153 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4154 return WINED3DERR_INVALIDCALL;
4157 if (view->depth > 1)
4159 FIXME("Layered clears not implemented.\n");
4160 return WINED3DERR_INVALIDCALL;
4163 if (!rect)
4165 SetRect(&r, 0, 0, view->width, view->height);
4166 rect = &r;
4169 if (flags & WINED3DCLEAR_TARGET)
4170 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4171 else
4172 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4174 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4175 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4177 FIXME("No blitter is capable of performing the requested fill operation.\n");
4178 return WINED3DERR_INVALIDCALL;
4181 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4182 return blitter->color_fill(device, view, rect, color);
4183 else
4184 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4187 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4188 unsigned int view_idx)
4190 TRACE("device %p, view_idx %u.\n", device, view_idx);
4192 if (view_idx >= device->adapter->gl_info.limits.buffers)
4194 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4195 return NULL;
4198 return device->fb.render_targets[view_idx];
4201 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4203 TRACE("device %p.\n", device);
4205 return device->fb.depth_stencil;
4208 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4209 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4211 struct wined3d_rendertarget_view *prev;
4213 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4214 device, view_idx, view, set_viewport);
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 WINED3DERR_INVALIDCALL;
4222 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4224 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4225 return WINED3DERR_INVALIDCALL;
4228 /* Set the viewport and scissor rectangles, if requested. Tests show that
4229 * stateblock recording is ignored, the change goes directly into the
4230 * primary stateblock. */
4231 if (!view_idx && set_viewport)
4233 struct wined3d_state *state = &device->state;
4235 state->viewport.x = 0;
4236 state->viewport.y = 0;
4237 state->viewport.width = view->width;
4238 state->viewport.height = view->height;
4239 state->viewport.min_z = 0.0f;
4240 state->viewport.max_z = 1.0f;
4241 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4243 state->scissor_rect.top = 0;
4244 state->scissor_rect.left = 0;
4245 state->scissor_rect.right = view->width;
4246 state->scissor_rect.bottom = view->height;
4247 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4251 prev = device->fb.render_targets[view_idx];
4252 if (view == prev)
4253 return WINED3D_OK;
4255 if (view)
4256 wined3d_rendertarget_view_incref(view);
4257 device->fb.render_targets[view_idx] = view;
4258 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4259 /* Release after the assignment, to prevent device_resource_released()
4260 * from seeing the surface as still in use. */
4261 if (prev)
4262 wined3d_rendertarget_view_decref(prev);
4264 return WINED3D_OK;
4267 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4269 struct wined3d_rendertarget_view *prev;
4271 TRACE("device %p, view %p.\n", device, view);
4273 prev = device->fb.depth_stencil;
4274 if (prev == view)
4276 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4277 return;
4280 if ((device->fb.depth_stencil = view))
4281 wined3d_rendertarget_view_incref(view);
4282 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4283 if (prev)
4284 wined3d_rendertarget_view_decref(prev);
4287 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4288 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4290 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4291 struct wined3d_sub_resource_data data;
4292 struct wined3d_resource_desc desc;
4293 struct wined3d_map_desc map_desc;
4294 struct wined3d_texture *texture;
4295 HRESULT hr;
4297 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4299 ERR("Failed to map source texture.\n");
4300 return NULL;
4303 data.data = map_desc.data;
4304 data.row_pitch = map_desc.row_pitch;
4305 data.slice_pitch = map_desc.slice_pitch;
4307 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4308 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4309 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4310 desc.multisample_quality = 0;
4311 desc.usage = WINED3DUSAGE_DYNAMIC;
4312 desc.pool = WINED3D_POOL_DEFAULT;
4313 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4314 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4315 desc.depth = 1;
4316 desc.size = 0;
4318 hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4319 &data, NULL, &wined3d_null_parent_ops, &texture);
4320 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4321 if (FAILED(hr))
4323 ERR("Failed to create cursor texture.\n");
4324 return NULL;
4327 return texture;
4330 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4331 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4333 unsigned int texture_level = sub_resource_idx % texture->level_count;
4334 unsigned int cursor_width, cursor_height;
4335 struct wined3d_display_mode mode;
4336 struct wined3d_map_desc map_desc;
4337 HRESULT hr;
4339 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4340 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4342 if (sub_resource_idx >= texture->level_count * texture->layer_count
4343 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4344 return WINED3DERR_INVALIDCALL;
4346 if (device->cursor_texture)
4348 wined3d_texture_decref(device->cursor_texture);
4349 device->cursor_texture = NULL;
4352 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4354 WARN("Texture %p has invalid format %s.\n",
4355 texture, debug_d3dformat(texture->resource.format->id));
4356 return WINED3DERR_INVALIDCALL;
4359 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4361 ERR("Failed to get display mode, hr %#x.\n", hr);
4362 return WINED3DERR_INVALIDCALL;
4365 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4366 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4367 if (cursor_width > mode.width || cursor_height > mode.height)
4369 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4370 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4371 return WINED3DERR_INVALIDCALL;
4374 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4376 /* Do not store the surface's pointer because the application may
4377 * release it after setting the cursor image. Windows doesn't
4378 * addref the set surface, so we can't do this either without
4379 * creating circular refcount dependencies. */
4380 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4382 ERR("Failed to create cursor texture.\n");
4383 return WINED3DERR_INVALIDCALL;
4386 if (cursor_width == 32 && cursor_height == 32)
4388 UINT mask_size = cursor_width * cursor_height / 8;
4389 ICONINFO cursor_info;
4390 DWORD *mask_bits;
4391 HCURSOR cursor;
4393 /* 32-bit user32 cursors ignore the alpha channel if it's all
4394 * zeroes, and use the mask instead. Fill the mask with all ones
4395 * to ensure we still get a fully transparent cursor. */
4396 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4397 return E_OUTOFMEMORY;
4398 memset(mask_bits, 0xff, mask_size);
4400 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4401 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4402 cursor_info.fIcon = FALSE;
4403 cursor_info.xHotspot = x_hotspot;
4404 cursor_info.yHotspot = y_hotspot;
4405 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4406 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4407 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4409 /* Create our cursor and clean up. */
4410 cursor = CreateIconIndirect(&cursor_info);
4411 if (cursor_info.hbmMask)
4412 DeleteObject(cursor_info.hbmMask);
4413 if (cursor_info.hbmColor)
4414 DeleteObject(cursor_info.hbmColor);
4415 if (device->hardwareCursor)
4416 DestroyCursor(device->hardwareCursor);
4417 device->hardwareCursor = cursor;
4418 if (device->bCursorVisible)
4419 SetCursor(cursor);
4421 HeapFree(GetProcessHeap(), 0, mask_bits);
4424 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4425 device->cursorWidth = cursor_width;
4426 device->cursorHeight = cursor_height;
4427 device->xHotSpot = x_hotspot;
4428 device->yHotSpot = y_hotspot;
4430 return WINED3D_OK;
4433 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4434 int x_screen_space, int y_screen_space, DWORD flags)
4436 TRACE("device %p, x %d, y %d, flags %#x.\n",
4437 device, x_screen_space, y_screen_space, flags);
4439 device->xScreenSpace = x_screen_space;
4440 device->yScreenSpace = y_screen_space;
4442 if (device->hardwareCursor)
4444 POINT pt;
4446 GetCursorPos( &pt );
4447 if (x_screen_space == pt.x && y_screen_space == pt.y)
4448 return;
4449 SetCursorPos( x_screen_space, y_screen_space );
4451 /* Switch to the software cursor if position diverges from the hardware one. */
4452 GetCursorPos( &pt );
4453 if (x_screen_space != pt.x || y_screen_space != pt.y)
4455 if (device->bCursorVisible) SetCursor( NULL );
4456 DestroyCursor( device->hardwareCursor );
4457 device->hardwareCursor = 0;
4462 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4464 BOOL oldVisible = device->bCursorVisible;
4466 TRACE("device %p, show %#x.\n", device, show);
4469 * When ShowCursor is first called it should make the cursor appear at the OS's last
4470 * known cursor position.
4472 if (show && !oldVisible)
4474 POINT pt;
4475 GetCursorPos(&pt);
4476 device->xScreenSpace = pt.x;
4477 device->yScreenSpace = pt.y;
4480 if (device->hardwareCursor)
4482 device->bCursorVisible = show;
4483 if (show)
4484 SetCursor(device->hardwareCursor);
4485 else
4486 SetCursor(NULL);
4488 else if (device->cursor_texture)
4490 device->bCursorVisible = show;
4493 return oldVisible;
4496 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4498 struct wined3d_resource *resource, *cursor;
4500 TRACE("device %p.\n", device);
4502 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4504 TRACE("Checking resource %p for eviction.\n", resource);
4506 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4508 TRACE("Evicting %p.\n", resource);
4509 resource->resource_ops->resource_unload(resource);
4513 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4514 device_invalidate_state(device, STATE_STREAMSRC);
4517 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4519 struct wined3d_resource *resource, *cursor;
4520 const struct wined3d_gl_info *gl_info;
4521 struct wined3d_context *context;
4522 struct wined3d_shader *shader;
4524 context = context_acquire(device, NULL);
4525 gl_info = context->gl_info;
4527 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4529 TRACE("Unloading resource %p.\n", resource);
4531 resource->resource_ops->resource_unload(resource);
4534 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4536 device->shader_backend->shader_destroy(shader);
4539 if (device->depth_blt_texture)
4541 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4542 device->depth_blt_texture = 0;
4545 device->blitter->free_private(device);
4546 device->shader_backend->shader_free_private(device);
4547 destroy_dummy_textures(device, gl_info);
4548 destroy_default_sampler(device);
4550 context_release(context);
4552 while (device->context_count)
4554 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4557 HeapFree(GetProcessHeap(), 0, swapchain->context);
4558 swapchain->context = NULL;
4561 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4563 struct wined3d_context *context;
4564 struct wined3d_texture *target;
4565 HRESULT hr;
4567 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4568 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4570 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4571 return hr;
4574 if (FAILED(hr = device->blitter->alloc_private(device)))
4576 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4577 device->shader_backend->shader_free_private(device);
4578 return hr;
4581 /* Recreate the primary swapchain's context */
4582 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4583 if (!swapchain->context)
4585 ERR("Failed to allocate memory for swapchain context array.\n");
4586 device->blitter->free_private(device);
4587 device->shader_backend->shader_free_private(device);
4588 return E_OUTOFMEMORY;
4591 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4592 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4594 WARN("Failed to create context.\n");
4595 device->blitter->free_private(device);
4596 device->shader_backend->shader_free_private(device);
4597 HeapFree(GetProcessHeap(), 0, swapchain->context);
4598 return E_FAIL;
4601 swapchain->context[0] = context;
4602 swapchain->num_contexts = 1;
4603 create_dummy_textures(device, context);
4604 create_default_sampler(device);
4605 context_release(context);
4607 return WINED3D_OK;
4610 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4611 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4612 wined3d_device_reset_cb callback, BOOL reset_state)
4614 struct wined3d_rendertarget_view_desc view_desc;
4615 struct wined3d_resource *resource, *cursor;
4616 struct wined3d_swapchain *swapchain;
4617 struct wined3d_display_mode m;
4618 BOOL DisplayModeChanged;
4619 HRESULT hr = WINED3D_OK;
4620 unsigned int i;
4622 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4623 device, swapchain_desc, mode, callback, reset_state);
4625 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4627 ERR("Failed to get the first implicit swapchain.\n");
4628 return WINED3DERR_INVALIDCALL;
4630 DisplayModeChanged = swapchain->reapply_mode;
4632 if (reset_state)
4634 if (device->logo_texture)
4636 wined3d_texture_decref(device->logo_texture);
4637 device->logo_texture = NULL;
4639 if (device->cursor_texture)
4641 wined3d_texture_decref(device->cursor_texture);
4642 device->cursor_texture = NULL;
4644 state_unbind_resources(&device->state);
4647 if (device->fb.render_targets)
4649 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4651 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4654 wined3d_device_set_depth_stencil_view(device, NULL);
4656 if (device->onscreen_depth_stencil)
4658 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4659 device->onscreen_depth_stencil = NULL;
4662 if (reset_state)
4664 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4666 TRACE("Enumerating resource %p.\n", resource);
4667 if (FAILED(hr = callback(resource)))
4668 return hr;
4672 TRACE("New params:\n");
4673 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4674 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4675 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4676 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4677 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4678 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4679 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4680 TRACE("device_window %p\n", swapchain_desc->device_window);
4681 TRACE("windowed %#x\n", swapchain_desc->windowed);
4682 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4683 if (swapchain_desc->enable_auto_depth_stencil)
4684 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4685 TRACE("flags %#x\n", swapchain_desc->flags);
4686 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4687 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4688 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4690 /* No special treatment of these parameters. Just store them */
4691 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4692 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4693 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4694 swapchain->desc.flags = swapchain_desc->flags;
4695 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4696 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4697 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4699 if (swapchain_desc->device_window
4700 && swapchain_desc->device_window != swapchain->desc.device_window)
4702 TRACE("Changing the device window from %p to %p.\n",
4703 swapchain->desc.device_window, swapchain_desc->device_window);
4704 swapchain->desc.device_window = swapchain_desc->device_window;
4705 swapchain->device_window = swapchain_desc->device_window;
4706 wined3d_swapchain_set_window(swapchain, NULL);
4709 if (mode)
4711 DisplayModeChanged = TRUE;
4712 m = *mode;
4714 else if (swapchain_desc->windowed)
4716 m = swapchain->original_mode;
4718 else
4720 m.width = swapchain_desc->backbuffer_width;
4721 m.height = swapchain_desc->backbuffer_height;
4722 m.refresh_rate = swapchain_desc->refresh_rate;
4723 m.format_id = swapchain_desc->backbuffer_format;
4724 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4726 if ((m.width != swapchain->desc.backbuffer_width
4727 || m.height != swapchain->desc.backbuffer_height))
4728 DisplayModeChanged = TRUE;
4731 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4732 || DisplayModeChanged)
4734 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4736 WARN("Failed to set display mode, hr %#x.\n", hr);
4737 return WINED3DERR_INVALIDCALL;
4740 if (!swapchain_desc->windowed)
4742 if (swapchain->desc.windowed)
4744 HWND focus_window = device->create_parms.focus_window;
4745 if (!focus_window)
4746 focus_window = swapchain_desc->device_window;
4747 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4749 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4750 return hr;
4753 /* switch from windowed to fs */
4754 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4755 swapchain_desc->backbuffer_width,
4756 swapchain_desc->backbuffer_height);
4758 else
4760 /* Fullscreen -> fullscreen mode change */
4761 MoveWindow(swapchain->device_window, 0, 0,
4762 swapchain_desc->backbuffer_width,
4763 swapchain_desc->backbuffer_height,
4764 TRUE);
4766 swapchain->d3d_mode = m;
4768 else if (!swapchain->desc.windowed)
4770 /* Fullscreen -> windowed switch */
4771 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4772 wined3d_device_release_focus_window(device);
4774 swapchain->desc.windowed = swapchain_desc->windowed;
4776 else if (!swapchain_desc->windowed)
4778 DWORD style = device->style;
4779 DWORD exStyle = device->exStyle;
4780 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4781 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4782 * Reset to clear up their mess. Guild Wars also loses the device during that.
4784 device->style = 0;
4785 device->exStyle = 0;
4786 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4787 swapchain_desc->backbuffer_width,
4788 swapchain_desc->backbuffer_height);
4789 device->style = style;
4790 device->exStyle = exStyle;
4793 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4794 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4795 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4796 return hr;
4798 if (device->auto_depth_stencil_view)
4800 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4801 device->auto_depth_stencil_view = NULL;
4803 if (swapchain->desc.enable_auto_depth_stencil)
4805 struct wined3d_resource_desc texture_desc;
4806 struct wined3d_texture *texture;
4808 TRACE("Creating the depth stencil buffer\n");
4810 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4811 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4812 texture_desc.multisample_type = swapchain->desc.multisample_type;
4813 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4814 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4815 texture_desc.pool = WINED3D_POOL_DEFAULT;
4816 texture_desc.width = swapchain->desc.backbuffer_width;
4817 texture_desc.height = swapchain->desc.backbuffer_height;
4818 texture_desc.depth = 1;
4819 texture_desc.size = 0;
4821 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4822 device->device_parent, &texture_desc, &texture)))
4824 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4825 return WINED3DERR_INVALIDCALL;
4828 view_desc.format_id = texture->resource.format->id;
4829 view_desc.u.texture.level_idx = 0;
4830 view_desc.u.texture.layer_idx = 0;
4831 view_desc.u.texture.layer_count = 1;
4832 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4833 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4834 wined3d_texture_decref(texture);
4835 if (FAILED(hr))
4837 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4838 return hr;
4841 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4844 if (device->back_buffer_view)
4846 wined3d_rendertarget_view_decref(device->back_buffer_view);
4847 device->back_buffer_view = NULL;
4849 if (swapchain->desc.backbuffer_count)
4851 view_desc.format_id = swapchain_desc->backbuffer_format;
4852 view_desc.u.texture.level_idx = 0;
4853 view_desc.u.texture.layer_idx = 0;
4854 view_desc.u.texture.layer_count = 1;
4855 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
4856 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4858 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4859 return hr;
4863 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4865 if (reset_state)
4867 TRACE("Resetting stateblock.\n");
4868 if (device->recording)
4870 wined3d_stateblock_decref(device->recording);
4871 device->recording = NULL;
4873 wined3d_cs_emit_reset_state(device->cs);
4874 state_cleanup(&device->state);
4876 if (device->d3d_initialized)
4877 delete_opengl_contexts(device, swapchain);
4879 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4880 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4881 ERR("Failed to initialize device state, hr %#x.\n", hr);
4882 device->update_state = &device->state;
4884 device_init_swapchain_state(device, swapchain);
4886 else if (device->back_buffer_view)
4888 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4889 struct wined3d_state *state = &device->state;
4891 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4893 /* Note the min_z / max_z is not reset. */
4894 state->viewport.x = 0;
4895 state->viewport.y = 0;
4896 state->viewport.width = view->width;
4897 state->viewport.height = view->height;
4898 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4900 state->scissor_rect.top = 0;
4901 state->scissor_rect.left = 0;
4902 state->scissor_rect.right = view->width;
4903 state->scissor_rect.bottom = view->height;
4904 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4907 if (device->d3d_initialized)
4909 if (reset_state)
4910 hr = create_primary_opengl_context(device, swapchain);
4911 swapchain_update_swap_interval(swapchain);
4914 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4915 * first use
4917 return hr;
4920 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4922 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4924 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4926 return WINED3D_OK;
4930 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4931 struct wined3d_device_creation_parameters *parameters)
4933 TRACE("device %p, parameters %p.\n", device, parameters);
4935 *parameters = device->create_parms;
4938 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4940 TRACE("device %p.\n", device);
4942 return device->wined3d;
4945 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4946 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4948 struct wined3d_swapchain *swapchain;
4950 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4951 device, swapchain_idx, flags, ramp);
4953 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4954 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4957 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4958 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4960 struct wined3d_swapchain *swapchain;
4962 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4963 device, swapchain_idx, ramp);
4965 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4966 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4969 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4971 TRACE("device %p, resource %p.\n", device, resource);
4973 list_add_head(&device->resources, &resource->resource_list_entry);
4976 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4978 TRACE("device %p, resource %p.\n", device, resource);
4980 list_remove(&resource->resource_list_entry);
4983 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4985 enum wined3d_resource_type type = resource->type;
4986 struct wined3d_rendertarget_view *rtv;
4987 unsigned int i;
4989 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4991 context_resource_released(device, resource, type);
4993 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4995 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4996 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4999 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5000 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5002 switch (type)
5004 case WINED3D_RTYPE_TEXTURE_2D:
5005 case WINED3D_RTYPE_TEXTURE_3D:
5006 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5008 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5010 if (device->state.textures[i] == texture)
5012 ERR("Texture %p is still in use, stage %u.\n", texture, i);
5013 device->state.textures[i] = NULL;
5016 if (device->recording && device->update_state->textures[i] == texture)
5018 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
5019 texture, device->recording, i);
5020 device->update_state->textures[i] = NULL;
5023 break;
5025 case WINED3D_RTYPE_BUFFER:
5027 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5029 for (i = 0; i < MAX_STREAMS; ++i)
5031 if (device->state.streams[i].buffer == buffer)
5033 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
5034 device->state.streams[i].buffer = NULL;
5037 if (device->recording && device->update_state->streams[i].buffer == buffer)
5039 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5040 buffer, device->recording, i);
5041 device->update_state->streams[i].buffer = NULL;
5045 if (device->state.index_buffer == buffer)
5047 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5048 device->state.index_buffer = NULL;
5051 if (device->recording && device->update_state->index_buffer == buffer)
5053 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5054 buffer, device->recording);
5055 device->update_state->index_buffer = NULL;
5058 break;
5060 default:
5061 break;
5064 /* Remove the resource from the resourceStore */
5065 device_resource_remove(device, resource);
5067 TRACE("Resource released.\n");
5070 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5072 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5074 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5077 static const struct wine_rb_functions wined3d_sampler_rb_functions =
5079 wined3d_rb_alloc,
5080 wined3d_rb_realloc,
5081 wined3d_rb_free,
5082 wined3d_sampler_compare,
5085 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5086 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5087 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5089 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5090 const struct fragment_pipeline *fragment_pipeline;
5091 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5092 unsigned int i;
5093 HRESULT hr;
5095 device->ref = 1;
5096 device->wined3d = wined3d;
5097 wined3d_incref(device->wined3d);
5098 device->adapter = wined3d->adapter_count ? adapter : NULL;
5099 device->device_parent = device_parent;
5100 list_init(&device->resources);
5101 list_init(&device->shaders);
5102 device->surface_alignment = surface_alignment;
5104 /* Save the creation parameters. */
5105 device->create_parms.adapter_idx = adapter_idx;
5106 device->create_parms.device_type = device_type;
5107 device->create_parms.focus_window = focus_window;
5108 device->create_parms.flags = flags;
5110 device->shader_backend = adapter->shader_backend;
5112 vertex_pipeline = adapter->vertex_pipe;
5114 fragment_pipeline = adapter->fragment_pipe;
5116 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
5118 ERR("Failed to initialize sampler rbtree.\n");
5119 return E_OUTOFMEMORY;
5122 if (vertex_pipeline->vp_states && fragment_pipeline->states
5123 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5124 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5125 fragment_pipeline, misc_state_template)))
5127 ERR("Failed to compile state table, hr %#x.\n", hr);
5128 wine_rb_destroy(&device->samplers, NULL, NULL);
5129 wined3d_decref(device->wined3d);
5130 return hr;
5133 device->blitter = adapter->blitter;
5135 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
5136 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
5138 ERR("Failed to initialize device state, hr %#x.\n", hr);
5139 goto err;
5141 device->update_state = &device->state;
5143 if (!(device->cs = wined3d_cs_create(device)))
5145 WARN("Failed to create command stream.\n");
5146 state_cleanup(&device->state);
5147 hr = E_FAIL;
5148 goto err;
5151 return WINED3D_OK;
5153 err:
5154 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5156 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5158 wine_rb_destroy(&device->samplers, NULL, NULL);
5159 wined3d_decref(device->wined3d);
5160 return hr;
5164 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5166 DWORD rep = device->StateTable[state].representative;
5167 struct wined3d_context *context;
5168 DWORD idx;
5169 BYTE shift;
5170 UINT i;
5172 for (i = 0; i < device->context_count; ++i)
5174 context = device->contexts[i];
5175 if(isStateDirty(context, rep)) continue;
5177 context->dirtyArray[context->numDirtyEntries++] = rep;
5178 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5179 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5180 context->isStateDirty[idx] |= (1u << shift);
5184 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5185 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5187 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5189 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5190 window, message, wparam, lparam);
5191 if (unicode)
5192 return DefWindowProcW(window, message, wparam, lparam);
5193 else
5194 return DefWindowProcA(window, message, wparam, lparam);
5197 if (message == WM_DESTROY)
5199 TRACE("unregister window %p.\n", window);
5200 wined3d_unregister_window(window);
5202 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5203 ERR("Window %p is not the focus window for device %p.\n", window, device);
5205 else if (message == WM_DISPLAYCHANGE)
5207 device->device_parent->ops->mode_changed(device->device_parent);
5209 else if (message == WM_ACTIVATEAPP)
5211 UINT i;
5213 for (i = 0; i < device->swapchain_count; i++)
5214 wined3d_swapchain_activate(device->swapchains[i], wparam);
5216 device->device_parent->ops->activate(device->device_parent, wparam);
5218 else if (message == WM_SYSCOMMAND)
5220 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5222 if (unicode)
5223 DefWindowProcW(window, message, wparam, lparam);
5224 else
5225 DefWindowProcA(window, message, wparam, lparam);
5229 if (unicode)
5230 return CallWindowProcW(proc, window, message, wparam, lparam);
5231 else
5232 return CallWindowProcA(proc, window, message, wparam, lparam);