wined3d: Simplify wined3d_device_update_sub_resource() a little.
[wine.git] / dlls / wined3d / device.c
blobb66b3946c465d97291ca6f88ef874ba688fab97c
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 static 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 checkGLcall("glScissor");
417 gl_info->gl_ops.gl.p_glClear(clear_mask);
418 checkGLcall("glClear");
420 else
422 RECT current_rect;
424 /* Now process each rect in turn. */
425 for (i = 0; i < rect_count; ++i)
427 /* Note that GL uses lower left, width/height. */
428 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
430 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
431 wine_dbgstr_rect(&clear_rect[i]),
432 wine_dbgstr_rect(&current_rect));
434 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
435 * The rectangle is not cleared, no error is returned, but further rectangles are
436 * still cleared if they are valid. */
437 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
439 TRACE("Rectangle with negative dimensions, ignoring.\n");
440 continue;
443 if (render_offscreen)
445 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
446 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
448 else
450 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
451 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
453 checkGLcall("glScissor");
455 gl_info->gl_ops.gl.p_glClear(clear_mask);
456 checkGLcall("glClear");
460 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
461 gl_info->gl_ops.gl.p_glFlush();
463 context_release(context);
466 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
468 ULONG refcount = InterlockedIncrement(&device->ref);
470 TRACE("%p increasing refcount to %u.\n", device, refcount);
472 return refcount;
475 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
477 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
479 ERR("Leftover sampler %p.\n", sampler);
482 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
484 ULONG refcount = InterlockedDecrement(&device->ref);
486 TRACE("%p decreasing refcount to %u.\n", device, refcount);
488 if (!refcount)
490 UINT i;
492 wined3d_cs_destroy(device->cs);
494 if (device->recording && wined3d_stateblock_decref(device->recording))
495 ERR("Something's still holding the recording stateblock.\n");
496 device->recording = NULL;
498 state_cleanup(&device->state);
500 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
502 heap_free(device->multistate_funcs[i]);
503 device->multistate_funcs[i] = NULL;
506 if (!list_empty(&device->resources))
508 struct wined3d_resource *resource;
510 ERR("Device released with resources still bound.\n");
512 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
514 ERR("Leftover resource %p with type %s (%#x).\n",
515 resource, debug_d3dresourcetype(resource->type), resource->type);
519 if (device->contexts)
520 ERR("Context array not freed!\n");
521 if (device->hardwareCursor)
522 DestroyCursor(device->hardwareCursor);
523 device->hardwareCursor = 0;
525 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
527 wined3d_decref(device->wined3d);
528 device->wined3d = NULL;
529 heap_free(device);
530 TRACE("Freed device %p.\n", device);
533 return refcount;
536 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
538 TRACE("device %p.\n", device);
540 return device->swapchain_count;
543 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
545 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
547 if (swapchain_idx >= device->swapchain_count)
549 WARN("swapchain_idx %u >= swapchain_count %u.\n",
550 swapchain_idx, device->swapchain_count);
551 return NULL;
554 return device->swapchains[swapchain_idx];
557 static void device_load_logo(struct wined3d_device *device, const char *filename)
559 struct wined3d_color_key color_key;
560 struct wined3d_resource_desc desc;
561 HBITMAP hbm;
562 BITMAP bm;
563 HRESULT hr;
564 HDC dcb = NULL, dcs = NULL;
566 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
568 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
569 return;
571 GetObjectA(hbm, sizeof(BITMAP), &bm);
573 if (!(dcb = CreateCompatibleDC(NULL)))
574 goto out;
575 SelectObject(dcb, hbm);
577 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
578 desc.format = WINED3DFMT_B5G6R5_UNORM;
579 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
580 desc.multisample_quality = 0;
581 desc.usage = WINED3DUSAGE_DYNAMIC;
582 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
583 desc.width = bm.bmWidth;
584 desc.height = bm.bmHeight;
585 desc.depth = 1;
586 desc.size = 0;
587 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1,
588 WINED3D_TEXTURE_CREATE_MAPPABLE | WINED3D_TEXTURE_CREATE_GET_DC,
589 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
591 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
592 goto out;
595 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
597 wined3d_texture_decref(device->logo_texture);
598 device->logo_texture = NULL;
599 goto out;
601 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
602 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
604 color_key.color_space_low_value = 0;
605 color_key.color_space_high_value = 0;
606 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
608 out:
609 if (dcb) DeleteDC(dcb);
610 if (hbm) DeleteObject(hbm);
613 /* Context activation is done by the caller. */
614 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
616 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
617 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
618 struct wined3d_dummy_textures *textures = &device->dummy_textures;
619 unsigned int i;
620 DWORD color;
622 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
623 color = 0x000000ff;
624 else
625 color = 0x00000000;
627 /* Under DirectX you can sample even if no texture is bound, whereas
628 * OpenGL will only allow that when a valid texture is bound.
629 * We emulate this by creating dummy textures and binding them
630 * to each texture stage when the currently set D3D texture is NULL. */
631 context_active_texture(context, gl_info, 0);
633 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
634 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
635 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
636 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
637 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
639 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
641 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
642 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
643 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
644 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
645 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
648 if (gl_info->supported[EXT_TEXTURE3D])
650 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
651 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
652 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
653 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
654 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
657 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
659 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
660 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
661 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
662 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
664 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
665 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
669 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
671 DWORD cube_array_data[6];
673 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
674 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
675 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
676 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
677 cube_array_data[i] = color;
678 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
679 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
682 if (gl_info->supported[EXT_TEXTURE_ARRAY])
684 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
685 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
686 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
687 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
688 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
691 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
693 GLuint buffer;
695 GL_EXTCALL(glGenBuffers(1, &buffer));
696 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
697 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
698 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
700 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
701 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
702 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
703 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
704 GL_EXTCALL(glDeleteBuffers(1, &buffer));
707 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
709 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
710 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
711 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
712 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
714 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
715 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
716 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
717 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
719 if (gl_info->supported[ARB_CLEAR_TEXTURE])
721 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
722 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
724 else
726 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
730 checkGLcall("create dummy textures");
732 context_bind_dummy_textures(device, context);
735 /* Context activation is done by the caller. */
736 static void destroy_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
738 struct wined3d_dummy_textures *dummy_textures = &device->dummy_textures;
739 const struct wined3d_gl_info *gl_info = context->gl_info;
741 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
743 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
744 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
747 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
748 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
750 if (gl_info->supported[EXT_TEXTURE_ARRAY])
751 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
753 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
754 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
756 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
757 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
759 if (gl_info->supported[EXT_TEXTURE3D])
760 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
762 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
763 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
765 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
767 checkGLcall("delete dummy textures");
769 memset(dummy_textures, 0, sizeof(*dummy_textures));
772 /* Context activation is done by the caller. */
773 static void create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
775 struct wined3d_sampler_desc desc;
776 HRESULT hr;
778 desc.address_u = WINED3D_TADDRESS_WRAP;
779 desc.address_v = WINED3D_TADDRESS_WRAP;
780 desc.address_w = WINED3D_TADDRESS_WRAP;
781 memset(desc.border_color, 0, sizeof(desc.border_color));
782 desc.mag_filter = WINED3D_TEXF_POINT;
783 desc.min_filter = WINED3D_TEXF_POINT;
784 desc.mip_filter = WINED3D_TEXF_NONE;
785 desc.lod_bias = 0.0f;
786 desc.min_lod = -1000.0f;
787 desc.max_lod = 1000.0f;
788 desc.mip_base_level = 0;
789 desc.max_anisotropy = 1;
790 desc.compare = FALSE;
791 desc.comparison_func = WINED3D_CMP_NEVER;
792 desc.srgb_decode = TRUE;
794 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
795 * instructions allow access to resources without using samplers.
796 * In GLSL, resources are always accessed through sampler or image variables. The default
797 * sampler object is used to emulate the direct resource access when there is no sampler state
798 * to use.
800 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
802 ERR("Failed to create default sampler, hr %#x.\n", hr);
803 device->default_sampler = NULL;
806 /* In D3D10+, a NULL sampler maps to the default sampler state. */
807 desc.address_u = WINED3D_TADDRESS_CLAMP;
808 desc.address_v = WINED3D_TADDRESS_CLAMP;
809 desc.address_w = WINED3D_TADDRESS_CLAMP;
810 desc.mag_filter = WINED3D_TEXF_LINEAR;
811 desc.min_filter = WINED3D_TEXF_LINEAR;
812 desc.mip_filter = WINED3D_TEXF_LINEAR;
813 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
815 ERR("Failed to create null sampler, hr %#x.\n", hr);
816 device->null_sampler = NULL;
820 /* Context activation is done by the caller. */
821 static void destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
823 wined3d_sampler_decref(device->default_sampler);
824 device->default_sampler = NULL;
825 wined3d_sampler_decref(device->null_sampler);
826 device->null_sampler = NULL;
829 static LONG fullscreen_style(LONG style)
831 /* Make sure the window is managed, otherwise we won't get keyboard input. */
832 style |= WS_POPUP | WS_SYSMENU;
833 style &= ~(WS_CAPTION | WS_THICKFRAME);
835 return style;
838 static LONG fullscreen_exstyle(LONG exstyle)
840 /* Filter out window decorations. */
841 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
843 return exstyle;
846 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
848 BOOL filter_messages;
849 LONG style, exstyle;
851 TRACE("Setting up window %p for fullscreen mode.\n", window);
853 if (device->style || device->exStyle)
855 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
856 window, device->style, device->exStyle);
859 device->style = GetWindowLongW(window, GWL_STYLE);
860 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
862 style = fullscreen_style(device->style);
863 exstyle = fullscreen_exstyle(device->exStyle);
865 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
866 device->style, device->exStyle, style, exstyle);
868 filter_messages = device->filter_messages;
869 device->filter_messages = TRUE;
871 SetWindowLongW(window, GWL_STYLE, style);
872 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
873 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
875 device->filter_messages = filter_messages;
878 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window,
879 const RECT *window_rect)
881 unsigned int window_pos_flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE;
882 BOOL filter_messages;
883 LONG style, exstyle;
884 RECT rect = {0};
886 if (!device->style && !device->exStyle)
887 return;
889 style = GetWindowLongW(window, GWL_STYLE);
890 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
892 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
893 * application, and we want to ignore them in the test below, since it's
894 * not the application's fault that they changed. Additionally, we want to
895 * preserve the current status of these flags (i.e. don't restore them) to
896 * more closely emulate the behavior of Direct3D, which leaves these flags
897 * alone when returning to windowed mode. */
898 device->style ^= (device->style ^ style) & WS_VISIBLE;
899 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
901 TRACE("Restoring window style of window %p to %08x, %08x.\n",
902 window, device->style, device->exStyle);
904 filter_messages = device->filter_messages;
905 device->filter_messages = TRUE;
907 /* Only restore the style if the application didn't modify it during the
908 * fullscreen phase. Some applications change it before calling Reset()
909 * when switching between windowed and fullscreen modes (HL2), some
910 * depend on the original style (Eve Online). */
911 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
913 SetWindowLongW(window, GWL_STYLE, device->style);
914 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
917 if (window_rect)
918 rect = *window_rect;
919 else
920 window_pos_flags |= (SWP_NOMOVE | SWP_NOSIZE);
921 SetWindowPos(window, 0, rect.left, rect.top,
922 rect.right - rect.left, rect.bottom - rect.top, window_pos_flags);
924 device->filter_messages = filter_messages;
926 /* Delete the old values. */
927 device->style = 0;
928 device->exStyle = 0;
931 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
933 TRACE("device %p, window %p.\n", device, window);
935 if (!wined3d_register_window(window, device))
937 ERR("Failed to register window %p.\n", window);
938 return E_FAIL;
941 InterlockedExchangePointer((void **)&device->focus_window, window);
942 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
944 return WINED3D_OK;
947 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
949 TRACE("device %p.\n", device);
951 if (device->focus_window) wined3d_unregister_window(device->focus_window);
952 InterlockedExchangePointer((void **)&device->focus_window, NULL);
955 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
957 BOOL ds_enable = swapchain->desc.enable_auto_depth_stencil;
958 unsigned int i;
960 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
962 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
964 if (device->back_buffer_view)
965 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
967 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
970 static void wined3d_device_delete_opengl_contexts_cs(void *object)
972 struct wined3d_resource *resource, *cursor;
973 struct wined3d_device *device = object;
974 struct wined3d_context *context;
975 struct wined3d_shader *shader;
977 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
979 TRACE("Unloading resource %p.\n", resource);
980 wined3d_cs_emit_unload_resource(device->cs, resource);
983 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
985 device->shader_backend->shader_destroy(shader);
988 context = context_acquire(device, NULL, 0);
989 device->blitter->ops->blitter_destroy(device->blitter, context);
990 device->shader_backend->shader_free_private(device);
991 destroy_dummy_textures(device, context);
992 destroy_default_samplers(device, context);
993 context_release(context);
995 while (device->context_count)
997 if (device->contexts[0]->swapchain)
998 swapchain_destroy_contexts(device->contexts[0]->swapchain);
999 else
1000 context_destroy(device, device->contexts[0]);
1004 static void wined3d_device_delete_opengl_contexts(struct wined3d_device *device)
1006 wined3d_cs_destroy_object(device->cs, wined3d_device_delete_opengl_contexts_cs, device);
1007 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1010 static void wined3d_device_create_primary_opengl_context_cs(void *object)
1012 struct wined3d_device *device = object;
1013 struct wined3d_swapchain *swapchain;
1014 struct wined3d_context *context;
1015 struct wined3d_texture *target;
1016 HRESULT hr;
1018 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1019 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1021 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1022 return;
1025 if (!(device->blitter = wined3d_cpu_blitter_create()))
1027 ERR("Failed to create CPU blitter.\n");
1028 device->shader_backend->shader_free_private(device);
1029 return;
1031 wined3d_ffp_blitter_create(&device->blitter, &device->adapter->gl_info);
1032 wined3d_arbfp_blitter_create(&device->blitter, device);
1033 wined3d_fbo_blitter_create(&device->blitter, &device->adapter->gl_info);
1034 wined3d_raw_blitter_create(&device->blitter, &device->adapter->gl_info);
1036 swapchain = device->swapchains[0];
1037 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1038 context = context_acquire(device, target, 0);
1039 create_dummy_textures(device, context);
1040 create_default_samplers(device, context);
1041 context_release(context);
1044 static HRESULT wined3d_device_create_primary_opengl_context(struct wined3d_device *device)
1046 wined3d_cs_init_object(device->cs, wined3d_device_create_primary_opengl_context_cs, device);
1047 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1048 if (!device->swapchains[0]->num_contexts)
1049 return E_FAIL;
1051 return WINED3D_OK;
1054 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1055 struct wined3d_swapchain_desc *swapchain_desc)
1057 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1058 struct wined3d_swapchain *swapchain = NULL;
1059 DWORD clear_flags = 0;
1060 HRESULT hr;
1062 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1064 if (device->d3d_initialized)
1065 return WINED3DERR_INVALIDCALL;
1066 if (device->wined3d->flags & WINED3D_NO3D)
1067 return WINED3DERR_INVALIDCALL;
1069 memset(device->fb.render_targets, 0, sizeof(device->fb.render_targets));
1071 /* Setup the implicit swapchain. This also initializes a context. */
1072 TRACE("Creating implicit swapchain.\n");
1073 if (FAILED(hr = device->device_parent->ops->create_swapchain(device->device_parent,
1074 swapchain_desc, &swapchain)))
1076 WARN("Failed to create implicit swapchain.\n");
1077 goto err_out;
1080 if (swapchain_desc->backbuffer_count)
1082 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1083 struct wined3d_view_desc view_desc;
1085 view_desc.format_id = back_buffer->format->id;
1086 view_desc.flags = 0;
1087 view_desc.u.texture.level_idx = 0;
1088 view_desc.u.texture.level_count = 1;
1089 view_desc.u.texture.layer_idx = 0;
1090 view_desc.u.texture.layer_count = 1;
1091 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1092 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1094 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1095 goto err_out;
1099 device->swapchain_count = 1;
1100 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1102 ERR("Out of memory!\n");
1103 goto err_out;
1105 device->swapchains[0] = swapchain;
1107 if (FAILED(hr = wined3d_device_create_primary_opengl_context(device)))
1108 goto err_out;
1109 device_init_swapchain_state(device, swapchain);
1111 device->contexts[0]->last_was_rhw = 0;
1113 TRACE("All defaults now set up.\n");
1115 /* Clear the screen */
1116 if (swapchain->back_buffers && swapchain->back_buffers[0])
1117 clear_flags |= WINED3DCLEAR_TARGET;
1118 if (swapchain_desc->enable_auto_depth_stencil)
1119 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1120 if (clear_flags)
1121 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1123 device->d3d_initialized = TRUE;
1125 if (wined3d_settings.logo)
1126 device_load_logo(device, wined3d_settings.logo);
1127 return WINED3D_OK;
1129 err_out:
1130 heap_free(device->swapchains);
1131 device->swapchain_count = 0;
1132 if (device->back_buffer_view)
1133 wined3d_rendertarget_view_decref(device->back_buffer_view);
1134 if (swapchain)
1135 wined3d_swapchain_decref(swapchain);
1137 return hr;
1140 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1141 struct wined3d_swapchain_desc *swapchain_desc)
1143 struct wined3d_swapchain *swapchain = NULL;
1144 HRESULT hr;
1146 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1148 /* Setup the implicit swapchain */
1149 TRACE("Creating implicit swapchain\n");
1150 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1151 swapchain_desc, &swapchain);
1152 if (FAILED(hr))
1154 WARN("Failed to create implicit swapchain\n");
1155 goto err_out;
1158 device->swapchain_count = 1;
1159 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1161 ERR("Out of memory!\n");
1162 goto err_out;
1164 device->swapchains[0] = swapchain;
1166 if (!(device->blitter = wined3d_cpu_blitter_create()))
1168 ERR("Failed to create CPU blitter.\n");
1169 heap_free(device->swapchains);
1170 device->swapchain_count = 0;
1171 goto err_out;
1174 return WINED3D_OK;
1176 err_out:
1177 wined3d_swapchain_decref(swapchain);
1178 return hr;
1181 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1183 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1185 wined3d_sampler_decref(sampler);
1188 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1190 unsigned int i;
1192 TRACE("device %p.\n", device);
1194 if (!device->d3d_initialized)
1195 return WINED3DERR_INVALIDCALL;
1197 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1199 if (device->logo_texture)
1200 wined3d_texture_decref(device->logo_texture);
1201 if (device->cursor_texture)
1202 wined3d_texture_decref(device->cursor_texture);
1204 state_unbind_resources(&device->state);
1206 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1208 wined3d_device_delete_opengl_contexts(device);
1210 if (device->fb.depth_stencil)
1212 struct wined3d_rendertarget_view *view = device->fb.depth_stencil;
1214 TRACE("Releasing depth/stencil view %p.\n", view);
1216 device->fb.depth_stencil = NULL;
1217 wined3d_rendertarget_view_decref(view);
1220 if (device->auto_depth_stencil_view)
1222 struct wined3d_rendertarget_view *view = device->auto_depth_stencil_view;
1224 device->auto_depth_stencil_view = NULL;
1225 if (wined3d_rendertarget_view_decref(view))
1226 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1229 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
1231 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
1233 if (device->back_buffer_view)
1235 wined3d_rendertarget_view_decref(device->back_buffer_view);
1236 device->back_buffer_view = NULL;
1239 for (i = 0; i < device->swapchain_count; ++i)
1241 TRACE("Releasing the implicit swapchain %u.\n", i);
1242 if (wined3d_swapchain_decref(device->swapchains[i]))
1243 FIXME("Something's still holding the implicit swapchain.\n");
1246 heap_free(device->swapchains);
1247 device->swapchains = NULL;
1248 device->swapchain_count = 0;
1250 device->d3d_initialized = FALSE;
1252 return WINED3D_OK;
1255 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1257 unsigned int i;
1259 device->blitter->ops->blitter_destroy(device->blitter, NULL);
1261 for (i = 0; i < device->swapchain_count; ++i)
1263 TRACE("Releasing the implicit swapchain %u.\n", i);
1264 if (wined3d_swapchain_decref(device->swapchains[i]))
1265 FIXME("Something's still holding the implicit swapchain.\n");
1268 heap_free(device->swapchains);
1269 device->swapchains = NULL;
1270 device->swapchain_count = 0;
1271 return WINED3D_OK;
1274 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1275 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1276 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1278 * There is no way to deactivate thread safety once it is enabled.
1280 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1282 TRACE("device %p.\n", device);
1284 /* For now just store the flag (needed in case of ddraw). */
1285 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1288 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1290 TRACE("device %p.\n", device);
1292 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1293 wine_dbgstr_longlong(device->adapter->vram_bytes),
1294 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1295 wine_dbgstr_longlong(device->adapter->vram_bytes - device->adapter->vram_bytes_used));
1297 return min(UINT_MAX, device->adapter->vram_bytes - device->adapter->vram_bytes_used);
1300 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1301 struct wined3d_buffer *buffer, UINT offset)
1303 struct wined3d_stream_output *stream;
1304 struct wined3d_buffer *prev_buffer;
1306 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1308 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1310 WARN("Invalid stream output %u.\n", idx);
1311 return;
1314 stream = &device->update_state->stream_output[idx];
1315 prev_buffer = stream->buffer;
1317 if (buffer)
1318 wined3d_buffer_incref(buffer);
1319 stream->buffer = buffer;
1320 stream->offset = offset;
1321 if (!device->recording)
1322 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1323 if (prev_buffer)
1324 wined3d_buffer_decref(prev_buffer);
1327 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1328 UINT idx, UINT *offset)
1330 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1332 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1334 WARN("Invalid stream output %u.\n", idx);
1335 return NULL;
1338 if (offset)
1339 *offset = device->state.stream_output[idx].offset;
1340 return device->state.stream_output[idx].buffer;
1343 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1344 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1346 struct wined3d_stream_state *stream;
1347 struct wined3d_buffer *prev_buffer;
1349 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1350 device, stream_idx, buffer, offset, stride);
1352 if (stream_idx >= MAX_STREAMS)
1354 WARN("Stream index %u out of range.\n", stream_idx);
1355 return WINED3DERR_INVALIDCALL;
1357 else if (offset & 0x3)
1359 WARN("Offset %u is not 4 byte aligned.\n", offset);
1360 return WINED3DERR_INVALIDCALL;
1363 stream = &device->update_state->streams[stream_idx];
1364 prev_buffer = stream->buffer;
1366 if (device->recording)
1367 device->recording->changed.streamSource |= 1u << stream_idx;
1369 if (prev_buffer == buffer
1370 && stream->stride == stride
1371 && stream->offset == offset)
1373 TRACE("Application is setting the old values over, nothing to do.\n");
1374 return WINED3D_OK;
1377 stream->buffer = buffer;
1378 if (buffer)
1380 stream->stride = stride;
1381 stream->offset = offset;
1382 wined3d_buffer_incref(buffer);
1385 if (!device->recording)
1386 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1387 if (prev_buffer)
1388 wined3d_buffer_decref(prev_buffer);
1390 return WINED3D_OK;
1393 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1394 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1396 const struct wined3d_stream_state *stream;
1398 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1399 device, stream_idx, buffer, offset, stride);
1401 if (stream_idx >= MAX_STREAMS)
1403 WARN("Stream index %u out of range.\n", stream_idx);
1404 return WINED3DERR_INVALIDCALL;
1407 stream = &device->state.streams[stream_idx];
1408 *buffer = stream->buffer;
1409 if (offset)
1410 *offset = stream->offset;
1411 *stride = stream->stride;
1413 return WINED3D_OK;
1416 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1418 struct wined3d_stream_state *stream;
1419 UINT old_flags, old_freq;
1421 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1423 /* Verify input. At least in d3d9 this is invalid. */
1424 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1426 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1427 return WINED3DERR_INVALIDCALL;
1429 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1431 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1432 return WINED3DERR_INVALIDCALL;
1434 if (!divider)
1436 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1437 return WINED3DERR_INVALIDCALL;
1440 stream = &device->update_state->streams[stream_idx];
1441 old_flags = stream->flags;
1442 old_freq = stream->frequency;
1444 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1445 stream->frequency = divider & 0x7fffff;
1447 if (device->recording)
1448 device->recording->changed.streamFreq |= 1u << stream_idx;
1449 else if (stream->frequency != old_freq || stream->flags != old_flags)
1450 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1452 return WINED3D_OK;
1455 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1456 UINT stream_idx, UINT *divider)
1458 const struct wined3d_stream_state *stream;
1460 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1462 stream = &device->state.streams[stream_idx];
1463 *divider = stream->flags | stream->frequency;
1465 TRACE("Returning %#x.\n", *divider);
1467 return WINED3D_OK;
1470 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1471 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1473 TRACE("device %p, state %s, matrix %p.\n",
1474 device, debug_d3dtstype(d3dts), matrix);
1475 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1476 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1477 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1478 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1480 /* Handle recording of state blocks. */
1481 if (device->recording)
1483 TRACE("Recording... not performing anything.\n");
1484 device->recording->changed.transform[d3dts >> 5] |= 1u << (d3dts & 0x1f);
1485 device->update_state->transforms[d3dts] = *matrix;
1486 return;
1489 /* If the new matrix is the same as the current one,
1490 * we cut off any further processing. this seems to be a reasonable
1491 * optimization because as was noticed, some apps (warcraft3 for example)
1492 * tend towards setting the same matrix repeatedly for some reason.
1494 * From here on we assume that the new matrix is different, wherever it matters. */
1495 if (!memcmp(&device->state.transforms[d3dts], matrix, sizeof(*matrix)))
1497 TRACE("The application is setting the same matrix over again.\n");
1498 return;
1501 device->state.transforms[d3dts] = *matrix;
1502 wined3d_cs_emit_set_transform(device->cs, d3dts, matrix);
1505 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1506 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1508 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1510 *matrix = device->state.transforms[state];
1513 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1514 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1516 const struct wined3d_matrix *mat;
1517 struct wined3d_matrix temp;
1519 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1521 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1522 * below means it will be recorded in a state block change, but it
1523 * works regardless where it is recorded.
1524 * If this is found to be wrong, change to StateBlock. */
1525 if (state > HIGHEST_TRANSFORMSTATE)
1527 WARN("Unhandled transform state %#x.\n", state);
1528 return;
1531 mat = &device->update_state->transforms[state];
1532 multiply_matrix(&temp, mat, matrix);
1534 /* Apply change via set transform - will reapply to eg. lights this way. */
1535 wined3d_device_set_transform(device, state, &temp);
1538 /* Note lights are real special cases. Although the device caps state only
1539 * e.g. 8 are supported, you can reference any indexes you want as long as
1540 * that number max are enabled at any one point in time. Therefore since the
1541 * indices can be anything, we need a hashmap of them. However, this causes
1542 * stateblock problems. When capturing the state block, I duplicate the
1543 * hashmap, but when recording, just build a chain pretty much of commands to
1544 * be replayed. */
1545 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1546 UINT light_idx, const struct wined3d_light *light)
1548 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1549 struct wined3d_light_info *object = NULL;
1550 float rho;
1552 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1554 /* Check the parameter range. Need for speed most wanted sets junk lights
1555 * which confuse the GL driver. */
1556 if (!light)
1557 return WINED3DERR_INVALIDCALL;
1559 switch (light->type)
1561 case WINED3D_LIGHT_POINT:
1562 case WINED3D_LIGHT_SPOT:
1563 case WINED3D_LIGHT_GLSPOT:
1564 /* Incorrect attenuation values can cause the gl driver to crash.
1565 * Happens with Need for speed most wanted. */
1566 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1568 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1569 return WINED3DERR_INVALIDCALL;
1571 break;
1573 case WINED3D_LIGHT_DIRECTIONAL:
1574 case WINED3D_LIGHT_PARALLELPOINT:
1575 /* Ignores attenuation */
1576 break;
1578 default:
1579 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1580 return WINED3DERR_INVALIDCALL;
1583 if (!(object = wined3d_state_get_light(device->update_state, light_idx)))
1585 TRACE("Adding new light\n");
1586 if (!(object = heap_alloc_zero(sizeof(*object))))
1587 return E_OUTOFMEMORY;
1589 list_add_head(&device->update_state->light_map[hash_idx], &object->entry);
1590 object->glIndex = -1;
1591 object->OriginalIndex = light_idx;
1594 /* Initialize the object. */
1595 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1596 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1597 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1598 light_idx, light->type, debug_color(&light->diffuse),
1599 debug_color(&light->specular), debug_color(&light->ambient),
1600 light->position.x, light->position.y, light->position.z,
1601 light->direction.x, light->direction.y, light->direction.z,
1602 light->range, light->falloff, light->theta, light->phi);
1604 /* Save away the information. */
1605 object->OriginalParms = *light;
1607 switch (light->type)
1609 case WINED3D_LIGHT_POINT:
1610 /* Position */
1611 object->position.x = light->position.x;
1612 object->position.y = light->position.y;
1613 object->position.z = light->position.z;
1614 object->position.w = 1.0f;
1615 object->cutoff = 180.0f;
1616 /* FIXME: Range */
1617 break;
1619 case WINED3D_LIGHT_DIRECTIONAL:
1620 /* Direction */
1621 object->direction.x = -light->direction.x;
1622 object->direction.y = -light->direction.y;
1623 object->direction.z = -light->direction.z;
1624 object->direction.w = 0.0f;
1625 object->exponent = 0.0f;
1626 object->cutoff = 180.0f;
1627 break;
1629 case WINED3D_LIGHT_SPOT:
1630 /* Position */
1631 object->position.x = light->position.x;
1632 object->position.y = light->position.y;
1633 object->position.z = light->position.z;
1634 object->position.w = 1.0f;
1636 /* Direction */
1637 object->direction.x = light->direction.x;
1638 object->direction.y = light->direction.y;
1639 object->direction.z = light->direction.z;
1640 object->direction.w = 0.0f;
1642 /* opengl-ish and d3d-ish spot lights use too different models
1643 * for the light "intensity" as a function of the angle towards
1644 * the main light direction, so we only can approximate very
1645 * roughly. However, spot lights are rather rarely used in games
1646 * (if ever used at all). Furthermore if still used, probably
1647 * nobody pays attention to such details. */
1648 if (!light->falloff)
1650 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1651 * equations have the falloff resp. exponent parameter as an
1652 * exponent, so the spot light lighting will always be 1.0 for
1653 * both of them, and we don't have to care for the rest of the
1654 * rather complex calculation. */
1655 object->exponent = 0.0f;
1657 else
1659 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1660 if (rho < 0.0001f)
1661 rho = 0.0001f;
1662 object->exponent = -0.3f / logf(cosf(rho / 2));
1665 if (object->exponent > 128.0f)
1666 object->exponent = 128.0f;
1668 object->cutoff = (float)(light->phi * 90 / M_PI);
1669 /* FIXME: Range */
1670 break;
1672 case WINED3D_LIGHT_PARALLELPOINT:
1673 object->position.x = light->position.x;
1674 object->position.y = light->position.y;
1675 object->position.z = light->position.z;
1676 object->position.w = 1.0f;
1677 break;
1679 default:
1680 FIXME("Unrecognized light type %#x.\n", light->type);
1683 if (!device->recording)
1684 wined3d_cs_emit_set_light(device->cs, object);
1686 return WINED3D_OK;
1689 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1690 UINT light_idx, struct wined3d_light *light)
1692 struct wined3d_light_info *light_info;
1694 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1696 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1698 TRACE("Light information requested but light not defined\n");
1699 return WINED3DERR_INVALIDCALL;
1702 *light = light_info->OriginalParms;
1703 return WINED3D_OK;
1706 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1708 struct wined3d_light_info *light_info;
1710 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1712 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1713 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1715 TRACE("Light enabled requested but light not defined, so defining one!\n");
1716 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1718 if (!(light_info = wined3d_state_get_light(device->update_state, light_idx)))
1720 FIXME("Adding default lights has failed dismally\n");
1721 return WINED3DERR_INVALIDCALL;
1725 wined3d_state_enable_light(device->update_state, &device->adapter->d3d_info, light_info, enable);
1726 if (!device->recording)
1727 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1729 return WINED3D_OK;
1732 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1734 struct wined3d_light_info *light_info;
1736 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1738 if (!(light_info = wined3d_state_get_light(&device->state, light_idx)))
1740 TRACE("Light enabled state requested but light not defined.\n");
1741 return WINED3DERR_INVALIDCALL;
1743 /* true is 128 according to SetLightEnable */
1744 *enable = light_info->enabled ? 128 : 0;
1745 return WINED3D_OK;
1748 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
1749 UINT plane_idx, const struct wined3d_vec4 *plane)
1751 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1753 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1755 TRACE("Application has requested clipplane this device doesn't support.\n");
1756 return WINED3DERR_INVALIDCALL;
1759 if (device->recording)
1760 device->recording->changed.clipplane |= 1u << plane_idx;
1762 if (!memcmp(&device->update_state->clip_planes[plane_idx], plane, sizeof(*plane)))
1764 TRACE("Application is setting old values over, nothing to do.\n");
1765 return WINED3D_OK;
1768 device->update_state->clip_planes[plane_idx] = *plane;
1770 if (!device->recording)
1771 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1773 return WINED3D_OK;
1776 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
1777 UINT plane_idx, struct wined3d_vec4 *plane)
1779 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1781 if (plane_idx >= device->adapter->gl_info.limits.user_clip_distances)
1783 TRACE("Application has requested clipplane this device doesn't support.\n");
1784 return WINED3DERR_INVALIDCALL;
1787 *plane = device->state.clip_planes[plane_idx];
1789 return WINED3D_OK;
1792 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1793 const struct wined3d_clip_status *clip_status)
1795 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1797 if (!clip_status)
1798 return WINED3DERR_INVALIDCALL;
1800 return WINED3D_OK;
1803 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1804 struct wined3d_clip_status *clip_status)
1806 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1808 if (!clip_status)
1809 return WINED3DERR_INVALIDCALL;
1811 return WINED3D_OK;
1814 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1816 TRACE("device %p, material %p.\n", device, material);
1818 device->update_state->material = *material;
1820 if (device->recording)
1821 device->recording->changed.material = TRUE;
1822 else
1823 wined3d_cs_emit_set_material(device->cs, material);
1826 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
1828 TRACE("device %p, material %p.\n", device, material);
1830 *material = device->state.material;
1832 TRACE("diffuse %s\n", debug_color(&material->diffuse));
1833 TRACE("ambient %s\n", debug_color(&material->ambient));
1834 TRACE("specular %s\n", debug_color(&material->specular));
1835 TRACE("emissive %s\n", debug_color(&material->emissive));
1836 TRACE("power %.8e.\n", material->power);
1839 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1840 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1842 enum wined3d_format_id prev_format;
1843 struct wined3d_buffer *prev_buffer;
1844 unsigned int prev_offset;
1846 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1847 device, buffer, debug_d3dformat(format_id), offset);
1849 prev_buffer = device->update_state->index_buffer;
1850 prev_format = device->update_state->index_format;
1851 prev_offset = device->update_state->index_offset;
1853 device->update_state->index_buffer = buffer;
1854 device->update_state->index_format = format_id;
1855 device->update_state->index_offset = offset;
1857 if (device->recording)
1858 device->recording->changed.indices = TRUE;
1860 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1861 return;
1863 if (buffer)
1864 wined3d_buffer_incref(buffer);
1865 if (!device->recording)
1866 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1867 if (prev_buffer)
1868 wined3d_buffer_decref(prev_buffer);
1871 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1872 enum wined3d_format_id *format, unsigned int *offset)
1874 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1876 *format = device->state.index_format;
1877 if (offset)
1878 *offset = device->state.index_offset;
1879 return device->state.index_buffer;
1882 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1884 TRACE("device %p, base_index %d.\n", device, base_index);
1886 device->update_state->base_vertex_index = base_index;
1889 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
1891 TRACE("device %p.\n", device);
1893 return device->state.base_vertex_index;
1896 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
1898 TRACE("device %p, viewport %p.\n", device, viewport);
1899 TRACE("x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n",
1900 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
1902 device->update_state->viewport = *viewport;
1904 /* Handle recording of state blocks */
1905 if (device->recording)
1907 TRACE("Recording... not performing anything\n");
1908 device->recording->changed.viewport = TRUE;
1909 return;
1912 wined3d_cs_emit_set_viewport(device->cs, viewport);
1915 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
1917 TRACE("device %p, viewport %p.\n", device, viewport);
1919 *viewport = device->state.viewport;
1922 static void resolve_depth_buffer(struct wined3d_device *device)
1924 const struct wined3d_state *state = &device->state;
1925 struct wined3d_rendertarget_view *src_view;
1926 struct wined3d_resource *dst_resource;
1927 struct wined3d_texture *dst_texture;
1929 if (!(dst_texture = state->textures[0]))
1930 return;
1931 dst_resource = &dst_texture->resource;
1932 if (!(dst_resource->format_flags & WINED3DFMT_FLAG_DEPTH))
1933 return;
1934 if (!(src_view = state->fb->depth_stencil))
1935 return;
1937 wined3d_device_resolve_sub_resource(device, dst_resource, 0,
1938 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1941 void CDECL wined3d_device_set_blend_state(struct wined3d_device *device, struct wined3d_blend_state *blend_state)
1943 struct wined3d_blend_state *prev;
1945 TRACE("device %p, blend_state %p.\n", device, blend_state);
1947 prev = device->update_state->blend_state;
1948 if (prev == blend_state)
1949 return;
1951 if (blend_state)
1952 wined3d_blend_state_incref(blend_state);
1953 device->update_state->blend_state = blend_state;
1954 wined3d_cs_emit_set_blend_state(device->cs, blend_state);
1955 if (prev)
1956 wined3d_blend_state_decref(prev);
1959 struct wined3d_blend_state * CDECL wined3d_device_get_blend_state(const struct wined3d_device *device)
1961 TRACE("device %p.\n", device);
1963 return device->state.blend_state;
1966 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1967 struct wined3d_rasterizer_state *rasterizer_state)
1969 struct wined3d_rasterizer_state *prev;
1971 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
1973 prev = device->update_state->rasterizer_state;
1974 if (prev == rasterizer_state)
1975 return;
1977 if (rasterizer_state)
1978 wined3d_rasterizer_state_incref(rasterizer_state);
1979 device->update_state->rasterizer_state = rasterizer_state;
1980 wined3d_cs_emit_set_rasterizer_state(device->cs, rasterizer_state);
1981 if (prev)
1982 wined3d_rasterizer_state_decref(prev);
1985 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
1987 TRACE("device %p.\n", device);
1989 return device->state.rasterizer_state;
1992 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1993 enum wined3d_render_state state, DWORD value)
1995 DWORD old_value;
1997 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1999 if (state > WINEHIGHEST_RENDER_STATE)
2001 WARN("Unhandled render state %#x.\n", state);
2002 return;
2005 old_value = device->state.render_states[state];
2006 device->update_state->render_states[state] = value;
2008 /* Handle recording of state blocks. */
2009 if (device->recording)
2011 TRACE("Recording... not performing anything.\n");
2012 device->recording->changed.renderState[state >> 5] |= 1u << (state & 0x1f);
2013 return;
2016 /* Compared here and not before the assignment to allow proper stateblock recording. */
2017 if (value == old_value)
2018 TRACE("Application is setting the old value over, nothing to do.\n");
2019 else
2020 wined3d_cs_emit_set_render_state(device->cs, state, value);
2022 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2024 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2025 resolve_depth_buffer(device);
2029 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2031 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2033 return device->state.render_states[state];
2036 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2037 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2039 DWORD old_value;
2041 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2042 device, sampler_idx, debug_d3dsamplerstate(state), value);
2044 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2045 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2047 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2049 WARN("Invalid sampler %u.\n", sampler_idx);
2050 return; /* Windows accepts overflowing this array ... we do not. */
2053 old_value = device->state.sampler_states[sampler_idx][state];
2054 device->update_state->sampler_states[sampler_idx][state] = value;
2056 /* Handle recording of state blocks. */
2057 if (device->recording)
2059 TRACE("Recording... not performing anything.\n");
2060 device->recording->changed.samplerState[sampler_idx] |= 1u << state;
2061 return;
2064 if (old_value == value)
2066 TRACE("Application is setting the old value over, nothing to do.\n");
2067 return;
2070 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
2073 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2074 UINT sampler_idx, enum wined3d_sampler_state state)
2076 TRACE("device %p, sampler_idx %u, state %s.\n",
2077 device, sampler_idx, debug_d3dsamplerstate(state));
2079 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2080 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2082 if (sampler_idx >= ARRAY_SIZE(device->state.sampler_states))
2084 WARN("Invalid sampler %u.\n", sampler_idx);
2085 return 0; /* Windows accepts overflowing this array ... we do not. */
2088 return device->state.sampler_states[sampler_idx][state];
2091 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2093 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2095 if (device->recording)
2096 device->recording->changed.scissorRect = TRUE;
2098 if (EqualRect(&device->update_state->scissor_rect, rect))
2100 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2101 return;
2103 CopyRect(&device->update_state->scissor_rect, rect);
2105 if (device->recording)
2107 TRACE("Recording... not performing anything.\n");
2108 return;
2111 wined3d_cs_emit_set_scissor_rect(device->cs, rect);
2114 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2116 TRACE("device %p, rect %p.\n", device, rect);
2118 *rect = device->state.scissor_rect;
2119 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2122 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2123 struct wined3d_vertex_declaration *declaration)
2125 struct wined3d_vertex_declaration *prev = device->update_state->vertex_declaration;
2127 TRACE("device %p, declaration %p.\n", device, declaration);
2129 if (device->recording)
2130 device->recording->changed.vertexDecl = TRUE;
2132 if (declaration == prev)
2133 return;
2135 if (declaration)
2136 wined3d_vertex_declaration_incref(declaration);
2137 device->update_state->vertex_declaration = declaration;
2138 if (!device->recording)
2139 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2140 if (prev)
2141 wined3d_vertex_declaration_decref(prev);
2144 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2146 TRACE("device %p.\n", device);
2148 return device->state.vertex_declaration;
2151 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2153 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX];
2155 TRACE("device %p, shader %p.\n", device, shader);
2157 if (device->recording)
2158 device->recording->changed.vertexShader = TRUE;
2160 if (shader == prev)
2161 return;
2163 if (shader)
2164 wined3d_shader_incref(shader);
2165 device->update_state->shader[WINED3D_SHADER_TYPE_VERTEX] = shader;
2166 if (!device->recording)
2167 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_VERTEX, shader);
2168 if (prev)
2169 wined3d_shader_decref(prev);
2172 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2174 TRACE("device %p.\n", device);
2176 return device->state.shader[WINED3D_SHADER_TYPE_VERTEX];
2179 static void wined3d_device_set_constant_buffer(struct wined3d_device *device,
2180 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2182 struct wined3d_buffer *prev;
2184 if (idx >= MAX_CONSTANT_BUFFERS)
2186 WARN("Invalid constant buffer index %u.\n", idx);
2187 return;
2190 prev = device->update_state->cb[type][idx];
2191 if (buffer == prev)
2192 return;
2194 if (buffer)
2195 wined3d_buffer_incref(buffer);
2196 device->update_state->cb[type][idx] = buffer;
2197 if (!device->recording)
2198 wined3d_cs_emit_set_constant_buffer(device->cs, type, idx, buffer);
2199 if (prev)
2200 wined3d_buffer_decref(prev);
2203 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2205 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2207 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx, buffer);
2210 static struct wined3d_buffer *wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2211 enum wined3d_shader_type shader_type, unsigned int idx)
2213 if (idx >= MAX_CONSTANT_BUFFERS)
2215 WARN("Invalid constant buffer index %u.\n", idx);
2216 return NULL;
2219 return device->state.cb[shader_type][idx];
2222 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2224 TRACE("device %p, idx %u.\n", device, idx);
2226 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2229 static void wined3d_device_set_shader_resource_view(struct wined3d_device *device,
2230 enum wined3d_shader_type type, UINT idx, struct wined3d_shader_resource_view *view)
2232 struct wined3d_shader_resource_view *prev;
2234 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2236 WARN("Invalid view index %u.\n", idx);
2237 return;
2240 prev = device->update_state->shader_resource_view[type][idx];
2241 if (view == prev)
2242 return;
2244 if (view)
2245 wined3d_shader_resource_view_incref(view);
2246 device->update_state->shader_resource_view[type][idx] = view;
2247 if (!device->recording)
2248 wined3d_cs_emit_set_shader_resource_view(device->cs, type, idx, view);
2249 if (prev)
2250 wined3d_shader_resource_view_decref(prev);
2253 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2254 UINT idx, struct wined3d_shader_resource_view *view)
2256 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2258 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2261 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2262 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2264 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2266 WARN("Invalid view index %u.\n", idx);
2267 return NULL;
2270 return device->state.shader_resource_view[shader_type][idx];
2273 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2274 UINT idx)
2276 TRACE("device %p, idx %u.\n", device, idx);
2278 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2281 static void wined3d_device_set_sampler(struct wined3d_device *device,
2282 enum wined3d_shader_type type, UINT idx, struct wined3d_sampler *sampler)
2284 struct wined3d_sampler *prev;
2286 if (idx >= MAX_SAMPLER_OBJECTS)
2288 WARN("Invalid sampler index %u.\n", idx);
2289 return;
2292 prev = device->update_state->sampler[type][idx];
2293 if (sampler == prev)
2294 return;
2296 if (sampler)
2297 wined3d_sampler_incref(sampler);
2298 device->update_state->sampler[type][idx] = sampler;
2299 if (!device->recording)
2300 wined3d_cs_emit_set_sampler(device->cs, type, idx, sampler);
2301 if (prev)
2302 wined3d_sampler_decref(prev);
2305 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2307 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2309 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2312 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2313 enum wined3d_shader_type shader_type, unsigned int idx)
2315 if (idx >= MAX_SAMPLER_OBJECTS)
2317 WARN("Invalid sampler index %u.\n", idx);
2318 return NULL;
2321 return device->state.sampler[shader_type][idx];
2324 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2326 TRACE("device %p, idx %u.\n", device, idx);
2328 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2331 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2332 unsigned int start_idx, unsigned int count, const BOOL *constants)
2334 unsigned int i;
2336 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2337 device, start_idx, count, constants);
2339 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2340 return WINED3DERR_INVALIDCALL;
2342 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2343 count = WINED3D_MAX_CONSTS_B - start_idx;
2344 memcpy(&device->update_state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2345 if (TRACE_ON(d3d))
2347 for (i = 0; i < count; ++i)
2348 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2351 if (device->recording)
2353 for (i = start_idx; i < count + start_idx; ++i)
2354 device->recording->changed.vertexShaderConstantsB |= (1u << i);
2356 else
2358 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2361 return WINED3D_OK;
2364 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2365 unsigned int start_idx, unsigned int count, BOOL *constants)
2367 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2368 device, start_idx, count, constants);
2370 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2371 return WINED3DERR_INVALIDCALL;
2373 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2374 count = WINED3D_MAX_CONSTS_B - start_idx;
2375 memcpy(constants, &device->state.vs_consts_b[start_idx], count * sizeof(*constants));
2377 return WINED3D_OK;
2380 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2381 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2383 unsigned int i;
2385 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2386 device, start_idx, count, constants);
2388 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2389 return WINED3DERR_INVALIDCALL;
2391 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2392 count = WINED3D_MAX_CONSTS_I - start_idx;
2393 memcpy(&device->update_state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2394 if (TRACE_ON(d3d))
2396 for (i = 0; i < count; ++i)
2397 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2400 if (device->recording)
2402 for (i = start_idx; i < count + start_idx; ++i)
2403 device->recording->changed.vertexShaderConstantsI |= (1u << i);
2405 else
2407 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2410 return WINED3D_OK;
2413 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2414 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2416 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2417 device, start_idx, count, constants);
2419 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2420 return WINED3DERR_INVALIDCALL;
2422 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2423 count = WINED3D_MAX_CONSTS_I - start_idx;
2424 memcpy(constants, &device->state.vs_consts_i[start_idx], count * sizeof(*constants));
2425 return WINED3D_OK;
2428 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2429 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2431 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2432 unsigned int i;
2434 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2435 device, start_idx, count, constants);
2437 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2438 || count > d3d_info->limits.vs_uniform_count - start_idx)
2439 return WINED3DERR_INVALIDCALL;
2441 memcpy(&device->update_state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2442 if (TRACE_ON(d3d))
2444 for (i = 0; i < count; ++i)
2445 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2448 if (device->recording)
2449 memset(&device->recording->changed.vs_consts_f[start_idx], 1,
2450 count * sizeof(*device->recording->changed.vs_consts_f));
2451 else
2452 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2454 return WINED3D_OK;
2457 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2458 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2460 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2462 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2463 device, start_idx, count, constants);
2465 if (!constants || start_idx >= d3d_info->limits.vs_uniform_count
2466 || count > d3d_info->limits.vs_uniform_count - start_idx)
2467 return WINED3DERR_INVALIDCALL;
2469 memcpy(constants, &device->state.vs_consts_f[start_idx], count * sizeof(*constants));
2471 return WINED3D_OK;
2474 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2476 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL];
2478 TRACE("device %p, shader %p.\n", device, shader);
2480 if (device->recording)
2481 device->recording->changed.pixelShader = TRUE;
2483 if (shader == prev)
2484 return;
2486 if (shader)
2487 wined3d_shader_incref(shader);
2488 device->update_state->shader[WINED3D_SHADER_TYPE_PIXEL] = shader;
2489 if (!device->recording)
2490 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_PIXEL, shader);
2491 if (prev)
2492 wined3d_shader_decref(prev);
2495 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2497 TRACE("device %p.\n", device);
2499 return device->state.shader[WINED3D_SHADER_TYPE_PIXEL];
2502 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2504 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2506 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx, buffer);
2509 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2511 TRACE("device %p, idx %u.\n", device, idx);
2513 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2516 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2517 UINT idx, struct wined3d_shader_resource_view *view)
2519 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2521 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2524 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2525 UINT idx)
2527 TRACE("device %p, idx %u.\n", device, idx);
2529 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2532 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2534 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2536 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2539 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2541 TRACE("device %p, idx %u.\n", device, idx);
2543 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2546 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2547 unsigned int start_idx, unsigned int count, const BOOL *constants)
2549 unsigned int i;
2551 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2552 device, start_idx, count, constants);
2554 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2555 return WINED3DERR_INVALIDCALL;
2557 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2558 count = WINED3D_MAX_CONSTS_B - start_idx;
2559 memcpy(&device->update_state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2560 if (TRACE_ON(d3d))
2562 for (i = 0; i < count; ++i)
2563 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2566 if (device->recording)
2568 for (i = start_idx; i < count + start_idx; ++i)
2569 device->recording->changed.pixelShaderConstantsB |= (1u << i);
2571 else
2573 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2576 return WINED3D_OK;
2579 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2580 unsigned int start_idx, unsigned int count, BOOL *constants)
2582 TRACE("device %p, start_idx %u, count %u,constants %p.\n",
2583 device, start_idx, count, constants);
2585 if (!constants || start_idx >= WINED3D_MAX_CONSTS_B)
2586 return WINED3DERR_INVALIDCALL;
2588 if (count > WINED3D_MAX_CONSTS_B - start_idx)
2589 count = WINED3D_MAX_CONSTS_B - start_idx;
2590 memcpy(constants, &device->state.ps_consts_b[start_idx], count * sizeof(*constants));
2592 return WINED3D_OK;
2595 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2596 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2598 unsigned int i;
2600 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2601 device, start_idx, count, constants);
2603 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2604 return WINED3DERR_INVALIDCALL;
2606 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2607 count = WINED3D_MAX_CONSTS_I - start_idx;
2608 memcpy(&device->update_state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2609 if (TRACE_ON(d3d))
2611 for (i = 0; i < count; ++i)
2612 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2615 if (device->recording)
2617 for (i = start_idx; i < count + start_idx; ++i)
2618 device->recording->changed.pixelShaderConstantsI |= (1u << i);
2620 else
2622 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2625 return WINED3D_OK;
2628 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2629 unsigned int start_idx, unsigned int count, struct wined3d_ivec4 *constants)
2631 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2632 device, start_idx, count, constants);
2634 if (!constants || start_idx >= WINED3D_MAX_CONSTS_I)
2635 return WINED3DERR_INVALIDCALL;
2637 if (count > WINED3D_MAX_CONSTS_I - start_idx)
2638 count = WINED3D_MAX_CONSTS_I - start_idx;
2639 memcpy(constants, &device->state.ps_consts_i[start_idx], count * sizeof(*constants));
2641 return WINED3D_OK;
2644 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2645 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2647 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2648 unsigned int i;
2650 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2651 device, start_idx, count, constants);
2653 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2654 || count > d3d_info->limits.ps_uniform_count - start_idx)
2655 return WINED3DERR_INVALIDCALL;
2657 memcpy(&device->update_state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2658 if (TRACE_ON(d3d))
2660 for (i = 0; i < count; ++i)
2661 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2664 if (device->recording)
2665 memset(&device->recording->changed.ps_consts_f[start_idx], 1,
2666 count * sizeof(*device->recording->changed.ps_consts_f));
2667 else
2668 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2670 return WINED3D_OK;
2673 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2674 unsigned int start_idx, unsigned int count, struct wined3d_vec4 *constants)
2676 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2678 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2679 device, start_idx, count, constants);
2681 if (!constants || start_idx >= d3d_info->limits.ps_uniform_count
2682 || count > d3d_info->limits.ps_uniform_count - start_idx)
2683 return WINED3DERR_INVALIDCALL;
2685 memcpy(constants, &device->state.ps_consts_f[start_idx], count * sizeof(*constants));
2687 return WINED3D_OK;
2690 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2692 struct wined3d_shader *prev;
2694 TRACE("device %p, shader %p.\n", device, shader);
2696 prev = device->update_state->shader[WINED3D_SHADER_TYPE_HULL];
2697 if (shader == prev)
2698 return;
2699 if (shader)
2700 wined3d_shader_incref(shader);
2701 device->update_state->shader[WINED3D_SHADER_TYPE_HULL] = shader;
2702 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_HULL, shader);
2703 if (prev)
2704 wined3d_shader_decref(prev);
2707 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2709 TRACE("device %p.\n", device);
2711 return device->state.shader[WINED3D_SHADER_TYPE_HULL];
2714 void CDECL wined3d_device_set_hs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2716 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2718 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_HULL, idx, buffer);
2721 struct wined3d_buffer * CDECL wined3d_device_get_hs_cb(const struct wined3d_device *device, unsigned int idx)
2723 TRACE("device %p, idx %u.\n", device, idx);
2725 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_HULL, idx);
2728 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2729 unsigned int idx, struct wined3d_shader_resource_view *view)
2731 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2733 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx, view);
2736 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2737 unsigned int idx)
2739 TRACE("device %p, idx %u.\n", device, idx);
2741 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2744 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2745 unsigned int idx, struct wined3d_sampler *sampler)
2747 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2749 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2752 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2754 TRACE("device %p, idx %u.\n", device, idx);
2756 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2759 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2761 struct wined3d_shader *prev;
2763 TRACE("device %p, shader %p.\n", device, shader);
2765 prev = device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2766 if (shader == prev)
2767 return;
2768 if (shader)
2769 wined3d_shader_incref(shader);
2770 device->update_state->shader[WINED3D_SHADER_TYPE_DOMAIN] = shader;
2771 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_DOMAIN, shader);
2772 if (prev)
2773 wined3d_shader_decref(prev);
2776 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2778 TRACE("device %p.\n", device);
2780 return device->state.shader[WINED3D_SHADER_TYPE_DOMAIN];
2783 void CDECL wined3d_device_set_ds_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2785 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2787 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_DOMAIN, idx, buffer);
2790 struct wined3d_buffer * CDECL wined3d_device_get_ds_cb(const struct wined3d_device *device, unsigned int idx)
2792 TRACE("device %p, idx %u.\n", device, idx);
2794 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2797 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2798 unsigned int idx, struct wined3d_shader_resource_view *view)
2800 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2802 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2805 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2806 unsigned int idx)
2808 TRACE("device %p, idx %u.\n", device, idx);
2810 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2813 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2814 unsigned int idx, struct wined3d_sampler *sampler)
2816 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2818 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2821 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2823 TRACE("device %p, idx %u.\n", device, idx);
2825 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2828 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2830 struct wined3d_shader *prev = device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2832 TRACE("device %p, shader %p.\n", device, shader);
2834 if (device->recording || shader == prev)
2835 return;
2836 if (shader)
2837 wined3d_shader_incref(shader);
2838 device->update_state->shader[WINED3D_SHADER_TYPE_GEOMETRY] = shader;
2839 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2840 if (prev)
2841 wined3d_shader_decref(prev);
2844 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2846 TRACE("device %p.\n", device);
2848 return device->state.shader[WINED3D_SHADER_TYPE_GEOMETRY];
2851 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2853 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2855 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, buffer);
2858 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
2860 TRACE("device %p, idx %u.\n", device, idx);
2862 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2865 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2866 UINT idx, struct wined3d_shader_resource_view *view)
2868 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2870 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2873 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2874 UINT idx)
2876 TRACE("device %p, idx %u.\n", device, idx);
2878 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2881 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2883 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2885 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2888 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2890 TRACE("device %p, idx %u.\n", device, idx);
2892 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2895 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2897 struct wined3d_shader *prev;
2899 TRACE("device %p, shader %p.\n", device, shader);
2901 prev = device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2902 if (device->recording || shader == prev)
2903 return;
2904 if (shader)
2905 wined3d_shader_incref(shader);
2906 device->update_state->shader[WINED3D_SHADER_TYPE_COMPUTE] = shader;
2907 wined3d_cs_emit_set_shader(device->cs, WINED3D_SHADER_TYPE_COMPUTE, shader);
2908 if (prev)
2909 wined3d_shader_decref(prev);
2912 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2914 TRACE("device %p.\n", device);
2916 return device->state.shader[WINED3D_SHADER_TYPE_COMPUTE];
2919 void CDECL wined3d_device_set_cs_cb(struct wined3d_device *device, unsigned int idx, struct wined3d_buffer *buffer)
2921 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2923 wined3d_device_set_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx, buffer);
2926 struct wined3d_buffer * CDECL wined3d_device_get_cs_cb(const struct wined3d_device *device, unsigned int idx)
2928 TRACE("device %p, idx %u.\n", device, idx);
2930 return wined3d_device_get_constant_buffer(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2933 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2934 unsigned int idx, struct wined3d_shader_resource_view *view)
2936 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2938 wined3d_device_set_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2941 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2942 unsigned int idx)
2944 TRACE("device %p, idx %u.\n", device, idx);
2946 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2949 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2950 unsigned int idx, struct wined3d_sampler *sampler)
2952 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2954 wined3d_device_set_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2957 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2959 TRACE("device %p, idx %u.\n", device, idx);
2961 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2964 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device *device,
2965 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2966 unsigned int initial_count)
2968 struct wined3d_unordered_access_view *prev;
2970 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2972 WARN("Invalid UAV index %u.\n", idx);
2973 return;
2976 prev = device->update_state->unordered_access_view[pipeline][idx];
2977 if (uav == prev && initial_count == ~0u)
2978 return;
2980 if (uav)
2981 wined3d_unordered_access_view_incref(uav);
2982 device->update_state->unordered_access_view[pipeline][idx] = uav;
2983 if (!device->recording)
2984 wined3d_cs_emit_set_unordered_access_view(device->cs, pipeline, idx, uav, initial_count);
2985 if (prev)
2986 wined3d_unordered_access_view_decref(prev);
2989 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2990 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2992 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2994 WARN("Invalid UAV index %u.\n", idx);
2995 return NULL;
2998 return device->state.unordered_access_view[pipeline][idx];
3001 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
3002 struct wined3d_unordered_access_view *uav, unsigned int initial_count)
3004 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
3006 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
3009 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
3010 unsigned int idx)
3012 TRACE("device %p, idx %u.\n", device, idx);
3014 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
3017 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
3018 unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
3020 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
3022 wined3d_device_set_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
3025 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
3026 const struct wined3d_device *device, unsigned int idx)
3028 TRACE("device %p, idx %u.\n", device, idx);
3030 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
3033 /* Context activation is done by the caller. */
3034 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3035 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3036 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3037 DWORD DestFVF)
3039 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3040 struct wined3d_map_desc map_desc;
3041 struct wined3d_box box = {0};
3042 struct wined3d_viewport vp;
3043 UINT vertex_size;
3044 unsigned int i;
3045 BYTE *dest_ptr;
3046 BOOL doClip;
3047 DWORD numTextures;
3048 HRESULT hr;
3050 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3052 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3055 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3057 ERR("Source has no position mask\n");
3058 return WINED3DERR_INVALIDCALL;
3061 if (device->state.render_states[WINED3D_RS_CLIPPING])
3063 static BOOL warned = FALSE;
3065 * The clipping code is not quite correct. Some things need
3066 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3067 * so disable clipping for now.
3068 * (The graphics in Half-Life are broken, and my processvertices
3069 * test crashes with IDirect3DDevice3)
3070 doClip = TRUE;
3072 doClip = FALSE;
3073 if(!warned) {
3074 warned = TRUE;
3075 FIXME("Clipping is broken and disabled for now\n");
3078 else
3079 doClip = FALSE;
3081 vertex_size = get_flexible_vertex_size(DestFVF);
3082 box.left = dwDestIndex * vertex_size;
3083 box.right = box.left + dwCount * vertex_size;
3084 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3086 WARN("Failed to map buffer, hr %#x.\n", hr);
3087 return hr;
3089 dest_ptr = map_desc.data;
3091 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3092 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3093 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3095 TRACE("View mat:\n");
3096 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3097 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3098 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3099 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3101 TRACE("Proj mat:\n");
3102 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3103 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3104 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3105 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3107 TRACE("World mat:\n");
3108 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3109 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3110 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3111 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3113 /* Get the viewport */
3114 wined3d_device_get_viewport(device, &vp);
3115 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3116 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3118 multiply_matrix(&mat,&view_mat,&world_mat);
3119 multiply_matrix(&mat,&proj_mat,&mat);
3121 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3123 for (i = 0; i < dwCount; i+= 1) {
3124 unsigned int tex_index;
3126 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3127 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3128 /* The position first */
3129 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3130 const float *p = (const float *)(element->data.addr + i * element->stride);
3131 float x, y, z, rhw;
3132 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3134 /* Multiplication with world, view and projection matrix. */
3135 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3136 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3137 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3138 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3140 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3142 /* WARNING: The following things are taken from d3d7 and were not yet checked
3143 * against d3d8 or d3d9!
3146 /* Clipping conditions: From msdn
3148 * A vertex is clipped if it does not match the following requirements
3149 * -rhw < x <= rhw
3150 * -rhw < y <= rhw
3151 * 0 < z <= rhw
3152 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3154 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3155 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3159 if( !doClip ||
3160 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3161 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3162 ( rhw > eps ) ) ) {
3164 /* "Normal" viewport transformation (not clipped)
3165 * 1) The values are divided by rhw
3166 * 2) The y axis is negative, so multiply it with -1
3167 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3168 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3169 * 4) Multiply x with Width/2 and add Width/2
3170 * 5) The same for the height
3171 * 6) Add the viewpoint X and Y to the 2D coordinates and
3172 * The minimum Z value to z
3173 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3175 * Well, basically it's simply a linear transformation into viewport
3176 * coordinates
3179 x /= rhw;
3180 y /= rhw;
3181 z /= rhw;
3183 y *= -1;
3185 x *= vp.width / 2;
3186 y *= vp.height / 2;
3187 z *= vp.max_z - vp.min_z;
3189 x += vp.width / 2 + vp.x;
3190 y += vp.height / 2 + vp.y;
3191 z += vp.min_z;
3193 rhw = 1 / rhw;
3194 } else {
3195 /* That vertex got clipped
3196 * Contrary to OpenGL it is not dropped completely, it just
3197 * undergoes a different calculation.
3199 TRACE("Vertex got clipped\n");
3200 x += rhw;
3201 y += rhw;
3203 x /= 2;
3204 y /= 2;
3206 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3207 * outside of the main vertex buffer memory. That needs some more
3208 * investigation...
3212 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3215 ( (float *) dest_ptr)[0] = x;
3216 ( (float *) dest_ptr)[1] = y;
3217 ( (float *) dest_ptr)[2] = z;
3218 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3220 dest_ptr += 3 * sizeof(float);
3222 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3223 dest_ptr += sizeof(float);
3226 if (DestFVF & WINED3DFVF_PSIZE)
3227 dest_ptr += sizeof(DWORD);
3229 if (DestFVF & WINED3DFVF_NORMAL)
3231 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3232 const float *normal = (const float *)(element->data.addr + i * element->stride);
3233 /* AFAIK this should go into the lighting information */
3234 FIXME("Didn't expect the destination to have a normal\n");
3235 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3238 if (DestFVF & WINED3DFVF_DIFFUSE)
3240 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3241 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3242 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3244 static BOOL warned = FALSE;
3246 if(!warned) {
3247 ERR("No diffuse color in source, but destination has one\n");
3248 warned = TRUE;
3251 *( (DWORD *) dest_ptr) = 0xffffffff;
3252 dest_ptr += sizeof(DWORD);
3254 else
3256 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3260 if (DestFVF & WINED3DFVF_SPECULAR)
3262 /* What's the color value in the feedback buffer? */
3263 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3264 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3265 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3267 static BOOL warned = FALSE;
3269 if(!warned) {
3270 ERR("No specular color in source, but destination has one\n");
3271 warned = TRUE;
3274 *(DWORD *)dest_ptr = 0xff000000;
3275 dest_ptr += sizeof(DWORD);
3277 else
3279 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3283 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3285 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3286 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3287 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3289 ERR("No source texture, but destination requests one\n");
3290 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3292 else
3294 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3299 wined3d_resource_unmap(&dest->resource, 0);
3301 return WINED3D_OK;
3303 #undef copy_and_next
3305 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3306 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3307 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3309 struct wined3d_state *state = &device->state;
3310 struct wined3d_stream_info stream_info;
3311 struct wined3d_resource *resource;
3312 struct wined3d_box box = {0};
3313 struct wined3d_shader *vs;
3314 unsigned int i;
3315 HRESULT hr;
3316 WORD map;
3318 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3319 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3320 device, src_start_idx, dst_idx, vertex_count,
3321 dst_buffer, declaration, flags, dst_fvf);
3323 if (declaration)
3324 FIXME("Output vertex declaration not implemented yet.\n");
3326 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3327 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3328 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->gl_info, &device->adapter->d3d_info);
3329 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3331 /* We can't convert FROM a VBO, and vertex buffers used to source into
3332 * process_vertices() are unlikely to ever be used for drawing. Release
3333 * VBOs in those buffers and fix up the stream_info structure.
3335 * Also apply the start index. */
3336 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3338 struct wined3d_stream_info_element *e;
3339 struct wined3d_map_desc map_desc;
3341 if (!(map & 1))
3342 continue;
3344 e = &stream_info.elements[i];
3345 resource = &state->streams[e->stream_idx].buffer->resource;
3346 box.left = src_start_idx * e->stride;
3347 box.right = box.left + vertex_count * e->stride;
3348 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3349 ERR("Failed to map resource.\n");
3350 e->data.buffer_object = 0;
3351 e->data.addr += (ULONG_PTR)map_desc.data;
3354 hr = process_vertices_strided(device, dst_idx, vertex_count,
3355 &stream_info, dst_buffer, flags, dst_fvf);
3357 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3359 if (!(map & 1))
3360 continue;
3362 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3363 if (FAILED(wined3d_resource_unmap(resource, 0)))
3364 ERR("Failed to unmap resource.\n");
3367 return hr;
3370 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3371 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3373 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3374 DWORD old_value;
3376 TRACE("device %p, stage %u, state %s, value %#x.\n",
3377 device, stage, debug_d3dtexturestate(state), value);
3379 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3381 WARN("Invalid state %#x passed.\n", state);
3382 return;
3385 if (stage >= d3d_info->limits.ffp_blend_stages)
3387 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3388 stage, d3d_info->limits.ffp_blend_stages - 1);
3389 return;
3392 old_value = device->update_state->texture_states[stage][state];
3393 device->update_state->texture_states[stage][state] = value;
3395 if (device->recording)
3397 TRACE("Recording... not performing anything.\n");
3398 device->recording->changed.textureState[stage] |= 1u << state;
3399 return;
3402 /* Checked after the assignments to allow proper stateblock recording. */
3403 if (old_value == value)
3405 TRACE("Application is setting the old value over, nothing to do.\n");
3406 return;
3409 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3412 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3413 UINT stage, enum wined3d_texture_stage_state state)
3415 TRACE("device %p, stage %u, state %s.\n",
3416 device, stage, debug_d3dtexturestate(state));
3418 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3420 WARN("Invalid state %#x passed.\n", state);
3421 return 0;
3424 return device->state.texture_states[stage][state];
3427 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3428 UINT stage, struct wined3d_texture *texture)
3430 struct wined3d_texture *prev;
3432 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3434 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3435 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3437 /* Windows accepts overflowing this array... we do not. */
3438 if (stage >= ARRAY_SIZE(device->state.textures))
3440 WARN("Ignoring invalid stage %u.\n", stage);
3441 return WINED3D_OK;
3444 if (texture && texture->resource.usage & WINED3DUSAGE_SCRATCH)
3446 WARN("Rejecting attempt to set scratch texture.\n");
3447 return WINED3DERR_INVALIDCALL;
3450 if (device->recording)
3451 device->recording->changed.textures |= 1u << stage;
3453 prev = device->update_state->textures[stage];
3454 TRACE("Previous texture %p.\n", prev);
3456 if (texture == prev)
3458 TRACE("App is setting the same texture again, nothing to do.\n");
3459 return WINED3D_OK;
3462 TRACE("Setting new texture to %p.\n", texture);
3463 device->update_state->textures[stage] = texture;
3465 if (texture)
3466 wined3d_texture_incref(texture);
3467 if (!device->recording)
3468 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3469 if (prev)
3470 wined3d_texture_decref(prev);
3472 return WINED3D_OK;
3475 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3477 TRACE("device %p, stage %u.\n", device, stage);
3479 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3480 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3482 if (stage >= ARRAY_SIZE(device->state.textures))
3484 WARN("Ignoring invalid stage %u.\n", stage);
3485 return NULL; /* Windows accepts overflowing this array ... we do not. */
3488 return device->state.textures[stage];
3491 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3493 TRACE("device %p, caps %p.\n", device, caps);
3495 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3496 device->create_parms.device_type, caps);
3499 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3500 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3502 struct wined3d_swapchain *swapchain;
3504 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3505 device, swapchain_idx, mode, rotation);
3507 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3508 return WINED3DERR_INVALIDCALL;
3510 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3513 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3515 struct wined3d_stateblock *stateblock;
3516 HRESULT hr;
3518 TRACE("device %p.\n", device);
3520 if (device->recording)
3521 return WINED3DERR_INVALIDCALL;
3523 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3524 if (FAILED(hr))
3525 return hr;
3527 device->recording = stateblock;
3528 device->update_state = &stateblock->state;
3530 TRACE("Recording stateblock %p.\n", stateblock);
3532 return WINED3D_OK;
3535 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3536 struct wined3d_stateblock **stateblock)
3538 struct wined3d_stateblock *object = device->recording;
3540 TRACE("device %p, stateblock %p.\n", device, stateblock);
3542 if (!device->recording)
3544 WARN("Not recording.\n");
3545 *stateblock = NULL;
3546 return WINED3DERR_INVALIDCALL;
3549 stateblock_init_contained_states(object);
3551 *stateblock = object;
3552 device->recording = NULL;
3553 device->update_state = &device->state;
3555 TRACE("Returning stateblock %p.\n", *stateblock);
3557 return WINED3D_OK;
3560 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3562 /* At the moment we have no need for any functionality at the beginning
3563 * of a scene. */
3564 TRACE("device %p.\n", device);
3566 if (device->inScene)
3568 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3569 return WINED3DERR_INVALIDCALL;
3571 device->inScene = TRUE;
3572 return WINED3D_OK;
3575 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3577 TRACE("device %p.\n", device);
3579 if (!device->inScene)
3581 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3582 return WINED3DERR_INVALIDCALL;
3585 wined3d_cs_emit_flush(device->cs);
3587 device->inScene = FALSE;
3588 return WINED3D_OK;
3591 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3592 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3594 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
3595 device, rect_count, rects, flags, debug_color(color), depth, stencil);
3597 if (!rect_count && rects)
3599 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3600 return WINED3D_OK;
3603 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3605 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3606 if (!ds)
3608 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3609 /* TODO: What about depth stencil buffers without stencil bits? */
3610 return WINED3DERR_INVALIDCALL;
3612 else if (flags & WINED3DCLEAR_TARGET)
3614 if (ds->width < device->fb.render_targets[0]->width
3615 || ds->height < device->fb.render_targets[0]->height)
3617 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3618 return WINED3D_OK;
3623 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
3625 return WINED3D_OK;
3628 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
3629 struct wined3d_query *predicate, BOOL value)
3631 struct wined3d_query *prev;
3633 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
3635 prev = device->update_state->predicate;
3636 if (predicate)
3638 FIXME("Predicated rendering not implemented.\n");
3639 wined3d_query_incref(predicate);
3641 device->update_state->predicate = predicate;
3642 device->update_state->predicate_value = value;
3643 if (!device->recording)
3644 wined3d_cs_emit_set_predication(device->cs, predicate, value);
3645 if (prev)
3646 wined3d_query_decref(prev);
3649 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
3651 TRACE("device %p, value %p.\n", device, value);
3653 if (value)
3654 *value = device->state.predicate_value;
3655 return device->state.predicate;
3658 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
3659 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
3661 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
3662 device, group_count_x, group_count_y, group_count_z);
3664 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
3667 void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
3668 struct wined3d_buffer *buffer, unsigned int offset)
3670 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3672 wined3d_cs_emit_dispatch_indirect(device->cs, buffer, offset);
3675 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3676 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
3678 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
3679 device, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
3681 device->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3682 device->state.gl_patch_vertices = patch_vertex_count;
3685 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3686 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
3688 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
3689 device, primitive_type, patch_vertex_count);
3691 *primitive_type = d3d_primitive_type_from_gl(device->state.gl_primitive_type);
3692 if (patch_vertex_count)
3693 *patch_vertex_count = device->state.gl_patch_vertices;
3695 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
3698 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3700 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3702 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3703 0, start_vertex, vertex_count, 0, 0, FALSE);
3705 return WINED3D_OK;
3708 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
3709 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
3711 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
3712 device, start_vertex, vertex_count, start_instance, instance_count);
3714 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3715 0, start_vertex, vertex_count, start_instance, instance_count, FALSE);
3718 void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
3719 struct wined3d_buffer *buffer, unsigned int offset)
3721 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3723 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3724 buffer, offset, FALSE);
3727 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3729 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3731 if (!device->state.index_buffer)
3733 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3734 * without an index buffer set. (The first time at least...)
3735 * D3D8 simply dies, but I doubt it can do much harm to return
3736 * D3DERR_INVALIDCALL there as well. */
3737 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3738 return WINED3DERR_INVALIDCALL;
3741 wined3d_cs_emit_draw(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3742 device->state.base_vertex_index, start_idx, index_count, 0, 0, TRUE);
3744 return WINED3D_OK;
3747 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
3748 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
3750 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
3751 device, start_idx, index_count, start_instance, instance_count);
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, start_instance, instance_count, TRUE);
3757 void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
3758 struct wined3d_buffer *buffer, unsigned int offset)
3760 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
3762 wined3d_cs_emit_draw_indirect(device->cs, device->state.gl_primitive_type, device->state.gl_patch_vertices,
3763 buffer, offset, TRUE);
3766 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
3767 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
3769 unsigned int src_size, dst_size, src_skip_levels = 0;
3770 unsigned int src_level_count, dst_level_count;
3771 unsigned int layer_count, level_count, i, j;
3772 unsigned int width, height, depth;
3773 enum wined3d_resource_type type;
3774 struct wined3d_box box;
3776 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
3778 /* Verify that the source and destination textures are non-NULL. */
3779 if (!src_texture || !dst_texture)
3781 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
3782 return WINED3DERR_INVALIDCALL;
3785 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
3786 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
3788 WARN("Source resource is GPU accessible or a scratch resource.\n");
3789 return WINED3DERR_INVALIDCALL;
3791 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
3793 WARN("Destination resource is CPU accessible.\n");
3794 return WINED3DERR_INVALIDCALL;
3797 /* Verify that the source and destination textures are the same type. */
3798 type = src_texture->resource.type;
3799 if (dst_texture->resource.type != type)
3801 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
3802 return WINED3DERR_INVALIDCALL;
3805 layer_count = src_texture->layer_count;
3806 if (layer_count != dst_texture->layer_count)
3808 WARN("Source and destination have different layer counts.\n");
3809 return WINED3DERR_INVALIDCALL;
3812 if (src_texture->resource.format != dst_texture->resource.format)
3814 WARN("Source and destination formats do not match.\n");
3815 return WINED3DERR_INVALIDCALL;
3818 src_level_count = src_texture->level_count;
3819 dst_level_count = dst_texture->level_count;
3820 level_count = min(src_level_count, dst_level_count);
3822 src_size = max(src_texture->resource.width, src_texture->resource.height);
3823 src_size = max(src_size, src_texture->resource.depth);
3824 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
3825 dst_size = max(dst_size, dst_texture->resource.depth);
3826 while (src_size > dst_size)
3828 src_size >>= 1;
3829 ++src_skip_levels;
3832 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
3833 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
3834 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
3836 WARN("Source and destination dimensions do not match.\n");
3837 return WINED3DERR_INVALIDCALL;
3840 /* Update every surface level of the texture. */
3841 for (i = 0; i < level_count; ++i)
3843 width = wined3d_texture_get_level_width(dst_texture, i);
3844 height = wined3d_texture_get_level_height(dst_texture, i);
3845 depth = wined3d_texture_get_level_depth(dst_texture, i);
3846 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3848 for (j = 0; j < layer_count; ++j)
3850 wined3d_cs_emit_blt_sub_resource(device->cs,
3851 &dst_texture->resource, j * dst_level_count + i, &box,
3852 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
3853 0, NULL, WINED3D_TEXF_POINT);
3857 return WINED3D_OK;
3860 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
3862 const struct wined3d_state *state = &device->state;
3863 struct wined3d_texture *texture;
3864 DWORD i;
3866 TRACE("device %p, num_passes %p.\n", device, num_passes);
3868 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3870 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
3872 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3873 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3875 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
3877 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
3878 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
3881 texture = state->textures[i];
3882 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
3884 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
3886 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
3887 return E_FAIL;
3889 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
3891 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
3892 return E_FAIL;
3894 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
3895 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
3897 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
3898 return E_FAIL;
3902 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
3903 || state->render_states[WINED3D_RS_STENCILENABLE])
3905 struct wined3d_rendertarget_view *rt = device->fb.render_targets[0];
3906 struct wined3d_rendertarget_view *ds = device->fb.depth_stencil;
3908 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
3910 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
3911 return WINED3DERR_CONFLICTINGRENDERSTATE;
3915 /* return a sensible default */
3916 *num_passes = 1;
3918 TRACE("returning D3D_OK\n");
3919 return WINED3D_OK;
3922 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
3924 static BOOL warned;
3926 TRACE("device %p, software %#x.\n", device, software);
3928 if (!warned)
3930 FIXME("device %p, software %#x stub!\n", device, software);
3931 warned = TRUE;
3934 device->softwareVertexProcessing = software;
3937 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
3939 static BOOL warned;
3941 TRACE("device %p.\n", device);
3943 if (!warned)
3945 TRACE("device %p stub!\n", device);
3946 warned = TRUE;
3949 return device->softwareVertexProcessing;
3952 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
3953 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
3955 struct wined3d_swapchain *swapchain;
3957 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
3958 device, swapchain_idx, raster_status);
3960 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3961 return WINED3DERR_INVALIDCALL;
3963 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
3966 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
3968 static BOOL warned;
3970 TRACE("device %p, segments %.8e.\n", device, segments);
3972 if (segments != 0.0f)
3974 if (!warned)
3976 FIXME("device %p, segments %.8e stub!\n", device, segments);
3977 warned = TRUE;
3981 return WINED3D_OK;
3984 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
3986 static BOOL warned;
3988 TRACE("device %p.\n", device);
3990 if (!warned)
3992 FIXME("device %p stub!\n", device);
3993 warned = TRUE;
3996 return 0.0f;
3999 void CDECL wined3d_device_copy_uav_counter(struct wined3d_device *device,
4000 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4002 TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
4003 device, dst_buffer, offset, uav);
4005 wined3d_cs_emit_copy_uav_counter(device->cs, dst_buffer, offset, uav);
4008 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
4009 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4011 struct wined3d_texture *dst_texture, *src_texture;
4012 struct wined3d_box box;
4013 unsigned int i, j;
4015 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
4017 if (src_resource == dst_resource)
4019 WARN("Source and destination are the same resource.\n");
4020 return;
4023 if (src_resource->type != dst_resource->type)
4025 WARN("Resource types (%s / %s) don't match.\n",
4026 debug_d3dresourcetype(dst_resource->type),
4027 debug_d3dresourcetype(src_resource->type));
4028 return;
4031 if (src_resource->width != dst_resource->width
4032 || src_resource->height != dst_resource->height
4033 || src_resource->depth != dst_resource->depth)
4035 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4036 dst_resource->width, dst_resource->height, dst_resource->depth,
4037 src_resource->width, src_resource->height, src_resource->depth);
4038 return;
4041 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4042 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4044 WARN("Resource formats %s and %s are incompatible.\n",
4045 debug_d3dformat(dst_resource->format->id),
4046 debug_d3dformat(src_resource->format->id));
4047 return;
4050 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4052 wined3d_box_set(&box, 0, 0, src_resource->size, 1, 0, 1);
4053 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &box,
4054 src_resource, 0, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4055 return;
4058 dst_texture = texture_from_resource(dst_resource);
4059 src_texture = texture_from_resource(src_resource);
4061 if (src_texture->layer_count != dst_texture->layer_count
4062 || src_texture->level_count != dst_texture->level_count)
4064 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4065 dst_texture->layer_count, dst_texture->level_count,
4066 src_texture->layer_count, src_texture->level_count);
4067 return;
4070 for (i = 0; i < dst_texture->level_count; ++i)
4072 wined3d_box_set(&box, 0, 0,
4073 wined3d_texture_get_level_width(dst_texture, i),
4074 wined3d_texture_get_level_height(dst_texture, i),
4075 0, wined3d_texture_get_level_depth(dst_texture, i));
4076 for (j = 0; j < dst_texture->layer_count; ++j)
4078 unsigned int idx = j * dst_texture->level_count + i;
4080 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &box,
4081 src_resource, idx, &box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4086 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
4087 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4088 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4089 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4091 struct wined3d_box dst_box, b;
4093 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4094 "src_resource %p, src_sub_resource_idx %u, src_box %s.\n",
4095 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4096 src_resource, src_sub_resource_idx, debug_box(src_box));
4098 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4100 WARN("Source and destination are the same sub-resource.\n");
4101 return WINED3DERR_INVALIDCALL;
4104 if (src_resource->type != dst_resource->type)
4106 WARN("Resource types (%s / %s) don't match.\n",
4107 debug_d3dresourcetype(dst_resource->type),
4108 debug_d3dresourcetype(src_resource->type));
4109 return WINED3DERR_INVALIDCALL;
4112 if (src_resource->format->typeless_id != dst_resource->format->typeless_id
4113 || (!src_resource->format->typeless_id && src_resource->format->id != dst_resource->format->id))
4115 WARN("Resource formats %s and %s are incompatible.\n",
4116 debug_d3dformat(dst_resource->format->id),
4117 debug_d3dformat(src_resource->format->id));
4118 return WINED3DERR_INVALIDCALL;
4121 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4123 if (dst_sub_resource_idx)
4125 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4126 return WINED3DERR_INVALIDCALL;
4129 if (src_sub_resource_idx)
4131 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4132 return WINED3DERR_INVALIDCALL;
4135 if (!src_box)
4137 unsigned int dst_w;
4139 dst_w = dst_resource->size - dst_x;
4140 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4141 src_box = &b;
4143 else if ((src_box->left >= src_box->right
4144 || src_box->top >= src_box->bottom
4145 || src_box->front >= src_box->back))
4147 WARN("Invalid box %s specified.\n", debug_box(src_box));
4148 return WINED3DERR_INVALIDCALL;
4151 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4152 || src_box->right - src_box->left > dst_resource->size - dst_x)
4154 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4155 dst_x, src_box->left, src_box->right - src_box->left);
4156 return WINED3DERR_INVALIDCALL;
4159 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4161 else
4163 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4164 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4165 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4167 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4169 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4170 return WINED3DERR_INVALIDCALL;
4173 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4175 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4176 return WINED3DERR_INVALIDCALL;
4179 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4181 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4182 return WINED3DERR_INVALIDCALL;
4185 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4187 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4188 return WINED3DERR_INVALIDCALL;
4191 if (!src_box)
4193 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4195 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4196 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4197 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4199 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4200 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4201 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4202 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4204 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4205 src_box = &b;
4207 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
4209 WARN("Invalid source box %s.\n", debug_box(src_box));
4210 return WINED3DERR_INVALIDCALL;
4213 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4214 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4215 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
4216 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
4218 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4219 return WINED3DERR_INVALIDCALL;
4223 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
4224 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4226 return WINED3D_OK;
4229 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
4230 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
4231 unsigned int depth_pitch)
4233 unsigned int width, height, depth;
4234 struct wined3d_box b;
4236 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u.\n",
4237 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch);
4239 if (resource->type == WINED3D_RTYPE_BUFFER)
4241 if (sub_resource_idx > 0)
4243 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4244 return;
4247 width = resource->size;
4248 height = 1;
4249 depth = 1;
4251 else
4253 struct wined3d_texture *texture = texture_from_resource(resource);
4254 unsigned int level;
4256 if (sub_resource_idx >= texture->level_count * texture->layer_count)
4258 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
4259 return;
4262 level = sub_resource_idx % texture->level_count;
4263 width = wined3d_texture_get_level_width(texture, level);
4264 height = wined3d_texture_get_level_height(texture, level);
4265 depth = wined3d_texture_get_level_depth(texture, level);
4268 if (!box)
4270 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
4271 box = &b;
4273 else if (box->left >= box->right || box->right > width
4274 || box->top >= box->bottom || box->bottom > height
4275 || box->front >= box->back || box->back > depth)
4277 WARN("Invalid box %s specified.\n", debug_box(box));
4278 return;
4281 wined3d_resource_wait_idle(resource);
4283 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
4286 void CDECL wined3d_device_resolve_sub_resource(struct wined3d_device *device,
4287 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4288 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4289 enum wined3d_format_id format_id)
4291 struct wined3d_texture *dst_texture, *src_texture;
4292 unsigned int dst_level, src_level;
4293 RECT dst_rect, src_rect;
4295 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, "
4296 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4297 device, dst_resource, dst_sub_resource_idx,
4298 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4300 if (wined3d_format_is_typeless(dst_resource->format)
4301 || wined3d_format_is_typeless(src_resource->format))
4303 FIXME("Unhandled multisample resolve, dst_format %s, src_format %s, format %s.\n",
4304 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4305 debug_d3dformat(format_id));
4306 return;
4308 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4310 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4311 return;
4313 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4315 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4316 return;
4319 dst_texture = texture_from_resource(dst_resource);
4320 src_texture = texture_from_resource(src_resource);
4322 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4323 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4324 wined3d_texture_get_level_height(dst_texture, dst_level));
4325 src_level = src_sub_resource_idx % src_texture->level_count;
4326 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4327 wined3d_texture_get_level_height(src_texture, src_level));
4328 wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
4329 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
4332 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4333 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
4334 const struct wined3d_color *color, float depth, DWORD stencil)
4336 struct wined3d_resource *resource;
4337 RECT r;
4339 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4340 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4342 if (!flags)
4343 return WINED3D_OK;
4345 resource = view->resource;
4346 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
4348 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4349 return WINED3DERR_INVALIDCALL;
4352 if (view->layer_count > 1)
4354 FIXME("Layered clears not implemented.\n");
4355 return WINED3DERR_INVALIDCALL;
4358 if (!rect)
4360 SetRect(&r, 0, 0, view->width, view->height);
4361 rect = &r;
4363 else
4365 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4366 struct wined3d_texture *texture = texture_from_resource(view->resource);
4367 HRESULT hr;
4369 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
4370 view->sub_resource_idx % texture->level_count, &b)))
4371 return hr;
4374 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
4376 return WINED3D_OK;
4379 void CDECL wined3d_device_clear_unordered_access_view_uint(struct wined3d_device *device,
4380 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4382 TRACE("device %p, view %p, clear_value %s.\n", device, view, debug_uvec4(clear_value));
4384 wined3d_cs_emit_clear_unordered_access_view_uint(device->cs, view, clear_value);
4387 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
4388 unsigned int view_idx)
4390 TRACE("device %p, view_idx %u.\n", device, view_idx);
4392 if (view_idx >= device->adapter->gl_info.limits.buffers)
4394 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4395 return NULL;
4398 return device->fb.render_targets[view_idx];
4401 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
4403 TRACE("device %p.\n", device);
4405 return device->fb.depth_stencil;
4408 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
4409 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
4411 struct wined3d_rendertarget_view *prev;
4413 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4414 device, view_idx, view, set_viewport);
4416 if (view_idx >= device->adapter->gl_info.limits.buffers)
4418 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4419 return WINED3DERR_INVALIDCALL;
4422 if (view && !(view->resource->usage & WINED3DUSAGE_RENDERTARGET))
4424 WARN("View resource %p doesn't have render target usage.\n", view->resource);
4425 return WINED3DERR_INVALIDCALL;
4428 /* Set the viewport and scissor rectangles, if requested. Tests show that
4429 * stateblock recording is ignored, the change goes directly into the
4430 * primary stateblock. */
4431 if (!view_idx && set_viewport)
4433 struct wined3d_state *state = &device->state;
4435 state->viewport.x = 0;
4436 state->viewport.y = 0;
4437 state->viewport.width = view->width;
4438 state->viewport.height = view->height;
4439 state->viewport.min_z = 0.0f;
4440 state->viewport.max_z = 1.0f;
4441 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4443 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4444 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4448 prev = device->fb.render_targets[view_idx];
4449 if (view == prev)
4450 return WINED3D_OK;
4452 if (view)
4453 wined3d_rendertarget_view_incref(view);
4454 device->fb.render_targets[view_idx] = view;
4455 wined3d_cs_emit_set_rendertarget_view(device->cs, view_idx, view);
4456 /* Release after the assignment, to prevent device_resource_released()
4457 * from seeing the surface as still in use. */
4458 if (prev)
4459 wined3d_rendertarget_view_decref(prev);
4461 return WINED3D_OK;
4464 void CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device, struct wined3d_rendertarget_view *view)
4466 struct wined3d_rendertarget_view *prev;
4468 TRACE("device %p, view %p.\n", device, view);
4470 prev = device->fb.depth_stencil;
4471 if (prev == view)
4473 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4474 return;
4477 if ((device->fb.depth_stencil = view))
4478 wined3d_rendertarget_view_incref(view);
4479 wined3d_cs_emit_set_depth_stencil_view(device->cs, view);
4480 if (prev)
4481 wined3d_rendertarget_view_decref(prev);
4484 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4485 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4487 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4488 struct wined3d_sub_resource_data data;
4489 struct wined3d_resource_desc desc;
4490 struct wined3d_map_desc map_desc;
4491 struct wined3d_texture *texture;
4492 HRESULT hr;
4494 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4496 ERR("Failed to map source texture.\n");
4497 return NULL;
4500 data.data = map_desc.data;
4501 data.row_pitch = map_desc.row_pitch;
4502 data.slice_pitch = map_desc.slice_pitch;
4504 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4505 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4506 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4507 desc.multisample_quality = 0;
4508 desc.usage = WINED3DUSAGE_DYNAMIC;
4509 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4510 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4511 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4512 desc.depth = 1;
4513 desc.size = 0;
4515 hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
4516 &data, NULL, &wined3d_null_parent_ops, &texture);
4517 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4518 if (FAILED(hr))
4520 ERR("Failed to create cursor texture.\n");
4521 return NULL;
4524 return texture;
4527 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4528 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4530 unsigned int texture_level = sub_resource_idx % texture->level_count;
4531 unsigned int cursor_width, cursor_height;
4532 struct wined3d_display_mode mode;
4533 struct wined3d_map_desc map_desc;
4534 HRESULT hr;
4536 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
4537 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
4539 if (sub_resource_idx >= texture->level_count * texture->layer_count
4540 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4541 return WINED3DERR_INVALIDCALL;
4543 if (device->cursor_texture)
4545 wined3d_texture_decref(device->cursor_texture);
4546 device->cursor_texture = NULL;
4549 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4551 WARN("Texture %p has invalid format %s.\n",
4552 texture, debug_d3dformat(texture->resource.format->id));
4553 return WINED3DERR_INVALIDCALL;
4556 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4558 ERR("Failed to get display mode, hr %#x.\n", hr);
4559 return WINED3DERR_INVALIDCALL;
4562 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
4563 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
4564 if (cursor_width > mode.width || cursor_height > mode.height)
4566 WARN("Texture %p, sub-resource %u dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4567 texture, sub_resource_idx, cursor_width, cursor_height, mode.width, mode.height);
4568 return WINED3DERR_INVALIDCALL;
4571 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4573 /* Do not store the surface's pointer because the application may
4574 * release it after setting the cursor image. Windows doesn't
4575 * addref the set surface, so we can't do this either without
4576 * creating circular refcount dependencies. */
4577 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
4579 ERR("Failed to create cursor texture.\n");
4580 return WINED3DERR_INVALIDCALL;
4583 if (cursor_width == 32 && cursor_height == 32)
4585 UINT mask_size = cursor_width * cursor_height / 8;
4586 ICONINFO cursor_info;
4587 DWORD *mask_bits;
4588 HCURSOR cursor;
4590 /* 32-bit user32 cursors ignore the alpha channel if it's all
4591 * zeroes, and use the mask instead. Fill the mask with all ones
4592 * to ensure we still get a fully transparent cursor. */
4593 if (!(mask_bits = heap_alloc(mask_size)))
4594 return E_OUTOFMEMORY;
4595 memset(mask_bits, 0xff, mask_size);
4597 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
4598 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
4599 cursor_info.fIcon = FALSE;
4600 cursor_info.xHotspot = x_hotspot;
4601 cursor_info.yHotspot = y_hotspot;
4602 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
4603 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
4604 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
4606 /* Create our cursor and clean up. */
4607 cursor = CreateIconIndirect(&cursor_info);
4608 if (cursor_info.hbmMask)
4609 DeleteObject(cursor_info.hbmMask);
4610 if (cursor_info.hbmColor)
4611 DeleteObject(cursor_info.hbmColor);
4612 if (device->hardwareCursor)
4613 DestroyCursor(device->hardwareCursor);
4614 device->hardwareCursor = cursor;
4615 if (device->bCursorVisible)
4616 SetCursor(cursor);
4618 heap_free(mask_bits);
4621 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
4622 device->cursorWidth = cursor_width;
4623 device->cursorHeight = cursor_height;
4624 device->xHotSpot = x_hotspot;
4625 device->yHotSpot = y_hotspot;
4627 return WINED3D_OK;
4630 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4631 int x_screen_space, int y_screen_space, DWORD flags)
4633 TRACE("device %p, x %d, y %d, flags %#x.\n",
4634 device, x_screen_space, y_screen_space, flags);
4636 device->xScreenSpace = x_screen_space;
4637 device->yScreenSpace = y_screen_space;
4639 if (device->hardwareCursor)
4641 POINT pt;
4643 GetCursorPos( &pt );
4644 if (x_screen_space == pt.x && y_screen_space == pt.y)
4645 return;
4646 SetCursorPos( x_screen_space, y_screen_space );
4648 /* Switch to the software cursor if position diverges from the hardware one. */
4649 GetCursorPos( &pt );
4650 if (x_screen_space != pt.x || y_screen_space != pt.y)
4652 if (device->bCursorVisible) SetCursor( NULL );
4653 DestroyCursor( device->hardwareCursor );
4654 device->hardwareCursor = 0;
4659 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4661 BOOL oldVisible = device->bCursorVisible;
4663 TRACE("device %p, show %#x.\n", device, show);
4666 * When ShowCursor is first called it should make the cursor appear at the OS's last
4667 * known cursor position.
4669 if (show && !oldVisible)
4671 POINT pt;
4672 GetCursorPos(&pt);
4673 device->xScreenSpace = pt.x;
4674 device->yScreenSpace = pt.y;
4677 if (device->hardwareCursor)
4679 device->bCursorVisible = show;
4680 if (show)
4681 SetCursor(device->hardwareCursor);
4682 else
4683 SetCursor(NULL);
4685 else if (device->cursor_texture)
4687 device->bCursorVisible = show;
4690 return oldVisible;
4693 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4695 struct wined3d_resource *resource, *cursor;
4697 TRACE("device %p.\n", device);
4699 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4701 TRACE("Checking resource %p for eviction.\n", resource);
4703 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
4705 TRACE("Evicting %p.\n", resource);
4706 wined3d_cs_emit_unload_resource(device->cs, resource);
4711 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4712 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4713 wined3d_device_reset_cb callback, BOOL reset_state)
4715 struct wined3d_resource *resource, *cursor;
4716 struct wined3d_swapchain *swapchain;
4717 struct wined3d_view_desc view_desc;
4718 BOOL backbuffer_resized;
4719 HRESULT hr = WINED3D_OK;
4720 unsigned int i;
4722 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
4723 device, swapchain_desc, mode, callback, reset_state);
4725 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4727 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4729 ERR("Failed to get the first implicit swapchain.\n");
4730 return WINED3DERR_INVALIDCALL;
4733 if (reset_state)
4735 if (device->logo_texture)
4737 wined3d_texture_decref(device->logo_texture);
4738 device->logo_texture = NULL;
4740 if (device->cursor_texture)
4742 wined3d_texture_decref(device->cursor_texture);
4743 device->cursor_texture = NULL;
4745 state_unbind_resources(&device->state);
4748 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
4750 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
4752 wined3d_device_set_depth_stencil_view(device, NULL);
4754 if (reset_state)
4756 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4758 TRACE("Enumerating resource %p.\n", resource);
4759 if (FAILED(hr = callback(resource)))
4760 return hr;
4764 TRACE("New params:\n");
4765 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4766 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4767 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4768 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4769 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4770 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4771 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4772 TRACE("device_window %p\n", swapchain_desc->device_window);
4773 TRACE("windowed %#x\n", swapchain_desc->windowed);
4774 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4775 if (swapchain_desc->enable_auto_depth_stencil)
4776 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4777 TRACE("flags %#x\n", swapchain_desc->flags);
4778 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4779 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4781 if (swapchain_desc->backbuffer_usage != WINED3DUSAGE_RENDERTARGET)
4782 FIXME("Got unexpected backbuffer usage %#x.\n", swapchain_desc->backbuffer_usage);
4784 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
4785 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
4786 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
4787 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
4789 /* No special treatment of these parameters. Just store them */
4790 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4791 swapchain->desc.enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
4792 swapchain->desc.auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
4793 swapchain->desc.flags = swapchain_desc->flags;
4794 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4795 swapchain->desc.auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
4797 if (swapchain_desc->device_window
4798 && swapchain_desc->device_window != swapchain->desc.device_window)
4800 TRACE("Changing the device window from %p to %p.\n",
4801 swapchain->desc.device_window, swapchain_desc->device_window);
4802 swapchain->desc.device_window = swapchain_desc->device_window;
4803 swapchain->device_window = swapchain_desc->device_window;
4804 wined3d_swapchain_set_window(swapchain, NULL);
4807 backbuffer_resized = swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
4808 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height;
4810 if (!swapchain_desc->windowed != !swapchain->desc.windowed
4811 || swapchain->reapply_mode || mode
4812 || (!swapchain_desc->windowed && backbuffer_resized))
4814 if (FAILED(hr = wined3d_swapchain_set_fullscreen(swapchain, swapchain_desc, mode)))
4815 return hr;
4817 else if (!swapchain_desc->windowed)
4819 DWORD style = device->style;
4820 DWORD exStyle = device->exStyle;
4821 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
4822 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
4823 * Reset to clear up their mess. Guild Wars also loses the device during that.
4825 device->style = 0;
4826 device->exStyle = 0;
4827 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
4828 swapchain_desc->backbuffer_width,
4829 swapchain_desc->backbuffer_height);
4830 device->style = style;
4831 device->exStyle = exStyle;
4834 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
4835 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
4836 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
4837 return hr;
4839 if (device->auto_depth_stencil_view)
4841 wined3d_rendertarget_view_decref(device->auto_depth_stencil_view);
4842 device->auto_depth_stencil_view = NULL;
4844 if (swapchain->desc.enable_auto_depth_stencil)
4846 struct wined3d_resource_desc texture_desc;
4847 struct wined3d_texture *texture;
4848 DWORD flags = 0;
4850 TRACE("Creating the depth stencil buffer.\n");
4852 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4853 texture_desc.format = swapchain->desc.auto_depth_stencil_format;
4854 texture_desc.multisample_type = swapchain->desc.multisample_type;
4855 texture_desc.multisample_quality = swapchain->desc.multisample_quality;
4856 texture_desc.usage = WINED3DUSAGE_DEPTHSTENCIL;
4857 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4858 texture_desc.width = swapchain->desc.backbuffer_width;
4859 texture_desc.height = swapchain->desc.backbuffer_height;
4860 texture_desc.depth = 1;
4861 texture_desc.size = 0;
4863 if (swapchain_desc->flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
4864 flags |= WINED3D_TEXTURE_CREATE_GET_DC;
4866 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
4867 device->device_parent, &texture_desc, flags, &texture)))
4869 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
4870 return WINED3DERR_INVALIDCALL;
4873 view_desc.format_id = texture->resource.format->id;
4874 view_desc.flags = 0;
4875 view_desc.u.texture.level_idx = 0;
4876 view_desc.u.texture.level_count = 1;
4877 view_desc.u.texture.layer_idx = 0;
4878 view_desc.u.texture.layer_count = 1;
4879 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
4880 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
4881 wined3d_texture_decref(texture);
4882 if (FAILED(hr))
4884 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4885 return hr;
4888 wined3d_device_set_depth_stencil_view(device, device->auto_depth_stencil_view);
4891 if (device->back_buffer_view)
4893 wined3d_rendertarget_view_decref(device->back_buffer_view);
4894 device->back_buffer_view = NULL;
4896 if (swapchain->desc.backbuffer_count)
4898 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
4900 view_desc.format_id = back_buffer->format->id;
4901 view_desc.flags = 0;
4902 view_desc.u.texture.level_idx = 0;
4903 view_desc.u.texture.level_count = 1;
4904 view_desc.u.texture.layer_idx = 0;
4905 view_desc.u.texture.layer_count = 1;
4906 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
4907 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
4909 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
4910 return hr;
4914 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
4916 if (reset_state)
4918 TRACE("Resetting stateblock.\n");
4919 if (device->recording)
4921 wined3d_stateblock_decref(device->recording);
4922 device->recording = NULL;
4924 wined3d_cs_emit_reset_state(device->cs);
4925 state_cleanup(&device->state);
4927 if (device->d3d_initialized)
4928 wined3d_device_delete_opengl_contexts(device);
4930 memset(&device->state, 0, sizeof(device->state));
4931 state_init(&device->state, &device->fb, &device->adapter->gl_info,
4932 &device->adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
4933 device->update_state = &device->state;
4935 device_init_swapchain_state(device, swapchain);
4936 if (wined3d_settings.logo)
4937 device_load_logo(device, wined3d_settings.logo);
4939 else if (device->back_buffer_view)
4941 struct wined3d_rendertarget_view *view = device->back_buffer_view;
4942 struct wined3d_state *state = &device->state;
4944 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
4946 /* Note the min_z / max_z is not reset. */
4947 state->viewport.x = 0;
4948 state->viewport.y = 0;
4949 state->viewport.width = view->width;
4950 state->viewport.height = view->height;
4951 wined3d_cs_emit_set_viewport(device->cs, &state->viewport);
4953 SetRect(&state->scissor_rect, 0, 0, view->width, view->height);
4954 wined3d_cs_emit_set_scissor_rect(device->cs, &state->scissor_rect);
4957 if (device->d3d_initialized)
4959 if (reset_state)
4960 hr = wined3d_device_create_primary_opengl_context(device);
4963 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
4964 * first use
4966 return hr;
4969 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
4971 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
4973 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
4975 return WINED3D_OK;
4979 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
4980 struct wined3d_device_creation_parameters *parameters)
4982 TRACE("device %p, parameters %p.\n", device, parameters);
4984 *parameters = device->create_parms;
4987 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
4989 TRACE("device %p.\n", device);
4991 return device->wined3d;
4994 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
4995 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
4997 struct wined3d_swapchain *swapchain;
4999 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5000 device, swapchain_idx, flags, ramp);
5002 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5003 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5006 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5007 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5009 struct wined3d_swapchain *swapchain;
5011 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5012 device, swapchain_idx, ramp);
5014 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5015 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5018 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5020 TRACE("device %p, resource %p.\n", device, resource);
5022 wined3d_not_from_cs(device->cs);
5024 list_add_head(&device->resources, &resource->resource_list_entry);
5027 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5029 TRACE("device %p, resource %p.\n", device, resource);
5031 wined3d_not_from_cs(device->cs);
5033 list_remove(&resource->resource_list_entry);
5036 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5038 enum wined3d_resource_type type = resource->type;
5039 struct wined3d_rendertarget_view *rtv;
5040 unsigned int i;
5042 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5044 if (device->d3d_initialized)
5046 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5048 if ((rtv = device->fb.render_targets[i]) && rtv->resource == resource)
5049 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5052 if ((rtv = device->fb.depth_stencil) && rtv->resource == resource)
5053 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5056 switch (type)
5058 case WINED3D_RTYPE_TEXTURE_2D:
5059 case WINED3D_RTYPE_TEXTURE_3D:
5060 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5062 if (&device->state.textures[i]->resource == resource)
5064 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5065 device->state.textures[i] = NULL;
5068 if (device->recording && &device->update_state->textures[i]->resource == resource)
5070 ERR("Texture resource %p is still in use by recording stateblock %p, stage %u.\n",
5071 resource, device->recording, i);
5072 device->update_state->textures[i] = NULL;
5075 break;
5077 case WINED3D_RTYPE_BUFFER:
5078 for (i = 0; i < MAX_STREAMS; ++i)
5080 if (&device->state.streams[i].buffer->resource == resource)
5082 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5083 device->state.streams[i].buffer = NULL;
5086 if (device->recording && &device->update_state->streams[i].buffer->resource == resource)
5088 ERR("Buffer resource %p is still in use by stateblock %p, stream %u.\n",
5089 resource, device->recording, i);
5090 device->update_state->streams[i].buffer = NULL;
5094 if (&device->state.index_buffer->resource == resource)
5096 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5097 device->state.index_buffer = NULL;
5100 if (device->recording && &device->update_state->index_buffer->resource == resource)
5102 ERR("Buffer resource %p is still in use by stateblock %p as index buffer.\n",
5103 resource, device->recording);
5104 device->update_state->index_buffer = NULL;
5106 break;
5108 default:
5109 break;
5112 /* Remove the resource from the resourceStore */
5113 device_resource_remove(device, resource);
5115 TRACE("Resource released.\n");
5118 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5120 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5122 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5125 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5126 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5127 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5129 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5130 const struct fragment_pipeline *fragment_pipeline;
5131 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5132 unsigned int i;
5133 HRESULT hr;
5135 device->ref = 1;
5136 device->wined3d = wined3d;
5137 wined3d_incref(device->wined3d);
5138 device->adapter = wined3d->adapter_count ? adapter : NULL;
5139 device->device_parent = device_parent;
5140 list_init(&device->resources);
5141 list_init(&device->shaders);
5142 device->surface_alignment = surface_alignment;
5144 /* Save the creation parameters. */
5145 device->create_parms.adapter_idx = adapter_idx;
5146 device->create_parms.device_type = device_type;
5147 device->create_parms.focus_window = focus_window;
5148 device->create_parms.flags = flags;
5150 device->shader_backend = adapter->shader_backend;
5152 vertex_pipeline = adapter->vertex_pipe;
5154 fragment_pipeline = adapter->fragment_pipe;
5156 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5158 if (vertex_pipeline->vp_states && fragment_pipeline->states
5159 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5160 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5161 fragment_pipeline, misc_state_template)))
5163 ERR("Failed to compile state table, hr %#x.\n", hr);
5164 wine_rb_destroy(&device->samplers, NULL, NULL);
5165 wined3d_decref(device->wined3d);
5166 return hr;
5169 state_init(&device->state, &device->fb, &adapter->gl_info,
5170 &adapter->d3d_info, WINED3D_STATE_INIT_DEFAULT);
5171 device->update_state = &device->state;
5173 if (!(device->cs = wined3d_cs_create(device)))
5175 WARN("Failed to create command stream.\n");
5176 state_cleanup(&device->state);
5177 hr = E_FAIL;
5178 goto err;
5181 return WINED3D_OK;
5183 err:
5184 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5186 heap_free(device->multistate_funcs[i]);
5188 wine_rb_destroy(&device->samplers, NULL, NULL);
5189 wined3d_decref(device->wined3d);
5190 return hr;
5193 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5195 DWORD rep = device->StateTable[state].representative;
5196 struct wined3d_context *context;
5197 DWORD idx;
5198 BYTE shift;
5199 UINT i;
5201 wined3d_from_cs(device->cs);
5203 if (STATE_IS_COMPUTE(state))
5205 for (i = 0; i < device->context_count; ++i)
5206 context_invalidate_compute_state(device->contexts[i], state);
5207 return;
5210 for (i = 0; i < device->context_count; ++i)
5212 context = device->contexts[i];
5213 if(isStateDirty(context, rep)) continue;
5215 context->dirtyArray[context->numDirtyEntries++] = rep;
5216 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5217 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5218 context->isStateDirty[idx] |= (1u << shift);
5222 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5223 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5225 if (device->filter_messages && message != WM_DISPLAYCHANGE)
5227 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5228 window, message, wparam, lparam);
5229 if (unicode)
5230 return DefWindowProcW(window, message, wparam, lparam);
5231 else
5232 return DefWindowProcA(window, message, wparam, lparam);
5235 if (message == WM_DESTROY)
5237 TRACE("unregister window %p.\n", window);
5238 wined3d_unregister_window(window);
5240 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5241 ERR("Window %p is not the focus window for device %p.\n", window, device);
5243 else if (message == WM_DISPLAYCHANGE)
5245 device->device_parent->ops->mode_changed(device->device_parent);
5247 else if (message == WM_ACTIVATEAPP)
5249 UINT i;
5251 for (i = 0; i < device->swapchain_count; i++)
5252 wined3d_swapchain_activate(device->swapchains[i], wparam);
5254 device->device_parent->ops->activate(device->device_parent, wparam);
5256 else if (message == WM_SYSCOMMAND)
5258 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5260 if (unicode)
5261 DefWindowProcW(window, message, wparam, lparam);
5262 else
5263 DefWindowProcA(window, message, wparam, lparam);
5267 if (unicode)
5268 return CallWindowProcW(proc, window, message, wparam, lparam);
5269 else
5270 return CallWindowProcA(proc, window, message, wparam, lparam);