wined3d: Release the context before potentially destroying the swapchain in wined3d_d...
[wine/multimedia.git] / dlls / wined3d / device.c
blob0822f61502699dabbdec18c7b52e934eee158e73
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 void device_switch_onscreen_ds(struct wined3d_device *device,
202 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
204 if (device->onscreen_depth_stencil)
206 surface_load_ds_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
208 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
209 device->onscreen_depth_stencil->ds_current_size.cx,
210 device->onscreen_depth_stencil->ds_current_size.cy);
211 wined3d_surface_decref(device->onscreen_depth_stencil);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_surface_incref(device->onscreen_depth_stencil);
217 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
219 /* partial draw rect */
220 if (draw_rect->left || draw_rect->top
221 || draw_rect->right < target->resource.width
222 || draw_rect->bottom < target->resource.height)
223 return FALSE;
225 /* partial clear rect */
226 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
227 || clear_rect->right < target->resource.width
228 || clear_rect->bottom < target->resource.height))
229 return FALSE;
231 return TRUE;
234 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
235 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
237 RECT current_rect, r;
239 if (ds->locations & WINED3D_LOCATION_DISCARDED)
241 /* Depth buffer was discarded, make it entirely current in its new location since
242 * there is no other place where we would get data anyway. */
243 SetRect(out_rect, 0, 0, ds->resource.width, ds->resource.height);
244 return;
247 if (ds->locations & location)
248 SetRect(&current_rect, 0, 0,
249 ds->ds_current_size.cx,
250 ds->ds_current_size.cy);
251 else
252 SetRectEmpty(&current_rect);
254 IntersectRect(&r, draw_rect, &current_rect);
255 if (EqualRect(&r, draw_rect))
257 /* current_rect ⊇ draw_rect, modify only. */
258 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
259 return;
262 if (EqualRect(&r, &current_rect))
264 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
266 if (!clear_rect)
268 /* Full clear, modify only. */
269 *out_rect = *draw_rect;
270 return;
273 IntersectRect(&r, draw_rect, clear_rect);
274 if (EqualRect(&r, draw_rect))
276 /* clear_rect ⊇ draw_rect, modify only. */
277 *out_rect = *draw_rect;
278 return;
282 /* Full load. */
283 surface_load_ds_location(ds, context, location);
284 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
287 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
288 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
289 float depth, DWORD stencil)
291 struct wined3d_surface *target = rt_count ? wined3d_rendertarget_view_get_surface(fb->render_targets[0]) : NULL;
292 struct wined3d_surface *depth_stencil = fb->depth_stencil
293 ? wined3d_rendertarget_view_get_surface(fb->depth_stencil) : NULL;
294 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
295 const struct wined3d_gl_info *gl_info;
296 UINT drawable_width, drawable_height;
297 struct wined3d_context *context;
298 GLbitfield clear_mask = 0;
299 BOOL render_offscreen;
300 unsigned int i;
301 RECT ds_rect;
303 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
304 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
305 * for the cleared parts, and the untouched parts.
307 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
308 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
309 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
310 * checking all this if the dest surface is in the drawable anyway. */
311 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
313 for (i = 0; i < rt_count; ++i)
315 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
316 if (rt)
317 surface_load_location(rt, rt->container->resource.draw_binding);
321 context = context_acquire(device, target);
322 if (!context->valid)
324 context_release(context);
325 WARN("Invalid context, skipping clear.\n");
326 return;
328 gl_info = context->gl_info;
330 if (target)
332 render_offscreen = context->render_offscreen;
333 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
335 else
337 render_offscreen = TRUE;
338 drawable_width = depth_stencil->pow2Width;
339 drawable_height = depth_stencil->pow2Height;
342 if (flags & WINED3DCLEAR_ZBUFFER)
344 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
346 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
347 device_switch_onscreen_ds(device, context, depth_stencil);
348 prepare_ds_clear(depth_stencil, context, location,
349 draw_rect, rect_count, clear_rect, &ds_rect);
352 if (!context_apply_clear_state(context, device, rt_count, fb))
354 context_release(context);
355 WARN("Failed to apply clear state, skipping clear.\n");
356 return;
359 /* Only set the values up once, as they are not changing. */
360 if (flags & WINED3DCLEAR_STENCIL)
362 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
364 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
365 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
367 gl_info->gl_ops.gl.p_glStencilMask(~0U);
368 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
369 gl_info->gl_ops.gl.p_glClearStencil(stencil);
370 checkGLcall("glClearStencil");
371 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
374 if (flags & WINED3DCLEAR_ZBUFFER)
376 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
378 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
380 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
382 gl_info->gl_ops.gl.p_glClearDepth(depth);
383 checkGLcall("glClearDepth");
384 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
387 if (flags & WINED3DCLEAR_TARGET)
389 for (i = 0; i < rt_count; ++i)
391 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
393 if (rt)
395 surface_validate_location(rt, rt->container->resource.draw_binding);
396 surface_invalidate_location(rt, ~rt->container->resource.draw_binding);
400 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
401 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
402 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
403 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
404 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
405 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
406 checkGLcall("glClearColor");
407 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
410 if (!clear_rect)
412 if (render_offscreen)
414 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
415 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
417 else
419 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
420 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
422 checkGLcall("glScissor");
423 gl_info->gl_ops.gl.p_glClear(clear_mask);
424 checkGLcall("glClear");
426 else
428 RECT current_rect;
430 /* Now process each rect in turn. */
431 for (i = 0; i < rect_count; ++i)
433 /* Note that GL uses lower left, width/height. */
434 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
436 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
437 wine_dbgstr_rect(&clear_rect[i]),
438 wine_dbgstr_rect(&current_rect));
440 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
441 * The rectangle is not cleared, no error is returned, but further rectangles are
442 * still cleared if they are valid. */
443 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
445 TRACE("Rectangle with negative dimensions, ignoring.\n");
446 continue;
449 if (render_offscreen)
451 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
452 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
454 else
456 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
457 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
459 checkGLcall("glScissor");
461 gl_info->gl_ops.gl.p_glClear(clear_mask);
462 checkGLcall("glClear");
466 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
467 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
468 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
470 context_release(context);
473 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
475 ULONG refcount = InterlockedIncrement(&device->ref);
477 TRACE("%p increasing refcount to %u.\n", device, refcount);
479 return refcount;
482 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
484 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
486 ERR("Leftover sampler %p.\n", sampler);
489 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
491 ULONG refcount = InterlockedDecrement(&device->ref);
493 TRACE("%p decreasing refcount to %u.\n", device, refcount);
495 if (!refcount)
497 UINT i;
499 wined3d_cs_destroy(device->cs);
501 if (device->recording && wined3d_stateblock_decref(device->recording))
502 FIXME("Something's still holding the recording stateblock.\n");
503 device->recording = NULL;
505 state_cleanup(&device->state);
507 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
509 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
510 device->multistate_funcs[i] = NULL;
513 if (!list_empty(&device->resources))
515 struct wined3d_resource *resource;
517 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
519 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
521 FIXME("Leftover resource %p with type %s (%#x).\n",
522 resource, debug_d3dresourcetype(resource->type), resource->type);
526 if (device->contexts)
527 ERR("Context array not freed!\n");
528 if (device->hardwareCursor)
529 DestroyCursor(device->hardwareCursor);
530 device->hardwareCursor = 0;
532 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
534 wined3d_decref(device->wined3d);
535 device->wined3d = NULL;
536 HeapFree(GetProcessHeap(), 0, device);
537 TRACE("Freed device %p.\n", device);
540 return refcount;
543 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
545 TRACE("device %p.\n", device);
547 return device->swapchain_count;
550 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
552 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
554 if (swapchain_idx >= device->swapchain_count)
556 WARN("swapchain_idx %u >= swapchain_count %u.\n",
557 swapchain_idx, device->swapchain_count);
558 return NULL;
561 return device->swapchains[swapchain_idx];
564 static void device_load_logo(struct wined3d_device *device, const char *filename)
566 struct wined3d_color_key color_key;
567 struct wined3d_resource_desc desc;
568 struct wined3d_surface *surface;
569 HBITMAP hbm;
570 BITMAP bm;
571 HRESULT hr;
572 HDC dcb = NULL, dcs = NULL;
574 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
575 if(hbm)
577 GetObjectA(hbm, sizeof(BITMAP), &bm);
578 dcb = CreateCompatibleDC(NULL);
579 if(!dcb) goto out;
580 SelectObject(dcb, hbm);
582 else
584 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
585 * couldn't be loaded
587 memset(&bm, 0, sizeof(bm));
588 bm.bmWidth = 32;
589 bm.bmHeight = 32;
592 desc.resource_type = WINED3D_RTYPE_TEXTURE;
593 desc.format = WINED3DFMT_B5G6R5_UNORM;
594 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
595 desc.multisample_quality = 0;
596 desc.usage = WINED3DUSAGE_DYNAMIC;
597 desc.pool = WINED3D_POOL_DEFAULT;
598 desc.width = bm.bmWidth;
599 desc.height = bm.bmHeight;
600 desc.depth = 1;
601 desc.size = 0;
602 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
603 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
605 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
606 goto out;
608 surface = surface_from_resource(wined3d_texture_get_sub_resource(device->logo_texture, 0));
610 if (dcb)
612 if (FAILED(hr = wined3d_surface_getdc(surface, &dcs)))
613 goto out;
614 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
615 wined3d_surface_releasedc(surface, dcs);
617 color_key.color_space_low_value = 0;
618 color_key.color_space_high_value = 0;
619 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
621 else
623 const RECT rect = {0, 0, surface->resource.width, surface->resource.height};
624 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
626 /* Fill the surface with a white color to show that wined3d is there */
627 surface_color_fill(surface, &rect, &c);
630 out:
631 if (dcb) DeleteDC(dcb);
632 if (hbm) DeleteObject(hbm);
635 /* Context activation is done by the caller. */
636 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
638 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
639 unsigned int i, j, count;
640 /* Under DirectX you can sample even if no texture is bound, whereas
641 * OpenGL will only allow that when a valid texture is bound.
642 * We emulate this by creating dummy textures and binding them
643 * to each texture stage when the currently set D3D texture is NULL. */
645 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
646 for (i = 0; i < count; ++i)
648 DWORD color = 0x000000ff;
650 /* Make appropriate texture active */
651 context_active_texture(context, gl_info, i);
653 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
654 checkGLcall("glGenTextures");
655 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
657 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
658 checkGLcall("glBindTexture");
660 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
661 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
662 checkGLcall("glTexImage2D");
664 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
666 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
667 checkGLcall("glGenTextures");
668 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
670 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
671 checkGLcall("glBindTexture");
673 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
674 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
675 checkGLcall("glTexImage2D");
678 if (gl_info->supported[EXT_TEXTURE3D])
680 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
681 checkGLcall("glGenTextures");
682 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
684 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
685 checkGLcall("glBindTexture");
687 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
688 checkGLcall("glTexImage3D");
691 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
693 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
694 checkGLcall("glGenTextures");
695 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
697 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
698 checkGLcall("glBindTexture");
700 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
702 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
703 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
704 checkGLcall("glTexImage2D");
710 /* Context activation is done by the caller. */
711 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
713 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
715 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
717 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
718 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
721 if (gl_info->supported[EXT_TEXTURE3D])
723 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
724 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
727 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
729 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
730 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
733 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
734 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
736 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
737 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
738 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
739 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
742 static LONG fullscreen_style(LONG style)
744 /* Make sure the window is managed, otherwise we won't get keyboard input. */
745 style |= WS_POPUP | WS_SYSMENU;
746 style &= ~(WS_CAPTION | WS_THICKFRAME);
748 return style;
751 static LONG fullscreen_exstyle(LONG exstyle)
753 /* Filter out window decorations. */
754 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
756 return exstyle;
759 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
761 BOOL filter_messages;
762 LONG style, exstyle;
764 TRACE("Setting up window %p for fullscreen mode.\n", window);
766 if (device->style || device->exStyle)
768 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
769 window, device->style, device->exStyle);
772 device->style = GetWindowLongW(window, GWL_STYLE);
773 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
775 style = fullscreen_style(device->style);
776 exstyle = fullscreen_exstyle(device->exStyle);
778 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
779 device->style, device->exStyle, style, exstyle);
781 filter_messages = device->filter_messages;
782 device->filter_messages = TRUE;
784 SetWindowLongW(window, GWL_STYLE, style);
785 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
786 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
788 device->filter_messages = filter_messages;
791 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
793 BOOL filter_messages;
794 LONG style, exstyle;
796 if (!device->style && !device->exStyle) return;
798 style = GetWindowLongW(window, GWL_STYLE);
799 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
801 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
802 * application, and we want to ignore them in the test below, since it's
803 * not the application's fault that they changed. Additionally, we want to
804 * preserve the current status of these flags (i.e. don't restore them) to
805 * more closely emulate the behavior of Direct3D, which leaves these flags
806 * alone when returning to windowed mode. */
807 device->style ^= (device->style ^ style) & WS_VISIBLE;
808 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
810 TRACE("Restoring window style of window %p to %08x, %08x.\n",
811 window, device->style, device->exStyle);
813 filter_messages = device->filter_messages;
814 device->filter_messages = TRUE;
816 /* Only restore the style if the application didn't modify it during the
817 * fullscreen phase. Some applications change it before calling Reset()
818 * when switching between windowed and fullscreen modes (HL2), some
819 * depend on the original style (Eve Online). */
820 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
822 SetWindowLongW(window, GWL_STYLE, device->style);
823 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
825 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
827 device->filter_messages = filter_messages;
829 /* Delete the old values. */
830 device->style = 0;
831 device->exStyle = 0;
834 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
836 TRACE("device %p, window %p.\n", device, window);
838 if (!wined3d_register_window(window, device))
840 ERR("Failed to register window %p.\n", window);
841 return E_FAIL;
844 InterlockedExchangePointer((void **)&device->focus_window, window);
845 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
847 return WINED3D_OK;
850 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
852 TRACE("device %p.\n", device);
854 if (device->focus_window) wined3d_unregister_window(device->focus_window);
855 InterlockedExchangePointer((void **)&device->focus_window, NULL);
858 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
860 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
861 unsigned int i;
863 if (device->fb.render_targets)
865 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
867 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
869 if (device->back_buffer_view)
870 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
873 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
874 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
877 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
878 struct wined3d_swapchain_desc *swapchain_desc)
880 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
881 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
882 struct wined3d_swapchain *swapchain = NULL;
883 struct wined3d_context *context;
884 DWORD clear_flags = 0;
885 HRESULT hr;
887 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
889 if (device->d3d_initialized)
890 return WINED3DERR_INVALIDCALL;
891 if (device->wined3d->flags & WINED3D_NO3D)
892 return WINED3DERR_INVALIDCALL;
894 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
895 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
897 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
898 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
900 TRACE("Shader private data couldn't be allocated\n");
901 goto err_out;
903 if (FAILED(hr = device->blitter->alloc_private(device)))
905 TRACE("Blitter private data couldn't be allocated\n");
906 goto err_out;
909 /* Setup the implicit swapchain. This also initializes a context. */
910 TRACE("Creating implicit swapchain\n");
911 hr = device->device_parent->ops->create_swapchain(device->device_parent,
912 swapchain_desc, &swapchain);
913 if (FAILED(hr))
915 WARN("Failed to create implicit swapchain\n");
916 goto err_out;
919 if (swapchain_desc->backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
920 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
921 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
923 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
924 goto err_out;
927 device->swapchain_count = 1;
928 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
929 if (!device->swapchains)
931 ERR("Out of memory!\n");
932 goto err_out;
934 device->swapchains[0] = swapchain;
935 device_init_swapchain_state(device, swapchain);
937 context = context_acquire(device,
938 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)));
940 create_dummy_textures(device, context);
942 device->contexts[0]->last_was_rhw = 0;
944 switch (wined3d_settings.offscreen_rendering_mode)
946 case ORM_FBO:
947 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
948 break;
950 case ORM_BACKBUFFER:
952 if (context_get_current()->aux_buffers > 0)
954 TRACE("Using auxiliary buffer for offscreen rendering\n");
955 device->offscreenBuffer = GL_AUX0;
957 else
959 TRACE("Using back buffer for offscreen rendering\n");
960 device->offscreenBuffer = GL_BACK;
965 TRACE("All defaults now set up, leaving 3D init.\n");
967 context_release(context);
969 /* Clear the screen */
970 if (swapchain->back_buffers && swapchain->back_buffers[0])
971 clear_flags |= WINED3DCLEAR_TARGET;
972 if (swapchain_desc->enable_auto_depth_stencil)
973 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
974 if (clear_flags)
975 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
977 device->d3d_initialized = TRUE;
979 if (wined3d_settings.logo)
980 device_load_logo(device, wined3d_settings.logo);
981 return WINED3D_OK;
983 err_out:
984 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
985 HeapFree(GetProcessHeap(), 0, device->swapchains);
986 device->swapchain_count = 0;
987 if (device->back_buffer_view)
988 wined3d_rendertarget_view_decref(device->back_buffer_view);
989 if (swapchain)
990 wined3d_swapchain_decref(swapchain);
991 if (device->blit_priv)
992 device->blitter->free_private(device);
993 if (device->shader_priv)
994 device->shader_backend->shader_free_private(device);
996 return hr;
999 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1000 struct wined3d_swapchain_desc *swapchain_desc)
1002 struct wined3d_swapchain *swapchain = NULL;
1003 HRESULT hr;
1005 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1007 /* Setup the implicit swapchain */
1008 TRACE("Creating implicit swapchain\n");
1009 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1010 swapchain_desc, &swapchain);
1011 if (FAILED(hr))
1013 WARN("Failed to create implicit swapchain\n");
1014 goto err_out;
1017 device->swapchain_count = 1;
1018 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1019 if (!device->swapchains)
1021 ERR("Out of memory!\n");
1022 goto err_out;
1024 device->swapchains[0] = swapchain;
1025 return WINED3D_OK;
1027 err_out:
1028 wined3d_swapchain_decref(swapchain);
1029 return hr;
1032 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1034 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1036 wined3d_sampler_decref(sampler);
1039 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1041 struct wined3d_resource *resource, *cursor;
1042 const struct wined3d_gl_info *gl_info;
1043 struct wined3d_context *context;
1044 struct wined3d_surface *surface;
1045 UINT i;
1047 TRACE("device %p.\n", device);
1049 if (!device->d3d_initialized)
1050 return WINED3DERR_INVALIDCALL;
1052 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1053 * it was created. Thus make sure a context is active for the glDelete* calls
1055 context = context_acquire(device, NULL);
1056 gl_info = context->gl_info;
1058 if (device->logo_texture)
1059 wined3d_texture_decref(device->logo_texture);
1060 if (device->cursor_texture)
1061 wined3d_texture_decref(device->cursor_texture);
1063 state_unbind_resources(&device->state);
1065 /* Unload resources */
1066 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1068 TRACE("Unloading resource %p.\n", resource);
1070 resource->resource_ops->resource_unload(resource);
1073 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1075 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1076 * private data, it might contain opengl pointers
1078 if (device->depth_blt_texture)
1080 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1081 device->depth_blt_texture = 0;
1084 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1085 device->blitter->free_private(device);
1086 device->shader_backend->shader_free_private(device);
1087 destroy_dummy_textures(device, gl_info);
1089 /* Release the context again as soon as possible. In particular,
1090 * releasing the render target views below may release the last reference
1091 * to the swapchain associated with this context, which in turn will
1092 * destroy the context. */
1093 context_release(context);
1095 /* Release the buffers (with sanity checks)*/
1096 if (device->onscreen_depth_stencil)
1098 surface = device->onscreen_depth_stencil;
1099 device->onscreen_depth_stencil = NULL;
1100 wined3d_surface_decref(surface);
1103 if (device->fb.depth_stencil)
1105 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1107 TRACE("Releasing depth/stencil view %p.\n", view);
1109 device->fb.depth_stencil = NULL;
1110 wined3d_rendertarget_view_decref(view);
1113 if (device->auto_depth_stencil_view)
1115 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1117 device->auto_depth_stencil_view = NULL;
1118 if (wined3d_rendertarget_view_decref(view))
1119 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1122 for (i = 0; i < gl_info->limits.buffers; ++i)
1124 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1126 if (device->back_buffer_view)
1128 wined3d_rendertarget_view_decref(device->back_buffer_view);
1129 device->back_buffer_view = NULL;
1132 for (i = 0; i < device->swapchain_count; ++i)
1134 TRACE("Releasing the implicit swapchain %u.\n", i);
1135 if (wined3d_swapchain_decref(device->swapchains[i]))
1136 FIXME("Something's still holding the implicit swapchain.\n");
1139 HeapFree(GetProcessHeap(), 0, device->swapchains);
1140 device->swapchains = NULL;
1141 device->swapchain_count = 0;
1143 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1144 device->fb.render_targets = NULL;
1146 device->d3d_initialized = FALSE;
1148 return WINED3D_OK;
1151 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1153 unsigned int i;
1155 for (i = 0; i < device->swapchain_count; ++i)
1157 TRACE("Releasing the implicit swapchain %u.\n", i);
1158 if (wined3d_swapchain_decref(device->swapchains[i]))
1159 FIXME("Something's still holding the implicit swapchain.\n");
1162 HeapFree(GetProcessHeap(), 0, device->swapchains);
1163 device->swapchains = NULL;
1164 device->swapchain_count = 0;
1165 return WINED3D_OK;
1168 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1169 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1170 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1172 * There is no way to deactivate thread safety once it is enabled.
1174 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1176 TRACE("device %p.\n", device);
1178 /* For now just store the flag (needed in case of ddraw). */
1179 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1182 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1184 TRACE("device %p.\n", device);
1186 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1187 wine_dbgstr_longlong(device->adapter->vram_bytes),
1188 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1189 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1191 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1194 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1195 struct wined3d_buffer *buffer, UINT offset)
1197 struct wined3d_stream_output *stream;
1198 struct wined3d_buffer *prev_buffer;
1200 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1202 if (idx >= MAX_STREAM_OUT)
1204 WARN("Invalid stream output %u.\n", idx);
1205 return;
1208 stream = &device->update_state->stream_output[idx];
1209 prev_buffer = stream->buffer;
1211 if (buffer)
1212 wined3d_buffer_incref(buffer);
1213 stream->buffer = buffer;
1214 stream->offset = offset;
1215 if (!device->recording)
1216 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1217 if (prev_buffer)
1218 wined3d_buffer_decref(prev_buffer);
1221 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1222 UINT idx, UINT *offset)
1224 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1226 if (idx >= MAX_STREAM_OUT)
1228 WARN("Invalid stream output %u.\n", idx);
1229 return NULL;
1232 *offset = device->state.stream_output[idx].offset;
1233 return device->state.stream_output[idx].buffer;
1236 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1237 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1239 struct wined3d_stream_state *stream;
1240 struct wined3d_buffer *prev_buffer;
1242 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1243 device, stream_idx, buffer, offset, stride);
1245 if (stream_idx >= MAX_STREAMS)
1247 WARN("Stream index %u out of range.\n", stream_idx);
1248 return WINED3DERR_INVALIDCALL;
1250 else if (offset & 0x3)
1252 WARN("Offset %u is not 4 byte aligned.\n", offset);
1253 return WINED3DERR_INVALIDCALL;
1256 stream = &device->update_state->streams[stream_idx];
1257 prev_buffer = stream->buffer;
1259 if (device->recording)
1260 device->recording->changed.streamSource |= 1 << stream_idx;
1262 if (prev_buffer == buffer
1263 && stream->stride == stride
1264 && stream->offset == offset)
1266 TRACE("Application is setting the old values over, nothing to do.\n");
1267 return WINED3D_OK;
1270 stream->buffer = buffer;
1271 if (buffer)
1273 stream->stride = stride;
1274 stream->offset = offset;
1277 if (buffer)
1278 wined3d_buffer_incref(buffer);
1279 if (!device->recording)
1280 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1281 if (prev_buffer)
1282 wined3d_buffer_decref(prev_buffer);
1284 return WINED3D_OK;
1287 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1288 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1290 const struct wined3d_stream_state *stream;
1292 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1293 device, stream_idx, buffer, offset, stride);
1295 if (stream_idx >= MAX_STREAMS)
1297 WARN("Stream index %u out of range.\n", stream_idx);
1298 return WINED3DERR_INVALIDCALL;
1301 stream = &device->state.streams[stream_idx];
1302 *buffer = stream->buffer;
1303 if (offset)
1304 *offset = stream->offset;
1305 *stride = stream->stride;
1307 return WINED3D_OK;
1310 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1312 struct wined3d_stream_state *stream;
1313 UINT old_flags, old_freq;
1315 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1317 /* Verify input. At least in d3d9 this is invalid. */
1318 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1320 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1321 return WINED3DERR_INVALIDCALL;
1323 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1325 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1326 return WINED3DERR_INVALIDCALL;
1328 if (!divider)
1330 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1331 return WINED3DERR_INVALIDCALL;
1334 stream = &device->update_state->streams[stream_idx];
1335 old_flags = stream->flags;
1336 old_freq = stream->frequency;
1338 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1339 stream->frequency = divider & 0x7fffff;
1341 if (device->recording)
1342 device->recording->changed.streamFreq |= 1 << stream_idx;
1343 else if (stream->frequency != old_freq || stream->flags != old_flags)
1344 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1346 return WINED3D_OK;
1349 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1350 UINT stream_idx, UINT *divider)
1352 const struct wined3d_stream_state *stream;
1354 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1356 stream = &device->state.streams[stream_idx];
1357 *divider = stream->flags | stream->frequency;
1359 TRACE("Returning %#x.\n", *divider);
1361 return WINED3D_OK;
1364 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1365 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1367 TRACE("device %p, state %s, matrix %p.\n",
1368 device, debug_d3dtstype(d3dts), matrix);
1369 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1370 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1371 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1372 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1374 /* Handle recording of state blocks. */
1375 if (device->recording)
1377 TRACE("Recording... not performing anything.\n");
1378 device->recording->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1379 device->update_state->transforms[d3dts] = *matrix;
1380 return;
1383 /* If the new matrix is the same as the current one,
1384 * we cut off any further processing. this seems to be a reasonable
1385 * optimization because as was noticed, some apps (warcraft3 for example)
1386 * tend towards setting the same matrix repeatedly for some reason.
1388 * From here on we assume that the new matrix is different, wherever it matters. */
1389 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1391 TRACE("The application is setting the same matrix over again.\n");
1392 return;
1395 device->state.transforms[d3dts] = *matrix;
1396 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1399 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1400 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1402 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1404 *matrix = device->state.transforms[state];
1407 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1408 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1410 const struct wined3d_matrix *mat;
1411 struct wined3d_matrix temp;
1413 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1415 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1416 * below means it will be recorded in a state block change, but it
1417 * works regardless where it is recorded.
1418 * If this is found to be wrong, change to StateBlock. */
1419 if (state > HIGHEST_TRANSFORMSTATE)
1421 WARN("Unhandled transform state %#x.\n", state);
1422 return;
1425 mat = &device->update_state->transforms[state];
1426 multiply_matrix(&temp, mat, matrix);
1428 /* Apply change via set transform - will reapply to eg. lights this way. */
1429 wined3d_device_set_transform(device, state, &temp);
1432 /* Note lights are real special cases. Although the device caps state only
1433 * e.g. 8 are supported, you can reference any indexes you want as long as
1434 * that number max are enabled at any one point in time. Therefore since the
1435 * indices can be anything, we need a hashmap of them. However, this causes
1436 * stateblock problems. When capturing the state block, I duplicate the
1437 * hashmap, but when recording, just build a chain pretty much of commands to
1438 * be replayed. */
1439 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1440 UINT light_idx, const struct wined3d_light *light)
1442 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1443 struct wined3d_light_info *object = NULL;
1444 struct list *e;
1445 float rho;
1447 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1449 /* Check the parameter range. Need for speed most wanted sets junk lights
1450 * which confuse the GL driver. */
1451 if (!light)
1452 return WINED3DERR_INVALIDCALL;
1454 switch (light->type)
1456 case WINED3D_LIGHT_POINT:
1457 case WINED3D_LIGHT_SPOT:
1458 case WINED3D_LIGHT_PARALLELPOINT:
1459 case WINED3D_LIGHT_GLSPOT:
1460 /* Incorrect attenuation values can cause the gl driver to crash.
1461 * Happens with Need for speed most wanted. */
1462 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1464 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1465 return WINED3DERR_INVALIDCALL;
1467 break;
1469 case WINED3D_LIGHT_DIRECTIONAL:
1470 /* Ignores attenuation */
1471 break;
1473 default:
1474 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1475 return WINED3DERR_INVALIDCALL;
1478 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1480 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1481 if (object->OriginalIndex == light_idx)
1482 break;
1483 object = NULL;
1486 if (!object)
1488 TRACE("Adding new light\n");
1489 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1490 if (!object)
1491 return E_OUTOFMEMORY;
1493 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1494 object->glIndex = -1;
1495 object->OriginalIndex = light_idx;
1498 /* Initialize the object. */
1499 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1500 light_idx, light->type,
1501 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1502 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1503 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1504 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1505 light->direction.x, light->direction.y, light->direction.z);
1506 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1507 light->range, light->falloff, light->theta, light->phi);
1509 /* Update the live definitions if the light is currently assigned a glIndex. */
1510 if (object->glIndex != -1 && !device->recording)
1512 if (object->OriginalParms.type != light->type)
1513 device_invalidate_state(device, STATE_LIGHT_TYPE);
1514 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1517 /* Save away the information. */
1518 object->OriginalParms = *light;
1520 switch (light->type)
1522 case WINED3D_LIGHT_POINT:
1523 /* Position */
1524 object->lightPosn[0] = light->position.x;
1525 object->lightPosn[1] = light->position.y;
1526 object->lightPosn[2] = light->position.z;
1527 object->lightPosn[3] = 1.0f;
1528 object->cutoff = 180.0f;
1529 /* FIXME: Range */
1530 break;
1532 case WINED3D_LIGHT_DIRECTIONAL:
1533 /* Direction */
1534 object->lightPosn[0] = -light->direction.x;
1535 object->lightPosn[1] = -light->direction.y;
1536 object->lightPosn[2] = -light->direction.z;
1537 object->lightPosn[3] = 0.0f;
1538 object->exponent = 0.0f;
1539 object->cutoff = 180.0f;
1540 break;
1542 case WINED3D_LIGHT_SPOT:
1543 /* Position */
1544 object->lightPosn[0] = light->position.x;
1545 object->lightPosn[1] = light->position.y;
1546 object->lightPosn[2] = light->position.z;
1547 object->lightPosn[3] = 1.0f;
1549 /* Direction */
1550 object->lightDirn[0] = light->direction.x;
1551 object->lightDirn[1] = light->direction.y;
1552 object->lightDirn[2] = light->direction.z;
1553 object->lightDirn[3] = 1.0f;
1555 /* opengl-ish and d3d-ish spot lights use too different models
1556 * for the light "intensity" as a function of the angle towards
1557 * the main light direction, so we only can approximate very
1558 * roughly. However, spot lights are rather rarely used in games
1559 * (if ever used at all). Furthermore if still used, probably
1560 * nobody pays attention to such details. */
1561 if (!light->falloff)
1563 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1564 * equations have the falloff resp. exponent parameter as an
1565 * exponent, so the spot light lighting will always be 1.0 for
1566 * both of them, and we don't have to care for the rest of the
1567 * rather complex calculation. */
1568 object->exponent = 0.0f;
1570 else
1572 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1573 if (rho < 0.0001f)
1574 rho = 0.0001f;
1575 object->exponent = -0.3f / logf(cosf(rho / 2));
1578 if (object->exponent > 128.0f)
1579 object->exponent = 128.0f;
1581 object->cutoff = (float)(light->phi * 90 / M_PI);
1582 /* FIXME: Range */
1583 break;
1585 default:
1586 FIXME("Unrecognized light type %#x.\n", light->type);
1589 return WINED3D_OK;
1592 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1593 UINT light_idx, struct wined3d_light *light)
1595 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1596 struct wined3d_light_info *light_info = NULL;
1597 struct list *e;
1599 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1601 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1603 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1604 if (light_info->OriginalIndex == light_idx)
1605 break;
1606 light_info = NULL;
1609 if (!light_info)
1611 TRACE("Light information requested but light not defined\n");
1612 return WINED3DERR_INVALIDCALL;
1615 *light = light_info->OriginalParms;
1616 return WINED3D_OK;
1619 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1621 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1622 struct wined3d_light_info *light_info = NULL;
1623 struct list *e;
1625 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1627 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1629 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1630 if (light_info->OriginalIndex == light_idx)
1631 break;
1632 light_info = NULL;
1634 TRACE("Found light %p.\n", light_info);
1636 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1637 if (!light_info)
1639 TRACE("Light enabled requested but light not defined, so defining one!\n");
1640 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1642 /* Search for it again! Should be fairly quick as near head of list. */
1643 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1645 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1646 if (light_info->OriginalIndex == light_idx)
1647 break;
1648 light_info = NULL;
1650 if (!light_info)
1652 FIXME("Adding default lights has failed dismally\n");
1653 return WINED3DERR_INVALIDCALL;
1657 if (!enable)
1659 if (light_info->glIndex != -1)
1661 if (!device->recording)
1663 device_invalidate_state(device, STATE_LIGHT_TYPE);
1664 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1667 device->update_state->lights[light_info->glIndex] = NULL;
1668 light_info->glIndex = -1;
1670 else
1672 TRACE("Light already disabled, nothing to do\n");
1674 light_info->enabled = FALSE;
1676 else
1678 light_info->enabled = TRUE;
1679 if (light_info->glIndex != -1)
1681 TRACE("Nothing to do as light was enabled\n");
1683 else
1685 unsigned int i;
1686 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1687 /* Find a free GL light. */
1688 for (i = 0; i < gl_info->limits.lights; ++i)
1690 if (!device->update_state->lights[i])
1692 device->update_state->lights[i] = light_info;
1693 light_info->glIndex = i;
1694 break;
1697 if (light_info->glIndex == -1)
1699 /* Our tests show that Windows returns D3D_OK in this situation, even with
1700 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1701 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1702 * as well for those lights.
1704 * TODO: Test how this affects rendering. */
1705 WARN("Too many concurrently active lights\n");
1706 return WINED3D_OK;
1709 /* i == light_info->glIndex */
1710 if (!device->recording)
1712 device_invalidate_state(device, STATE_LIGHT_TYPE);
1713 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1718 return WINED3D_OK;
1721 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1723 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1724 struct wined3d_light_info *light_info = NULL;
1725 struct list *e;
1727 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1729 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1731 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1732 if (light_info->OriginalIndex == light_idx)
1733 break;
1734 light_info = NULL;
1737 if (!light_info)
1739 TRACE("Light enabled state requested but light not defined.\n");
1740 return WINED3DERR_INVALIDCALL;
1742 /* true is 128 according to SetLightEnable */
1743 *enable = light_info->enabled ? 128 : 0;
1744 return WINED3D_OK;
1747 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1748 UINT plane_idx, const struct wined3d_vec4 *plane)
1750 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1752 /* Validate plane_idx. */
1753 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1755 TRACE("Application has requested clipplane this device doesn't support.\n");
1756 return WINED3DERR_INVALIDCALL;
1759 if (device->recording)
1760 device->recording->changed.clipplane |= 1 << plane_idx;
1762 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1764 TRACE("Application is setting old values over, nothing to do.\n");
1765 return WINED3D_OK;
1768 device->update_state->clip_planes[plane_idx] = *plane;
1770 if (!device->recording)
1771 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1773 return WINED3D_OK;
1776 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1777 UINT plane_idx, struct wined3d_vec4 *plane)
1779 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1781 /* Validate plane_idx. */
1782 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1784 TRACE("Application has requested clipplane this device doesn't support.\n");
1785 return WINED3DERR_INVALIDCALL;
1788 *plane = device->state.clip_planes[plane_idx];
1790 return WINED3D_OK;
1793 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1794 const struct wined3d_clip_status *clip_status)
1796 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1798 if (!clip_status)
1799 return WINED3DERR_INVALIDCALL;
1801 return WINED3D_OK;
1804 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1805 struct wined3d_clip_status *clip_status)
1807 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1809 if (!clip_status)
1810 return WINED3DERR_INVALIDCALL;
1812 return WINED3D_OK;
1815 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1817 TRACE("device %p, material %p.\n", device, material);
1819 device->update_state->material = *material;
1821 if (device->recording)
1822 device->recording->changed.material = TRUE;
1823 else
1824 wined3d_cs_emit_set_material(device->cs, material);
1827 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1829 TRACE("device %p, material %p.\n", device, material);
1831 *material = device->state.material;
1833 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1834 material->diffuse.r, material->diffuse.g,
1835 material->diffuse.b, material->diffuse.a);
1836 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1837 material->ambient.r, material->ambient.g,
1838 material->ambient.b, material->ambient.a);
1839 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1840 material->specular.r, material->specular.g,
1841 material->specular.b, material->specular.a);
1842 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1843 material->emissive.r, material->emissive.g,
1844 material->emissive.b, material->emissive.a);
1845 TRACE("power %.8e.\n", material->power);
1848 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1849 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1851 enum wined3d_format_id prev_format;
1852 struct wined3d_buffer *prev_buffer;
1854 TRACE("device %p, buffer %p, format %s.\n",
1855 device, buffer, debug_d3dformat(format_id));
1857 prev_buffer = device->update_state->index_buffer;
1858 prev_format = device->update_state->index_format;
1860 device->update_state->index_buffer = buffer;
1861 device->update_state->index_format = format_id;
1863 if (device->recording)
1864 device->recording->changed.indices = TRUE;
1866 if (prev_buffer == buffer && prev_format == format_id)
1867 return;
1869 if (buffer)
1870 wined3d_buffer_incref(buffer);
1871 if (!device->recording)
1872 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1873 if (prev_buffer)
1874 wined3d_buffer_decref(prev_buffer);
1877 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1878 enum wined3d_format_id *format)
1880 TRACE("device %p, format %p.\n", device, format);
1882 *format = device->state.index_format;
1883 return device->state.index_buffer;
1886 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1888 TRACE("device %p, base_index %d.\n", device, base_index);
1890 device->update_state->base_vertex_index = base_index;
1893 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1895 TRACE("device %p.\n", device);
1897 return device->state.base_vertex_index;
1900 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1902 TRACE("device %p, viewport %p.\n", device, viewport);
1903 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1904 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1906 device->update_state->viewport = *viewport;
1908 /* Handle recording of state blocks */
1909 if (device->recording)
1911 TRACE("Recording... not performing anything\n");
1912 device->recording->changed.viewport = TRUE;
1913 return;
1916 wined3d_cs_emit_set_viewport(device->cs, viewport);
1919 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1921 TRACE("device %p, viewport %p.\n", device, viewport);
1923 *viewport = device->state.viewport;
1926 static void resolve_depth_buffer(struct wined3d_state *state)
1928 struct wined3d_texture *texture = state->textures[0];
1929 struct wined3d_surface *depth_stencil, *surface;
1931 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1932 || !(texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1933 return;
1934 surface = surface_from_resource(texture->sub_resources[0]);
1935 if (!(depth_stencil = wined3d_rendertarget_view_get_surface(state->fb->depth_stencil)))
1936 return;
1938 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1941 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1942 enum wined3d_render_state state, DWORD value)
1944 DWORD old_value = device->state.render_states[state];
1946 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1948 device->update_state->render_states[state] = value;
1950 /* Handle recording of state blocks. */
1951 if (device->recording)
1953 TRACE("Recording... not performing anything.\n");
1954 device->recording->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
1955 return;
1958 /* Compared here and not before the assignment to allow proper stateblock recording. */
1959 if (value == old_value)
1960 TRACE("Application is setting the old value over, nothing to do.\n");
1961 else
1962 wined3d_cs_emit_set_render_state(device->cs, state, value);
1964 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1966 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1967 resolve_depth_buffer(&device->state);
1971 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1973 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1975 return device->state.render_states[state];
1978 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1979 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1981 DWORD old_value;
1983 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1984 device, sampler_idx, debug_d3dsamplerstate(state), value);
1986 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1987 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1989 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1991 WARN("Invalid sampler %u.\n", sampler_idx);
1992 return; /* Windows accepts overflowing this array ... we do not. */
1995 old_value = device->state.sampler_states[sampler_idx][state];
1996 device->update_state->sampler_states[sampler_idx][state] = value;
1998 /* Handle recording of state blocks. */
1999 if (device->recording)
2001 TRACE("Recording... not performing anything.\n");
2002 device->recording->changed.samplerState[sampler_idx] |= 1 << state;
2003 return;
2006 if (old_value == value)
2008 TRACE("Application is setting the old value over, nothing to do.\n");
2009 return;
2012 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2015 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2016 UINT sampler_idx, enum wined3d_sampler_state state)
2018 TRACE("device %p, sampler_idx %u, state %s.\n",
2019 device, sampler_idx, debug_d3dsamplerstate(state));
2021 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2022 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2024 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2026 WARN("Invalid sampler %u.\n", sampler_idx);
2027 return 0; /* Windows accepts overflowing this array ... we do not. */
2030 return device->state.sampler_states[sampler_idx][state];
2033 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2035 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2037 if (device->recording)
2038 device->recording->changed.scissorRect = TRUE;
2040 if (EqualRect(&device->update_state->scissor_rect, rect))
2042 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2043 return;
2045 CopyRect(&device->update_state->scissor_rect, rect);
2047 if (device->recording)
2049 TRACE("Recording... not performing anything.\n");
2050 return;
2053 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2056 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2058 TRACE("device %p, rect %p.\n", device, rect);
2060 *rect = device->state.scissor_rect;
2061 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2064 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2065 struct wined3d_vertex_declaration *declaration)
2067 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2069 TRACE("device %p, declaration %p.\n", device, declaration);
2071 if (device->recording)
2072 device->recording->changed.vertexDecl = TRUE;
2074 if (declaration == prev)
2075 return;
2077 if (declaration)
2078 wined3d_vertex_declaration_incref(declaration);
2079 device->update_state->vertex_declaration = declaration;
2080 if (!device->recording)
2081 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2082 if (prev)
2083 wined3d_vertex_declaration_decref(prev);
2086 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2088 TRACE("device %p.\n", device);
2090 return device->state.vertex_declaration;
2093 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2095 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2097 TRACE("device %p, shader %p.\n", device, shader);
2099 if (device->recording)
2100 device->recording->changed.vertexShader = TRUE;
2102 if (shader == prev)
2103 return;
2105 if (shader)
2106 wined3d_shader_incref(shader);
2107 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2108 if (!device->recording)
2109 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2110 if (prev)
2111 wined3d_shader_decref(prev);
2114 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2116 TRACE("device %p.\n", device);
2118 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2121 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2122 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2124 struct wined3d_buffer *prev;
2126 if (idx >= MAX_CONSTANT_BUFFERS)
2128 WARN("Invalid constant buffer index %u.\n", idx);
2129 return;
2132 prev = device->update_state->cb[type][idx];
2133 if (buffer == prev)
2134 return;
2136 if (buffer)
2137 wined3d_buffer_incref(buffer);
2138 device->update_state->cb[type][idx] = buffer;
2139 if (!device->recording)
2140 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2141 if (prev)
2142 wined3d_buffer_decref(prev);
2145 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2147 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2149 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2152 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2154 TRACE("device %p, idx %u.\n", device, idx);
2156 if (idx >= MAX_CONSTANT_BUFFERS)
2158 WARN("Invalid constant buffer index %u.\n", idx);
2159 return NULL;
2162 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2165 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2166 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2168 struct wined3d_shader_resource_view *prev;
2170 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2172 WARN("Invalid view index %u.\n", idx);
2173 return;
2176 prev = device->update_state->shader_resource_view[type][idx];
2177 if (view == prev)
2178 return;
2180 if (view)
2181 wined3d_shader_resource_view_incref(view);
2182 device->update_state->shader_resource_view[type][idx] = view;
2183 if (!device->recording)
2184 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2185 if (prev)
2186 wined3d_shader_resource_view_decref(prev);
2189 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2190 UINT idx, struct wined3d_shader_resource_view *view)
2192 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2194 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2197 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2198 UINT idx)
2200 TRACE("device %p, idx %u.\n", device, idx);
2202 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2204 WARN("Invalid view index %u.\n", idx);
2205 return NULL;
2208 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2211 static void wined3d_device_set_sampler(struct wined3d_device *device,
2212 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2214 struct wined3d_sampler *prev;
2216 if (idx >= MAX_SAMPLER_OBJECTS)
2218 WARN("Invalid sampler index %u.\n", idx);
2219 return;
2222 prev = device->update_state->sampler[type][idx];
2223 if (sampler == prev)
2224 return;
2226 if (sampler)
2227 wined3d_sampler_incref(sampler);
2228 device->update_state->sampler[type][idx] = sampler;
2229 if (!device->recording)
2230 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2231 if (prev)
2232 wined3d_sampler_decref(prev);
2235 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2237 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2239 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2242 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2244 TRACE("device %p, idx %u.\n", device, idx);
2246 if (idx >= MAX_SAMPLER_OBJECTS)
2248 WARN("Invalid sampler index %u.\n", idx);
2249 return NULL;
2252 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2255 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2257 UINT i;
2259 for (i = 0; i < device->context_count; ++i)
2261 device->contexts[i]->constant_update_mask |= mask;
2265 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2266 UINT start_register, const BOOL *constants, UINT bool_count)
2268 UINT count = min(bool_count, MAX_CONST_B - start_register);
2269 UINT i;
2271 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2272 device, start_register, constants, bool_count);
2274 if (!constants || start_register >= MAX_CONST_B)
2275 return WINED3DERR_INVALIDCALL;
2277 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2278 for (i = 0; i < count; ++i)
2279 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2281 if (device->recording)
2283 for (i = start_register; i < count + start_register; ++i)
2284 device->recording->changed.vertexShaderConstantsB |= (1 << i);
2286 else
2288 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2291 return WINED3D_OK;
2294 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2295 UINT start_register, BOOL *constants, UINT bool_count)
2297 UINT count = min(bool_count, MAX_CONST_B - start_register);
2299 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2300 device, start_register, constants, bool_count);
2302 if (!constants || start_register >= MAX_CONST_B)
2303 return WINED3DERR_INVALIDCALL;
2305 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2307 return WINED3D_OK;
2310 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2311 UINT start_register, const int *constants, UINT vector4i_count)
2313 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2314 UINT i;
2316 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2317 device, start_register, constants, vector4i_count);
2319 if (!constants || start_register >= MAX_CONST_I)
2320 return WINED3DERR_INVALIDCALL;
2322 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2323 for (i = 0; i < count; ++i)
2324 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2325 constants[i * 4], constants[i * 4 + 1],
2326 constants[i * 4 + 2], constants[i * 4 + 3]);
2328 if (device->recording)
2330 for (i = start_register; i < count + start_register; ++i)
2331 device->recording->changed.vertexShaderConstantsI |= (1 << i);
2333 else
2335 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2338 return WINED3D_OK;
2341 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2342 UINT start_register, int *constants, UINT vector4i_count)
2344 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2346 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2347 device, start_register, constants, vector4i_count);
2349 if (!constants || start_register >= MAX_CONST_I)
2350 return WINED3DERR_INVALIDCALL;
2352 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2353 return WINED3D_OK;
2356 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2357 UINT start_register, const float *constants, UINT vector4f_count)
2359 UINT i;
2360 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2362 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2363 device, start_register, constants, vector4f_count);
2365 /* Specifically test start_register > limit to catch MAX_UINT overflows
2366 * when adding start_register + vector4f_count. */
2367 if (!constants
2368 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2369 || start_register > d3d_info->limits.vs_uniform_count)
2370 return WINED3DERR_INVALIDCALL;
2372 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2373 constants, vector4f_count * sizeof(float) * 4);
2374 if (TRACE_ON(d3d))
2376 for (i = 0; i < vector4f_count; ++i)
2377 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2378 constants[i * 4], constants[i * 4 + 1],
2379 constants[i * 4 + 2], constants[i * 4 + 3]);
2382 if (device->recording)
2383 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2384 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2385 else
2386 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2389 return WINED3D_OK;
2392 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2393 UINT start_register, float *constants, UINT vector4f_count)
2395 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2396 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2398 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2399 device, start_register, constants, vector4f_count);
2401 if (!constants || count < 0)
2402 return WINED3DERR_INVALIDCALL;
2404 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2406 return WINED3D_OK;
2409 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2411 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2413 TRACE("device %p, shader %p.\n", device, shader);
2415 if (device->recording)
2416 device->recording->changed.pixelShader = TRUE;
2418 if (shader == prev)
2419 return;
2421 if (shader)
2422 wined3d_shader_incref(shader);
2423 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2424 if (!device->recording)
2425 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2426 if (prev)
2427 wined3d_shader_decref(prev);
2430 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2432 TRACE("device %p.\n", device);
2434 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2437 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2439 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2441 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2444 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2446 TRACE("device %p, idx %u.\n", device, idx);
2448 if (idx >= MAX_CONSTANT_BUFFERS)
2450 WARN("Invalid constant buffer index %u.\n", idx);
2451 return NULL;
2454 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2457 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2458 UINT idx, struct wined3d_shader_resource_view *view)
2460 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2462 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2465 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2466 UINT idx)
2468 TRACE("device %p, idx %u.\n", device, idx);
2470 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2472 WARN("Invalid view index %u.\n", idx);
2473 return NULL;
2476 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2479 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2481 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2483 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2486 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2488 TRACE("device %p, idx %u.\n", device, idx);
2490 if (idx >= MAX_SAMPLER_OBJECTS)
2492 WARN("Invalid sampler index %u.\n", idx);
2493 return NULL;
2496 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2499 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2500 UINT start_register, const BOOL *constants, UINT bool_count)
2502 UINT count = min(bool_count, MAX_CONST_B - start_register);
2503 UINT i;
2505 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2506 device, start_register, constants, bool_count);
2508 if (!constants || start_register >= MAX_CONST_B)
2509 return WINED3DERR_INVALIDCALL;
2511 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2512 for (i = 0; i < count; ++i)
2513 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2515 if (device->recording)
2517 for (i = start_register; i < count + start_register; ++i)
2518 device->recording->changed.pixelShaderConstantsB |= (1 << i);
2520 else
2522 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2525 return WINED3D_OK;
2528 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2529 UINT start_register, BOOL *constants, UINT bool_count)
2531 UINT count = min(bool_count, MAX_CONST_B - start_register);
2533 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2534 device, start_register, constants, bool_count);
2536 if (!constants || start_register >= MAX_CONST_B)
2537 return WINED3DERR_INVALIDCALL;
2539 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2541 return WINED3D_OK;
2544 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2545 UINT start_register, const int *constants, UINT vector4i_count)
2547 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2548 UINT i;
2550 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2551 device, start_register, constants, vector4i_count);
2553 if (!constants || start_register >= MAX_CONST_I)
2554 return WINED3DERR_INVALIDCALL;
2556 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2557 for (i = 0; i < count; ++i)
2558 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2559 constants[i * 4], constants[i * 4 + 1],
2560 constants[i * 4 + 2], constants[i * 4 + 3]);
2562 if (device->recording)
2564 for (i = start_register; i < count + start_register; ++i)
2565 device->recording->changed.pixelShaderConstantsI |= (1 << i);
2567 else
2569 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2572 return WINED3D_OK;
2575 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2576 UINT start_register, int *constants, UINT vector4i_count)
2578 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2580 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2581 device, start_register, constants, vector4i_count);
2583 if (!constants || start_register >= MAX_CONST_I)
2584 return WINED3DERR_INVALIDCALL;
2586 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2588 return WINED3D_OK;
2591 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2592 UINT start_register, const float *constants, UINT vector4f_count)
2594 UINT i;
2595 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2597 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2598 device, start_register, constants, vector4f_count);
2600 /* Specifically test start_register > limit to catch MAX_UINT overflows
2601 * when adding start_register + vector4f_count. */
2602 if (!constants
2603 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2604 || start_register > d3d_info->limits.ps_uniform_count)
2605 return WINED3DERR_INVALIDCALL;
2607 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2608 constants, vector4f_count * sizeof(float) * 4);
2609 if (TRACE_ON(d3d))
2611 for (i = 0; i < vector4f_count; ++i)
2612 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2613 constants[i * 4], constants[i * 4 + 1],
2614 constants[i * 4 + 2], constants[i * 4 + 3]);
2617 if (device->recording)
2618 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2619 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2620 else
2621 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2623 return WINED3D_OK;
2626 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2627 UINT start_register, float *constants, UINT vector4f_count)
2629 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2630 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2632 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2633 device, start_register, constants, vector4f_count);
2635 if (!constants || count < 0)
2636 return WINED3DERR_INVALIDCALL;
2638 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2640 return WINED3D_OK;
2643 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2645 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2647 TRACE("device %p, shader %p.\n", device, shader);
2649 if (device->recording || shader == prev)
2650 return;
2651 if (shader)
2652 wined3d_shader_incref(shader);
2653 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2654 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2655 if (prev)
2656 wined3d_shader_decref(prev);
2659 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2661 TRACE("device %p.\n", device);
2663 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2666 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2668 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2670 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2673 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2675 TRACE("device %p, idx %u.\n", device, idx);
2677 if (idx >= MAX_CONSTANT_BUFFERS)
2679 WARN("Invalid constant buffer index %u.\n", idx);
2680 return NULL;
2683 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2686 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2687 UINT idx, struct wined3d_shader_resource_view *view)
2689 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2691 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2694 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2695 UINT idx)
2697 TRACE("device %p, idx %u.\n", device, idx);
2699 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2701 WARN("Invalid view index %u.\n", idx);
2702 return NULL;
2705 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2708 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2710 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2712 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2715 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2717 TRACE("device %p, idx %u.\n", device, idx);
2719 if (idx >= MAX_SAMPLER_OBJECTS)
2721 WARN("Invalid sampler index %u.\n", idx);
2722 return NULL;
2725 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2728 /* Context activation is done by the caller. */
2729 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2730 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2731 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2732 DWORD DestFVF)
2734 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2735 struct wined3d_viewport vp;
2736 UINT vertex_size;
2737 unsigned int i;
2738 BYTE *dest_ptr;
2739 BOOL doClip;
2740 DWORD numTextures;
2741 HRESULT hr;
2743 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
2745 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2748 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
2750 ERR("Source has no position mask\n");
2751 return WINED3DERR_INVALIDCALL;
2754 if (device->state.render_states[WINED3D_RS_CLIPPING])
2756 static BOOL warned = FALSE;
2758 * The clipping code is not quite correct. Some things need
2759 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2760 * so disable clipping for now.
2761 * (The graphics in Half-Life are broken, and my processvertices
2762 * test crashes with IDirect3DDevice3)
2763 doClip = TRUE;
2765 doClip = FALSE;
2766 if(!warned) {
2767 warned = TRUE;
2768 FIXME("Clipping is broken and disabled for now\n");
2771 else
2772 doClip = FALSE;
2774 vertex_size = get_flexible_vertex_size(DestFVF);
2775 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2777 WARN("Failed to map buffer, hr %#x.\n", hr);
2778 return hr;
2781 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2782 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2783 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2785 TRACE("View mat:\n");
2786 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2787 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2788 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2789 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2791 TRACE("Proj mat:\n");
2792 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2793 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2794 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2795 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2797 TRACE("World mat:\n");
2798 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2799 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2800 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2801 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2803 /* Get the viewport */
2804 wined3d_device_get_viewport(device, &vp);
2805 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2806 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2808 multiply_matrix(&mat,&view_mat,&world_mat);
2809 multiply_matrix(&mat,&proj_mat,&mat);
2811 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2813 for (i = 0; i < dwCount; i+= 1) {
2814 unsigned int tex_index;
2816 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2817 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2818 /* The position first */
2819 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2820 const float *p = (const float *)(element->data.addr + i * element->stride);
2821 float x, y, z, rhw;
2822 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2824 /* Multiplication with world, view and projection matrix. */
2825 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2826 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2827 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2828 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2830 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2832 /* WARNING: The following things are taken from d3d7 and were not yet checked
2833 * against d3d8 or d3d9!
2836 /* Clipping conditions: From msdn
2838 * A vertex is clipped if it does not match the following requirements
2839 * -rhw < x <= rhw
2840 * -rhw < y <= rhw
2841 * 0 < z <= rhw
2842 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2844 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2845 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2849 if( !doClip ||
2850 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2851 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2852 ( rhw > eps ) ) ) {
2854 /* "Normal" viewport transformation (not clipped)
2855 * 1) The values are divided by rhw
2856 * 2) The y axis is negative, so multiply it with -1
2857 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2858 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2859 * 4) Multiply x with Width/2 and add Width/2
2860 * 5) The same for the height
2861 * 6) Add the viewpoint X and Y to the 2D coordinates and
2862 * The minimum Z value to z
2863 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2865 * Well, basically it's simply a linear transformation into viewport
2866 * coordinates
2869 x /= rhw;
2870 y /= rhw;
2871 z /= rhw;
2873 y *= -1;
2875 x *= vp.width / 2;
2876 y *= vp.height / 2;
2877 z *= vp.max_z - vp.min_z;
2879 x += vp.width / 2 + vp.x;
2880 y += vp.height / 2 + vp.y;
2881 z += vp.min_z;
2883 rhw = 1 / rhw;
2884 } else {
2885 /* That vertex got clipped
2886 * Contrary to OpenGL it is not dropped completely, it just
2887 * undergoes a different calculation.
2889 TRACE("Vertex got clipped\n");
2890 x += rhw;
2891 y += rhw;
2893 x /= 2;
2894 y /= 2;
2896 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2897 * outside of the main vertex buffer memory. That needs some more
2898 * investigation...
2902 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2905 ( (float *) dest_ptr)[0] = x;
2906 ( (float *) dest_ptr)[1] = y;
2907 ( (float *) dest_ptr)[2] = z;
2908 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2910 dest_ptr += 3 * sizeof(float);
2912 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2913 dest_ptr += sizeof(float);
2916 if (DestFVF & WINED3DFVF_PSIZE)
2917 dest_ptr += sizeof(DWORD);
2919 if (DestFVF & WINED3DFVF_NORMAL)
2921 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2922 const float *normal = (const float *)(element->data.addr + i * element->stride);
2923 /* AFAIK this should go into the lighting information */
2924 FIXME("Didn't expect the destination to have a normal\n");
2925 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2928 if (DestFVF & WINED3DFVF_DIFFUSE)
2930 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2931 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2932 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
2934 static BOOL warned = FALSE;
2936 if(!warned) {
2937 ERR("No diffuse color in source, but destination has one\n");
2938 warned = TRUE;
2941 *( (DWORD *) dest_ptr) = 0xffffffff;
2942 dest_ptr += sizeof(DWORD);
2944 else
2946 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2950 if (DestFVF & WINED3DFVF_SPECULAR)
2952 /* What's the color value in the feedback buffer? */
2953 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2954 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2955 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
2957 static BOOL warned = FALSE;
2959 if(!warned) {
2960 ERR("No specular color in source, but destination has one\n");
2961 warned = TRUE;
2964 *(DWORD *)dest_ptr = 0xff000000;
2965 dest_ptr += sizeof(DWORD);
2967 else
2969 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2973 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2975 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2976 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2977 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2979 ERR("No source texture, but destination requests one\n");
2980 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2982 else
2984 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2989 wined3d_buffer_unmap(dest);
2991 return WINED3D_OK;
2993 #undef copy_and_next
2995 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
2996 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
2997 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
2999 struct wined3d_state *state = &device->state;
3000 struct wined3d_stream_info stream_info;
3001 const struct wined3d_gl_info *gl_info;
3002 struct wined3d_context *context;
3003 struct wined3d_shader *vs;
3004 unsigned int i;
3005 HRESULT hr;
3006 WORD map;
3008 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3009 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3010 device, src_start_idx, dst_idx, vertex_count,
3011 dst_buffer, declaration, flags, dst_fvf);
3013 if (declaration)
3014 FIXME("Output vertex declaration not implemented yet.\n");
3016 /* Need any context to write to the vbo. */
3017 context = context_acquire(device, NULL);
3018 gl_info = context->gl_info;
3020 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3021 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3022 context_stream_info_from_declaration(context, state, &stream_info);
3023 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3025 /* We can't convert FROM a VBO, and vertex buffers used to source into
3026 * process_vertices() are unlikely to ever be used for drawing. Release
3027 * VBOs in those buffers and fix up the stream_info structure.
3029 * Also apply the start index. */
3030 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3032 struct wined3d_stream_info_element *e;
3033 struct wined3d_buffer *buffer;
3035 if (!(map & 1))
3036 continue;
3038 e = &stream_info.elements[i];
3039 buffer = state->streams[e->stream_idx].buffer;
3040 e->data.buffer_object = 0;
3041 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3042 if (buffer->buffer_object)
3044 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3045 buffer->buffer_object = 0;
3047 if (e->data.addr)
3048 e->data.addr += e->stride * src_start_idx;
3051 hr = process_vertices_strided(device, dst_idx, vertex_count,
3052 &stream_info, dst_buffer, flags, dst_fvf);
3054 context_release(context);
3056 return hr;
3059 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3060 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3062 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3063 DWORD old_value;
3065 TRACE("device %p, stage %u, state %s, value %#x.\n",
3066 device, stage, debug_d3dtexturestate(state), value);
3068 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3070 WARN("Invalid state %#x passed.\n", state);
3071 return;
3074 if (stage >= d3d_info->limits.ffp_blend_stages)
3076 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3077 stage, d3d_info->limits.ffp_blend_stages - 1);
3078 return;
3081 old_value = device->update_state->texture_states[stage][state];
3082 device->update_state->texture_states[stage][state] = value;
3084 if (device->recording)
3086 TRACE("Recording... not performing anything.\n");
3087 device->recording->changed.textureState[stage] |= 1 << state;
3088 return;
3091 /* Checked after the assignments to allow proper stateblock recording. */
3092 if (old_value == value)
3094 TRACE("Application is setting the old value over, nothing to do.\n");
3095 return;
3098 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3101 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3102 UINT stage, enum wined3d_texture_stage_state state)
3104 TRACE("device %p, stage %u, state %s.\n",
3105 device, stage, debug_d3dtexturestate(state));
3107 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3109 WARN("Invalid state %#x passed.\n", state);
3110 return 0;
3113 return device->state.texture_states[stage][state];
3116 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3117 UINT stage, struct wined3d_texture *texture)
3119 struct wined3d_texture *prev;
3121 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3123 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3124 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3126 /* Windows accepts overflowing this array... we do not. */
3127 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3129 WARN("Ignoring invalid stage %u.\n", stage);
3130 return WINED3D_OK;
3133 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3135 WARN("Rejecting attempt to set scratch texture.\n");
3136 return WINED3DERR_INVALIDCALL;
3139 if (device->recording)
3140 device->recording->changed.textures |= 1 << stage;
3142 prev = device->update_state->textures[stage];
3143 TRACE("Previous texture %p.\n", prev);
3145 if (texture == prev)
3147 TRACE("App is setting the same texture again, nothing to do.\n");
3148 return WINED3D_OK;
3151 TRACE("Setting new texture to %p.\n", texture);
3152 device->update_state->textures[stage] = texture;
3154 if (texture)
3155 wined3d_texture_incref(texture);
3156 if (!device->recording)
3157 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3158 if (prev)
3159 wined3d_texture_decref(prev);
3161 return WINED3D_OK;
3164 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3166 TRACE("device %p, stage %u.\n", device, stage);
3168 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3169 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3171 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3173 WARN("Ignoring invalid stage %u.\n", stage);
3174 return NULL; /* Windows accepts overflowing this array ... we do not. */
3177 return device->state.textures[stage];
3180 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3181 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3183 struct wined3d_swapchain *swapchain;
3185 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3186 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3188 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3189 return WINED3DERR_INVALIDCALL;
3191 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3192 return WINED3DERR_INVALIDCALL;
3193 return WINED3D_OK;
3196 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3198 TRACE("device %p, caps %p.\n", device, caps);
3200 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3201 device->create_parms.device_type, caps);
3204 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3205 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3207 struct wined3d_swapchain *swapchain;
3209 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3210 device, swapchain_idx, mode, rotation);
3212 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3213 return WINED3DERR_INVALIDCALL;
3215 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3218 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3220 struct wined3d_stateblock *stateblock;
3221 HRESULT hr;
3223 TRACE("device %p.\n", device);
3225 if (device->recording)
3226 return WINED3DERR_INVALIDCALL;
3228 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3229 if (FAILED(hr))
3230 return hr;
3232 device->recording = stateblock;
3233 device->update_state = &stateblock->state;
3235 TRACE("Recording stateblock %p.\n", stateblock);
3237 return WINED3D_OK;
3240 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3241 struct wined3d_stateblock **stateblock)
3243 struct wined3d_stateblock *object = device->recording;
3245 TRACE("device %p, stateblock %p.\n", device, stateblock);
3247 if (!device->recording)
3249 WARN("Not recording.\n");
3250 *stateblock = NULL;
3251 return WINED3DERR_INVALIDCALL;
3254 stateblock_init_contained_states(object);
3256 *stateblock = object;
3257 device->recording = NULL;
3258 device->update_state = &device->state;
3260 TRACE("Returning stateblock %p.\n", *stateblock);
3262 return WINED3D_OK;
3265 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3267 /* At the moment we have no need for any functionality at the beginning
3268 * of a scene. */
3269 TRACE("device %p.\n", device);
3271 if (device->inScene)
3273 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3274 return WINED3DERR_INVALIDCALL;
3276 device->inScene = TRUE;
3277 return WINED3D_OK;
3280 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3282 struct wined3d_context *context;
3284 TRACE("device %p.\n", device);
3286 if (!device->inScene)
3288 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3289 return WINED3DERR_INVALIDCALL;
3292 context = context_acquire(device, NULL);
3293 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3294 context->gl_info->gl_ops.gl.p_glFlush();
3295 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3296 * fails. */
3297 context_release(context);
3299 device->inScene = FALSE;
3300 return WINED3D_OK;
3303 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3304 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3306 UINT i;
3308 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3309 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3310 dst_window_override, dirty_region, flags);
3312 for (i = 0; i < device->swapchain_count; ++i)
3314 wined3d_swapchain_present(device->swapchains[i], src_rect,
3315 dst_rect, dst_window_override, dirty_region, flags);
3318 return WINED3D_OK;
3321 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3322 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3324 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3325 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3327 if (!rect_count && rects)
3329 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3330 return WINED3D_OK;
3333 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3335 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3336 if (!ds)
3338 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3339 /* TODO: What about depth stencil buffers without stencil bits? */
3340 return WINED3DERR_INVALIDCALL;
3342 else if (flags & WINED3DCLEAR_TARGET)
3344 if (ds->width < device->fb.render_targets[0]->width
3345 || ds->height < device->fb.render_targets[0]->height)
3347 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3348 return WINED3D_OK;
3353 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3355 return WINED3D_OK;
3358 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3359 struct wined3d_query *predicate, BOOL value)
3361 struct wined3d_query *prev;
3363 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3365 prev = device->update_state->predicate;
3366 if (predicate)
3368 FIXME("Predicated rendering not implemented.\n");
3369 wined3d_query_incref(predicate);
3371 device->update_state->predicate = predicate;
3372 device->update_state->predicate_value = value;
3373 if (!device->recording)
3374 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3375 if (prev)
3376 wined3d_query_decref(prev);
3379 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3381 TRACE("device %p, value %p.\n", device, value);
3383 *value = device->state.predicate_value;
3384 return device->state.predicate;
3387 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3388 enum wined3d_primitive_type primitive_type)
3390 GLenum gl_primitive_type, prev;
3392 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3394 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3395 prev = device->update_state->gl_primitive_type;
3396 device->update_state->gl_primitive_type = gl_primitive_type;
3397 if (device->recording)
3398 device->recording->changed.primitive_type = TRUE;
3399 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3400 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
3403 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3404 enum wined3d_primitive_type *primitive_type)
3406 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3408 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3410 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3413 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3415 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3417 if (!device->state.vertex_declaration)
3419 WARN("Called without a valid vertex declaration set.\n");
3420 return WINED3DERR_INVALIDCALL;
3423 if (device->state.load_base_vertex_index)
3425 device->state.load_base_vertex_index = 0;
3426 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3429 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3431 return WINED3D_OK;
3434 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3435 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3437 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3438 device, start_vertex, vertex_count, start_instance, instance_count);
3440 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3443 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3445 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3447 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3449 if (!device->state.index_buffer)
3451 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3452 * without an index buffer set. (The first time at least...)
3453 * D3D8 simply dies, but I doubt it can do much harm to return
3454 * D3DERR_INVALIDCALL there as well. */
3455 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3456 return WINED3DERR_INVALIDCALL;
3459 if (!device->state.vertex_declaration)
3461 WARN("Called without a valid vertex declaration set.\n");
3462 return WINED3DERR_INVALIDCALL;
3465 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3466 device->state.load_base_vertex_index != device->state.base_vertex_index)
3468 device->state.load_base_vertex_index = device->state.base_vertex_index;
3469 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3472 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3474 return WINED3D_OK;
3477 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3478 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3480 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3481 device, start_idx, index_count, start_instance, instance_count);
3483 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3486 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3487 static HRESULT device_update_volume(struct wined3d_device *device,
3488 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3490 struct wined3d_const_bo_address data;
3491 struct wined3d_map_desc src;
3492 HRESULT hr;
3493 struct wined3d_context *context;
3495 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3496 device, src_volume, dst_volume);
3498 if (src_volume->resource.format != dst_volume->resource.format)
3500 FIXME("Source and destination formats do not match.\n");
3501 return WINED3DERR_INVALIDCALL;
3503 if (src_volume->resource.width != dst_volume->resource.width
3504 || src_volume->resource.height != dst_volume->resource.height
3505 || src_volume->resource.depth != dst_volume->resource.depth)
3507 FIXME("Source and destination sizes do not match.\n");
3508 return WINED3DERR_INVALIDCALL;
3511 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3512 return hr;
3514 context = context_acquire(device, NULL);
3516 /* Only a prepare, since we're uploading the entire volume. */
3517 wined3d_texture_prepare_texture(dst_volume->container, context, FALSE);
3518 wined3d_texture_bind_and_dirtify(dst_volume->container, context, FALSE);
3520 data.buffer_object = 0;
3521 data.addr = src.data;
3522 wined3d_volume_upload_data(dst_volume, context, &data);
3523 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3525 context_release(context);
3527 hr = wined3d_volume_unmap(src_volume);
3529 return hr;
3532 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3533 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3535 enum wined3d_resource_type type;
3536 unsigned int level_count, i;
3537 HRESULT hr;
3538 struct wined3d_context *context;
3540 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3542 /* Verify that the source and destination textures are non-NULL. */
3543 if (!src_texture || !dst_texture)
3545 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3546 return WINED3DERR_INVALIDCALL;
3549 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3551 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3552 return WINED3DERR_INVALIDCALL;
3554 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3556 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3557 return WINED3DERR_INVALIDCALL;
3560 /* Verify that the source and destination textures are the same type. */
3561 type = src_texture->resource.type;
3562 if (dst_texture->resource.type != type)
3564 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3565 return WINED3DERR_INVALIDCALL;
3568 /* Check that both textures have the identical numbers of levels. */
3569 level_count = wined3d_texture_get_level_count(src_texture);
3570 if (wined3d_texture_get_level_count(dst_texture) != level_count)
3572 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
3573 return WINED3DERR_INVALIDCALL;
3576 /* Make sure that the destination texture is loaded. */
3577 context = context_acquire(device, NULL);
3578 wined3d_texture_load(dst_texture, context, FALSE);
3579 context_release(context);
3581 /* Update every surface level of the texture. */
3582 switch (type)
3584 case WINED3D_RTYPE_TEXTURE:
3586 struct wined3d_surface *src_surface;
3587 struct wined3d_surface *dst_surface;
3589 for (i = 0; i < level_count; ++i)
3591 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3592 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3593 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3594 if (FAILED(hr))
3596 WARN("Failed to update surface, hr %#x.\n", hr);
3597 return hr;
3600 break;
3603 case WINED3D_RTYPE_CUBE_TEXTURE:
3605 struct wined3d_surface *src_surface;
3606 struct wined3d_surface *dst_surface;
3608 for (i = 0; i < level_count * 6; ++i)
3610 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3611 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3612 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3613 if (FAILED(hr))
3615 WARN("Failed to update surface, hr %#x.\n", hr);
3616 return hr;
3619 break;
3622 case WINED3D_RTYPE_VOLUME_TEXTURE:
3624 for (i = 0; i < level_count; ++i)
3626 hr = device_update_volume(device,
3627 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
3628 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3629 if (FAILED(hr))
3631 WARN("Failed to update volume, hr %#x.\n", hr);
3632 return hr;
3635 break;
3638 default:
3639 FIXME("Unsupported texture type %#x.\n", type);
3640 return WINED3DERR_INVALIDCALL;
3643 return WINED3D_OK;
3646 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3647 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3649 struct wined3d_swapchain *swapchain;
3651 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3653 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3654 return WINED3DERR_INVALIDCALL;
3656 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3659 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3661 const struct wined3d_state *state = &device->state;
3662 struct wined3d_texture *texture;
3663 DWORD i;
3665 TRACE("device %p, num_passes %p.\n", device, num_passes);
3667 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3669 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3671 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3672 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3674 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3676 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3677 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3680 texture = state->textures[i];
3681 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3683 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3685 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3686 return E_FAIL;
3688 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3690 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3691 return E_FAIL;
3693 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3694 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3696 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3697 return E_FAIL;
3701 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3702 || state->render_states[WINED3D_RS_STENCILENABLE])
3704 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3705 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3707 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3709 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3710 return WINED3DERR_CONFLICTINGRENDERSTATE;
3714 /* return a sensible default */
3715 *num_passes = 1;
3717 TRACE("returning D3D_OK\n");
3718 return WINED3D_OK;
3721 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3723 static BOOL warned;
3725 TRACE("device %p, software %#x.\n", device, software);
3727 if (!warned)
3729 FIXME("device %p, software %#x stub!\n", device, software);
3730 warned = TRUE;
3733 device->softwareVertexProcessing = software;
3736 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3738 static BOOL warned;
3740 TRACE("device %p.\n", device);
3742 if (!warned)
3744 TRACE("device %p stub!\n", device);
3745 warned = TRUE;
3748 return device->softwareVertexProcessing;
3751 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3752 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3754 struct wined3d_swapchain *swapchain;
3756 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3757 device, swapchain_idx, raster_status);
3759 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3760 return WINED3DERR_INVALIDCALL;
3762 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3765 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3767 static BOOL warned;
3769 TRACE("device %p, segments %.8e.\n", device, segments);
3771 if (segments != 0.0f)
3773 if (!warned)
3775 FIXME("device %p, segments %.8e stub!\n", device, segments);
3776 warned = TRUE;
3780 return WINED3D_OK;
3783 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3785 static BOOL warned;
3787 TRACE("device %p.\n", device);
3789 if (!warned)
3791 FIXME("device %p stub!\n", device);
3792 warned = TRUE;
3795 return 0.0f;
3798 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3799 struct wined3d_surface *src_surface, const RECT *src_rect,
3800 struct wined3d_surface *dst_surface, const POINT *dst_point)
3802 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3803 device, src_surface, wine_dbgstr_rect(src_rect),
3804 dst_surface, wine_dbgstr_point(dst_point));
3806 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3808 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3809 src_surface, dst_surface);
3810 return WINED3DERR_INVALIDCALL;
3813 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3816 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3817 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3819 struct wined3d_surface *dst_surface, *src_surface;
3820 struct wined3d_texture *dst_texture, *src_texture;
3821 unsigned int i, count;
3822 HRESULT hr;
3824 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3826 if (src_resource == dst_resource)
3828 WARN("Source and destination are the same resource.\n");
3829 return;
3832 if (src_resource->type != dst_resource->type)
3834 WARN("Resource types (%s / %s) don't match.\n",
3835 debug_d3dresourcetype(dst_resource->type),
3836 debug_d3dresourcetype(src_resource->type));
3837 return;
3840 if (src_resource->width != dst_resource->width
3841 || src_resource->height != dst_resource->height
3842 || src_resource->depth != dst_resource->depth)
3844 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3845 dst_resource->width, dst_resource->height, dst_resource->depth,
3846 src_resource->width, src_resource->height, src_resource->depth);
3847 return;
3850 if (src_resource->format->id != dst_resource->format->id)
3852 WARN("Resource formats (%s / %s) don't match.\n",
3853 debug_d3dformat(dst_resource->format->id),
3854 debug_d3dformat(src_resource->format->id));
3855 return;
3858 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3860 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3861 return;
3864 dst_texture = wined3d_texture_from_resource(dst_resource);
3865 src_texture = wined3d_texture_from_resource(src_resource);
3867 if (src_texture->layer_count != dst_texture->layer_count
3868 || src_texture->level_count != dst_texture->level_count)
3870 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3871 dst_texture->layer_count, dst_texture->level_count,
3872 src_texture->layer_count, src_texture->level_count);
3873 return;
3876 count = dst_texture->layer_count * dst_texture->level_count;
3877 for (i = 0; i < count; ++i)
3879 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3880 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3882 if (FAILED(hr = wined3d_surface_blt(dst_surface, NULL, src_surface, NULL, 0, NULL, WINED3D_TEXF_POINT)))
3883 ERR("Failed to blit, subresource %u, hr %#x.\n", i, hr);
3887 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
3888 struct wined3d_rendertarget_view *view, const RECT *rect, const struct wined3d_color *color)
3890 struct wined3d_resource *resource;
3891 RECT r;
3893 TRACE("device %p, view %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
3894 device, view, wine_dbgstr_rect(rect), color->r, color->g, color->b, color->a);
3896 resource = view->resource;
3897 if (resource->type != WINED3D_RTYPE_TEXTURE && resource->type != WINED3D_RTYPE_CUBE_TEXTURE)
3899 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
3900 return WINED3DERR_INVALIDCALL;
3903 if (view->depth > 1)
3905 FIXME("Layered clears not implemented.\n");
3906 return WINED3DERR_INVALIDCALL;
3909 if (!rect)
3911 SetRect(&r, 0, 0, view->width, view->height);
3912 rect = &r;
3915 resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource), view->sub_resource_idx);
3917 return surface_color_fill(surface_from_resource(resource), rect, color);
3920 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
3921 unsigned int view_idx)
3923 TRACE("device %p, view_idx %u.\n", device, view_idx);
3925 if (view_idx >= device->adapter->gl_info.limits.buffers)
3927 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3928 return NULL;
3931 return device->fb.render_targets[view_idx];
3934 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
3936 TRACE("device %p.\n", device);
3938 return device->fb.depth_stencil;
3941 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
3942 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
3944 struct wined3d_rendertarget_view *prev;
3946 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
3947 device, view_idx, view, set_viewport);
3949 if (view_idx >= device->adapter->gl_info.limits.buffers)
3951 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3952 return WINED3DERR_INVALIDCALL;
3955 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
3957 WARN("View resource %p doesn't have render target usage.\n", view->resource);
3958 return WINED3DERR_INVALIDCALL;
3961 /* Set the viewport and scissor rectangles, if requested. Tests show that
3962 * stateblock recording is ignored, the change goes directly into the
3963 * primary stateblock. */
3964 if (!view_idx && set_viewport)
3966 struct wined3d_state *state = &device->state;
3968 state->viewport.x = 0;
3969 state->viewport.y = 0;
3970 state->viewport.width = view->width;
3971 state->viewport.height = view->height;
3972 state->viewport.min_z = 0.0f;
3973 state->viewport.max_z = 1.0f;
3974 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
3976 state->scissor_rect.top = 0;
3977 state->scissor_rect.left = 0;
3978 state->scissor_rect.right = view->width;
3979 state->scissor_rect.bottom = view->height;
3980 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
3984 prev = device->fb.render_targets[view_idx];
3985 if (view == prev)
3986 return WINED3D_OK;
3988 if (view)
3989 wined3d_rendertarget_view_incref(view);
3990 device->fb.render_targets[view_idx] = view;
3991 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
3992 /* Release after the assignment, to prevent device_resource_released()
3993 * from seeing the surface as still in use. */
3994 if (prev)
3995 wined3d_rendertarget_view_decref(prev);
3997 return WINED3D_OK;
4000 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4002 struct wined3d_rendertarget_view *prev;
4004 TRACE("device %p, view %p.\n", device, view);
4006 prev = device->fb.depth_stencil;
4007 if (prev == view)
4009 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4010 return;
4013 if ((device->fb.depth_stencil = view))
4014 wined3d_rendertarget_view_incref(view);
4015 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4016 if (prev)
4017 wined3d_rendertarget_view_decref(prev);
4020 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4021 struct wined3d_surface *cursor_image)
4023 struct wined3d_sub_resource_data data;
4024 struct wined3d_resource_desc desc;
4025 struct wined3d_map_desc map_desc;
4026 struct wined3d_texture *texture;
4027 HRESULT hr;
4029 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4031 ERR("Failed to map source surface.\n");
4032 return NULL;
4035 data.data = map_desc.data;
4036 data.row_pitch = map_desc.row_pitch;
4037 data.slice_pitch = map_desc.slice_pitch;
4039 desc.resource_type = WINED3D_RTYPE_TEXTURE;
4040 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4041 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4042 desc.multisample_quality = 0;
4043 desc.usage = WINED3DUSAGE_DYNAMIC;
4044 desc.pool = WINED3D_POOL_DEFAULT;
4045 desc.width = cursor_image->resource.width;
4046 desc.height = cursor_image->resource.height;
4047 desc.depth = 1;
4048 desc.size = 0;
4050 hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
4051 &data, NULL, &wined3d_null_parent_ops, &texture);
4052 wined3d_surface_unmap(cursor_image);
4053 if (FAILED(hr))
4055 ERR("Failed to create cursor texture.\n");
4056 return NULL;
4059 return texture;
4062 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4063 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4065 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4066 device, x_hotspot, y_hotspot, cursor_image);
4068 if (device->cursor_texture)
4070 wined3d_texture_decref(device->cursor_texture);
4071 device->cursor_texture = NULL;
4074 if (cursor_image)
4076 struct wined3d_display_mode mode;
4077 struct wined3d_map_desc map_desc;
4078 HRESULT hr;
4080 /* MSDN: Cursor must be A8R8G8B8 */
4081 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4083 WARN("surface %p has an invalid format.\n", cursor_image);
4084 return WINED3DERR_INVALIDCALL;
4087 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4089 ERR("Failed to get display mode, hr %#x.\n", hr);
4090 return WINED3DERR_INVALIDCALL;
4093 /* MSDN: Cursor must be smaller than the display mode */
4094 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4096 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4097 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4098 mode.width, mode.height);
4099 return WINED3DERR_INVALIDCALL;
4102 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4104 /* Do not store the surface's pointer because the application may
4105 * release it after setting the cursor image. Windows doesn't
4106 * addref the set surface, so we can't do this either without
4107 * creating circular refcount dependencies. */
4108 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
4110 ERR("Failed to create cursor texture.\n");
4111 return WINED3DERR_INVALIDCALL;
4114 device->cursorWidth = cursor_image->resource.width;
4115 device->cursorHeight = cursor_image->resource.height;
4117 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4119 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4120 ICONINFO cursorInfo;
4121 DWORD *maskBits;
4122 HCURSOR cursor;
4124 /* 32-bit user32 cursors ignore the alpha channel if it's all
4125 * zeroes, and use the mask instead. Fill the mask with all ones
4126 * to ensure we still get a fully transparent cursor. */
4127 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4128 memset(maskBits, 0xff, mask_size);
4129 wined3d_surface_map(cursor_image, &map_desc, NULL,
4130 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4131 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4133 cursorInfo.fIcon = FALSE;
4134 cursorInfo.xHotspot = x_hotspot;
4135 cursorInfo.yHotspot = y_hotspot;
4136 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4137 1, 1, maskBits);
4138 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4139 1, 32, map_desc.data);
4140 wined3d_surface_unmap(cursor_image);
4141 /* Create our cursor and clean up. */
4142 cursor = CreateIconIndirect(&cursorInfo);
4143 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4144 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4145 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4146 device->hardwareCursor = cursor;
4147 if (device->bCursorVisible) SetCursor( cursor );
4148 HeapFree(GetProcessHeap(), 0, maskBits);
4152 device->xHotSpot = x_hotspot;
4153 device->yHotSpot = y_hotspot;
4154 return WINED3D_OK;
4157 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4158 int x_screen_space, int y_screen_space, DWORD flags)
4160 TRACE("device %p, x %d, y %d, flags %#x.\n",
4161 device, x_screen_space, y_screen_space, flags);
4163 device->xScreenSpace = x_screen_space;
4164 device->yScreenSpace = y_screen_space;
4166 if (device->hardwareCursor)
4168 POINT pt;
4170 GetCursorPos( &pt );
4171 if (x_screen_space == pt.x && y_screen_space == pt.y)
4172 return;
4173 SetCursorPos( x_screen_space, y_screen_space );
4175 /* Switch to the software cursor if position diverges from the hardware one. */
4176 GetCursorPos( &pt );
4177 if (x_screen_space != pt.x || y_screen_space != pt.y)
4179 if (device->bCursorVisible) SetCursor( NULL );
4180 DestroyCursor( device->hardwareCursor );
4181 device->hardwareCursor = 0;
4186 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4188 BOOL oldVisible = device->bCursorVisible;
4190 TRACE("device %p, show %#x.\n", device, show);
4193 * When ShowCursor is first called it should make the cursor appear at the OS's last
4194 * known cursor position.
4196 if (show && !oldVisible)
4198 POINT pt;
4199 GetCursorPos(&pt);
4200 device->xScreenSpace = pt.x;
4201 device->yScreenSpace = pt.y;
4204 if (device->hardwareCursor)
4206 device->bCursorVisible = show;
4207 if (show)
4208 SetCursor(device->hardwareCursor);
4209 else
4210 SetCursor(NULL);
4212 else if (device->cursor_texture)
4214 device->bCursorVisible = show;
4217 return oldVisible;
4220 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4222 struct wined3d_resource *resource, *cursor;
4224 TRACE("device %p.\n", device);
4226 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4228 TRACE("Checking resource %p for eviction.\n", resource);
4230 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4232 TRACE("Evicting %p.\n", resource);
4233 resource->resource_ops->resource_unload(resource);
4237 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4238 device_invalidate_state(device, STATE_STREAMSRC);
4241 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4243 struct wined3d_resource *resource, *cursor;
4244 const struct wined3d_gl_info *gl_info;
4245 struct wined3d_context *context;
4246 struct wined3d_shader *shader;
4248 context = context_acquire(device, NULL);
4249 gl_info = context->gl_info;
4251 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4253 TRACE("Unloading resource %p.\n", resource);
4255 resource->resource_ops->resource_unload(resource);
4258 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4260 device->shader_backend->shader_destroy(shader);
4263 if (device->depth_blt_texture)
4265 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4266 device->depth_blt_texture = 0;
4269 device->blitter->free_private(device);
4270 device->shader_backend->shader_free_private(device);
4271 destroy_dummy_textures(device, gl_info);
4273 context_release(context);
4275 while (device->context_count)
4277 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4280 HeapFree(GetProcessHeap(), 0, swapchain->context);
4281 swapchain->context = NULL;
4284 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4286 struct wined3d_context *context;
4287 struct wined3d_surface *target;
4288 HRESULT hr;
4290 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4291 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4293 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4294 return hr;
4297 if (FAILED(hr = device->blitter->alloc_private(device)))
4299 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4300 device->shader_backend->shader_free_private(device);
4301 return hr;
4304 /* Recreate the primary swapchain's context */
4305 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4306 if (!swapchain->context)
4308 ERR("Failed to allocate memory for swapchain context array.\n");
4309 device->blitter->free_private(device);
4310 device->shader_backend->shader_free_private(device);
4311 return E_OUTOFMEMORY;
4314 target = swapchain->back_buffers
4315 ? surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0))
4316 : surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
4317 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4319 WARN("Failed to create context.\n");
4320 device->blitter->free_private(device);
4321 device->shader_backend->shader_free_private(device);
4322 HeapFree(GetProcessHeap(), 0, swapchain->context);
4323 return E_FAIL;
4326 swapchain->context[0] = context;
4327 swapchain->num_contexts = 1;
4328 create_dummy_textures(device, context);
4329 context_release(context);
4331 return WINED3D_OK;
4334 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4335 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4336 wined3d_device_reset_cb callback, BOOL reset_state)
4338 enum wined3d_format_id backbuffer_format = swapchain_desc->backbuffer_format;
4339 struct wined3d_resource *resource, *cursor;
4340 struct wined3d_swapchain *swapchain;
4341 struct wined3d_display_mode m;
4342 BOOL DisplayModeChanged;
4343 BOOL update_desc = FALSE;
4344 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4345 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4346 HRESULT hr = WINED3D_OK;
4347 unsigned int i;
4349 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4351 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4353 ERR("Failed to get the first implicit swapchain.\n");
4354 return WINED3DERR_INVALIDCALL;
4356 DisplayModeChanged = swapchain->reapply_mode;
4358 if (reset_state)
4360 if (device->logo_texture)
4362 wined3d_texture_decref(device->logo_texture);
4363 device->logo_texture = NULL;
4365 if (device->cursor_texture)
4367 wined3d_texture_decref(device->cursor_texture);
4368 device->cursor_texture = NULL;
4370 state_unbind_resources(&device->state);
4373 if (device->fb.render_targets)
4375 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4377 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4380 wined3d_device_set_depth_stencil_view(device, NULL);
4382 if (device->onscreen_depth_stencil)
4384 wined3d_surface_decref(device->onscreen_depth_stencil);
4385 device->onscreen_depth_stencil = NULL;
4388 if (reset_state)
4390 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4392 TRACE("Enumerating resource %p.\n", resource);
4393 if (FAILED(hr = callback(resource)))
4394 return hr;
4398 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4399 * on an existing gl context, so there's no real need for recreation.
4401 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4403 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4405 TRACE("New params:\n");
4406 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4407 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4408 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4409 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4410 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4411 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4412 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4413 TRACE("device_window %p\n", swapchain_desc->device_window);
4414 TRACE("windowed %#x\n", swapchain_desc->windowed);
4415 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4416 if (swapchain_desc->enable_auto_depth_stencil)
4417 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4418 TRACE("flags %#x\n", swapchain_desc->flags);
4419 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4420 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4421 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4423 /* No special treatment of these parameters. Just store them */
4424 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4425 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4426 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4427 swapchain->desc.flags = swapchain_desc->flags;
4428 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4429 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4430 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4432 /* What to do about these? */
4433 if (swapchain_desc->backbuffer_count
4434 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4435 FIXME("Cannot change the back buffer count yet.\n");
4437 if (swapchain_desc->device_window
4438 && swapchain_desc->device_window != swapchain->desc.device_window)
4440 TRACE("Changing the device window from %p to %p.\n",
4441 swapchain->desc.device_window, swapchain_desc->device_window);
4442 swapchain->desc.device_window = swapchain_desc->device_window;
4443 swapchain->device_window = swapchain_desc->device_window;
4444 wined3d_swapchain_set_window(swapchain, NULL);
4447 if (mode)
4449 DisplayModeChanged = TRUE;
4450 m = *mode;
4452 else if (swapchain_desc->windowed)
4454 m = swapchain->original_mode;
4456 else
4458 m.width = swapchain_desc->backbuffer_width;
4459 m.height = swapchain_desc->backbuffer_height;
4460 m.refresh_rate = swapchain_desc->refresh_rate;
4461 m.format_id = swapchain_desc->backbuffer_format;
4462 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4465 if (!backbuffer_width || !backbuffer_height)
4467 /* The application is requesting that either the swapchain width or
4468 * height be set to the corresponding dimension in the window's
4469 * client rect. */
4471 RECT client_rect;
4473 if (!swapchain_desc->windowed)
4474 return WINED3DERR_INVALIDCALL;
4476 if (!GetClientRect(swapchain->device_window, &client_rect))
4478 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
4479 return WINED3DERR_INVALIDCALL;
4482 if (!backbuffer_width)
4483 backbuffer_width = client_rect.right;
4485 if (!backbuffer_height)
4486 backbuffer_height = client_rect.bottom;
4489 if (backbuffer_width != swapchain->desc.backbuffer_width
4490 || backbuffer_height != swapchain->desc.backbuffer_height)
4492 if (!swapchain_desc->windowed)
4493 DisplayModeChanged = TRUE;
4495 swapchain->desc.backbuffer_width = backbuffer_width;
4496 swapchain->desc.backbuffer_height = backbuffer_height;
4497 update_desc = TRUE;
4500 if (backbuffer_format == WINED3DFMT_UNKNOWN)
4502 if (!swapchain_desc->windowed)
4503 return WINED3DERR_INVALIDCALL;
4504 backbuffer_format = swapchain->original_mode.format_id;
4507 if (backbuffer_format != swapchain->desc.backbuffer_format)
4509 swapchain->desc.backbuffer_format = backbuffer_format;
4510 update_desc = TRUE;
4513 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
4514 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
4516 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
4517 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
4518 update_desc = TRUE;
4521 if (update_desc)
4523 UINT i;
4525 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
4526 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4527 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4528 return hr;
4530 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4532 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
4533 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4534 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4535 return hr;
4539 if (device->auto_depth_stencil_view)
4541 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4542 device->auto_depth_stencil_view = NULL;
4544 if (swapchain->desc.enable_auto_depth_stencil)
4546 struct wined3d_resource_desc surface_desc;
4547 struct wined3d_surface *surface;
4549 TRACE("Creating the depth stencil buffer\n");
4551 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
4552 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
4553 surface_desc.multisample_type = swapchain->desc.multisample_type;
4554 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
4555 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4556 surface_desc.pool = WINED3D_POOL_DEFAULT;
4557 surface_desc.width = swapchain->desc.backbuffer_width;
4558 surface_desc.height = swapchain->desc.backbuffer_height;
4559 surface_desc.depth = 1;
4560 surface_desc.size = 0;
4562 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4563 device->device_parent, &surface_desc, &surface)))
4565 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4566 return WINED3DERR_INVALIDCALL;
4569 hr = wined3d_rendertarget_view_create_from_surface(surface,
4570 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4571 wined3d_surface_decref(surface);
4572 if (FAILED(hr))
4574 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4575 return hr;
4578 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4581 if (device->back_buffer_view)
4583 wined3d_rendertarget_view_decref(device->back_buffer_view);
4584 device->back_buffer_view = NULL;
4586 if (swapchain->desc.backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
4587 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
4588 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4590 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4591 return hr;
4594 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4595 || DisplayModeChanged)
4597 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4599 WARN("Failed to set display mode, hr %#x.\n", hr);
4600 return WINED3DERR_INVALIDCALL;
4603 if (!swapchain_desc->windowed)
4605 if (swapchain->desc.windowed)
4607 HWND focus_window = device->create_parms.focus_window;
4608 if (!focus_window)
4609 focus_window = swapchain_desc->device_window;
4610 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4612 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4613 return hr;
4616 /* switch from windowed to fs */
4617 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4618 swapchain_desc->backbuffer_width,
4619 swapchain_desc->backbuffer_height);
4621 else
4623 /* Fullscreen -> fullscreen mode change */
4624 MoveWindow(swapchain->device_window, 0, 0,
4625 swapchain_desc->backbuffer_width,
4626 swapchain_desc->backbuffer_height,
4627 TRUE);
4629 swapchain->d3d_mode = m;
4631 else if (!swapchain->desc.windowed)
4633 /* Fullscreen -> windowed switch */
4634 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4635 wined3d_device_release_focus_window(device);
4637 swapchain->desc.windowed = swapchain_desc->windowed;
4639 else if (!swapchain_desc->windowed)
4641 DWORD style = device->style;
4642 DWORD exStyle = device->exStyle;
4643 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4644 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4645 * Reset to clear up their mess. Guild Wars also loses the device during that.
4647 device->style = 0;
4648 device->exStyle = 0;
4649 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4650 swapchain_desc->backbuffer_width,
4651 swapchain_desc->backbuffer_height);
4652 device->style = style;
4653 device->exStyle = exStyle;
4656 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4658 if (reset_state)
4660 TRACE("Resetting stateblock.\n");
4661 if (device->recording)
4663 wined3d_stateblock_decref(device->recording);
4664 device->recording = NULL;
4666 wined3d_cs_emit_reset_state(device->cs);
4667 state_cleanup(&device->state);
4669 if (device->d3d_initialized)
4670 delete_opengl_contexts(device, swapchain);
4672 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4673 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4674 ERR("Failed to initialize device state, hr %#x.\n", hr);
4675 device->update_state = &device->state;
4677 device_init_swapchain_state(device, swapchain);
4679 else if (device->back_buffer_view)
4681 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4682 struct wined3d_state *state = &device->state;
4684 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4686 /* Note the min_z / max_z is not reset. */
4687 state->viewport.x = 0;
4688 state->viewport.y = 0;
4689 state->viewport.width = view->width;
4690 state->viewport.height = view->height;
4691 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4693 state->scissor_rect.top = 0;
4694 state->scissor_rect.left = 0;
4695 state->scissor_rect.right = view->width;
4696 state->scissor_rect.bottom = view->height;
4697 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4700 swapchain_update_render_to_fbo(swapchain);
4701 swapchain_update_draw_bindings(swapchain);
4703 if (reset_state && device->d3d_initialized)
4704 hr = create_primary_opengl_context(device, swapchain);
4706 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4707 * first use
4709 return hr;
4712 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4714 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4716 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4718 return WINED3D_OK;
4722 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4723 struct wined3d_device_creation_parameters *parameters)
4725 TRACE("device %p, parameters %p.\n", device, parameters);
4727 *parameters = device->create_parms;
4730 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4731 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4733 struct wined3d_swapchain *swapchain;
4735 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4736 device, swapchain_idx, flags, ramp);
4738 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4739 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4742 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4743 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4745 struct wined3d_swapchain *swapchain;
4747 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4748 device, swapchain_idx, ramp);
4750 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4751 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4754 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4756 TRACE("device %p, resource %p.\n", device, resource);
4758 list_add_head(&device->resources, &resource->resource_list_entry);
4761 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4763 TRACE("device %p, resource %p.\n", device, resource);
4765 list_remove(&resource->resource_list_entry);
4768 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4770 enum wined3d_resource_type type = resource->type;
4771 unsigned int i;
4773 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4775 context_resource_released(device, resource, type);
4777 switch (type)
4779 case WINED3D_RTYPE_SURFACE:
4781 struct wined3d_surface *surface = surface_from_resource(resource);
4783 if (!device->d3d_initialized) break;
4785 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4787 if (wined3d_rendertarget_view_get_surface(device->fb.render_targets[i]) == surface)
4789 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4790 device->fb.render_targets[i] = NULL;
4794 if (wined3d_rendertarget_view_get_surface(device->fb.depth_stencil) == surface)
4796 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4797 device->fb.depth_stencil = NULL;
4800 break;
4802 case WINED3D_RTYPE_TEXTURE:
4803 case WINED3D_RTYPE_CUBE_TEXTURE:
4804 case WINED3D_RTYPE_VOLUME_TEXTURE:
4805 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4807 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4809 if (device->state.textures[i] == texture)
4811 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4812 device->state.textures[i] = NULL;
4815 if (device->recording && device->update_state->textures[i] == texture)
4817 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4818 texture, device->recording, i);
4819 device->update_state->textures[i] = NULL;
4822 break;
4824 case WINED3D_RTYPE_BUFFER:
4826 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4828 for (i = 0; i < MAX_STREAMS; ++i)
4830 if (device->state.streams[i].buffer == buffer)
4832 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4833 device->state.streams[i].buffer = NULL;
4836 if (device->recording && device->update_state->streams[i].buffer == buffer)
4838 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4839 buffer, device->recording, i);
4840 device->update_state->streams[i].buffer = NULL;
4844 if (device->state.index_buffer == buffer)
4846 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4847 device->state.index_buffer = NULL;
4850 if (device->recording && device->update_state->index_buffer == buffer)
4852 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4853 buffer, device->recording);
4854 device->update_state->index_buffer = NULL;
4857 break;
4859 default:
4860 break;
4863 /* Remove the resource from the resourceStore */
4864 device_resource_remove(device, resource);
4866 TRACE("Resource released.\n");
4869 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4871 struct wined3d_resource *resource;
4873 TRACE("device %p, dc %p.\n", device, dc);
4875 if (!dc)
4876 return NULL;
4878 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4880 if (resource->type == WINED3D_RTYPE_SURFACE)
4882 struct wined3d_surface *s = surface_from_resource(resource);
4884 if (s->hDC == dc)
4886 TRACE("Found surface %p for dc %p.\n", s, dc);
4887 return s;
4892 return NULL;
4895 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4897 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4899 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4902 static const struct wine_rb_functions wined3d_sampler_rb_functions =
4904 wined3d_rb_alloc,
4905 wined3d_rb_realloc,
4906 wined3d_rb_free,
4907 wined3d_sampler_compare,
4910 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4911 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4912 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4914 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4915 const struct fragment_pipeline *fragment_pipeline;
4916 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4917 unsigned int i;
4918 HRESULT hr;
4920 device->ref = 1;
4921 device->wined3d = wined3d;
4922 wined3d_incref(device->wined3d);
4923 device->adapter = wined3d->adapter_count ? adapter : NULL;
4924 device->device_parent = device_parent;
4925 list_init(&device->resources);
4926 list_init(&device->shaders);
4927 device->surface_alignment = surface_alignment;
4929 /* Save the creation parameters. */
4930 device->create_parms.adapter_idx = adapter_idx;
4931 device->create_parms.device_type = device_type;
4932 device->create_parms.focus_window = focus_window;
4933 device->create_parms.flags = flags;
4935 device->shader_backend = adapter->shader_backend;
4937 vertex_pipeline = adapter->vertex_pipe;
4939 fragment_pipeline = adapter->fragment_pipe;
4941 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
4943 ERR("Failed to initialize sampler rbtree.\n");
4944 return E_OUTOFMEMORY;
4947 if (vertex_pipeline->vp_states && fragment_pipeline->states
4948 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4949 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4950 fragment_pipeline, misc_state_template)))
4952 ERR("Failed to compile state table, hr %#x.\n", hr);
4953 wine_rb_destroy(&device->samplers, NULL, NULL);
4954 wined3d_decref(device->wined3d);
4955 return hr;
4958 device->blitter = adapter->blitter;
4960 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
4961 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4963 ERR("Failed to initialize device state, hr %#x.\n", hr);
4964 goto err;
4966 device->update_state = &device->state;
4968 if (!(device->cs = wined3d_cs_create(device)))
4970 WARN("Failed to create command stream.\n");
4971 state_cleanup(&device->state);
4972 hr = E_FAIL;
4973 goto err;
4976 return WINED3D_OK;
4978 err:
4979 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4981 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4983 wine_rb_destroy(&device->samplers, NULL, NULL);
4984 wined3d_decref(device->wined3d);
4985 return hr;
4989 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4991 DWORD rep = device->StateTable[state].representative;
4992 struct wined3d_context *context;
4993 DWORD idx;
4994 BYTE shift;
4995 UINT i;
4997 for (i = 0; i < device->context_count; ++i)
4999 context = device->contexts[i];
5000 if(isStateDirty(context, rep)) continue;
5002 context->dirtyArray[context->numDirtyEntries++] = rep;
5003 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5004 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5005 context->isStateDirty[idx] |= (1 << shift);
5009 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5010 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5012 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5014 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5015 window, message, wparam, lparam);
5016 if (unicode)
5017 return DefWindowProcW(window, message, wparam, lparam);
5018 else
5019 return DefWindowProcA(window, message, wparam, lparam);
5022 if (message == WM_DESTROY)
5024 TRACE("unregister window %p.\n", window);
5025 wined3d_unregister_window(window);
5027 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5028 ERR("Window %p is not the focus window for device %p.\n", window, device);
5030 else if (message == WM_DISPLAYCHANGE)
5032 device->device_parent->ops->mode_changed(device->device_parent);
5034 else if (message == WM_ACTIVATEAPP)
5036 UINT i;
5038 for (i = 0; i < device->swapchain_count; i++)
5039 wined3d_swapchain_activate(device->swapchains[i], wparam);
5041 device->device_parent->ops->activate(device->device_parent, wparam);
5043 else if (message == WM_SYSCOMMAND)
5045 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5047 if (unicode)
5048 DefWindowProcW(window, message, wparam, lparam);
5049 else
5050 DefWindowProcA(window, message, wparam, lparam);
5054 if (unicode)
5055 return CallWindowProcW(proc, window, message, wparam, lparam);
5056 else
5057 return CallWindowProcA(proc, window, message, wparam, lparam);