wined3d: Set an array of constant buffers as a single CS operation.
[wine.git] / dlls / wined3d / device.c
blob47919ed4f2da7f664f0b40568958bdbbf0c19dd6
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #ifdef HAVE_FLOAT_H
31 # include <float.h>
32 #endif
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 struct wined3d_matrix_3x3
41 float _11, _12, _13;
42 float _21, _22, _23;
43 float _31, _32, _33;
46 struct light_transformed
48 struct wined3d_color diffuse, specular, ambient;
49 struct wined3d_vec4 position;
50 struct wined3d_vec3 direction;
51 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
54 struct lights_settings
56 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
57 struct wined3d_color ambient_light;
58 struct wined3d_matrix modelview_matrix;
59 struct wined3d_matrix_3x3 normal_matrix;
60 struct wined3d_vec4 position_transformed;
62 float fog_start, fog_end, fog_density;
64 uint32_t point_light_count : 8;
65 uint32_t spot_light_count : 8;
66 uint32_t directional_light_count : 8;
67 uint32_t parallel_point_light_count : 8;
68 uint32_t lighting : 1;
69 uint32_t legacy_lighting : 1;
70 uint32_t normalise : 1;
71 uint32_t localviewer : 1;
72 uint32_t fog_coord_mode : 2;
73 uint32_t fog_mode : 2;
74 uint32_t padding : 24;
77 /* Define the default light parameters as specified by MSDN. */
78 const struct wined3d_light WINED3D_default_light =
80 WINED3D_LIGHT_DIRECTIONAL, /* Type */
81 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
82 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
83 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
84 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
85 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
86 0.0f, /* Range */
87 0.0f, /* Falloff */
88 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
89 0.0f, /* Theta */
90 0.0f /* Phi */
93 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
95 struct wined3d_context **new_array;
97 TRACE("Adding context %p.\n", context);
99 if (!device->shader_backend->shader_allocate_context_data(context))
101 ERR("Failed to allocate shader backend context data.\n");
102 return FALSE;
104 device->shader_backend->shader_init_context_state(context);
106 if (!device->adapter->fragment_pipe->allocate_context_data(context))
108 ERR("Failed to allocate fragment pipeline context data.\n");
109 device->shader_backend->shader_free_context_data(context);
110 return FALSE;
113 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
115 ERR("Failed to grow the context array.\n");
116 device->adapter->fragment_pipe->free_context_data(context);
117 device->shader_backend->shader_free_context_data(context);
118 return FALSE;
121 new_array[device->context_count++] = context;
122 device->contexts = new_array;
124 return TRUE;
127 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
129 struct wined3d_context **new_array;
130 BOOL found = FALSE;
131 UINT i;
133 TRACE("Removing context %p.\n", context);
135 device->adapter->fragment_pipe->free_context_data(context);
136 device->shader_backend->shader_free_context_data(context);
138 for (i = 0; i < device->context_count; ++i)
140 if (device->contexts[i] == context)
142 found = TRUE;
143 break;
147 if (!found)
149 ERR("Context %p doesn't exist in context array.\n", context);
150 return;
153 if (!--device->context_count)
155 heap_free(device->contexts);
156 device->contexts = NULL;
157 return;
160 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
161 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
163 ERR("Failed to shrink context array. Oh well.\n");
164 return;
167 device->contexts = new_array;
170 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
172 ULONG refcount = InterlockedIncrement(&device->ref);
174 TRACE("%p increasing refcount to %u.\n", device, refcount);
176 return refcount;
179 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
181 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
183 heap_free(s);
186 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
188 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
190 ERR("Leftover sampler %p.\n", sampler);
193 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
195 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
197 ERR("Leftover rasterizer state %p.\n", state);
200 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
202 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
204 ERR("Leftover blend state %p.\n", blend_state);
207 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
209 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
211 ERR("Leftover depth/stencil state %p.\n", state);
214 void wined3d_device_cleanup(struct wined3d_device *device)
216 unsigned int i;
218 if (device->swapchain_count)
219 wined3d_device_uninit_3d(device);
221 wined3d_cs_destroy(device->cs);
223 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
225 heap_free(device->multistate_funcs[i]);
226 device->multistate_funcs[i] = NULL;
229 if (!list_empty(&device->resources))
231 struct wined3d_resource *resource;
233 ERR("Device released with resources still bound.\n");
235 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
237 ERR("Leftover resource %p with type %s (%#x).\n",
238 resource, debug_d3dresourcetype(resource->type), resource->type);
242 if (device->contexts)
243 ERR("Context array not freed!\n");
244 if (device->hardwareCursor)
245 DestroyCursor(device->hardwareCursor);
246 device->hardwareCursor = 0;
248 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
249 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
250 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
251 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
252 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
254 wined3d_decref(device->wined3d);
255 device->wined3d = NULL;
258 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
260 ULONG refcount = InterlockedDecrement(&device->ref);
262 TRACE("%p decreasing refcount to %u.\n", device, refcount);
264 if (!refcount)
266 device->adapter->adapter_ops->adapter_destroy_device(device);
267 TRACE("Destroyed device %p.\n", device);
270 return refcount;
273 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
275 TRACE("device %p.\n", device);
277 return device->swapchain_count;
280 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
282 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
284 if (swapchain_idx >= device->swapchain_count)
286 WARN("swapchain_idx %u >= swapchain_count %u.\n",
287 swapchain_idx, device->swapchain_count);
288 return NULL;
291 return device->swapchains[swapchain_idx];
294 static void device_load_logo(struct wined3d_device *device, const char *filename)
296 struct wined3d_color_key color_key;
297 struct wined3d_resource_desc desc;
298 HBITMAP hbm;
299 BITMAP bm;
300 HRESULT hr;
301 HDC dcb = NULL, dcs = NULL;
303 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
305 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
306 return;
308 GetObjectA(hbm, sizeof(BITMAP), &bm);
310 if (!(dcb = CreateCompatibleDC(NULL)))
311 goto out;
312 SelectObject(dcb, hbm);
314 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
315 desc.format = WINED3DFMT_B5G6R5_UNORM;
316 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
317 desc.multisample_quality = 0;
318 desc.usage = WINED3DUSAGE_DYNAMIC;
319 desc.bind_flags = 0;
320 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
321 desc.width = bm.bmWidth;
322 desc.height = bm.bmHeight;
323 desc.depth = 1;
324 desc.size = 0;
325 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
326 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
328 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
329 goto out;
332 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
334 wined3d_texture_decref(device->logo_texture);
335 device->logo_texture = NULL;
336 goto out;
338 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
339 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
341 color_key.color_space_low_value = 0;
342 color_key.color_space_high_value = 0;
343 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
345 out:
346 if (dcb) DeleteDC(dcb);
347 if (hbm) DeleteObject(hbm);
350 /* Context activation is done by the caller. */
351 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
352 struct wined3d_context_gl *context_gl)
354 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
355 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
356 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
357 unsigned int i;
358 DWORD color;
360 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
361 color = 0x000000ff;
362 else
363 color = 0x00000000;
365 /* Under DirectX you can sample even if no texture is bound, whereas
366 * OpenGL will only allow that when a valid texture is bound.
367 * We emulate this by creating dummy textures and binding them
368 * to each texture stage when the currently set D3D texture is NULL. */
369 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
371 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
372 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
373 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
374 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
375 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
377 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
378 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
379 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
380 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
381 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
383 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
385 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
386 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
387 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
388 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
389 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
392 if (gl_info->supported[EXT_TEXTURE3D])
394 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
395 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
396 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
397 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
398 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
401 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
403 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
404 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
405 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
406 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
408 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
409 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
413 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
415 DWORD cube_array_data[6];
417 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
418 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
419 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
420 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
421 cube_array_data[i] = color;
422 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
423 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
426 if (gl_info->supported[EXT_TEXTURE_ARRAY])
428 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
429 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
430 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
431 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
432 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
434 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
435 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
436 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
437 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
438 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
441 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
443 GLuint buffer;
445 GL_EXTCALL(glGenBuffers(1, &buffer));
446 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
447 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
448 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
450 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
451 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
452 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
453 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
454 GL_EXTCALL(glDeleteBuffers(1, &buffer));
457 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
459 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
460 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
461 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
462 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
464 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
465 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
466 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
467 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
469 if (gl_info->supported[ARB_CLEAR_TEXTURE])
471 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
472 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
474 else
476 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
480 checkGLcall("create dummy textures");
482 wined3d_context_gl_bind_dummy_textures(context_gl);
485 /* Context activation is done by the caller. */
486 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
487 struct wined3d_context_gl *context_gl)
489 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
490 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
492 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
494 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
495 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
498 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
499 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
501 if (gl_info->supported[EXT_TEXTURE_ARRAY])
503 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
504 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
507 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
508 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
510 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
511 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
513 if (gl_info->supported[EXT_TEXTURE3D])
514 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
516 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
517 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
519 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
520 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
522 checkGLcall("delete dummy textures");
524 memset(dummy_textures, 0, sizeof(*dummy_textures));
527 /* Context activation is done by the caller. */
528 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
530 struct wined3d_sampler_desc desc;
531 HRESULT hr;
533 desc.address_u = WINED3D_TADDRESS_WRAP;
534 desc.address_v = WINED3D_TADDRESS_WRAP;
535 desc.address_w = WINED3D_TADDRESS_WRAP;
536 memset(desc.border_color, 0, sizeof(desc.border_color));
537 desc.mag_filter = WINED3D_TEXF_POINT;
538 desc.min_filter = WINED3D_TEXF_POINT;
539 desc.mip_filter = WINED3D_TEXF_NONE;
540 desc.lod_bias = 0.0f;
541 desc.min_lod = -1000.0f;
542 desc.max_lod = 1000.0f;
543 desc.mip_base_level = 0;
544 desc.max_anisotropy = 1;
545 desc.compare = FALSE;
546 desc.comparison_func = WINED3D_CMP_NEVER;
547 desc.srgb_decode = TRUE;
549 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
550 * instructions allow access to resources without using samplers.
551 * In GLSL, resources are always accessed through sampler or image variables. The default
552 * sampler object is used to emulate the direct resource access when there is no sampler state
553 * to use.
555 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
557 ERR("Failed to create default sampler, hr %#x.\n", hr);
558 device->default_sampler = NULL;
561 /* In D3D10+, a NULL sampler maps to the default sampler state. */
562 desc.address_u = WINED3D_TADDRESS_CLAMP;
563 desc.address_v = WINED3D_TADDRESS_CLAMP;
564 desc.address_w = WINED3D_TADDRESS_CLAMP;
565 desc.mag_filter = WINED3D_TEXF_LINEAR;
566 desc.min_filter = WINED3D_TEXF_LINEAR;
567 desc.mip_filter = WINED3D_TEXF_LINEAR;
568 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
570 ERR("Failed to create null sampler, hr %#x.\n", hr);
571 device->null_sampler = NULL;
575 /* Context activation is done by the caller. */
576 void wined3d_device_destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
578 wined3d_sampler_decref(device->default_sampler);
579 device->default_sampler = NULL;
580 wined3d_sampler_decref(device->null_sampler);
581 device->null_sampler = NULL;
584 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
585 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
587 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
588 VkImageSubresourceRange range;
589 uint32_t flags = 0;
591 static const VkClearColorValue colour = {{0}};
593 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
594 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
596 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
597 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
599 if (!wined3d_context_vk_create_image(context_vk, type,
600 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
601 1, 1, 1, sample_count, 1, layer_count, flags, image))
603 return false;
606 wined3d_context_vk_reference_image(context_vk, image);
608 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
609 range.baseMipLevel = 0;
610 range.levelCount = 1;
611 range.baseArrayLayer = 0;
612 range.layerCount = layer_count;
614 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
615 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
616 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
618 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
619 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
621 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
622 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
623 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
625 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
626 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
628 return true;
631 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
632 struct wined3d_context_vk *context_vk)
634 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
635 const struct wined3d_vk_info *vk_info;
636 const struct wined3d_format *format;
637 VkMemoryPropertyFlags memory_type;
638 VkCommandBuffer vk_command_buffer;
639 unsigned int sample_count = 2;
640 VkBufferUsageFlags usage;
642 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
643 while (sample_count && !(sample_count & format->multisample_types))
644 sample_count <<= 1;
646 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
648 ERR("Failed to get command buffer.\n");
649 return false;
652 vk_info = context_vk->vk_info;
654 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
655 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
656 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
657 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
658 return false;
659 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.buffer_offset, r->bo.size, 0x00000000u));
660 r->buffer_info.buffer = r->bo.vk_buffer;
661 r->buffer_info.offset = r->bo.buffer_offset;
662 r->buffer_info.range = r->bo.size;
664 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
666 ERR("Failed to create 1D image.\n");
667 goto fail;
670 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
672 ERR("Failed to create 2D image.\n");
673 goto fail;
676 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
678 ERR("Failed to create 2D MSAA image.\n");
679 goto fail;
682 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
684 ERR("Failed to create 3D image.\n");
685 goto fail;
688 return true;
690 fail:
691 if (r->image_2dms.vk_image)
692 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
693 if (r->image_2d.vk_image)
694 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
695 if (r->image_1d.vk_image)
696 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
697 wined3d_context_vk_reference_bo(context_vk, &r->bo);
698 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
699 return false;
702 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
703 struct wined3d_context_vk *context_vk)
705 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
707 /* We don't track command buffer references to NULL resources. We easily
708 * could, but it doesn't seem worth it. */
709 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
710 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
711 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
712 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
713 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
714 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
715 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
716 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
717 wined3d_context_vk_reference_bo(context_vk, &r->bo);
718 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
721 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
723 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
724 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
725 VkBufferViewCreateInfo buffer_create_info;
726 const struct wined3d_vk_info *vk_info;
727 VkImageViewCreateInfo view_desc;
728 VkResult vr;
730 vk_info = context_vk->vk_info;
732 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
733 buffer_create_info.pNext = NULL;
734 buffer_create_info.flags = 0;
735 buffer_create_info.buffer = r->bo.vk_buffer;
736 buffer_create_info.format = VK_FORMAT_R32_UINT;
737 buffer_create_info.offset = r->bo.buffer_offset;
738 buffer_create_info.range = r->bo.size;
740 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
741 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
743 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
744 return false;
746 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
748 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
749 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
750 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
752 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
753 goto fail;
755 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
757 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
758 view_desc.pNext = NULL;
759 view_desc.flags = 0;
760 view_desc.image = r->image_1d.vk_image;
761 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
762 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
763 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
764 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
765 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
766 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
767 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
768 view_desc.subresourceRange.baseMipLevel = 0;
769 view_desc.subresourceRange.levelCount = 1;
770 view_desc.subresourceRange.baseArrayLayer = 0;
771 view_desc.subresourceRange.layerCount = 1;
772 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
774 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
775 goto fail;
777 v->vk_info_1d.sampler = VK_NULL_HANDLE;
778 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
779 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
781 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
782 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
784 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
785 goto fail;
787 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
788 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
789 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
791 view_desc.image = r->image_2d.vk_image;
792 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
793 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
795 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
796 goto fail;
798 v->vk_info_2d.sampler = VK_NULL_HANDLE;
799 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
800 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
802 view_desc.image = r->image_2dms.vk_image;
803 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
804 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
806 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
807 goto fail;
809 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
810 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
811 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
813 view_desc.image = r->image_3d.vk_image;
814 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
815 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
817 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
818 goto fail;
820 v->vk_info_3d.sampler = VK_NULL_HANDLE;
821 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
822 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
824 view_desc.image = r->image_2d.vk_image;
825 view_desc.subresourceRange.layerCount = 6;
826 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
827 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
829 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
830 goto fail;
832 v->vk_info_cube.sampler = VK_NULL_HANDLE;
833 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
834 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
836 view_desc.subresourceRange.layerCount = 1;
837 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
838 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
840 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
841 goto fail;
843 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
844 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
845 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
847 view_desc.image = r->image_2dms.vk_image;
848 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
849 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
851 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
852 goto fail;
854 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
855 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
856 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
858 return true;
860 fail:
861 if (v->vk_info_2d_array.imageView)
862 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
863 if (v->vk_info_cube.imageView)
864 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
865 if (v->vk_info_3d.imageView)
866 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
867 if (v->vk_info_2dms.imageView)
868 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
869 if (v->vk_info_2d.imageView)
870 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
871 if (v->vk_info_1d_array.imageView)
872 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
873 if (v->vk_info_1d.imageView)
874 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
875 if (v->vk_view_buffer_float)
876 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
877 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
878 return false;
881 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
883 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
884 uint64_t id = context_vk->current_command_buffer.id;
886 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
887 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
888 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
889 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
890 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
891 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
892 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
893 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
895 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
896 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
899 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
901 unsigned int screensaver_active;
903 TRACE("device %p, window %p.\n", device, window);
905 if (!wined3d_register_window(NULL, window, device, 0))
907 ERR("Failed to register window %p.\n", window);
908 return E_FAIL;
911 InterlockedExchangePointer((void **)&device->focus_window, window);
912 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
913 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
914 if ((device->restore_screensaver = !!screensaver_active))
915 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
917 return WINED3D_OK;
920 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
922 TRACE("device %p.\n", device);
924 if (device->focus_window) wined3d_unregister_window(device->focus_window);
925 InterlockedExchangePointer((void **)&device->focus_window, NULL);
926 if (device->restore_screensaver)
928 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
929 device->restore_screensaver = FALSE;
933 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
935 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
936 struct wined3d_device_context *context = &device->cs->c;
937 unsigned int i;
939 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
941 wined3d_device_context_set_rendertarget_view(context, i, NULL, FALSE);
943 if (device->back_buffer_view)
944 wined3d_device_context_set_rendertarget_view(context, 0, device->back_buffer_view, TRUE);
946 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
949 void wined3d_device_delete_opengl_contexts_cs(void *object)
951 struct wined3d_swapchain_gl *swapchain_gl;
952 struct wined3d_device *device = object;
953 struct wined3d_context_gl *context_gl;
954 struct wined3d_device_gl *device_gl;
955 struct wined3d_context *context;
956 struct wined3d_shader *shader;
958 TRACE("device %p.\n", device);
960 device_gl = wined3d_device_gl(device);
962 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
964 device->shader_backend->shader_destroy(shader);
967 context = context_acquire(device, NULL, 0);
968 context_gl = wined3d_context_gl(context);
969 device->blitter->ops->blitter_destroy(device->blitter, context);
970 device->shader_backend->shader_free_private(device, context);
971 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
972 wined3d_device_destroy_default_samplers(device, context);
973 context_release(context);
975 while (device->context_count)
977 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
978 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
979 else
980 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
984 void wined3d_device_create_primary_opengl_context_cs(void *object)
986 struct wined3d_device *device = object;
987 struct wined3d_context_gl *context_gl;
988 struct wined3d_swapchain *swapchain;
989 struct wined3d_context *context;
990 struct wined3d_texture *target;
991 HRESULT hr;
993 TRACE("device %p.\n", device);
995 swapchain = device->swapchains[0];
996 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
997 if (!(context = context_acquire(device, target, 0)))
999 WARN("Failed to acquire context.\n");
1000 return;
1003 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1004 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1006 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1007 context_release(context);
1008 return;
1011 if (!(device->blitter = wined3d_cpu_blitter_create()))
1013 ERR("Failed to create CPU blitter.\n");
1014 device->shader_backend->shader_free_private(device, NULL);
1015 context_release(context);
1016 return;
1019 context_gl = wined3d_context_gl(context);
1021 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1022 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1023 wined3d_arbfp_blitter_create(&device->blitter, device);
1024 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1025 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1027 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device), context_gl);
1028 wined3d_device_create_default_samplers(device, context);
1029 context_release(context);
1032 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1034 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1035 const struct wined3d_swapchain_desc *swapchain_desc;
1036 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1037 DWORD clear_flags = 0;
1038 unsigned int i;
1039 HRESULT hr;
1041 TRACE("device %p, swapchain %p.\n", device, swapchain);
1043 if (device->d3d_initialized)
1044 return WINED3DERR_INVALIDCALL;
1046 device->swapchain_count = 1;
1047 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1049 ERR("Failed to allocate swapchain array.\n");
1050 hr = E_OUTOFMEMORY;
1051 goto err_out;
1053 device->swapchains[0] = swapchain;
1055 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1057 if (fb->render_targets[i])
1058 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1060 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1062 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1063 goto err_out;
1064 device->d3d_initialized = TRUE;
1066 swapchain_desc = &swapchain->state.desc;
1067 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1069 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1070 struct wined3d_view_desc view_desc;
1072 view_desc.format_id = back_buffer->format->id;
1073 view_desc.flags = 0;
1074 view_desc.u.texture.level_idx = 0;
1075 view_desc.u.texture.level_count = 1;
1076 view_desc.u.texture.layer_idx = 0;
1077 view_desc.u.texture.layer_count = 1;
1078 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1079 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1081 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1082 device->adapter->adapter_ops->adapter_uninit_3d(device);
1083 device->d3d_initialized = FALSE;
1084 goto err_out;
1088 device_init_swapchain_state(device, swapchain);
1090 TRACE("All defaults now set up.\n");
1092 /* Clear the screen. */
1093 if (device->back_buffer_view)
1094 clear_flags |= WINED3DCLEAR_TARGET;
1095 if (swapchain_desc->enable_auto_depth_stencil)
1096 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1097 if (clear_flags)
1098 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1100 if (wined3d_settings.logo)
1101 device_load_logo(device, wined3d_settings.logo);
1103 return WINED3D_OK;
1105 err_out:
1106 heap_free(device->swapchains);
1107 device->swapchains = NULL;
1108 device->swapchain_count = 0;
1110 return hr;
1113 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1115 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1117 wined3d_sampler_decref(sampler);
1120 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1122 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1124 wined3d_rasterizer_state_decref(state);
1127 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1129 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1131 wined3d_blend_state_decref(blend_state);
1134 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1136 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1138 wined3d_depth_stencil_state_decref(state);
1141 void wined3d_device_uninit_3d(struct wined3d_device *device)
1143 struct wined3d_state *state = device->cs->c.state;
1144 struct wined3d_resource *resource, *cursor;
1145 struct wined3d_rendertarget_view *view;
1146 struct wined3d_texture *texture;
1148 TRACE("device %p.\n", device);
1150 if (!device->d3d_initialized)
1152 ERR("Called while 3D support was not initialised.\n");
1153 return;
1156 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1158 device->swapchain_count = 0;
1160 if ((texture = device->logo_texture))
1162 device->logo_texture = NULL;
1163 wined3d_texture_decref(texture);
1166 if ((texture = device->cursor_texture))
1168 device->cursor_texture = NULL;
1169 wined3d_texture_decref(texture);
1172 wined3d_device_context_emit_reset_state(&device->cs->c, false);
1173 state_cleanup(state);
1175 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1176 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1177 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1178 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1180 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1182 TRACE("Unloading resource %p.\n", resource);
1183 wined3d_cs_emit_unload_resource(device->cs, resource);
1186 device->adapter->adapter_ops->adapter_uninit_3d(device);
1187 device->d3d_initialized = FALSE;
1189 if ((view = device->auto_depth_stencil_view))
1191 device->auto_depth_stencil_view = NULL;
1192 if (wined3d_rendertarget_view_decref(view))
1193 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1196 if ((view = device->back_buffer_view))
1198 device->back_buffer_view = NULL;
1199 wined3d_rendertarget_view_decref(view);
1202 heap_free(device->swapchains);
1203 device->swapchains = NULL;
1205 wined3d_state_reset(state, &device->adapter->d3d_info);
1208 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1209 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1210 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1212 * There is no way to deactivate thread safety once it is enabled.
1214 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1216 TRACE("device %p.\n", device);
1218 /* For now just store the flag (needed in case of ddraw). */
1219 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1222 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1224 const struct wined3d_driver_info *driver_info;
1226 TRACE("device %p.\n", device);
1228 driver_info = &device->adapter->driver_info;
1230 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1231 wine_dbgstr_longlong(driver_info->vram_bytes),
1232 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1233 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1235 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1238 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1239 unsigned int idx, unsigned int *offset)
1241 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1243 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1245 WARN("Invalid stream output %u.\n", idx);
1246 return NULL;
1249 if (offset)
1250 *offset = context->state->stream_output[idx].offset;
1251 return context->state->stream_output[idx].buffer;
1254 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1255 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1257 const struct wined3d_stream_state *stream;
1259 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1260 context, stream_idx, buffer, offset, stride);
1262 if (stream_idx >= WINED3D_MAX_STREAMS)
1264 WARN("Stream index %u out of range.\n", stream_idx);
1265 return WINED3DERR_INVALIDCALL;
1268 stream = &context->state->streams[stream_idx];
1269 *buffer = stream->buffer;
1270 if (offset)
1271 *offset = stream->offset;
1272 *stride = stream->stride;
1274 return WINED3D_OK;
1277 static void wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1279 struct wined3d_stream_state *stream;
1280 UINT old_flags, old_freq;
1282 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1284 stream = &device->cs->c.state->streams[stream_idx];
1285 old_flags = stream->flags;
1286 old_freq = stream->frequency;
1288 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1289 stream->frequency = divider & 0x7fffff;
1290 if (stream->frequency != old_freq || stream->flags != old_flags)
1291 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1294 static void wined3d_device_set_transform(struct wined3d_device *device,
1295 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1297 TRACE("device %p, state %s, matrix %p.\n",
1298 device, debug_d3dtstype(state), matrix);
1299 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1300 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1301 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1302 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1304 /* If the new matrix is the same as the current one,
1305 * we cut off any further processing. this seems to be a reasonable
1306 * optimization because as was noticed, some apps (warcraft3 for example)
1307 * tend towards setting the same matrix repeatedly for some reason.
1309 * From here on we assume that the new matrix is different, wherever it matters. */
1310 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1312 TRACE("The application is setting the same matrix over again.\n");
1313 return;
1316 device->cs->c.state->transforms[state] = *matrix;
1317 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
1320 static void wined3d_device_get_transform(const struct wined3d_device *device,
1321 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1323 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1325 *matrix = device->cs->c.state->transforms[state];
1328 /* Note lights are real special cases. Although the device caps state only
1329 * e.g. 8 are supported, you can reference any indexes you want as long as
1330 * that number max are enabled at any one point in time. Therefore since the
1331 * indices can be anything, we need a hashmap of them. However, this causes
1332 * stateblock problems. When capturing the state block, I duplicate the
1333 * hashmap, but when recording, just build a chain pretty much of commands to
1334 * be replayed. */
1335 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
1336 unsigned int light_idx, const struct wined3d_light *light)
1338 struct wined3d_light_info *object = NULL;
1339 float rho;
1341 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
1342 return;
1344 /* Initialize the object. */
1345 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1346 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1347 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1348 light_idx, light->type, debug_color(&light->diffuse),
1349 debug_color(&light->specular), debug_color(&light->ambient),
1350 light->position.x, light->position.y, light->position.z,
1351 light->direction.x, light->direction.y, light->direction.z,
1352 light->range, light->falloff, light->theta, light->phi);
1354 switch (light->type)
1356 case WINED3D_LIGHT_POINT:
1357 /* Position */
1358 object->position.x = light->position.x;
1359 object->position.y = light->position.y;
1360 object->position.z = light->position.z;
1361 object->position.w = 1.0f;
1362 object->cutoff = 180.0f;
1363 /* FIXME: Range */
1364 break;
1366 case WINED3D_LIGHT_DIRECTIONAL:
1367 /* Direction */
1368 object->direction.x = -light->direction.x;
1369 object->direction.y = -light->direction.y;
1370 object->direction.z = -light->direction.z;
1371 object->direction.w = 0.0f;
1372 object->exponent = 0.0f;
1373 object->cutoff = 180.0f;
1374 break;
1376 case WINED3D_LIGHT_SPOT:
1377 /* Position */
1378 object->position.x = light->position.x;
1379 object->position.y = light->position.y;
1380 object->position.z = light->position.z;
1381 object->position.w = 1.0f;
1383 /* Direction */
1384 object->direction.x = light->direction.x;
1385 object->direction.y = light->direction.y;
1386 object->direction.z = light->direction.z;
1387 object->direction.w = 0.0f;
1389 /* opengl-ish and d3d-ish spot lights use too different models
1390 * for the light "intensity" as a function of the angle towards
1391 * the main light direction, so we only can approximate very
1392 * roughly. However, spot lights are rather rarely used in games
1393 * (if ever used at all). Furthermore if still used, probably
1394 * nobody pays attention to such details. */
1395 if (!light->falloff)
1397 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1398 * equations have the falloff resp. exponent parameter as an
1399 * exponent, so the spot light lighting will always be 1.0 for
1400 * both of them, and we don't have to care for the rest of the
1401 * rather complex calculation. */
1402 object->exponent = 0.0f;
1404 else
1406 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1407 if (rho < 0.0001f)
1408 rho = 0.0001f;
1409 object->exponent = -0.3f / logf(cosf(rho / 2));
1412 if (object->exponent > 128.0f)
1413 object->exponent = 128.0f;
1415 object->cutoff = (float)(light->phi * 90 / M_PI);
1416 /* FIXME: Range */
1417 break;
1419 case WINED3D_LIGHT_PARALLELPOINT:
1420 object->position.x = light->position.x;
1421 object->position.y = light->position.y;
1422 object->position.z = light->position.z;
1423 object->position.w = 1.0f;
1424 break;
1426 default:
1427 FIXME("Unrecognized light type %#x.\n", light->type);
1430 wined3d_device_context_emit_set_light(context, object);
1433 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1435 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1436 struct wined3d_light_info *light_info;
1438 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1440 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1441 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1443 TRACE("Light enabled requested but light not defined, so defining one!\n");
1444 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
1446 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1448 FIXME("Adding default lights has failed dismally\n");
1449 return;
1453 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1454 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
1457 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1458 UINT plane_idx, const struct wined3d_vec4 *plane)
1460 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1462 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1464 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1466 TRACE("Application has requested clipplane this device doesn't support.\n");
1467 return WINED3DERR_INVALIDCALL;
1470 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1472 TRACE("Application is setting old values over, nothing to do.\n");
1473 return WINED3D_OK;
1476 clip_planes[plane_idx] = *plane;
1478 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
1480 return WINED3D_OK;
1483 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1484 const struct wined3d_clip_status *clip_status)
1486 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1488 if (!clip_status)
1489 return WINED3DERR_INVALIDCALL;
1491 return WINED3D_OK;
1494 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1495 struct wined3d_clip_status *clip_status)
1497 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1499 if (!clip_status)
1500 return WINED3DERR_INVALIDCALL;
1502 return WINED3D_OK;
1505 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1507 TRACE("device %p, material %p.\n", device, material);
1509 device->cs->c.state->material = *material;
1510 wined3d_device_context_emit_set_material(&device->cs->c, material);
1513 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1514 enum wined3d_format_id *format, unsigned int *offset)
1516 const struct wined3d_state *state = context->state;
1518 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1520 *format = state->index_format;
1521 if (offset)
1522 *offset = state->index_offset;
1523 return state->index_buffer;
1526 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
1528 TRACE("device %p, base_index %d.\n", device, base_index);
1530 device->cs->c.state->base_vertex_index = base_index;
1533 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1534 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1536 const struct wined3d_state *state = context->state;
1537 unsigned int count;
1539 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1541 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1542 if (count && viewports)
1543 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1544 if (viewport_count)
1545 *viewport_count = state->viewport_count;
1548 static void resolve_depth_buffer(struct wined3d_device *device)
1550 const struct wined3d_state *state = device->cs->c.state;
1551 struct wined3d_rendertarget_view *src_view;
1552 struct wined3d_resource *dst_resource;
1553 struct wined3d_texture *dst_texture;
1555 if (!(dst_texture = state->textures[0]))
1556 return;
1557 dst_resource = &dst_texture->resource;
1558 if (!dst_resource->format->depth_size)
1559 return;
1560 if (!(src_view = state->fb.depth_stencil))
1561 return;
1563 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1564 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1567 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1568 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1570 const struct wined3d_state *state = context->state;
1572 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1574 *blend_factor = state->blend_factor;
1575 *sample_mask = state->sample_mask;
1576 return state->blend_state;
1579 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1580 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1582 const struct wined3d_state *state = context->state;
1584 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1586 *stencil_ref = state->stencil_ref;
1587 return state->depth_stencil_state;
1590 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1591 struct wined3d_device_context *context)
1593 TRACE("context %p.\n", context);
1595 return context->state->rasterizer_state;
1598 static void wined3d_device_set_render_state(struct wined3d_device *device,
1599 enum wined3d_render_state state, DWORD value)
1601 if (state > WINEHIGHEST_RENDER_STATE)
1603 WARN("Unhandled render state %#x.\n", state);
1604 return;
1607 if (value == device->cs->c.state->render_states[state])
1608 TRACE("Application is setting the old value over, nothing to do.\n");
1609 else
1611 device->cs->c.state->render_states[state] = value;
1612 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
1615 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1617 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1618 resolve_depth_buffer(device);
1622 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1623 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1625 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1626 device, sampler_idx, debug_d3dsamplerstate(state), value);
1628 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1630 TRACE("Application is setting the old value over, nothing to do.\n");
1631 return;
1634 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1635 wined3d_device_context_emit_set_sampler_state(&device->cs->c, sampler_idx, state, value);
1638 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1639 unsigned int *rect_count, RECT *rects)
1641 const struct wined3d_state *state = context->state;
1642 unsigned int count;
1644 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1646 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1647 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1648 if (rect_count)
1649 *rect_count = state->scissor_rect_count;
1652 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1654 TRACE("context %p.\n", context);
1656 state_cleanup(context->state);
1657 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1658 wined3d_device_context_emit_reset_state(context, true);
1661 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1663 const struct wined3d_light_info *light;
1664 unsigned int i, j;
1666 TRACE("context %p, state %p.\n", context, state);
1668 context->state = state;
1669 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1671 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
1673 wined3d_device_context_emit_set_rendertarget_view(context, i, state->fb.render_targets[i]);
1676 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1677 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1679 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
1681 wined3d_device_context_emit_set_stream_output(context, i,
1682 state->stream_output[i].buffer, state->stream_output[i].offset);
1685 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1687 wined3d_device_context_emit_set_stream_source(context, i, state->streams[i].buffer,
1688 state->streams[i].offset, state->streams[i].stride);
1691 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1692 state->index_format, state->index_offset);
1694 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1696 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1698 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1699 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1700 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
1702 wined3d_device_context_emit_set_sampler(context, i, j, state->sampler[i][j]);
1704 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
1706 wined3d_device_context_emit_set_shader_resource_view(context, i, j, state->shader_resource_view[i][j]);
1710 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1712 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
1714 wined3d_device_context_emit_set_unordered_access_view(context, i, j,
1715 state->unordered_access_view[i][j], ~0);
1719 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_F,
1720 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1721 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_I,
1722 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1723 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_B,
1724 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1726 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_F,
1727 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1728 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_I,
1729 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1730 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_B,
1731 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1733 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1735 wined3d_device_context_emit_set_texture(context, i, state->textures[i]);
1736 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1738 wined3d_device_context_emit_set_sampler_state(context, i, j, state->sampler_states[i][j]);
1742 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1744 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1746 wined3d_device_context_emit_set_texture_state(context, i, j, state->texture_states[i][j]);
1750 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1752 if (context->device->state_table[STATE_TRANSFORM(i)].representative)
1753 wined3d_device_context_emit_set_transform(context, i, state->transforms + i);
1756 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1758 wined3d_device_context_emit_set_clip_plane(context, i, state->clip_planes + i);
1761 wined3d_device_context_emit_set_material(context, &state->material);
1763 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1764 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1766 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1768 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1770 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
1771 wined3d_device_context_emit_set_light_enable(context, light->OriginalIndex, light->glIndex != -1);
1775 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1777 if (context->device->state_table[STATE_RENDER(i)].representative)
1778 wined3d_device_context_emit_set_render_state(context, i, state->render_states[i]);
1781 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1782 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1783 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1786 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1788 TRACE("device %p.\n", device);
1790 return device->cs->c.state;
1793 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1795 TRACE("device %p.\n", device);
1797 return &device->cs->c;
1800 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1801 const struct wined3d_device_context *context)
1803 TRACE("context %p.\n", context);
1805 return context->state->vertex_declaration;
1808 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1809 enum wined3d_shader_type type, struct wined3d_shader *shader)
1811 struct wined3d_state *state = context->state;
1812 struct wined3d_shader *prev;
1814 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1816 prev = state->shader[type];
1817 if (shader == prev)
1818 return;
1820 if (shader)
1821 wined3d_shader_incref(shader);
1822 state->shader[type] = shader;
1823 wined3d_device_context_emit_set_shader(context, type, shader);
1824 if (prev)
1825 wined3d_shader_decref(prev);
1828 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
1829 enum wined3d_shader_type type)
1831 TRACE("context %p, type %#x.\n", context, type);
1833 return context->state->shader[type];
1836 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
1837 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1838 struct wined3d_buffer *const *buffers)
1840 struct wined3d_state *state = context->state;
1841 unsigned int i;
1843 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
1845 if (start_idx >= MAX_CONSTANT_BUFFERS || count > MAX_CONSTANT_BUFFERS - start_idx)
1847 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
1848 return;
1851 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
1852 return;
1854 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
1855 for (i = 0; i < count; ++i)
1857 struct wined3d_buffer *prev = state->cb[type][start_idx + i];
1858 struct wined3d_buffer *buffer = buffers[i];
1860 if (buffer)
1861 wined3d_buffer_incref(buffer);
1862 state->cb[type][start_idx + i] = buffer;
1863 if (prev)
1864 wined3d_buffer_decref(prev);
1868 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1869 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1871 struct wined3d_state *state = context->state;
1872 struct wined3d_blend_state *prev;
1874 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1875 context, blend_state, blend_factor, sample_mask);
1877 prev = state->blend_state;
1878 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1879 && sample_mask == state->sample_mask)
1880 return;
1882 if (blend_state)
1883 wined3d_blend_state_incref(blend_state);
1884 state->blend_state = blend_state;
1885 state->blend_factor = *blend_factor;
1886 state->sample_mask = sample_mask;
1887 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1888 if (prev)
1889 wined3d_blend_state_decref(prev);
1892 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1893 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1895 struct wined3d_state *state = context->state;
1896 struct wined3d_depth_stencil_state *prev;
1898 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1900 prev = state->depth_stencil_state;
1901 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1902 return;
1904 if (depth_stencil_state)
1905 wined3d_depth_stencil_state_incref(depth_stencil_state);
1906 state->depth_stencil_state = depth_stencil_state;
1907 state->stencil_ref = stencil_ref;
1908 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1909 if (prev)
1910 wined3d_depth_stencil_state_decref(prev);
1913 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1914 struct wined3d_rasterizer_state *rasterizer_state)
1916 struct wined3d_state *state = context->state;
1917 struct wined3d_rasterizer_state *prev;
1919 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
1921 prev = state->rasterizer_state;
1922 if (prev == rasterizer_state)
1923 return;
1925 if (rasterizer_state)
1926 wined3d_rasterizer_state_incref(rasterizer_state);
1927 state->rasterizer_state = rasterizer_state;
1928 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
1929 if (prev)
1930 wined3d_rasterizer_state_decref(prev);
1933 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1934 const struct wined3d_viewport *viewports)
1936 struct wined3d_state *state = context->state;
1937 unsigned int i;
1939 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
1941 for (i = 0; i < viewport_count; ++i)
1943 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1944 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1947 if (viewport_count)
1948 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
1949 else
1950 memset(state->viewports, 0, sizeof(state->viewports));
1951 state->viewport_count = viewport_count;
1953 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
1956 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
1957 const RECT *rects)
1959 struct wined3d_state *state = context->state;
1960 unsigned int i;
1962 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
1964 for (i = 0; i < rect_count; ++i)
1966 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1969 if (state->scissor_rect_count == rect_count
1970 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
1972 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
1973 return;
1976 if (rect_count)
1977 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
1978 else
1979 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
1980 state->scissor_rect_count = rect_count;
1982 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
1985 void CDECL wined3d_device_context_set_shader_resource_view(struct wined3d_device_context *context,
1986 enum wined3d_shader_type type, unsigned int idx, struct wined3d_shader_resource_view *view)
1988 struct wined3d_state *state = context->state;
1989 const struct wined3d_rendertarget_view *dsv;
1990 struct wined3d_shader_resource_view *prev;
1992 TRACE("context %p, type %#x, idx %u, view %p.\n", context, type, idx, view);
1994 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
1996 WARN("Invalid view index %u.\n", idx);
1997 return;
2000 prev = state->shader_resource_view[type][idx];
2001 if (view == prev)
2002 return;
2004 if (view && (wined3d_is_srv_rtv_bound(state, view)
2005 || ((dsv = state->fb.depth_stencil)
2006 && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2008 WARN("Application is trying to bind resource which is attached as render target.\n");
2009 view = NULL;
2012 if (view)
2014 wined3d_shader_resource_view_incref(view);
2015 wined3d_srv_bind_count_inc(view);
2018 state->shader_resource_view[type][idx] = view;
2019 wined3d_device_context_emit_set_shader_resource_view(context, type, idx, view);
2020 if (prev)
2022 wined3d_srv_bind_count_dec(prev);
2023 wined3d_shader_resource_view_decref(prev);
2027 void CDECL wined3d_device_context_set_sampler(struct wined3d_device_context *context,
2028 enum wined3d_shader_type type, unsigned int idx, struct wined3d_sampler *sampler)
2030 struct wined3d_state *state = context->state;
2031 struct wined3d_sampler *prev;
2033 TRACE("context %p, type %#x, idx %u, sampler %p.\n", context, type, idx, sampler);
2035 if (idx >= MAX_SAMPLER_OBJECTS)
2037 WARN("Invalid sampler index %u.\n", idx);
2038 return;
2041 prev = state->sampler[type][idx];
2042 if (sampler == prev)
2043 return;
2045 if (sampler)
2046 wined3d_sampler_incref(sampler);
2047 state->sampler[type][idx] = sampler;
2048 wined3d_device_context_emit_set_sampler(context, type, idx, sampler);
2049 if (prev)
2050 wined3d_sampler_decref(prev);
2053 void CDECL wined3d_device_context_set_unordered_access_view(struct wined3d_device_context *context,
2054 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2055 unsigned int initial_count)
2057 struct wined3d_state *state = context->state;
2058 struct wined3d_unordered_access_view *prev;
2060 TRACE("context %p, pipeline %#x, idx %u, uav %p, initial_count %u.\n", context, pipeline, idx, uav, initial_count);
2062 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2064 WARN("Invalid UAV index %u.\n", idx);
2065 return;
2068 prev = state->unordered_access_view[pipeline][idx];
2069 if (uav == prev && initial_count == ~0u)
2070 return;
2072 if (uav)
2073 wined3d_unordered_access_view_incref(uav);
2074 state->unordered_access_view[pipeline][idx] = uav;
2075 wined3d_device_context_emit_set_unordered_access_view(context, pipeline, idx, uav, initial_count);
2076 if (prev)
2077 wined3d_unordered_access_view_decref(prev);
2080 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2081 const struct wined3d_rendertarget_view *view, BOOL dsv)
2083 const struct wined3d_state *state = context->state;
2084 const struct wined3d_resource *resource;
2086 if (!view)
2087 return;
2088 resource = view->resource;
2090 if (resource->srv_bind_count_device)
2092 const struct wined3d_shader_resource_view *srv;
2093 unsigned int i, j;
2095 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2097 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2099 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2100 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2101 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2103 WARN("Application sets bound resource as render target.\n");
2104 wined3d_device_context_set_shader_resource_view(context, i, j, NULL);
2111 HRESULT CDECL wined3d_device_context_set_rendertarget_view(struct wined3d_device_context *context,
2112 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
2114 struct wined3d_state *state = context->state;
2115 struct wined3d_rendertarget_view *prev;
2116 unsigned int max_rt_count;
2118 TRACE("context %p, view_idx %u, view %p, set_viewport %#x.\n",
2119 context, view_idx, view, set_viewport);
2121 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2122 if (view_idx >= max_rt_count)
2124 WARN("Only %u render targets are supported.\n", max_rt_count);
2125 return WINED3DERR_INVALIDCALL;
2128 if (view && !(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2130 WARN("View resource %p doesn't have render target bind flags.\n", view->resource);
2131 return WINED3DERR_INVALIDCALL;
2134 /* Set the viewport and scissor rectangles, if requested. Tests show that
2135 * stateblock recording is ignored, the change goes directly into the
2136 * primary stateblock. */
2137 if (!view_idx && set_viewport)
2139 state->viewports[0].x = 0;
2140 state->viewports[0].y = 0;
2141 state->viewports[0].width = view->width;
2142 state->viewports[0].height = view->height;
2143 state->viewports[0].min_z = 0.0f;
2144 state->viewports[0].max_z = 1.0f;
2145 state->viewport_count = 1;
2146 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2148 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
2149 state->scissor_rect_count = 1;
2150 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2153 prev = state->fb.render_targets[view_idx];
2154 if (view == prev)
2155 return WINED3D_OK;
2157 if (view)
2159 wined3d_rendertarget_view_incref(view);
2160 wined3d_rtv_bind_count_inc(view);
2162 state->fb.render_targets[view_idx] = view;
2163 wined3d_device_context_emit_set_rendertarget_view(context, view_idx, view);
2164 /* Release after the assignment, to prevent device_resource_released()
2165 * from seeing the surface as still in use. */
2166 if (prev)
2168 wined3d_rtv_bind_count_dec(prev);
2169 wined3d_rendertarget_view_decref(prev);
2172 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2174 return WINED3D_OK;
2177 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2178 struct wined3d_rendertarget_view *view)
2180 struct wined3d_fb_state *fb = &context->state->fb;
2181 struct wined3d_rendertarget_view *prev;
2183 TRACE("context %p, view %p.\n", context, view);
2185 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2187 WARN("View resource %p has incompatible %s bind flags.\n",
2188 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2189 return WINED3DERR_INVALIDCALL;
2192 prev = fb->depth_stencil;
2193 if (prev == view)
2195 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2196 return WINED3D_OK;
2199 if ((fb->depth_stencil = view))
2200 wined3d_rendertarget_view_incref(view);
2201 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2202 if (prev)
2203 wined3d_rendertarget_view_decref(prev);
2204 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2206 return WINED3D_OK;
2209 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2210 struct wined3d_query *predicate, BOOL value)
2212 struct wined3d_state *state = context->state;
2213 struct wined3d_query *prev;
2215 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2217 prev = state->predicate;
2218 if (predicate)
2220 FIXME("Predicated rendering not implemented.\n");
2221 wined3d_query_incref(predicate);
2223 state->predicate = predicate;
2224 state->predicate_value = value;
2225 wined3d_device_context_emit_set_predication(context, predicate, value);
2226 if (prev)
2227 wined3d_query_decref(prev);
2230 HRESULT CDECL wined3d_device_context_set_stream_source(struct wined3d_device_context *context,
2231 unsigned int stream_idx, struct wined3d_buffer *buffer, unsigned int offset, unsigned int stride)
2233 struct wined3d_stream_state *stream;
2234 struct wined3d_buffer *prev_buffer;
2236 TRACE("context %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2237 context, stream_idx, buffer, offset, stride);
2239 if (stream_idx >= WINED3D_MAX_STREAMS)
2241 WARN("Stream index %u out of range.\n", stream_idx);
2242 return WINED3DERR_INVALIDCALL;
2244 else if (offset & 0x3)
2246 WARN("Offset %u is not 4 byte aligned.\n", offset);
2247 return WINED3DERR_INVALIDCALL;
2250 stream = &context->state->streams[stream_idx];
2251 prev_buffer = stream->buffer;
2253 if (prev_buffer == buffer
2254 && stream->stride == stride
2255 && stream->offset == offset)
2257 TRACE("Application is setting the old values over, nothing to do.\n");
2258 return WINED3D_OK;
2261 stream->buffer = buffer;
2262 stream->stride = stride;
2263 stream->offset = offset;
2264 if (buffer)
2265 wined3d_buffer_incref(buffer);
2266 wined3d_device_context_emit_set_stream_source(context, stream_idx, buffer, offset, stride);
2267 if (prev_buffer)
2268 wined3d_buffer_decref(prev_buffer);
2270 return WINED3D_OK;
2273 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2274 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2276 struct wined3d_state *state = context->state;
2277 enum wined3d_format_id prev_format;
2278 struct wined3d_buffer *prev_buffer;
2279 unsigned int prev_offset;
2281 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2282 context, buffer, debug_d3dformat(format_id), offset);
2284 prev_buffer = state->index_buffer;
2285 prev_format = state->index_format;
2286 prev_offset = state->index_offset;
2288 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2289 return;
2291 if (buffer)
2292 wined3d_buffer_incref(buffer);
2293 state->index_buffer = buffer;
2294 state->index_format = format_id;
2295 state->index_offset = offset;
2296 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2297 if (prev_buffer)
2298 wined3d_buffer_decref(prev_buffer);
2301 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2302 struct wined3d_vertex_declaration *declaration)
2304 struct wined3d_state *state = context->state;
2305 struct wined3d_vertex_declaration *prev;
2307 TRACE("context %p, declaration %p.\n", context, declaration);
2309 prev = state->vertex_declaration;
2310 if (declaration == prev)
2311 return;
2313 if (declaration)
2314 wined3d_vertex_declaration_incref(declaration);
2315 state->vertex_declaration = declaration;
2316 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2317 if (prev)
2318 wined3d_vertex_declaration_decref(prev);
2321 void CDECL wined3d_device_context_set_stream_output(struct wined3d_device_context *context, unsigned int idx,
2322 struct wined3d_buffer *buffer, unsigned int offset)
2324 struct wined3d_stream_output *stream;
2325 struct wined3d_buffer *prev_buffer;
2327 TRACE("context %p, idx %u, buffer %p, offset %u.\n", context, idx, buffer, offset);
2329 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
2331 WARN("Invalid stream output %u.\n", idx);
2332 return;
2335 stream = &context->state->stream_output[idx];
2336 prev_buffer = stream->buffer;
2338 if (buffer)
2339 wined3d_buffer_incref(buffer);
2340 stream->buffer = buffer;
2341 stream->offset = offset;
2342 wined3d_device_context_emit_set_stream_output(context, idx, buffer, offset);
2343 if (prev_buffer)
2344 wined3d_buffer_decref(prev_buffer);
2347 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2348 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2350 struct wined3d_state *state = context->state;
2352 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2353 context, start_vertex, vertex_count, start_instance, instance_count);
2355 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2356 0, start_vertex, vertex_count, start_instance, instance_count, false);
2359 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2360 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2362 struct wined3d_state *state = context->state;
2364 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2365 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2367 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2368 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2371 struct wined3d_buffer * CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2372 enum wined3d_shader_type shader_type, unsigned int idx)
2374 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2376 if (idx >= MAX_CONSTANT_BUFFERS)
2378 WARN("Invalid constant buffer index %u.\n", idx);
2379 return NULL;
2382 return context->state->cb[shader_type][idx];
2385 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2386 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2388 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2390 WARN("Invalid view index %u.\n", idx);
2391 return NULL;
2394 return context->state->shader_resource_view[shader_type][idx];
2397 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2398 enum wined3d_shader_type shader_type, unsigned int idx)
2400 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2402 if (idx >= MAX_SAMPLER_OBJECTS)
2404 WARN("Invalid sampler index %u.\n", idx);
2405 return NULL;
2408 return context->state->sampler[shader_type][idx];
2411 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2412 unsigned int start_idx, unsigned int count, const BOOL *constants)
2414 unsigned int i;
2416 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2417 device, start_idx, count, constants);
2419 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2420 if (TRACE_ON(d3d))
2422 for (i = 0; i < count; ++i)
2423 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2426 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2429 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2430 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2432 unsigned int i;
2434 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2435 device, start_idx, count, constants);
2437 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2438 if (TRACE_ON(d3d))
2440 for (i = 0; i < count; ++i)
2441 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2444 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2447 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2448 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2450 unsigned int i;
2452 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2453 device, start_idx, count, constants);
2455 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2456 if (TRACE_ON(d3d))
2458 for (i = 0; i < count; ++i)
2459 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2462 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2465 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2466 unsigned int start_idx, unsigned int count, const BOOL *constants)
2468 unsigned int i;
2470 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2471 device, start_idx, count, constants);
2473 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2474 if (TRACE_ON(d3d))
2476 for (i = 0; i < count; ++i)
2477 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2480 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2483 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2484 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2486 unsigned int i;
2488 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2489 device, start_idx, count, constants);
2491 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2492 if (TRACE_ON(d3d))
2494 for (i = 0; i < count; ++i)
2495 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2498 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2501 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2502 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2504 unsigned int i;
2506 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2507 device, start_idx, count, constants);
2509 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2510 if (TRACE_ON(d3d))
2512 for (i = 0; i < count; ++i)
2513 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2516 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2519 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2520 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2522 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2524 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2526 WARN("Invalid UAV index %u.\n", idx);
2527 return NULL;
2530 return context->state->unordered_access_view[pipeline][idx];
2533 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2535 unsigned int i;
2537 if (!latency)
2538 latency = 3;
2540 device->max_frame_latency = latency;
2541 for (i = 0; i < device->swapchain_count; ++i)
2542 swapchain_set_max_frame_latency(device->swapchains[i], device);
2545 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2547 return device->max_frame_latency;
2550 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2552 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2553 unsigned int i, size = 0;
2555 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2556 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2557 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2558 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2559 switch (fvf & WINED3DFVF_POSITION_MASK)
2561 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2562 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2563 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2564 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2565 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2566 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2567 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2568 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2569 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2571 for (i = 0; i < texcoord_count; ++i)
2573 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2576 return size;
2579 static void wined3d_format_get_colour(const struct wined3d_format *format,
2580 const void *data, struct wined3d_color *colour)
2582 float *output = &colour->r;
2583 const uint32_t *u32_data;
2584 const uint16_t *u16_data;
2585 const float *f32_data;
2586 unsigned int i;
2588 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2589 static unsigned int warned;
2591 switch (format->id)
2593 case WINED3DFMT_B8G8R8A8_UNORM:
2594 u32_data = data;
2595 wined3d_color_from_d3dcolor(colour, *u32_data);
2596 break;
2598 case WINED3DFMT_R8G8B8A8_UNORM:
2599 u32_data = data;
2600 colour->r = (*u32_data & 0xffu) / 255.0f;
2601 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2602 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2603 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2604 break;
2606 case WINED3DFMT_R16G16_UNORM:
2607 case WINED3DFMT_R16G16B16A16_UNORM:
2608 u16_data = data;
2609 *colour = default_colour;
2610 for (i = 0; i < format->component_count; ++i)
2611 output[i] = u16_data[i] / 65535.0f;
2612 break;
2614 case WINED3DFMT_R32_FLOAT:
2615 case WINED3DFMT_R32G32_FLOAT:
2616 case WINED3DFMT_R32G32B32_FLOAT:
2617 case WINED3DFMT_R32G32B32A32_FLOAT:
2618 f32_data = data;
2619 *colour = default_colour;
2620 for (i = 0; i < format->component_count; ++i)
2621 output[i] = f32_data[i];
2622 break;
2624 default:
2625 *colour = default_colour;
2626 if (!warned++)
2627 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2628 break;
2632 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2633 const struct wined3d_color *material_colour, unsigned int index,
2634 const struct wined3d_stream_info *stream_info)
2636 const struct wined3d_stream_info_element *element = NULL;
2638 switch (mcs)
2640 case WINED3D_MCS_MATERIAL:
2641 *colour = *material_colour;
2642 return;
2644 case WINED3D_MCS_COLOR1:
2645 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2647 colour->r = colour->g = colour->b = colour->a = 1.0f;
2648 return;
2650 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2651 break;
2653 case WINED3D_MCS_COLOR2:
2654 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2656 colour->r = colour->g = colour->b = colour->a = 0.0f;
2657 return;
2659 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2660 break;
2662 default:
2663 colour->r = colour->g = colour->b = colour->a = 0.0f;
2664 ERR("Invalid material colour source %#x.\n", mcs);
2665 return;
2668 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
2671 static float wined3d_clamp(float value, float min_value, float max_value)
2673 return value < min_value ? min_value : value > max_value ? max_value : value;
2676 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2678 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
2681 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2683 v0->x -= v1->x;
2684 v0->y -= v1->y;
2685 v0->z -= v1->z;
2688 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
2690 v->x *= s;
2691 v->y *= s;
2692 v->z *= s;
2695 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
2697 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
2699 if (isfinite(rnorm))
2700 wined3d_vec3_scale(v, rnorm);
2703 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
2704 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
2706 struct wined3d_vec3 tmp;
2708 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
2709 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
2710 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
2712 *dst = tmp;
2715 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
2716 float min_value, float max_value)
2718 dst->r = wined3d_clamp(src->r, min_value, max_value);
2719 dst->g = wined3d_clamp(src->g, min_value, max_value);
2720 dst->b = wined3d_clamp(src->b, min_value, max_value);
2721 dst->a = wined3d_clamp(src->a, min_value, max_value);
2724 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
2726 dst->r += src->r * c;
2727 dst->g += src->g * c;
2728 dst->b += src->b * c;
2731 static void init_transformed_lights(struct lights_settings *ls,
2732 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
2734 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
2735 const struct wined3d_light_info *light_info;
2736 struct light_transformed *light;
2737 struct wined3d_vec4 vec4;
2738 unsigned int light_count;
2739 unsigned int i, index;
2741 memset(ls, 0, sizeof(*ls));
2743 ls->lighting = !!compute_lighting;
2744 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
2745 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
2746 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
2747 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
2748 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
2749 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
2751 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
2752 return;
2754 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
2755 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
2757 if (!compute_lighting)
2758 return;
2760 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
2762 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
2763 ls->legacy_lighting = !!legacy_lighting;
2764 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
2765 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
2767 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
2769 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
2771 if (!light_info->enabled)
2772 continue;
2774 switch (light_info->OriginalParms.type)
2776 case WINED3D_LIGHT_DIRECTIONAL:
2777 ++ls->directional_light_count;
2778 break;
2780 case WINED3D_LIGHT_POINT:
2781 ++ls->point_light_count;
2782 break;
2784 case WINED3D_LIGHT_SPOT:
2785 ++ls->spot_light_count;
2786 break;
2788 case WINED3D_LIGHT_PARALLELPOINT:
2789 ++ls->parallel_point_light_count;
2790 break;
2792 default:
2793 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
2794 continue;
2796 lights[index++] = light_info;
2797 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
2798 break;
2802 light_count = index;
2803 for (i = 0, index = 0; i < light_count; ++i)
2805 light_info = lights[i];
2806 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
2807 continue;
2809 light = &ls->lights[index];
2810 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2811 light->direction = *(struct wined3d_vec3 *)&vec4;
2812 wined3d_vec3_normalise(&light->direction);
2814 light->diffuse = light_info->OriginalParms.diffuse;
2815 light->ambient = light_info->OriginalParms.ambient;
2816 light->specular = light_info->OriginalParms.specular;
2817 ++index;
2820 for (i = 0; i < light_count; ++i)
2822 light_info = lights[i];
2823 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
2824 continue;
2826 light = &ls->lights[index];
2828 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2829 light->range = light_info->OriginalParms.range;
2830 light->c_att = light_info->OriginalParms.attenuation0;
2831 light->l_att = light_info->OriginalParms.attenuation1;
2832 light->q_att = light_info->OriginalParms.attenuation2;
2834 light->diffuse = light_info->OriginalParms.diffuse;
2835 light->ambient = light_info->OriginalParms.ambient;
2836 light->specular = light_info->OriginalParms.specular;
2837 ++index;
2840 for (i = 0; i < light_count; ++i)
2842 light_info = lights[i];
2843 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
2844 continue;
2846 light = &ls->lights[index];
2848 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2849 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2850 light->direction = *(struct wined3d_vec3 *)&vec4;
2851 wined3d_vec3_normalise(&light->direction);
2852 light->range = light_info->OriginalParms.range;
2853 light->falloff = light_info->OriginalParms.falloff;
2854 light->c_att = light_info->OriginalParms.attenuation0;
2855 light->l_att = light_info->OriginalParms.attenuation1;
2856 light->q_att = light_info->OriginalParms.attenuation2;
2857 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
2858 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
2860 light->diffuse = light_info->OriginalParms.diffuse;
2861 light->ambient = light_info->OriginalParms.ambient;
2862 light->specular = light_info->OriginalParms.specular;
2863 ++index;
2866 for (i = 0; i < light_count; ++i)
2868 light_info = lights[i];
2869 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
2870 continue;
2872 light = &ls->lights[index];
2874 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2875 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
2876 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
2877 light->diffuse = light_info->OriginalParms.diffuse;
2878 light->ambient = light_info->OriginalParms.ambient;
2879 light->specular = light_info->OriginalParms.specular;
2880 ++index;
2884 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
2885 const struct wined3d_vec3 *dir, float att, float material_shininess,
2886 const struct wined3d_vec3 *normal_transformed,
2887 const struct wined3d_vec3 *position_transformed_normalised,
2888 const struct light_transformed *light, const struct lights_settings *ls)
2890 struct wined3d_vec3 vec3;
2891 float t, c;
2893 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
2894 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
2896 vec3 = *dir;
2897 if (ls->localviewer)
2898 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
2899 else
2900 vec3.z -= 1.0f;
2901 wined3d_vec3_normalise(&vec3);
2902 t = wined3d_vec3_dot(normal_transformed, &vec3);
2903 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
2904 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
2905 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
2908 static void light_set_vertex_data(struct lights_settings *ls,
2909 const struct wined3d_vec4 *position)
2911 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
2912 return;
2914 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
2915 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
2918 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
2919 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
2920 float material_shininess)
2922 struct wined3d_vec3 position_transformed_normalised;
2923 struct wined3d_vec3 normal_transformed = {0.0f};
2924 const struct light_transformed *light;
2925 struct wined3d_vec3 dir, dst;
2926 unsigned int i, index;
2927 float att;
2929 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
2930 wined3d_vec3_normalise(&position_transformed_normalised);
2932 if (normal)
2934 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
2935 if (ls->normalise)
2936 wined3d_vec3_normalise(&normal_transformed);
2939 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
2940 *specular = *diffuse;
2941 *ambient = ls->ambient_light;
2943 index = 0;
2944 for (i = 0; i < ls->directional_light_count; ++i, ++index)
2946 light = &ls->lights[index];
2948 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
2949 if (normal)
2950 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
2951 &normal_transformed, &position_transformed_normalised, light, ls);
2954 for (i = 0; i < ls->point_light_count; ++i, ++index)
2956 light = &ls->lights[index];
2957 dir.x = light->position.x - ls->position_transformed.x;
2958 dir.y = light->position.y - ls->position_transformed.y;
2959 dir.z = light->position.z - ls->position_transformed.z;
2961 dst.z = wined3d_vec3_dot(&dir, &dir);
2962 dst.y = sqrtf(dst.z);
2963 dst.x = 1.0f;
2964 if (ls->legacy_lighting)
2966 dst.y = (light->range - dst.y) / light->range;
2967 if (!(dst.y > 0.0f))
2968 continue;
2969 dst.z = dst.y * dst.y;
2971 else
2973 if (!(dst.y <= light->range))
2974 continue;
2976 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
2977 if (!ls->legacy_lighting)
2978 att = 1.0f / att;
2980 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
2981 if (normal)
2983 wined3d_vec3_normalise(&dir);
2984 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
2985 &normal_transformed, &position_transformed_normalised, light, ls);
2989 for (i = 0; i < ls->spot_light_count; ++i, ++index)
2991 float t;
2993 light = &ls->lights[index];
2995 dir.x = light->position.x - ls->position_transformed.x;
2996 dir.y = light->position.y - ls->position_transformed.y;
2997 dir.z = light->position.z - ls->position_transformed.z;
2999 dst.z = wined3d_vec3_dot(&dir, &dir);
3000 dst.y = sqrtf(dst.z);
3001 dst.x = 1.0f;
3003 if (ls->legacy_lighting)
3005 dst.y = (light->range - dst.y) / light->range;
3006 if (!(dst.y > 0.0f))
3007 continue;
3008 dst.z = dst.y * dst.y;
3010 else
3012 if (!(dst.y <= light->range))
3013 continue;
3015 wined3d_vec3_normalise(&dir);
3016 t = -wined3d_vec3_dot(&dir, &light->direction);
3017 if (t > light->cos_htheta)
3018 att = 1.0f;
3019 else if (t <= light->cos_hphi)
3020 att = 0.0f;
3021 else
3022 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3024 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3025 if (ls->legacy_lighting)
3026 att *= t;
3027 else
3028 att /= t;
3030 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3032 if (normal)
3033 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3034 &normal_transformed, &position_transformed_normalised, light, ls);
3037 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3039 light = &ls->lights[index];
3041 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3042 if (normal)
3043 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3044 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3048 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3050 switch (ls->fog_mode)
3052 case WINED3D_FOG_NONE:
3053 return fog_coord;
3054 case WINED3D_FOG_LINEAR:
3055 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3056 case WINED3D_FOG_EXP:
3057 return expf(-fog_coord * ls->fog_density);
3058 case WINED3D_FOG_EXP2:
3059 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3060 default:
3061 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3062 return 0.0f;
3066 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3068 float fog_coord;
3070 if (ls->fog_mode == WINED3D_FOG_NONE)
3071 return;
3073 switch (ls->fog_coord_mode)
3075 case WINED3D_FFP_VS_FOG_RANGE:
3076 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3077 (const struct wined3d_vec3 *)&ls->position_transformed));
3078 break;
3080 case WINED3D_FFP_VS_FOG_DEPTH:
3081 fog_coord = fabsf(ls->position_transformed.z);
3082 break;
3084 default:
3085 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3086 return;
3088 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3091 /* Context activation is done by the caller. */
3092 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3093 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3094 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3096 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3097 const struct wined3d_color *material_specular_state_colour;
3098 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3099 const struct wined3d_state *state = device->cs->c.state;
3100 const struct wined3d_format *output_colour_format;
3101 static const struct wined3d_color black;
3102 struct wined3d_map_desc map_desc;
3103 struct wined3d_box box = {0};
3104 struct wined3d_viewport vp;
3105 unsigned int texture_count;
3106 struct lights_settings ls;
3107 unsigned int vertex_size;
3108 BOOL do_clip, lighting;
3109 float min_z, max_z;
3110 unsigned int i;
3111 BYTE *dest_ptr;
3112 HRESULT hr;
3114 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3116 ERR("Source has no position mask.\n");
3117 return WINED3DERR_INVALIDCALL;
3120 if (state->render_states[WINED3D_RS_CLIPPING])
3122 static BOOL warned = FALSE;
3124 * The clipping code is not quite correct. Some things need
3125 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3126 * so disable clipping for now.
3127 * (The graphics in Half-Life are broken, and my processvertices
3128 * test crashes with IDirect3DDevice3)
3129 do_clip = TRUE;
3131 do_clip = FALSE;
3132 if (!warned)
3134 warned = TRUE;
3135 FIXME("Clipping is broken and disabled for now\n");
3138 else
3139 do_clip = FALSE;
3141 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3142 box.left = dwDestIndex * vertex_size;
3143 box.right = box.left + dwCount * vertex_size;
3144 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3146 WARN("Failed to map buffer, hr %#x.\n", hr);
3147 return hr;
3149 dest_ptr = map_desc.data;
3151 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3152 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3153 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3155 TRACE("View mat:\n");
3156 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3157 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3158 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3159 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3161 TRACE("Proj mat:\n");
3162 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3163 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3164 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3165 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3167 TRACE("World mat:\n");
3168 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3169 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3170 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3171 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3173 /* Get the viewport */
3174 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3175 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3176 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3178 multiply_matrix(&mat,&view_mat,&world_mat);
3179 multiply_matrix(&mat,&proj_mat,&mat);
3181 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3183 lighting = state->render_states[WINED3D_RS_LIGHTING]
3184 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3185 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3186 &ambient_source, &specular_source, state, stream_info);
3187 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3188 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3189 ? &state->material.specular : &black;
3190 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3191 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3193 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3195 for (i = 0; i < dwCount; ++i)
3197 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3198 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3199 struct wined3d_color ambient, diffuse, specular;
3200 struct wined3d_vec4 position;
3201 unsigned int tex_index;
3203 position.x = p[0];
3204 position.y = p[1];
3205 position.z = p[2];
3206 position.w = 1.0f;
3208 light_set_vertex_data(&ls, &position);
3210 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3211 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3212 /* The position first */
3213 float x, y, z, rhw;
3214 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3216 /* Multiplication with world, view and projection matrix. */
3217 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3218 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3219 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3220 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3222 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3224 /* WARNING: The following things are taken from d3d7 and were not yet checked
3225 * against d3d8 or d3d9!
3228 /* Clipping conditions: From msdn
3230 * A vertex is clipped if it does not match the following requirements
3231 * -rhw < x <= rhw
3232 * -rhw < y <= rhw
3233 * 0 < z <= rhw
3234 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3236 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3237 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3241 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3242 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3244 /* "Normal" viewport transformation (not clipped)
3245 * 1) The values are divided by rhw
3246 * 2) The y axis is negative, so multiply it with -1
3247 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3248 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3249 * 4) Multiply x with Width/2 and add Width/2
3250 * 5) The same for the height
3251 * 6) Add the viewpoint X and Y to the 2D coordinates and
3252 * The minimum Z value to z
3253 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3255 * Well, basically it's simply a linear transformation into viewport
3256 * coordinates
3259 x /= rhw;
3260 y /= rhw;
3261 z /= rhw;
3263 y *= -1;
3265 x *= vp.width / 2;
3266 y *= vp.height / 2;
3267 z *= max_z - min_z;
3269 x += vp.width / 2 + vp.x;
3270 y += vp.height / 2 + vp.y;
3271 z += min_z;
3273 rhw = 1 / rhw;
3274 } else {
3275 /* That vertex got clipped
3276 * Contrary to OpenGL it is not dropped completely, it just
3277 * undergoes a different calculation.
3279 TRACE("Vertex got clipped\n");
3280 x += rhw;
3281 y += rhw;
3283 x /= 2;
3284 y /= 2;
3286 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3287 * outside of the main vertex buffer memory. That needs some more
3288 * investigation...
3292 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3295 ( (float *) dest_ptr)[0] = x;
3296 ( (float *) dest_ptr)[1] = y;
3297 ( (float *) dest_ptr)[2] = z;
3298 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3300 dest_ptr += 3 * sizeof(float);
3302 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3303 dest_ptr += sizeof(float);
3306 if (dst_fvf & WINED3DFVF_PSIZE)
3307 dest_ptr += sizeof(DWORD);
3309 if (dst_fvf & WINED3DFVF_NORMAL)
3311 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3312 const float *normal = (const float *)(element->data.addr + i * element->stride);
3313 /* AFAIK this should go into the lighting information */
3314 FIXME("Didn't expect the destination to have a normal\n");
3315 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3318 if (lighting)
3320 const struct wined3d_stream_info_element *element;
3321 struct wined3d_vec3 *normal;
3323 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3325 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3326 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3328 else
3330 normal = NULL;
3332 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3333 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3336 if (dst_fvf & WINED3DFVF_DIFFUSE)
3338 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3340 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3341 &state->material.diffuse, i, stream_info);
3343 if (lighting)
3345 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3346 &state->material.ambient, i, stream_info);
3347 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3348 &state->material.emissive, i, stream_info);
3350 diffuse_colour.r = ambient.r * material_ambient.r
3351 + diffuse.r * material_diffuse.r + material_emissive.r;
3352 diffuse_colour.g = ambient.g * material_ambient.g
3353 + diffuse.g * material_diffuse.g + material_emissive.g;
3354 diffuse_colour.b = ambient.b * material_ambient.b
3355 + diffuse.b * material_diffuse.b + material_emissive.b;
3356 diffuse_colour.a = material_diffuse.a;
3358 else
3360 diffuse_colour = material_diffuse;
3362 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3363 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3364 dest_ptr += sizeof(DWORD);
3367 if (dst_fvf & WINED3DFVF_SPECULAR)
3369 struct wined3d_color material_specular, specular_colour;
3371 wined3d_colour_from_mcs(&material_specular, specular_source,
3372 material_specular_state_colour, i, stream_info);
3374 if (lighting)
3376 specular_colour.r = specular.r * material_specular.r;
3377 specular_colour.g = specular.g * material_specular.g;
3378 specular_colour.b = specular.b * material_specular.b;
3379 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3381 else
3383 specular_colour = material_specular;
3385 update_fog_factor(&specular_colour.a, &ls);
3386 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3387 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3388 dest_ptr += sizeof(DWORD);
3391 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3393 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3394 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3395 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3397 ERR("No source texture, but destination requests one\n");
3398 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3400 else
3402 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3407 wined3d_resource_unmap(&dest->resource, 0);
3409 return WINED3D_OK;
3411 #undef copy_and_next
3413 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3414 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3415 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3417 struct wined3d_state *state = device->cs->c.state;
3418 struct wined3d_stream_info stream_info;
3419 struct wined3d_resource *resource;
3420 struct wined3d_box box = {0};
3421 struct wined3d_shader *vs;
3422 unsigned int i, j;
3423 HRESULT hr;
3424 WORD map;
3426 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3427 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3428 device, src_start_idx, dst_idx, vertex_count,
3429 dst_buffer, declaration, flags, dst_fvf);
3431 if (declaration)
3432 FIXME("Output vertex declaration not implemented yet.\n");
3434 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3435 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3436 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3437 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3439 /* We can't convert FROM a VBO, and vertex buffers used to source into
3440 * process_vertices() are unlikely to ever be used for drawing. Release
3441 * VBOs in those buffers and fix up the stream_info structure.
3443 * Also apply the start index. */
3444 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3446 struct wined3d_stream_info_element *e;
3447 struct wined3d_map_desc map_desc;
3449 if (!(map & 1))
3450 continue;
3452 e = &stream_info.elements[i];
3453 resource = &state->streams[e->stream_idx].buffer->resource;
3454 box.left = src_start_idx * e->stride;
3455 box.right = box.left + vertex_count * e->stride;
3456 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3458 ERR("Failed to map resource.\n");
3459 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3461 if (!(map & 1))
3462 continue;
3464 e = &stream_info.elements[j];
3465 resource = &state->streams[e->stream_idx].buffer->resource;
3466 if (FAILED(wined3d_resource_unmap(resource, 0)))
3467 ERR("Failed to unmap resource.\n");
3469 return WINED3DERR_INVALIDCALL;
3471 e->data.buffer_object = 0;
3472 e->data.addr += (ULONG_PTR)map_desc.data;
3475 hr = process_vertices_strided(device, dst_idx, vertex_count,
3476 &stream_info, dst_buffer, flags, dst_fvf);
3478 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3480 if (!(map & 1))
3481 continue;
3483 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3484 if (FAILED(wined3d_resource_unmap(resource, 0)))
3485 ERR("Failed to unmap resource.\n");
3488 return hr;
3491 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3492 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3494 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3496 TRACE("device %p, stage %u, state %s, value %#x.\n",
3497 device, stage, debug_d3dtexturestate(state), value);
3499 if (stage >= d3d_info->limits.ffp_blend_stages)
3501 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3502 stage, d3d_info->limits.ffp_blend_stages - 1);
3503 return;
3506 if (value == device->cs->c.state->texture_states[stage][state])
3508 TRACE("Application is setting the old value over, nothing to do.\n");
3509 return;
3512 device->cs->c.state->texture_states[stage][state] = value;
3514 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
3517 static void wined3d_device_set_texture(struct wined3d_device *device,
3518 UINT stage, struct wined3d_texture *texture)
3520 struct wined3d_state *state = device->cs->c.state;
3521 struct wined3d_texture *prev;
3523 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3525 /* Windows accepts overflowing this array... we do not. */
3526 if (stage >= ARRAY_SIZE(state->textures))
3528 WARN("Ignoring invalid stage %u.\n", stage);
3529 return;
3532 prev = state->textures[stage];
3533 TRACE("Previous texture %p.\n", prev);
3535 if (texture == prev)
3537 TRACE("App is setting the same texture again, nothing to do.\n");
3538 return;
3541 TRACE("Setting new texture to %p.\n", texture);
3542 state->textures[stage] = texture;
3544 if (texture)
3545 wined3d_texture_incref(texture);
3546 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
3547 if (prev)
3548 wined3d_texture_decref(prev);
3550 return;
3553 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3554 struct wined3d_stateblock *stateblock)
3556 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3557 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3558 const struct wined3d_saved_states *changed = &stateblock->changed;
3559 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3560 struct wined3d_device_context *context = &device->cs->c;
3561 unsigned int i, j, start, idx;
3562 struct wined3d_range range;
3563 uint32_t map;
3565 TRACE("device %p, stateblock %p.\n", device, stateblock);
3567 if (changed->vertexShader)
3568 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
3569 if (changed->pixelShader)
3570 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
3572 for (start = 0; ; start = range.offset + range.size)
3574 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3575 break;
3577 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3580 map = changed->vertexShaderConstantsI;
3581 for (start = 0; ; start = range.offset + range.size)
3583 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3584 break;
3586 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3589 map = changed->vertexShaderConstantsB;
3590 for (start = 0; ; start = range.offset + range.size)
3592 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3593 break;
3595 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3598 for (start = 0; ; start = range.offset + range.size)
3600 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3601 break;
3603 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3606 map = changed->pixelShaderConstantsI;
3607 for (start = 0; ; start = range.offset + range.size)
3609 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3610 break;
3612 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3615 map = changed->pixelShaderConstantsB;
3616 for (start = 0; ; start = range.offset + range.size)
3618 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3619 break;
3621 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3624 if (changed->lights)
3626 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3628 const struct wined3d_light_info *light;
3630 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3632 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
3633 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3638 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3640 map = changed->renderState[i];
3641 while (map)
3643 j = wined3d_bit_scan(&map);
3644 idx = i * word_bit_count + j;
3646 switch (idx)
3648 case WINED3D_RS_BLENDFACTOR:
3649 case WINED3D_RS_MULTISAMPLEMASK:
3650 case WINED3D_RS_ALPHABLENDENABLE:
3651 case WINED3D_RS_SRCBLEND:
3652 case WINED3D_RS_DESTBLEND:
3653 case WINED3D_RS_BLENDOP:
3654 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
3655 case WINED3D_RS_SRCBLENDALPHA:
3656 case WINED3D_RS_DESTBLENDALPHA:
3657 case WINED3D_RS_BLENDOPALPHA:
3658 case WINED3D_RS_COLORWRITEENABLE:
3659 case WINED3D_RS_COLORWRITEENABLE1:
3660 case WINED3D_RS_COLORWRITEENABLE2:
3661 case WINED3D_RS_COLORWRITEENABLE3:
3662 set_blend_state = TRUE;
3663 break;
3665 case WINED3D_RS_BACK_STENCILFAIL:
3666 case WINED3D_RS_BACK_STENCILFUNC:
3667 case WINED3D_RS_BACK_STENCILPASS:
3668 case WINED3D_RS_BACK_STENCILZFAIL:
3669 case WINED3D_RS_STENCILENABLE:
3670 case WINED3D_RS_STENCILFAIL:
3671 case WINED3D_RS_STENCILFUNC:
3672 case WINED3D_RS_STENCILREF:
3673 case WINED3D_RS_STENCILMASK:
3674 case WINED3D_RS_STENCILPASS:
3675 case WINED3D_RS_STENCILWRITEMASK:
3676 case WINED3D_RS_STENCILZFAIL:
3677 case WINED3D_RS_TWOSIDEDSTENCILMODE:
3678 case WINED3D_RS_ZENABLE:
3679 case WINED3D_RS_ZFUNC:
3680 case WINED3D_RS_ZWRITEENABLE:
3681 set_depth_stencil_state = TRUE;
3682 break;
3684 case WINED3D_RS_FILLMODE:
3685 case WINED3D_RS_CULLMODE:
3686 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
3687 case WINED3D_RS_DEPTHBIAS:
3688 case WINED3D_RS_SCISSORTESTENABLE:
3689 case WINED3D_RS_ANTIALIASEDLINEENABLE:
3690 set_rasterizer_state = TRUE;
3691 break;
3693 default:
3694 wined3d_device_set_render_state(device, idx, state->rs[idx]);
3695 break;
3700 if (set_rasterizer_state)
3702 struct wined3d_rasterizer_state *rasterizer_state;
3703 struct wined3d_rasterizer_state_desc desc;
3704 struct wine_rb_entry *entry;
3705 union
3707 DWORD d;
3708 float f;
3709 } bias;
3711 memset(&desc, 0, sizeof(desc));
3712 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
3713 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
3714 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
3715 desc.depth_bias = bias.f;
3716 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
3717 desc.scale_bias = bias.f;
3718 desc.depth_clip = TRUE;
3719 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
3720 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
3722 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
3724 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
3725 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3727 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
3728 &wined3d_null_parent_ops, &rasterizer_state)))
3730 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3731 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
3733 ERR("Failed to insert rasterizer state.\n");
3734 wined3d_rasterizer_state_decref(rasterizer_state);
3739 if (set_blend_state || changed->alpha_to_coverage
3740 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
3742 struct wined3d_blend_state *blend_state;
3743 struct wined3d_blend_state_desc desc;
3744 struct wine_rb_entry *entry;
3745 struct wined3d_color colour;
3746 unsigned int sample_mask;
3748 memset(&desc, 0, sizeof(desc));
3749 desc.alpha_to_coverage = state->alpha_to_coverage;
3750 desc.independent = FALSE;
3751 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
3752 desc.alpha_to_coverage = TRUE;
3753 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
3754 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
3755 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
3756 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
3757 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
3759 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
3760 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
3761 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
3763 else
3765 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
3766 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
3767 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
3769 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
3770 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
3771 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
3772 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
3773 if (desc.rt[1].writemask != desc.rt[0].writemask
3774 || desc.rt[2].writemask != desc.rt[0].writemask
3775 || desc.rt[3].writemask != desc.rt[0].writemask)
3777 desc.independent = TRUE;
3778 for (i = 1; i < 4; ++i)
3780 desc.rt[i].enable = desc.rt[0].enable;
3781 desc.rt[i].src = desc.rt[0].src;
3782 desc.rt[i].dst = desc.rt[0].dst;
3783 desc.rt[i].op = desc.rt[0].op;
3784 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
3785 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
3786 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
3790 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
3791 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
3792 else
3793 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
3795 if ((entry = wine_rb_get(&device->blend_states, &desc)))
3797 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
3798 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3799 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3801 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
3802 &wined3d_null_parent_ops, &blend_state)))
3804 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3805 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3806 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
3808 ERR("Failed to insert blend state.\n");
3809 wined3d_blend_state_decref(blend_state);
3814 if (set_depth_stencil_state)
3816 struct wined3d_depth_stencil_state *depth_stencil_state;
3817 struct wined3d_depth_stencil_state_desc desc;
3818 struct wine_rb_entry *entry;
3819 unsigned int stencil_ref;
3821 memset(&desc, 0, sizeof(desc));
3822 switch (state->rs[WINED3D_RS_ZENABLE])
3824 case WINED3D_ZB_FALSE:
3825 desc.depth = FALSE;
3826 break;
3828 case WINED3D_ZB_USEW:
3829 FIXME("W buffer is not well handled.\n");
3830 case WINED3D_ZB_TRUE:
3831 desc.depth = TRUE;
3832 break;
3834 default:
3835 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
3837 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
3838 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
3839 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
3840 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
3841 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
3842 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
3843 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
3844 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
3845 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
3847 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
3849 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
3850 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
3851 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
3852 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
3854 else
3856 desc.back = desc.front;
3859 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
3860 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
3861 else
3862 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
3864 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
3866 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
3867 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3869 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
3870 &wined3d_null_parent_ops, &depth_stencil_state)))
3872 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3873 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
3875 ERR("Failed to insert depth/stencil state.\n");
3876 wined3d_depth_stencil_state_decref(depth_stencil_state);
3881 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
3883 map = changed->textureState[i];
3884 while (map)
3886 j = wined3d_bit_scan(&map);
3887 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
3891 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
3893 map = changed->samplerState[i];
3894 while (map)
3896 j = wined3d_bit_scan(&map);
3897 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
3901 if (changed->transforms)
3903 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
3905 map = changed->transform[i];
3906 while (map)
3908 j = wined3d_bit_scan(&map);
3909 idx = i * word_bit_count + j;
3910 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
3915 if (changed->indices)
3916 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
3917 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
3918 if (changed->vertexDecl)
3919 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
3920 if (changed->material)
3921 wined3d_device_set_material(device, &state->material);
3922 if (changed->viewport)
3923 wined3d_device_context_set_viewports(context, 1, &state->viewport);
3924 if (changed->scissorRect)
3925 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
3927 map = changed->streamSource;
3928 while (map)
3930 i = wined3d_bit_scan(&map);
3931 wined3d_device_context_set_stream_source(context, i, state->streams[i].buffer,
3932 state->streams[i].offset, state->streams[i].stride);
3934 map = changed->streamFreq;
3935 while (map)
3937 i = wined3d_bit_scan(&map);
3938 wined3d_device_set_stream_source_freq(device, i,
3939 state->streams[i].frequency | state->streams[i].flags);
3942 map = changed->textures;
3943 while (map)
3945 i = wined3d_bit_scan(&map);
3946 wined3d_device_set_texture(device, i, state->textures[i]);
3949 map = changed->clipplane;
3950 while (map)
3952 i = wined3d_bit_scan(&map);
3953 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
3956 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
3958 TRACE("Applied stateblock %p.\n", stateblock);
3961 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
3963 TRACE("device %p, caps %p.\n", device, caps);
3965 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
3968 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3969 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3971 struct wined3d_swapchain *swapchain;
3973 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3974 device, swapchain_idx, mode, rotation);
3976 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3977 return WINED3DERR_INVALIDCALL;
3979 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3982 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3984 /* At the moment we have no need for any functionality at the beginning
3985 * of a scene. */
3986 TRACE("device %p.\n", device);
3988 if (device->inScene)
3990 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3991 return WINED3DERR_INVALIDCALL;
3993 device->inScene = TRUE;
3994 return WINED3D_OK;
3997 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3999 TRACE("device %p.\n", device);
4001 if (!device->inScene)
4003 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4004 return WINED3DERR_INVALIDCALL;
4007 device->inScene = FALSE;
4008 return WINED3D_OK;
4011 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4012 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4014 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4016 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4017 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4019 if (!rect_count && rects)
4021 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4022 return WINED3D_OK;
4025 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4027 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4028 if (!ds)
4030 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4031 /* TODO: What about depth stencil buffers without stencil bits? */
4032 return WINED3DERR_INVALIDCALL;
4034 else if (flags & WINED3DCLEAR_TARGET)
4036 if (ds->width < fb->render_targets[0]->width
4037 || ds->height < fb->render_targets[0]->height)
4039 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4040 return WINED3D_OK;
4045 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4047 return WINED3D_OK;
4050 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4052 struct wined3d_state *state = context->state;
4054 TRACE("context %p, value %p.\n", context, value);
4056 if (value)
4057 *value = state->predicate_value;
4058 return state->predicate;
4061 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4062 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4064 struct wined3d_state *state = context->state;
4066 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4067 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4069 state->primitive_type = primitive_type;
4070 state->patch_vertex_count = patch_vertex_count;
4073 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4074 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4076 const struct wined3d_state *state = context->state;
4078 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4079 context, primitive_type, patch_vertex_count);
4081 *primitive_type = state->primitive_type;
4082 if (patch_vertex_count)
4083 *patch_vertex_count = state->patch_vertex_count;
4085 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4088 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4089 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4091 unsigned int src_size, dst_size, src_skip_levels = 0;
4092 unsigned int src_level_count, dst_level_count;
4093 const struct wined3d_dirty_regions *regions;
4094 unsigned int layer_count, level_count, i, j;
4095 enum wined3d_resource_type type;
4096 BOOL entire_texture = TRUE;
4097 struct wined3d_box box;
4099 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4101 /* Verify that the source and destination textures are non-NULL. */
4102 if (!src_texture || !dst_texture)
4104 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4105 return WINED3DERR_INVALIDCALL;
4108 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4109 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4111 WARN("Source resource is GPU accessible or a scratch resource.\n");
4112 return WINED3DERR_INVALIDCALL;
4114 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4116 WARN("Destination resource is CPU accessible.\n");
4117 return WINED3DERR_INVALIDCALL;
4120 /* Verify that the source and destination textures are the same type. */
4121 type = src_texture->resource.type;
4122 if (dst_texture->resource.type != type)
4124 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4125 return WINED3DERR_INVALIDCALL;
4128 layer_count = src_texture->layer_count;
4129 if (layer_count != dst_texture->layer_count)
4131 WARN("Source and destination have different layer counts.\n");
4132 return WINED3DERR_INVALIDCALL;
4135 if (src_texture->resource.format != dst_texture->resource.format)
4137 WARN("Source and destination formats do not match.\n");
4138 return WINED3DERR_INVALIDCALL;
4141 src_level_count = src_texture->level_count;
4142 dst_level_count = dst_texture->level_count;
4143 level_count = min(src_level_count, dst_level_count);
4145 src_size = max(src_texture->resource.width, src_texture->resource.height);
4146 src_size = max(src_size, src_texture->resource.depth);
4147 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4148 dst_size = max(dst_size, dst_texture->resource.depth);
4149 while (src_size > dst_size)
4151 src_size >>= 1;
4152 ++src_skip_levels;
4155 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4156 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4157 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4159 WARN("Source and destination dimensions do not match.\n");
4160 return WINED3DERR_INVALIDCALL;
4163 if ((regions = src_texture->dirty_regions))
4165 for (i = 0; i < layer_count && entire_texture; ++i)
4167 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4168 continue;
4170 entire_texture = FALSE;
4171 break;
4175 /* Update every surface level of the texture. */
4176 if (entire_texture)
4178 for (i = 0; i < level_count; ++i)
4180 wined3d_texture_get_level_box(dst_texture, i, &box);
4181 for (j = 0; j < layer_count; ++j)
4183 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4184 &dst_texture->resource, j * dst_level_count + i, &box,
4185 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4186 0, NULL, WINED3D_TEXF_POINT);
4190 else
4192 unsigned int src_level, box_count, k;
4193 const struct wined3d_box *boxes;
4194 struct wined3d_box b;
4196 for (i = 0; i < layer_count; ++i)
4198 boxes = regions[i].boxes;
4199 box_count = regions[i].box_count;
4200 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4202 boxes = &b;
4203 box_count = 1;
4204 wined3d_texture_get_level_box(dst_texture, i, &b);
4207 for (j = 0; j < level_count; ++j)
4209 src_level = j + src_skip_levels;
4211 /* TODO: We could pass an array of boxes here to avoid
4212 * multiple context acquisitions for the same resource. */
4213 for (k = 0; k < box_count; ++k)
4215 box = boxes[k];
4216 if (src_level)
4218 box.left >>= src_level;
4219 box.top >>= src_level;
4220 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4221 wined3d_texture_get_level_width(src_texture, src_level));
4222 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4223 wined3d_texture_get_level_height(src_texture, src_level));
4224 box.front >>= src_level;
4225 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4226 wined3d_texture_get_level_depth(src_texture, src_level));
4229 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4230 &dst_texture->resource, i * dst_level_count + j, &box,
4231 &src_texture->resource, i * src_level_count + src_level, &box,
4232 0, NULL, WINED3D_TEXF_POINT);
4238 wined3d_texture_clear_dirty_regions(src_texture);
4240 return WINED3D_OK;
4243 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4245 const struct wined3d_state *state = device->cs->c.state;
4246 struct wined3d_texture *texture;
4247 DWORD i;
4249 TRACE("device %p, num_passes %p.\n", device, num_passes);
4251 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4253 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4255 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4256 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4258 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4260 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4261 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4264 texture = state->textures[i];
4265 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4267 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4269 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4270 return E_FAIL;
4272 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4274 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4275 return E_FAIL;
4277 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4278 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4280 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4281 return E_FAIL;
4285 if (wined3d_state_uses_depth_buffer(state)
4286 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4288 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4289 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4291 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4293 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4294 return WINED3DERR_CONFLICTINGRENDERSTATE;
4298 /* return a sensible default */
4299 *num_passes = 1;
4301 TRACE("returning D3D_OK\n");
4302 return WINED3D_OK;
4305 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4307 static BOOL warned;
4309 TRACE("device %p, software %#x.\n", device, software);
4311 if (!warned)
4313 FIXME("device %p, software %#x stub!\n", device, software);
4314 warned = TRUE;
4317 device->softwareVertexProcessing = software;
4320 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4322 static BOOL warned;
4324 TRACE("device %p.\n", device);
4326 if (!warned)
4328 TRACE("device %p stub!\n", device);
4329 warned = TRUE;
4332 return device->softwareVertexProcessing;
4335 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4336 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4338 struct wined3d_swapchain *swapchain;
4340 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4341 device, swapchain_idx, raster_status);
4343 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4344 return WINED3DERR_INVALIDCALL;
4346 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4349 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4351 static BOOL warned;
4353 TRACE("device %p, segments %.8e.\n", device, segments);
4355 if (segments != 0.0f)
4357 if (!warned)
4359 FIXME("device %p, segments %.8e stub!\n", device, segments);
4360 warned = TRUE;
4364 return WINED3D_OK;
4367 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4369 static BOOL warned;
4371 TRACE("device %p.\n", device);
4373 if (!warned)
4375 FIXME("device %p stub!\n", device);
4376 warned = TRUE;
4379 return 0.0f;
4382 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4383 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4385 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4386 context, dst_buffer, offset, uav);
4388 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4391 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4392 const struct wined3d_resource *dst_resource)
4394 if (src_resource->format->id == dst_resource->format->id)
4395 return true;
4396 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4397 return true;
4398 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4399 return false;
4400 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4401 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4402 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4403 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4404 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4405 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4406 return false;
4409 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4410 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4412 unsigned int src_row_block_count, dst_row_block_count;
4413 struct wined3d_texture *dst_texture, *src_texture;
4414 unsigned int src_row_count, dst_row_count;
4415 struct wined3d_box src_box, dst_box;
4416 unsigned int i, j;
4418 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4420 if (src_resource == dst_resource)
4422 WARN("Source and destination are the same resource.\n");
4423 return;
4426 if (src_resource->type != dst_resource->type)
4428 WARN("Resource types (%s / %s) don't match.\n",
4429 debug_d3dresourcetype(dst_resource->type),
4430 debug_d3dresourcetype(src_resource->type));
4431 return;
4434 if (!resources_format_compatible(src_resource, dst_resource))
4436 WARN("Resource formats %s and %s are incompatible.\n",
4437 debug_d3dformat(dst_resource->format->id),
4438 debug_d3dformat(src_resource->format->id));
4439 return;
4442 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4443 / src_resource->format->block_width;
4444 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4445 / dst_resource->format->block_width;
4446 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4447 / src_resource->format->block_height;
4448 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4449 / dst_resource->format->block_height;
4451 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4452 || src_resource->depth != dst_resource->depth)
4454 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4455 dst_row_block_count, dst_row_count, dst_resource->depth,
4456 src_row_block_count, src_row_count, src_resource->depth);
4457 return;
4460 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4462 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4463 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4464 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4465 return;
4468 dst_texture = texture_from_resource(dst_resource);
4469 src_texture = texture_from_resource(src_resource);
4471 if (src_texture->layer_count != dst_texture->layer_count
4472 || src_texture->level_count != dst_texture->level_count)
4474 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4475 dst_texture->layer_count, dst_texture->level_count,
4476 src_texture->layer_count, src_texture->level_count);
4477 return;
4480 for (i = 0; i < dst_texture->level_count; ++i)
4482 wined3d_texture_get_level_box(src_texture, i, &src_box);
4483 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4484 for (j = 0; j < dst_texture->layer_count; ++j)
4486 unsigned int idx = j * dst_texture->level_count + i;
4488 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4489 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4494 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4495 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4496 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4497 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4499 struct wined3d_box dst_box, b;
4501 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4502 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4503 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4504 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4506 if (flags)
4507 FIXME("Ignoring flags %#x.\n", flags);
4509 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4511 WARN("Source and destination are the same sub-resource.\n");
4512 return WINED3DERR_INVALIDCALL;
4515 if (!resources_format_compatible(src_resource, dst_resource))
4517 WARN("Resource formats %s and %s are incompatible.\n",
4518 debug_d3dformat(dst_resource->format->id),
4519 debug_d3dformat(src_resource->format->id));
4520 return WINED3DERR_INVALIDCALL;
4523 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4525 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4527 WARN("Resource types (%s / %s) don't match.\n",
4528 debug_d3dresourcetype(dst_resource->type),
4529 debug_d3dresourcetype(src_resource->type));
4530 return WINED3DERR_INVALIDCALL;
4533 if (dst_sub_resource_idx)
4535 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4536 return WINED3DERR_INVALIDCALL;
4539 if (src_sub_resource_idx)
4541 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4542 return WINED3DERR_INVALIDCALL;
4545 if (!src_box)
4547 unsigned int dst_w;
4549 dst_w = dst_resource->size - dst_x;
4550 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4551 src_box = &b;
4553 else if ((src_box->left >= src_box->right
4554 || src_box->top >= src_box->bottom
4555 || src_box->front >= src_box->back))
4557 WARN("Invalid box %s specified.\n", debug_box(src_box));
4558 return WINED3DERR_INVALIDCALL;
4561 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4562 || src_box->right - src_box->left > dst_resource->size - dst_x)
4564 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4565 dst_x, src_box->left, src_box->right - src_box->left);
4566 return WINED3DERR_INVALIDCALL;
4569 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4571 else
4573 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4574 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4575 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4576 unsigned int src_row_block_count, src_row_count;
4578 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4580 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4581 return WINED3DERR_INVALIDCALL;
4584 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4586 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4587 return WINED3DERR_INVALIDCALL;
4590 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4592 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4593 return WINED3DERR_INVALIDCALL;
4596 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4598 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4599 return WINED3DERR_INVALIDCALL;
4602 if (!src_box)
4604 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4606 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4607 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4608 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4610 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4611 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4612 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4613 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4615 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4616 src_box = &b;
4618 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
4620 WARN("Invalid source box %s.\n", debug_box(src_box));
4621 return WINED3DERR_INVALIDCALL;
4624 if (src_resource->format->block_width == dst_resource->format->block_width
4625 && src_resource->format->block_height == dst_resource->format->block_height)
4627 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4628 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4630 else
4632 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
4633 / src_resource->format->block_width;
4634 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
4635 / src_resource->format->block_height;
4636 wined3d_box_set(&dst_box, dst_x, dst_y,
4637 dst_x + (src_row_block_count * dst_resource->format->block_width),
4638 dst_y + (src_row_count * dst_resource->format->block_height),
4639 dst_z, dst_z + (src_box->back - src_box->front));
4641 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
4643 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4644 return WINED3DERR_INVALIDCALL;
4648 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
4649 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4651 return WINED3D_OK;
4654 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
4655 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
4656 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
4658 struct wined3d_sub_resource_desc desc;
4659 struct wined3d_box b;
4661 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
4662 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4664 if (flags)
4665 FIXME("Ignoring flags %#x.\n", flags);
4667 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4669 WARN("Resource %p is not GPU accessible.\n", resource);
4670 return;
4673 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4674 return;
4676 if (!box)
4678 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4679 box = &b;
4681 else if (box->left >= box->right || box->right > desc.width
4682 || box->top >= box->bottom || box->bottom > desc.height
4683 || box->front >= box->back || box->back > desc.depth)
4685 WARN("Invalid box %s specified.\n", debug_box(box));
4686 return;
4689 wined3d_device_context_emit_update_sub_resource(context, resource,
4690 sub_resource_idx, box, data, row_pitch, depth_pitch);
4693 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
4694 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4695 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4696 enum wined3d_format_id format_id)
4698 struct wined3d_texture *dst_texture, *src_texture;
4699 unsigned int dst_level, src_level;
4700 struct wined3d_blt_fx fx = {0};
4701 RECT dst_rect, src_rect;
4703 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
4704 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4705 context, dst_resource, dst_sub_resource_idx,
4706 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4708 if (wined3d_format_is_typeless(dst_resource->format)
4709 || wined3d_format_is_typeless(src_resource->format))
4711 FIXME("Multisample resolve is not fully supported for typeless formats "
4712 "(dst_format %s, src_format %s, format %s).\n",
4713 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4714 debug_d3dformat(format_id));
4716 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4718 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4719 return;
4721 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4723 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4724 return;
4727 fx.resolve_format_id = format_id;
4729 dst_texture = texture_from_resource(dst_resource);
4730 src_texture = texture_from_resource(src_resource);
4732 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4733 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4734 wined3d_texture_get_level_height(dst_texture, dst_level));
4735 src_level = src_sub_resource_idx % src_texture->level_count;
4736 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4737 wined3d_texture_get_level_height(src_texture, src_level));
4738 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
4739 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
4742 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
4743 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
4744 const struct wined3d_color *color, float depth, unsigned int stencil)
4746 struct wined3d_resource *resource;
4747 RECT r;
4749 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4750 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4752 if (!flags)
4753 return WINED3D_OK;
4755 resource = view->resource;
4756 if (resource->type == WINED3D_RTYPE_BUFFER)
4758 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4759 return WINED3DERR_INVALIDCALL;
4762 if (!rect)
4764 SetRect(&r, 0, 0, view->width, view->height);
4765 rect = &r;
4767 else
4769 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4770 HRESULT hr;
4772 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
4773 return hr;
4776 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
4778 return WINED3D_OK;
4781 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
4782 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
4784 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
4786 if (!(view->format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_FLOAT | WINED3DFMT_FLAG_NORMALISED)))
4788 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
4789 return;
4792 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
4795 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
4796 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4798 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
4800 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
4803 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
4805 /* Not all flags make sense together, but Windows never returns an error.
4806 * Catch the cases that could cause issues. */
4807 if (flags & WINED3D_MAP_READ)
4809 if (flags & WINED3D_MAP_DISCARD)
4811 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
4812 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4814 if (flags & WINED3D_MAP_NOOVERWRITE)
4816 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
4817 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4820 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4822 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
4824 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
4825 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4827 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4828 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4830 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
4831 flags &= ~WINED3D_MAP_DISCARD;
4835 return flags;
4838 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
4839 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4840 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
4842 struct wined3d_sub_resource_desc desc;
4843 struct wined3d_box b;
4844 HRESULT hr;
4846 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
4847 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
4849 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
4851 WARN("No read/write flags specified.\n");
4852 return E_INVALIDARG;
4855 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
4857 WARN("Resource does not have MAP_R access.\n");
4858 return E_INVALIDARG;
4861 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
4863 WARN("Resource does not have MAP_W access.\n");
4864 return E_INVALIDARG;
4867 flags = sanitise_map_flags(resource, flags);
4869 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4870 return E_INVALIDARG;
4872 if (!box)
4874 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4875 box = &b;
4877 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
4879 WARN("Map box is invalid.\n");
4881 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
4882 return WINED3DERR_INVALIDCALL;
4884 if ((resource->format_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
4885 return WINED3DERR_INVALIDCALL;
4888 if (SUCCEEDED(hr = wined3d_device_context_emit_map(context, resource,
4889 sub_resource_idx, &map_desc->data, box, flags)))
4890 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx,
4891 &map_desc->row_pitch, &map_desc->slice_pitch);
4892 return hr;
4895 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
4896 struct wined3d_resource *resource, unsigned int sub_resource_idx)
4898 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
4900 return wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
4903 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
4904 struct wined3d_query *query, unsigned int flags)
4906 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
4908 context->ops->issue_query(context, query, flags);
4911 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
4912 struct wined3d_command_list *list, bool restore_state)
4914 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
4916 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
4919 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
4920 const struct wined3d_device_context *context, unsigned int view_idx)
4922 unsigned int max_rt_count;
4924 TRACE("context %p, view_idx %u.\n", context, view_idx);
4926 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
4927 if (view_idx >= max_rt_count)
4929 WARN("Only %u render targets are supported.\n", max_rt_count);
4930 return NULL;
4933 return context->state->fb.render_targets[view_idx];
4936 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
4937 const struct wined3d_device_context *context)
4939 TRACE("context %p.\n", context);
4941 return context->state->fb.depth_stencil;
4944 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
4945 struct wined3d_shader_resource_view *view)
4947 struct wined3d_texture *texture;
4949 TRACE("context %p, view %p.\n", context, view);
4951 if (view->resource->type == WINED3D_RTYPE_BUFFER)
4953 WARN("Called on buffer resource %p.\n", view->resource);
4954 return;
4957 texture = texture_from_resource(view->resource);
4958 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
4960 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
4961 return;
4964 wined3d_device_context_emit_generate_mipmaps(context, view);
4967 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4968 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4970 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4971 struct wined3d_sub_resource_data data;
4972 struct wined3d_resource_desc desc;
4973 struct wined3d_map_desc map_desc;
4974 struct wined3d_texture *texture;
4975 HRESULT hr;
4977 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4979 ERR("Failed to map source texture.\n");
4980 return NULL;
4983 data.data = map_desc.data;
4984 data.row_pitch = map_desc.row_pitch;
4985 data.slice_pitch = map_desc.slice_pitch;
4987 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4988 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4989 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4990 desc.multisample_quality = 0;
4991 desc.usage = WINED3DUSAGE_DYNAMIC;
4992 desc.bind_flags = 0;
4993 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4994 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4995 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4996 desc.depth = 1;
4997 desc.size = 0;
4999 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5000 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5001 if (FAILED(hr))
5003 ERR("Failed to create cursor texture.\n");
5004 return NULL;
5007 return texture;
5010 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5011 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5013 unsigned int texture_level = sub_resource_idx % texture->level_count;
5014 unsigned int cursor_width, cursor_height;
5015 struct wined3d_map_desc map_desc;
5017 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5018 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5020 if (sub_resource_idx >= texture->level_count * texture->layer_count
5021 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5022 return WINED3DERR_INVALIDCALL;
5024 if (device->cursor_texture)
5026 wined3d_texture_decref(device->cursor_texture);
5027 device->cursor_texture = NULL;
5030 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5032 WARN("Texture %p has invalid format %s.\n",
5033 texture, debug_d3dformat(texture->resource.format->id));
5034 return WINED3DERR_INVALIDCALL;
5037 /* Cursor width and height must all be powers of two */
5038 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5039 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5040 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5042 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5043 return WINED3DERR_INVALIDCALL;
5046 /* Do not store the surface's pointer because the application may
5047 * release it after setting the cursor image. Windows doesn't
5048 * addref the set surface, so we can't do this either without
5049 * creating circular refcount dependencies. */
5050 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5052 ERR("Failed to create cursor texture.\n");
5053 return WINED3DERR_INVALIDCALL;
5056 if (cursor_width == 32 && cursor_height == 32)
5058 UINT mask_size = cursor_width * cursor_height / 8;
5059 ICONINFO cursor_info;
5060 DWORD *mask_bits;
5061 HCURSOR cursor;
5063 /* 32-bit user32 cursors ignore the alpha channel if it's all
5064 * zeroes, and use the mask instead. Fill the mask with all ones
5065 * to ensure we still get a fully transparent cursor. */
5066 if (!(mask_bits = heap_alloc(mask_size)))
5067 return E_OUTOFMEMORY;
5068 memset(mask_bits, 0xff, mask_size);
5070 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5071 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5072 cursor_info.fIcon = FALSE;
5073 cursor_info.xHotspot = x_hotspot;
5074 cursor_info.yHotspot = y_hotspot;
5075 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5076 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5077 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5079 /* Create our cursor and clean up. */
5080 cursor = CreateIconIndirect(&cursor_info);
5081 if (cursor_info.hbmMask)
5082 DeleteObject(cursor_info.hbmMask);
5083 if (cursor_info.hbmColor)
5084 DeleteObject(cursor_info.hbmColor);
5085 if (device->hardwareCursor)
5086 DestroyCursor(device->hardwareCursor);
5087 device->hardwareCursor = cursor;
5088 if (device->bCursorVisible)
5089 SetCursor(cursor);
5091 heap_free(mask_bits);
5094 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5095 device->cursorWidth = cursor_width;
5096 device->cursorHeight = cursor_height;
5097 device->xHotSpot = x_hotspot;
5098 device->yHotSpot = y_hotspot;
5100 return WINED3D_OK;
5103 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5104 int x_screen_space, int y_screen_space, DWORD flags)
5106 TRACE("device %p, x %d, y %d, flags %#x.\n",
5107 device, x_screen_space, y_screen_space, flags);
5109 device->xScreenSpace = x_screen_space;
5110 device->yScreenSpace = y_screen_space;
5112 if (device->hardwareCursor)
5114 POINT pt;
5116 GetCursorPos( &pt );
5117 if (x_screen_space == pt.x && y_screen_space == pt.y)
5118 return;
5119 SetCursorPos( x_screen_space, y_screen_space );
5121 /* Switch to the software cursor if position diverges from the hardware one. */
5122 GetCursorPos( &pt );
5123 if (x_screen_space != pt.x || y_screen_space != pt.y)
5125 if (device->bCursorVisible) SetCursor( NULL );
5126 DestroyCursor( device->hardwareCursor );
5127 device->hardwareCursor = 0;
5132 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5134 BOOL oldVisible = device->bCursorVisible;
5136 TRACE("device %p, show %#x.\n", device, show);
5139 * When ShowCursor is first called it should make the cursor appear at the OS's last
5140 * known cursor position.
5142 if (show && !oldVisible)
5144 POINT pt;
5145 GetCursorPos(&pt);
5146 device->xScreenSpace = pt.x;
5147 device->yScreenSpace = pt.y;
5150 if (device->hardwareCursor)
5152 device->bCursorVisible = show;
5153 if (show)
5154 SetCursor(device->hardwareCursor);
5155 else
5156 SetCursor(NULL);
5158 else if (device->cursor_texture)
5160 device->bCursorVisible = show;
5163 return oldVisible;
5166 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5168 struct wined3d_resource *resource, *cursor;
5170 TRACE("device %p.\n", device);
5172 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5174 TRACE("Checking resource %p for eviction.\n", resource);
5176 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5178 TRACE("Evicting %p.\n", resource);
5179 wined3d_cs_emit_unload_resource(device->cs, resource);
5184 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5186 TRACE("context %p.\n", context);
5188 context->ops->flush(context);
5191 static void update_swapchain_flags(struct wined3d_texture *texture)
5193 unsigned int flags = texture->swapchain->state.desc.flags;
5195 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5196 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5197 else
5198 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5200 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5201 texture->flags |= WINED3D_TEXTURE_GET_DC;
5202 else
5203 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5206 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5207 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5208 wined3d_device_reset_cb callback, BOOL reset_state)
5210 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5211 struct wined3d_device_context *context = &device->cs->c;
5212 struct wined3d_swapchain_state *swapchain_state;
5213 struct wined3d_state *state = context->state;
5214 struct wined3d_swapchain_desc *current_desc;
5215 struct wined3d_resource *resource, *cursor;
5216 struct wined3d_rendertarget_view *view;
5217 struct wined3d_swapchain *swapchain;
5218 struct wined3d_view_desc view_desc;
5219 BOOL backbuffer_resized, windowed;
5220 HRESULT hr = WINED3D_OK;
5221 unsigned int i;
5223 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5224 device, swapchain_desc, mode, callback, reset_state);
5226 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5228 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5230 ERR("Failed to get the first implicit swapchain.\n");
5231 return WINED3DERR_INVALIDCALL;
5233 swapchain_state = &swapchain->state;
5234 current_desc = &swapchain_state->desc;
5236 if (reset_state)
5238 if (device->logo_texture)
5240 wined3d_texture_decref(device->logo_texture);
5241 device->logo_texture = NULL;
5243 if (device->cursor_texture)
5245 wined3d_texture_decref(device->cursor_texture);
5246 device->cursor_texture = NULL;
5248 state_unbind_resources(state);
5251 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
5253 wined3d_device_context_set_rendertarget_view(context, i, NULL, FALSE);
5255 wined3d_device_context_set_depth_stencil_view(context, NULL);
5257 if (reset_state)
5259 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5261 TRACE("Enumerating resource %p.\n", resource);
5262 if (FAILED(hr = callback(resource)))
5263 return hr;
5267 TRACE("New params:\n");
5268 TRACE("output %p\n", swapchain_desc->output);
5269 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5270 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5271 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5272 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5273 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5274 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5275 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5276 TRACE("device_window %p\n", swapchain_desc->device_window);
5277 TRACE("windowed %#x\n", swapchain_desc->windowed);
5278 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5279 if (swapchain_desc->enable_auto_depth_stencil)
5280 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5281 TRACE("flags %#x\n", swapchain_desc->flags);
5282 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5283 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5285 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5286 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5288 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5289 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5290 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5291 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5293 /* No special treatment of these parameters. Just store them */
5294 current_desc->swap_effect = swapchain_desc->swap_effect;
5295 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5296 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5297 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5298 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5300 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5302 TRACE("Changing the device window from %p to %p.\n",
5303 current_desc->device_window, swapchain_desc->device_window);
5304 current_desc->device_window = swapchain_desc->device_window;
5305 swapchain_state->device_window = swapchain_desc->device_window;
5306 wined3d_swapchain_set_window(swapchain, NULL);
5309 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5310 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5311 windowed = current_desc->windowed;
5313 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5314 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5316 /* Switch from windowed to fullscreen. */
5317 if (windowed && !swapchain_desc->windowed)
5319 HWND focus_window = device->create_parms.focus_window;
5321 if (!focus_window)
5322 focus_window = swapchain->state.device_window;
5323 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5325 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5326 return hr;
5330 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5331 swapchain_desc, mode)))
5332 return hr;
5334 /* Switch from fullscreen to windowed. */
5335 if (!windowed && swapchain_desc->windowed)
5336 wined3d_device_release_focus_window(device);
5338 else if (!swapchain_desc->windowed)
5340 DWORD style = swapchain_state->style;
5341 DWORD exstyle = swapchain_state->exstyle;
5342 struct wined3d_output_desc output_desc;
5344 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5345 * the window back into the right position. Some applications
5346 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5347 * up their mess. Guild Wars also loses the device during that. */
5348 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5350 ERR("Failed to get output description, hr %#x.\n", hr);
5351 return hr;
5354 swapchain_state->style = 0;
5355 swapchain_state->exstyle = 0;
5356 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5357 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5358 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5359 swapchain_state->style = style;
5360 swapchain_state->exstyle = exstyle;
5363 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5364 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5365 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5366 return hr;
5368 if (swapchain_desc->flags != current_desc->flags)
5370 current_desc->flags = swapchain_desc->flags;
5372 update_swapchain_flags(swapchain->front_buffer);
5373 for (i = 0; i < current_desc->backbuffer_count; ++i)
5375 update_swapchain_flags(swapchain->back_buffers[i]);
5379 if ((view = device->auto_depth_stencil_view))
5381 device->auto_depth_stencil_view = NULL;
5382 wined3d_rendertarget_view_decref(view);
5384 if (current_desc->enable_auto_depth_stencil)
5386 struct wined3d_resource_desc texture_desc;
5387 struct wined3d_texture *texture;
5389 TRACE("Creating the depth stencil buffer.\n");
5391 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5392 texture_desc.format = current_desc->auto_depth_stencil_format;
5393 texture_desc.multisample_type = current_desc->multisample_type;
5394 texture_desc.multisample_quality = current_desc->multisample_quality;
5395 texture_desc.usage = 0;
5396 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5397 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5398 texture_desc.width = current_desc->backbuffer_width;
5399 texture_desc.height = current_desc->backbuffer_height;
5400 texture_desc.depth = 1;
5401 texture_desc.size = 0;
5403 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5404 device->device_parent, &texture_desc, 0, &texture)))
5406 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5407 return WINED3DERR_INVALIDCALL;
5410 view_desc.format_id = texture->resource.format->id;
5411 view_desc.flags = 0;
5412 view_desc.u.texture.level_idx = 0;
5413 view_desc.u.texture.level_count = 1;
5414 view_desc.u.texture.layer_idx = 0;
5415 view_desc.u.texture.layer_count = 1;
5416 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5417 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5418 wined3d_texture_decref(texture);
5419 if (FAILED(hr))
5421 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5422 return hr;
5426 if ((view = device->back_buffer_view))
5428 device->back_buffer_view = NULL;
5429 wined3d_rendertarget_view_decref(view);
5431 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5433 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5435 view_desc.format_id = back_buffer->format->id;
5436 view_desc.flags = 0;
5437 view_desc.u.texture.level_idx = 0;
5438 view_desc.u.texture.level_count = 1;
5439 view_desc.u.texture.layer_idx = 0;
5440 view_desc.u.texture.layer_count = 1;
5441 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5442 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5444 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5445 return hr;
5449 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5450 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5451 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5452 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5454 if (reset_state)
5456 TRACE("Resetting state.\n");
5457 wined3d_device_context_emit_reset_state(&device->cs->c, false);
5458 state_cleanup(state);
5460 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5462 TRACE("Unloading resource %p.\n", resource);
5463 wined3d_cs_emit_unload_resource(device->cs, resource);
5466 device->adapter->adapter_ops->adapter_uninit_3d(device);
5468 wined3d_state_reset(state, &device->adapter->d3d_info);
5470 device_init_swapchain_state(device, swapchain);
5471 if (wined3d_settings.logo)
5472 device_load_logo(device, wined3d_settings.logo);
5474 else
5476 if ((view = device->back_buffer_view))
5477 wined3d_device_context_set_rendertarget_view(context, 0, view, FALSE);
5478 if ((view = device->auto_depth_stencil_view))
5479 wined3d_device_context_set_depth_stencil_view(context, view);
5482 if (reset_state)
5483 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5485 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5486 * first use
5488 return hr;
5491 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5493 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5495 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5497 return WINED3D_OK;
5501 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5502 struct wined3d_device_creation_parameters *parameters)
5504 TRACE("device %p, parameters %p.\n", device, parameters);
5506 *parameters = device->create_parms;
5509 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5511 TRACE("device %p.\n", device);
5513 return device->wined3d;
5516 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5517 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5519 struct wined3d_swapchain *swapchain;
5521 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5522 device, swapchain_idx, flags, ramp);
5524 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5525 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5528 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5529 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5531 struct wined3d_swapchain *swapchain;
5533 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5534 device, swapchain_idx, ramp);
5536 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5537 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5540 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5542 TRACE("device %p, resource %p.\n", device, resource);
5544 wined3d_not_from_cs(device->cs);
5546 list_add_head(&device->resources, &resource->resource_list_entry);
5549 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5551 TRACE("device %p, resource %p.\n", device, resource);
5553 wined3d_not_from_cs(device->cs);
5555 list_remove(&resource->resource_list_entry);
5558 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5560 enum wined3d_resource_type type = resource->type;
5561 struct wined3d_state *state = device->cs->c.state;
5562 struct wined3d_rendertarget_view *rtv;
5563 unsigned int i;
5565 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5567 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5569 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5570 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5573 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5574 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5576 switch (type)
5578 case WINED3D_RTYPE_TEXTURE_1D:
5579 case WINED3D_RTYPE_TEXTURE_2D:
5580 case WINED3D_RTYPE_TEXTURE_3D:
5581 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5583 if (&state->textures[i]->resource == resource)
5585 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5586 state->textures[i] = NULL;
5589 break;
5591 case WINED3D_RTYPE_BUFFER:
5592 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5594 if (&state->streams[i].buffer->resource == resource)
5596 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5597 state->streams[i].buffer = NULL;
5601 if (&state->index_buffer->resource == resource)
5603 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5604 state->index_buffer = NULL;
5606 break;
5608 default:
5609 break;
5612 /* Remove the resource from the resourceStore */
5613 device_resource_remove(device, resource);
5615 TRACE("Resource released.\n");
5618 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5620 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5621 struct wined3d_so_desc_entry, entry)->desc;
5622 const struct wined3d_stream_output_desc *k = key;
5623 unsigned int i;
5624 int ret;
5626 if ((ret = (k->element_count - desc->element_count)))
5627 return ret;
5628 if ((ret = (k->buffer_stride_count - desc->buffer_stride_count)))
5629 return ret;
5630 if ((ret = (k->rasterizer_stream_idx - desc->rasterizer_stream_idx)))
5631 return ret;
5633 for (i = 0; i < k->element_count; ++i)
5635 const struct wined3d_stream_output_element *b = &desc->elements[i];
5636 const struct wined3d_stream_output_element *a = &k->elements[i];
5638 if ((ret = (a->stream_idx - b->stream_idx)))
5639 return ret;
5640 if ((ret = strcmp(a->semantic_name, b->semantic_name)))
5641 return ret;
5642 if ((ret = (a->semantic_idx - b->semantic_idx)))
5643 return ret;
5644 if ((ret = (a->component_idx - b->component_idx)))
5645 return ret;
5646 if ((ret = (a->component_count - b->component_count)))
5647 return ret;
5648 if ((ret = (a->output_slot - b->output_slot)))
5649 return ret;
5652 for (i = 0; i < k->buffer_stride_count; ++i)
5654 if ((ret = (k->buffer_strides[i] - desc->buffer_strides[i])))
5655 return ret;
5658 return 0;
5661 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5663 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5665 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5668 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
5670 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
5672 return memcmp(&state->desc, key, sizeof(state->desc));
5675 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
5677 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
5679 return memcmp(&state->desc, key, sizeof(state->desc));
5682 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
5684 const struct wined3d_depth_stencil_state *state
5685 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
5687 return memcmp(&state->desc, key, sizeof(state->desc));
5690 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5691 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5692 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5693 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
5695 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5696 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
5697 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5698 unsigned int i;
5699 HRESULT hr;
5701 device->ref = 1;
5702 device->wined3d = wined3d;
5703 wined3d_incref(device->wined3d);
5704 device->adapter = adapter;
5705 device->device_parent = device_parent;
5706 list_init(&device->resources);
5707 list_init(&device->shaders);
5708 device->surface_alignment = surface_alignment;
5710 /* Save the creation parameters. */
5711 device->create_parms.adapter_idx = adapter_idx;
5712 device->create_parms.device_type = device_type;
5713 device->create_parms.focus_window = focus_window;
5714 device->create_parms.flags = flags;
5716 device->shader_backend = adapter->shader_backend;
5718 vertex_pipeline = adapter->vertex_pipe;
5720 fragment_pipeline = adapter->fragment_pipe;
5722 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
5723 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5724 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
5725 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
5726 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
5728 if (vertex_pipeline->vp_states && fragment_pipeline->states
5729 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5730 &adapter->d3d_info, supported_extensions, vertex_pipeline,
5731 fragment_pipeline, adapter->misc_state_template)))
5733 ERR("Failed to compile state table, hr %#x.\n", hr);
5734 wine_rb_destroy(&device->samplers, NULL, NULL);
5735 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5736 wine_rb_destroy(&device->blend_states, NULL, NULL);
5737 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5738 wine_rb_destroy(&device->so_descs, NULL, NULL);
5739 wined3d_decref(device->wined3d);
5740 return hr;
5743 device->max_frame_latency = 3;
5745 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
5747 WARN("Failed to create command stream.\n");
5748 hr = E_FAIL;
5749 goto err;
5752 return WINED3D_OK;
5754 err:
5755 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5757 heap_free(device->multistate_funcs[i]);
5759 wine_rb_destroy(&device->samplers, NULL, NULL);
5760 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5761 wine_rb_destroy(&device->blend_states, NULL, NULL);
5762 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5763 wine_rb_destroy(&device->so_descs, NULL, NULL);
5764 wined3d_decref(device->wined3d);
5765 return hr;
5768 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
5770 unsigned int representative, i, idx, shift;
5771 struct wined3d_context *context;
5773 wined3d_from_cs(device->cs);
5775 if (STATE_IS_COMPUTE(state_id))
5777 for (i = 0; i < device->context_count; ++i)
5778 context_invalidate_compute_state(device->contexts[i], state_id);
5779 return;
5782 representative = device->state_table[state_id].representative;
5783 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
5784 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
5785 for (i = 0; i < device->context_count; ++i)
5787 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
5791 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5792 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5794 if (message == WM_DESTROY)
5796 TRACE("unregister window %p.\n", window);
5797 wined3d_unregister_window(window);
5799 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5800 ERR("Window %p is not the focus window for device %p.\n", window, device);
5802 else if (message == WM_DISPLAYCHANGE)
5804 device->device_parent->ops->mode_changed(device->device_parent);
5806 else if (message == WM_ACTIVATEAPP)
5808 unsigned int i = device->swapchain_count;
5810 /* Deactivating the implicit swapchain may cause the application
5811 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5812 * deactivate the implicit swapchain last, and to avoid accessing the
5813 * "device" pointer afterwards. */
5814 while (i--)
5815 wined3d_swapchain_activate(device->swapchains[i], wparam);
5817 else if (message == WM_SYSCOMMAND)
5819 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5821 if (unicode)
5822 DefWindowProcW(window, message, wparam, lparam);
5823 else
5824 DefWindowProcA(window, message, wparam, lparam);
5828 if (unicode)
5829 return CallWindowProcW(proc, window, message, wparam, lparam);
5830 else
5831 return CallWindowProcA(proc, window, message, wparam, lparam);