wined3d: Make wined3d_device_set_vs_consts_b() consistent with wined3d_device_set_vs_...
[wine.git] / dlls / wined3d / device.c
blobec489e944f2a81a11dab80a89be29000cb4da91b
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 void device_switch_onscreen_ds(struct wined3d_device *device,
202 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
204 if (device->onscreen_depth_stencil)
206 surface_load_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
208 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
209 device->onscreen_depth_stencil->ds_current_size.cx,
210 device->onscreen_depth_stencil->ds_current_size.cy);
211 wined3d_texture_decref(device->onscreen_depth_stencil->container);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_texture_incref(device->onscreen_depth_stencil->container);
217 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
219 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
220 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
222 /* partial draw rect */
223 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
224 return FALSE;
226 /* partial clear rect */
227 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
228 || clear_rect->right < width || clear_rect->bottom < height))
229 return FALSE;
231 return TRUE;
234 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
235 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
237 struct wined3d_texture_sub_resource *sub_resource = surface_get_sub_resource(ds);
238 RECT current_rect, r;
240 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
242 /* Depth buffer was discarded, make it entirely current in its new location since
243 * there is no other place where we would get data anyway. */
244 SetRect(out_rect, 0, 0,
245 wined3d_texture_get_level_width(ds->container, ds->texture_level),
246 wined3d_texture_get_level_height(ds->container, ds->texture_level));
247 return;
250 if (sub_resource->locations & location)
251 SetRect(&current_rect, 0, 0,
252 ds->ds_current_size.cx,
253 ds->ds_current_size.cy);
254 else
255 SetRectEmpty(&current_rect);
257 IntersectRect(&r, draw_rect, &current_rect);
258 if (EqualRect(&r, draw_rect))
260 /* current_rect ⊇ draw_rect, modify only. */
261 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
262 return;
265 if (EqualRect(&r, &current_rect))
267 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
269 if (!rect_count)
271 /* Full clear, modify only. */
272 *out_rect = *draw_rect;
273 return;
276 IntersectRect(&r, draw_rect, clear_rect);
277 if (EqualRect(&r, draw_rect))
279 /* clear_rect ⊇ draw_rect, modify only. */
280 *out_rect = *draw_rect;
281 return;
285 /* Full load. */
286 surface_load_location(ds, context, location);
287 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
290 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
291 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
292 float depth, DWORD stencil)
294 struct wined3d_surface *target = rt_count ? wined3d_rendertarget_view_get_surface(fb->render_targets[0]) : NULL;
295 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
296 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
297 const struct wined3d_state *state = &device->state;
298 const struct wined3d_gl_info *gl_info;
299 UINT drawable_width, drawable_height;
300 struct wined3d_color corrected_color;
301 struct wined3d_context *context;
302 GLbitfield clear_mask = 0;
303 BOOL render_offscreen;
304 unsigned int i;
305 RECT ds_rect = {0};
307 context = context_acquire(device, target);
308 if (!context->valid)
310 context_release(context);
311 WARN("Invalid context, skipping clear.\n");
312 return;
314 gl_info = context->gl_info;
316 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
317 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
318 * for the cleared parts, and the untouched parts.
320 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
321 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
322 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
323 * checking all this if the dest surface is in the drawable anyway. */
324 for (i = 0; i < rt_count; ++i)
326 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
327 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(rtv);
329 if (rt && rtv->format->id != WINED3DFMT_NULL)
331 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
332 surface_load_location(rt, context, rtv->resource->draw_binding);
333 else
334 wined3d_texture_prepare_location(rt->container, rtv->sub_resource_idx,
335 context, rtv->resource->draw_binding);
339 if (target)
341 render_offscreen = context->render_offscreen;
342 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
344 else
346 render_offscreen = TRUE;
347 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
348 depth_stencil->texture_level);
349 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
350 depth_stencil->texture_level);
353 if (depth_stencil && render_offscreen)
354 wined3d_texture_prepare_location(depth_stencil->container,
355 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
357 if (flags & WINED3DCLEAR_ZBUFFER)
359 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
361 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
362 device_switch_onscreen_ds(device, context, depth_stencil);
363 prepare_ds_clear(depth_stencil, context, location,
364 draw_rect, rect_count, clear_rect, &ds_rect);
367 if (!context_apply_clear_state(context, state, rt_count, fb))
369 context_release(context);
370 WARN("Failed to apply clear state, skipping clear.\n");
371 return;
374 /* Only set the values up once, as they are not changing. */
375 if (flags & WINED3DCLEAR_STENCIL)
377 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
379 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
380 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
382 gl_info->gl_ops.gl.p_glStencilMask(~0U);
383 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
384 gl_info->gl_ops.gl.p_glClearStencil(stencil);
385 checkGLcall("glClearStencil");
386 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
389 if (flags & WINED3DCLEAR_ZBUFFER)
391 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
393 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
395 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
396 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
397 gl_info->gl_ops.gl.p_glClearDepth(depth);
398 checkGLcall("glClearDepth");
399 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
402 if (flags & WINED3DCLEAR_TARGET)
404 for (i = 0; i < rt_count; ++i)
406 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
407 struct wined3d_texture *texture;
409 if (!rtv)
410 continue;
412 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
414 FIXME("Not supported on buffer resources.\n");
415 continue;
418 texture = texture_from_resource(rtv->resource);
419 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
420 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
423 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
425 if (rt_count > 1)
426 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
427 "support, this might cause graphical issues.\n");
429 corrected_color.r = color->r < wined3d_srgb_const1[0]
430 ? color->r * wined3d_srgb_const0[3]
431 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
432 - wined3d_srgb_const0[2];
433 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
434 corrected_color.g = color->g < wined3d_srgb_const1[0]
435 ? color->g * wined3d_srgb_const0[3]
436 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
437 - wined3d_srgb_const0[2];
438 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
439 corrected_color.b = color->b < wined3d_srgb_const1[0]
440 ? color->b * wined3d_srgb_const0[3]
441 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
442 - wined3d_srgb_const0[2];
443 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
444 color = &corrected_color;
447 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
448 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
449 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
450 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
451 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
452 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
453 checkGLcall("glClearColor");
454 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
457 if (!rect_count)
459 if (render_offscreen)
461 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
462 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
464 else
466 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
467 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
469 checkGLcall("glScissor");
470 gl_info->gl_ops.gl.p_glClear(clear_mask);
471 checkGLcall("glClear");
473 else
475 RECT current_rect;
477 /* Now process each rect in turn. */
478 for (i = 0; i < rect_count; ++i)
480 /* Note that GL uses lower left, width/height. */
481 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
483 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
484 wine_dbgstr_rect(&clear_rect[i]),
485 wine_dbgstr_rect(&current_rect));
487 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
488 * The rectangle is not cleared, no error is returned, but further rectangles are
489 * still cleared if they are valid. */
490 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
492 TRACE("Rectangle with negative dimensions, ignoring.\n");
493 continue;
496 if (render_offscreen)
498 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
499 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
501 else
503 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
504 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
506 checkGLcall("glScissor");
508 gl_info->gl_ops.gl.p_glClear(clear_mask);
509 checkGLcall("glClear");
513 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
514 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
515 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
517 context_release(context);
520 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
522 ULONG refcount = InterlockedIncrement(&device->ref);
524 TRACE("%p increasing refcount to %u.\n", device, refcount);
526 return refcount;
529 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
531 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
533 ERR("Leftover sampler %p.\n", sampler);
536 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
538 ULONG refcount = InterlockedDecrement(&device->ref);
540 TRACE("%p decreasing refcount to %u.\n", device, refcount);
542 if (!refcount)
544 UINT i;
546 wined3d_cs_destroy(device->cs);
548 if (device->recording && wined3d_stateblock_decref(device->recording))
549 FIXME("Something's still holding the recording stateblock.\n");
550 device->recording = NULL;
552 state_cleanup(&device->state);
554 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
556 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
557 device->multistate_funcs[i] = NULL;
560 if (!list_empty(&device->resources))
562 struct wined3d_resource *resource;
564 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
566 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
568 FIXME("Leftover resource %p with type %s (%#x).\n",
569 resource, debug_d3dresourcetype(resource->type), resource->type);
573 if (device->contexts)
574 ERR("Context array not freed!\n");
575 if (device->hardwareCursor)
576 DestroyCursor(device->hardwareCursor);
577 device->hardwareCursor = 0;
579 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
581 wined3d_decref(device->wined3d);
582 device->wined3d = NULL;
583 HeapFree(GetProcessHeap(), 0, device);
584 TRACE("Freed device %p.\n", device);
587 return refcount;
590 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
592 TRACE("device %p.\n", device);
594 return device->swapchain_count;
597 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
599 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
601 if (swapchain_idx >= device->swapchain_count)
603 WARN("swapchain_idx %u >= swapchain_count %u.\n",
604 swapchain_idx, device->swapchain_count);
605 return NULL;
608 return device->swapchains[swapchain_idx];
611 static void device_load_logo(struct wined3d_device *device, const char *filename)
613 struct wined3d_color_key color_key;
614 struct wined3d_resource_desc desc;
615 HBITMAP hbm;
616 BITMAP bm;
617 HRESULT hr;
618 HDC dcb = NULL, dcs = NULL;
620 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
621 if(hbm)
623 GetObjectA(hbm, sizeof(BITMAP), &bm);
624 dcb = CreateCompatibleDC(NULL);
625 if(!dcb) goto out;
626 SelectObject(dcb, hbm);
628 else
630 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
631 * couldn't be loaded
633 memset(&bm, 0, sizeof(bm));
634 bm.bmWidth = 32;
635 bm.bmHeight = 32;
638 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
639 desc.format = WINED3DFMT_B5G6R5_UNORM;
640 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
641 desc.multisample_quality = 0;
642 desc.usage = WINED3DUSAGE_DYNAMIC;
643 desc.pool = WINED3D_POOL_DEFAULT;
644 desc.width = bm.bmWidth;
645 desc.height = bm.bmHeight;
646 desc.depth = 1;
647 desc.size = 0;
648 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
649 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
651 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
652 goto out;
655 if (dcb)
657 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
658 goto out;
659 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
660 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
662 color_key.color_space_low_value = 0;
663 color_key.color_space_high_value = 0;
664 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
666 else
668 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
669 const RECT rect = {0, 0, desc.width, desc.height};
670 struct wined3d_surface *surface;
672 /* Fill the surface with a white color to show that wined3d is there */
673 surface = device->logo_texture->sub_resources[0].u.surface;
674 surface_color_fill(surface, &rect, &c);
677 out:
678 if (dcb) DeleteDC(dcb);
679 if (hbm) DeleteObject(hbm);
682 /* Context activation is done by the caller. */
683 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
685 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
686 unsigned int i, j, count;
687 /* Under DirectX you can sample even if no texture is bound, whereas
688 * OpenGL will only allow that when a valid texture is bound.
689 * We emulate this by creating dummy textures and binding them
690 * to each texture stage when the currently set D3D texture is NULL. */
692 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
693 for (i = 0; i < count; ++i)
695 static const DWORD d3d10_color = 0x00000000;
696 static const DWORD color = 0x000000ff;
698 /* Make appropriate texture active */
699 context_active_texture(context, gl_info, i);
701 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
702 checkGLcall("glGenTextures");
703 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
705 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
706 checkGLcall("glBindTexture");
708 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
709 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
710 checkGLcall("glTexImage2D");
712 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
714 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
715 checkGLcall("glGenTextures");
716 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
718 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
719 checkGLcall("glBindTexture");
721 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
722 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
723 checkGLcall("glTexImage2D");
726 if (gl_info->supported[EXT_TEXTURE3D])
728 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
729 checkGLcall("glGenTextures");
730 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
732 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
733 checkGLcall("glBindTexture");
735 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
736 checkGLcall("glTexImage3D");
739 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
741 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
742 checkGLcall("glGenTextures");
743 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
745 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
746 checkGLcall("glBindTexture");
748 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
750 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
751 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
752 checkGLcall("glTexImage2D");
756 if (gl_info->supported[EXT_TEXTURE_ARRAY])
758 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d_array[i]);
759 checkGLcall("glGenTextures");
760 TRACE("Dummy 2D array texture %u given name %u.\n", i, device->dummy_texture_2d_array[i]);
762 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_texture_2d_array[i]);
763 checkGLcall("glBindTexture");
765 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
766 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &d3d10_color));
767 checkGLcall("glTexImage3D");
772 /* Context activation is done by the caller. */
773 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
775 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
777 if (gl_info->supported[EXT_TEXTURE_ARRAY])
779 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d_array);
780 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d_array)");
783 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
785 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
786 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
789 if (gl_info->supported[EXT_TEXTURE3D])
791 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
792 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
795 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
797 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
798 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
801 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
802 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
804 memset(device->dummy_texture_2d_array, 0, count * sizeof(*device->dummy_texture_2d_array));
805 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
806 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
807 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
808 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
811 /* Context activation is done by the caller. */
812 static void create_default_sampler(struct wined3d_device *device)
814 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
817 * In SM4+ shaders there is a separation between resources and samplers. Some of shader
818 * instructions allow to access resources without using samplers.
819 * In GLSL resources are always accessed through sampler or image variables. The default
820 * sampler object is used to emulate the direct resource access when there is no sampler state
821 * to use.
824 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
826 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
827 checkGLcall("glGenSamplers");
828 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
829 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
830 checkGLcall("glSamplerParameteri");
832 else
834 device->default_sampler = 0;
838 /* Context activation is done by the caller. */
839 static void destroy_default_sampler(struct wined3d_device *device)
841 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
843 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
845 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
846 checkGLcall("glDeleteSamplers");
849 device->default_sampler = 0;
852 static LONG fullscreen_style(LONG style)
854 /* Make sure the window is managed, otherwise we won't get keyboard input. */
855 style |= WS_POPUP | WS_SYSMENU;
856 style &= ~(WS_CAPTION | WS_THICKFRAME);
858 return style;
861 static LONG fullscreen_exstyle(LONG exstyle)
863 /* Filter out window decorations. */
864 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
866 return exstyle;
869 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
871 BOOL filter_messages;
872 LONG style, exstyle;
874 TRACE("Setting up window %p for fullscreen mode.\n", window);
876 if (device->style || device->exStyle)
878 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
879 window, device->style, device->exStyle);
882 device->style = GetWindowLongW(window, GWL_STYLE);
883 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
885 style = fullscreen_style(device->style);
886 exstyle = fullscreen_exstyle(device->exStyle);
888 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
889 device->style, device->exStyle, style, exstyle);
891 filter_messages = device->filter_messages;
892 device->filter_messages = TRUE;
894 SetWindowLongW(window, GWL_STYLE, style);
895 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
896 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
898 device->filter_messages = filter_messages;
901 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
903 BOOL filter_messages;
904 LONG style, exstyle;
906 if (!device->style && !device->exStyle) return;
908 style = GetWindowLongW(window, GWL_STYLE);
909 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
911 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
912 * application, and we want to ignore them in the test below, since it's
913 * not the application's fault that they changed. Additionally, we want to
914 * preserve the current status of these flags (i.e. don't restore them) to
915 * more closely emulate the behavior of Direct3D, which leaves these flags
916 * alone when returning to windowed mode. */
917 device->style ^= (device->style ^ style) & WS_VISIBLE;
918 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
920 TRACE("Restoring window style of window %p to %08x, %08x.\n",
921 window, device->style, device->exStyle);
923 filter_messages = device->filter_messages;
924 device->filter_messages = TRUE;
926 /* Only restore the style if the application didn't modify it during the
927 * fullscreen phase. Some applications change it before calling Reset()
928 * when switching between windowed and fullscreen modes (HL2), some
929 * depend on the original style (Eve Online). */
930 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
932 SetWindowLongW(window, GWL_STYLE, device->style);
933 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
935 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
937 device->filter_messages = filter_messages;
939 /* Delete the old values. */
940 device->style = 0;
941 device->exStyle = 0;
944 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
946 TRACE("device %p, window %p.\n", device, window);
948 if (!wined3d_register_window(window, device))
950 ERR("Failed to register window %p.\n", window);
951 return E_FAIL;
954 InterlockedExchangePointer((void **)&device->focus_window, window);
955 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
957 return WINED3D_OK;
960 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
962 TRACE("device %p.\n", device);
964 if (device->focus_window) wined3d_unregister_window(device->focus_window);
965 InterlockedExchangePointer((void **)&device->focus_window, NULL);
968 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
970 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
971 unsigned int i;
973 if (device->fb.render_targets)
975 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
977 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
979 if (device->back_buffer_view)
980 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
983 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
984 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
987 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
988 struct wined3d_swapchain_desc *swapchain_desc)
990 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
991 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
992 struct wined3d_swapchain *swapchain = NULL;
993 struct wined3d_context *context;
994 DWORD clear_flags = 0;
995 HRESULT hr;
997 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
999 if (device->d3d_initialized)
1000 return WINED3DERR_INVALIDCALL;
1001 if (device->wined3d->flags & WINED3D_NO3D)
1002 return WINED3DERR_INVALIDCALL;
1004 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1005 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1007 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1008 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1010 TRACE("Shader private data couldn't be allocated\n");
1011 goto err_out;
1013 if (FAILED(hr = device->blitter->alloc_private(device)))
1015 TRACE("Blitter private data couldn't be allocated\n");
1016 goto err_out;
1019 /* Setup the implicit swapchain. This also initializes a context. */
1020 TRACE("Creating implicit swapchain\n");
1021 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1022 swapchain_desc, &swapchain);
1023 if (FAILED(hr))
1025 WARN("Failed to create implicit swapchain\n");
1026 goto err_out;
1029 if (swapchain_desc->backbuffer_count)
1031 struct wined3d_rendertarget_view_desc view_desc;
1033 view_desc.format_id = swapchain_desc->backbuffer_format;
1034 view_desc.u.texture.level_idx = 0;
1035 view_desc.u.texture.layer_idx = 0;
1036 view_desc.u.texture.layer_count = 1;
1037 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
1038 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1040 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1041 goto err_out;
1045 device->swapchain_count = 1;
1046 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1047 if (!device->swapchains)
1049 ERR("Out of memory!\n");
1050 goto err_out;
1052 device->swapchains[0] = swapchain;
1053 device_init_swapchain_state(device, swapchain);
1055 context = context_acquire(device, swapchain->front_buffer->sub_resources[0].u.surface);
1057 create_dummy_textures(device, context);
1058 create_default_sampler(device);
1060 device->contexts[0]->last_was_rhw = 0;
1062 TRACE("All defaults now set up, leaving 3D init.\n");
1064 context_release(context);
1066 /* Clear the screen */
1067 if (swapchain->back_buffers && swapchain->back_buffers[0])
1068 clear_flags |= WINED3DCLEAR_TARGET;
1069 if (swapchain_desc->enable_auto_depth_stencil)
1070 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1071 if (clear_flags)
1072 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1074 device->d3d_initialized = TRUE;
1076 if (wined3d_settings.logo)
1077 device_load_logo(device, wined3d_settings.logo);
1078 return WINED3D_OK;
1080 err_out:
1081 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1082 HeapFree(GetProcessHeap(), 0, device->swapchains);
1083 device->swapchain_count = 0;
1084 if (device->back_buffer_view)
1085 wined3d_rendertarget_view_decref(device->back_buffer_view);
1086 if (swapchain)
1087 wined3d_swapchain_decref(swapchain);
1088 if (device->blit_priv)
1089 device->blitter->free_private(device);
1090 if (device->shader_priv)
1091 device->shader_backend->shader_free_private(device);
1093 return hr;
1096 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1097 struct wined3d_swapchain_desc *swapchain_desc)
1099 struct wined3d_swapchain *swapchain = NULL;
1100 HRESULT hr;
1102 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1104 /* Setup the implicit swapchain */
1105 TRACE("Creating implicit swapchain\n");
1106 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1107 swapchain_desc, &swapchain);
1108 if (FAILED(hr))
1110 WARN("Failed to create implicit swapchain\n");
1111 goto err_out;
1114 device->swapchain_count = 1;
1115 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1116 if (!device->swapchains)
1118 ERR("Out of memory!\n");
1119 goto err_out;
1121 device->swapchains[0] = swapchain;
1122 return WINED3D_OK;
1124 err_out:
1125 wined3d_swapchain_decref(swapchain);
1126 return hr;
1129 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1131 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1133 wined3d_sampler_decref(sampler);
1136 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1138 struct wined3d_resource *resource, *cursor;
1139 const struct wined3d_gl_info *gl_info;
1140 struct wined3d_context *context;
1141 struct wined3d_surface *surface;
1142 UINT i;
1144 TRACE("device %p.\n", device);
1146 if (!device->d3d_initialized)
1147 return WINED3DERR_INVALIDCALL;
1149 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1150 * it was created. Thus make sure a context is active for the glDelete* calls
1152 context = context_acquire(device, NULL);
1153 gl_info = context->gl_info;
1155 if (device->logo_texture)
1156 wined3d_texture_decref(device->logo_texture);
1157 if (device->cursor_texture)
1158 wined3d_texture_decref(device->cursor_texture);
1160 state_unbind_resources(&device->state);
1162 /* Unload resources */
1163 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1165 TRACE("Unloading resource %p.\n", resource);
1166 resource->resource_ops->resource_unload(resource);
1169 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1171 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1172 * private data, it might contain opengl pointers
1174 if (device->depth_blt_texture)
1176 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1177 device->depth_blt_texture = 0;
1180 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1181 device->blitter->free_private(device);
1182 device->shader_backend->shader_free_private(device);
1183 destroy_dummy_textures(device, gl_info);
1184 destroy_default_sampler(device);
1186 /* Release the context again as soon as possible. In particular,
1187 * releasing the render target views below may release the last reference
1188 * to the swapchain associated with this context, which in turn will
1189 * destroy the context. */
1190 context_release(context);
1192 /* Release the buffers (with sanity checks)*/
1193 if (device->onscreen_depth_stencil)
1195 surface = device->onscreen_depth_stencil;
1196 device->onscreen_depth_stencil = NULL;
1197 wined3d_texture_decref(surface->container);
1200 if (device->fb.depth_stencil)
1202 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1204 TRACE("Releasing depth/stencil view %p.\n", view);
1206 device->fb.depth_stencil = NULL;
1207 wined3d_rendertarget_view_decref(view);
1210 if (device->auto_depth_stencil_view)
1212 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1214 device->auto_depth_stencil_view = NULL;
1215 if (wined3d_rendertarget_view_decref(view))
1216 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1219 for (i = 0; i < gl_info->limits.buffers; ++i)
1221 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1223 if (device->back_buffer_view)
1225 wined3d_rendertarget_view_decref(device->back_buffer_view);
1226 device->back_buffer_view = NULL;
1229 for (i = 0; i < device->swapchain_count; ++i)
1231 TRACE("Releasing the implicit swapchain %u.\n", i);
1232 if (wined3d_swapchain_decref(device->swapchains[i]))
1233 FIXME("Something's still holding the implicit swapchain.\n");
1236 HeapFree(GetProcessHeap(), 0, device->swapchains);
1237 device->swapchains = NULL;
1238 device->swapchain_count = 0;
1240 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1241 device->fb.render_targets = NULL;
1243 device->d3d_initialized = FALSE;
1245 return WINED3D_OK;
1248 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1250 unsigned int i;
1252 for (i = 0; i < device->swapchain_count; ++i)
1254 TRACE("Releasing the implicit swapchain %u.\n", i);
1255 if (wined3d_swapchain_decref(device->swapchains[i]))
1256 FIXME("Something's still holding the implicit swapchain.\n");
1259 HeapFree(GetProcessHeap(), 0, device->swapchains);
1260 device->swapchains = NULL;
1261 device->swapchain_count = 0;
1262 return WINED3D_OK;
1265 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1266 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1267 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1269 * There is no way to deactivate thread safety once it is enabled.
1271 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1273 TRACE("device %p.\n", device);
1275 /* For now just store the flag (needed in case of ddraw). */
1276 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1279 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1281 TRACE("device %p.\n", device);
1283 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1284 wine_dbgstr_longlong(device->adapter->vram_bytes),
1285 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1286 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1288 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1291 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1292 struct wined3d_buffer *buffer, UINT offset)
1294 struct wined3d_stream_output *stream;
1295 struct wined3d_buffer *prev_buffer;
1297 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1299 if (idx >= MAX_STREAM_OUT)
1301 WARN("Invalid stream output %u.\n", idx);
1302 return;
1305 stream = &device->update_state->stream_output[idx];
1306 prev_buffer = stream->buffer;
1308 if (buffer)
1309 wined3d_buffer_incref(buffer);
1310 stream->buffer = buffer;
1311 stream->offset = offset;
1312 if (!device->recording)
1313 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1314 if (prev_buffer)
1315 wined3d_buffer_decref(prev_buffer);
1318 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1319 UINT idx, UINT *offset)
1321 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1323 if (idx >= MAX_STREAM_OUT)
1325 WARN("Invalid stream output %u.\n", idx);
1326 return NULL;
1329 if (offset)
1330 *offset = device->state.stream_output[idx].offset;
1331 return device->state.stream_output[idx].buffer;
1334 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1335 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1337 struct wined3d_stream_state *stream;
1338 struct wined3d_buffer *prev_buffer;
1340 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1341 device, stream_idx, buffer, offset, stride);
1343 if (stream_idx >= MAX_STREAMS)
1345 WARN("Stream index %u out of range.\n", stream_idx);
1346 return WINED3DERR_INVALIDCALL;
1348 else if (offset & 0x3)
1350 WARN("Offset %u is not 4 byte aligned.\n", offset);
1351 return WINED3DERR_INVALIDCALL;
1354 stream = &device->update_state->streams[stream_idx];
1355 prev_buffer = stream->buffer;
1357 if (device->recording)
1358 device->recording->changed.streamSource |= 1u << stream_idx;
1360 if (prev_buffer == buffer
1361 && stream->stride == stride
1362 && stream->offset == offset)
1364 TRACE("Application is setting the old values over, nothing to do.\n");
1365 return WINED3D_OK;
1368 stream->buffer = buffer;
1369 if (buffer)
1371 stream->stride = stride;
1372 stream->offset = offset;
1373 wined3d_buffer_incref(buffer);
1376 if (!device->recording)
1377 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1378 if (prev_buffer)
1379 wined3d_buffer_decref(prev_buffer);
1381 return WINED3D_OK;
1384 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1385 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1387 const struct wined3d_stream_state *stream;
1389 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1390 device, stream_idx, buffer, offset, stride);
1392 if (stream_idx >= MAX_STREAMS)
1394 WARN("Stream index %u out of range.\n", stream_idx);
1395 return WINED3DERR_INVALIDCALL;
1398 stream = &device->state.streams[stream_idx];
1399 *buffer = stream->buffer;
1400 if (offset)
1401 *offset = stream->offset;
1402 *stride = stream->stride;
1404 return WINED3D_OK;
1407 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1409 struct wined3d_stream_state *stream;
1410 UINT old_flags, old_freq;
1412 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1414 /* Verify input. At least in d3d9 this is invalid. */
1415 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1417 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1418 return WINED3DERR_INVALIDCALL;
1420 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1422 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1423 return WINED3DERR_INVALIDCALL;
1425 if (!divider)
1427 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1428 return WINED3DERR_INVALIDCALL;
1431 stream = &device->update_state->streams[stream_idx];
1432 old_flags = stream->flags;
1433 old_freq = stream->frequency;
1435 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1436 stream->frequency = divider & 0x7fffff;
1438 if (device->recording)
1439 device->recording->changed.streamFreq |= 1u << stream_idx;
1440 else if (stream->frequency != old_freq || stream->flags != old_flags)
1441 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1443 return WINED3D_OK;
1446 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1447 UINT stream_idx, UINT *divider)
1449 const struct wined3d_stream_state *stream;
1451 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1453 stream = &device->state.streams[stream_idx];
1454 *divider = stream->flags | stream->frequency;
1456 TRACE("Returning %#x.\n", *divider);
1458 return WINED3D_OK;
1461 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1462 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1464 TRACE("device %p, state %s, matrix %p.\n",
1465 device, debug_d3dtstype(d3dts), matrix);
1466 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1467 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1468 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1469 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1471 /* Handle recording of state blocks. */
1472 if (device->recording)
1474 TRACE("Recording... not performing anything.\n");
1475 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1476 device->update_state->transforms[d3dts] = *matrix;
1477 return;
1480 /* If the new matrix is the same as the current one,
1481 * we cut off any further processing. this seems to be a reasonable
1482 * optimization because as was noticed, some apps (warcraft3 for example)
1483 * tend towards setting the same matrix repeatedly for some reason.
1485 * From here on we assume that the new matrix is different, wherever it matters. */
1486 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1488 TRACE("The application is setting the same matrix over again.\n");
1489 return;
1492 device->state.transforms[d3dts] = *matrix;
1493 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1496 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1497 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1499 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1501 *matrix = device->state.transforms[state];
1504 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1505 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1507 const struct wined3d_matrix *mat;
1508 struct wined3d_matrix temp;
1510 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1512 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1513 * below means it will be recorded in a state block change, but it
1514 * works regardless where it is recorded.
1515 * If this is found to be wrong, change to StateBlock. */
1516 if (state > HIGHEST_TRANSFORMSTATE)
1518 WARN("Unhandled transform state %#x.\n", state);
1519 return;
1522 mat = &device->update_state->transforms[state];
1523 multiply_matrix(&temp, mat, matrix);
1525 /* Apply change via set transform - will reapply to eg. lights this way. */
1526 wined3d_device_set_transform(device, state, &temp);
1529 /* Note lights are real special cases. Although the device caps state only
1530 * e.g. 8 are supported, you can reference any indexes you want as long as
1531 * that number max are enabled at any one point in time. Therefore since the
1532 * indices can be anything, we need a hashmap of them. However, this causes
1533 * stateblock problems. When capturing the state block, I duplicate the
1534 * hashmap, but when recording, just build a chain pretty much of commands to
1535 * be replayed. */
1536 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1537 UINT light_idx, const struct wined3d_light *light)
1539 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1540 struct wined3d_light_info *object = NULL;
1541 struct list *e;
1542 float rho;
1544 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1546 /* Check the parameter range. Need for speed most wanted sets junk lights
1547 * which confuse the GL driver. */
1548 if (!light)
1549 return WINED3DERR_INVALIDCALL;
1551 switch (light->type)
1553 case WINED3D_LIGHT_POINT:
1554 case WINED3D_LIGHT_SPOT:
1555 case WINED3D_LIGHT_GLSPOT:
1556 /* Incorrect attenuation values can cause the gl driver to crash.
1557 * Happens with Need for speed most wanted. */
1558 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1560 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1561 return WINED3DERR_INVALIDCALL;
1563 break;
1565 case WINED3D_LIGHT_DIRECTIONAL:
1566 case WINED3D_LIGHT_PARALLELPOINT:
1567 /* Ignores attenuation */
1568 break;
1570 default:
1571 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1572 return WINED3DERR_INVALIDCALL;
1575 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1577 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1578 if (object->OriginalIndex == light_idx)
1579 break;
1580 object = NULL;
1583 if (!object)
1585 TRACE("Adding new light\n");
1586 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1587 if (!object)
1588 return E_OUTOFMEMORY;
1590 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1591 object->glIndex = -1;
1592 object->OriginalIndex = light_idx;
1595 /* Initialize the object. */
1596 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1597 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1598 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1599 light_idx, light->type, debug_color(&light->diffuse),
1600 debug_color(&light->specular), debug_color(&light->ambient),
1601 light->position.x, light->position.y, light->position.z,
1602 light->direction.x, light->direction.y, light->direction.z,
1603 light->range, light->falloff, light->theta, light->phi);
1605 /* Update the live definitions if the light is currently assigned a glIndex. */
1606 if (object->glIndex != -1 && !device->recording)
1608 if (object->OriginalParms.type != light->type)
1609 device_invalidate_state(device, STATE_LIGHT_TYPE);
1610 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1613 /* Save away the information. */
1614 object->OriginalParms = *light;
1616 switch (light->type)
1618 case WINED3D_LIGHT_POINT:
1619 /* Position */
1620 object->position.x = light->position.x;
1621 object->position.y = light->position.y;
1622 object->position.z = light->position.z;
1623 object->position.w = 1.0f;
1624 object->cutoff = 180.0f;
1625 /* FIXME: Range */
1626 break;
1628 case WINED3D_LIGHT_DIRECTIONAL:
1629 /* Direction */
1630 object->direction.x = -light->direction.x;
1631 object->direction.y = -light->direction.y;
1632 object->direction.z = -light->direction.z;
1633 object->direction.w = 0.0f;
1634 object->exponent = 0.0f;
1635 object->cutoff = 180.0f;
1636 break;
1638 case WINED3D_LIGHT_SPOT:
1639 /* Position */
1640 object->position.x = light->position.x;
1641 object->position.y = light->position.y;
1642 object->position.z = light->position.z;
1643 object->position.w = 1.0f;
1645 /* Direction */
1646 object->direction.x = light->direction.x;
1647 object->direction.y = light->direction.y;
1648 object->direction.z = light->direction.z;
1649 object->direction.w = 0.0f;
1651 /* opengl-ish and d3d-ish spot lights use too different models
1652 * for the light "intensity" as a function of the angle towards
1653 * the main light direction, so we only can approximate very
1654 * roughly. However, spot lights are rather rarely used in games
1655 * (if ever used at all). Furthermore if still used, probably
1656 * nobody pays attention to such details. */
1657 if (!light->falloff)
1659 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1660 * equations have the falloff resp. exponent parameter as an
1661 * exponent, so the spot light lighting will always be 1.0 for
1662 * both of them, and we don't have to care for the rest of the
1663 * rather complex calculation. */
1664 object->exponent = 0.0f;
1666 else
1668 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1669 if (rho < 0.0001f)
1670 rho = 0.0001f;
1671 object->exponent = -0.3f / logf(cosf(rho / 2));
1674 if (object->exponent > 128.0f)
1675 object->exponent = 128.0f;
1677 object->cutoff = (float)(light->phi * 90 / M_PI);
1678 /* FIXME: Range */
1679 break;
1681 case WINED3D_LIGHT_PARALLELPOINT:
1682 object->position.x = light->position.x;
1683 object->position.y = light->position.y;
1684 object->position.z = light->position.z;
1685 object->position.w = 1.0f;
1686 break;
1688 default:
1689 FIXME("Unrecognized light type %#x.\n", light->type);
1692 return WINED3D_OK;
1695 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1696 UINT light_idx, struct wined3d_light *light)
1698 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1699 struct wined3d_light_info *light_info = NULL;
1700 struct list *e;
1702 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1704 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1706 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1707 if (light_info->OriginalIndex == light_idx)
1708 break;
1709 light_info = NULL;
1712 if (!light_info)
1714 TRACE("Light information requested but light not defined\n");
1715 return WINED3DERR_INVALIDCALL;
1718 *light = light_info->OriginalParms;
1719 return WINED3D_OK;
1722 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1724 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1725 struct wined3d_light_info *light_info = NULL;
1726 struct list *e;
1728 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1730 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1732 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1733 if (light_info->OriginalIndex == light_idx)
1734 break;
1735 light_info = NULL;
1737 TRACE("Found light %p.\n", light_info);
1739 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1740 if (!light_info)
1742 TRACE("Light enabled requested but light not defined, so defining one!\n");
1743 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1745 /* Search for it again! Should be fairly quick as near head of list. */
1746 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1748 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1749 if (light_info->OriginalIndex == light_idx)
1750 break;
1751 light_info = NULL;
1753 if (!light_info)
1755 FIXME("Adding default lights has failed dismally\n");
1756 return WINED3DERR_INVALIDCALL;
1760 if (!enable)
1762 if (light_info->glIndex != -1)
1764 if (!device->recording)
1766 device_invalidate_state(device, STATE_LIGHT_TYPE);
1767 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1770 device->update_state->lights[light_info->glIndex] = NULL;
1771 light_info->glIndex = -1;
1773 else
1775 TRACE("Light already disabled, nothing to do\n");
1777 light_info->enabled = FALSE;
1779 else
1781 light_info->enabled = TRUE;
1782 if (light_info->glIndex != -1)
1784 TRACE("Nothing to do as light was enabled\n");
1786 else
1788 unsigned int i;
1789 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1790 /* Find a free GL light. */
1791 for (i = 0; i < gl_info->limits.lights; ++i)
1793 if (!device->update_state->lights[i])
1795 device->update_state->lights[i] = light_info;
1796 light_info->glIndex = i;
1797 break;
1800 if (light_info->glIndex == -1)
1802 /* Our tests show that Windows returns D3D_OK in this situation, even with
1803 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1804 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1805 * as well for those lights.
1807 * TODO: Test how this affects rendering. */
1808 WARN("Too many concurrently active lights\n");
1809 return WINED3D_OK;
1812 /* i == light_info->glIndex */
1813 if (!device->recording)
1815 device_invalidate_state(device, STATE_LIGHT_TYPE);
1816 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1821 return WINED3D_OK;
1824 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1826 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1827 struct wined3d_light_info *light_info = NULL;
1828 struct list *e;
1830 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1832 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1834 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1835 if (light_info->OriginalIndex == light_idx)
1836 break;
1837 light_info = NULL;
1840 if (!light_info)
1842 TRACE("Light enabled state requested but light not defined.\n");
1843 return WINED3DERR_INVALIDCALL;
1845 /* true is 128 according to SetLightEnable */
1846 *enable = light_info->enabled ? 128 : 0;
1847 return WINED3D_OK;
1850 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1851 UINT plane_idx, const struct wined3d_vec4 *plane)
1853 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1855 /* Validate plane_idx. */
1856 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1858 TRACE("Application has requested clipplane this device doesn't support.\n");
1859 return WINED3DERR_INVALIDCALL;
1862 if (device->recording)
1863 device->recording->changed.clipplane |= 1u << plane_idx;
1865 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1867 TRACE("Application is setting old values over, nothing to do.\n");
1868 return WINED3D_OK;
1871 device->update_state->clip_planes[plane_idx] = *plane;
1873 if (!device->recording)
1874 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1876 return WINED3D_OK;
1879 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1880 UINT plane_idx, struct wined3d_vec4 *plane)
1882 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1884 /* Validate plane_idx. */
1885 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1887 TRACE("Application has requested clipplane this device doesn't support.\n");
1888 return WINED3DERR_INVALIDCALL;
1891 *plane = device->state.clip_planes[plane_idx];
1893 return WINED3D_OK;
1896 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1897 const struct wined3d_clip_status *clip_status)
1899 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1901 if (!clip_status)
1902 return WINED3DERR_INVALIDCALL;
1904 return WINED3D_OK;
1907 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1908 struct wined3d_clip_status *clip_status)
1910 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1912 if (!clip_status)
1913 return WINED3DERR_INVALIDCALL;
1915 return WINED3D_OK;
1918 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1920 TRACE("device %p, material %p.\n", device, material);
1922 device->update_state->material = *material;
1924 if (device->recording)
1925 device->recording->changed.material = TRUE;
1926 else
1927 wined3d_cs_emit_set_material(device->cs, material);
1930 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1932 TRACE("device %p, material %p.\n", device, material);
1934 *material = device->state.material;
1936 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1937 TRACE("ambient %s\n", debug_color(&material->ambient));
1938 TRACE("specular %s\n", debug_color(&material->specular));
1939 TRACE("emissive %s\n", debug_color(&material->emissive));
1940 TRACE("power %.8e.\n", material->power);
1943 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1944 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1946 enum wined3d_format_id prev_format;
1947 struct wined3d_buffer *prev_buffer;
1949 TRACE("device %p, buffer %p, format %s.\n",
1950 device, buffer, debug_d3dformat(format_id));
1952 prev_buffer = device->update_state->index_buffer;
1953 prev_format = device->update_state->index_format;
1955 device->update_state->index_buffer = buffer;
1956 device->update_state->index_format = format_id;
1958 if (device->recording)
1959 device->recording->changed.indices = TRUE;
1961 if (prev_buffer == buffer && prev_format == format_id)
1962 return;
1964 if (buffer)
1965 wined3d_buffer_incref(buffer);
1966 if (!device->recording)
1967 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1968 if (prev_buffer)
1969 wined3d_buffer_decref(prev_buffer);
1972 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1973 enum wined3d_format_id *format)
1975 TRACE("device %p, format %p.\n", device, format);
1977 *format = device->state.index_format;
1978 return device->state.index_buffer;
1981 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1983 TRACE("device %p, base_index %d.\n", device, base_index);
1985 device->update_state->base_vertex_index = base_index;
1988 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1990 TRACE("device %p.\n", device);
1992 return device->state.base_vertex_index;
1995 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1997 TRACE("device %p, viewport %p.\n", device, viewport);
1998 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1999 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2001 device->update_state->viewport = *viewport;
2003 /* Handle recording of state blocks */
2004 if (device->recording)
2006 TRACE("Recording... not performing anything\n");
2007 device->recording->changed.viewport = TRUE;
2008 return;
2011 wined3d_cs_emit_set_viewport(device->cs, viewport);
2014 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2016 TRACE("device %p, viewport %p.\n", device, viewport);
2018 *viewport = device->state.viewport;
2021 static void resolve_depth_buffer(struct wined3d_state *state)
2023 struct wined3d_texture *dst_texture = state->textures[0];
2024 struct wined3d_rendertarget_view *src_view;
2025 RECT src_rect, dst_rect;
2027 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2028 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2029 return;
2031 if (!(src_view = state->fb->depth_stencil))
2032 return;
2033 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2035 FIXME("Not supported on buffer resources.\n");
2036 return;
2039 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2040 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2041 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
2042 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2045 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2046 enum wined3d_render_state state, DWORD value)
2048 DWORD old_value;
2050 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2052 if (state > WINEHIGHEST_RENDER_STATE)
2054 WARN("Unhandled render state %#x.\n", state);
2055 return;
2058 old_value = device->state.render_states[state];
2059 device->update_state->render_states[state] = value;
2061 /* Handle recording of state blocks. */
2062 if (device->recording)
2064 TRACE("Recording... not performing anything.\n");
2065 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2066 return;
2069 /* Compared here and not before the assignment to allow proper stateblock recording. */
2070 if (value == old_value)
2071 TRACE("Application is setting the old value over, nothing to do.\n");
2072 else
2073 wined3d_cs_emit_set_render_state(device->cs, state, value);
2075 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2077 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2078 resolve_depth_buffer(&device->state);
2082 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2084 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2086 return device->state.render_states[state];
2089 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2090 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2092 DWORD old_value;
2094 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2095 device, sampler_idx, debug_d3dsamplerstate(state), value);
2097 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2098 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2100 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2102 WARN("Invalid sampler %u.\n", sampler_idx);
2103 return; /* Windows accepts overflowing this array ... we do not. */
2106 old_value = device->state.sampler_states[sampler_idx][state];
2107 device->update_state->sampler_states[sampler_idx][state] = value;
2109 /* Handle recording of state blocks. */
2110 if (device->recording)
2112 TRACE("Recording... not performing anything.\n");
2113 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2114 return;
2117 if (old_value == value)
2119 TRACE("Application is setting the old value over, nothing to do.\n");
2120 return;
2123 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2126 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2127 UINT sampler_idx, enum wined3d_sampler_state state)
2129 TRACE("device %p, sampler_idx %u, state %s.\n",
2130 device, sampler_idx, debug_d3dsamplerstate(state));
2132 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2133 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2135 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2137 WARN("Invalid sampler %u.\n", sampler_idx);
2138 return 0; /* Windows accepts overflowing this array ... we do not. */
2141 return device->state.sampler_states[sampler_idx][state];
2144 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2146 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2148 if (device->recording)
2149 device->recording->changed.scissorRect = TRUE;
2151 if (EqualRect(&device->update_state->scissor_rect, rect))
2153 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2154 return;
2156 CopyRect(&device->update_state->scissor_rect, rect);
2158 if (device->recording)
2160 TRACE("Recording... not performing anything.\n");
2161 return;
2164 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2167 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2169 TRACE("device %p, rect %p.\n", device, rect);
2171 *rect = device->state.scissor_rect;
2172 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2175 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2176 struct wined3d_vertex_declaration *declaration)
2178 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2180 TRACE("device %p, declaration %p.\n", device, declaration);
2182 if (device->recording)
2183 device->recording->changed.vertexDecl = TRUE;
2185 if (declaration == prev)
2186 return;
2188 if (declaration)
2189 wined3d_vertex_declaration_incref(declaration);
2190 device->update_state->vertex_declaration = declaration;
2191 if (!device->recording)
2192 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2193 if (prev)
2194 wined3d_vertex_declaration_decref(prev);
2197 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2199 TRACE("device %p.\n", device);
2201 return device->state.vertex_declaration;
2204 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2206 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2208 TRACE("device %p, shader %p.\n", device, shader);
2210 if (device->recording)
2211 device->recording->changed.vertexShader = TRUE;
2213 if (shader == prev)
2214 return;
2216 if (shader)
2217 wined3d_shader_incref(shader);
2218 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2219 if (!device->recording)
2220 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2221 if (prev)
2222 wined3d_shader_decref(prev);
2225 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2227 TRACE("device %p.\n", device);
2229 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2232 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2233 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2235 struct wined3d_buffer *prev;
2237 if (idx >= MAX_CONSTANT_BUFFERS)
2239 WARN("Invalid constant buffer index %u.\n", idx);
2240 return;
2243 prev = device->update_state->cb[type][idx];
2244 if (buffer == prev)
2245 return;
2247 if (buffer)
2248 wined3d_buffer_incref(buffer);
2249 device->update_state->cb[type][idx] = buffer;
2250 if (!device->recording)
2251 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2252 if (prev)
2253 wined3d_buffer_decref(prev);
2256 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2258 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2260 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2263 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2265 TRACE("device %p, idx %u.\n", device, idx);
2267 if (idx >= MAX_CONSTANT_BUFFERS)
2269 WARN("Invalid constant buffer index %u.\n", idx);
2270 return NULL;
2273 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2276 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2277 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2279 struct wined3d_shader_resource_view *prev;
2281 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2283 WARN("Invalid view index %u.\n", idx);
2284 return;
2287 prev = device->update_state->shader_resource_view[type][idx];
2288 if (view == prev)
2289 return;
2291 if (view)
2292 wined3d_shader_resource_view_incref(view);
2293 device->update_state->shader_resource_view[type][idx] = view;
2294 if (!device->recording)
2295 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2296 if (prev)
2297 wined3d_shader_resource_view_decref(prev);
2300 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2301 UINT idx, struct wined3d_shader_resource_view *view)
2303 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2305 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2308 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2309 UINT idx)
2311 TRACE("device %p, idx %u.\n", device, idx);
2313 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2315 WARN("Invalid view index %u.\n", idx);
2316 return NULL;
2319 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2322 static void wined3d_device_set_sampler(struct wined3d_device *device,
2323 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2325 struct wined3d_sampler *prev;
2327 if (idx >= MAX_SAMPLER_OBJECTS)
2329 WARN("Invalid sampler index %u.\n", idx);
2330 return;
2333 prev = device->update_state->sampler[type][idx];
2334 if (sampler == prev)
2335 return;
2337 if (sampler)
2338 wined3d_sampler_incref(sampler);
2339 device->update_state->sampler[type][idx] = sampler;
2340 if (!device->recording)
2341 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2342 if (prev)
2343 wined3d_sampler_decref(prev);
2346 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2348 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2350 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2353 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2355 TRACE("device %p, idx %u.\n", device, idx);
2357 if (idx >= MAX_SAMPLER_OBJECTS)
2359 WARN("Invalid sampler index %u.\n", idx);
2360 return NULL;
2363 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2366 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2368 UINT i;
2370 for (i = 0; i < device->context_count; ++i)
2372 device->contexts[i]->constant_update_mask |= mask;
2376 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2377 unsigned int start_idx, unsigned int count, const BOOL *constants)
2379 unsigned int i;
2381 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2382 device, start_idx, count, constants);
2384 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2385 return WINED3DERR_INVALIDCALL;
2387 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2388 count = WINED3D_MAX_CONSTS_B - start_idx;
2389 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2390 if (TRACE_ON(d3d))
2392 for (i = 0; i < count; ++i)
2393 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2396 if (device->recording)
2398 for (i = start_idx; i < count + start_idx; ++i)
2399 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2401 else
2403 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2406 return WINED3D_OK;
2409 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2410 UINT start_register, BOOL *constants, UINT bool_count)
2412 UINT count = min(bool_count, WINED3D_MAX_CONSTS_B - start_register);
2414 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2415 device, start_register, constants, bool_count);
2417 if (!constants || start_register >= WINED3D_MAX_CONSTS_B)
2418 return WINED3DERR_INVALIDCALL;
2420 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2422 return WINED3D_OK;
2425 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2426 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2428 unsigned int i;
2430 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2431 device, start_idx, count, constants);
2433 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2434 return WINED3DERR_INVALIDCALL;
2436 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2437 count = WINED3D_MAX_CONSTS_I - start_idx;
2438 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2439 if (TRACE_ON(d3d))
2441 for (i = 0; i < count; ++i)
2442 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2445 if (device->recording)
2447 for (i = start_idx; i < count + start_idx; ++i)
2448 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2450 else
2452 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2455 return WINED3D_OK;
2458 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2459 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2461 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2462 device, start_idx, count, constants);
2464 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2465 return WINED3DERR_INVALIDCALL;
2467 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2468 count = WINED3D_MAX_CONSTS_I - start_idx;
2469 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2470 return WINED3D_OK;
2473 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2474 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2476 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2477 unsigned int i;
2479 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2480 device, start_idx, count, constants);
2482 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2483 || count > d3d_info->limits.vs_uniform_count - start_idx)
2484 return WINED3DERR_INVALIDCALL;
2486 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2487 if (TRACE_ON(d3d))
2489 for (i = 0; i < count; ++i)
2490 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2493 if (device->recording)
2494 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2495 count * sizeof(*device->recording->changed.vs_consts_f));
2496 else
2497 device->shader_backend->shader_update_float_vertex_constants(device, start_idx, count);
2499 return WINED3D_OK;
2502 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2503 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2505 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2507 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2508 device, start_idx, count, constants);
2510 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2511 || count > d3d_info->limits.vs_uniform_count - start_idx)
2512 return WINED3DERR_INVALIDCALL;
2514 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2516 return WINED3D_OK;
2519 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2521 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2523 TRACE("device %p, shader %p.\n", device, shader);
2525 if (device->recording)
2526 device->recording->changed.pixelShader = TRUE;
2528 if (shader == prev)
2529 return;
2531 if (shader)
2532 wined3d_shader_incref(shader);
2533 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2534 if (!device->recording)
2535 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2536 if (prev)
2537 wined3d_shader_decref(prev);
2540 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2542 TRACE("device %p.\n", device);
2544 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2547 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2549 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2551 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2554 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2556 TRACE("device %p, idx %u.\n", device, idx);
2558 if (idx >= MAX_CONSTANT_BUFFERS)
2560 WARN("Invalid constant buffer index %u.\n", idx);
2561 return NULL;
2564 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2567 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2568 UINT idx, struct wined3d_shader_resource_view *view)
2570 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2572 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2575 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2576 UINT idx)
2578 TRACE("device %p, idx %u.\n", device, idx);
2580 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2582 WARN("Invalid view index %u.\n", idx);
2583 return NULL;
2586 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2589 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2591 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2593 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2596 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2598 TRACE("device %p, idx %u.\n", device, idx);
2600 if (idx >= MAX_SAMPLER_OBJECTS)
2602 WARN("Invalid sampler index %u.\n", idx);
2603 return NULL;
2606 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2609 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2610 UINT start_register, const BOOL *constants, UINT bool_count)
2612 UINT count = min(bool_count, WINED3D_MAX_CONSTS_B - start_register);
2613 UINT i;
2615 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2616 device, start_register, constants, bool_count);
2618 if (!constants || start_register >= WINED3D_MAX_CONSTS_B)
2619 return WINED3DERR_INVALIDCALL;
2621 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2622 for (i = 0; i < count; ++i)
2623 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2625 if (device->recording)
2627 for (i = start_register; i < count + start_register; ++i)
2628 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2630 else
2632 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2635 return WINED3D_OK;
2638 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2639 UINT start_register, BOOL *constants, UINT bool_count)
2641 UINT count = min(bool_count, WINED3D_MAX_CONSTS_B - start_register);
2643 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2644 device, start_register, constants, bool_count);
2646 if (!constants || start_register >= WINED3D_MAX_CONSTS_B)
2647 return WINED3DERR_INVALIDCALL;
2649 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2651 return WINED3D_OK;
2654 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2655 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2657 unsigned int i;
2659 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2660 device, start_idx, count, constants);
2662 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2663 return WINED3DERR_INVALIDCALL;
2665 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2666 count = WINED3D_MAX_CONSTS_I - start_idx;
2667 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2668 if (TRACE_ON(d3d))
2670 for (i = 0; i < count; ++i)
2671 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2674 if (device->recording)
2676 for (i = start_idx; i < count + start_idx; ++i)
2677 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2679 else
2681 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2684 return WINED3D_OK;
2687 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2688 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2690 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2691 device, start_idx, count, constants);
2693 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2694 return WINED3DERR_INVALIDCALL;
2696 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2697 count = WINED3D_MAX_CONSTS_I - start_idx;
2698 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2700 return WINED3D_OK;
2703 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2704 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2706 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2707 unsigned int i;
2709 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2710 device, start_idx, count, constants);
2712 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2713 || count > d3d_info->limits.ps_uniform_count - start_idx)
2714 return WINED3DERR_INVALIDCALL;
2716 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2717 if (TRACE_ON(d3d))
2719 for (i = 0; i < count; ++i)
2720 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2723 if (device->recording)
2724 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2725 count * sizeof(*device->recording->changed.ps_consts_f));
2726 else
2727 device->shader_backend->shader_update_float_pixel_constants(device, start_idx, count);
2729 return WINED3D_OK;
2732 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2733 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2735 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2737 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2738 device, start_idx, count, constants);
2740 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2741 || count > d3d_info->limits.ps_uniform_count - start_idx)
2742 return WINED3DERR_INVALIDCALL;
2744 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2746 return WINED3D_OK;
2749 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2751 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2753 TRACE("device %p, shader %p.\n", device, shader);
2755 if (device->recording || shader == prev)
2756 return;
2757 if (shader)
2758 wined3d_shader_incref(shader);
2759 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2760 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2761 if (prev)
2762 wined3d_shader_decref(prev);
2765 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2767 TRACE("device %p.\n", device);
2769 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2772 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2774 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2776 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2779 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2781 TRACE("device %p, idx %u.\n", device, idx);
2783 if (idx >= MAX_CONSTANT_BUFFERS)
2785 WARN("Invalid constant buffer index %u.\n", idx);
2786 return NULL;
2789 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2792 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2793 UINT idx, struct wined3d_shader_resource_view *view)
2795 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2797 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2800 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2801 UINT idx)
2803 TRACE("device %p, idx %u.\n", device, idx);
2805 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2807 WARN("Invalid view index %u.\n", idx);
2808 return NULL;
2811 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2814 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2816 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2818 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2821 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2823 TRACE("device %p, idx %u.\n", device, idx);
2825 if (idx >= MAX_SAMPLER_OBJECTS)
2827 WARN("Invalid sampler index %u.\n", idx);
2828 return NULL;
2831 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2834 /* Context activation is done by the caller. */
2835 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2836 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2837 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2838 DWORD DestFVF)
2840 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2841 struct wined3d_viewport vp;
2842 UINT vertex_size;
2843 unsigned int i;
2844 BYTE *dest_ptr;
2845 BOOL doClip;
2846 DWORD numTextures;
2847 HRESULT hr;
2849 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2851 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2854 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2856 ERR("Source has no position mask\n");
2857 return WINED3DERR_INVALIDCALL;
2860 if (device->state.render_states[WINED3D_RS_CLIPPING])
2862 static BOOL warned = FALSE;
2864 * The clipping code is not quite correct. Some things need
2865 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2866 * so disable clipping for now.
2867 * (The graphics in Half-Life are broken, and my processvertices
2868 * test crashes with IDirect3DDevice3)
2869 doClip = TRUE;
2871 doClip = FALSE;
2872 if(!warned) {
2873 warned = TRUE;
2874 FIXME("Clipping is broken and disabled for now\n");
2877 else
2878 doClip = FALSE;
2880 vertex_size = get_flexible_vertex_size(DestFVF);
2881 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2883 WARN("Failed to map buffer, hr %#x.\n", hr);
2884 return hr;
2887 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2888 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2889 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2891 TRACE("View mat:\n");
2892 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2893 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2894 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2895 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2897 TRACE("Proj mat:\n");
2898 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2899 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2900 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2901 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2903 TRACE("World mat:\n");
2904 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2905 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2906 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2907 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2909 /* Get the viewport */
2910 wined3d_device_get_viewport(device, &vp);
2911 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2912 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2914 multiply_matrix(&mat,&view_mat,&world_mat);
2915 multiply_matrix(&mat,&proj_mat,&mat);
2917 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2919 for (i = 0; i < dwCount; i+= 1) {
2920 unsigned int tex_index;
2922 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2923 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2924 /* The position first */
2925 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2926 const float *p = (const float *)(element->data.addr + i * element->stride);
2927 float x, y, z, rhw;
2928 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2930 /* Multiplication with world, view and projection matrix. */
2931 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2932 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2933 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2934 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2936 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2938 /* WARNING: The following things are taken from d3d7 and were not yet checked
2939 * against d3d8 or d3d9!
2942 /* Clipping conditions: From msdn
2944 * A vertex is clipped if it does not match the following requirements
2945 * -rhw < x <= rhw
2946 * -rhw < y <= rhw
2947 * 0 < z <= rhw
2948 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2950 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2951 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2955 if( !doClip ||
2956 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2957 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2958 ( rhw > eps ) ) ) {
2960 /* "Normal" viewport transformation (not clipped)
2961 * 1) The values are divided by rhw
2962 * 2) The y axis is negative, so multiply it with -1
2963 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2964 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2965 * 4) Multiply x with Width/2 and add Width/2
2966 * 5) The same for the height
2967 * 6) Add the viewpoint X and Y to the 2D coordinates and
2968 * The minimum Z value to z
2969 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2971 * Well, basically it's simply a linear transformation into viewport
2972 * coordinates
2975 x /= rhw;
2976 y /= rhw;
2977 z /= rhw;
2979 y *= -1;
2981 x *= vp.width / 2;
2982 y *= vp.height / 2;
2983 z *= vp.max_z - vp.min_z;
2985 x += vp.width / 2 + vp.x;
2986 y += vp.height / 2 + vp.y;
2987 z += vp.min_z;
2989 rhw = 1 / rhw;
2990 } else {
2991 /* That vertex got clipped
2992 * Contrary to OpenGL it is not dropped completely, it just
2993 * undergoes a different calculation.
2995 TRACE("Vertex got clipped\n");
2996 x += rhw;
2997 y += rhw;
2999 x /= 2;
3000 y /= 2;
3002 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3003 * outside of the main vertex buffer memory. That needs some more
3004 * investigation...
3008 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3011 ( (float *) dest_ptr)[0] = x;
3012 ( (float *) dest_ptr)[1] = y;
3013 ( (float *) dest_ptr)[2] = z;
3014 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3016 dest_ptr += 3 * sizeof(float);
3018 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3019 dest_ptr += sizeof(float);
3022 if (DestFVF & WINED3DFVF_PSIZE)
3023 dest_ptr += sizeof(DWORD);
3025 if (DestFVF & WINED3DFVF_NORMAL)
3027 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3028 const float *normal = (const float *)(element->data.addr + i * element->stride);
3029 /* AFAIK this should go into the lighting information */
3030 FIXME("Didn't expect the destination to have a normal\n");
3031 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3034 if (DestFVF & WINED3DFVF_DIFFUSE)
3036 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3037 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3038 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3040 static BOOL warned = FALSE;
3042 if(!warned) {
3043 ERR("No diffuse color in source, but destination has one\n");
3044 warned = TRUE;
3047 *( (DWORD *) dest_ptr) = 0xffffffff;
3048 dest_ptr += sizeof(DWORD);
3050 else
3052 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3056 if (DestFVF & WINED3DFVF_SPECULAR)
3058 /* What's the color value in the feedback buffer? */
3059 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3060 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3061 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3063 static BOOL warned = FALSE;
3065 if(!warned) {
3066 ERR("No specular color in source, but destination has one\n");
3067 warned = TRUE;
3070 *(DWORD *)dest_ptr = 0xff000000;
3071 dest_ptr += sizeof(DWORD);
3073 else
3075 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3079 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3081 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3082 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3083 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3085 ERR("No source texture, but destination requests one\n");
3086 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3088 else
3090 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3095 wined3d_buffer_unmap(dest);
3097 return WINED3D_OK;
3099 #undef copy_and_next
3101 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3102 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3103 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3105 struct wined3d_state *state = &device->state;
3106 struct wined3d_stream_info stream_info;
3107 const struct wined3d_gl_info *gl_info;
3108 struct wined3d_context *context;
3109 struct wined3d_shader *vs;
3110 unsigned int i;
3111 HRESULT hr;
3112 WORD map;
3114 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3115 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3116 device, src_start_idx, dst_idx, vertex_count,
3117 dst_buffer, declaration, flags, dst_fvf);
3119 if (declaration)
3120 FIXME("Output vertex declaration not implemented yet.\n");
3122 /* Need any context to write to the vbo. */
3123 context = context_acquire(device, NULL);
3124 gl_info = context->gl_info;
3126 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3127 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3128 context_stream_info_from_declaration(context, state, &stream_info);
3129 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3131 /* We can't convert FROM a VBO, and vertex buffers used to source into
3132 * process_vertices() are unlikely to ever be used for drawing. Release
3133 * VBOs in those buffers and fix up the stream_info structure.
3135 * Also apply the start index. */
3136 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3138 struct wined3d_stream_info_element *e;
3139 struct wined3d_buffer *buffer;
3141 if (!(map & 1))
3142 continue;
3144 e = &stream_info.elements[i];
3145 buffer = state->streams[e->stream_idx].buffer;
3146 e->data.buffer_object = 0;
3147 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3148 if (buffer->buffer_object)
3150 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3151 buffer->buffer_object = 0;
3153 if (e->data.addr)
3154 e->data.addr += e->stride * src_start_idx;
3157 hr = process_vertices_strided(device, dst_idx, vertex_count,
3158 &stream_info, dst_buffer, flags, dst_fvf);
3160 context_release(context);
3162 return hr;
3165 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3166 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3168 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3169 DWORD old_value;
3171 TRACE("device %p, stage %u, state %s, value %#x.\n",
3172 device, stage, debug_d3dtexturestate(state), value);
3174 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3176 WARN("Invalid state %#x passed.\n", state);
3177 return;
3180 if (stage >= d3d_info->limits.ffp_blend_stages)
3182 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3183 stage, d3d_info->limits.ffp_blend_stages - 1);
3184 return;
3187 old_value = device->update_state->texture_states[stage][state];
3188 device->update_state->texture_states[stage][state] = value;
3190 if (device->recording)
3192 TRACE("Recording... not performing anything.\n");
3193 device->recording->changed.textureState[stage] |= 1u << state;
3194 return;
3197 /* Checked after the assignments to allow proper stateblock recording. */
3198 if (old_value == value)
3200 TRACE("Application is setting the old value over, nothing to do.\n");
3201 return;
3204 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3207 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3208 UINT stage, enum wined3d_texture_stage_state state)
3210 TRACE("device %p, stage %u, state %s.\n",
3211 device, stage, debug_d3dtexturestate(state));
3213 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3215 WARN("Invalid state %#x passed.\n", state);
3216 return 0;
3219 return device->state.texture_states[stage][state];
3222 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3223 UINT stage, struct wined3d_texture *texture)
3225 struct wined3d_texture *prev;
3227 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3229 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3230 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3232 /* Windows accepts overflowing this array... we do not. */
3233 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3235 WARN("Ignoring invalid stage %u.\n", stage);
3236 return WINED3D_OK;
3239 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3241 WARN("Rejecting attempt to set scratch texture.\n");
3242 return WINED3DERR_INVALIDCALL;
3245 if (device->recording)
3246 device->recording->changed.textures |= 1u << stage;
3248 prev = device->update_state->textures[stage];
3249 TRACE("Previous texture %p.\n", prev);
3251 if (texture == prev)
3253 TRACE("App is setting the same texture again, nothing to do.\n");
3254 return WINED3D_OK;
3257 TRACE("Setting new texture to %p.\n", texture);
3258 device->update_state->textures[stage] = texture;
3260 if (texture)
3261 wined3d_texture_incref(texture);
3262 if (!device->recording)
3263 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3264 if (prev)
3265 wined3d_texture_decref(prev);
3267 return WINED3D_OK;
3270 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3272 TRACE("device %p, stage %u.\n", device, stage);
3274 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3275 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3277 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3279 WARN("Ignoring invalid stage %u.\n", stage);
3280 return NULL; /* Windows accepts overflowing this array ... we do not. */
3283 return device->state.textures[stage];
3286 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3288 TRACE("device %p, caps %p.\n", device, caps);
3290 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3291 device->create_parms.device_type, caps);
3294 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3295 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3297 struct wined3d_swapchain *swapchain;
3299 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3300 device, swapchain_idx, mode, rotation);
3302 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3303 return WINED3DERR_INVALIDCALL;
3305 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3308 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3310 struct wined3d_stateblock *stateblock;
3311 HRESULT hr;
3313 TRACE("device %p.\n", device);
3315 if (device->recording)
3316 return WINED3DERR_INVALIDCALL;
3318 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3319 if (FAILED(hr))
3320 return hr;
3322 device->recording = stateblock;
3323 device->update_state = &stateblock->state;
3325 TRACE("Recording stateblock %p.\n", stateblock);
3327 return WINED3D_OK;
3330 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3331 struct wined3d_stateblock **stateblock)
3333 struct wined3d_stateblock *object = device->recording;
3335 TRACE("device %p, stateblock %p.\n", device, stateblock);
3337 if (!device->recording)
3339 WARN("Not recording.\n");
3340 *stateblock = NULL;
3341 return WINED3DERR_INVALIDCALL;
3344 stateblock_init_contained_states(object);
3346 *stateblock = object;
3347 device->recording = NULL;
3348 device->update_state = &device->state;
3350 TRACE("Returning stateblock %p.\n", *stateblock);
3352 return WINED3D_OK;
3355 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3357 /* At the moment we have no need for any functionality at the beginning
3358 * of a scene. */
3359 TRACE("device %p.\n", device);
3361 if (device->inScene)
3363 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3364 return WINED3DERR_INVALIDCALL;
3366 device->inScene = TRUE;
3367 return WINED3D_OK;
3370 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3372 struct wined3d_context *context;
3374 TRACE("device %p.\n", device);
3376 if (!device->inScene)
3378 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3379 return WINED3DERR_INVALIDCALL;
3382 context = context_acquire(device, NULL);
3383 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3384 context->gl_info->gl_ops.gl.p_glFlush();
3385 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3386 * fails. */
3387 context_release(context);
3389 device->inScene = FALSE;
3390 return WINED3D_OK;
3393 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3394 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3396 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3397 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3399 if (!rect_count && rects)
3401 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3402 return WINED3D_OK;
3405 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3407 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3408 if (!ds)
3410 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3411 /* TODO: What about depth stencil buffers without stencil bits? */
3412 return WINED3DERR_INVALIDCALL;
3414 else if (flags & WINED3DCLEAR_TARGET)
3416 if (ds->width < device->fb.render_targets[0]->width
3417 || ds->height < device->fb.render_targets[0]->height)
3419 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3420 return WINED3D_OK;
3425 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3427 return WINED3D_OK;
3430 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3431 struct wined3d_query *predicate, BOOL value)
3433 struct wined3d_query *prev;
3435 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3437 prev = device->update_state->predicate;
3438 if (predicate)
3440 FIXME("Predicated rendering not implemented.\n");
3441 wined3d_query_incref(predicate);
3443 device->update_state->predicate = predicate;
3444 device->update_state->predicate_value = value;
3445 if (!device->recording)
3446 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3447 if (prev)
3448 wined3d_query_decref(prev);
3451 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3453 TRACE("device %p, value %p.\n", device, value);
3455 *value = device->state.predicate_value;
3456 return device->state.predicate;
3459 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3460 enum wined3d_primitive_type primitive_type)
3462 GLenum gl_primitive_type, prev;
3464 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3466 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3467 prev = device->update_state->gl_primitive_type;
3468 device->update_state->gl_primitive_type = gl_primitive_type;
3469 if (device->recording)
3470 device->recording->changed.primitive_type = TRUE;
3471 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3472 device_invalidate_state(device, STATE_POINT_ENABLE);
3475 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3476 enum wined3d_primitive_type *primitive_type)
3478 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3480 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3482 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3485 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3487 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3489 if (!device->state.vertex_declaration)
3491 WARN("Called without a valid vertex declaration set.\n");
3492 return WINED3DERR_INVALIDCALL;
3495 if (device->state.load_base_vertex_index)
3497 device->state.load_base_vertex_index = 0;
3498 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3501 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3503 return WINED3D_OK;
3506 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3507 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3509 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3510 device, start_vertex, vertex_count, start_instance, instance_count);
3512 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3515 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3517 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3519 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3521 if (!device->state.index_buffer)
3523 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3524 * without an index buffer set. (The first time at least...)
3525 * D3D8 simply dies, but I doubt it can do much harm to return
3526 * D3DERR_INVALIDCALL there as well. */
3527 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3528 return WINED3DERR_INVALIDCALL;
3531 if (!device->state.vertex_declaration)
3533 WARN("Called without a valid vertex declaration set.\n");
3534 return WINED3DERR_INVALIDCALL;
3537 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3538 device->state.load_base_vertex_index != device->state.base_vertex_index)
3540 device->state.load_base_vertex_index = device->state.base_vertex_index;
3541 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3544 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3546 return WINED3D_OK;
3549 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3550 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3552 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3553 device, start_idx, index_count, start_instance, instance_count);
3555 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3558 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3559 struct wined3d_texture *src_texture, unsigned int src_level,
3560 struct wined3d_texture *dst_texture, unsigned int level_count)
3562 struct wined3d_const_bo_address data;
3563 struct wined3d_context *context;
3564 struct wined3d_map_desc src;
3565 HRESULT hr = WINED3D_OK;
3566 unsigned int i;
3568 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3569 device, src_texture, src_level, dst_texture, level_count);
3571 if (src_texture->resource.format != dst_texture->resource.format)
3573 WARN("Source and destination formats do not match.\n");
3574 return WINED3DERR_INVALIDCALL;
3577 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3578 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3579 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3581 WARN("Source and destination dimensions do not match.\n");
3582 return WINED3DERR_INVALIDCALL;
3585 context = context_acquire(device, NULL);
3587 /* Only a prepare, since we're uploading entire volumes. */
3588 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3589 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3591 for (i = 0; i < level_count; ++i)
3593 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3594 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3595 goto done;
3597 data.buffer_object = 0;
3598 data.addr = src.data;
3599 wined3d_volume_upload_data(dst_texture, i, context, &data);
3600 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3602 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3603 goto done;
3606 done:
3607 context_release(context);
3608 return hr;
3611 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3612 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3614 unsigned int src_size, dst_size, src_skip_levels = 0;
3615 unsigned int layer_count, level_count, i, j;
3616 enum wined3d_resource_type type;
3617 HRESULT hr;
3618 struct wined3d_context *context;
3620 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3622 /* Verify that the source and destination textures are non-NULL. */
3623 if (!src_texture || !dst_texture)
3625 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3626 return WINED3DERR_INVALIDCALL;
3629 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3631 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3632 return WINED3DERR_INVALIDCALL;
3634 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3636 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3637 return WINED3DERR_INVALIDCALL;
3640 /* Verify that the source and destination textures are the same type. */
3641 type = src_texture->resource.type;
3642 if (dst_texture->resource.type != type)
3644 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3645 return WINED3DERR_INVALIDCALL;
3648 layer_count = src_texture->layer_count;
3649 if (layer_count != dst_texture->layer_count)
3651 WARN("Source and destination have different layer counts.\n");
3652 return WINED3DERR_INVALIDCALL;
3655 level_count = min(wined3d_texture_get_level_count(src_texture),
3656 wined3d_texture_get_level_count(dst_texture));
3658 src_size = max(src_texture->resource.width, src_texture->resource.height);
3659 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3660 if (type == WINED3D_RTYPE_TEXTURE_3D)
3662 src_size = max(src_size, src_texture->resource.depth);
3663 dst_size = max(dst_size, dst_texture->resource.depth);
3665 while (src_size > dst_size)
3667 src_size >>= 1;
3668 ++src_skip_levels;
3671 /* Make sure that the destination texture is loaded. */
3672 context = context_acquire(device, NULL);
3673 wined3d_texture_load(dst_texture, context, FALSE);
3674 context_release(context);
3676 /* Update every surface level of the texture. */
3677 switch (type)
3679 case WINED3D_RTYPE_TEXTURE_2D:
3681 unsigned int src_levels = src_texture->level_count;
3682 unsigned int dst_levels = dst_texture->level_count;
3683 struct wined3d_surface *src_surface;
3684 struct wined3d_surface *dst_surface;
3686 for (i = 0; i < layer_count; ++i)
3688 for (j = 0; j < level_count; ++j)
3690 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3691 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3692 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3694 WARN("Failed to update surface, hr %#x.\n", hr);
3695 return hr;
3699 return WINED3D_OK;
3702 case WINED3D_RTYPE_TEXTURE_3D:
3703 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3704 src_texture, src_skip_levels, dst_texture, level_count)))
3705 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3706 return hr;
3708 default:
3709 FIXME("Unsupported texture type %#x.\n", type);
3710 return WINED3DERR_INVALIDCALL;
3714 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3716 const struct wined3d_state *state = &device->state;
3717 struct wined3d_texture *texture;
3718 DWORD i;
3720 TRACE("device %p, num_passes %p.\n", device, num_passes);
3722 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3724 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3726 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3727 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3729 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3731 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3732 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3735 texture = state->textures[i];
3736 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3738 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3740 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3741 return E_FAIL;
3743 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3745 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3746 return E_FAIL;
3748 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3749 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3751 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3752 return E_FAIL;
3756 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3757 || state->render_states[WINED3D_RS_STENCILENABLE])
3759 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3760 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3762 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3764 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3765 return WINED3DERR_CONFLICTINGRENDERSTATE;
3769 /* return a sensible default */
3770 *num_passes = 1;
3772 TRACE("returning D3D_OK\n");
3773 return WINED3D_OK;
3776 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3778 static BOOL warned;
3780 TRACE("device %p, software %#x.\n", device, software);
3782 if (!warned)
3784 FIXME("device %p, software %#x stub!\n", device, software);
3785 warned = TRUE;
3788 device->softwareVertexProcessing = software;
3791 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3793 static BOOL warned;
3795 TRACE("device %p.\n", device);
3797 if (!warned)
3799 TRACE("device %p stub!\n", device);
3800 warned = TRUE;
3803 return device->softwareVertexProcessing;
3806 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3807 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3809 struct wined3d_swapchain *swapchain;
3811 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3812 device, swapchain_idx, raster_status);
3814 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3815 return WINED3DERR_INVALIDCALL;
3817 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3820 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3822 static BOOL warned;
3824 TRACE("device %p, segments %.8e.\n", device, segments);
3826 if (segments != 0.0f)
3828 if (!warned)
3830 FIXME("device %p, segments %.8e stub!\n", device, segments);
3831 warned = TRUE;
3835 return WINED3D_OK;
3838 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3840 static BOOL warned;
3842 TRACE("device %p.\n", device);
3844 if (!warned)
3846 FIXME("device %p stub!\n", device);
3847 warned = TRUE;
3850 return 0.0f;
3853 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3854 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3856 struct wined3d_texture *dst_texture, *src_texture;
3857 RECT dst_rect, src_rect;
3858 unsigned int i, j;
3859 HRESULT hr;
3861 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3863 if (src_resource == dst_resource)
3865 WARN("Source and destination are the same resource.\n");
3866 return;
3869 if (src_resource->type != dst_resource->type)
3871 WARN("Resource types (%s / %s) don't match.\n",
3872 debug_d3dresourcetype(dst_resource->type),
3873 debug_d3dresourcetype(src_resource->type));
3874 return;
3877 if (src_resource->width != dst_resource->width
3878 || src_resource->height != dst_resource->height
3879 || src_resource->depth != dst_resource->depth)
3881 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3882 dst_resource->width, dst_resource->height, dst_resource->depth,
3883 src_resource->width, src_resource->height, src_resource->depth);
3884 return;
3887 if (src_resource->format->id != dst_resource->format->id)
3889 WARN("Resource formats (%s / %s) don't match.\n",
3890 debug_d3dformat(dst_resource->format->id),
3891 debug_d3dformat(src_resource->format->id));
3892 return;
3895 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3897 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3898 buffer_from_resource(src_resource), 0,
3899 dst_resource->size)))
3900 ERR("Failed to copy buffer, hr %#x.\n", hr);
3901 return;
3904 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3906 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3907 return;
3910 dst_texture = texture_from_resource(dst_resource);
3911 src_texture = texture_from_resource(src_resource);
3913 if (src_texture->layer_count != dst_texture->layer_count
3914 || src_texture->level_count != dst_texture->level_count)
3916 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3917 dst_texture->layer_count, dst_texture->level_count,
3918 src_texture->layer_count, src_texture->level_count);
3919 return;
3922 for (i = 0; i < dst_texture->level_count; ++i)
3924 SetRect(&dst_rect, 0, 0,
3925 wined3d_texture_get_level_width(dst_texture, i),
3926 wined3d_texture_get_level_height(dst_texture, i));
3927 SetRect(&src_rect, 0, 0,
3928 wined3d_texture_get_level_width(src_texture, i),
3929 wined3d_texture_get_level_height(dst_texture, i));
3930 for (j = 0; j < dst_texture->layer_count; ++j)
3932 unsigned int idx = j * dst_texture->level_count + i;
3934 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3935 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3936 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3941 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3942 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3943 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3944 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3946 struct wined3d_texture *dst_texture, *src_texture;
3947 RECT dst_rect, src_rect;
3948 HRESULT hr;
3950 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3951 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3952 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3953 src_resource, src_sub_resource_idx, debug_box(src_box));
3955 if (src_box && (src_box->left >= src_box->right
3956 || src_box->top >= src_box->bottom
3957 || src_box->front >= src_box->back))
3959 WARN("Invalid box %s specified.\n", debug_box(src_box));
3960 return WINED3DERR_INVALIDCALL;
3963 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3965 WARN("Source and destination are the same sub-resource.\n");
3966 return WINED3DERR_INVALIDCALL;
3969 if (src_resource->type != dst_resource->type)
3971 WARN("Resource types (%s / %s) don't match.\n",
3972 debug_d3dresourcetype(dst_resource->type),
3973 debug_d3dresourcetype(src_resource->type));
3974 return WINED3DERR_INVALIDCALL;
3977 if (src_resource->format->id != dst_resource->format->id)
3979 WARN("Resource formats (%s / %s) don't match.\n",
3980 debug_d3dformat(dst_resource->format->id),
3981 debug_d3dformat(src_resource->format->id));
3982 return WINED3DERR_INVALIDCALL;
3985 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3987 unsigned int src_offset, size;
3989 if (dst_sub_resource_idx)
3991 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3992 return WINED3DERR_INVALIDCALL;
3994 if (src_sub_resource_idx)
3996 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3997 return WINED3DERR_INVALIDCALL;
4000 if (src_box)
4002 src_offset = src_box->left;
4003 size = src_box->right - src_box->left;
4005 else
4007 src_offset = 0;
4008 size = src_resource->size;
4011 if (src_offset > src_resource->size
4012 || size > src_resource->size - src_offset
4013 || dst_x > dst_resource->size
4014 || size > dst_resource->size - dst_x)
4016 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4017 dst_x, src_offset, size);
4018 return WINED3DERR_INVALIDCALL;
4021 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4022 buffer_from_resource(src_resource), src_offset, size);
4025 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4027 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4028 return WINED3DERR_INVALIDCALL;
4031 dst_texture = texture_from_resource(dst_resource);
4032 src_texture = texture_from_resource(src_resource);
4034 if (src_box)
4036 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4038 else
4040 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4042 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4043 wined3d_texture_get_level_height(src_texture, level));
4046 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4047 dst_y + (src_rect.bottom - src_rect.top));
4049 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4050 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4051 WARN("Failed to blit, hr %#x.\n", hr);
4053 return hr;
4056 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4057 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4058 unsigned int depth_pitch)
4060 struct wined3d_texture_sub_resource *sub_resource;
4061 const struct wined3d_gl_info *gl_info;
4062 struct wined3d_const_bo_address addr;
4063 unsigned int width, height, level;
4064 struct wined3d_context *context;
4065 struct wined3d_texture *texture;
4066 struct wined3d_surface *surface;
4067 POINT dst_point;
4068 RECT src_rect;
4070 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4071 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4073 if (resource->type == WINED3D_RTYPE_BUFFER)
4075 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4076 HRESULT hr;
4078 if (sub_resource_idx > 0)
4080 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4081 return;
4084 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4085 WARN("Failed to update buffer data, hr %#x.\n", hr);
4087 return;
4090 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4092 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4093 return;
4096 texture = texture_from_resource(resource);
4097 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4099 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4100 return;
4102 surface = sub_resource->u.surface;
4104 level = sub_resource_idx % texture->level_count;
4105 width = wined3d_texture_get_level_width(texture, level);
4106 height = wined3d_texture_get_level_height(texture, level);
4108 src_rect.left = 0;
4109 src_rect.top = 0;
4110 if (box)
4112 if (box->left >= box->right || box->right > width
4113 || box->top >= box->bottom || box->bottom > height
4114 || box->front >= box->back)
4116 WARN("Invalid box %s specified.\n", debug_box(box));
4117 return;
4120 src_rect.right = box->right - box->left;
4121 src_rect.bottom = box->bottom - box->top;
4122 dst_point.x = box->left;
4123 dst_point.y = box->top;
4125 else
4127 src_rect.right = width;
4128 src_rect.bottom = height;
4129 dst_point.x = 0;
4130 dst_point.y = 0;
4133 addr.buffer_object = 0;
4134 addr.addr = data;
4136 context = context_acquire(resource->device, NULL);
4137 gl_info = context->gl_info;
4139 /* Only load the surface for partial updates. */
4140 if (!dst_point.x && !dst_point.y && src_rect.right == width && src_rect.bottom == height)
4141 wined3d_texture_prepare_texture(texture, context, FALSE);
4142 else
4143 surface_load_location(surface, context, WINED3D_LOCATION_TEXTURE_RGB);
4144 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4146 wined3d_surface_upload_data(surface, gl_info, resource->format,
4147 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4149 context_release(context);
4151 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4152 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4155 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4156 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4157 const struct wined3d_color *color, float depth, DWORD stencil)
4159 const struct blit_shader *blitter;
4160 struct wined3d_resource *resource;
4161 enum wined3d_blit_op blit_op;
4162 RECT r;
4164 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4165 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4167 if (!flags)
4168 return WINED3D_OK;
4170 resource = view->resource;
4171 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4173 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4174 return WINED3DERR_INVALIDCALL;
4177 if (view->depth > 1)
4179 FIXME("Layered clears not implemented.\n");
4180 return WINED3DERR_INVALIDCALL;
4183 if (!rect)
4185 SetRect(&r, 0, 0, view->width, view->height);
4186 rect = &r;
4189 if (flags & WINED3DCLEAR_TARGET)
4190 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4191 else
4192 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4194 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4195 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4197 FIXME("No blitter is capable of performing the requested fill operation.\n");
4198 return WINED3DERR_INVALIDCALL;
4201 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4202 return blitter->color_fill(device, view, rect, color);
4203 else
4204 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4207 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4208 unsigned int view_idx)
4210 TRACE("device %p, view_idx %u.\n", device, view_idx);
4212 if (view_idx >= device->adapter->gl_info.limits.buffers)
4214 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4215 return NULL;
4218 return device->fb.render_targets[view_idx];
4221 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4223 TRACE("device %p.\n", device);
4225 return device->fb.depth_stencil;
4228 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4229 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4231 struct wined3d_rendertarget_view *prev;
4233 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4234 device, view_idx, view, set_viewport);
4236 if (view_idx >= device->adapter->gl_info.limits.buffers)
4238 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4239 return WINED3DERR_INVALIDCALL;
4242 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4244 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4245 return WINED3DERR_INVALIDCALL;
4248 /* Set the viewport and scissor rectangles, if requested. Tests show that
4249 * stateblock recording is ignored, the change goes directly into the
4250 * primary stateblock. */
4251 if (!view_idx && set_viewport)
4253 struct wined3d_state *state = &device->state;
4255 state->viewport.x = 0;
4256 state->viewport.y = 0;
4257 state->viewport.width = view->width;
4258 state->viewport.height = view->height;
4259 state->viewport.min_z = 0.0f;
4260 state->viewport.max_z = 1.0f;
4261 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4263 state->scissor_rect.top = 0;
4264 state->scissor_rect.left = 0;
4265 state->scissor_rect.right = view->width;
4266 state->scissor_rect.bottom = view->height;
4267 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4271 prev = device->fb.render_targets[view_idx];
4272 if (view == prev)
4273 return WINED3D_OK;
4275 if (view)
4276 wined3d_rendertarget_view_incref(view);
4277 device->fb.render_targets[view_idx] = view;
4278 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4279 /* Release after the assignment, to prevent device_resource_released()
4280 * from seeing the surface as still in use. */
4281 if (prev)
4282 wined3d_rendertarget_view_decref(prev);
4284 return WINED3D_OK;
4287 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4289 struct wined3d_rendertarget_view *prev;
4291 TRACE("device %p, view %p.\n", device, view);
4293 prev = device->fb.depth_stencil;
4294 if (prev == view)
4296 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4297 return;
4300 if ((device->fb.depth_stencil = view))
4301 wined3d_rendertarget_view_incref(view);
4302 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4303 if (prev)
4304 wined3d_rendertarget_view_decref(prev);
4307 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4308 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4310 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4311 struct wined3d_sub_resource_data data;
4312 struct wined3d_resource_desc desc;
4313 struct wined3d_map_desc map_desc;
4314 struct wined3d_texture *texture;
4315 HRESULT hr;
4317 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4319 ERR("Failed to map source texture.\n");
4320 return NULL;
4323 data.data = map_desc.data;
4324 data.row_pitch = map_desc.row_pitch;
4325 data.slice_pitch = map_desc.slice_pitch;
4327 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4328 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4329 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4330 desc.multisample_quality = 0;
4331 desc.usage = WINED3DUSAGE_DYNAMIC;
4332 desc.pool = WINED3D_POOL_DEFAULT;
4333 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4334 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4335 desc.depth = 1;
4336 desc.size = 0;
4338 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4339 &data, NULL, &wined3d_null_parent_ops, &texture);
4340 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4341 if (FAILED(hr))
4343 ERR("Failed to create cursor texture.\n");
4344 return NULL;
4347 return texture;
4350 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4351 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4353 unsigned int texture_level = sub_resource_idx % texture->level_count;
4354 unsigned int cursor_width, cursor_height;
4355 struct wined3d_display_mode mode;
4356 struct wined3d_map_desc map_desc;
4357 HRESULT hr;
4359 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4360 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4362 if (sub_resource_idx >= texture->level_count * texture->layer_count
4363 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4364 return WINED3DERR_INVALIDCALL;
4366 if (device->cursor_texture)
4368 wined3d_texture_decref(device->cursor_texture);
4369 device->cursor_texture = NULL;
4372 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4374 WARN("Texture %p has invalid format %s.\n",
4375 texture, debug_d3dformat(texture->resource.format->id));
4376 return WINED3DERR_INVALIDCALL;
4379 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4381 ERR("Failed to get display mode, hr %#x.\n", hr);
4382 return WINED3DERR_INVALIDCALL;
4385 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4386 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4387 if (cursor_width > mode.width || cursor_height > mode.height)
4389 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4390 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4391 return WINED3DERR_INVALIDCALL;
4394 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4396 /* Do not store the surface's pointer because the application may
4397 * release it after setting the cursor image. Windows doesn't
4398 * addref the set surface, so we can't do this either without
4399 * creating circular refcount dependencies. */
4400 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4402 ERR("Failed to create cursor texture.\n");
4403 return WINED3DERR_INVALIDCALL;
4406 if (cursor_width == 32 && cursor_height == 32)
4408 UINT mask_size = cursor_width * cursor_height / 8;
4409 ICONINFO cursor_info;
4410 DWORD *mask_bits;
4411 HCURSOR cursor;
4413 /* 32-bit user32 cursors ignore the alpha channel if it's all
4414 * zeroes, and use the mask instead. Fill the mask with all ones
4415 * to ensure we still get a fully transparent cursor. */
4416 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4417 return E_OUTOFMEMORY;
4418 memset(mask_bits, 0xff, mask_size);
4420 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4421 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4422 cursor_info.fIcon = FALSE;
4423 cursor_info.xHotspot = x_hotspot;
4424 cursor_info.yHotspot = y_hotspot;
4425 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4426 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4427 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4429 /* Create our cursor and clean up. */
4430 cursor = CreateIconIndirect(&cursor_info);
4431 if (cursor_info.hbmMask)
4432 DeleteObject(cursor_info.hbmMask);
4433 if (cursor_info.hbmColor)
4434 DeleteObject(cursor_info.hbmColor);
4435 if (device->hardwareCursor)
4436 DestroyCursor(device->hardwareCursor);
4437 device->hardwareCursor = cursor;
4438 if (device->bCursorVisible)
4439 SetCursor(cursor);
4441 HeapFree(GetProcessHeap(), 0, mask_bits);
4444 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4445 device->cursorWidth = cursor_width;
4446 device->cursorHeight = cursor_height;
4447 device->xHotSpot = x_hotspot;
4448 device->yHotSpot = y_hotspot;
4450 return WINED3D_OK;
4453 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4454 int x_screen_space, int y_screen_space, DWORD flags)
4456 TRACE("device %p, x %d, y %d, flags %#x.\n",
4457 device, x_screen_space, y_screen_space, flags);
4459 device->xScreenSpace = x_screen_space;
4460 device->yScreenSpace = y_screen_space;
4462 if (device->hardwareCursor)
4464 POINT pt;
4466 GetCursorPos( &pt );
4467 if (x_screen_space == pt.x && y_screen_space == pt.y)
4468 return;
4469 SetCursorPos( x_screen_space, y_screen_space );
4471 /* Switch to the software cursor if position diverges from the hardware one. */
4472 GetCursorPos( &pt );
4473 if (x_screen_space != pt.x || y_screen_space != pt.y)
4475 if (device->bCursorVisible) SetCursor( NULL );
4476 DestroyCursor( device->hardwareCursor );
4477 device->hardwareCursor = 0;
4482 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4484 BOOL oldVisible = device->bCursorVisible;
4486 TRACE("device %p, show %#x.\n", device, show);
4489 * When ShowCursor is first called it should make the cursor appear at the OS's last
4490 * known cursor position.
4492 if (show && !oldVisible)
4494 POINT pt;
4495 GetCursorPos(&pt);
4496 device->xScreenSpace = pt.x;
4497 device->yScreenSpace = pt.y;
4500 if (device->hardwareCursor)
4502 device->bCursorVisible = show;
4503 if (show)
4504 SetCursor(device->hardwareCursor);
4505 else
4506 SetCursor(NULL);
4508 else if (device->cursor_texture)
4510 device->bCursorVisible = show;
4513 return oldVisible;
4516 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4518 struct wined3d_resource *resource, *cursor;
4520 TRACE("device %p.\n", device);
4522 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4524 TRACE("Checking resource %p for eviction.\n", resource);
4526 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4528 TRACE("Evicting %p.\n", resource);
4529 resource->resource_ops->resource_unload(resource);
4533 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4534 device_invalidate_state(device, STATE_STREAMSRC);
4537 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4539 struct wined3d_resource *resource, *cursor;
4540 const struct wined3d_gl_info *gl_info;
4541 struct wined3d_context *context;
4542 struct wined3d_shader *shader;
4544 context = context_acquire(device, NULL);
4545 gl_info = context->gl_info;
4547 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4549 TRACE("Unloading resource %p.\n", resource);
4550 resource->resource_ops->resource_unload(resource);
4553 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4555 device->shader_backend->shader_destroy(shader);
4558 if (device->depth_blt_texture)
4560 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4561 device->depth_blt_texture = 0;
4564 device->blitter->free_private(device);
4565 device->shader_backend->shader_free_private(device);
4566 destroy_dummy_textures(device, gl_info);
4567 destroy_default_sampler(device);
4569 context_release(context);
4571 while (device->context_count)
4573 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4576 HeapFree(GetProcessHeap(), 0, swapchain->context);
4577 swapchain->context = NULL;
4580 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4582 struct wined3d_context *context;
4583 struct wined3d_texture *target;
4584 HRESULT hr;
4586 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4587 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4589 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4590 return hr;
4593 if (FAILED(hr = device->blitter->alloc_private(device)))
4595 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4596 device->shader_backend->shader_free_private(device);
4597 return hr;
4600 /* Recreate the primary swapchain's context */
4601 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4602 if (!swapchain->context)
4604 ERR("Failed to allocate memory for swapchain context array.\n");
4605 device->blitter->free_private(device);
4606 device->shader_backend->shader_free_private(device);
4607 return E_OUTOFMEMORY;
4610 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4611 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4613 WARN("Failed to create context.\n");
4614 device->blitter->free_private(device);
4615 device->shader_backend->shader_free_private(device);
4616 HeapFree(GetProcessHeap(), 0, swapchain->context);
4617 return E_FAIL;
4620 swapchain->context[0] = context;
4621 swapchain->num_contexts = 1;
4622 create_dummy_textures(device, context);
4623 create_default_sampler(device);
4624 context_release(context);
4626 return WINED3D_OK;
4629 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4630 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4631 wined3d_device_reset_cb callback, BOOL reset_state)
4633 struct wined3d_rendertarget_view_desc view_desc;
4634 struct wined3d_resource *resource, *cursor;
4635 struct wined3d_swapchain *swapchain;
4636 struct wined3d_display_mode m;
4637 BOOL DisplayModeChanged;
4638 HRESULT hr = WINED3D_OK;
4639 unsigned int i;
4641 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4642 device, swapchain_desc, mode, callback, reset_state);
4644 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4646 ERR("Failed to get the first implicit swapchain.\n");
4647 return WINED3DERR_INVALIDCALL;
4649 DisplayModeChanged = swapchain->reapply_mode;
4651 if (reset_state)
4653 if (device->logo_texture)
4655 wined3d_texture_decref(device->logo_texture);
4656 device->logo_texture = NULL;
4658 if (device->cursor_texture)
4660 wined3d_texture_decref(device->cursor_texture);
4661 device->cursor_texture = NULL;
4663 state_unbind_resources(&device->state);
4666 if (device->fb.render_targets)
4668 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4670 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4673 wined3d_device_set_depth_stencil_view(device, NULL);
4675 if (device->onscreen_depth_stencil)
4677 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4678 device->onscreen_depth_stencil = NULL;
4681 if (reset_state)
4683 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4685 TRACE("Enumerating resource %p.\n", resource);
4686 if (FAILED(hr = callback(resource)))
4687 return hr;
4691 TRACE("New params:\n");
4692 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4693 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4694 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4695 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4696 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4697 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4698 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4699 TRACE("device_window %p\n", swapchain_desc->device_window);
4700 TRACE("windowed %#x\n", swapchain_desc->windowed);
4701 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4702 if (swapchain_desc->enable_auto_depth_stencil)
4703 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4704 TRACE("flags %#x\n", swapchain_desc->flags);
4705 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4706 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4707 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4709 /* No special treatment of these parameters. Just store them */
4710 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4711 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4712 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4713 swapchain->desc.flags = swapchain_desc->flags;
4714 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4715 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4716 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4718 if (swapchain_desc->device_window
4719 && swapchain_desc->device_window != swapchain->desc.device_window)
4721 TRACE("Changing the device window from %p to %p.\n",
4722 swapchain->desc.device_window, swapchain_desc->device_window);
4723 swapchain->desc.device_window = swapchain_desc->device_window;
4724 swapchain->device_window = swapchain_desc->device_window;
4725 wined3d_swapchain_set_window(swapchain, NULL);
4728 if (mode)
4730 DisplayModeChanged = TRUE;
4731 m = *mode;
4733 else if (swapchain_desc->windowed)
4735 m = swapchain->original_mode;
4737 else
4739 m.width = swapchain_desc->backbuffer_width;
4740 m.height = swapchain_desc->backbuffer_height;
4741 m.refresh_rate = swapchain_desc->refresh_rate;
4742 m.format_id = swapchain_desc->backbuffer_format;
4743 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4745 if ((m.width != swapchain->desc.backbuffer_width
4746 || m.height != swapchain->desc.backbuffer_height))
4747 DisplayModeChanged = TRUE;
4750 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4751 || DisplayModeChanged)
4753 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4755 WARN("Failed to set display mode, hr %#x.\n", hr);
4756 return WINED3DERR_INVALIDCALL;
4759 if (!swapchain_desc->windowed)
4761 if (swapchain->desc.windowed)
4763 HWND focus_window = device->create_parms.focus_window;
4764 if (!focus_window)
4765 focus_window = swapchain_desc->device_window;
4766 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4768 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4769 return hr;
4772 /* switch from windowed to fs */
4773 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4774 swapchain_desc->backbuffer_width,
4775 swapchain_desc->backbuffer_height);
4777 else
4779 /* Fullscreen -> fullscreen mode change */
4780 MoveWindow(swapchain->device_window, 0, 0,
4781 swapchain_desc->backbuffer_width,
4782 swapchain_desc->backbuffer_height,
4783 TRUE);
4785 swapchain->d3d_mode = m;
4787 else if (!swapchain->desc.windowed)
4789 /* Fullscreen -> windowed switch */
4790 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4791 wined3d_device_release_focus_window(device);
4793 swapchain->desc.windowed = swapchain_desc->windowed;
4795 else if (!swapchain_desc->windowed)
4797 DWORD style = device->style;
4798 DWORD exStyle = device->exStyle;
4799 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4800 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4801 * Reset to clear up their mess. Guild Wars also loses the device during that.
4803 device->style = 0;
4804 device->exStyle = 0;
4805 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4806 swapchain_desc->backbuffer_width,
4807 swapchain_desc->backbuffer_height);
4808 device->style = style;
4809 device->exStyle = exStyle;
4812 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4813 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4814 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4815 return hr;
4817 if (device->auto_depth_stencil_view)
4819 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4820 device->auto_depth_stencil_view = NULL;
4822 if (swapchain->desc.enable_auto_depth_stencil)
4824 struct wined3d_resource_desc texture_desc;
4825 struct wined3d_texture *texture;
4827 TRACE("Creating the depth stencil buffer\n");
4829 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4830 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4831 texture_desc.multisample_type = swapchain->desc.multisample_type;
4832 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4833 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4834 texture_desc.pool = WINED3D_POOL_DEFAULT;
4835 texture_desc.width = swapchain->desc.backbuffer_width;
4836 texture_desc.height = swapchain->desc.backbuffer_height;
4837 texture_desc.depth = 1;
4838 texture_desc.size = 0;
4840 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4841 device->device_parent, &texture_desc, &texture)))
4843 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4844 return WINED3DERR_INVALIDCALL;
4847 view_desc.format_id = texture->resource.format->id;
4848 view_desc.u.texture.level_idx = 0;
4849 view_desc.u.texture.layer_idx = 0;
4850 view_desc.u.texture.layer_count = 1;
4851 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4852 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4853 wined3d_texture_decref(texture);
4854 if (FAILED(hr))
4856 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4857 return hr;
4860 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4863 if (device->back_buffer_view)
4865 wined3d_rendertarget_view_decref(device->back_buffer_view);
4866 device->back_buffer_view = NULL;
4868 if (swapchain->desc.backbuffer_count)
4870 view_desc.format_id = swapchain_desc->backbuffer_format;
4871 view_desc.u.texture.level_idx = 0;
4872 view_desc.u.texture.layer_idx = 0;
4873 view_desc.u.texture.layer_count = 1;
4874 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
4875 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4877 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4878 return hr;
4882 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4884 if (reset_state)
4886 TRACE("Resetting stateblock.\n");
4887 if (device->recording)
4889 wined3d_stateblock_decref(device->recording);
4890 device->recording = NULL;
4892 wined3d_cs_emit_reset_state(device->cs);
4893 state_cleanup(&device->state);
4895 if (device->d3d_initialized)
4896 delete_opengl_contexts(device, swapchain);
4898 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4899 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4900 device->update_state = &device->state;
4902 device_init_swapchain_state(device, swapchain);
4904 else if (device->back_buffer_view)
4906 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4907 struct wined3d_state *state = &device->state;
4909 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4911 /* Note the min_z / max_z is not reset. */
4912 state->viewport.x = 0;
4913 state->viewport.y = 0;
4914 state->viewport.width = view->width;
4915 state->viewport.height = view->height;
4916 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4918 state->scissor_rect.top = 0;
4919 state->scissor_rect.left = 0;
4920 state->scissor_rect.right = view->width;
4921 state->scissor_rect.bottom = view->height;
4922 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4925 if (device->d3d_initialized)
4927 if (reset_state)
4928 hr = create_primary_opengl_context(device, swapchain);
4929 swapchain_update_swap_interval(swapchain);
4932 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4933 * first use
4935 return hr;
4938 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4940 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4942 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4944 return WINED3D_OK;
4948 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4949 struct wined3d_device_creation_parameters *parameters)
4951 TRACE("device %p, parameters %p.\n", device, parameters);
4953 *parameters = device->create_parms;
4956 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4958 TRACE("device %p.\n", device);
4960 return device->wined3d;
4963 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4964 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4966 struct wined3d_swapchain *swapchain;
4968 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4969 device, swapchain_idx, flags, ramp);
4971 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4972 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4975 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4976 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4978 struct wined3d_swapchain *swapchain;
4980 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4981 device, swapchain_idx, ramp);
4983 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4984 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4987 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4989 TRACE("device %p, resource %p.\n", device, resource);
4991 list_add_head(&device->resources, &resource->resource_list_entry);
4994 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4996 TRACE("device %p, resource %p.\n", device, resource);
4998 list_remove(&resource->resource_list_entry);
5001 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5003 enum wined3d_resource_type type = resource->type;
5004 struct wined3d_rendertarget_view *rtv;
5005 unsigned int i;
5007 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5009 context_resource_released(device, resource, type);
5011 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5013 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
5014 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5017 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5018 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5020 switch (type)
5022 case WINED3D_RTYPE_TEXTURE_2D:
5023 case WINED3D_RTYPE_TEXTURE_3D:
5024 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5026 struct wined3d_texture *texture = texture_from_resource(resource);
5028 if (device->state.textures[i] == texture)
5030 ERR("Texture %p is still in use, stage %u.\n", texture, i);
5031 device->state.textures[i] = NULL;
5034 if (device->recording && device->update_state->textures[i] == texture)
5036 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
5037 texture, device->recording, i);
5038 device->update_state->textures[i] = NULL;
5041 break;
5043 case WINED3D_RTYPE_BUFFER:
5045 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5047 for (i = 0; i < MAX_STREAMS; ++i)
5049 if (device->state.streams[i].buffer == buffer)
5051 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
5052 device->state.streams[i].buffer = NULL;
5055 if (device->recording && device->update_state->streams[i].buffer == buffer)
5057 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5058 buffer, device->recording, i);
5059 device->update_state->streams[i].buffer = NULL;
5063 if (device->state.index_buffer == buffer)
5065 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5066 device->state.index_buffer = NULL;
5069 if (device->recording && device->update_state->index_buffer == buffer)
5071 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5072 buffer, device->recording);
5073 device->update_state->index_buffer = NULL;
5076 break;
5078 default:
5079 break;
5082 /* Remove the resource from the resourceStore */
5083 device_resource_remove(device, resource);
5085 TRACE("Resource released.\n");
5088 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5090 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5092 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5095 static const struct wine_rb_functions wined3d_sampler_rb_functions =
5097 wined3d_rb_alloc,
5098 wined3d_rb_realloc,
5099 wined3d_rb_free,
5100 wined3d_sampler_compare,
5103 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5104 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5105 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5107 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5108 const struct fragment_pipeline *fragment_pipeline;
5109 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5110 unsigned int i;
5111 HRESULT hr;
5113 device->ref = 1;
5114 device->wined3d = wined3d;
5115 wined3d_incref(device->wined3d);
5116 device->adapter = wined3d->adapter_count ? adapter : NULL;
5117 device->device_parent = device_parent;
5118 list_init(&device->resources);
5119 list_init(&device->shaders);
5120 device->surface_alignment = surface_alignment;
5122 /* Save the creation parameters. */
5123 device->create_parms.adapter_idx = adapter_idx;
5124 device->create_parms.device_type = device_type;
5125 device->create_parms.focus_window = focus_window;
5126 device->create_parms.flags = flags;
5128 device->shader_backend = adapter->shader_backend;
5130 vertex_pipeline = adapter->vertex_pipe;
5132 fragment_pipeline = adapter->fragment_pipe;
5134 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
5136 ERR("Failed to initialize sampler rbtree.\n");
5137 return E_OUTOFMEMORY;
5140 if (vertex_pipeline->vp_states && fragment_pipeline->states
5141 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5142 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5143 fragment_pipeline, misc_state_template)))
5145 ERR("Failed to compile state table, hr %#x.\n", hr);
5146 wine_rb_destroy(&device->samplers, NULL, NULL);
5147 wined3d_decref(device->wined3d);
5148 return hr;
5151 device->blitter = adapter->blitter;
5153 state_init(&device->state, &device->fb, &adapter->gl_info,
5154 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5155 device->update_state = &device->state;
5157 if (!(device->cs = wined3d_cs_create(device)))
5159 WARN("Failed to create command stream.\n");
5160 state_cleanup(&device->state);
5161 hr = E_FAIL;
5162 goto err;
5165 return WINED3D_OK;
5167 err:
5168 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5170 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5172 wine_rb_destroy(&device->samplers, NULL, NULL);
5173 wined3d_decref(device->wined3d);
5174 return hr;
5178 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5180 DWORD rep = device->StateTable[state].representative;
5181 struct wined3d_context *context;
5182 DWORD idx;
5183 BYTE shift;
5184 UINT i;
5186 for (i = 0; i < device->context_count; ++i)
5188 context = device->contexts[i];
5189 if(isStateDirty(context, rep)) continue;
5191 context->dirtyArray[context->numDirtyEntries++] = rep;
5192 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5193 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5194 context->isStateDirty[idx] |= (1u << shift);
5198 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5199 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5201 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5203 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5204 window, message, wparam, lparam);
5205 if (unicode)
5206 return DefWindowProcW(window, message, wparam, lparam);
5207 else
5208 return DefWindowProcA(window, message, wparam, lparam);
5211 if (message == WM_DESTROY)
5213 TRACE("unregister window %p.\n", window);
5214 wined3d_unregister_window(window);
5216 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5217 ERR("Window %p is not the focus window for device %p.\n", window, device);
5219 else if (message == WM_DISPLAYCHANGE)
5221 device->device_parent->ops->mode_changed(device->device_parent);
5223 else if (message == WM_ACTIVATEAPP)
5225 UINT i;
5227 for (i = 0; i < device->swapchain_count; i++)
5228 wined3d_swapchain_activate(device->swapchains[i], wparam);
5230 device->device_parent->ops->activate(device->device_parent, wparam);
5232 else if (message == WM_SYSCOMMAND)
5234 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5236 if (unicode)
5237 DefWindowProcW(window, message, wparam, lparam);
5238 else
5239 DefWindowProcA(window, message, wparam, lparam);
5243 if (unicode)
5244 return CallWindowProcW(proc, window, message, wparam, lparam);
5245 else
5246 return CallWindowProcA(proc, window, message, wparam, lparam);