winemac.drv: Remove workarounds for Mac OS X 10.6 and earlier.
[wine.git] / dlls / wined3d / device.c
blobc5de58c29c94b459eebb0245e07c73bb91bb7c4a
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 wined3d_mutex_lock();
267 device->adapter->adapter_ops->adapter_destroy_device(device);
268 TRACE("Destroyed device %p.\n", device);
269 wined3d_mutex_unlock();
272 return refcount;
275 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
277 TRACE("device %p.\n", device);
279 return device->swapchain_count;
282 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
284 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
286 if (swapchain_idx >= device->swapchain_count)
288 WARN("swapchain_idx %u >= swapchain_count %u.\n",
289 swapchain_idx, device->swapchain_count);
290 return NULL;
293 return device->swapchains[swapchain_idx];
296 static void device_load_logo(struct wined3d_device *device, const char *filename)
298 struct wined3d_color_key color_key;
299 struct wined3d_resource_desc desc;
300 HBITMAP hbm;
301 BITMAP bm;
302 HRESULT hr;
303 HDC dcb = NULL, dcs = NULL;
305 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
307 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
308 return;
310 GetObjectA(hbm, sizeof(BITMAP), &bm);
312 if (!(dcb = CreateCompatibleDC(NULL)))
313 goto out;
314 SelectObject(dcb, hbm);
316 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
317 desc.format = WINED3DFMT_B5G6R5_UNORM;
318 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
319 desc.multisample_quality = 0;
320 desc.usage = WINED3DUSAGE_DYNAMIC;
321 desc.bind_flags = 0;
322 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
323 desc.width = bm.bmWidth;
324 desc.height = bm.bmHeight;
325 desc.depth = 1;
326 desc.size = 0;
327 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
328 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
330 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
331 goto out;
334 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
336 wined3d_texture_decref(device->logo_texture);
337 device->logo_texture = NULL;
338 goto out;
340 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
341 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
343 color_key.color_space_low_value = 0;
344 color_key.color_space_high_value = 0;
345 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
347 out:
348 if (dcb) DeleteDC(dcb);
349 if (hbm) DeleteObject(hbm);
352 /* Context activation is done by the caller. */
353 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
354 struct wined3d_context_gl *context_gl)
356 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
357 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
358 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
359 unsigned int i;
360 DWORD color;
362 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
363 color = 0x000000ff;
364 else
365 color = 0x00000000;
367 /* Under DirectX you can sample even if no texture is bound, whereas
368 * OpenGL will only allow that when a valid texture is bound.
369 * We emulate this by creating dummy textures and binding them
370 * to each texture stage when the currently set D3D texture is NULL. */
371 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
373 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
374 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
375 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
376 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
377 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
379 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
380 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
381 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
382 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
383 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
385 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
387 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
388 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
389 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
390 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
391 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
394 if (gl_info->supported[EXT_TEXTURE3D])
396 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
397 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
398 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
399 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
400 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
403 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
405 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
406 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
407 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
408 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
410 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
411 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
415 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
417 DWORD cube_array_data[6];
419 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
420 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
421 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
422 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
423 cube_array_data[i] = color;
424 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
425 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
428 if (gl_info->supported[EXT_TEXTURE_ARRAY])
430 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
431 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
432 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
433 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
434 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
436 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
437 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
438 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
439 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
440 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
443 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
445 GLuint buffer;
447 GL_EXTCALL(glGenBuffers(1, &buffer));
448 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
449 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
450 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
452 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
453 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
454 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
455 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
456 GL_EXTCALL(glDeleteBuffers(1, &buffer));
459 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
461 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
462 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
463 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
464 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
466 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
467 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
468 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
469 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
471 if (gl_info->supported[ARB_CLEAR_TEXTURE])
473 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
474 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
476 else
478 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
482 checkGLcall("create dummy textures");
484 wined3d_context_gl_bind_dummy_textures(context_gl);
487 /* Context activation is done by the caller. */
488 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
489 struct wined3d_context_gl *context_gl)
491 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
492 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
494 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
496 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
497 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
500 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
501 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
503 if (gl_info->supported[EXT_TEXTURE_ARRAY])
505 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
506 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
509 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
510 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
512 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
513 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
515 if (gl_info->supported[EXT_TEXTURE3D])
516 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
518 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
519 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
521 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
522 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
524 checkGLcall("delete dummy textures");
526 memset(dummy_textures, 0, sizeof(*dummy_textures));
529 /* Context activation is done by the caller. */
530 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
532 struct wined3d_sampler_desc desc;
533 HRESULT hr;
535 desc.address_u = WINED3D_TADDRESS_WRAP;
536 desc.address_v = WINED3D_TADDRESS_WRAP;
537 desc.address_w = WINED3D_TADDRESS_WRAP;
538 memset(desc.border_color, 0, sizeof(desc.border_color));
539 desc.mag_filter = WINED3D_TEXF_POINT;
540 desc.min_filter = WINED3D_TEXF_POINT;
541 desc.mip_filter = WINED3D_TEXF_NONE;
542 desc.lod_bias = 0.0f;
543 desc.min_lod = -1000.0f;
544 desc.max_lod = 1000.0f;
545 desc.mip_base_level = 0;
546 desc.max_anisotropy = 1;
547 desc.compare = FALSE;
548 desc.comparison_func = WINED3D_CMP_NEVER;
549 desc.srgb_decode = TRUE;
551 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
552 * instructions allow access to resources without using samplers.
553 * In GLSL, resources are always accessed through sampler or image variables. The default
554 * sampler object is used to emulate the direct resource access when there is no sampler state
555 * to use.
557 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
559 ERR("Failed to create default sampler, hr %#x.\n", hr);
560 device->default_sampler = NULL;
563 /* In D3D10+, a NULL sampler maps to the default sampler state. */
564 desc.address_u = WINED3D_TADDRESS_CLAMP;
565 desc.address_v = WINED3D_TADDRESS_CLAMP;
566 desc.address_w = WINED3D_TADDRESS_CLAMP;
567 desc.mag_filter = WINED3D_TEXF_LINEAR;
568 desc.min_filter = WINED3D_TEXF_LINEAR;
569 desc.mip_filter = WINED3D_TEXF_LINEAR;
570 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
572 ERR("Failed to create null sampler, hr %#x.\n", hr);
573 device->null_sampler = NULL;
577 void wined3d_device_destroy_default_samplers(struct wined3d_device *device)
579 wined3d_sampler_decref(device->default_sampler);
580 device->default_sampler = NULL;
581 wined3d_sampler_decref(device->null_sampler);
582 device->null_sampler = NULL;
585 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
586 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
588 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
589 VkImageSubresourceRange range;
590 uint32_t flags = 0;
592 static const VkClearColorValue colour = {{0}};
594 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
595 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
597 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
598 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
600 if (!wined3d_context_vk_create_image(context_vk, type,
601 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
602 1, 1, 1, sample_count, 1, layer_count, flags, image))
604 return false;
607 wined3d_context_vk_reference_image(context_vk, image);
609 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
610 range.baseMipLevel = 0;
611 range.levelCount = 1;
612 range.baseArrayLayer = 0;
613 range.layerCount = layer_count;
615 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
616 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
617 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
619 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
620 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
622 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
623 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
624 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
626 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
627 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
629 return true;
632 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
633 struct wined3d_context_vk *context_vk)
635 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
636 const struct wined3d_vk_info *vk_info;
637 const struct wined3d_format *format;
638 VkMemoryPropertyFlags memory_type;
639 VkCommandBuffer vk_command_buffer;
640 unsigned int sample_count = 2;
641 VkBufferUsageFlags usage;
643 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
644 while (sample_count && !(sample_count & format->multisample_types))
645 sample_count <<= 1;
647 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
649 ERR("Failed to get command buffer.\n");
650 return false;
653 vk_info = context_vk->vk_info;
655 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
656 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
657 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
658 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
659 return false;
660 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.buffer_offset, r->bo.size, 0x00000000u));
661 r->buffer_info.buffer = r->bo.vk_buffer;
662 r->buffer_info.offset = r->bo.buffer_offset;
663 r->buffer_info.range = r->bo.size;
665 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
667 ERR("Failed to create 1D image.\n");
668 goto fail;
671 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
673 ERR("Failed to create 2D image.\n");
674 goto fail;
677 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
679 ERR("Failed to create 2D MSAA image.\n");
680 goto fail;
683 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
685 ERR("Failed to create 3D image.\n");
686 goto fail;
689 return true;
691 fail:
692 if (r->image_2dms.vk_image)
693 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
694 if (r->image_2d.vk_image)
695 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
696 if (r->image_1d.vk_image)
697 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
698 wined3d_context_vk_reference_bo(context_vk, &r->bo);
699 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
700 return false;
703 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
704 struct wined3d_context_vk *context_vk)
706 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
708 /* We don't track command buffer references to NULL resources. We easily
709 * could, but it doesn't seem worth it. */
710 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
711 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
712 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
713 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
714 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
715 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
716 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
717 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
718 wined3d_context_vk_reference_bo(context_vk, &r->bo);
719 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
722 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
724 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
725 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
726 VkBufferViewCreateInfo buffer_create_info;
727 const struct wined3d_vk_info *vk_info;
728 VkImageViewCreateInfo view_desc;
729 VkResult vr;
731 vk_info = context_vk->vk_info;
733 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
734 buffer_create_info.pNext = NULL;
735 buffer_create_info.flags = 0;
736 buffer_create_info.buffer = r->bo.vk_buffer;
737 buffer_create_info.format = VK_FORMAT_R32_UINT;
738 buffer_create_info.offset = r->bo.buffer_offset;
739 buffer_create_info.range = r->bo.size;
741 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
742 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
744 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
745 return false;
747 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
749 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
750 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
751 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
753 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
754 goto fail;
756 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
758 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
759 view_desc.pNext = NULL;
760 view_desc.flags = 0;
761 view_desc.image = r->image_1d.vk_image;
762 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
763 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
764 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
765 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
766 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
767 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
768 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
769 view_desc.subresourceRange.baseMipLevel = 0;
770 view_desc.subresourceRange.levelCount = 1;
771 view_desc.subresourceRange.baseArrayLayer = 0;
772 view_desc.subresourceRange.layerCount = 1;
773 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
775 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
776 goto fail;
778 v->vk_info_1d.sampler = VK_NULL_HANDLE;
779 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
780 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
782 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
783 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
785 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
786 goto fail;
788 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
789 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
790 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
792 view_desc.image = r->image_2d.vk_image;
793 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
794 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
796 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
797 goto fail;
799 v->vk_info_2d.sampler = VK_NULL_HANDLE;
800 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
801 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
803 view_desc.image = r->image_2dms.vk_image;
804 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
805 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
807 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
808 goto fail;
810 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
811 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
812 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
814 view_desc.image = r->image_3d.vk_image;
815 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
816 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
818 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
819 goto fail;
821 v->vk_info_3d.sampler = VK_NULL_HANDLE;
822 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
823 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
825 view_desc.image = r->image_2d.vk_image;
826 view_desc.subresourceRange.layerCount = 6;
827 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
828 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
830 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
831 goto fail;
833 v->vk_info_cube.sampler = VK_NULL_HANDLE;
834 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
835 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
837 view_desc.subresourceRange.layerCount = 1;
838 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
839 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
841 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
842 goto fail;
844 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
845 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
846 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
848 view_desc.image = r->image_2dms.vk_image;
849 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
850 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
852 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
853 goto fail;
855 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
856 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
857 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
859 view_desc.image = r->image_2d.vk_image;
860 view_desc.subresourceRange.layerCount = 6;
861 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
862 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube_array.imageView))) < 0)
864 ERR("Failed to create cube array image view, vr %s.\n", wined3d_debug_vkresult(vr));
865 goto fail;
867 v->vk_info_cube_array.sampler = VK_NULL_HANDLE;
868 v->vk_info_cube_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
869 TRACE("Created cube array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube_array.imageView));
871 return true;
873 fail:
874 if (v->vk_info_cube_array.imageView)
875 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube_array.imageView, NULL));
876 if (v->vk_info_2d_array.imageView)
877 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
878 if (v->vk_info_cube.imageView)
879 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
880 if (v->vk_info_3d.imageView)
881 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
882 if (v->vk_info_2dms.imageView)
883 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
884 if (v->vk_info_2d.imageView)
885 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
886 if (v->vk_info_1d_array.imageView)
887 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
888 if (v->vk_info_1d.imageView)
889 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
890 if (v->vk_view_buffer_float)
891 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
892 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
893 return false;
896 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
898 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
899 uint64_t id = context_vk->current_command_buffer.id;
901 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube_array.imageView, id);
902 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
903 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
904 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
905 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
906 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
907 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
908 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
909 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
911 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
912 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
915 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
917 unsigned int screensaver_active;
919 TRACE("device %p, window %p.\n", device, window);
921 if (!wined3d_register_window(NULL, window, device, 0))
923 ERR("Failed to register window %p.\n", window);
924 return E_FAIL;
927 InterlockedExchangePointer((void **)&device->focus_window, window);
928 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
929 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
930 if ((device->restore_screensaver = !!screensaver_active))
931 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
933 return WINED3D_OK;
936 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
938 TRACE("device %p.\n", device);
940 if (device->focus_window) wined3d_unregister_window(device->focus_window);
941 InterlockedExchangePointer((void **)&device->focus_window, NULL);
942 if (device->restore_screensaver)
944 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
945 device->restore_screensaver = FALSE;
949 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
951 struct wined3d_rendertarget_view *views[WINED3D_MAX_RENDER_TARGETS] = {0};
952 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
953 struct wined3d_device_context *context = &device->cs->c;
955 if (device->back_buffer_view)
956 views[0] = device->back_buffer_view;
957 wined3d_device_context_set_rendertarget_views(context, 0,
958 device->adapter->d3d_info.limits.max_rt_count, views, !!device->back_buffer_view);
960 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
963 void wined3d_device_delete_opengl_contexts_cs(void *object)
965 struct wined3d_swapchain_gl *swapchain_gl;
966 struct wined3d_device *device = object;
967 struct wined3d_context_gl *context_gl;
968 struct wined3d_device_gl *device_gl;
969 struct wined3d_context *context;
970 struct wined3d_shader *shader;
972 TRACE("device %p.\n", device);
974 device_gl = wined3d_device_gl(device);
976 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
978 device->shader_backend->shader_destroy(shader);
981 context = context_acquire(device, NULL, 0);
982 context_gl = wined3d_context_gl(context);
983 device->blitter->ops->blitter_destroy(device->blitter, context);
984 device->shader_backend->shader_free_private(device, context);
985 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
986 context_release(context);
988 while (device->context_count)
990 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
991 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
992 else
993 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
997 void wined3d_device_create_primary_opengl_context_cs(void *object)
999 struct wined3d_device *device = object;
1000 struct wined3d_context_gl *context_gl;
1001 struct wined3d_swapchain *swapchain;
1002 struct wined3d_context *context;
1003 struct wined3d_texture *target;
1004 HRESULT hr;
1006 TRACE("device %p.\n", device);
1008 swapchain = device->swapchains[0];
1009 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1010 if (!(context = context_acquire(device, target, 0)))
1012 WARN("Failed to acquire context.\n");
1013 return;
1016 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1017 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1019 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1020 context_release(context);
1021 return;
1024 if (!(device->blitter = wined3d_cpu_blitter_create()))
1026 ERR("Failed to create CPU blitter.\n");
1027 device->shader_backend->shader_free_private(device, NULL);
1028 context_release(context);
1029 return;
1032 context_gl = wined3d_context_gl(context);
1034 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1035 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1036 wined3d_arbfp_blitter_create(&device->blitter, device);
1037 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1038 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1040 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device), context_gl);
1041 wined3d_device_create_default_samplers(device, context);
1042 context_release(context);
1045 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1047 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1048 const struct wined3d_swapchain_desc *swapchain_desc;
1049 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1050 DWORD clear_flags = 0;
1051 unsigned int i;
1052 HRESULT hr;
1054 TRACE("device %p, swapchain %p.\n", device, swapchain);
1056 if (device->d3d_initialized)
1057 return WINED3DERR_INVALIDCALL;
1059 device->swapchain_count = 1;
1060 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1062 ERR("Failed to allocate swapchain array.\n");
1063 hr = E_OUTOFMEMORY;
1064 goto err_out;
1066 device->swapchains[0] = swapchain;
1068 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1070 if (fb->render_targets[i])
1071 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1073 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1075 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1076 goto err_out;
1077 device->d3d_initialized = TRUE;
1079 swapchain_desc = &swapchain->state.desc;
1080 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1082 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1083 struct wined3d_view_desc view_desc;
1085 view_desc.format_id = back_buffer->format->id;
1086 view_desc.flags = 0;
1087 view_desc.u.texture.level_idx = 0;
1088 view_desc.u.texture.level_count = 1;
1089 view_desc.u.texture.layer_idx = 0;
1090 view_desc.u.texture.layer_count = 1;
1091 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1092 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1094 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1095 device->adapter->adapter_ops->adapter_uninit_3d(device);
1096 device->d3d_initialized = FALSE;
1097 goto err_out;
1101 device_init_swapchain_state(device, swapchain);
1103 TRACE("All defaults now set up.\n");
1105 /* Clear the screen. */
1106 if (device->back_buffer_view)
1107 clear_flags |= WINED3DCLEAR_TARGET;
1108 if (swapchain_desc->enable_auto_depth_stencil)
1109 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1110 if (clear_flags)
1111 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1113 if (wined3d_settings.logo)
1114 device_load_logo(device, wined3d_settings.logo);
1116 return WINED3D_OK;
1118 err_out:
1119 heap_free(device->swapchains);
1120 device->swapchains = NULL;
1121 device->swapchain_count = 0;
1123 return hr;
1126 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1128 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1130 wined3d_sampler_decref(sampler);
1133 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1135 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1137 wined3d_rasterizer_state_decref(state);
1140 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1142 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1144 wined3d_blend_state_decref(blend_state);
1147 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1149 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1151 wined3d_depth_stencil_state_decref(state);
1154 void wined3d_device_uninit_3d(struct wined3d_device *device)
1156 struct wined3d_state *state = device->cs->c.state;
1157 struct wined3d_resource *resource, *cursor;
1158 struct wined3d_rendertarget_view *view;
1159 struct wined3d_texture *texture;
1161 TRACE("device %p.\n", device);
1163 if (!device->d3d_initialized)
1165 ERR("Called while 3D support was not initialised.\n");
1166 return;
1169 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1171 device->swapchain_count = 0;
1173 if ((texture = device->logo_texture))
1175 device->logo_texture = NULL;
1176 wined3d_texture_decref(texture);
1179 if ((texture = device->cursor_texture))
1181 device->cursor_texture = NULL;
1182 wined3d_texture_decref(texture);
1185 wined3d_device_context_emit_reset_state(&device->cs->c, true);
1186 state_cleanup(state);
1188 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1189 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1190 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1191 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1193 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1195 TRACE("Unloading resource %p.\n", resource);
1196 wined3d_cs_emit_unload_resource(device->cs, resource);
1199 device->adapter->adapter_ops->adapter_uninit_3d(device);
1200 device->d3d_initialized = FALSE;
1202 if ((view = device->auto_depth_stencil_view))
1204 device->auto_depth_stencil_view = NULL;
1205 if (wined3d_rendertarget_view_decref(view))
1206 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1209 if ((view = device->back_buffer_view))
1211 device->back_buffer_view = NULL;
1212 wined3d_rendertarget_view_decref(view);
1215 heap_free(device->swapchains);
1216 device->swapchains = NULL;
1218 wined3d_state_reset(state, &device->adapter->d3d_info);
1221 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1222 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1223 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1225 * There is no way to deactivate thread safety once it is enabled.
1227 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1229 TRACE("device %p.\n", device);
1231 /* For now just store the flag (needed in case of ddraw). */
1232 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1235 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1237 const struct wined3d_driver_info *driver_info;
1239 TRACE("device %p.\n", device);
1241 driver_info = &device->adapter->driver_info;
1243 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1244 wine_dbgstr_longlong(driver_info->vram_bytes),
1245 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1246 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1248 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1251 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1252 unsigned int idx, unsigned int *offset)
1254 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1256 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1258 WARN("Invalid stream output %u.\n", idx);
1259 return NULL;
1262 if (offset)
1263 *offset = context->state->stream_output[idx].offset;
1264 return context->state->stream_output[idx].buffer;
1267 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1268 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1270 const struct wined3d_stream_state *stream;
1272 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1273 context, stream_idx, buffer, offset, stride);
1275 if (stream_idx >= WINED3D_MAX_STREAMS)
1277 WARN("Stream index %u out of range.\n", stream_idx);
1278 return WINED3DERR_INVALIDCALL;
1281 stream = &context->state->streams[stream_idx];
1282 *buffer = stream->buffer;
1283 if (offset)
1284 *offset = stream->offset;
1285 *stride = stream->stride;
1287 return WINED3D_OK;
1290 static void wined3d_device_set_transform(struct wined3d_device *device,
1291 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1293 TRACE("device %p, state %s, matrix %p.\n",
1294 device, debug_d3dtstype(state), matrix);
1295 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1296 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1297 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1298 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1300 /* If the new matrix is the same as the current one,
1301 * we cut off any further processing. this seems to be a reasonable
1302 * optimization because as was noticed, some apps (warcraft3 for example)
1303 * tend towards setting the same matrix repeatedly for some reason.
1305 * From here on we assume that the new matrix is different, wherever it matters. */
1306 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1308 TRACE("The application is setting the same matrix over again.\n");
1309 return;
1312 device->cs->c.state->transforms[state] = *matrix;
1313 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
1316 static void wined3d_device_get_transform(const struct wined3d_device *device,
1317 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1319 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1321 *matrix = device->cs->c.state->transforms[state];
1324 /* Note lights are real special cases. Although the device caps state only
1325 * e.g. 8 are supported, you can reference any indexes you want as long as
1326 * that number max are enabled at any one point in time. Therefore since the
1327 * indices can be anything, we need a hashmap of them. However, this causes
1328 * stateblock problems. When capturing the state block, I duplicate the
1329 * hashmap, but when recording, just build a chain pretty much of commands to
1330 * be replayed. */
1331 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
1332 unsigned int light_idx, const struct wined3d_light *light)
1334 struct wined3d_light_info *object = NULL;
1335 float rho;
1337 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
1338 return;
1340 /* Initialize the object. */
1341 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1342 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1343 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1344 light_idx, light->type, debug_color(&light->diffuse),
1345 debug_color(&light->specular), debug_color(&light->ambient),
1346 light->position.x, light->position.y, light->position.z,
1347 light->direction.x, light->direction.y, light->direction.z,
1348 light->range, light->falloff, light->theta, light->phi);
1350 switch (light->type)
1352 case WINED3D_LIGHT_POINT:
1353 /* Position */
1354 object->position.x = light->position.x;
1355 object->position.y = light->position.y;
1356 object->position.z = light->position.z;
1357 object->position.w = 1.0f;
1358 object->cutoff = 180.0f;
1359 /* FIXME: Range */
1360 break;
1362 case WINED3D_LIGHT_DIRECTIONAL:
1363 /* Direction */
1364 object->direction.x = -light->direction.x;
1365 object->direction.y = -light->direction.y;
1366 object->direction.z = -light->direction.z;
1367 object->direction.w = 0.0f;
1368 object->exponent = 0.0f;
1369 object->cutoff = 180.0f;
1370 break;
1372 case WINED3D_LIGHT_SPOT:
1373 /* Position */
1374 object->position.x = light->position.x;
1375 object->position.y = light->position.y;
1376 object->position.z = light->position.z;
1377 object->position.w = 1.0f;
1379 /* Direction */
1380 object->direction.x = light->direction.x;
1381 object->direction.y = light->direction.y;
1382 object->direction.z = light->direction.z;
1383 object->direction.w = 0.0f;
1385 /* opengl-ish and d3d-ish spot lights use too different models
1386 * for the light "intensity" as a function of the angle towards
1387 * the main light direction, so we only can approximate very
1388 * roughly. However, spot lights are rather rarely used in games
1389 * (if ever used at all). Furthermore if still used, probably
1390 * nobody pays attention to such details. */
1391 if (!light->falloff)
1393 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1394 * equations have the falloff resp. exponent parameter as an
1395 * exponent, so the spot light lighting will always be 1.0 for
1396 * both of them, and we don't have to care for the rest of the
1397 * rather complex calculation. */
1398 object->exponent = 0.0f;
1400 else
1402 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1403 if (rho < 0.0001f)
1404 rho = 0.0001f;
1405 object->exponent = -0.3f / logf(cosf(rho / 2));
1408 if (object->exponent > 128.0f)
1409 object->exponent = 128.0f;
1411 object->cutoff = (float)(light->phi * 90 / M_PI);
1412 /* FIXME: Range */
1413 break;
1415 case WINED3D_LIGHT_PARALLELPOINT:
1416 object->position.x = light->position.x;
1417 object->position.y = light->position.y;
1418 object->position.z = light->position.z;
1419 object->position.w = 1.0f;
1420 break;
1422 default:
1423 FIXME("Unrecognized light type %#x.\n", light->type);
1426 wined3d_device_context_emit_set_light(context, object);
1429 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1431 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1432 struct wined3d_light_info *light_info;
1434 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1436 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1437 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1439 TRACE("Light enabled requested but light not defined, so defining one!\n");
1440 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
1442 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1444 FIXME("Adding default lights has failed dismally\n");
1445 return;
1449 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1450 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
1453 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1454 UINT plane_idx, const struct wined3d_vec4 *plane)
1456 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1458 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1460 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1462 TRACE("Application has requested clipplane this device doesn't support.\n");
1463 return WINED3DERR_INVALIDCALL;
1466 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1468 TRACE("Application is setting old values over, nothing to do.\n");
1469 return WINED3D_OK;
1472 clip_planes[plane_idx] = *plane;
1474 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
1476 return WINED3D_OK;
1479 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1480 const struct wined3d_clip_status *clip_status)
1482 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1484 if (!clip_status)
1485 return WINED3DERR_INVALIDCALL;
1487 return WINED3D_OK;
1490 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1491 struct wined3d_clip_status *clip_status)
1493 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1495 if (!clip_status)
1496 return WINED3DERR_INVALIDCALL;
1498 return WINED3D_OK;
1501 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1503 TRACE("device %p, material %p.\n", device, material);
1505 device->cs->c.state->material = *material;
1506 wined3d_device_context_emit_set_material(&device->cs->c, material);
1509 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1510 enum wined3d_format_id *format, unsigned int *offset)
1512 const struct wined3d_state *state = context->state;
1514 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1516 *format = state->index_format;
1517 if (offset)
1518 *offset = state->index_offset;
1519 return state->index_buffer;
1522 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
1524 TRACE("device %p, base_index %d.\n", device, base_index);
1526 device->cs->c.state->base_vertex_index = base_index;
1529 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1530 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1532 const struct wined3d_state *state = context->state;
1533 unsigned int count;
1535 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1537 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1538 if (count && viewports)
1539 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1540 if (viewport_count)
1541 *viewport_count = state->viewport_count;
1544 static void resolve_depth_buffer(struct wined3d_device *device)
1546 const struct wined3d_state *state = device->cs->c.state;
1547 struct wined3d_rendertarget_view *src_view;
1548 struct wined3d_resource *dst_resource;
1549 struct wined3d_texture *dst_texture;
1551 if (!(dst_texture = state->textures[0]))
1552 return;
1553 dst_resource = &dst_texture->resource;
1554 if (!dst_resource->format->depth_size)
1555 return;
1556 if (!(src_view = state->fb.depth_stencil))
1557 return;
1559 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1560 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1563 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1564 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1566 const struct wined3d_state *state = context->state;
1568 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1570 *blend_factor = state->blend_factor;
1571 *sample_mask = state->sample_mask;
1572 return state->blend_state;
1575 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1576 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1578 const struct wined3d_state *state = context->state;
1580 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1582 *stencil_ref = state->stencil_ref;
1583 return state->depth_stencil_state;
1586 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1587 struct wined3d_device_context *context)
1589 TRACE("context %p.\n", context);
1591 return context->state->rasterizer_state;
1594 static void wined3d_device_set_render_state(struct wined3d_device *device,
1595 enum wined3d_render_state state, DWORD value)
1597 if (state > WINEHIGHEST_RENDER_STATE)
1599 WARN("Unhandled render state %#x.\n", state);
1600 return;
1603 if (value == device->cs->c.state->render_states[state])
1604 TRACE("Application is setting the old value over, nothing to do.\n");
1605 else
1607 device->cs->c.state->render_states[state] = value;
1608 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
1611 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1613 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1614 resolve_depth_buffer(device);
1618 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1619 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1621 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1622 device, sampler_idx, debug_d3dsamplerstate(state), value);
1624 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1626 TRACE("Application is setting the old value over, nothing to do.\n");
1627 return;
1630 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1631 wined3d_device_context_emit_set_sampler_state(&device->cs->c, sampler_idx, state, value);
1634 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1635 unsigned int *rect_count, RECT *rects)
1637 const struct wined3d_state *state = context->state;
1638 unsigned int count;
1640 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1642 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1643 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1644 if (rect_count)
1645 *rect_count = state->scissor_rect_count;
1648 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1650 TRACE("context %p.\n", context);
1652 wined3d_device_context_lock(context);
1653 state_cleanup(context->state);
1654 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1655 wined3d_device_context_emit_reset_state(context, true);
1656 wined3d_device_context_unlock(context);
1659 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1661 const struct wined3d_light_info *light;
1662 unsigned int i, j;
1664 TRACE("context %p, state %p.\n", context, state);
1666 wined3d_device_context_lock(context);
1667 context->state = state;
1668 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1670 wined3d_device_context_emit_set_rendertarget_views(context, 0,
1671 ARRAY_SIZE(state->fb.render_targets), state->fb.render_targets);
1673 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1674 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1676 wined3d_device_context_emit_set_stream_outputs(context, state->stream_output);
1678 wined3d_device_context_emit_set_stream_sources(context, 0, WINED3D_MAX_STREAMS, state->streams);
1680 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1681 state->index_format, state->index_offset);
1683 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1685 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1687 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1688 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1689 wined3d_device_context_emit_set_samplers(context, i, 0, MAX_SAMPLER_OBJECTS, state->sampler[i]);
1690 wined3d_device_context_emit_set_shader_resource_views(context, i, 0,
1691 MAX_SHADER_RESOURCE_VIEWS, state->shader_resource_view[i]);
1694 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1695 wined3d_device_context_emit_set_unordered_access_views(context, i, 0, MAX_UNORDERED_ACCESS_VIEWS,
1696 state->unordered_access_view[i], NULL);
1698 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_F,
1699 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1700 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_I,
1701 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1702 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_B,
1703 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1705 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_F,
1706 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1707 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_I,
1708 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1709 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_B,
1710 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1712 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1714 wined3d_device_context_emit_set_texture(context, i, state->textures[i]);
1715 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1717 wined3d_device_context_emit_set_sampler_state(context, i, j, state->sampler_states[i][j]);
1721 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1723 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1725 wined3d_device_context_emit_set_texture_state(context, i, j, state->texture_states[i][j]);
1729 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1731 if (context->device->state_table[STATE_TRANSFORM(i)].representative)
1732 wined3d_device_context_emit_set_transform(context, i, state->transforms + i);
1735 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1737 wined3d_device_context_emit_set_clip_plane(context, i, state->clip_planes + i);
1740 wined3d_device_context_emit_set_material(context, &state->material);
1742 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1743 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1745 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1747 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1749 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
1750 wined3d_device_context_emit_set_light_enable(context, light->OriginalIndex, light->glIndex != -1);
1754 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1756 if (context->device->state_table[STATE_RENDER(i)].representative)
1757 wined3d_device_context_emit_set_render_state(context, i, state->render_states[i]);
1760 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1761 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1762 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1763 wined3d_device_context_unlock(context);
1766 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1768 TRACE("device %p.\n", device);
1770 return device->cs->c.state;
1773 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1775 TRACE("device %p.\n", device);
1777 return &device->cs->c;
1780 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1781 const struct wined3d_device_context *context)
1783 TRACE("context %p.\n", context);
1785 return context->state->vertex_declaration;
1788 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1789 enum wined3d_shader_type type, struct wined3d_shader *shader)
1791 struct wined3d_state *state = context->state;
1792 struct wined3d_shader *prev;
1794 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1796 wined3d_device_context_lock(context);
1797 prev = state->shader[type];
1798 if (shader == prev)
1799 goto out;
1801 if (shader)
1802 wined3d_shader_incref(shader);
1803 state->shader[type] = shader;
1804 wined3d_device_context_emit_set_shader(context, type, shader);
1805 if (prev)
1806 wined3d_shader_decref(prev);
1807 out:
1808 wined3d_device_context_unlock(context);
1811 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
1812 enum wined3d_shader_type type)
1814 TRACE("context %p, type %#x.\n", context, type);
1816 return context->state->shader[type];
1819 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
1820 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1821 const struct wined3d_constant_buffer_state *buffers)
1823 struct wined3d_state *state = context->state;
1824 unsigned int i;
1826 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
1828 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
1830 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
1831 return;
1834 wined3d_device_context_lock(context);
1835 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
1836 goto out;
1838 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
1839 for (i = 0; i < count; ++i)
1841 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
1842 struct wined3d_buffer *buffer = buffers[i].buffer;
1844 if (buffer)
1845 wined3d_buffer_incref(buffer);
1846 state->cb[type][start_idx + i] = buffers[i];
1847 if (prev)
1848 wined3d_buffer_decref(prev);
1850 out:
1851 wined3d_device_context_unlock(context);
1854 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1855 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1857 struct wined3d_state *state = context->state;
1858 struct wined3d_blend_state *prev;
1860 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1861 context, blend_state, blend_factor, sample_mask);
1863 wined3d_device_context_lock(context);
1864 prev = state->blend_state;
1865 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1866 && sample_mask == state->sample_mask)
1867 goto out;
1869 if (blend_state)
1870 wined3d_blend_state_incref(blend_state);
1871 state->blend_state = blend_state;
1872 state->blend_factor = *blend_factor;
1873 state->sample_mask = sample_mask;
1874 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1875 if (prev)
1876 wined3d_blend_state_decref(prev);
1877 out:
1878 wined3d_device_context_unlock(context);
1881 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1882 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1884 struct wined3d_state *state = context->state;
1885 struct wined3d_depth_stencil_state *prev;
1887 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1889 wined3d_device_context_lock(context);
1890 prev = state->depth_stencil_state;
1891 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1892 goto out;
1894 if (depth_stencil_state)
1895 wined3d_depth_stencil_state_incref(depth_stencil_state);
1896 state->depth_stencil_state = depth_stencil_state;
1897 state->stencil_ref = stencil_ref;
1898 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1899 if (prev)
1900 wined3d_depth_stencil_state_decref(prev);
1901 out:
1902 wined3d_device_context_unlock(context);
1905 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1906 struct wined3d_rasterizer_state *rasterizer_state)
1908 struct wined3d_state *state = context->state;
1909 struct wined3d_rasterizer_state *prev;
1911 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
1913 wined3d_device_context_lock(context);
1914 prev = state->rasterizer_state;
1915 if (prev == rasterizer_state)
1916 goto out;
1918 if (rasterizer_state)
1919 wined3d_rasterizer_state_incref(rasterizer_state);
1920 state->rasterizer_state = rasterizer_state;
1921 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
1922 if (prev)
1923 wined3d_rasterizer_state_decref(prev);
1924 out:
1925 wined3d_device_context_unlock(context);
1928 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1929 const struct wined3d_viewport *viewports)
1931 struct wined3d_state *state = context->state;
1932 unsigned int i;
1934 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
1936 for (i = 0; i < viewport_count; ++i)
1938 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1939 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1942 wined3d_device_context_lock(context);
1943 if (viewport_count)
1944 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
1945 else
1946 memset(state->viewports, 0, sizeof(state->viewports));
1947 state->viewport_count = viewport_count;
1949 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
1950 wined3d_device_context_unlock(context);
1953 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
1954 const RECT *rects)
1956 struct wined3d_state *state = context->state;
1957 unsigned int i;
1959 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
1961 for (i = 0; i < rect_count; ++i)
1963 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1966 wined3d_device_context_lock(context);
1967 if (state->scissor_rect_count == rect_count
1968 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
1970 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
1971 goto out;
1974 if (rect_count)
1975 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
1976 else
1977 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
1978 state->scissor_rect_count = rect_count;
1980 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
1981 out:
1982 wined3d_device_context_unlock(context);
1985 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
1986 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1987 struct wined3d_shader_resource_view *const *const views)
1989 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
1990 struct wined3d_state *state = context->state;
1991 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
1992 unsigned int i;
1994 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
1996 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
1998 WARN("Invalid view index %u, count %u.\n", start_idx, count);
1999 return;
2002 wined3d_device_context_lock(context);
2003 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
2004 goto out;
2006 memcpy(real_views, views, count * sizeof(*views));
2008 for (i = 0; i < count; ++i)
2010 struct wined3d_shader_resource_view *view = real_views[i];
2012 if (view && (wined3d_is_srv_rtv_bound(state, view)
2013 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2015 WARN("Application is trying to bind resource which is attached as render target.\n");
2016 real_views[i] = NULL;
2020 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
2021 for (i = 0; i < count; ++i)
2023 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
2024 struct wined3d_shader_resource_view *view = real_views[i];
2026 if (view)
2028 wined3d_shader_resource_view_incref(view);
2029 wined3d_srv_bind_count_inc(view);
2032 state->shader_resource_view[type][start_idx + i] = view;
2033 if (prev)
2035 wined3d_srv_bind_count_dec(prev);
2036 wined3d_shader_resource_view_decref(prev);
2039 out:
2040 wined3d_device_context_unlock(context);
2043 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
2044 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
2046 struct wined3d_state *state = context->state;
2047 unsigned int i;
2049 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
2051 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
2053 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
2054 return;
2057 wined3d_device_context_lock(context);
2058 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2059 goto out;
2061 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2062 for (i = 0; i < count; ++i)
2064 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2065 struct wined3d_sampler *sampler = samplers[i];
2067 if (sampler)
2068 wined3d_sampler_incref(sampler);
2069 state->sampler[type][start_idx + i] = sampler;
2070 if (prev)
2071 wined3d_sampler_decref(prev);
2073 out:
2074 wined3d_device_context_unlock(context);
2077 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2078 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2079 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2081 struct wined3d_state *state = context->state;
2082 unsigned int i;
2084 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2085 context, pipeline, start_idx, count, uavs, initial_counts);
2087 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2089 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2090 return;
2093 wined3d_device_context_lock(context);
2094 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2095 goto out;
2097 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2098 for (i = 0; i < count; ++i)
2100 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2101 struct wined3d_unordered_access_view *uav = uavs[i];
2103 if (uav)
2104 wined3d_unordered_access_view_incref(uav);
2105 state->unordered_access_view[pipeline][start_idx + i] = uav;
2106 if (prev)
2107 wined3d_unordered_access_view_decref(prev);
2109 out:
2110 wined3d_device_context_unlock(context);
2113 void CDECL wined3d_device_context_set_render_targets_and_unordered_access_views(struct wined3d_device_context *context,
2114 unsigned int rtv_count, struct wined3d_rendertarget_view *const *render_target_views,
2115 struct wined3d_rendertarget_view *depth_stencil_view, UINT uav_count,
2116 struct wined3d_unordered_access_view *const *unordered_access_views, const unsigned int *initial_counts)
2118 wined3d_device_context_lock(context);
2119 if (rtv_count != ~0u)
2121 if (depth_stencil_view && !(depth_stencil_view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2123 WARN("View resource %p has incompatible %s bind flags.\n",
2124 depth_stencil_view->resource, wined3d_debug_bind_flags(depth_stencil_view->resource->bind_flags));
2125 goto out;
2128 if (FAILED(wined3d_device_context_set_rendertarget_views(context, 0, rtv_count,
2129 render_target_views, FALSE)))
2130 goto out;
2132 wined3d_device_context_set_depth_stencil_view(context, depth_stencil_view);
2135 if (uav_count != ~0u)
2137 wined3d_device_context_set_unordered_access_views(context, WINED3D_PIPELINE_GRAPHICS, 0, uav_count,
2138 unordered_access_views, initial_counts);
2140 out:
2141 wined3d_device_context_unlock(context);
2144 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2145 const struct wined3d_rendertarget_view *view, BOOL dsv)
2147 const struct wined3d_state *state = context->state;
2148 const struct wined3d_resource *resource;
2150 if (!view)
2151 return;
2152 resource = view->resource;
2154 if (resource->srv_bind_count_device)
2156 const struct wined3d_shader_resource_view *srv;
2157 unsigned int i, j;
2159 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2161 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2163 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2164 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2165 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2167 static struct wined3d_shader_resource_view *const null_srv;
2169 WARN("Application sets bound resource as render target.\n");
2170 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2177 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2178 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2180 struct wined3d_state *state = context->state;
2181 unsigned int i, max_rt_count;
2183 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2184 context, start_idx, count, views, set_viewport);
2186 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2187 if (start_idx >= max_rt_count)
2189 WARN("Only %u render targets are supported.\n", max_rt_count);
2190 return WINED3DERR_INVALIDCALL;
2192 count = min(count, max_rt_count - start_idx);
2194 for (i = 0; i < count; ++i)
2196 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2198 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2199 return WINED3DERR_INVALIDCALL;
2203 wined3d_device_context_lock(context);
2204 /* Set the viewport and scissor rectangles, if requested. Tests show that
2205 * stateblock recording is ignored, the change goes directly into the
2206 * primary stateblock. */
2207 if (!start_idx && set_viewport)
2209 state->viewports[0].x = 0;
2210 state->viewports[0].y = 0;
2211 state->viewports[0].width = views[0]->width;
2212 state->viewports[0].height = views[0]->height;
2213 state->viewports[0].min_z = 0.0f;
2214 state->viewports[0].max_z = 1.0f;
2215 state->viewport_count = 1;
2216 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2218 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2219 state->scissor_rect_count = 1;
2220 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2223 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2224 goto out;
2226 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2227 for (i = 0; i < count; ++i)
2229 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2230 struct wined3d_rendertarget_view *view = views[i];
2232 if (view)
2234 wined3d_rendertarget_view_incref(view);
2235 wined3d_rtv_bind_count_inc(view);
2237 state->fb.render_targets[start_idx + i] = view;
2238 /* Release after the assignment, to prevent device_resource_released()
2239 * from seeing the resource as still in use. */
2240 if (prev)
2242 wined3d_rtv_bind_count_dec(prev);
2243 wined3d_rendertarget_view_decref(prev);
2246 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2248 out:
2249 wined3d_device_context_unlock(context);
2250 return WINED3D_OK;
2253 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2254 struct wined3d_rendertarget_view *view)
2256 struct wined3d_fb_state *fb = &context->state->fb;
2257 struct wined3d_rendertarget_view *prev;
2259 TRACE("context %p, view %p.\n", context, view);
2261 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2263 WARN("View resource %p has incompatible %s bind flags.\n",
2264 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2265 return WINED3DERR_INVALIDCALL;
2268 wined3d_device_context_lock(context);
2269 prev = fb->depth_stencil;
2270 if (prev == view)
2272 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2273 goto out;
2276 if ((fb->depth_stencil = view))
2277 wined3d_rendertarget_view_incref(view);
2278 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2279 if (prev)
2280 wined3d_rendertarget_view_decref(prev);
2281 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2282 out:
2283 wined3d_device_context_unlock(context);
2284 return WINED3D_OK;
2287 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2288 struct wined3d_query *predicate, BOOL value)
2290 struct wined3d_state *state = context->state;
2291 struct wined3d_query *prev;
2293 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2295 wined3d_device_context_lock(context);
2296 prev = state->predicate;
2297 if (predicate)
2299 FIXME("Predicated rendering not implemented.\n");
2300 wined3d_query_incref(predicate);
2302 state->predicate = predicate;
2303 state->predicate_value = value;
2304 wined3d_device_context_emit_set_predication(context, predicate, value);
2305 if (prev)
2306 wined3d_query_decref(prev);
2307 wined3d_device_context_unlock(context);
2310 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2311 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2313 struct wined3d_state *state = context->state;
2314 unsigned int i;
2316 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2318 if (start_idx >= WINED3D_MAX_STREAMS)
2320 WARN("Start index %u is out of range.\n", start_idx);
2321 return WINED3DERR_INVALIDCALL;
2324 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2326 for (i = 0; i < count; ++i)
2328 if (streams[i].offset & 0x3)
2330 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2331 return WINED3DERR_INVALIDCALL;
2335 wined3d_device_context_lock(context);
2336 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2337 goto out;
2339 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2340 for (i = 0; i < count; ++i)
2342 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2343 struct wined3d_buffer *buffer = streams[i].buffer;
2345 state->streams[start_idx + i] = streams[i];
2347 if (buffer)
2348 wined3d_buffer_incref(buffer);
2349 if (prev)
2350 wined3d_buffer_decref(prev);
2352 out:
2353 wined3d_device_context_unlock(context);
2354 return WINED3D_OK;
2357 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2358 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2360 struct wined3d_state *state = context->state;
2361 enum wined3d_format_id prev_format;
2362 struct wined3d_buffer *prev_buffer;
2363 unsigned int prev_offset;
2365 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2366 context, buffer, debug_d3dformat(format_id), offset);
2368 wined3d_device_context_lock(context);
2369 prev_buffer = state->index_buffer;
2370 prev_format = state->index_format;
2371 prev_offset = state->index_offset;
2373 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2374 goto out;
2376 if (buffer)
2377 wined3d_buffer_incref(buffer);
2378 state->index_buffer = buffer;
2379 state->index_format = format_id;
2380 state->index_offset = offset;
2381 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2382 if (prev_buffer)
2383 wined3d_buffer_decref(prev_buffer);
2384 out:
2385 wined3d_device_context_unlock(context);
2388 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2389 struct wined3d_vertex_declaration *declaration)
2391 struct wined3d_state *state = context->state;
2392 struct wined3d_vertex_declaration *prev;
2394 TRACE("context %p, declaration %p.\n", context, declaration);
2396 wined3d_device_context_lock(context);
2397 prev = state->vertex_declaration;
2398 if (declaration == prev)
2399 goto out;
2401 if (declaration)
2402 wined3d_vertex_declaration_incref(declaration);
2403 state->vertex_declaration = declaration;
2404 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2405 if (prev)
2406 wined3d_vertex_declaration_decref(prev);
2407 out:
2408 wined3d_device_context_unlock(context);
2411 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2412 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2414 struct wined3d_state *state = context->state;
2415 unsigned int i;
2417 TRACE("context %p, outputs %p.\n", context, outputs);
2419 wined3d_device_context_lock(context);
2420 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2421 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2423 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2424 struct wined3d_buffer *buffer = outputs[i].buffer;
2426 if (buffer)
2427 wined3d_buffer_incref(buffer);
2428 state->stream_output[i] = outputs[i];
2429 if (prev_buffer)
2430 wined3d_buffer_decref(prev_buffer);
2432 wined3d_device_context_unlock(context);
2435 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2436 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2438 struct wined3d_state *state = context->state;
2440 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2441 context, start_vertex, vertex_count, start_instance, instance_count);
2443 wined3d_device_context_lock(context);
2444 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2445 0, start_vertex, vertex_count, start_instance, instance_count, false);
2446 wined3d_device_context_unlock(context);
2449 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2450 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2452 struct wined3d_state *state = context->state;
2454 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2455 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2457 wined3d_device_context_lock(context);
2458 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2459 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2460 wined3d_device_context_unlock(context);
2463 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2464 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2466 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2468 if (idx >= MAX_CONSTANT_BUFFERS)
2470 WARN("Invalid constant buffer index %u.\n", idx);
2471 return;
2474 *state = context->state->cb[shader_type][idx];
2477 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2478 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2480 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2482 WARN("Invalid view index %u.\n", idx);
2483 return NULL;
2486 return context->state->shader_resource_view[shader_type][idx];
2489 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2490 enum wined3d_shader_type shader_type, unsigned int idx)
2492 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2494 if (idx >= MAX_SAMPLER_OBJECTS)
2496 WARN("Invalid sampler index %u.\n", idx);
2497 return NULL;
2500 return context->state->sampler[shader_type][idx];
2503 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2504 unsigned int start_idx, unsigned int count, const BOOL *constants)
2506 unsigned int i;
2508 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2509 device, start_idx, count, constants);
2511 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2512 if (TRACE_ON(d3d))
2514 for (i = 0; i < count; ++i)
2515 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2518 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2521 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2522 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2524 unsigned int i;
2526 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2527 device, start_idx, count, constants);
2529 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2530 if (TRACE_ON(d3d))
2532 for (i = 0; i < count; ++i)
2533 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2536 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2539 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2540 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2542 unsigned int i;
2544 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2545 device, start_idx, count, constants);
2547 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2548 if (TRACE_ON(d3d))
2550 for (i = 0; i < count; ++i)
2551 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2554 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2557 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2558 unsigned int start_idx, unsigned int count, const BOOL *constants)
2560 unsigned int i;
2562 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2563 device, start_idx, count, constants);
2565 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2566 if (TRACE_ON(d3d))
2568 for (i = 0; i < count; ++i)
2569 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2572 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2575 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2576 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2578 unsigned int i;
2580 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2581 device, start_idx, count, constants);
2583 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2584 if (TRACE_ON(d3d))
2586 for (i = 0; i < count; ++i)
2587 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2590 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2593 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2594 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2596 unsigned int i;
2598 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2599 device, start_idx, count, constants);
2601 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2602 if (TRACE_ON(d3d))
2604 for (i = 0; i < count; ++i)
2605 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2608 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2611 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2612 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2614 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2616 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2618 WARN("Invalid UAV index %u.\n", idx);
2619 return NULL;
2622 return context->state->unordered_access_view[pipeline][idx];
2625 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2627 unsigned int i;
2629 if (!latency)
2630 latency = 3;
2632 device->max_frame_latency = latency;
2633 for (i = 0; i < device->swapchain_count; ++i)
2634 swapchain_set_max_frame_latency(device->swapchains[i], device);
2637 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2639 return device->max_frame_latency;
2642 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2644 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2645 unsigned int i, size = 0;
2647 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2648 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2649 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2650 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2651 switch (fvf & WINED3DFVF_POSITION_MASK)
2653 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2654 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2655 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2656 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2657 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2658 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2659 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2660 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2661 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2663 for (i = 0; i < texcoord_count; ++i)
2665 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2668 return size;
2671 static void wined3d_format_get_colour(const struct wined3d_format *format,
2672 const void *data, struct wined3d_color *colour)
2674 float *output = &colour->r;
2675 const uint32_t *u32_data;
2676 const uint16_t *u16_data;
2677 const float *f32_data;
2678 unsigned int i;
2680 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2681 static unsigned int warned;
2683 switch (format->id)
2685 case WINED3DFMT_B8G8R8A8_UNORM:
2686 u32_data = data;
2687 wined3d_color_from_d3dcolor(colour, *u32_data);
2688 break;
2690 case WINED3DFMT_R8G8B8A8_UNORM:
2691 u32_data = data;
2692 colour->r = (*u32_data & 0xffu) / 255.0f;
2693 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2694 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2695 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2696 break;
2698 case WINED3DFMT_R16G16_UNORM:
2699 case WINED3DFMT_R16G16B16A16_UNORM:
2700 u16_data = data;
2701 *colour = default_colour;
2702 for (i = 0; i < format->component_count; ++i)
2703 output[i] = u16_data[i] / 65535.0f;
2704 break;
2706 case WINED3DFMT_R32_FLOAT:
2707 case WINED3DFMT_R32G32_FLOAT:
2708 case WINED3DFMT_R32G32B32_FLOAT:
2709 case WINED3DFMT_R32G32B32A32_FLOAT:
2710 f32_data = data;
2711 *colour = default_colour;
2712 for (i = 0; i < format->component_count; ++i)
2713 output[i] = f32_data[i];
2714 break;
2716 default:
2717 *colour = default_colour;
2718 if (!warned++)
2719 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2720 break;
2724 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2725 const struct wined3d_color *material_colour, unsigned int index,
2726 const struct wined3d_stream_info *stream_info)
2728 const struct wined3d_stream_info_element *element = NULL;
2730 switch (mcs)
2732 case WINED3D_MCS_MATERIAL:
2733 *colour = *material_colour;
2734 return;
2736 case WINED3D_MCS_COLOR1:
2737 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2739 colour->r = colour->g = colour->b = colour->a = 1.0f;
2740 return;
2742 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2743 break;
2745 case WINED3D_MCS_COLOR2:
2746 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2748 colour->r = colour->g = colour->b = colour->a = 0.0f;
2749 return;
2751 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2752 break;
2754 default:
2755 colour->r = colour->g = colour->b = colour->a = 0.0f;
2756 ERR("Invalid material colour source %#x.\n", mcs);
2757 return;
2760 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
2763 static float wined3d_clamp(float value, float min_value, float max_value)
2765 return value < min_value ? min_value : value > max_value ? max_value : value;
2768 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2770 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
2773 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2775 v0->x -= v1->x;
2776 v0->y -= v1->y;
2777 v0->z -= v1->z;
2780 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
2782 v->x *= s;
2783 v->y *= s;
2784 v->z *= s;
2787 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
2789 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
2791 if (isfinite(rnorm))
2792 wined3d_vec3_scale(v, rnorm);
2795 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
2796 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
2798 struct wined3d_vec3 tmp;
2800 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
2801 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
2802 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
2804 *dst = tmp;
2807 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
2808 float min_value, float max_value)
2810 dst->r = wined3d_clamp(src->r, min_value, max_value);
2811 dst->g = wined3d_clamp(src->g, min_value, max_value);
2812 dst->b = wined3d_clamp(src->b, min_value, max_value);
2813 dst->a = wined3d_clamp(src->a, min_value, max_value);
2816 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
2818 dst->r += src->r * c;
2819 dst->g += src->g * c;
2820 dst->b += src->b * c;
2823 static void init_transformed_lights(struct lights_settings *ls,
2824 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
2826 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
2827 const struct wined3d_light_info *light_info;
2828 struct light_transformed *light;
2829 struct wined3d_vec4 vec4;
2830 unsigned int light_count;
2831 unsigned int i, index;
2833 memset(ls, 0, sizeof(*ls));
2835 ls->lighting = !!compute_lighting;
2836 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
2837 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
2838 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
2839 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
2840 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
2841 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
2843 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
2844 return;
2846 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
2847 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
2849 if (!compute_lighting)
2850 return;
2852 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
2854 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
2855 ls->legacy_lighting = !!legacy_lighting;
2856 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
2857 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
2859 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
2861 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
2863 if (!light_info->enabled)
2864 continue;
2866 switch (light_info->OriginalParms.type)
2868 case WINED3D_LIGHT_DIRECTIONAL:
2869 ++ls->directional_light_count;
2870 break;
2872 case WINED3D_LIGHT_POINT:
2873 ++ls->point_light_count;
2874 break;
2876 case WINED3D_LIGHT_SPOT:
2877 ++ls->spot_light_count;
2878 break;
2880 case WINED3D_LIGHT_PARALLELPOINT:
2881 ++ls->parallel_point_light_count;
2882 break;
2884 default:
2885 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
2886 continue;
2888 lights[index++] = light_info;
2889 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
2890 break;
2894 light_count = index;
2895 for (i = 0, index = 0; i < light_count; ++i)
2897 light_info = lights[i];
2898 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
2899 continue;
2901 light = &ls->lights[index];
2902 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2903 light->direction = *(struct wined3d_vec3 *)&vec4;
2904 wined3d_vec3_normalise(&light->direction);
2906 light->diffuse = light_info->OriginalParms.diffuse;
2907 light->ambient = light_info->OriginalParms.ambient;
2908 light->specular = light_info->OriginalParms.specular;
2909 ++index;
2912 for (i = 0; i < light_count; ++i)
2914 light_info = lights[i];
2915 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
2916 continue;
2918 light = &ls->lights[index];
2920 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2921 light->range = light_info->OriginalParms.range;
2922 light->c_att = light_info->OriginalParms.attenuation0;
2923 light->l_att = light_info->OriginalParms.attenuation1;
2924 light->q_att = light_info->OriginalParms.attenuation2;
2926 light->diffuse = light_info->OriginalParms.diffuse;
2927 light->ambient = light_info->OriginalParms.ambient;
2928 light->specular = light_info->OriginalParms.specular;
2929 ++index;
2932 for (i = 0; i < light_count; ++i)
2934 light_info = lights[i];
2935 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
2936 continue;
2938 light = &ls->lights[index];
2940 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2941 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2942 light->direction = *(struct wined3d_vec3 *)&vec4;
2943 wined3d_vec3_normalise(&light->direction);
2944 light->range = light_info->OriginalParms.range;
2945 light->falloff = light_info->OriginalParms.falloff;
2946 light->c_att = light_info->OriginalParms.attenuation0;
2947 light->l_att = light_info->OriginalParms.attenuation1;
2948 light->q_att = light_info->OriginalParms.attenuation2;
2949 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
2950 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
2952 light->diffuse = light_info->OriginalParms.diffuse;
2953 light->ambient = light_info->OriginalParms.ambient;
2954 light->specular = light_info->OriginalParms.specular;
2955 ++index;
2958 for (i = 0; i < light_count; ++i)
2960 light_info = lights[i];
2961 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
2962 continue;
2964 light = &ls->lights[index];
2966 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2967 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
2968 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
2969 light->diffuse = light_info->OriginalParms.diffuse;
2970 light->ambient = light_info->OriginalParms.ambient;
2971 light->specular = light_info->OriginalParms.specular;
2972 ++index;
2976 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
2977 const struct wined3d_vec3 *dir, float att, float material_shininess,
2978 const struct wined3d_vec3 *normal_transformed,
2979 const struct wined3d_vec3 *position_transformed_normalised,
2980 const struct light_transformed *light, const struct lights_settings *ls)
2982 struct wined3d_vec3 vec3;
2983 float t, c;
2985 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
2986 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
2988 vec3 = *dir;
2989 if (ls->localviewer)
2990 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
2991 else
2992 vec3.z -= 1.0f;
2993 wined3d_vec3_normalise(&vec3);
2994 t = wined3d_vec3_dot(normal_transformed, &vec3);
2995 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
2996 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
2997 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
3000 static void light_set_vertex_data(struct lights_settings *ls,
3001 const struct wined3d_vec4 *position)
3003 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3004 return;
3006 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3007 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3010 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3011 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3012 float material_shininess)
3014 struct wined3d_vec3 position_transformed_normalised;
3015 struct wined3d_vec3 normal_transformed = {0.0f};
3016 const struct light_transformed *light;
3017 struct wined3d_vec3 dir, dst;
3018 unsigned int i, index;
3019 float att;
3021 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3022 wined3d_vec3_normalise(&position_transformed_normalised);
3024 if (normal)
3026 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3027 if (ls->normalise)
3028 wined3d_vec3_normalise(&normal_transformed);
3031 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3032 *specular = *diffuse;
3033 *ambient = ls->ambient_light;
3035 index = 0;
3036 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3038 light = &ls->lights[index];
3040 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3041 if (normal)
3042 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3043 &normal_transformed, &position_transformed_normalised, light, ls);
3046 for (i = 0; i < ls->point_light_count; ++i, ++index)
3048 light = &ls->lights[index];
3049 dir.x = light->position.x - ls->position_transformed.x;
3050 dir.y = light->position.y - ls->position_transformed.y;
3051 dir.z = light->position.z - ls->position_transformed.z;
3053 dst.z = wined3d_vec3_dot(&dir, &dir);
3054 dst.y = sqrtf(dst.z);
3055 dst.x = 1.0f;
3056 if (ls->legacy_lighting)
3058 dst.y = (light->range - dst.y) / light->range;
3059 if (!(dst.y > 0.0f))
3060 continue;
3061 dst.z = dst.y * dst.y;
3063 else
3065 if (!(dst.y <= light->range))
3066 continue;
3068 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3069 if (!ls->legacy_lighting)
3070 att = 1.0f / att;
3072 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3073 if (normal)
3075 wined3d_vec3_normalise(&dir);
3076 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3077 &normal_transformed, &position_transformed_normalised, light, ls);
3081 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3083 float t;
3085 light = &ls->lights[index];
3087 dir.x = light->position.x - ls->position_transformed.x;
3088 dir.y = light->position.y - ls->position_transformed.y;
3089 dir.z = light->position.z - ls->position_transformed.z;
3091 dst.z = wined3d_vec3_dot(&dir, &dir);
3092 dst.y = sqrtf(dst.z);
3093 dst.x = 1.0f;
3095 if (ls->legacy_lighting)
3097 dst.y = (light->range - dst.y) / light->range;
3098 if (!(dst.y > 0.0f))
3099 continue;
3100 dst.z = dst.y * dst.y;
3102 else
3104 if (!(dst.y <= light->range))
3105 continue;
3107 wined3d_vec3_normalise(&dir);
3108 t = -wined3d_vec3_dot(&dir, &light->direction);
3109 if (t > light->cos_htheta)
3110 att = 1.0f;
3111 else if (t <= light->cos_hphi)
3112 att = 0.0f;
3113 else
3114 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3116 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3117 if (ls->legacy_lighting)
3118 att *= t;
3119 else
3120 att /= t;
3122 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3124 if (normal)
3125 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3126 &normal_transformed, &position_transformed_normalised, light, ls);
3129 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3131 light = &ls->lights[index];
3133 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3134 if (normal)
3135 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3136 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3140 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3142 switch (ls->fog_mode)
3144 case WINED3D_FOG_NONE:
3145 return fog_coord;
3146 case WINED3D_FOG_LINEAR:
3147 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3148 case WINED3D_FOG_EXP:
3149 return expf(-fog_coord * ls->fog_density);
3150 case WINED3D_FOG_EXP2:
3151 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3152 default:
3153 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3154 return 0.0f;
3158 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3160 float fog_coord;
3162 if (ls->fog_mode == WINED3D_FOG_NONE)
3163 return;
3165 switch (ls->fog_coord_mode)
3167 case WINED3D_FFP_VS_FOG_RANGE:
3168 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3169 (const struct wined3d_vec3 *)&ls->position_transformed));
3170 break;
3172 case WINED3D_FFP_VS_FOG_DEPTH:
3173 fog_coord = fabsf(ls->position_transformed.z);
3174 break;
3176 default:
3177 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3178 return;
3180 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3183 /* Context activation is done by the caller. */
3184 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3185 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3186 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3188 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3189 const struct wined3d_color *material_specular_state_colour;
3190 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3191 const struct wined3d_state *state = device->cs->c.state;
3192 const struct wined3d_format *output_colour_format;
3193 static const struct wined3d_color black;
3194 struct wined3d_map_desc map_desc;
3195 struct wined3d_box box = {0};
3196 struct wined3d_viewport vp;
3197 unsigned int texture_count;
3198 struct lights_settings ls;
3199 unsigned int vertex_size;
3200 BOOL do_clip, lighting;
3201 float min_z, max_z;
3202 unsigned int i;
3203 BYTE *dest_ptr;
3204 HRESULT hr;
3206 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3208 ERR("Source has no position mask.\n");
3209 return WINED3DERR_INVALIDCALL;
3212 if (state->render_states[WINED3D_RS_CLIPPING])
3214 static BOOL warned = FALSE;
3216 * The clipping code is not quite correct. Some things need
3217 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3218 * so disable clipping for now.
3219 * (The graphics in Half-Life are broken, and my processvertices
3220 * test crashes with IDirect3DDevice3)
3221 do_clip = TRUE;
3223 do_clip = FALSE;
3224 if (!warned)
3226 warned = TRUE;
3227 FIXME("Clipping is broken and disabled for now\n");
3230 else
3231 do_clip = FALSE;
3233 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3234 box.left = dwDestIndex * vertex_size;
3235 box.right = box.left + dwCount * vertex_size;
3236 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3238 WARN("Failed to map buffer, hr %#x.\n", hr);
3239 return hr;
3241 dest_ptr = map_desc.data;
3243 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3244 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3245 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3247 TRACE("View mat:\n");
3248 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3249 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3250 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3251 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3253 TRACE("Proj mat:\n");
3254 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3255 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3256 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3257 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3259 TRACE("World mat:\n");
3260 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3261 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3262 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3263 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3265 /* Get the viewport */
3266 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3267 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3268 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3270 multiply_matrix(&mat,&view_mat,&world_mat);
3271 multiply_matrix(&mat,&proj_mat,&mat);
3273 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3275 lighting = state->render_states[WINED3D_RS_LIGHTING]
3276 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3277 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3278 &ambient_source, &specular_source, state, stream_info);
3279 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3280 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3281 ? &state->material.specular : &black;
3282 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3283 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3285 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3287 for (i = 0; i < dwCount; ++i)
3289 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3290 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3291 struct wined3d_color ambient, diffuse, specular;
3292 struct wined3d_vec4 position;
3293 unsigned int tex_index;
3295 position.x = p[0];
3296 position.y = p[1];
3297 position.z = p[2];
3298 position.w = 1.0f;
3300 light_set_vertex_data(&ls, &position);
3302 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3303 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3304 /* The position first */
3305 float x, y, z, rhw;
3306 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3308 /* Multiplication with world, view and projection matrix. */
3309 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3310 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3311 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3312 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3314 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3316 /* WARNING: The following things are taken from d3d7 and were not yet checked
3317 * against d3d8 or d3d9!
3320 /* Clipping conditions: From msdn
3322 * A vertex is clipped if it does not match the following requirements
3323 * -rhw < x <= rhw
3324 * -rhw < y <= rhw
3325 * 0 < z <= rhw
3326 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3328 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3329 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3333 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3334 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3336 /* "Normal" viewport transformation (not clipped)
3337 * 1) The values are divided by rhw
3338 * 2) The y axis is negative, so multiply it with -1
3339 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3340 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3341 * 4) Multiply x with Width/2 and add Width/2
3342 * 5) The same for the height
3343 * 6) Add the viewpoint X and Y to the 2D coordinates and
3344 * The minimum Z value to z
3345 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3347 * Well, basically it's simply a linear transformation into viewport
3348 * coordinates
3351 x /= rhw;
3352 y /= rhw;
3353 z /= rhw;
3355 y *= -1;
3357 x *= vp.width / 2;
3358 y *= vp.height / 2;
3359 z *= max_z - min_z;
3361 x += vp.width / 2 + vp.x;
3362 y += vp.height / 2 + vp.y;
3363 z += min_z;
3365 rhw = 1 / rhw;
3366 } else {
3367 /* That vertex got clipped
3368 * Contrary to OpenGL it is not dropped completely, it just
3369 * undergoes a different calculation.
3371 TRACE("Vertex got clipped\n");
3372 x += rhw;
3373 y += rhw;
3375 x /= 2;
3376 y /= 2;
3378 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3379 * outside of the main vertex buffer memory. That needs some more
3380 * investigation...
3384 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3387 ( (float *) dest_ptr)[0] = x;
3388 ( (float *) dest_ptr)[1] = y;
3389 ( (float *) dest_ptr)[2] = z;
3390 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3392 dest_ptr += 3 * sizeof(float);
3394 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3395 dest_ptr += sizeof(float);
3398 if (dst_fvf & WINED3DFVF_PSIZE)
3399 dest_ptr += sizeof(DWORD);
3401 if (dst_fvf & WINED3DFVF_NORMAL)
3403 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3404 const float *normal = (const float *)(element->data.addr + i * element->stride);
3405 /* AFAIK this should go into the lighting information */
3406 FIXME("Didn't expect the destination to have a normal\n");
3407 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3410 if (lighting)
3412 const struct wined3d_stream_info_element *element;
3413 struct wined3d_vec3 *normal;
3415 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3417 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3418 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3420 else
3422 normal = NULL;
3424 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3425 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3428 if (dst_fvf & WINED3DFVF_DIFFUSE)
3430 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3432 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3433 &state->material.diffuse, i, stream_info);
3435 if (lighting)
3437 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3438 &state->material.ambient, i, stream_info);
3439 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3440 &state->material.emissive, i, stream_info);
3442 diffuse_colour.r = ambient.r * material_ambient.r
3443 + diffuse.r * material_diffuse.r + material_emissive.r;
3444 diffuse_colour.g = ambient.g * material_ambient.g
3445 + diffuse.g * material_diffuse.g + material_emissive.g;
3446 diffuse_colour.b = ambient.b * material_ambient.b
3447 + diffuse.b * material_diffuse.b + material_emissive.b;
3448 diffuse_colour.a = material_diffuse.a;
3450 else
3452 diffuse_colour = material_diffuse;
3454 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3455 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3456 dest_ptr += sizeof(DWORD);
3459 if (dst_fvf & WINED3DFVF_SPECULAR)
3461 struct wined3d_color material_specular, specular_colour;
3463 wined3d_colour_from_mcs(&material_specular, specular_source,
3464 material_specular_state_colour, i, stream_info);
3466 if (lighting)
3468 specular_colour.r = specular.r * material_specular.r;
3469 specular_colour.g = specular.g * material_specular.g;
3470 specular_colour.b = specular.b * material_specular.b;
3471 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3473 else
3475 specular_colour = material_specular;
3477 update_fog_factor(&specular_colour.a, &ls);
3478 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3479 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3480 dest_ptr += sizeof(DWORD);
3483 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3485 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3486 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3487 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3489 ERR("No source texture, but destination requests one\n");
3490 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3492 else
3494 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3499 wined3d_resource_unmap(&dest->resource, 0);
3501 return WINED3D_OK;
3503 #undef copy_and_next
3505 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3506 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3507 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3509 struct wined3d_state *state = device->cs->c.state;
3510 struct wined3d_stream_info stream_info;
3511 struct wined3d_resource *resource;
3512 struct wined3d_box box = {0};
3513 struct wined3d_shader *vs;
3514 unsigned int i, j;
3515 HRESULT hr;
3516 WORD map;
3518 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3519 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3520 device, src_start_idx, dst_idx, vertex_count,
3521 dst_buffer, declaration, flags, dst_fvf);
3523 if (declaration)
3524 FIXME("Output vertex declaration not implemented yet.\n");
3526 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3527 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3528 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3529 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3531 /* We can't convert FROM a VBO, and vertex buffers used to source into
3532 * process_vertices() are unlikely to ever be used for drawing. Release
3533 * VBOs in those buffers and fix up the stream_info structure.
3535 * Also apply the start index. */
3536 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3538 struct wined3d_stream_info_element *e;
3539 struct wined3d_map_desc map_desc;
3541 if (!(map & 1))
3542 continue;
3544 e = &stream_info.elements[i];
3545 resource = &state->streams[e->stream_idx].buffer->resource;
3546 box.left = src_start_idx * e->stride;
3547 box.right = box.left + vertex_count * e->stride;
3548 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3550 ERR("Failed to map resource.\n");
3551 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3553 if (!(map & 1))
3554 continue;
3556 e = &stream_info.elements[j];
3557 resource = &state->streams[e->stream_idx].buffer->resource;
3558 if (FAILED(wined3d_resource_unmap(resource, 0)))
3559 ERR("Failed to unmap resource.\n");
3561 return WINED3DERR_INVALIDCALL;
3563 e->data.buffer_object = 0;
3564 e->data.addr += (ULONG_PTR)map_desc.data;
3567 hr = process_vertices_strided(device, dst_idx, vertex_count,
3568 &stream_info, dst_buffer, flags, dst_fvf);
3570 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3572 if (!(map & 1))
3573 continue;
3575 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3576 if (FAILED(wined3d_resource_unmap(resource, 0)))
3577 ERR("Failed to unmap resource.\n");
3580 return hr;
3583 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3584 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3586 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3588 TRACE("device %p, stage %u, state %s, value %#x.\n",
3589 device, stage, debug_d3dtexturestate(state), value);
3591 if (stage >= d3d_info->limits.ffp_blend_stages)
3593 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3594 stage, d3d_info->limits.ffp_blend_stages - 1);
3595 return;
3598 if (value == device->cs->c.state->texture_states[stage][state])
3600 TRACE("Application is setting the old value over, nothing to do.\n");
3601 return;
3604 device->cs->c.state->texture_states[stage][state] = value;
3606 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
3609 static void wined3d_device_set_texture(struct wined3d_device *device,
3610 UINT stage, struct wined3d_texture *texture)
3612 struct wined3d_state *state = device->cs->c.state;
3613 struct wined3d_texture *prev;
3615 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3617 /* Windows accepts overflowing this array... we do not. */
3618 if (stage >= ARRAY_SIZE(state->textures))
3620 WARN("Ignoring invalid stage %u.\n", stage);
3621 return;
3624 prev = state->textures[stage];
3625 TRACE("Previous texture %p.\n", prev);
3627 if (texture == prev)
3629 TRACE("App is setting the same texture again, nothing to do.\n");
3630 return;
3633 TRACE("Setting new texture to %p.\n", texture);
3634 state->textures[stage] = texture;
3636 if (texture)
3637 wined3d_texture_incref(texture);
3638 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
3639 if (prev)
3640 wined3d_texture_decref(prev);
3642 return;
3645 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3646 struct wined3d_stateblock *stateblock)
3648 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3649 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3650 const struct wined3d_saved_states *changed = &stateblock->changed;
3651 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3652 struct wined3d_device_context *context = &device->cs->c;
3653 unsigned int i, j, start, idx;
3654 struct wined3d_range range;
3655 uint32_t map;
3657 TRACE("device %p, stateblock %p.\n", device, stateblock);
3659 if (changed->vertexShader)
3660 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
3661 if (changed->pixelShader)
3662 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
3664 for (start = 0; ; start = range.offset + range.size)
3666 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3667 break;
3669 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3672 map = changed->vertexShaderConstantsI;
3673 for (start = 0; ; start = range.offset + range.size)
3675 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3676 break;
3678 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3681 map = changed->vertexShaderConstantsB;
3682 for (start = 0; ; start = range.offset + range.size)
3684 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3685 break;
3687 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3690 for (start = 0; ; start = range.offset + range.size)
3692 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3693 break;
3695 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3698 map = changed->pixelShaderConstantsI;
3699 for (start = 0; ; start = range.offset + range.size)
3701 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3702 break;
3704 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3707 map = changed->pixelShaderConstantsB;
3708 for (start = 0; ; start = range.offset + range.size)
3710 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3711 break;
3713 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3716 if (changed->lights)
3718 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3720 const struct wined3d_light_info *light;
3722 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3724 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
3725 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3730 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3732 map = changed->renderState[i];
3733 while (map)
3735 j = wined3d_bit_scan(&map);
3736 idx = i * word_bit_count + j;
3738 switch (idx)
3740 case WINED3D_RS_BLENDFACTOR:
3741 case WINED3D_RS_MULTISAMPLEMASK:
3742 case WINED3D_RS_ALPHABLENDENABLE:
3743 case WINED3D_RS_SRCBLEND:
3744 case WINED3D_RS_DESTBLEND:
3745 case WINED3D_RS_BLENDOP:
3746 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
3747 case WINED3D_RS_SRCBLENDALPHA:
3748 case WINED3D_RS_DESTBLENDALPHA:
3749 case WINED3D_RS_BLENDOPALPHA:
3750 case WINED3D_RS_COLORWRITEENABLE:
3751 case WINED3D_RS_COLORWRITEENABLE1:
3752 case WINED3D_RS_COLORWRITEENABLE2:
3753 case WINED3D_RS_COLORWRITEENABLE3:
3754 set_blend_state = TRUE;
3755 break;
3757 case WINED3D_RS_BACK_STENCILFAIL:
3758 case WINED3D_RS_BACK_STENCILFUNC:
3759 case WINED3D_RS_BACK_STENCILPASS:
3760 case WINED3D_RS_BACK_STENCILZFAIL:
3761 case WINED3D_RS_STENCILENABLE:
3762 case WINED3D_RS_STENCILFAIL:
3763 case WINED3D_RS_STENCILFUNC:
3764 case WINED3D_RS_STENCILREF:
3765 case WINED3D_RS_STENCILMASK:
3766 case WINED3D_RS_STENCILPASS:
3767 case WINED3D_RS_STENCILWRITEMASK:
3768 case WINED3D_RS_STENCILZFAIL:
3769 case WINED3D_RS_TWOSIDEDSTENCILMODE:
3770 case WINED3D_RS_ZENABLE:
3771 case WINED3D_RS_ZFUNC:
3772 case WINED3D_RS_ZWRITEENABLE:
3773 set_depth_stencil_state = TRUE;
3774 break;
3776 case WINED3D_RS_FILLMODE:
3777 case WINED3D_RS_CULLMODE:
3778 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
3779 case WINED3D_RS_DEPTHBIAS:
3780 case WINED3D_RS_SCISSORTESTENABLE:
3781 case WINED3D_RS_ANTIALIASEDLINEENABLE:
3782 set_rasterizer_state = TRUE;
3783 break;
3785 default:
3786 wined3d_device_set_render_state(device, idx, state->rs[idx]);
3787 break;
3792 if (set_rasterizer_state)
3794 struct wined3d_rasterizer_state *rasterizer_state;
3795 struct wined3d_rasterizer_state_desc desc;
3796 struct wine_rb_entry *entry;
3797 union
3799 DWORD d;
3800 float f;
3801 } bias;
3803 memset(&desc, 0, sizeof(desc));
3804 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
3805 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
3806 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
3807 desc.depth_bias = bias.f;
3808 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
3809 desc.scale_bias = bias.f;
3810 desc.depth_clip = TRUE;
3811 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
3812 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
3814 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
3816 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
3817 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3819 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
3820 &wined3d_null_parent_ops, &rasterizer_state)))
3822 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3823 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
3825 ERR("Failed to insert rasterizer state.\n");
3826 wined3d_rasterizer_state_decref(rasterizer_state);
3831 if (set_blend_state || changed->alpha_to_coverage
3832 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
3834 struct wined3d_blend_state *blend_state;
3835 struct wined3d_blend_state_desc desc;
3836 struct wine_rb_entry *entry;
3837 struct wined3d_color colour;
3838 unsigned int sample_mask;
3840 memset(&desc, 0, sizeof(desc));
3841 desc.alpha_to_coverage = state->alpha_to_coverage;
3842 desc.independent = FALSE;
3843 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
3844 desc.alpha_to_coverage = TRUE;
3845 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
3846 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
3847 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
3848 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
3849 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
3851 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
3852 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
3853 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
3855 else
3857 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
3858 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
3859 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
3861 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
3862 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
3863 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
3864 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
3865 if (desc.rt[1].writemask != desc.rt[0].writemask
3866 || desc.rt[2].writemask != desc.rt[0].writemask
3867 || desc.rt[3].writemask != desc.rt[0].writemask)
3869 desc.independent = TRUE;
3870 for (i = 1; i < 4; ++i)
3872 desc.rt[i].enable = desc.rt[0].enable;
3873 desc.rt[i].src = desc.rt[0].src;
3874 desc.rt[i].dst = desc.rt[0].dst;
3875 desc.rt[i].op = desc.rt[0].op;
3876 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
3877 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
3878 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
3882 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
3883 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
3884 else
3885 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
3887 if ((entry = wine_rb_get(&device->blend_states, &desc)))
3889 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
3890 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3891 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3893 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
3894 &wined3d_null_parent_ops, &blend_state)))
3896 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3897 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3898 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
3900 ERR("Failed to insert blend state.\n");
3901 wined3d_blend_state_decref(blend_state);
3906 if (set_depth_stencil_state)
3908 struct wined3d_depth_stencil_state *depth_stencil_state;
3909 struct wined3d_depth_stencil_state_desc desc;
3910 struct wine_rb_entry *entry;
3911 unsigned int stencil_ref;
3913 memset(&desc, 0, sizeof(desc));
3914 switch (state->rs[WINED3D_RS_ZENABLE])
3916 case WINED3D_ZB_FALSE:
3917 desc.depth = FALSE;
3918 break;
3920 case WINED3D_ZB_USEW:
3921 FIXME("W buffer is not well handled.\n");
3922 case WINED3D_ZB_TRUE:
3923 desc.depth = TRUE;
3924 break;
3926 default:
3927 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
3929 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
3930 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
3931 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
3932 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
3933 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
3934 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
3935 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
3936 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
3937 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
3939 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
3941 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
3942 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
3943 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
3944 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
3946 else
3948 desc.back = desc.front;
3951 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
3952 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
3953 else
3954 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
3956 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
3958 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
3959 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3961 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
3962 &wined3d_null_parent_ops, &depth_stencil_state)))
3964 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3965 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
3967 ERR("Failed to insert depth/stencil state.\n");
3968 wined3d_depth_stencil_state_decref(depth_stencil_state);
3973 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
3975 map = changed->textureState[i];
3976 while (map)
3978 j = wined3d_bit_scan(&map);
3979 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
3983 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
3985 map = changed->samplerState[i];
3986 while (map)
3988 j = wined3d_bit_scan(&map);
3989 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
3993 if (changed->transforms)
3995 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
3997 map = changed->transform[i];
3998 while (map)
4000 j = wined3d_bit_scan(&map);
4001 idx = i * word_bit_count + j;
4002 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
4007 if (changed->indices)
4008 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
4009 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
4010 if (changed->vertexDecl)
4011 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
4012 if (changed->material)
4013 wined3d_device_set_material(device, &state->material);
4014 if (changed->viewport)
4015 wined3d_device_context_set_viewports(context, 1, &state->viewport);
4016 if (changed->scissorRect)
4017 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
4019 map = changed->streamSource | changed->streamFreq;
4020 while (map)
4022 i = wined3d_bit_scan(&map);
4023 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
4026 map = changed->textures;
4027 while (map)
4029 i = wined3d_bit_scan(&map);
4030 wined3d_device_set_texture(device, i, state->textures[i]);
4033 map = changed->clipplane;
4034 while (map)
4036 i = wined3d_bit_scan(&map);
4037 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4040 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4042 TRACE("Applied stateblock %p.\n", stateblock);
4045 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4047 TRACE("device %p, caps %p.\n", device, caps);
4049 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4052 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4053 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4055 struct wined3d_swapchain *swapchain;
4057 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4058 device, swapchain_idx, mode, rotation);
4060 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4061 return WINED3DERR_INVALIDCALL;
4063 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4066 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4068 /* At the moment we have no need for any functionality at the beginning
4069 * of a scene. */
4070 TRACE("device %p.\n", device);
4072 if (device->inScene)
4074 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4075 return WINED3DERR_INVALIDCALL;
4077 device->inScene = TRUE;
4078 return WINED3D_OK;
4081 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4083 TRACE("device %p.\n", device);
4085 if (!device->inScene)
4087 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4088 return WINED3DERR_INVALIDCALL;
4091 device->inScene = FALSE;
4092 return WINED3D_OK;
4095 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4096 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4098 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4100 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4101 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4103 if (!rect_count && rects)
4105 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4106 return WINED3D_OK;
4109 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4111 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4112 if (!ds)
4114 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4115 /* TODO: What about depth stencil buffers without stencil bits? */
4116 return WINED3DERR_INVALIDCALL;
4118 else if (flags & WINED3DCLEAR_TARGET)
4120 if (ds->width < fb->render_targets[0]->width
4121 || ds->height < fb->render_targets[0]->height)
4123 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4124 return WINED3D_OK;
4129 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4131 return WINED3D_OK;
4134 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4136 struct wined3d_state *state = context->state;
4138 TRACE("context %p, value %p.\n", context, value);
4140 if (value)
4141 *value = state->predicate_value;
4142 return state->predicate;
4145 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4146 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4148 struct wined3d_state *state = context->state;
4150 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4151 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4153 wined3d_device_context_lock(context);
4154 state->primitive_type = primitive_type;
4155 state->patch_vertex_count = patch_vertex_count;
4156 wined3d_device_context_unlock(context);
4159 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4160 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4162 const struct wined3d_state *state = context->state;
4164 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4165 context, primitive_type, patch_vertex_count);
4167 *primitive_type = state->primitive_type;
4168 if (patch_vertex_count)
4169 *patch_vertex_count = state->patch_vertex_count;
4171 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4174 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4175 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4177 unsigned int src_size, dst_size, src_skip_levels = 0;
4178 unsigned int src_level_count, dst_level_count;
4179 const struct wined3d_dirty_regions *regions;
4180 unsigned int layer_count, level_count, i, j;
4181 enum wined3d_resource_type type;
4182 BOOL entire_texture = TRUE;
4183 struct wined3d_box box;
4185 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4187 /* Verify that the source and destination textures are non-NULL. */
4188 if (!src_texture || !dst_texture)
4190 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4191 return WINED3DERR_INVALIDCALL;
4194 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4195 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4197 WARN("Source resource is GPU accessible or a scratch resource.\n");
4198 return WINED3DERR_INVALIDCALL;
4200 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4202 WARN("Destination resource is CPU accessible.\n");
4203 return WINED3DERR_INVALIDCALL;
4206 /* Verify that the source and destination textures are the same type. */
4207 type = src_texture->resource.type;
4208 if (dst_texture->resource.type != type)
4210 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4211 return WINED3DERR_INVALIDCALL;
4214 layer_count = src_texture->layer_count;
4215 if (layer_count != dst_texture->layer_count)
4217 WARN("Source and destination have different layer counts.\n");
4218 return WINED3DERR_INVALIDCALL;
4221 if (src_texture->resource.format != dst_texture->resource.format)
4223 WARN("Source and destination formats do not match.\n");
4224 return WINED3DERR_INVALIDCALL;
4227 src_level_count = src_texture->level_count;
4228 dst_level_count = dst_texture->level_count;
4229 level_count = min(src_level_count, dst_level_count);
4231 src_size = max(src_texture->resource.width, src_texture->resource.height);
4232 src_size = max(src_size, src_texture->resource.depth);
4233 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4234 dst_size = max(dst_size, dst_texture->resource.depth);
4235 while (src_size > dst_size)
4237 src_size >>= 1;
4238 ++src_skip_levels;
4241 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4242 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4243 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4245 WARN("Source and destination dimensions do not match.\n");
4246 return WINED3DERR_INVALIDCALL;
4249 if ((regions = src_texture->dirty_regions))
4251 for (i = 0; i < layer_count && entire_texture; ++i)
4253 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4254 continue;
4256 entire_texture = FALSE;
4257 break;
4261 /* Update every surface level of the texture. */
4262 if (entire_texture)
4264 for (i = 0; i < level_count; ++i)
4266 wined3d_texture_get_level_box(dst_texture, i, &box);
4267 for (j = 0; j < layer_count; ++j)
4269 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4270 &dst_texture->resource, j * dst_level_count + i, &box,
4271 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4272 0, NULL, WINED3D_TEXF_POINT);
4276 else
4278 unsigned int src_level, box_count, k;
4279 const struct wined3d_box *boxes;
4280 struct wined3d_box b;
4282 for (i = 0; i < layer_count; ++i)
4284 boxes = regions[i].boxes;
4285 box_count = regions[i].box_count;
4286 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4288 boxes = &b;
4289 box_count = 1;
4290 wined3d_texture_get_level_box(dst_texture, i, &b);
4293 for (j = 0; j < level_count; ++j)
4295 src_level = j + src_skip_levels;
4297 /* TODO: We could pass an array of boxes here to avoid
4298 * multiple context acquisitions for the same resource. */
4299 for (k = 0; k < box_count; ++k)
4301 box = boxes[k];
4302 if (src_level)
4304 box.left >>= src_level;
4305 box.top >>= src_level;
4306 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4307 wined3d_texture_get_level_width(src_texture, src_level));
4308 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4309 wined3d_texture_get_level_height(src_texture, src_level));
4310 box.front >>= src_level;
4311 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4312 wined3d_texture_get_level_depth(src_texture, src_level));
4315 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4316 &dst_texture->resource, i * dst_level_count + j, &box,
4317 &src_texture->resource, i * src_level_count + src_level, &box,
4318 0, NULL, WINED3D_TEXF_POINT);
4324 wined3d_texture_clear_dirty_regions(src_texture);
4326 return WINED3D_OK;
4329 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4331 const struct wined3d_state *state = device->cs->c.state;
4332 struct wined3d_texture *texture;
4333 DWORD i;
4335 TRACE("device %p, num_passes %p.\n", device, num_passes);
4337 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4339 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4341 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4342 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4344 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4346 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4347 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4350 texture = state->textures[i];
4351 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4353 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4355 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4356 return E_FAIL;
4358 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4360 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4361 return E_FAIL;
4363 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4364 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4366 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4367 return E_FAIL;
4371 if (wined3d_state_uses_depth_buffer(state)
4372 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4374 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4375 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4377 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4379 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4380 return WINED3DERR_CONFLICTINGRENDERSTATE;
4384 /* return a sensible default */
4385 *num_passes = 1;
4387 TRACE("returning D3D_OK\n");
4388 return WINED3D_OK;
4391 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4393 static BOOL warned;
4395 TRACE("device %p, software %#x.\n", device, software);
4397 if (!warned)
4399 FIXME("device %p, software %#x stub!\n", device, software);
4400 warned = TRUE;
4403 device->softwareVertexProcessing = software;
4406 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4408 static BOOL warned;
4410 TRACE("device %p.\n", device);
4412 if (!warned)
4414 TRACE("device %p stub!\n", device);
4415 warned = TRUE;
4418 return device->softwareVertexProcessing;
4421 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4422 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4424 struct wined3d_swapchain *swapchain;
4426 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4427 device, swapchain_idx, raster_status);
4429 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4430 return WINED3DERR_INVALIDCALL;
4432 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4435 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4437 static BOOL warned;
4439 TRACE("device %p, segments %.8e.\n", device, segments);
4441 if (segments != 0.0f)
4443 if (!warned)
4445 FIXME("device %p, segments %.8e stub!\n", device, segments);
4446 warned = TRUE;
4450 return WINED3D_OK;
4453 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4455 static BOOL warned;
4457 TRACE("device %p.\n", device);
4459 if (!warned)
4461 FIXME("device %p stub!\n", device);
4462 warned = TRUE;
4465 return 0.0f;
4468 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4469 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4471 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4472 context, dst_buffer, offset, uav);
4474 wined3d_device_context_lock(context);
4475 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4476 wined3d_device_context_unlock(context);
4479 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4480 const struct wined3d_resource *dst_resource)
4482 if (src_resource->format->id == dst_resource->format->id)
4483 return true;
4484 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4485 return true;
4486 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4487 return false;
4488 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4489 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4490 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4491 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4492 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4493 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4494 return false;
4497 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4498 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4500 unsigned int src_row_block_count, dst_row_block_count;
4501 struct wined3d_texture *dst_texture, *src_texture;
4502 unsigned int src_row_count, dst_row_count;
4503 struct wined3d_box src_box, dst_box;
4504 unsigned int i, j;
4506 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4508 if (src_resource == dst_resource)
4510 WARN("Source and destination are the same resource.\n");
4511 return;
4514 if (src_resource->type != dst_resource->type)
4516 WARN("Resource types (%s / %s) don't match.\n",
4517 debug_d3dresourcetype(dst_resource->type),
4518 debug_d3dresourcetype(src_resource->type));
4519 return;
4522 if (!resources_format_compatible(src_resource, dst_resource))
4524 WARN("Resource formats %s and %s are incompatible.\n",
4525 debug_d3dformat(dst_resource->format->id),
4526 debug_d3dformat(src_resource->format->id));
4527 return;
4530 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4531 / src_resource->format->block_width;
4532 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4533 / dst_resource->format->block_width;
4534 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4535 / src_resource->format->block_height;
4536 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4537 / dst_resource->format->block_height;
4539 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4540 || src_resource->depth != dst_resource->depth)
4542 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4543 dst_row_block_count, dst_row_count, dst_resource->depth,
4544 src_row_block_count, src_row_count, src_resource->depth);
4545 return;
4548 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4550 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4551 wined3d_device_context_lock(context);
4552 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4553 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4554 wined3d_device_context_unlock(context);
4555 return;
4558 dst_texture = texture_from_resource(dst_resource);
4559 src_texture = texture_from_resource(src_resource);
4561 if (src_texture->layer_count != dst_texture->layer_count
4562 || src_texture->level_count != dst_texture->level_count)
4564 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4565 dst_texture->layer_count, dst_texture->level_count,
4566 src_texture->layer_count, src_texture->level_count);
4567 return;
4570 wined3d_device_context_lock(context);
4571 for (i = 0; i < dst_texture->level_count; ++i)
4573 wined3d_texture_get_level_box(src_texture, i, &src_box);
4574 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4575 for (j = 0; j < dst_texture->layer_count; ++j)
4577 unsigned int idx = j * dst_texture->level_count + i;
4579 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4580 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4583 wined3d_device_context_unlock(context);
4586 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4587 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4588 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4589 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4591 struct wined3d_box dst_box, b;
4593 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4594 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4595 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4596 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4598 if (flags)
4599 FIXME("Ignoring flags %#x.\n", flags);
4601 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4603 WARN("Source and destination are the same sub-resource.\n");
4604 return WINED3DERR_INVALIDCALL;
4607 if (!resources_format_compatible(src_resource, dst_resource))
4609 WARN("Resource formats %s and %s are incompatible.\n",
4610 debug_d3dformat(dst_resource->format->id),
4611 debug_d3dformat(src_resource->format->id));
4612 return WINED3DERR_INVALIDCALL;
4615 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4617 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4619 WARN("Resource types (%s / %s) don't match.\n",
4620 debug_d3dresourcetype(dst_resource->type),
4621 debug_d3dresourcetype(src_resource->type));
4622 return WINED3DERR_INVALIDCALL;
4625 if (dst_sub_resource_idx)
4627 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4628 return WINED3DERR_INVALIDCALL;
4631 if (src_sub_resource_idx)
4633 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4634 return WINED3DERR_INVALIDCALL;
4637 if (!src_box)
4639 unsigned int dst_w;
4641 dst_w = dst_resource->size - dst_x;
4642 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4643 src_box = &b;
4645 else if ((src_box->left >= src_box->right
4646 || src_box->top >= src_box->bottom
4647 || src_box->front >= src_box->back))
4649 WARN("Invalid box %s specified.\n", debug_box(src_box));
4650 return WINED3DERR_INVALIDCALL;
4653 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4654 || src_box->right - src_box->left > dst_resource->size - dst_x)
4656 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4657 dst_x, src_box->left, src_box->right - src_box->left);
4658 return WINED3DERR_INVALIDCALL;
4661 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4663 else
4665 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4666 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4667 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4668 unsigned int src_row_block_count, src_row_count;
4670 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4672 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4673 return WINED3DERR_INVALIDCALL;
4676 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4678 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4679 return WINED3DERR_INVALIDCALL;
4682 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4684 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4685 return WINED3DERR_INVALIDCALL;
4688 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4690 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4691 return WINED3DERR_INVALIDCALL;
4694 if (!src_box)
4696 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4698 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4699 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4700 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4702 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4703 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4704 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4705 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4707 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4708 src_box = &b;
4710 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
4712 WARN("Invalid source box %s.\n", debug_box(src_box));
4713 return WINED3DERR_INVALIDCALL;
4716 if (src_resource->format->block_width == dst_resource->format->block_width
4717 && src_resource->format->block_height == dst_resource->format->block_height)
4719 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4720 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4722 else
4724 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
4725 / src_resource->format->block_width;
4726 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
4727 / src_resource->format->block_height;
4728 wined3d_box_set(&dst_box, dst_x, dst_y,
4729 dst_x + (src_row_block_count * dst_resource->format->block_width),
4730 dst_y + (src_row_count * dst_resource->format->block_height),
4731 dst_z, dst_z + (src_box->back - src_box->front));
4733 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
4735 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4736 return WINED3DERR_INVALIDCALL;
4740 wined3d_device_context_lock(context);
4741 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
4742 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4743 wined3d_device_context_unlock(context);
4745 return WINED3D_OK;
4748 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
4749 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
4750 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
4752 struct wined3d_sub_resource_desc desc;
4753 struct wined3d_box b;
4755 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
4756 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4758 if (flags)
4759 FIXME("Ignoring flags %#x.\n", flags);
4761 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4763 WARN("Resource %p is not GPU accessible.\n", resource);
4764 return;
4767 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4768 return;
4770 if (!box)
4772 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4773 box = &b;
4775 else if (box->left >= box->right || box->right > desc.width
4776 || box->top >= box->bottom || box->bottom > desc.height
4777 || box->front >= box->back || box->back > desc.depth)
4779 WARN("Invalid box %s specified.\n", debug_box(box));
4780 return;
4783 wined3d_device_context_lock(context);
4784 wined3d_device_context_emit_update_sub_resource(context, resource,
4785 sub_resource_idx, box, data, row_pitch, depth_pitch);
4786 wined3d_device_context_unlock(context);
4789 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
4790 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4791 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4792 enum wined3d_format_id format_id)
4794 struct wined3d_texture *dst_texture, *src_texture;
4795 unsigned int dst_level, src_level;
4796 struct wined3d_blt_fx fx = {0};
4797 RECT dst_rect, src_rect;
4799 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
4800 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4801 context, dst_resource, dst_sub_resource_idx,
4802 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4804 if (wined3d_format_is_typeless(dst_resource->format)
4805 || wined3d_format_is_typeless(src_resource->format))
4807 FIXME("Multisample resolve is not fully supported for typeless formats "
4808 "(dst_format %s, src_format %s, format %s).\n",
4809 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4810 debug_d3dformat(format_id));
4812 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4814 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4815 return;
4817 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4819 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4820 return;
4823 wined3d_device_context_lock(context);
4824 fx.resolve_format_id = format_id;
4826 dst_texture = texture_from_resource(dst_resource);
4827 src_texture = texture_from_resource(src_resource);
4829 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4830 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4831 wined3d_texture_get_level_height(dst_texture, dst_level));
4832 src_level = src_sub_resource_idx % src_texture->level_count;
4833 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4834 wined3d_texture_get_level_height(src_texture, src_level));
4835 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
4836 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
4837 wined3d_device_context_unlock(context);
4840 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
4841 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
4842 const struct wined3d_color *color, float depth, unsigned int stencil)
4844 struct wined3d_resource *resource;
4845 RECT r;
4847 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4848 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4850 if (!flags)
4851 return WINED3D_OK;
4853 resource = view->resource;
4854 if (resource->type == WINED3D_RTYPE_BUFFER)
4856 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4857 return WINED3DERR_INVALIDCALL;
4860 if (!rect)
4862 SetRect(&r, 0, 0, view->width, view->height);
4863 rect = &r;
4865 else
4867 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4868 HRESULT hr;
4870 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
4871 return hr;
4874 wined3d_device_context_lock(context);
4875 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
4876 wined3d_device_context_unlock(context);
4878 return WINED3D_OK;
4881 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
4882 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
4884 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
4886 if (!(view->format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_FLOAT | WINED3DFMT_FLAG_NORMALISED)))
4888 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
4889 return;
4892 wined3d_device_context_lock(context);
4893 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
4894 wined3d_device_context_unlock(context);
4897 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
4898 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4900 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
4902 wined3d_device_context_lock(context);
4903 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
4904 wined3d_device_context_unlock(context);
4907 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
4909 /* Not all flags make sense together, but Windows never returns an error.
4910 * Catch the cases that could cause issues. */
4911 if (flags & WINED3D_MAP_READ)
4913 if (flags & WINED3D_MAP_DISCARD)
4915 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
4916 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4918 if (flags & WINED3D_MAP_NOOVERWRITE)
4920 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
4921 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4924 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4926 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
4928 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
4929 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4931 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4932 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4934 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
4935 flags &= ~WINED3D_MAP_DISCARD;
4939 return flags;
4942 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
4943 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4944 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
4946 struct wined3d_sub_resource_desc desc;
4947 struct wined3d_box b;
4948 HRESULT hr;
4950 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
4951 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
4953 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
4955 WARN("No read/write flags specified.\n");
4956 return E_INVALIDARG;
4959 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
4961 WARN("Resource does not have MAP_R access.\n");
4962 return E_INVALIDARG;
4965 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
4967 WARN("Resource does not have MAP_W access.\n");
4968 return E_INVALIDARG;
4971 flags = sanitise_map_flags(resource, flags);
4973 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4974 return E_INVALIDARG;
4976 if (!box)
4978 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4979 box = &b;
4981 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
4983 WARN("Map box is invalid.\n");
4985 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
4986 return WINED3DERR_INVALIDCALL;
4988 if ((resource->format_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
4989 return WINED3DERR_INVALIDCALL;
4992 wined3d_device_context_lock(context);
4993 hr = wined3d_device_context_emit_map(context, resource, sub_resource_idx, map_desc, box, flags);
4994 wined3d_device_context_unlock(context);
4995 return hr;
4998 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
4999 struct wined3d_resource *resource, unsigned int sub_resource_idx)
5001 HRESULT hr;
5002 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
5004 wined3d_device_context_lock(context);
5005 hr = wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
5006 wined3d_device_context_unlock(context);
5007 return hr;
5010 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
5011 struct wined3d_query *query, unsigned int flags)
5013 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
5015 wined3d_device_context_lock(context);
5016 context->ops->issue_query(context, query, flags);
5017 wined3d_device_context_unlock(context);
5020 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
5021 struct wined3d_command_list *list, bool restore_state)
5023 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
5025 wined3d_device_context_lock(context);
5026 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
5027 wined3d_device_context_unlock(context);
5030 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
5031 const struct wined3d_device_context *context, unsigned int view_idx)
5033 unsigned int max_rt_count;
5035 TRACE("context %p, view_idx %u.\n", context, view_idx);
5037 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
5038 if (view_idx >= max_rt_count)
5040 WARN("Only %u render targets are supported.\n", max_rt_count);
5041 return NULL;
5044 return context->state->fb.render_targets[view_idx];
5047 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
5048 const struct wined3d_device_context *context)
5050 TRACE("context %p.\n", context);
5052 return context->state->fb.depth_stencil;
5055 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
5056 struct wined3d_shader_resource_view *view)
5058 struct wined3d_texture *texture;
5060 TRACE("context %p, view %p.\n", context, view);
5062 if (view->resource->type == WINED3D_RTYPE_BUFFER)
5064 WARN("Called on buffer resource %p.\n", view->resource);
5065 return;
5068 texture = texture_from_resource(view->resource);
5069 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
5071 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
5072 return;
5075 wined3d_device_context_lock(context);
5076 wined3d_device_context_emit_generate_mipmaps(context, view);
5077 wined3d_device_context_unlock(context);
5080 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5081 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5083 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5084 struct wined3d_sub_resource_data data;
5085 struct wined3d_resource_desc desc;
5086 struct wined3d_map_desc map_desc;
5087 struct wined3d_texture *texture;
5088 HRESULT hr;
5090 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5092 ERR("Failed to map source texture.\n");
5093 return NULL;
5096 data.data = map_desc.data;
5097 data.row_pitch = map_desc.row_pitch;
5098 data.slice_pitch = map_desc.slice_pitch;
5100 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5101 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5102 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5103 desc.multisample_quality = 0;
5104 desc.usage = WINED3DUSAGE_DYNAMIC;
5105 desc.bind_flags = 0;
5106 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5107 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5108 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5109 desc.depth = 1;
5110 desc.size = 0;
5112 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5113 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5114 if (FAILED(hr))
5116 ERR("Failed to create cursor texture.\n");
5117 return NULL;
5120 return texture;
5123 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5124 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5126 unsigned int texture_level = sub_resource_idx % texture->level_count;
5127 unsigned int cursor_width, cursor_height;
5128 struct wined3d_map_desc map_desc;
5130 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5131 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5133 if (sub_resource_idx >= texture->level_count * texture->layer_count
5134 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5135 return WINED3DERR_INVALIDCALL;
5137 if (device->cursor_texture)
5139 wined3d_texture_decref(device->cursor_texture);
5140 device->cursor_texture = NULL;
5143 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5145 WARN("Texture %p has invalid format %s.\n",
5146 texture, debug_d3dformat(texture->resource.format->id));
5147 return WINED3DERR_INVALIDCALL;
5150 /* Cursor width and height must all be powers of two */
5151 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5152 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5153 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5155 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5156 return WINED3DERR_INVALIDCALL;
5159 /* Do not store the surface's pointer because the application may
5160 * release it after setting the cursor image. Windows doesn't
5161 * addref the set surface, so we can't do this either without
5162 * creating circular refcount dependencies. */
5163 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5165 ERR("Failed to create cursor texture.\n");
5166 return WINED3DERR_INVALIDCALL;
5169 if (cursor_width == 32 && cursor_height == 32)
5171 UINT mask_size = cursor_width * cursor_height / 8;
5172 ICONINFO cursor_info;
5173 DWORD *mask_bits;
5174 HCURSOR cursor;
5176 /* 32-bit user32 cursors ignore the alpha channel if it's all
5177 * zeroes, and use the mask instead. Fill the mask with all ones
5178 * to ensure we still get a fully transparent cursor. */
5179 if (!(mask_bits = heap_alloc(mask_size)))
5180 return E_OUTOFMEMORY;
5181 memset(mask_bits, 0xff, mask_size);
5183 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5184 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5185 cursor_info.fIcon = FALSE;
5186 cursor_info.xHotspot = x_hotspot;
5187 cursor_info.yHotspot = y_hotspot;
5188 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5189 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5190 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5192 /* Create our cursor and clean up. */
5193 cursor = CreateIconIndirect(&cursor_info);
5194 if (cursor_info.hbmMask)
5195 DeleteObject(cursor_info.hbmMask);
5196 if (cursor_info.hbmColor)
5197 DeleteObject(cursor_info.hbmColor);
5198 if (device->hardwareCursor)
5199 DestroyCursor(device->hardwareCursor);
5200 device->hardwareCursor = cursor;
5201 if (device->bCursorVisible)
5202 SetCursor(cursor);
5204 heap_free(mask_bits);
5207 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5208 device->cursorWidth = cursor_width;
5209 device->cursorHeight = cursor_height;
5210 device->xHotSpot = x_hotspot;
5211 device->yHotSpot = y_hotspot;
5213 return WINED3D_OK;
5216 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5217 int x_screen_space, int y_screen_space, DWORD flags)
5219 TRACE("device %p, x %d, y %d, flags %#x.\n",
5220 device, x_screen_space, y_screen_space, flags);
5222 device->xScreenSpace = x_screen_space;
5223 device->yScreenSpace = y_screen_space;
5225 if (device->hardwareCursor)
5227 POINT pt;
5229 GetCursorPos( &pt );
5230 if (x_screen_space == pt.x && y_screen_space == pt.y)
5231 return;
5232 SetCursorPos( x_screen_space, y_screen_space );
5234 /* Switch to the software cursor if position diverges from the hardware one. */
5235 GetCursorPos( &pt );
5236 if (x_screen_space != pt.x || y_screen_space != pt.y)
5238 if (device->bCursorVisible) SetCursor( NULL );
5239 DestroyCursor( device->hardwareCursor );
5240 device->hardwareCursor = 0;
5245 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5247 BOOL oldVisible = device->bCursorVisible;
5249 TRACE("device %p, show %#x.\n", device, show);
5252 * When ShowCursor is first called it should make the cursor appear at the OS's last
5253 * known cursor position.
5255 if (show && !oldVisible)
5257 POINT pt;
5258 GetCursorPos(&pt);
5259 device->xScreenSpace = pt.x;
5260 device->yScreenSpace = pt.y;
5263 if (device->hardwareCursor)
5265 device->bCursorVisible = show;
5266 if (show)
5267 SetCursor(device->hardwareCursor);
5268 else
5269 SetCursor(NULL);
5271 else if (device->cursor_texture)
5273 device->bCursorVisible = show;
5276 return oldVisible;
5279 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5281 struct wined3d_resource *resource, *cursor;
5283 TRACE("device %p.\n", device);
5285 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5287 TRACE("Checking resource %p for eviction.\n", resource);
5289 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5291 TRACE("Evicting %p.\n", resource);
5292 wined3d_cs_emit_unload_resource(device->cs, resource);
5297 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5299 TRACE("context %p.\n", context);
5301 wined3d_device_context_lock(context);
5302 context->ops->flush(context);
5303 wined3d_device_context_unlock(context);
5306 static void update_swapchain_flags(struct wined3d_texture *texture)
5308 unsigned int flags = texture->swapchain->state.desc.flags;
5310 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5311 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5312 else
5313 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5315 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5316 texture->flags |= WINED3D_TEXTURE_GET_DC;
5317 else
5318 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5321 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5322 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5323 wined3d_device_reset_cb callback, BOOL reset_state)
5325 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
5326 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5327 struct wined3d_device_context *context = &device->cs->c;
5328 struct wined3d_swapchain_state *swapchain_state;
5329 struct wined3d_state *state = context->state;
5330 struct wined3d_swapchain_desc *current_desc;
5331 struct wined3d_resource *resource, *cursor;
5332 struct wined3d_rendertarget_view *view;
5333 struct wined3d_swapchain *swapchain;
5334 struct wined3d_view_desc view_desc;
5335 BOOL backbuffer_resized, windowed;
5336 HRESULT hr = WINED3D_OK;
5337 unsigned int i;
5339 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5340 device, swapchain_desc, mode, callback, reset_state);
5342 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5344 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5346 ERR("Failed to get the first implicit swapchain.\n");
5347 return WINED3DERR_INVALIDCALL;
5349 swapchain_state = &swapchain->state;
5350 current_desc = &swapchain_state->desc;
5352 if (reset_state)
5354 if (device->logo_texture)
5356 wined3d_texture_decref(device->logo_texture);
5357 device->logo_texture = NULL;
5359 if (device->cursor_texture)
5361 wined3d_texture_decref(device->cursor_texture);
5362 device->cursor_texture = NULL;
5364 state_unbind_resources(state);
5367 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
5368 wined3d_device_context_set_depth_stencil_view(context, NULL);
5370 if (reset_state)
5372 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5374 TRACE("Enumerating resource %p.\n", resource);
5375 if (FAILED(hr = callback(resource)))
5376 return hr;
5380 TRACE("New params:\n");
5381 TRACE("output %p\n", swapchain_desc->output);
5382 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5383 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5384 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5385 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5386 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5387 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5388 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5389 TRACE("device_window %p\n", swapchain_desc->device_window);
5390 TRACE("windowed %#x\n", swapchain_desc->windowed);
5391 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5392 if (swapchain_desc->enable_auto_depth_stencil)
5393 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5394 TRACE("flags %#x\n", swapchain_desc->flags);
5395 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5396 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5398 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5399 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5401 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5402 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5403 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5404 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5406 /* No special treatment of these parameters. Just store them */
5407 current_desc->swap_effect = swapchain_desc->swap_effect;
5408 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5409 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5410 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5411 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5413 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5415 TRACE("Changing the device window from %p to %p.\n",
5416 current_desc->device_window, swapchain_desc->device_window);
5417 current_desc->device_window = swapchain_desc->device_window;
5418 swapchain_state->device_window = swapchain_desc->device_window;
5419 wined3d_swapchain_set_window(swapchain, NULL);
5422 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5423 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5424 windowed = current_desc->windowed;
5426 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5427 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5429 /* Switch from windowed to fullscreen. */
5430 if (windowed && !swapchain_desc->windowed)
5432 HWND focus_window = device->create_parms.focus_window;
5434 if (!focus_window)
5435 focus_window = swapchain->state.device_window;
5436 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5438 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5439 return hr;
5443 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5444 swapchain_desc, mode)))
5445 return hr;
5447 /* Switch from fullscreen to windowed. */
5448 if (!windowed && swapchain_desc->windowed)
5449 wined3d_device_release_focus_window(device);
5451 else if (!swapchain_desc->windowed)
5453 DWORD style = swapchain_state->style;
5454 DWORD exstyle = swapchain_state->exstyle;
5455 struct wined3d_output_desc output_desc;
5457 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5458 * the window back into the right position. Some applications
5459 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5460 * up their mess. Guild Wars also loses the device during that. */
5461 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5463 ERR("Failed to get output description, hr %#x.\n", hr);
5464 return hr;
5467 swapchain_state->style = 0;
5468 swapchain_state->exstyle = 0;
5469 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5470 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5471 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5472 swapchain_state->style = style;
5473 swapchain_state->exstyle = exstyle;
5476 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5477 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5478 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5479 return hr;
5481 if (swapchain_desc->flags != current_desc->flags)
5483 current_desc->flags = swapchain_desc->flags;
5485 update_swapchain_flags(swapchain->front_buffer);
5486 for (i = 0; i < current_desc->backbuffer_count; ++i)
5488 update_swapchain_flags(swapchain->back_buffers[i]);
5492 if ((view = device->auto_depth_stencil_view))
5494 device->auto_depth_stencil_view = NULL;
5495 wined3d_rendertarget_view_decref(view);
5497 if (current_desc->enable_auto_depth_stencil)
5499 struct wined3d_resource_desc texture_desc;
5500 struct wined3d_texture *texture;
5502 TRACE("Creating the depth stencil buffer.\n");
5504 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5505 texture_desc.format = current_desc->auto_depth_stencil_format;
5506 texture_desc.multisample_type = current_desc->multisample_type;
5507 texture_desc.multisample_quality = current_desc->multisample_quality;
5508 texture_desc.usage = 0;
5509 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5510 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5511 texture_desc.width = current_desc->backbuffer_width;
5512 texture_desc.height = current_desc->backbuffer_height;
5513 texture_desc.depth = 1;
5514 texture_desc.size = 0;
5516 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5517 device->device_parent, &texture_desc, 0, &texture)))
5519 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5520 return WINED3DERR_INVALIDCALL;
5523 view_desc.format_id = texture->resource.format->id;
5524 view_desc.flags = 0;
5525 view_desc.u.texture.level_idx = 0;
5526 view_desc.u.texture.level_count = 1;
5527 view_desc.u.texture.layer_idx = 0;
5528 view_desc.u.texture.layer_count = 1;
5529 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5530 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5531 wined3d_texture_decref(texture);
5532 if (FAILED(hr))
5534 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5535 return hr;
5539 if ((view = device->back_buffer_view))
5541 device->back_buffer_view = NULL;
5542 wined3d_rendertarget_view_decref(view);
5544 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5546 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5548 view_desc.format_id = back_buffer->format->id;
5549 view_desc.flags = 0;
5550 view_desc.u.texture.level_idx = 0;
5551 view_desc.u.texture.level_count = 1;
5552 view_desc.u.texture.layer_idx = 0;
5553 view_desc.u.texture.layer_count = 1;
5554 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5555 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5557 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5558 return hr;
5562 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5563 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5564 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5565 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5567 if (reset_state)
5569 TRACE("Resetting state.\n");
5570 wined3d_device_context_emit_reset_state(&device->cs->c, false);
5571 state_cleanup(state);
5573 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5575 TRACE("Unloading resource %p.\n", resource);
5576 wined3d_cs_emit_unload_resource(device->cs, resource);
5579 device->adapter->adapter_ops->adapter_uninit_3d(device);
5581 wined3d_state_reset(state, &device->adapter->d3d_info);
5583 device_init_swapchain_state(device, swapchain);
5584 if (wined3d_settings.logo)
5585 device_load_logo(device, wined3d_settings.logo);
5587 else
5589 if ((view = device->back_buffer_view))
5590 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
5591 if ((view = device->auto_depth_stencil_view))
5592 wined3d_device_context_set_depth_stencil_view(context, view);
5595 if (reset_state)
5596 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5598 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5599 * first use
5601 return hr;
5604 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5606 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5608 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5610 return WINED3D_OK;
5614 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5615 struct wined3d_device_creation_parameters *parameters)
5617 TRACE("device %p, parameters %p.\n", device, parameters);
5619 *parameters = device->create_parms;
5622 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5624 TRACE("device %p.\n", device);
5626 return device->wined3d;
5629 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5630 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5632 struct wined3d_swapchain *swapchain;
5634 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5635 device, swapchain_idx, flags, ramp);
5637 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5638 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5641 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5642 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5644 struct wined3d_swapchain *swapchain;
5646 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5647 device, swapchain_idx, ramp);
5649 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5650 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5653 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5655 TRACE("device %p, resource %p.\n", device, resource);
5657 wined3d_not_from_cs(device->cs);
5659 list_add_head(&device->resources, &resource->resource_list_entry);
5662 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5664 TRACE("device %p, resource %p.\n", device, resource);
5666 wined3d_not_from_cs(device->cs);
5668 list_remove(&resource->resource_list_entry);
5671 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5673 enum wined3d_resource_type type = resource->type;
5674 struct wined3d_state *state = device->cs->c.state;
5675 struct wined3d_rendertarget_view *rtv;
5676 unsigned int i;
5678 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5680 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5682 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5683 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5686 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5687 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5689 switch (type)
5691 case WINED3D_RTYPE_TEXTURE_1D:
5692 case WINED3D_RTYPE_TEXTURE_2D:
5693 case WINED3D_RTYPE_TEXTURE_3D:
5694 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5696 if (&state->textures[i]->resource == resource)
5698 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5699 state->textures[i] = NULL;
5702 break;
5704 case WINED3D_RTYPE_BUFFER:
5705 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5707 if (&state->streams[i].buffer->resource == resource)
5709 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5710 state->streams[i].buffer = NULL;
5714 if (&state->index_buffer->resource == resource)
5716 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5717 state->index_buffer = NULL;
5719 break;
5721 default:
5722 break;
5725 /* Remove the resource from the resourceStore */
5726 device_resource_remove(device, resource);
5728 TRACE("Resource released.\n");
5731 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5733 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5734 struct wined3d_so_desc_entry, entry)->desc;
5735 const struct wined3d_stream_output_desc *k = key;
5736 unsigned int i;
5737 int ret;
5739 if ((ret = (k->element_count - desc->element_count)))
5740 return ret;
5741 if ((ret = (k->buffer_stride_count - desc->buffer_stride_count)))
5742 return ret;
5743 if ((ret = (k->rasterizer_stream_idx - desc->rasterizer_stream_idx)))
5744 return ret;
5746 for (i = 0; i < k->element_count; ++i)
5748 const struct wined3d_stream_output_element *b = &desc->elements[i];
5749 const struct wined3d_stream_output_element *a = &k->elements[i];
5751 if ((ret = (a->stream_idx - b->stream_idx)))
5752 return ret;
5753 if ((ret = (!a->semantic_name - !b->semantic_name)))
5754 return ret;
5755 if (a->semantic_name && (ret = strcmp(a->semantic_name, b->semantic_name)))
5756 return ret;
5757 if ((ret = (a->semantic_idx - b->semantic_idx)))
5758 return ret;
5759 if ((ret = (a->component_idx - b->component_idx)))
5760 return ret;
5761 if ((ret = (a->component_count - b->component_count)))
5762 return ret;
5763 if ((ret = (a->output_slot - b->output_slot)))
5764 return ret;
5767 for (i = 0; i < k->buffer_stride_count; ++i)
5769 if ((ret = (k->buffer_strides[i] - desc->buffer_strides[i])))
5770 return ret;
5773 return 0;
5776 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5778 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5780 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5783 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
5785 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
5787 return memcmp(&state->desc, key, sizeof(state->desc));
5790 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
5792 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
5794 return memcmp(&state->desc, key, sizeof(state->desc));
5797 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
5799 const struct wined3d_depth_stencil_state *state
5800 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
5802 return memcmp(&state->desc, key, sizeof(state->desc));
5805 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5806 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5807 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5808 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
5810 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5811 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
5812 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5813 unsigned int i;
5814 HRESULT hr;
5816 device->ref = 1;
5817 device->wined3d = wined3d;
5818 wined3d_incref(device->wined3d);
5819 device->adapter = adapter;
5820 device->device_parent = device_parent;
5821 list_init(&device->resources);
5822 list_init(&device->shaders);
5823 device->surface_alignment = surface_alignment;
5825 /* Save the creation parameters. */
5826 device->create_parms.adapter_idx = adapter_idx;
5827 device->create_parms.device_type = device_type;
5828 device->create_parms.focus_window = focus_window;
5829 device->create_parms.flags = flags;
5831 device->shader_backend = adapter->shader_backend;
5833 vertex_pipeline = adapter->vertex_pipe;
5835 fragment_pipeline = adapter->fragment_pipe;
5837 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
5838 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5839 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
5840 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
5841 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
5843 if (vertex_pipeline->vp_states && fragment_pipeline->states
5844 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5845 &adapter->d3d_info, supported_extensions, vertex_pipeline,
5846 fragment_pipeline, adapter->misc_state_template)))
5848 ERR("Failed to compile state table, hr %#x.\n", hr);
5849 wine_rb_destroy(&device->samplers, NULL, NULL);
5850 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5851 wine_rb_destroy(&device->blend_states, NULL, NULL);
5852 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5853 wine_rb_destroy(&device->so_descs, NULL, NULL);
5854 wined3d_decref(device->wined3d);
5855 return hr;
5858 device->max_frame_latency = 3;
5860 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
5862 WARN("Failed to create command stream.\n");
5863 hr = E_FAIL;
5864 goto err;
5867 return WINED3D_OK;
5869 err:
5870 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5872 heap_free(device->multistate_funcs[i]);
5874 wine_rb_destroy(&device->samplers, NULL, NULL);
5875 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5876 wine_rb_destroy(&device->blend_states, NULL, NULL);
5877 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5878 wine_rb_destroy(&device->so_descs, NULL, NULL);
5879 wined3d_decref(device->wined3d);
5880 return hr;
5883 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
5885 unsigned int representative, i, idx, shift;
5886 struct wined3d_context *context;
5888 wined3d_from_cs(device->cs);
5890 if (STATE_IS_COMPUTE(state_id))
5892 for (i = 0; i < device->context_count; ++i)
5893 context_invalidate_compute_state(device->contexts[i], state_id);
5894 return;
5897 representative = device->state_table[state_id].representative;
5898 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
5899 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
5900 for (i = 0; i < device->context_count; ++i)
5902 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
5906 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5907 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5909 if (message == WM_DESTROY)
5911 TRACE("unregister window %p.\n", window);
5912 wined3d_unregister_window(window);
5914 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5915 ERR("Window %p is not the focus window for device %p.\n", window, device);
5917 else if (message == WM_DISPLAYCHANGE)
5919 device->device_parent->ops->mode_changed(device->device_parent);
5921 else if (message == WM_ACTIVATEAPP)
5923 unsigned int i = device->swapchain_count;
5925 /* Deactivating the implicit swapchain may cause the application
5926 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5927 * deactivate the implicit swapchain last, and to avoid accessing the
5928 * "device" pointer afterwards. */
5929 while (i--)
5930 wined3d_swapchain_activate(device->swapchains[i], wparam);
5932 else if (message == WM_SYSCOMMAND)
5934 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5936 if (unicode)
5937 DefWindowProcW(window, message, wparam, lparam);
5938 else
5939 DefWindowProcA(window, message, wparam, lparam);
5943 if (unicode)
5944 return CallWindowProcW(proc, window, message, wparam, lparam);
5945 else
5946 return CallWindowProcA(proc, window, message, wparam, lparam);