wineconsole: Try harder to get a scalable font.
[wine.git] / dlls / wined3d / device.c
blobabb2a893637338664813cc9aa22c92232511dcd5
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(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.access = WINED3D_RESOURCE_ACCESS_GPU;
580 desc.width = bm.bmWidth;
581 desc.height = bm.bmHeight;
582 desc.depth = 1;
583 desc.size = 0;
584 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1,
585 WINED3D_TEXTURE_CREATE_MAPPABLE | WINED3D_TEXTURE_CREATE_GET_DC,
586 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
588 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
589 goto out;
592 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
594 wined3d_texture_decref(device->logo_texture);
595 device->logo_texture = NULL;
596 goto out;
598 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
599 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
601 color_key.color_space_low_value = 0;
602 color_key.color_space_high_value = 0;
603 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
605 out:
606 if (dcb) DeleteDC(dcb);
607 if (hbm) DeleteObject(hbm);
610 /* Context activation is done by the caller. */
611 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
613 struct wined3d_dummy_textures *textures = &device->dummy_textures;
614 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
615 const struct wined3d_gl_info *gl_info = context->gl_info;
616 unsigned int i;
617 DWORD color;
619 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
620 color = 0x000000ff;
621 else
622 color = 0x00000000;
624 /* Under DirectX you can sample even if no texture is bound, whereas
625 * OpenGL will only allow that when a valid texture is bound.
626 * We emulate this by creating dummy textures and binding them
627 * to each texture stage when the currently set D3D texture is NULL. */
628 context_active_texture(context, gl_info, 0);
630 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
631 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
632 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
633 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
634 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
636 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
637 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
638 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
639 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
640 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
642 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
644 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
645 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
646 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
647 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
648 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
651 if (gl_info->supported[EXT_TEXTURE3D])
653 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
654 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
655 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
656 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
657 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
660 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
662 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
663 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
664 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
665 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
667 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
668 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
672 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
674 DWORD cube_array_data[6];
676 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
677 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
678 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
679 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
680 cube_array_data[i] = color;
681 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
682 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
685 if (gl_info->supported[EXT_TEXTURE_ARRAY])
687 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
688 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
689 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
690 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
691 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
693 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
694 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
695 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
696 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
697 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
700 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
702 GLuint buffer;
704 GL_EXTCALL(glGenBuffers(1, &buffer));
705 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
706 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
707 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
709 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
710 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
711 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
712 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
713 GL_EXTCALL(glDeleteBuffers(1, &buffer));
716 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
718 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
719 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
720 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
721 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
723 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
724 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
725 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
726 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
728 if (gl_info->supported[ARB_CLEAR_TEXTURE])
730 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
731 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
733 else
735 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
739 checkGLcall("create dummy textures");
741 context_bind_dummy_textures(device, context);
744 /* Context activation is done by the caller. */
745 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
747 struct wined3d_dummy_textures *dummy_textures = &device->dummy_textures;
748 const struct wined3d_gl_info *gl_info = context->gl_info;
750 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
752 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
753 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
756 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
757 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
759 if (gl_info->supported[EXT_TEXTURE_ARRAY])
761 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
762 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
765 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
766 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
768 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
769 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
771 if (gl_info->supported[EXT_TEXTURE3D])
772 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
774 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
775 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
777 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
778 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
780 checkGLcall("delete dummy textures");
782 memset(dummy_textures, 0, sizeof(*dummy_textures));
785 /* Context activation is done by the caller. */
786 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
788 struct wined3d_sampler_desc desc;
789 HRESULT hr;
791 desc.address_u = WINED3D_TADDRESS_WRAP;
792 desc.address_v = WINED3D_TADDRESS_WRAP;
793 desc.address_w = WINED3D_TADDRESS_WRAP;
794 memset(desc.border_color, 0, sizeof(desc.border_color));
795 desc.mag_filter = WINED3D_TEXF_POINT;
796 desc.min_filter = WINED3D_TEXF_POINT;
797 desc.mip_filter = WINED3D_TEXF_NONE;
798 desc.lod_bias = 0.0f;
799 desc.min_lod = -1000.0f;
800 desc.max_lod = 1000.0f;
801 desc.mip_base_level = 0;
802 desc.max_anisotropy = 1;
803 desc.compare = FALSE;
804 desc.comparison_func = WINED3D_CMP_NEVER;
805 desc.srgb_decode = TRUE;
807 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
808 * instructions allow access to resources without using samplers.
809 * In GLSL, resources are always accessed through sampler or image variables. The default
810 * sampler object is used to emulate the direct resource access when there is no sampler state
811 * to use.
813 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
815 ERR("Failed to create default sampler, hr %#x.\n", hr);
816 device->default_sampler = NULL;
819 /* In D3D10+, a NULL sampler maps to the default sampler state. */
820 desc.address_u = WINED3D_TADDRESS_CLAMP;
821 desc.address_v = WINED3D_TADDRESS_CLAMP;
822 desc.address_w = WINED3D_TADDRESS_CLAMP;
823 desc.mag_filter = WINED3D_TEXF_LINEAR;
824 desc.min_filter = WINED3D_TEXF_LINEAR;
825 desc.mip_filter = WINED3D_TEXF_LINEAR;
826 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
828 ERR("Failed to create null sampler, hr %#x.\n", hr);
829 device->null_sampler = NULL;
833 /* Context activation is done by the caller. */
834 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
836 wined3d_sampler_decref(device->default_sampler);
837 device->default_sampler = NULL;
838 wined3d_sampler_decref(device->null_sampler);
839 device->null_sampler = NULL;
842 static LONG fullscreen_style(LONG style)
844 /* Make sure the window is managed, otherwise we won't get keyboard input. */
845 style |= WS_POPUP | WS_SYSMENU;
846 style &= ~(WS_CAPTION | WS_THICKFRAME);
848 return style;
851 static LONG fullscreen_exstyle(LONG exstyle)
853 /* Filter out window decorations. */
854 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
856 return exstyle;
859 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
861 BOOL filter_messages;
862 LONG style, exstyle;
864 TRACE("Setting up window %p for fullscreen mode.\n", window);
866 if (device->style || device->exStyle)
868 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
869 window, device->style, device->exStyle);
872 device->style = GetWindowLongW(window, GWL_STYLE);
873 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
875 style = fullscreen_style(device->style);
876 exstyle = fullscreen_exstyle(device->exStyle);
878 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
879 device->style, device->exStyle, style, exstyle);
881 filter_messages = device->filter_messages;
882 device->filter_messages = TRUE;
884 SetWindowLongW(window, GWL_STYLE, style);
885 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
886 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
888 device->filter_messages = filter_messages;
891 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
892 const RECT *window_rect)
894 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
895 BOOL filter_messages;
896 LONG style, exstyle;
897 RECT rect = {0};
899 if (!device->style && !device->exStyle)
900 return;
902 style = GetWindowLongW(window, GWL_STYLE);
903 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
905 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
906 * application, and we want to ignore them in the test below, since it's
907 * not the application's fault that they changed. Additionally, we want to
908 * preserve the current status of these flags (i.e. don't restore them) to
909 * more closely emulate the behavior of Direct3D, which leaves these flags
910 * alone when returning to windowed mode. */
911 device->style ^= (device->style ^ style) & WS_VISIBLE;
912 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
914 TRACE("Restoring window style of window %p to %08x, %08x.\n",
915 window, device->style, device->exStyle);
917 filter_messages = device->filter_messages;
918 device->filter_messages = TRUE;
920 /* Only restore the style if the application didn't modify it during the
921 * fullscreen phase. Some applications change it before calling Reset()
922 * when switching between windowed and fullscreen modes (HL2), some
923 * depend on the original style (Eve Online). */
924 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
926 SetWindowLongW(window, GWL_STYLE, device->style);
927 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
930 if (window_rect)
931 rect = *window_rect;
932 else
933 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
934 SetWindowPos(window, 0, rect.left, rect.top,
935 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
937 device->filter_messages = filter_messages;
939 /* Delete the old values. */
940 device->style = 0;
941 device->exStyle = 0;
944 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
946 TRACE("device %p, window %p.\n", device, window);
948 if (!wined3d_register_window(window, device))
950 ERR("Failed to register window %p.\n", window);
951 return E_FAIL;
954 InterlockedExchangePointer((void **)&device->focus_window, window);
955 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
957 return WINED3D_OK;
960 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
962 TRACE("device %p.\n", device);
964 if (device->focus_window) wined3d_unregister_window(device->focus_window);
965 InterlockedExchangePointer((void **)&device->focus_window, NULL);
968 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
970 BOOL ds_enable = swapchain->desc.enable_auto_depth_stencil;
971 unsigned int i;
973 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
975 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
977 if (device->back_buffer_view)
978 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
980 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
983 static void wined3d_device_delete_opengl_contexts_cs(void *object)
985 struct wined3d_resource *resource, *cursor;
986 struct wined3d_device *device = object;
987 struct wined3d_context *context;
988 struct wined3d_shader *shader;
990 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
992 TRACE("Unloading resource %p.\n", resource);
993 wined3d_cs_emit_unload_resource(device->cs, resource);
996 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
998 device->shader_backend->shader_destroy(shader);
1001 context = context_acquire(device, NULL, 0);
1002 device->blitter->ops->blitter_destroy(device->blitter, context);
1003 device->shader_backend->shader_free_private(device);
1004 destroy_dummy_textures(device, context);
1005 destroy_default_samplers(device, context);
1006 context_release(context);
1008 while (device->context_count)
1010 if (device->contexts[0]->swapchain)
1011 swapchain_destroy_contexts(device->contexts[0]->swapchain);
1012 else
1013 context_destroy(device, device->contexts[0]);
1017 static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
1019 wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
1020 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1023 static void wined3d_device_create_primary_opengl_context_cs(void *object)
1025 struct wined3d_device *device = object;
1026 struct wined3d_swapchain *swapchain;
1027 struct wined3d_context *context;
1028 struct wined3d_texture *target;
1029 HRESULT hr;
1031 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1032 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1034 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1035 return;
1038 if (!(device->blitter = wined3d_cpu_blitter_create()))
1040 ERR("Failed to create CPU blitter.\n");
1041 device->shader_backend->shader_free_private(device);
1042 return;
1044 wined3d_ffp_blitter_create(&device->blitter, &device->adapter->gl_info);
1045 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1046 wined3d_arbfp_blitter_create(&device->blitter, device);
1047 wined3d_fbo_blitter_create(&device->blitter, &device->adapter->gl_info);
1048 wined3d_raw_blitter_create(&device->blitter, &device->adapter->gl_info);
1050 swapchain = device->swapchains[0];
1051 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1052 context = context_acquire(device, target, 0);
1053 create_dummy_textures(device, context);
1054 create_default_samplers(device, context);
1055 context_release(context);
1058 static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1060 wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1061 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1062 if (!device->swapchains[0]->num_contexts)
1063 return E_FAIL;
1065 return WINED3D_OK;
1068 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1069 struct wined3d_swapchain_desc *swapchain_desc)
1071 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1072 struct wined3d_swapchain *swapchain = NULL;
1073 DWORD clear_flags = 0;
1074 HRESULT hr;
1076 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1078 if (device->d3d_initialized)
1079 return WINED3DERR_INVALIDCALL;
1080 if (device->wined3d->flags & WINED3D_NO3D)
1081 return WINED3DERR_INVALIDCALL;
1083 memset(device->fb.render_targets, 0, sizeof(device->fb.render_targets));
1085 /* Setup the implicit swapchain. This also initializes a context. */
1086 TRACE("Creating implicit swapchain.\n");
1087 if (FAILED(hr = device->device_parent->ops->create_swapchain(device->device_parent,
1088 swapchain_desc, &swapchain)))
1090 WARN("Failed to create implicit swapchain.\n");
1091 goto err_out;
1094 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_usage & WINED3DUSAGE_RENDERTARGET)
1096 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1097 struct wined3d_view_desc view_desc;
1099 view_desc.format_id = back_buffer->format->id;
1100 view_desc.flags = 0;
1101 view_desc.u.texture.level_idx = 0;
1102 view_desc.u.texture.level_count = 1;
1103 view_desc.u.texture.layer_idx = 0;
1104 view_desc.u.texture.layer_count = 1;
1105 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1106 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1108 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1109 goto err_out;
1113 device->swapchain_count = 1;
1114 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1116 ERR("Out of memory!\n");
1117 goto err_out;
1119 device->swapchains[0] = swapchain;
1121 if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1122 goto err_out;
1123 device_init_swapchain_state(device, swapchain);
1125 device->contexts[0]->last_was_rhw = 0;
1127 TRACE("All defaults now set up.\n");
1129 /* Clear the screen */
1130 if (device->back_buffer_view)
1131 clear_flags |= WINED3DCLEAR_TARGET;
1132 if (swapchain_desc->enable_auto_depth_stencil)
1133 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1134 if (clear_flags)
1135 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1137 device->d3d_initialized = TRUE;
1139 if (wined3d_settings.logo)
1140 device_load_logo(device, wined3d_settings.logo);
1141 return WINED3D_OK;
1143 err_out:
1144 heap_free(device->swapchains);
1145 device->swapchain_count = 0;
1146 if (device->back_buffer_view)
1147 wined3d_rendertarget_view_decref(device->back_buffer_view);
1148 if (swapchain)
1149 wined3d_swapchain_decref(swapchain);
1151 return hr;
1154 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1155 struct wined3d_swapchain_desc *swapchain_desc)
1157 struct wined3d_swapchain *swapchain = NULL;
1158 HRESULT hr;
1160 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1162 /* Setup the implicit swapchain */
1163 TRACE("Creating implicit swapchain\n");
1164 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1165 swapchain_desc, &swapchain);
1166 if (FAILED(hr))
1168 WARN("Failed to create implicit swapchain\n");
1169 goto err_out;
1172 device->swapchain_count = 1;
1173 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1175 ERR("Out of memory!\n");
1176 goto err_out;
1178 device->swapchains[0] = swapchain;
1180 if (!(device->blitter = wined3d_cpu_blitter_create()))
1182 ERR("Failed to create CPU blitter.\n");
1183 heap_free(device->swapchains);
1184 device->swapchain_count = 0;
1185 goto err_out;
1188 return WINED3D_OK;
1190 err_out:
1191 wined3d_swapchain_decref(swapchain);
1192 return hr;
1195 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1197 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1199 wined3d_sampler_decref(sampler);
1202 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1204 unsigned int i;
1206 TRACE("device %p.\n", device);
1208 if (!device->d3d_initialized)
1209 return WINED3DERR_INVALIDCALL;
1211 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1213 if (device->logo_texture)
1214 wined3d_texture_decref(device->logo_texture);
1215 if (device->cursor_texture)
1216 wined3d_texture_decref(device->cursor_texture);
1218 state_unbind_resources(&device->state);
1220 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1222 wined3d_device_delete_opengl_contexts(device);
1224 if (device->fb.depth_stencil)
1226 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1228 TRACE("Releasing depth/stencil view %p.\n", view);
1230 device->fb.depth_stencil = NULL;
1231 wined3d_rendertarget_view_decref(view);
1234 if (device->auto_depth_stencil_view)
1236 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1238 device->auto_depth_stencil_view = NULL;
1239 if (wined3d_rendertarget_view_decref(view))
1240 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1243 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
1245 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1247 if (device->back_buffer_view)
1249 wined3d_rendertarget_view_decref(device->back_buffer_view);
1250 device->back_buffer_view = NULL;
1253 for (i = 0; i < device->swapchain_count; ++i)
1255 TRACE("Releasing the implicit swapchain %u.\n", i);
1256 if (wined3d_swapchain_decref(device->swapchains[i]))
1257 FIXME("Something's still holding the implicit swapchain.\n");
1260 heap_free(device->swapchains);
1261 device->swapchains = NULL;
1262 device->swapchain_count = 0;
1264 device->d3d_initialized = FALSE;
1266 return WINED3D_OK;
1269 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1271 unsigned int i;
1273 device->blitter->ops->blitter_destroy(device->blitter, NULL);
1275 for (i = 0; i < device->swapchain_count; ++i)
1277 TRACE("Releasing the implicit swapchain %u.\n", i);
1278 if (wined3d_swapchain_decref(device->swapchains[i]))
1279 FIXME("Something's still holding the implicit swapchain.\n");
1282 heap_free(device->swapchains);
1283 device->swapchains = NULL;
1284 device->swapchain_count = 0;
1285 return WINED3D_OK;
1288 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1289 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1290 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1292 * There is no way to deactivate thread safety once it is enabled.
1294 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1296 TRACE("device %p.\n", device);
1298 /* For now just store the flag (needed in case of ddraw). */
1299 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1302 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1304 TRACE("device %p.\n", device);
1306 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1307 wine_dbgstr_longlong(device->adapter->vram_bytes),
1308 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1309 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1311 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1314 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1315 struct wined3d_buffer *buffer, UINT offset)
1317 struct wined3d_stream_output *stream;
1318 struct wined3d_buffer *prev_buffer;
1320 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1322 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1324 WARN("Invalid stream output %u.\n", idx);
1325 return;
1328 stream = &device->update_state->stream_output[idx];
1329 prev_buffer = stream->buffer;
1331 if (buffer)
1332 wined3d_buffer_incref(buffer);
1333 stream->buffer = buffer;
1334 stream->offset = offset;
1335 if (!device->recording)
1336 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1337 if (prev_buffer)
1338 wined3d_buffer_decref(prev_buffer);
1341 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1342 UINT idx, UINT *offset)
1344 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1346 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1348 WARN("Invalid stream output %u.\n", idx);
1349 return NULL;
1352 if (offset)
1353 *offset = device->state.stream_output[idx].offset;
1354 return device->state.stream_output[idx].buffer;
1357 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1358 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1360 struct wined3d_stream_state *stream;
1361 struct wined3d_buffer *prev_buffer;
1363 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1364 device, stream_idx, buffer, offset, stride);
1366 if (stream_idx >= MAX_STREAMS)
1368 WARN("Stream index %u out of range.\n", stream_idx);
1369 return WINED3DERR_INVALIDCALL;
1371 else if (offset & 0x3)
1373 WARN("Offset %u is not 4 byte aligned.\n", offset);
1374 return WINED3DERR_INVALIDCALL;
1377 stream = &device->update_state->streams[stream_idx];
1378 prev_buffer = stream->buffer;
1380 if (device->recording)
1381 device->recording->changed.streamSource |= 1u << stream_idx;
1383 if (prev_buffer == buffer
1384 && stream->stride == stride
1385 && stream->offset == offset)
1387 TRACE("Application is setting the old values over, nothing to do.\n");
1388 return WINED3D_OK;
1391 stream->buffer = buffer;
1392 stream->stride = stride;
1393 stream->offset = offset;
1394 if (buffer)
1395 wined3d_buffer_incref(buffer);
1397 if (!device->recording)
1398 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1399 if (prev_buffer)
1400 wined3d_buffer_decref(prev_buffer);
1402 return WINED3D_OK;
1405 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1406 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1408 const struct wined3d_stream_state *stream;
1410 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1411 device, stream_idx, buffer, offset, stride);
1413 if (stream_idx >= MAX_STREAMS)
1415 WARN("Stream index %u out of range.\n", stream_idx);
1416 return WINED3DERR_INVALIDCALL;
1419 stream = &device->state.streams[stream_idx];
1420 *buffer = stream->buffer;
1421 if (offset)
1422 *offset = stream->offset;
1423 *stride = stream->stride;
1425 return WINED3D_OK;
1428 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1430 struct wined3d_stream_state *stream;
1431 UINT old_flags, old_freq;
1433 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1435 /* Verify input. At least in d3d9 this is invalid. */
1436 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1438 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1439 return WINED3DERR_INVALIDCALL;
1441 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1443 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1444 return WINED3DERR_INVALIDCALL;
1446 if (!divider)
1448 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1449 return WINED3DERR_INVALIDCALL;
1452 stream = &device->update_state->streams[stream_idx];
1453 old_flags = stream->flags;
1454 old_freq = stream->frequency;
1456 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1457 stream->frequency = divider & 0x7fffff;
1459 if (device->recording)
1460 device->recording->changed.streamFreq |= 1u << stream_idx;
1461 else if (stream->frequency != old_freq || stream->flags != old_flags)
1462 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1464 return WINED3D_OK;
1467 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1468 UINT stream_idx, UINT *divider)
1470 const struct wined3d_stream_state *stream;
1472 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1474 stream = &device->state.streams[stream_idx];
1475 *divider = stream->flags | stream->frequency;
1477 TRACE("Returning %#x.\n", *divider);
1479 return WINED3D_OK;
1482 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1483 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1485 TRACE("device %p, state %s, matrix %p.\n",
1486 device, debug_d3dtstype(d3dts), matrix);
1487 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1488 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1489 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1490 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1492 /* Handle recording of state blocks. */
1493 if (device->recording)
1495 TRACE("Recording... not performing anything.\n");
1496 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1497 device->update_state->transforms[d3dts] = *matrix;
1498 return;
1501 /* If the new matrix is the same as the current one,
1502 * we cut off any further processing. this seems to be a reasonable
1503 * optimization because as was noticed, some apps (warcraft3 for example)
1504 * tend towards setting the same matrix repeatedly for some reason.
1506 * From here on we assume that the new matrix is different, wherever it matters. */
1507 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1509 TRACE("The application is setting the same matrix over again.\n");
1510 return;
1513 device->state.transforms[d3dts] = *matrix;
1514 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1517 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1518 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1520 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1522 *matrix = device->state.transforms[state];
1525 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1526 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1528 const struct wined3d_matrix *mat;
1529 struct wined3d_matrix temp;
1531 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1533 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1534 * below means it will be recorded in a state block change, but it
1535 * works regardless where it is recorded.
1536 * If this is found to be wrong, change to StateBlock. */
1537 if (state > HIGHEST_TRANSFORMSTATE)
1539 WARN("Unhandled transform state %#x.\n", state);
1540 return;
1543 mat = &device->update_state->transforms[state];
1544 multiply_matrix(&temp, mat, matrix);
1546 /* Apply change via set transform - will reapply to eg. lights this way. */
1547 wined3d_device_set_transform(device, state, &temp);
1550 /* Note lights are real special cases. Although the device caps state only
1551 * e.g. 8 are supported, you can reference any indexes you want as long as
1552 * that number max are enabled at any one point in time. Therefore since the
1553 * indices can be anything, we need a hashmap of them. However, this causes
1554 * stateblock problems. When capturing the state block, I duplicate the
1555 * hashmap, but when recording, just build a chain pretty much of commands to
1556 * be replayed. */
1557 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1558 UINT light_idx, const struct wined3d_light *light)
1560 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1561 struct wined3d_light_info *object = NULL;
1562 float rho;
1564 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1566 /* Check the parameter range. Need for speed most wanted sets junk lights
1567 * which confuse the GL driver. */
1568 if (!light)
1569 return WINED3DERR_INVALIDCALL;
1571 switch (light->type)
1573 case WINED3D_LIGHT_POINT:
1574 case WINED3D_LIGHT_SPOT:
1575 case WINED3D_LIGHT_GLSPOT:
1576 /* Incorrect attenuation values can cause the gl driver to crash.
1577 * Happens with Need for speed most wanted. */
1578 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1580 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1581 return WINED3DERR_INVALIDCALL;
1583 break;
1585 case WINED3D_LIGHT_DIRECTIONAL:
1586 case WINED3D_LIGHT_PARALLELPOINT:
1587 /* Ignores attenuation */
1588 break;
1590 default:
1591 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1592 return WINED3DERR_INVALIDCALL;
1595 if (!(object = wined3d_state_get_light(device->update_state, light_idx)))
1597 TRACE("Adding new light\n");
1598 if (!(object = heap_alloc_zero(sizeof(*object))))
1599 return E_OUTOFMEMORY;
1601 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1602 object->glIndex = -1;
1603 object->OriginalIndex = light_idx;
1606 /* Initialize the object. */
1607 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1608 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1609 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1610 light_idx, light->type, debug_color(&light->diffuse),
1611 debug_color(&light->specular), debug_color(&light->ambient),
1612 light->position.x, light->position.y, light->position.z,
1613 light->direction.x, light->direction.y, light->direction.z,
1614 light->range, light->falloff, light->theta, light->phi);
1616 /* Save away the information. */
1617 object->OriginalParms = *light;
1619 switch (light->type)
1621 case WINED3D_LIGHT_POINT:
1622 /* Position */
1623 object->position.x = light->position.x;
1624 object->position.y = light->position.y;
1625 object->position.z = light->position.z;
1626 object->position.w = 1.0f;
1627 object->cutoff = 180.0f;
1628 /* FIXME: Range */
1629 break;
1631 case WINED3D_LIGHT_DIRECTIONAL:
1632 /* Direction */
1633 object->direction.x = -light->direction.x;
1634 object->direction.y = -light->direction.y;
1635 object->direction.z = -light->direction.z;
1636 object->direction.w = 0.0f;
1637 object->exponent = 0.0f;
1638 object->cutoff = 180.0f;
1639 break;
1641 case WINED3D_LIGHT_SPOT:
1642 /* Position */
1643 object->position.x = light->position.x;
1644 object->position.y = light->position.y;
1645 object->position.z = light->position.z;
1646 object->position.w = 1.0f;
1648 /* Direction */
1649 object->direction.x = light->direction.x;
1650 object->direction.y = light->direction.y;
1651 object->direction.z = light->direction.z;
1652 object->direction.w = 0.0f;
1654 /* opengl-ish and d3d-ish spot lights use too different models
1655 * for the light "intensity" as a function of the angle towards
1656 * the main light direction, so we only can approximate very
1657 * roughly. However, spot lights are rather rarely used in games
1658 * (if ever used at all). Furthermore if still used, probably
1659 * nobody pays attention to such details. */
1660 if (!light->falloff)
1662 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1663 * equations have the falloff resp. exponent parameter as an
1664 * exponent, so the spot light lighting will always be 1.0 for
1665 * both of them, and we don't have to care for the rest of the
1666 * rather complex calculation. */
1667 object->exponent = 0.0f;
1669 else
1671 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1672 if (rho < 0.0001f)
1673 rho = 0.0001f;
1674 object->exponent = -0.3f / logf(cosf(rho / 2));
1677 if (object->exponent > 128.0f)
1678 object->exponent = 128.0f;
1680 object->cutoff = (float)(light->phi * 90 / M_PI);
1681 /* FIXME: Range */
1682 break;
1684 case WINED3D_LIGHT_PARALLELPOINT:
1685 object->position.x = light->position.x;
1686 object->position.y = light->position.y;
1687 object->position.z = light->position.z;
1688 object->position.w = 1.0f;
1689 break;
1691 default:
1692 FIXME("Unrecognized light type %#x.\n", light->type);
1695 if (!device->recording)
1696 wined3d_cs_emit_set_light(device->cs, object);
1698 return WINED3D_OK;
1701 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1702 UINT light_idx, struct wined3d_light *light)
1704 struct wined3d_light_info *light_info;
1706 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1708 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1710 TRACE("Light information requested but light not defined\n");
1711 return WINED3DERR_INVALIDCALL;
1714 *light = light_info->OriginalParms;
1715 return WINED3D_OK;
1718 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1720 struct wined3d_light_info *light_info;
1722 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1724 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1725 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1727 TRACE("Light enabled requested but light not defined, so defining one!\n");
1728 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1730 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1732 FIXME("Adding default lights has failed dismally\n");
1733 return WINED3DERR_INVALIDCALL;
1737 wined3d_state_enable_light(device->update_state, &device->adapter->d3d_info, light_info, enable);
1738 if (!device->recording)
1739 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1741 return WINED3D_OK;
1744 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1746 struct wined3d_light_info *light_info;
1748 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1750 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1752 TRACE("Light enabled state requested but light not defined.\n");
1753 return WINED3DERR_INVALIDCALL;
1755 /* true is 128 according to SetLightEnable */
1756 *enable = light_info->enabled ? 128 : 0;
1757 return WINED3D_OK;
1760 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1761 UINT plane_idx, const struct wined3d_vec4 *plane)
1763 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1765 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1767 TRACE("Application has requested clipplane this device doesn't support.\n");
1768 return WINED3DERR_INVALIDCALL;
1771 if (device->recording)
1772 device->recording->changed.clipplane |= 1u << plane_idx;
1774 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1776 TRACE("Application is setting old values over, nothing to do.\n");
1777 return WINED3D_OK;
1780 device->update_state->clip_planes[plane_idx] = *plane;
1782 if (!device->recording)
1783 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1785 return WINED3D_OK;
1788 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1789 UINT plane_idx, struct wined3d_vec4 *plane)
1791 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1793 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1795 TRACE("Application has requested clipplane this device doesn't support.\n");
1796 return WINED3DERR_INVALIDCALL;
1799 *plane = device->state.clip_planes[plane_idx];
1801 return WINED3D_OK;
1804 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1805 const struct wined3d_clip_status *clip_status)
1807 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1809 if (!clip_status)
1810 return WINED3DERR_INVALIDCALL;
1812 return WINED3D_OK;
1815 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1816 struct wined3d_clip_status *clip_status)
1818 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1820 if (!clip_status)
1821 return WINED3DERR_INVALIDCALL;
1823 return WINED3D_OK;
1826 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1828 TRACE("device %p, material %p.\n", device, material);
1830 device->update_state->material = *material;
1832 if (device->recording)
1833 device->recording->changed.material = TRUE;
1834 else
1835 wined3d_cs_emit_set_material(device->cs, material);
1838 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1840 TRACE("device %p, material %p.\n", device, material);
1842 *material = device->state.material;
1844 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1845 TRACE("ambient %s\n", debug_color(&material->ambient));
1846 TRACE("specular %s\n", debug_color(&material->specular));
1847 TRACE("emissive %s\n", debug_color(&material->emissive));
1848 TRACE("power %.8e.\n", material->power);
1851 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1852 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1854 enum wined3d_format_id prev_format;
1855 struct wined3d_buffer *prev_buffer;
1856 unsigned int prev_offset;
1858 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1859 device, buffer, debug_d3dformat(format_id), offset);
1861 prev_buffer = device->update_state->index_buffer;
1862 prev_format = device->update_state->index_format;
1863 prev_offset = device->update_state->index_offset;
1865 device->update_state->index_buffer = buffer;
1866 device->update_state->index_format = format_id;
1867 device->update_state->index_offset = offset;
1869 if (device->recording)
1870 device->recording->changed.indices = TRUE;
1872 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1873 return;
1875 if (buffer)
1876 wined3d_buffer_incref(buffer);
1877 if (!device->recording)
1878 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1879 if (prev_buffer)
1880 wined3d_buffer_decref(prev_buffer);
1883 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1884 enum wined3d_format_id *format, unsigned int *offset)
1886 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1888 *format = device->state.index_format;
1889 if (offset)
1890 *offset = device->state.index_offset;
1891 return device->state.index_buffer;
1894 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1896 TRACE("device %p, base_index %d.\n", device, base_index);
1898 device->update_state->base_vertex_index = base_index;
1901 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1903 TRACE("device %p.\n", device);
1905 return device->state.base_vertex_index;
1908 void CDECL wined3d_device_set_viewports(struct wined3d_device *device, unsigned int viewport_count,
1909 const struct wined3d_viewport *viewports)
1911 unsigned int i;
1913 TRACE("device %p, viewport_count %u, viewports %p.\n", device, viewport_count, viewports);
1915 for (i = 0; i < viewport_count; ++i)
1917 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1918 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1921 if (viewport_count)
1922 memcpy(device->update_state->viewports, viewports, viewport_count * sizeof(*viewports));
1923 else
1924 memset(device->update_state->viewports, 0, sizeof(device->update_state->viewports));
1925 device->update_state->viewport_count = viewport_count;
1927 /* Handle recording of state blocks */
1928 if (device->recording)
1930 TRACE("Recording... not performing anything\n");
1931 device->recording->changed.viewport = TRUE;
1932 return;
1935 wined3d_cs_emit_set_viewports(device->cs, viewport_count, viewports);
1938 void CDECL wined3d_device_get_viewports(const struct wined3d_device *device, unsigned int *viewport_count,
1939 struct wined3d_viewport *viewports)
1941 unsigned int count;
1943 TRACE("device %p, viewport_count %p, viewports %p.\n", device, viewport_count, viewports);
1945 count = viewport_count ? min(*viewport_count, device->state.viewport_count) : 1;
1946 if (count && viewports)
1947 memcpy(viewports, device->state.viewports, count * sizeof(*viewports));
1948 if (viewport_count)
1949 *viewport_count = device->state.viewport_count;
1952 static void resolve_depth_buffer(struct wined3d_device *device)
1954 const struct wined3d_state *state = &device->state;
1955 struct wined3d_rendertarget_view *src_view;
1956 struct wined3d_resource *dst_resource;
1957 struct wined3d_texture *dst_texture;
1959 if (!(dst_texture = state->textures[0]))
1960 return;
1961 dst_resource = &dst_texture->resource;
1962 if (!(dst_resource->format_flags & WINED3DFMT_FLAG_DEPTH))
1963 return;
1964 if (!(src_view = state->fb->depth_stencil))
1965 return;
1967 wined3d_device_resolve_sub_resource(device, dst_resource, 0,
1968 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1971 void CDECL wined3d_device_set_blend_state(struct wined3d_device *device, struct wined3d_blend_state *blend_state)
1973 struct wined3d_blend_state *prev;
1975 TRACE("device %p, blend_state %p.\n", device, blend_state);
1977 prev = device->update_state->blend_state;
1978 if (prev == blend_state)
1979 return;
1981 if (blend_state)
1982 wined3d_blend_state_incref(blend_state);
1983 device->update_state->blend_state = blend_state;
1984 wined3d_cs_emit_set_blend_state(device->cs, blend_state);
1985 if (prev)
1986 wined3d_blend_state_decref(prev);
1989 struct wined3d_blend_state * CDECL wined3d_device_get_blend_state(const struct wined3d_device *device)
1991 TRACE("device %p.\n", device);
1993 return device->state.blend_state;
1996 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1997 struct wined3d_rasterizer_state *rasterizer_state)
1999 struct wined3d_rasterizer_state *prev;
2001 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
2003 prev = device->update_state->rasterizer_state;
2004 if (prev == rasterizer_state)
2005 return;
2007 if (rasterizer_state)
2008 wined3d_rasterizer_state_incref(rasterizer_state);
2009 device->update_state->rasterizer_state = rasterizer_state;
2010 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
2011 if (prev)
2012 wined3d_rasterizer_state_decref(prev);
2015 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
2017 TRACE("device %p.\n", device);
2019 return device->state.rasterizer_state;
2022 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2023 enum wined3d_render_state state, DWORD value)
2025 DWORD old_value;
2027 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2029 if (state > WINEHIGHEST_RENDER_STATE)
2031 WARN("Unhandled render state %#x.\n", state);
2032 return;
2035 old_value = device->state.render_states[state];
2036 device->update_state->render_states[state] = value;
2038 /* Handle recording of state blocks. */
2039 if (device->recording)
2041 TRACE("Recording... not performing anything.\n");
2042 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2043 return;
2046 /* Compared here and not before the assignment to allow proper stateblock recording. */
2047 if (value == old_value)
2048 TRACE("Application is setting the old value over, nothing to do.\n");
2049 else
2050 wined3d_cs_emit_set_render_state(device->cs, state, value);
2052 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2054 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2055 resolve_depth_buffer(device);
2059 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2061 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2063 return device->state.render_states[state];
2066 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2067 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2069 DWORD old_value;
2071 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2072 device, sampler_idx, debug_d3dsamplerstate(state), value);
2074 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2075 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2077 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2079 WARN("Invalid sampler %u.\n", sampler_idx);
2080 return; /* Windows accepts overflowing this array ... we do not. */
2083 old_value = device->state.sampler_states[sampler_idx][state];
2084 device->update_state->sampler_states[sampler_idx][state] = value;
2086 /* Handle recording of state blocks. */
2087 if (device->recording)
2089 TRACE("Recording... not performing anything.\n");
2090 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2091 return;
2094 if (old_value == value)
2096 TRACE("Application is setting the old value over, nothing to do.\n");
2097 return;
2100 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2103 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2104 UINT sampler_idx, enum wined3d_sampler_state state)
2106 TRACE("device %p, sampler_idx %u, state %s.\n",
2107 device, sampler_idx, debug_d3dsamplerstate(state));
2109 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2110 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2112 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2114 WARN("Invalid sampler %u.\n", sampler_idx);
2115 return 0; /* Windows accepts overflowing this array ... we do not. */
2118 return device->state.sampler_states[sampler_idx][state];
2121 void CDECL wined3d_device_set_scissor_rects(struct wined3d_device *device, unsigned int rect_count,
2122 const RECT *rects)
2124 unsigned int i;
2126 TRACE("device %p, rect_count %u, rects %p.\n", device, rect_count, rects);
2128 for (i = 0; i < rect_count; ++i)
2130 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2133 if (device->recording)
2134 device->recording->changed.scissorRect = TRUE;
2136 if (device->update_state->scissor_rect_count == rect_count
2137 && !memcmp(device->update_state->scissor_rects, rects, rect_count * sizeof(*rects)))
2139 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2140 return;
2143 if (rect_count)
2144 memcpy(device->update_state->scissor_rects, rects, rect_count * sizeof(*rects));
2145 else
2146 memset(device->update_state->scissor_rects, 0, sizeof(device->update_state->scissor_rects));
2147 device->update_state->scissor_rect_count = rect_count;
2149 if (device->recording)
2151 TRACE("Recording... not performing anything.\n");
2152 return;
2155 wined3d_cs_emit_set_scissor_rects(device->cs, rect_count, rects);
2158 void CDECL wined3d_device_get_scissor_rects(const struct wined3d_device *device, unsigned int *rect_count, RECT *rects)
2160 unsigned int count;
2162 TRACE("device %p, rect_count %p, rects %p.\n", device, rect_count, rects);
2164 count = rect_count ? min(*rect_count, device->state.scissor_rect_count) : 1;
2165 if (count && rects)
2166 memcpy(rects, device->state.scissor_rects, count * sizeof(*rects));
2167 if (rect_count)
2168 *rect_count = device->state.scissor_rect_count;
2171 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2172 struct wined3d_vertex_declaration *declaration)
2174 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2176 TRACE("device %p, declaration %p.\n", device, declaration);
2178 if (device->recording)
2179 device->recording->changed.vertexDecl = TRUE;
2181 if (declaration == prev)
2182 return;
2184 if (declaration)
2185 wined3d_vertex_declaration_incref(declaration);
2186 device->update_state->vertex_declaration = declaration;
2187 if (!device->recording)
2188 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2189 if (prev)
2190 wined3d_vertex_declaration_decref(prev);
2193 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2195 TRACE("device %p.\n", device);
2197 return device->state.vertex_declaration;
2200 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2202 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2204 TRACE("device %p, shader %p.\n", device, shader);
2206 if (device->recording)
2207 device->recording->changed.vertexShader = TRUE;
2209 if (shader == prev)
2210 return;
2212 if (shader)
2213 wined3d_shader_incref(shader);
2214 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2215 if (!device->recording)
2216 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2217 if (prev)
2218 wined3d_shader_decref(prev);
2221 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2223 TRACE("device %p.\n", device);
2225 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2228 void CDECL wined3d_device_set_constant_buffer(struct wined3d_device *device,
2229 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2231 struct wined3d_buffer *prev;
2233 TRACE("device %p, type %#x, idx %u, buffer %p.\n", device, type, idx, buffer);
2235 if (idx >= MAX_CONSTANT_BUFFERS)
2237 WARN("Invalid constant buffer index %u.\n", idx);
2238 return;
2241 prev = device->update_state->cb[type][idx];
2242 if (buffer == prev)
2243 return;
2245 if (buffer)
2246 wined3d_buffer_incref(buffer);
2247 device->update_state->cb[type][idx] = buffer;
2248 if (!device->recording)
2249 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2250 if (prev)
2251 wined3d_buffer_decref(prev);
2254 struct wined3d_buffer * CDECL wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2255 enum wined3d_shader_type shader_type, unsigned int idx)
2257 TRACE("device %p, shader_type %#x, idx %u.\n", device, shader_type, idx);
2259 if (idx >= MAX_CONSTANT_BUFFERS)
2261 WARN("Invalid constant buffer index %u.\n", idx);
2262 return NULL;
2265 return device->state.cb[shader_type][idx];
2268 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2269 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2271 struct wined3d_shader_resource_view *prev;
2273 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2275 WARN("Invalid view index %u.\n", idx);
2276 return;
2279 prev = device->update_state->shader_resource_view[type][idx];
2280 if (view == prev)
2281 return;
2283 if (view)
2284 wined3d_shader_resource_view_incref(view);
2285 device->update_state->shader_resource_view[type][idx] = view;
2286 if (!device->recording)
2287 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2288 if (prev)
2289 wined3d_shader_resource_view_decref(prev);
2292 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2293 UINT idx, struct wined3d_shader_resource_view *view)
2295 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2297 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2300 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2301 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2303 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2305 WARN("Invalid view index %u.\n", idx);
2306 return NULL;
2309 return device->state.shader_resource_view[shader_type][idx];
2312 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2313 UINT idx)
2315 TRACE("device %p, idx %u.\n", device, idx);
2317 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2320 static void wined3d_device_set_sampler(struct wined3d_device *device,
2321 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2323 struct wined3d_sampler *prev;
2325 if (idx >= MAX_SAMPLER_OBJECTS)
2327 WARN("Invalid sampler index %u.\n", idx);
2328 return;
2331 prev = device->update_state->sampler[type][idx];
2332 if (sampler == prev)
2333 return;
2335 if (sampler)
2336 wined3d_sampler_incref(sampler);
2337 device->update_state->sampler[type][idx] = sampler;
2338 if (!device->recording)
2339 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2340 if (prev)
2341 wined3d_sampler_decref(prev);
2344 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2346 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2348 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2351 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2352 enum wined3d_shader_type shader_type, unsigned int idx)
2354 if (idx >= MAX_SAMPLER_OBJECTS)
2356 WARN("Invalid sampler index %u.\n", idx);
2357 return NULL;
2360 return device->state.sampler[shader_type][idx];
2363 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2365 TRACE("device %p, idx %u.\n", device, idx);
2367 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2370 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2371 unsigned int start_idx, unsigned int count, const BOOL *constants)
2373 unsigned int i;
2375 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2376 device, start_idx, count, constants);
2378 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2379 return WINED3DERR_INVALIDCALL;
2381 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2382 count = WINED3D_MAX_CONSTS_B - start_idx;
2383 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2384 if (TRACE_ON(d3d))
2386 for (i = 0; i < count; ++i)
2387 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2390 if (device->recording)
2392 for (i = start_idx; i < count + start_idx; ++i)
2393 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2395 else
2397 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2400 return WINED3D_OK;
2403 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2404 unsigned int start_idx, unsigned int count, BOOL *constants)
2406 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2407 device, start_idx, count, constants);
2409 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2410 return WINED3DERR_INVALIDCALL;
2412 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2413 count = WINED3D_MAX_CONSTS_B - start_idx;
2414 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2416 return WINED3D_OK;
2419 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2420 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2422 unsigned int i;
2424 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2425 device, start_idx, count, constants);
2427 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2428 return WINED3DERR_INVALIDCALL;
2430 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2431 count = WINED3D_MAX_CONSTS_I - start_idx;
2432 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2433 if (TRACE_ON(d3d))
2435 for (i = 0; i < count; ++i)
2436 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2439 if (device->recording)
2441 for (i = start_idx; i < count + start_idx; ++i)
2442 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2444 else
2446 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2449 return WINED3D_OK;
2452 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2453 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2455 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2456 device, start_idx, count, constants);
2458 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2459 return WINED3DERR_INVALIDCALL;
2461 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2462 count = WINED3D_MAX_CONSTS_I - start_idx;
2463 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2464 return WINED3D_OK;
2467 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2468 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2470 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2471 unsigned int i;
2473 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2474 device, start_idx, count, constants);
2476 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2477 || count > d3d_info->limits.vs_uniform_count - start_idx)
2478 return WINED3DERR_INVALIDCALL;
2480 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2481 if (TRACE_ON(d3d))
2483 for (i = 0; i < count; ++i)
2484 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2487 if (device->recording)
2488 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2489 count * sizeof(*device->recording->changed.vs_consts_f));
2490 else
2491 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2493 return WINED3D_OK;
2496 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2497 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2499 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2501 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2502 device, start_idx, count, constants);
2504 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2505 || count > d3d_info->limits.vs_uniform_count - start_idx)
2506 return WINED3DERR_INVALIDCALL;
2508 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2510 return WINED3D_OK;
2513 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2515 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2517 TRACE("device %p, shader %p.\n", device, shader);
2519 if (device->recording)
2520 device->recording->changed.pixelShader = TRUE;
2522 if (shader == prev)
2523 return;
2525 if (shader)
2526 wined3d_shader_incref(shader);
2527 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2528 if (!device->recording)
2529 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2530 if (prev)
2531 wined3d_shader_decref(prev);
2534 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2536 TRACE("device %p.\n", device);
2538 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2541 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2542 UINT idx, struct wined3d_shader_resource_view *view)
2544 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2546 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2549 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2550 UINT idx)
2552 TRACE("device %p, idx %u.\n", device, idx);
2554 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2557 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2559 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2561 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2564 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2566 TRACE("device %p, idx %u.\n", device, idx);
2568 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2571 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2572 unsigned int start_idx, unsigned int count, const BOOL *constants)
2574 unsigned int i;
2576 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2577 device, start_idx, count, constants);
2579 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2580 return WINED3DERR_INVALIDCALL;
2582 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2583 count = WINED3D_MAX_CONSTS_B - start_idx;
2584 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2585 if (TRACE_ON(d3d))
2587 for (i = 0; i < count; ++i)
2588 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2591 if (device->recording)
2593 for (i = start_idx; i < count + start_idx; ++i)
2594 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2596 else
2598 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2601 return WINED3D_OK;
2604 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2605 unsigned int start_idx, unsigned int count, BOOL *constants)
2607 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2608 device, start_idx, count, constants);
2610 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2611 return WINED3DERR_INVALIDCALL;
2613 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2614 count = WINED3D_MAX_CONSTS_B - start_idx;
2615 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2617 return WINED3D_OK;
2620 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2621 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2623 unsigned int i;
2625 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2626 device, start_idx, count, constants);
2628 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2629 return WINED3DERR_INVALIDCALL;
2631 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2632 count = WINED3D_MAX_CONSTS_I - start_idx;
2633 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2634 if (TRACE_ON(d3d))
2636 for (i = 0; i < count; ++i)
2637 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2640 if (device->recording)
2642 for (i = start_idx; i < count + start_idx; ++i)
2643 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2645 else
2647 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2650 return WINED3D_OK;
2653 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2654 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2656 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2657 device, start_idx, count, constants);
2659 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2660 return WINED3DERR_INVALIDCALL;
2662 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2663 count = WINED3D_MAX_CONSTS_I - start_idx;
2664 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2666 return WINED3D_OK;
2669 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2670 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2672 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2673 unsigned int i;
2675 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2676 device, start_idx, count, constants);
2678 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2679 || count > d3d_info->limits.ps_uniform_count - start_idx)
2680 return WINED3DERR_INVALIDCALL;
2682 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2683 if (TRACE_ON(d3d))
2685 for (i = 0; i < count; ++i)
2686 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2689 if (device->recording)
2690 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2691 count * sizeof(*device->recording->changed.ps_consts_f));
2692 else
2693 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2695 return WINED3D_OK;
2698 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2699 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2701 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2703 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2704 device, start_idx, count, constants);
2706 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2707 || count > d3d_info->limits.ps_uniform_count - start_idx)
2708 return WINED3DERR_INVALIDCALL;
2710 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2712 return WINED3D_OK;
2715 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2717 struct wined3d_shader *prev;
2719 TRACE("device %p, shader %p.\n", device, shader);
2721 prev = device->update_state->shader[WINED3D_SHADER_TYPE_HULL];
2722 if (shader == prev)
2723 return;
2724 if (shader)
2725 wined3d_shader_incref(shader);
2726 device->update_state->shader[WINED3D_SHADER_TYPE_HULL] = shader;
2727 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_HULL, shader);
2728 if (prev)
2729 wined3d_shader_decref(prev);
2732 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2734 TRACE("device %p.\n", device);
2736 return device->state.shader[WINED3D_SHADER_TYPE_HULL];
2739 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2740 unsigned int idx, struct wined3d_shader_resource_view *view)
2742 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2744 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx, view);
2747 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2748 unsigned int idx)
2750 TRACE("device %p, idx %u.\n", device, idx);
2752 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2755 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2756 unsigned int idx, struct wined3d_sampler *sampler)
2758 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2760 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2763 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2765 TRACE("device %p, idx %u.\n", device, idx);
2767 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2770 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2772 struct wined3d_shader *prev;
2774 TRACE("device %p, shader %p.\n", device, shader);
2776 prev = device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2777 if (shader == prev)
2778 return;
2779 if (shader)
2780 wined3d_shader_incref(shader);
2781 device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN] = shader;
2782 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_DOMAIN, shader);
2783 if (prev)
2784 wined3d_shader_decref(prev);
2787 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2789 TRACE("device %p.\n", device);
2791 return device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2794 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2795 unsigned int idx, struct wined3d_shader_resource_view *view)
2797 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2799 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2802 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2803 unsigned int idx)
2805 TRACE("device %p, idx %u.\n", device, idx);
2807 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2810 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2811 unsigned int idx, struct wined3d_sampler *sampler)
2813 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2815 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2818 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2820 TRACE("device %p, idx %u.\n", device, idx);
2822 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2825 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2827 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2829 TRACE("device %p, shader %p.\n", device, shader);
2831 if (device->recording || shader == prev)
2832 return;
2833 if (shader)
2834 wined3d_shader_incref(shader);
2835 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2836 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2837 if (prev)
2838 wined3d_shader_decref(prev);
2841 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2843 TRACE("device %p.\n", device);
2845 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2848 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2849 UINT idx, struct wined3d_shader_resource_view *view)
2851 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2853 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2856 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2857 UINT idx)
2859 TRACE("device %p, idx %u.\n", device, idx);
2861 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2864 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2866 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2868 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2871 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2873 TRACE("device %p, idx %u.\n", device, idx);
2875 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2878 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2880 struct wined3d_shader *prev;
2882 TRACE("device %p, shader %p.\n", device, shader);
2884 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2885 if (device->recording || shader == prev)
2886 return;
2887 if (shader)
2888 wined3d_shader_incref(shader);
2889 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2890 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2891 if (prev)
2892 wined3d_shader_decref(prev);
2895 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2897 TRACE("device %p.\n", device);
2899 return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2902 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2903 unsigned int idx, struct wined3d_shader_resource_view *view)
2905 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2907 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2910 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2911 unsigned int idx)
2913 TRACE("device %p, idx %u.\n", device, idx);
2915 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2918 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2919 unsigned int idx, struct wined3d_sampler *sampler)
2921 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2923 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2926 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2928 TRACE("device %p, idx %u.\n", device, idx);
2930 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2933 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2934 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2935 unsigned int initial_count)
2937 struct wined3d_unordered_access_view *prev;
2939 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2941 WARN("Invalid UAV index %u.\n", idx);
2942 return;
2945 prev = device->update_state->unordered_access_view[pipeline][idx];
2946 if (uav == prev && initial_count == ~0u)
2947 return;
2949 if (uav)
2950 wined3d_unordered_access_view_incref(uav);
2951 device->update_state->unordered_access_view[pipeline][idx] = uav;
2952 if (!device->recording)
2953 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav, initial_count);
2954 if (prev)
2955 wined3d_unordered_access_view_decref(prev);
2958 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2959 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2961 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2963 WARN("Invalid UAV index %u.\n", idx);
2964 return NULL;
2967 return device->state.unordered_access_view[pipeline][idx];
2970 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2971 struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2973 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2975 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
2978 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
2979 unsigned int idx)
2981 TRACE("device %p, idx %u.\n", device, idx);
2983 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
2986 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2987 unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2989 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2991 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
2994 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
2995 const struct wined3d_device *device, unsigned int idx)
2997 TRACE("device %p, idx %u.\n", device, idx);
2999 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
3002 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
3004 unsigned int i;
3006 if (!latency)
3007 latency = 3;
3009 device->max_frame_latency = latency;
3010 for (i = 0; i < device->swapchain_count; ++i)
3011 swapchain_set_max_frame_latency(device->swapchains[i], device);
3014 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
3016 return device->max_frame_latency;
3019 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
3021 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3022 unsigned int i, size = 0;
3024 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
3025 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
3026 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
3027 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
3028 switch (fvf & WINED3DFVF_POSITION_MASK)
3030 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
3031 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
3032 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
3033 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
3034 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
3035 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
3036 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
3037 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
3038 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
3040 for (i = 0; i < texcoord_count; ++i)
3042 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
3045 return size;
3048 /* Context activation is done by the caller. */
3049 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3050 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3051 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3053 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3054 struct wined3d_map_desc map_desc;
3055 struct wined3d_box box = {0};
3056 struct wined3d_viewport vp;
3057 unsigned int vertex_size;
3058 unsigned int i;
3059 BYTE *dest_ptr;
3060 BOOL doClip;
3061 DWORD numTextures;
3062 HRESULT hr;
3064 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3066 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3069 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3071 ERR("Source has no position mask\n");
3072 return WINED3DERR_INVALIDCALL;
3075 if (device->state.render_states[WINED3D_RS_CLIPPING])
3077 static BOOL warned = FALSE;
3079 * The clipping code is not quite correct. Some things need
3080 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3081 * so disable clipping for now.
3082 * (The graphics in Half-Life are broken, and my processvertices
3083 * test crashes with IDirect3DDevice3)
3084 doClip = TRUE;
3086 doClip = FALSE;
3087 if(!warned) {
3088 warned = TRUE;
3089 FIXME("Clipping is broken and disabled for now\n");
3092 else
3093 doClip = FALSE;
3095 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3096 box.left = dwDestIndex * vertex_size;
3097 box.right = box.left + dwCount * vertex_size;
3098 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3100 WARN("Failed to map buffer, hr %#x.\n", hr);
3101 return hr;
3103 dest_ptr = map_desc.data;
3105 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3106 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3107 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3109 TRACE("View mat:\n");
3110 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3111 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3112 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3113 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3115 TRACE("Proj mat:\n");
3116 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3117 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3118 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3119 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3121 TRACE("World mat:\n");
3122 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3123 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3124 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3125 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3127 /* Get the viewport */
3128 wined3d_device_get_viewports(device, NULL, &vp);
3129 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3130 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3132 multiply_matrix(&mat,&view_mat,&world_mat);
3133 multiply_matrix(&mat,&proj_mat,&mat);
3135 numTextures = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3137 for (i = 0; i < dwCount; i+= 1) {
3138 unsigned int tex_index;
3140 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3141 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3142 /* The position first */
3143 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3144 const float *p = (const float *)(element->data.addr + i * element->stride);
3145 float x, y, z, rhw;
3146 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3148 /* Multiplication with world, view and projection matrix. */
3149 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3150 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3151 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3152 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3154 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3156 /* WARNING: The following things are taken from d3d7 and were not yet checked
3157 * against d3d8 or d3d9!
3160 /* Clipping conditions: From msdn
3162 * A vertex is clipped if it does not match the following requirements
3163 * -rhw < x <= rhw
3164 * -rhw < y <= rhw
3165 * 0 < z <= rhw
3166 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3168 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3169 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3173 if( !doClip ||
3174 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3175 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3176 ( rhw > eps ) ) ) {
3178 /* "Normal" viewport transformation (not clipped)
3179 * 1) The values are divided by rhw
3180 * 2) The y axis is negative, so multiply it with -1
3181 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3182 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3183 * 4) Multiply x with Width/2 and add Width/2
3184 * 5) The same for the height
3185 * 6) Add the viewpoint X and Y to the 2D coordinates and
3186 * The minimum Z value to z
3187 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3189 * Well, basically it's simply a linear transformation into viewport
3190 * coordinates
3193 x /= rhw;
3194 y /= rhw;
3195 z /= rhw;
3197 y *= -1;
3199 x *= vp.width / 2;
3200 y *= vp.height / 2;
3201 z *= vp.max_z - vp.min_z;
3203 x += vp.width / 2 + vp.x;
3204 y += vp.height / 2 + vp.y;
3205 z += vp.min_z;
3207 rhw = 1 / rhw;
3208 } else {
3209 /* That vertex got clipped
3210 * Contrary to OpenGL it is not dropped completely, it just
3211 * undergoes a different calculation.
3213 TRACE("Vertex got clipped\n");
3214 x += rhw;
3215 y += rhw;
3217 x /= 2;
3218 y /= 2;
3220 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3221 * outside of the main vertex buffer memory. That needs some more
3222 * investigation...
3226 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3229 ( (float *) dest_ptr)[0] = x;
3230 ( (float *) dest_ptr)[1] = y;
3231 ( (float *) dest_ptr)[2] = z;
3232 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3234 dest_ptr += 3 * sizeof(float);
3236 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3237 dest_ptr += sizeof(float);
3240 if (dst_fvf & WINED3DFVF_PSIZE)
3241 dest_ptr += sizeof(DWORD);
3243 if (dst_fvf & WINED3DFVF_NORMAL)
3245 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3246 const float *normal = (const float *)(element->data.addr + i * element->stride);
3247 /* AFAIK this should go into the lighting information */
3248 FIXME("Didn't expect the destination to have a normal\n");
3249 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3252 if (dst_fvf & WINED3DFVF_DIFFUSE)
3254 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3255 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3256 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3258 static BOOL warned = FALSE;
3260 if(!warned) {
3261 ERR("No diffuse color in source, but destination has one\n");
3262 warned = TRUE;
3265 *( (DWORD *) dest_ptr) = 0xffffffff;
3266 dest_ptr += sizeof(DWORD);
3268 else
3270 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3274 if (dst_fvf & WINED3DFVF_SPECULAR)
3276 /* What's the color value in the feedback buffer? */
3277 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3278 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3279 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3281 static BOOL warned = FALSE;
3283 if(!warned) {
3284 ERR("No specular color in source, but destination has one\n");
3285 warned = TRUE;
3288 *(DWORD *)dest_ptr = 0xff000000;
3289 dest_ptr += sizeof(DWORD);
3291 else
3293 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3297 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3299 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3300 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3301 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3303 ERR("No source texture, but destination requests one\n");
3304 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3306 else
3308 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3313 wined3d_resource_unmap(&dest->resource, 0);
3315 return WINED3D_OK;
3317 #undef copy_and_next
3319 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3320 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3321 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3323 struct wined3d_state *state = &device->state;
3324 struct wined3d_stream_info stream_info;
3325 struct wined3d_resource *resource;
3326 struct wined3d_box box = {0};
3327 struct wined3d_shader *vs;
3328 unsigned int i;
3329 HRESULT hr;
3330 WORD map;
3332 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3333 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3334 device, src_start_idx, dst_idx, vertex_count,
3335 dst_buffer, declaration, flags, dst_fvf);
3337 if (declaration)
3338 FIXME("Output vertex declaration not implemented yet.\n");
3340 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3341 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3342 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->gl_info, &device->adapter->d3d_info);
3343 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3345 /* We can't convert FROM a VBO, and vertex buffers used to source into
3346 * process_vertices() are unlikely to ever be used for drawing. Release
3347 * VBOs in those buffers and fix up the stream_info structure.
3349 * Also apply the start index. */
3350 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3352 struct wined3d_stream_info_element *e;
3353 struct wined3d_map_desc map_desc;
3355 if (!(map & 1))
3356 continue;
3358 e = &stream_info.elements[i];
3359 resource = &state->streams[e->stream_idx].buffer->resource;
3360 box.left = src_start_idx * e->stride;
3361 box.right = box.left + vertex_count * e->stride;
3362 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3363 ERR("Failed to map resource.\n");
3364 e->data.buffer_object = 0;
3365 e->data.addr += (ULONG_PTR)map_desc.data;
3368 hr = process_vertices_strided(device, dst_idx, vertex_count,
3369 &stream_info, dst_buffer, flags, dst_fvf);
3371 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3373 if (!(map & 1))
3374 continue;
3376 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3377 if (FAILED(wined3d_resource_unmap(resource, 0)))
3378 ERR("Failed to unmap resource.\n");
3381 return hr;
3384 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3385 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3387 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3388 DWORD old_value;
3390 TRACE("device %p, stage %u, state %s, value %#x.\n",
3391 device, stage, debug_d3dtexturestate(state), value);
3393 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3395 WARN("Invalid state %#x passed.\n", state);
3396 return;
3399 if (stage >= d3d_info->limits.ffp_blend_stages)
3401 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3402 stage, d3d_info->limits.ffp_blend_stages - 1);
3403 return;
3406 old_value = device->update_state->texture_states[stage][state];
3407 device->update_state->texture_states[stage][state] = value;
3409 if (device->recording)
3411 TRACE("Recording... not performing anything.\n");
3412 device->recording->changed.textureState[stage] |= 1u << state;
3413 return;
3416 /* Checked after the assignments to allow proper stateblock recording. */
3417 if (old_value == value)
3419 TRACE("Application is setting the old value over, nothing to do.\n");
3420 return;
3423 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3426 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3427 UINT stage, enum wined3d_texture_stage_state state)
3429 TRACE("device %p, stage %u, state %s.\n",
3430 device, stage, debug_d3dtexturestate(state));
3432 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3434 WARN("Invalid state %#x passed.\n", state);
3435 return 0;
3438 return device->state.texture_states[stage][state];
3441 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3442 UINT stage, struct wined3d_texture *texture)
3444 struct wined3d_texture *prev;
3446 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3448 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3449 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3451 /* Windows accepts overflowing this array... we do not. */
3452 if (stage >= ARRAY_SIZE(device->state.textures))
3454 WARN("Ignoring invalid stage %u.\n", stage);
3455 return WINED3D_OK;
3458 if (texture && texture->resource.usage & WINED3DUSAGE_SCRATCH)
3460 WARN("Rejecting attempt to set scratch texture.\n");
3461 return WINED3DERR_INVALIDCALL;
3464 if (device->recording)
3465 device->recording->changed.textures |= 1u << stage;
3467 prev = device->update_state->textures[stage];
3468 TRACE("Previous texture %p.\n", prev);
3470 if (texture == prev)
3472 TRACE("App is setting the same texture again, nothing to do.\n");
3473 return WINED3D_OK;
3476 TRACE("Setting new texture to %p.\n", texture);
3477 device->update_state->textures[stage] = texture;
3479 if (texture)
3480 wined3d_texture_incref(texture);
3481 if (!device->recording)
3482 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3483 if (prev)
3484 wined3d_texture_decref(prev);
3486 return WINED3D_OK;
3489 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3491 TRACE("device %p, stage %u.\n", device, stage);
3493 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3494 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3496 if (stage >= ARRAY_SIZE(device->state.textures))
3498 WARN("Ignoring invalid stage %u.\n", stage);
3499 return NULL; /* Windows accepts overflowing this array ... we do not. */
3502 return device->state.textures[stage];
3505 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
3507 TRACE("device %p, caps %p.\n", device, caps);
3509 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3510 device->create_parms.device_type, caps);
3513 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3514 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3516 struct wined3d_swapchain *swapchain;
3518 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3519 device, swapchain_idx, mode, rotation);
3521 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3522 return WINED3DERR_INVALIDCALL;
3524 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3527 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3529 struct wined3d_stateblock *stateblock;
3530 HRESULT hr;
3532 TRACE("device %p.\n", device);
3534 if (device->recording)
3535 return WINED3DERR_INVALIDCALL;
3537 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3538 if (FAILED(hr))
3539 return hr;
3541 device->recording = stateblock;
3542 device->update_state = &stateblock->state;
3544 TRACE("Recording stateblock %p.\n", stateblock);
3546 return WINED3D_OK;
3549 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3550 struct wined3d_stateblock **stateblock)
3552 struct wined3d_stateblock *object = device->recording;
3554 TRACE("device %p, stateblock %p.\n", device, stateblock);
3556 if (!device->recording)
3558 WARN("Not recording.\n");
3559 *stateblock = NULL;
3560 return WINED3DERR_INVALIDCALL;
3563 stateblock_init_contained_states(object);
3565 *stateblock = object;
3566 device->recording = NULL;
3567 device->update_state = &device->state;
3569 TRACE("Returning stateblock %p.\n", *stateblock);
3571 return WINED3D_OK;
3574 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3576 /* At the moment we have no need for any functionality at the beginning
3577 * of a scene. */
3578 TRACE("device %p.\n", device);
3580 if (device->inScene)
3582 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3583 return WINED3DERR_INVALIDCALL;
3585 device->inScene = TRUE;
3586 return WINED3D_OK;
3589 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3591 TRACE("device %p.\n", device);
3593 if (!device->inScene)
3595 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3596 return WINED3DERR_INVALIDCALL;
3599 device->inScene = FALSE;
3600 return WINED3D_OK;
3603 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3604 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3606 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3607 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3609 if (!rect_count && rects)
3611 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3612 return WINED3D_OK;
3615 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3617 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3618 if (!ds)
3620 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3621 /* TODO: What about depth stencil buffers without stencil bits? */
3622 return WINED3DERR_INVALIDCALL;
3624 else if (flags & WINED3DCLEAR_TARGET)
3626 if (ds->width < device->fb.render_targets[0]->width
3627 || ds->height < device->fb.render_targets[0]->height)
3629 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3630 return WINED3D_OK;
3635 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3637 return WINED3D_OK;
3640 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3641 struct wined3d_query *predicate, BOOL value)
3643 struct wined3d_query *prev;
3645 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3647 prev = device->update_state->predicate;
3648 if (predicate)
3650 FIXME("Predicated rendering not implemented.\n");
3651 wined3d_query_incref(predicate);
3653 device->update_state->predicate = predicate;
3654 device->update_state->predicate_value = value;
3655 if (!device->recording)
3656 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3657 if (prev)
3658 wined3d_query_decref(prev);
3661 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3663 TRACE("device %p, value %p.\n", device, value);
3665 if (value)
3666 *value = device->state.predicate_value;
3667 return device->state.predicate;
3670 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3671 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3673 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3674 device, group_count_x, group_count_y, group_count_z);
3676 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3679 void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
3680 struct wined3d_buffer *buffer, unsigned int offset)
3682 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3684 wined3d_cs_emit_dispatch_indirect(device->cs, buffer, offset);
3687 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3688 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
3690 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
3691 device, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
3693 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3694 device->state.gl_patch_vertices = patch_vertex_count;
3697 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3698 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
3700 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
3701 device, primitive_type, patch_vertex_count);
3703 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3704 if (patch_vertex_count)
3705 *patch_vertex_count = device->state.gl_patch_vertices;
3707 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
3710 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3712 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3714 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3715 0, start_vertex, vertex_count, 0, 0, FALSE);
3717 return WINED3D_OK;
3720 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3721 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3723 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3724 device, start_vertex, vertex_count, start_instance, instance_count);
3726 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3727 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3730 void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
3731 struct wined3d_buffer *buffer, unsigned int offset)
3733 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3735 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3736 buffer, offset, FALSE);
3739 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3741 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3743 if (!device->state.index_buffer)
3745 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3746 * without an index buffer set. (The first time at least...)
3747 * D3D8 simply dies, but I doubt it can do much harm to return
3748 * D3DERR_INVALIDCALL there as well. */
3749 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3750 return WINED3DERR_INVALIDCALL;
3753 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3754 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3756 return WINED3D_OK;
3759 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3760 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3762 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3763 device, start_idx, index_count, start_instance, instance_count);
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, start_instance, instance_count, TRUE);
3769 void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
3770 struct wined3d_buffer *buffer, unsigned int offset)
3772 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3774 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3775 buffer, offset, TRUE);
3778 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3779 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3781 unsigned int src_size, dst_size, src_skip_levels = 0;
3782 unsigned int src_level_count, dst_level_count;
3783 unsigned int layer_count, level_count, i, j;
3784 unsigned int width, height, depth;
3785 enum wined3d_resource_type type;
3786 struct wined3d_box box;
3788 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3790 /* Verify that the source and destination textures are non-NULL. */
3791 if (!src_texture || !dst_texture)
3793 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3794 return WINED3DERR_INVALIDCALL;
3797 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
3798 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
3800 WARN("Source resource is GPU accessible or a scratch resource.\n");
3801 return WINED3DERR_INVALIDCALL;
3803 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
3805 WARN("Destination resource is CPU accessible.\n");
3806 return WINED3DERR_INVALIDCALL;
3809 /* Verify that the source and destination textures are the same type. */
3810 type = src_texture->resource.type;
3811 if (dst_texture->resource.type != type)
3813 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3814 return WINED3DERR_INVALIDCALL;
3817 layer_count = src_texture->layer_count;
3818 if (layer_count != dst_texture->layer_count)
3820 WARN("Source and destination have different layer counts.\n");
3821 return WINED3DERR_INVALIDCALL;
3824 if (src_texture->resource.format != dst_texture->resource.format)
3826 WARN("Source and destination formats do not match.\n");
3827 return WINED3DERR_INVALIDCALL;
3830 src_level_count = src_texture->level_count;
3831 dst_level_count = dst_texture->level_count;
3832 level_count = min(src_level_count, dst_level_count);
3834 src_size = max(src_texture->resource.width, src_texture->resource.height);
3835 src_size = max(src_size, src_texture->resource.depth);
3836 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3837 dst_size = max(dst_size, dst_texture->resource.depth);
3838 while (src_size > dst_size)
3840 src_size >>= 1;
3841 ++src_skip_levels;
3844 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3845 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3846 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3848 WARN("Source and destination dimensions do not match.\n");
3849 return WINED3DERR_INVALIDCALL;
3852 /* Update every surface level of the texture. */
3853 for (i = 0; i < level_count; ++i)
3855 width = wined3d_texture_get_level_width(dst_texture, i);
3856 height = wined3d_texture_get_level_height(dst_texture, i);
3857 depth = wined3d_texture_get_level_depth(dst_texture, i);
3858 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3860 for (j = 0; j < layer_count; ++j)
3862 wined3d_cs_emit_blt_sub_resource(device->cs,
3863 &dst_texture->resource, j * dst_level_count + i, &box,
3864 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3865 0, NULL, WINED3D_TEXF_POINT);
3869 return WINED3D_OK;
3872 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3874 const struct wined3d_state *state = &device->state;
3875 struct wined3d_texture *texture;
3876 DWORD i;
3878 TRACE("device %p, num_passes %p.\n", device, num_passes);
3880 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3882 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3884 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3885 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3887 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3889 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3890 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3893 texture = state->textures[i];
3894 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3896 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3898 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3899 return E_FAIL;
3901 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3903 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3904 return E_FAIL;
3906 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3907 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3909 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3910 return E_FAIL;
3914 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3915 || state->render_states[WINED3D_RS_STENCILENABLE])
3917 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3918 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3920 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3922 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3923 return WINED3DERR_CONFLICTINGRENDERSTATE;
3927 /* return a sensible default */
3928 *num_passes = 1;
3930 TRACE("returning D3D_OK\n");
3931 return WINED3D_OK;
3934 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3936 static BOOL warned;
3938 TRACE("device %p, software %#x.\n", device, software);
3940 if (!warned)
3942 FIXME("device %p, software %#x stub!\n", device, software);
3943 warned = TRUE;
3946 device->softwareVertexProcessing = software;
3949 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3951 static BOOL warned;
3953 TRACE("device %p.\n", device);
3955 if (!warned)
3957 TRACE("device %p stub!\n", device);
3958 warned = TRUE;
3961 return device->softwareVertexProcessing;
3964 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3965 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3967 struct wined3d_swapchain *swapchain;
3969 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3970 device, swapchain_idx, raster_status);
3972 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3973 return WINED3DERR_INVALIDCALL;
3975 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3978 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3980 static BOOL warned;
3982 TRACE("device %p, segments %.8e.\n", device, segments);
3984 if (segments != 0.0f)
3986 if (!warned)
3988 FIXME("device %p, segments %.8e stub!\n", device, segments);
3989 warned = TRUE;
3993 return WINED3D_OK;
3996 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3998 static BOOL warned;
4000 TRACE("device %p.\n", device);
4002 if (!warned)
4004 FIXME("device %p stub!\n", device);
4005 warned = TRUE;
4008 return 0.0f;
4011 void CDECL wined3d_device_copy_uav_counter(struct wined3d_device *device,
4012 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4014 TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
4015 device, dst_buffer, offset, uav);
4017 wined3d_cs_emit_copy_uav_counter(device->cs, dst_buffer, offset, uav);
4020 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
4021 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4023 struct wined3d_texture *dst_texture, *src_texture;
4024 struct wined3d_box box;
4025 unsigned int i, j;
4027 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
4029 if (src_resource == dst_resource)
4031 WARN("Source and destination are the same resource.\n");
4032 return;
4035 if (src_resource->type != dst_resource->type)
4037 WARN("Resource types (%s / %s) don't match.\n",
4038 debug_d3dresourcetype(dst_resource->type),
4039 debug_d3dresourcetype(src_resource->type));
4040 return;
4043 if (src_resource->width != dst_resource->width
4044 || src_resource->height != dst_resource->height
4045 || src_resource->depth != dst_resource->depth)
4047 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4048 dst_resource->width, dst_resource->height, dst_resource->depth,
4049 src_resource->width, src_resource->height, src_resource->depth);
4050 return;
4053 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4054 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4056 WARN("Resource formats %s and %s are incompatible.\n",
4057 debug_d3dformat(dst_resource->format->id),
4058 debug_d3dformat(src_resource->format->id));
4059 return;
4062 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4064 wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
4065 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
4066 src_resource, 0, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4067 return;
4070 dst_texture = texture_from_resource(dst_resource);
4071 src_texture = texture_from_resource(src_resource);
4073 if (src_texture->layer_count != dst_texture->layer_count
4074 || src_texture->level_count != dst_texture->level_count)
4076 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4077 dst_texture->layer_count, dst_texture->level_count,
4078 src_texture->layer_count, src_texture->level_count);
4079 return;
4082 for (i = 0; i < dst_texture->level_count; ++i)
4084 wined3d_box_set(&box, 0, 0,
4085 wined3d_texture_get_level_width(dst_texture, i),
4086 wined3d_texture_get_level_height(dst_texture, i),
4087 0, wined3d_texture_get_level_depth(dst_texture, i));
4088 for (j = 0; j < dst_texture->layer_count; ++j)
4090 unsigned int idx = j * dst_texture->level_count + i;
4092 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
4093 src_resource, idx, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4098 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
4099 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4100 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4101 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4103 struct wined3d_box dst_box, b;
4105 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4106 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4107 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4108 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4110 if (flags)
4111 FIXME("Ignoring flags %#x.\n", flags);
4113 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4115 WARN("Source and destination are the same sub-resource.\n");
4116 return WINED3DERR_INVALIDCALL;
4119 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4120 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4122 WARN("Resource formats %s and %s are incompatible.\n",
4123 debug_d3dformat(dst_resource->format->id),
4124 debug_d3dformat(src_resource->format->id));
4125 return WINED3DERR_INVALIDCALL;
4128 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4130 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4132 WARN("Resource types (%s / %s) don't match.\n",
4133 debug_d3dresourcetype(dst_resource->type),
4134 debug_d3dresourcetype(src_resource->type));
4135 return WINED3DERR_INVALIDCALL;
4138 if (dst_sub_resource_idx)
4140 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4141 return WINED3DERR_INVALIDCALL;
4144 if (src_sub_resource_idx)
4146 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4147 return WINED3DERR_INVALIDCALL;
4150 if (!src_box)
4152 unsigned int dst_w;
4154 dst_w = dst_resource->size - dst_x;
4155 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4156 src_box = &b;
4158 else if ((src_box->left >= src_box->right
4159 || src_box->top >= src_box->bottom
4160 || src_box->front >= src_box->back))
4162 WARN("Invalid box %s specified.\n", debug_box(src_box));
4163 return WINED3DERR_INVALIDCALL;
4166 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4167 || src_box->right - src_box->left > dst_resource->size - dst_x)
4169 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4170 dst_x, src_box->left, src_box->right - src_box->left);
4171 return WINED3DERR_INVALIDCALL;
4174 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4176 else
4178 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4179 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4180 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4182 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4184 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4185 return WINED3DERR_INVALIDCALL;
4188 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4190 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4191 return WINED3DERR_INVALIDCALL;
4194 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4196 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4197 return WINED3DERR_INVALIDCALL;
4200 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4202 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4203 return WINED3DERR_INVALIDCALL;
4206 if (!src_box)
4208 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4210 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4211 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4212 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4214 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4215 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4216 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4217 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4219 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4220 src_box = &b;
4222 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4224 WARN("Invalid source box %s.\n", debug_box(src_box));
4225 return WINED3DERR_INVALIDCALL;
4228 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4229 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4230 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4231 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4233 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4234 return WINED3DERR_INVALIDCALL;
4238 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4239 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4241 return WINED3D_OK;
4244 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4245 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4246 unsigned int depth_pitch, unsigned int flags)
4248 unsigned int width, height, depth;
4249 struct wined3d_box b;
4251 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, "
4252 "flags %#x.\n",
4253 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4255 if (flags)
4256 FIXME("Ignoring flags %#x.\n", flags);
4258 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4260 WARN("Resource %p is not GPU accessible.\n", resource);
4261 return;
4264 if (resource->type == WINED3D_RTYPE_BUFFER)
4266 if (sub_resource_idx > 0)
4268 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4269 return;
4272 width = resource->size;
4273 height = 1;
4274 depth = 1;
4276 else
4278 struct wined3d_texture *texture = texture_from_resource(resource);
4279 unsigned int level;
4281 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4283 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4284 return;
4287 level = sub_resource_idx % texture->level_count;
4288 width = wined3d_texture_get_level_width(texture, level);
4289 height = wined3d_texture_get_level_height(texture, level);
4290 depth = wined3d_texture_get_level_depth(texture, level);
4293 if (!box)
4295 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4296 box = &b;
4298 else if (box->left >= box->right || box->right > width
4299 || box->top >= box->bottom || box->bottom > height
4300 || box->front >= box->back || box->back > depth)
4302 WARN("Invalid box %s specified.\n", debug_box(box));
4303 return;
4306 wined3d_resource_wait_idle(resource);
4308 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4311 void CDECL wined3d_device_resolve_sub_resource(struct wined3d_device *device,
4312 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4313 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4314 enum wined3d_format_id format_id)
4316 struct wined3d_texture *dst_texture, *src_texture;
4317 unsigned int dst_level, src_level;
4318 RECT dst_rect, src_rect;
4320 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, "
4321 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4322 device, dst_resource, dst_sub_resource_idx,
4323 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4325 if (wined3d_format_is_typeless(dst_resource->format)
4326 || wined3d_format_is_typeless(src_resource->format))
4328 FIXME("Multisample resolve is not fully supported for typeless formats "
4329 "(dst_format %s, src_format %s, format %s).\n",
4330 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4331 debug_d3dformat(format_id));
4333 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4335 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4336 return;
4338 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4340 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4341 return;
4344 dst_texture = texture_from_resource(dst_resource);
4345 src_texture = texture_from_resource(src_resource);
4347 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4348 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4349 wined3d_texture_get_level_height(dst_texture, dst_level));
4350 src_level = src_sub_resource_idx % src_texture->level_count;
4351 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4352 wined3d_texture_get_level_height(src_texture, src_level));
4353 wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4354 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
4357 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4358 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4359 const struct wined3d_color *color, float depth, DWORD stencil)
4361 struct wined3d_resource *resource;
4362 RECT r;
4364 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4365 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4367 if (!flags)
4368 return WINED3D_OK;
4370 resource = view->resource;
4371 if (resource->type == WINED3D_RTYPE_BUFFER)
4373 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4374 return WINED3DERR_INVALIDCALL;
4377 if (view->layer_count != max(1, resource->depth >> view->desc.u.texture.level_idx))
4379 FIXME("Layered clears not implemented.\n");
4380 return WINED3DERR_INVALIDCALL;
4383 if (!rect)
4385 SetRect(&r, 0, 0, view->width, view->height);
4386 rect = &r;
4388 else
4390 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4391 struct wined3d_texture *texture = texture_from_resource(view->resource);
4392 HRESULT hr;
4394 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4395 view->sub_resource_idx % texture->level_count, &b)))
4396 return hr;
4399 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4401 return WINED3D_OK;
4404 void CDECL wined3d_device_clear_unordered_access_view_uint(struct wined3d_device *device,
4405 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4407 TRACE("device %p, view %p, clear_value %s.\n", device, view, debug_uvec4(clear_value));
4409 wined3d_cs_emit_clear_unordered_access_view_uint(device->cs, view, clear_value);
4412 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4413 unsigned int view_idx)
4415 unsigned int max_rt_count;
4417 TRACE("device %p, view_idx %u.\n", device, view_idx);
4419 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
4420 if (view_idx >= max_rt_count)
4422 WARN("Only %u render targets are supported.\n", max_rt_count);
4423 return NULL;
4426 return device->fb.render_targets[view_idx];
4429 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4431 TRACE("device %p.\n", device);
4433 return device->fb.depth_stencil;
4436 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4437 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4439 struct wined3d_rendertarget_view *prev;
4440 unsigned int max_rt_count;
4442 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4443 device, view_idx, view, set_viewport);
4445 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
4446 if (view_idx >= max_rt_count)
4448 WARN("Only %u render targets are supported.\n", max_rt_count);
4449 return WINED3DERR_INVALIDCALL;
4452 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4454 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4455 return WINED3DERR_INVALIDCALL;
4458 /* Set the viewport and scissor rectangles, if requested. Tests show that
4459 * stateblock recording is ignored, the change goes directly into the
4460 * primary stateblock. */
4461 if (!view_idx && set_viewport)
4463 struct wined3d_state *state = &device->state;
4465 state->viewports[0].x = 0;
4466 state->viewports[0].y = 0;
4467 state->viewports[0].width = view->width;
4468 state->viewports[0].height = view->height;
4469 state->viewports[0].min_z = 0.0f;
4470 state->viewports[0].max_z = 1.0f;
4471 state->viewport_count = 1;
4472 wined3d_cs_emit_set_viewports(device->cs, 1, state->viewports);
4474 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
4475 state->scissor_rect_count = 1;
4476 wined3d_cs_emit_set_scissor_rects(device->cs, 1, state->scissor_rects);
4479 prev = device->fb.render_targets[view_idx];
4480 if (view == prev)
4481 return WINED3D_OK;
4483 if (view)
4484 wined3d_rendertarget_view_incref(view);
4485 device->fb.render_targets[view_idx] = view;
4486 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4487 /* Release after the assignment, to prevent device_resource_released()
4488 * from seeing the surface as still in use. */
4489 if (prev)
4490 wined3d_rendertarget_view_decref(prev);
4492 return WINED3D_OK;
4495 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4497 struct wined3d_rendertarget_view *prev;
4499 TRACE("device %p, view %p.\n", device, view);
4501 prev = device->fb.depth_stencil;
4502 if (prev == view)
4504 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4505 return;
4508 if ((device->fb.depth_stencil = view))
4509 wined3d_rendertarget_view_incref(view);
4510 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4511 if (prev)
4512 wined3d_rendertarget_view_decref(prev);
4515 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4516 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4518 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4519 struct wined3d_sub_resource_data data;
4520 struct wined3d_resource_desc desc;
4521 struct wined3d_map_desc map_desc;
4522 struct wined3d_texture *texture;
4523 HRESULT hr;
4525 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4527 ERR("Failed to map source texture.\n");
4528 return NULL;
4531 data.data = map_desc.data;
4532 data.row_pitch = map_desc.row_pitch;
4533 data.slice_pitch = map_desc.slice_pitch;
4535 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4536 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4537 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4538 desc.multisample_quality = 0;
4539 desc.usage = WINED3DUSAGE_DYNAMIC;
4540 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4541 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4542 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4543 desc.depth = 1;
4544 desc.size = 0;
4546 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4547 &data, NULL, &wined3d_null_parent_ops, &texture);
4548 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4549 if (FAILED(hr))
4551 ERR("Failed to create cursor texture.\n");
4552 return NULL;
4555 return texture;
4558 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4559 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4561 unsigned int texture_level = sub_resource_idx % texture->level_count;
4562 unsigned int cursor_width, cursor_height;
4563 struct wined3d_display_mode mode;
4564 struct wined3d_map_desc map_desc;
4565 HRESULT hr;
4567 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4568 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4570 if (sub_resource_idx >= texture->level_count * texture->layer_count
4571 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4572 return WINED3DERR_INVALIDCALL;
4574 if (device->cursor_texture)
4576 wined3d_texture_decref(device->cursor_texture);
4577 device->cursor_texture = NULL;
4580 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4582 WARN("Texture %p has invalid format %s.\n",
4583 texture, debug_d3dformat(texture->resource.format->id));
4584 return WINED3DERR_INVALIDCALL;
4587 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4589 ERR("Failed to get display mode, hr %#x.\n", hr);
4590 return WINED3DERR_INVALIDCALL;
4593 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4594 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4595 if (cursor_width > mode.width || cursor_height > mode.height)
4597 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4598 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4599 return WINED3DERR_INVALIDCALL;
4602 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4604 /* Do not store the surface's pointer because the application may
4605 * release it after setting the cursor image. Windows doesn't
4606 * addref the set surface, so we can't do this either without
4607 * creating circular refcount dependencies. */
4608 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4610 ERR("Failed to create cursor texture.\n");
4611 return WINED3DERR_INVALIDCALL;
4614 if (cursor_width == 32 && cursor_height == 32)
4616 UINT mask_size = cursor_width * cursor_height / 8;
4617 ICONINFO cursor_info;
4618 DWORD *mask_bits;
4619 HCURSOR cursor;
4621 /* 32-bit user32 cursors ignore the alpha channel if it's all
4622 * zeroes, and use the mask instead. Fill the mask with all ones
4623 * to ensure we still get a fully transparent cursor. */
4624 if (!(mask_bits = heap_alloc(mask_size)))
4625 return E_OUTOFMEMORY;
4626 memset(mask_bits, 0xff, mask_size);
4628 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4629 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
4630 cursor_info.fIcon = FALSE;
4631 cursor_info.xHotspot = x_hotspot;
4632 cursor_info.yHotspot = y_hotspot;
4633 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4634 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4635 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4637 /* Create our cursor and clean up. */
4638 cursor = CreateIconIndirect(&cursor_info);
4639 if (cursor_info.hbmMask)
4640 DeleteObject(cursor_info.hbmMask);
4641 if (cursor_info.hbmColor)
4642 DeleteObject(cursor_info.hbmColor);
4643 if (device->hardwareCursor)
4644 DestroyCursor(device->hardwareCursor);
4645 device->hardwareCursor = cursor;
4646 if (device->bCursorVisible)
4647 SetCursor(cursor);
4649 heap_free(mask_bits);
4652 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4653 device->cursorWidth = cursor_width;
4654 device->cursorHeight = cursor_height;
4655 device->xHotSpot = x_hotspot;
4656 device->yHotSpot = y_hotspot;
4658 return WINED3D_OK;
4661 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4662 int x_screen_space, int y_screen_space, DWORD flags)
4664 TRACE("device %p, x %d, y %d, flags %#x.\n",
4665 device, x_screen_space, y_screen_space, flags);
4667 device->xScreenSpace = x_screen_space;
4668 device->yScreenSpace = y_screen_space;
4670 if (device->hardwareCursor)
4672 POINT pt;
4674 GetCursorPos( &pt );
4675 if (x_screen_space == pt.x && y_screen_space == pt.y)
4676 return;
4677 SetCursorPos( x_screen_space, y_screen_space );
4679 /* Switch to the software cursor if position diverges from the hardware one. */
4680 GetCursorPos( &pt );
4681 if (x_screen_space != pt.x || y_screen_space != pt.y)
4683 if (device->bCursorVisible) SetCursor( NULL );
4684 DestroyCursor( device->hardwareCursor );
4685 device->hardwareCursor = 0;
4690 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4692 BOOL oldVisible = device->bCursorVisible;
4694 TRACE("device %p, show %#x.\n", device, show);
4697 * When ShowCursor is first called it should make the cursor appear at the OS's last
4698 * known cursor position.
4700 if (show && !oldVisible)
4702 POINT pt;
4703 GetCursorPos(&pt);
4704 device->xScreenSpace = pt.x;
4705 device->yScreenSpace = pt.y;
4708 if (device->hardwareCursor)
4710 device->bCursorVisible = show;
4711 if (show)
4712 SetCursor(device->hardwareCursor);
4713 else
4714 SetCursor(NULL);
4716 else if (device->cursor_texture)
4718 device->bCursorVisible = show;
4721 return oldVisible;
4724 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4726 struct wined3d_resource *resource, *cursor;
4728 TRACE("device %p.\n", device);
4730 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4732 TRACE("Checking resource %p for eviction.\n", resource);
4734 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
4736 TRACE("Evicting %p.\n", resource);
4737 wined3d_cs_emit_unload_resource(device->cs, resource);
4742 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4743 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4744 wined3d_device_reset_cb callback, BOOL reset_state)
4746 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
4747 struct wined3d_resource *resource, *cursor;
4748 struct wined3d_swapchain *swapchain;
4749 struct wined3d_view_desc view_desc;
4750 BOOL backbuffer_resized;
4751 HRESULT hr = WINED3D_OK;
4752 unsigned int i;
4754 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4755 device, swapchain_desc, mode, callback, reset_state);
4757 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4759 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4761 ERR("Failed to get the first implicit swapchain.\n");
4762 return WINED3DERR_INVALIDCALL;
4765 if (reset_state)
4767 if (device->logo_texture)
4769 wined3d_texture_decref(device->logo_texture);
4770 device->logo_texture = NULL;
4772 if (device->cursor_texture)
4774 wined3d_texture_decref(device->cursor_texture);
4775 device->cursor_texture = NULL;
4777 state_unbind_resources(&device->state);
4780 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
4782 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4784 wined3d_device_set_depth_stencil_view(device, NULL);
4786 if (reset_state)
4788 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4790 TRACE("Enumerating resource %p.\n", resource);
4791 if (FAILED(hr = callback(resource)))
4792 return hr;
4796 TRACE("New params:\n");
4797 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4798 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4799 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4800 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4801 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4802 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4803 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4804 TRACE("device_window %p\n", swapchain_desc->device_window);
4805 TRACE("windowed %#x\n", swapchain_desc->windowed);
4806 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4807 if (swapchain_desc->enable_auto_depth_stencil)
4808 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4809 TRACE("flags %#x\n", swapchain_desc->flags);
4810 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4811 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4813 if (swapchain_desc->backbuffer_usage && swapchain_desc->backbuffer_usage != WINED3DUSAGE_RENDERTARGET)
4814 FIXME("Got unexpected backbuffer usage %#x.\n", swapchain_desc->backbuffer_usage);
4816 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
4817 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
4818 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
4819 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
4821 /* No special treatment of these parameters. Just store them */
4822 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4823 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4824 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4825 swapchain->desc.flags = swapchain_desc->flags;
4826 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4827 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4829 if (swapchain_desc->device_window
4830 && swapchain_desc->device_window != swapchain->desc.device_window)
4832 TRACE("Changing the device window from %p to %p.\n",
4833 swapchain->desc.device_window, swapchain_desc->device_window);
4834 swapchain->desc.device_window = swapchain_desc->device_window;
4835 swapchain->device_window = swapchain_desc->device_window;
4836 wined3d_swapchain_set_window(swapchain, NULL);
4839 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4840 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4842 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4843 || swapchain->reapply_mode || mode
4844 || (!swapchain_desc->windowed && backbuffer_resized))
4846 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4847 return hr;
4849 else if (!swapchain_desc->windowed)
4851 DWORD style = device->style;
4852 DWORD exStyle = device->exStyle;
4853 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4854 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4855 * Reset to clear up their mess. Guild Wars also loses the device during that.
4857 device->style = 0;
4858 device->exStyle = 0;
4859 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4860 swapchain_desc->backbuffer_width,
4861 swapchain_desc->backbuffer_height);
4862 device->style = style;
4863 device->exStyle = exStyle;
4866 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4867 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4868 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4869 return hr;
4871 if (device->auto_depth_stencil_view)
4873 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4874 device->auto_depth_stencil_view = NULL;
4876 if (swapchain->desc.enable_auto_depth_stencil)
4878 struct wined3d_resource_desc texture_desc;
4879 struct wined3d_texture *texture;
4880 DWORD flags = 0;
4882 TRACE("Creating the depth stencil buffer.\n");
4884 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4885 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4886 texture_desc.multisample_type = swapchain->desc.multisample_type;
4887 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4888 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4889 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4890 texture_desc.width = swapchain->desc.backbuffer_width;
4891 texture_desc.height = swapchain->desc.backbuffer_height;
4892 texture_desc.depth = 1;
4893 texture_desc.size = 0;
4895 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4896 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4898 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4899 device->device_parent, &texture_desc, flags, &texture)))
4901 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4902 return WINED3DERR_INVALIDCALL;
4905 view_desc.format_id = texture->resource.format->id;
4906 view_desc.flags = 0;
4907 view_desc.u.texture.level_idx = 0;
4908 view_desc.u.texture.level_count = 1;
4909 view_desc.u.texture.layer_idx = 0;
4910 view_desc.u.texture.layer_count = 1;
4911 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4912 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4913 wined3d_texture_decref(texture);
4914 if (FAILED(hr))
4916 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4917 return hr;
4920 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4923 if (device->back_buffer_view)
4925 wined3d_rendertarget_view_decref(device->back_buffer_view);
4926 device->back_buffer_view = NULL;
4928 if (swapchain->desc.backbuffer_count && swapchain->desc.backbuffer_usage & WINED3DUSAGE_RENDERTARGET)
4930 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4932 view_desc.format_id = back_buffer->format->id;
4933 view_desc.flags = 0;
4934 view_desc.u.texture.level_idx = 0;
4935 view_desc.u.texture.level_count = 1;
4936 view_desc.u.texture.layer_idx = 0;
4937 view_desc.u.texture.layer_count = 1;
4938 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4939 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4941 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4942 return hr;
4946 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4948 if (reset_state)
4950 TRACE("Resetting stateblock.\n");
4951 if (device->recording)
4953 wined3d_stateblock_decref(device->recording);
4954 device->recording = NULL;
4956 wined3d_cs_emit_reset_state(device->cs);
4957 state_cleanup(&device->state);
4959 if (device->d3d_initialized)
4960 wined3d_device_delete_opengl_contexts(device);
4962 memset(&device->state, 0, sizeof(device->state));
4963 state_init(&device->state, &device->fb, &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4964 device->update_state = &device->state;
4966 device_init_swapchain_state(device, swapchain);
4967 if (wined3d_settings.logo)
4968 device_load_logo(device, wined3d_settings.logo);
4970 else if (device->back_buffer_view)
4972 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4973 struct wined3d_state *state = &device->state;
4975 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4977 /* Note the min_z / max_z is not reset. */
4978 state->viewports[0].x = 0;
4979 state->viewports[0].y = 0;
4980 state->viewports[0].width = view->width;
4981 state->viewports[0].height = view->height;
4982 state->viewport_count = 1;
4983 wined3d_cs_emit_set_viewports(device->cs, 1, state->viewports);
4985 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
4986 state->scissor_rect_count = 1;
4987 wined3d_cs_emit_set_scissor_rects(device->cs, 1, state->scissor_rects);
4990 if (device->d3d_initialized)
4992 if (reset_state)
4993 hr = wined3d_device_create_primary_opengl_context(device);
4996 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4997 * first use
4999 return hr;
5002 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5004 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5006 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5008 return WINED3D_OK;
5012 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5013 struct wined3d_device_creation_parameters *parameters)
5015 TRACE("device %p, parameters %p.\n", device, parameters);
5017 *parameters = device->create_parms;
5020 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5022 TRACE("device %p.\n", device);
5024 return device->wined3d;
5027 enum wined3d_feature_level CDECL wined3d_device_get_feature_level(const struct wined3d_device *device)
5029 TRACE("device %p.\n", device);
5031 return device->feature_level;
5034 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5035 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5037 struct wined3d_swapchain *swapchain;
5039 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5040 device, swapchain_idx, flags, ramp);
5042 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5043 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5046 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5047 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5049 struct wined3d_swapchain *swapchain;
5051 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5052 device, swapchain_idx, ramp);
5054 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5055 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5058 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5060 TRACE("device %p, resource %p.\n", device, resource);
5062 wined3d_not_from_cs(device->cs);
5064 list_add_head(&device->resources, &resource->resource_list_entry);
5067 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5069 TRACE("device %p, resource %p.\n", device, resource);
5071 wined3d_not_from_cs(device->cs);
5073 list_remove(&resource->resource_list_entry);
5076 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5078 enum wined3d_resource_type type = resource->type;
5079 struct wined3d_rendertarget_view *rtv;
5080 unsigned int i;
5082 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5084 if (device->d3d_initialized)
5086 for (i = 0; i < ARRAY_SIZE(device->fb.render_targets); ++i)
5088 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
5089 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5092 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5093 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5096 switch (type)
5098 case WINED3D_RTYPE_TEXTURE_1D:
5099 case WINED3D_RTYPE_TEXTURE_2D:
5100 case WINED3D_RTYPE_TEXTURE_3D:
5101 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5103 if (&device->state.textures[i]->resource == resource)
5105 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5106 device->state.textures[i] = NULL;
5109 if (device->recording && &device->update_state->textures[i]->resource == resource)
5111 ERR("Texture resource %p is still in use by recording stateblock %p, stage %u.\n",
5112 resource, device->recording, i);
5113 device->update_state->textures[i] = NULL;
5116 break;
5118 case WINED3D_RTYPE_BUFFER:
5119 for (i = 0; i < MAX_STREAMS; ++i)
5121 if (&device->state.streams[i].buffer->resource == resource)
5123 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5124 device->state.streams[i].buffer = NULL;
5127 if (device->recording && &device->update_state->streams[i].buffer->resource == resource)
5129 ERR("Buffer resource %p is still in use by stateblock %p, stream %u.\n",
5130 resource, device->recording, i);
5131 device->update_state->streams[i].buffer = NULL;
5135 if (&device->state.index_buffer->resource == resource)
5137 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5138 device->state.index_buffer = NULL;
5141 if (device->recording && &device->update_state->index_buffer->resource == resource)
5143 ERR("Buffer resource %p is still in use by stateblock %p as index buffer.\n",
5144 resource, device->recording);
5145 device->update_state->index_buffer = NULL;
5147 break;
5149 default:
5150 break;
5153 /* Remove the resource from the resourceStore */
5154 device_resource_remove(device, resource);
5156 TRACE("Resource released.\n");
5159 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5161 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5163 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5166 static BOOL wined3d_select_feature_level(const struct wined3d_adapter *adapter,
5167 const enum wined3d_feature_level *levels, unsigned int level_count,
5168 enum wined3d_feature_level *selected_level)
5170 const struct wined3d_d3d_info *d3d_info = &adapter->d3d_info;
5171 unsigned int i;
5173 for (i = 0; i < level_count; ++i)
5175 if (levels[i] && d3d_info->feature_level >= levels[i])
5177 *selected_level = levels[i];
5178 return TRUE;
5182 FIXME_(winediag)("None of the requested D3D feature levels is supported on this GPU "
5183 "with the current shader backend.\n");
5184 return FALSE;
5187 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5188 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5189 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5190 struct wined3d_device_parent *device_parent)
5192 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5193 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5194 const struct fragment_pipeline *fragment_pipeline;
5195 unsigned int i;
5196 HRESULT hr;
5198 if (!wined3d_select_feature_level(adapter, levels, level_count, &device->feature_level))
5199 return E_FAIL;
5201 TRACE("Device feature level %s.\n", wined3d_debug_feature_level(device->feature_level));
5203 device->ref = 1;
5204 device->wined3d = wined3d;
5205 wined3d_incref(device->wined3d);
5206 device->adapter = wined3d->adapter_count ? adapter : NULL;
5207 device->device_parent = device_parent;
5208 list_init(&device->resources);
5209 list_init(&device->shaders);
5210 device->surface_alignment = surface_alignment;
5212 /* Save the creation parameters. */
5213 device->create_parms.adapter_idx = adapter_idx;
5214 device->create_parms.device_type = device_type;
5215 device->create_parms.focus_window = focus_window;
5216 device->create_parms.flags = flags;
5218 device->shader_backend = adapter->shader_backend;
5220 vertex_pipeline = adapter->vertex_pipe;
5222 fragment_pipeline = adapter->fragment_pipe;
5224 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5226 if (vertex_pipeline->vp_states && fragment_pipeline->states
5227 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5228 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5229 fragment_pipeline, misc_state_template)))
5231 ERR("Failed to compile state table, hr %#x.\n", hr);
5232 wine_rb_destroy(&device->samplers, NULL, NULL);
5233 wined3d_decref(device->wined3d);
5234 return hr;
5237 state_init(&device->state, &device->fb, &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5238 device->update_state = &device->state;
5240 device->max_frame_latency = 3;
5242 if (!(device->cs = wined3d_cs_create(device)))
5244 WARN("Failed to create command stream.\n");
5245 state_cleanup(&device->state);
5246 hr = E_FAIL;
5247 goto err;
5250 return WINED3D_OK;
5252 err:
5253 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5255 heap_free(device->multistate_funcs[i]);
5257 wine_rb_destroy(&device->samplers, NULL, NULL);
5258 wined3d_decref(device->wined3d);
5259 return hr;
5262 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5264 DWORD rep = device->StateTable[state].representative;
5265 struct wined3d_context *context;
5266 DWORD idx;
5267 BYTE shift;
5268 UINT i;
5270 wined3d_from_cs(device->cs);
5272 if (STATE_IS_COMPUTE(state))
5274 for (i = 0; i < device->context_count; ++i)
5275 context_invalidate_compute_state(device->contexts[i], state);
5276 return;
5279 for (i = 0; i < device->context_count; ++i)
5281 context = device->contexts[i];
5282 if(isStateDirty(context, rep)) continue;
5284 context->dirtyArray[context->numDirtyEntries++] = rep;
5285 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5286 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5287 context->isStateDirty[idx] |= (1u << shift);
5291 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5292 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5294 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5296 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5297 window, message, wparam, lparam);
5298 if (unicode)
5299 return DefWindowProcW(window, message, wparam, lparam);
5300 else
5301 return DefWindowProcA(window, message, wparam, lparam);
5304 if (message == WM_DESTROY)
5306 TRACE("unregister window %p.\n", window);
5307 wined3d_unregister_window(window);
5309 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5310 ERR("Window %p is not the focus window for device %p.\n", window, device);
5312 else if (message == WM_DISPLAYCHANGE)
5314 device->device_parent->ops->mode_changed(device->device_parent);
5316 else if (message == WM_ACTIVATEAPP)
5318 UINT i;
5320 for (i = 0; i < device->swapchain_count; i++)
5321 wined3d_swapchain_activate(device->swapchains[i], wparam);
5323 device->device_parent->ops->activate(device->device_parent, wparam);
5325 else if (message == WM_SYSCOMMAND)
5327 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5329 if (unicode)
5330 DefWindowProcW(window, message, wparam, lparam);
5331 else
5332 DefWindowProcA(window, message, wparam, lparam);
5336 if (unicode)
5337 return CallWindowProcW(proc, window, message, wparam, lparam);
5338 else
5339 return CallWindowProcA(proc, window, message, wparam, lparam);