wined3d: Get rid of the redundant "device" parameter to context_destroy().
[wine.git] / dlls / wined3d / device.c
blobe25565da7f5cb24012fd055da427201b8435d389
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);
38 WINE_DECLARE_DEBUG_CHANNEL(winediag);
40 /* Define the default light parameters as specified by MSDN. */
41 const struct wined3d_light WINED3D_default_light =
43 WINED3D_LIGHT_DIRECTIONAL, /* Type */
44 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
46 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
47 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
48 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
49 0.0f, /* Range */
50 0.0f, /* Falloff */
51 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
52 0.0f, /* Theta */
53 0.0f /* Phi */
56 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
57 * actually have the same values in GL and D3D. */
58 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
60 switch (primitive_type)
62 case WINED3D_PT_POINTLIST:
63 return GL_POINTS;
65 case WINED3D_PT_LINELIST:
66 return GL_LINES;
68 case WINED3D_PT_LINESTRIP:
69 return GL_LINE_STRIP;
71 case WINED3D_PT_TRIANGLELIST:
72 return GL_TRIANGLES;
74 case WINED3D_PT_TRIANGLESTRIP:
75 return GL_TRIANGLE_STRIP;
77 case WINED3D_PT_TRIANGLEFAN:
78 return GL_TRIANGLE_FAN;
80 case WINED3D_PT_LINELIST_ADJ:
81 return GL_LINES_ADJACENCY_ARB;
83 case WINED3D_PT_LINESTRIP_ADJ:
84 return GL_LINE_STRIP_ADJACENCY_ARB;
86 case WINED3D_PT_TRIANGLELIST_ADJ:
87 return GL_TRIANGLES_ADJACENCY_ARB;
89 case WINED3D_PT_TRIANGLESTRIP_ADJ:
90 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
92 case WINED3D_PT_PATCH:
93 return GL_PATCHES;
95 default:
96 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
97 case WINED3D_PT_UNDEFINED:
98 return ~0u;
102 enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
104 switch (primitive_type)
106 case GL_POINTS:
107 return WINED3D_PT_POINTLIST;
109 case GL_LINES:
110 return WINED3D_PT_LINELIST;
112 case GL_LINE_STRIP:
113 return WINED3D_PT_LINESTRIP;
115 case GL_TRIANGLES:
116 return WINED3D_PT_TRIANGLELIST;
118 case GL_TRIANGLE_STRIP:
119 return WINED3D_PT_TRIANGLESTRIP;
121 case GL_TRIANGLE_FAN:
122 return WINED3D_PT_TRIANGLEFAN;
124 case GL_LINES_ADJACENCY_ARB:
125 return WINED3D_PT_LINELIST_ADJ;
127 case GL_LINE_STRIP_ADJACENCY_ARB:
128 return WINED3D_PT_LINESTRIP_ADJ;
130 case GL_TRIANGLES_ADJACENCY_ARB:
131 return WINED3D_PT_TRIANGLELIST_ADJ;
133 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
134 return WINED3D_PT_TRIANGLESTRIP_ADJ;
136 case GL_PATCHES:
137 return WINED3D_PT_PATCH;
139 default:
140 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
141 case ~0u:
142 return WINED3D_PT_UNDEFINED;
146 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
148 struct wined3d_context **new_array;
150 TRACE("Adding context %p.\n", context);
152 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
154 ERR("Failed to grow the context array.\n");
155 return FALSE;
158 new_array[device->context_count++] = context;
159 device->contexts = new_array;
160 return TRUE;
163 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
165 struct wined3d_context **new_array;
166 BOOL found = FALSE;
167 UINT i;
169 TRACE("Removing context %p.\n", context);
171 for (i = 0; i < device->context_count; ++i)
173 if (device->contexts[i] == context)
175 found = TRUE;
176 break;
180 if (!found)
182 ERR("Context %p doesn't exist in context array.\n", context);
183 return;
186 if (!--device->context_count)
188 heap_free(device->contexts);
189 device->contexts = NULL;
190 return;
193 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
194 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
196 ERR("Failed to shrink context array. Oh well.\n");
197 return;
200 device->contexts = new_array;
203 static BOOL is_full_clear(const struct wined3d_texture *texture, unsigned int sub_resource_idx,
204 const RECT *draw_rect, const RECT *clear_rect)
206 unsigned int width, height, level;
208 level = sub_resource_idx % texture->level_count;
209 width = wined3d_texture_get_level_width(texture, level);
210 height = wined3d_texture_get_level_height(texture, level);
212 /* partial draw rect */
213 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
214 return FALSE;
216 /* partial clear rect */
217 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
218 || clear_rect->right < width || clear_rect->bottom < height))
219 return FALSE;
221 return TRUE;
224 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
225 UINT rect_count, const RECT *clear_rect, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
226 float depth, DWORD stencil)
228 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
229 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
230 const struct wined3d_state *state = &device->cs->state;
231 struct wined3d_texture *depth_stencil = NULL;
232 const struct wined3d_gl_info *gl_info;
233 struct wined3d_texture *target = NULL;
234 UINT drawable_width, drawable_height;
235 struct wined3d_color corrected_color;
236 struct wined3d_context *context;
237 GLbitfield clear_mask = 0;
238 BOOL render_offscreen;
239 unsigned int i;
241 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
243 target = texture_from_resource(rtv->resource);
244 context = context_acquire(device, target, rtv->sub_resource_idx);
246 else
248 context = context_acquire(device, NULL, 0);
251 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
252 depth_stencil = texture_from_resource(dsv->resource);
254 if (!context->valid)
256 context_release(context);
257 WARN("Invalid context, skipping clear.\n");
258 return;
260 gl_info = context->gl_info;
262 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
263 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
264 * for the cleared parts, and the untouched parts.
266 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
267 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
268 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
269 * checking all this if the dest surface is in the drawable anyway. */
270 for (i = 0; i < rt_count; ++i)
272 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
274 if (rtv && rtv->format->id != WINED3DFMT_NULL)
276 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
278 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rt, rtv->sub_resource_idx,
279 draw_rect, rect_count ? clear_rect : NULL))
280 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
281 else
282 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
286 if (target)
288 render_offscreen = context->render_offscreen;
289 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
291 else
293 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
295 render_offscreen = TRUE;
296 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
297 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
300 if (depth_stencil)
302 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
303 struct wined3d_texture *ds = wined3d_texture_from_resource(dsv->resource);
305 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
306 && !is_full_clear(ds, dsv->sub_resource_idx, draw_rect, rect_count ? clear_rect : NULL))
307 wined3d_texture_load_location(ds, dsv->sub_resource_idx, context, ds_location);
308 else
309 wined3d_texture_prepare_location(ds, dsv->sub_resource_idx, context, ds_location);
311 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
313 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, ds_location);
314 wined3d_texture_invalidate_location(ds, dsv->sub_resource_idx, ~ds_location);
318 if (!context_apply_clear_state(context, state, rt_count, fb))
320 context_release(context);
321 WARN("Failed to apply clear state, skipping clear.\n");
322 return;
325 /* Only set the values up once, as they are not changing. */
326 if (flags & WINED3DCLEAR_STENCIL)
328 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
330 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
331 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
333 gl_info->gl_ops.gl.p_glStencilMask(~0U);
334 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
335 gl_info->gl_ops.gl.p_glClearStencil(stencil);
336 checkGLcall("glClearStencil");
337 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
340 if (flags & WINED3DCLEAR_ZBUFFER)
342 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
343 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
344 gl_info->gl_ops.gl.p_glClearDepth(depth);
345 checkGLcall("glClearDepth");
346 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
349 if (flags & WINED3DCLEAR_TARGET)
351 for (i = 0; i < rt_count; ++i)
353 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
354 struct wined3d_texture *texture;
356 if (!rtv)
357 continue;
359 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
361 FIXME("Not supported on buffer resources.\n");
362 continue;
365 texture = texture_from_resource(rtv->resource);
366 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
367 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
370 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context, state, fb))
372 if (rt_count > 1)
373 WARN("Clearing multiple sRGB render targets with no GL_ARB_framebuffer_sRGB "
374 "support, this might cause graphical issues.\n");
376 corrected_color.r = color->r < wined3d_srgb_const1[0]
377 ? color->r * wined3d_srgb_const0[3]
378 : pow(color->r, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
379 - wined3d_srgb_const0[2];
380 corrected_color.r = min(max(corrected_color.r, 0.0f), 1.0f);
381 corrected_color.g = color->g < wined3d_srgb_const1[0]
382 ? color->g * wined3d_srgb_const0[3]
383 : pow(color->g, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
384 - wined3d_srgb_const0[2];
385 corrected_color.g = min(max(corrected_color.g, 0.0f), 1.0f);
386 corrected_color.b = color->b < wined3d_srgb_const1[0]
387 ? color->b * wined3d_srgb_const0[3]
388 : pow(color->b, wined3d_srgb_const0[0]) * wined3d_srgb_const0[1]
389 - wined3d_srgb_const0[2];
390 corrected_color.b = min(max(corrected_color.b, 0.0f), 1.0f);
391 color = &corrected_color;
394 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
395 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
396 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
397 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
398 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
399 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
400 checkGLcall("glClearColor");
401 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
404 if (!rect_count)
406 if (render_offscreen)
408 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
409 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
411 else
413 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
414 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
416 gl_info->gl_ops.gl.p_glClear(clear_mask);
418 else
420 RECT current_rect;
422 /* Now process each rect in turn. */
423 for (i = 0; i < rect_count; ++i)
425 /* Note that GL uses lower left, width/height. */
426 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
428 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
429 wine_dbgstr_rect(&clear_rect[i]),
430 wine_dbgstr_rect(&current_rect));
432 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
433 * The rectangle is not cleared, no error is returned, but further rectangles are
434 * still cleared if they are valid. */
435 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
437 TRACE("Rectangle with negative dimensions, ignoring.\n");
438 continue;
441 if (render_offscreen)
443 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
444 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
446 else
448 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
449 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
451 gl_info->gl_ops.gl.p_glClear(clear_mask);
454 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
455 checkGLcall("clear");
457 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
458 gl_info->gl_ops.gl.p_glFlush();
460 context_release(context);
463 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
465 ULONG refcount = InterlockedIncrement(&device->ref);
467 TRACE("%p increasing refcount to %u.\n", device, refcount);
469 return refcount;
472 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
474 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
476 ERR("Leftover sampler %p.\n", sampler);
479 void wined3d_device_cleanup(struct wined3d_device *device)
481 unsigned int i;
483 if (device->swapchain_count)
484 wined3d_device_uninit_3d(device);
486 wined3d_stateblock_state_cleanup(&device->stateblock_state);
488 wined3d_cs_destroy(device->cs);
490 if (device->recording && wined3d_stateblock_decref(device->recording))
491 ERR("Something's still holding the recording stateblock.\n");
492 device->recording = NULL;
494 state_cleanup(&device->state);
496 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
498 heap_free(device->multistate_funcs[i]);
499 device->multistate_funcs[i] = NULL;
502 if (!list_empty(&device->resources))
504 struct wined3d_resource *resource;
506 ERR("Device released with resources still bound.\n");
508 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
510 ERR("Leftover resource %p with type %s (%#x).\n",
511 resource, debug_d3dresourcetype(resource->type), resource->type);
515 if (device->contexts)
516 ERR("Context array not freed!\n");
517 if (device->hardwareCursor)
518 DestroyCursor(device->hardwareCursor);
519 device->hardwareCursor = 0;
521 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
523 wined3d_decref(device->wined3d);
524 device->wined3d = NULL;
527 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
529 ULONG refcount = InterlockedDecrement(&device->ref);
531 TRACE("%p decreasing refcount to %u.\n", device, refcount);
533 if (!refcount)
535 device->adapter->adapter_ops->adapter_destroy_device(device);
536 TRACE("Destroyed device %p.\n", device);
539 return refcount;
542 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
544 TRACE("device %p.\n", device);
546 return device->swapchain_count;
549 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
551 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
553 if (swapchain_idx >= device->swapchain_count)
555 WARN("swapchain_idx %u >= swapchain_count %u.\n",
556 swapchain_idx, device->swapchain_count);
557 return NULL;
560 return device->swapchains[swapchain_idx];
563 static void device_load_logo(struct wined3d_device *device, const char *filename)
565 struct wined3d_color_key color_key;
566 struct wined3d_resource_desc desc;
567 HBITMAP hbm;
568 BITMAP bm;
569 HRESULT hr;
570 HDC dcb = NULL, dcs = NULL;
572 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
574 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
575 return;
577 GetObjectA(hbm, sizeof(BITMAP), &bm);
579 if (!(dcb = CreateCompatibleDC(NULL)))
580 goto out;
581 SelectObject(dcb, hbm);
583 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
584 desc.format = WINED3DFMT_B5G6R5_UNORM;
585 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
586 desc.multisample_quality = 0;
587 desc.usage = WINED3DUSAGE_DYNAMIC;
588 desc.bind_flags = 0;
589 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
590 desc.width = bm.bmWidth;
591 desc.height = bm.bmHeight;
592 desc.depth = 1;
593 desc.size = 0;
594 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
595 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
597 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
598 goto out;
601 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
603 wined3d_texture_decref(device->logo_texture);
604 device->logo_texture = NULL;
605 goto out;
607 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
608 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
610 color_key.color_space_low_value = 0;
611 color_key.color_space_high_value = 0;
612 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
614 out:
615 if (dcb) DeleteDC(dcb);
616 if (hbm) DeleteObject(hbm);
619 /* Context activation is done by the caller. */
620 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
622 struct wined3d_dummy_textures *textures = &wined3d_device_gl(device)->dummy_textures;
623 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
624 const struct wined3d_gl_info *gl_info = context->gl_info;
625 unsigned int i;
626 DWORD color;
628 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
629 color = 0x000000ff;
630 else
631 color = 0x00000000;
633 /* Under DirectX you can sample even if no texture is bound, whereas
634 * OpenGL will only allow that when a valid texture is bound.
635 * We emulate this by creating dummy textures and binding them
636 * to each texture stage when the currently set D3D texture is NULL. */
637 context_active_texture(context, gl_info, 0);
639 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
640 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
641 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
642 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
643 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
645 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
646 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
647 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
648 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
649 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
651 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
653 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
654 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
655 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
656 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
657 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
660 if (gl_info->supported[EXT_TEXTURE3D])
662 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
663 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
664 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
665 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
666 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
669 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
671 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
672 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
673 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
674 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
676 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
677 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
681 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
683 DWORD cube_array_data[6];
685 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
686 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
687 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
688 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
689 cube_array_data[i] = color;
690 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
691 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
694 if (gl_info->supported[EXT_TEXTURE_ARRAY])
696 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
697 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
698 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
699 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
700 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
702 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
703 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
704 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
705 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
706 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
709 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
711 GLuint buffer;
713 GL_EXTCALL(glGenBuffers(1, &buffer));
714 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
715 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
716 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
718 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
719 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
720 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
721 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
722 GL_EXTCALL(glDeleteBuffers(1, &buffer));
725 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
727 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
728 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
729 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
730 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
732 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
733 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
734 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
735 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
737 if (gl_info->supported[ARB_CLEAR_TEXTURE])
739 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
740 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
742 else
744 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
748 checkGLcall("create dummy textures");
750 context_bind_dummy_textures(context);
753 /* Context activation is done by the caller. */
754 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
756 struct wined3d_dummy_textures *dummy_textures = &wined3d_device_gl(device)->dummy_textures;
757 const struct wined3d_gl_info *gl_info = context->gl_info;
759 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
761 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
762 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
765 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
766 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
768 if (gl_info->supported[EXT_TEXTURE_ARRAY])
770 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
771 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
774 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
775 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
777 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
778 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
780 if (gl_info->supported[EXT_TEXTURE3D])
781 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
783 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
784 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
786 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
787 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
789 checkGLcall("delete dummy textures");
791 memset(dummy_textures, 0, sizeof(*dummy_textures));
794 /* Context activation is done by the caller. */
795 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
797 struct wined3d_sampler_desc desc;
798 HRESULT hr;
800 desc.address_u = WINED3D_TADDRESS_WRAP;
801 desc.address_v = WINED3D_TADDRESS_WRAP;
802 desc.address_w = WINED3D_TADDRESS_WRAP;
803 memset(desc.border_color, 0, sizeof(desc.border_color));
804 desc.mag_filter = WINED3D_TEXF_POINT;
805 desc.min_filter = WINED3D_TEXF_POINT;
806 desc.mip_filter = WINED3D_TEXF_NONE;
807 desc.lod_bias = 0.0f;
808 desc.min_lod = -1000.0f;
809 desc.max_lod = 1000.0f;
810 desc.mip_base_level = 0;
811 desc.max_anisotropy = 1;
812 desc.compare = FALSE;
813 desc.comparison_func = WINED3D_CMP_NEVER;
814 desc.srgb_decode = TRUE;
816 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
817 * instructions allow access to resources without using samplers.
818 * In GLSL, resources are always accessed through sampler or image variables. The default
819 * sampler object is used to emulate the direct resource access when there is no sampler state
820 * to use.
822 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
824 ERR("Failed to create default sampler, hr %#x.\n", hr);
825 device->default_sampler = NULL;
828 /* In D3D10+, a NULL sampler maps to the default sampler state. */
829 desc.address_u = WINED3D_TADDRESS_CLAMP;
830 desc.address_v = WINED3D_TADDRESS_CLAMP;
831 desc.address_w = WINED3D_TADDRESS_CLAMP;
832 desc.mag_filter = WINED3D_TEXF_LINEAR;
833 desc.min_filter = WINED3D_TEXF_LINEAR;
834 desc.mip_filter = WINED3D_TEXF_LINEAR;
835 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
837 ERR("Failed to create null sampler, hr %#x.\n", hr);
838 device->null_sampler = NULL;
842 /* Context activation is done by the caller. */
843 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
845 wined3d_sampler_decref(device->default_sampler);
846 device->default_sampler = NULL;
847 wined3d_sampler_decref(device->null_sampler);
848 device->null_sampler = NULL;
851 static LONG fullscreen_style(LONG style)
853 /* Make sure the window is managed, otherwise we won't get keyboard input. */
854 style |= WS_POPUP | WS_SYSMENU;
855 style &= ~(WS_CAPTION | WS_THICKFRAME);
857 return style;
860 static LONG fullscreen_exstyle(LONG exstyle)
862 /* Filter out window decorations. */
863 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
865 return exstyle;
868 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
870 BOOL filter_messages;
871 LONG style, exstyle;
873 TRACE("Setting up window %p for fullscreen mode.\n", window);
875 if (device->style || device->exStyle)
877 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
878 window, device->style, device->exStyle);
881 device->style = GetWindowLongW(window, GWL_STYLE);
882 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
884 style = fullscreen_style(device->style);
885 exstyle = fullscreen_exstyle(device->exStyle);
887 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
888 device->style, device->exStyle, style, exstyle);
890 filter_messages = device->filter_messages;
891 device->filter_messages = TRUE;
893 SetWindowLongW(window, GWL_STYLE, style);
894 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
895 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
897 device->filter_messages = filter_messages;
900 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
901 const RECT *window_rect)
903 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
904 BOOL filter_messages;
905 LONG style, exstyle;
906 RECT rect = {0};
908 if (!device->style && !device->exStyle)
909 return;
911 style = GetWindowLongW(window, GWL_STYLE);
912 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
914 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
915 * application, and we want to ignore them in the test below, since it's
916 * not the application's fault that they changed. Additionally, we want to
917 * preserve the current status of these flags (i.e. don't restore them) to
918 * more closely emulate the behavior of Direct3D, which leaves these flags
919 * alone when returning to windowed mode. */
920 device->style ^= (device->style ^ style) & WS_VISIBLE;
921 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
923 TRACE("Restoring window style of window %p to %08x, %08x.\n",
924 window, device->style, device->exStyle);
926 filter_messages = device->filter_messages;
927 device->filter_messages = TRUE;
929 /* Only restore the style if the application didn't modify it during the
930 * fullscreen phase. Some applications change it before calling Reset()
931 * when switching between windowed and fullscreen modes (HL2), some
932 * depend on the original style (Eve Online). */
933 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
935 SetWindowLongW(window, GWL_STYLE, device->style);
936 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
939 if (window_rect)
940 rect = *window_rect;
941 else
942 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
943 SetWindowPos(window, 0, rect.left, rect.top,
944 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
946 device->filter_messages = filter_messages;
948 /* Delete the old values. */
949 device->style = 0;
950 device->exStyle = 0;
953 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
955 TRACE("device %p, window %p.\n", device, window);
957 if (!wined3d_register_window(window, device))
959 ERR("Failed to register window %p.\n", window);
960 return E_FAIL;
963 InterlockedExchangePointer((void **)&device->focus_window, window);
964 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
966 return WINED3D_OK;
969 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
971 TRACE("device %p.\n", device);
973 if (device->focus_window) wined3d_unregister_window(device->focus_window);
974 InterlockedExchangePointer((void **)&device->focus_window, NULL);
977 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
979 BOOL ds_enable = swapchain->desc.enable_auto_depth_stencil;
980 unsigned int i;
982 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
984 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
986 if (device->back_buffer_view)
987 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
989 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
992 static void wined3d_device_delete_opengl_contexts_cs(void *object)
994 struct wined3d_resource *resource, *cursor;
995 struct wined3d_device *device = object;
996 struct wined3d_context *context;
997 struct wined3d_shader *shader;
999 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1001 TRACE("Unloading resource %p.\n", resource);
1002 wined3d_cs_emit_unload_resource(device->cs, resource);
1005 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
1007 device->shader_backend->shader_destroy(shader);
1010 context = context_acquire(device, NULL, 0);
1011 device->blitter->ops->blitter_destroy(device->blitter, context);
1012 device->shader_backend->shader_free_private(device, context);
1013 destroy_dummy_textures(device, context);
1014 destroy_default_samplers(device, context);
1015 context_release(context);
1017 while (device->context_count)
1019 if (device->contexts[0]->swapchain)
1020 swapchain_destroy_contexts(device->contexts[0]->swapchain);
1021 else
1022 wined3d_context_destroy(device->contexts[0]);
1026 static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
1028 wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
1029 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1032 static void wined3d_device_create_primary_opengl_context_cs(void *object)
1034 struct wined3d_device *device = object;
1035 struct wined3d_swapchain *swapchain;
1036 struct wined3d_context *context;
1037 struct wined3d_texture *target;
1038 HRESULT hr;
1040 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1041 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1043 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1044 return;
1047 if (!(device->blitter = wined3d_cpu_blitter_create()))
1049 ERR("Failed to create CPU blitter.\n");
1050 device->shader_backend->shader_free_private(device, NULL);
1051 return;
1053 wined3d_ffp_blitter_create(&device->blitter, &device->adapter->gl_info);
1054 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1055 wined3d_arbfp_blitter_create(&device->blitter, device);
1056 wined3d_fbo_blitter_create(&device->blitter, &device->adapter->gl_info);
1057 wined3d_raw_blitter_create(&device->blitter, &device->adapter->gl_info);
1059 swapchain = device->swapchains[0];
1060 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1061 context = context_acquire(device, target, 0);
1062 create_dummy_textures(device, context);
1063 create_default_samplers(device, context);
1064 context_release(context);
1067 static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1069 wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1070 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1071 if (!device->swapchains[0]->num_contexts)
1072 return E_FAIL;
1074 return WINED3D_OK;
1077 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1079 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1080 const struct wined3d_swapchain_desc *swapchain_desc;
1081 DWORD clear_flags = 0;
1082 HRESULT hr;
1084 TRACE("device %p, swapchain %p.\n", device, swapchain);
1086 if (device->d3d_initialized)
1087 return WINED3DERR_INVALIDCALL;
1089 swapchain_desc = &swapchain->desc;
1090 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1092 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1093 struct wined3d_view_desc view_desc;
1095 view_desc.format_id = back_buffer->format->id;
1096 view_desc.flags = 0;
1097 view_desc.u.texture.level_idx = 0;
1098 view_desc.u.texture.level_count = 1;
1099 view_desc.u.texture.layer_idx = 0;
1100 view_desc.u.texture.layer_count = 1;
1101 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1102 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1104 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1105 return hr;
1109 device->swapchain_count = 1;
1110 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1112 ERR("Failed to allocate swapchain array.\n");
1113 hr = E_OUTOFMEMORY;
1114 goto err_out;
1116 device->swapchains[0] = swapchain;
1118 memset(device->fb.render_targets, 0, sizeof(device->fb.render_targets));
1119 if (device->wined3d->flags & WINED3D_NO3D)
1121 if (!(device->blitter = wined3d_cpu_blitter_create()))
1123 ERR("Failed to create CPU blitter.\n");
1124 heap_free(device->swapchains);
1125 device->swapchain_count = 0;
1126 hr = E_FAIL;
1127 goto err_out;
1130 else
1132 if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1133 goto err_out;
1134 device_init_swapchain_state(device, swapchain);
1136 device->contexts[0]->last_was_rhw = 0;
1138 TRACE("All defaults now set up.\n");
1140 /* Clear the screen */
1141 if (device->back_buffer_view)
1142 clear_flags |= WINED3DCLEAR_TARGET;
1143 if (swapchain_desc->enable_auto_depth_stencil)
1144 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1145 if (clear_flags)
1146 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1148 device->d3d_initialized = TRUE;
1150 if (wined3d_settings.logo)
1151 device_load_logo(device, wined3d_settings.logo);
1154 return WINED3D_OK;
1156 err_out:
1157 heap_free(device->swapchains);
1158 device->swapchains = NULL;
1159 device->swapchain_count = 0;
1160 if (device->back_buffer_view)
1162 wined3d_rendertarget_view_decref(device->back_buffer_view);
1163 device->back_buffer_view = NULL;
1166 return hr;
1169 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1171 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1173 wined3d_sampler_decref(sampler);
1176 void wined3d_device_uninit_3d(struct wined3d_device *device)
1178 BOOL no3d = device->wined3d->flags & WINED3D_NO3D;
1179 struct wined3d_rendertarget_view *view;
1180 struct wined3d_texture *texture;
1181 unsigned int i;
1183 TRACE("device %p.\n", device);
1185 if (!device->d3d_initialized && !no3d)
1187 ERR("Called while 3D support was not initialised.\n");
1188 return;
1191 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1193 device->swapchain_count = 0;
1195 if ((texture = device->logo_texture))
1197 device->logo_texture = NULL;
1198 wined3d_texture_decref(texture);
1201 if ((texture = device->cursor_texture))
1203 device->cursor_texture = NULL;
1204 wined3d_texture_decref(texture);
1207 state_unbind_resources(&device->state);
1208 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
1210 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1213 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1215 if (no3d)
1216 device->blitter->ops->blitter_destroy(device->blitter, NULL);
1217 else
1218 wined3d_device_delete_opengl_contexts(device);
1220 if ((view = device->fb.depth_stencil))
1222 TRACE("Releasing depth/stencil view %p.\n", view);
1224 device->fb.depth_stencil = NULL;
1225 wined3d_rendertarget_view_decref(view);
1228 if ((view = device->auto_depth_stencil_view))
1230 device->auto_depth_stencil_view = NULL;
1231 if (wined3d_rendertarget_view_decref(view))
1232 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1235 if ((view = device->back_buffer_view))
1237 device->back_buffer_view = NULL;
1238 wined3d_rendertarget_view_decref(view);
1241 heap_free(device->swapchains);
1242 device->swapchains = NULL;
1244 device->d3d_initialized = FALSE;
1247 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1248 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1249 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1251 * There is no way to deactivate thread safety once it is enabled.
1253 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1255 TRACE("device %p.\n", device);
1257 /* For now just store the flag (needed in case of ddraw). */
1258 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1261 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1263 const struct wined3d_driver_info *driver_info;
1265 TRACE("device %p.\n", device);
1267 driver_info = &device->adapter->driver_info;
1269 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1270 wine_dbgstr_longlong(driver_info->vram_bytes),
1271 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1272 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1274 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1277 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1278 struct wined3d_buffer *buffer, UINT offset)
1280 struct wined3d_stream_output *stream;
1281 struct wined3d_buffer *prev_buffer;
1283 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1285 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1287 WARN("Invalid stream output %u.\n", idx);
1288 return;
1291 stream = &device->state.stream_output[idx];
1292 prev_buffer = stream->buffer;
1294 if (buffer)
1295 wined3d_buffer_incref(buffer);
1296 stream->buffer = buffer;
1297 stream->offset = offset;
1298 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1299 if (prev_buffer)
1300 wined3d_buffer_decref(prev_buffer);
1303 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1304 UINT idx, UINT *offset)
1306 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1308 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1310 WARN("Invalid stream output %u.\n", idx);
1311 return NULL;
1314 if (offset)
1315 *offset = device->state.stream_output[idx].offset;
1316 return device->state.stream_output[idx].buffer;
1319 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1320 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1322 struct wined3d_stream_state *stream;
1323 struct wined3d_buffer *prev_buffer;
1325 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1326 device, stream_idx, buffer, offset, stride);
1328 if (stream_idx >= WINED3D_MAX_STREAMS)
1330 WARN("Stream index %u out of range.\n", stream_idx);
1331 return WINED3DERR_INVALIDCALL;
1333 else if (offset & 0x3)
1335 WARN("Offset %u is not 4 byte aligned.\n", offset);
1336 return WINED3DERR_INVALIDCALL;
1339 stream = &device->state.streams[stream_idx];
1340 prev_buffer = stream->buffer;
1342 if (buffer)
1343 wined3d_buffer_incref(buffer);
1344 if (device->update_stateblock_state->streams[stream_idx].buffer)
1345 wined3d_buffer_decref(device->update_stateblock_state->streams[stream_idx].buffer);
1346 device->update_stateblock_state->streams[stream_idx].buffer = buffer;
1347 device->update_stateblock_state->streams[stream_idx].stride = stride;
1348 device->update_stateblock_state->streams[stream_idx].offset = offset;
1350 if (device->recording)
1352 device->recording->changed.streamSource |= 1u << stream_idx;
1353 return WINED3D_OK;
1356 if (prev_buffer == buffer
1357 && stream->stride == stride
1358 && stream->offset == offset)
1360 TRACE("Application is setting the old values over, nothing to do.\n");
1361 return WINED3D_OK;
1364 stream->buffer = buffer;
1365 stream->stride = stride;
1366 stream->offset = offset;
1367 if (buffer)
1368 wined3d_buffer_incref(buffer);
1369 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1370 if (prev_buffer)
1371 wined3d_buffer_decref(prev_buffer);
1373 return WINED3D_OK;
1376 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1377 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1379 const struct wined3d_stream_state *stream;
1381 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1382 device, stream_idx, buffer, offset, stride);
1384 if (stream_idx >= WINED3D_MAX_STREAMS)
1386 WARN("Stream index %u out of range.\n", stream_idx);
1387 return WINED3DERR_INVALIDCALL;
1390 stream = &device->state.streams[stream_idx];
1391 *buffer = stream->buffer;
1392 if (offset)
1393 *offset = stream->offset;
1394 *stride = stream->stride;
1396 return WINED3D_OK;
1399 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1401 UINT old_flags, old_freq, flags, freq;
1402 struct wined3d_stream_state *stream;
1404 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1406 /* Verify input. At least in d3d9 this is invalid. */
1407 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1409 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1410 return WINED3DERR_INVALIDCALL;
1412 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1414 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1415 return WINED3DERR_INVALIDCALL;
1417 if (!divider)
1419 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1420 return WINED3DERR_INVALIDCALL;
1423 stream = &device->state.streams[stream_idx];
1424 old_flags = stream->flags;
1425 old_freq = stream->frequency;
1427 flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1428 freq = divider & 0x7fffff;
1430 device->update_stateblock_state->streams[stream_idx].flags = flags;
1431 device->update_stateblock_state->streams[stream_idx].frequency = freq;
1432 if (device->recording)
1434 device->recording->changed.streamFreq |= 1u << stream_idx;
1435 return WINED3D_OK;
1438 stream->flags = flags;
1439 stream->frequency = freq;
1440 if (stream->frequency != old_freq || stream->flags != old_flags)
1441 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1443 return WINED3D_OK;
1446 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1447 UINT stream_idx, UINT *divider)
1449 const struct wined3d_stream_state *stream;
1451 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1453 stream = &device->state.streams[stream_idx];
1454 *divider = stream->flags | stream->frequency;
1456 TRACE("Returning %#x.\n", *divider);
1458 return WINED3D_OK;
1461 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1462 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1464 TRACE("device %p, state %s, matrix %p.\n",
1465 device, debug_d3dtstype(d3dts), matrix);
1466 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1467 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1468 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1469 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1471 /* Handle recording of state blocks. */
1472 device->update_stateblock_state->transforms[d3dts] = *matrix;
1473 if (device->recording)
1475 TRACE("Recording... not performing anything.\n");
1476 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1477 return;
1480 /* If the new matrix is the same as the current one,
1481 * we cut off any further processing. this seems to be a reasonable
1482 * optimization because as was noticed, some apps (warcraft3 for example)
1483 * tend towards setting the same matrix repeatedly for some reason.
1485 * From here on we assume that the new matrix is different, wherever it matters. */
1486 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1488 TRACE("The application is setting the same matrix over again.\n");
1489 return;
1492 device->state.transforms[d3dts] = *matrix;
1493 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1496 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1497 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1499 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1501 *matrix = device->state.transforms[state];
1504 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1505 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1507 struct wined3d_matrix *mat;
1509 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1511 if (state > WINED3D_HIGHEST_TRANSFORM_STATE)
1513 WARN("Unhandled transform state %#x.\n", state);
1514 return;
1517 /* Tests show that stateblock recording is ignored; the change goes directly
1518 * into the primary stateblock. */
1519 mat = &device->state.transforms[state];
1520 multiply_matrix(mat, mat, matrix);
1521 wined3d_cs_emit_set_transform(device->cs, state, mat);
1524 /* Note lights are real special cases. Although the device caps state only
1525 * e.g. 8 are supported, you can reference any indexes you want as long as
1526 * that number max are enabled at any one point in time. Therefore since the
1527 * indices can be anything, we need a hashmap of them. However, this causes
1528 * stateblock problems. When capturing the state block, I duplicate the
1529 * hashmap, but when recording, just build a chain pretty much of commands to
1530 * be replayed. */
1531 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1532 UINT light_idx, const struct wined3d_light *light)
1534 struct wined3d_light_info *object = NULL;
1535 HRESULT hr;
1536 float rho;
1538 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1540 /* Check the parameter range. Need for speed most wanted sets junk lights
1541 * which confuse the GL driver. */
1542 if (!light)
1543 return WINED3DERR_INVALIDCALL;
1545 switch (light->type)
1547 case WINED3D_LIGHT_POINT:
1548 case WINED3D_LIGHT_SPOT:
1549 case WINED3D_LIGHT_GLSPOT:
1550 /* Incorrect attenuation values can cause the gl driver to crash.
1551 * Happens with Need for speed most wanted. */
1552 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1554 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1555 return WINED3DERR_INVALIDCALL;
1557 break;
1559 case WINED3D_LIGHT_DIRECTIONAL:
1560 case WINED3D_LIGHT_PARALLELPOINT:
1561 /* Ignores attenuation */
1562 break;
1564 default:
1565 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1566 return WINED3DERR_INVALIDCALL;
1569 if (FAILED(hr = wined3d_light_state_set_light(&device->update_stateblock_state->light_state, light_idx, light, &object)))
1570 return hr;
1571 if (device->recording)
1572 return WINED3D_OK;
1574 if (FAILED(hr = wined3d_light_state_set_light(&device->state.light_state, light_idx, light, &object)))
1575 return hr;
1577 /* Initialize the object. */
1578 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1579 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1580 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1581 light_idx, light->type, debug_color(&light->diffuse),
1582 debug_color(&light->specular), debug_color(&light->ambient),
1583 light->position.x, light->position.y, light->position.z,
1584 light->direction.x, light->direction.y, light->direction.z,
1585 light->range, light->falloff, light->theta, light->phi);
1587 switch (light->type)
1589 case WINED3D_LIGHT_POINT:
1590 /* Position */
1591 object->position.x = light->position.x;
1592 object->position.y = light->position.y;
1593 object->position.z = light->position.z;
1594 object->position.w = 1.0f;
1595 object->cutoff = 180.0f;
1596 /* FIXME: Range */
1597 break;
1599 case WINED3D_LIGHT_DIRECTIONAL:
1600 /* Direction */
1601 object->direction.x = -light->direction.x;
1602 object->direction.y = -light->direction.y;
1603 object->direction.z = -light->direction.z;
1604 object->direction.w = 0.0f;
1605 object->exponent = 0.0f;
1606 object->cutoff = 180.0f;
1607 break;
1609 case WINED3D_LIGHT_SPOT:
1610 /* Position */
1611 object->position.x = light->position.x;
1612 object->position.y = light->position.y;
1613 object->position.z = light->position.z;
1614 object->position.w = 1.0f;
1616 /* Direction */
1617 object->direction.x = light->direction.x;
1618 object->direction.y = light->direction.y;
1619 object->direction.z = light->direction.z;
1620 object->direction.w = 0.0f;
1622 /* opengl-ish and d3d-ish spot lights use too different models
1623 * for the light "intensity" as a function of the angle towards
1624 * the main light direction, so we only can approximate very
1625 * roughly. However, spot lights are rather rarely used in games
1626 * (if ever used at all). Furthermore if still used, probably
1627 * nobody pays attention to such details. */
1628 if (!light->falloff)
1630 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1631 * equations have the falloff resp. exponent parameter as an
1632 * exponent, so the spot light lighting will always be 1.0 for
1633 * both of them, and we don't have to care for the rest of the
1634 * rather complex calculation. */
1635 object->exponent = 0.0f;
1637 else
1639 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1640 if (rho < 0.0001f)
1641 rho = 0.0001f;
1642 object->exponent = -0.3f / logf(cosf(rho / 2));
1645 if (object->exponent > 128.0f)
1646 object->exponent = 128.0f;
1648 object->cutoff = (float)(light->phi * 90 / M_PI);
1649 /* FIXME: Range */
1650 break;
1652 case WINED3D_LIGHT_PARALLELPOINT:
1653 object->position.x = light->position.x;
1654 object->position.y = light->position.y;
1655 object->position.z = light->position.z;
1656 object->position.w = 1.0f;
1657 break;
1659 default:
1660 FIXME("Unrecognized light type %#x.\n", light->type);
1663 wined3d_cs_emit_set_light(device->cs, object);
1665 return WINED3D_OK;
1668 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1669 UINT light_idx, struct wined3d_light *light)
1671 struct wined3d_light_info *light_info;
1673 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1675 if (!(light_info = wined3d_light_state_get_light(&device->state.light_state, light_idx)))
1677 TRACE("Light information requested but light not defined\n");
1678 return WINED3DERR_INVALIDCALL;
1681 *light = light_info->OriginalParms;
1682 return WINED3D_OK;
1685 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1687 struct wined3d_light_info *light_info;
1688 HRESULT hr;
1690 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1692 if (!(light_info = wined3d_light_state_get_light(&device->update_stateblock_state->light_state, light_idx)))
1694 if (FAILED(hr = wined3d_light_state_set_light(&device->update_stateblock_state->light_state, light_idx,
1695 &WINED3D_default_light, &light_info)))
1696 return hr;
1698 wined3d_light_state_enable_light(&device->update_stateblock_state->light_state,
1699 &device->adapter->d3d_info, light_info, enable);
1701 if (device->recording)
1702 return WINED3D_OK;
1704 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1705 if (!(light_info = wined3d_light_state_get_light(&device->state.light_state, light_idx)))
1707 TRACE("Light enabled requested but light not defined, so defining one!\n");
1708 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1710 if (!(light_info = wined3d_light_state_get_light(&device->state.light_state, light_idx)))
1712 FIXME("Adding default lights has failed dismally\n");
1713 return WINED3DERR_INVALIDCALL;
1717 wined3d_light_state_enable_light(&device->state.light_state, &device->adapter->d3d_info, light_info, enable);
1718 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1720 return WINED3D_OK;
1723 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1725 struct wined3d_light_info *light_info;
1727 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1729 if (!(light_info = wined3d_light_state_get_light(&device->state.light_state, light_idx)))
1731 TRACE("Light enabled state requested but light not defined.\n");
1732 return WINED3DERR_INVALIDCALL;
1734 /* true is 128 according to SetLightEnable */
1735 *enable = light_info->enabled ? 128 : 0;
1736 return WINED3D_OK;
1739 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1740 UINT plane_idx, const struct wined3d_vec4 *plane)
1742 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1744 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1746 TRACE("Application has requested clipplane this device doesn't support.\n");
1747 return WINED3DERR_INVALIDCALL;
1750 device->update_stateblock_state->clip_planes[plane_idx] = *plane;
1752 if (device->recording)
1754 device->recording->changed.clipplane |= 1u << plane_idx;
1755 return WINED3D_OK;
1758 if (!memcmp(&device->state.clip_planes[plane_idx], plane, sizeof(*plane)))
1760 TRACE("Application is setting old values over, nothing to do.\n");
1761 return WINED3D_OK;
1764 device->state.clip_planes[plane_idx] = *plane;
1766 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1768 return WINED3D_OK;
1771 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1772 UINT plane_idx, struct wined3d_vec4 *plane)
1774 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1776 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1778 TRACE("Application has requested clipplane this device doesn't support.\n");
1779 return WINED3DERR_INVALIDCALL;
1782 *plane = device->state.clip_planes[plane_idx];
1784 return WINED3D_OK;
1787 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1788 const struct wined3d_clip_status *clip_status)
1790 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1792 if (!clip_status)
1793 return WINED3DERR_INVALIDCALL;
1795 return WINED3D_OK;
1798 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1799 struct wined3d_clip_status *clip_status)
1801 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1803 if (!clip_status)
1804 return WINED3DERR_INVALIDCALL;
1806 return WINED3D_OK;
1809 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1811 TRACE("device %p, material %p.\n", device, material);
1813 device->update_stateblock_state->material = *material;
1814 if (device->recording)
1816 device->recording->changed.material = TRUE;
1817 return;
1820 device->state.material = *material;
1821 wined3d_cs_emit_set_material(device->cs, material);
1824 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1826 TRACE("device %p, material %p.\n", device, material);
1828 *material = device->state.material;
1830 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1831 TRACE("ambient %s\n", debug_color(&material->ambient));
1832 TRACE("specular %s\n", debug_color(&material->specular));
1833 TRACE("emissive %s\n", debug_color(&material->emissive));
1834 TRACE("power %.8e.\n", material->power);
1837 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1838 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1840 enum wined3d_format_id prev_format;
1841 struct wined3d_buffer *prev_buffer;
1842 unsigned int prev_offset;
1844 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1845 device, buffer, debug_d3dformat(format_id), offset);
1847 prev_buffer = device->state.index_buffer;
1848 prev_format = device->state.index_format;
1849 prev_offset = device->state.index_offset;
1851 if (buffer)
1852 wined3d_buffer_incref(buffer);
1853 if (device->update_stateblock_state->index_buffer)
1854 wined3d_buffer_decref(device->update_stateblock_state->index_buffer);
1855 device->update_stateblock_state->index_buffer = buffer;
1856 device->update_stateblock_state->index_format = format_id;
1858 if (device->recording)
1860 device->recording->changed.indices = TRUE;
1861 return;
1864 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1865 return;
1867 if (buffer)
1868 wined3d_buffer_incref(buffer);
1869 device->state.index_buffer = buffer;
1870 device->state.index_format = format_id;
1871 device->state.index_offset = offset;
1872 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1873 if (prev_buffer)
1874 wined3d_buffer_decref(prev_buffer);
1877 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1878 enum wined3d_format_id *format, unsigned int *offset)
1880 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1882 *format = device->state.index_format;
1883 if (offset)
1884 *offset = device->state.index_offset;
1885 return device->state.index_buffer;
1888 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1890 TRACE("device %p, base_index %d.\n", device, base_index);
1892 device->update_stateblock_state->base_vertex_index = base_index;
1893 if (!device->recording)
1894 device->state.base_vertex_index = base_index;
1897 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1899 TRACE("device %p.\n", device);
1901 return device->state.base_vertex_index;
1904 void CDECL wined3d_device_set_viewports(struct wined3d_device *device, unsigned int viewport_count,
1905 const struct wined3d_viewport *viewports)
1907 unsigned int i;
1909 TRACE("device %p, viewport_count %u, viewports %p.\n", device, viewport_count, viewports);
1911 for (i = 0; i < viewport_count; ++i)
1913 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1914 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1917 if (viewport_count)
1918 device->update_stateblock_state->viewport = viewports[0];
1920 /* Handle recording of state blocks */
1921 if (device->recording)
1923 TRACE("Recording... not performing anything\n");
1924 device->recording->changed.viewport = TRUE;
1925 return;
1928 if (viewport_count)
1929 memcpy(device->state.viewports, viewports, viewport_count * sizeof(*viewports));
1930 else
1931 memset(device->state.viewports, 0, sizeof(device->state.viewports));
1932 device->state.viewport_count = viewport_count;
1934 wined3d_cs_emit_set_viewports(device->cs, viewport_count, viewports);
1937 void CDECL wined3d_device_get_viewports(const struct wined3d_device *device, unsigned int *viewport_count,
1938 struct wined3d_viewport *viewports)
1940 unsigned int count;
1942 TRACE("device %p, viewport_count %p, viewports %p.\n", device, viewport_count, viewports);
1944 count = viewport_count ? min(*viewport_count, device->state.viewport_count) : 1;
1945 if (count && viewports)
1946 memcpy(viewports, device->state.viewports, count * sizeof(*viewports));
1947 if (viewport_count)
1948 *viewport_count = device->state.viewport_count;
1951 static void resolve_depth_buffer(struct wined3d_device *device)
1953 const struct wined3d_state *state = &device->state;
1954 struct wined3d_rendertarget_view *src_view;
1955 struct wined3d_resource *dst_resource;
1956 struct wined3d_texture *dst_texture;
1958 if (!(dst_texture = state->textures[0]))
1959 return;
1960 dst_resource = &dst_texture->resource;
1961 if (!(dst_resource->format_flags & WINED3DFMT_FLAG_DEPTH))
1962 return;
1963 if (!(src_view = state->fb->depth_stencil))
1964 return;
1966 wined3d_device_resolve_sub_resource(device, dst_resource, 0,
1967 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1970 void CDECL wined3d_device_set_blend_state(struct wined3d_device *device,
1971 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor)
1973 struct wined3d_state *state = &device->state;
1974 struct wined3d_blend_state *prev;
1976 TRACE("device %p, blend_state %p, blend_factor %s.\n", device, blend_state, debug_color(blend_factor));
1978 device->update_stateblock_state->blend_factor = *blend_factor;
1979 if (device->recording)
1981 device->recording->changed.blend_state = TRUE;
1982 return;
1985 prev = state->blend_state;
1986 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor)))
1987 return;
1989 if (blend_state)
1990 wined3d_blend_state_incref(blend_state);
1991 state->blend_state = blend_state;
1992 state->blend_factor = *blend_factor;
1993 wined3d_cs_emit_set_blend_state(device->cs, blend_state, blend_factor);
1994 if (prev)
1995 wined3d_blend_state_decref(prev);
1998 struct wined3d_blend_state * CDECL wined3d_device_get_blend_state(const struct wined3d_device *device,
1999 struct wined3d_color *blend_factor)
2001 const struct wined3d_state *state = &device->state;
2003 TRACE("device %p, blend_factor %p.\n", device, blend_factor);
2005 *blend_factor = state->blend_factor;
2006 return state->blend_state;
2009 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2010 struct wined3d_rasterizer_state *rasterizer_state)
2012 struct wined3d_rasterizer_state *prev;
2014 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2016 prev = device->state.rasterizer_state;
2017 if (prev == rasterizer_state)
2018 return;
2020 if (rasterizer_state)
2021 wined3d_rasterizer_state_incref(rasterizer_state);
2022 device->state.rasterizer_state = rasterizer_state;
2023 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2024 if (prev)
2025 wined3d_rasterizer_state_decref(prev);
2028 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2030 TRACE("device %p.\n", device);
2032 return device->state.rasterizer_state;
2035 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2036 enum wined3d_render_state state, DWORD value)
2038 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2040 if (state > WINEHIGHEST_RENDER_STATE)
2042 WARN("Unhandled render state %#x.\n", state);
2043 return;
2046 device->update_stateblock_state->rs[state] = value;
2048 /* Handle recording of state blocks. */
2049 if (device->recording)
2051 TRACE("Recording... not performing anything.\n");
2052 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2053 return;
2056 if (value == device->state.render_states[state])
2057 TRACE("Application is setting the old value over, nothing to do.\n");
2058 else
2060 device->state.render_states[state] = value;
2061 wined3d_cs_emit_set_render_state(device->cs, state, value);
2064 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2066 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2067 resolve_depth_buffer(device);
2071 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2073 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2075 return device->state.render_states[state];
2078 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2079 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2081 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2082 device, sampler_idx, debug_d3dsamplerstate(state), value);
2084 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2085 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - WINED3D_MAX_FRAGMENT_SAMPLERS);
2087 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2089 WARN("Invalid sampler %u.\n", sampler_idx);
2090 return; /* Windows accepts overflowing this array ... we do not. */
2093 device->update_stateblock_state->sampler_states[sampler_idx][state] = value;
2095 /* Handle recording of state blocks. */
2096 if (device->recording)
2098 TRACE("Recording... not performing anything.\n");
2099 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2100 return;
2103 if (value == device->state.sampler_states[sampler_idx][state])
2105 TRACE("Application is setting the old value over, nothing to do.\n");
2106 return;
2109 device->state.sampler_states[sampler_idx][state] = value;
2110 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2113 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2114 UINT sampler_idx, enum wined3d_sampler_state state)
2116 TRACE("device %p, sampler_idx %u, state %s.\n",
2117 device, sampler_idx, debug_d3dsamplerstate(state));
2119 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2120 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - WINED3D_MAX_FRAGMENT_SAMPLERS);
2122 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2124 WARN("Invalid sampler %u.\n", sampler_idx);
2125 return 0; /* Windows accepts overflowing this array ... we do not. */
2128 return device->state.sampler_states[sampler_idx][state];
2131 void CDECL wined3d_device_set_scissor_rects(struct wined3d_device *device, unsigned int rect_count,
2132 const RECT *rects)
2134 unsigned int i;
2136 TRACE("device %p, rect_count %u, rects %p.\n", device, rect_count, rects);
2138 for (i = 0; i < rect_count; ++i)
2140 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2143 if (rect_count)
2144 device->update_stateblock_state->scissor_rect = rects[0];
2146 if (device->recording)
2148 device->recording->changed.scissorRect = TRUE;
2149 return;
2152 if (device->state.scissor_rect_count == rect_count
2153 && !memcmp(device->state.scissor_rects, rects, rect_count * sizeof(*rects)))
2155 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2156 return;
2159 if (rect_count)
2160 memcpy(device->state.scissor_rects, rects, rect_count * sizeof(*rects));
2161 else
2162 memset(device->state.scissor_rects, 0, sizeof(device->state.scissor_rects));
2163 device->state.scissor_rect_count = rect_count;
2165 wined3d_cs_emit_set_scissor_rects(device->cs, rect_count, rects);
2168 void CDECL wined3d_device_get_scissor_rects(const struct wined3d_device *device, unsigned int *rect_count, RECT *rects)
2170 unsigned int count;
2172 TRACE("device %p, rect_count %p, rects %p.\n", device, rect_count, rects);
2174 count = rect_count ? min(*rect_count, device->state.scissor_rect_count) : 1;
2175 if (count && rects)
2176 memcpy(rects, device->state.scissor_rects, count * sizeof(*rects));
2177 if (rect_count)
2178 *rect_count = device->state.scissor_rect_count;
2181 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2182 struct wined3d_vertex_declaration *declaration)
2184 struct wined3d_vertex_declaration *prev = device->state.vertex_declaration;
2186 TRACE("device %p, declaration %p.\n", device, declaration);
2188 if (declaration)
2189 wined3d_vertex_declaration_incref(declaration);
2190 if (device->update_stateblock_state->vertex_declaration)
2191 wined3d_vertex_declaration_decref(device->update_stateblock_state->vertex_declaration);
2192 device->update_stateblock_state->vertex_declaration = declaration;
2194 if (device->recording)
2196 device->recording->changed.vertexDecl = TRUE;
2197 return;
2200 if (declaration == prev)
2201 return;
2203 if (declaration)
2204 wined3d_vertex_declaration_incref(declaration);
2205 device->state.vertex_declaration = declaration;
2206 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2207 if (prev)
2208 wined3d_vertex_declaration_decref(prev);
2211 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2213 TRACE("device %p.\n", device);
2215 return device->state.vertex_declaration;
2218 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2220 struct wined3d_shader *prev = device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2222 TRACE("device %p, shader %p.\n", device, shader);
2224 if (shader)
2225 wined3d_shader_incref(shader);
2226 if (device->update_stateblock_state->vs)
2227 wined3d_shader_decref(device->update_stateblock_state->vs);
2228 device->update_stateblock_state->vs = shader;
2230 if (device->recording)
2232 device->recording->changed.vertexShader = TRUE;
2233 return;
2236 if (shader == prev)
2237 return;
2239 if (shader)
2240 wined3d_shader_incref(shader);
2241 device->state.shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2242 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2243 if (prev)
2244 wined3d_shader_decref(prev);
2247 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2249 TRACE("device %p.\n", device);
2251 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2254 void CDECL wined3d_device_set_constant_buffer(struct wined3d_device *device,
2255 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2257 struct wined3d_buffer *prev;
2259 TRACE("device %p, type %#x, idx %u, buffer %p.\n", device, type, idx, buffer);
2261 if (idx >= MAX_CONSTANT_BUFFERS)
2263 WARN("Invalid constant buffer index %u.\n", idx);
2264 return;
2267 prev = device->state.cb[type][idx];
2268 if (buffer == prev)
2269 return;
2271 if (buffer)
2272 wined3d_buffer_incref(buffer);
2273 device->state.cb[type][idx] = buffer;
2274 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2275 if (prev)
2276 wined3d_buffer_decref(prev);
2279 struct wined3d_buffer * CDECL wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2280 enum wined3d_shader_type shader_type, unsigned int idx)
2282 TRACE("device %p, shader_type %#x, idx %u.\n", device, shader_type, idx);
2284 if (idx >= MAX_CONSTANT_BUFFERS)
2286 WARN("Invalid constant buffer index %u.\n", idx);
2287 return NULL;
2290 return device->state.cb[shader_type][idx];
2293 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2294 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2296 struct wined3d_shader_resource_view *prev;
2298 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2300 WARN("Invalid view index %u.\n", idx);
2301 return;
2304 prev = device->state.shader_resource_view[type][idx];
2305 if (view == prev)
2306 return;
2308 if (view)
2309 wined3d_shader_resource_view_incref(view);
2310 device->state.shader_resource_view[type][idx] = view;
2311 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2312 if (prev)
2313 wined3d_shader_resource_view_decref(prev);
2316 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2317 UINT idx, struct wined3d_shader_resource_view *view)
2319 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2321 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2324 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2325 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2327 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2329 WARN("Invalid view index %u.\n", idx);
2330 return NULL;
2333 return device->state.shader_resource_view[shader_type][idx];
2336 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2337 UINT idx)
2339 TRACE("device %p, idx %u.\n", device, idx);
2341 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2344 static void wined3d_device_set_sampler(struct wined3d_device *device,
2345 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2347 struct wined3d_sampler *prev;
2349 if (idx >= MAX_SAMPLER_OBJECTS)
2351 WARN("Invalid sampler index %u.\n", idx);
2352 return;
2355 prev = device->state.sampler[type][idx];
2356 if (sampler == prev)
2357 return;
2359 if (sampler)
2360 wined3d_sampler_incref(sampler);
2361 device->state.sampler[type][idx] = sampler;
2362 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2363 if (prev)
2364 wined3d_sampler_decref(prev);
2367 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2369 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2371 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2374 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2375 enum wined3d_shader_type shader_type, unsigned int idx)
2377 if (idx >= MAX_SAMPLER_OBJECTS)
2379 WARN("Invalid sampler index %u.\n", idx);
2380 return NULL;
2383 return device->state.sampler[shader_type][idx];
2386 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2388 TRACE("device %p, idx %u.\n", device, idx);
2390 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2393 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2394 unsigned int start_idx, unsigned int count, const BOOL *constants)
2396 unsigned int i;
2398 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2399 device, start_idx, count, constants);
2401 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2402 return WINED3DERR_INVALIDCALL;
2404 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2405 count = WINED3D_MAX_CONSTS_B - start_idx;
2407 memcpy(&device->update_stateblock_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2408 if (device->recording)
2410 for (i = start_idx; i < count + start_idx; ++i)
2411 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2412 return WINED3D_OK;
2415 memcpy(&device->state.vs_consts_b[start_idx], constants, count * sizeof(*constants));
2416 if (TRACE_ON(d3d))
2418 for (i = 0; i < count; ++i)
2419 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2422 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2424 return WINED3D_OK;
2427 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2428 unsigned int start_idx, unsigned int count, BOOL *constants)
2430 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2431 device, start_idx, count, constants);
2433 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2434 return WINED3DERR_INVALIDCALL;
2436 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2437 count = WINED3D_MAX_CONSTS_B - start_idx;
2438 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2440 return WINED3D_OK;
2443 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2444 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2446 unsigned int i;
2448 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2449 device, start_idx, count, constants);
2451 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2452 return WINED3DERR_INVALIDCALL;
2454 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2455 count = WINED3D_MAX_CONSTS_I - start_idx;
2457 memcpy(&device->update_stateblock_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2458 if (device->recording)
2460 for (i = start_idx; i < count + start_idx; ++i)
2461 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2462 return WINED3D_OK;
2465 memcpy(&device->state.vs_consts_i[start_idx], constants, count * sizeof(*constants));
2466 if (TRACE_ON(d3d))
2468 for (i = 0; i < count; ++i)
2469 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2472 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2474 return WINED3D_OK;
2477 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2478 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2480 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2481 device, start_idx, count, constants);
2483 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2484 return WINED3DERR_INVALIDCALL;
2486 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2487 count = WINED3D_MAX_CONSTS_I - start_idx;
2488 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2489 return WINED3D_OK;
2492 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2493 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2495 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2496 unsigned int i;
2498 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2499 device, start_idx, count, constants);
2501 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2502 || count > d3d_info->limits.vs_uniform_count - start_idx)
2503 return WINED3DERR_INVALIDCALL;
2505 memcpy(&device->update_stateblock_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2506 if (device->recording)
2508 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2509 count * sizeof(*device->recording->changed.vs_consts_f));
2510 return WINED3D_OK;
2513 memcpy(&device->state.vs_consts_f[start_idx], constants, count * sizeof(*constants));
2514 if (TRACE_ON(d3d))
2516 for (i = 0; i < count; ++i)
2517 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2520 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2522 return WINED3D_OK;
2525 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2526 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2528 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2530 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2531 device, start_idx, count, constants);
2533 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2534 || count > d3d_info->limits.vs_uniform_count - start_idx)
2535 return WINED3DERR_INVALIDCALL;
2537 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2539 return WINED3D_OK;
2542 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2544 struct wined3d_shader *prev = device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2546 TRACE("device %p, shader %p.\n", device, shader);
2548 if (shader)
2549 wined3d_shader_incref(shader);
2550 if (device->update_stateblock_state->ps)
2551 wined3d_shader_decref(device->update_stateblock_state->ps);
2552 device->update_stateblock_state->ps = shader;
2553 if (device->recording)
2555 device->recording->changed.pixelShader = TRUE;
2556 return;
2559 if (shader == prev)
2560 return;
2562 if (shader)
2563 wined3d_shader_incref(shader);
2564 device->state.shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2565 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2566 if (prev)
2567 wined3d_shader_decref(prev);
2570 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2572 TRACE("device %p.\n", device);
2574 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2577 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2578 UINT idx, struct wined3d_shader_resource_view *view)
2580 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2582 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2585 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2586 UINT idx)
2588 TRACE("device %p, idx %u.\n", device, idx);
2590 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2593 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2595 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2597 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2600 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2602 TRACE("device %p, idx %u.\n", device, idx);
2604 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2607 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2608 unsigned int start_idx, unsigned int count, const BOOL *constants)
2610 unsigned int i;
2612 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2613 device, start_idx, count, constants);
2615 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2616 return WINED3DERR_INVALIDCALL;
2618 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2619 count = WINED3D_MAX_CONSTS_B - start_idx;
2621 memcpy(&device->update_stateblock_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2622 if (device->recording)
2624 for (i = start_idx; i < count + start_idx; ++i)
2625 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2626 return WINED3D_OK;
2629 memcpy(&device->state.ps_consts_b[start_idx], constants, count * sizeof(*constants));
2630 if (TRACE_ON(d3d))
2632 for (i = 0; i < count; ++i)
2633 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2636 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2638 return WINED3D_OK;
2641 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2642 unsigned int start_idx, unsigned int count, BOOL *constants)
2644 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2645 device, start_idx, count, constants);
2647 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2648 return WINED3DERR_INVALIDCALL;
2650 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2651 count = WINED3D_MAX_CONSTS_B - start_idx;
2652 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2654 return WINED3D_OK;
2657 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2658 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2660 unsigned int i;
2662 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2663 device, start_idx, count, constants);
2665 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2666 return WINED3DERR_INVALIDCALL;
2668 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2669 count = WINED3D_MAX_CONSTS_I - start_idx;
2671 memcpy(&device->update_stateblock_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2672 if (device->recording)
2674 for (i = start_idx; i < count + start_idx; ++i)
2675 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2676 return WINED3D_OK;
2679 memcpy(&device->state.ps_consts_i[start_idx], constants, count * sizeof(*constants));
2680 if (TRACE_ON(d3d))
2682 for (i = 0; i < count; ++i)
2683 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2686 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2688 return WINED3D_OK;
2691 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2692 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2694 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2695 device, start_idx, count, constants);
2697 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2698 return WINED3DERR_INVALIDCALL;
2700 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2701 count = WINED3D_MAX_CONSTS_I - start_idx;
2702 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2704 return WINED3D_OK;
2707 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2708 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2710 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2711 unsigned int i;
2713 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2714 device, start_idx, count, constants);
2716 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2717 || count > d3d_info->limits.ps_uniform_count - start_idx)
2718 return WINED3DERR_INVALIDCALL;
2720 memcpy(&device->update_stateblock_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2721 if (device->recording)
2723 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2724 count * sizeof(*device->recording->changed.ps_consts_f));
2725 return WINED3D_OK;
2728 memcpy(&device->state.ps_consts_f[start_idx], constants, count * sizeof(*constants));
2729 if (TRACE_ON(d3d))
2731 for (i = 0; i < count; ++i)
2732 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2735 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2737 return WINED3D_OK;
2740 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2741 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2743 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2745 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2746 device, start_idx, count, constants);
2748 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2749 || count > d3d_info->limits.ps_uniform_count - start_idx)
2750 return WINED3DERR_INVALIDCALL;
2752 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2754 return WINED3D_OK;
2757 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2759 struct wined3d_shader *prev;
2761 TRACE("device %p, shader %p.\n", device, shader);
2763 prev = device->state.shader[WINED3D_SHADER_TYPE_HULL];
2764 if (shader == prev)
2765 return;
2766 if (shader)
2767 wined3d_shader_incref(shader);
2768 device->state.shader[WINED3D_SHADER_TYPE_HULL] = shader;
2769 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_HULL, shader);
2770 if (prev)
2771 wined3d_shader_decref(prev);
2774 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2776 TRACE("device %p.\n", device);
2778 return device->state.shader[WINED3D_SHADER_TYPE_HULL];
2781 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2782 unsigned int idx, struct wined3d_shader_resource_view *view)
2784 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2786 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx, view);
2789 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2790 unsigned int idx)
2792 TRACE("device %p, idx %u.\n", device, idx);
2794 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2797 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2798 unsigned int idx, struct wined3d_sampler *sampler)
2800 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2802 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2805 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2807 TRACE("device %p, idx %u.\n", device, idx);
2809 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2812 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2814 struct wined3d_shader *prev;
2816 TRACE("device %p, shader %p.\n", device, shader);
2818 prev = device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2819 if (shader == prev)
2820 return;
2821 if (shader)
2822 wined3d_shader_incref(shader);
2823 device->state.shader[WINED3D_SHADER_TYPE_DOMAIN] = shader;
2824 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_DOMAIN, shader);
2825 if (prev)
2826 wined3d_shader_decref(prev);
2829 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2831 TRACE("device %p.\n", device);
2833 return device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2836 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2837 unsigned int idx, struct wined3d_shader_resource_view *view)
2839 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2841 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2844 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2845 unsigned int idx)
2847 TRACE("device %p, idx %u.\n", device, idx);
2849 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2852 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2853 unsigned int idx, struct wined3d_sampler *sampler)
2855 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2857 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2860 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2862 TRACE("device %p, idx %u.\n", device, idx);
2864 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2867 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2869 struct wined3d_shader *prev = device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2871 TRACE("device %p, shader %p.\n", device, shader);
2873 if (shader == prev)
2874 return;
2875 if (shader)
2876 wined3d_shader_incref(shader);
2877 device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2878 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2879 if (prev)
2880 wined3d_shader_decref(prev);
2883 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2885 TRACE("device %p.\n", device);
2887 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2890 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2891 UINT idx, struct wined3d_shader_resource_view *view)
2893 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2895 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2898 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2899 UINT idx)
2901 TRACE("device %p, idx %u.\n", device, idx);
2903 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2906 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2908 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2910 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2913 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2915 TRACE("device %p, idx %u.\n", device, idx);
2917 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2920 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2922 struct wined3d_shader *prev;
2924 TRACE("device %p, shader %p.\n", device, shader);
2926 prev = device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2927 if (shader == prev)
2928 return;
2929 if (shader)
2930 wined3d_shader_incref(shader);
2931 device->state.shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2932 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2933 if (prev)
2934 wined3d_shader_decref(prev);
2937 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2939 TRACE("device %p.\n", device);
2941 return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2944 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2945 unsigned int idx, struct wined3d_shader_resource_view *view)
2947 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2949 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2952 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2953 unsigned int idx)
2955 TRACE("device %p, idx %u.\n", device, idx);
2957 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2960 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2961 unsigned int idx, struct wined3d_sampler *sampler)
2963 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2965 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2968 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2970 TRACE("device %p, idx %u.\n", device, idx);
2972 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2975 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2976 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2977 unsigned int initial_count)
2979 struct wined3d_unordered_access_view *prev;
2981 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2983 WARN("Invalid UAV index %u.\n", idx);
2984 return;
2987 prev = device->state.unordered_access_view[pipeline][idx];
2988 if (uav == prev && initial_count == ~0u)
2989 return;
2991 if (uav)
2992 wined3d_unordered_access_view_incref(uav);
2993 device->state.unordered_access_view[pipeline][idx] = uav;
2994 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav, initial_count);
2995 if (prev)
2996 wined3d_unordered_access_view_decref(prev);
2999 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
3000 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
3002 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
3004 WARN("Invalid UAV index %u.\n", idx);
3005 return NULL;
3008 return device->state.unordered_access_view[pipeline][idx];
3011 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
3012 struct wined3d_unordered_access_view *uav, unsigned int initial_count)
3014 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
3016 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
3019 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
3020 unsigned int idx)
3022 TRACE("device %p, idx %u.\n", device, idx);
3024 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
3027 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
3028 unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
3030 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
3032 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
3035 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
3036 const struct wined3d_device *device, unsigned int idx)
3038 TRACE("device %p, idx %u.\n", device, idx);
3040 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
3043 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
3045 unsigned int i;
3047 if (!latency)
3048 latency = 3;
3050 device->max_frame_latency = latency;
3051 for (i = 0; i < device->swapchain_count; ++i)
3052 swapchain_set_max_frame_latency(device->swapchains[i], device);
3055 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
3057 return device->max_frame_latency;
3060 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
3062 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3063 unsigned int i, size = 0;
3065 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
3066 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
3067 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
3068 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
3069 switch (fvf & WINED3DFVF_POSITION_MASK)
3071 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
3072 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
3073 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
3074 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
3075 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
3076 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
3077 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
3078 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
3079 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
3081 for (i = 0; i < texcoord_count; ++i)
3083 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
3086 return size;
3089 /* Context activation is done by the caller. */
3090 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3091 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3092 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3094 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3095 struct wined3d_map_desc map_desc;
3096 struct wined3d_box box = {0};
3097 struct wined3d_viewport vp;
3098 unsigned int vertex_size;
3099 unsigned int i;
3100 BYTE *dest_ptr;
3101 BOOL doClip;
3102 DWORD numTextures;
3103 HRESULT hr;
3105 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3107 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3110 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3112 ERR("Source has no position mask\n");
3113 return WINED3DERR_INVALIDCALL;
3116 if (device->state.render_states[WINED3D_RS_CLIPPING])
3118 static BOOL warned = FALSE;
3120 * The clipping code is not quite correct. Some things need
3121 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3122 * so disable clipping for now.
3123 * (The graphics in Half-Life are broken, and my processvertices
3124 * test crashes with IDirect3DDevice3)
3125 doClip = TRUE;
3127 doClip = FALSE;
3128 if(!warned) {
3129 warned = TRUE;
3130 FIXME("Clipping is broken and disabled for now\n");
3133 else
3134 doClip = FALSE;
3136 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3137 box.left = dwDestIndex * vertex_size;
3138 box.right = box.left + dwCount * vertex_size;
3139 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3141 WARN("Failed to map buffer, hr %#x.\n", hr);
3142 return hr;
3144 dest_ptr = map_desc.data;
3146 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3147 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3148 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3150 TRACE("View mat:\n");
3151 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3152 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3153 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3154 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3156 TRACE("Proj mat:\n");
3157 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3158 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3159 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3160 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3162 TRACE("World mat:\n");
3163 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3164 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3165 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3166 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3168 /* Get the viewport */
3169 wined3d_device_get_viewports(device, NULL, &vp);
3170 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3171 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3173 multiply_matrix(&mat,&view_mat,&world_mat);
3174 multiply_matrix(&mat,&proj_mat,&mat);
3176 numTextures = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3178 for (i = 0; i < dwCount; i+= 1) {
3179 unsigned int tex_index;
3181 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3182 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3183 /* The position first */
3184 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3185 const float *p = (const float *)(element->data.addr + i * element->stride);
3186 float x, y, z, rhw;
3187 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3189 /* Multiplication with world, view and projection matrix. */
3190 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3191 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3192 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3193 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3195 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3197 /* WARNING: The following things are taken from d3d7 and were not yet checked
3198 * against d3d8 or d3d9!
3201 /* Clipping conditions: From msdn
3203 * A vertex is clipped if it does not match the following requirements
3204 * -rhw < x <= rhw
3205 * -rhw < y <= rhw
3206 * 0 < z <= rhw
3207 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3209 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3210 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3214 if( !doClip ||
3215 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3216 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3217 ( rhw > eps ) ) ) {
3219 /* "Normal" viewport transformation (not clipped)
3220 * 1) The values are divided by rhw
3221 * 2) The y axis is negative, so multiply it with -1
3222 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3223 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3224 * 4) Multiply x with Width/2 and add Width/2
3225 * 5) The same for the height
3226 * 6) Add the viewpoint X and Y to the 2D coordinates and
3227 * The minimum Z value to z
3228 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3230 * Well, basically it's simply a linear transformation into viewport
3231 * coordinates
3234 x /= rhw;
3235 y /= rhw;
3236 z /= rhw;
3238 y *= -1;
3240 x *= vp.width / 2;
3241 y *= vp.height / 2;
3242 z *= vp.max_z - vp.min_z;
3244 x += vp.width / 2 + vp.x;
3245 y += vp.height / 2 + vp.y;
3246 z += vp.min_z;
3248 rhw = 1 / rhw;
3249 } else {
3250 /* That vertex got clipped
3251 * Contrary to OpenGL it is not dropped completely, it just
3252 * undergoes a different calculation.
3254 TRACE("Vertex got clipped\n");
3255 x += rhw;
3256 y += rhw;
3258 x /= 2;
3259 y /= 2;
3261 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3262 * outside of the main vertex buffer memory. That needs some more
3263 * investigation...
3267 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3270 ( (float *) dest_ptr)[0] = x;
3271 ( (float *) dest_ptr)[1] = y;
3272 ( (float *) dest_ptr)[2] = z;
3273 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3275 dest_ptr += 3 * sizeof(float);
3277 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3278 dest_ptr += sizeof(float);
3281 if (dst_fvf & WINED3DFVF_PSIZE)
3282 dest_ptr += sizeof(DWORD);
3284 if (dst_fvf & WINED3DFVF_NORMAL)
3286 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3287 const float *normal = (const float *)(element->data.addr + i * element->stride);
3288 /* AFAIK this should go into the lighting information */
3289 FIXME("Didn't expect the destination to have a normal\n");
3290 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3293 if (dst_fvf & WINED3DFVF_DIFFUSE)
3295 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3296 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3297 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3299 static BOOL warned = FALSE;
3301 if(!warned) {
3302 ERR("No diffuse color in source, but destination has one\n");
3303 warned = TRUE;
3306 *( (DWORD *) dest_ptr) = 0xffffffff;
3307 dest_ptr += sizeof(DWORD);
3309 else
3311 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3315 if (dst_fvf & WINED3DFVF_SPECULAR)
3317 /* What's the color value in the feedback buffer? */
3318 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3319 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3320 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3322 static BOOL warned = FALSE;
3324 if(!warned) {
3325 ERR("No specular color in source, but destination has one\n");
3326 warned = TRUE;
3329 *(DWORD *)dest_ptr = 0xff000000;
3330 dest_ptr += sizeof(DWORD);
3332 else
3334 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3338 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3340 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3341 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3342 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3344 ERR("No source texture, but destination requests one\n");
3345 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3347 else
3349 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3354 wined3d_resource_unmap(&dest->resource, 0);
3356 return WINED3D_OK;
3358 #undef copy_and_next
3360 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3361 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3362 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3364 struct wined3d_state *state = &device->state;
3365 struct wined3d_stream_info stream_info;
3366 struct wined3d_resource *resource;
3367 struct wined3d_box box = {0};
3368 struct wined3d_shader *vs;
3369 unsigned int i, j;
3370 HRESULT hr;
3371 WORD map;
3373 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3374 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3375 device, src_start_idx, dst_idx, vertex_count,
3376 dst_buffer, declaration, flags, dst_fvf);
3378 if (declaration)
3379 FIXME("Output vertex declaration not implemented yet.\n");
3381 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3382 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3383 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3384 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3386 /* We can't convert FROM a VBO, and vertex buffers used to source into
3387 * process_vertices() are unlikely to ever be used for drawing. Release
3388 * VBOs in those buffers and fix up the stream_info structure.
3390 * Also apply the start index. */
3391 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3393 struct wined3d_stream_info_element *e;
3394 struct wined3d_map_desc map_desc;
3396 if (!(map & 1))
3397 continue;
3399 e = &stream_info.elements[i];
3400 resource = &state->streams[e->stream_idx].buffer->resource;
3401 box.left = src_start_idx * e->stride;
3402 box.right = box.left + vertex_count * e->stride;
3403 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3405 ERR("Failed to map resource.\n");
3406 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3408 if (!(map & 1))
3409 continue;
3411 e = &stream_info.elements[j];
3412 resource = &state->streams[e->stream_idx].buffer->resource;
3413 if (FAILED(wined3d_resource_unmap(resource, 0)))
3414 ERR("Failed to unmap resource.\n");
3416 return WINED3DERR_INVALIDCALL;
3418 e->data.buffer_object = 0;
3419 e->data.addr += (ULONG_PTR)map_desc.data;
3422 hr = process_vertices_strided(device, dst_idx, vertex_count,
3423 &stream_info, dst_buffer, flags, dst_fvf);
3425 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3427 if (!(map & 1))
3428 continue;
3430 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3431 if (FAILED(wined3d_resource_unmap(resource, 0)))
3432 ERR("Failed to unmap resource.\n");
3435 return hr;
3438 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3439 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3441 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3443 TRACE("device %p, stage %u, state %s, value %#x.\n",
3444 device, stage, debug_d3dtexturestate(state), value);
3446 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3448 WARN("Invalid state %#x passed.\n", state);
3449 return;
3452 if (stage >= d3d_info->limits.ffp_blend_stages)
3454 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3455 stage, d3d_info->limits.ffp_blend_stages - 1);
3456 return;
3459 device->update_stateblock_state->texture_states[stage][state] = value;
3461 if (device->recording)
3463 TRACE("Recording... not performing anything.\n");
3464 device->recording->changed.textureState[stage] |= 1u << state;
3465 return;
3468 if (value == device->state.texture_states[stage][state])
3470 TRACE("Application is setting the old value over, nothing to do.\n");
3471 return;
3474 device->state.texture_states[stage][state] = value;
3476 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3479 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3480 UINT stage, enum wined3d_texture_stage_state state)
3482 TRACE("device %p, stage %u, state %s.\n",
3483 device, stage, debug_d3dtexturestate(state));
3485 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3487 WARN("Invalid state %#x passed.\n", state);
3488 return 0;
3491 return device->state.texture_states[stage][state];
3494 void CDECL wined3d_device_set_texture(struct wined3d_device *device,
3495 UINT stage, struct wined3d_texture *texture)
3497 struct wined3d_texture *prev;
3499 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3501 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3502 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - WINED3D_MAX_FRAGMENT_SAMPLERS);
3504 /* Windows accepts overflowing this array... we do not. */
3505 if (stage >= ARRAY_SIZE(device->state.textures))
3507 WARN("Ignoring invalid stage %u.\n", stage);
3508 return;
3511 if (texture)
3512 wined3d_texture_incref(texture);
3513 if (device->update_stateblock_state->textures[stage])
3514 wined3d_texture_decref(device->update_stateblock_state->textures[stage]);
3515 device->update_stateblock_state->textures[stage] = texture;
3517 if (device->recording)
3519 device->recording->changed.textures |= 1u << stage;
3520 return;
3523 prev = device->state.textures[stage];
3524 TRACE("Previous texture %p.\n", prev);
3526 if (texture == prev)
3528 TRACE("App is setting the same texture again, nothing to do.\n");
3529 return;
3532 TRACE("Setting new texture to %p.\n", texture);
3533 device->state.textures[stage] = texture;
3535 if (texture)
3536 wined3d_texture_incref(texture);
3537 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3538 if (prev)
3539 wined3d_texture_decref(prev);
3541 return;
3544 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3546 TRACE("device %p, stage %u.\n", device, stage);
3548 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3549 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - WINED3D_MAX_FRAGMENT_SAMPLERS);
3551 if (stage >= ARRAY_SIZE(device->state.textures))
3553 WARN("Ignoring invalid stage %u.\n", stage);
3554 return NULL; /* Windows accepts overflowing this array ... we do not. */
3557 return device->state.textures[stage];
3560 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
3562 TRACE("device %p, caps %p.\n", device, caps);
3564 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3565 device->create_parms.device_type, caps);
3568 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3569 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3571 struct wined3d_swapchain *swapchain;
3573 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3574 device, swapchain_idx, mode, rotation);
3576 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3577 return WINED3DERR_INVALIDCALL;
3579 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3582 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3584 struct wined3d_stateblock *stateblock;
3585 HRESULT hr;
3587 TRACE("device %p.\n", device);
3589 if (device->recording)
3590 return WINED3DERR_INVALIDCALL;
3592 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3593 if (FAILED(hr))
3594 return hr;
3596 device->recording = stateblock;
3597 device->update_stateblock_state = &stateblock->stateblock_state;
3599 TRACE("Recording stateblock %p.\n", stateblock);
3601 return WINED3D_OK;
3604 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3605 struct wined3d_stateblock **stateblock)
3607 struct wined3d_stateblock *object = device->recording;
3609 TRACE("device %p, stateblock %p.\n", device, stateblock);
3611 if (!device->recording)
3613 WARN("Not recording.\n");
3614 *stateblock = NULL;
3615 return WINED3DERR_INVALIDCALL;
3618 stateblock_init_contained_states(object);
3620 *stateblock = object;
3621 device->recording = NULL;
3622 device->update_stateblock_state = &device->stateblock_state;
3624 TRACE("Returning stateblock %p.\n", *stateblock);
3626 return WINED3D_OK;
3629 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3631 /* At the moment we have no need for any functionality at the beginning
3632 * of a scene. */
3633 TRACE("device %p.\n", device);
3635 if (device->inScene)
3637 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3638 return WINED3DERR_INVALIDCALL;
3640 device->inScene = TRUE;
3641 return WINED3D_OK;
3644 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3646 TRACE("device %p.\n", device);
3648 if (!device->inScene)
3650 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3651 return WINED3DERR_INVALIDCALL;
3654 device->inScene = FALSE;
3655 return WINED3D_OK;
3658 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3659 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3661 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3662 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3664 if (!rect_count && rects)
3666 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3667 return WINED3D_OK;
3670 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3672 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3673 if (!ds)
3675 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3676 /* TODO: What about depth stencil buffers without stencil bits? */
3677 return WINED3DERR_INVALIDCALL;
3679 else if (flags & WINED3DCLEAR_TARGET)
3681 if (ds->width < device->fb.render_targets[0]->width
3682 || ds->height < device->fb.render_targets[0]->height)
3684 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3685 return WINED3D_OK;
3690 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3692 return WINED3D_OK;
3695 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3696 struct wined3d_query *predicate, BOOL value)
3698 struct wined3d_query *prev;
3700 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3702 prev = device->state.predicate;
3703 if (predicate)
3705 FIXME("Predicated rendering not implemented.\n");
3706 wined3d_query_incref(predicate);
3708 device->state.predicate = predicate;
3709 device->state.predicate_value = value;
3710 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3711 if (prev)
3712 wined3d_query_decref(prev);
3715 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3717 TRACE("device %p, value %p.\n", device, value);
3719 if (value)
3720 *value = device->state.predicate_value;
3721 return device->state.predicate;
3724 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3725 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3727 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3728 device, group_count_x, group_count_y, group_count_z);
3730 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3733 void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
3734 struct wined3d_buffer *buffer, unsigned int offset)
3736 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3738 wined3d_cs_emit_dispatch_indirect(device->cs, buffer, offset);
3741 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3742 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
3744 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
3745 device, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
3747 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3748 device->state.gl_patch_vertices = patch_vertex_count;
3751 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3752 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
3754 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
3755 device, primitive_type, patch_vertex_count);
3757 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3758 if (patch_vertex_count)
3759 *patch_vertex_count = device->state.gl_patch_vertices;
3761 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
3764 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3766 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3768 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3769 0, start_vertex, vertex_count, 0, 0, FALSE);
3771 return WINED3D_OK;
3774 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3775 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3777 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3778 device, start_vertex, vertex_count, start_instance, instance_count);
3780 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3781 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3784 void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
3785 struct wined3d_buffer *buffer, unsigned int offset)
3787 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3789 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3790 buffer, offset, FALSE);
3793 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3795 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3797 if (!device->state.index_buffer)
3799 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3800 * without an index buffer set. (The first time at least...)
3801 * D3D8 simply dies, but I doubt it can do much harm to return
3802 * D3DERR_INVALIDCALL there as well. */
3803 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3804 return WINED3DERR_INVALIDCALL;
3807 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3808 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3810 return WINED3D_OK;
3813 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3814 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3816 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3817 device, start_idx, index_count, start_instance, instance_count);
3819 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3820 device->state.base_vertex_index, start_idx, index_count, start_instance, instance_count, TRUE);
3823 void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
3824 struct wined3d_buffer *buffer, unsigned int offset)
3826 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3828 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3829 buffer, offset, TRUE);
3832 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3833 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3835 unsigned int src_size, dst_size, src_skip_levels = 0;
3836 unsigned int src_level_count, dst_level_count;
3837 unsigned int layer_count, level_count, i, j;
3838 unsigned int width, height, depth;
3839 enum wined3d_resource_type type;
3840 struct wined3d_box box;
3842 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3844 /* Verify that the source and destination textures are non-NULL. */
3845 if (!src_texture || !dst_texture)
3847 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3848 return WINED3DERR_INVALIDCALL;
3851 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
3852 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
3854 WARN("Source resource is GPU accessible or a scratch resource.\n");
3855 return WINED3DERR_INVALIDCALL;
3857 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
3859 WARN("Destination resource is CPU accessible.\n");
3860 return WINED3DERR_INVALIDCALL;
3863 /* Verify that the source and destination textures are the same type. */
3864 type = src_texture->resource.type;
3865 if (dst_texture->resource.type != type)
3867 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3868 return WINED3DERR_INVALIDCALL;
3871 layer_count = src_texture->layer_count;
3872 if (layer_count != dst_texture->layer_count)
3874 WARN("Source and destination have different layer counts.\n");
3875 return WINED3DERR_INVALIDCALL;
3878 if (src_texture->resource.format != dst_texture->resource.format)
3880 WARN("Source and destination formats do not match.\n");
3881 return WINED3DERR_INVALIDCALL;
3884 src_level_count = src_texture->level_count;
3885 dst_level_count = dst_texture->level_count;
3886 level_count = min(src_level_count, dst_level_count);
3888 src_size = max(src_texture->resource.width, src_texture->resource.height);
3889 src_size = max(src_size, src_texture->resource.depth);
3890 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3891 dst_size = max(dst_size, dst_texture->resource.depth);
3892 while (src_size > dst_size)
3894 src_size >>= 1;
3895 ++src_skip_levels;
3898 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3899 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3900 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3902 WARN("Source and destination dimensions do not match.\n");
3903 return WINED3DERR_INVALIDCALL;
3906 /* Update every surface level of the texture. */
3907 for (i = 0; i < level_count; ++i)
3909 width = wined3d_texture_get_level_width(dst_texture, i);
3910 height = wined3d_texture_get_level_height(dst_texture, i);
3911 depth = wined3d_texture_get_level_depth(dst_texture, i);
3912 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3914 for (j = 0; j < layer_count; ++j)
3916 wined3d_cs_emit_blt_sub_resource(device->cs,
3917 &dst_texture->resource, j * dst_level_count + i, &box,
3918 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3919 0, NULL, WINED3D_TEXF_POINT);
3923 return WINED3D_OK;
3926 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3928 const struct wined3d_state *state = &device->state;
3929 struct wined3d_texture *texture;
3930 DWORD i;
3932 TRACE("device %p, num_passes %p.\n", device, num_passes);
3934 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
3936 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3938 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3939 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3941 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3943 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3944 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3947 texture = state->textures[i];
3948 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3950 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3952 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3953 return E_FAIL;
3955 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3957 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3958 return E_FAIL;
3960 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3961 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3963 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3964 return E_FAIL;
3968 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3969 || state->render_states[WINED3D_RS_STENCILENABLE])
3971 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3972 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3974 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3976 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3977 return WINED3DERR_CONFLICTINGRENDERSTATE;
3981 /* return a sensible default */
3982 *num_passes = 1;
3984 TRACE("returning D3D_OK\n");
3985 return WINED3D_OK;
3988 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3990 static BOOL warned;
3992 TRACE("device %p, software %#x.\n", device, software);
3994 if (!warned)
3996 FIXME("device %p, software %#x stub!\n", device, software);
3997 warned = TRUE;
4000 device->softwareVertexProcessing = software;
4003 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4005 static BOOL warned;
4007 TRACE("device %p.\n", device);
4009 if (!warned)
4011 TRACE("device %p stub!\n", device);
4012 warned = TRUE;
4015 return device->softwareVertexProcessing;
4018 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4019 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4021 struct wined3d_swapchain *swapchain;
4023 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4024 device, swapchain_idx, raster_status);
4026 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4027 return WINED3DERR_INVALIDCALL;
4029 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4032 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4034 static BOOL warned;
4036 TRACE("device %p, segments %.8e.\n", device, segments);
4038 if (segments != 0.0f)
4040 if (!warned)
4042 FIXME("device %p, segments %.8e stub!\n", device, segments);
4043 warned = TRUE;
4047 return WINED3D_OK;
4050 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4052 static BOOL warned;
4054 TRACE("device %p.\n", device);
4056 if (!warned)
4058 FIXME("device %p stub!\n", device);
4059 warned = TRUE;
4062 return 0.0f;
4065 void CDECL wined3d_device_copy_uav_counter(struct wined3d_device *device,
4066 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4068 TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
4069 device, dst_buffer, offset, uav);
4071 wined3d_cs_emit_copy_uav_counter(device->cs, dst_buffer, offset, uav);
4074 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
4075 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4077 struct wined3d_texture *dst_texture, *src_texture;
4078 struct wined3d_box box;
4079 unsigned int i, j;
4081 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
4083 if (src_resource == dst_resource)
4085 WARN("Source and destination are the same resource.\n");
4086 return;
4089 if (src_resource->type != dst_resource->type)
4091 WARN("Resource types (%s / %s) don't match.\n",
4092 debug_d3dresourcetype(dst_resource->type),
4093 debug_d3dresourcetype(src_resource->type));
4094 return;
4097 if (src_resource->width != dst_resource->width
4098 || src_resource->height != dst_resource->height
4099 || src_resource->depth != dst_resource->depth)
4101 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4102 dst_resource->width, dst_resource->height, dst_resource->depth,
4103 src_resource->width, src_resource->height, src_resource->depth);
4104 return;
4107 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4108 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4110 WARN("Resource formats %s and %s are incompatible.\n",
4111 debug_d3dformat(dst_resource->format->id),
4112 debug_d3dformat(src_resource->format->id));
4113 return;
4116 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4118 wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
4119 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
4120 src_resource, 0, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4121 return;
4124 dst_texture = texture_from_resource(dst_resource);
4125 src_texture = texture_from_resource(src_resource);
4127 if (src_texture->layer_count != dst_texture->layer_count
4128 || src_texture->level_count != dst_texture->level_count)
4130 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4131 dst_texture->layer_count, dst_texture->level_count,
4132 src_texture->layer_count, src_texture->level_count);
4133 return;
4136 for (i = 0; i < dst_texture->level_count; ++i)
4138 wined3d_box_set(&box, 0, 0,
4139 wined3d_texture_get_level_width(dst_texture, i),
4140 wined3d_texture_get_level_height(dst_texture, i),
4141 0, wined3d_texture_get_level_depth(dst_texture, i));
4142 for (j = 0; j < dst_texture->layer_count; ++j)
4144 unsigned int idx = j * dst_texture->level_count + i;
4146 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
4147 src_resource, idx, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4152 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
4153 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4154 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4155 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4157 struct wined3d_box dst_box, b;
4159 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4160 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4161 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4162 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4164 if (flags)
4165 FIXME("Ignoring flags %#x.\n", flags);
4167 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4169 WARN("Source and destination are the same sub-resource.\n");
4170 return WINED3DERR_INVALIDCALL;
4173 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4174 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4176 WARN("Resource formats %s and %s are incompatible.\n",
4177 debug_d3dformat(dst_resource->format->id),
4178 debug_d3dformat(src_resource->format->id));
4179 return WINED3DERR_INVALIDCALL;
4182 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4184 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4186 WARN("Resource types (%s / %s) don't match.\n",
4187 debug_d3dresourcetype(dst_resource->type),
4188 debug_d3dresourcetype(src_resource->type));
4189 return WINED3DERR_INVALIDCALL;
4192 if (dst_sub_resource_idx)
4194 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4195 return WINED3DERR_INVALIDCALL;
4198 if (src_sub_resource_idx)
4200 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4201 return WINED3DERR_INVALIDCALL;
4204 if (!src_box)
4206 unsigned int dst_w;
4208 dst_w = dst_resource->size - dst_x;
4209 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4210 src_box = &b;
4212 else if ((src_box->left >= src_box->right
4213 || src_box->top >= src_box->bottom
4214 || src_box->front >= src_box->back))
4216 WARN("Invalid box %s specified.\n", debug_box(src_box));
4217 return WINED3DERR_INVALIDCALL;
4220 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4221 || src_box->right - src_box->left > dst_resource->size - dst_x)
4223 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4224 dst_x, src_box->left, src_box->right - src_box->left);
4225 return WINED3DERR_INVALIDCALL;
4228 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4230 else
4232 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4233 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4234 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4236 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4238 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4239 return WINED3DERR_INVALIDCALL;
4242 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4244 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4245 return WINED3DERR_INVALIDCALL;
4248 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4250 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4251 return WINED3DERR_INVALIDCALL;
4254 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4256 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4257 return WINED3DERR_INVALIDCALL;
4260 if (!src_box)
4262 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4264 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4265 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4266 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4268 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4269 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4270 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4271 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4273 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4274 src_box = &b;
4276 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4278 WARN("Invalid source box %s.\n", debug_box(src_box));
4279 return WINED3DERR_INVALIDCALL;
4282 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4283 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4284 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4285 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4287 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4288 return WINED3DERR_INVALIDCALL;
4292 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4293 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4295 return WINED3D_OK;
4298 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4299 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4300 unsigned int depth_pitch, unsigned int flags)
4302 unsigned int width, height, depth;
4303 struct wined3d_box b;
4305 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, "
4306 "flags %#x.\n",
4307 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4309 if (flags)
4310 FIXME("Ignoring flags %#x.\n", flags);
4312 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4314 WARN("Resource %p is not GPU accessible.\n", resource);
4315 return;
4318 if (resource->type == WINED3D_RTYPE_BUFFER)
4320 if (sub_resource_idx > 0)
4322 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4323 return;
4326 width = resource->size;
4327 height = 1;
4328 depth = 1;
4330 else
4332 struct wined3d_texture *texture = texture_from_resource(resource);
4333 unsigned int level;
4335 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4337 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4338 return;
4341 level = sub_resource_idx % texture->level_count;
4342 width = wined3d_texture_get_level_width(texture, level);
4343 height = wined3d_texture_get_level_height(texture, level);
4344 depth = wined3d_texture_get_level_depth(texture, level);
4347 if (!box)
4349 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4350 box = &b;
4352 else if (box->left >= box->right || box->right > width
4353 || box->top >= box->bottom || box->bottom > height
4354 || box->front >= box->back || box->back > depth)
4356 WARN("Invalid box %s specified.\n", debug_box(box));
4357 return;
4360 wined3d_resource_wait_idle(resource);
4362 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4365 void CDECL wined3d_device_resolve_sub_resource(struct wined3d_device *device,
4366 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4367 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4368 enum wined3d_format_id format_id)
4370 struct wined3d_texture *dst_texture, *src_texture;
4371 unsigned int dst_level, src_level;
4372 RECT dst_rect, src_rect;
4374 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, "
4375 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4376 device, dst_resource, dst_sub_resource_idx,
4377 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4379 if (wined3d_format_is_typeless(dst_resource->format)
4380 || wined3d_format_is_typeless(src_resource->format))
4382 FIXME("Multisample resolve is not fully supported for typeless formats "
4383 "(dst_format %s, src_format %s, format %s).\n",
4384 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4385 debug_d3dformat(format_id));
4387 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4389 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4390 return;
4392 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4394 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4395 return;
4398 dst_texture = texture_from_resource(dst_resource);
4399 src_texture = texture_from_resource(src_resource);
4401 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4402 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4403 wined3d_texture_get_level_height(dst_texture, dst_level));
4404 src_level = src_sub_resource_idx % src_texture->level_count;
4405 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4406 wined3d_texture_get_level_height(src_texture, src_level));
4407 wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4408 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
4411 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4412 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4413 const struct wined3d_color *color, float depth, DWORD stencil)
4415 struct wined3d_resource *resource;
4416 RECT r;
4418 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4419 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4421 if (!flags)
4422 return WINED3D_OK;
4424 resource = view->resource;
4425 if (resource->type == WINED3D_RTYPE_BUFFER)
4427 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4428 return WINED3DERR_INVALIDCALL;
4431 if (view->layer_count != max(1, resource->depth >> view->desc.u.texture.level_idx))
4433 FIXME("Layered clears not implemented.\n");
4434 return WINED3DERR_INVALIDCALL;
4437 if (!rect)
4439 SetRect(&r, 0, 0, view->width, view->height);
4440 rect = &r;
4442 else
4444 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4445 struct wined3d_texture *texture = texture_from_resource(view->resource);
4446 HRESULT hr;
4448 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4449 view->sub_resource_idx % texture->level_count, &b)))
4450 return hr;
4453 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4455 return WINED3D_OK;
4458 void CDECL wined3d_device_clear_unordered_access_view_uint(struct wined3d_device *device,
4459 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4461 TRACE("device %p, view %p, clear_value %s.\n", device, view, debug_uvec4(clear_value));
4463 wined3d_cs_emit_clear_unordered_access_view_uint(device->cs, view, clear_value);
4466 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4467 unsigned int view_idx)
4469 unsigned int max_rt_count;
4471 TRACE("device %p, view_idx %u.\n", device, view_idx);
4473 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
4474 if (view_idx >= max_rt_count)
4476 WARN("Only %u render targets are supported.\n", max_rt_count);
4477 return NULL;
4480 return device->fb.render_targets[view_idx];
4483 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4485 TRACE("device %p.\n", device);
4487 return device->fb.depth_stencil;
4490 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4491 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4493 struct wined3d_rendertarget_view *prev;
4494 unsigned int max_rt_count;
4496 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4497 device, view_idx, view, set_viewport);
4499 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
4500 if (view_idx >= max_rt_count)
4502 WARN("Only %u render targets are supported.\n", max_rt_count);
4503 return WINED3DERR_INVALIDCALL;
4506 if (view && !(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
4508 WARN("View resource %p doesn't have render target bind flags.\n", view->resource);
4509 return WINED3DERR_INVALIDCALL;
4512 /* Set the viewport and scissor rectangles, if requested. Tests show that
4513 * stateblock recording is ignored, the change goes directly into the
4514 * primary stateblock. */
4515 if (!view_idx && set_viewport)
4517 struct wined3d_state *state = &device->state;
4519 state->viewports[0].x = 0;
4520 state->viewports[0].y = 0;
4521 state->viewports[0].width = view->width;
4522 state->viewports[0].height = view->height;
4523 state->viewports[0].min_z = 0.0f;
4524 state->viewports[0].max_z = 1.0f;
4525 state->viewport_count = 1;
4526 wined3d_cs_emit_set_viewports(device->cs, 1, state->viewports);
4527 device->stateblock_state.viewport = state->viewports[0];
4529 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
4530 state->scissor_rect_count = 1;
4531 wined3d_cs_emit_set_scissor_rects(device->cs, 1, state->scissor_rects);
4532 device->stateblock_state.scissor_rect = state->scissor_rects[0];
4535 prev = device->fb.render_targets[view_idx];
4536 if (view == prev)
4537 return WINED3D_OK;
4539 if (view)
4540 wined3d_rendertarget_view_incref(view);
4541 device->fb.render_targets[view_idx] = view;
4542 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4543 /* Release after the assignment, to prevent device_resource_released()
4544 * from seeing the surface as still in use. */
4545 if (prev)
4546 wined3d_rendertarget_view_decref(prev);
4548 return WINED3D_OK;
4551 HRESULT CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device,
4552 struct wined3d_rendertarget_view *view)
4554 struct wined3d_rendertarget_view *prev;
4556 TRACE("device %p, view %p.\n", device, view);
4558 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
4560 WARN("View resource %p has incompatible %s bind flags.\n",
4561 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
4562 return WINED3DERR_INVALIDCALL;
4565 prev = device->fb.depth_stencil;
4566 if (prev == view)
4568 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4569 return WINED3D_OK;
4572 if ((device->fb.depth_stencil = view))
4573 wined3d_rendertarget_view_incref(view);
4574 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4575 if (prev)
4576 wined3d_rendertarget_view_decref(prev);
4578 return WINED3D_OK;
4581 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4582 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4584 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4585 struct wined3d_sub_resource_data data;
4586 struct wined3d_resource_desc desc;
4587 struct wined3d_map_desc map_desc;
4588 struct wined3d_texture *texture;
4589 HRESULT hr;
4591 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4593 ERR("Failed to map source texture.\n");
4594 return NULL;
4597 data.data = map_desc.data;
4598 data.row_pitch = map_desc.row_pitch;
4599 data.slice_pitch = map_desc.slice_pitch;
4601 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4602 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4603 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4604 desc.multisample_quality = 0;
4605 desc.usage = WINED3DUSAGE_DYNAMIC;
4606 desc.bind_flags = 0;
4607 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4608 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4609 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4610 desc.depth = 1;
4611 desc.size = 0;
4613 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
4614 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4615 if (FAILED(hr))
4617 ERR("Failed to create cursor texture.\n");
4618 return NULL;
4621 return texture;
4624 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4625 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4627 unsigned int texture_level = sub_resource_idx % texture->level_count;
4628 unsigned int cursor_width, cursor_height;
4629 struct wined3d_display_mode mode;
4630 struct wined3d_map_desc map_desc;
4631 HRESULT hr;
4633 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4634 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4636 if (sub_resource_idx >= texture->level_count * texture->layer_count
4637 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4638 return WINED3DERR_INVALIDCALL;
4640 if (device->cursor_texture)
4642 wined3d_texture_decref(device->cursor_texture);
4643 device->cursor_texture = NULL;
4646 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4648 WARN("Texture %p has invalid format %s.\n",
4649 texture, debug_d3dformat(texture->resource.format->id));
4650 return WINED3DERR_INVALIDCALL;
4653 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4655 ERR("Failed to get display mode, hr %#x.\n", hr);
4656 return WINED3DERR_INVALIDCALL;
4659 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4660 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4661 if (cursor_width > mode.width || cursor_height > mode.height)
4663 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4664 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4665 return WINED3DERR_INVALIDCALL;
4668 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4670 /* Do not store the surface's pointer because the application may
4671 * release it after setting the cursor image. Windows doesn't
4672 * addref the set surface, so we can't do this either without
4673 * creating circular refcount dependencies. */
4674 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4676 ERR("Failed to create cursor texture.\n");
4677 return WINED3DERR_INVALIDCALL;
4680 if (cursor_width == 32 && cursor_height == 32)
4682 UINT mask_size = cursor_width * cursor_height / 8;
4683 ICONINFO cursor_info;
4684 DWORD *mask_bits;
4685 HCURSOR cursor;
4687 /* 32-bit user32 cursors ignore the alpha channel if it's all
4688 * zeroes, and use the mask instead. Fill the mask with all ones
4689 * to ensure we still get a fully transparent cursor. */
4690 if (!(mask_bits = heap_alloc(mask_size)))
4691 return E_OUTOFMEMORY;
4692 memset(mask_bits, 0xff, mask_size);
4694 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4695 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
4696 cursor_info.fIcon = FALSE;
4697 cursor_info.xHotspot = x_hotspot;
4698 cursor_info.yHotspot = y_hotspot;
4699 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4700 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4701 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4703 /* Create our cursor and clean up. */
4704 cursor = CreateIconIndirect(&cursor_info);
4705 if (cursor_info.hbmMask)
4706 DeleteObject(cursor_info.hbmMask);
4707 if (cursor_info.hbmColor)
4708 DeleteObject(cursor_info.hbmColor);
4709 if (device->hardwareCursor)
4710 DestroyCursor(device->hardwareCursor);
4711 device->hardwareCursor = cursor;
4712 if (device->bCursorVisible)
4713 SetCursor(cursor);
4715 heap_free(mask_bits);
4718 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4719 device->cursorWidth = cursor_width;
4720 device->cursorHeight = cursor_height;
4721 device->xHotSpot = x_hotspot;
4722 device->yHotSpot = y_hotspot;
4724 return WINED3D_OK;
4727 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4728 int x_screen_space, int y_screen_space, DWORD flags)
4730 TRACE("device %p, x %d, y %d, flags %#x.\n",
4731 device, x_screen_space, y_screen_space, flags);
4733 device->xScreenSpace = x_screen_space;
4734 device->yScreenSpace = y_screen_space;
4736 if (device->hardwareCursor)
4738 POINT pt;
4740 GetCursorPos( &pt );
4741 if (x_screen_space == pt.x && y_screen_space == pt.y)
4742 return;
4743 SetCursorPos( x_screen_space, y_screen_space );
4745 /* Switch to the software cursor if position diverges from the hardware one. */
4746 GetCursorPos( &pt );
4747 if (x_screen_space != pt.x || y_screen_space != pt.y)
4749 if (device->bCursorVisible) SetCursor( NULL );
4750 DestroyCursor( device->hardwareCursor );
4751 device->hardwareCursor = 0;
4756 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4758 BOOL oldVisible = device->bCursorVisible;
4760 TRACE("device %p, show %#x.\n", device, show);
4763 * When ShowCursor is first called it should make the cursor appear at the OS's last
4764 * known cursor position.
4766 if (show && !oldVisible)
4768 POINT pt;
4769 GetCursorPos(&pt);
4770 device->xScreenSpace = pt.x;
4771 device->yScreenSpace = pt.y;
4774 if (device->hardwareCursor)
4776 device->bCursorVisible = show;
4777 if (show)
4778 SetCursor(device->hardwareCursor);
4779 else
4780 SetCursor(NULL);
4782 else if (device->cursor_texture)
4784 device->bCursorVisible = show;
4787 return oldVisible;
4790 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4792 struct wined3d_resource *resource, *cursor;
4794 TRACE("device %p.\n", device);
4796 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4798 TRACE("Checking resource %p for eviction.\n", resource);
4800 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
4802 TRACE("Evicting %p.\n", resource);
4803 wined3d_cs_emit_unload_resource(device->cs, resource);
4808 static void update_swapchain_flags(struct wined3d_texture *texture)
4810 unsigned int flags = texture->swapchain->desc.flags;
4812 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
4813 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4814 else
4815 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
4817 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4818 texture->flags |= WINED3D_TEXTURE_GET_DC;
4819 else
4820 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
4823 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4824 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4825 wined3d_device_reset_cb callback, BOOL reset_state)
4827 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
4828 struct wined3d_resource *resource, *cursor;
4829 struct wined3d_rendertarget_view *view;
4830 struct wined3d_swapchain *swapchain;
4831 struct wined3d_view_desc view_desc;
4832 BOOL backbuffer_resized;
4833 HRESULT hr = WINED3D_OK;
4834 unsigned int i;
4836 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4837 device, swapchain_desc, mode, callback, reset_state);
4839 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4841 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4843 ERR("Failed to get the first implicit swapchain.\n");
4844 return WINED3DERR_INVALIDCALL;
4847 if (reset_state)
4849 if (device->logo_texture)
4851 wined3d_texture_decref(device->logo_texture);
4852 device->logo_texture = NULL;
4854 if (device->cursor_texture)
4856 wined3d_texture_decref(device->cursor_texture);
4857 device->cursor_texture = NULL;
4859 wined3d_stateblock_state_cleanup(&device->stateblock_state);
4860 state_unbind_resources(&device->state);
4863 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
4865 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4867 wined3d_device_set_depth_stencil_view(device, NULL);
4869 if (reset_state)
4871 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4873 TRACE("Enumerating resource %p.\n", resource);
4874 if (FAILED(hr = callback(resource)))
4875 return hr;
4879 TRACE("New params:\n");
4880 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4881 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4882 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4883 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4884 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4885 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4886 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4887 TRACE("device_window %p\n", swapchain_desc->device_window);
4888 TRACE("windowed %#x\n", swapchain_desc->windowed);
4889 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4890 if (swapchain_desc->enable_auto_depth_stencil)
4891 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4892 TRACE("flags %#x\n", swapchain_desc->flags);
4893 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4894 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4896 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
4897 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
4899 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
4900 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
4901 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
4902 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
4904 /* No special treatment of these parameters. Just store them */
4905 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4906 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4907 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4908 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4909 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4911 if (swapchain_desc->device_window
4912 && swapchain_desc->device_window != swapchain->desc.device_window)
4914 TRACE("Changing the device window from %p to %p.\n",
4915 swapchain->desc.device_window, swapchain_desc->device_window);
4916 swapchain->desc.device_window = swapchain_desc->device_window;
4917 swapchain->device_window = swapchain_desc->device_window;
4918 wined3d_swapchain_set_window(swapchain, NULL);
4921 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4922 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4924 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4925 || swapchain->reapply_mode || mode
4926 || (!swapchain_desc->windowed && backbuffer_resized))
4928 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4929 return hr;
4931 else if (!swapchain_desc->windowed)
4933 DWORD style = device->style;
4934 DWORD exStyle = device->exStyle;
4935 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4936 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4937 * Reset to clear up their mess. Guild Wars also loses the device during that.
4939 device->style = 0;
4940 device->exStyle = 0;
4941 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4942 swapchain_desc->backbuffer_width,
4943 swapchain_desc->backbuffer_height);
4944 device->style = style;
4945 device->exStyle = exStyle;
4948 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4949 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4950 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4951 return hr;
4953 if (swapchain_desc->flags != swapchain->desc.flags)
4955 swapchain->desc.flags = swapchain_desc->flags;
4957 update_swapchain_flags(swapchain->front_buffer);
4958 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4960 update_swapchain_flags(swapchain->back_buffers[i]);
4964 if ((view = device->auto_depth_stencil_view))
4966 device->auto_depth_stencil_view = NULL;
4967 wined3d_rendertarget_view_decref(view);
4969 if (swapchain->desc.enable_auto_depth_stencil)
4971 struct wined3d_resource_desc texture_desc;
4972 struct wined3d_texture *texture;
4974 TRACE("Creating the depth stencil buffer.\n");
4976 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4977 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4978 texture_desc.multisample_type = swapchain->desc.multisample_type;
4979 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4980 texture_desc.usage = 0;
4981 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
4982 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4983 texture_desc.width = swapchain->desc.backbuffer_width;
4984 texture_desc.height = swapchain->desc.backbuffer_height;
4985 texture_desc.depth = 1;
4986 texture_desc.size = 0;
4988 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4989 device->device_parent, &texture_desc, 0, &texture)))
4991 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4992 return WINED3DERR_INVALIDCALL;
4995 view_desc.format_id = texture->resource.format->id;
4996 view_desc.flags = 0;
4997 view_desc.u.texture.level_idx = 0;
4998 view_desc.u.texture.level_count = 1;
4999 view_desc.u.texture.layer_idx = 0;
5000 view_desc.u.texture.layer_count = 1;
5001 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5002 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5003 wined3d_texture_decref(texture);
5004 if (FAILED(hr))
5006 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5007 return hr;
5010 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
5013 if ((view = device->back_buffer_view))
5015 device->back_buffer_view = NULL;
5016 wined3d_rendertarget_view_decref(view);
5018 if (swapchain->desc.backbuffer_count && swapchain->desc.backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5020 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5022 view_desc.format_id = back_buffer->format->id;
5023 view_desc.flags = 0;
5024 view_desc.u.texture.level_idx = 0;
5025 view_desc.u.texture.level_count = 1;
5026 view_desc.u.texture.layer_idx = 0;
5027 view_desc.u.texture.layer_count = 1;
5028 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5029 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5031 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5032 return hr;
5036 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5038 if (reset_state)
5040 TRACE("Resetting stateblock.\n");
5041 if (device->recording)
5043 wined3d_stateblock_decref(device->recording);
5044 device->recording = NULL;
5046 wined3d_cs_emit_reset_state(device->cs);
5047 state_cleanup(&device->state);
5049 if (device->d3d_initialized)
5050 wined3d_device_delete_opengl_contexts(device);
5052 memset(&device->state, 0, sizeof(device->state));
5053 state_init(&device->state, &device->fb, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5054 memset(&device->stateblock_state, 0, sizeof(device->stateblock_state));
5055 wined3d_stateblock_state_init(&device->stateblock_state, device, WINED3D_STATE_INIT_DEFAULT);
5056 device->update_stateblock_state = &device->stateblock_state;
5058 device_init_swapchain_state(device, swapchain);
5059 if (wined3d_settings.logo)
5060 device_load_logo(device, wined3d_settings.logo);
5062 else if ((view = device->back_buffer_view))
5064 struct wined3d_state *state = &device->state;
5066 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
5068 /* Note the min_z / max_z is not reset. */
5069 state->viewports[0].x = 0;
5070 state->viewports[0].y = 0;
5071 state->viewports[0].width = view->width;
5072 state->viewports[0].height = view->height;
5073 state->viewport_count = 1;
5074 wined3d_cs_emit_set_viewports(device->cs, 1, state->viewports);
5076 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
5077 state->scissor_rect_count = 1;
5078 wined3d_cs_emit_set_scissor_rects(device->cs, 1, state->scissor_rects);
5081 if (device->d3d_initialized)
5083 if (reset_state)
5084 hr = wined3d_device_create_primary_opengl_context(device);
5087 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5088 * first use
5090 return hr;
5093 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5095 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5097 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5099 return WINED3D_OK;
5103 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5104 struct wined3d_device_creation_parameters *parameters)
5106 TRACE("device %p, parameters %p.\n", device, parameters);
5108 *parameters = device->create_parms;
5111 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5113 TRACE("device %p.\n", device);
5115 return device->wined3d;
5118 enum wined3d_feature_level CDECL wined3d_device_get_feature_level(const struct wined3d_device *device)
5120 TRACE("device %p.\n", device);
5122 return device->feature_level;
5125 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5126 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5128 struct wined3d_swapchain *swapchain;
5130 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5131 device, swapchain_idx, flags, ramp);
5133 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5134 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5137 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5138 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5140 struct wined3d_swapchain *swapchain;
5142 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5143 device, swapchain_idx, ramp);
5145 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5146 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5149 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5151 TRACE("device %p, resource %p.\n", device, resource);
5153 wined3d_not_from_cs(device->cs);
5155 list_add_head(&device->resources, &resource->resource_list_entry);
5158 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5160 TRACE("device %p, resource %p.\n", device, resource);
5162 wined3d_not_from_cs(device->cs);
5164 list_remove(&resource->resource_list_entry);
5167 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5169 enum wined3d_resource_type type = resource->type;
5170 struct wined3d_rendertarget_view *rtv;
5171 unsigned int i;
5173 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5175 if (device->d3d_initialized)
5177 for (i = 0; i < ARRAY_SIZE(device->fb.render_targets); ++i)
5179 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
5180 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5183 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5184 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5187 switch (type)
5189 case WINED3D_RTYPE_TEXTURE_1D:
5190 case WINED3D_RTYPE_TEXTURE_2D:
5191 case WINED3D_RTYPE_TEXTURE_3D:
5192 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5194 if (&device->state.textures[i]->resource == resource)
5196 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5197 device->state.textures[i] = NULL;
5200 if (device->recording && &device->update_stateblock_state->textures[i]->resource == resource)
5202 ERR("Texture resource %p is still in use by recording stateblock %p, stage %u.\n",
5203 resource, device->recording, i);
5204 device->update_stateblock_state->textures[i] = NULL;
5207 break;
5209 case WINED3D_RTYPE_BUFFER:
5210 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5212 if (&device->state.streams[i].buffer->resource == resource)
5214 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5215 device->state.streams[i].buffer = NULL;
5218 if (device->recording && &device->update_stateblock_state->streams[i].buffer->resource == resource)
5220 ERR("Buffer resource %p is still in use by stateblock %p, stream %u.\n",
5221 resource, device->recording, i);
5222 device->update_stateblock_state->streams[i].buffer = NULL;
5226 if (&device->state.index_buffer->resource == resource)
5228 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5229 device->state.index_buffer = NULL;
5232 if (device->recording && &device->update_stateblock_state->index_buffer->resource == resource)
5234 ERR("Buffer resource %p is still in use by stateblock %p as index buffer.\n",
5235 resource, device->recording);
5236 device->update_stateblock_state->index_buffer = NULL;
5238 break;
5240 default:
5241 break;
5244 /* Remove the resource from the resourceStore */
5245 device_resource_remove(device, resource);
5247 TRACE("Resource released.\n");
5250 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5252 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5254 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5257 static BOOL wined3d_select_feature_level(const struct wined3d_adapter *adapter,
5258 const enum wined3d_feature_level *levels, unsigned int level_count,
5259 enum wined3d_feature_level *selected_level)
5261 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
5262 unsigned int i;
5264 for (i = 0; i < level_count; ++i)
5266 if (levels[i] && d3d_info->feature_level >= levels[i])
5268 *selected_level = levels[i];
5269 return TRUE;
5273 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
5274 "with the current shader backend.\n");
5275 return FALSE;
5278 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5279 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5280 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5281 struct wined3d_device_parent *device_parent)
5283 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5284 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5285 const struct fragment_pipeline *fragment_pipeline;
5286 unsigned int i;
5287 HRESULT hr;
5289 if (!wined3d_select_feature_level(adapter, levels, level_count, &device->feature_level))
5290 return E_FAIL;
5292 TRACE("Device feature level %s.\n", wined3d_debug_feature_level(device->feature_level));
5294 device->ref = 1;
5295 device->wined3d = wined3d;
5296 wined3d_incref(device->wined3d);
5297 device->adapter = adapter;
5298 device->device_parent = device_parent;
5299 list_init(&device->resources);
5300 list_init(&device->shaders);
5301 device->surface_alignment = surface_alignment;
5303 /* Save the creation parameters. */
5304 device->create_parms.adapter_idx = adapter_idx;
5305 device->create_parms.device_type = device_type;
5306 device->create_parms.focus_window = focus_window;
5307 device->create_parms.flags = flags;
5309 device->shader_backend = adapter->shader_backend;
5311 vertex_pipeline = adapter->vertex_pipe;
5313 fragment_pipeline = adapter->fragment_pipe;
5315 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5317 if (vertex_pipeline->vp_states && fragment_pipeline->states
5318 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5319 &adapter->d3d_info, adapter->gl_info.supported, vertex_pipeline,
5320 fragment_pipeline, misc_state_template)))
5322 ERR("Failed to compile state table, hr %#x.\n", hr);
5323 wine_rb_destroy(&device->samplers, NULL, NULL);
5324 wined3d_decref(device->wined3d);
5325 return hr;
5328 state_init(&device->state, &device->fb, &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5329 wined3d_stateblock_state_init(&device->stateblock_state, device, WINED3D_STATE_INIT_DEFAULT);
5330 device->update_stateblock_state = &device->stateblock_state;
5332 device->max_frame_latency = 3;
5334 if (!(device->cs = wined3d_cs_create(device)))
5336 WARN("Failed to create command stream.\n");
5337 state_cleanup(&device->state);
5338 wined3d_stateblock_state_cleanup(&device->stateblock_state);
5339 hr = E_FAIL;
5340 goto err;
5343 return WINED3D_OK;
5345 err:
5346 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5348 heap_free(device->multistate_funcs[i]);
5350 wine_rb_destroy(&device->samplers, NULL, NULL);
5351 wined3d_decref(device->wined3d);
5352 return hr;
5355 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5357 DWORD rep = device->state_table[state].representative;
5358 struct wined3d_context *context;
5359 unsigned int i, idx, shift;
5361 wined3d_from_cs(device->cs);
5363 if (STATE_IS_COMPUTE(state))
5365 for (i = 0; i < device->context_count; ++i)
5366 context_invalidate_compute_state(device->contexts[i], state);
5367 return;
5370 for (i = 0; i < device->context_count; ++i)
5372 context = device->contexts[i];
5373 if (isStateDirty(context, rep)) continue;
5375 context->dirtyArray[context->numDirtyEntries++] = rep;
5376 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5377 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5378 context->isStateDirty[idx] |= (1u << shift);
5382 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5383 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5385 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5387 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5388 window, message, wparam, lparam);
5389 if (unicode)
5390 return DefWindowProcW(window, message, wparam, lparam);
5391 else
5392 return DefWindowProcA(window, message, wparam, lparam);
5395 if (message == WM_DESTROY)
5397 TRACE("unregister window %p.\n", window);
5398 wined3d_unregister_window(window);
5400 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5401 ERR("Window %p is not the focus window for device %p.\n", window, device);
5403 else if (message == WM_DISPLAYCHANGE)
5405 device->device_parent->ops->mode_changed(device->device_parent);
5407 else if (message == WM_ACTIVATEAPP)
5409 unsigned int i = device->swapchain_count;
5411 /* Deactivating the implicit swapchain may cause the application
5412 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5413 * deactivate the implicit swapchain last, and to avoid accessing the
5414 * "device" pointer afterwards. */
5415 while (i--)
5416 wined3d_swapchain_activate(device->swapchains[i], wparam);
5418 else if (message == WM_SYSCOMMAND)
5420 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5422 if (unicode)
5423 DefWindowProcW(window, message, wparam, lparam);
5424 else
5425 DefWindowProcA(window, message, wparam, lparam);
5429 if (unicode)
5430 return CallWindowProcW(proc, window, message, wparam, lparam);
5431 else
5432 return CallWindowProcA(proc, window, message, wparam, lparam);