wined3d: Pass the primitive type to wined3d_cs_emit_draw().
[wine.git] / dlls / wined3d / device.c
blob2b95f5071db800bc21544351180d9412c518aa4b
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
203 unsigned int height = wined3d_texture_get_level_height(target->container, target->texture_level);
204 unsigned int width = wined3d_texture_get_level_width(target->container, target->texture_level);
206 /* partial draw rect */
207 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
208 return FALSE;
210 /* partial clear rect */
211 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
212 || clear_rect->right < width || clear_rect->bottom < height))
213 return FALSE;
215 return TRUE;
218 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
219 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
220 float depth, DWORD stencil)
222 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
223 struct wined3d_surface *target = rtv ? wined3d_rendertarget_view_get_surface(rtv) : NULL;
224 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
225 struct wined3d_surface *depth_stencil = dsv ? wined3d_rendertarget_view_get_surface(dsv) : NULL;
226 const struct wined3d_state *state = &device->state;
227 const struct wined3d_gl_info *gl_info;
228 UINT drawable_width, drawable_height;
229 struct wined3d_color corrected_color;
230 struct wined3d_context *context;
231 GLbitfield clear_mask = 0;
232 BOOL render_offscreen;
233 unsigned int i;
235 if (target)
236 context = context_acquire(device, target->container, rtv->sub_resource_idx);
237 else
238 context = context_acquire(device, NULL, 0);
239 if (!context->valid)
241 context_release(context);
242 WARN("Invalid context, skipping clear.\n");
243 return;
245 gl_info = context->gl_info;
247 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
248 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
249 * for the cleared parts, and the untouched parts.
251 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
252 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
253 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
254 * checking all this if the dest surface is in the drawable anyway. */
255 for (i = 0; i < rt_count; ++i)
257 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
259 if (rtv && rtv->format->id != WINED3DFMT_NULL)
261 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
263 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
264 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
265 else
266 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
270 if (target)
272 render_offscreen = context->render_offscreen;
273 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
275 else
277 render_offscreen = TRUE;
278 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
279 depth_stencil->texture_level);
280 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
281 depth_stencil->texture_level);
284 if (depth_stencil && render_offscreen)
285 wined3d_texture_prepare_location(depth_stencil->container,
286 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
288 if (flags & WINED3DCLEAR_ZBUFFER)
290 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
292 wined3d_texture_load_location(depth_stencil->container,
293 dsv->sub_resource_idx, context, location);
296 if (!context_apply_clear_state(context, state, rt_count, fb))
298 context_release(context);
299 WARN("Failed to apply clear state, skipping clear.\n");
300 return;
303 /* Only set the values up once, as they are not changing. */
304 if (flags & WINED3DCLEAR_STENCIL)
306 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
308 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
309 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
311 gl_info->gl_ops.gl.p_glStencilMask(~0U);
312 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
313 gl_info->gl_ops.gl.p_glClearStencil(stencil);
314 checkGLcall("glClearStencil");
315 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
318 if (flags & WINED3DCLEAR_ZBUFFER)
320 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
322 wined3d_texture_validate_location(depth_stencil->container, dsv->sub_resource_idx, location);
323 wined3d_texture_invalidate_location(depth_stencil->container, dsv->sub_resource_idx, ~location);
325 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
326 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
327 gl_info->gl_ops.gl.p_glClearDepth(depth);
328 checkGLcall("glClearDepth");
329 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
332 if (flags & WINED3DCLEAR_TARGET)
334 for (i = 0; i < rt_count; ++i)
336 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
337 struct wined3d_texture *texture;
339 if (!rtv)
340 continue;
342 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
344 FIXME("Not supported on buffer resources.\n");
345 continue;
348 texture = texture_from_resource(rtv->resource);
349 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
350 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
353 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
355 if (rt_count > 1)
356 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
357 "support, this might cause graphical issues.\n");
359 corrected_color.r = color->r < wined3d_srgb_const1[0]
360 ? color->r * wined3d_srgb_const0[3]
361 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
362 - wined3d_srgb_const0[2];
363 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
364 corrected_color.g = color->g < wined3d_srgb_const1[0]
365 ? color->g * wined3d_srgb_const0[3]
366 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
367 - wined3d_srgb_const0[2];
368 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
369 corrected_color.b = color->b < wined3d_srgb_const1[0]
370 ? color->b * wined3d_srgb_const0[3]
371 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
372 - wined3d_srgb_const0[2];
373 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
374 color = &corrected_color;
377 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
378 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
379 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
380 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
382 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
383 checkGLcall("glClearColor");
384 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
387 if (!rect_count)
389 if (render_offscreen)
391 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
392 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
394 else
396 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
397 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
399 checkGLcall("glScissor");
400 gl_info->gl_ops.gl.p_glClear(clear_mask);
401 checkGLcall("glClear");
403 else
405 RECT current_rect;
407 /* Now process each rect in turn. */
408 for (i = 0; i < rect_count; ++i)
410 /* Note that GL uses lower left, width/height. */
411 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
413 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
414 wine_dbgstr_rect(&clear_rect[i]),
415 wine_dbgstr_rect(&current_rect));
417 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
418 * The rectangle is not cleared, no error is returned, but further rectangles are
419 * still cleared if they are valid. */
420 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
422 TRACE("Rectangle with negative dimensions, ignoring.\n");
423 continue;
426 if (render_offscreen)
428 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
429 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
431 else
433 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
434 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
436 checkGLcall("glScissor");
438 gl_info->gl_ops.gl.p_glClear(clear_mask);
439 checkGLcall("glClear");
443 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
444 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
445 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
447 context_release(context);
450 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
452 ULONG refcount = InterlockedIncrement(&device->ref);
454 TRACE("%p increasing refcount to %u.\n", device, refcount);
456 return refcount;
459 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
461 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
463 ERR("Leftover sampler %p.\n", sampler);
466 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
468 ULONG refcount = InterlockedDecrement(&device->ref);
470 TRACE("%p decreasing refcount to %u.\n", device, refcount);
472 if (!refcount)
474 UINT i;
476 wined3d_cs_destroy(device->cs);
478 if (device->recording && wined3d_stateblock_decref(device->recording))
479 ERR("Something's still holding the recording stateblock.\n");
480 device->recording = NULL;
482 state_cleanup(&device->state);
484 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
486 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
487 device->multistate_funcs[i] = NULL;
490 if (!list_empty(&device->resources))
492 struct wined3d_resource *resource;
494 ERR("Device released with resources still bound.\n");
496 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
498 ERR("Leftover resource %p with type %s (%#x).\n",
499 resource, debug_d3dresourcetype(resource->type), resource->type);
503 if (device->contexts)
504 ERR("Context array not freed!\n");
505 if (device->hardwareCursor)
506 DestroyCursor(device->hardwareCursor);
507 device->hardwareCursor = 0;
509 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
511 wined3d_decref(device->wined3d);
512 device->wined3d = NULL;
513 HeapFree(GetProcessHeap(), 0, device);
514 TRACE("Freed device %p.\n", device);
517 return refcount;
520 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
522 TRACE("device %p.\n", device);
524 return device->swapchain_count;
527 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
529 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
531 if (swapchain_idx >= device->swapchain_count)
533 WARN("swapchain_idx %u >= swapchain_count %u.\n",
534 swapchain_idx, device->swapchain_count);
535 return NULL;
538 return device->swapchains[swapchain_idx];
541 static void device_load_logo(struct wined3d_device *device, const char *filename)
543 struct wined3d_color_key color_key;
544 struct wined3d_resource_desc desc;
545 HBITMAP hbm;
546 BITMAP bm;
547 HRESULT hr;
548 HDC dcb = NULL, dcs = NULL;
550 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
551 if(hbm)
553 GetObjectA(hbm, sizeof(BITMAP), &bm);
554 dcb = CreateCompatibleDC(NULL);
555 if(!dcb) goto out;
556 SelectObject(dcb, hbm);
558 else
560 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
561 * couldn't be loaded
563 memset(&bm, 0, sizeof(bm));
564 bm.bmWidth = 32;
565 bm.bmHeight = 32;
568 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
569 desc.format = WINED3DFMT_B5G6R5_UNORM;
570 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
571 desc.multisample_quality = 0;
572 desc.usage = WINED3DUSAGE_DYNAMIC;
573 desc.pool = WINED3D_POOL_DEFAULT;
574 desc.width = bm.bmWidth;
575 desc.height = bm.bmHeight;
576 desc.depth = 1;
577 desc.size = 0;
578 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
579 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
581 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
582 goto out;
585 if (dcb)
587 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
588 goto out;
589 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
590 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
592 color_key.color_space_low_value = 0;
593 color_key.color_space_high_value = 0;
594 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
596 else
598 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
599 const RECT rect = {0, 0, desc.width, desc.height};
600 struct wined3d_surface *surface;
602 /* Fill the surface with a white color to show that wined3d is there */
603 surface = device->logo_texture->sub_resources[0].u.surface;
604 surface_color_fill(surface, &rect, &c);
607 out:
608 if (dcb) DeleteDC(dcb);
609 if (hbm) DeleteObject(hbm);
612 /* Context activation is done by the caller. */
613 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
615 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
616 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
617 unsigned int i;
618 DWORD color;
620 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
621 color = 0x000000ff;
622 else
623 color = 0x00000000;
625 /* Under DirectX you can sample even if no texture is bound, whereas
626 * OpenGL will only allow that when a valid texture is bound.
627 * We emulate this by creating dummy textures and binding them
628 * to each texture stage when the currently set D3D texture is NULL. */
629 context_active_texture(context, gl_info, 0);
631 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d);
632 checkGLcall("glGenTextures");
633 TRACE("Dummy 2D texture given name %u.\n", device->dummy_textures.tex_2d);
635 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
636 checkGLcall("glBindTexture");
638 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
639 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
640 checkGLcall("glTexImage2D");
642 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
644 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_rect);
645 checkGLcall("glGenTextures");
646 TRACE("Dummy rectangle texture given name %u.\n", device->dummy_textures.tex_rect);
648 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
649 checkGLcall("glBindTexture");
651 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
652 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
653 checkGLcall("glTexImage2D");
656 if (gl_info->supported[EXT_TEXTURE3D])
658 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_3d);
659 checkGLcall("glGenTextures");
660 TRACE("Dummy 3D texture given name %u.\n", device->dummy_textures.tex_3d);
662 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
663 checkGLcall("glBindTexture");
665 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
666 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
667 checkGLcall("glTexImage3D");
670 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
672 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube);
673 checkGLcall("glGenTextures");
674 TRACE("Dummy cube texture given name %u.\n", device->dummy_textures.tex_cube);
676 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
677 checkGLcall("glBindTexture");
679 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
681 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
682 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
683 checkGLcall("glTexImage2D");
687 if (gl_info->supported[EXT_TEXTURE_ARRAY])
689 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d_array);
690 checkGLcall("glGenTextures");
691 TRACE("Dummy 2D array texture given name %u.\n", device->dummy_textures.tex_2d_array);
693 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
694 checkGLcall("glBindTexture");
696 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
697 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
698 checkGLcall("glTexImage3D");
701 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
703 GLuint buffer;
705 GL_EXTCALL(glGenBuffers(1, &buffer));
706 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
707 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
708 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
709 checkGLcall("Create buffer object");
711 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_buffer);
712 checkGLcall("glGenTextures");
713 TRACE("Dummy buffer texture given name %u.\n", device->dummy_textures.tex_buffer);
715 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, device->dummy_textures.tex_buffer);
716 checkGLcall("glBindTexture");
717 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
718 checkGLcall("glTexBuffer");
720 GL_EXTCALL(glDeleteBuffers(1, &buffer));
721 checkGLcall("glDeleteBuffers");
724 context_bind_dummy_textures(device, context);
727 /* Context activation is done by the caller. */
728 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
730 const struct wined3d_gl_info *gl_info = context->gl_info;
732 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
733 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_buffer);
735 if (gl_info->supported[EXT_TEXTURE_ARRAY])
736 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d_array);
738 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
739 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube);
741 if (gl_info->supported[EXT_TEXTURE3D])
742 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_3d);
744 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
745 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_rect);
747 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d);
749 checkGLcall("Delete dummy textures");
751 memset(&device->dummy_textures, 0, sizeof(device->dummy_textures));
754 /* Context activation is done by the caller. */
755 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
757 const struct wined3d_gl_info *gl_info = context->gl_info;
759 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
761 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
762 * instructions allow access to resources without using samplers.
763 * In GLSL, resources are always accessed through sampler or image variables. The default
764 * sampler object is used to emulate the direct resource access when there is no sampler state
765 * to use.
767 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
768 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
769 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
770 checkGLcall("Create default sampler");
772 /* In D3D10+, a NULL sampler maps to the default sampler state. */
773 GL_EXTCALL(glGenSamplers(1, &device->null_sampler));
774 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
775 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
776 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
777 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
778 checkGLcall("Create null sampler");
780 else
782 device->default_sampler = 0;
783 device->null_sampler = 0;
787 /* Context activation is done by the caller. */
788 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
790 const struct wined3d_gl_info *gl_info = context->gl_info;
792 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
794 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
795 GL_EXTCALL(glDeleteSamplers(1, &device->null_sampler));
796 checkGLcall("glDeleteSamplers");
799 device->default_sampler = 0;
800 device->null_sampler = 0;
803 static LONG fullscreen_style(LONG style)
805 /* Make sure the window is managed, otherwise we won't get keyboard input. */
806 style |= WS_POPUP | WS_SYSMENU;
807 style &= ~(WS_CAPTION | WS_THICKFRAME);
809 return style;
812 static LONG fullscreen_exstyle(LONG exstyle)
814 /* Filter out window decorations. */
815 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
817 return exstyle;
820 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
822 BOOL filter_messages;
823 LONG style, exstyle;
825 TRACE("Setting up window %p for fullscreen mode.\n", window);
827 if (device->style || device->exStyle)
829 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
830 window, device->style, device->exStyle);
833 device->style = GetWindowLongW(window, GWL_STYLE);
834 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
836 style = fullscreen_style(device->style);
837 exstyle = fullscreen_exstyle(device->exStyle);
839 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
840 device->style, device->exStyle, style, exstyle);
842 filter_messages = device->filter_messages;
843 device->filter_messages = TRUE;
845 SetWindowLongW(window, GWL_STYLE, style);
846 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
847 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
849 device->filter_messages = filter_messages;
852 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
853 const RECT *window_rect)
855 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
856 BOOL filter_messages;
857 LONG style, exstyle;
858 RECT rect = {0};
860 if (!device->style && !device->exStyle)
861 return;
863 style = GetWindowLongW(window, GWL_STYLE);
864 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
866 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
867 * application, and we want to ignore them in the test below, since it's
868 * not the application's fault that they changed. Additionally, we want to
869 * preserve the current status of these flags (i.e. don't restore them) to
870 * more closely emulate the behavior of Direct3D, which leaves these flags
871 * alone when returning to windowed mode. */
872 device->style ^= (device->style ^ style) & WS_VISIBLE;
873 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
875 TRACE("Restoring window style of window %p to %08x, %08x.\n",
876 window, device->style, device->exStyle);
878 filter_messages = device->filter_messages;
879 device->filter_messages = TRUE;
881 /* Only restore the style if the application didn't modify it during the
882 * fullscreen phase. Some applications change it before calling Reset()
883 * when switching between windowed and fullscreen modes (HL2), some
884 * depend on the original style (Eve Online). */
885 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
887 SetWindowLongW(window, GWL_STYLE, device->style);
888 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
891 if (window_rect)
892 rect = *window_rect;
893 else
894 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
895 SetWindowPos(window, 0, rect.left, rect.top,
896 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
898 device->filter_messages = filter_messages;
900 /* Delete the old values. */
901 device->style = 0;
902 device->exStyle = 0;
905 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
907 TRACE("device %p, window %p.\n", device, window);
909 if (!wined3d_register_window(window, device))
911 ERR("Failed to register window %p.\n", window);
912 return E_FAIL;
915 InterlockedExchangePointer((void **)&device->focus_window, window);
916 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
918 return WINED3D_OK;
921 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
923 TRACE("device %p.\n", device);
925 if (device->focus_window) wined3d_unregister_window(device->focus_window);
926 InterlockedExchangePointer((void **)&device->focus_window, NULL);
929 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
931 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
932 unsigned int i;
934 if (device->fb.render_targets)
936 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
938 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
940 if (device->back_buffer_view)
941 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
944 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
945 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
948 static void wined3d_device_delete_opengl_contexts_cs(void *object)
950 struct wined3d_resource *resource, *cursor;
951 struct wined3d_device *device = object;
952 struct wined3d_context *context;
953 struct wined3d_shader *shader;
955 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
957 TRACE("Unloading resource %p.\n", resource);
958 wined3d_cs_emit_unload_resource(device->cs, resource);
961 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
963 device->shader_backend->shader_destroy(shader);
966 context = context_acquire(device, NULL, 0);
967 device->blitter->free_private(device);
968 device->shader_backend->shader_free_private(device);
969 destroy_dummy_textures(device, context);
970 destroy_default_samplers(device, context);
971 context_release(context);
973 while (device->context_count)
975 if (device->contexts[0]->swapchain)
976 swapchain_destroy_contexts(device->contexts[0]->swapchain);
977 else
978 context_destroy(device, device->contexts[0]);
982 static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
984 wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
987 static void wined3d_device_create_primary_opengl_context_cs(void *object)
989 struct wined3d_device *device = object;
990 struct wined3d_swapchain *swapchain;
991 struct wined3d_context *context;
992 struct wined3d_texture *target;
993 HRESULT hr;
995 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
996 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
998 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
999 return;
1002 if (FAILED(hr = device->blitter->alloc_private(device)))
1004 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
1005 device->shader_backend->shader_free_private(device);
1006 return;
1009 swapchain = device->swapchains[0];
1010 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1011 context = context_acquire(device, target, 0);
1012 create_dummy_textures(device, context);
1013 create_default_samplers(device, context);
1014 context_release(context);
1017 static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1019 wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1020 if (!device->swapchains[0]->num_contexts)
1021 return E_FAIL;
1023 return WINED3D_OK;
1026 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1027 struct wined3d_swapchain_desc *swapchain_desc)
1029 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1030 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1031 struct wined3d_swapchain *swapchain = NULL;
1032 DWORD clear_flags = 0;
1033 HRESULT hr;
1035 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1037 if (device->d3d_initialized)
1038 return WINED3DERR_INVALIDCALL;
1039 if (device->wined3d->flags & WINED3D_NO3D)
1040 return WINED3DERR_INVALIDCALL;
1042 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
1043 return E_OUTOFMEMORY;
1045 /* Setup the implicit swapchain. This also initializes a context. */
1046 TRACE("Creating implicit swapchain\n");
1047 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1048 swapchain_desc, &swapchain);
1049 if (FAILED(hr))
1051 WARN("Failed to create implicit swapchain\n");
1052 goto err_out;
1055 if (swapchain_desc->backbuffer_count)
1057 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1058 struct wined3d_view_desc view_desc;
1060 view_desc.format_id = back_buffer->format->id;
1061 view_desc.flags = 0;
1062 view_desc.u.texture.level_idx = 0;
1063 view_desc.u.texture.level_count = 1;
1064 view_desc.u.texture.layer_idx = 0;
1065 view_desc.u.texture.layer_count = 1;
1066 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1067 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1069 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1070 goto err_out;
1074 device->swapchain_count = 1;
1075 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1077 ERR("Out of memory!\n");
1078 goto err_out;
1080 device->swapchains[0] = swapchain;
1082 if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1083 goto err_out;
1084 device_init_swapchain_state(device, swapchain);
1086 device->contexts[0]->last_was_rhw = 0;
1088 TRACE("All defaults now set up, leaving 3D init.\n");
1090 /* Clear the screen */
1091 if (swapchain->back_buffers && swapchain->back_buffers[0])
1092 clear_flags |= WINED3DCLEAR_TARGET;
1093 if (swapchain_desc->enable_auto_depth_stencil)
1094 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1095 if (clear_flags)
1096 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1098 device->d3d_initialized = TRUE;
1100 if (wined3d_settings.logo)
1101 device_load_logo(device, wined3d_settings.logo);
1102 return WINED3D_OK;
1104 err_out:
1105 HeapFree(GetProcessHeap(), 0, device->swapchains);
1106 device->swapchain_count = 0;
1107 if (device->back_buffer_view)
1108 wined3d_rendertarget_view_decref(device->back_buffer_view);
1109 if (swapchain)
1110 wined3d_swapchain_decref(swapchain);
1111 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1113 return hr;
1116 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1117 struct wined3d_swapchain_desc *swapchain_desc)
1119 struct wined3d_swapchain *swapchain = NULL;
1120 HRESULT hr;
1122 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1124 /* Setup the implicit swapchain */
1125 TRACE("Creating implicit swapchain\n");
1126 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1127 swapchain_desc, &swapchain);
1128 if (FAILED(hr))
1130 WARN("Failed to create implicit swapchain\n");
1131 goto err_out;
1134 device->swapchain_count = 1;
1135 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1137 ERR("Out of memory!\n");
1138 goto err_out;
1140 device->swapchains[0] = swapchain;
1141 return WINED3D_OK;
1143 err_out:
1144 wined3d_swapchain_decref(swapchain);
1145 return hr;
1148 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1150 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1152 wined3d_sampler_decref(sampler);
1155 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1157 UINT i;
1159 TRACE("device %p.\n", device);
1161 if (!device->d3d_initialized)
1162 return WINED3DERR_INVALIDCALL;
1164 if (device->logo_texture)
1165 wined3d_texture_decref(device->logo_texture);
1166 if (device->cursor_texture)
1167 wined3d_texture_decref(device->cursor_texture);
1169 state_unbind_resources(&device->state);
1171 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1173 wined3d_device_delete_opengl_contexts(device);
1175 if (device->fb.depth_stencil)
1177 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1179 TRACE("Releasing depth/stencil view %p.\n", view);
1181 device->fb.depth_stencil = NULL;
1182 wined3d_rendertarget_view_decref(view);
1185 if (device->auto_depth_stencil_view)
1187 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1189 device->auto_depth_stencil_view = NULL;
1190 if (wined3d_rendertarget_view_decref(view))
1191 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1194 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
1196 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1198 if (device->back_buffer_view)
1200 wined3d_rendertarget_view_decref(device->back_buffer_view);
1201 device->back_buffer_view = NULL;
1204 for (i = 0; i < device->swapchain_count; ++i)
1206 TRACE("Releasing the implicit swapchain %u.\n", i);
1207 if (wined3d_swapchain_decref(device->swapchains[i]))
1208 FIXME("Something's still holding the implicit swapchain.\n");
1211 HeapFree(GetProcessHeap(), 0, device->swapchains);
1212 device->swapchains = NULL;
1213 device->swapchain_count = 0;
1215 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1216 device->fb.render_targets = NULL;
1218 device->d3d_initialized = FALSE;
1220 return WINED3D_OK;
1223 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1225 unsigned int i;
1227 for (i = 0; i < device->swapchain_count; ++i)
1229 TRACE("Releasing the implicit swapchain %u.\n", i);
1230 if (wined3d_swapchain_decref(device->swapchains[i]))
1231 FIXME("Something's still holding the implicit swapchain.\n");
1234 HeapFree(GetProcessHeap(), 0, device->swapchains);
1235 device->swapchains = NULL;
1236 device->swapchain_count = 0;
1237 return WINED3D_OK;
1240 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1241 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1242 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1244 * There is no way to deactivate thread safety once it is enabled.
1246 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1248 TRACE("device %p.\n", device);
1250 /* For now just store the flag (needed in case of ddraw). */
1251 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1254 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1256 TRACE("device %p.\n", device);
1258 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1259 wine_dbgstr_longlong(device->adapter->vram_bytes),
1260 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1261 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1263 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1266 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1267 struct wined3d_buffer *buffer, UINT offset)
1269 struct wined3d_stream_output *stream;
1270 struct wined3d_buffer *prev_buffer;
1272 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1274 if (idx >= MAX_STREAM_OUT)
1276 WARN("Invalid stream output %u.\n", idx);
1277 return;
1280 stream = &device->update_state->stream_output[idx];
1281 prev_buffer = stream->buffer;
1283 if (buffer)
1284 wined3d_buffer_incref(buffer);
1285 stream->buffer = buffer;
1286 stream->offset = offset;
1287 if (!device->recording)
1288 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1289 if (prev_buffer)
1290 wined3d_buffer_decref(prev_buffer);
1293 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1294 UINT idx, UINT *offset)
1296 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1298 if (idx >= MAX_STREAM_OUT)
1300 WARN("Invalid stream output %u.\n", idx);
1301 return NULL;
1304 if (offset)
1305 *offset = device->state.stream_output[idx].offset;
1306 return device->state.stream_output[idx].buffer;
1309 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1310 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1312 struct wined3d_stream_state *stream;
1313 struct wined3d_buffer *prev_buffer;
1315 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1316 device, stream_idx, buffer, offset, stride);
1318 if (stream_idx >= MAX_STREAMS)
1320 WARN("Stream index %u out of range.\n", stream_idx);
1321 return WINED3DERR_INVALIDCALL;
1323 else if (offset & 0x3)
1325 WARN("Offset %u is not 4 byte aligned.\n", offset);
1326 return WINED3DERR_INVALIDCALL;
1329 stream = &device->update_state->streams[stream_idx];
1330 prev_buffer = stream->buffer;
1332 if (device->recording)
1333 device->recording->changed.streamSource |= 1u << stream_idx;
1335 if (prev_buffer == buffer
1336 && stream->stride == stride
1337 && stream->offset == offset)
1339 TRACE("Application is setting the old values over, nothing to do.\n");
1340 return WINED3D_OK;
1343 stream->buffer = buffer;
1344 if (buffer)
1346 stream->stride = stride;
1347 stream->offset = offset;
1348 wined3d_buffer_incref(buffer);
1351 if (!device->recording)
1352 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1353 if (prev_buffer)
1354 wined3d_buffer_decref(prev_buffer);
1356 return WINED3D_OK;
1359 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1360 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1362 const struct wined3d_stream_state *stream;
1364 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1365 device, stream_idx, buffer, offset, stride);
1367 if (stream_idx >= MAX_STREAMS)
1369 WARN("Stream index %u out of range.\n", stream_idx);
1370 return WINED3DERR_INVALIDCALL;
1373 stream = &device->state.streams[stream_idx];
1374 *buffer = stream->buffer;
1375 if (offset)
1376 *offset = stream->offset;
1377 *stride = stream->stride;
1379 return WINED3D_OK;
1382 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1384 struct wined3d_stream_state *stream;
1385 UINT old_flags, old_freq;
1387 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1389 /* Verify input. At least in d3d9 this is invalid. */
1390 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1392 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1393 return WINED3DERR_INVALIDCALL;
1395 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1397 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1398 return WINED3DERR_INVALIDCALL;
1400 if (!divider)
1402 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1403 return WINED3DERR_INVALIDCALL;
1406 stream = &device->update_state->streams[stream_idx];
1407 old_flags = stream->flags;
1408 old_freq = stream->frequency;
1410 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1411 stream->frequency = divider & 0x7fffff;
1413 if (device->recording)
1414 device->recording->changed.streamFreq |= 1u << stream_idx;
1415 else if (stream->frequency != old_freq || stream->flags != old_flags)
1416 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1418 return WINED3D_OK;
1421 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1422 UINT stream_idx, UINT *divider)
1424 const struct wined3d_stream_state *stream;
1426 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1428 stream = &device->state.streams[stream_idx];
1429 *divider = stream->flags | stream->frequency;
1431 TRACE("Returning %#x.\n", *divider);
1433 return WINED3D_OK;
1436 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1437 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1439 TRACE("device %p, state %s, matrix %p.\n",
1440 device, debug_d3dtstype(d3dts), matrix);
1441 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1442 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1443 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1444 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1446 /* Handle recording of state blocks. */
1447 if (device->recording)
1449 TRACE("Recording... not performing anything.\n");
1450 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1451 device->update_state->transforms[d3dts] = *matrix;
1452 return;
1455 /* If the new matrix is the same as the current one,
1456 * we cut off any further processing. this seems to be a reasonable
1457 * optimization because as was noticed, some apps (warcraft3 for example)
1458 * tend towards setting the same matrix repeatedly for some reason.
1460 * From here on we assume that the new matrix is different, wherever it matters. */
1461 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1463 TRACE("The application is setting the same matrix over again.\n");
1464 return;
1467 device->state.transforms[d3dts] = *matrix;
1468 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1471 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1472 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1474 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1476 *matrix = device->state.transforms[state];
1479 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1480 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1482 const struct wined3d_matrix *mat;
1483 struct wined3d_matrix temp;
1485 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1487 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1488 * below means it will be recorded in a state block change, but it
1489 * works regardless where it is recorded.
1490 * If this is found to be wrong, change to StateBlock. */
1491 if (state > HIGHEST_TRANSFORMSTATE)
1493 WARN("Unhandled transform state %#x.\n", state);
1494 return;
1497 mat = &device->update_state->transforms[state];
1498 multiply_matrix(&temp, mat, matrix);
1500 /* Apply change via set transform - will reapply to eg. lights this way. */
1501 wined3d_device_set_transform(device, state, &temp);
1504 /* Note lights are real special cases. Although the device caps state only
1505 * e.g. 8 are supported, you can reference any indexes you want as long as
1506 * that number max are enabled at any one point in time. Therefore since the
1507 * indices can be anything, we need a hashmap of them. However, this causes
1508 * stateblock problems. When capturing the state block, I duplicate the
1509 * hashmap, but when recording, just build a chain pretty much of commands to
1510 * be replayed. */
1511 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1512 UINT light_idx, const struct wined3d_light *light)
1514 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1515 struct wined3d_light_info *object = NULL;
1516 float rho;
1518 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1520 /* Check the parameter range. Need for speed most wanted sets junk lights
1521 * which confuse the GL driver. */
1522 if (!light)
1523 return WINED3DERR_INVALIDCALL;
1525 switch (light->type)
1527 case WINED3D_LIGHT_POINT:
1528 case WINED3D_LIGHT_SPOT:
1529 case WINED3D_LIGHT_GLSPOT:
1530 /* Incorrect attenuation values can cause the gl driver to crash.
1531 * Happens with Need for speed most wanted. */
1532 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1534 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1535 return WINED3DERR_INVALIDCALL;
1537 break;
1539 case WINED3D_LIGHT_DIRECTIONAL:
1540 case WINED3D_LIGHT_PARALLELPOINT:
1541 /* Ignores attenuation */
1542 break;
1544 default:
1545 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1546 return WINED3DERR_INVALIDCALL;
1549 if (!(object = wined3d_state_get_light(device->update_state, light_idx)))
1551 TRACE("Adding new light\n");
1552 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1553 if (!object)
1554 return E_OUTOFMEMORY;
1556 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1557 object->glIndex = -1;
1558 object->OriginalIndex = light_idx;
1561 /* Initialize the object. */
1562 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1563 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1564 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1565 light_idx, light->type, debug_color(&light->diffuse),
1566 debug_color(&light->specular), debug_color(&light->ambient),
1567 light->position.x, light->position.y, light->position.z,
1568 light->direction.x, light->direction.y, light->direction.z,
1569 light->range, light->falloff, light->theta, light->phi);
1571 /* Save away the information. */
1572 object->OriginalParms = *light;
1574 switch (light->type)
1576 case WINED3D_LIGHT_POINT:
1577 /* Position */
1578 object->position.x = light->position.x;
1579 object->position.y = light->position.y;
1580 object->position.z = light->position.z;
1581 object->position.w = 1.0f;
1582 object->cutoff = 180.0f;
1583 /* FIXME: Range */
1584 break;
1586 case WINED3D_LIGHT_DIRECTIONAL:
1587 /* Direction */
1588 object->direction.x = -light->direction.x;
1589 object->direction.y = -light->direction.y;
1590 object->direction.z = -light->direction.z;
1591 object->direction.w = 0.0f;
1592 object->exponent = 0.0f;
1593 object->cutoff = 180.0f;
1594 break;
1596 case WINED3D_LIGHT_SPOT:
1597 /* Position */
1598 object->position.x = light->position.x;
1599 object->position.y = light->position.y;
1600 object->position.z = light->position.z;
1601 object->position.w = 1.0f;
1603 /* Direction */
1604 object->direction.x = light->direction.x;
1605 object->direction.y = light->direction.y;
1606 object->direction.z = light->direction.z;
1607 object->direction.w = 0.0f;
1609 /* opengl-ish and d3d-ish spot lights use too different models
1610 * for the light "intensity" as a function of the angle towards
1611 * the main light direction, so we only can approximate very
1612 * roughly. However, spot lights are rather rarely used in games
1613 * (if ever used at all). Furthermore if still used, probably
1614 * nobody pays attention to such details. */
1615 if (!light->falloff)
1617 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1618 * equations have the falloff resp. exponent parameter as an
1619 * exponent, so the spot light lighting will always be 1.0 for
1620 * both of them, and we don't have to care for the rest of the
1621 * rather complex calculation. */
1622 object->exponent = 0.0f;
1624 else
1626 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1627 if (rho < 0.0001f)
1628 rho = 0.0001f;
1629 object->exponent = -0.3f / logf(cosf(rho / 2));
1632 if (object->exponent > 128.0f)
1633 object->exponent = 128.0f;
1635 object->cutoff = (float)(light->phi * 90 / M_PI);
1636 /* FIXME: Range */
1637 break;
1639 case WINED3D_LIGHT_PARALLELPOINT:
1640 object->position.x = light->position.x;
1641 object->position.y = light->position.y;
1642 object->position.z = light->position.z;
1643 object->position.w = 1.0f;
1644 break;
1646 default:
1647 FIXME("Unrecognized light type %#x.\n", light->type);
1650 if (!device->recording)
1651 wined3d_cs_emit_set_light(device->cs, object);
1653 return WINED3D_OK;
1656 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1657 UINT light_idx, struct wined3d_light *light)
1659 struct wined3d_light_info *light_info;
1661 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1663 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1665 TRACE("Light information requested but light not defined\n");
1666 return WINED3DERR_INVALIDCALL;
1669 *light = light_info->OriginalParms;
1670 return WINED3D_OK;
1673 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1675 struct wined3d_light_info *light_info;
1677 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1679 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1680 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1682 TRACE("Light enabled requested but light not defined, so defining one!\n");
1683 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1685 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1687 FIXME("Adding default lights has failed dismally\n");
1688 return WINED3DERR_INVALIDCALL;
1692 wined3d_state_enable_light(device->update_state, &device->adapter->d3d_info, light_info, enable);
1693 if (!device->recording)
1694 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1696 return WINED3D_OK;
1699 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1701 struct wined3d_light_info *light_info;
1703 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1705 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1707 TRACE("Light enabled state requested but light not defined.\n");
1708 return WINED3DERR_INVALIDCALL;
1710 /* true is 128 according to SetLightEnable */
1711 *enable = light_info->enabled ? 128 : 0;
1712 return WINED3D_OK;
1715 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1716 UINT plane_idx, const struct wined3d_vec4 *plane)
1718 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1720 /* Validate plane_idx. */
1721 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1723 TRACE("Application has requested clipplane this device doesn't support.\n");
1724 return WINED3DERR_INVALIDCALL;
1727 if (device->recording)
1728 device->recording->changed.clipplane |= 1u << plane_idx;
1730 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1732 TRACE("Application is setting old values over, nothing to do.\n");
1733 return WINED3D_OK;
1736 device->update_state->clip_planes[plane_idx] = *plane;
1738 if (!device->recording)
1739 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1741 return WINED3D_OK;
1744 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1745 UINT plane_idx, struct wined3d_vec4 *plane)
1747 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1749 /* Validate plane_idx. */
1750 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1752 TRACE("Application has requested clipplane this device doesn't support.\n");
1753 return WINED3DERR_INVALIDCALL;
1756 *plane = device->state.clip_planes[plane_idx];
1758 return WINED3D_OK;
1761 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1762 const struct wined3d_clip_status *clip_status)
1764 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1766 if (!clip_status)
1767 return WINED3DERR_INVALIDCALL;
1769 return WINED3D_OK;
1772 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1773 struct wined3d_clip_status *clip_status)
1775 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1777 if (!clip_status)
1778 return WINED3DERR_INVALIDCALL;
1780 return WINED3D_OK;
1783 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1785 TRACE("device %p, material %p.\n", device, material);
1787 device->update_state->material = *material;
1789 if (device->recording)
1790 device->recording->changed.material = TRUE;
1791 else
1792 wined3d_cs_emit_set_material(device->cs, material);
1795 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1797 TRACE("device %p, material %p.\n", device, material);
1799 *material = device->state.material;
1801 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1802 TRACE("ambient %s\n", debug_color(&material->ambient));
1803 TRACE("specular %s\n", debug_color(&material->specular));
1804 TRACE("emissive %s\n", debug_color(&material->emissive));
1805 TRACE("power %.8e.\n", material->power);
1808 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1809 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1811 enum wined3d_format_id prev_format;
1812 struct wined3d_buffer *prev_buffer;
1813 unsigned int prev_offset;
1815 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1816 device, buffer, debug_d3dformat(format_id), offset);
1818 prev_buffer = device->update_state->index_buffer;
1819 prev_format = device->update_state->index_format;
1820 prev_offset = device->update_state->index_offset;
1822 device->update_state->index_buffer = buffer;
1823 device->update_state->index_format = format_id;
1824 device->update_state->index_offset = offset;
1826 if (device->recording)
1827 device->recording->changed.indices = TRUE;
1829 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1830 return;
1832 if (buffer)
1833 wined3d_buffer_incref(buffer);
1834 if (!device->recording)
1835 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1836 if (prev_buffer)
1837 wined3d_buffer_decref(prev_buffer);
1840 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1841 enum wined3d_format_id *format, unsigned int *offset)
1843 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1845 *format = device->state.index_format;
1846 if (offset)
1847 *offset = device->state.index_offset;
1848 return device->state.index_buffer;
1851 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1853 TRACE("device %p, base_index %d.\n", device, base_index);
1855 device->update_state->base_vertex_index = base_index;
1858 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1860 TRACE("device %p.\n", device);
1862 return device->state.base_vertex_index;
1865 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1867 TRACE("device %p, viewport %p.\n", device, viewport);
1868 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1869 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1871 device->update_state->viewport = *viewport;
1873 /* Handle recording of state blocks */
1874 if (device->recording)
1876 TRACE("Recording... not performing anything\n");
1877 device->recording->changed.viewport = TRUE;
1878 return;
1881 wined3d_cs_emit_set_viewport(device->cs, viewport);
1884 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1886 TRACE("device %p, viewport %p.\n", device, viewport);
1888 *viewport = device->state.viewport;
1891 static void resolve_depth_buffer(struct wined3d_state *state)
1893 struct wined3d_texture *dst_texture = state->textures[0];
1894 struct wined3d_rendertarget_view *src_view;
1895 RECT src_rect, dst_rect;
1897 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1898 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1899 return;
1901 if (!(src_view = state->fb->depth_stencil))
1902 return;
1903 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
1905 FIXME("Not supported on buffer resources.\n");
1906 return;
1909 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
1910 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
1911 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
1912 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1915 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1916 struct wined3d_rasterizer_state *rasterizer_state)
1918 struct wined3d_rasterizer_state *prev;
1920 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
1922 prev = device->update_state->rasterizer_state;
1923 if (prev == rasterizer_state)
1924 return;
1926 if (rasterizer_state)
1927 wined3d_rasterizer_state_incref(rasterizer_state);
1928 device->update_state->rasterizer_state = rasterizer_state;
1929 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
1930 if (prev)
1931 wined3d_rasterizer_state_decref(prev);
1934 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
1936 TRACE("device %p.\n", device);
1938 return device->state.rasterizer_state;
1941 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1942 enum wined3d_render_state state, DWORD value)
1944 DWORD old_value;
1946 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1948 if (state > WINEHIGHEST_RENDER_STATE)
1950 WARN("Unhandled render state %#x.\n", state);
1951 return;
1954 old_value = device->state.render_states[state];
1955 device->update_state->render_states[state] = value;
1957 /* Handle recording of state blocks. */
1958 if (device->recording)
1960 TRACE("Recording... not performing anything.\n");
1961 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
1962 return;
1965 /* Compared here and not before the assignment to allow proper stateblock recording. */
1966 if (value == old_value)
1967 TRACE("Application is setting the old value over, nothing to do.\n");
1968 else
1969 wined3d_cs_emit_set_render_state(device->cs, state, value);
1971 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1973 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1974 resolve_depth_buffer(&device->state);
1978 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1980 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1982 return device->state.render_states[state];
1985 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1986 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1988 DWORD old_value;
1990 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1991 device, sampler_idx, debug_d3dsamplerstate(state), value);
1993 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1994 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1996 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1998 WARN("Invalid sampler %u.\n", sampler_idx);
1999 return; /* Windows accepts overflowing this array ... we do not. */
2002 old_value = device->state.sampler_states[sampler_idx][state];
2003 device->update_state->sampler_states[sampler_idx][state] = value;
2005 /* Handle recording of state blocks. */
2006 if (device->recording)
2008 TRACE("Recording... not performing anything.\n");
2009 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2010 return;
2013 if (old_value == value)
2015 TRACE("Application is setting the old value over, nothing to do.\n");
2016 return;
2019 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2022 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2023 UINT sampler_idx, enum wined3d_sampler_state state)
2025 TRACE("device %p, sampler_idx %u, state %s.\n",
2026 device, sampler_idx, debug_d3dsamplerstate(state));
2028 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2029 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2031 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2033 WARN("Invalid sampler %u.\n", sampler_idx);
2034 return 0; /* Windows accepts overflowing this array ... we do not. */
2037 return device->state.sampler_states[sampler_idx][state];
2040 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2042 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2044 if (device->recording)
2045 device->recording->changed.scissorRect = TRUE;
2047 if (EqualRect(&device->update_state->scissor_rect, rect))
2049 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2050 return;
2052 CopyRect(&device->update_state->scissor_rect, rect);
2054 if (device->recording)
2056 TRACE("Recording... not performing anything.\n");
2057 return;
2060 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2063 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2065 TRACE("device %p, rect %p.\n", device, rect);
2067 *rect = device->state.scissor_rect;
2068 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2071 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2072 struct wined3d_vertex_declaration *declaration)
2074 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2076 TRACE("device %p, declaration %p.\n", device, declaration);
2078 if (device->recording)
2079 device->recording->changed.vertexDecl = TRUE;
2081 if (declaration == prev)
2082 return;
2084 if (declaration)
2085 wined3d_vertex_declaration_incref(declaration);
2086 device->update_state->vertex_declaration = declaration;
2087 if (!device->recording)
2088 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2089 if (prev)
2090 wined3d_vertex_declaration_decref(prev);
2093 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2095 TRACE("device %p.\n", device);
2097 return device->state.vertex_declaration;
2100 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2102 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2104 TRACE("device %p, shader %p.\n", device, shader);
2106 if (device->recording)
2107 device->recording->changed.vertexShader = TRUE;
2109 if (shader == prev)
2110 return;
2112 if (shader)
2113 wined3d_shader_incref(shader);
2114 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2115 if (!device->recording)
2116 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2117 if (prev)
2118 wined3d_shader_decref(prev);
2121 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2123 TRACE("device %p.\n", device);
2125 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2128 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2129 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2131 struct wined3d_buffer *prev;
2133 if (idx >= MAX_CONSTANT_BUFFERS)
2135 WARN("Invalid constant buffer index %u.\n", idx);
2136 return;
2139 prev = device->update_state->cb[type][idx];
2140 if (buffer == prev)
2141 return;
2143 if (buffer)
2144 wined3d_buffer_incref(buffer);
2145 device->update_state->cb[type][idx] = buffer;
2146 if (!device->recording)
2147 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2148 if (prev)
2149 wined3d_buffer_decref(prev);
2152 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2154 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2156 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2159 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2161 TRACE("device %p, idx %u.\n", device, idx);
2163 if (idx >= MAX_CONSTANT_BUFFERS)
2165 WARN("Invalid constant buffer index %u.\n", idx);
2166 return NULL;
2169 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2172 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2173 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2175 struct wined3d_shader_resource_view *prev;
2177 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2179 WARN("Invalid view index %u.\n", idx);
2180 return;
2183 prev = device->update_state->shader_resource_view[type][idx];
2184 if (view == prev)
2185 return;
2187 if (view)
2188 wined3d_shader_resource_view_incref(view);
2189 device->update_state->shader_resource_view[type][idx] = view;
2190 if (!device->recording)
2191 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2192 if (prev)
2193 wined3d_shader_resource_view_decref(prev);
2196 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2197 UINT idx, struct wined3d_shader_resource_view *view)
2199 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2201 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2204 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2205 UINT idx)
2207 TRACE("device %p, idx %u.\n", device, idx);
2209 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2211 WARN("Invalid view index %u.\n", idx);
2212 return NULL;
2215 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2218 static void wined3d_device_set_sampler(struct wined3d_device *device,
2219 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2221 struct wined3d_sampler *prev;
2223 if (idx >= MAX_SAMPLER_OBJECTS)
2225 WARN("Invalid sampler index %u.\n", idx);
2226 return;
2229 prev = device->update_state->sampler[type][idx];
2230 if (sampler == prev)
2231 return;
2233 if (sampler)
2234 wined3d_sampler_incref(sampler);
2235 device->update_state->sampler[type][idx] = sampler;
2236 if (!device->recording)
2237 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2238 if (prev)
2239 wined3d_sampler_decref(prev);
2242 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2244 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2246 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2249 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2251 TRACE("device %p, idx %u.\n", device, idx);
2253 if (idx >= MAX_SAMPLER_OBJECTS)
2255 WARN("Invalid sampler index %u.\n", idx);
2256 return NULL;
2259 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2262 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2263 unsigned int start_idx, unsigned int count, const BOOL *constants)
2265 unsigned int i;
2267 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2268 device, start_idx, count, constants);
2270 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2271 return WINED3DERR_INVALIDCALL;
2273 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2274 count = WINED3D_MAX_CONSTS_B - start_idx;
2275 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2276 if (TRACE_ON(d3d))
2278 for (i = 0; i < count; ++i)
2279 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2282 if (device->recording)
2284 for (i = start_idx; i < count + start_idx; ++i)
2285 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2287 else
2289 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2292 return WINED3D_OK;
2295 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2296 unsigned int start_idx, unsigned int count, BOOL *constants)
2298 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2299 device, start_idx, count, constants);
2301 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2302 return WINED3DERR_INVALIDCALL;
2304 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2305 count = WINED3D_MAX_CONSTS_B - start_idx;
2306 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2308 return WINED3D_OK;
2311 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2312 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2314 unsigned int i;
2316 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2317 device, start_idx, count, constants);
2319 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2320 return WINED3DERR_INVALIDCALL;
2322 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2323 count = WINED3D_MAX_CONSTS_I - start_idx;
2324 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2325 if (TRACE_ON(d3d))
2327 for (i = 0; i < count; ++i)
2328 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2331 if (device->recording)
2333 for (i = start_idx; i < count + start_idx; ++i)
2334 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2336 else
2338 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2341 return WINED3D_OK;
2344 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2345 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2347 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2348 device, start_idx, count, constants);
2350 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2351 return WINED3DERR_INVALIDCALL;
2353 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2354 count = WINED3D_MAX_CONSTS_I - start_idx;
2355 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2356 return WINED3D_OK;
2359 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2360 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2362 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2363 unsigned int i;
2365 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2366 device, start_idx, count, constants);
2368 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2369 || count > d3d_info->limits.vs_uniform_count - start_idx)
2370 return WINED3DERR_INVALIDCALL;
2372 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2373 if (TRACE_ON(d3d))
2375 for (i = 0; i < count; ++i)
2376 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2379 if (device->recording)
2380 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2381 count * sizeof(*device->recording->changed.vs_consts_f));
2382 else
2383 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2385 return WINED3D_OK;
2388 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2389 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2391 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2393 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2394 device, start_idx, count, constants);
2396 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2397 || count > d3d_info->limits.vs_uniform_count - start_idx)
2398 return WINED3DERR_INVALIDCALL;
2400 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2402 return WINED3D_OK;
2405 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2407 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2409 TRACE("device %p, shader %p.\n", device, shader);
2411 if (device->recording)
2412 device->recording->changed.pixelShader = TRUE;
2414 if (shader == prev)
2415 return;
2417 if (shader)
2418 wined3d_shader_incref(shader);
2419 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2420 if (!device->recording)
2421 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2422 if (prev)
2423 wined3d_shader_decref(prev);
2426 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2428 TRACE("device %p.\n", device);
2430 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2433 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2435 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2437 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2440 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2442 TRACE("device %p, idx %u.\n", device, idx);
2444 if (idx >= MAX_CONSTANT_BUFFERS)
2446 WARN("Invalid constant buffer index %u.\n", idx);
2447 return NULL;
2450 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2453 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2454 UINT idx, struct wined3d_shader_resource_view *view)
2456 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2458 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2461 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2462 UINT idx)
2464 TRACE("device %p, idx %u.\n", device, idx);
2466 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2468 WARN("Invalid view index %u.\n", idx);
2469 return NULL;
2472 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2475 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2477 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2479 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2482 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2484 TRACE("device %p, idx %u.\n", device, idx);
2486 if (idx >= MAX_SAMPLER_OBJECTS)
2488 WARN("Invalid sampler index %u.\n", idx);
2489 return NULL;
2492 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2495 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2496 unsigned int start_idx, unsigned int count, const BOOL *constants)
2498 unsigned int i;
2500 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2501 device, start_idx, count, constants);
2503 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2504 return WINED3DERR_INVALIDCALL;
2506 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2507 count = WINED3D_MAX_CONSTS_B - start_idx;
2508 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2509 if (TRACE_ON(d3d))
2511 for (i = 0; i < count; ++i)
2512 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2515 if (device->recording)
2517 for (i = start_idx; i < count + start_idx; ++i)
2518 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2520 else
2522 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2525 return WINED3D_OK;
2528 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2529 unsigned int start_idx, unsigned int count, BOOL *constants)
2531 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2532 device, start_idx, count, constants);
2534 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2535 return WINED3DERR_INVALIDCALL;
2537 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2538 count = WINED3D_MAX_CONSTS_B - start_idx;
2539 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2541 return WINED3D_OK;
2544 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2545 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2547 unsigned int i;
2549 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2550 device, start_idx, count, constants);
2552 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2553 return WINED3DERR_INVALIDCALL;
2555 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2556 count = WINED3D_MAX_CONSTS_I - start_idx;
2557 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2558 if (TRACE_ON(d3d))
2560 for (i = 0; i < count; ++i)
2561 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2564 if (device->recording)
2566 for (i = start_idx; i < count + start_idx; ++i)
2567 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2569 else
2571 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2574 return WINED3D_OK;
2577 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2578 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2580 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2581 device, start_idx, count, constants);
2583 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2584 return WINED3DERR_INVALIDCALL;
2586 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2587 count = WINED3D_MAX_CONSTS_I - start_idx;
2588 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2590 return WINED3D_OK;
2593 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2594 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2596 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2597 unsigned int i;
2599 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2600 device, start_idx, count, constants);
2602 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2603 || count > d3d_info->limits.ps_uniform_count - start_idx)
2604 return WINED3DERR_INVALIDCALL;
2606 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2607 if (TRACE_ON(d3d))
2609 for (i = 0; i < count; ++i)
2610 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2613 if (device->recording)
2614 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2615 count * sizeof(*device->recording->changed.ps_consts_f));
2616 else
2617 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2619 return WINED3D_OK;
2622 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2623 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2625 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2627 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2628 device, start_idx, count, constants);
2630 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2631 || count > d3d_info->limits.ps_uniform_count - start_idx)
2632 return WINED3DERR_INVALIDCALL;
2634 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2636 return WINED3D_OK;
2639 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2641 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2643 TRACE("device %p, shader %p.\n", device, shader);
2645 if (device->recording || shader == prev)
2646 return;
2647 if (shader)
2648 wined3d_shader_incref(shader);
2649 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2650 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2651 if (prev)
2652 wined3d_shader_decref(prev);
2655 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2657 TRACE("device %p.\n", device);
2659 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2662 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2664 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2666 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2669 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2671 TRACE("device %p, idx %u.\n", device, idx);
2673 if (idx >= MAX_CONSTANT_BUFFERS)
2675 WARN("Invalid constant buffer index %u.\n", idx);
2676 return NULL;
2679 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2682 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2683 UINT idx, struct wined3d_shader_resource_view *view)
2685 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2687 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2690 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2691 UINT idx)
2693 TRACE("device %p, idx %u.\n", device, idx);
2695 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2697 WARN("Invalid view index %u.\n", idx);
2698 return NULL;
2701 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2704 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2706 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2708 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2711 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2713 TRACE("device %p, idx %u.\n", device, idx);
2715 if (idx >= MAX_SAMPLER_OBJECTS)
2717 WARN("Invalid sampler index %u.\n", idx);
2718 return NULL;
2721 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2724 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2726 struct wined3d_shader *prev;
2728 TRACE("device %p, shader %p.\n", device, shader);
2730 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2731 if (device->recording || shader == prev)
2732 return;
2733 if (shader)
2734 wined3d_shader_incref(shader);
2735 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2736 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2737 if (prev)
2738 wined3d_shader_decref(prev);
2741 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2743 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2745 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2748 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2749 unsigned int idx, struct wined3d_shader_resource_view *view)
2751 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2753 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2756 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2757 unsigned int idx, struct wined3d_sampler *sampler)
2759 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2761 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2764 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2765 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav)
2767 struct wined3d_unordered_access_view *prev;
2769 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2771 WARN("Invalid UAV index %u.\n", idx);
2772 return;
2775 prev = device->update_state->unordered_access_view[pipeline][idx];
2776 if (uav == prev)
2777 return;
2779 if (uav)
2780 wined3d_unordered_access_view_incref(uav);
2781 device->update_state->unordered_access_view[pipeline][idx] = uav;
2782 if (!device->recording)
2783 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav);
2784 if (prev)
2785 wined3d_unordered_access_view_decref(prev);
2788 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2789 struct wined3d_unordered_access_view *uav)
2791 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2793 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav);
2796 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2797 unsigned int idx, struct wined3d_unordered_access_view *uav)
2799 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2801 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav);
2804 /* Context activation is done by the caller. */
2805 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2806 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2807 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2808 DWORD DestFVF)
2810 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2811 struct wined3d_map_desc map_desc;
2812 struct wined3d_box box = {0};
2813 struct wined3d_viewport vp;
2814 UINT vertex_size;
2815 unsigned int i;
2816 BYTE *dest_ptr;
2817 BOOL doClip;
2818 DWORD numTextures;
2819 HRESULT hr;
2821 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2823 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2826 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2828 ERR("Source has no position mask\n");
2829 return WINED3DERR_INVALIDCALL;
2832 if (device->state.render_states[WINED3D_RS_CLIPPING])
2834 static BOOL warned = FALSE;
2836 * The clipping code is not quite correct. Some things need
2837 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2838 * so disable clipping for now.
2839 * (The graphics in Half-Life are broken, and my processvertices
2840 * test crashes with IDirect3DDevice3)
2841 doClip = TRUE;
2843 doClip = FALSE;
2844 if(!warned) {
2845 warned = TRUE;
2846 FIXME("Clipping is broken and disabled for now\n");
2849 else
2850 doClip = FALSE;
2852 vertex_size = get_flexible_vertex_size(DestFVF);
2853 box.left = dwDestIndex * vertex_size;
2854 box.right = box.left + dwCount * vertex_size;
2855 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2857 WARN("Failed to map buffer, hr %#x.\n", hr);
2858 return hr;
2860 dest_ptr = map_desc.data;
2862 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2863 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2864 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2866 TRACE("View mat:\n");
2867 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2868 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2869 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2870 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2872 TRACE("Proj mat:\n");
2873 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2874 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2875 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2876 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2878 TRACE("World mat:\n");
2879 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2880 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2881 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2882 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2884 /* Get the viewport */
2885 wined3d_device_get_viewport(device, &vp);
2886 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2887 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2889 multiply_matrix(&mat,&view_mat,&world_mat);
2890 multiply_matrix(&mat,&proj_mat,&mat);
2892 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2894 for (i = 0; i < dwCount; i+= 1) {
2895 unsigned int tex_index;
2897 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2898 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2899 /* The position first */
2900 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2901 const float *p = (const float *)(element->data.addr + i * element->stride);
2902 float x, y, z, rhw;
2903 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2905 /* Multiplication with world, view and projection matrix. */
2906 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2907 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2908 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2909 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2911 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2913 /* WARNING: The following things are taken from d3d7 and were not yet checked
2914 * against d3d8 or d3d9!
2917 /* Clipping conditions: From msdn
2919 * A vertex is clipped if it does not match the following requirements
2920 * -rhw < x <= rhw
2921 * -rhw < y <= rhw
2922 * 0 < z <= rhw
2923 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2925 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2926 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2930 if( !doClip ||
2931 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2932 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2933 ( rhw > eps ) ) ) {
2935 /* "Normal" viewport transformation (not clipped)
2936 * 1) The values are divided by rhw
2937 * 2) The y axis is negative, so multiply it with -1
2938 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2939 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2940 * 4) Multiply x with Width/2 and add Width/2
2941 * 5) The same for the height
2942 * 6) Add the viewpoint X and Y to the 2D coordinates and
2943 * The minimum Z value to z
2944 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2946 * Well, basically it's simply a linear transformation into viewport
2947 * coordinates
2950 x /= rhw;
2951 y /= rhw;
2952 z /= rhw;
2954 y *= -1;
2956 x *= vp.width / 2;
2957 y *= vp.height / 2;
2958 z *= vp.max_z - vp.min_z;
2960 x += vp.width / 2 + vp.x;
2961 y += vp.height / 2 + vp.y;
2962 z += vp.min_z;
2964 rhw = 1 / rhw;
2965 } else {
2966 /* That vertex got clipped
2967 * Contrary to OpenGL it is not dropped completely, it just
2968 * undergoes a different calculation.
2970 TRACE("Vertex got clipped\n");
2971 x += rhw;
2972 y += rhw;
2974 x /= 2;
2975 y /= 2;
2977 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2978 * outside of the main vertex buffer memory. That needs some more
2979 * investigation...
2983 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2986 ( (float *) dest_ptr)[0] = x;
2987 ( (float *) dest_ptr)[1] = y;
2988 ( (float *) dest_ptr)[2] = z;
2989 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2991 dest_ptr += 3 * sizeof(float);
2993 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2994 dest_ptr += sizeof(float);
2997 if (DestFVF & WINED3DFVF_PSIZE)
2998 dest_ptr += sizeof(DWORD);
3000 if (DestFVF & WINED3DFVF_NORMAL)
3002 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3003 const float *normal = (const float *)(element->data.addr + i * element->stride);
3004 /* AFAIK this should go into the lighting information */
3005 FIXME("Didn't expect the destination to have a normal\n");
3006 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3009 if (DestFVF & WINED3DFVF_DIFFUSE)
3011 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3012 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3013 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3015 static BOOL warned = FALSE;
3017 if(!warned) {
3018 ERR("No diffuse color in source, but destination has one\n");
3019 warned = TRUE;
3022 *( (DWORD *) dest_ptr) = 0xffffffff;
3023 dest_ptr += sizeof(DWORD);
3025 else
3027 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3031 if (DestFVF & WINED3DFVF_SPECULAR)
3033 /* What's the color value in the feedback buffer? */
3034 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3035 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3036 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3038 static BOOL warned = FALSE;
3040 if(!warned) {
3041 ERR("No specular color in source, but destination has one\n");
3042 warned = TRUE;
3045 *(DWORD *)dest_ptr = 0xff000000;
3046 dest_ptr += sizeof(DWORD);
3048 else
3050 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3054 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3056 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3057 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3058 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3060 ERR("No source texture, but destination requests one\n");
3061 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3063 else
3065 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3070 wined3d_resource_unmap(&dest->resource, 0);
3072 return WINED3D_OK;
3074 #undef copy_and_next
3076 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3077 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3078 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3080 struct wined3d_state *state = &device->state;
3081 struct wined3d_stream_info stream_info;
3082 const struct wined3d_gl_info *gl_info;
3083 struct wined3d_context *context;
3084 struct wined3d_shader *vs;
3085 unsigned int i;
3086 HRESULT hr;
3087 WORD map;
3089 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3090 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3091 device, src_start_idx, dst_idx, vertex_count,
3092 dst_buffer, declaration, flags, dst_fvf);
3094 if (declaration)
3095 FIXME("Output vertex declaration not implemented yet.\n");
3097 /* Need any context to write to the vbo. */
3098 context = context_acquire(device, NULL, 0);
3099 gl_info = context->gl_info;
3101 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3102 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3103 context_stream_info_from_declaration(context, state, &stream_info);
3104 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3106 /* We can't convert FROM a VBO, and vertex buffers used to source into
3107 * process_vertices() are unlikely to ever be used for drawing. Release
3108 * VBOs in those buffers and fix up the stream_info structure.
3110 * Also apply the start index. */
3111 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3113 struct wined3d_stream_info_element *e;
3114 struct wined3d_buffer *buffer;
3116 if (!(map & 1))
3117 continue;
3119 e = &stream_info.elements[i];
3120 buffer = state->streams[e->stream_idx].buffer;
3121 e->data.buffer_object = 0;
3122 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3123 if (buffer->buffer_object)
3125 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3126 buffer->buffer_object = 0;
3127 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
3129 if (e->data.addr)
3130 e->data.addr += e->stride * src_start_idx;
3133 hr = process_vertices_strided(device, dst_idx, vertex_count,
3134 &stream_info, dst_buffer, flags, dst_fvf);
3136 context_release(context);
3138 return hr;
3141 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3142 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3144 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3145 DWORD old_value;
3147 TRACE("device %p, stage %u, state %s, value %#x.\n",
3148 device, stage, debug_d3dtexturestate(state), value);
3150 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3152 WARN("Invalid state %#x passed.\n", state);
3153 return;
3156 if (stage >= d3d_info->limits.ffp_blend_stages)
3158 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3159 stage, d3d_info->limits.ffp_blend_stages - 1);
3160 return;
3163 old_value = device->update_state->texture_states[stage][state];
3164 device->update_state->texture_states[stage][state] = value;
3166 if (device->recording)
3168 TRACE("Recording... not performing anything.\n");
3169 device->recording->changed.textureState[stage] |= 1u << state;
3170 return;
3173 /* Checked after the assignments to allow proper stateblock recording. */
3174 if (old_value == value)
3176 TRACE("Application is setting the old value over, nothing to do.\n");
3177 return;
3180 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3183 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3184 UINT stage, enum wined3d_texture_stage_state state)
3186 TRACE("device %p, stage %u, state %s.\n",
3187 device, stage, debug_d3dtexturestate(state));
3189 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3191 WARN("Invalid state %#x passed.\n", state);
3192 return 0;
3195 return device->state.texture_states[stage][state];
3198 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3199 UINT stage, struct wined3d_texture *texture)
3201 struct wined3d_texture *prev;
3203 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3205 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3206 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3208 /* Windows accepts overflowing this array... we do not. */
3209 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3211 WARN("Ignoring invalid stage %u.\n", stage);
3212 return WINED3D_OK;
3215 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3217 WARN("Rejecting attempt to set scratch texture.\n");
3218 return WINED3DERR_INVALIDCALL;
3221 if (device->recording)
3222 device->recording->changed.textures |= 1u << stage;
3224 prev = device->update_state->textures[stage];
3225 TRACE("Previous texture %p.\n", prev);
3227 if (texture == prev)
3229 TRACE("App is setting the same texture again, nothing to do.\n");
3230 return WINED3D_OK;
3233 TRACE("Setting new texture to %p.\n", texture);
3234 device->update_state->textures[stage] = texture;
3236 if (texture)
3237 wined3d_texture_incref(texture);
3238 if (!device->recording)
3239 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3240 if (prev)
3241 wined3d_texture_decref(prev);
3243 return WINED3D_OK;
3246 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3248 TRACE("device %p, stage %u.\n", device, stage);
3250 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3251 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3253 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3255 WARN("Ignoring invalid stage %u.\n", stage);
3256 return NULL; /* Windows accepts overflowing this array ... we do not. */
3259 return device->state.textures[stage];
3262 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3264 TRACE("device %p, caps %p.\n", device, caps);
3266 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3267 device->create_parms.device_type, caps);
3270 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3271 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3273 struct wined3d_swapchain *swapchain;
3275 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3276 device, swapchain_idx, mode, rotation);
3278 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3279 return WINED3DERR_INVALIDCALL;
3281 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3284 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3286 struct wined3d_stateblock *stateblock;
3287 HRESULT hr;
3289 TRACE("device %p.\n", device);
3291 if (device->recording)
3292 return WINED3DERR_INVALIDCALL;
3294 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3295 if (FAILED(hr))
3296 return hr;
3298 device->recording = stateblock;
3299 device->update_state = &stateblock->state;
3301 TRACE("Recording stateblock %p.\n", stateblock);
3303 return WINED3D_OK;
3306 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3307 struct wined3d_stateblock **stateblock)
3309 struct wined3d_stateblock *object = device->recording;
3311 TRACE("device %p, stateblock %p.\n", device, stateblock);
3313 if (!device->recording)
3315 WARN("Not recording.\n");
3316 *stateblock = NULL;
3317 return WINED3DERR_INVALIDCALL;
3320 stateblock_init_contained_states(object);
3322 *stateblock = object;
3323 device->recording = NULL;
3324 device->update_state = &device->state;
3326 TRACE("Returning stateblock %p.\n", *stateblock);
3328 return WINED3D_OK;
3331 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3333 /* At the moment we have no need for any functionality at the beginning
3334 * of a scene. */
3335 TRACE("device %p.\n", device);
3337 if (device->inScene)
3339 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3340 return WINED3DERR_INVALIDCALL;
3342 device->inScene = TRUE;
3343 return WINED3D_OK;
3346 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3348 struct wined3d_context *context;
3350 TRACE("device %p.\n", device);
3352 if (!device->inScene)
3354 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3355 return WINED3DERR_INVALIDCALL;
3358 context = context_acquire(device, NULL, 0);
3359 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3360 context->gl_info->gl_ops.gl.p_glFlush();
3361 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3362 * fails. */
3363 context_release(context);
3365 device->inScene = FALSE;
3366 return WINED3D_OK;
3369 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3370 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3372 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3373 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3375 if (!rect_count && rects)
3377 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3378 return WINED3D_OK;
3381 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3383 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3384 if (!ds)
3386 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3387 /* TODO: What about depth stencil buffers without stencil bits? */
3388 return WINED3DERR_INVALIDCALL;
3390 else if (flags & WINED3DCLEAR_TARGET)
3392 if (ds->width < device->fb.render_targets[0]->width
3393 || ds->height < device->fb.render_targets[0]->height)
3395 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3396 return WINED3D_OK;
3401 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3403 return WINED3D_OK;
3406 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3407 struct wined3d_query *predicate, BOOL value)
3409 struct wined3d_query *prev;
3411 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3413 prev = device->update_state->predicate;
3414 if (predicate)
3416 FIXME("Predicated rendering not implemented.\n");
3417 wined3d_query_incref(predicate);
3419 device->update_state->predicate = predicate;
3420 device->update_state->predicate_value = value;
3421 if (!device->recording)
3422 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3423 if (prev)
3424 wined3d_query_decref(prev);
3427 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3429 TRACE("device %p, value %p.\n", device, value);
3431 *value = device->state.predicate_value;
3432 return device->state.predicate;
3435 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3436 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3438 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3439 device, group_count_x, group_count_y, group_count_z);
3441 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3444 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3445 enum wined3d_primitive_type primitive_type)
3447 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3449 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3452 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3453 enum wined3d_primitive_type *primitive_type)
3455 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3457 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3459 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3462 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3464 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3466 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3467 start_vertex, vertex_count, 0, 0, FALSE);
3469 return WINED3D_OK;
3472 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3473 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3475 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3476 device, start_vertex, vertex_count, start_instance, instance_count);
3478 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, 0,
3479 start_vertex, vertex_count, start_instance, instance_count, FALSE);
3482 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3484 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3486 if (!device->state.index_buffer)
3488 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3489 * without an index buffer set. (The first time at least...)
3490 * D3D8 simply dies, but I doubt it can do much harm to return
3491 * D3DERR_INVALIDCALL there as well. */
3492 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3493 return WINED3DERR_INVALIDCALL;
3496 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type,
3497 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3499 return WINED3D_OK;
3502 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3503 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3505 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3506 device, start_idx, index_count, start_instance, instance_count);
3508 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.base_vertex_index,
3509 start_idx, index_count, start_instance, instance_count, TRUE);
3512 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3513 struct wined3d_texture *src_texture, unsigned int src_level,
3514 struct wined3d_texture *dst_texture, unsigned int level_count)
3516 struct wined3d_const_bo_address data;
3517 struct wined3d_context *context;
3518 struct wined3d_map_desc src;
3519 HRESULT hr = WINED3D_OK;
3520 unsigned int i;
3522 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3523 device, src_texture, src_level, dst_texture, level_count);
3525 if (src_texture->resource.format != dst_texture->resource.format)
3527 WARN("Source and destination formats do not match.\n");
3528 return WINED3DERR_INVALIDCALL;
3531 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3532 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3533 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3535 WARN("Source and destination dimensions do not match.\n");
3536 return WINED3DERR_INVALIDCALL;
3539 context = context_acquire(device, NULL, 0);
3541 /* Only a prepare, since we're uploading entire volumes. */
3542 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3543 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3545 for (i = 0; i < level_count; ++i)
3547 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3548 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3549 goto done;
3551 data.buffer_object = 0;
3552 data.addr = src.data;
3553 wined3d_texture_upload_data(dst_texture, i, context, NULL, &data, src.row_pitch, src.slice_pitch);
3554 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3556 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3557 goto done;
3560 done:
3561 context_release(context);
3562 return hr;
3565 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3566 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3568 unsigned int src_size, dst_size, src_skip_levels = 0;
3569 unsigned int layer_count, level_count, i, j;
3570 enum wined3d_resource_type type;
3571 HRESULT hr;
3572 struct wined3d_context *context;
3574 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3576 /* Verify that the source and destination textures are non-NULL. */
3577 if (!src_texture || !dst_texture)
3579 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3580 return WINED3DERR_INVALIDCALL;
3583 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3585 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3586 return WINED3DERR_INVALIDCALL;
3588 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3590 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3591 return WINED3DERR_INVALIDCALL;
3594 /* Verify that the source and destination textures are the same type. */
3595 type = src_texture->resource.type;
3596 if (dst_texture->resource.type != type)
3598 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3599 return WINED3DERR_INVALIDCALL;
3602 layer_count = src_texture->layer_count;
3603 if (layer_count != dst_texture->layer_count)
3605 WARN("Source and destination have different layer counts.\n");
3606 return WINED3DERR_INVALIDCALL;
3609 level_count = min(wined3d_texture_get_level_count(src_texture),
3610 wined3d_texture_get_level_count(dst_texture));
3612 src_size = max(src_texture->resource.width, src_texture->resource.height);
3613 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3614 if (type == WINED3D_RTYPE_TEXTURE_3D)
3616 src_size = max(src_size, src_texture->resource.depth);
3617 dst_size = max(dst_size, dst_texture->resource.depth);
3619 while (src_size > dst_size)
3621 src_size >>= 1;
3622 ++src_skip_levels;
3625 /* Make sure that the destination texture is loaded. */
3626 context = context_acquire(device, NULL, 0);
3627 wined3d_texture_load(dst_texture, context, FALSE);
3628 context_release(context);
3630 /* Update every surface level of the texture. */
3631 switch (type)
3633 case WINED3D_RTYPE_TEXTURE_2D:
3635 unsigned int src_levels = src_texture->level_count;
3636 unsigned int dst_levels = dst_texture->level_count;
3637 struct wined3d_surface *src_surface;
3638 struct wined3d_surface *dst_surface;
3640 for (i = 0; i < layer_count; ++i)
3642 for (j = 0; j < level_count; ++j)
3644 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3645 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3646 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3648 WARN("Failed to update surface, hr %#x.\n", hr);
3649 return hr;
3653 return WINED3D_OK;
3656 case WINED3D_RTYPE_TEXTURE_3D:
3657 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3658 src_texture, src_skip_levels, dst_texture, level_count)))
3659 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3660 return hr;
3662 default:
3663 FIXME("Unsupported texture type %#x.\n", type);
3664 return WINED3DERR_INVALIDCALL;
3668 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3670 const struct wined3d_state *state = &device->state;
3671 struct wined3d_texture *texture;
3672 DWORD i;
3674 TRACE("device %p, num_passes %p.\n", device, num_passes);
3676 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3678 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3680 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3681 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3683 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3685 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3686 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3689 texture = state->textures[i];
3690 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3692 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3694 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3695 return E_FAIL;
3697 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3699 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3700 return E_FAIL;
3702 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3703 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3705 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3706 return E_FAIL;
3710 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3711 || state->render_states[WINED3D_RS_STENCILENABLE])
3713 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3714 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3716 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3718 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3719 return WINED3DERR_CONFLICTINGRENDERSTATE;
3723 /* return a sensible default */
3724 *num_passes = 1;
3726 TRACE("returning D3D_OK\n");
3727 return WINED3D_OK;
3730 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3732 static BOOL warned;
3734 TRACE("device %p, software %#x.\n", device, software);
3736 if (!warned)
3738 FIXME("device %p, software %#x stub!\n", device, software);
3739 warned = TRUE;
3742 device->softwareVertexProcessing = software;
3745 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3747 static BOOL warned;
3749 TRACE("device %p.\n", device);
3751 if (!warned)
3753 TRACE("device %p stub!\n", device);
3754 warned = TRUE;
3757 return device->softwareVertexProcessing;
3760 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3761 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3763 struct wined3d_swapchain *swapchain;
3765 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3766 device, swapchain_idx, raster_status);
3768 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3769 return WINED3DERR_INVALIDCALL;
3771 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3774 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3776 static BOOL warned;
3778 TRACE("device %p, segments %.8e.\n", device, segments);
3780 if (segments != 0.0f)
3782 if (!warned)
3784 FIXME("device %p, segments %.8e stub!\n", device, segments);
3785 warned = TRUE;
3789 return WINED3D_OK;
3792 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3794 static BOOL warned;
3796 TRACE("device %p.\n", device);
3798 if (!warned)
3800 FIXME("device %p stub!\n", device);
3801 warned = TRUE;
3804 return 0.0f;
3807 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3808 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3810 struct wined3d_texture *dst_texture, *src_texture;
3811 RECT dst_rect, src_rect;
3812 unsigned int i, j;
3813 HRESULT hr;
3815 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3817 if (src_resource == dst_resource)
3819 WARN("Source and destination are the same resource.\n");
3820 return;
3823 if (src_resource->type != dst_resource->type)
3825 WARN("Resource types (%s / %s) don't match.\n",
3826 debug_d3dresourcetype(dst_resource->type),
3827 debug_d3dresourcetype(src_resource->type));
3828 return;
3831 if (src_resource->width != dst_resource->width
3832 || src_resource->height != dst_resource->height
3833 || src_resource->depth != dst_resource->depth)
3835 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3836 dst_resource->width, dst_resource->height, dst_resource->depth,
3837 src_resource->width, src_resource->height, src_resource->depth);
3838 return;
3841 if (src_resource->format->id != dst_resource->format->id)
3843 WARN("Resource formats (%s / %s) don't match.\n",
3844 debug_d3dformat(dst_resource->format->id),
3845 debug_d3dformat(src_resource->format->id));
3846 return;
3849 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3851 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3852 buffer_from_resource(src_resource), 0,
3853 dst_resource->size)))
3854 ERR("Failed to copy buffer, hr %#x.\n", hr);
3855 return;
3858 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3860 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3861 return;
3864 dst_texture = texture_from_resource(dst_resource);
3865 src_texture = texture_from_resource(src_resource);
3867 if (src_texture->layer_count != dst_texture->layer_count
3868 || src_texture->level_count != dst_texture->level_count)
3870 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3871 dst_texture->layer_count, dst_texture->level_count,
3872 src_texture->layer_count, src_texture->level_count);
3873 return;
3876 for (i = 0; i < dst_texture->level_count; ++i)
3878 SetRect(&dst_rect, 0, 0,
3879 wined3d_texture_get_level_width(dst_texture, i),
3880 wined3d_texture_get_level_height(dst_texture, i));
3881 SetRect(&src_rect, 0, 0,
3882 wined3d_texture_get_level_width(src_texture, i),
3883 wined3d_texture_get_level_height(dst_texture, i));
3884 for (j = 0; j < dst_texture->layer_count; ++j)
3886 unsigned int idx = j * dst_texture->level_count + i;
3888 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3889 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3890 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3895 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3896 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3897 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3898 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3900 struct wined3d_texture *dst_texture, *src_texture;
3901 RECT dst_rect, src_rect;
3902 HRESULT hr;
3904 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3905 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3906 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3907 src_resource, src_sub_resource_idx, debug_box(src_box));
3909 if (src_box && (src_box->left >= src_box->right
3910 || src_box->top >= src_box->bottom
3911 || src_box->front >= src_box->back))
3913 WARN("Invalid box %s specified.\n", debug_box(src_box));
3914 return WINED3DERR_INVALIDCALL;
3917 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3919 WARN("Source and destination are the same sub-resource.\n");
3920 return WINED3DERR_INVALIDCALL;
3923 if (src_resource->type != dst_resource->type)
3925 WARN("Resource types (%s / %s) don't match.\n",
3926 debug_d3dresourcetype(dst_resource->type),
3927 debug_d3dresourcetype(src_resource->type));
3928 return WINED3DERR_INVALIDCALL;
3931 if (src_resource->format->id != dst_resource->format->id)
3933 WARN("Resource formats (%s / %s) don't match.\n",
3934 debug_d3dformat(dst_resource->format->id),
3935 debug_d3dformat(src_resource->format->id));
3936 return WINED3DERR_INVALIDCALL;
3939 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3941 unsigned int src_offset, size;
3943 if (dst_sub_resource_idx)
3945 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3946 return WINED3DERR_INVALIDCALL;
3948 if (src_sub_resource_idx)
3950 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3951 return WINED3DERR_INVALIDCALL;
3954 if (src_box)
3956 src_offset = src_box->left;
3957 size = src_box->right - src_box->left;
3959 else
3961 src_offset = 0;
3962 size = src_resource->size;
3965 if (src_offset > src_resource->size
3966 || size > src_resource->size - src_offset
3967 || dst_x > dst_resource->size
3968 || size > dst_resource->size - dst_x)
3970 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
3971 dst_x, src_offset, size);
3972 return WINED3DERR_INVALIDCALL;
3975 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
3976 buffer_from_resource(src_resource), src_offset, size);
3979 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3981 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3982 return WINED3DERR_INVALIDCALL;
3985 dst_texture = texture_from_resource(dst_resource);
3986 src_texture = texture_from_resource(src_resource);
3988 if (src_box)
3990 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
3992 else
3994 unsigned int level = src_sub_resource_idx % src_texture->level_count;
3996 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
3997 wined3d_texture_get_level_height(src_texture, level));
4000 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4001 dst_y + (src_rect.bottom - src_rect.top));
4003 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4004 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4005 WARN("Failed to blit, hr %#x.\n", hr);
4007 return hr;
4010 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4011 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4012 unsigned int depth_pitch)
4014 unsigned int width, height, depth, level;
4015 struct wined3d_const_bo_address addr;
4016 struct wined3d_context *context;
4017 struct wined3d_texture *texture;
4019 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4020 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4022 if (resource->type == WINED3D_RTYPE_BUFFER)
4024 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4026 if (sub_resource_idx > 0)
4028 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4029 return;
4032 context = context_acquire(resource->device, NULL, 0);
4033 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
4035 ERR("Failed to load buffer location.\n");
4036 context_release(context);
4037 return;
4040 wined3d_buffer_upload_data(buffer, context, box, data);
4041 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
4042 context_release(context);
4044 return;
4047 if (resource->type != WINED3D_RTYPE_TEXTURE_2D && resource->type != WINED3D_RTYPE_TEXTURE_3D)
4049 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4050 return;
4053 texture = texture_from_resource(resource);
4054 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4056 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4057 return;
4060 level = sub_resource_idx % texture->level_count;
4061 width = wined3d_texture_get_level_width(texture, level);
4062 height = wined3d_texture_get_level_height(texture, level);
4063 depth = wined3d_texture_get_level_depth(texture, level);
4065 if (box && (box->left >= box->right || box->right > width
4066 || box->top >= box->bottom || box->bottom > height
4067 || box->front >= box->back || box->back > depth))
4069 WARN("Invalid box %s specified.\n", debug_box(box));
4070 return;
4073 addr.buffer_object = 0;
4074 addr.addr = data;
4076 context = context_acquire(resource->device, NULL, 0);
4078 /* Only load the sub-resource for partial updates. */
4079 if (!box || (!box->left && !box->top && !box->front
4080 && box->right == width && box->bottom == height && box->back == depth))
4081 wined3d_texture_prepare_texture(texture, context, FALSE);
4082 else
4083 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4084 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4086 wined3d_texture_upload_data(texture, sub_resource_idx, context, box, &addr, row_pitch, depth_pitch);
4088 context_release(context);
4090 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4091 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4094 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4095 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4096 const struct wined3d_color *color, float depth, DWORD stencil)
4098 const struct blit_shader *blitter;
4099 struct wined3d_resource *resource;
4100 enum wined3d_blit_op blit_op;
4101 RECT r;
4103 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4104 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4106 if (!flags)
4107 return WINED3D_OK;
4109 resource = view->resource;
4110 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4112 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4113 return WINED3DERR_INVALIDCALL;
4116 if (view->depth > 1)
4118 FIXME("Layered clears not implemented.\n");
4119 return WINED3DERR_INVALIDCALL;
4122 if (!rect)
4124 SetRect(&r, 0, 0, view->width, view->height);
4125 rect = &r;
4128 if (flags & WINED3DCLEAR_TARGET)
4129 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4130 else
4131 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4133 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4134 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4136 FIXME("No blitter is capable of performing the requested fill operation.\n");
4137 return WINED3DERR_INVALIDCALL;
4140 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4141 return blitter->color_fill(device, view, rect, color);
4142 else
4143 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4146 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4147 unsigned int view_idx)
4149 TRACE("device %p, view_idx %u.\n", device, view_idx);
4151 if (view_idx >= device->adapter->gl_info.limits.buffers)
4153 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4154 return NULL;
4157 return device->fb.render_targets[view_idx];
4160 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4162 TRACE("device %p.\n", device);
4164 return device->fb.depth_stencil;
4167 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4168 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4170 struct wined3d_rendertarget_view *prev;
4172 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4173 device, view_idx, view, set_viewport);
4175 if (view_idx >= device->adapter->gl_info.limits.buffers)
4177 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4178 return WINED3DERR_INVALIDCALL;
4181 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4183 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4184 return WINED3DERR_INVALIDCALL;
4187 /* Set the viewport and scissor rectangles, if requested. Tests show that
4188 * stateblock recording is ignored, the change goes directly into the
4189 * primary stateblock. */
4190 if (!view_idx && set_viewport)
4192 struct wined3d_state *state = &device->state;
4194 state->viewport.x = 0;
4195 state->viewport.y = 0;
4196 state->viewport.width = view->width;
4197 state->viewport.height = view->height;
4198 state->viewport.min_z = 0.0f;
4199 state->viewport.max_z = 1.0f;
4200 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4202 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4203 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4207 prev = device->fb.render_targets[view_idx];
4208 if (view == prev)
4209 return WINED3D_OK;
4211 if (view)
4212 wined3d_rendertarget_view_incref(view);
4213 device->fb.render_targets[view_idx] = view;
4214 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4215 /* Release after the assignment, to prevent device_resource_released()
4216 * from seeing the surface as still in use. */
4217 if (prev)
4218 wined3d_rendertarget_view_decref(prev);
4220 return WINED3D_OK;
4223 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4225 struct wined3d_rendertarget_view *prev;
4227 TRACE("device %p, view %p.\n", device, view);
4229 prev = device->fb.depth_stencil;
4230 if (prev == view)
4232 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4233 return;
4236 if ((device->fb.depth_stencil = view))
4237 wined3d_rendertarget_view_incref(view);
4238 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4239 if (prev)
4240 wined3d_rendertarget_view_decref(prev);
4243 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4244 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4246 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4247 struct wined3d_sub_resource_data data;
4248 struct wined3d_resource_desc desc;
4249 struct wined3d_map_desc map_desc;
4250 struct wined3d_texture *texture;
4251 HRESULT hr;
4253 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4255 ERR("Failed to map source texture.\n");
4256 return NULL;
4259 data.data = map_desc.data;
4260 data.row_pitch = map_desc.row_pitch;
4261 data.slice_pitch = map_desc.slice_pitch;
4263 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4264 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4265 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4266 desc.multisample_quality = 0;
4267 desc.usage = WINED3DUSAGE_DYNAMIC;
4268 desc.pool = WINED3D_POOL_DEFAULT;
4269 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4270 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4271 desc.depth = 1;
4272 desc.size = 0;
4274 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4275 &data, NULL, &wined3d_null_parent_ops, &texture);
4276 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4277 if (FAILED(hr))
4279 ERR("Failed to create cursor texture.\n");
4280 return NULL;
4283 return texture;
4286 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4287 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4289 unsigned int texture_level = sub_resource_idx % texture->level_count;
4290 unsigned int cursor_width, cursor_height;
4291 struct wined3d_display_mode mode;
4292 struct wined3d_map_desc map_desc;
4293 HRESULT hr;
4295 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4296 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4298 if (sub_resource_idx >= texture->level_count * texture->layer_count
4299 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4300 return WINED3DERR_INVALIDCALL;
4302 if (device->cursor_texture)
4304 wined3d_texture_decref(device->cursor_texture);
4305 device->cursor_texture = NULL;
4308 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4310 WARN("Texture %p has invalid format %s.\n",
4311 texture, debug_d3dformat(texture->resource.format->id));
4312 return WINED3DERR_INVALIDCALL;
4315 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4317 ERR("Failed to get display mode, hr %#x.\n", hr);
4318 return WINED3DERR_INVALIDCALL;
4321 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4322 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4323 if (cursor_width > mode.width || cursor_height > mode.height)
4325 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4326 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4327 return WINED3DERR_INVALIDCALL;
4330 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4332 /* Do not store the surface's pointer because the application may
4333 * release it after setting the cursor image. Windows doesn't
4334 * addref the set surface, so we can't do this either without
4335 * creating circular refcount dependencies. */
4336 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4338 ERR("Failed to create cursor texture.\n");
4339 return WINED3DERR_INVALIDCALL;
4342 if (cursor_width == 32 && cursor_height == 32)
4344 UINT mask_size = cursor_width * cursor_height / 8;
4345 ICONINFO cursor_info;
4346 DWORD *mask_bits;
4347 HCURSOR cursor;
4349 /* 32-bit user32 cursors ignore the alpha channel if it's all
4350 * zeroes, and use the mask instead. Fill the mask with all ones
4351 * to ensure we still get a fully transparent cursor. */
4352 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4353 return E_OUTOFMEMORY;
4354 memset(mask_bits, 0xff, mask_size);
4356 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4357 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4358 cursor_info.fIcon = FALSE;
4359 cursor_info.xHotspot = x_hotspot;
4360 cursor_info.yHotspot = y_hotspot;
4361 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4362 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4363 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4365 /* Create our cursor and clean up. */
4366 cursor = CreateIconIndirect(&cursor_info);
4367 if (cursor_info.hbmMask)
4368 DeleteObject(cursor_info.hbmMask);
4369 if (cursor_info.hbmColor)
4370 DeleteObject(cursor_info.hbmColor);
4371 if (device->hardwareCursor)
4372 DestroyCursor(device->hardwareCursor);
4373 device->hardwareCursor = cursor;
4374 if (device->bCursorVisible)
4375 SetCursor(cursor);
4377 HeapFree(GetProcessHeap(), 0, mask_bits);
4380 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4381 device->cursorWidth = cursor_width;
4382 device->cursorHeight = cursor_height;
4383 device->xHotSpot = x_hotspot;
4384 device->yHotSpot = y_hotspot;
4386 return WINED3D_OK;
4389 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4390 int x_screen_space, int y_screen_space, DWORD flags)
4392 TRACE("device %p, x %d, y %d, flags %#x.\n",
4393 device, x_screen_space, y_screen_space, flags);
4395 device->xScreenSpace = x_screen_space;
4396 device->yScreenSpace = y_screen_space;
4398 if (device->hardwareCursor)
4400 POINT pt;
4402 GetCursorPos( &pt );
4403 if (x_screen_space == pt.x && y_screen_space == pt.y)
4404 return;
4405 SetCursorPos( x_screen_space, y_screen_space );
4407 /* Switch to the software cursor if position diverges from the hardware one. */
4408 GetCursorPos( &pt );
4409 if (x_screen_space != pt.x || y_screen_space != pt.y)
4411 if (device->bCursorVisible) SetCursor( NULL );
4412 DestroyCursor( device->hardwareCursor );
4413 device->hardwareCursor = 0;
4418 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4420 BOOL oldVisible = device->bCursorVisible;
4422 TRACE("device %p, show %#x.\n", device, show);
4425 * When ShowCursor is first called it should make the cursor appear at the OS's last
4426 * known cursor position.
4428 if (show && !oldVisible)
4430 POINT pt;
4431 GetCursorPos(&pt);
4432 device->xScreenSpace = pt.x;
4433 device->yScreenSpace = pt.y;
4436 if (device->hardwareCursor)
4438 device->bCursorVisible = show;
4439 if (show)
4440 SetCursor(device->hardwareCursor);
4441 else
4442 SetCursor(NULL);
4444 else if (device->cursor_texture)
4446 device->bCursorVisible = show;
4449 return oldVisible;
4452 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4454 struct wined3d_resource *resource, *cursor;
4456 TRACE("device %p.\n", device);
4458 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4460 TRACE("Checking resource %p for eviction.\n", resource);
4462 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4464 TRACE("Evicting %p.\n", resource);
4465 wined3d_cs_emit_unload_resource(device->cs, resource);
4470 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4471 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4472 wined3d_device_reset_cb callback, BOOL reset_state)
4474 struct wined3d_resource *resource, *cursor;
4475 struct wined3d_swapchain *swapchain;
4476 struct wined3d_view_desc view_desc;
4477 BOOL backbuffer_resized;
4478 HRESULT hr = WINED3D_OK;
4479 unsigned int i;
4481 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4482 device, swapchain_desc, mode, callback, reset_state);
4484 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4486 ERR("Failed to get the first implicit swapchain.\n");
4487 return WINED3DERR_INVALIDCALL;
4490 if (reset_state)
4492 if (device->logo_texture)
4494 wined3d_texture_decref(device->logo_texture);
4495 device->logo_texture = NULL;
4497 if (device->cursor_texture)
4499 wined3d_texture_decref(device->cursor_texture);
4500 device->cursor_texture = NULL;
4502 state_unbind_resources(&device->state);
4505 if (device->fb.render_targets)
4507 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4509 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4512 wined3d_device_set_depth_stencil_view(device, NULL);
4514 if (reset_state)
4516 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4518 TRACE("Enumerating resource %p.\n", resource);
4519 if (FAILED(hr = callback(resource)))
4520 return hr;
4524 TRACE("New params:\n");
4525 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4526 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4527 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4528 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4529 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4530 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4531 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4532 TRACE("device_window %p\n", swapchain_desc->device_window);
4533 TRACE("windowed %#x\n", swapchain_desc->windowed);
4534 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4535 if (swapchain_desc->enable_auto_depth_stencil)
4536 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4537 TRACE("flags %#x\n", swapchain_desc->flags);
4538 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4539 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4540 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4542 /* No special treatment of these parameters. Just store them */
4543 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4544 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4545 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4546 swapchain->desc.flags = swapchain_desc->flags;
4547 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4548 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4549 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4551 if (swapchain_desc->device_window
4552 && swapchain_desc->device_window != swapchain->desc.device_window)
4554 TRACE("Changing the device window from %p to %p.\n",
4555 swapchain->desc.device_window, swapchain_desc->device_window);
4556 swapchain->desc.device_window = swapchain_desc->device_window;
4557 swapchain->device_window = swapchain_desc->device_window;
4558 wined3d_swapchain_set_window(swapchain, NULL);
4561 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4562 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4564 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4565 || swapchain->reapply_mode || mode
4566 || (!swapchain_desc->windowed && backbuffer_resized))
4568 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4569 return hr;
4571 else if (!swapchain_desc->windowed)
4573 DWORD style = device->style;
4574 DWORD exStyle = device->exStyle;
4575 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4576 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4577 * Reset to clear up their mess. Guild Wars also loses the device during that.
4579 device->style = 0;
4580 device->exStyle = 0;
4581 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4582 swapchain_desc->backbuffer_width,
4583 swapchain_desc->backbuffer_height);
4584 device->style = style;
4585 device->exStyle = exStyle;
4588 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4589 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4590 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4591 return hr;
4593 if (device->auto_depth_stencil_view)
4595 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4596 device->auto_depth_stencil_view = NULL;
4598 if (swapchain->desc.enable_auto_depth_stencil)
4600 struct wined3d_resource_desc texture_desc;
4601 struct wined3d_texture *texture;
4602 DWORD flags = 0;
4604 TRACE("Creating the depth stencil buffer.\n");
4606 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4607 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4608 texture_desc.multisample_type = swapchain->desc.multisample_type;
4609 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4610 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4611 texture_desc.pool = WINED3D_POOL_DEFAULT;
4612 texture_desc.width = swapchain->desc.backbuffer_width;
4613 texture_desc.height = swapchain->desc.backbuffer_height;
4614 texture_desc.depth = 1;
4615 texture_desc.size = 0;
4617 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4618 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4620 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4621 device->device_parent, &texture_desc, flags, &texture)))
4623 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4624 return WINED3DERR_INVALIDCALL;
4627 view_desc.format_id = texture->resource.format->id;
4628 view_desc.flags = 0;
4629 view_desc.u.texture.level_idx = 0;
4630 view_desc.u.texture.level_count = 1;
4631 view_desc.u.texture.layer_idx = 0;
4632 view_desc.u.texture.layer_count = 1;
4633 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4634 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4635 wined3d_texture_decref(texture);
4636 if (FAILED(hr))
4638 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4639 return hr;
4642 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4645 if (device->back_buffer_view)
4647 wined3d_rendertarget_view_decref(device->back_buffer_view);
4648 device->back_buffer_view = NULL;
4650 if (swapchain->desc.backbuffer_count)
4652 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4654 view_desc.format_id = back_buffer->format->id;
4655 view_desc.flags = 0;
4656 view_desc.u.texture.level_idx = 0;
4657 view_desc.u.texture.level_count = 1;
4658 view_desc.u.texture.layer_idx = 0;
4659 view_desc.u.texture.layer_count = 1;
4660 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4661 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4663 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4664 return hr;
4668 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4670 if (reset_state)
4672 TRACE("Resetting stateblock.\n");
4673 if (device->recording)
4675 wined3d_stateblock_decref(device->recording);
4676 device->recording = NULL;
4678 wined3d_cs_emit_reset_state(device->cs);
4679 state_cleanup(&device->state);
4681 if (device->d3d_initialized)
4682 wined3d_device_delete_opengl_contexts(device);
4684 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4685 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4686 device->update_state = &device->state;
4688 device_init_swapchain_state(device, swapchain);
4690 else if (device->back_buffer_view)
4692 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4693 struct wined3d_state *state = &device->state;
4695 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4697 /* Note the min_z / max_z is not reset. */
4698 state->viewport.x = 0;
4699 state->viewport.y = 0;
4700 state->viewport.width = view->width;
4701 state->viewport.height = view->height;
4702 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4704 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4705 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4708 if (device->d3d_initialized)
4710 if (reset_state)
4711 hr = wined3d_device_create_primary_opengl_context(device);
4712 swapchain_update_swap_interval(swapchain);
4715 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4716 * first use
4718 return hr;
4721 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4723 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4725 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4727 return WINED3D_OK;
4731 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4732 struct wined3d_device_creation_parameters *parameters)
4734 TRACE("device %p, parameters %p.\n", device, parameters);
4736 *parameters = device->create_parms;
4739 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4741 TRACE("device %p.\n", device);
4743 return device->wined3d;
4746 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4747 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4749 struct wined3d_swapchain *swapchain;
4751 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4752 device, swapchain_idx, flags, ramp);
4754 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4755 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4758 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4759 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4761 struct wined3d_swapchain *swapchain;
4763 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4764 device, swapchain_idx, ramp);
4766 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4767 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4770 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4772 TRACE("device %p, resource %p.\n", device, resource);
4774 list_add_head(&device->resources, &resource->resource_list_entry);
4777 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4779 TRACE("device %p, resource %p.\n", device, resource);
4781 list_remove(&resource->resource_list_entry);
4784 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4786 enum wined3d_resource_type type = resource->type;
4787 struct wined3d_rendertarget_view *rtv;
4788 unsigned int i;
4790 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4792 if (device->d3d_initialized)
4794 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4796 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4797 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4800 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4801 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4804 switch (type)
4806 case WINED3D_RTYPE_TEXTURE_2D:
4807 case WINED3D_RTYPE_TEXTURE_3D:
4808 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4810 struct wined3d_texture *texture = texture_from_resource(resource);
4812 if (device->state.textures[i] == texture)
4814 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4815 device->state.textures[i] = NULL;
4818 if (device->recording && device->update_state->textures[i] == texture)
4820 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4821 texture, device->recording, i);
4822 device->update_state->textures[i] = NULL;
4825 break;
4827 case WINED3D_RTYPE_BUFFER:
4829 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4831 for (i = 0; i < MAX_STREAMS; ++i)
4833 if (device->state.streams[i].buffer == buffer)
4835 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4836 device->state.streams[i].buffer = NULL;
4839 if (device->recording && device->update_state->streams[i].buffer == buffer)
4841 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4842 buffer, device->recording, i);
4843 device->update_state->streams[i].buffer = NULL;
4847 if (device->state.index_buffer == buffer)
4849 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4850 device->state.index_buffer = NULL;
4853 if (device->recording && device->update_state->index_buffer == buffer)
4855 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4856 buffer, device->recording);
4857 device->update_state->index_buffer = NULL;
4860 break;
4862 default:
4863 break;
4866 /* Remove the resource from the resourceStore */
4867 device_resource_remove(device, resource);
4869 TRACE("Resource released.\n");
4872 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4874 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4876 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4879 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4880 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4881 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4883 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4884 const struct fragment_pipeline *fragment_pipeline;
4885 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4886 unsigned int i;
4887 HRESULT hr;
4889 device->ref = 1;
4890 device->wined3d = wined3d;
4891 wined3d_incref(device->wined3d);
4892 device->adapter = wined3d->adapter_count ? adapter : NULL;
4893 device->device_parent = device_parent;
4894 list_init(&device->resources);
4895 list_init(&device->shaders);
4896 device->surface_alignment = surface_alignment;
4898 /* Save the creation parameters. */
4899 device->create_parms.adapter_idx = adapter_idx;
4900 device->create_parms.device_type = device_type;
4901 device->create_parms.focus_window = focus_window;
4902 device->create_parms.flags = flags;
4904 device->shader_backend = adapter->shader_backend;
4906 vertex_pipeline = adapter->vertex_pipe;
4908 fragment_pipeline = adapter->fragment_pipe;
4910 wine_rb_init(&device->samplers, wined3d_sampler_compare);
4912 if (vertex_pipeline->vp_states && fragment_pipeline->states
4913 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4914 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4915 fragment_pipeline, misc_state_template)))
4917 ERR("Failed to compile state table, hr %#x.\n", hr);
4918 wine_rb_destroy(&device->samplers, NULL, NULL);
4919 wined3d_decref(device->wined3d);
4920 return hr;
4923 device->blitter = adapter->blitter;
4925 state_init(&device->state, &device->fb, &adapter->gl_info,
4926 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4927 device->update_state = &device->state;
4929 if (!(device->cs = wined3d_cs_create(device)))
4931 WARN("Failed to create command stream.\n");
4932 state_cleanup(&device->state);
4933 hr = E_FAIL;
4934 goto err;
4937 return WINED3D_OK;
4939 err:
4940 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4942 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4944 wine_rb_destroy(&device->samplers, NULL, NULL);
4945 wined3d_decref(device->wined3d);
4946 return hr;
4949 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4951 DWORD rep = device->StateTable[state].representative;
4952 struct wined3d_context *context;
4953 DWORD idx;
4954 BYTE shift;
4955 UINT i;
4957 if (STATE_IS_COMPUTE(state))
4959 for (i = 0; i < device->context_count; ++i)
4960 context_invalidate_compute_state(device->contexts[i], state);
4961 return;
4964 for (i = 0; i < device->context_count; ++i)
4966 context = device->contexts[i];
4967 if(isStateDirty(context, rep)) continue;
4969 context->dirtyArray[context->numDirtyEntries++] = rep;
4970 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4971 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4972 context->isStateDirty[idx] |= (1u << shift);
4976 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4977 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4979 if (device->filter_messages && message != WM_DISPLAYCHANGE)
4981 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4982 window, message, wparam, lparam);
4983 if (unicode)
4984 return DefWindowProcW(window, message, wparam, lparam);
4985 else
4986 return DefWindowProcA(window, message, wparam, lparam);
4989 if (message == WM_DESTROY)
4991 TRACE("unregister window %p.\n", window);
4992 wined3d_unregister_window(window);
4994 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
4995 ERR("Window %p is not the focus window for device %p.\n", window, device);
4997 else if (message == WM_DISPLAYCHANGE)
4999 device->device_parent->ops->mode_changed(device->device_parent);
5001 else if (message == WM_ACTIVATEAPP)
5003 UINT i;
5005 for (i = 0; i < device->swapchain_count; i++)
5006 wined3d_swapchain_activate(device->swapchains[i], wparam);
5008 device->device_parent->ops->activate(device->device_parent, wparam);
5010 else if (message == WM_SYSCOMMAND)
5012 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5014 if (unicode)
5015 DefWindowProcW(window, message, wparam, lparam);
5016 else
5017 DefWindowProcA(window, message, wparam, lparam);
5021 if (unicode)
5022 return CallWindowProcW(proc, window, message, wparam, lparam);
5023 else
5024 return CallWindowProcA(proc, window, message, wparam, lparam);