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