include: Add constants for CryptProtectMemory/CryptUnprotectMemory.
[wine.git] / dlls / wined3d / device.c
blob7898527f6c03e3510700b18aaffbe27ea0f7dbe5
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 context = context_acquire(device, target);
236 if (!context->valid)
238 context_release(context);
239 WARN("Invalid context, skipping clear.\n");
240 return;
242 gl_info = context->gl_info;
244 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
245 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
246 * for the cleared parts, and the untouched parts.
248 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
249 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
250 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
251 * checking all this if the dest surface is in the drawable anyway. */
252 for (i = 0; i < rt_count; ++i)
254 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
256 if (rtv && rtv->format->id != WINED3DFMT_NULL)
258 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
260 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, rect_count ? clear_rect : NULL))
261 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
262 else
263 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
267 if (target)
269 render_offscreen = context->render_offscreen;
270 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
272 else
274 render_offscreen = TRUE;
275 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil->container,
276 depth_stencil->texture_level);
277 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil->container,
278 depth_stencil->texture_level);
281 if (depth_stencil && render_offscreen)
282 wined3d_texture_prepare_location(depth_stencil->container,
283 dsv->sub_resource_idx, context, dsv->resource->draw_binding);
285 if (flags & WINED3DCLEAR_ZBUFFER)
287 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
289 surface_load_location(depth_stencil, context, location);
292 if (!context_apply_clear_state(context, state, rt_count, fb))
294 context_release(context);
295 WARN("Failed to apply clear state, skipping clear.\n");
296 return;
299 /* Only set the values up once, as they are not changing. */
300 if (flags & WINED3DCLEAR_STENCIL)
302 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
304 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
305 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
307 gl_info->gl_ops.gl.p_glStencilMask(~0U);
308 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
309 gl_info->gl_ops.gl.p_glClearStencil(stencil);
310 checkGLcall("glClearStencil");
311 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
314 if (flags & WINED3DCLEAR_ZBUFFER)
316 DWORD location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
318 wined3d_texture_validate_location(depth_stencil->container, dsv->sub_resource_idx, location);
319 wined3d_texture_invalidate_location(depth_stencil->container, dsv->sub_resource_idx, ~location);
321 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
322 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
323 gl_info->gl_ops.gl.p_glClearDepth(depth);
324 checkGLcall("glClearDepth");
325 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
328 if (flags & WINED3DCLEAR_TARGET)
330 for (i = 0; i < rt_count; ++i)
332 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
333 struct wined3d_texture *texture;
335 if (!rtv)
336 continue;
338 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
340 FIXME("Not supported on buffer resources.\n");
341 continue;
344 texture = texture_from_resource(rtv->resource);
345 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
346 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
349 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
351 if (rt_count > 1)
352 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
353 "support, this might cause graphical issues.\n");
355 corrected_color.r = color->r < wined3d_srgb_const1[0]
356 ? color->r * wined3d_srgb_const0[3]
357 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
358 - wined3d_srgb_const0[2];
359 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
360 corrected_color.g = color->g < wined3d_srgb_const1[0]
361 ? color->g * wined3d_srgb_const0[3]
362 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
363 - wined3d_srgb_const0[2];
364 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
365 corrected_color.b = color->b < wined3d_srgb_const1[0]
366 ? color->b * wined3d_srgb_const0[3]
367 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
368 - wined3d_srgb_const0[2];
369 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
370 color = &corrected_color;
373 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
374 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
375 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
376 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
377 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
378 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
379 checkGLcall("glClearColor");
380 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
383 if (!rect_count)
385 if (render_offscreen)
387 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
388 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
390 else
392 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
393 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
395 checkGLcall("glScissor");
396 gl_info->gl_ops.gl.p_glClear(clear_mask);
397 checkGLcall("glClear");
399 else
401 RECT current_rect;
403 /* Now process each rect in turn. */
404 for (i = 0; i < rect_count; ++i)
406 /* Note that GL uses lower left, width/height. */
407 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
409 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
410 wine_dbgstr_rect(&clear_rect[i]),
411 wine_dbgstr_rect(&current_rect));
413 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
414 * The rectangle is not cleared, no error is returned, but further rectangles are
415 * still cleared if they are valid. */
416 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
418 TRACE("Rectangle with negative dimensions, ignoring.\n");
419 continue;
422 if (render_offscreen)
424 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
425 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
427 else
429 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
430 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
432 checkGLcall("glScissor");
434 gl_info->gl_ops.gl.p_glClear(clear_mask);
435 checkGLcall("glClear");
439 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
440 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
441 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
443 context_release(context);
446 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
448 ULONG refcount = InterlockedIncrement(&device->ref);
450 TRACE("%p increasing refcount to %u.\n", device, refcount);
452 return refcount;
455 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
457 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
459 ERR("Leftover sampler %p.\n", sampler);
462 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
464 ULONG refcount = InterlockedDecrement(&device->ref);
466 TRACE("%p decreasing refcount to %u.\n", device, refcount);
468 if (!refcount)
470 UINT i;
472 wined3d_cs_destroy(device->cs);
474 if (device->recording && wined3d_stateblock_decref(device->recording))
475 ERR("Something's still holding the recording stateblock.\n");
476 device->recording = NULL;
478 state_cleanup(&device->state);
480 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
482 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
483 device->multistate_funcs[i] = NULL;
486 if (!list_empty(&device->resources))
488 struct wined3d_resource *resource;
490 ERR("Device released with resources still bound.\n");
492 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
494 ERR("Leftover resource %p with type %s (%#x).\n",
495 resource, debug_d3dresourcetype(resource->type), resource->type);
499 if (device->contexts)
500 ERR("Context array not freed!\n");
501 if (device->hardwareCursor)
502 DestroyCursor(device->hardwareCursor);
503 device->hardwareCursor = 0;
505 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
507 wined3d_decref(device->wined3d);
508 device->wined3d = NULL;
509 HeapFree(GetProcessHeap(), 0, device);
510 TRACE("Freed device %p.\n", device);
513 return refcount;
516 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
518 TRACE("device %p.\n", device);
520 return device->swapchain_count;
523 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
525 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
527 if (swapchain_idx >= device->swapchain_count)
529 WARN("swapchain_idx %u >= swapchain_count %u.\n",
530 swapchain_idx, device->swapchain_count);
531 return NULL;
534 return device->swapchains[swapchain_idx];
537 static void device_load_logo(struct wined3d_device *device, const char *filename)
539 struct wined3d_color_key color_key;
540 struct wined3d_resource_desc desc;
541 HBITMAP hbm;
542 BITMAP bm;
543 HRESULT hr;
544 HDC dcb = NULL, dcs = NULL;
546 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
547 if(hbm)
549 GetObjectA(hbm, sizeof(BITMAP), &bm);
550 dcb = CreateCompatibleDC(NULL);
551 if(!dcb) goto out;
552 SelectObject(dcb, hbm);
554 else
556 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
557 * couldn't be loaded
559 memset(&bm, 0, sizeof(bm));
560 bm.bmWidth = 32;
561 bm.bmHeight = 32;
564 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
565 desc.format = WINED3DFMT_B5G6R5_UNORM;
566 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
567 desc.multisample_quality = 0;
568 desc.usage = WINED3DUSAGE_DYNAMIC;
569 desc.pool = WINED3D_POOL_DEFAULT;
570 desc.width = bm.bmWidth;
571 desc.height = bm.bmHeight;
572 desc.depth = 1;
573 desc.size = 0;
574 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
575 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
577 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
578 goto out;
581 if (dcb)
583 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
584 goto out;
585 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
586 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
588 color_key.color_space_low_value = 0;
589 color_key.color_space_high_value = 0;
590 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
592 else
594 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
595 const RECT rect = {0, 0, desc.width, desc.height};
596 struct wined3d_surface *surface;
598 /* Fill the surface with a white color to show that wined3d is there */
599 surface = device->logo_texture->sub_resources[0].u.surface;
600 surface_color_fill(surface, &rect, &c);
603 out:
604 if (dcb) DeleteDC(dcb);
605 if (hbm) DeleteObject(hbm);
608 /* Context activation is done by the caller. */
609 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
611 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
612 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
613 unsigned int i;
614 DWORD color;
616 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
617 color = 0x000000ff;
618 else
619 color = 0x00000000;
621 /* Under DirectX you can sample even if no texture is bound, whereas
622 * OpenGL will only allow that when a valid texture is bound.
623 * We emulate this by creating dummy textures and binding them
624 * to each texture stage when the currently set D3D texture is NULL. */
625 context_active_texture(context, gl_info, 0);
627 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d);
628 checkGLcall("glGenTextures");
629 TRACE("Dummy 2D texture given name %u.\n", device->dummy_textures.tex_2d);
631 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
632 checkGLcall("glBindTexture");
634 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
635 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
636 checkGLcall("glTexImage2D");
638 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
640 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_rect);
641 checkGLcall("glGenTextures");
642 TRACE("Dummy rectangle texture given name %u.\n", device->dummy_textures.tex_rect);
644 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
645 checkGLcall("glBindTexture");
647 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
648 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
649 checkGLcall("glTexImage2D");
652 if (gl_info->supported[EXT_TEXTURE3D])
654 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_3d);
655 checkGLcall("glGenTextures");
656 TRACE("Dummy 3D texture given name %u.\n", device->dummy_textures.tex_3d);
658 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
659 checkGLcall("glBindTexture");
661 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
662 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
663 checkGLcall("glTexImage3D");
666 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
668 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_cube);
669 checkGLcall("glGenTextures");
670 TRACE("Dummy cube texture given name %u.\n", device->dummy_textures.tex_cube);
672 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
673 checkGLcall("glBindTexture");
675 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
677 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
678 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
679 checkGLcall("glTexImage2D");
683 if (gl_info->supported[EXT_TEXTURE_ARRAY])
685 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_2d_array);
686 checkGLcall("glGenTextures");
687 TRACE("Dummy 2D array texture given name %u.\n", device->dummy_textures.tex_2d_array);
689 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
690 checkGLcall("glBindTexture");
692 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
693 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
694 checkGLcall("glTexImage3D");
697 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
699 GLuint buffer;
701 GL_EXTCALL(glGenBuffers(1, &buffer));
702 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
703 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
704 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
705 checkGLcall("Create buffer object");
707 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_textures.tex_buffer);
708 checkGLcall("glGenTextures");
709 TRACE("Dummy buffer texture given name %u.\n", device->dummy_textures.tex_buffer);
711 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, device->dummy_textures.tex_buffer);
712 checkGLcall("glBindTexture");
713 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
714 checkGLcall("glTexBuffer");
716 GL_EXTCALL(glDeleteBuffers(1, &buffer));
717 checkGLcall("glDeleteBuffers");
720 context_bind_dummy_textures(device, context);
723 /* Context activation is done by the caller. */
724 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
726 const struct wined3d_gl_info *gl_info = context->gl_info;
728 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
729 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_buffer);
731 if (gl_info->supported[EXT_TEXTURE_ARRAY])
732 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d_array);
734 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
735 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_cube);
737 if (gl_info->supported[EXT_TEXTURE3D])
738 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_3d);
740 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
741 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_rect);
743 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->dummy_textures.tex_2d);
745 checkGLcall("Delete dummy textures");
747 memset(&device->dummy_textures, 0, sizeof(device->dummy_textures));
750 /* Context activation is done by the caller. */
751 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
753 const struct wined3d_gl_info *gl_info = context->gl_info;
755 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
757 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
758 * instructions allow access to resources without using samplers.
759 * In GLSL, resources are always accessed through sampler or image variables. The default
760 * sampler object is used to emulate the direct resource access when there is no sampler state
761 * to use.
763 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
764 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
765 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
766 checkGLcall("Create default sampler");
768 /* In D3D10+, a NULL sampler maps to the default sampler state. */
769 GL_EXTCALL(glGenSamplers(1, &device->null_sampler));
770 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
771 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
772 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
773 GL_EXTCALL(glSamplerParameteri(device->null_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
774 checkGLcall("Create null sampler");
776 else
778 device->default_sampler = 0;
779 device->null_sampler = 0;
783 /* Context activation is done by the caller. */
784 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
786 const struct wined3d_gl_info *gl_info = context->gl_info;
788 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
790 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
791 GL_EXTCALL(glDeleteSamplers(1, &device->null_sampler));
792 checkGLcall("glDeleteSamplers");
795 device->default_sampler = 0;
796 device->null_sampler = 0;
799 static LONG fullscreen_style(LONG style)
801 /* Make sure the window is managed, otherwise we won't get keyboard input. */
802 style |= WS_POPUP | WS_SYSMENU;
803 style &= ~(WS_CAPTION | WS_THICKFRAME);
805 return style;
808 static LONG fullscreen_exstyle(LONG exstyle)
810 /* Filter out window decorations. */
811 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
813 return exstyle;
816 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
818 BOOL filter_messages;
819 LONG style, exstyle;
821 TRACE("Setting up window %p for fullscreen mode.\n", window);
823 if (device->style || device->exStyle)
825 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
826 window, device->style, device->exStyle);
829 device->style = GetWindowLongW(window, GWL_STYLE);
830 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
832 style = fullscreen_style(device->style);
833 exstyle = fullscreen_exstyle(device->exStyle);
835 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
836 device->style, device->exStyle, style, exstyle);
838 filter_messages = device->filter_messages;
839 device->filter_messages = TRUE;
841 SetWindowLongW(window, GWL_STYLE, style);
842 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
843 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
845 device->filter_messages = filter_messages;
848 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
849 const RECT *window_rect)
851 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
852 BOOL filter_messages;
853 LONG style, exstyle;
854 RECT rect = {0};
856 if (!device->style && !device->exStyle)
857 return;
859 style = GetWindowLongW(window, GWL_STYLE);
860 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
862 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
863 * application, and we want to ignore them in the test below, since it's
864 * not the application's fault that they changed. Additionally, we want to
865 * preserve the current status of these flags (i.e. don't restore them) to
866 * more closely emulate the behavior of Direct3D, which leaves these flags
867 * alone when returning to windowed mode. */
868 device->style ^= (device->style ^ style) & WS_VISIBLE;
869 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
871 TRACE("Restoring window style of window %p to %08x, %08x.\n",
872 window, device->style, device->exStyle);
874 filter_messages = device->filter_messages;
875 device->filter_messages = TRUE;
877 /* Only restore the style if the application didn't modify it during the
878 * fullscreen phase. Some applications change it before calling Reset()
879 * when switching between windowed and fullscreen modes (HL2), some
880 * depend on the original style (Eve Online). */
881 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
883 SetWindowLongW(window, GWL_STYLE, device->style);
884 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
887 if (window_rect)
888 rect = *window_rect;
889 else
890 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
891 SetWindowPos(window, 0, rect.left, rect.top,
892 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
894 device->filter_messages = filter_messages;
896 /* Delete the old values. */
897 device->style = 0;
898 device->exStyle = 0;
901 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
903 TRACE("device %p, window %p.\n", device, window);
905 if (!wined3d_register_window(window, device))
907 ERR("Failed to register window %p.\n", window);
908 return E_FAIL;
911 InterlockedExchangePointer((void **)&device->focus_window, window);
912 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
914 return WINED3D_OK;
917 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
919 TRACE("device %p.\n", device);
921 if (device->focus_window) wined3d_unregister_window(device->focus_window);
922 InterlockedExchangePointer((void **)&device->focus_window, NULL);
925 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
927 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
928 unsigned int i;
930 if (device->fb.render_targets)
932 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
934 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
936 if (device->back_buffer_view)
937 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
940 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
941 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
944 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
945 struct wined3d_swapchain_desc *swapchain_desc)
947 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
948 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
949 struct wined3d_swapchain *swapchain = NULL;
950 struct wined3d_context *context;
951 DWORD clear_flags = 0;
952 HRESULT hr;
954 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
956 if (device->d3d_initialized)
957 return WINED3DERR_INVALIDCALL;
958 if (device->wined3d->flags & WINED3D_NO3D)
959 return WINED3DERR_INVALIDCALL;
961 if (!(device->fb.render_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*device->fb.render_targets))))
962 return E_OUTOFMEMORY;
964 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
965 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
967 TRACE("Shader private data couldn't be allocated\n");
968 goto err_out;
970 if (FAILED(hr = device->blitter->alloc_private(device)))
972 TRACE("Blitter private data couldn't be allocated\n");
973 goto err_out;
976 /* Setup the implicit swapchain. This also initializes a context. */
977 TRACE("Creating implicit swapchain\n");
978 hr = device->device_parent->ops->create_swapchain(device->device_parent,
979 swapchain_desc, &swapchain);
980 if (FAILED(hr))
982 WARN("Failed to create implicit swapchain\n");
983 goto err_out;
986 if (swapchain_desc->backbuffer_count)
988 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
989 struct wined3d_view_desc view_desc;
991 view_desc.format_id = back_buffer->format->id;
992 view_desc.flags = 0;
993 view_desc.u.texture.level_idx = 0;
994 view_desc.u.texture.level_count = 1;
995 view_desc.u.texture.layer_idx = 0;
996 view_desc.u.texture.layer_count = 1;
997 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
998 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1000 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1001 goto err_out;
1005 device->swapchain_count = 1;
1006 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1008 ERR("Out of memory!\n");
1009 goto err_out;
1011 device->swapchains[0] = swapchain;
1012 device_init_swapchain_state(device, swapchain);
1014 context = context_acquire(device, NULL);
1016 create_dummy_textures(device, context);
1017 create_default_samplers(device, context);
1019 device->contexts[0]->last_was_rhw = 0;
1021 TRACE("All defaults now set up, leaving 3D init.\n");
1023 context_release(context);
1025 /* Clear the screen */
1026 if (swapchain->back_buffers && swapchain->back_buffers[0])
1027 clear_flags |= WINED3DCLEAR_TARGET;
1028 if (swapchain_desc->enable_auto_depth_stencil)
1029 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1030 if (clear_flags)
1031 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1033 device->d3d_initialized = TRUE;
1035 if (wined3d_settings.logo)
1036 device_load_logo(device, wined3d_settings.logo);
1037 return WINED3D_OK;
1039 err_out:
1040 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1041 HeapFree(GetProcessHeap(), 0, device->swapchains);
1042 device->swapchain_count = 0;
1043 if (device->back_buffer_view)
1044 wined3d_rendertarget_view_decref(device->back_buffer_view);
1045 if (swapchain)
1046 wined3d_swapchain_decref(swapchain);
1047 if (device->blit_priv)
1048 device->blitter->free_private(device);
1049 if (device->shader_priv)
1050 device->shader_backend->shader_free_private(device);
1052 return hr;
1055 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1056 struct wined3d_swapchain_desc *swapchain_desc)
1058 struct wined3d_swapchain *swapchain = NULL;
1059 HRESULT hr;
1061 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1063 /* Setup the implicit swapchain */
1064 TRACE("Creating implicit swapchain\n");
1065 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1066 swapchain_desc, &swapchain);
1067 if (FAILED(hr))
1069 WARN("Failed to create implicit swapchain\n");
1070 goto err_out;
1073 device->swapchain_count = 1;
1074 if (!(device->swapchains = wined3d_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1076 ERR("Out of memory!\n");
1077 goto err_out;
1079 device->swapchains[0] = swapchain;
1080 return WINED3D_OK;
1082 err_out:
1083 wined3d_swapchain_decref(swapchain);
1084 return hr;
1087 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1089 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1091 wined3d_sampler_decref(sampler);
1094 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1096 struct wined3d_resource *resource, *cursor;
1097 const struct wined3d_gl_info *gl_info;
1098 struct wined3d_context *context;
1099 UINT i;
1101 TRACE("device %p.\n", device);
1103 if (!device->d3d_initialized)
1104 return WINED3DERR_INVALIDCALL;
1106 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1107 * it was created. Thus make sure a context is active for the glDelete* calls
1109 context = context_acquire(device, NULL);
1110 gl_info = context->gl_info;
1112 if (device->logo_texture)
1113 wined3d_texture_decref(device->logo_texture);
1114 if (device->cursor_texture)
1115 wined3d_texture_decref(device->cursor_texture);
1117 state_unbind_resources(&device->state);
1119 /* Unload resources */
1120 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1122 TRACE("Unloading resource %p.\n", resource);
1123 wined3d_cs_emit_unload_resource(device->cs, resource);
1126 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1128 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1129 * private data, it might contain opengl pointers
1131 if (device->depth_blt_texture)
1133 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1134 device->depth_blt_texture = 0;
1137 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1138 device->blitter->free_private(device);
1139 device->shader_backend->shader_free_private(device);
1140 destroy_dummy_textures(device, context);
1141 destroy_default_samplers(device, context);
1143 context_release(context);
1145 if (device->fb.depth_stencil)
1147 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1149 TRACE("Releasing depth/stencil view %p.\n", view);
1151 device->fb.depth_stencil = NULL;
1152 wined3d_rendertarget_view_decref(view);
1155 if (device->auto_depth_stencil_view)
1157 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1159 device->auto_depth_stencil_view = NULL;
1160 if (wined3d_rendertarget_view_decref(view))
1161 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1164 for (i = 0; i < gl_info->limits.buffers; ++i)
1166 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1168 if (device->back_buffer_view)
1170 wined3d_rendertarget_view_decref(device->back_buffer_view);
1171 device->back_buffer_view = NULL;
1174 for (i = 0; i < device->swapchain_count; ++i)
1176 TRACE("Releasing the implicit swapchain %u.\n", i);
1177 if (wined3d_swapchain_decref(device->swapchains[i]))
1178 FIXME("Something's still holding the implicit swapchain.\n");
1181 HeapFree(GetProcessHeap(), 0, device->swapchains);
1182 device->swapchains = NULL;
1183 device->swapchain_count = 0;
1185 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1186 device->fb.render_targets = NULL;
1188 device->d3d_initialized = FALSE;
1190 return WINED3D_OK;
1193 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1195 unsigned int i;
1197 for (i = 0; i < device->swapchain_count; ++i)
1199 TRACE("Releasing the implicit swapchain %u.\n", i);
1200 if (wined3d_swapchain_decref(device->swapchains[i]))
1201 FIXME("Something's still holding the implicit swapchain.\n");
1204 HeapFree(GetProcessHeap(), 0, device->swapchains);
1205 device->swapchains = NULL;
1206 device->swapchain_count = 0;
1207 return WINED3D_OK;
1210 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1211 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1212 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1214 * There is no way to deactivate thread safety once it is enabled.
1216 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1218 TRACE("device %p.\n", device);
1220 /* For now just store the flag (needed in case of ddraw). */
1221 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1224 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1226 TRACE("device %p.\n", device);
1228 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1229 wine_dbgstr_longlong(device->adapter->vram_bytes),
1230 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1231 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1233 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1236 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1237 struct wined3d_buffer *buffer, UINT offset)
1239 struct wined3d_stream_output *stream;
1240 struct wined3d_buffer *prev_buffer;
1242 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1244 if (idx >= MAX_STREAM_OUT)
1246 WARN("Invalid stream output %u.\n", idx);
1247 return;
1250 stream = &device->update_state->stream_output[idx];
1251 prev_buffer = stream->buffer;
1253 if (buffer)
1254 wined3d_buffer_incref(buffer);
1255 stream->buffer = buffer;
1256 stream->offset = offset;
1257 if (!device->recording)
1258 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1259 if (prev_buffer)
1260 wined3d_buffer_decref(prev_buffer);
1263 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1264 UINT idx, UINT *offset)
1266 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1268 if (idx >= MAX_STREAM_OUT)
1270 WARN("Invalid stream output %u.\n", idx);
1271 return NULL;
1274 if (offset)
1275 *offset = device->state.stream_output[idx].offset;
1276 return device->state.stream_output[idx].buffer;
1279 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1280 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1282 struct wined3d_stream_state *stream;
1283 struct wined3d_buffer *prev_buffer;
1285 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1286 device, stream_idx, buffer, offset, stride);
1288 if (stream_idx >= MAX_STREAMS)
1290 WARN("Stream index %u out of range.\n", stream_idx);
1291 return WINED3DERR_INVALIDCALL;
1293 else if (offset & 0x3)
1295 WARN("Offset %u is not 4 byte aligned.\n", offset);
1296 return WINED3DERR_INVALIDCALL;
1299 stream = &device->update_state->streams[stream_idx];
1300 prev_buffer = stream->buffer;
1302 if (device->recording)
1303 device->recording->changed.streamSource |= 1u << stream_idx;
1305 if (prev_buffer == buffer
1306 && stream->stride == stride
1307 && stream->offset == offset)
1309 TRACE("Application is setting the old values over, nothing to do.\n");
1310 return WINED3D_OK;
1313 stream->buffer = buffer;
1314 if (buffer)
1316 stream->stride = stride;
1317 stream->offset = offset;
1318 wined3d_buffer_incref(buffer);
1321 if (!device->recording)
1322 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1323 if (prev_buffer)
1324 wined3d_buffer_decref(prev_buffer);
1326 return WINED3D_OK;
1329 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1330 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1332 const struct wined3d_stream_state *stream;
1334 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1335 device, stream_idx, buffer, offset, stride);
1337 if (stream_idx >= MAX_STREAMS)
1339 WARN("Stream index %u out of range.\n", stream_idx);
1340 return WINED3DERR_INVALIDCALL;
1343 stream = &device->state.streams[stream_idx];
1344 *buffer = stream->buffer;
1345 if (offset)
1346 *offset = stream->offset;
1347 *stride = stream->stride;
1349 return WINED3D_OK;
1352 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1354 struct wined3d_stream_state *stream;
1355 UINT old_flags, old_freq;
1357 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1359 /* Verify input. At least in d3d9 this is invalid. */
1360 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1362 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1363 return WINED3DERR_INVALIDCALL;
1365 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1367 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1368 return WINED3DERR_INVALIDCALL;
1370 if (!divider)
1372 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1373 return WINED3DERR_INVALIDCALL;
1376 stream = &device->update_state->streams[stream_idx];
1377 old_flags = stream->flags;
1378 old_freq = stream->frequency;
1380 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1381 stream->frequency = divider & 0x7fffff;
1383 if (device->recording)
1384 device->recording->changed.streamFreq |= 1u << stream_idx;
1385 else if (stream->frequency != old_freq || stream->flags != old_flags)
1386 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1388 return WINED3D_OK;
1391 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1392 UINT stream_idx, UINT *divider)
1394 const struct wined3d_stream_state *stream;
1396 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1398 stream = &device->state.streams[stream_idx];
1399 *divider = stream->flags | stream->frequency;
1401 TRACE("Returning %#x.\n", *divider);
1403 return WINED3D_OK;
1406 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1407 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1409 TRACE("device %p, state %s, matrix %p.\n",
1410 device, debug_d3dtstype(d3dts), matrix);
1411 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1412 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1413 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1414 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1416 /* Handle recording of state blocks. */
1417 if (device->recording)
1419 TRACE("Recording... not performing anything.\n");
1420 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1421 device->update_state->transforms[d3dts] = *matrix;
1422 return;
1425 /* If the new matrix is the same as the current one,
1426 * we cut off any further processing. this seems to be a reasonable
1427 * optimization because as was noticed, some apps (warcraft3 for example)
1428 * tend towards setting the same matrix repeatedly for some reason.
1430 * From here on we assume that the new matrix is different, wherever it matters. */
1431 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1433 TRACE("The application is setting the same matrix over again.\n");
1434 return;
1437 device->state.transforms[d3dts] = *matrix;
1438 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1441 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1442 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1444 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1446 *matrix = device->state.transforms[state];
1449 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1450 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1452 const struct wined3d_matrix *mat;
1453 struct wined3d_matrix temp;
1455 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1457 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1458 * below means it will be recorded in a state block change, but it
1459 * works regardless where it is recorded.
1460 * If this is found to be wrong, change to StateBlock. */
1461 if (state > HIGHEST_TRANSFORMSTATE)
1463 WARN("Unhandled transform state %#x.\n", state);
1464 return;
1467 mat = &device->update_state->transforms[state];
1468 multiply_matrix(&temp, mat, matrix);
1470 /* Apply change via set transform - will reapply to eg. lights this way. */
1471 wined3d_device_set_transform(device, state, &temp);
1474 /* Note lights are real special cases. Although the device caps state only
1475 * e.g. 8 are supported, you can reference any indexes you want as long as
1476 * that number max are enabled at any one point in time. Therefore since the
1477 * indices can be anything, we need a hashmap of them. However, this causes
1478 * stateblock problems. When capturing the state block, I duplicate the
1479 * hashmap, but when recording, just build a chain pretty much of commands to
1480 * be replayed. */
1481 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1482 UINT light_idx, const struct wined3d_light *light)
1484 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1485 struct wined3d_light_info *object = NULL;
1486 struct list *e;
1487 float rho;
1489 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1491 /* Check the parameter range. Need for speed most wanted sets junk lights
1492 * which confuse the GL driver. */
1493 if (!light)
1494 return WINED3DERR_INVALIDCALL;
1496 switch (light->type)
1498 case WINED3D_LIGHT_POINT:
1499 case WINED3D_LIGHT_SPOT:
1500 case WINED3D_LIGHT_GLSPOT:
1501 /* Incorrect attenuation values can cause the gl driver to crash.
1502 * Happens with Need for speed most wanted. */
1503 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1505 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1506 return WINED3DERR_INVALIDCALL;
1508 break;
1510 case WINED3D_LIGHT_DIRECTIONAL:
1511 case WINED3D_LIGHT_PARALLELPOINT:
1512 /* Ignores attenuation */
1513 break;
1515 default:
1516 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1517 return WINED3DERR_INVALIDCALL;
1520 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1522 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1523 if (object->OriginalIndex == light_idx)
1524 break;
1525 object = NULL;
1528 if (!object)
1530 TRACE("Adding new light\n");
1531 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1532 if (!object)
1533 return E_OUTOFMEMORY;
1535 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1536 object->glIndex = -1;
1537 object->OriginalIndex = light_idx;
1540 /* Initialize the object. */
1541 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1542 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1543 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1544 light_idx, light->type, debug_color(&light->diffuse),
1545 debug_color(&light->specular), debug_color(&light->ambient),
1546 light->position.x, light->position.y, light->position.z,
1547 light->direction.x, light->direction.y, light->direction.z,
1548 light->range, light->falloff, light->theta, light->phi);
1550 /* Update the live definitions if the light is currently assigned a glIndex. */
1551 if (object->glIndex != -1 && !device->recording)
1553 if (object->OriginalParms.type != light->type)
1554 device_invalidate_state(device, STATE_LIGHT_TYPE);
1555 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1558 /* Save away the information. */
1559 object->OriginalParms = *light;
1561 switch (light->type)
1563 case WINED3D_LIGHT_POINT:
1564 /* Position */
1565 object->position.x = light->position.x;
1566 object->position.y = light->position.y;
1567 object->position.z = light->position.z;
1568 object->position.w = 1.0f;
1569 object->cutoff = 180.0f;
1570 /* FIXME: Range */
1571 break;
1573 case WINED3D_LIGHT_DIRECTIONAL:
1574 /* Direction */
1575 object->direction.x = -light->direction.x;
1576 object->direction.y = -light->direction.y;
1577 object->direction.z = -light->direction.z;
1578 object->direction.w = 0.0f;
1579 object->exponent = 0.0f;
1580 object->cutoff = 180.0f;
1581 break;
1583 case WINED3D_LIGHT_SPOT:
1584 /* Position */
1585 object->position.x = light->position.x;
1586 object->position.y = light->position.y;
1587 object->position.z = light->position.z;
1588 object->position.w = 1.0f;
1590 /* Direction */
1591 object->direction.x = light->direction.x;
1592 object->direction.y = light->direction.y;
1593 object->direction.z = light->direction.z;
1594 object->direction.w = 0.0f;
1596 /* opengl-ish and d3d-ish spot lights use too different models
1597 * for the light "intensity" as a function of the angle towards
1598 * the main light direction, so we only can approximate very
1599 * roughly. However, spot lights are rather rarely used in games
1600 * (if ever used at all). Furthermore if still used, probably
1601 * nobody pays attention to such details. */
1602 if (!light->falloff)
1604 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1605 * equations have the falloff resp. exponent parameter as an
1606 * exponent, so the spot light lighting will always be 1.0 for
1607 * both of them, and we don't have to care for the rest of the
1608 * rather complex calculation. */
1609 object->exponent = 0.0f;
1611 else
1613 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1614 if (rho < 0.0001f)
1615 rho = 0.0001f;
1616 object->exponent = -0.3f / logf(cosf(rho / 2));
1619 if (object->exponent > 128.0f)
1620 object->exponent = 128.0f;
1622 object->cutoff = (float)(light->phi * 90 / M_PI);
1623 /* FIXME: Range */
1624 break;
1626 case WINED3D_LIGHT_PARALLELPOINT:
1627 object->position.x = light->position.x;
1628 object->position.y = light->position.y;
1629 object->position.z = light->position.z;
1630 object->position.w = 1.0f;
1631 break;
1633 default:
1634 FIXME("Unrecognized light type %#x.\n", light->type);
1637 return WINED3D_OK;
1640 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1641 UINT light_idx, struct wined3d_light *light)
1643 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1644 struct wined3d_light_info *light_info = NULL;
1645 struct list *e;
1647 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1649 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1651 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1652 if (light_info->OriginalIndex == light_idx)
1653 break;
1654 light_info = NULL;
1657 if (!light_info)
1659 TRACE("Light information requested but light not defined\n");
1660 return WINED3DERR_INVALIDCALL;
1663 *light = light_info->OriginalParms;
1664 return WINED3D_OK;
1667 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1669 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1670 struct wined3d_light_info *light_info = NULL;
1671 struct list *e;
1673 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1675 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1677 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1678 if (light_info->OriginalIndex == light_idx)
1679 break;
1680 light_info = NULL;
1682 TRACE("Found light %p.\n", light_info);
1684 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1685 if (!light_info)
1687 TRACE("Light enabled requested but light not defined, so defining one!\n");
1688 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1690 /* Search for it again! Should be fairly quick as near head of list. */
1691 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1693 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1694 if (light_info->OriginalIndex == light_idx)
1695 break;
1696 light_info = NULL;
1698 if (!light_info)
1700 FIXME("Adding default lights has failed dismally\n");
1701 return WINED3DERR_INVALIDCALL;
1705 if (!enable)
1707 if (light_info->glIndex != -1)
1709 if (!device->recording)
1711 device_invalidate_state(device, STATE_LIGHT_TYPE);
1712 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1715 device->update_state->lights[light_info->glIndex] = NULL;
1716 light_info->glIndex = -1;
1718 else
1720 TRACE("Light already disabled, nothing to do\n");
1722 light_info->enabled = FALSE;
1724 else
1726 light_info->enabled = TRUE;
1727 if (light_info->glIndex != -1)
1729 TRACE("Nothing to do as light was enabled\n");
1731 else
1733 unsigned int i;
1734 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1735 /* Find a free GL light. */
1736 for (i = 0; i < gl_info->limits.lights; ++i)
1738 if (!device->update_state->lights[i])
1740 device->update_state->lights[i] = light_info;
1741 light_info->glIndex = i;
1742 break;
1745 if (light_info->glIndex == -1)
1747 /* Our tests show that Windows returns D3D_OK in this situation, even with
1748 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1749 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1750 * as well for those lights.
1752 * TODO: Test how this affects rendering. */
1753 WARN("Too many concurrently active lights\n");
1754 return WINED3D_OK;
1757 /* i == light_info->glIndex */
1758 if (!device->recording)
1760 device_invalidate_state(device, STATE_LIGHT_TYPE);
1761 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1766 return WINED3D_OK;
1769 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1771 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1772 struct wined3d_light_info *light_info = NULL;
1773 struct list *e;
1775 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1777 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1779 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1780 if (light_info->OriginalIndex == light_idx)
1781 break;
1782 light_info = NULL;
1785 if (!light_info)
1787 TRACE("Light enabled state requested but light not defined.\n");
1788 return WINED3DERR_INVALIDCALL;
1790 /* true is 128 according to SetLightEnable */
1791 *enable = light_info->enabled ? 128 : 0;
1792 return WINED3D_OK;
1795 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1796 UINT plane_idx, const struct wined3d_vec4 *plane)
1798 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1800 /* Validate plane_idx. */
1801 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1803 TRACE("Application has requested clipplane this device doesn't support.\n");
1804 return WINED3DERR_INVALIDCALL;
1807 if (device->recording)
1808 device->recording->changed.clipplane |= 1u << plane_idx;
1810 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1812 TRACE("Application is setting old values over, nothing to do.\n");
1813 return WINED3D_OK;
1816 device->update_state->clip_planes[plane_idx] = *plane;
1818 if (!device->recording)
1819 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1821 return WINED3D_OK;
1824 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1825 UINT plane_idx, struct wined3d_vec4 *plane)
1827 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1829 /* Validate plane_idx. */
1830 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1832 TRACE("Application has requested clipplane this device doesn't support.\n");
1833 return WINED3DERR_INVALIDCALL;
1836 *plane = device->state.clip_planes[plane_idx];
1838 return WINED3D_OK;
1841 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1842 const struct wined3d_clip_status *clip_status)
1844 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1846 if (!clip_status)
1847 return WINED3DERR_INVALIDCALL;
1849 return WINED3D_OK;
1852 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1853 struct wined3d_clip_status *clip_status)
1855 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1857 if (!clip_status)
1858 return WINED3DERR_INVALIDCALL;
1860 return WINED3D_OK;
1863 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1865 TRACE("device %p, material %p.\n", device, material);
1867 device->update_state->material = *material;
1869 if (device->recording)
1870 device->recording->changed.material = TRUE;
1871 else
1872 wined3d_cs_emit_set_material(device->cs, material);
1875 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1877 TRACE("device %p, material %p.\n", device, material);
1879 *material = device->state.material;
1881 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1882 TRACE("ambient %s\n", debug_color(&material->ambient));
1883 TRACE("specular %s\n", debug_color(&material->specular));
1884 TRACE("emissive %s\n", debug_color(&material->emissive));
1885 TRACE("power %.8e.\n", material->power);
1888 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1889 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1891 enum wined3d_format_id prev_format;
1892 struct wined3d_buffer *prev_buffer;
1893 unsigned int prev_offset;
1895 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1896 device, buffer, debug_d3dformat(format_id), offset);
1898 prev_buffer = device->update_state->index_buffer;
1899 prev_format = device->update_state->index_format;
1900 prev_offset = device->update_state->index_offset;
1902 device->update_state->index_buffer = buffer;
1903 device->update_state->index_format = format_id;
1904 device->update_state->index_offset = offset;
1906 if (device->recording)
1907 device->recording->changed.indices = TRUE;
1909 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1910 return;
1912 if (buffer)
1913 wined3d_buffer_incref(buffer);
1914 if (!device->recording)
1915 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1916 if (prev_buffer)
1917 wined3d_buffer_decref(prev_buffer);
1920 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1921 enum wined3d_format_id *format, unsigned int *offset)
1923 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1925 *format = device->state.index_format;
1926 if (offset)
1927 *offset = device->state.index_offset;
1928 return device->state.index_buffer;
1931 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1933 TRACE("device %p, base_index %d.\n", device, base_index);
1935 device->update_state->base_vertex_index = base_index;
1938 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1940 TRACE("device %p.\n", device);
1942 return device->state.base_vertex_index;
1945 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1947 TRACE("device %p, viewport %p.\n", device, viewport);
1948 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1949 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1951 device->update_state->viewport = *viewport;
1953 /* Handle recording of state blocks */
1954 if (device->recording)
1956 TRACE("Recording... not performing anything\n");
1957 device->recording->changed.viewport = TRUE;
1958 return;
1961 wined3d_cs_emit_set_viewport(device->cs, viewport);
1964 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1966 TRACE("device %p, viewport %p.\n", device, viewport);
1968 *viewport = device->state.viewport;
1971 static void resolve_depth_buffer(struct wined3d_state *state)
1973 struct wined3d_texture *dst_texture = state->textures[0];
1974 struct wined3d_rendertarget_view *src_view;
1975 RECT src_rect, dst_rect;
1977 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1978 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
1979 return;
1981 if (!(src_view = state->fb->depth_stencil))
1982 return;
1983 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
1985 FIXME("Not supported on buffer resources.\n");
1986 return;
1989 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
1990 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
1991 wined3d_texture_blt(dst_texture, 0, &dst_rect, texture_from_resource(src_view->resource),
1992 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
1995 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1996 struct wined3d_rasterizer_state *rasterizer_state)
1998 struct wined3d_rasterizer_state *prev;
2000 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2002 prev = device->update_state->rasterizer_state;
2003 if (prev == rasterizer_state)
2004 return;
2006 if (rasterizer_state)
2007 wined3d_rasterizer_state_incref(rasterizer_state);
2008 device->update_state->rasterizer_state = rasterizer_state;
2009 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2010 if (prev)
2011 wined3d_rasterizer_state_decref(prev);
2014 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2016 TRACE("device %p.\n", device);
2018 return device->state.rasterizer_state;
2021 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2022 enum wined3d_render_state state, DWORD value)
2024 DWORD old_value;
2026 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2028 if (state > WINEHIGHEST_RENDER_STATE)
2030 WARN("Unhandled render state %#x.\n", state);
2031 return;
2034 old_value = device->state.render_states[state];
2035 device->update_state->render_states[state] = value;
2037 /* Handle recording of state blocks. */
2038 if (device->recording)
2040 TRACE("Recording... not performing anything.\n");
2041 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2042 return;
2045 /* Compared here and not before the assignment to allow proper stateblock recording. */
2046 if (value == old_value)
2047 TRACE("Application is setting the old value over, nothing to do.\n");
2048 else
2049 wined3d_cs_emit_set_render_state(device->cs, state, value);
2051 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2053 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2054 resolve_depth_buffer(&device->state);
2058 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2060 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2062 return device->state.render_states[state];
2065 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2066 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2068 DWORD old_value;
2070 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2071 device, sampler_idx, debug_d3dsamplerstate(state), value);
2073 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2074 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2076 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2078 WARN("Invalid sampler %u.\n", sampler_idx);
2079 return; /* Windows accepts overflowing this array ... we do not. */
2082 old_value = device->state.sampler_states[sampler_idx][state];
2083 device->update_state->sampler_states[sampler_idx][state] = value;
2085 /* Handle recording of state blocks. */
2086 if (device->recording)
2088 TRACE("Recording... not performing anything.\n");
2089 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2090 return;
2093 if (old_value == value)
2095 TRACE("Application is setting the old value over, nothing to do.\n");
2096 return;
2099 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2102 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2103 UINT sampler_idx, enum wined3d_sampler_state state)
2105 TRACE("device %p, sampler_idx %u, state %s.\n",
2106 device, sampler_idx, debug_d3dsamplerstate(state));
2108 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2109 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2111 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2113 WARN("Invalid sampler %u.\n", sampler_idx);
2114 return 0; /* Windows accepts overflowing this array ... we do not. */
2117 return device->state.sampler_states[sampler_idx][state];
2120 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2122 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2124 if (device->recording)
2125 device->recording->changed.scissorRect = TRUE;
2127 if (EqualRect(&device->update_state->scissor_rect, rect))
2129 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2130 return;
2132 CopyRect(&device->update_state->scissor_rect, rect);
2134 if (device->recording)
2136 TRACE("Recording... not performing anything.\n");
2137 return;
2140 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2143 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2145 TRACE("device %p, rect %p.\n", device, rect);
2147 *rect = device->state.scissor_rect;
2148 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2151 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2152 struct wined3d_vertex_declaration *declaration)
2154 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2156 TRACE("device %p, declaration %p.\n", device, declaration);
2158 if (device->recording)
2159 device->recording->changed.vertexDecl = TRUE;
2161 if (declaration == prev)
2162 return;
2164 if (declaration)
2165 wined3d_vertex_declaration_incref(declaration);
2166 device->update_state->vertex_declaration = declaration;
2167 if (!device->recording)
2168 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2169 if (prev)
2170 wined3d_vertex_declaration_decref(prev);
2173 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2175 TRACE("device %p.\n", device);
2177 return device->state.vertex_declaration;
2180 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2182 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2184 TRACE("device %p, shader %p.\n", device, shader);
2186 if (device->recording)
2187 device->recording->changed.vertexShader = TRUE;
2189 if (shader == prev)
2190 return;
2192 if (shader)
2193 wined3d_shader_incref(shader);
2194 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2195 if (!device->recording)
2196 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2197 if (prev)
2198 wined3d_shader_decref(prev);
2201 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2203 TRACE("device %p.\n", device);
2205 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2208 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2209 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2211 struct wined3d_buffer *prev;
2213 if (idx >= MAX_CONSTANT_BUFFERS)
2215 WARN("Invalid constant buffer index %u.\n", idx);
2216 return;
2219 prev = device->update_state->cb[type][idx];
2220 if (buffer == prev)
2221 return;
2223 if (buffer)
2224 wined3d_buffer_incref(buffer);
2225 device->update_state->cb[type][idx] = buffer;
2226 if (!device->recording)
2227 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2228 if (prev)
2229 wined3d_buffer_decref(prev);
2232 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2234 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2236 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2239 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2241 TRACE("device %p, idx %u.\n", device, idx);
2243 if (idx >= MAX_CONSTANT_BUFFERS)
2245 WARN("Invalid constant buffer index %u.\n", idx);
2246 return NULL;
2249 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2252 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2253 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2255 struct wined3d_shader_resource_view *prev;
2257 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2259 WARN("Invalid view index %u.\n", idx);
2260 return;
2263 prev = device->update_state->shader_resource_view[type][idx];
2264 if (view == prev)
2265 return;
2267 if (view)
2268 wined3d_shader_resource_view_incref(view);
2269 device->update_state->shader_resource_view[type][idx] = view;
2270 if (!device->recording)
2271 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2272 if (prev)
2273 wined3d_shader_resource_view_decref(prev);
2276 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2277 UINT idx, struct wined3d_shader_resource_view *view)
2279 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2281 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2284 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2285 UINT idx)
2287 TRACE("device %p, idx %u.\n", device, idx);
2289 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2291 WARN("Invalid view index %u.\n", idx);
2292 return NULL;
2295 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2298 static void wined3d_device_set_sampler(struct wined3d_device *device,
2299 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2301 struct wined3d_sampler *prev;
2303 if (idx >= MAX_SAMPLER_OBJECTS)
2305 WARN("Invalid sampler index %u.\n", idx);
2306 return;
2309 prev = device->update_state->sampler[type][idx];
2310 if (sampler == prev)
2311 return;
2313 if (sampler)
2314 wined3d_sampler_incref(sampler);
2315 device->update_state->sampler[type][idx] = sampler;
2316 if (!device->recording)
2317 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2318 if (prev)
2319 wined3d_sampler_decref(prev);
2322 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2324 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2326 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2329 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2331 TRACE("device %p, idx %u.\n", device, idx);
2333 if (idx >= MAX_SAMPLER_OBJECTS)
2335 WARN("Invalid sampler index %u.\n", idx);
2336 return NULL;
2339 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2342 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2343 unsigned int start_idx, unsigned int count, const BOOL *constants)
2345 unsigned int i;
2347 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2348 device, start_idx, count, constants);
2350 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2351 return WINED3DERR_INVALIDCALL;
2353 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2354 count = WINED3D_MAX_CONSTS_B - start_idx;
2355 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2356 if (TRACE_ON(d3d))
2358 for (i = 0; i < count; ++i)
2359 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2362 if (device->recording)
2364 for (i = start_idx; i < count + start_idx; ++i)
2365 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2367 else
2369 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2372 return WINED3D_OK;
2375 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2376 unsigned int start_idx, unsigned int count, BOOL *constants)
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(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2388 return WINED3D_OK;
2391 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2392 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2394 unsigned int i;
2396 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2397 device, start_idx, count, constants);
2399 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2400 return WINED3DERR_INVALIDCALL;
2402 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2403 count = WINED3D_MAX_CONSTS_I - start_idx;
2404 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2405 if (TRACE_ON(d3d))
2407 for (i = 0; i < count; ++i)
2408 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2411 if (device->recording)
2413 for (i = start_idx; i < count + start_idx; ++i)
2414 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2416 else
2418 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2421 return WINED3D_OK;
2424 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2425 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
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(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2436 return WINED3D_OK;
2439 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2440 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2442 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2443 unsigned int i;
2445 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2446 device, start_idx, count, constants);
2448 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2449 || count > d3d_info->limits.vs_uniform_count - start_idx)
2450 return WINED3DERR_INVALIDCALL;
2452 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2453 if (TRACE_ON(d3d))
2455 for (i = 0; i < count; ++i)
2456 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2459 if (device->recording)
2460 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2461 count * sizeof(*device->recording->changed.vs_consts_f));
2462 else
2463 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2465 return WINED3D_OK;
2468 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2469 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2471 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2473 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2474 device, start_idx, count, constants);
2476 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2477 || count > d3d_info->limits.vs_uniform_count - start_idx)
2478 return WINED3DERR_INVALIDCALL;
2480 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2482 return WINED3D_OK;
2485 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2487 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2489 TRACE("device %p, shader %p.\n", device, shader);
2491 if (device->recording)
2492 device->recording->changed.pixelShader = TRUE;
2494 if (shader == prev)
2495 return;
2497 if (shader)
2498 wined3d_shader_incref(shader);
2499 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2500 if (!device->recording)
2501 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2502 if (prev)
2503 wined3d_shader_decref(prev);
2506 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2508 TRACE("device %p.\n", device);
2510 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2513 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2515 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2517 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2520 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2522 TRACE("device %p, idx %u.\n", device, idx);
2524 if (idx >= MAX_CONSTANT_BUFFERS)
2526 WARN("Invalid constant buffer index %u.\n", idx);
2527 return NULL;
2530 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2533 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2534 UINT idx, struct wined3d_shader_resource_view *view)
2536 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2538 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2541 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2542 UINT idx)
2544 TRACE("device %p, idx %u.\n", device, idx);
2546 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2548 WARN("Invalid view index %u.\n", idx);
2549 return NULL;
2552 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2555 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2557 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2559 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2562 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2564 TRACE("device %p, idx %u.\n", device, idx);
2566 if (idx >= MAX_SAMPLER_OBJECTS)
2568 WARN("Invalid sampler index %u.\n", idx);
2569 return NULL;
2572 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2575 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2576 unsigned int start_idx, unsigned int count, const BOOL *constants)
2578 unsigned int i;
2580 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2581 device, start_idx, count, constants);
2583 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2584 return WINED3DERR_INVALIDCALL;
2586 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2587 count = WINED3D_MAX_CONSTS_B - start_idx;
2588 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2589 if (TRACE_ON(d3d))
2591 for (i = 0; i < count; ++i)
2592 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2595 if (device->recording)
2597 for (i = start_idx; i < count + start_idx; ++i)
2598 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2600 else
2602 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2605 return WINED3D_OK;
2608 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2609 unsigned int start_idx, unsigned int count, BOOL *constants)
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(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2621 return WINED3D_OK;
2624 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2625 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2627 unsigned int i;
2629 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2630 device, start_idx, count, constants);
2632 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2633 return WINED3DERR_INVALIDCALL;
2635 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2636 count = WINED3D_MAX_CONSTS_I - start_idx;
2637 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2638 if (TRACE_ON(d3d))
2640 for (i = 0; i < count; ++i)
2641 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2644 if (device->recording)
2646 for (i = start_idx; i < count + start_idx; ++i)
2647 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2649 else
2651 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2654 return WINED3D_OK;
2657 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2658 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
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(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2670 return WINED3D_OK;
2673 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2674 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2676 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2677 unsigned int i;
2679 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2680 device, start_idx, count, constants);
2682 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2683 || count > d3d_info->limits.ps_uniform_count - start_idx)
2684 return WINED3DERR_INVALIDCALL;
2686 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2687 if (TRACE_ON(d3d))
2689 for (i = 0; i < count; ++i)
2690 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2693 if (device->recording)
2694 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2695 count * sizeof(*device->recording->changed.ps_consts_f));
2696 else
2697 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2699 return WINED3D_OK;
2702 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2703 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2705 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2707 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2708 device, start_idx, count, constants);
2710 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2711 || count > d3d_info->limits.ps_uniform_count - start_idx)
2712 return WINED3DERR_INVALIDCALL;
2714 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2716 return WINED3D_OK;
2719 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2721 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2723 TRACE("device %p, shader %p.\n", device, shader);
2725 if (device->recording || shader == prev)
2726 return;
2727 if (shader)
2728 wined3d_shader_incref(shader);
2729 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2730 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2731 if (prev)
2732 wined3d_shader_decref(prev);
2735 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2737 TRACE("device %p.\n", device);
2739 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2742 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2744 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2746 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2749 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2751 TRACE("device %p, idx %u.\n", device, idx);
2753 if (idx >= MAX_CONSTANT_BUFFERS)
2755 WARN("Invalid constant buffer index %u.\n", idx);
2756 return NULL;
2759 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2762 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2763 UINT idx, struct wined3d_shader_resource_view *view)
2765 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2767 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2770 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2771 UINT idx)
2773 TRACE("device %p, idx %u.\n", device, idx);
2775 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2777 WARN("Invalid view index %u.\n", idx);
2778 return NULL;
2781 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2784 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2786 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2788 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2791 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2793 TRACE("device %p, idx %u.\n", device, idx);
2795 if (idx >= MAX_SAMPLER_OBJECTS)
2797 WARN("Invalid sampler index %u.\n", idx);
2798 return NULL;
2801 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2804 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2806 struct wined3d_shader *prev;
2808 TRACE("device %p, shader %p.\n", device, shader);
2810 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2811 if (device->recording || shader == prev)
2812 return;
2813 if (shader)
2814 wined3d_shader_incref(shader);
2815 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2816 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2817 if (prev)
2818 wined3d_shader_decref(prev);
2821 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2822 unsigned int idx, struct wined3d_unordered_access_view *uav)
2824 struct wined3d_unordered_access_view *prev;
2826 TRACE("device %p, idx %u, uav %p.\n", device, idx, uav);
2828 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2830 WARN("Invalid UAV index %u.\n", idx);
2831 return;
2834 prev = device->update_state->unordered_access_view[idx];
2835 if (uav == prev)
2836 return;
2838 if (uav)
2839 wined3d_unordered_access_view_incref(uav);
2840 device->update_state->unordered_access_view[idx] = uav;
2841 if (!device->recording)
2842 wined3d_cs_emit_set_unordered_access_view(device->cs, idx, uav);
2843 if (prev)
2844 wined3d_unordered_access_view_decref(prev);
2847 /* Context activation is done by the caller. */
2848 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2849 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2850 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2851 DWORD DestFVF)
2853 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2854 struct wined3d_map_desc map_desc;
2855 struct wined3d_box box = {0};
2856 struct wined3d_viewport vp;
2857 UINT vertex_size;
2858 unsigned int i;
2859 BYTE *dest_ptr;
2860 BOOL doClip;
2861 DWORD numTextures;
2862 HRESULT hr;
2864 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2866 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2869 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2871 ERR("Source has no position mask\n");
2872 return WINED3DERR_INVALIDCALL;
2875 if (device->state.render_states[WINED3D_RS_CLIPPING])
2877 static BOOL warned = FALSE;
2879 * The clipping code is not quite correct. Some things need
2880 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2881 * so disable clipping for now.
2882 * (The graphics in Half-Life are broken, and my processvertices
2883 * test crashes with IDirect3DDevice3)
2884 doClip = TRUE;
2886 doClip = FALSE;
2887 if(!warned) {
2888 warned = TRUE;
2889 FIXME("Clipping is broken and disabled for now\n");
2892 else
2893 doClip = FALSE;
2895 vertex_size = get_flexible_vertex_size(DestFVF);
2896 box.left = dwDestIndex * vertex_size;
2897 box.right = box.left + dwCount * vertex_size;
2898 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, 0)))
2900 WARN("Failed to map buffer, hr %#x.\n", hr);
2901 return hr;
2903 dest_ptr = map_desc.data;
2905 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2906 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2907 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2909 TRACE("View mat:\n");
2910 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2911 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2912 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2913 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2915 TRACE("Proj mat:\n");
2916 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2917 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2918 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2919 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2921 TRACE("World mat:\n");
2922 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2923 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2924 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2925 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2927 /* Get the viewport */
2928 wined3d_device_get_viewport(device, &vp);
2929 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2930 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2932 multiply_matrix(&mat,&view_mat,&world_mat);
2933 multiply_matrix(&mat,&proj_mat,&mat);
2935 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2937 for (i = 0; i < dwCount; i+= 1) {
2938 unsigned int tex_index;
2940 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2941 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2942 /* The position first */
2943 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2944 const float *p = (const float *)(element->data.addr + i * element->stride);
2945 float x, y, z, rhw;
2946 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2948 /* Multiplication with world, view and projection matrix. */
2949 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2950 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2951 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2952 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2954 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2956 /* WARNING: The following things are taken from d3d7 and were not yet checked
2957 * against d3d8 or d3d9!
2960 /* Clipping conditions: From msdn
2962 * A vertex is clipped if it does not match the following requirements
2963 * -rhw < x <= rhw
2964 * -rhw < y <= rhw
2965 * 0 < z <= rhw
2966 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2968 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2969 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2973 if( !doClip ||
2974 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2975 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2976 ( rhw > eps ) ) ) {
2978 /* "Normal" viewport transformation (not clipped)
2979 * 1) The values are divided by rhw
2980 * 2) The y axis is negative, so multiply it with -1
2981 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2982 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2983 * 4) Multiply x with Width/2 and add Width/2
2984 * 5) The same for the height
2985 * 6) Add the viewpoint X and Y to the 2D coordinates and
2986 * The minimum Z value to z
2987 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2989 * Well, basically it's simply a linear transformation into viewport
2990 * coordinates
2993 x /= rhw;
2994 y /= rhw;
2995 z /= rhw;
2997 y *= -1;
2999 x *= vp.width / 2;
3000 y *= vp.height / 2;
3001 z *= vp.max_z - vp.min_z;
3003 x += vp.width / 2 + vp.x;
3004 y += vp.height / 2 + vp.y;
3005 z += vp.min_z;
3007 rhw = 1 / rhw;
3008 } else {
3009 /* That vertex got clipped
3010 * Contrary to OpenGL it is not dropped completely, it just
3011 * undergoes a different calculation.
3013 TRACE("Vertex got clipped\n");
3014 x += rhw;
3015 y += rhw;
3017 x /= 2;
3018 y /= 2;
3020 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3021 * outside of the main vertex buffer memory. That needs some more
3022 * investigation...
3026 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3029 ( (float *) dest_ptr)[0] = x;
3030 ( (float *) dest_ptr)[1] = y;
3031 ( (float *) dest_ptr)[2] = z;
3032 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3034 dest_ptr += 3 * sizeof(float);
3036 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3037 dest_ptr += sizeof(float);
3040 if (DestFVF & WINED3DFVF_PSIZE)
3041 dest_ptr += sizeof(DWORD);
3043 if (DestFVF & WINED3DFVF_NORMAL)
3045 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3046 const float *normal = (const float *)(element->data.addr + i * element->stride);
3047 /* AFAIK this should go into the lighting information */
3048 FIXME("Didn't expect the destination to have a normal\n");
3049 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3052 if (DestFVF & WINED3DFVF_DIFFUSE)
3054 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3055 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3056 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3058 static BOOL warned = FALSE;
3060 if(!warned) {
3061 ERR("No diffuse color in source, but destination has one\n");
3062 warned = TRUE;
3065 *( (DWORD *) dest_ptr) = 0xffffffff;
3066 dest_ptr += sizeof(DWORD);
3068 else
3070 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3074 if (DestFVF & WINED3DFVF_SPECULAR)
3076 /* What's the color value in the feedback buffer? */
3077 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3078 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3079 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3081 static BOOL warned = FALSE;
3083 if(!warned) {
3084 ERR("No specular color in source, but destination has one\n");
3085 warned = TRUE;
3088 *(DWORD *)dest_ptr = 0xff000000;
3089 dest_ptr += sizeof(DWORD);
3091 else
3093 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3097 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3099 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3100 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3101 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3103 ERR("No source texture, but destination requests one\n");
3104 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3106 else
3108 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3113 wined3d_resource_unmap(&dest->resource, 0);
3115 return WINED3D_OK;
3117 #undef copy_and_next
3119 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3120 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3121 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3123 struct wined3d_state *state = &device->state;
3124 struct wined3d_stream_info stream_info;
3125 const struct wined3d_gl_info *gl_info;
3126 struct wined3d_context *context;
3127 struct wined3d_shader *vs;
3128 unsigned int i;
3129 HRESULT hr;
3130 WORD map;
3132 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3133 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3134 device, src_start_idx, dst_idx, vertex_count,
3135 dst_buffer, declaration, flags, dst_fvf);
3137 if (declaration)
3138 FIXME("Output vertex declaration not implemented yet.\n");
3140 /* Need any context to write to the vbo. */
3141 context = context_acquire(device, NULL);
3142 gl_info = context->gl_info;
3144 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3145 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3146 context_stream_info_from_declaration(context, state, &stream_info);
3147 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3149 /* We can't convert FROM a VBO, and vertex buffers used to source into
3150 * process_vertices() are unlikely to ever be used for drawing. Release
3151 * VBOs in those buffers and fix up the stream_info structure.
3153 * Also apply the start index. */
3154 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3156 struct wined3d_stream_info_element *e;
3157 struct wined3d_buffer *buffer;
3159 if (!(map & 1))
3160 continue;
3162 e = &stream_info.elements[i];
3163 buffer = state->streams[e->stream_idx].buffer;
3164 e->data.buffer_object = 0;
3165 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3166 if (buffer->buffer_object)
3168 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3169 buffer->buffer_object = 0;
3170 wined3d_buffer_invalidate_location(buffer, WINED3D_LOCATION_BUFFER);
3172 if (e->data.addr)
3173 e->data.addr += e->stride * src_start_idx;
3176 hr = process_vertices_strided(device, dst_idx, vertex_count,
3177 &stream_info, dst_buffer, flags, dst_fvf);
3179 context_release(context);
3181 return hr;
3184 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3185 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3187 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3188 DWORD old_value;
3190 TRACE("device %p, stage %u, state %s, value %#x.\n",
3191 device, stage, debug_d3dtexturestate(state), value);
3193 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3195 WARN("Invalid state %#x passed.\n", state);
3196 return;
3199 if (stage >= d3d_info->limits.ffp_blend_stages)
3201 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3202 stage, d3d_info->limits.ffp_blend_stages - 1);
3203 return;
3206 old_value = device->update_state->texture_states[stage][state];
3207 device->update_state->texture_states[stage][state] = value;
3209 if (device->recording)
3211 TRACE("Recording... not performing anything.\n");
3212 device->recording->changed.textureState[stage] |= 1u << state;
3213 return;
3216 /* Checked after the assignments to allow proper stateblock recording. */
3217 if (old_value == value)
3219 TRACE("Application is setting the old value over, nothing to do.\n");
3220 return;
3223 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3226 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3227 UINT stage, enum wined3d_texture_stage_state state)
3229 TRACE("device %p, stage %u, state %s.\n",
3230 device, stage, debug_d3dtexturestate(state));
3232 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3234 WARN("Invalid state %#x passed.\n", state);
3235 return 0;
3238 return device->state.texture_states[stage][state];
3241 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3242 UINT stage, struct wined3d_texture *texture)
3244 struct wined3d_texture *prev;
3246 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3248 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3249 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3251 /* Windows accepts overflowing this array... we do not. */
3252 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3254 WARN("Ignoring invalid stage %u.\n", stage);
3255 return WINED3D_OK;
3258 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3260 WARN("Rejecting attempt to set scratch texture.\n");
3261 return WINED3DERR_INVALIDCALL;
3264 if (device->recording)
3265 device->recording->changed.textures |= 1u << stage;
3267 prev = device->update_state->textures[stage];
3268 TRACE("Previous texture %p.\n", prev);
3270 if (texture == prev)
3272 TRACE("App is setting the same texture again, nothing to do.\n");
3273 return WINED3D_OK;
3276 TRACE("Setting new texture to %p.\n", texture);
3277 device->update_state->textures[stage] = texture;
3279 if (texture)
3280 wined3d_texture_incref(texture);
3281 if (!device->recording)
3282 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3283 if (prev)
3284 wined3d_texture_decref(prev);
3286 return WINED3D_OK;
3289 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3291 TRACE("device %p, stage %u.\n", device, stage);
3293 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3294 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3296 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3298 WARN("Ignoring invalid stage %u.\n", stage);
3299 return NULL; /* Windows accepts overflowing this array ... we do not. */
3302 return device->state.textures[stage];
3305 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3307 TRACE("device %p, caps %p.\n", device, caps);
3309 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3310 device->create_parms.device_type, caps);
3313 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3314 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3316 struct wined3d_swapchain *swapchain;
3318 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3319 device, swapchain_idx, mode, rotation);
3321 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3322 return WINED3DERR_INVALIDCALL;
3324 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3327 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3329 struct wined3d_stateblock *stateblock;
3330 HRESULT hr;
3332 TRACE("device %p.\n", device);
3334 if (device->recording)
3335 return WINED3DERR_INVALIDCALL;
3337 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3338 if (FAILED(hr))
3339 return hr;
3341 device->recording = stateblock;
3342 device->update_state = &stateblock->state;
3344 TRACE("Recording stateblock %p.\n", stateblock);
3346 return WINED3D_OK;
3349 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3350 struct wined3d_stateblock **stateblock)
3352 struct wined3d_stateblock *object = device->recording;
3354 TRACE("device %p, stateblock %p.\n", device, stateblock);
3356 if (!device->recording)
3358 WARN("Not recording.\n");
3359 *stateblock = NULL;
3360 return WINED3DERR_INVALIDCALL;
3363 stateblock_init_contained_states(object);
3365 *stateblock = object;
3366 device->recording = NULL;
3367 device->update_state = &device->state;
3369 TRACE("Returning stateblock %p.\n", *stateblock);
3371 return WINED3D_OK;
3374 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3376 /* At the moment we have no need for any functionality at the beginning
3377 * of a scene. */
3378 TRACE("device %p.\n", device);
3380 if (device->inScene)
3382 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3383 return WINED3DERR_INVALIDCALL;
3385 device->inScene = TRUE;
3386 return WINED3D_OK;
3389 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3391 struct wined3d_context *context;
3393 TRACE("device %p.\n", device);
3395 if (!device->inScene)
3397 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3398 return WINED3DERR_INVALIDCALL;
3401 context = context_acquire(device, NULL);
3402 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3403 context->gl_info->gl_ops.gl.p_glFlush();
3404 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3405 * fails. */
3406 context_release(context);
3408 device->inScene = FALSE;
3409 return WINED3D_OK;
3412 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3413 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3415 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3416 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3418 if (!rect_count && rects)
3420 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3421 return WINED3D_OK;
3424 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3426 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3427 if (!ds)
3429 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3430 /* TODO: What about depth stencil buffers without stencil bits? */
3431 return WINED3DERR_INVALIDCALL;
3433 else if (flags & WINED3DCLEAR_TARGET)
3435 if (ds->width < device->fb.render_targets[0]->width
3436 || ds->height < device->fb.render_targets[0]->height)
3438 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3439 return WINED3D_OK;
3444 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3446 return WINED3D_OK;
3449 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3450 struct wined3d_query *predicate, BOOL value)
3452 struct wined3d_query *prev;
3454 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3456 prev = device->update_state->predicate;
3457 if (predicate)
3459 FIXME("Predicated rendering not implemented.\n");
3460 wined3d_query_incref(predicate);
3462 device->update_state->predicate = predicate;
3463 device->update_state->predicate_value = value;
3464 if (!device->recording)
3465 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3466 if (prev)
3467 wined3d_query_decref(prev);
3470 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3472 TRACE("device %p, value %p.\n", device, value);
3474 *value = device->state.predicate_value;
3475 return device->state.predicate;
3478 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3479 enum wined3d_primitive_type primitive_type)
3481 GLenum gl_primitive_type, prev;
3483 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3485 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3486 prev = device->update_state->gl_primitive_type;
3487 device->update_state->gl_primitive_type = gl_primitive_type;
3488 if (device->recording)
3489 device->recording->changed.primitive_type = TRUE;
3490 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3491 device_invalidate_state(device, STATE_POINT_ENABLE);
3494 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3495 enum wined3d_primitive_type *primitive_type)
3497 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3499 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3501 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3504 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3506 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3508 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, 0, 0, FALSE);
3510 return WINED3D_OK;
3513 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3514 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3516 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3517 device, start_vertex, vertex_count, start_instance, instance_count);
3519 wined3d_cs_emit_draw(device->cs, 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3522 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3524 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3526 if (!device->state.index_buffer)
3528 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3529 * without an index buffer set. (The first time at least...)
3530 * D3D8 simply dies, but I doubt it can do much harm to return
3531 * D3DERR_INVALIDCALL there as well. */
3532 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3533 return WINED3DERR_INVALIDCALL;
3536 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3538 return WINED3D_OK;
3541 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3542 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3544 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3545 device, start_idx, index_count, start_instance, instance_count);
3547 wined3d_cs_emit_draw(device->cs, device->state.base_vertex_index,
3548 start_idx, index_count, start_instance, instance_count, TRUE);
3551 static HRESULT wined3d_device_update_texture_3d(struct wined3d_device *device,
3552 struct wined3d_texture *src_texture, unsigned int src_level,
3553 struct wined3d_texture *dst_texture, unsigned int level_count)
3555 struct wined3d_const_bo_address data;
3556 struct wined3d_context *context;
3557 struct wined3d_map_desc src;
3558 HRESULT hr = WINED3D_OK;
3559 unsigned int i;
3561 TRACE("device %p, src_texture %p, src_level %u, dst_texture %p, level_count %u.\n",
3562 device, src_texture, src_level, dst_texture, level_count);
3564 if (src_texture->resource.format != dst_texture->resource.format)
3566 WARN("Source and destination formats do not match.\n");
3567 return WINED3DERR_INVALIDCALL;
3570 if (wined3d_texture_get_level_width(src_texture, src_level) != dst_texture->resource.width
3571 || wined3d_texture_get_level_height(src_texture, src_level) != dst_texture->resource.height
3572 || wined3d_texture_get_level_depth(src_texture, src_level) != dst_texture->resource.depth)
3574 WARN("Source and destination dimensions do not match.\n");
3575 return WINED3DERR_INVALIDCALL;
3578 context = context_acquire(device, NULL);
3580 /* Only a prepare, since we're uploading entire volumes. */
3581 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3582 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3584 for (i = 0; i < level_count; ++i)
3586 if (FAILED(hr = wined3d_resource_map(&src_texture->resource,
3587 src_level + i, &src, NULL, WINED3D_MAP_READONLY)))
3588 goto done;
3590 data.buffer_object = 0;
3591 data.addr = src.data;
3592 wined3d_texture_upload_data(dst_texture, i, context, NULL, &data, src.row_pitch, src.slice_pitch);
3593 wined3d_texture_invalidate_location(dst_texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
3595 if (FAILED(hr = wined3d_resource_unmap(&src_texture->resource, src_level + i)))
3596 goto done;
3599 done:
3600 context_release(context);
3601 return hr;
3604 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3605 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3607 unsigned int src_size, dst_size, src_skip_levels = 0;
3608 unsigned int layer_count, level_count, i, j;
3609 enum wined3d_resource_type type;
3610 HRESULT hr;
3611 struct wined3d_context *context;
3613 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3615 /* Verify that the source and destination textures are non-NULL. */
3616 if (!src_texture || !dst_texture)
3618 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3619 return WINED3DERR_INVALIDCALL;
3622 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3624 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3625 return WINED3DERR_INVALIDCALL;
3627 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3629 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3630 return WINED3DERR_INVALIDCALL;
3633 /* Verify that the source and destination textures are the same type. */
3634 type = src_texture->resource.type;
3635 if (dst_texture->resource.type != type)
3637 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3638 return WINED3DERR_INVALIDCALL;
3641 layer_count = src_texture->layer_count;
3642 if (layer_count != dst_texture->layer_count)
3644 WARN("Source and destination have different layer counts.\n");
3645 return WINED3DERR_INVALIDCALL;
3648 level_count = min(wined3d_texture_get_level_count(src_texture),
3649 wined3d_texture_get_level_count(dst_texture));
3651 src_size = max(src_texture->resource.width, src_texture->resource.height);
3652 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3653 if (type == WINED3D_RTYPE_TEXTURE_3D)
3655 src_size = max(src_size, src_texture->resource.depth);
3656 dst_size = max(dst_size, dst_texture->resource.depth);
3658 while (src_size > dst_size)
3660 src_size >>= 1;
3661 ++src_skip_levels;
3664 /* Make sure that the destination texture is loaded. */
3665 context = context_acquire(device, NULL);
3666 wined3d_texture_load(dst_texture, context, FALSE);
3667 context_release(context);
3669 /* Update every surface level of the texture. */
3670 switch (type)
3672 case WINED3D_RTYPE_TEXTURE_2D:
3674 unsigned int src_levels = src_texture->level_count;
3675 unsigned int dst_levels = dst_texture->level_count;
3676 struct wined3d_surface *src_surface;
3677 struct wined3d_surface *dst_surface;
3679 for (i = 0; i < layer_count; ++i)
3681 for (j = 0; j < level_count; ++j)
3683 src_surface = src_texture->sub_resources[i * src_levels + j + src_skip_levels].u.surface;
3684 dst_surface = dst_texture->sub_resources[i * dst_levels + j].u.surface;
3685 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3687 WARN("Failed to update surface, hr %#x.\n", hr);
3688 return hr;
3692 return WINED3D_OK;
3695 case WINED3D_RTYPE_TEXTURE_3D:
3696 if (FAILED(hr = wined3d_device_update_texture_3d(device,
3697 src_texture, src_skip_levels, dst_texture, level_count)))
3698 WARN("Failed to update 3D texture, hr %#x.\n", hr);
3699 return hr;
3701 default:
3702 FIXME("Unsupported texture type %#x.\n", type);
3703 return WINED3DERR_INVALIDCALL;
3707 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3709 const struct wined3d_state *state = &device->state;
3710 struct wined3d_texture *texture;
3711 DWORD i;
3713 TRACE("device %p, num_passes %p.\n", device, num_passes);
3715 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3717 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3719 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3720 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3722 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3724 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3725 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3728 texture = state->textures[i];
3729 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3731 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3733 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3734 return E_FAIL;
3736 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3738 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3739 return E_FAIL;
3741 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3742 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3744 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3745 return E_FAIL;
3749 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3750 || state->render_states[WINED3D_RS_STENCILENABLE])
3752 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3753 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3755 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3757 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3758 return WINED3DERR_CONFLICTINGRENDERSTATE;
3762 /* return a sensible default */
3763 *num_passes = 1;
3765 TRACE("returning D3D_OK\n");
3766 return WINED3D_OK;
3769 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3771 static BOOL warned;
3773 TRACE("device %p, software %#x.\n", device, software);
3775 if (!warned)
3777 FIXME("device %p, software %#x stub!\n", device, software);
3778 warned = TRUE;
3781 device->softwareVertexProcessing = software;
3784 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3786 static BOOL warned;
3788 TRACE("device %p.\n", device);
3790 if (!warned)
3792 TRACE("device %p stub!\n", device);
3793 warned = TRUE;
3796 return device->softwareVertexProcessing;
3799 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3800 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3802 struct wined3d_swapchain *swapchain;
3804 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3805 device, swapchain_idx, raster_status);
3807 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3808 return WINED3DERR_INVALIDCALL;
3810 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3813 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3815 static BOOL warned;
3817 TRACE("device %p, segments %.8e.\n", device, segments);
3819 if (segments != 0.0f)
3821 if (!warned)
3823 FIXME("device %p, segments %.8e stub!\n", device, segments);
3824 warned = TRUE;
3828 return WINED3D_OK;
3831 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3833 static BOOL warned;
3835 TRACE("device %p.\n", device);
3837 if (!warned)
3839 FIXME("device %p stub!\n", device);
3840 warned = TRUE;
3843 return 0.0f;
3846 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3847 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3849 struct wined3d_texture *dst_texture, *src_texture;
3850 RECT dst_rect, src_rect;
3851 unsigned int i, j;
3852 HRESULT hr;
3854 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3856 if (src_resource == dst_resource)
3858 WARN("Source and destination are the same resource.\n");
3859 return;
3862 if (src_resource->type != dst_resource->type)
3864 WARN("Resource types (%s / %s) don't match.\n",
3865 debug_d3dresourcetype(dst_resource->type),
3866 debug_d3dresourcetype(src_resource->type));
3867 return;
3870 if (src_resource->width != dst_resource->width
3871 || src_resource->height != dst_resource->height
3872 || src_resource->depth != dst_resource->depth)
3874 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3875 dst_resource->width, dst_resource->height, dst_resource->depth,
3876 src_resource->width, src_resource->height, src_resource->depth);
3877 return;
3880 if (src_resource->format->id != dst_resource->format->id)
3882 WARN("Resource formats (%s / %s) don't match.\n",
3883 debug_d3dformat(dst_resource->format->id),
3884 debug_d3dformat(src_resource->format->id));
3885 return;
3888 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3890 if (FAILED(hr = wined3d_buffer_copy(buffer_from_resource(dst_resource), 0,
3891 buffer_from_resource(src_resource), 0,
3892 dst_resource->size)))
3893 ERR("Failed to copy buffer, hr %#x.\n", hr);
3894 return;
3897 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3899 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3900 return;
3903 dst_texture = texture_from_resource(dst_resource);
3904 src_texture = texture_from_resource(src_resource);
3906 if (src_texture->layer_count != dst_texture->layer_count
3907 || src_texture->level_count != dst_texture->level_count)
3909 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3910 dst_texture->layer_count, dst_texture->level_count,
3911 src_texture->layer_count, src_texture->level_count);
3912 return;
3915 for (i = 0; i < dst_texture->level_count; ++i)
3917 SetRect(&dst_rect, 0, 0,
3918 wined3d_texture_get_level_width(dst_texture, i),
3919 wined3d_texture_get_level_height(dst_texture, i));
3920 SetRect(&src_rect, 0, 0,
3921 wined3d_texture_get_level_width(src_texture, i),
3922 wined3d_texture_get_level_height(dst_texture, i));
3923 for (j = 0; j < dst_texture->layer_count; ++j)
3925 unsigned int idx = j * dst_texture->level_count + i;
3927 if (FAILED(hr = wined3d_texture_blt(dst_texture, idx, &dst_rect,
3928 src_texture, idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3929 ERR("Failed to blit, sub-resource %u, hr %#x.\n", idx, hr);
3934 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3935 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3936 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3937 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3939 struct wined3d_texture *dst_texture, *src_texture;
3940 RECT dst_rect, src_rect;
3941 HRESULT hr;
3943 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3944 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3945 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3946 src_resource, src_sub_resource_idx, debug_box(src_box));
3948 if (src_box && (src_box->left >= src_box->right
3949 || src_box->top >= src_box->bottom
3950 || src_box->front >= src_box->back))
3952 WARN("Invalid box %s specified.\n", debug_box(src_box));
3953 return WINED3DERR_INVALIDCALL;
3956 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3958 WARN("Source and destination are the same sub-resource.\n");
3959 return WINED3DERR_INVALIDCALL;
3962 if (src_resource->type != dst_resource->type)
3964 WARN("Resource types (%s / %s) don't match.\n",
3965 debug_d3dresourcetype(dst_resource->type),
3966 debug_d3dresourcetype(src_resource->type));
3967 return WINED3DERR_INVALIDCALL;
3970 if (src_resource->format->id != dst_resource->format->id)
3972 WARN("Resource formats (%s / %s) don't match.\n",
3973 debug_d3dformat(dst_resource->format->id),
3974 debug_d3dformat(src_resource->format->id));
3975 return WINED3DERR_INVALIDCALL;
3978 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
3980 unsigned int src_offset, size;
3982 if (dst_sub_resource_idx)
3984 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3985 return WINED3DERR_INVALIDCALL;
3987 if (src_sub_resource_idx)
3989 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3990 return WINED3DERR_INVALIDCALL;
3993 if (src_box)
3995 src_offset = src_box->left;
3996 size = src_box->right - src_box->left;
3998 else
4000 src_offset = 0;
4001 size = src_resource->size;
4004 if (src_offset > src_resource->size
4005 || size > src_resource->size - src_offset
4006 || dst_x > dst_resource->size
4007 || size > dst_resource->size - dst_x)
4009 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4010 dst_x, src_offset, size);
4011 return WINED3DERR_INVALIDCALL;
4014 return wined3d_buffer_copy(buffer_from_resource(dst_resource), dst_x,
4015 buffer_from_resource(src_resource), src_offset, size);
4018 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4020 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
4021 return WINED3DERR_INVALIDCALL;
4024 dst_texture = texture_from_resource(dst_resource);
4025 src_texture = texture_from_resource(src_resource);
4027 if (src_box)
4029 SetRect(&src_rect, src_box->left, src_box->top, src_box->right, src_box->bottom);
4031 else
4033 unsigned int level = src_sub_resource_idx % src_texture->level_count;
4035 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, level),
4036 wined3d_texture_get_level_height(src_texture, level));
4039 SetRect(&dst_rect, dst_x, dst_y, dst_x + (src_rect.right - src_rect.left),
4040 dst_y + (src_rect.bottom - src_rect.top));
4042 if (FAILED(hr = wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4043 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4044 WARN("Failed to blit, hr %#x.\n", hr);
4046 return hr;
4049 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4050 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4051 unsigned int depth_pitch)
4053 unsigned int width, height, depth, level;
4054 struct wined3d_const_bo_address addr;
4055 struct wined3d_context *context;
4056 struct wined3d_texture *texture;
4058 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4059 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4061 if (resource->type == WINED3D_RTYPE_BUFFER)
4063 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4064 HRESULT hr;
4066 if (sub_resource_idx > 0)
4068 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4069 return;
4072 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4073 WARN("Failed to update buffer data, hr %#x.\n", hr);
4075 return;
4078 if (resource->type != WINED3D_RTYPE_TEXTURE_2D && resource->type != WINED3D_RTYPE_TEXTURE_3D)
4080 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4081 return;
4084 texture = texture_from_resource(resource);
4085 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4087 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4088 return;
4091 level = sub_resource_idx % texture->level_count;
4092 width = wined3d_texture_get_level_width(texture, level);
4093 height = wined3d_texture_get_level_height(texture, level);
4094 depth = wined3d_texture_get_level_depth(texture, level);
4096 if (box && (box->left >= box->right || box->right > width
4097 || box->top >= box->bottom || box->bottom > height
4098 || box->front >= box->back || box->back > depth))
4100 WARN("Invalid box %s specified.\n", debug_box(box));
4101 return;
4104 addr.buffer_object = 0;
4105 addr.addr = data;
4107 context = context_acquire(resource->device, NULL);
4109 /* Only load the sub-resource for partial updates. */
4110 if (!box || (!box->left && !box->top && !box->front
4111 && box->right == width && box->bottom == height && box->back == depth))
4112 wined3d_texture_prepare_texture(texture, context, FALSE);
4113 else
4114 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4115 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4117 wined3d_texture_upload_data(texture, sub_resource_idx, context, box, &addr, row_pitch, depth_pitch);
4119 context_release(context);
4121 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4122 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4125 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4126 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4127 const struct wined3d_color *color, float depth, DWORD stencil)
4129 const struct blit_shader *blitter;
4130 struct wined3d_resource *resource;
4131 enum wined3d_blit_op blit_op;
4132 RECT r;
4134 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4135 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4137 if (!flags)
4138 return WINED3D_OK;
4140 resource = view->resource;
4141 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4143 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4144 return WINED3DERR_INVALIDCALL;
4147 if (view->depth > 1)
4149 FIXME("Layered clears not implemented.\n");
4150 return WINED3DERR_INVALIDCALL;
4153 if (!rect)
4155 SetRect(&r, 0, 0, view->width, view->height);
4156 rect = &r;
4159 if (flags & WINED3DCLEAR_TARGET)
4160 blit_op = WINED3D_BLIT_OP_COLOR_FILL;
4161 else
4162 blit_op = WINED3D_BLIT_OP_DEPTH_FILL;
4164 if (!(blitter = wined3d_select_blitter(&device->adapter->gl_info, &device->adapter->d3d_info,
4165 blit_op, NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format)))
4167 FIXME("No blitter is capable of performing the requested fill operation.\n");
4168 return WINED3DERR_INVALIDCALL;
4171 if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
4172 return blitter->color_fill(device, view, rect, color);
4173 else
4174 return blitter->depth_fill(device, view, rect, flags, depth, stencil);
4177 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4178 unsigned int view_idx)
4180 TRACE("device %p, view_idx %u.\n", device, view_idx);
4182 if (view_idx >= device->adapter->gl_info.limits.buffers)
4184 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4185 return NULL;
4188 return device->fb.render_targets[view_idx];
4191 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4193 TRACE("device %p.\n", device);
4195 return device->fb.depth_stencil;
4198 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4199 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4201 struct wined3d_rendertarget_view *prev;
4203 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4204 device, view_idx, view, set_viewport);
4206 if (view_idx >= device->adapter->gl_info.limits.buffers)
4208 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4209 return WINED3DERR_INVALIDCALL;
4212 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4214 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4215 return WINED3DERR_INVALIDCALL;
4218 /* Set the viewport and scissor rectangles, if requested. Tests show that
4219 * stateblock recording is ignored, the change goes directly into the
4220 * primary stateblock. */
4221 if (!view_idx && set_viewport)
4223 struct wined3d_state *state = &device->state;
4225 state->viewport.x = 0;
4226 state->viewport.y = 0;
4227 state->viewport.width = view->width;
4228 state->viewport.height = view->height;
4229 state->viewport.min_z = 0.0f;
4230 state->viewport.max_z = 1.0f;
4231 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4233 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4234 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4238 prev = device->fb.render_targets[view_idx];
4239 if (view == prev)
4240 return WINED3D_OK;
4242 if (view)
4243 wined3d_rendertarget_view_incref(view);
4244 device->fb.render_targets[view_idx] = view;
4245 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4246 /* Release after the assignment, to prevent device_resource_released()
4247 * from seeing the surface as still in use. */
4248 if (prev)
4249 wined3d_rendertarget_view_decref(prev);
4251 return WINED3D_OK;
4254 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4256 struct wined3d_rendertarget_view *prev;
4258 TRACE("device %p, view %p.\n", device, view);
4260 prev = device->fb.depth_stencil;
4261 if (prev == view)
4263 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4264 return;
4267 if ((device->fb.depth_stencil = view))
4268 wined3d_rendertarget_view_incref(view);
4269 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4270 if (prev)
4271 wined3d_rendertarget_view_decref(prev);
4274 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4275 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4277 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4278 struct wined3d_sub_resource_data data;
4279 struct wined3d_resource_desc desc;
4280 struct wined3d_map_desc map_desc;
4281 struct wined3d_texture *texture;
4282 HRESULT hr;
4284 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READONLY)))
4286 ERR("Failed to map source texture.\n");
4287 return NULL;
4290 data.data = map_desc.data;
4291 data.row_pitch = map_desc.row_pitch;
4292 data.slice_pitch = map_desc.slice_pitch;
4294 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4295 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4296 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4297 desc.multisample_quality = 0;
4298 desc.usage = WINED3DUSAGE_DYNAMIC;
4299 desc.pool = WINED3D_POOL_DEFAULT;
4300 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4301 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4302 desc.depth = 1;
4303 desc.size = 0;
4305 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4306 &data, NULL, &wined3d_null_parent_ops, &texture);
4307 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4308 if (FAILED(hr))
4310 ERR("Failed to create cursor texture.\n");
4311 return NULL;
4314 return texture;
4317 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4318 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4320 unsigned int texture_level = sub_resource_idx % texture->level_count;
4321 unsigned int cursor_width, cursor_height;
4322 struct wined3d_display_mode mode;
4323 struct wined3d_map_desc map_desc;
4324 HRESULT hr;
4326 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4327 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4329 if (sub_resource_idx >= texture->level_count * texture->layer_count
4330 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4331 return WINED3DERR_INVALIDCALL;
4333 if (device->cursor_texture)
4335 wined3d_texture_decref(device->cursor_texture);
4336 device->cursor_texture = NULL;
4339 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4341 WARN("Texture %p has invalid format %s.\n",
4342 texture, debug_d3dformat(texture->resource.format->id));
4343 return WINED3DERR_INVALIDCALL;
4346 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4348 ERR("Failed to get display mode, hr %#x.\n", hr);
4349 return WINED3DERR_INVALIDCALL;
4352 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4353 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4354 if (cursor_width > mode.width || cursor_height > mode.height)
4356 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4357 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4358 return WINED3DERR_INVALIDCALL;
4361 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4363 /* Do not store the surface's pointer because the application may
4364 * release it after setting the cursor image. Windows doesn't
4365 * addref the set surface, so we can't do this either without
4366 * creating circular refcount dependencies. */
4367 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4369 ERR("Failed to create cursor texture.\n");
4370 return WINED3DERR_INVALIDCALL;
4373 if (cursor_width == 32 && cursor_height == 32)
4375 UINT mask_size = cursor_width * cursor_height / 8;
4376 ICONINFO cursor_info;
4377 DWORD *mask_bits;
4378 HCURSOR cursor;
4380 /* 32-bit user32 cursors ignore the alpha channel if it's all
4381 * zeroes, and use the mask instead. Fill the mask with all ones
4382 * to ensure we still get a fully transparent cursor. */
4383 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4384 return E_OUTOFMEMORY;
4385 memset(mask_bits, 0xff, mask_size);
4387 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4388 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4389 cursor_info.fIcon = FALSE;
4390 cursor_info.xHotspot = x_hotspot;
4391 cursor_info.yHotspot = y_hotspot;
4392 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4393 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4394 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4396 /* Create our cursor and clean up. */
4397 cursor = CreateIconIndirect(&cursor_info);
4398 if (cursor_info.hbmMask)
4399 DeleteObject(cursor_info.hbmMask);
4400 if (cursor_info.hbmColor)
4401 DeleteObject(cursor_info.hbmColor);
4402 if (device->hardwareCursor)
4403 DestroyCursor(device->hardwareCursor);
4404 device->hardwareCursor = cursor;
4405 if (device->bCursorVisible)
4406 SetCursor(cursor);
4408 HeapFree(GetProcessHeap(), 0, mask_bits);
4411 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4412 device->cursorWidth = cursor_width;
4413 device->cursorHeight = cursor_height;
4414 device->xHotSpot = x_hotspot;
4415 device->yHotSpot = y_hotspot;
4417 return WINED3D_OK;
4420 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4421 int x_screen_space, int y_screen_space, DWORD flags)
4423 TRACE("device %p, x %d, y %d, flags %#x.\n",
4424 device, x_screen_space, y_screen_space, flags);
4426 device->xScreenSpace = x_screen_space;
4427 device->yScreenSpace = y_screen_space;
4429 if (device->hardwareCursor)
4431 POINT pt;
4433 GetCursorPos( &pt );
4434 if (x_screen_space == pt.x && y_screen_space == pt.y)
4435 return;
4436 SetCursorPos( x_screen_space, y_screen_space );
4438 /* Switch to the software cursor if position diverges from the hardware one. */
4439 GetCursorPos( &pt );
4440 if (x_screen_space != pt.x || y_screen_space != pt.y)
4442 if (device->bCursorVisible) SetCursor( NULL );
4443 DestroyCursor( device->hardwareCursor );
4444 device->hardwareCursor = 0;
4449 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4451 BOOL oldVisible = device->bCursorVisible;
4453 TRACE("device %p, show %#x.\n", device, show);
4456 * When ShowCursor is first called it should make the cursor appear at the OS's last
4457 * known cursor position.
4459 if (show && !oldVisible)
4461 POINT pt;
4462 GetCursorPos(&pt);
4463 device->xScreenSpace = pt.x;
4464 device->yScreenSpace = pt.y;
4467 if (device->hardwareCursor)
4469 device->bCursorVisible = show;
4470 if (show)
4471 SetCursor(device->hardwareCursor);
4472 else
4473 SetCursor(NULL);
4475 else if (device->cursor_texture)
4477 device->bCursorVisible = show;
4480 return oldVisible;
4483 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4485 struct wined3d_resource *resource, *cursor;
4487 TRACE("device %p.\n", device);
4489 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4491 TRACE("Checking resource %p for eviction.\n", resource);
4493 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4495 TRACE("Evicting %p.\n", resource);
4496 wined3d_cs_emit_unload_resource(device->cs, resource);
4501 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4503 struct wined3d_resource *resource, *cursor;
4504 const struct wined3d_gl_info *gl_info;
4505 struct wined3d_context *context;
4506 struct wined3d_shader *shader;
4508 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4510 TRACE("Unloading resource %p.\n", resource);
4511 wined3d_cs_emit_unload_resource(device->cs, resource);
4514 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4516 device->shader_backend->shader_destroy(shader);
4519 context = context_acquire(device, NULL);
4520 gl_info = context->gl_info;
4522 if (device->depth_blt_texture)
4524 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4525 device->depth_blt_texture = 0;
4528 device->blitter->free_private(device);
4529 device->shader_backend->shader_free_private(device);
4530 destroy_dummy_textures(device, context);
4531 destroy_default_samplers(device, context);
4533 context_release(context);
4535 while (device->context_count)
4537 if (device->contexts[0]->swapchain)
4538 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4539 else
4540 context_destroy(device, device->contexts[0]);
4543 HeapFree(GetProcessHeap(), 0, swapchain->context);
4544 swapchain->context = NULL;
4547 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4549 struct wined3d_context *context;
4550 struct wined3d_texture *target;
4551 HRESULT hr;
4553 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4554 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4556 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4557 return hr;
4560 if (FAILED(hr = device->blitter->alloc_private(device)))
4562 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4563 device->shader_backend->shader_free_private(device);
4564 return hr;
4567 /* Recreate the primary swapchain's context */
4568 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4569 if (!swapchain->context)
4571 ERR("Failed to allocate memory for swapchain context array.\n");
4572 device->blitter->free_private(device);
4573 device->shader_backend->shader_free_private(device);
4574 return E_OUTOFMEMORY;
4577 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4578 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4580 WARN("Failed to create context.\n");
4581 device->blitter->free_private(device);
4582 device->shader_backend->shader_free_private(device);
4583 HeapFree(GetProcessHeap(), 0, swapchain->context);
4584 return E_FAIL;
4587 swapchain->context[0] = context;
4588 swapchain->num_contexts = 1;
4589 create_dummy_textures(device, context);
4590 create_default_samplers(device, context);
4591 context_release(context);
4593 return WINED3D_OK;
4596 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4597 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4598 wined3d_device_reset_cb callback, BOOL reset_state)
4600 struct wined3d_resource *resource, *cursor;
4601 struct wined3d_swapchain *swapchain;
4602 struct wined3d_view_desc view_desc;
4603 BOOL backbuffer_resized;
4604 HRESULT hr = WINED3D_OK;
4605 unsigned int i;
4607 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4608 device, swapchain_desc, mode, callback, reset_state);
4610 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4612 ERR("Failed to get the first implicit swapchain.\n");
4613 return WINED3DERR_INVALIDCALL;
4616 if (reset_state)
4618 if (device->logo_texture)
4620 wined3d_texture_decref(device->logo_texture);
4621 device->logo_texture = NULL;
4623 if (device->cursor_texture)
4625 wined3d_texture_decref(device->cursor_texture);
4626 device->cursor_texture = NULL;
4628 state_unbind_resources(&device->state);
4631 if (device->fb.render_targets)
4633 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4635 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4638 wined3d_device_set_depth_stencil_view(device, NULL);
4640 if (reset_state)
4642 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4644 TRACE("Enumerating resource %p.\n", resource);
4645 if (FAILED(hr = callback(resource)))
4646 return hr;
4650 TRACE("New params:\n");
4651 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4652 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4653 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4654 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4655 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4656 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4657 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4658 TRACE("device_window %p\n", swapchain_desc->device_window);
4659 TRACE("windowed %#x\n", swapchain_desc->windowed);
4660 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4661 if (swapchain_desc->enable_auto_depth_stencil)
4662 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4663 TRACE("flags %#x\n", swapchain_desc->flags);
4664 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4665 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4666 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4668 /* No special treatment of these parameters. Just store them */
4669 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4670 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4671 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4672 swapchain->desc.flags = swapchain_desc->flags;
4673 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4674 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4675 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4677 if (swapchain_desc->device_window
4678 && swapchain_desc->device_window != swapchain->desc.device_window)
4680 TRACE("Changing the device window from %p to %p.\n",
4681 swapchain->desc.device_window, swapchain_desc->device_window);
4682 swapchain->desc.device_window = swapchain_desc->device_window;
4683 swapchain->device_window = swapchain_desc->device_window;
4684 wined3d_swapchain_set_window(swapchain, NULL);
4687 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4688 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4690 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4691 || swapchain->reapply_mode || mode
4692 || (!swapchain_desc->windowed && backbuffer_resized))
4694 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4695 return hr;
4697 else if (!swapchain_desc->windowed)
4699 DWORD style = device->style;
4700 DWORD exStyle = device->exStyle;
4701 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4702 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4703 * Reset to clear up their mess. Guild Wars also loses the device during that.
4705 device->style = 0;
4706 device->exStyle = 0;
4707 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4708 swapchain_desc->backbuffer_width,
4709 swapchain_desc->backbuffer_height);
4710 device->style = style;
4711 device->exStyle = exStyle;
4714 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4715 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4716 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4717 return hr;
4719 if (device->auto_depth_stencil_view)
4721 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4722 device->auto_depth_stencil_view = NULL;
4724 if (swapchain->desc.enable_auto_depth_stencil)
4726 struct wined3d_resource_desc texture_desc;
4727 struct wined3d_texture *texture;
4729 TRACE("Creating the depth stencil buffer.\n");
4731 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4732 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4733 texture_desc.multisample_type = swapchain->desc.multisample_type;
4734 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4735 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4736 texture_desc.pool = WINED3D_POOL_DEFAULT;
4737 texture_desc.width = swapchain->desc.backbuffer_width;
4738 texture_desc.height = swapchain->desc.backbuffer_height;
4739 texture_desc.depth = 1;
4740 texture_desc.size = 0;
4742 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4743 device->device_parent, &texture_desc, &texture)))
4745 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4746 return WINED3DERR_INVALIDCALL;
4749 view_desc.format_id = texture->resource.format->id;
4750 view_desc.flags = 0;
4751 view_desc.u.texture.level_idx = 0;
4752 view_desc.u.texture.level_count = 1;
4753 view_desc.u.texture.layer_idx = 0;
4754 view_desc.u.texture.layer_count = 1;
4755 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4756 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4757 wined3d_texture_decref(texture);
4758 if (FAILED(hr))
4760 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4761 return hr;
4764 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4767 if (device->back_buffer_view)
4769 wined3d_rendertarget_view_decref(device->back_buffer_view);
4770 device->back_buffer_view = NULL;
4772 if (swapchain->desc.backbuffer_count)
4774 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4776 view_desc.format_id = back_buffer->format->id;
4777 view_desc.flags = 0;
4778 view_desc.u.texture.level_idx = 0;
4779 view_desc.u.texture.level_count = 1;
4780 view_desc.u.texture.layer_idx = 0;
4781 view_desc.u.texture.layer_count = 1;
4782 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4783 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4785 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4786 return hr;
4790 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4792 if (reset_state)
4794 TRACE("Resetting stateblock.\n");
4795 if (device->recording)
4797 wined3d_stateblock_decref(device->recording);
4798 device->recording = NULL;
4800 wined3d_cs_emit_reset_state(device->cs);
4801 state_cleanup(&device->state);
4803 if (device->d3d_initialized)
4804 delete_opengl_contexts(device, swapchain);
4806 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4807 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4808 device->update_state = &device->state;
4810 device_init_swapchain_state(device, swapchain);
4812 else if (device->back_buffer_view)
4814 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4815 struct wined3d_state *state = &device->state;
4817 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4819 /* Note the min_z / max_z is not reset. */
4820 state->viewport.x = 0;
4821 state->viewport.y = 0;
4822 state->viewport.width = view->width;
4823 state->viewport.height = view->height;
4824 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4826 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4827 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4830 if (device->d3d_initialized)
4832 if (reset_state)
4833 hr = create_primary_opengl_context(device, swapchain);
4834 swapchain_update_swap_interval(swapchain);
4837 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4838 * first use
4840 return hr;
4843 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4845 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4847 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4849 return WINED3D_OK;
4853 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4854 struct wined3d_device_creation_parameters *parameters)
4856 TRACE("device %p, parameters %p.\n", device, parameters);
4858 *parameters = device->create_parms;
4861 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4863 TRACE("device %p.\n", device);
4865 return device->wined3d;
4868 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4869 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4871 struct wined3d_swapchain *swapchain;
4873 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4874 device, swapchain_idx, flags, ramp);
4876 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4877 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4880 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4881 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4883 struct wined3d_swapchain *swapchain;
4885 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4886 device, swapchain_idx, ramp);
4888 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4889 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4892 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4894 TRACE("device %p, resource %p.\n", device, resource);
4896 list_add_head(&device->resources, &resource->resource_list_entry);
4899 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4901 TRACE("device %p, resource %p.\n", device, resource);
4903 list_remove(&resource->resource_list_entry);
4906 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4908 enum wined3d_resource_type type = resource->type;
4909 struct wined3d_rendertarget_view *rtv;
4910 unsigned int i;
4912 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4914 if (device->d3d_initialized)
4916 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4918 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
4919 ERR("Resource %p is still in use as render target %u.\n", resource, i);
4922 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
4923 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
4926 switch (type)
4928 case WINED3D_RTYPE_TEXTURE_2D:
4929 case WINED3D_RTYPE_TEXTURE_3D:
4930 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4932 struct wined3d_texture *texture = texture_from_resource(resource);
4934 if (device->state.textures[i] == texture)
4936 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4937 device->state.textures[i] = NULL;
4940 if (device->recording && device->update_state->textures[i] == texture)
4942 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4943 texture, device->recording, i);
4944 device->update_state->textures[i] = NULL;
4947 break;
4949 case WINED3D_RTYPE_BUFFER:
4951 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4953 for (i = 0; i < MAX_STREAMS; ++i)
4955 if (device->state.streams[i].buffer == buffer)
4957 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4958 device->state.streams[i].buffer = NULL;
4961 if (device->recording && device->update_state->streams[i].buffer == buffer)
4963 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4964 buffer, device->recording, i);
4965 device->update_state->streams[i].buffer = NULL;
4969 if (device->state.index_buffer == buffer)
4971 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4972 device->state.index_buffer = NULL;
4975 if (device->recording && device->update_state->index_buffer == buffer)
4977 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4978 buffer, device->recording);
4979 device->update_state->index_buffer = NULL;
4982 break;
4984 default:
4985 break;
4988 /* Remove the resource from the resourceStore */
4989 device_resource_remove(device, resource);
4991 TRACE("Resource released.\n");
4994 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
4996 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
4998 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5001 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5002 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5003 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5005 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5006 const struct fragment_pipeline *fragment_pipeline;
5007 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5008 unsigned int i;
5009 HRESULT hr;
5011 device->ref = 1;
5012 device->wined3d = wined3d;
5013 wined3d_incref(device->wined3d);
5014 device->adapter = wined3d->adapter_count ? adapter : NULL;
5015 device->device_parent = device_parent;
5016 list_init(&device->resources);
5017 list_init(&device->shaders);
5018 device->surface_alignment = surface_alignment;
5020 /* Save the creation parameters. */
5021 device->create_parms.adapter_idx = adapter_idx;
5022 device->create_parms.device_type = device_type;
5023 device->create_parms.focus_window = focus_window;
5024 device->create_parms.flags = flags;
5026 device->shader_backend = adapter->shader_backend;
5028 vertex_pipeline = adapter->vertex_pipe;
5030 fragment_pipeline = adapter->fragment_pipe;
5032 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5034 if (vertex_pipeline->vp_states && fragment_pipeline->states
5035 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5036 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5037 fragment_pipeline, misc_state_template)))
5039 ERR("Failed to compile state table, hr %#x.\n", hr);
5040 wine_rb_destroy(&device->samplers, NULL, NULL);
5041 wined3d_decref(device->wined3d);
5042 return hr;
5045 device->blitter = adapter->blitter;
5047 state_init(&device->state, &device->fb, &adapter->gl_info,
5048 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5049 device->update_state = &device->state;
5051 if (!(device->cs = wined3d_cs_create(device)))
5053 WARN("Failed to create command stream.\n");
5054 state_cleanup(&device->state);
5055 hr = E_FAIL;
5056 goto err;
5059 return WINED3D_OK;
5061 err:
5062 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5064 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5066 wine_rb_destroy(&device->samplers, NULL, NULL);
5067 wined3d_decref(device->wined3d);
5068 return hr;
5072 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5074 DWORD rep = device->StateTable[state].representative;
5075 struct wined3d_context *context;
5076 DWORD idx;
5077 BYTE shift;
5078 UINT i;
5080 for (i = 0; i < device->context_count; ++i)
5082 context = device->contexts[i];
5083 if(isStateDirty(context, rep)) continue;
5085 context->dirtyArray[context->numDirtyEntries++] = rep;
5086 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5087 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5088 context->isStateDirty[idx] |= (1u << shift);
5092 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5093 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5095 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5097 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5098 window, message, wparam, lparam);
5099 if (unicode)
5100 return DefWindowProcW(window, message, wparam, lparam);
5101 else
5102 return DefWindowProcA(window, message, wparam, lparam);
5105 if (message == WM_DESTROY)
5107 TRACE("unregister window %p.\n", window);
5108 wined3d_unregister_window(window);
5110 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5111 ERR("Window %p is not the focus window for device %p.\n", window, device);
5113 else if (message == WM_DISPLAYCHANGE)
5115 device->device_parent->ops->mode_changed(device->device_parent);
5117 else if (message == WM_ACTIVATEAPP)
5119 UINT i;
5121 for (i = 0; i < device->swapchain_count; i++)
5122 wined3d_swapchain_activate(device->swapchains[i], wparam);
5124 device->device_parent->ops->activate(device->device_parent, wparam);
5126 else if (message == WM_SYSCOMMAND)
5128 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5130 if (unicode)
5131 DefWindowProcW(window, message, wparam, lparam);
5132 else
5133 DefWindowProcA(window, message, wparam, lparam);
5137 if (unicode)
5138 return CallWindowProcW(proc, window, message, wparam, lparam);
5139 else
5140 return CallWindowProcA(proc, window, message, wparam, lparam);