wined3d: Introduce wined3d_device_get_cs_uav().
[wine.git] / dlls / wined3d / device.c
blob4cf038404edf6e62025709f0990449a302754fe7
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_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2659 struct wined3d_shader *prev;
2661 TRACE("device %p, shader %p.\n", device, shader);
2663 prev = device->update_state->shader[WINED3D_SHADER_TYPE_HULL];
2664 if (shader == prev)
2665 return;
2666 if (shader)
2667 wined3d_shader_incref(shader);
2668 device->update_state->shader[WINED3D_SHADER_TYPE_HULL] = shader;
2669 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_HULL, shader);
2670 if (prev)
2671 wined3d_shader_decref(prev);
2674 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2676 TRACE("device %p.\n", device);
2678 return device->state.shader[WINED3D_SHADER_TYPE_HULL];
2681 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2683 struct wined3d_shader *prev;
2685 TRACE("device %p, shader %p.\n", device, shader);
2687 prev = device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2688 if (shader == prev)
2689 return;
2690 if (shader)
2691 wined3d_shader_incref(shader);
2692 device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN] = shader;
2693 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_DOMAIN, shader);
2694 if (prev)
2695 wined3d_shader_decref(prev);
2698 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2700 TRACE("device %p.\n", device);
2702 return device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2705 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2707 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2709 TRACE("device %p, shader %p.\n", device, shader);
2711 if (device->recording || shader == prev)
2712 return;
2713 if (shader)
2714 wined3d_shader_incref(shader);
2715 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2716 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2717 if (prev)
2718 wined3d_shader_decref(prev);
2721 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2723 TRACE("device %p.\n", device);
2725 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2728 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2730 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2732 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2735 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2737 TRACE("device %p, idx %u.\n", device, idx);
2739 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2742 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2743 UINT idx, struct wined3d_shader_resource_view *view)
2745 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2747 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2750 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2751 UINT idx)
2753 TRACE("device %p, idx %u.\n", device, idx);
2755 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2758 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2760 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2762 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2765 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2767 TRACE("device %p, idx %u.\n", device, idx);
2769 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2772 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2774 struct wined3d_shader *prev;
2776 TRACE("device %p, shader %p.\n", device, shader);
2778 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2779 if (device->recording || shader == prev)
2780 return;
2781 if (shader)
2782 wined3d_shader_incref(shader);
2783 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2784 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2785 if (prev)
2786 wined3d_shader_decref(prev);
2789 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2791 TRACE("device %p.\n", device);
2793 return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2796 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2798 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2800 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2803 struct wined3d_buffer * CDECL wined3d_device_get_cs_cb(const struct wined3d_device *device, unsigned int idx)
2805 TRACE("device %p, idx %u.\n", device, idx);
2807 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2810 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2811 unsigned int idx, struct wined3d_shader_resource_view *view)
2813 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2815 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2818 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2819 unsigned int idx)
2821 TRACE("device %p, idx %u.\n", device, idx);
2823 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2826 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2827 unsigned int idx, struct wined3d_sampler *sampler)
2829 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2831 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2834 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2836 TRACE("device %p, idx %u.\n", device, idx);
2838 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2841 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2842 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav)
2844 struct wined3d_unordered_access_view *prev;
2846 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2848 WARN("Invalid UAV index %u.\n", idx);
2849 return;
2852 prev = device->update_state->unordered_access_view[pipeline][idx];
2853 if (uav == prev)
2854 return;
2856 if (uav)
2857 wined3d_unordered_access_view_incref(uav);
2858 device->update_state->unordered_access_view[pipeline][idx] = uav;
2859 if (!device->recording)
2860 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav);
2861 if (prev)
2862 wined3d_unordered_access_view_decref(prev);
2865 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2866 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2868 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2870 WARN("Invalid UAV index %u.\n", idx);
2871 return NULL;
2874 return device->state.unordered_access_view[pipeline][idx];
2877 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2878 struct wined3d_unordered_access_view *uav)
2880 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2882 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav);
2885 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
2886 unsigned int idx)
2888 TRACE("device %p, idx %u.\n", device, idx);
2890 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
2893 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2894 unsigned int idx, struct wined3d_unordered_access_view *uav)
2896 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2898 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav);
2901 /* Context activation is done by the caller. */
2902 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2903 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2904 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2905 DWORD DestFVF)
2907 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2908 struct wined3d_map_desc map_desc;
2909 struct wined3d_box box = {0};
2910 struct wined3d_viewport vp;
2911 UINT vertex_size;
2912 unsigned int i;
2913 BYTE *dest_ptr;
2914 BOOL doClip;
2915 DWORD numTextures;
2916 HRESULT hr;
2918 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2920 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2923 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2925 ERR("Source has no position mask\n");
2926 return WINED3DERR_INVALIDCALL;
2929 if (device->state.render_states[WINED3D_RS_CLIPPING])
2931 static BOOL warned = FALSE;
2933 * The clipping code is not quite correct. Some things need
2934 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2935 * so disable clipping for now.
2936 * (The graphics in Half-Life are broken, and my processvertices
2937 * test crashes with IDirect3DDevice3)
2938 doClip = TRUE;
2940 doClip = FALSE;
2941 if(!warned) {
2942 warned = TRUE;
2943 FIXME("Clipping is broken and disabled for now\n");
2946 else
2947 doClip = FALSE;
2949 vertex_size = get_flexible_vertex_size(DestFVF);
2950 box.left = dwDestIndex * vertex_size;
2951 box.right = box.left + dwCount * vertex_size;
2952 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2954 WARN("Failed to map buffer, hr %#x.\n", hr);
2955 return hr;
2957 dest_ptr = map_desc.data;
2959 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2960 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2961 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2963 TRACE("View mat:\n");
2964 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2965 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2966 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2967 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2969 TRACE("Proj mat:\n");
2970 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2971 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2972 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2973 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2975 TRACE("World mat:\n");
2976 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2977 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2978 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2979 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2981 /* Get the viewport */
2982 wined3d_device_get_viewport(device, &vp);
2983 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2984 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2986 multiply_matrix(&mat,&view_mat,&world_mat);
2987 multiply_matrix(&mat,&proj_mat,&mat);
2989 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2991 for (i = 0; i < dwCount; i+= 1) {
2992 unsigned int tex_index;
2994 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2995 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2996 /* The position first */
2997 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2998 const float *p = (const float *)(element->data.addr + i * element->stride);
2999 float x, y, z, rhw;
3000 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3002 /* Multiplication with world, view and projection matrix. */
3003 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3004 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3005 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3006 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3008 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3010 /* WARNING: The following things are taken from d3d7 and were not yet checked
3011 * against d3d8 or d3d9!
3014 /* Clipping conditions: From msdn
3016 * A vertex is clipped if it does not match the following requirements
3017 * -rhw < x <= rhw
3018 * -rhw < y <= rhw
3019 * 0 < z <= rhw
3020 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3022 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3023 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3027 if( !doClip ||
3028 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3029 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3030 ( rhw > eps ) ) ) {
3032 /* "Normal" viewport transformation (not clipped)
3033 * 1) The values are divided by rhw
3034 * 2) The y axis is negative, so multiply it with -1
3035 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3036 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3037 * 4) Multiply x with Width/2 and add Width/2
3038 * 5) The same for the height
3039 * 6) Add the viewpoint X and Y to the 2D coordinates and
3040 * The minimum Z value to z
3041 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3043 * Well, basically it's simply a linear transformation into viewport
3044 * coordinates
3047 x /= rhw;
3048 y /= rhw;
3049 z /= rhw;
3051 y *= -1;
3053 x *= vp.width / 2;
3054 y *= vp.height / 2;
3055 z *= vp.max_z - vp.min_z;
3057 x += vp.width / 2 + vp.x;
3058 y += vp.height / 2 + vp.y;
3059 z += vp.min_z;
3061 rhw = 1 / rhw;
3062 } else {
3063 /* That vertex got clipped
3064 * Contrary to OpenGL it is not dropped completely, it just
3065 * undergoes a different calculation.
3067 TRACE("Vertex got clipped\n");
3068 x += rhw;
3069 y += rhw;
3071 x /= 2;
3072 y /= 2;
3074 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3075 * outside of the main vertex buffer memory. That needs some more
3076 * investigation...
3080 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3083 ( (float *) dest_ptr)[0] = x;
3084 ( (float *) dest_ptr)[1] = y;
3085 ( (float *) dest_ptr)[2] = z;
3086 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3088 dest_ptr += 3 * sizeof(float);
3090 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3091 dest_ptr += sizeof(float);
3094 if (DestFVF & WINED3DFVF_PSIZE)
3095 dest_ptr += sizeof(DWORD);
3097 if (DestFVF & WINED3DFVF_NORMAL)
3099 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3100 const float *normal = (const float *)(element->data.addr + i * element->stride);
3101 /* AFAIK this should go into the lighting information */
3102 FIXME("Didn't expect the destination to have a normal\n");
3103 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3106 if (DestFVF & WINED3DFVF_DIFFUSE)
3108 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3109 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3110 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3112 static BOOL warned = FALSE;
3114 if(!warned) {
3115 ERR("No diffuse color in source, but destination has one\n");
3116 warned = TRUE;
3119 *( (DWORD *) dest_ptr) = 0xffffffff;
3120 dest_ptr += sizeof(DWORD);
3122 else
3124 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3128 if (DestFVF & WINED3DFVF_SPECULAR)
3130 /* What's the color value in the feedback buffer? */
3131 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3132 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3133 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3135 static BOOL warned = FALSE;
3137 if(!warned) {
3138 ERR("No specular color in source, but destination has one\n");
3139 warned = TRUE;
3142 *(DWORD *)dest_ptr = 0xff000000;
3143 dest_ptr += sizeof(DWORD);
3145 else
3147 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3151 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3153 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3154 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3155 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3157 ERR("No source texture, but destination requests one\n");
3158 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3160 else
3162 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3167 wined3d_resource_unmap(&dest->resource, 0);
3169 return WINED3D_OK;
3171 #undef copy_and_next
3173 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3174 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3175 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3177 struct wined3d_state *state = &device->state;
3178 struct wined3d_stream_info stream_info;
3179 struct wined3d_resource *resource;
3180 struct wined3d_box box = {0};
3181 struct wined3d_shader *vs;
3182 unsigned int i;
3183 HRESULT hr;
3184 WORD map;
3186 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3187 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3188 device, src_start_idx, dst_idx, vertex_count,
3189 dst_buffer, declaration, flags, dst_fvf);
3191 if (declaration)
3192 FIXME("Output vertex declaration not implemented yet.\n");
3194 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3195 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3196 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->gl_info, &device->adapter->d3d_info);
3197 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3199 /* We can't convert FROM a VBO, and vertex buffers used to source into
3200 * process_vertices() are unlikely to ever be used for drawing. Release
3201 * VBOs in those buffers and fix up the stream_info structure.
3203 * Also apply the start index. */
3204 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3206 struct wined3d_stream_info_element *e;
3207 struct wined3d_map_desc map_desc;
3209 if (!(map & 1))
3210 continue;
3212 e = &stream_info.elements[i];
3213 resource = &state->streams[e->stream_idx].buffer->resource;
3214 box.left = src_start_idx * e->stride;
3215 box.right = box.left + vertex_count * e->stride;
3216 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READONLY)))
3217 ERR("Failed to map resource.\n");
3218 e->data.buffer_object = 0;
3219 e->data.addr += (ULONG_PTR)map_desc.data;
3222 hr = process_vertices_strided(device, dst_idx, vertex_count,
3223 &stream_info, dst_buffer, flags, dst_fvf);
3225 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3227 if (!(map & 1))
3228 continue;
3230 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3231 if (FAILED(wined3d_resource_unmap(resource, 0)))
3232 ERR("Failed to unmap resource.\n");
3235 return hr;
3238 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3239 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3241 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3242 DWORD old_value;
3244 TRACE("device %p, stage %u, state %s, value %#x.\n",
3245 device, stage, debug_d3dtexturestate(state), value);
3247 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3249 WARN("Invalid state %#x passed.\n", state);
3250 return;
3253 if (stage >= d3d_info->limits.ffp_blend_stages)
3255 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3256 stage, d3d_info->limits.ffp_blend_stages - 1);
3257 return;
3260 old_value = device->update_state->texture_states[stage][state];
3261 device->update_state->texture_states[stage][state] = value;
3263 if (device->recording)
3265 TRACE("Recording... not performing anything.\n");
3266 device->recording->changed.textureState[stage] |= 1u << state;
3267 return;
3270 /* Checked after the assignments to allow proper stateblock recording. */
3271 if (old_value == value)
3273 TRACE("Application is setting the old value over, nothing to do.\n");
3274 return;
3277 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3280 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3281 UINT stage, enum wined3d_texture_stage_state state)
3283 TRACE("device %p, stage %u, state %s.\n",
3284 device, stage, debug_d3dtexturestate(state));
3286 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3288 WARN("Invalid state %#x passed.\n", state);
3289 return 0;
3292 return device->state.texture_states[stage][state];
3295 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3296 UINT stage, struct wined3d_texture *texture)
3298 struct wined3d_texture *prev;
3300 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3302 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3303 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3305 /* Windows accepts overflowing this array... we do not. */
3306 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3308 WARN("Ignoring invalid stage %u.\n", stage);
3309 return WINED3D_OK;
3312 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3314 WARN("Rejecting attempt to set scratch texture.\n");
3315 return WINED3DERR_INVALIDCALL;
3318 if (device->recording)
3319 device->recording->changed.textures |= 1u << stage;
3321 prev = device->update_state->textures[stage];
3322 TRACE("Previous texture %p.\n", prev);
3324 if (texture == prev)
3326 TRACE("App is setting the same texture again, nothing to do.\n");
3327 return WINED3D_OK;
3330 TRACE("Setting new texture to %p.\n", texture);
3331 device->update_state->textures[stage] = texture;
3333 if (texture)
3334 wined3d_texture_incref(texture);
3335 if (!device->recording)
3336 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3337 if (prev)
3338 wined3d_texture_decref(prev);
3340 return WINED3D_OK;
3343 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3345 TRACE("device %p, stage %u.\n", device, stage);
3347 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3348 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3350 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3352 WARN("Ignoring invalid stage %u.\n", stage);
3353 return NULL; /* Windows accepts overflowing this array ... we do not. */
3356 return device->state.textures[stage];
3359 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3361 TRACE("device %p, caps %p.\n", device, caps);
3363 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3364 device->create_parms.device_type, caps);
3367 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3368 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3370 struct wined3d_swapchain *swapchain;
3372 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3373 device, swapchain_idx, mode, rotation);
3375 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3376 return WINED3DERR_INVALIDCALL;
3378 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3381 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3383 struct wined3d_stateblock *stateblock;
3384 HRESULT hr;
3386 TRACE("device %p.\n", device);
3388 if (device->recording)
3389 return WINED3DERR_INVALIDCALL;
3391 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3392 if (FAILED(hr))
3393 return hr;
3395 device->recording = stateblock;
3396 device->update_state = &stateblock->state;
3398 TRACE("Recording stateblock %p.\n", stateblock);
3400 return WINED3D_OK;
3403 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3404 struct wined3d_stateblock **stateblock)
3406 struct wined3d_stateblock *object = device->recording;
3408 TRACE("device %p, stateblock %p.\n", device, stateblock);
3410 if (!device->recording)
3412 WARN("Not recording.\n");
3413 *stateblock = NULL;
3414 return WINED3DERR_INVALIDCALL;
3417 stateblock_init_contained_states(object);
3419 *stateblock = object;
3420 device->recording = NULL;
3421 device->update_state = &device->state;
3423 TRACE("Returning stateblock %p.\n", *stateblock);
3425 return WINED3D_OK;
3428 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3430 /* At the moment we have no need for any functionality at the beginning
3431 * of a scene. */
3432 TRACE("device %p.\n", device);
3434 if (device->inScene)
3436 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3437 return WINED3DERR_INVALIDCALL;
3439 device->inScene = TRUE;
3440 return WINED3D_OK;
3443 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3445 TRACE("device %p.\n", device);
3447 if (!device->inScene)
3449 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3450 return WINED3DERR_INVALIDCALL;
3453 wined3d_cs_emit_flush(device->cs);
3455 device->inScene = FALSE;
3456 return WINED3D_OK;
3459 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3460 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3462 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3463 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3465 if (!rect_count && rects)
3467 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3468 return WINED3D_OK;
3471 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3473 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3474 if (!ds)
3476 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3477 /* TODO: What about depth stencil buffers without stencil bits? */
3478 return WINED3DERR_INVALIDCALL;
3480 else if (flags & WINED3DCLEAR_TARGET)
3482 if (ds->width < device->fb.render_targets[0]->width
3483 || ds->height < device->fb.render_targets[0]->height)
3485 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3486 return WINED3D_OK;
3491 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3493 return WINED3D_OK;
3496 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3497 struct wined3d_query *predicate, BOOL value)
3499 struct wined3d_query *prev;
3501 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3503 prev = device->update_state->predicate;
3504 if (predicate)
3506 FIXME("Predicated rendering not implemented.\n");
3507 wined3d_query_incref(predicate);
3509 device->update_state->predicate = predicate;
3510 device->update_state->predicate_value = value;
3511 if (!device->recording)
3512 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3513 if (prev)
3514 wined3d_query_decref(prev);
3517 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3519 TRACE("device %p, value %p.\n", device, value);
3521 *value = device->state.predicate_value;
3522 return device->state.predicate;
3525 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3526 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3528 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3529 device, group_count_x, group_count_y, group_count_z);
3531 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3534 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3535 enum wined3d_primitive_type primitive_type)
3537 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3539 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3542 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3543 enum wined3d_primitive_type *primitive_type)
3545 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3547 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3549 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3552 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3554 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3556 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3557 start_vertex, vertex_count, 0, 0, FALSE);
3559 return WINED3D_OK;
3562 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3563 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3565 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3566 device, start_vertex, vertex_count, start_instance, instance_count);
3568 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3569 start_vertex, vertex_count, start_instance, instance_count, FALSE);
3572 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3574 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3576 if (!device->state.index_buffer)
3578 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3579 * without an index buffer set. (The first time at least...)
3580 * D3D8 simply dies, but I doubt it can do much harm to return
3581 * D3DERR_INVALIDCALL there as well. */
3582 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3583 return WINED3DERR_INVALIDCALL;
3586 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type,
3587 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3589 return WINED3D_OK;
3592 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3593 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3595 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3596 device, start_idx, index_count, start_instance, instance_count);
3598 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.base_vertex_index,
3599 start_idx, index_count, start_instance, instance_count, TRUE);
3602 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3603 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3605 unsigned int src_size, dst_size, src_skip_levels = 0;
3606 unsigned int src_level_count, dst_level_count;
3607 unsigned int layer_count, level_count, i, j;
3608 unsigned int width, height, depth;
3609 enum wined3d_resource_type type;
3610 struct wined3d_box box;
3612 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3614 /* Verify that the source and destination textures are non-NULL. */
3615 if (!src_texture || !dst_texture)
3617 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3618 return WINED3DERR_INVALIDCALL;
3621 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3623 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3624 return WINED3DERR_INVALIDCALL;
3626 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3628 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3629 return WINED3DERR_INVALIDCALL;
3632 /* Verify that the source and destination textures are the same type. */
3633 type = src_texture->resource.type;
3634 if (dst_texture->resource.type != type)
3636 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3637 return WINED3DERR_INVALIDCALL;
3640 layer_count = src_texture->layer_count;
3641 if (layer_count != dst_texture->layer_count)
3643 WARN("Source and destination have different layer counts.\n");
3644 return WINED3DERR_INVALIDCALL;
3647 if (src_texture->resource.format != dst_texture->resource.format)
3649 WARN("Source and destination formats do not match.\n");
3650 return WINED3DERR_INVALIDCALL;
3653 src_level_count = src_texture->level_count;
3654 dst_level_count = dst_texture->level_count;
3655 level_count = min(src_level_count, dst_level_count);
3657 src_size = max(src_texture->resource.width, src_texture->resource.height);
3658 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3659 if (type == WINED3D_RTYPE_TEXTURE_3D)
3661 src_size = max(src_size, src_texture->resource.depth);
3662 dst_size = max(dst_size, dst_texture->resource.depth);
3664 while (src_size > dst_size)
3666 src_size >>= 1;
3667 ++src_skip_levels;
3670 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3671 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3672 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3674 WARN("Source and destination dimensions do not match.\n");
3675 return WINED3DERR_INVALIDCALL;
3678 /* Update every surface level of the texture. */
3679 for (i = 0; i < level_count; ++i)
3681 width = wined3d_texture_get_level_width(dst_texture, i);
3682 height = wined3d_texture_get_level_height(dst_texture, i);
3683 depth = wined3d_texture_get_level_depth(dst_texture, i);
3684 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3686 for (j = 0; j < layer_count; ++j)
3688 wined3d_cs_emit_blt_sub_resource(device->cs,
3689 &dst_texture->resource, j * dst_level_count + i, &box,
3690 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3691 0, NULL, WINED3D_TEXF_POINT);
3695 return WINED3D_OK;
3698 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3700 const struct wined3d_state *state = &device->state;
3701 struct wined3d_texture *texture;
3702 DWORD i;
3704 TRACE("device %p, num_passes %p.\n", device, num_passes);
3706 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3708 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3710 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3711 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3713 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3715 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3716 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3719 texture = state->textures[i];
3720 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3722 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3724 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3725 return E_FAIL;
3727 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3729 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3730 return E_FAIL;
3732 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3733 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3735 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3736 return E_FAIL;
3740 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3741 || state->render_states[WINED3D_RS_STENCILENABLE])
3743 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3744 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3746 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3748 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3749 return WINED3DERR_CONFLICTINGRENDERSTATE;
3753 /* return a sensible default */
3754 *num_passes = 1;
3756 TRACE("returning D3D_OK\n");
3757 return WINED3D_OK;
3760 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3762 static BOOL warned;
3764 TRACE("device %p, software %#x.\n", device, software);
3766 if (!warned)
3768 FIXME("device %p, software %#x stub!\n", device, software);
3769 warned = TRUE;
3772 device->softwareVertexProcessing = software;
3775 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3777 static BOOL warned;
3779 TRACE("device %p.\n", device);
3781 if (!warned)
3783 TRACE("device %p stub!\n", device);
3784 warned = TRUE;
3787 return device->softwareVertexProcessing;
3790 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3791 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3793 struct wined3d_swapchain *swapchain;
3795 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3796 device, swapchain_idx, raster_status);
3798 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3799 return WINED3DERR_INVALIDCALL;
3801 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3804 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3806 static BOOL warned;
3808 TRACE("device %p, segments %.8e.\n", device, segments);
3810 if (segments != 0.0f)
3812 if (!warned)
3814 FIXME("device %p, segments %.8e stub!\n", device, segments);
3815 warned = TRUE;
3819 return WINED3D_OK;
3822 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3824 static BOOL warned;
3826 TRACE("device %p.\n", device);
3828 if (!warned)
3830 FIXME("device %p stub!\n", device);
3831 warned = TRUE;
3834 return 0.0f;
3837 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3838 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3840 struct wined3d_texture *dst_texture, *src_texture;
3841 struct wined3d_box box;
3842 unsigned int i, j;
3844 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3846 if (src_resource == dst_resource)
3848 WARN("Source and destination are the same resource.\n");
3849 return;
3852 if (src_resource->type != dst_resource->type)
3854 WARN("Resource types (%s / %s) don't match.\n",
3855 debug_d3dresourcetype(dst_resource->type),
3856 debug_d3dresourcetype(src_resource->type));
3857 return;
3860 if (src_resource->width != dst_resource->width
3861 || src_resource->height != dst_resource->height
3862 || src_resource->depth != dst_resource->depth)
3864 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3865 dst_resource->width, dst_resource->height, dst_resource->depth,
3866 src_resource->width, src_resource->height, src_resource->depth);
3867 return;
3870 if (src_resource->format->id != dst_resource->format->id)
3872 WARN("Resource formats (%s / %s) don't match.\n",
3873 debug_d3dformat(dst_resource->format->id),
3874 debug_d3dformat(src_resource->format->id));
3875 return;
3878 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3880 wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
3881 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
3882 src_resource, 0, &box, 0, NULL, WINED3D_TEXF_POINT);
3883 return;
3886 dst_texture = texture_from_resource(dst_resource);
3887 src_texture = texture_from_resource(src_resource);
3889 if (src_texture->layer_count != dst_texture->layer_count
3890 || src_texture->level_count != dst_texture->level_count)
3892 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3893 dst_texture->layer_count, dst_texture->level_count,
3894 src_texture->layer_count, src_texture->level_count);
3895 return;
3898 for (i = 0; i < dst_texture->level_count; ++i)
3900 wined3d_box_set(&box, 0, 0,
3901 wined3d_texture_get_level_width(dst_texture, i),
3902 wined3d_texture_get_level_height(dst_texture, i),
3903 0, wined3d_texture_get_level_depth(dst_texture, i));
3904 for (j = 0; j < dst_texture->layer_count; ++j)
3906 unsigned int idx = j * dst_texture->level_count + i;
3908 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
3909 src_resource, idx, &box, 0, NULL, WINED3D_TEXF_POINT);
3914 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3915 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3916 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3917 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3919 struct wined3d_box dst_box, b;
3921 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3922 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3923 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3924 src_resource, src_sub_resource_idx, debug_box(src_box));
3926 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3928 WARN("Source and destination are the same sub-resource.\n");
3929 return WINED3DERR_INVALIDCALL;
3932 if (src_resource->type != dst_resource->type)
3934 WARN("Resource types (%s / %s) don't match.\n",
3935 debug_d3dresourcetype(dst_resource->type),
3936 debug_d3dresourcetype(src_resource->type));
3937 return WINED3DERR_INVALIDCALL;
3940 if (src_resource->format->id != dst_resource->format->id)
3942 WARN("Resource formats (%s / %s) don't match.\n",
3943 debug_d3dformat(dst_resource->format->id),
3944 debug_d3dformat(src_resource->format->id));
3945 return WINED3DERR_INVALIDCALL;
3948 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3950 if (dst_sub_resource_idx)
3952 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3953 return WINED3DERR_INVALIDCALL;
3956 if (src_sub_resource_idx)
3958 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3959 return WINED3DERR_INVALIDCALL;
3962 if (!src_box)
3964 wined3d_box_set(&b, 0, 0, src_resource->size, 1, 0, 1);
3965 src_box = &b;
3967 else if ((src_box->left >= src_box->right
3968 || src_box->top >= src_box->bottom
3969 || src_box->front >= src_box->back))
3971 WARN("Invalid box %s specified.\n", debug_box(src_box));
3972 return WINED3DERR_INVALIDCALL;
3975 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
3976 || src_box->right - src_box->left > dst_resource->size - dst_x)
3978 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
3979 dst_x, src_box->left, src_box->right - src_box->left);
3980 return WINED3DERR_INVALIDCALL;
3983 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
3985 else if (dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
3987 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
3988 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
3989 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
3991 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3993 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
3994 return WINED3DERR_INVALIDCALL;
3997 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
3999 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4000 return WINED3DERR_INVALIDCALL;
4003 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4005 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4006 return WINED3DERR_INVALIDCALL;
4009 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4011 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4012 return WINED3DERR_INVALIDCALL;
4015 if (!src_box)
4017 wined3d_box_set(&b, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4018 wined3d_texture_get_level_height(src_texture, src_level), 0, 1);
4019 src_box = &b;
4021 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4023 WARN("Invalid source box %s.\n", debug_box(src_box));
4024 return WINED3DERR_INVALIDCALL;
4027 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4028 dst_y + (src_box->bottom - src_box->top), 0, 1);
4029 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4030 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4032 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4033 return WINED3DERR_INVALIDCALL;
4036 else
4038 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4039 return WINED3DERR_INVALIDCALL;
4042 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4043 src_resource, src_sub_resource_idx, src_box, 0, NULL, WINED3D_TEXF_POINT);
4045 return WINED3D_OK;
4048 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4049 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4050 unsigned int depth_pitch)
4052 unsigned int width, height, depth;
4053 struct wined3d_box b;
4055 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4056 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4058 if (resource->type == WINED3D_RTYPE_BUFFER)
4060 if (sub_resource_idx > 0)
4062 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4063 return;
4066 width = resource->size;
4067 height = 1;
4068 depth = 1;
4070 else if (resource->type == WINED3D_RTYPE_TEXTURE_2D || resource->type == WINED3D_RTYPE_TEXTURE_3D)
4072 struct wined3d_texture *texture = texture_from_resource(resource);
4073 unsigned int level;
4075 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4077 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4078 return;
4081 level = sub_resource_idx % texture->level_count;
4082 width = wined3d_texture_get_level_width(texture, level);
4083 height = wined3d_texture_get_level_height(texture, level);
4084 depth = wined3d_texture_get_level_depth(texture, level);
4086 else
4088 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4089 return;
4092 if (!box)
4094 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4095 box = &b;
4097 else if (box->left >= box->right || box->right > width
4098 || box->top >= box->bottom || box->bottom > height
4099 || box->front >= box->back || box->back > depth)
4101 WARN("Invalid box %s specified.\n", debug_box(box));
4102 return;
4105 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4108 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4109 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4110 const struct wined3d_color *color, float depth, DWORD stencil)
4112 struct wined3d_resource *resource;
4113 RECT r;
4115 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4116 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4118 if (!flags)
4119 return WINED3D_OK;
4121 resource = view->resource;
4122 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4124 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4125 return WINED3DERR_INVALIDCALL;
4128 if (view->layer_count > 1)
4130 FIXME("Layered clears not implemented.\n");
4131 return WINED3DERR_INVALIDCALL;
4134 if (!rect)
4136 SetRect(&r, 0, 0, view->width, view->height);
4137 rect = &r;
4139 else
4141 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4142 struct wined3d_texture *texture = texture_from_resource(view->resource);
4143 HRESULT hr;
4145 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4146 view->sub_resource_idx % texture->level_count, &b)))
4147 return hr;
4150 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4152 return WINED3D_OK;
4155 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4156 unsigned int view_idx)
4158 TRACE("device %p, view_idx %u.\n", device, view_idx);
4160 if (view_idx >= device->adapter->gl_info.limits.buffers)
4162 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4163 return NULL;
4166 return device->fb.render_targets[view_idx];
4169 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4171 TRACE("device %p.\n", device);
4173 return device->fb.depth_stencil;
4176 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4177 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4179 struct wined3d_rendertarget_view *prev;
4181 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4182 device, view_idx, view, set_viewport);
4184 if (view_idx >= device->adapter->gl_info.limits.buffers)
4186 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4187 return WINED3DERR_INVALIDCALL;
4190 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4192 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4193 return WINED3DERR_INVALIDCALL;
4196 /* Set the viewport and scissor rectangles, if requested. Tests show that
4197 * stateblock recording is ignored, the change goes directly into the
4198 * primary stateblock. */
4199 if (!view_idx && set_viewport)
4201 struct wined3d_state *state = &device->state;
4203 state->viewport.x = 0;
4204 state->viewport.y = 0;
4205 state->viewport.width = view->width;
4206 state->viewport.height = view->height;
4207 state->viewport.min_z = 0.0f;
4208 state->viewport.max_z = 1.0f;
4209 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4211 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4212 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4216 prev = device->fb.render_targets[view_idx];
4217 if (view == prev)
4218 return WINED3D_OK;
4220 if (view)
4221 wined3d_rendertarget_view_incref(view);
4222 device->fb.render_targets[view_idx] = view;
4223 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4224 /* Release after the assignment, to prevent device_resource_released()
4225 * from seeing the surface as still in use. */
4226 if (prev)
4227 wined3d_rendertarget_view_decref(prev);
4229 return WINED3D_OK;
4232 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4234 struct wined3d_rendertarget_view *prev;
4236 TRACE("device %p, view %p.\n", device, view);
4238 prev = device->fb.depth_stencil;
4239 if (prev == view)
4241 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4242 return;
4245 if ((device->fb.depth_stencil = view))
4246 wined3d_rendertarget_view_incref(view);
4247 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4248 if (prev)
4249 wined3d_rendertarget_view_decref(prev);
4252 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4253 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4255 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4256 struct wined3d_sub_resource_data data;
4257 struct wined3d_resource_desc desc;
4258 struct wined3d_map_desc map_desc;
4259 struct wined3d_texture *texture;
4260 HRESULT hr;
4262 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4264 ERR("Failed to map source texture.\n");
4265 return NULL;
4268 data.data = map_desc.data;
4269 data.row_pitch = map_desc.row_pitch;
4270 data.slice_pitch = map_desc.slice_pitch;
4272 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4273 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4274 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4275 desc.multisample_quality = 0;
4276 desc.usage = WINED3DUSAGE_DYNAMIC;
4277 desc.pool = WINED3D_POOL_DEFAULT;
4278 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4279 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4280 desc.depth = 1;
4281 desc.size = 0;
4283 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4284 &data, NULL, &wined3d_null_parent_ops, &texture);
4285 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4286 if (FAILED(hr))
4288 ERR("Failed to create cursor texture.\n");
4289 return NULL;
4292 return texture;
4295 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4296 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4298 unsigned int texture_level = sub_resource_idx % texture->level_count;
4299 unsigned int cursor_width, cursor_height;
4300 struct wined3d_display_mode mode;
4301 struct wined3d_map_desc map_desc;
4302 HRESULT hr;
4304 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4305 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4307 if (sub_resource_idx >= texture->level_count * texture->layer_count
4308 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4309 return WINED3DERR_INVALIDCALL;
4311 if (device->cursor_texture)
4313 wined3d_texture_decref(device->cursor_texture);
4314 device->cursor_texture = NULL;
4317 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4319 WARN("Texture %p has invalid format %s.\n",
4320 texture, debug_d3dformat(texture->resource.format->id));
4321 return WINED3DERR_INVALIDCALL;
4324 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4326 ERR("Failed to get display mode, hr %#x.\n", hr);
4327 return WINED3DERR_INVALIDCALL;
4330 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4331 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4332 if (cursor_width > mode.width || cursor_height > mode.height)
4334 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4335 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4336 return WINED3DERR_INVALIDCALL;
4339 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4341 /* Do not store the surface's pointer because the application may
4342 * release it after setting the cursor image. Windows doesn't
4343 * addref the set surface, so we can't do this either without
4344 * creating circular refcount dependencies. */
4345 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4347 ERR("Failed to create cursor texture.\n");
4348 return WINED3DERR_INVALIDCALL;
4351 if (cursor_width == 32 && cursor_height == 32)
4353 UINT mask_size = cursor_width * cursor_height / 8;
4354 ICONINFO cursor_info;
4355 DWORD *mask_bits;
4356 HCURSOR cursor;
4358 /* 32-bit user32 cursors ignore the alpha channel if it's all
4359 * zeroes, and use the mask instead. Fill the mask with all ones
4360 * to ensure we still get a fully transparent cursor. */
4361 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4362 return E_OUTOFMEMORY;
4363 memset(mask_bits, 0xff, mask_size);
4365 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4366 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4367 cursor_info.fIcon = FALSE;
4368 cursor_info.xHotspot = x_hotspot;
4369 cursor_info.yHotspot = y_hotspot;
4370 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4371 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4372 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4374 /* Create our cursor and clean up. */
4375 cursor = CreateIconIndirect(&cursor_info);
4376 if (cursor_info.hbmMask)
4377 DeleteObject(cursor_info.hbmMask);
4378 if (cursor_info.hbmColor)
4379 DeleteObject(cursor_info.hbmColor);
4380 if (device->hardwareCursor)
4381 DestroyCursor(device->hardwareCursor);
4382 device->hardwareCursor = cursor;
4383 if (device->bCursorVisible)
4384 SetCursor(cursor);
4386 HeapFree(GetProcessHeap(), 0, mask_bits);
4389 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4390 device->cursorWidth = cursor_width;
4391 device->cursorHeight = cursor_height;
4392 device->xHotSpot = x_hotspot;
4393 device->yHotSpot = y_hotspot;
4395 return WINED3D_OK;
4398 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4399 int x_screen_space, int y_screen_space, DWORD flags)
4401 TRACE("device %p, x %d, y %d, flags %#x.\n",
4402 device, x_screen_space, y_screen_space, flags);
4404 device->xScreenSpace = x_screen_space;
4405 device->yScreenSpace = y_screen_space;
4407 if (device->hardwareCursor)
4409 POINT pt;
4411 GetCursorPos( &pt );
4412 if (x_screen_space == pt.x && y_screen_space == pt.y)
4413 return;
4414 SetCursorPos( x_screen_space, y_screen_space );
4416 /* Switch to the software cursor if position diverges from the hardware one. */
4417 GetCursorPos( &pt );
4418 if (x_screen_space != pt.x || y_screen_space != pt.y)
4420 if (device->bCursorVisible) SetCursor( NULL );
4421 DestroyCursor( device->hardwareCursor );
4422 device->hardwareCursor = 0;
4427 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4429 BOOL oldVisible = device->bCursorVisible;
4431 TRACE("device %p, show %#x.\n", device, show);
4434 * When ShowCursor is first called it should make the cursor appear at the OS's last
4435 * known cursor position.
4437 if (show && !oldVisible)
4439 POINT pt;
4440 GetCursorPos(&pt);
4441 device->xScreenSpace = pt.x;
4442 device->yScreenSpace = pt.y;
4445 if (device->hardwareCursor)
4447 device->bCursorVisible = show;
4448 if (show)
4449 SetCursor(device->hardwareCursor);
4450 else
4451 SetCursor(NULL);
4453 else if (device->cursor_texture)
4455 device->bCursorVisible = show;
4458 return oldVisible;
4461 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4463 struct wined3d_resource *resource, *cursor;
4465 TRACE("device %p.\n", device);
4467 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4469 TRACE("Checking resource %p for eviction.\n", resource);
4471 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4473 TRACE("Evicting %p.\n", resource);
4474 wined3d_cs_emit_unload_resource(device->cs, resource);
4479 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4480 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4481 wined3d_device_reset_cb callback, BOOL reset_state)
4483 struct wined3d_resource *resource, *cursor;
4484 struct wined3d_swapchain *swapchain;
4485 struct wined3d_view_desc view_desc;
4486 BOOL backbuffer_resized;
4487 HRESULT hr = WINED3D_OK;
4488 unsigned int i;
4490 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4491 device, swapchain_desc, mode, callback, reset_state);
4493 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4495 ERR("Failed to get the first implicit swapchain.\n");
4496 return WINED3DERR_INVALIDCALL;
4499 if (reset_state)
4501 if (device->logo_texture)
4503 wined3d_texture_decref(device->logo_texture);
4504 device->logo_texture = NULL;
4506 if (device->cursor_texture)
4508 wined3d_texture_decref(device->cursor_texture);
4509 device->cursor_texture = NULL;
4511 state_unbind_resources(&device->state);
4514 if (device->fb.render_targets)
4516 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4518 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4521 wined3d_device_set_depth_stencil_view(device, NULL);
4523 if (reset_state)
4525 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4527 TRACE("Enumerating resource %p.\n", resource);
4528 if (FAILED(hr = callback(resource)))
4529 return hr;
4533 TRACE("New params:\n");
4534 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4535 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4536 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4537 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4538 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4539 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4540 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4541 TRACE("device_window %p\n", swapchain_desc->device_window);
4542 TRACE("windowed %#x\n", swapchain_desc->windowed);
4543 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4544 if (swapchain_desc->enable_auto_depth_stencil)
4545 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4546 TRACE("flags %#x\n", swapchain_desc->flags);
4547 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4548 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4549 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4551 /* No special treatment of these parameters. Just store them */
4552 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4553 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4554 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4555 swapchain->desc.flags = swapchain_desc->flags;
4556 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4557 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4558 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4560 if (swapchain_desc->device_window
4561 && swapchain_desc->device_window != swapchain->desc.device_window)
4563 TRACE("Changing the device window from %p to %p.\n",
4564 swapchain->desc.device_window, swapchain_desc->device_window);
4565 swapchain->desc.device_window = swapchain_desc->device_window;
4566 swapchain->device_window = swapchain_desc->device_window;
4567 wined3d_swapchain_set_window(swapchain, NULL);
4570 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4571 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4573 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4574 || swapchain->reapply_mode || mode
4575 || (!swapchain_desc->windowed && backbuffer_resized))
4577 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4578 return hr;
4580 else if (!swapchain_desc->windowed)
4582 DWORD style = device->style;
4583 DWORD exStyle = device->exStyle;
4584 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4585 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4586 * Reset to clear up their mess. Guild Wars also loses the device during that.
4588 device->style = 0;
4589 device->exStyle = 0;
4590 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4591 swapchain_desc->backbuffer_width,
4592 swapchain_desc->backbuffer_height);
4593 device->style = style;
4594 device->exStyle = exStyle;
4597 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4598 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4599 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4600 return hr;
4602 if (device->auto_depth_stencil_view)
4604 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4605 device->auto_depth_stencil_view = NULL;
4607 if (swapchain->desc.enable_auto_depth_stencil)
4609 struct wined3d_resource_desc texture_desc;
4610 struct wined3d_texture *texture;
4611 DWORD flags = 0;
4613 TRACE("Creating the depth stencil buffer.\n");
4615 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4616 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4617 texture_desc.multisample_type = swapchain->desc.multisample_type;
4618 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4619 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4620 texture_desc.pool = WINED3D_POOL_DEFAULT;
4621 texture_desc.width = swapchain->desc.backbuffer_width;
4622 texture_desc.height = swapchain->desc.backbuffer_height;
4623 texture_desc.depth = 1;
4624 texture_desc.size = 0;
4626 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4627 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4629 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4630 device->device_parent, &texture_desc, flags, &texture)))
4632 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4633 return WINED3DERR_INVALIDCALL;
4636 view_desc.format_id = texture->resource.format->id;
4637 view_desc.flags = 0;
4638 view_desc.u.texture.level_idx = 0;
4639 view_desc.u.texture.level_count = 1;
4640 view_desc.u.texture.layer_idx = 0;
4641 view_desc.u.texture.layer_count = 1;
4642 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4643 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4644 wined3d_texture_decref(texture);
4645 if (FAILED(hr))
4647 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4648 return hr;
4651 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4654 if (device->back_buffer_view)
4656 wined3d_rendertarget_view_decref(device->back_buffer_view);
4657 device->back_buffer_view = NULL;
4659 if (swapchain->desc.backbuffer_count)
4661 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4663 view_desc.format_id = back_buffer->format->id;
4664 view_desc.flags = 0;
4665 view_desc.u.texture.level_idx = 0;
4666 view_desc.u.texture.level_count = 1;
4667 view_desc.u.texture.layer_idx = 0;
4668 view_desc.u.texture.layer_count = 1;
4669 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4670 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4672 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4673 return hr;
4677 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4679 if (reset_state)
4681 TRACE("Resetting stateblock.\n");
4682 if (device->recording)
4684 wined3d_stateblock_decref(device->recording);
4685 device->recording = NULL;
4687 wined3d_cs_emit_reset_state(device->cs);
4688 state_cleanup(&device->state);
4690 if (device->d3d_initialized)
4691 wined3d_device_delete_opengl_contexts(device);
4693 memset(&device->state, 0, sizeof(device->state));
4694 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4695 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4696 device->update_state = &device->state;
4698 device_init_swapchain_state(device, swapchain);
4699 if (wined3d_settings.logo)
4700 device_load_logo(device, wined3d_settings.logo);
4702 else if (device->back_buffer_view)
4704 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4705 struct wined3d_state *state = &device->state;
4707 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4709 /* Note the min_z / max_z is not reset. */
4710 state->viewport.x = 0;
4711 state->viewport.y = 0;
4712 state->viewport.width = view->width;
4713 state->viewport.height = view->height;
4714 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4716 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4717 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4720 if (device->d3d_initialized)
4722 if (reset_state)
4723 hr = wined3d_device_create_primary_opengl_context(device);
4724 swapchain_update_swap_interval(swapchain);
4727 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4728 * first use
4730 return hr;
4733 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4735 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4737 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4739 return WINED3D_OK;
4743 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4744 struct wined3d_device_creation_parameters *parameters)
4746 TRACE("device %p, parameters %p.\n", device, parameters);
4748 *parameters = device->create_parms;
4751 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4753 TRACE("device %p.\n", device);
4755 return device->wined3d;
4758 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4759 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4761 struct wined3d_swapchain *swapchain;
4763 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4764 device, swapchain_idx, flags, ramp);
4766 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4767 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4770 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4771 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4773 struct wined3d_swapchain *swapchain;
4775 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4776 device, swapchain_idx, ramp);
4778 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4779 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4782 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4784 TRACE("device %p, resource %p.\n", device, resource);
4786 wined3d_not_from_cs(device->cs);
4788 list_add_head(&device->resources, &resource->resource_list_entry);
4791 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4793 TRACE("device %p, resource %p.\n", device, resource);
4795 wined3d_not_from_cs(device->cs);
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 wined3d_from_cs(device->cs);
4973 if (STATE_IS_COMPUTE(state))
4975 for (i = 0; i < device->context_count; ++i)
4976 context_invalidate_compute_state(device->contexts[i], state);
4977 return;
4980 for (i = 0; i < device->context_count; ++i)
4982 context = device->contexts[i];
4983 if(isStateDirty(context, rep)) continue;
4985 context->dirtyArray[context->numDirtyEntries++] = rep;
4986 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4987 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4988 context->isStateDirty[idx] |= (1u << shift);
4992 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4993 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4995 if (device->filter_messages && message != WM_DISPLAYCHANGE)
4997 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4998 window, message, wparam, lparam);
4999 if (unicode)
5000 return DefWindowProcW(window, message, wparam, lparam);
5001 else
5002 return DefWindowProcA(window, message, wparam, lparam);
5005 if (message == WM_DESTROY)
5007 TRACE("unregister window %p.\n", window);
5008 wined3d_unregister_window(window);
5010 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5011 ERR("Window %p is not the focus window for device %p.\n", window, device);
5013 else if (message == WM_DISPLAYCHANGE)
5015 device->device_parent->ops->mode_changed(device->device_parent);
5017 else if (message == WM_ACTIVATEAPP)
5019 UINT i;
5021 for (i = 0; i < device->swapchain_count; i++)
5022 wined3d_swapchain_activate(device->swapchains[i], wparam);
5024 device->device_parent->ops->activate(device->device_parent, wparam);
5026 else if (message == WM_SYSCOMMAND)
5028 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5030 if (unicode)
5031 DefWindowProcW(window, message, wparam, lparam);
5032 else
5033 DefWindowProcA(window, message, wparam, lparam);
5037 if (unicode)
5038 return CallWindowProcW(proc, window, message, wparam, lparam);
5039 else
5040 return CallWindowProcA(proc, window, message, wparam, lparam);