wined3d: Rename create/destroy helpers dealing with Vulkan objects.
[wine/zf.git] / dlls / wined3d / device.c
blobbbf1bada7b662b987f2ae27ac58545f090cb7721
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 #ifdef HAVE_FLOAT_H
31 # include <float.h>
32 #endif
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 struct wined3d_matrix_3x3
41 float _11, _12, _13;
42 float _21, _22, _23;
43 float _31, _32, _33;
46 struct light_transformed
48 struct wined3d_color diffuse, specular, ambient;
49 struct wined3d_vec4 position;
50 struct wined3d_vec3 direction;
51 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
54 struct lights_settings
56 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
57 struct wined3d_color ambient_light;
58 struct wined3d_matrix modelview_matrix;
59 struct wined3d_matrix_3x3 normal_matrix;
60 struct wined3d_vec4 position_transformed;
62 float fog_start, fog_end, fog_density;
64 uint32_t point_light_count : 8;
65 uint32_t spot_light_count : 8;
66 uint32_t directional_light_count : 8;
67 uint32_t parallel_point_light_count : 8;
68 uint32_t lighting : 1;
69 uint32_t legacy_lighting : 1;
70 uint32_t normalise : 1;
71 uint32_t localviewer : 1;
72 uint32_t fog_coord_mode : 2;
73 uint32_t fog_mode : 2;
74 uint32_t padding : 24;
77 /* Define the default light parameters as specified by MSDN. */
78 const struct wined3d_light WINED3D_default_light =
80 WINED3D_LIGHT_DIRECTIONAL, /* Type */
81 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
82 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
83 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
84 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
85 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
86 0.0f, /* Range */
87 0.0f, /* Falloff */
88 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
89 0.0f, /* Theta */
90 0.0f /* Phi */
93 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
95 struct wined3d_context **new_array;
97 TRACE("Adding context %p.\n", context);
99 if (!device->shader_backend->shader_allocate_context_data(context))
101 ERR("Failed to allocate shader backend context data.\n");
102 return FALSE;
104 device->shader_backend->shader_init_context_state(context);
106 if (!device->adapter->fragment_pipe->allocate_context_data(context))
108 ERR("Failed to allocate fragment pipeline context data.\n");
109 device->shader_backend->shader_free_context_data(context);
110 return FALSE;
113 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
115 ERR("Failed to grow the context array.\n");
116 device->adapter->fragment_pipe->free_context_data(context);
117 device->shader_backend->shader_free_context_data(context);
118 return FALSE;
121 new_array[device->context_count++] = context;
122 device->contexts = new_array;
124 return TRUE;
127 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
129 struct wined3d_context **new_array;
130 BOOL found = FALSE;
131 UINT i;
133 TRACE("Removing context %p.\n", context);
135 device->adapter->fragment_pipe->free_context_data(context);
136 device->shader_backend->shader_free_context_data(context);
138 for (i = 0; i < device->context_count; ++i)
140 if (device->contexts[i] == context)
142 found = TRUE;
143 break;
147 if (!found)
149 ERR("Context %p doesn't exist in context array.\n", context);
150 return;
153 if (!--device->context_count)
155 heap_free(device->contexts);
156 device->contexts = NULL;
157 return;
160 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
161 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
163 ERR("Failed to shrink context array. Oh well.\n");
164 return;
167 device->contexts = new_array;
170 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
172 ULONG refcount = InterlockedIncrement(&device->ref);
174 TRACE("%p increasing refcount to %u.\n", device, refcount);
176 return refcount;
179 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
181 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
183 heap_free(s);
186 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
188 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
190 ERR("Leftover sampler %p.\n", sampler);
193 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
195 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
197 ERR("Leftover rasterizer state %p.\n", state);
200 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
202 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
204 ERR("Leftover blend state %p.\n", blend_state);
207 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
209 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
211 ERR("Leftover depth/stencil state %p.\n", state);
214 void wined3d_device_cleanup(struct wined3d_device *device)
216 unsigned int i;
218 if (device->swapchain_count)
219 wined3d_device_uninit_3d(device);
221 wined3d_cs_destroy(device->cs);
223 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
225 heap_free(device->multistate_funcs[i]);
226 device->multistate_funcs[i] = NULL;
229 if (!list_empty(&device->resources))
231 struct wined3d_resource *resource;
233 ERR("Device released with resources still bound.\n");
235 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
237 ERR("Leftover resource %p with type %s (%#x).\n",
238 resource, debug_d3dresourcetype(resource->type), resource->type);
242 if (device->contexts)
243 ERR("Context array not freed!\n");
244 if (device->hardwareCursor)
245 DestroyCursor(device->hardwareCursor);
246 device->hardwareCursor = 0;
248 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
249 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
250 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
251 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
252 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
254 wined3d_decref(device->wined3d);
255 device->wined3d = NULL;
258 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
260 ULONG refcount = InterlockedDecrement(&device->ref);
262 TRACE("%p decreasing refcount to %u.\n", device, refcount);
264 if (!refcount)
266 device->adapter->adapter_ops->adapter_destroy_device(device);
267 TRACE("Destroyed device %p.\n", device);
270 return refcount;
273 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
275 TRACE("device %p.\n", device);
277 return device->swapchain_count;
280 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
282 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
284 if (swapchain_idx >= device->swapchain_count)
286 WARN("swapchain_idx %u >= swapchain_count %u.\n",
287 swapchain_idx, device->swapchain_count);
288 return NULL;
291 return device->swapchains[swapchain_idx];
294 static void device_load_logo(struct wined3d_device *device, const char *filename)
296 struct wined3d_color_key color_key;
297 struct wined3d_resource_desc desc;
298 HBITMAP hbm;
299 BITMAP bm;
300 HRESULT hr;
301 HDC dcb = NULL, dcs = NULL;
303 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
305 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
306 return;
308 GetObjectA(hbm, sizeof(BITMAP), &bm);
310 if (!(dcb = CreateCompatibleDC(NULL)))
311 goto out;
312 SelectObject(dcb, hbm);
314 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
315 desc.format = WINED3DFMT_B5G6R5_UNORM;
316 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
317 desc.multisample_quality = 0;
318 desc.usage = WINED3DUSAGE_DYNAMIC;
319 desc.bind_flags = 0;
320 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
321 desc.width = bm.bmWidth;
322 desc.height = bm.bmHeight;
323 desc.depth = 1;
324 desc.size = 0;
325 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
326 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
328 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
329 goto out;
332 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
334 wined3d_texture_decref(device->logo_texture);
335 device->logo_texture = NULL;
336 goto out;
338 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
339 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
341 color_key.color_space_low_value = 0;
342 color_key.color_space_high_value = 0;
343 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
345 out:
346 if (dcb) DeleteDC(dcb);
347 if (hbm) DeleteObject(hbm);
350 /* Context activation is done by the caller. */
351 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
352 struct wined3d_context_gl *context_gl)
354 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
355 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
356 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
357 unsigned int i;
358 DWORD color;
360 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
361 color = 0x000000ff;
362 else
363 color = 0x00000000;
365 /* Under DirectX you can sample even if no texture is bound, whereas
366 * OpenGL will only allow that when a valid texture is bound.
367 * We emulate this by creating dummy textures and binding them
368 * to each texture stage when the currently set D3D texture is NULL. */
369 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
371 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
372 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
373 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
374 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
375 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
377 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
378 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
379 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
380 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
381 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
383 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
385 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
386 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
387 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
388 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
389 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
392 if (gl_info->supported[EXT_TEXTURE3D])
394 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
395 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
396 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
397 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
398 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
401 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
403 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
404 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
405 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
406 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
408 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
409 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
413 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
415 DWORD cube_array_data[6];
417 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
418 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
419 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
420 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
421 cube_array_data[i] = color;
422 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
423 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
426 if (gl_info->supported[EXT_TEXTURE_ARRAY])
428 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
429 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
430 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
431 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
432 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
434 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
435 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
436 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
437 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
438 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
441 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
443 GLuint buffer;
445 GL_EXTCALL(glGenBuffers(1, &buffer));
446 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
447 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
448 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
450 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
451 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
452 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
453 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
454 GL_EXTCALL(glDeleteBuffers(1, &buffer));
457 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
459 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
460 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
461 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
462 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
464 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
465 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
466 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
467 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
469 if (gl_info->supported[ARB_CLEAR_TEXTURE])
471 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
472 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
474 else
476 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
480 checkGLcall("create dummy textures");
482 wined3d_context_gl_bind_dummy_textures(context_gl);
485 /* Context activation is done by the caller. */
486 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
487 struct wined3d_context_gl *context_gl)
489 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
490 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
492 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
494 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
495 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
498 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
499 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
501 if (gl_info->supported[EXT_TEXTURE_ARRAY])
503 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
504 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
507 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
508 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
510 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
511 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
513 if (gl_info->supported[EXT_TEXTURE3D])
514 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
516 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
517 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
519 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
520 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
522 checkGLcall("delete dummy textures");
524 memset(dummy_textures, 0, sizeof(*dummy_textures));
527 /* Context activation is done by the caller. */
528 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
530 struct wined3d_sampler_desc desc;
531 HRESULT hr;
533 desc.address_u = WINED3D_TADDRESS_WRAP;
534 desc.address_v = WINED3D_TADDRESS_WRAP;
535 desc.address_w = WINED3D_TADDRESS_WRAP;
536 memset(desc.border_color, 0, sizeof(desc.border_color));
537 desc.mag_filter = WINED3D_TEXF_POINT;
538 desc.min_filter = WINED3D_TEXF_POINT;
539 desc.mip_filter = WINED3D_TEXF_NONE;
540 desc.lod_bias = 0.0f;
541 desc.min_lod = -1000.0f;
542 desc.max_lod = 1000.0f;
543 desc.mip_base_level = 0;
544 desc.max_anisotropy = 1;
545 desc.compare = FALSE;
546 desc.comparison_func = WINED3D_CMP_NEVER;
547 desc.srgb_decode = TRUE;
549 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
550 * instructions allow access to resources without using samplers.
551 * In GLSL, resources are always accessed through sampler or image variables. The default
552 * sampler object is used to emulate the direct resource access when there is no sampler state
553 * to use.
555 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
557 ERR("Failed to create default sampler, hr %#x.\n", hr);
558 device->default_sampler = NULL;
561 /* In D3D10+, a NULL sampler maps to the default sampler state. */
562 desc.address_u = WINED3D_TADDRESS_CLAMP;
563 desc.address_v = WINED3D_TADDRESS_CLAMP;
564 desc.address_w = WINED3D_TADDRESS_CLAMP;
565 desc.mag_filter = WINED3D_TEXF_LINEAR;
566 desc.min_filter = WINED3D_TEXF_LINEAR;
567 desc.mip_filter = WINED3D_TEXF_LINEAR;
568 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
570 ERR("Failed to create null sampler, hr %#x.\n", hr);
571 device->null_sampler = NULL;
575 /* Context activation is done by the caller. */
576 void wined3d_device_destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
578 wined3d_sampler_decref(device->default_sampler);
579 device->default_sampler = NULL;
580 wined3d_sampler_decref(device->null_sampler);
581 device->null_sampler = NULL;
584 static void wined3d_null_image_vk_cleanup(struct wined3d_null_image_vk *image,
585 struct wined3d_context_vk *context_vk, uint64_t command_buffer_id)
587 wined3d_context_vk_destroy_vk_image(context_vk, image->vk_image, command_buffer_id);
588 if (image->memory)
589 wined3d_context_vk_destroy_allocator_block(context_vk, image->memory, command_buffer_id);
590 else
591 wined3d_context_vk_destroy_vk_memory(context_vk, image->vk_memory, command_buffer_id);
594 static bool wined3d_null_image_vk_init(struct wined3d_null_image_vk *image, struct wined3d_context_vk *context_vk,
595 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
597 struct wined3d_device_vk *device_vk = wined3d_device_vk(context_vk->c.device);
598 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
599 VkMemoryRequirements memory_requirements;
600 VkImageSubresourceRange range;
601 VkImageCreateInfo image_desc;
602 unsigned int memory_type_idx;
603 uint32_t flags = 0;
604 VkResult vr;
606 static const VkClearColorValue colour = {{0}};
608 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
609 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
611 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
612 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
614 image_desc.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
615 image_desc.pNext = NULL;
616 image_desc.flags = flags;
617 image_desc.imageType = type;
618 image_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
619 image_desc.extent.width = 1;
620 image_desc.extent.height = 1;
621 image_desc.extent.depth = 1;
622 image_desc.mipLevels = 1;
623 image_desc.arrayLayers = layer_count;
624 image_desc.samples = sample_count;
625 image_desc.tiling = VK_IMAGE_TILING_OPTIMAL;
626 image_desc.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
627 image_desc.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
628 image_desc.queueFamilyIndexCount = 0;
629 image_desc.pQueueFamilyIndices = NULL;
630 image_desc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
631 if ((vr = VK_CALL(vkCreateImage(device_vk->vk_device, &image_desc, NULL, &image->vk_image))) < 0)
633 ERR("Failed to create Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
634 return false;
637 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, image->vk_image, &memory_requirements));
639 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(wined3d_adapter_vk(device_vk->d.adapter),
640 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
641 if (memory_type_idx == ~0u)
643 ERR("Failed to find suitable image memory type.\n");
644 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
645 image->vk_image = VK_NULL_HANDLE;
646 return false;
649 image->memory = wined3d_context_vk_allocate_memory(context_vk,
650 memory_type_idx, memory_requirements.size, &image->vk_memory);
651 if (!image->vk_memory)
653 ERR("Failed to allocate image memory.\n");
654 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
655 image->vk_image = VK_NULL_HANDLE;
656 return false;
659 if ((vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, image->vk_image,
660 image->vk_memory, image->memory ? image->memory->offset : 0))) < 0)
662 ERR("Failed to bind image memory, vr %s.\n", wined3d_debug_vkresult(vr));
663 if (image->memory)
664 wined3d_allocator_block_free(image->memory);
665 else
666 VK_CALL(vkFreeMemory(device_vk->vk_device, image->vk_memory, NULL));
667 VK_CALL(vkDestroyImage(device_vk->vk_device, image->vk_image, NULL));
668 image->vk_image = VK_NULL_HANDLE;
669 return false;
672 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
673 range.baseMipLevel = 0;
674 range.levelCount = 1;
675 range.baseArrayLayer = 0;
676 range.layerCount = layer_count;
678 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
679 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
680 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
682 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
683 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
685 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
686 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
687 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
689 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
690 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
692 return true;
695 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
696 struct wined3d_context_vk *context_vk)
698 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
699 const struct wined3d_vk_info *vk_info;
700 const struct wined3d_format *format;
701 VkMemoryPropertyFlags memory_type;
702 VkCommandBuffer vk_command_buffer;
703 unsigned int sample_count = 2;
704 VkBufferUsageFlags usage;
705 uint64_t id;
707 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
708 while (sample_count && !(sample_count & format->multisample_types))
709 sample_count <<= 1;
711 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
713 ERR("Failed to get command buffer.\n");
714 return false;
717 vk_info = context_vk->vk_info;
719 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
720 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
721 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
722 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
723 return false;
724 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.buffer_offset, r->bo.size, 0x00000000u));
725 r->buffer_info.buffer = r->bo.vk_buffer;
726 r->buffer_info.offset = r->bo.buffer_offset;
727 r->buffer_info.range = r->bo.size;
729 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
731 ERR("Failed to create 1D image.\n");
732 goto fail;
735 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
737 ERR("Failed to create 2D image.\n");
738 goto fail;
741 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
743 ERR("Failed to create 2D MSAA image.\n");
744 goto fail;
747 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
749 ERR("Failed to create 3D image.\n");
750 goto fail;
753 return true;
755 fail:
756 id = context_vk->current_command_buffer.id;
757 if (r->image_2dms.vk_image)
758 wined3d_null_image_vk_cleanup(&r->image_2dms, context_vk, id);
759 if (r->image_2d.vk_image)
760 wined3d_null_image_vk_cleanup(&r->image_2d, context_vk, id);
761 if (r->image_1d.vk_image)
762 wined3d_null_image_vk_cleanup(&r->image_1d, context_vk, id);
763 wined3d_context_vk_reference_bo(context_vk, &r->bo);
764 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
765 return false;
768 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
769 struct wined3d_context_vk *context_vk)
771 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
772 uint64_t id = context_vk->current_command_buffer.id;
774 /* We don't track command buffer references to NULL resources. We easily
775 * could, but it doesn't seem worth it. */
776 wined3d_null_image_vk_cleanup(&r->image_3d, context_vk, id);
777 wined3d_null_image_vk_cleanup(&r->image_2dms, context_vk, id);
778 wined3d_null_image_vk_cleanup(&r->image_2d, context_vk, id);
779 wined3d_null_image_vk_cleanup(&r->image_1d, context_vk, id);
780 wined3d_context_vk_reference_bo(context_vk, &r->bo);
781 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
784 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
786 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
787 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
788 VkBufferViewCreateInfo buffer_create_info;
789 const struct wined3d_vk_info *vk_info;
790 VkImageViewCreateInfo view_desc;
791 VkResult vr;
793 vk_info = context_vk->vk_info;
795 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
796 buffer_create_info.pNext = NULL;
797 buffer_create_info.flags = 0;
798 buffer_create_info.buffer = r->bo.vk_buffer;
799 buffer_create_info.format = VK_FORMAT_R32_UINT;
800 buffer_create_info.offset = r->bo.buffer_offset;
801 buffer_create_info.range = r->bo.size;
803 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
804 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
806 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
807 return false;
809 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
811 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
812 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
813 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
815 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
816 goto fail;
818 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
820 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
821 view_desc.pNext = NULL;
822 view_desc.flags = 0;
823 view_desc.image = r->image_1d.vk_image;
824 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
825 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
826 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
827 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
828 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
829 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
830 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
831 view_desc.subresourceRange.baseMipLevel = 0;
832 view_desc.subresourceRange.levelCount = 1;
833 view_desc.subresourceRange.baseArrayLayer = 0;
834 view_desc.subresourceRange.layerCount = 1;
835 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
837 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
838 goto fail;
840 v->vk_info_1d.sampler = VK_NULL_HANDLE;
841 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
842 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
844 view_desc.image = r->image_2d.vk_image;
845 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
846 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
848 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
849 goto fail;
851 v->vk_info_2d.sampler = VK_NULL_HANDLE;
852 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
853 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
855 view_desc.image = r->image_2dms.vk_image;
856 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
857 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
859 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
860 goto fail;
862 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
863 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
864 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
866 view_desc.image = r->image_3d.vk_image;
867 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
868 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
870 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
871 goto fail;
873 v->vk_info_3d.sampler = VK_NULL_HANDLE;
874 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
875 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
877 view_desc.image = r->image_2d.vk_image;
878 view_desc.subresourceRange.layerCount = 6;
879 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
880 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
882 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
883 goto fail;
885 v->vk_info_cube.sampler = VK_NULL_HANDLE;
886 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
887 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
889 view_desc.subresourceRange.layerCount = 1;
890 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
891 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
893 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
894 goto fail;
896 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
897 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
898 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
900 view_desc.image = r->image_2dms.vk_image;
901 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
902 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
904 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
905 goto fail;
907 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
908 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
909 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
911 return true;
913 fail:
914 if (v->vk_info_2d_array.imageView)
915 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
916 if (v->vk_info_cube.imageView)
917 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
918 if (v->vk_info_3d.imageView)
919 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
920 if (v->vk_info_2dms.imageView)
921 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
922 if (v->vk_info_2d.imageView)
923 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
924 if (v->vk_info_1d.imageView)
925 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
926 if (v->vk_view_buffer_float)
927 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
928 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
929 return false;
932 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
934 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
935 uint64_t id = context_vk->current_command_buffer.id;
937 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
938 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
939 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
940 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
941 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
942 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
943 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
945 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
946 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
949 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
951 unsigned int screensaver_active;
953 TRACE("device %p, window %p.\n", device, window);
955 if (!wined3d_register_window(NULL, window, device, 0))
957 ERR("Failed to register window %p.\n", window);
958 return E_FAIL;
961 InterlockedExchangePointer((void **)&device->focus_window, window);
962 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
963 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
964 if ((device->restore_screensaver = !!screensaver_active))
965 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
967 return WINED3D_OK;
970 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
972 TRACE("device %p.\n", device);
974 if (device->focus_window) wined3d_unregister_window(device->focus_window);
975 InterlockedExchangePointer((void **)&device->focus_window, NULL);
976 if (device->restore_screensaver)
978 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
979 device->restore_screensaver = FALSE;
983 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
985 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
986 unsigned int i;
988 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
990 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
992 if (device->back_buffer_view)
993 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
995 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
998 void wined3d_device_delete_opengl_contexts_cs(void *object)
1000 struct wined3d_swapchain_gl *swapchain_gl;
1001 struct wined3d_device *device = object;
1002 struct wined3d_context_gl *context_gl;
1003 struct wined3d_device_gl *device_gl;
1004 struct wined3d_context *context;
1005 struct wined3d_shader *shader;
1007 TRACE("device %p.\n", device);
1009 device_gl = wined3d_device_gl(device);
1011 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
1013 device->shader_backend->shader_destroy(shader);
1016 context = context_acquire(device, NULL, 0);
1017 context_gl = wined3d_context_gl(context);
1018 device->blitter->ops->blitter_destroy(device->blitter, context);
1019 device->shader_backend->shader_free_private(device, context);
1020 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
1021 wined3d_device_destroy_default_samplers(device, context);
1022 context_release(context);
1024 while (device->context_count)
1026 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
1027 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
1028 else
1029 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1033 void wined3d_device_create_primary_opengl_context_cs(void *object)
1035 struct wined3d_device *device = object;
1036 struct wined3d_context_gl *context_gl;
1037 struct wined3d_swapchain *swapchain;
1038 struct wined3d_context *context;
1039 struct wined3d_texture *target;
1040 HRESULT hr;
1042 TRACE("device %p.\n", device);
1044 swapchain = device->swapchains[0];
1045 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1046 if (!(context = context_acquire(device, target, 0)))
1048 WARN("Failed to acquire context.\n");
1049 return;
1052 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1053 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1055 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1056 context_release(context);
1057 return;
1060 if (!(device->blitter = wined3d_cpu_blitter_create()))
1062 ERR("Failed to create CPU blitter.\n");
1063 device->shader_backend->shader_free_private(device, NULL);
1064 context_release(context);
1065 return;
1068 context_gl = wined3d_context_gl(context);
1070 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1071 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1072 wined3d_arbfp_blitter_create(&device->blitter, device);
1073 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1074 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1076 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device), context_gl);
1077 wined3d_device_create_default_samplers(device, context);
1078 context_release(context);
1081 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1083 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1084 const struct wined3d_swapchain_desc *swapchain_desc;
1085 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1086 DWORD clear_flags = 0;
1087 unsigned int i;
1088 HRESULT hr;
1090 TRACE("device %p, swapchain %p.\n", device, swapchain);
1092 if (device->d3d_initialized)
1093 return WINED3DERR_INVALIDCALL;
1095 device->swapchain_count = 1;
1096 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1098 ERR("Failed to allocate swapchain array.\n");
1099 hr = E_OUTOFMEMORY;
1100 goto err_out;
1102 device->swapchains[0] = swapchain;
1104 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1106 if (fb->render_targets[i])
1107 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1109 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1111 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1112 goto err_out;
1113 device->d3d_initialized = TRUE;
1115 swapchain_desc = &swapchain->state.desc;
1116 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1118 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1119 struct wined3d_view_desc view_desc;
1121 view_desc.format_id = back_buffer->format->id;
1122 view_desc.flags = 0;
1123 view_desc.u.texture.level_idx = 0;
1124 view_desc.u.texture.level_count = 1;
1125 view_desc.u.texture.layer_idx = 0;
1126 view_desc.u.texture.layer_count = 1;
1127 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1128 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1130 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1131 device->adapter->adapter_ops->adapter_uninit_3d(device);
1132 device->d3d_initialized = FALSE;
1133 goto err_out;
1137 device_init_swapchain_state(device, swapchain);
1139 TRACE("All defaults now set up.\n");
1141 /* Clear the screen. */
1142 if (device->back_buffer_view)
1143 clear_flags |= WINED3DCLEAR_TARGET;
1144 if (swapchain_desc->enable_auto_depth_stencil)
1145 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1146 if (clear_flags)
1147 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1149 if (wined3d_settings.logo)
1150 device_load_logo(device, wined3d_settings.logo);
1152 return WINED3D_OK;
1154 err_out:
1155 heap_free(device->swapchains);
1156 device->swapchains = NULL;
1157 device->swapchain_count = 0;
1159 return hr;
1162 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1164 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1166 wined3d_sampler_decref(sampler);
1169 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1171 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1173 wined3d_rasterizer_state_decref(state);
1176 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1178 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1180 wined3d_blend_state_decref(blend_state);
1183 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1185 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1187 wined3d_depth_stencil_state_decref(state);
1190 void wined3d_device_uninit_3d(struct wined3d_device *device)
1192 struct wined3d_state *state = device->cs->c.state;
1193 struct wined3d_resource *resource, *cursor;
1194 struct wined3d_rendertarget_view *view;
1195 struct wined3d_texture *texture;
1197 TRACE("device %p.\n", device);
1199 if (!device->d3d_initialized)
1201 ERR("Called while 3D support was not initialised.\n");
1202 return;
1205 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1207 device->swapchain_count = 0;
1209 if ((texture = device->logo_texture))
1211 device->logo_texture = NULL;
1212 wined3d_texture_decref(texture);
1215 if ((texture = device->cursor_texture))
1217 device->cursor_texture = NULL;
1218 wined3d_texture_decref(texture);
1221 wined3d_cs_emit_reset_state(device->cs);
1222 state_cleanup(state);
1224 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1225 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1226 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1227 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1229 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1231 TRACE("Unloading resource %p.\n", resource);
1232 wined3d_cs_emit_unload_resource(device->cs, resource);
1235 device->adapter->adapter_ops->adapter_uninit_3d(device);
1236 device->d3d_initialized = FALSE;
1238 if ((view = device->auto_depth_stencil_view))
1240 device->auto_depth_stencil_view = NULL;
1241 if (wined3d_rendertarget_view_decref(view))
1242 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1245 if ((view = device->back_buffer_view))
1247 device->back_buffer_view = NULL;
1248 wined3d_rendertarget_view_decref(view);
1251 heap_free(device->swapchains);
1252 device->swapchains = NULL;
1254 wined3d_state_reset(state, &device->adapter->d3d_info);
1257 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1258 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1259 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1261 * There is no way to deactivate thread safety once it is enabled.
1263 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1265 TRACE("device %p.\n", device);
1267 /* For now just store the flag (needed in case of ddraw). */
1268 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1271 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1273 const struct wined3d_driver_info *driver_info;
1275 TRACE("device %p.\n", device);
1277 driver_info = &device->adapter->driver_info;
1279 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1280 wine_dbgstr_longlong(driver_info->vram_bytes),
1281 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1282 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1284 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1287 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1288 struct wined3d_buffer *buffer, UINT offset)
1290 struct wined3d_stream_output *stream;
1291 struct wined3d_buffer *prev_buffer;
1293 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1295 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1297 WARN("Invalid stream output %u.\n", idx);
1298 return;
1301 stream = &device->cs->c.state->stream_output[idx];
1302 prev_buffer = stream->buffer;
1304 if (buffer)
1305 wined3d_buffer_incref(buffer);
1306 stream->buffer = buffer;
1307 stream->offset = offset;
1308 wined3d_cs_emit_set_stream_output(device->cs, idx, buffer, offset);
1309 if (prev_buffer)
1310 wined3d_buffer_decref(prev_buffer);
1313 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1314 UINT idx, UINT *offset)
1316 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1318 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1320 WARN("Invalid stream output %u.\n", idx);
1321 return NULL;
1324 if (offset)
1325 *offset = device->cs->c.state->stream_output[idx].offset;
1326 return device->cs->c.state->stream_output[idx].buffer;
1329 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1330 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1332 struct wined3d_stream_state *stream;
1333 struct wined3d_buffer *prev_buffer;
1335 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1336 device, stream_idx, buffer, offset, stride);
1338 if (stream_idx >= WINED3D_MAX_STREAMS)
1340 WARN("Stream index %u out of range.\n", stream_idx);
1341 return WINED3DERR_INVALIDCALL;
1343 else if (offset & 0x3)
1345 WARN("Offset %u is not 4 byte aligned.\n", offset);
1346 return WINED3DERR_INVALIDCALL;
1349 stream = &device->cs->c.state->streams[stream_idx];
1350 prev_buffer = stream->buffer;
1352 if (prev_buffer == buffer
1353 && stream->stride == stride
1354 && stream->offset == offset)
1356 TRACE("Application is setting the old values over, nothing to do.\n");
1357 return WINED3D_OK;
1360 stream->buffer = buffer;
1361 stream->stride = stride;
1362 stream->offset = offset;
1363 if (buffer)
1364 wined3d_buffer_incref(buffer);
1365 wined3d_cs_emit_set_stream_source(device->cs, stream_idx, buffer, offset, stride);
1366 if (prev_buffer)
1367 wined3d_buffer_decref(prev_buffer);
1369 return WINED3D_OK;
1372 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1373 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1375 const struct wined3d_stream_state *stream;
1377 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1378 device, stream_idx, buffer, offset, stride);
1380 if (stream_idx >= WINED3D_MAX_STREAMS)
1382 WARN("Stream index %u out of range.\n", stream_idx);
1383 return WINED3DERR_INVALIDCALL;
1386 stream = &device->cs->c.state->streams[stream_idx];
1387 *buffer = stream->buffer;
1388 if (offset)
1389 *offset = stream->offset;
1390 *stride = stream->stride;
1392 return WINED3D_OK;
1395 static void wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1397 struct wined3d_stream_state *stream;
1398 UINT old_flags, old_freq;
1400 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1402 stream = &device->cs->c.state->streams[stream_idx];
1403 old_flags = stream->flags;
1404 old_freq = stream->frequency;
1406 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1407 stream->frequency = divider & 0x7fffff;
1408 if (stream->frequency != old_freq || stream->flags != old_flags)
1409 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1412 static void wined3d_device_set_transform(struct wined3d_device *device,
1413 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1415 TRACE("device %p, state %s, matrix %p.\n",
1416 device, debug_d3dtstype(state), matrix);
1417 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1418 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1419 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1420 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1422 /* If the new matrix is the same as the current one,
1423 * we cut off any further processing. this seems to be a reasonable
1424 * optimization because as was noticed, some apps (warcraft3 for example)
1425 * tend towards setting the same matrix repeatedly for some reason.
1427 * From here on we assume that the new matrix is different, wherever it matters. */
1428 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1430 TRACE("The application is setting the same matrix over again.\n");
1431 return;
1434 device->cs->c.state->transforms[state] = *matrix;
1435 wined3d_cs_emit_set_transform(device->cs, state, matrix);
1438 static void wined3d_device_get_transform(const struct wined3d_device *device,
1439 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1441 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1443 *matrix = device->cs->c.state->transforms[state];
1446 /* Note lights are real special cases. Although the device caps state only
1447 * e.g. 8 are supported, you can reference any indexes you want as long as
1448 * that number max are enabled at any one point in time. Therefore since the
1449 * indices can be anything, we need a hashmap of them. However, this causes
1450 * stateblock problems. When capturing the state block, I duplicate the
1451 * hashmap, but when recording, just build a chain pretty much of commands to
1452 * be replayed. */
1453 static void wined3d_device_set_light(struct wined3d_device *device,
1454 UINT light_idx, const struct wined3d_light *light)
1456 struct wined3d_light_info *object = NULL;
1457 float rho;
1459 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1461 if (FAILED(wined3d_light_state_set_light(&device->cs->c.state->light_state, light_idx, light, &object)))
1462 return;
1464 /* Initialize the object. */
1465 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1466 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1467 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1468 light_idx, light->type, debug_color(&light->diffuse),
1469 debug_color(&light->specular), debug_color(&light->ambient),
1470 light->position.x, light->position.y, light->position.z,
1471 light->direction.x, light->direction.y, light->direction.z,
1472 light->range, light->falloff, light->theta, light->phi);
1474 switch (light->type)
1476 case WINED3D_LIGHT_POINT:
1477 /* Position */
1478 object->position.x = light->position.x;
1479 object->position.y = light->position.y;
1480 object->position.z = light->position.z;
1481 object->position.w = 1.0f;
1482 object->cutoff = 180.0f;
1483 /* FIXME: Range */
1484 break;
1486 case WINED3D_LIGHT_DIRECTIONAL:
1487 /* Direction */
1488 object->direction.x = -light->direction.x;
1489 object->direction.y = -light->direction.y;
1490 object->direction.z = -light->direction.z;
1491 object->direction.w = 0.0f;
1492 object->exponent = 0.0f;
1493 object->cutoff = 180.0f;
1494 break;
1496 case WINED3D_LIGHT_SPOT:
1497 /* Position */
1498 object->position.x = light->position.x;
1499 object->position.y = light->position.y;
1500 object->position.z = light->position.z;
1501 object->position.w = 1.0f;
1503 /* Direction */
1504 object->direction.x = light->direction.x;
1505 object->direction.y = light->direction.y;
1506 object->direction.z = light->direction.z;
1507 object->direction.w = 0.0f;
1509 /* opengl-ish and d3d-ish spot lights use too different models
1510 * for the light "intensity" as a function of the angle towards
1511 * the main light direction, so we only can approximate very
1512 * roughly. However, spot lights are rather rarely used in games
1513 * (if ever used at all). Furthermore if still used, probably
1514 * nobody pays attention to such details. */
1515 if (!light->falloff)
1517 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1518 * equations have the falloff resp. exponent parameter as an
1519 * exponent, so the spot light lighting will always be 1.0 for
1520 * both of them, and we don't have to care for the rest of the
1521 * rather complex calculation. */
1522 object->exponent = 0.0f;
1524 else
1526 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1527 if (rho < 0.0001f)
1528 rho = 0.0001f;
1529 object->exponent = -0.3f / logf(cosf(rho / 2));
1532 if (object->exponent > 128.0f)
1533 object->exponent = 128.0f;
1535 object->cutoff = (float)(light->phi * 90 / M_PI);
1536 /* FIXME: Range */
1537 break;
1539 case WINED3D_LIGHT_PARALLELPOINT:
1540 object->position.x = light->position.x;
1541 object->position.y = light->position.y;
1542 object->position.z = light->position.z;
1543 object->position.w = 1.0f;
1544 break;
1546 default:
1547 FIXME("Unrecognized light type %#x.\n", light->type);
1550 wined3d_cs_emit_set_light(device->cs, object);
1553 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1555 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1556 struct wined3d_light_info *light_info;
1558 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1560 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1561 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1563 TRACE("Light enabled requested but light not defined, so defining one!\n");
1564 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1566 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1568 FIXME("Adding default lights has failed dismally\n");
1569 return;
1573 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1574 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1577 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1578 UINT plane_idx, const struct wined3d_vec4 *plane)
1580 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1582 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1584 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1586 TRACE("Application has requested clipplane this device doesn't support.\n");
1587 return WINED3DERR_INVALIDCALL;
1590 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1592 TRACE("Application is setting old values over, nothing to do.\n");
1593 return WINED3D_OK;
1596 clip_planes[plane_idx] = *plane;
1598 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1600 return WINED3D_OK;
1603 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1604 const struct wined3d_clip_status *clip_status)
1606 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1608 if (!clip_status)
1609 return WINED3DERR_INVALIDCALL;
1611 return WINED3D_OK;
1614 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1615 struct wined3d_clip_status *clip_status)
1617 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1619 if (!clip_status)
1620 return WINED3DERR_INVALIDCALL;
1622 return WINED3D_OK;
1625 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1627 TRACE("device %p, material %p.\n", device, material);
1629 device->cs->c.state->material = *material;
1630 wined3d_cs_emit_set_material(device->cs, material);
1633 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1634 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1636 struct wined3d_state *state = device->cs->c.state;
1637 enum wined3d_format_id prev_format;
1638 struct wined3d_buffer *prev_buffer;
1639 unsigned int prev_offset;
1641 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1642 device, buffer, debug_d3dformat(format_id), offset);
1644 prev_buffer = state->index_buffer;
1645 prev_format = state->index_format;
1646 prev_offset = state->index_offset;
1648 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
1649 return;
1651 if (buffer)
1652 wined3d_buffer_incref(buffer);
1653 state->index_buffer = buffer;
1654 state->index_format = format_id;
1655 state->index_offset = offset;
1656 wined3d_cs_emit_set_index_buffer(device->cs, buffer, format_id, offset);
1657 if (prev_buffer)
1658 wined3d_buffer_decref(prev_buffer);
1661 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1662 enum wined3d_format_id *format, unsigned int *offset)
1664 const struct wined3d_state *state = device->cs->c.state;
1666 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1668 *format = state->index_format;
1669 if (offset)
1670 *offset = state->index_offset;
1671 return state->index_buffer;
1674 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1676 TRACE("device %p, base_index %d.\n", device, base_index);
1678 device->cs->c.state->base_vertex_index = base_index;
1681 void CDECL wined3d_device_set_viewports(struct wined3d_device *device, unsigned int viewport_count,
1682 const struct wined3d_viewport *viewports)
1684 unsigned int i;
1686 TRACE("device %p, viewport_count %u, viewports %p.\n", device, viewport_count, viewports);
1688 for (i = 0; i < viewport_count; ++i)
1690 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1691 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1694 wined3d_device_context_set_viewports(&device->cs->c, viewport_count, viewports);
1697 void CDECL wined3d_device_get_viewports(const struct wined3d_device *device, unsigned int *viewport_count,
1698 struct wined3d_viewport *viewports)
1700 const struct wined3d_state *state = device->cs->c.state;
1701 unsigned int count;
1703 TRACE("device %p, viewport_count %p, viewports %p.\n", device, viewport_count, viewports);
1705 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1706 if (count && viewports)
1707 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1708 if (viewport_count)
1709 *viewport_count = state->viewport_count;
1712 static void resolve_depth_buffer(struct wined3d_device *device)
1714 const struct wined3d_state *state = device->cs->c.state;
1715 struct wined3d_rendertarget_view *src_view;
1716 struct wined3d_resource *dst_resource;
1717 struct wined3d_texture *dst_texture;
1719 if (!(dst_texture = state->textures[0]))
1720 return;
1721 dst_resource = &dst_texture->resource;
1722 if (!dst_resource->format->depth_size)
1723 return;
1724 if (!(src_view = state->fb.depth_stencil))
1725 return;
1727 wined3d_device_resolve_sub_resource(device, dst_resource, 0,
1728 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1731 void CDECL wined3d_device_set_blend_state(struct wined3d_device *device,
1732 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1734 TRACE("device %p, blend_state %p, blend_factor %s, sample_mask %#x.\n",
1735 device, blend_state, debug_color(blend_factor), sample_mask);
1737 wined3d_device_context_set_blend_state(&device->cs->c, blend_state, blend_factor, sample_mask);
1740 struct wined3d_blend_state * CDECL wined3d_device_get_blend_state(const struct wined3d_device *device,
1741 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1743 const struct wined3d_state *state = device->cs->c.state;
1745 TRACE("device %p, blend_factor %p, sample_mask %p.\n", device, blend_factor, sample_mask);
1747 *blend_factor = state->blend_factor;
1748 *sample_mask = state->sample_mask;
1749 return state->blend_state;
1752 void CDECL wined3d_device_set_depth_stencil_state(struct wined3d_device *device,
1753 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1755 TRACE("device %p, depth_stencil_state %p, stencil_ref %u.\n", device, depth_stencil_state, stencil_ref);
1757 wined3d_device_context_set_depth_stencil_state(&device->cs->c, depth_stencil_state, stencil_ref);
1760 struct wined3d_depth_stencil_state * CDECL wined3d_device_get_depth_stencil_state(const struct wined3d_device *device,
1761 unsigned int *stencil_ref)
1763 const struct wined3d_state *state = device->cs->c.state;
1765 TRACE("device %p, stencil_ref %p.\n", device, stencil_ref);
1767 *stencil_ref = state->stencil_ref;
1768 return state->depth_stencil_state;
1771 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1772 struct wined3d_rasterizer_state *rasterizer_state)
1774 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
1776 wined3d_device_context_set_rasterizer_state(&device->cs->c, rasterizer_state);
1779 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
1781 TRACE("device %p.\n", device);
1783 return device->cs->c.state->rasterizer_state;
1786 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1787 enum wined3d_render_state state, DWORD value)
1789 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1791 if (state > WINEHIGHEST_RENDER_STATE)
1793 WARN("Unhandled render state %#x.\n", state);
1794 return;
1797 if (value == device->cs->c.state->render_states[state])
1798 TRACE("Application is setting the old value over, nothing to do.\n");
1799 else
1801 device->cs->c.state->render_states[state] = value;
1802 wined3d_cs_emit_set_render_state(device->cs, state, value);
1805 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1807 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1808 resolve_depth_buffer(device);
1812 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1814 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1816 return device->cs->c.state->render_states[state];
1819 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1820 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1822 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1823 device, sampler_idx, debug_d3dsamplerstate(state), value);
1825 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1827 TRACE("Application is setting the old value over, nothing to do.\n");
1828 return;
1831 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1832 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
1835 void CDECL wined3d_device_set_scissor_rects(struct wined3d_device *device, unsigned int rect_count,
1836 const RECT *rects)
1838 unsigned int i;
1840 TRACE("device %p, rect_count %u, rects %p.\n", device, rect_count, rects);
1842 for (i = 0; i < rect_count; ++i)
1844 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1847 wined3d_device_context_set_scissor_rects(&device->cs->c, rect_count, rects);
1850 void CDECL wined3d_device_get_scissor_rects(const struct wined3d_device *device, unsigned int *rect_count, RECT *rects)
1852 const struct wined3d_state *state = device->cs->c.state;
1853 unsigned int count;
1855 TRACE("device %p, rect_count %p, rects %p.\n", device, rect_count, rects);
1857 count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1;
1858 if (count && rects)
1859 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1860 if (rect_count)
1861 *rect_count = state->scissor_rect_count;
1864 void CDECL wined3d_device_set_state(struct wined3d_device *device, struct wined3d_state *state)
1866 struct wined3d_device_context *context = &device->cs->c;
1867 const struct wined3d_light_info *light;
1868 unsigned int i, j;
1870 TRACE("device %p, state %p.\n", device, state);
1872 device->cs->c.state = state;
1873 wined3d_cs_emit_set_feature_level(device->cs, state->feature_level);
1875 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
1877 wined3d_device_context_emit_set_rendertarget_view(context, i, state->fb.render_targets[i]);
1880 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1881 wined3d_cs_emit_set_vertex_declaration(device->cs, state->vertex_declaration);
1883 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
1885 wined3d_cs_emit_set_stream_output(device->cs, i,
1886 state->stream_output[i].buffer, state->stream_output[i].offset);
1889 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1891 wined3d_cs_emit_set_stream_source(device->cs, i, state->streams[i].buffer,
1892 state->streams[i].offset, state->streams[i].stride);
1895 wined3d_cs_emit_set_index_buffer(device->cs, state->index_buffer, state->index_format, state->index_offset);
1897 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1899 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1901 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1902 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
1903 wined3d_device_context_emit_set_constant_buffer(context, i, j, state->cb[i][j]);
1904 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
1906 wined3d_device_context_emit_set_sampler(context, i, j, state->sampler[i][j]);
1908 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
1910 wined3d_device_context_emit_set_shader_resource_view(context, i, j, state->shader_resource_view[i][j]);
1914 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1916 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
1918 wined3d_device_context_emit_set_unordered_access_view(context, i, j,
1919 state->unordered_access_view[i][j], ~0);
1923 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F,
1924 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1925 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I,
1926 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1927 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B,
1928 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1930 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F,
1931 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1932 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I,
1933 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1934 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B,
1935 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1937 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1939 wined3d_cs_emit_set_texture(device->cs, i, state->textures[i]);
1940 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1942 wined3d_cs_emit_set_sampler_state(device->cs, i, j, state->sampler_states[i][j]);
1946 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1948 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1950 wined3d_cs_emit_set_texture_state(device->cs, i, j, state->texture_states[i][j]);
1954 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1956 wined3d_cs_emit_set_transform(device->cs, i, state->transforms + i);
1959 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1961 wined3d_cs_emit_set_clip_plane(device->cs, i, state->clip_planes + i);
1964 wined3d_cs_emit_set_material(device->cs, &state->material);
1966 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1967 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1969 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1971 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1973 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
1974 wined3d_cs_emit_set_light_enable(device->cs, light->OriginalIndex, light->glIndex != -1);
1978 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1980 wined3d_cs_emit_set_render_state(device->cs, i, state->render_states[i]);
1983 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1984 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1985 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1988 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1990 TRACE("device %p.\n", device);
1992 return device->cs->c.state;
1995 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1997 TRACE("device %p.\n", device);
1999 return &device->cs->c;
2002 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2003 struct wined3d_vertex_declaration *declaration)
2005 struct wined3d_state *state = device->cs->c.state;
2006 struct wined3d_vertex_declaration *prev;
2008 TRACE("device %p, declaration %p.\n", device, declaration);
2010 prev = state->vertex_declaration;
2011 if (declaration == prev)
2012 return;
2014 if (declaration)
2015 wined3d_vertex_declaration_incref(declaration);
2016 state->vertex_declaration = declaration;
2017 wined3d_cs_emit_set_vertex_declaration(device->cs, declaration);
2018 if (prev)
2019 wined3d_vertex_declaration_decref(prev);
2022 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2024 TRACE("device %p.\n", device);
2026 return device->cs->c.state->vertex_declaration;
2029 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
2030 enum wined3d_shader_type type, struct wined3d_shader *shader)
2032 struct wined3d_state *state = context->state;
2033 struct wined3d_shader *prev;
2035 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
2037 prev = state->shader[type];
2038 if (shader == prev)
2039 return;
2041 if (shader)
2042 wined3d_shader_incref(shader);
2043 state->shader[type] = shader;
2044 wined3d_device_context_emit_set_shader(context, type, shader);
2045 if (prev)
2046 wined3d_shader_decref(prev);
2049 void CDECL wined3d_device_context_set_constant_buffer(struct wined3d_device_context *context,
2050 enum wined3d_shader_type type, unsigned int idx, struct wined3d_buffer *buffer)
2052 struct wined3d_state *state = context->state;
2053 struct wined3d_buffer *prev;
2055 TRACE("context %p, type %#x, idx %u, buffer %p.\n", context, type, idx, buffer);
2057 if (idx >= MAX_CONSTANT_BUFFERS)
2059 WARN("Invalid constant buffer index %u.\n", idx);
2060 return;
2063 prev = state->cb[type][idx];
2064 if (buffer == prev)
2065 return;
2067 if (buffer)
2068 wined3d_buffer_incref(buffer);
2069 state->cb[type][idx] = buffer;
2070 wined3d_device_context_emit_set_constant_buffer(context, type, idx, buffer);
2071 if (prev)
2072 wined3d_buffer_decref(prev);
2075 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
2076 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
2078 struct wined3d_state *state = context->state;
2079 struct wined3d_blend_state *prev;
2081 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
2082 context, blend_state, blend_factor, sample_mask);
2084 prev = state->blend_state;
2085 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
2086 && sample_mask == state->sample_mask)
2087 return;
2089 if (blend_state)
2090 wined3d_blend_state_incref(blend_state);
2091 state->blend_state = blend_state;
2092 state->blend_factor = *blend_factor;
2093 state->sample_mask = sample_mask;
2094 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
2095 if (prev)
2096 wined3d_blend_state_decref(prev);
2099 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
2100 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
2102 struct wined3d_state *state = context->state;
2103 struct wined3d_depth_stencil_state *prev;
2105 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
2107 prev = state->depth_stencil_state;
2108 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
2109 return;
2111 if (depth_stencil_state)
2112 wined3d_depth_stencil_state_incref(depth_stencil_state);
2113 state->depth_stencil_state = depth_stencil_state;
2114 state->stencil_ref = stencil_ref;
2115 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
2116 if (prev)
2117 wined3d_depth_stencil_state_decref(prev);
2120 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
2121 struct wined3d_rasterizer_state *rasterizer_state)
2123 struct wined3d_state *state = context->state;
2124 struct wined3d_rasterizer_state *prev;
2126 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
2128 prev = state->rasterizer_state;
2129 if (prev == rasterizer_state)
2130 return;
2132 if (rasterizer_state)
2133 wined3d_rasterizer_state_incref(rasterizer_state);
2134 state->rasterizer_state = rasterizer_state;
2135 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
2136 if (prev)
2137 wined3d_rasterizer_state_decref(prev);
2140 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
2141 const struct wined3d_viewport *viewports)
2143 struct wined3d_state *state = context->state;
2144 unsigned int i;
2146 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
2148 for (i = 0; i < viewport_count; ++i)
2150 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
2151 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
2154 if (viewport_count)
2155 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
2156 else
2157 memset(state->viewports, 0, sizeof(state->viewports));
2158 state->viewport_count = viewport_count;
2160 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
2163 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
2164 const RECT *rects)
2166 struct wined3d_state *state = context->state;
2167 unsigned int i;
2169 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
2171 for (i = 0; i < rect_count; ++i)
2173 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2176 if (state->scissor_rect_count == rect_count
2177 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
2179 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2180 return;
2183 if (rect_count)
2184 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
2185 else
2186 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
2187 state->scissor_rect_count = rect_count;
2189 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
2192 void CDECL wined3d_device_context_set_shader_resource_view(struct wined3d_device_context *context,
2193 enum wined3d_shader_type type, unsigned int idx, struct wined3d_shader_resource_view *view)
2195 struct wined3d_state *state = context->state;
2196 const struct wined3d_rendertarget_view *dsv;
2197 struct wined3d_shader_resource_view *prev;
2199 TRACE("context %p, type %#x, idx %u, view %p.\n", context, type, idx, view);
2201 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2203 WARN("Invalid view index %u.\n", idx);
2204 return;
2207 prev = state->shader_resource_view[type][idx];
2208 if (view == prev)
2209 return;
2211 if (view && (wined3d_is_srv_rtv_bound(view)
2212 || ((dsv = state->fb.depth_stencil)
2213 && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2215 WARN("Application is trying to bind resource which is attached as render target.\n");
2216 view = NULL;
2219 if (view)
2221 wined3d_shader_resource_view_incref(view);
2222 wined3d_srv_bind_count_inc(view);
2225 state->shader_resource_view[type][idx] = view;
2226 wined3d_device_context_emit_set_shader_resource_view(context, type, idx, view);
2227 if (prev)
2229 wined3d_srv_bind_count_dec(prev);
2230 wined3d_shader_resource_view_decref(prev);
2234 void CDECL wined3d_device_context_set_sampler(struct wined3d_device_context *context,
2235 enum wined3d_shader_type type, unsigned int idx, struct wined3d_sampler *sampler)
2237 struct wined3d_state *state = context->state;
2238 struct wined3d_sampler *prev;
2240 TRACE("context %p, type %#x, idx %u, sampler %p.\n", context, type, idx, sampler);
2242 if (idx >= MAX_SAMPLER_OBJECTS)
2244 WARN("Invalid sampler index %u.\n", idx);
2245 return;
2248 prev = state->sampler[type][idx];
2249 if (sampler == prev)
2250 return;
2252 if (sampler)
2253 wined3d_sampler_incref(sampler);
2254 state->sampler[type][idx] = sampler;
2255 wined3d_device_context_emit_set_sampler(context, type, idx, sampler);
2256 if (prev)
2257 wined3d_sampler_decref(prev);
2260 void CDECL wined3d_device_context_set_unordered_access_view(struct wined3d_device_context *context,
2261 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2262 unsigned int initial_count)
2264 struct wined3d_state *state = context->state;
2265 struct wined3d_unordered_access_view *prev;
2267 TRACE("context %p, pipeline %#x, idx %u, uav %p, initial_count %u.\n", context, pipeline, idx, uav, initial_count);
2269 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2271 WARN("Invalid UAV index %u.\n", idx);
2272 return;
2275 prev = state->unordered_access_view[pipeline][idx];
2276 if (uav == prev && initial_count == ~0u)
2277 return;
2279 if (uav)
2280 wined3d_unordered_access_view_incref(uav);
2281 state->unordered_access_view[pipeline][idx] = uav;
2282 wined3d_device_context_emit_set_unordered_access_view(context, pipeline, idx, uav, initial_count);
2283 if (prev)
2284 wined3d_unordered_access_view_decref(prev);
2287 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2288 const struct wined3d_rendertarget_view *view, BOOL dsv)
2290 struct wined3d_state *state = context->state;
2292 if (view && wined3d_is_rtv_srv_bound(view))
2294 const struct wined3d_resource *resource = view->resource;
2295 const struct wined3d_shader_resource_view *srv;
2296 unsigned int i, j;
2298 WARN("Application sets bound resource as render target.\n");
2300 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2301 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2302 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2303 && ((!dsv && wined3d_is_srv_rtv_bound(srv))
2304 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2305 wined3d_device_context_set_shader_resource_view(context, i, j, NULL);
2309 HRESULT CDECL wined3d_device_context_set_rendertarget_view(struct wined3d_device_context *context,
2310 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
2312 struct wined3d_state *state = context->state;
2313 struct wined3d_rendertarget_view *prev;
2314 unsigned int max_rt_count;
2316 TRACE("context %p, view_idx %u, view %p, set_viewport %#x.\n",
2317 context, view_idx, view, set_viewport);
2319 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2320 if (view_idx >= max_rt_count)
2322 WARN("Only %u render targets are supported.\n", max_rt_count);
2323 return WINED3DERR_INVALIDCALL;
2326 if (view && !(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2328 WARN("View resource %p doesn't have render target bind flags.\n", view->resource);
2329 return WINED3DERR_INVALIDCALL;
2332 /* Set the viewport and scissor rectangles, if requested. Tests show that
2333 * stateblock recording is ignored, the change goes directly into the
2334 * primary stateblock. */
2335 if (!view_idx && set_viewport)
2337 state->viewports[0].x = 0;
2338 state->viewports[0].y = 0;
2339 state->viewports[0].width = view->width;
2340 state->viewports[0].height = view->height;
2341 state->viewports[0].min_z = 0.0f;
2342 state->viewports[0].max_z = 1.0f;
2343 state->viewport_count = 1;
2344 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2346 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
2347 state->scissor_rect_count = 1;
2348 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2351 prev = state->fb.render_targets[view_idx];
2352 if (view == prev)
2353 return WINED3D_OK;
2355 if (view)
2357 wined3d_rendertarget_view_incref(view);
2358 wined3d_rtv_bind_count_inc(view);
2360 state->fb.render_targets[view_idx] = view;
2361 wined3d_device_context_emit_set_rendertarget_view(context, view_idx, view);
2362 /* Release after the assignment, to prevent device_resource_released()
2363 * from seeing the surface as still in use. */
2364 if (prev)
2366 wined3d_rtv_bind_count_dec(prev);
2367 wined3d_rendertarget_view_decref(prev);
2370 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2372 return WINED3D_OK;
2375 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2376 struct wined3d_rendertarget_view *view)
2378 struct wined3d_fb_state *fb = &context->state->fb;
2379 struct wined3d_rendertarget_view *prev;
2381 TRACE("context %p, view %p.\n", context, view);
2383 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2385 WARN("View resource %p has incompatible %s bind flags.\n",
2386 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2387 return WINED3DERR_INVALIDCALL;
2390 prev = fb->depth_stencil;
2391 if (prev == view)
2393 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2394 return WINED3D_OK;
2397 if ((fb->depth_stencil = view))
2398 wined3d_rendertarget_view_incref(view);
2399 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2400 if (prev)
2401 wined3d_rendertarget_view_decref(prev);
2402 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2404 return WINED3D_OK;
2407 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2408 struct wined3d_query *predicate, BOOL value)
2410 struct wined3d_state *state = context->state;
2411 struct wined3d_query *prev;
2413 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2415 prev = state->predicate;
2416 if (predicate)
2418 FIXME("Predicated rendering not implemented.\n");
2419 wined3d_query_incref(predicate);
2421 state->predicate = predicate;
2422 state->predicate_value = value;
2423 wined3d_device_context_emit_set_predication(context, predicate, value);
2424 if (prev)
2425 wined3d_query_decref(prev);
2428 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2430 TRACE("device %p, shader %p.\n", device, shader);
2432 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, shader);
2435 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2437 TRACE("device %p.\n", device);
2439 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_VERTEX];
2442 void CDECL wined3d_device_set_constant_buffer(struct wined3d_device *device,
2443 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2445 TRACE("device %p, type %#x, idx %u, buffer %p.\n", device, type, idx, buffer);
2447 return wined3d_device_context_set_constant_buffer(&device->cs->c, type, idx, buffer);
2450 struct wined3d_buffer * CDECL wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2451 enum wined3d_shader_type shader_type, unsigned int idx)
2453 TRACE("device %p, shader_type %#x, idx %u.\n", device, shader_type, idx);
2455 if (idx >= MAX_CONSTANT_BUFFERS)
2457 WARN("Invalid constant buffer index %u.\n", idx);
2458 return NULL;
2461 return device->cs->c.state->cb[shader_type][idx];
2464 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2465 UINT idx, struct wined3d_shader_resource_view *view)
2467 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2469 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2472 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2473 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2475 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2477 WARN("Invalid view index %u.\n", idx);
2478 return NULL;
2481 return device->cs->c.state->shader_resource_view[shader_type][idx];
2484 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2485 UINT idx)
2487 TRACE("device %p, idx %u.\n", device, idx);
2489 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2492 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2494 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2496 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2499 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2500 enum wined3d_shader_type shader_type, unsigned int idx)
2502 if (idx >= MAX_SAMPLER_OBJECTS)
2504 WARN("Invalid sampler index %u.\n", idx);
2505 return NULL;
2508 return device->cs->c.state->sampler[shader_type][idx];
2511 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2513 TRACE("device %p, idx %u.\n", device, idx);
2515 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2518 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2519 unsigned int start_idx, unsigned int count, const BOOL *constants)
2521 unsigned int i;
2523 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2524 device, start_idx, count, constants);
2526 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2527 if (TRACE_ON(d3d))
2529 for (i = 0; i < count; ++i)
2530 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2533 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2536 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2537 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2539 unsigned int i;
2541 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2542 device, start_idx, count, constants);
2544 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2545 if (TRACE_ON(d3d))
2547 for (i = 0; i < count; ++i)
2548 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2551 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2554 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2555 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2557 unsigned int i;
2559 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2560 device, start_idx, count, constants);
2562 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2563 if (TRACE_ON(d3d))
2565 for (i = 0; i < count; ++i)
2566 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2569 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2572 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2574 TRACE("device %p, shader %p.\n", device, shader);
2576 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, shader);
2579 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2581 TRACE("device %p.\n", device);
2583 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_PIXEL];
2586 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2587 UINT idx, struct wined3d_shader_resource_view *view)
2589 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2591 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2594 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2595 UINT idx)
2597 TRACE("device %p, idx %u.\n", device, idx);
2599 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2602 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2604 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2606 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2609 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2611 TRACE("device %p, idx %u.\n", device, idx);
2613 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2616 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2617 unsigned int start_idx, unsigned int count, const BOOL *constants)
2619 unsigned int i;
2621 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2622 device, start_idx, count, constants);
2624 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2625 if (TRACE_ON(d3d))
2627 for (i = 0; i < count; ++i)
2628 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2631 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2634 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2635 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2637 unsigned int i;
2639 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2640 device, start_idx, count, constants);
2642 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2643 if (TRACE_ON(d3d))
2645 for (i = 0; i < count; ++i)
2646 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2649 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2652 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2653 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2655 unsigned int i;
2657 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2658 device, start_idx, count, constants);
2660 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2661 if (TRACE_ON(d3d))
2663 for (i = 0; i < count; ++i)
2664 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2667 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2670 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2672 TRACE("device %p, shader %p.\n", device, shader);
2674 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_HULL, shader);
2677 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2679 TRACE("device %p.\n", device);
2681 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_HULL];
2684 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2685 unsigned int idx, struct wined3d_shader_resource_view *view)
2687 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2689 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_HULL, idx, view);
2692 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2693 unsigned int idx)
2695 TRACE("device %p, idx %u.\n", device, idx);
2697 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2700 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2701 unsigned int idx, struct wined3d_sampler *sampler)
2703 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2705 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2708 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2710 TRACE("device %p, idx %u.\n", device, idx);
2712 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2715 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2717 TRACE("device %p, shader %p.\n", device, shader);
2719 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, shader);
2722 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2724 TRACE("device %p.\n", device);
2726 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2729 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2730 unsigned int idx, struct wined3d_shader_resource_view *view)
2732 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2734 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2737 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2738 unsigned int idx)
2740 TRACE("device %p, idx %u.\n", device, idx);
2742 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2745 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2746 unsigned int idx, struct wined3d_sampler *sampler)
2748 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2750 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2753 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2755 TRACE("device %p, idx %u.\n", device, idx);
2757 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2760 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2762 TRACE("device %p, shader %p.\n", device, shader);
2764 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2767 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2769 TRACE("device %p.\n", device);
2771 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2774 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2775 UINT idx, struct wined3d_shader_resource_view *view)
2777 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2779 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2782 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2783 UINT idx)
2785 TRACE("device %p, idx %u.\n", device, idx);
2787 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2790 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2792 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2794 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2797 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2799 TRACE("device %p, idx %u.\n", device, idx);
2801 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2804 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2806 TRACE("device %p, shader %p.\n", device, shader);
2808 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, shader);
2811 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2813 TRACE("device %p.\n", device);
2815 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2818 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2819 unsigned int idx, struct wined3d_shader_resource_view *view)
2821 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2823 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2826 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2827 unsigned int idx)
2829 TRACE("device %p, idx %u.\n", device, idx);
2831 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2834 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2835 unsigned int idx, struct wined3d_sampler *sampler)
2837 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2839 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2842 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2844 TRACE("device %p, idx %u.\n", device, idx);
2846 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2849 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2850 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2852 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2854 WARN("Invalid UAV index %u.\n", idx);
2855 return NULL;
2858 return device->cs->c.state->unordered_access_view[pipeline][idx];
2861 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2862 struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2864 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2866 wined3d_device_context_set_unordered_access_view(&device->cs->c, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
2869 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
2870 unsigned int idx)
2872 TRACE("device %p, idx %u.\n", device, idx);
2874 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
2877 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2878 unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2880 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2882 wined3d_device_context_set_unordered_access_view(&device->cs->c, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
2885 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
2886 const struct wined3d_device *device, unsigned int idx)
2888 TRACE("device %p, idx %u.\n", device, idx);
2890 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
2893 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2895 unsigned int i;
2897 if (!latency)
2898 latency = 3;
2900 device->max_frame_latency = latency;
2901 for (i = 0; i < device->swapchain_count; ++i)
2902 swapchain_set_max_frame_latency(device->swapchains[i], device);
2905 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2907 return device->max_frame_latency;
2910 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2912 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2913 unsigned int i, size = 0;
2915 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2916 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2917 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2918 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2919 switch (fvf & WINED3DFVF_POSITION_MASK)
2921 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2922 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2923 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2924 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2925 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2926 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2927 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2928 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2929 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2931 for (i = 0; i < texcoord_count; ++i)
2933 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2936 return size;
2939 static void wined3d_format_get_colour(const struct wined3d_format *format,
2940 const void *data, struct wined3d_color *colour)
2942 float *output = &colour->r;
2943 const uint32_t *u32_data;
2944 const uint16_t *u16_data;
2945 const float *f32_data;
2946 unsigned int i;
2948 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2949 static unsigned int warned;
2951 switch (format->id)
2953 case WINED3DFMT_B8G8R8A8_UNORM:
2954 u32_data = data;
2955 wined3d_color_from_d3dcolor(colour, *u32_data);
2956 break;
2958 case WINED3DFMT_R8G8B8A8_UNORM:
2959 u32_data = data;
2960 colour->r = (*u32_data & 0xffu) / 255.0f;
2961 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2962 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2963 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2964 break;
2966 case WINED3DFMT_R16G16_UNORM:
2967 case WINED3DFMT_R16G16B16A16_UNORM:
2968 u16_data = data;
2969 *colour = default_colour;
2970 for (i = 0; i < format->component_count; ++i)
2971 output[i] = u16_data[i] / 65535.0f;
2972 break;
2974 case WINED3DFMT_R32_FLOAT:
2975 case WINED3DFMT_R32G32_FLOAT:
2976 case WINED3DFMT_R32G32B32_FLOAT:
2977 case WINED3DFMT_R32G32B32A32_FLOAT:
2978 f32_data = data;
2979 *colour = default_colour;
2980 for (i = 0; i < format->component_count; ++i)
2981 output[i] = f32_data[i];
2982 break;
2984 default:
2985 *colour = default_colour;
2986 if (!warned++)
2987 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2988 break;
2992 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2993 const struct wined3d_color *material_colour, unsigned int index,
2994 const struct wined3d_stream_info *stream_info)
2996 const struct wined3d_stream_info_element *element = NULL;
2998 switch (mcs)
3000 case WINED3D_MCS_MATERIAL:
3001 *colour = *material_colour;
3002 return;
3004 case WINED3D_MCS_COLOR1:
3005 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3007 colour->r = colour->g = colour->b = colour->a = 1.0f;
3008 return;
3010 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3011 break;
3013 case WINED3D_MCS_COLOR2:
3014 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3016 colour->r = colour->g = colour->b = colour->a = 0.0f;
3017 return;
3019 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3020 break;
3022 default:
3023 colour->r = colour->g = colour->b = colour->a = 0.0f;
3024 ERR("Invalid material colour source %#x.\n", mcs);
3025 return;
3028 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
3031 static float wined3d_clamp(float value, float min_value, float max_value)
3033 return value < min_value ? min_value : value > max_value ? max_value : value;
3036 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3038 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
3041 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3043 v0->x -= v1->x;
3044 v0->y -= v1->y;
3045 v0->z -= v1->z;
3048 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
3050 v->x *= s;
3051 v->y *= s;
3052 v->z *= s;
3055 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
3057 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
3059 if (isfinite(rnorm))
3060 wined3d_vec3_scale(v, rnorm);
3063 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
3064 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
3066 struct wined3d_vec3 tmp;
3068 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
3069 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
3070 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
3072 *dst = tmp;
3075 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
3076 float min_value, float max_value)
3078 dst->r = wined3d_clamp(src->r, min_value, max_value);
3079 dst->g = wined3d_clamp(src->g, min_value, max_value);
3080 dst->b = wined3d_clamp(src->b, min_value, max_value);
3081 dst->a = wined3d_clamp(src->a, min_value, max_value);
3084 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
3086 dst->r += src->r * c;
3087 dst->g += src->g * c;
3088 dst->b += src->b * c;
3091 static void init_transformed_lights(struct lights_settings *ls,
3092 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
3094 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
3095 const struct wined3d_light_info *light_info;
3096 struct light_transformed *light;
3097 struct wined3d_vec4 vec4;
3098 unsigned int light_count;
3099 unsigned int i, index;
3101 memset(ls, 0, sizeof(*ls));
3103 ls->lighting = !!compute_lighting;
3104 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
3105 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
3106 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
3107 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
3108 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
3109 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
3111 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
3112 return;
3114 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
3115 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
3117 if (!compute_lighting)
3118 return;
3120 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
3122 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
3123 ls->legacy_lighting = !!legacy_lighting;
3124 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
3125 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
3127 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
3129 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
3131 if (!light_info->enabled)
3132 continue;
3134 switch (light_info->OriginalParms.type)
3136 case WINED3D_LIGHT_DIRECTIONAL:
3137 ++ls->directional_light_count;
3138 break;
3140 case WINED3D_LIGHT_POINT:
3141 ++ls->point_light_count;
3142 break;
3144 case WINED3D_LIGHT_SPOT:
3145 ++ls->spot_light_count;
3146 break;
3148 case WINED3D_LIGHT_PARALLELPOINT:
3149 ++ls->parallel_point_light_count;
3150 break;
3152 default:
3153 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
3154 continue;
3156 lights[index++] = light_info;
3157 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
3158 break;
3162 light_count = index;
3163 for (i = 0, index = 0; i < light_count; ++i)
3165 light_info = lights[i];
3166 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
3167 continue;
3169 light = &ls->lights[index];
3170 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3171 light->direction = *(struct wined3d_vec3 *)&vec4;
3172 wined3d_vec3_normalise(&light->direction);
3174 light->diffuse = light_info->OriginalParms.diffuse;
3175 light->ambient = light_info->OriginalParms.ambient;
3176 light->specular = light_info->OriginalParms.specular;
3177 ++index;
3180 for (i = 0; i < light_count; ++i)
3182 light_info = lights[i];
3183 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
3184 continue;
3186 light = &ls->lights[index];
3188 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3189 light->range = light_info->OriginalParms.range;
3190 light->c_att = light_info->OriginalParms.attenuation0;
3191 light->l_att = light_info->OriginalParms.attenuation1;
3192 light->q_att = light_info->OriginalParms.attenuation2;
3194 light->diffuse = light_info->OriginalParms.diffuse;
3195 light->ambient = light_info->OriginalParms.ambient;
3196 light->specular = light_info->OriginalParms.specular;
3197 ++index;
3200 for (i = 0; i < light_count; ++i)
3202 light_info = lights[i];
3203 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
3204 continue;
3206 light = &ls->lights[index];
3208 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3209 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3210 light->direction = *(struct wined3d_vec3 *)&vec4;
3211 wined3d_vec3_normalise(&light->direction);
3212 light->range = light_info->OriginalParms.range;
3213 light->falloff = light_info->OriginalParms.falloff;
3214 light->c_att = light_info->OriginalParms.attenuation0;
3215 light->l_att = light_info->OriginalParms.attenuation1;
3216 light->q_att = light_info->OriginalParms.attenuation2;
3217 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
3218 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
3220 light->diffuse = light_info->OriginalParms.diffuse;
3221 light->ambient = light_info->OriginalParms.ambient;
3222 light->specular = light_info->OriginalParms.specular;
3223 ++index;
3226 for (i = 0; i < light_count; ++i)
3228 light_info = lights[i];
3229 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
3230 continue;
3232 light = &ls->lights[index];
3234 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3235 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
3236 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
3237 light->diffuse = light_info->OriginalParms.diffuse;
3238 light->ambient = light_info->OriginalParms.ambient;
3239 light->specular = light_info->OriginalParms.specular;
3240 ++index;
3244 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
3245 const struct wined3d_vec3 *dir, float att, float material_shininess,
3246 const struct wined3d_vec3 *normal_transformed,
3247 const struct wined3d_vec3 *position_transformed_normalised,
3248 const struct light_transformed *light, const struct lights_settings *ls)
3250 struct wined3d_vec3 vec3;
3251 float t, c;
3253 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
3254 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
3256 vec3 = *dir;
3257 if (ls->localviewer)
3258 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
3259 else
3260 vec3.z -= 1.0f;
3261 wined3d_vec3_normalise(&vec3);
3262 t = wined3d_vec3_dot(normal_transformed, &vec3);
3263 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
3264 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
3265 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
3268 static void light_set_vertex_data(struct lights_settings *ls,
3269 const struct wined3d_vec4 *position)
3271 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3272 return;
3274 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3275 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3278 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3279 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3280 float material_shininess)
3282 struct wined3d_vec3 position_transformed_normalised;
3283 struct wined3d_vec3 normal_transformed = {0.0f};
3284 const struct light_transformed *light;
3285 struct wined3d_vec3 dir, dst;
3286 unsigned int i, index;
3287 float att;
3289 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3290 wined3d_vec3_normalise(&position_transformed_normalised);
3292 if (normal)
3294 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3295 if (ls->normalise)
3296 wined3d_vec3_normalise(&normal_transformed);
3299 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3300 *specular = *diffuse;
3301 *ambient = ls->ambient_light;
3303 index = 0;
3304 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3306 light = &ls->lights[index];
3308 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3309 if (normal)
3310 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3311 &normal_transformed, &position_transformed_normalised, light, ls);
3314 for (i = 0; i < ls->point_light_count; ++i, ++index)
3316 light = &ls->lights[index];
3317 dir.x = light->position.x - ls->position_transformed.x;
3318 dir.y = light->position.y - ls->position_transformed.y;
3319 dir.z = light->position.z - ls->position_transformed.z;
3321 dst.z = wined3d_vec3_dot(&dir, &dir);
3322 dst.y = sqrtf(dst.z);
3323 dst.x = 1.0f;
3324 if (ls->legacy_lighting)
3326 dst.y = (light->range - dst.y) / light->range;
3327 if (!(dst.y > 0.0f))
3328 continue;
3329 dst.z = dst.y * dst.y;
3331 else
3333 if (!(dst.y <= light->range))
3334 continue;
3336 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3337 if (!ls->legacy_lighting)
3338 att = 1.0f / att;
3340 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3341 if (normal)
3343 wined3d_vec3_normalise(&dir);
3344 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3345 &normal_transformed, &position_transformed_normalised, light, ls);
3349 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3351 float t;
3353 light = &ls->lights[index];
3355 dir.x = light->position.x - ls->position_transformed.x;
3356 dir.y = light->position.y - ls->position_transformed.y;
3357 dir.z = light->position.z - ls->position_transformed.z;
3359 dst.z = wined3d_vec3_dot(&dir, &dir);
3360 dst.y = sqrtf(dst.z);
3361 dst.x = 1.0f;
3363 if (ls->legacy_lighting)
3365 dst.y = (light->range - dst.y) / light->range;
3366 if (!(dst.y > 0.0f))
3367 continue;
3368 dst.z = dst.y * dst.y;
3370 else
3372 if (!(dst.y <= light->range))
3373 continue;
3375 wined3d_vec3_normalise(&dir);
3376 t = -wined3d_vec3_dot(&dir, &light->direction);
3377 if (t > light->cos_htheta)
3378 att = 1.0f;
3379 else if (t <= light->cos_hphi)
3380 att = 0.0f;
3381 else
3382 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3384 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3385 if (ls->legacy_lighting)
3386 att *= t;
3387 else
3388 att /= t;
3390 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3392 if (normal)
3393 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3394 &normal_transformed, &position_transformed_normalised, light, ls);
3397 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3399 light = &ls->lights[index];
3401 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3402 if (normal)
3403 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3404 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3408 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3410 switch (ls->fog_mode)
3412 case WINED3D_FOG_NONE:
3413 return fog_coord;
3414 case WINED3D_FOG_LINEAR:
3415 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3416 case WINED3D_FOG_EXP:
3417 return expf(-fog_coord * ls->fog_density);
3418 case WINED3D_FOG_EXP2:
3419 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3420 default:
3421 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3422 return 0.0f;
3426 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3428 float fog_coord;
3430 if (ls->fog_mode == WINED3D_FOG_NONE)
3431 return;
3433 switch (ls->fog_coord_mode)
3435 case WINED3D_FFP_VS_FOG_RANGE:
3436 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3437 (const struct wined3d_vec3 *)&ls->position_transformed));
3438 break;
3440 case WINED3D_FFP_VS_FOG_DEPTH:
3441 fog_coord = fabsf(ls->position_transformed.z);
3442 break;
3444 default:
3445 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3446 return;
3448 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3451 /* Context activation is done by the caller. */
3452 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3453 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3454 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3456 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3457 const struct wined3d_color *material_specular_state_colour;
3458 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3459 const struct wined3d_state *state = device->cs->c.state;
3460 const struct wined3d_format *output_colour_format;
3461 static const struct wined3d_color black;
3462 struct wined3d_map_desc map_desc;
3463 struct wined3d_box box = {0};
3464 struct wined3d_viewport vp;
3465 unsigned int texture_count;
3466 struct lights_settings ls;
3467 unsigned int vertex_size;
3468 BOOL do_clip, lighting;
3469 float min_z, max_z;
3470 unsigned int i;
3471 BYTE *dest_ptr;
3472 HRESULT hr;
3474 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3476 ERR("Source has no position mask.\n");
3477 return WINED3DERR_INVALIDCALL;
3480 if (state->render_states[WINED3D_RS_CLIPPING])
3482 static BOOL warned = FALSE;
3484 * The clipping code is not quite correct. Some things need
3485 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3486 * so disable clipping for now.
3487 * (The graphics in Half-Life are broken, and my processvertices
3488 * test crashes with IDirect3DDevice3)
3489 do_clip = TRUE;
3491 do_clip = FALSE;
3492 if (!warned)
3494 warned = TRUE;
3495 FIXME("Clipping is broken and disabled for now\n");
3498 else
3499 do_clip = FALSE;
3501 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3502 box.left = dwDestIndex * vertex_size;
3503 box.right = box.left + dwCount * vertex_size;
3504 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3506 WARN("Failed to map buffer, hr %#x.\n", hr);
3507 return hr;
3509 dest_ptr = map_desc.data;
3511 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3512 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3513 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3515 TRACE("View mat:\n");
3516 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3517 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3518 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3519 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3521 TRACE("Proj mat:\n");
3522 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3523 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3524 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3525 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3527 TRACE("World mat:\n");
3528 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3529 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3530 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3531 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3533 /* Get the viewport */
3534 wined3d_device_get_viewports(device, NULL, &vp);
3535 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3536 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3538 multiply_matrix(&mat,&view_mat,&world_mat);
3539 multiply_matrix(&mat,&proj_mat,&mat);
3541 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3543 lighting = state->render_states[WINED3D_RS_LIGHTING]
3544 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3545 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3546 &ambient_source, &specular_source, state, stream_info);
3547 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3548 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3549 ? &state->material.specular : &black;
3550 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3551 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3553 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3555 for (i = 0; i < dwCount; ++i)
3557 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3558 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3559 struct wined3d_color ambient, diffuse, specular;
3560 struct wined3d_vec4 position;
3561 unsigned int tex_index;
3563 position.x = p[0];
3564 position.y = p[1];
3565 position.z = p[2];
3566 position.w = 1.0f;
3568 light_set_vertex_data(&ls, &position);
3570 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3571 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3572 /* The position first */
3573 float x, y, z, rhw;
3574 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3576 /* Multiplication with world, view and projection matrix. */
3577 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3578 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3579 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3580 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3582 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3584 /* WARNING: The following things are taken from d3d7 and were not yet checked
3585 * against d3d8 or d3d9!
3588 /* Clipping conditions: From msdn
3590 * A vertex is clipped if it does not match the following requirements
3591 * -rhw < x <= rhw
3592 * -rhw < y <= rhw
3593 * 0 < z <= rhw
3594 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3596 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3597 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3601 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3602 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3604 /* "Normal" viewport transformation (not clipped)
3605 * 1) The values are divided by rhw
3606 * 2) The y axis is negative, so multiply it with -1
3607 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3608 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3609 * 4) Multiply x with Width/2 and add Width/2
3610 * 5) The same for the height
3611 * 6) Add the viewpoint X and Y to the 2D coordinates and
3612 * The minimum Z value to z
3613 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3615 * Well, basically it's simply a linear transformation into viewport
3616 * coordinates
3619 x /= rhw;
3620 y /= rhw;
3621 z /= rhw;
3623 y *= -1;
3625 x *= vp.width / 2;
3626 y *= vp.height / 2;
3627 z *= max_z - min_z;
3629 x += vp.width / 2 + vp.x;
3630 y += vp.height / 2 + vp.y;
3631 z += min_z;
3633 rhw = 1 / rhw;
3634 } else {
3635 /* That vertex got clipped
3636 * Contrary to OpenGL it is not dropped completely, it just
3637 * undergoes a different calculation.
3639 TRACE("Vertex got clipped\n");
3640 x += rhw;
3641 y += rhw;
3643 x /= 2;
3644 y /= 2;
3646 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3647 * outside of the main vertex buffer memory. That needs some more
3648 * investigation...
3652 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3655 ( (float *) dest_ptr)[0] = x;
3656 ( (float *) dest_ptr)[1] = y;
3657 ( (float *) dest_ptr)[2] = z;
3658 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3660 dest_ptr += 3 * sizeof(float);
3662 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3663 dest_ptr += sizeof(float);
3666 if (dst_fvf & WINED3DFVF_PSIZE)
3667 dest_ptr += sizeof(DWORD);
3669 if (dst_fvf & WINED3DFVF_NORMAL)
3671 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3672 const float *normal = (const float *)(element->data.addr + i * element->stride);
3673 /* AFAIK this should go into the lighting information */
3674 FIXME("Didn't expect the destination to have a normal\n");
3675 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3678 if (lighting)
3680 const struct wined3d_stream_info_element *element;
3681 struct wined3d_vec3 *normal;
3683 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3685 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3686 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3688 else
3690 normal = NULL;
3692 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3693 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3696 if (dst_fvf & WINED3DFVF_DIFFUSE)
3698 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3700 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3701 &state->material.diffuse, i, stream_info);
3703 if (lighting)
3705 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3706 &state->material.ambient, i, stream_info);
3707 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3708 &state->material.emissive, i, stream_info);
3710 diffuse_colour.r = ambient.r * material_ambient.r
3711 + diffuse.r * material_diffuse.r + material_emissive.r;
3712 diffuse_colour.g = ambient.g * material_ambient.g
3713 + diffuse.g * material_diffuse.g + material_emissive.g;
3714 diffuse_colour.b = ambient.b * material_ambient.b
3715 + diffuse.b * material_diffuse.b + material_emissive.b;
3716 diffuse_colour.a = material_diffuse.a;
3718 else
3720 diffuse_colour = material_diffuse;
3722 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3723 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3724 dest_ptr += sizeof(DWORD);
3727 if (dst_fvf & WINED3DFVF_SPECULAR)
3729 struct wined3d_color material_specular, specular_colour;
3731 wined3d_colour_from_mcs(&material_specular, specular_source,
3732 material_specular_state_colour, i, stream_info);
3734 if (lighting)
3736 specular_colour.r = specular.r * material_specular.r;
3737 specular_colour.g = specular.g * material_specular.g;
3738 specular_colour.b = specular.b * material_specular.b;
3739 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3741 else
3743 specular_colour = material_specular;
3745 update_fog_factor(&specular_colour.a, &ls);
3746 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3747 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3748 dest_ptr += sizeof(DWORD);
3751 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3753 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3754 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3755 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3757 ERR("No source texture, but destination requests one\n");
3758 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3760 else
3762 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3767 wined3d_resource_unmap(&dest->resource, 0);
3769 return WINED3D_OK;
3771 #undef copy_and_next
3773 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3774 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3775 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3777 struct wined3d_state *state = device->cs->c.state;
3778 struct wined3d_stream_info stream_info;
3779 struct wined3d_resource *resource;
3780 struct wined3d_box box = {0};
3781 struct wined3d_shader *vs;
3782 unsigned int i, j;
3783 HRESULT hr;
3784 WORD map;
3786 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3787 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3788 device, src_start_idx, dst_idx, vertex_count,
3789 dst_buffer, declaration, flags, dst_fvf);
3791 if (declaration)
3792 FIXME("Output vertex declaration not implemented yet.\n");
3794 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3795 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3796 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3797 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3799 /* We can't convert FROM a VBO, and vertex buffers used to source into
3800 * process_vertices() are unlikely to ever be used for drawing. Release
3801 * VBOs in those buffers and fix up the stream_info structure.
3803 * Also apply the start index. */
3804 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3806 struct wined3d_stream_info_element *e;
3807 struct wined3d_map_desc map_desc;
3809 if (!(map & 1))
3810 continue;
3812 e = &stream_info.elements[i];
3813 resource = &state->streams[e->stream_idx].buffer->resource;
3814 box.left = src_start_idx * e->stride;
3815 box.right = box.left + vertex_count * e->stride;
3816 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3818 ERR("Failed to map resource.\n");
3819 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3821 if (!(map & 1))
3822 continue;
3824 e = &stream_info.elements[j];
3825 resource = &state->streams[e->stream_idx].buffer->resource;
3826 if (FAILED(wined3d_resource_unmap(resource, 0)))
3827 ERR("Failed to unmap resource.\n");
3829 return WINED3DERR_INVALIDCALL;
3831 e->data.buffer_object = 0;
3832 e->data.addr += (ULONG_PTR)map_desc.data;
3835 hr = process_vertices_strided(device, dst_idx, vertex_count,
3836 &stream_info, dst_buffer, flags, dst_fvf);
3838 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3840 if (!(map & 1))
3841 continue;
3843 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3844 if (FAILED(wined3d_resource_unmap(resource, 0)))
3845 ERR("Failed to unmap resource.\n");
3848 return hr;
3851 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3852 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3854 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3856 TRACE("device %p, stage %u, state %s, value %#x.\n",
3857 device, stage, debug_d3dtexturestate(state), value);
3859 if (stage >= d3d_info->limits.ffp_blend_stages)
3861 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3862 stage, d3d_info->limits.ffp_blend_stages - 1);
3863 return;
3866 if (value == device->cs->c.state->texture_states[stage][state])
3868 TRACE("Application is setting the old value over, nothing to do.\n");
3869 return;
3872 device->cs->c.state->texture_states[stage][state] = value;
3874 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3877 static void wined3d_device_set_texture(struct wined3d_device *device,
3878 UINT stage, struct wined3d_texture *texture)
3880 struct wined3d_state *state = device->cs->c.state;
3881 struct wined3d_texture *prev;
3883 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3885 /* Windows accepts overflowing this array... we do not. */
3886 if (stage >= ARRAY_SIZE(state->textures))
3888 WARN("Ignoring invalid stage %u.\n", stage);
3889 return;
3892 prev = state->textures[stage];
3893 TRACE("Previous texture %p.\n", prev);
3895 if (texture == prev)
3897 TRACE("App is setting the same texture again, nothing to do.\n");
3898 return;
3901 TRACE("Setting new texture to %p.\n", texture);
3902 state->textures[stage] = texture;
3904 if (texture)
3905 wined3d_texture_incref(texture);
3906 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3907 if (prev)
3908 wined3d_texture_decref(prev);
3910 return;
3913 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3914 struct wined3d_stateblock *stateblock)
3916 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3917 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3918 const struct wined3d_saved_states *changed = &stateblock->changed;
3919 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3920 unsigned int i, j, start, idx;
3921 struct wined3d_range range;
3922 uint32_t map;
3924 TRACE("device %p, stateblock %p.\n", device, stateblock);
3926 if (changed->vertexShader)
3927 wined3d_device_set_vertex_shader(device, state->vs);
3928 if (changed->pixelShader)
3929 wined3d_device_set_pixel_shader(device, state->ps);
3931 for (start = 0; ; start = range.offset + range.size)
3933 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3934 break;
3936 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3939 map = changed->vertexShaderConstantsI;
3940 for (start = 0; ; start = range.offset + range.size)
3942 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3943 break;
3945 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3948 map = changed->vertexShaderConstantsB;
3949 for (start = 0; ; start = range.offset + range.size)
3951 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3952 break;
3954 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3957 for (start = 0; ; start = range.offset + range.size)
3959 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3960 break;
3962 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3965 map = changed->pixelShaderConstantsI;
3966 for (start = 0; ; start = range.offset + range.size)
3968 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3969 break;
3971 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3974 map = changed->pixelShaderConstantsB;
3975 for (start = 0; ; start = range.offset + range.size)
3977 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3978 break;
3980 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3983 if (changed->lights)
3985 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3987 const struct wined3d_light_info *light;
3989 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3991 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
3992 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3997 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3999 map = changed->renderState[i];
4000 while (map)
4002 j = wined3d_bit_scan(&map);
4003 idx = i * word_bit_count + j;
4005 switch (idx)
4007 case WINED3D_RS_BLENDFACTOR:
4008 case WINED3D_RS_MULTISAMPLEMASK:
4009 case WINED3D_RS_ALPHABLENDENABLE:
4010 case WINED3D_RS_SRCBLEND:
4011 case WINED3D_RS_DESTBLEND:
4012 case WINED3D_RS_BLENDOP:
4013 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
4014 case WINED3D_RS_SRCBLENDALPHA:
4015 case WINED3D_RS_DESTBLENDALPHA:
4016 case WINED3D_RS_BLENDOPALPHA:
4017 case WINED3D_RS_COLORWRITEENABLE:
4018 case WINED3D_RS_COLORWRITEENABLE1:
4019 case WINED3D_RS_COLORWRITEENABLE2:
4020 case WINED3D_RS_COLORWRITEENABLE3:
4021 set_blend_state = TRUE;
4022 break;
4024 case WINED3D_RS_BACK_STENCILFAIL:
4025 case WINED3D_RS_BACK_STENCILFUNC:
4026 case WINED3D_RS_BACK_STENCILPASS:
4027 case WINED3D_RS_BACK_STENCILZFAIL:
4028 case WINED3D_RS_STENCILENABLE:
4029 case WINED3D_RS_STENCILFAIL:
4030 case WINED3D_RS_STENCILFUNC:
4031 case WINED3D_RS_STENCILREF:
4032 case WINED3D_RS_STENCILMASK:
4033 case WINED3D_RS_STENCILPASS:
4034 case WINED3D_RS_STENCILWRITEMASK:
4035 case WINED3D_RS_STENCILZFAIL:
4036 case WINED3D_RS_TWOSIDEDSTENCILMODE:
4037 case WINED3D_RS_ZENABLE:
4038 case WINED3D_RS_ZFUNC:
4039 case WINED3D_RS_ZWRITEENABLE:
4040 set_depth_stencil_state = TRUE;
4041 break;
4043 case WINED3D_RS_FILLMODE:
4044 case WINED3D_RS_CULLMODE:
4045 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
4046 case WINED3D_RS_DEPTHBIAS:
4047 case WINED3D_RS_SCISSORTESTENABLE:
4048 case WINED3D_RS_ANTIALIASEDLINEENABLE:
4049 set_rasterizer_state = TRUE;
4050 break;
4052 default:
4053 wined3d_device_set_render_state(device, idx, state->rs[idx]);
4054 break;
4059 if (set_rasterizer_state)
4061 struct wined3d_rasterizer_state *rasterizer_state;
4062 struct wined3d_rasterizer_state_desc desc;
4063 struct wine_rb_entry *entry;
4064 union
4066 DWORD d;
4067 float f;
4068 } bias;
4070 memset(&desc, 0, sizeof(desc));
4071 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
4072 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
4073 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
4074 desc.depth_bias = bias.f;
4075 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
4076 desc.scale_bias = bias.f;
4077 desc.depth_clip = TRUE;
4078 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
4079 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
4081 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
4083 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
4084 wined3d_device_set_rasterizer_state(device, rasterizer_state);
4086 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
4087 &wined3d_null_parent_ops, &rasterizer_state)))
4089 wined3d_device_set_rasterizer_state(device, rasterizer_state);
4090 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
4092 ERR("Failed to insert rasterizer state.\n");
4093 wined3d_rasterizer_state_decref(rasterizer_state);
4098 if (set_blend_state || changed->alpha_to_coverage
4099 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
4101 struct wined3d_blend_state *blend_state;
4102 struct wined3d_blend_state_desc desc;
4103 struct wine_rb_entry *entry;
4104 struct wined3d_color colour;
4105 unsigned int sample_mask;
4107 memset(&desc, 0, sizeof(desc));
4108 desc.alpha_to_coverage = state->alpha_to_coverage;
4109 desc.independent = FALSE;
4110 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
4111 desc.alpha_to_coverage = TRUE;
4112 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
4113 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
4114 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
4115 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
4116 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
4118 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
4119 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
4120 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
4122 else
4124 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
4125 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
4126 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
4128 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
4129 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
4130 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
4131 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
4132 if (desc.rt[1].writemask != desc.rt[0].writemask
4133 || desc.rt[2].writemask != desc.rt[0].writemask
4134 || desc.rt[3].writemask != desc.rt[0].writemask)
4136 desc.independent = TRUE;
4137 for (i = 1; i < 4; ++i)
4139 desc.rt[i].enable = desc.rt[0].enable;
4140 desc.rt[i].src = desc.rt[0].src;
4141 desc.rt[i].dst = desc.rt[0].dst;
4142 desc.rt[i].op = desc.rt[0].op;
4143 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
4144 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
4145 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
4149 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
4150 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
4151 else
4152 wined3d_device_get_blend_state(device, &colour, &sample_mask);
4154 if ((entry = wine_rb_get(&device->blend_states, &desc)))
4156 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
4157 wined3d_device_set_blend_state(device, blend_state, &colour, state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4159 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
4160 &wined3d_null_parent_ops, &blend_state)))
4162 wined3d_device_set_blend_state(device, blend_state, &colour, state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4163 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
4165 ERR("Failed to insert blend state.\n");
4166 wined3d_blend_state_decref(blend_state);
4171 if (set_depth_stencil_state)
4173 struct wined3d_depth_stencil_state *depth_stencil_state;
4174 struct wined3d_depth_stencil_state_desc desc;
4175 struct wine_rb_entry *entry;
4176 unsigned int stencil_ref;
4178 memset(&desc, 0, sizeof(desc));
4179 switch (state->rs[WINED3D_RS_ZENABLE])
4181 case WINED3D_ZB_FALSE:
4182 desc.depth = FALSE;
4183 break;
4185 case WINED3D_ZB_USEW:
4186 FIXME("W buffer is not well handled.\n");
4187 case WINED3D_ZB_TRUE:
4188 desc.depth = TRUE;
4189 break;
4191 default:
4192 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
4194 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
4195 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
4196 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
4197 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
4198 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
4199 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
4200 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
4201 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
4202 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
4204 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
4206 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
4207 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
4208 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
4209 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
4211 else
4213 desc.back = desc.front;
4216 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
4217 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
4218 else
4219 wined3d_device_get_depth_stencil_state(device, &stencil_ref);
4221 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
4223 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
4224 wined3d_device_set_depth_stencil_state(device, depth_stencil_state, stencil_ref);
4226 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
4227 &wined3d_null_parent_ops, &depth_stencil_state)))
4229 wined3d_device_set_depth_stencil_state(device, depth_stencil_state, stencil_ref);
4230 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
4232 ERR("Failed to insert depth/stencil state.\n");
4233 wined3d_depth_stencil_state_decref(depth_stencil_state);
4238 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
4240 map = changed->textureState[i];
4241 while (map)
4243 j = wined3d_bit_scan(&map);
4244 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
4248 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
4250 map = changed->samplerState[i];
4251 while (map)
4253 j = wined3d_bit_scan(&map);
4254 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
4258 if (changed->transforms)
4260 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
4262 map = changed->transform[i];
4263 while (map)
4265 j = wined3d_bit_scan(&map);
4266 idx = i * word_bit_count + j;
4267 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
4272 if (changed->indices)
4273 wined3d_device_set_index_buffer(device, state->index_buffer, state->index_format, 0);
4274 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
4275 if (changed->vertexDecl)
4276 wined3d_device_set_vertex_declaration(device, state->vertex_declaration);
4277 if (changed->material)
4278 wined3d_device_set_material(device, &state->material);
4279 if (changed->viewport)
4280 wined3d_device_set_viewports(device, 1, &state->viewport);
4281 if (changed->scissorRect)
4282 wined3d_device_set_scissor_rects(device, 1, &state->scissor_rect);
4284 map = changed->streamSource;
4285 while (map)
4287 i = wined3d_bit_scan(&map);
4288 wined3d_device_set_stream_source(device, i, state->streams[i].buffer,
4289 state->streams[i].offset, state->streams[i].stride);
4291 map = changed->streamFreq;
4292 while (map)
4294 i = wined3d_bit_scan(&map);
4295 wined3d_device_set_stream_source_freq(device, i,
4296 state->streams[i].frequency | state->streams[i].flags);
4299 map = changed->textures;
4300 while (map)
4302 i = wined3d_bit_scan(&map);
4303 wined3d_device_set_texture(device, i, state->textures[i]);
4306 map = changed->clipplane;
4307 while (map)
4309 i = wined3d_bit_scan(&map);
4310 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4313 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4315 TRACE("Applied stateblock %p.\n", stateblock);
4318 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4320 TRACE("device %p, caps %p.\n", device, caps);
4322 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4325 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4326 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4328 struct wined3d_swapchain *swapchain;
4330 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4331 device, swapchain_idx, mode, rotation);
4333 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4334 return WINED3DERR_INVALIDCALL;
4336 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4339 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4341 /* At the moment we have no need for any functionality at the beginning
4342 * of a scene. */
4343 TRACE("device %p.\n", device);
4345 if (device->inScene)
4347 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4348 return WINED3DERR_INVALIDCALL;
4350 device->inScene = TRUE;
4351 return WINED3D_OK;
4354 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4356 TRACE("device %p.\n", device);
4358 if (!device->inScene)
4360 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4361 return WINED3DERR_INVALIDCALL;
4364 device->inScene = FALSE;
4365 return WINED3D_OK;
4368 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4369 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4371 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4373 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4374 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4376 if (!rect_count && rects)
4378 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4379 return WINED3D_OK;
4382 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4384 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4385 if (!ds)
4387 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4388 /* TODO: What about depth stencil buffers without stencil bits? */
4389 return WINED3DERR_INVALIDCALL;
4391 else if (flags & WINED3DCLEAR_TARGET)
4393 if (ds->width < fb->render_targets[0]->width
4394 || ds->height < fb->render_targets[0]->height)
4396 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4397 return WINED3D_OK;
4402 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4404 return WINED3D_OK;
4407 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
4408 struct wined3d_query *predicate, BOOL value)
4410 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
4412 wined3d_device_context_set_predication(&device->cs->c, predicate, value);
4415 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
4417 struct wined3d_state *state = device->cs->c.state;
4419 TRACE("device %p, value %p.\n", device, value);
4421 if (value)
4422 *value = state->predicate_value;
4423 return state->predicate;
4426 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
4427 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
4429 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
4430 device, group_count_x, group_count_y, group_count_z);
4432 wined3d_cs_emit_dispatch(device->cs, group_count_x, group_count_y, group_count_z);
4435 void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
4436 struct wined3d_buffer *buffer, unsigned int offset)
4438 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4440 wined3d_cs_emit_dispatch_indirect(device->cs, buffer, offset);
4443 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
4444 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4446 struct wined3d_state *state = device->cs->c.state;
4448 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
4449 device, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4451 state->primitive_type = primitive_type;
4452 state->patch_vertex_count = patch_vertex_count;
4455 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
4456 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4458 const struct wined3d_state *state = device->cs->c.state;
4460 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
4461 device, primitive_type, patch_vertex_count);
4463 *primitive_type = state->primitive_type;
4464 if (patch_vertex_count)
4465 *patch_vertex_count = state->patch_vertex_count;
4467 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4470 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4472 struct wined3d_state *state = device->cs->c.state;
4474 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4476 wined3d_cs_emit_draw(device->cs, state->primitive_type,
4477 state->patch_vertex_count, 0, start_vertex, vertex_count, 0, 0, false);
4479 return WINED3D_OK;
4482 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
4483 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
4485 struct wined3d_state *state = device->cs->c.state;
4487 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
4488 device, start_vertex, vertex_count, start_instance, instance_count);
4490 wined3d_cs_emit_draw(device->cs, state->primitive_type, state->patch_vertex_count,
4491 0, start_vertex, vertex_count, start_instance, instance_count, false);
4494 void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
4495 struct wined3d_buffer *buffer, unsigned int offset)
4497 struct wined3d_state *state = device->cs->c.state;
4499 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4501 wined3d_cs_emit_draw_indirect(device->cs, state->primitive_type,
4502 state->patch_vertex_count, buffer, offset, false);
4505 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4507 struct wined3d_state *state = device->cs->c.state;
4509 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4511 if (!state->index_buffer)
4513 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4514 * without an index buffer set. (The first time at least...)
4515 * D3D8 simply dies, but I doubt it can do much harm to return
4516 * D3DERR_INVALIDCALL there as well. */
4517 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4518 return WINED3DERR_INVALIDCALL;
4521 wined3d_cs_emit_draw(device->cs, state->primitive_type, state->patch_vertex_count,
4522 state->base_vertex_index, start_idx, index_count, 0, 0, true);
4524 return WINED3D_OK;
4527 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4528 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4530 struct wined3d_state *state = device->cs->c.state;
4532 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4533 device, start_idx, index_count, start_instance, instance_count);
4535 wined3d_cs_emit_draw(device->cs, state->primitive_type, state->patch_vertex_count,
4536 state->base_vertex_index, start_idx, index_count, start_instance, instance_count, true);
4539 void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
4540 struct wined3d_buffer *buffer, unsigned int offset)
4542 struct wined3d_state *state = device->cs->c.state;
4544 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4546 wined3d_cs_emit_draw_indirect(device->cs, state->primitive_type,
4547 state->patch_vertex_count, buffer, offset, true);
4550 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4551 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4553 unsigned int src_size, dst_size, src_skip_levels = 0;
4554 unsigned int src_level_count, dst_level_count;
4555 const struct wined3d_dirty_regions *regions;
4556 unsigned int layer_count, level_count, i, j;
4557 enum wined3d_resource_type type;
4558 BOOL entire_texture = TRUE;
4559 struct wined3d_box box;
4561 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4563 /* Verify that the source and destination textures are non-NULL. */
4564 if (!src_texture || !dst_texture)
4566 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4567 return WINED3DERR_INVALIDCALL;
4570 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4571 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4573 WARN("Source resource is GPU accessible or a scratch resource.\n");
4574 return WINED3DERR_INVALIDCALL;
4576 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4578 WARN("Destination resource is CPU accessible.\n");
4579 return WINED3DERR_INVALIDCALL;
4582 /* Verify that the source and destination textures are the same type. */
4583 type = src_texture->resource.type;
4584 if (dst_texture->resource.type != type)
4586 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4587 return WINED3DERR_INVALIDCALL;
4590 layer_count = src_texture->layer_count;
4591 if (layer_count != dst_texture->layer_count)
4593 WARN("Source and destination have different layer counts.\n");
4594 return WINED3DERR_INVALIDCALL;
4597 if (src_texture->resource.format != dst_texture->resource.format)
4599 WARN("Source and destination formats do not match.\n");
4600 return WINED3DERR_INVALIDCALL;
4603 src_level_count = src_texture->level_count;
4604 dst_level_count = dst_texture->level_count;
4605 level_count = min(src_level_count, dst_level_count);
4607 src_size = max(src_texture->resource.width, src_texture->resource.height);
4608 src_size = max(src_size, src_texture->resource.depth);
4609 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4610 dst_size = max(dst_size, dst_texture->resource.depth);
4611 while (src_size > dst_size)
4613 src_size >>= 1;
4614 ++src_skip_levels;
4617 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4618 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4619 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4621 WARN("Source and destination dimensions do not match.\n");
4622 return WINED3DERR_INVALIDCALL;
4625 if ((regions = src_texture->dirty_regions))
4627 for (i = 0; i < layer_count && entire_texture; ++i)
4629 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4630 continue;
4632 entire_texture = FALSE;
4633 break;
4637 /* Update every surface level of the texture. */
4638 if (entire_texture)
4640 for (i = 0; i < level_count; ++i)
4642 wined3d_texture_get_level_box(dst_texture, i, &box);
4643 for (j = 0; j < layer_count; ++j)
4645 wined3d_cs_emit_blt_sub_resource(device->cs,
4646 &dst_texture->resource, j * dst_level_count + i, &box,
4647 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4648 0, NULL, WINED3D_TEXF_POINT);
4652 else
4654 unsigned int src_level, box_count, k;
4655 const struct wined3d_box *boxes;
4656 struct wined3d_box b;
4658 for (i = 0; i < layer_count; ++i)
4660 boxes = regions[i].boxes;
4661 box_count = regions[i].box_count;
4662 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4664 boxes = &b;
4665 box_count = 1;
4666 wined3d_texture_get_level_box(dst_texture, i, &b);
4669 for (j = 0; j < level_count; ++j)
4671 src_level = j + src_skip_levels;
4673 /* TODO: We could pass an array of boxes here to avoid
4674 * multiple context acquisitions for the same resource. */
4675 for (k = 0; k < box_count; ++k)
4677 box = boxes[k];
4678 if (src_level)
4680 box.left >>= src_level;
4681 box.top >>= src_level;
4682 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4683 wined3d_texture_get_level_width(src_texture, src_level));
4684 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4685 wined3d_texture_get_level_height(src_texture, src_level));
4686 box.front >>= src_level;
4687 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4688 wined3d_texture_get_level_depth(src_texture, src_level));
4691 wined3d_cs_emit_blt_sub_resource(device->cs,
4692 &dst_texture->resource, i * dst_level_count + j, &box,
4693 &src_texture->resource, i * src_level_count + src_level, &box,
4694 0, NULL, WINED3D_TEXF_POINT);
4700 wined3d_texture_clear_dirty_regions(src_texture);
4702 return WINED3D_OK;
4705 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4707 const struct wined3d_state *state = device->cs->c.state;
4708 struct wined3d_texture *texture;
4709 DWORD i;
4711 TRACE("device %p, num_passes %p.\n", device, num_passes);
4713 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4715 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4717 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4718 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4720 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4722 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4723 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4726 texture = state->textures[i];
4727 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4729 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4731 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4732 return E_FAIL;
4734 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4736 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4737 return E_FAIL;
4739 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4740 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4742 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4743 return E_FAIL;
4747 if (wined3d_state_uses_depth_buffer(state)
4748 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4750 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4751 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4753 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4755 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4756 return WINED3DERR_CONFLICTINGRENDERSTATE;
4760 /* return a sensible default */
4761 *num_passes = 1;
4763 TRACE("returning D3D_OK\n");
4764 return WINED3D_OK;
4767 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4769 static BOOL warned;
4771 TRACE("device %p, software %#x.\n", device, software);
4773 if (!warned)
4775 FIXME("device %p, software %#x stub!\n", device, software);
4776 warned = TRUE;
4779 device->softwareVertexProcessing = software;
4782 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4784 static BOOL warned;
4786 TRACE("device %p.\n", device);
4788 if (!warned)
4790 TRACE("device %p stub!\n", device);
4791 warned = TRUE;
4794 return device->softwareVertexProcessing;
4797 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4798 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4800 struct wined3d_swapchain *swapchain;
4802 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4803 device, swapchain_idx, raster_status);
4805 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4806 return WINED3DERR_INVALIDCALL;
4808 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4811 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4813 static BOOL warned;
4815 TRACE("device %p, segments %.8e.\n", device, segments);
4817 if (segments != 0.0f)
4819 if (!warned)
4821 FIXME("device %p, segments %.8e stub!\n", device, segments);
4822 warned = TRUE;
4826 return WINED3D_OK;
4829 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4831 static BOOL warned;
4833 TRACE("device %p.\n", device);
4835 if (!warned)
4837 FIXME("device %p stub!\n", device);
4838 warned = TRUE;
4841 return 0.0f;
4844 void CDECL wined3d_device_copy_uav_counter(struct wined3d_device *device,
4845 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4847 TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
4848 device, dst_buffer, offset, uav);
4850 wined3d_cs_emit_copy_uav_counter(device->cs, dst_buffer, offset, uav);
4853 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4854 const struct wined3d_resource *dst_resource)
4856 if (src_resource->format->id == dst_resource->format->id)
4857 return true;
4858 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4859 return true;
4860 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4861 return false;
4862 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4863 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4864 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4865 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4866 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4867 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4868 return false;
4871 void CDECL wined3d_device_copy_resource(struct wined3d_device *device,
4872 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4874 unsigned int src_row_block_count, dst_row_block_count;
4875 struct wined3d_texture *dst_texture, *src_texture;
4876 unsigned int src_row_count, dst_row_count;
4877 struct wined3d_box src_box, dst_box;
4878 unsigned int i, j;
4880 TRACE("device %p, dst_resource %p, src_resource %p.\n", device, dst_resource, src_resource);
4882 if (src_resource == dst_resource)
4884 WARN("Source and destination are the same resource.\n");
4885 return;
4888 if (src_resource->type != dst_resource->type)
4890 WARN("Resource types (%s / %s) don't match.\n",
4891 debug_d3dresourcetype(dst_resource->type),
4892 debug_d3dresourcetype(src_resource->type));
4893 return;
4896 if (!resources_format_compatible(src_resource, dst_resource))
4898 WARN("Resource formats %s and %s are incompatible.\n",
4899 debug_d3dformat(dst_resource->format->id),
4900 debug_d3dformat(src_resource->format->id));
4901 return;
4904 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4905 / src_resource->format->block_width;
4906 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4907 / dst_resource->format->block_width;
4908 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4909 / src_resource->format->block_height;
4910 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4911 / dst_resource->format->block_height;
4913 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4914 || src_resource->depth != dst_resource->depth)
4916 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4917 dst_row_block_count, dst_row_count, dst_resource->depth,
4918 src_row_block_count, src_row_count, src_resource->depth);
4919 return;
4922 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4924 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4925 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, 0, &src_box,
4926 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4927 return;
4930 dst_texture = texture_from_resource(dst_resource);
4931 src_texture = texture_from_resource(src_resource);
4933 if (src_texture->layer_count != dst_texture->layer_count
4934 || src_texture->level_count != dst_texture->level_count)
4936 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4937 dst_texture->layer_count, dst_texture->level_count,
4938 src_texture->layer_count, src_texture->level_count);
4939 return;
4942 for (i = 0; i < dst_texture->level_count; ++i)
4944 wined3d_texture_get_level_box(src_texture, i, &src_box);
4945 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4946 for (j = 0; j < dst_texture->layer_count; ++j)
4948 unsigned int idx = j * dst_texture->level_count + i;
4950 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, idx, &dst_box,
4951 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4956 HRESULT CDECL wined3d_device_copy_sub_resource_region(struct wined3d_device *device,
4957 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4958 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4959 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4961 struct wined3d_box dst_box, b;
4963 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4964 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4965 device, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4966 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4968 if (flags)
4969 FIXME("Ignoring flags %#x.\n", flags);
4971 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4973 WARN("Source and destination are the same sub-resource.\n");
4974 return WINED3DERR_INVALIDCALL;
4977 if (!resources_format_compatible(src_resource, dst_resource))
4979 WARN("Resource formats %s and %s are incompatible.\n",
4980 debug_d3dformat(dst_resource->format->id),
4981 debug_d3dformat(src_resource->format->id));
4982 return WINED3DERR_INVALIDCALL;
4985 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4987 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4989 WARN("Resource types (%s / %s) don't match.\n",
4990 debug_d3dresourcetype(dst_resource->type),
4991 debug_d3dresourcetype(src_resource->type));
4992 return WINED3DERR_INVALIDCALL;
4995 if (dst_sub_resource_idx)
4997 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4998 return WINED3DERR_INVALIDCALL;
5001 if (src_sub_resource_idx)
5003 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
5004 return WINED3DERR_INVALIDCALL;
5007 if (!src_box)
5009 unsigned int dst_w;
5011 dst_w = dst_resource->size - dst_x;
5012 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
5013 src_box = &b;
5015 else if ((src_box->left >= src_box->right
5016 || src_box->top >= src_box->bottom
5017 || src_box->front >= src_box->back))
5019 WARN("Invalid box %s specified.\n", debug_box(src_box));
5020 return WINED3DERR_INVALIDCALL;
5023 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
5024 || src_box->right - src_box->left > dst_resource->size - dst_x)
5026 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
5027 dst_x, src_box->left, src_box->right - src_box->left);
5028 return WINED3DERR_INVALIDCALL;
5031 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
5033 else
5035 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
5036 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
5037 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
5038 unsigned int src_row_block_count, src_row_count;
5040 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
5042 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
5043 return WINED3DERR_INVALIDCALL;
5046 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
5048 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
5049 return WINED3DERR_INVALIDCALL;
5052 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
5054 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
5055 return WINED3DERR_INVALIDCALL;
5058 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
5060 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
5061 return WINED3DERR_INVALIDCALL;
5064 if (!src_box)
5066 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
5068 src_w = wined3d_texture_get_level_width(src_texture, src_level);
5069 src_h = wined3d_texture_get_level_height(src_texture, src_level);
5070 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
5072 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5073 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
5074 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
5075 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
5077 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
5078 src_box = &b;
5080 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
5082 WARN("Invalid source box %s.\n", debug_box(src_box));
5083 return WINED3DERR_INVALIDCALL;
5086 if (src_resource->format->block_width == dst_resource->format->block_width
5087 && src_resource->format->block_height == dst_resource->format->block_height)
5089 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
5090 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
5092 else
5094 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
5095 / src_resource->format->block_width;
5096 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
5097 / src_resource->format->block_height;
5098 wined3d_box_set(&dst_box, dst_x, dst_y,
5099 dst_x + (src_row_block_count * dst_resource->format->block_width),
5100 dst_y + (src_row_count * dst_resource->format->block_height),
5101 dst_z, dst_z + (src_box->back - src_box->front));
5103 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
5104 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
5106 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
5107 return WINED3DERR_INVALIDCALL;
5111 wined3d_cs_emit_blt_sub_resource(device->cs, dst_resource, dst_sub_resource_idx, &dst_box,
5112 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5114 return WINED3D_OK;
5117 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
5118 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
5119 unsigned int depth_pitch, unsigned int flags)
5121 unsigned int width, height, depth;
5122 struct wined3d_box b;
5124 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, "
5125 "flags %#x.\n",
5126 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
5128 if (flags)
5129 FIXME("Ignoring flags %#x.\n", flags);
5131 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5133 WARN("Resource %p is not GPU accessible.\n", resource);
5134 return;
5137 if (resource->type == WINED3D_RTYPE_BUFFER)
5139 if (sub_resource_idx > 0)
5141 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
5142 return;
5145 width = resource->size;
5146 height = 1;
5147 depth = 1;
5149 else
5151 struct wined3d_texture *texture = texture_from_resource(resource);
5152 unsigned int level;
5154 if (sub_resource_idx >= texture->level_count * texture->layer_count)
5156 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
5157 return;
5160 level = sub_resource_idx % texture->level_count;
5161 width = wined3d_texture_get_level_width(texture, level);
5162 height = wined3d_texture_get_level_height(texture, level);
5163 depth = wined3d_texture_get_level_depth(texture, level);
5166 if (!box)
5168 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
5169 box = &b;
5171 else if (box->left >= box->right || box->right > width
5172 || box->top >= box->bottom || box->bottom > height
5173 || box->front >= box->back || box->back > depth)
5175 WARN("Invalid box %s specified.\n", debug_box(box));
5176 return;
5179 wined3d_resource_wait_idle(resource);
5181 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
5184 void CDECL wined3d_device_resolve_sub_resource(struct wined3d_device *device,
5185 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
5186 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
5187 enum wined3d_format_id format_id)
5189 struct wined3d_texture *dst_texture, *src_texture;
5190 unsigned int dst_level, src_level;
5191 RECT dst_rect, src_rect;
5193 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, "
5194 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
5195 device, dst_resource, dst_sub_resource_idx,
5196 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
5198 if (wined3d_format_is_typeless(dst_resource->format)
5199 || wined3d_format_is_typeless(src_resource->format))
5201 FIXME("Multisample resolve is not fully supported for typeless formats "
5202 "(dst_format %s, src_format %s, format %s).\n",
5203 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
5204 debug_d3dformat(format_id));
5206 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5208 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
5209 return;
5211 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5213 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
5214 return;
5217 dst_texture = texture_from_resource(dst_resource);
5218 src_texture = texture_from_resource(src_resource);
5220 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5221 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
5222 wined3d_texture_get_level_height(dst_texture, dst_level));
5223 src_level = src_sub_resource_idx % src_texture->level_count;
5224 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
5225 wined3d_texture_get_level_height(src_texture, src_level));
5226 wined3d_texture_blt(dst_texture, dst_sub_resource_idx, &dst_rect,
5227 src_texture, src_sub_resource_idx, &src_rect, 0, NULL, WINED3D_TEXF_POINT);
5230 HRESULT CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
5231 struct wined3d_rendertarget_view *view, const RECT *rect, DWORD flags,
5232 const struct wined3d_color *color, float depth, DWORD stencil)
5234 struct wined3d_resource *resource;
5235 RECT r;
5237 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
5238 device, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
5240 if (!flags)
5241 return WINED3D_OK;
5243 resource = view->resource;
5244 if (resource->type == WINED3D_RTYPE_BUFFER)
5246 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
5247 return WINED3DERR_INVALIDCALL;
5250 if (!rect)
5252 SetRect(&r, 0, 0, view->width, view->height);
5253 rect = &r;
5255 else
5257 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
5258 struct wined3d_texture *texture = texture_from_resource(view->resource);
5259 HRESULT hr;
5261 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
5262 view->sub_resource_idx % texture->level_count, &b)))
5263 return hr;
5266 wined3d_cs_emit_clear_rendertarget_view(device->cs, view, rect, flags, color, depth, stencil);
5268 return WINED3D_OK;
5271 void CDECL wined3d_device_clear_unordered_access_view_uint(struct wined3d_device *device,
5272 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
5274 TRACE("device %p, view %p, clear_value %s.\n", device, view, debug_uvec4(clear_value));
5276 wined3d_cs_emit_clear_unordered_access_view_uint(device->cs, view, clear_value);
5279 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
5280 unsigned int view_idx)
5282 unsigned int max_rt_count;
5284 TRACE("device %p, view_idx %u.\n", device, view_idx);
5286 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
5287 if (view_idx >= max_rt_count)
5289 WARN("Only %u render targets are supported.\n", max_rt_count);
5290 return NULL;
5293 return device->cs->c.state->fb.render_targets[view_idx];
5296 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
5298 TRACE("device %p.\n", device);
5300 return device->cs->c.state->fb.depth_stencil;
5303 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
5304 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
5306 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
5307 device, view_idx, view, set_viewport);
5309 return wined3d_device_context_set_rendertarget_view(&device->cs->c, view_idx, view, set_viewport);
5312 HRESULT CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device,
5313 struct wined3d_rendertarget_view *view)
5315 TRACE("device %p, view %p.\n", device, view);
5317 return wined3d_device_context_set_depth_stencil_view(&device->cs->c, view);
5320 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5321 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5323 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5324 struct wined3d_sub_resource_data data;
5325 struct wined3d_resource_desc desc;
5326 struct wined3d_map_desc map_desc;
5327 struct wined3d_texture *texture;
5328 HRESULT hr;
5330 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5332 ERR("Failed to map source texture.\n");
5333 return NULL;
5336 data.data = map_desc.data;
5337 data.row_pitch = map_desc.row_pitch;
5338 data.slice_pitch = map_desc.slice_pitch;
5340 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5341 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5342 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5343 desc.multisample_quality = 0;
5344 desc.usage = WINED3DUSAGE_DYNAMIC;
5345 desc.bind_flags = 0;
5346 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5347 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5348 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5349 desc.depth = 1;
5350 desc.size = 0;
5352 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5353 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5354 if (FAILED(hr))
5356 ERR("Failed to create cursor texture.\n");
5357 return NULL;
5360 return texture;
5363 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5364 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5366 unsigned int texture_level = sub_resource_idx % texture->level_count;
5367 unsigned int cursor_width, cursor_height;
5368 struct wined3d_map_desc map_desc;
5370 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5371 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5373 if (sub_resource_idx >= texture->level_count * texture->layer_count
5374 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5375 return WINED3DERR_INVALIDCALL;
5377 if (device->cursor_texture)
5379 wined3d_texture_decref(device->cursor_texture);
5380 device->cursor_texture = NULL;
5383 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5385 WARN("Texture %p has invalid format %s.\n",
5386 texture, debug_d3dformat(texture->resource.format->id));
5387 return WINED3DERR_INVALIDCALL;
5390 /* Cursor width and height must all be powers of two */
5391 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5392 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5393 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5395 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5396 return WINED3DERR_INVALIDCALL;
5399 /* Do not store the surface's pointer because the application may
5400 * release it after setting the cursor image. Windows doesn't
5401 * addref the set surface, so we can't do this either without
5402 * creating circular refcount dependencies. */
5403 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5405 ERR("Failed to create cursor texture.\n");
5406 return WINED3DERR_INVALIDCALL;
5409 if (cursor_width == 32 && cursor_height == 32)
5411 UINT mask_size = cursor_width * cursor_height / 8;
5412 ICONINFO cursor_info;
5413 DWORD *mask_bits;
5414 HCURSOR cursor;
5416 /* 32-bit user32 cursors ignore the alpha channel if it's all
5417 * zeroes, and use the mask instead. Fill the mask with all ones
5418 * to ensure we still get a fully transparent cursor. */
5419 if (!(mask_bits = heap_alloc(mask_size)))
5420 return E_OUTOFMEMORY;
5421 memset(mask_bits, 0xff, mask_size);
5423 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5424 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5425 cursor_info.fIcon = FALSE;
5426 cursor_info.xHotspot = x_hotspot;
5427 cursor_info.yHotspot = y_hotspot;
5428 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5429 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5430 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5432 /* Create our cursor and clean up. */
5433 cursor = CreateIconIndirect(&cursor_info);
5434 if (cursor_info.hbmMask)
5435 DeleteObject(cursor_info.hbmMask);
5436 if (cursor_info.hbmColor)
5437 DeleteObject(cursor_info.hbmColor);
5438 if (device->hardwareCursor)
5439 DestroyCursor(device->hardwareCursor);
5440 device->hardwareCursor = cursor;
5441 if (device->bCursorVisible)
5442 SetCursor(cursor);
5444 heap_free(mask_bits);
5447 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5448 device->cursorWidth = cursor_width;
5449 device->cursorHeight = cursor_height;
5450 device->xHotSpot = x_hotspot;
5451 device->yHotSpot = y_hotspot;
5453 return WINED3D_OK;
5456 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5457 int x_screen_space, int y_screen_space, DWORD flags)
5459 TRACE("device %p, x %d, y %d, flags %#x.\n",
5460 device, x_screen_space, y_screen_space, flags);
5462 device->xScreenSpace = x_screen_space;
5463 device->yScreenSpace = y_screen_space;
5465 if (device->hardwareCursor)
5467 POINT pt;
5469 GetCursorPos( &pt );
5470 if (x_screen_space == pt.x && y_screen_space == pt.y)
5471 return;
5472 SetCursorPos( x_screen_space, y_screen_space );
5474 /* Switch to the software cursor if position diverges from the hardware one. */
5475 GetCursorPos( &pt );
5476 if (x_screen_space != pt.x || y_screen_space != pt.y)
5478 if (device->bCursorVisible) SetCursor( NULL );
5479 DestroyCursor( device->hardwareCursor );
5480 device->hardwareCursor = 0;
5485 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5487 BOOL oldVisible = device->bCursorVisible;
5489 TRACE("device %p, show %#x.\n", device, show);
5492 * When ShowCursor is first called it should make the cursor appear at the OS's last
5493 * known cursor position.
5495 if (show && !oldVisible)
5497 POINT pt;
5498 GetCursorPos(&pt);
5499 device->xScreenSpace = pt.x;
5500 device->yScreenSpace = pt.y;
5503 if (device->hardwareCursor)
5505 device->bCursorVisible = show;
5506 if (show)
5507 SetCursor(device->hardwareCursor);
5508 else
5509 SetCursor(NULL);
5511 else if (device->cursor_texture)
5513 device->bCursorVisible = show;
5516 return oldVisible;
5519 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5521 struct wined3d_resource *resource, *cursor;
5523 TRACE("device %p.\n", device);
5525 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5527 TRACE("Checking resource %p for eviction.\n", resource);
5529 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5531 TRACE("Evicting %p.\n", resource);
5532 wined3d_cs_emit_unload_resource(device->cs, resource);
5537 void CDECL wined3d_device_flush(struct wined3d_device *device)
5539 TRACE("device %p.\n", device);
5541 wined3d_cs_emit_flush(device->cs);
5544 static void update_swapchain_flags(struct wined3d_texture *texture)
5546 unsigned int flags = texture->swapchain->state.desc.flags;
5548 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5549 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5550 else
5551 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5553 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5554 texture->flags |= WINED3D_TEXTURE_GET_DC;
5555 else
5556 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5559 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5560 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5561 wined3d_device_reset_cb callback, BOOL reset_state)
5563 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5564 struct wined3d_swapchain_state *swapchain_state;
5565 struct wined3d_swapchain_desc *current_desc;
5566 struct wined3d_state *state = device->cs->c.state;
5567 struct wined3d_resource *resource, *cursor;
5568 struct wined3d_rendertarget_view *view;
5569 struct wined3d_swapchain *swapchain;
5570 struct wined3d_view_desc view_desc;
5571 BOOL backbuffer_resized, windowed;
5572 HRESULT hr = WINED3D_OK;
5573 unsigned int i;
5575 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5576 device, swapchain_desc, mode, callback, reset_state);
5578 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5580 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5582 ERR("Failed to get the first implicit swapchain.\n");
5583 return WINED3DERR_INVALIDCALL;
5585 swapchain_state = &swapchain->state;
5586 current_desc = &swapchain_state->desc;
5588 if (reset_state)
5590 if (device->logo_texture)
5592 wined3d_texture_decref(device->logo_texture);
5593 device->logo_texture = NULL;
5595 if (device->cursor_texture)
5597 wined3d_texture_decref(device->cursor_texture);
5598 device->cursor_texture = NULL;
5600 state_unbind_resources(state);
5603 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
5605 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
5607 wined3d_device_set_depth_stencil_view(device, NULL);
5609 if (reset_state)
5611 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5613 TRACE("Enumerating resource %p.\n", resource);
5614 if (FAILED(hr = callback(resource)))
5615 return hr;
5619 TRACE("New params:\n");
5620 TRACE("output %p\n", swapchain_desc->output);
5621 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5622 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5623 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5624 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5625 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5626 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5627 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5628 TRACE("device_window %p\n", swapchain_desc->device_window);
5629 TRACE("windowed %#x\n", swapchain_desc->windowed);
5630 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5631 if (swapchain_desc->enable_auto_depth_stencil)
5632 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5633 TRACE("flags %#x\n", swapchain_desc->flags);
5634 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5635 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5637 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5638 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5640 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5641 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5642 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5643 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5645 /* No special treatment of these parameters. Just store them */
5646 current_desc->swap_effect = swapchain_desc->swap_effect;
5647 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5648 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5649 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5650 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5652 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5654 TRACE("Changing the device window from %p to %p.\n",
5655 current_desc->device_window, swapchain_desc->device_window);
5656 current_desc->device_window = swapchain_desc->device_window;
5657 swapchain_state->device_window = swapchain_desc->device_window;
5658 wined3d_swapchain_set_window(swapchain, NULL);
5661 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5662 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5663 windowed = current_desc->windowed;
5665 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5666 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5668 /* Switch from windowed to fullscreen. */
5669 if (windowed && !swapchain_desc->windowed)
5671 HWND focus_window = device->create_parms.focus_window;
5673 if (!focus_window)
5674 focus_window = swapchain->state.device_window;
5675 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5677 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5678 return hr;
5682 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5683 swapchain_desc, mode)))
5684 return hr;
5686 /* Switch from fullscreen to windowed. */
5687 if (!windowed && swapchain_desc->windowed)
5688 wined3d_device_release_focus_window(device);
5690 else if (!swapchain_desc->windowed)
5692 DWORD style = swapchain_state->style;
5693 DWORD exstyle = swapchain_state->exstyle;
5694 struct wined3d_output_desc output_desc;
5696 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5697 * the window back into the right position. Some applications
5698 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5699 * up their mess. Guild Wars also loses the device during that. */
5700 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5702 ERR("Failed to get output description, hr %#x.\n", hr);
5703 return hr;
5706 swapchain_state->style = 0;
5707 swapchain_state->exstyle = 0;
5708 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5709 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5710 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5711 swapchain_state->style = style;
5712 swapchain_state->exstyle = exstyle;
5715 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5716 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5717 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5718 return hr;
5720 if (swapchain_desc->flags != current_desc->flags)
5722 current_desc->flags = swapchain_desc->flags;
5724 update_swapchain_flags(swapchain->front_buffer);
5725 for (i = 0; i < current_desc->backbuffer_count; ++i)
5727 update_swapchain_flags(swapchain->back_buffers[i]);
5731 if ((view = device->auto_depth_stencil_view))
5733 device->auto_depth_stencil_view = NULL;
5734 wined3d_rendertarget_view_decref(view);
5736 if (current_desc->enable_auto_depth_stencil)
5738 struct wined3d_resource_desc texture_desc;
5739 struct wined3d_texture *texture;
5741 TRACE("Creating the depth stencil buffer.\n");
5743 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5744 texture_desc.format = current_desc->auto_depth_stencil_format;
5745 texture_desc.multisample_type = current_desc->multisample_type;
5746 texture_desc.multisample_quality = current_desc->multisample_quality;
5747 texture_desc.usage = 0;
5748 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5749 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5750 texture_desc.width = current_desc->backbuffer_width;
5751 texture_desc.height = current_desc->backbuffer_height;
5752 texture_desc.depth = 1;
5753 texture_desc.size = 0;
5755 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5756 device->device_parent, &texture_desc, 0, &texture)))
5758 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5759 return WINED3DERR_INVALIDCALL;
5762 view_desc.format_id = texture->resource.format->id;
5763 view_desc.flags = 0;
5764 view_desc.u.texture.level_idx = 0;
5765 view_desc.u.texture.level_count = 1;
5766 view_desc.u.texture.layer_idx = 0;
5767 view_desc.u.texture.layer_count = 1;
5768 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5769 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5770 wined3d_texture_decref(texture);
5771 if (FAILED(hr))
5773 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5774 return hr;
5778 if ((view = device->back_buffer_view))
5780 device->back_buffer_view = NULL;
5781 wined3d_rendertarget_view_decref(view);
5783 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5785 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5787 view_desc.format_id = back_buffer->format->id;
5788 view_desc.flags = 0;
5789 view_desc.u.texture.level_idx = 0;
5790 view_desc.u.texture.level_count = 1;
5791 view_desc.u.texture.layer_idx = 0;
5792 view_desc.u.texture.layer_count = 1;
5793 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5794 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5796 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5797 return hr;
5801 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5802 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5803 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5804 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5806 if (reset_state)
5808 TRACE("Resetting state.\n");
5809 wined3d_cs_emit_reset_state(device->cs);
5810 state_cleanup(state);
5812 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5814 TRACE("Unloading resource %p.\n", resource);
5815 wined3d_cs_emit_unload_resource(device->cs, resource);
5818 device->adapter->adapter_ops->adapter_uninit_3d(device);
5820 wined3d_state_reset(state, &device->adapter->d3d_info);
5822 device_init_swapchain_state(device, swapchain);
5823 if (wined3d_settings.logo)
5824 device_load_logo(device, wined3d_settings.logo);
5826 else
5828 if ((view = device->back_buffer_view))
5829 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
5830 if ((view = device->auto_depth_stencil_view))
5831 wined3d_device_set_depth_stencil_view(device, view);
5834 if (reset_state)
5835 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5837 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5838 * first use
5840 return hr;
5843 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5845 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5847 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5849 return WINED3D_OK;
5853 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5854 struct wined3d_device_creation_parameters *parameters)
5856 TRACE("device %p, parameters %p.\n", device, parameters);
5858 *parameters = device->create_parms;
5861 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5863 TRACE("device %p.\n", device);
5865 return device->wined3d;
5868 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5869 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5871 struct wined3d_swapchain *swapchain;
5873 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5874 device, swapchain_idx, flags, ramp);
5876 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5877 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5880 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5881 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5883 struct wined3d_swapchain *swapchain;
5885 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5886 device, swapchain_idx, ramp);
5888 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5889 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5892 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5894 TRACE("device %p, resource %p.\n", device, resource);
5896 wined3d_not_from_cs(device->cs);
5898 list_add_head(&device->resources, &resource->resource_list_entry);
5901 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5903 TRACE("device %p, resource %p.\n", device, resource);
5905 wined3d_not_from_cs(device->cs);
5907 list_remove(&resource->resource_list_entry);
5910 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5912 enum wined3d_resource_type type = resource->type;
5913 struct wined3d_state *state = device->cs->c.state;
5914 struct wined3d_rendertarget_view *rtv;
5915 unsigned int i;
5917 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5919 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5921 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5922 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5925 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5926 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5928 switch (type)
5930 case WINED3D_RTYPE_TEXTURE_1D:
5931 case WINED3D_RTYPE_TEXTURE_2D:
5932 case WINED3D_RTYPE_TEXTURE_3D:
5933 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5935 if (&state->textures[i]->resource == resource)
5937 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5938 state->textures[i] = NULL;
5941 break;
5943 case WINED3D_RTYPE_BUFFER:
5944 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5946 if (&state->streams[i].buffer->resource == resource)
5948 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5949 state->streams[i].buffer = NULL;
5953 if (&state->index_buffer->resource == resource)
5955 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5956 state->index_buffer = NULL;
5958 break;
5960 default:
5961 break;
5964 /* Remove the resource from the resourceStore */
5965 device_resource_remove(device, resource);
5967 TRACE("Resource released.\n");
5970 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5972 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5973 struct wined3d_so_desc_entry, entry)->desc;
5974 const struct wined3d_stream_output_desc *k = key;
5975 unsigned int i;
5976 int ret;
5978 if ((ret = (k->element_count - desc->element_count)))
5979 return ret;
5980 if ((ret = (k->buffer_stride_count - desc->buffer_stride_count)))
5981 return ret;
5982 if ((ret = (k->rasterizer_stream_idx - desc->rasterizer_stream_idx)))
5983 return ret;
5985 for (i = 0; i < k->element_count; ++i)
5987 const struct wined3d_stream_output_element *b = &desc->elements[i];
5988 const struct wined3d_stream_output_element *a = &k->elements[i];
5990 if ((ret = (a->stream_idx - b->stream_idx)))
5991 return ret;
5992 if ((ret = strcmp(a->semantic_name, b->semantic_name)))
5993 return ret;
5994 if ((ret = (a->semantic_idx - b->semantic_idx)))
5995 return ret;
5996 if ((ret = (a->component_idx - b->component_idx)))
5997 return ret;
5998 if ((ret = (a->component_count - b->component_count)))
5999 return ret;
6000 if ((ret = (a->output_slot - b->output_slot)))
6001 return ret;
6004 for (i = 0; i < k->buffer_stride_count; ++i)
6006 if ((ret = (k->buffer_strides[i] - desc->buffer_strides[i])))
6007 return ret;
6010 return 0;
6013 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
6015 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
6017 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
6020 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
6022 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
6024 return memcmp(&state->desc, key, sizeof(state->desc));
6027 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
6029 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
6031 return memcmp(&state->desc, key, sizeof(state->desc));
6034 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
6036 const struct wined3d_depth_stencil_state *state
6037 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
6039 return memcmp(&state->desc, key, sizeof(state->desc));
6042 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
6043 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
6044 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
6045 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
6047 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
6048 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
6049 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
6050 unsigned int i;
6051 HRESULT hr;
6053 device->ref = 1;
6054 device->wined3d = wined3d;
6055 wined3d_incref(device->wined3d);
6056 device->adapter = adapter;
6057 device->device_parent = device_parent;
6058 list_init(&device->resources);
6059 list_init(&device->shaders);
6060 device->surface_alignment = surface_alignment;
6062 /* Save the creation parameters. */
6063 device->create_parms.adapter_idx = adapter_idx;
6064 device->create_parms.device_type = device_type;
6065 device->create_parms.focus_window = focus_window;
6066 device->create_parms.flags = flags;
6068 device->shader_backend = adapter->shader_backend;
6070 vertex_pipeline = adapter->vertex_pipe;
6072 fragment_pipeline = adapter->fragment_pipe;
6074 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
6075 wine_rb_init(&device->samplers, wined3d_sampler_compare);
6076 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
6077 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
6078 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
6080 if (vertex_pipeline->vp_states && fragment_pipeline->states
6081 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
6082 &adapter->d3d_info, supported_extensions, vertex_pipeline,
6083 fragment_pipeline, adapter->misc_state_template)))
6085 ERR("Failed to compile state table, hr %#x.\n", hr);
6086 wine_rb_destroy(&device->samplers, NULL, NULL);
6087 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6088 wine_rb_destroy(&device->blend_states, NULL, NULL);
6089 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6090 wine_rb_destroy(&device->so_descs, NULL, NULL);
6091 wined3d_decref(device->wined3d);
6092 return hr;
6095 device->max_frame_latency = 3;
6097 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
6099 WARN("Failed to create command stream.\n");
6100 hr = E_FAIL;
6101 goto err;
6104 return WINED3D_OK;
6106 err:
6107 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
6109 heap_free(device->multistate_funcs[i]);
6111 wine_rb_destroy(&device->samplers, NULL, NULL);
6112 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6113 wine_rb_destroy(&device->blend_states, NULL, NULL);
6114 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6115 wine_rb_destroy(&device->so_descs, NULL, NULL);
6116 wined3d_decref(device->wined3d);
6117 return hr;
6120 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
6122 unsigned int representative, i, idx, shift;
6123 struct wined3d_context *context;
6125 wined3d_from_cs(device->cs);
6127 if (STATE_IS_COMPUTE(state_id))
6129 for (i = 0; i < device->context_count; ++i)
6130 context_invalidate_compute_state(device->contexts[i], state_id);
6131 return;
6134 representative = device->state_table[state_id].representative;
6135 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
6136 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
6137 for (i = 0; i < device->context_count; ++i)
6139 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
6143 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
6144 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
6146 if (message == WM_DESTROY)
6148 TRACE("unregister window %p.\n", window);
6149 wined3d_unregister_window(window);
6151 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
6152 ERR("Window %p is not the focus window for device %p.\n", window, device);
6154 else if (message == WM_DISPLAYCHANGE)
6156 device->device_parent->ops->mode_changed(device->device_parent);
6158 else if (message == WM_ACTIVATEAPP)
6160 unsigned int i = device->swapchain_count;
6162 /* Deactivating the implicit swapchain may cause the application
6163 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
6164 * deactivate the implicit swapchain last, and to avoid accessing the
6165 * "device" pointer afterwards. */
6166 while (i--)
6167 wined3d_swapchain_activate(device->swapchains[i], wparam);
6169 else if (message == WM_SYSCOMMAND)
6171 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
6173 if (unicode)
6174 DefWindowProcW(window, message, wparam, lparam);
6175 else
6176 DefWindowProcA(window, message, wparam, lparam);
6180 if (unicode)
6181 return CallWindowProcW(proc, window, message, wparam, lparam);
6182 else
6183 return CallWindowProcA(proc, window, message, wparam, lparam);