wined3d: Use the core version of the gl*Buffer* functions.
[wine/multimedia.git] / dlls / wined3d / device.c
blob271a2166a635b38fa2f52115367b11155066de31
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
56 * actually have the same values in GL and D3D. */
57 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
59 switch(primitive_type)
61 case WINED3D_PT_POINTLIST:
62 return GL_POINTS;
64 case WINED3D_PT_LINELIST:
65 return GL_LINES;
67 case WINED3D_PT_LINESTRIP:
68 return GL_LINE_STRIP;
70 case WINED3D_PT_TRIANGLELIST:
71 return GL_TRIANGLES;
73 case WINED3D_PT_TRIANGLESTRIP:
74 return GL_TRIANGLE_STRIP;
76 case WINED3D_PT_TRIANGLEFAN:
77 return GL_TRIANGLE_FAN;
79 case WINED3D_PT_LINELIST_ADJ:
80 return GL_LINES_ADJACENCY_ARB;
82 case WINED3D_PT_LINESTRIP_ADJ:
83 return GL_LINE_STRIP_ADJACENCY_ARB;
85 case WINED3D_PT_TRIANGLELIST_ADJ:
86 return GL_TRIANGLES_ADJACENCY_ARB;
88 case WINED3D_PT_TRIANGLESTRIP_ADJ:
89 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
91 default:
92 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
93 case WINED3D_PT_UNDEFINED:
94 return ~0u;
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
102 case GL_POINTS:
103 return WINED3D_PT_POINTLIST;
105 case GL_LINES:
106 return WINED3D_PT_LINELIST;
108 case GL_LINE_STRIP:
109 return WINED3D_PT_LINESTRIP;
111 case GL_TRIANGLES:
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
132 default:
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 case ~0u:
135 return WINED3D_PT_UNDEFINED;
139 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
141 struct wined3d_context **new_array;
143 TRACE("Adding context %p.\n", context);
145 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
146 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
147 sizeof(*new_array) * (device->context_count + 1));
149 if (!new_array)
151 ERR("Failed to grow the context array.\n");
152 return FALSE;
155 new_array[device->context_count++] = context;
156 device->contexts = new_array;
157 return TRUE;
160 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
162 struct wined3d_context **new_array;
163 BOOL found = FALSE;
164 UINT i;
166 TRACE("Removing context %p.\n", context);
168 for (i = 0; i < device->context_count; ++i)
170 if (device->contexts[i] == context)
172 found = TRUE;
173 break;
177 if (!found)
179 ERR("Context %p doesn't exist in context array.\n", context);
180 return;
183 if (!--device->context_count)
185 HeapFree(GetProcessHeap(), 0, device->contexts);
186 device->contexts = NULL;
187 return;
190 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
191 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
192 if (!new_array)
194 ERR("Failed to shrink context array. Oh well.\n");
195 return;
198 device->contexts = new_array;
201 void device_switch_onscreen_ds(struct wined3d_device *device,
202 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
204 if (device->onscreen_depth_stencil)
206 surface_load_ds_location(device->onscreen_depth_stencil, context, WINED3D_LOCATION_TEXTURE_RGB);
208 surface_modify_ds_location(device->onscreen_depth_stencil, WINED3D_LOCATION_TEXTURE_RGB,
209 device->onscreen_depth_stencil->ds_current_size.cx,
210 device->onscreen_depth_stencil->ds_current_size.cy);
211 wined3d_surface_decref(device->onscreen_depth_stencil);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_surface_incref(device->onscreen_depth_stencil);
217 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
219 /* partial draw rect */
220 if (draw_rect->left || draw_rect->top
221 || draw_rect->right < target->resource.width
222 || draw_rect->bottom < target->resource.height)
223 return FALSE;
225 /* partial clear rect */
226 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
227 || clear_rect->right < target->resource.width
228 || clear_rect->bottom < target->resource.height))
229 return FALSE;
231 return TRUE;
234 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
235 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
237 RECT current_rect, r;
239 if (ds->locations & WINED3D_LOCATION_DISCARDED)
241 /* Depth buffer was discarded, make it entirely current in its new location since
242 * there is no other place where we would get data anyway. */
243 SetRect(out_rect, 0, 0, ds->resource.width, ds->resource.height);
244 return;
247 if (ds->locations & location)
248 SetRect(&current_rect, 0, 0,
249 ds->ds_current_size.cx,
250 ds->ds_current_size.cy);
251 else
252 SetRectEmpty(&current_rect);
254 IntersectRect(&r, draw_rect, &current_rect);
255 if (EqualRect(&r, draw_rect))
257 /* current_rect ⊇ draw_rect, modify only. */
258 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
259 return;
262 if (EqualRect(&r, &current_rect))
264 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
266 if (!clear_rect)
268 /* Full clear, modify only. */
269 *out_rect = *draw_rect;
270 return;
273 IntersectRect(&r, draw_rect, clear_rect);
274 if (EqualRect(&r, draw_rect))
276 /* clear_rect ⊇ draw_rect, modify only. */
277 *out_rect = *draw_rect;
278 return;
282 /* Full load. */
283 surface_load_ds_location(ds, context, location);
284 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
287 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
288 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
289 float depth, DWORD stencil)
291 struct wined3d_surface *target = rt_count ? wined3d_rendertarget_view_get_surface(fb->render_targets[0]) : NULL;
292 struct wined3d_surface *depth_stencil = fb->depth_stencil
293 ? wined3d_rendertarget_view_get_surface(fb->depth_stencil) : NULL;
294 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
295 const struct wined3d_gl_info *gl_info;
296 UINT drawable_width, drawable_height;
297 struct wined3d_context *context;
298 GLbitfield clear_mask = 0;
299 BOOL render_offscreen;
300 unsigned int i;
301 RECT ds_rect;
303 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
304 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
305 * for the cleared parts, and the untouched parts.
307 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
308 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
309 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
310 * checking all this if the dest surface is in the drawable anyway. */
311 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
313 for (i = 0; i < rt_count; ++i)
315 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
316 if (rt)
317 surface_load_location(rt, rt->container->resource.draw_binding);
321 context = context_acquire(device, target);
322 if (!context->valid)
324 context_release(context);
325 WARN("Invalid context, skipping clear.\n");
326 return;
328 gl_info = context->gl_info;
330 if (target)
332 render_offscreen = context->render_offscreen;
333 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
335 else
337 render_offscreen = TRUE;
338 drawable_width = depth_stencil->pow2Width;
339 drawable_height = depth_stencil->pow2Height;
342 if (flags & WINED3DCLEAR_ZBUFFER)
344 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
346 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
347 device_switch_onscreen_ds(device, context, depth_stencil);
348 prepare_ds_clear(depth_stencil, context, location,
349 draw_rect, rect_count, clear_rect, &ds_rect);
352 if (!context_apply_clear_state(context, device, rt_count, fb))
354 context_release(context);
355 WARN("Failed to apply clear state, skipping clear.\n");
356 return;
359 /* Only set the values up once, as they are not changing. */
360 if (flags & WINED3DCLEAR_STENCIL)
362 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
364 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
365 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
367 gl_info->gl_ops.gl.p_glStencilMask(~0U);
368 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
369 gl_info->gl_ops.gl.p_glClearStencil(stencil);
370 checkGLcall("glClearStencil");
371 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
374 if (flags & WINED3DCLEAR_ZBUFFER)
376 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
378 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
380 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
381 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
382 gl_info->gl_ops.gl.p_glClearDepth(depth);
383 checkGLcall("glClearDepth");
384 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
387 if (flags & WINED3DCLEAR_TARGET)
389 for (i = 0; i < rt_count; ++i)
391 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(fb->render_targets[i]);
393 if (rt)
395 surface_validate_location(rt, rt->container->resource.draw_binding);
396 surface_invalidate_location(rt, ~rt->container->resource.draw_binding);
400 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
401 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
402 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
403 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
404 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
405 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
406 checkGLcall("glClearColor");
407 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
410 if (!clear_rect)
412 if (render_offscreen)
414 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
415 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
417 else
419 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
420 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
422 checkGLcall("glScissor");
423 gl_info->gl_ops.gl.p_glClear(clear_mask);
424 checkGLcall("glClear");
426 else
428 RECT current_rect;
430 /* Now process each rect in turn. */
431 for (i = 0; i < rect_count; ++i)
433 /* Note that GL uses lower left, width/height. */
434 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
436 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
437 wine_dbgstr_rect(&clear_rect[i]),
438 wine_dbgstr_rect(&current_rect));
440 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
441 * The rectangle is not cleared, no error is returned, but further rectangles are
442 * still cleared if they are valid. */
443 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
445 TRACE("Rectangle with negative dimensions, ignoring.\n");
446 continue;
449 if (render_offscreen)
451 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
452 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
454 else
456 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
457 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
459 checkGLcall("glScissor");
461 gl_info->gl_ops.gl.p_glClear(clear_mask);
462 checkGLcall("glClear");
466 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
467 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
468 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
470 context_release(context);
473 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
475 ULONG refcount = InterlockedIncrement(&device->ref);
477 TRACE("%p increasing refcount to %u.\n", device, refcount);
479 return refcount;
482 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
484 ULONG refcount = InterlockedDecrement(&device->ref);
486 TRACE("%p decreasing refcount to %u.\n", device, refcount);
488 if (!refcount)
490 UINT i;
492 wined3d_cs_destroy(device->cs);
494 if (device->recording && wined3d_stateblock_decref(device->recording))
495 FIXME("Something's still holding the recording stateblock.\n");
496 device->recording = NULL;
498 state_cleanup(&device->state);
500 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
502 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
503 device->multistate_funcs[i] = NULL;
506 if (!list_empty(&device->resources))
508 struct wined3d_resource *resource;
510 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
512 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
514 FIXME("Leftover resource %p with type %s (%#x).\n",
515 resource, debug_d3dresourcetype(resource->type), resource->type);
519 if (device->contexts)
520 ERR("Context array not freed!\n");
521 if (device->hardwareCursor)
522 DestroyCursor(device->hardwareCursor);
523 device->hardwareCursor = 0;
525 wined3d_decref(device->wined3d);
526 device->wined3d = NULL;
527 HeapFree(GetProcessHeap(), 0, device);
528 TRACE("Freed device %p.\n", device);
531 return refcount;
534 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
536 TRACE("device %p.\n", device);
538 return device->swapchain_count;
541 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
543 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
545 if (swapchain_idx >= device->swapchain_count)
547 WARN("swapchain_idx %u >= swapchain_count %u.\n",
548 swapchain_idx, device->swapchain_count);
549 return NULL;
552 return device->swapchains[swapchain_idx];
555 static void device_load_logo(struct wined3d_device *device, const char *filename)
557 struct wined3d_color_key color_key;
558 struct wined3d_resource_desc desc;
559 struct wined3d_surface *surface;
560 HBITMAP hbm;
561 BITMAP bm;
562 HRESULT hr;
563 HDC dcb = NULL, dcs = NULL;
565 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
566 if(hbm)
568 GetObjectA(hbm, sizeof(BITMAP), &bm);
569 dcb = CreateCompatibleDC(NULL);
570 if(!dcb) goto out;
571 SelectObject(dcb, hbm);
573 else
575 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
576 * couldn't be loaded
578 memset(&bm, 0, sizeof(bm));
579 bm.bmWidth = 32;
580 bm.bmHeight = 32;
583 desc.resource_type = WINED3D_RTYPE_TEXTURE;
584 desc.format = WINED3DFMT_B5G6R5_UNORM;
585 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
586 desc.multisample_quality = 0;
587 desc.usage = WINED3DUSAGE_DYNAMIC;
588 desc.pool = WINED3D_POOL_DEFAULT;
589 desc.width = bm.bmWidth;
590 desc.height = bm.bmHeight;
591 desc.depth = 1;
592 desc.size = 0;
593 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
594 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
596 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
597 goto out;
599 surface = surface_from_resource(wined3d_texture_get_sub_resource(device->logo_texture, 0));
601 if (dcb)
603 if (FAILED(hr = wined3d_surface_getdc(surface, &dcs)))
604 goto out;
605 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
606 wined3d_surface_releasedc(surface, dcs);
608 color_key.color_space_low_value = 0;
609 color_key.color_space_high_value = 0;
610 wined3d_texture_set_color_key(device->logo_texture, WINEDDCKEY_SRCBLT, &color_key);
612 else
614 const RECT rect = {0, 0, surface->resource.width, surface->resource.height};
615 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
617 /* Fill the surface with a white color to show that wined3d is there */
618 surface_color_fill(surface, &rect, &c);
621 out:
622 if (dcb) DeleteDC(dcb);
623 if (hbm) DeleteObject(hbm);
626 /* Context activation is done by the caller. */
627 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
629 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
630 unsigned int i, j, count;
631 /* Under DirectX you can sample even if no texture is bound, whereas
632 * OpenGL will only allow that when a valid texture is bound.
633 * We emulate this by creating dummy textures and binding them
634 * to each texture stage when the currently set D3D texture is NULL. */
636 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
637 for (i = 0; i < count; ++i)
639 DWORD color = 0x000000ff;
641 /* Make appropriate texture active */
642 context_active_texture(context, gl_info, i);
644 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
645 checkGLcall("glGenTextures");
646 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
648 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
649 checkGLcall("glBindTexture");
651 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
652 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
653 checkGLcall("glTexImage2D");
655 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
657 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
658 checkGLcall("glGenTextures");
659 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
661 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
662 checkGLcall("glBindTexture");
664 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
665 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
666 checkGLcall("glTexImage2D");
669 if (gl_info->supported[EXT_TEXTURE3D])
671 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
672 checkGLcall("glGenTextures");
673 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
675 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
676 checkGLcall("glBindTexture");
678 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
679 checkGLcall("glTexImage3D");
682 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
684 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
685 checkGLcall("glGenTextures");
686 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
688 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
689 checkGLcall("glBindTexture");
691 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
693 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
694 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
695 checkGLcall("glTexImage2D");
701 /* Context activation is done by the caller. */
702 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
704 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
706 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
708 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
709 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
712 if (gl_info->supported[EXT_TEXTURE3D])
714 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
715 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
718 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
720 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
721 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
724 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
725 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
727 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
728 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
729 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
730 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
733 static LONG fullscreen_style(LONG style)
735 /* Make sure the window is managed, otherwise we won't get keyboard input. */
736 style |= WS_POPUP | WS_SYSMENU;
737 style &= ~(WS_CAPTION | WS_THICKFRAME);
739 return style;
742 static LONG fullscreen_exstyle(LONG exstyle)
744 /* Filter out window decorations. */
745 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
747 return exstyle;
750 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
752 BOOL filter_messages;
753 LONG style, exstyle;
755 TRACE("Setting up window %p for fullscreen mode.\n", window);
757 if (device->style || device->exStyle)
759 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
760 window, device->style, device->exStyle);
763 device->style = GetWindowLongW(window, GWL_STYLE);
764 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
766 style = fullscreen_style(device->style);
767 exstyle = fullscreen_exstyle(device->exStyle);
769 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
770 device->style, device->exStyle, style, exstyle);
772 filter_messages = device->filter_messages;
773 device->filter_messages = TRUE;
775 SetWindowLongW(window, GWL_STYLE, style);
776 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
777 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
779 device->filter_messages = filter_messages;
782 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
784 BOOL filter_messages;
785 LONG style, exstyle;
787 if (!device->style && !device->exStyle) return;
789 style = GetWindowLongW(window, GWL_STYLE);
790 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
792 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
793 * application, and we want to ignore them in the test below, since it's
794 * not the application's fault that they changed. Additionally, we want to
795 * preserve the current status of these flags (i.e. don't restore them) to
796 * more closely emulate the behavior of Direct3D, which leaves these flags
797 * alone when returning to windowed mode. */
798 device->style ^= (device->style ^ style) & WS_VISIBLE;
799 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
801 TRACE("Restoring window style of window %p to %08x, %08x.\n",
802 window, device->style, device->exStyle);
804 filter_messages = device->filter_messages;
805 device->filter_messages = TRUE;
807 /* Only restore the style if the application didn't modify it during the
808 * fullscreen phase. Some applications change it before calling Reset()
809 * when switching between windowed and fullscreen modes (HL2), some
810 * depend on the original style (Eve Online). */
811 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
813 SetWindowLongW(window, GWL_STYLE, device->style);
814 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
816 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
818 device->filter_messages = filter_messages;
820 /* Delete the old values. */
821 device->style = 0;
822 device->exStyle = 0;
825 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
827 TRACE("device %p, window %p.\n", device, window);
829 if (!wined3d_register_window(window, device))
831 ERR("Failed to register window %p.\n", window);
832 return E_FAIL;
835 InterlockedExchangePointer((void **)&device->focus_window, window);
836 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
838 return WINED3D_OK;
841 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
843 TRACE("device %p.\n", device);
845 if (device->focus_window) wined3d_unregister_window(device->focus_window);
846 InterlockedExchangePointer((void **)&device->focus_window, NULL);
849 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
851 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
852 unsigned int i;
854 if (device->fb.render_targets)
856 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
858 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
860 if (device->back_buffer_view)
861 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
864 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
865 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
868 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
869 struct wined3d_swapchain_desc *swapchain_desc)
871 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
872 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
873 struct wined3d_swapchain *swapchain = NULL;
874 struct wined3d_context *context;
875 DWORD clear_flags = 0;
876 HRESULT hr;
878 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
880 if (device->d3d_initialized)
881 return WINED3DERR_INVALIDCALL;
882 if (device->wined3d->flags & WINED3D_NO3D)
883 return WINED3DERR_INVALIDCALL;
885 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
886 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
888 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
889 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
891 TRACE("Shader private data couldn't be allocated\n");
892 goto err_out;
894 if (FAILED(hr = device->blitter->alloc_private(device)))
896 TRACE("Blitter private data couldn't be allocated\n");
897 goto err_out;
900 /* Setup the implicit swapchain. This also initializes a context. */
901 TRACE("Creating implicit swapchain\n");
902 hr = device->device_parent->ops->create_swapchain(device->device_parent,
903 swapchain_desc, &swapchain);
904 if (FAILED(hr))
906 WARN("Failed to create implicit swapchain\n");
907 goto err_out;
910 if (swapchain_desc->backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
911 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
912 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
914 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
915 goto err_out;
918 device->swapchain_count = 1;
919 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
920 if (!device->swapchains)
922 ERR("Out of memory!\n");
923 goto err_out;
925 device->swapchains[0] = swapchain;
926 device_init_swapchain_state(device, swapchain);
928 context = context_acquire(device,
929 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)));
931 create_dummy_textures(device, context);
933 device->contexts[0]->last_was_rhw = 0;
935 switch (wined3d_settings.offscreen_rendering_mode)
937 case ORM_FBO:
938 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
939 break;
941 case ORM_BACKBUFFER:
943 if (context_get_current()->aux_buffers > 0)
945 TRACE("Using auxiliary buffer for offscreen rendering\n");
946 device->offscreenBuffer = GL_AUX0;
948 else
950 TRACE("Using back buffer for offscreen rendering\n");
951 device->offscreenBuffer = GL_BACK;
956 TRACE("All defaults now set up, leaving 3D init.\n");
958 context_release(context);
960 /* Clear the screen */
961 if (swapchain->back_buffers && swapchain->back_buffers[0])
962 clear_flags |= WINED3DCLEAR_TARGET;
963 if (swapchain_desc->enable_auto_depth_stencil)
964 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
965 if (clear_flags)
966 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
968 device->d3d_initialized = TRUE;
970 if (wined3d_settings.logo)
971 device_load_logo(device, wined3d_settings.logo);
972 return WINED3D_OK;
974 err_out:
975 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
976 HeapFree(GetProcessHeap(), 0, device->swapchains);
977 device->swapchain_count = 0;
978 if (device->back_buffer_view)
979 wined3d_rendertarget_view_decref(device->back_buffer_view);
980 if (swapchain)
981 wined3d_swapchain_decref(swapchain);
982 if (device->blit_priv)
983 device->blitter->free_private(device);
984 if (device->shader_priv)
985 device->shader_backend->shader_free_private(device);
987 return hr;
990 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
991 struct wined3d_swapchain_desc *swapchain_desc)
993 struct wined3d_swapchain *swapchain = NULL;
994 HRESULT hr;
996 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
998 /* Setup the implicit swapchain */
999 TRACE("Creating implicit swapchain\n");
1000 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1001 swapchain_desc, &swapchain);
1002 if (FAILED(hr))
1004 WARN("Failed to create implicit swapchain\n");
1005 goto err_out;
1008 device->swapchain_count = 1;
1009 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1010 if (!device->swapchains)
1012 ERR("Out of memory!\n");
1013 goto err_out;
1015 device->swapchains[0] = swapchain;
1016 return WINED3D_OK;
1018 err_out:
1019 wined3d_swapchain_decref(swapchain);
1020 return hr;
1023 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1025 struct wined3d_resource *resource, *cursor;
1026 const struct wined3d_gl_info *gl_info;
1027 struct wined3d_context *context;
1028 struct wined3d_surface *surface;
1029 UINT i;
1031 TRACE("device %p.\n", device);
1033 if (!device->d3d_initialized)
1034 return WINED3DERR_INVALIDCALL;
1036 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1037 * it was created. Thus make sure a context is active for the glDelete* calls
1039 context = context_acquire(device, NULL);
1040 gl_info = context->gl_info;
1042 if (device->logo_texture)
1043 wined3d_texture_decref(device->logo_texture);
1044 if (device->cursor_texture)
1045 wined3d_texture_decref(device->cursor_texture);
1047 state_unbind_resources(&device->state);
1049 /* Unload resources */
1050 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1052 TRACE("Unloading resource %p.\n", resource);
1054 resource->resource_ops->resource_unload(resource);
1057 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1058 * private data, it might contain opengl pointers
1060 if (device->depth_blt_texture)
1062 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1063 device->depth_blt_texture = 0;
1066 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1067 device->blitter->free_private(device);
1068 device->shader_backend->shader_free_private(device);
1069 destroy_dummy_textures(device, gl_info);
1071 /* Release the buffers (with sanity checks)*/
1072 if (device->onscreen_depth_stencil)
1074 surface = device->onscreen_depth_stencil;
1075 device->onscreen_depth_stencil = NULL;
1076 wined3d_surface_decref(surface);
1079 if (device->fb.depth_stencil)
1081 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1083 TRACE("Releasing depth/stencil view %p.\n", view);
1085 device->fb.depth_stencil = NULL;
1086 wined3d_rendertarget_view_decref(view);
1089 if (device->auto_depth_stencil_view)
1091 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1093 device->auto_depth_stencil_view = NULL;
1094 if (wined3d_rendertarget_view_decref(view))
1095 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1098 for (i = 0; i < gl_info->limits.buffers; ++i)
1100 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1102 if (device->back_buffer_view)
1104 wined3d_rendertarget_view_decref(device->back_buffer_view);
1105 device->back_buffer_view = NULL;
1108 context_release(context);
1110 for (i = 0; i < device->swapchain_count; ++i)
1112 TRACE("Releasing the implicit swapchain %u.\n", i);
1113 if (wined3d_swapchain_decref(device->swapchains[i]))
1114 FIXME("Something's still holding the implicit swapchain.\n");
1117 HeapFree(GetProcessHeap(), 0, device->swapchains);
1118 device->swapchains = NULL;
1119 device->swapchain_count = 0;
1121 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1122 device->fb.render_targets = NULL;
1124 device->d3d_initialized = FALSE;
1126 return WINED3D_OK;
1129 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1131 unsigned int i;
1133 for (i = 0; i < device->swapchain_count; ++i)
1135 TRACE("Releasing the implicit swapchain %u.\n", i);
1136 if (wined3d_swapchain_decref(device->swapchains[i]))
1137 FIXME("Something's still holding the implicit swapchain.\n");
1140 HeapFree(GetProcessHeap(), 0, device->swapchains);
1141 device->swapchains = NULL;
1142 device->swapchain_count = 0;
1143 return WINED3D_OK;
1146 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1147 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1148 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1150 * There is no way to deactivate thread safety once it is enabled.
1152 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1154 TRACE("device %p.\n", device);
1156 /* For now just store the flag (needed in case of ddraw). */
1157 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1160 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1162 TRACE("device %p.\n", device);
1164 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1165 wine_dbgstr_longlong(device->adapter->vram_bytes),
1166 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1167 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1169 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1172 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1173 struct wined3d_buffer *buffer, UINT offset)
1175 struct wined3d_stream_output *stream;
1176 struct wined3d_buffer *prev_buffer;
1178 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1180 if (idx >= MAX_STREAM_OUT)
1182 WARN("Invalid stream output %u.\n", idx);
1183 return;
1186 stream = &device->update_state->stream_output[idx];
1187 prev_buffer = stream->buffer;
1189 if (buffer)
1190 wined3d_buffer_incref(buffer);
1191 stream->buffer = buffer;
1192 stream->offset = offset;
1193 if (!device->recording)
1194 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1195 if (prev_buffer)
1196 wined3d_buffer_decref(prev_buffer);
1199 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1200 UINT idx, UINT *offset)
1202 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1204 if (idx >= MAX_STREAM_OUT)
1206 WARN("Invalid stream output %u.\n", idx);
1207 return NULL;
1210 *offset = device->state.stream_output[idx].offset;
1211 return device->state.stream_output[idx].buffer;
1214 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1215 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1217 struct wined3d_stream_state *stream;
1218 struct wined3d_buffer *prev_buffer;
1220 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1221 device, stream_idx, buffer, offset, stride);
1223 if (stream_idx >= MAX_STREAMS)
1225 WARN("Stream index %u out of range.\n", stream_idx);
1226 return WINED3DERR_INVALIDCALL;
1228 else if (offset & 0x3)
1230 WARN("Offset %u is not 4 byte aligned.\n", offset);
1231 return WINED3DERR_INVALIDCALL;
1234 stream = &device->update_state->streams[stream_idx];
1235 prev_buffer = stream->buffer;
1237 if (device->recording)
1238 device->recording->changed.streamSource |= 1 << stream_idx;
1240 if (prev_buffer == buffer
1241 && stream->stride == stride
1242 && stream->offset == offset)
1244 TRACE("Application is setting the old values over, nothing to do.\n");
1245 return WINED3D_OK;
1248 stream->buffer = buffer;
1249 if (buffer)
1251 stream->stride = stride;
1252 stream->offset = offset;
1255 if (buffer)
1256 wined3d_buffer_incref(buffer);
1257 if (!device->recording)
1258 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1259 if (prev_buffer)
1260 wined3d_buffer_decref(prev_buffer);
1262 return WINED3D_OK;
1265 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1266 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1268 const struct wined3d_stream_state *stream;
1270 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1271 device, stream_idx, buffer, offset, stride);
1273 if (stream_idx >= MAX_STREAMS)
1275 WARN("Stream index %u out of range.\n", stream_idx);
1276 return WINED3DERR_INVALIDCALL;
1279 stream = &device->state.streams[stream_idx];
1280 *buffer = stream->buffer;
1281 if (offset)
1282 *offset = stream->offset;
1283 *stride = stream->stride;
1285 return WINED3D_OK;
1288 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1290 struct wined3d_stream_state *stream;
1291 UINT old_flags, old_freq;
1293 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1295 /* Verify input. At least in d3d9 this is invalid. */
1296 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1298 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1299 return WINED3DERR_INVALIDCALL;
1301 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1303 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1304 return WINED3DERR_INVALIDCALL;
1306 if (!divider)
1308 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1309 return WINED3DERR_INVALIDCALL;
1312 stream = &device->update_state->streams[stream_idx];
1313 old_flags = stream->flags;
1314 old_freq = stream->frequency;
1316 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1317 stream->frequency = divider & 0x7fffff;
1319 if (device->recording)
1320 device->recording->changed.streamFreq |= 1 << stream_idx;
1321 else if (stream->frequency != old_freq || stream->flags != old_flags)
1322 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1324 return WINED3D_OK;
1327 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1328 UINT stream_idx, UINT *divider)
1330 const struct wined3d_stream_state *stream;
1332 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1334 stream = &device->state.streams[stream_idx];
1335 *divider = stream->flags | stream->frequency;
1337 TRACE("Returning %#x.\n", *divider);
1339 return WINED3D_OK;
1342 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1343 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1345 TRACE("device %p, state %s, matrix %p.\n",
1346 device, debug_d3dtstype(d3dts), matrix);
1347 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1348 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1349 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1350 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1352 /* Handle recording of state blocks. */
1353 if (device->recording)
1355 TRACE("Recording... not performing anything.\n");
1356 device->recording->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1357 device->update_state->transforms[d3dts] = *matrix;
1358 return;
1361 /* If the new matrix is the same as the current one,
1362 * we cut off any further processing. this seems to be a reasonable
1363 * optimization because as was noticed, some apps (warcraft3 for example)
1364 * tend towards setting the same matrix repeatedly for some reason.
1366 * From here on we assume that the new matrix is different, wherever it matters. */
1367 if (!memcmp(&device->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1369 TRACE("The application is setting the same matrix over again.\n");
1370 return;
1373 device->state.transforms[d3dts] = *matrix;
1374 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1377 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1378 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1380 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1382 *matrix = device->state.transforms[state];
1385 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1386 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1388 const struct wined3d_matrix *mat;
1389 struct wined3d_matrix temp;
1391 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1393 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1394 * below means it will be recorded in a state block change, but it
1395 * works regardless where it is recorded.
1396 * If this is found to be wrong, change to StateBlock. */
1397 if (state > HIGHEST_TRANSFORMSTATE)
1399 WARN("Unhandled transform state %#x.\n", state);
1400 return;
1403 mat = &device->update_state->transforms[state];
1404 multiply_matrix(&temp, mat, matrix);
1406 /* Apply change via set transform - will reapply to eg. lights this way. */
1407 wined3d_device_set_transform(device, state, &temp);
1410 /* Note lights are real special cases. Although the device caps state only
1411 * e.g. 8 are supported, you can reference any indexes you want as long as
1412 * that number max are enabled at any one point in time. Therefore since the
1413 * indices can be anything, we need a hashmap of them. However, this causes
1414 * stateblock problems. When capturing the state block, I duplicate the
1415 * hashmap, but when recording, just build a chain pretty much of commands to
1416 * be replayed. */
1417 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1418 UINT light_idx, const struct wined3d_light *light)
1420 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1421 struct wined3d_light_info *object = NULL;
1422 struct list *e;
1423 float rho;
1425 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1427 /* Check the parameter range. Need for speed most wanted sets junk lights
1428 * which confuse the GL driver. */
1429 if (!light)
1430 return WINED3DERR_INVALIDCALL;
1432 switch (light->type)
1434 case WINED3D_LIGHT_POINT:
1435 case WINED3D_LIGHT_SPOT:
1436 case WINED3D_LIGHT_PARALLELPOINT:
1437 case WINED3D_LIGHT_GLSPOT:
1438 /* Incorrect attenuation values can cause the gl driver to crash.
1439 * Happens with Need for speed most wanted. */
1440 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1442 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1443 return WINED3DERR_INVALIDCALL;
1445 break;
1447 case WINED3D_LIGHT_DIRECTIONAL:
1448 /* Ignores attenuation */
1449 break;
1451 default:
1452 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1453 return WINED3DERR_INVALIDCALL;
1456 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1458 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1459 if (object->OriginalIndex == light_idx)
1460 break;
1461 object = NULL;
1464 if (!object)
1466 TRACE("Adding new light\n");
1467 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1468 if (!object)
1469 return E_OUTOFMEMORY;
1471 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1472 object->glIndex = -1;
1473 object->OriginalIndex = light_idx;
1476 /* Initialize the object. */
1477 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1478 light_idx, light->type,
1479 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1480 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1481 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1482 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1483 light->direction.x, light->direction.y, light->direction.z);
1484 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1485 light->range, light->falloff, light->theta, light->phi);
1487 /* Update the live definitions if the light is currently assigned a glIndex. */
1488 if (object->glIndex != -1 && !device->recording)
1490 if (object->OriginalParms.type != light->type)
1491 device_invalidate_state(device, STATE_LIGHT_TYPE);
1492 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1495 /* Save away the information. */
1496 object->OriginalParms = *light;
1498 switch (light->type)
1500 case WINED3D_LIGHT_POINT:
1501 /* Position */
1502 object->lightPosn[0] = light->position.x;
1503 object->lightPosn[1] = light->position.y;
1504 object->lightPosn[2] = light->position.z;
1505 object->lightPosn[3] = 1.0f;
1506 object->cutoff = 180.0f;
1507 /* FIXME: Range */
1508 break;
1510 case WINED3D_LIGHT_DIRECTIONAL:
1511 /* Direction */
1512 object->lightPosn[0] = -light->direction.x;
1513 object->lightPosn[1] = -light->direction.y;
1514 object->lightPosn[2] = -light->direction.z;
1515 object->lightPosn[3] = 0.0f;
1516 object->exponent = 0.0f;
1517 object->cutoff = 180.0f;
1518 break;
1520 case WINED3D_LIGHT_SPOT:
1521 /* Position */
1522 object->lightPosn[0] = light->position.x;
1523 object->lightPosn[1] = light->position.y;
1524 object->lightPosn[2] = light->position.z;
1525 object->lightPosn[3] = 1.0f;
1527 /* Direction */
1528 object->lightDirn[0] = light->direction.x;
1529 object->lightDirn[1] = light->direction.y;
1530 object->lightDirn[2] = light->direction.z;
1531 object->lightDirn[3] = 1.0f;
1533 /* opengl-ish and d3d-ish spot lights use too different models
1534 * for the light "intensity" as a function of the angle towards
1535 * the main light direction, so we only can approximate very
1536 * roughly. However, spot lights are rather rarely used in games
1537 * (if ever used at all). Furthermore if still used, probably
1538 * nobody pays attention to such details. */
1539 if (!light->falloff)
1541 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1542 * equations have the falloff resp. exponent parameter as an
1543 * exponent, so the spot light lighting will always be 1.0 for
1544 * both of them, and we don't have to care for the rest of the
1545 * rather complex calculation. */
1546 object->exponent = 0.0f;
1548 else
1550 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1551 if (rho < 0.0001f)
1552 rho = 0.0001f;
1553 object->exponent = -0.3f / logf(cosf(rho / 2));
1556 if (object->exponent > 128.0f)
1557 object->exponent = 128.0f;
1559 object->cutoff = (float)(light->phi * 90 / M_PI);
1560 /* FIXME: Range */
1561 break;
1563 default:
1564 FIXME("Unrecognized light type %#x.\n", light->type);
1567 return WINED3D_OK;
1570 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1571 UINT light_idx, struct wined3d_light *light)
1573 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1574 struct wined3d_light_info *light_info = NULL;
1575 struct list *e;
1577 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1579 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1581 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1582 if (light_info->OriginalIndex == light_idx)
1583 break;
1584 light_info = NULL;
1587 if (!light_info)
1589 TRACE("Light information requested but light not defined\n");
1590 return WINED3DERR_INVALIDCALL;
1593 *light = light_info->OriginalParms;
1594 return WINED3D_OK;
1597 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1599 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1600 struct wined3d_light_info *light_info = NULL;
1601 struct list *e;
1603 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1605 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1607 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1608 if (light_info->OriginalIndex == light_idx)
1609 break;
1610 light_info = NULL;
1612 TRACE("Found light %p.\n", light_info);
1614 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1615 if (!light_info)
1617 TRACE("Light enabled requested but light not defined, so defining one!\n");
1618 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1620 /* Search for it again! Should be fairly quick as near head of list. */
1621 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1623 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1624 if (light_info->OriginalIndex == light_idx)
1625 break;
1626 light_info = NULL;
1628 if (!light_info)
1630 FIXME("Adding default lights has failed dismally\n");
1631 return WINED3DERR_INVALIDCALL;
1635 if (!enable)
1637 if (light_info->glIndex != -1)
1639 if (!device->recording)
1641 device_invalidate_state(device, STATE_LIGHT_TYPE);
1642 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1645 device->update_state->lights[light_info->glIndex] = NULL;
1646 light_info->glIndex = -1;
1648 else
1650 TRACE("Light already disabled, nothing to do\n");
1652 light_info->enabled = FALSE;
1654 else
1656 light_info->enabled = TRUE;
1657 if (light_info->glIndex != -1)
1659 TRACE("Nothing to do as light was enabled\n");
1661 else
1663 unsigned int i;
1664 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1665 /* Find a free GL light. */
1666 for (i = 0; i < gl_info->limits.lights; ++i)
1668 if (!device->update_state->lights[i])
1670 device->update_state->lights[i] = light_info;
1671 light_info->glIndex = i;
1672 break;
1675 if (light_info->glIndex == -1)
1677 /* Our tests show that Windows returns D3D_OK in this situation, even with
1678 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1679 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1680 * as well for those lights.
1682 * TODO: Test how this affects rendering. */
1683 WARN("Too many concurrently active lights\n");
1684 return WINED3D_OK;
1687 /* i == light_info->glIndex */
1688 if (!device->recording)
1690 device_invalidate_state(device, STATE_LIGHT_TYPE);
1691 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1696 return WINED3D_OK;
1699 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1701 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1702 struct wined3d_light_info *light_info = NULL;
1703 struct list *e;
1705 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1707 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1709 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1710 if (light_info->OriginalIndex == light_idx)
1711 break;
1712 light_info = NULL;
1715 if (!light_info)
1717 TRACE("Light enabled state requested but light not defined.\n");
1718 return WINED3DERR_INVALIDCALL;
1720 /* true is 128 according to SetLightEnable */
1721 *enable = light_info->enabled ? 128 : 0;
1722 return WINED3D_OK;
1725 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1726 UINT plane_idx, const struct wined3d_vec4 *plane)
1728 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1730 /* Validate plane_idx. */
1731 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1733 TRACE("Application has requested clipplane this device doesn't support.\n");
1734 return WINED3DERR_INVALIDCALL;
1737 if (device->recording)
1738 device->recording->changed.clipplane |= 1 << plane_idx;
1740 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1742 TRACE("Application is setting old values over, nothing to do.\n");
1743 return WINED3D_OK;
1746 device->update_state->clip_planes[plane_idx] = *plane;
1748 if (!device->recording)
1749 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1751 return WINED3D_OK;
1754 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1755 UINT plane_idx, struct wined3d_vec4 *plane)
1757 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1759 /* Validate plane_idx. */
1760 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1762 TRACE("Application has requested clipplane this device doesn't support.\n");
1763 return WINED3DERR_INVALIDCALL;
1766 *plane = device->state.clip_planes[plane_idx];
1768 return WINED3D_OK;
1771 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1772 const struct wined3d_clip_status *clip_status)
1774 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1776 if (!clip_status)
1777 return WINED3DERR_INVALIDCALL;
1779 return WINED3D_OK;
1782 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1783 struct wined3d_clip_status *clip_status)
1785 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1787 if (!clip_status)
1788 return WINED3DERR_INVALIDCALL;
1790 return WINED3D_OK;
1793 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1795 TRACE("device %p, material %p.\n", device, material);
1797 device->update_state->material = *material;
1799 if (device->recording)
1800 device->recording->changed.material = TRUE;
1801 else
1802 wined3d_cs_emit_set_material(device->cs, material);
1805 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1807 TRACE("device %p, material %p.\n", device, material);
1809 *material = device->state.material;
1811 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1812 material->diffuse.r, material->diffuse.g,
1813 material->diffuse.b, material->diffuse.a);
1814 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1815 material->ambient.r, material->ambient.g,
1816 material->ambient.b, material->ambient.a);
1817 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1818 material->specular.r, material->specular.g,
1819 material->specular.b, material->specular.a);
1820 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1821 material->emissive.r, material->emissive.g,
1822 material->emissive.b, material->emissive.a);
1823 TRACE("power %.8e.\n", material->power);
1826 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1827 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1829 enum wined3d_format_id prev_format;
1830 struct wined3d_buffer *prev_buffer;
1832 TRACE("device %p, buffer %p, format %s.\n",
1833 device, buffer, debug_d3dformat(format_id));
1835 prev_buffer = device->update_state->index_buffer;
1836 prev_format = device->update_state->index_format;
1838 device->update_state->index_buffer = buffer;
1839 device->update_state->index_format = format_id;
1841 if (device->recording)
1842 device->recording->changed.indices = TRUE;
1844 if (prev_buffer == buffer && prev_format == format_id)
1845 return;
1847 if (buffer)
1848 wined3d_buffer_incref(buffer);
1849 if (!device->recording)
1850 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1851 if (prev_buffer)
1852 wined3d_buffer_decref(prev_buffer);
1855 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1856 enum wined3d_format_id *format)
1858 TRACE("device %p, format %p.\n", device, format);
1860 *format = device->state.index_format;
1861 return device->state.index_buffer;
1864 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1866 TRACE("device %p, base_index %d.\n", device, base_index);
1868 device->update_state->base_vertex_index = base_index;
1871 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1873 TRACE("device %p.\n", device);
1875 return device->state.base_vertex_index;
1878 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1880 TRACE("device %p, viewport %p.\n", device, viewport);
1881 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1882 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1884 device->update_state->viewport = *viewport;
1886 /* Handle recording of state blocks */
1887 if (device->recording)
1889 TRACE("Recording... not performing anything\n");
1890 device->recording->changed.viewport = TRUE;
1891 return;
1894 wined3d_cs_emit_set_viewport(device->cs, viewport);
1897 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1899 TRACE("device %p, viewport %p.\n", device, viewport);
1901 *viewport = device->state.viewport;
1904 static void resolve_depth_buffer(struct wined3d_state *state)
1906 struct wined3d_texture *texture = state->textures[0];
1907 struct wined3d_surface *depth_stencil, *surface;
1909 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
1910 || !(texture->resource.format->flags & WINED3DFMT_FLAG_DEPTH))
1911 return;
1912 surface = surface_from_resource(texture->sub_resources[0]);
1913 if (!(depth_stencil = wined3d_rendertarget_view_get_surface(state->fb->depth_stencil)))
1914 return;
1916 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
1919 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1920 enum wined3d_render_state state, DWORD value)
1922 DWORD old_value = device->state.render_states[state];
1924 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1926 device->update_state->render_states[state] = value;
1928 /* Handle recording of state blocks. */
1929 if (device->recording)
1931 TRACE("Recording... not performing anything.\n");
1932 device->recording->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
1933 return;
1936 /* Compared here and not before the assignment to allow proper stateblock recording. */
1937 if (value == old_value)
1938 TRACE("Application is setting the old value over, nothing to do.\n");
1939 else
1940 wined3d_cs_emit_set_render_state(device->cs, state, value);
1942 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1944 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1945 resolve_depth_buffer(&device->state);
1949 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1951 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1953 return device->state.render_states[state];
1956 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
1957 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1959 DWORD old_value;
1961 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1962 device, sampler_idx, debug_d3dsamplerstate(state), value);
1964 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
1965 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
1967 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
1969 WARN("Invalid sampler %u.\n", sampler_idx);
1970 return; /* Windows accepts overflowing this array ... we do not. */
1973 old_value = device->state.sampler_states[sampler_idx][state];
1974 device->update_state->sampler_states[sampler_idx][state] = value;
1976 /* Handle recording of state blocks. */
1977 if (device->recording)
1979 TRACE("Recording... not performing anything.\n");
1980 device->recording->changed.samplerState[sampler_idx] |= 1 << state;
1981 return;
1984 if (old_value == value)
1986 TRACE("Application is setting the old value over, nothing to do.\n");
1987 return;
1990 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
1993 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
1994 UINT sampler_idx, enum wined3d_sampler_state state)
1996 TRACE("device %p, sampler_idx %u, state %s.\n",
1997 device, sampler_idx, debug_d3dsamplerstate(state));
1999 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2000 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2002 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2004 WARN("Invalid sampler %u.\n", sampler_idx);
2005 return 0; /* Windows accepts overflowing this array ... we do not. */
2008 return device->state.sampler_states[sampler_idx][state];
2011 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2013 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2015 if (device->recording)
2016 device->recording->changed.scissorRect = TRUE;
2018 if (EqualRect(&device->update_state->scissor_rect, rect))
2020 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2021 return;
2023 CopyRect(&device->update_state->scissor_rect, rect);
2025 if (device->recording)
2027 TRACE("Recording... not performing anything.\n");
2028 return;
2031 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2034 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2036 TRACE("device %p, rect %p.\n", device, rect);
2038 *rect = device->state.scissor_rect;
2039 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2042 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2043 struct wined3d_vertex_declaration *declaration)
2045 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2047 TRACE("device %p, declaration %p.\n", device, declaration);
2049 if (device->recording)
2050 device->recording->changed.vertexDecl = TRUE;
2052 if (declaration == prev)
2053 return;
2055 if (declaration)
2056 wined3d_vertex_declaration_incref(declaration);
2057 device->update_state->vertex_declaration = declaration;
2058 if (!device->recording)
2059 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2060 if (prev)
2061 wined3d_vertex_declaration_decref(prev);
2064 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2066 TRACE("device %p.\n", device);
2068 return device->state.vertex_declaration;
2071 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2073 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2075 TRACE("device %p, shader %p.\n", device, shader);
2077 if (device->recording)
2078 device->recording->changed.vertexShader = TRUE;
2080 if (shader == prev)
2081 return;
2083 if (shader)
2084 wined3d_shader_incref(shader);
2085 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2086 if (!device->recording)
2087 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2088 if (prev)
2089 wined3d_shader_decref(prev);
2092 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2094 TRACE("device %p.\n", device);
2096 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2099 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2100 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2102 struct wined3d_buffer *prev;
2104 if (idx >= MAX_CONSTANT_BUFFERS)
2106 WARN("Invalid constant buffer index %u.\n", idx);
2107 return;
2110 prev = device->update_state->cb[type][idx];
2111 if (buffer == prev)
2112 return;
2114 if (buffer)
2115 wined3d_buffer_incref(buffer);
2116 device->update_state->cb[type][idx] = buffer;
2117 if (!device->recording)
2118 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2119 if (prev)
2120 wined3d_buffer_decref(prev);
2123 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2125 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2127 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2130 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2132 TRACE("device %p, idx %u.\n", device, idx);
2134 if (idx >= MAX_CONSTANT_BUFFERS)
2136 WARN("Invalid constant buffer index %u.\n", idx);
2137 return NULL;
2140 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2143 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2144 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2146 struct wined3d_shader_resource_view *prev;
2148 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2150 WARN("Invalid view index %u.\n", idx);
2151 return;
2154 prev = device->update_state->shader_resource_view[type][idx];
2155 if (view == prev)
2156 return;
2158 if (view)
2159 wined3d_shader_resource_view_incref(view);
2160 device->update_state->shader_resource_view[type][idx] = view;
2161 if (!device->recording)
2162 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2163 if (prev)
2164 wined3d_shader_resource_view_decref(prev);
2167 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2168 UINT idx, struct wined3d_shader_resource_view *view)
2170 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2172 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2175 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2176 UINT idx)
2178 TRACE("device %p, idx %u.\n", device, idx);
2180 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2182 WARN("Invalid view index %u.\n", idx);
2183 return NULL;
2186 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2189 static void wined3d_device_set_sampler(struct wined3d_device *device,
2190 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2192 struct wined3d_sampler *prev;
2194 if (idx >= MAX_SAMPLER_OBJECTS)
2196 WARN("Invalid sampler index %u.\n", idx);
2197 return;
2200 prev = device->update_state->sampler[type][idx];
2201 if (sampler == prev)
2202 return;
2204 if (sampler)
2205 wined3d_sampler_incref(sampler);
2206 device->update_state->sampler[type][idx] = sampler;
2207 if (!device->recording)
2208 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2209 if (prev)
2210 wined3d_sampler_decref(prev);
2213 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2215 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2217 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2220 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2222 TRACE("device %p, idx %u.\n", device, idx);
2224 if (idx >= MAX_SAMPLER_OBJECTS)
2226 WARN("Invalid sampler index %u.\n", idx);
2227 return NULL;
2230 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2233 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2235 UINT i;
2237 for (i = 0; i < device->context_count; ++i)
2239 device->contexts[i]->constant_update_mask |= mask;
2243 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2244 UINT start_register, const BOOL *constants, UINT bool_count)
2246 UINT count = min(bool_count, MAX_CONST_B - start_register);
2247 UINT i;
2249 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2250 device, start_register, constants, bool_count);
2252 if (!constants || start_register >= MAX_CONST_B)
2253 return WINED3DERR_INVALIDCALL;
2255 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2256 for (i = 0; i < count; ++i)
2257 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2259 if (device->recording)
2261 for (i = start_register; i < count + start_register; ++i)
2262 device->recording->changed.vertexShaderConstantsB |= (1 << i);
2264 else
2266 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2269 return WINED3D_OK;
2272 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2273 UINT start_register, BOOL *constants, UINT bool_count)
2275 UINT count = min(bool_count, MAX_CONST_B - start_register);
2277 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2278 device, start_register, constants, bool_count);
2280 if (!constants || start_register >= MAX_CONST_B)
2281 return WINED3DERR_INVALIDCALL;
2283 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2285 return WINED3D_OK;
2288 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2289 UINT start_register, const int *constants, UINT vector4i_count)
2291 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2292 UINT i;
2294 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2295 device, start_register, constants, vector4i_count);
2297 if (!constants || start_register >= MAX_CONST_I)
2298 return WINED3DERR_INVALIDCALL;
2300 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2301 for (i = 0; i < count; ++i)
2302 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2303 constants[i * 4], constants[i * 4 + 1],
2304 constants[i * 4 + 2], constants[i * 4 + 3]);
2306 if (device->recording)
2308 for (i = start_register; i < count + start_register; ++i)
2309 device->recording->changed.vertexShaderConstantsI |= (1 << i);
2311 else
2313 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2316 return WINED3D_OK;
2319 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2320 UINT start_register, int *constants, UINT vector4i_count)
2322 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2324 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2325 device, start_register, constants, vector4i_count);
2327 if (!constants || start_register >= MAX_CONST_I)
2328 return WINED3DERR_INVALIDCALL;
2330 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2331 return WINED3D_OK;
2334 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2335 UINT start_register, const float *constants, UINT vector4f_count)
2337 UINT i;
2338 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2340 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2341 device, start_register, constants, vector4f_count);
2343 /* Specifically test start_register > limit to catch MAX_UINT overflows
2344 * when adding start_register + vector4f_count. */
2345 if (!constants
2346 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2347 || start_register > d3d_info->limits.vs_uniform_count)
2348 return WINED3DERR_INVALIDCALL;
2350 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2351 constants, vector4f_count * sizeof(float) * 4);
2352 if (TRACE_ON(d3d))
2354 for (i = 0; i < vector4f_count; ++i)
2355 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2356 constants[i * 4], constants[i * 4 + 1],
2357 constants[i * 4 + 2], constants[i * 4 + 3]);
2360 if (device->recording)
2361 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2362 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2363 else
2364 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2367 return WINED3D_OK;
2370 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2371 UINT start_register, float *constants, UINT vector4f_count)
2373 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2374 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2376 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2377 device, start_register, constants, vector4f_count);
2379 if (!constants || count < 0)
2380 return WINED3DERR_INVALIDCALL;
2382 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2384 return WINED3D_OK;
2387 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2389 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2391 TRACE("device %p, shader %p.\n", device, shader);
2393 if (device->recording)
2394 device->recording->changed.pixelShader = TRUE;
2396 if (shader == prev)
2397 return;
2399 if (shader)
2400 wined3d_shader_incref(shader);
2401 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2402 if (!device->recording)
2403 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2404 if (prev)
2405 wined3d_shader_decref(prev);
2408 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2410 TRACE("device %p.\n", device);
2412 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2415 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2417 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2419 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2422 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2424 TRACE("device %p, idx %u.\n", device, idx);
2426 if (idx >= MAX_CONSTANT_BUFFERS)
2428 WARN("Invalid constant buffer index %u.\n", idx);
2429 return NULL;
2432 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2435 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2436 UINT idx, struct wined3d_shader_resource_view *view)
2438 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2440 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2443 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2444 UINT idx)
2446 TRACE("device %p, idx %u.\n", device, idx);
2448 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2450 WARN("Invalid view index %u.\n", idx);
2451 return NULL;
2454 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2457 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2459 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2461 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2464 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2466 TRACE("device %p, idx %u.\n", device, idx);
2468 if (idx >= MAX_SAMPLER_OBJECTS)
2470 WARN("Invalid sampler index %u.\n", idx);
2471 return NULL;
2474 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2477 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2478 UINT start_register, const BOOL *constants, UINT bool_count)
2480 UINT count = min(bool_count, MAX_CONST_B - start_register);
2481 UINT i;
2483 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2484 device, start_register, constants, bool_count);
2486 if (!constants || start_register >= MAX_CONST_B)
2487 return WINED3DERR_INVALIDCALL;
2489 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2490 for (i = 0; i < count; ++i)
2491 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2493 if (device->recording)
2495 for (i = start_register; i < count + start_register; ++i)
2496 device->recording->changed.pixelShaderConstantsB |= (1 << i);
2498 else
2500 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2503 return WINED3D_OK;
2506 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2507 UINT start_register, BOOL *constants, UINT bool_count)
2509 UINT count = min(bool_count, MAX_CONST_B - start_register);
2511 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2512 device, start_register, constants, bool_count);
2514 if (!constants || start_register >= MAX_CONST_B)
2515 return WINED3DERR_INVALIDCALL;
2517 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2519 return WINED3D_OK;
2522 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2523 UINT start_register, const int *constants, UINT vector4i_count)
2525 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2526 UINT i;
2528 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2529 device, start_register, constants, vector4i_count);
2531 if (!constants || start_register >= MAX_CONST_I)
2532 return WINED3DERR_INVALIDCALL;
2534 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2535 for (i = 0; i < count; ++i)
2536 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2537 constants[i * 4], constants[i * 4 + 1],
2538 constants[i * 4 + 2], constants[i * 4 + 3]);
2540 if (device->recording)
2542 for (i = start_register; i < count + start_register; ++i)
2543 device->recording->changed.pixelShaderConstantsI |= (1 << i);
2545 else
2547 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2550 return WINED3D_OK;
2553 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2554 UINT start_register, int *constants, UINT vector4i_count)
2556 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2558 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2559 device, start_register, constants, vector4i_count);
2561 if (!constants || start_register >= MAX_CONST_I)
2562 return WINED3DERR_INVALIDCALL;
2564 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2566 return WINED3D_OK;
2569 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2570 UINT start_register, const float *constants, UINT vector4f_count)
2572 UINT i;
2573 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2575 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2576 device, start_register, constants, vector4f_count);
2578 /* Specifically test start_register > limit to catch MAX_UINT overflows
2579 * when adding start_register + vector4f_count. */
2580 if (!constants
2581 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2582 || start_register > d3d_info->limits.ps_uniform_count)
2583 return WINED3DERR_INVALIDCALL;
2585 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2586 constants, vector4f_count * sizeof(float) * 4);
2587 if (TRACE_ON(d3d))
2589 for (i = 0; i < vector4f_count; ++i)
2590 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2591 constants[i * 4], constants[i * 4 + 1],
2592 constants[i * 4 + 2], constants[i * 4 + 3]);
2595 if (device->recording)
2596 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2597 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2598 else
2599 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2601 return WINED3D_OK;
2604 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2605 UINT start_register, float *constants, UINT vector4f_count)
2607 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2608 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2610 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2611 device, start_register, constants, vector4f_count);
2613 if (!constants || count < 0)
2614 return WINED3DERR_INVALIDCALL;
2616 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2618 return WINED3D_OK;
2621 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2623 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2625 TRACE("device %p, shader %p.\n", device, shader);
2627 if (device->recording || shader == prev)
2628 return;
2629 if (shader)
2630 wined3d_shader_incref(shader);
2631 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2632 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2633 if (prev)
2634 wined3d_shader_decref(prev);
2637 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2639 TRACE("device %p.\n", device);
2641 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2644 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2646 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2648 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2651 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2653 TRACE("device %p, idx %u.\n", device, idx);
2655 if (idx >= MAX_CONSTANT_BUFFERS)
2657 WARN("Invalid constant buffer index %u.\n", idx);
2658 return NULL;
2661 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2664 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2665 UINT idx, struct wined3d_shader_resource_view *view)
2667 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2669 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2672 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2673 UINT idx)
2675 TRACE("device %p, idx %u.\n", device, idx);
2677 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2679 WARN("Invalid view index %u.\n", idx);
2680 return NULL;
2683 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2686 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2688 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2690 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2693 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2695 TRACE("device %p, idx %u.\n", device, idx);
2697 if (idx >= MAX_SAMPLER_OBJECTS)
2699 WARN("Invalid sampler index %u.\n", idx);
2700 return NULL;
2703 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2706 /* Context activation is done by the caller. */
2707 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2708 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2709 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2710 DWORD DestFVF)
2712 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2713 struct wined3d_viewport vp;
2714 UINT vertex_size;
2715 unsigned int i;
2716 BYTE *dest_ptr;
2717 BOOL doClip;
2718 DWORD numTextures;
2719 HRESULT hr;
2721 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
2723 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2726 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
2728 ERR("Source has no position mask\n");
2729 return WINED3DERR_INVALIDCALL;
2732 if (device->state.render_states[WINED3D_RS_CLIPPING])
2734 static BOOL warned = FALSE;
2736 * The clipping code is not quite correct. Some things need
2737 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2738 * so disable clipping for now.
2739 * (The graphics in Half-Life are broken, and my processvertices
2740 * test crashes with IDirect3DDevice3)
2741 doClip = TRUE;
2743 doClip = FALSE;
2744 if(!warned) {
2745 warned = TRUE;
2746 FIXME("Clipping is broken and disabled for now\n");
2749 else
2750 doClip = FALSE;
2752 vertex_size = get_flexible_vertex_size(DestFVF);
2753 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2755 WARN("Failed to map buffer, hr %#x.\n", hr);
2756 return hr;
2759 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2760 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2761 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2763 TRACE("View mat:\n");
2764 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);
2765 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);
2766 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);
2767 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);
2769 TRACE("Proj mat:\n");
2770 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);
2771 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);
2772 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);
2773 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);
2775 TRACE("World mat:\n");
2776 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);
2777 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);
2778 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);
2779 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);
2781 /* Get the viewport */
2782 wined3d_device_get_viewport(device, &vp);
2783 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2784 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2786 multiply_matrix(&mat,&view_mat,&world_mat);
2787 multiply_matrix(&mat,&proj_mat,&mat);
2789 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2791 for (i = 0; i < dwCount; i+= 1) {
2792 unsigned int tex_index;
2794 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2795 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2796 /* The position first */
2797 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2798 const float *p = (const float *)(element->data.addr + i * element->stride);
2799 float x, y, z, rhw;
2800 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2802 /* Multiplication with world, view and projection matrix */
2803 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);
2804 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);
2805 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);
2806 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);
2808 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2810 /* WARNING: The following things are taken from d3d7 and were not yet checked
2811 * against d3d8 or d3d9!
2814 /* Clipping conditions: From msdn
2816 * A vertex is clipped if it does not match the following requirements
2817 * -rhw < x <= rhw
2818 * -rhw < y <= rhw
2819 * 0 < z <= rhw
2820 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2822 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2823 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2827 if( !doClip ||
2828 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2829 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2830 ( rhw > eps ) ) ) {
2832 /* "Normal" viewport transformation (not clipped)
2833 * 1) The values are divided by rhw
2834 * 2) The y axis is negative, so multiply it with -1
2835 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2836 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2837 * 4) Multiply x with Width/2 and add Width/2
2838 * 5) The same for the height
2839 * 6) Add the viewpoint X and Y to the 2D coordinates and
2840 * The minimum Z value to z
2841 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2843 * Well, basically it's simply a linear transformation into viewport
2844 * coordinates
2847 x /= rhw;
2848 y /= rhw;
2849 z /= rhw;
2851 y *= -1;
2853 x *= vp.width / 2;
2854 y *= vp.height / 2;
2855 z *= vp.max_z - vp.min_z;
2857 x += vp.width / 2 + vp.x;
2858 y += vp.height / 2 + vp.y;
2859 z += vp.min_z;
2861 rhw = 1 / rhw;
2862 } else {
2863 /* That vertex got clipped
2864 * Contrary to OpenGL it is not dropped completely, it just
2865 * undergoes a different calculation.
2867 TRACE("Vertex got clipped\n");
2868 x += rhw;
2869 y += rhw;
2871 x /= 2;
2872 y /= 2;
2874 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2875 * outside of the main vertex buffer memory. That needs some more
2876 * investigation...
2880 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2883 ( (float *) dest_ptr)[0] = x;
2884 ( (float *) dest_ptr)[1] = y;
2885 ( (float *) dest_ptr)[2] = z;
2886 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2888 dest_ptr += 3 * sizeof(float);
2890 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2891 dest_ptr += sizeof(float);
2894 if (DestFVF & WINED3DFVF_PSIZE)
2895 dest_ptr += sizeof(DWORD);
2897 if (DestFVF & WINED3DFVF_NORMAL)
2899 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
2900 const float *normal = (const float *)(element->data.addr + i * element->stride);
2901 /* AFAIK this should go into the lighting information */
2902 FIXME("Didn't expect the destination to have a normal\n");
2903 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
2906 if (DestFVF & WINED3DFVF_DIFFUSE)
2908 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2909 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
2910 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
2912 static BOOL warned = FALSE;
2914 if(!warned) {
2915 ERR("No diffuse color in source, but destination has one\n");
2916 warned = TRUE;
2919 *( (DWORD *) dest_ptr) = 0xffffffff;
2920 dest_ptr += sizeof(DWORD);
2922 else
2924 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
2928 if (DestFVF & WINED3DFVF_SPECULAR)
2930 /* What's the color value in the feedback buffer? */
2931 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2932 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
2933 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
2935 static BOOL warned = FALSE;
2937 if(!warned) {
2938 ERR("No specular color in source, but destination has one\n");
2939 warned = TRUE;
2942 *(DWORD *)dest_ptr = 0xff000000;
2943 dest_ptr += sizeof(DWORD);
2945 else
2947 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
2951 for (tex_index = 0; tex_index < numTextures; ++tex_index)
2953 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
2954 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
2955 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
2957 ERR("No source texture, but destination requests one\n");
2958 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
2960 else
2962 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
2967 wined3d_buffer_unmap(dest);
2969 return WINED3D_OK;
2971 #undef copy_and_next
2973 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
2974 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
2975 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
2977 struct wined3d_state *state = &device->state;
2978 struct wined3d_stream_info stream_info;
2979 const struct wined3d_gl_info *gl_info;
2980 struct wined3d_context *context;
2981 struct wined3d_shader *vs;
2982 unsigned int i;
2983 HRESULT hr;
2984 WORD map;
2986 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
2987 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
2988 device, src_start_idx, dst_idx, vertex_count,
2989 dst_buffer, declaration, flags, dst_fvf);
2991 if (declaration)
2992 FIXME("Output vertex declaration not implemented yet.\n");
2994 /* Need any context to write to the vbo. */
2995 context = context_acquire(device, NULL);
2996 gl_info = context->gl_info;
2998 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
2999 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3000 context_stream_info_from_declaration(context, state, &stream_info);
3001 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3003 /* We can't convert FROM a VBO, and vertex buffers used to source into
3004 * process_vertices() are unlikely to ever be used for drawing. Release
3005 * VBOs in those buffers and fix up the stream_info structure.
3007 * Also apply the start index. */
3008 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3010 struct wined3d_stream_info_element *e;
3011 struct wined3d_buffer *buffer;
3013 if (!(map & 1))
3014 continue;
3016 e = &stream_info.elements[i];
3017 buffer = state->streams[e->stream_idx].buffer;
3018 e->data.buffer_object = 0;
3019 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3020 if (buffer->buffer_object)
3022 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3023 buffer->buffer_object = 0;
3025 if (e->data.addr)
3026 e->data.addr += e->stride * src_start_idx;
3029 hr = process_vertices_strided(device, dst_idx, vertex_count,
3030 &stream_info, dst_buffer, flags, dst_fvf);
3032 context_release(context);
3034 return hr;
3037 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3038 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3040 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3041 DWORD old_value;
3043 TRACE("device %p, stage %u, state %s, value %#x.\n",
3044 device, stage, debug_d3dtexturestate(state), value);
3046 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3048 WARN("Invalid state %#x passed.\n", state);
3049 return;
3052 if (stage >= d3d_info->limits.ffp_blend_stages)
3054 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3055 stage, d3d_info->limits.ffp_blend_stages - 1);
3056 return;
3059 old_value = device->update_state->texture_states[stage][state];
3060 device->update_state->texture_states[stage][state] = value;
3062 if (device->recording)
3064 TRACE("Recording... not performing anything.\n");
3065 device->recording->changed.textureState[stage] |= 1 << state;
3066 return;
3069 /* Checked after the assignments to allow proper stateblock recording. */
3070 if (old_value == value)
3072 TRACE("Application is setting the old value over, nothing to do.\n");
3073 return;
3076 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3079 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3080 UINT stage, enum wined3d_texture_stage_state state)
3082 TRACE("device %p, stage %u, state %s.\n",
3083 device, stage, debug_d3dtexturestate(state));
3085 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3087 WARN("Invalid state %#x passed.\n", state);
3088 return 0;
3091 return device->state.texture_states[stage][state];
3094 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3095 UINT stage, struct wined3d_texture *texture)
3097 struct wined3d_texture *prev;
3099 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3101 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3102 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3104 /* Windows accepts overflowing this array... we do not. */
3105 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3107 WARN("Ignoring invalid stage %u.\n", stage);
3108 return WINED3D_OK;
3111 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3113 WARN("Rejecting attempt to set scratch texture.\n");
3114 return WINED3DERR_INVALIDCALL;
3117 if (device->recording)
3118 device->recording->changed.textures |= 1 << stage;
3120 prev = device->update_state->textures[stage];
3121 TRACE("Previous texture %p.\n", prev);
3123 if (texture == prev)
3125 TRACE("App is setting the same texture again, nothing to do.\n");
3126 return WINED3D_OK;
3129 TRACE("Setting new texture to %p.\n", texture);
3130 device->update_state->textures[stage] = texture;
3132 if (texture)
3133 wined3d_texture_incref(texture);
3134 if (!device->recording)
3135 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3136 if (prev)
3137 wined3d_texture_decref(prev);
3139 return WINED3D_OK;
3142 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3144 TRACE("device %p, stage %u.\n", device, stage);
3146 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3147 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3149 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3151 WARN("Ignoring invalid stage %u.\n", stage);
3152 return NULL; /* Windows accepts overflowing this array ... we do not. */
3155 return device->state.textures[stage];
3158 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3159 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3161 struct wined3d_swapchain *swapchain;
3163 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3164 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3166 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3167 return WINED3DERR_INVALIDCALL;
3169 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3170 return WINED3DERR_INVALIDCALL;
3171 return WINED3D_OK;
3174 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3176 TRACE("device %p, caps %p.\n", device, caps);
3178 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3179 device->create_parms.device_type, caps);
3182 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3183 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3185 struct wined3d_swapchain *swapchain;
3187 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3188 device, swapchain_idx, mode, rotation);
3190 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3191 return WINED3DERR_INVALIDCALL;
3193 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3196 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3198 struct wined3d_stateblock *stateblock;
3199 HRESULT hr;
3201 TRACE("device %p.\n", device);
3203 if (device->recording)
3204 return WINED3DERR_INVALIDCALL;
3206 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3207 if (FAILED(hr))
3208 return hr;
3210 device->recording = stateblock;
3211 device->update_state = &stateblock->state;
3213 TRACE("Recording stateblock %p.\n", stateblock);
3215 return WINED3D_OK;
3218 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3219 struct wined3d_stateblock **stateblock)
3221 struct wined3d_stateblock *object = device->recording;
3223 TRACE("device %p, stateblock %p.\n", device, stateblock);
3225 if (!device->recording)
3227 WARN("Not recording.\n");
3228 *stateblock = NULL;
3229 return WINED3DERR_INVALIDCALL;
3232 stateblock_init_contained_states(object);
3234 *stateblock = object;
3235 device->recording = NULL;
3236 device->update_state = &device->state;
3238 TRACE("Returning stateblock %p.\n", *stateblock);
3240 return WINED3D_OK;
3243 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3245 /* At the moment we have no need for any functionality at the beginning
3246 * of a scene. */
3247 TRACE("device %p.\n", device);
3249 if (device->inScene)
3251 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3252 return WINED3DERR_INVALIDCALL;
3254 device->inScene = TRUE;
3255 return WINED3D_OK;
3258 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3260 struct wined3d_context *context;
3262 TRACE("device %p.\n", device);
3264 if (!device->inScene)
3266 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3267 return WINED3DERR_INVALIDCALL;
3270 context = context_acquire(device, NULL);
3271 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3272 context->gl_info->gl_ops.gl.p_glFlush();
3273 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3274 * fails. */
3275 context_release(context);
3277 device->inScene = FALSE;
3278 return WINED3D_OK;
3281 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3282 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3284 UINT i;
3286 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3287 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3288 dst_window_override, dirty_region, flags);
3290 for (i = 0; i < device->swapchain_count; ++i)
3292 wined3d_swapchain_present(device->swapchains[i], src_rect,
3293 dst_rect, dst_window_override, dirty_region, flags);
3296 return WINED3D_OK;
3299 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3300 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3302 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3303 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3305 if (!rect_count && rects)
3307 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3308 return WINED3D_OK;
3311 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3313 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3314 if (!ds)
3316 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3317 /* TODO: What about depth stencil buffers without stencil bits? */
3318 return WINED3DERR_INVALIDCALL;
3320 else if (flags & WINED3DCLEAR_TARGET)
3322 if (ds->width < device->fb.render_targets[0]->width
3323 || ds->height < device->fb.render_targets[0]->height)
3325 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3326 return WINED3D_OK;
3331 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3333 return WINED3D_OK;
3336 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3337 struct wined3d_query *predicate, BOOL value)
3339 struct wined3d_query *prev;
3341 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3343 prev = device->update_state->predicate;
3344 if (predicate)
3346 FIXME("Predicated rendering not implemented.\n");
3347 wined3d_query_incref(predicate);
3349 device->update_state->predicate = predicate;
3350 device->update_state->predicate_value = value;
3351 if (!device->recording)
3352 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3353 if (prev)
3354 wined3d_query_decref(prev);
3357 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3359 TRACE("device %p, value %p.\n", device, value);
3361 *value = device->state.predicate_value;
3362 return device->state.predicate;
3365 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3366 enum wined3d_primitive_type primitive_type)
3368 GLenum gl_primitive_type, prev;
3370 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3372 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3373 prev = device->update_state->gl_primitive_type;
3374 device->update_state->gl_primitive_type = gl_primitive_type;
3375 if (device->recording)
3376 device->recording->changed.primitive_type = TRUE;
3377 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3378 device_invalidate_state(device, STATE_POINT_SIZE_ENABLE);
3381 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3382 enum wined3d_primitive_type *primitive_type)
3384 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3386 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3388 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3391 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3393 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3395 if (!device->state.vertex_declaration)
3397 WARN("Called without a valid vertex declaration set.\n");
3398 return WINED3DERR_INVALIDCALL;
3401 if (device->state.load_base_vertex_index)
3403 device->state.load_base_vertex_index = 0;
3404 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3407 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3409 return WINED3D_OK;
3412 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3414 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3416 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3418 if (!device->state.index_buffer)
3420 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3421 * without an index buffer set. (The first time at least...)
3422 * D3D8 simply dies, but I doubt it can do much harm to return
3423 * D3DERR_INVALIDCALL there as well. */
3424 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3425 return WINED3DERR_INVALIDCALL;
3428 if (!device->state.vertex_declaration)
3430 WARN("Called without a valid vertex declaration set.\n");
3431 return WINED3DERR_INVALIDCALL;
3434 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3435 device->state.load_base_vertex_index != device->state.base_vertex_index)
3437 device->state.load_base_vertex_index = device->state.base_vertex_index;
3438 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3441 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3443 return WINED3D_OK;
3446 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3447 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3449 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3451 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3454 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3455 static HRESULT device_update_volume(struct wined3d_device *device,
3456 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3458 struct wined3d_const_bo_address data;
3459 struct wined3d_map_desc src;
3460 HRESULT hr;
3461 struct wined3d_context *context;
3463 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3464 device, src_volume, dst_volume);
3466 if (src_volume->resource.format != dst_volume->resource.format)
3468 FIXME("Source and destination formats do not match.\n");
3469 return WINED3DERR_INVALIDCALL;
3471 if (src_volume->resource.width != dst_volume->resource.width
3472 || src_volume->resource.height != dst_volume->resource.height
3473 || src_volume->resource.depth != dst_volume->resource.depth)
3475 FIXME("Source and destination sizes do not match.\n");
3476 return WINED3DERR_INVALIDCALL;
3479 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3480 return hr;
3482 context = context_acquire(device, NULL);
3484 /* Only a prepare, since we're uploading the entire volume. */
3485 wined3d_texture_prepare_texture(dst_volume->container, context, FALSE);
3487 data.buffer_object = 0;
3488 data.addr = src.data;
3489 wined3d_volume_upload_data(dst_volume, context, &data);
3490 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3492 context_release(context);
3494 hr = wined3d_volume_unmap(src_volume);
3496 return hr;
3499 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3500 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3502 enum wined3d_resource_type type;
3503 unsigned int level_count, i;
3504 HRESULT hr;
3505 struct wined3d_context *context;
3507 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3509 /* Verify that the source and destination textures are non-NULL. */
3510 if (!src_texture || !dst_texture)
3512 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3513 return WINED3DERR_INVALIDCALL;
3516 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3518 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3519 return WINED3DERR_INVALIDCALL;
3521 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3523 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3524 return WINED3DERR_INVALIDCALL;
3527 /* Verify that the source and destination textures are the same type. */
3528 type = src_texture->resource.type;
3529 if (dst_texture->resource.type != type)
3531 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3532 return WINED3DERR_INVALIDCALL;
3535 /* Check that both textures have the identical numbers of levels. */
3536 level_count = wined3d_texture_get_level_count(src_texture);
3537 if (wined3d_texture_get_level_count(dst_texture) != level_count)
3539 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
3540 return WINED3DERR_INVALIDCALL;
3543 /* Make sure that the destination texture is loaded. */
3544 context = context_acquire(device, NULL);
3545 wined3d_texture_load(dst_texture, context, FALSE);
3546 context_release(context);
3548 /* Update every surface level of the texture. */
3549 switch (type)
3551 case WINED3D_RTYPE_TEXTURE:
3553 struct wined3d_surface *src_surface;
3554 struct wined3d_surface *dst_surface;
3556 for (i = 0; i < level_count; ++i)
3558 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3559 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3560 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3561 if (FAILED(hr))
3563 WARN("Failed to update surface, hr %#x.\n", hr);
3564 return hr;
3567 break;
3570 case WINED3D_RTYPE_CUBE_TEXTURE:
3572 struct wined3d_surface *src_surface;
3573 struct wined3d_surface *dst_surface;
3575 for (i = 0; i < level_count * 6; ++i)
3577 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3578 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3579 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
3580 if (FAILED(hr))
3582 WARN("Failed to update surface, hr %#x.\n", hr);
3583 return hr;
3586 break;
3589 case WINED3D_RTYPE_VOLUME_TEXTURE:
3591 for (i = 0; i < level_count; ++i)
3593 hr = device_update_volume(device,
3594 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
3595 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3596 if (FAILED(hr))
3598 WARN("Failed to update volume, hr %#x.\n", hr);
3599 return hr;
3602 break;
3605 default:
3606 FIXME("Unsupported texture type %#x.\n", type);
3607 return WINED3DERR_INVALIDCALL;
3610 return WINED3D_OK;
3613 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
3614 UINT swapchain_idx, struct wined3d_surface *dst_surface)
3616 struct wined3d_swapchain *swapchain;
3618 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
3620 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3621 return WINED3DERR_INVALIDCALL;
3623 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
3626 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3628 const struct wined3d_state *state = &device->state;
3629 struct wined3d_texture *texture;
3630 DWORD i;
3632 TRACE("device %p, num_passes %p.\n", device, num_passes);
3634 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3636 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3638 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3639 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3641 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3643 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3644 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3647 texture = state->textures[i];
3648 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
3650 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3652 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3653 return E_FAIL;
3655 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3657 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3658 return E_FAIL;
3660 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3661 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3663 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3664 return E_FAIL;
3668 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3669 || state->render_states[WINED3D_RS_STENCILENABLE])
3671 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3672 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3674 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3676 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3677 return WINED3DERR_CONFLICTINGRENDERSTATE;
3681 /* return a sensible default */
3682 *num_passes = 1;
3684 TRACE("returning D3D_OK\n");
3685 return WINED3D_OK;
3688 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3690 static BOOL warned;
3692 TRACE("device %p, software %#x.\n", device, software);
3694 if (!warned)
3696 FIXME("device %p, software %#x stub!\n", device, software);
3697 warned = TRUE;
3700 device->softwareVertexProcessing = software;
3703 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3705 static BOOL warned;
3707 TRACE("device %p.\n", device);
3709 if (!warned)
3711 TRACE("device %p stub!\n", device);
3712 warned = TRUE;
3715 return device->softwareVertexProcessing;
3718 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3719 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3721 struct wined3d_swapchain *swapchain;
3723 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3724 device, swapchain_idx, raster_status);
3726 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3727 return WINED3DERR_INVALIDCALL;
3729 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3732 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3734 static BOOL warned;
3736 TRACE("device %p, segments %.8e.\n", device, segments);
3738 if (segments != 0.0f)
3740 if (!warned)
3742 FIXME("device %p, segments %.8e stub!\n", device, segments);
3743 warned = TRUE;
3747 return WINED3D_OK;
3750 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3752 static BOOL warned;
3754 TRACE("device %p.\n", device);
3756 if (!warned)
3758 FIXME("device %p stub!\n", device);
3759 warned = TRUE;
3762 return 0.0f;
3765 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
3766 struct wined3d_surface *src_surface, const RECT *src_rect,
3767 struct wined3d_surface *dst_surface, const POINT *dst_point)
3769 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
3770 device, src_surface, wine_dbgstr_rect(src_rect),
3771 dst_surface, wine_dbgstr_point(dst_point));
3773 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
3775 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
3776 src_surface, dst_surface);
3777 return WINED3DERR_INVALIDCALL;
3780 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
3783 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3784 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3786 struct wined3d_surface *dst_surface, *src_surface;
3787 struct wined3d_texture *dst_texture, *src_texture;
3788 unsigned int i, count;
3789 HRESULT hr;
3791 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3793 if (src_resource == dst_resource)
3795 WARN("Source and destination are the same resource.\n");
3796 return;
3799 if (src_resource->type != dst_resource->type)
3801 WARN("Resource types (%s / %s) don't match.\n",
3802 debug_d3dresourcetype(dst_resource->type),
3803 debug_d3dresourcetype(src_resource->type));
3804 return;
3807 if (src_resource->width != dst_resource->width
3808 || src_resource->height != dst_resource->height
3809 || src_resource->depth != dst_resource->depth)
3811 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3812 dst_resource->width, dst_resource->height, dst_resource->depth,
3813 src_resource->width, src_resource->height, src_resource->depth);
3814 return;
3817 if (src_resource->format->id != dst_resource->format->id)
3819 WARN("Resource formats (%s / %s) don't match.\n",
3820 debug_d3dformat(dst_resource->format->id),
3821 debug_d3dformat(src_resource->format->id));
3822 return;
3825 if (dst_resource->type != WINED3D_RTYPE_TEXTURE)
3827 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3828 return;
3831 dst_texture = wined3d_texture_from_resource(dst_resource);
3832 src_texture = wined3d_texture_from_resource(src_resource);
3834 if (src_texture->layer_count != dst_texture->layer_count
3835 || src_texture->level_count != dst_texture->level_count)
3837 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3838 dst_texture->layer_count, dst_texture->level_count,
3839 src_texture->layer_count, src_texture->level_count);
3840 return;
3843 count = dst_texture->layer_count * dst_texture->level_count;
3844 for (i = 0; i < count; ++i)
3846 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3847 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3849 if (FAILED(hr = wined3d_surface_blt(dst_surface, NULL, src_surface, NULL, 0, NULL, WINED3D_TEXF_POINT)))
3850 ERR("Failed to blit, subresource %u, hr %#x.\n", i, hr);
3854 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
3855 struct wined3d_rendertarget_view *view, const RECT *rect, const struct wined3d_color *color)
3857 struct wined3d_resource *resource;
3858 RECT r;
3860 TRACE("device %p, view %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
3861 device, view, wine_dbgstr_rect(rect), color->r, color->g, color->b, color->a);
3863 resource = view->resource;
3864 if (resource->type != WINED3D_RTYPE_TEXTURE && resource->type != WINED3D_RTYPE_CUBE_TEXTURE)
3866 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
3867 return WINED3DERR_INVALIDCALL;
3870 if (view->depth > 1)
3872 FIXME("Layered clears not implemented.\n");
3873 return WINED3DERR_INVALIDCALL;
3876 if (!rect)
3878 SetRect(&r, 0, 0, view->width, view->height);
3879 rect = &r;
3882 resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource), view->sub_resource_idx);
3884 return surface_color_fill(surface_from_resource(resource), rect, color);
3887 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
3888 unsigned int view_idx)
3890 TRACE("device %p, view_idx %u.\n", device, view_idx);
3892 if (view_idx >= device->adapter->gl_info.limits.buffers)
3894 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3895 return NULL;
3898 return device->fb.render_targets[view_idx];
3901 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
3903 TRACE("device %p.\n", device);
3905 return device->fb.depth_stencil;
3908 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
3909 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
3911 struct wined3d_rendertarget_view *prev;
3913 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
3914 device, view_idx, view, set_viewport);
3916 if (view_idx >= device->adapter->gl_info.limits.buffers)
3918 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
3919 return WINED3DERR_INVALIDCALL;
3922 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
3924 WARN("View resource %p doesn't have render target usage.\n", view->resource);
3925 return WINED3DERR_INVALIDCALL;
3928 /* Set the viewport and scissor rectangles, if requested. Tests show that
3929 * stateblock recording is ignored, the change goes directly into the
3930 * primary stateblock. */
3931 if (!view_idx && set_viewport)
3933 struct wined3d_state *state = &device->state;
3935 state->viewport.x = 0;
3936 state->viewport.y = 0;
3937 state->viewport.width = view->width;
3938 state->viewport.height = view->height;
3939 state->viewport.min_z = 0.0f;
3940 state->viewport.max_z = 1.0f;
3941 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
3943 state->scissor_rect.top = 0;
3944 state->scissor_rect.left = 0;
3945 state->scissor_rect.right = view->width;
3946 state->scissor_rect.bottom = view->height;
3947 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
3951 prev = device->fb.render_targets[view_idx];
3952 if (view == prev)
3953 return WINED3D_OK;
3955 if (view)
3956 wined3d_rendertarget_view_incref(view);
3957 device->fb.render_targets[view_idx] = view;
3958 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
3959 /* Release after the assignment, to prevent device_resource_released()
3960 * from seeing the surface as still in use. */
3961 if (prev)
3962 wined3d_rendertarget_view_decref(prev);
3964 return WINED3D_OK;
3967 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
3969 struct wined3d_rendertarget_view *prev;
3971 TRACE("device %p, view %p.\n", device, view);
3973 prev = device->fb.depth_stencil;
3974 if (prev == view)
3976 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
3977 return;
3980 if ((device->fb.depth_stencil = view))
3981 wined3d_rendertarget_view_incref(view);
3982 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
3983 if (prev)
3984 wined3d_rendertarget_view_decref(prev);
3987 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
3988 struct wined3d_surface *cursor_image)
3990 struct wined3d_sub_resource_data data;
3991 struct wined3d_resource_desc desc;
3992 struct wined3d_map_desc map_desc;
3993 struct wined3d_texture *texture;
3994 HRESULT hr;
3996 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
3998 ERR("Failed to map source surface.\n");
3999 return NULL;
4002 data.data = map_desc.data;
4003 data.row_pitch = map_desc.row_pitch;
4004 data.slice_pitch = map_desc.slice_pitch;
4006 desc.resource_type = WINED3D_RTYPE_TEXTURE;
4007 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4008 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4009 desc.multisample_quality = 0;
4010 desc.usage = WINED3DUSAGE_DYNAMIC;
4011 desc.pool = WINED3D_POOL_DEFAULT;
4012 desc.width = cursor_image->resource.width;
4013 desc.height = cursor_image->resource.height;
4014 desc.depth = 1;
4015 desc.size = 0;
4017 hr = wined3d_texture_create(device, &desc, 1, WINED3D_SURFACE_MAPPABLE,
4018 &data, NULL, &wined3d_null_parent_ops, &texture);
4019 wined3d_surface_unmap(cursor_image);
4020 if (FAILED(hr))
4022 ERR("Failed to create cursor texture.\n");
4023 return NULL;
4026 return texture;
4029 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4030 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4032 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4033 device, x_hotspot, y_hotspot, cursor_image);
4035 if (device->cursor_texture)
4037 wined3d_texture_decref(device->cursor_texture);
4038 device->cursor_texture = NULL;
4041 if (cursor_image)
4043 struct wined3d_display_mode mode;
4044 struct wined3d_map_desc map_desc;
4045 HRESULT hr;
4047 /* MSDN: Cursor must be A8R8G8B8 */
4048 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4050 WARN("surface %p has an invalid format.\n", cursor_image);
4051 return WINED3DERR_INVALIDCALL;
4054 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4056 ERR("Failed to get display mode, hr %#x.\n", hr);
4057 return WINED3DERR_INVALIDCALL;
4060 /* MSDN: Cursor must be smaller than the display mode */
4061 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4063 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4064 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4065 mode.width, mode.height);
4066 return WINED3DERR_INVALIDCALL;
4069 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4071 /* Do not store the surface's pointer because the application may
4072 * release it after setting the cursor image. Windows doesn't
4073 * addref the set surface, so we can't do this either without
4074 * creating circular refcount dependencies. */
4075 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
4077 ERR("Failed to create cursor texture.\n");
4078 return WINED3DERR_INVALIDCALL;
4081 device->cursorWidth = cursor_image->resource.width;
4082 device->cursorHeight = cursor_image->resource.height;
4084 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4086 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4087 ICONINFO cursorInfo;
4088 DWORD *maskBits;
4089 HCURSOR cursor;
4091 /* 32-bit user32 cursors ignore the alpha channel if it's all
4092 * zeroes, and use the mask instead. Fill the mask with all ones
4093 * to ensure we still get a fully transparent cursor. */
4094 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4095 memset(maskBits, 0xff, mask_size);
4096 wined3d_surface_map(cursor_image, &map_desc, NULL,
4097 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4098 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4100 cursorInfo.fIcon = FALSE;
4101 cursorInfo.xHotspot = x_hotspot;
4102 cursorInfo.yHotspot = y_hotspot;
4103 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4104 1, 1, maskBits);
4105 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4106 1, 32, map_desc.data);
4107 wined3d_surface_unmap(cursor_image);
4108 /* Create our cursor and clean up. */
4109 cursor = CreateIconIndirect(&cursorInfo);
4110 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4111 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4112 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4113 device->hardwareCursor = cursor;
4114 if (device->bCursorVisible) SetCursor( cursor );
4115 HeapFree(GetProcessHeap(), 0, maskBits);
4119 device->xHotSpot = x_hotspot;
4120 device->yHotSpot = y_hotspot;
4121 return WINED3D_OK;
4124 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4125 int x_screen_space, int y_screen_space, DWORD flags)
4127 TRACE("device %p, x %d, y %d, flags %#x.\n",
4128 device, x_screen_space, y_screen_space, flags);
4130 device->xScreenSpace = x_screen_space;
4131 device->yScreenSpace = y_screen_space;
4133 if (device->hardwareCursor)
4135 POINT pt;
4137 GetCursorPos( &pt );
4138 if (x_screen_space == pt.x && y_screen_space == pt.y)
4139 return;
4140 SetCursorPos( x_screen_space, y_screen_space );
4142 /* Switch to the software cursor if position diverges from the hardware one. */
4143 GetCursorPos( &pt );
4144 if (x_screen_space != pt.x || y_screen_space != pt.y)
4146 if (device->bCursorVisible) SetCursor( NULL );
4147 DestroyCursor( device->hardwareCursor );
4148 device->hardwareCursor = 0;
4153 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4155 BOOL oldVisible = device->bCursorVisible;
4157 TRACE("device %p, show %#x.\n", device, show);
4160 * When ShowCursor is first called it should make the cursor appear at the OS's last
4161 * known cursor position.
4163 if (show && !oldVisible)
4165 POINT pt;
4166 GetCursorPos(&pt);
4167 device->xScreenSpace = pt.x;
4168 device->yScreenSpace = pt.y;
4171 if (device->hardwareCursor)
4173 device->bCursorVisible = show;
4174 if (show)
4175 SetCursor(device->hardwareCursor);
4176 else
4177 SetCursor(NULL);
4179 else if (device->cursor_texture)
4181 device->bCursorVisible = show;
4184 return oldVisible;
4187 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4189 struct wined3d_resource *resource, *cursor;
4191 TRACE("device %p.\n", device);
4193 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4195 TRACE("Checking resource %p for eviction.\n", resource);
4197 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4199 TRACE("Evicting %p.\n", resource);
4200 resource->resource_ops->resource_unload(resource);
4204 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4205 device_invalidate_state(device, STATE_STREAMSRC);
4208 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4210 struct wined3d_resource *resource, *cursor;
4211 const struct wined3d_gl_info *gl_info;
4212 struct wined3d_context *context;
4213 struct wined3d_shader *shader;
4215 context = context_acquire(device, NULL);
4216 gl_info = context->gl_info;
4218 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4220 TRACE("Unloading resource %p.\n", resource);
4222 resource->resource_ops->resource_unload(resource);
4225 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4227 device->shader_backend->shader_destroy(shader);
4230 if (device->depth_blt_texture)
4232 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4233 device->depth_blt_texture = 0;
4236 device->blitter->free_private(device);
4237 device->shader_backend->shader_free_private(device);
4238 destroy_dummy_textures(device, gl_info);
4240 context_release(context);
4242 while (device->context_count)
4244 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4247 HeapFree(GetProcessHeap(), 0, swapchain->context);
4248 swapchain->context = NULL;
4251 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4253 struct wined3d_context *context;
4254 struct wined3d_surface *target;
4255 HRESULT hr;
4257 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4258 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4260 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4261 return hr;
4264 if (FAILED(hr = device->blitter->alloc_private(device)))
4266 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4267 device->shader_backend->shader_free_private(device);
4268 return hr;
4271 /* Recreate the primary swapchain's context */
4272 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4273 if (!swapchain->context)
4275 ERR("Failed to allocate memory for swapchain context array.\n");
4276 device->blitter->free_private(device);
4277 device->shader_backend->shader_free_private(device);
4278 return E_OUTOFMEMORY;
4281 target = swapchain->back_buffers
4282 ? surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0))
4283 : surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
4284 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4286 WARN("Failed to create context.\n");
4287 device->blitter->free_private(device);
4288 device->shader_backend->shader_free_private(device);
4289 HeapFree(GetProcessHeap(), 0, swapchain->context);
4290 return E_FAIL;
4293 swapchain->context[0] = context;
4294 swapchain->num_contexts = 1;
4295 create_dummy_textures(device, context);
4296 context_release(context);
4298 return WINED3D_OK;
4301 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4302 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4303 wined3d_device_reset_cb callback, BOOL reset_state)
4305 struct wined3d_resource *resource, *cursor;
4306 struct wined3d_swapchain *swapchain;
4307 struct wined3d_display_mode m;
4308 BOOL DisplayModeChanged;
4309 BOOL update_desc = FALSE;
4310 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4311 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4312 HRESULT hr = WINED3D_OK;
4313 unsigned int i;
4315 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4317 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4319 ERR("Failed to get the first implicit swapchain.\n");
4320 return WINED3DERR_INVALIDCALL;
4322 DisplayModeChanged = swapchain->reapply_mode;
4324 if (reset_state)
4326 if (device->logo_texture)
4328 wined3d_texture_decref(device->logo_texture);
4329 device->logo_texture = NULL;
4331 if (device->cursor_texture)
4333 wined3d_texture_decref(device->cursor_texture);
4334 device->cursor_texture = NULL;
4336 state_unbind_resources(&device->state);
4339 if (device->fb.render_targets)
4341 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4343 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4346 wined3d_device_set_depth_stencil_view(device, NULL);
4348 if (device->onscreen_depth_stencil)
4350 wined3d_surface_decref(device->onscreen_depth_stencil);
4351 device->onscreen_depth_stencil = NULL;
4354 if (reset_state)
4356 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4358 TRACE("Enumerating resource %p.\n", resource);
4359 if (FAILED(hr = callback(resource)))
4360 return hr;
4364 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4365 * on an existing gl context, so there's no real need for recreation.
4367 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4369 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4371 TRACE("New params:\n");
4372 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4373 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4374 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4375 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4376 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4377 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4378 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4379 TRACE("device_window %p\n", swapchain_desc->device_window);
4380 TRACE("windowed %#x\n", swapchain_desc->windowed);
4381 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4382 if (swapchain_desc->enable_auto_depth_stencil)
4383 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4384 TRACE("flags %#x\n", swapchain_desc->flags);
4385 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4386 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4387 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4389 /* No special treatment of these parameters. Just store them */
4390 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4391 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4392 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4393 swapchain->desc.flags = swapchain_desc->flags;
4394 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4395 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4396 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4398 /* What to do about these? */
4399 if (swapchain_desc->backbuffer_count
4400 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4401 FIXME("Cannot change the back buffer count yet.\n");
4403 if (swapchain_desc->device_window
4404 && swapchain_desc->device_window != swapchain->desc.device_window)
4406 TRACE("Changing the device window from %p to %p.\n",
4407 swapchain->desc.device_window, swapchain_desc->device_window);
4408 swapchain->desc.device_window = swapchain_desc->device_window;
4409 swapchain->device_window = swapchain_desc->device_window;
4410 wined3d_swapchain_set_window(swapchain, NULL);
4413 if (mode)
4415 DisplayModeChanged = TRUE;
4416 m = *mode;
4418 else if (swapchain_desc->windowed)
4420 m = swapchain->original_mode;
4422 else
4424 m.width = swapchain_desc->backbuffer_width;
4425 m.height = swapchain_desc->backbuffer_height;
4426 m.refresh_rate = swapchain_desc->refresh_rate;
4427 m.format_id = swapchain_desc->backbuffer_format;
4428 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4431 if (!backbuffer_width || !backbuffer_height)
4433 /* The application is requesting that either the swapchain width or
4434 * height be set to the corresponding dimension in the window's
4435 * client rect. */
4437 RECT client_rect;
4439 if (!swapchain_desc->windowed)
4440 return WINED3DERR_INVALIDCALL;
4442 if (!GetClientRect(swapchain->device_window, &client_rect))
4444 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
4445 return WINED3DERR_INVALIDCALL;
4448 if (!backbuffer_width)
4449 backbuffer_width = client_rect.right;
4451 if (!backbuffer_height)
4452 backbuffer_height = client_rect.bottom;
4455 if (backbuffer_width != swapchain->desc.backbuffer_width
4456 || backbuffer_height != swapchain->desc.backbuffer_height)
4458 if (!swapchain_desc->windowed)
4459 DisplayModeChanged = TRUE;
4461 swapchain->desc.backbuffer_width = backbuffer_width;
4462 swapchain->desc.backbuffer_height = backbuffer_height;
4463 update_desc = TRUE;
4466 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
4467 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
4469 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
4470 update_desc = TRUE;
4473 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
4474 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
4476 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
4477 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
4478 update_desc = TRUE;
4481 if (update_desc)
4483 UINT i;
4485 if (FAILED(hr = wined3d_texture_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
4486 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4487 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4488 return hr;
4490 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4492 if (FAILED(hr = wined3d_texture_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
4493 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
4494 swapchain->desc.multisample_type, swapchain->desc.multisample_quality, NULL, 0)))
4495 return hr;
4499 if (device->auto_depth_stencil_view)
4501 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4502 device->auto_depth_stencil_view = NULL;
4504 if (swapchain->desc.enable_auto_depth_stencil)
4506 struct wined3d_resource_desc surface_desc;
4507 struct wined3d_surface *surface;
4509 TRACE("Creating the depth stencil buffer\n");
4511 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
4512 surface_desc.format = swapchain->desc.auto_depth_stencil_format;
4513 surface_desc.multisample_type = swapchain->desc.multisample_type;
4514 surface_desc.multisample_quality = swapchain->desc.multisample_quality;
4515 surface_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4516 surface_desc.pool = WINED3D_POOL_DEFAULT;
4517 surface_desc.width = swapchain->desc.backbuffer_width;
4518 surface_desc.height = swapchain->desc.backbuffer_height;
4519 surface_desc.depth = 1;
4520 surface_desc.size = 0;
4522 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4523 device->device_parent, &surface_desc, &surface)))
4525 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4526 return WINED3DERR_INVALIDCALL;
4529 hr = wined3d_rendertarget_view_create_from_surface(surface,
4530 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4531 wined3d_surface_decref(surface);
4532 if (FAILED(hr))
4534 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4535 return hr;
4538 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4541 if (device->back_buffer_view)
4543 wined3d_rendertarget_view_decref(device->back_buffer_view);
4544 device->back_buffer_view = NULL;
4546 if (swapchain->desc.backbuffer_count && FAILED(hr = wined3d_rendertarget_view_create_from_surface(
4547 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0)),
4548 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4550 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4551 return hr;
4554 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4555 || DisplayModeChanged)
4557 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4559 WARN("Failed to set display mode, hr %#x.\n", hr);
4560 return WINED3DERR_INVALIDCALL;
4563 if (!swapchain_desc->windowed)
4565 if (swapchain->desc.windowed)
4567 HWND focus_window = device->create_parms.focus_window;
4568 if (!focus_window)
4569 focus_window = swapchain_desc->device_window;
4570 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4572 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4573 return hr;
4576 /* switch from windowed to fs */
4577 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4578 swapchain_desc->backbuffer_width,
4579 swapchain_desc->backbuffer_height);
4581 else
4583 /* Fullscreen -> fullscreen mode change */
4584 MoveWindow(swapchain->device_window, 0, 0,
4585 swapchain_desc->backbuffer_width,
4586 swapchain_desc->backbuffer_height,
4587 TRUE);
4589 swapchain->d3d_mode = m;
4591 else if (!swapchain->desc.windowed)
4593 /* Fullscreen -> windowed switch */
4594 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4595 wined3d_device_release_focus_window(device);
4597 swapchain->desc.windowed = swapchain_desc->windowed;
4599 else if (!swapchain_desc->windowed)
4601 DWORD style = device->style;
4602 DWORD exStyle = device->exStyle;
4603 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4604 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4605 * Reset to clear up their mess. Guild Wars also loses the device during that.
4607 device->style = 0;
4608 device->exStyle = 0;
4609 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4610 swapchain_desc->backbuffer_width,
4611 swapchain_desc->backbuffer_height);
4612 device->style = style;
4613 device->exStyle = exStyle;
4616 if (reset_state)
4618 TRACE("Resetting stateblock.\n");
4619 if (device->recording)
4621 wined3d_stateblock_decref(device->recording);
4622 device->recording = NULL;
4624 wined3d_cs_emit_reset_state(device->cs);
4625 state_cleanup(&device->state);
4627 if (device->d3d_initialized)
4628 delete_opengl_contexts(device, swapchain);
4630 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4631 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4632 ERR("Failed to initialize device state, hr %#x.\n", hr);
4633 device->update_state = &device->state;
4635 device_init_swapchain_state(device, swapchain);
4637 else if (device->back_buffer_view)
4639 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4640 struct wined3d_state *state = &device->state;
4642 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4644 /* Note the min_z / max_z is not reset. */
4645 state->viewport.x = 0;
4646 state->viewport.y = 0;
4647 state->viewport.width = view->width;
4648 state->viewport.height = view->height;
4649 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4651 state->scissor_rect.top = 0;
4652 state->scissor_rect.left = 0;
4653 state->scissor_rect.right = view->width;
4654 state->scissor_rect.bottom = view->height;
4655 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4658 swapchain_update_render_to_fbo(swapchain);
4659 swapchain_update_draw_bindings(swapchain);
4661 if (reset_state && device->d3d_initialized)
4662 hr = create_primary_opengl_context(device, swapchain);
4664 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4665 * first use
4667 return hr;
4670 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4672 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4674 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4676 return WINED3D_OK;
4680 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4681 struct wined3d_device_creation_parameters *parameters)
4683 TRACE("device %p, parameters %p.\n", device, parameters);
4685 *parameters = device->create_parms;
4688 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4689 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4691 struct wined3d_swapchain *swapchain;
4693 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4694 device, swapchain_idx, flags, ramp);
4696 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4697 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4700 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4701 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4703 struct wined3d_swapchain *swapchain;
4705 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4706 device, swapchain_idx, ramp);
4708 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4709 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4712 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4714 TRACE("device %p, resource %p.\n", device, resource);
4716 list_add_head(&device->resources, &resource->resource_list_entry);
4719 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4721 TRACE("device %p, resource %p.\n", device, resource);
4723 list_remove(&resource->resource_list_entry);
4726 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4728 enum wined3d_resource_type type = resource->type;
4729 unsigned int i;
4731 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4733 context_resource_released(device, resource, type);
4735 switch (type)
4737 case WINED3D_RTYPE_SURFACE:
4739 struct wined3d_surface *surface = surface_from_resource(resource);
4741 if (!device->d3d_initialized) break;
4743 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4745 if (wined3d_rendertarget_view_get_surface(device->fb.render_targets[i]) == surface)
4747 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4748 device->fb.render_targets[i] = NULL;
4752 if (wined3d_rendertarget_view_get_surface(device->fb.depth_stencil) == surface)
4754 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4755 device->fb.depth_stencil = NULL;
4758 break;
4760 case WINED3D_RTYPE_TEXTURE:
4761 case WINED3D_RTYPE_CUBE_TEXTURE:
4762 case WINED3D_RTYPE_VOLUME_TEXTURE:
4763 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4765 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4767 if (device->state.textures[i] == texture)
4769 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4770 device->state.textures[i] = NULL;
4773 if (device->recording && device->update_state->textures[i] == texture)
4775 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4776 texture, device->recording, i);
4777 device->update_state->textures[i] = NULL;
4780 break;
4782 case WINED3D_RTYPE_BUFFER:
4784 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4786 for (i = 0; i < MAX_STREAMS; ++i)
4788 if (device->state.streams[i].buffer == buffer)
4790 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4791 device->state.streams[i].buffer = NULL;
4794 if (device->recording && device->update_state->streams[i].buffer == buffer)
4796 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
4797 buffer, device->recording, i);
4798 device->update_state->streams[i].buffer = NULL;
4802 if (device->state.index_buffer == buffer)
4804 ERR("Buffer %p is still in use as index buffer.\n", buffer);
4805 device->state.index_buffer = NULL;
4808 if (device->recording && device->update_state->index_buffer == buffer)
4810 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
4811 buffer, device->recording);
4812 device->update_state->index_buffer = NULL;
4815 break;
4817 default:
4818 break;
4821 /* Remove the resource from the resourceStore */
4822 device_resource_remove(device, resource);
4824 TRACE("Resource released.\n");
4827 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
4829 struct wined3d_resource *resource;
4831 TRACE("device %p, dc %p.\n", device, dc);
4833 if (!dc)
4834 return NULL;
4836 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
4838 if (resource->type == WINED3D_RTYPE_SURFACE)
4840 struct wined3d_surface *s = surface_from_resource(resource);
4842 if (s->hDC == dc)
4844 TRACE("Found surface %p for dc %p.\n", s, dc);
4845 return s;
4850 return NULL;
4853 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
4854 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
4855 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
4857 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
4858 const struct fragment_pipeline *fragment_pipeline;
4859 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
4860 unsigned int i;
4861 HRESULT hr;
4863 device->ref = 1;
4864 device->wined3d = wined3d;
4865 wined3d_incref(device->wined3d);
4866 device->adapter = wined3d->adapter_count ? adapter : NULL;
4867 device->device_parent = device_parent;
4868 list_init(&device->resources);
4869 list_init(&device->shaders);
4870 device->surface_alignment = surface_alignment;
4872 /* Save the creation parameters. */
4873 device->create_parms.adapter_idx = adapter_idx;
4874 device->create_parms.device_type = device_type;
4875 device->create_parms.focus_window = focus_window;
4876 device->create_parms.flags = flags;
4878 device->shader_backend = adapter->shader_backend;
4880 vertex_pipeline = adapter->vertex_pipe;
4882 fragment_pipeline = adapter->fragment_pipe;
4884 if (vertex_pipeline->vp_states && fragment_pipeline->states
4885 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
4886 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
4887 fragment_pipeline, misc_state_template)))
4889 ERR("Failed to compile state table, hr %#x.\n", hr);
4890 wined3d_decref(device->wined3d);
4891 return hr;
4894 device->blitter = adapter->blitter;
4896 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
4897 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4899 ERR("Failed to initialize device state, hr %#x.\n", hr);
4900 goto err;
4902 device->update_state = &device->state;
4904 if (!(device->cs = wined3d_cs_create(device)))
4906 WARN("Failed to create command stream.\n");
4907 state_cleanup(&device->state);
4908 hr = E_FAIL;
4909 goto err;
4912 return WINED3D_OK;
4914 err:
4915 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
4917 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
4919 wined3d_decref(device->wined3d);
4920 return hr;
4924 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
4926 DWORD rep = device->StateTable[state].representative;
4927 struct wined3d_context *context;
4928 DWORD idx;
4929 BYTE shift;
4930 UINT i;
4932 for (i = 0; i < device->context_count; ++i)
4934 context = device->contexts[i];
4935 if(isStateDirty(context, rep)) continue;
4937 context->dirtyArray[context->numDirtyEntries++] = rep;
4938 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
4939 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
4940 context->isStateDirty[idx] |= (1 << shift);
4944 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
4945 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
4947 if (device->filter_messages && message != WM_DISPLAYCHANGE)
4949 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
4950 window, message, wparam, lparam);
4951 if (unicode)
4952 return DefWindowProcW(window, message, wparam, lparam);
4953 else
4954 return DefWindowProcA(window, message, wparam, lparam);
4957 if (message == WM_DESTROY)
4959 TRACE("unregister window %p.\n", window);
4960 wined3d_unregister_window(window);
4962 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
4963 ERR("Window %p is not the focus window for device %p.\n", window, device);
4965 else if (message == WM_DISPLAYCHANGE)
4967 device->device_parent->ops->mode_changed(device->device_parent);
4969 else if (message == WM_ACTIVATEAPP)
4971 UINT i;
4973 for (i = 0; i < device->swapchain_count; i++)
4974 wined3d_swapchain_activate(device->swapchains[i], wparam);
4976 device->device_parent->ops->activate(device->device_parent, wparam);
4979 if (unicode)
4980 return CallWindowProcW(proc, window, message, wparam, lparam);
4981 else
4982 return CallWindowProcA(proc, window, message, wparam, lparam);