wined3d: Get resource info from the rendertarget view in device_clear_render_targets().
[wine.git] / dlls / wined3d / device.c
blob2305b2c56f1d009727a4a3d159ddd3b8d39cea52
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_texture_decref(device->onscreen_depth_stencil->container);
213 device->onscreen_depth_stencil = depth_stencil;
214 wined3d_texture_incref(device->onscreen_depth_stencil->container);
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_color corrected_color;
298 struct wined3d_context *context;
299 GLbitfield clear_mask = 0;
300 BOOL render_offscreen;
301 unsigned int i;
302 RECT ds_rect;
304 context = context_acquire(device, target);
305 if (!context->valid)
307 context_release(context);
308 WARN("Invalid context, skipping clear.\n");
309 return;
311 gl_info = context->gl_info;
313 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
314 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
315 * for the cleared parts, and the untouched parts.
317 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
318 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
319 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
320 * checking all this if the dest surface is in the drawable anyway. */
321 for (i = 0; i < rt_count; ++i)
323 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
324 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(rtv);
326 if (rt && rtv->format->id != WINED3DFMT_NULL)
328 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
329 surface_load_location(rt, context, rtv->resource->draw_binding);
330 else
331 wined3d_surface_prepare(rt, context, rtv->resource->draw_binding);
335 if (target)
337 render_offscreen = context->render_offscreen;
338 surface_get_drawable_size(target, context, &drawable_width, &drawable_height);
340 else
342 render_offscreen = TRUE;
343 drawable_width = depth_stencil->pow2Width;
344 drawable_height = depth_stencil->pow2Height;
347 if (depth_stencil && render_offscreen)
348 wined3d_surface_prepare(depth_stencil, context, depth_stencil->container->resource.draw_binding);
350 if (flags & WINED3DCLEAR_ZBUFFER)
352 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
354 if (!render_offscreen && depth_stencil != device->onscreen_depth_stencil)
355 device_switch_onscreen_ds(device, context, depth_stencil);
356 prepare_ds_clear(depth_stencil, context, location,
357 draw_rect, rect_count, clear_rect, &ds_rect);
360 if (!context_apply_clear_state(context, device, rt_count, fb))
362 context_release(context);
363 WARN("Failed to apply clear state, skipping clear.\n");
364 return;
367 /* Only set the values up once, as they are not changing. */
368 if (flags & WINED3DCLEAR_STENCIL)
370 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
372 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
373 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
375 gl_info->gl_ops.gl.p_glStencilMask(~0U);
376 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
377 gl_info->gl_ops.gl.p_glClearStencil(stencil);
378 checkGLcall("glClearStencil");
379 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
382 if (flags & WINED3DCLEAR_ZBUFFER)
384 DWORD location = render_offscreen ? fb->depth_stencil->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
386 surface_modify_ds_location(depth_stencil, location, ds_rect.right, ds_rect.bottom);
388 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
389 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
390 gl_info->gl_ops.gl.p_glClearDepth(depth);
391 checkGLcall("glClearDepth");
392 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
395 if (flags & WINED3DCLEAR_TARGET)
397 for (i = 0; i < rt_count; ++i)
399 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
400 struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(rtv);
402 if (rt)
404 surface_validate_location(rt, rtv->resource->draw_binding);
405 surface_invalidate_location(rt, ~rtv->resource->draw_binding);
409 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, &device->state, fb))
411 if (rt_count > 1)
412 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
413 "support, this might cause graphical issues.\n");
415 corrected_color.r = color->r < wined3d_srgb_const1[0]
416 ? color->r * wined3d_srgb_const0[3]
417 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
418 - wined3d_srgb_const0[2];
419 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
420 corrected_color.g = color->g < wined3d_srgb_const1[0]
421 ? color->g * wined3d_srgb_const0[3]
422 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
423 - wined3d_srgb_const0[2];
424 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
425 corrected_color.b = color->b < wined3d_srgb_const1[0]
426 ? color->b * wined3d_srgb_const0[3]
427 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
428 - wined3d_srgb_const0[2];
429 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
430 color = &corrected_color;
433 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
434 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
435 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
436 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
437 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
438 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
439 checkGLcall("glClearColor");
440 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
443 if (!clear_rect)
445 if (render_offscreen)
447 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
448 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
450 else
452 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
453 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
455 checkGLcall("glScissor");
456 gl_info->gl_ops.gl.p_glClear(clear_mask);
457 checkGLcall("glClear");
459 else
461 RECT current_rect;
463 /* Now process each rect in turn. */
464 for (i = 0; i < rect_count; ++i)
466 /* Note that GL uses lower left, width/height. */
467 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
469 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
470 wine_dbgstr_rect(&clear_rect[i]),
471 wine_dbgstr_rect(&current_rect));
473 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
474 * The rectangle is not cleared, no error is returned, but further rectangles are
475 * still cleared if they are valid. */
476 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
478 TRACE("Rectangle with negative dimensions, ignoring.\n");
479 continue;
482 if (render_offscreen)
484 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
485 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
487 else
489 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
490 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
492 checkGLcall("glScissor");
494 gl_info->gl_ops.gl.p_glClear(clear_mask);
495 checkGLcall("glClear");
499 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
500 && target->container->swapchain && target->container->swapchain->front_buffer == target->container))
501 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
503 context_release(context);
506 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
508 ULONG refcount = InterlockedIncrement(&device->ref);
510 TRACE("%p increasing refcount to %u.\n", device, refcount);
512 return refcount;
515 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
517 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
519 ERR("Leftover sampler %p.\n", sampler);
522 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
524 ULONG refcount = InterlockedDecrement(&device->ref);
526 TRACE("%p decreasing refcount to %u.\n", device, refcount);
528 if (!refcount)
530 UINT i;
532 wined3d_cs_destroy(device->cs);
534 if (device->recording && wined3d_stateblock_decref(device->recording))
535 FIXME("Something's still holding the recording stateblock.\n");
536 device->recording = NULL;
538 state_cleanup(&device->state);
540 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
542 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
543 device->multistate_funcs[i] = NULL;
546 if (!list_empty(&device->resources))
548 struct wined3d_resource *resource;
550 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
552 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
554 FIXME("Leftover resource %p with type %s (%#x).\n",
555 resource, debug_d3dresourcetype(resource->type), resource->type);
559 if (device->contexts)
560 ERR("Context array not freed!\n");
561 if (device->hardwareCursor)
562 DestroyCursor(device->hardwareCursor);
563 device->hardwareCursor = 0;
565 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
567 wined3d_decref(device->wined3d);
568 device->wined3d = NULL;
569 HeapFree(GetProcessHeap(), 0, device);
570 TRACE("Freed device %p.\n", device);
573 return refcount;
576 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
578 TRACE("device %p.\n", device);
580 return device->swapchain_count;
583 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
585 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
587 if (swapchain_idx >= device->swapchain_count)
589 WARN("swapchain_idx %u >= swapchain_count %u.\n",
590 swapchain_idx, device->swapchain_count);
591 return NULL;
594 return device->swapchains[swapchain_idx];
597 static void device_load_logo(struct wined3d_device *device, const char *filename)
599 struct wined3d_color_key color_key;
600 struct wined3d_resource_desc desc;
601 HBITMAP hbm;
602 BITMAP bm;
603 HRESULT hr;
604 HDC dcb = NULL, dcs = NULL;
606 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
607 if(hbm)
609 GetObjectA(hbm, sizeof(BITMAP), &bm);
610 dcb = CreateCompatibleDC(NULL);
611 if(!dcb) goto out;
612 SelectObject(dcb, hbm);
614 else
616 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
617 * couldn't be loaded
619 memset(&bm, 0, sizeof(bm));
620 bm.bmWidth = 32;
621 bm.bmHeight = 32;
624 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
625 desc.format = WINED3DFMT_B5G6R5_UNORM;
626 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
627 desc.multisample_quality = 0;
628 desc.usage = WINED3DUSAGE_DYNAMIC;
629 desc.pool = WINED3D_POOL_DEFAULT;
630 desc.width = bm.bmWidth;
631 desc.height = bm.bmHeight;
632 desc.depth = 1;
633 desc.size = 0;
634 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
635 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
637 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
638 goto out;
641 if (dcb)
643 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
644 goto out;
645 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
646 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
648 color_key.color_space_low_value = 0;
649 color_key.color_space_high_value = 0;
650 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
652 else
654 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
655 const RECT rect = {0, 0, desc.width, desc.height};
656 struct wined3d_surface *surface;
658 /* Fill the surface with a white color to show that wined3d is there */
659 surface = surface_from_resource(wined3d_texture_get_sub_resource(device->logo_texture, 0));
660 surface_color_fill(surface, &rect, &c);
663 out:
664 if (dcb) DeleteDC(dcb);
665 if (hbm) DeleteObject(hbm);
668 /* Context activation is done by the caller. */
669 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
671 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
672 unsigned int i, j, count;
673 /* Under DirectX you can sample even if no texture is bound, whereas
674 * OpenGL will only allow that when a valid texture is bound.
675 * We emulate this by creating dummy textures and binding them
676 * to each texture stage when the currently set D3D texture is NULL. */
678 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
679 for (i = 0; i < count; ++i)
681 static const DWORD color = 0x000000ff;
683 /* Make appropriate texture active */
684 context_active_texture(context, gl_info, i);
686 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
687 checkGLcall("glGenTextures");
688 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
690 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
691 checkGLcall("glBindTexture");
693 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
694 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
695 checkGLcall("glTexImage2D");
697 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
699 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
700 checkGLcall("glGenTextures");
701 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
703 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
704 checkGLcall("glBindTexture");
706 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
707 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
708 checkGLcall("glTexImage2D");
711 if (gl_info->supported[EXT_TEXTURE3D])
713 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
714 checkGLcall("glGenTextures");
715 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
717 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
718 checkGLcall("glBindTexture");
720 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
721 checkGLcall("glTexImage3D");
724 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
726 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
727 checkGLcall("glGenTextures");
728 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
730 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
731 checkGLcall("glBindTexture");
733 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
735 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
736 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
737 checkGLcall("glTexImage2D");
743 /* Context activation is done by the caller. */
744 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
746 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
748 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
750 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
751 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
754 if (gl_info->supported[EXT_TEXTURE3D])
756 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
757 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
760 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
762 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
763 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
766 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
767 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
769 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
770 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
771 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
772 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
775 /* Context activation is done by the caller. */
776 static void create_default_sampler(struct wined3d_device *device)
778 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
781 * In SM4+ shaders there is a separation between resources and samplers. Some of shader
782 * instructions allow to access resources without using samplers.
783 * In GLSL resources are always accessed through sampler or image variables. The default
784 * sampler object is used to emulate the direct resource access when there is no sampler state
785 * to use.
788 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
790 GL_EXTCALL(glGenSamplers(1, &device->default_sampler));
791 checkGLcall("glGenSamplers");
792 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
793 GL_EXTCALL(glSamplerParameteri(device->default_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST));
794 checkGLcall("glSamplerParamteri");
796 else
798 device->default_sampler = 0;
802 /* Context activation is done by the caller. */
803 static void destroy_default_sampler(struct wined3d_device *device)
805 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
807 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
809 GL_EXTCALL(glDeleteSamplers(1, &device->default_sampler));
810 checkGLcall("glDeleteSamplers");
813 device->default_sampler = 0;
816 static LONG fullscreen_style(LONG style)
818 /* Make sure the window is managed, otherwise we won't get keyboard input. */
819 style |= WS_POPUP | WS_SYSMENU;
820 style &= ~(WS_CAPTION | WS_THICKFRAME);
822 return style;
825 static LONG fullscreen_exstyle(LONG exstyle)
827 /* Filter out window decorations. */
828 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
830 return exstyle;
833 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
835 BOOL filter_messages;
836 LONG style, exstyle;
838 TRACE("Setting up window %p for fullscreen mode.\n", window);
840 if (device->style || device->exStyle)
842 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
843 window, device->style, device->exStyle);
846 device->style = GetWindowLongW(window, GWL_STYLE);
847 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
849 style = fullscreen_style(device->style);
850 exstyle = fullscreen_exstyle(device->exStyle);
852 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
853 device->style, device->exStyle, style, exstyle);
855 filter_messages = device->filter_messages;
856 device->filter_messages = TRUE;
858 SetWindowLongW(window, GWL_STYLE, style);
859 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
860 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
862 device->filter_messages = filter_messages;
865 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
867 BOOL filter_messages;
868 LONG style, exstyle;
870 if (!device->style && !device->exStyle) return;
872 style = GetWindowLongW(window, GWL_STYLE);
873 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
875 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
876 * application, and we want to ignore them in the test below, since it's
877 * not the application's fault that they changed. Additionally, we want to
878 * preserve the current status of these flags (i.e. don't restore them) to
879 * more closely emulate the behavior of Direct3D, which leaves these flags
880 * alone when returning to windowed mode. */
881 device->style ^= (device->style ^ style) & WS_VISIBLE;
882 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
884 TRACE("Restoring window style of window %p to %08x, %08x.\n",
885 window, device->style, device->exStyle);
887 filter_messages = device->filter_messages;
888 device->filter_messages = TRUE;
890 /* Only restore the style if the application didn't modify it during the
891 * fullscreen phase. Some applications change it before calling Reset()
892 * when switching between windowed and fullscreen modes (HL2), some
893 * depend on the original style (Eve Online). */
894 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
896 SetWindowLongW(window, GWL_STYLE, device->style);
897 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
899 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
901 device->filter_messages = filter_messages;
903 /* Delete the old values. */
904 device->style = 0;
905 device->exStyle = 0;
908 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
910 TRACE("device %p, window %p.\n", device, window);
912 if (!wined3d_register_window(window, device))
914 ERR("Failed to register window %p.\n", window);
915 return E_FAIL;
918 InterlockedExchangePointer((void **)&device->focus_window, window);
919 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
921 return WINED3D_OK;
924 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
926 TRACE("device %p.\n", device);
928 if (device->focus_window) wined3d_unregister_window(device->focus_window);
929 InterlockedExchangePointer((void **)&device->focus_window, NULL);
932 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
934 BOOL ds_enable = !!swapchain->desc.enable_auto_depth_stencil;
935 unsigned int i;
937 if (device->fb.render_targets)
939 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
941 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
943 if (device->back_buffer_view)
944 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
947 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
948 wined3d_device_set_render_state(device, WINED3D_RS_ZENABLE, ds_enable);
951 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
952 struct wined3d_swapchain_desc *swapchain_desc)
954 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
955 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
956 struct wined3d_swapchain *swapchain = NULL;
957 struct wined3d_context *context;
958 DWORD clear_flags = 0;
959 HRESULT hr;
961 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
963 if (device->d3d_initialized)
964 return WINED3DERR_INVALIDCALL;
965 if (device->wined3d->flags & WINED3D_NO3D)
966 return WINED3DERR_INVALIDCALL;
968 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
969 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
971 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
972 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
974 TRACE("Shader private data couldn't be allocated\n");
975 goto err_out;
977 if (FAILED(hr = device->blitter->alloc_private(device)))
979 TRACE("Blitter private data couldn't be allocated\n");
980 goto err_out;
983 /* Setup the implicit swapchain. This also initializes a context. */
984 TRACE("Creating implicit swapchain\n");
985 hr = device->device_parent->ops->create_swapchain(device->device_parent,
986 swapchain_desc, &swapchain);
987 if (FAILED(hr))
989 WARN("Failed to create implicit swapchain\n");
990 goto err_out;
993 if (swapchain_desc->backbuffer_count)
995 struct wined3d_rendertarget_view_desc view_desc;
997 view_desc.format_id = swapchain_desc->backbuffer_format;
998 view_desc.u.texture.level_idx = 0;
999 view_desc.u.texture.layer_idx = 0;
1000 view_desc.u.texture.layer_count = 1;
1001 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
1002 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1004 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1005 goto err_out;
1009 device->swapchain_count = 1;
1010 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1011 if (!device->swapchains)
1013 ERR("Out of memory!\n");
1014 goto err_out;
1016 device->swapchains[0] = swapchain;
1017 device_init_swapchain_state(device, swapchain);
1019 context = context_acquire(device,
1020 surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0)));
1022 create_dummy_textures(device, context);
1023 create_default_sampler(device);
1025 device->contexts[0]->last_was_rhw = 0;
1027 TRACE("All defaults now set up, leaving 3D init.\n");
1029 context_release(context);
1031 /* Clear the screen */
1032 if (swapchain->back_buffers && swapchain->back_buffers[0])
1033 clear_flags |= WINED3DCLEAR_TARGET;
1034 if (swapchain_desc->enable_auto_depth_stencil)
1035 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1036 if (clear_flags)
1037 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1039 device->d3d_initialized = TRUE;
1041 if (wined3d_settings.logo)
1042 device_load_logo(device, wined3d_settings.logo);
1043 return WINED3D_OK;
1045 err_out:
1046 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1047 HeapFree(GetProcessHeap(), 0, device->swapchains);
1048 device->swapchain_count = 0;
1049 if (device->back_buffer_view)
1050 wined3d_rendertarget_view_decref(device->back_buffer_view);
1051 if (swapchain)
1052 wined3d_swapchain_decref(swapchain);
1053 if (device->blit_priv)
1054 device->blitter->free_private(device);
1055 if (device->shader_priv)
1056 device->shader_backend->shader_free_private(device);
1058 return hr;
1061 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1062 struct wined3d_swapchain_desc *swapchain_desc)
1064 struct wined3d_swapchain *swapchain = NULL;
1065 HRESULT hr;
1067 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1069 /* Setup the implicit swapchain */
1070 TRACE("Creating implicit swapchain\n");
1071 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1072 swapchain_desc, &swapchain);
1073 if (FAILED(hr))
1075 WARN("Failed to create implicit swapchain\n");
1076 goto err_out;
1079 device->swapchain_count = 1;
1080 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1081 if (!device->swapchains)
1083 ERR("Out of memory!\n");
1084 goto err_out;
1086 device->swapchains[0] = swapchain;
1087 return WINED3D_OK;
1089 err_out:
1090 wined3d_swapchain_decref(swapchain);
1091 return hr;
1094 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1096 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1098 wined3d_sampler_decref(sampler);
1101 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1103 struct wined3d_resource *resource, *cursor;
1104 const struct wined3d_gl_info *gl_info;
1105 struct wined3d_context *context;
1106 struct wined3d_surface *surface;
1107 UINT i;
1109 TRACE("device %p.\n", device);
1111 if (!device->d3d_initialized)
1112 return WINED3DERR_INVALIDCALL;
1114 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1115 * it was created. Thus make sure a context is active for the glDelete* calls
1117 context = context_acquire(device, NULL);
1118 gl_info = context->gl_info;
1120 if (device->logo_texture)
1121 wined3d_texture_decref(device->logo_texture);
1122 if (device->cursor_texture)
1123 wined3d_texture_decref(device->cursor_texture);
1125 state_unbind_resources(&device->state);
1127 /* Unload resources */
1128 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1130 TRACE("Unloading resource %p.\n", resource);
1132 resource->resource_ops->resource_unload(resource);
1135 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1137 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1138 * private data, it might contain opengl pointers
1140 if (device->depth_blt_texture)
1142 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1143 device->depth_blt_texture = 0;
1146 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1147 device->blitter->free_private(device);
1148 device->shader_backend->shader_free_private(device);
1149 destroy_dummy_textures(device, gl_info);
1150 destroy_default_sampler(device);
1152 /* Release the context again as soon as possible. In particular,
1153 * releasing the render target views below may release the last reference
1154 * to the swapchain associated with this context, which in turn will
1155 * destroy the context. */
1156 context_release(context);
1158 /* Release the buffers (with sanity checks)*/
1159 if (device->onscreen_depth_stencil)
1161 surface = device->onscreen_depth_stencil;
1162 device->onscreen_depth_stencil = NULL;
1163 wined3d_texture_decref(surface->container);
1166 if (device->fb.depth_stencil)
1168 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1170 TRACE("Releasing depth/stencil view %p.\n", view);
1172 device->fb.depth_stencil = NULL;
1173 wined3d_rendertarget_view_decref(view);
1176 if (device->auto_depth_stencil_view)
1178 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1180 device->auto_depth_stencil_view = NULL;
1181 if (wined3d_rendertarget_view_decref(view))
1182 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1185 for (i = 0; i < gl_info->limits.buffers; ++i)
1187 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1189 if (device->back_buffer_view)
1191 wined3d_rendertarget_view_decref(device->back_buffer_view);
1192 device->back_buffer_view = NULL;
1195 for (i = 0; i < device->swapchain_count; ++i)
1197 TRACE("Releasing the implicit swapchain %u.\n", i);
1198 if (wined3d_swapchain_decref(device->swapchains[i]))
1199 FIXME("Something's still holding the implicit swapchain.\n");
1202 HeapFree(GetProcessHeap(), 0, device->swapchains);
1203 device->swapchains = NULL;
1204 device->swapchain_count = 0;
1206 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1207 device->fb.render_targets = NULL;
1209 device->d3d_initialized = FALSE;
1211 return WINED3D_OK;
1214 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1216 unsigned int i;
1218 for (i = 0; i < device->swapchain_count; ++i)
1220 TRACE("Releasing the implicit swapchain %u.\n", i);
1221 if (wined3d_swapchain_decref(device->swapchains[i]))
1222 FIXME("Something's still holding the implicit swapchain.\n");
1225 HeapFree(GetProcessHeap(), 0, device->swapchains);
1226 device->swapchains = NULL;
1227 device->swapchain_count = 0;
1228 return WINED3D_OK;
1231 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1232 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1233 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1235 * There is no way to deactivate thread safety once it is enabled.
1237 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1239 TRACE("device %p.\n", device);
1241 /* For now just store the flag (needed in case of ddraw). */
1242 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1245 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1247 TRACE("device %p.\n", device);
1249 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1250 wine_dbgstr_longlong(device->adapter->vram_bytes),
1251 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1252 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1254 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1257 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1258 struct wined3d_buffer *buffer, UINT offset)
1260 struct wined3d_stream_output *stream;
1261 struct wined3d_buffer *prev_buffer;
1263 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1265 if (idx >= MAX_STREAM_OUT)
1267 WARN("Invalid stream output %u.\n", idx);
1268 return;
1271 stream = &device->update_state->stream_output[idx];
1272 prev_buffer = stream->buffer;
1274 if (buffer)
1275 wined3d_buffer_incref(buffer);
1276 stream->buffer = buffer;
1277 stream->offset = offset;
1278 if (!device->recording)
1279 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1280 if (prev_buffer)
1281 wined3d_buffer_decref(prev_buffer);
1284 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1285 UINT idx, UINT *offset)
1287 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1289 if (idx >= MAX_STREAM_OUT)
1291 WARN("Invalid stream output %u.\n", idx);
1292 return NULL;
1295 if (offset)
1296 *offset = device->state.stream_output[idx].offset;
1297 return device->state.stream_output[idx].buffer;
1300 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1301 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1303 struct wined3d_stream_state *stream;
1304 struct wined3d_buffer *prev_buffer;
1306 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1307 device, stream_idx, buffer, offset, stride);
1309 if (stream_idx >= MAX_STREAMS)
1311 WARN("Stream index %u out of range.\n", stream_idx);
1312 return WINED3DERR_INVALIDCALL;
1314 else if (offset & 0x3)
1316 WARN("Offset %u is not 4 byte aligned.\n", offset);
1317 return WINED3DERR_INVALIDCALL;
1320 stream = &device->update_state->streams[stream_idx];
1321 prev_buffer = stream->buffer;
1323 if (device->recording)
1324 device->recording->changed.streamSource |= 1u << stream_idx;
1326 if (prev_buffer == buffer
1327 && stream->stride == stride
1328 && stream->offset == offset)
1330 TRACE("Application is setting the old values over, nothing to do.\n");
1331 return WINED3D_OK;
1334 stream->buffer = buffer;
1335 if (buffer)
1337 stream->stride = stride;
1338 stream->offset = offset;
1339 wined3d_buffer_incref(buffer);
1342 if (!device->recording)
1343 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1344 if (prev_buffer)
1345 wined3d_buffer_decref(prev_buffer);
1347 return WINED3D_OK;
1350 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1351 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1353 const struct wined3d_stream_state *stream;
1355 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1356 device, stream_idx, buffer, offset, stride);
1358 if (stream_idx >= MAX_STREAMS)
1360 WARN("Stream index %u out of range.\n", stream_idx);
1361 return WINED3DERR_INVALIDCALL;
1364 stream = &device->state.streams[stream_idx];
1365 *buffer = stream->buffer;
1366 if (offset)
1367 *offset = stream->offset;
1368 *stride = stream->stride;
1370 return WINED3D_OK;
1373 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1375 struct wined3d_stream_state *stream;
1376 UINT old_flags, old_freq;
1378 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1380 /* Verify input. At least in d3d9 this is invalid. */
1381 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1383 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1384 return WINED3DERR_INVALIDCALL;
1386 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1388 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1389 return WINED3DERR_INVALIDCALL;
1391 if (!divider)
1393 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1394 return WINED3DERR_INVALIDCALL;
1397 stream = &device->update_state->streams[stream_idx];
1398 old_flags = stream->flags;
1399 old_freq = stream->frequency;
1401 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1402 stream->frequency = divider & 0x7fffff;
1404 if (device->recording)
1405 device->recording->changed.streamFreq |= 1u << stream_idx;
1406 else if (stream->frequency != old_freq || stream->flags != old_flags)
1407 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1409 return WINED3D_OK;
1412 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1413 UINT stream_idx, UINT *divider)
1415 const struct wined3d_stream_state *stream;
1417 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1419 stream = &device->state.streams[stream_idx];
1420 *divider = stream->flags | stream->frequency;
1422 TRACE("Returning %#x.\n", *divider);
1424 return WINED3D_OK;
1427 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1428 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1430 TRACE("device %p, state %s, matrix %p.\n",
1431 device, debug_d3dtstype(d3dts), matrix);
1432 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1433 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1434 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1435 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1437 /* Handle recording of state blocks. */
1438 if (device->recording)
1440 TRACE("Recording... not performing anything.\n");
1441 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1442 device->update_state->transforms[d3dts] = *matrix;
1443 return;
1446 /* If the new matrix is the same as the current one,
1447 * we cut off any further processing. this seems to be a reasonable
1448 * optimization because as was noticed, some apps (warcraft3 for example)
1449 * tend towards setting the same matrix repeatedly for some reason.
1451 * From here on we assume that the new matrix is different, wherever it matters. */
1452 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1454 TRACE("The application is setting the same matrix over again.\n");
1455 return;
1458 device->state.transforms[d3dts] = *matrix;
1459 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1462 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1463 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1465 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1467 *matrix = device->state.transforms[state];
1470 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1471 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1473 const struct wined3d_matrix *mat;
1474 struct wined3d_matrix temp;
1476 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1478 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1479 * below means it will be recorded in a state block change, but it
1480 * works regardless where it is recorded.
1481 * If this is found to be wrong, change to StateBlock. */
1482 if (state > HIGHEST_TRANSFORMSTATE)
1484 WARN("Unhandled transform state %#x.\n", state);
1485 return;
1488 mat = &device->update_state->transforms[state];
1489 multiply_matrix(&temp, mat, matrix);
1491 /* Apply change via set transform - will reapply to eg. lights this way. */
1492 wined3d_device_set_transform(device, state, &temp);
1495 /* Note lights are real special cases. Although the device caps state only
1496 * e.g. 8 are supported, you can reference any indexes you want as long as
1497 * that number max are enabled at any one point in time. Therefore since the
1498 * indices can be anything, we need a hashmap of them. However, this causes
1499 * stateblock problems. When capturing the state block, I duplicate the
1500 * hashmap, but when recording, just build a chain pretty much of commands to
1501 * be replayed. */
1502 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1503 UINT light_idx, const struct wined3d_light *light)
1505 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1506 struct wined3d_light_info *object = NULL;
1507 struct list *e;
1508 float rho;
1510 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1512 /* Check the parameter range. Need for speed most wanted sets junk lights
1513 * which confuse the GL driver. */
1514 if (!light)
1515 return WINED3DERR_INVALIDCALL;
1517 switch (light->type)
1519 case WINED3D_LIGHT_POINT:
1520 case WINED3D_LIGHT_SPOT:
1521 case WINED3D_LIGHT_GLSPOT:
1522 /* Incorrect attenuation values can cause the gl driver to crash.
1523 * Happens with Need for speed most wanted. */
1524 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1526 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1527 return WINED3DERR_INVALIDCALL;
1529 break;
1531 case WINED3D_LIGHT_DIRECTIONAL:
1532 case WINED3D_LIGHT_PARALLELPOINT:
1533 /* Ignores attenuation */
1534 break;
1536 default:
1537 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1538 return WINED3DERR_INVALIDCALL;
1541 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1543 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1544 if (object->OriginalIndex == light_idx)
1545 break;
1546 object = NULL;
1549 if (!object)
1551 TRACE("Adding new light\n");
1552 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1553 if (!object)
1554 return E_OUTOFMEMORY;
1556 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1557 object->glIndex = -1;
1558 object->OriginalIndex = light_idx;
1561 /* Initialize the object. */
1562 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1563 light_idx, light->type,
1564 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1565 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1566 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1567 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1568 light->direction.x, light->direction.y, light->direction.z);
1569 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1570 light->range, light->falloff, light->theta, light->phi);
1572 /* Update the live definitions if the light is currently assigned a glIndex. */
1573 if (object->glIndex != -1 && !device->recording)
1575 if (object->OriginalParms.type != light->type)
1576 device_invalidate_state(device, STATE_LIGHT_TYPE);
1577 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1580 /* Save away the information. */
1581 object->OriginalParms = *light;
1583 switch (light->type)
1585 case WINED3D_LIGHT_POINT:
1586 /* Position */
1587 object->position.x = light->position.x;
1588 object->position.y = light->position.y;
1589 object->position.z = light->position.z;
1590 object->position.w = 1.0f;
1591 object->cutoff = 180.0f;
1592 /* FIXME: Range */
1593 break;
1595 case WINED3D_LIGHT_DIRECTIONAL:
1596 /* Direction */
1597 object->direction.x = -light->direction.x;
1598 object->direction.y = -light->direction.y;
1599 object->direction.z = -light->direction.z;
1600 object->direction.w = 0.0f;
1601 object->exponent = 0.0f;
1602 object->cutoff = 180.0f;
1603 break;
1605 case WINED3D_LIGHT_SPOT:
1606 /* Position */
1607 object->position.x = light->position.x;
1608 object->position.y = light->position.y;
1609 object->position.z = light->position.z;
1610 object->position.w = 1.0f;
1612 /* Direction */
1613 object->direction.x = light->direction.x;
1614 object->direction.y = light->direction.y;
1615 object->direction.z = light->direction.z;
1616 object->direction.w = 0.0f;
1618 /* opengl-ish and d3d-ish spot lights use too different models
1619 * for the light "intensity" as a function of the angle towards
1620 * the main light direction, so we only can approximate very
1621 * roughly. However, spot lights are rather rarely used in games
1622 * (if ever used at all). Furthermore if still used, probably
1623 * nobody pays attention to such details. */
1624 if (!light->falloff)
1626 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1627 * equations have the falloff resp. exponent parameter as an
1628 * exponent, so the spot light lighting will always be 1.0 for
1629 * both of them, and we don't have to care for the rest of the
1630 * rather complex calculation. */
1631 object->exponent = 0.0f;
1633 else
1635 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1636 if (rho < 0.0001f)
1637 rho = 0.0001f;
1638 object->exponent = -0.3f / logf(cosf(rho / 2));
1641 if (object->exponent > 128.0f)
1642 object->exponent = 128.0f;
1644 object->cutoff = (float)(light->phi * 90 / M_PI);
1645 /* FIXME: Range */
1646 break;
1648 case WINED3D_LIGHT_PARALLELPOINT:
1649 object->position.x = light->position.x;
1650 object->position.y = light->position.y;
1651 object->position.z = light->position.z;
1652 object->position.w = 1.0f;
1653 break;
1655 default:
1656 FIXME("Unrecognized light type %#x.\n", light->type);
1659 return WINED3D_OK;
1662 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1663 UINT light_idx, struct wined3d_light *light)
1665 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1666 struct wined3d_light_info *light_info = NULL;
1667 struct list *e;
1669 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1671 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1673 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1674 if (light_info->OriginalIndex == light_idx)
1675 break;
1676 light_info = NULL;
1679 if (!light_info)
1681 TRACE("Light information requested but light not defined\n");
1682 return WINED3DERR_INVALIDCALL;
1685 *light = light_info->OriginalParms;
1686 return WINED3D_OK;
1689 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1691 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1692 struct wined3d_light_info *light_info = NULL;
1693 struct list *e;
1695 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1697 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1699 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1700 if (light_info->OriginalIndex == light_idx)
1701 break;
1702 light_info = NULL;
1704 TRACE("Found light %p.\n", light_info);
1706 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1707 if (!light_info)
1709 TRACE("Light enabled requested but light not defined, so defining one!\n");
1710 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1712 /* Search for it again! Should be fairly quick as near head of list. */
1713 LIST_FOR_EACH(e, &device->update_state->light_map[hash_idx])
1715 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1716 if (light_info->OriginalIndex == light_idx)
1717 break;
1718 light_info = NULL;
1720 if (!light_info)
1722 FIXME("Adding default lights has failed dismally\n");
1723 return WINED3DERR_INVALIDCALL;
1727 if (!enable)
1729 if (light_info->glIndex != -1)
1731 if (!device->recording)
1733 device_invalidate_state(device, STATE_LIGHT_TYPE);
1734 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1737 device->update_state->lights[light_info->glIndex] = NULL;
1738 light_info->glIndex = -1;
1740 else
1742 TRACE("Light already disabled, nothing to do\n");
1744 light_info->enabled = FALSE;
1746 else
1748 light_info->enabled = TRUE;
1749 if (light_info->glIndex != -1)
1751 TRACE("Nothing to do as light was enabled\n");
1753 else
1755 unsigned int i;
1756 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1757 /* Find a free GL light. */
1758 for (i = 0; i < gl_info->limits.lights; ++i)
1760 if (!device->update_state->lights[i])
1762 device->update_state->lights[i] = light_info;
1763 light_info->glIndex = i;
1764 break;
1767 if (light_info->glIndex == -1)
1769 /* Our tests show that Windows returns D3D_OK in this situation, even with
1770 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1771 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1772 * as well for those lights.
1774 * TODO: Test how this affects rendering. */
1775 WARN("Too many concurrently active lights\n");
1776 return WINED3D_OK;
1779 /* i == light_info->glIndex */
1780 if (!device->recording)
1782 device_invalidate_state(device, STATE_LIGHT_TYPE);
1783 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1788 return WINED3D_OK;
1791 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1793 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1794 struct wined3d_light_info *light_info = NULL;
1795 struct list *e;
1797 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1799 LIST_FOR_EACH(e, &device->state.light_map[hash_idx])
1801 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1802 if (light_info->OriginalIndex == light_idx)
1803 break;
1804 light_info = NULL;
1807 if (!light_info)
1809 TRACE("Light enabled state requested but light not defined.\n");
1810 return WINED3DERR_INVALIDCALL;
1812 /* true is 128 according to SetLightEnable */
1813 *enable = light_info->enabled ? 128 : 0;
1814 return WINED3D_OK;
1817 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1818 UINT plane_idx, const struct wined3d_vec4 *plane)
1820 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1822 /* Validate plane_idx. */
1823 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1825 TRACE("Application has requested clipplane this device doesn't support.\n");
1826 return WINED3DERR_INVALIDCALL;
1829 if (device->recording)
1830 device->recording->changed.clipplane |= 1u << plane_idx;
1832 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1834 TRACE("Application is setting old values over, nothing to do.\n");
1835 return WINED3D_OK;
1838 device->update_state->clip_planes[plane_idx] = *plane;
1840 if (!device->recording)
1841 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1843 return WINED3D_OK;
1846 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1847 UINT plane_idx, struct wined3d_vec4 *plane)
1849 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1851 /* Validate plane_idx. */
1852 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
1854 TRACE("Application has requested clipplane this device doesn't support.\n");
1855 return WINED3DERR_INVALIDCALL;
1858 *plane = device->state.clip_planes[plane_idx];
1860 return WINED3D_OK;
1863 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1864 const struct wined3d_clip_status *clip_status)
1866 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1868 if (!clip_status)
1869 return WINED3DERR_INVALIDCALL;
1871 return WINED3D_OK;
1874 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1875 struct wined3d_clip_status *clip_status)
1877 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1879 if (!clip_status)
1880 return WINED3DERR_INVALIDCALL;
1882 return WINED3D_OK;
1885 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1887 TRACE("device %p, material %p.\n", device, material);
1889 device->update_state->material = *material;
1891 if (device->recording)
1892 device->recording->changed.material = TRUE;
1893 else
1894 wined3d_cs_emit_set_material(device->cs, material);
1897 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1899 TRACE("device %p, material %p.\n", device, material);
1901 *material = device->state.material;
1903 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
1904 material->diffuse.r, material->diffuse.g,
1905 material->diffuse.b, material->diffuse.a);
1906 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
1907 material->ambient.r, material->ambient.g,
1908 material->ambient.b, material->ambient.a);
1909 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
1910 material->specular.r, material->specular.g,
1911 material->specular.b, material->specular.a);
1912 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
1913 material->emissive.r, material->emissive.g,
1914 material->emissive.b, material->emissive.a);
1915 TRACE("power %.8e.\n", material->power);
1918 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1919 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
1921 enum wined3d_format_id prev_format;
1922 struct wined3d_buffer *prev_buffer;
1924 TRACE("device %p, buffer %p, format %s.\n",
1925 device, buffer, debug_d3dformat(format_id));
1927 prev_buffer = device->update_state->index_buffer;
1928 prev_format = device->update_state->index_format;
1930 device->update_state->index_buffer = buffer;
1931 device->update_state->index_format = format_id;
1933 if (device->recording)
1934 device->recording->changed.indices = TRUE;
1936 if (prev_buffer == buffer && prev_format == format_id)
1937 return;
1939 if (buffer)
1940 wined3d_buffer_incref(buffer);
1941 if (!device->recording)
1942 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id);
1943 if (prev_buffer)
1944 wined3d_buffer_decref(prev_buffer);
1947 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1948 enum wined3d_format_id *format)
1950 TRACE("device %p, format %p.\n", device, format);
1952 *format = device->state.index_format;
1953 return device->state.index_buffer;
1956 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1958 TRACE("device %p, base_index %d.\n", device, base_index);
1960 device->update_state->base_vertex_index = base_index;
1963 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1965 TRACE("device %p.\n", device);
1967 return device->state.base_vertex_index;
1970 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1972 TRACE("device %p, viewport %p.\n", device, viewport);
1973 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
1974 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1976 device->update_state->viewport = *viewport;
1978 /* Handle recording of state blocks */
1979 if (device->recording)
1981 TRACE("Recording... not performing anything\n");
1982 device->recording->changed.viewport = TRUE;
1983 return;
1986 wined3d_cs_emit_set_viewport(device->cs, viewport);
1989 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1991 TRACE("device %p, viewport %p.\n", device, viewport);
1993 *viewport = device->state.viewport;
1996 static void resolve_depth_buffer(struct wined3d_state *state)
1998 struct wined3d_texture *dst_texture = state->textures[0];
1999 struct wined3d_rendertarget_view *src_view;
2000 RECT src_rect, dst_rect;
2002 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2003 || !(dst_texture->resource.format_flags & WINED3DFMT_FLAG_DEPTH))
2004 return;
2006 if (!(src_view = state->fb->depth_stencil))
2007 return;
2008 if (src_view->resource->type == WINED3D_RTYPE_BUFFER)
2010 FIXME("Not supported on buffer resources.\n");
2011 return;
2014 SetRect(&dst_rect, 0, 0, dst_texture->resource.width, dst_texture->resource.height);
2015 SetRect(&src_rect, 0, 0, src_view->width, src_view->height);
2016 wined3d_texture_blt(dst_texture, 0, &dst_rect, wined3d_texture_from_resource(src_view->resource),
2017 src_view->sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
2020 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2021 enum wined3d_render_state state, DWORD value)
2023 DWORD old_value;
2025 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2027 if (state > WINEHIGHEST_RENDER_STATE)
2029 WARN("Unhandled render state %#x.\n", state);
2030 return;
2033 old_value = device->state.render_states[state];
2034 device->update_state->render_states[state] = value;
2036 /* Handle recording of state blocks. */
2037 if (device->recording)
2039 TRACE("Recording... not performing anything.\n");
2040 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2041 return;
2044 /* Compared here and not before the assignment to allow proper stateblock recording. */
2045 if (value == old_value)
2046 TRACE("Application is setting the old value over, nothing to do.\n");
2047 else
2048 wined3d_cs_emit_set_render_state(device->cs, state, value);
2050 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2052 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2053 resolve_depth_buffer(&device->state);
2057 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2059 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2061 return device->state.render_states[state];
2064 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2065 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2067 DWORD old_value;
2069 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2070 device, sampler_idx, debug_d3dsamplerstate(state), value);
2072 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2073 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2075 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2077 WARN("Invalid sampler %u.\n", sampler_idx);
2078 return; /* Windows accepts overflowing this array ... we do not. */
2081 old_value = device->state.sampler_states[sampler_idx][state];
2082 device->update_state->sampler_states[sampler_idx][state] = value;
2084 /* Handle recording of state blocks. */
2085 if (device->recording)
2087 TRACE("Recording... not performing anything.\n");
2088 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2089 return;
2092 if (old_value == value)
2094 TRACE("Application is setting the old value over, nothing to do.\n");
2095 return;
2098 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2101 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2102 UINT sampler_idx, enum wined3d_sampler_state state)
2104 TRACE("device %p, sampler_idx %u, state %s.\n",
2105 device, sampler_idx, debug_d3dsamplerstate(state));
2107 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2108 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2110 if (sampler_idx >= sizeof(device->state.sampler_states) / sizeof(*device->state.sampler_states))
2112 WARN("Invalid sampler %u.\n", sampler_idx);
2113 return 0; /* Windows accepts overflowing this array ... we do not. */
2116 return device->state.sampler_states[sampler_idx][state];
2119 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2121 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2123 if (device->recording)
2124 device->recording->changed.scissorRect = TRUE;
2126 if (EqualRect(&device->update_state->scissor_rect, rect))
2128 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2129 return;
2131 CopyRect(&device->update_state->scissor_rect, rect);
2133 if (device->recording)
2135 TRACE("Recording... not performing anything.\n");
2136 return;
2139 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2142 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2144 TRACE("device %p, rect %p.\n", device, rect);
2146 *rect = device->state.scissor_rect;
2147 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2150 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2151 struct wined3d_vertex_declaration *declaration)
2153 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2155 TRACE("device %p, declaration %p.\n", device, declaration);
2157 if (device->recording)
2158 device->recording->changed.vertexDecl = TRUE;
2160 if (declaration == prev)
2161 return;
2163 if (declaration)
2164 wined3d_vertex_declaration_incref(declaration);
2165 device->update_state->vertex_declaration = declaration;
2166 if (!device->recording)
2167 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2168 if (prev)
2169 wined3d_vertex_declaration_decref(prev);
2172 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2174 TRACE("device %p.\n", device);
2176 return device->state.vertex_declaration;
2179 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2181 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2183 TRACE("device %p, shader %p.\n", device, shader);
2185 if (device->recording)
2186 device->recording->changed.vertexShader = TRUE;
2188 if (shader == prev)
2189 return;
2191 if (shader)
2192 wined3d_shader_incref(shader);
2193 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2194 if (!device->recording)
2195 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2196 if (prev)
2197 wined3d_shader_decref(prev);
2200 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2202 TRACE("device %p.\n", device);
2204 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2207 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2208 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2210 struct wined3d_buffer *prev;
2212 if (idx >= MAX_CONSTANT_BUFFERS)
2214 WARN("Invalid constant buffer index %u.\n", idx);
2215 return;
2218 prev = device->update_state->cb[type][idx];
2219 if (buffer == prev)
2220 return;
2222 if (buffer)
2223 wined3d_buffer_incref(buffer);
2224 device->update_state->cb[type][idx] = buffer;
2225 if (!device->recording)
2226 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2227 if (prev)
2228 wined3d_buffer_decref(prev);
2231 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2233 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2235 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2238 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2240 TRACE("device %p, idx %u.\n", device, idx);
2242 if (idx >= MAX_CONSTANT_BUFFERS)
2244 WARN("Invalid constant buffer index %u.\n", idx);
2245 return NULL;
2248 return device->state.cb[WINED3D_SHADER_TYPE_VERTEX][idx];
2251 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2252 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2254 struct wined3d_shader_resource_view *prev;
2256 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2258 WARN("Invalid view index %u.\n", idx);
2259 return;
2262 prev = device->update_state->shader_resource_view[type][idx];
2263 if (view == prev)
2264 return;
2266 if (view)
2267 wined3d_shader_resource_view_incref(view);
2268 device->update_state->shader_resource_view[type][idx] = view;
2269 if (!device->recording)
2270 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2271 if (prev)
2272 wined3d_shader_resource_view_decref(prev);
2275 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2276 UINT idx, struct wined3d_shader_resource_view *view)
2278 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2280 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2283 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2284 UINT idx)
2286 TRACE("device %p, idx %u.\n", device, idx);
2288 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2290 WARN("Invalid view index %u.\n", idx);
2291 return NULL;
2294 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_VERTEX][idx];
2297 static void wined3d_device_set_sampler(struct wined3d_device *device,
2298 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2300 struct wined3d_sampler *prev;
2302 if (idx >= MAX_SAMPLER_OBJECTS)
2304 WARN("Invalid sampler index %u.\n", idx);
2305 return;
2308 prev = device->update_state->sampler[type][idx];
2309 if (sampler == prev)
2310 return;
2312 if (sampler)
2313 wined3d_sampler_incref(sampler);
2314 device->update_state->sampler[type][idx] = sampler;
2315 if (!device->recording)
2316 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2317 if (prev)
2318 wined3d_sampler_decref(prev);
2321 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2323 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2325 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2328 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2330 TRACE("device %p, idx %u.\n", device, idx);
2332 if (idx >= MAX_SAMPLER_OBJECTS)
2334 WARN("Invalid sampler index %u.\n", idx);
2335 return NULL;
2338 return device->state.sampler[WINED3D_SHADER_TYPE_VERTEX][idx];
2341 static void device_invalidate_shader_constants(const struct wined3d_device *device, DWORD mask)
2343 UINT i;
2345 for (i = 0; i < device->context_count; ++i)
2347 device->contexts[i]->constant_update_mask |= mask;
2351 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2352 UINT start_register, const BOOL *constants, UINT bool_count)
2354 UINT count = min(bool_count, MAX_CONST_B - start_register);
2355 UINT i;
2357 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2358 device, start_register, constants, bool_count);
2360 if (!constants || start_register >= MAX_CONST_B)
2361 return WINED3DERR_INVALIDCALL;
2363 memcpy(&device->update_state->vs_consts_b[start_register], constants, count * sizeof(BOOL));
2364 for (i = 0; i < count; ++i)
2365 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2367 if (device->recording)
2369 for (i = start_register; i < count + start_register; ++i)
2370 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2372 else
2374 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_B);
2377 return WINED3D_OK;
2380 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2381 UINT start_register, BOOL *constants, UINT bool_count)
2383 UINT count = min(bool_count, MAX_CONST_B - start_register);
2385 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2386 device, start_register, constants, bool_count);
2388 if (!constants || start_register >= MAX_CONST_B)
2389 return WINED3DERR_INVALIDCALL;
2391 memcpy(constants, &device->state.vs_consts_b[start_register], count * sizeof(BOOL));
2393 return WINED3D_OK;
2396 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2397 UINT start_register, const int *constants, UINT vector4i_count)
2399 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2400 UINT i;
2402 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2403 device, start_register, constants, vector4i_count);
2405 if (!constants || start_register >= MAX_CONST_I)
2406 return WINED3DERR_INVALIDCALL;
2408 memcpy(&device->update_state->vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2409 for (i = 0; i < count; ++i)
2410 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2411 constants[i * 4], constants[i * 4 + 1],
2412 constants[i * 4 + 2], constants[i * 4 + 3]);
2414 if (device->recording)
2416 for (i = start_register; i < count + start_register; ++i)
2417 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2419 else
2421 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_VS_I);
2424 return WINED3D_OK;
2427 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2428 UINT start_register, int *constants, UINT vector4i_count)
2430 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2432 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2433 device, start_register, constants, vector4i_count);
2435 if (!constants || start_register >= MAX_CONST_I)
2436 return WINED3DERR_INVALIDCALL;
2438 memcpy(constants, &device->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2439 return WINED3D_OK;
2442 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2443 UINT start_register, const float *constants, UINT vector4f_count)
2445 UINT i;
2446 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2448 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2449 device, start_register, constants, vector4f_count);
2451 /* Specifically test start_register > limit to catch MAX_UINT overflows
2452 * when adding start_register + vector4f_count. */
2453 if (!constants
2454 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2455 || start_register > d3d_info->limits.vs_uniform_count)
2456 return WINED3DERR_INVALIDCALL;
2458 memcpy(&device->update_state->vs_consts_f[start_register * 4],
2459 constants, vector4f_count * sizeof(float) * 4);
2460 if (TRACE_ON(d3d))
2462 for (i = 0; i < vector4f_count; ++i)
2463 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2464 constants[i * 4], constants[i * 4 + 1],
2465 constants[i * 4 + 2], constants[i * 4 + 3]);
2468 if (device->recording)
2469 memset(device->recording->changed.vertexShaderConstantsF + start_register, 1,
2470 sizeof(*device->recording->changed.vertexShaderConstantsF) * vector4f_count);
2471 else
2472 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2475 return WINED3D_OK;
2478 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2479 UINT start_register, float *constants, UINT vector4f_count)
2481 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2482 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2484 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2485 device, start_register, constants, vector4f_count);
2487 if (!constants || count < 0)
2488 return WINED3DERR_INVALIDCALL;
2490 memcpy(constants, &device->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2492 return WINED3D_OK;
2495 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2497 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2499 TRACE("device %p, shader %p.\n", device, shader);
2501 if (device->recording)
2502 device->recording->changed.pixelShader = TRUE;
2504 if (shader == prev)
2505 return;
2507 if (shader)
2508 wined3d_shader_incref(shader);
2509 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2510 if (!device->recording)
2511 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2512 if (prev)
2513 wined3d_shader_decref(prev);
2516 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2518 TRACE("device %p.\n", device);
2520 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2523 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2525 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2527 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2530 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2532 TRACE("device %p, idx %u.\n", device, idx);
2534 if (idx >= MAX_CONSTANT_BUFFERS)
2536 WARN("Invalid constant buffer index %u.\n", idx);
2537 return NULL;
2540 return device->state.cb[WINED3D_SHADER_TYPE_PIXEL][idx];
2543 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2544 UINT idx, struct wined3d_shader_resource_view *view)
2546 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2548 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2551 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2552 UINT idx)
2554 TRACE("device %p, idx %u.\n", device, idx);
2556 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2558 WARN("Invalid view index %u.\n", idx);
2559 return NULL;
2562 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_PIXEL][idx];
2565 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2567 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2569 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2572 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2574 TRACE("device %p, idx %u.\n", device, idx);
2576 if (idx >= MAX_SAMPLER_OBJECTS)
2578 WARN("Invalid sampler index %u.\n", idx);
2579 return NULL;
2582 return device->state.sampler[WINED3D_SHADER_TYPE_PIXEL][idx];
2585 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2586 UINT start_register, const BOOL *constants, UINT bool_count)
2588 UINT count = min(bool_count, MAX_CONST_B - start_register);
2589 UINT i;
2591 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2592 device, start_register, constants, bool_count);
2594 if (!constants || start_register >= MAX_CONST_B)
2595 return WINED3DERR_INVALIDCALL;
2597 memcpy(&device->update_state->ps_consts_b[start_register], constants, count * sizeof(BOOL));
2598 for (i = 0; i < count; ++i)
2599 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2601 if (device->recording)
2603 for (i = start_register; i < count + start_register; ++i)
2604 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2606 else
2608 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_B);
2611 return WINED3D_OK;
2614 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2615 UINT start_register, BOOL *constants, UINT bool_count)
2617 UINT count = min(bool_count, MAX_CONST_B - start_register);
2619 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2620 device, start_register, constants, bool_count);
2622 if (!constants || start_register >= MAX_CONST_B)
2623 return WINED3DERR_INVALIDCALL;
2625 memcpy(constants, &device->state.ps_consts_b[start_register], count * sizeof(BOOL));
2627 return WINED3D_OK;
2630 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2631 UINT start_register, const int *constants, UINT vector4i_count)
2633 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2634 UINT i;
2636 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2637 device, start_register, constants, vector4i_count);
2639 if (!constants || start_register >= MAX_CONST_I)
2640 return WINED3DERR_INVALIDCALL;
2642 memcpy(&device->update_state->ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2643 for (i = 0; i < count; ++i)
2644 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2645 constants[i * 4], constants[i * 4 + 1],
2646 constants[i * 4 + 2], constants[i * 4 + 3]);
2648 if (device->recording)
2650 for (i = start_register; i < count + start_register; ++i)
2651 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2653 else
2655 device_invalidate_shader_constants(device, WINED3D_SHADER_CONST_PS_I);
2658 return WINED3D_OK;
2661 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2662 UINT start_register, int *constants, UINT vector4i_count)
2664 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2666 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2667 device, start_register, constants, vector4i_count);
2669 if (!constants || start_register >= MAX_CONST_I)
2670 return WINED3DERR_INVALIDCALL;
2672 memcpy(constants, &device->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2674 return WINED3D_OK;
2677 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2678 UINT start_register, const float *constants, UINT vector4f_count)
2680 UINT i;
2681 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2683 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2684 device, start_register, constants, vector4f_count);
2686 /* Specifically test start_register > limit to catch MAX_UINT overflows
2687 * when adding start_register + vector4f_count. */
2688 if (!constants
2689 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
2690 || start_register > d3d_info->limits.ps_uniform_count)
2691 return WINED3DERR_INVALIDCALL;
2693 memcpy(&device->update_state->ps_consts_f[start_register * 4],
2694 constants, vector4f_count * sizeof(float) * 4);
2695 if (TRACE_ON(d3d))
2697 for (i = 0; i < vector4f_count; ++i)
2698 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2699 constants[i * 4], constants[i * 4 + 1],
2700 constants[i * 4 + 2], constants[i * 4 + 3]);
2703 if (device->recording)
2704 memset(device->recording->changed.pixelShaderConstantsF + start_register, 1,
2705 sizeof(*device->recording->changed.pixelShaderConstantsF) * vector4f_count);
2706 else
2707 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2709 return WINED3D_OK;
2712 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2713 UINT start_register, float *constants, UINT vector4f_count)
2715 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2716 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
2718 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2719 device, start_register, constants, vector4f_count);
2721 if (!constants || count < 0)
2722 return WINED3DERR_INVALIDCALL;
2724 memcpy(constants, &device->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2726 return WINED3D_OK;
2729 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2731 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2733 TRACE("device %p, shader %p.\n", device, shader);
2735 if (device->recording || shader == prev)
2736 return;
2737 if (shader)
2738 wined3d_shader_incref(shader);
2739 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2740 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2741 if (prev)
2742 wined3d_shader_decref(prev);
2745 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2747 TRACE("device %p.\n", device);
2749 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2752 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2754 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2756 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2759 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2761 TRACE("device %p, idx %u.\n", device, idx);
2763 if (idx >= MAX_CONSTANT_BUFFERS)
2765 WARN("Invalid constant buffer index %u.\n", idx);
2766 return NULL;
2769 return device->state.cb[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2772 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2773 UINT idx, struct wined3d_shader_resource_view *view)
2775 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2777 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2780 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2781 UINT idx)
2783 TRACE("device %p, idx %u.\n", device, idx);
2785 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2787 WARN("Invalid view index %u.\n", idx);
2788 return NULL;
2791 return device->state.shader_resource_view[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2794 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2796 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2798 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2801 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2803 TRACE("device %p, idx %u.\n", device, idx);
2805 if (idx >= MAX_SAMPLER_OBJECTS)
2807 WARN("Invalid sampler index %u.\n", idx);
2808 return NULL;
2811 return device->state.sampler[WINED3D_SHADER_TYPE_GEOMETRY][idx];
2814 /* Context activation is done by the caller. */
2815 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
2816 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
2817 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
2818 DWORD DestFVF)
2820 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
2821 struct wined3d_viewport vp;
2822 UINT vertex_size;
2823 unsigned int i;
2824 BYTE *dest_ptr;
2825 BOOL doClip;
2826 DWORD numTextures;
2827 HRESULT hr;
2829 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
2831 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
2834 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
2836 ERR("Source has no position mask\n");
2837 return WINED3DERR_INVALIDCALL;
2840 if (device->state.render_states[WINED3D_RS_CLIPPING])
2842 static BOOL warned = FALSE;
2844 * The clipping code is not quite correct. Some things need
2845 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
2846 * so disable clipping for now.
2847 * (The graphics in Half-Life are broken, and my processvertices
2848 * test crashes with IDirect3DDevice3)
2849 doClip = TRUE;
2851 doClip = FALSE;
2852 if(!warned) {
2853 warned = TRUE;
2854 FIXME("Clipping is broken and disabled for now\n");
2857 else
2858 doClip = FALSE;
2860 vertex_size = get_flexible_vertex_size(DestFVF);
2861 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
2863 WARN("Failed to map buffer, hr %#x.\n", hr);
2864 return hr;
2867 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
2868 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
2869 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
2871 TRACE("View mat:\n");
2872 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
2873 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
2874 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
2875 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
2877 TRACE("Proj mat:\n");
2878 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
2879 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
2880 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
2881 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
2883 TRACE("World mat:\n");
2884 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
2885 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
2886 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
2887 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
2889 /* Get the viewport */
2890 wined3d_device_get_viewport(device, &vp);
2891 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
2892 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
2894 multiply_matrix(&mat,&view_mat,&world_mat);
2895 multiply_matrix(&mat,&proj_mat,&mat);
2897 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2899 for (i = 0; i < dwCount; i+= 1) {
2900 unsigned int tex_index;
2902 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
2903 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
2904 /* The position first */
2905 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
2906 const float *p = (const float *)(element->data.addr + i * element->stride);
2907 float x, y, z, rhw;
2908 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
2910 /* Multiplication with world, view and projection matrix. */
2911 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
2912 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
2913 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
2914 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
2916 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
2918 /* WARNING: The following things are taken from d3d7 and were not yet checked
2919 * against d3d8 or d3d9!
2922 /* Clipping conditions: From msdn
2924 * A vertex is clipped if it does not match the following requirements
2925 * -rhw < x <= rhw
2926 * -rhw < y <= rhw
2927 * 0 < z <= rhw
2928 * 0 < rhw ( Not in d3d7, but tested in d3d7)
2930 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
2931 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
2935 if( !doClip ||
2936 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
2937 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
2938 ( rhw > eps ) ) ) {
2940 /* "Normal" viewport transformation (not clipped)
2941 * 1) The values are divided by rhw
2942 * 2) The y axis is negative, so multiply it with -1
2943 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
2944 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
2945 * 4) Multiply x with Width/2 and add Width/2
2946 * 5) The same for the height
2947 * 6) Add the viewpoint X and Y to the 2D coordinates and
2948 * The minimum Z value to z
2949 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
2951 * Well, basically it's simply a linear transformation into viewport
2952 * coordinates
2955 x /= rhw;
2956 y /= rhw;
2957 z /= rhw;
2959 y *= -1;
2961 x *= vp.width / 2;
2962 y *= vp.height / 2;
2963 z *= vp.max_z - vp.min_z;
2965 x += vp.width / 2 + vp.x;
2966 y += vp.height / 2 + vp.y;
2967 z += vp.min_z;
2969 rhw = 1 / rhw;
2970 } else {
2971 /* That vertex got clipped
2972 * Contrary to OpenGL it is not dropped completely, it just
2973 * undergoes a different calculation.
2975 TRACE("Vertex got clipped\n");
2976 x += rhw;
2977 y += rhw;
2979 x /= 2;
2980 y /= 2;
2982 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
2983 * outside of the main vertex buffer memory. That needs some more
2984 * investigation...
2988 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
2991 ( (float *) dest_ptr)[0] = x;
2992 ( (float *) dest_ptr)[1] = y;
2993 ( (float *) dest_ptr)[2] = z;
2994 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
2996 dest_ptr += 3 * sizeof(float);
2998 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
2999 dest_ptr += sizeof(float);
3002 if (DestFVF & WINED3DFVF_PSIZE)
3003 dest_ptr += sizeof(DWORD);
3005 if (DestFVF & WINED3DFVF_NORMAL)
3007 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3008 const float *normal = (const float *)(element->data.addr + i * element->stride);
3009 /* AFAIK this should go into the lighting information */
3010 FIXME("Didn't expect the destination to have a normal\n");
3011 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3014 if (DestFVF & WINED3DFVF_DIFFUSE)
3016 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3017 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3018 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3020 static BOOL warned = FALSE;
3022 if(!warned) {
3023 ERR("No diffuse color in source, but destination has one\n");
3024 warned = TRUE;
3027 *( (DWORD *) dest_ptr) = 0xffffffff;
3028 dest_ptr += sizeof(DWORD);
3030 else
3032 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3036 if (DestFVF & WINED3DFVF_SPECULAR)
3038 /* What's the color value in the feedback buffer? */
3039 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3040 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3041 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3043 static BOOL warned = FALSE;
3045 if(!warned) {
3046 ERR("No specular color in source, but destination has one\n");
3047 warned = TRUE;
3050 *(DWORD *)dest_ptr = 0xff000000;
3051 dest_ptr += sizeof(DWORD);
3053 else
3055 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3059 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3061 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3062 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3063 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3065 ERR("No source texture, but destination requests one\n");
3066 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3068 else
3070 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3075 wined3d_buffer_unmap(dest);
3077 return WINED3D_OK;
3079 #undef copy_and_next
3081 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3082 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3083 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3085 struct wined3d_state *state = &device->state;
3086 struct wined3d_stream_info stream_info;
3087 const struct wined3d_gl_info *gl_info;
3088 struct wined3d_context *context;
3089 struct wined3d_shader *vs;
3090 unsigned int i;
3091 HRESULT hr;
3092 WORD map;
3094 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3095 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3096 device, src_start_idx, dst_idx, vertex_count,
3097 dst_buffer, declaration, flags, dst_fvf);
3099 if (declaration)
3100 FIXME("Output vertex declaration not implemented yet.\n");
3102 /* Need any context to write to the vbo. */
3103 context = context_acquire(device, NULL);
3104 gl_info = context->gl_info;
3106 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3107 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3108 context_stream_info_from_declaration(context, state, &stream_info);
3109 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3111 /* We can't convert FROM a VBO, and vertex buffers used to source into
3112 * process_vertices() are unlikely to ever be used for drawing. Release
3113 * VBOs in those buffers and fix up the stream_info structure.
3115 * Also apply the start index. */
3116 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3118 struct wined3d_stream_info_element *e;
3119 struct wined3d_buffer *buffer;
3121 if (!(map & 1))
3122 continue;
3124 e = &stream_info.elements[i];
3125 buffer = state->streams[e->stream_idx].buffer;
3126 e->data.buffer_object = 0;
3127 e->data.addr += (ULONG_PTR)buffer_get_sysmem(buffer, context);
3128 if (buffer->buffer_object)
3130 GL_EXTCALL(glDeleteBuffers(1, &buffer->buffer_object));
3131 buffer->buffer_object = 0;
3133 if (e->data.addr)
3134 e->data.addr += e->stride * src_start_idx;
3137 hr = process_vertices_strided(device, dst_idx, vertex_count,
3138 &stream_info, dst_buffer, flags, dst_fvf);
3140 context_release(context);
3142 return hr;
3145 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3146 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3148 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3149 DWORD old_value;
3151 TRACE("device %p, stage %u, state %s, value %#x.\n",
3152 device, stage, debug_d3dtexturestate(state), value);
3154 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3156 WARN("Invalid state %#x passed.\n", state);
3157 return;
3160 if (stage >= d3d_info->limits.ffp_blend_stages)
3162 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3163 stage, d3d_info->limits.ffp_blend_stages - 1);
3164 return;
3167 old_value = device->update_state->texture_states[stage][state];
3168 device->update_state->texture_states[stage][state] = value;
3170 if (device->recording)
3172 TRACE("Recording... not performing anything.\n");
3173 device->recording->changed.textureState[stage] |= 1u << state;
3174 return;
3177 /* Checked after the assignments to allow proper stateblock recording. */
3178 if (old_value == value)
3180 TRACE("Application is setting the old value over, nothing to do.\n");
3181 return;
3184 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3187 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3188 UINT stage, enum wined3d_texture_stage_state state)
3190 TRACE("device %p, stage %u, state %s.\n",
3191 device, stage, debug_d3dtexturestate(state));
3193 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3195 WARN("Invalid state %#x passed.\n", state);
3196 return 0;
3199 return device->state.texture_states[stage][state];
3202 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3203 UINT stage, struct wined3d_texture *texture)
3205 struct wined3d_texture *prev;
3207 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3209 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3210 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3212 /* Windows accepts overflowing this array... we do not. */
3213 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3215 WARN("Ignoring invalid stage %u.\n", stage);
3216 return WINED3D_OK;
3219 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3221 WARN("Rejecting attempt to set scratch texture.\n");
3222 return WINED3DERR_INVALIDCALL;
3225 if (device->recording)
3226 device->recording->changed.textures |= 1u << stage;
3228 prev = device->update_state->textures[stage];
3229 TRACE("Previous texture %p.\n", prev);
3231 if (texture == prev)
3233 TRACE("App is setting the same texture again, nothing to do.\n");
3234 return WINED3D_OK;
3237 TRACE("Setting new texture to %p.\n", texture);
3238 device->update_state->textures[stage] = texture;
3240 if (texture)
3241 wined3d_texture_incref(texture);
3242 if (!device->recording)
3243 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3244 if (prev)
3245 wined3d_texture_decref(prev);
3247 return WINED3D_OK;
3250 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3252 TRACE("device %p, stage %u.\n", device, stage);
3254 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3255 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3257 if (stage >= sizeof(device->state.textures) / sizeof(*device->state.textures))
3259 WARN("Ignoring invalid stage %u.\n", stage);
3260 return NULL; /* Windows accepts overflowing this array ... we do not. */
3263 return device->state.textures[stage];
3266 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3268 TRACE("device %p, caps %p.\n", device, caps);
3270 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3271 device->create_parms.device_type, caps);
3274 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3275 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3277 struct wined3d_swapchain *swapchain;
3279 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3280 device, swapchain_idx, mode, rotation);
3282 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3283 return WINED3DERR_INVALIDCALL;
3285 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3288 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3290 struct wined3d_stateblock *stateblock;
3291 HRESULT hr;
3293 TRACE("device %p.\n", device);
3295 if (device->recording)
3296 return WINED3DERR_INVALIDCALL;
3298 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3299 if (FAILED(hr))
3300 return hr;
3302 device->recording = stateblock;
3303 device->update_state = &stateblock->state;
3305 TRACE("Recording stateblock %p.\n", stateblock);
3307 return WINED3D_OK;
3310 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3311 struct wined3d_stateblock **stateblock)
3313 struct wined3d_stateblock *object = device->recording;
3315 TRACE("device %p, stateblock %p.\n", device, stateblock);
3317 if (!device->recording)
3319 WARN("Not recording.\n");
3320 *stateblock = NULL;
3321 return WINED3DERR_INVALIDCALL;
3324 stateblock_init_contained_states(object);
3326 *stateblock = object;
3327 device->recording = NULL;
3328 device->update_state = &device->state;
3330 TRACE("Returning stateblock %p.\n", *stateblock);
3332 return WINED3D_OK;
3335 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3337 /* At the moment we have no need for any functionality at the beginning
3338 * of a scene. */
3339 TRACE("device %p.\n", device);
3341 if (device->inScene)
3343 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3344 return WINED3DERR_INVALIDCALL;
3346 device->inScene = TRUE;
3347 return WINED3D_OK;
3350 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3352 struct wined3d_context *context;
3354 TRACE("device %p.\n", device);
3356 if (!device->inScene)
3358 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3359 return WINED3DERR_INVALIDCALL;
3362 context = context_acquire(device, NULL);
3363 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3364 context->gl_info->gl_ops.gl.p_glFlush();
3365 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3366 * fails. */
3367 context_release(context);
3369 device->inScene = FALSE;
3370 return WINED3D_OK;
3373 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3374 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3376 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3377 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3379 if (!rect_count && rects)
3381 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3382 return WINED3D_OK;
3385 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3387 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3388 if (!ds)
3390 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3391 /* TODO: What about depth stencil buffers without stencil bits? */
3392 return WINED3DERR_INVALIDCALL;
3394 else if (flags & WINED3DCLEAR_TARGET)
3396 if (ds->width < device->fb.render_targets[0]->width
3397 || ds->height < device->fb.render_targets[0]->height)
3399 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3400 return WINED3D_OK;
3405 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3407 return WINED3D_OK;
3410 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3411 struct wined3d_query *predicate, BOOL value)
3413 struct wined3d_query *prev;
3415 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3417 prev = device->update_state->predicate;
3418 if (predicate)
3420 FIXME("Predicated rendering not implemented.\n");
3421 wined3d_query_incref(predicate);
3423 device->update_state->predicate = predicate;
3424 device->update_state->predicate_value = value;
3425 if (!device->recording)
3426 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3427 if (prev)
3428 wined3d_query_decref(prev);
3431 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3433 TRACE("device %p, value %p.\n", device, value);
3435 *value = device->state.predicate_value;
3436 return device->state.predicate;
3439 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3440 enum wined3d_primitive_type primitive_type)
3442 GLenum gl_primitive_type, prev;
3444 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3446 gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3447 prev = device->update_state->gl_primitive_type;
3448 device->update_state->gl_primitive_type = gl_primitive_type;
3449 if (device->recording)
3450 device->recording->changed.primitive_type = TRUE;
3451 else if (gl_primitive_type != prev && (gl_primitive_type == GL_POINTS || prev == GL_POINTS))
3452 device_invalidate_state(device, STATE_POINT_ENABLE);
3455 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3456 enum wined3d_primitive_type *primitive_type)
3458 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3460 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3462 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3465 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3467 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3469 if (!device->state.vertex_declaration)
3471 WARN("Called without a valid vertex declaration set.\n");
3472 return WINED3DERR_INVALIDCALL;
3475 if (device->state.load_base_vertex_index)
3477 device->state.load_base_vertex_index = 0;
3478 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3481 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, 0, 0, FALSE);
3483 return WINED3D_OK;
3486 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3487 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3489 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3490 device, start_vertex, vertex_count, start_instance, instance_count);
3492 wined3d_cs_emit_draw(device->cs, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3495 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3497 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3499 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3501 if (!device->state.index_buffer)
3503 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3504 * without an index buffer set. (The first time at least...)
3505 * D3D8 simply dies, but I doubt it can do much harm to return
3506 * D3DERR_INVALIDCALL there as well. */
3507 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3508 return WINED3DERR_INVALIDCALL;
3511 if (!device->state.vertex_declaration)
3513 WARN("Called without a valid vertex declaration set.\n");
3514 return WINED3DERR_INVALIDCALL;
3517 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3518 device->state.load_base_vertex_index != device->state.base_vertex_index)
3520 device->state.load_base_vertex_index = device->state.base_vertex_index;
3521 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3524 wined3d_cs_emit_draw(device->cs, start_idx, index_count, 0, 0, TRUE);
3526 return WINED3D_OK;
3529 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3530 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3532 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3533 device, start_idx, index_count, start_instance, instance_count);
3535 wined3d_cs_emit_draw(device->cs, start_idx, index_count, start_instance, instance_count, TRUE);
3538 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
3539 static HRESULT device_update_volume(struct wined3d_device *device,
3540 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
3542 struct wined3d_const_bo_address data;
3543 struct wined3d_map_desc src;
3544 HRESULT hr;
3545 struct wined3d_context *context;
3547 TRACE("device %p, src_volume %p, dst_volume %p.\n",
3548 device, src_volume, dst_volume);
3550 if (src_volume->resource.format != dst_volume->resource.format)
3552 FIXME("Source and destination formats do not match.\n");
3553 return WINED3DERR_INVALIDCALL;
3555 if (src_volume->resource.width != dst_volume->resource.width
3556 || src_volume->resource.height != dst_volume->resource.height
3557 || src_volume->resource.depth != dst_volume->resource.depth)
3559 FIXME("Source and destination sizes do not match.\n");
3560 return WINED3DERR_INVALIDCALL;
3563 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
3564 return hr;
3566 context = context_acquire(device, NULL);
3568 /* Only a prepare, since we're uploading the entire volume. */
3569 wined3d_texture_prepare_texture(dst_volume->container, context, FALSE);
3570 wined3d_texture_bind_and_dirtify(dst_volume->container, context, FALSE);
3572 data.buffer_object = 0;
3573 data.addr = src.data;
3574 wined3d_volume_upload_data(dst_volume, context, &data);
3575 wined3d_volume_invalidate_location(dst_volume, ~WINED3D_LOCATION_TEXTURE_RGB);
3577 context_release(context);
3579 hr = wined3d_volume_unmap(src_volume);
3581 return hr;
3584 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3585 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3587 unsigned int src_size, dst_size, src_skip_levels = 0;
3588 unsigned int layer_count, level_count, i, j;
3589 enum wined3d_resource_type type;
3590 HRESULT hr;
3591 struct wined3d_context *context;
3593 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3595 /* Verify that the source and destination textures are non-NULL. */
3596 if (!src_texture || !dst_texture)
3598 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3599 return WINED3DERR_INVALIDCALL;
3602 if (src_texture->resource.pool != WINED3D_POOL_SYSTEM_MEM)
3604 WARN("Source texture not in WINED3D_POOL_SYSTEM_MEM, returning WINED3DERR_INVALIDCALL.\n");
3605 return WINED3DERR_INVALIDCALL;
3607 if (dst_texture->resource.pool != WINED3D_POOL_DEFAULT)
3609 WARN("Destination texture not in WINED3D_POOL_DEFAULT, returning WINED3DERR_INVALIDCALL.\n");
3610 return WINED3DERR_INVALIDCALL;
3613 /* Verify that the source and destination textures are the same type. */
3614 type = src_texture->resource.type;
3615 if (dst_texture->resource.type != type)
3617 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3618 return WINED3DERR_INVALIDCALL;
3621 layer_count = src_texture->layer_count;
3622 if (layer_count != dst_texture->layer_count)
3624 WARN("Source and destination have different layer counts.\n");
3625 return WINED3DERR_INVALIDCALL;
3628 level_count = min(wined3d_texture_get_level_count(src_texture),
3629 wined3d_texture_get_level_count(dst_texture));
3631 src_size = max(src_texture->resource.width, src_texture->resource.height);
3632 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3633 if (type == WINED3D_RTYPE_VOLUME)
3635 src_size = max(src_size, src_texture->resource.depth);
3636 dst_size = max(dst_size, dst_texture->resource.depth);
3638 while (src_size > dst_size)
3640 src_size >>= 1;
3641 ++src_skip_levels;
3644 /* Make sure that the destination texture is loaded. */
3645 context = context_acquire(device, NULL);
3646 wined3d_texture_load(dst_texture, context, FALSE);
3647 context_release(context);
3649 /* Update every surface level of the texture. */
3650 switch (type)
3652 case WINED3D_RTYPE_TEXTURE_2D:
3654 unsigned int src_levels = src_texture->level_count;
3655 unsigned int dst_levels = dst_texture->level_count;
3656 struct wined3d_surface *src_surface;
3657 struct wined3d_surface *dst_surface;
3659 for (i = 0; i < layer_count; ++i)
3661 for (j = 0; j < level_count; ++j)
3663 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture,
3664 i * src_levels + j + src_skip_levels));
3665 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture,
3666 i * dst_levels + j));
3667 if (FAILED(hr = surface_upload_from_surface(dst_surface, NULL, src_surface, NULL)))
3669 WARN("Failed to update surface, hr %#x.\n", hr);
3670 return hr;
3674 break;
3677 case WINED3D_RTYPE_TEXTURE_3D:
3679 for (i = 0; i < level_count; ++i)
3681 hr = device_update_volume(device,
3682 volume_from_resource(wined3d_texture_get_sub_resource(src_texture,
3683 i + src_skip_levels)),
3684 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
3685 if (FAILED(hr))
3687 WARN("Failed to update volume, hr %#x.\n", hr);
3688 return hr;
3691 break;
3694 default:
3695 FIXME("Unsupported texture type %#x.\n", type);
3696 return WINED3DERR_INVALIDCALL;
3699 return WINED3D_OK;
3702 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3704 const struct wined3d_state *state = &device->state;
3705 struct wined3d_texture *texture;
3706 DWORD i;
3708 TRACE("device %p, num_passes %p.\n", device, num_passes);
3710 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3712 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3714 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3715 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3717 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3719 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3720 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3723 texture = state->textures[i];
3724 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3726 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3728 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3729 return E_FAIL;
3731 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3733 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3734 return E_FAIL;
3736 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3737 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3739 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3740 return E_FAIL;
3744 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3745 || state->render_states[WINED3D_RS_STENCILENABLE])
3747 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3748 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3750 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3752 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3753 return WINED3DERR_CONFLICTINGRENDERSTATE;
3757 /* return a sensible default */
3758 *num_passes = 1;
3760 TRACE("returning D3D_OK\n");
3761 return WINED3D_OK;
3764 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3766 static BOOL warned;
3768 TRACE("device %p, software %#x.\n", device, software);
3770 if (!warned)
3772 FIXME("device %p, software %#x stub!\n", device, software);
3773 warned = TRUE;
3776 device->softwareVertexProcessing = software;
3779 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3781 static BOOL warned;
3783 TRACE("device %p.\n", device);
3785 if (!warned)
3787 TRACE("device %p stub!\n", device);
3788 warned = TRUE;
3791 return device->softwareVertexProcessing;
3794 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3795 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3797 struct wined3d_swapchain *swapchain;
3799 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3800 device, swapchain_idx, raster_status);
3802 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3803 return WINED3DERR_INVALIDCALL;
3805 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3808 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3810 static BOOL warned;
3812 TRACE("device %p, segments %.8e.\n", device, segments);
3814 if (segments != 0.0f)
3816 if (!warned)
3818 FIXME("device %p, segments %.8e stub!\n", device, segments);
3819 warned = TRUE;
3823 return WINED3D_OK;
3826 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3828 static BOOL warned;
3830 TRACE("device %p.\n", device);
3832 if (!warned)
3834 FIXME("device %p stub!\n", device);
3835 warned = TRUE;
3838 return 0.0f;
3841 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
3842 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
3844 struct wined3d_surface *dst_surface, *src_surface;
3845 struct wined3d_texture *dst_texture, *src_texture;
3846 RECT dst_rect, src_rect;
3847 unsigned int i, count;
3848 HRESULT hr;
3850 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
3852 if (src_resource == dst_resource)
3854 WARN("Source and destination are the same resource.\n");
3855 return;
3858 if (src_resource->type != dst_resource->type)
3860 WARN("Resource types (%s / %s) don't match.\n",
3861 debug_d3dresourcetype(dst_resource->type),
3862 debug_d3dresourcetype(src_resource->type));
3863 return;
3866 if (src_resource->width != dst_resource->width
3867 || src_resource->height != dst_resource->height
3868 || src_resource->depth != dst_resource->depth)
3870 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
3871 dst_resource->width, dst_resource->height, dst_resource->depth,
3872 src_resource->width, src_resource->height, src_resource->depth);
3873 return;
3876 if (src_resource->format->id != dst_resource->format->id)
3878 WARN("Resource formats (%s / %s) don't match.\n",
3879 debug_d3dformat(dst_resource->format->id),
3880 debug_d3dformat(src_resource->format->id));
3881 return;
3884 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3886 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3887 return;
3890 dst_texture = wined3d_texture_from_resource(dst_resource);
3891 src_texture = wined3d_texture_from_resource(src_resource);
3893 if (src_texture->layer_count != dst_texture->layer_count
3894 || src_texture->level_count != dst_texture->level_count)
3896 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
3897 dst_texture->layer_count, dst_texture->level_count,
3898 src_texture->layer_count, src_texture->level_count);
3899 return;
3902 count = dst_texture->layer_count * dst_texture->level_count;
3903 for (i = 0; i < count; ++i)
3905 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
3906 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
3908 SetRect(&dst_rect, 0, 0, dst_surface->resource.width, dst_surface->resource.height);
3909 SetRect(&src_rect, 0, 0, src_surface->resource.width, src_surface->resource.height);
3910 if (FAILED(hr = wined3d_surface_blt(dst_surface, &dst_rect,
3911 src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
3912 ERR("Failed to blit, sub-resource %u, hr %#x.\n", i, hr);
3916 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
3917 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
3918 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
3919 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3921 struct wined3d_surface *dst_surface, *src_surface;
3922 struct wined3d_texture *dst_texture, *src_texture;
3923 struct wined3d_resource *tmp;
3924 RECT dst_rect, src_rect;
3925 HRESULT hr;
3927 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3928 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
3929 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3930 src_resource, src_sub_resource_idx, debug_box(src_box));
3932 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
3934 WARN("Source and destination are the same sub-resource.\n");
3935 return WINED3DERR_INVALIDCALL;
3938 if (src_resource->type != dst_resource->type)
3940 WARN("Resource types (%s / %s) don't match.\n",
3941 debug_d3dresourcetype(dst_resource->type),
3942 debug_d3dresourcetype(src_resource->type));
3943 return WINED3DERR_INVALIDCALL;
3946 if (src_resource->format->id != dst_resource->format->id)
3948 WARN("Resource formats (%s / %s) don't match.\n",
3949 debug_d3dformat(dst_resource->format->id),
3950 debug_d3dformat(src_resource->format->id));
3951 return WINED3DERR_INVALIDCALL;
3954 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
3956 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(dst_resource->type));
3957 return WINED3DERR_INVALIDCALL;
3960 dst_texture = wined3d_texture_from_resource(dst_resource);
3961 if (!(tmp = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
3963 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
3964 return WINED3DERR_INVALIDCALL;
3966 dst_surface = surface_from_resource(tmp);
3968 src_texture = wined3d_texture_from_resource(src_resource);
3969 if (!(tmp = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx)))
3971 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
3972 return WINED3DERR_INVALIDCALL;
3974 src_surface = surface_from_resource(tmp);
3976 if (src_box)
3978 if (src_box->front >= src_box->back)
3980 WARN("Invalid box %s specified.\n", debug_box(src_box));
3981 return WINED3DERR_INVALIDCALL;
3984 src_rect.left = src_box->left;
3985 src_rect.top = src_box->top;
3986 src_rect.right = src_box->right;
3987 src_rect.bottom = src_box->bottom;
3989 else
3991 src_rect.left = 0;
3992 src_rect.top = 0;
3993 src_rect.right = src_surface->resource.width;
3994 src_rect.bottom = src_surface->resource.height;
3997 dst_rect.left = dst_x;
3998 dst_rect.top = dst_y;
3999 dst_rect.right = dst_x + (src_rect.right - src_rect.left);
4000 dst_rect.bottom = dst_y + (src_rect.bottom - src_rect.top);
4002 if (FAILED(hr = wined3d_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, 0, NULL, WINED3D_TEXF_POINT)))
4003 WARN("Failed to blit, hr %#x.\n", hr);
4005 return hr;
4008 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4009 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4010 unsigned int depth_pitch)
4012 struct wined3d_resource *sub_resource;
4013 const struct wined3d_gl_info *gl_info;
4014 struct wined3d_const_bo_address addr;
4015 struct wined3d_context *context;
4016 struct wined3d_texture *texture;
4017 struct wined3d_surface *surface;
4018 POINT dst_point;
4019 RECT src_rect;
4021 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4022 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4024 if (resource->type == WINED3D_RTYPE_BUFFER)
4026 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4027 HRESULT hr;
4029 if (sub_resource_idx > 0)
4031 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4032 return;
4035 if (FAILED(hr = wined3d_buffer_upload_data(buffer, box, data)))
4036 WARN("Failed to update buffer data, hr %#x.\n", hr);
4038 return;
4041 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4043 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4044 return;
4047 texture = wined3d_texture_from_resource(resource);
4048 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4050 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4051 return;
4053 surface = surface_from_resource(sub_resource);
4055 src_rect.left = 0;
4056 src_rect.top = 0;
4057 if (box)
4059 if (box->left >= box->right || box->right > sub_resource->width
4060 || box->top >= box->bottom || box->bottom > sub_resource->height
4061 || box->front >= box->back)
4063 WARN("Invalid box %s specified.\n", debug_box(box));
4064 return;
4067 src_rect.right = box->right - box->left;
4068 src_rect.bottom = box->bottom - box->top;
4069 dst_point.x = box->left;
4070 dst_point.y = box->top;
4072 else
4074 src_rect.right = sub_resource->width;
4075 src_rect.bottom = sub_resource->height;
4076 dst_point.x = 0;
4077 dst_point.y = 0;
4080 addr.buffer_object = 0;
4081 addr.addr = data;
4083 context = context_acquire(resource->device, NULL);
4084 gl_info = context->gl_info;
4086 /* Only load the surface for partial updates. */
4087 if (!dst_point.x && !dst_point.y && src_rect.right == sub_resource->width
4088 && src_rect.bottom == sub_resource->height)
4089 wined3d_texture_prepare_texture(texture, context, FALSE);
4090 else
4091 surface_load_location(surface, context, WINED3D_LOCATION_TEXTURE_RGB);
4092 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
4094 wined3d_surface_upload_data(surface, gl_info, resource->format,
4095 &src_rect, row_pitch, &dst_point, FALSE, &addr);
4097 context_release(context);
4099 surface_validate_location(surface, WINED3D_LOCATION_TEXTURE_RGB);
4100 surface_invalidate_location(surface, ~WINED3D_LOCATION_TEXTURE_RGB);
4103 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4104 struct wined3d_rendertarget_view *view, const RECT *rect, const struct wined3d_color *color)
4106 struct wined3d_resource *resource;
4107 RECT r;
4109 TRACE("device %p, view %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4110 device, view, wine_dbgstr_rect(rect), color->r, color->g, color->b, color->a);
4112 resource = view->resource;
4113 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4115 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4116 return WINED3DERR_INVALIDCALL;
4119 if (view->depth > 1)
4121 FIXME("Layered clears not implemented.\n");
4122 return WINED3DERR_INVALIDCALL;
4125 if (!rect)
4127 SetRect(&r, 0, 0, view->width, view->height);
4128 rect = &r;
4131 resource = wined3d_texture_get_sub_resource(wined3d_texture_from_resource(resource), view->sub_resource_idx);
4133 return surface_color_fill(surface_from_resource(resource), rect, color);
4136 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4137 unsigned int view_idx)
4139 TRACE("device %p, view_idx %u.\n", device, view_idx);
4141 if (view_idx >= device->adapter->gl_info.limits.buffers)
4143 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4144 return NULL;
4147 return device->fb.render_targets[view_idx];
4150 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4152 TRACE("device %p.\n", device);
4154 return device->fb.depth_stencil;
4157 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4158 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4160 struct wined3d_rendertarget_view *prev;
4162 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4163 device, view_idx, view, set_viewport);
4165 if (view_idx >= device->adapter->gl_info.limits.buffers)
4167 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4168 return WINED3DERR_INVALIDCALL;
4171 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4173 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4174 return WINED3DERR_INVALIDCALL;
4177 /* Set the viewport and scissor rectangles, if requested. Tests show that
4178 * stateblock recording is ignored, the change goes directly into the
4179 * primary stateblock. */
4180 if (!view_idx && set_viewport)
4182 struct wined3d_state *state = &device->state;
4184 state->viewport.x = 0;
4185 state->viewport.y = 0;
4186 state->viewport.width = view->width;
4187 state->viewport.height = view->height;
4188 state->viewport.min_z = 0.0f;
4189 state->viewport.max_z = 1.0f;
4190 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4192 state->scissor_rect.top = 0;
4193 state->scissor_rect.left = 0;
4194 state->scissor_rect.right = view->width;
4195 state->scissor_rect.bottom = view->height;
4196 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4200 prev = device->fb.render_targets[view_idx];
4201 if (view == prev)
4202 return WINED3D_OK;
4204 if (view)
4205 wined3d_rendertarget_view_incref(view);
4206 device->fb.render_targets[view_idx] = view;
4207 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4208 /* Release after the assignment, to prevent device_resource_released()
4209 * from seeing the surface as still in use. */
4210 if (prev)
4211 wined3d_rendertarget_view_decref(prev);
4213 return WINED3D_OK;
4216 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4218 struct wined3d_rendertarget_view *prev;
4220 TRACE("device %p, view %p.\n", device, view);
4222 prev = device->fb.depth_stencil;
4223 if (prev == view)
4225 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4226 return;
4229 if ((device->fb.depth_stencil = view))
4230 wined3d_rendertarget_view_incref(view);
4231 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4232 if (prev)
4233 wined3d_rendertarget_view_decref(prev);
4236 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4237 struct wined3d_surface *cursor_image)
4239 struct wined3d_sub_resource_data data;
4240 struct wined3d_resource_desc desc;
4241 struct wined3d_map_desc map_desc;
4242 struct wined3d_texture *texture;
4243 HRESULT hr;
4245 if (FAILED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4247 ERR("Failed to map source surface.\n");
4248 return NULL;
4251 data.data = map_desc.data;
4252 data.row_pitch = map_desc.row_pitch;
4253 data.slice_pitch = map_desc.slice_pitch;
4255 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4256 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4257 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4258 desc.multisample_quality = 0;
4259 desc.usage = WINED3DUSAGE_DYNAMIC;
4260 desc.pool = WINED3D_POOL_DEFAULT;
4261 desc.width = cursor_image->resource.width;
4262 desc.height = cursor_image->resource.height;
4263 desc.depth = 1;
4264 desc.size = 0;
4266 hr = wined3d_texture_create(device, &desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4267 &data, NULL, &wined3d_null_parent_ops, &texture);
4268 wined3d_surface_unmap(cursor_image);
4269 if (FAILED(hr))
4271 ERR("Failed to create cursor texture.\n");
4272 return NULL;
4275 return texture;
4278 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4279 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4281 struct wined3d_display_mode mode;
4282 struct wined3d_map_desc map_desc;
4283 struct wined3d_resource *sub_resource;
4284 struct wined3d_surface *cursor_image;
4285 HRESULT hr;
4287 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4288 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4290 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx))
4291 || sub_resource->type != WINED3D_RTYPE_SURFACE)
4292 return WINED3DERR_INVALIDCALL;
4294 cursor_image = surface_from_resource(sub_resource);
4296 if (device->cursor_texture)
4298 wined3d_texture_decref(device->cursor_texture);
4299 device->cursor_texture = NULL;
4302 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4304 WARN("Texture %p has invalid format %s.\n",
4305 texture, debug_d3dformat(texture->resource.format->id));
4306 return WINED3DERR_INVALIDCALL;
4309 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4311 ERR("Failed to get display mode, hr %#x.\n", hr);
4312 return WINED3DERR_INVALIDCALL;
4315 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4317 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4318 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4319 mode.width, mode.height);
4320 return WINED3DERR_INVALIDCALL;
4323 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4325 /* Do not store the surface's pointer because the application may
4326 * release it after setting the cursor image. Windows doesn't
4327 * addref the set surface, so we can't do this either without
4328 * creating circular refcount dependencies. */
4329 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, cursor_image)))
4331 ERR("Failed to create cursor texture.\n");
4332 return WINED3DERR_INVALIDCALL;
4335 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4337 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4338 ICONINFO cursor_info;
4339 DWORD *mask_bits;
4340 HCURSOR cursor;
4342 /* 32-bit user32 cursors ignore the alpha channel if it's all
4343 * zeroes, and use the mask instead. Fill the mask with all ones
4344 * to ensure we still get a fully transparent cursor. */
4345 if (!(mask_bits = HeapAlloc(GetProcessHeap(), 0, mask_size)))
4346 return E_OUTOFMEMORY;
4347 memset(mask_bits, 0xff, mask_size);
4349 wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4350 cursor_info.fIcon = FALSE;
4351 cursor_info.xHotspot = x_hotspot;
4352 cursor_info.yHotspot = y_hotspot;
4353 cursor_info.hbmMask = CreateBitmap(cursor_image->resource.width,
4354 cursor_image->resource.height, 1, 1, mask_bits);
4355 cursor_info.hbmColor = CreateBitmap(cursor_image->resource.width,
4356 cursor_image->resource.height, 1, 32, map_desc.data);
4357 wined3d_surface_unmap(cursor_image);
4359 /* Create our cursor and clean up. */
4360 cursor = CreateIconIndirect(&cursor_info);
4361 if (cursor_info.hbmMask)
4362 DeleteObject(cursor_info.hbmMask);
4363 if (cursor_info.hbmColor)
4364 DeleteObject(cursor_info.hbmColor);
4365 if (device->hardwareCursor)
4366 DestroyCursor(device->hardwareCursor);
4367 device->hardwareCursor = cursor;
4368 if (device->bCursorVisible)
4369 SetCursor(cursor);
4371 HeapFree(GetProcessHeap(), 0, mask_bits);
4374 TRACE("New cursor dimensions are %ux%u.\n", cursor_image->resource.width, cursor_image->resource.height);
4375 device->cursorWidth = cursor_image->resource.width;
4376 device->cursorHeight = cursor_image->resource.height;
4377 device->xHotSpot = x_hotspot;
4378 device->yHotSpot = y_hotspot;
4380 return WINED3D_OK;
4383 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4384 int x_screen_space, int y_screen_space, DWORD flags)
4386 TRACE("device %p, x %d, y %d, flags %#x.\n",
4387 device, x_screen_space, y_screen_space, flags);
4389 device->xScreenSpace = x_screen_space;
4390 device->yScreenSpace = y_screen_space;
4392 if (device->hardwareCursor)
4394 POINT pt;
4396 GetCursorPos( &pt );
4397 if (x_screen_space == pt.x && y_screen_space == pt.y)
4398 return;
4399 SetCursorPos( x_screen_space, y_screen_space );
4401 /* Switch to the software cursor if position diverges from the hardware one. */
4402 GetCursorPos( &pt );
4403 if (x_screen_space != pt.x || y_screen_space != pt.y)
4405 if (device->bCursorVisible) SetCursor( NULL );
4406 DestroyCursor( device->hardwareCursor );
4407 device->hardwareCursor = 0;
4412 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4414 BOOL oldVisible = device->bCursorVisible;
4416 TRACE("device %p, show %#x.\n", device, show);
4419 * When ShowCursor is first called it should make the cursor appear at the OS's last
4420 * known cursor position.
4422 if (show && !oldVisible)
4424 POINT pt;
4425 GetCursorPos(&pt);
4426 device->xScreenSpace = pt.x;
4427 device->yScreenSpace = pt.y;
4430 if (device->hardwareCursor)
4432 device->bCursorVisible = show;
4433 if (show)
4434 SetCursor(device->hardwareCursor);
4435 else
4436 SetCursor(NULL);
4438 else if (device->cursor_texture)
4440 device->bCursorVisible = show;
4443 return oldVisible;
4446 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4448 struct wined3d_resource *resource, *cursor;
4450 TRACE("device %p.\n", device);
4452 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4454 TRACE("Checking resource %p for eviction.\n", resource);
4456 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4458 TRACE("Evicting %p.\n", resource);
4459 resource->resource_ops->resource_unload(resource);
4463 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4464 device_invalidate_state(device, STATE_STREAMSRC);
4467 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4469 struct wined3d_resource *resource, *cursor;
4470 const struct wined3d_gl_info *gl_info;
4471 struct wined3d_context *context;
4472 struct wined3d_shader *shader;
4474 context = context_acquire(device, NULL);
4475 gl_info = context->gl_info;
4477 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4479 TRACE("Unloading resource %p.\n", resource);
4481 resource->resource_ops->resource_unload(resource);
4484 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4486 device->shader_backend->shader_destroy(shader);
4489 if (device->depth_blt_texture)
4491 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4492 device->depth_blt_texture = 0;
4495 device->blitter->free_private(device);
4496 device->shader_backend->shader_free_private(device);
4497 destroy_dummy_textures(device, gl_info);
4498 destroy_default_sampler(device);
4500 context_release(context);
4502 while (device->context_count)
4504 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4507 HeapFree(GetProcessHeap(), 0, swapchain->context);
4508 swapchain->context = NULL;
4511 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4513 struct wined3d_context *context;
4514 struct wined3d_surface *target;
4515 HRESULT hr;
4517 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4518 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4520 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4521 return hr;
4524 if (FAILED(hr = device->blitter->alloc_private(device)))
4526 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4527 device->shader_backend->shader_free_private(device);
4528 return hr;
4531 /* Recreate the primary swapchain's context */
4532 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4533 if (!swapchain->context)
4535 ERR("Failed to allocate memory for swapchain context array.\n");
4536 device->blitter->free_private(device);
4537 device->shader_backend->shader_free_private(device);
4538 return E_OUTOFMEMORY;
4541 target = swapchain->back_buffers
4542 ? surface_from_resource(wined3d_texture_get_sub_resource(swapchain->back_buffers[0], 0))
4543 : surface_from_resource(wined3d_texture_get_sub_resource(swapchain->front_buffer, 0));
4544 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4546 WARN("Failed to create context.\n");
4547 device->blitter->free_private(device);
4548 device->shader_backend->shader_free_private(device);
4549 HeapFree(GetProcessHeap(), 0, swapchain->context);
4550 return E_FAIL;
4553 swapchain->context[0] = context;
4554 swapchain->num_contexts = 1;
4555 create_dummy_textures(device, context);
4556 create_default_sampler(device);
4557 context_release(context);
4559 return WINED3D_OK;
4562 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4563 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4564 wined3d_device_reset_cb callback, BOOL reset_state)
4566 struct wined3d_rendertarget_view_desc view_desc;
4567 struct wined3d_resource *resource, *cursor;
4568 struct wined3d_swapchain *swapchain;
4569 struct wined3d_display_mode m;
4570 BOOL DisplayModeChanged;
4571 HRESULT hr = WINED3D_OK;
4572 unsigned int i;
4574 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4575 device, swapchain_desc, mode, callback, reset_state);
4577 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4579 ERR("Failed to get the first implicit swapchain.\n");
4580 return WINED3DERR_INVALIDCALL;
4582 DisplayModeChanged = swapchain->reapply_mode;
4584 if (reset_state)
4586 if (device->logo_texture)
4588 wined3d_texture_decref(device->logo_texture);
4589 device->logo_texture = NULL;
4591 if (device->cursor_texture)
4593 wined3d_texture_decref(device->cursor_texture);
4594 device->cursor_texture = NULL;
4596 state_unbind_resources(&device->state);
4599 if (device->fb.render_targets)
4601 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4603 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4606 wined3d_device_set_depth_stencil_view(device, NULL);
4608 if (device->onscreen_depth_stencil)
4610 wined3d_texture_decref(device->onscreen_depth_stencil->container);
4611 device->onscreen_depth_stencil = NULL;
4614 if (reset_state)
4616 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4618 TRACE("Enumerating resource %p.\n", resource);
4619 if (FAILED(hr = callback(resource)))
4620 return hr;
4624 TRACE("New params:\n");
4625 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4626 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4627 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4628 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4629 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4630 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4631 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4632 TRACE("device_window %p\n", swapchain_desc->device_window);
4633 TRACE("windowed %#x\n", swapchain_desc->windowed);
4634 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4635 if (swapchain_desc->enable_auto_depth_stencil)
4636 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4637 TRACE("flags %#x\n", swapchain_desc->flags);
4638 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4639 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4640 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4642 /* No special treatment of these parameters. Just store them */
4643 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4644 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4645 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4646 swapchain->desc.flags = swapchain_desc->flags;
4647 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4648 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4649 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4651 if (swapchain_desc->device_window
4652 && swapchain_desc->device_window != swapchain->desc.device_window)
4654 TRACE("Changing the device window from %p to %p.\n",
4655 swapchain->desc.device_window, swapchain_desc->device_window);
4656 swapchain->desc.device_window = swapchain_desc->device_window;
4657 swapchain->device_window = swapchain_desc->device_window;
4658 wined3d_swapchain_set_window(swapchain, NULL);
4661 if (mode)
4663 DisplayModeChanged = TRUE;
4664 m = *mode;
4666 else if (swapchain_desc->windowed)
4668 m = swapchain->original_mode;
4670 else
4672 m.width = swapchain_desc->backbuffer_width;
4673 m.height = swapchain_desc->backbuffer_height;
4674 m.refresh_rate = swapchain_desc->refresh_rate;
4675 m.format_id = swapchain_desc->backbuffer_format;
4676 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
4678 if ((m.width != swapchain->desc.backbuffer_width
4679 || m.height != swapchain->desc.backbuffer_height))
4680 DisplayModeChanged = TRUE;
4683 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4684 || DisplayModeChanged)
4686 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
4688 WARN("Failed to set display mode, hr %#x.\n", hr);
4689 return WINED3DERR_INVALIDCALL;
4692 if (!swapchain_desc->windowed)
4694 if (swapchain->desc.windowed)
4696 HWND focus_window = device->create_parms.focus_window;
4697 if (!focus_window)
4698 focus_window = swapchain_desc->device_window;
4699 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
4701 ERR("Failed to acquire focus window, hr %#x.\n", hr);
4702 return hr;
4705 /* switch from windowed to fs */
4706 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4707 swapchain_desc->backbuffer_width,
4708 swapchain_desc->backbuffer_height);
4710 else
4712 /* Fullscreen -> fullscreen mode change */
4713 MoveWindow(swapchain->device_window, 0, 0,
4714 swapchain_desc->backbuffer_width,
4715 swapchain_desc->backbuffer_height,
4716 TRUE);
4718 swapchain->d3d_mode = m;
4720 else if (!swapchain->desc.windowed)
4722 /* Fullscreen -> windowed switch */
4723 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
4724 wined3d_device_release_focus_window(device);
4726 swapchain->desc.windowed = swapchain_desc->windowed;
4728 else if (!swapchain_desc->windowed)
4730 DWORD style = device->style;
4731 DWORD exStyle = device->exStyle;
4732 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4733 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4734 * Reset to clear up their mess. Guild Wars also loses the device during that.
4736 device->style = 0;
4737 device->exStyle = 0;
4738 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4739 swapchain_desc->backbuffer_width,
4740 swapchain_desc->backbuffer_height);
4741 device->style = style;
4742 device->exStyle = exStyle;
4745 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4746 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4747 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4748 return hr;
4750 if (device->auto_depth_stencil_view)
4752 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4753 device->auto_depth_stencil_view = NULL;
4755 if (swapchain->desc.enable_auto_depth_stencil)
4757 struct wined3d_resource_desc texture_desc;
4758 struct wined3d_texture *texture;
4760 TRACE("Creating the depth stencil buffer\n");
4762 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4763 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4764 texture_desc.multisample_type = swapchain->desc.multisample_type;
4765 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4766 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4767 texture_desc.pool = WINED3D_POOL_DEFAULT;
4768 texture_desc.width = swapchain->desc.backbuffer_width;
4769 texture_desc.height = swapchain->desc.backbuffer_height;
4770 texture_desc.depth = 1;
4771 texture_desc.size = 0;
4773 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4774 device->device_parent, &texture_desc, &texture)))
4776 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4777 return WINED3DERR_INVALIDCALL;
4780 view_desc.format_id = texture->resource.format->id;
4781 view_desc.u.texture.level_idx = 0;
4782 view_desc.u.texture.layer_idx = 0;
4783 view_desc.u.texture.layer_count = 1;
4784 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4785 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4786 wined3d_texture_decref(texture);
4787 if (FAILED(hr))
4789 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4790 return hr;
4793 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4796 if (device->back_buffer_view)
4798 wined3d_rendertarget_view_decref(device->back_buffer_view);
4799 device->back_buffer_view = NULL;
4801 if (swapchain->desc.backbuffer_count)
4803 view_desc.format_id = swapchain_desc->backbuffer_format;
4804 view_desc.u.texture.level_idx = 0;
4805 view_desc.u.texture.layer_idx = 0;
4806 view_desc.u.texture.layer_count = 1;
4807 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, &swapchain->back_buffers[0]->resource,
4808 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4810 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4811 return hr;
4815 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4817 if (reset_state)
4819 TRACE("Resetting stateblock.\n");
4820 if (device->recording)
4822 wined3d_stateblock_decref(device->recording);
4823 device->recording = NULL;
4825 wined3d_cs_emit_reset_state(device->cs);
4826 state_cleanup(&device->state);
4828 if (device->d3d_initialized)
4829 delete_opengl_contexts(device, swapchain);
4831 if (FAILED(hr = state_init(&device->state, &device->fb, &device->adapter->gl_info,
4832 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
4833 ERR("Failed to initialize device state, hr %#x.\n", hr);
4834 device->update_state = &device->state;
4836 device_init_swapchain_state(device, swapchain);
4838 else if (device->back_buffer_view)
4840 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4841 struct wined3d_state *state = &device->state;
4843 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4845 /* Note the min_z / max_z is not reset. */
4846 state->viewport.x = 0;
4847 state->viewport.y = 0;
4848 state->viewport.width = view->width;
4849 state->viewport.height = view->height;
4850 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4852 state->scissor_rect.top = 0;
4853 state->scissor_rect.left = 0;
4854 state->scissor_rect.right = view->width;
4855 state->scissor_rect.bottom = view->height;
4856 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4859 if (reset_state && device->d3d_initialized)
4860 hr = create_primary_opengl_context(device, swapchain);
4862 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4863 * first use
4865 return hr;
4868 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4870 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4872 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4874 return WINED3D_OK;
4878 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4879 struct wined3d_device_creation_parameters *parameters)
4881 TRACE("device %p, parameters %p.\n", device, parameters);
4883 *parameters = device->create_parms;
4886 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4888 TRACE("device %p.\n", device);
4890 return device->wined3d;
4893 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4894 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4896 struct wined3d_swapchain *swapchain;
4898 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
4899 device, swapchain_idx, flags, ramp);
4901 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4902 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
4905 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
4906 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
4908 struct wined3d_swapchain *swapchain;
4910 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
4911 device, swapchain_idx, ramp);
4913 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4914 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
4917 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
4919 TRACE("device %p, resource %p.\n", device, resource);
4921 list_add_head(&device->resources, &resource->resource_list_entry);
4924 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
4926 TRACE("device %p, resource %p.\n", device, resource);
4928 list_remove(&resource->resource_list_entry);
4931 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
4933 enum wined3d_resource_type type = resource->type;
4934 unsigned int i;
4936 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
4938 context_resource_released(device, resource, type);
4940 switch (type)
4942 case WINED3D_RTYPE_SURFACE:
4944 struct wined3d_surface *surface = surface_from_resource(resource);
4946 if (!device->d3d_initialized) break;
4948 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4950 if (wined3d_rendertarget_view_get_surface(device->fb.render_targets[i]) == surface)
4952 ERR("Surface %p is still in use as render target %u.\n", surface, i);
4953 device->fb.render_targets[i] = NULL;
4957 if (wined3d_rendertarget_view_get_surface(device->fb.depth_stencil) == surface)
4959 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
4960 device->fb.depth_stencil = NULL;
4963 break;
4965 case WINED3D_RTYPE_TEXTURE_2D:
4966 case WINED3D_RTYPE_TEXTURE_3D:
4967 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4969 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
4971 if (device->state.textures[i] == texture)
4973 ERR("Texture %p is still in use, stage %u.\n", texture, i);
4974 device->state.textures[i] = NULL;
4977 if (device->recording && device->update_state->textures[i] == texture)
4979 ERR("Texture %p is still in use by recording stateblock %p, stage %u.\n",
4980 texture, device->recording, i);
4981 device->update_state->textures[i] = NULL;
4984 break;
4986 case WINED3D_RTYPE_BUFFER:
4988 struct wined3d_buffer *buffer = buffer_from_resource(resource);
4990 for (i = 0; i < MAX_STREAMS; ++i)
4992 if (device->state.streams[i].buffer == buffer)
4994 ERR("Buffer %p is still in use, stream %u.\n", buffer, i);
4995 device->state.streams[i].buffer = NULL;
4998 if (device->recording && device->update_state->streams[i].buffer == buffer)
5000 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5001 buffer, device->recording, i);
5002 device->update_state->streams[i].buffer = NULL;
5006 if (device->state.index_buffer == buffer)
5008 ERR("Buffer %p is still in use as index buffer.\n", buffer);
5009 device->state.index_buffer = NULL;
5012 if (device->recording && device->update_state->index_buffer == buffer)
5014 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5015 buffer, device->recording);
5016 device->update_state->index_buffer = NULL;
5019 break;
5021 default:
5022 break;
5025 /* Remove the resource from the resourceStore */
5026 device_resource_remove(device, resource);
5028 TRACE("Resource released.\n");
5031 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5033 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5035 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5038 static const struct wine_rb_functions wined3d_sampler_rb_functions =
5040 wined3d_rb_alloc,
5041 wined3d_rb_realloc,
5042 wined3d_rb_free,
5043 wined3d_sampler_compare,
5046 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5047 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5048 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5050 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5051 const struct fragment_pipeline *fragment_pipeline;
5052 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5053 unsigned int i;
5054 HRESULT hr;
5056 device->ref = 1;
5057 device->wined3d = wined3d;
5058 wined3d_incref(device->wined3d);
5059 device->adapter = wined3d->adapter_count ? adapter : NULL;
5060 device->device_parent = device_parent;
5061 list_init(&device->resources);
5062 list_init(&device->shaders);
5063 device->surface_alignment = surface_alignment;
5065 /* Save the creation parameters. */
5066 device->create_parms.adapter_idx = adapter_idx;
5067 device->create_parms.device_type = device_type;
5068 device->create_parms.focus_window = focus_window;
5069 device->create_parms.flags = flags;
5071 device->shader_backend = adapter->shader_backend;
5073 vertex_pipeline = adapter->vertex_pipe;
5075 fragment_pipeline = adapter->fragment_pipe;
5077 if (wine_rb_init(&device->samplers, &wined3d_sampler_rb_functions) == -1)
5079 ERR("Failed to initialize sampler rbtree.\n");
5080 return E_OUTOFMEMORY;
5083 if (vertex_pipeline->vp_states && fragment_pipeline->states
5084 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5085 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5086 fragment_pipeline, misc_state_template)))
5088 ERR("Failed to compile state table, hr %#x.\n", hr);
5089 wine_rb_destroy(&device->samplers, NULL, NULL);
5090 wined3d_decref(device->wined3d);
5091 return hr;
5094 device->blitter = adapter->blitter;
5096 if (FAILED(hr = state_init(&device->state, &device->fb, &adapter->gl_info,
5097 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT)))
5099 ERR("Failed to initialize device state, hr %#x.\n", hr);
5100 goto err;
5102 device->update_state = &device->state;
5104 if (!(device->cs = wined3d_cs_create(device)))
5106 WARN("Failed to create command stream.\n");
5107 state_cleanup(&device->state);
5108 hr = E_FAIL;
5109 goto err;
5112 return WINED3D_OK;
5114 err:
5115 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5117 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5119 wine_rb_destroy(&device->samplers, NULL, NULL);
5120 wined3d_decref(device->wined3d);
5121 return hr;
5125 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5127 DWORD rep = device->StateTable[state].representative;
5128 struct wined3d_context *context;
5129 DWORD idx;
5130 BYTE shift;
5131 UINT i;
5133 for (i = 0; i < device->context_count; ++i)
5135 context = device->contexts[i];
5136 if(isStateDirty(context, rep)) continue;
5138 context->dirtyArray[context->numDirtyEntries++] = rep;
5139 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5140 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5141 context->isStateDirty[idx] |= (1u << shift);
5145 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5146 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5148 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5150 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5151 window, message, wparam, lparam);
5152 if (unicode)
5153 return DefWindowProcW(window, message, wparam, lparam);
5154 else
5155 return DefWindowProcA(window, message, wparam, lparam);
5158 if (message == WM_DESTROY)
5160 TRACE("unregister window %p.\n", window);
5161 wined3d_unregister_window(window);
5163 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5164 ERR("Window %p is not the focus window for device %p.\n", window, device);
5166 else if (message == WM_DISPLAYCHANGE)
5168 device->device_parent->ops->mode_changed(device->device_parent);
5170 else if (message == WM_ACTIVATEAPP)
5172 UINT i;
5174 for (i = 0; i < device->swapchain_count; i++)
5175 wined3d_swapchain_activate(device->swapchains[i], wparam);
5177 device->device_parent->ops->activate(device->device_parent, wparam);
5179 else if (message == WM_SYSCOMMAND)
5181 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5183 if (unicode)
5184 DefWindowProcW(window, message, wparam, lparam);
5185 else
5186 DefWindowProcA(window, message, wparam, lparam);
5190 if (unicode)
5191 return CallWindowProcW(proc, window, message, wparam, lparam);
5192 else
5193 return CallWindowProcA(proc, window, message, wparam, lparam);