iccvid: Use the ARRAY_SIZE() macro.
[wine.git] / dlls / wined3d / device.c
blobb1a322e33de8496da98f0029fbbda691006a067a
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 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
481 ULONG refcount = InterlockedDecrement(&device->ref);
483 TRACE("%p decreasing refcount to %u.\n", device, refcount);
485 if (!refcount)
487 UINT i;
489 wined3d_cs_destroy(device->cs);
491 if (device->recording && wined3d_stateblock_decref(device->recording))
492 ERR("Something's still holding the recording stateblock.\n");
493 device->recording = NULL;
495 state_cleanup(&device->state);
497 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
499 heap_free(device->multistate_funcs[i]);
500 device->multistate_funcs[i] = NULL;
503 if (!list_empty(&device->resources))
505 struct wined3d_resource *resource;
507 ERR("Device released with resources still bound.\n");
509 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
511 ERR("Leftover resource %p with type %s (%#x).\n",
512 resource, debug_d3dresourcetype(resource->type), resource->type);
516 if (device->contexts)
517 ERR("Context array not freed!\n");
518 if (device->hardwareCursor)
519 DestroyCursor(device->hardwareCursor);
520 device->hardwareCursor = 0;
522 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
524 wined3d_decref(device->wined3d);
525 device->wined3d = NULL;
526 heap_free(wined3d_device_gl(device));
527 TRACE("Freed device %p.\n", device);
530 return refcount;
533 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
535 TRACE("device %p.\n", device);
537 return device->swapchain_count;
540 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
542 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
544 if (swapchain_idx >= device->swapchain_count)
546 WARN("swapchain_idx %u >= swapchain_count %u.\n",
547 swapchain_idx, device->swapchain_count);
548 return NULL;
551 return device->swapchains[swapchain_idx];
554 static void device_load_logo(struct wined3d_device *device, const char *filename)
556 struct wined3d_color_key color_key;
557 struct wined3d_resource_desc desc;
558 HBITMAP hbm;
559 BITMAP bm;
560 HRESULT hr;
561 HDC dcb = NULL, dcs = NULL;
563 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
565 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
566 return;
568 GetObjectA(hbm, sizeof(BITMAP), &bm);
570 if (!(dcb = CreateCompatibleDC(NULL)))
571 goto out;
572 SelectObject(dcb, hbm);
574 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
575 desc.format = WINED3DFMT_B5G6R5_UNORM;
576 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
577 desc.multisample_quality = 0;
578 desc.usage = WINED3DUSAGE_DYNAMIC;
579 desc.bind_flags = 0;
580 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
581 desc.width = bm.bmWidth;
582 desc.height = bm.bmHeight;
583 desc.depth = 1;
584 desc.size = 0;
585 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1,
586 WINED3D_TEXTURE_CREATE_MAPPABLE | WINED3D_TEXTURE_CREATE_GET_DC,
587 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
589 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
590 goto out;
593 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
595 wined3d_texture_decref(device->logo_texture);
596 device->logo_texture = NULL;
597 goto out;
599 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
600 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
602 color_key.color_space_low_value = 0;
603 color_key.color_space_high_value = 0;
604 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
606 out:
607 if (dcb) DeleteDC(dcb);
608 if (hbm) DeleteObject(hbm);
611 /* Context activation is done by the caller. */
612 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
614 struct wined3d_dummy_textures *textures = &wined3d_device_gl(device)->dummy_textures;
615 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
616 const struct wined3d_gl_info *gl_info = context->gl_info;
617 unsigned int i;
618 DWORD color;
620 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
621 color = 0x000000ff;
622 else
623 color = 0x00000000;
625 /* Under DirectX you can sample even if no texture is bound, whereas
626 * OpenGL will only allow that when a valid texture is bound.
627 * We emulate this by creating dummy textures and binding them
628 * to each texture stage when the currently set D3D texture is NULL. */
629 context_active_texture(context, gl_info, 0);
631 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
632 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
633 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
634 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
635 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
637 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
638 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
639 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
640 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
641 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
643 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
645 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
646 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
647 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
648 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
649 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
652 if (gl_info->supported[EXT_TEXTURE3D])
654 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
655 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
656 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
657 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
658 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
661 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
663 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
664 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
665 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
666 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
668 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
669 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
673 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
675 DWORD cube_array_data[6];
677 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
678 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
679 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
680 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
681 cube_array_data[i] = color;
682 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
683 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
686 if (gl_info->supported[EXT_TEXTURE_ARRAY])
688 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
689 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
690 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
691 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
692 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
694 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
695 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
696 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
697 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
698 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
701 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
703 GLuint buffer;
705 GL_EXTCALL(glGenBuffers(1, &buffer));
706 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
707 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
708 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
710 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
711 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
712 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
713 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
714 GL_EXTCALL(glDeleteBuffers(1, &buffer));
717 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
719 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
720 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
721 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
722 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
724 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
725 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
726 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
727 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
729 if (gl_info->supported[ARB_CLEAR_TEXTURE])
731 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
732 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
734 else
736 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
740 checkGLcall("create dummy textures");
742 context_bind_dummy_textures(context);
745 /* Context activation is done by the caller. */
746 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
748 struct wined3d_dummy_textures *dummy_textures = &wined3d_device_gl(device)->dummy_textures;
749 const struct wined3d_gl_info *gl_info = context->gl_info;
751 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
753 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
754 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
757 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
758 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
760 if (gl_info->supported[EXT_TEXTURE_ARRAY])
762 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
763 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
766 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
767 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
769 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
770 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
772 if (gl_info->supported[EXT_TEXTURE3D])
773 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
775 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
776 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
778 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
779 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
781 checkGLcall("delete dummy textures");
783 memset(dummy_textures, 0, sizeof(*dummy_textures));
786 /* Context activation is done by the caller. */
787 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
789 struct wined3d_sampler_desc desc;
790 HRESULT hr;
792 desc.address_u = WINED3D_TADDRESS_WRAP;
793 desc.address_v = WINED3D_TADDRESS_WRAP;
794 desc.address_w = WINED3D_TADDRESS_WRAP;
795 memset(desc.border_color, 0, sizeof(desc.border_color));
796 desc.mag_filter = WINED3D_TEXF_POINT;
797 desc.min_filter = WINED3D_TEXF_POINT;
798 desc.mip_filter = WINED3D_TEXF_NONE;
799 desc.lod_bias = 0.0f;
800 desc.min_lod = -1000.0f;
801 desc.max_lod = 1000.0f;
802 desc.mip_base_level = 0;
803 desc.max_anisotropy = 1;
804 desc.compare = FALSE;
805 desc.comparison_func = WINED3D_CMP_NEVER;
806 desc.srgb_decode = TRUE;
808 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
809 * instructions allow access to resources without using samplers.
810 * In GLSL, resources are always accessed through sampler or image variables. The default
811 * sampler object is used to emulate the direct resource access when there is no sampler state
812 * to use.
814 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
816 ERR("Failed to create default sampler, hr %#x.\n", hr);
817 device->default_sampler = NULL;
820 /* In D3D10+, a NULL sampler maps to the default sampler state. */
821 desc.address_u = WINED3D_TADDRESS_CLAMP;
822 desc.address_v = WINED3D_TADDRESS_CLAMP;
823 desc.address_w = WINED3D_TADDRESS_CLAMP;
824 desc.mag_filter = WINED3D_TEXF_LINEAR;
825 desc.min_filter = WINED3D_TEXF_LINEAR;
826 desc.mip_filter = WINED3D_TEXF_LINEAR;
827 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
829 ERR("Failed to create null sampler, hr %#x.\n", hr);
830 device->null_sampler = NULL;
834 /* Context activation is done by the caller. */
835 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
837 wined3d_sampler_decref(device->default_sampler);
838 device->default_sampler = NULL;
839 wined3d_sampler_decref(device->null_sampler);
840 device->null_sampler = NULL;
843 static LONG fullscreen_style(LONG style)
845 /* Make sure the window is managed, otherwise we won't get keyboard input. */
846 style |= WS_POPUP | WS_SYSMENU;
847 style &= ~(WS_CAPTION | WS_THICKFRAME);
849 return style;
852 static LONG fullscreen_exstyle(LONG exstyle)
854 /* Filter out window decorations. */
855 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
857 return exstyle;
860 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
862 BOOL filter_messages;
863 LONG style, exstyle;
865 TRACE("Setting up window %p for fullscreen mode.\n", window);
867 if (device->style || device->exStyle)
869 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
870 window, device->style, device->exStyle);
873 device->style = GetWindowLongW(window, GWL_STYLE);
874 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
876 style = fullscreen_style(device->style);
877 exstyle = fullscreen_exstyle(device->exStyle);
879 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
880 device->style, device->exStyle, style, exstyle);
882 filter_messages = device->filter_messages;
883 device->filter_messages = TRUE;
885 SetWindowLongW(window, GWL_STYLE, style);
886 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
887 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
889 device->filter_messages = filter_messages;
892 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
893 const RECT *window_rect)
895 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
896 BOOL filter_messages;
897 LONG style, exstyle;
898 RECT rect = {0};
900 if (!device->style && !device->exStyle)
901 return;
903 style = GetWindowLongW(window, GWL_STYLE);
904 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
906 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
907 * application, and we want to ignore them in the test below, since it's
908 * not the application's fault that they changed. Additionally, we want to
909 * preserve the current status of these flags (i.e. don't restore them) to
910 * more closely emulate the behavior of Direct3D, which leaves these flags
911 * alone when returning to windowed mode. */
912 device->style ^= (device->style ^ style) & WS_VISIBLE;
913 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
915 TRACE("Restoring window style of window %p to %08x, %08x.\n",
916 window, device->style, device->exStyle);
918 filter_messages = device->filter_messages;
919 device->filter_messages = TRUE;
921 /* Only restore the style if the application didn't modify it during the
922 * fullscreen phase. Some applications change it before calling Reset()
923 * when switching between windowed and fullscreen modes (HL2), some
924 * depend on the original style (Eve Online). */
925 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
927 SetWindowLongW(window, GWL_STYLE, device->style);
928 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
931 if (window_rect)
932 rect = *window_rect;
933 else
934 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
935 SetWindowPos(window, 0, rect.left, rect.top,
936 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
938 device->filter_messages = filter_messages;
940 /* Delete the old values. */
941 device->style = 0;
942 device->exStyle = 0;
945 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
947 TRACE("device %p, window %p.\n", device, window);
949 if (!wined3d_register_window(window, device))
951 ERR("Failed to register window %p.\n", window);
952 return E_FAIL;
955 InterlockedExchangePointer((void **)&device->focus_window, window);
956 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
958 return WINED3D_OK;
961 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
963 TRACE("device %p.\n", device);
965 if (device->focus_window) wined3d_unregister_window(device->focus_window);
966 InterlockedExchangePointer((void **)&device->focus_window, NULL);
969 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
971 BOOL ds_enable = swapchain->desc.enable_auto_depth_stencil;
972 unsigned int i;
974 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
976 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
978 if (device->back_buffer_view)
979 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
981 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
984 static void wined3d_device_delete_opengl_contexts_cs(void *object)
986 struct wined3d_resource *resource, *cursor;
987 struct wined3d_device *device = object;
988 struct wined3d_context *context;
989 struct wined3d_shader *shader;
991 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
993 TRACE("Unloading resource %p.\n", resource);
994 wined3d_cs_emit_unload_resource(device->cs, resource);
997 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
999 device->shader_backend->shader_destroy(shader);
1002 context = context_acquire(device, NULL, 0);
1003 device->blitter->ops->blitter_destroy(device->blitter, context);
1004 device->shader_backend->shader_free_private(device);
1005 destroy_dummy_textures(device, context);
1006 destroy_default_samplers(device, context);
1007 context_release(context);
1009 while (device->context_count)
1011 if (device->contexts[0]->swapchain)
1012 swapchain_destroy_contexts(device->contexts[0]->swapchain);
1013 else
1014 context_destroy(device, device->contexts[0]);
1018 static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
1020 wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
1021 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1024 static void wined3d_device_create_primary_opengl_context_cs(void *object)
1026 struct wined3d_device *device = object;
1027 struct wined3d_swapchain *swapchain;
1028 struct wined3d_context *context;
1029 struct wined3d_texture *target;
1030 HRESULT hr;
1032 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1033 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1035 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1036 return;
1039 if (!(device->blitter = wined3d_cpu_blitter_create()))
1041 ERR("Failed to create CPU blitter.\n");
1042 device->shader_backend->shader_free_private(device);
1043 return;
1045 wined3d_ffp_blitter_create(&device->blitter, &device->adapter->gl_info);
1046 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1047 wined3d_arbfp_blitter_create(&device->blitter, device);
1048 wined3d_fbo_blitter_create(&device->blitter, &device->adapter->gl_info);
1049 wined3d_raw_blitter_create(&device->blitter, &device->adapter->gl_info);
1051 swapchain = device->swapchains[0];
1052 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1053 context = context_acquire(device, target, 0);
1054 create_dummy_textures(device, context);
1055 create_default_samplers(device, context);
1056 context_release(context);
1059 static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1061 wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1062 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1063 if (!device->swapchains[0]->num_contexts)
1064 return E_FAIL;
1066 return WINED3D_OK;
1069 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1070 struct wined3d_swapchain_desc *swapchain_desc)
1072 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1073 struct wined3d_swapchain *swapchain = NULL;
1074 DWORD clear_flags = 0;
1075 HRESULT hr;
1077 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1079 if (device->d3d_initialized)
1080 return WINED3DERR_INVALIDCALL;
1081 if (device->wined3d->flags & WINED3D_NO3D)
1082 return WINED3DERR_INVALIDCALL;
1084 memset(device->fb.render_targets, 0, sizeof(device->fb.render_targets));
1086 /* Setup the implicit swapchain. This also initializes a context. */
1087 TRACE("Creating implicit swapchain.\n");
1088 if (FAILED(hr = device->device_parent->ops->create_swapchain(device->device_parent,
1089 swapchain_desc, &swapchain)))
1091 WARN("Failed to create implicit swapchain.\n");
1092 goto err_out;
1095 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1097 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1098 struct wined3d_view_desc view_desc;
1100 view_desc.format_id = back_buffer->format->id;
1101 view_desc.flags = 0;
1102 view_desc.u.texture.level_idx = 0;
1103 view_desc.u.texture.level_count = 1;
1104 view_desc.u.texture.layer_idx = 0;
1105 view_desc.u.texture.layer_count = 1;
1106 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1107 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1109 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1110 goto err_out;
1114 device->swapchain_count = 1;
1115 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1117 ERR("Out of memory!\n");
1118 goto err_out;
1120 device->swapchains[0] = swapchain;
1122 if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1123 goto err_out;
1124 device_init_swapchain_state(device, swapchain);
1126 device->contexts[0]->last_was_rhw = 0;
1128 TRACE("All defaults now set up.\n");
1130 /* Clear the screen */
1131 if (device->back_buffer_view)
1132 clear_flags |= WINED3DCLEAR_TARGET;
1133 if (swapchain_desc->enable_auto_depth_stencil)
1134 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1135 if (clear_flags)
1136 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1138 device->d3d_initialized = TRUE;
1140 if (wined3d_settings.logo)
1141 device_load_logo(device, wined3d_settings.logo);
1142 return WINED3D_OK;
1144 err_out:
1145 heap_free(device->swapchains);
1146 device->swapchain_count = 0;
1147 if (device->back_buffer_view)
1148 wined3d_rendertarget_view_decref(device->back_buffer_view);
1149 if (swapchain)
1150 wined3d_swapchain_decref(swapchain);
1152 return hr;
1155 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1156 struct wined3d_swapchain_desc *swapchain_desc)
1158 struct wined3d_swapchain *swapchain = NULL;
1159 HRESULT hr;
1161 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1163 /* Setup the implicit swapchain */
1164 TRACE("Creating implicit swapchain\n");
1165 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1166 swapchain_desc, &swapchain);
1167 if (FAILED(hr))
1169 WARN("Failed to create implicit swapchain\n");
1170 goto err_out;
1173 device->swapchain_count = 1;
1174 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1176 ERR("Out of memory!\n");
1177 goto err_out;
1179 device->swapchains[0] = swapchain;
1181 if (!(device->blitter = wined3d_cpu_blitter_create()))
1183 ERR("Failed to create CPU blitter.\n");
1184 heap_free(device->swapchains);
1185 device->swapchain_count = 0;
1186 goto err_out;
1189 return WINED3D_OK;
1191 err_out:
1192 wined3d_swapchain_decref(swapchain);
1193 return hr;
1196 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1198 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1200 wined3d_sampler_decref(sampler);
1203 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1205 unsigned int i;
1207 TRACE("device %p.\n", device);
1209 if (!device->d3d_initialized)
1210 return WINED3DERR_INVALIDCALL;
1212 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1214 if (device->logo_texture)
1215 wined3d_texture_decref(device->logo_texture);
1216 if (device->cursor_texture)
1217 wined3d_texture_decref(device->cursor_texture);
1219 state_unbind_resources(&device->state);
1221 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1223 wined3d_device_delete_opengl_contexts(device);
1225 if (device->fb.depth_stencil)
1227 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1229 TRACE("Releasing depth/stencil view %p.\n", view);
1231 device->fb.depth_stencil = NULL;
1232 wined3d_rendertarget_view_decref(view);
1235 if (device->auto_depth_stencil_view)
1237 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1239 device->auto_depth_stencil_view = NULL;
1240 if (wined3d_rendertarget_view_decref(view))
1241 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1244 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
1246 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1248 if (device->back_buffer_view)
1250 wined3d_rendertarget_view_decref(device->back_buffer_view);
1251 device->back_buffer_view = NULL;
1254 for (i = 0; i < device->swapchain_count; ++i)
1256 TRACE("Releasing the implicit swapchain %u.\n", i);
1257 if (wined3d_swapchain_decref(device->swapchains[i]))
1258 FIXME("Something's still holding the implicit swapchain.\n");
1261 heap_free(device->swapchains);
1262 device->swapchains = NULL;
1263 device->swapchain_count = 0;
1265 device->d3d_initialized = FALSE;
1267 return WINED3D_OK;
1270 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1272 unsigned int i;
1274 device->blitter->ops->blitter_destroy(device->blitter, NULL);
1276 for (i = 0; i < device->swapchain_count; ++i)
1278 TRACE("Releasing the implicit swapchain %u.\n", i);
1279 if (wined3d_swapchain_decref(device->swapchains[i]))
1280 FIXME("Something's still holding the implicit swapchain.\n");
1283 heap_free(device->swapchains);
1284 device->swapchains = NULL;
1285 device->swapchain_count = 0;
1286 return WINED3D_OK;
1289 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1290 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1291 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1293 * There is no way to deactivate thread safety once it is enabled.
1295 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1297 TRACE("device %p.\n", device);
1299 /* For now just store the flag (needed in case of ddraw). */
1300 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1303 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1305 TRACE("device %p.\n", device);
1307 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1308 wine_dbgstr_longlong(device->adapter->vram_bytes),
1309 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1310 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1312 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1315 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1316 struct wined3d_buffer *buffer, UINT offset)
1318 struct wined3d_stream_output *stream;
1319 struct wined3d_buffer *prev_buffer;
1321 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1323 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1325 WARN("Invalid stream output %u.\n", idx);
1326 return;
1329 stream = &device->update_state->stream_output[idx];
1330 prev_buffer = stream->buffer;
1332 if (buffer)
1333 wined3d_buffer_incref(buffer);
1334 stream->buffer = buffer;
1335 stream->offset = offset;
1336 if (!device->recording)
1337 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1338 if (prev_buffer)
1339 wined3d_buffer_decref(prev_buffer);
1342 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1343 UINT idx, UINT *offset)
1345 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1347 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1349 WARN("Invalid stream output %u.\n", idx);
1350 return NULL;
1353 if (offset)
1354 *offset = device->state.stream_output[idx].offset;
1355 return device->state.stream_output[idx].buffer;
1358 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1359 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1361 struct wined3d_stream_state *stream;
1362 struct wined3d_buffer *prev_buffer;
1364 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1365 device, stream_idx, buffer, offset, stride);
1367 if (stream_idx >= MAX_STREAMS)
1369 WARN("Stream index %u out of range.\n", stream_idx);
1370 return WINED3DERR_INVALIDCALL;
1372 else if (offset & 0x3)
1374 WARN("Offset %u is not 4 byte aligned.\n", offset);
1375 return WINED3DERR_INVALIDCALL;
1378 stream = &device->update_state->streams[stream_idx];
1379 prev_buffer = stream->buffer;
1381 if (device->recording)
1382 device->recording->changed.streamSource |= 1u << stream_idx;
1384 if (prev_buffer == buffer
1385 && stream->stride == stride
1386 && stream->offset == offset)
1388 TRACE("Application is setting the old values over, nothing to do.\n");
1389 return WINED3D_OK;
1392 stream->buffer = buffer;
1393 stream->stride = stride;
1394 stream->offset = offset;
1395 if (buffer)
1396 wined3d_buffer_incref(buffer);
1398 if (!device->recording)
1399 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1400 if (prev_buffer)
1401 wined3d_buffer_decref(prev_buffer);
1403 return WINED3D_OK;
1406 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1407 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1409 const struct wined3d_stream_state *stream;
1411 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1412 device, stream_idx, buffer, offset, stride);
1414 if (stream_idx >= MAX_STREAMS)
1416 WARN("Stream index %u out of range.\n", stream_idx);
1417 return WINED3DERR_INVALIDCALL;
1420 stream = &device->state.streams[stream_idx];
1421 *buffer = stream->buffer;
1422 if (offset)
1423 *offset = stream->offset;
1424 *stride = stream->stride;
1426 return WINED3D_OK;
1429 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1431 struct wined3d_stream_state *stream;
1432 UINT old_flags, old_freq;
1434 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1436 /* Verify input. At least in d3d9 this is invalid. */
1437 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1439 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1440 return WINED3DERR_INVALIDCALL;
1442 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1444 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1445 return WINED3DERR_INVALIDCALL;
1447 if (!divider)
1449 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1450 return WINED3DERR_INVALIDCALL;
1453 stream = &device->update_state->streams[stream_idx];
1454 old_flags = stream->flags;
1455 old_freq = stream->frequency;
1457 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1458 stream->frequency = divider & 0x7fffff;
1460 if (device->recording)
1461 device->recording->changed.streamFreq |= 1u << stream_idx;
1462 else if (stream->frequency != old_freq || stream->flags != old_flags)
1463 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1465 return WINED3D_OK;
1468 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1469 UINT stream_idx, UINT *divider)
1471 const struct wined3d_stream_state *stream;
1473 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1475 stream = &device->state.streams[stream_idx];
1476 *divider = stream->flags | stream->frequency;
1478 TRACE("Returning %#x.\n", *divider);
1480 return WINED3D_OK;
1483 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1484 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1486 TRACE("device %p, state %s, matrix %p.\n",
1487 device, debug_d3dtstype(d3dts), matrix);
1488 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1489 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1490 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1491 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1493 /* Handle recording of state blocks. */
1494 if (device->recording)
1496 TRACE("Recording... not performing anything.\n");
1497 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1498 device->update_state->transforms[d3dts] = *matrix;
1499 return;
1502 /* If the new matrix is the same as the current one,
1503 * we cut off any further processing. this seems to be a reasonable
1504 * optimization because as was noticed, some apps (warcraft3 for example)
1505 * tend towards setting the same matrix repeatedly for some reason.
1507 * From here on we assume that the new matrix is different, wherever it matters. */
1508 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1510 TRACE("The application is setting the same matrix over again.\n");
1511 return;
1514 device->state.transforms[d3dts] = *matrix;
1515 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1518 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1519 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1521 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1523 *matrix = device->state.transforms[state];
1526 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1527 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1529 const struct wined3d_matrix *mat;
1530 struct wined3d_matrix temp;
1532 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1534 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1535 * below means it will be recorded in a state block change, but it
1536 * works regardless where it is recorded.
1537 * If this is found to be wrong, change to StateBlock. */
1538 if (state > HIGHEST_TRANSFORMSTATE)
1540 WARN("Unhandled transform state %#x.\n", state);
1541 return;
1544 mat = &device->update_state->transforms[state];
1545 multiply_matrix(&temp, mat, matrix);
1547 /* Apply change via set transform - will reapply to eg. lights this way. */
1548 wined3d_device_set_transform(device, state, &temp);
1551 /* Note lights are real special cases. Although the device caps state only
1552 * e.g. 8 are supported, you can reference any indexes you want as long as
1553 * that number max are enabled at any one point in time. Therefore since the
1554 * indices can be anything, we need a hashmap of them. However, this causes
1555 * stateblock problems. When capturing the state block, I duplicate the
1556 * hashmap, but when recording, just build a chain pretty much of commands to
1557 * be replayed. */
1558 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1559 UINT light_idx, const struct wined3d_light *light)
1561 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1562 struct wined3d_light_info *object = NULL;
1563 float rho;
1565 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1567 /* Check the parameter range. Need for speed most wanted sets junk lights
1568 * which confuse the GL driver. */
1569 if (!light)
1570 return WINED3DERR_INVALIDCALL;
1572 switch (light->type)
1574 case WINED3D_LIGHT_POINT:
1575 case WINED3D_LIGHT_SPOT:
1576 case WINED3D_LIGHT_GLSPOT:
1577 /* Incorrect attenuation values can cause the gl driver to crash.
1578 * Happens with Need for speed most wanted. */
1579 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1581 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1582 return WINED3DERR_INVALIDCALL;
1584 break;
1586 case WINED3D_LIGHT_DIRECTIONAL:
1587 case WINED3D_LIGHT_PARALLELPOINT:
1588 /* Ignores attenuation */
1589 break;
1591 default:
1592 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1593 return WINED3DERR_INVALIDCALL;
1596 if (!(object = wined3d_state_get_light(device->update_state, light_idx)))
1598 TRACE("Adding new light\n");
1599 if (!(object = heap_alloc_zero(sizeof(*object))))
1600 return E_OUTOFMEMORY;
1602 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1603 object->glIndex = -1;
1604 object->OriginalIndex = light_idx;
1607 /* Initialize the object. */
1608 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1609 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1610 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1611 light_idx, light->type, debug_color(&light->diffuse),
1612 debug_color(&light->specular), debug_color(&light->ambient),
1613 light->position.x, light->position.y, light->position.z,
1614 light->direction.x, light->direction.y, light->direction.z,
1615 light->range, light->falloff, light->theta, light->phi);
1617 /* Save away the information. */
1618 object->OriginalParms = *light;
1620 switch (light->type)
1622 case WINED3D_LIGHT_POINT:
1623 /* Position */
1624 object->position.x = light->position.x;
1625 object->position.y = light->position.y;
1626 object->position.z = light->position.z;
1627 object->position.w = 1.0f;
1628 object->cutoff = 180.0f;
1629 /* FIXME: Range */
1630 break;
1632 case WINED3D_LIGHT_DIRECTIONAL:
1633 /* Direction */
1634 object->direction.x = -light->direction.x;
1635 object->direction.y = -light->direction.y;
1636 object->direction.z = -light->direction.z;
1637 object->direction.w = 0.0f;
1638 object->exponent = 0.0f;
1639 object->cutoff = 180.0f;
1640 break;
1642 case WINED3D_LIGHT_SPOT:
1643 /* Position */
1644 object->position.x = light->position.x;
1645 object->position.y = light->position.y;
1646 object->position.z = light->position.z;
1647 object->position.w = 1.0f;
1649 /* Direction */
1650 object->direction.x = light->direction.x;
1651 object->direction.y = light->direction.y;
1652 object->direction.z = light->direction.z;
1653 object->direction.w = 0.0f;
1655 /* opengl-ish and d3d-ish spot lights use too different models
1656 * for the light "intensity" as a function of the angle towards
1657 * the main light direction, so we only can approximate very
1658 * roughly. However, spot lights are rather rarely used in games
1659 * (if ever used at all). Furthermore if still used, probably
1660 * nobody pays attention to such details. */
1661 if (!light->falloff)
1663 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1664 * equations have the falloff resp. exponent parameter as an
1665 * exponent, so the spot light lighting will always be 1.0 for
1666 * both of them, and we don't have to care for the rest of the
1667 * rather complex calculation. */
1668 object->exponent = 0.0f;
1670 else
1672 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1673 if (rho < 0.0001f)
1674 rho = 0.0001f;
1675 object->exponent = -0.3f / logf(cosf(rho / 2));
1678 if (object->exponent > 128.0f)
1679 object->exponent = 128.0f;
1681 object->cutoff = (float)(light->phi * 90 / M_PI);
1682 /* FIXME: Range */
1683 break;
1685 case WINED3D_LIGHT_PARALLELPOINT:
1686 object->position.x = light->position.x;
1687 object->position.y = light->position.y;
1688 object->position.z = light->position.z;
1689 object->position.w = 1.0f;
1690 break;
1692 default:
1693 FIXME("Unrecognized light type %#x.\n", light->type);
1696 if (!device->recording)
1697 wined3d_cs_emit_set_light(device->cs, object);
1699 return WINED3D_OK;
1702 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1703 UINT light_idx, struct wined3d_light *light)
1705 struct wined3d_light_info *light_info;
1707 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1709 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1711 TRACE("Light information requested but light not defined\n");
1712 return WINED3DERR_INVALIDCALL;
1715 *light = light_info->OriginalParms;
1716 return WINED3D_OK;
1719 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1721 struct wined3d_light_info *light_info;
1723 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1725 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1726 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1728 TRACE("Light enabled requested but light not defined, so defining one!\n");
1729 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1731 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1733 FIXME("Adding default lights has failed dismally\n");
1734 return WINED3DERR_INVALIDCALL;
1738 wined3d_state_enable_light(device->update_state, &device->adapter->d3d_info, light_info, enable);
1739 if (!device->recording)
1740 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1742 return WINED3D_OK;
1745 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1747 struct wined3d_light_info *light_info;
1749 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1751 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1753 TRACE("Light enabled state requested but light not defined.\n");
1754 return WINED3DERR_INVALIDCALL;
1756 /* true is 128 according to SetLightEnable */
1757 *enable = light_info->enabled ? 128 : 0;
1758 return WINED3D_OK;
1761 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1762 UINT plane_idx, const struct wined3d_vec4 *plane)
1764 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1766 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1768 TRACE("Application has requested clipplane this device doesn't support.\n");
1769 return WINED3DERR_INVALIDCALL;
1772 if (device->recording)
1773 device->recording->changed.clipplane |= 1u << plane_idx;
1775 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1777 TRACE("Application is setting old values over, nothing to do.\n");
1778 return WINED3D_OK;
1781 device->update_state->clip_planes[plane_idx] = *plane;
1783 if (!device->recording)
1784 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1786 return WINED3D_OK;
1789 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1790 UINT plane_idx, struct wined3d_vec4 *plane)
1792 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1794 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1796 TRACE("Application has requested clipplane this device doesn't support.\n");
1797 return WINED3DERR_INVALIDCALL;
1800 *plane = device->state.clip_planes[plane_idx];
1802 return WINED3D_OK;
1805 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1806 const struct wined3d_clip_status *clip_status)
1808 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1810 if (!clip_status)
1811 return WINED3DERR_INVALIDCALL;
1813 return WINED3D_OK;
1816 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1817 struct wined3d_clip_status *clip_status)
1819 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1821 if (!clip_status)
1822 return WINED3DERR_INVALIDCALL;
1824 return WINED3D_OK;
1827 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1829 TRACE("device %p, material %p.\n", device, material);
1831 device->update_state->material = *material;
1833 if (device->recording)
1834 device->recording->changed.material = TRUE;
1835 else
1836 wined3d_cs_emit_set_material(device->cs, material);
1839 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1841 TRACE("device %p, material %p.\n", device, material);
1843 *material = device->state.material;
1845 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1846 TRACE("ambient %s\n", debug_color(&material->ambient));
1847 TRACE("specular %s\n", debug_color(&material->specular));
1848 TRACE("emissive %s\n", debug_color(&material->emissive));
1849 TRACE("power %.8e.\n", material->power);
1852 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1853 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1855 enum wined3d_format_id prev_format;
1856 struct wined3d_buffer *prev_buffer;
1857 unsigned int prev_offset;
1859 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1860 device, buffer, debug_d3dformat(format_id), offset);
1862 prev_buffer = device->update_state->index_buffer;
1863 prev_format = device->update_state->index_format;
1864 prev_offset = device->update_state->index_offset;
1866 device->update_state->index_buffer = buffer;
1867 device->update_state->index_format = format_id;
1868 device->update_state->index_offset = offset;
1870 if (device->recording)
1871 device->recording->changed.indices = TRUE;
1873 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1874 return;
1876 if (buffer)
1877 wined3d_buffer_incref(buffer);
1878 if (!device->recording)
1879 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1880 if (prev_buffer)
1881 wined3d_buffer_decref(prev_buffer);
1884 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1885 enum wined3d_format_id *format, unsigned int *offset)
1887 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1889 *format = device->state.index_format;
1890 if (offset)
1891 *offset = device->state.index_offset;
1892 return device->state.index_buffer;
1895 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1897 TRACE("device %p, base_index %d.\n", device, base_index);
1899 device->update_state->base_vertex_index = base_index;
1902 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1904 TRACE("device %p.\n", device);
1906 return device->state.base_vertex_index;
1909 void CDECL wined3d_device_set_viewports(struct wined3d_device *device, unsigned int viewport_count,
1910 const struct wined3d_viewport *viewports)
1912 unsigned int i;
1914 TRACE("device %p, viewport_count %u, viewports %p.\n", device, viewport_count, viewports);
1916 for (i = 0; i < viewport_count; ++i)
1918 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1919 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1922 if (viewport_count)
1923 memcpy(device->update_state->viewports, viewports, viewport_count * sizeof(*viewports));
1924 else
1925 memset(device->update_state->viewports, 0, sizeof(device->update_state->viewports));
1926 device->update_state->viewport_count = viewport_count;
1928 /* Handle recording of state blocks */
1929 if (device->recording)
1931 TRACE("Recording... not performing anything\n");
1932 device->recording->changed.viewport = TRUE;
1933 return;
1936 wined3d_cs_emit_set_viewports(device->cs, viewport_count, viewports);
1939 void CDECL wined3d_device_get_viewports(const struct wined3d_device *device, unsigned int *viewport_count,
1940 struct wined3d_viewport *viewports)
1942 unsigned int count;
1944 TRACE("device %p, viewport_count %p, viewports %p.\n", device, viewport_count, viewports);
1946 count = viewport_count ? min(*viewport_count, device->state.viewport_count) : 1;
1947 if (count && viewports)
1948 memcpy(viewports, device->state.viewports, count * sizeof(*viewports));
1949 if (viewport_count)
1950 *viewport_count = device->state.viewport_count;
1953 static void resolve_depth_buffer(struct wined3d_device *device)
1955 const struct wined3d_state *state = &device->state;
1956 struct wined3d_rendertarget_view *src_view;
1957 struct wined3d_resource *dst_resource;
1958 struct wined3d_texture *dst_texture;
1960 if (!(dst_texture = state->textures[0]))
1961 return;
1962 dst_resource = &dst_texture->resource;
1963 if (!(dst_resource->format_flags & WINED3DFMT_FLAG_DEPTH))
1964 return;
1965 if (!(src_view = state->fb->depth_stencil))
1966 return;
1968 wined3d_device_resolve_sub_resource(device, dst_resource, 0,
1969 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1972 void CDECL wined3d_device_set_blend_state(struct wined3d_device *device,
1973 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor)
1975 struct wined3d_state *state = device->update_state;
1976 struct wined3d_blend_state *prev;
1978 TRACE("device %p, blend_state %p, blend_factor %s.\n", device, blend_state, debug_color(blend_factor));
1980 if (device->recording)
1981 device->recording->changed.blend_state = TRUE;
1983 prev = state->blend_state;
1984 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor)))
1985 return;
1987 if (blend_state)
1988 wined3d_blend_state_incref(blend_state);
1989 state->blend_state = blend_state;
1990 state->blend_factor = *blend_factor;
1991 if (!device->recording)
1992 wined3d_cs_emit_set_blend_state(device->cs, blend_state, blend_factor);
1993 if (prev)
1994 wined3d_blend_state_decref(prev);
1997 struct wined3d_blend_state * CDECL wined3d_device_get_blend_state(const struct wined3d_device *device,
1998 struct wined3d_color *blend_factor)
2000 const struct wined3d_state *state = &device->state;
2002 TRACE("device %p, blend_factor %p.\n", device, blend_factor);
2004 *blend_factor = state->blend_factor;
2005 return state->blend_state;
2008 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
2009 struct wined3d_rasterizer_state *rasterizer_state)
2011 struct wined3d_rasterizer_state *prev;
2013 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2015 prev = device->update_state->rasterizer_state;
2016 if (prev == rasterizer_state)
2017 return;
2019 if (rasterizer_state)
2020 wined3d_rasterizer_state_incref(rasterizer_state);
2021 device->update_state->rasterizer_state = rasterizer_state;
2022 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2023 if (prev)
2024 wined3d_rasterizer_state_decref(prev);
2027 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2029 TRACE("device %p.\n", device);
2031 return device->state.rasterizer_state;
2034 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2035 enum wined3d_render_state state, DWORD value)
2037 DWORD old_value;
2039 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2041 if (state > WINEHIGHEST_RENDER_STATE)
2043 WARN("Unhandled render state %#x.\n", state);
2044 return;
2047 old_value = device->state.render_states[state];
2048 device->update_state->render_states[state] = value;
2050 /* Handle recording of state blocks. */
2051 if (device->recording)
2053 TRACE("Recording... not performing anything.\n");
2054 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2055 return;
2058 /* Compared here and not before the assignment to allow proper stateblock recording. */
2059 if (value == old_value)
2060 TRACE("Application is setting the old value over, nothing to do.\n");
2061 else
2062 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 DWORD old_value;
2083 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2084 device, sampler_idx, debug_d3dsamplerstate(state), value);
2086 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2087 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2089 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2091 WARN("Invalid sampler %u.\n", sampler_idx);
2092 return; /* Windows accepts overflowing this array ... we do not. */
2095 old_value = device->state.sampler_states[sampler_idx][state];
2096 device->update_state->sampler_states[sampler_idx][state] = value;
2098 /* Handle recording of state blocks. */
2099 if (device->recording)
2101 TRACE("Recording... not performing anything.\n");
2102 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2103 return;
2106 if (old_value == value)
2108 TRACE("Application is setting the old value over, nothing to do.\n");
2109 return;
2112 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2115 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2116 UINT sampler_idx, enum wined3d_sampler_state state)
2118 TRACE("device %p, sampler_idx %u, state %s.\n",
2119 device, sampler_idx, debug_d3dsamplerstate(state));
2121 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2122 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2124 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2126 WARN("Invalid sampler %u.\n", sampler_idx);
2127 return 0; /* Windows accepts overflowing this array ... we do not. */
2130 return device->state.sampler_states[sampler_idx][state];
2133 void CDECL wined3d_device_set_scissor_rects(struct wined3d_device *device, unsigned int rect_count,
2134 const RECT *rects)
2136 unsigned int i;
2138 TRACE("device %p, rect_count %u, rects %p.\n", device, rect_count, rects);
2140 for (i = 0; i < rect_count; ++i)
2142 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2145 if (device->recording)
2146 device->recording->changed.scissorRect = TRUE;
2148 if (device->update_state->scissor_rect_count == rect_count
2149 && !memcmp(device->update_state->scissor_rects, rects, rect_count * sizeof(*rects)))
2151 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2152 return;
2155 if (rect_count)
2156 memcpy(device->update_state->scissor_rects, rects, rect_count * sizeof(*rects));
2157 else
2158 memset(device->update_state->scissor_rects, 0, sizeof(device->update_state->scissor_rects));
2159 device->update_state->scissor_rect_count = rect_count;
2161 if (device->recording)
2163 TRACE("Recording... not performing anything.\n");
2164 return;
2167 wined3d_cs_emit_set_scissor_rects(device->cs, rect_count, rects);
2170 void CDECL wined3d_device_get_scissor_rects(const struct wined3d_device *device, unsigned int *rect_count, RECT *rects)
2172 unsigned int count;
2174 TRACE("device %p, rect_count %p, rects %p.\n", device, rect_count, rects);
2176 count = rect_count ? min(*rect_count, device->state.scissor_rect_count) : 1;
2177 if (count && rects)
2178 memcpy(rects, device->state.scissor_rects, count * sizeof(*rects));
2179 if (rect_count)
2180 *rect_count = device->state.scissor_rect_count;
2183 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2184 struct wined3d_vertex_declaration *declaration)
2186 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2188 TRACE("device %p, declaration %p.\n", device, declaration);
2190 if (device->recording)
2191 device->recording->changed.vertexDecl = TRUE;
2193 if (declaration == prev)
2194 return;
2196 if (declaration)
2197 wined3d_vertex_declaration_incref(declaration);
2198 device->update_state->vertex_declaration = declaration;
2199 if (!device->recording)
2200 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2201 if (prev)
2202 wined3d_vertex_declaration_decref(prev);
2205 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2207 TRACE("device %p.\n", device);
2209 return device->state.vertex_declaration;
2212 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2214 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2216 TRACE("device %p, shader %p.\n", device, shader);
2218 if (device->recording)
2219 device->recording->changed.vertexShader = TRUE;
2221 if (shader == prev)
2222 return;
2224 if (shader)
2225 wined3d_shader_incref(shader);
2226 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2227 if (!device->recording)
2228 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2229 if (prev)
2230 wined3d_shader_decref(prev);
2233 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2235 TRACE("device %p.\n", device);
2237 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2240 void CDECL wined3d_device_set_constant_buffer(struct wined3d_device *device,
2241 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2243 struct wined3d_buffer *prev;
2245 TRACE("device %p, type %#x, idx %u, buffer %p.\n", device, type, idx, buffer);
2247 if (idx >= MAX_CONSTANT_BUFFERS)
2249 WARN("Invalid constant buffer index %u.\n", idx);
2250 return;
2253 prev = device->update_state->cb[type][idx];
2254 if (buffer == prev)
2255 return;
2257 if (buffer)
2258 wined3d_buffer_incref(buffer);
2259 device->update_state->cb[type][idx] = buffer;
2260 if (!device->recording)
2261 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2262 if (prev)
2263 wined3d_buffer_decref(prev);
2266 struct wined3d_buffer * CDECL wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2267 enum wined3d_shader_type shader_type, unsigned int idx)
2269 TRACE("device %p, shader_type %#x, idx %u.\n", device, shader_type, idx);
2271 if (idx >= MAX_CONSTANT_BUFFERS)
2273 WARN("Invalid constant buffer index %u.\n", idx);
2274 return NULL;
2277 return device->state.cb[shader_type][idx];
2280 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2281 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2283 struct wined3d_shader_resource_view *prev;
2285 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2287 WARN("Invalid view index %u.\n", idx);
2288 return;
2291 prev = device->update_state->shader_resource_view[type][idx];
2292 if (view == prev)
2293 return;
2295 if (view)
2296 wined3d_shader_resource_view_incref(view);
2297 device->update_state->shader_resource_view[type][idx] = view;
2298 if (!device->recording)
2299 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2300 if (prev)
2301 wined3d_shader_resource_view_decref(prev);
2304 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2305 UINT idx, struct wined3d_shader_resource_view *view)
2307 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2309 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2312 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2313 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2315 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2317 WARN("Invalid view index %u.\n", idx);
2318 return NULL;
2321 return device->state.shader_resource_view[shader_type][idx];
2324 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2325 UINT idx)
2327 TRACE("device %p, idx %u.\n", device, idx);
2329 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2332 static void wined3d_device_set_sampler(struct wined3d_device *device,
2333 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2335 struct wined3d_sampler *prev;
2337 if (idx >= MAX_SAMPLER_OBJECTS)
2339 WARN("Invalid sampler index %u.\n", idx);
2340 return;
2343 prev = device->update_state->sampler[type][idx];
2344 if (sampler == prev)
2345 return;
2347 if (sampler)
2348 wined3d_sampler_incref(sampler);
2349 device->update_state->sampler[type][idx] = sampler;
2350 if (!device->recording)
2351 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2352 if (prev)
2353 wined3d_sampler_decref(prev);
2356 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2358 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2360 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2363 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2364 enum wined3d_shader_type shader_type, unsigned int idx)
2366 if (idx >= MAX_SAMPLER_OBJECTS)
2368 WARN("Invalid sampler index %u.\n", idx);
2369 return NULL;
2372 return device->state.sampler[shader_type][idx];
2375 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2377 TRACE("device %p, idx %u.\n", device, idx);
2379 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2382 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2383 unsigned int start_idx, unsigned int count, const BOOL *constants)
2385 unsigned int i;
2387 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2388 device, start_idx, count, constants);
2390 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2391 return WINED3DERR_INVALIDCALL;
2393 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2394 count = WINED3D_MAX_CONSTS_B - start_idx;
2395 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2396 if (TRACE_ON(d3d))
2398 for (i = 0; i < count; ++i)
2399 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2402 if (device->recording)
2404 for (i = start_idx; i < count + start_idx; ++i)
2405 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2407 else
2409 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2412 return WINED3D_OK;
2415 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2416 unsigned int start_idx, unsigned int count, BOOL *constants)
2418 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2419 device, start_idx, count, constants);
2421 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2422 return WINED3DERR_INVALIDCALL;
2424 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2425 count = WINED3D_MAX_CONSTS_B - start_idx;
2426 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2428 return WINED3D_OK;
2431 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2432 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2434 unsigned int i;
2436 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2437 device, start_idx, count, constants);
2439 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2440 return WINED3DERR_INVALIDCALL;
2442 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2443 count = WINED3D_MAX_CONSTS_I - start_idx;
2444 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2445 if (TRACE_ON(d3d))
2447 for (i = 0; i < count; ++i)
2448 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2451 if (device->recording)
2453 for (i = start_idx; i < count + start_idx; ++i)
2454 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2456 else
2458 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2461 return WINED3D_OK;
2464 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2465 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2467 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2468 device, start_idx, count, constants);
2470 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2471 return WINED3DERR_INVALIDCALL;
2473 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2474 count = WINED3D_MAX_CONSTS_I - start_idx;
2475 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2476 return WINED3D_OK;
2479 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2480 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2482 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2483 unsigned int i;
2485 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2486 device, start_idx, count, constants);
2488 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2489 || count > d3d_info->limits.vs_uniform_count - start_idx)
2490 return WINED3DERR_INVALIDCALL;
2492 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2493 if (TRACE_ON(d3d))
2495 for (i = 0; i < count; ++i)
2496 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2499 if (device->recording)
2500 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2501 count * sizeof(*device->recording->changed.vs_consts_f));
2502 else
2503 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2505 return WINED3D_OK;
2508 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2509 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2511 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2513 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2514 device, start_idx, count, constants);
2516 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2517 || count > d3d_info->limits.vs_uniform_count - start_idx)
2518 return WINED3DERR_INVALIDCALL;
2520 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2522 return WINED3D_OK;
2525 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2527 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2529 TRACE("device %p, shader %p.\n", device, shader);
2531 if (device->recording)
2532 device->recording->changed.pixelShader = TRUE;
2534 if (shader == prev)
2535 return;
2537 if (shader)
2538 wined3d_shader_incref(shader);
2539 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2540 if (!device->recording)
2541 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2542 if (prev)
2543 wined3d_shader_decref(prev);
2546 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2548 TRACE("device %p.\n", device);
2550 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2553 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2554 UINT idx, struct wined3d_shader_resource_view *view)
2556 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2558 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2561 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2562 UINT idx)
2564 TRACE("device %p, idx %u.\n", device, idx);
2566 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2569 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2571 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2573 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2576 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2578 TRACE("device %p, idx %u.\n", device, idx);
2580 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2583 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2584 unsigned int start_idx, unsigned int count, const BOOL *constants)
2586 unsigned int i;
2588 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2589 device, start_idx, count, constants);
2591 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2592 return WINED3DERR_INVALIDCALL;
2594 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2595 count = WINED3D_MAX_CONSTS_B - start_idx;
2596 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2597 if (TRACE_ON(d3d))
2599 for (i = 0; i < count; ++i)
2600 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2603 if (device->recording)
2605 for (i = start_idx; i < count + start_idx; ++i)
2606 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2608 else
2610 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2613 return WINED3D_OK;
2616 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2617 unsigned int start_idx, unsigned int count, BOOL *constants)
2619 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2620 device, start_idx, count, constants);
2622 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2623 return WINED3DERR_INVALIDCALL;
2625 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2626 count = WINED3D_MAX_CONSTS_B - start_idx;
2627 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2629 return WINED3D_OK;
2632 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2633 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2635 unsigned int i;
2637 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2638 device, start_idx, count, constants);
2640 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2641 return WINED3DERR_INVALIDCALL;
2643 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2644 count = WINED3D_MAX_CONSTS_I - start_idx;
2645 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2646 if (TRACE_ON(d3d))
2648 for (i = 0; i < count; ++i)
2649 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2652 if (device->recording)
2654 for (i = start_idx; i < count + start_idx; ++i)
2655 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2657 else
2659 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2662 return WINED3D_OK;
2665 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2666 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2668 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2669 device, start_idx, count, constants);
2671 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2672 return WINED3DERR_INVALIDCALL;
2674 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2675 count = WINED3D_MAX_CONSTS_I - start_idx;
2676 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2678 return WINED3D_OK;
2681 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2682 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2684 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2685 unsigned int i;
2687 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2688 device, start_idx, count, constants);
2690 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2691 || count > d3d_info->limits.ps_uniform_count - start_idx)
2692 return WINED3DERR_INVALIDCALL;
2694 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2695 if (TRACE_ON(d3d))
2697 for (i = 0; i < count; ++i)
2698 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2701 if (device->recording)
2702 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2703 count * sizeof(*device->recording->changed.ps_consts_f));
2704 else
2705 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2707 return WINED3D_OK;
2710 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2711 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2713 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2715 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2716 device, start_idx, count, constants);
2718 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2719 || count > d3d_info->limits.ps_uniform_count - start_idx)
2720 return WINED3DERR_INVALIDCALL;
2722 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2724 return WINED3D_OK;
2727 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2729 struct wined3d_shader *prev;
2731 TRACE("device %p, shader %p.\n", device, shader);
2733 prev = device->update_state->shader[WINED3D_SHADER_TYPE_HULL];
2734 if (shader == prev)
2735 return;
2736 if (shader)
2737 wined3d_shader_incref(shader);
2738 device->update_state->shader[WINED3D_SHADER_TYPE_HULL] = shader;
2739 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_HULL, shader);
2740 if (prev)
2741 wined3d_shader_decref(prev);
2744 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2746 TRACE("device %p.\n", device);
2748 return device->state.shader[WINED3D_SHADER_TYPE_HULL];
2751 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2752 unsigned int idx, struct wined3d_shader_resource_view *view)
2754 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2756 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx, view);
2759 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2760 unsigned int idx)
2762 TRACE("device %p, idx %u.\n", device, idx);
2764 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2767 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2768 unsigned int idx, struct wined3d_sampler *sampler)
2770 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2772 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2775 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2777 TRACE("device %p, idx %u.\n", device, idx);
2779 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2782 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2784 struct wined3d_shader *prev;
2786 TRACE("device %p, shader %p.\n", device, shader);
2788 prev = device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2789 if (shader == prev)
2790 return;
2791 if (shader)
2792 wined3d_shader_incref(shader);
2793 device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN] = shader;
2794 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_DOMAIN, shader);
2795 if (prev)
2796 wined3d_shader_decref(prev);
2799 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2801 TRACE("device %p.\n", device);
2803 return device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2806 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2807 unsigned int idx, struct wined3d_shader_resource_view *view)
2809 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2811 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2814 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2815 unsigned int idx)
2817 TRACE("device %p, idx %u.\n", device, idx);
2819 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2822 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2823 unsigned int idx, struct wined3d_sampler *sampler)
2825 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2827 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2830 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2832 TRACE("device %p, idx %u.\n", device, idx);
2834 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2837 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2839 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2841 TRACE("device %p, shader %p.\n", device, shader);
2843 if (device->recording || shader == prev)
2844 return;
2845 if (shader)
2846 wined3d_shader_incref(shader);
2847 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2848 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2849 if (prev)
2850 wined3d_shader_decref(prev);
2853 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2855 TRACE("device %p.\n", device);
2857 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2860 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2861 UINT idx, struct wined3d_shader_resource_view *view)
2863 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2865 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2868 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2869 UINT idx)
2871 TRACE("device %p, idx %u.\n", device, idx);
2873 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2876 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2878 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2880 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2883 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2885 TRACE("device %p, idx %u.\n", device, idx);
2887 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2890 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2892 struct wined3d_shader *prev;
2894 TRACE("device %p, shader %p.\n", device, shader);
2896 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2897 if (device->recording || shader == prev)
2898 return;
2899 if (shader)
2900 wined3d_shader_incref(shader);
2901 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2902 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2903 if (prev)
2904 wined3d_shader_decref(prev);
2907 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2909 TRACE("device %p.\n", device);
2911 return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2914 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2915 unsigned int idx, struct wined3d_shader_resource_view *view)
2917 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2919 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2922 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2923 unsigned int idx)
2925 TRACE("device %p, idx %u.\n", device, idx);
2927 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2930 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2931 unsigned int idx, struct wined3d_sampler *sampler)
2933 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2935 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2938 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2940 TRACE("device %p, idx %u.\n", device, idx);
2942 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2945 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2946 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2947 unsigned int initial_count)
2949 struct wined3d_unordered_access_view *prev;
2951 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2953 WARN("Invalid UAV index %u.\n", idx);
2954 return;
2957 prev = device->update_state->unordered_access_view[pipeline][idx];
2958 if (uav == prev && initial_count == ~0u)
2959 return;
2961 if (uav)
2962 wined3d_unordered_access_view_incref(uav);
2963 device->update_state->unordered_access_view[pipeline][idx] = uav;
2964 if (!device->recording)
2965 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav, initial_count);
2966 if (prev)
2967 wined3d_unordered_access_view_decref(prev);
2970 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2971 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2973 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2975 WARN("Invalid UAV index %u.\n", idx);
2976 return NULL;
2979 return device->state.unordered_access_view[pipeline][idx];
2982 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2983 struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2985 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2987 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
2990 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
2991 unsigned int idx)
2993 TRACE("device %p, idx %u.\n", device, idx);
2995 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
2998 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2999 unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
3001 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
3003 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
3006 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
3007 const struct wined3d_device *device, unsigned int idx)
3009 TRACE("device %p, idx %u.\n", device, idx);
3011 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
3014 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
3016 unsigned int i;
3018 if (!latency)
3019 latency = 3;
3021 device->max_frame_latency = latency;
3022 for (i = 0; i < device->swapchain_count; ++i)
3023 swapchain_set_max_frame_latency(device->swapchains[i], device);
3026 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
3028 return device->max_frame_latency;
3031 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
3033 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3034 unsigned int i, size = 0;
3036 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
3037 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
3038 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
3039 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
3040 switch (fvf & WINED3DFVF_POSITION_MASK)
3042 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
3043 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
3044 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
3045 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
3046 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
3047 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
3048 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
3049 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
3050 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
3052 for (i = 0; i < texcoord_count; ++i)
3054 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
3057 return size;
3060 /* Context activation is done by the caller. */
3061 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3062 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3063 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3065 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3066 struct wined3d_map_desc map_desc;
3067 struct wined3d_box box = {0};
3068 struct wined3d_viewport vp;
3069 unsigned int vertex_size;
3070 unsigned int i;
3071 BYTE *dest_ptr;
3072 BOOL doClip;
3073 DWORD numTextures;
3074 HRESULT hr;
3076 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3078 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3081 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3083 ERR("Source has no position mask\n");
3084 return WINED3DERR_INVALIDCALL;
3087 if (device->state.render_states[WINED3D_RS_CLIPPING])
3089 static BOOL warned = FALSE;
3091 * The clipping code is not quite correct. Some things need
3092 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3093 * so disable clipping for now.
3094 * (The graphics in Half-Life are broken, and my processvertices
3095 * test crashes with IDirect3DDevice3)
3096 doClip = TRUE;
3098 doClip = FALSE;
3099 if(!warned) {
3100 warned = TRUE;
3101 FIXME("Clipping is broken and disabled for now\n");
3104 else
3105 doClip = FALSE;
3107 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3108 box.left = dwDestIndex * vertex_size;
3109 box.right = box.left + dwCount * vertex_size;
3110 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3112 WARN("Failed to map buffer, hr %#x.\n", hr);
3113 return hr;
3115 dest_ptr = map_desc.data;
3117 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3118 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3119 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3121 TRACE("View mat:\n");
3122 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3123 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3124 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3125 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3127 TRACE("Proj mat:\n");
3128 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3129 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3130 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3131 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3133 TRACE("World mat:\n");
3134 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3135 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3136 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3137 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3139 /* Get the viewport */
3140 wined3d_device_get_viewports(device, NULL, &vp);
3141 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3142 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3144 multiply_matrix(&mat,&view_mat,&world_mat);
3145 multiply_matrix(&mat,&proj_mat,&mat);
3147 numTextures = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3149 for (i = 0; i < dwCount; i+= 1) {
3150 unsigned int tex_index;
3152 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3153 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3154 /* The position first */
3155 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3156 const float *p = (const float *)(element->data.addr + i * element->stride);
3157 float x, y, z, rhw;
3158 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3160 /* Multiplication with world, view and projection matrix. */
3161 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3162 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3163 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3164 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3166 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3168 /* WARNING: The following things are taken from d3d7 and were not yet checked
3169 * against d3d8 or d3d9!
3172 /* Clipping conditions: From msdn
3174 * A vertex is clipped if it does not match the following requirements
3175 * -rhw < x <= rhw
3176 * -rhw < y <= rhw
3177 * 0 < z <= rhw
3178 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3180 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3181 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3185 if( !doClip ||
3186 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3187 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3188 ( rhw > eps ) ) ) {
3190 /* "Normal" viewport transformation (not clipped)
3191 * 1) The values are divided by rhw
3192 * 2) The y axis is negative, so multiply it with -1
3193 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3194 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3195 * 4) Multiply x with Width/2 and add Width/2
3196 * 5) The same for the height
3197 * 6) Add the viewpoint X and Y to the 2D coordinates and
3198 * The minimum Z value to z
3199 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3201 * Well, basically it's simply a linear transformation into viewport
3202 * coordinates
3205 x /= rhw;
3206 y /= rhw;
3207 z /= rhw;
3209 y *= -1;
3211 x *= vp.width / 2;
3212 y *= vp.height / 2;
3213 z *= vp.max_z - vp.min_z;
3215 x += vp.width / 2 + vp.x;
3216 y += vp.height / 2 + vp.y;
3217 z += vp.min_z;
3219 rhw = 1 / rhw;
3220 } else {
3221 /* That vertex got clipped
3222 * Contrary to OpenGL it is not dropped completely, it just
3223 * undergoes a different calculation.
3225 TRACE("Vertex got clipped\n");
3226 x += rhw;
3227 y += rhw;
3229 x /= 2;
3230 y /= 2;
3232 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3233 * outside of the main vertex buffer memory. That needs some more
3234 * investigation...
3238 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3241 ( (float *) dest_ptr)[0] = x;
3242 ( (float *) dest_ptr)[1] = y;
3243 ( (float *) dest_ptr)[2] = z;
3244 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3246 dest_ptr += 3 * sizeof(float);
3248 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3249 dest_ptr += sizeof(float);
3252 if (dst_fvf & WINED3DFVF_PSIZE)
3253 dest_ptr += sizeof(DWORD);
3255 if (dst_fvf & WINED3DFVF_NORMAL)
3257 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3258 const float *normal = (const float *)(element->data.addr + i * element->stride);
3259 /* AFAIK this should go into the lighting information */
3260 FIXME("Didn't expect the destination to have a normal\n");
3261 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3264 if (dst_fvf & WINED3DFVF_DIFFUSE)
3266 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3267 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3268 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3270 static BOOL warned = FALSE;
3272 if(!warned) {
3273 ERR("No diffuse color in source, but destination has one\n");
3274 warned = TRUE;
3277 *( (DWORD *) dest_ptr) = 0xffffffff;
3278 dest_ptr += sizeof(DWORD);
3280 else
3282 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3286 if (dst_fvf & WINED3DFVF_SPECULAR)
3288 /* What's the color value in the feedback buffer? */
3289 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3290 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3291 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3293 static BOOL warned = FALSE;
3295 if(!warned) {
3296 ERR("No specular color in source, but destination has one\n");
3297 warned = TRUE;
3300 *(DWORD *)dest_ptr = 0xff000000;
3301 dest_ptr += sizeof(DWORD);
3303 else
3305 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3309 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3311 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3312 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3313 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3315 ERR("No source texture, but destination requests one\n");
3316 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3318 else
3320 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3325 wined3d_resource_unmap(&dest->resource, 0);
3327 return WINED3D_OK;
3329 #undef copy_and_next
3331 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3332 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3333 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3335 struct wined3d_state *state = &device->state;
3336 struct wined3d_stream_info stream_info;
3337 struct wined3d_resource *resource;
3338 struct wined3d_box box = {0};
3339 struct wined3d_shader *vs;
3340 unsigned int i;
3341 HRESULT hr;
3342 WORD map;
3344 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3345 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3346 device, src_start_idx, dst_idx, vertex_count,
3347 dst_buffer, declaration, flags, dst_fvf);
3349 if (declaration)
3350 FIXME("Output vertex declaration not implemented yet.\n");
3352 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3353 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3354 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->gl_info, &device->adapter->d3d_info);
3355 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3357 /* We can't convert FROM a VBO, and vertex buffers used to source into
3358 * process_vertices() are unlikely to ever be used for drawing. Release
3359 * VBOs in those buffers and fix up the stream_info structure.
3361 * Also apply the start index. */
3362 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3364 struct wined3d_stream_info_element *e;
3365 struct wined3d_map_desc map_desc;
3367 if (!(map & 1))
3368 continue;
3370 e = &stream_info.elements[i];
3371 resource = &state->streams[e->stream_idx].buffer->resource;
3372 box.left = src_start_idx * e->stride;
3373 box.right = box.left + vertex_count * e->stride;
3374 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3375 ERR("Failed to map resource.\n");
3376 e->data.buffer_object = 0;
3377 e->data.addr += (ULONG_PTR)map_desc.data;
3380 hr = process_vertices_strided(device, dst_idx, vertex_count,
3381 &stream_info, dst_buffer, flags, dst_fvf);
3383 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3385 if (!(map & 1))
3386 continue;
3388 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3389 if (FAILED(wined3d_resource_unmap(resource, 0)))
3390 ERR("Failed to unmap resource.\n");
3393 return hr;
3396 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3397 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3399 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3400 DWORD old_value;
3402 TRACE("device %p, stage %u, state %s, value %#x.\n",
3403 device, stage, debug_d3dtexturestate(state), value);
3405 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3407 WARN("Invalid state %#x passed.\n", state);
3408 return;
3411 if (stage >= d3d_info->limits.ffp_blend_stages)
3413 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3414 stage, d3d_info->limits.ffp_blend_stages - 1);
3415 return;
3418 old_value = device->update_state->texture_states[stage][state];
3419 device->update_state->texture_states[stage][state] = value;
3421 if (device->recording)
3423 TRACE("Recording... not performing anything.\n");
3424 device->recording->changed.textureState[stage] |= 1u << state;
3425 return;
3428 /* Checked after the assignments to allow proper stateblock recording. */
3429 if (old_value == value)
3431 TRACE("Application is setting the old value over, nothing to do.\n");
3432 return;
3435 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3438 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3439 UINT stage, enum wined3d_texture_stage_state state)
3441 TRACE("device %p, stage %u, state %s.\n",
3442 device, stage, debug_d3dtexturestate(state));
3444 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3446 WARN("Invalid state %#x passed.\n", state);
3447 return 0;
3450 return device->state.texture_states[stage][state];
3453 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3454 UINT stage, struct wined3d_texture *texture)
3456 struct wined3d_texture *prev;
3458 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3460 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3461 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3463 /* Windows accepts overflowing this array... we do not. */
3464 if (stage >= ARRAY_SIZE(device->state.textures))
3466 WARN("Ignoring invalid stage %u.\n", stage);
3467 return WINED3D_OK;
3470 if (texture && texture->resource.usage & WINED3DUSAGE_SCRATCH)
3472 WARN("Rejecting attempt to set scratch texture.\n");
3473 return WINED3DERR_INVALIDCALL;
3476 if (device->recording)
3477 device->recording->changed.textures |= 1u << stage;
3479 prev = device->update_state->textures[stage];
3480 TRACE("Previous texture %p.\n", prev);
3482 if (texture == prev)
3484 TRACE("App is setting the same texture again, nothing to do.\n");
3485 return WINED3D_OK;
3488 TRACE("Setting new texture to %p.\n", texture);
3489 device->update_state->textures[stage] = texture;
3491 if (texture)
3492 wined3d_texture_incref(texture);
3493 if (!device->recording)
3494 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3495 if (prev)
3496 wined3d_texture_decref(prev);
3498 return WINED3D_OK;
3501 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3503 TRACE("device %p, stage %u.\n", device, stage);
3505 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3506 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3508 if (stage >= ARRAY_SIZE(device->state.textures))
3510 WARN("Ignoring invalid stage %u.\n", stage);
3511 return NULL; /* Windows accepts overflowing this array ... we do not. */
3514 return device->state.textures[stage];
3517 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
3519 TRACE("device %p, caps %p.\n", device, caps);
3521 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3522 device->create_parms.device_type, caps);
3525 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3526 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3528 struct wined3d_swapchain *swapchain;
3530 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3531 device, swapchain_idx, mode, rotation);
3533 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3534 return WINED3DERR_INVALIDCALL;
3536 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3539 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3541 struct wined3d_stateblock *stateblock;
3542 HRESULT hr;
3544 TRACE("device %p.\n", device);
3546 if (device->recording)
3547 return WINED3DERR_INVALIDCALL;
3549 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3550 if (FAILED(hr))
3551 return hr;
3553 device->recording = stateblock;
3554 device->update_state = &stateblock->state;
3556 TRACE("Recording stateblock %p.\n", stateblock);
3558 return WINED3D_OK;
3561 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3562 struct wined3d_stateblock **stateblock)
3564 struct wined3d_stateblock *object = device->recording;
3566 TRACE("device %p, stateblock %p.\n", device, stateblock);
3568 if (!device->recording)
3570 WARN("Not recording.\n");
3571 *stateblock = NULL;
3572 return WINED3DERR_INVALIDCALL;
3575 stateblock_init_contained_states(object);
3577 *stateblock = object;
3578 device->recording = NULL;
3579 device->update_state = &device->state;
3581 TRACE("Returning stateblock %p.\n", *stateblock);
3583 return WINED3D_OK;
3586 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3588 /* At the moment we have no need for any functionality at the beginning
3589 * of a scene. */
3590 TRACE("device %p.\n", device);
3592 if (device->inScene)
3594 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3595 return WINED3DERR_INVALIDCALL;
3597 device->inScene = TRUE;
3598 return WINED3D_OK;
3601 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3603 TRACE("device %p.\n", device);
3605 if (!device->inScene)
3607 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3608 return WINED3DERR_INVALIDCALL;
3611 device->inScene = FALSE;
3612 return WINED3D_OK;
3615 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3616 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3618 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3619 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3621 if (!rect_count && rects)
3623 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3624 return WINED3D_OK;
3627 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3629 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3630 if (!ds)
3632 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3633 /* TODO: What about depth stencil buffers without stencil bits? */
3634 return WINED3DERR_INVALIDCALL;
3636 else if (flags & WINED3DCLEAR_TARGET)
3638 if (ds->width < device->fb.render_targets[0]->width
3639 || ds->height < device->fb.render_targets[0]->height)
3641 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3642 return WINED3D_OK;
3647 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3649 return WINED3D_OK;
3652 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3653 struct wined3d_query *predicate, BOOL value)
3655 struct wined3d_query *prev;
3657 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3659 prev = device->update_state->predicate;
3660 if (predicate)
3662 FIXME("Predicated rendering not implemented.\n");
3663 wined3d_query_incref(predicate);
3665 device->update_state->predicate = predicate;
3666 device->update_state->predicate_value = value;
3667 if (!device->recording)
3668 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3669 if (prev)
3670 wined3d_query_decref(prev);
3673 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3675 TRACE("device %p, value %p.\n", device, value);
3677 if (value)
3678 *value = device->state.predicate_value;
3679 return device->state.predicate;
3682 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3683 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3685 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3686 device, group_count_x, group_count_y, group_count_z);
3688 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3691 void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
3692 struct wined3d_buffer *buffer, unsigned int offset)
3694 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3696 wined3d_cs_emit_dispatch_indirect(device->cs, buffer, offset);
3699 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3700 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
3702 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
3703 device, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
3705 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3706 device->state.gl_patch_vertices = patch_vertex_count;
3709 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3710 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
3712 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
3713 device, primitive_type, patch_vertex_count);
3715 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3716 if (patch_vertex_count)
3717 *patch_vertex_count = device->state.gl_patch_vertices;
3719 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
3722 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3724 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3726 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3727 0, start_vertex, vertex_count, 0, 0, FALSE);
3729 return WINED3D_OK;
3732 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3733 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3735 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3736 device, start_vertex, vertex_count, start_instance, instance_count);
3738 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3739 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3742 void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
3743 struct wined3d_buffer *buffer, unsigned int offset)
3745 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3747 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3748 buffer, offset, FALSE);
3751 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3753 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3755 if (!device->state.index_buffer)
3757 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3758 * without an index buffer set. (The first time at least...)
3759 * D3D8 simply dies, but I doubt it can do much harm to return
3760 * D3DERR_INVALIDCALL there as well. */
3761 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3762 return WINED3DERR_INVALIDCALL;
3765 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3766 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3768 return WINED3D_OK;
3771 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3772 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3774 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3775 device, start_idx, index_count, start_instance, instance_count);
3777 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3778 device->state.base_vertex_index, start_idx, index_count, start_instance, instance_count, TRUE);
3781 void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
3782 struct wined3d_buffer *buffer, unsigned int offset)
3784 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3786 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3787 buffer, offset, TRUE);
3790 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3791 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3793 unsigned int src_size, dst_size, src_skip_levels = 0;
3794 unsigned int src_level_count, dst_level_count;
3795 unsigned int layer_count, level_count, i, j;
3796 unsigned int width, height, depth;
3797 enum wined3d_resource_type type;
3798 struct wined3d_box box;
3800 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3802 /* Verify that the source and destination textures are non-NULL. */
3803 if (!src_texture || !dst_texture)
3805 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3806 return WINED3DERR_INVALIDCALL;
3809 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
3810 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
3812 WARN("Source resource is GPU accessible or a scratch resource.\n");
3813 return WINED3DERR_INVALIDCALL;
3815 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
3817 WARN("Destination resource is CPU accessible.\n");
3818 return WINED3DERR_INVALIDCALL;
3821 /* Verify that the source and destination textures are the same type. */
3822 type = src_texture->resource.type;
3823 if (dst_texture->resource.type != type)
3825 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3826 return WINED3DERR_INVALIDCALL;
3829 layer_count = src_texture->layer_count;
3830 if (layer_count != dst_texture->layer_count)
3832 WARN("Source and destination have different layer counts.\n");
3833 return WINED3DERR_INVALIDCALL;
3836 if (src_texture->resource.format != dst_texture->resource.format)
3838 WARN("Source and destination formats do not match.\n");
3839 return WINED3DERR_INVALIDCALL;
3842 src_level_count = src_texture->level_count;
3843 dst_level_count = dst_texture->level_count;
3844 level_count = min(src_level_count, dst_level_count);
3846 src_size = max(src_texture->resource.width, src_texture->resource.height);
3847 src_size = max(src_size, src_texture->resource.depth);
3848 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3849 dst_size = max(dst_size, dst_texture->resource.depth);
3850 while (src_size > dst_size)
3852 src_size >>= 1;
3853 ++src_skip_levels;
3856 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3857 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3858 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3860 WARN("Source and destination dimensions do not match.\n");
3861 return WINED3DERR_INVALIDCALL;
3864 /* Update every surface level of the texture. */
3865 for (i = 0; i < level_count; ++i)
3867 width = wined3d_texture_get_level_width(dst_texture, i);
3868 height = wined3d_texture_get_level_height(dst_texture, i);
3869 depth = wined3d_texture_get_level_depth(dst_texture, i);
3870 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3872 for (j = 0; j < layer_count; ++j)
3874 wined3d_cs_emit_blt_sub_resource(device->cs,
3875 &dst_texture->resource, j * dst_level_count + i, &box,
3876 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3877 0, NULL, WINED3D_TEXF_POINT);
3881 return WINED3D_OK;
3884 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3886 const struct wined3d_state *state = &device->state;
3887 struct wined3d_texture *texture;
3888 DWORD i;
3890 TRACE("device %p, num_passes %p.\n", device, num_passes);
3892 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3894 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3896 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3897 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3899 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3901 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3902 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3905 texture = state->textures[i];
3906 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3908 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3910 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3911 return E_FAIL;
3913 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3915 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3916 return E_FAIL;
3918 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3919 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3921 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3922 return E_FAIL;
3926 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3927 || state->render_states[WINED3D_RS_STENCILENABLE])
3929 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3930 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3932 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3934 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3935 return WINED3DERR_CONFLICTINGRENDERSTATE;
3939 /* return a sensible default */
3940 *num_passes = 1;
3942 TRACE("returning D3D_OK\n");
3943 return WINED3D_OK;
3946 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3948 static BOOL warned;
3950 TRACE("device %p, software %#x.\n", device, software);
3952 if (!warned)
3954 FIXME("device %p, software %#x stub!\n", device, software);
3955 warned = TRUE;
3958 device->softwareVertexProcessing = software;
3961 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3963 static BOOL warned;
3965 TRACE("device %p.\n", device);
3967 if (!warned)
3969 TRACE("device %p stub!\n", device);
3970 warned = TRUE;
3973 return device->softwareVertexProcessing;
3976 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3977 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3979 struct wined3d_swapchain *swapchain;
3981 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3982 device, swapchain_idx, raster_status);
3984 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3985 return WINED3DERR_INVALIDCALL;
3987 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3990 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3992 static BOOL warned;
3994 TRACE("device %p, segments %.8e.\n", device, segments);
3996 if (segments != 0.0f)
3998 if (!warned)
4000 FIXME("device %p, segments %.8e stub!\n", device, segments);
4001 warned = TRUE;
4005 return WINED3D_OK;
4008 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4010 static BOOL warned;
4012 TRACE("device %p.\n", device);
4014 if (!warned)
4016 FIXME("device %p stub!\n", device);
4017 warned = TRUE;
4020 return 0.0f;
4023 void CDECL wined3d_device_copy_uav_counter(struct wined3d_device *device,
4024 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4026 TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
4027 device, dst_buffer, offset, uav);
4029 wined3d_cs_emit_copy_uav_counter(device->cs, dst_buffer, offset, uav);
4032 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
4033 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4035 struct wined3d_texture *dst_texture, *src_texture;
4036 struct wined3d_box box;
4037 unsigned int i, j;
4039 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
4041 if (src_resource == dst_resource)
4043 WARN("Source and destination are the same resource.\n");
4044 return;
4047 if (src_resource->type != dst_resource->type)
4049 WARN("Resource types (%s / %s) don't match.\n",
4050 debug_d3dresourcetype(dst_resource->type),
4051 debug_d3dresourcetype(src_resource->type));
4052 return;
4055 if (src_resource->width != dst_resource->width
4056 || src_resource->height != dst_resource->height
4057 || src_resource->depth != dst_resource->depth)
4059 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4060 dst_resource->width, dst_resource->height, dst_resource->depth,
4061 src_resource->width, src_resource->height, src_resource->depth);
4062 return;
4065 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4066 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4068 WARN("Resource formats %s and %s are incompatible.\n",
4069 debug_d3dformat(dst_resource->format->id),
4070 debug_d3dformat(src_resource->format->id));
4071 return;
4074 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4076 wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
4077 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
4078 src_resource, 0, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4079 return;
4082 dst_texture = texture_from_resource(dst_resource);
4083 src_texture = texture_from_resource(src_resource);
4085 if (src_texture->layer_count != dst_texture->layer_count
4086 || src_texture->level_count != dst_texture->level_count)
4088 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4089 dst_texture->layer_count, dst_texture->level_count,
4090 src_texture->layer_count, src_texture->level_count);
4091 return;
4094 for (i = 0; i < dst_texture->level_count; ++i)
4096 wined3d_box_set(&box, 0, 0,
4097 wined3d_texture_get_level_width(dst_texture, i),
4098 wined3d_texture_get_level_height(dst_texture, i),
4099 0, wined3d_texture_get_level_depth(dst_texture, i));
4100 for (j = 0; j < dst_texture->layer_count; ++j)
4102 unsigned int idx = j * dst_texture->level_count + i;
4104 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
4105 src_resource, idx, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4110 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
4111 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4112 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4113 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4115 struct wined3d_box dst_box, b;
4117 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4118 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4119 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4120 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4122 if (flags)
4123 FIXME("Ignoring flags %#x.\n", flags);
4125 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4127 WARN("Source and destination are the same sub-resource.\n");
4128 return WINED3DERR_INVALIDCALL;
4131 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4132 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4134 WARN("Resource formats %s and %s are incompatible.\n",
4135 debug_d3dformat(dst_resource->format->id),
4136 debug_d3dformat(src_resource->format->id));
4137 return WINED3DERR_INVALIDCALL;
4140 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4142 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4144 WARN("Resource types (%s / %s) don't match.\n",
4145 debug_d3dresourcetype(dst_resource->type),
4146 debug_d3dresourcetype(src_resource->type));
4147 return WINED3DERR_INVALIDCALL;
4150 if (dst_sub_resource_idx)
4152 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4153 return WINED3DERR_INVALIDCALL;
4156 if (src_sub_resource_idx)
4158 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4159 return WINED3DERR_INVALIDCALL;
4162 if (!src_box)
4164 unsigned int dst_w;
4166 dst_w = dst_resource->size - dst_x;
4167 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4168 src_box = &b;
4170 else if ((src_box->left >= src_box->right
4171 || src_box->top >= src_box->bottom
4172 || src_box->front >= src_box->back))
4174 WARN("Invalid box %s specified.\n", debug_box(src_box));
4175 return WINED3DERR_INVALIDCALL;
4178 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4179 || src_box->right - src_box->left > dst_resource->size - dst_x)
4181 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4182 dst_x, src_box->left, src_box->right - src_box->left);
4183 return WINED3DERR_INVALIDCALL;
4186 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4188 else
4190 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4191 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4192 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4194 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4196 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4197 return WINED3DERR_INVALIDCALL;
4200 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4202 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4203 return WINED3DERR_INVALIDCALL;
4206 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4208 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4209 return WINED3DERR_INVALIDCALL;
4212 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4214 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4215 return WINED3DERR_INVALIDCALL;
4218 if (!src_box)
4220 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4222 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4223 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4224 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4226 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4227 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4228 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4229 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4231 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4232 src_box = &b;
4234 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4236 WARN("Invalid source box %s.\n", debug_box(src_box));
4237 return WINED3DERR_INVALIDCALL;
4240 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4241 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4242 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4243 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4245 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4246 return WINED3DERR_INVALIDCALL;
4250 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4251 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4253 return WINED3D_OK;
4256 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4257 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4258 unsigned int depth_pitch, unsigned int flags)
4260 unsigned int width, height, depth;
4261 struct wined3d_box b;
4263 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, "
4264 "flags %#x.\n",
4265 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4267 if (flags)
4268 FIXME("Ignoring flags %#x.\n", flags);
4270 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4272 WARN("Resource %p is not GPU accessible.\n", resource);
4273 return;
4276 if (resource->type == WINED3D_RTYPE_BUFFER)
4278 if (sub_resource_idx > 0)
4280 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4281 return;
4284 width = resource->size;
4285 height = 1;
4286 depth = 1;
4288 else
4290 struct wined3d_texture *texture = texture_from_resource(resource);
4291 unsigned int level;
4293 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4295 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4296 return;
4299 level = sub_resource_idx % texture->level_count;
4300 width = wined3d_texture_get_level_width(texture, level);
4301 height = wined3d_texture_get_level_height(texture, level);
4302 depth = wined3d_texture_get_level_depth(texture, level);
4305 if (!box)
4307 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4308 box = &b;
4310 else if (box->left >= box->right || box->right > width
4311 || box->top >= box->bottom || box->bottom > height
4312 || box->front >= box->back || box->back > depth)
4314 WARN("Invalid box %s specified.\n", debug_box(box));
4315 return;
4318 wined3d_resource_wait_idle(resource);
4320 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4323 void CDECL wined3d_device_resolve_sub_resource(struct wined3d_device *device,
4324 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4325 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4326 enum wined3d_format_id format_id)
4328 struct wined3d_texture *dst_texture, *src_texture;
4329 unsigned int dst_level, src_level;
4330 RECT dst_rect, src_rect;
4332 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, "
4333 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4334 device, dst_resource, dst_sub_resource_idx,
4335 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4337 if (wined3d_format_is_typeless(dst_resource->format)
4338 || wined3d_format_is_typeless(src_resource->format))
4340 FIXME("Multisample resolve is not fully supported for typeless formats "
4341 "(dst_format %s, src_format %s, format %s).\n",
4342 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4343 debug_d3dformat(format_id));
4345 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4347 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4348 return;
4350 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4352 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4353 return;
4356 dst_texture = texture_from_resource(dst_resource);
4357 src_texture = texture_from_resource(src_resource);
4359 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4360 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4361 wined3d_texture_get_level_height(dst_texture, dst_level));
4362 src_level = src_sub_resource_idx % src_texture->level_count;
4363 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4364 wined3d_texture_get_level_height(src_texture, src_level));
4365 wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4366 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
4369 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4370 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4371 const struct wined3d_color *color, float depth, DWORD stencil)
4373 struct wined3d_resource *resource;
4374 RECT r;
4376 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4377 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4379 if (!flags)
4380 return WINED3D_OK;
4382 resource = view->resource;
4383 if (resource->type == WINED3D_RTYPE_BUFFER)
4385 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4386 return WINED3DERR_INVALIDCALL;
4389 if (view->layer_count != max(1, resource->depth >> view->desc.u.texture.level_idx))
4391 FIXME("Layered clears not implemented.\n");
4392 return WINED3DERR_INVALIDCALL;
4395 if (!rect)
4397 SetRect(&r, 0, 0, view->width, view->height);
4398 rect = &r;
4400 else
4402 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4403 struct wined3d_texture *texture = texture_from_resource(view->resource);
4404 HRESULT hr;
4406 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4407 view->sub_resource_idx % texture->level_count, &b)))
4408 return hr;
4411 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4413 return WINED3D_OK;
4416 void CDECL wined3d_device_clear_unordered_access_view_uint(struct wined3d_device *device,
4417 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4419 TRACE("device %p, view %p, clear_value %s.\n", device, view, debug_uvec4(clear_value));
4421 wined3d_cs_emit_clear_unordered_access_view_uint(device->cs, view, clear_value);
4424 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4425 unsigned int view_idx)
4427 unsigned int max_rt_count;
4429 TRACE("device %p, view_idx %u.\n", device, view_idx);
4431 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
4432 if (view_idx >= max_rt_count)
4434 WARN("Only %u render targets are supported.\n", max_rt_count);
4435 return NULL;
4438 return device->fb.render_targets[view_idx];
4441 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4443 TRACE("device %p.\n", device);
4445 return device->fb.depth_stencil;
4448 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4449 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4451 struct wined3d_rendertarget_view *prev;
4452 unsigned int max_rt_count;
4454 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4455 device, view_idx, view, set_viewport);
4457 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
4458 if (view_idx >= max_rt_count)
4460 WARN("Only %u render targets are supported.\n", max_rt_count);
4461 return WINED3DERR_INVALIDCALL;
4464 if (view && !(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
4466 WARN("View resource %p doesn't have render target bind flags.\n", view->resource);
4467 return WINED3DERR_INVALIDCALL;
4470 /* Set the viewport and scissor rectangles, if requested. Tests show that
4471 * stateblock recording is ignored, the change goes directly into the
4472 * primary stateblock. */
4473 if (!view_idx && set_viewport)
4475 struct wined3d_state *state = &device->state;
4477 state->viewports[0].x = 0;
4478 state->viewports[0].y = 0;
4479 state->viewports[0].width = view->width;
4480 state->viewports[0].height = view->height;
4481 state->viewports[0].min_z = 0.0f;
4482 state->viewports[0].max_z = 1.0f;
4483 state->viewport_count = 1;
4484 wined3d_cs_emit_set_viewports(device->cs, 1, state->viewports);
4486 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
4487 state->scissor_rect_count = 1;
4488 wined3d_cs_emit_set_scissor_rects(device->cs, 1, state->scissor_rects);
4491 prev = device->fb.render_targets[view_idx];
4492 if (view == prev)
4493 return WINED3D_OK;
4495 if (view)
4496 wined3d_rendertarget_view_incref(view);
4497 device->fb.render_targets[view_idx] = view;
4498 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4499 /* Release after the assignment, to prevent device_resource_released()
4500 * from seeing the surface as still in use. */
4501 if (prev)
4502 wined3d_rendertarget_view_decref(prev);
4504 return WINED3D_OK;
4507 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4509 struct wined3d_rendertarget_view *prev;
4511 TRACE("device %p, view %p.\n", device, view);
4513 prev = device->fb.depth_stencil;
4514 if (prev == view)
4516 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4517 return;
4520 if ((device->fb.depth_stencil = view))
4521 wined3d_rendertarget_view_incref(view);
4522 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4523 if (prev)
4524 wined3d_rendertarget_view_decref(prev);
4527 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4528 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4530 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4531 struct wined3d_sub_resource_data data;
4532 struct wined3d_resource_desc desc;
4533 struct wined3d_map_desc map_desc;
4534 struct wined3d_texture *texture;
4535 HRESULT hr;
4537 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4539 ERR("Failed to map source texture.\n");
4540 return NULL;
4543 data.data = map_desc.data;
4544 data.row_pitch = map_desc.row_pitch;
4545 data.slice_pitch = map_desc.slice_pitch;
4547 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4548 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4549 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4550 desc.multisample_quality = 0;
4551 desc.usage = WINED3DUSAGE_DYNAMIC;
4552 desc.bind_flags = 0;
4553 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4554 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4555 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4556 desc.depth = 1;
4557 desc.size = 0;
4559 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4560 &data, NULL, &wined3d_null_parent_ops, &texture);
4561 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4562 if (FAILED(hr))
4564 ERR("Failed to create cursor texture.\n");
4565 return NULL;
4568 return texture;
4571 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4572 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4574 unsigned int texture_level = sub_resource_idx % texture->level_count;
4575 unsigned int cursor_width, cursor_height;
4576 struct wined3d_display_mode mode;
4577 struct wined3d_map_desc map_desc;
4578 HRESULT hr;
4580 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4581 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4583 if (sub_resource_idx >= texture->level_count * texture->layer_count
4584 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4585 return WINED3DERR_INVALIDCALL;
4587 if (device->cursor_texture)
4589 wined3d_texture_decref(device->cursor_texture);
4590 device->cursor_texture = NULL;
4593 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4595 WARN("Texture %p has invalid format %s.\n",
4596 texture, debug_d3dformat(texture->resource.format->id));
4597 return WINED3DERR_INVALIDCALL;
4600 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4602 ERR("Failed to get display mode, hr %#x.\n", hr);
4603 return WINED3DERR_INVALIDCALL;
4606 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4607 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4608 if (cursor_width > mode.width || cursor_height > mode.height)
4610 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4611 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4612 return WINED3DERR_INVALIDCALL;
4615 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4617 /* Do not store the surface's pointer because the application may
4618 * release it after setting the cursor image. Windows doesn't
4619 * addref the set surface, so we can't do this either without
4620 * creating circular refcount dependencies. */
4621 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4623 ERR("Failed to create cursor texture.\n");
4624 return WINED3DERR_INVALIDCALL;
4627 if (cursor_width == 32 && cursor_height == 32)
4629 UINT mask_size = cursor_width * cursor_height / 8;
4630 ICONINFO cursor_info;
4631 DWORD *mask_bits;
4632 HCURSOR cursor;
4634 /* 32-bit user32 cursors ignore the alpha channel if it's all
4635 * zeroes, and use the mask instead. Fill the mask with all ones
4636 * to ensure we still get a fully transparent cursor. */
4637 if (!(mask_bits = heap_alloc(mask_size)))
4638 return E_OUTOFMEMORY;
4639 memset(mask_bits, 0xff, mask_size);
4641 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4642 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
4643 cursor_info.fIcon = FALSE;
4644 cursor_info.xHotspot = x_hotspot;
4645 cursor_info.yHotspot = y_hotspot;
4646 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4647 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4648 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4650 /* Create our cursor and clean up. */
4651 cursor = CreateIconIndirect(&cursor_info);
4652 if (cursor_info.hbmMask)
4653 DeleteObject(cursor_info.hbmMask);
4654 if (cursor_info.hbmColor)
4655 DeleteObject(cursor_info.hbmColor);
4656 if (device->hardwareCursor)
4657 DestroyCursor(device->hardwareCursor);
4658 device->hardwareCursor = cursor;
4659 if (device->bCursorVisible)
4660 SetCursor(cursor);
4662 heap_free(mask_bits);
4665 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4666 device->cursorWidth = cursor_width;
4667 device->cursorHeight = cursor_height;
4668 device->xHotSpot = x_hotspot;
4669 device->yHotSpot = y_hotspot;
4671 return WINED3D_OK;
4674 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4675 int x_screen_space, int y_screen_space, DWORD flags)
4677 TRACE("device %p, x %d, y %d, flags %#x.\n",
4678 device, x_screen_space, y_screen_space, flags);
4680 device->xScreenSpace = x_screen_space;
4681 device->yScreenSpace = y_screen_space;
4683 if (device->hardwareCursor)
4685 POINT pt;
4687 GetCursorPos( &pt );
4688 if (x_screen_space == pt.x && y_screen_space == pt.y)
4689 return;
4690 SetCursorPos( x_screen_space, y_screen_space );
4692 /* Switch to the software cursor if position diverges from the hardware one. */
4693 GetCursorPos( &pt );
4694 if (x_screen_space != pt.x || y_screen_space != pt.y)
4696 if (device->bCursorVisible) SetCursor( NULL );
4697 DestroyCursor( device->hardwareCursor );
4698 device->hardwareCursor = 0;
4703 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4705 BOOL oldVisible = device->bCursorVisible;
4707 TRACE("device %p, show %#x.\n", device, show);
4710 * When ShowCursor is first called it should make the cursor appear at the OS's last
4711 * known cursor position.
4713 if (show && !oldVisible)
4715 POINT pt;
4716 GetCursorPos(&pt);
4717 device->xScreenSpace = pt.x;
4718 device->yScreenSpace = pt.y;
4721 if (device->hardwareCursor)
4723 device->bCursorVisible = show;
4724 if (show)
4725 SetCursor(device->hardwareCursor);
4726 else
4727 SetCursor(NULL);
4729 else if (device->cursor_texture)
4731 device->bCursorVisible = show;
4734 return oldVisible;
4737 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4739 struct wined3d_resource *resource, *cursor;
4741 TRACE("device %p.\n", device);
4743 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4745 TRACE("Checking resource %p for eviction.\n", resource);
4747 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
4749 TRACE("Evicting %p.\n", resource);
4750 wined3d_cs_emit_unload_resource(device->cs, resource);
4755 static void update_swapchain_flags(struct wined3d_texture *texture)
4757 unsigned int flags = texture->swapchain->desc.flags;
4759 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
4760 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
4761 else
4762 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
4764 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4765 texture->flags |= WINED3D_TEXTURE_GET_DC;
4766 else
4767 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
4770 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4771 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4772 wined3d_device_reset_cb callback, BOOL reset_state)
4774 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
4775 struct wined3d_resource *resource, *cursor;
4776 struct wined3d_swapchain *swapchain;
4777 struct wined3d_view_desc view_desc;
4778 BOOL backbuffer_resized;
4779 HRESULT hr = WINED3D_OK;
4780 unsigned int i;
4782 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4783 device, swapchain_desc, mode, callback, reset_state);
4785 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4787 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4789 ERR("Failed to get the first implicit swapchain.\n");
4790 return WINED3DERR_INVALIDCALL;
4793 if (reset_state)
4795 if (device->logo_texture)
4797 wined3d_texture_decref(device->logo_texture);
4798 device->logo_texture = NULL;
4800 if (device->cursor_texture)
4802 wined3d_texture_decref(device->cursor_texture);
4803 device->cursor_texture = NULL;
4805 state_unbind_resources(&device->state);
4808 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
4810 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4812 wined3d_device_set_depth_stencil_view(device, NULL);
4814 if (reset_state)
4816 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4818 TRACE("Enumerating resource %p.\n", resource);
4819 if (FAILED(hr = callback(resource)))
4820 return hr;
4824 TRACE("New params:\n");
4825 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4826 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4827 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4828 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4829 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4830 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4831 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4832 TRACE("device_window %p\n", swapchain_desc->device_window);
4833 TRACE("windowed %#x\n", swapchain_desc->windowed);
4834 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4835 if (swapchain_desc->enable_auto_depth_stencil)
4836 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4837 TRACE("flags %#x\n", swapchain_desc->flags);
4838 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4839 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4841 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
4842 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
4844 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
4845 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
4846 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
4847 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
4849 /* No special treatment of these parameters. Just store them */
4850 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4851 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4852 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4853 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4854 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4856 if (swapchain_desc->device_window
4857 && swapchain_desc->device_window != swapchain->desc.device_window)
4859 TRACE("Changing the device window from %p to %p.\n",
4860 swapchain->desc.device_window, swapchain_desc->device_window);
4861 swapchain->desc.device_window = swapchain_desc->device_window;
4862 swapchain->device_window = swapchain_desc->device_window;
4863 wined3d_swapchain_set_window(swapchain, NULL);
4866 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4867 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4869 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4870 || swapchain->reapply_mode || mode
4871 || (!swapchain_desc->windowed && backbuffer_resized))
4873 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4874 return hr;
4876 else if (!swapchain_desc->windowed)
4878 DWORD style = device->style;
4879 DWORD exStyle = device->exStyle;
4880 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4881 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4882 * Reset to clear up their mess. Guild Wars also loses the device during that.
4884 device->style = 0;
4885 device->exStyle = 0;
4886 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4887 swapchain_desc->backbuffer_width,
4888 swapchain_desc->backbuffer_height);
4889 device->style = style;
4890 device->exStyle = exStyle;
4893 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4894 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4895 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4896 return hr;
4898 if (swapchain_desc->flags != swapchain->desc.flags)
4900 swapchain->desc.flags = swapchain_desc->flags;
4902 update_swapchain_flags(swapchain->front_buffer);
4903 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
4905 update_swapchain_flags(swapchain->back_buffers[i]);
4909 if (device->auto_depth_stencil_view)
4911 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4912 device->auto_depth_stencil_view = NULL;
4914 if (swapchain->desc.enable_auto_depth_stencil)
4916 struct wined3d_resource_desc texture_desc;
4917 struct wined3d_texture *texture;
4919 TRACE("Creating the depth stencil buffer.\n");
4921 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4922 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4923 texture_desc.multisample_type = swapchain->desc.multisample_type;
4924 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4925 texture_desc.usage = 0;
4926 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
4927 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4928 texture_desc.width = swapchain->desc.backbuffer_width;
4929 texture_desc.height = swapchain->desc.backbuffer_height;
4930 texture_desc.depth = 1;
4931 texture_desc.size = 0;
4933 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4934 device->device_parent, &texture_desc, 0, &texture)))
4936 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4937 return WINED3DERR_INVALIDCALL;
4940 view_desc.format_id = texture->resource.format->id;
4941 view_desc.flags = 0;
4942 view_desc.u.texture.level_idx = 0;
4943 view_desc.u.texture.level_count = 1;
4944 view_desc.u.texture.layer_idx = 0;
4945 view_desc.u.texture.layer_count = 1;
4946 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4947 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4948 wined3d_texture_decref(texture);
4949 if (FAILED(hr))
4951 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4952 return hr;
4955 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4958 if (device->back_buffer_view)
4960 wined3d_rendertarget_view_decref(device->back_buffer_view);
4961 device->back_buffer_view = NULL;
4963 if (swapchain->desc.backbuffer_count && swapchain->desc.backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
4965 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4967 view_desc.format_id = back_buffer->format->id;
4968 view_desc.flags = 0;
4969 view_desc.u.texture.level_idx = 0;
4970 view_desc.u.texture.level_count = 1;
4971 view_desc.u.texture.layer_idx = 0;
4972 view_desc.u.texture.layer_count = 1;
4973 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4974 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4976 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4977 return hr;
4981 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4983 if (reset_state)
4985 TRACE("Resetting stateblock.\n");
4986 if (device->recording)
4988 wined3d_stateblock_decref(device->recording);
4989 device->recording = NULL;
4991 wined3d_cs_emit_reset_state(device->cs);
4992 state_cleanup(&device->state);
4994 if (device->d3d_initialized)
4995 wined3d_device_delete_opengl_contexts(device);
4997 memset(&device->state, 0, sizeof(device->state));
4998 state_init(&device->state, &device->fb, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4999 device->update_state = &device->state;
5001 device_init_swapchain_state(device, swapchain);
5002 if (wined3d_settings.logo)
5003 device_load_logo(device, wined3d_settings.logo);
5005 else if (device->back_buffer_view)
5007 struct wined3d_rendertarget_view *view = device->back_buffer_view;
5008 struct wined3d_state *state = &device->state;
5010 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
5012 /* Note the min_z / max_z is not reset. */
5013 state->viewports[0].x = 0;
5014 state->viewports[0].y = 0;
5015 state->viewports[0].width = view->width;
5016 state->viewports[0].height = view->height;
5017 state->viewport_count = 1;
5018 wined3d_cs_emit_set_viewports(device->cs, 1, state->viewports);
5020 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
5021 state->scissor_rect_count = 1;
5022 wined3d_cs_emit_set_scissor_rects(device->cs, 1, state->scissor_rects);
5025 if (device->d3d_initialized)
5027 if (reset_state)
5028 hr = wined3d_device_create_primary_opengl_context(device);
5031 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5032 * first use
5034 return hr;
5037 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5039 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5041 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5043 return WINED3D_OK;
5047 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5048 struct wined3d_device_creation_parameters *parameters)
5050 TRACE("device %p, parameters %p.\n", device, parameters);
5052 *parameters = device->create_parms;
5055 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5057 TRACE("device %p.\n", device);
5059 return device->wined3d;
5062 enum wined3d_feature_level CDECL wined3d_device_get_feature_level(const struct wined3d_device *device)
5064 TRACE("device %p.\n", device);
5066 return device->feature_level;
5069 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5070 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5072 struct wined3d_swapchain *swapchain;
5074 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5075 device, swapchain_idx, flags, ramp);
5077 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5078 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5081 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5082 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5084 struct wined3d_swapchain *swapchain;
5086 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5087 device, swapchain_idx, ramp);
5089 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5090 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5093 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5095 TRACE("device %p, resource %p.\n", device, resource);
5097 wined3d_not_from_cs(device->cs);
5099 list_add_head(&device->resources, &resource->resource_list_entry);
5102 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5104 TRACE("device %p, resource %p.\n", device, resource);
5106 wined3d_not_from_cs(device->cs);
5108 list_remove(&resource->resource_list_entry);
5111 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5113 enum wined3d_resource_type type = resource->type;
5114 struct wined3d_rendertarget_view *rtv;
5115 unsigned int i;
5117 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5119 if (device->d3d_initialized)
5121 for (i = 0; i < ARRAY_SIZE(device->fb.render_targets); ++i)
5123 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
5124 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5127 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5128 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5131 switch (type)
5133 case WINED3D_RTYPE_TEXTURE_1D:
5134 case WINED3D_RTYPE_TEXTURE_2D:
5135 case WINED3D_RTYPE_TEXTURE_3D:
5136 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5138 if (&device->state.textures[i]->resource == resource)
5140 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5141 device->state.textures[i] = NULL;
5144 if (device->recording && &device->update_state->textures[i]->resource == resource)
5146 ERR("Texture resource %p is still in use by recording stateblock %p, stage %u.\n",
5147 resource, device->recording, i);
5148 device->update_state->textures[i] = NULL;
5151 break;
5153 case WINED3D_RTYPE_BUFFER:
5154 for (i = 0; i < MAX_STREAMS; ++i)
5156 if (&device->state.streams[i].buffer->resource == resource)
5158 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5159 device->state.streams[i].buffer = NULL;
5162 if (device->recording && &device->update_state->streams[i].buffer->resource == resource)
5164 ERR("Buffer resource %p is still in use by stateblock %p, stream %u.\n",
5165 resource, device->recording, i);
5166 device->update_state->streams[i].buffer = NULL;
5170 if (&device->state.index_buffer->resource == resource)
5172 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5173 device->state.index_buffer = NULL;
5176 if (device->recording && &device->update_state->index_buffer->resource == resource)
5178 ERR("Buffer resource %p is still in use by stateblock %p as index buffer.\n",
5179 resource, device->recording);
5180 device->update_state->index_buffer = NULL;
5182 break;
5184 default:
5185 break;
5188 /* Remove the resource from the resourceStore */
5189 device_resource_remove(device, resource);
5191 TRACE("Resource released.\n");
5194 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5196 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5198 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5201 static BOOL wined3d_select_feature_level(const struct wined3d_adapter *adapter,
5202 const enum wined3d_feature_level *levels, unsigned int level_count,
5203 enum wined3d_feature_level *selected_level)
5205 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
5206 unsigned int i;
5208 for (i = 0; i < level_count; ++i)
5210 if (levels[i] && d3d_info->feature_level >= levels[i])
5212 *selected_level = levels[i];
5213 return TRUE;
5217 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
5218 "with the current shader backend.\n");
5219 return FALSE;
5222 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5223 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5224 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5225 struct wined3d_device_parent *device_parent)
5227 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5228 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5229 const struct fragment_pipeline *fragment_pipeline;
5230 unsigned int i;
5231 HRESULT hr;
5233 if (!wined3d_select_feature_level(adapter, levels, level_count, &device->feature_level))
5234 return E_FAIL;
5236 TRACE("Device feature level %s.\n", wined3d_debug_feature_level(device->feature_level));
5238 device->ref = 1;
5239 device->wined3d = wined3d;
5240 wined3d_incref(device->wined3d);
5241 device->adapter = wined3d->adapter_count ? adapter : NULL;
5242 device->device_parent = device_parent;
5243 list_init(&device->resources);
5244 list_init(&device->shaders);
5245 device->surface_alignment = surface_alignment;
5247 /* Save the creation parameters. */
5248 device->create_parms.adapter_idx = adapter_idx;
5249 device->create_parms.device_type = device_type;
5250 device->create_parms.focus_window = focus_window;
5251 device->create_parms.flags = flags;
5253 device->shader_backend = adapter->shader_backend;
5255 vertex_pipeline = adapter->vertex_pipe;
5257 fragment_pipeline = adapter->fragment_pipe;
5259 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5261 if (vertex_pipeline->vp_states && fragment_pipeline->states
5262 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5263 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5264 fragment_pipeline, misc_state_template)))
5266 ERR("Failed to compile state table, hr %#x.\n", hr);
5267 wine_rb_destroy(&device->samplers, NULL, NULL);
5268 wined3d_decref(device->wined3d);
5269 return hr;
5272 state_init(&device->state, &device->fb, &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5273 device->update_state = &device->state;
5275 device->max_frame_latency = 3;
5277 if (!(device->cs = wined3d_cs_create(device)))
5279 WARN("Failed to create command stream.\n");
5280 state_cleanup(&device->state);
5281 hr = E_FAIL;
5282 goto err;
5285 return WINED3D_OK;
5287 err:
5288 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5290 heap_free(device->multistate_funcs[i]);
5292 wine_rb_destroy(&device->samplers, NULL, NULL);
5293 wined3d_decref(device->wined3d);
5294 return hr;
5297 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5299 DWORD rep = device->StateTable[state].representative;
5300 struct wined3d_context *context;
5301 DWORD idx;
5302 BYTE shift;
5303 UINT i;
5305 wined3d_from_cs(device->cs);
5307 if (STATE_IS_COMPUTE(state))
5309 for (i = 0; i < device->context_count; ++i)
5310 context_invalidate_compute_state(device->contexts[i], state);
5311 return;
5314 for (i = 0; i < device->context_count; ++i)
5316 context = device->contexts[i];
5317 if(isStateDirty(context, rep)) continue;
5319 context->dirtyArray[context->numDirtyEntries++] = rep;
5320 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5321 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5322 context->isStateDirty[idx] |= (1u << shift);
5326 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5327 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5329 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5331 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5332 window, message, wparam, lparam);
5333 if (unicode)
5334 return DefWindowProcW(window, message, wparam, lparam);
5335 else
5336 return DefWindowProcA(window, message, wparam, lparam);
5339 if (message == WM_DESTROY)
5341 TRACE("unregister window %p.\n", window);
5342 wined3d_unregister_window(window);
5344 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5345 ERR("Window %p is not the focus window for device %p.\n", window, device);
5347 else if (message == WM_DISPLAYCHANGE)
5349 device->device_parent->ops->mode_changed(device->device_parent);
5351 else if (message == WM_ACTIVATEAPP)
5353 unsigned int i = device->swapchain_count;
5355 /* Deactivating the implicit swapchain may cause the application
5356 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5357 * deactivate the implicit swapchain last, and to avoid accessing the
5358 * "device" pointer afterwards. */
5359 while (i--)
5360 wined3d_swapchain_activate(device->swapchains[i], wparam);
5362 else if (message == WM_SYSCOMMAND)
5364 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5366 if (unicode)
5367 DefWindowProcW(window, message, wparam, lparam);
5368 else
5369 DefWindowProcA(window, message, wparam, lparam);
5373 if (unicode)
5374 return CallWindowProcW(proc, window, message, wparam, lparam);
5375 else
5376 return CallWindowProcA(proc, window, message, wparam, lparam);