kernel32/tests: Fix timer tests compilation with __WINESRC__ defined.
[wine.git] / dlls / wined3d / device.c
blob8a1af6b43b0392d0d64edf318091f328af2c80cb
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, SFLAG_INTEXTURE);
206 surface_modify_ds_location(device->onscreen_depth_stencil, SFLAG_INTEXTURE,
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->flags & SFLAG_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->flags & 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, NULL);
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 : SFLAG_INDRAWABLE;
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 : SFLAG_INDRAWABLE;
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 HBITMAP hbm;
555 BITMAP bm;
556 HRESULT hr;
557 HDC dcb = NULL, dcs = NULL;
559 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
560 if(hbm)
562 GetObjectA(hbm, sizeof(BITMAP), &bm);
563 dcb = CreateCompatibleDC(NULL);
564 if(!dcb) goto out;
565 SelectObject(dcb, hbm);
567 else
569 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
570 * couldn't be loaded
572 memset(&bm, 0, sizeof(bm));
573 bm.bmWidth = 32;
574 bm.bmHeight = 32;
577 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0,
578 WINED3D_POOL_SYSTEM_MEM, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_MAPPABLE,
579 NULL, &wined3d_null_parent_ops, &device->logo_surface);
580 if (FAILED(hr))
582 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
583 goto out;
586 if (dcb)
588 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
589 goto out;
590 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
591 wined3d_surface_releasedc(device->logo_surface, dcs);
593 color_key.color_space_low_value = 0;
594 color_key.color_space_high_value = 0;
595 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
597 else
599 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
600 /* Fill the surface with a white color to show that wined3d is there */
601 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
604 out:
605 if (dcb) DeleteDC(dcb);
606 if (hbm) DeleteObject(hbm);
609 /* Context activation is done by the caller. */
610 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
612 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
613 unsigned int i, j, count;
614 /* Under DirectX you can sample even if no texture is bound, whereas
615 * OpenGL will only allow that when a valid texture is bound.
616 * We emulate this by creating dummy textures and binding them
617 * to each texture stage when the currently set D3D texture is NULL. */
619 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
620 for (i = 0; i < count; ++i)
622 DWORD color = 0x000000ff;
624 /* Make appropriate texture active */
625 context_active_texture(context, gl_info, i);
627 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
628 checkGLcall("glGenTextures");
629 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
631 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
632 checkGLcall("glBindTexture");
634 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
635 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
636 checkGLcall("glTexImage2D");
638 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
640 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
641 checkGLcall("glGenTextures");
642 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
644 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
645 checkGLcall("glBindTexture");
647 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
648 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
649 checkGLcall("glTexImage2D");
652 if (gl_info->supported[EXT_TEXTURE3D])
654 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
655 checkGLcall("glGenTextures");
656 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
658 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
659 checkGLcall("glBindTexture");
661 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
662 checkGLcall("glTexImage3D");
665 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
667 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
668 checkGLcall("glGenTextures");
669 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
671 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
672 checkGLcall("glBindTexture");
674 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
676 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
677 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
678 checkGLcall("glTexImage2D");
684 /* Context activation is done by the caller. */
685 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
687 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
689 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
691 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
692 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
695 if (gl_info->supported[EXT_TEXTURE3D])
697 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
698 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
701 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
703 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
704 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
707 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
708 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
710 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
711 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
712 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
713 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
716 static LONG fullscreen_style(LONG style)
718 /* Make sure the window is managed, otherwise we won't get keyboard input. */
719 style |= WS_POPUP | WS_SYSMENU;
720 style &= ~(WS_CAPTION | WS_THICKFRAME);
722 return style;
725 static LONG fullscreen_exstyle(LONG exstyle)
727 /* Filter out window decorations. */
728 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
730 return exstyle;
733 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
735 BOOL filter_messages;
736 LONG style, exstyle;
738 TRACE("Setting up window %p for fullscreen mode.\n", window);
740 if (device->style || device->exStyle)
742 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
743 window, device->style, device->exStyle);
746 device->style = GetWindowLongW(window, GWL_STYLE);
747 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
749 style = fullscreen_style(device->style);
750 exstyle = fullscreen_exstyle(device->exStyle);
752 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
753 device->style, device->exStyle, style, exstyle);
755 filter_messages = device->filter_messages;
756 device->filter_messages = TRUE;
758 SetWindowLongW(window, GWL_STYLE, style);
759 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
760 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
762 device->filter_messages = filter_messages;
765 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
767 BOOL filter_messages;
768 LONG style, exstyle;
770 if (!device->style && !device->exStyle) return;
772 style = GetWindowLongW(window, GWL_STYLE);
773 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
775 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
776 * application, and we want to ignore them in the test below, since it's
777 * not the application's fault that they changed. Additionally, we want to
778 * preserve the current status of these flags (i.e. don't restore them) to
779 * more closely emulate the behavior of Direct3D, which leaves these flags
780 * alone when returning to windowed mode. */
781 device->style ^= (device->style ^ style) & WS_VISIBLE;
782 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
784 TRACE("Restoring window style of window %p to %08x, %08x.\n",
785 window, device->style, device->exStyle);
787 filter_messages = device->filter_messages;
788 device->filter_messages = TRUE;
790 /* Only restore the style if the application didn't modify it during the
791 * fullscreen phase. Some applications change it before calling Reset()
792 * when switching between windowed and fullscreen modes (HL2), some
793 * depend on the original style (Eve Online). */
794 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
796 SetWindowLongW(window, GWL_STYLE, device->style);
797 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
799 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
801 device->filter_messages = filter_messages;
803 /* Delete the old values. */
804 device->style = 0;
805 device->exStyle = 0;
808 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
810 TRACE("device %p, window %p.\n", device, window);
812 if (!wined3d_register_window(window, device))
814 ERR("Failed to register window %p.\n", window);
815 return E_FAIL;
818 InterlockedExchangePointer((void **)&device->focus_window, window);
819 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
821 return WINED3D_OK;
824 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
826 TRACE("device %p.\n", device);
828 if (device->focus_window) wined3d_unregister_window(device->focus_window);
829 InterlockedExchangePointer((void **)&device->focus_window, NULL);
832 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
834 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
835 unsigned int i;
837 if (device->fb.render_targets)
839 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
841 wined3d_device_set_render_target(device, i, NULL, FALSE);
843 if (swapchain->back_buffers && swapchain->back_buffers[0])
844 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], TRUE);
847 wined3d_device_set_depth_stencil(device, ds_enable ? device->auto_depth_stencil : NULL);
848 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
851 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
852 struct wined3d_swapchain_desc *swapchain_desc)
854 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
855 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
856 struct wined3d_swapchain *swapchain = NULL;
857 struct wined3d_context *context;
858 DWORD clear_flags = 0;
859 HRESULT hr;
861 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
863 if (device->d3d_initialized)
864 return WINED3DERR_INVALIDCALL;
865 if (device->wined3d->flags & WINED3D_NO3D)
866 return WINED3DERR_INVALIDCALL;
868 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
869 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
871 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
872 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
874 TRACE("Shader private data couldn't be allocated\n");
875 goto err_out;
877 if (FAILED(hr = device->blitter->alloc_private(device)))
879 TRACE("Blitter private data couldn't be allocated\n");
880 goto err_out;
883 /* Setup the implicit swapchain. This also initializes a context. */
884 TRACE("Creating implicit swapchain\n");
885 hr = device->device_parent->ops->create_swapchain(device->device_parent,
886 swapchain_desc, &swapchain);
887 if (FAILED(hr))
889 WARN("Failed to create implicit swapchain\n");
890 goto err_out;
893 device->swapchain_count = 1;
894 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
895 if (!device->swapchains)
897 ERR("Out of memory!\n");
898 goto err_out;
900 device->swapchains[0] = swapchain;
901 device_init_swapchain_state(device, swapchain);
903 context = context_acquire(device, swapchain->front_buffer);
905 create_dummy_textures(device, context);
907 device->contexts[0]->last_was_rhw = 0;
909 switch (wined3d_settings.offscreen_rendering_mode)
911 case ORM_FBO:
912 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
913 break;
915 case ORM_BACKBUFFER:
917 if (context_get_current()->aux_buffers > 0)
919 TRACE("Using auxiliary buffer for offscreen rendering\n");
920 device->offscreenBuffer = GL_AUX0;
922 else
924 TRACE("Using back buffer for offscreen rendering\n");
925 device->offscreenBuffer = GL_BACK;
930 TRACE("All defaults now set up, leaving 3D init.\n");
932 context_release(context);
934 /* Clear the screen */
935 if (swapchain->back_buffers && swapchain->back_buffers[0])
936 clear_flags |= WINED3DCLEAR_TARGET;
937 if (swapchain_desc->enable_auto_depth_stencil)
938 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
939 if (clear_flags)
940 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
942 device->d3d_initialized = TRUE;
944 if (wined3d_settings.logo)
945 device_load_logo(device, wined3d_settings.logo);
946 return WINED3D_OK;
948 err_out:
949 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
950 HeapFree(GetProcessHeap(), 0, device->swapchains);
951 device->swapchain_count = 0;
952 if (swapchain)
953 wined3d_swapchain_decref(swapchain);
954 if (device->blit_priv)
955 device->blitter->free_private(device);
956 if (device->shader_priv)
957 device->shader_backend->shader_free_private(device);
959 return hr;
962 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
963 struct wined3d_swapchain_desc *swapchain_desc)
965 struct wined3d_swapchain *swapchain = NULL;
966 HRESULT hr;
968 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
970 /* Setup the implicit swapchain */
971 TRACE("Creating implicit swapchain\n");
972 hr = device->device_parent->ops->create_swapchain(device->device_parent,
973 swapchain_desc, &swapchain);
974 if (FAILED(hr))
976 WARN("Failed to create implicit swapchain\n");
977 goto err_out;
980 device->swapchain_count = 1;
981 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
982 if (!device->swapchains)
984 ERR("Out of memory!\n");
985 goto err_out;
987 device->swapchains[0] = swapchain;
988 return WINED3D_OK;
990 err_out:
991 wined3d_swapchain_decref(swapchain);
992 return hr;
995 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
997 struct wined3d_resource *resource, *cursor;
998 const struct wined3d_gl_info *gl_info;
999 struct wined3d_context *context;
1000 struct wined3d_surface *surface;
1001 UINT i;
1003 TRACE("device %p.\n", device);
1005 if (!device->d3d_initialized)
1006 return WINED3DERR_INVALIDCALL;
1008 /* Force making the context current again, to verify it is still valid
1009 * (workaround for broken drivers) */
1010 context_set_current(NULL);
1011 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1012 * it was created. Thus make sure a context is active for the glDelete* calls
1014 context = context_acquire(device, NULL);
1015 gl_info = context->gl_info;
1017 if (device->logo_surface)
1018 wined3d_surface_decref(device->logo_surface);
1020 state_unbind_resources(&device->state);
1022 /* Unload resources */
1023 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1025 TRACE("Unloading resource %p.\n", resource);
1027 resource->resource_ops->resource_unload(resource);
1030 /* Delete the mouse cursor texture */
1031 if (device->cursorTexture)
1033 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
1034 device->cursorTexture = 0;
1037 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1038 * private data, it might contain opengl pointers
1040 if (device->depth_blt_texture)
1042 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1043 device->depth_blt_texture = 0;
1046 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1047 device->blitter->free_private(device);
1048 device->shader_backend->shader_free_private(device);
1049 destroy_dummy_textures(device, gl_info);
1051 /* Release the buffers (with sanity checks)*/
1052 if (device->onscreen_depth_stencil)
1054 surface = device->onscreen_depth_stencil;
1055 device->onscreen_depth_stencil = NULL;
1056 wined3d_surface_decref(surface);
1059 if (device->fb.depth_stencil)
1061 surface = device->fb.depth_stencil;
1063 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1065 device->fb.depth_stencil = NULL;
1066 wined3d_surface_decref(surface);
1069 if (device->auto_depth_stencil)
1071 surface = device->auto_depth_stencil;
1072 device->auto_depth_stencil = NULL;
1073 if (wined3d_surface_decref(surface))
1074 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1077 for (i = 0; i < gl_info->limits.buffers; ++i)
1079 wined3d_device_set_render_target(device, i, NULL, FALSE);
1082 context_release(context);
1084 for (i = 0; i < device->swapchain_count; ++i)
1086 TRACE("Releasing the implicit swapchain %u.\n", i);
1087 if (wined3d_swapchain_decref(device->swapchains[i]))
1088 FIXME("Something's still holding the implicit swapchain.\n");
1091 HeapFree(GetProcessHeap(), 0, device->swapchains);
1092 device->swapchains = NULL;
1093 device->swapchain_count = 0;
1095 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1096 device->fb.render_targets = NULL;
1098 device->d3d_initialized = FALSE;
1100 return WINED3D_OK;
1103 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1105 unsigned int i;
1107 for (i = 0; i < device->swapchain_count; ++i)
1109 TRACE("Releasing the implicit swapchain %u.\n", i);
1110 if (wined3d_swapchain_decref(device->swapchains[i]))
1111 FIXME("Something's still holding the implicit swapchain.\n");
1114 HeapFree(GetProcessHeap(), 0, device->swapchains);
1115 device->swapchains = NULL;
1116 device->swapchain_count = 0;
1117 return WINED3D_OK;
1120 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1121 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1122 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1124 * There is no way to deactivate thread safety once it is enabled.
1126 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1128 TRACE("device %p.\n", device);
1130 /* For now just store the flag (needed in case of ddraw). */
1131 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1134 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1136 TRACE("device %p.\n", device);
1138 TRACE("Emulating %d MB, returning %d MB left.\n",
1139 device->adapter->TextureRam / (1024 * 1024),
1140 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1142 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1145 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1146 struct wined3d_buffer *buffer, UINT offset)
1148 struct wined3d_stream_output *stream;
1149 struct wined3d_buffer *prev_buffer;
1151 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1153 if (idx >= MAX_STREAM_OUT)
1155 WARN("Invalid stream output %u.\n", idx);
1156 return;
1159 stream = &device->update_state->stream_output[idx];
1160 prev_buffer = stream->buffer;
1162 if (buffer)
1163 wined3d_buffer_incref(buffer);
1164 stream->buffer = buffer;
1165 stream->offset = offset;
1166 if (!device->recording)
1167 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1168 if (prev_buffer)
1169 wined3d_buffer_decref(prev_buffer);
1172 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1173 UINT idx, UINT *offset)
1175 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1177 if (idx >= MAX_STREAM_OUT)
1179 WARN("Invalid stream output %u.\n", idx);
1180 return NULL;
1183 *offset = device->state.stream_output[idx].offset;
1184 return device->state.stream_output[idx].buffer;
1187 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1188 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1190 struct wined3d_stream_state *stream;
1191 struct wined3d_buffer *prev_buffer;
1193 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1194 device, stream_idx, buffer, offset, stride);
1196 if (stream_idx >= MAX_STREAMS)
1198 WARN("Stream index %u out of range.\n", stream_idx);
1199 return WINED3DERR_INVALIDCALL;
1201 else if (offset & 0x3)
1203 WARN("Offset %u is not 4 byte aligned.\n", offset);
1204 return WINED3DERR_INVALIDCALL;
1207 stream = &device->update_state->streams[stream_idx];
1208 prev_buffer = stream->buffer;
1210 if (device->recording)
1211 device->recording->changed.streamSource |= 1 << stream_idx;
1213 if (prev_buffer == buffer
1214 && stream->stride == stride
1215 && stream->offset == offset)
1217 TRACE("Application is setting the old values over, nothing to do.\n");
1218 return WINED3D_OK;
1221 stream->buffer = buffer;
1222 if (buffer)
1224 stream->stride = stride;
1225 stream->offset = offset;
1228 if (buffer)
1229 wined3d_buffer_incref(buffer);
1230 if (!device->recording)
1231 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1232 if (prev_buffer)
1233 wined3d_buffer_decref(prev_buffer);
1235 return WINED3D_OK;
1238 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1239 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1241 const struct wined3d_stream_state *stream;
1243 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1244 device, stream_idx, buffer, offset, stride);
1246 if (stream_idx >= MAX_STREAMS)
1248 WARN("Stream index %u out of range.\n", stream_idx);
1249 return WINED3DERR_INVALIDCALL;
1252 stream = &device->state.streams[stream_idx];
1253 *buffer = stream->buffer;
1254 if (*buffer)
1255 wined3d_buffer_incref(*buffer);
1256 if (offset)
1257 *offset = stream->offset;
1258 *stride = stream->stride;
1260 return WINED3D_OK;
1263 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1265 struct wined3d_stream_state *stream;
1266 UINT old_flags, old_freq;
1268 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1270 /* Verify input. At least in d3d9 this is invalid. */
1271 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1273 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1274 return WINED3DERR_INVALIDCALL;
1276 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1278 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1279 return WINED3DERR_INVALIDCALL;
1281 if (!divider)
1283 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1284 return WINED3DERR_INVALIDCALL;
1287 stream = &device->update_state->streams[stream_idx];
1288 old_flags = stream->flags;
1289 old_freq = stream->frequency;
1291 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1292 stream->frequency = divider & 0x7fffff;
1294 if (device->recording)
1295 device->recording->changed.streamFreq |= 1 << stream_idx;
1296 else if (stream->frequency != old_freq || stream->flags != old_flags)
1297 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1299 return WINED3D_OK;
1302 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1303 UINT stream_idx, UINT *divider)
1305 const struct wined3d_stream_state *stream;
1307 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1309 stream = &device->state.streams[stream_idx];
1310 *divider = stream->flags | stream->frequency;
1312 TRACE("Returning %#x.\n", *divider);
1314 return WINED3D_OK;
1317 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1318 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1320 TRACE("device %p, state %s, matrix %p.\n",
1321 device, debug_d3dtstype(d3dts), matrix);
1322 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1323 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1324 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1325 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1327 /* Handle recording of state blocks. */
1328 if (device->recording)
1330 TRACE("Recording... not performing anything.\n");
1331 device->recording->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1332 device->update_state->transforms[d3dts] = *matrix;
1333 return;
1336 /* If the new matrix is the same as the current one,
1337 * we cut off any further processing. this seems to be a reasonable
1338 * optimization because as was noticed, some apps (warcraft3 for example)
1339 * tend towards setting the same matrix repeatedly for some reason.
1341 * From here on we assume that the new matrix is different, wherever it matters. */
1342 if (!memcmp(&device->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1344 TRACE("The application is setting the same matrix over again.\n");
1345 return;
1348 device->state.transforms[d3dts] = *matrix;
1349 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1352 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1353 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1355 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1357 *matrix = device->state.transforms[state];
1360 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1361 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1363 const struct wined3d_matrix *mat;
1364 struct wined3d_matrix temp;
1366 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1368 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1369 * below means it will be recorded in a state block change, but it
1370 * works regardless where it is recorded.
1371 * If this is found to be wrong, change to StateBlock. */
1372 if (state > HIGHEST_TRANSFORMSTATE)
1374 WARN("Unhandled transform state %#x.\n", state);
1375 return;
1378 mat = &device->update_state->transforms[state];
1379 multiply_matrix(&temp, mat, matrix);
1381 /* Apply change via set transform - will reapply to eg. lights this way. */
1382 wined3d_device_set_transform(device, state, &temp);
1385 /* Note lights are real special cases. Although the device caps state only
1386 * e.g. 8 are supported, you can reference any indexes you want as long as
1387 * that number max are enabled at any one point in time. Therefore since the
1388 * indices can be anything, we need a hashmap of them. However, this causes
1389 * stateblock problems. When capturing the state block, I duplicate the
1390 * hashmap, but when recording, just build a chain pretty much of commands to
1391 * be replayed. */
1392 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1393 UINT light_idx, const struct wined3d_light *light)
1395 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1396 struct wined3d_light_info *object = NULL;
1397 struct list *e;
1398 float rho;
1400 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1402 /* Check the parameter range. Need for speed most wanted sets junk lights
1403 * which confuse the GL driver. */
1404 if (!light)
1405 return WINED3DERR_INVALIDCALL;
1407 switch (light->type)
1409 case WINED3D_LIGHT_POINT:
1410 case WINED3D_LIGHT_SPOT:
1411 case WINED3D_LIGHT_PARALLELPOINT:
1412 case WINED3D_LIGHT_GLSPOT:
1413 /* Incorrect attenuation values can cause the gl driver to crash.
1414 * Happens with Need for speed most wanted. */
1415 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1417 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1418 return WINED3DERR_INVALIDCALL;
1420 break;
1422 case WINED3D_LIGHT_DIRECTIONAL:
1423 /* Ignores attenuation */
1424 break;
1426 default:
1427 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1428 return WINED3DERR_INVALIDCALL;
1431 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1433 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1434 if (object->OriginalIndex == light_idx)
1435 break;
1436 object = NULL;
1439 if (!object)
1441 TRACE("Adding new light\n");
1442 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1443 if (!object)
1444 return E_OUTOFMEMORY;
1446 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1447 object->glIndex = -1;
1448 object->OriginalIndex = light_idx;
1451 /* Initialize the object. */
1452 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1453 light_idx, light->type,
1454 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1455 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1456 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1457 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1458 light->direction.x, light->direction.y, light->direction.z);
1459 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1460 light->range, light->falloff, light->theta, light->phi);
1462 /* Update the live definitions if the light is currently assigned a glIndex. */
1463 if (object->glIndex != -1 && !device->recording)
1465 if (object->OriginalParms.type != light->type)
1466 device_invalidate_state(device, STATE_LIGHT_TYPE);
1467 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1470 /* Save away the information. */
1471 object->OriginalParms = *light;
1473 switch (light->type)
1475 case WINED3D_LIGHT_POINT:
1476 /* Position */
1477 object->lightPosn[0] = light->position.x;
1478 object->lightPosn[1] = light->position.y;
1479 object->lightPosn[2] = light->position.z;
1480 object->lightPosn[3] = 1.0f;
1481 object->cutoff = 180.0f;
1482 /* FIXME: Range */
1483 break;
1485 case WINED3D_LIGHT_DIRECTIONAL:
1486 /* Direction */
1487 object->lightPosn[0] = -light->direction.x;
1488 object->lightPosn[1] = -light->direction.y;
1489 object->lightPosn[2] = -light->direction.z;
1490 object->lightPosn[3] = 0.0f;
1491 object->exponent = 0.0f;
1492 object->cutoff = 180.0f;
1493 break;
1495 case WINED3D_LIGHT_SPOT:
1496 /* Position */
1497 object->lightPosn[0] = light->position.x;
1498 object->lightPosn[1] = light->position.y;
1499 object->lightPosn[2] = light->position.z;
1500 object->lightPosn[3] = 1.0f;
1502 /* Direction */
1503 object->lightDirn[0] = light->direction.x;
1504 object->lightDirn[1] = light->direction.y;
1505 object->lightDirn[2] = light->direction.z;
1506 object->lightDirn[3] = 1.0f;
1508 /* opengl-ish and d3d-ish spot lights use too different models
1509 * for the light "intensity" as a function of the angle towards
1510 * the main light direction, so we only can approximate very
1511 * roughly. However, spot lights are rather rarely used in games
1512 * (if ever used at all). Furthermore if still used, probably
1513 * nobody pays attention to such details. */
1514 if (!light->falloff)
1516 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1517 * equations have the falloff resp. exponent parameter as an
1518 * exponent, so the spot light lighting will always be 1.0 for
1519 * both of them, and we don't have to care for the rest of the
1520 * rather complex calculation. */
1521 object->exponent = 0.0f;
1523 else
1525 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1526 if (rho < 0.0001f)
1527 rho = 0.0001f;
1528 object->exponent = -0.3f / logf(cosf(rho / 2));
1531 if (object->exponent > 128.0f)
1532 object->exponent = 128.0f;
1534 object->cutoff = (float)(light->phi * 90 / M_PI);
1535 /* FIXME: Range */
1536 break;
1538 default:
1539 FIXME("Unrecognized light type %#x.\n", light->type);
1542 return WINED3D_OK;
1545 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1546 UINT light_idx, struct wined3d_light *light)
1548 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1549 struct wined3d_light_info *light_info = NULL;
1550 struct list *e;
1552 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1554 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1556 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1557 if (light_info->OriginalIndex == light_idx)
1558 break;
1559 light_info = NULL;
1562 if (!light_info)
1564 TRACE("Light information requested but light not defined\n");
1565 return WINED3DERR_INVALIDCALL;
1568 *light = light_info->OriginalParms;
1569 return WINED3D_OK;
1572 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1574 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1575 struct wined3d_light_info *light_info = NULL;
1576 struct list *e;
1578 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1580 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1582 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1583 if (light_info->OriginalIndex == light_idx)
1584 break;
1585 light_info = NULL;
1587 TRACE("Found light %p.\n", light_info);
1589 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1590 if (!light_info)
1592 TRACE("Light enabled requested but light not defined, so defining one!\n");
1593 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1595 /* Search for it again! Should be fairly quick as near head of list. */
1596 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1598 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1599 if (light_info->OriginalIndex == light_idx)
1600 break;
1601 light_info = NULL;
1603 if (!light_info)
1605 FIXME("Adding default lights has failed dismally\n");
1606 return WINED3DERR_INVALIDCALL;
1610 if (!enable)
1612 if (light_info->glIndex != -1)
1614 if (!device->recording)
1616 device_invalidate_state(device, STATE_LIGHT_TYPE);
1617 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1620 device->update_state->lights[light_info->glIndex] = NULL;
1621 light_info->glIndex = -1;
1623 else
1625 TRACE("Light already disabled, nothing to do\n");
1627 light_info->enabled = FALSE;
1629 else
1631 light_info->enabled = TRUE;
1632 if (light_info->glIndex != -1)
1634 TRACE("Nothing to do as light was enabled\n");
1636 else
1638 unsigned int i;
1639 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1640 /* Find a free GL light. */
1641 for (i = 0; i < gl_info->limits.lights; ++i)
1643 if (!device->update_state->lights[i])
1645 device->update_state->lights[i] = light_info;
1646 light_info->glIndex = i;
1647 break;
1650 if (light_info->glIndex == -1)
1652 /* Our tests show that Windows returns D3D_OK in this situation, even with
1653 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1654 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1655 * as well for those lights.
1657 * TODO: Test how this affects rendering. */
1658 WARN("Too many concurrently active lights\n");
1659 return WINED3D_OK;
1662 /* i == light_info->glIndex */
1663 if (!device->recording)
1665 device_invalidate_state(device, STATE_LIGHT_TYPE);
1666 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1671 return WINED3D_OK;
1674 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1676 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1677 struct wined3d_light_info *light_info = NULL;
1678 struct list *e;
1680 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1682 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1684 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1685 if (light_info->OriginalIndex == light_idx)
1686 break;
1687 light_info = NULL;
1690 if (!light_info)
1692 TRACE("Light enabled state requested but light not defined.\n");
1693 return WINED3DERR_INVALIDCALL;
1695 /* true is 128 according to SetLightEnable */
1696 *enable = light_info->enabled ? 128 : 0;
1697 return WINED3D_OK;
1700 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1701 UINT plane_idx, const struct wined3d_vec4 *plane)
1703 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1705 /* Validate plane_idx. */
1706 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1708 TRACE("Application has requested clipplane this device doesn't support.\n");
1709 return WINED3DERR_INVALIDCALL;
1712 if (device->recording)
1713 device->recording->changed.clipplane |= 1 << plane_idx;
1715 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1717 TRACE("Application is setting old values over, nothing to do.\n");
1718 return WINED3D_OK;
1721 device->update_state->clip_planes[plane_idx] = *plane;
1723 if (!device->recording)
1724 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1726 return WINED3D_OK;
1729 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1730 UINT plane_idx, struct wined3d_vec4 *plane)
1732 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1734 /* Validate plane_idx. */
1735 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1737 TRACE("Application has requested clipplane this device doesn't support.\n");
1738 return WINED3DERR_INVALIDCALL;
1741 *plane = device->state.clip_planes[plane_idx];
1743 return WINED3D_OK;
1746 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1747 const struct wined3d_clip_status *clip_status)
1749 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1751 if (!clip_status)
1752 return WINED3DERR_INVALIDCALL;
1754 return WINED3D_OK;
1757 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1758 struct wined3d_clip_status *clip_status)
1760 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1762 if (!clip_status)
1763 return WINED3DERR_INVALIDCALL;
1765 return WINED3D_OK;
1768 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1770 TRACE("device %p, material %p.\n", device, material);
1772 device->update_state->material = *material;
1774 if (device->recording)
1775 device->recording->changed.material = TRUE;
1776 else
1777 wined3d_cs_emit_set_material(device->cs, material);
1780 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1782 TRACE("device %p, material %p.\n", device, material);
1784 *material = device->state.material;
1786 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1787 material->diffuse.r, material->diffuse.g,
1788 material->diffuse.b, material->diffuse.a);
1789 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1790 material->ambient.r, material->ambient.g,
1791 material->ambient.b, material->ambient.a);
1792 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1793 material->specular.r, material->specular.g,
1794 material->specular.b, material->specular.a);
1795 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1796 material->emissive.r, material->emissive.g,
1797 material->emissive.b, material->emissive.a);
1798 TRACE("power %.8e.\n", material->power);
1801 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1802 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1804 enum wined3d_format_id prev_format;
1805 struct wined3d_buffer *prev_buffer;
1807 TRACE("device %p, buffer %p, format %s.\n",
1808 device, buffer, debug_d3dformat(format_id));
1810 prev_buffer = device->update_state->index_buffer;
1811 prev_format = device->update_state->index_format;
1813 device->update_state->index_buffer = buffer;
1814 device->update_state->index_format = format_id;
1816 if (device->recording)
1817 device->recording->changed.indices = TRUE;
1819 if (prev_buffer == buffer && prev_format == format_id)
1820 return;
1822 if (buffer)
1823 wined3d_buffer_incref(buffer);
1824 if (!device->recording)
1825 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1826 if (prev_buffer)
1827 wined3d_buffer_decref(prev_buffer);
1830 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1831 enum wined3d_format_id *format)
1833 TRACE("device %p, format %p.\n", device, format);
1835 *format = device->state.index_format;
1836 return device->state.index_buffer;
1839 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1841 TRACE("device %p, base_index %d.\n", device, base_index);
1843 device->update_state->base_vertex_index = base_index;
1846 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1848 TRACE("device %p.\n", device);
1850 return device->state.base_vertex_index;
1853 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1855 TRACE("device %p, viewport %p.\n", device, viewport);
1856 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1857 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1859 device->update_state->viewport = *viewport;
1861 /* Handle recording of state blocks */
1862 if (device->recording)
1864 TRACE("Recording... not performing anything\n");
1865 device->recording->changed.viewport = TRUE;
1866 return;
1869 wined3d_cs_emit_set_viewport(device->cs, viewport);
1872 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1874 TRACE("device %p, viewport %p.\n", device, viewport);
1876 *viewport = device->state.viewport;
1879 static void resolve_depth_buffer(struct wined3d_state *state)
1881 struct wined3d_texture *texture = state->textures[0];
1882 struct wined3d_surface *depth_stencil, *surface;
1884 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1885 || !(texture->resource.format->flags & WINED3DFMT_FLAG_DEPTH))
1886 return;
1887 surface = surface_from_resource(texture->sub_resources[0]);
1888 depth_stencil = state->fb->depth_stencil;
1889 if (!depth_stencil)
1890 return;
1892 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1895 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1896 enum wined3d_render_state state, DWORD value)
1898 DWORD old_value = device->state.render_states[state];
1900 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1902 device->update_state->render_states[state] = value;
1904 /* Handle recording of state blocks. */
1905 if (device->recording)
1907 TRACE("Recording... not performing anything.\n");
1908 device->recording->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
1909 return;
1912 /* Compared here and not before the assignment to allow proper stateblock recording. */
1913 if (value == old_value)
1914 TRACE("Application is setting the old value over, nothing to do.\n");
1915 else
1916 wined3d_cs_emit_set_render_state(device->cs, state, value);
1918 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1920 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1921 resolve_depth_buffer(&device->state);
1925 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1927 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1929 return device->state.render_states[state];
1932 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1933 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1935 DWORD old_value;
1937 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1938 device, sampler_idx, debug_d3dsamplerstate(state), value);
1940 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1941 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1943 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1945 WARN("Invalid sampler %u.\n", sampler_idx);
1946 return; /* Windows accepts overflowing this array ... we do not. */
1949 old_value = device->state.sampler_states[sampler_idx][state];
1950 device->update_state->sampler_states[sampler_idx][state] = value;
1952 /* Handle recording of state blocks. */
1953 if (device->recording)
1955 TRACE("Recording... not performing anything.\n");
1956 device->recording->changed.samplerState[sampler_idx] |= 1 << state;
1957 return;
1960 if (old_value == value)
1962 TRACE("Application is setting the old value over, nothing to do.\n");
1963 return;
1966 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
1969 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
1970 UINT sampler_idx, enum wined3d_sampler_state state)
1972 TRACE("device %p, sampler_idx %u, state %s.\n",
1973 device, sampler_idx, debug_d3dsamplerstate(state));
1975 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1976 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1978 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1980 WARN("Invalid sampler %u.\n", sampler_idx);
1981 return 0; /* Windows accepts overflowing this array ... we do not. */
1984 return device->state.sampler_states[sampler_idx][state];
1987 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
1989 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
1991 if (device->recording)
1992 device->recording->changed.scissorRect = TRUE;
1994 if (EqualRect(&device->update_state->scissor_rect, rect))
1996 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
1997 return;
1999 CopyRect(&device->update_state->scissor_rect, rect);
2001 if (device->recording)
2003 TRACE("Recording... not performing anything.\n");
2004 return;
2007 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2010 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2012 TRACE("device %p, rect %p.\n", device, rect);
2014 *rect = device->state.scissor_rect;
2015 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2018 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2019 struct wined3d_vertex_declaration *declaration)
2021 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2023 TRACE("device %p, declaration %p.\n", device, declaration);
2025 if (device->recording)
2026 device->recording->changed.vertexDecl = TRUE;
2028 if (declaration == prev)
2029 return;
2031 if (declaration)
2032 wined3d_vertex_declaration_incref(declaration);
2033 device->update_state->vertex_declaration = declaration;
2034 if (!device->recording)
2035 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2036 if (prev)
2037 wined3d_vertex_declaration_decref(prev);
2040 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2042 TRACE("device %p.\n", device);
2044 return device->state.vertex_declaration;
2047 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2049 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2051 TRACE("device %p, shader %p.\n", device, shader);
2053 if (device->recording)
2054 device->recording->changed.vertexShader = TRUE;
2056 if (shader == prev)
2057 return;
2059 if (shader)
2060 wined3d_shader_incref(shader);
2061 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2062 if (!device->recording)
2063 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2064 if (prev)
2065 wined3d_shader_decref(prev);
2068 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2070 TRACE("device %p.\n", device);
2072 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2075 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2076 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2078 struct wined3d_buffer *prev;
2080 if (idx >= MAX_CONSTANT_BUFFERS)
2082 WARN("Invalid constant buffer index %u.\n", idx);
2083 return;
2086 prev = device->update_state->cb[type][idx];
2087 if (buffer == prev)
2088 return;
2090 if (buffer)
2091 wined3d_buffer_incref(buffer);
2092 device->update_state->cb[type][idx] = buffer;
2093 if (!device->recording)
2094 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2095 if (prev)
2096 wined3d_buffer_decref(prev);
2099 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2101 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2103 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2106 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2108 TRACE("device %p, idx %u.\n", device, idx);
2110 if (idx >= MAX_CONSTANT_BUFFERS)
2112 WARN("Invalid constant buffer index %u.\n", idx);
2113 return NULL;
2116 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2119 static void wined3d_device_set_sampler(struct wined3d_device *device,
2120 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2122 struct wined3d_sampler *prev;
2124 if (idx >= MAX_SAMPLER_OBJECTS)
2126 WARN("Invalid sampler index %u.\n", idx);
2127 return;
2130 prev = device->update_state->sampler[type][idx];
2131 if (sampler == prev)
2132 return;
2134 if (sampler)
2135 wined3d_sampler_incref(sampler);
2136 device->update_state->sampler[type][idx] = sampler;
2137 if (!device->recording)
2138 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2139 if (prev)
2140 wined3d_sampler_decref(prev);
2143 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2145 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2147 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2150 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2152 TRACE("device %p, idx %u.\n", device, idx);
2154 if (idx >= MAX_SAMPLER_OBJECTS)
2156 WARN("Invalid sampler index %u.\n", idx);
2157 return NULL;
2160 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2163 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2165 UINT i;
2167 for (i = 0; i < device->context_count; ++i)
2169 device->contexts[i]->constant_update_mask |= mask;
2173 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2174 UINT start_register, const BOOL *constants, UINT bool_count)
2176 UINT count = min(bool_count, MAX_CONST_B - start_register);
2177 UINT i;
2179 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2180 device, start_register, constants, bool_count);
2182 if (!constants || start_register >= MAX_CONST_B)
2183 return WINED3DERR_INVALIDCALL;
2185 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2186 for (i = 0; i < count; ++i)
2187 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2189 if (device->recording)
2191 for (i = start_register; i < count + start_register; ++i)
2192 device->recording->changed.vertexShaderConstantsB |= (1 << i);
2194 else
2196 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2199 return WINED3D_OK;
2202 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2203 UINT start_register, BOOL *constants, UINT bool_count)
2205 UINT count = min(bool_count, MAX_CONST_B - start_register);
2207 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2208 device, start_register, constants, bool_count);
2210 if (!constants || start_register >= MAX_CONST_B)
2211 return WINED3DERR_INVALIDCALL;
2213 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2215 return WINED3D_OK;
2218 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2219 UINT start_register, const int *constants, UINT vector4i_count)
2221 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2222 UINT i;
2224 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2225 device, start_register, constants, vector4i_count);
2227 if (!constants || start_register >= MAX_CONST_I)
2228 return WINED3DERR_INVALIDCALL;
2230 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2231 for (i = 0; i < count; ++i)
2232 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2233 constants[i * 4], constants[i * 4 + 1],
2234 constants[i * 4 + 2], constants[i * 4 + 3]);
2236 if (device->recording)
2238 for (i = start_register; i < count + start_register; ++i)
2239 device->recording->changed.vertexShaderConstantsI |= (1 << i);
2241 else
2243 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2246 return WINED3D_OK;
2249 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2250 UINT start_register, int *constants, UINT vector4i_count)
2252 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2254 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2255 device, start_register, constants, vector4i_count);
2257 if (!constants || start_register >= MAX_CONST_I)
2258 return WINED3DERR_INVALIDCALL;
2260 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2261 return WINED3D_OK;
2264 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2265 UINT start_register, const float *constants, UINT vector4f_count)
2267 UINT i;
2268 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2270 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2271 device, start_register, constants, vector4f_count);
2273 /* Specifically test start_register > limit to catch MAX_UINT overflows
2274 * when adding start_register + vector4f_count. */
2275 if (!constants
2276 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2277 || start_register > d3d_info->limits.vs_uniform_count)
2278 return WINED3DERR_INVALIDCALL;
2280 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2281 constants, vector4f_count * sizeof(float) * 4);
2282 if (TRACE_ON(d3d))
2284 for (i = 0; i < vector4f_count; ++i)
2285 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2286 constants[i * 4], constants[i * 4 + 1],
2287 constants[i * 4 + 2], constants[i * 4 + 3]);
2290 if (device->recording)
2291 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2292 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2293 else
2294 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2297 return WINED3D_OK;
2300 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2301 UINT start_register, float *constants, UINT vector4f_count)
2303 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2304 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2306 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2307 device, start_register, constants, vector4f_count);
2309 if (!constants || count < 0)
2310 return WINED3DERR_INVALIDCALL;
2312 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2314 return WINED3D_OK;
2317 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2319 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2321 TRACE("device %p, shader %p.\n", device, shader);
2323 if (device->recording)
2324 device->recording->changed.pixelShader = TRUE;
2326 if (shader == prev)
2327 return;
2329 if (shader)
2330 wined3d_shader_incref(shader);
2331 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2332 if (!device->recording)
2333 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2334 if (prev)
2335 wined3d_shader_decref(prev);
2338 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2340 TRACE("device %p.\n", device);
2342 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2345 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2347 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2349 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2352 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2354 TRACE("device %p, idx %u.\n", device, idx);
2356 if (idx >= MAX_CONSTANT_BUFFERS)
2358 WARN("Invalid constant buffer index %u.\n", idx);
2359 return NULL;
2362 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2365 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2367 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2369 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2372 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2374 TRACE("device %p, idx %u.\n", device, idx);
2376 if (idx >= MAX_SAMPLER_OBJECTS)
2378 WARN("Invalid sampler index %u.\n", idx);
2379 return NULL;
2382 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2385 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2386 UINT start_register, const BOOL *constants, UINT bool_count)
2388 UINT count = min(bool_count, MAX_CONST_B - start_register);
2389 UINT i;
2391 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2392 device, start_register, constants, bool_count);
2394 if (!constants || start_register >= MAX_CONST_B)
2395 return WINED3DERR_INVALIDCALL;
2397 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2398 for (i = 0; i < count; ++i)
2399 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2401 if (device->recording)
2403 for (i = start_register; i < count + start_register; ++i)
2404 device->recording->changed.pixelShaderConstantsB |= (1 << i);
2406 else
2408 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2411 return WINED3D_OK;
2414 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2415 UINT start_register, BOOL *constants, UINT bool_count)
2417 UINT count = min(bool_count, MAX_CONST_B - start_register);
2419 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2420 device, start_register, constants, bool_count);
2422 if (!constants || start_register >= MAX_CONST_B)
2423 return WINED3DERR_INVALIDCALL;
2425 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2427 return WINED3D_OK;
2430 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2431 UINT start_register, const int *constants, UINT vector4i_count)
2433 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2434 UINT i;
2436 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2437 device, start_register, constants, vector4i_count);
2439 if (!constants || start_register >= MAX_CONST_I)
2440 return WINED3DERR_INVALIDCALL;
2442 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2443 for (i = 0; i < count; ++i)
2444 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2445 constants[i * 4], constants[i * 4 + 1],
2446 constants[i * 4 + 2], constants[i * 4 + 3]);
2448 if (device->recording)
2450 for (i = start_register; i < count + start_register; ++i)
2451 device->recording->changed.pixelShaderConstantsI |= (1 << i);
2453 else
2455 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2458 return WINED3D_OK;
2461 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2462 UINT start_register, int *constants, UINT vector4i_count)
2464 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2466 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2467 device, start_register, constants, vector4i_count);
2469 if (!constants || start_register >= MAX_CONST_I)
2470 return WINED3DERR_INVALIDCALL;
2472 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2474 return WINED3D_OK;
2477 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2478 UINT start_register, const float *constants, UINT vector4f_count)
2480 UINT i;
2481 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2483 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2484 device, start_register, constants, vector4f_count);
2486 /* Specifically test start_register > limit to catch MAX_UINT overflows
2487 * when adding start_register + vector4f_count. */
2488 if (!constants
2489 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2490 || start_register > d3d_info->limits.ps_uniform_count)
2491 return WINED3DERR_INVALIDCALL;
2493 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2494 constants, vector4f_count * sizeof(float) * 4);
2495 if (TRACE_ON(d3d))
2497 for (i = 0; i < vector4f_count; ++i)
2498 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2499 constants[i * 4], constants[i * 4 + 1],
2500 constants[i * 4 + 2], constants[i * 4 + 3]);
2503 if (device->recording)
2504 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2505 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2506 else
2507 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2509 return WINED3D_OK;
2512 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2513 UINT start_register, float *constants, UINT vector4f_count)
2515 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2516 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2518 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2519 device, start_register, constants, vector4f_count);
2521 if (!constants || count < 0)
2522 return WINED3DERR_INVALIDCALL;
2524 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2526 return WINED3D_OK;
2529 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2531 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2533 TRACE("device %p, shader %p.\n", device, shader);
2535 if (device->recording || shader == prev)
2536 return;
2537 if (shader)
2538 wined3d_shader_incref(shader);
2539 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2540 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2541 if (prev)
2542 wined3d_shader_decref(prev);
2545 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2547 TRACE("device %p.\n", device);
2549 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2552 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2554 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2556 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2559 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2561 TRACE("device %p, idx %u.\n", device, idx);
2563 if (idx >= MAX_CONSTANT_BUFFERS)
2565 WARN("Invalid constant buffer index %u.\n", idx);
2566 return NULL;
2569 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2572 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2574 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2576 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2579 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2581 TRACE("device %p, idx %u.\n", device, idx);
2583 if (idx >= MAX_SAMPLER_OBJECTS)
2585 WARN("Invalid sampler index %u.\n", idx);
2586 return NULL;
2589 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2592 /* Context activation is done by the caller. */
2593 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2594 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2595 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2596 DWORD DestFVF)
2598 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2599 struct wined3d_viewport vp;
2600 UINT vertex_size;
2601 unsigned int i;
2602 BYTE *dest_ptr;
2603 BOOL doClip;
2604 DWORD numTextures;
2605 HRESULT hr;
2607 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
2609 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2612 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
2614 ERR("Source has no position mask\n");
2615 return WINED3DERR_INVALIDCALL;
2618 if (device->state.render_states[WINED3D_RS_CLIPPING])
2620 static BOOL warned = FALSE;
2622 * The clipping code is not quite correct. Some things need
2623 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2624 * so disable clipping for now.
2625 * (The graphics in Half-Life are broken, and my processvertices
2626 * test crashes with IDirect3DDevice3)
2627 doClip = TRUE;
2629 doClip = FALSE;
2630 if(!warned) {
2631 warned = TRUE;
2632 FIXME("Clipping is broken and disabled for now\n");
2635 else
2636 doClip = FALSE;
2638 vertex_size = get_flexible_vertex_size(DestFVF);
2639 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2641 WARN("Failed to map buffer, hr %#x.\n", hr);
2642 return hr;
2645 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2646 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2647 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2649 TRACE("View mat:\n");
2650 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);
2651 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);
2652 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);
2653 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);
2655 TRACE("Proj mat:\n");
2656 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);
2657 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);
2658 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);
2659 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);
2661 TRACE("World mat:\n");
2662 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);
2663 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);
2664 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);
2665 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);
2667 /* Get the viewport */
2668 wined3d_device_get_viewport(device, &vp);
2669 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2670 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2672 multiply_matrix(&mat,&view_mat,&world_mat);
2673 multiply_matrix(&mat,&proj_mat,&mat);
2675 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2677 for (i = 0; i < dwCount; i+= 1) {
2678 unsigned int tex_index;
2680 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2681 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2682 /* The position first */
2683 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2684 const float *p = (const float *)(element->data.addr + i * element->stride);
2685 float x, y, z, rhw;
2686 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2688 /* Multiplication with world, view and projection matrix */
2689 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);
2690 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);
2691 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);
2692 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);
2694 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2696 /* WARNING: The following things are taken from d3d7 and were not yet checked
2697 * against d3d8 or d3d9!
2700 /* Clipping conditions: From msdn
2702 * A vertex is clipped if it does not match the following requirements
2703 * -rhw < x <= rhw
2704 * -rhw < y <= rhw
2705 * 0 < z <= rhw
2706 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2708 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2709 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2713 if( !doClip ||
2714 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2715 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2716 ( rhw > eps ) ) ) {
2718 /* "Normal" viewport transformation (not clipped)
2719 * 1) The values are divided by rhw
2720 * 2) The y axis is negative, so multiply it with -1
2721 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2722 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2723 * 4) Multiply x with Width/2 and add Width/2
2724 * 5) The same for the height
2725 * 6) Add the viewpoint X and Y to the 2D coordinates and
2726 * The minimum Z value to z
2727 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2729 * Well, basically it's simply a linear transformation into viewport
2730 * coordinates
2733 x /= rhw;
2734 y /= rhw;
2735 z /= rhw;
2737 y *= -1;
2739 x *= vp.width / 2;
2740 y *= vp.height / 2;
2741 z *= vp.max_z - vp.min_z;
2743 x += vp.width / 2 + vp.x;
2744 y += vp.height / 2 + vp.y;
2745 z += vp.min_z;
2747 rhw = 1 / rhw;
2748 } else {
2749 /* That vertex got clipped
2750 * Contrary to OpenGL it is not dropped completely, it just
2751 * undergoes a different calculation.
2753 TRACE("Vertex got clipped\n");
2754 x += rhw;
2755 y += rhw;
2757 x /= 2;
2758 y /= 2;
2760 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2761 * outside of the main vertex buffer memory. That needs some more
2762 * investigation...
2766 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2769 ( (float *) dest_ptr)[0] = x;
2770 ( (float *) dest_ptr)[1] = y;
2771 ( (float *) dest_ptr)[2] = z;
2772 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2774 dest_ptr += 3 * sizeof(float);
2776 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2777 dest_ptr += sizeof(float);
2780 if (DestFVF & WINED3DFVF_PSIZE)
2781 dest_ptr += sizeof(DWORD);
2783 if (DestFVF & WINED3DFVF_NORMAL)
2785 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2786 const float *normal = (const float *)(element->data.addr + i * element->stride);
2787 /* AFAIK this should go into the lighting information */
2788 FIXME("Didn't expect the destination to have a normal\n");
2789 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2792 if (DestFVF & WINED3DFVF_DIFFUSE)
2794 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2795 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2796 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
2798 static BOOL warned = FALSE;
2800 if(!warned) {
2801 ERR("No diffuse color in source, but destination has one\n");
2802 warned = TRUE;
2805 *( (DWORD *) dest_ptr) = 0xffffffff;
2806 dest_ptr += sizeof(DWORD);
2808 else
2810 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2814 if (DestFVF & WINED3DFVF_SPECULAR)
2816 /* What's the color value in the feedback buffer? */
2817 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2818 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2819 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
2821 static BOOL warned = FALSE;
2823 if(!warned) {
2824 ERR("No specular color in source, but destination has one\n");
2825 warned = TRUE;
2828 *(DWORD *)dest_ptr = 0xff000000;
2829 dest_ptr += sizeof(DWORD);
2831 else
2833 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2837 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2839 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2840 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2841 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2843 ERR("No source texture, but destination requests one\n");
2844 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2846 else
2848 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2853 wined3d_buffer_unmap(dest);
2855 return WINED3D_OK;
2857 #undef copy_and_next
2859 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
2860 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
2861 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
2863 struct wined3d_state *state = &device->state;
2864 struct wined3d_stream_info stream_info;
2865 const struct wined3d_gl_info *gl_info;
2866 struct wined3d_context *context;
2867 struct wined3d_shader *vs;
2868 unsigned int i;
2869 HRESULT hr;
2871 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
2872 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
2873 device, src_start_idx, dst_idx, vertex_count,
2874 dst_buffer, declaration, flags, dst_fvf);
2876 if (declaration)
2877 FIXME("Output vertex declaration not implemented yet.\n");
2879 /* Need any context to write to the vbo. */
2880 context = context_acquire(device, NULL);
2881 gl_info = context->gl_info;
2883 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
2884 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
2885 context_stream_info_from_declaration(context, state, &stream_info);
2886 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
2888 /* We can't convert FROM a VBO, and vertex buffers used to source into
2889 * process_vertices() are unlikely to ever be used for drawing. Release
2890 * VBOs in those buffers and fix up the stream_info structure.
2892 * Also apply the start index. */
2893 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
2895 struct wined3d_stream_info_element *e;
2897 if (!(stream_info.use_map & (1 << i)))
2898 continue;
2900 e = &stream_info.elements[i];
2901 if (e->data.buffer_object)
2903 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
2904 e->data.buffer_object = 0;
2905 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, context));
2906 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
2907 vb->buffer_object = 0;
2909 if (e->data.addr)
2910 e->data.addr += e->stride * src_start_idx;
2913 hr = process_vertices_strided(device, dst_idx, vertex_count,
2914 &stream_info, dst_buffer, flags, dst_fvf);
2916 context_release(context);
2918 return hr;
2921 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
2922 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
2924 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2925 DWORD old_value;
2927 TRACE("device %p, stage %u, state %s, value %#x.\n",
2928 device, stage, debug_d3dtexturestate(state), value);
2930 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
2932 WARN("Invalid state %#x passed.\n", state);
2933 return;
2936 if (stage >= d3d_info->limits.ffp_blend_stages)
2938 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
2939 stage, d3d_info->limits.ffp_blend_stages - 1);
2940 return;
2943 old_value = device->update_state->texture_states[stage][state];
2944 device->update_state->texture_states[stage][state] = value;
2946 if (device->recording)
2948 TRACE("Recording... not performing anything.\n");
2949 device->recording->changed.textureState[stage] |= 1 << state;
2950 return;
2953 /* Checked after the assignments to allow proper stateblock recording. */
2954 if (old_value == value)
2956 TRACE("Application is setting the old value over, nothing to do.\n");
2957 return;
2960 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
2963 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
2964 UINT stage, enum wined3d_texture_stage_state state)
2966 TRACE("device %p, stage %u, state %s.\n",
2967 device, stage, debug_d3dtexturestate(state));
2969 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
2971 WARN("Invalid state %#x passed.\n", state);
2972 return 0;
2975 return device->state.texture_states[stage][state];
2978 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
2979 UINT stage, struct wined3d_texture *texture)
2981 struct wined3d_texture *prev;
2983 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
2985 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
2986 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2988 /* Windows accepts overflowing this array... we do not. */
2989 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
2991 WARN("Ignoring invalid stage %u.\n", stage);
2992 return WINED3D_OK;
2995 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
2997 WARN("Rejecting attempt to set scratch texture.\n");
2998 return WINED3DERR_INVALIDCALL;
3001 if (device->recording)
3002 device->recording->changed.textures |= 1 << stage;
3004 prev = device->update_state->textures[stage];
3005 TRACE("Previous texture %p.\n", prev);
3007 if (texture == prev)
3009 TRACE("App is setting the same texture again, nothing to do.\n");
3010 return WINED3D_OK;
3013 TRACE("Setting new texture to %p.\n", texture);
3014 device->update_state->textures[stage] = texture;
3016 if (texture)
3017 wined3d_texture_incref(texture);
3018 if (!device->recording)
3019 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3020 if (prev)
3021 wined3d_texture_decref(prev);
3023 return WINED3D_OK;
3026 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3028 TRACE("device %p, stage %u.\n", device, stage);
3030 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3031 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3033 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3035 WARN("Ignoring invalid stage %u.\n", stage);
3036 return NULL; /* Windows accepts overflowing this array ... we do not. */
3039 return device->state.textures[stage];
3042 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3043 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3045 struct wined3d_swapchain *swapchain;
3047 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3048 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3050 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3051 return WINED3DERR_INVALIDCALL;
3053 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3054 return WINED3DERR_INVALIDCALL;
3055 return WINED3D_OK;
3058 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3060 TRACE("device %p, caps %p.\n", device, caps);
3062 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3063 device->create_parms.device_type, caps);
3066 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3067 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3069 struct wined3d_swapchain *swapchain;
3071 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3072 device, swapchain_idx, mode, rotation);
3074 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3075 return WINED3DERR_INVALIDCALL;
3077 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3080 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3082 struct wined3d_stateblock *stateblock;
3083 HRESULT hr;
3085 TRACE("device %p.\n", device);
3087 if (device->recording)
3088 return WINED3DERR_INVALIDCALL;
3090 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3091 if (FAILED(hr))
3092 return hr;
3094 device->recording = stateblock;
3095 device->update_state = &stateblock->state;
3097 TRACE("Recording stateblock %p.\n", stateblock);
3099 return WINED3D_OK;
3102 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3103 struct wined3d_stateblock **stateblock)
3105 struct wined3d_stateblock *object = device->recording;
3107 TRACE("device %p, stateblock %p.\n", device, stateblock);
3109 if (!device->recording)
3111 WARN("Not recording.\n");
3112 *stateblock = NULL;
3113 return WINED3DERR_INVALIDCALL;
3116 stateblock_init_contained_states(object);
3118 *stateblock = object;
3119 device->recording = NULL;
3120 device->update_state = &device->state;
3122 TRACE("Returning stateblock %p.\n", *stateblock);
3124 return WINED3D_OK;
3127 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3129 /* At the moment we have no need for any functionality at the beginning
3130 * of a scene. */
3131 TRACE("device %p.\n", device);
3133 if (device->inScene)
3135 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3136 return WINED3DERR_INVALIDCALL;
3138 device->inScene = TRUE;
3139 return WINED3D_OK;
3142 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3144 struct wined3d_context *context;
3146 TRACE("device %p.\n", device);
3148 if (!device->inScene)
3150 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3151 return WINED3DERR_INVALIDCALL;
3154 context = context_acquire(device, NULL);
3155 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3156 context->gl_info->gl_ops.gl.p_glFlush();
3157 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3158 * fails. */
3159 context_release(context);
3161 device->inScene = FALSE;
3162 return WINED3D_OK;
3165 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3166 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3168 UINT i;
3170 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3171 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3172 dst_window_override, dirty_region, flags);
3174 for (i = 0; i < device->swapchain_count; ++i)
3176 wined3d_swapchain_present(device->swapchains[i], src_rect,
3177 dst_rect, dst_window_override, dirty_region, flags);
3180 return WINED3D_OK;
3183 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3184 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3186 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3187 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3189 if (!rect_count && rects)
3191 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3192 return WINED3D_OK;
3195 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3197 struct wined3d_surface *ds = device->fb.depth_stencil;
3198 if (!ds)
3200 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3201 /* TODO: What about depth stencil buffers without stencil bits? */
3202 return WINED3DERR_INVALIDCALL;
3204 else if (flags & WINED3DCLEAR_TARGET)
3206 if (ds->resource.width < device->fb.render_targets[0]->resource.width
3207 || ds->resource.height < device->fb.render_targets[0]->resource.height)
3209 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3210 return WINED3D_OK;
3215 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3217 return WINED3D_OK;
3220 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3221 enum wined3d_primitive_type primitive_type)
3223 GLenum gl_primitive_type, prev;
3225 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3227 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3228 prev = device->update_state->gl_primitive_type;
3229 device->update_state->gl_primitive_type = gl_primitive_type;
3230 if (device->recording)
3231 device->recording->changed.primitive_type = TRUE;
3232 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3233 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
3236 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3237 enum wined3d_primitive_type *primitive_type)
3239 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3241 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3243 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3246 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3248 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3250 if (!device->state.vertex_declaration)
3252 WARN("Called without a valid vertex declaration set.\n");
3253 return WINED3DERR_INVALIDCALL;
3256 if (device->state.load_base_vertex_index)
3258 device->state.load_base_vertex_index = 0;
3259 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3262 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3264 return WINED3D_OK;
3267 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3269 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3271 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3273 if (!device->state.index_buffer)
3275 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3276 * without an index buffer set. (The first time at least...)
3277 * D3D8 simply dies, but I doubt it can do much harm to return
3278 * D3DERR_INVALIDCALL there as well. */
3279 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3280 return WINED3DERR_INVALIDCALL;
3283 if (!device->state.vertex_declaration)
3285 WARN("Called without a valid vertex declaration set.\n");
3286 return WINED3DERR_INVALIDCALL;
3289 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3290 device->state.load_base_vertex_index != device->state.base_vertex_index)
3292 device->state.load_base_vertex_index = device->state.base_vertex_index;
3293 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3296 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3298 return WINED3D_OK;
3301 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3302 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3304 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3306 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3309 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3310 static HRESULT device_update_volume(struct wined3d_device *device,
3311 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3313 struct wined3d_map_desc src;
3314 HRESULT hr;
3315 struct wined3d_bo_address data;
3316 struct wined3d_context *context;
3318 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3319 device, src_volume, dst_volume);
3321 if (src_volume->resource.format != dst_volume->resource.format)
3323 FIXME("Source and destination formats do not match.\n");
3324 return WINED3DERR_INVALIDCALL;
3326 if (src_volume->resource.width != dst_volume->resource.width
3327 || src_volume->resource.height != dst_volume->resource.height
3328 || src_volume->resource.depth != dst_volume->resource.depth)
3330 FIXME("Source and destination sizes do not match.\n");
3331 return WINED3DERR_INVALIDCALL;
3334 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3335 return hr;
3337 context = context_acquire(device, NULL);
3339 wined3d_volume_load(dst_volume, context, FALSE);
3341 data.buffer_object = 0;
3342 data.addr = src.data;
3343 wined3d_volume_upload_data(dst_volume, context, &data);
3344 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3346 context_release(context);
3348 hr = wined3d_volume_unmap(src_volume);
3350 return hr;
3353 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3354 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3356 enum wined3d_resource_type type;
3357 unsigned int level_count, i;
3358 HRESULT hr;
3359 struct wined3d_context *context;
3361 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3363 /* Verify that the source and destination textures are non-NULL. */
3364 if (!src_texture || !dst_texture)
3366 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3367 return WINED3DERR_INVALIDCALL;
3370 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3372 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3373 return WINED3DERR_INVALIDCALL;
3375 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3377 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3378 return WINED3DERR_INVALIDCALL;
3381 /* Verify that the source and destination textures are the same type. */
3382 type = src_texture->resource.type;
3383 if (dst_texture->resource.type != type)
3385 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3386 return WINED3DERR_INVALIDCALL;
3389 /* Check that both textures have the identical numbers of levels. */
3390 level_count = wined3d_texture_get_level_count(src_texture);
3391 if (wined3d_texture_get_level_count(dst_texture) != level_count)
3393 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
3394 return WINED3DERR_INVALIDCALL;
3397 /* Make sure that the destination texture is loaded. */
3398 context = context_acquire(device, NULL);
3399 dst_texture->texture_ops->texture_preload(dst_texture, context, SRGB_RGB);
3400 context_release(context);
3402 /* Update every surface level of the texture. */
3403 switch (type)
3405 case WINED3D_RTYPE_TEXTURE:
3407 struct wined3d_surface *src_surface;
3408 struct wined3d_surface *dst_surface;
3410 for (i = 0; i < level_count; ++i)
3412 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3413 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3414 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3415 if (FAILED(hr))
3417 WARN("Failed to update surface, hr %#x.\n", hr);
3418 return hr;
3421 break;
3424 case WINED3D_RTYPE_CUBE_TEXTURE:
3426 struct wined3d_surface *src_surface;
3427 struct wined3d_surface *dst_surface;
3429 for (i = 0; i < level_count * 6; ++i)
3431 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3432 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3433 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3434 if (FAILED(hr))
3436 WARN("Failed to update surface, hr %#x.\n", hr);
3437 return hr;
3440 break;
3443 case WINED3D_RTYPE_VOLUME_TEXTURE:
3445 for (i = 0; i < level_count; ++i)
3447 hr = device_update_volume(device,
3448 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
3449 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3450 if (FAILED(hr))
3452 WARN("Failed to update volume, hr %#x.\n", hr);
3453 return hr;
3456 break;
3459 default:
3460 FIXME("Unsupported texture type %#x.\n", type);
3461 return WINED3DERR_INVALIDCALL;
3464 return WINED3D_OK;
3467 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3468 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3470 struct wined3d_swapchain *swapchain;
3472 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3474 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3475 return WINED3DERR_INVALIDCALL;
3477 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3480 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3482 const struct wined3d_state *state = &device->state;
3483 struct wined3d_texture *texture;
3484 DWORD i;
3486 TRACE("device %p, num_passes %p.\n", device, num_passes);
3488 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3490 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3492 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3493 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3495 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3497 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3498 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3501 texture = state->textures[i];
3502 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
3504 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3506 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3507 return E_FAIL;
3509 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3511 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3512 return E_FAIL;
3514 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3515 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3517 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3518 return E_FAIL;
3522 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3523 || state->render_states[WINED3D_RS_STENCILENABLE])
3525 struct wined3d_surface *ds = device->fb.depth_stencil;
3526 struct wined3d_surface *target = device->fb.render_targets[0];
3528 if(ds && target
3529 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
3531 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3532 return WINED3DERR_CONFLICTINGRENDERSTATE;
3536 /* return a sensible default */
3537 *num_passes = 1;
3539 TRACE("returning D3D_OK\n");
3540 return WINED3D_OK;
3543 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3545 static BOOL warned;
3547 TRACE("device %p, software %#x.\n", device, software);
3549 if (!warned)
3551 FIXME("device %p, software %#x stub!\n", device, software);
3552 warned = TRUE;
3555 device->softwareVertexProcessing = software;
3558 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3560 static BOOL warned;
3562 TRACE("device %p.\n", device);
3564 if (!warned)
3566 TRACE("device %p stub!\n", device);
3567 warned = TRUE;
3570 return device->softwareVertexProcessing;
3573 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3574 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3576 struct wined3d_swapchain *swapchain;
3578 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3579 device, swapchain_idx, raster_status);
3581 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3582 return WINED3DERR_INVALIDCALL;
3584 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3587 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3589 static BOOL warned;
3591 TRACE("device %p, segments %.8e.\n", device, segments);
3593 if (segments != 0.0f)
3595 if (!warned)
3597 FIXME("device %p, segments %.8e stub!\n", device, segments);
3598 warned = TRUE;
3602 return WINED3D_OK;
3605 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3607 static BOOL warned;
3609 TRACE("device %p.\n", device);
3611 if (!warned)
3613 FIXME("device %p stub!\n", device);
3614 warned = TRUE;
3617 return 0.0f;
3620 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3621 struct wined3d_surface *src_surface, const RECT *src_rect,
3622 struct wined3d_surface *dst_surface, const POINT *dst_point)
3624 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3625 device, src_surface, wine_dbgstr_rect(src_rect),
3626 dst_surface, wine_dbgstr_point(dst_point));
3628 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3630 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3631 src_surface, dst_surface);
3632 return WINED3DERR_INVALIDCALL;
3635 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3638 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
3639 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
3641 RECT r;
3643 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
3644 device, surface, wine_dbgstr_rect(rect),
3645 color->r, color->g, color->b, color->a);
3647 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3649 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
3650 return WINED3DERR_INVALIDCALL;
3653 if (!rect)
3655 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
3656 rect = &r;
3659 return surface_color_fill(surface, rect, color);
3662 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
3663 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
3665 struct wined3d_resource *resource;
3666 HRESULT hr;
3667 RECT rect;
3669 resource = rendertarget_view->resource;
3670 if (resource->type != WINED3D_RTYPE_SURFACE)
3672 FIXME("Only supported on surface resources\n");
3673 return;
3676 SetRect(&rect, 0, 0, resource->width, resource->height);
3677 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
3678 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
3681 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
3682 UINT render_target_idx)
3684 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
3686 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
3688 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3689 return NULL;
3692 return device->fb.render_targets[render_target_idx];
3695 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
3697 TRACE("device %p.\n", device);
3699 return device->fb.depth_stencil;
3702 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
3703 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
3705 struct wined3d_surface *prev;
3707 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
3708 device, render_target_idx, render_target, set_viewport);
3710 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
3712 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3713 return WINED3DERR_INVALIDCALL;
3716 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
3718 WARN("Surface %p doesn't have render target usage.\n", render_target);
3719 return WINED3DERR_INVALIDCALL;
3722 /* Set the viewport and scissor rectangles, if requested. Tests show that
3723 * stateblock recording is ignored, the change goes directly into the
3724 * primary stateblock. */
3725 if (!render_target_idx && set_viewport)
3727 struct wined3d_state *state = &device->state;
3729 state->viewport.x = 0;
3730 state->viewport.y = 0;
3731 state->viewport.width = render_target->resource.width;
3732 state->viewport.height = render_target->resource.height;
3733 state->viewport.min_z = 0.0f;
3734 state->viewport.max_z = 1.0f;
3735 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
3737 state->scissor_rect.top = 0;
3738 state->scissor_rect.left = 0;
3739 state->scissor_rect.right = render_target->resource.width;
3740 state->scissor_rect.bottom = render_target->resource.height;
3741 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
3745 prev = device->fb.render_targets[render_target_idx];
3746 if (render_target == prev)
3747 return WINED3D_OK;
3749 if (render_target)
3750 wined3d_surface_incref(render_target);
3751 device->fb.render_targets[render_target_idx] = render_target;
3752 wined3d_cs_emit_set_render_target(device->cs, render_target_idx, render_target);
3753 /* Release after the assignment, to prevent device_resource_released()
3754 * from seeing the surface as still in use. */
3755 if (prev)
3756 wined3d_surface_decref(prev);
3758 return WINED3D_OK;
3761 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
3763 struct wined3d_surface *prev = device->fb.depth_stencil;
3765 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
3766 device, depth_stencil, prev);
3768 if (prev == depth_stencil)
3770 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
3771 return;
3774 device->fb.depth_stencil = depth_stencil;
3775 if (depth_stencil)
3776 wined3d_surface_incref(depth_stencil);
3777 wined3d_cs_emit_set_depth_stencil(device->cs, depth_stencil);
3778 if (prev)
3779 wined3d_surface_decref(prev);
3782 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
3783 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
3785 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
3786 device, x_hotspot, y_hotspot, cursor_image);
3788 /* some basic validation checks */
3789 if (device->cursorTexture)
3791 struct wined3d_context *context = context_acquire(device, NULL);
3792 context->gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
3793 context_release(context);
3794 device->cursorTexture = 0;
3797 if (cursor_image)
3799 struct wined3d_display_mode mode;
3800 struct wined3d_map_desc map_desc;
3801 HRESULT hr;
3803 /* MSDN: Cursor must be A8R8G8B8 */
3804 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
3806 WARN("surface %p has an invalid format.\n", cursor_image);
3807 return WINED3DERR_INVALIDCALL;
3810 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
3812 ERR("Failed to get display mode, hr %#x.\n", hr);
3813 return WINED3DERR_INVALIDCALL;
3816 /* MSDN: Cursor must be smaller than the display mode */
3817 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
3819 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
3820 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
3821 mode.width, mode.height);
3822 return WINED3DERR_INVALIDCALL;
3825 /* TODO: MSDN: Cursor sizes must be a power of 2 */
3827 /* Do not store the surface's pointer because the application may
3828 * release it after setting the cursor image. Windows doesn't
3829 * addref the set surface, so we can't do this either without
3830 * creating circular refcount dependencies. Copy out the gl texture
3831 * instead. */
3832 device->cursorWidth = cursor_image->resource.width;
3833 device->cursorHeight = cursor_image->resource.height;
3834 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
3836 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3837 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
3838 struct wined3d_context *context;
3839 char *mem, *bits = map_desc.data;
3840 GLint intfmt = format->glInternal;
3841 GLint gl_format = format->glFormat;
3842 GLint type = format->glType;
3843 INT height = device->cursorHeight;
3844 INT width = device->cursorWidth;
3845 INT bpp = format->byte_count;
3846 INT i;
3848 /* Reformat the texture memory (pitch and width can be
3849 * different) */
3850 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
3851 for (i = 0; i < height; ++i)
3852 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
3853 wined3d_surface_unmap(cursor_image);
3855 context = context_acquire(device, NULL);
3857 context_invalidate_active_texture(context);
3858 /* Create a new cursor texture */
3859 gl_info->gl_ops.gl.p_glGenTextures(1, &device->cursorTexture);
3860 checkGLcall("glGenTextures");
3861 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
3862 /* Copy the bitmap memory into the cursor texture */
3863 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
3864 checkGLcall("glTexImage2D");
3865 HeapFree(GetProcessHeap(), 0, mem);
3867 context_release(context);
3869 else
3871 FIXME("A cursor texture was not returned.\n");
3872 device->cursorTexture = 0;
3875 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
3877 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
3878 ICONINFO cursorInfo;
3879 DWORD *maskBits;
3880 HCURSOR cursor;
3882 /* 32-bit user32 cursors ignore the alpha channel if it's all
3883 * zeroes, and use the mask instead. Fill the mask with all ones
3884 * to ensure we still get a fully transparent cursor. */
3885 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
3886 memset(maskBits, 0xff, mask_size);
3887 wined3d_surface_map(cursor_image, &map_desc, NULL,
3888 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
3889 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
3891 cursorInfo.fIcon = FALSE;
3892 cursorInfo.xHotspot = x_hotspot;
3893 cursorInfo.yHotspot = y_hotspot;
3894 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
3895 1, 1, maskBits);
3896 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
3897 1, 32, map_desc.data);
3898 wined3d_surface_unmap(cursor_image);
3899 /* Create our cursor and clean up. */
3900 cursor = CreateIconIndirect(&cursorInfo);
3901 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
3902 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
3903 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
3904 device->hardwareCursor = cursor;
3905 if (device->bCursorVisible) SetCursor( cursor );
3906 HeapFree(GetProcessHeap(), 0, maskBits);
3910 device->xHotSpot = x_hotspot;
3911 device->yHotSpot = y_hotspot;
3912 return WINED3D_OK;
3915 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
3916 int x_screen_space, int y_screen_space, DWORD flags)
3918 TRACE("device %p, x %d, y %d, flags %#x.\n",
3919 device, x_screen_space, y_screen_space, flags);
3921 device->xScreenSpace = x_screen_space;
3922 device->yScreenSpace = y_screen_space;
3924 if (device->hardwareCursor)
3926 POINT pt;
3928 GetCursorPos( &pt );
3929 if (x_screen_space == pt.x && y_screen_space == pt.y)
3930 return;
3931 SetCursorPos( x_screen_space, y_screen_space );
3933 /* Switch to the software cursor if position diverges from the hardware one. */
3934 GetCursorPos( &pt );
3935 if (x_screen_space != pt.x || y_screen_space != pt.y)
3937 if (device->bCursorVisible) SetCursor( NULL );
3938 DestroyCursor( device->hardwareCursor );
3939 device->hardwareCursor = 0;
3944 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
3946 BOOL oldVisible = device->bCursorVisible;
3948 TRACE("device %p, show %#x.\n", device, show);
3951 * When ShowCursor is first called it should make the cursor appear at the OS's last
3952 * known cursor position.
3954 if (show && !oldVisible)
3956 POINT pt;
3957 GetCursorPos(&pt);
3958 device->xScreenSpace = pt.x;
3959 device->yScreenSpace = pt.y;
3962 if (device->hardwareCursor)
3964 device->bCursorVisible = show;
3965 if (show)
3966 SetCursor(device->hardwareCursor);
3967 else
3968 SetCursor(NULL);
3970 else
3972 if (device->cursorTexture)
3973 device->bCursorVisible = show;
3976 return oldVisible;
3979 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
3981 struct wined3d_resource *resource, *cursor;
3983 TRACE("device %p.\n", device);
3985 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
3987 TRACE("Checking resource %p for eviction.\n", resource);
3989 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
3991 TRACE("Evicting %p.\n", resource);
3992 resource->resource_ops->resource_unload(resource);
3996 /* Invalidate stream sources, the buffer(s) may have been evicted. */
3997 device_invalidate_state(device, STATE_STREAMSRC);
4000 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4002 struct wined3d_resource *resource, *cursor;
4003 const struct wined3d_gl_info *gl_info;
4004 struct wined3d_context *context;
4005 struct wined3d_shader *shader;
4007 context = context_acquire(device, NULL);
4008 gl_info = context->gl_info;
4010 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4012 TRACE("Unloading resource %p.\n", resource);
4014 resource->resource_ops->resource_unload(resource);
4017 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4019 device->shader_backend->shader_destroy(shader);
4022 if (device->depth_blt_texture)
4024 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4025 device->depth_blt_texture = 0;
4027 if (device->cursorTexture)
4029 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4030 device->cursorTexture = 0;
4033 device->blitter->free_private(device);
4034 device->shader_backend->shader_free_private(device);
4035 destroy_dummy_textures(device, gl_info);
4037 context_release(context);
4039 while (device->context_count)
4041 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4044 HeapFree(GetProcessHeap(), 0, swapchain->context);
4045 swapchain->context = NULL;
4048 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4050 struct wined3d_context *context;
4051 struct wined3d_surface *target;
4052 HRESULT hr;
4054 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4055 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4057 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4058 return hr;
4061 if (FAILED(hr = device->blitter->alloc_private(device)))
4063 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4064 device->shader_backend->shader_free_private(device);
4065 return hr;
4068 /* Recreate the primary swapchain's context */
4069 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4070 if (!swapchain->context)
4072 ERR("Failed to allocate memory for swapchain context array.\n");
4073 device->blitter->free_private(device);
4074 device->shader_backend->shader_free_private(device);
4075 return E_OUTOFMEMORY;
4078 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4079 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4081 WARN("Failed to create context.\n");
4082 device->blitter->free_private(device);
4083 device->shader_backend->shader_free_private(device);
4084 HeapFree(GetProcessHeap(), 0, swapchain->context);
4085 return E_FAIL;
4088 swapchain->context[0] = context;
4089 swapchain->num_contexts = 1;
4090 create_dummy_textures(device, context);
4091 context_release(context);
4093 return WINED3D_OK;
4096 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4097 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4098 wined3d_device_reset_cb callback, BOOL reset_state)
4100 struct wined3d_resource *resource, *cursor;
4101 struct wined3d_swapchain *swapchain;
4102 struct wined3d_display_mode m;
4103 BOOL DisplayModeChanged = FALSE;
4104 BOOL update_desc = FALSE;
4105 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4106 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4107 HRESULT hr = WINED3D_OK;
4108 unsigned int i;
4110 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4112 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4114 ERR("Failed to get the first implicit swapchain.\n");
4115 return WINED3DERR_INVALIDCALL;
4118 if (reset_state)
4119 state_unbind_resources(&device->state);
4121 if (device->fb.render_targets)
4123 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4125 wined3d_device_set_render_target(device, i, NULL, FALSE);
4127 if (swapchain->back_buffers && swapchain->back_buffers[0])
4128 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
4130 wined3d_device_set_depth_stencil(device, NULL);
4132 if (device->onscreen_depth_stencil)
4134 wined3d_surface_decref(device->onscreen_depth_stencil);
4135 device->onscreen_depth_stencil = NULL;
4138 if (reset_state)
4140 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4142 TRACE("Enumerating resource %p.\n", resource);
4143 if (FAILED(hr = callback(resource)))
4144 return hr;
4148 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4149 * on an existing gl context, so there's no real need for recreation.
4151 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4153 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4155 TRACE("New params:\n");
4156 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4157 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4158 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4159 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4160 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4161 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4162 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4163 TRACE("device_window %p\n", swapchain_desc->device_window);
4164 TRACE("windowed %#x\n", swapchain_desc->windowed);
4165 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4166 if (swapchain_desc->enable_auto_depth_stencil)
4167 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4168 TRACE("flags %#x\n", swapchain_desc->flags);
4169 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4170 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4171 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4173 /* No special treatment of these parameters. Just store them */
4174 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4175 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4176 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4177 swapchain->desc.flags = swapchain_desc->flags;
4178 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4179 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4180 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4182 /* What to do about these? */
4183 if (swapchain_desc->backbuffer_count
4184 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4185 FIXME("Cannot change the back buffer count yet.\n");
4187 if (swapchain_desc->device_window
4188 && swapchain_desc->device_window != swapchain->desc.device_window)
4190 TRACE("Changing the device window from %p to %p.\n",
4191 swapchain->desc.device_window, swapchain_desc->device_window);
4192 swapchain->desc.device_window = swapchain_desc->device_window;
4193 swapchain->device_window = swapchain_desc->device_window;
4194 wined3d_swapchain_set_window(swapchain, NULL);
4197 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
4199 struct wined3d_resource_desc surface_desc;
4201 TRACE("Creating the depth stencil buffer\n");
4203 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
4204 surface_desc.format = swapchain_desc->auto_depth_stencil_format;
4205 surface_desc.multisample_type = swapchain_desc->multisample_type;
4206 surface_desc.multisample_quality = swapchain_desc->multisample_quality;
4207 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4208 surface_desc.pool = WINED3D_POOL_DEFAULT;
4209 surface_desc.width = swapchain_desc->backbuffer_width;
4210 surface_desc.height = swapchain_desc->backbuffer_height;
4211 surface_desc.depth = 1;
4212 surface_desc.size = 0;
4214 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4215 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
4217 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
4218 return WINED3DERR_INVALIDCALL;
4222 /* Reset the depth stencil */
4223 if (swapchain_desc->enable_auto_depth_stencil)
4224 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
4226 if (mode)
4228 DisplayModeChanged = TRUE;
4229 m = *mode;
4231 else if (swapchain_desc->windowed)
4233 m = swapchain->original_mode;
4235 else
4237 m.width = swapchain_desc->backbuffer_width;
4238 m.height = swapchain_desc->backbuffer_height;
4239 m.refresh_rate = swapchain_desc->refresh_rate;
4240 m.format_id = swapchain_desc->backbuffer_format;
4241 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4244 if (!backbuffer_width || !backbuffer_height)
4246 /* The application is requesting that either the swapchain width or
4247 * height be set to the corresponding dimension in the window's
4248 * client rect. */
4250 RECT client_rect;
4252 if (!swapchain_desc->windowed)
4253 return WINED3DERR_INVALIDCALL;
4255 if (!GetClientRect(swapchain->device_window, &client_rect))
4257 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
4258 return WINED3DERR_INVALIDCALL;
4261 if (!backbuffer_width)
4262 backbuffer_width = client_rect.right;
4264 if (!backbuffer_height)
4265 backbuffer_height = client_rect.bottom;
4268 if (backbuffer_width != swapchain->desc.backbuffer_width
4269 || backbuffer_height != swapchain->desc.backbuffer_height)
4271 if (!swapchain_desc->windowed)
4272 DisplayModeChanged = TRUE;
4274 swapchain->desc.backbuffer_width = backbuffer_width;
4275 swapchain->desc.backbuffer_height = backbuffer_height;
4276 update_desc = TRUE;
4279 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
4280 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
4282 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
4283 update_desc = TRUE;
4286 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
4287 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
4289 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
4290 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
4291 update_desc = TRUE;
4294 if (update_desc)
4296 UINT i;
4298 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
4299 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4300 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
4301 return hr;
4303 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4305 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
4306 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4307 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
4308 return hr;
4310 if (device->auto_depth_stencil)
4312 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
4313 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
4314 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
4315 return hr;
4319 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4320 || DisplayModeChanged)
4322 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4324 WARN("Failed to set display mode, hr %#x.\n", hr);
4325 return WINED3DERR_INVALIDCALL;
4328 if (!swapchain_desc->windowed)
4330 if (swapchain->desc.windowed)
4332 HWND focus_window = device->create_parms.focus_window;
4333 if (!focus_window)
4334 focus_window = swapchain_desc->device_window;
4335 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4337 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4338 return hr;
4341 /* switch from windowed to fs */
4342 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4343 swapchain_desc->backbuffer_width,
4344 swapchain_desc->backbuffer_height);
4346 else
4348 /* Fullscreen -> fullscreen mode change */
4349 MoveWindow(swapchain->device_window, 0, 0,
4350 swapchain_desc->backbuffer_width,
4351 swapchain_desc->backbuffer_height,
4352 TRUE);
4355 else if (!swapchain->desc.windowed)
4357 /* Fullscreen -> windowed switch */
4358 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4359 wined3d_device_release_focus_window(device);
4361 swapchain->desc.windowed = swapchain_desc->windowed;
4363 else if (!swapchain_desc->windowed)
4365 DWORD style = device->style;
4366 DWORD exStyle = device->exStyle;
4367 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4368 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4369 * Reset to clear up their mess. Guild Wars also loses the device during that.
4371 device->style = 0;
4372 device->exStyle = 0;
4373 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4374 swapchain_desc->backbuffer_width,
4375 swapchain_desc->backbuffer_height);
4376 device->style = style;
4377 device->exStyle = exStyle;
4380 if (reset_state)
4382 TRACE("Resetting stateblock.\n");
4383 if (device->recording)
4385 wined3d_stateblock_decref(device->recording);
4386 device->recording = NULL;
4388 state_cleanup(&device->state);
4390 if (device->d3d_initialized)
4391 delete_opengl_contexts(device, swapchain);
4393 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4394 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4395 ERR("Failed to initialize device state, hr %#x.\n", hr);
4396 device->update_state = &device->state;
4398 device_init_swapchain_state(device, swapchain);
4400 else
4402 struct wined3d_surface *rt = device->fb.render_targets[0];
4403 struct wined3d_state *state = &device->state;
4405 /* Note the min_z / max_z is not reset. */
4406 state->viewport.x = 0;
4407 state->viewport.y = 0;
4408 state->viewport.width = rt->resource.width;
4409 state->viewport.height = rt->resource.height;
4410 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4412 state->scissor_rect.top = 0;
4413 state->scissor_rect.left = 0;
4414 state->scissor_rect.right = rt->resource.width;
4415 state->scissor_rect.bottom = rt->resource.height;
4416 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4419 swapchain_update_render_to_fbo(swapchain);
4420 swapchain_update_draw_bindings(swapchain);
4422 if (reset_state && device->d3d_initialized)
4423 hr = create_primary_opengl_context(device, swapchain);
4425 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4426 * first use
4428 return hr;
4431 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4433 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4435 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4437 return WINED3D_OK;
4441 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4442 struct wined3d_device_creation_parameters *parameters)
4444 TRACE("device %p, parameters %p.\n", device, parameters);
4446 *parameters = device->create_parms;
4449 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4450 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4452 struct wined3d_swapchain *swapchain;
4454 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4455 device, swapchain_idx, flags, ramp);
4457 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4458 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4461 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4462 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4464 struct wined3d_swapchain *swapchain;
4466 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4467 device, swapchain_idx, ramp);
4469 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4470 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4473 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4475 TRACE("device %p, resource %p.\n", device, resource);
4477 list_add_head(&device->resources, &resource->resource_list_entry);
4480 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4482 TRACE("device %p, resource %p.\n", device, resource);
4484 list_remove(&resource->resource_list_entry);
4487 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4489 enum wined3d_resource_type type = resource->type;
4490 unsigned int i;
4492 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4494 context_resource_released(device, resource, type);
4496 switch (type)
4498 case WINED3D_RTYPE_SURFACE:
4500 struct wined3d_surface *surface = surface_from_resource(resource);
4502 if (!device->d3d_initialized) break;
4504 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4506 if (device->fb.render_targets[i] == surface)
4508 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4509 device->fb.render_targets[i] = NULL;
4513 if (device->fb.depth_stencil == surface)
4515 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4516 device->fb.depth_stencil = NULL;
4519 break;
4521 case WINED3D_RTYPE_TEXTURE:
4522 case WINED3D_RTYPE_CUBE_TEXTURE:
4523 case WINED3D_RTYPE_VOLUME_TEXTURE:
4524 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4526 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4528 if (device->state.textures[i] == texture)
4530 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4531 device->state.textures[i] = NULL;
4534 if (device->recording && device->update_state->textures[i] == texture)
4536 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4537 texture, device->recording, i);
4538 device->update_state->textures[i] = NULL;
4541 break;
4543 case WINED3D_RTYPE_BUFFER:
4545 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4547 for (i = 0; i < MAX_STREAMS; ++i)
4549 if (device->state.streams[i].buffer == buffer)
4551 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4552 device->state.streams[i].buffer = NULL;
4555 if (device->recording && device->update_state->streams[i].buffer == buffer)
4557 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4558 buffer, device->recording, i);
4559 device->update_state->streams[i].buffer = NULL;
4563 if (device->state.index_buffer == buffer)
4565 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4566 device->state.index_buffer = NULL;
4569 if (device->recording && device->update_state->index_buffer == buffer)
4571 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4572 buffer, device->recording);
4573 device->update_state->index_buffer = NULL;
4576 break;
4578 default:
4579 break;
4582 /* Remove the resource from the resourceStore */
4583 device_resource_remove(device, resource);
4585 TRACE("Resource released.\n");
4588 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4590 struct wined3d_resource *resource;
4592 TRACE("device %p, dc %p.\n", device, dc);
4594 if (!dc)
4595 return NULL;
4597 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4599 if (resource->type == WINED3D_RTYPE_SURFACE)
4601 struct wined3d_surface *s = surface_from_resource(resource);
4603 if (s->hDC == dc)
4605 TRACE("Found surface %p for dc %p.\n", s, dc);
4606 return s;
4611 return NULL;
4614 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4615 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4616 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4618 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4619 const struct fragment_pipeline *fragment_pipeline;
4620 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4621 unsigned int i;
4622 HRESULT hr;
4624 device->ref = 1;
4625 device->wined3d = wined3d;
4626 wined3d_incref(device->wined3d);
4627 device->adapter = wined3d->adapter_count ? adapter : NULL;
4628 device->device_parent = device_parent;
4629 list_init(&device->resources);
4630 list_init(&device->shaders);
4631 device->surface_alignment = surface_alignment;
4633 /* Save the creation parameters. */
4634 device->create_parms.adapter_idx = adapter_idx;
4635 device->create_parms.device_type = device_type;
4636 device->create_parms.focus_window = focus_window;
4637 device->create_parms.flags = flags;
4639 device->shader_backend = adapter->shader_backend;
4641 vertex_pipeline = adapter->vertex_pipe;
4643 fragment_pipeline = adapter->fragment_pipe;
4645 if (vertex_pipeline->vp_states && fragment_pipeline->states
4646 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4647 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4648 fragment_pipeline, misc_state_template)))
4650 ERR("Failed to compile state table, hr %#x.\n", hr);
4651 wined3d_decref(device->wined3d);
4652 return hr;
4655 device->blitter = adapter->blitter;
4657 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
4658 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4660 ERR("Failed to initialize device state, hr %#x.\n", hr);
4661 goto err;
4663 device->update_state = &device->state;
4665 if (!(device->cs = wined3d_cs_create(device)))
4667 WARN("Failed to create command stream.\n");
4668 state_cleanup(&device->state);
4669 hr = E_FAIL;
4670 goto err;
4673 return WINED3D_OK;
4675 err:
4676 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4678 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4680 wined3d_decref(device->wined3d);
4681 return hr;
4685 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4687 DWORD rep = device->StateTable[state].representative;
4688 struct wined3d_context *context;
4689 DWORD idx;
4690 BYTE shift;
4691 UINT i;
4693 for (i = 0; i < device->context_count; ++i)
4695 context = device->contexts[i];
4696 if(isStateDirty(context, rep)) continue;
4698 context->dirtyArray[context->numDirtyEntries++] = rep;
4699 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4700 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4701 context->isStateDirty[idx] |= (1 << shift);
4705 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
4707 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
4708 *width = context->current_rt->pow2Width;
4709 *height = context->current_rt->pow2Height;
4712 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
4714 const struct wined3d_swapchain *swapchain = context->swapchain;
4715 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
4716 * current context's drawable, which is the size of the back buffer of the swapchain
4717 * the active context belongs to. */
4718 *width = swapchain->desc.backbuffer_width;
4719 *height = swapchain->desc.backbuffer_height;
4722 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4723 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4725 if (device->filter_messages)
4727 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4728 window, message, wparam, lparam);
4729 if (unicode)
4730 return DefWindowProcW(window, message, wparam, lparam);
4731 else
4732 return DefWindowProcA(window, message, wparam, lparam);
4735 if (message == WM_DESTROY)
4737 TRACE("unregister window %p.\n", window);
4738 wined3d_unregister_window(window);
4740 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
4741 ERR("Window %p is not the focus window for device %p.\n", window, device);
4743 else if (message == WM_DISPLAYCHANGE)
4745 device->device_parent->ops->mode_changed(device->device_parent);
4748 if (unicode)
4749 return CallWindowProcW(proc, window, message, wparam, lparam);
4750 else
4751 return CallWindowProcA(proc, window, message, wparam, lparam);