wined3d: Make sure the correct texture is bound in device_update_volume.
[wine/multimedia.git] / dlls / wined3d / device.c
blob8cab927f30a9754b0fd93f7f586d9087126ecbd1
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 buffers (with sanity checks)*/
1090 if (device->onscreen_depth_stencil)
1092 surface = device->onscreen_depth_stencil;
1093 device->onscreen_depth_stencil = NULL;
1094 wined3d_surface_decref(surface);
1097 if (device->fb.depth_stencil)
1099 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1101 TRACE("Releasing depth/stencil view %p.\n", view);
1103 device->fb.depth_stencil = NULL;
1104 wined3d_rendertarget_view_decref(view);
1107 if (device->auto_depth_stencil_view)
1109 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1111 device->auto_depth_stencil_view = NULL;
1112 if (wined3d_rendertarget_view_decref(view))
1113 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1116 for (i = 0; i < gl_info->limits.buffers; ++i)
1118 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1120 if (device->back_buffer_view)
1122 wined3d_rendertarget_view_decref(device->back_buffer_view);
1123 device->back_buffer_view = NULL;
1126 context_release(context);
1128 for (i = 0; i < device->swapchain_count; ++i)
1130 TRACE("Releasing the implicit swapchain %u.\n", i);
1131 if (wined3d_swapchain_decref(device->swapchains[i]))
1132 FIXME("Something's still holding the implicit swapchain.\n");
1135 HeapFree(GetProcessHeap(), 0, device->swapchains);
1136 device->swapchains = NULL;
1137 device->swapchain_count = 0;
1139 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1140 device->fb.render_targets = NULL;
1142 device->d3d_initialized = FALSE;
1144 return WINED3D_OK;
1147 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1149 unsigned int i;
1151 for (i = 0; i < device->swapchain_count; ++i)
1153 TRACE("Releasing the implicit swapchain %u.\n", i);
1154 if (wined3d_swapchain_decref(device->swapchains[i]))
1155 FIXME("Something's still holding the implicit swapchain.\n");
1158 HeapFree(GetProcessHeap(), 0, device->swapchains);
1159 device->swapchains = NULL;
1160 device->swapchain_count = 0;
1161 return WINED3D_OK;
1164 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1165 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1166 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1168 * There is no way to deactivate thread safety once it is enabled.
1170 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1172 TRACE("device %p.\n", device);
1174 /* For now just store the flag (needed in case of ddraw). */
1175 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1178 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1180 TRACE("device %p.\n", device);
1182 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1183 wine_dbgstr_longlong(device->adapter->vram_bytes),
1184 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1185 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1187 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1190 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1191 struct wined3d_buffer *buffer, UINT offset)
1193 struct wined3d_stream_output *stream;
1194 struct wined3d_buffer *prev_buffer;
1196 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1198 if (idx >= MAX_STREAM_OUT)
1200 WARN("Invalid stream output %u.\n", idx);
1201 return;
1204 stream = &device->update_state->stream_output[idx];
1205 prev_buffer = stream->buffer;
1207 if (buffer)
1208 wined3d_buffer_incref(buffer);
1209 stream->buffer = buffer;
1210 stream->offset = offset;
1211 if (!device->recording)
1212 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1213 if (prev_buffer)
1214 wined3d_buffer_decref(prev_buffer);
1217 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1218 UINT idx, UINT *offset)
1220 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1222 if (idx >= MAX_STREAM_OUT)
1224 WARN("Invalid stream output %u.\n", idx);
1225 return NULL;
1228 *offset = device->state.stream_output[idx].offset;
1229 return device->state.stream_output[idx].buffer;
1232 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1233 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1235 struct wined3d_stream_state *stream;
1236 struct wined3d_buffer *prev_buffer;
1238 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1239 device, stream_idx, buffer, offset, stride);
1241 if (stream_idx >= MAX_STREAMS)
1243 WARN("Stream index %u out of range.\n", stream_idx);
1244 return WINED3DERR_INVALIDCALL;
1246 else if (offset & 0x3)
1248 WARN("Offset %u is not 4 byte aligned.\n", offset);
1249 return WINED3DERR_INVALIDCALL;
1252 stream = &device->update_state->streams[stream_idx];
1253 prev_buffer = stream->buffer;
1255 if (device->recording)
1256 device->recording->changed.streamSource |= 1 << stream_idx;
1258 if (prev_buffer == buffer
1259 && stream->stride == stride
1260 && stream->offset == offset)
1262 TRACE("Application is setting the old values over, nothing to do.\n");
1263 return WINED3D_OK;
1266 stream->buffer = buffer;
1267 if (buffer)
1269 stream->stride = stride;
1270 stream->offset = offset;
1273 if (buffer)
1274 wined3d_buffer_incref(buffer);
1275 if (!device->recording)
1276 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1277 if (prev_buffer)
1278 wined3d_buffer_decref(prev_buffer);
1280 return WINED3D_OK;
1283 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1284 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1286 const struct wined3d_stream_state *stream;
1288 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1289 device, stream_idx, buffer, offset, stride);
1291 if (stream_idx >= MAX_STREAMS)
1293 WARN("Stream index %u out of range.\n", stream_idx);
1294 return WINED3DERR_INVALIDCALL;
1297 stream = &device->state.streams[stream_idx];
1298 *buffer = stream->buffer;
1299 if (offset)
1300 *offset = stream->offset;
1301 *stride = stream->stride;
1303 return WINED3D_OK;
1306 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1308 struct wined3d_stream_state *stream;
1309 UINT old_flags, old_freq;
1311 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1313 /* Verify input. At least in d3d9 this is invalid. */
1314 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1316 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1317 return WINED3DERR_INVALIDCALL;
1319 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1321 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1322 return WINED3DERR_INVALIDCALL;
1324 if (!divider)
1326 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1327 return WINED3DERR_INVALIDCALL;
1330 stream = &device->update_state->streams[stream_idx];
1331 old_flags = stream->flags;
1332 old_freq = stream->frequency;
1334 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1335 stream->frequency = divider & 0x7fffff;
1337 if (device->recording)
1338 device->recording->changed.streamFreq |= 1 << stream_idx;
1339 else if (stream->frequency != old_freq || stream->flags != old_flags)
1340 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1342 return WINED3D_OK;
1345 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1346 UINT stream_idx, UINT *divider)
1348 const struct wined3d_stream_state *stream;
1350 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1352 stream = &device->state.streams[stream_idx];
1353 *divider = stream->flags | stream->frequency;
1355 TRACE("Returning %#x.\n", *divider);
1357 return WINED3D_OK;
1360 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1361 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1363 TRACE("device %p, state %s, matrix %p.\n",
1364 device, debug_d3dtstype(d3dts), matrix);
1365 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1366 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1367 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1368 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1370 /* Handle recording of state blocks. */
1371 if (device->recording)
1373 TRACE("Recording... not performing anything.\n");
1374 device->recording->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1375 device->update_state->transforms[d3dts] = *matrix;
1376 return;
1379 /* If the new matrix is the same as the current one,
1380 * we cut off any further processing. this seems to be a reasonable
1381 * optimization because as was noticed, some apps (warcraft3 for example)
1382 * tend towards setting the same matrix repeatedly for some reason.
1384 * From here on we assume that the new matrix is different, wherever it matters. */
1385 if (!memcmp(&device->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1387 TRACE("The application is setting the same matrix over again.\n");
1388 return;
1391 device->state.transforms[d3dts] = *matrix;
1392 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1395 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1396 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1398 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1400 *matrix = device->state.transforms[state];
1403 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1404 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1406 const struct wined3d_matrix *mat;
1407 struct wined3d_matrix temp;
1409 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1411 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1412 * below means it will be recorded in a state block change, but it
1413 * works regardless where it is recorded.
1414 * If this is found to be wrong, change to StateBlock. */
1415 if (state > HIGHEST_TRANSFORMSTATE)
1417 WARN("Unhandled transform state %#x.\n", state);
1418 return;
1421 mat = &device->update_state->transforms[state];
1422 multiply_matrix(&temp, mat, matrix);
1424 /* Apply change via set transform - will reapply to eg. lights this way. */
1425 wined3d_device_set_transform(device, state, &temp);
1428 /* Note lights are real special cases. Although the device caps state only
1429 * e.g. 8 are supported, you can reference any indexes you want as long as
1430 * that number max are enabled at any one point in time. Therefore since the
1431 * indices can be anything, we need a hashmap of them. However, this causes
1432 * stateblock problems. When capturing the state block, I duplicate the
1433 * hashmap, but when recording, just build a chain pretty much of commands to
1434 * be replayed. */
1435 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1436 UINT light_idx, const struct wined3d_light *light)
1438 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1439 struct wined3d_light_info *object = NULL;
1440 struct list *e;
1441 float rho;
1443 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1445 /* Check the parameter range. Need for speed most wanted sets junk lights
1446 * which confuse the GL driver. */
1447 if (!light)
1448 return WINED3DERR_INVALIDCALL;
1450 switch (light->type)
1452 case WINED3D_LIGHT_POINT:
1453 case WINED3D_LIGHT_SPOT:
1454 case WINED3D_LIGHT_PARALLELPOINT:
1455 case WINED3D_LIGHT_GLSPOT:
1456 /* Incorrect attenuation values can cause the gl driver to crash.
1457 * Happens with Need for speed most wanted. */
1458 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1460 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1461 return WINED3DERR_INVALIDCALL;
1463 break;
1465 case WINED3D_LIGHT_DIRECTIONAL:
1466 /* Ignores attenuation */
1467 break;
1469 default:
1470 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1471 return WINED3DERR_INVALIDCALL;
1474 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1476 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1477 if (object->OriginalIndex == light_idx)
1478 break;
1479 object = NULL;
1482 if (!object)
1484 TRACE("Adding new light\n");
1485 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1486 if (!object)
1487 return E_OUTOFMEMORY;
1489 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1490 object->glIndex = -1;
1491 object->OriginalIndex = light_idx;
1494 /* Initialize the object. */
1495 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1496 light_idx, light->type,
1497 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1498 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1499 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1500 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1501 light->direction.x, light->direction.y, light->direction.z);
1502 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1503 light->range, light->falloff, light->theta, light->phi);
1505 /* Update the live definitions if the light is currently assigned a glIndex. */
1506 if (object->glIndex != -1 && !device->recording)
1508 if (object->OriginalParms.type != light->type)
1509 device_invalidate_state(device, STATE_LIGHT_TYPE);
1510 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1513 /* Save away the information. */
1514 object->OriginalParms = *light;
1516 switch (light->type)
1518 case WINED3D_LIGHT_POINT:
1519 /* Position */
1520 object->lightPosn[0] = light->position.x;
1521 object->lightPosn[1] = light->position.y;
1522 object->lightPosn[2] = light->position.z;
1523 object->lightPosn[3] = 1.0f;
1524 object->cutoff = 180.0f;
1525 /* FIXME: Range */
1526 break;
1528 case WINED3D_LIGHT_DIRECTIONAL:
1529 /* Direction */
1530 object->lightPosn[0] = -light->direction.x;
1531 object->lightPosn[1] = -light->direction.y;
1532 object->lightPosn[2] = -light->direction.z;
1533 object->lightPosn[3] = 0.0f;
1534 object->exponent = 0.0f;
1535 object->cutoff = 180.0f;
1536 break;
1538 case WINED3D_LIGHT_SPOT:
1539 /* Position */
1540 object->lightPosn[0] = light->position.x;
1541 object->lightPosn[1] = light->position.y;
1542 object->lightPosn[2] = light->position.z;
1543 object->lightPosn[3] = 1.0f;
1545 /* Direction */
1546 object->lightDirn[0] = light->direction.x;
1547 object->lightDirn[1] = light->direction.y;
1548 object->lightDirn[2] = light->direction.z;
1549 object->lightDirn[3] = 1.0f;
1551 /* opengl-ish and d3d-ish spot lights use too different models
1552 * for the light "intensity" as a function of the angle towards
1553 * the main light direction, so we only can approximate very
1554 * roughly. However, spot lights are rather rarely used in games
1555 * (if ever used at all). Furthermore if still used, probably
1556 * nobody pays attention to such details. */
1557 if (!light->falloff)
1559 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1560 * equations have the falloff resp. exponent parameter as an
1561 * exponent, so the spot light lighting will always be 1.0 for
1562 * both of them, and we don't have to care for the rest of the
1563 * rather complex calculation. */
1564 object->exponent = 0.0f;
1566 else
1568 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1569 if (rho < 0.0001f)
1570 rho = 0.0001f;
1571 object->exponent = -0.3f / logf(cosf(rho / 2));
1574 if (object->exponent > 128.0f)
1575 object->exponent = 128.0f;
1577 object->cutoff = (float)(light->phi * 90 / M_PI);
1578 /* FIXME: Range */
1579 break;
1581 default:
1582 FIXME("Unrecognized light type %#x.\n", light->type);
1585 return WINED3D_OK;
1588 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1589 UINT light_idx, struct wined3d_light *light)
1591 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1592 struct wined3d_light_info *light_info = NULL;
1593 struct list *e;
1595 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1597 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1599 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1600 if (light_info->OriginalIndex == light_idx)
1601 break;
1602 light_info = NULL;
1605 if (!light_info)
1607 TRACE("Light information requested but light not defined\n");
1608 return WINED3DERR_INVALIDCALL;
1611 *light = light_info->OriginalParms;
1612 return WINED3D_OK;
1615 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1617 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1618 struct wined3d_light_info *light_info = NULL;
1619 struct list *e;
1621 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1623 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1625 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1626 if (light_info->OriginalIndex == light_idx)
1627 break;
1628 light_info = NULL;
1630 TRACE("Found light %p.\n", light_info);
1632 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1633 if (!light_info)
1635 TRACE("Light enabled requested but light not defined, so defining one!\n");
1636 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1638 /* Search for it again! Should be fairly quick as near head of list. */
1639 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1641 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1642 if (light_info->OriginalIndex == light_idx)
1643 break;
1644 light_info = NULL;
1646 if (!light_info)
1648 FIXME("Adding default lights has failed dismally\n");
1649 return WINED3DERR_INVALIDCALL;
1653 if (!enable)
1655 if (light_info->glIndex != -1)
1657 if (!device->recording)
1659 device_invalidate_state(device, STATE_LIGHT_TYPE);
1660 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1663 device->update_state->lights[light_info->glIndex] = NULL;
1664 light_info->glIndex = -1;
1666 else
1668 TRACE("Light already disabled, nothing to do\n");
1670 light_info->enabled = FALSE;
1672 else
1674 light_info->enabled = TRUE;
1675 if (light_info->glIndex != -1)
1677 TRACE("Nothing to do as light was enabled\n");
1679 else
1681 unsigned int i;
1682 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1683 /* Find a free GL light. */
1684 for (i = 0; i < gl_info->limits.lights; ++i)
1686 if (!device->update_state->lights[i])
1688 device->update_state->lights[i] = light_info;
1689 light_info->glIndex = i;
1690 break;
1693 if (light_info->glIndex == -1)
1695 /* Our tests show that Windows returns D3D_OK in this situation, even with
1696 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1697 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1698 * as well for those lights.
1700 * TODO: Test how this affects rendering. */
1701 WARN("Too many concurrently active lights\n");
1702 return WINED3D_OK;
1705 /* i == light_info->glIndex */
1706 if (!device->recording)
1708 device_invalidate_state(device, STATE_LIGHT_TYPE);
1709 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1714 return WINED3D_OK;
1717 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1719 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1720 struct wined3d_light_info *light_info = NULL;
1721 struct list *e;
1723 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1725 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1727 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1728 if (light_info->OriginalIndex == light_idx)
1729 break;
1730 light_info = NULL;
1733 if (!light_info)
1735 TRACE("Light enabled state requested but light not defined.\n");
1736 return WINED3DERR_INVALIDCALL;
1738 /* true is 128 according to SetLightEnable */
1739 *enable = light_info->enabled ? 128 : 0;
1740 return WINED3D_OK;
1743 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1744 UINT plane_idx, const struct wined3d_vec4 *plane)
1746 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1748 /* Validate plane_idx. */
1749 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1751 TRACE("Application has requested clipplane this device doesn't support.\n");
1752 return WINED3DERR_INVALIDCALL;
1755 if (device->recording)
1756 device->recording->changed.clipplane |= 1 << plane_idx;
1758 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1760 TRACE("Application is setting old values over, nothing to do.\n");
1761 return WINED3D_OK;
1764 device->update_state->clip_planes[plane_idx] = *plane;
1766 if (!device->recording)
1767 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1769 return WINED3D_OK;
1772 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1773 UINT plane_idx, struct wined3d_vec4 *plane)
1775 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1777 /* Validate plane_idx. */
1778 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1780 TRACE("Application has requested clipplane this device doesn't support.\n");
1781 return WINED3DERR_INVALIDCALL;
1784 *plane = device->state.clip_planes[plane_idx];
1786 return WINED3D_OK;
1789 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1790 const struct wined3d_clip_status *clip_status)
1792 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1794 if (!clip_status)
1795 return WINED3DERR_INVALIDCALL;
1797 return WINED3D_OK;
1800 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1801 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 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1813 TRACE("device %p, material %p.\n", device, material);
1815 device->update_state->material = *material;
1817 if (device->recording)
1818 device->recording->changed.material = TRUE;
1819 else
1820 wined3d_cs_emit_set_material(device->cs, material);
1823 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1825 TRACE("device %p, material %p.\n", device, material);
1827 *material = device->state.material;
1829 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1830 material->diffuse.r, material->diffuse.g,
1831 material->diffuse.b, material->diffuse.a);
1832 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1833 material->ambient.r, material->ambient.g,
1834 material->ambient.b, material->ambient.a);
1835 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1836 material->specular.r, material->specular.g,
1837 material->specular.b, material->specular.a);
1838 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1839 material->emissive.r, material->emissive.g,
1840 material->emissive.b, material->emissive.a);
1841 TRACE("power %.8e.\n", material->power);
1844 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1845 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1847 enum wined3d_format_id prev_format;
1848 struct wined3d_buffer *prev_buffer;
1850 TRACE("device %p, buffer %p, format %s.\n",
1851 device, buffer, debug_d3dformat(format_id));
1853 prev_buffer = device->update_state->index_buffer;
1854 prev_format = device->update_state->index_format;
1856 device->update_state->index_buffer = buffer;
1857 device->update_state->index_format = format_id;
1859 if (device->recording)
1860 device->recording->changed.indices = TRUE;
1862 if (prev_buffer == buffer && prev_format == format_id)
1863 return;
1865 if (buffer)
1866 wined3d_buffer_incref(buffer);
1867 if (!device->recording)
1868 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1869 if (prev_buffer)
1870 wined3d_buffer_decref(prev_buffer);
1873 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1874 enum wined3d_format_id *format)
1876 TRACE("device %p, format %p.\n", device, format);
1878 *format = device->state.index_format;
1879 return device->state.index_buffer;
1882 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1884 TRACE("device %p, base_index %d.\n", device, base_index);
1886 device->update_state->base_vertex_index = base_index;
1889 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1891 TRACE("device %p.\n", device);
1893 return device->state.base_vertex_index;
1896 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1898 TRACE("device %p, viewport %p.\n", device, viewport);
1899 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1900 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1902 device->update_state->viewport = *viewport;
1904 /* Handle recording of state blocks */
1905 if (device->recording)
1907 TRACE("Recording... not performing anything\n");
1908 device->recording->changed.viewport = TRUE;
1909 return;
1912 wined3d_cs_emit_set_viewport(device->cs, viewport);
1915 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1917 TRACE("device %p, viewport %p.\n", device, viewport);
1919 *viewport = device->state.viewport;
1922 static void resolve_depth_buffer(struct wined3d_state *state)
1924 struct wined3d_texture *texture = state->textures[0];
1925 struct wined3d_surface *depth_stencil, *surface;
1927 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1928 || !(texture->resource.format->flags & WINED3DFMT_FLAG_DEPTH))
1929 return;
1930 surface = surface_from_resource(texture->sub_resources[0]);
1931 if (!(depth_stencil = wined3d_rendertarget_view_get_surface(state->fb->depth_stencil)))
1932 return;
1934 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1937 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1938 enum wined3d_render_state state, DWORD value)
1940 DWORD old_value = device->state.render_states[state];
1942 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1944 device->update_state->render_states[state] = value;
1946 /* Handle recording of state blocks. */
1947 if (device->recording)
1949 TRACE("Recording... not performing anything.\n");
1950 device->recording->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
1951 return;
1954 /* Compared here and not before the assignment to allow proper stateblock recording. */
1955 if (value == old_value)
1956 TRACE("Application is setting the old value over, nothing to do.\n");
1957 else
1958 wined3d_cs_emit_set_render_state(device->cs, state, value);
1960 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1962 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1963 resolve_depth_buffer(&device->state);
1967 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1969 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1971 return device->state.render_states[state];
1974 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1975 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1977 DWORD old_value;
1979 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1980 device, sampler_idx, debug_d3dsamplerstate(state), value);
1982 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1983 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1985 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1987 WARN("Invalid sampler %u.\n", sampler_idx);
1988 return; /* Windows accepts overflowing this array ... we do not. */
1991 old_value = device->state.sampler_states[sampler_idx][state];
1992 device->update_state->sampler_states[sampler_idx][state] = value;
1994 /* Handle recording of state blocks. */
1995 if (device->recording)
1997 TRACE("Recording... not performing anything.\n");
1998 device->recording->changed.samplerState[sampler_idx] |= 1 << state;
1999 return;
2002 if (old_value == value)
2004 TRACE("Application is setting the old value over, nothing to do.\n");
2005 return;
2008 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2011 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2012 UINT sampler_idx, enum wined3d_sampler_state state)
2014 TRACE("device %p, sampler_idx %u, state %s.\n",
2015 device, sampler_idx, debug_d3dsamplerstate(state));
2017 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2018 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2020 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2022 WARN("Invalid sampler %u.\n", sampler_idx);
2023 return 0; /* Windows accepts overflowing this array ... we do not. */
2026 return device->state.sampler_states[sampler_idx][state];
2029 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2031 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2033 if (device->recording)
2034 device->recording->changed.scissorRect = TRUE;
2036 if (EqualRect(&device->update_state->scissor_rect, rect))
2038 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2039 return;
2041 CopyRect(&device->update_state->scissor_rect, rect);
2043 if (device->recording)
2045 TRACE("Recording... not performing anything.\n");
2046 return;
2049 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2052 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2054 TRACE("device %p, rect %p.\n", device, rect);
2056 *rect = device->state.scissor_rect;
2057 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2060 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2061 struct wined3d_vertex_declaration *declaration)
2063 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2065 TRACE("device %p, declaration %p.\n", device, declaration);
2067 if (device->recording)
2068 device->recording->changed.vertexDecl = TRUE;
2070 if (declaration == prev)
2071 return;
2073 if (declaration)
2074 wined3d_vertex_declaration_incref(declaration);
2075 device->update_state->vertex_declaration = declaration;
2076 if (!device->recording)
2077 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2078 if (prev)
2079 wined3d_vertex_declaration_decref(prev);
2082 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2084 TRACE("device %p.\n", device);
2086 return device->state.vertex_declaration;
2089 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2091 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2093 TRACE("device %p, shader %p.\n", device, shader);
2095 if (device->recording)
2096 device->recording->changed.vertexShader = TRUE;
2098 if (shader == prev)
2099 return;
2101 if (shader)
2102 wined3d_shader_incref(shader);
2103 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2104 if (!device->recording)
2105 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2106 if (prev)
2107 wined3d_shader_decref(prev);
2110 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2112 TRACE("device %p.\n", device);
2114 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2117 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2118 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2120 struct wined3d_buffer *prev;
2122 if (idx >= MAX_CONSTANT_BUFFERS)
2124 WARN("Invalid constant buffer index %u.\n", idx);
2125 return;
2128 prev = device->update_state->cb[type][idx];
2129 if (buffer == prev)
2130 return;
2132 if (buffer)
2133 wined3d_buffer_incref(buffer);
2134 device->update_state->cb[type][idx] = buffer;
2135 if (!device->recording)
2136 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2137 if (prev)
2138 wined3d_buffer_decref(prev);
2141 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2143 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2145 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2148 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2150 TRACE("device %p, idx %u.\n", device, idx);
2152 if (idx >= MAX_CONSTANT_BUFFERS)
2154 WARN("Invalid constant buffer index %u.\n", idx);
2155 return NULL;
2158 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2161 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2162 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2164 struct wined3d_shader_resource_view *prev;
2166 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2168 WARN("Invalid view index %u.\n", idx);
2169 return;
2172 prev = device->update_state->shader_resource_view[type][idx];
2173 if (view == prev)
2174 return;
2176 if (view)
2177 wined3d_shader_resource_view_incref(view);
2178 device->update_state->shader_resource_view[type][idx] = view;
2179 if (!device->recording)
2180 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2181 if (prev)
2182 wined3d_shader_resource_view_decref(prev);
2185 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2186 UINT idx, struct wined3d_shader_resource_view *view)
2188 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2190 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2193 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2194 UINT idx)
2196 TRACE("device %p, idx %u.\n", device, idx);
2198 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2200 WARN("Invalid view index %u.\n", idx);
2201 return NULL;
2204 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2207 static void wined3d_device_set_sampler(struct wined3d_device *device,
2208 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2210 struct wined3d_sampler *prev;
2212 if (idx >= MAX_SAMPLER_OBJECTS)
2214 WARN("Invalid sampler index %u.\n", idx);
2215 return;
2218 prev = device->update_state->sampler[type][idx];
2219 if (sampler == prev)
2220 return;
2222 if (sampler)
2223 wined3d_sampler_incref(sampler);
2224 device->update_state->sampler[type][idx] = sampler;
2225 if (!device->recording)
2226 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2227 if (prev)
2228 wined3d_sampler_decref(prev);
2231 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2233 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2235 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2238 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2240 TRACE("device %p, idx %u.\n", device, idx);
2242 if (idx >= MAX_SAMPLER_OBJECTS)
2244 WARN("Invalid sampler index %u.\n", idx);
2245 return NULL;
2248 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2251 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2253 UINT i;
2255 for (i = 0; i < device->context_count; ++i)
2257 device->contexts[i]->constant_update_mask |= mask;
2261 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2262 UINT start_register, const BOOL *constants, UINT bool_count)
2264 UINT count = min(bool_count, MAX_CONST_B - start_register);
2265 UINT i;
2267 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2268 device, start_register, constants, bool_count);
2270 if (!constants || start_register >= MAX_CONST_B)
2271 return WINED3DERR_INVALIDCALL;
2273 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2274 for (i = 0; i < count; ++i)
2275 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2277 if (device->recording)
2279 for (i = start_register; i < count + start_register; ++i)
2280 device->recording->changed.vertexShaderConstantsB |= (1 << i);
2282 else
2284 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2287 return WINED3D_OK;
2290 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2291 UINT start_register, BOOL *constants, UINT bool_count)
2293 UINT count = min(bool_count, MAX_CONST_B - start_register);
2295 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2296 device, start_register, constants, bool_count);
2298 if (!constants || start_register >= MAX_CONST_B)
2299 return WINED3DERR_INVALIDCALL;
2301 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2303 return WINED3D_OK;
2306 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2307 UINT start_register, const int *constants, UINT vector4i_count)
2309 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2310 UINT i;
2312 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2313 device, start_register, constants, vector4i_count);
2315 if (!constants || start_register >= MAX_CONST_I)
2316 return WINED3DERR_INVALIDCALL;
2318 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2319 for (i = 0; i < count; ++i)
2320 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2321 constants[i * 4], constants[i * 4 + 1],
2322 constants[i * 4 + 2], constants[i * 4 + 3]);
2324 if (device->recording)
2326 for (i = start_register; i < count + start_register; ++i)
2327 device->recording->changed.vertexShaderConstantsI |= (1 << i);
2329 else
2331 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2334 return WINED3D_OK;
2337 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2338 UINT start_register, int *constants, UINT vector4i_count)
2340 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2342 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2343 device, start_register, constants, vector4i_count);
2345 if (!constants || start_register >= MAX_CONST_I)
2346 return WINED3DERR_INVALIDCALL;
2348 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2349 return WINED3D_OK;
2352 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2353 UINT start_register, const float *constants, UINT vector4f_count)
2355 UINT i;
2356 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2358 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2359 device, start_register, constants, vector4f_count);
2361 /* Specifically test start_register > limit to catch MAX_UINT overflows
2362 * when adding start_register + vector4f_count. */
2363 if (!constants
2364 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2365 || start_register > d3d_info->limits.vs_uniform_count)
2366 return WINED3DERR_INVALIDCALL;
2368 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2369 constants, vector4f_count * sizeof(float) * 4);
2370 if (TRACE_ON(d3d))
2372 for (i = 0; i < vector4f_count; ++i)
2373 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2374 constants[i * 4], constants[i * 4 + 1],
2375 constants[i * 4 + 2], constants[i * 4 + 3]);
2378 if (device->recording)
2379 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2380 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2381 else
2382 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2385 return WINED3D_OK;
2388 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2389 UINT start_register, float *constants, UINT vector4f_count)
2391 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2392 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2394 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2395 device, start_register, constants, vector4f_count);
2397 if (!constants || count < 0)
2398 return WINED3DERR_INVALIDCALL;
2400 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2402 return WINED3D_OK;
2405 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2407 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2409 TRACE("device %p, shader %p.\n", device, shader);
2411 if (device->recording)
2412 device->recording->changed.pixelShader = TRUE;
2414 if (shader == prev)
2415 return;
2417 if (shader)
2418 wined3d_shader_incref(shader);
2419 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2420 if (!device->recording)
2421 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2422 if (prev)
2423 wined3d_shader_decref(prev);
2426 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2428 TRACE("device %p.\n", device);
2430 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2433 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2435 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2437 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2440 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2442 TRACE("device %p, idx %u.\n", device, idx);
2444 if (idx >= MAX_CONSTANT_BUFFERS)
2446 WARN("Invalid constant buffer index %u.\n", idx);
2447 return NULL;
2450 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2453 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2454 UINT idx, struct wined3d_shader_resource_view *view)
2456 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2458 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2461 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2462 UINT idx)
2464 TRACE("device %p, idx %u.\n", device, idx);
2466 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2468 WARN("Invalid view index %u.\n", idx);
2469 return NULL;
2472 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2475 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2477 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2479 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2482 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2484 TRACE("device %p, idx %u.\n", device, idx);
2486 if (idx >= MAX_SAMPLER_OBJECTS)
2488 WARN("Invalid sampler index %u.\n", idx);
2489 return NULL;
2492 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2495 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2496 UINT start_register, const BOOL *constants, UINT bool_count)
2498 UINT count = min(bool_count, MAX_CONST_B - start_register);
2499 UINT i;
2501 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2502 device, start_register, constants, bool_count);
2504 if (!constants || start_register >= MAX_CONST_B)
2505 return WINED3DERR_INVALIDCALL;
2507 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2508 for (i = 0; i < count; ++i)
2509 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2511 if (device->recording)
2513 for (i = start_register; i < count + start_register; ++i)
2514 device->recording->changed.pixelShaderConstantsB |= (1 << i);
2516 else
2518 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2521 return WINED3D_OK;
2524 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2525 UINT start_register, BOOL *constants, UINT bool_count)
2527 UINT count = min(bool_count, MAX_CONST_B - start_register);
2529 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2530 device, start_register, constants, bool_count);
2532 if (!constants || start_register >= MAX_CONST_B)
2533 return WINED3DERR_INVALIDCALL;
2535 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2537 return WINED3D_OK;
2540 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2541 UINT start_register, const int *constants, UINT vector4i_count)
2543 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2544 UINT i;
2546 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2547 device, start_register, constants, vector4i_count);
2549 if (!constants || start_register >= MAX_CONST_I)
2550 return WINED3DERR_INVALIDCALL;
2552 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2553 for (i = 0; i < count; ++i)
2554 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2555 constants[i * 4], constants[i * 4 + 1],
2556 constants[i * 4 + 2], constants[i * 4 + 3]);
2558 if (device->recording)
2560 for (i = start_register; i < count + start_register; ++i)
2561 device->recording->changed.pixelShaderConstantsI |= (1 << i);
2563 else
2565 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2568 return WINED3D_OK;
2571 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2572 UINT start_register, int *constants, UINT vector4i_count)
2574 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2576 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2577 device, start_register, constants, vector4i_count);
2579 if (!constants || start_register >= MAX_CONST_I)
2580 return WINED3DERR_INVALIDCALL;
2582 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2584 return WINED3D_OK;
2587 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2588 UINT start_register, const float *constants, UINT vector4f_count)
2590 UINT i;
2591 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2593 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2594 device, start_register, constants, vector4f_count);
2596 /* Specifically test start_register > limit to catch MAX_UINT overflows
2597 * when adding start_register + vector4f_count. */
2598 if (!constants
2599 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2600 || start_register > d3d_info->limits.ps_uniform_count)
2601 return WINED3DERR_INVALIDCALL;
2603 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2604 constants, vector4f_count * sizeof(float) * 4);
2605 if (TRACE_ON(d3d))
2607 for (i = 0; i < vector4f_count; ++i)
2608 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2609 constants[i * 4], constants[i * 4 + 1],
2610 constants[i * 4 + 2], constants[i * 4 + 3]);
2613 if (device->recording)
2614 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2615 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2616 else
2617 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2619 return WINED3D_OK;
2622 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2623 UINT start_register, float *constants, UINT vector4f_count)
2625 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2626 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2628 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2629 device, start_register, constants, vector4f_count);
2631 if (!constants || count < 0)
2632 return WINED3DERR_INVALIDCALL;
2634 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2636 return WINED3D_OK;
2639 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2641 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2643 TRACE("device %p, shader %p.\n", device, shader);
2645 if (device->recording || shader == prev)
2646 return;
2647 if (shader)
2648 wined3d_shader_incref(shader);
2649 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2650 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2651 if (prev)
2652 wined3d_shader_decref(prev);
2655 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2657 TRACE("device %p.\n", device);
2659 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2662 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2664 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2666 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2669 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2671 TRACE("device %p, idx %u.\n", device, idx);
2673 if (idx >= MAX_CONSTANT_BUFFERS)
2675 WARN("Invalid constant buffer index %u.\n", idx);
2676 return NULL;
2679 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2682 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2683 UINT idx, struct wined3d_shader_resource_view *view)
2685 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2687 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2690 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2691 UINT idx)
2693 TRACE("device %p, idx %u.\n", device, idx);
2695 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2697 WARN("Invalid view index %u.\n", idx);
2698 return NULL;
2701 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2704 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2706 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2708 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2711 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2713 TRACE("device %p, idx %u.\n", device, idx);
2715 if (idx >= MAX_SAMPLER_OBJECTS)
2717 WARN("Invalid sampler index %u.\n", idx);
2718 return NULL;
2721 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2724 /* Context activation is done by the caller. */
2725 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2726 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2727 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2728 DWORD DestFVF)
2730 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2731 struct wined3d_viewport vp;
2732 UINT vertex_size;
2733 unsigned int i;
2734 BYTE *dest_ptr;
2735 BOOL doClip;
2736 DWORD numTextures;
2737 HRESULT hr;
2739 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
2741 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2744 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
2746 ERR("Source has no position mask\n");
2747 return WINED3DERR_INVALIDCALL;
2750 if (device->state.render_states[WINED3D_RS_CLIPPING])
2752 static BOOL warned = FALSE;
2754 * The clipping code is not quite correct. Some things need
2755 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2756 * so disable clipping for now.
2757 * (The graphics in Half-Life are broken, and my processvertices
2758 * test crashes with IDirect3DDevice3)
2759 doClip = TRUE;
2761 doClip = FALSE;
2762 if(!warned) {
2763 warned = TRUE;
2764 FIXME("Clipping is broken and disabled for now\n");
2767 else
2768 doClip = FALSE;
2770 vertex_size = get_flexible_vertex_size(DestFVF);
2771 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2773 WARN("Failed to map buffer, hr %#x.\n", hr);
2774 return hr;
2777 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2778 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2779 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2781 TRACE("View mat:\n");
2782 TRACE("%f %f %f %f\n", view_mat.u.s._11, view_mat.u.s._12, view_mat.u.s._13, view_mat.u.s._14);
2783 TRACE("%f %f %f %f\n", view_mat.u.s._21, view_mat.u.s._22, view_mat.u.s._23, view_mat.u.s._24);
2784 TRACE("%f %f %f %f\n", view_mat.u.s._31, view_mat.u.s._32, view_mat.u.s._33, view_mat.u.s._34);
2785 TRACE("%f %f %f %f\n", view_mat.u.s._41, view_mat.u.s._42, view_mat.u.s._43, view_mat.u.s._44);
2787 TRACE("Proj mat:\n");
2788 TRACE("%f %f %f %f\n", proj_mat.u.s._11, proj_mat.u.s._12, proj_mat.u.s._13, proj_mat.u.s._14);
2789 TRACE("%f %f %f %f\n", proj_mat.u.s._21, proj_mat.u.s._22, proj_mat.u.s._23, proj_mat.u.s._24);
2790 TRACE("%f %f %f %f\n", proj_mat.u.s._31, proj_mat.u.s._32, proj_mat.u.s._33, proj_mat.u.s._34);
2791 TRACE("%f %f %f %f\n", proj_mat.u.s._41, proj_mat.u.s._42, proj_mat.u.s._43, proj_mat.u.s._44);
2793 TRACE("World mat:\n");
2794 TRACE("%f %f %f %f\n", world_mat.u.s._11, world_mat.u.s._12, world_mat.u.s._13, world_mat.u.s._14);
2795 TRACE("%f %f %f %f\n", world_mat.u.s._21, world_mat.u.s._22, world_mat.u.s._23, world_mat.u.s._24);
2796 TRACE("%f %f %f %f\n", world_mat.u.s._31, world_mat.u.s._32, world_mat.u.s._33, world_mat.u.s._34);
2797 TRACE("%f %f %f %f\n", world_mat.u.s._41, world_mat.u.s._42, world_mat.u.s._43, world_mat.u.s._44);
2799 /* Get the viewport */
2800 wined3d_device_get_viewport(device, &vp);
2801 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2802 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2804 multiply_matrix(&mat,&view_mat,&world_mat);
2805 multiply_matrix(&mat,&proj_mat,&mat);
2807 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2809 for (i = 0; i < dwCount; i+= 1) {
2810 unsigned int tex_index;
2812 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2813 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2814 /* The position first */
2815 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2816 const float *p = (const float *)(element->data.addr + i * element->stride);
2817 float x, y, z, rhw;
2818 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2820 /* Multiplication with world, view and projection matrix */
2821 x = (p[0] * mat.u.s._11) + (p[1] * mat.u.s._21) + (p[2] * mat.u.s._31) + (1.0f * mat.u.s._41);
2822 y = (p[0] * mat.u.s._12) + (p[1] * mat.u.s._22) + (p[2] * mat.u.s._32) + (1.0f * mat.u.s._42);
2823 z = (p[0] * mat.u.s._13) + (p[1] * mat.u.s._23) + (p[2] * mat.u.s._33) + (1.0f * mat.u.s._43);
2824 rhw = (p[0] * mat.u.s._14) + (p[1] * mat.u.s._24) + (p[2] * mat.u.s._34) + (1.0f * mat.u.s._44);
2826 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2828 /* WARNING: The following things are taken from d3d7 and were not yet checked
2829 * against d3d8 or d3d9!
2832 /* Clipping conditions: From msdn
2834 * A vertex is clipped if it does not match the following requirements
2835 * -rhw < x <= rhw
2836 * -rhw < y <= rhw
2837 * 0 < z <= rhw
2838 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2840 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2841 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2845 if( !doClip ||
2846 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2847 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2848 ( rhw > eps ) ) ) {
2850 /* "Normal" viewport transformation (not clipped)
2851 * 1) The values are divided by rhw
2852 * 2) The y axis is negative, so multiply it with -1
2853 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2854 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2855 * 4) Multiply x with Width/2 and add Width/2
2856 * 5) The same for the height
2857 * 6) Add the viewpoint X and Y to the 2D coordinates and
2858 * The minimum Z value to z
2859 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2861 * Well, basically it's simply a linear transformation into viewport
2862 * coordinates
2865 x /= rhw;
2866 y /= rhw;
2867 z /= rhw;
2869 y *= -1;
2871 x *= vp.width / 2;
2872 y *= vp.height / 2;
2873 z *= vp.max_z - vp.min_z;
2875 x += vp.width / 2 + vp.x;
2876 y += vp.height / 2 + vp.y;
2877 z += vp.min_z;
2879 rhw = 1 / rhw;
2880 } else {
2881 /* That vertex got clipped
2882 * Contrary to OpenGL it is not dropped completely, it just
2883 * undergoes a different calculation.
2885 TRACE("Vertex got clipped\n");
2886 x += rhw;
2887 y += rhw;
2889 x /= 2;
2890 y /= 2;
2892 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2893 * outside of the main vertex buffer memory. That needs some more
2894 * investigation...
2898 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2901 ( (float *) dest_ptr)[0] = x;
2902 ( (float *) dest_ptr)[1] = y;
2903 ( (float *) dest_ptr)[2] = z;
2904 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2906 dest_ptr += 3 * sizeof(float);
2908 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2909 dest_ptr += sizeof(float);
2912 if (DestFVF & WINED3DFVF_PSIZE)
2913 dest_ptr += sizeof(DWORD);
2915 if (DestFVF & WINED3DFVF_NORMAL)
2917 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2918 const float *normal = (const float *)(element->data.addr + i * element->stride);
2919 /* AFAIK this should go into the lighting information */
2920 FIXME("Didn't expect the destination to have a normal\n");
2921 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2924 if (DestFVF & WINED3DFVF_DIFFUSE)
2926 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2927 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2928 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
2930 static BOOL warned = FALSE;
2932 if(!warned) {
2933 ERR("No diffuse color in source, but destination has one\n");
2934 warned = TRUE;
2937 *( (DWORD *) dest_ptr) = 0xffffffff;
2938 dest_ptr += sizeof(DWORD);
2940 else
2942 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2946 if (DestFVF & WINED3DFVF_SPECULAR)
2948 /* What's the color value in the feedback buffer? */
2949 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2950 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2951 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
2953 static BOOL warned = FALSE;
2955 if(!warned) {
2956 ERR("No specular color in source, but destination has one\n");
2957 warned = TRUE;
2960 *(DWORD *)dest_ptr = 0xff000000;
2961 dest_ptr += sizeof(DWORD);
2963 else
2965 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2969 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2971 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2972 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2973 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2975 ERR("No source texture, but destination requests one\n");
2976 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2978 else
2980 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2985 wined3d_buffer_unmap(dest);
2987 return WINED3D_OK;
2989 #undef copy_and_next
2991 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
2992 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
2993 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
2995 struct wined3d_state *state = &device->state;
2996 struct wined3d_stream_info stream_info;
2997 const struct wined3d_gl_info *gl_info;
2998 struct wined3d_context *context;
2999 struct wined3d_shader *vs;
3000 unsigned int i;
3001 HRESULT hr;
3002 WORD map;
3004 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3005 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3006 device, src_start_idx, dst_idx, vertex_count,
3007 dst_buffer, declaration, flags, dst_fvf);
3009 if (declaration)
3010 FIXME("Output vertex declaration not implemented yet.\n");
3012 /* Need any context to write to the vbo. */
3013 context = context_acquire(device, NULL);
3014 gl_info = context->gl_info;
3016 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3017 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3018 context_stream_info_from_declaration(context, state, &stream_info);
3019 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3021 /* We can't convert FROM a VBO, and vertex buffers used to source into
3022 * process_vertices() are unlikely to ever be used for drawing. Release
3023 * VBOs in those buffers and fix up the stream_info structure.
3025 * Also apply the start index. */
3026 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3028 struct wined3d_stream_info_element *e;
3029 struct wined3d_buffer *buffer;
3031 if (!(map & 1))
3032 continue;
3034 e = &stream_info.elements[i];
3035 buffer = state->streams[e->stream_idx].buffer;
3036 e->data.buffer_object = 0;
3037 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3038 if (buffer->buffer_object)
3040 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3041 buffer->buffer_object = 0;
3043 if (e->data.addr)
3044 e->data.addr += e->stride * src_start_idx;
3047 hr = process_vertices_strided(device, dst_idx, vertex_count,
3048 &stream_info, dst_buffer, flags, dst_fvf);
3050 context_release(context);
3052 return hr;
3055 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3056 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3058 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3059 DWORD old_value;
3061 TRACE("device %p, stage %u, state %s, value %#x.\n",
3062 device, stage, debug_d3dtexturestate(state), value);
3064 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3066 WARN("Invalid state %#x passed.\n", state);
3067 return;
3070 if (stage >= d3d_info->limits.ffp_blend_stages)
3072 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3073 stage, d3d_info->limits.ffp_blend_stages - 1);
3074 return;
3077 old_value = device->update_state->texture_states[stage][state];
3078 device->update_state->texture_states[stage][state] = value;
3080 if (device->recording)
3082 TRACE("Recording... not performing anything.\n");
3083 device->recording->changed.textureState[stage] |= 1 << state;
3084 return;
3087 /* Checked after the assignments to allow proper stateblock recording. */
3088 if (old_value == value)
3090 TRACE("Application is setting the old value over, nothing to do.\n");
3091 return;
3094 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3097 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3098 UINT stage, enum wined3d_texture_stage_state state)
3100 TRACE("device %p, stage %u, state %s.\n",
3101 device, stage, debug_d3dtexturestate(state));
3103 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3105 WARN("Invalid state %#x passed.\n", state);
3106 return 0;
3109 return device->state.texture_states[stage][state];
3112 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3113 UINT stage, struct wined3d_texture *texture)
3115 struct wined3d_texture *prev;
3117 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3119 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3120 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3122 /* Windows accepts overflowing this array... we do not. */
3123 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3125 WARN("Ignoring invalid stage %u.\n", stage);
3126 return WINED3D_OK;
3129 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3131 WARN("Rejecting attempt to set scratch texture.\n");
3132 return WINED3DERR_INVALIDCALL;
3135 if (device->recording)
3136 device->recording->changed.textures |= 1 << stage;
3138 prev = device->update_state->textures[stage];
3139 TRACE("Previous texture %p.\n", prev);
3141 if (texture == prev)
3143 TRACE("App is setting the same texture again, nothing to do.\n");
3144 return WINED3D_OK;
3147 TRACE("Setting new texture to %p.\n", texture);
3148 device->update_state->textures[stage] = texture;
3150 if (texture)
3151 wined3d_texture_incref(texture);
3152 if (!device->recording)
3153 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3154 if (prev)
3155 wined3d_texture_decref(prev);
3157 return WINED3D_OK;
3160 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3162 TRACE("device %p, stage %u.\n", device, stage);
3164 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3165 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3167 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3169 WARN("Ignoring invalid stage %u.\n", stage);
3170 return NULL; /* Windows accepts overflowing this array ... we do not. */
3173 return device->state.textures[stage];
3176 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3177 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3179 struct wined3d_swapchain *swapchain;
3181 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3182 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3184 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3185 return WINED3DERR_INVALIDCALL;
3187 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3188 return WINED3DERR_INVALIDCALL;
3189 return WINED3D_OK;
3192 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3194 TRACE("device %p, caps %p.\n", device, caps);
3196 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3197 device->create_parms.device_type, caps);
3200 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3201 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3203 struct wined3d_swapchain *swapchain;
3205 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3206 device, swapchain_idx, mode, rotation);
3208 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3209 return WINED3DERR_INVALIDCALL;
3211 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3214 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3216 struct wined3d_stateblock *stateblock;
3217 HRESULT hr;
3219 TRACE("device %p.\n", device);
3221 if (device->recording)
3222 return WINED3DERR_INVALIDCALL;
3224 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3225 if (FAILED(hr))
3226 return hr;
3228 device->recording = stateblock;
3229 device->update_state = &stateblock->state;
3231 TRACE("Recording stateblock %p.\n", stateblock);
3233 return WINED3D_OK;
3236 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3237 struct wined3d_stateblock **stateblock)
3239 struct wined3d_stateblock *object = device->recording;
3241 TRACE("device %p, stateblock %p.\n", device, stateblock);
3243 if (!device->recording)
3245 WARN("Not recording.\n");
3246 *stateblock = NULL;
3247 return WINED3DERR_INVALIDCALL;
3250 stateblock_init_contained_states(object);
3252 *stateblock = object;
3253 device->recording = NULL;
3254 device->update_state = &device->state;
3256 TRACE("Returning stateblock %p.\n", *stateblock);
3258 return WINED3D_OK;
3261 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3263 /* At the moment we have no need for any functionality at the beginning
3264 * of a scene. */
3265 TRACE("device %p.\n", device);
3267 if (device->inScene)
3269 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3270 return WINED3DERR_INVALIDCALL;
3272 device->inScene = TRUE;
3273 return WINED3D_OK;
3276 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3278 struct wined3d_context *context;
3280 TRACE("device %p.\n", device);
3282 if (!device->inScene)
3284 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3285 return WINED3DERR_INVALIDCALL;
3288 context = context_acquire(device, NULL);
3289 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3290 context->gl_info->gl_ops.gl.p_glFlush();
3291 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3292 * fails. */
3293 context_release(context);
3295 device->inScene = FALSE;
3296 return WINED3D_OK;
3299 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3300 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3302 UINT i;
3304 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3305 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3306 dst_window_override, dirty_region, flags);
3308 for (i = 0; i < device->swapchain_count; ++i)
3310 wined3d_swapchain_present(device->swapchains[i], src_rect,
3311 dst_rect, dst_window_override, dirty_region, flags);
3314 return WINED3D_OK;
3317 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3318 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3320 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3321 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3323 if (!rect_count && rects)
3325 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3326 return WINED3D_OK;
3329 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3331 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3332 if (!ds)
3334 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3335 /* TODO: What about depth stencil buffers without stencil bits? */
3336 return WINED3DERR_INVALIDCALL;
3338 else if (flags & WINED3DCLEAR_TARGET)
3340 if (ds->width < device->fb.render_targets[0]->width
3341 || ds->height < device->fb.render_targets[0]->height)
3343 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3344 return WINED3D_OK;
3349 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3351 return WINED3D_OK;
3354 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3355 struct wined3d_query *predicate, BOOL value)
3357 struct wined3d_query *prev;
3359 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3361 prev = device->update_state->predicate;
3362 if (predicate)
3364 FIXME("Predicated rendering not implemented.\n");
3365 wined3d_query_incref(predicate);
3367 device->update_state->predicate = predicate;
3368 device->update_state->predicate_value = value;
3369 if (!device->recording)
3370 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3371 if (prev)
3372 wined3d_query_decref(prev);
3375 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3377 TRACE("device %p, value %p.\n", device, value);
3379 *value = device->state.predicate_value;
3380 return device->state.predicate;
3383 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3384 enum wined3d_primitive_type primitive_type)
3386 GLenum gl_primitive_type, prev;
3388 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3390 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3391 prev = device->update_state->gl_primitive_type;
3392 device->update_state->gl_primitive_type = gl_primitive_type;
3393 if (device->recording)
3394 device->recording->changed.primitive_type = TRUE;
3395 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3396 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
3399 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3400 enum wined3d_primitive_type *primitive_type)
3402 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3404 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3406 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3409 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3411 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3413 if (!device->state.vertex_declaration)
3415 WARN("Called without a valid vertex declaration set.\n");
3416 return WINED3DERR_INVALIDCALL;
3419 if (device->state.load_base_vertex_index)
3421 device->state.load_base_vertex_index = 0;
3422 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3425 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3427 return WINED3D_OK;
3430 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3432 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3434 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3436 if (!device->state.index_buffer)
3438 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3439 * without an index buffer set. (The first time at least...)
3440 * D3D8 simply dies, but I doubt it can do much harm to return
3441 * D3DERR_INVALIDCALL there as well. */
3442 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3443 return WINED3DERR_INVALIDCALL;
3446 if (!device->state.vertex_declaration)
3448 WARN("Called without a valid vertex declaration set.\n");
3449 return WINED3DERR_INVALIDCALL;
3452 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3453 device->state.load_base_vertex_index != device->state.base_vertex_index)
3455 device->state.load_base_vertex_index = device->state.base_vertex_index;
3456 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3459 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3461 return WINED3D_OK;
3464 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3465 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3467 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3469 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3472 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3473 static HRESULT device_update_volume(struct wined3d_device *device,
3474 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3476 struct wined3d_const_bo_address data;
3477 struct wined3d_map_desc src;
3478 HRESULT hr;
3479 struct wined3d_context *context;
3481 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3482 device, src_volume, dst_volume);
3484 if (src_volume->resource.format != dst_volume->resource.format)
3486 FIXME("Source and destination formats do not match.\n");
3487 return WINED3DERR_INVALIDCALL;
3489 if (src_volume->resource.width != dst_volume->resource.width
3490 || src_volume->resource.height != dst_volume->resource.height
3491 || src_volume->resource.depth != dst_volume->resource.depth)
3493 FIXME("Source and destination sizes do not match.\n");
3494 return WINED3DERR_INVALIDCALL;
3497 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3498 return hr;
3500 context = context_acquire(device, NULL);
3502 /* Only a prepare, since we're uploading the entire volume. */
3503 wined3d_texture_prepare_texture(dst_volume->container, context, FALSE);
3504 wined3d_texture_bind(dst_volume->container, context, FALSE);
3506 data.buffer_object = 0;
3507 data.addr = src.data;
3508 wined3d_volume_upload_data(dst_volume, context, &data);
3509 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3511 context_release(context);
3513 hr = wined3d_volume_unmap(src_volume);
3515 return hr;
3518 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3519 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3521 enum wined3d_resource_type type;
3522 unsigned int level_count, i;
3523 HRESULT hr;
3524 struct wined3d_context *context;
3526 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3528 /* Verify that the source and destination textures are non-NULL. */
3529 if (!src_texture || !dst_texture)
3531 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3532 return WINED3DERR_INVALIDCALL;
3535 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3537 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3538 return WINED3DERR_INVALIDCALL;
3540 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3542 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3543 return WINED3DERR_INVALIDCALL;
3546 /* Verify that the source and destination textures are the same type. */
3547 type = src_texture->resource.type;
3548 if (dst_texture->resource.type != type)
3550 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3551 return WINED3DERR_INVALIDCALL;
3554 /* Check that both textures have the identical numbers of levels. */
3555 level_count = wined3d_texture_get_level_count(src_texture);
3556 if (wined3d_texture_get_level_count(dst_texture) != level_count)
3558 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
3559 return WINED3DERR_INVALIDCALL;
3562 /* Make sure that the destination texture is loaded. */
3563 context = context_acquire(device, NULL);
3564 wined3d_texture_load(dst_texture, context, FALSE);
3565 context_release(context);
3567 /* Update every surface level of the texture. */
3568 switch (type)
3570 case WINED3D_RTYPE_TEXTURE:
3572 struct wined3d_surface *src_surface;
3573 struct wined3d_surface *dst_surface;
3575 for (i = 0; i < level_count; ++i)
3577 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3578 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3579 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3580 if (FAILED(hr))
3582 WARN("Failed to update surface, hr %#x.\n", hr);
3583 return hr;
3586 break;
3589 case WINED3D_RTYPE_CUBE_TEXTURE:
3591 struct wined3d_surface *src_surface;
3592 struct wined3d_surface *dst_surface;
3594 for (i = 0; i < level_count * 6; ++i)
3596 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3597 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3598 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3599 if (FAILED(hr))
3601 WARN("Failed to update surface, hr %#x.\n", hr);
3602 return hr;
3605 break;
3608 case WINED3D_RTYPE_VOLUME_TEXTURE:
3610 for (i = 0; i < level_count; ++i)
3612 hr = device_update_volume(device,
3613 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
3614 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3615 if (FAILED(hr))
3617 WARN("Failed to update volume, hr %#x.\n", hr);
3618 return hr;
3621 break;
3624 default:
3625 FIXME("Unsupported texture type %#x.\n", type);
3626 return WINED3DERR_INVALIDCALL;
3629 return WINED3D_OK;
3632 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3633 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3635 struct wined3d_swapchain *swapchain;
3637 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3639 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3640 return WINED3DERR_INVALIDCALL;
3642 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3645 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3647 const struct wined3d_state *state = &device->state;
3648 struct wined3d_texture *texture;
3649 DWORD i;
3651 TRACE("device %p, num_passes %p.\n", device, num_passes);
3653 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3655 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3657 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3658 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3660 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3662 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3663 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3666 texture = state->textures[i];
3667 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
3669 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3671 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3672 return E_FAIL;
3674 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3676 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3677 return E_FAIL;
3679 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3680 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3682 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3683 return E_FAIL;
3687 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3688 || state->render_states[WINED3D_RS_STENCILENABLE])
3690 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3691 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3693 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3695 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3696 return WINED3DERR_CONFLICTINGRENDERSTATE;
3700 /* return a sensible default */
3701 *num_passes = 1;
3703 TRACE("returning D3D_OK\n");
3704 return WINED3D_OK;
3707 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3709 static BOOL warned;
3711 TRACE("device %p, software %#x.\n", device, software);
3713 if (!warned)
3715 FIXME("device %p, software %#x stub!\n", device, software);
3716 warned = TRUE;
3719 device->softwareVertexProcessing = software;
3722 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3724 static BOOL warned;
3726 TRACE("device %p.\n", device);
3728 if (!warned)
3730 TRACE("device %p stub!\n", device);
3731 warned = TRUE;
3734 return device->softwareVertexProcessing;
3737 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3738 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3740 struct wined3d_swapchain *swapchain;
3742 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3743 device, swapchain_idx, raster_status);
3745 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3746 return WINED3DERR_INVALIDCALL;
3748 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3751 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3753 static BOOL warned;
3755 TRACE("device %p, segments %.8e.\n", device, segments);
3757 if (segments != 0.0f)
3759 if (!warned)
3761 FIXME("device %p, segments %.8e stub!\n", device, segments);
3762 warned = TRUE;
3766 return WINED3D_OK;
3769 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3771 static BOOL warned;
3773 TRACE("device %p.\n", device);
3775 if (!warned)
3777 FIXME("device %p stub!\n", device);
3778 warned = TRUE;
3781 return 0.0f;
3784 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3785 struct wined3d_surface *src_surface, const RECT *src_rect,
3786 struct wined3d_surface *dst_surface, const POINT *dst_point)
3788 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3789 device, src_surface, wine_dbgstr_rect(src_rect),
3790 dst_surface, wine_dbgstr_point(dst_point));
3792 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3794 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3795 src_surface, dst_surface);
3796 return WINED3DERR_INVALIDCALL;
3799 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3802 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3803 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3805 struct wined3d_surface *dst_surface, *src_surface;
3806 struct wined3d_texture *dst_texture, *src_texture;
3807 unsigned int i, count;
3808 HRESULT hr;
3810 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3812 if (src_resource == dst_resource)
3814 WARN("Source and destination are the same resource.\n");
3815 return;
3818 if (src_resource->type != dst_resource->type)
3820 WARN("Resource types (%s / %s) don't match.\n",
3821 debug_d3dresourcetype(dst_resource->type),
3822 debug_d3dresourcetype(src_resource->type));
3823 return;
3826 if (src_resource->width != dst_resource->width
3827 || src_resource->height != dst_resource->height
3828 || src_resource->depth != dst_resource->depth)
3830 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3831 dst_resource->width, dst_resource->height, dst_resource->depth,
3832 src_resource->width, src_resource->height, src_resource->depth);
3833 return;
3836 if (src_resource->format->id != dst_resource->format->id)
3838 WARN("Resource formats (%s / %s) don't match.\n",
3839 debug_d3dformat(dst_resource->format->id),
3840 debug_d3dformat(src_resource->format->id));
3841 return;
3844 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3846 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3847 return;
3850 dst_texture = wined3d_texture_from_resource(dst_resource);
3851 src_texture = wined3d_texture_from_resource(src_resource);
3853 if (src_texture->layer_count != dst_texture->layer_count
3854 || src_texture->level_count != dst_texture->level_count)
3856 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3857 dst_texture->layer_count, dst_texture->level_count,
3858 src_texture->layer_count, src_texture->level_count);
3859 return;
3862 count = dst_texture->layer_count * dst_texture->level_count;
3863 for (i = 0; i < count; ++i)
3865 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3866 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3868 if (FAILED(hr = wined3d_surface_blt(dst_surface, NULL, src_surface, NULL, 0, NULL, WINED3D_TEXF_POINT)))
3869 ERR("Failed to blit, subresource %u, hr %#x.\n", i, hr);
3873 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
3874 struct wined3d_rendertarget_view *view, const RECT *rect, const struct wined3d_color *color)
3876 struct wined3d_resource *resource;
3877 RECT r;
3879 TRACE("device %p, view %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
3880 device, view, wine_dbgstr_rect(rect), color->r, color->g, color->b, color->a);
3882 resource = view->resource;
3883 if (resource->type != WINED3D_RTYPE_TEXTURE && resource->type != WINED3D_RTYPE_CUBE_TEXTURE)
3885 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
3886 return WINED3DERR_INVALIDCALL;
3889 if (view->depth > 1)
3891 FIXME("Layered clears not implemented.\n");
3892 return WINED3DERR_INVALIDCALL;
3895 if (!rect)
3897 SetRect(&r, 0, 0, view->width, view->height);
3898 rect = &r;
3901 resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource), view->sub_resource_idx);
3903 return surface_color_fill(surface_from_resource(resource), rect, color);
3906 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
3907 unsigned int view_idx)
3909 TRACE("device %p, view_idx %u.\n", device, view_idx);
3911 if (view_idx >= device->adapter->gl_info.limits.buffers)
3913 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3914 return NULL;
3917 return device->fb.render_targets[view_idx];
3920 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
3922 TRACE("device %p.\n", device);
3924 return device->fb.depth_stencil;
3927 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
3928 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
3930 struct wined3d_rendertarget_view *prev;
3932 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
3933 device, view_idx, view, set_viewport);
3935 if (view_idx >= device->adapter->gl_info.limits.buffers)
3937 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3938 return WINED3DERR_INVALIDCALL;
3941 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
3943 WARN("View resource %p doesn't have render target usage.\n", view->resource);
3944 return WINED3DERR_INVALIDCALL;
3947 /* Set the viewport and scissor rectangles, if requested. Tests show that
3948 * stateblock recording is ignored, the change goes directly into the
3949 * primary stateblock. */
3950 if (!view_idx && set_viewport)
3952 struct wined3d_state *state = &device->state;
3954 state->viewport.x = 0;
3955 state->viewport.y = 0;
3956 state->viewport.width = view->width;
3957 state->viewport.height = view->height;
3958 state->viewport.min_z = 0.0f;
3959 state->viewport.max_z = 1.0f;
3960 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
3962 state->scissor_rect.top = 0;
3963 state->scissor_rect.left = 0;
3964 state->scissor_rect.right = view->width;
3965 state->scissor_rect.bottom = view->height;
3966 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
3970 prev = device->fb.render_targets[view_idx];
3971 if (view == prev)
3972 return WINED3D_OK;
3974 if (view)
3975 wined3d_rendertarget_view_incref(view);
3976 device->fb.render_targets[view_idx] = view;
3977 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
3978 /* Release after the assignment, to prevent device_resource_released()
3979 * from seeing the surface as still in use. */
3980 if (prev)
3981 wined3d_rendertarget_view_decref(prev);
3983 return WINED3D_OK;
3986 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
3988 struct wined3d_rendertarget_view *prev;
3990 TRACE("device %p, view %p.\n", device, view);
3992 prev = device->fb.depth_stencil;
3993 if (prev == view)
3995 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
3996 return;
3999 if ((device->fb.depth_stencil = view))
4000 wined3d_rendertarget_view_incref(view);
4001 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4002 if (prev)
4003 wined3d_rendertarget_view_decref(prev);
4006 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4007 struct wined3d_surface *cursor_image)
4009 struct wined3d_sub_resource_data data;
4010 struct wined3d_resource_desc desc;
4011 struct wined3d_map_desc map_desc;
4012 struct wined3d_texture *texture;
4013 HRESULT hr;
4015 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4017 ERR("Failed to map source surface.\n");
4018 return NULL;
4021 data.data = map_desc.data;
4022 data.row_pitch = map_desc.row_pitch;
4023 data.slice_pitch = map_desc.slice_pitch;
4025 desc.resource_type = WINED3D_RTYPE_TEXTURE;
4026 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4027 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4028 desc.multisample_quality = 0;
4029 desc.usage = WINED3DUSAGE_DYNAMIC;
4030 desc.pool = WINED3D_POOL_DEFAULT;
4031 desc.width = cursor_image->resource.width;
4032 desc.height = cursor_image->resource.height;
4033 desc.depth = 1;
4034 desc.size = 0;
4036 hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
4037 &data, NULL, &wined3d_null_parent_ops, &texture);
4038 wined3d_surface_unmap(cursor_image);
4039 if (FAILED(hr))
4041 ERR("Failed to create cursor texture.\n");
4042 return NULL;
4045 return texture;
4048 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4049 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4051 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4052 device, x_hotspot, y_hotspot, cursor_image);
4054 if (device->cursor_texture)
4056 wined3d_texture_decref(device->cursor_texture);
4057 device->cursor_texture = NULL;
4060 if (cursor_image)
4062 struct wined3d_display_mode mode;
4063 struct wined3d_map_desc map_desc;
4064 HRESULT hr;
4066 /* MSDN: Cursor must be A8R8G8B8 */
4067 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4069 WARN("surface %p has an invalid format.\n", cursor_image);
4070 return WINED3DERR_INVALIDCALL;
4073 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4075 ERR("Failed to get display mode, hr %#x.\n", hr);
4076 return WINED3DERR_INVALIDCALL;
4079 /* MSDN: Cursor must be smaller than the display mode */
4080 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4082 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4083 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4084 mode.width, mode.height);
4085 return WINED3DERR_INVALIDCALL;
4088 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4090 /* Do not store the surface's pointer because the application may
4091 * release it after setting the cursor image. Windows doesn't
4092 * addref the set surface, so we can't do this either without
4093 * creating circular refcount dependencies. */
4094 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
4096 ERR("Failed to create cursor texture.\n");
4097 return WINED3DERR_INVALIDCALL;
4100 device->cursorWidth = cursor_image->resource.width;
4101 device->cursorHeight = cursor_image->resource.height;
4103 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4105 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4106 ICONINFO cursorInfo;
4107 DWORD *maskBits;
4108 HCURSOR cursor;
4110 /* 32-bit user32 cursors ignore the alpha channel if it's all
4111 * zeroes, and use the mask instead. Fill the mask with all ones
4112 * to ensure we still get a fully transparent cursor. */
4113 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4114 memset(maskBits, 0xff, mask_size);
4115 wined3d_surface_map(cursor_image, &map_desc, NULL,
4116 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4117 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4119 cursorInfo.fIcon = FALSE;
4120 cursorInfo.xHotspot = x_hotspot;
4121 cursorInfo.yHotspot = y_hotspot;
4122 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4123 1, 1, maskBits);
4124 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4125 1, 32, map_desc.data);
4126 wined3d_surface_unmap(cursor_image);
4127 /* Create our cursor and clean up. */
4128 cursor = CreateIconIndirect(&cursorInfo);
4129 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4130 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4131 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4132 device->hardwareCursor = cursor;
4133 if (device->bCursorVisible) SetCursor( cursor );
4134 HeapFree(GetProcessHeap(), 0, maskBits);
4138 device->xHotSpot = x_hotspot;
4139 device->yHotSpot = y_hotspot;
4140 return WINED3D_OK;
4143 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4144 int x_screen_space, int y_screen_space, DWORD flags)
4146 TRACE("device %p, x %d, y %d, flags %#x.\n",
4147 device, x_screen_space, y_screen_space, flags);
4149 device->xScreenSpace = x_screen_space;
4150 device->yScreenSpace = y_screen_space;
4152 if (device->hardwareCursor)
4154 POINT pt;
4156 GetCursorPos( &pt );
4157 if (x_screen_space == pt.x && y_screen_space == pt.y)
4158 return;
4159 SetCursorPos( x_screen_space, y_screen_space );
4161 /* Switch to the software cursor if position diverges from the hardware one. */
4162 GetCursorPos( &pt );
4163 if (x_screen_space != pt.x || y_screen_space != pt.y)
4165 if (device->bCursorVisible) SetCursor( NULL );
4166 DestroyCursor( device->hardwareCursor );
4167 device->hardwareCursor = 0;
4172 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4174 BOOL oldVisible = device->bCursorVisible;
4176 TRACE("device %p, show %#x.\n", device, show);
4179 * When ShowCursor is first called it should make the cursor appear at the OS's last
4180 * known cursor position.
4182 if (show && !oldVisible)
4184 POINT pt;
4185 GetCursorPos(&pt);
4186 device->xScreenSpace = pt.x;
4187 device->yScreenSpace = pt.y;
4190 if (device->hardwareCursor)
4192 device->bCursorVisible = show;
4193 if (show)
4194 SetCursor(device->hardwareCursor);
4195 else
4196 SetCursor(NULL);
4198 else if (device->cursor_texture)
4200 device->bCursorVisible = show;
4203 return oldVisible;
4206 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4208 struct wined3d_resource *resource, *cursor;
4210 TRACE("device %p.\n", device);
4212 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4214 TRACE("Checking resource %p for eviction.\n", resource);
4216 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4218 TRACE("Evicting %p.\n", resource);
4219 resource->resource_ops->resource_unload(resource);
4223 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4224 device_invalidate_state(device, STATE_STREAMSRC);
4227 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4229 struct wined3d_resource *resource, *cursor;
4230 const struct wined3d_gl_info *gl_info;
4231 struct wined3d_context *context;
4232 struct wined3d_shader *shader;
4234 context = context_acquire(device, NULL);
4235 gl_info = context->gl_info;
4237 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4239 TRACE("Unloading resource %p.\n", resource);
4241 resource->resource_ops->resource_unload(resource);
4244 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4246 device->shader_backend->shader_destroy(shader);
4249 if (device->depth_blt_texture)
4251 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4252 device->depth_blt_texture = 0;
4255 device->blitter->free_private(device);
4256 device->shader_backend->shader_free_private(device);
4257 destroy_dummy_textures(device, gl_info);
4259 context_release(context);
4261 while (device->context_count)
4263 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4266 HeapFree(GetProcessHeap(), 0, swapchain->context);
4267 swapchain->context = NULL;
4270 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4272 struct wined3d_context *context;
4273 struct wined3d_surface *target;
4274 HRESULT hr;
4276 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4277 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4279 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4280 return hr;
4283 if (FAILED(hr = device->blitter->alloc_private(device)))
4285 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4286 device->shader_backend->shader_free_private(device);
4287 return hr;
4290 /* Recreate the primary swapchain's context */
4291 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4292 if (!swapchain->context)
4294 ERR("Failed to allocate memory for swapchain context array.\n");
4295 device->blitter->free_private(device);
4296 device->shader_backend->shader_free_private(device);
4297 return E_OUTOFMEMORY;
4300 target = swapchain->back_buffers
4301 ? surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0))
4302 : surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
4303 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4305 WARN("Failed to create context.\n");
4306 device->blitter->free_private(device);
4307 device->shader_backend->shader_free_private(device);
4308 HeapFree(GetProcessHeap(), 0, swapchain->context);
4309 return E_FAIL;
4312 swapchain->context[0] = context;
4313 swapchain->num_contexts = 1;
4314 create_dummy_textures(device, context);
4315 context_release(context);
4317 return WINED3D_OK;
4320 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4321 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4322 wined3d_device_reset_cb callback, BOOL reset_state)
4324 struct wined3d_resource *resource, *cursor;
4325 struct wined3d_swapchain *swapchain;
4326 struct wined3d_display_mode m;
4327 BOOL DisplayModeChanged;
4328 BOOL update_desc = FALSE;
4329 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4330 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4331 HRESULT hr = WINED3D_OK;
4332 unsigned int i;
4334 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4336 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4338 ERR("Failed to get the first implicit swapchain.\n");
4339 return WINED3DERR_INVALIDCALL;
4341 DisplayModeChanged = swapchain->reapply_mode;
4343 if (reset_state)
4345 if (device->logo_texture)
4347 wined3d_texture_decref(device->logo_texture);
4348 device->logo_texture = NULL;
4350 if (device->cursor_texture)
4352 wined3d_texture_decref(device->cursor_texture);
4353 device->cursor_texture = NULL;
4355 state_unbind_resources(&device->state);
4358 if (device->fb.render_targets)
4360 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4362 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4365 wined3d_device_set_depth_stencil_view(device, NULL);
4367 if (device->onscreen_depth_stencil)
4369 wined3d_surface_decref(device->onscreen_depth_stencil);
4370 device->onscreen_depth_stencil = NULL;
4373 if (reset_state)
4375 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4377 TRACE("Enumerating resource %p.\n", resource);
4378 if (FAILED(hr = callback(resource)))
4379 return hr;
4383 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4384 * on an existing gl context, so there's no real need for recreation.
4386 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4388 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4390 TRACE("New params:\n");
4391 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4392 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4393 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4394 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4395 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4396 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4397 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4398 TRACE("device_window %p\n", swapchain_desc->device_window);
4399 TRACE("windowed %#x\n", swapchain_desc->windowed);
4400 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4401 if (swapchain_desc->enable_auto_depth_stencil)
4402 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4403 TRACE("flags %#x\n", swapchain_desc->flags);
4404 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4405 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4406 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4408 /* No special treatment of these parameters. Just store them */
4409 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4410 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4411 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4412 swapchain->desc.flags = swapchain_desc->flags;
4413 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4414 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4415 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4417 /* What to do about these? */
4418 if (swapchain_desc->backbuffer_count
4419 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4420 FIXME("Cannot change the back buffer count yet.\n");
4422 if (swapchain_desc->device_window
4423 && swapchain_desc->device_window != swapchain->desc.device_window)
4425 TRACE("Changing the device window from %p to %p.\n",
4426 swapchain->desc.device_window, swapchain_desc->device_window);
4427 swapchain->desc.device_window = swapchain_desc->device_window;
4428 swapchain->device_window = swapchain_desc->device_window;
4429 wined3d_swapchain_set_window(swapchain, NULL);
4432 if (mode)
4434 DisplayModeChanged = TRUE;
4435 m = *mode;
4437 else if (swapchain_desc->windowed)
4439 m = swapchain->original_mode;
4441 else
4443 m.width = swapchain_desc->backbuffer_width;
4444 m.height = swapchain_desc->backbuffer_height;
4445 m.refresh_rate = swapchain_desc->refresh_rate;
4446 m.format_id = swapchain_desc->backbuffer_format;
4447 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4450 if (!backbuffer_width || !backbuffer_height)
4452 /* The application is requesting that either the swapchain width or
4453 * height be set to the corresponding dimension in the window's
4454 * client rect. */
4456 RECT client_rect;
4458 if (!swapchain_desc->windowed)
4459 return WINED3DERR_INVALIDCALL;
4461 if (!GetClientRect(swapchain->device_window, &client_rect))
4463 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
4464 return WINED3DERR_INVALIDCALL;
4467 if (!backbuffer_width)
4468 backbuffer_width = client_rect.right;
4470 if (!backbuffer_height)
4471 backbuffer_height = client_rect.bottom;
4474 if (backbuffer_width != swapchain->desc.backbuffer_width
4475 || backbuffer_height != swapchain->desc.backbuffer_height)
4477 if (!swapchain_desc->windowed)
4478 DisplayModeChanged = TRUE;
4480 swapchain->desc.backbuffer_width = backbuffer_width;
4481 swapchain->desc.backbuffer_height = backbuffer_height;
4482 update_desc = TRUE;
4485 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
4486 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
4488 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
4489 update_desc = TRUE;
4492 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
4493 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
4495 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
4496 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
4497 update_desc = TRUE;
4500 if (update_desc)
4502 UINT i;
4504 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
4505 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4506 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4507 return hr;
4509 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4511 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
4512 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4513 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4514 return hr;
4518 if (device->auto_depth_stencil_view)
4520 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4521 device->auto_depth_stencil_view = NULL;
4523 if (swapchain->desc.enable_auto_depth_stencil)
4525 struct wined3d_resource_desc surface_desc;
4526 struct wined3d_surface *surface;
4528 TRACE("Creating the depth stencil buffer\n");
4530 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
4531 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
4532 surface_desc.multisample_type = swapchain->desc.multisample_type;
4533 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
4534 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4535 surface_desc.pool = WINED3D_POOL_DEFAULT;
4536 surface_desc.width = swapchain->desc.backbuffer_width;
4537 surface_desc.height = swapchain->desc.backbuffer_height;
4538 surface_desc.depth = 1;
4539 surface_desc.size = 0;
4541 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4542 device->device_parent, &surface_desc, &surface)))
4544 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4545 return WINED3DERR_INVALIDCALL;
4548 hr = wined3d_rendertarget_view_create_from_surface(surface,
4549 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4550 wined3d_surface_decref(surface);
4551 if (FAILED(hr))
4553 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4554 return hr;
4557 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4560 if (device->back_buffer_view)
4562 wined3d_rendertarget_view_decref(device->back_buffer_view);
4563 device->back_buffer_view = NULL;
4565 if (swapchain->desc.backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
4566 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
4567 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4569 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4570 return hr;
4573 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4574 || DisplayModeChanged)
4576 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4578 WARN("Failed to set display mode, hr %#x.\n", hr);
4579 return WINED3DERR_INVALIDCALL;
4582 if (!swapchain_desc->windowed)
4584 if (swapchain->desc.windowed)
4586 HWND focus_window = device->create_parms.focus_window;
4587 if (!focus_window)
4588 focus_window = swapchain_desc->device_window;
4589 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4591 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4592 return hr;
4595 /* switch from windowed to fs */
4596 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4597 swapchain_desc->backbuffer_width,
4598 swapchain_desc->backbuffer_height);
4600 else
4602 /* Fullscreen -> fullscreen mode change */
4603 MoveWindow(swapchain->device_window, 0, 0,
4604 swapchain_desc->backbuffer_width,
4605 swapchain_desc->backbuffer_height,
4606 TRUE);
4608 swapchain->d3d_mode = m;
4610 else if (!swapchain->desc.windowed)
4612 /* Fullscreen -> windowed switch */
4613 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4614 wined3d_device_release_focus_window(device);
4616 swapchain->desc.windowed = swapchain_desc->windowed;
4618 else if (!swapchain_desc->windowed)
4620 DWORD style = device->style;
4621 DWORD exStyle = device->exStyle;
4622 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4623 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4624 * Reset to clear up their mess. Guild Wars also loses the device during that.
4626 device->style = 0;
4627 device->exStyle = 0;
4628 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4629 swapchain_desc->backbuffer_width,
4630 swapchain_desc->backbuffer_height);
4631 device->style = style;
4632 device->exStyle = exStyle;
4635 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4637 if (reset_state)
4639 TRACE("Resetting stateblock.\n");
4640 if (device->recording)
4642 wined3d_stateblock_decref(device->recording);
4643 device->recording = NULL;
4645 wined3d_cs_emit_reset_state(device->cs);
4646 state_cleanup(&device->state);
4648 if (device->d3d_initialized)
4649 delete_opengl_contexts(device, swapchain);
4651 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4652 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4653 ERR("Failed to initialize device state, hr %#x.\n", hr);
4654 device->update_state = &device->state;
4656 device_init_swapchain_state(device, swapchain);
4658 else if (device->back_buffer_view)
4660 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4661 struct wined3d_state *state = &device->state;
4663 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4665 /* Note the min_z / max_z is not reset. */
4666 state->viewport.x = 0;
4667 state->viewport.y = 0;
4668 state->viewport.width = view->width;
4669 state->viewport.height = view->height;
4670 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4672 state->scissor_rect.top = 0;
4673 state->scissor_rect.left = 0;
4674 state->scissor_rect.right = view->width;
4675 state->scissor_rect.bottom = view->height;
4676 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4679 swapchain_update_render_to_fbo(swapchain);
4680 swapchain_update_draw_bindings(swapchain);
4682 if (reset_state && device->d3d_initialized)
4683 hr = create_primary_opengl_context(device, swapchain);
4685 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4686 * first use
4688 return hr;
4691 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4693 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4695 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4697 return WINED3D_OK;
4701 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4702 struct wined3d_device_creation_parameters *parameters)
4704 TRACE("device %p, parameters %p.\n", device, parameters);
4706 *parameters = device->create_parms;
4709 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4710 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4712 struct wined3d_swapchain *swapchain;
4714 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4715 device, swapchain_idx, flags, ramp);
4717 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4718 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4721 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4722 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4724 struct wined3d_swapchain *swapchain;
4726 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4727 device, swapchain_idx, ramp);
4729 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4730 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4733 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4735 TRACE("device %p, resource %p.\n", device, resource);
4737 list_add_head(&device->resources, &resource->resource_list_entry);
4740 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4742 TRACE("device %p, resource %p.\n", device, resource);
4744 list_remove(&resource->resource_list_entry);
4747 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4749 enum wined3d_resource_type type = resource->type;
4750 unsigned int i;
4752 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4754 context_resource_released(device, resource, type);
4756 switch (type)
4758 case WINED3D_RTYPE_SURFACE:
4760 struct wined3d_surface *surface = surface_from_resource(resource);
4762 if (!device->d3d_initialized) break;
4764 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4766 if (wined3d_rendertarget_view_get_surface(device->fb.render_targets[i]) == surface)
4768 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4769 device->fb.render_targets[i] = NULL;
4773 if (wined3d_rendertarget_view_get_surface(device->fb.depth_stencil) == surface)
4775 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4776 device->fb.depth_stencil = NULL;
4779 break;
4781 case WINED3D_RTYPE_TEXTURE:
4782 case WINED3D_RTYPE_CUBE_TEXTURE:
4783 case WINED3D_RTYPE_VOLUME_TEXTURE:
4784 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4786 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4788 if (device->state.textures[i] == texture)
4790 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4791 device->state.textures[i] = NULL;
4794 if (device->recording && device->update_state->textures[i] == texture)
4796 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4797 texture, device->recording, i);
4798 device->update_state->textures[i] = NULL;
4801 break;
4803 case WINED3D_RTYPE_BUFFER:
4805 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4807 for (i = 0; i < MAX_STREAMS; ++i)
4809 if (device->state.streams[i].buffer == buffer)
4811 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4812 device->state.streams[i].buffer = NULL;
4815 if (device->recording && device->update_state->streams[i].buffer == buffer)
4817 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4818 buffer, device->recording, i);
4819 device->update_state->streams[i].buffer = NULL;
4823 if (device->state.index_buffer == buffer)
4825 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4826 device->state.index_buffer = NULL;
4829 if (device->recording && device->update_state->index_buffer == buffer)
4831 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4832 buffer, device->recording);
4833 device->update_state->index_buffer = NULL;
4836 break;
4838 default:
4839 break;
4842 /* Remove the resource from the resourceStore */
4843 device_resource_remove(device, resource);
4845 TRACE("Resource released.\n");
4848 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4850 struct wined3d_resource *resource;
4852 TRACE("device %p, dc %p.\n", device, dc);
4854 if (!dc)
4855 return NULL;
4857 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4859 if (resource->type == WINED3D_RTYPE_SURFACE)
4861 struct wined3d_surface *s = surface_from_resource(resource);
4863 if (s->hDC == dc)
4865 TRACE("Found surface %p for dc %p.\n", s, dc);
4866 return s;
4871 return NULL;
4874 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4876 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4878 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4881 static const struct wine_rb_functions wined3d_sampler_rb_functions =
4883 wined3d_rb_alloc,
4884 wined3d_rb_realloc,
4885 wined3d_rb_free,
4886 wined3d_sampler_compare,
4889 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4890 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4891 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4893 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4894 const struct fragment_pipeline *fragment_pipeline;
4895 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4896 unsigned int i;
4897 HRESULT hr;
4899 device->ref = 1;
4900 device->wined3d = wined3d;
4901 wined3d_incref(device->wined3d);
4902 device->adapter = wined3d->adapter_count ? adapter : NULL;
4903 device->device_parent = device_parent;
4904 list_init(&device->resources);
4905 list_init(&device->shaders);
4906 device->surface_alignment = surface_alignment;
4908 /* Save the creation parameters. */
4909 device->create_parms.adapter_idx = adapter_idx;
4910 device->create_parms.device_type = device_type;
4911 device->create_parms.focus_window = focus_window;
4912 device->create_parms.flags = flags;
4914 device->shader_backend = adapter->shader_backend;
4916 vertex_pipeline = adapter->vertex_pipe;
4918 fragment_pipeline = adapter->fragment_pipe;
4920 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
4922 ERR("Failed to initialize sampler rbtree.\n");
4923 return E_OUTOFMEMORY;
4926 if (vertex_pipeline->vp_states && fragment_pipeline->states
4927 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4928 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4929 fragment_pipeline, misc_state_template)))
4931 ERR("Failed to compile state table, hr %#x.\n", hr);
4932 wine_rb_destroy(&device->samplers, NULL, NULL);
4933 wined3d_decref(device->wined3d);
4934 return hr;
4937 device->blitter = adapter->blitter;
4939 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
4940 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4942 ERR("Failed to initialize device state, hr %#x.\n", hr);
4943 goto err;
4945 device->update_state = &device->state;
4947 if (!(device->cs = wined3d_cs_create(device)))
4949 WARN("Failed to create command stream.\n");
4950 state_cleanup(&device->state);
4951 hr = E_FAIL;
4952 goto err;
4955 return WINED3D_OK;
4957 err:
4958 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4960 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4962 wine_rb_destroy(&device->samplers, NULL, NULL);
4963 wined3d_decref(device->wined3d);
4964 return hr;
4968 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4970 DWORD rep = device->StateTable[state].representative;
4971 struct wined3d_context *context;
4972 DWORD idx;
4973 BYTE shift;
4974 UINT i;
4976 for (i = 0; i < device->context_count; ++i)
4978 context = device->contexts[i];
4979 if(isStateDirty(context, rep)) continue;
4981 context->dirtyArray[context->numDirtyEntries++] = rep;
4982 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4983 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4984 context->isStateDirty[idx] |= (1 << shift);
4988 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4989 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4991 if (device->filter_messages && message != WM_DISPLAYCHANGE)
4993 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4994 window, message, wparam, lparam);
4995 if (unicode)
4996 return DefWindowProcW(window, message, wparam, lparam);
4997 else
4998 return DefWindowProcA(window, message, wparam, lparam);
5001 if (message == WM_DESTROY)
5003 TRACE("unregister window %p.\n", window);
5004 wined3d_unregister_window(window);
5006 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5007 ERR("Window %p is not the focus window for device %p.\n", window, device);
5009 else if (message == WM_DISPLAYCHANGE)
5011 device->device_parent->ops->mode_changed(device->device_parent);
5013 else if (message == WM_ACTIVATEAPP)
5015 UINT i;
5017 for (i = 0; i < device->swapchain_count; i++)
5018 wined3d_swapchain_activate(device->swapchains[i], wparam);
5020 device->device_parent->ops->activate(device->device_parent, wparam);
5022 else if (message == WM_SYSCOMMAND)
5024 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5026 if (unicode)
5027 DefWindowProcW(window, message, wparam, lparam);
5028 else
5029 DefWindowProcA(window, message, wparam, lparam);
5033 if (unicode)
5034 return CallWindowProcW(proc, window, message, wparam, lparam);
5035 else
5036 return CallWindowProcA(proc, window, message, wparam, lparam);