d3d8: TRACE fixes.
[wine/multimedia.git] / dlls / wined3d / device.c
blob88f00389552c96fe792d5b4b70df676d820d75f8
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 void device_switch_onscreen_ds(struct wined3d_device *device,
202 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
204 if (device->onscreen_depth_stencil)
206 surface_load_ds_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
208 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
209 device->onscreen_depth_stencil->ds_current_size.cx,
210 device->onscreen_depth_stencil->ds_current_size.cy);
211 wined3d_surface_decref(device->onscreen_depth_stencil);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_surface_incref(device->onscreen_depth_stencil);
217 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
219 /* partial draw rect */
220 if (draw_rect->left || draw_rect->top
221 || draw_rect->right < target->resource.width
222 || draw_rect->bottom < target->resource.height)
223 return FALSE;
225 /* partial clear rect */
226 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
227 || clear_rect->right < target->resource.width
228 || clear_rect->bottom < target->resource.height))
229 return FALSE;
231 return TRUE;
234 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
235 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
237 RECT current_rect, r;
239 if (ds->locations & WINED3D_LOCATION_DISCARDED)
241 /* Depth buffer was discarded, make it entirely current in its new location since
242 * there is no other place where we would get data anyway. */
243 SetRect(out_rect, 0, 0, ds->resource.width, ds->resource.height);
244 return;
247 if (ds->locations & location)
248 SetRect(&current_rect, 0, 0,
249 ds->ds_current_size.cx,
250 ds->ds_current_size.cy);
251 else
252 SetRectEmpty(&current_rect);
254 IntersectRect(&r, draw_rect, &current_rect);
255 if (EqualRect(&r, draw_rect))
257 /* current_rect ⊇ draw_rect, modify only. */
258 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
259 return;
262 if (EqualRect(&r, &current_rect))
264 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
266 if (!clear_rect)
268 /* Full clear, modify only. */
269 *out_rect = *draw_rect;
270 return;
273 IntersectRect(&r, draw_rect, clear_rect);
274 if (EqualRect(&r, draw_rect))
276 /* clear_rect ⊇ draw_rect, modify only. */
277 *out_rect = *draw_rect;
278 return;
282 /* Full load. */
283 surface_load_ds_location(ds, context, location);
284 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
287 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
288 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
289 float depth, DWORD stencil)
291 struct wined3d_surface *target = rt_count ? wined3d_rendertarget_view_get_surface(fb->render_targets[0]) : NULL;
292 struct wined3d_surface *depth_stencil = fb->depth_stencil
293 ? wined3d_rendertarget_view_get_surface(fb->depth_stencil) : NULL;
294 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
295 const struct wined3d_gl_info *gl_info;
296 UINT drawable_width, drawable_height;
297 struct wined3d_context *context;
298 GLbitfield clear_mask = 0;
299 BOOL render_offscreen;
300 unsigned int i;
301 RECT ds_rect;
303 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
304 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
305 * for the cleared parts, and the untouched parts.
307 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
308 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
309 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
310 * checking all this if the dest surface is in the drawable anyway. */
311 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
313 for (i = 0; i < rt_count; ++i)
315 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
316 if (rt)
317 surface_load_location(rt, rt->container->resource.draw_binding);
321 context = context_acquire(device, target);
322 if (!context->valid)
324 context_release(context);
325 WARN("Invalid context, skipping clear.\n");
326 return;
328 gl_info = context->gl_info;
330 if (target)
332 render_offscreen = context->render_offscreen;
333 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
335 else
337 render_offscreen = TRUE;
338 drawable_width = depth_stencil->pow2Width;
339 drawable_height = depth_stencil->pow2Height;
342 if (flags & WINED3DCLEAR_ZBUFFER)
344 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
346 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
347 device_switch_onscreen_ds(device, context, depth_stencil);
348 prepare_ds_clear(depth_stencil, context, location,
349 draw_rect, rect_count, clear_rect, &ds_rect);
352 if (!context_apply_clear_state(context, device, rt_count, fb))
354 context_release(context);
355 WARN("Failed to apply clear state, skipping clear.\n");
356 return;
359 /* Only set the values up once, as they are not changing. */
360 if (flags & WINED3DCLEAR_STENCIL)
362 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
364 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
365 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
367 gl_info->gl_ops.gl.p_glStencilMask(~0U);
368 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
369 gl_info->gl_ops.gl.p_glClearStencil(stencil);
370 checkGLcall("glClearStencil");
371 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
374 if (flags & WINED3DCLEAR_ZBUFFER)
376 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
378 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
380 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
382 gl_info->gl_ops.gl.p_glClearDepth(depth);
383 checkGLcall("glClearDepth");
384 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
387 if (flags & WINED3DCLEAR_TARGET)
389 for (i = 0; i < rt_count; ++i)
391 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
393 if (rt)
395 surface_validate_location(rt, rt->container->resource.draw_binding);
396 surface_invalidate_location(rt, ~rt->container->resource.draw_binding);
400 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
401 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
402 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
403 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
404 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
405 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
406 checkGLcall("glClearColor");
407 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
410 if (!clear_rect)
412 if (render_offscreen)
414 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
415 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
417 else
419 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
420 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
422 checkGLcall("glScissor");
423 gl_info->gl_ops.gl.p_glClear(clear_mask);
424 checkGLcall("glClear");
426 else
428 RECT current_rect;
430 /* Now process each rect in turn. */
431 for (i = 0; i < rect_count; ++i)
433 /* Note that GL uses lower left, width/height. */
434 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
436 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
437 wine_dbgstr_rect(&clear_rect[i]),
438 wine_dbgstr_rect(&current_rect));
440 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
441 * The rectangle is not cleared, no error is returned, but further rectangles are
442 * still cleared if they are valid. */
443 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
445 TRACE("Rectangle with negative dimensions, ignoring.\n");
446 continue;
449 if (render_offscreen)
451 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
452 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
454 else
456 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
457 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
459 checkGLcall("glScissor");
461 gl_info->gl_ops.gl.p_glClear(clear_mask);
462 checkGLcall("glClear");
466 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
467 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
468 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
470 context_release(context);
473 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
475 ULONG refcount = InterlockedIncrement(&device->ref);
477 TRACE("%p increasing refcount to %u.\n", device, refcount);
479 return refcount;
482 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
484 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
486 ERR("Leftover sampler %p.\n", sampler);
489 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
491 ULONG refcount = InterlockedDecrement(&device->ref);
493 TRACE("%p decreasing refcount to %u.\n", device, refcount);
495 if (!refcount)
497 UINT i;
499 wined3d_cs_destroy(device->cs);
501 if (device->recording && wined3d_stateblock_decref(device->recording))
502 FIXME("Something's still holding the recording stateblock.\n");
503 device->recording = NULL;
505 state_cleanup(&device->state);
507 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
509 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
510 device->multistate_funcs[i] = NULL;
513 if (!list_empty(&device->resources))
515 struct wined3d_resource *resource;
517 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
519 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
521 FIXME("Leftover resource %p with type %s (%#x).\n",
522 resource, debug_d3dresourcetype(resource->type), resource->type);
526 if (device->contexts)
527 ERR("Context array not freed!\n");
528 if (device->hardwareCursor)
529 DestroyCursor(device->hardwareCursor);
530 device->hardwareCursor = 0;
532 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
534 wined3d_decref(device->wined3d);
535 device->wined3d = NULL;
536 HeapFree(GetProcessHeap(), 0, device);
537 TRACE("Freed device %p.\n", device);
540 return refcount;
543 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
545 TRACE("device %p.\n", device);
547 return device->swapchain_count;
550 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
552 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
554 if (swapchain_idx >= device->swapchain_count)
556 WARN("swapchain_idx %u >= swapchain_count %u.\n",
557 swapchain_idx, device->swapchain_count);
558 return NULL;
561 return device->swapchains[swapchain_idx];
564 static void device_load_logo(struct wined3d_device *device, const char *filename)
566 struct wined3d_color_key color_key;
567 struct wined3d_resource_desc desc;
568 struct wined3d_surface *surface;
569 HBITMAP hbm;
570 BITMAP bm;
571 HRESULT hr;
572 HDC dcb = NULL, dcs = NULL;
574 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
575 if(hbm)
577 GetObjectA(hbm, sizeof(BITMAP), &bm);
578 dcb = CreateCompatibleDC(NULL);
579 if(!dcb) goto out;
580 SelectObject(dcb, hbm);
582 else
584 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
585 * couldn't be loaded
587 memset(&bm, 0, sizeof(bm));
588 bm.bmWidth = 32;
589 bm.bmHeight = 32;
592 desc.resource_type = WINED3D_RTYPE_TEXTURE;
593 desc.format = WINED3DFMT_B5G6R5_UNORM;
594 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
595 desc.multisample_quality = 0;
596 desc.usage = WINED3DUSAGE_DYNAMIC;
597 desc.pool = WINED3D_POOL_DEFAULT;
598 desc.width = bm.bmWidth;
599 desc.height = bm.bmHeight;
600 desc.depth = 1;
601 desc.size = 0;
602 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
603 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
605 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
606 goto out;
608 surface = surface_from_resource(wined3d_texture_get_sub_resource(device->logo_texture, 0));
610 if (dcb)
612 if (FAILED(hr = wined3d_surface_getdc(surface, &dcs)))
613 goto out;
614 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
615 wined3d_surface_releasedc(surface, dcs);
617 color_key.color_space_low_value = 0;
618 color_key.color_space_high_value = 0;
619 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
621 else
623 const RECT rect = {0, 0, surface->resource.width, surface->resource.height};
624 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
626 /* Fill the surface with a white color to show that wined3d is there */
627 surface_color_fill(surface, &rect, &c);
630 out:
631 if (dcb) DeleteDC(dcb);
632 if (hbm) DeleteObject(hbm);
635 /* Context activation is done by the caller. */
636 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
638 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
639 unsigned int i, j, count;
640 /* Under DirectX you can sample even if no texture is bound, whereas
641 * OpenGL will only allow that when a valid texture is bound.
642 * We emulate this by creating dummy textures and binding them
643 * to each texture stage when the currently set D3D texture is NULL. */
645 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
646 for (i = 0; i < count; ++i)
648 DWORD color = 0x000000ff;
650 /* Make appropriate texture active */
651 context_active_texture(context, gl_info, i);
653 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
654 checkGLcall("glGenTextures");
655 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
657 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
658 checkGLcall("glBindTexture");
660 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
661 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
662 checkGLcall("glTexImage2D");
664 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
666 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
667 checkGLcall("glGenTextures");
668 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
670 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
671 checkGLcall("glBindTexture");
673 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
674 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
675 checkGLcall("glTexImage2D");
678 if (gl_info->supported[EXT_TEXTURE3D])
680 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
681 checkGLcall("glGenTextures");
682 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
684 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
685 checkGLcall("glBindTexture");
687 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
688 checkGLcall("glTexImage3D");
691 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
693 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
694 checkGLcall("glGenTextures");
695 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
697 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
698 checkGLcall("glBindTexture");
700 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
702 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
703 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
704 checkGLcall("glTexImage2D");
710 /* Context activation is done by the caller. */
711 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
713 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
715 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
717 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
718 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
721 if (gl_info->supported[EXT_TEXTURE3D])
723 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
724 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
727 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
729 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
730 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
733 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
734 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
736 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
737 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
738 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
739 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
742 static LONG fullscreen_style(LONG style)
744 /* Make sure the window is managed, otherwise we won't get keyboard input. */
745 style |= WS_POPUP | WS_SYSMENU;
746 style &= ~(WS_CAPTION | WS_THICKFRAME);
748 return style;
751 static LONG fullscreen_exstyle(LONG exstyle)
753 /* Filter out window decorations. */
754 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
756 return exstyle;
759 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
761 BOOL filter_messages;
762 LONG style, exstyle;
764 TRACE("Setting up window %p for fullscreen mode.\n", window);
766 if (device->style || device->exStyle)
768 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
769 window, device->style, device->exStyle);
772 device->style = GetWindowLongW(window, GWL_STYLE);
773 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
775 style = fullscreen_style(device->style);
776 exstyle = fullscreen_exstyle(device->exStyle);
778 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
779 device->style, device->exStyle, style, exstyle);
781 filter_messages = device->filter_messages;
782 device->filter_messages = TRUE;
784 SetWindowLongW(window, GWL_STYLE, style);
785 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
786 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
788 device->filter_messages = filter_messages;
791 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
793 BOOL filter_messages;
794 LONG style, exstyle;
796 if (!device->style && !device->exStyle) return;
798 style = GetWindowLongW(window, GWL_STYLE);
799 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
801 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
802 * application, and we want to ignore them in the test below, since it's
803 * not the application's fault that they changed. Additionally, we want to
804 * preserve the current status of these flags (i.e. don't restore them) to
805 * more closely emulate the behavior of Direct3D, which leaves these flags
806 * alone when returning to windowed mode. */
807 device->style ^= (device->style ^ style) & WS_VISIBLE;
808 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
810 TRACE("Restoring window style of window %p to %08x, %08x.\n",
811 window, device->style, device->exStyle);
813 filter_messages = device->filter_messages;
814 device->filter_messages = TRUE;
816 /* Only restore the style if the application didn't modify it during the
817 * fullscreen phase. Some applications change it before calling Reset()
818 * when switching between windowed and fullscreen modes (HL2), some
819 * depend on the original style (Eve Online). */
820 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
822 SetWindowLongW(window, GWL_STYLE, device->style);
823 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
825 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
827 device->filter_messages = filter_messages;
829 /* Delete the old values. */
830 device->style = 0;
831 device->exStyle = 0;
834 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
836 TRACE("device %p, window %p.\n", device, window);
838 if (!wined3d_register_window(window, device))
840 ERR("Failed to register window %p.\n", window);
841 return E_FAIL;
844 InterlockedExchangePointer((void **)&device->focus_window, window);
845 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
847 return WINED3D_OK;
850 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
852 TRACE("device %p.\n", device);
854 if (device->focus_window) wined3d_unregister_window(device->focus_window);
855 InterlockedExchangePointer((void **)&device->focus_window, NULL);
858 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
860 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
861 unsigned int i;
863 if (device->fb.render_targets)
865 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
867 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
869 if (device->back_buffer_view)
870 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
873 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
874 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
877 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
878 struct wined3d_swapchain_desc *swapchain_desc)
880 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
881 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
882 struct wined3d_swapchain *swapchain = NULL;
883 struct wined3d_context *context;
884 DWORD clear_flags = 0;
885 HRESULT hr;
887 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
889 if (device->d3d_initialized)
890 return WINED3DERR_INVALIDCALL;
891 if (device->wined3d->flags & WINED3D_NO3D)
892 return WINED3DERR_INVALIDCALL;
894 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
895 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
897 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
898 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
900 TRACE("Shader private data couldn't be allocated\n");
901 goto err_out;
903 if (FAILED(hr = device->blitter->alloc_private(device)))
905 TRACE("Blitter private data couldn't be allocated\n");
906 goto err_out;
909 /* Setup the implicit swapchain. This also initializes a context. */
910 TRACE("Creating implicit swapchain\n");
911 hr = device->device_parent->ops->create_swapchain(device->device_parent,
912 swapchain_desc, &swapchain);
913 if (FAILED(hr))
915 WARN("Failed to create implicit swapchain\n");
916 goto err_out;
919 if (swapchain_desc->backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
920 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
921 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
923 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
924 goto err_out;
927 device->swapchain_count = 1;
928 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
929 if (!device->swapchains)
931 ERR("Out of memory!\n");
932 goto err_out;
934 device->swapchains[0] = swapchain;
935 device_init_swapchain_state(device, swapchain);
937 context = context_acquire(device,
938 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)));
940 create_dummy_textures(device, context);
942 device->contexts[0]->last_was_rhw = 0;
944 switch (wined3d_settings.offscreen_rendering_mode)
946 case ORM_FBO:
947 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
948 break;
950 case ORM_BACKBUFFER:
952 if (context_get_current()->aux_buffers > 0)
954 TRACE("Using auxiliary buffer for offscreen rendering\n");
955 device->offscreenBuffer = GL_AUX0;
957 else
959 TRACE("Using back buffer for offscreen rendering\n");
960 device->offscreenBuffer = GL_BACK;
965 TRACE("All defaults now set up, leaving 3D init.\n");
967 context_release(context);
969 /* Clear the screen */
970 if (swapchain->back_buffers && swapchain->back_buffers[0])
971 clear_flags |= WINED3DCLEAR_TARGET;
972 if (swapchain_desc->enable_auto_depth_stencil)
973 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
974 if (clear_flags)
975 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
977 device->d3d_initialized = TRUE;
979 if (wined3d_settings.logo)
980 device_load_logo(device, wined3d_settings.logo);
981 return WINED3D_OK;
983 err_out:
984 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
985 HeapFree(GetProcessHeap(), 0, device->swapchains);
986 device->swapchain_count = 0;
987 if (device->back_buffer_view)
988 wined3d_rendertarget_view_decref(device->back_buffer_view);
989 if (swapchain)
990 wined3d_swapchain_decref(swapchain);
991 if (device->blit_priv)
992 device->blitter->free_private(device);
993 if (device->shader_priv)
994 device->shader_backend->shader_free_private(device);
996 return hr;
999 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1000 struct wined3d_swapchain_desc *swapchain_desc)
1002 struct wined3d_swapchain *swapchain = NULL;
1003 HRESULT hr;
1005 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1007 /* Setup the implicit swapchain */
1008 TRACE("Creating implicit swapchain\n");
1009 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1010 swapchain_desc, &swapchain);
1011 if (FAILED(hr))
1013 WARN("Failed to create implicit swapchain\n");
1014 goto err_out;
1017 device->swapchain_count = 1;
1018 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1019 if (!device->swapchains)
1021 ERR("Out of memory!\n");
1022 goto err_out;
1024 device->swapchains[0] = swapchain;
1025 return WINED3D_OK;
1027 err_out:
1028 wined3d_swapchain_decref(swapchain);
1029 return hr;
1032 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1034 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1036 wined3d_sampler_decref(sampler);
1039 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1041 struct wined3d_resource *resource, *cursor;
1042 const struct wined3d_gl_info *gl_info;
1043 struct wined3d_context *context;
1044 struct wined3d_surface *surface;
1045 UINT i;
1047 TRACE("device %p.\n", device);
1049 if (!device->d3d_initialized)
1050 return WINED3DERR_INVALIDCALL;
1052 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1053 * it was created. Thus make sure a context is active for the glDelete* calls
1055 context = context_acquire(device, NULL);
1056 gl_info = context->gl_info;
1058 if (device->logo_texture)
1059 wined3d_texture_decref(device->logo_texture);
1060 if (device->cursor_texture)
1061 wined3d_texture_decref(device->cursor_texture);
1063 state_unbind_resources(&device->state);
1065 /* Unload resources */
1066 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1068 TRACE("Unloading resource %p.\n", resource);
1070 resource->resource_ops->resource_unload(resource);
1073 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1075 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1076 * private data, it might contain opengl pointers
1078 if (device->depth_blt_texture)
1080 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1081 device->depth_blt_texture = 0;
1084 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1085 device->blitter->free_private(device);
1086 device->shader_backend->shader_free_private(device);
1087 destroy_dummy_textures(device, gl_info);
1089 /* Release the context again as soon as possible. In particular,
1090 * releasing the render target views below may release the last reference
1091 * to the swapchain associated with this context, which in turn will
1092 * destroy the context. */
1093 context_release(context);
1095 /* Release the buffers (with sanity checks)*/
1096 if (device->onscreen_depth_stencil)
1098 surface = device->onscreen_depth_stencil;
1099 device->onscreen_depth_stencil = NULL;
1100 wined3d_surface_decref(surface);
1103 if (device->fb.depth_stencil)
1105 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1107 TRACE("Releasing depth/stencil view %p.\n", view);
1109 device->fb.depth_stencil = NULL;
1110 wined3d_rendertarget_view_decref(view);
1113 if (device->auto_depth_stencil_view)
1115 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1117 device->auto_depth_stencil_view = NULL;
1118 if (wined3d_rendertarget_view_decref(view))
1119 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1122 for (i = 0; i < gl_info->limits.buffers; ++i)
1124 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1126 if (device->back_buffer_view)
1128 wined3d_rendertarget_view_decref(device->back_buffer_view);
1129 device->back_buffer_view = NULL;
1132 for (i = 0; i < device->swapchain_count; ++i)
1134 TRACE("Releasing the implicit swapchain %u.\n", i);
1135 if (wined3d_swapchain_decref(device->swapchains[i]))
1136 FIXME("Something's still holding the implicit swapchain.\n");
1139 HeapFree(GetProcessHeap(), 0, device->swapchains);
1140 device->swapchains = NULL;
1141 device->swapchain_count = 0;
1143 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1144 device->fb.render_targets = NULL;
1146 device->d3d_initialized = FALSE;
1148 return WINED3D_OK;
1151 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1153 unsigned int i;
1155 for (i = 0; i < device->swapchain_count; ++i)
1157 TRACE("Releasing the implicit swapchain %u.\n", i);
1158 if (wined3d_swapchain_decref(device->swapchains[i]))
1159 FIXME("Something's still holding the implicit swapchain.\n");
1162 HeapFree(GetProcessHeap(), 0, device->swapchains);
1163 device->swapchains = NULL;
1164 device->swapchain_count = 0;
1165 return WINED3D_OK;
1168 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1169 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1170 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1172 * There is no way to deactivate thread safety once it is enabled.
1174 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1176 TRACE("device %p.\n", device);
1178 /* For now just store the flag (needed in case of ddraw). */
1179 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1182 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1184 TRACE("device %p.\n", device);
1186 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1187 wine_dbgstr_longlong(device->adapter->vram_bytes),
1188 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1189 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1191 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1194 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1195 struct wined3d_buffer *buffer, UINT offset)
1197 struct wined3d_stream_output *stream;
1198 struct wined3d_buffer *prev_buffer;
1200 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1202 if (idx >= MAX_STREAM_OUT)
1204 WARN("Invalid stream output %u.\n", idx);
1205 return;
1208 stream = &device->update_state->stream_output[idx];
1209 prev_buffer = stream->buffer;
1211 if (buffer)
1212 wined3d_buffer_incref(buffer);
1213 stream->buffer = buffer;
1214 stream->offset = offset;
1215 if (!device->recording)
1216 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1217 if (prev_buffer)
1218 wined3d_buffer_decref(prev_buffer);
1221 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1222 UINT idx, UINT *offset)
1224 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1226 if (idx >= MAX_STREAM_OUT)
1228 WARN("Invalid stream output %u.\n", idx);
1229 return NULL;
1232 *offset = device->state.stream_output[idx].offset;
1233 return device->state.stream_output[idx].buffer;
1236 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1237 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1239 struct wined3d_stream_state *stream;
1240 struct wined3d_buffer *prev_buffer;
1242 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1243 device, stream_idx, buffer, offset, stride);
1245 if (stream_idx >= MAX_STREAMS)
1247 WARN("Stream index %u out of range.\n", stream_idx);
1248 return WINED3DERR_INVALIDCALL;
1250 else if (offset & 0x3)
1252 WARN("Offset %u is not 4 byte aligned.\n", offset);
1253 return WINED3DERR_INVALIDCALL;
1256 stream = &device->update_state->streams[stream_idx];
1257 prev_buffer = stream->buffer;
1259 if (device->recording)
1260 device->recording->changed.streamSource |= 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_present(const struct wined3d_device *device, const RECT *src_rect,
3294 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3296 UINT i;
3298 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3299 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3300 dst_window_override, dirty_region, flags);
3302 for (i = 0; i < device->swapchain_count; ++i)
3304 wined3d_swapchain_present(device->swapchains[i], src_rect,
3305 dst_rect, dst_window_override, dirty_region, flags);
3308 return WINED3D_OK;
3311 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3312 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3314 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3315 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3317 if (!rect_count && rects)
3319 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3320 return WINED3D_OK;
3323 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3325 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3326 if (!ds)
3328 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3329 /* TODO: What about depth stencil buffers without stencil bits? */
3330 return WINED3DERR_INVALIDCALL;
3332 else if (flags & WINED3DCLEAR_TARGET)
3334 if (ds->width < device->fb.render_targets[0]->width
3335 || ds->height < device->fb.render_targets[0]->height)
3337 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3338 return WINED3D_OK;
3343 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3345 return WINED3D_OK;
3348 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3349 struct wined3d_query *predicate, BOOL value)
3351 struct wined3d_query *prev;
3353 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3355 prev = device->update_state->predicate;
3356 if (predicate)
3358 FIXME("Predicated rendering not implemented.\n");
3359 wined3d_query_incref(predicate);
3361 device->update_state->predicate = predicate;
3362 device->update_state->predicate_value = value;
3363 if (!device->recording)
3364 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3365 if (prev)
3366 wined3d_query_decref(prev);
3369 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3371 TRACE("device %p, value %p.\n", device, value);
3373 *value = device->state.predicate_value;
3374 return device->state.predicate;
3377 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3378 enum wined3d_primitive_type primitive_type)
3380 GLenum gl_primitive_type, prev;
3382 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3384 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3385 prev = device->update_state->gl_primitive_type;
3386 device->update_state->gl_primitive_type = gl_primitive_type;
3387 if (device->recording)
3388 device->recording->changed.primitive_type = TRUE;
3389 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3390 device_invalidate_state(device, STATE_POINT_ENABLE);
3393 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3394 enum wined3d_primitive_type *primitive_type)
3396 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3398 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3400 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3403 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3405 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3407 if (!device->state.vertex_declaration)
3409 WARN("Called without a valid vertex declaration set.\n");
3410 return WINED3DERR_INVALIDCALL;
3413 if (device->state.load_base_vertex_index)
3415 device->state.load_base_vertex_index = 0;
3416 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3419 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3421 return WINED3D_OK;
3424 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3425 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3427 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3428 device, start_vertex, vertex_count, start_instance, instance_count);
3430 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3433 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3435 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3437 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3439 if (!device->state.index_buffer)
3441 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3442 * without an index buffer set. (The first time at least...)
3443 * D3D8 simply dies, but I doubt it can do much harm to return
3444 * D3DERR_INVALIDCALL there as well. */
3445 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3446 return WINED3DERR_INVALIDCALL;
3449 if (!device->state.vertex_declaration)
3451 WARN("Called without a valid vertex declaration set.\n");
3452 return WINED3DERR_INVALIDCALL;
3455 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3456 device->state.load_base_vertex_index != device->state.base_vertex_index)
3458 device->state.load_base_vertex_index = device->state.base_vertex_index;
3459 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3462 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3464 return WINED3D_OK;
3467 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3468 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3470 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3471 device, start_idx, index_count, start_instance, instance_count);
3473 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3476 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3477 static HRESULT device_update_volume(struct wined3d_device *device,
3478 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3480 struct wined3d_const_bo_address data;
3481 struct wined3d_map_desc src;
3482 HRESULT hr;
3483 struct wined3d_context *context;
3485 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3486 device, src_volume, dst_volume);
3488 if (src_volume->resource.format != dst_volume->resource.format)
3490 FIXME("Source and destination formats do not match.\n");
3491 return WINED3DERR_INVALIDCALL;
3493 if (src_volume->resource.width != dst_volume->resource.width
3494 || src_volume->resource.height != dst_volume->resource.height
3495 || src_volume->resource.depth != dst_volume->resource.depth)
3497 FIXME("Source and destination sizes do not match.\n");
3498 return WINED3DERR_INVALIDCALL;
3501 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3502 return hr;
3504 context = context_acquire(device, NULL);
3506 /* Only a prepare, since we're uploading the entire volume. */
3507 wined3d_texture_prepare_texture(dst_volume->container, context, FALSE);
3508 wined3d_texture_bind_and_dirtify(dst_volume->container, context, FALSE);
3510 data.buffer_object = 0;
3511 data.addr = src.data;
3512 wined3d_volume_upload_data(dst_volume, context, &data);
3513 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3515 context_release(context);
3517 hr = wined3d_volume_unmap(src_volume);
3519 return hr;
3522 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3523 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3525 enum wined3d_resource_type type;
3526 unsigned int level_count, i, j, src_size, dst_size, src_skip_levels = 0;
3527 HRESULT hr;
3528 struct wined3d_context *context;
3530 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3532 /* Verify that the source and destination textures are non-NULL. */
3533 if (!src_texture || !dst_texture)
3535 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3536 return WINED3DERR_INVALIDCALL;
3539 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3541 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3542 return WINED3DERR_INVALIDCALL;
3544 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3546 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3547 return WINED3DERR_INVALIDCALL;
3550 /* Verify that the source and destination textures are the same type. */
3551 type = src_texture->resource.type;
3552 if (dst_texture->resource.type != type)
3554 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3555 return WINED3DERR_INVALIDCALL;
3558 level_count = min(wined3d_texture_get_level_count(src_texture),
3559 wined3d_texture_get_level_count(dst_texture));
3561 src_size = max(src_texture->resource.width, src_texture->resource.height);
3562 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3563 if (type == WINED3D_RTYPE_VOLUME)
3565 src_size = max(src_size, src_texture->resource.depth);
3566 dst_size = max(dst_size, dst_texture->resource.depth);
3568 while (src_size > dst_size)
3570 src_size >>= 1;
3571 ++src_skip_levels;
3574 /* Make sure that the destination texture is loaded. */
3575 context = context_acquire(device, NULL);
3576 wined3d_texture_load(dst_texture, context, FALSE);
3577 context_release(context);
3579 /* Update every surface level of the texture. */
3580 switch (type)
3582 case WINED3D_RTYPE_TEXTURE:
3584 struct wined3d_surface *src_surface;
3585 struct wined3d_surface *dst_surface;
3587 for (i = 0; i < level_count; ++i)
3589 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture,
3590 i + src_skip_levels));
3591 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3592 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3593 if (FAILED(hr))
3595 WARN("Failed to update surface, hr %#x.\n", hr);
3596 return hr;
3599 break;
3602 case WINED3D_RTYPE_CUBE_TEXTURE:
3604 struct wined3d_surface *src_surface;
3605 struct wined3d_surface *dst_surface;
3606 unsigned int src_levels = wined3d_texture_get_level_count(src_texture);
3607 unsigned int dst_levels = wined3d_texture_get_level_count(dst_texture);
3609 for (i = 0; i < 6; ++i)
3611 for (j = 0; j < level_count; ++j)
3613 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture,
3614 i * src_levels + j + src_skip_levels));
3615 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture,
3616 i * dst_levels + j));
3617 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3618 if (FAILED(hr))
3620 WARN("Failed to update surface, hr %#x.\n", hr);
3621 return hr;
3625 break;
3628 case WINED3D_RTYPE_VOLUME_TEXTURE:
3630 for (i = 0; i < level_count; ++i)
3632 hr = device_update_volume(device,
3633 volume_from_resource(wined3d_texture_get_sub_resource(src_texture,
3634 i + src_skip_levels)),
3635 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3636 if (FAILED(hr))
3638 WARN("Failed to update volume, hr %#x.\n", hr);
3639 return hr;
3642 break;
3645 default:
3646 FIXME("Unsupported texture type %#x.\n", type);
3647 return WINED3DERR_INVALIDCALL;
3650 return WINED3D_OK;
3653 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3654 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3656 struct wined3d_swapchain *swapchain;
3658 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3660 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3661 return WINED3DERR_INVALIDCALL;
3663 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3666 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3668 const struct wined3d_state *state = &device->state;
3669 struct wined3d_texture *texture;
3670 DWORD i;
3672 TRACE("device %p, num_passes %p.\n", device, num_passes);
3674 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3676 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3678 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3679 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3681 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3683 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3684 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3687 texture = state->textures[i];
3688 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3690 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3692 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3693 return E_FAIL;
3695 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3697 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3698 return E_FAIL;
3700 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3701 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3703 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3704 return E_FAIL;
3708 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3709 || state->render_states[WINED3D_RS_STENCILENABLE])
3711 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3712 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3714 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3716 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3717 return WINED3DERR_CONFLICTINGRENDERSTATE;
3721 /* return a sensible default */
3722 *num_passes = 1;
3724 TRACE("returning D3D_OK\n");
3725 return WINED3D_OK;
3728 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3730 static BOOL warned;
3732 TRACE("device %p, software %#x.\n", device, software);
3734 if (!warned)
3736 FIXME("device %p, software %#x stub!\n", device, software);
3737 warned = TRUE;
3740 device->softwareVertexProcessing = software;
3743 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3745 static BOOL warned;
3747 TRACE("device %p.\n", device);
3749 if (!warned)
3751 TRACE("device %p stub!\n", device);
3752 warned = TRUE;
3755 return device->softwareVertexProcessing;
3758 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3759 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3761 struct wined3d_swapchain *swapchain;
3763 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3764 device, swapchain_idx, raster_status);
3766 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3767 return WINED3DERR_INVALIDCALL;
3769 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3772 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3774 static BOOL warned;
3776 TRACE("device %p, segments %.8e.\n", device, segments);
3778 if (segments != 0.0f)
3780 if (!warned)
3782 FIXME("device %p, segments %.8e stub!\n", device, segments);
3783 warned = TRUE;
3787 return WINED3D_OK;
3790 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3792 static BOOL warned;
3794 TRACE("device %p.\n", device);
3796 if (!warned)
3798 FIXME("device %p stub!\n", device);
3799 warned = TRUE;
3802 return 0.0f;
3805 /* FIXME: Callers should probably use wined3d_device_update_sub_resource()
3806 * instead. */
3807 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3808 struct wined3d_surface *src_surface, const RECT *src_rect,
3809 struct wined3d_surface *dst_surface, const POINT *dst_point)
3811 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3812 device, src_surface, wine_dbgstr_rect(src_rect),
3813 dst_surface, wine_dbgstr_point(dst_point));
3815 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3817 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3818 src_surface, dst_surface);
3819 return WINED3DERR_INVALIDCALL;
3822 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3825 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3826 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3828 struct wined3d_surface *dst_surface, *src_surface;
3829 struct wined3d_texture *dst_texture, *src_texture;
3830 unsigned int i, count;
3831 HRESULT hr;
3833 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3835 if (src_resource == dst_resource)
3837 WARN("Source and destination are the same resource.\n");
3838 return;
3841 if (src_resource->type != dst_resource->type)
3843 WARN("Resource types (%s / %s) don't match.\n",
3844 debug_d3dresourcetype(dst_resource->type),
3845 debug_d3dresourcetype(src_resource->type));
3846 return;
3849 if (src_resource->width != dst_resource->width
3850 || src_resource->height != dst_resource->height
3851 || src_resource->depth != dst_resource->depth)
3853 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3854 dst_resource->width, dst_resource->height, dst_resource->depth,
3855 src_resource->width, src_resource->height, src_resource->depth);
3856 return;
3859 if (src_resource->format->id != dst_resource->format->id)
3861 WARN("Resource formats (%s / %s) don't match.\n",
3862 debug_d3dformat(dst_resource->format->id),
3863 debug_d3dformat(src_resource->format->id));
3864 return;
3867 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3869 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3870 return;
3873 dst_texture = wined3d_texture_from_resource(dst_resource);
3874 src_texture = wined3d_texture_from_resource(src_resource);
3876 if (src_texture->layer_count != dst_texture->layer_count
3877 || src_texture->level_count != dst_texture->level_count)
3879 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3880 dst_texture->layer_count, dst_texture->level_count,
3881 src_texture->layer_count, src_texture->level_count);
3882 return;
3885 count = dst_texture->layer_count * dst_texture->level_count;
3886 for (i = 0; i < count; ++i)
3888 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3889 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3891 if (FAILED(hr = wined3d_surface_blt(dst_surface, NULL, src_surface, NULL, 0, NULL, WINED3D_TEXF_POINT)))
3892 ERR("Failed to blit, subresource %u, hr %#x.\n", i, hr);
3896 void CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3897 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3898 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3899 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3901 struct wined3d_surface *dst_surface, *src_surface;
3902 struct wined3d_texture *dst_texture, *src_texture;
3903 struct wined3d_resource *tmp;
3904 RECT dst_rect, src_rect;
3905 HRESULT hr;
3907 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3908 "src_resource %p, src_sub_resource_idx %u, src_box %p.\n",
3909 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3910 src_resource, src_sub_resource_idx, src_box);
3912 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3914 WARN("Source and destination are the same sub-resource.\n");
3915 return;
3918 if (src_resource->type != dst_resource->type)
3920 WARN("Resource types (%s / %s) don't match.\n",
3921 debug_d3dresourcetype(dst_resource->type),
3922 debug_d3dresourcetype(src_resource->type));
3923 return;
3926 if (src_resource->format->id != dst_resource->format->id)
3928 WARN("Resource formats (%s / %s) don't match.\n",
3929 debug_d3dformat(dst_resource->format->id),
3930 debug_d3dformat(src_resource->format->id));
3931 return;
3934 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3936 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3937 return;
3940 dst_texture = wined3d_texture_from_resource(dst_resource);
3941 if (!(tmp = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
3943 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3944 return;
3946 dst_surface = surface_from_resource(tmp);
3948 src_texture = wined3d_texture_from_resource(src_resource);
3949 if (!(tmp = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx)))
3951 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3952 return;
3954 src_surface = surface_from_resource(tmp);
3956 dst_rect.left = dst_x;
3957 dst_rect.top = dst_y;
3958 dst_rect.right = dst_x + (src_box->right - src_box->left);
3959 dst_rect.bottom = dst_y + (src_box->bottom - src_box->top);
3961 src_rect.left = src_box->left;
3962 src_rect.top = src_box->top;
3963 src_rect.right = src_box->right;
3964 src_rect.bottom = src_box->bottom;
3966 if (FAILED(hr = wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3967 ERR("Failed to blit, hr %#x.\n", hr);
3970 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
3971 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
3972 unsigned int depth_pitch)
3974 struct wined3d_resource *sub_resource;
3975 const struct wined3d_gl_info *gl_info;
3976 struct wined3d_const_bo_address addr;
3977 struct wined3d_context *context;
3978 struct wined3d_texture *texture;
3979 struct wined3d_surface *surface;
3980 POINT dst_point;
3981 RECT src_rect;
3983 TRACE("device %p, resource %p, sub_resource_idx %u, box %p, data %p, row_pitch %u, depth_pitch %u.\n",
3984 device, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
3986 if (resource->type != WINED3D_RTYPE_TEXTURE)
3988 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
3989 return;
3992 texture = wined3d_texture_from_resource(resource);
3993 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3995 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
3996 return;
3998 surface = surface_from_resource(sub_resource);
4000 src_rect.left = 0;
4001 src_rect.top = 0;
4002 if (box)
4004 if (box->left >= box->right || box->right > sub_resource->width
4005 || box->top >= box->bottom || box->bottom > sub_resource->height)
4007 WARN("Invalid box (%u, %u, %u)->(%u, %u, %u) specified.\n",
4008 box->left, box->top, box->front, box->right, box->bottom, box->back);
4009 return;
4012 src_rect.right = box->right - box->left;
4013 src_rect.bottom = box->bottom - box->top;
4014 dst_point.x = box->left;
4015 dst_point.y = box->top;
4017 else
4019 src_rect.right = sub_resource->width;
4020 src_rect.bottom = sub_resource->height;
4021 dst_point.x = 0;
4022 dst_point.y = 0;
4025 addr.buffer_object = 0;
4026 addr.addr = data;
4028 context = context_acquire(resource->device, NULL);
4029 gl_info = context->gl_info;
4031 /* Only load the surface for partial updates. */
4032 if (!dst_point.x && !dst_point.y && src_rect.right == sub_resource->width
4033 && src_rect.bottom == sub_resource->height)
4034 wined3d_texture_prepare_texture(texture, context, FALSE);
4035 else
4036 surface_load_location(surface, WINED3D_LOCATION_TEXTURE_RGB);
4037 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4039 wined3d_surface_upload_data(surface, gl_info, resource->format,
4040 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4042 context_release(context);
4044 surface_validate_location(surface, WINED3D_LOCATION_TEXTURE_RGB);
4045 surface_invalidate_location(surface, ~WINED3D_LOCATION_TEXTURE_RGB);
4048 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4049 struct wined3d_rendertarget_view *view, const RECT *rect, const struct wined3d_color *color)
4051 struct wined3d_resource *resource;
4052 RECT r;
4054 TRACE("device %p, view %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4055 device, view, wine_dbgstr_rect(rect), color->r, color->g, color->b, color->a);
4057 resource = view->resource;
4058 if (resource->type != WINED3D_RTYPE_TEXTURE && resource->type != WINED3D_RTYPE_CUBE_TEXTURE)
4060 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4061 return WINED3DERR_INVALIDCALL;
4064 if (view->depth > 1)
4066 FIXME("Layered clears not implemented.\n");
4067 return WINED3DERR_INVALIDCALL;
4070 if (!rect)
4072 SetRect(&r, 0, 0, view->width, view->height);
4073 rect = &r;
4076 resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource), view->sub_resource_idx);
4078 return surface_color_fill(surface_from_resource(resource), rect, color);
4081 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4082 unsigned int view_idx)
4084 TRACE("device %p, view_idx %u.\n", device, view_idx);
4086 if (view_idx >= device->adapter->gl_info.limits.buffers)
4088 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4089 return NULL;
4092 return device->fb.render_targets[view_idx];
4095 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4097 TRACE("device %p.\n", device);
4099 return device->fb.depth_stencil;
4102 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4103 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4105 struct wined3d_rendertarget_view *prev;
4107 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4108 device, view_idx, view, set_viewport);
4110 if (view_idx >= device->adapter->gl_info.limits.buffers)
4112 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4113 return WINED3DERR_INVALIDCALL;
4116 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4118 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4119 return WINED3DERR_INVALIDCALL;
4122 /* Set the viewport and scissor rectangles, if requested. Tests show that
4123 * stateblock recording is ignored, the change goes directly into the
4124 * primary stateblock. */
4125 if (!view_idx && set_viewport)
4127 struct wined3d_state *state = &device->state;
4129 state->viewport.x = 0;
4130 state->viewport.y = 0;
4131 state->viewport.width = view->width;
4132 state->viewport.height = view->height;
4133 state->viewport.min_z = 0.0f;
4134 state->viewport.max_z = 1.0f;
4135 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4137 state->scissor_rect.top = 0;
4138 state->scissor_rect.left = 0;
4139 state->scissor_rect.right = view->width;
4140 state->scissor_rect.bottom = view->height;
4141 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4145 prev = device->fb.render_targets[view_idx];
4146 if (view == prev)
4147 return WINED3D_OK;
4149 if (view)
4150 wined3d_rendertarget_view_incref(view);
4151 device->fb.render_targets[view_idx] = view;
4152 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4153 /* Release after the assignment, to prevent device_resource_released()
4154 * from seeing the surface as still in use. */
4155 if (prev)
4156 wined3d_rendertarget_view_decref(prev);
4158 return WINED3D_OK;
4161 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4163 struct wined3d_rendertarget_view *prev;
4165 TRACE("device %p, view %p.\n", device, view);
4167 prev = device->fb.depth_stencil;
4168 if (prev == view)
4170 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4171 return;
4174 if ((device->fb.depth_stencil = view))
4175 wined3d_rendertarget_view_incref(view);
4176 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4177 if (prev)
4178 wined3d_rendertarget_view_decref(prev);
4181 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4182 struct wined3d_surface *cursor_image)
4184 struct wined3d_sub_resource_data data;
4185 struct wined3d_resource_desc desc;
4186 struct wined3d_map_desc map_desc;
4187 struct wined3d_texture *texture;
4188 HRESULT hr;
4190 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4192 ERR("Failed to map source surface.\n");
4193 return NULL;
4196 data.data = map_desc.data;
4197 data.row_pitch = map_desc.row_pitch;
4198 data.slice_pitch = map_desc.slice_pitch;
4200 desc.resource_type = WINED3D_RTYPE_TEXTURE;
4201 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4202 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4203 desc.multisample_quality = 0;
4204 desc.usage = WINED3DUSAGE_DYNAMIC;
4205 desc.pool = WINED3D_POOL_DEFAULT;
4206 desc.width = cursor_image->resource.width;
4207 desc.height = cursor_image->resource.height;
4208 desc.depth = 1;
4209 desc.size = 0;
4211 hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
4212 &data, NULL, &wined3d_null_parent_ops, &texture);
4213 wined3d_surface_unmap(cursor_image);
4214 if (FAILED(hr))
4216 ERR("Failed to create cursor texture.\n");
4217 return NULL;
4220 return texture;
4223 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4224 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4226 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4227 device, x_hotspot, y_hotspot, cursor_image);
4229 if (device->cursor_texture)
4231 wined3d_texture_decref(device->cursor_texture);
4232 device->cursor_texture = NULL;
4235 if (cursor_image)
4237 struct wined3d_display_mode mode;
4238 struct wined3d_map_desc map_desc;
4239 HRESULT hr;
4241 /* MSDN: Cursor must be A8R8G8B8 */
4242 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4244 WARN("surface %p has an invalid format.\n", cursor_image);
4245 return WINED3DERR_INVALIDCALL;
4248 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4250 ERR("Failed to get display mode, hr %#x.\n", hr);
4251 return WINED3DERR_INVALIDCALL;
4254 /* MSDN: Cursor must be smaller than the display mode */
4255 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4257 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4258 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4259 mode.width, mode.height);
4260 return WINED3DERR_INVALIDCALL;
4263 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4265 /* Do not store the surface's pointer because the application may
4266 * release it after setting the cursor image. Windows doesn't
4267 * addref the set surface, so we can't do this either without
4268 * creating circular refcount dependencies. */
4269 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
4271 ERR("Failed to create cursor texture.\n");
4272 return WINED3DERR_INVALIDCALL;
4275 device->cursorWidth = cursor_image->resource.width;
4276 device->cursorHeight = cursor_image->resource.height;
4278 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4280 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4281 ICONINFO cursorInfo;
4282 DWORD *maskBits;
4283 HCURSOR cursor;
4285 /* 32-bit user32 cursors ignore the alpha channel if it's all
4286 * zeroes, and use the mask instead. Fill the mask with all ones
4287 * to ensure we still get a fully transparent cursor. */
4288 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4289 memset(maskBits, 0xff, mask_size);
4290 wined3d_surface_map(cursor_image, &map_desc, NULL,
4291 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4292 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4294 cursorInfo.fIcon = FALSE;
4295 cursorInfo.xHotspot = x_hotspot;
4296 cursorInfo.yHotspot = y_hotspot;
4297 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4298 1, 1, maskBits);
4299 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4300 1, 32, map_desc.data);
4301 wined3d_surface_unmap(cursor_image);
4302 /* Create our cursor and clean up. */
4303 cursor = CreateIconIndirect(&cursorInfo);
4304 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4305 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4306 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4307 device->hardwareCursor = cursor;
4308 if (device->bCursorVisible) SetCursor( cursor );
4309 HeapFree(GetProcessHeap(), 0, maskBits);
4313 device->xHotSpot = x_hotspot;
4314 device->yHotSpot = y_hotspot;
4315 return WINED3D_OK;
4318 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4319 int x_screen_space, int y_screen_space, DWORD flags)
4321 TRACE("device %p, x %d, y %d, flags %#x.\n",
4322 device, x_screen_space, y_screen_space, flags);
4324 device->xScreenSpace = x_screen_space;
4325 device->yScreenSpace = y_screen_space;
4327 if (device->hardwareCursor)
4329 POINT pt;
4331 GetCursorPos( &pt );
4332 if (x_screen_space == pt.x && y_screen_space == pt.y)
4333 return;
4334 SetCursorPos( x_screen_space, y_screen_space );
4336 /* Switch to the software cursor if position diverges from the hardware one. */
4337 GetCursorPos( &pt );
4338 if (x_screen_space != pt.x || y_screen_space != pt.y)
4340 if (device->bCursorVisible) SetCursor( NULL );
4341 DestroyCursor( device->hardwareCursor );
4342 device->hardwareCursor = 0;
4347 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4349 BOOL oldVisible = device->bCursorVisible;
4351 TRACE("device %p, show %#x.\n", device, show);
4354 * When ShowCursor is first called it should make the cursor appear at the OS's last
4355 * known cursor position.
4357 if (show && !oldVisible)
4359 POINT pt;
4360 GetCursorPos(&pt);
4361 device->xScreenSpace = pt.x;
4362 device->yScreenSpace = pt.y;
4365 if (device->hardwareCursor)
4367 device->bCursorVisible = show;
4368 if (show)
4369 SetCursor(device->hardwareCursor);
4370 else
4371 SetCursor(NULL);
4373 else if (device->cursor_texture)
4375 device->bCursorVisible = show;
4378 return oldVisible;
4381 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4383 struct wined3d_resource *resource, *cursor;
4385 TRACE("device %p.\n", device);
4387 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4389 TRACE("Checking resource %p for eviction.\n", resource);
4391 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4393 TRACE("Evicting %p.\n", resource);
4394 resource->resource_ops->resource_unload(resource);
4398 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4399 device_invalidate_state(device, STATE_STREAMSRC);
4402 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4404 struct wined3d_resource *resource, *cursor;
4405 const struct wined3d_gl_info *gl_info;
4406 struct wined3d_context *context;
4407 struct wined3d_shader *shader;
4409 context = context_acquire(device, NULL);
4410 gl_info = context->gl_info;
4412 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4414 TRACE("Unloading resource %p.\n", resource);
4416 resource->resource_ops->resource_unload(resource);
4419 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4421 device->shader_backend->shader_destroy(shader);
4424 if (device->depth_blt_texture)
4426 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4427 device->depth_blt_texture = 0;
4430 device->blitter->free_private(device);
4431 device->shader_backend->shader_free_private(device);
4432 destroy_dummy_textures(device, gl_info);
4434 context_release(context);
4436 while (device->context_count)
4438 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4441 HeapFree(GetProcessHeap(), 0, swapchain->context);
4442 swapchain->context = NULL;
4445 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4447 struct wined3d_context *context;
4448 struct wined3d_surface *target;
4449 HRESULT hr;
4451 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4452 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4454 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4455 return hr;
4458 if (FAILED(hr = device->blitter->alloc_private(device)))
4460 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4461 device->shader_backend->shader_free_private(device);
4462 return hr;
4465 /* Recreate the primary swapchain's context */
4466 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4467 if (!swapchain->context)
4469 ERR("Failed to allocate memory for swapchain context array.\n");
4470 device->blitter->free_private(device);
4471 device->shader_backend->shader_free_private(device);
4472 return E_OUTOFMEMORY;
4475 target = swapchain->back_buffers
4476 ? surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0))
4477 : surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
4478 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4480 WARN("Failed to create context.\n");
4481 device->blitter->free_private(device);
4482 device->shader_backend->shader_free_private(device);
4483 HeapFree(GetProcessHeap(), 0, swapchain->context);
4484 return E_FAIL;
4487 swapchain->context[0] = context;
4488 swapchain->num_contexts = 1;
4489 create_dummy_textures(device, context);
4490 context_release(context);
4492 return WINED3D_OK;
4495 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4496 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4497 wined3d_device_reset_cb callback, BOOL reset_state)
4499 struct wined3d_resource *resource, *cursor;
4500 struct wined3d_swapchain *swapchain;
4501 struct wined3d_display_mode m;
4502 BOOL DisplayModeChanged;
4503 HRESULT hr = WINED3D_OK;
4504 unsigned int i;
4506 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4508 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4510 ERR("Failed to get the first implicit swapchain.\n");
4511 return WINED3DERR_INVALIDCALL;
4513 DisplayModeChanged = swapchain->reapply_mode;
4515 if (reset_state)
4517 if (device->logo_texture)
4519 wined3d_texture_decref(device->logo_texture);
4520 device->logo_texture = NULL;
4522 if (device->cursor_texture)
4524 wined3d_texture_decref(device->cursor_texture);
4525 device->cursor_texture = NULL;
4527 state_unbind_resources(&device->state);
4530 if (device->fb.render_targets)
4532 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4534 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4537 wined3d_device_set_depth_stencil_view(device, NULL);
4539 if (device->onscreen_depth_stencil)
4541 wined3d_surface_decref(device->onscreen_depth_stencil);
4542 device->onscreen_depth_stencil = NULL;
4545 if (reset_state)
4547 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4549 TRACE("Enumerating resource %p.\n", resource);
4550 if (FAILED(hr = callback(resource)))
4551 return hr;
4555 TRACE("New params:\n");
4556 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4557 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4558 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4559 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4560 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4561 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4562 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4563 TRACE("device_window %p\n", swapchain_desc->device_window);
4564 TRACE("windowed %#x\n", swapchain_desc->windowed);
4565 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4566 if (swapchain_desc->enable_auto_depth_stencil)
4567 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4568 TRACE("flags %#x\n", swapchain_desc->flags);
4569 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4570 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4571 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4573 /* No special treatment of these parameters. Just store them */
4574 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4575 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4576 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4577 swapchain->desc.flags = swapchain_desc->flags;
4578 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4579 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4580 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4582 if (swapchain_desc->device_window
4583 && swapchain_desc->device_window != swapchain->desc.device_window)
4585 TRACE("Changing the device window from %p to %p.\n",
4586 swapchain->desc.device_window, swapchain_desc->device_window);
4587 swapchain->desc.device_window = swapchain_desc->device_window;
4588 swapchain->device_window = swapchain_desc->device_window;
4589 wined3d_swapchain_set_window(swapchain, NULL);
4592 if (mode)
4594 DisplayModeChanged = TRUE;
4595 m = *mode;
4597 else if (swapchain_desc->windowed)
4599 m = swapchain->original_mode;
4601 else
4603 m.width = swapchain_desc->backbuffer_width;
4604 m.height = swapchain_desc->backbuffer_height;
4605 m.refresh_rate = swapchain_desc->refresh_rate;
4606 m.format_id = swapchain_desc->backbuffer_format;
4607 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4609 if ((m.width != swapchain->desc.backbuffer_width
4610 || m.height != swapchain->desc.backbuffer_height))
4611 DisplayModeChanged = TRUE;
4614 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4615 || DisplayModeChanged)
4617 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4619 WARN("Failed to set display mode, hr %#x.\n", hr);
4620 return WINED3DERR_INVALIDCALL;
4623 if (!swapchain_desc->windowed)
4625 if (swapchain->desc.windowed)
4627 HWND focus_window = device->create_parms.focus_window;
4628 if (!focus_window)
4629 focus_window = swapchain_desc->device_window;
4630 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4632 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4633 return hr;
4636 /* switch from windowed to fs */
4637 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4638 swapchain_desc->backbuffer_width,
4639 swapchain_desc->backbuffer_height);
4641 else
4643 /* Fullscreen -> fullscreen mode change */
4644 MoveWindow(swapchain->device_window, 0, 0,
4645 swapchain_desc->backbuffer_width,
4646 swapchain_desc->backbuffer_height,
4647 TRUE);
4649 swapchain->d3d_mode = m;
4651 else if (!swapchain->desc.windowed)
4653 /* Fullscreen -> windowed switch */
4654 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4655 wined3d_device_release_focus_window(device);
4657 swapchain->desc.windowed = swapchain_desc->windowed;
4659 else if (!swapchain_desc->windowed)
4661 DWORD style = device->style;
4662 DWORD exStyle = device->exStyle;
4663 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4664 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4665 * Reset to clear up their mess. Guild Wars also loses the device during that.
4667 device->style = 0;
4668 device->exStyle = 0;
4669 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4670 swapchain_desc->backbuffer_width,
4671 swapchain_desc->backbuffer_height);
4672 device->style = style;
4673 device->exStyle = exStyle;
4676 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4677 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4678 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4679 return hr;
4681 if (device->auto_depth_stencil_view)
4683 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4684 device->auto_depth_stencil_view = NULL;
4686 if (swapchain->desc.enable_auto_depth_stencil)
4688 struct wined3d_resource_desc texture_desc;
4689 struct wined3d_texture *texture;
4690 struct wined3d_rendertarget_view_desc view_desc;
4692 TRACE("Creating the depth stencil buffer\n");
4694 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
4695 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4696 texture_desc.multisample_type = swapchain->desc.multisample_type;
4697 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4698 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4699 texture_desc.pool = WINED3D_POOL_DEFAULT;
4700 texture_desc.width = swapchain->desc.backbuffer_width;
4701 texture_desc.height = swapchain->desc.backbuffer_height;
4702 texture_desc.depth = 1;
4703 texture_desc.size = 0;
4705 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4706 device->device_parent, &texture_desc, &texture)))
4708 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4709 return WINED3DERR_INVALIDCALL;
4712 view_desc.format_id = texture->resource.format->id;
4713 view_desc.u.texture.level_idx = 0;
4714 view_desc.u.texture.layer_idx = 0;
4715 view_desc.u.texture.layer_count = 1;
4716 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4717 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4718 wined3d_texture_decref(texture);
4719 if (FAILED(hr))
4721 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4722 return hr;
4725 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4728 if (device->back_buffer_view)
4730 wined3d_rendertarget_view_decref(device->back_buffer_view);
4731 device->back_buffer_view = NULL;
4733 if (swapchain->desc.backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
4734 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
4735 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4737 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4738 return hr;
4741 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4743 if (reset_state)
4745 TRACE("Resetting stateblock.\n");
4746 if (device->recording)
4748 wined3d_stateblock_decref(device->recording);
4749 device->recording = NULL;
4751 wined3d_cs_emit_reset_state(device->cs);
4752 state_cleanup(&device->state);
4754 if (device->d3d_initialized)
4755 delete_opengl_contexts(device, swapchain);
4757 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4758 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4759 ERR("Failed to initialize device state, hr %#x.\n", hr);
4760 device->update_state = &device->state;
4762 device_init_swapchain_state(device, swapchain);
4764 else if (device->back_buffer_view)
4766 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4767 struct wined3d_state *state = &device->state;
4769 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4771 /* Note the min_z / max_z is not reset. */
4772 state->viewport.x = 0;
4773 state->viewport.y = 0;
4774 state->viewport.width = view->width;
4775 state->viewport.height = view->height;
4776 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4778 state->scissor_rect.top = 0;
4779 state->scissor_rect.left = 0;
4780 state->scissor_rect.right = view->width;
4781 state->scissor_rect.bottom = view->height;
4782 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4785 if (reset_state && device->d3d_initialized)
4786 hr = create_primary_opengl_context(device, swapchain);
4788 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4789 * first use
4791 return hr;
4794 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4796 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4798 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4800 return WINED3D_OK;
4804 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4805 struct wined3d_device_creation_parameters *parameters)
4807 TRACE("device %p, parameters %p.\n", device, parameters);
4809 *parameters = device->create_parms;
4812 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4813 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4815 struct wined3d_swapchain *swapchain;
4817 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4818 device, swapchain_idx, flags, ramp);
4820 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4821 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4824 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4825 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4827 struct wined3d_swapchain *swapchain;
4829 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4830 device, swapchain_idx, ramp);
4832 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4833 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4836 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4838 TRACE("device %p, resource %p.\n", device, resource);
4840 list_add_head(&device->resources, &resource->resource_list_entry);
4843 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4845 TRACE("device %p, resource %p.\n", device, resource);
4847 list_remove(&resource->resource_list_entry);
4850 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4852 enum wined3d_resource_type type = resource->type;
4853 unsigned int i;
4855 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4857 context_resource_released(device, resource, type);
4859 switch (type)
4861 case WINED3D_RTYPE_SURFACE:
4863 struct wined3d_surface *surface = surface_from_resource(resource);
4865 if (!device->d3d_initialized) break;
4867 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4869 if (wined3d_rendertarget_view_get_surface(device->fb.render_targets[i]) == surface)
4871 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4872 device->fb.render_targets[i] = NULL;
4876 if (wined3d_rendertarget_view_get_surface(device->fb.depth_stencil) == surface)
4878 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4879 device->fb.depth_stencil = NULL;
4882 break;
4884 case WINED3D_RTYPE_TEXTURE:
4885 case WINED3D_RTYPE_CUBE_TEXTURE:
4886 case WINED3D_RTYPE_VOLUME_TEXTURE:
4887 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4889 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4891 if (device->state.textures[i] == texture)
4893 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4894 device->state.textures[i] = NULL;
4897 if (device->recording && device->update_state->textures[i] == texture)
4899 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4900 texture, device->recording, i);
4901 device->update_state->textures[i] = NULL;
4904 break;
4906 case WINED3D_RTYPE_BUFFER:
4908 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4910 for (i = 0; i < MAX_STREAMS; ++i)
4912 if (device->state.streams[i].buffer == buffer)
4914 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4915 device->state.streams[i].buffer = NULL;
4918 if (device->recording && device->update_state->streams[i].buffer == buffer)
4920 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4921 buffer, device->recording, i);
4922 device->update_state->streams[i].buffer = NULL;
4926 if (device->state.index_buffer == buffer)
4928 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4929 device->state.index_buffer = NULL;
4932 if (device->recording && device->update_state->index_buffer == buffer)
4934 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4935 buffer, device->recording);
4936 device->update_state->index_buffer = NULL;
4939 break;
4941 default:
4942 break;
4945 /* Remove the resource from the resourceStore */
4946 device_resource_remove(device, resource);
4948 TRACE("Resource released.\n");
4951 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4953 struct wined3d_resource *resource;
4955 TRACE("device %p, dc %p.\n", device, dc);
4957 if (!dc)
4958 return NULL;
4960 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4962 if (resource->type == WINED3D_RTYPE_SURFACE)
4964 struct wined3d_surface *s = surface_from_resource(resource);
4966 if (s->hDC == dc)
4968 TRACE("Found surface %p for dc %p.\n", s, dc);
4969 return s;
4974 return NULL;
4977 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4979 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4981 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4984 static const struct wine_rb_functions wined3d_sampler_rb_functions =
4986 wined3d_rb_alloc,
4987 wined3d_rb_realloc,
4988 wined3d_rb_free,
4989 wined3d_sampler_compare,
4992 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4993 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4994 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4996 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4997 const struct fragment_pipeline *fragment_pipeline;
4998 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4999 unsigned int i;
5000 HRESULT hr;
5002 device->ref = 1;
5003 device->wined3d = wined3d;
5004 wined3d_incref(device->wined3d);
5005 device->adapter = wined3d->adapter_count ? adapter : NULL;
5006 device->device_parent = device_parent;
5007 list_init(&device->resources);
5008 list_init(&device->shaders);
5009 device->surface_alignment = surface_alignment;
5011 /* Save the creation parameters. */
5012 device->create_parms.adapter_idx = adapter_idx;
5013 device->create_parms.device_type = device_type;
5014 device->create_parms.focus_window = focus_window;
5015 device->create_parms.flags = flags;
5017 device->shader_backend = adapter->shader_backend;
5019 vertex_pipeline = adapter->vertex_pipe;
5021 fragment_pipeline = adapter->fragment_pipe;
5023 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
5025 ERR("Failed to initialize sampler rbtree.\n");
5026 return E_OUTOFMEMORY;
5029 if (vertex_pipeline->vp_states && fragment_pipeline->states
5030 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5031 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5032 fragment_pipeline, misc_state_template)))
5034 ERR("Failed to compile state table, hr %#x.\n", hr);
5035 wine_rb_destroy(&device->samplers, NULL, NULL);
5036 wined3d_decref(device->wined3d);
5037 return hr;
5040 device->blitter = adapter->blitter;
5042 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
5043 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
5045 ERR("Failed to initialize device state, hr %#x.\n", hr);
5046 goto err;
5048 device->update_state = &device->state;
5050 if (!(device->cs = wined3d_cs_create(device)))
5052 WARN("Failed to create command stream.\n");
5053 state_cleanup(&device->state);
5054 hr = E_FAIL;
5055 goto err;
5058 return WINED3D_OK;
5060 err:
5061 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5063 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5065 wine_rb_destroy(&device->samplers, NULL, NULL);
5066 wined3d_decref(device->wined3d);
5067 return hr;
5071 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5073 DWORD rep = device->StateTable[state].representative;
5074 struct wined3d_context *context;
5075 DWORD idx;
5076 BYTE shift;
5077 UINT i;
5079 for (i = 0; i < device->context_count; ++i)
5081 context = device->contexts[i];
5082 if(isStateDirty(context, rep)) continue;
5084 context->dirtyArray[context->numDirtyEntries++] = rep;
5085 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5086 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5087 context->isStateDirty[idx] |= (1u << shift);
5091 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5092 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5094 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5096 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5097 window, message, wparam, lparam);
5098 if (unicode)
5099 return DefWindowProcW(window, message, wparam, lparam);
5100 else
5101 return DefWindowProcA(window, message, wparam, lparam);
5104 if (message == WM_DESTROY)
5106 TRACE("unregister window %p.\n", window);
5107 wined3d_unregister_window(window);
5109 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5110 ERR("Window %p is not the focus window for device %p.\n", window, device);
5112 else if (message == WM_DISPLAYCHANGE)
5114 device->device_parent->ops->mode_changed(device->device_parent);
5116 else if (message == WM_ACTIVATEAPP)
5118 UINT i;
5120 for (i = 0; i < device->swapchain_count; i++)
5121 wined3d_swapchain_activate(device->swapchains[i], wparam);
5123 device->device_parent->ops->activate(device->device_parent, wparam);
5125 else if (message == WM_SYSCOMMAND)
5127 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5129 if (unicode)
5130 DefWindowProcW(window, message, wparam, lparam);
5131 else
5132 DefWindowProcA(window, message, wparam, lparam);
5136 if (unicode)
5137 return CallWindowProcW(proc, window, message, wparam, lparam);
5138 else
5139 return CallWindowProcA(proc, window, message, wparam, lparam);