wininet: Added support for INTERNET_COOKIE_HTTPONLY flag to InternetSetCookieEx.
[wine/multimedia.git] / dlls / wined3d / device.c
blob65f4ad78229fc0fcc0d8a54605e9e913a76c4a44
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 return GL_NONE;
97 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
99 switch(primitive_type)
101 case GL_POINTS:
102 return WINED3D_PT_POINTLIST;
104 case GL_LINES:
105 return WINED3D_PT_LINELIST;
107 case GL_LINE_STRIP:
108 return WINED3D_PT_LINESTRIP;
110 case GL_TRIANGLES:
111 return WINED3D_PT_TRIANGLELIST;
113 case GL_TRIANGLE_STRIP:
114 return WINED3D_PT_TRIANGLESTRIP;
116 case GL_TRIANGLE_FAN:
117 return WINED3D_PT_TRIANGLEFAN;
119 case GL_LINES_ADJACENCY_ARB:
120 return WINED3D_PT_LINELIST_ADJ;
122 case GL_LINE_STRIP_ADJACENCY_ARB:
123 return WINED3D_PT_LINESTRIP_ADJ;
125 case GL_TRIANGLES_ADJACENCY_ARB:
126 return WINED3D_PT_TRIANGLELIST_ADJ;
128 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
129 return WINED3D_PT_TRIANGLESTRIP_ADJ;
131 default:
132 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
133 return WINED3D_PT_UNDEFINED;
137 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
139 struct wined3d_context **new_array;
141 TRACE("Adding context %p.\n", context);
143 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
144 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
145 sizeof(*new_array) * (device->context_count + 1));
147 if (!new_array)
149 ERR("Failed to grow the context array.\n");
150 return FALSE;
153 new_array[device->context_count++] = context;
154 device->contexts = new_array;
155 return TRUE;
158 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
160 struct wined3d_context **new_array;
161 BOOL found = FALSE;
162 UINT i;
164 TRACE("Removing context %p.\n", context);
166 for (i = 0; i < device->context_count; ++i)
168 if (device->contexts[i] == context)
170 found = TRUE;
171 break;
175 if (!found)
177 ERR("Context %p doesn't exist in context array.\n", context);
178 return;
181 if (!--device->context_count)
183 HeapFree(GetProcessHeap(), 0, device->contexts);
184 device->contexts = NULL;
185 return;
188 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
189 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
190 if (!new_array)
192 ERR("Failed to shrink context array. Oh well.\n");
193 return;
196 device->contexts = new_array;
199 void device_switch_onscreen_ds(struct wined3d_device *device,
200 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
202 if (device->onscreen_depth_stencil)
204 surface_load_ds_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
206 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
207 device->onscreen_depth_stencil->ds_current_size.cx,
208 device->onscreen_depth_stencil->ds_current_size.cy);
209 wined3d_surface_decref(device->onscreen_depth_stencil);
211 device->onscreen_depth_stencil = depth_stencil;
212 wined3d_surface_incref(device->onscreen_depth_stencil);
215 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
217 /* partial draw rect */
218 if (draw_rect->left || draw_rect->top
219 || draw_rect->right < target->resource.width
220 || draw_rect->bottom < target->resource.height)
221 return FALSE;
223 /* partial clear rect */
224 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
225 || clear_rect->right < target->resource.width
226 || clear_rect->bottom < target->resource.height))
227 return FALSE;
229 return TRUE;
232 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
233 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
235 RECT current_rect, r;
237 if (ds->locations & WINED3D_LOCATION_DISCARDED)
239 /* Depth buffer was discarded, make it entirely current in its new location since
240 * there is no other place where we would get data anyway. */
241 SetRect(out_rect, 0, 0, ds->resource.width, ds->resource.height);
242 return;
245 if (ds->locations & location)
246 SetRect(&current_rect, 0, 0,
247 ds->ds_current_size.cx,
248 ds->ds_current_size.cy);
249 else
250 SetRectEmpty(&current_rect);
252 IntersectRect(&r, draw_rect, &current_rect);
253 if (EqualRect(&r, draw_rect))
255 /* current_rect ⊇ draw_rect, modify only. */
256 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
257 return;
260 if (EqualRect(&r, &current_rect))
262 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
264 if (!clear_rect)
266 /* Full clear, modify only. */
267 *out_rect = *draw_rect;
268 return;
271 IntersectRect(&r, draw_rect, clear_rect);
272 if (EqualRect(&r, draw_rect))
274 /* clear_rect ⊇ draw_rect, modify only. */
275 *out_rect = *draw_rect;
276 return;
280 /* Full load. */
281 surface_load_ds_location(ds, context, location);
282 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
285 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
286 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
287 float depth, DWORD stencil)
289 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
290 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
291 const struct wined3d_gl_info *gl_info;
292 UINT drawable_width, drawable_height;
293 struct wined3d_context *context;
294 GLbitfield clear_mask = 0;
295 BOOL render_offscreen;
296 unsigned int i;
297 RECT ds_rect;
299 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
300 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
301 * for the cleared parts, and the untouched parts.
303 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
304 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
305 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
306 * checking all this if the dest surface is in the drawable anyway. */
307 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
309 for (i = 0; i < rt_count; ++i)
311 struct wined3d_surface *rt = fb->render_targets[i];
312 if (rt)
313 surface_load_location(rt, rt->draw_binding);
317 context = context_acquire(device, target);
318 if (!context->valid)
320 context_release(context);
321 WARN("Invalid context, skipping clear.\n");
322 return;
324 gl_info = context->gl_info;
326 if (target)
328 render_offscreen = context->render_offscreen;
329 target->get_drawable_size(context, &drawable_width, &drawable_height);
331 else
333 render_offscreen = TRUE;
334 drawable_width = fb->depth_stencil->pow2Width;
335 drawable_height = fb->depth_stencil->pow2Height;
338 if (flags & WINED3DCLEAR_ZBUFFER)
340 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : WINED3D_LOCATION_DRAWABLE;
342 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
343 device_switch_onscreen_ds(device, context, fb->depth_stencil);
344 prepare_ds_clear(fb->depth_stencil, context, location,
345 draw_rect, rect_count, clear_rect, &ds_rect);
348 if (!context_apply_clear_state(context, device, rt_count, fb))
350 context_release(context);
351 WARN("Failed to apply clear state, skipping clear.\n");
352 return;
355 /* Only set the values up once, as they are not changing. */
356 if (flags & WINED3DCLEAR_STENCIL)
358 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
360 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
361 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
363 gl_info->gl_ops.gl.p_glStencilMask(~0U);
364 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
365 gl_info->gl_ops.gl.p_glClearStencil(stencil);
366 checkGLcall("glClearStencil");
367 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
370 if (flags & WINED3DCLEAR_ZBUFFER)
372 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : WINED3D_LOCATION_DRAWABLE;
374 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
376 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
377 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
378 gl_info->gl_ops.gl.p_glClearDepth(depth);
379 checkGLcall("glClearDepth");
380 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
383 if (flags & WINED3DCLEAR_TARGET)
385 for (i = 0; i < rt_count; ++i)
387 struct wined3d_surface *rt = fb->render_targets[i];
389 if (rt)
391 surface_validate_location(rt, rt->draw_binding);
392 surface_invalidate_location(rt, ~rt->draw_binding);
396 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
397 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
398 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
399 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
400 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
401 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
402 checkGLcall("glClearColor");
403 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
406 if (!clear_rect)
408 if (render_offscreen)
410 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
411 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
413 else
415 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
416 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
418 checkGLcall("glScissor");
419 gl_info->gl_ops.gl.p_glClear(clear_mask);
420 checkGLcall("glClear");
422 else
424 RECT current_rect;
426 /* Now process each rect in turn. */
427 for (i = 0; i < rect_count; ++i)
429 /* Note that GL uses lower left, width/height. */
430 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
432 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
433 wine_dbgstr_rect(&clear_rect[i]),
434 wine_dbgstr_rect(&current_rect));
436 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
437 * The rectangle is not cleared, no error is returned, but further rectangles are
438 * still cleared if they are valid. */
439 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
441 TRACE("Rectangle with negative dimensions, ignoring.\n");
442 continue;
445 if (render_offscreen)
447 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
448 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
450 else
452 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
453 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
455 checkGLcall("glScissor");
457 gl_info->gl_ops.gl.p_glClear(clear_mask);
458 checkGLcall("glClear");
462 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
463 && target->swapchain && target->swapchain->front_buffer == target))
464 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
466 context_release(context);
469 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
471 ULONG refcount = InterlockedIncrement(&device->ref);
473 TRACE("%p increasing refcount to %u.\n", device, refcount);
475 return refcount;
478 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
480 ULONG refcount = InterlockedDecrement(&device->ref);
482 TRACE("%p decreasing refcount to %u.\n", device, refcount);
484 if (!refcount)
486 UINT i;
488 wined3d_cs_destroy(device->cs);
490 if (device->recording && wined3d_stateblock_decref(device->recording))
491 FIXME("Something's still holding the recording stateblock.\n");
492 device->recording = NULL;
494 state_cleanup(&device->state);
496 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
498 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
499 device->multistate_funcs[i] = NULL;
502 if (!list_empty(&device->resources))
504 struct wined3d_resource *resource;
506 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
508 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
510 FIXME("Leftover resource %p with type %s (%#x).\n",
511 resource, debug_d3dresourcetype(resource->type), resource->type);
515 if (device->contexts)
516 ERR("Context array not freed!\n");
517 if (device->hardwareCursor)
518 DestroyCursor(device->hardwareCursor);
519 device->hardwareCursor = 0;
521 wined3d_decref(device->wined3d);
522 device->wined3d = NULL;
523 HeapFree(GetProcessHeap(), 0, device);
524 TRACE("Freed device %p.\n", device);
527 return refcount;
530 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
532 TRACE("device %p.\n", device);
534 return device->swapchain_count;
537 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
539 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
541 if (swapchain_idx >= device->swapchain_count)
543 WARN("swapchain_idx %u >= swapchain_count %u.\n",
544 swapchain_idx, device->swapchain_count);
545 return NULL;
548 return device->swapchains[swapchain_idx];
551 static void device_load_logo(struct wined3d_device *device, const char *filename)
553 struct wined3d_color_key color_key;
554 struct wined3d_resource_desc desc;
555 struct wined3d_surface *surface;
556 HBITMAP hbm;
557 BITMAP bm;
558 HRESULT hr;
559 HDC dcb = NULL, dcs = NULL;
561 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
562 if(hbm)
564 GetObjectA(hbm, sizeof(BITMAP), &bm);
565 dcb = CreateCompatibleDC(NULL);
566 if(!dcb) goto out;
567 SelectObject(dcb, hbm);
569 else
571 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
572 * couldn't be loaded
574 memset(&bm, 0, sizeof(bm));
575 bm.bmWidth = 32;
576 bm.bmHeight = 32;
579 desc.resource_type = WINED3D_RTYPE_TEXTURE;
580 desc.format = WINED3DFMT_B5G6R5_UNORM;
581 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
582 desc.multisample_quality = 0;
583 desc.usage = WINED3DUSAGE_DYNAMIC;
584 desc.pool = WINED3D_POOL_DEFAULT;
585 desc.width = bm.bmWidth;
586 desc.height = bm.bmHeight;
587 desc.depth = 1;
588 desc.size = 0;
589 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
590 NULL, &wined3d_null_parent_ops, &device->logo_texture)))
592 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
593 goto out;
595 surface = surface_from_resource(wined3d_texture_get_sub_resource(device->logo_texture, 0));
597 if (dcb)
599 if (FAILED(hr = wined3d_surface_getdc(surface, &dcs)))
600 goto out;
601 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
602 wined3d_surface_releasedc(surface, dcs);
604 color_key.color_space_low_value = 0;
605 color_key.color_space_high_value = 0;
606 wined3d_texture_set_color_key(device->logo_texture, WINEDDCKEY_SRCBLT, &color_key);
608 else
610 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
611 /* Fill the surface with a white color to show that wined3d is there */
612 wined3d_device_color_fill(device, surface, NULL, &c);
615 out:
616 if (dcb) DeleteDC(dcb);
617 if (hbm) DeleteObject(hbm);
620 /* Context activation is done by the caller. */
621 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
623 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
624 unsigned int i, j, count;
625 /* Under DirectX you can sample even if no texture is bound, whereas
626 * OpenGL will only allow that when a valid texture is bound.
627 * We emulate this by creating dummy textures and binding them
628 * to each texture stage when the currently set D3D texture is NULL. */
630 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
631 for (i = 0; i < count; ++i)
633 DWORD color = 0x000000ff;
635 /* Make appropriate texture active */
636 context_active_texture(context, gl_info, i);
638 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
639 checkGLcall("glGenTextures");
640 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
642 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
643 checkGLcall("glBindTexture");
645 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
646 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
647 checkGLcall("glTexImage2D");
649 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
651 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
652 checkGLcall("glGenTextures");
653 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
655 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
656 checkGLcall("glBindTexture");
658 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
659 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
660 checkGLcall("glTexImage2D");
663 if (gl_info->supported[EXT_TEXTURE3D])
665 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
666 checkGLcall("glGenTextures");
667 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
669 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
670 checkGLcall("glBindTexture");
672 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
673 checkGLcall("glTexImage3D");
676 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
678 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
679 checkGLcall("glGenTextures");
680 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
682 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
683 checkGLcall("glBindTexture");
685 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
687 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
688 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
689 checkGLcall("glTexImage2D");
695 /* Context activation is done by the caller. */
696 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
698 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
700 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
702 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
703 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
706 if (gl_info->supported[EXT_TEXTURE3D])
708 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
709 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
712 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
714 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
715 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
718 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
719 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
721 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
722 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
723 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
724 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
727 static LONG fullscreen_style(LONG style)
729 /* Make sure the window is managed, otherwise we won't get keyboard input. */
730 style |= WS_POPUP | WS_SYSMENU;
731 style &= ~(WS_CAPTION | WS_THICKFRAME);
733 return style;
736 static LONG fullscreen_exstyle(LONG exstyle)
738 /* Filter out window decorations. */
739 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
741 return exstyle;
744 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
746 BOOL filter_messages;
747 LONG style, exstyle;
749 TRACE("Setting up window %p for fullscreen mode.\n", window);
751 if (device->style || device->exStyle)
753 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
754 window, device->style, device->exStyle);
757 device->style = GetWindowLongW(window, GWL_STYLE);
758 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
760 style = fullscreen_style(device->style);
761 exstyle = fullscreen_exstyle(device->exStyle);
763 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
764 device->style, device->exStyle, style, exstyle);
766 filter_messages = device->filter_messages;
767 device->filter_messages = TRUE;
769 SetWindowLongW(window, GWL_STYLE, style);
770 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
771 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
773 device->filter_messages = filter_messages;
776 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
778 BOOL filter_messages;
779 LONG style, exstyle;
781 if (!device->style && !device->exStyle) return;
783 style = GetWindowLongW(window, GWL_STYLE);
784 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
786 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
787 * application, and we want to ignore them in the test below, since it's
788 * not the application's fault that they changed. Additionally, we want to
789 * preserve the current status of these flags (i.e. don't restore them) to
790 * more closely emulate the behavior of Direct3D, which leaves these flags
791 * alone when returning to windowed mode. */
792 device->style ^= (device->style ^ style) & WS_VISIBLE;
793 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
795 TRACE("Restoring window style of window %p to %08x, %08x.\n",
796 window, device->style, device->exStyle);
798 filter_messages = device->filter_messages;
799 device->filter_messages = TRUE;
801 /* Only restore the style if the application didn't modify it during the
802 * fullscreen phase. Some applications change it before calling Reset()
803 * when switching between windowed and fullscreen modes (HL2), some
804 * depend on the original style (Eve Online). */
805 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
807 SetWindowLongW(window, GWL_STYLE, device->style);
808 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
810 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
812 device->filter_messages = filter_messages;
814 /* Delete the old values. */
815 device->style = 0;
816 device->exStyle = 0;
819 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
821 TRACE("device %p, window %p.\n", device, window);
823 if (!wined3d_register_window(window, device))
825 ERR("Failed to register window %p.\n", window);
826 return E_FAIL;
829 InterlockedExchangePointer((void **)&device->focus_window, window);
830 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
832 return WINED3D_OK;
835 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
837 TRACE("device %p.\n", device);
839 if (device->focus_window) wined3d_unregister_window(device->focus_window);
840 InterlockedExchangePointer((void **)&device->focus_window, NULL);
843 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
845 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
846 unsigned int i;
848 if (device->fb.render_targets)
850 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
852 wined3d_device_set_render_target(device, i, NULL, FALSE);
854 if (swapchain->back_buffers && swapchain->back_buffers[0])
855 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], TRUE);
858 wined3d_device_set_depth_stencil(device, ds_enable ? device->auto_depth_stencil : NULL);
859 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
862 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
863 struct wined3d_swapchain_desc *swapchain_desc)
865 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
866 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
867 struct wined3d_swapchain *swapchain = NULL;
868 struct wined3d_context *context;
869 DWORD clear_flags = 0;
870 HRESULT hr;
872 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
874 if (device->d3d_initialized)
875 return WINED3DERR_INVALIDCALL;
876 if (device->wined3d->flags & WINED3D_NO3D)
877 return WINED3DERR_INVALIDCALL;
879 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
880 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
882 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
883 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
885 TRACE("Shader private data couldn't be allocated\n");
886 goto err_out;
888 if (FAILED(hr = device->blitter->alloc_private(device)))
890 TRACE("Blitter private data couldn't be allocated\n");
891 goto err_out;
894 /* Setup the implicit swapchain. This also initializes a context. */
895 TRACE("Creating implicit swapchain\n");
896 hr = device->device_parent->ops->create_swapchain(device->device_parent,
897 swapchain_desc, &swapchain);
898 if (FAILED(hr))
900 WARN("Failed to create implicit swapchain\n");
901 goto err_out;
904 device->swapchain_count = 1;
905 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
906 if (!device->swapchains)
908 ERR("Out of memory!\n");
909 goto err_out;
911 device->swapchains[0] = swapchain;
912 device_init_swapchain_state(device, swapchain);
914 context = context_acquire(device, swapchain->front_buffer);
916 create_dummy_textures(device, context);
918 device->contexts[0]->last_was_rhw = 0;
920 switch (wined3d_settings.offscreen_rendering_mode)
922 case ORM_FBO:
923 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
924 break;
926 case ORM_BACKBUFFER:
928 if (context_get_current()->aux_buffers > 0)
930 TRACE("Using auxiliary buffer for offscreen rendering\n");
931 device->offscreenBuffer = GL_AUX0;
933 else
935 TRACE("Using back buffer for offscreen rendering\n");
936 device->offscreenBuffer = GL_BACK;
941 TRACE("All defaults now set up, leaving 3D init.\n");
943 context_release(context);
945 /* Clear the screen */
946 if (swapchain->back_buffers && swapchain->back_buffers[0])
947 clear_flags |= WINED3DCLEAR_TARGET;
948 if (swapchain_desc->enable_auto_depth_stencil)
949 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
950 if (clear_flags)
951 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
953 device->d3d_initialized = TRUE;
955 if (wined3d_settings.logo)
956 device_load_logo(device, wined3d_settings.logo);
957 return WINED3D_OK;
959 err_out:
960 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
961 HeapFree(GetProcessHeap(), 0, device->swapchains);
962 device->swapchain_count = 0;
963 if (swapchain)
964 wined3d_swapchain_decref(swapchain);
965 if (device->blit_priv)
966 device->blitter->free_private(device);
967 if (device->shader_priv)
968 device->shader_backend->shader_free_private(device);
970 return hr;
973 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
974 struct wined3d_swapchain_desc *swapchain_desc)
976 struct wined3d_swapchain *swapchain = NULL;
977 HRESULT hr;
979 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
981 /* Setup the implicit swapchain */
982 TRACE("Creating implicit swapchain\n");
983 hr = device->device_parent->ops->create_swapchain(device->device_parent,
984 swapchain_desc, &swapchain);
985 if (FAILED(hr))
987 WARN("Failed to create implicit swapchain\n");
988 goto err_out;
991 device->swapchain_count = 1;
992 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
993 if (!device->swapchains)
995 ERR("Out of memory!\n");
996 goto err_out;
998 device->swapchains[0] = swapchain;
999 return WINED3D_OK;
1001 err_out:
1002 wined3d_swapchain_decref(swapchain);
1003 return hr;
1006 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1008 struct wined3d_resource *resource, *cursor;
1009 const struct wined3d_gl_info *gl_info;
1010 struct wined3d_context *context;
1011 struct wined3d_surface *surface;
1012 UINT i;
1014 TRACE("device %p.\n", device);
1016 if (!device->d3d_initialized)
1017 return WINED3DERR_INVALIDCALL;
1019 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1020 * it was created. Thus make sure a context is active for the glDelete* calls
1022 context = context_acquire(device, NULL);
1023 gl_info = context->gl_info;
1025 if (device->logo_texture)
1026 wined3d_texture_decref(device->logo_texture);
1027 if (device->cursor_texture)
1028 wined3d_texture_decref(device->cursor_texture);
1030 state_unbind_resources(&device->state);
1032 /* Unload resources */
1033 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1035 TRACE("Unloading resource %p.\n", resource);
1037 resource->resource_ops->resource_unload(resource);
1040 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1041 * private data, it might contain opengl pointers
1043 if (device->depth_blt_texture)
1045 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1046 device->depth_blt_texture = 0;
1049 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1050 device->blitter->free_private(device);
1051 device->shader_backend->shader_free_private(device);
1052 destroy_dummy_textures(device, gl_info);
1054 /* Release the buffers (with sanity checks)*/
1055 if (device->onscreen_depth_stencil)
1057 surface = device->onscreen_depth_stencil;
1058 device->onscreen_depth_stencil = NULL;
1059 wined3d_surface_decref(surface);
1062 if (device->fb.depth_stencil)
1064 surface = device->fb.depth_stencil;
1066 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1068 device->fb.depth_stencil = NULL;
1069 wined3d_surface_decref(surface);
1072 if (device->auto_depth_stencil)
1074 surface = device->auto_depth_stencil;
1075 device->auto_depth_stencil = NULL;
1076 if (wined3d_surface_decref(surface))
1077 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1080 for (i = 0; i < gl_info->limits.buffers; ++i)
1082 wined3d_device_set_render_target(device, i, NULL, FALSE);
1085 context_release(context);
1087 for (i = 0; i < device->swapchain_count; ++i)
1089 TRACE("Releasing the implicit swapchain %u.\n", i);
1090 if (wined3d_swapchain_decref(device->swapchains[i]))
1091 FIXME("Something's still holding the implicit swapchain.\n");
1094 HeapFree(GetProcessHeap(), 0, device->swapchains);
1095 device->swapchains = NULL;
1096 device->swapchain_count = 0;
1098 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1099 device->fb.render_targets = NULL;
1101 device->d3d_initialized = FALSE;
1103 return WINED3D_OK;
1106 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1108 unsigned int i;
1110 for (i = 0; i < device->swapchain_count; ++i)
1112 TRACE("Releasing the implicit swapchain %u.\n", i);
1113 if (wined3d_swapchain_decref(device->swapchains[i]))
1114 FIXME("Something's still holding the implicit swapchain.\n");
1117 HeapFree(GetProcessHeap(), 0, device->swapchains);
1118 device->swapchains = NULL;
1119 device->swapchain_count = 0;
1120 return WINED3D_OK;
1123 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1124 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1125 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1127 * There is no way to deactivate thread safety once it is enabled.
1129 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1131 TRACE("device %p.\n", device);
1133 /* For now just store the flag (needed in case of ddraw). */
1134 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1137 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1139 TRACE("device %p.\n", device);
1141 TRACE("Emulating %d MB, returning %d MB left.\n",
1142 device->adapter->TextureRam / (1024 * 1024),
1143 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1145 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1148 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1149 struct wined3d_buffer *buffer, UINT offset)
1151 struct wined3d_stream_output *stream;
1152 struct wined3d_buffer *prev_buffer;
1154 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1156 if (idx >= MAX_STREAM_OUT)
1158 WARN("Invalid stream output %u.\n", idx);
1159 return;
1162 stream = &device->update_state->stream_output[idx];
1163 prev_buffer = stream->buffer;
1165 if (buffer)
1166 wined3d_buffer_incref(buffer);
1167 stream->buffer = buffer;
1168 stream->offset = offset;
1169 if (!device->recording)
1170 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1171 if (prev_buffer)
1172 wined3d_buffer_decref(prev_buffer);
1175 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1176 UINT idx, UINT *offset)
1178 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1180 if (idx >= MAX_STREAM_OUT)
1182 WARN("Invalid stream output %u.\n", idx);
1183 return NULL;
1186 *offset = device->state.stream_output[idx].offset;
1187 return device->state.stream_output[idx].buffer;
1190 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1191 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1193 struct wined3d_stream_state *stream;
1194 struct wined3d_buffer *prev_buffer;
1196 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1197 device, stream_idx, buffer, offset, stride);
1199 if (stream_idx >= MAX_STREAMS)
1201 WARN("Stream index %u out of range.\n", stream_idx);
1202 return WINED3DERR_INVALIDCALL;
1204 else if (offset & 0x3)
1206 WARN("Offset %u is not 4 byte aligned.\n", offset);
1207 return WINED3DERR_INVALIDCALL;
1210 stream = &device->update_state->streams[stream_idx];
1211 prev_buffer = stream->buffer;
1213 if (device->recording)
1214 device->recording->changed.streamSource |= 1 << stream_idx;
1216 if (prev_buffer == buffer
1217 && stream->stride == stride
1218 && stream->offset == offset)
1220 TRACE("Application is setting the old values over, nothing to do.\n");
1221 return WINED3D_OK;
1224 stream->buffer = buffer;
1225 if (buffer)
1227 stream->stride = stride;
1228 stream->offset = offset;
1231 if (buffer)
1232 wined3d_buffer_incref(buffer);
1233 if (!device->recording)
1234 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1235 if (prev_buffer)
1236 wined3d_buffer_decref(prev_buffer);
1238 return WINED3D_OK;
1241 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1242 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1244 const struct wined3d_stream_state *stream;
1246 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1247 device, stream_idx, buffer, offset, stride);
1249 if (stream_idx >= MAX_STREAMS)
1251 WARN("Stream index %u out of range.\n", stream_idx);
1252 return WINED3DERR_INVALIDCALL;
1255 stream = &device->state.streams[stream_idx];
1256 *buffer = stream->buffer;
1257 if (*buffer)
1258 wined3d_buffer_incref(*buffer);
1259 if (offset)
1260 *offset = stream->offset;
1261 *stride = stream->stride;
1263 return WINED3D_OK;
1266 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1268 struct wined3d_stream_state *stream;
1269 UINT old_flags, old_freq;
1271 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1273 /* Verify input. At least in d3d9 this is invalid. */
1274 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1276 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1277 return WINED3DERR_INVALIDCALL;
1279 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1281 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1282 return WINED3DERR_INVALIDCALL;
1284 if (!divider)
1286 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1287 return WINED3DERR_INVALIDCALL;
1290 stream = &device->update_state->streams[stream_idx];
1291 old_flags = stream->flags;
1292 old_freq = stream->frequency;
1294 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1295 stream->frequency = divider & 0x7fffff;
1297 if (device->recording)
1298 device->recording->changed.streamFreq |= 1 << stream_idx;
1299 else if (stream->frequency != old_freq || stream->flags != old_flags)
1300 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1302 return WINED3D_OK;
1305 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1306 UINT stream_idx, UINT *divider)
1308 const struct wined3d_stream_state *stream;
1310 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1312 stream = &device->state.streams[stream_idx];
1313 *divider = stream->flags | stream->frequency;
1315 TRACE("Returning %#x.\n", *divider);
1317 return WINED3D_OK;
1320 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1321 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1323 TRACE("device %p, state %s, matrix %p.\n",
1324 device, debug_d3dtstype(d3dts), matrix);
1325 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1326 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1327 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1328 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1330 /* Handle recording of state blocks. */
1331 if (device->recording)
1333 TRACE("Recording... not performing anything.\n");
1334 device->recording->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1335 device->update_state->transforms[d3dts] = *matrix;
1336 return;
1339 /* If the new matrix is the same as the current one,
1340 * we cut off any further processing. this seems to be a reasonable
1341 * optimization because as was noticed, some apps (warcraft3 for example)
1342 * tend towards setting the same matrix repeatedly for some reason.
1344 * From here on we assume that the new matrix is different, wherever it matters. */
1345 if (!memcmp(&device->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1347 TRACE("The application is setting the same matrix over again.\n");
1348 return;
1351 device->state.transforms[d3dts] = *matrix;
1352 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1355 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1356 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1358 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1360 *matrix = device->state.transforms[state];
1363 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1364 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1366 const struct wined3d_matrix *mat;
1367 struct wined3d_matrix temp;
1369 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1371 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1372 * below means it will be recorded in a state block change, but it
1373 * works regardless where it is recorded.
1374 * If this is found to be wrong, change to StateBlock. */
1375 if (state > HIGHEST_TRANSFORMSTATE)
1377 WARN("Unhandled transform state %#x.\n", state);
1378 return;
1381 mat = &device->update_state->transforms[state];
1382 multiply_matrix(&temp, mat, matrix);
1384 /* Apply change via set transform - will reapply to eg. lights this way. */
1385 wined3d_device_set_transform(device, state, &temp);
1388 /* Note lights are real special cases. Although the device caps state only
1389 * e.g. 8 are supported, you can reference any indexes you want as long as
1390 * that number max are enabled at any one point in time. Therefore since the
1391 * indices can be anything, we need a hashmap of them. However, this causes
1392 * stateblock problems. When capturing the state block, I duplicate the
1393 * hashmap, but when recording, just build a chain pretty much of commands to
1394 * be replayed. */
1395 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1396 UINT light_idx, const struct wined3d_light *light)
1398 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1399 struct wined3d_light_info *object = NULL;
1400 struct list *e;
1401 float rho;
1403 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1405 /* Check the parameter range. Need for speed most wanted sets junk lights
1406 * which confuse the GL driver. */
1407 if (!light)
1408 return WINED3DERR_INVALIDCALL;
1410 switch (light->type)
1412 case WINED3D_LIGHT_POINT:
1413 case WINED3D_LIGHT_SPOT:
1414 case WINED3D_LIGHT_PARALLELPOINT:
1415 case WINED3D_LIGHT_GLSPOT:
1416 /* Incorrect attenuation values can cause the gl driver to crash.
1417 * Happens with Need for speed most wanted. */
1418 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1420 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1421 return WINED3DERR_INVALIDCALL;
1423 break;
1425 case WINED3D_LIGHT_DIRECTIONAL:
1426 /* Ignores attenuation */
1427 break;
1429 default:
1430 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1431 return WINED3DERR_INVALIDCALL;
1434 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1436 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1437 if (object->OriginalIndex == light_idx)
1438 break;
1439 object = NULL;
1442 if (!object)
1444 TRACE("Adding new light\n");
1445 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1446 if (!object)
1447 return E_OUTOFMEMORY;
1449 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1450 object->glIndex = -1;
1451 object->OriginalIndex = light_idx;
1454 /* Initialize the object. */
1455 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1456 light_idx, light->type,
1457 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1458 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1459 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1460 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1461 light->direction.x, light->direction.y, light->direction.z);
1462 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1463 light->range, light->falloff, light->theta, light->phi);
1465 /* Update the live definitions if the light is currently assigned a glIndex. */
1466 if (object->glIndex != -1 && !device->recording)
1468 if (object->OriginalParms.type != light->type)
1469 device_invalidate_state(device, STATE_LIGHT_TYPE);
1470 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1473 /* Save away the information. */
1474 object->OriginalParms = *light;
1476 switch (light->type)
1478 case WINED3D_LIGHT_POINT:
1479 /* Position */
1480 object->lightPosn[0] = light->position.x;
1481 object->lightPosn[1] = light->position.y;
1482 object->lightPosn[2] = light->position.z;
1483 object->lightPosn[3] = 1.0f;
1484 object->cutoff = 180.0f;
1485 /* FIXME: Range */
1486 break;
1488 case WINED3D_LIGHT_DIRECTIONAL:
1489 /* Direction */
1490 object->lightPosn[0] = -light->direction.x;
1491 object->lightPosn[1] = -light->direction.y;
1492 object->lightPosn[2] = -light->direction.z;
1493 object->lightPosn[3] = 0.0f;
1494 object->exponent = 0.0f;
1495 object->cutoff = 180.0f;
1496 break;
1498 case WINED3D_LIGHT_SPOT:
1499 /* Position */
1500 object->lightPosn[0] = light->position.x;
1501 object->lightPosn[1] = light->position.y;
1502 object->lightPosn[2] = light->position.z;
1503 object->lightPosn[3] = 1.0f;
1505 /* Direction */
1506 object->lightDirn[0] = light->direction.x;
1507 object->lightDirn[1] = light->direction.y;
1508 object->lightDirn[2] = light->direction.z;
1509 object->lightDirn[3] = 1.0f;
1511 /* opengl-ish and d3d-ish spot lights use too different models
1512 * for the light "intensity" as a function of the angle towards
1513 * the main light direction, so we only can approximate very
1514 * roughly. However, spot lights are rather rarely used in games
1515 * (if ever used at all). Furthermore if still used, probably
1516 * nobody pays attention to such details. */
1517 if (!light->falloff)
1519 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1520 * equations have the falloff resp. exponent parameter as an
1521 * exponent, so the spot light lighting will always be 1.0 for
1522 * both of them, and we don't have to care for the rest of the
1523 * rather complex calculation. */
1524 object->exponent = 0.0f;
1526 else
1528 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1529 if (rho < 0.0001f)
1530 rho = 0.0001f;
1531 object->exponent = -0.3f / logf(cosf(rho / 2));
1534 if (object->exponent > 128.0f)
1535 object->exponent = 128.0f;
1537 object->cutoff = (float)(light->phi * 90 / M_PI);
1538 /* FIXME: Range */
1539 break;
1541 default:
1542 FIXME("Unrecognized light type %#x.\n", light->type);
1545 return WINED3D_OK;
1548 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1549 UINT light_idx, struct wined3d_light *light)
1551 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1552 struct wined3d_light_info *light_info = NULL;
1553 struct list *e;
1555 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1557 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1559 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1560 if (light_info->OriginalIndex == light_idx)
1561 break;
1562 light_info = NULL;
1565 if (!light_info)
1567 TRACE("Light information requested but light not defined\n");
1568 return WINED3DERR_INVALIDCALL;
1571 *light = light_info->OriginalParms;
1572 return WINED3D_OK;
1575 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1577 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1578 struct wined3d_light_info *light_info = NULL;
1579 struct list *e;
1581 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1583 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1585 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1586 if (light_info->OriginalIndex == light_idx)
1587 break;
1588 light_info = NULL;
1590 TRACE("Found light %p.\n", light_info);
1592 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1593 if (!light_info)
1595 TRACE("Light enabled requested but light not defined, so defining one!\n");
1596 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1598 /* Search for it again! Should be fairly quick as near head of list. */
1599 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1601 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1602 if (light_info->OriginalIndex == light_idx)
1603 break;
1604 light_info = NULL;
1606 if (!light_info)
1608 FIXME("Adding default lights has failed dismally\n");
1609 return WINED3DERR_INVALIDCALL;
1613 if (!enable)
1615 if (light_info->glIndex != -1)
1617 if (!device->recording)
1619 device_invalidate_state(device, STATE_LIGHT_TYPE);
1620 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1623 device->update_state->lights[light_info->glIndex] = NULL;
1624 light_info->glIndex = -1;
1626 else
1628 TRACE("Light already disabled, nothing to do\n");
1630 light_info->enabled = FALSE;
1632 else
1634 light_info->enabled = TRUE;
1635 if (light_info->glIndex != -1)
1637 TRACE("Nothing to do as light was enabled\n");
1639 else
1641 unsigned int i;
1642 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1643 /* Find a free GL light. */
1644 for (i = 0; i < gl_info->limits.lights; ++i)
1646 if (!device->update_state->lights[i])
1648 device->update_state->lights[i] = light_info;
1649 light_info->glIndex = i;
1650 break;
1653 if (light_info->glIndex == -1)
1655 /* Our tests show that Windows returns D3D_OK in this situation, even with
1656 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1657 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1658 * as well for those lights.
1660 * TODO: Test how this affects rendering. */
1661 WARN("Too many concurrently active lights\n");
1662 return WINED3D_OK;
1665 /* i == light_info->glIndex */
1666 if (!device->recording)
1668 device_invalidate_state(device, STATE_LIGHT_TYPE);
1669 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1674 return WINED3D_OK;
1677 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1679 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1680 struct wined3d_light_info *light_info = NULL;
1681 struct list *e;
1683 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1685 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1687 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1688 if (light_info->OriginalIndex == light_idx)
1689 break;
1690 light_info = NULL;
1693 if (!light_info)
1695 TRACE("Light enabled state requested but light not defined.\n");
1696 return WINED3DERR_INVALIDCALL;
1698 /* true is 128 according to SetLightEnable */
1699 *enable = light_info->enabled ? 128 : 0;
1700 return WINED3D_OK;
1703 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1704 UINT plane_idx, const struct wined3d_vec4 *plane)
1706 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1708 /* Validate plane_idx. */
1709 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1711 TRACE("Application has requested clipplane this device doesn't support.\n");
1712 return WINED3DERR_INVALIDCALL;
1715 if (device->recording)
1716 device->recording->changed.clipplane |= 1 << plane_idx;
1718 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1720 TRACE("Application is setting old values over, nothing to do.\n");
1721 return WINED3D_OK;
1724 device->update_state->clip_planes[plane_idx] = *plane;
1726 if (!device->recording)
1727 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1729 return WINED3D_OK;
1732 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1733 UINT plane_idx, struct wined3d_vec4 *plane)
1735 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1737 /* Validate plane_idx. */
1738 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1740 TRACE("Application has requested clipplane this device doesn't support.\n");
1741 return WINED3DERR_INVALIDCALL;
1744 *plane = device->state.clip_planes[plane_idx];
1746 return WINED3D_OK;
1749 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1750 const struct wined3d_clip_status *clip_status)
1752 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1754 if (!clip_status)
1755 return WINED3DERR_INVALIDCALL;
1757 return WINED3D_OK;
1760 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1761 struct wined3d_clip_status *clip_status)
1763 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1765 if (!clip_status)
1766 return WINED3DERR_INVALIDCALL;
1768 return WINED3D_OK;
1771 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1773 TRACE("device %p, material %p.\n", device, material);
1775 device->update_state->material = *material;
1777 if (device->recording)
1778 device->recording->changed.material = TRUE;
1779 else
1780 wined3d_cs_emit_set_material(device->cs, material);
1783 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1785 TRACE("device %p, material %p.\n", device, material);
1787 *material = device->state.material;
1789 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1790 material->diffuse.r, material->diffuse.g,
1791 material->diffuse.b, material->diffuse.a);
1792 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1793 material->ambient.r, material->ambient.g,
1794 material->ambient.b, material->ambient.a);
1795 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1796 material->specular.r, material->specular.g,
1797 material->specular.b, material->specular.a);
1798 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1799 material->emissive.r, material->emissive.g,
1800 material->emissive.b, material->emissive.a);
1801 TRACE("power %.8e.\n", material->power);
1804 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1805 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1807 enum wined3d_format_id prev_format;
1808 struct wined3d_buffer *prev_buffer;
1810 TRACE("device %p, buffer %p, format %s.\n",
1811 device, buffer, debug_d3dformat(format_id));
1813 prev_buffer = device->update_state->index_buffer;
1814 prev_format = device->update_state->index_format;
1816 device->update_state->index_buffer = buffer;
1817 device->update_state->index_format = format_id;
1819 if (device->recording)
1820 device->recording->changed.indices = TRUE;
1822 if (prev_buffer == buffer && prev_format == format_id)
1823 return;
1825 if (buffer)
1826 wined3d_buffer_incref(buffer);
1827 if (!device->recording)
1828 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1829 if (prev_buffer)
1830 wined3d_buffer_decref(prev_buffer);
1833 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1834 enum wined3d_format_id *format)
1836 TRACE("device %p, format %p.\n", device, format);
1838 *format = device->state.index_format;
1839 return device->state.index_buffer;
1842 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1844 TRACE("device %p, base_index %d.\n", device, base_index);
1846 device->update_state->base_vertex_index = base_index;
1849 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1851 TRACE("device %p.\n", device);
1853 return device->state.base_vertex_index;
1856 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1858 TRACE("device %p, viewport %p.\n", device, viewport);
1859 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1860 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1862 device->update_state->viewport = *viewport;
1864 /* Handle recording of state blocks */
1865 if (device->recording)
1867 TRACE("Recording... not performing anything\n");
1868 device->recording->changed.viewport = TRUE;
1869 return;
1872 wined3d_cs_emit_set_viewport(device->cs, viewport);
1875 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1877 TRACE("device %p, viewport %p.\n", device, viewport);
1879 *viewport = device->state.viewport;
1882 static void resolve_depth_buffer(struct wined3d_state *state)
1884 struct wined3d_texture *texture = state->textures[0];
1885 struct wined3d_surface *depth_stencil, *surface;
1887 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1888 || !(texture->resource.format->flags & WINED3DFMT_FLAG_DEPTH))
1889 return;
1890 surface = surface_from_resource(texture->sub_resources[0]);
1891 depth_stencil = state->fb->depth_stencil;
1892 if (!depth_stencil)
1893 return;
1895 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1898 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1899 enum wined3d_render_state state, DWORD value)
1901 DWORD old_value = device->state.render_states[state];
1903 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1905 device->update_state->render_states[state] = value;
1907 /* Handle recording of state blocks. */
1908 if (device->recording)
1910 TRACE("Recording... not performing anything.\n");
1911 device->recording->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
1912 return;
1915 /* Compared here and not before the assignment to allow proper stateblock recording. */
1916 if (value == old_value)
1917 TRACE("Application is setting the old value over, nothing to do.\n");
1918 else
1919 wined3d_cs_emit_set_render_state(device->cs, state, value);
1921 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1923 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1924 resolve_depth_buffer(&device->state);
1928 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1930 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1932 return device->state.render_states[state];
1935 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1936 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1938 DWORD old_value;
1940 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1941 device, sampler_idx, debug_d3dsamplerstate(state), value);
1943 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1944 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1946 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1948 WARN("Invalid sampler %u.\n", sampler_idx);
1949 return; /* Windows accepts overflowing this array ... we do not. */
1952 old_value = device->state.sampler_states[sampler_idx][state];
1953 device->update_state->sampler_states[sampler_idx][state] = value;
1955 /* Handle recording of state blocks. */
1956 if (device->recording)
1958 TRACE("Recording... not performing anything.\n");
1959 device->recording->changed.samplerState[sampler_idx] |= 1 << state;
1960 return;
1963 if (old_value == value)
1965 TRACE("Application is setting the old value over, nothing to do.\n");
1966 return;
1969 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
1972 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
1973 UINT sampler_idx, enum wined3d_sampler_state state)
1975 TRACE("device %p, sampler_idx %u, state %s.\n",
1976 device, sampler_idx, debug_d3dsamplerstate(state));
1978 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1979 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1981 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1983 WARN("Invalid sampler %u.\n", sampler_idx);
1984 return 0; /* Windows accepts overflowing this array ... we do not. */
1987 return device->state.sampler_states[sampler_idx][state];
1990 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
1992 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
1994 if (device->recording)
1995 device->recording->changed.scissorRect = TRUE;
1997 if (EqualRect(&device->update_state->scissor_rect, rect))
1999 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2000 return;
2002 CopyRect(&device->update_state->scissor_rect, rect);
2004 if (device->recording)
2006 TRACE("Recording... not performing anything.\n");
2007 return;
2010 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2013 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2015 TRACE("device %p, rect %p.\n", device, rect);
2017 *rect = device->state.scissor_rect;
2018 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2021 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2022 struct wined3d_vertex_declaration *declaration)
2024 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2026 TRACE("device %p, declaration %p.\n", device, declaration);
2028 if (device->recording)
2029 device->recording->changed.vertexDecl = TRUE;
2031 if (declaration == prev)
2032 return;
2034 if (declaration)
2035 wined3d_vertex_declaration_incref(declaration);
2036 device->update_state->vertex_declaration = declaration;
2037 if (!device->recording)
2038 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2039 if (prev)
2040 wined3d_vertex_declaration_decref(prev);
2043 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2045 TRACE("device %p.\n", device);
2047 return device->state.vertex_declaration;
2050 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2052 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2054 TRACE("device %p, shader %p.\n", device, shader);
2056 if (device->recording)
2057 device->recording->changed.vertexShader = TRUE;
2059 if (shader == prev)
2060 return;
2062 if (shader)
2063 wined3d_shader_incref(shader);
2064 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2065 if (!device->recording)
2066 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2067 if (prev)
2068 wined3d_shader_decref(prev);
2071 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2073 TRACE("device %p.\n", device);
2075 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2078 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2079 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2081 struct wined3d_buffer *prev;
2083 if (idx >= MAX_CONSTANT_BUFFERS)
2085 WARN("Invalid constant buffer index %u.\n", idx);
2086 return;
2089 prev = device->update_state->cb[type][idx];
2090 if (buffer == prev)
2091 return;
2093 if (buffer)
2094 wined3d_buffer_incref(buffer);
2095 device->update_state->cb[type][idx] = buffer;
2096 if (!device->recording)
2097 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2098 if (prev)
2099 wined3d_buffer_decref(prev);
2102 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2104 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2106 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2109 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2111 TRACE("device %p, idx %u.\n", device, idx);
2113 if (idx >= MAX_CONSTANT_BUFFERS)
2115 WARN("Invalid constant buffer index %u.\n", idx);
2116 return NULL;
2119 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2122 static void wined3d_device_set_sampler(struct wined3d_device *device,
2123 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2125 struct wined3d_sampler *prev;
2127 if (idx >= MAX_SAMPLER_OBJECTS)
2129 WARN("Invalid sampler index %u.\n", idx);
2130 return;
2133 prev = device->update_state->sampler[type][idx];
2134 if (sampler == prev)
2135 return;
2137 if (sampler)
2138 wined3d_sampler_incref(sampler);
2139 device->update_state->sampler[type][idx] = sampler;
2140 if (!device->recording)
2141 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2142 if (prev)
2143 wined3d_sampler_decref(prev);
2146 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2148 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2150 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2153 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2155 TRACE("device %p, idx %u.\n", device, idx);
2157 if (idx >= MAX_SAMPLER_OBJECTS)
2159 WARN("Invalid sampler index %u.\n", idx);
2160 return NULL;
2163 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2166 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2168 UINT i;
2170 for (i = 0; i < device->context_count; ++i)
2172 device->contexts[i]->constant_update_mask |= mask;
2176 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2177 UINT start_register, const BOOL *constants, UINT bool_count)
2179 UINT count = min(bool_count, MAX_CONST_B - start_register);
2180 UINT i;
2182 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2183 device, start_register, constants, bool_count);
2185 if (!constants || start_register >= MAX_CONST_B)
2186 return WINED3DERR_INVALIDCALL;
2188 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2189 for (i = 0; i < count; ++i)
2190 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2192 if (device->recording)
2194 for (i = start_register; i < count + start_register; ++i)
2195 device->recording->changed.vertexShaderConstantsB |= (1 << i);
2197 else
2199 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2202 return WINED3D_OK;
2205 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2206 UINT start_register, BOOL *constants, UINT bool_count)
2208 UINT count = min(bool_count, MAX_CONST_B - start_register);
2210 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2211 device, start_register, constants, bool_count);
2213 if (!constants || start_register >= MAX_CONST_B)
2214 return WINED3DERR_INVALIDCALL;
2216 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2218 return WINED3D_OK;
2221 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2222 UINT start_register, const int *constants, UINT vector4i_count)
2224 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2225 UINT i;
2227 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2228 device, start_register, constants, vector4i_count);
2230 if (!constants || start_register >= MAX_CONST_I)
2231 return WINED3DERR_INVALIDCALL;
2233 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2234 for (i = 0; i < count; ++i)
2235 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2236 constants[i * 4], constants[i * 4 + 1],
2237 constants[i * 4 + 2], constants[i * 4 + 3]);
2239 if (device->recording)
2241 for (i = start_register; i < count + start_register; ++i)
2242 device->recording->changed.vertexShaderConstantsI |= (1 << i);
2244 else
2246 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2249 return WINED3D_OK;
2252 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2253 UINT start_register, int *constants, UINT vector4i_count)
2255 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2257 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2258 device, start_register, constants, vector4i_count);
2260 if (!constants || start_register >= MAX_CONST_I)
2261 return WINED3DERR_INVALIDCALL;
2263 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2264 return WINED3D_OK;
2267 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2268 UINT start_register, const float *constants, UINT vector4f_count)
2270 UINT i;
2271 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2273 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2274 device, start_register, constants, vector4f_count);
2276 /* Specifically test start_register > limit to catch MAX_UINT overflows
2277 * when adding start_register + vector4f_count. */
2278 if (!constants
2279 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2280 || start_register > d3d_info->limits.vs_uniform_count)
2281 return WINED3DERR_INVALIDCALL;
2283 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2284 constants, vector4f_count * sizeof(float) * 4);
2285 if (TRACE_ON(d3d))
2287 for (i = 0; i < vector4f_count; ++i)
2288 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2289 constants[i * 4], constants[i * 4 + 1],
2290 constants[i * 4 + 2], constants[i * 4 + 3]);
2293 if (device->recording)
2294 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2295 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2296 else
2297 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2300 return WINED3D_OK;
2303 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2304 UINT start_register, float *constants, UINT vector4f_count)
2306 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2307 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2309 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2310 device, start_register, constants, vector4f_count);
2312 if (!constants || count < 0)
2313 return WINED3DERR_INVALIDCALL;
2315 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2317 return WINED3D_OK;
2320 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2322 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2324 TRACE("device %p, shader %p.\n", device, shader);
2326 if (device->recording)
2327 device->recording->changed.pixelShader = TRUE;
2329 if (shader == prev)
2330 return;
2332 if (shader)
2333 wined3d_shader_incref(shader);
2334 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2335 if (!device->recording)
2336 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2337 if (prev)
2338 wined3d_shader_decref(prev);
2341 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2343 TRACE("device %p.\n", device);
2345 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2348 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2350 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2352 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2355 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2357 TRACE("device %p, idx %u.\n", device, idx);
2359 if (idx >= MAX_CONSTANT_BUFFERS)
2361 WARN("Invalid constant buffer index %u.\n", idx);
2362 return NULL;
2365 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2368 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2370 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2372 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2375 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2377 TRACE("device %p, idx %u.\n", device, idx);
2379 if (idx >= MAX_SAMPLER_OBJECTS)
2381 WARN("Invalid sampler index %u.\n", idx);
2382 return NULL;
2385 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2388 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2389 UINT start_register, const BOOL *constants, UINT bool_count)
2391 UINT count = min(bool_count, MAX_CONST_B - start_register);
2392 UINT i;
2394 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2395 device, start_register, constants, bool_count);
2397 if (!constants || start_register >= MAX_CONST_B)
2398 return WINED3DERR_INVALIDCALL;
2400 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2401 for (i = 0; i < count; ++i)
2402 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2404 if (device->recording)
2406 for (i = start_register; i < count + start_register; ++i)
2407 device->recording->changed.pixelShaderConstantsB |= (1 << i);
2409 else
2411 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2414 return WINED3D_OK;
2417 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2418 UINT start_register, BOOL *constants, UINT bool_count)
2420 UINT count = min(bool_count, MAX_CONST_B - start_register);
2422 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2423 device, start_register, constants, bool_count);
2425 if (!constants || start_register >= MAX_CONST_B)
2426 return WINED3DERR_INVALIDCALL;
2428 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2430 return WINED3D_OK;
2433 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2434 UINT start_register, const int *constants, UINT vector4i_count)
2436 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2437 UINT i;
2439 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2440 device, start_register, constants, vector4i_count);
2442 if (!constants || start_register >= MAX_CONST_I)
2443 return WINED3DERR_INVALIDCALL;
2445 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2446 for (i = 0; i < count; ++i)
2447 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2448 constants[i * 4], constants[i * 4 + 1],
2449 constants[i * 4 + 2], constants[i * 4 + 3]);
2451 if (device->recording)
2453 for (i = start_register; i < count + start_register; ++i)
2454 device->recording->changed.pixelShaderConstantsI |= (1 << i);
2456 else
2458 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2461 return WINED3D_OK;
2464 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2465 UINT start_register, int *constants, UINT vector4i_count)
2467 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2469 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2470 device, start_register, constants, vector4i_count);
2472 if (!constants || start_register >= MAX_CONST_I)
2473 return WINED3DERR_INVALIDCALL;
2475 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2477 return WINED3D_OK;
2480 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2481 UINT start_register, const float *constants, UINT vector4f_count)
2483 UINT i;
2484 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2486 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2487 device, start_register, constants, vector4f_count);
2489 /* Specifically test start_register > limit to catch MAX_UINT overflows
2490 * when adding start_register + vector4f_count. */
2491 if (!constants
2492 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2493 || start_register > d3d_info->limits.ps_uniform_count)
2494 return WINED3DERR_INVALIDCALL;
2496 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2497 constants, vector4f_count * sizeof(float) * 4);
2498 if (TRACE_ON(d3d))
2500 for (i = 0; i < vector4f_count; ++i)
2501 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2502 constants[i * 4], constants[i * 4 + 1],
2503 constants[i * 4 + 2], constants[i * 4 + 3]);
2506 if (device->recording)
2507 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2508 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2509 else
2510 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2512 return WINED3D_OK;
2515 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2516 UINT start_register, float *constants, UINT vector4f_count)
2518 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2519 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2521 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2522 device, start_register, constants, vector4f_count);
2524 if (!constants || count < 0)
2525 return WINED3DERR_INVALIDCALL;
2527 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2529 return WINED3D_OK;
2532 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2534 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2536 TRACE("device %p, shader %p.\n", device, shader);
2538 if (device->recording || shader == prev)
2539 return;
2540 if (shader)
2541 wined3d_shader_incref(shader);
2542 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2543 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2544 if (prev)
2545 wined3d_shader_decref(prev);
2548 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2550 TRACE("device %p.\n", device);
2552 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2555 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2557 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2559 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2562 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2564 TRACE("device %p, idx %u.\n", device, idx);
2566 if (idx >= MAX_CONSTANT_BUFFERS)
2568 WARN("Invalid constant buffer index %u.\n", idx);
2569 return NULL;
2572 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2575 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2577 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2579 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2582 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2584 TRACE("device %p, idx %u.\n", device, idx);
2586 if (idx >= MAX_SAMPLER_OBJECTS)
2588 WARN("Invalid sampler index %u.\n", idx);
2589 return NULL;
2592 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2595 /* Context activation is done by the caller. */
2596 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2597 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2598 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2599 DWORD DestFVF)
2601 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2602 struct wined3d_viewport vp;
2603 UINT vertex_size;
2604 unsigned int i;
2605 BYTE *dest_ptr;
2606 BOOL doClip;
2607 DWORD numTextures;
2608 HRESULT hr;
2610 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
2612 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2615 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
2617 ERR("Source has no position mask\n");
2618 return WINED3DERR_INVALIDCALL;
2621 if (device->state.render_states[WINED3D_RS_CLIPPING])
2623 static BOOL warned = FALSE;
2625 * The clipping code is not quite correct. Some things need
2626 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2627 * so disable clipping for now.
2628 * (The graphics in Half-Life are broken, and my processvertices
2629 * test crashes with IDirect3DDevice3)
2630 doClip = TRUE;
2632 doClip = FALSE;
2633 if(!warned) {
2634 warned = TRUE;
2635 FIXME("Clipping is broken and disabled for now\n");
2638 else
2639 doClip = FALSE;
2641 vertex_size = get_flexible_vertex_size(DestFVF);
2642 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2644 WARN("Failed to map buffer, hr %#x.\n", hr);
2645 return hr;
2648 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2649 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2650 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2652 TRACE("View mat:\n");
2653 TRACE("%f %f %f %f\n", view_mat.u.s._11, view_mat.u.s._12, view_mat.u.s._13, view_mat.u.s._14);
2654 TRACE("%f %f %f %f\n", view_mat.u.s._21, view_mat.u.s._22, view_mat.u.s._23, view_mat.u.s._24);
2655 TRACE("%f %f %f %f\n", view_mat.u.s._31, view_mat.u.s._32, view_mat.u.s._33, view_mat.u.s._34);
2656 TRACE("%f %f %f %f\n", view_mat.u.s._41, view_mat.u.s._42, view_mat.u.s._43, view_mat.u.s._44);
2658 TRACE("Proj mat:\n");
2659 TRACE("%f %f %f %f\n", proj_mat.u.s._11, proj_mat.u.s._12, proj_mat.u.s._13, proj_mat.u.s._14);
2660 TRACE("%f %f %f %f\n", proj_mat.u.s._21, proj_mat.u.s._22, proj_mat.u.s._23, proj_mat.u.s._24);
2661 TRACE("%f %f %f %f\n", proj_mat.u.s._31, proj_mat.u.s._32, proj_mat.u.s._33, proj_mat.u.s._34);
2662 TRACE("%f %f %f %f\n", proj_mat.u.s._41, proj_mat.u.s._42, proj_mat.u.s._43, proj_mat.u.s._44);
2664 TRACE("World mat:\n");
2665 TRACE("%f %f %f %f\n", world_mat.u.s._11, world_mat.u.s._12, world_mat.u.s._13, world_mat.u.s._14);
2666 TRACE("%f %f %f %f\n", world_mat.u.s._21, world_mat.u.s._22, world_mat.u.s._23, world_mat.u.s._24);
2667 TRACE("%f %f %f %f\n", world_mat.u.s._31, world_mat.u.s._32, world_mat.u.s._33, world_mat.u.s._34);
2668 TRACE("%f %f %f %f\n", world_mat.u.s._41, world_mat.u.s._42, world_mat.u.s._43, world_mat.u.s._44);
2670 /* Get the viewport */
2671 wined3d_device_get_viewport(device, &vp);
2672 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2673 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2675 multiply_matrix(&mat,&view_mat,&world_mat);
2676 multiply_matrix(&mat,&proj_mat,&mat);
2678 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2680 for (i = 0; i < dwCount; i+= 1) {
2681 unsigned int tex_index;
2683 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2684 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2685 /* The position first */
2686 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2687 const float *p = (const float *)(element->data.addr + i * element->stride);
2688 float x, y, z, rhw;
2689 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2691 /* Multiplication with world, view and projection matrix */
2692 x = (p[0] * mat.u.s._11) + (p[1] * mat.u.s._21) + (p[2] * mat.u.s._31) + (1.0f * mat.u.s._41);
2693 y = (p[0] * mat.u.s._12) + (p[1] * mat.u.s._22) + (p[2] * mat.u.s._32) + (1.0f * mat.u.s._42);
2694 z = (p[0] * mat.u.s._13) + (p[1] * mat.u.s._23) + (p[2] * mat.u.s._33) + (1.0f * mat.u.s._43);
2695 rhw = (p[0] * mat.u.s._14) + (p[1] * mat.u.s._24) + (p[2] * mat.u.s._34) + (1.0f * mat.u.s._44);
2697 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2699 /* WARNING: The following things are taken from d3d7 and were not yet checked
2700 * against d3d8 or d3d9!
2703 /* Clipping conditions: From msdn
2705 * A vertex is clipped if it does not match the following requirements
2706 * -rhw < x <= rhw
2707 * -rhw < y <= rhw
2708 * 0 < z <= rhw
2709 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2711 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2712 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2716 if( !doClip ||
2717 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2718 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2719 ( rhw > eps ) ) ) {
2721 /* "Normal" viewport transformation (not clipped)
2722 * 1) The values are divided by rhw
2723 * 2) The y axis is negative, so multiply it with -1
2724 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2725 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2726 * 4) Multiply x with Width/2 and add Width/2
2727 * 5) The same for the height
2728 * 6) Add the viewpoint X and Y to the 2D coordinates and
2729 * The minimum Z value to z
2730 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2732 * Well, basically it's simply a linear transformation into viewport
2733 * coordinates
2736 x /= rhw;
2737 y /= rhw;
2738 z /= rhw;
2740 y *= -1;
2742 x *= vp.width / 2;
2743 y *= vp.height / 2;
2744 z *= vp.max_z - vp.min_z;
2746 x += vp.width / 2 + vp.x;
2747 y += vp.height / 2 + vp.y;
2748 z += vp.min_z;
2750 rhw = 1 / rhw;
2751 } else {
2752 /* That vertex got clipped
2753 * Contrary to OpenGL it is not dropped completely, it just
2754 * undergoes a different calculation.
2756 TRACE("Vertex got clipped\n");
2757 x += rhw;
2758 y += rhw;
2760 x /= 2;
2761 y /= 2;
2763 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2764 * outside of the main vertex buffer memory. That needs some more
2765 * investigation...
2769 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2772 ( (float *) dest_ptr)[0] = x;
2773 ( (float *) dest_ptr)[1] = y;
2774 ( (float *) dest_ptr)[2] = z;
2775 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2777 dest_ptr += 3 * sizeof(float);
2779 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2780 dest_ptr += sizeof(float);
2783 if (DestFVF & WINED3DFVF_PSIZE)
2784 dest_ptr += sizeof(DWORD);
2786 if (DestFVF & WINED3DFVF_NORMAL)
2788 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2789 const float *normal = (const float *)(element->data.addr + i * element->stride);
2790 /* AFAIK this should go into the lighting information */
2791 FIXME("Didn't expect the destination to have a normal\n");
2792 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2795 if (DestFVF & WINED3DFVF_DIFFUSE)
2797 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2798 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2799 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
2801 static BOOL warned = FALSE;
2803 if(!warned) {
2804 ERR("No diffuse color in source, but destination has one\n");
2805 warned = TRUE;
2808 *( (DWORD *) dest_ptr) = 0xffffffff;
2809 dest_ptr += sizeof(DWORD);
2811 else
2813 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2817 if (DestFVF & WINED3DFVF_SPECULAR)
2819 /* What's the color value in the feedback buffer? */
2820 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2821 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2822 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
2824 static BOOL warned = FALSE;
2826 if(!warned) {
2827 ERR("No specular color in source, but destination has one\n");
2828 warned = TRUE;
2831 *(DWORD *)dest_ptr = 0xff000000;
2832 dest_ptr += sizeof(DWORD);
2834 else
2836 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2840 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2842 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2843 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2844 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2846 ERR("No source texture, but destination requests one\n");
2847 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2849 else
2851 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2856 wined3d_buffer_unmap(dest);
2858 return WINED3D_OK;
2860 #undef copy_and_next
2862 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
2863 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
2864 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
2866 struct wined3d_state *state = &device->state;
2867 struct wined3d_stream_info stream_info;
2868 const struct wined3d_gl_info *gl_info;
2869 struct wined3d_context *context;
2870 struct wined3d_shader *vs;
2871 unsigned int i;
2872 HRESULT hr;
2874 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
2875 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
2876 device, src_start_idx, dst_idx, vertex_count,
2877 dst_buffer, declaration, flags, dst_fvf);
2879 if (declaration)
2880 FIXME("Output vertex declaration not implemented yet.\n");
2882 /* Need any context to write to the vbo. */
2883 context = context_acquire(device, NULL);
2884 gl_info = context->gl_info;
2886 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
2887 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
2888 context_stream_info_from_declaration(context, state, &stream_info);
2889 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
2891 /* We can't convert FROM a VBO, and vertex buffers used to source into
2892 * process_vertices() are unlikely to ever be used for drawing. Release
2893 * VBOs in those buffers and fix up the stream_info structure.
2895 * Also apply the start index. */
2896 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
2898 struct wined3d_stream_info_element *e;
2900 if (!(stream_info.use_map & (1 << i)))
2901 continue;
2903 e = &stream_info.elements[i];
2904 if (e->data.buffer_object)
2906 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
2907 e->data.buffer_object = 0;
2908 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, context));
2909 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
2910 vb->buffer_object = 0;
2912 if (e->data.addr)
2913 e->data.addr += e->stride * src_start_idx;
2916 hr = process_vertices_strided(device, dst_idx, vertex_count,
2917 &stream_info, dst_buffer, flags, dst_fvf);
2919 context_release(context);
2921 return hr;
2924 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
2925 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
2927 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2928 DWORD old_value;
2930 TRACE("device %p, stage %u, state %s, value %#x.\n",
2931 device, stage, debug_d3dtexturestate(state), value);
2933 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
2935 WARN("Invalid state %#x passed.\n", state);
2936 return;
2939 if (stage >= d3d_info->limits.ffp_blend_stages)
2941 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
2942 stage, d3d_info->limits.ffp_blend_stages - 1);
2943 return;
2946 old_value = device->update_state->texture_states[stage][state];
2947 device->update_state->texture_states[stage][state] = value;
2949 if (device->recording)
2951 TRACE("Recording... not performing anything.\n");
2952 device->recording->changed.textureState[stage] |= 1 << state;
2953 return;
2956 /* Checked after the assignments to allow proper stateblock recording. */
2957 if (old_value == value)
2959 TRACE("Application is setting the old value over, nothing to do.\n");
2960 return;
2963 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
2966 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
2967 UINT stage, enum wined3d_texture_stage_state state)
2969 TRACE("device %p, stage %u, state %s.\n",
2970 device, stage, debug_d3dtexturestate(state));
2972 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
2974 WARN("Invalid state %#x passed.\n", state);
2975 return 0;
2978 return device->state.texture_states[stage][state];
2981 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
2982 UINT stage, struct wined3d_texture *texture)
2984 struct wined3d_texture *prev;
2986 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
2988 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
2989 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2991 /* Windows accepts overflowing this array... we do not. */
2992 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
2994 WARN("Ignoring invalid stage %u.\n", stage);
2995 return WINED3D_OK;
2998 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3000 WARN("Rejecting attempt to set scratch texture.\n");
3001 return WINED3DERR_INVALIDCALL;
3004 if (device->recording)
3005 device->recording->changed.textures |= 1 << stage;
3007 prev = device->update_state->textures[stage];
3008 TRACE("Previous texture %p.\n", prev);
3010 if (texture == prev)
3012 TRACE("App is setting the same texture again, nothing to do.\n");
3013 return WINED3D_OK;
3016 TRACE("Setting new texture to %p.\n", texture);
3017 device->update_state->textures[stage] = texture;
3019 if (texture)
3020 wined3d_texture_incref(texture);
3021 if (!device->recording)
3022 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3023 if (prev)
3024 wined3d_texture_decref(prev);
3026 return WINED3D_OK;
3029 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3031 TRACE("device %p, stage %u.\n", device, stage);
3033 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3034 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3036 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3038 WARN("Ignoring invalid stage %u.\n", stage);
3039 return NULL; /* Windows accepts overflowing this array ... we do not. */
3042 return device->state.textures[stage];
3045 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3046 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3048 struct wined3d_swapchain *swapchain;
3050 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3051 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3053 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3054 return WINED3DERR_INVALIDCALL;
3056 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3057 return WINED3DERR_INVALIDCALL;
3058 return WINED3D_OK;
3061 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3063 TRACE("device %p, caps %p.\n", device, caps);
3065 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3066 device->create_parms.device_type, caps);
3069 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3070 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3072 struct wined3d_swapchain *swapchain;
3074 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3075 device, swapchain_idx, mode, rotation);
3077 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3078 return WINED3DERR_INVALIDCALL;
3080 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3083 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3085 struct wined3d_stateblock *stateblock;
3086 HRESULT hr;
3088 TRACE("device %p.\n", device);
3090 if (device->recording)
3091 return WINED3DERR_INVALIDCALL;
3093 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3094 if (FAILED(hr))
3095 return hr;
3097 device->recording = stateblock;
3098 device->update_state = &stateblock->state;
3100 TRACE("Recording stateblock %p.\n", stateblock);
3102 return WINED3D_OK;
3105 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3106 struct wined3d_stateblock **stateblock)
3108 struct wined3d_stateblock *object = device->recording;
3110 TRACE("device %p, stateblock %p.\n", device, stateblock);
3112 if (!device->recording)
3114 WARN("Not recording.\n");
3115 *stateblock = NULL;
3116 return WINED3DERR_INVALIDCALL;
3119 stateblock_init_contained_states(object);
3121 *stateblock = object;
3122 device->recording = NULL;
3123 device->update_state = &device->state;
3125 TRACE("Returning stateblock %p.\n", *stateblock);
3127 return WINED3D_OK;
3130 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3132 /* At the moment we have no need for any functionality at the beginning
3133 * of a scene. */
3134 TRACE("device %p.\n", device);
3136 if (device->inScene)
3138 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3139 return WINED3DERR_INVALIDCALL;
3141 device->inScene = TRUE;
3142 return WINED3D_OK;
3145 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3147 struct wined3d_context *context;
3149 TRACE("device %p.\n", device);
3151 if (!device->inScene)
3153 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3154 return WINED3DERR_INVALIDCALL;
3157 context = context_acquire(device, NULL);
3158 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3159 context->gl_info->gl_ops.gl.p_glFlush();
3160 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3161 * fails. */
3162 context_release(context);
3164 device->inScene = FALSE;
3165 return WINED3D_OK;
3168 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3169 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3171 UINT i;
3173 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3174 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3175 dst_window_override, dirty_region, flags);
3177 for (i = 0; i < device->swapchain_count; ++i)
3179 wined3d_swapchain_present(device->swapchains[i], src_rect,
3180 dst_rect, dst_window_override, dirty_region, flags);
3183 return WINED3D_OK;
3186 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3187 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3189 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3190 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3192 if (!rect_count && rects)
3194 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3195 return WINED3D_OK;
3198 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3200 struct wined3d_surface *ds = device->fb.depth_stencil;
3201 if (!ds)
3203 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3204 /* TODO: What about depth stencil buffers without stencil bits? */
3205 return WINED3DERR_INVALIDCALL;
3207 else if (flags & WINED3DCLEAR_TARGET)
3209 if (ds->resource.width < device->fb.render_targets[0]->resource.width
3210 || ds->resource.height < device->fb.render_targets[0]->resource.height)
3212 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3213 return WINED3D_OK;
3218 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3220 return WINED3D_OK;
3223 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3224 enum wined3d_primitive_type primitive_type)
3226 GLenum gl_primitive_type, prev;
3228 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3230 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3231 prev = device->update_state->gl_primitive_type;
3232 device->update_state->gl_primitive_type = gl_primitive_type;
3233 if (device->recording)
3234 device->recording->changed.primitive_type = TRUE;
3235 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3236 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
3239 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3240 enum wined3d_primitive_type *primitive_type)
3242 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3244 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3246 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3249 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3251 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3253 if (!device->state.vertex_declaration)
3255 WARN("Called without a valid vertex declaration set.\n");
3256 return WINED3DERR_INVALIDCALL;
3259 if (device->state.load_base_vertex_index)
3261 device->state.load_base_vertex_index = 0;
3262 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3265 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3267 return WINED3D_OK;
3270 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3272 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3274 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3276 if (!device->state.index_buffer)
3278 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3279 * without an index buffer set. (The first time at least...)
3280 * D3D8 simply dies, but I doubt it can do much harm to return
3281 * D3DERR_INVALIDCALL there as well. */
3282 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3283 return WINED3DERR_INVALIDCALL;
3286 if (!device->state.vertex_declaration)
3288 WARN("Called without a valid vertex declaration set.\n");
3289 return WINED3DERR_INVALIDCALL;
3292 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3293 device->state.load_base_vertex_index != device->state.base_vertex_index)
3295 device->state.load_base_vertex_index = device->state.base_vertex_index;
3296 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3299 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3301 return WINED3D_OK;
3304 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3305 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3307 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3309 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3312 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3313 static HRESULT device_update_volume(struct wined3d_device *device,
3314 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3316 struct wined3d_map_desc src;
3317 HRESULT hr;
3318 struct wined3d_bo_address data;
3319 struct wined3d_context *context;
3321 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3322 device, src_volume, dst_volume);
3324 if (src_volume->resource.format != dst_volume->resource.format)
3326 FIXME("Source and destination formats do not match.\n");
3327 return WINED3DERR_INVALIDCALL;
3329 if (src_volume->resource.width != dst_volume->resource.width
3330 || src_volume->resource.height != dst_volume->resource.height
3331 || src_volume->resource.depth != dst_volume->resource.depth)
3333 FIXME("Source and destination sizes do not match.\n");
3334 return WINED3DERR_INVALIDCALL;
3337 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3338 return hr;
3340 context = context_acquire(device, NULL);
3342 wined3d_volume_load(dst_volume, context, FALSE);
3344 data.buffer_object = 0;
3345 data.addr = src.data;
3346 wined3d_volume_upload_data(dst_volume, context, &data);
3347 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3349 context_release(context);
3351 hr = wined3d_volume_unmap(src_volume);
3353 return hr;
3356 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3357 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3359 enum wined3d_resource_type type;
3360 unsigned int level_count, i;
3361 HRESULT hr;
3362 struct wined3d_context *context;
3364 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3366 /* Verify that the source and destination textures are non-NULL. */
3367 if (!src_texture || !dst_texture)
3369 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3370 return WINED3DERR_INVALIDCALL;
3373 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3375 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3376 return WINED3DERR_INVALIDCALL;
3378 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3380 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3381 return WINED3DERR_INVALIDCALL;
3384 /* Verify that the source and destination textures are the same type. */
3385 type = src_texture->resource.type;
3386 if (dst_texture->resource.type != type)
3388 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3389 return WINED3DERR_INVALIDCALL;
3392 /* Check that both textures have the identical numbers of levels. */
3393 level_count = wined3d_texture_get_level_count(src_texture);
3394 if (wined3d_texture_get_level_count(dst_texture) != level_count)
3396 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
3397 return WINED3DERR_INVALIDCALL;
3400 /* Make sure that the destination texture is loaded. */
3401 context = context_acquire(device, NULL);
3402 wined3d_texture_load(dst_texture, context, FALSE);
3403 context_release(context);
3405 /* Update every surface level of the texture. */
3406 switch (type)
3408 case WINED3D_RTYPE_TEXTURE:
3410 struct wined3d_surface *src_surface;
3411 struct wined3d_surface *dst_surface;
3413 for (i = 0; i < level_count; ++i)
3415 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3416 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3417 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3418 if (FAILED(hr))
3420 WARN("Failed to update surface, hr %#x.\n", hr);
3421 return hr;
3424 break;
3427 case WINED3D_RTYPE_CUBE_TEXTURE:
3429 struct wined3d_surface *src_surface;
3430 struct wined3d_surface *dst_surface;
3432 for (i = 0; i < level_count * 6; ++i)
3434 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3435 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3436 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3437 if (FAILED(hr))
3439 WARN("Failed to update surface, hr %#x.\n", hr);
3440 return hr;
3443 break;
3446 case WINED3D_RTYPE_VOLUME_TEXTURE:
3448 for (i = 0; i < level_count; ++i)
3450 hr = device_update_volume(device,
3451 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
3452 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3453 if (FAILED(hr))
3455 WARN("Failed to update volume, hr %#x.\n", hr);
3456 return hr;
3459 break;
3462 default:
3463 FIXME("Unsupported texture type %#x.\n", type);
3464 return WINED3DERR_INVALIDCALL;
3467 return WINED3D_OK;
3470 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3471 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3473 struct wined3d_swapchain *swapchain;
3475 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3477 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3478 return WINED3DERR_INVALIDCALL;
3480 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3483 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3485 const struct wined3d_state *state = &device->state;
3486 struct wined3d_texture *texture;
3487 DWORD i;
3489 TRACE("device %p, num_passes %p.\n", device, num_passes);
3491 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3493 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3495 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3496 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3498 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3500 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3501 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3504 texture = state->textures[i];
3505 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
3507 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3509 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3510 return E_FAIL;
3512 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3514 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3515 return E_FAIL;
3517 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3518 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3520 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3521 return E_FAIL;
3525 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3526 || state->render_states[WINED3D_RS_STENCILENABLE])
3528 struct wined3d_surface *ds = device->fb.depth_stencil;
3529 struct wined3d_surface *target = device->fb.render_targets[0];
3531 if(ds && target
3532 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
3534 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3535 return WINED3DERR_CONFLICTINGRENDERSTATE;
3539 /* return a sensible default */
3540 *num_passes = 1;
3542 TRACE("returning D3D_OK\n");
3543 return WINED3D_OK;
3546 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3548 static BOOL warned;
3550 TRACE("device %p, software %#x.\n", device, software);
3552 if (!warned)
3554 FIXME("device %p, software %#x stub!\n", device, software);
3555 warned = TRUE;
3558 device->softwareVertexProcessing = software;
3561 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3563 static BOOL warned;
3565 TRACE("device %p.\n", device);
3567 if (!warned)
3569 TRACE("device %p stub!\n", device);
3570 warned = TRUE;
3573 return device->softwareVertexProcessing;
3576 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3577 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3579 struct wined3d_swapchain *swapchain;
3581 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3582 device, swapchain_idx, raster_status);
3584 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3585 return WINED3DERR_INVALIDCALL;
3587 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3590 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3592 static BOOL warned;
3594 TRACE("device %p, segments %.8e.\n", device, segments);
3596 if (segments != 0.0f)
3598 if (!warned)
3600 FIXME("device %p, segments %.8e stub!\n", device, segments);
3601 warned = TRUE;
3605 return WINED3D_OK;
3608 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3610 static BOOL warned;
3612 TRACE("device %p.\n", device);
3614 if (!warned)
3616 FIXME("device %p stub!\n", device);
3617 warned = TRUE;
3620 return 0.0f;
3623 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3624 struct wined3d_surface *src_surface, const RECT *src_rect,
3625 struct wined3d_surface *dst_surface, const POINT *dst_point)
3627 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3628 device, src_surface, wine_dbgstr_rect(src_rect),
3629 dst_surface, wine_dbgstr_point(dst_point));
3631 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3633 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3634 src_surface, dst_surface);
3635 return WINED3DERR_INVALIDCALL;
3638 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3641 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
3642 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
3644 RECT r;
3646 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
3647 device, surface, wine_dbgstr_rect(rect),
3648 color->r, color->g, color->b, color->a);
3650 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3652 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
3653 return WINED3DERR_INVALIDCALL;
3656 if (!rect)
3658 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
3659 rect = &r;
3662 return surface_color_fill(surface, rect, color);
3665 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
3666 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
3668 struct wined3d_resource *resource;
3669 HRESULT hr;
3670 RECT rect;
3672 resource = rendertarget_view->resource;
3673 if (resource->type != WINED3D_RTYPE_SURFACE)
3675 FIXME("Only supported on surface resources\n");
3676 return;
3679 SetRect(&rect, 0, 0, resource->width, resource->height);
3680 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
3681 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
3684 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
3685 UINT render_target_idx)
3687 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
3689 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
3691 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3692 return NULL;
3695 return device->fb.render_targets[render_target_idx];
3698 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
3700 TRACE("device %p.\n", device);
3702 return device->fb.depth_stencil;
3705 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
3706 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
3708 struct wined3d_surface *prev;
3710 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
3711 device, render_target_idx, render_target, set_viewport);
3713 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
3715 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3716 return WINED3DERR_INVALIDCALL;
3719 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
3721 WARN("Surface %p doesn't have render target usage.\n", render_target);
3722 return WINED3DERR_INVALIDCALL;
3725 /* Set the viewport and scissor rectangles, if requested. Tests show that
3726 * stateblock recording is ignored, the change goes directly into the
3727 * primary stateblock. */
3728 if (!render_target_idx && set_viewport)
3730 struct wined3d_state *state = &device->state;
3732 state->viewport.x = 0;
3733 state->viewport.y = 0;
3734 state->viewport.width = render_target->resource.width;
3735 state->viewport.height = render_target->resource.height;
3736 state->viewport.min_z = 0.0f;
3737 state->viewport.max_z = 1.0f;
3738 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
3740 state->scissor_rect.top = 0;
3741 state->scissor_rect.left = 0;
3742 state->scissor_rect.right = render_target->resource.width;
3743 state->scissor_rect.bottom = render_target->resource.height;
3744 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
3748 prev = device->fb.render_targets[render_target_idx];
3749 if (render_target == prev)
3750 return WINED3D_OK;
3752 if (render_target)
3753 wined3d_surface_incref(render_target);
3754 device->fb.render_targets[render_target_idx] = render_target;
3755 wined3d_cs_emit_set_render_target(device->cs, render_target_idx, render_target);
3756 /* Release after the assignment, to prevent device_resource_released()
3757 * from seeing the surface as still in use. */
3758 if (prev)
3759 wined3d_surface_decref(prev);
3761 return WINED3D_OK;
3764 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
3766 struct wined3d_surface *prev = device->fb.depth_stencil;
3768 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
3769 device, depth_stencil, prev);
3771 if (prev == depth_stencil)
3773 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
3774 return;
3777 device->fb.depth_stencil = depth_stencil;
3778 if (depth_stencil)
3779 wined3d_surface_incref(depth_stencil);
3780 wined3d_cs_emit_set_depth_stencil(device->cs, depth_stencil);
3781 if (prev)
3782 wined3d_surface_decref(prev);
3785 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
3786 struct wined3d_surface *cursor_image)
3788 struct wined3d_resource_desc desc;
3789 struct wined3d_map_desc map_desc;
3790 struct wined3d_texture *texture;
3791 struct wined3d_surface *surface;
3792 BYTE *src_data, *dst_data;
3793 unsigned int src_pitch;
3794 unsigned int i;
3796 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
3798 ERR("Failed to map source surface.\n");
3799 return NULL;
3802 src_pitch = map_desc.row_pitch;
3803 src_data = map_desc.data;
3805 desc.resource_type = WINED3D_RTYPE_TEXTURE;
3806 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
3807 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
3808 desc.multisample_quality = 0;
3809 desc.usage = WINED3DUSAGE_DYNAMIC;
3810 desc.pool = WINED3D_POOL_DEFAULT;
3811 desc.width = cursor_image->resource.width;
3812 desc.height = cursor_image->resource.height;
3813 desc.depth = 1;
3814 desc.size = 0;
3816 if (FAILED(wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
3817 NULL, &wined3d_null_parent_ops, &texture)))
3819 ERR("Failed to create cursor texture.\n");
3820 wined3d_surface_unmap(cursor_image);
3821 return NULL;
3824 surface = surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3825 if (FAILED(wined3d_surface_map(surface, &map_desc, NULL, WINED3D_MAP_DISCARD)))
3827 ERR("Failed to map destination surface.\n");
3828 wined3d_texture_decref(texture);
3829 wined3d_surface_unmap(cursor_image);
3830 return NULL;
3833 dst_data = map_desc.data;
3835 for (i = 0; i < desc.height; ++i)
3836 memcpy(&dst_data[map_desc.row_pitch * i], &src_data[src_pitch * i], desc.width * 4);
3838 wined3d_surface_unmap(surface);
3839 wined3d_surface_unmap(cursor_image);
3841 return texture;
3844 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
3845 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
3847 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
3848 device, x_hotspot, y_hotspot, cursor_image);
3850 if (device->cursor_texture)
3852 wined3d_texture_decref(device->cursor_texture);
3853 device->cursor_texture = NULL;
3856 if (cursor_image)
3858 struct wined3d_display_mode mode;
3859 struct wined3d_map_desc map_desc;
3860 HRESULT hr;
3862 /* MSDN: Cursor must be A8R8G8B8 */
3863 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
3865 WARN("surface %p has an invalid format.\n", cursor_image);
3866 return WINED3DERR_INVALIDCALL;
3869 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
3871 ERR("Failed to get display mode, hr %#x.\n", hr);
3872 return WINED3DERR_INVALIDCALL;
3875 /* MSDN: Cursor must be smaller than the display mode */
3876 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
3878 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
3879 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
3880 mode.width, mode.height);
3881 return WINED3DERR_INVALIDCALL;
3884 /* TODO: MSDN: Cursor sizes must be a power of 2 */
3886 /* Do not store the surface's pointer because the application may
3887 * release it after setting the cursor image. Windows doesn't
3888 * addref the set surface, so we can't do this either without
3889 * creating circular refcount dependencies. */
3890 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
3892 ERR("Failed to create cursor texture.\n");
3893 return WINED3DERR_INVALIDCALL;
3896 device->cursorWidth = cursor_image->resource.width;
3897 device->cursorHeight = cursor_image->resource.height;
3899 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
3901 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
3902 ICONINFO cursorInfo;
3903 DWORD *maskBits;
3904 HCURSOR cursor;
3906 /* 32-bit user32 cursors ignore the alpha channel if it's all
3907 * zeroes, and use the mask instead. Fill the mask with all ones
3908 * to ensure we still get a fully transparent cursor. */
3909 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
3910 memset(maskBits, 0xff, mask_size);
3911 wined3d_surface_map(cursor_image, &map_desc, NULL,
3912 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
3913 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
3915 cursorInfo.fIcon = FALSE;
3916 cursorInfo.xHotspot = x_hotspot;
3917 cursorInfo.yHotspot = y_hotspot;
3918 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
3919 1, 1, maskBits);
3920 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
3921 1, 32, map_desc.data);
3922 wined3d_surface_unmap(cursor_image);
3923 /* Create our cursor and clean up. */
3924 cursor = CreateIconIndirect(&cursorInfo);
3925 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
3926 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
3927 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
3928 device->hardwareCursor = cursor;
3929 if (device->bCursorVisible) SetCursor( cursor );
3930 HeapFree(GetProcessHeap(), 0, maskBits);
3934 device->xHotSpot = x_hotspot;
3935 device->yHotSpot = y_hotspot;
3936 return WINED3D_OK;
3939 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
3940 int x_screen_space, int y_screen_space, DWORD flags)
3942 TRACE("device %p, x %d, y %d, flags %#x.\n",
3943 device, x_screen_space, y_screen_space, flags);
3945 device->xScreenSpace = x_screen_space;
3946 device->yScreenSpace = y_screen_space;
3948 if (device->hardwareCursor)
3950 POINT pt;
3952 GetCursorPos( &pt );
3953 if (x_screen_space == pt.x && y_screen_space == pt.y)
3954 return;
3955 SetCursorPos( x_screen_space, y_screen_space );
3957 /* Switch to the software cursor if position diverges from the hardware one. */
3958 GetCursorPos( &pt );
3959 if (x_screen_space != pt.x || y_screen_space != pt.y)
3961 if (device->bCursorVisible) SetCursor( NULL );
3962 DestroyCursor( device->hardwareCursor );
3963 device->hardwareCursor = 0;
3968 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
3970 BOOL oldVisible = device->bCursorVisible;
3972 TRACE("device %p, show %#x.\n", device, show);
3975 * When ShowCursor is first called it should make the cursor appear at the OS's last
3976 * known cursor position.
3978 if (show && !oldVisible)
3980 POINT pt;
3981 GetCursorPos(&pt);
3982 device->xScreenSpace = pt.x;
3983 device->yScreenSpace = pt.y;
3986 if (device->hardwareCursor)
3988 device->bCursorVisible = show;
3989 if (show)
3990 SetCursor(device->hardwareCursor);
3991 else
3992 SetCursor(NULL);
3994 else if (device->cursor_texture)
3996 device->bCursorVisible = show;
3999 return oldVisible;
4002 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4004 struct wined3d_resource *resource, *cursor;
4006 TRACE("device %p.\n", device);
4008 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4010 TRACE("Checking resource %p for eviction.\n", resource);
4012 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4014 TRACE("Evicting %p.\n", resource);
4015 resource->resource_ops->resource_unload(resource);
4019 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4020 device_invalidate_state(device, STATE_STREAMSRC);
4023 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4025 struct wined3d_resource *resource, *cursor;
4026 const struct wined3d_gl_info *gl_info;
4027 struct wined3d_context *context;
4028 struct wined3d_shader *shader;
4030 context = context_acquire(device, NULL);
4031 gl_info = context->gl_info;
4033 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4035 TRACE("Unloading resource %p.\n", resource);
4037 resource->resource_ops->resource_unload(resource);
4040 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4042 device->shader_backend->shader_destroy(shader);
4045 if (device->depth_blt_texture)
4047 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4048 device->depth_blt_texture = 0;
4051 device->blitter->free_private(device);
4052 device->shader_backend->shader_free_private(device);
4053 destroy_dummy_textures(device, gl_info);
4055 context_release(context);
4057 while (device->context_count)
4059 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4062 HeapFree(GetProcessHeap(), 0, swapchain->context);
4063 swapchain->context = NULL;
4066 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4068 struct wined3d_context *context;
4069 struct wined3d_surface *target;
4070 HRESULT hr;
4072 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4073 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4075 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4076 return hr;
4079 if (FAILED(hr = device->blitter->alloc_private(device)))
4081 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4082 device->shader_backend->shader_free_private(device);
4083 return hr;
4086 /* Recreate the primary swapchain's context */
4087 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4088 if (!swapchain->context)
4090 ERR("Failed to allocate memory for swapchain context array.\n");
4091 device->blitter->free_private(device);
4092 device->shader_backend->shader_free_private(device);
4093 return E_OUTOFMEMORY;
4096 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4097 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4099 WARN("Failed to create context.\n");
4100 device->blitter->free_private(device);
4101 device->shader_backend->shader_free_private(device);
4102 HeapFree(GetProcessHeap(), 0, swapchain->context);
4103 return E_FAIL;
4106 swapchain->context[0] = context;
4107 swapchain->num_contexts = 1;
4108 create_dummy_textures(device, context);
4109 context_release(context);
4111 return WINED3D_OK;
4114 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4115 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4116 wined3d_device_reset_cb callback, BOOL reset_state)
4118 struct wined3d_resource *resource, *cursor;
4119 struct wined3d_swapchain *swapchain;
4120 struct wined3d_display_mode m;
4121 BOOL DisplayModeChanged = FALSE;
4122 BOOL update_desc = FALSE;
4123 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4124 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4125 HRESULT hr = WINED3D_OK;
4126 unsigned int i;
4128 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4130 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4132 ERR("Failed to get the first implicit swapchain.\n");
4133 return WINED3DERR_INVALIDCALL;
4136 if (reset_state)
4138 if (device->logo_texture)
4140 wined3d_texture_decref(device->logo_texture);
4141 device->logo_texture = NULL;
4143 if (device->cursor_texture)
4145 wined3d_texture_decref(device->cursor_texture);
4146 device->cursor_texture = NULL;
4148 state_unbind_resources(&device->state);
4151 if (device->fb.render_targets)
4153 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4155 wined3d_device_set_render_target(device, i, NULL, FALSE);
4157 if (swapchain->back_buffers && swapchain->back_buffers[0])
4158 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
4160 wined3d_device_set_depth_stencil(device, NULL);
4162 if (device->onscreen_depth_stencil)
4164 wined3d_surface_decref(device->onscreen_depth_stencil);
4165 device->onscreen_depth_stencil = NULL;
4168 if (reset_state)
4170 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4172 TRACE("Enumerating resource %p.\n", resource);
4173 if (FAILED(hr = callback(resource)))
4174 return hr;
4178 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4179 * on an existing gl context, so there's no real need for recreation.
4181 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4183 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4185 TRACE("New params:\n");
4186 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4187 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4188 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4189 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4190 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4191 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4192 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4193 TRACE("device_window %p\n", swapchain_desc->device_window);
4194 TRACE("windowed %#x\n", swapchain_desc->windowed);
4195 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4196 if (swapchain_desc->enable_auto_depth_stencil)
4197 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4198 TRACE("flags %#x\n", swapchain_desc->flags);
4199 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4200 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4201 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4203 /* No special treatment of these parameters. Just store them */
4204 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4205 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4206 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4207 swapchain->desc.flags = swapchain_desc->flags;
4208 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4209 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4210 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4212 /* What to do about these? */
4213 if (swapchain_desc->backbuffer_count
4214 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4215 FIXME("Cannot change the back buffer count yet.\n");
4217 if (swapchain_desc->device_window
4218 && swapchain_desc->device_window != swapchain->desc.device_window)
4220 TRACE("Changing the device window from %p to %p.\n",
4221 swapchain->desc.device_window, swapchain_desc->device_window);
4222 swapchain->desc.device_window = swapchain_desc->device_window;
4223 swapchain->device_window = swapchain_desc->device_window;
4224 wined3d_swapchain_set_window(swapchain, NULL);
4227 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
4229 struct wined3d_resource_desc surface_desc;
4231 TRACE("Creating the depth stencil buffer\n");
4233 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
4234 surface_desc.format = swapchain_desc->auto_depth_stencil_format;
4235 surface_desc.multisample_type = swapchain_desc->multisample_type;
4236 surface_desc.multisample_quality = swapchain_desc->multisample_quality;
4237 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4238 surface_desc.pool = WINED3D_POOL_DEFAULT;
4239 surface_desc.width = swapchain_desc->backbuffer_width;
4240 surface_desc.height = swapchain_desc->backbuffer_height;
4241 surface_desc.depth = 1;
4242 surface_desc.size = 0;
4244 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4245 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
4247 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
4248 return WINED3DERR_INVALIDCALL;
4252 /* Reset the depth stencil */
4253 if (swapchain_desc->enable_auto_depth_stencil)
4254 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
4256 if (mode)
4258 DisplayModeChanged = TRUE;
4259 m = *mode;
4261 else if (swapchain_desc->windowed)
4263 m = swapchain->original_mode;
4265 else
4267 m.width = swapchain_desc->backbuffer_width;
4268 m.height = swapchain_desc->backbuffer_height;
4269 m.refresh_rate = swapchain_desc->refresh_rate;
4270 m.format_id = swapchain_desc->backbuffer_format;
4271 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4274 if (!backbuffer_width || !backbuffer_height)
4276 /* The application is requesting that either the swapchain width or
4277 * height be set to the corresponding dimension in the window's
4278 * client rect. */
4280 RECT client_rect;
4282 if (!swapchain_desc->windowed)
4283 return WINED3DERR_INVALIDCALL;
4285 if (!GetClientRect(swapchain->device_window, &client_rect))
4287 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
4288 return WINED3DERR_INVALIDCALL;
4291 if (!backbuffer_width)
4292 backbuffer_width = client_rect.right;
4294 if (!backbuffer_height)
4295 backbuffer_height = client_rect.bottom;
4298 if (backbuffer_width != swapchain->desc.backbuffer_width
4299 || backbuffer_height != swapchain->desc.backbuffer_height)
4301 if (!swapchain_desc->windowed)
4302 DisplayModeChanged = TRUE;
4304 swapchain->desc.backbuffer_width = backbuffer_width;
4305 swapchain->desc.backbuffer_height = backbuffer_height;
4306 update_desc = TRUE;
4309 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
4310 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
4312 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
4313 update_desc = TRUE;
4316 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
4317 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
4319 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
4320 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
4321 update_desc = TRUE;
4324 if (update_desc)
4326 UINT i;
4328 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
4329 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4330 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4331 return hr;
4333 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4335 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
4336 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4337 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4338 return hr;
4340 if (device->auto_depth_stencil)
4342 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
4343 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
4344 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4345 return hr;
4349 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4350 || DisplayModeChanged)
4352 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4354 WARN("Failed to set display mode, hr %#x.\n", hr);
4355 return WINED3DERR_INVALIDCALL;
4358 if (!swapchain_desc->windowed)
4360 if (swapchain->desc.windowed)
4362 HWND focus_window = device->create_parms.focus_window;
4363 if (!focus_window)
4364 focus_window = swapchain_desc->device_window;
4365 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4367 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4368 return hr;
4371 /* switch from windowed to fs */
4372 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4373 swapchain_desc->backbuffer_width,
4374 swapchain_desc->backbuffer_height);
4376 else
4378 /* Fullscreen -> fullscreen mode change */
4379 MoveWindow(swapchain->device_window, 0, 0,
4380 swapchain_desc->backbuffer_width,
4381 swapchain_desc->backbuffer_height,
4382 TRUE);
4385 else if (!swapchain->desc.windowed)
4387 /* Fullscreen -> windowed switch */
4388 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4389 wined3d_device_release_focus_window(device);
4391 swapchain->desc.windowed = swapchain_desc->windowed;
4393 else if (!swapchain_desc->windowed)
4395 DWORD style = device->style;
4396 DWORD exStyle = device->exStyle;
4397 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4398 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4399 * Reset to clear up their mess. Guild Wars also loses the device during that.
4401 device->style = 0;
4402 device->exStyle = 0;
4403 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4404 swapchain_desc->backbuffer_width,
4405 swapchain_desc->backbuffer_height);
4406 device->style = style;
4407 device->exStyle = exStyle;
4410 if (reset_state)
4412 TRACE("Resetting stateblock.\n");
4413 if (device->recording)
4415 wined3d_stateblock_decref(device->recording);
4416 device->recording = NULL;
4418 wined3d_cs_emit_reset_state(device->cs);
4419 state_cleanup(&device->state);
4421 if (device->d3d_initialized)
4422 delete_opengl_contexts(device, swapchain);
4424 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4425 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4426 ERR("Failed to initialize device state, hr %#x.\n", hr);
4427 device->update_state = &device->state;
4429 device_init_swapchain_state(device, swapchain);
4431 else
4433 struct wined3d_surface *rt = device->fb.render_targets[0];
4434 struct wined3d_state *state = &device->state;
4436 /* Note the min_z / max_z is not reset. */
4437 state->viewport.x = 0;
4438 state->viewport.y = 0;
4439 state->viewport.width = rt->resource.width;
4440 state->viewport.height = rt->resource.height;
4441 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4443 state->scissor_rect.top = 0;
4444 state->scissor_rect.left = 0;
4445 state->scissor_rect.right = rt->resource.width;
4446 state->scissor_rect.bottom = rt->resource.height;
4447 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4450 swapchain_update_render_to_fbo(swapchain);
4451 swapchain_update_draw_bindings(swapchain);
4453 if (reset_state && device->d3d_initialized)
4454 hr = create_primary_opengl_context(device, swapchain);
4456 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4457 * first use
4459 return hr;
4462 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4464 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4466 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4468 return WINED3D_OK;
4472 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4473 struct wined3d_device_creation_parameters *parameters)
4475 TRACE("device %p, parameters %p.\n", device, parameters);
4477 *parameters = device->create_parms;
4480 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4481 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4483 struct wined3d_swapchain *swapchain;
4485 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4486 device, swapchain_idx, flags, ramp);
4488 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4489 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4492 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4493 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4495 struct wined3d_swapchain *swapchain;
4497 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4498 device, swapchain_idx, ramp);
4500 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4501 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4504 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4506 TRACE("device %p, resource %p.\n", device, resource);
4508 list_add_head(&device->resources, &resource->resource_list_entry);
4511 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4513 TRACE("device %p, resource %p.\n", device, resource);
4515 list_remove(&resource->resource_list_entry);
4518 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4520 enum wined3d_resource_type type = resource->type;
4521 unsigned int i;
4523 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4525 context_resource_released(device, resource, type);
4527 switch (type)
4529 case WINED3D_RTYPE_SURFACE:
4531 struct wined3d_surface *surface = surface_from_resource(resource);
4533 if (!device->d3d_initialized) break;
4535 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4537 if (device->fb.render_targets[i] == surface)
4539 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4540 device->fb.render_targets[i] = NULL;
4544 if (device->fb.depth_stencil == surface)
4546 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4547 device->fb.depth_stencil = NULL;
4550 break;
4552 case WINED3D_RTYPE_TEXTURE:
4553 case WINED3D_RTYPE_CUBE_TEXTURE:
4554 case WINED3D_RTYPE_VOLUME_TEXTURE:
4555 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4557 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4559 if (device->state.textures[i] == texture)
4561 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4562 device->state.textures[i] = NULL;
4565 if (device->recording && device->update_state->textures[i] == texture)
4567 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4568 texture, device->recording, i);
4569 device->update_state->textures[i] = NULL;
4572 break;
4574 case WINED3D_RTYPE_BUFFER:
4576 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4578 for (i = 0; i < MAX_STREAMS; ++i)
4580 if (device->state.streams[i].buffer == buffer)
4582 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4583 device->state.streams[i].buffer = NULL;
4586 if (device->recording && device->update_state->streams[i].buffer == buffer)
4588 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4589 buffer, device->recording, i);
4590 device->update_state->streams[i].buffer = NULL;
4594 if (device->state.index_buffer == buffer)
4596 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4597 device->state.index_buffer = NULL;
4600 if (device->recording && device->update_state->index_buffer == buffer)
4602 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4603 buffer, device->recording);
4604 device->update_state->index_buffer = NULL;
4607 break;
4609 default:
4610 break;
4613 /* Remove the resource from the resourceStore */
4614 device_resource_remove(device, resource);
4616 TRACE("Resource released.\n");
4619 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4621 struct wined3d_resource *resource;
4623 TRACE("device %p, dc %p.\n", device, dc);
4625 if (!dc)
4626 return NULL;
4628 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4630 if (resource->type == WINED3D_RTYPE_SURFACE)
4632 struct wined3d_surface *s = surface_from_resource(resource);
4634 if (s->hDC == dc)
4636 TRACE("Found surface %p for dc %p.\n", s, dc);
4637 return s;
4642 return NULL;
4645 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4646 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4647 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4649 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4650 const struct fragment_pipeline *fragment_pipeline;
4651 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4652 unsigned int i;
4653 HRESULT hr;
4655 device->ref = 1;
4656 device->wined3d = wined3d;
4657 wined3d_incref(device->wined3d);
4658 device->adapter = wined3d->adapter_count ? adapter : NULL;
4659 device->device_parent = device_parent;
4660 list_init(&device->resources);
4661 list_init(&device->shaders);
4662 device->surface_alignment = surface_alignment;
4664 /* Save the creation parameters. */
4665 device->create_parms.adapter_idx = adapter_idx;
4666 device->create_parms.device_type = device_type;
4667 device->create_parms.focus_window = focus_window;
4668 device->create_parms.flags = flags;
4670 device->shader_backend = adapter->shader_backend;
4672 vertex_pipeline = adapter->vertex_pipe;
4674 fragment_pipeline = adapter->fragment_pipe;
4676 if (vertex_pipeline->vp_states && fragment_pipeline->states
4677 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4678 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4679 fragment_pipeline, misc_state_template)))
4681 ERR("Failed to compile state table, hr %#x.\n", hr);
4682 wined3d_decref(device->wined3d);
4683 return hr;
4686 device->blitter = adapter->blitter;
4688 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
4689 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4691 ERR("Failed to initialize device state, hr %#x.\n", hr);
4692 goto err;
4694 device->update_state = &device->state;
4696 if (!(device->cs = wined3d_cs_create(device)))
4698 WARN("Failed to create command stream.\n");
4699 state_cleanup(&device->state);
4700 hr = E_FAIL;
4701 goto err;
4704 return WINED3D_OK;
4706 err:
4707 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4709 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4711 wined3d_decref(device->wined3d);
4712 return hr;
4716 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4718 DWORD rep = device->StateTable[state].representative;
4719 struct wined3d_context *context;
4720 DWORD idx;
4721 BYTE shift;
4722 UINT i;
4724 for (i = 0; i < device->context_count; ++i)
4726 context = device->contexts[i];
4727 if(isStateDirty(context, rep)) continue;
4729 context->dirtyArray[context->numDirtyEntries++] = rep;
4730 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4731 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4732 context->isStateDirty[idx] |= (1 << shift);
4736 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
4738 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
4739 *width = context->current_rt->pow2Width;
4740 *height = context->current_rt->pow2Height;
4743 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
4745 const struct wined3d_swapchain *swapchain = context->swapchain;
4746 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
4747 * current context's drawable, which is the size of the back buffer of the swapchain
4748 * the active context belongs to. */
4749 *width = swapchain->desc.backbuffer_width;
4750 *height = swapchain->desc.backbuffer_height;
4753 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4754 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4756 if (device->filter_messages)
4758 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4759 window, message, wparam, lparam);
4760 if (unicode)
4761 return DefWindowProcW(window, message, wparam, lparam);
4762 else
4763 return DefWindowProcA(window, message, wparam, lparam);
4766 if (message == WM_DESTROY)
4768 TRACE("unregister window %p.\n", window);
4769 wined3d_unregister_window(window);
4771 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
4772 ERR("Window %p is not the focus window for device %p.\n", window, device);
4774 else if (message == WM_DISPLAYCHANGE)
4776 device->device_parent->ops->mode_changed(device->device_parent);
4778 else if (message == WM_ACTIVATEAPP)
4780 device->device_parent->ops->activate(device->device_parent, wparam);
4783 if (unicode)
4784 return CallWindowProcW(proc, window, message, wparam, lparam);
4785 else
4786 return CallWindowProcA(proc, window, message, wparam, lparam);