wined3d: Send primary GL context initialisation through the command stream.
[wine.git] / dlls / wined3d / device.c
blob8192bf96c6d7f6cee8466c4016d17ecaafd028e1
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 struct list *e;
1517 float rho;
1519 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1521 /* Check the parameter range. Need for speed most wanted sets junk lights
1522 * which confuse the GL driver. */
1523 if (!light)
1524 return WINED3DERR_INVALIDCALL;
1526 switch (light->type)
1528 case WINED3D_LIGHT_POINT:
1529 case WINED3D_LIGHT_SPOT:
1530 case WINED3D_LIGHT_GLSPOT:
1531 /* Incorrect attenuation values can cause the gl driver to crash.
1532 * Happens with Need for speed most wanted. */
1533 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1535 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1536 return WINED3DERR_INVALIDCALL;
1538 break;
1540 case WINED3D_LIGHT_DIRECTIONAL:
1541 case WINED3D_LIGHT_PARALLELPOINT:
1542 /* Ignores attenuation */
1543 break;
1545 default:
1546 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1547 return WINED3DERR_INVALIDCALL;
1550 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1552 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1553 if (object->OriginalIndex == light_idx)
1554 break;
1555 object = NULL;
1558 if (!object)
1560 TRACE("Adding new light\n");
1561 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1562 if (!object)
1563 return E_OUTOFMEMORY;
1565 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1566 object->glIndex = -1;
1567 object->OriginalIndex = light_idx;
1570 /* Initialize the object. */
1571 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1572 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1573 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1574 light_idx, light->type, debug_color(&light->diffuse),
1575 debug_color(&light->specular), debug_color(&light->ambient),
1576 light->position.x, light->position.y, light->position.z,
1577 light->direction.x, light->direction.y, light->direction.z,
1578 light->range, light->falloff, light->theta, light->phi);
1580 /* Update the live definitions if the light is currently assigned a glIndex. */
1581 if (object->glIndex != -1 && !device->recording)
1583 if (object->OriginalParms.type != light->type)
1584 device_invalidate_state(device, STATE_LIGHT_TYPE);
1585 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1588 /* Save away the information. */
1589 object->OriginalParms = *light;
1591 switch (light->type)
1593 case WINED3D_LIGHT_POINT:
1594 /* Position */
1595 object->position.x = light->position.x;
1596 object->position.y = light->position.y;
1597 object->position.z = light->position.z;
1598 object->position.w = 1.0f;
1599 object->cutoff = 180.0f;
1600 /* FIXME: Range */
1601 break;
1603 case WINED3D_LIGHT_DIRECTIONAL:
1604 /* Direction */
1605 object->direction.x = -light->direction.x;
1606 object->direction.y = -light->direction.y;
1607 object->direction.z = -light->direction.z;
1608 object->direction.w = 0.0f;
1609 object->exponent = 0.0f;
1610 object->cutoff = 180.0f;
1611 break;
1613 case WINED3D_LIGHT_SPOT:
1614 /* Position */
1615 object->position.x = light->position.x;
1616 object->position.y = light->position.y;
1617 object->position.z = light->position.z;
1618 object->position.w = 1.0f;
1620 /* Direction */
1621 object->direction.x = light->direction.x;
1622 object->direction.y = light->direction.y;
1623 object->direction.z = light->direction.z;
1624 object->direction.w = 0.0f;
1626 /* opengl-ish and d3d-ish spot lights use too different models
1627 * for the light "intensity" as a function of the angle towards
1628 * the main light direction, so we only can approximate very
1629 * roughly. However, spot lights are rather rarely used in games
1630 * (if ever used at all). Furthermore if still used, probably
1631 * nobody pays attention to such details. */
1632 if (!light->falloff)
1634 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1635 * equations have the falloff resp. exponent parameter as an
1636 * exponent, so the spot light lighting will always be 1.0 for
1637 * both of them, and we don't have to care for the rest of the
1638 * rather complex calculation. */
1639 object->exponent = 0.0f;
1641 else
1643 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1644 if (rho < 0.0001f)
1645 rho = 0.0001f;
1646 object->exponent = -0.3f / logf(cosf(rho / 2));
1649 if (object->exponent > 128.0f)
1650 object->exponent = 128.0f;
1652 object->cutoff = (float)(light->phi * 90 / M_PI);
1653 /* FIXME: Range */
1654 break;
1656 case WINED3D_LIGHT_PARALLELPOINT:
1657 object->position.x = light->position.x;
1658 object->position.y = light->position.y;
1659 object->position.z = light->position.z;
1660 object->position.w = 1.0f;
1661 break;
1663 default:
1664 FIXME("Unrecognized light type %#x.\n", light->type);
1667 return WINED3D_OK;
1670 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1671 UINT light_idx, struct wined3d_light *light)
1673 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1674 struct wined3d_light_info *light_info = NULL;
1675 struct list *e;
1677 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1679 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1681 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1682 if (light_info->OriginalIndex == light_idx)
1683 break;
1684 light_info = NULL;
1687 if (!light_info)
1689 TRACE("Light information requested but light not defined\n");
1690 return WINED3DERR_INVALIDCALL;
1693 *light = light_info->OriginalParms;
1694 return WINED3D_OK;
1697 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1699 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1700 struct wined3d_light_info *light_info = NULL;
1701 struct list *e;
1703 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1705 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1707 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1708 if (light_info->OriginalIndex == light_idx)
1709 break;
1710 light_info = NULL;
1712 TRACE("Found light %p.\n", light_info);
1714 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1715 if (!light_info)
1717 TRACE("Light enabled requested but light not defined, so defining one!\n");
1718 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1720 /* Search for it again! Should be fairly quick as near head of list. */
1721 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1723 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1724 if (light_info->OriginalIndex == light_idx)
1725 break;
1726 light_info = NULL;
1728 if (!light_info)
1730 FIXME("Adding default lights has failed dismally\n");
1731 return WINED3DERR_INVALIDCALL;
1735 if (!enable)
1737 if (light_info->glIndex != -1)
1739 if (!device->recording)
1741 device_invalidate_state(device, STATE_LIGHT_TYPE);
1742 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1745 device->update_state->lights[light_info->glIndex] = NULL;
1746 light_info->glIndex = -1;
1748 else
1750 TRACE("Light already disabled, nothing to do\n");
1752 light_info->enabled = FALSE;
1754 else
1756 light_info->enabled = TRUE;
1757 if (light_info->glIndex != -1)
1759 TRACE("Nothing to do as light was enabled\n");
1761 else
1763 unsigned int light_count = device->adapter->d3d_info.limits.active_light_count;
1764 unsigned int i;
1766 /* Find a free light. */
1767 for (i = 0; i < light_count; ++i)
1769 if (!device->update_state->lights[i])
1771 device->update_state->lights[i] = light_info;
1772 light_info->glIndex = i;
1773 break;
1776 if (light_info->glIndex == -1)
1778 /* Our tests show that Windows returns D3D_OK in this situation, even with
1779 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1780 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1781 * as well for those lights.
1783 * TODO: Test how this affects rendering. */
1784 WARN("Too many concurrently active lights\n");
1785 return WINED3D_OK;
1788 /* i == light_info->glIndex */
1789 if (!device->recording)
1791 device_invalidate_state(device, STATE_LIGHT_TYPE);
1792 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1797 return WINED3D_OK;
1800 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1802 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1803 struct wined3d_light_info *light_info = NULL;
1804 struct list *e;
1806 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1808 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1810 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1811 if (light_info->OriginalIndex == light_idx)
1812 break;
1813 light_info = NULL;
1816 if (!light_info)
1818 TRACE("Light enabled state requested but light not defined.\n");
1819 return WINED3DERR_INVALIDCALL;
1821 /* true is 128 according to SetLightEnable */
1822 *enable = light_info->enabled ? 128 : 0;
1823 return WINED3D_OK;
1826 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1827 UINT plane_idx, const struct wined3d_vec4 *plane)
1829 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1831 /* Validate plane_idx. */
1832 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1834 TRACE("Application has requested clipplane this device doesn't support.\n");
1835 return WINED3DERR_INVALIDCALL;
1838 if (device->recording)
1839 device->recording->changed.clipplane |= 1u << plane_idx;
1841 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1843 TRACE("Application is setting old values over, nothing to do.\n");
1844 return WINED3D_OK;
1847 device->update_state->clip_planes[plane_idx] = *plane;
1849 if (!device->recording)
1850 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1852 return WINED3D_OK;
1855 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1856 UINT plane_idx, struct wined3d_vec4 *plane)
1858 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1860 /* Validate plane_idx. */
1861 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1863 TRACE("Application has requested clipplane this device doesn't support.\n");
1864 return WINED3DERR_INVALIDCALL;
1867 *plane = device->state.clip_planes[plane_idx];
1869 return WINED3D_OK;
1872 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1873 const struct wined3d_clip_status *clip_status)
1875 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1877 if (!clip_status)
1878 return WINED3DERR_INVALIDCALL;
1880 return WINED3D_OK;
1883 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1884 struct wined3d_clip_status *clip_status)
1886 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1888 if (!clip_status)
1889 return WINED3DERR_INVALIDCALL;
1891 return WINED3D_OK;
1894 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1896 TRACE("device %p, material %p.\n", device, material);
1898 device->update_state->material = *material;
1900 if (device->recording)
1901 device->recording->changed.material = TRUE;
1902 else
1903 wined3d_cs_emit_set_material(device->cs, material);
1906 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1908 TRACE("device %p, material %p.\n", device, material);
1910 *material = device->state.material;
1912 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1913 TRACE("ambient %s\n", debug_color(&material->ambient));
1914 TRACE("specular %s\n", debug_color(&material->specular));
1915 TRACE("emissive %s\n", debug_color(&material->emissive));
1916 TRACE("power %.8e.\n", material->power);
1919 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1920 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1922 enum wined3d_format_id prev_format;
1923 struct wined3d_buffer *prev_buffer;
1924 unsigned int prev_offset;
1926 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1927 device, buffer, debug_d3dformat(format_id), offset);
1929 prev_buffer = device->update_state->index_buffer;
1930 prev_format = device->update_state->index_format;
1931 prev_offset = device->update_state->index_offset;
1933 device->update_state->index_buffer = buffer;
1934 device->update_state->index_format = format_id;
1935 device->update_state->index_offset = offset;
1937 if (device->recording)
1938 device->recording->changed.indices = TRUE;
1940 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1941 return;
1943 if (buffer)
1944 wined3d_buffer_incref(buffer);
1945 if (!device->recording)
1946 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1947 if (prev_buffer)
1948 wined3d_buffer_decref(prev_buffer);
1951 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1952 enum wined3d_format_id *format, unsigned int *offset)
1954 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1956 *format = device->state.index_format;
1957 if (offset)
1958 *offset = device->state.index_offset;
1959 return device->state.index_buffer;
1962 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1964 TRACE("device %p, base_index %d.\n", device, base_index);
1966 device->update_state->base_vertex_index = base_index;
1969 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1971 TRACE("device %p.\n", device);
1973 return device->state.base_vertex_index;
1976 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1978 TRACE("device %p, viewport %p.\n", device, viewport);
1979 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1980 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1982 device->update_state->viewport = *viewport;
1984 /* Handle recording of state blocks */
1985 if (device->recording)
1987 TRACE("Recording... not performing anything\n");
1988 device->recording->changed.viewport = TRUE;
1989 return;
1992 wined3d_cs_emit_set_viewport(device->cs, viewport);
1995 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1997 TRACE("device %p, viewport %p.\n", device, viewport);
1999 *viewport = device->state.viewport;
2002 static void resolve_depth_buffer(struct wined3d_state *state)
2004 struct wined3d_texture *dst_texture = state->textures[0];
2005 struct wined3d_rendertarget_view *src_view;
2006 RECT src_rect, dst_rect;
2008 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2009 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2010 return;
2012 if (!(src_view = state->fb->depth_stencil))
2013 return;
2014 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2016 FIXME("Not supported on buffer resources.\n");
2017 return;
2020 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2021 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2022 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
2023 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2026 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2027 struct wined3d_rasterizer_state *rasterizer_state)
2029 struct wined3d_rasterizer_state *prev;
2031 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2033 prev = device->update_state->rasterizer_state;
2034 if (prev == rasterizer_state)
2035 return;
2037 if (rasterizer_state)
2038 wined3d_rasterizer_state_incref(rasterizer_state);
2039 device->update_state->rasterizer_state = rasterizer_state;
2040 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2041 if (prev)
2042 wined3d_rasterizer_state_decref(prev);
2045 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2047 TRACE("device %p.\n", device);
2049 return device->state.rasterizer_state;
2052 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2053 enum wined3d_render_state state, DWORD value)
2055 DWORD old_value;
2057 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2059 if (state > WINEHIGHEST_RENDER_STATE)
2061 WARN("Unhandled render state %#x.\n", state);
2062 return;
2065 old_value = device->state.render_states[state];
2066 device->update_state->render_states[state] = value;
2068 /* Handle recording of state blocks. */
2069 if (device->recording)
2071 TRACE("Recording... not performing anything.\n");
2072 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2073 return;
2076 /* Compared here and not before the assignment to allow proper stateblock recording. */
2077 if (value == old_value)
2078 TRACE("Application is setting the old value over, nothing to do.\n");
2079 else
2080 wined3d_cs_emit_set_render_state(device->cs, state, value);
2082 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2084 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2085 resolve_depth_buffer(&device->state);
2089 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2091 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2093 return device->state.render_states[state];
2096 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2097 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2099 DWORD old_value;
2101 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2102 device, sampler_idx, debug_d3dsamplerstate(state), value);
2104 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2105 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2107 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2109 WARN("Invalid sampler %u.\n", sampler_idx);
2110 return; /* Windows accepts overflowing this array ... we do not. */
2113 old_value = device->state.sampler_states[sampler_idx][state];
2114 device->update_state->sampler_states[sampler_idx][state] = value;
2116 /* Handle recording of state blocks. */
2117 if (device->recording)
2119 TRACE("Recording... not performing anything.\n");
2120 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2121 return;
2124 if (old_value == value)
2126 TRACE("Application is setting the old value over, nothing to do.\n");
2127 return;
2130 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2133 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2134 UINT sampler_idx, enum wined3d_sampler_state state)
2136 TRACE("device %p, sampler_idx %u, state %s.\n",
2137 device, sampler_idx, debug_d3dsamplerstate(state));
2139 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2140 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2142 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2144 WARN("Invalid sampler %u.\n", sampler_idx);
2145 return 0; /* Windows accepts overflowing this array ... we do not. */
2148 return device->state.sampler_states[sampler_idx][state];
2151 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2153 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2155 if (device->recording)
2156 device->recording->changed.scissorRect = TRUE;
2158 if (EqualRect(&device->update_state->scissor_rect, rect))
2160 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2161 return;
2163 CopyRect(&device->update_state->scissor_rect, rect);
2165 if (device->recording)
2167 TRACE("Recording... not performing anything.\n");
2168 return;
2171 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2174 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2176 TRACE("device %p, rect %p.\n", device, rect);
2178 *rect = device->state.scissor_rect;
2179 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2182 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2183 struct wined3d_vertex_declaration *declaration)
2185 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2187 TRACE("device %p, declaration %p.\n", device, declaration);
2189 if (device->recording)
2190 device->recording->changed.vertexDecl = TRUE;
2192 if (declaration == prev)
2193 return;
2195 if (declaration)
2196 wined3d_vertex_declaration_incref(declaration);
2197 device->update_state->vertex_declaration = declaration;
2198 if (!device->recording)
2199 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2200 if (prev)
2201 wined3d_vertex_declaration_decref(prev);
2204 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2206 TRACE("device %p.\n", device);
2208 return device->state.vertex_declaration;
2211 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2213 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2215 TRACE("device %p, shader %p.\n", device, shader);
2217 if (device->recording)
2218 device->recording->changed.vertexShader = TRUE;
2220 if (shader == prev)
2221 return;
2223 if (shader)
2224 wined3d_shader_incref(shader);
2225 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2226 if (!device->recording)
2227 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2228 if (prev)
2229 wined3d_shader_decref(prev);
2232 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2234 TRACE("device %p.\n", device);
2236 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2239 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2240 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2242 struct wined3d_buffer *prev;
2244 if (idx >= MAX_CONSTANT_BUFFERS)
2246 WARN("Invalid constant buffer index %u.\n", idx);
2247 return;
2250 prev = device->update_state->cb[type][idx];
2251 if (buffer == prev)
2252 return;
2254 if (buffer)
2255 wined3d_buffer_incref(buffer);
2256 device->update_state->cb[type][idx] = buffer;
2257 if (!device->recording)
2258 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2259 if (prev)
2260 wined3d_buffer_decref(prev);
2263 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2265 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2267 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2270 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2272 TRACE("device %p, idx %u.\n", device, idx);
2274 if (idx >= MAX_CONSTANT_BUFFERS)
2276 WARN("Invalid constant buffer index %u.\n", idx);
2277 return NULL;
2280 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2283 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2284 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2286 struct wined3d_shader_resource_view *prev;
2288 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2290 WARN("Invalid view index %u.\n", idx);
2291 return;
2294 prev = device->update_state->shader_resource_view[type][idx];
2295 if (view == prev)
2296 return;
2298 if (view)
2299 wined3d_shader_resource_view_incref(view);
2300 device->update_state->shader_resource_view[type][idx] = view;
2301 if (!device->recording)
2302 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2303 if (prev)
2304 wined3d_shader_resource_view_decref(prev);
2307 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2308 UINT idx, struct wined3d_shader_resource_view *view)
2310 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2312 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2315 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2316 UINT idx)
2318 TRACE("device %p, idx %u.\n", device, idx);
2320 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2322 WARN("Invalid view index %u.\n", idx);
2323 return NULL;
2326 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2329 static void wined3d_device_set_sampler(struct wined3d_device *device,
2330 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2332 struct wined3d_sampler *prev;
2334 if (idx >= MAX_SAMPLER_OBJECTS)
2336 WARN("Invalid sampler index %u.\n", idx);
2337 return;
2340 prev = device->update_state->sampler[type][idx];
2341 if (sampler == prev)
2342 return;
2344 if (sampler)
2345 wined3d_sampler_incref(sampler);
2346 device->update_state->sampler[type][idx] = sampler;
2347 if (!device->recording)
2348 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2349 if (prev)
2350 wined3d_sampler_decref(prev);
2353 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2355 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2357 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2360 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2362 TRACE("device %p, idx %u.\n", device, idx);
2364 if (idx >= MAX_SAMPLER_OBJECTS)
2366 WARN("Invalid sampler index %u.\n", idx);
2367 return NULL;
2370 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2373 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2374 unsigned int start_idx, unsigned int count, const BOOL *constants)
2376 unsigned int i;
2378 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2379 device, start_idx, count, constants);
2381 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2382 return WINED3DERR_INVALIDCALL;
2384 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2385 count = WINED3D_MAX_CONSTS_B - start_idx;
2386 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2387 if (TRACE_ON(d3d))
2389 for (i = 0; i < count; ++i)
2390 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2393 if (device->recording)
2395 for (i = start_idx; i < count + start_idx; ++i)
2396 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2398 else
2400 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2403 return WINED3D_OK;
2406 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2407 unsigned int start_idx, unsigned int count, BOOL *constants)
2409 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2410 device, start_idx, count, constants);
2412 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2413 return WINED3DERR_INVALIDCALL;
2415 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2416 count = WINED3D_MAX_CONSTS_B - start_idx;
2417 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2419 return WINED3D_OK;
2422 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2423 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2425 unsigned int i;
2427 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2428 device, start_idx, count, constants);
2430 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2431 return WINED3DERR_INVALIDCALL;
2433 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2434 count = WINED3D_MAX_CONSTS_I - start_idx;
2435 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2436 if (TRACE_ON(d3d))
2438 for (i = 0; i < count; ++i)
2439 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2442 if (device->recording)
2444 for (i = start_idx; i < count + start_idx; ++i)
2445 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2447 else
2449 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2452 return WINED3D_OK;
2455 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2456 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2458 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2459 device, start_idx, count, constants);
2461 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2462 return WINED3DERR_INVALIDCALL;
2464 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2465 count = WINED3D_MAX_CONSTS_I - start_idx;
2466 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2467 return WINED3D_OK;
2470 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2471 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2473 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2474 unsigned int i;
2476 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2477 device, start_idx, count, constants);
2479 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2480 || count > d3d_info->limits.vs_uniform_count - start_idx)
2481 return WINED3DERR_INVALIDCALL;
2483 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2484 if (TRACE_ON(d3d))
2486 for (i = 0; i < count; ++i)
2487 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2490 if (device->recording)
2491 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2492 count * sizeof(*device->recording->changed.vs_consts_f));
2493 else
2494 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2496 return WINED3D_OK;
2499 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2500 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2502 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2504 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2505 device, start_idx, count, constants);
2507 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2508 || count > d3d_info->limits.vs_uniform_count - start_idx)
2509 return WINED3DERR_INVALIDCALL;
2511 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2513 return WINED3D_OK;
2516 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2518 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2520 TRACE("device %p, shader %p.\n", device, shader);
2522 if (device->recording)
2523 device->recording->changed.pixelShader = TRUE;
2525 if (shader == prev)
2526 return;
2528 if (shader)
2529 wined3d_shader_incref(shader);
2530 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2531 if (!device->recording)
2532 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2533 if (prev)
2534 wined3d_shader_decref(prev);
2537 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2539 TRACE("device %p.\n", device);
2541 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2544 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2546 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2548 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2551 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2553 TRACE("device %p, idx %u.\n", device, idx);
2555 if (idx >= MAX_CONSTANT_BUFFERS)
2557 WARN("Invalid constant buffer index %u.\n", idx);
2558 return NULL;
2561 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2564 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2565 UINT idx, struct wined3d_shader_resource_view *view)
2567 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2569 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2572 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2573 UINT idx)
2575 TRACE("device %p, idx %u.\n", device, idx);
2577 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2579 WARN("Invalid view index %u.\n", idx);
2580 return NULL;
2583 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2586 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2588 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2590 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2593 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2595 TRACE("device %p, idx %u.\n", device, idx);
2597 if (idx >= MAX_SAMPLER_OBJECTS)
2599 WARN("Invalid sampler index %u.\n", idx);
2600 return NULL;
2603 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2606 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2607 unsigned int start_idx, unsigned int count, const BOOL *constants)
2609 unsigned int i;
2611 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2612 device, start_idx, count, constants);
2614 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2615 return WINED3DERR_INVALIDCALL;
2617 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2618 count = WINED3D_MAX_CONSTS_B - start_idx;
2619 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2620 if (TRACE_ON(d3d))
2622 for (i = 0; i < count; ++i)
2623 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2626 if (device->recording)
2628 for (i = start_idx; i < count + start_idx; ++i)
2629 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2631 else
2633 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2636 return WINED3D_OK;
2639 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2640 unsigned int start_idx, unsigned int count, BOOL *constants)
2642 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2643 device, start_idx, count, constants);
2645 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2646 return WINED3DERR_INVALIDCALL;
2648 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2649 count = WINED3D_MAX_CONSTS_B - start_idx;
2650 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2652 return WINED3D_OK;
2655 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2656 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2658 unsigned int i;
2660 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2661 device, start_idx, count, constants);
2663 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2664 return WINED3DERR_INVALIDCALL;
2666 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2667 count = WINED3D_MAX_CONSTS_I - start_idx;
2668 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2669 if (TRACE_ON(d3d))
2671 for (i = 0; i < count; ++i)
2672 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2675 if (device->recording)
2677 for (i = start_idx; i < count + start_idx; ++i)
2678 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2680 else
2682 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2685 return WINED3D_OK;
2688 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2689 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2691 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2692 device, start_idx, count, constants);
2694 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2695 return WINED3DERR_INVALIDCALL;
2697 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2698 count = WINED3D_MAX_CONSTS_I - start_idx;
2699 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2701 return WINED3D_OK;
2704 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2705 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2707 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2708 unsigned int i;
2710 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2711 device, start_idx, count, constants);
2713 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2714 || count > d3d_info->limits.ps_uniform_count - start_idx)
2715 return WINED3DERR_INVALIDCALL;
2717 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2718 if (TRACE_ON(d3d))
2720 for (i = 0; i < count; ++i)
2721 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2724 if (device->recording)
2725 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2726 count * sizeof(*device->recording->changed.ps_consts_f));
2727 else
2728 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2730 return WINED3D_OK;
2733 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2734 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2736 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2738 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2739 device, start_idx, count, constants);
2741 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2742 || count > d3d_info->limits.ps_uniform_count - start_idx)
2743 return WINED3DERR_INVALIDCALL;
2745 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2747 return WINED3D_OK;
2750 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2752 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2754 TRACE("device %p, shader %p.\n", device, shader);
2756 if (device->recording || shader == prev)
2757 return;
2758 if (shader)
2759 wined3d_shader_incref(shader);
2760 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2761 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2762 if (prev)
2763 wined3d_shader_decref(prev);
2766 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2768 TRACE("device %p.\n", device);
2770 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2773 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2775 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2777 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2780 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2782 TRACE("device %p, idx %u.\n", device, idx);
2784 if (idx >= MAX_CONSTANT_BUFFERS)
2786 WARN("Invalid constant buffer index %u.\n", idx);
2787 return NULL;
2790 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2793 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2794 UINT idx, struct wined3d_shader_resource_view *view)
2796 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2798 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2801 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2802 UINT idx)
2804 TRACE("device %p, idx %u.\n", device, idx);
2806 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2808 WARN("Invalid view index %u.\n", idx);
2809 return NULL;
2812 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2815 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2817 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2819 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2822 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2824 TRACE("device %p, idx %u.\n", device, idx);
2826 if (idx >= MAX_SAMPLER_OBJECTS)
2828 WARN("Invalid sampler index %u.\n", idx);
2829 return NULL;
2832 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2835 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2837 struct wined3d_shader *prev;
2839 TRACE("device %p, shader %p.\n", device, shader);
2841 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2842 if (device->recording || shader == prev)
2843 return;
2844 if (shader)
2845 wined3d_shader_incref(shader);
2846 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2847 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2848 if (prev)
2849 wined3d_shader_decref(prev);
2852 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2854 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2856 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2859 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2860 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav)
2862 struct wined3d_unordered_access_view *prev;
2864 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2866 WARN("Invalid UAV index %u.\n", idx);
2867 return;
2870 prev = device->update_state->unordered_access_view[pipeline][idx];
2871 if (uav == prev)
2872 return;
2874 if (uav)
2875 wined3d_unordered_access_view_incref(uav);
2876 device->update_state->unordered_access_view[pipeline][idx] = uav;
2877 if (!device->recording)
2878 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav);
2879 if (prev)
2880 wined3d_unordered_access_view_decref(prev);
2883 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2884 struct wined3d_unordered_access_view *uav)
2886 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2888 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav);
2891 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2892 unsigned int idx, struct wined3d_unordered_access_view *uav)
2894 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2896 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav);
2899 /* Context activation is done by the caller. */
2900 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2901 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2902 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2903 DWORD DestFVF)
2905 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2906 struct wined3d_map_desc map_desc;
2907 struct wined3d_box box = {0};
2908 struct wined3d_viewport vp;
2909 UINT vertex_size;
2910 unsigned int i;
2911 BYTE *dest_ptr;
2912 BOOL doClip;
2913 DWORD numTextures;
2914 HRESULT hr;
2916 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2918 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2921 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2923 ERR("Source has no position mask\n");
2924 return WINED3DERR_INVALIDCALL;
2927 if (device->state.render_states[WINED3D_RS_CLIPPING])
2929 static BOOL warned = FALSE;
2931 * The clipping code is not quite correct. Some things need
2932 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2933 * so disable clipping for now.
2934 * (The graphics in Half-Life are broken, and my processvertices
2935 * test crashes with IDirect3DDevice3)
2936 doClip = TRUE;
2938 doClip = FALSE;
2939 if(!warned) {
2940 warned = TRUE;
2941 FIXME("Clipping is broken and disabled for now\n");
2944 else
2945 doClip = FALSE;
2947 vertex_size = get_flexible_vertex_size(DestFVF);
2948 box.left = dwDestIndex * vertex_size;
2949 box.right = box.left + dwCount * vertex_size;
2950 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2952 WARN("Failed to map buffer, hr %#x.\n", hr);
2953 return hr;
2955 dest_ptr = map_desc.data;
2957 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2958 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2959 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2961 TRACE("View mat:\n");
2962 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2963 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2964 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2965 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2967 TRACE("Proj mat:\n");
2968 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2969 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2970 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2971 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2973 TRACE("World mat:\n");
2974 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2975 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2976 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2977 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2979 /* Get the viewport */
2980 wined3d_device_get_viewport(device, &vp);
2981 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2982 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2984 multiply_matrix(&mat,&view_mat,&world_mat);
2985 multiply_matrix(&mat,&proj_mat,&mat);
2987 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2989 for (i = 0; i < dwCount; i+= 1) {
2990 unsigned int tex_index;
2992 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2993 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2994 /* The position first */
2995 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2996 const float *p = (const float *)(element->data.addr + i * element->stride);
2997 float x, y, z, rhw;
2998 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3000 /* Multiplication with world, view and projection matrix. */
3001 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3002 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3003 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3004 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3006 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3008 /* WARNING: The following things are taken from d3d7 and were not yet checked
3009 * against d3d8 or d3d9!
3012 /* Clipping conditions: From msdn
3014 * A vertex is clipped if it does not match the following requirements
3015 * -rhw < x <= rhw
3016 * -rhw < y <= rhw
3017 * 0 < z <= rhw
3018 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3020 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3021 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3025 if( !doClip ||
3026 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3027 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3028 ( rhw > eps ) ) ) {
3030 /* "Normal" viewport transformation (not clipped)
3031 * 1) The values are divided by rhw
3032 * 2) The y axis is negative, so multiply it with -1
3033 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3034 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3035 * 4) Multiply x with Width/2 and add Width/2
3036 * 5) The same for the height
3037 * 6) Add the viewpoint X and Y to the 2D coordinates and
3038 * The minimum Z value to z
3039 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3041 * Well, basically it's simply a linear transformation into viewport
3042 * coordinates
3045 x /= rhw;
3046 y /= rhw;
3047 z /= rhw;
3049 y *= -1;
3051 x *= vp.width / 2;
3052 y *= vp.height / 2;
3053 z *= vp.max_z - vp.min_z;
3055 x += vp.width / 2 + vp.x;
3056 y += vp.height / 2 + vp.y;
3057 z += vp.min_z;
3059 rhw = 1 / rhw;
3060 } else {
3061 /* That vertex got clipped
3062 * Contrary to OpenGL it is not dropped completely, it just
3063 * undergoes a different calculation.
3065 TRACE("Vertex got clipped\n");
3066 x += rhw;
3067 y += rhw;
3069 x /= 2;
3070 y /= 2;
3072 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3073 * outside of the main vertex buffer memory. That needs some more
3074 * investigation...
3078 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3081 ( (float *) dest_ptr)[0] = x;
3082 ( (float *) dest_ptr)[1] = y;
3083 ( (float *) dest_ptr)[2] = z;
3084 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3086 dest_ptr += 3 * sizeof(float);
3088 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3089 dest_ptr += sizeof(float);
3092 if (DestFVF & WINED3DFVF_PSIZE)
3093 dest_ptr += sizeof(DWORD);
3095 if (DestFVF & WINED3DFVF_NORMAL)
3097 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3098 const float *normal = (const float *)(element->data.addr + i * element->stride);
3099 /* AFAIK this should go into the lighting information */
3100 FIXME("Didn't expect the destination to have a normal\n");
3101 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3104 if (DestFVF & WINED3DFVF_DIFFUSE)
3106 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3107 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3108 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3110 static BOOL warned = FALSE;
3112 if(!warned) {
3113 ERR("No diffuse color in source, but destination has one\n");
3114 warned = TRUE;
3117 *( (DWORD *) dest_ptr) = 0xffffffff;
3118 dest_ptr += sizeof(DWORD);
3120 else
3122 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3126 if (DestFVF & WINED3DFVF_SPECULAR)
3128 /* What's the color value in the feedback buffer? */
3129 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3130 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3131 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3133 static BOOL warned = FALSE;
3135 if(!warned) {
3136 ERR("No specular color in source, but destination has one\n");
3137 warned = TRUE;
3140 *(DWORD *)dest_ptr = 0xff000000;
3141 dest_ptr += sizeof(DWORD);
3143 else
3145 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3149 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3151 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3152 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3153 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3155 ERR("No source texture, but destination requests one\n");
3156 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3158 else
3160 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3165 wined3d_resource_unmap(&dest->resource, 0);
3167 return WINED3D_OK;
3169 #undef copy_and_next
3171 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3172 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3173 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3175 struct wined3d_state *state = &device->state;
3176 struct wined3d_stream_info stream_info;
3177 const struct wined3d_gl_info *gl_info;
3178 struct wined3d_context *context;
3179 struct wined3d_shader *vs;
3180 unsigned int i;
3181 HRESULT hr;
3182 WORD map;
3184 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3185 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3186 device, src_start_idx, dst_idx, vertex_count,
3187 dst_buffer, declaration, flags, dst_fvf);
3189 if (declaration)
3190 FIXME("Output vertex declaration not implemented yet.\n");
3192 /* Need any context to write to the vbo. */
3193 context = context_acquire(device, NULL, 0);
3194 gl_info = context->gl_info;
3196 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3197 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3198 context_stream_info_from_declaration(context, state, &stream_info);
3199 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3201 /* We can't convert FROM a VBO, and vertex buffers used to source into
3202 * process_vertices() are unlikely to ever be used for drawing. Release
3203 * VBOs in those buffers and fix up the stream_info structure.
3205 * Also apply the start index. */
3206 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3208 struct wined3d_stream_info_element *e;
3209 struct wined3d_buffer *buffer;
3211 if (!(map & 1))
3212 continue;
3214 e = &stream_info.elements[i];
3215 buffer = state->streams[e->stream_idx].buffer;
3216 e->data.buffer_object = 0;
3217 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3218 if (buffer->buffer_object)
3220 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3221 buffer->buffer_object = 0;
3222 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
3224 if (e->data.addr)
3225 e->data.addr += e->stride * src_start_idx;
3228 hr = process_vertices_strided(device, dst_idx, vertex_count,
3229 &stream_info, dst_buffer, flags, dst_fvf);
3231 context_release(context);
3233 return hr;
3236 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3237 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3239 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3240 DWORD old_value;
3242 TRACE("device %p, stage %u, state %s, value %#x.\n",
3243 device, stage, debug_d3dtexturestate(state), value);
3245 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3247 WARN("Invalid state %#x passed.\n", state);
3248 return;
3251 if (stage >= d3d_info->limits.ffp_blend_stages)
3253 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3254 stage, d3d_info->limits.ffp_blend_stages - 1);
3255 return;
3258 old_value = device->update_state->texture_states[stage][state];
3259 device->update_state->texture_states[stage][state] = value;
3261 if (device->recording)
3263 TRACE("Recording... not performing anything.\n");
3264 device->recording->changed.textureState[stage] |= 1u << state;
3265 return;
3268 /* Checked after the assignments to allow proper stateblock recording. */
3269 if (old_value == value)
3271 TRACE("Application is setting the old value over, nothing to do.\n");
3272 return;
3275 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3278 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3279 UINT stage, enum wined3d_texture_stage_state state)
3281 TRACE("device %p, stage %u, state %s.\n",
3282 device, stage, debug_d3dtexturestate(state));
3284 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3286 WARN("Invalid state %#x passed.\n", state);
3287 return 0;
3290 return device->state.texture_states[stage][state];
3293 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3294 UINT stage, struct wined3d_texture *texture)
3296 struct wined3d_texture *prev;
3298 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3300 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3301 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3303 /* Windows accepts overflowing this array... we do not. */
3304 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3306 WARN("Ignoring invalid stage %u.\n", stage);
3307 return WINED3D_OK;
3310 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3312 WARN("Rejecting attempt to set scratch texture.\n");
3313 return WINED3DERR_INVALIDCALL;
3316 if (device->recording)
3317 device->recording->changed.textures |= 1u << stage;
3319 prev = device->update_state->textures[stage];
3320 TRACE("Previous texture %p.\n", prev);
3322 if (texture == prev)
3324 TRACE("App is setting the same texture again, nothing to do.\n");
3325 return WINED3D_OK;
3328 TRACE("Setting new texture to %p.\n", texture);
3329 device->update_state->textures[stage] = texture;
3331 if (texture)
3332 wined3d_texture_incref(texture);
3333 if (!device->recording)
3334 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3335 if (prev)
3336 wined3d_texture_decref(prev);
3338 return WINED3D_OK;
3341 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3343 TRACE("device %p, stage %u.\n", device, stage);
3345 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3346 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3348 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3350 WARN("Ignoring invalid stage %u.\n", stage);
3351 return NULL; /* Windows accepts overflowing this array ... we do not. */
3354 return device->state.textures[stage];
3357 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3359 TRACE("device %p, caps %p.\n", device, caps);
3361 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3362 device->create_parms.device_type, caps);
3365 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3366 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3368 struct wined3d_swapchain *swapchain;
3370 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3371 device, swapchain_idx, mode, rotation);
3373 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3374 return WINED3DERR_INVALIDCALL;
3376 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3379 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3381 struct wined3d_stateblock *stateblock;
3382 HRESULT hr;
3384 TRACE("device %p.\n", device);
3386 if (device->recording)
3387 return WINED3DERR_INVALIDCALL;
3389 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3390 if (FAILED(hr))
3391 return hr;
3393 device->recording = stateblock;
3394 device->update_state = &stateblock->state;
3396 TRACE("Recording stateblock %p.\n", stateblock);
3398 return WINED3D_OK;
3401 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3402 struct wined3d_stateblock **stateblock)
3404 struct wined3d_stateblock *object = device->recording;
3406 TRACE("device %p, stateblock %p.\n", device, stateblock);
3408 if (!device->recording)
3410 WARN("Not recording.\n");
3411 *stateblock = NULL;
3412 return WINED3DERR_INVALIDCALL;
3415 stateblock_init_contained_states(object);
3417 *stateblock = object;
3418 device->recording = NULL;
3419 device->update_state = &device->state;
3421 TRACE("Returning stateblock %p.\n", *stateblock);
3423 return WINED3D_OK;
3426 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3428 /* At the moment we have no need for any functionality at the beginning
3429 * of a scene. */
3430 TRACE("device %p.\n", device);
3432 if (device->inScene)
3434 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3435 return WINED3DERR_INVALIDCALL;
3437 device->inScene = TRUE;
3438 return WINED3D_OK;
3441 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3443 struct wined3d_context *context;
3445 TRACE("device %p.\n", device);
3447 if (!device->inScene)
3449 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3450 return WINED3DERR_INVALIDCALL;
3453 context = context_acquire(device, NULL, 0);
3454 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3455 context->gl_info->gl_ops.gl.p_glFlush();
3456 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3457 * fails. */
3458 context_release(context);
3460 device->inScene = FALSE;
3461 return WINED3D_OK;
3464 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3465 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3467 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3468 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3470 if (!rect_count && rects)
3472 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3473 return WINED3D_OK;
3476 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3478 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3479 if (!ds)
3481 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3482 /* TODO: What about depth stencil buffers without stencil bits? */
3483 return WINED3DERR_INVALIDCALL;
3485 else if (flags & WINED3DCLEAR_TARGET)
3487 if (ds->width < device->fb.render_targets[0]->width
3488 || ds->height < device->fb.render_targets[0]->height)
3490 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3491 return WINED3D_OK;
3496 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3498 return WINED3D_OK;
3501 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3502 struct wined3d_query *predicate, BOOL value)
3504 struct wined3d_query *prev;
3506 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3508 prev = device->update_state->predicate;
3509 if (predicate)
3511 FIXME("Predicated rendering not implemented.\n");
3512 wined3d_query_incref(predicate);
3514 device->update_state->predicate = predicate;
3515 device->update_state->predicate_value = value;
3516 if (!device->recording)
3517 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3518 if (prev)
3519 wined3d_query_decref(prev);
3522 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3524 TRACE("device %p, value %p.\n", device, value);
3526 *value = device->state.predicate_value;
3527 return device->state.predicate;
3530 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3531 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3533 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3534 device, group_count_x, group_count_y, group_count_z);
3536 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3539 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3540 enum wined3d_primitive_type primitive_type)
3542 GLenum gl_primitive_type, prev;
3544 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3546 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3547 prev = device->update_state->gl_primitive_type;
3548 device->update_state->gl_primitive_type = gl_primitive_type;
3549 if (device->recording)
3550 device->recording->changed.primitive_type = TRUE;
3551 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3552 device_invalidate_state(device, STATE_POINT_ENABLE);
3555 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3556 enum wined3d_primitive_type *primitive_type)
3558 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3560 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3562 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3565 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3567 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3569 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, 0, 0, FALSE);
3571 return WINED3D_OK;
3574 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3575 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3577 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3578 device, start_vertex, vertex_count, start_instance, instance_count);
3580 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3583 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3585 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3587 if (!device->state.index_buffer)
3589 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3590 * without an index buffer set. (The first time at least...)
3591 * D3D8 simply dies, but I doubt it can do much harm to return
3592 * D3DERR_INVALIDCALL there as well. */
3593 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3594 return WINED3DERR_INVALIDCALL;
3597 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3599 return WINED3D_OK;
3602 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3603 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3605 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3606 device, start_idx, index_count, start_instance, instance_count);
3608 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index,
3609 start_idx, index_count, start_instance, instance_count, TRUE);
3612 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3613 struct wined3d_texture *src_texture, unsigned int src_level,
3614 struct wined3d_texture *dst_texture, unsigned int level_count)
3616 struct wined3d_const_bo_address data;
3617 struct wined3d_context *context;
3618 struct wined3d_map_desc src;
3619 HRESULT hr = WINED3D_OK;
3620 unsigned int i;
3622 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3623 device, src_texture, src_level, dst_texture, level_count);
3625 if (src_texture->resource.format != dst_texture->resource.format)
3627 WARN("Source and destination formats do not match.\n");
3628 return WINED3DERR_INVALIDCALL;
3631 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3632 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3633 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3635 WARN("Source and destination dimensions do not match.\n");
3636 return WINED3DERR_INVALIDCALL;
3639 context = context_acquire(device, NULL, 0);
3641 /* Only a prepare, since we're uploading entire volumes. */
3642 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3643 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3645 for (i = 0; i < level_count; ++i)
3647 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3648 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3649 goto done;
3651 data.buffer_object = 0;
3652 data.addr = src.data;
3653 wined3d_texture_upload_data(dst_texture, i, context, NULL, &data, src.row_pitch, src.slice_pitch);
3654 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3656 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3657 goto done;
3660 done:
3661 context_release(context);
3662 return hr;
3665 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3666 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3668 unsigned int src_size, dst_size, src_skip_levels = 0;
3669 unsigned int layer_count, level_count, i, j;
3670 enum wined3d_resource_type type;
3671 HRESULT hr;
3672 struct wined3d_context *context;
3674 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3676 /* Verify that the source and destination textures are non-NULL. */
3677 if (!src_texture || !dst_texture)
3679 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3680 return WINED3DERR_INVALIDCALL;
3683 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3685 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3686 return WINED3DERR_INVALIDCALL;
3688 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3690 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3691 return WINED3DERR_INVALIDCALL;
3694 /* Verify that the source and destination textures are the same type. */
3695 type = src_texture->resource.type;
3696 if (dst_texture->resource.type != type)
3698 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3699 return WINED3DERR_INVALIDCALL;
3702 layer_count = src_texture->layer_count;
3703 if (layer_count != dst_texture->layer_count)
3705 WARN("Source and destination have different layer counts.\n");
3706 return WINED3DERR_INVALIDCALL;
3709 level_count = min(wined3d_texture_get_level_count(src_texture),
3710 wined3d_texture_get_level_count(dst_texture));
3712 src_size = max(src_texture->resource.width, src_texture->resource.height);
3713 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3714 if (type == WINED3D_RTYPE_TEXTURE_3D)
3716 src_size = max(src_size, src_texture->resource.depth);
3717 dst_size = max(dst_size, dst_texture->resource.depth);
3719 while (src_size > dst_size)
3721 src_size >>= 1;
3722 ++src_skip_levels;
3725 /* Make sure that the destination texture is loaded. */
3726 context = context_acquire(device, NULL, 0);
3727 wined3d_texture_load(dst_texture, context, FALSE);
3728 context_release(context);
3730 /* Update every surface level of the texture. */
3731 switch (type)
3733 case WINED3D_RTYPE_TEXTURE_2D:
3735 unsigned int src_levels = src_texture->level_count;
3736 unsigned int dst_levels = dst_texture->level_count;
3737 struct wined3d_surface *src_surface;
3738 struct wined3d_surface *dst_surface;
3740 for (i = 0; i < layer_count; ++i)
3742 for (j = 0; j < level_count; ++j)
3744 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3745 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3746 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3748 WARN("Failed to update surface, hr %#x.\n", hr);
3749 return hr;
3753 return WINED3D_OK;
3756 case WINED3D_RTYPE_TEXTURE_3D:
3757 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3758 src_texture, src_skip_levels, dst_texture, level_count)))
3759 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3760 return hr;
3762 default:
3763 FIXME("Unsupported texture type %#x.\n", type);
3764 return WINED3DERR_INVALIDCALL;
3768 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3770 const struct wined3d_state *state = &device->state;
3771 struct wined3d_texture *texture;
3772 DWORD i;
3774 TRACE("device %p, num_passes %p.\n", device, num_passes);
3776 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3778 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3780 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3781 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3783 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3785 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3786 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3789 texture = state->textures[i];
3790 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3792 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3794 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3795 return E_FAIL;
3797 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3799 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3800 return E_FAIL;
3802 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3803 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3805 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3806 return E_FAIL;
3810 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3811 || state->render_states[WINED3D_RS_STENCILENABLE])
3813 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3814 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3816 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3818 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3819 return WINED3DERR_CONFLICTINGRENDERSTATE;
3823 /* return a sensible default */
3824 *num_passes = 1;
3826 TRACE("returning D3D_OK\n");
3827 return WINED3D_OK;
3830 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3832 static BOOL warned;
3834 TRACE("device %p, software %#x.\n", device, software);
3836 if (!warned)
3838 FIXME("device %p, software %#x stub!\n", device, software);
3839 warned = TRUE;
3842 device->softwareVertexProcessing = software;
3845 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3847 static BOOL warned;
3849 TRACE("device %p.\n", device);
3851 if (!warned)
3853 TRACE("device %p stub!\n", device);
3854 warned = TRUE;
3857 return device->softwareVertexProcessing;
3860 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3861 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3863 struct wined3d_swapchain *swapchain;
3865 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3866 device, swapchain_idx, raster_status);
3868 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3869 return WINED3DERR_INVALIDCALL;
3871 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3874 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3876 static BOOL warned;
3878 TRACE("device %p, segments %.8e.\n", device, segments);
3880 if (segments != 0.0f)
3882 if (!warned)
3884 FIXME("device %p, segments %.8e stub!\n", device, segments);
3885 warned = TRUE;
3889 return WINED3D_OK;
3892 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3894 static BOOL warned;
3896 TRACE("device %p.\n", device);
3898 if (!warned)
3900 FIXME("device %p stub!\n", device);
3901 warned = TRUE;
3904 return 0.0f;
3907 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3908 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3910 struct wined3d_texture *dst_texture, *src_texture;
3911 RECT dst_rect, src_rect;
3912 unsigned int i, j;
3913 HRESULT hr;
3915 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3917 if (src_resource == dst_resource)
3919 WARN("Source and destination are the same resource.\n");
3920 return;
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;
3931 if (src_resource->width != dst_resource->width
3932 || src_resource->height != dst_resource->height
3933 || src_resource->depth != dst_resource->depth)
3935 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3936 dst_resource->width, dst_resource->height, dst_resource->depth,
3937 src_resource->width, src_resource->height, src_resource->depth);
3938 return;
3941 if (src_resource->format->id != dst_resource->format->id)
3943 WARN("Resource formats (%s / %s) don't match.\n",
3944 debug_d3dformat(dst_resource->format->id),
3945 debug_d3dformat(src_resource->format->id));
3946 return;
3949 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3951 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3952 buffer_from_resource(src_resource), 0,
3953 dst_resource->size)))
3954 ERR("Failed to copy buffer, hr %#x.\n", hr);
3955 return;
3958 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3960 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3961 return;
3964 dst_texture = texture_from_resource(dst_resource);
3965 src_texture = texture_from_resource(src_resource);
3967 if (src_texture->layer_count != dst_texture->layer_count
3968 || src_texture->level_count != dst_texture->level_count)
3970 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3971 dst_texture->layer_count, dst_texture->level_count,
3972 src_texture->layer_count, src_texture->level_count);
3973 return;
3976 for (i = 0; i < dst_texture->level_count; ++i)
3978 SetRect(&dst_rect, 0, 0,
3979 wined3d_texture_get_level_width(dst_texture, i),
3980 wined3d_texture_get_level_height(dst_texture, i));
3981 SetRect(&src_rect, 0, 0,
3982 wined3d_texture_get_level_width(src_texture, i),
3983 wined3d_texture_get_level_height(dst_texture, i));
3984 for (j = 0; j < dst_texture->layer_count; ++j)
3986 unsigned int idx = j * dst_texture->level_count + i;
3988 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3989 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3990 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3995 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3996 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3997 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3998 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4000 struct wined3d_texture *dst_texture, *src_texture;
4001 RECT dst_rect, src_rect;
4002 HRESULT hr;
4004 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4005 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
4006 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4007 src_resource, src_sub_resource_idx, debug_box(src_box));
4009 if (src_box && (src_box->left >= src_box->right
4010 || src_box->top >= src_box->bottom
4011 || src_box->front >= src_box->back))
4013 WARN("Invalid box %s specified.\n", debug_box(src_box));
4014 return WINED3DERR_INVALIDCALL;
4017 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4019 WARN("Source and destination are the same sub-resource.\n");
4020 return WINED3DERR_INVALIDCALL;
4023 if (src_resource->type != dst_resource->type)
4025 WARN("Resource types (%s / %s) don't match.\n",
4026 debug_d3dresourcetype(dst_resource->type),
4027 debug_d3dresourcetype(src_resource->type));
4028 return WINED3DERR_INVALIDCALL;
4031 if (src_resource->format->id != dst_resource->format->id)
4033 WARN("Resource formats (%s / %s) don't match.\n",
4034 debug_d3dformat(dst_resource->format->id),
4035 debug_d3dformat(src_resource->format->id));
4036 return WINED3DERR_INVALIDCALL;
4039 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4041 unsigned int src_offset, size;
4043 if (dst_sub_resource_idx)
4045 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4046 return WINED3DERR_INVALIDCALL;
4048 if (src_sub_resource_idx)
4050 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4051 return WINED3DERR_INVALIDCALL;
4054 if (src_box)
4056 src_offset = src_box->left;
4057 size = src_box->right - src_box->left;
4059 else
4061 src_offset = 0;
4062 size = src_resource->size;
4065 if (src_offset > src_resource->size
4066 || size > src_resource->size - src_offset
4067 || dst_x > dst_resource->size
4068 || size > dst_resource->size - dst_x)
4070 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4071 dst_x, src_offset, size);
4072 return WINED3DERR_INVALIDCALL;
4075 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4076 buffer_from_resource(src_resource), src_offset, size);
4079 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4081 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4082 return WINED3DERR_INVALIDCALL;
4085 dst_texture = texture_from_resource(dst_resource);
4086 src_texture = texture_from_resource(src_resource);
4088 if (src_box)
4090 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4092 else
4094 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4096 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4097 wined3d_texture_get_level_height(src_texture, level));
4100 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4101 dst_y + (src_rect.bottom - src_rect.top));
4103 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4104 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4105 WARN("Failed to blit, hr %#x.\n", hr);
4107 return hr;
4110 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4111 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4112 unsigned int depth_pitch)
4114 unsigned int width, height, depth, level;
4115 struct wined3d_const_bo_address addr;
4116 struct wined3d_context *context;
4117 struct wined3d_texture *texture;
4119 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4120 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4122 if (resource->type == WINED3D_RTYPE_BUFFER)
4124 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4126 if (sub_resource_idx > 0)
4128 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4129 return;
4132 context = context_acquire(resource->device, NULL, 0);
4133 if (!wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER))
4135 ERR("Failed to load buffer location.\n");
4136 context_release(context);
4137 return;
4140 wined3d_buffer_upload_data(buffer, context, box, data);
4141 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
4142 context_release(context);
4144 return;
4147 if (resource->type != WINED3D_RTYPE_TEXTURE_2D && resource->type != WINED3D_RTYPE_TEXTURE_3D)
4149 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4150 return;
4153 texture = texture_from_resource(resource);
4154 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4156 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4157 return;
4160 level = sub_resource_idx % texture->level_count;
4161 width = wined3d_texture_get_level_width(texture, level);
4162 height = wined3d_texture_get_level_height(texture, level);
4163 depth = wined3d_texture_get_level_depth(texture, level);
4165 if (box && (box->left >= box->right || box->right > width
4166 || box->top >= box->bottom || box->bottom > height
4167 || box->front >= box->back || box->back > depth))
4169 WARN("Invalid box %s specified.\n", debug_box(box));
4170 return;
4173 addr.buffer_object = 0;
4174 addr.addr = data;
4176 context = context_acquire(resource->device, NULL, 0);
4178 /* Only load the sub-resource for partial updates. */
4179 if (!box || (!box->left && !box->top && !box->front
4180 && box->right == width && box->bottom == height && box->back == depth))
4181 wined3d_texture_prepare_texture(texture, context, FALSE);
4182 else
4183 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4184 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4186 wined3d_texture_upload_data(texture, sub_resource_idx, context, box, &addr, row_pitch, depth_pitch);
4188 context_release(context);
4190 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4191 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4194 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4195 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4196 const struct wined3d_color *color, float depth, DWORD stencil)
4198 const struct blit_shader *blitter;
4199 struct wined3d_resource *resource;
4200 enum wined3d_blit_op blit_op;
4201 RECT r;
4203 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4204 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4206 if (!flags)
4207 return WINED3D_OK;
4209 resource = view->resource;
4210 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4212 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4213 return WINED3DERR_INVALIDCALL;
4216 if (view->depth > 1)
4218 FIXME("Layered clears not implemented.\n");
4219 return WINED3DERR_INVALIDCALL;
4222 if (!rect)
4224 SetRect(&r, 0, 0, view->width, view->height);
4225 rect = &r;
4228 if (flags & WINED3DCLEAR_TARGET)
4229 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4230 else
4231 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4233 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4234 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4236 FIXME("No blitter is capable of performing the requested fill operation.\n");
4237 return WINED3DERR_INVALIDCALL;
4240 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4241 return blitter->color_fill(device, view, rect, color);
4242 else
4243 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4246 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4247 unsigned int view_idx)
4249 TRACE("device %p, view_idx %u.\n", device, view_idx);
4251 if (view_idx >= device->adapter->gl_info.limits.buffers)
4253 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4254 return NULL;
4257 return device->fb.render_targets[view_idx];
4260 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4262 TRACE("device %p.\n", device);
4264 return device->fb.depth_stencil;
4267 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4268 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4270 struct wined3d_rendertarget_view *prev;
4272 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4273 device, view_idx, view, set_viewport);
4275 if (view_idx >= device->adapter->gl_info.limits.buffers)
4277 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4278 return WINED3DERR_INVALIDCALL;
4281 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4283 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4284 return WINED3DERR_INVALIDCALL;
4287 /* Set the viewport and scissor rectangles, if requested. Tests show that
4288 * stateblock recording is ignored, the change goes directly into the
4289 * primary stateblock. */
4290 if (!view_idx && set_viewport)
4292 struct wined3d_state *state = &device->state;
4294 state->viewport.x = 0;
4295 state->viewport.y = 0;
4296 state->viewport.width = view->width;
4297 state->viewport.height = view->height;
4298 state->viewport.min_z = 0.0f;
4299 state->viewport.max_z = 1.0f;
4300 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4302 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4303 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4307 prev = device->fb.render_targets[view_idx];
4308 if (view == prev)
4309 return WINED3D_OK;
4311 if (view)
4312 wined3d_rendertarget_view_incref(view);
4313 device->fb.render_targets[view_idx] = view;
4314 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4315 /* Release after the assignment, to prevent device_resource_released()
4316 * from seeing the surface as still in use. */
4317 if (prev)
4318 wined3d_rendertarget_view_decref(prev);
4320 return WINED3D_OK;
4323 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4325 struct wined3d_rendertarget_view *prev;
4327 TRACE("device %p, view %p.\n", device, view);
4329 prev = device->fb.depth_stencil;
4330 if (prev == view)
4332 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4333 return;
4336 if ((device->fb.depth_stencil = view))
4337 wined3d_rendertarget_view_incref(view);
4338 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4339 if (prev)
4340 wined3d_rendertarget_view_decref(prev);
4343 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4344 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4346 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4347 struct wined3d_sub_resource_data data;
4348 struct wined3d_resource_desc desc;
4349 struct wined3d_map_desc map_desc;
4350 struct wined3d_texture *texture;
4351 HRESULT hr;
4353 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4355 ERR("Failed to map source texture.\n");
4356 return NULL;
4359 data.data = map_desc.data;
4360 data.row_pitch = map_desc.row_pitch;
4361 data.slice_pitch = map_desc.slice_pitch;
4363 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4364 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4365 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4366 desc.multisample_quality = 0;
4367 desc.usage = WINED3DUSAGE_DYNAMIC;
4368 desc.pool = WINED3D_POOL_DEFAULT;
4369 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4370 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4371 desc.depth = 1;
4372 desc.size = 0;
4374 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4375 &data, NULL, &wined3d_null_parent_ops, &texture);
4376 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4377 if (FAILED(hr))
4379 ERR("Failed to create cursor texture.\n");
4380 return NULL;
4383 return texture;
4386 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4387 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4389 unsigned int texture_level = sub_resource_idx % texture->level_count;
4390 unsigned int cursor_width, cursor_height;
4391 struct wined3d_display_mode mode;
4392 struct wined3d_map_desc map_desc;
4393 HRESULT hr;
4395 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4396 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4398 if (sub_resource_idx >= texture->level_count * texture->layer_count
4399 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4400 return WINED3DERR_INVALIDCALL;
4402 if (device->cursor_texture)
4404 wined3d_texture_decref(device->cursor_texture);
4405 device->cursor_texture = NULL;
4408 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4410 WARN("Texture %p has invalid format %s.\n",
4411 texture, debug_d3dformat(texture->resource.format->id));
4412 return WINED3DERR_INVALIDCALL;
4415 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4417 ERR("Failed to get display mode, hr %#x.\n", hr);
4418 return WINED3DERR_INVALIDCALL;
4421 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4422 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4423 if (cursor_width > mode.width || cursor_height > mode.height)
4425 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4426 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4427 return WINED3DERR_INVALIDCALL;
4430 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4432 /* Do not store the surface's pointer because the application may
4433 * release it after setting the cursor image. Windows doesn't
4434 * addref the set surface, so we can't do this either without
4435 * creating circular refcount dependencies. */
4436 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4438 ERR("Failed to create cursor texture.\n");
4439 return WINED3DERR_INVALIDCALL;
4442 if (cursor_width == 32 && cursor_height == 32)
4444 UINT mask_size = cursor_width * cursor_height / 8;
4445 ICONINFO cursor_info;
4446 DWORD *mask_bits;
4447 HCURSOR cursor;
4449 /* 32-bit user32 cursors ignore the alpha channel if it's all
4450 * zeroes, and use the mask instead. Fill the mask with all ones
4451 * to ensure we still get a fully transparent cursor. */
4452 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4453 return E_OUTOFMEMORY;
4454 memset(mask_bits, 0xff, mask_size);
4456 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4457 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4458 cursor_info.fIcon = FALSE;
4459 cursor_info.xHotspot = x_hotspot;
4460 cursor_info.yHotspot = y_hotspot;
4461 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4462 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4463 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4465 /* Create our cursor and clean up. */
4466 cursor = CreateIconIndirect(&cursor_info);
4467 if (cursor_info.hbmMask)
4468 DeleteObject(cursor_info.hbmMask);
4469 if (cursor_info.hbmColor)
4470 DeleteObject(cursor_info.hbmColor);
4471 if (device->hardwareCursor)
4472 DestroyCursor(device->hardwareCursor);
4473 device->hardwareCursor = cursor;
4474 if (device->bCursorVisible)
4475 SetCursor(cursor);
4477 HeapFree(GetProcessHeap(), 0, mask_bits);
4480 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4481 device->cursorWidth = cursor_width;
4482 device->cursorHeight = cursor_height;
4483 device->xHotSpot = x_hotspot;
4484 device->yHotSpot = y_hotspot;
4486 return WINED3D_OK;
4489 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4490 int x_screen_space, int y_screen_space, DWORD flags)
4492 TRACE("device %p, x %d, y %d, flags %#x.\n",
4493 device, x_screen_space, y_screen_space, flags);
4495 device->xScreenSpace = x_screen_space;
4496 device->yScreenSpace = y_screen_space;
4498 if (device->hardwareCursor)
4500 POINT pt;
4502 GetCursorPos( &pt );
4503 if (x_screen_space == pt.x && y_screen_space == pt.y)
4504 return;
4505 SetCursorPos( x_screen_space, y_screen_space );
4507 /* Switch to the software cursor if position diverges from the hardware one. */
4508 GetCursorPos( &pt );
4509 if (x_screen_space != pt.x || y_screen_space != pt.y)
4511 if (device->bCursorVisible) SetCursor( NULL );
4512 DestroyCursor( device->hardwareCursor );
4513 device->hardwareCursor = 0;
4518 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4520 BOOL oldVisible = device->bCursorVisible;
4522 TRACE("device %p, show %#x.\n", device, show);
4525 * When ShowCursor is first called it should make the cursor appear at the OS's last
4526 * known cursor position.
4528 if (show && !oldVisible)
4530 POINT pt;
4531 GetCursorPos(&pt);
4532 device->xScreenSpace = pt.x;
4533 device->yScreenSpace = pt.y;
4536 if (device->hardwareCursor)
4538 device->bCursorVisible = show;
4539 if (show)
4540 SetCursor(device->hardwareCursor);
4541 else
4542 SetCursor(NULL);
4544 else if (device->cursor_texture)
4546 device->bCursorVisible = show;
4549 return oldVisible;
4552 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4554 struct wined3d_resource *resource, *cursor;
4556 TRACE("device %p.\n", device);
4558 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4560 TRACE("Checking resource %p for eviction.\n", resource);
4562 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4564 TRACE("Evicting %p.\n", resource);
4565 wined3d_cs_emit_unload_resource(device->cs, resource);
4570 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4571 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4572 wined3d_device_reset_cb callback, BOOL reset_state)
4574 struct wined3d_resource *resource, *cursor;
4575 struct wined3d_swapchain *swapchain;
4576 struct wined3d_view_desc view_desc;
4577 BOOL backbuffer_resized;
4578 HRESULT hr = WINED3D_OK;
4579 unsigned int i;
4581 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4582 device, swapchain_desc, mode, callback, reset_state);
4584 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4586 ERR("Failed to get the first implicit swapchain.\n");
4587 return WINED3DERR_INVALIDCALL;
4590 if (reset_state)
4592 if (device->logo_texture)
4594 wined3d_texture_decref(device->logo_texture);
4595 device->logo_texture = NULL;
4597 if (device->cursor_texture)
4599 wined3d_texture_decref(device->cursor_texture);
4600 device->cursor_texture = NULL;
4602 state_unbind_resources(&device->state);
4605 if (device->fb.render_targets)
4607 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4609 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4612 wined3d_device_set_depth_stencil_view(device, NULL);
4614 if (reset_state)
4616 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4618 TRACE("Enumerating resource %p.\n", resource);
4619 if (FAILED(hr = callback(resource)))
4620 return hr;
4624 TRACE("New params:\n");
4625 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4626 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4627 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4628 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4629 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4630 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4631 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4632 TRACE("device_window %p\n", swapchain_desc->device_window);
4633 TRACE("windowed %#x\n", swapchain_desc->windowed);
4634 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4635 if (swapchain_desc->enable_auto_depth_stencil)
4636 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4637 TRACE("flags %#x\n", swapchain_desc->flags);
4638 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4639 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4640 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4642 /* No special treatment of these parameters. Just store them */
4643 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4644 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4645 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4646 swapchain->desc.flags = swapchain_desc->flags;
4647 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4648 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4649 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4651 if (swapchain_desc->device_window
4652 && swapchain_desc->device_window != swapchain->desc.device_window)
4654 TRACE("Changing the device window from %p to %p.\n",
4655 swapchain->desc.device_window, swapchain_desc->device_window);
4656 swapchain->desc.device_window = swapchain_desc->device_window;
4657 swapchain->device_window = swapchain_desc->device_window;
4658 wined3d_swapchain_set_window(swapchain, NULL);
4661 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4662 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4664 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4665 || swapchain->reapply_mode || mode
4666 || (!swapchain_desc->windowed && backbuffer_resized))
4668 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4669 return hr;
4671 else if (!swapchain_desc->windowed)
4673 DWORD style = device->style;
4674 DWORD exStyle = device->exStyle;
4675 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4676 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4677 * Reset to clear up their mess. Guild Wars also loses the device during that.
4679 device->style = 0;
4680 device->exStyle = 0;
4681 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4682 swapchain_desc->backbuffer_width,
4683 swapchain_desc->backbuffer_height);
4684 device->style = style;
4685 device->exStyle = exStyle;
4688 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4689 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4690 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4691 return hr;
4693 if (device->auto_depth_stencil_view)
4695 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4696 device->auto_depth_stencil_view = NULL;
4698 if (swapchain->desc.enable_auto_depth_stencil)
4700 struct wined3d_resource_desc texture_desc;
4701 struct wined3d_texture *texture;
4702 DWORD flags = 0;
4704 TRACE("Creating the depth stencil buffer.\n");
4706 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4707 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4708 texture_desc.multisample_type = swapchain->desc.multisample_type;
4709 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4710 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4711 texture_desc.pool = WINED3D_POOL_DEFAULT;
4712 texture_desc.width = swapchain->desc.backbuffer_width;
4713 texture_desc.height = swapchain->desc.backbuffer_height;
4714 texture_desc.depth = 1;
4715 texture_desc.size = 0;
4717 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4718 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4720 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4721 device->device_parent, &texture_desc, flags, &texture)))
4723 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4724 return WINED3DERR_INVALIDCALL;
4727 view_desc.format_id = texture->resource.format->id;
4728 view_desc.flags = 0;
4729 view_desc.u.texture.level_idx = 0;
4730 view_desc.u.texture.level_count = 1;
4731 view_desc.u.texture.layer_idx = 0;
4732 view_desc.u.texture.layer_count = 1;
4733 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4734 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4735 wined3d_texture_decref(texture);
4736 if (FAILED(hr))
4738 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4739 return hr;
4742 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4745 if (device->back_buffer_view)
4747 wined3d_rendertarget_view_decref(device->back_buffer_view);
4748 device->back_buffer_view = NULL;
4750 if (swapchain->desc.backbuffer_count)
4752 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4754 view_desc.format_id = back_buffer->format->id;
4755 view_desc.flags = 0;
4756 view_desc.u.texture.level_idx = 0;
4757 view_desc.u.texture.level_count = 1;
4758 view_desc.u.texture.layer_idx = 0;
4759 view_desc.u.texture.layer_count = 1;
4760 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4761 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4763 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4764 return hr;
4768 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4770 if (reset_state)
4772 TRACE("Resetting stateblock.\n");
4773 if (device->recording)
4775 wined3d_stateblock_decref(device->recording);
4776 device->recording = NULL;
4778 wined3d_cs_emit_reset_state(device->cs);
4779 state_cleanup(&device->state);
4781 if (device->d3d_initialized)
4782 wined3d_device_delete_opengl_contexts(device);
4784 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4785 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4786 device->update_state = &device->state;
4788 device_init_swapchain_state(device, swapchain);
4790 else if (device->back_buffer_view)
4792 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4793 struct wined3d_state *state = &device->state;
4795 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4797 /* Note the min_z / max_z is not reset. */
4798 state->viewport.x = 0;
4799 state->viewport.y = 0;
4800 state->viewport.width = view->width;
4801 state->viewport.height = view->height;
4802 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4804 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4805 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4808 if (device->d3d_initialized)
4810 if (reset_state)
4811 hr = wined3d_device_create_primary_opengl_context(device);
4812 swapchain_update_swap_interval(swapchain);
4815 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4816 * first use
4818 return hr;
4821 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4823 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4825 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4827 return WINED3D_OK;
4831 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4832 struct wined3d_device_creation_parameters *parameters)
4834 TRACE("device %p, parameters %p.\n", device, parameters);
4836 *parameters = device->create_parms;
4839 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4841 TRACE("device %p.\n", device);
4843 return device->wined3d;
4846 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4847 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4849 struct wined3d_swapchain *swapchain;
4851 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4852 device, swapchain_idx, flags, ramp);
4854 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4855 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4858 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4859 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4861 struct wined3d_swapchain *swapchain;
4863 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4864 device, swapchain_idx, ramp);
4866 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4867 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4870 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4872 TRACE("device %p, resource %p.\n", device, resource);
4874 list_add_head(&device->resources, &resource->resource_list_entry);
4877 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4879 TRACE("device %p, resource %p.\n", device, resource);
4881 list_remove(&resource->resource_list_entry);
4884 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4886 enum wined3d_resource_type type = resource->type;
4887 struct wined3d_rendertarget_view *rtv;
4888 unsigned int i;
4890 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4892 if (device->d3d_initialized)
4894 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4896 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4897 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4900 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4901 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4904 switch (type)
4906 case WINED3D_RTYPE_TEXTURE_2D:
4907 case WINED3D_RTYPE_TEXTURE_3D:
4908 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4910 struct wined3d_texture *texture = texture_from_resource(resource);
4912 if (device->state.textures[i] == texture)
4914 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4915 device->state.textures[i] = NULL;
4918 if (device->recording && device->update_state->textures[i] == texture)
4920 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4921 texture, device->recording, i);
4922 device->update_state->textures[i] = NULL;
4925 break;
4927 case WINED3D_RTYPE_BUFFER:
4929 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4931 for (i = 0; i < MAX_STREAMS; ++i)
4933 if (device->state.streams[i].buffer == buffer)
4935 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4936 device->state.streams[i].buffer = NULL;
4939 if (device->recording && device->update_state->streams[i].buffer == buffer)
4941 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4942 buffer, device->recording, i);
4943 device->update_state->streams[i].buffer = NULL;
4947 if (device->state.index_buffer == buffer)
4949 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4950 device->state.index_buffer = NULL;
4953 if (device->recording && device->update_state->index_buffer == buffer)
4955 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4956 buffer, device->recording);
4957 device->update_state->index_buffer = NULL;
4960 break;
4962 default:
4963 break;
4966 /* Remove the resource from the resourceStore */
4967 device_resource_remove(device, resource);
4969 TRACE("Resource released.\n");
4972 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4974 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4976 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
4979 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4980 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4981 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4983 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4984 const struct fragment_pipeline *fragment_pipeline;
4985 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4986 unsigned int i;
4987 HRESULT hr;
4989 device->ref = 1;
4990 device->wined3d = wined3d;
4991 wined3d_incref(device->wined3d);
4992 device->adapter = wined3d->adapter_count ? adapter : NULL;
4993 device->device_parent = device_parent;
4994 list_init(&device->resources);
4995 list_init(&device->shaders);
4996 device->surface_alignment = surface_alignment;
4998 /* Save the creation parameters. */
4999 device->create_parms.adapter_idx = adapter_idx;
5000 device->create_parms.device_type = device_type;
5001 device->create_parms.focus_window = focus_window;
5002 device->create_parms.flags = flags;
5004 device->shader_backend = adapter->shader_backend;
5006 vertex_pipeline = adapter->vertex_pipe;
5008 fragment_pipeline = adapter->fragment_pipe;
5010 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5012 if (vertex_pipeline->vp_states && fragment_pipeline->states
5013 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5014 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5015 fragment_pipeline, misc_state_template)))
5017 ERR("Failed to compile state table, hr %#x.\n", hr);
5018 wine_rb_destroy(&device->samplers, NULL, NULL);
5019 wined3d_decref(device->wined3d);
5020 return hr;
5023 device->blitter = adapter->blitter;
5025 state_init(&device->state, &device->fb, &adapter->gl_info,
5026 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5027 device->update_state = &device->state;
5029 if (!(device->cs = wined3d_cs_create(device)))
5031 WARN("Failed to create command stream.\n");
5032 state_cleanup(&device->state);
5033 hr = E_FAIL;
5034 goto err;
5037 return WINED3D_OK;
5039 err:
5040 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5042 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5044 wine_rb_destroy(&device->samplers, NULL, NULL);
5045 wined3d_decref(device->wined3d);
5046 return hr;
5049 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5051 DWORD rep = device->StateTable[state].representative;
5052 struct wined3d_context *context;
5053 DWORD idx;
5054 BYTE shift;
5055 UINT i;
5057 if (STATE_IS_COMPUTE(state))
5059 for (i = 0; i < device->context_count; ++i)
5060 context_invalidate_compute_state(device->contexts[i], state);
5061 return;
5064 for (i = 0; i < device->context_count; ++i)
5066 context = device->contexts[i];
5067 if(isStateDirty(context, rep)) continue;
5069 context->dirtyArray[context->numDirtyEntries++] = rep;
5070 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5071 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5072 context->isStateDirty[idx] |= (1u << shift);
5076 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5077 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5079 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5081 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5082 window, message, wparam, lparam);
5083 if (unicode)
5084 return DefWindowProcW(window, message, wparam, lparam);
5085 else
5086 return DefWindowProcA(window, message, wparam, lparam);
5089 if (message == WM_DESTROY)
5091 TRACE("unregister window %p.\n", window);
5092 wined3d_unregister_window(window);
5094 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5095 ERR("Window %p is not the focus window for device %p.\n", window, device);
5097 else if (message == WM_DISPLAYCHANGE)
5099 device->device_parent->ops->mode_changed(device->device_parent);
5101 else if (message == WM_ACTIVATEAPP)
5103 UINT i;
5105 for (i = 0; i < device->swapchain_count; i++)
5106 wined3d_swapchain_activate(device->swapchains[i], wparam);
5108 device->device_parent->ops->activate(device->device_parent, wparam);
5110 else if (message == WM_SYSCOMMAND)
5112 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5114 if (unicode)
5115 DefWindowProcW(window, message, wparam, lparam);
5116 else
5117 DefWindowProcA(window, message, wparam, lparam);
5121 if (unicode)
5122 return CallWindowProcW(proc, window, message, wparam, lparam);
5123 else
5124 return CallWindowProcA(proc, window, message, wparam, lparam);