wined3d: Introduce wined3d_device_get_ds_sampler().
[wine.git] / dlls / wined3d / device.c
blob26374113b1d94c5292c9b39c9e239bf484cae5b5
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, &wined3d_null_parent_ops, &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, &wined3d_null_parent_ops, &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);
1000 device->cs->ops->finish(device->cs);
1003 static void wined3d_device_create_primary_opengl_context_cs(void *object)
1005 struct wined3d_device *device = object;
1006 struct wined3d_swapchain *swapchain;
1007 struct wined3d_context *context;
1008 struct wined3d_texture *target;
1009 HRESULT hr;
1011 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1012 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1014 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1015 return;
1018 if (!(device->blitter = wined3d_cpu_blitter_create()))
1020 ERR("Failed to create CPU blitter.\n");
1021 device->shader_backend->shader_free_private(device);
1022 return;
1024 wined3d_ffp_blitter_create(&device->blitter, &device->adapter->gl_info);
1025 wined3d_arbfp_blitter_create(&device->blitter, device);
1026 wined3d_fbo_blitter_create(&device->blitter, &device->adapter->gl_info);
1028 swapchain = device->swapchains[0];
1029 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1030 context = context_acquire(device, target, 0);
1031 create_dummy_textures(device, context);
1032 create_default_samplers(device, context);
1033 context_release(context);
1036 static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1038 wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1039 device->cs->ops->finish(device->cs);
1040 if (!device->swapchains[0]->num_contexts)
1041 return E_FAIL;
1043 return WINED3D_OK;
1046 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1047 struct wined3d_swapchain_desc *swapchain_desc)
1049 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1050 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1051 struct wined3d_swapchain *swapchain = NULL;
1052 DWORD clear_flags = 0;
1053 HRESULT hr;
1055 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1057 if (device->d3d_initialized)
1058 return WINED3DERR_INVALIDCALL;
1059 if (device->wined3d->flags & WINED3D_NO3D)
1060 return WINED3DERR_INVALIDCALL;
1062 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
1063 return E_OUTOFMEMORY;
1065 /* Setup the implicit swapchain. This also initializes a context. */
1066 TRACE("Creating implicit swapchain\n");
1067 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1068 swapchain_desc, &swapchain);
1069 if (FAILED(hr))
1071 WARN("Failed to create implicit swapchain\n");
1072 goto err_out;
1075 if (swapchain_desc->backbuffer_count)
1077 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1078 struct wined3d_view_desc view_desc;
1080 view_desc.format_id = back_buffer->format->id;
1081 view_desc.flags = 0;
1082 view_desc.u.texture.level_idx = 0;
1083 view_desc.u.texture.level_count = 1;
1084 view_desc.u.texture.layer_idx = 0;
1085 view_desc.u.texture.layer_count = 1;
1086 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1087 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1089 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1090 goto err_out;
1094 device->swapchain_count = 1;
1095 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1097 ERR("Out of memory!\n");
1098 goto err_out;
1100 device->swapchains[0] = swapchain;
1102 if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1103 goto err_out;
1104 device_init_swapchain_state(device, swapchain);
1106 device->contexts[0]->last_was_rhw = 0;
1108 TRACE("All defaults now set up, leaving 3D init.\n");
1110 /* Clear the screen */
1111 if (swapchain->back_buffers && swapchain->back_buffers[0])
1112 clear_flags |= WINED3DCLEAR_TARGET;
1113 if (swapchain_desc->enable_auto_depth_stencil)
1114 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1115 if (clear_flags)
1116 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1118 device->d3d_initialized = TRUE;
1120 if (wined3d_settings.logo)
1121 device_load_logo(device, wined3d_settings.logo);
1122 return WINED3D_OK;
1124 err_out:
1125 HeapFree(GetProcessHeap(), 0, device->swapchains);
1126 device->swapchain_count = 0;
1127 if (device->back_buffer_view)
1128 wined3d_rendertarget_view_decref(device->back_buffer_view);
1129 if (swapchain)
1130 wined3d_swapchain_decref(swapchain);
1131 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1133 return hr;
1136 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1137 struct wined3d_swapchain_desc *swapchain_desc)
1139 struct wined3d_swapchain *swapchain = NULL;
1140 HRESULT hr;
1142 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1144 /* Setup the implicit swapchain */
1145 TRACE("Creating implicit swapchain\n");
1146 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1147 swapchain_desc, &swapchain);
1148 if (FAILED(hr))
1150 WARN("Failed to create implicit swapchain\n");
1151 goto err_out;
1154 device->swapchain_count = 1;
1155 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1157 ERR("Out of memory!\n");
1158 goto err_out;
1160 device->swapchains[0] = swapchain;
1161 return WINED3D_OK;
1163 err_out:
1164 wined3d_swapchain_decref(swapchain);
1165 return hr;
1168 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1170 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1172 wined3d_sampler_decref(sampler);
1175 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1177 UINT i;
1179 TRACE("device %p.\n", device);
1181 if (!device->d3d_initialized)
1182 return WINED3DERR_INVALIDCALL;
1184 device->cs->ops->finish(device->cs);
1186 if (device->logo_texture)
1187 wined3d_texture_decref(device->logo_texture);
1188 if (device->cursor_texture)
1189 wined3d_texture_decref(device->cursor_texture);
1191 state_unbind_resources(&device->state);
1193 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1195 wined3d_device_delete_opengl_contexts(device);
1197 if (device->fb.depth_stencil)
1199 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1201 TRACE("Releasing depth/stencil view %p.\n", view);
1203 device->fb.depth_stencil = NULL;
1204 wined3d_rendertarget_view_decref(view);
1207 if (device->auto_depth_stencil_view)
1209 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1211 device->auto_depth_stencil_view = NULL;
1212 if (wined3d_rendertarget_view_decref(view))
1213 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1216 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
1218 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1220 if (device->back_buffer_view)
1222 wined3d_rendertarget_view_decref(device->back_buffer_view);
1223 device->back_buffer_view = NULL;
1226 for (i = 0; i < device->swapchain_count; ++i)
1228 TRACE("Releasing the implicit swapchain %u.\n", i);
1229 if (wined3d_swapchain_decref(device->swapchains[i]))
1230 FIXME("Something's still holding the implicit swapchain.\n");
1233 HeapFree(GetProcessHeap(), 0, device->swapchains);
1234 device->swapchains = NULL;
1235 device->swapchain_count = 0;
1237 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1238 device->fb.render_targets = NULL;
1240 device->d3d_initialized = FALSE;
1242 return WINED3D_OK;
1245 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1247 unsigned int i;
1249 for (i = 0; i < device->swapchain_count; ++i)
1251 TRACE("Releasing the implicit swapchain %u.\n", i);
1252 if (wined3d_swapchain_decref(device->swapchains[i]))
1253 FIXME("Something's still holding the implicit swapchain.\n");
1256 HeapFree(GetProcessHeap(), 0, device->swapchains);
1257 device->swapchains = NULL;
1258 device->swapchain_count = 0;
1259 return WINED3D_OK;
1262 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1263 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1264 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1266 * There is no way to deactivate thread safety once it is enabled.
1268 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1270 TRACE("device %p.\n", device);
1272 /* For now just store the flag (needed in case of ddraw). */
1273 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1276 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1278 TRACE("device %p.\n", device);
1280 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1281 wine_dbgstr_longlong(device->adapter->vram_bytes),
1282 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1283 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1285 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1288 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1289 struct wined3d_buffer *buffer, UINT offset)
1291 struct wined3d_stream_output *stream;
1292 struct wined3d_buffer *prev_buffer;
1294 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1296 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1298 WARN("Invalid stream output %u.\n", idx);
1299 return;
1302 stream = &device->update_state->stream_output[idx];
1303 prev_buffer = stream->buffer;
1305 if (buffer)
1306 wined3d_buffer_incref(buffer);
1307 stream->buffer = buffer;
1308 stream->offset = offset;
1309 if (!device->recording)
1310 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1311 if (prev_buffer)
1312 wined3d_buffer_decref(prev_buffer);
1315 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1316 UINT idx, UINT *offset)
1318 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1320 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1322 WARN("Invalid stream output %u.\n", idx);
1323 return NULL;
1326 if (offset)
1327 *offset = device->state.stream_output[idx].offset;
1328 return device->state.stream_output[idx].buffer;
1331 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1332 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1334 struct wined3d_stream_state *stream;
1335 struct wined3d_buffer *prev_buffer;
1337 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1338 device, stream_idx, buffer, offset, stride);
1340 if (stream_idx >= MAX_STREAMS)
1342 WARN("Stream index %u out of range.\n", stream_idx);
1343 return WINED3DERR_INVALIDCALL;
1345 else if (offset & 0x3)
1347 WARN("Offset %u is not 4 byte aligned.\n", offset);
1348 return WINED3DERR_INVALIDCALL;
1351 stream = &device->update_state->streams[stream_idx];
1352 prev_buffer = stream->buffer;
1354 if (device->recording)
1355 device->recording->changed.streamSource |= 1u << stream_idx;
1357 if (prev_buffer == buffer
1358 && stream->stride == stride
1359 && stream->offset == offset)
1361 TRACE("Application is setting the old values over, nothing to do.\n");
1362 return WINED3D_OK;
1365 stream->buffer = buffer;
1366 if (buffer)
1368 stream->stride = stride;
1369 stream->offset = offset;
1370 wined3d_buffer_incref(buffer);
1373 if (!device->recording)
1374 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1375 if (prev_buffer)
1376 wined3d_buffer_decref(prev_buffer);
1378 return WINED3D_OK;
1381 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1382 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1384 const struct wined3d_stream_state *stream;
1386 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1387 device, stream_idx, buffer, offset, stride);
1389 if (stream_idx >= MAX_STREAMS)
1391 WARN("Stream index %u out of range.\n", stream_idx);
1392 return WINED3DERR_INVALIDCALL;
1395 stream = &device->state.streams[stream_idx];
1396 *buffer = stream->buffer;
1397 if (offset)
1398 *offset = stream->offset;
1399 *stride = stream->stride;
1401 return WINED3D_OK;
1404 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1406 struct wined3d_stream_state *stream;
1407 UINT old_flags, old_freq;
1409 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1411 /* Verify input. At least in d3d9 this is invalid. */
1412 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1414 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1415 return WINED3DERR_INVALIDCALL;
1417 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1419 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1420 return WINED3DERR_INVALIDCALL;
1422 if (!divider)
1424 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1425 return WINED3DERR_INVALIDCALL;
1428 stream = &device->update_state->streams[stream_idx];
1429 old_flags = stream->flags;
1430 old_freq = stream->frequency;
1432 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1433 stream->frequency = divider & 0x7fffff;
1435 if (device->recording)
1436 device->recording->changed.streamFreq |= 1u << stream_idx;
1437 else if (stream->frequency != old_freq || stream->flags != old_flags)
1438 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1440 return WINED3D_OK;
1443 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1444 UINT stream_idx, UINT *divider)
1446 const struct wined3d_stream_state *stream;
1448 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1450 stream = &device->state.streams[stream_idx];
1451 *divider = stream->flags | stream->frequency;
1453 TRACE("Returning %#x.\n", *divider);
1455 return WINED3D_OK;
1458 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1459 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1461 TRACE("device %p, state %s, matrix %p.\n",
1462 device, debug_d3dtstype(d3dts), matrix);
1463 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1464 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1465 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1466 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1468 /* Handle recording of state blocks. */
1469 if (device->recording)
1471 TRACE("Recording... not performing anything.\n");
1472 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1473 device->update_state->transforms[d3dts] = *matrix;
1474 return;
1477 /* If the new matrix is the same as the current one,
1478 * we cut off any further processing. this seems to be a reasonable
1479 * optimization because as was noticed, some apps (warcraft3 for example)
1480 * tend towards setting the same matrix repeatedly for some reason.
1482 * From here on we assume that the new matrix is different, wherever it matters. */
1483 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1485 TRACE("The application is setting the same matrix over again.\n");
1486 return;
1489 device->state.transforms[d3dts] = *matrix;
1490 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1493 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1494 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1496 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1498 *matrix = device->state.transforms[state];
1501 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1502 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1504 const struct wined3d_matrix *mat;
1505 struct wined3d_matrix temp;
1507 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1509 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1510 * below means it will be recorded in a state block change, but it
1511 * works regardless where it is recorded.
1512 * If this is found to be wrong, change to StateBlock. */
1513 if (state > HIGHEST_TRANSFORMSTATE)
1515 WARN("Unhandled transform state %#x.\n", state);
1516 return;
1519 mat = &device->update_state->transforms[state];
1520 multiply_matrix(&temp, mat, matrix);
1522 /* Apply change via set transform - will reapply to eg. lights this way. */
1523 wined3d_device_set_transform(device, state, &temp);
1526 /* Note lights are real special cases. Although the device caps state only
1527 * e.g. 8 are supported, you can reference any indexes you want as long as
1528 * that number max are enabled at any one point in time. Therefore since the
1529 * indices can be anything, we need a hashmap of them. However, this causes
1530 * stateblock problems. When capturing the state block, I duplicate the
1531 * hashmap, but when recording, just build a chain pretty much of commands to
1532 * be replayed. */
1533 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1534 UINT light_idx, const struct wined3d_light *light)
1536 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1537 struct wined3d_light_info *object = NULL;
1538 float rho;
1540 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1542 /* Check the parameter range. Need for speed most wanted sets junk lights
1543 * which confuse the GL driver. */
1544 if (!light)
1545 return WINED3DERR_INVALIDCALL;
1547 switch (light->type)
1549 case WINED3D_LIGHT_POINT:
1550 case WINED3D_LIGHT_SPOT:
1551 case WINED3D_LIGHT_GLSPOT:
1552 /* Incorrect attenuation values can cause the gl driver to crash.
1553 * Happens with Need for speed most wanted. */
1554 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1556 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1557 return WINED3DERR_INVALIDCALL;
1559 break;
1561 case WINED3D_LIGHT_DIRECTIONAL:
1562 case WINED3D_LIGHT_PARALLELPOINT:
1563 /* Ignores attenuation */
1564 break;
1566 default:
1567 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1568 return WINED3DERR_INVALIDCALL;
1571 if (!(object = wined3d_state_get_light(device->update_state, light_idx)))
1573 TRACE("Adding new light\n");
1574 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1575 if (!object)
1576 return E_OUTOFMEMORY;
1578 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1579 object->glIndex = -1;
1580 object->OriginalIndex = light_idx;
1583 /* Initialize the object. */
1584 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1585 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1586 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1587 light_idx, light->type, debug_color(&light->diffuse),
1588 debug_color(&light->specular), debug_color(&light->ambient),
1589 light->position.x, light->position.y, light->position.z,
1590 light->direction.x, light->direction.y, light->direction.z,
1591 light->range, light->falloff, light->theta, light->phi);
1593 /* Save away the information. */
1594 object->OriginalParms = *light;
1596 switch (light->type)
1598 case WINED3D_LIGHT_POINT:
1599 /* Position */
1600 object->position.x = light->position.x;
1601 object->position.y = light->position.y;
1602 object->position.z = light->position.z;
1603 object->position.w = 1.0f;
1604 object->cutoff = 180.0f;
1605 /* FIXME: Range */
1606 break;
1608 case WINED3D_LIGHT_DIRECTIONAL:
1609 /* Direction */
1610 object->direction.x = -light->direction.x;
1611 object->direction.y = -light->direction.y;
1612 object->direction.z = -light->direction.z;
1613 object->direction.w = 0.0f;
1614 object->exponent = 0.0f;
1615 object->cutoff = 180.0f;
1616 break;
1618 case WINED3D_LIGHT_SPOT:
1619 /* Position */
1620 object->position.x = light->position.x;
1621 object->position.y = light->position.y;
1622 object->position.z = light->position.z;
1623 object->position.w = 1.0f;
1625 /* Direction */
1626 object->direction.x = light->direction.x;
1627 object->direction.y = light->direction.y;
1628 object->direction.z = light->direction.z;
1629 object->direction.w = 0.0f;
1631 /* opengl-ish and d3d-ish spot lights use too different models
1632 * for the light "intensity" as a function of the angle towards
1633 * the main light direction, so we only can approximate very
1634 * roughly. However, spot lights are rather rarely used in games
1635 * (if ever used at all). Furthermore if still used, probably
1636 * nobody pays attention to such details. */
1637 if (!light->falloff)
1639 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1640 * equations have the falloff resp. exponent parameter as an
1641 * exponent, so the spot light lighting will always be 1.0 for
1642 * both of them, and we don't have to care for the rest of the
1643 * rather complex calculation. */
1644 object->exponent = 0.0f;
1646 else
1648 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1649 if (rho < 0.0001f)
1650 rho = 0.0001f;
1651 object->exponent = -0.3f / logf(cosf(rho / 2));
1654 if (object->exponent > 128.0f)
1655 object->exponent = 128.0f;
1657 object->cutoff = (float)(light->phi * 90 / M_PI);
1658 /* FIXME: Range */
1659 break;
1661 case WINED3D_LIGHT_PARALLELPOINT:
1662 object->position.x = light->position.x;
1663 object->position.y = light->position.y;
1664 object->position.z = light->position.z;
1665 object->position.w = 1.0f;
1666 break;
1668 default:
1669 FIXME("Unrecognized light type %#x.\n", light->type);
1672 if (!device->recording)
1673 wined3d_cs_emit_set_light(device->cs, object);
1675 return WINED3D_OK;
1678 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1679 UINT light_idx, struct wined3d_light *light)
1681 struct wined3d_light_info *light_info;
1683 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1685 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1687 TRACE("Light information requested but light not defined\n");
1688 return WINED3DERR_INVALIDCALL;
1691 *light = light_info->OriginalParms;
1692 return WINED3D_OK;
1695 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1697 struct wined3d_light_info *light_info;
1699 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1701 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1702 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1704 TRACE("Light enabled requested but light not defined, so defining one!\n");
1705 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1707 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1709 FIXME("Adding default lights has failed dismally\n");
1710 return WINED3DERR_INVALIDCALL;
1714 wined3d_state_enable_light(device->update_state, &device->adapter->d3d_info, light_info, enable);
1715 if (!device->recording)
1716 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1718 return WINED3D_OK;
1721 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1723 struct wined3d_light_info *light_info;
1725 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1727 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1729 TRACE("Light enabled state requested but light not defined.\n");
1730 return WINED3DERR_INVALIDCALL;
1732 /* true is 128 according to SetLightEnable */
1733 *enable = light_info->enabled ? 128 : 0;
1734 return WINED3D_OK;
1737 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1738 UINT plane_idx, const struct wined3d_vec4 *plane)
1740 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1742 /* Validate plane_idx. */
1743 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1745 TRACE("Application has requested clipplane this device doesn't support.\n");
1746 return WINED3DERR_INVALIDCALL;
1749 if (device->recording)
1750 device->recording->changed.clipplane |= 1u << plane_idx;
1752 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1754 TRACE("Application is setting old values over, nothing to do.\n");
1755 return WINED3D_OK;
1758 device->update_state->clip_planes[plane_idx] = *plane;
1760 if (!device->recording)
1761 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1763 return WINED3D_OK;
1766 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1767 UINT plane_idx, struct wined3d_vec4 *plane)
1769 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1771 /* Validate plane_idx. */
1772 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1774 TRACE("Application has requested clipplane this device doesn't support.\n");
1775 return WINED3DERR_INVALIDCALL;
1778 *plane = device->state.clip_planes[plane_idx];
1780 return WINED3D_OK;
1783 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1784 const struct wined3d_clip_status *clip_status)
1786 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1788 if (!clip_status)
1789 return WINED3DERR_INVALIDCALL;
1791 return WINED3D_OK;
1794 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1795 struct wined3d_clip_status *clip_status)
1797 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1799 if (!clip_status)
1800 return WINED3DERR_INVALIDCALL;
1802 return WINED3D_OK;
1805 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1807 TRACE("device %p, material %p.\n", device, material);
1809 device->update_state->material = *material;
1811 if (device->recording)
1812 device->recording->changed.material = TRUE;
1813 else
1814 wined3d_cs_emit_set_material(device->cs, material);
1817 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1819 TRACE("device %p, material %p.\n", device, material);
1821 *material = device->state.material;
1823 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1824 TRACE("ambient %s\n", debug_color(&material->ambient));
1825 TRACE("specular %s\n", debug_color(&material->specular));
1826 TRACE("emissive %s\n", debug_color(&material->emissive));
1827 TRACE("power %.8e.\n", material->power);
1830 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1831 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1833 enum wined3d_format_id prev_format;
1834 struct wined3d_buffer *prev_buffer;
1835 unsigned int prev_offset;
1837 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1838 device, buffer, debug_d3dformat(format_id), offset);
1840 prev_buffer = device->update_state->index_buffer;
1841 prev_format = device->update_state->index_format;
1842 prev_offset = device->update_state->index_offset;
1844 device->update_state->index_buffer = buffer;
1845 device->update_state->index_format = format_id;
1846 device->update_state->index_offset = offset;
1848 if (device->recording)
1849 device->recording->changed.indices = TRUE;
1851 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1852 return;
1854 if (buffer)
1855 wined3d_buffer_incref(buffer);
1856 if (!device->recording)
1857 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1858 if (prev_buffer)
1859 wined3d_buffer_decref(prev_buffer);
1862 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1863 enum wined3d_format_id *format, unsigned int *offset)
1865 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1867 *format = device->state.index_format;
1868 if (offset)
1869 *offset = device->state.index_offset;
1870 return device->state.index_buffer;
1873 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1875 TRACE("device %p, base_index %d.\n", device, base_index);
1877 device->update_state->base_vertex_index = base_index;
1880 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1882 TRACE("device %p.\n", device);
1884 return device->state.base_vertex_index;
1887 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1889 TRACE("device %p, viewport %p.\n", device, viewport);
1890 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1891 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1893 device->update_state->viewport = *viewport;
1895 /* Handle recording of state blocks */
1896 if (device->recording)
1898 TRACE("Recording... not performing anything\n");
1899 device->recording->changed.viewport = TRUE;
1900 return;
1903 wined3d_cs_emit_set_viewport(device->cs, viewport);
1906 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1908 TRACE("device %p, viewport %p.\n", device, viewport);
1910 *viewport = device->state.viewport;
1913 static void resolve_depth_buffer(struct wined3d_state *state)
1915 struct wined3d_texture *dst_texture = state->textures[0];
1916 struct wined3d_rendertarget_view *src_view;
1917 RECT src_rect, dst_rect;
1919 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1920 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1921 return;
1923 if (!(src_view = state->fb->depth_stencil))
1924 return;
1925 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
1927 FIXME("Not supported on buffer resources.\n");
1928 return;
1931 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
1932 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
1933 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
1934 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1937 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1938 struct wined3d_rasterizer_state *rasterizer_state)
1940 struct wined3d_rasterizer_state *prev;
1942 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
1944 prev = device->update_state->rasterizer_state;
1945 if (prev == rasterizer_state)
1946 return;
1948 if (rasterizer_state)
1949 wined3d_rasterizer_state_incref(rasterizer_state);
1950 device->update_state->rasterizer_state = rasterizer_state;
1951 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
1952 if (prev)
1953 wined3d_rasterizer_state_decref(prev);
1956 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
1958 TRACE("device %p.\n", device);
1960 return device->state.rasterizer_state;
1963 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1964 enum wined3d_render_state state, DWORD value)
1966 DWORD old_value;
1968 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1970 if (state > WINEHIGHEST_RENDER_STATE)
1972 WARN("Unhandled render state %#x.\n", state);
1973 return;
1976 old_value = device->state.render_states[state];
1977 device->update_state->render_states[state] = value;
1979 /* Handle recording of state blocks. */
1980 if (device->recording)
1982 TRACE("Recording... not performing anything.\n");
1983 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1984 return;
1987 /* Compared here and not before the assignment to allow proper stateblock recording. */
1988 if (value == old_value)
1989 TRACE("Application is setting the old value over, nothing to do.\n");
1990 else
1991 wined3d_cs_emit_set_render_state(device->cs, state, value);
1993 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1995 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1996 resolve_depth_buffer(&device->state);
2000 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2002 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2004 return device->state.render_states[state];
2007 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2008 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2010 DWORD old_value;
2012 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2013 device, sampler_idx, debug_d3dsamplerstate(state), value);
2015 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2016 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2018 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2020 WARN("Invalid sampler %u.\n", sampler_idx);
2021 return; /* Windows accepts overflowing this array ... we do not. */
2024 old_value = device->state.sampler_states[sampler_idx][state];
2025 device->update_state->sampler_states[sampler_idx][state] = value;
2027 /* Handle recording of state blocks. */
2028 if (device->recording)
2030 TRACE("Recording... not performing anything.\n");
2031 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2032 return;
2035 if (old_value == value)
2037 TRACE("Application is setting the old value over, nothing to do.\n");
2038 return;
2041 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2044 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2045 UINT sampler_idx, enum wined3d_sampler_state state)
2047 TRACE("device %p, sampler_idx %u, state %s.\n",
2048 device, sampler_idx, debug_d3dsamplerstate(state));
2050 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2051 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2053 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2055 WARN("Invalid sampler %u.\n", sampler_idx);
2056 return 0; /* Windows accepts overflowing this array ... we do not. */
2059 return device->state.sampler_states[sampler_idx][state];
2062 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2064 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2066 if (device->recording)
2067 device->recording->changed.scissorRect = TRUE;
2069 if (EqualRect(&device->update_state->scissor_rect, rect))
2071 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2072 return;
2074 CopyRect(&device->update_state->scissor_rect, rect);
2076 if (device->recording)
2078 TRACE("Recording... not performing anything.\n");
2079 return;
2082 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2085 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2087 TRACE("device %p, rect %p.\n", device, rect);
2089 *rect = device->state.scissor_rect;
2090 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2093 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2094 struct wined3d_vertex_declaration *declaration)
2096 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2098 TRACE("device %p, declaration %p.\n", device, declaration);
2100 if (device->recording)
2101 device->recording->changed.vertexDecl = TRUE;
2103 if (declaration == prev)
2104 return;
2106 if (declaration)
2107 wined3d_vertex_declaration_incref(declaration);
2108 device->update_state->vertex_declaration = declaration;
2109 if (!device->recording)
2110 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2111 if (prev)
2112 wined3d_vertex_declaration_decref(prev);
2115 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2117 TRACE("device %p.\n", device);
2119 return device->state.vertex_declaration;
2122 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2124 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2126 TRACE("device %p, shader %p.\n", device, shader);
2128 if (device->recording)
2129 device->recording->changed.vertexShader = TRUE;
2131 if (shader == prev)
2132 return;
2134 if (shader)
2135 wined3d_shader_incref(shader);
2136 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2137 if (!device->recording)
2138 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2139 if (prev)
2140 wined3d_shader_decref(prev);
2143 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2145 TRACE("device %p.\n", device);
2147 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2150 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2151 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2153 struct wined3d_buffer *prev;
2155 if (idx >= MAX_CONSTANT_BUFFERS)
2157 WARN("Invalid constant buffer index %u.\n", idx);
2158 return;
2161 prev = device->update_state->cb[type][idx];
2162 if (buffer == prev)
2163 return;
2165 if (buffer)
2166 wined3d_buffer_incref(buffer);
2167 device->update_state->cb[type][idx] = buffer;
2168 if (!device->recording)
2169 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2170 if (prev)
2171 wined3d_buffer_decref(prev);
2174 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2176 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2178 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2181 static struct wined3d_buffer *wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2182 enum wined3d_shader_type shader_type, unsigned int idx)
2184 if (idx >= MAX_CONSTANT_BUFFERS)
2186 WARN("Invalid constant buffer index %u.\n", idx);
2187 return NULL;
2190 return device->state.cb[shader_type][idx];
2193 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2195 TRACE("device %p, idx %u.\n", device, idx);
2197 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2200 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2201 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2203 struct wined3d_shader_resource_view *prev;
2205 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2207 WARN("Invalid view index %u.\n", idx);
2208 return;
2211 prev = device->update_state->shader_resource_view[type][idx];
2212 if (view == prev)
2213 return;
2215 if (view)
2216 wined3d_shader_resource_view_incref(view);
2217 device->update_state->shader_resource_view[type][idx] = view;
2218 if (!device->recording)
2219 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2220 if (prev)
2221 wined3d_shader_resource_view_decref(prev);
2224 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2225 UINT idx, struct wined3d_shader_resource_view *view)
2227 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2229 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2232 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2233 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2235 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2237 WARN("Invalid view index %u.\n", idx);
2238 return NULL;
2241 return device->state.shader_resource_view[shader_type][idx];
2244 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2245 UINT idx)
2247 TRACE("device %p, idx %u.\n", device, idx);
2249 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2252 static void wined3d_device_set_sampler(struct wined3d_device *device,
2253 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2255 struct wined3d_sampler *prev;
2257 if (idx >= MAX_SAMPLER_OBJECTS)
2259 WARN("Invalid sampler index %u.\n", idx);
2260 return;
2263 prev = device->update_state->sampler[type][idx];
2264 if (sampler == prev)
2265 return;
2267 if (sampler)
2268 wined3d_sampler_incref(sampler);
2269 device->update_state->sampler[type][idx] = sampler;
2270 if (!device->recording)
2271 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2272 if (prev)
2273 wined3d_sampler_decref(prev);
2276 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2278 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2280 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2283 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2284 enum wined3d_shader_type shader_type, unsigned int idx)
2286 if (idx >= MAX_SAMPLER_OBJECTS)
2288 WARN("Invalid sampler index %u.\n", idx);
2289 return NULL;
2292 return device->state.sampler[shader_type][idx];
2295 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2297 TRACE("device %p, idx %u.\n", device, idx);
2299 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2302 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2303 unsigned int start_idx, unsigned int count, const BOOL *constants)
2305 unsigned int i;
2307 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2308 device, start_idx, count, constants);
2310 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2311 return WINED3DERR_INVALIDCALL;
2313 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2314 count = WINED3D_MAX_CONSTS_B - start_idx;
2315 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2316 if (TRACE_ON(d3d))
2318 for (i = 0; i < count; ++i)
2319 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2322 if (device->recording)
2324 for (i = start_idx; i < count + start_idx; ++i)
2325 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2327 else
2329 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2332 return WINED3D_OK;
2335 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2336 unsigned int start_idx, unsigned int count, BOOL *constants)
2338 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2339 device, start_idx, count, constants);
2341 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2342 return WINED3DERR_INVALIDCALL;
2344 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2345 count = WINED3D_MAX_CONSTS_B - start_idx;
2346 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2348 return WINED3D_OK;
2351 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2352 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2354 unsigned int i;
2356 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2357 device, start_idx, count, constants);
2359 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2360 return WINED3DERR_INVALIDCALL;
2362 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2363 count = WINED3D_MAX_CONSTS_I - start_idx;
2364 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2365 if (TRACE_ON(d3d))
2367 for (i = 0; i < count; ++i)
2368 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2371 if (device->recording)
2373 for (i = start_idx; i < count + start_idx; ++i)
2374 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2376 else
2378 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2381 return WINED3D_OK;
2384 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2385 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2387 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2388 device, start_idx, count, constants);
2390 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2391 return WINED3DERR_INVALIDCALL;
2393 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2394 count = WINED3D_MAX_CONSTS_I - start_idx;
2395 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2396 return WINED3D_OK;
2399 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2400 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2402 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2403 unsigned int i;
2405 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2406 device, start_idx, count, constants);
2408 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2409 || count > d3d_info->limits.vs_uniform_count - start_idx)
2410 return WINED3DERR_INVALIDCALL;
2412 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2413 if (TRACE_ON(d3d))
2415 for (i = 0; i < count; ++i)
2416 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2419 if (device->recording)
2420 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2421 count * sizeof(*device->recording->changed.vs_consts_f));
2422 else
2423 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2425 return WINED3D_OK;
2428 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2429 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2431 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2433 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2434 device, start_idx, count, constants);
2436 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2437 || count > d3d_info->limits.vs_uniform_count - start_idx)
2438 return WINED3DERR_INVALIDCALL;
2440 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2442 return WINED3D_OK;
2445 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2447 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2449 TRACE("device %p, shader %p.\n", device, shader);
2451 if (device->recording)
2452 device->recording->changed.pixelShader = TRUE;
2454 if (shader == prev)
2455 return;
2457 if (shader)
2458 wined3d_shader_incref(shader);
2459 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2460 if (!device->recording)
2461 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2462 if (prev)
2463 wined3d_shader_decref(prev);
2466 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2468 TRACE("device %p.\n", device);
2470 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2473 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2475 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2477 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2480 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2482 TRACE("device %p, idx %u.\n", device, idx);
2484 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2487 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2488 UINT idx, struct wined3d_shader_resource_view *view)
2490 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2492 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2495 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2496 UINT idx)
2498 TRACE("device %p, idx %u.\n", device, idx);
2500 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2503 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2505 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2507 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2510 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2512 TRACE("device %p, idx %u.\n", device, idx);
2514 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2517 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2518 unsigned int start_idx, unsigned int count, const BOOL *constants)
2520 unsigned int i;
2522 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2523 device, start_idx, count, constants);
2525 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2526 return WINED3DERR_INVALIDCALL;
2528 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2529 count = WINED3D_MAX_CONSTS_B - start_idx;
2530 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2531 if (TRACE_ON(d3d))
2533 for (i = 0; i < count; ++i)
2534 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2537 if (device->recording)
2539 for (i = start_idx; i < count + start_idx; ++i)
2540 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2542 else
2544 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2547 return WINED3D_OK;
2550 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2551 unsigned int start_idx, unsigned int count, BOOL *constants)
2553 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2554 device, start_idx, count, constants);
2556 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2557 return WINED3DERR_INVALIDCALL;
2559 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2560 count = WINED3D_MAX_CONSTS_B - start_idx;
2561 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2563 return WINED3D_OK;
2566 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2567 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2569 unsigned int i;
2571 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2572 device, start_idx, count, constants);
2574 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2575 return WINED3DERR_INVALIDCALL;
2577 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2578 count = WINED3D_MAX_CONSTS_I - start_idx;
2579 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2580 if (TRACE_ON(d3d))
2582 for (i = 0; i < count; ++i)
2583 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2586 if (device->recording)
2588 for (i = start_idx; i < count + start_idx; ++i)
2589 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2591 else
2593 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2596 return WINED3D_OK;
2599 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2600 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2602 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2603 device, start_idx, count, constants);
2605 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2606 return WINED3DERR_INVALIDCALL;
2608 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2609 count = WINED3D_MAX_CONSTS_I - start_idx;
2610 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2612 return WINED3D_OK;
2615 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2616 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2618 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2619 unsigned int i;
2621 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2622 device, start_idx, count, constants);
2624 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2625 || count > d3d_info->limits.ps_uniform_count - start_idx)
2626 return WINED3DERR_INVALIDCALL;
2628 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2629 if (TRACE_ON(d3d))
2631 for (i = 0; i < count; ++i)
2632 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2635 if (device->recording)
2636 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2637 count * sizeof(*device->recording->changed.ps_consts_f));
2638 else
2639 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2641 return WINED3D_OK;
2644 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2645 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2647 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2649 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2650 device, start_idx, count, constants);
2652 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2653 || count > d3d_info->limits.ps_uniform_count - start_idx)
2654 return WINED3DERR_INVALIDCALL;
2656 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2658 return WINED3D_OK;
2661 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2663 struct wined3d_shader *prev;
2665 TRACE("device %p, shader %p.\n", device, shader);
2667 prev = device->update_state->shader[WINED3D_SHADER_TYPE_HULL];
2668 if (shader == prev)
2669 return;
2670 if (shader)
2671 wined3d_shader_incref(shader);
2672 device->update_state->shader[WINED3D_SHADER_TYPE_HULL] = shader;
2673 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_HULL, shader);
2674 if (prev)
2675 wined3d_shader_decref(prev);
2678 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2680 TRACE("device %p.\n", device);
2682 return device->state.shader[WINED3D_SHADER_TYPE_HULL];
2685 void CDECL wined3d_device_set_hs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2687 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2689 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_HULL, idx, buffer);
2692 struct wined3d_buffer * CDECL wined3d_device_get_hs_cb(const struct wined3d_device *device, unsigned int idx)
2694 TRACE("device %p, idx %u.\n", device, idx);
2696 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_HULL, idx);
2699 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2700 unsigned int idx, struct wined3d_shader_resource_view *view)
2702 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2704 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx, view);
2707 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2708 unsigned int idx)
2710 TRACE("device %p, idx %u.\n", device, idx);
2712 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2715 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2716 unsigned int idx, struct wined3d_sampler *sampler)
2718 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2720 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2723 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2725 TRACE("device %p, idx %u.\n", device, idx);
2727 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2730 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2732 struct wined3d_shader *prev;
2734 TRACE("device %p, shader %p.\n", device, shader);
2736 prev = device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2737 if (shader == prev)
2738 return;
2739 if (shader)
2740 wined3d_shader_incref(shader);
2741 device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN] = shader;
2742 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_DOMAIN, shader);
2743 if (prev)
2744 wined3d_shader_decref(prev);
2747 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2749 TRACE("device %p.\n", device);
2751 return device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2754 void CDECL wined3d_device_set_ds_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2756 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2758 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_DOMAIN, idx, buffer);
2761 struct wined3d_buffer * CDECL wined3d_device_get_ds_cb(const struct wined3d_device *device, unsigned int idx)
2763 TRACE("device %p, idx %u.\n", device, idx);
2765 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2768 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2769 unsigned int idx, struct wined3d_shader_resource_view *view)
2771 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2773 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2776 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2777 unsigned int idx)
2779 TRACE("device %p, idx %u.\n", device, idx);
2781 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2784 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2785 unsigned int idx, struct wined3d_sampler *sampler)
2787 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2789 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2792 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2794 TRACE("device %p, idx %u.\n", device, idx);
2796 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2799 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2801 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2803 TRACE("device %p, shader %p.\n", device, shader);
2805 if (device->recording || shader == prev)
2806 return;
2807 if (shader)
2808 wined3d_shader_incref(shader);
2809 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2810 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2811 if (prev)
2812 wined3d_shader_decref(prev);
2815 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2817 TRACE("device %p.\n", device);
2819 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2822 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2824 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2826 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2829 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2831 TRACE("device %p, idx %u.\n", device, idx);
2833 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2836 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2837 UINT idx, struct wined3d_shader_resource_view *view)
2839 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2841 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2844 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2845 UINT idx)
2847 TRACE("device %p, idx %u.\n", device, idx);
2849 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2852 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2854 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2856 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2859 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2861 TRACE("device %p, idx %u.\n", device, idx);
2863 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2866 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2868 struct wined3d_shader *prev;
2870 TRACE("device %p, shader %p.\n", device, shader);
2872 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2873 if (device->recording || shader == prev)
2874 return;
2875 if (shader)
2876 wined3d_shader_incref(shader);
2877 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2878 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2879 if (prev)
2880 wined3d_shader_decref(prev);
2883 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2885 TRACE("device %p.\n", device);
2887 return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2890 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2892 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2894 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2897 struct wined3d_buffer * CDECL wined3d_device_get_cs_cb(const struct wined3d_device *device, unsigned int idx)
2899 TRACE("device %p, idx %u.\n", device, idx);
2901 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2904 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2905 unsigned int idx, struct wined3d_shader_resource_view *view)
2907 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2909 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2912 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2913 unsigned int idx)
2915 TRACE("device %p, idx %u.\n", device, idx);
2917 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2920 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2921 unsigned int idx, struct wined3d_sampler *sampler)
2923 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2925 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2928 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2930 TRACE("device %p, idx %u.\n", device, idx);
2932 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2935 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2936 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav)
2938 struct wined3d_unordered_access_view *prev;
2940 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2942 WARN("Invalid UAV index %u.\n", idx);
2943 return;
2946 prev = device->update_state->unordered_access_view[pipeline][idx];
2947 if (uav == prev)
2948 return;
2950 if (uav)
2951 wined3d_unordered_access_view_incref(uav);
2952 device->update_state->unordered_access_view[pipeline][idx] = uav;
2953 if (!device->recording)
2954 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav);
2955 if (prev)
2956 wined3d_unordered_access_view_decref(prev);
2959 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2960 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2962 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2964 WARN("Invalid UAV index %u.\n", idx);
2965 return NULL;
2968 return device->state.unordered_access_view[pipeline][idx];
2971 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2972 struct wined3d_unordered_access_view *uav)
2974 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2976 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav);
2979 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
2980 unsigned int idx)
2982 TRACE("device %p, idx %u.\n", device, idx);
2984 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
2987 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2988 unsigned int idx, struct wined3d_unordered_access_view *uav)
2990 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2992 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav);
2995 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
2996 const struct wined3d_device *device, unsigned int idx)
2998 TRACE("device %p, idx %u.\n", device, idx);
3000 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
3003 /* Context activation is done by the caller. */
3004 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3005 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3006 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3007 DWORD DestFVF)
3009 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3010 struct wined3d_map_desc map_desc;
3011 struct wined3d_box box = {0};
3012 struct wined3d_viewport vp;
3013 UINT vertex_size;
3014 unsigned int i;
3015 BYTE *dest_ptr;
3016 BOOL doClip;
3017 DWORD numTextures;
3018 HRESULT hr;
3020 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3022 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3025 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3027 ERR("Source has no position mask\n");
3028 return WINED3DERR_INVALIDCALL;
3031 if (device->state.render_states[WINED3D_RS_CLIPPING])
3033 static BOOL warned = FALSE;
3035 * The clipping code is not quite correct. Some things need
3036 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3037 * so disable clipping for now.
3038 * (The graphics in Half-Life are broken, and my processvertices
3039 * test crashes with IDirect3DDevice3)
3040 doClip = TRUE;
3042 doClip = FALSE;
3043 if(!warned) {
3044 warned = TRUE;
3045 FIXME("Clipping is broken and disabled for now\n");
3048 else
3049 doClip = FALSE;
3051 vertex_size = get_flexible_vertex_size(DestFVF);
3052 box.left = dwDestIndex * vertex_size;
3053 box.right = box.left + dwCount * vertex_size;
3054 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
3056 WARN("Failed to map buffer, hr %#x.\n", hr);
3057 return hr;
3059 dest_ptr = map_desc.data;
3061 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3062 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3063 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3065 TRACE("View mat:\n");
3066 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3067 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3068 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3069 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3071 TRACE("Proj mat:\n");
3072 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3073 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3074 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3075 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3077 TRACE("World mat:\n");
3078 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3079 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3080 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3081 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3083 /* Get the viewport */
3084 wined3d_device_get_viewport(device, &vp);
3085 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3086 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3088 multiply_matrix(&mat,&view_mat,&world_mat);
3089 multiply_matrix(&mat,&proj_mat,&mat);
3091 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3093 for (i = 0; i < dwCount; i+= 1) {
3094 unsigned int tex_index;
3096 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3097 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3098 /* The position first */
3099 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3100 const float *p = (const float *)(element->data.addr + i * element->stride);
3101 float x, y, z, rhw;
3102 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3104 /* Multiplication with world, view and projection matrix. */
3105 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3106 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3107 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3108 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3110 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3112 /* WARNING: The following things are taken from d3d7 and were not yet checked
3113 * against d3d8 or d3d9!
3116 /* Clipping conditions: From msdn
3118 * A vertex is clipped if it does not match the following requirements
3119 * -rhw < x <= rhw
3120 * -rhw < y <= rhw
3121 * 0 < z <= rhw
3122 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3124 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3125 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3129 if( !doClip ||
3130 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3131 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3132 ( rhw > eps ) ) ) {
3134 /* "Normal" viewport transformation (not clipped)
3135 * 1) The values are divided by rhw
3136 * 2) The y axis is negative, so multiply it with -1
3137 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3138 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3139 * 4) Multiply x with Width/2 and add Width/2
3140 * 5) The same for the height
3141 * 6) Add the viewpoint X and Y to the 2D coordinates and
3142 * The minimum Z value to z
3143 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3145 * Well, basically it's simply a linear transformation into viewport
3146 * coordinates
3149 x /= rhw;
3150 y /= rhw;
3151 z /= rhw;
3153 y *= -1;
3155 x *= vp.width / 2;
3156 y *= vp.height / 2;
3157 z *= vp.max_z - vp.min_z;
3159 x += vp.width / 2 + vp.x;
3160 y += vp.height / 2 + vp.y;
3161 z += vp.min_z;
3163 rhw = 1 / rhw;
3164 } else {
3165 /* That vertex got clipped
3166 * Contrary to OpenGL it is not dropped completely, it just
3167 * undergoes a different calculation.
3169 TRACE("Vertex got clipped\n");
3170 x += rhw;
3171 y += rhw;
3173 x /= 2;
3174 y /= 2;
3176 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3177 * outside of the main vertex buffer memory. That needs some more
3178 * investigation...
3182 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3185 ( (float *) dest_ptr)[0] = x;
3186 ( (float *) dest_ptr)[1] = y;
3187 ( (float *) dest_ptr)[2] = z;
3188 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3190 dest_ptr += 3 * sizeof(float);
3192 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3193 dest_ptr += sizeof(float);
3196 if (DestFVF & WINED3DFVF_PSIZE)
3197 dest_ptr += sizeof(DWORD);
3199 if (DestFVF & WINED3DFVF_NORMAL)
3201 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3202 const float *normal = (const float *)(element->data.addr + i * element->stride);
3203 /* AFAIK this should go into the lighting information */
3204 FIXME("Didn't expect the destination to have a normal\n");
3205 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3208 if (DestFVF & WINED3DFVF_DIFFUSE)
3210 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3211 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3212 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3214 static BOOL warned = FALSE;
3216 if(!warned) {
3217 ERR("No diffuse color in source, but destination has one\n");
3218 warned = TRUE;
3221 *( (DWORD *) dest_ptr) = 0xffffffff;
3222 dest_ptr += sizeof(DWORD);
3224 else
3226 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3230 if (DestFVF & WINED3DFVF_SPECULAR)
3232 /* What's the color value in the feedback buffer? */
3233 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3234 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3235 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3237 static BOOL warned = FALSE;
3239 if(!warned) {
3240 ERR("No specular color in source, but destination has one\n");
3241 warned = TRUE;
3244 *(DWORD *)dest_ptr = 0xff000000;
3245 dest_ptr += sizeof(DWORD);
3247 else
3249 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3253 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3255 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3256 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3257 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3259 ERR("No source texture, but destination requests one\n");
3260 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3262 else
3264 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3269 wined3d_resource_unmap(&dest->resource, 0);
3271 return WINED3D_OK;
3273 #undef copy_and_next
3275 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3276 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3277 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3279 struct wined3d_state *state = &device->state;
3280 struct wined3d_stream_info stream_info;
3281 struct wined3d_resource *resource;
3282 struct wined3d_box box = {0};
3283 struct wined3d_shader *vs;
3284 unsigned int i;
3285 HRESULT hr;
3286 WORD map;
3288 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3289 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3290 device, src_start_idx, dst_idx, vertex_count,
3291 dst_buffer, declaration, flags, dst_fvf);
3293 if (declaration)
3294 FIXME("Output vertex declaration not implemented yet.\n");
3296 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3297 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3298 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->gl_info, &device->adapter->d3d_info);
3299 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3301 /* We can't convert FROM a VBO, and vertex buffers used to source into
3302 * process_vertices() are unlikely to ever be used for drawing. Release
3303 * VBOs in those buffers and fix up the stream_info structure.
3305 * Also apply the start index. */
3306 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3308 struct wined3d_stream_info_element *e;
3309 struct wined3d_map_desc map_desc;
3311 if (!(map & 1))
3312 continue;
3314 e = &stream_info.elements[i];
3315 resource = &state->streams[e->stream_idx].buffer->resource;
3316 box.left = src_start_idx * e->stride;
3317 box.right = box.left + vertex_count * e->stride;
3318 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READONLY)))
3319 ERR("Failed to map resource.\n");
3320 e->data.buffer_object = 0;
3321 e->data.addr += (ULONG_PTR)map_desc.data;
3324 hr = process_vertices_strided(device, dst_idx, vertex_count,
3325 &stream_info, dst_buffer, flags, dst_fvf);
3327 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3329 if (!(map & 1))
3330 continue;
3332 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3333 if (FAILED(wined3d_resource_unmap(resource, 0)))
3334 ERR("Failed to unmap resource.\n");
3337 return hr;
3340 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3341 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3343 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3344 DWORD old_value;
3346 TRACE("device %p, stage %u, state %s, value %#x.\n",
3347 device, stage, debug_d3dtexturestate(state), value);
3349 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3351 WARN("Invalid state %#x passed.\n", state);
3352 return;
3355 if (stage >= d3d_info->limits.ffp_blend_stages)
3357 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3358 stage, d3d_info->limits.ffp_blend_stages - 1);
3359 return;
3362 old_value = device->update_state->texture_states[stage][state];
3363 device->update_state->texture_states[stage][state] = value;
3365 if (device->recording)
3367 TRACE("Recording... not performing anything.\n");
3368 device->recording->changed.textureState[stage] |= 1u << state;
3369 return;
3372 /* Checked after the assignments to allow proper stateblock recording. */
3373 if (old_value == value)
3375 TRACE("Application is setting the old value over, nothing to do.\n");
3376 return;
3379 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3382 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3383 UINT stage, enum wined3d_texture_stage_state state)
3385 TRACE("device %p, stage %u, state %s.\n",
3386 device, stage, debug_d3dtexturestate(state));
3388 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3390 WARN("Invalid state %#x passed.\n", state);
3391 return 0;
3394 return device->state.texture_states[stage][state];
3397 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3398 UINT stage, struct wined3d_texture *texture)
3400 struct wined3d_texture *prev;
3402 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3404 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3405 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3407 /* Windows accepts overflowing this array... we do not. */
3408 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3410 WARN("Ignoring invalid stage %u.\n", stage);
3411 return WINED3D_OK;
3414 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3416 WARN("Rejecting attempt to set scratch texture.\n");
3417 return WINED3DERR_INVALIDCALL;
3420 if (device->recording)
3421 device->recording->changed.textures |= 1u << stage;
3423 prev = device->update_state->textures[stage];
3424 TRACE("Previous texture %p.\n", prev);
3426 if (texture == prev)
3428 TRACE("App is setting the same texture again, nothing to do.\n");
3429 return WINED3D_OK;
3432 TRACE("Setting new texture to %p.\n", texture);
3433 device->update_state->textures[stage] = texture;
3435 if (texture)
3436 wined3d_texture_incref(texture);
3437 if (!device->recording)
3438 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3439 if (prev)
3440 wined3d_texture_decref(prev);
3442 return WINED3D_OK;
3445 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3447 TRACE("device %p, stage %u.\n", device, stage);
3449 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3450 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3452 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3454 WARN("Ignoring invalid stage %u.\n", stage);
3455 return NULL; /* Windows accepts overflowing this array ... we do not. */
3458 return device->state.textures[stage];
3461 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3463 TRACE("device %p, caps %p.\n", device, caps);
3465 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3466 device->create_parms.device_type, caps);
3469 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3470 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3472 struct wined3d_swapchain *swapchain;
3474 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3475 device, swapchain_idx, mode, rotation);
3477 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3478 return WINED3DERR_INVALIDCALL;
3480 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3483 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3485 struct wined3d_stateblock *stateblock;
3486 HRESULT hr;
3488 TRACE("device %p.\n", device);
3490 if (device->recording)
3491 return WINED3DERR_INVALIDCALL;
3493 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3494 if (FAILED(hr))
3495 return hr;
3497 device->recording = stateblock;
3498 device->update_state = &stateblock->state;
3500 TRACE("Recording stateblock %p.\n", stateblock);
3502 return WINED3D_OK;
3505 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3506 struct wined3d_stateblock **stateblock)
3508 struct wined3d_stateblock *object = device->recording;
3510 TRACE("device %p, stateblock %p.\n", device, stateblock);
3512 if (!device->recording)
3514 WARN("Not recording.\n");
3515 *stateblock = NULL;
3516 return WINED3DERR_INVALIDCALL;
3519 stateblock_init_contained_states(object);
3521 *stateblock = object;
3522 device->recording = NULL;
3523 device->update_state = &device->state;
3525 TRACE("Returning stateblock %p.\n", *stateblock);
3527 return WINED3D_OK;
3530 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3532 /* At the moment we have no need for any functionality at the beginning
3533 * of a scene. */
3534 TRACE("device %p.\n", device);
3536 if (device->inScene)
3538 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3539 return WINED3DERR_INVALIDCALL;
3541 device->inScene = TRUE;
3542 return WINED3D_OK;
3545 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3547 TRACE("device %p.\n", device);
3549 if (!device->inScene)
3551 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3552 return WINED3DERR_INVALIDCALL;
3555 wined3d_cs_emit_flush(device->cs);
3557 device->inScene = FALSE;
3558 return WINED3D_OK;
3561 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3562 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3564 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3565 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3567 if (!rect_count && rects)
3569 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3570 return WINED3D_OK;
3573 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3575 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3576 if (!ds)
3578 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3579 /* TODO: What about depth stencil buffers without stencil bits? */
3580 return WINED3DERR_INVALIDCALL;
3582 else if (flags & WINED3DCLEAR_TARGET)
3584 if (ds->width < device->fb.render_targets[0]->width
3585 || ds->height < device->fb.render_targets[0]->height)
3587 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3588 return WINED3D_OK;
3593 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3595 return WINED3D_OK;
3598 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3599 struct wined3d_query *predicate, BOOL value)
3601 struct wined3d_query *prev;
3603 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3605 prev = device->update_state->predicate;
3606 if (predicate)
3608 FIXME("Predicated rendering not implemented.\n");
3609 wined3d_query_incref(predicate);
3611 device->update_state->predicate = predicate;
3612 device->update_state->predicate_value = value;
3613 if (!device->recording)
3614 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3615 if (prev)
3616 wined3d_query_decref(prev);
3619 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3621 TRACE("device %p, value %p.\n", device, value);
3623 *value = device->state.predicate_value;
3624 return device->state.predicate;
3627 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3628 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3630 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3631 device, group_count_x, group_count_y, group_count_z);
3633 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3636 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3637 enum wined3d_primitive_type primitive_type)
3639 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3641 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3644 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3645 enum wined3d_primitive_type *primitive_type)
3647 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3649 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3651 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3654 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3656 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3658 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3659 start_vertex, vertex_count, 0, 0, FALSE);
3661 return WINED3D_OK;
3664 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3665 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3667 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3668 device, start_vertex, vertex_count, start_instance, instance_count);
3670 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3671 start_vertex, vertex_count, start_instance, instance_count, FALSE);
3674 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3676 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3678 if (!device->state.index_buffer)
3680 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3681 * without an index buffer set. (The first time at least...)
3682 * D3D8 simply dies, but I doubt it can do much harm to return
3683 * D3DERR_INVALIDCALL there as well. */
3684 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3685 return WINED3DERR_INVALIDCALL;
3688 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type,
3689 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3691 return WINED3D_OK;
3694 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3695 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3697 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3698 device, start_idx, index_count, start_instance, instance_count);
3700 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.base_vertex_index,
3701 start_idx, index_count, start_instance, instance_count, TRUE);
3704 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3705 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3707 unsigned int src_size, dst_size, src_skip_levels = 0;
3708 unsigned int src_level_count, dst_level_count;
3709 unsigned int layer_count, level_count, i, j;
3710 unsigned int width, height, depth;
3711 enum wined3d_resource_type type;
3712 struct wined3d_box box;
3714 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3716 /* Verify that the source and destination textures are non-NULL. */
3717 if (!src_texture || !dst_texture)
3719 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3720 return WINED3DERR_INVALIDCALL;
3723 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3725 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3726 return WINED3DERR_INVALIDCALL;
3728 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3730 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3731 return WINED3DERR_INVALIDCALL;
3734 /* Verify that the source and destination textures are the same type. */
3735 type = src_texture->resource.type;
3736 if (dst_texture->resource.type != type)
3738 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3739 return WINED3DERR_INVALIDCALL;
3742 layer_count = src_texture->layer_count;
3743 if (layer_count != dst_texture->layer_count)
3745 WARN("Source and destination have different layer counts.\n");
3746 return WINED3DERR_INVALIDCALL;
3749 if (src_texture->resource.format != dst_texture->resource.format)
3751 WARN("Source and destination formats do not match.\n");
3752 return WINED3DERR_INVALIDCALL;
3755 src_level_count = src_texture->level_count;
3756 dst_level_count = dst_texture->level_count;
3757 level_count = min(src_level_count, dst_level_count);
3759 src_size = max(src_texture->resource.width, src_texture->resource.height);
3760 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3761 if (type == WINED3D_RTYPE_TEXTURE_3D)
3763 src_size = max(src_size, src_texture->resource.depth);
3764 dst_size = max(dst_size, dst_texture->resource.depth);
3766 while (src_size > dst_size)
3768 src_size >>= 1;
3769 ++src_skip_levels;
3772 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3773 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3774 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3776 WARN("Source and destination dimensions do not match.\n");
3777 return WINED3DERR_INVALIDCALL;
3780 /* Update every surface level of the texture. */
3781 for (i = 0; i < level_count; ++i)
3783 width = wined3d_texture_get_level_width(dst_texture, i);
3784 height = wined3d_texture_get_level_height(dst_texture, i);
3785 depth = wined3d_texture_get_level_depth(dst_texture, i);
3786 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3788 for (j = 0; j < layer_count; ++j)
3790 wined3d_cs_emit_blt_sub_resource(device->cs,
3791 &dst_texture->resource, j * dst_level_count + i, &box,
3792 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3793 0, NULL, WINED3D_TEXF_POINT);
3797 return WINED3D_OK;
3800 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3802 const struct wined3d_state *state = &device->state;
3803 struct wined3d_texture *texture;
3804 DWORD i;
3806 TRACE("device %p, num_passes %p.\n", device, num_passes);
3808 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3810 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3812 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3813 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3815 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3817 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3818 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3821 texture = state->textures[i];
3822 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3824 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3826 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3827 return E_FAIL;
3829 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3831 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3832 return E_FAIL;
3834 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3835 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3837 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3838 return E_FAIL;
3842 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3843 || state->render_states[WINED3D_RS_STENCILENABLE])
3845 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3846 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3848 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3850 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3851 return WINED3DERR_CONFLICTINGRENDERSTATE;
3855 /* return a sensible default */
3856 *num_passes = 1;
3858 TRACE("returning D3D_OK\n");
3859 return WINED3D_OK;
3862 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3864 static BOOL warned;
3866 TRACE("device %p, software %#x.\n", device, software);
3868 if (!warned)
3870 FIXME("device %p, software %#x stub!\n", device, software);
3871 warned = TRUE;
3874 device->softwareVertexProcessing = software;
3877 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3879 static BOOL warned;
3881 TRACE("device %p.\n", device);
3883 if (!warned)
3885 TRACE("device %p stub!\n", device);
3886 warned = TRUE;
3889 return device->softwareVertexProcessing;
3892 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3893 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3895 struct wined3d_swapchain *swapchain;
3897 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3898 device, swapchain_idx, raster_status);
3900 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3901 return WINED3DERR_INVALIDCALL;
3903 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3906 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3908 static BOOL warned;
3910 TRACE("device %p, segments %.8e.\n", device, segments);
3912 if (segments != 0.0f)
3914 if (!warned)
3916 FIXME("device %p, segments %.8e stub!\n", device, segments);
3917 warned = TRUE;
3921 return WINED3D_OK;
3924 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3926 static BOOL warned;
3928 TRACE("device %p.\n", device);
3930 if (!warned)
3932 FIXME("device %p stub!\n", device);
3933 warned = TRUE;
3936 return 0.0f;
3939 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3940 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3942 struct wined3d_texture *dst_texture, *src_texture;
3943 struct wined3d_box box;
3944 unsigned int i, j;
3946 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3948 if (src_resource == dst_resource)
3950 WARN("Source and destination are the same resource.\n");
3951 return;
3954 if (src_resource->type != dst_resource->type)
3956 WARN("Resource types (%s / %s) don't match.\n",
3957 debug_d3dresourcetype(dst_resource->type),
3958 debug_d3dresourcetype(src_resource->type));
3959 return;
3962 if (src_resource->width != dst_resource->width
3963 || src_resource->height != dst_resource->height
3964 || src_resource->depth != dst_resource->depth)
3966 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3967 dst_resource->width, dst_resource->height, dst_resource->depth,
3968 src_resource->width, src_resource->height, src_resource->depth);
3969 return;
3972 if (src_resource->format->id != dst_resource->format->id)
3974 WARN("Resource formats (%s / %s) don't match.\n",
3975 debug_d3dformat(dst_resource->format->id),
3976 debug_d3dformat(src_resource->format->id));
3977 return;
3980 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3982 wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
3983 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
3984 src_resource, 0, &box, 0, NULL, WINED3D_TEXF_POINT);
3985 return;
3988 dst_texture = texture_from_resource(dst_resource);
3989 src_texture = texture_from_resource(src_resource);
3991 if (src_texture->layer_count != dst_texture->layer_count
3992 || src_texture->level_count != dst_texture->level_count)
3994 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3995 dst_texture->layer_count, dst_texture->level_count,
3996 src_texture->layer_count, src_texture->level_count);
3997 return;
4000 for (i = 0; i < dst_texture->level_count; ++i)
4002 wined3d_box_set(&box, 0, 0,
4003 wined3d_texture_get_level_width(dst_texture, i),
4004 wined3d_texture_get_level_height(dst_texture, i),
4005 0, wined3d_texture_get_level_depth(dst_texture, i));
4006 for (j = 0; j < dst_texture->layer_count; ++j)
4008 unsigned int idx = j * dst_texture->level_count + i;
4010 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
4011 src_resource, idx, &box, 0, NULL, WINED3D_TEXF_POINT);
4016 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
4017 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4018 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4019 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4021 struct wined3d_box dst_box, b;
4023 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4024 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
4025 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4026 src_resource, src_sub_resource_idx, debug_box(src_box));
4028 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4030 WARN("Source and destination are the same sub-resource.\n");
4031 return WINED3DERR_INVALIDCALL;
4034 if (src_resource->type != dst_resource->type)
4036 WARN("Resource types (%s / %s) don't match.\n",
4037 debug_d3dresourcetype(dst_resource->type),
4038 debug_d3dresourcetype(src_resource->type));
4039 return WINED3DERR_INVALIDCALL;
4042 if (src_resource->format->id != dst_resource->format->id)
4044 WARN("Resource formats (%s / %s) don't match.\n",
4045 debug_d3dformat(dst_resource->format->id),
4046 debug_d3dformat(src_resource->format->id));
4047 return WINED3DERR_INVALIDCALL;
4050 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4052 if (dst_sub_resource_idx)
4054 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4055 return WINED3DERR_INVALIDCALL;
4058 if (src_sub_resource_idx)
4060 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4061 return WINED3DERR_INVALIDCALL;
4064 if (!src_box)
4066 wined3d_box_set(&b, 0, 0, src_resource->size, 1, 0, 1);
4067 src_box = &b;
4069 else if ((src_box->left >= src_box->right
4070 || src_box->top >= src_box->bottom
4071 || src_box->front >= src_box->back))
4073 WARN("Invalid box %s specified.\n", debug_box(src_box));
4074 return WINED3DERR_INVALIDCALL;
4077 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4078 || src_box->right - src_box->left > dst_resource->size - dst_x)
4080 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4081 dst_x, src_box->left, src_box->right - src_box->left);
4082 return WINED3DERR_INVALIDCALL;
4085 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4087 else if (dst_resource->type == WINED3D_RTYPE_TEXTURE_2D)
4089 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4090 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4091 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4093 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4095 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4096 return WINED3DERR_INVALIDCALL;
4099 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4101 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4102 return WINED3DERR_INVALIDCALL;
4105 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4107 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4108 return WINED3DERR_INVALIDCALL;
4111 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4113 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4114 return WINED3DERR_INVALIDCALL;
4117 if (!src_box)
4119 wined3d_box_set(&b, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4120 wined3d_texture_get_level_height(src_texture, src_level), 0, 1);
4121 src_box = &b;
4123 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4125 WARN("Invalid source box %s.\n", debug_box(src_box));
4126 return WINED3DERR_INVALIDCALL;
4129 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4130 dst_y + (src_box->bottom - src_box->top), 0, 1);
4131 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4132 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4134 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4135 return WINED3DERR_INVALIDCALL;
4138 else
4140 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4141 return WINED3DERR_INVALIDCALL;
4144 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4145 src_resource, src_sub_resource_idx, src_box, 0, NULL, WINED3D_TEXF_POINT);
4147 return WINED3D_OK;
4150 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4151 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4152 unsigned int depth_pitch)
4154 unsigned int width, height, depth;
4155 struct wined3d_box b;
4157 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4158 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4160 if (resource->type == WINED3D_RTYPE_BUFFER)
4162 if (sub_resource_idx > 0)
4164 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4165 return;
4168 width = resource->size;
4169 height = 1;
4170 depth = 1;
4172 else if (resource->type == WINED3D_RTYPE_TEXTURE_2D || resource->type == WINED3D_RTYPE_TEXTURE_3D)
4174 struct wined3d_texture *texture = texture_from_resource(resource);
4175 unsigned int level;
4177 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4179 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4180 return;
4183 level = sub_resource_idx % texture->level_count;
4184 width = wined3d_texture_get_level_width(texture, level);
4185 height = wined3d_texture_get_level_height(texture, level);
4186 depth = wined3d_texture_get_level_depth(texture, level);
4188 else
4190 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4191 return;
4194 if (!box)
4196 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4197 box = &b;
4199 else if (box->left >= box->right || box->right > width
4200 || box->top >= box->bottom || box->bottom > height
4201 || box->front >= box->back || box->back > depth)
4203 WARN("Invalid box %s specified.\n", debug_box(box));
4204 return;
4207 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4210 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4211 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4212 const struct wined3d_color *color, float depth, DWORD stencil)
4214 struct wined3d_resource *resource;
4215 RECT r;
4217 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4218 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4220 if (!flags)
4221 return WINED3D_OK;
4223 resource = view->resource;
4224 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4226 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4227 return WINED3DERR_INVALIDCALL;
4230 if (view->layer_count > 1)
4232 FIXME("Layered clears not implemented.\n");
4233 return WINED3DERR_INVALIDCALL;
4236 if (!rect)
4238 SetRect(&r, 0, 0, view->width, view->height);
4239 rect = &r;
4241 else
4243 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4244 struct wined3d_texture *texture = texture_from_resource(view->resource);
4245 HRESULT hr;
4247 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4248 view->sub_resource_idx % texture->level_count, &b)))
4249 return hr;
4252 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4254 return WINED3D_OK;
4257 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4258 unsigned int view_idx)
4260 TRACE("device %p, view_idx %u.\n", device, view_idx);
4262 if (view_idx >= device->adapter->gl_info.limits.buffers)
4264 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4265 return NULL;
4268 return device->fb.render_targets[view_idx];
4271 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4273 TRACE("device %p.\n", device);
4275 return device->fb.depth_stencil;
4278 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4279 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4281 struct wined3d_rendertarget_view *prev;
4283 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4284 device, view_idx, view, set_viewport);
4286 if (view_idx >= device->adapter->gl_info.limits.buffers)
4288 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4289 return WINED3DERR_INVALIDCALL;
4292 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4294 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4295 return WINED3DERR_INVALIDCALL;
4298 /* Set the viewport and scissor rectangles, if requested. Tests show that
4299 * stateblock recording is ignored, the change goes directly into the
4300 * primary stateblock. */
4301 if (!view_idx && set_viewport)
4303 struct wined3d_state *state = &device->state;
4305 state->viewport.x = 0;
4306 state->viewport.y = 0;
4307 state->viewport.width = view->width;
4308 state->viewport.height = view->height;
4309 state->viewport.min_z = 0.0f;
4310 state->viewport.max_z = 1.0f;
4311 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4313 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4314 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4318 prev = device->fb.render_targets[view_idx];
4319 if (view == prev)
4320 return WINED3D_OK;
4322 if (view)
4323 wined3d_rendertarget_view_incref(view);
4324 device->fb.render_targets[view_idx] = view;
4325 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4326 /* Release after the assignment, to prevent device_resource_released()
4327 * from seeing the surface as still in use. */
4328 if (prev)
4329 wined3d_rendertarget_view_decref(prev);
4331 return WINED3D_OK;
4334 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4336 struct wined3d_rendertarget_view *prev;
4338 TRACE("device %p, view %p.\n", device, view);
4340 prev = device->fb.depth_stencil;
4341 if (prev == view)
4343 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4344 return;
4347 if ((device->fb.depth_stencil = view))
4348 wined3d_rendertarget_view_incref(view);
4349 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4350 if (prev)
4351 wined3d_rendertarget_view_decref(prev);
4354 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4355 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4357 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4358 struct wined3d_sub_resource_data data;
4359 struct wined3d_resource_desc desc;
4360 struct wined3d_map_desc map_desc;
4361 struct wined3d_texture *texture;
4362 HRESULT hr;
4364 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4366 ERR("Failed to map source texture.\n");
4367 return NULL;
4370 data.data = map_desc.data;
4371 data.row_pitch = map_desc.row_pitch;
4372 data.slice_pitch = map_desc.slice_pitch;
4374 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4375 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4376 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4377 desc.multisample_quality = 0;
4378 desc.usage = WINED3DUSAGE_DYNAMIC;
4379 desc.pool = WINED3D_POOL_DEFAULT;
4380 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4381 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4382 desc.depth = 1;
4383 desc.size = 0;
4385 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4386 &data, NULL, &wined3d_null_parent_ops, &texture);
4387 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4388 if (FAILED(hr))
4390 ERR("Failed to create cursor texture.\n");
4391 return NULL;
4394 return texture;
4397 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4398 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4400 unsigned int texture_level = sub_resource_idx % texture->level_count;
4401 unsigned int cursor_width, cursor_height;
4402 struct wined3d_display_mode mode;
4403 struct wined3d_map_desc map_desc;
4404 HRESULT hr;
4406 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4407 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4409 if (sub_resource_idx >= texture->level_count * texture->layer_count
4410 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4411 return WINED3DERR_INVALIDCALL;
4413 if (device->cursor_texture)
4415 wined3d_texture_decref(device->cursor_texture);
4416 device->cursor_texture = NULL;
4419 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4421 WARN("Texture %p has invalid format %s.\n",
4422 texture, debug_d3dformat(texture->resource.format->id));
4423 return WINED3DERR_INVALIDCALL;
4426 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4428 ERR("Failed to get display mode, hr %#x.\n", hr);
4429 return WINED3DERR_INVALIDCALL;
4432 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4433 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4434 if (cursor_width > mode.width || cursor_height > mode.height)
4436 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4437 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4438 return WINED3DERR_INVALIDCALL;
4441 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4443 /* Do not store the surface's pointer because the application may
4444 * release it after setting the cursor image. Windows doesn't
4445 * addref the set surface, so we can't do this either without
4446 * creating circular refcount dependencies. */
4447 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4449 ERR("Failed to create cursor texture.\n");
4450 return WINED3DERR_INVALIDCALL;
4453 if (cursor_width == 32 && cursor_height == 32)
4455 UINT mask_size = cursor_width * cursor_height / 8;
4456 ICONINFO cursor_info;
4457 DWORD *mask_bits;
4458 HCURSOR cursor;
4460 /* 32-bit user32 cursors ignore the alpha channel if it's all
4461 * zeroes, and use the mask instead. Fill the mask with all ones
4462 * to ensure we still get a fully transparent cursor. */
4463 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4464 return E_OUTOFMEMORY;
4465 memset(mask_bits, 0xff, mask_size);
4467 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4468 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4469 cursor_info.fIcon = FALSE;
4470 cursor_info.xHotspot = x_hotspot;
4471 cursor_info.yHotspot = y_hotspot;
4472 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4473 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4474 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4476 /* Create our cursor and clean up. */
4477 cursor = CreateIconIndirect(&cursor_info);
4478 if (cursor_info.hbmMask)
4479 DeleteObject(cursor_info.hbmMask);
4480 if (cursor_info.hbmColor)
4481 DeleteObject(cursor_info.hbmColor);
4482 if (device->hardwareCursor)
4483 DestroyCursor(device->hardwareCursor);
4484 device->hardwareCursor = cursor;
4485 if (device->bCursorVisible)
4486 SetCursor(cursor);
4488 HeapFree(GetProcessHeap(), 0, mask_bits);
4491 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4492 device->cursorWidth = cursor_width;
4493 device->cursorHeight = cursor_height;
4494 device->xHotSpot = x_hotspot;
4495 device->yHotSpot = y_hotspot;
4497 return WINED3D_OK;
4500 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4501 int x_screen_space, int y_screen_space, DWORD flags)
4503 TRACE("device %p, x %d, y %d, flags %#x.\n",
4504 device, x_screen_space, y_screen_space, flags);
4506 device->xScreenSpace = x_screen_space;
4507 device->yScreenSpace = y_screen_space;
4509 if (device->hardwareCursor)
4511 POINT pt;
4513 GetCursorPos( &pt );
4514 if (x_screen_space == pt.x && y_screen_space == pt.y)
4515 return;
4516 SetCursorPos( x_screen_space, y_screen_space );
4518 /* Switch to the software cursor if position diverges from the hardware one. */
4519 GetCursorPos( &pt );
4520 if (x_screen_space != pt.x || y_screen_space != pt.y)
4522 if (device->bCursorVisible) SetCursor( NULL );
4523 DestroyCursor( device->hardwareCursor );
4524 device->hardwareCursor = 0;
4529 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4531 BOOL oldVisible = device->bCursorVisible;
4533 TRACE("device %p, show %#x.\n", device, show);
4536 * When ShowCursor is first called it should make the cursor appear at the OS's last
4537 * known cursor position.
4539 if (show && !oldVisible)
4541 POINT pt;
4542 GetCursorPos(&pt);
4543 device->xScreenSpace = pt.x;
4544 device->yScreenSpace = pt.y;
4547 if (device->hardwareCursor)
4549 device->bCursorVisible = show;
4550 if (show)
4551 SetCursor(device->hardwareCursor);
4552 else
4553 SetCursor(NULL);
4555 else if (device->cursor_texture)
4557 device->bCursorVisible = show;
4560 return oldVisible;
4563 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4565 struct wined3d_resource *resource, *cursor;
4567 TRACE("device %p.\n", device);
4569 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4571 TRACE("Checking resource %p for eviction.\n", resource);
4573 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4575 TRACE("Evicting %p.\n", resource);
4576 wined3d_cs_emit_unload_resource(device->cs, resource);
4581 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4582 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4583 wined3d_device_reset_cb callback, BOOL reset_state)
4585 struct wined3d_resource *resource, *cursor;
4586 struct wined3d_swapchain *swapchain;
4587 struct wined3d_view_desc view_desc;
4588 BOOL backbuffer_resized;
4589 HRESULT hr = WINED3D_OK;
4590 unsigned int i;
4592 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4593 device, swapchain_desc, mode, callback, reset_state);
4595 device->cs->ops->finish(device->cs);
4597 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4599 ERR("Failed to get the first implicit swapchain.\n");
4600 return WINED3DERR_INVALIDCALL;
4603 if (reset_state)
4605 if (device->logo_texture)
4607 wined3d_texture_decref(device->logo_texture);
4608 device->logo_texture = NULL;
4610 if (device->cursor_texture)
4612 wined3d_texture_decref(device->cursor_texture);
4613 device->cursor_texture = NULL;
4615 state_unbind_resources(&device->state);
4618 if (device->fb.render_targets)
4620 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4622 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4625 wined3d_device_set_depth_stencil_view(device, NULL);
4627 if (reset_state)
4629 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4631 TRACE("Enumerating resource %p.\n", resource);
4632 if (FAILED(hr = callback(resource)))
4633 return hr;
4637 TRACE("New params:\n");
4638 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4639 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4640 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4641 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4642 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4643 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4644 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4645 TRACE("device_window %p\n", swapchain_desc->device_window);
4646 TRACE("windowed %#x\n", swapchain_desc->windowed);
4647 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4648 if (swapchain_desc->enable_auto_depth_stencil)
4649 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4650 TRACE("flags %#x\n", swapchain_desc->flags);
4651 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4652 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4653 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4655 /* No special treatment of these parameters. Just store them */
4656 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4657 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4658 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4659 swapchain->desc.flags = swapchain_desc->flags;
4660 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4661 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4662 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4664 if (swapchain_desc->device_window
4665 && swapchain_desc->device_window != swapchain->desc.device_window)
4667 TRACE("Changing the device window from %p to %p.\n",
4668 swapchain->desc.device_window, swapchain_desc->device_window);
4669 swapchain->desc.device_window = swapchain_desc->device_window;
4670 swapchain->device_window = swapchain_desc->device_window;
4671 wined3d_swapchain_set_window(swapchain, NULL);
4674 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4675 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4677 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4678 || swapchain->reapply_mode || mode
4679 || (!swapchain_desc->windowed && backbuffer_resized))
4681 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4682 return hr;
4684 else if (!swapchain_desc->windowed)
4686 DWORD style = device->style;
4687 DWORD exStyle = device->exStyle;
4688 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4689 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4690 * Reset to clear up their mess. Guild Wars also loses the device during that.
4692 device->style = 0;
4693 device->exStyle = 0;
4694 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4695 swapchain_desc->backbuffer_width,
4696 swapchain_desc->backbuffer_height);
4697 device->style = style;
4698 device->exStyle = exStyle;
4701 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4702 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4703 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4704 return hr;
4706 if (device->auto_depth_stencil_view)
4708 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4709 device->auto_depth_stencil_view = NULL;
4711 if (swapchain->desc.enable_auto_depth_stencil)
4713 struct wined3d_resource_desc texture_desc;
4714 struct wined3d_texture *texture;
4715 DWORD flags = 0;
4717 TRACE("Creating the depth stencil buffer.\n");
4719 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4720 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4721 texture_desc.multisample_type = swapchain->desc.multisample_type;
4722 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4723 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4724 texture_desc.pool = WINED3D_POOL_DEFAULT;
4725 texture_desc.width = swapchain->desc.backbuffer_width;
4726 texture_desc.height = swapchain->desc.backbuffer_height;
4727 texture_desc.depth = 1;
4728 texture_desc.size = 0;
4730 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4731 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4733 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4734 device->device_parent, &texture_desc, flags, &texture)))
4736 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4737 return WINED3DERR_INVALIDCALL;
4740 view_desc.format_id = texture->resource.format->id;
4741 view_desc.flags = 0;
4742 view_desc.u.texture.level_idx = 0;
4743 view_desc.u.texture.level_count = 1;
4744 view_desc.u.texture.layer_idx = 0;
4745 view_desc.u.texture.layer_count = 1;
4746 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4747 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4748 wined3d_texture_decref(texture);
4749 if (FAILED(hr))
4751 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4752 return hr;
4755 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4758 if (device->back_buffer_view)
4760 wined3d_rendertarget_view_decref(device->back_buffer_view);
4761 device->back_buffer_view = NULL;
4763 if (swapchain->desc.backbuffer_count)
4765 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4767 view_desc.format_id = back_buffer->format->id;
4768 view_desc.flags = 0;
4769 view_desc.u.texture.level_idx = 0;
4770 view_desc.u.texture.level_count = 1;
4771 view_desc.u.texture.layer_idx = 0;
4772 view_desc.u.texture.layer_count = 1;
4773 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4774 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4776 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4777 return hr;
4781 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4783 if (reset_state)
4785 TRACE("Resetting stateblock.\n");
4786 if (device->recording)
4788 wined3d_stateblock_decref(device->recording);
4789 device->recording = NULL;
4791 wined3d_cs_emit_reset_state(device->cs);
4792 state_cleanup(&device->state);
4794 if (device->d3d_initialized)
4795 wined3d_device_delete_opengl_contexts(device);
4797 memset(&device->state, 0, sizeof(device->state));
4798 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4799 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4800 device->update_state = &device->state;
4802 device_init_swapchain_state(device, swapchain);
4803 if (wined3d_settings.logo)
4804 device_load_logo(device, wined3d_settings.logo);
4806 else if (device->back_buffer_view)
4808 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4809 struct wined3d_state *state = &device->state;
4811 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4813 /* Note the min_z / max_z is not reset. */
4814 state->viewport.x = 0;
4815 state->viewport.y = 0;
4816 state->viewport.width = view->width;
4817 state->viewport.height = view->height;
4818 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4820 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4821 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4824 if (device->d3d_initialized)
4826 if (reset_state)
4827 hr = wined3d_device_create_primary_opengl_context(device);
4828 swapchain_update_swap_interval(swapchain);
4831 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4832 * first use
4834 return hr;
4837 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4839 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4841 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4843 return WINED3D_OK;
4847 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4848 struct wined3d_device_creation_parameters *parameters)
4850 TRACE("device %p, parameters %p.\n", device, parameters);
4852 *parameters = device->create_parms;
4855 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4857 TRACE("device %p.\n", device);
4859 return device->wined3d;
4862 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4863 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4865 struct wined3d_swapchain *swapchain;
4867 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4868 device, swapchain_idx, flags, ramp);
4870 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4871 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4874 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4875 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4877 struct wined3d_swapchain *swapchain;
4879 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4880 device, swapchain_idx, ramp);
4882 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4883 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4886 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4888 TRACE("device %p, resource %p.\n", device, resource);
4890 wined3d_not_from_cs(device->cs);
4892 list_add_head(&device->resources, &resource->resource_list_entry);
4895 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4897 TRACE("device %p, resource %p.\n", device, resource);
4899 wined3d_not_from_cs(device->cs);
4901 list_remove(&resource->resource_list_entry);
4904 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4906 enum wined3d_resource_type type = resource->type;
4907 struct wined3d_rendertarget_view *rtv;
4908 unsigned int i;
4910 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4912 if (device->d3d_initialized)
4914 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4916 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4917 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4920 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4921 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4924 switch (type)
4926 case WINED3D_RTYPE_TEXTURE_2D:
4927 case WINED3D_RTYPE_TEXTURE_3D:
4928 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4930 struct wined3d_texture *texture = texture_from_resource(resource);
4932 if (device->state.textures[i] == texture)
4934 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4935 device->state.textures[i] = NULL;
4938 if (device->recording && device->update_state->textures[i] == texture)
4940 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4941 texture, device->recording, i);
4942 device->update_state->textures[i] = NULL;
4945 break;
4947 case WINED3D_RTYPE_BUFFER:
4949 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4951 for (i = 0; i < MAX_STREAMS; ++i)
4953 if (device->state.streams[i].buffer == buffer)
4955 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4956 device->state.streams[i].buffer = NULL;
4959 if (device->recording && device->update_state->streams[i].buffer == buffer)
4961 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4962 buffer, device->recording, i);
4963 device->update_state->streams[i].buffer = NULL;
4967 if (device->state.index_buffer == buffer)
4969 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4970 device->state.index_buffer = NULL;
4973 if (device->recording && device->update_state->index_buffer == buffer)
4975 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4976 buffer, device->recording);
4977 device->update_state->index_buffer = NULL;
4980 break;
4982 default:
4983 break;
4986 /* Remove the resource from the resourceStore */
4987 device_resource_remove(device, resource);
4989 TRACE("Resource released.\n");
4992 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4994 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4996 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4999 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5000 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5001 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5003 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5004 const struct fragment_pipeline *fragment_pipeline;
5005 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5006 unsigned int i;
5007 HRESULT hr;
5009 device->ref = 1;
5010 device->wined3d = wined3d;
5011 wined3d_incref(device->wined3d);
5012 device->adapter = wined3d->adapter_count ? adapter : NULL;
5013 device->device_parent = device_parent;
5014 list_init(&device->resources);
5015 list_init(&device->shaders);
5016 device->surface_alignment = surface_alignment;
5018 /* Save the creation parameters. */
5019 device->create_parms.adapter_idx = adapter_idx;
5020 device->create_parms.device_type = device_type;
5021 device->create_parms.focus_window = focus_window;
5022 device->create_parms.flags = flags;
5024 device->shader_backend = adapter->shader_backend;
5026 vertex_pipeline = adapter->vertex_pipe;
5028 fragment_pipeline = adapter->fragment_pipe;
5030 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5032 if (vertex_pipeline->vp_states && fragment_pipeline->states
5033 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5034 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5035 fragment_pipeline, misc_state_template)))
5037 ERR("Failed to compile state table, hr %#x.\n", hr);
5038 wine_rb_destroy(&device->samplers, NULL, NULL);
5039 wined3d_decref(device->wined3d);
5040 return hr;
5043 state_init(&device->state, &device->fb, &adapter->gl_info,
5044 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5045 device->update_state = &device->state;
5047 if (!(device->cs = wined3d_cs_create(device)))
5049 WARN("Failed to create command stream.\n");
5050 state_cleanup(&device->state);
5051 hr = E_FAIL;
5052 goto err;
5055 return WINED3D_OK;
5057 err:
5058 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5060 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5062 wine_rb_destroy(&device->samplers, NULL, NULL);
5063 wined3d_decref(device->wined3d);
5064 return hr;
5067 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5069 DWORD rep = device->StateTable[state].representative;
5070 struct wined3d_context *context;
5071 DWORD idx;
5072 BYTE shift;
5073 UINT i;
5075 wined3d_from_cs(device->cs);
5077 if (STATE_IS_COMPUTE(state))
5079 for (i = 0; i < device->context_count; ++i)
5080 context_invalidate_compute_state(device->contexts[i], state);
5081 return;
5084 for (i = 0; i < device->context_count; ++i)
5086 context = device->contexts[i];
5087 if(isStateDirty(context, rep)) continue;
5089 context->dirtyArray[context->numDirtyEntries++] = rep;
5090 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5091 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5092 context->isStateDirty[idx] |= (1u << shift);
5096 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5097 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5099 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5101 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5102 window, message, wparam, lparam);
5103 if (unicode)
5104 return DefWindowProcW(window, message, wparam, lparam);
5105 else
5106 return DefWindowProcA(window, message, wparam, lparam);
5109 if (message == WM_DESTROY)
5111 TRACE("unregister window %p.\n", window);
5112 wined3d_unregister_window(window);
5114 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5115 ERR("Window %p is not the focus window for device %p.\n", window, device);
5117 else if (message == WM_DISPLAYCHANGE)
5119 device->device_parent->ops->mode_changed(device->device_parent);
5121 else if (message == WM_ACTIVATEAPP)
5123 UINT i;
5125 for (i = 0; i < device->swapchain_count; i++)
5126 wined3d_swapchain_activate(device->swapchains[i], wparam);
5128 device->device_parent->ops->activate(device->device_parent, wparam);
5130 else if (message == WM_SYSCOMMAND)
5132 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5134 if (unicode)
5135 DefWindowProcW(window, message, wparam, lparam);
5136 else
5137 DefWindowProcA(window, message, wparam, lparam);
5141 if (unicode)
5142 return CallWindowProcW(proc, window, message, wparam, lparam);
5143 else
5144 return CallWindowProcA(proc, window, message, wparam, lparam);