wined3d: Use the texture draw binding instead of the surface draw binding.
[wine.git] / dlls / wined3d / device.c
blobc650066142f9c3ae3e17641987fa1e3d4336646f
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 return GL_NONE;
97 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
99 switch(primitive_type)
101 case GL_POINTS:
102 return WINED3D_PT_POINTLIST;
104 case GL_LINES:
105 return WINED3D_PT_LINELIST;
107 case GL_LINE_STRIP:
108 return WINED3D_PT_LINESTRIP;
110 case GL_TRIANGLES:
111 return WINED3D_PT_TRIANGLELIST;
113 case GL_TRIANGLE_STRIP:
114 return WINED3D_PT_TRIANGLESTRIP;
116 case GL_TRIANGLE_FAN:
117 return WINED3D_PT_TRIANGLEFAN;
119 case GL_LINES_ADJACENCY_ARB:
120 return WINED3D_PT_LINELIST_ADJ;
122 case GL_LINE_STRIP_ADJACENCY_ARB:
123 return WINED3D_PT_LINESTRIP_ADJ;
125 case GL_TRIANGLES_ADJACENCY_ARB:
126 return WINED3D_PT_TRIANGLELIST_ADJ;
128 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
129 return WINED3D_PT_TRIANGLESTRIP_ADJ;
131 default:
132 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
133 return WINED3D_PT_UNDEFINED;
137 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
139 struct wined3d_context **new_array;
141 TRACE("Adding context %p.\n", context);
143 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
144 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
145 sizeof(*new_array) * (device->context_count + 1));
147 if (!new_array)
149 ERR("Failed to grow the context array.\n");
150 return FALSE;
153 new_array[device->context_count++] = context;
154 device->contexts = new_array;
155 return TRUE;
158 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
160 struct wined3d_context **new_array;
161 BOOL found = FALSE;
162 UINT i;
164 TRACE("Removing context %p.\n", context);
166 for (i = 0; i < device->context_count; ++i)
168 if (device->contexts[i] == context)
170 found = TRUE;
171 break;
175 if (!found)
177 ERR("Context %p doesn't exist in context array.\n", context);
178 return;
181 if (!--device->context_count)
183 HeapFree(GetProcessHeap(), 0, device->contexts);
184 device->contexts = NULL;
185 return;
188 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
189 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
190 if (!new_array)
192 ERR("Failed to shrink context array. Oh well.\n");
193 return;
196 device->contexts = new_array;
199 void device_switch_onscreen_ds(struct wined3d_device *device,
200 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
202 if (device->onscreen_depth_stencil)
204 surface_load_ds_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
206 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
207 device->onscreen_depth_stencil->ds_current_size.cx,
208 device->onscreen_depth_stencil->ds_current_size.cy);
209 wined3d_surface_decref(device->onscreen_depth_stencil);
211 device->onscreen_depth_stencil = depth_stencil;
212 wined3d_surface_incref(device->onscreen_depth_stencil);
215 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
217 /* partial draw rect */
218 if (draw_rect->left || draw_rect->top
219 || draw_rect->right < target->resource.width
220 || draw_rect->bottom < target->resource.height)
221 return FALSE;
223 /* partial clear rect */
224 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
225 || clear_rect->right < target->resource.width
226 || clear_rect->bottom < target->resource.height))
227 return FALSE;
229 return TRUE;
232 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
233 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
235 RECT current_rect, r;
237 if (ds->locations & WINED3D_LOCATION_DISCARDED)
239 /* Depth buffer was discarded, make it entirely current in its new location since
240 * there is no other place where we would get data anyway. */
241 SetRect(out_rect, 0, 0, ds->resource.width, ds->resource.height);
242 return;
245 if (ds->locations & location)
246 SetRect(&current_rect, 0, 0,
247 ds->ds_current_size.cx,
248 ds->ds_current_size.cy);
249 else
250 SetRectEmpty(&current_rect);
252 IntersectRect(&r, draw_rect, &current_rect);
253 if (EqualRect(&r, draw_rect))
255 /* current_rect ⊇ draw_rect, modify only. */
256 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
257 return;
260 if (EqualRect(&r, &current_rect))
262 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
264 if (!clear_rect)
266 /* Full clear, modify only. */
267 *out_rect = *draw_rect;
268 return;
271 IntersectRect(&r, draw_rect, clear_rect);
272 if (EqualRect(&r, draw_rect))
274 /* clear_rect ⊇ draw_rect, modify only. */
275 *out_rect = *draw_rect;
276 return;
280 /* Full load. */
281 surface_load_ds_location(ds, context, location);
282 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
285 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
286 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
287 float depth, DWORD stencil)
289 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
290 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
291 const struct wined3d_gl_info *gl_info;
292 UINT drawable_width, drawable_height;
293 struct wined3d_context *context;
294 GLbitfield clear_mask = 0;
295 BOOL render_offscreen;
296 unsigned int i;
297 RECT ds_rect;
299 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
300 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
301 * for the cleared parts, and the untouched parts.
303 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
304 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
305 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
306 * checking all this if the dest surface is in the drawable anyway. */
307 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
309 for (i = 0; i < rt_count; ++i)
311 struct wined3d_surface *rt = fb->render_targets[i];
312 if (rt)
313 surface_load_location(rt, rt->container->resource.draw_binding);
317 context = context_acquire(device, target);
318 if (!context->valid)
320 context_release(context);
321 WARN("Invalid context, skipping clear.\n");
322 return;
324 gl_info = context->gl_info;
326 if (target)
328 render_offscreen = context->render_offscreen;
329 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
331 else
333 render_offscreen = TRUE;
334 drawable_width = fb->depth_stencil->pow2Width;
335 drawable_height = fb->depth_stencil->pow2Height;
338 if (flags & WINED3DCLEAR_ZBUFFER)
340 DWORD location = render_offscreen ? fb->depth_stencil->container->resource.draw_binding
341 : WINED3D_LOCATION_DRAWABLE;
343 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
344 device_switch_onscreen_ds(device, context, fb->depth_stencil);
345 prepare_ds_clear(fb->depth_stencil, context, location,
346 draw_rect, rect_count, clear_rect, &ds_rect);
349 if (!context_apply_clear_state(context, device, rt_count, fb))
351 context_release(context);
352 WARN("Failed to apply clear state, skipping clear.\n");
353 return;
356 /* Only set the values up once, as they are not changing. */
357 if (flags & WINED3DCLEAR_STENCIL)
359 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
361 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
362 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
364 gl_info->gl_ops.gl.p_glStencilMask(~0U);
365 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
366 gl_info->gl_ops.gl.p_glClearStencil(stencil);
367 checkGLcall("glClearStencil");
368 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
371 if (flags & WINED3DCLEAR_ZBUFFER)
373 DWORD location = render_offscreen ? fb->depth_stencil->container->resource.draw_binding
374 : WINED3D_LOCATION_DRAWABLE;
376 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
378 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
379 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
380 gl_info->gl_ops.gl.p_glClearDepth(depth);
381 checkGLcall("glClearDepth");
382 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
385 if (flags & WINED3DCLEAR_TARGET)
387 for (i = 0; i < rt_count; ++i)
389 struct wined3d_surface *rt = fb->render_targets[i];
391 if (rt)
393 surface_validate_location(rt, rt->container->resource.draw_binding);
394 surface_invalidate_location(rt, ~rt->container->resource.draw_binding);
398 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
399 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
400 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
401 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
402 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
403 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
404 checkGLcall("glClearColor");
405 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
408 if (!clear_rect)
410 if (render_offscreen)
412 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
413 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
415 else
417 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
418 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
420 checkGLcall("glScissor");
421 gl_info->gl_ops.gl.p_glClear(clear_mask);
422 checkGLcall("glClear");
424 else
426 RECT current_rect;
428 /* Now process each rect in turn. */
429 for (i = 0; i < rect_count; ++i)
431 /* Note that GL uses lower left, width/height. */
432 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
434 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
435 wine_dbgstr_rect(&clear_rect[i]),
436 wine_dbgstr_rect(&current_rect));
438 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
439 * The rectangle is not cleared, no error is returned, but further rectangles are
440 * still cleared if they are valid. */
441 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
443 TRACE("Rectangle with negative dimensions, ignoring.\n");
444 continue;
447 if (render_offscreen)
449 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
450 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
452 else
454 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
455 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
457 checkGLcall("glScissor");
459 gl_info->gl_ops.gl.p_glClear(clear_mask);
460 checkGLcall("glClear");
464 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
465 && target->container->swapchain && target->container->swapchain->front_buffer == target))
466 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
468 context_release(context);
471 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
473 ULONG refcount = InterlockedIncrement(&device->ref);
475 TRACE("%p increasing refcount to %u.\n", device, refcount);
477 return refcount;
480 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
482 ULONG refcount = InterlockedDecrement(&device->ref);
484 TRACE("%p decreasing refcount to %u.\n", device, refcount);
486 if (!refcount)
488 UINT i;
490 wined3d_cs_destroy(device->cs);
492 if (device->recording && wined3d_stateblock_decref(device->recording))
493 FIXME("Something's still holding the recording stateblock.\n");
494 device->recording = NULL;
496 state_cleanup(&device->state);
498 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
500 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
501 device->multistate_funcs[i] = NULL;
504 if (!list_empty(&device->resources))
506 struct wined3d_resource *resource;
508 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
510 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
512 FIXME("Leftover resource %p with type %s (%#x).\n",
513 resource, debug_d3dresourcetype(resource->type), resource->type);
517 if (device->contexts)
518 ERR("Context array not freed!\n");
519 if (device->hardwareCursor)
520 DestroyCursor(device->hardwareCursor);
521 device->hardwareCursor = 0;
523 wined3d_decref(device->wined3d);
524 device->wined3d = NULL;
525 HeapFree(GetProcessHeap(), 0, device);
526 TRACE("Freed device %p.\n", device);
529 return refcount;
532 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
534 TRACE("device %p.\n", device);
536 return device->swapchain_count;
539 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
541 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
543 if (swapchain_idx >= device->swapchain_count)
545 WARN("swapchain_idx %u >= swapchain_count %u.\n",
546 swapchain_idx, device->swapchain_count);
547 return NULL;
550 return device->swapchains[swapchain_idx];
553 static void device_load_logo(struct wined3d_device *device, const char *filename)
555 struct wined3d_color_key color_key;
556 struct wined3d_resource_desc desc;
557 struct wined3d_surface *surface;
558 HBITMAP hbm;
559 BITMAP bm;
560 HRESULT hr;
561 HDC dcb = NULL, dcs = NULL;
563 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
564 if(hbm)
566 GetObjectA(hbm, sizeof(BITMAP), &bm);
567 dcb = CreateCompatibleDC(NULL);
568 if(!dcb) goto out;
569 SelectObject(dcb, hbm);
571 else
573 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
574 * couldn't be loaded
576 memset(&bm, 0, sizeof(bm));
577 bm.bmWidth = 32;
578 bm.bmHeight = 32;
581 desc.resource_type = WINED3D_RTYPE_TEXTURE;
582 desc.format = WINED3DFMT_B5G6R5_UNORM;
583 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
584 desc.multisample_quality = 0;
585 desc.usage = WINED3DUSAGE_DYNAMIC;
586 desc.pool = WINED3D_POOL_DEFAULT;
587 desc.width = bm.bmWidth;
588 desc.height = bm.bmHeight;
589 desc.depth = 1;
590 desc.size = 0;
591 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
592 NULL, &wined3d_null_parent_ops, &device->logo_texture)))
594 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
595 goto out;
597 surface = surface_from_resource(wined3d_texture_get_sub_resource(device->logo_texture, 0));
599 if (dcb)
601 if (FAILED(hr = wined3d_surface_getdc(surface, &dcs)))
602 goto out;
603 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
604 wined3d_surface_releasedc(surface, dcs);
606 color_key.color_space_low_value = 0;
607 color_key.color_space_high_value = 0;
608 wined3d_texture_set_color_key(device->logo_texture, WINEDDCKEY_SRCBLT, &color_key);
610 else
612 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
613 /* Fill the surface with a white color to show that wined3d is there */
614 wined3d_device_color_fill(device, surface, NULL, &c);
617 out:
618 if (dcb) DeleteDC(dcb);
619 if (hbm) DeleteObject(hbm);
622 /* Context activation is done by the caller. */
623 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
625 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
626 unsigned int i, j, count;
627 /* Under DirectX you can sample even if no texture is bound, whereas
628 * OpenGL will only allow that when a valid texture is bound.
629 * We emulate this by creating dummy textures and binding them
630 * to each texture stage when the currently set D3D texture is NULL. */
632 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
633 for (i = 0; i < count; ++i)
635 DWORD color = 0x000000ff;
637 /* Make appropriate texture active */
638 context_active_texture(context, gl_info, i);
640 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
641 checkGLcall("glGenTextures");
642 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
644 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
645 checkGLcall("glBindTexture");
647 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
648 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
649 checkGLcall("glTexImage2D");
651 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
653 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
654 checkGLcall("glGenTextures");
655 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
657 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
658 checkGLcall("glBindTexture");
660 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
661 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
662 checkGLcall("glTexImage2D");
665 if (gl_info->supported[EXT_TEXTURE3D])
667 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
668 checkGLcall("glGenTextures");
669 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
671 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
672 checkGLcall("glBindTexture");
674 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
675 checkGLcall("glTexImage3D");
678 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
680 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
681 checkGLcall("glGenTextures");
682 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
684 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
685 checkGLcall("glBindTexture");
687 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
689 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
690 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
691 checkGLcall("glTexImage2D");
697 /* Context activation is done by the caller. */
698 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
700 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
702 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
704 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
705 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
708 if (gl_info->supported[EXT_TEXTURE3D])
710 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
711 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
714 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
716 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
717 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
720 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
721 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
723 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
724 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
725 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
726 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
729 static LONG fullscreen_style(LONG style)
731 /* Make sure the window is managed, otherwise we won't get keyboard input. */
732 style |= WS_POPUP | WS_SYSMENU;
733 style &= ~(WS_CAPTION | WS_THICKFRAME);
735 return style;
738 static LONG fullscreen_exstyle(LONG exstyle)
740 /* Filter out window decorations. */
741 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
743 return exstyle;
746 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
748 BOOL filter_messages;
749 LONG style, exstyle;
751 TRACE("Setting up window %p for fullscreen mode.\n", window);
753 if (device->style || device->exStyle)
755 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
756 window, device->style, device->exStyle);
759 device->style = GetWindowLongW(window, GWL_STYLE);
760 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
762 style = fullscreen_style(device->style);
763 exstyle = fullscreen_exstyle(device->exStyle);
765 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
766 device->style, device->exStyle, style, exstyle);
768 filter_messages = device->filter_messages;
769 device->filter_messages = TRUE;
771 SetWindowLongW(window, GWL_STYLE, style);
772 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
773 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
775 device->filter_messages = filter_messages;
778 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
780 BOOL filter_messages;
781 LONG style, exstyle;
783 if (!device->style && !device->exStyle) return;
785 style = GetWindowLongW(window, GWL_STYLE);
786 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
788 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
789 * application, and we want to ignore them in the test below, since it's
790 * not the application's fault that they changed. Additionally, we want to
791 * preserve the current status of these flags (i.e. don't restore them) to
792 * more closely emulate the behavior of Direct3D, which leaves these flags
793 * alone when returning to windowed mode. */
794 device->style ^= (device->style ^ style) & WS_VISIBLE;
795 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
797 TRACE("Restoring window style of window %p to %08x, %08x.\n",
798 window, device->style, device->exStyle);
800 filter_messages = device->filter_messages;
801 device->filter_messages = TRUE;
803 /* Only restore the style if the application didn't modify it during the
804 * fullscreen phase. Some applications change it before calling Reset()
805 * when switching between windowed and fullscreen modes (HL2), some
806 * depend on the original style (Eve Online). */
807 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
809 SetWindowLongW(window, GWL_STYLE, device->style);
810 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
812 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
814 device->filter_messages = filter_messages;
816 /* Delete the old values. */
817 device->style = 0;
818 device->exStyle = 0;
821 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
823 TRACE("device %p, window %p.\n", device, window);
825 if (!wined3d_register_window(window, device))
827 ERR("Failed to register window %p.\n", window);
828 return E_FAIL;
831 InterlockedExchangePointer((void **)&device->focus_window, window);
832 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
834 return WINED3D_OK;
837 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
839 TRACE("device %p.\n", device);
841 if (device->focus_window) wined3d_unregister_window(device->focus_window);
842 InterlockedExchangePointer((void **)&device->focus_window, NULL);
845 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
847 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
848 unsigned int i;
850 if (device->fb.render_targets)
852 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
854 wined3d_device_set_render_target(device, i, NULL, FALSE);
856 if (swapchain->back_buffers && swapchain->back_buffers[0])
857 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], TRUE);
860 wined3d_device_set_depth_stencil(device, ds_enable ? device->auto_depth_stencil : NULL);
861 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
864 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
865 struct wined3d_swapchain_desc *swapchain_desc)
867 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
868 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
869 struct wined3d_swapchain *swapchain = NULL;
870 struct wined3d_context *context;
871 DWORD clear_flags = 0;
872 HRESULT hr;
874 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
876 if (device->d3d_initialized)
877 return WINED3DERR_INVALIDCALL;
878 if (device->wined3d->flags & WINED3D_NO3D)
879 return WINED3DERR_INVALIDCALL;
881 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
882 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
884 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
885 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
887 TRACE("Shader private data couldn't be allocated\n");
888 goto err_out;
890 if (FAILED(hr = device->blitter->alloc_private(device)))
892 TRACE("Blitter private data couldn't be allocated\n");
893 goto err_out;
896 /* Setup the implicit swapchain. This also initializes a context. */
897 TRACE("Creating implicit swapchain\n");
898 hr = device->device_parent->ops->create_swapchain(device->device_parent,
899 swapchain_desc, &swapchain);
900 if (FAILED(hr))
902 WARN("Failed to create implicit swapchain\n");
903 goto err_out;
906 device->swapchain_count = 1;
907 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
908 if (!device->swapchains)
910 ERR("Out of memory!\n");
911 goto err_out;
913 device->swapchains[0] = swapchain;
914 device_init_swapchain_state(device, swapchain);
916 context = context_acquire(device, swapchain->front_buffer);
918 create_dummy_textures(device, context);
920 device->contexts[0]->last_was_rhw = 0;
922 switch (wined3d_settings.offscreen_rendering_mode)
924 case ORM_FBO:
925 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
926 break;
928 case ORM_BACKBUFFER:
930 if (context_get_current()->aux_buffers > 0)
932 TRACE("Using auxiliary buffer for offscreen rendering\n");
933 device->offscreenBuffer = GL_AUX0;
935 else
937 TRACE("Using back buffer for offscreen rendering\n");
938 device->offscreenBuffer = GL_BACK;
943 TRACE("All defaults now set up, leaving 3D init.\n");
945 context_release(context);
947 /* Clear the screen */
948 if (swapchain->back_buffers && swapchain->back_buffers[0])
949 clear_flags |= WINED3DCLEAR_TARGET;
950 if (swapchain_desc->enable_auto_depth_stencil)
951 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
952 if (clear_flags)
953 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
955 device->d3d_initialized = TRUE;
957 if (wined3d_settings.logo)
958 device_load_logo(device, wined3d_settings.logo);
959 return WINED3D_OK;
961 err_out:
962 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
963 HeapFree(GetProcessHeap(), 0, device->swapchains);
964 device->swapchain_count = 0;
965 if (swapchain)
966 wined3d_swapchain_decref(swapchain);
967 if (device->blit_priv)
968 device->blitter->free_private(device);
969 if (device->shader_priv)
970 device->shader_backend->shader_free_private(device);
972 return hr;
975 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
976 struct wined3d_swapchain_desc *swapchain_desc)
978 struct wined3d_swapchain *swapchain = NULL;
979 HRESULT hr;
981 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
983 /* Setup the implicit swapchain */
984 TRACE("Creating implicit swapchain\n");
985 hr = device->device_parent->ops->create_swapchain(device->device_parent,
986 swapchain_desc, &swapchain);
987 if (FAILED(hr))
989 WARN("Failed to create implicit swapchain\n");
990 goto err_out;
993 device->swapchain_count = 1;
994 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
995 if (!device->swapchains)
997 ERR("Out of memory!\n");
998 goto err_out;
1000 device->swapchains[0] = swapchain;
1001 return WINED3D_OK;
1003 err_out:
1004 wined3d_swapchain_decref(swapchain);
1005 return hr;
1008 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1010 struct wined3d_resource *resource, *cursor;
1011 const struct wined3d_gl_info *gl_info;
1012 struct wined3d_context *context;
1013 struct wined3d_surface *surface;
1014 UINT i;
1016 TRACE("device %p.\n", device);
1018 if (!device->d3d_initialized)
1019 return WINED3DERR_INVALIDCALL;
1021 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1022 * it was created. Thus make sure a context is active for the glDelete* calls
1024 context = context_acquire(device, NULL);
1025 gl_info = context->gl_info;
1027 if (device->logo_texture)
1028 wined3d_texture_decref(device->logo_texture);
1029 if (device->cursor_texture)
1030 wined3d_texture_decref(device->cursor_texture);
1032 state_unbind_resources(&device->state);
1034 /* Unload resources */
1035 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1037 TRACE("Unloading resource %p.\n", resource);
1039 resource->resource_ops->resource_unload(resource);
1042 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1043 * private data, it might contain opengl pointers
1045 if (device->depth_blt_texture)
1047 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1048 device->depth_blt_texture = 0;
1051 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1052 device->blitter->free_private(device);
1053 device->shader_backend->shader_free_private(device);
1054 destroy_dummy_textures(device, gl_info);
1056 /* Release the buffers (with sanity checks)*/
1057 if (device->onscreen_depth_stencil)
1059 surface = device->onscreen_depth_stencil;
1060 device->onscreen_depth_stencil = NULL;
1061 wined3d_surface_decref(surface);
1064 if (device->fb.depth_stencil)
1066 surface = device->fb.depth_stencil;
1068 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1070 device->fb.depth_stencil = NULL;
1071 wined3d_surface_decref(surface);
1074 if (device->auto_depth_stencil)
1076 surface = device->auto_depth_stencil;
1077 device->auto_depth_stencil = NULL;
1078 if (wined3d_surface_decref(surface))
1079 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1082 for (i = 0; i < gl_info->limits.buffers; ++i)
1084 wined3d_device_set_render_target(device, i, NULL, FALSE);
1087 context_release(context);
1089 for (i = 0; i < device->swapchain_count; ++i)
1091 TRACE("Releasing the implicit swapchain %u.\n", i);
1092 if (wined3d_swapchain_decref(device->swapchains[i]))
1093 FIXME("Something's still holding the implicit swapchain.\n");
1096 HeapFree(GetProcessHeap(), 0, device->swapchains);
1097 device->swapchains = NULL;
1098 device->swapchain_count = 0;
1100 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1101 device->fb.render_targets = NULL;
1103 device->d3d_initialized = FALSE;
1105 return WINED3D_OK;
1108 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1110 unsigned int i;
1112 for (i = 0; i < device->swapchain_count; ++i)
1114 TRACE("Releasing the implicit swapchain %u.\n", i);
1115 if (wined3d_swapchain_decref(device->swapchains[i]))
1116 FIXME("Something's still holding the implicit swapchain.\n");
1119 HeapFree(GetProcessHeap(), 0, device->swapchains);
1120 device->swapchains = NULL;
1121 device->swapchain_count = 0;
1122 return WINED3D_OK;
1125 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1126 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1127 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1129 * There is no way to deactivate thread safety once it is enabled.
1131 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1133 TRACE("device %p.\n", device);
1135 /* For now just store the flag (needed in case of ddraw). */
1136 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1139 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1141 TRACE("device %p.\n", device);
1143 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1144 wine_dbgstr_longlong(device->adapter->vram_bytes),
1145 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1146 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1148 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1151 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1152 struct wined3d_buffer *buffer, UINT offset)
1154 struct wined3d_stream_output *stream;
1155 struct wined3d_buffer *prev_buffer;
1157 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1159 if (idx >= MAX_STREAM_OUT)
1161 WARN("Invalid stream output %u.\n", idx);
1162 return;
1165 stream = &device->update_state->stream_output[idx];
1166 prev_buffer = stream->buffer;
1168 if (buffer)
1169 wined3d_buffer_incref(buffer);
1170 stream->buffer = buffer;
1171 stream->offset = offset;
1172 if (!device->recording)
1173 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1174 if (prev_buffer)
1175 wined3d_buffer_decref(prev_buffer);
1178 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1179 UINT idx, UINT *offset)
1181 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1183 if (idx >= MAX_STREAM_OUT)
1185 WARN("Invalid stream output %u.\n", idx);
1186 return NULL;
1189 *offset = device->state.stream_output[idx].offset;
1190 return device->state.stream_output[idx].buffer;
1193 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1194 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1196 struct wined3d_stream_state *stream;
1197 struct wined3d_buffer *prev_buffer;
1199 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1200 device, stream_idx, buffer, offset, stride);
1202 if (stream_idx >= MAX_STREAMS)
1204 WARN("Stream index %u out of range.\n", stream_idx);
1205 return WINED3DERR_INVALIDCALL;
1207 else if (offset & 0x3)
1209 WARN("Offset %u is not 4 byte aligned.\n", offset);
1210 return WINED3DERR_INVALIDCALL;
1213 stream = &device->update_state->streams[stream_idx];
1214 prev_buffer = stream->buffer;
1216 if (device->recording)
1217 device->recording->changed.streamSource |= 1 << stream_idx;
1219 if (prev_buffer == buffer
1220 && stream->stride == stride
1221 && stream->offset == offset)
1223 TRACE("Application is setting the old values over, nothing to do.\n");
1224 return WINED3D_OK;
1227 stream->buffer = buffer;
1228 if (buffer)
1230 stream->stride = stride;
1231 stream->offset = offset;
1234 if (buffer)
1235 wined3d_buffer_incref(buffer);
1236 if (!device->recording)
1237 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1238 if (prev_buffer)
1239 wined3d_buffer_decref(prev_buffer);
1241 return WINED3D_OK;
1244 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1245 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1247 const struct wined3d_stream_state *stream;
1249 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1250 device, stream_idx, buffer, offset, stride);
1252 if (stream_idx >= MAX_STREAMS)
1254 WARN("Stream index %u out of range.\n", stream_idx);
1255 return WINED3DERR_INVALIDCALL;
1258 stream = &device->state.streams[stream_idx];
1259 *buffer = stream->buffer;
1260 if (*buffer)
1261 wined3d_buffer_incref(*buffer);
1262 if (offset)
1263 *offset = stream->offset;
1264 *stride = stream->stride;
1266 return WINED3D_OK;
1269 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1271 struct wined3d_stream_state *stream;
1272 UINT old_flags, old_freq;
1274 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1276 /* Verify input. At least in d3d9 this is invalid. */
1277 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1279 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1280 return WINED3DERR_INVALIDCALL;
1282 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1284 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1285 return WINED3DERR_INVALIDCALL;
1287 if (!divider)
1289 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1290 return WINED3DERR_INVALIDCALL;
1293 stream = &device->update_state->streams[stream_idx];
1294 old_flags = stream->flags;
1295 old_freq = stream->frequency;
1297 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1298 stream->frequency = divider & 0x7fffff;
1300 if (device->recording)
1301 device->recording->changed.streamFreq |= 1 << stream_idx;
1302 else if (stream->frequency != old_freq || stream->flags != old_flags)
1303 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1305 return WINED3D_OK;
1308 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1309 UINT stream_idx, UINT *divider)
1311 const struct wined3d_stream_state *stream;
1313 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1315 stream = &device->state.streams[stream_idx];
1316 *divider = stream->flags | stream->frequency;
1318 TRACE("Returning %#x.\n", *divider);
1320 return WINED3D_OK;
1323 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1324 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1326 TRACE("device %p, state %s, matrix %p.\n",
1327 device, debug_d3dtstype(d3dts), matrix);
1328 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1329 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1330 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1331 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1333 /* Handle recording of state blocks. */
1334 if (device->recording)
1336 TRACE("Recording... not performing anything.\n");
1337 device->recording->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1338 device->update_state->transforms[d3dts] = *matrix;
1339 return;
1342 /* If the new matrix is the same as the current one,
1343 * we cut off any further processing. this seems to be a reasonable
1344 * optimization because as was noticed, some apps (warcraft3 for example)
1345 * tend towards setting the same matrix repeatedly for some reason.
1347 * From here on we assume that the new matrix is different, wherever it matters. */
1348 if (!memcmp(&device->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1350 TRACE("The application is setting the same matrix over again.\n");
1351 return;
1354 device->state.transforms[d3dts] = *matrix;
1355 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1358 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1359 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1361 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1363 *matrix = device->state.transforms[state];
1366 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1367 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1369 const struct wined3d_matrix *mat;
1370 struct wined3d_matrix temp;
1372 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1374 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1375 * below means it will be recorded in a state block change, but it
1376 * works regardless where it is recorded.
1377 * If this is found to be wrong, change to StateBlock. */
1378 if (state > HIGHEST_TRANSFORMSTATE)
1380 WARN("Unhandled transform state %#x.\n", state);
1381 return;
1384 mat = &device->update_state->transforms[state];
1385 multiply_matrix(&temp, mat, matrix);
1387 /* Apply change via set transform - will reapply to eg. lights this way. */
1388 wined3d_device_set_transform(device, state, &temp);
1391 /* Note lights are real special cases. Although the device caps state only
1392 * e.g. 8 are supported, you can reference any indexes you want as long as
1393 * that number max are enabled at any one point in time. Therefore since the
1394 * indices can be anything, we need a hashmap of them. However, this causes
1395 * stateblock problems. When capturing the state block, I duplicate the
1396 * hashmap, but when recording, just build a chain pretty much of commands to
1397 * be replayed. */
1398 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1399 UINT light_idx, const struct wined3d_light *light)
1401 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1402 struct wined3d_light_info *object = NULL;
1403 struct list *e;
1404 float rho;
1406 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1408 /* Check the parameter range. Need for speed most wanted sets junk lights
1409 * which confuse the GL driver. */
1410 if (!light)
1411 return WINED3DERR_INVALIDCALL;
1413 switch (light->type)
1415 case WINED3D_LIGHT_POINT:
1416 case WINED3D_LIGHT_SPOT:
1417 case WINED3D_LIGHT_PARALLELPOINT:
1418 case WINED3D_LIGHT_GLSPOT:
1419 /* Incorrect attenuation values can cause the gl driver to crash.
1420 * Happens with Need for speed most wanted. */
1421 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1423 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1424 return WINED3DERR_INVALIDCALL;
1426 break;
1428 case WINED3D_LIGHT_DIRECTIONAL:
1429 /* Ignores attenuation */
1430 break;
1432 default:
1433 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1434 return WINED3DERR_INVALIDCALL;
1437 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1439 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1440 if (object->OriginalIndex == light_idx)
1441 break;
1442 object = NULL;
1445 if (!object)
1447 TRACE("Adding new light\n");
1448 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1449 if (!object)
1450 return E_OUTOFMEMORY;
1452 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1453 object->glIndex = -1;
1454 object->OriginalIndex = light_idx;
1457 /* Initialize the object. */
1458 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1459 light_idx, light->type,
1460 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1461 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1462 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1463 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1464 light->direction.x, light->direction.y, light->direction.z);
1465 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1466 light->range, light->falloff, light->theta, light->phi);
1468 /* Update the live definitions if the light is currently assigned a glIndex. */
1469 if (object->glIndex != -1 && !device->recording)
1471 if (object->OriginalParms.type != light->type)
1472 device_invalidate_state(device, STATE_LIGHT_TYPE);
1473 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1476 /* Save away the information. */
1477 object->OriginalParms = *light;
1479 switch (light->type)
1481 case WINED3D_LIGHT_POINT:
1482 /* Position */
1483 object->lightPosn[0] = light->position.x;
1484 object->lightPosn[1] = light->position.y;
1485 object->lightPosn[2] = light->position.z;
1486 object->lightPosn[3] = 1.0f;
1487 object->cutoff = 180.0f;
1488 /* FIXME: Range */
1489 break;
1491 case WINED3D_LIGHT_DIRECTIONAL:
1492 /* Direction */
1493 object->lightPosn[0] = -light->direction.x;
1494 object->lightPosn[1] = -light->direction.y;
1495 object->lightPosn[2] = -light->direction.z;
1496 object->lightPosn[3] = 0.0f;
1497 object->exponent = 0.0f;
1498 object->cutoff = 180.0f;
1499 break;
1501 case WINED3D_LIGHT_SPOT:
1502 /* Position */
1503 object->lightPosn[0] = light->position.x;
1504 object->lightPosn[1] = light->position.y;
1505 object->lightPosn[2] = light->position.z;
1506 object->lightPosn[3] = 1.0f;
1508 /* Direction */
1509 object->lightDirn[0] = light->direction.x;
1510 object->lightDirn[1] = light->direction.y;
1511 object->lightDirn[2] = light->direction.z;
1512 object->lightDirn[3] = 1.0f;
1514 /* opengl-ish and d3d-ish spot lights use too different models
1515 * for the light "intensity" as a function of the angle towards
1516 * the main light direction, so we only can approximate very
1517 * roughly. However, spot lights are rather rarely used in games
1518 * (if ever used at all). Furthermore if still used, probably
1519 * nobody pays attention to such details. */
1520 if (!light->falloff)
1522 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1523 * equations have the falloff resp. exponent parameter as an
1524 * exponent, so the spot light lighting will always be 1.0 for
1525 * both of them, and we don't have to care for the rest of the
1526 * rather complex calculation. */
1527 object->exponent = 0.0f;
1529 else
1531 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1532 if (rho < 0.0001f)
1533 rho = 0.0001f;
1534 object->exponent = -0.3f / logf(cosf(rho / 2));
1537 if (object->exponent > 128.0f)
1538 object->exponent = 128.0f;
1540 object->cutoff = (float)(light->phi * 90 / M_PI);
1541 /* FIXME: Range */
1542 break;
1544 default:
1545 FIXME("Unrecognized light type %#x.\n", light->type);
1548 return WINED3D_OK;
1551 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1552 UINT light_idx, struct wined3d_light *light)
1554 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1555 struct wined3d_light_info *light_info = NULL;
1556 struct list *e;
1558 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1560 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1562 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1563 if (light_info->OriginalIndex == light_idx)
1564 break;
1565 light_info = NULL;
1568 if (!light_info)
1570 TRACE("Light information requested but light not defined\n");
1571 return WINED3DERR_INVALIDCALL;
1574 *light = light_info->OriginalParms;
1575 return WINED3D_OK;
1578 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1580 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1581 struct wined3d_light_info *light_info = NULL;
1582 struct list *e;
1584 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1586 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1588 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1589 if (light_info->OriginalIndex == light_idx)
1590 break;
1591 light_info = NULL;
1593 TRACE("Found light %p.\n", light_info);
1595 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1596 if (!light_info)
1598 TRACE("Light enabled requested but light not defined, so defining one!\n");
1599 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1601 /* Search for it again! Should be fairly quick as near head of list. */
1602 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1604 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1605 if (light_info->OriginalIndex == light_idx)
1606 break;
1607 light_info = NULL;
1609 if (!light_info)
1611 FIXME("Adding default lights has failed dismally\n");
1612 return WINED3DERR_INVALIDCALL;
1616 if (!enable)
1618 if (light_info->glIndex != -1)
1620 if (!device->recording)
1622 device_invalidate_state(device, STATE_LIGHT_TYPE);
1623 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1626 device->update_state->lights[light_info->glIndex] = NULL;
1627 light_info->glIndex = -1;
1629 else
1631 TRACE("Light already disabled, nothing to do\n");
1633 light_info->enabled = FALSE;
1635 else
1637 light_info->enabled = TRUE;
1638 if (light_info->glIndex != -1)
1640 TRACE("Nothing to do as light was enabled\n");
1642 else
1644 unsigned int i;
1645 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1646 /* Find a free GL light. */
1647 for (i = 0; i < gl_info->limits.lights; ++i)
1649 if (!device->update_state->lights[i])
1651 device->update_state->lights[i] = light_info;
1652 light_info->glIndex = i;
1653 break;
1656 if (light_info->glIndex == -1)
1658 /* Our tests show that Windows returns D3D_OK in this situation, even with
1659 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1660 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1661 * as well for those lights.
1663 * TODO: Test how this affects rendering. */
1664 WARN("Too many concurrently active lights\n");
1665 return WINED3D_OK;
1668 /* i == light_info->glIndex */
1669 if (!device->recording)
1671 device_invalidate_state(device, STATE_LIGHT_TYPE);
1672 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1677 return WINED3D_OK;
1680 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1682 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1683 struct wined3d_light_info *light_info = NULL;
1684 struct list *e;
1686 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1688 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1690 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1691 if (light_info->OriginalIndex == light_idx)
1692 break;
1693 light_info = NULL;
1696 if (!light_info)
1698 TRACE("Light enabled state requested but light not defined.\n");
1699 return WINED3DERR_INVALIDCALL;
1701 /* true is 128 according to SetLightEnable */
1702 *enable = light_info->enabled ? 128 : 0;
1703 return WINED3D_OK;
1706 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1707 UINT plane_idx, const struct wined3d_vec4 *plane)
1709 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1711 /* Validate plane_idx. */
1712 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1714 TRACE("Application has requested clipplane this device doesn't support.\n");
1715 return WINED3DERR_INVALIDCALL;
1718 if (device->recording)
1719 device->recording->changed.clipplane |= 1 << plane_idx;
1721 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1723 TRACE("Application is setting old values over, nothing to do.\n");
1724 return WINED3D_OK;
1727 device->update_state->clip_planes[plane_idx] = *plane;
1729 if (!device->recording)
1730 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1732 return WINED3D_OK;
1735 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1736 UINT plane_idx, struct wined3d_vec4 *plane)
1738 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1740 /* Validate plane_idx. */
1741 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1743 TRACE("Application has requested clipplane this device doesn't support.\n");
1744 return WINED3DERR_INVALIDCALL;
1747 *plane = device->state.clip_planes[plane_idx];
1749 return WINED3D_OK;
1752 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1753 const struct wined3d_clip_status *clip_status)
1755 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1757 if (!clip_status)
1758 return WINED3DERR_INVALIDCALL;
1760 return WINED3D_OK;
1763 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1764 struct wined3d_clip_status *clip_status)
1766 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1768 if (!clip_status)
1769 return WINED3DERR_INVALIDCALL;
1771 return WINED3D_OK;
1774 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1776 TRACE("device %p, material %p.\n", device, material);
1778 device->update_state->material = *material;
1780 if (device->recording)
1781 device->recording->changed.material = TRUE;
1782 else
1783 wined3d_cs_emit_set_material(device->cs, material);
1786 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1788 TRACE("device %p, material %p.\n", device, material);
1790 *material = device->state.material;
1792 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1793 material->diffuse.r, material->diffuse.g,
1794 material->diffuse.b, material->diffuse.a);
1795 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1796 material->ambient.r, material->ambient.g,
1797 material->ambient.b, material->ambient.a);
1798 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1799 material->specular.r, material->specular.g,
1800 material->specular.b, material->specular.a);
1801 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1802 material->emissive.r, material->emissive.g,
1803 material->emissive.b, material->emissive.a);
1804 TRACE("power %.8e.\n", material->power);
1807 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1808 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1810 enum wined3d_format_id prev_format;
1811 struct wined3d_buffer *prev_buffer;
1813 TRACE("device %p, buffer %p, format %s.\n",
1814 device, buffer, debug_d3dformat(format_id));
1816 prev_buffer = device->update_state->index_buffer;
1817 prev_format = device->update_state->index_format;
1819 device->update_state->index_buffer = buffer;
1820 device->update_state->index_format = format_id;
1822 if (device->recording)
1823 device->recording->changed.indices = TRUE;
1825 if (prev_buffer == buffer && prev_format == format_id)
1826 return;
1828 if (buffer)
1829 wined3d_buffer_incref(buffer);
1830 if (!device->recording)
1831 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1832 if (prev_buffer)
1833 wined3d_buffer_decref(prev_buffer);
1836 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1837 enum wined3d_format_id *format)
1839 TRACE("device %p, format %p.\n", device, format);
1841 *format = device->state.index_format;
1842 return device->state.index_buffer;
1845 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1847 TRACE("device %p, base_index %d.\n", device, base_index);
1849 device->update_state->base_vertex_index = base_index;
1852 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1854 TRACE("device %p.\n", device);
1856 return device->state.base_vertex_index;
1859 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1861 TRACE("device %p, viewport %p.\n", device, viewport);
1862 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1863 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1865 device->update_state->viewport = *viewport;
1867 /* Handle recording of state blocks */
1868 if (device->recording)
1870 TRACE("Recording... not performing anything\n");
1871 device->recording->changed.viewport = TRUE;
1872 return;
1875 wined3d_cs_emit_set_viewport(device->cs, viewport);
1878 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1880 TRACE("device %p, viewport %p.\n", device, viewport);
1882 *viewport = device->state.viewport;
1885 static void resolve_depth_buffer(struct wined3d_state *state)
1887 struct wined3d_texture *texture = state->textures[0];
1888 struct wined3d_surface *depth_stencil, *surface;
1890 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1891 || !(texture->resource.format->flags & WINED3DFMT_FLAG_DEPTH))
1892 return;
1893 surface = surface_from_resource(texture->sub_resources[0]);
1894 depth_stencil = state->fb->depth_stencil;
1895 if (!depth_stencil)
1896 return;
1898 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1901 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1902 enum wined3d_render_state state, DWORD value)
1904 DWORD old_value = device->state.render_states[state];
1906 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1908 device->update_state->render_states[state] = value;
1910 /* Handle recording of state blocks. */
1911 if (device->recording)
1913 TRACE("Recording... not performing anything.\n");
1914 device->recording->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
1915 return;
1918 /* Compared here and not before the assignment to allow proper stateblock recording. */
1919 if (value == old_value)
1920 TRACE("Application is setting the old value over, nothing to do.\n");
1921 else
1922 wined3d_cs_emit_set_render_state(device->cs, state, value);
1924 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1926 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1927 resolve_depth_buffer(&device->state);
1931 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1933 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1935 return device->state.render_states[state];
1938 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1939 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1941 DWORD old_value;
1943 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1944 device, sampler_idx, debug_d3dsamplerstate(state), value);
1946 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1947 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1949 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1951 WARN("Invalid sampler %u.\n", sampler_idx);
1952 return; /* Windows accepts overflowing this array ... we do not. */
1955 old_value = device->state.sampler_states[sampler_idx][state];
1956 device->update_state->sampler_states[sampler_idx][state] = value;
1958 /* Handle recording of state blocks. */
1959 if (device->recording)
1961 TRACE("Recording... not performing anything.\n");
1962 device->recording->changed.samplerState[sampler_idx] |= 1 << state;
1963 return;
1966 if (old_value == value)
1968 TRACE("Application is setting the old value over, nothing to do.\n");
1969 return;
1972 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
1975 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
1976 UINT sampler_idx, enum wined3d_sampler_state state)
1978 TRACE("device %p, sampler_idx %u, state %s.\n",
1979 device, sampler_idx, debug_d3dsamplerstate(state));
1981 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1982 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1984 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1986 WARN("Invalid sampler %u.\n", sampler_idx);
1987 return 0; /* Windows accepts overflowing this array ... we do not. */
1990 return device->state.sampler_states[sampler_idx][state];
1993 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
1995 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
1997 if (device->recording)
1998 device->recording->changed.scissorRect = TRUE;
2000 if (EqualRect(&device->update_state->scissor_rect, rect))
2002 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2003 return;
2005 CopyRect(&device->update_state->scissor_rect, rect);
2007 if (device->recording)
2009 TRACE("Recording... not performing anything.\n");
2010 return;
2013 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2016 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2018 TRACE("device %p, rect %p.\n", device, rect);
2020 *rect = device->state.scissor_rect;
2021 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2024 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2025 struct wined3d_vertex_declaration *declaration)
2027 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2029 TRACE("device %p, declaration %p.\n", device, declaration);
2031 if (device->recording)
2032 device->recording->changed.vertexDecl = TRUE;
2034 if (declaration == prev)
2035 return;
2037 if (declaration)
2038 wined3d_vertex_declaration_incref(declaration);
2039 device->update_state->vertex_declaration = declaration;
2040 if (!device->recording)
2041 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2042 if (prev)
2043 wined3d_vertex_declaration_decref(prev);
2046 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2048 TRACE("device %p.\n", device);
2050 return device->state.vertex_declaration;
2053 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2055 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2057 TRACE("device %p, shader %p.\n", device, shader);
2059 if (device->recording)
2060 device->recording->changed.vertexShader = TRUE;
2062 if (shader == prev)
2063 return;
2065 if (shader)
2066 wined3d_shader_incref(shader);
2067 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2068 if (!device->recording)
2069 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2070 if (prev)
2071 wined3d_shader_decref(prev);
2074 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2076 TRACE("device %p.\n", device);
2078 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2081 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2082 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2084 struct wined3d_buffer *prev;
2086 if (idx >= MAX_CONSTANT_BUFFERS)
2088 WARN("Invalid constant buffer index %u.\n", idx);
2089 return;
2092 prev = device->update_state->cb[type][idx];
2093 if (buffer == prev)
2094 return;
2096 if (buffer)
2097 wined3d_buffer_incref(buffer);
2098 device->update_state->cb[type][idx] = buffer;
2099 if (!device->recording)
2100 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2101 if (prev)
2102 wined3d_buffer_decref(prev);
2105 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2107 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2109 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2112 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2114 TRACE("device %p, idx %u.\n", device, idx);
2116 if (idx >= MAX_CONSTANT_BUFFERS)
2118 WARN("Invalid constant buffer index %u.\n", idx);
2119 return NULL;
2122 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2125 static void wined3d_device_set_sampler(struct wined3d_device *device,
2126 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2128 struct wined3d_sampler *prev;
2130 if (idx >= MAX_SAMPLER_OBJECTS)
2132 WARN("Invalid sampler index %u.\n", idx);
2133 return;
2136 prev = device->update_state->sampler[type][idx];
2137 if (sampler == prev)
2138 return;
2140 if (sampler)
2141 wined3d_sampler_incref(sampler);
2142 device->update_state->sampler[type][idx] = sampler;
2143 if (!device->recording)
2144 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2145 if (prev)
2146 wined3d_sampler_decref(prev);
2149 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2151 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2153 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2156 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2158 TRACE("device %p, idx %u.\n", device, idx);
2160 if (idx >= MAX_SAMPLER_OBJECTS)
2162 WARN("Invalid sampler index %u.\n", idx);
2163 return NULL;
2166 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2169 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2171 UINT i;
2173 for (i = 0; i < device->context_count; ++i)
2175 device->contexts[i]->constant_update_mask |= mask;
2179 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2180 UINT start_register, const BOOL *constants, UINT bool_count)
2182 UINT count = min(bool_count, MAX_CONST_B - start_register);
2183 UINT i;
2185 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2186 device, start_register, constants, bool_count);
2188 if (!constants || start_register >= MAX_CONST_B)
2189 return WINED3DERR_INVALIDCALL;
2191 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2192 for (i = 0; i < count; ++i)
2193 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2195 if (device->recording)
2197 for (i = start_register; i < count + start_register; ++i)
2198 device->recording->changed.vertexShaderConstantsB |= (1 << i);
2200 else
2202 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2205 return WINED3D_OK;
2208 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2209 UINT start_register, BOOL *constants, UINT bool_count)
2211 UINT count = min(bool_count, MAX_CONST_B - start_register);
2213 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2214 device, start_register, constants, bool_count);
2216 if (!constants || start_register >= MAX_CONST_B)
2217 return WINED3DERR_INVALIDCALL;
2219 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2221 return WINED3D_OK;
2224 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2225 UINT start_register, const int *constants, UINT vector4i_count)
2227 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2228 UINT i;
2230 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2231 device, start_register, constants, vector4i_count);
2233 if (!constants || start_register >= MAX_CONST_I)
2234 return WINED3DERR_INVALIDCALL;
2236 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2237 for (i = 0; i < count; ++i)
2238 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2239 constants[i * 4], constants[i * 4 + 1],
2240 constants[i * 4 + 2], constants[i * 4 + 3]);
2242 if (device->recording)
2244 for (i = start_register; i < count + start_register; ++i)
2245 device->recording->changed.vertexShaderConstantsI |= (1 << i);
2247 else
2249 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2252 return WINED3D_OK;
2255 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2256 UINT start_register, int *constants, UINT vector4i_count)
2258 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2260 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2261 device, start_register, constants, vector4i_count);
2263 if (!constants || start_register >= MAX_CONST_I)
2264 return WINED3DERR_INVALIDCALL;
2266 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2267 return WINED3D_OK;
2270 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2271 UINT start_register, const float *constants, UINT vector4f_count)
2273 UINT i;
2274 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2276 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2277 device, start_register, constants, vector4f_count);
2279 /* Specifically test start_register > limit to catch MAX_UINT overflows
2280 * when adding start_register + vector4f_count. */
2281 if (!constants
2282 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2283 || start_register > d3d_info->limits.vs_uniform_count)
2284 return WINED3DERR_INVALIDCALL;
2286 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2287 constants, vector4f_count * sizeof(float) * 4);
2288 if (TRACE_ON(d3d))
2290 for (i = 0; i < vector4f_count; ++i)
2291 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2292 constants[i * 4], constants[i * 4 + 1],
2293 constants[i * 4 + 2], constants[i * 4 + 3]);
2296 if (device->recording)
2297 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2298 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2299 else
2300 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2303 return WINED3D_OK;
2306 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2307 UINT start_register, float *constants, UINT vector4f_count)
2309 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2310 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2312 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2313 device, start_register, constants, vector4f_count);
2315 if (!constants || count < 0)
2316 return WINED3DERR_INVALIDCALL;
2318 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2320 return WINED3D_OK;
2323 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2325 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2327 TRACE("device %p, shader %p.\n", device, shader);
2329 if (device->recording)
2330 device->recording->changed.pixelShader = TRUE;
2332 if (shader == prev)
2333 return;
2335 if (shader)
2336 wined3d_shader_incref(shader);
2337 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2338 if (!device->recording)
2339 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2340 if (prev)
2341 wined3d_shader_decref(prev);
2344 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2346 TRACE("device %p.\n", device);
2348 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2351 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2353 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2355 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2358 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2360 TRACE("device %p, idx %u.\n", device, idx);
2362 if (idx >= MAX_CONSTANT_BUFFERS)
2364 WARN("Invalid constant buffer index %u.\n", idx);
2365 return NULL;
2368 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2371 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2373 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2375 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2378 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2380 TRACE("device %p, idx %u.\n", device, idx);
2382 if (idx >= MAX_SAMPLER_OBJECTS)
2384 WARN("Invalid sampler index %u.\n", idx);
2385 return NULL;
2388 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2391 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2392 UINT start_register, const BOOL *constants, UINT bool_count)
2394 UINT count = min(bool_count, MAX_CONST_B - start_register);
2395 UINT i;
2397 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2398 device, start_register, constants, bool_count);
2400 if (!constants || start_register >= MAX_CONST_B)
2401 return WINED3DERR_INVALIDCALL;
2403 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2404 for (i = 0; i < count; ++i)
2405 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2407 if (device->recording)
2409 for (i = start_register; i < count + start_register; ++i)
2410 device->recording->changed.pixelShaderConstantsB |= (1 << i);
2412 else
2414 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2417 return WINED3D_OK;
2420 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2421 UINT start_register, BOOL *constants, UINT bool_count)
2423 UINT count = min(bool_count, MAX_CONST_B - start_register);
2425 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2426 device, start_register, constants, bool_count);
2428 if (!constants || start_register >= MAX_CONST_B)
2429 return WINED3DERR_INVALIDCALL;
2431 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2433 return WINED3D_OK;
2436 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2437 UINT start_register, const int *constants, UINT vector4i_count)
2439 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2440 UINT i;
2442 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2443 device, start_register, constants, vector4i_count);
2445 if (!constants || start_register >= MAX_CONST_I)
2446 return WINED3DERR_INVALIDCALL;
2448 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2449 for (i = 0; i < count; ++i)
2450 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2451 constants[i * 4], constants[i * 4 + 1],
2452 constants[i * 4 + 2], constants[i * 4 + 3]);
2454 if (device->recording)
2456 for (i = start_register; i < count + start_register; ++i)
2457 device->recording->changed.pixelShaderConstantsI |= (1 << i);
2459 else
2461 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2464 return WINED3D_OK;
2467 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2468 UINT start_register, int *constants, UINT vector4i_count)
2470 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2472 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2473 device, start_register, constants, vector4i_count);
2475 if (!constants || start_register >= MAX_CONST_I)
2476 return WINED3DERR_INVALIDCALL;
2478 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2480 return WINED3D_OK;
2483 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2484 UINT start_register, const float *constants, UINT vector4f_count)
2486 UINT i;
2487 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2489 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2490 device, start_register, constants, vector4f_count);
2492 /* Specifically test start_register > limit to catch MAX_UINT overflows
2493 * when adding start_register + vector4f_count. */
2494 if (!constants
2495 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2496 || start_register > d3d_info->limits.ps_uniform_count)
2497 return WINED3DERR_INVALIDCALL;
2499 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2500 constants, vector4f_count * sizeof(float) * 4);
2501 if (TRACE_ON(d3d))
2503 for (i = 0; i < vector4f_count; ++i)
2504 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2505 constants[i * 4], constants[i * 4 + 1],
2506 constants[i * 4 + 2], constants[i * 4 + 3]);
2509 if (device->recording)
2510 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2511 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2512 else
2513 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2515 return WINED3D_OK;
2518 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2519 UINT start_register, float *constants, UINT vector4f_count)
2521 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2522 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2524 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2525 device, start_register, constants, vector4f_count);
2527 if (!constants || count < 0)
2528 return WINED3DERR_INVALIDCALL;
2530 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2532 return WINED3D_OK;
2535 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2537 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2539 TRACE("device %p, shader %p.\n", device, shader);
2541 if (device->recording || shader == prev)
2542 return;
2543 if (shader)
2544 wined3d_shader_incref(shader);
2545 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2546 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2547 if (prev)
2548 wined3d_shader_decref(prev);
2551 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2553 TRACE("device %p.\n", device);
2555 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2558 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2560 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2562 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2565 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2567 TRACE("device %p, idx %u.\n", device, idx);
2569 if (idx >= MAX_CONSTANT_BUFFERS)
2571 WARN("Invalid constant buffer index %u.\n", idx);
2572 return NULL;
2575 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2578 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2580 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2582 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2585 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2587 TRACE("device %p, idx %u.\n", device, idx);
2589 if (idx >= MAX_SAMPLER_OBJECTS)
2591 WARN("Invalid sampler index %u.\n", idx);
2592 return NULL;
2595 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2598 /* Context activation is done by the caller. */
2599 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2600 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2601 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2602 DWORD DestFVF)
2604 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2605 struct wined3d_viewport vp;
2606 UINT vertex_size;
2607 unsigned int i;
2608 BYTE *dest_ptr;
2609 BOOL doClip;
2610 DWORD numTextures;
2611 HRESULT hr;
2613 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
2615 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2618 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
2620 ERR("Source has no position mask\n");
2621 return WINED3DERR_INVALIDCALL;
2624 if (device->state.render_states[WINED3D_RS_CLIPPING])
2626 static BOOL warned = FALSE;
2628 * The clipping code is not quite correct. Some things need
2629 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2630 * so disable clipping for now.
2631 * (The graphics in Half-Life are broken, and my processvertices
2632 * test crashes with IDirect3DDevice3)
2633 doClip = TRUE;
2635 doClip = FALSE;
2636 if(!warned) {
2637 warned = TRUE;
2638 FIXME("Clipping is broken and disabled for now\n");
2641 else
2642 doClip = FALSE;
2644 vertex_size = get_flexible_vertex_size(DestFVF);
2645 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2647 WARN("Failed to map buffer, hr %#x.\n", hr);
2648 return hr;
2651 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2652 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2653 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2655 TRACE("View mat:\n");
2656 TRACE("%f %f %f %f\n", view_mat.u.s._11, view_mat.u.s._12, view_mat.u.s._13, view_mat.u.s._14);
2657 TRACE("%f %f %f %f\n", view_mat.u.s._21, view_mat.u.s._22, view_mat.u.s._23, view_mat.u.s._24);
2658 TRACE("%f %f %f %f\n", view_mat.u.s._31, view_mat.u.s._32, view_mat.u.s._33, view_mat.u.s._34);
2659 TRACE("%f %f %f %f\n", view_mat.u.s._41, view_mat.u.s._42, view_mat.u.s._43, view_mat.u.s._44);
2661 TRACE("Proj mat:\n");
2662 TRACE("%f %f %f %f\n", proj_mat.u.s._11, proj_mat.u.s._12, proj_mat.u.s._13, proj_mat.u.s._14);
2663 TRACE("%f %f %f %f\n", proj_mat.u.s._21, proj_mat.u.s._22, proj_mat.u.s._23, proj_mat.u.s._24);
2664 TRACE("%f %f %f %f\n", proj_mat.u.s._31, proj_mat.u.s._32, proj_mat.u.s._33, proj_mat.u.s._34);
2665 TRACE("%f %f %f %f\n", proj_mat.u.s._41, proj_mat.u.s._42, proj_mat.u.s._43, proj_mat.u.s._44);
2667 TRACE("World mat:\n");
2668 TRACE("%f %f %f %f\n", world_mat.u.s._11, world_mat.u.s._12, world_mat.u.s._13, world_mat.u.s._14);
2669 TRACE("%f %f %f %f\n", world_mat.u.s._21, world_mat.u.s._22, world_mat.u.s._23, world_mat.u.s._24);
2670 TRACE("%f %f %f %f\n", world_mat.u.s._31, world_mat.u.s._32, world_mat.u.s._33, world_mat.u.s._34);
2671 TRACE("%f %f %f %f\n", world_mat.u.s._41, world_mat.u.s._42, world_mat.u.s._43, world_mat.u.s._44);
2673 /* Get the viewport */
2674 wined3d_device_get_viewport(device, &vp);
2675 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2676 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2678 multiply_matrix(&mat,&view_mat,&world_mat);
2679 multiply_matrix(&mat,&proj_mat,&mat);
2681 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2683 for (i = 0; i < dwCount; i+= 1) {
2684 unsigned int tex_index;
2686 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2687 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2688 /* The position first */
2689 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2690 const float *p = (const float *)(element->data.addr + i * element->stride);
2691 float x, y, z, rhw;
2692 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2694 /* Multiplication with world, view and projection matrix */
2695 x = (p[0] * mat.u.s._11) + (p[1] * mat.u.s._21) + (p[2] * mat.u.s._31) + (1.0f * mat.u.s._41);
2696 y = (p[0] * mat.u.s._12) + (p[1] * mat.u.s._22) + (p[2] * mat.u.s._32) + (1.0f * mat.u.s._42);
2697 z = (p[0] * mat.u.s._13) + (p[1] * mat.u.s._23) + (p[2] * mat.u.s._33) + (1.0f * mat.u.s._43);
2698 rhw = (p[0] * mat.u.s._14) + (p[1] * mat.u.s._24) + (p[2] * mat.u.s._34) + (1.0f * mat.u.s._44);
2700 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2702 /* WARNING: The following things are taken from d3d7 and were not yet checked
2703 * against d3d8 or d3d9!
2706 /* Clipping conditions: From msdn
2708 * A vertex is clipped if it does not match the following requirements
2709 * -rhw < x <= rhw
2710 * -rhw < y <= rhw
2711 * 0 < z <= rhw
2712 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2714 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2715 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2719 if( !doClip ||
2720 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2721 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2722 ( rhw > eps ) ) ) {
2724 /* "Normal" viewport transformation (not clipped)
2725 * 1) The values are divided by rhw
2726 * 2) The y axis is negative, so multiply it with -1
2727 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2728 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2729 * 4) Multiply x with Width/2 and add Width/2
2730 * 5) The same for the height
2731 * 6) Add the viewpoint X and Y to the 2D coordinates and
2732 * The minimum Z value to z
2733 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2735 * Well, basically it's simply a linear transformation into viewport
2736 * coordinates
2739 x /= rhw;
2740 y /= rhw;
2741 z /= rhw;
2743 y *= -1;
2745 x *= vp.width / 2;
2746 y *= vp.height / 2;
2747 z *= vp.max_z - vp.min_z;
2749 x += vp.width / 2 + vp.x;
2750 y += vp.height / 2 + vp.y;
2751 z += vp.min_z;
2753 rhw = 1 / rhw;
2754 } else {
2755 /* That vertex got clipped
2756 * Contrary to OpenGL it is not dropped completely, it just
2757 * undergoes a different calculation.
2759 TRACE("Vertex got clipped\n");
2760 x += rhw;
2761 y += rhw;
2763 x /= 2;
2764 y /= 2;
2766 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2767 * outside of the main vertex buffer memory. That needs some more
2768 * investigation...
2772 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2775 ( (float *) dest_ptr)[0] = x;
2776 ( (float *) dest_ptr)[1] = y;
2777 ( (float *) dest_ptr)[2] = z;
2778 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2780 dest_ptr += 3 * sizeof(float);
2782 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2783 dest_ptr += sizeof(float);
2786 if (DestFVF & WINED3DFVF_PSIZE)
2787 dest_ptr += sizeof(DWORD);
2789 if (DestFVF & WINED3DFVF_NORMAL)
2791 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2792 const float *normal = (const float *)(element->data.addr + i * element->stride);
2793 /* AFAIK this should go into the lighting information */
2794 FIXME("Didn't expect the destination to have a normal\n");
2795 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2798 if (DestFVF & WINED3DFVF_DIFFUSE)
2800 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2801 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2802 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
2804 static BOOL warned = FALSE;
2806 if(!warned) {
2807 ERR("No diffuse color in source, but destination has one\n");
2808 warned = TRUE;
2811 *( (DWORD *) dest_ptr) = 0xffffffff;
2812 dest_ptr += sizeof(DWORD);
2814 else
2816 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2820 if (DestFVF & WINED3DFVF_SPECULAR)
2822 /* What's the color value in the feedback buffer? */
2823 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2824 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2825 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
2827 static BOOL warned = FALSE;
2829 if(!warned) {
2830 ERR("No specular color in source, but destination has one\n");
2831 warned = TRUE;
2834 *(DWORD *)dest_ptr = 0xff000000;
2835 dest_ptr += sizeof(DWORD);
2837 else
2839 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2843 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2845 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2846 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2847 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2849 ERR("No source texture, but destination requests one\n");
2850 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2852 else
2854 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2859 wined3d_buffer_unmap(dest);
2861 return WINED3D_OK;
2863 #undef copy_and_next
2865 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
2866 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
2867 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
2869 struct wined3d_state *state = &device->state;
2870 struct wined3d_stream_info stream_info;
2871 const struct wined3d_gl_info *gl_info;
2872 struct wined3d_context *context;
2873 struct wined3d_shader *vs;
2874 unsigned int i;
2875 HRESULT hr;
2876 WORD map;
2878 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
2879 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
2880 device, src_start_idx, dst_idx, vertex_count,
2881 dst_buffer, declaration, flags, dst_fvf);
2883 if (declaration)
2884 FIXME("Output vertex declaration not implemented yet.\n");
2886 /* Need any context to write to the vbo. */
2887 context = context_acquire(device, NULL);
2888 gl_info = context->gl_info;
2890 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
2891 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
2892 context_stream_info_from_declaration(context, state, &stream_info);
2893 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
2895 /* We can't convert FROM a VBO, and vertex buffers used to source into
2896 * process_vertices() are unlikely to ever be used for drawing. Release
2897 * VBOs in those buffers and fix up the stream_info structure.
2899 * Also apply the start index. */
2900 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
2902 struct wined3d_stream_info_element *e;
2903 struct wined3d_buffer *buffer;
2905 if (!(map & 1))
2906 continue;
2908 e = &stream_info.elements[i];
2909 buffer = state->streams[e->stream_idx].buffer;
2910 e->data.buffer_object = 0;
2911 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
2912 if (buffer->buffer_object)
2914 GL_EXTCALL(glDeleteBuffersARB(1, &buffer->buffer_object));
2915 buffer->buffer_object = 0;
2917 if (e->data.addr)
2918 e->data.addr += e->stride * src_start_idx;
2921 hr = process_vertices_strided(device, dst_idx, vertex_count,
2922 &stream_info, dst_buffer, flags, dst_fvf);
2924 context_release(context);
2926 return hr;
2929 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
2930 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
2932 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2933 DWORD old_value;
2935 TRACE("device %p, stage %u, state %s, value %#x.\n",
2936 device, stage, debug_d3dtexturestate(state), value);
2938 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
2940 WARN("Invalid state %#x passed.\n", state);
2941 return;
2944 if (stage >= d3d_info->limits.ffp_blend_stages)
2946 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
2947 stage, d3d_info->limits.ffp_blend_stages - 1);
2948 return;
2951 old_value = device->update_state->texture_states[stage][state];
2952 device->update_state->texture_states[stage][state] = value;
2954 if (device->recording)
2956 TRACE("Recording... not performing anything.\n");
2957 device->recording->changed.textureState[stage] |= 1 << state;
2958 return;
2961 /* Checked after the assignments to allow proper stateblock recording. */
2962 if (old_value == value)
2964 TRACE("Application is setting the old value over, nothing to do.\n");
2965 return;
2968 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
2971 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
2972 UINT stage, enum wined3d_texture_stage_state state)
2974 TRACE("device %p, stage %u, state %s.\n",
2975 device, stage, debug_d3dtexturestate(state));
2977 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
2979 WARN("Invalid state %#x passed.\n", state);
2980 return 0;
2983 return device->state.texture_states[stage][state];
2986 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
2987 UINT stage, struct wined3d_texture *texture)
2989 struct wined3d_texture *prev;
2991 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
2993 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
2994 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2996 /* Windows accepts overflowing this array... we do not. */
2997 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
2999 WARN("Ignoring invalid stage %u.\n", stage);
3000 return WINED3D_OK;
3003 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3005 WARN("Rejecting attempt to set scratch texture.\n");
3006 return WINED3DERR_INVALIDCALL;
3009 if (device->recording)
3010 device->recording->changed.textures |= 1 << stage;
3012 prev = device->update_state->textures[stage];
3013 TRACE("Previous texture %p.\n", prev);
3015 if (texture == prev)
3017 TRACE("App is setting the same texture again, nothing to do.\n");
3018 return WINED3D_OK;
3021 TRACE("Setting new texture to %p.\n", texture);
3022 device->update_state->textures[stage] = texture;
3024 if (texture)
3025 wined3d_texture_incref(texture);
3026 if (!device->recording)
3027 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3028 if (prev)
3029 wined3d_texture_decref(prev);
3031 return WINED3D_OK;
3034 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3036 TRACE("device %p, stage %u.\n", device, stage);
3038 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3039 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3041 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3043 WARN("Ignoring invalid stage %u.\n", stage);
3044 return NULL; /* Windows accepts overflowing this array ... we do not. */
3047 return device->state.textures[stage];
3050 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3051 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3053 struct wined3d_swapchain *swapchain;
3055 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3056 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3058 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3059 return WINED3DERR_INVALIDCALL;
3061 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3062 return WINED3DERR_INVALIDCALL;
3063 return WINED3D_OK;
3066 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3068 TRACE("device %p, caps %p.\n", device, caps);
3070 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3071 device->create_parms.device_type, caps);
3074 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3075 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3077 struct wined3d_swapchain *swapchain;
3079 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3080 device, swapchain_idx, mode, rotation);
3082 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3083 return WINED3DERR_INVALIDCALL;
3085 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3088 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3090 struct wined3d_stateblock *stateblock;
3091 HRESULT hr;
3093 TRACE("device %p.\n", device);
3095 if (device->recording)
3096 return WINED3DERR_INVALIDCALL;
3098 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3099 if (FAILED(hr))
3100 return hr;
3102 device->recording = stateblock;
3103 device->update_state = &stateblock->state;
3105 TRACE("Recording stateblock %p.\n", stateblock);
3107 return WINED3D_OK;
3110 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3111 struct wined3d_stateblock **stateblock)
3113 struct wined3d_stateblock *object = device->recording;
3115 TRACE("device %p, stateblock %p.\n", device, stateblock);
3117 if (!device->recording)
3119 WARN("Not recording.\n");
3120 *stateblock = NULL;
3121 return WINED3DERR_INVALIDCALL;
3124 stateblock_init_contained_states(object);
3126 *stateblock = object;
3127 device->recording = NULL;
3128 device->update_state = &device->state;
3130 TRACE("Returning stateblock %p.\n", *stateblock);
3132 return WINED3D_OK;
3135 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3137 /* At the moment we have no need for any functionality at the beginning
3138 * of a scene. */
3139 TRACE("device %p.\n", device);
3141 if (device->inScene)
3143 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3144 return WINED3DERR_INVALIDCALL;
3146 device->inScene = TRUE;
3147 return WINED3D_OK;
3150 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3152 struct wined3d_context *context;
3154 TRACE("device %p.\n", device);
3156 if (!device->inScene)
3158 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3159 return WINED3DERR_INVALIDCALL;
3162 context = context_acquire(device, NULL);
3163 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3164 context->gl_info->gl_ops.gl.p_glFlush();
3165 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3166 * fails. */
3167 context_release(context);
3169 device->inScene = FALSE;
3170 return WINED3D_OK;
3173 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3174 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3176 UINT i;
3178 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3179 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3180 dst_window_override, dirty_region, flags);
3182 for (i = 0; i < device->swapchain_count; ++i)
3184 wined3d_swapchain_present(device->swapchains[i], src_rect,
3185 dst_rect, dst_window_override, dirty_region, flags);
3188 return WINED3D_OK;
3191 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3192 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3194 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3195 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3197 if (!rect_count && rects)
3199 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3200 return WINED3D_OK;
3203 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3205 struct wined3d_surface *ds = device->fb.depth_stencil;
3206 if (!ds)
3208 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3209 /* TODO: What about depth stencil buffers without stencil bits? */
3210 return WINED3DERR_INVALIDCALL;
3212 else if (flags & WINED3DCLEAR_TARGET)
3214 if (ds->resource.width < device->fb.render_targets[0]->resource.width
3215 || ds->resource.height < device->fb.render_targets[0]->resource.height)
3217 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3218 return WINED3D_OK;
3223 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3225 return WINED3D_OK;
3228 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3229 enum wined3d_primitive_type primitive_type)
3231 GLenum gl_primitive_type, prev;
3233 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3235 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3236 prev = device->update_state->gl_primitive_type;
3237 device->update_state->gl_primitive_type = gl_primitive_type;
3238 if (device->recording)
3239 device->recording->changed.primitive_type = TRUE;
3240 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3241 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
3244 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3245 enum wined3d_primitive_type *primitive_type)
3247 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3249 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3251 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3254 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3256 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3258 if (!device->state.vertex_declaration)
3260 WARN("Called without a valid vertex declaration set.\n");
3261 return WINED3DERR_INVALIDCALL;
3264 if (device->state.load_base_vertex_index)
3266 device->state.load_base_vertex_index = 0;
3267 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3270 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3272 return WINED3D_OK;
3275 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3277 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3279 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3281 if (!device->state.index_buffer)
3283 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3284 * without an index buffer set. (The first time at least...)
3285 * D3D8 simply dies, but I doubt it can do much harm to return
3286 * D3DERR_INVALIDCALL there as well. */
3287 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3288 return WINED3DERR_INVALIDCALL;
3291 if (!device->state.vertex_declaration)
3293 WARN("Called without a valid vertex declaration set.\n");
3294 return WINED3DERR_INVALIDCALL;
3297 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3298 device->state.load_base_vertex_index != device->state.base_vertex_index)
3300 device->state.load_base_vertex_index = device->state.base_vertex_index;
3301 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3304 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3306 return WINED3D_OK;
3309 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3310 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3312 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3314 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3317 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3318 static HRESULT device_update_volume(struct wined3d_device *device,
3319 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3321 struct wined3d_map_desc src;
3322 HRESULT hr;
3323 struct wined3d_bo_address data;
3324 struct wined3d_context *context;
3326 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3327 device, src_volume, dst_volume);
3329 if (src_volume->resource.format != dst_volume->resource.format)
3331 FIXME("Source and destination formats do not match.\n");
3332 return WINED3DERR_INVALIDCALL;
3334 if (src_volume->resource.width != dst_volume->resource.width
3335 || src_volume->resource.height != dst_volume->resource.height
3336 || src_volume->resource.depth != dst_volume->resource.depth)
3338 FIXME("Source and destination sizes do not match.\n");
3339 return WINED3DERR_INVALIDCALL;
3342 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3343 return hr;
3345 context = context_acquire(device, NULL);
3347 wined3d_volume_load(dst_volume, context, FALSE);
3349 data.buffer_object = 0;
3350 data.addr = src.data;
3351 wined3d_volume_upload_data(dst_volume, context, &data);
3352 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3354 context_release(context);
3356 hr = wined3d_volume_unmap(src_volume);
3358 return hr;
3361 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3362 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3364 enum wined3d_resource_type type;
3365 unsigned int level_count, i;
3366 HRESULT hr;
3367 struct wined3d_context *context;
3369 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3371 /* Verify that the source and destination textures are non-NULL. */
3372 if (!src_texture || !dst_texture)
3374 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3375 return WINED3DERR_INVALIDCALL;
3378 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3380 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3381 return WINED3DERR_INVALIDCALL;
3383 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3385 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3386 return WINED3DERR_INVALIDCALL;
3389 /* Verify that the source and destination textures are the same type. */
3390 type = src_texture->resource.type;
3391 if (dst_texture->resource.type != type)
3393 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3394 return WINED3DERR_INVALIDCALL;
3397 /* Check that both textures have the identical numbers of levels. */
3398 level_count = wined3d_texture_get_level_count(src_texture);
3399 if (wined3d_texture_get_level_count(dst_texture) != level_count)
3401 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
3402 return WINED3DERR_INVALIDCALL;
3405 /* Make sure that the destination texture is loaded. */
3406 context = context_acquire(device, NULL);
3407 wined3d_texture_load(dst_texture, context, FALSE);
3408 context_release(context);
3410 /* Update every surface level of the texture. */
3411 switch (type)
3413 case WINED3D_RTYPE_TEXTURE:
3415 struct wined3d_surface *src_surface;
3416 struct wined3d_surface *dst_surface;
3418 for (i = 0; i < level_count; ++i)
3420 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3421 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3422 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3423 if (FAILED(hr))
3425 WARN("Failed to update surface, hr %#x.\n", hr);
3426 return hr;
3429 break;
3432 case WINED3D_RTYPE_CUBE_TEXTURE:
3434 struct wined3d_surface *src_surface;
3435 struct wined3d_surface *dst_surface;
3437 for (i = 0; i < level_count * 6; ++i)
3439 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3440 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3441 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3442 if (FAILED(hr))
3444 WARN("Failed to update surface, hr %#x.\n", hr);
3445 return hr;
3448 break;
3451 case WINED3D_RTYPE_VOLUME_TEXTURE:
3453 for (i = 0; i < level_count; ++i)
3455 hr = device_update_volume(device,
3456 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
3457 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3458 if (FAILED(hr))
3460 WARN("Failed to update volume, hr %#x.\n", hr);
3461 return hr;
3464 break;
3467 default:
3468 FIXME("Unsupported texture type %#x.\n", type);
3469 return WINED3DERR_INVALIDCALL;
3472 return WINED3D_OK;
3475 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3476 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3478 struct wined3d_swapchain *swapchain;
3480 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3482 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3483 return WINED3DERR_INVALIDCALL;
3485 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3488 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3490 const struct wined3d_state *state = &device->state;
3491 struct wined3d_texture *texture;
3492 DWORD i;
3494 TRACE("device %p, num_passes %p.\n", device, num_passes);
3496 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3498 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3500 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3501 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3503 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3505 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3506 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3509 texture = state->textures[i];
3510 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
3512 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3514 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3515 return E_FAIL;
3517 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3519 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3520 return E_FAIL;
3522 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3523 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3525 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3526 return E_FAIL;
3530 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3531 || state->render_states[WINED3D_RS_STENCILENABLE])
3533 struct wined3d_surface *ds = device->fb.depth_stencil;
3534 struct wined3d_surface *target = device->fb.render_targets[0];
3536 if(ds && target
3537 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
3539 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3540 return WINED3DERR_CONFLICTINGRENDERSTATE;
3544 /* return a sensible default */
3545 *num_passes = 1;
3547 TRACE("returning D3D_OK\n");
3548 return WINED3D_OK;
3551 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3553 static BOOL warned;
3555 TRACE("device %p, software %#x.\n", device, software);
3557 if (!warned)
3559 FIXME("device %p, software %#x stub!\n", device, software);
3560 warned = TRUE;
3563 device->softwareVertexProcessing = software;
3566 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3568 static BOOL warned;
3570 TRACE("device %p.\n", device);
3572 if (!warned)
3574 TRACE("device %p stub!\n", device);
3575 warned = TRUE;
3578 return device->softwareVertexProcessing;
3581 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3582 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3584 struct wined3d_swapchain *swapchain;
3586 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3587 device, swapchain_idx, raster_status);
3589 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3590 return WINED3DERR_INVALIDCALL;
3592 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3595 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3597 static BOOL warned;
3599 TRACE("device %p, segments %.8e.\n", device, segments);
3601 if (segments != 0.0f)
3603 if (!warned)
3605 FIXME("device %p, segments %.8e stub!\n", device, segments);
3606 warned = TRUE;
3610 return WINED3D_OK;
3613 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3615 static BOOL warned;
3617 TRACE("device %p.\n", device);
3619 if (!warned)
3621 FIXME("device %p stub!\n", device);
3622 warned = TRUE;
3625 return 0.0f;
3628 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3629 struct wined3d_surface *src_surface, const RECT *src_rect,
3630 struct wined3d_surface *dst_surface, const POINT *dst_point)
3632 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3633 device, src_surface, wine_dbgstr_rect(src_rect),
3634 dst_surface, wine_dbgstr_point(dst_point));
3636 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3638 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3639 src_surface, dst_surface);
3640 return WINED3DERR_INVALIDCALL;
3643 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3646 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
3647 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
3649 RECT r;
3651 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
3652 device, surface, wine_dbgstr_rect(rect),
3653 color->r, color->g, color->b, color->a);
3655 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3657 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
3658 return WINED3DERR_INVALIDCALL;
3661 if (!rect)
3663 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
3664 rect = &r;
3667 return surface_color_fill(surface, rect, color);
3670 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3671 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3673 struct wined3d_surface *dst_surface, *src_surface;
3674 struct wined3d_texture *dst_texture, *src_texture;
3675 unsigned int i, count;
3676 HRESULT hr;
3678 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3680 if (src_resource == dst_resource)
3682 WARN("Source and destination are the same resource.\n");
3683 return;
3686 if (src_resource->type != dst_resource->type)
3688 WARN("Resource types (%s / %s) don't match.\n",
3689 debug_d3dresourcetype(dst_resource->type),
3690 debug_d3dresourcetype(src_resource->type));
3691 return;
3694 if (src_resource->width != dst_resource->width
3695 || src_resource->height != dst_resource->height
3696 || src_resource->depth != dst_resource->depth)
3698 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3699 dst_resource->width, dst_resource->height, dst_resource->depth,
3700 src_resource->width, src_resource->height, src_resource->depth);
3701 return;
3704 if (src_resource->format->id != dst_resource->format->id)
3706 WARN("Resource formats (%s / %s) don't match.\n",
3707 debug_d3dformat(dst_resource->format->id),
3708 debug_d3dformat(src_resource->format->id));
3709 return;
3712 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3714 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3715 return;
3718 dst_texture = wined3d_texture_from_resource(dst_resource);
3719 src_texture = wined3d_texture_from_resource(src_resource);
3721 if (src_texture->layer_count != dst_texture->layer_count
3722 || src_texture->level_count != dst_texture->level_count)
3724 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3725 dst_texture->layer_count, dst_texture->level_count,
3726 src_texture->layer_count, src_texture->level_count);
3727 return;
3730 count = dst_texture->layer_count * dst_texture->level_count;
3731 for (i = 0; i < count; ++i)
3733 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3734 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3736 if (FAILED(hr = wined3d_surface_blt(dst_surface, NULL, src_surface, NULL, 0, NULL, WINED3D_TEXF_POINT)))
3737 ERR("Failed to blit, subresource %u, hr %#x.\n", i, hr);
3741 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
3742 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
3744 struct wined3d_resource *resource;
3745 HRESULT hr;
3746 RECT rect;
3748 resource = rendertarget_view->resource;
3749 if (resource->type != WINED3D_RTYPE_SURFACE)
3751 FIXME("Only supported on surface resources\n");
3752 return;
3755 SetRect(&rect, 0, 0, resource->width, resource->height);
3756 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
3757 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
3760 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
3761 UINT render_target_idx)
3763 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
3765 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
3767 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3768 return NULL;
3771 return device->fb.render_targets[render_target_idx];
3774 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
3776 TRACE("device %p.\n", device);
3778 return device->fb.depth_stencil;
3781 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
3782 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
3784 struct wined3d_surface *prev;
3786 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
3787 device, render_target_idx, render_target, set_viewport);
3789 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
3791 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3792 return WINED3DERR_INVALIDCALL;
3795 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
3797 WARN("Surface %p doesn't have render target usage.\n", render_target);
3798 return WINED3DERR_INVALIDCALL;
3801 /* Set the viewport and scissor rectangles, if requested. Tests show that
3802 * stateblock recording is ignored, the change goes directly into the
3803 * primary stateblock. */
3804 if (!render_target_idx && set_viewport)
3806 struct wined3d_state *state = &device->state;
3808 state->viewport.x = 0;
3809 state->viewport.y = 0;
3810 state->viewport.width = render_target->resource.width;
3811 state->viewport.height = render_target->resource.height;
3812 state->viewport.min_z = 0.0f;
3813 state->viewport.max_z = 1.0f;
3814 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
3816 state->scissor_rect.top = 0;
3817 state->scissor_rect.left = 0;
3818 state->scissor_rect.right = render_target->resource.width;
3819 state->scissor_rect.bottom = render_target->resource.height;
3820 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
3824 prev = device->fb.render_targets[render_target_idx];
3825 if (render_target == prev)
3826 return WINED3D_OK;
3828 if (render_target)
3829 wined3d_surface_incref(render_target);
3830 device->fb.render_targets[render_target_idx] = render_target;
3831 wined3d_cs_emit_set_render_target(device->cs, render_target_idx, render_target);
3832 /* Release after the assignment, to prevent device_resource_released()
3833 * from seeing the surface as still in use. */
3834 if (prev)
3835 wined3d_surface_decref(prev);
3837 return WINED3D_OK;
3840 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
3842 struct wined3d_surface *prev = device->fb.depth_stencil;
3844 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
3845 device, depth_stencil, prev);
3847 if (prev == depth_stencil)
3849 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
3850 return;
3853 device->fb.depth_stencil = depth_stencil;
3854 if (depth_stencil)
3855 wined3d_surface_incref(depth_stencil);
3856 wined3d_cs_emit_set_depth_stencil(device->cs, depth_stencil);
3857 if (prev)
3858 wined3d_surface_decref(prev);
3861 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
3862 struct wined3d_surface *cursor_image)
3864 struct wined3d_resource_desc desc;
3865 struct wined3d_map_desc map_desc;
3866 struct wined3d_texture *texture;
3867 struct wined3d_surface *surface;
3868 BYTE *src_data, *dst_data;
3869 unsigned int src_pitch;
3870 unsigned int i;
3872 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
3874 ERR("Failed to map source surface.\n");
3875 return NULL;
3878 src_pitch = map_desc.row_pitch;
3879 src_data = map_desc.data;
3881 desc.resource_type = WINED3D_RTYPE_TEXTURE;
3882 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
3883 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
3884 desc.multisample_quality = 0;
3885 desc.usage = WINED3DUSAGE_DYNAMIC;
3886 desc.pool = WINED3D_POOL_DEFAULT;
3887 desc.width = cursor_image->resource.width;
3888 desc.height = cursor_image->resource.height;
3889 desc.depth = 1;
3890 desc.size = 0;
3892 if (FAILED(wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
3893 NULL, &wined3d_null_parent_ops, &texture)))
3895 ERR("Failed to create cursor texture.\n");
3896 wined3d_surface_unmap(cursor_image);
3897 return NULL;
3900 surface = surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3901 if (FAILED(wined3d_surface_map(surface, &map_desc, NULL, WINED3D_MAP_DISCARD)))
3903 ERR("Failed to map destination surface.\n");
3904 wined3d_texture_decref(texture);
3905 wined3d_surface_unmap(cursor_image);
3906 return NULL;
3909 dst_data = map_desc.data;
3911 for (i = 0; i < desc.height; ++i)
3912 memcpy(&dst_data[map_desc.row_pitch * i], &src_data[src_pitch * i], desc.width * 4);
3914 wined3d_surface_unmap(surface);
3915 wined3d_surface_unmap(cursor_image);
3917 return texture;
3920 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
3921 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
3923 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
3924 device, x_hotspot, y_hotspot, cursor_image);
3926 if (device->cursor_texture)
3928 wined3d_texture_decref(device->cursor_texture);
3929 device->cursor_texture = NULL;
3932 if (cursor_image)
3934 struct wined3d_display_mode mode;
3935 struct wined3d_map_desc map_desc;
3936 HRESULT hr;
3938 /* MSDN: Cursor must be A8R8G8B8 */
3939 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
3941 WARN("surface %p has an invalid format.\n", cursor_image);
3942 return WINED3DERR_INVALIDCALL;
3945 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
3947 ERR("Failed to get display mode, hr %#x.\n", hr);
3948 return WINED3DERR_INVALIDCALL;
3951 /* MSDN: Cursor must be smaller than the display mode */
3952 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
3954 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
3955 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
3956 mode.width, mode.height);
3957 return WINED3DERR_INVALIDCALL;
3960 /* TODO: MSDN: Cursor sizes must be a power of 2 */
3962 /* Do not store the surface's pointer because the application may
3963 * release it after setting the cursor image. Windows doesn't
3964 * addref the set surface, so we can't do this either without
3965 * creating circular refcount dependencies. */
3966 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
3968 ERR("Failed to create cursor texture.\n");
3969 return WINED3DERR_INVALIDCALL;
3972 device->cursorWidth = cursor_image->resource.width;
3973 device->cursorHeight = cursor_image->resource.height;
3975 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
3977 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
3978 ICONINFO cursorInfo;
3979 DWORD *maskBits;
3980 HCURSOR cursor;
3982 /* 32-bit user32 cursors ignore the alpha channel if it's all
3983 * zeroes, and use the mask instead. Fill the mask with all ones
3984 * to ensure we still get a fully transparent cursor. */
3985 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
3986 memset(maskBits, 0xff, mask_size);
3987 wined3d_surface_map(cursor_image, &map_desc, NULL,
3988 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
3989 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
3991 cursorInfo.fIcon = FALSE;
3992 cursorInfo.xHotspot = x_hotspot;
3993 cursorInfo.yHotspot = y_hotspot;
3994 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
3995 1, 1, maskBits);
3996 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
3997 1, 32, map_desc.data);
3998 wined3d_surface_unmap(cursor_image);
3999 /* Create our cursor and clean up. */
4000 cursor = CreateIconIndirect(&cursorInfo);
4001 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4002 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4003 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4004 device->hardwareCursor = cursor;
4005 if (device->bCursorVisible) SetCursor( cursor );
4006 HeapFree(GetProcessHeap(), 0, maskBits);
4010 device->xHotSpot = x_hotspot;
4011 device->yHotSpot = y_hotspot;
4012 return WINED3D_OK;
4015 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4016 int x_screen_space, int y_screen_space, DWORD flags)
4018 TRACE("device %p, x %d, y %d, flags %#x.\n",
4019 device, x_screen_space, y_screen_space, flags);
4021 device->xScreenSpace = x_screen_space;
4022 device->yScreenSpace = y_screen_space;
4024 if (device->hardwareCursor)
4026 POINT pt;
4028 GetCursorPos( &pt );
4029 if (x_screen_space == pt.x && y_screen_space == pt.y)
4030 return;
4031 SetCursorPos( x_screen_space, y_screen_space );
4033 /* Switch to the software cursor if position diverges from the hardware one. */
4034 GetCursorPos( &pt );
4035 if (x_screen_space != pt.x || y_screen_space != pt.y)
4037 if (device->bCursorVisible) SetCursor( NULL );
4038 DestroyCursor( device->hardwareCursor );
4039 device->hardwareCursor = 0;
4044 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4046 BOOL oldVisible = device->bCursorVisible;
4048 TRACE("device %p, show %#x.\n", device, show);
4051 * When ShowCursor is first called it should make the cursor appear at the OS's last
4052 * known cursor position.
4054 if (show && !oldVisible)
4056 POINT pt;
4057 GetCursorPos(&pt);
4058 device->xScreenSpace = pt.x;
4059 device->yScreenSpace = pt.y;
4062 if (device->hardwareCursor)
4064 device->bCursorVisible = show;
4065 if (show)
4066 SetCursor(device->hardwareCursor);
4067 else
4068 SetCursor(NULL);
4070 else if (device->cursor_texture)
4072 device->bCursorVisible = show;
4075 return oldVisible;
4078 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4080 struct wined3d_resource *resource, *cursor;
4082 TRACE("device %p.\n", device);
4084 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4086 TRACE("Checking resource %p for eviction.\n", resource);
4088 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4090 TRACE("Evicting %p.\n", resource);
4091 resource->resource_ops->resource_unload(resource);
4095 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4096 device_invalidate_state(device, STATE_STREAMSRC);
4099 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4101 struct wined3d_resource *resource, *cursor;
4102 const struct wined3d_gl_info *gl_info;
4103 struct wined3d_context *context;
4104 struct wined3d_shader *shader;
4106 context = context_acquire(device, NULL);
4107 gl_info = context->gl_info;
4109 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4111 TRACE("Unloading resource %p.\n", resource);
4113 resource->resource_ops->resource_unload(resource);
4116 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4118 device->shader_backend->shader_destroy(shader);
4121 if (device->depth_blt_texture)
4123 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4124 device->depth_blt_texture = 0;
4127 device->blitter->free_private(device);
4128 device->shader_backend->shader_free_private(device);
4129 destroy_dummy_textures(device, gl_info);
4131 context_release(context);
4133 while (device->context_count)
4135 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4138 HeapFree(GetProcessHeap(), 0, swapchain->context);
4139 swapchain->context = NULL;
4142 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4144 struct wined3d_context *context;
4145 struct wined3d_surface *target;
4146 HRESULT hr;
4148 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4149 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4151 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4152 return hr;
4155 if (FAILED(hr = device->blitter->alloc_private(device)))
4157 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4158 device->shader_backend->shader_free_private(device);
4159 return hr;
4162 /* Recreate the primary swapchain's context */
4163 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4164 if (!swapchain->context)
4166 ERR("Failed to allocate memory for swapchain context array.\n");
4167 device->blitter->free_private(device);
4168 device->shader_backend->shader_free_private(device);
4169 return E_OUTOFMEMORY;
4172 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4173 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4175 WARN("Failed to create context.\n");
4176 device->blitter->free_private(device);
4177 device->shader_backend->shader_free_private(device);
4178 HeapFree(GetProcessHeap(), 0, swapchain->context);
4179 return E_FAIL;
4182 swapchain->context[0] = context;
4183 swapchain->num_contexts = 1;
4184 create_dummy_textures(device, context);
4185 context_release(context);
4187 return WINED3D_OK;
4190 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4191 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4192 wined3d_device_reset_cb callback, BOOL reset_state)
4194 struct wined3d_resource *resource, *cursor;
4195 struct wined3d_swapchain *swapchain;
4196 struct wined3d_display_mode m;
4197 BOOL DisplayModeChanged = FALSE;
4198 BOOL update_desc = FALSE;
4199 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4200 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4201 HRESULT hr = WINED3D_OK;
4202 unsigned int i;
4204 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4206 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4208 ERR("Failed to get the first implicit swapchain.\n");
4209 return WINED3DERR_INVALIDCALL;
4212 if (reset_state)
4214 if (device->logo_texture)
4216 wined3d_texture_decref(device->logo_texture);
4217 device->logo_texture = NULL;
4219 if (device->cursor_texture)
4221 wined3d_texture_decref(device->cursor_texture);
4222 device->cursor_texture = NULL;
4224 state_unbind_resources(&device->state);
4227 if (device->fb.render_targets)
4229 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4231 wined3d_device_set_render_target(device, i, NULL, FALSE);
4233 if (swapchain->back_buffers && swapchain->back_buffers[0])
4234 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
4236 wined3d_device_set_depth_stencil(device, NULL);
4238 if (device->onscreen_depth_stencil)
4240 wined3d_surface_decref(device->onscreen_depth_stencil);
4241 device->onscreen_depth_stencil = NULL;
4244 if (reset_state)
4246 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4248 TRACE("Enumerating resource %p.\n", resource);
4249 if (FAILED(hr = callback(resource)))
4250 return hr;
4254 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4255 * on an existing gl context, so there's no real need for recreation.
4257 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4259 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4261 TRACE("New params:\n");
4262 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4263 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4264 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4265 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4266 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4267 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4268 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4269 TRACE("device_window %p\n", swapchain_desc->device_window);
4270 TRACE("windowed %#x\n", swapchain_desc->windowed);
4271 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4272 if (swapchain_desc->enable_auto_depth_stencil)
4273 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4274 TRACE("flags %#x\n", swapchain_desc->flags);
4275 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4276 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4277 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4279 /* No special treatment of these parameters. Just store them */
4280 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4281 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4282 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4283 swapchain->desc.flags = swapchain_desc->flags;
4284 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4285 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4286 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4288 /* What to do about these? */
4289 if (swapchain_desc->backbuffer_count
4290 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4291 FIXME("Cannot change the back buffer count yet.\n");
4293 if (swapchain_desc->device_window
4294 && swapchain_desc->device_window != swapchain->desc.device_window)
4296 TRACE("Changing the device window from %p to %p.\n",
4297 swapchain->desc.device_window, swapchain_desc->device_window);
4298 swapchain->desc.device_window = swapchain_desc->device_window;
4299 swapchain->device_window = swapchain_desc->device_window;
4300 wined3d_swapchain_set_window(swapchain, NULL);
4303 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
4305 struct wined3d_resource_desc surface_desc;
4307 TRACE("Creating the depth stencil buffer\n");
4309 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
4310 surface_desc.format = swapchain_desc->auto_depth_stencil_format;
4311 surface_desc.multisample_type = swapchain_desc->multisample_type;
4312 surface_desc.multisample_quality = swapchain_desc->multisample_quality;
4313 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4314 surface_desc.pool = WINED3D_POOL_DEFAULT;
4315 surface_desc.width = swapchain_desc->backbuffer_width;
4316 surface_desc.height = swapchain_desc->backbuffer_height;
4317 surface_desc.depth = 1;
4318 surface_desc.size = 0;
4320 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4321 device->device_parent, &surface_desc, &device->auto_depth_stencil)))
4323 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
4324 return WINED3DERR_INVALIDCALL;
4328 /* Reset the depth stencil */
4329 if (swapchain_desc->enable_auto_depth_stencil)
4330 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
4332 if (mode)
4334 DisplayModeChanged = TRUE;
4335 m = *mode;
4337 else if (swapchain_desc->windowed)
4339 m = swapchain->original_mode;
4341 else
4343 m.width = swapchain_desc->backbuffer_width;
4344 m.height = swapchain_desc->backbuffer_height;
4345 m.refresh_rate = swapchain_desc->refresh_rate;
4346 m.format_id = swapchain_desc->backbuffer_format;
4347 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4350 if (!backbuffer_width || !backbuffer_height)
4352 /* The application is requesting that either the swapchain width or
4353 * height be set to the corresponding dimension in the window's
4354 * client rect. */
4356 RECT client_rect;
4358 if (!swapchain_desc->windowed)
4359 return WINED3DERR_INVALIDCALL;
4361 if (!GetClientRect(swapchain->device_window, &client_rect))
4363 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
4364 return WINED3DERR_INVALIDCALL;
4367 if (!backbuffer_width)
4368 backbuffer_width = client_rect.right;
4370 if (!backbuffer_height)
4371 backbuffer_height = client_rect.bottom;
4374 if (backbuffer_width != swapchain->desc.backbuffer_width
4375 || backbuffer_height != swapchain->desc.backbuffer_height)
4377 if (!swapchain_desc->windowed)
4378 DisplayModeChanged = TRUE;
4380 swapchain->desc.backbuffer_width = backbuffer_width;
4381 swapchain->desc.backbuffer_height = backbuffer_height;
4382 update_desc = TRUE;
4385 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
4386 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
4388 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
4389 update_desc = TRUE;
4392 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
4393 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
4395 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
4396 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
4397 update_desc = TRUE;
4400 if (update_desc)
4402 UINT i;
4404 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
4405 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4406 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4407 return hr;
4409 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4411 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
4412 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4413 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4414 return hr;
4416 if (device->auto_depth_stencil)
4418 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
4419 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
4420 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4421 return hr;
4425 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4426 || DisplayModeChanged)
4428 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4430 WARN("Failed to set display mode, hr %#x.\n", hr);
4431 return WINED3DERR_INVALIDCALL;
4434 if (!swapchain_desc->windowed)
4436 if (swapchain->desc.windowed)
4438 HWND focus_window = device->create_parms.focus_window;
4439 if (!focus_window)
4440 focus_window = swapchain_desc->device_window;
4441 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4443 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4444 return hr;
4447 /* switch from windowed to fs */
4448 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4449 swapchain_desc->backbuffer_width,
4450 swapchain_desc->backbuffer_height);
4452 else
4454 /* Fullscreen -> fullscreen mode change */
4455 MoveWindow(swapchain->device_window, 0, 0,
4456 swapchain_desc->backbuffer_width,
4457 swapchain_desc->backbuffer_height,
4458 TRUE);
4461 else if (!swapchain->desc.windowed)
4463 /* Fullscreen -> windowed switch */
4464 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4465 wined3d_device_release_focus_window(device);
4467 swapchain->desc.windowed = swapchain_desc->windowed;
4469 else if (!swapchain_desc->windowed)
4471 DWORD style = device->style;
4472 DWORD exStyle = device->exStyle;
4473 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4474 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4475 * Reset to clear up their mess. Guild Wars also loses the device during that.
4477 device->style = 0;
4478 device->exStyle = 0;
4479 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4480 swapchain_desc->backbuffer_width,
4481 swapchain_desc->backbuffer_height);
4482 device->style = style;
4483 device->exStyle = exStyle;
4486 if (reset_state)
4488 TRACE("Resetting stateblock.\n");
4489 if (device->recording)
4491 wined3d_stateblock_decref(device->recording);
4492 device->recording = NULL;
4494 wined3d_cs_emit_reset_state(device->cs);
4495 state_cleanup(&device->state);
4497 if (device->d3d_initialized)
4498 delete_opengl_contexts(device, swapchain);
4500 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4501 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4502 ERR("Failed to initialize device state, hr %#x.\n", hr);
4503 device->update_state = &device->state;
4505 device_init_swapchain_state(device, swapchain);
4507 else
4509 struct wined3d_surface *rt = device->fb.render_targets[0];
4510 struct wined3d_state *state = &device->state;
4512 /* Note the min_z / max_z is not reset. */
4513 state->viewport.x = 0;
4514 state->viewport.y = 0;
4515 state->viewport.width = rt->resource.width;
4516 state->viewport.height = rt->resource.height;
4517 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4519 state->scissor_rect.top = 0;
4520 state->scissor_rect.left = 0;
4521 state->scissor_rect.right = rt->resource.width;
4522 state->scissor_rect.bottom = rt->resource.height;
4523 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4526 swapchain_update_render_to_fbo(swapchain);
4527 swapchain_update_draw_bindings(swapchain);
4529 if (reset_state && device->d3d_initialized)
4530 hr = create_primary_opengl_context(device, swapchain);
4532 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4533 * first use
4535 return hr;
4538 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4540 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4542 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4544 return WINED3D_OK;
4548 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4549 struct wined3d_device_creation_parameters *parameters)
4551 TRACE("device %p, parameters %p.\n", device, parameters);
4553 *parameters = device->create_parms;
4556 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4557 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4559 struct wined3d_swapchain *swapchain;
4561 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4562 device, swapchain_idx, flags, ramp);
4564 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4565 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4568 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4569 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4571 struct wined3d_swapchain *swapchain;
4573 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4574 device, swapchain_idx, ramp);
4576 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4577 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4580 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4582 TRACE("device %p, resource %p.\n", device, resource);
4584 list_add_head(&device->resources, &resource->resource_list_entry);
4587 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4589 TRACE("device %p, resource %p.\n", device, resource);
4591 list_remove(&resource->resource_list_entry);
4594 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4596 enum wined3d_resource_type type = resource->type;
4597 unsigned int i;
4599 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4601 context_resource_released(device, resource, type);
4603 switch (type)
4605 case WINED3D_RTYPE_SURFACE:
4607 struct wined3d_surface *surface = surface_from_resource(resource);
4609 if (!device->d3d_initialized) break;
4611 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4613 if (device->fb.render_targets[i] == surface)
4615 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4616 device->fb.render_targets[i] = NULL;
4620 if (device->fb.depth_stencil == surface)
4622 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4623 device->fb.depth_stencil = NULL;
4626 break;
4628 case WINED3D_RTYPE_TEXTURE:
4629 case WINED3D_RTYPE_CUBE_TEXTURE:
4630 case WINED3D_RTYPE_VOLUME_TEXTURE:
4631 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4633 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4635 if (device->state.textures[i] == texture)
4637 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4638 device->state.textures[i] = NULL;
4641 if (device->recording && device->update_state->textures[i] == texture)
4643 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4644 texture, device->recording, i);
4645 device->update_state->textures[i] = NULL;
4648 break;
4650 case WINED3D_RTYPE_BUFFER:
4652 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4654 for (i = 0; i < MAX_STREAMS; ++i)
4656 if (device->state.streams[i].buffer == buffer)
4658 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4659 device->state.streams[i].buffer = NULL;
4662 if (device->recording && device->update_state->streams[i].buffer == buffer)
4664 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4665 buffer, device->recording, i);
4666 device->update_state->streams[i].buffer = NULL;
4670 if (device->state.index_buffer == buffer)
4672 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4673 device->state.index_buffer = NULL;
4676 if (device->recording && device->update_state->index_buffer == buffer)
4678 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4679 buffer, device->recording);
4680 device->update_state->index_buffer = NULL;
4683 break;
4685 default:
4686 break;
4689 /* Remove the resource from the resourceStore */
4690 device_resource_remove(device, resource);
4692 TRACE("Resource released.\n");
4695 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4697 struct wined3d_resource *resource;
4699 TRACE("device %p, dc %p.\n", device, dc);
4701 if (!dc)
4702 return NULL;
4704 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4706 if (resource->type == WINED3D_RTYPE_SURFACE)
4708 struct wined3d_surface *s = surface_from_resource(resource);
4710 if (s->hDC == dc)
4712 TRACE("Found surface %p for dc %p.\n", s, dc);
4713 return s;
4718 return NULL;
4721 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4722 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4723 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4725 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4726 const struct fragment_pipeline *fragment_pipeline;
4727 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4728 unsigned int i;
4729 HRESULT hr;
4731 device->ref = 1;
4732 device->wined3d = wined3d;
4733 wined3d_incref(device->wined3d);
4734 device->adapter = wined3d->adapter_count ? adapter : NULL;
4735 device->device_parent = device_parent;
4736 list_init(&device->resources);
4737 list_init(&device->shaders);
4738 device->surface_alignment = surface_alignment;
4740 /* Save the creation parameters. */
4741 device->create_parms.adapter_idx = adapter_idx;
4742 device->create_parms.device_type = device_type;
4743 device->create_parms.focus_window = focus_window;
4744 device->create_parms.flags = flags;
4746 device->shader_backend = adapter->shader_backend;
4748 vertex_pipeline = adapter->vertex_pipe;
4750 fragment_pipeline = adapter->fragment_pipe;
4752 if (vertex_pipeline->vp_states && fragment_pipeline->states
4753 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4754 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4755 fragment_pipeline, misc_state_template)))
4757 ERR("Failed to compile state table, hr %#x.\n", hr);
4758 wined3d_decref(device->wined3d);
4759 return hr;
4762 device->blitter = adapter->blitter;
4764 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
4765 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4767 ERR("Failed to initialize device state, hr %#x.\n", hr);
4768 goto err;
4770 device->update_state = &device->state;
4772 if (!(device->cs = wined3d_cs_create(device)))
4774 WARN("Failed to create command stream.\n");
4775 state_cleanup(&device->state);
4776 hr = E_FAIL;
4777 goto err;
4780 return WINED3D_OK;
4782 err:
4783 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4785 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4787 wined3d_decref(device->wined3d);
4788 return hr;
4792 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4794 DWORD rep = device->StateTable[state].representative;
4795 struct wined3d_context *context;
4796 DWORD idx;
4797 BYTE shift;
4798 UINT i;
4800 for (i = 0; i < device->context_count; ++i)
4802 context = device->contexts[i];
4803 if(isStateDirty(context, rep)) continue;
4805 context->dirtyArray[context->numDirtyEntries++] = rep;
4806 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4807 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4808 context->isStateDirty[idx] |= (1 << shift);
4812 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4813 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4815 if (device->filter_messages)
4817 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4818 window, message, wparam, lparam);
4819 if (unicode)
4820 return DefWindowProcW(window, message, wparam, lparam);
4821 else
4822 return DefWindowProcA(window, message, wparam, lparam);
4825 if (message == WM_DESTROY)
4827 TRACE("unregister window %p.\n", window);
4828 wined3d_unregister_window(window);
4830 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
4831 ERR("Window %p is not the focus window for device %p.\n", window, device);
4833 else if (message == WM_DISPLAYCHANGE)
4835 device->device_parent->ops->mode_changed(device->device_parent);
4837 else if (message == WM_ACTIVATEAPP)
4839 device->device_parent->ops->activate(device->device_parent, wparam);
4842 if (unicode)
4843 return CallWindowProcW(proc, window, message, wparam, lparam);
4844 else
4845 return CallWindowProcA(proc, window, message, wparam, lparam);