wined3d: Send GL context destruction through the command stream.
[wine.git] / dlls / wined3d / device.c
blob37773315767bfda98dbd9ba01abe08a99c0d1bea
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 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
203 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
204 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
206 /* partial draw rect */
207 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
208 return FALSE;
210 /* partial clear rect */
211 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
212 || clear_rect->right < width || clear_rect->bottom < height))
213 return FALSE;
215 return TRUE;
218 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
219 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
220 float depth, DWORD stencil)
222 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
223 struct wined3d_surface *target = rtv ? wined3d_rendertarget_view_get_surface(rtv) : NULL;
224 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
225 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
226 const struct wined3d_state *state = &device->state;
227 const struct wined3d_gl_info *gl_info;
228 UINT drawable_width, drawable_height;
229 struct wined3d_color corrected_color;
230 struct wined3d_context *context;
231 GLbitfield clear_mask = 0;
232 BOOL render_offscreen;
233 unsigned int i;
235 if (target)
236 context = context_acquire(device, target->container, rtv->sub_resource_idx);
237 else
238 context = context_acquire(device, NULL, 0);
239 if (!context->valid)
241 context_release(context);
242 WARN("Invalid context, skipping clear.\n");
243 return;
245 gl_info = context->gl_info;
247 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
248 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
249 * for the cleared parts, and the untouched parts.
251 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
252 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
253 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
254 * checking all this if the dest surface is in the drawable anyway. */
255 for (i = 0; i < rt_count; ++i)
257 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
259 if (rtv && rtv->format->id != WINED3DFMT_NULL)
261 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
263 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
264 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
265 else
266 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
270 if (target)
272 render_offscreen = context->render_offscreen;
273 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
275 else
277 render_offscreen = TRUE;
278 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
279 depth_stencil->texture_level);
280 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
281 depth_stencil->texture_level);
284 if (depth_stencil && render_offscreen)
285 wined3d_texture_prepare_location(depth_stencil->container,
286 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
288 if (flags & WINED3DCLEAR_ZBUFFER)
290 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
292 wined3d_texture_load_location(depth_stencil->container,
293 dsv->sub_resource_idx, context, location);
296 if (!context_apply_clear_state(context, state, rt_count, fb))
298 context_release(context);
299 WARN("Failed to apply clear state, skipping clear.\n");
300 return;
303 /* Only set the values up once, as they are not changing. */
304 if (flags & WINED3DCLEAR_STENCIL)
306 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
308 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
309 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
311 gl_info->gl_ops.gl.p_glStencilMask(~0U);
312 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
313 gl_info->gl_ops.gl.p_glClearStencil(stencil);
314 checkGLcall("glClearStencil");
315 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
318 if (flags & WINED3DCLEAR_ZBUFFER)
320 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
322 wined3d_texture_validate_location(depth_stencil->container, dsv->sub_resource_idx, location);
323 wined3d_texture_invalidate_location(depth_stencil->container, dsv->sub_resource_idx, ~location);
325 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
326 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
327 gl_info->gl_ops.gl.p_glClearDepth(depth);
328 checkGLcall("glClearDepth");
329 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
332 if (flags & WINED3DCLEAR_TARGET)
334 for (i = 0; i < rt_count; ++i)
336 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
337 struct wined3d_texture *texture;
339 if (!rtv)
340 continue;
342 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
344 FIXME("Not supported on buffer resources.\n");
345 continue;
348 texture = texture_from_resource(rtv->resource);
349 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
350 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
353 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
355 if (rt_count > 1)
356 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
357 "support, this might cause graphical issues.\n");
359 corrected_color.r = color->r < wined3d_srgb_const1[0]
360 ? color->r * wined3d_srgb_const0[3]
361 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
362 - wined3d_srgb_const0[2];
363 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
364 corrected_color.g = color->g < wined3d_srgb_const1[0]
365 ? color->g * wined3d_srgb_const0[3]
366 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
367 - wined3d_srgb_const0[2];
368 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
369 corrected_color.b = color->b < wined3d_srgb_const1[0]
370 ? color->b * wined3d_srgb_const0[3]
371 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
372 - wined3d_srgb_const0[2];
373 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
374 color = &corrected_color;
377 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
378 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
379 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
380 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
382 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
383 checkGLcall("glClearColor");
384 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
387 if (!rect_count)
389 if (render_offscreen)
391 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
392 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
394 else
396 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
397 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
399 checkGLcall("glScissor");
400 gl_info->gl_ops.gl.p_glClear(clear_mask);
401 checkGLcall("glClear");
403 else
405 RECT current_rect;
407 /* Now process each rect in turn. */
408 for (i = 0; i < rect_count; ++i)
410 /* Note that GL uses lower left, width/height. */
411 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
413 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
414 wine_dbgstr_rect(&clear_rect[i]),
415 wine_dbgstr_rect(&current_rect));
417 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
418 * The rectangle is not cleared, no error is returned, but further rectangles are
419 * still cleared if they are valid. */
420 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
422 TRACE("Rectangle with negative dimensions, ignoring.\n");
423 continue;
426 if (render_offscreen)
428 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
429 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
431 else
433 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
434 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
436 checkGLcall("glScissor");
438 gl_info->gl_ops.gl.p_glClear(clear_mask);
439 checkGLcall("glClear");
443 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
444 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
445 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
447 context_release(context);
450 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
452 ULONG refcount = InterlockedIncrement(&device->ref);
454 TRACE("%p increasing refcount to %u.\n", device, refcount);
456 return refcount;
459 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
461 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
463 ERR("Leftover sampler %p.\n", sampler);
466 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
468 ULONG refcount = InterlockedDecrement(&device->ref);
470 TRACE("%p decreasing refcount to %u.\n", device, refcount);
472 if (!refcount)
474 UINT i;
476 wined3d_cs_destroy(device->cs);
478 if (device->recording && wined3d_stateblock_decref(device->recording))
479 ERR("Something's still holding the recording stateblock.\n");
480 device->recording = NULL;
482 state_cleanup(&device->state);
484 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
486 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
487 device->multistate_funcs[i] = NULL;
490 if (!list_empty(&device->resources))
492 struct wined3d_resource *resource;
494 ERR("Device released with resources still bound.\n");
496 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
498 ERR("Leftover resource %p with type %s (%#x).\n",
499 resource, debug_d3dresourcetype(resource->type), resource->type);
503 if (device->contexts)
504 ERR("Context array not freed!\n");
505 if (device->hardwareCursor)
506 DestroyCursor(device->hardwareCursor);
507 device->hardwareCursor = 0;
509 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
511 wined3d_decref(device->wined3d);
512 device->wined3d = NULL;
513 HeapFree(GetProcessHeap(), 0, device);
514 TRACE("Freed device %p.\n", device);
517 return refcount;
520 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
522 TRACE("device %p.\n", device);
524 return device->swapchain_count;
527 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
529 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
531 if (swapchain_idx >= device->swapchain_count)
533 WARN("swapchain_idx %u >= swapchain_count %u.\n",
534 swapchain_idx, device->swapchain_count);
535 return NULL;
538 return device->swapchains[swapchain_idx];
541 static void device_load_logo(struct wined3d_device *device, const char *filename)
543 struct wined3d_color_key color_key;
544 struct wined3d_resource_desc desc;
545 HBITMAP hbm;
546 BITMAP bm;
547 HRESULT hr;
548 HDC dcb = NULL, dcs = NULL;
550 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
551 if(hbm)
553 GetObjectA(hbm, sizeof(BITMAP), &bm);
554 dcb = CreateCompatibleDC(NULL);
555 if(!dcb) goto out;
556 SelectObject(dcb, hbm);
558 else
560 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
561 * couldn't be loaded
563 memset(&bm, 0, sizeof(bm));
564 bm.bmWidth = 32;
565 bm.bmHeight = 32;
568 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
569 desc.format = WINED3DFMT_B5G6R5_UNORM;
570 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
571 desc.multisample_quality = 0;
572 desc.usage = WINED3DUSAGE_DYNAMIC;
573 desc.pool = WINED3D_POOL_DEFAULT;
574 desc.width = bm.bmWidth;
575 desc.height = bm.bmHeight;
576 desc.depth = 1;
577 desc.size = 0;
578 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
579 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
581 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
582 goto out;
585 if (dcb)
587 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
588 goto out;
589 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
590 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
592 color_key.color_space_low_value = 0;
593 color_key.color_space_high_value = 0;
594 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
596 else
598 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
599 const RECT rect = {0, 0, desc.width, desc.height};
600 struct wined3d_surface *surface;
602 /* Fill the surface with a white color to show that wined3d is there */
603 surface = device->logo_texture->sub_resources[0].u.surface;
604 surface_color_fill(surface, &rect, &c);
607 out:
608 if (dcb) DeleteDC(dcb);
609 if (hbm) DeleteObject(hbm);
612 /* Context activation is done by the caller. */
613 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
615 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
616 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
617 unsigned int i;
618 DWORD color;
620 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
621 color = 0x000000ff;
622 else
623 color = 0x00000000;
625 /* Under DirectX you can sample even if no texture is bound, whereas
626 * OpenGL will only allow that when a valid texture is bound.
627 * We emulate this by creating dummy textures and binding them
628 * to each texture stage when the currently set D3D texture is NULL. */
629 context_active_texture(context, gl_info, 0);
631 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d);
632 checkGLcall("glGenTextures");
633 TRACE("Dummy 2D texture given name %u.\n", device->dummy_textures.tex_2d);
635 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
636 checkGLcall("glBindTexture");
638 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
639 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
640 checkGLcall("glTexImage2D");
642 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
644 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_rect);
645 checkGLcall("glGenTextures");
646 TRACE("Dummy rectangle texture given name %u.\n", device->dummy_textures.tex_rect);
648 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
649 checkGLcall("glBindTexture");
651 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
652 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
653 checkGLcall("glTexImage2D");
656 if (gl_info->supported[EXT_TEXTURE3D])
658 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_3d);
659 checkGLcall("glGenTextures");
660 TRACE("Dummy 3D texture given name %u.\n", device->dummy_textures.tex_3d);
662 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
663 checkGLcall("glBindTexture");
665 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
666 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
667 checkGLcall("glTexImage3D");
670 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
672 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube);
673 checkGLcall("glGenTextures");
674 TRACE("Dummy cube texture given name %u.\n", device->dummy_textures.tex_cube);
676 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
677 checkGLcall("glBindTexture");
679 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
681 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
682 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
683 checkGLcall("glTexImage2D");
687 if (gl_info->supported[EXT_TEXTURE_ARRAY])
689 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d_array);
690 checkGLcall("glGenTextures");
691 TRACE("Dummy 2D array texture given name %u.\n", device->dummy_textures.tex_2d_array);
693 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
694 checkGLcall("glBindTexture");
696 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
697 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
698 checkGLcall("glTexImage3D");
701 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
703 GLuint buffer;
705 GL_EXTCALL(glGenBuffers(1, &buffer));
706 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
707 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
708 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
709 checkGLcall("Create buffer object");
711 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_buffer);
712 checkGLcall("glGenTextures");
713 TRACE("Dummy buffer texture given name %u.\n", device->dummy_textures.tex_buffer);
715 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, device->dummy_textures.tex_buffer);
716 checkGLcall("glBindTexture");
717 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
718 checkGLcall("glTexBuffer");
720 GL_EXTCALL(glDeleteBuffers(1, &buffer));
721 checkGLcall("glDeleteBuffers");
724 context_bind_dummy_textures(device, context);
727 /* Context activation is done by the caller. */
728 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
730 const struct wined3d_gl_info *gl_info = context->gl_info;
732 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
733 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_buffer);
735 if (gl_info->supported[EXT_TEXTURE_ARRAY])
736 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d_array);
738 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
739 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube);
741 if (gl_info->supported[EXT_TEXTURE3D])
742 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_3d);
744 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
745 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_rect);
747 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d);
749 checkGLcall("Delete dummy textures");
751 memset(&device->dummy_textures, 0, sizeof(device->dummy_textures));
754 /* Context activation is done by the caller. */
755 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
757 const struct wined3d_gl_info *gl_info = context->gl_info;
759 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
761 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
762 * instructions allow access to resources without using samplers.
763 * In GLSL, resources are always accessed through sampler or image variables. The default
764 * sampler object is used to emulate the direct resource access when there is no sampler state
765 * to use.
767 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
768 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
769 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
770 checkGLcall("Create default sampler");
772 /* In D3D10+, a NULL sampler maps to the default sampler state. */
773 GL_EXTCALL(glGenSamplers(1, &device->null_sampler));
774 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
775 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
776 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
777 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
778 checkGLcall("Create null sampler");
780 else
782 device->default_sampler = 0;
783 device->null_sampler = 0;
787 /* Context activation is done by the caller. */
788 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
790 const struct wined3d_gl_info *gl_info = context->gl_info;
792 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
794 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
795 GL_EXTCALL(glDeleteSamplers(1, &device->null_sampler));
796 checkGLcall("glDeleteSamplers");
799 device->default_sampler = 0;
800 device->null_sampler = 0;
803 static LONG fullscreen_style(LONG style)
805 /* Make sure the window is managed, otherwise we won't get keyboard input. */
806 style |= WS_POPUP | WS_SYSMENU;
807 style &= ~(WS_CAPTION | WS_THICKFRAME);
809 return style;
812 static LONG fullscreen_exstyle(LONG exstyle)
814 /* Filter out window decorations. */
815 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
817 return exstyle;
820 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
822 BOOL filter_messages;
823 LONG style, exstyle;
825 TRACE("Setting up window %p for fullscreen mode.\n", window);
827 if (device->style || device->exStyle)
829 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
830 window, device->style, device->exStyle);
833 device->style = GetWindowLongW(window, GWL_STYLE);
834 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
836 style = fullscreen_style(device->style);
837 exstyle = fullscreen_exstyle(device->exStyle);
839 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
840 device->style, device->exStyle, style, exstyle);
842 filter_messages = device->filter_messages;
843 device->filter_messages = TRUE;
845 SetWindowLongW(window, GWL_STYLE, style);
846 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
847 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
849 device->filter_messages = filter_messages;
852 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
853 const RECT *window_rect)
855 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
856 BOOL filter_messages;
857 LONG style, exstyle;
858 RECT rect = {0};
860 if (!device->style && !device->exStyle)
861 return;
863 style = GetWindowLongW(window, GWL_STYLE);
864 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
866 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
867 * application, and we want to ignore them in the test below, since it's
868 * not the application's fault that they changed. Additionally, we want to
869 * preserve the current status of these flags (i.e. don't restore them) to
870 * more closely emulate the behavior of Direct3D, which leaves these flags
871 * alone when returning to windowed mode. */
872 device->style ^= (device->style ^ style) & WS_VISIBLE;
873 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
875 TRACE("Restoring window style of window %p to %08x, %08x.\n",
876 window, device->style, device->exStyle);
878 filter_messages = device->filter_messages;
879 device->filter_messages = TRUE;
881 /* Only restore the style if the application didn't modify it during the
882 * fullscreen phase. Some applications change it before calling Reset()
883 * when switching between windowed and fullscreen modes (HL2), some
884 * depend on the original style (Eve Online). */
885 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
887 SetWindowLongW(window, GWL_STYLE, device->style);
888 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
891 if (window_rect)
892 rect = *window_rect;
893 else
894 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
895 SetWindowPos(window, 0, rect.left, rect.top,
896 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
898 device->filter_messages = filter_messages;
900 /* Delete the old values. */
901 device->style = 0;
902 device->exStyle = 0;
905 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
907 TRACE("device %p, window %p.\n", device, window);
909 if (!wined3d_register_window(window, device))
911 ERR("Failed to register window %p.\n", window);
912 return E_FAIL;
915 InterlockedExchangePointer((void **)&device->focus_window, window);
916 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
918 return WINED3D_OK;
921 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
923 TRACE("device %p.\n", device);
925 if (device->focus_window) wined3d_unregister_window(device->focus_window);
926 InterlockedExchangePointer((void **)&device->focus_window, NULL);
929 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
931 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
932 unsigned int i;
934 if (device->fb.render_targets)
936 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
938 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
940 if (device->back_buffer_view)
941 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
944 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
945 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
948 static void wined3d_device_delete_opengl_contexts_cs(void *object)
950 struct wined3d_resource *resource, *cursor;
951 struct wined3d_device *device = object;
952 struct wined3d_context *context;
953 struct wined3d_shader *shader;
955 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
957 TRACE("Unloading resource %p.\n", resource);
958 wined3d_cs_emit_unload_resource(device->cs, resource);
961 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
963 device->shader_backend->shader_destroy(shader);
966 context = context_acquire(device, NULL, 0);
967 device->blitter->free_private(device);
968 device->shader_backend->shader_free_private(device);
969 destroy_dummy_textures(device, context);
970 destroy_default_samplers(device, context);
971 context_release(context);
973 while (device->context_count)
975 if (device->contexts[0]->swapchain)
976 swapchain_destroy_contexts(device->contexts[0]->swapchain);
977 else
978 context_destroy(device, device->contexts[0]);
982 static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
984 wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
987 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
989 struct wined3d_context *context;
990 struct wined3d_texture *target;
991 HRESULT hr;
993 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
994 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
996 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
997 return hr;
1000 if (FAILED(hr = device->blitter->alloc_private(device)))
1002 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
1003 device->shader_backend->shader_free_private(device);
1004 return hr;
1007 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1008 context = context_acquire(device, target, 0);
1009 create_dummy_textures(device, context);
1010 create_default_samplers(device, context);
1011 context_release(context);
1013 return WINED3D_OK;
1016 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1017 struct wined3d_swapchain_desc *swapchain_desc)
1019 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1020 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1021 struct wined3d_swapchain *swapchain = NULL;
1022 DWORD clear_flags = 0;
1023 HRESULT hr;
1025 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1027 if (device->d3d_initialized)
1028 return WINED3DERR_INVALIDCALL;
1029 if (device->wined3d->flags & WINED3D_NO3D)
1030 return WINED3DERR_INVALIDCALL;
1032 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
1033 return E_OUTOFMEMORY;
1035 /* Setup the implicit swapchain. This also initializes a context. */
1036 TRACE("Creating implicit swapchain\n");
1037 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1038 swapchain_desc, &swapchain);
1039 if (FAILED(hr))
1041 WARN("Failed to create implicit swapchain\n");
1042 goto err_out;
1045 if (swapchain_desc->backbuffer_count)
1047 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1048 struct wined3d_view_desc view_desc;
1050 view_desc.format_id = back_buffer->format->id;
1051 view_desc.flags = 0;
1052 view_desc.u.texture.level_idx = 0;
1053 view_desc.u.texture.level_count = 1;
1054 view_desc.u.texture.layer_idx = 0;
1055 view_desc.u.texture.layer_count = 1;
1056 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1057 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1059 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1060 goto err_out;
1064 device->swapchain_count = 1;
1065 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1067 ERR("Out of memory!\n");
1068 goto err_out;
1070 device->swapchains[0] = swapchain;
1072 if (FAILED(hr = create_primary_opengl_context(device, swapchain)))
1073 goto err_out;
1074 device_init_swapchain_state(device, swapchain);
1076 device->contexts[0]->last_was_rhw = 0;
1078 TRACE("All defaults now set up, leaving 3D init.\n");
1080 /* Clear the screen */
1081 if (swapchain->back_buffers && swapchain->back_buffers[0])
1082 clear_flags |= WINED3DCLEAR_TARGET;
1083 if (swapchain_desc->enable_auto_depth_stencil)
1084 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1085 if (clear_flags)
1086 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1088 device->d3d_initialized = TRUE;
1090 if (wined3d_settings.logo)
1091 device_load_logo(device, wined3d_settings.logo);
1092 return WINED3D_OK;
1094 err_out:
1095 HeapFree(GetProcessHeap(), 0, device->swapchains);
1096 device->swapchain_count = 0;
1097 if (device->back_buffer_view)
1098 wined3d_rendertarget_view_decref(device->back_buffer_view);
1099 if (swapchain)
1100 wined3d_swapchain_decref(swapchain);
1101 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1103 return hr;
1106 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1107 struct wined3d_swapchain_desc *swapchain_desc)
1109 struct wined3d_swapchain *swapchain = NULL;
1110 HRESULT hr;
1112 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1114 /* Setup the implicit swapchain */
1115 TRACE("Creating implicit swapchain\n");
1116 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1117 swapchain_desc, &swapchain);
1118 if (FAILED(hr))
1120 WARN("Failed to create implicit swapchain\n");
1121 goto err_out;
1124 device->swapchain_count = 1;
1125 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1127 ERR("Out of memory!\n");
1128 goto err_out;
1130 device->swapchains[0] = swapchain;
1131 return WINED3D_OK;
1133 err_out:
1134 wined3d_swapchain_decref(swapchain);
1135 return hr;
1138 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1140 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1142 wined3d_sampler_decref(sampler);
1145 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1147 UINT i;
1149 TRACE("device %p.\n", device);
1151 if (!device->d3d_initialized)
1152 return WINED3DERR_INVALIDCALL;
1154 if (device->logo_texture)
1155 wined3d_texture_decref(device->logo_texture);
1156 if (device->cursor_texture)
1157 wined3d_texture_decref(device->cursor_texture);
1159 state_unbind_resources(&device->state);
1161 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1163 wined3d_device_delete_opengl_contexts(device);
1165 if (device->fb.depth_stencil)
1167 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1169 TRACE("Releasing depth/stencil view %p.\n", view);
1171 device->fb.depth_stencil = NULL;
1172 wined3d_rendertarget_view_decref(view);
1175 if (device->auto_depth_stencil_view)
1177 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1179 device->auto_depth_stencil_view = NULL;
1180 if (wined3d_rendertarget_view_decref(view))
1181 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1184 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
1186 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1188 if (device->back_buffer_view)
1190 wined3d_rendertarget_view_decref(device->back_buffer_view);
1191 device->back_buffer_view = NULL;
1194 for (i = 0; i < device->swapchain_count; ++i)
1196 TRACE("Releasing the implicit swapchain %u.\n", i);
1197 if (wined3d_swapchain_decref(device->swapchains[i]))
1198 FIXME("Something's still holding the implicit swapchain.\n");
1201 HeapFree(GetProcessHeap(), 0, device->swapchains);
1202 device->swapchains = NULL;
1203 device->swapchain_count = 0;
1205 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1206 device->fb.render_targets = NULL;
1208 device->d3d_initialized = FALSE;
1210 return WINED3D_OK;
1213 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1215 unsigned int i;
1217 for (i = 0; i < device->swapchain_count; ++i)
1219 TRACE("Releasing the implicit swapchain %u.\n", i);
1220 if (wined3d_swapchain_decref(device->swapchains[i]))
1221 FIXME("Something's still holding the implicit swapchain.\n");
1224 HeapFree(GetProcessHeap(), 0, device->swapchains);
1225 device->swapchains = NULL;
1226 device->swapchain_count = 0;
1227 return WINED3D_OK;
1230 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1231 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1232 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1234 * There is no way to deactivate thread safety once it is enabled.
1236 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1238 TRACE("device %p.\n", device);
1240 /* For now just store the flag (needed in case of ddraw). */
1241 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1244 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1246 TRACE("device %p.\n", device);
1248 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1249 wine_dbgstr_longlong(device->adapter->vram_bytes),
1250 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1251 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1253 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1256 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1257 struct wined3d_buffer *buffer, UINT offset)
1259 struct wined3d_stream_output *stream;
1260 struct wined3d_buffer *prev_buffer;
1262 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1264 if (idx >= MAX_STREAM_OUT)
1266 WARN("Invalid stream output %u.\n", idx);
1267 return;
1270 stream = &device->update_state->stream_output[idx];
1271 prev_buffer = stream->buffer;
1273 if (buffer)
1274 wined3d_buffer_incref(buffer);
1275 stream->buffer = buffer;
1276 stream->offset = offset;
1277 if (!device->recording)
1278 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1279 if (prev_buffer)
1280 wined3d_buffer_decref(prev_buffer);
1283 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1284 UINT idx, UINT *offset)
1286 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1288 if (idx >= MAX_STREAM_OUT)
1290 WARN("Invalid stream output %u.\n", idx);
1291 return NULL;
1294 if (offset)
1295 *offset = device->state.stream_output[idx].offset;
1296 return device->state.stream_output[idx].buffer;
1299 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1300 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1302 struct wined3d_stream_state *stream;
1303 struct wined3d_buffer *prev_buffer;
1305 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1306 device, stream_idx, buffer, offset, stride);
1308 if (stream_idx >= MAX_STREAMS)
1310 WARN("Stream index %u out of range.\n", stream_idx);
1311 return WINED3DERR_INVALIDCALL;
1313 else if (offset & 0x3)
1315 WARN("Offset %u is not 4 byte aligned.\n", offset);
1316 return WINED3DERR_INVALIDCALL;
1319 stream = &device->update_state->streams[stream_idx];
1320 prev_buffer = stream->buffer;
1322 if (device->recording)
1323 device->recording->changed.streamSource |= 1u << stream_idx;
1325 if (prev_buffer == buffer
1326 && stream->stride == stride
1327 && stream->offset == offset)
1329 TRACE("Application is setting the old values over, nothing to do.\n");
1330 return WINED3D_OK;
1333 stream->buffer = buffer;
1334 if (buffer)
1336 stream->stride = stride;
1337 stream->offset = offset;
1338 wined3d_buffer_incref(buffer);
1341 if (!device->recording)
1342 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1343 if (prev_buffer)
1344 wined3d_buffer_decref(prev_buffer);
1346 return WINED3D_OK;
1349 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1350 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1352 const struct wined3d_stream_state *stream;
1354 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1355 device, stream_idx, buffer, offset, stride);
1357 if (stream_idx >= MAX_STREAMS)
1359 WARN("Stream index %u out of range.\n", stream_idx);
1360 return WINED3DERR_INVALIDCALL;
1363 stream = &device->state.streams[stream_idx];
1364 *buffer = stream->buffer;
1365 if (offset)
1366 *offset = stream->offset;
1367 *stride = stream->stride;
1369 return WINED3D_OK;
1372 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1374 struct wined3d_stream_state *stream;
1375 UINT old_flags, old_freq;
1377 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1379 /* Verify input. At least in d3d9 this is invalid. */
1380 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1382 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1383 return WINED3DERR_INVALIDCALL;
1385 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1387 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1388 return WINED3DERR_INVALIDCALL;
1390 if (!divider)
1392 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1393 return WINED3DERR_INVALIDCALL;
1396 stream = &device->update_state->streams[stream_idx];
1397 old_flags = stream->flags;
1398 old_freq = stream->frequency;
1400 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1401 stream->frequency = divider & 0x7fffff;
1403 if (device->recording)
1404 device->recording->changed.streamFreq |= 1u << stream_idx;
1405 else if (stream->frequency != old_freq || stream->flags != old_flags)
1406 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1408 return WINED3D_OK;
1411 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1412 UINT stream_idx, UINT *divider)
1414 const struct wined3d_stream_state *stream;
1416 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1418 stream = &device->state.streams[stream_idx];
1419 *divider = stream->flags | stream->frequency;
1421 TRACE("Returning %#x.\n", *divider);
1423 return WINED3D_OK;
1426 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1427 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1429 TRACE("device %p, state %s, matrix %p.\n",
1430 device, debug_d3dtstype(d3dts), matrix);
1431 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1432 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1433 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1434 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1436 /* Handle recording of state blocks. */
1437 if (device->recording)
1439 TRACE("Recording... not performing anything.\n");
1440 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1441 device->update_state->transforms[d3dts] = *matrix;
1442 return;
1445 /* If the new matrix is the same as the current one,
1446 * we cut off any further processing. this seems to be a reasonable
1447 * optimization because as was noticed, some apps (warcraft3 for example)
1448 * tend towards setting the same matrix repeatedly for some reason.
1450 * From here on we assume that the new matrix is different, wherever it matters. */
1451 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1453 TRACE("The application is setting the same matrix over again.\n");
1454 return;
1457 device->state.transforms[d3dts] = *matrix;
1458 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1461 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1462 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1464 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1466 *matrix = device->state.transforms[state];
1469 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1470 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1472 const struct wined3d_matrix *mat;
1473 struct wined3d_matrix temp;
1475 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1477 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1478 * below means it will be recorded in a state block change, but it
1479 * works regardless where it is recorded.
1480 * If this is found to be wrong, change to StateBlock. */
1481 if (state > HIGHEST_TRANSFORMSTATE)
1483 WARN("Unhandled transform state %#x.\n", state);
1484 return;
1487 mat = &device->update_state->transforms[state];
1488 multiply_matrix(&temp, mat, matrix);
1490 /* Apply change via set transform - will reapply to eg. lights this way. */
1491 wined3d_device_set_transform(device, state, &temp);
1494 /* Note lights are real special cases. Although the device caps state only
1495 * e.g. 8 are supported, you can reference any indexes you want as long as
1496 * that number max are enabled at any one point in time. Therefore since the
1497 * indices can be anything, we need a hashmap of them. However, this causes
1498 * stateblock problems. When capturing the state block, I duplicate the
1499 * hashmap, but when recording, just build a chain pretty much of commands to
1500 * be replayed. */
1501 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1502 UINT light_idx, const struct wined3d_light *light)
1504 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1505 struct wined3d_light_info *object = NULL;
1506 struct list *e;
1507 float rho;
1509 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1511 /* Check the parameter range. Need for speed most wanted sets junk lights
1512 * which confuse the GL driver. */
1513 if (!light)
1514 return WINED3DERR_INVALIDCALL;
1516 switch (light->type)
1518 case WINED3D_LIGHT_POINT:
1519 case WINED3D_LIGHT_SPOT:
1520 case WINED3D_LIGHT_GLSPOT:
1521 /* Incorrect attenuation values can cause the gl driver to crash.
1522 * Happens with Need for speed most wanted. */
1523 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1525 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1526 return WINED3DERR_INVALIDCALL;
1528 break;
1530 case WINED3D_LIGHT_DIRECTIONAL:
1531 case WINED3D_LIGHT_PARALLELPOINT:
1532 /* Ignores attenuation */
1533 break;
1535 default:
1536 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1537 return WINED3DERR_INVALIDCALL;
1540 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1542 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1543 if (object->OriginalIndex == light_idx)
1544 break;
1545 object = NULL;
1548 if (!object)
1550 TRACE("Adding new light\n");
1551 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1552 if (!object)
1553 return E_OUTOFMEMORY;
1555 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1556 object->glIndex = -1;
1557 object->OriginalIndex = light_idx;
1560 /* Initialize the object. */
1561 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1562 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1563 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1564 light_idx, light->type, debug_color(&light->diffuse),
1565 debug_color(&light->specular), debug_color(&light->ambient),
1566 light->position.x, light->position.y, light->position.z,
1567 light->direction.x, light->direction.y, light->direction.z,
1568 light->range, light->falloff, light->theta, light->phi);
1570 /* Update the live definitions if the light is currently assigned a glIndex. */
1571 if (object->glIndex != -1 && !device->recording)
1573 if (object->OriginalParms.type != light->type)
1574 device_invalidate_state(device, STATE_LIGHT_TYPE);
1575 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1578 /* Save away the information. */
1579 object->OriginalParms = *light;
1581 switch (light->type)
1583 case WINED3D_LIGHT_POINT:
1584 /* Position */
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 object->cutoff = 180.0f;
1590 /* FIXME: Range */
1591 break;
1593 case WINED3D_LIGHT_DIRECTIONAL:
1594 /* Direction */
1595 object->direction.x = -light->direction.x;
1596 object->direction.y = -light->direction.y;
1597 object->direction.z = -light->direction.z;
1598 object->direction.w = 0.0f;
1599 object->exponent = 0.0f;
1600 object->cutoff = 180.0f;
1601 break;
1603 case WINED3D_LIGHT_SPOT:
1604 /* Position */
1605 object->position.x = light->position.x;
1606 object->position.y = light->position.y;
1607 object->position.z = light->position.z;
1608 object->position.w = 1.0f;
1610 /* Direction */
1611 object->direction.x = light->direction.x;
1612 object->direction.y = light->direction.y;
1613 object->direction.z = light->direction.z;
1614 object->direction.w = 0.0f;
1616 /* opengl-ish and d3d-ish spot lights use too different models
1617 * for the light "intensity" as a function of the angle towards
1618 * the main light direction, so we only can approximate very
1619 * roughly. However, spot lights are rather rarely used in games
1620 * (if ever used at all). Furthermore if still used, probably
1621 * nobody pays attention to such details. */
1622 if (!light->falloff)
1624 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1625 * equations have the falloff resp. exponent parameter as an
1626 * exponent, so the spot light lighting will always be 1.0 for
1627 * both of them, and we don't have to care for the rest of the
1628 * rather complex calculation. */
1629 object->exponent = 0.0f;
1631 else
1633 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1634 if (rho < 0.0001f)
1635 rho = 0.0001f;
1636 object->exponent = -0.3f / logf(cosf(rho / 2));
1639 if (object->exponent > 128.0f)
1640 object->exponent = 128.0f;
1642 object->cutoff = (float)(light->phi * 90 / M_PI);
1643 /* FIXME: Range */
1644 break;
1646 case WINED3D_LIGHT_PARALLELPOINT:
1647 object->position.x = light->position.x;
1648 object->position.y = light->position.y;
1649 object->position.z = light->position.z;
1650 object->position.w = 1.0f;
1651 break;
1653 default:
1654 FIXME("Unrecognized light type %#x.\n", light->type);
1657 return WINED3D_OK;
1660 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1661 UINT light_idx, struct wined3d_light *light)
1663 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1664 struct wined3d_light_info *light_info = NULL;
1665 struct list *e;
1667 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1669 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1671 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1672 if (light_info->OriginalIndex == light_idx)
1673 break;
1674 light_info = NULL;
1677 if (!light_info)
1679 TRACE("Light information requested but light not defined\n");
1680 return WINED3DERR_INVALIDCALL;
1683 *light = light_info->OriginalParms;
1684 return WINED3D_OK;
1687 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1689 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1690 struct wined3d_light_info *light_info = NULL;
1691 struct list *e;
1693 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1695 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1697 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1698 if (light_info->OriginalIndex == light_idx)
1699 break;
1700 light_info = NULL;
1702 TRACE("Found light %p.\n", light_info);
1704 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1705 if (!light_info)
1707 TRACE("Light enabled requested but light not defined, so defining one!\n");
1708 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1710 /* Search for it again! Should be fairly quick as near head of list. */
1711 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1713 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1714 if (light_info->OriginalIndex == light_idx)
1715 break;
1716 light_info = NULL;
1718 if (!light_info)
1720 FIXME("Adding default lights has failed dismally\n");
1721 return WINED3DERR_INVALIDCALL;
1725 if (!enable)
1727 if (light_info->glIndex != -1)
1729 if (!device->recording)
1731 device_invalidate_state(device, STATE_LIGHT_TYPE);
1732 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1735 device->update_state->lights[light_info->glIndex] = NULL;
1736 light_info->glIndex = -1;
1738 else
1740 TRACE("Light already disabled, nothing to do\n");
1742 light_info->enabled = FALSE;
1744 else
1746 light_info->enabled = TRUE;
1747 if (light_info->glIndex != -1)
1749 TRACE("Nothing to do as light was enabled\n");
1751 else
1753 unsigned int light_count = device->adapter->d3d_info.limits.active_light_count;
1754 unsigned int i;
1756 /* Find a free light. */
1757 for (i = 0; i < light_count; ++i)
1759 if (!device->update_state->lights[i])
1761 device->update_state->lights[i] = light_info;
1762 light_info->glIndex = i;
1763 break;
1766 if (light_info->glIndex == -1)
1768 /* Our tests show that Windows returns D3D_OK in this situation, even with
1769 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1770 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1771 * as well for those lights.
1773 * TODO: Test how this affects rendering. */
1774 WARN("Too many concurrently active lights\n");
1775 return WINED3D_OK;
1778 /* i == light_info->glIndex */
1779 if (!device->recording)
1781 device_invalidate_state(device, STATE_LIGHT_TYPE);
1782 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1787 return WINED3D_OK;
1790 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1792 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1793 struct wined3d_light_info *light_info = NULL;
1794 struct list *e;
1796 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1798 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1800 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1801 if (light_info->OriginalIndex == light_idx)
1802 break;
1803 light_info = NULL;
1806 if (!light_info)
1808 TRACE("Light enabled state requested but light not defined.\n");
1809 return WINED3DERR_INVALIDCALL;
1811 /* true is 128 according to SetLightEnable */
1812 *enable = light_info->enabled ? 128 : 0;
1813 return WINED3D_OK;
1816 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1817 UINT plane_idx, const struct wined3d_vec4 *plane)
1819 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1821 /* Validate plane_idx. */
1822 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1824 TRACE("Application has requested clipplane this device doesn't support.\n");
1825 return WINED3DERR_INVALIDCALL;
1828 if (device->recording)
1829 device->recording->changed.clipplane |= 1u << plane_idx;
1831 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1833 TRACE("Application is setting old values over, nothing to do.\n");
1834 return WINED3D_OK;
1837 device->update_state->clip_planes[plane_idx] = *plane;
1839 if (!device->recording)
1840 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1842 return WINED3D_OK;
1845 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1846 UINT plane_idx, struct wined3d_vec4 *plane)
1848 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1850 /* Validate plane_idx. */
1851 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1853 TRACE("Application has requested clipplane this device doesn't support.\n");
1854 return WINED3DERR_INVALIDCALL;
1857 *plane = device->state.clip_planes[plane_idx];
1859 return WINED3D_OK;
1862 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1863 const struct wined3d_clip_status *clip_status)
1865 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1867 if (!clip_status)
1868 return WINED3DERR_INVALIDCALL;
1870 return WINED3D_OK;
1873 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1874 struct wined3d_clip_status *clip_status)
1876 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1878 if (!clip_status)
1879 return WINED3DERR_INVALIDCALL;
1881 return WINED3D_OK;
1884 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1886 TRACE("device %p, material %p.\n", device, material);
1888 device->update_state->material = *material;
1890 if (device->recording)
1891 device->recording->changed.material = TRUE;
1892 else
1893 wined3d_cs_emit_set_material(device->cs, material);
1896 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1898 TRACE("device %p, material %p.\n", device, material);
1900 *material = device->state.material;
1902 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1903 TRACE("ambient %s\n", debug_color(&material->ambient));
1904 TRACE("specular %s\n", debug_color(&material->specular));
1905 TRACE("emissive %s\n", debug_color(&material->emissive));
1906 TRACE("power %.8e.\n", material->power);
1909 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1910 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1912 enum wined3d_format_id prev_format;
1913 struct wined3d_buffer *prev_buffer;
1914 unsigned int prev_offset;
1916 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1917 device, buffer, debug_d3dformat(format_id), offset);
1919 prev_buffer = device->update_state->index_buffer;
1920 prev_format = device->update_state->index_format;
1921 prev_offset = device->update_state->index_offset;
1923 device->update_state->index_buffer = buffer;
1924 device->update_state->index_format = format_id;
1925 device->update_state->index_offset = offset;
1927 if (device->recording)
1928 device->recording->changed.indices = TRUE;
1930 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1931 return;
1933 if (buffer)
1934 wined3d_buffer_incref(buffer);
1935 if (!device->recording)
1936 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1937 if (prev_buffer)
1938 wined3d_buffer_decref(prev_buffer);
1941 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1942 enum wined3d_format_id *format, unsigned int *offset)
1944 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1946 *format = device->state.index_format;
1947 if (offset)
1948 *offset = device->state.index_offset;
1949 return device->state.index_buffer;
1952 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1954 TRACE("device %p, base_index %d.\n", device, base_index);
1956 device->update_state->base_vertex_index = base_index;
1959 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1961 TRACE("device %p.\n", device);
1963 return device->state.base_vertex_index;
1966 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1968 TRACE("device %p, viewport %p.\n", device, viewport);
1969 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1970 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1972 device->update_state->viewport = *viewport;
1974 /* Handle recording of state blocks */
1975 if (device->recording)
1977 TRACE("Recording... not performing anything\n");
1978 device->recording->changed.viewport = TRUE;
1979 return;
1982 wined3d_cs_emit_set_viewport(device->cs, viewport);
1985 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1987 TRACE("device %p, viewport %p.\n", device, viewport);
1989 *viewport = device->state.viewport;
1992 static void resolve_depth_buffer(struct wined3d_state *state)
1994 struct wined3d_texture *dst_texture = state->textures[0];
1995 struct wined3d_rendertarget_view *src_view;
1996 RECT src_rect, dst_rect;
1998 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1999 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2000 return;
2002 if (!(src_view = state->fb->depth_stencil))
2003 return;
2004 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2006 FIXME("Not supported on buffer resources.\n");
2007 return;
2010 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2011 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2012 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
2013 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2016 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2017 struct wined3d_rasterizer_state *rasterizer_state)
2019 struct wined3d_rasterizer_state *prev;
2021 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2023 prev = device->update_state->rasterizer_state;
2024 if (prev == rasterizer_state)
2025 return;
2027 if (rasterizer_state)
2028 wined3d_rasterizer_state_incref(rasterizer_state);
2029 device->update_state->rasterizer_state = rasterizer_state;
2030 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2031 if (prev)
2032 wined3d_rasterizer_state_decref(prev);
2035 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2037 TRACE("device %p.\n", device);
2039 return device->state.rasterizer_state;
2042 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2043 enum wined3d_render_state state, DWORD value)
2045 DWORD old_value;
2047 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2049 if (state > WINEHIGHEST_RENDER_STATE)
2051 WARN("Unhandled render state %#x.\n", state);
2052 return;
2055 old_value = device->state.render_states[state];
2056 device->update_state->render_states[state] = value;
2058 /* Handle recording of state blocks. */
2059 if (device->recording)
2061 TRACE("Recording... not performing anything.\n");
2062 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2063 return;
2066 /* Compared here and not before the assignment to allow proper stateblock recording. */
2067 if (value == old_value)
2068 TRACE("Application is setting the old value over, nothing to do.\n");
2069 else
2070 wined3d_cs_emit_set_render_state(device->cs, state, value);
2072 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2074 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2075 resolve_depth_buffer(&device->state);
2079 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2081 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2083 return device->state.render_states[state];
2086 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2087 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2089 DWORD old_value;
2091 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2092 device, sampler_idx, debug_d3dsamplerstate(state), value);
2094 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2095 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2097 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2099 WARN("Invalid sampler %u.\n", sampler_idx);
2100 return; /* Windows accepts overflowing this array ... we do not. */
2103 old_value = device->state.sampler_states[sampler_idx][state];
2104 device->update_state->sampler_states[sampler_idx][state] = value;
2106 /* Handle recording of state blocks. */
2107 if (device->recording)
2109 TRACE("Recording... not performing anything.\n");
2110 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2111 return;
2114 if (old_value == value)
2116 TRACE("Application is setting the old value over, nothing to do.\n");
2117 return;
2120 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2123 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2124 UINT sampler_idx, enum wined3d_sampler_state state)
2126 TRACE("device %p, sampler_idx %u, state %s.\n",
2127 device, sampler_idx, debug_d3dsamplerstate(state));
2129 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2130 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2132 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2134 WARN("Invalid sampler %u.\n", sampler_idx);
2135 return 0; /* Windows accepts overflowing this array ... we do not. */
2138 return device->state.sampler_states[sampler_idx][state];
2141 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2143 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2145 if (device->recording)
2146 device->recording->changed.scissorRect = TRUE;
2148 if (EqualRect(&device->update_state->scissor_rect, rect))
2150 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2151 return;
2153 CopyRect(&device->update_state->scissor_rect, rect);
2155 if (device->recording)
2157 TRACE("Recording... not performing anything.\n");
2158 return;
2161 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2164 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2166 TRACE("device %p, rect %p.\n", device, rect);
2168 *rect = device->state.scissor_rect;
2169 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2172 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2173 struct wined3d_vertex_declaration *declaration)
2175 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2177 TRACE("device %p, declaration %p.\n", device, declaration);
2179 if (device->recording)
2180 device->recording->changed.vertexDecl = TRUE;
2182 if (declaration == prev)
2183 return;
2185 if (declaration)
2186 wined3d_vertex_declaration_incref(declaration);
2187 device->update_state->vertex_declaration = declaration;
2188 if (!device->recording)
2189 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2190 if (prev)
2191 wined3d_vertex_declaration_decref(prev);
2194 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2196 TRACE("device %p.\n", device);
2198 return device->state.vertex_declaration;
2201 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2203 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2205 TRACE("device %p, shader %p.\n", device, shader);
2207 if (device->recording)
2208 device->recording->changed.vertexShader = TRUE;
2210 if (shader == prev)
2211 return;
2213 if (shader)
2214 wined3d_shader_incref(shader);
2215 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2216 if (!device->recording)
2217 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2218 if (prev)
2219 wined3d_shader_decref(prev);
2222 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2224 TRACE("device %p.\n", device);
2226 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2229 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2230 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2232 struct wined3d_buffer *prev;
2234 if (idx >= MAX_CONSTANT_BUFFERS)
2236 WARN("Invalid constant buffer index %u.\n", idx);
2237 return;
2240 prev = device->update_state->cb[type][idx];
2241 if (buffer == prev)
2242 return;
2244 if (buffer)
2245 wined3d_buffer_incref(buffer);
2246 device->update_state->cb[type][idx] = buffer;
2247 if (!device->recording)
2248 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2249 if (prev)
2250 wined3d_buffer_decref(prev);
2253 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2255 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2257 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2260 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2262 TRACE("device %p, idx %u.\n", device, idx);
2264 if (idx >= MAX_CONSTANT_BUFFERS)
2266 WARN("Invalid constant buffer index %u.\n", idx);
2267 return NULL;
2270 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2273 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2274 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2276 struct wined3d_shader_resource_view *prev;
2278 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2280 WARN("Invalid view index %u.\n", idx);
2281 return;
2284 prev = device->update_state->shader_resource_view[type][idx];
2285 if (view == prev)
2286 return;
2288 if (view)
2289 wined3d_shader_resource_view_incref(view);
2290 device->update_state->shader_resource_view[type][idx] = view;
2291 if (!device->recording)
2292 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2293 if (prev)
2294 wined3d_shader_resource_view_decref(prev);
2297 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2298 UINT idx, struct wined3d_shader_resource_view *view)
2300 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2302 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2305 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2306 UINT idx)
2308 TRACE("device %p, idx %u.\n", device, idx);
2310 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2312 WARN("Invalid view index %u.\n", idx);
2313 return NULL;
2316 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2319 static void wined3d_device_set_sampler(struct wined3d_device *device,
2320 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2322 struct wined3d_sampler *prev;
2324 if (idx >= MAX_SAMPLER_OBJECTS)
2326 WARN("Invalid sampler index %u.\n", idx);
2327 return;
2330 prev = device->update_state->sampler[type][idx];
2331 if (sampler == prev)
2332 return;
2334 if (sampler)
2335 wined3d_sampler_incref(sampler);
2336 device->update_state->sampler[type][idx] = sampler;
2337 if (!device->recording)
2338 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2339 if (prev)
2340 wined3d_sampler_decref(prev);
2343 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2345 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2347 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2350 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2352 TRACE("device %p, idx %u.\n", device, idx);
2354 if (idx >= MAX_SAMPLER_OBJECTS)
2356 WARN("Invalid sampler index %u.\n", idx);
2357 return NULL;
2360 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2363 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2364 unsigned int start_idx, unsigned int count, const BOOL *constants)
2366 unsigned int i;
2368 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2369 device, start_idx, count, constants);
2371 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2372 return WINED3DERR_INVALIDCALL;
2374 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2375 count = WINED3D_MAX_CONSTS_B - start_idx;
2376 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2377 if (TRACE_ON(d3d))
2379 for (i = 0; i < count; ++i)
2380 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2383 if (device->recording)
2385 for (i = start_idx; i < count + start_idx; ++i)
2386 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2388 else
2390 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2393 return WINED3D_OK;
2396 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2397 unsigned int start_idx, unsigned int count, BOOL *constants)
2399 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2400 device, start_idx, count, constants);
2402 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2403 return WINED3DERR_INVALIDCALL;
2405 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2406 count = WINED3D_MAX_CONSTS_B - start_idx;
2407 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2409 return WINED3D_OK;
2412 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2413 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2415 unsigned int i;
2417 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2418 device, start_idx, count, constants);
2420 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2421 return WINED3DERR_INVALIDCALL;
2423 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2424 count = WINED3D_MAX_CONSTS_I - start_idx;
2425 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2426 if (TRACE_ON(d3d))
2428 for (i = 0; i < count; ++i)
2429 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2432 if (device->recording)
2434 for (i = start_idx; i < count + start_idx; ++i)
2435 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2437 else
2439 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2442 return WINED3D_OK;
2445 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2446 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2448 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2449 device, start_idx, count, constants);
2451 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2452 return WINED3DERR_INVALIDCALL;
2454 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2455 count = WINED3D_MAX_CONSTS_I - start_idx;
2456 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2457 return WINED3D_OK;
2460 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2461 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2463 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2464 unsigned int i;
2466 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2467 device, start_idx, count, constants);
2469 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2470 || count > d3d_info->limits.vs_uniform_count - start_idx)
2471 return WINED3DERR_INVALIDCALL;
2473 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2474 if (TRACE_ON(d3d))
2476 for (i = 0; i < count; ++i)
2477 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2480 if (device->recording)
2481 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2482 count * sizeof(*device->recording->changed.vs_consts_f));
2483 else
2484 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2486 return WINED3D_OK;
2489 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2490 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2492 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2494 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2495 device, start_idx, count, constants);
2497 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2498 || count > d3d_info->limits.vs_uniform_count - start_idx)
2499 return WINED3DERR_INVALIDCALL;
2501 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2503 return WINED3D_OK;
2506 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2508 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2510 TRACE("device %p, shader %p.\n", device, shader);
2512 if (device->recording)
2513 device->recording->changed.pixelShader = TRUE;
2515 if (shader == prev)
2516 return;
2518 if (shader)
2519 wined3d_shader_incref(shader);
2520 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2521 if (!device->recording)
2522 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2523 if (prev)
2524 wined3d_shader_decref(prev);
2527 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2529 TRACE("device %p.\n", device);
2531 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2534 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2536 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2538 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2541 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2543 TRACE("device %p, idx %u.\n", device, idx);
2545 if (idx >= MAX_CONSTANT_BUFFERS)
2547 WARN("Invalid constant buffer index %u.\n", idx);
2548 return NULL;
2551 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2554 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2555 UINT idx, struct wined3d_shader_resource_view *view)
2557 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2559 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2562 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2563 UINT idx)
2565 TRACE("device %p, idx %u.\n", device, idx);
2567 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2569 WARN("Invalid view index %u.\n", idx);
2570 return NULL;
2573 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2576 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2578 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2580 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2583 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2585 TRACE("device %p, idx %u.\n", device, idx);
2587 if (idx >= MAX_SAMPLER_OBJECTS)
2589 WARN("Invalid sampler index %u.\n", idx);
2590 return NULL;
2593 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2596 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2597 unsigned int start_idx, unsigned int count, const BOOL *constants)
2599 unsigned int i;
2601 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2602 device, start_idx, count, constants);
2604 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2605 return WINED3DERR_INVALIDCALL;
2607 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2608 count = WINED3D_MAX_CONSTS_B - start_idx;
2609 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2610 if (TRACE_ON(d3d))
2612 for (i = 0; i < count; ++i)
2613 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2616 if (device->recording)
2618 for (i = start_idx; i < count + start_idx; ++i)
2619 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2621 else
2623 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2626 return WINED3D_OK;
2629 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2630 unsigned int start_idx, unsigned int count, BOOL *constants)
2632 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2633 device, start_idx, count, constants);
2635 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2636 return WINED3DERR_INVALIDCALL;
2638 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2639 count = WINED3D_MAX_CONSTS_B - start_idx;
2640 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2642 return WINED3D_OK;
2645 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2646 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2648 unsigned int i;
2650 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2651 device, start_idx, count, constants);
2653 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2654 return WINED3DERR_INVALIDCALL;
2656 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2657 count = WINED3D_MAX_CONSTS_I - start_idx;
2658 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2659 if (TRACE_ON(d3d))
2661 for (i = 0; i < count; ++i)
2662 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2665 if (device->recording)
2667 for (i = start_idx; i < count + start_idx; ++i)
2668 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2670 else
2672 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2675 return WINED3D_OK;
2678 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2679 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2681 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2682 device, start_idx, count, constants);
2684 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2685 return WINED3DERR_INVALIDCALL;
2687 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2688 count = WINED3D_MAX_CONSTS_I - start_idx;
2689 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2691 return WINED3D_OK;
2694 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2695 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2697 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2698 unsigned int i;
2700 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2701 device, start_idx, count, constants);
2703 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2704 || count > d3d_info->limits.ps_uniform_count - start_idx)
2705 return WINED3DERR_INVALIDCALL;
2707 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2708 if (TRACE_ON(d3d))
2710 for (i = 0; i < count; ++i)
2711 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2714 if (device->recording)
2715 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2716 count * sizeof(*device->recording->changed.ps_consts_f));
2717 else
2718 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2720 return WINED3D_OK;
2723 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2724 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2726 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2728 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2729 device, start_idx, count, constants);
2731 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2732 || count > d3d_info->limits.ps_uniform_count - start_idx)
2733 return WINED3DERR_INVALIDCALL;
2735 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2737 return WINED3D_OK;
2740 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2742 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2744 TRACE("device %p, shader %p.\n", device, shader);
2746 if (device->recording || shader == prev)
2747 return;
2748 if (shader)
2749 wined3d_shader_incref(shader);
2750 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2751 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2752 if (prev)
2753 wined3d_shader_decref(prev);
2756 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2758 TRACE("device %p.\n", device);
2760 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2763 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2765 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2767 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2770 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2772 TRACE("device %p, idx %u.\n", device, idx);
2774 if (idx >= MAX_CONSTANT_BUFFERS)
2776 WARN("Invalid constant buffer index %u.\n", idx);
2777 return NULL;
2780 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2783 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2784 UINT idx, struct wined3d_shader_resource_view *view)
2786 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2788 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2791 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2792 UINT idx)
2794 TRACE("device %p, idx %u.\n", device, idx);
2796 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2798 WARN("Invalid view index %u.\n", idx);
2799 return NULL;
2802 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2805 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2807 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2809 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2812 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2814 TRACE("device %p, idx %u.\n", device, idx);
2816 if (idx >= MAX_SAMPLER_OBJECTS)
2818 WARN("Invalid sampler index %u.\n", idx);
2819 return NULL;
2822 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2825 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2827 struct wined3d_shader *prev;
2829 TRACE("device %p, shader %p.\n", device, shader);
2831 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2832 if (device->recording || shader == prev)
2833 return;
2834 if (shader)
2835 wined3d_shader_incref(shader);
2836 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2837 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2838 if (prev)
2839 wined3d_shader_decref(prev);
2842 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2844 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2846 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2849 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2850 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav)
2852 struct wined3d_unordered_access_view *prev;
2854 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2856 WARN("Invalid UAV index %u.\n", idx);
2857 return;
2860 prev = device->update_state->unordered_access_view[pipeline][idx];
2861 if (uav == prev)
2862 return;
2864 if (uav)
2865 wined3d_unordered_access_view_incref(uav);
2866 device->update_state->unordered_access_view[pipeline][idx] = uav;
2867 if (!device->recording)
2868 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav);
2869 if (prev)
2870 wined3d_unordered_access_view_decref(prev);
2873 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2874 struct wined3d_unordered_access_view *uav)
2876 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2878 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav);
2881 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2882 unsigned int idx, struct wined3d_unordered_access_view *uav)
2884 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2886 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav);
2889 /* Context activation is done by the caller. */
2890 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2891 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2892 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2893 DWORD DestFVF)
2895 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2896 struct wined3d_map_desc map_desc;
2897 struct wined3d_box box = {0};
2898 struct wined3d_viewport vp;
2899 UINT vertex_size;
2900 unsigned int i;
2901 BYTE *dest_ptr;
2902 BOOL doClip;
2903 DWORD numTextures;
2904 HRESULT hr;
2906 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2908 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2911 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2913 ERR("Source has no position mask\n");
2914 return WINED3DERR_INVALIDCALL;
2917 if (device->state.render_states[WINED3D_RS_CLIPPING])
2919 static BOOL warned = FALSE;
2921 * The clipping code is not quite correct. Some things need
2922 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2923 * so disable clipping for now.
2924 * (The graphics in Half-Life are broken, and my processvertices
2925 * test crashes with IDirect3DDevice3)
2926 doClip = TRUE;
2928 doClip = FALSE;
2929 if(!warned) {
2930 warned = TRUE;
2931 FIXME("Clipping is broken and disabled for now\n");
2934 else
2935 doClip = FALSE;
2937 vertex_size = get_flexible_vertex_size(DestFVF);
2938 box.left = dwDestIndex * vertex_size;
2939 box.right = box.left + dwCount * vertex_size;
2940 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2942 WARN("Failed to map buffer, hr %#x.\n", hr);
2943 return hr;
2945 dest_ptr = map_desc.data;
2947 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2948 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2949 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2951 TRACE("View mat:\n");
2952 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2953 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2954 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2955 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2957 TRACE("Proj mat:\n");
2958 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2959 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2960 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2961 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2963 TRACE("World mat:\n");
2964 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2965 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2966 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2967 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2969 /* Get the viewport */
2970 wined3d_device_get_viewport(device, &vp);
2971 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2972 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2974 multiply_matrix(&mat,&view_mat,&world_mat);
2975 multiply_matrix(&mat,&proj_mat,&mat);
2977 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2979 for (i = 0; i < dwCount; i+= 1) {
2980 unsigned int tex_index;
2982 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2983 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2984 /* The position first */
2985 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2986 const float *p = (const float *)(element->data.addr + i * element->stride);
2987 float x, y, z, rhw;
2988 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2990 /* Multiplication with world, view and projection matrix. */
2991 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2992 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2993 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2994 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2996 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2998 /* WARNING: The following things are taken from d3d7 and were not yet checked
2999 * against d3d8 or d3d9!
3002 /* Clipping conditions: From msdn
3004 * A vertex is clipped if it does not match the following requirements
3005 * -rhw < x <= rhw
3006 * -rhw < y <= rhw
3007 * 0 < z <= rhw
3008 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3010 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3011 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3015 if( !doClip ||
3016 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3017 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3018 ( rhw > eps ) ) ) {
3020 /* "Normal" viewport transformation (not clipped)
3021 * 1) The values are divided by rhw
3022 * 2) The y axis is negative, so multiply it with -1
3023 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3024 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3025 * 4) Multiply x with Width/2 and add Width/2
3026 * 5) The same for the height
3027 * 6) Add the viewpoint X and Y to the 2D coordinates and
3028 * The minimum Z value to z
3029 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3031 * Well, basically it's simply a linear transformation into viewport
3032 * coordinates
3035 x /= rhw;
3036 y /= rhw;
3037 z /= rhw;
3039 y *= -1;
3041 x *= vp.width / 2;
3042 y *= vp.height / 2;
3043 z *= vp.max_z - vp.min_z;
3045 x += vp.width / 2 + vp.x;
3046 y += vp.height / 2 + vp.y;
3047 z += vp.min_z;
3049 rhw = 1 / rhw;
3050 } else {
3051 /* That vertex got clipped
3052 * Contrary to OpenGL it is not dropped completely, it just
3053 * undergoes a different calculation.
3055 TRACE("Vertex got clipped\n");
3056 x += rhw;
3057 y += rhw;
3059 x /= 2;
3060 y /= 2;
3062 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3063 * outside of the main vertex buffer memory. That needs some more
3064 * investigation...
3068 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3071 ( (float *) dest_ptr)[0] = x;
3072 ( (float *) dest_ptr)[1] = y;
3073 ( (float *) dest_ptr)[2] = z;
3074 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3076 dest_ptr += 3 * sizeof(float);
3078 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3079 dest_ptr += sizeof(float);
3082 if (DestFVF & WINED3DFVF_PSIZE)
3083 dest_ptr += sizeof(DWORD);
3085 if (DestFVF & WINED3DFVF_NORMAL)
3087 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3088 const float *normal = (const float *)(element->data.addr + i * element->stride);
3089 /* AFAIK this should go into the lighting information */
3090 FIXME("Didn't expect the destination to have a normal\n");
3091 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3094 if (DestFVF & WINED3DFVF_DIFFUSE)
3096 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3097 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3098 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3100 static BOOL warned = FALSE;
3102 if(!warned) {
3103 ERR("No diffuse color in source, but destination has one\n");
3104 warned = TRUE;
3107 *( (DWORD *) dest_ptr) = 0xffffffff;
3108 dest_ptr += sizeof(DWORD);
3110 else
3112 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3116 if (DestFVF & WINED3DFVF_SPECULAR)
3118 /* What's the color value in the feedback buffer? */
3119 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3120 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3121 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3123 static BOOL warned = FALSE;
3125 if(!warned) {
3126 ERR("No specular color in source, but destination has one\n");
3127 warned = TRUE;
3130 *(DWORD *)dest_ptr = 0xff000000;
3131 dest_ptr += sizeof(DWORD);
3133 else
3135 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3139 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3141 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3142 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3143 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3145 ERR("No source texture, but destination requests one\n");
3146 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3148 else
3150 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3155 wined3d_resource_unmap(&dest->resource, 0);
3157 return WINED3D_OK;
3159 #undef copy_and_next
3161 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3162 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3163 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3165 struct wined3d_state *state = &device->state;
3166 struct wined3d_stream_info stream_info;
3167 const struct wined3d_gl_info *gl_info;
3168 struct wined3d_context *context;
3169 struct wined3d_shader *vs;
3170 unsigned int i;
3171 HRESULT hr;
3172 WORD map;
3174 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3175 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3176 device, src_start_idx, dst_idx, vertex_count,
3177 dst_buffer, declaration, flags, dst_fvf);
3179 if (declaration)
3180 FIXME("Output vertex declaration not implemented yet.\n");
3182 /* Need any context to write to the vbo. */
3183 context = context_acquire(device, NULL, 0);
3184 gl_info = context->gl_info;
3186 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3187 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3188 context_stream_info_from_declaration(context, state, &stream_info);
3189 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3191 /* We can't convert FROM a VBO, and vertex buffers used to source into
3192 * process_vertices() are unlikely to ever be used for drawing. Release
3193 * VBOs in those buffers and fix up the stream_info structure.
3195 * Also apply the start index. */
3196 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3198 struct wined3d_stream_info_element *e;
3199 struct wined3d_buffer *buffer;
3201 if (!(map & 1))
3202 continue;
3204 e = &stream_info.elements[i];
3205 buffer = state->streams[e->stream_idx].buffer;
3206 e->data.buffer_object = 0;
3207 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3208 if (buffer->buffer_object)
3210 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3211 buffer->buffer_object = 0;
3212 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
3214 if (e->data.addr)
3215 e->data.addr += e->stride * src_start_idx;
3218 hr = process_vertices_strided(device, dst_idx, vertex_count,
3219 &stream_info, dst_buffer, flags, dst_fvf);
3221 context_release(context);
3223 return hr;
3226 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3227 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3229 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3230 DWORD old_value;
3232 TRACE("device %p, stage %u, state %s, value %#x.\n",
3233 device, stage, debug_d3dtexturestate(state), value);
3235 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3237 WARN("Invalid state %#x passed.\n", state);
3238 return;
3241 if (stage >= d3d_info->limits.ffp_blend_stages)
3243 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3244 stage, d3d_info->limits.ffp_blend_stages - 1);
3245 return;
3248 old_value = device->update_state->texture_states[stage][state];
3249 device->update_state->texture_states[stage][state] = value;
3251 if (device->recording)
3253 TRACE("Recording... not performing anything.\n");
3254 device->recording->changed.textureState[stage] |= 1u << state;
3255 return;
3258 /* Checked after the assignments to allow proper stateblock recording. */
3259 if (old_value == value)
3261 TRACE("Application is setting the old value over, nothing to do.\n");
3262 return;
3265 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3268 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3269 UINT stage, enum wined3d_texture_stage_state state)
3271 TRACE("device %p, stage %u, state %s.\n",
3272 device, stage, debug_d3dtexturestate(state));
3274 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3276 WARN("Invalid state %#x passed.\n", state);
3277 return 0;
3280 return device->state.texture_states[stage][state];
3283 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3284 UINT stage, struct wined3d_texture *texture)
3286 struct wined3d_texture *prev;
3288 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3290 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3291 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3293 /* Windows accepts overflowing this array... we do not. */
3294 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3296 WARN("Ignoring invalid stage %u.\n", stage);
3297 return WINED3D_OK;
3300 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3302 WARN("Rejecting attempt to set scratch texture.\n");
3303 return WINED3DERR_INVALIDCALL;
3306 if (device->recording)
3307 device->recording->changed.textures |= 1u << stage;
3309 prev = device->update_state->textures[stage];
3310 TRACE("Previous texture %p.\n", prev);
3312 if (texture == prev)
3314 TRACE("App is setting the same texture again, nothing to do.\n");
3315 return WINED3D_OK;
3318 TRACE("Setting new texture to %p.\n", texture);
3319 device->update_state->textures[stage] = texture;
3321 if (texture)
3322 wined3d_texture_incref(texture);
3323 if (!device->recording)
3324 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3325 if (prev)
3326 wined3d_texture_decref(prev);
3328 return WINED3D_OK;
3331 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3333 TRACE("device %p, stage %u.\n", device, stage);
3335 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3336 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3338 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3340 WARN("Ignoring invalid stage %u.\n", stage);
3341 return NULL; /* Windows accepts overflowing this array ... we do not. */
3344 return device->state.textures[stage];
3347 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3349 TRACE("device %p, caps %p.\n", device, caps);
3351 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3352 device->create_parms.device_type, caps);
3355 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3356 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3358 struct wined3d_swapchain *swapchain;
3360 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3361 device, swapchain_idx, mode, rotation);
3363 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3364 return WINED3DERR_INVALIDCALL;
3366 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3369 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3371 struct wined3d_stateblock *stateblock;
3372 HRESULT hr;
3374 TRACE("device %p.\n", device);
3376 if (device->recording)
3377 return WINED3DERR_INVALIDCALL;
3379 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3380 if (FAILED(hr))
3381 return hr;
3383 device->recording = stateblock;
3384 device->update_state = &stateblock->state;
3386 TRACE("Recording stateblock %p.\n", stateblock);
3388 return WINED3D_OK;
3391 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3392 struct wined3d_stateblock **stateblock)
3394 struct wined3d_stateblock *object = device->recording;
3396 TRACE("device %p, stateblock %p.\n", device, stateblock);
3398 if (!device->recording)
3400 WARN("Not recording.\n");
3401 *stateblock = NULL;
3402 return WINED3DERR_INVALIDCALL;
3405 stateblock_init_contained_states(object);
3407 *stateblock = object;
3408 device->recording = NULL;
3409 device->update_state = &device->state;
3411 TRACE("Returning stateblock %p.\n", *stateblock);
3413 return WINED3D_OK;
3416 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3418 /* At the moment we have no need for any functionality at the beginning
3419 * of a scene. */
3420 TRACE("device %p.\n", device);
3422 if (device->inScene)
3424 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3425 return WINED3DERR_INVALIDCALL;
3427 device->inScene = TRUE;
3428 return WINED3D_OK;
3431 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3433 struct wined3d_context *context;
3435 TRACE("device %p.\n", device);
3437 if (!device->inScene)
3439 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3440 return WINED3DERR_INVALIDCALL;
3443 context = context_acquire(device, NULL, 0);
3444 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3445 context->gl_info->gl_ops.gl.p_glFlush();
3446 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3447 * fails. */
3448 context_release(context);
3450 device->inScene = FALSE;
3451 return WINED3D_OK;
3454 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3455 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3457 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3458 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3460 if (!rect_count && rects)
3462 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3463 return WINED3D_OK;
3466 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3468 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3469 if (!ds)
3471 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3472 /* TODO: What about depth stencil buffers without stencil bits? */
3473 return WINED3DERR_INVALIDCALL;
3475 else if (flags & WINED3DCLEAR_TARGET)
3477 if (ds->width < device->fb.render_targets[0]->width
3478 || ds->height < device->fb.render_targets[0]->height)
3480 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3481 return WINED3D_OK;
3486 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3488 return WINED3D_OK;
3491 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3492 struct wined3d_query *predicate, BOOL value)
3494 struct wined3d_query *prev;
3496 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3498 prev = device->update_state->predicate;
3499 if (predicate)
3501 FIXME("Predicated rendering not implemented.\n");
3502 wined3d_query_incref(predicate);
3504 device->update_state->predicate = predicate;
3505 device->update_state->predicate_value = value;
3506 if (!device->recording)
3507 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3508 if (prev)
3509 wined3d_query_decref(prev);
3512 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3514 TRACE("device %p, value %p.\n", device, value);
3516 *value = device->state.predicate_value;
3517 return device->state.predicate;
3520 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3521 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3523 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3524 device, group_count_x, group_count_y, group_count_z);
3526 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3529 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3530 enum wined3d_primitive_type primitive_type)
3532 GLenum gl_primitive_type, prev;
3534 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3536 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3537 prev = device->update_state->gl_primitive_type;
3538 device->update_state->gl_primitive_type = gl_primitive_type;
3539 if (device->recording)
3540 device->recording->changed.primitive_type = TRUE;
3541 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3542 device_invalidate_state(device, STATE_POINT_ENABLE);
3545 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3546 enum wined3d_primitive_type *primitive_type)
3548 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3550 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3552 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3555 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3557 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3559 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, 0, 0, FALSE);
3561 return WINED3D_OK;
3564 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3565 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3567 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3568 device, start_vertex, vertex_count, start_instance, instance_count);
3570 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3573 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3575 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3577 if (!device->state.index_buffer)
3579 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3580 * without an index buffer set. (The first time at least...)
3581 * D3D8 simply dies, but I doubt it can do much harm to return
3582 * D3DERR_INVALIDCALL there as well. */
3583 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3584 return WINED3DERR_INVALIDCALL;
3587 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3589 return WINED3D_OK;
3592 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3593 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3595 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3596 device, start_idx, index_count, start_instance, instance_count);
3598 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index,
3599 start_idx, index_count, start_instance, instance_count, TRUE);
3602 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3603 struct wined3d_texture *src_texture, unsigned int src_level,
3604 struct wined3d_texture *dst_texture, unsigned int level_count)
3606 struct wined3d_const_bo_address data;
3607 struct wined3d_context *context;
3608 struct wined3d_map_desc src;
3609 HRESULT hr = WINED3D_OK;
3610 unsigned int i;
3612 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3613 device, src_texture, src_level, dst_texture, level_count);
3615 if (src_texture->resource.format != dst_texture->resource.format)
3617 WARN("Source and destination formats do not match.\n");
3618 return WINED3DERR_INVALIDCALL;
3621 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3622 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3623 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3625 WARN("Source and destination dimensions do not match.\n");
3626 return WINED3DERR_INVALIDCALL;
3629 context = context_acquire(device, NULL, 0);
3631 /* Only a prepare, since we're uploading entire volumes. */
3632 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3633 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3635 for (i = 0; i < level_count; ++i)
3637 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3638 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3639 goto done;
3641 data.buffer_object = 0;
3642 data.addr = src.data;
3643 wined3d_texture_upload_data(dst_texture, i, context, NULL, &data, src.row_pitch, src.slice_pitch);
3644 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3646 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3647 goto done;
3650 done:
3651 context_release(context);
3652 return hr;
3655 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3656 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3658 unsigned int src_size, dst_size, src_skip_levels = 0;
3659 unsigned int layer_count, level_count, i, j;
3660 enum wined3d_resource_type type;
3661 HRESULT hr;
3662 struct wined3d_context *context;
3664 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3666 /* Verify that the source and destination textures are non-NULL. */
3667 if (!src_texture || !dst_texture)
3669 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3670 return WINED3DERR_INVALIDCALL;
3673 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3675 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3676 return WINED3DERR_INVALIDCALL;
3678 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3680 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3681 return WINED3DERR_INVALIDCALL;
3684 /* Verify that the source and destination textures are the same type. */
3685 type = src_texture->resource.type;
3686 if (dst_texture->resource.type != type)
3688 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3689 return WINED3DERR_INVALIDCALL;
3692 layer_count = src_texture->layer_count;
3693 if (layer_count != dst_texture->layer_count)
3695 WARN("Source and destination have different layer counts.\n");
3696 return WINED3DERR_INVALIDCALL;
3699 level_count = min(wined3d_texture_get_level_count(src_texture),
3700 wined3d_texture_get_level_count(dst_texture));
3702 src_size = max(src_texture->resource.width, src_texture->resource.height);
3703 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3704 if (type == WINED3D_RTYPE_TEXTURE_3D)
3706 src_size = max(src_size, src_texture->resource.depth);
3707 dst_size = max(dst_size, dst_texture->resource.depth);
3709 while (src_size > dst_size)
3711 src_size >>= 1;
3712 ++src_skip_levels;
3715 /* Make sure that the destination texture is loaded. */
3716 context = context_acquire(device, NULL, 0);
3717 wined3d_texture_load(dst_texture, context, FALSE);
3718 context_release(context);
3720 /* Update every surface level of the texture. */
3721 switch (type)
3723 case WINED3D_RTYPE_TEXTURE_2D:
3725 unsigned int src_levels = src_texture->level_count;
3726 unsigned int dst_levels = dst_texture->level_count;
3727 struct wined3d_surface *src_surface;
3728 struct wined3d_surface *dst_surface;
3730 for (i = 0; i < layer_count; ++i)
3732 for (j = 0; j < level_count; ++j)
3734 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3735 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3736 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3738 WARN("Failed to update surface, hr %#x.\n", hr);
3739 return hr;
3743 return WINED3D_OK;
3746 case WINED3D_RTYPE_TEXTURE_3D:
3747 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3748 src_texture, src_skip_levels, dst_texture, level_count)))
3749 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3750 return hr;
3752 default:
3753 FIXME("Unsupported texture type %#x.\n", type);
3754 return WINED3DERR_INVALIDCALL;
3758 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3760 const struct wined3d_state *state = &device->state;
3761 struct wined3d_texture *texture;
3762 DWORD i;
3764 TRACE("device %p, num_passes %p.\n", device, num_passes);
3766 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3768 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3770 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3771 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3773 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3775 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3776 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3779 texture = state->textures[i];
3780 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3782 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3784 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3785 return E_FAIL;
3787 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3789 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3790 return E_FAIL;
3792 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3793 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3795 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3796 return E_FAIL;
3800 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3801 || state->render_states[WINED3D_RS_STENCILENABLE])
3803 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3804 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3806 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3808 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3809 return WINED3DERR_CONFLICTINGRENDERSTATE;
3813 /* return a sensible default */
3814 *num_passes = 1;
3816 TRACE("returning D3D_OK\n");
3817 return WINED3D_OK;
3820 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3822 static BOOL warned;
3824 TRACE("device %p, software %#x.\n", device, software);
3826 if (!warned)
3828 FIXME("device %p, software %#x stub!\n", device, software);
3829 warned = TRUE;
3832 device->softwareVertexProcessing = software;
3835 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3837 static BOOL warned;
3839 TRACE("device %p.\n", device);
3841 if (!warned)
3843 TRACE("device %p stub!\n", device);
3844 warned = TRUE;
3847 return device->softwareVertexProcessing;
3850 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3851 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3853 struct wined3d_swapchain *swapchain;
3855 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3856 device, swapchain_idx, raster_status);
3858 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3859 return WINED3DERR_INVALIDCALL;
3861 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3864 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3866 static BOOL warned;
3868 TRACE("device %p, segments %.8e.\n", device, segments);
3870 if (segments != 0.0f)
3872 if (!warned)
3874 FIXME("device %p, segments %.8e stub!\n", device, segments);
3875 warned = TRUE;
3879 return WINED3D_OK;
3882 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3884 static BOOL warned;
3886 TRACE("device %p.\n", device);
3888 if (!warned)
3890 FIXME("device %p stub!\n", device);
3891 warned = TRUE;
3894 return 0.0f;
3897 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3898 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3900 struct wined3d_texture *dst_texture, *src_texture;
3901 RECT dst_rect, src_rect;
3902 unsigned int i, j;
3903 HRESULT hr;
3905 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3907 if (src_resource == dst_resource)
3909 WARN("Source and destination are the same resource.\n");
3910 return;
3913 if (src_resource->type != dst_resource->type)
3915 WARN("Resource types (%s / %s) don't match.\n",
3916 debug_d3dresourcetype(dst_resource->type),
3917 debug_d3dresourcetype(src_resource->type));
3918 return;
3921 if (src_resource->width != dst_resource->width
3922 || src_resource->height != dst_resource->height
3923 || src_resource->depth != dst_resource->depth)
3925 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3926 dst_resource->width, dst_resource->height, dst_resource->depth,
3927 src_resource->width, src_resource->height, src_resource->depth);
3928 return;
3931 if (src_resource->format->id != dst_resource->format->id)
3933 WARN("Resource formats (%s / %s) don't match.\n",
3934 debug_d3dformat(dst_resource->format->id),
3935 debug_d3dformat(src_resource->format->id));
3936 return;
3939 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3941 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3942 buffer_from_resource(src_resource), 0,
3943 dst_resource->size)))
3944 ERR("Failed to copy buffer, hr %#x.\n", hr);
3945 return;
3948 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3950 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3951 return;
3954 dst_texture = texture_from_resource(dst_resource);
3955 src_texture = texture_from_resource(src_resource);
3957 if (src_texture->layer_count != dst_texture->layer_count
3958 || src_texture->level_count != dst_texture->level_count)
3960 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3961 dst_texture->layer_count, dst_texture->level_count,
3962 src_texture->layer_count, src_texture->level_count);
3963 return;
3966 for (i = 0; i < dst_texture->level_count; ++i)
3968 SetRect(&dst_rect, 0, 0,
3969 wined3d_texture_get_level_width(dst_texture, i),
3970 wined3d_texture_get_level_height(dst_texture, i));
3971 SetRect(&src_rect, 0, 0,
3972 wined3d_texture_get_level_width(src_texture, i),
3973 wined3d_texture_get_level_height(dst_texture, i));
3974 for (j = 0; j < dst_texture->layer_count; ++j)
3976 unsigned int idx = j * dst_texture->level_count + i;
3978 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3979 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3980 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3985 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3986 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3987 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3988 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3990 struct wined3d_texture *dst_texture, *src_texture;
3991 RECT dst_rect, src_rect;
3992 HRESULT hr;
3994 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3995 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3996 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3997 src_resource, src_sub_resource_idx, debug_box(src_box));
3999 if (src_box && (src_box->left >= src_box->right
4000 || src_box->top >= src_box->bottom
4001 || src_box->front >= src_box->back))
4003 WARN("Invalid box %s specified.\n", debug_box(src_box));
4004 return WINED3DERR_INVALIDCALL;
4007 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4009 WARN("Source and destination are the same sub-resource.\n");
4010 return WINED3DERR_INVALIDCALL;
4013 if (src_resource->type != dst_resource->type)
4015 WARN("Resource types (%s / %s) don't match.\n",
4016 debug_d3dresourcetype(dst_resource->type),
4017 debug_d3dresourcetype(src_resource->type));
4018 return WINED3DERR_INVALIDCALL;
4021 if (src_resource->format->id != dst_resource->format->id)
4023 WARN("Resource formats (%s / %s) don't match.\n",
4024 debug_d3dformat(dst_resource->format->id),
4025 debug_d3dformat(src_resource->format->id));
4026 return WINED3DERR_INVALIDCALL;
4029 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4031 unsigned int src_offset, size;
4033 if (dst_sub_resource_idx)
4035 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4036 return WINED3DERR_INVALIDCALL;
4038 if (src_sub_resource_idx)
4040 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4041 return WINED3DERR_INVALIDCALL;
4044 if (src_box)
4046 src_offset = src_box->left;
4047 size = src_box->right - src_box->left;
4049 else
4051 src_offset = 0;
4052 size = src_resource->size;
4055 if (src_offset > src_resource->size
4056 || size > src_resource->size - src_offset
4057 || dst_x > dst_resource->size
4058 || size > dst_resource->size - dst_x)
4060 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4061 dst_x, src_offset, size);
4062 return WINED3DERR_INVALIDCALL;
4065 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4066 buffer_from_resource(src_resource), src_offset, size);
4069 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4071 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4072 return WINED3DERR_INVALIDCALL;
4075 dst_texture = texture_from_resource(dst_resource);
4076 src_texture = texture_from_resource(src_resource);
4078 if (src_box)
4080 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4082 else
4084 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4086 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4087 wined3d_texture_get_level_height(src_texture, level));
4090 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4091 dst_y + (src_rect.bottom - src_rect.top));
4093 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4094 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4095 WARN("Failed to blit, hr %#x.\n", hr);
4097 return hr;
4100 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4101 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4102 unsigned int depth_pitch)
4104 unsigned int width, height, depth, level;
4105 struct wined3d_const_bo_address addr;
4106 struct wined3d_context *context;
4107 struct wined3d_texture *texture;
4109 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4110 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4112 if (resource->type == WINED3D_RTYPE_BUFFER)
4114 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4116 if (sub_resource_idx > 0)
4118 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4119 return;
4122 context = context_acquire(resource->device, NULL, 0);
4123 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
4125 ERR("Failed to load buffer location.\n");
4126 context_release(context);
4127 return;
4130 wined3d_buffer_upload_data(buffer, context, box, data);
4131 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
4132 context_release(context);
4134 return;
4137 if (resource->type != WINED3D_RTYPE_TEXTURE_2D && resource->type != WINED3D_RTYPE_TEXTURE_3D)
4139 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4140 return;
4143 texture = texture_from_resource(resource);
4144 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4146 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4147 return;
4150 level = sub_resource_idx % texture->level_count;
4151 width = wined3d_texture_get_level_width(texture, level);
4152 height = wined3d_texture_get_level_height(texture, level);
4153 depth = wined3d_texture_get_level_depth(texture, level);
4155 if (box && (box->left >= box->right || box->right > width
4156 || box->top >= box->bottom || box->bottom > height
4157 || box->front >= box->back || box->back > depth))
4159 WARN("Invalid box %s specified.\n", debug_box(box));
4160 return;
4163 addr.buffer_object = 0;
4164 addr.addr = data;
4166 context = context_acquire(resource->device, NULL, 0);
4168 /* Only load the sub-resource for partial updates. */
4169 if (!box || (!box->left && !box->top && !box->front
4170 && box->right == width && box->bottom == height && box->back == depth))
4171 wined3d_texture_prepare_texture(texture, context, FALSE);
4172 else
4173 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4174 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4176 wined3d_texture_upload_data(texture, sub_resource_idx, context, box, &addr, row_pitch, depth_pitch);
4178 context_release(context);
4180 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4181 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4184 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4185 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4186 const struct wined3d_color *color, float depth, DWORD stencil)
4188 const struct blit_shader *blitter;
4189 struct wined3d_resource *resource;
4190 enum wined3d_blit_op blit_op;
4191 RECT r;
4193 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4194 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4196 if (!flags)
4197 return WINED3D_OK;
4199 resource = view->resource;
4200 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4202 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4203 return WINED3DERR_INVALIDCALL;
4206 if (view->depth > 1)
4208 FIXME("Layered clears not implemented.\n");
4209 return WINED3DERR_INVALIDCALL;
4212 if (!rect)
4214 SetRect(&r, 0, 0, view->width, view->height);
4215 rect = &r;
4218 if (flags & WINED3DCLEAR_TARGET)
4219 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4220 else
4221 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4223 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4224 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4226 FIXME("No blitter is capable of performing the requested fill operation.\n");
4227 return WINED3DERR_INVALIDCALL;
4230 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4231 return blitter->color_fill(device, view, rect, color);
4232 else
4233 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4236 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4237 unsigned int view_idx)
4239 TRACE("device %p, view_idx %u.\n", device, view_idx);
4241 if (view_idx >= device->adapter->gl_info.limits.buffers)
4243 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4244 return NULL;
4247 return device->fb.render_targets[view_idx];
4250 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4252 TRACE("device %p.\n", device);
4254 return device->fb.depth_stencil;
4257 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4258 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4260 struct wined3d_rendertarget_view *prev;
4262 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4263 device, view_idx, view, set_viewport);
4265 if (view_idx >= device->adapter->gl_info.limits.buffers)
4267 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4268 return WINED3DERR_INVALIDCALL;
4271 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4273 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4274 return WINED3DERR_INVALIDCALL;
4277 /* Set the viewport and scissor rectangles, if requested. Tests show that
4278 * stateblock recording is ignored, the change goes directly into the
4279 * primary stateblock. */
4280 if (!view_idx && set_viewport)
4282 struct wined3d_state *state = &device->state;
4284 state->viewport.x = 0;
4285 state->viewport.y = 0;
4286 state->viewport.width = view->width;
4287 state->viewport.height = view->height;
4288 state->viewport.min_z = 0.0f;
4289 state->viewport.max_z = 1.0f;
4290 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4292 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4293 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4297 prev = device->fb.render_targets[view_idx];
4298 if (view == prev)
4299 return WINED3D_OK;
4301 if (view)
4302 wined3d_rendertarget_view_incref(view);
4303 device->fb.render_targets[view_idx] = view;
4304 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4305 /* Release after the assignment, to prevent device_resource_released()
4306 * from seeing the surface as still in use. */
4307 if (prev)
4308 wined3d_rendertarget_view_decref(prev);
4310 return WINED3D_OK;
4313 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4315 struct wined3d_rendertarget_view *prev;
4317 TRACE("device %p, view %p.\n", device, view);
4319 prev = device->fb.depth_stencil;
4320 if (prev == view)
4322 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4323 return;
4326 if ((device->fb.depth_stencil = view))
4327 wined3d_rendertarget_view_incref(view);
4328 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4329 if (prev)
4330 wined3d_rendertarget_view_decref(prev);
4333 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4334 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4336 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4337 struct wined3d_sub_resource_data data;
4338 struct wined3d_resource_desc desc;
4339 struct wined3d_map_desc map_desc;
4340 struct wined3d_texture *texture;
4341 HRESULT hr;
4343 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4345 ERR("Failed to map source texture.\n");
4346 return NULL;
4349 data.data = map_desc.data;
4350 data.row_pitch = map_desc.row_pitch;
4351 data.slice_pitch = map_desc.slice_pitch;
4353 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4354 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4355 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4356 desc.multisample_quality = 0;
4357 desc.usage = WINED3DUSAGE_DYNAMIC;
4358 desc.pool = WINED3D_POOL_DEFAULT;
4359 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4360 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4361 desc.depth = 1;
4362 desc.size = 0;
4364 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4365 &data, NULL, &wined3d_null_parent_ops, &texture);
4366 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4367 if (FAILED(hr))
4369 ERR("Failed to create cursor texture.\n");
4370 return NULL;
4373 return texture;
4376 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4377 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4379 unsigned int texture_level = sub_resource_idx % texture->level_count;
4380 unsigned int cursor_width, cursor_height;
4381 struct wined3d_display_mode mode;
4382 struct wined3d_map_desc map_desc;
4383 HRESULT hr;
4385 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4386 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4388 if (sub_resource_idx >= texture->level_count * texture->layer_count
4389 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4390 return WINED3DERR_INVALIDCALL;
4392 if (device->cursor_texture)
4394 wined3d_texture_decref(device->cursor_texture);
4395 device->cursor_texture = NULL;
4398 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4400 WARN("Texture %p has invalid format %s.\n",
4401 texture, debug_d3dformat(texture->resource.format->id));
4402 return WINED3DERR_INVALIDCALL;
4405 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4407 ERR("Failed to get display mode, hr %#x.\n", hr);
4408 return WINED3DERR_INVALIDCALL;
4411 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4412 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4413 if (cursor_width > mode.width || cursor_height > mode.height)
4415 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4416 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4417 return WINED3DERR_INVALIDCALL;
4420 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4422 /* Do not store the surface's pointer because the application may
4423 * release it after setting the cursor image. Windows doesn't
4424 * addref the set surface, so we can't do this either without
4425 * creating circular refcount dependencies. */
4426 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4428 ERR("Failed to create cursor texture.\n");
4429 return WINED3DERR_INVALIDCALL;
4432 if (cursor_width == 32 && cursor_height == 32)
4434 UINT mask_size = cursor_width * cursor_height / 8;
4435 ICONINFO cursor_info;
4436 DWORD *mask_bits;
4437 HCURSOR cursor;
4439 /* 32-bit user32 cursors ignore the alpha channel if it's all
4440 * zeroes, and use the mask instead. Fill the mask with all ones
4441 * to ensure we still get a fully transparent cursor. */
4442 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4443 return E_OUTOFMEMORY;
4444 memset(mask_bits, 0xff, mask_size);
4446 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4447 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4448 cursor_info.fIcon = FALSE;
4449 cursor_info.xHotspot = x_hotspot;
4450 cursor_info.yHotspot = y_hotspot;
4451 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4452 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4453 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4455 /* Create our cursor and clean up. */
4456 cursor = CreateIconIndirect(&cursor_info);
4457 if (cursor_info.hbmMask)
4458 DeleteObject(cursor_info.hbmMask);
4459 if (cursor_info.hbmColor)
4460 DeleteObject(cursor_info.hbmColor);
4461 if (device->hardwareCursor)
4462 DestroyCursor(device->hardwareCursor);
4463 device->hardwareCursor = cursor;
4464 if (device->bCursorVisible)
4465 SetCursor(cursor);
4467 HeapFree(GetProcessHeap(), 0, mask_bits);
4470 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4471 device->cursorWidth = cursor_width;
4472 device->cursorHeight = cursor_height;
4473 device->xHotSpot = x_hotspot;
4474 device->yHotSpot = y_hotspot;
4476 return WINED3D_OK;
4479 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4480 int x_screen_space, int y_screen_space, DWORD flags)
4482 TRACE("device %p, x %d, y %d, flags %#x.\n",
4483 device, x_screen_space, y_screen_space, flags);
4485 device->xScreenSpace = x_screen_space;
4486 device->yScreenSpace = y_screen_space;
4488 if (device->hardwareCursor)
4490 POINT pt;
4492 GetCursorPos( &pt );
4493 if (x_screen_space == pt.x && y_screen_space == pt.y)
4494 return;
4495 SetCursorPos( x_screen_space, y_screen_space );
4497 /* Switch to the software cursor if position diverges from the hardware one. */
4498 GetCursorPos( &pt );
4499 if (x_screen_space != pt.x || y_screen_space != pt.y)
4501 if (device->bCursorVisible) SetCursor( NULL );
4502 DestroyCursor( device->hardwareCursor );
4503 device->hardwareCursor = 0;
4508 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4510 BOOL oldVisible = device->bCursorVisible;
4512 TRACE("device %p, show %#x.\n", device, show);
4515 * When ShowCursor is first called it should make the cursor appear at the OS's last
4516 * known cursor position.
4518 if (show && !oldVisible)
4520 POINT pt;
4521 GetCursorPos(&pt);
4522 device->xScreenSpace = pt.x;
4523 device->yScreenSpace = pt.y;
4526 if (device->hardwareCursor)
4528 device->bCursorVisible = show;
4529 if (show)
4530 SetCursor(device->hardwareCursor);
4531 else
4532 SetCursor(NULL);
4534 else if (device->cursor_texture)
4536 device->bCursorVisible = show;
4539 return oldVisible;
4542 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4544 struct wined3d_resource *resource, *cursor;
4546 TRACE("device %p.\n", device);
4548 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4550 TRACE("Checking resource %p for eviction.\n", resource);
4552 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4554 TRACE("Evicting %p.\n", resource);
4555 wined3d_cs_emit_unload_resource(device->cs, resource);
4560 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4561 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4562 wined3d_device_reset_cb callback, BOOL reset_state)
4564 struct wined3d_resource *resource, *cursor;
4565 struct wined3d_swapchain *swapchain;
4566 struct wined3d_view_desc view_desc;
4567 BOOL backbuffer_resized;
4568 HRESULT hr = WINED3D_OK;
4569 unsigned int i;
4571 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4572 device, swapchain_desc, mode, callback, reset_state);
4574 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4576 ERR("Failed to get the first implicit swapchain.\n");
4577 return WINED3DERR_INVALIDCALL;
4580 if (reset_state)
4582 if (device->logo_texture)
4584 wined3d_texture_decref(device->logo_texture);
4585 device->logo_texture = NULL;
4587 if (device->cursor_texture)
4589 wined3d_texture_decref(device->cursor_texture);
4590 device->cursor_texture = NULL;
4592 state_unbind_resources(&device->state);
4595 if (device->fb.render_targets)
4597 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4599 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4602 wined3d_device_set_depth_stencil_view(device, NULL);
4604 if (reset_state)
4606 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4608 TRACE("Enumerating resource %p.\n", resource);
4609 if (FAILED(hr = callback(resource)))
4610 return hr;
4614 TRACE("New params:\n");
4615 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4616 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4617 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4618 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4619 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4620 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4621 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4622 TRACE("device_window %p\n", swapchain_desc->device_window);
4623 TRACE("windowed %#x\n", swapchain_desc->windowed);
4624 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4625 if (swapchain_desc->enable_auto_depth_stencil)
4626 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4627 TRACE("flags %#x\n", swapchain_desc->flags);
4628 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4629 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4630 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4632 /* No special treatment of these parameters. Just store them */
4633 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4634 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4635 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4636 swapchain->desc.flags = swapchain_desc->flags;
4637 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4638 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4639 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4641 if (swapchain_desc->device_window
4642 && swapchain_desc->device_window != swapchain->desc.device_window)
4644 TRACE("Changing the device window from %p to %p.\n",
4645 swapchain->desc.device_window, swapchain_desc->device_window);
4646 swapchain->desc.device_window = swapchain_desc->device_window;
4647 swapchain->device_window = swapchain_desc->device_window;
4648 wined3d_swapchain_set_window(swapchain, NULL);
4651 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4652 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4654 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4655 || swapchain->reapply_mode || mode
4656 || (!swapchain_desc->windowed && backbuffer_resized))
4658 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4659 return hr;
4661 else if (!swapchain_desc->windowed)
4663 DWORD style = device->style;
4664 DWORD exStyle = device->exStyle;
4665 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4666 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4667 * Reset to clear up their mess. Guild Wars also loses the device during that.
4669 device->style = 0;
4670 device->exStyle = 0;
4671 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4672 swapchain_desc->backbuffer_width,
4673 swapchain_desc->backbuffer_height);
4674 device->style = style;
4675 device->exStyle = exStyle;
4678 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4679 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4680 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4681 return hr;
4683 if (device->auto_depth_stencil_view)
4685 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4686 device->auto_depth_stencil_view = NULL;
4688 if (swapchain->desc.enable_auto_depth_stencil)
4690 struct wined3d_resource_desc texture_desc;
4691 struct wined3d_texture *texture;
4692 DWORD flags = 0;
4694 TRACE("Creating the depth stencil buffer.\n");
4696 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4697 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4698 texture_desc.multisample_type = swapchain->desc.multisample_type;
4699 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4700 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4701 texture_desc.pool = WINED3D_POOL_DEFAULT;
4702 texture_desc.width = swapchain->desc.backbuffer_width;
4703 texture_desc.height = swapchain->desc.backbuffer_height;
4704 texture_desc.depth = 1;
4705 texture_desc.size = 0;
4707 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4708 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4710 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4711 device->device_parent, &texture_desc, flags, &texture)))
4713 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4714 return WINED3DERR_INVALIDCALL;
4717 view_desc.format_id = texture->resource.format->id;
4718 view_desc.flags = 0;
4719 view_desc.u.texture.level_idx = 0;
4720 view_desc.u.texture.level_count = 1;
4721 view_desc.u.texture.layer_idx = 0;
4722 view_desc.u.texture.layer_count = 1;
4723 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4724 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4725 wined3d_texture_decref(texture);
4726 if (FAILED(hr))
4728 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4729 return hr;
4732 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4735 if (device->back_buffer_view)
4737 wined3d_rendertarget_view_decref(device->back_buffer_view);
4738 device->back_buffer_view = NULL;
4740 if (swapchain->desc.backbuffer_count)
4742 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4744 view_desc.format_id = back_buffer->format->id;
4745 view_desc.flags = 0;
4746 view_desc.u.texture.level_idx = 0;
4747 view_desc.u.texture.level_count = 1;
4748 view_desc.u.texture.layer_idx = 0;
4749 view_desc.u.texture.layer_count = 1;
4750 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4751 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4753 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4754 return hr;
4758 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4760 if (reset_state)
4762 TRACE("Resetting stateblock.\n");
4763 if (device->recording)
4765 wined3d_stateblock_decref(device->recording);
4766 device->recording = NULL;
4768 wined3d_cs_emit_reset_state(device->cs);
4769 state_cleanup(&device->state);
4771 if (device->d3d_initialized)
4772 wined3d_device_delete_opengl_contexts(device);
4774 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4775 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4776 device->update_state = &device->state;
4778 device_init_swapchain_state(device, swapchain);
4780 else if (device->back_buffer_view)
4782 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4783 struct wined3d_state *state = &device->state;
4785 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4787 /* Note the min_z / max_z is not reset. */
4788 state->viewport.x = 0;
4789 state->viewport.y = 0;
4790 state->viewport.width = view->width;
4791 state->viewport.height = view->height;
4792 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4794 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4795 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4798 if (device->d3d_initialized)
4800 if (reset_state)
4801 hr = create_primary_opengl_context(device, swapchain);
4802 swapchain_update_swap_interval(swapchain);
4805 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4806 * first use
4808 return hr;
4811 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4813 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4815 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4817 return WINED3D_OK;
4821 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4822 struct wined3d_device_creation_parameters *parameters)
4824 TRACE("device %p, parameters %p.\n", device, parameters);
4826 *parameters = device->create_parms;
4829 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4831 TRACE("device %p.\n", device);
4833 return device->wined3d;
4836 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4837 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4839 struct wined3d_swapchain *swapchain;
4841 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4842 device, swapchain_idx, flags, ramp);
4844 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4845 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4848 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4849 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4851 struct wined3d_swapchain *swapchain;
4853 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4854 device, swapchain_idx, ramp);
4856 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4857 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4860 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4862 TRACE("device %p, resource %p.\n", device, resource);
4864 list_add_head(&device->resources, &resource->resource_list_entry);
4867 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4869 TRACE("device %p, resource %p.\n", device, resource);
4871 list_remove(&resource->resource_list_entry);
4874 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4876 enum wined3d_resource_type type = resource->type;
4877 struct wined3d_rendertarget_view *rtv;
4878 unsigned int i;
4880 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4882 if (device->d3d_initialized)
4884 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4886 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4887 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4890 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4891 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4894 switch (type)
4896 case WINED3D_RTYPE_TEXTURE_2D:
4897 case WINED3D_RTYPE_TEXTURE_3D:
4898 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4900 struct wined3d_texture *texture = texture_from_resource(resource);
4902 if (device->state.textures[i] == texture)
4904 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4905 device->state.textures[i] = NULL;
4908 if (device->recording && device->update_state->textures[i] == texture)
4910 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4911 texture, device->recording, i);
4912 device->update_state->textures[i] = NULL;
4915 break;
4917 case WINED3D_RTYPE_BUFFER:
4919 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4921 for (i = 0; i < MAX_STREAMS; ++i)
4923 if (device->state.streams[i].buffer == buffer)
4925 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4926 device->state.streams[i].buffer = NULL;
4929 if (device->recording && device->update_state->streams[i].buffer == buffer)
4931 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4932 buffer, device->recording, i);
4933 device->update_state->streams[i].buffer = NULL;
4937 if (device->state.index_buffer == buffer)
4939 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4940 device->state.index_buffer = NULL;
4943 if (device->recording && device->update_state->index_buffer == buffer)
4945 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4946 buffer, device->recording);
4947 device->update_state->index_buffer = NULL;
4950 break;
4952 default:
4953 break;
4956 /* Remove the resource from the resourceStore */
4957 device_resource_remove(device, resource);
4959 TRACE("Resource released.\n");
4962 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4964 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4966 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4969 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4970 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4971 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4973 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4974 const struct fragment_pipeline *fragment_pipeline;
4975 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4976 unsigned int i;
4977 HRESULT hr;
4979 device->ref = 1;
4980 device->wined3d = wined3d;
4981 wined3d_incref(device->wined3d);
4982 device->adapter = wined3d->adapter_count ? adapter : NULL;
4983 device->device_parent = device_parent;
4984 list_init(&device->resources);
4985 list_init(&device->shaders);
4986 device->surface_alignment = surface_alignment;
4988 /* Save the creation parameters. */
4989 device->create_parms.adapter_idx = adapter_idx;
4990 device->create_parms.device_type = device_type;
4991 device->create_parms.focus_window = focus_window;
4992 device->create_parms.flags = flags;
4994 device->shader_backend = adapter->shader_backend;
4996 vertex_pipeline = adapter->vertex_pipe;
4998 fragment_pipeline = adapter->fragment_pipe;
5000 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5002 if (vertex_pipeline->vp_states && fragment_pipeline->states
5003 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5004 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5005 fragment_pipeline, misc_state_template)))
5007 ERR("Failed to compile state table, hr %#x.\n", hr);
5008 wine_rb_destroy(&device->samplers, NULL, NULL);
5009 wined3d_decref(device->wined3d);
5010 return hr;
5013 device->blitter = adapter->blitter;
5015 state_init(&device->state, &device->fb, &adapter->gl_info,
5016 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5017 device->update_state = &device->state;
5019 if (!(device->cs = wined3d_cs_create(device)))
5021 WARN("Failed to create command stream.\n");
5022 state_cleanup(&device->state);
5023 hr = E_FAIL;
5024 goto err;
5027 return WINED3D_OK;
5029 err:
5030 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5032 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5034 wine_rb_destroy(&device->samplers, NULL, NULL);
5035 wined3d_decref(device->wined3d);
5036 return hr;
5039 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5041 DWORD rep = device->StateTable[state].representative;
5042 struct wined3d_context *context;
5043 DWORD idx;
5044 BYTE shift;
5045 UINT i;
5047 if (STATE_IS_COMPUTE(state))
5049 for (i = 0; i < device->context_count; ++i)
5050 context_invalidate_compute_state(device->contexts[i], state);
5051 return;
5054 for (i = 0; i < device->context_count; ++i)
5056 context = device->contexts[i];
5057 if(isStateDirty(context, rep)) continue;
5059 context->dirtyArray[context->numDirtyEntries++] = rep;
5060 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5061 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5062 context->isStateDirty[idx] |= (1u << shift);
5066 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5067 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5069 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5071 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5072 window, message, wparam, lparam);
5073 if (unicode)
5074 return DefWindowProcW(window, message, wparam, lparam);
5075 else
5076 return DefWindowProcA(window, message, wparam, lparam);
5079 if (message == WM_DESTROY)
5081 TRACE("unregister window %p.\n", window);
5082 wined3d_unregister_window(window);
5084 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5085 ERR("Window %p is not the focus window for device %p.\n", window, device);
5087 else if (message == WM_DISPLAYCHANGE)
5089 device->device_parent->ops->mode_changed(device->device_parent);
5091 else if (message == WM_ACTIVATEAPP)
5093 UINT i;
5095 for (i = 0; i < device->swapchain_count; i++)
5096 wined3d_swapchain_activate(device->swapchains[i], wparam);
5098 device->device_parent->ops->activate(device->device_parent, wparam);
5100 else if (message == WM_SYSCOMMAND)
5102 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5104 if (unicode)
5105 DefWindowProcW(window, message, wparam, lparam);
5106 else
5107 DefWindowProcA(window, message, wparam, lparam);
5111 if (unicode)
5112 return CallWindowProcW(proc, window, message, wparam, lparam);
5113 else
5114 return CallWindowProcA(proc, window, message, wparam, lparam);