winepulse: Move AudioClient's GetService into mmdevapi.
[wine.git] / dlls / wined3d / device.c
blob6c9d7f4368cf5e2d7810bc3f4692eff9ca079a8d
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 "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
31 WINE_DECLARE_DEBUG_CHANNEL(winediag);
33 struct wined3d_matrix_3x3
35 float _11, _12, _13;
36 float _21, _22, _23;
37 float _31, _32, _33;
40 struct light_transformed
42 struct wined3d_color diffuse, specular, ambient;
43 struct wined3d_vec4 position;
44 struct wined3d_vec3 direction;
45 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
48 struct lights_settings
50 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
51 struct wined3d_color ambient_light;
52 struct wined3d_matrix modelview_matrix;
53 struct wined3d_matrix_3x3 normal_matrix;
54 struct wined3d_vec4 position_transformed;
56 float fog_start, fog_end, fog_density;
58 uint32_t point_light_count : 8;
59 uint32_t spot_light_count : 8;
60 uint32_t directional_light_count : 8;
61 uint32_t parallel_point_light_count : 8;
62 uint32_t lighting : 1;
63 uint32_t legacy_lighting : 1;
64 uint32_t normalise : 1;
65 uint32_t localviewer : 1;
66 uint32_t fog_coord_mode : 2;
67 uint32_t fog_mode : 2;
68 uint32_t padding : 24;
71 /* Define the default light parameters as specified by MSDN. */
72 const struct wined3d_light WINED3D_default_light =
74 WINED3D_LIGHT_DIRECTIONAL, /* Type */
75 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
76 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
77 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
78 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
79 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
80 0.0f, /* Range */
81 0.0f, /* Falloff */
82 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
83 0.0f, /* Theta */
84 0.0f /* Phi */
87 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
89 struct wined3d_context **new_array;
91 TRACE("Adding context %p.\n", context);
93 if (!device->shader_backend->shader_allocate_context_data(context))
95 ERR("Failed to allocate shader backend context data.\n");
96 return FALSE;
98 device->shader_backend->shader_init_context_state(context);
100 if (!device->adapter->fragment_pipe->allocate_context_data(context))
102 ERR("Failed to allocate fragment pipeline context data.\n");
103 device->shader_backend->shader_free_context_data(context);
104 return FALSE;
107 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
109 ERR("Failed to grow the context array.\n");
110 device->adapter->fragment_pipe->free_context_data(context);
111 device->shader_backend->shader_free_context_data(context);
112 return FALSE;
115 new_array[device->context_count++] = context;
116 device->contexts = new_array;
118 return TRUE;
121 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
123 struct wined3d_context **new_array;
124 BOOL found = FALSE;
125 UINT i;
127 TRACE("Removing context %p.\n", context);
129 device->adapter->fragment_pipe->free_context_data(context);
130 device->shader_backend->shader_free_context_data(context);
132 for (i = 0; i < device->context_count; ++i)
134 if (device->contexts[i] == context)
136 found = TRUE;
137 break;
141 if (!found)
143 ERR("Context %p doesn't exist in context array.\n", context);
144 return;
147 if (!--device->context_count)
149 heap_free(device->contexts);
150 device->contexts = NULL;
151 return;
154 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
155 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
157 ERR("Failed to shrink context array. Oh well.\n");
158 return;
161 device->contexts = new_array;
164 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
166 unsigned int refcount = InterlockedIncrement(&device->ref);
168 TRACE("%p increasing refcount to %u.\n", device, refcount);
170 return refcount;
173 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
175 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
177 heap_free(s);
180 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
182 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
184 ERR("Leftover sampler %p.\n", sampler);
187 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
189 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
191 ERR("Leftover rasterizer state %p.\n", state);
194 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
196 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
198 ERR("Leftover blend state %p.\n", blend_state);
201 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
203 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
205 ERR("Leftover depth/stencil state %p.\n", state);
208 void wined3d_device_cleanup(struct wined3d_device *device)
210 unsigned int i;
212 if (device->swapchain_count)
213 wined3d_device_uninit_3d(device);
215 wined3d_cs_destroy(device->cs);
217 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
219 heap_free(device->multistate_funcs[i]);
220 device->multistate_funcs[i] = NULL;
223 if (!list_empty(&device->resources))
225 struct wined3d_resource *resource;
227 ERR("Device released with resources still bound.\n");
229 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
231 ERR("Leftover resource %p with type %s (%#x).\n",
232 resource, debug_d3dresourcetype(resource->type), resource->type);
236 if (device->contexts)
237 ERR("Context array not freed!\n");
238 if (device->hardwareCursor)
239 DestroyCursor(device->hardwareCursor);
240 device->hardwareCursor = 0;
242 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
243 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
244 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
245 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
246 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
248 wined3d_lock_cleanup(&device->bo_map_lock);
250 wined3d_decref(device->wined3d);
251 device->wined3d = NULL;
254 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
256 unsigned int refcount = InterlockedDecrement(&device->ref);
258 TRACE("%p decreasing refcount to %u.\n", device, refcount);
260 if (!refcount)
262 wined3d_mutex_lock();
263 device->adapter->adapter_ops->adapter_destroy_device(device);
264 TRACE("Destroyed device %p.\n", device);
265 wined3d_mutex_unlock();
268 return refcount;
271 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
273 TRACE("device %p.\n", device);
275 return device->swapchain_count;
278 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
280 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
282 if (swapchain_idx >= device->swapchain_count)
284 WARN("swapchain_idx %u >= swapchain_count %u.\n",
285 swapchain_idx, device->swapchain_count);
286 return NULL;
289 return device->swapchains[swapchain_idx];
292 static void device_load_logo(struct wined3d_device *device, const char *filename)
294 struct wined3d_color_key color_key;
295 struct wined3d_resource_desc desc;
296 HBITMAP hbm;
297 BITMAP bm;
298 HRESULT hr;
299 HDC dcb = NULL, dcs = NULL;
301 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
303 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
304 return;
306 GetObjectA(hbm, sizeof(BITMAP), &bm);
308 if (!(dcb = CreateCompatibleDC(NULL)))
309 goto out;
310 SelectObject(dcb, hbm);
312 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
313 desc.format = WINED3DFMT_B5G6R5_UNORM;
314 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
315 desc.multisample_quality = 0;
316 desc.usage = WINED3DUSAGE_DYNAMIC;
317 desc.bind_flags = 0;
318 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
319 desc.width = bm.bmWidth;
320 desc.height = bm.bmHeight;
321 desc.depth = 1;
322 desc.size = 0;
323 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
324 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
326 ERR("Wine logo requested, but failed to create texture, hr %#lx.\n", hr);
327 goto out;
330 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
332 wined3d_texture_decref(device->logo_texture);
333 device->logo_texture = NULL;
334 goto out;
336 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
337 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
339 color_key.color_space_low_value = 0;
340 color_key.color_space_high_value = 0;
341 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
343 out:
344 if (dcb) DeleteDC(dcb);
345 if (hbm) DeleteObject(hbm);
348 /* Context activation is done by the caller. */
349 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
350 struct wined3d_context_gl *context_gl)
352 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
353 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
354 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
355 unsigned int i;
356 DWORD color;
358 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
359 color = 0x000000ff;
360 else
361 color = 0x00000000;
363 /* Under DirectX you can sample even if no texture is bound, whereas
364 * OpenGL will only allow that when a valid texture is bound.
365 * We emulate this by creating dummy textures and binding them
366 * to each texture stage when the currently set D3D texture is NULL. */
367 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
369 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
370 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
371 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
372 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
373 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
375 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
376 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
377 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
378 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
379 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
381 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
383 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
384 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
385 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
386 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
387 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
390 if (gl_info->supported[EXT_TEXTURE3D])
392 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
393 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
394 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
395 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
396 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
399 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
401 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
402 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
403 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
404 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
406 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
407 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
411 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
413 DWORD cube_array_data[6];
415 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
416 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
417 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
418 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
419 cube_array_data[i] = color;
420 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
421 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
424 if (gl_info->supported[EXT_TEXTURE_ARRAY])
426 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
427 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
428 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
429 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
430 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
432 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
433 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
434 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
435 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
436 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
439 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
441 GLuint buffer;
443 GL_EXTCALL(glGenBuffers(1, &buffer));
444 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
445 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
446 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
448 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
449 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
450 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
451 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
452 GL_EXTCALL(glDeleteBuffers(1, &buffer));
455 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
457 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
458 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
459 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
460 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
462 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
463 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
464 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
465 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
467 if (gl_info->supported[ARB_CLEAR_TEXTURE])
469 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
470 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
472 else
474 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
478 checkGLcall("create dummy textures");
480 wined3d_context_gl_bind_dummy_textures(context_gl);
483 /* Context activation is done by the caller. */
484 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
485 struct wined3d_context_gl *context_gl)
487 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
488 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
490 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
492 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
493 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
496 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
497 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
499 if (gl_info->supported[EXT_TEXTURE_ARRAY])
501 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
502 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
505 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
506 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
508 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
509 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
511 if (gl_info->supported[EXT_TEXTURE3D])
512 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
514 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
515 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
517 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
518 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
520 checkGLcall("delete dummy textures");
522 memset(dummy_textures, 0, sizeof(*dummy_textures));
525 /* Context activation is done by the caller. */
526 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
528 struct wined3d_sampler_desc desc;
529 HRESULT hr;
531 desc.address_u = WINED3D_TADDRESS_WRAP;
532 desc.address_v = WINED3D_TADDRESS_WRAP;
533 desc.address_w = WINED3D_TADDRESS_WRAP;
534 memset(desc.border_color, 0, sizeof(desc.border_color));
535 desc.mag_filter = WINED3D_TEXF_POINT;
536 desc.min_filter = WINED3D_TEXF_POINT;
537 desc.mip_filter = WINED3D_TEXF_NONE;
538 desc.lod_bias = 0.0f;
539 desc.min_lod = -1000.0f;
540 desc.max_lod = 1000.0f;
541 desc.mip_base_level = 0;
542 desc.max_anisotropy = 1;
543 desc.compare = FALSE;
544 desc.comparison_func = WINED3D_CMP_NEVER;
545 desc.srgb_decode = TRUE;
547 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
548 * instructions allow access to resources without using samplers.
549 * In GLSL, resources are always accessed through sampler or image variables. The default
550 * sampler object is used to emulate the direct resource access when there is no sampler state
551 * to use.
553 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
555 ERR("Failed to create default sampler, hr %#lx.\n", hr);
556 device->default_sampler = NULL;
559 /* In D3D10+, a NULL sampler maps to the default sampler state. */
560 desc.address_u = WINED3D_TADDRESS_CLAMP;
561 desc.address_v = WINED3D_TADDRESS_CLAMP;
562 desc.address_w = WINED3D_TADDRESS_CLAMP;
563 desc.mag_filter = WINED3D_TEXF_LINEAR;
564 desc.min_filter = WINED3D_TEXF_LINEAR;
565 desc.mip_filter = WINED3D_TEXF_LINEAR;
566 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
568 ERR("Failed to create null sampler, hr %#lx.\n", hr);
569 device->null_sampler = NULL;
573 void wined3d_device_destroy_default_samplers(struct wined3d_device *device)
575 wined3d_sampler_decref(device->default_sampler);
576 device->default_sampler = NULL;
577 wined3d_sampler_decref(device->null_sampler);
578 device->null_sampler = NULL;
581 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
582 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
584 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
585 VkImageSubresourceRange range;
586 uint32_t flags = 0;
588 static const VkClearColorValue colour = {{0}};
590 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
591 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
593 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
594 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
596 if (!wined3d_context_vk_create_image(context_vk, type,
597 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
598 1, 1, 1, sample_count, 1, layer_count, flags, image))
600 return false;
603 wined3d_context_vk_reference_image(context_vk, image);
605 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
606 range.baseMipLevel = 0;
607 range.levelCount = 1;
608 range.baseArrayLayer = 0;
609 range.layerCount = layer_count;
611 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
612 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
613 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
615 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
616 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
618 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
619 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
620 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
622 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
623 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
625 return true;
628 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
629 struct wined3d_context_vk *context_vk)
631 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
632 const struct wined3d_vk_info *vk_info;
633 const struct wined3d_format *format;
634 VkMemoryPropertyFlags memory_type;
635 VkCommandBuffer vk_command_buffer;
636 unsigned int sample_count = 2;
637 VkBufferUsageFlags usage;
639 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
640 while (sample_count && !(sample_count & format->multisample_types))
641 sample_count <<= 1;
643 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
645 ERR("Failed to get command buffer.\n");
646 return false;
649 vk_info = context_vk->vk_info;
651 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
652 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
653 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
654 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
655 return false;
656 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.b.buffer_offset, r->bo.size, 0x00000000u));
657 r->buffer_info.buffer = r->bo.vk_buffer;
658 r->buffer_info.offset = r->bo.b.buffer_offset;
659 r->buffer_info.range = r->bo.size;
661 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
663 ERR("Failed to create 1D image.\n");
664 goto fail;
667 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
669 ERR("Failed to create 2D image.\n");
670 goto fail;
673 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
675 ERR("Failed to create 2D MSAA image.\n");
676 goto fail;
679 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
681 ERR("Failed to create 3D image.\n");
682 goto fail;
685 return true;
687 fail:
688 if (r->image_2dms.vk_image)
689 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
690 if (r->image_2d.vk_image)
691 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
692 if (r->image_1d.vk_image)
693 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
694 wined3d_context_vk_reference_bo(context_vk, &r->bo);
695 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
696 return false;
699 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
700 struct wined3d_context_vk *context_vk)
702 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
704 /* We don't track command buffer references to NULL resources. We easily
705 * could, but it doesn't seem worth it. */
706 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
707 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
708 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
709 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
710 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
711 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
712 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
713 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
714 wined3d_context_vk_reference_bo(context_vk, &r->bo);
715 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
718 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
720 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
721 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
722 VkBufferViewCreateInfo buffer_create_info;
723 const struct wined3d_vk_info *vk_info;
724 VkImageViewCreateInfo view_desc;
725 VkResult vr;
727 vk_info = context_vk->vk_info;
729 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
730 buffer_create_info.pNext = NULL;
731 buffer_create_info.flags = 0;
732 buffer_create_info.buffer = r->bo.vk_buffer;
733 buffer_create_info.format = VK_FORMAT_R32_UINT;
734 buffer_create_info.offset = r->bo.b.buffer_offset;
735 buffer_create_info.range = r->bo.size;
737 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
738 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
740 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
741 return false;
743 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
745 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
746 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
747 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
749 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
750 goto fail;
752 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
754 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
755 view_desc.pNext = NULL;
756 view_desc.flags = 0;
757 view_desc.image = r->image_1d.vk_image;
758 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
759 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
760 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
761 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
762 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
763 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
764 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
765 view_desc.subresourceRange.baseMipLevel = 0;
766 view_desc.subresourceRange.levelCount = 1;
767 view_desc.subresourceRange.baseArrayLayer = 0;
768 view_desc.subresourceRange.layerCount = 1;
769 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
771 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
772 goto fail;
774 v->vk_info_1d.sampler = VK_NULL_HANDLE;
775 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
776 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
778 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
779 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
781 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
782 goto fail;
784 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
785 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
786 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
788 view_desc.image = r->image_2d.vk_image;
789 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
790 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
792 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
793 goto fail;
795 v->vk_info_2d.sampler = VK_NULL_HANDLE;
796 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
797 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
799 view_desc.image = r->image_2dms.vk_image;
800 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
801 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
803 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
804 goto fail;
806 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
807 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
808 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
810 view_desc.image = r->image_3d.vk_image;
811 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
812 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
814 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
815 goto fail;
817 v->vk_info_3d.sampler = VK_NULL_HANDLE;
818 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
819 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
821 view_desc.image = r->image_2d.vk_image;
822 view_desc.subresourceRange.layerCount = 6;
823 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
824 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
826 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
827 goto fail;
829 v->vk_info_cube.sampler = VK_NULL_HANDLE;
830 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
831 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
833 view_desc.subresourceRange.layerCount = 1;
834 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
835 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
837 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
838 goto fail;
840 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
841 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
842 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
844 view_desc.image = r->image_2dms.vk_image;
845 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
846 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
848 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
849 goto fail;
851 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
852 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
853 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
855 view_desc.image = r->image_2d.vk_image;
856 view_desc.subresourceRange.layerCount = 6;
857 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
858 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube_array.imageView))) < 0)
860 ERR("Failed to create cube array image view, vr %s.\n", wined3d_debug_vkresult(vr));
861 goto fail;
863 v->vk_info_cube_array.sampler = VK_NULL_HANDLE;
864 v->vk_info_cube_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
865 TRACE("Created cube array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube_array.imageView));
867 return true;
869 fail:
870 if (v->vk_info_cube_array.imageView)
871 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube_array.imageView, NULL));
872 if (v->vk_info_2d_array.imageView)
873 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
874 if (v->vk_info_cube.imageView)
875 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
876 if (v->vk_info_3d.imageView)
877 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
878 if (v->vk_info_2dms.imageView)
879 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
880 if (v->vk_info_2d.imageView)
881 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
882 if (v->vk_info_1d_array.imageView)
883 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
884 if (v->vk_info_1d.imageView)
885 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
886 if (v->vk_view_buffer_float)
887 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
888 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
889 return false;
892 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
894 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
895 uint64_t id = context_vk->current_command_buffer.id;
897 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube_array.imageView, id);
898 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
899 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
900 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
901 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
902 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
903 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
904 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
905 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
907 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
908 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
911 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
913 unsigned int screensaver_active;
915 TRACE("device %p, window %p.\n", device, window);
917 if (!wined3d_register_window(NULL, window, device, 0))
919 ERR("Failed to register window %p.\n", window);
920 return E_FAIL;
923 InterlockedExchangePointer((void **)&device->focus_window, window);
924 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
925 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
926 if ((device->restore_screensaver = !!screensaver_active))
927 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
929 return WINED3D_OK;
932 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
934 TRACE("device %p.\n", device);
936 if (device->focus_window) wined3d_unregister_window(device->focus_window);
937 InterlockedExchangePointer((void **)&device->focus_window, NULL);
938 if (device->restore_screensaver)
940 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
941 device->restore_screensaver = FALSE;
945 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
947 struct wined3d_rendertarget_view *views[WINED3D_MAX_RENDER_TARGETS] = {0};
948 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
949 struct wined3d_device_context *context = &device->cs->c;
951 if (device->back_buffer_view)
952 views[0] = device->back_buffer_view;
953 wined3d_device_context_set_rendertarget_views(context, 0,
954 device->adapter->d3d_info.limits.max_rt_count, views, !!device->back_buffer_view);
956 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
959 static struct wined3d_allocator_chunk *wined3d_allocator_gl_create_chunk(struct wined3d_allocator *allocator,
960 struct wined3d_context *context, unsigned int memory_type, size_t chunk_size)
962 struct wined3d_allocator_chunk_gl *chunk_gl;
963 struct wined3d_context_gl *context_gl;
965 TRACE("allocator %p, context %p, memory_type %u, chunk_size %Iu.\n", allocator, context, memory_type, chunk_size);
967 if (!context)
968 return NULL;
969 context_gl = wined3d_context_gl(context);
971 if (!(chunk_gl = heap_alloc(sizeof(*chunk_gl))))
972 return NULL;
974 if (!wined3d_allocator_chunk_init(&chunk_gl->c, allocator))
976 heap_free(chunk_gl);
977 return NULL;
980 chunk_gl->memory_type = memory_type;
981 if (!(chunk_gl->gl_buffer = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type, chunk_size)))
983 wined3d_allocator_chunk_cleanup(&chunk_gl->c);
984 heap_free(chunk_gl);
985 return NULL;
987 list_add_head(&allocator->pools[memory_type].chunks, &chunk_gl->c.entry);
989 return &chunk_gl->c;
992 static void wined3d_allocator_gl_destroy_chunk(struct wined3d_allocator_chunk *chunk)
994 struct wined3d_device_gl *device_gl = wined3d_device_gl_from_allocator(chunk->allocator);
995 struct wined3d_allocator_chunk_gl *chunk_gl = wined3d_allocator_chunk_gl(chunk);
996 const struct wined3d_gl_info *gl_info;
997 struct wined3d_context_gl *context_gl;
999 TRACE("chunk %p.\n", chunk);
1001 context_gl = wined3d_context_gl(context_acquire(&device_gl->d, NULL, 0));
1002 gl_info = context_gl->gl_info;
1004 wined3d_context_gl_bind_bo(context_gl, GL_PIXEL_UNPACK_BUFFER, chunk_gl->gl_buffer);
1005 if (chunk_gl->c.map_ptr)
1006 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
1007 GL_EXTCALL(glDeleteBuffers(1, &chunk_gl->gl_buffer));
1008 TRACE("Freed buffer %u.\n", chunk_gl->gl_buffer);
1009 wined3d_allocator_chunk_cleanup(&chunk_gl->c);
1010 heap_free(chunk_gl);
1012 context_release(&context_gl->c);
1015 static const struct wined3d_allocator_ops wined3d_allocator_gl_ops =
1017 .allocator_create_chunk = wined3d_allocator_gl_create_chunk,
1018 .allocator_destroy_chunk = wined3d_allocator_gl_destroy_chunk,
1021 static const struct
1023 GLbitfield flags;
1025 gl_memory_types[] =
1027 {0},
1028 {GL_MAP_READ_BIT},
1029 {GL_MAP_WRITE_BIT},
1030 {GL_MAP_READ_BIT | GL_MAP_WRITE_BIT},
1032 {GL_CLIENT_STORAGE_BIT},
1033 {GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT},
1034 {GL_CLIENT_STORAGE_BIT | GL_MAP_WRITE_BIT},
1035 {GL_CLIENT_STORAGE_BIT | GL_MAP_READ_BIT | GL_MAP_WRITE_BIT},
1038 static unsigned int wined3d_device_gl_find_memory_type(GLbitfield flags)
1040 unsigned int i;
1042 for (i = 0; i < ARRAY_SIZE(gl_memory_types); ++i)
1044 if (gl_memory_types[i].flags == flags)
1045 return i;
1048 assert(0);
1049 return 0;
1052 GLbitfield wined3d_device_gl_get_memory_type_flags(unsigned int memory_type_idx)
1054 return gl_memory_types[memory_type_idx].flags;
1057 static struct wined3d_allocator_block *wined3d_device_gl_allocate_memory(struct wined3d_device_gl *device_gl,
1058 struct wined3d_context_gl *context_gl, unsigned int memory_type, GLsizeiptr size, GLuint *id)
1060 struct wined3d_allocator *allocator = &device_gl->allocator;
1061 struct wined3d_allocator_block *block;
1063 wined3d_device_gl_allocator_lock(device_gl);
1065 if (size > WINED3D_ALLOCATOR_CHUNK_SIZE / 2)
1067 if (context_gl)
1068 *id = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type, size);
1069 wined3d_device_gl_allocator_unlock(device_gl);
1070 return NULL;
1073 if (!(block = wined3d_allocator_allocate(allocator, context_gl ? &context_gl->c : NULL, memory_type, size)))
1075 wined3d_device_gl_allocator_unlock(device_gl);
1076 *id = 0;
1077 return NULL;
1080 *id = wined3d_allocator_chunk_gl(block->chunk)->gl_buffer;
1082 wined3d_device_gl_allocator_unlock(device_gl);
1083 TRACE("Allocated offset %#Ix from chunk %p.\n", block->offset, block->chunk);
1084 return block;
1087 static bool use_buffer_chunk_suballocation(struct wined3d_device_gl *device_gl,
1088 const struct wined3d_gl_info *gl_info, GLenum binding)
1090 switch (binding)
1092 case GL_ARRAY_BUFFER:
1093 case GL_ATOMIC_COUNTER_BUFFER:
1094 case GL_DRAW_INDIRECT_BUFFER:
1095 case GL_PIXEL_UNPACK_BUFFER:
1096 case GL_UNIFORM_BUFFER:
1097 return true;
1099 case GL_ELEMENT_ARRAY_BUFFER:
1100 /* There is no way to specify an element array buffer offset for
1101 * indirect draws in OpenGL. */
1102 return device_gl->d.wined3d->flags & WINED3D_NO_DRAW_INDIRECT
1103 || !gl_info->supported[ARB_DRAW_INDIRECT];
1105 case GL_TEXTURE_BUFFER:
1106 return gl_info->supported[ARB_TEXTURE_BUFFER_RANGE];
1108 default:
1109 return false;
1113 bool wined3d_device_gl_create_bo(struct wined3d_device_gl *device_gl, struct wined3d_context_gl *context_gl,
1114 GLsizeiptr size, GLenum binding, GLenum usage, bool coherent, GLbitfield flags, struct wined3d_bo_gl *bo)
1116 unsigned int memory_type_idx = wined3d_device_gl_find_memory_type(flags);
1117 const struct wined3d_gl_info *gl_info = &device_gl->d.adapter->gl_info;
1118 struct wined3d_allocator_block *memory = NULL;
1119 GLsizeiptr buffer_offset = 0;
1120 GLuint id = 0;
1122 TRACE("device_gl %p, context_gl %p, size %Iu, binding %#x, usage %#x, coherent %#x, flags %#x, bo %p.\n",
1123 device_gl, context_gl, size, binding, usage, coherent, flags, bo);
1125 if (gl_info->supported[ARB_BUFFER_STORAGE])
1127 if (use_buffer_chunk_suballocation(device_gl, gl_info, binding))
1129 if ((memory = wined3d_device_gl_allocate_memory(device_gl, context_gl, memory_type_idx, size, &id)))
1130 buffer_offset = memory->offset;
1132 else if (context_gl)
1134 WARN_(d3d_perf)("Not allocating chunk memory for binding type %#x.\n", binding);
1135 id = wined3d_context_gl_allocate_vram_chunk_buffer(context_gl, memory_type_idx, size);
1138 if (!id)
1140 WARN("Failed to allocate buffer.\n");
1141 return false;
1144 else
1146 if (!context_gl)
1147 return false;
1149 GL_EXTCALL(glGenBuffers(1, &id));
1150 if (!id)
1152 checkGLcall("buffer object creation");
1153 return false;
1155 TRACE("Created buffer object %u.\n", id);
1156 wined3d_context_gl_bind_bo(context_gl, binding, id);
1158 if (!coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
1160 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
1161 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
1164 GL_EXTCALL(glBufferData(binding, size, NULL, usage));
1166 wined3d_context_gl_bind_bo(context_gl, binding, 0);
1167 checkGLcall("buffer object creation");
1170 bo->id = id;
1171 bo->memory = memory;
1172 bo->size = size;
1173 bo->binding = binding;
1174 bo->usage = usage;
1175 bo->flags = flags;
1176 bo->b.coherent = coherent;
1177 list_init(&bo->b.users);
1178 bo->command_fence_id = 0;
1179 bo->b.buffer_offset = buffer_offset;
1180 bo->b.memory_offset = bo->b.buffer_offset;
1181 bo->b.map_ptr = NULL;
1182 bo->b.client_map_count = 0;
1184 return true;
1187 void wined3d_device_gl_delete_opengl_contexts_cs(void *object)
1189 struct wined3d_device_gl *device_gl = object;
1190 struct wined3d_context_gl *context_gl;
1191 struct wined3d_context *context;
1192 struct wined3d_device *device;
1193 struct wined3d_shader *shader;
1195 TRACE("device %p.\n", device_gl);
1197 device = &device_gl->d;
1199 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
1201 device->shader_backend->shader_destroy(shader);
1204 context = context_acquire(device, NULL, 0);
1205 context_gl = wined3d_context_gl(context);
1206 device->blitter->ops->blitter_destroy(device->blitter, context);
1207 device->shader_backend->shader_free_private(device, context);
1208 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
1210 if (context_gl->c.d3d_info->fences)
1212 wined3d_context_gl_submit_command_fence(context_gl);
1213 wined3d_context_gl_wait_command_fence(context_gl,
1214 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1216 wined3d_allocator_cleanup(&device_gl->allocator);
1218 context_release(context);
1220 while (device->context_count)
1221 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1223 if (device_gl->backup_dc)
1225 TRACE("Destroying backup wined3d window %p, dc %p.\n", device_gl->backup_wnd, device_gl->backup_dc);
1227 wined3d_release_dc(device_gl->backup_wnd, device_gl->backup_dc);
1228 DestroyWindow(device_gl->backup_wnd);
1232 void wined3d_device_gl_create_primary_opengl_context_cs(void *object)
1234 struct wined3d_device_gl *device_gl = object;
1235 struct wined3d_context_gl *context_gl;
1236 struct wined3d_swapchain *swapchain;
1237 struct wined3d_context *context;
1238 struct wined3d_texture *target;
1239 struct wined3d_device *device;
1240 HRESULT hr;
1242 TRACE("device %p.\n", device_gl);
1244 device = &device_gl->d;
1245 swapchain = device->swapchains[0];
1246 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
1247 if (!(context = context_acquire(device, target, 0)))
1249 WARN("Failed to acquire context.\n");
1250 return;
1253 context_gl = wined3d_context_gl(context);
1255 if (!wined3d_allocator_init(&device_gl->allocator, ARRAY_SIZE(gl_memory_types), &wined3d_allocator_gl_ops))
1257 WARN("Failed to initialise allocator.\n");
1258 context_release(context);
1259 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1260 return;
1263 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1264 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1266 ERR("Failed to allocate shader private data, hr %#lx.\n", hr);
1267 wined3d_allocator_cleanup(&device_gl->allocator);
1268 context_release(context);
1269 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1270 return;
1273 if (!(device->blitter = wined3d_cpu_blitter_create()))
1275 ERR("Failed to create CPU blitter.\n");
1276 device->shader_backend->shader_free_private(device, NULL);
1277 wined3d_allocator_cleanup(&device_gl->allocator);
1278 context_release(context);
1279 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
1280 return;
1283 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1284 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1285 wined3d_arbfp_blitter_create(&device->blitter, device);
1286 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1287 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1289 wined3d_device_gl_create_dummy_textures(device_gl, context_gl);
1290 wined3d_device_create_default_samplers(device, context);
1291 context_release(context);
1294 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1296 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1297 const struct wined3d_swapchain_desc *swapchain_desc;
1298 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1299 DWORD clear_flags = 0;
1300 unsigned int i;
1301 HRESULT hr;
1303 TRACE("device %p, swapchain %p.\n", device, swapchain);
1305 if (device->d3d_initialized)
1306 return WINED3DERR_INVALIDCALL;
1308 device->swapchain_count = 1;
1309 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1311 ERR("Failed to allocate swapchain array.\n");
1312 hr = E_OUTOFMEMORY;
1313 goto err_out;
1315 device->swapchains[0] = swapchain;
1317 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1319 if (fb->render_targets[i])
1320 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1322 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1324 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1325 goto err_out;
1326 device->d3d_initialized = TRUE;
1328 swapchain_desc = &swapchain->state.desc;
1329 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1331 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1332 struct wined3d_view_desc view_desc;
1334 view_desc.format_id = back_buffer->format->id;
1335 view_desc.flags = 0;
1336 view_desc.u.texture.level_idx = 0;
1337 view_desc.u.texture.level_count = 1;
1338 view_desc.u.texture.layer_idx = 0;
1339 view_desc.u.texture.layer_count = 1;
1340 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1341 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1343 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
1344 device->adapter->adapter_ops->adapter_uninit_3d(device);
1345 device->d3d_initialized = FALSE;
1346 goto err_out;
1350 device_init_swapchain_state(device, swapchain);
1352 TRACE("All defaults now set up.\n");
1354 /* Clear the screen. */
1355 if (device->back_buffer_view)
1356 clear_flags |= WINED3DCLEAR_TARGET;
1357 if (swapchain_desc->enable_auto_depth_stencil)
1358 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1359 if (clear_flags)
1360 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1362 if (wined3d_settings.logo)
1363 device_load_logo(device, wined3d_settings.logo);
1365 return WINED3D_OK;
1367 err_out:
1368 heap_free(device->swapchains);
1369 device->swapchains = NULL;
1370 device->swapchain_count = 0;
1372 return hr;
1375 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1377 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1379 wined3d_sampler_decref(sampler);
1382 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1384 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1386 wined3d_rasterizer_state_decref(state);
1389 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1391 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1393 wined3d_blend_state_decref(blend_state);
1396 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1398 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1400 wined3d_depth_stencil_state_decref(state);
1403 void wined3d_device_uninit_3d(struct wined3d_device *device)
1405 struct wined3d_state *state = device->cs->c.state;
1406 struct wined3d_resource *resource, *cursor;
1407 struct wined3d_rendertarget_view *view;
1408 struct wined3d_texture *texture;
1410 TRACE("device %p.\n", device);
1412 if (!device->d3d_initialized)
1414 ERR("Called while 3D support was not initialised.\n");
1415 return;
1418 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1420 device->swapchain_count = 0;
1422 if ((texture = device->logo_texture))
1424 device->logo_texture = NULL;
1425 wined3d_texture_decref(texture);
1428 if ((texture = device->cursor_texture))
1430 device->cursor_texture = NULL;
1431 wined3d_texture_decref(texture);
1434 wined3d_device_context_emit_reset_state(&device->cs->c, true);
1435 state_cleanup(state);
1437 wine_rb_destroy(&device->samplers, device_free_sampler, NULL);
1438 wine_rb_destroy(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1439 wine_rb_destroy(&device->blend_states, device_free_blend_state, NULL);
1440 wine_rb_destroy(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1442 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1444 TRACE("Unloading resource %p.\n", resource);
1445 wined3d_cs_emit_unload_resource(device->cs, resource);
1448 device->adapter->adapter_ops->adapter_uninit_3d(device);
1449 device->d3d_initialized = FALSE;
1451 if ((view = device->auto_depth_stencil_view))
1453 device->auto_depth_stencil_view = NULL;
1454 if (wined3d_rendertarget_view_decref(view))
1455 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1458 if ((view = device->back_buffer_view))
1460 device->back_buffer_view = NULL;
1461 wined3d_rendertarget_view_decref(view);
1464 heap_free(device->swapchains);
1465 device->swapchains = NULL;
1467 wined3d_state_reset(state, &device->adapter->d3d_info);
1470 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1471 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1472 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1474 * There is no way to deactivate thread safety once it is enabled.
1476 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1478 TRACE("device %p.\n", device);
1480 /* For now just store the flag (needed in case of ddraw). */
1481 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1484 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1486 const struct wined3d_driver_info *driver_info;
1488 TRACE("device %p.\n", device);
1490 driver_info = &device->adapter->driver_info;
1492 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1493 wine_dbgstr_longlong(driver_info->vram_bytes),
1494 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1495 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1497 return min(UINT_MAX, driver_info->vram_bytes) - device->adapter->vram_bytes_used;
1500 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1501 unsigned int idx, unsigned int *offset)
1503 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1505 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1507 WARN("Invalid stream output %u.\n", idx);
1508 return NULL;
1511 if (offset)
1512 *offset = context->state->stream_output[idx].offset;
1513 return context->state->stream_output[idx].buffer;
1516 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1517 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1519 const struct wined3d_stream_state *stream;
1521 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1522 context, stream_idx, buffer, offset, stride);
1524 if (stream_idx >= WINED3D_MAX_STREAMS)
1526 WARN("Stream index %u out of range.\n", stream_idx);
1527 return WINED3DERR_INVALIDCALL;
1530 stream = &context->state->streams[stream_idx];
1531 *buffer = stream->buffer;
1532 if (offset)
1533 *offset = stream->offset;
1534 *stride = stream->stride;
1536 return WINED3D_OK;
1539 static void wined3d_device_set_transform(struct wined3d_device *device,
1540 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1542 TRACE("device %p, state %s, matrix %p.\n",
1543 device, debug_d3dtstype(state), matrix);
1544 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1545 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1546 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1547 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1549 /* If the new matrix is the same as the current one,
1550 * we cut off any further processing. this seems to be a reasonable
1551 * optimization because as was noticed, some apps (warcraft3 for example)
1552 * tend towards setting the same matrix repeatedly for some reason.
1554 * From here on we assume that the new matrix is different, wherever it matters. */
1555 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1557 TRACE("The application is setting the same matrix over again.\n");
1558 return;
1561 device->cs->c.state->transforms[state] = *matrix;
1562 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
1565 static void wined3d_device_get_transform(const struct wined3d_device *device,
1566 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1568 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1570 *matrix = device->cs->c.state->transforms[state];
1573 /* Note lights are real special cases. Although the device caps state only
1574 * e.g. 8 are supported, you can reference any indexes you want as long as
1575 * that number max are enabled at any one point in time. Therefore since the
1576 * indices can be anything, we need a hashmap of them. However, this causes
1577 * stateblock problems. When capturing the state block, I duplicate the
1578 * hashmap, but when recording, just build a chain pretty much of commands to
1579 * be replayed. */
1580 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
1581 unsigned int light_idx, const struct wined3d_light *light)
1583 struct wined3d_light_info *object = NULL;
1584 float rho;
1586 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
1587 return;
1589 /* Initialize the object. */
1590 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1591 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1592 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1593 light_idx, light->type, debug_color(&light->diffuse),
1594 debug_color(&light->specular), debug_color(&light->ambient),
1595 light->position.x, light->position.y, light->position.z,
1596 light->direction.x, light->direction.y, light->direction.z,
1597 light->range, light->falloff, light->theta, light->phi);
1599 switch (light->type)
1601 case WINED3D_LIGHT_POINT:
1602 /* Position */
1603 object->position.x = light->position.x;
1604 object->position.y = light->position.y;
1605 object->position.z = light->position.z;
1606 object->position.w = 1.0f;
1607 object->cutoff = 180.0f;
1608 /* FIXME: Range */
1609 break;
1611 case WINED3D_LIGHT_DIRECTIONAL:
1612 /* Direction */
1613 object->direction.x = -light->direction.x;
1614 object->direction.y = -light->direction.y;
1615 object->direction.z = -light->direction.z;
1616 object->direction.w = 0.0f;
1617 object->exponent = 0.0f;
1618 object->cutoff = 180.0f;
1619 break;
1621 case WINED3D_LIGHT_SPOT:
1622 /* Position */
1623 object->position.x = light->position.x;
1624 object->position.y = light->position.y;
1625 object->position.z = light->position.z;
1626 object->position.w = 1.0f;
1628 /* Direction */
1629 object->direction.x = light->direction.x;
1630 object->direction.y = light->direction.y;
1631 object->direction.z = light->direction.z;
1632 object->direction.w = 0.0f;
1634 /* opengl-ish and d3d-ish spot lights use too different models
1635 * for the light "intensity" as a function of the angle towards
1636 * the main light direction, so we only can approximate very
1637 * roughly. However, spot lights are rather rarely used in games
1638 * (if ever used at all). Furthermore if still used, probably
1639 * nobody pays attention to such details. */
1640 if (!light->falloff)
1642 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1643 * equations have the falloff resp. exponent parameter as an
1644 * exponent, so the spot light lighting will always be 1.0 for
1645 * both of them, and we don't have to care for the rest of the
1646 * rather complex calculation. */
1647 object->exponent = 0.0f;
1649 else
1651 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1652 if (rho < 0.0001f)
1653 rho = 0.0001f;
1654 object->exponent = -0.3f / logf(cosf(rho / 2));
1657 if (object->exponent > 128.0f)
1658 object->exponent = 128.0f;
1660 object->cutoff = (float)(light->phi * 90 / M_PI);
1661 /* FIXME: Range */
1662 break;
1664 case WINED3D_LIGHT_PARALLELPOINT:
1665 object->position.x = light->position.x;
1666 object->position.y = light->position.y;
1667 object->position.z = light->position.z;
1668 object->position.w = 1.0f;
1669 break;
1671 default:
1672 FIXME("Unrecognized light type %#x.\n", light->type);
1675 wined3d_device_context_emit_set_light(context, object);
1678 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1680 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1681 struct wined3d_light_info *light_info;
1683 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1685 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1686 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1688 TRACE("Light enabled requested but light not defined, so defining one!\n");
1689 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
1691 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1693 FIXME("Adding default lights has failed dismally\n");
1694 return;
1698 if (wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable))
1699 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
1702 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1703 UINT plane_idx, const struct wined3d_vec4 *plane)
1705 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1707 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1709 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1711 TRACE("Application has requested clipplane this device doesn't support.\n");
1712 return WINED3DERR_INVALIDCALL;
1715 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1717 TRACE("Application is setting old values over, nothing to do.\n");
1718 return WINED3D_OK;
1721 clip_planes[plane_idx] = *plane;
1723 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
1725 return WINED3D_OK;
1728 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1729 const struct wined3d_clip_status *clip_status)
1731 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1733 if (!clip_status)
1734 return WINED3DERR_INVALIDCALL;
1736 return WINED3D_OK;
1739 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1740 struct wined3d_clip_status *clip_status)
1742 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1744 if (!clip_status)
1745 return WINED3DERR_INVALIDCALL;
1747 return WINED3D_OK;
1750 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1752 TRACE("device %p, material %p.\n", device, material);
1754 device->cs->c.state->material = *material;
1755 wined3d_device_context_emit_set_material(&device->cs->c, material);
1758 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1759 enum wined3d_format_id *format, unsigned int *offset)
1761 const struct wined3d_state *state = context->state;
1763 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1765 *format = state->index_format;
1766 if (offset)
1767 *offset = state->index_offset;
1768 return state->index_buffer;
1771 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
1773 TRACE("device %p, base_index %d.\n", device, base_index);
1775 device->cs->c.state->base_vertex_index = base_index;
1778 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1779 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1781 const struct wined3d_state *state = context->state;
1782 unsigned int count;
1784 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1786 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1787 if (count && viewports)
1788 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1789 if (viewport_count)
1790 *viewport_count = state->viewport_count;
1793 static void resolve_depth_buffer(struct wined3d_device *device)
1795 const struct wined3d_state *state = device->cs->c.state;
1796 struct wined3d_rendertarget_view *src_view;
1797 struct wined3d_resource *dst_resource;
1798 struct wined3d_texture *dst_texture;
1800 if (!(dst_texture = state->textures[0]))
1801 return;
1802 dst_resource = &dst_texture->resource;
1803 if (!dst_resource->format->depth_size)
1804 return;
1805 if (!(src_view = state->fb.depth_stencil))
1806 return;
1808 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1809 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1812 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1813 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1815 const struct wined3d_state *state = context->state;
1817 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1819 *blend_factor = state->blend_factor;
1820 *sample_mask = state->sample_mask;
1821 return state->blend_state;
1824 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1825 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1827 const struct wined3d_state *state = context->state;
1829 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1831 *stencil_ref = state->stencil_ref;
1832 return state->depth_stencil_state;
1835 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1836 struct wined3d_device_context *context)
1838 TRACE("context %p.\n", context);
1840 return context->state->rasterizer_state;
1843 static void wined3d_device_set_render_state(struct wined3d_device *device,
1844 enum wined3d_render_state state, DWORD value)
1846 if (state > WINEHIGHEST_RENDER_STATE)
1848 WARN("Unhandled render state %#x.\n", state);
1849 return;
1852 if (value == device->cs->c.state->render_states[state])
1853 TRACE("Application is setting the old value over, nothing to do.\n");
1854 else
1856 device->cs->c.state->render_states[state] = value;
1857 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
1860 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1862 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1863 resolve_depth_buffer(device);
1867 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1868 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1870 TRACE("device %p, sampler_idx %u, state %s, value %#lx.\n",
1871 device, sampler_idx, debug_d3dsamplerstate(state), value);
1873 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1875 TRACE("Application is setting the old value over, nothing to do.\n");
1876 return;
1879 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1880 wined3d_device_context_emit_set_sampler_state(&device->cs->c, sampler_idx, state, value);
1883 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1884 unsigned int *rect_count, RECT *rects)
1886 const struct wined3d_state *state = context->state;
1887 unsigned int count;
1889 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1891 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1892 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1893 if (rect_count)
1894 *rect_count = state->scissor_rect_count;
1897 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1899 TRACE("context %p.\n", context);
1901 wined3d_device_context_lock(context);
1902 state_cleanup(context->state);
1903 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1904 wined3d_device_context_emit_reset_state(context, true);
1905 wined3d_device_context_unlock(context);
1908 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1910 struct wined3d_light_info *light;
1911 unsigned int i, j;
1913 TRACE("context %p, state %p.\n", context, state);
1915 wined3d_device_context_lock(context);
1916 context->state = state;
1917 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1919 wined3d_device_context_emit_set_rendertarget_views(context, 0,
1920 ARRAY_SIZE(state->fb.render_targets), state->fb.render_targets);
1922 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1923 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1925 wined3d_device_context_emit_set_stream_outputs(context, state->stream_output);
1927 wined3d_device_context_emit_set_stream_sources(context, 0, WINED3D_MAX_STREAMS, state->streams);
1929 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1930 state->index_format, state->index_offset);
1932 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1934 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1936 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1937 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1938 wined3d_device_context_emit_set_samplers(context, i, 0, MAX_SAMPLER_OBJECTS, state->sampler[i]);
1939 wined3d_device_context_emit_set_shader_resource_views(context, i, 0,
1940 MAX_SHADER_RESOURCE_VIEWS, state->shader_resource_view[i]);
1943 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1944 wined3d_device_context_emit_set_unordered_access_views(context, i, 0, MAX_UNORDERED_ACCESS_VIEWS,
1945 state->unordered_access_view[i], NULL);
1947 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_F,
1948 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1949 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_I,
1950 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1951 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_B,
1952 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1954 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_F,
1955 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1956 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_I,
1957 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1958 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_B,
1959 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1961 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1963 wined3d_device_context_emit_set_texture(context, i, state->textures[i]);
1964 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1966 wined3d_device_context_emit_set_sampler_state(context, i, j, state->sampler_states[i][j]);
1970 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1972 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1974 wined3d_device_context_emit_set_texture_state(context, i, j, state->texture_states[i][j]);
1978 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1980 if (context->device->state_table[STATE_TRANSFORM(i)].representative)
1981 wined3d_device_context_emit_set_transform(context, i, state->transforms + i);
1984 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1986 wined3d_device_context_emit_set_clip_plane(context, i, state->clip_planes + i);
1989 wined3d_device_context_emit_set_material(context, &state->material);
1991 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1992 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1994 RB_FOR_EACH_ENTRY(light, &state->light_state.lights_tree, struct wined3d_light_info, entry)
1996 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
1997 wined3d_device_context_emit_set_light_enable(context, light->OriginalIndex, light->glIndex != -1);
2000 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
2002 if (context->device->state_table[STATE_RENDER(i)].representative)
2003 wined3d_device_context_emit_set_render_state(context, i, state->render_states[i]);
2006 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
2007 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
2008 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
2009 wined3d_device_context_unlock(context);
2012 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
2014 TRACE("device %p.\n", device);
2016 return device->cs->c.state;
2019 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
2021 TRACE("device %p.\n", device);
2023 return &device->cs->c;
2026 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
2027 const struct wined3d_device_context *context)
2029 TRACE("context %p.\n", context);
2031 return context->state->vertex_declaration;
2034 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
2035 enum wined3d_shader_type type, struct wined3d_shader *shader)
2037 struct wined3d_state *state = context->state;
2038 struct wined3d_shader *prev;
2040 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
2042 wined3d_device_context_lock(context);
2043 prev = state->shader[type];
2044 if (shader == prev)
2045 goto out;
2047 if (shader)
2048 wined3d_shader_incref(shader);
2049 state->shader[type] = shader;
2050 wined3d_device_context_emit_set_shader(context, type, shader);
2051 if (prev)
2052 wined3d_shader_decref(prev);
2053 out:
2054 wined3d_device_context_unlock(context);
2057 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
2058 enum wined3d_shader_type type)
2060 TRACE("context %p, type %#x.\n", context, type);
2062 return context->state->shader[type];
2065 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
2066 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
2067 const struct wined3d_constant_buffer_state *buffers)
2069 struct wined3d_state *state = context->state;
2070 unsigned int i;
2072 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
2074 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
2076 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
2077 return;
2080 wined3d_device_context_lock(context);
2081 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
2082 goto out;
2084 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
2085 for (i = 0; i < count; ++i)
2087 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
2088 struct wined3d_buffer *buffer = buffers[i].buffer;
2090 if (buffer)
2091 wined3d_buffer_incref(buffer);
2092 state->cb[type][start_idx + i] = buffers[i];
2093 if (prev)
2094 wined3d_buffer_decref(prev);
2096 out:
2097 wined3d_device_context_unlock(context);
2100 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
2101 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
2103 struct wined3d_state *state = context->state;
2104 struct wined3d_blend_state *prev;
2106 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
2107 context, blend_state, blend_factor, sample_mask);
2109 wined3d_device_context_lock(context);
2110 prev = state->blend_state;
2111 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
2112 && sample_mask == state->sample_mask)
2113 goto out;
2115 if (blend_state)
2116 wined3d_blend_state_incref(blend_state);
2117 state->blend_state = blend_state;
2118 state->blend_factor = *blend_factor;
2119 state->sample_mask = sample_mask;
2120 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
2121 if (prev)
2122 wined3d_blend_state_decref(prev);
2123 out:
2124 wined3d_device_context_unlock(context);
2127 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
2128 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
2130 struct wined3d_state *state = context->state;
2131 struct wined3d_depth_stencil_state *prev;
2133 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
2135 wined3d_device_context_lock(context);
2136 prev = state->depth_stencil_state;
2137 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
2138 goto out;
2140 if (depth_stencil_state)
2141 wined3d_depth_stencil_state_incref(depth_stencil_state);
2142 state->depth_stencil_state = depth_stencil_state;
2143 state->stencil_ref = stencil_ref;
2144 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
2145 if (prev)
2146 wined3d_depth_stencil_state_decref(prev);
2147 out:
2148 wined3d_device_context_unlock(context);
2151 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
2152 struct wined3d_rasterizer_state *rasterizer_state)
2154 struct wined3d_state *state = context->state;
2155 struct wined3d_rasterizer_state *prev;
2157 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
2159 wined3d_device_context_lock(context);
2160 prev = state->rasterizer_state;
2161 if (prev == rasterizer_state)
2162 goto out;
2164 if (rasterizer_state)
2165 wined3d_rasterizer_state_incref(rasterizer_state);
2166 state->rasterizer_state = rasterizer_state;
2167 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
2168 if (prev)
2169 wined3d_rasterizer_state_decref(prev);
2170 out:
2171 wined3d_device_context_unlock(context);
2174 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
2175 const struct wined3d_viewport *viewports)
2177 struct wined3d_state *state = context->state;
2178 unsigned int i;
2180 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
2182 for (i = 0; i < viewport_count; ++i)
2184 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
2185 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
2188 wined3d_device_context_lock(context);
2189 if (viewport_count)
2190 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
2191 else
2192 memset(state->viewports, 0, sizeof(state->viewports));
2193 state->viewport_count = viewport_count;
2195 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
2196 wined3d_device_context_unlock(context);
2199 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
2200 const RECT *rects)
2202 struct wined3d_state *state = context->state;
2203 unsigned int i;
2205 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
2207 for (i = 0; i < rect_count; ++i)
2209 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2212 wined3d_device_context_lock(context);
2213 if (state->scissor_rect_count == rect_count
2214 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
2216 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2217 goto out;
2220 if (rect_count)
2221 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
2222 else
2223 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
2224 state->scissor_rect_count = rect_count;
2226 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
2227 out:
2228 wined3d_device_context_unlock(context);
2231 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
2232 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
2233 struct wined3d_shader_resource_view *const *const views)
2235 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
2236 struct wined3d_state *state = context->state;
2237 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
2238 unsigned int i;
2240 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
2242 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
2244 WARN("Invalid view index %u, count %u.\n", start_idx, count);
2245 return;
2248 wined3d_device_context_lock(context);
2249 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
2250 goto out;
2252 memcpy(real_views, views, count * sizeof(*views));
2254 for (i = 0; i < count; ++i)
2256 struct wined3d_shader_resource_view *view = real_views[i];
2258 if (view && (wined3d_is_srv_rtv_bound(state, view)
2259 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2261 WARN("Application is trying to bind resource which is attached as render target.\n");
2262 real_views[i] = NULL;
2266 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
2267 for (i = 0; i < count; ++i)
2269 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
2270 struct wined3d_shader_resource_view *view = real_views[i];
2272 if (view)
2274 wined3d_shader_resource_view_incref(view);
2275 wined3d_srv_bind_count_inc(view);
2278 state->shader_resource_view[type][start_idx + i] = view;
2279 if (prev)
2281 wined3d_srv_bind_count_dec(prev);
2282 wined3d_shader_resource_view_decref(prev);
2285 out:
2286 wined3d_device_context_unlock(context);
2289 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
2290 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
2292 struct wined3d_state *state = context->state;
2293 unsigned int i;
2295 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
2297 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
2299 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
2300 return;
2303 wined3d_device_context_lock(context);
2304 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2305 goto out;
2307 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2308 for (i = 0; i < count; ++i)
2310 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2311 struct wined3d_sampler *sampler = samplers[i];
2313 if (sampler)
2314 wined3d_sampler_incref(sampler);
2315 state->sampler[type][start_idx + i] = sampler;
2316 if (prev)
2317 wined3d_sampler_decref(prev);
2319 out:
2320 wined3d_device_context_unlock(context);
2323 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2324 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2325 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2327 struct wined3d_state *state = context->state;
2328 unsigned int i;
2330 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2331 context, pipeline, start_idx, count, uavs, initial_counts);
2333 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2335 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2336 return;
2339 wined3d_device_context_lock(context);
2340 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2341 goto out;
2343 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2344 for (i = 0; i < count; ++i)
2346 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2347 struct wined3d_unordered_access_view *uav = uavs[i];
2349 if (uav)
2350 wined3d_unordered_access_view_incref(uav);
2351 state->unordered_access_view[pipeline][start_idx + i] = uav;
2352 if (prev)
2353 wined3d_unordered_access_view_decref(prev);
2355 out:
2356 wined3d_device_context_unlock(context);
2359 void CDECL wined3d_device_context_set_render_targets_and_unordered_access_views(struct wined3d_device_context *context,
2360 unsigned int rtv_count, struct wined3d_rendertarget_view *const *render_target_views,
2361 struct wined3d_rendertarget_view *depth_stencil_view, UINT uav_count,
2362 struct wined3d_unordered_access_view *const *unordered_access_views, const unsigned int *initial_counts)
2364 wined3d_device_context_lock(context);
2365 if (rtv_count != ~0u)
2367 if (depth_stencil_view && !(depth_stencil_view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2369 WARN("View resource %p has incompatible %s bind flags.\n",
2370 depth_stencil_view->resource, wined3d_debug_bind_flags(depth_stencil_view->resource->bind_flags));
2371 goto out;
2374 if (FAILED(wined3d_device_context_set_rendertarget_views(context, 0, rtv_count,
2375 render_target_views, FALSE)))
2376 goto out;
2378 wined3d_device_context_set_depth_stencil_view(context, depth_stencil_view);
2381 if (uav_count != ~0u)
2383 wined3d_device_context_set_unordered_access_views(context, WINED3D_PIPELINE_GRAPHICS, 0, uav_count,
2384 unordered_access_views, initial_counts);
2386 out:
2387 wined3d_device_context_unlock(context);
2390 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2391 const struct wined3d_rendertarget_view *view, BOOL dsv)
2393 const struct wined3d_state *state = context->state;
2394 const struct wined3d_resource *resource;
2396 if (!view)
2397 return;
2398 resource = view->resource;
2400 if (resource->srv_bind_count_device)
2402 const struct wined3d_shader_resource_view *srv;
2403 unsigned int i, j;
2405 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2407 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2409 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2410 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2411 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2413 static struct wined3d_shader_resource_view *const null_srv;
2415 WARN("Application sets bound resource as render target.\n");
2416 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2423 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2424 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2426 struct wined3d_state *state = context->state;
2427 unsigned int i, max_rt_count;
2429 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2430 context, start_idx, count, views, set_viewport);
2432 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2433 if (start_idx >= max_rt_count)
2435 WARN("Only %u render targets are supported.\n", max_rt_count);
2436 return WINED3DERR_INVALIDCALL;
2438 count = min(count, max_rt_count - start_idx);
2440 for (i = 0; i < count; ++i)
2442 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2444 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2445 return WINED3DERR_INVALIDCALL;
2449 wined3d_device_context_lock(context);
2450 /* Set the viewport and scissor rectangles, if requested. Tests show that
2451 * stateblock recording is ignored, the change goes directly into the
2452 * primary stateblock. */
2453 if (!start_idx && set_viewport)
2455 state->viewports[0].x = 0;
2456 state->viewports[0].y = 0;
2457 state->viewports[0].width = views[0]->width;
2458 state->viewports[0].height = views[0]->height;
2459 state->viewports[0].min_z = 0.0f;
2460 state->viewports[0].max_z = 1.0f;
2461 state->viewport_count = 1;
2462 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2464 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2465 state->scissor_rect_count = 1;
2466 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2469 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2470 goto out;
2472 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2473 for (i = 0; i < count; ++i)
2475 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2476 struct wined3d_rendertarget_view *view = views[i];
2478 if (view)
2480 wined3d_rendertarget_view_incref(view);
2481 wined3d_rtv_bind_count_inc(view);
2483 state->fb.render_targets[start_idx + i] = view;
2484 /* Release after the assignment, to prevent device_resource_released()
2485 * from seeing the resource as still in use. */
2486 if (prev)
2488 wined3d_rtv_bind_count_dec(prev);
2489 wined3d_rendertarget_view_decref(prev);
2492 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2494 out:
2495 wined3d_device_context_unlock(context);
2496 return WINED3D_OK;
2499 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2500 struct wined3d_rendertarget_view *view)
2502 struct wined3d_fb_state *fb = &context->state->fb;
2503 struct wined3d_rendertarget_view *prev;
2505 TRACE("context %p, view %p.\n", context, view);
2507 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2509 WARN("View resource %p has incompatible %s bind flags.\n",
2510 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2511 return WINED3DERR_INVALIDCALL;
2514 wined3d_device_context_lock(context);
2515 prev = fb->depth_stencil;
2516 if (prev == view)
2518 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2519 goto out;
2522 if ((fb->depth_stencil = view))
2523 wined3d_rendertarget_view_incref(view);
2524 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2525 if (prev)
2526 wined3d_rendertarget_view_decref(prev);
2527 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2528 out:
2529 wined3d_device_context_unlock(context);
2530 return WINED3D_OK;
2533 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2534 struct wined3d_query *predicate, BOOL value)
2536 struct wined3d_state *state = context->state;
2537 struct wined3d_query *prev;
2539 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2541 wined3d_device_context_lock(context);
2542 prev = state->predicate;
2543 if (predicate)
2545 FIXME("Predicated rendering not implemented.\n");
2546 wined3d_query_incref(predicate);
2548 state->predicate = predicate;
2549 state->predicate_value = value;
2550 wined3d_device_context_emit_set_predication(context, predicate, value);
2551 if (prev)
2552 wined3d_query_decref(prev);
2553 wined3d_device_context_unlock(context);
2556 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2557 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2559 struct wined3d_state *state = context->state;
2560 unsigned int i;
2562 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2564 if (start_idx >= WINED3D_MAX_STREAMS)
2566 WARN("Start index %u is out of range.\n", start_idx);
2567 return WINED3DERR_INVALIDCALL;
2570 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2572 for (i = 0; i < count; ++i)
2574 if (streams[i].offset & 0x3)
2576 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2577 return WINED3DERR_INVALIDCALL;
2581 wined3d_device_context_lock(context);
2582 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2583 goto out;
2585 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2586 for (i = 0; i < count; ++i)
2588 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2589 struct wined3d_buffer *buffer = streams[i].buffer;
2591 state->streams[start_idx + i] = streams[i];
2593 if (buffer)
2594 wined3d_buffer_incref(buffer);
2595 if (prev)
2596 wined3d_buffer_decref(prev);
2598 out:
2599 wined3d_device_context_unlock(context);
2600 return WINED3D_OK;
2603 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2604 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2606 struct wined3d_state *state = context->state;
2607 enum wined3d_format_id prev_format;
2608 struct wined3d_buffer *prev_buffer;
2609 unsigned int prev_offset;
2611 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2612 context, buffer, debug_d3dformat(format_id), offset);
2614 wined3d_device_context_lock(context);
2615 prev_buffer = state->index_buffer;
2616 prev_format = state->index_format;
2617 prev_offset = state->index_offset;
2619 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2620 goto out;
2622 if (buffer)
2623 wined3d_buffer_incref(buffer);
2624 state->index_buffer = buffer;
2625 state->index_format = format_id;
2626 state->index_offset = offset;
2627 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2628 if (prev_buffer)
2629 wined3d_buffer_decref(prev_buffer);
2630 out:
2631 wined3d_device_context_unlock(context);
2634 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2635 struct wined3d_vertex_declaration *declaration)
2637 struct wined3d_state *state = context->state;
2638 struct wined3d_vertex_declaration *prev;
2640 TRACE("context %p, declaration %p.\n", context, declaration);
2642 wined3d_device_context_lock(context);
2643 prev = state->vertex_declaration;
2644 if (declaration == prev)
2645 goto out;
2647 if (declaration)
2648 wined3d_vertex_declaration_incref(declaration);
2649 state->vertex_declaration = declaration;
2650 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2651 if (prev)
2652 wined3d_vertex_declaration_decref(prev);
2653 out:
2654 wined3d_device_context_unlock(context);
2657 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2658 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2660 struct wined3d_state *state = context->state;
2661 unsigned int i;
2663 TRACE("context %p, outputs %p.\n", context, outputs);
2665 wined3d_device_context_lock(context);
2666 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2667 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2669 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2670 struct wined3d_buffer *buffer = outputs[i].buffer;
2672 if (buffer)
2673 wined3d_buffer_incref(buffer);
2674 state->stream_output[i] = outputs[i];
2675 if (prev_buffer)
2676 wined3d_buffer_decref(prev_buffer);
2678 wined3d_device_context_unlock(context);
2681 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2682 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2684 struct wined3d_state *state = context->state;
2686 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2687 context, start_vertex, vertex_count, start_instance, instance_count);
2689 wined3d_device_context_lock(context);
2690 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2691 0, start_vertex, vertex_count, start_instance, instance_count, false);
2692 wined3d_device_context_unlock(context);
2695 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2696 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2698 struct wined3d_state *state = context->state;
2700 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2701 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2703 wined3d_device_context_lock(context);
2704 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2705 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2706 wined3d_device_context_unlock(context);
2709 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2710 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2712 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2714 if (idx >= MAX_CONSTANT_BUFFERS)
2716 WARN("Invalid constant buffer index %u.\n", idx);
2717 return;
2720 *state = context->state->cb[shader_type][idx];
2723 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2724 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2726 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2728 WARN("Invalid view index %u.\n", idx);
2729 return NULL;
2732 return context->state->shader_resource_view[shader_type][idx];
2735 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2736 enum wined3d_shader_type shader_type, unsigned int idx)
2738 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2740 if (idx >= MAX_SAMPLER_OBJECTS)
2742 WARN("Invalid sampler index %u.\n", idx);
2743 return NULL;
2746 return context->state->sampler[shader_type][idx];
2749 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2750 unsigned int start_idx, unsigned int count, const BOOL *constants)
2752 unsigned int i;
2754 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2755 device, start_idx, count, constants);
2757 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2758 if (TRACE_ON(d3d))
2760 for (i = 0; i < count; ++i)
2761 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2764 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2767 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2768 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2770 unsigned int i;
2772 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2773 device, start_idx, count, constants);
2775 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2776 if (TRACE_ON(d3d))
2778 for (i = 0; i < count; ++i)
2779 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2782 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2785 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2786 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2788 unsigned int i;
2790 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2791 device, start_idx, count, constants);
2793 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2794 if (TRACE_ON(d3d))
2796 for (i = 0; i < count; ++i)
2797 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2800 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2803 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2804 unsigned int start_idx, unsigned int count, const BOOL *constants)
2806 unsigned int i;
2808 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2809 device, start_idx, count, constants);
2811 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2812 if (TRACE_ON(d3d))
2814 for (i = 0; i < count; ++i)
2815 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2818 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2821 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2822 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2824 unsigned int i;
2826 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2827 device, start_idx, count, constants);
2829 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2830 if (TRACE_ON(d3d))
2832 for (i = 0; i < count; ++i)
2833 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2836 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2839 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2840 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2842 unsigned int i;
2844 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2845 device, start_idx, count, constants);
2847 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2848 if (TRACE_ON(d3d))
2850 for (i = 0; i < count; ++i)
2851 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2854 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2857 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2858 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2860 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2862 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2864 WARN("Invalid UAV index %u.\n", idx);
2865 return NULL;
2868 return context->state->unordered_access_view[pipeline][idx];
2871 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2873 unsigned int i;
2875 if (!latency)
2876 latency = 3;
2878 device->max_frame_latency = latency;
2879 for (i = 0; i < device->swapchain_count; ++i)
2880 swapchain_set_max_frame_latency(device->swapchains[i], device);
2883 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2885 return device->max_frame_latency;
2888 static unsigned int wined3d_get_flexible_vertex_size(uint32_t fvf)
2890 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2891 unsigned int i, size = 0;
2893 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2894 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2895 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2896 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2897 switch (fvf & WINED3DFVF_POSITION_MASK)
2899 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2900 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2901 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2902 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2903 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2904 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2905 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2906 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2907 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2909 for (i = 0; i < texcoord_count; ++i)
2911 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2914 return size;
2917 static void wined3d_format_get_colour(const struct wined3d_format *format,
2918 const void *data, struct wined3d_color *colour)
2920 float *output = &colour->r;
2921 const uint32_t *u32_data;
2922 const uint16_t *u16_data;
2923 const float *f32_data;
2924 unsigned int i;
2926 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2927 static unsigned int warned;
2929 switch (format->id)
2931 case WINED3DFMT_B8G8R8A8_UNORM:
2932 u32_data = data;
2933 wined3d_color_from_d3dcolor(colour, *u32_data);
2934 break;
2936 case WINED3DFMT_R8G8B8A8_UNORM:
2937 u32_data = data;
2938 colour->r = (*u32_data & 0xffu) / 255.0f;
2939 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2940 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2941 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2942 break;
2944 case WINED3DFMT_R16G16_UNORM:
2945 case WINED3DFMT_R16G16B16A16_UNORM:
2946 u16_data = data;
2947 *colour = default_colour;
2948 for (i = 0; i < format->component_count; ++i)
2949 output[i] = u16_data[i] / 65535.0f;
2950 break;
2952 case WINED3DFMT_R32_FLOAT:
2953 case WINED3DFMT_R32G32_FLOAT:
2954 case WINED3DFMT_R32G32B32_FLOAT:
2955 case WINED3DFMT_R32G32B32A32_FLOAT:
2956 f32_data = data;
2957 *colour = default_colour;
2958 for (i = 0; i < format->component_count; ++i)
2959 output[i] = f32_data[i];
2960 break;
2962 default:
2963 *colour = default_colour;
2964 if (!warned++)
2965 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2966 break;
2970 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2971 const struct wined3d_color *material_colour, unsigned int index,
2972 const struct wined3d_stream_info *stream_info)
2974 const struct wined3d_stream_info_element *element = NULL;
2976 switch (mcs)
2978 case WINED3D_MCS_MATERIAL:
2979 *colour = *material_colour;
2980 return;
2982 case WINED3D_MCS_COLOR1:
2983 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2985 colour->r = colour->g = colour->b = colour->a = 1.0f;
2986 return;
2988 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2989 break;
2991 case WINED3D_MCS_COLOR2:
2992 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2994 colour->r = colour->g = colour->b = colour->a = 0.0f;
2995 return;
2997 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2998 break;
3000 default:
3001 colour->r = colour->g = colour->b = colour->a = 0.0f;
3002 ERR("Invalid material colour source %#x.\n", mcs);
3003 return;
3006 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
3009 static float wined3d_clamp(float value, float min_value, float max_value)
3011 return value < min_value ? min_value : value > max_value ? max_value : value;
3014 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3016 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
3019 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3021 v0->x -= v1->x;
3022 v0->y -= v1->y;
3023 v0->z -= v1->z;
3026 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
3028 v->x *= s;
3029 v->y *= s;
3030 v->z *= s;
3033 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
3035 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
3037 if (isfinite(rnorm))
3038 wined3d_vec3_scale(v, rnorm);
3041 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
3042 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
3044 struct wined3d_vec3 tmp;
3046 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
3047 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
3048 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
3050 *dst = tmp;
3053 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
3054 float min_value, float max_value)
3056 dst->r = wined3d_clamp(src->r, min_value, max_value);
3057 dst->g = wined3d_clamp(src->g, min_value, max_value);
3058 dst->b = wined3d_clamp(src->b, min_value, max_value);
3059 dst->a = wined3d_clamp(src->a, min_value, max_value);
3062 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
3064 dst->r += src->r * c;
3065 dst->g += src->g * c;
3066 dst->b += src->b * c;
3069 static void init_transformed_lights(struct lights_settings *ls,
3070 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
3072 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
3073 const struct wined3d_light_info *light_info;
3074 struct wined3d_light_info *light_iter;
3075 struct light_transformed *light;
3076 struct wined3d_vec4 vec4;
3077 unsigned int light_count;
3078 unsigned int i, index;
3080 memset(ls, 0, sizeof(*ls));
3082 ls->lighting = !!compute_lighting;
3083 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
3084 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
3085 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
3086 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
3087 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
3088 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
3090 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
3091 return;
3093 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
3094 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
3096 if (!compute_lighting)
3097 return;
3099 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
3101 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
3102 ls->legacy_lighting = !!legacy_lighting;
3103 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
3104 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
3106 index = 0;
3107 RB_FOR_EACH_ENTRY(light_iter, &state->light_state.lights_tree, struct wined3d_light_info, entry)
3109 if (!light_iter->enabled)
3110 continue;
3112 switch (light_iter->OriginalParms.type)
3114 case WINED3D_LIGHT_DIRECTIONAL:
3115 ++ls->directional_light_count;
3116 break;
3118 case WINED3D_LIGHT_POINT:
3119 ++ls->point_light_count;
3120 break;
3122 case WINED3D_LIGHT_SPOT:
3123 ++ls->spot_light_count;
3124 break;
3126 case WINED3D_LIGHT_PARALLELPOINT:
3127 ++ls->parallel_point_light_count;
3128 break;
3130 default:
3131 FIXME("Unhandled light type %#x.\n", light_iter->OriginalParms.type);
3132 continue;
3134 lights[index++] = light_iter;
3135 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
3136 break;
3139 light_count = index;
3140 for (i = 0, index = 0; i < light_count; ++i)
3142 light_info = lights[i];
3143 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
3144 continue;
3146 light = &ls->lights[index];
3147 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3148 light->direction = *(struct wined3d_vec3 *)&vec4;
3149 wined3d_vec3_normalise(&light->direction);
3151 light->diffuse = light_info->OriginalParms.diffuse;
3152 light->ambient = light_info->OriginalParms.ambient;
3153 light->specular = light_info->OriginalParms.specular;
3154 ++index;
3157 for (i = 0; i < light_count; ++i)
3159 light_info = lights[i];
3160 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
3161 continue;
3163 light = &ls->lights[index];
3165 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3166 light->range = light_info->OriginalParms.range;
3167 light->c_att = light_info->OriginalParms.attenuation0;
3168 light->l_att = light_info->OriginalParms.attenuation1;
3169 light->q_att = light_info->OriginalParms.attenuation2;
3171 light->diffuse = light_info->OriginalParms.diffuse;
3172 light->ambient = light_info->OriginalParms.ambient;
3173 light->specular = light_info->OriginalParms.specular;
3174 ++index;
3177 for (i = 0; i < light_count; ++i)
3179 light_info = lights[i];
3180 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
3181 continue;
3183 light = &ls->lights[index];
3185 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3186 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3187 light->direction = *(struct wined3d_vec3 *)&vec4;
3188 wined3d_vec3_normalise(&light->direction);
3189 light->range = light_info->OriginalParms.range;
3190 light->falloff = light_info->OriginalParms.falloff;
3191 light->c_att = light_info->OriginalParms.attenuation0;
3192 light->l_att = light_info->OriginalParms.attenuation1;
3193 light->q_att = light_info->OriginalParms.attenuation2;
3194 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
3195 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
3197 light->diffuse = light_info->OriginalParms.diffuse;
3198 light->ambient = light_info->OriginalParms.ambient;
3199 light->specular = light_info->OriginalParms.specular;
3200 ++index;
3203 for (i = 0; i < light_count; ++i)
3205 light_info = lights[i];
3206 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
3207 continue;
3209 light = &ls->lights[index];
3211 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3212 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
3213 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
3214 light->diffuse = light_info->OriginalParms.diffuse;
3215 light->ambient = light_info->OriginalParms.ambient;
3216 light->specular = light_info->OriginalParms.specular;
3217 ++index;
3221 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
3222 const struct wined3d_vec3 *dir, float att, float material_shininess,
3223 const struct wined3d_vec3 *normal_transformed,
3224 const struct wined3d_vec3 *position_transformed_normalised,
3225 const struct light_transformed *light, const struct lights_settings *ls)
3227 struct wined3d_vec3 vec3;
3228 float t, c;
3230 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
3231 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
3233 vec3 = *dir;
3234 if (ls->localviewer)
3235 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
3236 else
3237 vec3.z -= 1.0f;
3238 wined3d_vec3_normalise(&vec3);
3239 t = wined3d_vec3_dot(normal_transformed, &vec3);
3240 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
3241 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
3242 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
3245 static void light_set_vertex_data(struct lights_settings *ls,
3246 const struct wined3d_vec4 *position)
3248 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3249 return;
3251 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3252 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3255 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3256 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3257 float material_shininess)
3259 struct wined3d_vec3 position_transformed_normalised;
3260 struct wined3d_vec3 normal_transformed = {0.0f};
3261 const struct light_transformed *light;
3262 struct wined3d_vec3 dir, dst;
3263 unsigned int i, index;
3264 float att;
3266 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3267 wined3d_vec3_normalise(&position_transformed_normalised);
3269 if (normal)
3271 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3272 if (ls->normalise)
3273 wined3d_vec3_normalise(&normal_transformed);
3276 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3277 *specular = *diffuse;
3278 *ambient = ls->ambient_light;
3280 index = 0;
3281 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3283 light = &ls->lights[index];
3285 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3286 if (normal)
3287 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3288 &normal_transformed, &position_transformed_normalised, light, ls);
3291 for (i = 0; i < ls->point_light_count; ++i, ++index)
3293 light = &ls->lights[index];
3294 dir.x = light->position.x - ls->position_transformed.x;
3295 dir.y = light->position.y - ls->position_transformed.y;
3296 dir.z = light->position.z - ls->position_transformed.z;
3298 dst.z = wined3d_vec3_dot(&dir, &dir);
3299 dst.y = sqrtf(dst.z);
3300 dst.x = 1.0f;
3301 if (ls->legacy_lighting)
3303 dst.y = (light->range - dst.y) / light->range;
3304 if (!(dst.y > 0.0f))
3305 continue;
3306 dst.z = dst.y * dst.y;
3308 else
3310 if (!(dst.y <= light->range))
3311 continue;
3313 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3314 if (!ls->legacy_lighting)
3315 att = 1.0f / att;
3317 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3318 if (normal)
3320 wined3d_vec3_normalise(&dir);
3321 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3322 &normal_transformed, &position_transformed_normalised, light, ls);
3326 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3328 float t;
3330 light = &ls->lights[index];
3332 dir.x = light->position.x - ls->position_transformed.x;
3333 dir.y = light->position.y - ls->position_transformed.y;
3334 dir.z = light->position.z - ls->position_transformed.z;
3336 dst.z = wined3d_vec3_dot(&dir, &dir);
3337 dst.y = sqrtf(dst.z);
3338 dst.x = 1.0f;
3340 if (ls->legacy_lighting)
3342 dst.y = (light->range - dst.y) / light->range;
3343 if (!(dst.y > 0.0f))
3344 continue;
3345 dst.z = dst.y * dst.y;
3347 else
3349 if (!(dst.y <= light->range))
3350 continue;
3352 wined3d_vec3_normalise(&dir);
3353 t = -wined3d_vec3_dot(&dir, &light->direction);
3354 if (t > light->cos_htheta)
3355 att = 1.0f;
3356 else if (t <= light->cos_hphi)
3357 att = 0.0f;
3358 else
3359 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3361 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3362 if (ls->legacy_lighting)
3363 att *= t;
3364 else
3365 att /= t;
3367 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3369 if (normal)
3370 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3371 &normal_transformed, &position_transformed_normalised, light, ls);
3374 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3376 light = &ls->lights[index];
3378 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3379 if (normal)
3380 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3381 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3385 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3387 switch (ls->fog_mode)
3389 case WINED3D_FOG_NONE:
3390 return fog_coord;
3391 case WINED3D_FOG_LINEAR:
3392 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3393 case WINED3D_FOG_EXP:
3394 return expf(-fog_coord * ls->fog_density);
3395 case WINED3D_FOG_EXP2:
3396 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3397 default:
3398 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3399 return 0.0f;
3403 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3405 float fog_coord;
3407 if (ls->fog_mode == WINED3D_FOG_NONE)
3408 return;
3410 switch (ls->fog_coord_mode)
3412 case WINED3D_FFP_VS_FOG_RANGE:
3413 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3414 (const struct wined3d_vec3 *)&ls->position_transformed));
3415 break;
3417 case WINED3D_FFP_VS_FOG_DEPTH:
3418 fog_coord = fabsf(ls->position_transformed.z);
3419 break;
3421 default:
3422 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3423 return;
3425 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3428 /* Context activation is done by the caller. */
3429 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3430 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3431 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, uint32_t flags, uint32_t dst_fvf)
3433 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3434 const struct wined3d_color *material_specular_state_colour;
3435 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3436 const struct wined3d_state *state = device->cs->c.state;
3437 const struct wined3d_format *output_colour_format;
3438 static const struct wined3d_color black;
3439 struct wined3d_map_desc map_desc;
3440 struct wined3d_box box = {0};
3441 struct wined3d_viewport vp;
3442 unsigned int texture_count;
3443 struct lights_settings ls;
3444 unsigned int vertex_size;
3445 BOOL do_clip, lighting;
3446 float min_z, max_z;
3447 unsigned int i;
3448 BYTE *dest_ptr;
3449 HRESULT hr;
3451 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3453 ERR("Source has no position mask.\n");
3454 return WINED3DERR_INVALIDCALL;
3457 if (state->render_states[WINED3D_RS_CLIPPING])
3459 static BOOL warned = FALSE;
3461 * The clipping code is not quite correct. Some things need
3462 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3463 * so disable clipping for now.
3464 * (The graphics in Half-Life are broken, and my processvertices
3465 * test crashes with IDirect3DDevice3)
3466 do_clip = TRUE;
3468 do_clip = FALSE;
3469 if (!warned)
3471 warned = TRUE;
3472 FIXME("Clipping is broken and disabled for now\n");
3475 else
3476 do_clip = FALSE;
3478 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3479 box.left = dwDestIndex * vertex_size;
3480 box.right = box.left + dwCount * vertex_size;
3481 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3483 WARN("Failed to map buffer, hr %#lx.\n", hr);
3484 return hr;
3486 dest_ptr = map_desc.data;
3488 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3489 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3490 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3492 TRACE("View mat:\n");
3493 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3494 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3495 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3496 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3498 TRACE("Proj mat:\n");
3499 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3500 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3501 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3502 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3504 TRACE("World mat:\n");
3505 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3506 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3507 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3508 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3510 /* Get the viewport */
3511 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3512 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3513 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3515 multiply_matrix(&mat,&view_mat,&world_mat);
3516 multiply_matrix(&mat,&proj_mat,&mat);
3518 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3520 lighting = state->render_states[WINED3D_RS_LIGHTING]
3521 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3522 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3523 &ambient_source, &specular_source, state, stream_info);
3524 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3525 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3526 ? &state->material.specular : &black;
3527 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3528 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3530 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3532 for (i = 0; i < dwCount; ++i)
3534 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3535 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3536 struct wined3d_color ambient, diffuse, specular;
3537 struct wined3d_vec4 position;
3538 unsigned int tex_index;
3540 position.x = p[0];
3541 position.y = p[1];
3542 position.z = p[2];
3543 position.w = 1.0f;
3545 light_set_vertex_data(&ls, &position);
3547 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3548 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3549 /* The position first */
3550 float x, y, z, rhw;
3551 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3553 /* Multiplication with world, view and projection matrix. */
3554 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3555 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3556 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3557 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3559 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3561 /* WARNING: The following things are taken from d3d7 and were not yet checked
3562 * against d3d8 or d3d9!
3565 /* Clipping conditions: From msdn
3567 * A vertex is clipped if it does not match the following requirements
3568 * -rhw < x <= rhw
3569 * -rhw < y <= rhw
3570 * 0 < z <= rhw
3571 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3573 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3574 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3578 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3579 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3581 /* "Normal" viewport transformation (not clipped)
3582 * 1) The values are divided by rhw
3583 * 2) The y axis is negative, so multiply it with -1
3584 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3585 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3586 * 4) Multiply x with Width/2 and add Width/2
3587 * 5) The same for the height
3588 * 6) Add the viewpoint X and Y to the 2D coordinates and
3589 * The minimum Z value to z
3590 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3592 * Well, basically it's simply a linear transformation into viewport
3593 * coordinates
3596 x /= rhw;
3597 y /= rhw;
3598 z /= rhw;
3600 y *= -1;
3602 x *= vp.width / 2;
3603 y *= vp.height / 2;
3604 z *= max_z - min_z;
3606 x += vp.width / 2 + vp.x;
3607 y += vp.height / 2 + vp.y;
3608 z += min_z;
3610 rhw = 1 / rhw;
3611 } else {
3612 /* That vertex got clipped
3613 * Contrary to OpenGL it is not dropped completely, it just
3614 * undergoes a different calculation.
3616 TRACE("Vertex got clipped\n");
3617 x += rhw;
3618 y += rhw;
3620 x /= 2;
3621 y /= 2;
3623 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3624 * outside of the main vertex buffer memory. That needs some more
3625 * investigation...
3629 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3632 ( (float *) dest_ptr)[0] = x;
3633 ( (float *) dest_ptr)[1] = y;
3634 ( (float *) dest_ptr)[2] = z;
3635 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3637 dest_ptr += 3 * sizeof(float);
3639 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3640 dest_ptr += sizeof(float);
3643 if (dst_fvf & WINED3DFVF_PSIZE)
3644 dest_ptr += sizeof(DWORD);
3646 if (dst_fvf & WINED3DFVF_NORMAL)
3648 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3649 const float *normal = (const float *)(element->data.addr + i * element->stride);
3650 /* AFAIK this should go into the lighting information */
3651 FIXME("Didn't expect the destination to have a normal\n");
3652 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3655 if (lighting)
3657 const struct wined3d_stream_info_element *element;
3658 struct wined3d_vec3 *normal;
3660 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3662 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3663 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3665 else
3667 normal = NULL;
3669 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3670 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3673 if (dst_fvf & WINED3DFVF_DIFFUSE)
3675 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3677 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3678 &state->material.diffuse, i, stream_info);
3680 if (lighting)
3682 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3683 &state->material.ambient, i, stream_info);
3684 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3685 &state->material.emissive, i, stream_info);
3687 diffuse_colour.r = ambient.r * material_ambient.r
3688 + diffuse.r * material_diffuse.r + material_emissive.r;
3689 diffuse_colour.g = ambient.g * material_ambient.g
3690 + diffuse.g * material_diffuse.g + material_emissive.g;
3691 diffuse_colour.b = ambient.b * material_ambient.b
3692 + diffuse.b * material_diffuse.b + material_emissive.b;
3693 diffuse_colour.a = material_diffuse.a;
3695 else
3697 diffuse_colour = material_diffuse;
3699 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3700 wined3d_format_convert_from_float(output_colour_format, &diffuse_colour, dest_ptr);
3701 dest_ptr += sizeof(DWORD);
3704 if (dst_fvf & WINED3DFVF_SPECULAR)
3706 struct wined3d_color material_specular, specular_colour;
3708 wined3d_colour_from_mcs(&material_specular, specular_source,
3709 material_specular_state_colour, i, stream_info);
3711 if (lighting)
3713 specular_colour.r = specular.r * material_specular.r;
3714 specular_colour.g = specular.g * material_specular.g;
3715 specular_colour.b = specular.b * material_specular.b;
3716 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3718 else
3720 specular_colour = material_specular;
3722 update_fog_factor(&specular_colour.a, &ls);
3723 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3724 wined3d_format_convert_from_float(output_colour_format, &specular_colour, dest_ptr);
3725 dest_ptr += sizeof(DWORD);
3728 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3730 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3731 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3732 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3734 ERR("No source texture, but destination requests one\n");
3735 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3737 else
3739 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3744 wined3d_resource_unmap(&dest->resource, 0);
3746 return WINED3D_OK;
3748 #undef copy_and_next
3750 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3751 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3752 const struct wined3d_vertex_declaration *declaration, uint32_t flags, uint32_t dst_fvf)
3754 struct wined3d_state *state = device->cs->c.state;
3755 struct wined3d_stream_info stream_info;
3756 struct wined3d_resource *resource;
3757 struct wined3d_box box = {0};
3758 struct wined3d_shader *vs;
3759 unsigned int i, j;
3760 uint32_t map;
3761 HRESULT hr;
3763 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3764 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3765 device, src_start_idx, dst_idx, vertex_count,
3766 dst_buffer, declaration, flags, dst_fvf);
3768 if (declaration)
3769 FIXME("Output vertex declaration not implemented yet.\n");
3771 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3772 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3773 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3774 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3776 /* We can't convert FROM a VBO, and vertex buffers used to source into
3777 * process_vertices() are unlikely to ever be used for drawing. Release
3778 * VBOs in those buffers and fix up the stream_info structure.
3780 * Also apply the start index. */
3781 map = stream_info.use_map;
3782 while (map)
3784 struct wined3d_stream_info_element *e;
3785 struct wined3d_map_desc map_desc;
3787 i = wined3d_bit_scan(&map);
3788 e = &stream_info.elements[i];
3789 resource = &state->streams[e->stream_idx].buffer->resource;
3790 box.left = src_start_idx * e->stride;
3791 box.right = box.left + vertex_count * e->stride;
3792 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3794 ERR("Failed to map resource.\n");
3795 map = stream_info.use_map;
3796 while (map)
3798 j = wined3d_bit_scan(&map);
3799 if (j >= i)
3800 break;
3802 e = &stream_info.elements[j];
3803 resource = &state->streams[e->stream_idx].buffer->resource;
3804 if (FAILED(wined3d_resource_unmap(resource, 0)))
3805 ERR("Failed to unmap resource.\n");
3807 return WINED3DERR_INVALIDCALL;
3809 e->data.buffer_object = 0;
3810 e->data.addr += (ULONG_PTR)map_desc.data;
3813 hr = process_vertices_strided(device, dst_idx, vertex_count,
3814 &stream_info, dst_buffer, flags, dst_fvf);
3816 map = stream_info.use_map;
3817 while (map)
3819 i = wined3d_bit_scan(&map);
3820 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3821 if (FAILED(wined3d_resource_unmap(resource, 0)))
3822 ERR("Failed to unmap resource.\n");
3825 return hr;
3828 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3829 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3831 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3833 TRACE("device %p, stage %u, state %s, value %lx.\n",
3834 device, stage, debug_d3dtexturestate(state), value);
3836 if (stage >= d3d_info->limits.ffp_blend_stages)
3838 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3839 stage, d3d_info->limits.ffp_blend_stages - 1);
3840 return;
3843 if (value == device->cs->c.state->texture_states[stage][state])
3845 TRACE("Application is setting the old value over, nothing to do.\n");
3846 return;
3849 device->cs->c.state->texture_states[stage][state] = value;
3851 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
3854 static void wined3d_device_set_texture(struct wined3d_device *device,
3855 UINT stage, struct wined3d_texture *texture)
3857 struct wined3d_state *state = device->cs->c.state;
3858 struct wined3d_texture *prev;
3860 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3862 /* Windows accepts overflowing this array... we do not. */
3863 if (stage >= ARRAY_SIZE(state->textures))
3865 WARN("Ignoring invalid stage %u.\n", stage);
3866 return;
3869 prev = state->textures[stage];
3870 TRACE("Previous texture %p.\n", prev);
3872 if (texture == prev)
3874 TRACE("App is setting the same texture again, nothing to do.\n");
3875 return;
3878 TRACE("Setting new texture to %p.\n", texture);
3879 state->textures[stage] = texture;
3881 if (texture)
3882 wined3d_texture_incref(texture);
3883 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
3884 if (prev)
3885 wined3d_texture_decref(prev);
3887 return;
3890 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3891 struct wined3d_stateblock *stateblock)
3893 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3894 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3895 const struct wined3d_saved_states *changed = &stateblock->changed;
3896 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3897 struct wined3d_device_context *context = &device->cs->c;
3898 unsigned int i, j, start, idx;
3899 bool set_depth_bounds = false;
3900 struct wined3d_range range;
3901 uint32_t map;
3903 TRACE("device %p, stateblock %p.\n", device, stateblock);
3905 if (changed->vertexShader)
3906 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
3907 if (changed->pixelShader)
3908 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
3910 for (start = 0; ; start = range.offset + range.size)
3912 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3913 break;
3915 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3918 map = changed->vertexShaderConstantsI;
3919 for (start = 0; ; start = range.offset + range.size)
3921 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3922 break;
3924 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3927 map = changed->vertexShaderConstantsB;
3928 for (start = 0; ; start = range.offset + range.size)
3930 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3931 break;
3933 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3936 for (start = 0; ; start = range.offset + range.size)
3938 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3939 break;
3941 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3944 map = changed->pixelShaderConstantsI;
3945 for (start = 0; ; start = range.offset + range.size)
3947 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3948 break;
3950 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3953 map = changed->pixelShaderConstantsB;
3954 for (start = 0; ; start = range.offset + range.size)
3956 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3957 break;
3959 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3962 if (changed->lights)
3964 struct wined3d_light_info *light, *cursor;
3966 LIST_FOR_EACH_ENTRY_SAFE(light, cursor, &changed->changed_lights, struct wined3d_light_info, changed_entry)
3968 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
3969 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3970 list_remove(&light->changed_entry);
3971 light->changed = false;
3975 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3977 map = changed->renderState[i];
3978 while (map)
3980 j = wined3d_bit_scan(&map);
3981 idx = i * word_bit_count + j;
3983 switch (idx)
3985 case WINED3D_RS_BLENDFACTOR:
3986 case WINED3D_RS_MULTISAMPLEMASK:
3987 case WINED3D_RS_ALPHABLENDENABLE:
3988 case WINED3D_RS_SRCBLEND:
3989 case WINED3D_RS_DESTBLEND:
3990 case WINED3D_RS_BLENDOP:
3991 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
3992 case WINED3D_RS_SRCBLENDALPHA:
3993 case WINED3D_RS_DESTBLENDALPHA:
3994 case WINED3D_RS_BLENDOPALPHA:
3995 case WINED3D_RS_COLORWRITEENABLE:
3996 case WINED3D_RS_COLORWRITEENABLE1:
3997 case WINED3D_RS_COLORWRITEENABLE2:
3998 case WINED3D_RS_COLORWRITEENABLE3:
3999 set_blend_state = TRUE;
4000 break;
4002 case WINED3D_RS_BACK_STENCILFAIL:
4003 case WINED3D_RS_BACK_STENCILFUNC:
4004 case WINED3D_RS_BACK_STENCILPASS:
4005 case WINED3D_RS_BACK_STENCILZFAIL:
4006 case WINED3D_RS_STENCILENABLE:
4007 case WINED3D_RS_STENCILFAIL:
4008 case WINED3D_RS_STENCILFUNC:
4009 case WINED3D_RS_STENCILREF:
4010 case WINED3D_RS_STENCILMASK:
4011 case WINED3D_RS_STENCILPASS:
4012 case WINED3D_RS_STENCILWRITEMASK:
4013 case WINED3D_RS_STENCILZFAIL:
4014 case WINED3D_RS_TWOSIDEDSTENCILMODE:
4015 case WINED3D_RS_ZENABLE:
4016 case WINED3D_RS_ZFUNC:
4017 case WINED3D_RS_ZWRITEENABLE:
4018 set_depth_stencil_state = TRUE;
4019 break;
4021 case WINED3D_RS_FILLMODE:
4022 case WINED3D_RS_CULLMODE:
4023 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
4024 case WINED3D_RS_DEPTHBIAS:
4025 case WINED3D_RS_SCISSORTESTENABLE:
4026 case WINED3D_RS_ANTIALIASEDLINEENABLE:
4027 set_rasterizer_state = TRUE;
4028 break;
4030 case WINED3D_RS_ADAPTIVETESS_X:
4031 case WINED3D_RS_ADAPTIVETESS_Z:
4032 case WINED3D_RS_ADAPTIVETESS_W:
4033 set_depth_bounds = true;
4034 break;
4036 case WINED3D_RS_ADAPTIVETESS_Y:
4037 break;
4039 case WINED3D_RS_ANTIALIAS:
4040 if (state->rs[WINED3D_RS_ANTIALIAS])
4041 FIXME("Antialias not supported yet.\n");
4042 break;
4044 case WINED3D_RS_TEXTUREPERSPECTIVE:
4045 break;
4047 case WINED3D_RS_WRAPU:
4048 if (state->rs[WINED3D_RS_WRAPU])
4049 FIXME("Render state WINED3D_RS_WRAPU not implemented yet.\n");
4050 break;
4052 case WINED3D_RS_WRAPV:
4053 if (state->rs[WINED3D_RS_WRAPV])
4054 FIXME("Render state WINED3D_RS_WRAPV not implemented yet.\n");
4055 break;
4057 case WINED3D_RS_MONOENABLE:
4058 if (state->rs[WINED3D_RS_MONOENABLE])
4059 FIXME("Render state WINED3D_RS_MONOENABLE not implemented yet.\n");
4060 break;
4062 case WINED3D_RS_ROP2:
4063 if (state->rs[WINED3D_RS_ROP2])
4064 FIXME("Render state WINED3D_RS_ROP2 not implemented yet.\n");
4065 break;
4067 case WINED3D_RS_PLANEMASK:
4068 if (state->rs[WINED3D_RS_PLANEMASK])
4069 FIXME("Render state WINED3D_RS_PLANEMASK not implemented yet.\n");
4070 break;
4072 case WINED3D_RS_LASTPIXEL:
4073 if (!state->rs[WINED3D_RS_LASTPIXEL])
4075 static bool warned;
4076 if (!warned)
4078 FIXME("Last Pixel Drawing Disabled, not handled yet.\n");
4079 warned = true;
4082 break;
4084 case WINED3D_RS_ZVISIBLE:
4085 if (state->rs[WINED3D_RS_ZVISIBLE])
4086 FIXME("WINED3D_RS_ZVISIBLE not implemented.\n");
4087 break;
4089 case WINED3D_RS_SUBPIXEL:
4090 if (state->rs[WINED3D_RS_SUBPIXEL])
4091 FIXME("Render state WINED3D_RS_SUBPIXEL not implemented yet.\n");
4092 break;
4094 case WINED3D_RS_SUBPIXELX:
4095 if (state->rs[WINED3D_RS_SUBPIXELX])
4096 FIXME("Render state WINED3D_RS_SUBPIXELX not implemented yet.\n");
4097 break;
4099 case WINED3D_RS_STIPPLEDALPHA:
4100 if (state->rs[WINED3D_RS_STIPPLEDALPHA])
4101 FIXME("Stippled Alpha not supported yet.\n");
4102 break;
4104 case WINED3D_RS_STIPPLEENABLE:
4105 if (state->rs[WINED3D_RS_STIPPLEENABLE])
4106 FIXME("Render state WINED3D_RS_STIPPLEENABLE not implemented yet.\n");
4107 break;
4109 case WINED3D_RS_MIPMAPLODBIAS:
4110 if (state->rs[WINED3D_RS_MIPMAPLODBIAS])
4111 FIXME("Render state WINED3D_RS_MIPMAPLODBIAS not implemented yet.\n");
4112 break;
4114 case WINED3D_RS_ANISOTROPY:
4115 if (state->rs[WINED3D_RS_ANISOTROPY])
4116 FIXME("Render state WINED3D_RS_ANISOTROPY not implemented yet.\n");
4117 break;
4119 case WINED3D_RS_FLUSHBATCH:
4120 if (state->rs[WINED3D_RS_FLUSHBATCH])
4121 FIXME("Render state WINED3D_RS_FLUSHBATCH not implemented yet.\n");
4122 break;
4124 case WINED3D_RS_TRANSLUCENTSORTINDEPENDENT:
4125 if (state->rs[WINED3D_RS_TRANSLUCENTSORTINDEPENDENT])
4126 FIXME("Render state WINED3D_RS_TRANSLUCENTSORTINDEPENDENT not implemented yet.\n");
4127 break;
4129 case WINED3D_RS_WRAP0:
4130 case WINED3D_RS_WRAP1:
4131 case WINED3D_RS_WRAP2:
4132 case WINED3D_RS_WRAP3:
4133 case WINED3D_RS_WRAP4:
4134 case WINED3D_RS_WRAP5:
4135 case WINED3D_RS_WRAP6:
4136 case WINED3D_RS_WRAP7:
4137 case WINED3D_RS_WRAP8:
4138 case WINED3D_RS_WRAP9:
4139 case WINED3D_RS_WRAP10:
4140 case WINED3D_RS_WRAP11:
4141 case WINED3D_RS_WRAP12:
4142 case WINED3D_RS_WRAP13:
4143 case WINED3D_RS_WRAP14:
4144 case WINED3D_RS_WRAP15:
4146 static unsigned int once;
4148 if ((state->rs[idx]) && !once++)
4149 FIXME("(WINED3D_RS_WRAP0) Texture wrapping not yet supported.\n");
4150 break;
4153 case WINED3D_RS_EXTENTS:
4154 if (state->rs[WINED3D_RS_EXTENTS])
4155 FIXME("Render state WINED3D_RS_EXTENTS not implemented yet.\n");
4156 break;
4158 case WINED3D_RS_COLORKEYBLENDENABLE:
4159 if (state->rs[WINED3D_RS_COLORKEYBLENDENABLE])
4160 FIXME("Render state WINED3D_RS_COLORKEYBLENDENABLE not implemented yet.\n");
4161 break;
4163 case WINED3D_RS_SOFTWAREVERTEXPROCESSING:
4165 static unsigned int once;
4167 if ((state->rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING]) && !once++)
4168 FIXME("Software vertex processing not implemented.\n");
4169 break;
4172 case WINED3D_RS_PATCHEDGESTYLE:
4173 if (state->rs[WINED3D_RS_PATCHEDGESTYLE] != WINED3D_PATCH_EDGE_DISCRETE)
4174 FIXME("WINED3D_RS_PATCHEDGESTYLE %#x not yet implemented.\n",
4175 state->rs[WINED3D_RS_PATCHEDGESTYLE]);
4176 break;
4178 case WINED3D_RS_PATCHSEGMENTS:
4180 union
4182 DWORD d;
4183 float f;
4184 } tmpvalue;
4185 tmpvalue.f = 1.0f;
4187 if (state->rs[WINED3D_RS_PATCHSEGMENTS] != tmpvalue.d)
4189 static bool displayed = false;
4191 tmpvalue.d = state->rs[WINED3D_RS_PATCHSEGMENTS];
4192 if(!displayed)
4193 FIXME("(WINED3D_RS_PATCHSEGMENTS,%f) not yet implemented.\n", tmpvalue.f);
4195 displayed = true;
4197 break;
4200 case WINED3D_RS_DEBUGMONITORTOKEN:
4201 WARN("token: %#x.\n", state->rs[WINED3D_RS_DEBUGMONITORTOKEN]);
4202 break;
4204 case WINED3D_RS_INDEXEDVERTEXBLENDENABLE:
4205 break;
4207 case WINED3D_RS_TWEENFACTOR:
4208 break;
4210 case WINED3D_RS_POSITIONDEGREE:
4211 if (state->rs[WINED3D_RS_POSITIONDEGREE] != WINED3D_DEGREE_CUBIC)
4212 FIXME("WINED3D_RS_POSITIONDEGREE %#x not yet implemented.\n",
4213 state->rs[WINED3D_RS_POSITIONDEGREE]);
4214 break;
4216 case WINED3D_RS_NORMALDEGREE:
4217 if (state->rs[WINED3D_RS_NORMALDEGREE] != WINED3D_DEGREE_LINEAR)
4218 FIXME("WINED3D_RS_NORMALDEGREE %#x not yet implemented.\n",
4219 state->rs[WINED3D_RS_NORMALDEGREE]);
4220 break;
4222 case WINED3D_RS_MINTESSELLATIONLEVEL:
4223 break;
4225 case WINED3D_RS_MAXTESSELLATIONLEVEL:
4226 break;
4228 case WINED3D_RS_ENABLEADAPTIVETESSELLATION:
4229 if (state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION])
4230 FIXME("WINED3D_RS_ENABLEADAPTIVETESSELLATION %#x not yet implemented.\n",
4231 state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION]);
4232 break;
4234 default:
4235 wined3d_device_set_render_state(device, idx, state->rs[idx]);
4236 break;
4241 if (set_rasterizer_state)
4243 struct wined3d_rasterizer_state *rasterizer_state;
4244 struct wined3d_rasterizer_state_desc desc;
4245 struct wine_rb_entry *entry;
4246 union
4248 DWORD d;
4249 float f;
4250 } bias;
4252 memset(&desc, 0, sizeof(desc));
4253 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
4254 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
4255 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
4256 desc.depth_bias = bias.f;
4257 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
4258 desc.scale_bias = bias.f;
4259 desc.depth_clip = TRUE;
4260 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
4261 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
4263 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
4265 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
4266 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
4268 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
4269 &wined3d_null_parent_ops, &rasterizer_state)))
4271 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
4272 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
4274 ERR("Failed to insert rasterizer state.\n");
4275 wined3d_rasterizer_state_decref(rasterizer_state);
4280 if (set_blend_state || changed->alpha_to_coverage
4281 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
4283 struct wined3d_blend_state *blend_state;
4284 struct wined3d_blend_state_desc desc;
4285 struct wine_rb_entry *entry;
4286 struct wined3d_color colour;
4287 unsigned int sample_mask;
4289 memset(&desc, 0, sizeof(desc));
4290 desc.alpha_to_coverage = state->alpha_to_coverage;
4291 desc.independent = FALSE;
4292 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
4293 desc.alpha_to_coverage = TRUE;
4294 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
4295 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
4296 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
4297 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
4298 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
4300 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
4301 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
4302 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
4304 else
4306 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
4307 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
4308 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
4310 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
4311 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
4312 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
4313 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
4314 if (desc.rt[1].writemask != desc.rt[0].writemask
4315 || desc.rt[2].writemask != desc.rt[0].writemask
4316 || desc.rt[3].writemask != desc.rt[0].writemask)
4318 desc.independent = TRUE;
4319 for (i = 1; i < 4; ++i)
4321 desc.rt[i].enable = desc.rt[0].enable;
4322 desc.rt[i].src = desc.rt[0].src;
4323 desc.rt[i].dst = desc.rt[0].dst;
4324 desc.rt[i].op = desc.rt[0].op;
4325 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
4326 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
4327 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
4331 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
4332 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
4333 else
4334 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
4336 if ((entry = wine_rb_get(&device->blend_states, &desc)))
4338 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
4339 wined3d_device_context_set_blend_state(context, blend_state, &colour,
4340 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4342 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
4343 &wined3d_null_parent_ops, &blend_state)))
4345 wined3d_device_context_set_blend_state(context, blend_state, &colour,
4346 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4347 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
4349 ERR("Failed to insert blend state.\n");
4350 wined3d_blend_state_decref(blend_state);
4355 if (set_depth_stencil_state)
4357 struct wined3d_depth_stencil_state *depth_stencil_state;
4358 struct wined3d_depth_stencil_state_desc desc;
4359 struct wine_rb_entry *entry;
4360 unsigned int stencil_ref;
4362 memset(&desc, 0, sizeof(desc));
4363 switch (state->rs[WINED3D_RS_ZENABLE])
4365 case WINED3D_ZB_FALSE:
4366 desc.depth = FALSE;
4367 break;
4369 case WINED3D_ZB_USEW:
4370 FIXME("W buffer is not well handled.\n");
4371 case WINED3D_ZB_TRUE:
4372 desc.depth = TRUE;
4373 break;
4375 default:
4376 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
4378 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
4379 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
4380 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
4381 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
4382 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
4383 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
4384 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
4385 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
4386 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
4388 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
4390 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
4391 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
4392 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
4393 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
4395 else
4397 desc.back = desc.front;
4400 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
4401 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
4402 else
4403 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
4405 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
4407 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
4408 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
4410 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
4411 &wined3d_null_parent_ops, &depth_stencil_state)))
4413 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
4414 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
4416 ERR("Failed to insert depth/stencil state.\n");
4417 wined3d_depth_stencil_state_decref(depth_stencil_state);
4422 if (set_depth_bounds)
4424 wined3d_device_context_set_depth_bounds(context,
4425 state->rs[WINED3D_RS_ADAPTIVETESS_X] == WINED3DFMT_NVDB,
4426 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_Z]),
4427 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_W]));
4430 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
4432 map = changed->textureState[i];
4433 while (map)
4435 j = wined3d_bit_scan(&map);
4436 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
4440 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
4442 map = changed->samplerState[i];
4443 while (map)
4445 j = wined3d_bit_scan(&map);
4446 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
4450 if (changed->transforms)
4452 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
4454 map = changed->transform[i];
4455 while (map)
4457 j = wined3d_bit_scan(&map);
4458 idx = i * word_bit_count + j;
4459 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
4464 if (changed->indices)
4465 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
4466 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
4467 if (changed->vertexDecl)
4468 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
4469 if (changed->material)
4470 wined3d_device_set_material(device, &state->material);
4471 if (changed->viewport)
4472 wined3d_device_context_set_viewports(context, 1, &state->viewport);
4473 if (changed->scissorRect)
4474 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
4476 map = changed->streamSource | changed->streamFreq;
4477 while (map)
4479 i = wined3d_bit_scan(&map);
4480 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
4483 map = changed->textures;
4484 while (map)
4486 i = wined3d_bit_scan(&map);
4487 wined3d_device_set_texture(device, i, state->textures[i]);
4490 map = changed->clipplane;
4491 while (map)
4493 i = wined3d_bit_scan(&map);
4494 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4497 assert(list_empty(&stateblock->changed.changed_lights));
4498 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4499 list_init(&stateblock->changed.changed_lights);
4501 TRACE("Applied stateblock %p.\n", stateblock);
4504 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4506 TRACE("device %p, caps %p.\n", device, caps);
4508 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4511 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4512 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4514 struct wined3d_swapchain *swapchain;
4516 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4517 device, swapchain_idx, mode, rotation);
4519 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4520 return WINED3DERR_INVALIDCALL;
4522 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4525 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4527 /* At the moment we have no need for any functionality at the beginning
4528 * of a scene. */
4529 TRACE("device %p.\n", device);
4531 if (device->inScene)
4533 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4534 return WINED3DERR_INVALIDCALL;
4536 device->inScene = TRUE;
4537 return WINED3D_OK;
4540 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4542 TRACE("device %p.\n", device);
4544 if (!device->inScene)
4546 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4547 return WINED3DERR_INVALIDCALL;
4550 device->inScene = FALSE;
4551 return WINED3D_OK;
4554 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, unsigned int rect_count,
4555 const RECT *rects, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
4557 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4559 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4560 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4562 if (!rect_count && rects)
4564 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4565 return WINED3D_OK;
4568 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4570 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4571 if (!ds)
4573 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4574 /* TODO: What about depth stencil buffers without stencil bits? */
4575 return WINED3DERR_INVALIDCALL;
4577 else if (flags & WINED3DCLEAR_TARGET)
4579 if (ds->width < fb->render_targets[0]->width
4580 || ds->height < fb->render_targets[0]->height)
4582 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4583 return WINED3D_OK;
4588 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4590 return WINED3D_OK;
4593 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4595 struct wined3d_state *state = context->state;
4597 TRACE("context %p, value %p.\n", context, value);
4599 if (value)
4600 *value = state->predicate_value;
4601 return state->predicate;
4604 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4605 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4607 struct wined3d_state *state = context->state;
4609 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4610 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4612 wined3d_device_context_lock(context);
4613 state->primitive_type = primitive_type;
4614 state->patch_vertex_count = patch_vertex_count;
4615 wined3d_device_context_unlock(context);
4618 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4619 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4621 const struct wined3d_state *state = context->state;
4623 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4624 context, primitive_type, patch_vertex_count);
4626 *primitive_type = state->primitive_type;
4627 if (patch_vertex_count)
4628 *patch_vertex_count = state->patch_vertex_count;
4630 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4633 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4634 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4636 unsigned int src_size, dst_size, src_skip_levels = 0;
4637 unsigned int src_level_count, dst_level_count;
4638 const struct wined3d_dirty_regions *regions;
4639 unsigned int layer_count, level_count, i, j;
4640 enum wined3d_resource_type type;
4641 BOOL entire_texture = TRUE;
4642 struct wined3d_box box;
4644 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4646 /* Verify that the source and destination textures are non-NULL. */
4647 if (!src_texture || !dst_texture)
4649 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4650 return WINED3DERR_INVALIDCALL;
4653 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4654 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4656 WARN("Source resource is GPU accessible or a scratch resource.\n");
4657 return WINED3DERR_INVALIDCALL;
4659 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4661 WARN("Destination resource is CPU accessible.\n");
4662 return WINED3DERR_INVALIDCALL;
4665 /* Verify that the source and destination textures are the same type. */
4666 type = src_texture->resource.type;
4667 if (dst_texture->resource.type != type)
4669 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4670 return WINED3DERR_INVALIDCALL;
4673 layer_count = src_texture->layer_count;
4674 if (layer_count != dst_texture->layer_count)
4676 WARN("Source and destination have different layer counts.\n");
4677 return WINED3DERR_INVALIDCALL;
4680 if (src_texture->resource.format != dst_texture->resource.format)
4682 WARN("Source and destination formats do not match.\n");
4683 return WINED3DERR_INVALIDCALL;
4686 src_level_count = src_texture->level_count;
4687 dst_level_count = dst_texture->level_count;
4688 level_count = min(src_level_count, dst_level_count);
4690 src_size = max(src_texture->resource.width, src_texture->resource.height);
4691 src_size = max(src_size, src_texture->resource.depth);
4692 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4693 dst_size = max(dst_size, dst_texture->resource.depth);
4694 while (src_size > dst_size)
4696 src_size >>= 1;
4697 ++src_skip_levels;
4700 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4701 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4702 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4704 WARN("Source and destination dimensions do not match.\n");
4705 return WINED3DERR_INVALIDCALL;
4708 if ((regions = src_texture->dirty_regions))
4710 for (i = 0; i < layer_count && entire_texture; ++i)
4712 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4713 continue;
4715 entire_texture = FALSE;
4716 break;
4720 /* Update every surface level of the texture. */
4721 if (entire_texture)
4723 for (i = 0; i < level_count; ++i)
4725 wined3d_texture_get_level_box(dst_texture, i, &box);
4726 for (j = 0; j < layer_count; ++j)
4728 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4729 &dst_texture->resource, j * dst_level_count + i, &box,
4730 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4731 0, NULL, WINED3D_TEXF_POINT);
4735 else
4737 unsigned int src_level, box_count, k;
4738 const struct wined3d_box *boxes;
4739 struct wined3d_box b;
4741 for (i = 0; i < layer_count; ++i)
4743 boxes = regions[i].boxes;
4744 box_count = regions[i].box_count;
4745 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4747 boxes = &b;
4748 box_count = 1;
4749 wined3d_texture_get_level_box(dst_texture, i, &b);
4752 for (j = 0; j < level_count; ++j)
4754 src_level = j + src_skip_levels;
4756 /* TODO: We could pass an array of boxes here to avoid
4757 * multiple context acquisitions for the same resource. */
4758 for (k = 0; k < box_count; ++k)
4760 box = boxes[k];
4761 if (src_level)
4763 box.left >>= src_level;
4764 box.top >>= src_level;
4765 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4766 wined3d_texture_get_level_width(src_texture, src_level));
4767 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4768 wined3d_texture_get_level_height(src_texture, src_level));
4769 box.front >>= src_level;
4770 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4771 wined3d_texture_get_level_depth(src_texture, src_level));
4774 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4775 &dst_texture->resource, i * dst_level_count + j, &box,
4776 &src_texture->resource, i * src_level_count + src_level, &box,
4777 0, NULL, WINED3D_TEXF_POINT);
4783 wined3d_texture_clear_dirty_regions(src_texture);
4785 return WINED3D_OK;
4788 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4790 const struct wined3d_state *state = device->cs->c.state;
4791 struct wined3d_texture *texture;
4792 unsigned i;
4794 TRACE("device %p, num_passes %p.\n", device, num_passes);
4796 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4798 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4800 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4801 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4803 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4805 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4806 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4809 texture = state->textures[i];
4810 if (!texture || texture->resource.format_caps & WINED3D_FORMAT_CAP_FILTERING)
4811 continue;
4813 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4815 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4816 return E_FAIL;
4818 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4820 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4821 return E_FAIL;
4823 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4824 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4826 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4827 return E_FAIL;
4831 if (wined3d_state_uses_depth_buffer(state)
4832 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4834 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4835 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4837 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4839 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4840 return WINED3DERR_CONFLICTINGRENDERSTATE;
4844 /* return a sensible default */
4845 *num_passes = 1;
4847 TRACE("returning D3D_OK\n");
4848 return WINED3D_OK;
4851 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4853 static BOOL warned;
4855 TRACE("device %p, software %#x.\n", device, software);
4857 if (!warned)
4859 FIXME("device %p, software %#x stub!\n", device, software);
4860 warned = TRUE;
4863 device->softwareVertexProcessing = software;
4866 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4868 static BOOL warned;
4870 TRACE("device %p.\n", device);
4872 if (!warned)
4874 TRACE("device %p stub!\n", device);
4875 warned = TRUE;
4878 return device->softwareVertexProcessing;
4881 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4882 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4884 struct wined3d_swapchain *swapchain;
4886 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4887 device, swapchain_idx, raster_status);
4889 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4890 return WINED3DERR_INVALIDCALL;
4892 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4895 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4897 static BOOL warned;
4899 TRACE("device %p, segments %.8e.\n", device, segments);
4901 if (segments != 0.0f)
4903 if (!warned)
4905 FIXME("device %p, segments %.8e stub!\n", device, segments);
4906 warned = TRUE;
4910 return WINED3D_OK;
4913 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4915 static BOOL warned;
4917 TRACE("device %p.\n", device);
4919 if (!warned)
4921 FIXME("device %p stub!\n", device);
4922 warned = TRUE;
4925 return 0.0f;
4928 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4929 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4931 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4932 context, dst_buffer, offset, uav);
4934 wined3d_device_context_lock(context);
4935 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4936 wined3d_device_context_unlock(context);
4939 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4940 const struct wined3d_resource *dst_resource)
4942 if (src_resource->format->id == dst_resource->format->id)
4943 return true;
4944 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4945 return true;
4946 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4947 return false;
4948 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
4949 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK))
4950 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4951 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK)
4952 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS))
4953 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4954 return false;
4957 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4958 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4960 unsigned int src_row_block_count, dst_row_block_count;
4961 struct wined3d_texture *dst_texture, *src_texture;
4962 unsigned int src_row_count, dst_row_count;
4963 struct wined3d_box src_box, dst_box;
4964 unsigned int i, j;
4966 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4968 if (src_resource == dst_resource)
4970 WARN("Source and destination are the same resource.\n");
4971 return;
4974 if (src_resource->type != dst_resource->type)
4976 WARN("Resource types (%s / %s) don't match.\n",
4977 debug_d3dresourcetype(dst_resource->type),
4978 debug_d3dresourcetype(src_resource->type));
4979 return;
4982 if (!resources_format_compatible(src_resource, dst_resource))
4984 WARN("Resource formats %s and %s are incompatible.\n",
4985 debug_d3dformat(dst_resource->format->id),
4986 debug_d3dformat(src_resource->format->id));
4987 return;
4990 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4991 / src_resource->format->block_width;
4992 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4993 / dst_resource->format->block_width;
4994 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4995 / src_resource->format->block_height;
4996 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4997 / dst_resource->format->block_height;
4999 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
5000 || src_resource->depth != dst_resource->depth)
5002 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
5003 dst_row_block_count, dst_row_count, dst_resource->depth,
5004 src_row_block_count, src_row_count, src_resource->depth);
5005 return;
5008 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
5010 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
5011 wined3d_device_context_lock(context);
5012 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
5013 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5014 wined3d_device_context_unlock(context);
5015 return;
5018 dst_texture = texture_from_resource(dst_resource);
5019 src_texture = texture_from_resource(src_resource);
5021 if (src_texture->layer_count != dst_texture->layer_count
5022 || src_texture->level_count != dst_texture->level_count)
5024 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
5025 dst_texture->layer_count, dst_texture->level_count,
5026 src_texture->layer_count, src_texture->level_count);
5027 return;
5030 wined3d_device_context_lock(context);
5031 for (i = 0; i < dst_texture->level_count; ++i)
5033 wined3d_texture_get_level_box(src_texture, i, &src_box);
5034 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
5035 for (j = 0; j < dst_texture->layer_count; ++j)
5037 unsigned int idx = j * dst_texture->level_count + i;
5039 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
5040 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5043 wined3d_device_context_unlock(context);
5046 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
5047 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
5048 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
5049 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
5051 struct wined3d_box dst_box, b;
5053 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
5054 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
5055 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
5056 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
5058 if (flags)
5059 FIXME("Ignoring flags %#x.\n", flags);
5061 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
5063 WARN("Source and destination are the same sub-resource.\n");
5064 return WINED3DERR_INVALIDCALL;
5067 if (!resources_format_compatible(src_resource, dst_resource))
5069 WARN("Resource formats %s and %s are incompatible.\n",
5070 debug_d3dformat(dst_resource->format->id),
5071 debug_d3dformat(src_resource->format->id));
5072 return WINED3DERR_INVALIDCALL;
5075 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
5077 if (src_resource->type != WINED3D_RTYPE_BUFFER)
5079 WARN("Resource types (%s / %s) don't match.\n",
5080 debug_d3dresourcetype(dst_resource->type),
5081 debug_d3dresourcetype(src_resource->type));
5082 return WINED3DERR_INVALIDCALL;
5085 if (dst_sub_resource_idx)
5087 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
5088 return WINED3DERR_INVALIDCALL;
5091 if (src_sub_resource_idx)
5093 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
5094 return WINED3DERR_INVALIDCALL;
5097 if (!src_box)
5099 unsigned int dst_w;
5101 dst_w = dst_resource->size - dst_x;
5102 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
5103 src_box = &b;
5105 else if ((src_box->left >= src_box->right
5106 || src_box->top >= src_box->bottom
5107 || src_box->front >= src_box->back))
5109 WARN("Invalid box %s specified.\n", debug_box(src_box));
5110 return WINED3DERR_INVALIDCALL;
5113 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
5114 || src_box->right - src_box->left > dst_resource->size - dst_x)
5116 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
5117 dst_x, src_box->left, src_box->right - src_box->left);
5118 return WINED3DERR_INVALIDCALL;
5121 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
5123 else
5125 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
5126 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
5127 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
5128 unsigned int src_row_block_count, src_row_count;
5130 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
5131 return WINED3DERR_INVALIDCALL;
5133 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx))
5134 return WINED3DERR_INVALIDCALL;
5136 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
5138 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
5139 return WINED3DERR_INVALIDCALL;
5142 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
5144 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
5145 return WINED3DERR_INVALIDCALL;
5148 if (!src_box)
5150 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
5152 src_w = wined3d_texture_get_level_width(src_texture, src_level);
5153 src_h = wined3d_texture_get_level_height(src_texture, src_level);
5154 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
5156 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5157 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
5158 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
5159 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
5161 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
5162 src_box = &b;
5164 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
5166 WARN("Invalid source box %s.\n", debug_box(src_box));
5167 return WINED3DERR_INVALIDCALL;
5170 if (src_resource->format->block_width == dst_resource->format->block_width
5171 && src_resource->format->block_height == dst_resource->format->block_height)
5173 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
5174 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
5176 else
5178 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
5179 / src_resource->format->block_width;
5180 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
5181 / src_resource->format->block_height;
5182 wined3d_box_set(&dst_box, dst_x, dst_y,
5183 dst_x + (src_row_block_count * dst_resource->format->block_width),
5184 dst_y + (src_row_count * dst_resource->format->block_height),
5185 dst_z, dst_z + (src_box->back - src_box->front));
5187 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
5189 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
5190 return WINED3DERR_INVALIDCALL;
5194 wined3d_device_context_lock(context);
5195 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
5196 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5197 wined3d_device_context_unlock(context);
5199 return WINED3D_OK;
5202 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
5203 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
5204 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
5206 struct wined3d_sub_resource_desc desc;
5207 struct wined3d_box b;
5209 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
5210 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
5212 if (flags)
5213 FIXME("Ignoring flags %#x.\n", flags);
5215 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5217 WARN("Resource %p is not GPU accessible.\n", resource);
5218 return;
5221 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
5222 return;
5224 if (!box)
5226 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
5227 box = &b;
5229 else if (box->left >= box->right || box->right > desc.width
5230 || box->top >= box->bottom || box->bottom > desc.height
5231 || box->front >= box->back || box->back > desc.depth)
5233 WARN("Invalid box %s specified.\n", debug_box(box));
5234 return;
5237 wined3d_device_context_lock(context);
5238 wined3d_device_context_emit_update_sub_resource(context, resource,
5239 sub_resource_idx, box, data, row_pitch, depth_pitch);
5240 wined3d_device_context_unlock(context);
5243 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
5244 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
5245 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
5246 enum wined3d_format_id format_id)
5248 struct wined3d_texture *dst_texture, *src_texture;
5249 unsigned int dst_level, src_level;
5250 struct wined3d_blt_fx fx = {0};
5251 RECT dst_rect, src_rect;
5253 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
5254 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
5255 context, dst_resource, dst_sub_resource_idx,
5256 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
5258 if (wined3d_format_is_typeless(dst_resource->format)
5259 || wined3d_format_is_typeless(src_resource->format))
5261 FIXME("Multisample resolve is not fully supported for typeless formats "
5262 "(dst_format %s, src_format %s, format %s).\n",
5263 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
5264 debug_d3dformat(format_id));
5266 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5268 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
5269 return;
5271 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5273 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
5274 return;
5277 wined3d_device_context_lock(context);
5278 fx.resolve_format_id = format_id;
5280 dst_texture = texture_from_resource(dst_resource);
5281 src_texture = texture_from_resource(src_resource);
5283 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5284 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
5285 wined3d_texture_get_level_height(dst_texture, dst_level));
5286 src_level = src_sub_resource_idx % src_texture->level_count;
5287 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
5288 wined3d_texture_get_level_height(src_texture, src_level));
5289 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
5290 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
5291 wined3d_device_context_unlock(context);
5294 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
5295 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
5296 const struct wined3d_color *color, float depth, unsigned int stencil)
5298 struct wined3d_resource *resource;
5299 RECT r;
5301 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
5302 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
5304 if (!flags)
5305 return WINED3D_OK;
5307 resource = view->resource;
5308 if (resource->type == WINED3D_RTYPE_BUFFER)
5310 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
5311 return WINED3DERR_INVALIDCALL;
5314 if (!rect)
5316 SetRect(&r, 0, 0, view->width, view->height);
5317 rect = &r;
5319 else
5321 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
5322 HRESULT hr;
5324 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
5325 return hr;
5328 wined3d_device_context_lock(context);
5329 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
5330 wined3d_device_context_unlock(context);
5332 return WINED3D_OK;
5335 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
5336 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
5338 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
5340 if (!(view->format->attrs & (WINED3D_FORMAT_ATTR_FLOAT | WINED3D_FORMAT_ATTR_NORMALISED)))
5342 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
5343 return;
5346 wined3d_device_context_lock(context);
5347 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
5348 wined3d_device_context_unlock(context);
5351 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
5352 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
5354 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
5356 wined3d_device_context_lock(context);
5357 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
5358 wined3d_device_context_unlock(context);
5361 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
5363 /* Not all flags make sense together, but Windows never returns an error.
5364 * Catch the cases that could cause issues. */
5365 if (flags & WINED3D_MAP_READ)
5367 if (flags & WINED3D_MAP_DISCARD)
5369 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
5370 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5372 if (flags & WINED3D_MAP_NOOVERWRITE)
5374 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
5375 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5378 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5380 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU) || !(resource->usage & WINED3DUSAGE_DYNAMIC))
5382 WARN("DISCARD or NOOVERWRITE map not on dynamic GPU-accessible buffer, ignoring.\n");
5383 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5385 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5386 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5388 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
5389 flags &= ~WINED3D_MAP_DISCARD;
5393 return flags;
5396 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
5397 struct wined3d_resource *resource, unsigned int sub_resource_idx,
5398 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
5400 struct wined3d_sub_resource_desc desc;
5401 struct wined3d_box b;
5402 HRESULT hr;
5404 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
5405 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
5407 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
5409 WARN("No read/write flags specified.\n");
5410 return E_INVALIDARG;
5413 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
5415 WARN("Resource does not have MAP_R access.\n");
5416 return E_INVALIDARG;
5419 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
5421 WARN("Resource does not have MAP_W access.\n");
5422 return E_INVALIDARG;
5425 flags = sanitise_map_flags(resource, flags);
5427 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
5428 return E_INVALIDARG;
5430 if (!box)
5432 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
5433 box = &b;
5435 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
5437 WARN("Map box is invalid.\n");
5439 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
5440 return WINED3DERR_INVALIDCALL;
5442 if ((resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS) &&
5443 !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
5444 return WINED3DERR_INVALIDCALL;
5447 wined3d_device_context_lock(context);
5448 hr = wined3d_device_context_emit_map(context, resource, sub_resource_idx, map_desc, box, flags);
5449 wined3d_device_context_unlock(context);
5450 return hr;
5453 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
5454 struct wined3d_resource *resource, unsigned int sub_resource_idx)
5456 HRESULT hr;
5457 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
5459 wined3d_device_context_lock(context);
5460 hr = wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
5461 wined3d_device_context_unlock(context);
5462 return hr;
5465 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
5466 struct wined3d_query *query, unsigned int flags)
5468 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
5470 wined3d_device_context_lock(context);
5471 context->ops->issue_query(context, query, flags);
5472 wined3d_device_context_unlock(context);
5475 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
5476 struct wined3d_command_list *list, bool restore_state)
5478 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
5480 wined3d_device_context_lock(context);
5481 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
5482 wined3d_device_context_unlock(context);
5485 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
5486 const struct wined3d_device_context *context, unsigned int view_idx)
5488 unsigned int max_rt_count;
5490 TRACE("context %p, view_idx %u.\n", context, view_idx);
5492 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
5493 if (view_idx >= max_rt_count)
5495 WARN("Only %u render targets are supported.\n", max_rt_count);
5496 return NULL;
5499 return context->state->fb.render_targets[view_idx];
5502 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
5503 const struct wined3d_device_context *context)
5505 TRACE("context %p.\n", context);
5507 return context->state->fb.depth_stencil;
5510 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
5511 struct wined3d_shader_resource_view *view)
5513 struct wined3d_texture *texture;
5515 TRACE("context %p, view %p.\n", context, view);
5517 if (view->resource->type == WINED3D_RTYPE_BUFFER)
5519 WARN("Called on buffer resource %p.\n", view->resource);
5520 return;
5523 texture = texture_from_resource(view->resource);
5524 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
5526 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
5527 return;
5530 wined3d_device_context_lock(context);
5531 wined3d_device_context_emit_generate_mipmaps(context, view);
5532 wined3d_device_context_unlock(context);
5535 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5536 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5538 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5539 struct wined3d_sub_resource_data data;
5540 struct wined3d_resource_desc desc;
5541 struct wined3d_map_desc map_desc;
5542 struct wined3d_texture *texture;
5543 HRESULT hr;
5545 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5547 ERR("Failed to map source texture.\n");
5548 return NULL;
5551 data.data = map_desc.data;
5552 data.row_pitch = map_desc.row_pitch;
5553 data.slice_pitch = map_desc.slice_pitch;
5555 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5556 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5557 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5558 desc.multisample_quality = 0;
5559 desc.usage = WINED3DUSAGE_DYNAMIC;
5560 desc.bind_flags = 0;
5561 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5562 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5563 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5564 desc.depth = 1;
5565 desc.size = 0;
5567 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5568 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5569 if (FAILED(hr))
5571 ERR("Failed to create cursor texture.\n");
5572 return NULL;
5575 return texture;
5578 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5579 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5581 unsigned int texture_level = sub_resource_idx % texture->level_count;
5582 unsigned int cursor_width, cursor_height;
5583 struct wined3d_map_desc map_desc;
5585 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5586 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5588 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx)
5589 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5590 return WINED3DERR_INVALIDCALL;
5592 if (device->cursor_texture)
5594 wined3d_texture_decref(device->cursor_texture);
5595 device->cursor_texture = NULL;
5598 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5600 WARN("Texture %p has invalid format %s.\n",
5601 texture, debug_d3dformat(texture->resource.format->id));
5602 return WINED3DERR_INVALIDCALL;
5605 /* Cursor width and height must all be powers of two */
5606 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5607 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5608 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5610 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5611 return WINED3DERR_INVALIDCALL;
5614 /* Do not store the surface's pointer because the application may
5615 * release it after setting the cursor image. Windows doesn't
5616 * addref the set surface, so we can't do this either without
5617 * creating circular refcount dependencies. */
5618 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5620 ERR("Failed to create cursor texture.\n");
5621 return WINED3DERR_INVALIDCALL;
5624 if (cursor_width == 32 && cursor_height == 32)
5626 UINT mask_size = cursor_width * cursor_height / 8;
5627 ICONINFO cursor_info;
5628 DWORD *mask_bits;
5629 HCURSOR cursor;
5631 /* 32-bit user32 cursors ignore the alpha channel if it's all
5632 * zeroes, and use the mask instead. Fill the mask with all ones
5633 * to ensure we still get a fully transparent cursor. */
5634 if (!(mask_bits = heap_alloc(mask_size)))
5635 return E_OUTOFMEMORY;
5636 memset(mask_bits, 0xff, mask_size);
5638 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5639 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5640 cursor_info.fIcon = FALSE;
5641 cursor_info.xHotspot = x_hotspot;
5642 cursor_info.yHotspot = y_hotspot;
5643 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5644 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5645 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5647 /* Create our cursor and clean up. */
5648 cursor = CreateIconIndirect(&cursor_info);
5649 if (cursor_info.hbmMask)
5650 DeleteObject(cursor_info.hbmMask);
5651 if (cursor_info.hbmColor)
5652 DeleteObject(cursor_info.hbmColor);
5653 if (device->hardwareCursor)
5654 DestroyCursor(device->hardwareCursor);
5655 device->hardwareCursor = cursor;
5656 if (device->bCursorVisible)
5657 SetCursor(cursor);
5659 heap_free(mask_bits);
5662 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5663 device->cursorWidth = cursor_width;
5664 device->cursorHeight = cursor_height;
5665 device->xHotSpot = x_hotspot;
5666 device->yHotSpot = y_hotspot;
5668 return WINED3D_OK;
5671 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5672 int x_screen_space, int y_screen_space, uint32_t flags)
5674 TRACE("device %p, x %d, y %d, flags %#x.\n",
5675 device, x_screen_space, y_screen_space, flags);
5677 device->xScreenSpace = x_screen_space;
5678 device->yScreenSpace = y_screen_space;
5680 if (device->hardwareCursor)
5682 POINT pt;
5684 GetCursorPos( &pt );
5685 if (x_screen_space == pt.x && y_screen_space == pt.y)
5686 return;
5687 SetCursorPos( x_screen_space, y_screen_space );
5689 /* Switch to the software cursor if position diverges from the hardware one. */
5690 GetCursorPos( &pt );
5691 if (x_screen_space != pt.x || y_screen_space != pt.y)
5693 if (device->bCursorVisible) SetCursor( NULL );
5694 DestroyCursor( device->hardwareCursor );
5695 device->hardwareCursor = 0;
5700 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5702 BOOL oldVisible = device->bCursorVisible;
5704 TRACE("device %p, show %#x.\n", device, show);
5707 * When ShowCursor is first called it should make the cursor appear at the OS's last
5708 * known cursor position.
5710 if (show && !oldVisible)
5712 POINT pt;
5713 GetCursorPos(&pt);
5714 device->xScreenSpace = pt.x;
5715 device->yScreenSpace = pt.y;
5718 if (device->hardwareCursor)
5720 device->bCursorVisible = show;
5721 if (show)
5722 SetCursor(device->hardwareCursor);
5723 else
5724 SetCursor(NULL);
5726 else if (device->cursor_texture)
5728 device->bCursorVisible = show;
5731 return oldVisible;
5734 static void mark_managed_resource_dirty(struct wined3d_resource *resource)
5736 if (resource->type != WINED3D_RTYPE_BUFFER)
5738 struct wined3d_texture *texture = texture_from_resource(resource);
5739 unsigned int i;
5741 if (texture->dirty_regions)
5743 for (i = 0; i < texture->layer_count; ++i)
5744 wined3d_texture_add_dirty_region(texture, i, NULL);
5749 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5751 struct wined3d_resource *resource, *cursor;
5753 TRACE("device %p.\n", device);
5755 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5757 TRACE("Checking resource %p for eviction.\n", resource);
5759 if ((resource->usage & WINED3DUSAGE_MANAGED) && !resource->map_count)
5761 if (resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5763 TRACE("Evicting %p.\n", resource);
5764 wined3d_cs_emit_unload_resource(device->cs, resource);
5767 mark_managed_resource_dirty(resource);
5772 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5774 TRACE("context %p.\n", context);
5776 wined3d_device_context_lock(context);
5777 context->ops->flush(context);
5778 wined3d_device_context_unlock(context);
5781 static void update_swapchain_flags(struct wined3d_texture *texture)
5783 unsigned int flags = texture->swapchain->state.desc.flags;
5785 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5786 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5787 else
5788 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5790 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5791 texture->flags |= WINED3D_TEXTURE_GET_DC;
5792 else
5793 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5796 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5797 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5798 wined3d_device_reset_cb callback, BOOL reset_state)
5800 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
5801 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5802 struct wined3d_device_context *context = &device->cs->c;
5803 struct wined3d_swapchain_state *swapchain_state;
5804 struct wined3d_state *state = context->state;
5805 struct wined3d_swapchain_desc *current_desc;
5806 struct wined3d_resource *resource, *cursor;
5807 struct wined3d_rendertarget_view *view;
5808 struct wined3d_swapchain *swapchain;
5809 struct wined3d_view_desc view_desc;
5810 BOOL backbuffer_resized, windowed;
5811 HRESULT hr = WINED3D_OK;
5812 HWND device_window;
5813 unsigned int i;
5815 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5816 device, swapchain_desc, mode, callback, reset_state);
5818 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5820 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5822 ERR("Failed to get the first implicit swapchain.\n");
5823 return WINED3DERR_INVALIDCALL;
5825 swapchain_state = &swapchain->state;
5826 current_desc = &swapchain_state->desc;
5828 if (reset_state)
5830 if (device->logo_texture)
5832 wined3d_texture_decref(device->logo_texture);
5833 device->logo_texture = NULL;
5835 if (device->cursor_texture)
5837 wined3d_texture_decref(device->cursor_texture);
5838 device->cursor_texture = NULL;
5840 state_unbind_resources(state);
5843 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
5844 wined3d_device_context_set_depth_stencil_view(context, NULL);
5846 if (reset_state)
5848 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5850 TRACE("Enumerating resource %p.\n", resource);
5851 if (FAILED(hr = callback(resource)))
5852 return hr;
5856 TRACE("New params:\n");
5857 TRACE("output %p\n", swapchain_desc->output);
5858 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5859 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5860 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5861 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5862 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5863 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5864 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5865 TRACE("device_window %p\n", swapchain_desc->device_window);
5866 TRACE("windowed %#x\n", swapchain_desc->windowed);
5867 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5868 if (swapchain_desc->enable_auto_depth_stencil)
5869 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5870 TRACE("flags %#x\n", swapchain_desc->flags);
5871 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5872 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5874 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5875 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5877 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5878 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5879 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5880 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5882 /* No special treatment of these parameters. Just store them */
5883 current_desc->swap_effect = swapchain_desc->swap_effect;
5884 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5885 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5886 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5887 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5889 device_window = swapchain_desc->device_window ? swapchain_desc->device_window : device->create_parms.focus_window;
5890 if (device_window && device_window != current_desc->device_window)
5892 TRACE("Changing the device window from %p to %p.\n",
5893 current_desc->device_window, device_window);
5894 current_desc->device_window = device_window;
5895 swapchain_state->device_window = device_window;
5896 wined3d_swapchain_set_window(swapchain, NULL);
5899 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5900 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5901 windowed = current_desc->windowed;
5903 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5904 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5906 /* Switch from windowed to fullscreen. */
5907 if (windowed && !swapchain_desc->windowed)
5909 HWND focus_window = device->create_parms.focus_window;
5911 if (!focus_window)
5912 focus_window = swapchain->state.device_window;
5913 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5915 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
5916 return hr;
5920 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5921 swapchain_desc, mode)))
5922 return hr;
5924 /* Switch from fullscreen to windowed. */
5925 if (!windowed && swapchain_desc->windowed)
5926 wined3d_device_release_focus_window(device);
5928 else if (!swapchain_desc->windowed)
5930 DWORD style = swapchain_state->style;
5931 DWORD exstyle = swapchain_state->exstyle;
5932 struct wined3d_output_desc output_desc;
5934 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5935 * the window back into the right position. Some applications
5936 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5937 * up their mess. Guild Wars also loses the device during that. */
5938 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5940 ERR("Failed to get output description, hr %#lx.\n", hr);
5941 return hr;
5944 swapchain_state->style = 0;
5945 swapchain_state->exstyle = 0;
5946 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5947 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5948 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5949 swapchain_state->style = style;
5950 swapchain_state->exstyle = exstyle;
5953 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5954 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5955 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5956 return hr;
5958 if (swapchain_desc->flags != current_desc->flags)
5960 current_desc->flags = swapchain_desc->flags;
5962 update_swapchain_flags(swapchain->front_buffer);
5963 for (i = 0; i < current_desc->backbuffer_count; ++i)
5965 update_swapchain_flags(swapchain->back_buffers[i]);
5969 if ((view = device->auto_depth_stencil_view))
5971 device->auto_depth_stencil_view = NULL;
5972 wined3d_rendertarget_view_decref(view);
5974 if (current_desc->enable_auto_depth_stencil)
5976 struct wined3d_resource_desc texture_desc;
5977 struct wined3d_texture *texture;
5979 TRACE("Creating the depth stencil buffer.\n");
5981 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5982 texture_desc.format = current_desc->auto_depth_stencil_format;
5983 texture_desc.multisample_type = current_desc->multisample_type;
5984 texture_desc.multisample_quality = current_desc->multisample_quality;
5985 texture_desc.usage = 0;
5986 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5987 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5988 texture_desc.width = current_desc->backbuffer_width;
5989 texture_desc.height = current_desc->backbuffer_height;
5990 texture_desc.depth = 1;
5991 texture_desc.size = 0;
5993 if (FAILED(hr = wined3d_texture_create(device, &texture_desc, 1, 1, 0,
5994 NULL, NULL, &wined3d_null_parent_ops, &texture)))
5996 ERR("Failed to create the auto depth/stencil surface, hr %#lx.\n", hr);
5997 return WINED3DERR_INVALIDCALL;
6000 view_desc.format_id = texture->resource.format->id;
6001 view_desc.flags = 0;
6002 view_desc.u.texture.level_idx = 0;
6003 view_desc.u.texture.level_count = 1;
6004 view_desc.u.texture.layer_idx = 0;
6005 view_desc.u.texture.layer_count = 1;
6006 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
6007 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
6008 wined3d_texture_decref(texture);
6009 if (FAILED(hr))
6011 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
6012 return hr;
6016 if ((view = device->back_buffer_view))
6018 device->back_buffer_view = NULL;
6019 wined3d_rendertarget_view_decref(view);
6021 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
6023 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
6025 view_desc.format_id = back_buffer->format->id;
6026 view_desc.flags = 0;
6027 view_desc.u.texture.level_idx = 0;
6028 view_desc.u.texture.level_count = 1;
6029 view_desc.u.texture.layer_idx = 0;
6030 view_desc.u.texture.layer_count = 1;
6031 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
6032 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
6034 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
6035 return hr;
6039 wine_rb_destroy(&device->samplers, device_free_sampler, NULL);
6040 wine_rb_destroy(&device->rasterizer_states, device_free_rasterizer_state, NULL);
6041 wine_rb_destroy(&device->blend_states, device_free_blend_state, NULL);
6042 wine_rb_destroy(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
6044 if (reset_state)
6046 TRACE("Resetting state.\n");
6047 wined3d_device_context_emit_reset_state(&device->cs->c, false);
6048 state_cleanup(state);
6050 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
6052 TRACE("Unloading resource %p.\n", resource);
6053 wined3d_cs_emit_unload_resource(device->cs, resource);
6055 if (resource->usage & WINED3DUSAGE_MANAGED)
6056 mark_managed_resource_dirty(resource);
6059 device->adapter->adapter_ops->adapter_uninit_3d(device);
6061 wined3d_state_reset(state, &device->adapter->d3d_info);
6063 device_init_swapchain_state(device, swapchain);
6064 if (wined3d_settings.logo)
6065 device_load_logo(device, wined3d_settings.logo);
6067 hr = device->adapter->adapter_ops->adapter_init_3d(device);
6069 else
6071 if ((view = device->back_buffer_view))
6072 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
6073 if ((view = device->auto_depth_stencil_view))
6074 wined3d_device_context_set_depth_stencil_view(context, view);
6077 /* All done. There is no need to reload resources or shaders, this will
6078 * happen automatically on the first use. */
6079 return hr;
6082 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
6084 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
6086 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
6088 return WINED3D_OK;
6092 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
6093 struct wined3d_device_creation_parameters *parameters)
6095 TRACE("device %p, parameters %p.\n", device, parameters);
6097 *parameters = device->create_parms;
6100 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
6102 TRACE("device %p.\n", device);
6104 return device->wined3d;
6107 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
6108 UINT swapchain_idx, uint32_t flags, const struct wined3d_gamma_ramp *ramp)
6110 struct wined3d_swapchain *swapchain;
6112 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
6113 device, swapchain_idx, flags, ramp);
6115 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
6116 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
6119 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
6120 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
6122 struct wined3d_swapchain *swapchain;
6124 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
6125 device, swapchain_idx, ramp);
6127 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
6128 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
6131 HDC wined3d_device_gl_get_backup_dc(struct wined3d_device_gl *device_gl)
6133 TRACE("device_gl %p.\n", device_gl);
6135 if (!device_gl->backup_dc)
6137 TRACE("Creating the backup window for device %p.\n", device_gl);
6139 if (!(device_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
6140 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
6142 ERR("Failed to create a window.\n");
6143 return NULL;
6146 if (!(device_gl->backup_dc = GetDC(device_gl->backup_wnd)))
6148 ERR("Failed to get a DC.\n");
6149 DestroyWindow(device_gl->backup_wnd);
6150 device_gl->backup_wnd = NULL;
6151 return NULL;
6155 return device_gl->backup_dc;
6158 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
6160 TRACE("device %p, resource %p.\n", device, resource);
6162 wined3d_not_from_cs(device->cs);
6164 list_add_head(&device->resources, &resource->resource_list_entry);
6167 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
6169 TRACE("device %p, resource %p.\n", device, resource);
6171 wined3d_not_from_cs(device->cs);
6173 list_remove(&resource->resource_list_entry);
6176 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
6178 enum wined3d_resource_type type = resource->type;
6179 struct wined3d_state *state = device->cs->c.state;
6180 struct wined3d_rendertarget_view *rtv;
6181 unsigned int i;
6183 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
6185 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
6187 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
6188 ERR("Resource %p is still in use as render target %u.\n", resource, i);
6191 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
6192 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
6194 switch (type)
6196 case WINED3D_RTYPE_TEXTURE_1D:
6197 case WINED3D_RTYPE_TEXTURE_2D:
6198 case WINED3D_RTYPE_TEXTURE_3D:
6199 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
6201 if (&state->textures[i]->resource == resource)
6203 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
6204 state->textures[i] = NULL;
6207 break;
6209 case WINED3D_RTYPE_BUFFER:
6210 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
6212 if (&state->streams[i].buffer->resource == resource)
6214 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
6215 state->streams[i].buffer = NULL;
6219 if (&state->index_buffer->resource == resource)
6221 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
6222 state->index_buffer = NULL;
6224 break;
6226 default:
6227 break;
6230 /* Remove the resource from the resourceStore */
6231 device_resource_remove(device, resource);
6233 TRACE("Resource released.\n");
6236 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
6238 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
6239 struct wined3d_so_desc_entry, entry)->desc;
6240 const struct wined3d_stream_output_desc *k = key;
6241 unsigned int i;
6242 int ret;
6244 if ((ret = wined3d_uint32_compare(k->element_count, desc->element_count)))
6245 return ret;
6246 if ((ret = wined3d_uint32_compare(k->buffer_stride_count, desc->buffer_stride_count)))
6247 return ret;
6248 if ((ret = wined3d_uint32_compare(k->rasterizer_stream_idx, desc->rasterizer_stream_idx)))
6249 return ret;
6251 for (i = 0; i < k->element_count; ++i)
6253 const struct wined3d_stream_output_element *b = &desc->elements[i];
6254 const struct wined3d_stream_output_element *a = &k->elements[i];
6256 if ((ret = wined3d_uint32_compare(a->stream_idx, b->stream_idx)))
6257 return ret;
6258 if ((ret = (!a->semantic_name - !b->semantic_name)))
6259 return ret;
6260 if (a->semantic_name && (ret = strcmp(a->semantic_name, b->semantic_name)))
6261 return ret;
6262 if ((ret = wined3d_uint32_compare(a->semantic_idx, b->semantic_idx)))
6263 return ret;
6264 if ((ret = wined3d_uint32_compare(a->component_idx, b->component_idx)))
6265 return ret;
6266 if ((ret = wined3d_uint32_compare(a->component_count, b->component_count)))
6267 return ret;
6268 if ((ret = wined3d_uint32_compare(a->output_slot, b->output_slot)))
6269 return ret;
6272 for (i = 0; i < k->buffer_stride_count; ++i)
6274 if ((ret = wined3d_uint32_compare(k->buffer_strides[i], desc->buffer_strides[i])))
6275 return ret;
6278 return 0;
6281 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
6283 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
6285 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
6288 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
6290 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
6292 return memcmp(&state->desc, key, sizeof(state->desc));
6295 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
6297 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
6299 return memcmp(&state->desc, key, sizeof(state->desc));
6302 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
6304 const struct wined3d_depth_stencil_state *state
6305 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
6307 return memcmp(&state->desc, key, sizeof(state->desc));
6310 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
6311 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
6312 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
6313 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
6315 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
6316 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
6317 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
6318 unsigned int i;
6319 HRESULT hr;
6321 device->ref = 1;
6322 device->wined3d = wined3d;
6323 wined3d_incref(device->wined3d);
6324 device->adapter = adapter;
6325 device->device_parent = device_parent;
6326 list_init(&device->resources);
6327 list_init(&device->shaders);
6328 device->surface_alignment = surface_alignment;
6330 /* Save the creation parameters. */
6331 device->create_parms.adapter_idx = adapter_idx;
6332 device->create_parms.device_type = device_type;
6333 device->create_parms.focus_window = focus_window;
6334 device->create_parms.flags = flags;
6336 device->shader_backend = adapter->shader_backend;
6338 vertex_pipeline = adapter->vertex_pipe;
6340 fragment_pipeline = adapter->fragment_pipe;
6342 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
6343 wine_rb_init(&device->samplers, wined3d_sampler_compare);
6344 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
6345 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
6346 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
6348 if (vertex_pipeline->vp_states && fragment_pipeline->states
6349 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
6350 &adapter->d3d_info, supported_extensions, vertex_pipeline,
6351 fragment_pipeline, adapter->misc_state_template)))
6353 ERR("Failed to compile state table, hr %#lx.\n", hr);
6354 wine_rb_destroy(&device->samplers, NULL, NULL);
6355 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6356 wine_rb_destroy(&device->blend_states, NULL, NULL);
6357 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6358 wine_rb_destroy(&device->so_descs, NULL, NULL);
6359 wined3d_decref(device->wined3d);
6360 return hr;
6363 device->max_frame_latency = 3;
6365 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
6367 WARN("Failed to create command stream.\n");
6368 hr = E_FAIL;
6369 goto err;
6372 wined3d_lock_init(&device->bo_map_lock, "wined3d_device.bo_map_lock");
6374 return WINED3D_OK;
6376 err:
6377 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
6379 heap_free(device->multistate_funcs[i]);
6381 wine_rb_destroy(&device->samplers, NULL, NULL);
6382 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6383 wine_rb_destroy(&device->blend_states, NULL, NULL);
6384 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6385 wine_rb_destroy(&device->so_descs, NULL, NULL);
6386 wined3d_decref(device->wined3d);
6387 return hr;
6390 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
6392 unsigned int representative, i, idx, shift;
6393 struct wined3d_context *context;
6395 wined3d_from_cs(device->cs);
6397 if (STATE_IS_COMPUTE(state_id))
6399 for (i = 0; i < device->context_count; ++i)
6400 context_invalidate_compute_state(device->contexts[i], state_id);
6401 return;
6404 representative = device->state_table[state_id].representative;
6405 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
6406 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
6407 for (i = 0; i < device->context_count; ++i)
6409 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
6413 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
6414 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
6416 if (message == WM_DESTROY)
6418 TRACE("unregister window %p.\n", window);
6419 wined3d_unregister_window(window);
6421 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
6422 ERR("Window %p is not the focus window for device %p.\n", window, device);
6424 else if (message == WM_DISPLAYCHANGE)
6426 device->device_parent->ops->mode_changed(device->device_parent);
6428 else if (message == WM_ACTIVATEAPP)
6430 unsigned int i = device->swapchain_count;
6432 /* Deactivating the implicit swapchain may cause the application
6433 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
6434 * deactivate the implicit swapchain last, and to avoid accessing the
6435 * "device" pointer afterwards. */
6436 while (i--)
6437 wined3d_swapchain_activate(device->swapchains[i], wparam);
6439 else if (message == WM_SYSCOMMAND)
6441 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
6443 if (unicode)
6444 DefWindowProcW(window, message, wparam, lparam);
6445 else
6446 DefWindowProcA(window, message, wparam, lparam);
6450 if (unicode)
6451 return CallWindowProcW(proc, window, message, wparam, lparam);
6452 else
6453 return CallWindowProcA(proc, window, message, wparam, lparam);