qcap/tests: Add media tests for the SmartTee filter.
[wine/multimedia.git] / dlls / wined3d / device.c
blob6b40c6272f5d43062b9f13c340dd9e6dfc7b35b5
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_GLSPOT:
1459 /* Incorrect attenuation values can cause the gl driver to crash.
1460 * Happens with Need for speed most wanted. */
1461 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1463 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1464 return WINED3DERR_INVALIDCALL;
1466 break;
1468 case WINED3D_LIGHT_DIRECTIONAL:
1469 case WINED3D_LIGHT_PARALLELPOINT:
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->position.x = light->position.x;
1525 object->position.y = light->position.y;
1526 object->position.z = light->position.z;
1527 object->position.w = 1.0f;
1528 object->cutoff = 180.0f;
1529 /* FIXME: Range */
1530 break;
1532 case WINED3D_LIGHT_DIRECTIONAL:
1533 /* Direction */
1534 object->direction.x = -light->direction.x;
1535 object->direction.y = -light->direction.y;
1536 object->direction.z = -light->direction.z;
1537 object->direction.w = 0.0f;
1538 object->exponent = 0.0f;
1539 object->cutoff = 180.0f;
1540 break;
1542 case WINED3D_LIGHT_SPOT:
1543 /* Position */
1544 object->position.x = light->position.x;
1545 object->position.y = light->position.y;
1546 object->position.z = light->position.z;
1547 object->position.w = 1.0f;
1549 /* Direction */
1550 object->direction.x = light->direction.x;
1551 object->direction.y = light->direction.y;
1552 object->direction.z = light->direction.z;
1553 object->direction.w = 0.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 case WINED3D_LIGHT_PARALLELPOINT:
1586 object->position.x = light->position.x;
1587 object->position.y = light->position.y;
1588 object->position.z = light->position.z;
1589 object->position.w = 1.0f;
1590 break;
1592 default:
1593 FIXME("Unrecognized light type %#x.\n", light->type);
1596 return WINED3D_OK;
1599 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1600 UINT light_idx, struct wined3d_light *light)
1602 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1603 struct wined3d_light_info *light_info = NULL;
1604 struct list *e;
1606 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1608 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1610 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1611 if (light_info->OriginalIndex == light_idx)
1612 break;
1613 light_info = NULL;
1616 if (!light_info)
1618 TRACE("Light information requested but light not defined\n");
1619 return WINED3DERR_INVALIDCALL;
1622 *light = light_info->OriginalParms;
1623 return WINED3D_OK;
1626 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1628 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1629 struct wined3d_light_info *light_info = NULL;
1630 struct list *e;
1632 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1634 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1636 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1637 if (light_info->OriginalIndex == light_idx)
1638 break;
1639 light_info = NULL;
1641 TRACE("Found light %p.\n", light_info);
1643 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1644 if (!light_info)
1646 TRACE("Light enabled requested but light not defined, so defining one!\n");
1647 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1649 /* Search for it again! Should be fairly quick as near head of list. */
1650 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1652 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1653 if (light_info->OriginalIndex == light_idx)
1654 break;
1655 light_info = NULL;
1657 if (!light_info)
1659 FIXME("Adding default lights has failed dismally\n");
1660 return WINED3DERR_INVALIDCALL;
1664 if (!enable)
1666 if (light_info->glIndex != -1)
1668 if (!device->recording)
1670 device_invalidate_state(device, STATE_LIGHT_TYPE);
1671 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1674 device->update_state->lights[light_info->glIndex] = NULL;
1675 light_info->glIndex = -1;
1677 else
1679 TRACE("Light already disabled, nothing to do\n");
1681 light_info->enabled = FALSE;
1683 else
1685 light_info->enabled = TRUE;
1686 if (light_info->glIndex != -1)
1688 TRACE("Nothing to do as light was enabled\n");
1690 else
1692 unsigned int i;
1693 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1694 /* Find a free GL light. */
1695 for (i = 0; i < gl_info->limits.lights; ++i)
1697 if (!device->update_state->lights[i])
1699 device->update_state->lights[i] = light_info;
1700 light_info->glIndex = i;
1701 break;
1704 if (light_info->glIndex == -1)
1706 /* Our tests show that Windows returns D3D_OK in this situation, even with
1707 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1708 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1709 * as well for those lights.
1711 * TODO: Test how this affects rendering. */
1712 WARN("Too many concurrently active lights\n");
1713 return WINED3D_OK;
1716 /* i == light_info->glIndex */
1717 if (!device->recording)
1719 device_invalidate_state(device, STATE_LIGHT_TYPE);
1720 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1725 return WINED3D_OK;
1728 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1730 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1731 struct wined3d_light_info *light_info = NULL;
1732 struct list *e;
1734 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1736 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1738 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1739 if (light_info->OriginalIndex == light_idx)
1740 break;
1741 light_info = NULL;
1744 if (!light_info)
1746 TRACE("Light enabled state requested but light not defined.\n");
1747 return WINED3DERR_INVALIDCALL;
1749 /* true is 128 according to SetLightEnable */
1750 *enable = light_info->enabled ? 128 : 0;
1751 return WINED3D_OK;
1754 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1755 UINT plane_idx, const struct wined3d_vec4 *plane)
1757 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1759 /* Validate plane_idx. */
1760 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1762 TRACE("Application has requested clipplane this device doesn't support.\n");
1763 return WINED3DERR_INVALIDCALL;
1766 if (device->recording)
1767 device->recording->changed.clipplane |= 1 << plane_idx;
1769 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1771 TRACE("Application is setting old values over, nothing to do.\n");
1772 return WINED3D_OK;
1775 device->update_state->clip_planes[plane_idx] = *plane;
1777 if (!device->recording)
1778 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1780 return WINED3D_OK;
1783 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1784 UINT plane_idx, struct wined3d_vec4 *plane)
1786 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1788 /* Validate plane_idx. */
1789 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1791 TRACE("Application has requested clipplane this device doesn't support.\n");
1792 return WINED3DERR_INVALIDCALL;
1795 *plane = device->state.clip_planes[plane_idx];
1797 return WINED3D_OK;
1800 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1801 const struct wined3d_clip_status *clip_status)
1803 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1805 if (!clip_status)
1806 return WINED3DERR_INVALIDCALL;
1808 return WINED3D_OK;
1811 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1812 struct wined3d_clip_status *clip_status)
1814 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1816 if (!clip_status)
1817 return WINED3DERR_INVALIDCALL;
1819 return WINED3D_OK;
1822 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1824 TRACE("device %p, material %p.\n", device, material);
1826 device->update_state->material = *material;
1828 if (device->recording)
1829 device->recording->changed.material = TRUE;
1830 else
1831 wined3d_cs_emit_set_material(device->cs, material);
1834 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1836 TRACE("device %p, material %p.\n", device, material);
1838 *material = device->state.material;
1840 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1841 material->diffuse.r, material->diffuse.g,
1842 material->diffuse.b, material->diffuse.a);
1843 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1844 material->ambient.r, material->ambient.g,
1845 material->ambient.b, material->ambient.a);
1846 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1847 material->specular.r, material->specular.g,
1848 material->specular.b, material->specular.a);
1849 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1850 material->emissive.r, material->emissive.g,
1851 material->emissive.b, material->emissive.a);
1852 TRACE("power %.8e.\n", material->power);
1855 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1856 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1858 enum wined3d_format_id prev_format;
1859 struct wined3d_buffer *prev_buffer;
1861 TRACE("device %p, buffer %p, format %s.\n",
1862 device, buffer, debug_d3dformat(format_id));
1864 prev_buffer = device->update_state->index_buffer;
1865 prev_format = device->update_state->index_format;
1867 device->update_state->index_buffer = buffer;
1868 device->update_state->index_format = format_id;
1870 if (device->recording)
1871 device->recording->changed.indices = TRUE;
1873 if (prev_buffer == buffer && prev_format == format_id)
1874 return;
1876 if (buffer)
1877 wined3d_buffer_incref(buffer);
1878 if (!device->recording)
1879 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1880 if (prev_buffer)
1881 wined3d_buffer_decref(prev_buffer);
1884 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1885 enum wined3d_format_id *format)
1887 TRACE("device %p, format %p.\n", device, format);
1889 *format = device->state.index_format;
1890 return device->state.index_buffer;
1893 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1895 TRACE("device %p, base_index %d.\n", device, base_index);
1897 device->update_state->base_vertex_index = base_index;
1900 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1902 TRACE("device %p.\n", device);
1904 return device->state.base_vertex_index;
1907 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1909 TRACE("device %p, viewport %p.\n", device, viewport);
1910 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1911 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1913 device->update_state->viewport = *viewport;
1915 /* Handle recording of state blocks */
1916 if (device->recording)
1918 TRACE("Recording... not performing anything\n");
1919 device->recording->changed.viewport = TRUE;
1920 return;
1923 wined3d_cs_emit_set_viewport(device->cs, viewport);
1926 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1928 TRACE("device %p, viewport %p.\n", device, viewport);
1930 *viewport = device->state.viewport;
1933 static void resolve_depth_buffer(struct wined3d_state *state)
1935 struct wined3d_texture *texture = state->textures[0];
1936 struct wined3d_surface *depth_stencil, *surface;
1938 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1939 || !(texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1940 return;
1941 surface = surface_from_resource(texture->sub_resources[0]);
1942 if (!(depth_stencil = wined3d_rendertarget_view_get_surface(state->fb->depth_stencil)))
1943 return;
1945 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1948 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1949 enum wined3d_render_state state, DWORD value)
1951 DWORD old_value = device->state.render_states[state];
1953 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1955 device->update_state->render_states[state] = value;
1957 /* Handle recording of state blocks. */
1958 if (device->recording)
1960 TRACE("Recording... not performing anything.\n");
1961 device->recording->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
1962 return;
1965 /* Compared here and not before the assignment to allow proper stateblock recording. */
1966 if (value == old_value)
1967 TRACE("Application is setting the old value over, nothing to do.\n");
1968 else
1969 wined3d_cs_emit_set_render_state(device->cs, state, value);
1971 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1973 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1974 resolve_depth_buffer(&device->state);
1978 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1980 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1982 return device->state.render_states[state];
1985 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1986 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1988 DWORD old_value;
1990 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1991 device, sampler_idx, debug_d3dsamplerstate(state), value);
1993 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1994 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1996 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1998 WARN("Invalid sampler %u.\n", sampler_idx);
1999 return; /* Windows accepts overflowing this array ... we do not. */
2002 old_value = device->state.sampler_states[sampler_idx][state];
2003 device->update_state->sampler_states[sampler_idx][state] = value;
2005 /* Handle recording of state blocks. */
2006 if (device->recording)
2008 TRACE("Recording... not performing anything.\n");
2009 device->recording->changed.samplerState[sampler_idx] |= 1 << state;
2010 return;
2013 if (old_value == value)
2015 TRACE("Application is setting the old value over, nothing to do.\n");
2016 return;
2019 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2022 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2023 UINT sampler_idx, enum wined3d_sampler_state state)
2025 TRACE("device %p, sampler_idx %u, state %s.\n",
2026 device, sampler_idx, debug_d3dsamplerstate(state));
2028 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2029 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2031 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2033 WARN("Invalid sampler %u.\n", sampler_idx);
2034 return 0; /* Windows accepts overflowing this array ... we do not. */
2037 return device->state.sampler_states[sampler_idx][state];
2040 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2042 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2044 if (device->recording)
2045 device->recording->changed.scissorRect = TRUE;
2047 if (EqualRect(&device->update_state->scissor_rect, rect))
2049 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2050 return;
2052 CopyRect(&device->update_state->scissor_rect, rect);
2054 if (device->recording)
2056 TRACE("Recording... not performing anything.\n");
2057 return;
2060 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2063 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2065 TRACE("device %p, rect %p.\n", device, rect);
2067 *rect = device->state.scissor_rect;
2068 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2071 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2072 struct wined3d_vertex_declaration *declaration)
2074 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2076 TRACE("device %p, declaration %p.\n", device, declaration);
2078 if (device->recording)
2079 device->recording->changed.vertexDecl = TRUE;
2081 if (declaration == prev)
2082 return;
2084 if (declaration)
2085 wined3d_vertex_declaration_incref(declaration);
2086 device->update_state->vertex_declaration = declaration;
2087 if (!device->recording)
2088 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2089 if (prev)
2090 wined3d_vertex_declaration_decref(prev);
2093 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2095 TRACE("device %p.\n", device);
2097 return device->state.vertex_declaration;
2100 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2102 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2104 TRACE("device %p, shader %p.\n", device, shader);
2106 if (device->recording)
2107 device->recording->changed.vertexShader = TRUE;
2109 if (shader == prev)
2110 return;
2112 if (shader)
2113 wined3d_shader_incref(shader);
2114 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2115 if (!device->recording)
2116 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2117 if (prev)
2118 wined3d_shader_decref(prev);
2121 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2123 TRACE("device %p.\n", device);
2125 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2128 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2129 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2131 struct wined3d_buffer *prev;
2133 if (idx >= MAX_CONSTANT_BUFFERS)
2135 WARN("Invalid constant buffer index %u.\n", idx);
2136 return;
2139 prev = device->update_state->cb[type][idx];
2140 if (buffer == prev)
2141 return;
2143 if (buffer)
2144 wined3d_buffer_incref(buffer);
2145 device->update_state->cb[type][idx] = buffer;
2146 if (!device->recording)
2147 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2148 if (prev)
2149 wined3d_buffer_decref(prev);
2152 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2154 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2156 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2159 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2161 TRACE("device %p, idx %u.\n", device, idx);
2163 if (idx >= MAX_CONSTANT_BUFFERS)
2165 WARN("Invalid constant buffer index %u.\n", idx);
2166 return NULL;
2169 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2172 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2173 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2175 struct wined3d_shader_resource_view *prev;
2177 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2179 WARN("Invalid view index %u.\n", idx);
2180 return;
2183 prev = device->update_state->shader_resource_view[type][idx];
2184 if (view == prev)
2185 return;
2187 if (view)
2188 wined3d_shader_resource_view_incref(view);
2189 device->update_state->shader_resource_view[type][idx] = view;
2190 if (!device->recording)
2191 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2192 if (prev)
2193 wined3d_shader_resource_view_decref(prev);
2196 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2197 UINT idx, struct wined3d_shader_resource_view *view)
2199 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2201 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2204 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2205 UINT idx)
2207 TRACE("device %p, idx %u.\n", device, idx);
2209 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2211 WARN("Invalid view index %u.\n", idx);
2212 return NULL;
2215 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2218 static void wined3d_device_set_sampler(struct wined3d_device *device,
2219 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2221 struct wined3d_sampler *prev;
2223 if (idx >= MAX_SAMPLER_OBJECTS)
2225 WARN("Invalid sampler index %u.\n", idx);
2226 return;
2229 prev = device->update_state->sampler[type][idx];
2230 if (sampler == prev)
2231 return;
2233 if (sampler)
2234 wined3d_sampler_incref(sampler);
2235 device->update_state->sampler[type][idx] = sampler;
2236 if (!device->recording)
2237 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2238 if (prev)
2239 wined3d_sampler_decref(prev);
2242 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2244 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2246 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2249 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2251 TRACE("device %p, idx %u.\n", device, idx);
2253 if (idx >= MAX_SAMPLER_OBJECTS)
2255 WARN("Invalid sampler index %u.\n", idx);
2256 return NULL;
2259 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2262 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2264 UINT i;
2266 for (i = 0; i < device->context_count; ++i)
2268 device->contexts[i]->constant_update_mask |= mask;
2272 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2273 UINT start_register, const BOOL *constants, UINT bool_count)
2275 UINT count = min(bool_count, MAX_CONST_B - start_register);
2276 UINT i;
2278 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2279 device, start_register, constants, bool_count);
2281 if (!constants || start_register >= MAX_CONST_B)
2282 return WINED3DERR_INVALIDCALL;
2284 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2285 for (i = 0; i < count; ++i)
2286 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2288 if (device->recording)
2290 for (i = start_register; i < count + start_register; ++i)
2291 device->recording->changed.vertexShaderConstantsB |= (1 << i);
2293 else
2295 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2298 return WINED3D_OK;
2301 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2302 UINT start_register, BOOL *constants, UINT bool_count)
2304 UINT count = min(bool_count, MAX_CONST_B - start_register);
2306 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2307 device, start_register, constants, bool_count);
2309 if (!constants || start_register >= MAX_CONST_B)
2310 return WINED3DERR_INVALIDCALL;
2312 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2314 return WINED3D_OK;
2317 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2318 UINT start_register, const int *constants, UINT vector4i_count)
2320 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2321 UINT i;
2323 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2324 device, start_register, constants, vector4i_count);
2326 if (!constants || start_register >= MAX_CONST_I)
2327 return WINED3DERR_INVALIDCALL;
2329 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2330 for (i = 0; i < count; ++i)
2331 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2332 constants[i * 4], constants[i * 4 + 1],
2333 constants[i * 4 + 2], constants[i * 4 + 3]);
2335 if (device->recording)
2337 for (i = start_register; i < count + start_register; ++i)
2338 device->recording->changed.vertexShaderConstantsI |= (1 << i);
2340 else
2342 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2345 return WINED3D_OK;
2348 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2349 UINT start_register, int *constants, UINT vector4i_count)
2351 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2353 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2354 device, start_register, constants, vector4i_count);
2356 if (!constants || start_register >= MAX_CONST_I)
2357 return WINED3DERR_INVALIDCALL;
2359 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2360 return WINED3D_OK;
2363 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2364 UINT start_register, const float *constants, UINT vector4f_count)
2366 UINT i;
2367 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2369 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2370 device, start_register, constants, vector4f_count);
2372 /* Specifically test start_register > limit to catch MAX_UINT overflows
2373 * when adding start_register + vector4f_count. */
2374 if (!constants
2375 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2376 || start_register > d3d_info->limits.vs_uniform_count)
2377 return WINED3DERR_INVALIDCALL;
2379 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2380 constants, vector4f_count * sizeof(float) * 4);
2381 if (TRACE_ON(d3d))
2383 for (i = 0; i < vector4f_count; ++i)
2384 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2385 constants[i * 4], constants[i * 4 + 1],
2386 constants[i * 4 + 2], constants[i * 4 + 3]);
2389 if (device->recording)
2390 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2391 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2392 else
2393 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2396 return WINED3D_OK;
2399 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2400 UINT start_register, float *constants, UINT vector4f_count)
2402 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2403 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2405 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2406 device, start_register, constants, vector4f_count);
2408 if (!constants || count < 0)
2409 return WINED3DERR_INVALIDCALL;
2411 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2413 return WINED3D_OK;
2416 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2418 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2420 TRACE("device %p, shader %p.\n", device, shader);
2422 if (device->recording)
2423 device->recording->changed.pixelShader = TRUE;
2425 if (shader == prev)
2426 return;
2428 if (shader)
2429 wined3d_shader_incref(shader);
2430 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2431 if (!device->recording)
2432 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2433 if (prev)
2434 wined3d_shader_decref(prev);
2437 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2439 TRACE("device %p.\n", device);
2441 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2444 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2446 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2448 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2451 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2453 TRACE("device %p, idx %u.\n", device, idx);
2455 if (idx >= MAX_CONSTANT_BUFFERS)
2457 WARN("Invalid constant buffer index %u.\n", idx);
2458 return NULL;
2461 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2464 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2465 UINT idx, struct wined3d_shader_resource_view *view)
2467 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2469 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2472 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2473 UINT idx)
2475 TRACE("device %p, idx %u.\n", device, idx);
2477 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2479 WARN("Invalid view index %u.\n", idx);
2480 return NULL;
2483 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2486 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2488 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2490 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2493 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2495 TRACE("device %p, idx %u.\n", device, idx);
2497 if (idx >= MAX_SAMPLER_OBJECTS)
2499 WARN("Invalid sampler index %u.\n", idx);
2500 return NULL;
2503 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2506 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2507 UINT start_register, const BOOL *constants, UINT bool_count)
2509 UINT count = min(bool_count, MAX_CONST_B - start_register);
2510 UINT i;
2512 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2513 device, start_register, constants, bool_count);
2515 if (!constants || start_register >= MAX_CONST_B)
2516 return WINED3DERR_INVALIDCALL;
2518 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2519 for (i = 0; i < count; ++i)
2520 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2522 if (device->recording)
2524 for (i = start_register; i < count + start_register; ++i)
2525 device->recording->changed.pixelShaderConstantsB |= (1 << i);
2527 else
2529 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2532 return WINED3D_OK;
2535 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2536 UINT start_register, BOOL *constants, UINT bool_count)
2538 UINT count = min(bool_count, MAX_CONST_B - start_register);
2540 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2541 device, start_register, constants, bool_count);
2543 if (!constants || start_register >= MAX_CONST_B)
2544 return WINED3DERR_INVALIDCALL;
2546 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2548 return WINED3D_OK;
2551 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2552 UINT start_register, const int *constants, UINT vector4i_count)
2554 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2555 UINT i;
2557 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2558 device, start_register, constants, vector4i_count);
2560 if (!constants || start_register >= MAX_CONST_I)
2561 return WINED3DERR_INVALIDCALL;
2563 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2564 for (i = 0; i < count; ++i)
2565 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2566 constants[i * 4], constants[i * 4 + 1],
2567 constants[i * 4 + 2], constants[i * 4 + 3]);
2569 if (device->recording)
2571 for (i = start_register; i < count + start_register; ++i)
2572 device->recording->changed.pixelShaderConstantsI |= (1 << i);
2574 else
2576 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2579 return WINED3D_OK;
2582 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2583 UINT start_register, int *constants, UINT vector4i_count)
2585 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2587 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2588 device, start_register, constants, vector4i_count);
2590 if (!constants || start_register >= MAX_CONST_I)
2591 return WINED3DERR_INVALIDCALL;
2593 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2595 return WINED3D_OK;
2598 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2599 UINT start_register, const float *constants, UINT vector4f_count)
2601 UINT i;
2602 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2604 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2605 device, start_register, constants, vector4f_count);
2607 /* Specifically test start_register > limit to catch MAX_UINT overflows
2608 * when adding start_register + vector4f_count. */
2609 if (!constants
2610 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2611 || start_register > d3d_info->limits.ps_uniform_count)
2612 return WINED3DERR_INVALIDCALL;
2614 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2615 constants, vector4f_count * sizeof(float) * 4);
2616 if (TRACE_ON(d3d))
2618 for (i = 0; i < vector4f_count; ++i)
2619 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2620 constants[i * 4], constants[i * 4 + 1],
2621 constants[i * 4 + 2], constants[i * 4 + 3]);
2624 if (device->recording)
2625 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2626 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2627 else
2628 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2630 return WINED3D_OK;
2633 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2634 UINT start_register, float *constants, UINT vector4f_count)
2636 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2637 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2639 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2640 device, start_register, constants, vector4f_count);
2642 if (!constants || count < 0)
2643 return WINED3DERR_INVALIDCALL;
2645 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2647 return WINED3D_OK;
2650 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2652 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2654 TRACE("device %p, shader %p.\n", device, shader);
2656 if (device->recording || shader == prev)
2657 return;
2658 if (shader)
2659 wined3d_shader_incref(shader);
2660 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2661 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2662 if (prev)
2663 wined3d_shader_decref(prev);
2666 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2668 TRACE("device %p.\n", device);
2670 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2673 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2675 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2677 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2680 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2682 TRACE("device %p, idx %u.\n", device, idx);
2684 if (idx >= MAX_CONSTANT_BUFFERS)
2686 WARN("Invalid constant buffer index %u.\n", idx);
2687 return NULL;
2690 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2693 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2694 UINT idx, struct wined3d_shader_resource_view *view)
2696 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2698 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2701 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2702 UINT idx)
2704 TRACE("device %p, idx %u.\n", device, idx);
2706 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2708 WARN("Invalid view index %u.\n", idx);
2709 return NULL;
2712 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2715 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2717 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2719 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2722 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2724 TRACE("device %p, idx %u.\n", device, idx);
2726 if (idx >= MAX_SAMPLER_OBJECTS)
2728 WARN("Invalid sampler index %u.\n", idx);
2729 return NULL;
2732 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2735 /* Context activation is done by the caller. */
2736 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2737 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2738 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2739 DWORD DestFVF)
2741 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2742 struct wined3d_viewport vp;
2743 UINT vertex_size;
2744 unsigned int i;
2745 BYTE *dest_ptr;
2746 BOOL doClip;
2747 DWORD numTextures;
2748 HRESULT hr;
2750 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
2752 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2755 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
2757 ERR("Source has no position mask\n");
2758 return WINED3DERR_INVALIDCALL;
2761 if (device->state.render_states[WINED3D_RS_CLIPPING])
2763 static BOOL warned = FALSE;
2765 * The clipping code is not quite correct. Some things need
2766 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2767 * so disable clipping for now.
2768 * (The graphics in Half-Life are broken, and my processvertices
2769 * test crashes with IDirect3DDevice3)
2770 doClip = TRUE;
2772 doClip = FALSE;
2773 if(!warned) {
2774 warned = TRUE;
2775 FIXME("Clipping is broken and disabled for now\n");
2778 else
2779 doClip = FALSE;
2781 vertex_size = get_flexible_vertex_size(DestFVF);
2782 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2784 WARN("Failed to map buffer, hr %#x.\n", hr);
2785 return hr;
2788 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2789 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2790 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2792 TRACE("View mat:\n");
2793 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2794 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2795 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2796 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2798 TRACE("Proj mat:\n");
2799 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2800 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2801 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2802 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2804 TRACE("World mat:\n");
2805 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2806 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2807 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2808 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2810 /* Get the viewport */
2811 wined3d_device_get_viewport(device, &vp);
2812 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2813 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2815 multiply_matrix(&mat,&view_mat,&world_mat);
2816 multiply_matrix(&mat,&proj_mat,&mat);
2818 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2820 for (i = 0; i < dwCount; i+= 1) {
2821 unsigned int tex_index;
2823 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2824 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2825 /* The position first */
2826 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2827 const float *p = (const float *)(element->data.addr + i * element->stride);
2828 float x, y, z, rhw;
2829 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2831 /* Multiplication with world, view and projection matrix. */
2832 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2833 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2834 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2835 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2837 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2839 /* WARNING: The following things are taken from d3d7 and were not yet checked
2840 * against d3d8 or d3d9!
2843 /* Clipping conditions: From msdn
2845 * A vertex is clipped if it does not match the following requirements
2846 * -rhw < x <= rhw
2847 * -rhw < y <= rhw
2848 * 0 < z <= rhw
2849 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2851 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2852 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2856 if( !doClip ||
2857 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2858 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2859 ( rhw > eps ) ) ) {
2861 /* "Normal" viewport transformation (not clipped)
2862 * 1) The values are divided by rhw
2863 * 2) The y axis is negative, so multiply it with -1
2864 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2865 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2866 * 4) Multiply x with Width/2 and add Width/2
2867 * 5) The same for the height
2868 * 6) Add the viewpoint X and Y to the 2D coordinates and
2869 * The minimum Z value to z
2870 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2872 * Well, basically it's simply a linear transformation into viewport
2873 * coordinates
2876 x /= rhw;
2877 y /= rhw;
2878 z /= rhw;
2880 y *= -1;
2882 x *= vp.width / 2;
2883 y *= vp.height / 2;
2884 z *= vp.max_z - vp.min_z;
2886 x += vp.width / 2 + vp.x;
2887 y += vp.height / 2 + vp.y;
2888 z += vp.min_z;
2890 rhw = 1 / rhw;
2891 } else {
2892 /* That vertex got clipped
2893 * Contrary to OpenGL it is not dropped completely, it just
2894 * undergoes a different calculation.
2896 TRACE("Vertex got clipped\n");
2897 x += rhw;
2898 y += rhw;
2900 x /= 2;
2901 y /= 2;
2903 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2904 * outside of the main vertex buffer memory. That needs some more
2905 * investigation...
2909 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2912 ( (float *) dest_ptr)[0] = x;
2913 ( (float *) dest_ptr)[1] = y;
2914 ( (float *) dest_ptr)[2] = z;
2915 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2917 dest_ptr += 3 * sizeof(float);
2919 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2920 dest_ptr += sizeof(float);
2923 if (DestFVF & WINED3DFVF_PSIZE)
2924 dest_ptr += sizeof(DWORD);
2926 if (DestFVF & WINED3DFVF_NORMAL)
2928 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2929 const float *normal = (const float *)(element->data.addr + i * element->stride);
2930 /* AFAIK this should go into the lighting information */
2931 FIXME("Didn't expect the destination to have a normal\n");
2932 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2935 if (DestFVF & WINED3DFVF_DIFFUSE)
2937 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2938 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2939 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
2941 static BOOL warned = FALSE;
2943 if(!warned) {
2944 ERR("No diffuse color in source, but destination has one\n");
2945 warned = TRUE;
2948 *( (DWORD *) dest_ptr) = 0xffffffff;
2949 dest_ptr += sizeof(DWORD);
2951 else
2953 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2957 if (DestFVF & WINED3DFVF_SPECULAR)
2959 /* What's the color value in the feedback buffer? */
2960 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2961 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2962 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
2964 static BOOL warned = FALSE;
2966 if(!warned) {
2967 ERR("No specular color in source, but destination has one\n");
2968 warned = TRUE;
2971 *(DWORD *)dest_ptr = 0xff000000;
2972 dest_ptr += sizeof(DWORD);
2974 else
2976 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2980 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2982 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2983 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2984 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2986 ERR("No source texture, but destination requests one\n");
2987 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2989 else
2991 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2996 wined3d_buffer_unmap(dest);
2998 return WINED3D_OK;
3000 #undef copy_and_next
3002 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3003 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3004 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3006 struct wined3d_state *state = &device->state;
3007 struct wined3d_stream_info stream_info;
3008 const struct wined3d_gl_info *gl_info;
3009 struct wined3d_context *context;
3010 struct wined3d_shader *vs;
3011 unsigned int i;
3012 HRESULT hr;
3013 WORD map;
3015 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3016 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3017 device, src_start_idx, dst_idx, vertex_count,
3018 dst_buffer, declaration, flags, dst_fvf);
3020 if (declaration)
3021 FIXME("Output vertex declaration not implemented yet.\n");
3023 /* Need any context to write to the vbo. */
3024 context = context_acquire(device, NULL);
3025 gl_info = context->gl_info;
3027 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3028 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3029 context_stream_info_from_declaration(context, state, &stream_info);
3030 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3032 /* We can't convert FROM a VBO, and vertex buffers used to source into
3033 * process_vertices() are unlikely to ever be used for drawing. Release
3034 * VBOs in those buffers and fix up the stream_info structure.
3036 * Also apply the start index. */
3037 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3039 struct wined3d_stream_info_element *e;
3040 struct wined3d_buffer *buffer;
3042 if (!(map & 1))
3043 continue;
3045 e = &stream_info.elements[i];
3046 buffer = state->streams[e->stream_idx].buffer;
3047 e->data.buffer_object = 0;
3048 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3049 if (buffer->buffer_object)
3051 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3052 buffer->buffer_object = 0;
3054 if (e->data.addr)
3055 e->data.addr += e->stride * src_start_idx;
3058 hr = process_vertices_strided(device, dst_idx, vertex_count,
3059 &stream_info, dst_buffer, flags, dst_fvf);
3061 context_release(context);
3063 return hr;
3066 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3067 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3069 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3070 DWORD old_value;
3072 TRACE("device %p, stage %u, state %s, value %#x.\n",
3073 device, stage, debug_d3dtexturestate(state), value);
3075 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3077 WARN("Invalid state %#x passed.\n", state);
3078 return;
3081 if (stage >= d3d_info->limits.ffp_blend_stages)
3083 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3084 stage, d3d_info->limits.ffp_blend_stages - 1);
3085 return;
3088 old_value = device->update_state->texture_states[stage][state];
3089 device->update_state->texture_states[stage][state] = value;
3091 if (device->recording)
3093 TRACE("Recording... not performing anything.\n");
3094 device->recording->changed.textureState[stage] |= 1 << state;
3095 return;
3098 /* Checked after the assignments to allow proper stateblock recording. */
3099 if (old_value == value)
3101 TRACE("Application is setting the old value over, nothing to do.\n");
3102 return;
3105 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3108 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3109 UINT stage, enum wined3d_texture_stage_state state)
3111 TRACE("device %p, stage %u, state %s.\n",
3112 device, stage, debug_d3dtexturestate(state));
3114 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3116 WARN("Invalid state %#x passed.\n", state);
3117 return 0;
3120 return device->state.texture_states[stage][state];
3123 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3124 UINT stage, struct wined3d_texture *texture)
3126 struct wined3d_texture *prev;
3128 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3130 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3131 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3133 /* Windows accepts overflowing this array... we do not. */
3134 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3136 WARN("Ignoring invalid stage %u.\n", stage);
3137 return WINED3D_OK;
3140 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3142 WARN("Rejecting attempt to set scratch texture.\n");
3143 return WINED3DERR_INVALIDCALL;
3146 if (device->recording)
3147 device->recording->changed.textures |= 1 << stage;
3149 prev = device->update_state->textures[stage];
3150 TRACE("Previous texture %p.\n", prev);
3152 if (texture == prev)
3154 TRACE("App is setting the same texture again, nothing to do.\n");
3155 return WINED3D_OK;
3158 TRACE("Setting new texture to %p.\n", texture);
3159 device->update_state->textures[stage] = texture;
3161 if (texture)
3162 wined3d_texture_incref(texture);
3163 if (!device->recording)
3164 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3165 if (prev)
3166 wined3d_texture_decref(prev);
3168 return WINED3D_OK;
3171 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3173 TRACE("device %p, stage %u.\n", device, stage);
3175 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3176 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3178 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3180 WARN("Ignoring invalid stage %u.\n", stage);
3181 return NULL; /* Windows accepts overflowing this array ... we do not. */
3184 return device->state.textures[stage];
3187 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3188 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3190 struct wined3d_swapchain *swapchain;
3192 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3193 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3195 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3196 return WINED3DERR_INVALIDCALL;
3198 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3199 return WINED3DERR_INVALIDCALL;
3200 return WINED3D_OK;
3203 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3205 TRACE("device %p, caps %p.\n", device, caps);
3207 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3208 device->create_parms.device_type, caps);
3211 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3212 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3214 struct wined3d_swapchain *swapchain;
3216 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3217 device, swapchain_idx, mode, rotation);
3219 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3220 return WINED3DERR_INVALIDCALL;
3222 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3225 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3227 struct wined3d_stateblock *stateblock;
3228 HRESULT hr;
3230 TRACE("device %p.\n", device);
3232 if (device->recording)
3233 return WINED3DERR_INVALIDCALL;
3235 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3236 if (FAILED(hr))
3237 return hr;
3239 device->recording = stateblock;
3240 device->update_state = &stateblock->state;
3242 TRACE("Recording stateblock %p.\n", stateblock);
3244 return WINED3D_OK;
3247 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3248 struct wined3d_stateblock **stateblock)
3250 struct wined3d_stateblock *object = device->recording;
3252 TRACE("device %p, stateblock %p.\n", device, stateblock);
3254 if (!device->recording)
3256 WARN("Not recording.\n");
3257 *stateblock = NULL;
3258 return WINED3DERR_INVALIDCALL;
3261 stateblock_init_contained_states(object);
3263 *stateblock = object;
3264 device->recording = NULL;
3265 device->update_state = &device->state;
3267 TRACE("Returning stateblock %p.\n", *stateblock);
3269 return WINED3D_OK;
3272 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3274 /* At the moment we have no need for any functionality at the beginning
3275 * of a scene. */
3276 TRACE("device %p.\n", device);
3278 if (device->inScene)
3280 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3281 return WINED3DERR_INVALIDCALL;
3283 device->inScene = TRUE;
3284 return WINED3D_OK;
3287 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3289 struct wined3d_context *context;
3291 TRACE("device %p.\n", device);
3293 if (!device->inScene)
3295 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3296 return WINED3DERR_INVALIDCALL;
3299 context = context_acquire(device, NULL);
3300 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3301 context->gl_info->gl_ops.gl.p_glFlush();
3302 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3303 * fails. */
3304 context_release(context);
3306 device->inScene = FALSE;
3307 return WINED3D_OK;
3310 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3311 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3313 UINT i;
3315 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3316 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3317 dst_window_override, dirty_region, flags);
3319 for (i = 0; i < device->swapchain_count; ++i)
3321 wined3d_swapchain_present(device->swapchains[i], src_rect,
3322 dst_rect, dst_window_override, dirty_region, flags);
3325 return WINED3D_OK;
3328 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3329 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3331 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3332 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3334 if (!rect_count && rects)
3336 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3337 return WINED3D_OK;
3340 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3342 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3343 if (!ds)
3345 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3346 /* TODO: What about depth stencil buffers without stencil bits? */
3347 return WINED3DERR_INVALIDCALL;
3349 else if (flags & WINED3DCLEAR_TARGET)
3351 if (ds->width < device->fb.render_targets[0]->width
3352 || ds->height < device->fb.render_targets[0]->height)
3354 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3355 return WINED3D_OK;
3360 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3362 return WINED3D_OK;
3365 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3366 struct wined3d_query *predicate, BOOL value)
3368 struct wined3d_query *prev;
3370 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3372 prev = device->update_state->predicate;
3373 if (predicate)
3375 FIXME("Predicated rendering not implemented.\n");
3376 wined3d_query_incref(predicate);
3378 device->update_state->predicate = predicate;
3379 device->update_state->predicate_value = value;
3380 if (!device->recording)
3381 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3382 if (prev)
3383 wined3d_query_decref(prev);
3386 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3388 TRACE("device %p, value %p.\n", device, value);
3390 *value = device->state.predicate_value;
3391 return device->state.predicate;
3394 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3395 enum wined3d_primitive_type primitive_type)
3397 GLenum gl_primitive_type, prev;
3399 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3401 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3402 prev = device->update_state->gl_primitive_type;
3403 device->update_state->gl_primitive_type = gl_primitive_type;
3404 if (device->recording)
3405 device->recording->changed.primitive_type = TRUE;
3406 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3407 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
3410 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3411 enum wined3d_primitive_type *primitive_type)
3413 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3415 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3417 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3420 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3422 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3424 if (!device->state.vertex_declaration)
3426 WARN("Called without a valid vertex declaration set.\n");
3427 return WINED3DERR_INVALIDCALL;
3430 if (device->state.load_base_vertex_index)
3432 device->state.load_base_vertex_index = 0;
3433 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3436 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3438 return WINED3D_OK;
3441 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3442 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3444 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3445 device, start_vertex, vertex_count, start_instance, instance_count);
3447 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3450 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3452 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3454 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3456 if (!device->state.index_buffer)
3458 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3459 * without an index buffer set. (The first time at least...)
3460 * D3D8 simply dies, but I doubt it can do much harm to return
3461 * D3DERR_INVALIDCALL there as well. */
3462 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3463 return WINED3DERR_INVALIDCALL;
3466 if (!device->state.vertex_declaration)
3468 WARN("Called without a valid vertex declaration set.\n");
3469 return WINED3DERR_INVALIDCALL;
3472 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3473 device->state.load_base_vertex_index != device->state.base_vertex_index)
3475 device->state.load_base_vertex_index = device->state.base_vertex_index;
3476 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3479 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3481 return WINED3D_OK;
3484 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3485 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3487 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3488 device, start_idx, index_count, start_instance, instance_count);
3490 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3493 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3494 static HRESULT device_update_volume(struct wined3d_device *device,
3495 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3497 struct wined3d_const_bo_address data;
3498 struct wined3d_map_desc src;
3499 HRESULT hr;
3500 struct wined3d_context *context;
3502 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3503 device, src_volume, dst_volume);
3505 if (src_volume->resource.format != dst_volume->resource.format)
3507 FIXME("Source and destination formats do not match.\n");
3508 return WINED3DERR_INVALIDCALL;
3510 if (src_volume->resource.width != dst_volume->resource.width
3511 || src_volume->resource.height != dst_volume->resource.height
3512 || src_volume->resource.depth != dst_volume->resource.depth)
3514 FIXME("Source and destination sizes do not match.\n");
3515 return WINED3DERR_INVALIDCALL;
3518 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3519 return hr;
3521 context = context_acquire(device, NULL);
3523 /* Only a prepare, since we're uploading the entire volume. */
3524 wined3d_texture_prepare_texture(dst_volume->container, context, FALSE);
3525 wined3d_texture_bind_and_dirtify(dst_volume->container, context, FALSE);
3527 data.buffer_object = 0;
3528 data.addr = src.data;
3529 wined3d_volume_upload_data(dst_volume, context, &data);
3530 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3532 context_release(context);
3534 hr = wined3d_volume_unmap(src_volume);
3536 return hr;
3539 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3540 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3542 enum wined3d_resource_type type;
3543 unsigned int level_count, i, j, src_size, dst_size, src_skip_levels = 0;
3544 HRESULT hr;
3545 struct wined3d_context *context;
3547 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3549 /* Verify that the source and destination textures are non-NULL. */
3550 if (!src_texture || !dst_texture)
3552 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3553 return WINED3DERR_INVALIDCALL;
3556 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3558 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3559 return WINED3DERR_INVALIDCALL;
3561 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3563 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3564 return WINED3DERR_INVALIDCALL;
3567 /* Verify that the source and destination textures are the same type. */
3568 type = src_texture->resource.type;
3569 if (dst_texture->resource.type != type)
3571 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3572 return WINED3DERR_INVALIDCALL;
3575 level_count = min(wined3d_texture_get_level_count(src_texture),
3576 wined3d_texture_get_level_count(dst_texture));
3578 src_size = max(src_texture->resource.width, src_texture->resource.height);
3579 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3580 if (type == WINED3D_RTYPE_VOLUME)
3582 src_size = max(src_size, src_texture->resource.depth);
3583 dst_size = max(dst_size, dst_texture->resource.depth);
3585 while (src_size > dst_size)
3587 src_size >>= 1;
3588 ++src_skip_levels;
3591 /* Make sure that the destination texture is loaded. */
3592 context = context_acquire(device, NULL);
3593 wined3d_texture_load(dst_texture, context, FALSE);
3594 context_release(context);
3596 /* Update every surface level of the texture. */
3597 switch (type)
3599 case WINED3D_RTYPE_TEXTURE:
3601 struct wined3d_surface *src_surface;
3602 struct wined3d_surface *dst_surface;
3604 for (i = 0; i < level_count; ++i)
3606 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture,
3607 i + src_skip_levels));
3608 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3609 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3610 if (FAILED(hr))
3612 WARN("Failed to update surface, hr %#x.\n", hr);
3613 return hr;
3616 break;
3619 case WINED3D_RTYPE_CUBE_TEXTURE:
3621 struct wined3d_surface *src_surface;
3622 struct wined3d_surface *dst_surface;
3623 unsigned int src_levels = wined3d_texture_get_level_count(src_texture);
3624 unsigned int dst_levels = wined3d_texture_get_level_count(dst_texture);
3626 for (i = 0; i < 6; ++i)
3628 for (j = 0; j < level_count; ++j)
3630 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture,
3631 i * src_levels + j + src_skip_levels));
3632 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture,
3633 i * dst_levels + j));
3634 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3635 if (FAILED(hr))
3637 WARN("Failed to update surface, hr %#x.\n", hr);
3638 return hr;
3642 break;
3645 case WINED3D_RTYPE_VOLUME_TEXTURE:
3647 for (i = 0; i < level_count; ++i)
3649 hr = device_update_volume(device,
3650 volume_from_resource(wined3d_texture_get_sub_resource(src_texture,
3651 i + src_skip_levels)),
3652 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3653 if (FAILED(hr))
3655 WARN("Failed to update volume, hr %#x.\n", hr);
3656 return hr;
3659 break;
3662 default:
3663 FIXME("Unsupported texture type %#x.\n", type);
3664 return WINED3DERR_INVALIDCALL;
3667 return WINED3D_OK;
3670 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3671 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3673 struct wined3d_swapchain *swapchain;
3675 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3677 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3678 return WINED3DERR_INVALIDCALL;
3680 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3683 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3685 const struct wined3d_state *state = &device->state;
3686 struct wined3d_texture *texture;
3687 DWORD i;
3689 TRACE("device %p, num_passes %p.\n", device, num_passes);
3691 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3693 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3695 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3696 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3698 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3700 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3701 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3704 texture = state->textures[i];
3705 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3707 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3709 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3710 return E_FAIL;
3712 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3714 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3715 return E_FAIL;
3717 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3718 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3720 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3721 return E_FAIL;
3725 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3726 || state->render_states[WINED3D_RS_STENCILENABLE])
3728 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3729 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3731 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3733 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3734 return WINED3DERR_CONFLICTINGRENDERSTATE;
3738 /* return a sensible default */
3739 *num_passes = 1;
3741 TRACE("returning D3D_OK\n");
3742 return WINED3D_OK;
3745 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3747 static BOOL warned;
3749 TRACE("device %p, software %#x.\n", device, software);
3751 if (!warned)
3753 FIXME("device %p, software %#x stub!\n", device, software);
3754 warned = TRUE;
3757 device->softwareVertexProcessing = software;
3760 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3762 static BOOL warned;
3764 TRACE("device %p.\n", device);
3766 if (!warned)
3768 TRACE("device %p stub!\n", device);
3769 warned = TRUE;
3772 return device->softwareVertexProcessing;
3775 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3776 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3778 struct wined3d_swapchain *swapchain;
3780 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3781 device, swapchain_idx, raster_status);
3783 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3784 return WINED3DERR_INVALIDCALL;
3786 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3789 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3791 static BOOL warned;
3793 TRACE("device %p, segments %.8e.\n", device, segments);
3795 if (segments != 0.0f)
3797 if (!warned)
3799 FIXME("device %p, segments %.8e stub!\n", device, segments);
3800 warned = TRUE;
3804 return WINED3D_OK;
3807 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3809 static BOOL warned;
3811 TRACE("device %p.\n", device);
3813 if (!warned)
3815 FIXME("device %p stub!\n", device);
3816 warned = TRUE;
3819 return 0.0f;
3822 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3823 struct wined3d_surface *src_surface, const RECT *src_rect,
3824 struct wined3d_surface *dst_surface, const POINT *dst_point)
3826 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3827 device, src_surface, wine_dbgstr_rect(src_rect),
3828 dst_surface, wine_dbgstr_point(dst_point));
3830 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3832 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3833 src_surface, dst_surface);
3834 return WINED3DERR_INVALIDCALL;
3837 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3840 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3841 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3843 struct wined3d_surface *dst_surface, *src_surface;
3844 struct wined3d_texture *dst_texture, *src_texture;
3845 unsigned int i, count;
3846 HRESULT hr;
3848 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3850 if (src_resource == dst_resource)
3852 WARN("Source and destination are the same resource.\n");
3853 return;
3856 if (src_resource->type != dst_resource->type)
3858 WARN("Resource types (%s / %s) don't match.\n",
3859 debug_d3dresourcetype(dst_resource->type),
3860 debug_d3dresourcetype(src_resource->type));
3861 return;
3864 if (src_resource->width != dst_resource->width
3865 || src_resource->height != dst_resource->height
3866 || src_resource->depth != dst_resource->depth)
3868 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3869 dst_resource->width, dst_resource->height, dst_resource->depth,
3870 src_resource->width, src_resource->height, src_resource->depth);
3871 return;
3874 if (src_resource->format->id != dst_resource->format->id)
3876 WARN("Resource formats (%s / %s) don't match.\n",
3877 debug_d3dformat(dst_resource->format->id),
3878 debug_d3dformat(src_resource->format->id));
3879 return;
3882 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3884 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3885 return;
3888 dst_texture = wined3d_texture_from_resource(dst_resource);
3889 src_texture = wined3d_texture_from_resource(src_resource);
3891 if (src_texture->layer_count != dst_texture->layer_count
3892 || src_texture->level_count != dst_texture->level_count)
3894 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3895 dst_texture->layer_count, dst_texture->level_count,
3896 src_texture->layer_count, src_texture->level_count);
3897 return;
3900 count = dst_texture->layer_count * dst_texture->level_count;
3901 for (i = 0; i < count; ++i)
3903 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3904 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3906 if (FAILED(hr = wined3d_surface_blt(dst_surface, NULL, src_surface, NULL, 0, NULL, WINED3D_TEXF_POINT)))
3907 ERR("Failed to blit, subresource %u, hr %#x.\n", i, hr);
3911 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
3912 struct wined3d_rendertarget_view *view, const RECT *rect, const struct wined3d_color *color)
3914 struct wined3d_resource *resource;
3915 RECT r;
3917 TRACE("device %p, view %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
3918 device, view, wine_dbgstr_rect(rect), color->r, color->g, color->b, color->a);
3920 resource = view->resource;
3921 if (resource->type != WINED3D_RTYPE_TEXTURE && resource->type != WINED3D_RTYPE_CUBE_TEXTURE)
3923 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
3924 return WINED3DERR_INVALIDCALL;
3927 if (view->depth > 1)
3929 FIXME("Layered clears not implemented.\n");
3930 return WINED3DERR_INVALIDCALL;
3933 if (!rect)
3935 SetRect(&r, 0, 0, view->width, view->height);
3936 rect = &r;
3939 resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource), view->sub_resource_idx);
3941 return surface_color_fill(surface_from_resource(resource), rect, color);
3944 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
3945 unsigned int view_idx)
3947 TRACE("device %p, view_idx %u.\n", device, view_idx);
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 NULL;
3955 return device->fb.render_targets[view_idx];
3958 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
3960 TRACE("device %p.\n", device);
3962 return device->fb.depth_stencil;
3965 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
3966 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
3968 struct wined3d_rendertarget_view *prev;
3970 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
3971 device, view_idx, view, set_viewport);
3973 if (view_idx >= device->adapter->gl_info.limits.buffers)
3975 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3976 return WINED3DERR_INVALIDCALL;
3979 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
3981 WARN("View resource %p doesn't have render target usage.\n", view->resource);
3982 return WINED3DERR_INVALIDCALL;
3985 /* Set the viewport and scissor rectangles, if requested. Tests show that
3986 * stateblock recording is ignored, the change goes directly into the
3987 * primary stateblock. */
3988 if (!view_idx && set_viewport)
3990 struct wined3d_state *state = &device->state;
3992 state->viewport.x = 0;
3993 state->viewport.y = 0;
3994 state->viewport.width = view->width;
3995 state->viewport.height = view->height;
3996 state->viewport.min_z = 0.0f;
3997 state->viewport.max_z = 1.0f;
3998 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4000 state->scissor_rect.top = 0;
4001 state->scissor_rect.left = 0;
4002 state->scissor_rect.right = view->width;
4003 state->scissor_rect.bottom = view->height;
4004 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4008 prev = device->fb.render_targets[view_idx];
4009 if (view == prev)
4010 return WINED3D_OK;
4012 if (view)
4013 wined3d_rendertarget_view_incref(view);
4014 device->fb.render_targets[view_idx] = view;
4015 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4016 /* Release after the assignment, to prevent device_resource_released()
4017 * from seeing the surface as still in use. */
4018 if (prev)
4019 wined3d_rendertarget_view_decref(prev);
4021 return WINED3D_OK;
4024 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4026 struct wined3d_rendertarget_view *prev;
4028 TRACE("device %p, view %p.\n", device, view);
4030 prev = device->fb.depth_stencil;
4031 if (prev == view)
4033 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4034 return;
4037 if ((device->fb.depth_stencil = view))
4038 wined3d_rendertarget_view_incref(view);
4039 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4040 if (prev)
4041 wined3d_rendertarget_view_decref(prev);
4044 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4045 struct wined3d_surface *cursor_image)
4047 struct wined3d_sub_resource_data data;
4048 struct wined3d_resource_desc desc;
4049 struct wined3d_map_desc map_desc;
4050 struct wined3d_texture *texture;
4051 HRESULT hr;
4053 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4055 ERR("Failed to map source surface.\n");
4056 return NULL;
4059 data.data = map_desc.data;
4060 data.row_pitch = map_desc.row_pitch;
4061 data.slice_pitch = map_desc.slice_pitch;
4063 desc.resource_type = WINED3D_RTYPE_TEXTURE;
4064 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4065 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4066 desc.multisample_quality = 0;
4067 desc.usage = WINED3DUSAGE_DYNAMIC;
4068 desc.pool = WINED3D_POOL_DEFAULT;
4069 desc.width = cursor_image->resource.width;
4070 desc.height = cursor_image->resource.height;
4071 desc.depth = 1;
4072 desc.size = 0;
4074 hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
4075 &data, NULL, &wined3d_null_parent_ops, &texture);
4076 wined3d_surface_unmap(cursor_image);
4077 if (FAILED(hr))
4079 ERR("Failed to create cursor texture.\n");
4080 return NULL;
4083 return texture;
4086 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4087 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4089 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4090 device, x_hotspot, y_hotspot, cursor_image);
4092 if (device->cursor_texture)
4094 wined3d_texture_decref(device->cursor_texture);
4095 device->cursor_texture = NULL;
4098 if (cursor_image)
4100 struct wined3d_display_mode mode;
4101 struct wined3d_map_desc map_desc;
4102 HRESULT hr;
4104 /* MSDN: Cursor must be A8R8G8B8 */
4105 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4107 WARN("surface %p has an invalid format.\n", cursor_image);
4108 return WINED3DERR_INVALIDCALL;
4111 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4113 ERR("Failed to get display mode, hr %#x.\n", hr);
4114 return WINED3DERR_INVALIDCALL;
4117 /* MSDN: Cursor must be smaller than the display mode */
4118 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4120 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4121 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4122 mode.width, mode.height);
4123 return WINED3DERR_INVALIDCALL;
4126 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4128 /* Do not store the surface's pointer because the application may
4129 * release it after setting the cursor image. Windows doesn't
4130 * addref the set surface, so we can't do this either without
4131 * creating circular refcount dependencies. */
4132 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
4134 ERR("Failed to create cursor texture.\n");
4135 return WINED3DERR_INVALIDCALL;
4138 device->cursorWidth = cursor_image->resource.width;
4139 device->cursorHeight = cursor_image->resource.height;
4141 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4143 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4144 ICONINFO cursorInfo;
4145 DWORD *maskBits;
4146 HCURSOR cursor;
4148 /* 32-bit user32 cursors ignore the alpha channel if it's all
4149 * zeroes, and use the mask instead. Fill the mask with all ones
4150 * to ensure we still get a fully transparent cursor. */
4151 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4152 memset(maskBits, 0xff, mask_size);
4153 wined3d_surface_map(cursor_image, &map_desc, NULL,
4154 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4155 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4157 cursorInfo.fIcon = FALSE;
4158 cursorInfo.xHotspot = x_hotspot;
4159 cursorInfo.yHotspot = y_hotspot;
4160 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4161 1, 1, maskBits);
4162 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4163 1, 32, map_desc.data);
4164 wined3d_surface_unmap(cursor_image);
4165 /* Create our cursor and clean up. */
4166 cursor = CreateIconIndirect(&cursorInfo);
4167 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4168 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4169 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4170 device->hardwareCursor = cursor;
4171 if (device->bCursorVisible) SetCursor( cursor );
4172 HeapFree(GetProcessHeap(), 0, maskBits);
4176 device->xHotSpot = x_hotspot;
4177 device->yHotSpot = y_hotspot;
4178 return WINED3D_OK;
4181 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4182 int x_screen_space, int y_screen_space, DWORD flags)
4184 TRACE("device %p, x %d, y %d, flags %#x.\n",
4185 device, x_screen_space, y_screen_space, flags);
4187 device->xScreenSpace = x_screen_space;
4188 device->yScreenSpace = y_screen_space;
4190 if (device->hardwareCursor)
4192 POINT pt;
4194 GetCursorPos( &pt );
4195 if (x_screen_space == pt.x && y_screen_space == pt.y)
4196 return;
4197 SetCursorPos( x_screen_space, y_screen_space );
4199 /* Switch to the software cursor if position diverges from the hardware one. */
4200 GetCursorPos( &pt );
4201 if (x_screen_space != pt.x || y_screen_space != pt.y)
4203 if (device->bCursorVisible) SetCursor( NULL );
4204 DestroyCursor( device->hardwareCursor );
4205 device->hardwareCursor = 0;
4210 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4212 BOOL oldVisible = device->bCursorVisible;
4214 TRACE("device %p, show %#x.\n", device, show);
4217 * When ShowCursor is first called it should make the cursor appear at the OS's last
4218 * known cursor position.
4220 if (show && !oldVisible)
4222 POINT pt;
4223 GetCursorPos(&pt);
4224 device->xScreenSpace = pt.x;
4225 device->yScreenSpace = pt.y;
4228 if (device->hardwareCursor)
4230 device->bCursorVisible = show;
4231 if (show)
4232 SetCursor(device->hardwareCursor);
4233 else
4234 SetCursor(NULL);
4236 else if (device->cursor_texture)
4238 device->bCursorVisible = show;
4241 return oldVisible;
4244 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4246 struct wined3d_resource *resource, *cursor;
4248 TRACE("device %p.\n", device);
4250 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4252 TRACE("Checking resource %p for eviction.\n", resource);
4254 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4256 TRACE("Evicting %p.\n", resource);
4257 resource->resource_ops->resource_unload(resource);
4261 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4262 device_invalidate_state(device, STATE_STREAMSRC);
4265 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4267 struct wined3d_resource *resource, *cursor;
4268 const struct wined3d_gl_info *gl_info;
4269 struct wined3d_context *context;
4270 struct wined3d_shader *shader;
4272 context = context_acquire(device, NULL);
4273 gl_info = context->gl_info;
4275 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4277 TRACE("Unloading resource %p.\n", resource);
4279 resource->resource_ops->resource_unload(resource);
4282 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4284 device->shader_backend->shader_destroy(shader);
4287 if (device->depth_blt_texture)
4289 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4290 device->depth_blt_texture = 0;
4293 device->blitter->free_private(device);
4294 device->shader_backend->shader_free_private(device);
4295 destroy_dummy_textures(device, gl_info);
4297 context_release(context);
4299 while (device->context_count)
4301 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4304 HeapFree(GetProcessHeap(), 0, swapchain->context);
4305 swapchain->context = NULL;
4308 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4310 struct wined3d_context *context;
4311 struct wined3d_surface *target;
4312 HRESULT hr;
4314 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4315 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4317 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4318 return hr;
4321 if (FAILED(hr = device->blitter->alloc_private(device)))
4323 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4324 device->shader_backend->shader_free_private(device);
4325 return hr;
4328 /* Recreate the primary swapchain's context */
4329 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4330 if (!swapchain->context)
4332 ERR("Failed to allocate memory for swapchain context array.\n");
4333 device->blitter->free_private(device);
4334 device->shader_backend->shader_free_private(device);
4335 return E_OUTOFMEMORY;
4338 target = swapchain->back_buffers
4339 ? surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0))
4340 : surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
4341 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4343 WARN("Failed to create context.\n");
4344 device->blitter->free_private(device);
4345 device->shader_backend->shader_free_private(device);
4346 HeapFree(GetProcessHeap(), 0, swapchain->context);
4347 return E_FAIL;
4350 swapchain->context[0] = context;
4351 swapchain->num_contexts = 1;
4352 create_dummy_textures(device, context);
4353 context_release(context);
4355 return WINED3D_OK;
4358 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4359 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4360 wined3d_device_reset_cb callback, BOOL reset_state)
4362 enum wined3d_format_id backbuffer_format = swapchain_desc->backbuffer_format;
4363 struct wined3d_resource *resource, *cursor;
4364 struct wined3d_swapchain *swapchain;
4365 struct wined3d_display_mode m;
4366 BOOL DisplayModeChanged;
4367 BOOL update_desc = FALSE;
4368 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4369 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4370 HRESULT hr = WINED3D_OK;
4371 unsigned int i;
4373 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4375 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4377 ERR("Failed to get the first implicit swapchain.\n");
4378 return WINED3DERR_INVALIDCALL;
4380 DisplayModeChanged = swapchain->reapply_mode;
4382 if (reset_state)
4384 if (device->logo_texture)
4386 wined3d_texture_decref(device->logo_texture);
4387 device->logo_texture = NULL;
4389 if (device->cursor_texture)
4391 wined3d_texture_decref(device->cursor_texture);
4392 device->cursor_texture = NULL;
4394 state_unbind_resources(&device->state);
4397 if (device->fb.render_targets)
4399 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4401 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4404 wined3d_device_set_depth_stencil_view(device, NULL);
4406 if (device->onscreen_depth_stencil)
4408 wined3d_surface_decref(device->onscreen_depth_stencil);
4409 device->onscreen_depth_stencil = NULL;
4412 if (reset_state)
4414 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4416 TRACE("Enumerating resource %p.\n", resource);
4417 if (FAILED(hr = callback(resource)))
4418 return hr;
4422 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4423 * on an existing gl context, so there's no real need for recreation.
4425 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4427 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4429 TRACE("New params:\n");
4430 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4431 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4432 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4433 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4434 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4435 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4436 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4437 TRACE("device_window %p\n", swapchain_desc->device_window);
4438 TRACE("windowed %#x\n", swapchain_desc->windowed);
4439 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4440 if (swapchain_desc->enable_auto_depth_stencil)
4441 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4442 TRACE("flags %#x\n", swapchain_desc->flags);
4443 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4444 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4445 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4447 /* No special treatment of these parameters. Just store them */
4448 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4449 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4450 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4451 swapchain->desc.flags = swapchain_desc->flags;
4452 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4453 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4454 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4456 /* What to do about these? */
4457 if (swapchain_desc->backbuffer_count
4458 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4459 FIXME("Cannot change the back buffer count yet.\n");
4461 if (swapchain_desc->device_window
4462 && swapchain_desc->device_window != swapchain->desc.device_window)
4464 TRACE("Changing the device window from %p to %p.\n",
4465 swapchain->desc.device_window, swapchain_desc->device_window);
4466 swapchain->desc.device_window = swapchain_desc->device_window;
4467 swapchain->device_window = swapchain_desc->device_window;
4468 wined3d_swapchain_set_window(swapchain, NULL);
4471 if (mode)
4473 DisplayModeChanged = TRUE;
4474 m = *mode;
4476 else if (swapchain_desc->windowed)
4478 m = swapchain->original_mode;
4480 else
4482 m.width = swapchain_desc->backbuffer_width;
4483 m.height = swapchain_desc->backbuffer_height;
4484 m.refresh_rate = swapchain_desc->refresh_rate;
4485 m.format_id = swapchain_desc->backbuffer_format;
4486 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4489 if (!backbuffer_width || !backbuffer_height)
4491 /* The application is requesting that either the swapchain width or
4492 * height be set to the corresponding dimension in the window's
4493 * client rect. */
4495 RECT client_rect;
4497 if (!swapchain_desc->windowed)
4498 return WINED3DERR_INVALIDCALL;
4500 if (!GetClientRect(swapchain->device_window, &client_rect))
4502 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
4503 return WINED3DERR_INVALIDCALL;
4506 if (!backbuffer_width)
4507 backbuffer_width = client_rect.right;
4509 if (!backbuffer_height)
4510 backbuffer_height = client_rect.bottom;
4513 if (backbuffer_width != swapchain->desc.backbuffer_width
4514 || backbuffer_height != swapchain->desc.backbuffer_height)
4516 if (!swapchain_desc->windowed)
4517 DisplayModeChanged = TRUE;
4519 swapchain->desc.backbuffer_width = backbuffer_width;
4520 swapchain->desc.backbuffer_height = backbuffer_height;
4521 update_desc = TRUE;
4524 if (backbuffer_format == WINED3DFMT_UNKNOWN)
4526 if (!swapchain_desc->windowed)
4527 return WINED3DERR_INVALIDCALL;
4528 backbuffer_format = swapchain->original_mode.format_id;
4531 if (backbuffer_format != swapchain->desc.backbuffer_format)
4533 swapchain->desc.backbuffer_format = backbuffer_format;
4534 update_desc = TRUE;
4537 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
4538 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
4540 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
4541 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
4542 update_desc = TRUE;
4545 if (update_desc)
4547 UINT i;
4549 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
4550 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4551 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4552 return hr;
4554 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4556 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
4557 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4558 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4559 return hr;
4563 if (device->auto_depth_stencil_view)
4565 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4566 device->auto_depth_stencil_view = NULL;
4568 if (swapchain->desc.enable_auto_depth_stencil)
4570 struct wined3d_resource_desc surface_desc;
4571 struct wined3d_surface *surface;
4573 TRACE("Creating the depth stencil buffer\n");
4575 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
4576 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
4577 surface_desc.multisample_type = swapchain->desc.multisample_type;
4578 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
4579 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4580 surface_desc.pool = WINED3D_POOL_DEFAULT;
4581 surface_desc.width = swapchain->desc.backbuffer_width;
4582 surface_desc.height = swapchain->desc.backbuffer_height;
4583 surface_desc.depth = 1;
4584 surface_desc.size = 0;
4586 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4587 device->device_parent, &surface_desc, &surface)))
4589 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4590 return WINED3DERR_INVALIDCALL;
4593 hr = wined3d_rendertarget_view_create_from_surface(surface,
4594 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4595 wined3d_surface_decref(surface);
4596 if (FAILED(hr))
4598 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4599 return hr;
4602 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4605 if (device->back_buffer_view)
4607 wined3d_rendertarget_view_decref(device->back_buffer_view);
4608 device->back_buffer_view = NULL;
4610 if (swapchain->desc.backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
4611 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
4612 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4614 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4615 return hr;
4618 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4619 || DisplayModeChanged)
4621 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4623 WARN("Failed to set display mode, hr %#x.\n", hr);
4624 return WINED3DERR_INVALIDCALL;
4627 if (!swapchain_desc->windowed)
4629 if (swapchain->desc.windowed)
4631 HWND focus_window = device->create_parms.focus_window;
4632 if (!focus_window)
4633 focus_window = swapchain_desc->device_window;
4634 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4636 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4637 return hr;
4640 /* switch from windowed to fs */
4641 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4642 swapchain_desc->backbuffer_width,
4643 swapchain_desc->backbuffer_height);
4645 else
4647 /* Fullscreen -> fullscreen mode change */
4648 MoveWindow(swapchain->device_window, 0, 0,
4649 swapchain_desc->backbuffer_width,
4650 swapchain_desc->backbuffer_height,
4651 TRUE);
4653 swapchain->d3d_mode = m;
4655 else if (!swapchain->desc.windowed)
4657 /* Fullscreen -> windowed switch */
4658 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4659 wined3d_device_release_focus_window(device);
4661 swapchain->desc.windowed = swapchain_desc->windowed;
4663 else if (!swapchain_desc->windowed)
4665 DWORD style = device->style;
4666 DWORD exStyle = device->exStyle;
4667 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4668 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4669 * Reset to clear up their mess. Guild Wars also loses the device during that.
4671 device->style = 0;
4672 device->exStyle = 0;
4673 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4674 swapchain_desc->backbuffer_width,
4675 swapchain_desc->backbuffer_height);
4676 device->style = style;
4677 device->exStyle = exStyle;
4680 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4682 if (reset_state)
4684 TRACE("Resetting stateblock.\n");
4685 if (device->recording)
4687 wined3d_stateblock_decref(device->recording);
4688 device->recording = NULL;
4690 wined3d_cs_emit_reset_state(device->cs);
4691 state_cleanup(&device->state);
4693 if (device->d3d_initialized)
4694 delete_opengl_contexts(device, swapchain);
4696 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4697 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4698 ERR("Failed to initialize device state, hr %#x.\n", hr);
4699 device->update_state = &device->state;
4701 device_init_swapchain_state(device, swapchain);
4703 else if (device->back_buffer_view)
4705 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4706 struct wined3d_state *state = &device->state;
4708 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4710 /* Note the min_z / max_z is not reset. */
4711 state->viewport.x = 0;
4712 state->viewport.y = 0;
4713 state->viewport.width = view->width;
4714 state->viewport.height = view->height;
4715 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4717 state->scissor_rect.top = 0;
4718 state->scissor_rect.left = 0;
4719 state->scissor_rect.right = view->width;
4720 state->scissor_rect.bottom = view->height;
4721 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4724 swapchain_update_render_to_fbo(swapchain);
4725 swapchain_update_draw_bindings(swapchain);
4727 if (reset_state && device->d3d_initialized)
4728 hr = create_primary_opengl_context(device, swapchain);
4730 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4731 * first use
4733 return hr;
4736 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4738 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4740 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4742 return WINED3D_OK;
4746 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4747 struct wined3d_device_creation_parameters *parameters)
4749 TRACE("device %p, parameters %p.\n", device, parameters);
4751 *parameters = device->create_parms;
4754 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4755 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4757 struct wined3d_swapchain *swapchain;
4759 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4760 device, swapchain_idx, flags, ramp);
4762 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4763 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4766 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4767 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4769 struct wined3d_swapchain *swapchain;
4771 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4772 device, swapchain_idx, ramp);
4774 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4775 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4778 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4780 TRACE("device %p, resource %p.\n", device, resource);
4782 list_add_head(&device->resources, &resource->resource_list_entry);
4785 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4787 TRACE("device %p, resource %p.\n", device, resource);
4789 list_remove(&resource->resource_list_entry);
4792 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4794 enum wined3d_resource_type type = resource->type;
4795 unsigned int i;
4797 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4799 context_resource_released(device, resource, type);
4801 switch (type)
4803 case WINED3D_RTYPE_SURFACE:
4805 struct wined3d_surface *surface = surface_from_resource(resource);
4807 if (!device->d3d_initialized) break;
4809 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4811 if (wined3d_rendertarget_view_get_surface(device->fb.render_targets[i]) == surface)
4813 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4814 device->fb.render_targets[i] = NULL;
4818 if (wined3d_rendertarget_view_get_surface(device->fb.depth_stencil) == surface)
4820 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4821 device->fb.depth_stencil = NULL;
4824 break;
4826 case WINED3D_RTYPE_TEXTURE:
4827 case WINED3D_RTYPE_CUBE_TEXTURE:
4828 case WINED3D_RTYPE_VOLUME_TEXTURE:
4829 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4831 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4833 if (device->state.textures[i] == texture)
4835 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4836 device->state.textures[i] = NULL;
4839 if (device->recording && device->update_state->textures[i] == texture)
4841 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4842 texture, device->recording, i);
4843 device->update_state->textures[i] = NULL;
4846 break;
4848 case WINED3D_RTYPE_BUFFER:
4850 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4852 for (i = 0; i < MAX_STREAMS; ++i)
4854 if (device->state.streams[i].buffer == buffer)
4856 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4857 device->state.streams[i].buffer = NULL;
4860 if (device->recording && device->update_state->streams[i].buffer == buffer)
4862 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4863 buffer, device->recording, i);
4864 device->update_state->streams[i].buffer = NULL;
4868 if (device->state.index_buffer == buffer)
4870 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4871 device->state.index_buffer = NULL;
4874 if (device->recording && device->update_state->index_buffer == buffer)
4876 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4877 buffer, device->recording);
4878 device->update_state->index_buffer = NULL;
4881 break;
4883 default:
4884 break;
4887 /* Remove the resource from the resourceStore */
4888 device_resource_remove(device, resource);
4890 TRACE("Resource released.\n");
4893 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4895 struct wined3d_resource *resource;
4897 TRACE("device %p, dc %p.\n", device, dc);
4899 if (!dc)
4900 return NULL;
4902 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4904 if (resource->type == WINED3D_RTYPE_SURFACE)
4906 struct wined3d_surface *s = surface_from_resource(resource);
4908 if (s->hDC == dc)
4910 TRACE("Found surface %p for dc %p.\n", s, dc);
4911 return s;
4916 return NULL;
4919 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4921 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4923 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4926 static const struct wine_rb_functions wined3d_sampler_rb_functions =
4928 wined3d_rb_alloc,
4929 wined3d_rb_realloc,
4930 wined3d_rb_free,
4931 wined3d_sampler_compare,
4934 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4935 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4936 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4938 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4939 const struct fragment_pipeline *fragment_pipeline;
4940 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4941 unsigned int i;
4942 HRESULT hr;
4944 device->ref = 1;
4945 device->wined3d = wined3d;
4946 wined3d_incref(device->wined3d);
4947 device->adapter = wined3d->adapter_count ? adapter : NULL;
4948 device->device_parent = device_parent;
4949 list_init(&device->resources);
4950 list_init(&device->shaders);
4951 device->surface_alignment = surface_alignment;
4953 /* Save the creation parameters. */
4954 device->create_parms.adapter_idx = adapter_idx;
4955 device->create_parms.device_type = device_type;
4956 device->create_parms.focus_window = focus_window;
4957 device->create_parms.flags = flags;
4959 device->shader_backend = adapter->shader_backend;
4961 vertex_pipeline = adapter->vertex_pipe;
4963 fragment_pipeline = adapter->fragment_pipe;
4965 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
4967 ERR("Failed to initialize sampler rbtree.\n");
4968 return E_OUTOFMEMORY;
4971 if (vertex_pipeline->vp_states && fragment_pipeline->states
4972 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4973 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4974 fragment_pipeline, misc_state_template)))
4976 ERR("Failed to compile state table, hr %#x.\n", hr);
4977 wine_rb_destroy(&device->samplers, NULL, NULL);
4978 wined3d_decref(device->wined3d);
4979 return hr;
4982 device->blitter = adapter->blitter;
4984 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
4985 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4987 ERR("Failed to initialize device state, hr %#x.\n", hr);
4988 goto err;
4990 device->update_state = &device->state;
4992 if (!(device->cs = wined3d_cs_create(device)))
4994 WARN("Failed to create command stream.\n");
4995 state_cleanup(&device->state);
4996 hr = E_FAIL;
4997 goto err;
5000 return WINED3D_OK;
5002 err:
5003 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5005 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5007 wine_rb_destroy(&device->samplers, NULL, NULL);
5008 wined3d_decref(device->wined3d);
5009 return hr;
5013 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5015 DWORD rep = device->StateTable[state].representative;
5016 struct wined3d_context *context;
5017 DWORD idx;
5018 BYTE shift;
5019 UINT i;
5021 for (i = 0; i < device->context_count; ++i)
5023 context = device->contexts[i];
5024 if(isStateDirty(context, rep)) continue;
5026 context->dirtyArray[context->numDirtyEntries++] = rep;
5027 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5028 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5029 context->isStateDirty[idx] |= (1 << shift);
5033 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5034 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5036 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5038 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5039 window, message, wparam, lparam);
5040 if (unicode)
5041 return DefWindowProcW(window, message, wparam, lparam);
5042 else
5043 return DefWindowProcA(window, message, wparam, lparam);
5046 if (message == WM_DESTROY)
5048 TRACE("unregister window %p.\n", window);
5049 wined3d_unregister_window(window);
5051 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5052 ERR("Window %p is not the focus window for device %p.\n", window, device);
5054 else if (message == WM_DISPLAYCHANGE)
5056 device->device_parent->ops->mode_changed(device->device_parent);
5058 else if (message == WM_ACTIVATEAPP)
5060 UINT i;
5062 for (i = 0; i < device->swapchain_count; i++)
5063 wined3d_swapchain_activate(device->swapchains[i], wparam);
5065 device->device_parent->ops->activate(device->device_parent, wparam);
5067 else if (message == WM_SYSCOMMAND)
5069 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5071 if (unicode)
5072 DefWindowProcW(window, message, wparam, lparam);
5073 else
5074 DefWindowProcA(window, message, wparam, lparam);
5078 if (unicode)
5079 return CallWindowProcW(proc, window, message, wparam, lparam);
5080 else
5081 return CallWindowProcA(proc, window, message, wparam, lparam);