wined3d: Return a wined3d_texture_sub_resource structure from wined3d_texture_get_sub...
[wine.git] / dlls / wined3d / device.c
blob523153349739574f3caa1f7c438bd473a8e4fc65
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_gl_info *gl_info;
298 UINT drawable_width, drawable_height;
299 struct wined3d_color corrected_color;
300 struct wined3d_context *context;
301 GLbitfield clear_mask = 0;
302 BOOL render_offscreen;
303 unsigned int i;
304 RECT ds_rect;
306 context = context_acquire(device, target);
307 if (!context->valid)
309 context_release(context);
310 WARN("Invalid context, skipping clear.\n");
311 return;
313 gl_info = context->gl_info;
315 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
316 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
317 * for the cleared parts, and the untouched parts.
319 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
320 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
321 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
322 * checking all this if the dest surface is in the drawable anyway. */
323 for (i = 0; i < rt_count; ++i)
325 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
326 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(rtv);
328 if (rt && rtv->format->id != WINED3DFMT_NULL)
330 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
331 surface_load_location(rt, context, rtv->resource->draw_binding);
332 else
333 wined3d_surface_prepare(rt, context, rtv->resource->draw_binding);
337 if (target)
339 render_offscreen = context->render_offscreen;
340 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
342 else
344 render_offscreen = TRUE;
345 drawable_width = depth_stencil->pow2Width;
346 drawable_height = depth_stencil->pow2Height;
349 if (depth_stencil && render_offscreen)
350 wined3d_surface_prepare(depth_stencil, context, depth_stencil->container->resource.draw_binding);
352 if (flags & WINED3DCLEAR_ZBUFFER)
354 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
356 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
357 device_switch_onscreen_ds(device, context, depth_stencil);
358 prepare_ds_clear(depth_stencil, context, location,
359 draw_rect, rect_count, clear_rect, &ds_rect);
362 if (!context_apply_clear_state(context, device, rt_count, fb))
364 context_release(context);
365 WARN("Failed to apply clear state, skipping clear.\n");
366 return;
369 /* Only set the values up once, as they are not changing. */
370 if (flags & WINED3DCLEAR_STENCIL)
372 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
374 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
375 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
377 gl_info->gl_ops.gl.p_glStencilMask(~0U);
378 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
379 gl_info->gl_ops.gl.p_glClearStencil(stencil);
380 checkGLcall("glClearStencil");
381 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
384 if (flags & WINED3DCLEAR_ZBUFFER)
386 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
388 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
390 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
391 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
392 gl_info->gl_ops.gl.p_glClearDepth(depth);
393 checkGLcall("glClearDepth");
394 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
397 if (flags & WINED3DCLEAR_TARGET)
399 for (i = 0; i < rt_count; ++i)
401 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
402 struct wined3d_texture *texture;
404 if (!rtv)
405 continue;
407 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
409 FIXME("Not supported on buffer resources.\n");
410 continue;
413 texture = wined3d_texture_from_resource(rtv->resource);
414 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
415 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
418 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, &device->state, fb))
420 if (rt_count > 1)
421 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
422 "support, this might cause graphical issues.\n");
424 corrected_color.r = color->r < wined3d_srgb_const1[0]
425 ? color->r * wined3d_srgb_const0[3]
426 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
427 - wined3d_srgb_const0[2];
428 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
429 corrected_color.g = color->g < wined3d_srgb_const1[0]
430 ? color->g * wined3d_srgb_const0[3]
431 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
432 - wined3d_srgb_const0[2];
433 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
434 corrected_color.b = color->b < wined3d_srgb_const1[0]
435 ? color->b * wined3d_srgb_const0[3]
436 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
437 - wined3d_srgb_const0[2];
438 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
439 color = &corrected_color;
442 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
443 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
444 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
445 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
446 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
447 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
448 checkGLcall("glClearColor");
449 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
452 if (!rect_count)
454 if (render_offscreen)
456 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
457 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
459 else
461 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
462 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
464 checkGLcall("glScissor");
465 gl_info->gl_ops.gl.p_glClear(clear_mask);
466 checkGLcall("glClear");
468 else
470 RECT current_rect;
472 /* Now process each rect in turn. */
473 for (i = 0; i < rect_count; ++i)
475 /* Note that GL uses lower left, width/height. */
476 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
478 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
479 wine_dbgstr_rect(&clear_rect[i]),
480 wine_dbgstr_rect(&current_rect));
482 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
483 * The rectangle is not cleared, no error is returned, but further rectangles are
484 * still cleared if they are valid. */
485 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
487 TRACE("Rectangle with negative dimensions, ignoring.\n");
488 continue;
491 if (render_offscreen)
493 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
494 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
496 else
498 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
499 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
501 checkGLcall("glScissor");
503 gl_info->gl_ops.gl.p_glClear(clear_mask);
504 checkGLcall("glClear");
508 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
509 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
510 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
512 context_release(context);
515 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
517 ULONG refcount = InterlockedIncrement(&device->ref);
519 TRACE("%p increasing refcount to %u.\n", device, refcount);
521 return refcount;
524 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
526 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
528 ERR("Leftover sampler %p.\n", sampler);
531 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
533 ULONG refcount = InterlockedDecrement(&device->ref);
535 TRACE("%p decreasing refcount to %u.\n", device, refcount);
537 if (!refcount)
539 UINT i;
541 wined3d_cs_destroy(device->cs);
543 if (device->recording && wined3d_stateblock_decref(device->recording))
544 FIXME("Something's still holding the recording stateblock.\n");
545 device->recording = NULL;
547 state_cleanup(&device->state);
549 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
551 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
552 device->multistate_funcs[i] = NULL;
555 if (!list_empty(&device->resources))
557 struct wined3d_resource *resource;
559 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
561 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
563 FIXME("Leftover resource %p with type %s (%#x).\n",
564 resource, debug_d3dresourcetype(resource->type), resource->type);
568 if (device->contexts)
569 ERR("Context array not freed!\n");
570 if (device->hardwareCursor)
571 DestroyCursor(device->hardwareCursor);
572 device->hardwareCursor = 0;
574 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
576 wined3d_decref(device->wined3d);
577 device->wined3d = NULL;
578 HeapFree(GetProcessHeap(), 0, device);
579 TRACE("Freed device %p.\n", device);
582 return refcount;
585 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
587 TRACE("device %p.\n", device);
589 return device->swapchain_count;
592 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
594 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
596 if (swapchain_idx >= device->swapchain_count)
598 WARN("swapchain_idx %u >= swapchain_count %u.\n",
599 swapchain_idx, device->swapchain_count);
600 return NULL;
603 return device->swapchains[swapchain_idx];
606 static void device_load_logo(struct wined3d_device *device, const char *filename)
608 struct wined3d_color_key color_key;
609 struct wined3d_resource_desc desc;
610 HBITMAP hbm;
611 BITMAP bm;
612 HRESULT hr;
613 HDC dcb = NULL, dcs = NULL;
615 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
616 if(hbm)
618 GetObjectA(hbm, sizeof(BITMAP), &bm);
619 dcb = CreateCompatibleDC(NULL);
620 if(!dcb) goto out;
621 SelectObject(dcb, hbm);
623 else
625 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
626 * couldn't be loaded
628 memset(&bm, 0, sizeof(bm));
629 bm.bmWidth = 32;
630 bm.bmHeight = 32;
633 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
634 desc.format = WINED3DFMT_B5G6R5_UNORM;
635 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
636 desc.multisample_quality = 0;
637 desc.usage = WINED3DUSAGE_DYNAMIC;
638 desc.pool = WINED3D_POOL_DEFAULT;
639 desc.width = bm.bmWidth;
640 desc.height = bm.bmHeight;
641 desc.depth = 1;
642 desc.size = 0;
643 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
644 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
646 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
647 goto out;
650 if (dcb)
652 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
653 goto out;
654 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
655 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
657 color_key.color_space_low_value = 0;
658 color_key.color_space_high_value = 0;
659 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
661 else
663 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
664 const RECT rect = {0, 0, desc.width, desc.height};
665 struct wined3d_surface *surface;
667 /* Fill the surface with a white color to show that wined3d is there */
668 surface = device->logo_texture->sub_resources[0].u.surface;
669 surface_color_fill(surface, &rect, &c);
672 out:
673 if (dcb) DeleteDC(dcb);
674 if (hbm) DeleteObject(hbm);
677 /* Context activation is done by the caller. */
678 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
680 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
681 unsigned int i, j, count;
682 /* Under DirectX you can sample even if no texture is bound, whereas
683 * OpenGL will only allow that when a valid texture is bound.
684 * We emulate this by creating dummy textures and binding them
685 * to each texture stage when the currently set D3D texture is NULL. */
687 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
688 for (i = 0; i < count; ++i)
690 static const DWORD color = 0x000000ff;
692 /* Make appropriate texture active */
693 context_active_texture(context, gl_info, i);
695 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
696 checkGLcall("glGenTextures");
697 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
699 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
700 checkGLcall("glBindTexture");
702 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
703 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
704 checkGLcall("glTexImage2D");
706 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
708 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
709 checkGLcall("glGenTextures");
710 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
712 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
713 checkGLcall("glBindTexture");
715 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
716 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
717 checkGLcall("glTexImage2D");
720 if (gl_info->supported[EXT_TEXTURE3D])
722 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
723 checkGLcall("glGenTextures");
724 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
726 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
727 checkGLcall("glBindTexture");
729 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
730 checkGLcall("glTexImage3D");
733 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
735 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
736 checkGLcall("glGenTextures");
737 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
739 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
740 checkGLcall("glBindTexture");
742 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
744 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
745 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
746 checkGLcall("glTexImage2D");
752 /* Context activation is done by the caller. */
753 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
755 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
757 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
759 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
760 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
763 if (gl_info->supported[EXT_TEXTURE3D])
765 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
766 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
769 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
771 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
772 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
775 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
776 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
778 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
779 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
780 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
781 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
784 /* Context activation is done by the caller. */
785 static void create_default_sampler(struct wined3d_device *device)
787 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
790 * In SM4+ shaders there is a separation between resources and samplers. Some of shader
791 * instructions allow to access resources without using samplers.
792 * In GLSL resources are always accessed through sampler or image variables. The default
793 * sampler object is used to emulate the direct resource access when there is no sampler state
794 * to use.
797 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
799 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
800 checkGLcall("glGenSamplers");
801 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
802 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
803 checkGLcall("glSamplerParamteri");
805 else
807 device->default_sampler = 0;
811 /* Context activation is done by the caller. */
812 static void destroy_default_sampler(struct wined3d_device *device)
814 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
816 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
818 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
819 checkGLcall("glDeleteSamplers");
822 device->default_sampler = 0;
825 static LONG fullscreen_style(LONG style)
827 /* Make sure the window is managed, otherwise we won't get keyboard input. */
828 style |= WS_POPUP | WS_SYSMENU;
829 style &= ~(WS_CAPTION | WS_THICKFRAME);
831 return style;
834 static LONG fullscreen_exstyle(LONG exstyle)
836 /* Filter out window decorations. */
837 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
839 return exstyle;
842 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
844 BOOL filter_messages;
845 LONG style, exstyle;
847 TRACE("Setting up window %p for fullscreen mode.\n", window);
849 if (device->style || device->exStyle)
851 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
852 window, device->style, device->exStyle);
855 device->style = GetWindowLongW(window, GWL_STYLE);
856 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
858 style = fullscreen_style(device->style);
859 exstyle = fullscreen_exstyle(device->exStyle);
861 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
862 device->style, device->exStyle, style, exstyle);
864 filter_messages = device->filter_messages;
865 device->filter_messages = TRUE;
867 SetWindowLongW(window, GWL_STYLE, style);
868 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
869 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
871 device->filter_messages = filter_messages;
874 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
876 BOOL filter_messages;
877 LONG style, exstyle;
879 if (!device->style && !device->exStyle) return;
881 style = GetWindowLongW(window, GWL_STYLE);
882 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
884 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
885 * application, and we want to ignore them in the test below, since it's
886 * not the application's fault that they changed. Additionally, we want to
887 * preserve the current status of these flags (i.e. don't restore them) to
888 * more closely emulate the behavior of Direct3D, which leaves these flags
889 * alone when returning to windowed mode. */
890 device->style ^= (device->style ^ style) & WS_VISIBLE;
891 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
893 TRACE("Restoring window style of window %p to %08x, %08x.\n",
894 window, device->style, device->exStyle);
896 filter_messages = device->filter_messages;
897 device->filter_messages = TRUE;
899 /* Only restore the style if the application didn't modify it during the
900 * fullscreen phase. Some applications change it before calling Reset()
901 * when switching between windowed and fullscreen modes (HL2), some
902 * depend on the original style (Eve Online). */
903 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
905 SetWindowLongW(window, GWL_STYLE, device->style);
906 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
908 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
910 device->filter_messages = filter_messages;
912 /* Delete the old values. */
913 device->style = 0;
914 device->exStyle = 0;
917 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
919 TRACE("device %p, window %p.\n", device, window);
921 if (!wined3d_register_window(window, device))
923 ERR("Failed to register window %p.\n", window);
924 return E_FAIL;
927 InterlockedExchangePointer((void **)&device->focus_window, window);
928 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
930 return WINED3D_OK;
933 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
935 TRACE("device %p.\n", device);
937 if (device->focus_window) wined3d_unregister_window(device->focus_window);
938 InterlockedExchangePointer((void **)&device->focus_window, NULL);
941 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
943 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
944 unsigned int i;
946 if (device->fb.render_targets)
948 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
950 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
952 if (device->back_buffer_view)
953 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
956 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
957 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
960 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
961 struct wined3d_swapchain_desc *swapchain_desc)
963 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
964 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
965 struct wined3d_swapchain *swapchain = NULL;
966 struct wined3d_context *context;
967 DWORD clear_flags = 0;
968 HRESULT hr;
970 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
972 if (device->d3d_initialized)
973 return WINED3DERR_INVALIDCALL;
974 if (device->wined3d->flags & WINED3D_NO3D)
975 return WINED3DERR_INVALIDCALL;
977 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
978 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
980 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
981 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
983 TRACE("Shader private data couldn't be allocated\n");
984 goto err_out;
986 if (FAILED(hr = device->blitter->alloc_private(device)))
988 TRACE("Blitter private data couldn't be allocated\n");
989 goto err_out;
992 /* Setup the implicit swapchain. This also initializes a context. */
993 TRACE("Creating implicit swapchain\n");
994 hr = device->device_parent->ops->create_swapchain(device->device_parent,
995 swapchain_desc, &swapchain);
996 if (FAILED(hr))
998 WARN("Failed to create implicit swapchain\n");
999 goto err_out;
1002 if (swapchain_desc->backbuffer_count)
1004 struct wined3d_rendertarget_view_desc view_desc;
1006 view_desc.format_id = swapchain_desc->backbuffer_format;
1007 view_desc.u.texture.level_idx = 0;
1008 view_desc.u.texture.layer_idx = 0;
1009 view_desc.u.texture.layer_count = 1;
1010 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
1011 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1013 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1014 goto err_out;
1018 device->swapchain_count = 1;
1019 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1020 if (!device->swapchains)
1022 ERR("Out of memory!\n");
1023 goto err_out;
1025 device->swapchains[0] = swapchain;
1026 device_init_swapchain_state(device, swapchain);
1028 context = context_acquire(device, swapchain->front_buffer->sub_resources[0].u.surface);
1030 create_dummy_textures(device, context);
1031 create_default_sampler(device);
1033 device->contexts[0]->last_was_rhw = 0;
1035 TRACE("All defaults now set up, leaving 3D init.\n");
1037 context_release(context);
1039 /* Clear the screen */
1040 if (swapchain->back_buffers && swapchain->back_buffers[0])
1041 clear_flags |= WINED3DCLEAR_TARGET;
1042 if (swapchain_desc->enable_auto_depth_stencil)
1043 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1044 if (clear_flags)
1045 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1047 device->d3d_initialized = TRUE;
1049 if (wined3d_settings.logo)
1050 device_load_logo(device, wined3d_settings.logo);
1051 return WINED3D_OK;
1053 err_out:
1054 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1055 HeapFree(GetProcessHeap(), 0, device->swapchains);
1056 device->swapchain_count = 0;
1057 if (device->back_buffer_view)
1058 wined3d_rendertarget_view_decref(device->back_buffer_view);
1059 if (swapchain)
1060 wined3d_swapchain_decref(swapchain);
1061 if (device->blit_priv)
1062 device->blitter->free_private(device);
1063 if (device->shader_priv)
1064 device->shader_backend->shader_free_private(device);
1066 return hr;
1069 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1070 struct wined3d_swapchain_desc *swapchain_desc)
1072 struct wined3d_swapchain *swapchain = NULL;
1073 HRESULT hr;
1075 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1077 /* Setup the implicit swapchain */
1078 TRACE("Creating implicit swapchain\n");
1079 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1080 swapchain_desc, &swapchain);
1081 if (FAILED(hr))
1083 WARN("Failed to create implicit swapchain\n");
1084 goto err_out;
1087 device->swapchain_count = 1;
1088 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1089 if (!device->swapchains)
1091 ERR("Out of memory!\n");
1092 goto err_out;
1094 device->swapchains[0] = swapchain;
1095 return WINED3D_OK;
1097 err_out:
1098 wined3d_swapchain_decref(swapchain);
1099 return hr;
1102 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1104 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1106 wined3d_sampler_decref(sampler);
1109 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1111 struct wined3d_resource *resource, *cursor;
1112 const struct wined3d_gl_info *gl_info;
1113 struct wined3d_context *context;
1114 struct wined3d_surface *surface;
1115 UINT i;
1117 TRACE("device %p.\n", device);
1119 if (!device->d3d_initialized)
1120 return WINED3DERR_INVALIDCALL;
1122 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1123 * it was created. Thus make sure a context is active for the glDelete* calls
1125 context = context_acquire(device, NULL);
1126 gl_info = context->gl_info;
1128 if (device->logo_texture)
1129 wined3d_texture_decref(device->logo_texture);
1130 if (device->cursor_texture)
1131 wined3d_texture_decref(device->cursor_texture);
1133 state_unbind_resources(&device->state);
1135 /* Unload resources */
1136 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1138 TRACE("Unloading resource %p.\n", resource);
1140 resource->resource_ops->resource_unload(resource);
1143 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1145 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1146 * private data, it might contain opengl pointers
1148 if (device->depth_blt_texture)
1150 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1151 device->depth_blt_texture = 0;
1154 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1155 device->blitter->free_private(device);
1156 device->shader_backend->shader_free_private(device);
1157 destroy_dummy_textures(device, gl_info);
1158 destroy_default_sampler(device);
1160 /* Release the context again as soon as possible. In particular,
1161 * releasing the render target views below may release the last reference
1162 * to the swapchain associated with this context, which in turn will
1163 * destroy the context. */
1164 context_release(context);
1166 /* Release the buffers (with sanity checks)*/
1167 if (device->onscreen_depth_stencil)
1169 surface = device->onscreen_depth_stencil;
1170 device->onscreen_depth_stencil = NULL;
1171 wined3d_texture_decref(surface->container);
1174 if (device->fb.depth_stencil)
1176 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1178 TRACE("Releasing depth/stencil view %p.\n", view);
1180 device->fb.depth_stencil = NULL;
1181 wined3d_rendertarget_view_decref(view);
1184 if (device->auto_depth_stencil_view)
1186 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1188 device->auto_depth_stencil_view = NULL;
1189 if (wined3d_rendertarget_view_decref(view))
1190 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1193 for (i = 0; i < gl_info->limits.buffers; ++i)
1195 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1197 if (device->back_buffer_view)
1199 wined3d_rendertarget_view_decref(device->back_buffer_view);
1200 device->back_buffer_view = NULL;
1203 for (i = 0; i < device->swapchain_count; ++i)
1205 TRACE("Releasing the implicit swapchain %u.\n", i);
1206 if (wined3d_swapchain_decref(device->swapchains[i]))
1207 FIXME("Something's still holding the implicit swapchain.\n");
1210 HeapFree(GetProcessHeap(), 0, device->swapchains);
1211 device->swapchains = NULL;
1212 device->swapchain_count = 0;
1214 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1215 device->fb.render_targets = NULL;
1217 device->d3d_initialized = FALSE;
1219 return WINED3D_OK;
1222 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1224 unsigned int i;
1226 for (i = 0; i < device->swapchain_count; ++i)
1228 TRACE("Releasing the implicit swapchain %u.\n", i);
1229 if (wined3d_swapchain_decref(device->swapchains[i]))
1230 FIXME("Something's still holding the implicit swapchain.\n");
1233 HeapFree(GetProcessHeap(), 0, device->swapchains);
1234 device->swapchains = NULL;
1235 device->swapchain_count = 0;
1236 return WINED3D_OK;
1239 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1240 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1241 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1243 * There is no way to deactivate thread safety once it is enabled.
1245 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1247 TRACE("device %p.\n", device);
1249 /* For now just store the flag (needed in case of ddraw). */
1250 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1253 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1255 TRACE("device %p.\n", device);
1257 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1258 wine_dbgstr_longlong(device->adapter->vram_bytes),
1259 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1260 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1262 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1265 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1266 struct wined3d_buffer *buffer, UINT offset)
1268 struct wined3d_stream_output *stream;
1269 struct wined3d_buffer *prev_buffer;
1271 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1273 if (idx >= MAX_STREAM_OUT)
1275 WARN("Invalid stream output %u.\n", idx);
1276 return;
1279 stream = &device->update_state->stream_output[idx];
1280 prev_buffer = stream->buffer;
1282 if (buffer)
1283 wined3d_buffer_incref(buffer);
1284 stream->buffer = buffer;
1285 stream->offset = offset;
1286 if (!device->recording)
1287 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1288 if (prev_buffer)
1289 wined3d_buffer_decref(prev_buffer);
1292 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1293 UINT idx, UINT *offset)
1295 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1297 if (idx >= MAX_STREAM_OUT)
1299 WARN("Invalid stream output %u.\n", idx);
1300 return NULL;
1303 if (offset)
1304 *offset = device->state.stream_output[idx].offset;
1305 return device->state.stream_output[idx].buffer;
1308 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1309 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1311 struct wined3d_stream_state *stream;
1312 struct wined3d_buffer *prev_buffer;
1314 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1315 device, stream_idx, buffer, offset, stride);
1317 if (stream_idx >= MAX_STREAMS)
1319 WARN("Stream index %u out of range.\n", stream_idx);
1320 return WINED3DERR_INVALIDCALL;
1322 else if (offset & 0x3)
1324 WARN("Offset %u is not 4 byte aligned.\n", offset);
1325 return WINED3DERR_INVALIDCALL;
1328 stream = &device->update_state->streams[stream_idx];
1329 prev_buffer = stream->buffer;
1331 if (device->recording)
1332 device->recording->changed.streamSource |= 1u << stream_idx;
1334 if (prev_buffer == buffer
1335 && stream->stride == stride
1336 && stream->offset == offset)
1338 TRACE("Application is setting the old values over, nothing to do.\n");
1339 return WINED3D_OK;
1342 stream->buffer = buffer;
1343 if (buffer)
1345 stream->stride = stride;
1346 stream->offset = offset;
1347 wined3d_buffer_incref(buffer);
1350 if (!device->recording)
1351 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1352 if (prev_buffer)
1353 wined3d_buffer_decref(prev_buffer);
1355 return WINED3D_OK;
1358 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1359 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1361 const struct wined3d_stream_state *stream;
1363 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1364 device, stream_idx, buffer, offset, stride);
1366 if (stream_idx >= MAX_STREAMS)
1368 WARN("Stream index %u out of range.\n", stream_idx);
1369 return WINED3DERR_INVALIDCALL;
1372 stream = &device->state.streams[stream_idx];
1373 *buffer = stream->buffer;
1374 if (offset)
1375 *offset = stream->offset;
1376 *stride = stream->stride;
1378 return WINED3D_OK;
1381 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1383 struct wined3d_stream_state *stream;
1384 UINT old_flags, old_freq;
1386 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1388 /* Verify input. At least in d3d9 this is invalid. */
1389 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1391 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1392 return WINED3DERR_INVALIDCALL;
1394 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1396 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1397 return WINED3DERR_INVALIDCALL;
1399 if (!divider)
1401 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1402 return WINED3DERR_INVALIDCALL;
1405 stream = &device->update_state->streams[stream_idx];
1406 old_flags = stream->flags;
1407 old_freq = stream->frequency;
1409 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1410 stream->frequency = divider & 0x7fffff;
1412 if (device->recording)
1413 device->recording->changed.streamFreq |= 1u << stream_idx;
1414 else if (stream->frequency != old_freq || stream->flags != old_flags)
1415 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1417 return WINED3D_OK;
1420 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1421 UINT stream_idx, UINT *divider)
1423 const struct wined3d_stream_state *stream;
1425 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1427 stream = &device->state.streams[stream_idx];
1428 *divider = stream->flags | stream->frequency;
1430 TRACE("Returning %#x.\n", *divider);
1432 return WINED3D_OK;
1435 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1436 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1438 TRACE("device %p, state %s, matrix %p.\n",
1439 device, debug_d3dtstype(d3dts), matrix);
1440 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1441 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1442 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1443 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1445 /* Handle recording of state blocks. */
1446 if (device->recording)
1448 TRACE("Recording... not performing anything.\n");
1449 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1450 device->update_state->transforms[d3dts] = *matrix;
1451 return;
1454 /* If the new matrix is the same as the current one,
1455 * we cut off any further processing. this seems to be a reasonable
1456 * optimization because as was noticed, some apps (warcraft3 for example)
1457 * tend towards setting the same matrix repeatedly for some reason.
1459 * From here on we assume that the new matrix is different, wherever it matters. */
1460 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1462 TRACE("The application is setting the same matrix over again.\n");
1463 return;
1466 device->state.transforms[d3dts] = *matrix;
1467 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1470 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1471 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1473 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1475 *matrix = device->state.transforms[state];
1478 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1479 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1481 const struct wined3d_matrix *mat;
1482 struct wined3d_matrix temp;
1484 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1486 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1487 * below means it will be recorded in a state block change, but it
1488 * works regardless where it is recorded.
1489 * If this is found to be wrong, change to StateBlock. */
1490 if (state > HIGHEST_TRANSFORMSTATE)
1492 WARN("Unhandled transform state %#x.\n", state);
1493 return;
1496 mat = &device->update_state->transforms[state];
1497 multiply_matrix(&temp, mat, matrix);
1499 /* Apply change via set transform - will reapply to eg. lights this way. */
1500 wined3d_device_set_transform(device, state, &temp);
1503 /* Note lights are real special cases. Although the device caps state only
1504 * e.g. 8 are supported, you can reference any indexes you want as long as
1505 * that number max are enabled at any one point in time. Therefore since the
1506 * indices can be anything, we need a hashmap of them. However, this causes
1507 * stateblock problems. When capturing the state block, I duplicate the
1508 * hashmap, but when recording, just build a chain pretty much of commands to
1509 * be replayed. */
1510 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1511 UINT light_idx, const struct wined3d_light *light)
1513 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1514 struct wined3d_light_info *object = NULL;
1515 struct list *e;
1516 float rho;
1518 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1520 /* Check the parameter range. Need for speed most wanted sets junk lights
1521 * which confuse the GL driver. */
1522 if (!light)
1523 return WINED3DERR_INVALIDCALL;
1525 switch (light->type)
1527 case WINED3D_LIGHT_POINT:
1528 case WINED3D_LIGHT_SPOT:
1529 case WINED3D_LIGHT_GLSPOT:
1530 /* Incorrect attenuation values can cause the gl driver to crash.
1531 * Happens with Need for speed most wanted. */
1532 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1534 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1535 return WINED3DERR_INVALIDCALL;
1537 break;
1539 case WINED3D_LIGHT_DIRECTIONAL:
1540 case WINED3D_LIGHT_PARALLELPOINT:
1541 /* Ignores attenuation */
1542 break;
1544 default:
1545 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1546 return WINED3DERR_INVALIDCALL;
1549 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1551 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1552 if (object->OriginalIndex == light_idx)
1553 break;
1554 object = NULL;
1557 if (!object)
1559 TRACE("Adding new light\n");
1560 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1561 if (!object)
1562 return E_OUTOFMEMORY;
1564 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1565 object->glIndex = -1;
1566 object->OriginalIndex = light_idx;
1569 /* Initialize the object. */
1570 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1571 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1572 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1573 light_idx, light->type, debug_color(&light->diffuse),
1574 debug_color(&light->specular), debug_color(&light->ambient),
1575 light->position.x, light->position.y, light->position.z,
1576 light->direction.x, light->direction.y, light->direction.z,
1577 light->range, light->falloff, light->theta, light->phi);
1579 /* Update the live definitions if the light is currently assigned a glIndex. */
1580 if (object->glIndex != -1 && !device->recording)
1582 if (object->OriginalParms.type != light->type)
1583 device_invalidate_state(device, STATE_LIGHT_TYPE);
1584 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1587 /* Save away the information. */
1588 object->OriginalParms = *light;
1590 switch (light->type)
1592 case WINED3D_LIGHT_POINT:
1593 /* Position */
1594 object->position.x = light->position.x;
1595 object->position.y = light->position.y;
1596 object->position.z = light->position.z;
1597 object->position.w = 1.0f;
1598 object->cutoff = 180.0f;
1599 /* FIXME: Range */
1600 break;
1602 case WINED3D_LIGHT_DIRECTIONAL:
1603 /* Direction */
1604 object->direction.x = -light->direction.x;
1605 object->direction.y = -light->direction.y;
1606 object->direction.z = -light->direction.z;
1607 object->direction.w = 0.0f;
1608 object->exponent = 0.0f;
1609 object->cutoff = 180.0f;
1610 break;
1612 case WINED3D_LIGHT_SPOT:
1613 /* Position */
1614 object->position.x = light->position.x;
1615 object->position.y = light->position.y;
1616 object->position.z = light->position.z;
1617 object->position.w = 1.0f;
1619 /* Direction */
1620 object->direction.x = light->direction.x;
1621 object->direction.y = light->direction.y;
1622 object->direction.z = light->direction.z;
1623 object->direction.w = 0.0f;
1625 /* opengl-ish and d3d-ish spot lights use too different models
1626 * for the light "intensity" as a function of the angle towards
1627 * the main light direction, so we only can approximate very
1628 * roughly. However, spot lights are rather rarely used in games
1629 * (if ever used at all). Furthermore if still used, probably
1630 * nobody pays attention to such details. */
1631 if (!light->falloff)
1633 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1634 * equations have the falloff resp. exponent parameter as an
1635 * exponent, so the spot light lighting will always be 1.0 for
1636 * both of them, and we don't have to care for the rest of the
1637 * rather complex calculation. */
1638 object->exponent = 0.0f;
1640 else
1642 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1643 if (rho < 0.0001f)
1644 rho = 0.0001f;
1645 object->exponent = -0.3f / logf(cosf(rho / 2));
1648 if (object->exponent > 128.0f)
1649 object->exponent = 128.0f;
1651 object->cutoff = (float)(light->phi * 90 / M_PI);
1652 /* FIXME: Range */
1653 break;
1655 case WINED3D_LIGHT_PARALLELPOINT:
1656 object->position.x = light->position.x;
1657 object->position.y = light->position.y;
1658 object->position.z = light->position.z;
1659 object->position.w = 1.0f;
1660 break;
1662 default:
1663 FIXME("Unrecognized light type %#x.\n", light->type);
1666 return WINED3D_OK;
1669 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1670 UINT light_idx, struct wined3d_light *light)
1672 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1673 struct wined3d_light_info *light_info = NULL;
1674 struct list *e;
1676 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1678 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1680 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1681 if (light_info->OriginalIndex == light_idx)
1682 break;
1683 light_info = NULL;
1686 if (!light_info)
1688 TRACE("Light information requested but light not defined\n");
1689 return WINED3DERR_INVALIDCALL;
1692 *light = light_info->OriginalParms;
1693 return WINED3D_OK;
1696 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1698 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1699 struct wined3d_light_info *light_info = NULL;
1700 struct list *e;
1702 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1704 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1706 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1707 if (light_info->OriginalIndex == light_idx)
1708 break;
1709 light_info = NULL;
1711 TRACE("Found light %p.\n", light_info);
1713 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1714 if (!light_info)
1716 TRACE("Light enabled requested but light not defined, so defining one!\n");
1717 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1719 /* Search for it again! Should be fairly quick as near head of list. */
1720 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1722 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1723 if (light_info->OriginalIndex == light_idx)
1724 break;
1725 light_info = NULL;
1727 if (!light_info)
1729 FIXME("Adding default lights has failed dismally\n");
1730 return WINED3DERR_INVALIDCALL;
1734 if (!enable)
1736 if (light_info->glIndex != -1)
1738 if (!device->recording)
1740 device_invalidate_state(device, STATE_LIGHT_TYPE);
1741 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1744 device->update_state->lights[light_info->glIndex] = NULL;
1745 light_info->glIndex = -1;
1747 else
1749 TRACE("Light already disabled, nothing to do\n");
1751 light_info->enabled = FALSE;
1753 else
1755 light_info->enabled = TRUE;
1756 if (light_info->glIndex != -1)
1758 TRACE("Nothing to do as light was enabled\n");
1760 else
1762 unsigned int i;
1763 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1764 /* Find a free GL light. */
1765 for (i = 0; i < gl_info->limits.lights; ++i)
1767 if (!device->update_state->lights[i])
1769 device->update_state->lights[i] = light_info;
1770 light_info->glIndex = i;
1771 break;
1774 if (light_info->glIndex == -1)
1776 /* Our tests show that Windows returns D3D_OK in this situation, even with
1777 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1778 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1779 * as well for those lights.
1781 * TODO: Test how this affects rendering. */
1782 WARN("Too many concurrently active lights\n");
1783 return WINED3D_OK;
1786 /* i == light_info->glIndex */
1787 if (!device->recording)
1789 device_invalidate_state(device, STATE_LIGHT_TYPE);
1790 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1795 return WINED3D_OK;
1798 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1800 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1801 struct wined3d_light_info *light_info = NULL;
1802 struct list *e;
1804 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1806 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1808 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1809 if (light_info->OriginalIndex == light_idx)
1810 break;
1811 light_info = NULL;
1814 if (!light_info)
1816 TRACE("Light enabled state requested but light not defined.\n");
1817 return WINED3DERR_INVALIDCALL;
1819 /* true is 128 according to SetLightEnable */
1820 *enable = light_info->enabled ? 128 : 0;
1821 return WINED3D_OK;
1824 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1825 UINT plane_idx, const struct wined3d_vec4 *plane)
1827 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1829 /* Validate plane_idx. */
1830 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1832 TRACE("Application has requested clipplane this device doesn't support.\n");
1833 return WINED3DERR_INVALIDCALL;
1836 if (device->recording)
1837 device->recording->changed.clipplane |= 1u << plane_idx;
1839 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1841 TRACE("Application is setting old values over, nothing to do.\n");
1842 return WINED3D_OK;
1845 device->update_state->clip_planes[plane_idx] = *plane;
1847 if (!device->recording)
1848 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1850 return WINED3D_OK;
1853 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1854 UINT plane_idx, struct wined3d_vec4 *plane)
1856 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1858 /* Validate plane_idx. */
1859 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1861 TRACE("Application has requested clipplane this device doesn't support.\n");
1862 return WINED3DERR_INVALIDCALL;
1865 *plane = device->state.clip_planes[plane_idx];
1867 return WINED3D_OK;
1870 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1871 const struct wined3d_clip_status *clip_status)
1873 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1875 if (!clip_status)
1876 return WINED3DERR_INVALIDCALL;
1878 return WINED3D_OK;
1881 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1882 struct wined3d_clip_status *clip_status)
1884 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1886 if (!clip_status)
1887 return WINED3DERR_INVALIDCALL;
1889 return WINED3D_OK;
1892 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1894 TRACE("device %p, material %p.\n", device, material);
1896 device->update_state->material = *material;
1898 if (device->recording)
1899 device->recording->changed.material = TRUE;
1900 else
1901 wined3d_cs_emit_set_material(device->cs, material);
1904 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1906 TRACE("device %p, material %p.\n", device, material);
1908 *material = device->state.material;
1910 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1911 TRACE("ambient %s\n", debug_color(&material->ambient));
1912 TRACE("specular %s\n", debug_color(&material->specular));
1913 TRACE("emissive %s\n", debug_color(&material->emissive));
1914 TRACE("power %.8e.\n", material->power);
1917 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1918 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1920 enum wined3d_format_id prev_format;
1921 struct wined3d_buffer *prev_buffer;
1923 TRACE("device %p, buffer %p, format %s.\n",
1924 device, buffer, debug_d3dformat(format_id));
1926 prev_buffer = device->update_state->index_buffer;
1927 prev_format = device->update_state->index_format;
1929 device->update_state->index_buffer = buffer;
1930 device->update_state->index_format = format_id;
1932 if (device->recording)
1933 device->recording->changed.indices = TRUE;
1935 if (prev_buffer == buffer && prev_format == format_id)
1936 return;
1938 if (buffer)
1939 wined3d_buffer_incref(buffer);
1940 if (!device->recording)
1941 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1942 if (prev_buffer)
1943 wined3d_buffer_decref(prev_buffer);
1946 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1947 enum wined3d_format_id *format)
1949 TRACE("device %p, format %p.\n", device, format);
1951 *format = device->state.index_format;
1952 return device->state.index_buffer;
1955 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1957 TRACE("device %p, base_index %d.\n", device, base_index);
1959 device->update_state->base_vertex_index = base_index;
1962 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1964 TRACE("device %p.\n", device);
1966 return device->state.base_vertex_index;
1969 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1971 TRACE("device %p, viewport %p.\n", device, viewport);
1972 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1973 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1975 device->update_state->viewport = *viewport;
1977 /* Handle recording of state blocks */
1978 if (device->recording)
1980 TRACE("Recording... not performing anything\n");
1981 device->recording->changed.viewport = TRUE;
1982 return;
1985 wined3d_cs_emit_set_viewport(device->cs, viewport);
1988 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1990 TRACE("device %p, viewport %p.\n", device, viewport);
1992 *viewport = device->state.viewport;
1995 static void resolve_depth_buffer(struct wined3d_state *state)
1997 struct wined3d_texture *dst_texture = state->textures[0];
1998 struct wined3d_rendertarget_view *src_view;
1999 RECT src_rect, dst_rect;
2001 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2002 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2003 return;
2005 if (!(src_view = state->fb->depth_stencil))
2006 return;
2007 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2009 FIXME("Not supported on buffer resources.\n");
2010 return;
2013 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2014 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2015 wined3d_texture_blt(dst_texture, 0, &dst_rect, wined3d_texture_from_resource(src_view->resource),
2016 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2019 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2020 enum wined3d_render_state state, DWORD value)
2022 DWORD old_value;
2024 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2026 if (state > WINEHIGHEST_RENDER_STATE)
2028 WARN("Unhandled render state %#x.\n", state);
2029 return;
2032 old_value = device->state.render_states[state];
2033 device->update_state->render_states[state] = value;
2035 /* Handle recording of state blocks. */
2036 if (device->recording)
2038 TRACE("Recording... not performing anything.\n");
2039 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2040 return;
2043 /* Compared here and not before the assignment to allow proper stateblock recording. */
2044 if (value == old_value)
2045 TRACE("Application is setting the old value over, nothing to do.\n");
2046 else
2047 wined3d_cs_emit_set_render_state(device->cs, state, value);
2049 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2051 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2052 resolve_depth_buffer(&device->state);
2056 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2058 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2060 return device->state.render_states[state];
2063 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2064 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2066 DWORD old_value;
2068 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2069 device, sampler_idx, debug_d3dsamplerstate(state), value);
2071 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2072 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2074 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2076 WARN("Invalid sampler %u.\n", sampler_idx);
2077 return; /* Windows accepts overflowing this array ... we do not. */
2080 old_value = device->state.sampler_states[sampler_idx][state];
2081 device->update_state->sampler_states[sampler_idx][state] = value;
2083 /* Handle recording of state blocks. */
2084 if (device->recording)
2086 TRACE("Recording... not performing anything.\n");
2087 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2088 return;
2091 if (old_value == value)
2093 TRACE("Application is setting the old value over, nothing to do.\n");
2094 return;
2097 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2100 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2101 UINT sampler_idx, enum wined3d_sampler_state state)
2103 TRACE("device %p, sampler_idx %u, state %s.\n",
2104 device, sampler_idx, debug_d3dsamplerstate(state));
2106 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2107 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2109 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2111 WARN("Invalid sampler %u.\n", sampler_idx);
2112 return 0; /* Windows accepts overflowing this array ... we do not. */
2115 return device->state.sampler_states[sampler_idx][state];
2118 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2120 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2122 if (device->recording)
2123 device->recording->changed.scissorRect = TRUE;
2125 if (EqualRect(&device->update_state->scissor_rect, rect))
2127 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2128 return;
2130 CopyRect(&device->update_state->scissor_rect, rect);
2132 if (device->recording)
2134 TRACE("Recording... not performing anything.\n");
2135 return;
2138 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2141 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2143 TRACE("device %p, rect %p.\n", device, rect);
2145 *rect = device->state.scissor_rect;
2146 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2149 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2150 struct wined3d_vertex_declaration *declaration)
2152 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2154 TRACE("device %p, declaration %p.\n", device, declaration);
2156 if (device->recording)
2157 device->recording->changed.vertexDecl = TRUE;
2159 if (declaration == prev)
2160 return;
2162 if (declaration)
2163 wined3d_vertex_declaration_incref(declaration);
2164 device->update_state->vertex_declaration = declaration;
2165 if (!device->recording)
2166 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2167 if (prev)
2168 wined3d_vertex_declaration_decref(prev);
2171 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2173 TRACE("device %p.\n", device);
2175 return device->state.vertex_declaration;
2178 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2180 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2182 TRACE("device %p, shader %p.\n", device, shader);
2184 if (device->recording)
2185 device->recording->changed.vertexShader = TRUE;
2187 if (shader == prev)
2188 return;
2190 if (shader)
2191 wined3d_shader_incref(shader);
2192 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2193 if (!device->recording)
2194 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2195 if (prev)
2196 wined3d_shader_decref(prev);
2199 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2201 TRACE("device %p.\n", device);
2203 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2206 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2207 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2209 struct wined3d_buffer *prev;
2211 if (idx >= MAX_CONSTANT_BUFFERS)
2213 WARN("Invalid constant buffer index %u.\n", idx);
2214 return;
2217 prev = device->update_state->cb[type][idx];
2218 if (buffer == prev)
2219 return;
2221 if (buffer)
2222 wined3d_buffer_incref(buffer);
2223 device->update_state->cb[type][idx] = buffer;
2224 if (!device->recording)
2225 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2226 if (prev)
2227 wined3d_buffer_decref(prev);
2230 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2232 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2234 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2237 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2239 TRACE("device %p, idx %u.\n", device, idx);
2241 if (idx >= MAX_CONSTANT_BUFFERS)
2243 WARN("Invalid constant buffer index %u.\n", idx);
2244 return NULL;
2247 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2250 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2251 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2253 struct wined3d_shader_resource_view *prev;
2255 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2257 WARN("Invalid view index %u.\n", idx);
2258 return;
2261 prev = device->update_state->shader_resource_view[type][idx];
2262 if (view == prev)
2263 return;
2265 if (view)
2266 wined3d_shader_resource_view_incref(view);
2267 device->update_state->shader_resource_view[type][idx] = view;
2268 if (!device->recording)
2269 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2270 if (prev)
2271 wined3d_shader_resource_view_decref(prev);
2274 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2275 UINT idx, struct wined3d_shader_resource_view *view)
2277 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2279 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2282 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2283 UINT idx)
2285 TRACE("device %p, idx %u.\n", device, idx);
2287 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2289 WARN("Invalid view index %u.\n", idx);
2290 return NULL;
2293 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2296 static void wined3d_device_set_sampler(struct wined3d_device *device,
2297 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2299 struct wined3d_sampler *prev;
2301 if (idx >= MAX_SAMPLER_OBJECTS)
2303 WARN("Invalid sampler index %u.\n", idx);
2304 return;
2307 prev = device->update_state->sampler[type][idx];
2308 if (sampler == prev)
2309 return;
2311 if (sampler)
2312 wined3d_sampler_incref(sampler);
2313 device->update_state->sampler[type][idx] = sampler;
2314 if (!device->recording)
2315 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2316 if (prev)
2317 wined3d_sampler_decref(prev);
2320 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2322 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2324 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2327 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2329 TRACE("device %p, idx %u.\n", device, idx);
2331 if (idx >= MAX_SAMPLER_OBJECTS)
2333 WARN("Invalid sampler index %u.\n", idx);
2334 return NULL;
2337 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2340 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2342 UINT i;
2344 for (i = 0; i < device->context_count; ++i)
2346 device->contexts[i]->constant_update_mask |= mask;
2350 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2351 UINT start_register, const BOOL *constants, UINT bool_count)
2353 UINT count = min(bool_count, MAX_CONST_B - start_register);
2354 UINT i;
2356 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2357 device, start_register, constants, bool_count);
2359 if (!constants || start_register >= MAX_CONST_B)
2360 return WINED3DERR_INVALIDCALL;
2362 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2363 for (i = 0; i < count; ++i)
2364 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2366 if (device->recording)
2368 for (i = start_register; i < count + start_register; ++i)
2369 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2371 else
2373 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2376 return WINED3D_OK;
2379 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2380 UINT start_register, BOOL *constants, UINT bool_count)
2382 UINT count = min(bool_count, MAX_CONST_B - start_register);
2384 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2385 device, start_register, constants, bool_count);
2387 if (!constants || start_register >= MAX_CONST_B)
2388 return WINED3DERR_INVALIDCALL;
2390 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2392 return WINED3D_OK;
2395 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2396 UINT start_register, const int *constants, UINT vector4i_count)
2398 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2399 UINT i;
2401 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2402 device, start_register, constants, vector4i_count);
2404 if (!constants || start_register >= MAX_CONST_I)
2405 return WINED3DERR_INVALIDCALL;
2407 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2408 for (i = 0; i < count; ++i)
2409 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2410 constants[i * 4], constants[i * 4 + 1],
2411 constants[i * 4 + 2], constants[i * 4 + 3]);
2413 if (device->recording)
2415 for (i = start_register; i < count + start_register; ++i)
2416 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2418 else
2420 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2423 return WINED3D_OK;
2426 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2427 UINT start_register, int *constants, UINT vector4i_count)
2429 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2431 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2432 device, start_register, constants, vector4i_count);
2434 if (!constants || start_register >= MAX_CONST_I)
2435 return WINED3DERR_INVALIDCALL;
2437 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2438 return WINED3D_OK;
2441 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2442 UINT start_register, const float *constants, UINT vector4f_count)
2444 UINT i;
2445 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2447 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2448 device, start_register, constants, vector4f_count);
2450 /* Specifically test start_register > limit to catch MAX_UINT overflows
2451 * when adding start_register + vector4f_count. */
2452 if (!constants
2453 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2454 || start_register > d3d_info->limits.vs_uniform_count)
2455 return WINED3DERR_INVALIDCALL;
2457 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2458 constants, vector4f_count * sizeof(float) * 4);
2459 if (TRACE_ON(d3d))
2461 for (i = 0; i < vector4f_count; ++i)
2462 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2463 constants[i * 4], constants[i * 4 + 1],
2464 constants[i * 4 + 2], constants[i * 4 + 3]);
2467 if (device->recording)
2468 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2469 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2470 else
2471 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2474 return WINED3D_OK;
2477 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2478 UINT start_register, float *constants, UINT vector4f_count)
2480 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2481 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2483 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2484 device, start_register, constants, vector4f_count);
2486 if (!constants || count < 0)
2487 return WINED3DERR_INVALIDCALL;
2489 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2491 return WINED3D_OK;
2494 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2496 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2498 TRACE("device %p, shader %p.\n", device, shader);
2500 if (device->recording)
2501 device->recording->changed.pixelShader = TRUE;
2503 if (shader == prev)
2504 return;
2506 if (shader)
2507 wined3d_shader_incref(shader);
2508 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2509 if (!device->recording)
2510 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2511 if (prev)
2512 wined3d_shader_decref(prev);
2515 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2517 TRACE("device %p.\n", device);
2519 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2522 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2524 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2526 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2529 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2531 TRACE("device %p, idx %u.\n", device, idx);
2533 if (idx >= MAX_CONSTANT_BUFFERS)
2535 WARN("Invalid constant buffer index %u.\n", idx);
2536 return NULL;
2539 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2542 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2543 UINT idx, struct wined3d_shader_resource_view *view)
2545 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2547 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2550 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2551 UINT idx)
2553 TRACE("device %p, idx %u.\n", device, idx);
2555 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2557 WARN("Invalid view index %u.\n", idx);
2558 return NULL;
2561 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2564 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2566 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2568 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2571 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2573 TRACE("device %p, idx %u.\n", device, idx);
2575 if (idx >= MAX_SAMPLER_OBJECTS)
2577 WARN("Invalid sampler index %u.\n", idx);
2578 return NULL;
2581 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2584 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2585 UINT start_register, const BOOL *constants, UINT bool_count)
2587 UINT count = min(bool_count, MAX_CONST_B - start_register);
2588 UINT i;
2590 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2591 device, start_register, constants, bool_count);
2593 if (!constants || start_register >= MAX_CONST_B)
2594 return WINED3DERR_INVALIDCALL;
2596 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2597 for (i = 0; i < count; ++i)
2598 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2600 if (device->recording)
2602 for (i = start_register; i < count + start_register; ++i)
2603 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2605 else
2607 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2610 return WINED3D_OK;
2613 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2614 UINT start_register, BOOL *constants, UINT bool_count)
2616 UINT count = min(bool_count, MAX_CONST_B - start_register);
2618 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2619 device, start_register, constants, bool_count);
2621 if (!constants || start_register >= MAX_CONST_B)
2622 return WINED3DERR_INVALIDCALL;
2624 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2626 return WINED3D_OK;
2629 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2630 UINT start_register, const int *constants, UINT vector4i_count)
2632 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2633 UINT i;
2635 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2636 device, start_register, constants, vector4i_count);
2638 if (!constants || start_register >= MAX_CONST_I)
2639 return WINED3DERR_INVALIDCALL;
2641 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2642 for (i = 0; i < count; ++i)
2643 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2644 constants[i * 4], constants[i * 4 + 1],
2645 constants[i * 4 + 2], constants[i * 4 + 3]);
2647 if (device->recording)
2649 for (i = start_register; i < count + start_register; ++i)
2650 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2652 else
2654 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2657 return WINED3D_OK;
2660 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2661 UINT start_register, int *constants, UINT vector4i_count)
2663 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2665 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2666 device, start_register, constants, vector4i_count);
2668 if (!constants || start_register >= MAX_CONST_I)
2669 return WINED3DERR_INVALIDCALL;
2671 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2673 return WINED3D_OK;
2676 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2677 UINT start_register, const float *constants, UINT vector4f_count)
2679 UINT i;
2680 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2682 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2683 device, start_register, constants, vector4f_count);
2685 /* Specifically test start_register > limit to catch MAX_UINT overflows
2686 * when adding start_register + vector4f_count. */
2687 if (!constants
2688 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2689 || start_register > d3d_info->limits.ps_uniform_count)
2690 return WINED3DERR_INVALIDCALL;
2692 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2693 constants, vector4f_count * sizeof(float) * 4);
2694 if (TRACE_ON(d3d))
2696 for (i = 0; i < vector4f_count; ++i)
2697 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2698 constants[i * 4], constants[i * 4 + 1],
2699 constants[i * 4 + 2], constants[i * 4 + 3]);
2702 if (device->recording)
2703 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2704 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2705 else
2706 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2708 return WINED3D_OK;
2711 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2712 UINT start_register, float *constants, UINT vector4f_count)
2714 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2715 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2717 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2718 device, start_register, constants, vector4f_count);
2720 if (!constants || count < 0)
2721 return WINED3DERR_INVALIDCALL;
2723 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2725 return WINED3D_OK;
2728 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2730 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2732 TRACE("device %p, shader %p.\n", device, shader);
2734 if (device->recording || shader == prev)
2735 return;
2736 if (shader)
2737 wined3d_shader_incref(shader);
2738 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2739 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2740 if (prev)
2741 wined3d_shader_decref(prev);
2744 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2746 TRACE("device %p.\n", device);
2748 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2751 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2753 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2755 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2758 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2760 TRACE("device %p, idx %u.\n", device, idx);
2762 if (idx >= MAX_CONSTANT_BUFFERS)
2764 WARN("Invalid constant buffer index %u.\n", idx);
2765 return NULL;
2768 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2771 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2772 UINT idx, struct wined3d_shader_resource_view *view)
2774 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2776 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2779 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2780 UINT idx)
2782 TRACE("device %p, idx %u.\n", device, idx);
2784 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2786 WARN("Invalid view index %u.\n", idx);
2787 return NULL;
2790 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2793 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2795 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2797 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2800 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2802 TRACE("device %p, idx %u.\n", device, idx);
2804 if (idx >= MAX_SAMPLER_OBJECTS)
2806 WARN("Invalid sampler index %u.\n", idx);
2807 return NULL;
2810 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2813 /* Context activation is done by the caller. */
2814 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2815 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2816 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2817 DWORD DestFVF)
2819 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2820 struct wined3d_viewport vp;
2821 UINT vertex_size;
2822 unsigned int i;
2823 BYTE *dest_ptr;
2824 BOOL doClip;
2825 DWORD numTextures;
2826 HRESULT hr;
2828 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2830 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2833 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2835 ERR("Source has no position mask\n");
2836 return WINED3DERR_INVALIDCALL;
2839 if (device->state.render_states[WINED3D_RS_CLIPPING])
2841 static BOOL warned = FALSE;
2843 * The clipping code is not quite correct. Some things need
2844 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2845 * so disable clipping for now.
2846 * (The graphics in Half-Life are broken, and my processvertices
2847 * test crashes with IDirect3DDevice3)
2848 doClip = TRUE;
2850 doClip = FALSE;
2851 if(!warned) {
2852 warned = TRUE;
2853 FIXME("Clipping is broken and disabled for now\n");
2856 else
2857 doClip = FALSE;
2859 vertex_size = get_flexible_vertex_size(DestFVF);
2860 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2862 WARN("Failed to map buffer, hr %#x.\n", hr);
2863 return hr;
2866 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2867 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2868 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2870 TRACE("View mat:\n");
2871 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2872 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2873 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2874 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2876 TRACE("Proj mat:\n");
2877 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2878 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2879 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2880 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2882 TRACE("World mat:\n");
2883 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2884 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2885 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2886 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2888 /* Get the viewport */
2889 wined3d_device_get_viewport(device, &vp);
2890 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2891 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2893 multiply_matrix(&mat,&view_mat,&world_mat);
2894 multiply_matrix(&mat,&proj_mat,&mat);
2896 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2898 for (i = 0; i < dwCount; i+= 1) {
2899 unsigned int tex_index;
2901 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2902 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2903 /* The position first */
2904 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2905 const float *p = (const float *)(element->data.addr + i * element->stride);
2906 float x, y, z, rhw;
2907 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2909 /* Multiplication with world, view and projection matrix. */
2910 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2911 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2912 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2913 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2915 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2917 /* WARNING: The following things are taken from d3d7 and were not yet checked
2918 * against d3d8 or d3d9!
2921 /* Clipping conditions: From msdn
2923 * A vertex is clipped if it does not match the following requirements
2924 * -rhw < x <= rhw
2925 * -rhw < y <= rhw
2926 * 0 < z <= rhw
2927 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2929 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2930 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2934 if( !doClip ||
2935 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2936 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2937 ( rhw > eps ) ) ) {
2939 /* "Normal" viewport transformation (not clipped)
2940 * 1) The values are divided by rhw
2941 * 2) The y axis is negative, so multiply it with -1
2942 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2943 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2944 * 4) Multiply x with Width/2 and add Width/2
2945 * 5) The same for the height
2946 * 6) Add the viewpoint X and Y to the 2D coordinates and
2947 * The minimum Z value to z
2948 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2950 * Well, basically it's simply a linear transformation into viewport
2951 * coordinates
2954 x /= rhw;
2955 y /= rhw;
2956 z /= rhw;
2958 y *= -1;
2960 x *= vp.width / 2;
2961 y *= vp.height / 2;
2962 z *= vp.max_z - vp.min_z;
2964 x += vp.width / 2 + vp.x;
2965 y += vp.height / 2 + vp.y;
2966 z += vp.min_z;
2968 rhw = 1 / rhw;
2969 } else {
2970 /* That vertex got clipped
2971 * Contrary to OpenGL it is not dropped completely, it just
2972 * undergoes a different calculation.
2974 TRACE("Vertex got clipped\n");
2975 x += rhw;
2976 y += rhw;
2978 x /= 2;
2979 y /= 2;
2981 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2982 * outside of the main vertex buffer memory. That needs some more
2983 * investigation...
2987 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2990 ( (float *) dest_ptr)[0] = x;
2991 ( (float *) dest_ptr)[1] = y;
2992 ( (float *) dest_ptr)[2] = z;
2993 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2995 dest_ptr += 3 * sizeof(float);
2997 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2998 dest_ptr += sizeof(float);
3001 if (DestFVF & WINED3DFVF_PSIZE)
3002 dest_ptr += sizeof(DWORD);
3004 if (DestFVF & WINED3DFVF_NORMAL)
3006 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3007 const float *normal = (const float *)(element->data.addr + i * element->stride);
3008 /* AFAIK this should go into the lighting information */
3009 FIXME("Didn't expect the destination to have a normal\n");
3010 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3013 if (DestFVF & WINED3DFVF_DIFFUSE)
3015 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3016 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3017 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3019 static BOOL warned = FALSE;
3021 if(!warned) {
3022 ERR("No diffuse color in source, but destination has one\n");
3023 warned = TRUE;
3026 *( (DWORD *) dest_ptr) = 0xffffffff;
3027 dest_ptr += sizeof(DWORD);
3029 else
3031 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3035 if (DestFVF & WINED3DFVF_SPECULAR)
3037 /* What's the color value in the feedback buffer? */
3038 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3039 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3040 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3042 static BOOL warned = FALSE;
3044 if(!warned) {
3045 ERR("No specular color in source, but destination has one\n");
3046 warned = TRUE;
3049 *(DWORD *)dest_ptr = 0xff000000;
3050 dest_ptr += sizeof(DWORD);
3052 else
3054 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3058 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3060 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3061 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3062 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3064 ERR("No source texture, but destination requests one\n");
3065 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3067 else
3069 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3074 wined3d_buffer_unmap(dest);
3076 return WINED3D_OK;
3078 #undef copy_and_next
3080 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3081 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3082 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3084 struct wined3d_state *state = &device->state;
3085 struct wined3d_stream_info stream_info;
3086 const struct wined3d_gl_info *gl_info;
3087 struct wined3d_context *context;
3088 struct wined3d_shader *vs;
3089 unsigned int i;
3090 HRESULT hr;
3091 WORD map;
3093 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3094 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3095 device, src_start_idx, dst_idx, vertex_count,
3096 dst_buffer, declaration, flags, dst_fvf);
3098 if (declaration)
3099 FIXME("Output vertex declaration not implemented yet.\n");
3101 /* Need any context to write to the vbo. */
3102 context = context_acquire(device, NULL);
3103 gl_info = context->gl_info;
3105 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3106 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3107 context_stream_info_from_declaration(context, state, &stream_info);
3108 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3110 /* We can't convert FROM a VBO, and vertex buffers used to source into
3111 * process_vertices() are unlikely to ever be used for drawing. Release
3112 * VBOs in those buffers and fix up the stream_info structure.
3114 * Also apply the start index. */
3115 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3117 struct wined3d_stream_info_element *e;
3118 struct wined3d_buffer *buffer;
3120 if (!(map & 1))
3121 continue;
3123 e = &stream_info.elements[i];
3124 buffer = state->streams[e->stream_idx].buffer;
3125 e->data.buffer_object = 0;
3126 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3127 if (buffer->buffer_object)
3129 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3130 buffer->buffer_object = 0;
3132 if (e->data.addr)
3133 e->data.addr += e->stride * src_start_idx;
3136 hr = process_vertices_strided(device, dst_idx, vertex_count,
3137 &stream_info, dst_buffer, flags, dst_fvf);
3139 context_release(context);
3141 return hr;
3144 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3145 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3147 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3148 DWORD old_value;
3150 TRACE("device %p, stage %u, state %s, value %#x.\n",
3151 device, stage, debug_d3dtexturestate(state), value);
3153 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3155 WARN("Invalid state %#x passed.\n", state);
3156 return;
3159 if (stage >= d3d_info->limits.ffp_blend_stages)
3161 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3162 stage, d3d_info->limits.ffp_blend_stages - 1);
3163 return;
3166 old_value = device->update_state->texture_states[stage][state];
3167 device->update_state->texture_states[stage][state] = value;
3169 if (device->recording)
3171 TRACE("Recording... not performing anything.\n");
3172 device->recording->changed.textureState[stage] |= 1u << state;
3173 return;
3176 /* Checked after the assignments to allow proper stateblock recording. */
3177 if (old_value == value)
3179 TRACE("Application is setting the old value over, nothing to do.\n");
3180 return;
3183 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3186 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3187 UINT stage, enum wined3d_texture_stage_state state)
3189 TRACE("device %p, stage %u, state %s.\n",
3190 device, stage, debug_d3dtexturestate(state));
3192 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3194 WARN("Invalid state %#x passed.\n", state);
3195 return 0;
3198 return device->state.texture_states[stage][state];
3201 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3202 UINT stage, struct wined3d_texture *texture)
3204 struct wined3d_texture *prev;
3206 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3208 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3209 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3211 /* Windows accepts overflowing this array... we do not. */
3212 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3214 WARN("Ignoring invalid stage %u.\n", stage);
3215 return WINED3D_OK;
3218 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3220 WARN("Rejecting attempt to set scratch texture.\n");
3221 return WINED3DERR_INVALIDCALL;
3224 if (device->recording)
3225 device->recording->changed.textures |= 1u << stage;
3227 prev = device->update_state->textures[stage];
3228 TRACE("Previous texture %p.\n", prev);
3230 if (texture == prev)
3232 TRACE("App is setting the same texture again, nothing to do.\n");
3233 return WINED3D_OK;
3236 TRACE("Setting new texture to %p.\n", texture);
3237 device->update_state->textures[stage] = texture;
3239 if (texture)
3240 wined3d_texture_incref(texture);
3241 if (!device->recording)
3242 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3243 if (prev)
3244 wined3d_texture_decref(prev);
3246 return WINED3D_OK;
3249 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3251 TRACE("device %p, stage %u.\n", device, stage);
3253 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3254 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3256 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3258 WARN("Ignoring invalid stage %u.\n", stage);
3259 return NULL; /* Windows accepts overflowing this array ... we do not. */
3262 return device->state.textures[stage];
3265 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3267 TRACE("device %p, caps %p.\n", device, caps);
3269 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3270 device->create_parms.device_type, caps);
3273 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3274 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3276 struct wined3d_swapchain *swapchain;
3278 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3279 device, swapchain_idx, mode, rotation);
3281 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3282 return WINED3DERR_INVALIDCALL;
3284 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3287 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3289 struct wined3d_stateblock *stateblock;
3290 HRESULT hr;
3292 TRACE("device %p.\n", device);
3294 if (device->recording)
3295 return WINED3DERR_INVALIDCALL;
3297 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3298 if (FAILED(hr))
3299 return hr;
3301 device->recording = stateblock;
3302 device->update_state = &stateblock->state;
3304 TRACE("Recording stateblock %p.\n", stateblock);
3306 return WINED3D_OK;
3309 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3310 struct wined3d_stateblock **stateblock)
3312 struct wined3d_stateblock *object = device->recording;
3314 TRACE("device %p, stateblock %p.\n", device, stateblock);
3316 if (!device->recording)
3318 WARN("Not recording.\n");
3319 *stateblock = NULL;
3320 return WINED3DERR_INVALIDCALL;
3323 stateblock_init_contained_states(object);
3325 *stateblock = object;
3326 device->recording = NULL;
3327 device->update_state = &device->state;
3329 TRACE("Returning stateblock %p.\n", *stateblock);
3331 return WINED3D_OK;
3334 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3336 /* At the moment we have no need for any functionality at the beginning
3337 * of a scene. */
3338 TRACE("device %p.\n", device);
3340 if (device->inScene)
3342 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3343 return WINED3DERR_INVALIDCALL;
3345 device->inScene = TRUE;
3346 return WINED3D_OK;
3349 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3351 struct wined3d_context *context;
3353 TRACE("device %p.\n", device);
3355 if (!device->inScene)
3357 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3358 return WINED3DERR_INVALIDCALL;
3361 context = context_acquire(device, NULL);
3362 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3363 context->gl_info->gl_ops.gl.p_glFlush();
3364 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3365 * fails. */
3366 context_release(context);
3368 device->inScene = FALSE;
3369 return WINED3D_OK;
3372 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3373 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3375 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3376 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3378 if (!rect_count && rects)
3380 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3381 return WINED3D_OK;
3384 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3386 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3387 if (!ds)
3389 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3390 /* TODO: What about depth stencil buffers without stencil bits? */
3391 return WINED3DERR_INVALIDCALL;
3393 else if (flags & WINED3DCLEAR_TARGET)
3395 if (ds->width < device->fb.render_targets[0]->width
3396 || ds->height < device->fb.render_targets[0]->height)
3398 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3399 return WINED3D_OK;
3404 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3406 return WINED3D_OK;
3409 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3410 struct wined3d_query *predicate, BOOL value)
3412 struct wined3d_query *prev;
3414 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3416 prev = device->update_state->predicate;
3417 if (predicate)
3419 FIXME("Predicated rendering not implemented.\n");
3420 wined3d_query_incref(predicate);
3422 device->update_state->predicate = predicate;
3423 device->update_state->predicate_value = value;
3424 if (!device->recording)
3425 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3426 if (prev)
3427 wined3d_query_decref(prev);
3430 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3432 TRACE("device %p, value %p.\n", device, value);
3434 *value = device->state.predicate_value;
3435 return device->state.predicate;
3438 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3439 enum wined3d_primitive_type primitive_type)
3441 GLenum gl_primitive_type, prev;
3443 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3445 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3446 prev = device->update_state->gl_primitive_type;
3447 device->update_state->gl_primitive_type = gl_primitive_type;
3448 if (device->recording)
3449 device->recording->changed.primitive_type = TRUE;
3450 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3451 device_invalidate_state(device, STATE_POINT_ENABLE);
3454 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3455 enum wined3d_primitive_type *primitive_type)
3457 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3459 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3461 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3464 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3466 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3468 if (!device->state.vertex_declaration)
3470 WARN("Called without a valid vertex declaration set.\n");
3471 return WINED3DERR_INVALIDCALL;
3474 if (device->state.load_base_vertex_index)
3476 device->state.load_base_vertex_index = 0;
3477 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3480 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3482 return WINED3D_OK;
3485 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3486 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3488 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3489 device, start_vertex, vertex_count, start_instance, instance_count);
3491 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3494 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3496 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3498 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3500 if (!device->state.index_buffer)
3502 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3503 * without an index buffer set. (The first time at least...)
3504 * D3D8 simply dies, but I doubt it can do much harm to return
3505 * D3DERR_INVALIDCALL there as well. */
3506 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3507 return WINED3DERR_INVALIDCALL;
3510 if (!device->state.vertex_declaration)
3512 WARN("Called without a valid vertex declaration set.\n");
3513 return WINED3DERR_INVALIDCALL;
3516 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3517 device->state.load_base_vertex_index != device->state.base_vertex_index)
3519 device->state.load_base_vertex_index = device->state.base_vertex_index;
3520 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3523 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3525 return WINED3D_OK;
3528 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3529 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3531 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3532 device, start_idx, index_count, start_instance, instance_count);
3534 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3537 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3538 struct wined3d_texture *src_texture, unsigned int src_level,
3539 struct wined3d_texture *dst_texture, unsigned int level_count)
3541 struct wined3d_const_bo_address data;
3542 struct wined3d_context *context;
3543 struct wined3d_map_desc src;
3544 HRESULT hr = WINED3D_OK;
3545 unsigned int i;
3547 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3548 device, src_texture, src_level, dst_texture, level_count);
3550 if (src_texture->resource.format != dst_texture->resource.format)
3552 WARN("Source and destination formats do not match.\n");
3553 return WINED3DERR_INVALIDCALL;
3556 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3557 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3558 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3560 WARN("Source and destination dimensions do not match.\n");
3561 return WINED3DERR_INVALIDCALL;
3564 context = context_acquire(device, NULL);
3566 /* Only a prepare, since we're uploading entire volumes. */
3567 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3568 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3570 for (i = 0; i < level_count; ++i)
3572 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3573 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3574 goto done;
3576 data.buffer_object = 0;
3577 data.addr = src.data;
3578 wined3d_volume_upload_data(dst_texture->sub_resources[i].u.volume, context, &data);
3579 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3581 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3582 goto done;
3585 done:
3586 context_release(context);
3587 return hr;
3590 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3591 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3593 unsigned int src_size, dst_size, src_skip_levels = 0;
3594 unsigned int layer_count, level_count, i, j;
3595 enum wined3d_resource_type type;
3596 HRESULT hr;
3597 struct wined3d_context *context;
3599 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3601 /* Verify that the source and destination textures are non-NULL. */
3602 if (!src_texture || !dst_texture)
3604 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3605 return WINED3DERR_INVALIDCALL;
3608 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3610 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3611 return WINED3DERR_INVALIDCALL;
3613 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3615 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3616 return WINED3DERR_INVALIDCALL;
3619 /* Verify that the source and destination textures are the same type. */
3620 type = src_texture->resource.type;
3621 if (dst_texture->resource.type != type)
3623 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3624 return WINED3DERR_INVALIDCALL;
3627 layer_count = src_texture->layer_count;
3628 if (layer_count != dst_texture->layer_count)
3630 WARN("Source and destination have different layer counts.\n");
3631 return WINED3DERR_INVALIDCALL;
3634 level_count = min(wined3d_texture_get_level_count(src_texture),
3635 wined3d_texture_get_level_count(dst_texture));
3637 src_size = max(src_texture->resource.width, src_texture->resource.height);
3638 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3639 if (type == WINED3D_RTYPE_VOLUME)
3641 src_size = max(src_size, src_texture->resource.depth);
3642 dst_size = max(dst_size, dst_texture->resource.depth);
3644 while (src_size > dst_size)
3646 src_size >>= 1;
3647 ++src_skip_levels;
3650 /* Make sure that the destination texture is loaded. */
3651 context = context_acquire(device, NULL);
3652 wined3d_texture_load(dst_texture, context, FALSE);
3653 context_release(context);
3655 /* Update every surface level of the texture. */
3656 switch (type)
3658 case WINED3D_RTYPE_TEXTURE_2D:
3660 unsigned int src_levels = src_texture->level_count;
3661 unsigned int dst_levels = dst_texture->level_count;
3662 struct wined3d_surface *src_surface;
3663 struct wined3d_surface *dst_surface;
3665 for (i = 0; i < layer_count; ++i)
3667 for (j = 0; j < level_count; ++j)
3669 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3670 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3671 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3673 WARN("Failed to update surface, hr %#x.\n", hr);
3674 return hr;
3678 return WINED3D_OK;
3681 case WINED3D_RTYPE_TEXTURE_3D:
3682 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3683 src_texture, src_skip_levels, dst_texture, level_count)))
3684 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3685 return hr;
3687 default:
3688 FIXME("Unsupported texture type %#x.\n", type);
3689 return WINED3DERR_INVALIDCALL;
3693 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3695 const struct wined3d_state *state = &device->state;
3696 struct wined3d_texture *texture;
3697 DWORD i;
3699 TRACE("device %p, num_passes %p.\n", device, num_passes);
3701 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3703 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3705 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3706 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3708 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3710 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3711 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3714 texture = state->textures[i];
3715 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3717 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3719 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3720 return E_FAIL;
3722 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3724 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3725 return E_FAIL;
3727 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3728 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3730 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3731 return E_FAIL;
3735 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3736 || state->render_states[WINED3D_RS_STENCILENABLE])
3738 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3739 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3741 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3743 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3744 return WINED3DERR_CONFLICTINGRENDERSTATE;
3748 /* return a sensible default */
3749 *num_passes = 1;
3751 TRACE("returning D3D_OK\n");
3752 return WINED3D_OK;
3755 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3757 static BOOL warned;
3759 TRACE("device %p, software %#x.\n", device, software);
3761 if (!warned)
3763 FIXME("device %p, software %#x stub!\n", device, software);
3764 warned = TRUE;
3767 device->softwareVertexProcessing = software;
3770 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3772 static BOOL warned;
3774 TRACE("device %p.\n", device);
3776 if (!warned)
3778 TRACE("device %p stub!\n", device);
3779 warned = TRUE;
3782 return device->softwareVertexProcessing;
3785 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3786 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3788 struct wined3d_swapchain *swapchain;
3790 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3791 device, swapchain_idx, raster_status);
3793 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3794 return WINED3DERR_INVALIDCALL;
3796 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3799 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3801 static BOOL warned;
3803 TRACE("device %p, segments %.8e.\n", device, segments);
3805 if (segments != 0.0f)
3807 if (!warned)
3809 FIXME("device %p, segments %.8e stub!\n", device, segments);
3810 warned = TRUE;
3814 return WINED3D_OK;
3817 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3819 static BOOL warned;
3821 TRACE("device %p.\n", device);
3823 if (!warned)
3825 FIXME("device %p stub!\n", device);
3826 warned = TRUE;
3829 return 0.0f;
3832 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3833 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3835 struct wined3d_texture *dst_texture, *src_texture;
3836 RECT dst_rect, src_rect;
3837 unsigned int i, j;
3838 HRESULT hr;
3840 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3842 if (src_resource == dst_resource)
3844 WARN("Source and destination are the same resource.\n");
3845 return;
3848 if (src_resource->type != dst_resource->type)
3850 WARN("Resource types (%s / %s) don't match.\n",
3851 debug_d3dresourcetype(dst_resource->type),
3852 debug_d3dresourcetype(src_resource->type));
3853 return;
3856 if (src_resource->width != dst_resource->width
3857 || src_resource->height != dst_resource->height
3858 || src_resource->depth != dst_resource->depth)
3860 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3861 dst_resource->width, dst_resource->height, dst_resource->depth,
3862 src_resource->width, src_resource->height, src_resource->depth);
3863 return;
3866 if (src_resource->format->id != dst_resource->format->id)
3868 WARN("Resource formats (%s / %s) don't match.\n",
3869 debug_d3dformat(dst_resource->format->id),
3870 debug_d3dformat(src_resource->format->id));
3871 return;
3874 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3876 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3877 buffer_from_resource(src_resource), 0,
3878 dst_resource->size)))
3879 ERR("Failed to copy buffer, hr %#x.\n", hr);
3880 return;
3883 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3885 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3886 return;
3889 dst_texture = wined3d_texture_from_resource(dst_resource);
3890 src_texture = wined3d_texture_from_resource(src_resource);
3892 if (src_texture->layer_count != dst_texture->layer_count
3893 || src_texture->level_count != dst_texture->level_count)
3895 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3896 dst_texture->layer_count, dst_texture->level_count,
3897 src_texture->layer_count, src_texture->level_count);
3898 return;
3901 for (i = 0; i < dst_texture->level_count; ++i)
3903 SetRect(&dst_rect, 0, 0,
3904 wined3d_texture_get_level_width(dst_texture, i),
3905 wined3d_texture_get_level_height(dst_texture, i));
3906 SetRect(&src_rect, 0, 0,
3907 wined3d_texture_get_level_width(src_texture, i),
3908 wined3d_texture_get_level_height(dst_texture, i));
3909 for (j = 0; j < dst_texture->layer_count; ++j)
3911 unsigned int idx = j * dst_texture->level_count + i;
3913 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3914 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3915 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3920 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3921 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3922 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3923 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3925 struct wined3d_texture *dst_texture, *src_texture;
3926 RECT dst_rect, src_rect;
3927 HRESULT hr;
3929 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3930 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3931 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3932 src_resource, src_sub_resource_idx, debug_box(src_box));
3934 if (src_box && (src_box->left >= src_box->right
3935 || src_box->top >= src_box->bottom
3936 || src_box->front >= src_box->back))
3938 WARN("Invalid box %s specified.\n", debug_box(src_box));
3939 return WINED3DERR_INVALIDCALL;
3942 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3944 WARN("Source and destination are the same sub-resource.\n");
3945 return WINED3DERR_INVALIDCALL;
3948 if (src_resource->type != dst_resource->type)
3950 WARN("Resource types (%s / %s) don't match.\n",
3951 debug_d3dresourcetype(dst_resource->type),
3952 debug_d3dresourcetype(src_resource->type));
3953 return WINED3DERR_INVALIDCALL;
3956 if (src_resource->format->id != dst_resource->format->id)
3958 WARN("Resource formats (%s / %s) don't match.\n",
3959 debug_d3dformat(dst_resource->format->id),
3960 debug_d3dformat(src_resource->format->id));
3961 return WINED3DERR_INVALIDCALL;
3964 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3966 unsigned int src_offset, size;
3968 if (dst_sub_resource_idx)
3970 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3971 return WINED3DERR_INVALIDCALL;
3973 if (src_sub_resource_idx)
3975 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3976 return WINED3DERR_INVALIDCALL;
3979 if (src_box)
3981 src_offset = src_box->left;
3982 size = src_box->right - src_box->left;
3984 else
3986 src_offset = 0;
3987 size = src_resource->size;
3990 if (src_offset > src_resource->size
3991 || size > src_resource->size - src_offset
3992 || dst_x > dst_resource->size
3993 || size > dst_resource->size - dst_x)
3995 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
3996 dst_x, src_offset, size);
3997 return WINED3DERR_INVALIDCALL;
4000 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4001 buffer_from_resource(src_resource), src_offset, size);
4004 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4006 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4007 return WINED3DERR_INVALIDCALL;
4010 dst_texture = wined3d_texture_from_resource(dst_resource);
4011 src_texture = wined3d_texture_from_resource(src_resource);
4013 if (src_box)
4015 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4017 else
4019 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4021 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4022 wined3d_texture_get_level_height(src_texture, level));
4025 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4026 dst_y + (src_rect.bottom - src_rect.top));
4028 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4029 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4030 WARN("Failed to blit, hr %#x.\n", hr);
4032 return hr;
4035 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4036 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4037 unsigned int depth_pitch)
4039 struct wined3d_texture_sub_resource *sub_resource;
4040 const struct wined3d_gl_info *gl_info;
4041 struct wined3d_const_bo_address addr;
4042 unsigned int width, height, level;
4043 struct wined3d_context *context;
4044 struct wined3d_texture *texture;
4045 struct wined3d_surface *surface;
4046 POINT dst_point;
4047 RECT src_rect;
4049 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4050 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4052 if (resource->type == WINED3D_RTYPE_BUFFER)
4054 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4055 HRESULT hr;
4057 if (sub_resource_idx > 0)
4059 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4060 return;
4063 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4064 WARN("Failed to update buffer data, hr %#x.\n", hr);
4066 return;
4069 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4071 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4072 return;
4075 texture = wined3d_texture_from_resource(resource);
4076 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4078 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4079 return;
4081 surface = sub_resource->u.surface;
4083 level = sub_resource_idx % texture->level_count;
4084 width = wined3d_texture_get_level_width(texture, level);
4085 height = wined3d_texture_get_level_height(texture, level);
4087 src_rect.left = 0;
4088 src_rect.top = 0;
4089 if (box)
4091 if (box->left >= box->right || box->right > width
4092 || box->top >= box->bottom || box->bottom > height
4093 || box->front >= box->back)
4095 WARN("Invalid box %s specified.\n", debug_box(box));
4096 return;
4099 src_rect.right = box->right - box->left;
4100 src_rect.bottom = box->bottom - box->top;
4101 dst_point.x = box->left;
4102 dst_point.y = box->top;
4104 else
4106 src_rect.right = width;
4107 src_rect.bottom = height;
4108 dst_point.x = 0;
4109 dst_point.y = 0;
4112 addr.buffer_object = 0;
4113 addr.addr = data;
4115 context = context_acquire(resource->device, NULL);
4116 gl_info = context->gl_info;
4118 /* Only load the surface for partial updates. */
4119 if (!dst_point.x && !dst_point.y && src_rect.right == width && src_rect.bottom == height)
4120 wined3d_texture_prepare_texture(texture, context, FALSE);
4121 else
4122 surface_load_location(surface, context, WINED3D_LOCATION_TEXTURE_RGB);
4123 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4125 wined3d_surface_upload_data(surface, gl_info, resource->format,
4126 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4128 context_release(context);
4130 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4131 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4134 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4135 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4136 const struct wined3d_color *color, float depth, DWORD stencil)
4138 const struct blit_shader *blitter;
4139 struct wined3d_resource *resource;
4140 enum wined3d_blit_op blit_op;
4141 RECT r;
4143 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4144 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4146 if (!flags)
4147 return WINED3D_OK;
4149 resource = view->resource;
4150 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4152 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4153 return WINED3DERR_INVALIDCALL;
4156 if (view->depth > 1)
4158 FIXME("Layered clears not implemented.\n");
4159 return WINED3DERR_INVALIDCALL;
4162 if (!rect)
4164 SetRect(&r, 0, 0, view->width, view->height);
4165 rect = &r;
4168 if (flags & WINED3DCLEAR_TARGET)
4169 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4170 else
4171 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4173 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4174 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4176 FIXME("No blitter is capable of performing the requested fill operation.\n");
4177 return WINED3DERR_INVALIDCALL;
4180 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4181 return blitter->color_fill(device, view, rect, color);
4182 else
4183 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4186 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4187 unsigned int view_idx)
4189 TRACE("device %p, view_idx %u.\n", device, view_idx);
4191 if (view_idx >= device->adapter->gl_info.limits.buffers)
4193 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4194 return NULL;
4197 return device->fb.render_targets[view_idx];
4200 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4202 TRACE("device %p.\n", device);
4204 return device->fb.depth_stencil;
4207 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4208 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4210 struct wined3d_rendertarget_view *prev;
4212 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4213 device, view_idx, view, set_viewport);
4215 if (view_idx >= device->adapter->gl_info.limits.buffers)
4217 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4218 return WINED3DERR_INVALIDCALL;
4221 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4223 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4224 return WINED3DERR_INVALIDCALL;
4227 /* Set the viewport and scissor rectangles, if requested. Tests show that
4228 * stateblock recording is ignored, the change goes directly into the
4229 * primary stateblock. */
4230 if (!view_idx && set_viewport)
4232 struct wined3d_state *state = &device->state;
4234 state->viewport.x = 0;
4235 state->viewport.y = 0;
4236 state->viewport.width = view->width;
4237 state->viewport.height = view->height;
4238 state->viewport.min_z = 0.0f;
4239 state->viewport.max_z = 1.0f;
4240 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4242 state->scissor_rect.top = 0;
4243 state->scissor_rect.left = 0;
4244 state->scissor_rect.right = view->width;
4245 state->scissor_rect.bottom = view->height;
4246 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4250 prev = device->fb.render_targets[view_idx];
4251 if (view == prev)
4252 return WINED3D_OK;
4254 if (view)
4255 wined3d_rendertarget_view_incref(view);
4256 device->fb.render_targets[view_idx] = view;
4257 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4258 /* Release after the assignment, to prevent device_resource_released()
4259 * from seeing the surface as still in use. */
4260 if (prev)
4261 wined3d_rendertarget_view_decref(prev);
4263 return WINED3D_OK;
4266 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4268 struct wined3d_rendertarget_view *prev;
4270 TRACE("device %p, view %p.\n", device, view);
4272 prev = device->fb.depth_stencil;
4273 if (prev == view)
4275 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4276 return;
4279 if ((device->fb.depth_stencil = view))
4280 wined3d_rendertarget_view_incref(view);
4281 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4282 if (prev)
4283 wined3d_rendertarget_view_decref(prev);
4286 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4287 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4289 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4290 struct wined3d_sub_resource_data data;
4291 struct wined3d_resource_desc desc;
4292 struct wined3d_map_desc map_desc;
4293 struct wined3d_texture *texture;
4294 HRESULT hr;
4296 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4298 ERR("Failed to map source texture.\n");
4299 return NULL;
4302 data.data = map_desc.data;
4303 data.row_pitch = map_desc.row_pitch;
4304 data.slice_pitch = map_desc.slice_pitch;
4306 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4307 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4308 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4309 desc.multisample_quality = 0;
4310 desc.usage = WINED3DUSAGE_DYNAMIC;
4311 desc.pool = WINED3D_POOL_DEFAULT;
4312 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4313 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4314 desc.depth = 1;
4315 desc.size = 0;
4317 hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4318 &data, NULL, &wined3d_null_parent_ops, &texture);
4319 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4320 if (FAILED(hr))
4322 ERR("Failed to create cursor texture.\n");
4323 return NULL;
4326 return texture;
4329 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4330 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4332 unsigned int texture_level = sub_resource_idx % texture->level_count;
4333 unsigned int cursor_width, cursor_height;
4334 struct wined3d_display_mode mode;
4335 struct wined3d_map_desc map_desc;
4336 HRESULT hr;
4338 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4339 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4341 if (sub_resource_idx >= texture->level_count * texture->layer_count
4342 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4343 return WINED3DERR_INVALIDCALL;
4345 if (device->cursor_texture)
4347 wined3d_texture_decref(device->cursor_texture);
4348 device->cursor_texture = NULL;
4351 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4353 WARN("Texture %p has invalid format %s.\n",
4354 texture, debug_d3dformat(texture->resource.format->id));
4355 return WINED3DERR_INVALIDCALL;
4358 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4360 ERR("Failed to get display mode, hr %#x.\n", hr);
4361 return WINED3DERR_INVALIDCALL;
4364 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4365 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4366 if (cursor_width > mode.width || cursor_height > mode.height)
4368 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4369 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4370 return WINED3DERR_INVALIDCALL;
4373 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4375 /* Do not store the surface's pointer because the application may
4376 * release it after setting the cursor image. Windows doesn't
4377 * addref the set surface, so we can't do this either without
4378 * creating circular refcount dependencies. */
4379 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4381 ERR("Failed to create cursor texture.\n");
4382 return WINED3DERR_INVALIDCALL;
4385 if (cursor_width == 32 && cursor_height == 32)
4387 UINT mask_size = cursor_width * cursor_height / 8;
4388 ICONINFO cursor_info;
4389 DWORD *mask_bits;
4390 HCURSOR cursor;
4392 /* 32-bit user32 cursors ignore the alpha channel if it's all
4393 * zeroes, and use the mask instead. Fill the mask with all ones
4394 * to ensure we still get a fully transparent cursor. */
4395 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4396 return E_OUTOFMEMORY;
4397 memset(mask_bits, 0xff, mask_size);
4399 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4400 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4401 cursor_info.fIcon = FALSE;
4402 cursor_info.xHotspot = x_hotspot;
4403 cursor_info.yHotspot = y_hotspot;
4404 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4405 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4406 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4408 /* Create our cursor and clean up. */
4409 cursor = CreateIconIndirect(&cursor_info);
4410 if (cursor_info.hbmMask)
4411 DeleteObject(cursor_info.hbmMask);
4412 if (cursor_info.hbmColor)
4413 DeleteObject(cursor_info.hbmColor);
4414 if (device->hardwareCursor)
4415 DestroyCursor(device->hardwareCursor);
4416 device->hardwareCursor = cursor;
4417 if (device->bCursorVisible)
4418 SetCursor(cursor);
4420 HeapFree(GetProcessHeap(), 0, mask_bits);
4423 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4424 device->cursorWidth = cursor_width;
4425 device->cursorHeight = cursor_height;
4426 device->xHotSpot = x_hotspot;
4427 device->yHotSpot = y_hotspot;
4429 return WINED3D_OK;
4432 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4433 int x_screen_space, int y_screen_space, DWORD flags)
4435 TRACE("device %p, x %d, y %d, flags %#x.\n",
4436 device, x_screen_space, y_screen_space, flags);
4438 device->xScreenSpace = x_screen_space;
4439 device->yScreenSpace = y_screen_space;
4441 if (device->hardwareCursor)
4443 POINT pt;
4445 GetCursorPos( &pt );
4446 if (x_screen_space == pt.x && y_screen_space == pt.y)
4447 return;
4448 SetCursorPos( x_screen_space, y_screen_space );
4450 /* Switch to the software cursor if position diverges from the hardware one. */
4451 GetCursorPos( &pt );
4452 if (x_screen_space != pt.x || y_screen_space != pt.y)
4454 if (device->bCursorVisible) SetCursor( NULL );
4455 DestroyCursor( device->hardwareCursor );
4456 device->hardwareCursor = 0;
4461 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4463 BOOL oldVisible = device->bCursorVisible;
4465 TRACE("device %p, show %#x.\n", device, show);
4468 * When ShowCursor is first called it should make the cursor appear at the OS's last
4469 * known cursor position.
4471 if (show && !oldVisible)
4473 POINT pt;
4474 GetCursorPos(&pt);
4475 device->xScreenSpace = pt.x;
4476 device->yScreenSpace = pt.y;
4479 if (device->hardwareCursor)
4481 device->bCursorVisible = show;
4482 if (show)
4483 SetCursor(device->hardwareCursor);
4484 else
4485 SetCursor(NULL);
4487 else if (device->cursor_texture)
4489 device->bCursorVisible = show;
4492 return oldVisible;
4495 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4497 struct wined3d_resource *resource, *cursor;
4499 TRACE("device %p.\n", device);
4501 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4503 TRACE("Checking resource %p for eviction.\n", resource);
4505 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4507 TRACE("Evicting %p.\n", resource);
4508 resource->resource_ops->resource_unload(resource);
4512 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4513 device_invalidate_state(device, STATE_STREAMSRC);
4516 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4518 struct wined3d_resource *resource, *cursor;
4519 const struct wined3d_gl_info *gl_info;
4520 struct wined3d_context *context;
4521 struct wined3d_shader *shader;
4523 context = context_acquire(device, NULL);
4524 gl_info = context->gl_info;
4526 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4528 TRACE("Unloading resource %p.\n", resource);
4530 resource->resource_ops->resource_unload(resource);
4533 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4535 device->shader_backend->shader_destroy(shader);
4538 if (device->depth_blt_texture)
4540 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4541 device->depth_blt_texture = 0;
4544 device->blitter->free_private(device);
4545 device->shader_backend->shader_free_private(device);
4546 destroy_dummy_textures(device, gl_info);
4547 destroy_default_sampler(device);
4549 context_release(context);
4551 while (device->context_count)
4553 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4556 HeapFree(GetProcessHeap(), 0, swapchain->context);
4557 swapchain->context = NULL;
4560 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4562 struct wined3d_context *context;
4563 struct wined3d_texture *target;
4564 HRESULT hr;
4566 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4567 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4569 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4570 return hr;
4573 if (FAILED(hr = device->blitter->alloc_private(device)))
4575 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4576 device->shader_backend->shader_free_private(device);
4577 return hr;
4580 /* Recreate the primary swapchain's context */
4581 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4582 if (!swapchain->context)
4584 ERR("Failed to allocate memory for swapchain context array.\n");
4585 device->blitter->free_private(device);
4586 device->shader_backend->shader_free_private(device);
4587 return E_OUTOFMEMORY;
4590 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4591 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4593 WARN("Failed to create context.\n");
4594 device->blitter->free_private(device);
4595 device->shader_backend->shader_free_private(device);
4596 HeapFree(GetProcessHeap(), 0, swapchain->context);
4597 return E_FAIL;
4600 swapchain->context[0] = context;
4601 swapchain->num_contexts = 1;
4602 create_dummy_textures(device, context);
4603 create_default_sampler(device);
4604 context_release(context);
4606 return WINED3D_OK;
4609 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4610 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4611 wined3d_device_reset_cb callback, BOOL reset_state)
4613 struct wined3d_rendertarget_view_desc view_desc;
4614 struct wined3d_resource *resource, *cursor;
4615 struct wined3d_swapchain *swapchain;
4616 struct wined3d_display_mode m;
4617 BOOL DisplayModeChanged;
4618 HRESULT hr = WINED3D_OK;
4619 unsigned int i;
4621 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4622 device, swapchain_desc, mode, callback, reset_state);
4624 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4626 ERR("Failed to get the first implicit swapchain.\n");
4627 return WINED3DERR_INVALIDCALL;
4629 DisplayModeChanged = swapchain->reapply_mode;
4631 if (reset_state)
4633 if (device->logo_texture)
4635 wined3d_texture_decref(device->logo_texture);
4636 device->logo_texture = NULL;
4638 if (device->cursor_texture)
4640 wined3d_texture_decref(device->cursor_texture);
4641 device->cursor_texture = NULL;
4643 state_unbind_resources(&device->state);
4646 if (device->fb.render_targets)
4648 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4650 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4653 wined3d_device_set_depth_stencil_view(device, NULL);
4655 if (device->onscreen_depth_stencil)
4657 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4658 device->onscreen_depth_stencil = NULL;
4661 if (reset_state)
4663 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4665 TRACE("Enumerating resource %p.\n", resource);
4666 if (FAILED(hr = callback(resource)))
4667 return hr;
4671 TRACE("New params:\n");
4672 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4673 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4674 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4675 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4676 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4677 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4678 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4679 TRACE("device_window %p\n", swapchain_desc->device_window);
4680 TRACE("windowed %#x\n", swapchain_desc->windowed);
4681 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4682 if (swapchain_desc->enable_auto_depth_stencil)
4683 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4684 TRACE("flags %#x\n", swapchain_desc->flags);
4685 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4686 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4687 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4689 /* No special treatment of these parameters. Just store them */
4690 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4691 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4692 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4693 swapchain->desc.flags = swapchain_desc->flags;
4694 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4695 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4696 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4698 if (swapchain_desc->device_window
4699 && swapchain_desc->device_window != swapchain->desc.device_window)
4701 TRACE("Changing the device window from %p to %p.\n",
4702 swapchain->desc.device_window, swapchain_desc->device_window);
4703 swapchain->desc.device_window = swapchain_desc->device_window;
4704 swapchain->device_window = swapchain_desc->device_window;
4705 wined3d_swapchain_set_window(swapchain, NULL);
4708 if (mode)
4710 DisplayModeChanged = TRUE;
4711 m = *mode;
4713 else if (swapchain_desc->windowed)
4715 m = swapchain->original_mode;
4717 else
4719 m.width = swapchain_desc->backbuffer_width;
4720 m.height = swapchain_desc->backbuffer_height;
4721 m.refresh_rate = swapchain_desc->refresh_rate;
4722 m.format_id = swapchain_desc->backbuffer_format;
4723 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4725 if ((m.width != swapchain->desc.backbuffer_width
4726 || m.height != swapchain->desc.backbuffer_height))
4727 DisplayModeChanged = TRUE;
4730 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4731 || DisplayModeChanged)
4733 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4735 WARN("Failed to set display mode, hr %#x.\n", hr);
4736 return WINED3DERR_INVALIDCALL;
4739 if (!swapchain_desc->windowed)
4741 if (swapchain->desc.windowed)
4743 HWND focus_window = device->create_parms.focus_window;
4744 if (!focus_window)
4745 focus_window = swapchain_desc->device_window;
4746 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4748 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4749 return hr;
4752 /* switch from windowed to fs */
4753 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4754 swapchain_desc->backbuffer_width,
4755 swapchain_desc->backbuffer_height);
4757 else
4759 /* Fullscreen -> fullscreen mode change */
4760 MoveWindow(swapchain->device_window, 0, 0,
4761 swapchain_desc->backbuffer_width,
4762 swapchain_desc->backbuffer_height,
4763 TRUE);
4765 swapchain->d3d_mode = m;
4767 else if (!swapchain->desc.windowed)
4769 /* Fullscreen -> windowed switch */
4770 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4771 wined3d_device_release_focus_window(device);
4773 swapchain->desc.windowed = swapchain_desc->windowed;
4775 else if (!swapchain_desc->windowed)
4777 DWORD style = device->style;
4778 DWORD exStyle = device->exStyle;
4779 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4780 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4781 * Reset to clear up their mess. Guild Wars also loses the device during that.
4783 device->style = 0;
4784 device->exStyle = 0;
4785 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4786 swapchain_desc->backbuffer_width,
4787 swapchain_desc->backbuffer_height);
4788 device->style = style;
4789 device->exStyle = exStyle;
4792 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4793 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4794 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4795 return hr;
4797 if (device->auto_depth_stencil_view)
4799 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4800 device->auto_depth_stencil_view = NULL;
4802 if (swapchain->desc.enable_auto_depth_stencil)
4804 struct wined3d_resource_desc texture_desc;
4805 struct wined3d_texture *texture;
4807 TRACE("Creating the depth stencil buffer\n");
4809 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4810 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4811 texture_desc.multisample_type = swapchain->desc.multisample_type;
4812 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4813 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4814 texture_desc.pool = WINED3D_POOL_DEFAULT;
4815 texture_desc.width = swapchain->desc.backbuffer_width;
4816 texture_desc.height = swapchain->desc.backbuffer_height;
4817 texture_desc.depth = 1;
4818 texture_desc.size = 0;
4820 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4821 device->device_parent, &texture_desc, &texture)))
4823 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4824 return WINED3DERR_INVALIDCALL;
4827 view_desc.format_id = texture->resource.format->id;
4828 view_desc.u.texture.level_idx = 0;
4829 view_desc.u.texture.layer_idx = 0;
4830 view_desc.u.texture.layer_count = 1;
4831 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4832 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4833 wined3d_texture_decref(texture);
4834 if (FAILED(hr))
4836 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4837 return hr;
4840 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4843 if (device->back_buffer_view)
4845 wined3d_rendertarget_view_decref(device->back_buffer_view);
4846 device->back_buffer_view = NULL;
4848 if (swapchain->desc.backbuffer_count)
4850 view_desc.format_id = swapchain_desc->backbuffer_format;
4851 view_desc.u.texture.level_idx = 0;
4852 view_desc.u.texture.layer_idx = 0;
4853 view_desc.u.texture.layer_count = 1;
4854 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
4855 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4857 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4858 return hr;
4862 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4864 if (reset_state)
4866 TRACE("Resetting stateblock.\n");
4867 if (device->recording)
4869 wined3d_stateblock_decref(device->recording);
4870 device->recording = NULL;
4872 wined3d_cs_emit_reset_state(device->cs);
4873 state_cleanup(&device->state);
4875 if (device->d3d_initialized)
4876 delete_opengl_contexts(device, swapchain);
4878 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4879 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4880 ERR("Failed to initialize device state, hr %#x.\n", hr);
4881 device->update_state = &device->state;
4883 device_init_swapchain_state(device, swapchain);
4885 else if (device->back_buffer_view)
4887 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4888 struct wined3d_state *state = &device->state;
4890 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4892 /* Note the min_z / max_z is not reset. */
4893 state->viewport.x = 0;
4894 state->viewport.y = 0;
4895 state->viewport.width = view->width;
4896 state->viewport.height = view->height;
4897 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4899 state->scissor_rect.top = 0;
4900 state->scissor_rect.left = 0;
4901 state->scissor_rect.right = view->width;
4902 state->scissor_rect.bottom = view->height;
4903 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4906 if (device->d3d_initialized)
4908 if (reset_state)
4909 hr = create_primary_opengl_context(device, swapchain);
4910 swapchain_update_swap_interval(swapchain);
4913 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4914 * first use
4916 return hr;
4919 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4921 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4923 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4925 return WINED3D_OK;
4929 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4930 struct wined3d_device_creation_parameters *parameters)
4932 TRACE("device %p, parameters %p.\n", device, parameters);
4934 *parameters = device->create_parms;
4937 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4939 TRACE("device %p.\n", device);
4941 return device->wined3d;
4944 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4945 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4947 struct wined3d_swapchain *swapchain;
4949 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4950 device, swapchain_idx, flags, ramp);
4952 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4953 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4956 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4957 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4959 struct wined3d_swapchain *swapchain;
4961 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4962 device, swapchain_idx, ramp);
4964 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4965 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4968 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4970 TRACE("device %p, resource %p.\n", device, resource);
4972 list_add_head(&device->resources, &resource->resource_list_entry);
4975 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4977 TRACE("device %p, resource %p.\n", device, resource);
4979 list_remove(&resource->resource_list_entry);
4982 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4984 enum wined3d_resource_type type = resource->type;
4985 struct wined3d_rendertarget_view *rtv;
4986 unsigned int i;
4988 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4990 context_resource_released(device, resource, type);
4992 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4994 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4995 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4998 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4999 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5001 switch (type)
5003 case WINED3D_RTYPE_TEXTURE_2D:
5004 case WINED3D_RTYPE_TEXTURE_3D:
5005 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5007 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5009 if (device->state.textures[i] == texture)
5011 ERR("Texture %p is still in use, stage %u.\n", texture, i);
5012 device->state.textures[i] = NULL;
5015 if (device->recording && device->update_state->textures[i] == texture)
5017 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
5018 texture, device->recording, i);
5019 device->update_state->textures[i] = NULL;
5022 break;
5024 case WINED3D_RTYPE_BUFFER:
5026 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5028 for (i = 0; i < MAX_STREAMS; ++i)
5030 if (device->state.streams[i].buffer == buffer)
5032 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
5033 device->state.streams[i].buffer = NULL;
5036 if (device->recording && device->update_state->streams[i].buffer == buffer)
5038 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5039 buffer, device->recording, i);
5040 device->update_state->streams[i].buffer = NULL;
5044 if (device->state.index_buffer == buffer)
5046 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5047 device->state.index_buffer = NULL;
5050 if (device->recording && device->update_state->index_buffer == buffer)
5052 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5053 buffer, device->recording);
5054 device->update_state->index_buffer = NULL;
5057 break;
5059 default:
5060 break;
5063 /* Remove the resource from the resourceStore */
5064 device_resource_remove(device, resource);
5066 TRACE("Resource released.\n");
5069 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5071 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5073 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5076 static const struct wine_rb_functions wined3d_sampler_rb_functions =
5078 wined3d_rb_alloc,
5079 wined3d_rb_realloc,
5080 wined3d_rb_free,
5081 wined3d_sampler_compare,
5084 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5085 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5086 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5088 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5089 const struct fragment_pipeline *fragment_pipeline;
5090 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5091 unsigned int i;
5092 HRESULT hr;
5094 device->ref = 1;
5095 device->wined3d = wined3d;
5096 wined3d_incref(device->wined3d);
5097 device->adapter = wined3d->adapter_count ? adapter : NULL;
5098 device->device_parent = device_parent;
5099 list_init(&device->resources);
5100 list_init(&device->shaders);
5101 device->surface_alignment = surface_alignment;
5103 /* Save the creation parameters. */
5104 device->create_parms.adapter_idx = adapter_idx;
5105 device->create_parms.device_type = device_type;
5106 device->create_parms.focus_window = focus_window;
5107 device->create_parms.flags = flags;
5109 device->shader_backend = adapter->shader_backend;
5111 vertex_pipeline = adapter->vertex_pipe;
5113 fragment_pipeline = adapter->fragment_pipe;
5115 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
5117 ERR("Failed to initialize sampler rbtree.\n");
5118 return E_OUTOFMEMORY;
5121 if (vertex_pipeline->vp_states && fragment_pipeline->states
5122 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5123 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5124 fragment_pipeline, misc_state_template)))
5126 ERR("Failed to compile state table, hr %#x.\n", hr);
5127 wine_rb_destroy(&device->samplers, NULL, NULL);
5128 wined3d_decref(device->wined3d);
5129 return hr;
5132 device->blitter = adapter->blitter;
5134 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
5135 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
5137 ERR("Failed to initialize device state, hr %#x.\n", hr);
5138 goto err;
5140 device->update_state = &device->state;
5142 if (!(device->cs = wined3d_cs_create(device)))
5144 WARN("Failed to create command stream.\n");
5145 state_cleanup(&device->state);
5146 hr = E_FAIL;
5147 goto err;
5150 return WINED3D_OK;
5152 err:
5153 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5155 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5157 wine_rb_destroy(&device->samplers, NULL, NULL);
5158 wined3d_decref(device->wined3d);
5159 return hr;
5163 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5165 DWORD rep = device->StateTable[state].representative;
5166 struct wined3d_context *context;
5167 DWORD idx;
5168 BYTE shift;
5169 UINT i;
5171 for (i = 0; i < device->context_count; ++i)
5173 context = device->contexts[i];
5174 if(isStateDirty(context, rep)) continue;
5176 context->dirtyArray[context->numDirtyEntries++] = rep;
5177 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5178 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5179 context->isStateDirty[idx] |= (1u << shift);
5183 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5184 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5186 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5188 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5189 window, message, wparam, lparam);
5190 if (unicode)
5191 return DefWindowProcW(window, message, wparam, lparam);
5192 else
5193 return DefWindowProcA(window, message, wparam, lparam);
5196 if (message == WM_DESTROY)
5198 TRACE("unregister window %p.\n", window);
5199 wined3d_unregister_window(window);
5201 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5202 ERR("Window %p is not the focus window for device %p.\n", window, device);
5204 else if (message == WM_DISPLAYCHANGE)
5206 device->device_parent->ops->mode_changed(device->device_parent);
5208 else if (message == WM_ACTIVATEAPP)
5210 UINT i;
5212 for (i = 0; i < device->swapchain_count; i++)
5213 wined3d_swapchain_activate(device->swapchains[i], wparam);
5215 device->device_parent->ops->activate(device->device_parent, wparam);
5217 else if (message == WM_SYSCOMMAND)
5219 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5221 if (unicode)
5222 DefWindowProcW(window, message, wparam, lparam);
5223 else
5224 DefWindowProcA(window, message, wparam, lparam);
5228 if (unicode)
5229 return CallWindowProcW(proc, window, message, wparam, lparam);
5230 else
5231 return CallWindowProcA(proc, window, message, wparam, lparam);