wined3d: Allow copying 3D texture resources.
[wine.git] / dlls / wined3d / device.c
blob8c3cd160209c30cc8e52e9a7939d8d9c7fb9c8dc
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);
38 WINE_DECLARE_DEBUG_CHANNEL(winediag);
40 /* Define the default light parameters as specified by MSDN. */
41 const struct wined3d_light WINED3D_default_light =
43 WINED3D_LIGHT_DIRECTIONAL, /* Type */
44 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
46 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
47 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
48 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
49 0.0f, /* Range */
50 0.0f, /* Falloff */
51 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
52 0.0f, /* Theta */
53 0.0f /* Phi */
56 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
57 * actually have the same values in GL and D3D. */
58 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
60 switch (primitive_type)
62 case WINED3D_PT_POINTLIST:
63 return GL_POINTS;
65 case WINED3D_PT_LINELIST:
66 return GL_LINES;
68 case WINED3D_PT_LINESTRIP:
69 return GL_LINE_STRIP;
71 case WINED3D_PT_TRIANGLELIST:
72 return GL_TRIANGLES;
74 case WINED3D_PT_TRIANGLESTRIP:
75 return GL_TRIANGLE_STRIP;
77 case WINED3D_PT_TRIANGLEFAN:
78 return GL_TRIANGLE_FAN;
80 case WINED3D_PT_LINELIST_ADJ:
81 return GL_LINES_ADJACENCY_ARB;
83 case WINED3D_PT_LINESTRIP_ADJ:
84 return GL_LINE_STRIP_ADJACENCY_ARB;
86 case WINED3D_PT_TRIANGLELIST_ADJ:
87 return GL_TRIANGLES_ADJACENCY_ARB;
89 case WINED3D_PT_TRIANGLESTRIP_ADJ:
90 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
92 default:
93 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
94 case WINED3D_PT_UNDEFINED:
95 return ~0u;
99 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
101 switch (primitive_type)
103 case GL_POINTS:
104 return WINED3D_PT_POINTLIST;
106 case GL_LINES:
107 return WINED3D_PT_LINELIST;
109 case GL_LINE_STRIP:
110 return WINED3D_PT_LINESTRIP;
112 case GL_TRIANGLES:
113 return WINED3D_PT_TRIANGLELIST;
115 case GL_TRIANGLE_STRIP:
116 return WINED3D_PT_TRIANGLESTRIP;
118 case GL_TRIANGLE_FAN:
119 return WINED3D_PT_TRIANGLEFAN;
121 case GL_LINES_ADJACENCY_ARB:
122 return WINED3D_PT_LINELIST_ADJ;
124 case GL_LINE_STRIP_ADJACENCY_ARB:
125 return WINED3D_PT_LINESTRIP_ADJ;
127 case GL_TRIANGLES_ADJACENCY_ARB:
128 return WINED3D_PT_TRIANGLELIST_ADJ;
130 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
131 return WINED3D_PT_TRIANGLESTRIP_ADJ;
133 default:
134 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
135 case ~0u:
136 return WINED3D_PT_UNDEFINED;
140 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
142 struct wined3d_context **new_array;
144 TRACE("Adding context %p.\n", context);
146 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
147 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
148 sizeof(*new_array) * (device->context_count + 1));
150 if (!new_array)
152 ERR("Failed to grow the context array.\n");
153 return FALSE;
156 new_array[device->context_count++] = context;
157 device->contexts = new_array;
158 return TRUE;
161 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
163 struct wined3d_context **new_array;
164 BOOL found = FALSE;
165 UINT i;
167 TRACE("Removing context %p.\n", context);
169 for (i = 0; i < device->context_count; ++i)
171 if (device->contexts[i] == context)
173 found = TRUE;
174 break;
178 if (!found)
180 ERR("Context %p doesn't exist in context array.\n", context);
181 return;
184 if (!--device->context_count)
186 HeapFree(GetProcessHeap(), 0, device->contexts);
187 device->contexts = NULL;
188 return;
191 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
192 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
193 if (!new_array)
195 ERR("Failed to shrink context array. Oh well.\n");
196 return;
199 device->contexts = new_array;
202 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
204 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
205 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
207 /* partial draw rect */
208 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
209 return FALSE;
211 /* partial clear rect */
212 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
213 || clear_rect->right < width || clear_rect->bottom < height))
214 return FALSE;
216 return TRUE;
219 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
220 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
221 float depth, DWORD stencil)
223 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
224 struct wined3d_surface *target = rtv ? wined3d_rendertarget_view_get_surface(rtv) : NULL;
225 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
226 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
227 const struct wined3d_state *state = &device->cs->state;
228 const struct wined3d_gl_info *gl_info;
229 UINT drawable_width, drawable_height;
230 struct wined3d_color corrected_color;
231 struct wined3d_context *context;
232 GLbitfield clear_mask = 0;
233 BOOL render_offscreen;
234 unsigned int i;
236 if (target)
237 context = context_acquire(device, target->container, rtv->sub_resource_idx);
238 else
239 context = context_acquire(device, NULL, 0);
240 if (!context->valid)
242 context_release(context);
243 WARN("Invalid context, skipping clear.\n");
244 return;
246 gl_info = context->gl_info;
248 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
249 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
250 * for the cleared parts, and the untouched parts.
252 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
253 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
254 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
255 * checking all this if the dest surface is in the drawable anyway. */
256 for (i = 0; i < rt_count; ++i)
258 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
260 if (rtv && rtv->format->id != WINED3DFMT_NULL)
262 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
264 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
265 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
266 else
267 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
271 if (target)
273 render_offscreen = context->render_offscreen;
274 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
276 else
278 render_offscreen = TRUE;
279 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
280 depth_stencil->texture_level);
281 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
282 depth_stencil->texture_level);
285 if (depth_stencil && render_offscreen)
286 wined3d_texture_prepare_location(depth_stencil->container,
287 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
289 if (flags & WINED3DCLEAR_ZBUFFER)
291 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
293 wined3d_texture_load_location(depth_stencil->container,
294 dsv->sub_resource_idx, context, location);
297 if (!context_apply_clear_state(context, state, rt_count, fb))
299 context_release(context);
300 WARN("Failed to apply clear state, skipping clear.\n");
301 return;
304 /* Only set the values up once, as they are not changing. */
305 if (flags & WINED3DCLEAR_STENCIL)
307 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
309 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
310 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
312 gl_info->gl_ops.gl.p_glStencilMask(~0U);
313 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
314 gl_info->gl_ops.gl.p_glClearStencil(stencil);
315 checkGLcall("glClearStencil");
316 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
319 if (flags & WINED3DCLEAR_ZBUFFER)
321 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
323 wined3d_texture_validate_location(depth_stencil->container, dsv->sub_resource_idx, location);
324 wined3d_texture_invalidate_location(depth_stencil->container, dsv->sub_resource_idx, ~location);
326 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
327 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
328 gl_info->gl_ops.gl.p_glClearDepth(depth);
329 checkGLcall("glClearDepth");
330 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
333 if (flags & WINED3DCLEAR_TARGET)
335 for (i = 0; i < rt_count; ++i)
337 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
338 struct wined3d_texture *texture;
340 if (!rtv)
341 continue;
343 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
345 FIXME("Not supported on buffer resources.\n");
346 continue;
349 texture = texture_from_resource(rtv->resource);
350 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
351 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
354 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
356 if (rt_count > 1)
357 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
358 "support, this might cause graphical issues.\n");
360 corrected_color.r = color->r < wined3d_srgb_const1[0]
361 ? color->r * wined3d_srgb_const0[3]
362 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
363 - wined3d_srgb_const0[2];
364 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
365 corrected_color.g = color->g < wined3d_srgb_const1[0]
366 ? color->g * wined3d_srgb_const0[3]
367 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
368 - wined3d_srgb_const0[2];
369 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
370 corrected_color.b = color->b < wined3d_srgb_const1[0]
371 ? color->b * wined3d_srgb_const0[3]
372 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
373 - wined3d_srgb_const0[2];
374 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
375 color = &corrected_color;
378 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
379 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
380 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
382 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
383 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
384 checkGLcall("glClearColor");
385 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
388 if (!rect_count)
390 if (render_offscreen)
392 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
393 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
395 else
397 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
398 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
400 checkGLcall("glScissor");
401 gl_info->gl_ops.gl.p_glClear(clear_mask);
402 checkGLcall("glClear");
404 else
406 RECT current_rect;
408 /* Now process each rect in turn. */
409 for (i = 0; i < rect_count; ++i)
411 /* Note that GL uses lower left, width/height. */
412 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
414 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
415 wine_dbgstr_rect(&clear_rect[i]),
416 wine_dbgstr_rect(&current_rect));
418 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
419 * The rectangle is not cleared, no error is returned, but further rectangles are
420 * still cleared if they are valid. */
421 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
423 TRACE("Rectangle with negative dimensions, ignoring.\n");
424 continue;
427 if (render_offscreen)
429 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
430 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
432 else
434 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
435 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
437 checkGLcall("glScissor");
439 gl_info->gl_ops.gl.p_glClear(clear_mask);
440 checkGLcall("glClear");
444 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
445 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
446 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
448 context_release(context);
451 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
453 ULONG refcount = InterlockedIncrement(&device->ref);
455 TRACE("%p increasing refcount to %u.\n", device, refcount);
457 return refcount;
460 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
462 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
464 ERR("Leftover sampler %p.\n", sampler);
467 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
469 ULONG refcount = InterlockedDecrement(&device->ref);
471 TRACE("%p decreasing refcount to %u.\n", device, refcount);
473 if (!refcount)
475 UINT i;
477 wined3d_cs_destroy(device->cs);
479 if (device->recording && wined3d_stateblock_decref(device->recording))
480 ERR("Something's still holding the recording stateblock.\n");
481 device->recording = NULL;
483 state_cleanup(&device->state);
485 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
487 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
488 device->multistate_funcs[i] = NULL;
491 if (!list_empty(&device->resources))
493 struct wined3d_resource *resource;
495 ERR("Device released with resources still bound.\n");
497 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
499 ERR("Leftover resource %p with type %s (%#x).\n",
500 resource, debug_d3dresourcetype(resource->type), resource->type);
504 if (device->contexts)
505 ERR("Context array not freed!\n");
506 if (device->hardwareCursor)
507 DestroyCursor(device->hardwareCursor);
508 device->hardwareCursor = 0;
510 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
512 wined3d_decref(device->wined3d);
513 device->wined3d = NULL;
514 HeapFree(GetProcessHeap(), 0, device);
515 TRACE("Freed device %p.\n", device);
518 return refcount;
521 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
523 TRACE("device %p.\n", device);
525 return device->swapchain_count;
528 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
530 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
532 if (swapchain_idx >= device->swapchain_count)
534 WARN("swapchain_idx %u >= swapchain_count %u.\n",
535 swapchain_idx, device->swapchain_count);
536 return NULL;
539 return device->swapchains[swapchain_idx];
542 static void device_load_logo(struct wined3d_device *device, const char *filename)
544 struct wined3d_color_key color_key;
545 struct wined3d_resource_desc desc;
546 HBITMAP hbm;
547 BITMAP bm;
548 HRESULT hr;
549 HDC dcb = NULL, dcs = NULL;
551 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
553 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
554 return;
556 GetObjectA(hbm, sizeof(BITMAP), &bm);
558 if (!(dcb = CreateCompatibleDC(NULL)))
559 goto out;
560 SelectObject(dcb, hbm);
562 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
563 desc.format = WINED3DFMT_B5G6R5_UNORM;
564 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
565 desc.multisample_quality = 0;
566 desc.usage = WINED3DUSAGE_DYNAMIC;
567 desc.pool = WINED3D_POOL_DEFAULT;
568 desc.width = bm.bmWidth;
569 desc.height = bm.bmHeight;
570 desc.depth = 1;
571 desc.size = 0;
572 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1,
573 WINED3D_TEXTURE_CREATE_MAPPABLE | WINED3D_TEXTURE_CREATE_GET_DC,
574 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
576 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
577 goto out;
580 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
582 wined3d_texture_decref(device->logo_texture);
583 device->logo_texture = NULL;
584 goto out;
586 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
587 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
589 color_key.color_space_low_value = 0;
590 color_key.color_space_high_value = 0;
591 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
593 out:
594 if (dcb) DeleteDC(dcb);
595 if (hbm) DeleteObject(hbm);
598 /* Context activation is done by the caller. */
599 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
601 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
602 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
603 unsigned int i;
604 DWORD color;
606 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
607 color = 0x000000ff;
608 else
609 color = 0x00000000;
611 /* Under DirectX you can sample even if no texture is bound, whereas
612 * OpenGL will only allow that when a valid texture is bound.
613 * We emulate this by creating dummy textures and binding them
614 * to each texture stage when the currently set D3D texture is NULL. */
615 context_active_texture(context, gl_info, 0);
617 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d);
618 checkGLcall("glGenTextures");
619 TRACE("Dummy 2D texture given name %u.\n", device->dummy_textures.tex_2d);
621 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
622 checkGLcall("glBindTexture");
624 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
625 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
626 checkGLcall("glTexImage2D");
628 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
630 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_rect);
631 checkGLcall("glGenTextures");
632 TRACE("Dummy rectangle texture given name %u.\n", device->dummy_textures.tex_rect);
634 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
635 checkGLcall("glBindTexture");
637 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
638 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
639 checkGLcall("glTexImage2D");
642 if (gl_info->supported[EXT_TEXTURE3D])
644 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_3d);
645 checkGLcall("glGenTextures");
646 TRACE("Dummy 3D texture given name %u.\n", device->dummy_textures.tex_3d);
648 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
649 checkGLcall("glBindTexture");
651 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
652 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
653 checkGLcall("glTexImage3D");
656 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
658 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube);
659 checkGLcall("glGenTextures");
660 TRACE("Dummy cube texture given name %u.\n", device->dummy_textures.tex_cube);
662 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
663 checkGLcall("glBindTexture");
665 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
667 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
668 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
669 checkGLcall("glTexImage2D");
673 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
675 DWORD cube_array_data[6];
677 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube_array);
678 checkGLcall("glGenTextures");
679 TRACE("Dummy cube array texture given name %u.\n", device->dummy_textures.tex_cube_array);
681 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, device->dummy_textures.tex_cube_array);
682 checkGLcall("glBindTexture");
684 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
685 cube_array_data[i] = color;
686 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
687 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
688 checkGLcall("glTexImage3D");
691 if (gl_info->supported[EXT_TEXTURE_ARRAY])
693 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d_array);
694 checkGLcall("glGenTextures");
695 TRACE("Dummy 2D array texture given name %u.\n", device->dummy_textures.tex_2d_array);
697 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
698 checkGLcall("glBindTexture");
700 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
701 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
702 checkGLcall("glTexImage3D");
705 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
707 GLuint buffer;
709 GL_EXTCALL(glGenBuffers(1, &buffer));
710 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
711 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
712 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
713 checkGLcall("Create buffer object");
715 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_buffer);
716 checkGLcall("glGenTextures");
717 TRACE("Dummy buffer texture given name %u.\n", device->dummy_textures.tex_buffer);
719 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, device->dummy_textures.tex_buffer);
720 checkGLcall("glBindTexture");
721 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
722 checkGLcall("glTexBuffer");
724 GL_EXTCALL(glDeleteBuffers(1, &buffer));
725 checkGLcall("glDeleteBuffers");
728 context_bind_dummy_textures(device, context);
731 /* Context activation is done by the caller. */
732 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
734 const struct wined3d_gl_info *gl_info = context->gl_info;
736 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
737 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_buffer);
739 if (gl_info->supported[EXT_TEXTURE_ARRAY])
740 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d_array);
742 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
743 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube_array);
745 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
746 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube);
748 if (gl_info->supported[EXT_TEXTURE3D])
749 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_3d);
751 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
752 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_rect);
754 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d);
756 checkGLcall("Delete dummy textures");
758 memset(&device->dummy_textures, 0, sizeof(device->dummy_textures));
761 /* Context activation is done by the caller. */
762 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
764 struct wined3d_sampler_desc desc;
765 HRESULT hr;
767 desc.address_u = WINED3D_TADDRESS_WRAP;
768 desc.address_v = WINED3D_TADDRESS_WRAP;
769 desc.address_w = WINED3D_TADDRESS_WRAP;
770 memset(desc.border_color, 0, sizeof(desc.border_color));
771 desc.mag_filter = WINED3D_TEXF_POINT;
772 desc.min_filter = WINED3D_TEXF_POINT;
773 desc.mip_filter = WINED3D_TEXF_NONE;
774 desc.lod_bias = 0.0f;
775 desc.min_lod = -1000.0f;
776 desc.max_lod = 1000.0f;
777 desc.mip_base_level = 0;
778 desc.max_anisotropy = 1;
779 desc.compare = FALSE;
780 desc.comparison_func = WINED3D_CMP_NEVER;
781 desc.srgb_decode = TRUE;
783 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
784 * instructions allow access to resources without using samplers.
785 * In GLSL, resources are always accessed through sampler or image variables. The default
786 * sampler object is used to emulate the direct resource access when there is no sampler state
787 * to use.
789 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &device->default_sampler)))
791 ERR("Failed to create default sampler, hr %#x.\n", hr);
792 device->default_sampler = NULL;
795 /* In D3D10+, a NULL sampler maps to the default sampler state. */
796 desc.address_u = WINED3D_TADDRESS_CLAMP;
797 desc.address_v = WINED3D_TADDRESS_CLAMP;
798 desc.address_w = WINED3D_TADDRESS_CLAMP;
799 desc.mag_filter = WINED3D_TEXF_LINEAR;
800 desc.min_filter = WINED3D_TEXF_LINEAR;
801 desc.mip_filter = WINED3D_TEXF_LINEAR;
802 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &device->null_sampler)))
804 ERR("Failed to create null sampler, hr %#x.\n", hr);
805 device->null_sampler = NULL;
809 /* Context activation is done by the caller. */
810 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
812 wined3d_sampler_decref(device->default_sampler);
813 device->default_sampler = NULL;
814 wined3d_sampler_decref(device->null_sampler);
815 device->null_sampler = NULL;
818 static LONG fullscreen_style(LONG style)
820 /* Make sure the window is managed, otherwise we won't get keyboard input. */
821 style |= WS_POPUP | WS_SYSMENU;
822 style &= ~(WS_CAPTION | WS_THICKFRAME);
824 return style;
827 static LONG fullscreen_exstyle(LONG exstyle)
829 /* Filter out window decorations. */
830 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
832 return exstyle;
835 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
837 BOOL filter_messages;
838 LONG style, exstyle;
840 TRACE("Setting up window %p for fullscreen mode.\n", window);
842 if (device->style || device->exStyle)
844 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
845 window, device->style, device->exStyle);
848 device->style = GetWindowLongW(window, GWL_STYLE);
849 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
851 style = fullscreen_style(device->style);
852 exstyle = fullscreen_exstyle(device->exStyle);
854 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
855 device->style, device->exStyle, style, exstyle);
857 filter_messages = device->filter_messages;
858 device->filter_messages = TRUE;
860 SetWindowLongW(window, GWL_STYLE, style);
861 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
862 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
864 device->filter_messages = filter_messages;
867 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
868 const RECT *window_rect)
870 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
871 BOOL filter_messages;
872 LONG style, exstyle;
873 RECT rect = {0};
875 if (!device->style && !device->exStyle)
876 return;
878 style = GetWindowLongW(window, GWL_STYLE);
879 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
881 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
882 * application, and we want to ignore them in the test below, since it's
883 * not the application's fault that they changed. Additionally, we want to
884 * preserve the current status of these flags (i.e. don't restore them) to
885 * more closely emulate the behavior of Direct3D, which leaves these flags
886 * alone when returning to windowed mode. */
887 device->style ^= (device->style ^ style) & WS_VISIBLE;
888 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
890 TRACE("Restoring window style of window %p to %08x, %08x.\n",
891 window, device->style, device->exStyle);
893 filter_messages = device->filter_messages;
894 device->filter_messages = TRUE;
896 /* Only restore the style if the application didn't modify it during the
897 * fullscreen phase. Some applications change it before calling Reset()
898 * when switching between windowed and fullscreen modes (HL2), some
899 * depend on the original style (Eve Online). */
900 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
902 SetWindowLongW(window, GWL_STYLE, device->style);
903 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
906 if (window_rect)
907 rect = *window_rect;
908 else
909 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
910 SetWindowPos(window, 0, rect.left, rect.top,
911 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
913 device->filter_messages = filter_messages;
915 /* Delete the old values. */
916 device->style = 0;
917 device->exStyle = 0;
920 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
922 TRACE("device %p, window %p.\n", device, window);
924 if (!wined3d_register_window(window, device))
926 ERR("Failed to register window %p.\n", window);
927 return E_FAIL;
930 InterlockedExchangePointer((void **)&device->focus_window, window);
931 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
933 return WINED3D_OK;
936 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
938 TRACE("device %p.\n", device);
940 if (device->focus_window) wined3d_unregister_window(device->focus_window);
941 InterlockedExchangePointer((void **)&device->focus_window, NULL);
944 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
946 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
947 unsigned int i;
949 if (device->fb.render_targets)
951 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
953 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
955 if (device->back_buffer_view)
956 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
959 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
960 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
963 static void wined3d_device_delete_opengl_contexts_cs(void *object)
965 struct wined3d_resource *resource, *cursor;
966 struct wined3d_device *device = object;
967 struct wined3d_context *context;
968 struct wined3d_shader *shader;
970 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
972 TRACE("Unloading resource %p.\n", resource);
973 wined3d_cs_emit_unload_resource(device->cs, resource);
976 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
978 device->shader_backend->shader_destroy(shader);
981 context = context_acquire(device, NULL, 0);
982 device->blitter->ops->blitter_destroy(device->blitter, context);
983 device->shader_backend->shader_free_private(device);
984 destroy_dummy_textures(device, context);
985 destroy_default_samplers(device, context);
986 context_release(context);
988 while (device->context_count)
990 if (device->contexts[0]->swapchain)
991 swapchain_destroy_contexts(device->contexts[0]->swapchain);
992 else
993 context_destroy(device, device->contexts[0]);
997 static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
999 wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
1002 static void wined3d_device_create_primary_opengl_context_cs(void *object)
1004 struct wined3d_device *device = object;
1005 struct wined3d_swapchain *swapchain;
1006 struct wined3d_context *context;
1007 struct wined3d_texture *target;
1008 HRESULT hr;
1010 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1011 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1013 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1014 return;
1017 if (!(device->blitter = wined3d_cpu_blitter_create()))
1019 ERR("Failed to create CPU blitter.\n");
1020 device->shader_backend->shader_free_private(device);
1021 return;
1023 wined3d_ffp_blitter_create(&device->blitter, &device->adapter->gl_info);
1024 wined3d_arbfp_blitter_create(&device->blitter, device);
1025 wined3d_fbo_blitter_create(&device->blitter, &device->adapter->gl_info);
1027 swapchain = device->swapchains[0];
1028 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1029 context = context_acquire(device, target, 0);
1030 create_dummy_textures(device, context);
1031 create_default_samplers(device, context);
1032 context_release(context);
1035 static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1037 wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1038 if (!device->swapchains[0]->num_contexts)
1039 return E_FAIL;
1041 return WINED3D_OK;
1044 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1045 struct wined3d_swapchain_desc *swapchain_desc)
1047 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1048 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1049 struct wined3d_swapchain *swapchain = NULL;
1050 DWORD clear_flags = 0;
1051 HRESULT hr;
1053 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1055 if (device->d3d_initialized)
1056 return WINED3DERR_INVALIDCALL;
1057 if (device->wined3d->flags & WINED3D_NO3D)
1058 return WINED3DERR_INVALIDCALL;
1060 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
1061 return E_OUTOFMEMORY;
1063 /* Setup the implicit swapchain. This also initializes a context. */
1064 TRACE("Creating implicit swapchain\n");
1065 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1066 swapchain_desc, &swapchain);
1067 if (FAILED(hr))
1069 WARN("Failed to create implicit swapchain\n");
1070 goto err_out;
1073 if (swapchain_desc->backbuffer_count)
1075 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1076 struct wined3d_view_desc view_desc;
1078 view_desc.format_id = back_buffer->format->id;
1079 view_desc.flags = 0;
1080 view_desc.u.texture.level_idx = 0;
1081 view_desc.u.texture.level_count = 1;
1082 view_desc.u.texture.layer_idx = 0;
1083 view_desc.u.texture.layer_count = 1;
1084 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1085 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1087 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1088 goto err_out;
1092 device->swapchain_count = 1;
1093 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1095 ERR("Out of memory!\n");
1096 goto err_out;
1098 device->swapchains[0] = swapchain;
1100 if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1101 goto err_out;
1102 device_init_swapchain_state(device, swapchain);
1104 device->contexts[0]->last_was_rhw = 0;
1106 TRACE("All defaults now set up, leaving 3D init.\n");
1108 /* Clear the screen */
1109 if (swapchain->back_buffers && swapchain->back_buffers[0])
1110 clear_flags |= WINED3DCLEAR_TARGET;
1111 if (swapchain_desc->enable_auto_depth_stencil)
1112 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1113 if (clear_flags)
1114 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1116 device->d3d_initialized = TRUE;
1118 if (wined3d_settings.logo)
1119 device_load_logo(device, wined3d_settings.logo);
1120 return WINED3D_OK;
1122 err_out:
1123 HeapFree(GetProcessHeap(), 0, device->swapchains);
1124 device->swapchain_count = 0;
1125 if (device->back_buffer_view)
1126 wined3d_rendertarget_view_decref(device->back_buffer_view);
1127 if (swapchain)
1128 wined3d_swapchain_decref(swapchain);
1129 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1131 return hr;
1134 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1135 struct wined3d_swapchain_desc *swapchain_desc)
1137 struct wined3d_swapchain *swapchain = NULL;
1138 HRESULT hr;
1140 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1142 /* Setup the implicit swapchain */
1143 TRACE("Creating implicit swapchain\n");
1144 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1145 swapchain_desc, &swapchain);
1146 if (FAILED(hr))
1148 WARN("Failed to create implicit swapchain\n");
1149 goto err_out;
1152 device->swapchain_count = 1;
1153 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1155 ERR("Out of memory!\n");
1156 goto err_out;
1158 device->swapchains[0] = swapchain;
1159 return WINED3D_OK;
1161 err_out:
1162 wined3d_swapchain_decref(swapchain);
1163 return hr;
1166 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1168 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1170 wined3d_sampler_decref(sampler);
1173 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1175 UINT i;
1177 TRACE("device %p.\n", device);
1179 if (!device->d3d_initialized)
1180 return WINED3DERR_INVALIDCALL;
1182 if (device->logo_texture)
1183 wined3d_texture_decref(device->logo_texture);
1184 if (device->cursor_texture)
1185 wined3d_texture_decref(device->cursor_texture);
1187 state_unbind_resources(&device->state);
1189 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1191 wined3d_device_delete_opengl_contexts(device);
1193 if (device->fb.depth_stencil)
1195 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1197 TRACE("Releasing depth/stencil view %p.\n", view);
1199 device->fb.depth_stencil = NULL;
1200 wined3d_rendertarget_view_decref(view);
1203 if (device->auto_depth_stencil_view)
1205 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1207 device->auto_depth_stencil_view = NULL;
1208 if (wined3d_rendertarget_view_decref(view))
1209 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1212 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
1214 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1216 if (device->back_buffer_view)
1218 wined3d_rendertarget_view_decref(device->back_buffer_view);
1219 device->back_buffer_view = NULL;
1222 for (i = 0; i < device->swapchain_count; ++i)
1224 TRACE("Releasing the implicit swapchain %u.\n", i);
1225 if (wined3d_swapchain_decref(device->swapchains[i]))
1226 FIXME("Something's still holding the implicit swapchain.\n");
1229 HeapFree(GetProcessHeap(), 0, device->swapchains);
1230 device->swapchains = NULL;
1231 device->swapchain_count = 0;
1233 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1234 device->fb.render_targets = NULL;
1236 device->d3d_initialized = FALSE;
1238 return WINED3D_OK;
1241 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1243 unsigned int i;
1245 for (i = 0; i < device->swapchain_count; ++i)
1247 TRACE("Releasing the implicit swapchain %u.\n", i);
1248 if (wined3d_swapchain_decref(device->swapchains[i]))
1249 FIXME("Something's still holding the implicit swapchain.\n");
1252 HeapFree(GetProcessHeap(), 0, device->swapchains);
1253 device->swapchains = NULL;
1254 device->swapchain_count = 0;
1255 return WINED3D_OK;
1258 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1259 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1260 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1262 * There is no way to deactivate thread safety once it is enabled.
1264 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1266 TRACE("device %p.\n", device);
1268 /* For now just store the flag (needed in case of ddraw). */
1269 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1272 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1274 TRACE("device %p.\n", device);
1276 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1277 wine_dbgstr_longlong(device->adapter->vram_bytes),
1278 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1279 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1281 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1284 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1285 struct wined3d_buffer *buffer, UINT offset)
1287 struct wined3d_stream_output *stream;
1288 struct wined3d_buffer *prev_buffer;
1290 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1292 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1294 WARN("Invalid stream output %u.\n", idx);
1295 return;
1298 stream = &device->update_state->stream_output[idx];
1299 prev_buffer = stream->buffer;
1301 if (buffer)
1302 wined3d_buffer_incref(buffer);
1303 stream->buffer = buffer;
1304 stream->offset = offset;
1305 if (!device->recording)
1306 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1307 if (prev_buffer)
1308 wined3d_buffer_decref(prev_buffer);
1311 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1312 UINT idx, UINT *offset)
1314 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1316 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1318 WARN("Invalid stream output %u.\n", idx);
1319 return NULL;
1322 if (offset)
1323 *offset = device->state.stream_output[idx].offset;
1324 return device->state.stream_output[idx].buffer;
1327 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1328 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1330 struct wined3d_stream_state *stream;
1331 struct wined3d_buffer *prev_buffer;
1333 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1334 device, stream_idx, buffer, offset, stride);
1336 if (stream_idx >= MAX_STREAMS)
1338 WARN("Stream index %u out of range.\n", stream_idx);
1339 return WINED3DERR_INVALIDCALL;
1341 else if (offset & 0x3)
1343 WARN("Offset %u is not 4 byte aligned.\n", offset);
1344 return WINED3DERR_INVALIDCALL;
1347 stream = &device->update_state->streams[stream_idx];
1348 prev_buffer = stream->buffer;
1350 if (device->recording)
1351 device->recording->changed.streamSource |= 1u << stream_idx;
1353 if (prev_buffer == buffer
1354 && stream->stride == stride
1355 && stream->offset == offset)
1357 TRACE("Application is setting the old values over, nothing to do.\n");
1358 return WINED3D_OK;
1361 stream->buffer = buffer;
1362 if (buffer)
1364 stream->stride = stride;
1365 stream->offset = offset;
1366 wined3d_buffer_incref(buffer);
1369 if (!device->recording)
1370 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1371 if (prev_buffer)
1372 wined3d_buffer_decref(prev_buffer);
1374 return WINED3D_OK;
1377 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1378 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1380 const struct wined3d_stream_state *stream;
1382 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1383 device, stream_idx, buffer, offset, stride);
1385 if (stream_idx >= MAX_STREAMS)
1387 WARN("Stream index %u out of range.\n", stream_idx);
1388 return WINED3DERR_INVALIDCALL;
1391 stream = &device->state.streams[stream_idx];
1392 *buffer = stream->buffer;
1393 if (offset)
1394 *offset = stream->offset;
1395 *stride = stream->stride;
1397 return WINED3D_OK;
1400 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1402 struct wined3d_stream_state *stream;
1403 UINT old_flags, old_freq;
1405 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1407 /* Verify input. At least in d3d9 this is invalid. */
1408 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1410 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1411 return WINED3DERR_INVALIDCALL;
1413 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1415 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1416 return WINED3DERR_INVALIDCALL;
1418 if (!divider)
1420 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1421 return WINED3DERR_INVALIDCALL;
1424 stream = &device->update_state->streams[stream_idx];
1425 old_flags = stream->flags;
1426 old_freq = stream->frequency;
1428 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1429 stream->frequency = divider & 0x7fffff;
1431 if (device->recording)
1432 device->recording->changed.streamFreq |= 1u << stream_idx;
1433 else if (stream->frequency != old_freq || stream->flags != old_flags)
1434 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1436 return WINED3D_OK;
1439 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1440 UINT stream_idx, UINT *divider)
1442 const struct wined3d_stream_state *stream;
1444 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1446 stream = &device->state.streams[stream_idx];
1447 *divider = stream->flags | stream->frequency;
1449 TRACE("Returning %#x.\n", *divider);
1451 return WINED3D_OK;
1454 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1455 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1457 TRACE("device %p, state %s, matrix %p.\n",
1458 device, debug_d3dtstype(d3dts), matrix);
1459 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1460 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1461 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1462 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1464 /* Handle recording of state blocks. */
1465 if (device->recording)
1467 TRACE("Recording... not performing anything.\n");
1468 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1469 device->update_state->transforms[d3dts] = *matrix;
1470 return;
1473 /* If the new matrix is the same as the current one,
1474 * we cut off any further processing. this seems to be a reasonable
1475 * optimization because as was noticed, some apps (warcraft3 for example)
1476 * tend towards setting the same matrix repeatedly for some reason.
1478 * From here on we assume that the new matrix is different, wherever it matters. */
1479 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1481 TRACE("The application is setting the same matrix over again.\n");
1482 return;
1485 device->state.transforms[d3dts] = *matrix;
1486 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1489 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1490 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1492 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1494 *matrix = device->state.transforms[state];
1497 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1498 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1500 const struct wined3d_matrix *mat;
1501 struct wined3d_matrix temp;
1503 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1505 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1506 * below means it will be recorded in a state block change, but it
1507 * works regardless where it is recorded.
1508 * If this is found to be wrong, change to StateBlock. */
1509 if (state > HIGHEST_TRANSFORMSTATE)
1511 WARN("Unhandled transform state %#x.\n", state);
1512 return;
1515 mat = &device->update_state->transforms[state];
1516 multiply_matrix(&temp, mat, matrix);
1518 /* Apply change via set transform - will reapply to eg. lights this way. */
1519 wined3d_device_set_transform(device, state, &temp);
1522 /* Note lights are real special cases. Although the device caps state only
1523 * e.g. 8 are supported, you can reference any indexes you want as long as
1524 * that number max are enabled at any one point in time. Therefore since the
1525 * indices can be anything, we need a hashmap of them. However, this causes
1526 * stateblock problems. When capturing the state block, I duplicate the
1527 * hashmap, but when recording, just build a chain pretty much of commands to
1528 * be replayed. */
1529 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1530 UINT light_idx, const struct wined3d_light *light)
1532 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1533 struct wined3d_light_info *object = NULL;
1534 float rho;
1536 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1538 /* Check the parameter range. Need for speed most wanted sets junk lights
1539 * which confuse the GL driver. */
1540 if (!light)
1541 return WINED3DERR_INVALIDCALL;
1543 switch (light->type)
1545 case WINED3D_LIGHT_POINT:
1546 case WINED3D_LIGHT_SPOT:
1547 case WINED3D_LIGHT_GLSPOT:
1548 /* Incorrect attenuation values can cause the gl driver to crash.
1549 * Happens with Need for speed most wanted. */
1550 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1552 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1553 return WINED3DERR_INVALIDCALL;
1555 break;
1557 case WINED3D_LIGHT_DIRECTIONAL:
1558 case WINED3D_LIGHT_PARALLELPOINT:
1559 /* Ignores attenuation */
1560 break;
1562 default:
1563 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1564 return WINED3DERR_INVALIDCALL;
1567 if (!(object = wined3d_state_get_light(device->update_state, light_idx)))
1569 TRACE("Adding new light\n");
1570 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1571 if (!object)
1572 return E_OUTOFMEMORY;
1574 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1575 object->glIndex = -1;
1576 object->OriginalIndex = light_idx;
1579 /* Initialize the object. */
1580 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1581 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1582 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1583 light_idx, light->type, debug_color(&light->diffuse),
1584 debug_color(&light->specular), debug_color(&light->ambient),
1585 light->position.x, light->position.y, light->position.z,
1586 light->direction.x, light->direction.y, light->direction.z,
1587 light->range, light->falloff, light->theta, light->phi);
1589 /* Save away the information. */
1590 object->OriginalParms = *light;
1592 switch (light->type)
1594 case WINED3D_LIGHT_POINT:
1595 /* Position */
1596 object->position.x = light->position.x;
1597 object->position.y = light->position.y;
1598 object->position.z = light->position.z;
1599 object->position.w = 1.0f;
1600 object->cutoff = 180.0f;
1601 /* FIXME: Range */
1602 break;
1604 case WINED3D_LIGHT_DIRECTIONAL:
1605 /* Direction */
1606 object->direction.x = -light->direction.x;
1607 object->direction.y = -light->direction.y;
1608 object->direction.z = -light->direction.z;
1609 object->direction.w = 0.0f;
1610 object->exponent = 0.0f;
1611 object->cutoff = 180.0f;
1612 break;
1614 case WINED3D_LIGHT_SPOT:
1615 /* Position */
1616 object->position.x = light->position.x;
1617 object->position.y = light->position.y;
1618 object->position.z = light->position.z;
1619 object->position.w = 1.0f;
1621 /* Direction */
1622 object->direction.x = light->direction.x;
1623 object->direction.y = light->direction.y;
1624 object->direction.z = light->direction.z;
1625 object->direction.w = 0.0f;
1627 /* opengl-ish and d3d-ish spot lights use too different models
1628 * for the light "intensity" as a function of the angle towards
1629 * the main light direction, so we only can approximate very
1630 * roughly. However, spot lights are rather rarely used in games
1631 * (if ever used at all). Furthermore if still used, probably
1632 * nobody pays attention to such details. */
1633 if (!light->falloff)
1635 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1636 * equations have the falloff resp. exponent parameter as an
1637 * exponent, so the spot light lighting will always be 1.0 for
1638 * both of them, and we don't have to care for the rest of the
1639 * rather complex calculation. */
1640 object->exponent = 0.0f;
1642 else
1644 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1645 if (rho < 0.0001f)
1646 rho = 0.0001f;
1647 object->exponent = -0.3f / logf(cosf(rho / 2));
1650 if (object->exponent > 128.0f)
1651 object->exponent = 128.0f;
1653 object->cutoff = (float)(light->phi * 90 / M_PI);
1654 /* FIXME: Range */
1655 break;
1657 case WINED3D_LIGHT_PARALLELPOINT:
1658 object->position.x = light->position.x;
1659 object->position.y = light->position.y;
1660 object->position.z = light->position.z;
1661 object->position.w = 1.0f;
1662 break;
1664 default:
1665 FIXME("Unrecognized light type %#x.\n", light->type);
1668 if (!device->recording)
1669 wined3d_cs_emit_set_light(device->cs, object);
1671 return WINED3D_OK;
1674 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1675 UINT light_idx, struct wined3d_light *light)
1677 struct wined3d_light_info *light_info;
1679 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1681 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1683 TRACE("Light information requested but light not defined\n");
1684 return WINED3DERR_INVALIDCALL;
1687 *light = light_info->OriginalParms;
1688 return WINED3D_OK;
1691 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1693 struct wined3d_light_info *light_info;
1695 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1697 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1698 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1700 TRACE("Light enabled requested but light not defined, so defining one!\n");
1701 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1703 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1705 FIXME("Adding default lights has failed dismally\n");
1706 return WINED3DERR_INVALIDCALL;
1710 wined3d_state_enable_light(device->update_state, &device->adapter->d3d_info, light_info, enable);
1711 if (!device->recording)
1712 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1714 return WINED3D_OK;
1717 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1719 struct wined3d_light_info *light_info;
1721 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1723 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1725 TRACE("Light enabled state requested but light not defined.\n");
1726 return WINED3DERR_INVALIDCALL;
1728 /* true is 128 according to SetLightEnable */
1729 *enable = light_info->enabled ? 128 : 0;
1730 return WINED3D_OK;
1733 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1734 UINT plane_idx, const struct wined3d_vec4 *plane)
1736 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1738 /* Validate plane_idx. */
1739 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1741 TRACE("Application has requested clipplane this device doesn't support.\n");
1742 return WINED3DERR_INVALIDCALL;
1745 if (device->recording)
1746 device->recording->changed.clipplane |= 1u << plane_idx;
1748 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1750 TRACE("Application is setting old values over, nothing to do.\n");
1751 return WINED3D_OK;
1754 device->update_state->clip_planes[plane_idx] = *plane;
1756 if (!device->recording)
1757 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1759 return WINED3D_OK;
1762 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1763 UINT plane_idx, struct wined3d_vec4 *plane)
1765 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1767 /* Validate plane_idx. */
1768 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1770 TRACE("Application has requested clipplane this device doesn't support.\n");
1771 return WINED3DERR_INVALIDCALL;
1774 *plane = device->state.clip_planes[plane_idx];
1776 return WINED3D_OK;
1779 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1780 const struct wined3d_clip_status *clip_status)
1782 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1784 if (!clip_status)
1785 return WINED3DERR_INVALIDCALL;
1787 return WINED3D_OK;
1790 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1791 struct wined3d_clip_status *clip_status)
1793 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1795 if (!clip_status)
1796 return WINED3DERR_INVALIDCALL;
1798 return WINED3D_OK;
1801 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1803 TRACE("device %p, material %p.\n", device, material);
1805 device->update_state->material = *material;
1807 if (device->recording)
1808 device->recording->changed.material = TRUE;
1809 else
1810 wined3d_cs_emit_set_material(device->cs, material);
1813 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1815 TRACE("device %p, material %p.\n", device, material);
1817 *material = device->state.material;
1819 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1820 TRACE("ambient %s\n", debug_color(&material->ambient));
1821 TRACE("specular %s\n", debug_color(&material->specular));
1822 TRACE("emissive %s\n", debug_color(&material->emissive));
1823 TRACE("power %.8e.\n", material->power);
1826 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1827 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1829 enum wined3d_format_id prev_format;
1830 struct wined3d_buffer *prev_buffer;
1831 unsigned int prev_offset;
1833 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1834 device, buffer, debug_d3dformat(format_id), offset);
1836 prev_buffer = device->update_state->index_buffer;
1837 prev_format = device->update_state->index_format;
1838 prev_offset = device->update_state->index_offset;
1840 device->update_state->index_buffer = buffer;
1841 device->update_state->index_format = format_id;
1842 device->update_state->index_offset = offset;
1844 if (device->recording)
1845 device->recording->changed.indices = TRUE;
1847 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1848 return;
1850 if (buffer)
1851 wined3d_buffer_incref(buffer);
1852 if (!device->recording)
1853 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1854 if (prev_buffer)
1855 wined3d_buffer_decref(prev_buffer);
1858 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1859 enum wined3d_format_id *format, unsigned int *offset)
1861 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1863 *format = device->state.index_format;
1864 if (offset)
1865 *offset = device->state.index_offset;
1866 return device->state.index_buffer;
1869 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1871 TRACE("device %p, base_index %d.\n", device, base_index);
1873 device->update_state->base_vertex_index = base_index;
1876 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1878 TRACE("device %p.\n", device);
1880 return device->state.base_vertex_index;
1883 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1885 TRACE("device %p, viewport %p.\n", device, viewport);
1886 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1887 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1889 device->update_state->viewport = *viewport;
1891 /* Handle recording of state blocks */
1892 if (device->recording)
1894 TRACE("Recording... not performing anything\n");
1895 device->recording->changed.viewport = TRUE;
1896 return;
1899 wined3d_cs_emit_set_viewport(device->cs, viewport);
1902 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1904 TRACE("device %p, viewport %p.\n", device, viewport);
1906 *viewport = device->state.viewport;
1909 static void resolve_depth_buffer(struct wined3d_state *state)
1911 struct wined3d_texture *dst_texture = state->textures[0];
1912 struct wined3d_rendertarget_view *src_view;
1913 RECT src_rect, dst_rect;
1915 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1916 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1917 return;
1919 if (!(src_view = state->fb->depth_stencil))
1920 return;
1921 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
1923 FIXME("Not supported on buffer resources.\n");
1924 return;
1927 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
1928 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
1929 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
1930 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1933 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1934 struct wined3d_rasterizer_state *rasterizer_state)
1936 struct wined3d_rasterizer_state *prev;
1938 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
1940 prev = device->update_state->rasterizer_state;
1941 if (prev == rasterizer_state)
1942 return;
1944 if (rasterizer_state)
1945 wined3d_rasterizer_state_incref(rasterizer_state);
1946 device->update_state->rasterizer_state = rasterizer_state;
1947 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
1948 if (prev)
1949 wined3d_rasterizer_state_decref(prev);
1952 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
1954 TRACE("device %p.\n", device);
1956 return device->state.rasterizer_state;
1959 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1960 enum wined3d_render_state state, DWORD value)
1962 DWORD old_value;
1964 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1966 if (state > WINEHIGHEST_RENDER_STATE)
1968 WARN("Unhandled render state %#x.\n", state);
1969 return;
1972 old_value = device->state.render_states[state];
1973 device->update_state->render_states[state] = value;
1975 /* Handle recording of state blocks. */
1976 if (device->recording)
1978 TRACE("Recording... not performing anything.\n");
1979 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1980 return;
1983 /* Compared here and not before the assignment to allow proper stateblock recording. */
1984 if (value == old_value)
1985 TRACE("Application is setting the old value over, nothing to do.\n");
1986 else
1987 wined3d_cs_emit_set_render_state(device->cs, state, value);
1989 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1991 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1992 resolve_depth_buffer(&device->state);
1996 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1998 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2000 return device->state.render_states[state];
2003 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2004 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2006 DWORD old_value;
2008 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2009 device, sampler_idx, debug_d3dsamplerstate(state), value);
2011 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2012 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2014 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2016 WARN("Invalid sampler %u.\n", sampler_idx);
2017 return; /* Windows accepts overflowing this array ... we do not. */
2020 old_value = device->state.sampler_states[sampler_idx][state];
2021 device->update_state->sampler_states[sampler_idx][state] = value;
2023 /* Handle recording of state blocks. */
2024 if (device->recording)
2026 TRACE("Recording... not performing anything.\n");
2027 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2028 return;
2031 if (old_value == value)
2033 TRACE("Application is setting the old value over, nothing to do.\n");
2034 return;
2037 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2040 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2041 UINT sampler_idx, enum wined3d_sampler_state state)
2043 TRACE("device %p, sampler_idx %u, state %s.\n",
2044 device, sampler_idx, debug_d3dsamplerstate(state));
2046 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2047 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2049 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2051 WARN("Invalid sampler %u.\n", sampler_idx);
2052 return 0; /* Windows accepts overflowing this array ... we do not. */
2055 return device->state.sampler_states[sampler_idx][state];
2058 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2060 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2062 if (device->recording)
2063 device->recording->changed.scissorRect = TRUE;
2065 if (EqualRect(&device->update_state->scissor_rect, rect))
2067 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2068 return;
2070 CopyRect(&device->update_state->scissor_rect, rect);
2072 if (device->recording)
2074 TRACE("Recording... not performing anything.\n");
2075 return;
2078 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2081 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2083 TRACE("device %p, rect %p.\n", device, rect);
2085 *rect = device->state.scissor_rect;
2086 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2089 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2090 struct wined3d_vertex_declaration *declaration)
2092 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2094 TRACE("device %p, declaration %p.\n", device, declaration);
2096 if (device->recording)
2097 device->recording->changed.vertexDecl = TRUE;
2099 if (declaration == prev)
2100 return;
2102 if (declaration)
2103 wined3d_vertex_declaration_incref(declaration);
2104 device->update_state->vertex_declaration = declaration;
2105 if (!device->recording)
2106 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2107 if (prev)
2108 wined3d_vertex_declaration_decref(prev);
2111 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2113 TRACE("device %p.\n", device);
2115 return device->state.vertex_declaration;
2118 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2120 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2122 TRACE("device %p, shader %p.\n", device, shader);
2124 if (device->recording)
2125 device->recording->changed.vertexShader = TRUE;
2127 if (shader == prev)
2128 return;
2130 if (shader)
2131 wined3d_shader_incref(shader);
2132 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2133 if (!device->recording)
2134 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2135 if (prev)
2136 wined3d_shader_decref(prev);
2139 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2141 TRACE("device %p.\n", device);
2143 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2146 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2147 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2149 struct wined3d_buffer *prev;
2151 if (idx >= MAX_CONSTANT_BUFFERS)
2153 WARN("Invalid constant buffer index %u.\n", idx);
2154 return;
2157 prev = device->update_state->cb[type][idx];
2158 if (buffer == prev)
2159 return;
2161 if (buffer)
2162 wined3d_buffer_incref(buffer);
2163 device->update_state->cb[type][idx] = buffer;
2164 if (!device->recording)
2165 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2166 if (prev)
2167 wined3d_buffer_decref(prev);
2170 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2172 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2174 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2177 static struct wined3d_buffer *wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2178 enum wined3d_shader_type shader_type, unsigned int idx)
2180 if (idx >= MAX_CONSTANT_BUFFERS)
2182 WARN("Invalid constant buffer index %u.\n", idx);
2183 return NULL;
2186 return device->state.cb[shader_type][idx];
2189 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2191 TRACE("device %p, idx %u.\n", device, idx);
2193 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2196 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2197 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2199 struct wined3d_shader_resource_view *prev;
2201 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2203 WARN("Invalid view index %u.\n", idx);
2204 return;
2207 prev = device->update_state->shader_resource_view[type][idx];
2208 if (view == prev)
2209 return;
2211 if (view)
2212 wined3d_shader_resource_view_incref(view);
2213 device->update_state->shader_resource_view[type][idx] = view;
2214 if (!device->recording)
2215 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2216 if (prev)
2217 wined3d_shader_resource_view_decref(prev);
2220 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2221 UINT idx, struct wined3d_shader_resource_view *view)
2223 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2225 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2228 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2229 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2231 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2233 WARN("Invalid view index %u.\n", idx);
2234 return NULL;
2237 return device->state.shader_resource_view[shader_type][idx];
2240 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2241 UINT idx)
2243 TRACE("device %p, idx %u.\n", device, idx);
2245 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2248 static void wined3d_device_set_sampler(struct wined3d_device *device,
2249 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2251 struct wined3d_sampler *prev;
2253 if (idx >= MAX_SAMPLER_OBJECTS)
2255 WARN("Invalid sampler index %u.\n", idx);
2256 return;
2259 prev = device->update_state->sampler[type][idx];
2260 if (sampler == prev)
2261 return;
2263 if (sampler)
2264 wined3d_sampler_incref(sampler);
2265 device->update_state->sampler[type][idx] = sampler;
2266 if (!device->recording)
2267 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2268 if (prev)
2269 wined3d_sampler_decref(prev);
2272 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2274 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2276 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2279 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2280 enum wined3d_shader_type shader_type, unsigned int idx)
2282 if (idx >= MAX_SAMPLER_OBJECTS)
2284 WARN("Invalid sampler index %u.\n", idx);
2285 return NULL;
2288 return device->state.sampler[shader_type][idx];
2291 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2293 TRACE("device %p, idx %u.\n", device, idx);
2295 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2298 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2299 unsigned int start_idx, unsigned int count, const BOOL *constants)
2301 unsigned int i;
2303 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2304 device, start_idx, count, constants);
2306 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2307 return WINED3DERR_INVALIDCALL;
2309 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2310 count = WINED3D_MAX_CONSTS_B - start_idx;
2311 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2312 if (TRACE_ON(d3d))
2314 for (i = 0; i < count; ++i)
2315 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2318 if (device->recording)
2320 for (i = start_idx; i < count + start_idx; ++i)
2321 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2323 else
2325 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2328 return WINED3D_OK;
2331 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2332 unsigned int start_idx, unsigned int count, BOOL *constants)
2334 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2335 device, start_idx, count, constants);
2337 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2338 return WINED3DERR_INVALIDCALL;
2340 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2341 count = WINED3D_MAX_CONSTS_B - start_idx;
2342 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2344 return WINED3D_OK;
2347 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2348 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2350 unsigned int i;
2352 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2353 device, start_idx, count, constants);
2355 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2356 return WINED3DERR_INVALIDCALL;
2358 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2359 count = WINED3D_MAX_CONSTS_I - start_idx;
2360 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2361 if (TRACE_ON(d3d))
2363 for (i = 0; i < count; ++i)
2364 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2367 if (device->recording)
2369 for (i = start_idx; i < count + start_idx; ++i)
2370 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2372 else
2374 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2377 return WINED3D_OK;
2380 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2381 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2383 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2384 device, start_idx, count, constants);
2386 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2387 return WINED3DERR_INVALIDCALL;
2389 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2390 count = WINED3D_MAX_CONSTS_I - start_idx;
2391 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2392 return WINED3D_OK;
2395 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2396 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2398 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2399 unsigned int i;
2401 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2402 device, start_idx, count, constants);
2404 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2405 || count > d3d_info->limits.vs_uniform_count - start_idx)
2406 return WINED3DERR_INVALIDCALL;
2408 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2409 if (TRACE_ON(d3d))
2411 for (i = 0; i < count; ++i)
2412 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2415 if (device->recording)
2416 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2417 count * sizeof(*device->recording->changed.vs_consts_f));
2418 else
2419 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2421 return WINED3D_OK;
2424 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2425 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2427 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2429 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2430 device, start_idx, count, constants);
2432 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2433 || count > d3d_info->limits.vs_uniform_count - start_idx)
2434 return WINED3DERR_INVALIDCALL;
2436 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2438 return WINED3D_OK;
2441 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2443 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2445 TRACE("device %p, shader %p.\n", device, shader);
2447 if (device->recording)
2448 device->recording->changed.pixelShader = TRUE;
2450 if (shader == prev)
2451 return;
2453 if (shader)
2454 wined3d_shader_incref(shader);
2455 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2456 if (!device->recording)
2457 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2458 if (prev)
2459 wined3d_shader_decref(prev);
2462 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2464 TRACE("device %p.\n", device);
2466 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2469 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2471 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2473 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2476 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2478 TRACE("device %p, idx %u.\n", device, idx);
2480 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2483 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2484 UINT idx, struct wined3d_shader_resource_view *view)
2486 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2488 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2491 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2492 UINT idx)
2494 TRACE("device %p, idx %u.\n", device, idx);
2496 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2499 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2501 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2503 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2506 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2508 TRACE("device %p, idx %u.\n", device, idx);
2510 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2513 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2514 unsigned int start_idx, unsigned int count, const BOOL *constants)
2516 unsigned int i;
2518 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2519 device, start_idx, count, constants);
2521 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2522 return WINED3DERR_INVALIDCALL;
2524 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2525 count = WINED3D_MAX_CONSTS_B - start_idx;
2526 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2527 if (TRACE_ON(d3d))
2529 for (i = 0; i < count; ++i)
2530 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2533 if (device->recording)
2535 for (i = start_idx; i < count + start_idx; ++i)
2536 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2538 else
2540 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2543 return WINED3D_OK;
2546 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2547 unsigned int start_idx, unsigned int count, BOOL *constants)
2549 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2550 device, start_idx, count, constants);
2552 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2553 return WINED3DERR_INVALIDCALL;
2555 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2556 count = WINED3D_MAX_CONSTS_B - start_idx;
2557 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2559 return WINED3D_OK;
2562 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2563 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2565 unsigned int i;
2567 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2568 device, start_idx, count, constants);
2570 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2571 return WINED3DERR_INVALIDCALL;
2573 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2574 count = WINED3D_MAX_CONSTS_I - start_idx;
2575 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2576 if (TRACE_ON(d3d))
2578 for (i = 0; i < count; ++i)
2579 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2582 if (device->recording)
2584 for (i = start_idx; i < count + start_idx; ++i)
2585 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2587 else
2589 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2592 return WINED3D_OK;
2595 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2596 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2598 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2599 device, start_idx, count, constants);
2601 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2602 return WINED3DERR_INVALIDCALL;
2604 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2605 count = WINED3D_MAX_CONSTS_I - start_idx;
2606 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2608 return WINED3D_OK;
2611 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2612 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2614 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2615 unsigned int i;
2617 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2618 device, start_idx, count, constants);
2620 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2621 || count > d3d_info->limits.ps_uniform_count - start_idx)
2622 return WINED3DERR_INVALIDCALL;
2624 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2625 if (TRACE_ON(d3d))
2627 for (i = 0; i < count; ++i)
2628 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2631 if (device->recording)
2632 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2633 count * sizeof(*device->recording->changed.ps_consts_f));
2634 else
2635 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2637 return WINED3D_OK;
2640 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2641 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2643 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2645 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2646 device, start_idx, count, constants);
2648 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2649 || count > d3d_info->limits.ps_uniform_count - start_idx)
2650 return WINED3DERR_INVALIDCALL;
2652 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2654 return WINED3D_OK;
2657 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2659 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2661 TRACE("device %p, shader %p.\n", device, shader);
2663 if (device->recording || shader == prev)
2664 return;
2665 if (shader)
2666 wined3d_shader_incref(shader);
2667 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2668 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2669 if (prev)
2670 wined3d_shader_decref(prev);
2673 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2675 TRACE("device %p.\n", device);
2677 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2680 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2682 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2684 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2687 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2689 TRACE("device %p, idx %u.\n", device, idx);
2691 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2694 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2695 UINT idx, struct wined3d_shader_resource_view *view)
2697 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2699 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2702 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2703 UINT idx)
2705 TRACE("device %p, idx %u.\n", device, idx);
2707 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2710 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2712 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2714 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2717 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2719 TRACE("device %p, idx %u.\n", device, idx);
2721 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2724 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2726 struct wined3d_shader *prev;
2728 TRACE("device %p, shader %p.\n", device, shader);
2730 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2731 if (device->recording || shader == prev)
2732 return;
2733 if (shader)
2734 wined3d_shader_incref(shader);
2735 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2736 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2737 if (prev)
2738 wined3d_shader_decref(prev);
2741 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2743 TRACE("device %p.\n", device);
2745 return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2748 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2750 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2752 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2755 struct wined3d_buffer * CDECL wined3d_device_get_cs_cb(const struct wined3d_device *device, unsigned int idx)
2757 TRACE("device %p, idx %u.\n", device, idx);
2759 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2762 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2763 unsigned int idx, struct wined3d_shader_resource_view *view)
2765 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2767 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2770 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2771 unsigned int idx)
2773 TRACE("device %p, idx %u.\n", device, idx);
2775 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2778 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2779 unsigned int idx, struct wined3d_sampler *sampler)
2781 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2783 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2786 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2788 TRACE("device %p, idx %u.\n", device, idx);
2790 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2793 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2794 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav)
2796 struct wined3d_unordered_access_view *prev;
2798 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2800 WARN("Invalid UAV index %u.\n", idx);
2801 return;
2804 prev = device->update_state->unordered_access_view[pipeline][idx];
2805 if (uav == prev)
2806 return;
2808 if (uav)
2809 wined3d_unordered_access_view_incref(uav);
2810 device->update_state->unordered_access_view[pipeline][idx] = uav;
2811 if (!device->recording)
2812 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav);
2813 if (prev)
2814 wined3d_unordered_access_view_decref(prev);
2817 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2818 struct wined3d_unordered_access_view *uav)
2820 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2822 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav);
2825 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2826 unsigned int idx, struct wined3d_unordered_access_view *uav)
2828 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2830 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav);
2833 /* Context activation is done by the caller. */
2834 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2835 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2836 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2837 DWORD DestFVF)
2839 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2840 struct wined3d_map_desc map_desc;
2841 struct wined3d_box box = {0};
2842 struct wined3d_viewport vp;
2843 UINT vertex_size;
2844 unsigned int i;
2845 BYTE *dest_ptr;
2846 BOOL doClip;
2847 DWORD numTextures;
2848 HRESULT hr;
2850 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2852 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2855 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2857 ERR("Source has no position mask\n");
2858 return WINED3DERR_INVALIDCALL;
2861 if (device->state.render_states[WINED3D_RS_CLIPPING])
2863 static BOOL warned = FALSE;
2865 * The clipping code is not quite correct. Some things need
2866 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2867 * so disable clipping for now.
2868 * (The graphics in Half-Life are broken, and my processvertices
2869 * test crashes with IDirect3DDevice3)
2870 doClip = TRUE;
2872 doClip = FALSE;
2873 if(!warned) {
2874 warned = TRUE;
2875 FIXME("Clipping is broken and disabled for now\n");
2878 else
2879 doClip = FALSE;
2881 vertex_size = get_flexible_vertex_size(DestFVF);
2882 box.left = dwDestIndex * vertex_size;
2883 box.right = box.left + dwCount * vertex_size;
2884 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2886 WARN("Failed to map buffer, hr %#x.\n", hr);
2887 return hr;
2889 dest_ptr = map_desc.data;
2891 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2892 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2893 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2895 TRACE("View mat:\n");
2896 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2897 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2898 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2899 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2901 TRACE("Proj mat:\n");
2902 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2903 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2904 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2905 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2907 TRACE("World mat:\n");
2908 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2909 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2910 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2911 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2913 /* Get the viewport */
2914 wined3d_device_get_viewport(device, &vp);
2915 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2916 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2918 multiply_matrix(&mat,&view_mat,&world_mat);
2919 multiply_matrix(&mat,&proj_mat,&mat);
2921 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2923 for (i = 0; i < dwCount; i+= 1) {
2924 unsigned int tex_index;
2926 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2927 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2928 /* The position first */
2929 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2930 const float *p = (const float *)(element->data.addr + i * element->stride);
2931 float x, y, z, rhw;
2932 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2934 /* Multiplication with world, view and projection matrix. */
2935 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2936 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2937 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2938 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2940 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2942 /* WARNING: The following things are taken from d3d7 and were not yet checked
2943 * against d3d8 or d3d9!
2946 /* Clipping conditions: From msdn
2948 * A vertex is clipped if it does not match the following requirements
2949 * -rhw < x <= rhw
2950 * -rhw < y <= rhw
2951 * 0 < z <= rhw
2952 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2954 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2955 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2959 if( !doClip ||
2960 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2961 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2962 ( rhw > eps ) ) ) {
2964 /* "Normal" viewport transformation (not clipped)
2965 * 1) The values are divided by rhw
2966 * 2) The y axis is negative, so multiply it with -1
2967 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2968 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2969 * 4) Multiply x with Width/2 and add Width/2
2970 * 5) The same for the height
2971 * 6) Add the viewpoint X and Y to the 2D coordinates and
2972 * The minimum Z value to z
2973 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2975 * Well, basically it's simply a linear transformation into viewport
2976 * coordinates
2979 x /= rhw;
2980 y /= rhw;
2981 z /= rhw;
2983 y *= -1;
2985 x *= vp.width / 2;
2986 y *= vp.height / 2;
2987 z *= vp.max_z - vp.min_z;
2989 x += vp.width / 2 + vp.x;
2990 y += vp.height / 2 + vp.y;
2991 z += vp.min_z;
2993 rhw = 1 / rhw;
2994 } else {
2995 /* That vertex got clipped
2996 * Contrary to OpenGL it is not dropped completely, it just
2997 * undergoes a different calculation.
2999 TRACE("Vertex got clipped\n");
3000 x += rhw;
3001 y += rhw;
3003 x /= 2;
3004 y /= 2;
3006 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3007 * outside of the main vertex buffer memory. That needs some more
3008 * investigation...
3012 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3015 ( (float *) dest_ptr)[0] = x;
3016 ( (float *) dest_ptr)[1] = y;
3017 ( (float *) dest_ptr)[2] = z;
3018 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3020 dest_ptr += 3 * sizeof(float);
3022 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3023 dest_ptr += sizeof(float);
3026 if (DestFVF & WINED3DFVF_PSIZE)
3027 dest_ptr += sizeof(DWORD);
3029 if (DestFVF & WINED3DFVF_NORMAL)
3031 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3032 const float *normal = (const float *)(element->data.addr + i * element->stride);
3033 /* AFAIK this should go into the lighting information */
3034 FIXME("Didn't expect the destination to have a normal\n");
3035 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3038 if (DestFVF & WINED3DFVF_DIFFUSE)
3040 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3041 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3042 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3044 static BOOL warned = FALSE;
3046 if(!warned) {
3047 ERR("No diffuse color in source, but destination has one\n");
3048 warned = TRUE;
3051 *( (DWORD *) dest_ptr) = 0xffffffff;
3052 dest_ptr += sizeof(DWORD);
3054 else
3056 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3060 if (DestFVF & WINED3DFVF_SPECULAR)
3062 /* What's the color value in the feedback buffer? */
3063 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3064 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3065 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3067 static BOOL warned = FALSE;
3069 if(!warned) {
3070 ERR("No specular color in source, but destination has one\n");
3071 warned = TRUE;
3074 *(DWORD *)dest_ptr = 0xff000000;
3075 dest_ptr += sizeof(DWORD);
3077 else
3079 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3083 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3085 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3086 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3087 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3089 ERR("No source texture, but destination requests one\n");
3090 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3092 else
3094 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3099 wined3d_resource_unmap(&dest->resource, 0);
3101 return WINED3D_OK;
3103 #undef copy_and_next
3105 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3106 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3107 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3109 struct wined3d_state *state = &device->state;
3110 struct wined3d_stream_info stream_info;
3111 struct wined3d_resource *resource;
3112 struct wined3d_box box = {0};
3113 struct wined3d_shader *vs;
3114 unsigned int i;
3115 HRESULT hr;
3116 WORD map;
3118 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3119 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3120 device, src_start_idx, dst_idx, vertex_count,
3121 dst_buffer, declaration, flags, dst_fvf);
3123 if (declaration)
3124 FIXME("Output vertex declaration not implemented yet.\n");
3126 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3127 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3128 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->gl_info, &device->adapter->d3d_info);
3129 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3131 /* We can't convert FROM a VBO, and vertex buffers used to source into
3132 * process_vertices() are unlikely to ever be used for drawing. Release
3133 * VBOs in those buffers and fix up the stream_info structure.
3135 * Also apply the start index. */
3136 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3138 struct wined3d_stream_info_element *e;
3139 struct wined3d_map_desc map_desc;
3141 if (!(map & 1))
3142 continue;
3144 e = &stream_info.elements[i];
3145 resource = &state->streams[e->stream_idx].buffer->resource;
3146 box.left = src_start_idx * e->stride;
3147 box.right = box.left + vertex_count * e->stride;
3148 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READONLY)))
3149 ERR("Failed to map resource.\n");
3150 e->data.buffer_object = 0;
3151 e->data.addr += (ULONG_PTR)map_desc.data;
3154 hr = process_vertices_strided(device, dst_idx, vertex_count,
3155 &stream_info, dst_buffer, flags, dst_fvf);
3157 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3159 if (!(map & 1))
3160 continue;
3162 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3163 if (FAILED(wined3d_resource_unmap(resource, 0)))
3164 ERR("Failed to unmap resource.\n");
3167 return hr;
3170 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3171 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3173 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3174 DWORD old_value;
3176 TRACE("device %p, stage %u, state %s, value %#x.\n",
3177 device, stage, debug_d3dtexturestate(state), value);
3179 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3181 WARN("Invalid state %#x passed.\n", state);
3182 return;
3185 if (stage >= d3d_info->limits.ffp_blend_stages)
3187 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3188 stage, d3d_info->limits.ffp_blend_stages - 1);
3189 return;
3192 old_value = device->update_state->texture_states[stage][state];
3193 device->update_state->texture_states[stage][state] = value;
3195 if (device->recording)
3197 TRACE("Recording... not performing anything.\n");
3198 device->recording->changed.textureState[stage] |= 1u << state;
3199 return;
3202 /* Checked after the assignments to allow proper stateblock recording. */
3203 if (old_value == value)
3205 TRACE("Application is setting the old value over, nothing to do.\n");
3206 return;
3209 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3212 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3213 UINT stage, enum wined3d_texture_stage_state state)
3215 TRACE("device %p, stage %u, state %s.\n",
3216 device, stage, debug_d3dtexturestate(state));
3218 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3220 WARN("Invalid state %#x passed.\n", state);
3221 return 0;
3224 return device->state.texture_states[stage][state];
3227 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3228 UINT stage, struct wined3d_texture *texture)
3230 struct wined3d_texture *prev;
3232 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3234 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3235 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3237 /* Windows accepts overflowing this array... we do not. */
3238 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3240 WARN("Ignoring invalid stage %u.\n", stage);
3241 return WINED3D_OK;
3244 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3246 WARN("Rejecting attempt to set scratch texture.\n");
3247 return WINED3DERR_INVALIDCALL;
3250 if (device->recording)
3251 device->recording->changed.textures |= 1u << stage;
3253 prev = device->update_state->textures[stage];
3254 TRACE("Previous texture %p.\n", prev);
3256 if (texture == prev)
3258 TRACE("App is setting the same texture again, nothing to do.\n");
3259 return WINED3D_OK;
3262 TRACE("Setting new texture to %p.\n", texture);
3263 device->update_state->textures[stage] = texture;
3265 if (texture)
3266 wined3d_texture_incref(texture);
3267 if (!device->recording)
3268 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3269 if (prev)
3270 wined3d_texture_decref(prev);
3272 return WINED3D_OK;
3275 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3277 TRACE("device %p, stage %u.\n", device, stage);
3279 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3280 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3282 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3284 WARN("Ignoring invalid stage %u.\n", stage);
3285 return NULL; /* Windows accepts overflowing this array ... we do not. */
3288 return device->state.textures[stage];
3291 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3293 TRACE("device %p, caps %p.\n", device, caps);
3295 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3296 device->create_parms.device_type, caps);
3299 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3300 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3302 struct wined3d_swapchain *swapchain;
3304 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3305 device, swapchain_idx, mode, rotation);
3307 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3308 return WINED3DERR_INVALIDCALL;
3310 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3313 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3315 struct wined3d_stateblock *stateblock;
3316 HRESULT hr;
3318 TRACE("device %p.\n", device);
3320 if (device->recording)
3321 return WINED3DERR_INVALIDCALL;
3323 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3324 if (FAILED(hr))
3325 return hr;
3327 device->recording = stateblock;
3328 device->update_state = &stateblock->state;
3330 TRACE("Recording stateblock %p.\n", stateblock);
3332 return WINED3D_OK;
3335 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3336 struct wined3d_stateblock **stateblock)
3338 struct wined3d_stateblock *object = device->recording;
3340 TRACE("device %p, stateblock %p.\n", device, stateblock);
3342 if (!device->recording)
3344 WARN("Not recording.\n");
3345 *stateblock = NULL;
3346 return WINED3DERR_INVALIDCALL;
3349 stateblock_init_contained_states(object);
3351 *stateblock = object;
3352 device->recording = NULL;
3353 device->update_state = &device->state;
3355 TRACE("Returning stateblock %p.\n", *stateblock);
3357 return WINED3D_OK;
3360 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3362 /* At the moment we have no need for any functionality at the beginning
3363 * of a scene. */
3364 TRACE("device %p.\n", device);
3366 if (device->inScene)
3368 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3369 return WINED3DERR_INVALIDCALL;
3371 device->inScene = TRUE;
3372 return WINED3D_OK;
3375 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3377 struct wined3d_context *context;
3379 TRACE("device %p.\n", device);
3381 if (!device->inScene)
3383 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3384 return WINED3DERR_INVALIDCALL;
3387 context = context_acquire(device, NULL, 0);
3388 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3389 context->gl_info->gl_ops.gl.p_glFlush();
3390 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3391 * fails. */
3392 context_release(context);
3394 device->inScene = FALSE;
3395 return WINED3D_OK;
3398 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3399 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3401 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3402 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3404 if (!rect_count && rects)
3406 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3407 return WINED3D_OK;
3410 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3412 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3413 if (!ds)
3415 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3416 /* TODO: What about depth stencil buffers without stencil bits? */
3417 return WINED3DERR_INVALIDCALL;
3419 else if (flags & WINED3DCLEAR_TARGET)
3421 if (ds->width < device->fb.render_targets[0]->width
3422 || ds->height < device->fb.render_targets[0]->height)
3424 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3425 return WINED3D_OK;
3430 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3432 return WINED3D_OK;
3435 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3436 struct wined3d_query *predicate, BOOL value)
3438 struct wined3d_query *prev;
3440 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3442 prev = device->update_state->predicate;
3443 if (predicate)
3445 FIXME("Predicated rendering not implemented.\n");
3446 wined3d_query_incref(predicate);
3448 device->update_state->predicate = predicate;
3449 device->update_state->predicate_value = value;
3450 if (!device->recording)
3451 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3452 if (prev)
3453 wined3d_query_decref(prev);
3456 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3458 TRACE("device %p, value %p.\n", device, value);
3460 *value = device->state.predicate_value;
3461 return device->state.predicate;
3464 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3465 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3467 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3468 device, group_count_x, group_count_y, group_count_z);
3470 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3473 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3474 enum wined3d_primitive_type primitive_type)
3476 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3478 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3481 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3482 enum wined3d_primitive_type *primitive_type)
3484 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3486 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3488 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3491 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3493 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3495 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3496 start_vertex, vertex_count, 0, 0, FALSE);
3498 return WINED3D_OK;
3501 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3502 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3504 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3505 device, start_vertex, vertex_count, start_instance, instance_count);
3507 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3508 start_vertex, vertex_count, start_instance, instance_count, FALSE);
3511 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3513 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3515 if (!device->state.index_buffer)
3517 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3518 * without an index buffer set. (The first time at least...)
3519 * D3D8 simply dies, but I doubt it can do much harm to return
3520 * D3DERR_INVALIDCALL there as well. */
3521 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3522 return WINED3DERR_INVALIDCALL;
3525 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type,
3526 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3528 return WINED3D_OK;
3531 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3532 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3534 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3535 device, start_idx, index_count, start_instance, instance_count);
3537 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.base_vertex_index,
3538 start_idx, index_count, start_instance, instance_count, TRUE);
3541 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3542 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3544 unsigned int src_size, dst_size, src_skip_levels = 0;
3545 unsigned int layer_count, level_count, i, j;
3546 unsigned int row_pitch, slice_pitch;
3547 enum wined3d_resource_type type;
3548 struct wined3d_context *context;
3549 struct wined3d_bo_address data;
3550 HRESULT hr;
3551 RECT r;
3553 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3555 /* Verify that the source and destination textures are non-NULL. */
3556 if (!src_texture || !dst_texture)
3558 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3559 return WINED3DERR_INVALIDCALL;
3562 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3564 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3565 return WINED3DERR_INVALIDCALL;
3567 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3569 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3570 return WINED3DERR_INVALIDCALL;
3573 /* Verify that the source and destination textures are the same type. */
3574 type = src_texture->resource.type;
3575 if (dst_texture->resource.type != type)
3577 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3578 return WINED3DERR_INVALIDCALL;
3581 layer_count = src_texture->layer_count;
3582 if (layer_count != dst_texture->layer_count)
3584 WARN("Source and destination have different layer counts.\n");
3585 return WINED3DERR_INVALIDCALL;
3588 if (src_texture->resource.format != dst_texture->resource.format)
3590 WARN("Source and destination formats do not match.\n");
3591 return WINED3DERR_INVALIDCALL;
3594 level_count = min(wined3d_texture_get_level_count(src_texture),
3595 wined3d_texture_get_level_count(dst_texture));
3597 src_size = max(src_texture->resource.width, src_texture->resource.height);
3598 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3599 if (type == WINED3D_RTYPE_TEXTURE_3D)
3601 src_size = max(src_size, src_texture->resource.depth);
3602 dst_size = max(dst_size, dst_texture->resource.depth);
3604 while (src_size > dst_size)
3606 src_size >>= 1;
3607 ++src_skip_levels;
3610 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3611 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3612 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3614 WARN("Source and destination dimensions do not match.\n");
3615 return WINED3DERR_INVALIDCALL;
3618 /* Make sure that the destination texture is loaded. */
3619 context = context_acquire(device, NULL, 0);
3620 wined3d_texture_load(dst_texture, context, FALSE);
3621 context_release(context);
3623 /* Update every surface level of the texture. */
3624 switch (type)
3626 case WINED3D_RTYPE_TEXTURE_2D:
3628 unsigned int src_levels = src_texture->level_count;
3629 unsigned int dst_levels = dst_texture->level_count;
3630 struct wined3d_surface *src_surface;
3631 struct wined3d_surface *dst_surface;
3633 for (i = 0; i < layer_count; ++i)
3635 for (j = 0; j < level_count; ++j)
3637 unsigned int src_sub_resource_idx = i * src_levels + j + src_skip_levels;
3638 unsigned int dst_sub_resource_idx = i * dst_levels + j;
3640 /* Use wined3d_texture_blt() instead of uploading directly if we need conversion. */
3641 if (dst_texture->resource.format->convert
3642 || wined3d_format_get_color_key_conversion(dst_texture, FALSE))
3644 SetRect(&r, 0, 0, wined3d_texture_get_level_width(dst_texture, j),
3645 wined3d_texture_get_level_height(dst_texture, j));
3646 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &r,
3647 src_texture, src_sub_resource_idx, &r, 0, NULL, WINED3D_TEXF_POINT)))
3649 WARN("Failed to update surface, hr %#x.\n", hr);
3650 return hr;
3652 continue;
3655 src_surface = src_texture->sub_resources[src_sub_resource_idx].u.surface;
3656 dst_surface = dst_texture->sub_resources[dst_sub_resource_idx].u.surface;
3657 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3659 WARN("Failed to update surface, hr %#x.\n", hr);
3660 return hr;
3664 return WINED3D_OK;
3667 case WINED3D_RTYPE_TEXTURE_3D:
3668 context = context_acquire(device, NULL, 0);
3670 /* Only a prepare, since we're uploading entire volumes. */
3671 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3672 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3674 for (j = 0; j < level_count; ++j)
3676 if (!wined3d_texture_load_location(src_texture, src_skip_levels + j,
3677 context, src_texture->resource.map_binding))
3679 WARN("Failed to load source sub-resource %u into %s.\n", src_skip_levels + j,
3680 wined3d_debug_location(src_texture->resource.map_binding));
3681 context_release(context);
3682 return WINED3DERR_INVALIDCALL;
3685 wined3d_texture_get_memory(src_texture, src_skip_levels + j, &data, src_texture->resource.map_binding);
3686 wined3d_texture_get_pitch(src_texture, src_skip_levels + j, &row_pitch, &slice_pitch);
3688 wined3d_texture_upload_data(dst_texture, j, context, NULL,
3689 wined3d_const_bo_address(&data), row_pitch, slice_pitch);
3690 wined3d_texture_invalidate_location(dst_texture, j, ~WINED3D_LOCATION_TEXTURE_RGB);
3693 context_release(context);
3694 return WINED3D_OK;
3696 default:
3697 FIXME("Unsupported texture type %#x.\n", type);
3698 return WINED3DERR_INVALIDCALL;
3702 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3704 const struct wined3d_state *state = &device->state;
3705 struct wined3d_texture *texture;
3706 DWORD i;
3708 TRACE("device %p, num_passes %p.\n", device, num_passes);
3710 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3712 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3714 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3715 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3717 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3719 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3720 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3723 texture = state->textures[i];
3724 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3726 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3728 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3729 return E_FAIL;
3731 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3733 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3734 return E_FAIL;
3736 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3737 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3739 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3740 return E_FAIL;
3744 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3745 || state->render_states[WINED3D_RS_STENCILENABLE])
3747 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3748 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3750 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3752 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3753 return WINED3DERR_CONFLICTINGRENDERSTATE;
3757 /* return a sensible default */
3758 *num_passes = 1;
3760 TRACE("returning D3D_OK\n");
3761 return WINED3D_OK;
3764 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3766 static BOOL warned;
3768 TRACE("device %p, software %#x.\n", device, software);
3770 if (!warned)
3772 FIXME("device %p, software %#x stub!\n", device, software);
3773 warned = TRUE;
3776 device->softwareVertexProcessing = software;
3779 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3781 static BOOL warned;
3783 TRACE("device %p.\n", device);
3785 if (!warned)
3787 TRACE("device %p stub!\n", device);
3788 warned = TRUE;
3791 return device->softwareVertexProcessing;
3794 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3795 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3797 struct wined3d_swapchain *swapchain;
3799 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3800 device, swapchain_idx, raster_status);
3802 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3803 return WINED3DERR_INVALIDCALL;
3805 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3808 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3810 static BOOL warned;
3812 TRACE("device %p, segments %.8e.\n", device, segments);
3814 if (segments != 0.0f)
3816 if (!warned)
3818 FIXME("device %p, segments %.8e stub!\n", device, segments);
3819 warned = TRUE;
3823 return WINED3D_OK;
3826 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3828 static BOOL warned;
3830 TRACE("device %p.\n", device);
3832 if (!warned)
3834 FIXME("device %p stub!\n", device);
3835 warned = TRUE;
3838 return 0.0f;
3841 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3842 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3844 struct wined3d_texture *dst_texture, *src_texture;
3845 struct wined3d_box box;
3846 unsigned int i, j;
3848 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3850 if (src_resource == dst_resource)
3852 WARN("Source and destination are the same resource.\n");
3853 return;
3856 if (src_resource->type != dst_resource->type)
3858 WARN("Resource types (%s / %s) don't match.\n",
3859 debug_d3dresourcetype(dst_resource->type),
3860 debug_d3dresourcetype(src_resource->type));
3861 return;
3864 if (src_resource->width != dst_resource->width
3865 || src_resource->height != dst_resource->height
3866 || src_resource->depth != dst_resource->depth)
3868 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3869 dst_resource->width, dst_resource->height, dst_resource->depth,
3870 src_resource->width, src_resource->height, src_resource->depth);
3871 return;
3874 if (src_resource->format->id != dst_resource->format->id)
3876 WARN("Resource formats (%s / %s) don't match.\n",
3877 debug_d3dformat(dst_resource->format->id),
3878 debug_d3dformat(src_resource->format->id));
3879 return;
3882 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3884 wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
3885 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
3886 src_resource, 0, &box, 0, NULL, WINED3D_TEXF_POINT);
3887 return;
3890 dst_texture = texture_from_resource(dst_resource);
3891 src_texture = texture_from_resource(src_resource);
3893 if (src_texture->layer_count != dst_texture->layer_count
3894 || src_texture->level_count != dst_texture->level_count)
3896 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3897 dst_texture->layer_count, dst_texture->level_count,
3898 src_texture->layer_count, src_texture->level_count);
3899 return;
3902 for (i = 0; i < dst_texture->level_count; ++i)
3904 wined3d_box_set(&box, 0, 0,
3905 wined3d_texture_get_level_width(dst_texture, i),
3906 wined3d_texture_get_level_height(dst_texture, i),
3907 0, wined3d_texture_get_level_depth(dst_texture, i));
3908 for (j = 0; j < dst_texture->layer_count; ++j)
3910 unsigned int idx = j * dst_texture->level_count + i;
3912 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
3913 src_resource, idx, &box, 0, NULL, WINED3D_TEXF_POINT);
3918 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3919 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3920 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3921 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3923 struct wined3d_box dst_box, b;
3925 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3926 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3927 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3928 src_resource, src_sub_resource_idx, debug_box(src_box));
3930 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3932 WARN("Source and destination are the same sub-resource.\n");
3933 return WINED3DERR_INVALIDCALL;
3936 if (src_resource->type != dst_resource->type)
3938 WARN("Resource types (%s / %s) don't match.\n",
3939 debug_d3dresourcetype(dst_resource->type),
3940 debug_d3dresourcetype(src_resource->type));
3941 return WINED3DERR_INVALIDCALL;
3944 if (src_resource->format->id != dst_resource->format->id)
3946 WARN("Resource formats (%s / %s) don't match.\n",
3947 debug_d3dformat(dst_resource->format->id),
3948 debug_d3dformat(src_resource->format->id));
3949 return WINED3DERR_INVALIDCALL;
3952 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3954 if (dst_sub_resource_idx)
3956 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3957 return WINED3DERR_INVALIDCALL;
3960 if (src_sub_resource_idx)
3962 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3963 return WINED3DERR_INVALIDCALL;
3966 if (!src_box)
3968 wined3d_box_set(&b, 0, 0, src_resource->size, 1, 0, 1);
3969 src_box = &b;
3971 else if ((src_box->left >= src_box->right
3972 || src_box->top >= src_box->bottom
3973 || src_box->front >= src_box->back))
3975 WARN("Invalid box %s specified.\n", debug_box(src_box));
3976 return WINED3DERR_INVALIDCALL;
3979 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
3980 || src_box->right - src_box->left > dst_resource->size - dst_x)
3982 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
3983 dst_x, src_box->left, src_box->right - src_box->left);
3984 return WINED3DERR_INVALIDCALL;
3987 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
3989 else if (dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
3991 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
3992 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
3993 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
3995 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3997 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
3998 return WINED3DERR_INVALIDCALL;
4001 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4003 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4004 return WINED3DERR_INVALIDCALL;
4007 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4009 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4010 return WINED3DERR_INVALIDCALL;
4013 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4015 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4016 return WINED3DERR_INVALIDCALL;
4019 if (!src_box)
4021 wined3d_box_set(&b, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4022 wined3d_texture_get_level_height(src_texture, src_level), 0, 1);
4023 src_box = &b;
4025 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4027 WARN("Invalid source box %s.\n", debug_box(src_box));
4028 return WINED3DERR_INVALIDCALL;
4031 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4032 dst_y + (src_box->bottom - src_box->top), 0, 1);
4033 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4034 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4036 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4037 return WINED3DERR_INVALIDCALL;
4040 else
4042 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4043 return WINED3DERR_INVALIDCALL;
4046 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4047 src_resource, src_sub_resource_idx, src_box, 0, NULL, WINED3D_TEXF_POINT);
4049 return WINED3D_OK;
4052 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4053 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4054 unsigned int depth_pitch)
4056 unsigned int width, height, depth;
4057 struct wined3d_box b;
4059 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4060 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4062 if (resource->type == WINED3D_RTYPE_BUFFER)
4064 if (sub_resource_idx > 0)
4066 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4067 return;
4070 width = resource->size;
4071 height = 1;
4072 depth = 1;
4074 else if (resource->type == WINED3D_RTYPE_TEXTURE_2D || resource->type == WINED3D_RTYPE_TEXTURE_3D)
4076 struct wined3d_texture *texture = texture_from_resource(resource);
4077 unsigned int level;
4079 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4081 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4082 return;
4085 level = sub_resource_idx % texture->level_count;
4086 width = wined3d_texture_get_level_width(texture, level);
4087 height = wined3d_texture_get_level_height(texture, level);
4088 depth = wined3d_texture_get_level_depth(texture, level);
4090 else
4092 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4093 return;
4096 if (!box)
4098 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4099 box = &b;
4101 else if (box->left >= box->right || box->right > width
4102 || box->top >= box->bottom || box->bottom > height
4103 || box->front >= box->back || box->back > depth)
4105 WARN("Invalid box %s specified.\n", debug_box(box));
4106 return;
4109 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4112 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4113 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4114 const struct wined3d_color *color, float depth, DWORD stencil)
4116 struct wined3d_resource *resource;
4117 RECT r;
4119 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4120 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4122 if (!flags)
4123 return WINED3D_OK;
4125 resource = view->resource;
4126 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4128 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4129 return WINED3DERR_INVALIDCALL;
4132 if (view->depth > 1)
4134 FIXME("Layered clears not implemented.\n");
4135 return WINED3DERR_INVALIDCALL;
4138 if (!rect)
4140 SetRect(&r, 0, 0, view->width, view->height);
4141 rect = &r;
4143 else
4145 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4146 struct wined3d_texture *texture = texture_from_resource(view->resource);
4147 HRESULT hr;
4149 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4150 view->sub_resource_idx % texture->level_count, &b)))
4151 return hr;
4154 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4156 return WINED3D_OK;
4159 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4160 unsigned int view_idx)
4162 TRACE("device %p, view_idx %u.\n", device, view_idx);
4164 if (view_idx >= device->adapter->gl_info.limits.buffers)
4166 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4167 return NULL;
4170 return device->fb.render_targets[view_idx];
4173 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4175 TRACE("device %p.\n", device);
4177 return device->fb.depth_stencil;
4180 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4181 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4183 struct wined3d_rendertarget_view *prev;
4185 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4186 device, view_idx, view, set_viewport);
4188 if (view_idx >= device->adapter->gl_info.limits.buffers)
4190 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4191 return WINED3DERR_INVALIDCALL;
4194 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4196 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4197 return WINED3DERR_INVALIDCALL;
4200 /* Set the viewport and scissor rectangles, if requested. Tests show that
4201 * stateblock recording is ignored, the change goes directly into the
4202 * primary stateblock. */
4203 if (!view_idx && set_viewport)
4205 struct wined3d_state *state = &device->state;
4207 state->viewport.x = 0;
4208 state->viewport.y = 0;
4209 state->viewport.width = view->width;
4210 state->viewport.height = view->height;
4211 state->viewport.min_z = 0.0f;
4212 state->viewport.max_z = 1.0f;
4213 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4215 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4216 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4220 prev = device->fb.render_targets[view_idx];
4221 if (view == prev)
4222 return WINED3D_OK;
4224 if (view)
4225 wined3d_rendertarget_view_incref(view);
4226 device->fb.render_targets[view_idx] = view;
4227 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4228 /* Release after the assignment, to prevent device_resource_released()
4229 * from seeing the surface as still in use. */
4230 if (prev)
4231 wined3d_rendertarget_view_decref(prev);
4233 return WINED3D_OK;
4236 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4238 struct wined3d_rendertarget_view *prev;
4240 TRACE("device %p, view %p.\n", device, view);
4242 prev = device->fb.depth_stencil;
4243 if (prev == view)
4245 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4246 return;
4249 if ((device->fb.depth_stencil = view))
4250 wined3d_rendertarget_view_incref(view);
4251 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4252 if (prev)
4253 wined3d_rendertarget_view_decref(prev);
4256 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4257 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4259 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4260 struct wined3d_sub_resource_data data;
4261 struct wined3d_resource_desc desc;
4262 struct wined3d_map_desc map_desc;
4263 struct wined3d_texture *texture;
4264 HRESULT hr;
4266 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4268 ERR("Failed to map source texture.\n");
4269 return NULL;
4272 data.data = map_desc.data;
4273 data.row_pitch = map_desc.row_pitch;
4274 data.slice_pitch = map_desc.slice_pitch;
4276 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4277 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4278 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4279 desc.multisample_quality = 0;
4280 desc.usage = WINED3DUSAGE_DYNAMIC;
4281 desc.pool = WINED3D_POOL_DEFAULT;
4282 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4283 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4284 desc.depth = 1;
4285 desc.size = 0;
4287 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4288 &data, NULL, &wined3d_null_parent_ops, &texture);
4289 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4290 if (FAILED(hr))
4292 ERR("Failed to create cursor texture.\n");
4293 return NULL;
4296 return texture;
4299 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4300 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4302 unsigned int texture_level = sub_resource_idx % texture->level_count;
4303 unsigned int cursor_width, cursor_height;
4304 struct wined3d_display_mode mode;
4305 struct wined3d_map_desc map_desc;
4306 HRESULT hr;
4308 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4309 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4311 if (sub_resource_idx >= texture->level_count * texture->layer_count
4312 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4313 return WINED3DERR_INVALIDCALL;
4315 if (device->cursor_texture)
4317 wined3d_texture_decref(device->cursor_texture);
4318 device->cursor_texture = NULL;
4321 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4323 WARN("Texture %p has invalid format %s.\n",
4324 texture, debug_d3dformat(texture->resource.format->id));
4325 return WINED3DERR_INVALIDCALL;
4328 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4330 ERR("Failed to get display mode, hr %#x.\n", hr);
4331 return WINED3DERR_INVALIDCALL;
4334 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4335 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4336 if (cursor_width > mode.width || cursor_height > mode.height)
4338 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4339 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4340 return WINED3DERR_INVALIDCALL;
4343 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4345 /* Do not store the surface's pointer because the application may
4346 * release it after setting the cursor image. Windows doesn't
4347 * addref the set surface, so we can't do this either without
4348 * creating circular refcount dependencies. */
4349 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4351 ERR("Failed to create cursor texture.\n");
4352 return WINED3DERR_INVALIDCALL;
4355 if (cursor_width == 32 && cursor_height == 32)
4357 UINT mask_size = cursor_width * cursor_height / 8;
4358 ICONINFO cursor_info;
4359 DWORD *mask_bits;
4360 HCURSOR cursor;
4362 /* 32-bit user32 cursors ignore the alpha channel if it's all
4363 * zeroes, and use the mask instead. Fill the mask with all ones
4364 * to ensure we still get a fully transparent cursor. */
4365 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4366 return E_OUTOFMEMORY;
4367 memset(mask_bits, 0xff, mask_size);
4369 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4370 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4371 cursor_info.fIcon = FALSE;
4372 cursor_info.xHotspot = x_hotspot;
4373 cursor_info.yHotspot = y_hotspot;
4374 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4375 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4376 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4378 /* Create our cursor and clean up. */
4379 cursor = CreateIconIndirect(&cursor_info);
4380 if (cursor_info.hbmMask)
4381 DeleteObject(cursor_info.hbmMask);
4382 if (cursor_info.hbmColor)
4383 DeleteObject(cursor_info.hbmColor);
4384 if (device->hardwareCursor)
4385 DestroyCursor(device->hardwareCursor);
4386 device->hardwareCursor = cursor;
4387 if (device->bCursorVisible)
4388 SetCursor(cursor);
4390 HeapFree(GetProcessHeap(), 0, mask_bits);
4393 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4394 device->cursorWidth = cursor_width;
4395 device->cursorHeight = cursor_height;
4396 device->xHotSpot = x_hotspot;
4397 device->yHotSpot = y_hotspot;
4399 return WINED3D_OK;
4402 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4403 int x_screen_space, int y_screen_space, DWORD flags)
4405 TRACE("device %p, x %d, y %d, flags %#x.\n",
4406 device, x_screen_space, y_screen_space, flags);
4408 device->xScreenSpace = x_screen_space;
4409 device->yScreenSpace = y_screen_space;
4411 if (device->hardwareCursor)
4413 POINT pt;
4415 GetCursorPos( &pt );
4416 if (x_screen_space == pt.x && y_screen_space == pt.y)
4417 return;
4418 SetCursorPos( x_screen_space, y_screen_space );
4420 /* Switch to the software cursor if position diverges from the hardware one. */
4421 GetCursorPos( &pt );
4422 if (x_screen_space != pt.x || y_screen_space != pt.y)
4424 if (device->bCursorVisible) SetCursor( NULL );
4425 DestroyCursor( device->hardwareCursor );
4426 device->hardwareCursor = 0;
4431 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4433 BOOL oldVisible = device->bCursorVisible;
4435 TRACE("device %p, show %#x.\n", device, show);
4438 * When ShowCursor is first called it should make the cursor appear at the OS's last
4439 * known cursor position.
4441 if (show && !oldVisible)
4443 POINT pt;
4444 GetCursorPos(&pt);
4445 device->xScreenSpace = pt.x;
4446 device->yScreenSpace = pt.y;
4449 if (device->hardwareCursor)
4451 device->bCursorVisible = show;
4452 if (show)
4453 SetCursor(device->hardwareCursor);
4454 else
4455 SetCursor(NULL);
4457 else if (device->cursor_texture)
4459 device->bCursorVisible = show;
4462 return oldVisible;
4465 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4467 struct wined3d_resource *resource, *cursor;
4469 TRACE("device %p.\n", device);
4471 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4473 TRACE("Checking resource %p for eviction.\n", resource);
4475 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4477 TRACE("Evicting %p.\n", resource);
4478 wined3d_cs_emit_unload_resource(device->cs, resource);
4483 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4484 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4485 wined3d_device_reset_cb callback, BOOL reset_state)
4487 struct wined3d_resource *resource, *cursor;
4488 struct wined3d_swapchain *swapchain;
4489 struct wined3d_view_desc view_desc;
4490 BOOL backbuffer_resized;
4491 HRESULT hr = WINED3D_OK;
4492 unsigned int i;
4494 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4495 device, swapchain_desc, mode, callback, reset_state);
4497 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4499 ERR("Failed to get the first implicit swapchain.\n");
4500 return WINED3DERR_INVALIDCALL;
4503 if (reset_state)
4505 if (device->logo_texture)
4507 wined3d_texture_decref(device->logo_texture);
4508 device->logo_texture = NULL;
4510 if (device->cursor_texture)
4512 wined3d_texture_decref(device->cursor_texture);
4513 device->cursor_texture = NULL;
4515 state_unbind_resources(&device->state);
4518 if (device->fb.render_targets)
4520 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4522 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4525 wined3d_device_set_depth_stencil_view(device, NULL);
4527 if (reset_state)
4529 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4531 TRACE("Enumerating resource %p.\n", resource);
4532 if (FAILED(hr = callback(resource)))
4533 return hr;
4537 TRACE("New params:\n");
4538 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4539 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4540 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4541 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4542 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4543 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4544 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4545 TRACE("device_window %p\n", swapchain_desc->device_window);
4546 TRACE("windowed %#x\n", swapchain_desc->windowed);
4547 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4548 if (swapchain_desc->enable_auto_depth_stencil)
4549 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4550 TRACE("flags %#x\n", swapchain_desc->flags);
4551 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4552 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4553 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4555 /* No special treatment of these parameters. Just store them */
4556 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4557 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4558 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4559 swapchain->desc.flags = swapchain_desc->flags;
4560 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4561 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4562 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4564 if (swapchain_desc->device_window
4565 && swapchain_desc->device_window != swapchain->desc.device_window)
4567 TRACE("Changing the device window from %p to %p.\n",
4568 swapchain->desc.device_window, swapchain_desc->device_window);
4569 swapchain->desc.device_window = swapchain_desc->device_window;
4570 swapchain->device_window = swapchain_desc->device_window;
4571 wined3d_swapchain_set_window(swapchain, NULL);
4574 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4575 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4577 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4578 || swapchain->reapply_mode || mode
4579 || (!swapchain_desc->windowed && backbuffer_resized))
4581 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4582 return hr;
4584 else if (!swapchain_desc->windowed)
4586 DWORD style = device->style;
4587 DWORD exStyle = device->exStyle;
4588 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4589 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4590 * Reset to clear up their mess. Guild Wars also loses the device during that.
4592 device->style = 0;
4593 device->exStyle = 0;
4594 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4595 swapchain_desc->backbuffer_width,
4596 swapchain_desc->backbuffer_height);
4597 device->style = style;
4598 device->exStyle = exStyle;
4601 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4602 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4603 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4604 return hr;
4606 if (device->auto_depth_stencil_view)
4608 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4609 device->auto_depth_stencil_view = NULL;
4611 if (swapchain->desc.enable_auto_depth_stencil)
4613 struct wined3d_resource_desc texture_desc;
4614 struct wined3d_texture *texture;
4615 DWORD flags = 0;
4617 TRACE("Creating the depth stencil buffer.\n");
4619 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4620 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4621 texture_desc.multisample_type = swapchain->desc.multisample_type;
4622 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4623 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4624 texture_desc.pool = WINED3D_POOL_DEFAULT;
4625 texture_desc.width = swapchain->desc.backbuffer_width;
4626 texture_desc.height = swapchain->desc.backbuffer_height;
4627 texture_desc.depth = 1;
4628 texture_desc.size = 0;
4630 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4631 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4633 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4634 device->device_parent, &texture_desc, flags, &texture)))
4636 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4637 return WINED3DERR_INVALIDCALL;
4640 view_desc.format_id = texture->resource.format->id;
4641 view_desc.flags = 0;
4642 view_desc.u.texture.level_idx = 0;
4643 view_desc.u.texture.level_count = 1;
4644 view_desc.u.texture.layer_idx = 0;
4645 view_desc.u.texture.layer_count = 1;
4646 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4647 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4648 wined3d_texture_decref(texture);
4649 if (FAILED(hr))
4651 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4652 return hr;
4655 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4658 if (device->back_buffer_view)
4660 wined3d_rendertarget_view_decref(device->back_buffer_view);
4661 device->back_buffer_view = NULL;
4663 if (swapchain->desc.backbuffer_count)
4665 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4667 view_desc.format_id = back_buffer->format->id;
4668 view_desc.flags = 0;
4669 view_desc.u.texture.level_idx = 0;
4670 view_desc.u.texture.level_count = 1;
4671 view_desc.u.texture.layer_idx = 0;
4672 view_desc.u.texture.layer_count = 1;
4673 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4674 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4676 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4677 return hr;
4681 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4683 if (reset_state)
4685 TRACE("Resetting stateblock.\n");
4686 if (device->recording)
4688 wined3d_stateblock_decref(device->recording);
4689 device->recording = NULL;
4691 wined3d_cs_emit_reset_state(device->cs);
4692 state_cleanup(&device->state);
4694 if (device->d3d_initialized)
4695 wined3d_device_delete_opengl_contexts(device);
4697 memset(&device->state, 0, sizeof(device->state));
4698 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4699 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4700 device->update_state = &device->state;
4702 device_init_swapchain_state(device, swapchain);
4703 if (wined3d_settings.logo)
4704 device_load_logo(device, wined3d_settings.logo);
4706 else if (device->back_buffer_view)
4708 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4709 struct wined3d_state *state = &device->state;
4711 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4713 /* Note the min_z / max_z is not reset. */
4714 state->viewport.x = 0;
4715 state->viewport.y = 0;
4716 state->viewport.width = view->width;
4717 state->viewport.height = view->height;
4718 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4720 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4721 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4724 if (device->d3d_initialized)
4726 if (reset_state)
4727 hr = wined3d_device_create_primary_opengl_context(device);
4728 swapchain_update_swap_interval(swapchain);
4731 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4732 * first use
4734 return hr;
4737 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4739 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4741 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4743 return WINED3D_OK;
4747 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4748 struct wined3d_device_creation_parameters *parameters)
4750 TRACE("device %p, parameters %p.\n", device, parameters);
4752 *parameters = device->create_parms;
4755 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4757 TRACE("device %p.\n", device);
4759 return device->wined3d;
4762 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4763 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4765 struct wined3d_swapchain *swapchain;
4767 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4768 device, swapchain_idx, flags, ramp);
4770 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4771 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4774 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4775 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4777 struct wined3d_swapchain *swapchain;
4779 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4780 device, swapchain_idx, ramp);
4782 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4783 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4786 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4788 TRACE("device %p, resource %p.\n", device, resource);
4790 list_add_head(&device->resources, &resource->resource_list_entry);
4793 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4795 TRACE("device %p, resource %p.\n", device, resource);
4797 list_remove(&resource->resource_list_entry);
4800 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4802 enum wined3d_resource_type type = resource->type;
4803 struct wined3d_rendertarget_view *rtv;
4804 unsigned int i;
4806 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4808 if (device->d3d_initialized)
4810 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4812 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4813 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4816 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4817 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4820 switch (type)
4822 case WINED3D_RTYPE_TEXTURE_2D:
4823 case WINED3D_RTYPE_TEXTURE_3D:
4824 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4826 struct wined3d_texture *texture = texture_from_resource(resource);
4828 if (device->state.textures[i] == texture)
4830 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4831 device->state.textures[i] = NULL;
4834 if (device->recording && device->update_state->textures[i] == texture)
4836 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4837 texture, device->recording, i);
4838 device->update_state->textures[i] = NULL;
4841 break;
4843 case WINED3D_RTYPE_BUFFER:
4845 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4847 for (i = 0; i < MAX_STREAMS; ++i)
4849 if (device->state.streams[i].buffer == buffer)
4851 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4852 device->state.streams[i].buffer = NULL;
4855 if (device->recording && device->update_state->streams[i].buffer == buffer)
4857 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4858 buffer, device->recording, i);
4859 device->update_state->streams[i].buffer = NULL;
4863 if (device->state.index_buffer == buffer)
4865 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4866 device->state.index_buffer = NULL;
4869 if (device->recording && device->update_state->index_buffer == buffer)
4871 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4872 buffer, device->recording);
4873 device->update_state->index_buffer = NULL;
4876 break;
4878 default:
4879 break;
4882 /* Remove the resource from the resourceStore */
4883 device_resource_remove(device, resource);
4885 TRACE("Resource released.\n");
4888 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4890 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4892 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4895 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4896 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4897 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4899 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4900 const struct fragment_pipeline *fragment_pipeline;
4901 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4902 unsigned int i;
4903 HRESULT hr;
4905 device->ref = 1;
4906 device->wined3d = wined3d;
4907 wined3d_incref(device->wined3d);
4908 device->adapter = wined3d->adapter_count ? adapter : NULL;
4909 device->device_parent = device_parent;
4910 list_init(&device->resources);
4911 list_init(&device->shaders);
4912 device->surface_alignment = surface_alignment;
4914 /* Save the creation parameters. */
4915 device->create_parms.adapter_idx = adapter_idx;
4916 device->create_parms.device_type = device_type;
4917 device->create_parms.focus_window = focus_window;
4918 device->create_parms.flags = flags;
4920 device->shader_backend = adapter->shader_backend;
4922 vertex_pipeline = adapter->vertex_pipe;
4924 fragment_pipeline = adapter->fragment_pipe;
4926 wine_rb_init(&device->samplers, wined3d_sampler_compare);
4928 if (vertex_pipeline->vp_states && fragment_pipeline->states
4929 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4930 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4931 fragment_pipeline, misc_state_template)))
4933 ERR("Failed to compile state table, hr %#x.\n", hr);
4934 wine_rb_destroy(&device->samplers, NULL, NULL);
4935 wined3d_decref(device->wined3d);
4936 return hr;
4939 state_init(&device->state, &device->fb, &adapter->gl_info,
4940 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4941 device->update_state = &device->state;
4943 if (!(device->cs = wined3d_cs_create(device)))
4945 WARN("Failed to create command stream.\n");
4946 state_cleanup(&device->state);
4947 hr = E_FAIL;
4948 goto err;
4951 return WINED3D_OK;
4953 err:
4954 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4956 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4958 wine_rb_destroy(&device->samplers, NULL, NULL);
4959 wined3d_decref(device->wined3d);
4960 return hr;
4963 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4965 DWORD rep = device->StateTable[state].representative;
4966 struct wined3d_context *context;
4967 DWORD idx;
4968 BYTE shift;
4969 UINT i;
4971 if (STATE_IS_COMPUTE(state))
4973 for (i = 0; i < device->context_count; ++i)
4974 context_invalidate_compute_state(device->contexts[i], state);
4975 return;
4978 for (i = 0; i < device->context_count; ++i)
4980 context = device->contexts[i];
4981 if(isStateDirty(context, rep)) continue;
4983 context->dirtyArray[context->numDirtyEntries++] = rep;
4984 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4985 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4986 context->isStateDirty[idx] |= (1u << shift);
4990 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4991 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4993 if (device->filter_messages && message != WM_DISPLAYCHANGE)
4995 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4996 window, message, wparam, lparam);
4997 if (unicode)
4998 return DefWindowProcW(window, message, wparam, lparam);
4999 else
5000 return DefWindowProcA(window, message, wparam, lparam);
5003 if (message == WM_DESTROY)
5005 TRACE("unregister window %p.\n", window);
5006 wined3d_unregister_window(window);
5008 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5009 ERR("Window %p is not the focus window for device %p.\n", window, device);
5011 else if (message == WM_DISPLAYCHANGE)
5013 device->device_parent->ops->mode_changed(device->device_parent);
5015 else if (message == WM_ACTIVATEAPP)
5017 UINT i;
5019 for (i = 0; i < device->swapchain_count; i++)
5020 wined3d_swapchain_activate(device->swapchains[i], wparam);
5022 device->device_parent->ops->activate(device->device_parent, wparam);
5024 else if (message == WM_SYSCOMMAND)
5026 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5028 if (unicode)
5029 DefWindowProcW(window, message, wparam, lparam);
5030 else
5031 DefWindowProcA(window, message, wparam, lparam);
5035 if (unicode)
5036 return CallWindowProcW(proc, window, message, wparam, lparam);
5037 else
5038 return CallWindowProcA(proc, window, message, wparam, lparam);