include: Add D3D11_QUERY_MISC_FLAG declaration.
[wine.git] / dlls / wined3d / device.c
blob087dc335abcb8a7d4e1c5d245d8d2cf17051c74f
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 context = context_acquire(device, target);
304 if (!context->valid)
306 context_release(context);
307 WARN("Invalid context, skipping clear.\n");
308 return;
310 gl_info = context->gl_info;
312 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
313 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
314 * for the cleared parts, and the untouched parts.
316 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
317 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
318 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
319 * checking all this if the dest surface is in the drawable anyway. */
320 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
322 for (i = 0; i < rt_count; ++i)
324 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
325 if (rt)
326 surface_load_location(rt, context, rt->container->resource.draw_binding);
330 if (target)
332 render_offscreen = context->render_offscreen;
333 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
335 else
337 render_offscreen = TRUE;
338 drawable_width = depth_stencil->pow2Width;
339 drawable_height = depth_stencil->pow2Height;
342 if (flags & WINED3DCLEAR_ZBUFFER)
344 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
346 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
347 device_switch_onscreen_ds(device, context, depth_stencil);
348 prepare_ds_clear(depth_stencil, context, location,
349 draw_rect, rect_count, clear_rect, &ds_rect);
352 if (!context_apply_clear_state(context, device, rt_count, fb))
354 context_release(context);
355 WARN("Failed to apply clear state, skipping clear.\n");
356 return;
359 /* Only set the values up once, as they are not changing. */
360 if (flags & WINED3DCLEAR_STENCIL)
362 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
364 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
365 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
367 gl_info->gl_ops.gl.p_glStencilMask(~0U);
368 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
369 gl_info->gl_ops.gl.p_glClearStencil(stencil);
370 checkGLcall("glClearStencil");
371 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
374 if (flags & WINED3DCLEAR_ZBUFFER)
376 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
378 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
380 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
382 gl_info->gl_ops.gl.p_glClearDepth(depth);
383 checkGLcall("glClearDepth");
384 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
387 if (flags & WINED3DCLEAR_TARGET)
389 for (i = 0; i < rt_count; ++i)
391 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
393 if (rt)
395 surface_validate_location(rt, rt->container->resource.draw_binding);
396 surface_invalidate_location(rt, ~rt->container->resource.draw_binding);
400 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
401 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
402 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
403 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
404 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
405 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
406 checkGLcall("glClearColor");
407 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
410 if (!clear_rect)
412 if (render_offscreen)
414 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
415 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
417 else
419 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
420 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
422 checkGLcall("glScissor");
423 gl_info->gl_ops.gl.p_glClear(clear_mask);
424 checkGLcall("glClear");
426 else
428 RECT current_rect;
430 /* Now process each rect in turn. */
431 for (i = 0; i < rect_count; ++i)
433 /* Note that GL uses lower left, width/height. */
434 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
436 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
437 wine_dbgstr_rect(&clear_rect[i]),
438 wine_dbgstr_rect(&current_rect));
440 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
441 * The rectangle is not cleared, no error is returned, but further rectangles are
442 * still cleared if they are valid. */
443 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
445 TRACE("Rectangle with negative dimensions, ignoring.\n");
446 continue;
449 if (render_offscreen)
451 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
452 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
454 else
456 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
457 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
459 checkGLcall("glScissor");
461 gl_info->gl_ops.gl.p_glClear(clear_mask);
462 checkGLcall("glClear");
466 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
467 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
468 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
470 context_release(context);
473 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
475 ULONG refcount = InterlockedIncrement(&device->ref);
477 TRACE("%p increasing refcount to %u.\n", device, refcount);
479 return refcount;
482 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
484 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
486 ERR("Leftover sampler %p.\n", sampler);
489 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
491 ULONG refcount = InterlockedDecrement(&device->ref);
493 TRACE("%p decreasing refcount to %u.\n", device, refcount);
495 if (!refcount)
497 UINT i;
499 wined3d_cs_destroy(device->cs);
501 if (device->recording && wined3d_stateblock_decref(device->recording))
502 FIXME("Something's still holding the recording stateblock.\n");
503 device->recording = NULL;
505 state_cleanup(&device->state);
507 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
509 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
510 device->multistate_funcs[i] = NULL;
513 if (!list_empty(&device->resources))
515 struct wined3d_resource *resource;
517 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
519 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
521 FIXME("Leftover resource %p with type %s (%#x).\n",
522 resource, debug_d3dresourcetype(resource->type), resource->type);
526 if (device->contexts)
527 ERR("Context array not freed!\n");
528 if (device->hardwareCursor)
529 DestroyCursor(device->hardwareCursor);
530 device->hardwareCursor = 0;
532 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
534 wined3d_decref(device->wined3d);
535 device->wined3d = NULL;
536 HeapFree(GetProcessHeap(), 0, device);
537 TRACE("Freed device %p.\n", device);
540 return refcount;
543 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
545 TRACE("device %p.\n", device);
547 return device->swapchain_count;
550 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
552 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
554 if (swapchain_idx >= device->swapchain_count)
556 WARN("swapchain_idx %u >= swapchain_count %u.\n",
557 swapchain_idx, device->swapchain_count);
558 return NULL;
561 return device->swapchains[swapchain_idx];
564 static void device_load_logo(struct wined3d_device *device, const char *filename)
566 struct wined3d_color_key color_key;
567 struct wined3d_resource_desc desc;
568 struct wined3d_surface *surface;
569 HBITMAP hbm;
570 BITMAP bm;
571 HRESULT hr;
572 HDC dcb = NULL, dcs = NULL;
574 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
575 if(hbm)
577 GetObjectA(hbm, sizeof(BITMAP), &bm);
578 dcb = CreateCompatibleDC(NULL);
579 if(!dcb) goto out;
580 SelectObject(dcb, hbm);
582 else
584 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
585 * couldn't be loaded
587 memset(&bm, 0, sizeof(bm));
588 bm.bmWidth = 32;
589 bm.bmHeight = 32;
592 desc.resource_type = WINED3D_RTYPE_TEXTURE;
593 desc.format = WINED3DFMT_B5G6R5_UNORM;
594 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
595 desc.multisample_quality = 0;
596 desc.usage = WINED3DUSAGE_DYNAMIC;
597 desc.pool = WINED3D_POOL_DEFAULT;
598 desc.width = bm.bmWidth;
599 desc.height = bm.bmHeight;
600 desc.depth = 1;
601 desc.size = 0;
602 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
603 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
605 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
606 goto out;
608 surface = surface_from_resource(wined3d_texture_get_sub_resource(device->logo_texture, 0));
610 if (dcb)
612 if (FAILED(hr = wined3d_surface_getdc(surface, &dcs)))
613 goto out;
614 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
615 wined3d_surface_releasedc(surface, dcs);
617 color_key.color_space_low_value = 0;
618 color_key.color_space_high_value = 0;
619 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
621 else
623 const RECT rect = {0, 0, surface->resource.width, surface->resource.height};
624 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
626 /* Fill the surface with a white color to show that wined3d is there */
627 surface_color_fill(surface, &rect, &c);
630 out:
631 if (dcb) DeleteDC(dcb);
632 if (hbm) DeleteObject(hbm);
635 /* Context activation is done by the caller. */
636 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
638 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
639 unsigned int i, j, count;
640 /* Under DirectX you can sample even if no texture is bound, whereas
641 * OpenGL will only allow that when a valid texture is bound.
642 * We emulate this by creating dummy textures and binding them
643 * to each texture stage when the currently set D3D texture is NULL. */
645 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
646 for (i = 0; i < count; ++i)
648 DWORD color = 0x000000ff;
650 /* Make appropriate texture active */
651 context_active_texture(context, gl_info, i);
653 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
654 checkGLcall("glGenTextures");
655 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
657 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
658 checkGLcall("glBindTexture");
660 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
661 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
662 checkGLcall("glTexImage2D");
664 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
666 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
667 checkGLcall("glGenTextures");
668 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
670 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
671 checkGLcall("glBindTexture");
673 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
674 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
675 checkGLcall("glTexImage2D");
678 if (gl_info->supported[EXT_TEXTURE3D])
680 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
681 checkGLcall("glGenTextures");
682 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
684 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
685 checkGLcall("glBindTexture");
687 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
688 checkGLcall("glTexImage3D");
691 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
693 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
694 checkGLcall("glGenTextures");
695 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
697 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
698 checkGLcall("glBindTexture");
700 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
702 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
703 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
704 checkGLcall("glTexImage2D");
710 /* Context activation is done by the caller. */
711 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
713 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
715 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
717 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
718 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
721 if (gl_info->supported[EXT_TEXTURE3D])
723 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
724 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
727 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
729 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
730 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
733 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
734 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
736 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
737 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
738 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
739 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
742 static LONG fullscreen_style(LONG style)
744 /* Make sure the window is managed, otherwise we won't get keyboard input. */
745 style |= WS_POPUP | WS_SYSMENU;
746 style &= ~(WS_CAPTION | WS_THICKFRAME);
748 return style;
751 static LONG fullscreen_exstyle(LONG exstyle)
753 /* Filter out window decorations. */
754 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
756 return exstyle;
759 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
761 BOOL filter_messages;
762 LONG style, exstyle;
764 TRACE("Setting up window %p for fullscreen mode.\n", window);
766 if (device->style || device->exStyle)
768 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
769 window, device->style, device->exStyle);
772 device->style = GetWindowLongW(window, GWL_STYLE);
773 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
775 style = fullscreen_style(device->style);
776 exstyle = fullscreen_exstyle(device->exStyle);
778 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
779 device->style, device->exStyle, style, exstyle);
781 filter_messages = device->filter_messages;
782 device->filter_messages = TRUE;
784 SetWindowLongW(window, GWL_STYLE, style);
785 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
786 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
788 device->filter_messages = filter_messages;
791 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
793 BOOL filter_messages;
794 LONG style, exstyle;
796 if (!device->style && !device->exStyle) return;
798 style = GetWindowLongW(window, GWL_STYLE);
799 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
801 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
802 * application, and we want to ignore them in the test below, since it's
803 * not the application's fault that they changed. Additionally, we want to
804 * preserve the current status of these flags (i.e. don't restore them) to
805 * more closely emulate the behavior of Direct3D, which leaves these flags
806 * alone when returning to windowed mode. */
807 device->style ^= (device->style ^ style) & WS_VISIBLE;
808 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
810 TRACE("Restoring window style of window %p to %08x, %08x.\n",
811 window, device->style, device->exStyle);
813 filter_messages = device->filter_messages;
814 device->filter_messages = TRUE;
816 /* Only restore the style if the application didn't modify it during the
817 * fullscreen phase. Some applications change it before calling Reset()
818 * when switching between windowed and fullscreen modes (HL2), some
819 * depend on the original style (Eve Online). */
820 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
822 SetWindowLongW(window, GWL_STYLE, device->style);
823 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
825 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
827 device->filter_messages = filter_messages;
829 /* Delete the old values. */
830 device->style = 0;
831 device->exStyle = 0;
834 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
836 TRACE("device %p, window %p.\n", device, window);
838 if (!wined3d_register_window(window, device))
840 ERR("Failed to register window %p.\n", window);
841 return E_FAIL;
844 InterlockedExchangePointer((void **)&device->focus_window, window);
845 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
847 return WINED3D_OK;
850 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
852 TRACE("device %p.\n", device);
854 if (device->focus_window) wined3d_unregister_window(device->focus_window);
855 InterlockedExchangePointer((void **)&device->focus_window, NULL);
858 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
860 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
861 unsigned int i;
863 if (device->fb.render_targets)
865 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
867 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
869 if (device->back_buffer_view)
870 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
873 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
874 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
877 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
878 struct wined3d_swapchain_desc *swapchain_desc)
880 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
881 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
882 struct wined3d_swapchain *swapchain = NULL;
883 struct wined3d_context *context;
884 DWORD clear_flags = 0;
885 HRESULT hr;
887 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
889 if (device->d3d_initialized)
890 return WINED3DERR_INVALIDCALL;
891 if (device->wined3d->flags & WINED3D_NO3D)
892 return WINED3DERR_INVALIDCALL;
894 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
895 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
897 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
898 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
900 TRACE("Shader private data couldn't be allocated\n");
901 goto err_out;
903 if (FAILED(hr = device->blitter->alloc_private(device)))
905 TRACE("Blitter private data couldn't be allocated\n");
906 goto err_out;
909 /* Setup the implicit swapchain. This also initializes a context. */
910 TRACE("Creating implicit swapchain\n");
911 hr = device->device_parent->ops->create_swapchain(device->device_parent,
912 swapchain_desc, &swapchain);
913 if (FAILED(hr))
915 WARN("Failed to create implicit swapchain\n");
916 goto err_out;
919 if (swapchain_desc->backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
920 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
921 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
923 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
924 goto err_out;
927 device->swapchain_count = 1;
928 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
929 if (!device->swapchains)
931 ERR("Out of memory!\n");
932 goto err_out;
934 device->swapchains[0] = swapchain;
935 device_init_swapchain_state(device, swapchain);
937 context = context_acquire(device,
938 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)));
940 create_dummy_textures(device, context);
942 device->contexts[0]->last_was_rhw = 0;
944 switch (wined3d_settings.offscreen_rendering_mode)
946 case ORM_FBO:
947 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
948 break;
950 case ORM_BACKBUFFER:
952 if (context_get_current()->aux_buffers > 0)
954 TRACE("Using auxiliary buffer for offscreen rendering\n");
955 device->offscreenBuffer = GL_AUX0;
957 else
959 TRACE("Using back buffer for offscreen rendering\n");
960 device->offscreenBuffer = GL_BACK;
965 TRACE("All defaults now set up, leaving 3D init.\n");
967 context_release(context);
969 /* Clear the screen */
970 if (swapchain->back_buffers && swapchain->back_buffers[0])
971 clear_flags |= WINED3DCLEAR_TARGET;
972 if (swapchain_desc->enable_auto_depth_stencil)
973 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
974 if (clear_flags)
975 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
977 device->d3d_initialized = TRUE;
979 if (wined3d_settings.logo)
980 device_load_logo(device, wined3d_settings.logo);
981 return WINED3D_OK;
983 err_out:
984 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
985 HeapFree(GetProcessHeap(), 0, device->swapchains);
986 device->swapchain_count = 0;
987 if (device->back_buffer_view)
988 wined3d_rendertarget_view_decref(device->back_buffer_view);
989 if (swapchain)
990 wined3d_swapchain_decref(swapchain);
991 if (device->blit_priv)
992 device->blitter->free_private(device);
993 if (device->shader_priv)
994 device->shader_backend->shader_free_private(device);
996 return hr;
999 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1000 struct wined3d_swapchain_desc *swapchain_desc)
1002 struct wined3d_swapchain *swapchain = NULL;
1003 HRESULT hr;
1005 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1007 /* Setup the implicit swapchain */
1008 TRACE("Creating implicit swapchain\n");
1009 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1010 swapchain_desc, &swapchain);
1011 if (FAILED(hr))
1013 WARN("Failed to create implicit swapchain\n");
1014 goto err_out;
1017 device->swapchain_count = 1;
1018 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1019 if (!device->swapchains)
1021 ERR("Out of memory!\n");
1022 goto err_out;
1024 device->swapchains[0] = swapchain;
1025 return WINED3D_OK;
1027 err_out:
1028 wined3d_swapchain_decref(swapchain);
1029 return hr;
1032 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1034 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1036 wined3d_sampler_decref(sampler);
1039 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1041 struct wined3d_resource *resource, *cursor;
1042 const struct wined3d_gl_info *gl_info;
1043 struct wined3d_context *context;
1044 struct wined3d_surface *surface;
1045 UINT i;
1047 TRACE("device %p.\n", device);
1049 if (!device->d3d_initialized)
1050 return WINED3DERR_INVALIDCALL;
1052 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1053 * it was created. Thus make sure a context is active for the glDelete* calls
1055 context = context_acquire(device, NULL);
1056 gl_info = context->gl_info;
1058 if (device->logo_texture)
1059 wined3d_texture_decref(device->logo_texture);
1060 if (device->cursor_texture)
1061 wined3d_texture_decref(device->cursor_texture);
1063 state_unbind_resources(&device->state);
1065 /* Unload resources */
1066 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1068 TRACE("Unloading resource %p.\n", resource);
1070 resource->resource_ops->resource_unload(resource);
1073 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1075 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1076 * private data, it might contain opengl pointers
1078 if (device->depth_blt_texture)
1080 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1081 device->depth_blt_texture = 0;
1084 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1085 device->blitter->free_private(device);
1086 device->shader_backend->shader_free_private(device);
1087 destroy_dummy_textures(device, gl_info);
1089 /* Release the context again as soon as possible. In particular,
1090 * releasing the render target views below may release the last reference
1091 * to the swapchain associated with this context, which in turn will
1092 * destroy the context. */
1093 context_release(context);
1095 /* Release the buffers (with sanity checks)*/
1096 if (device->onscreen_depth_stencil)
1098 surface = device->onscreen_depth_stencil;
1099 device->onscreen_depth_stencil = NULL;
1100 wined3d_surface_decref(surface);
1103 if (device->fb.depth_stencil)
1105 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1107 TRACE("Releasing depth/stencil view %p.\n", view);
1109 device->fb.depth_stencil = NULL;
1110 wined3d_rendertarget_view_decref(view);
1113 if (device->auto_depth_stencil_view)
1115 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1117 device->auto_depth_stencil_view = NULL;
1118 if (wined3d_rendertarget_view_decref(view))
1119 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1122 for (i = 0; i < gl_info->limits.buffers; ++i)
1124 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1126 if (device->back_buffer_view)
1128 wined3d_rendertarget_view_decref(device->back_buffer_view);
1129 device->back_buffer_view = NULL;
1132 for (i = 0; i < device->swapchain_count; ++i)
1134 TRACE("Releasing the implicit swapchain %u.\n", i);
1135 if (wined3d_swapchain_decref(device->swapchains[i]))
1136 FIXME("Something's still holding the implicit swapchain.\n");
1139 HeapFree(GetProcessHeap(), 0, device->swapchains);
1140 device->swapchains = NULL;
1141 device->swapchain_count = 0;
1143 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1144 device->fb.render_targets = NULL;
1146 device->d3d_initialized = FALSE;
1148 return WINED3D_OK;
1151 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1153 unsigned int i;
1155 for (i = 0; i < device->swapchain_count; ++i)
1157 TRACE("Releasing the implicit swapchain %u.\n", i);
1158 if (wined3d_swapchain_decref(device->swapchains[i]))
1159 FIXME("Something's still holding the implicit swapchain.\n");
1162 HeapFree(GetProcessHeap(), 0, device->swapchains);
1163 device->swapchains = NULL;
1164 device->swapchain_count = 0;
1165 return WINED3D_OK;
1168 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1169 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1170 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1172 * There is no way to deactivate thread safety once it is enabled.
1174 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1176 TRACE("device %p.\n", device);
1178 /* For now just store the flag (needed in case of ddraw). */
1179 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1182 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1184 TRACE("device %p.\n", device);
1186 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1187 wine_dbgstr_longlong(device->adapter->vram_bytes),
1188 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1189 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1191 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1194 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1195 struct wined3d_buffer *buffer, UINT offset)
1197 struct wined3d_stream_output *stream;
1198 struct wined3d_buffer *prev_buffer;
1200 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1202 if (idx >= MAX_STREAM_OUT)
1204 WARN("Invalid stream output %u.\n", idx);
1205 return;
1208 stream = &device->update_state->stream_output[idx];
1209 prev_buffer = stream->buffer;
1211 if (buffer)
1212 wined3d_buffer_incref(buffer);
1213 stream->buffer = buffer;
1214 stream->offset = offset;
1215 if (!device->recording)
1216 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1217 if (prev_buffer)
1218 wined3d_buffer_decref(prev_buffer);
1221 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1222 UINT idx, UINT *offset)
1224 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1226 if (idx >= MAX_STREAM_OUT)
1228 WARN("Invalid stream output %u.\n", idx);
1229 return NULL;
1232 *offset = device->state.stream_output[idx].offset;
1233 return device->state.stream_output[idx].buffer;
1236 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1237 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1239 struct wined3d_stream_state *stream;
1240 struct wined3d_buffer *prev_buffer;
1242 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1243 device, stream_idx, buffer, offset, stride);
1245 if (stream_idx >= MAX_STREAMS)
1247 WARN("Stream index %u out of range.\n", stream_idx);
1248 return WINED3DERR_INVALIDCALL;
1250 else if (offset & 0x3)
1252 WARN("Offset %u is not 4 byte aligned.\n", offset);
1253 return WINED3DERR_INVALIDCALL;
1256 stream = &device->update_state->streams[stream_idx];
1257 prev_buffer = stream->buffer;
1259 if (device->recording)
1260 device->recording->changed.streamSource |= 1u << stream_idx;
1262 if (prev_buffer == buffer
1263 && stream->stride == stride
1264 && stream->offset == offset)
1266 TRACE("Application is setting the old values over, nothing to do.\n");
1267 return WINED3D_OK;
1270 stream->buffer = buffer;
1271 if (buffer)
1273 stream->stride = stride;
1274 stream->offset = offset;
1275 wined3d_buffer_incref(buffer);
1278 if (!device->recording)
1279 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1280 if (prev_buffer)
1281 wined3d_buffer_decref(prev_buffer);
1283 return WINED3D_OK;
1286 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1287 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1289 const struct wined3d_stream_state *stream;
1291 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1292 device, stream_idx, buffer, offset, stride);
1294 if (stream_idx >= MAX_STREAMS)
1296 WARN("Stream index %u out of range.\n", stream_idx);
1297 return WINED3DERR_INVALIDCALL;
1300 stream = &device->state.streams[stream_idx];
1301 *buffer = stream->buffer;
1302 if (offset)
1303 *offset = stream->offset;
1304 *stride = stream->stride;
1306 return WINED3D_OK;
1309 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1311 struct wined3d_stream_state *stream;
1312 UINT old_flags, old_freq;
1314 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1316 /* Verify input. At least in d3d9 this is invalid. */
1317 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1319 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1320 return WINED3DERR_INVALIDCALL;
1322 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1324 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1325 return WINED3DERR_INVALIDCALL;
1327 if (!divider)
1329 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1330 return WINED3DERR_INVALIDCALL;
1333 stream = &device->update_state->streams[stream_idx];
1334 old_flags = stream->flags;
1335 old_freq = stream->frequency;
1337 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1338 stream->frequency = divider & 0x7fffff;
1340 if (device->recording)
1341 device->recording->changed.streamFreq |= 1u << stream_idx;
1342 else if (stream->frequency != old_freq || stream->flags != old_flags)
1343 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1345 return WINED3D_OK;
1348 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1349 UINT stream_idx, UINT *divider)
1351 const struct wined3d_stream_state *stream;
1353 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1355 stream = &device->state.streams[stream_idx];
1356 *divider = stream->flags | stream->frequency;
1358 TRACE("Returning %#x.\n", *divider);
1360 return WINED3D_OK;
1363 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1364 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1366 TRACE("device %p, state %s, matrix %p.\n",
1367 device, debug_d3dtstype(d3dts), matrix);
1368 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1369 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1370 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1371 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1373 /* Handle recording of state blocks. */
1374 if (device->recording)
1376 TRACE("Recording... not performing anything.\n");
1377 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1378 device->update_state->transforms[d3dts] = *matrix;
1379 return;
1382 /* If the new matrix is the same as the current one,
1383 * we cut off any further processing. this seems to be a reasonable
1384 * optimization because as was noticed, some apps (warcraft3 for example)
1385 * tend towards setting the same matrix repeatedly for some reason.
1387 * From here on we assume that the new matrix is different, wherever it matters. */
1388 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1390 TRACE("The application is setting the same matrix over again.\n");
1391 return;
1394 device->state.transforms[d3dts] = *matrix;
1395 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1398 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1399 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1401 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1403 *matrix = device->state.transforms[state];
1406 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1407 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1409 const struct wined3d_matrix *mat;
1410 struct wined3d_matrix temp;
1412 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1414 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1415 * below means it will be recorded in a state block change, but it
1416 * works regardless where it is recorded.
1417 * If this is found to be wrong, change to StateBlock. */
1418 if (state > HIGHEST_TRANSFORMSTATE)
1420 WARN("Unhandled transform state %#x.\n", state);
1421 return;
1424 mat = &device->update_state->transforms[state];
1425 multiply_matrix(&temp, mat, matrix);
1427 /* Apply change via set transform - will reapply to eg. lights this way. */
1428 wined3d_device_set_transform(device, state, &temp);
1431 /* Note lights are real special cases. Although the device caps state only
1432 * e.g. 8 are supported, you can reference any indexes you want as long as
1433 * that number max are enabled at any one point in time. Therefore since the
1434 * indices can be anything, we need a hashmap of them. However, this causes
1435 * stateblock problems. When capturing the state block, I duplicate the
1436 * hashmap, but when recording, just build a chain pretty much of commands to
1437 * be replayed. */
1438 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1439 UINT light_idx, const struct wined3d_light *light)
1441 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1442 struct wined3d_light_info *object = NULL;
1443 struct list *e;
1444 float rho;
1446 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1448 /* Check the parameter range. Need for speed most wanted sets junk lights
1449 * which confuse the GL driver. */
1450 if (!light)
1451 return WINED3DERR_INVALIDCALL;
1453 switch (light->type)
1455 case WINED3D_LIGHT_POINT:
1456 case WINED3D_LIGHT_SPOT:
1457 case WINED3D_LIGHT_GLSPOT:
1458 /* Incorrect attenuation values can cause the gl driver to crash.
1459 * Happens with Need for speed most wanted. */
1460 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1462 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1463 return WINED3DERR_INVALIDCALL;
1465 break;
1467 case WINED3D_LIGHT_DIRECTIONAL:
1468 case WINED3D_LIGHT_PARALLELPOINT:
1469 /* Ignores attenuation */
1470 break;
1472 default:
1473 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1474 return WINED3DERR_INVALIDCALL;
1477 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1479 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1480 if (object->OriginalIndex == light_idx)
1481 break;
1482 object = NULL;
1485 if (!object)
1487 TRACE("Adding new light\n");
1488 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1489 if (!object)
1490 return E_OUTOFMEMORY;
1492 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1493 object->glIndex = -1;
1494 object->OriginalIndex = light_idx;
1497 /* Initialize the object. */
1498 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1499 light_idx, light->type,
1500 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1501 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1502 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1503 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1504 light->direction.x, light->direction.y, light->direction.z);
1505 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1506 light->range, light->falloff, light->theta, light->phi);
1508 /* Update the live definitions if the light is currently assigned a glIndex. */
1509 if (object->glIndex != -1 && !device->recording)
1511 if (object->OriginalParms.type != light->type)
1512 device_invalidate_state(device, STATE_LIGHT_TYPE);
1513 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1516 /* Save away the information. */
1517 object->OriginalParms = *light;
1519 switch (light->type)
1521 case WINED3D_LIGHT_POINT:
1522 /* Position */
1523 object->position.x = light->position.x;
1524 object->position.y = light->position.y;
1525 object->position.z = light->position.z;
1526 object->position.w = 1.0f;
1527 object->cutoff = 180.0f;
1528 /* FIXME: Range */
1529 break;
1531 case WINED3D_LIGHT_DIRECTIONAL:
1532 /* Direction */
1533 object->direction.x = -light->direction.x;
1534 object->direction.y = -light->direction.y;
1535 object->direction.z = -light->direction.z;
1536 object->direction.w = 0.0f;
1537 object->exponent = 0.0f;
1538 object->cutoff = 180.0f;
1539 break;
1541 case WINED3D_LIGHT_SPOT:
1542 /* Position */
1543 object->position.x = light->position.x;
1544 object->position.y = light->position.y;
1545 object->position.z = light->position.z;
1546 object->position.w = 1.0f;
1548 /* Direction */
1549 object->direction.x = light->direction.x;
1550 object->direction.y = light->direction.y;
1551 object->direction.z = light->direction.z;
1552 object->direction.w = 0.0f;
1554 /* opengl-ish and d3d-ish spot lights use too different models
1555 * for the light "intensity" as a function of the angle towards
1556 * the main light direction, so we only can approximate very
1557 * roughly. However, spot lights are rather rarely used in games
1558 * (if ever used at all). Furthermore if still used, probably
1559 * nobody pays attention to such details. */
1560 if (!light->falloff)
1562 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1563 * equations have the falloff resp. exponent parameter as an
1564 * exponent, so the spot light lighting will always be 1.0 for
1565 * both of them, and we don't have to care for the rest of the
1566 * rather complex calculation. */
1567 object->exponent = 0.0f;
1569 else
1571 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1572 if (rho < 0.0001f)
1573 rho = 0.0001f;
1574 object->exponent = -0.3f / logf(cosf(rho / 2));
1577 if (object->exponent > 128.0f)
1578 object->exponent = 128.0f;
1580 object->cutoff = (float)(light->phi * 90 / M_PI);
1581 /* FIXME: Range */
1582 break;
1584 case WINED3D_LIGHT_PARALLELPOINT:
1585 object->position.x = light->position.x;
1586 object->position.y = light->position.y;
1587 object->position.z = light->position.z;
1588 object->position.w = 1.0f;
1589 break;
1591 default:
1592 FIXME("Unrecognized light type %#x.\n", light->type);
1595 return WINED3D_OK;
1598 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1599 UINT light_idx, struct wined3d_light *light)
1601 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1602 struct wined3d_light_info *light_info = NULL;
1603 struct list *e;
1605 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1607 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1609 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1610 if (light_info->OriginalIndex == light_idx)
1611 break;
1612 light_info = NULL;
1615 if (!light_info)
1617 TRACE("Light information requested but light not defined\n");
1618 return WINED3DERR_INVALIDCALL;
1621 *light = light_info->OriginalParms;
1622 return WINED3D_OK;
1625 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1627 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1628 struct wined3d_light_info *light_info = NULL;
1629 struct list *e;
1631 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1633 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1635 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1636 if (light_info->OriginalIndex == light_idx)
1637 break;
1638 light_info = NULL;
1640 TRACE("Found light %p.\n", light_info);
1642 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1643 if (!light_info)
1645 TRACE("Light enabled requested but light not defined, so defining one!\n");
1646 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1648 /* Search for it again! Should be fairly quick as near head of list. */
1649 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1651 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1652 if (light_info->OriginalIndex == light_idx)
1653 break;
1654 light_info = NULL;
1656 if (!light_info)
1658 FIXME("Adding default lights has failed dismally\n");
1659 return WINED3DERR_INVALIDCALL;
1663 if (!enable)
1665 if (light_info->glIndex != -1)
1667 if (!device->recording)
1669 device_invalidate_state(device, STATE_LIGHT_TYPE);
1670 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1673 device->update_state->lights[light_info->glIndex] = NULL;
1674 light_info->glIndex = -1;
1676 else
1678 TRACE("Light already disabled, nothing to do\n");
1680 light_info->enabled = FALSE;
1682 else
1684 light_info->enabled = TRUE;
1685 if (light_info->glIndex != -1)
1687 TRACE("Nothing to do as light was enabled\n");
1689 else
1691 unsigned int i;
1692 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1693 /* Find a free GL light. */
1694 for (i = 0; i < gl_info->limits.lights; ++i)
1696 if (!device->update_state->lights[i])
1698 device->update_state->lights[i] = light_info;
1699 light_info->glIndex = i;
1700 break;
1703 if (light_info->glIndex == -1)
1705 /* Our tests show that Windows returns D3D_OK in this situation, even with
1706 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1707 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1708 * as well for those lights.
1710 * TODO: Test how this affects rendering. */
1711 WARN("Too many concurrently active lights\n");
1712 return WINED3D_OK;
1715 /* i == light_info->glIndex */
1716 if (!device->recording)
1718 device_invalidate_state(device, STATE_LIGHT_TYPE);
1719 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1724 return WINED3D_OK;
1727 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1729 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1730 struct wined3d_light_info *light_info = NULL;
1731 struct list *e;
1733 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1735 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1737 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1738 if (light_info->OriginalIndex == light_idx)
1739 break;
1740 light_info = NULL;
1743 if (!light_info)
1745 TRACE("Light enabled state requested but light not defined.\n");
1746 return WINED3DERR_INVALIDCALL;
1748 /* true is 128 according to SetLightEnable */
1749 *enable = light_info->enabled ? 128 : 0;
1750 return WINED3D_OK;
1753 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1754 UINT plane_idx, const struct wined3d_vec4 *plane)
1756 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1758 /* Validate plane_idx. */
1759 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1761 TRACE("Application has requested clipplane this device doesn't support.\n");
1762 return WINED3DERR_INVALIDCALL;
1765 if (device->recording)
1766 device->recording->changed.clipplane |= 1u << plane_idx;
1768 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1770 TRACE("Application is setting old values over, nothing to do.\n");
1771 return WINED3D_OK;
1774 device->update_state->clip_planes[plane_idx] = *plane;
1776 if (!device->recording)
1777 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1779 return WINED3D_OK;
1782 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1783 UINT plane_idx, struct wined3d_vec4 *plane)
1785 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1787 /* Validate plane_idx. */
1788 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1790 TRACE("Application has requested clipplane this device doesn't support.\n");
1791 return WINED3DERR_INVALIDCALL;
1794 *plane = device->state.clip_planes[plane_idx];
1796 return WINED3D_OK;
1799 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1800 const struct wined3d_clip_status *clip_status)
1802 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1804 if (!clip_status)
1805 return WINED3DERR_INVALIDCALL;
1807 return WINED3D_OK;
1810 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1811 struct wined3d_clip_status *clip_status)
1813 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1815 if (!clip_status)
1816 return WINED3DERR_INVALIDCALL;
1818 return WINED3D_OK;
1821 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1823 TRACE("device %p, material %p.\n", device, material);
1825 device->update_state->material = *material;
1827 if (device->recording)
1828 device->recording->changed.material = TRUE;
1829 else
1830 wined3d_cs_emit_set_material(device->cs, material);
1833 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1835 TRACE("device %p, material %p.\n", device, material);
1837 *material = device->state.material;
1839 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1840 material->diffuse.r, material->diffuse.g,
1841 material->diffuse.b, material->diffuse.a);
1842 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1843 material->ambient.r, material->ambient.g,
1844 material->ambient.b, material->ambient.a);
1845 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1846 material->specular.r, material->specular.g,
1847 material->specular.b, material->specular.a);
1848 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1849 material->emissive.r, material->emissive.g,
1850 material->emissive.b, material->emissive.a);
1851 TRACE("power %.8e.\n", material->power);
1854 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1855 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1857 enum wined3d_format_id prev_format;
1858 struct wined3d_buffer *prev_buffer;
1860 TRACE("device %p, buffer %p, format %s.\n",
1861 device, buffer, debug_d3dformat(format_id));
1863 prev_buffer = device->update_state->index_buffer;
1864 prev_format = device->update_state->index_format;
1866 device->update_state->index_buffer = buffer;
1867 device->update_state->index_format = format_id;
1869 if (device->recording)
1870 device->recording->changed.indices = TRUE;
1872 if (prev_buffer == buffer && prev_format == format_id)
1873 return;
1875 if (buffer)
1876 wined3d_buffer_incref(buffer);
1877 if (!device->recording)
1878 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1879 if (prev_buffer)
1880 wined3d_buffer_decref(prev_buffer);
1883 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1884 enum wined3d_format_id *format)
1886 TRACE("device %p, format %p.\n", device, format);
1888 *format = device->state.index_format;
1889 return device->state.index_buffer;
1892 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1894 TRACE("device %p, base_index %d.\n", device, base_index);
1896 device->update_state->base_vertex_index = base_index;
1899 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1901 TRACE("device %p.\n", device);
1903 return device->state.base_vertex_index;
1906 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1908 TRACE("device %p, viewport %p.\n", device, viewport);
1909 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1910 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1912 device->update_state->viewport = *viewport;
1914 /* Handle recording of state blocks */
1915 if (device->recording)
1917 TRACE("Recording... not performing anything\n");
1918 device->recording->changed.viewport = TRUE;
1919 return;
1922 wined3d_cs_emit_set_viewport(device->cs, viewport);
1925 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1927 TRACE("device %p, viewport %p.\n", device, viewport);
1929 *viewport = device->state.viewport;
1932 static void resolve_depth_buffer(struct wined3d_state *state)
1934 struct wined3d_texture *texture = state->textures[0];
1935 struct wined3d_surface *depth_stencil, *surface;
1937 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1938 || !(texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1939 return;
1940 surface = surface_from_resource(texture->sub_resources[0]);
1941 if (!(depth_stencil = wined3d_rendertarget_view_get_surface(state->fb->depth_stencil)))
1942 return;
1944 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1947 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1948 enum wined3d_render_state state, DWORD value)
1950 DWORD old_value = device->state.render_states[state];
1952 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1954 device->update_state->render_states[state] = value;
1956 /* Handle recording of state blocks. */
1957 if (device->recording)
1959 TRACE("Recording... not performing anything.\n");
1960 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1961 return;
1964 /* Compared here and not before the assignment to allow proper stateblock recording. */
1965 if (value == old_value)
1966 TRACE("Application is setting the old value over, nothing to do.\n");
1967 else
1968 wined3d_cs_emit_set_render_state(device->cs, state, value);
1970 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1972 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1973 resolve_depth_buffer(&device->state);
1977 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1979 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1981 return device->state.render_states[state];
1984 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1985 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1987 DWORD old_value;
1989 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1990 device, sampler_idx, debug_d3dsamplerstate(state), value);
1992 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1993 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1995 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1997 WARN("Invalid sampler %u.\n", sampler_idx);
1998 return; /* Windows accepts overflowing this array ... we do not. */
2001 old_value = device->state.sampler_states[sampler_idx][state];
2002 device->update_state->sampler_states[sampler_idx][state] = value;
2004 /* Handle recording of state blocks. */
2005 if (device->recording)
2007 TRACE("Recording... not performing anything.\n");
2008 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2009 return;
2012 if (old_value == value)
2014 TRACE("Application is setting the old value over, nothing to do.\n");
2015 return;
2018 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2021 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2022 UINT sampler_idx, enum wined3d_sampler_state state)
2024 TRACE("device %p, sampler_idx %u, state %s.\n",
2025 device, sampler_idx, debug_d3dsamplerstate(state));
2027 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2028 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2030 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2032 WARN("Invalid sampler %u.\n", sampler_idx);
2033 return 0; /* Windows accepts overflowing this array ... we do not. */
2036 return device->state.sampler_states[sampler_idx][state];
2039 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2041 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2043 if (device->recording)
2044 device->recording->changed.scissorRect = TRUE;
2046 if (EqualRect(&device->update_state->scissor_rect, rect))
2048 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2049 return;
2051 CopyRect(&device->update_state->scissor_rect, rect);
2053 if (device->recording)
2055 TRACE("Recording... not performing anything.\n");
2056 return;
2059 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2062 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2064 TRACE("device %p, rect %p.\n", device, rect);
2066 *rect = device->state.scissor_rect;
2067 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2070 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2071 struct wined3d_vertex_declaration *declaration)
2073 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2075 TRACE("device %p, declaration %p.\n", device, declaration);
2077 if (device->recording)
2078 device->recording->changed.vertexDecl = TRUE;
2080 if (declaration == prev)
2081 return;
2083 if (declaration)
2084 wined3d_vertex_declaration_incref(declaration);
2085 device->update_state->vertex_declaration = declaration;
2086 if (!device->recording)
2087 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2088 if (prev)
2089 wined3d_vertex_declaration_decref(prev);
2092 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2094 TRACE("device %p.\n", device);
2096 return device->state.vertex_declaration;
2099 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2101 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2103 TRACE("device %p, shader %p.\n", device, shader);
2105 if (device->recording)
2106 device->recording->changed.vertexShader = TRUE;
2108 if (shader == prev)
2109 return;
2111 if (shader)
2112 wined3d_shader_incref(shader);
2113 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2114 if (!device->recording)
2115 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2116 if (prev)
2117 wined3d_shader_decref(prev);
2120 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2122 TRACE("device %p.\n", device);
2124 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2127 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2128 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2130 struct wined3d_buffer *prev;
2132 if (idx >= MAX_CONSTANT_BUFFERS)
2134 WARN("Invalid constant buffer index %u.\n", idx);
2135 return;
2138 prev = device->update_state->cb[type][idx];
2139 if (buffer == prev)
2140 return;
2142 if (buffer)
2143 wined3d_buffer_incref(buffer);
2144 device->update_state->cb[type][idx] = buffer;
2145 if (!device->recording)
2146 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2147 if (prev)
2148 wined3d_buffer_decref(prev);
2151 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2153 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2155 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2158 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2160 TRACE("device %p, idx %u.\n", device, idx);
2162 if (idx >= MAX_CONSTANT_BUFFERS)
2164 WARN("Invalid constant buffer index %u.\n", idx);
2165 return NULL;
2168 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2171 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2172 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2174 struct wined3d_shader_resource_view *prev;
2176 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2178 WARN("Invalid view index %u.\n", idx);
2179 return;
2182 prev = device->update_state->shader_resource_view[type][idx];
2183 if (view == prev)
2184 return;
2186 if (view)
2187 wined3d_shader_resource_view_incref(view);
2188 device->update_state->shader_resource_view[type][idx] = view;
2189 if (!device->recording)
2190 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2191 if (prev)
2192 wined3d_shader_resource_view_decref(prev);
2195 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2196 UINT idx, struct wined3d_shader_resource_view *view)
2198 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2200 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2203 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2204 UINT idx)
2206 TRACE("device %p, idx %u.\n", device, idx);
2208 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2210 WARN("Invalid view index %u.\n", idx);
2211 return NULL;
2214 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2217 static void wined3d_device_set_sampler(struct wined3d_device *device,
2218 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2220 struct wined3d_sampler *prev;
2222 if (idx >= MAX_SAMPLER_OBJECTS)
2224 WARN("Invalid sampler index %u.\n", idx);
2225 return;
2228 prev = device->update_state->sampler[type][idx];
2229 if (sampler == prev)
2230 return;
2232 if (sampler)
2233 wined3d_sampler_incref(sampler);
2234 device->update_state->sampler[type][idx] = sampler;
2235 if (!device->recording)
2236 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2237 if (prev)
2238 wined3d_sampler_decref(prev);
2241 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2243 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2245 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2248 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2250 TRACE("device %p, idx %u.\n", device, idx);
2252 if (idx >= MAX_SAMPLER_OBJECTS)
2254 WARN("Invalid sampler index %u.\n", idx);
2255 return NULL;
2258 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2261 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2263 UINT i;
2265 for (i = 0; i < device->context_count; ++i)
2267 device->contexts[i]->constant_update_mask |= mask;
2271 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2272 UINT start_register, const BOOL *constants, UINT bool_count)
2274 UINT count = min(bool_count, MAX_CONST_B - start_register);
2275 UINT i;
2277 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2278 device, start_register, constants, bool_count);
2280 if (!constants || start_register >= MAX_CONST_B)
2281 return WINED3DERR_INVALIDCALL;
2283 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2284 for (i = 0; i < count; ++i)
2285 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2287 if (device->recording)
2289 for (i = start_register; i < count + start_register; ++i)
2290 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2292 else
2294 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2297 return WINED3D_OK;
2300 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2301 UINT start_register, BOOL *constants, UINT bool_count)
2303 UINT count = min(bool_count, MAX_CONST_B - start_register);
2305 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2306 device, start_register, constants, bool_count);
2308 if (!constants || start_register >= MAX_CONST_B)
2309 return WINED3DERR_INVALIDCALL;
2311 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2313 return WINED3D_OK;
2316 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2317 UINT start_register, const int *constants, UINT vector4i_count)
2319 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2320 UINT i;
2322 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2323 device, start_register, constants, vector4i_count);
2325 if (!constants || start_register >= MAX_CONST_I)
2326 return WINED3DERR_INVALIDCALL;
2328 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2329 for (i = 0; i < count; ++i)
2330 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2331 constants[i * 4], constants[i * 4 + 1],
2332 constants[i * 4 + 2], constants[i * 4 + 3]);
2334 if (device->recording)
2336 for (i = start_register; i < count + start_register; ++i)
2337 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2339 else
2341 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2344 return WINED3D_OK;
2347 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2348 UINT start_register, int *constants, UINT vector4i_count)
2350 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2352 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2353 device, start_register, constants, vector4i_count);
2355 if (!constants || start_register >= MAX_CONST_I)
2356 return WINED3DERR_INVALIDCALL;
2358 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2359 return WINED3D_OK;
2362 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2363 UINT start_register, const float *constants, UINT vector4f_count)
2365 UINT i;
2366 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2368 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2369 device, start_register, constants, vector4f_count);
2371 /* Specifically test start_register > limit to catch MAX_UINT overflows
2372 * when adding start_register + vector4f_count. */
2373 if (!constants
2374 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2375 || start_register > d3d_info->limits.vs_uniform_count)
2376 return WINED3DERR_INVALIDCALL;
2378 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2379 constants, vector4f_count * sizeof(float) * 4);
2380 if (TRACE_ON(d3d))
2382 for (i = 0; i < vector4f_count; ++i)
2383 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2384 constants[i * 4], constants[i * 4 + 1],
2385 constants[i * 4 + 2], constants[i * 4 + 3]);
2388 if (device->recording)
2389 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2390 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2391 else
2392 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2395 return WINED3D_OK;
2398 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2399 UINT start_register, float *constants, UINT vector4f_count)
2401 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2402 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2404 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2405 device, start_register, constants, vector4f_count);
2407 if (!constants || count < 0)
2408 return WINED3DERR_INVALIDCALL;
2410 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2412 return WINED3D_OK;
2415 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2417 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2419 TRACE("device %p, shader %p.\n", device, shader);
2421 if (device->recording)
2422 device->recording->changed.pixelShader = TRUE;
2424 if (shader == prev)
2425 return;
2427 if (shader)
2428 wined3d_shader_incref(shader);
2429 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2430 if (!device->recording)
2431 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2432 if (prev)
2433 wined3d_shader_decref(prev);
2436 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2438 TRACE("device %p.\n", device);
2440 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2443 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2445 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2447 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2450 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2452 TRACE("device %p, idx %u.\n", device, idx);
2454 if (idx >= MAX_CONSTANT_BUFFERS)
2456 WARN("Invalid constant buffer index %u.\n", idx);
2457 return NULL;
2460 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2463 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2464 UINT idx, struct wined3d_shader_resource_view *view)
2466 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2468 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2471 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2472 UINT idx)
2474 TRACE("device %p, idx %u.\n", device, idx);
2476 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2478 WARN("Invalid view index %u.\n", idx);
2479 return NULL;
2482 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2485 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2487 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2489 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2492 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2494 TRACE("device %p, idx %u.\n", device, idx);
2496 if (idx >= MAX_SAMPLER_OBJECTS)
2498 WARN("Invalid sampler index %u.\n", idx);
2499 return NULL;
2502 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2505 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2506 UINT start_register, const BOOL *constants, UINT bool_count)
2508 UINT count = min(bool_count, MAX_CONST_B - start_register);
2509 UINT i;
2511 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2512 device, start_register, constants, bool_count);
2514 if (!constants || start_register >= MAX_CONST_B)
2515 return WINED3DERR_INVALIDCALL;
2517 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2518 for (i = 0; i < count; ++i)
2519 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2521 if (device->recording)
2523 for (i = start_register; i < count + start_register; ++i)
2524 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2526 else
2528 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2531 return WINED3D_OK;
2534 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2535 UINT start_register, BOOL *constants, UINT bool_count)
2537 UINT count = min(bool_count, MAX_CONST_B - start_register);
2539 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2540 device, start_register, constants, bool_count);
2542 if (!constants || start_register >= MAX_CONST_B)
2543 return WINED3DERR_INVALIDCALL;
2545 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2547 return WINED3D_OK;
2550 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2551 UINT start_register, const int *constants, UINT vector4i_count)
2553 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2554 UINT i;
2556 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2557 device, start_register, constants, vector4i_count);
2559 if (!constants || start_register >= MAX_CONST_I)
2560 return WINED3DERR_INVALIDCALL;
2562 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2563 for (i = 0; i < count; ++i)
2564 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2565 constants[i * 4], constants[i * 4 + 1],
2566 constants[i * 4 + 2], constants[i * 4 + 3]);
2568 if (device->recording)
2570 for (i = start_register; i < count + start_register; ++i)
2571 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2573 else
2575 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2578 return WINED3D_OK;
2581 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2582 UINT start_register, int *constants, UINT vector4i_count)
2584 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2586 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2587 device, start_register, constants, vector4i_count);
2589 if (!constants || start_register >= MAX_CONST_I)
2590 return WINED3DERR_INVALIDCALL;
2592 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2594 return WINED3D_OK;
2597 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2598 UINT start_register, const float *constants, UINT vector4f_count)
2600 UINT i;
2601 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2603 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2604 device, start_register, constants, vector4f_count);
2606 /* Specifically test start_register > limit to catch MAX_UINT overflows
2607 * when adding start_register + vector4f_count. */
2608 if (!constants
2609 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2610 || start_register > d3d_info->limits.ps_uniform_count)
2611 return WINED3DERR_INVALIDCALL;
2613 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2614 constants, vector4f_count * sizeof(float) * 4);
2615 if (TRACE_ON(d3d))
2617 for (i = 0; i < vector4f_count; ++i)
2618 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2619 constants[i * 4], constants[i * 4 + 1],
2620 constants[i * 4 + 2], constants[i * 4 + 3]);
2623 if (device->recording)
2624 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2625 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2626 else
2627 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2629 return WINED3D_OK;
2632 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2633 UINT start_register, float *constants, UINT vector4f_count)
2635 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2636 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2638 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2639 device, start_register, constants, vector4f_count);
2641 if (!constants || count < 0)
2642 return WINED3DERR_INVALIDCALL;
2644 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2646 return WINED3D_OK;
2649 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2651 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2653 TRACE("device %p, shader %p.\n", device, shader);
2655 if (device->recording || shader == prev)
2656 return;
2657 if (shader)
2658 wined3d_shader_incref(shader);
2659 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2660 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2661 if (prev)
2662 wined3d_shader_decref(prev);
2665 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2667 TRACE("device %p.\n", device);
2669 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2672 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2674 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2676 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2679 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2681 TRACE("device %p, idx %u.\n", device, idx);
2683 if (idx >= MAX_CONSTANT_BUFFERS)
2685 WARN("Invalid constant buffer index %u.\n", idx);
2686 return NULL;
2689 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2692 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2693 UINT idx, struct wined3d_shader_resource_view *view)
2695 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2697 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2700 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2701 UINT idx)
2703 TRACE("device %p, idx %u.\n", device, idx);
2705 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2707 WARN("Invalid view index %u.\n", idx);
2708 return NULL;
2711 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2714 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2716 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2718 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2721 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2723 TRACE("device %p, idx %u.\n", device, idx);
2725 if (idx >= MAX_SAMPLER_OBJECTS)
2727 WARN("Invalid sampler index %u.\n", idx);
2728 return NULL;
2731 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2734 /* Context activation is done by the caller. */
2735 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2736 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2737 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2738 DWORD DestFVF)
2740 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2741 struct wined3d_viewport vp;
2742 UINT vertex_size;
2743 unsigned int i;
2744 BYTE *dest_ptr;
2745 BOOL doClip;
2746 DWORD numTextures;
2747 HRESULT hr;
2749 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2751 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2754 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2756 ERR("Source has no position mask\n");
2757 return WINED3DERR_INVALIDCALL;
2760 if (device->state.render_states[WINED3D_RS_CLIPPING])
2762 static BOOL warned = FALSE;
2764 * The clipping code is not quite correct. Some things need
2765 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2766 * so disable clipping for now.
2767 * (The graphics in Half-Life are broken, and my processvertices
2768 * test crashes with IDirect3DDevice3)
2769 doClip = TRUE;
2771 doClip = FALSE;
2772 if(!warned) {
2773 warned = TRUE;
2774 FIXME("Clipping is broken and disabled for now\n");
2777 else
2778 doClip = FALSE;
2780 vertex_size = get_flexible_vertex_size(DestFVF);
2781 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2783 WARN("Failed to map buffer, hr %#x.\n", hr);
2784 return hr;
2787 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2788 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2789 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2791 TRACE("View mat:\n");
2792 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2793 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2794 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2795 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2797 TRACE("Proj mat:\n");
2798 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2799 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2800 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2801 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2803 TRACE("World mat:\n");
2804 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2805 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2806 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2807 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2809 /* Get the viewport */
2810 wined3d_device_get_viewport(device, &vp);
2811 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2812 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2814 multiply_matrix(&mat,&view_mat,&world_mat);
2815 multiply_matrix(&mat,&proj_mat,&mat);
2817 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2819 for (i = 0; i < dwCount; i+= 1) {
2820 unsigned int tex_index;
2822 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2823 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2824 /* The position first */
2825 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2826 const float *p = (const float *)(element->data.addr + i * element->stride);
2827 float x, y, z, rhw;
2828 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2830 /* Multiplication with world, view and projection matrix. */
2831 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2832 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2833 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2834 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2836 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2838 /* WARNING: The following things are taken from d3d7 and were not yet checked
2839 * against d3d8 or d3d9!
2842 /* Clipping conditions: From msdn
2844 * A vertex is clipped if it does not match the following requirements
2845 * -rhw < x <= rhw
2846 * -rhw < y <= rhw
2847 * 0 < z <= rhw
2848 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2850 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2851 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2855 if( !doClip ||
2856 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2857 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2858 ( rhw > eps ) ) ) {
2860 /* "Normal" viewport transformation (not clipped)
2861 * 1) The values are divided by rhw
2862 * 2) The y axis is negative, so multiply it with -1
2863 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2864 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2865 * 4) Multiply x with Width/2 and add Width/2
2866 * 5) The same for the height
2867 * 6) Add the viewpoint X and Y to the 2D coordinates and
2868 * The minimum Z value to z
2869 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2871 * Well, basically it's simply a linear transformation into viewport
2872 * coordinates
2875 x /= rhw;
2876 y /= rhw;
2877 z /= rhw;
2879 y *= -1;
2881 x *= vp.width / 2;
2882 y *= vp.height / 2;
2883 z *= vp.max_z - vp.min_z;
2885 x += vp.width / 2 + vp.x;
2886 y += vp.height / 2 + vp.y;
2887 z += vp.min_z;
2889 rhw = 1 / rhw;
2890 } else {
2891 /* That vertex got clipped
2892 * Contrary to OpenGL it is not dropped completely, it just
2893 * undergoes a different calculation.
2895 TRACE("Vertex got clipped\n");
2896 x += rhw;
2897 y += rhw;
2899 x /= 2;
2900 y /= 2;
2902 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2903 * outside of the main vertex buffer memory. That needs some more
2904 * investigation...
2908 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2911 ( (float *) dest_ptr)[0] = x;
2912 ( (float *) dest_ptr)[1] = y;
2913 ( (float *) dest_ptr)[2] = z;
2914 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2916 dest_ptr += 3 * sizeof(float);
2918 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2919 dest_ptr += sizeof(float);
2922 if (DestFVF & WINED3DFVF_PSIZE)
2923 dest_ptr += sizeof(DWORD);
2925 if (DestFVF & WINED3DFVF_NORMAL)
2927 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2928 const float *normal = (const float *)(element->data.addr + i * element->stride);
2929 /* AFAIK this should go into the lighting information */
2930 FIXME("Didn't expect the destination to have a normal\n");
2931 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2934 if (DestFVF & WINED3DFVF_DIFFUSE)
2936 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2937 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2938 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2940 static BOOL warned = FALSE;
2942 if(!warned) {
2943 ERR("No diffuse color in source, but destination has one\n");
2944 warned = TRUE;
2947 *( (DWORD *) dest_ptr) = 0xffffffff;
2948 dest_ptr += sizeof(DWORD);
2950 else
2952 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2956 if (DestFVF & WINED3DFVF_SPECULAR)
2958 /* What's the color value in the feedback buffer? */
2959 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2960 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2961 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2963 static BOOL warned = FALSE;
2965 if(!warned) {
2966 ERR("No specular color in source, but destination has one\n");
2967 warned = TRUE;
2970 *(DWORD *)dest_ptr = 0xff000000;
2971 dest_ptr += sizeof(DWORD);
2973 else
2975 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2979 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2981 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2982 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2983 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2985 ERR("No source texture, but destination requests one\n");
2986 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2988 else
2990 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2995 wined3d_buffer_unmap(dest);
2997 return WINED3D_OK;
2999 #undef copy_and_next
3001 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3002 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3003 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3005 struct wined3d_state *state = &device->state;
3006 struct wined3d_stream_info stream_info;
3007 const struct wined3d_gl_info *gl_info;
3008 struct wined3d_context *context;
3009 struct wined3d_shader *vs;
3010 unsigned int i;
3011 HRESULT hr;
3012 WORD map;
3014 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3015 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3016 device, src_start_idx, dst_idx, vertex_count,
3017 dst_buffer, declaration, flags, dst_fvf);
3019 if (declaration)
3020 FIXME("Output vertex declaration not implemented yet.\n");
3022 /* Need any context to write to the vbo. */
3023 context = context_acquire(device, NULL);
3024 gl_info = context->gl_info;
3026 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3027 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3028 context_stream_info_from_declaration(context, state, &stream_info);
3029 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3031 /* We can't convert FROM a VBO, and vertex buffers used to source into
3032 * process_vertices() are unlikely to ever be used for drawing. Release
3033 * VBOs in those buffers and fix up the stream_info structure.
3035 * Also apply the start index. */
3036 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3038 struct wined3d_stream_info_element *e;
3039 struct wined3d_buffer *buffer;
3041 if (!(map & 1))
3042 continue;
3044 e = &stream_info.elements[i];
3045 buffer = state->streams[e->stream_idx].buffer;
3046 e->data.buffer_object = 0;
3047 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3048 if (buffer->buffer_object)
3050 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3051 buffer->buffer_object = 0;
3053 if (e->data.addr)
3054 e->data.addr += e->stride * src_start_idx;
3057 hr = process_vertices_strided(device, dst_idx, vertex_count,
3058 &stream_info, dst_buffer, flags, dst_fvf);
3060 context_release(context);
3062 return hr;
3065 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3066 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3068 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3069 DWORD old_value;
3071 TRACE("device %p, stage %u, state %s, value %#x.\n",
3072 device, stage, debug_d3dtexturestate(state), value);
3074 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3076 WARN("Invalid state %#x passed.\n", state);
3077 return;
3080 if (stage >= d3d_info->limits.ffp_blend_stages)
3082 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3083 stage, d3d_info->limits.ffp_blend_stages - 1);
3084 return;
3087 old_value = device->update_state->texture_states[stage][state];
3088 device->update_state->texture_states[stage][state] = value;
3090 if (device->recording)
3092 TRACE("Recording... not performing anything.\n");
3093 device->recording->changed.textureState[stage] |= 1u << state;
3094 return;
3097 /* Checked after the assignments to allow proper stateblock recording. */
3098 if (old_value == value)
3100 TRACE("Application is setting the old value over, nothing to do.\n");
3101 return;
3104 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3107 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3108 UINT stage, enum wined3d_texture_stage_state state)
3110 TRACE("device %p, stage %u, state %s.\n",
3111 device, stage, debug_d3dtexturestate(state));
3113 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3115 WARN("Invalid state %#x passed.\n", state);
3116 return 0;
3119 return device->state.texture_states[stage][state];
3122 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3123 UINT stage, struct wined3d_texture *texture)
3125 struct wined3d_texture *prev;
3127 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3129 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3130 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3132 /* Windows accepts overflowing this array... we do not. */
3133 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3135 WARN("Ignoring invalid stage %u.\n", stage);
3136 return WINED3D_OK;
3139 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3141 WARN("Rejecting attempt to set scratch texture.\n");
3142 return WINED3DERR_INVALIDCALL;
3145 if (device->recording)
3146 device->recording->changed.textures |= 1u << stage;
3148 prev = device->update_state->textures[stage];
3149 TRACE("Previous texture %p.\n", prev);
3151 if (texture == prev)
3153 TRACE("App is setting the same texture again, nothing to do.\n");
3154 return WINED3D_OK;
3157 TRACE("Setting new texture to %p.\n", texture);
3158 device->update_state->textures[stage] = texture;
3160 if (texture)
3161 wined3d_texture_incref(texture);
3162 if (!device->recording)
3163 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3164 if (prev)
3165 wined3d_texture_decref(prev);
3167 return WINED3D_OK;
3170 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3172 TRACE("device %p, stage %u.\n", device, stage);
3174 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3175 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3177 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3179 WARN("Ignoring invalid stage %u.\n", stage);
3180 return NULL; /* Windows accepts overflowing this array ... we do not. */
3183 return device->state.textures[stage];
3186 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3188 TRACE("device %p, caps %p.\n", device, caps);
3190 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3191 device->create_parms.device_type, caps);
3194 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3195 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3197 struct wined3d_swapchain *swapchain;
3199 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3200 device, swapchain_idx, mode, rotation);
3202 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3203 return WINED3DERR_INVALIDCALL;
3205 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3208 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3210 struct wined3d_stateblock *stateblock;
3211 HRESULT hr;
3213 TRACE("device %p.\n", device);
3215 if (device->recording)
3216 return WINED3DERR_INVALIDCALL;
3218 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3219 if (FAILED(hr))
3220 return hr;
3222 device->recording = stateblock;
3223 device->update_state = &stateblock->state;
3225 TRACE("Recording stateblock %p.\n", stateblock);
3227 return WINED3D_OK;
3230 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3231 struct wined3d_stateblock **stateblock)
3233 struct wined3d_stateblock *object = device->recording;
3235 TRACE("device %p, stateblock %p.\n", device, stateblock);
3237 if (!device->recording)
3239 WARN("Not recording.\n");
3240 *stateblock = NULL;
3241 return WINED3DERR_INVALIDCALL;
3244 stateblock_init_contained_states(object);
3246 *stateblock = object;
3247 device->recording = NULL;
3248 device->update_state = &device->state;
3250 TRACE("Returning stateblock %p.\n", *stateblock);
3252 return WINED3D_OK;
3255 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3257 /* At the moment we have no need for any functionality at the beginning
3258 * of a scene. */
3259 TRACE("device %p.\n", device);
3261 if (device->inScene)
3263 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3264 return WINED3DERR_INVALIDCALL;
3266 device->inScene = TRUE;
3267 return WINED3D_OK;
3270 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3272 struct wined3d_context *context;
3274 TRACE("device %p.\n", device);
3276 if (!device->inScene)
3278 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3279 return WINED3DERR_INVALIDCALL;
3282 context = context_acquire(device, NULL);
3283 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3284 context->gl_info->gl_ops.gl.p_glFlush();
3285 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3286 * fails. */
3287 context_release(context);
3289 device->inScene = FALSE;
3290 return WINED3D_OK;
3293 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3294 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3296 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3297 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3299 if (!rect_count && rects)
3301 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3302 return WINED3D_OK;
3305 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3307 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3308 if (!ds)
3310 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3311 /* TODO: What about depth stencil buffers without stencil bits? */
3312 return WINED3DERR_INVALIDCALL;
3314 else if (flags & WINED3DCLEAR_TARGET)
3316 if (ds->width < device->fb.render_targets[0]->width
3317 || ds->height < device->fb.render_targets[0]->height)
3319 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3320 return WINED3D_OK;
3325 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3327 return WINED3D_OK;
3330 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3331 struct wined3d_query *predicate, BOOL value)
3333 struct wined3d_query *prev;
3335 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3337 prev = device->update_state->predicate;
3338 if (predicate)
3340 FIXME("Predicated rendering not implemented.\n");
3341 wined3d_query_incref(predicate);
3343 device->update_state->predicate = predicate;
3344 device->update_state->predicate_value = value;
3345 if (!device->recording)
3346 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3347 if (prev)
3348 wined3d_query_decref(prev);
3351 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3353 TRACE("device %p, value %p.\n", device, value);
3355 *value = device->state.predicate_value;
3356 return device->state.predicate;
3359 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3360 enum wined3d_primitive_type primitive_type)
3362 GLenum gl_primitive_type, prev;
3364 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3366 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3367 prev = device->update_state->gl_primitive_type;
3368 device->update_state->gl_primitive_type = gl_primitive_type;
3369 if (device->recording)
3370 device->recording->changed.primitive_type = TRUE;
3371 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3372 device_invalidate_state(device, STATE_POINT_ENABLE);
3375 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3376 enum wined3d_primitive_type *primitive_type)
3378 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3380 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3382 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3385 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3387 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3389 if (!device->state.vertex_declaration)
3391 WARN("Called without a valid vertex declaration set.\n");
3392 return WINED3DERR_INVALIDCALL;
3395 if (device->state.load_base_vertex_index)
3397 device->state.load_base_vertex_index = 0;
3398 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3401 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3403 return WINED3D_OK;
3406 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3407 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3409 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3410 device, start_vertex, vertex_count, start_instance, instance_count);
3412 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3415 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3417 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3419 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3421 if (!device->state.index_buffer)
3423 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3424 * without an index buffer set. (The first time at least...)
3425 * D3D8 simply dies, but I doubt it can do much harm to return
3426 * D3DERR_INVALIDCALL there as well. */
3427 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3428 return WINED3DERR_INVALIDCALL;
3431 if (!device->state.vertex_declaration)
3433 WARN("Called without a valid vertex declaration set.\n");
3434 return WINED3DERR_INVALIDCALL;
3437 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3438 device->state.load_base_vertex_index != device->state.base_vertex_index)
3440 device->state.load_base_vertex_index = device->state.base_vertex_index;
3441 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3444 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3446 return WINED3D_OK;
3449 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3450 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3452 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3453 device, start_idx, index_count, start_instance, instance_count);
3455 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3458 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3459 static HRESULT device_update_volume(struct wined3d_device *device,
3460 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3462 struct wined3d_const_bo_address data;
3463 struct wined3d_map_desc src;
3464 HRESULT hr;
3465 struct wined3d_context *context;
3467 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3468 device, src_volume, dst_volume);
3470 if (src_volume->resource.format != dst_volume->resource.format)
3472 FIXME("Source and destination formats do not match.\n");
3473 return WINED3DERR_INVALIDCALL;
3475 if (src_volume->resource.width != dst_volume->resource.width
3476 || src_volume->resource.height != dst_volume->resource.height
3477 || src_volume->resource.depth != dst_volume->resource.depth)
3479 FIXME("Source and destination sizes do not match.\n");
3480 return WINED3DERR_INVALIDCALL;
3483 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3484 return hr;
3486 context = context_acquire(device, NULL);
3488 /* Only a prepare, since we're uploading the entire volume. */
3489 wined3d_texture_prepare_texture(dst_volume->container, context, FALSE);
3490 wined3d_texture_bind_and_dirtify(dst_volume->container, context, FALSE);
3492 data.buffer_object = 0;
3493 data.addr = src.data;
3494 wined3d_volume_upload_data(dst_volume, context, &data);
3495 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3497 context_release(context);
3499 hr = wined3d_volume_unmap(src_volume);
3501 return hr;
3504 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3505 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3507 enum wined3d_resource_type type;
3508 unsigned int level_count, i, j, src_size, dst_size, src_skip_levels = 0;
3509 HRESULT hr;
3510 struct wined3d_context *context;
3512 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3514 /* Verify that the source and destination textures are non-NULL. */
3515 if (!src_texture || !dst_texture)
3517 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3518 return WINED3DERR_INVALIDCALL;
3521 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3523 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3524 return WINED3DERR_INVALIDCALL;
3526 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3528 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3529 return WINED3DERR_INVALIDCALL;
3532 /* Verify that the source and destination textures are the same type. */
3533 type = src_texture->resource.type;
3534 if (dst_texture->resource.type != type)
3536 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3537 return WINED3DERR_INVALIDCALL;
3540 level_count = min(wined3d_texture_get_level_count(src_texture),
3541 wined3d_texture_get_level_count(dst_texture));
3543 src_size = max(src_texture->resource.width, src_texture->resource.height);
3544 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3545 if (type == WINED3D_RTYPE_VOLUME)
3547 src_size = max(src_size, src_texture->resource.depth);
3548 dst_size = max(dst_size, dst_texture->resource.depth);
3550 while (src_size > dst_size)
3552 src_size >>= 1;
3553 ++src_skip_levels;
3556 /* Make sure that the destination texture is loaded. */
3557 context = context_acquire(device, NULL);
3558 wined3d_texture_load(dst_texture, context, FALSE);
3559 context_release(context);
3561 /* Update every surface level of the texture. */
3562 switch (type)
3564 case WINED3D_RTYPE_TEXTURE:
3566 struct wined3d_surface *src_surface;
3567 struct wined3d_surface *dst_surface;
3569 for (i = 0; i < level_count; ++i)
3571 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture,
3572 i + src_skip_levels));
3573 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3574 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3575 if (FAILED(hr))
3577 WARN("Failed to update surface, hr %#x.\n", hr);
3578 return hr;
3581 break;
3584 case WINED3D_RTYPE_CUBE_TEXTURE:
3586 struct wined3d_surface *src_surface;
3587 struct wined3d_surface *dst_surface;
3588 unsigned int src_levels = wined3d_texture_get_level_count(src_texture);
3589 unsigned int dst_levels = wined3d_texture_get_level_count(dst_texture);
3591 for (i = 0; i < 6; ++i)
3593 for (j = 0; j < level_count; ++j)
3595 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture,
3596 i * src_levels + j + src_skip_levels));
3597 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture,
3598 i * dst_levels + j));
3599 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3600 if (FAILED(hr))
3602 WARN("Failed to update surface, hr %#x.\n", hr);
3603 return hr;
3607 break;
3610 case WINED3D_RTYPE_VOLUME_TEXTURE:
3612 for (i = 0; i < level_count; ++i)
3614 hr = device_update_volume(device,
3615 volume_from_resource(wined3d_texture_get_sub_resource(src_texture,
3616 i + src_skip_levels)),
3617 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3618 if (FAILED(hr))
3620 WARN("Failed to update volume, hr %#x.\n", hr);
3621 return hr;
3624 break;
3627 default:
3628 FIXME("Unsupported texture type %#x.\n", type);
3629 return WINED3DERR_INVALIDCALL;
3632 return WINED3D_OK;
3635 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3637 const struct wined3d_state *state = &device->state;
3638 struct wined3d_texture *texture;
3639 DWORD i;
3641 TRACE("device %p, num_passes %p.\n", device, num_passes);
3643 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3645 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3647 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3648 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3650 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3652 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3653 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3656 texture = state->textures[i];
3657 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3659 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3661 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3662 return E_FAIL;
3664 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3666 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3667 return E_FAIL;
3669 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3670 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3672 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3673 return E_FAIL;
3677 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3678 || state->render_states[WINED3D_RS_STENCILENABLE])
3680 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3681 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3683 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3685 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3686 return WINED3DERR_CONFLICTINGRENDERSTATE;
3690 /* return a sensible default */
3691 *num_passes = 1;
3693 TRACE("returning D3D_OK\n");
3694 return WINED3D_OK;
3697 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3699 static BOOL warned;
3701 TRACE("device %p, software %#x.\n", device, software);
3703 if (!warned)
3705 FIXME("device %p, software %#x stub!\n", device, software);
3706 warned = TRUE;
3709 device->softwareVertexProcessing = software;
3712 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3714 static BOOL warned;
3716 TRACE("device %p.\n", device);
3718 if (!warned)
3720 TRACE("device %p stub!\n", device);
3721 warned = TRUE;
3724 return device->softwareVertexProcessing;
3727 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3728 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3730 struct wined3d_swapchain *swapchain;
3732 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3733 device, swapchain_idx, raster_status);
3735 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3736 return WINED3DERR_INVALIDCALL;
3738 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3741 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3743 static BOOL warned;
3745 TRACE("device %p, segments %.8e.\n", device, segments);
3747 if (segments != 0.0f)
3749 if (!warned)
3751 FIXME("device %p, segments %.8e stub!\n", device, segments);
3752 warned = TRUE;
3756 return WINED3D_OK;
3759 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3761 static BOOL warned;
3763 TRACE("device %p.\n", device);
3765 if (!warned)
3767 FIXME("device %p stub!\n", device);
3768 warned = TRUE;
3771 return 0.0f;
3774 /* FIXME: Callers should probably use wined3d_device_update_sub_resource()
3775 * instead. */
3776 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3777 struct wined3d_surface *src_surface, const RECT *src_rect,
3778 struct wined3d_surface *dst_surface, const POINT *dst_point)
3780 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3781 device, src_surface, wine_dbgstr_rect(src_rect),
3782 dst_surface, wine_dbgstr_point(dst_point));
3784 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3786 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3787 src_surface, dst_surface);
3788 return WINED3DERR_INVALIDCALL;
3791 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3794 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3795 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3797 struct wined3d_surface *dst_surface, *src_surface;
3798 struct wined3d_texture *dst_texture, *src_texture;
3799 unsigned int i, count;
3800 HRESULT hr;
3802 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3804 if (src_resource == dst_resource)
3806 WARN("Source and destination are the same resource.\n");
3807 return;
3810 if (src_resource->type != dst_resource->type)
3812 WARN("Resource types (%s / %s) don't match.\n",
3813 debug_d3dresourcetype(dst_resource->type),
3814 debug_d3dresourcetype(src_resource->type));
3815 return;
3818 if (src_resource->width != dst_resource->width
3819 || src_resource->height != dst_resource->height
3820 || src_resource->depth != dst_resource->depth)
3822 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3823 dst_resource->width, dst_resource->height, dst_resource->depth,
3824 src_resource->width, src_resource->height, src_resource->depth);
3825 return;
3828 if (src_resource->format->id != dst_resource->format->id)
3830 WARN("Resource formats (%s / %s) don't match.\n",
3831 debug_d3dformat(dst_resource->format->id),
3832 debug_d3dformat(src_resource->format->id));
3833 return;
3836 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3838 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3839 return;
3842 dst_texture = wined3d_texture_from_resource(dst_resource);
3843 src_texture = wined3d_texture_from_resource(src_resource);
3845 if (src_texture->layer_count != dst_texture->layer_count
3846 || src_texture->level_count != dst_texture->level_count)
3848 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3849 dst_texture->layer_count, dst_texture->level_count,
3850 src_texture->layer_count, src_texture->level_count);
3851 return;
3854 count = dst_texture->layer_count * dst_texture->level_count;
3855 for (i = 0; i < count; ++i)
3857 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3858 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3860 if (FAILED(hr = wined3d_surface_blt(dst_surface, NULL, src_surface, NULL, 0, NULL, WINED3D_TEXF_POINT)))
3861 ERR("Failed to blit, subresource %u, hr %#x.\n", i, hr);
3865 void CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3866 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3867 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3868 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3870 struct wined3d_surface *dst_surface, *src_surface;
3871 struct wined3d_texture *dst_texture, *src_texture;
3872 struct wined3d_resource *tmp;
3873 RECT dst_rect, src_rect;
3874 HRESULT hr;
3876 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3877 "src_resource %p, src_sub_resource_idx %u, src_box %p.\n",
3878 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3879 src_resource, src_sub_resource_idx, src_box);
3881 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3883 WARN("Source and destination are the same sub-resource.\n");
3884 return;
3887 if (src_resource->type != dst_resource->type)
3889 WARN("Resource types (%s / %s) don't match.\n",
3890 debug_d3dresourcetype(dst_resource->type),
3891 debug_d3dresourcetype(src_resource->type));
3892 return;
3895 if (src_resource->format->id != dst_resource->format->id)
3897 WARN("Resource formats (%s / %s) don't match.\n",
3898 debug_d3dformat(dst_resource->format->id),
3899 debug_d3dformat(src_resource->format->id));
3900 return;
3903 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3905 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3906 return;
3909 dst_texture = wined3d_texture_from_resource(dst_resource);
3910 if (!(tmp = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
3912 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3913 return;
3915 dst_surface = surface_from_resource(tmp);
3917 src_texture = wined3d_texture_from_resource(src_resource);
3918 if (!(tmp = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx)))
3920 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3921 return;
3923 src_surface = surface_from_resource(tmp);
3925 dst_rect.left = dst_x;
3926 dst_rect.top = dst_y;
3927 dst_rect.right = dst_x + (src_box->right - src_box->left);
3928 dst_rect.bottom = dst_y + (src_box->bottom - src_box->top);
3930 src_rect.left = src_box->left;
3931 src_rect.top = src_box->top;
3932 src_rect.right = src_box->right;
3933 src_rect.bottom = src_box->bottom;
3935 if (FAILED(hr = wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3936 ERR("Failed to blit, hr %#x.\n", hr);
3939 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
3940 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
3941 unsigned int depth_pitch)
3943 struct wined3d_resource *sub_resource;
3944 const struct wined3d_gl_info *gl_info;
3945 struct wined3d_const_bo_address addr;
3946 struct wined3d_context *context;
3947 struct wined3d_texture *texture;
3948 struct wined3d_surface *surface;
3949 POINT dst_point;
3950 RECT src_rect;
3952 TRACE("device %p, resource %p, sub_resource_idx %u, box %p, data %p, row_pitch %u, depth_pitch %u.\n",
3953 device, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
3955 if (resource->type != WINED3D_RTYPE_TEXTURE)
3957 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
3958 return;
3961 texture = wined3d_texture_from_resource(resource);
3962 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3964 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
3965 return;
3967 surface = surface_from_resource(sub_resource);
3969 src_rect.left = 0;
3970 src_rect.top = 0;
3971 if (box)
3973 if (box->left >= box->right || box->right > sub_resource->width
3974 || box->top >= box->bottom || box->bottom > sub_resource->height)
3976 WARN("Invalid box (%u, %u, %u)->(%u, %u, %u) specified.\n",
3977 box->left, box->top, box->front, box->right, box->bottom, box->back);
3978 return;
3981 src_rect.right = box->right - box->left;
3982 src_rect.bottom = box->bottom - box->top;
3983 dst_point.x = box->left;
3984 dst_point.y = box->top;
3986 else
3988 src_rect.right = sub_resource->width;
3989 src_rect.bottom = sub_resource->height;
3990 dst_point.x = 0;
3991 dst_point.y = 0;
3994 addr.buffer_object = 0;
3995 addr.addr = data;
3997 context = context_acquire(resource->device, NULL);
3998 gl_info = context->gl_info;
4000 /* Only load the surface for partial updates. */
4001 if (!dst_point.x && !dst_point.y && src_rect.right == sub_resource->width
4002 && src_rect.bottom == sub_resource->height)
4003 wined3d_texture_prepare_texture(texture, context, FALSE);
4004 else
4005 surface_load_location(surface, context, WINED3D_LOCATION_TEXTURE_RGB);
4006 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4008 wined3d_surface_upload_data(surface, gl_info, resource->format,
4009 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4011 context_release(context);
4013 surface_validate_location(surface, WINED3D_LOCATION_TEXTURE_RGB);
4014 surface_invalidate_location(surface, ~WINED3D_LOCATION_TEXTURE_RGB);
4017 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4018 struct wined3d_rendertarget_view *view, const RECT *rect, const struct wined3d_color *color)
4020 struct wined3d_resource *resource;
4021 RECT r;
4023 TRACE("device %p, view %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4024 device, view, wine_dbgstr_rect(rect), color->r, color->g, color->b, color->a);
4026 resource = view->resource;
4027 if (resource->type != WINED3D_RTYPE_TEXTURE && resource->type != WINED3D_RTYPE_CUBE_TEXTURE)
4029 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4030 return WINED3DERR_INVALIDCALL;
4033 if (view->depth > 1)
4035 FIXME("Layered clears not implemented.\n");
4036 return WINED3DERR_INVALIDCALL;
4039 if (!rect)
4041 SetRect(&r, 0, 0, view->width, view->height);
4042 rect = &r;
4045 resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource), view->sub_resource_idx);
4047 return surface_color_fill(surface_from_resource(resource), rect, color);
4050 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4051 unsigned int view_idx)
4053 TRACE("device %p, view_idx %u.\n", device, view_idx);
4055 if (view_idx >= device->adapter->gl_info.limits.buffers)
4057 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4058 return NULL;
4061 return device->fb.render_targets[view_idx];
4064 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4066 TRACE("device %p.\n", device);
4068 return device->fb.depth_stencil;
4071 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4072 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4074 struct wined3d_rendertarget_view *prev;
4076 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4077 device, view_idx, view, set_viewport);
4079 if (view_idx >= device->adapter->gl_info.limits.buffers)
4081 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4082 return WINED3DERR_INVALIDCALL;
4085 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4087 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4088 return WINED3DERR_INVALIDCALL;
4091 /* Set the viewport and scissor rectangles, if requested. Tests show that
4092 * stateblock recording is ignored, the change goes directly into the
4093 * primary stateblock. */
4094 if (!view_idx && set_viewport)
4096 struct wined3d_state *state = &device->state;
4098 state->viewport.x = 0;
4099 state->viewport.y = 0;
4100 state->viewport.width = view->width;
4101 state->viewport.height = view->height;
4102 state->viewport.min_z = 0.0f;
4103 state->viewport.max_z = 1.0f;
4104 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4106 state->scissor_rect.top = 0;
4107 state->scissor_rect.left = 0;
4108 state->scissor_rect.right = view->width;
4109 state->scissor_rect.bottom = view->height;
4110 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4114 prev = device->fb.render_targets[view_idx];
4115 if (view == prev)
4116 return WINED3D_OK;
4118 if (view)
4119 wined3d_rendertarget_view_incref(view);
4120 device->fb.render_targets[view_idx] = view;
4121 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4122 /* Release after the assignment, to prevent device_resource_released()
4123 * from seeing the surface as still in use. */
4124 if (prev)
4125 wined3d_rendertarget_view_decref(prev);
4127 return WINED3D_OK;
4130 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4132 struct wined3d_rendertarget_view *prev;
4134 TRACE("device %p, view %p.\n", device, view);
4136 prev = device->fb.depth_stencil;
4137 if (prev == view)
4139 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4140 return;
4143 if ((device->fb.depth_stencil = view))
4144 wined3d_rendertarget_view_incref(view);
4145 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4146 if (prev)
4147 wined3d_rendertarget_view_decref(prev);
4150 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4151 struct wined3d_surface *cursor_image)
4153 struct wined3d_sub_resource_data data;
4154 struct wined3d_resource_desc desc;
4155 struct wined3d_map_desc map_desc;
4156 struct wined3d_texture *texture;
4157 HRESULT hr;
4159 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4161 ERR("Failed to map source surface.\n");
4162 return NULL;
4165 data.data = map_desc.data;
4166 data.row_pitch = map_desc.row_pitch;
4167 data.slice_pitch = map_desc.slice_pitch;
4169 desc.resource_type = WINED3D_RTYPE_TEXTURE;
4170 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4171 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4172 desc.multisample_quality = 0;
4173 desc.usage = WINED3DUSAGE_DYNAMIC;
4174 desc.pool = WINED3D_POOL_DEFAULT;
4175 desc.width = cursor_image->resource.width;
4176 desc.height = cursor_image->resource.height;
4177 desc.depth = 1;
4178 desc.size = 0;
4180 hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
4181 &data, NULL, &wined3d_null_parent_ops, &texture);
4182 wined3d_surface_unmap(cursor_image);
4183 if (FAILED(hr))
4185 ERR("Failed to create cursor texture.\n");
4186 return NULL;
4189 return texture;
4192 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4193 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4195 struct wined3d_display_mode mode;
4196 struct wined3d_map_desc map_desc;
4197 struct wined3d_resource *sub_resource;
4198 struct wined3d_surface *cursor_image;
4199 HRESULT hr;
4201 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4202 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4204 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx))
4205 || sub_resource->type != WINED3D_RTYPE_SURFACE)
4206 return WINED3DERR_INVALIDCALL;
4208 cursor_image = surface_from_resource(sub_resource);
4210 if (device->cursor_texture)
4212 wined3d_texture_decref(device->cursor_texture);
4213 device->cursor_texture = NULL;
4216 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4218 WARN("Surface %p has an invalid format %s.\n",
4219 cursor_image, debug_d3dformat(cursor_image->resource.format->id));
4220 return WINED3DERR_INVALIDCALL;
4223 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4225 ERR("Failed to get display mode, hr %#x.\n", hr);
4226 return WINED3DERR_INVALIDCALL;
4229 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4231 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4232 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4233 mode.width, mode.height);
4234 return WINED3DERR_INVALIDCALL;
4237 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4239 /* Do not store the surface's pointer because the application may
4240 * release it after setting the cursor image. Windows doesn't
4241 * addref the set surface, so we can't do this either without
4242 * creating circular refcount dependencies. */
4243 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
4245 ERR("Failed to create cursor texture.\n");
4246 return WINED3DERR_INVALIDCALL;
4249 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4251 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4252 ICONINFO cursor_info;
4253 DWORD *mask_bits;
4254 HCURSOR cursor;
4256 /* 32-bit user32 cursors ignore the alpha channel if it's all
4257 * zeroes, and use the mask instead. Fill the mask with all ones
4258 * to ensure we still get a fully transparent cursor. */
4259 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4260 return E_OUTOFMEMORY;
4261 memset(mask_bits, 0xff, mask_size);
4263 wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4264 cursor_info.fIcon = FALSE;
4265 cursor_info.xHotspot = x_hotspot;
4266 cursor_info.yHotspot = y_hotspot;
4267 cursor_info.hbmMask = CreateBitmap(cursor_image->resource.width,
4268 cursor_image->resource.height, 1, 1, mask_bits);
4269 cursor_info.hbmColor = CreateBitmap(cursor_image->resource.width,
4270 cursor_image->resource.height, 1, 32, map_desc.data);
4271 wined3d_surface_unmap(cursor_image);
4273 /* Create our cursor and clean up. */
4274 cursor = CreateIconIndirect(&cursor_info);
4275 if (cursor_info.hbmMask)
4276 DeleteObject(cursor_info.hbmMask);
4277 if (cursor_info.hbmColor)
4278 DeleteObject(cursor_info.hbmColor);
4279 if (device->hardwareCursor)
4280 DestroyCursor(device->hardwareCursor);
4281 device->hardwareCursor = cursor;
4282 if (device->bCursorVisible)
4283 SetCursor(cursor);
4285 HeapFree(GetProcessHeap(), 0, mask_bits);
4288 TRACE("New cursor dimensions are %ux%u.\n", cursor_image->resource.width, cursor_image->resource.height);
4289 device->cursorWidth = cursor_image->resource.width;
4290 device->cursorHeight = cursor_image->resource.height;
4291 device->xHotSpot = x_hotspot;
4292 device->yHotSpot = y_hotspot;
4294 return WINED3D_OK;
4297 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4298 int x_screen_space, int y_screen_space, DWORD flags)
4300 TRACE("device %p, x %d, y %d, flags %#x.\n",
4301 device, x_screen_space, y_screen_space, flags);
4303 device->xScreenSpace = x_screen_space;
4304 device->yScreenSpace = y_screen_space;
4306 if (device->hardwareCursor)
4308 POINT pt;
4310 GetCursorPos( &pt );
4311 if (x_screen_space == pt.x && y_screen_space == pt.y)
4312 return;
4313 SetCursorPos( x_screen_space, y_screen_space );
4315 /* Switch to the software cursor if position diverges from the hardware one. */
4316 GetCursorPos( &pt );
4317 if (x_screen_space != pt.x || y_screen_space != pt.y)
4319 if (device->bCursorVisible) SetCursor( NULL );
4320 DestroyCursor( device->hardwareCursor );
4321 device->hardwareCursor = 0;
4326 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4328 BOOL oldVisible = device->bCursorVisible;
4330 TRACE("device %p, show %#x.\n", device, show);
4333 * When ShowCursor is first called it should make the cursor appear at the OS's last
4334 * known cursor position.
4336 if (show && !oldVisible)
4338 POINT pt;
4339 GetCursorPos(&pt);
4340 device->xScreenSpace = pt.x;
4341 device->yScreenSpace = pt.y;
4344 if (device->hardwareCursor)
4346 device->bCursorVisible = show;
4347 if (show)
4348 SetCursor(device->hardwareCursor);
4349 else
4350 SetCursor(NULL);
4352 else if (device->cursor_texture)
4354 device->bCursorVisible = show;
4357 return oldVisible;
4360 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4362 struct wined3d_resource *resource, *cursor;
4364 TRACE("device %p.\n", device);
4366 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4368 TRACE("Checking resource %p for eviction.\n", resource);
4370 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4372 TRACE("Evicting %p.\n", resource);
4373 resource->resource_ops->resource_unload(resource);
4377 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4378 device_invalidate_state(device, STATE_STREAMSRC);
4381 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4383 struct wined3d_resource *resource, *cursor;
4384 const struct wined3d_gl_info *gl_info;
4385 struct wined3d_context *context;
4386 struct wined3d_shader *shader;
4388 context = context_acquire(device, NULL);
4389 gl_info = context->gl_info;
4391 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4393 TRACE("Unloading resource %p.\n", resource);
4395 resource->resource_ops->resource_unload(resource);
4398 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4400 device->shader_backend->shader_destroy(shader);
4403 if (device->depth_blt_texture)
4405 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4406 device->depth_blt_texture = 0;
4409 device->blitter->free_private(device);
4410 device->shader_backend->shader_free_private(device);
4411 destroy_dummy_textures(device, gl_info);
4413 context_release(context);
4415 while (device->context_count)
4417 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4420 HeapFree(GetProcessHeap(), 0, swapchain->context);
4421 swapchain->context = NULL;
4424 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4426 struct wined3d_context *context;
4427 struct wined3d_surface *target;
4428 HRESULT hr;
4430 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4431 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4433 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4434 return hr;
4437 if (FAILED(hr = device->blitter->alloc_private(device)))
4439 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4440 device->shader_backend->shader_free_private(device);
4441 return hr;
4444 /* Recreate the primary swapchain's context */
4445 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4446 if (!swapchain->context)
4448 ERR("Failed to allocate memory for swapchain context array.\n");
4449 device->blitter->free_private(device);
4450 device->shader_backend->shader_free_private(device);
4451 return E_OUTOFMEMORY;
4454 target = swapchain->back_buffers
4455 ? surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0))
4456 : surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
4457 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4459 WARN("Failed to create context.\n");
4460 device->blitter->free_private(device);
4461 device->shader_backend->shader_free_private(device);
4462 HeapFree(GetProcessHeap(), 0, swapchain->context);
4463 return E_FAIL;
4466 swapchain->context[0] = context;
4467 swapchain->num_contexts = 1;
4468 create_dummy_textures(device, context);
4469 context_release(context);
4471 return WINED3D_OK;
4474 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4475 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4476 wined3d_device_reset_cb callback, BOOL reset_state)
4478 struct wined3d_resource *resource, *cursor;
4479 struct wined3d_swapchain *swapchain;
4480 struct wined3d_display_mode m;
4481 BOOL DisplayModeChanged;
4482 HRESULT hr = WINED3D_OK;
4483 unsigned int i;
4485 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4486 device, swapchain_desc, mode, callback, reset_state);
4488 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4490 ERR("Failed to get the first implicit swapchain.\n");
4491 return WINED3DERR_INVALIDCALL;
4493 DisplayModeChanged = swapchain->reapply_mode;
4495 if (reset_state)
4497 if (device->logo_texture)
4499 wined3d_texture_decref(device->logo_texture);
4500 device->logo_texture = NULL;
4502 if (device->cursor_texture)
4504 wined3d_texture_decref(device->cursor_texture);
4505 device->cursor_texture = NULL;
4507 state_unbind_resources(&device->state);
4510 if (device->fb.render_targets)
4512 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4514 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4517 wined3d_device_set_depth_stencil_view(device, NULL);
4519 if (device->onscreen_depth_stencil)
4521 wined3d_surface_decref(device->onscreen_depth_stencil);
4522 device->onscreen_depth_stencil = NULL;
4525 if (reset_state)
4527 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4529 TRACE("Enumerating resource %p.\n", resource);
4530 if (FAILED(hr = callback(resource)))
4531 return hr;
4535 TRACE("New params:\n");
4536 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4537 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4538 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4539 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4540 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4541 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4542 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4543 TRACE("device_window %p\n", swapchain_desc->device_window);
4544 TRACE("windowed %#x\n", swapchain_desc->windowed);
4545 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4546 if (swapchain_desc->enable_auto_depth_stencil)
4547 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4548 TRACE("flags %#x\n", swapchain_desc->flags);
4549 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4550 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4551 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4553 /* No special treatment of these parameters. Just store them */
4554 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4555 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4556 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4557 swapchain->desc.flags = swapchain_desc->flags;
4558 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4559 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4560 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4562 if (swapchain_desc->device_window
4563 && swapchain_desc->device_window != swapchain->desc.device_window)
4565 TRACE("Changing the device window from %p to %p.\n",
4566 swapchain->desc.device_window, swapchain_desc->device_window);
4567 swapchain->desc.device_window = swapchain_desc->device_window;
4568 swapchain->device_window = swapchain_desc->device_window;
4569 wined3d_swapchain_set_window(swapchain, NULL);
4572 if (mode)
4574 DisplayModeChanged = TRUE;
4575 m = *mode;
4577 else if (swapchain_desc->windowed)
4579 m = swapchain->original_mode;
4581 else
4583 m.width = swapchain_desc->backbuffer_width;
4584 m.height = swapchain_desc->backbuffer_height;
4585 m.refresh_rate = swapchain_desc->refresh_rate;
4586 m.format_id = swapchain_desc->backbuffer_format;
4587 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4589 if ((m.width != swapchain->desc.backbuffer_width
4590 || m.height != swapchain->desc.backbuffer_height))
4591 DisplayModeChanged = TRUE;
4594 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4595 || DisplayModeChanged)
4597 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4599 WARN("Failed to set display mode, hr %#x.\n", hr);
4600 return WINED3DERR_INVALIDCALL;
4603 if (!swapchain_desc->windowed)
4605 if (swapchain->desc.windowed)
4607 HWND focus_window = device->create_parms.focus_window;
4608 if (!focus_window)
4609 focus_window = swapchain_desc->device_window;
4610 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4612 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4613 return hr;
4616 /* switch from windowed to fs */
4617 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4618 swapchain_desc->backbuffer_width,
4619 swapchain_desc->backbuffer_height);
4621 else
4623 /* Fullscreen -> fullscreen mode change */
4624 MoveWindow(swapchain->device_window, 0, 0,
4625 swapchain_desc->backbuffer_width,
4626 swapchain_desc->backbuffer_height,
4627 TRUE);
4629 swapchain->d3d_mode = m;
4631 else if (!swapchain->desc.windowed)
4633 /* Fullscreen -> windowed switch */
4634 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4635 wined3d_device_release_focus_window(device);
4637 swapchain->desc.windowed = swapchain_desc->windowed;
4639 else if (!swapchain_desc->windowed)
4641 DWORD style = device->style;
4642 DWORD exStyle = device->exStyle;
4643 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4644 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4645 * Reset to clear up their mess. Guild Wars also loses the device during that.
4647 device->style = 0;
4648 device->exStyle = 0;
4649 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4650 swapchain_desc->backbuffer_width,
4651 swapchain_desc->backbuffer_height);
4652 device->style = style;
4653 device->exStyle = exStyle;
4656 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4657 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4658 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4659 return hr;
4661 if (device->auto_depth_stencil_view)
4663 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4664 device->auto_depth_stencil_view = NULL;
4666 if (swapchain->desc.enable_auto_depth_stencil)
4668 struct wined3d_resource_desc texture_desc;
4669 struct wined3d_texture *texture;
4670 struct wined3d_rendertarget_view_desc view_desc;
4672 TRACE("Creating the depth stencil buffer\n");
4674 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
4675 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4676 texture_desc.multisample_type = swapchain->desc.multisample_type;
4677 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4678 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4679 texture_desc.pool = WINED3D_POOL_DEFAULT;
4680 texture_desc.width = swapchain->desc.backbuffer_width;
4681 texture_desc.height = swapchain->desc.backbuffer_height;
4682 texture_desc.depth = 1;
4683 texture_desc.size = 0;
4685 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4686 device->device_parent, &texture_desc, &texture)))
4688 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4689 return WINED3DERR_INVALIDCALL;
4692 view_desc.format_id = texture->resource.format->id;
4693 view_desc.u.texture.level_idx = 0;
4694 view_desc.u.texture.layer_idx = 0;
4695 view_desc.u.texture.layer_count = 1;
4696 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4697 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4698 wined3d_texture_decref(texture);
4699 if (FAILED(hr))
4701 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4702 return hr;
4705 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4708 if (device->back_buffer_view)
4710 wined3d_rendertarget_view_decref(device->back_buffer_view);
4711 device->back_buffer_view = NULL;
4713 if (swapchain->desc.backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
4714 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
4715 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4717 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4718 return hr;
4721 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4723 if (reset_state)
4725 TRACE("Resetting stateblock.\n");
4726 if (device->recording)
4728 wined3d_stateblock_decref(device->recording);
4729 device->recording = NULL;
4731 wined3d_cs_emit_reset_state(device->cs);
4732 state_cleanup(&device->state);
4734 if (device->d3d_initialized)
4735 delete_opengl_contexts(device, swapchain);
4737 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4738 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4739 ERR("Failed to initialize device state, hr %#x.\n", hr);
4740 device->update_state = &device->state;
4742 device_init_swapchain_state(device, swapchain);
4744 else if (device->back_buffer_view)
4746 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4747 struct wined3d_state *state = &device->state;
4749 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4751 /* Note the min_z / max_z is not reset. */
4752 state->viewport.x = 0;
4753 state->viewport.y = 0;
4754 state->viewport.width = view->width;
4755 state->viewport.height = view->height;
4756 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4758 state->scissor_rect.top = 0;
4759 state->scissor_rect.left = 0;
4760 state->scissor_rect.right = view->width;
4761 state->scissor_rect.bottom = view->height;
4762 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4765 if (reset_state && device->d3d_initialized)
4766 hr = create_primary_opengl_context(device, swapchain);
4768 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4769 * first use
4771 return hr;
4774 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4776 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4778 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4780 return WINED3D_OK;
4784 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4785 struct wined3d_device_creation_parameters *parameters)
4787 TRACE("device %p, parameters %p.\n", device, parameters);
4789 *parameters = device->create_parms;
4792 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4793 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4795 struct wined3d_swapchain *swapchain;
4797 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4798 device, swapchain_idx, flags, ramp);
4800 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4801 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4804 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4805 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4807 struct wined3d_swapchain *swapchain;
4809 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4810 device, swapchain_idx, ramp);
4812 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4813 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4816 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4818 TRACE("device %p, resource %p.\n", device, resource);
4820 list_add_head(&device->resources, &resource->resource_list_entry);
4823 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4825 TRACE("device %p, resource %p.\n", device, resource);
4827 list_remove(&resource->resource_list_entry);
4830 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4832 enum wined3d_resource_type type = resource->type;
4833 unsigned int i;
4835 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4837 context_resource_released(device, resource, type);
4839 switch (type)
4841 case WINED3D_RTYPE_SURFACE:
4843 struct wined3d_surface *surface = surface_from_resource(resource);
4845 if (!device->d3d_initialized) break;
4847 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4849 if (wined3d_rendertarget_view_get_surface(device->fb.render_targets[i]) == surface)
4851 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4852 device->fb.render_targets[i] = NULL;
4856 if (wined3d_rendertarget_view_get_surface(device->fb.depth_stencil) == surface)
4858 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4859 device->fb.depth_stencil = NULL;
4862 break;
4864 case WINED3D_RTYPE_TEXTURE:
4865 case WINED3D_RTYPE_CUBE_TEXTURE:
4866 case WINED3D_RTYPE_VOLUME_TEXTURE:
4867 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4869 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4871 if (device->state.textures[i] == texture)
4873 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4874 device->state.textures[i] = NULL;
4877 if (device->recording && device->update_state->textures[i] == texture)
4879 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4880 texture, device->recording, i);
4881 device->update_state->textures[i] = NULL;
4884 break;
4886 case WINED3D_RTYPE_BUFFER:
4888 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4890 for (i = 0; i < MAX_STREAMS; ++i)
4892 if (device->state.streams[i].buffer == buffer)
4894 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4895 device->state.streams[i].buffer = NULL;
4898 if (device->recording && device->update_state->streams[i].buffer == buffer)
4900 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4901 buffer, device->recording, i);
4902 device->update_state->streams[i].buffer = NULL;
4906 if (device->state.index_buffer == buffer)
4908 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4909 device->state.index_buffer = NULL;
4912 if (device->recording && device->update_state->index_buffer == buffer)
4914 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4915 buffer, device->recording);
4916 device->update_state->index_buffer = NULL;
4919 break;
4921 default:
4922 break;
4925 /* Remove the resource from the resourceStore */
4926 device_resource_remove(device, resource);
4928 TRACE("Resource released.\n");
4931 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4933 struct wined3d_resource *resource;
4935 TRACE("device %p, dc %p.\n", device, dc);
4937 if (!dc)
4938 return NULL;
4940 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4942 if (resource->type == WINED3D_RTYPE_SURFACE)
4944 struct wined3d_surface *s = surface_from_resource(resource);
4946 if (s->hDC == dc)
4948 TRACE("Found surface %p for dc %p.\n", s, dc);
4949 return s;
4954 return NULL;
4957 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4959 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4961 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4964 static const struct wine_rb_functions wined3d_sampler_rb_functions =
4966 wined3d_rb_alloc,
4967 wined3d_rb_realloc,
4968 wined3d_rb_free,
4969 wined3d_sampler_compare,
4972 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4973 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4974 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4976 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4977 const struct fragment_pipeline *fragment_pipeline;
4978 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4979 unsigned int i;
4980 HRESULT hr;
4982 device->ref = 1;
4983 device->wined3d = wined3d;
4984 wined3d_incref(device->wined3d);
4985 device->adapter = wined3d->adapter_count ? adapter : NULL;
4986 device->device_parent = device_parent;
4987 list_init(&device->resources);
4988 list_init(&device->shaders);
4989 device->surface_alignment = surface_alignment;
4991 /* Save the creation parameters. */
4992 device->create_parms.adapter_idx = adapter_idx;
4993 device->create_parms.device_type = device_type;
4994 device->create_parms.focus_window = focus_window;
4995 device->create_parms.flags = flags;
4997 device->shader_backend = adapter->shader_backend;
4999 vertex_pipeline = adapter->vertex_pipe;
5001 fragment_pipeline = adapter->fragment_pipe;
5003 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
5005 ERR("Failed to initialize sampler rbtree.\n");
5006 return E_OUTOFMEMORY;
5009 if (vertex_pipeline->vp_states && fragment_pipeline->states
5010 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5011 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5012 fragment_pipeline, misc_state_template)))
5014 ERR("Failed to compile state table, hr %#x.\n", hr);
5015 wine_rb_destroy(&device->samplers, NULL, NULL);
5016 wined3d_decref(device->wined3d);
5017 return hr;
5020 device->blitter = adapter->blitter;
5022 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
5023 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
5025 ERR("Failed to initialize device state, hr %#x.\n", hr);
5026 goto err;
5028 device->update_state = &device->state;
5030 if (!(device->cs = wined3d_cs_create(device)))
5032 WARN("Failed to create command stream.\n");
5033 state_cleanup(&device->state);
5034 hr = E_FAIL;
5035 goto err;
5038 return WINED3D_OK;
5040 err:
5041 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5043 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5045 wine_rb_destroy(&device->samplers, NULL, NULL);
5046 wined3d_decref(device->wined3d);
5047 return hr;
5051 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5053 DWORD rep = device->StateTable[state].representative;
5054 struct wined3d_context *context;
5055 DWORD idx;
5056 BYTE shift;
5057 UINT i;
5059 for (i = 0; i < device->context_count; ++i)
5061 context = device->contexts[i];
5062 if(isStateDirty(context, rep)) continue;
5064 context->dirtyArray[context->numDirtyEntries++] = rep;
5065 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5066 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5067 context->isStateDirty[idx] |= (1u << shift);
5071 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5072 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5074 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5076 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5077 window, message, wparam, lparam);
5078 if (unicode)
5079 return DefWindowProcW(window, message, wparam, lparam);
5080 else
5081 return DefWindowProcA(window, message, wparam, lparam);
5084 if (message == WM_DESTROY)
5086 TRACE("unregister window %p.\n", window);
5087 wined3d_unregister_window(window);
5089 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5090 ERR("Window %p is not the focus window for device %p.\n", window, device);
5092 else if (message == WM_DISPLAYCHANGE)
5094 device->device_parent->ops->mode_changed(device->device_parent);
5096 else if (message == WM_ACTIVATEAPP)
5098 UINT i;
5100 for (i = 0; i < device->swapchain_count; i++)
5101 wined3d_swapchain_activate(device->swapchains[i], wparam);
5103 device->device_parent->ops->activate(device->device_parent, wparam);
5105 else if (message == WM_SYSCOMMAND)
5107 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5109 if (unicode)
5110 DefWindowProcW(window, message, wparam, lparam);
5111 else
5112 DefWindowProcA(window, message, wparam, lparam);
5116 if (unicode)
5117 return CallWindowProcW(proc, window, message, wparam, lparam);
5118 else
5119 return CallWindowProcA(proc, window, message, wparam, lparam);