mshtml: Implement MediaQueryList's addListener method.
[wine.git] / dlls / wined3d / device.c
blob1603329a59f4e86b244c4e4449b729b430ecfdee
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 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 const 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 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1996 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1998 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
1999 wined3d_device_context_emit_set_light_enable(context, light->OriginalIndex, light->glIndex != -1);
2003 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
2005 if (context->device->state_table[STATE_RENDER(i)].representative)
2006 wined3d_device_context_emit_set_render_state(context, i, state->render_states[i]);
2009 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
2010 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
2011 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
2012 wined3d_device_context_unlock(context);
2015 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
2017 TRACE("device %p.\n", device);
2019 return device->cs->c.state;
2022 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
2024 TRACE("device %p.\n", device);
2026 return &device->cs->c;
2029 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
2030 const struct wined3d_device_context *context)
2032 TRACE("context %p.\n", context);
2034 return context->state->vertex_declaration;
2037 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
2038 enum wined3d_shader_type type, struct wined3d_shader *shader)
2040 struct wined3d_state *state = context->state;
2041 struct wined3d_shader *prev;
2043 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
2045 wined3d_device_context_lock(context);
2046 prev = state->shader[type];
2047 if (shader == prev)
2048 goto out;
2050 if (shader)
2051 wined3d_shader_incref(shader);
2052 state->shader[type] = shader;
2053 wined3d_device_context_emit_set_shader(context, type, shader);
2054 if (prev)
2055 wined3d_shader_decref(prev);
2056 out:
2057 wined3d_device_context_unlock(context);
2060 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
2061 enum wined3d_shader_type type)
2063 TRACE("context %p, type %#x.\n", context, type);
2065 return context->state->shader[type];
2068 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
2069 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
2070 const struct wined3d_constant_buffer_state *buffers)
2072 struct wined3d_state *state = context->state;
2073 unsigned int i;
2075 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
2077 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
2079 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
2080 return;
2083 wined3d_device_context_lock(context);
2084 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
2085 goto out;
2087 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
2088 for (i = 0; i < count; ++i)
2090 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
2091 struct wined3d_buffer *buffer = buffers[i].buffer;
2093 if (buffer)
2094 wined3d_buffer_incref(buffer);
2095 state->cb[type][start_idx + i] = buffers[i];
2096 if (prev)
2097 wined3d_buffer_decref(prev);
2099 out:
2100 wined3d_device_context_unlock(context);
2103 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
2104 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
2106 struct wined3d_state *state = context->state;
2107 struct wined3d_blend_state *prev;
2109 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
2110 context, blend_state, blend_factor, sample_mask);
2112 wined3d_device_context_lock(context);
2113 prev = state->blend_state;
2114 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
2115 && sample_mask == state->sample_mask)
2116 goto out;
2118 if (blend_state)
2119 wined3d_blend_state_incref(blend_state);
2120 state->blend_state = blend_state;
2121 state->blend_factor = *blend_factor;
2122 state->sample_mask = sample_mask;
2123 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
2124 if (prev)
2125 wined3d_blend_state_decref(prev);
2126 out:
2127 wined3d_device_context_unlock(context);
2130 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
2131 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
2133 struct wined3d_state *state = context->state;
2134 struct wined3d_depth_stencil_state *prev;
2136 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
2138 wined3d_device_context_lock(context);
2139 prev = state->depth_stencil_state;
2140 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
2141 goto out;
2143 if (depth_stencil_state)
2144 wined3d_depth_stencil_state_incref(depth_stencil_state);
2145 state->depth_stencil_state = depth_stencil_state;
2146 state->stencil_ref = stencil_ref;
2147 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
2148 if (prev)
2149 wined3d_depth_stencil_state_decref(prev);
2150 out:
2151 wined3d_device_context_unlock(context);
2154 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
2155 struct wined3d_rasterizer_state *rasterizer_state)
2157 struct wined3d_state *state = context->state;
2158 struct wined3d_rasterizer_state *prev;
2160 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
2162 wined3d_device_context_lock(context);
2163 prev = state->rasterizer_state;
2164 if (prev == rasterizer_state)
2165 goto out;
2167 if (rasterizer_state)
2168 wined3d_rasterizer_state_incref(rasterizer_state);
2169 state->rasterizer_state = rasterizer_state;
2170 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
2171 if (prev)
2172 wined3d_rasterizer_state_decref(prev);
2173 out:
2174 wined3d_device_context_unlock(context);
2177 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
2178 const struct wined3d_viewport *viewports)
2180 struct wined3d_state *state = context->state;
2181 unsigned int i;
2183 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
2185 for (i = 0; i < viewport_count; ++i)
2187 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
2188 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
2191 wined3d_device_context_lock(context);
2192 if (viewport_count)
2193 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
2194 else
2195 memset(state->viewports, 0, sizeof(state->viewports));
2196 state->viewport_count = viewport_count;
2198 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
2199 wined3d_device_context_unlock(context);
2202 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
2203 const RECT *rects)
2205 struct wined3d_state *state = context->state;
2206 unsigned int i;
2208 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
2210 for (i = 0; i < rect_count; ++i)
2212 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2215 wined3d_device_context_lock(context);
2216 if (state->scissor_rect_count == rect_count
2217 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
2219 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2220 goto out;
2223 if (rect_count)
2224 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
2225 else
2226 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
2227 state->scissor_rect_count = rect_count;
2229 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
2230 out:
2231 wined3d_device_context_unlock(context);
2234 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
2235 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
2236 struct wined3d_shader_resource_view *const *const views)
2238 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
2239 struct wined3d_state *state = context->state;
2240 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
2241 unsigned int i;
2243 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
2245 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
2247 WARN("Invalid view index %u, count %u.\n", start_idx, count);
2248 return;
2251 wined3d_device_context_lock(context);
2252 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
2253 goto out;
2255 memcpy(real_views, views, count * sizeof(*views));
2257 for (i = 0; i < count; ++i)
2259 struct wined3d_shader_resource_view *view = real_views[i];
2261 if (view && (wined3d_is_srv_rtv_bound(state, view)
2262 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2264 WARN("Application is trying to bind resource which is attached as render target.\n");
2265 real_views[i] = NULL;
2269 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
2270 for (i = 0; i < count; ++i)
2272 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
2273 struct wined3d_shader_resource_view *view = real_views[i];
2275 if (view)
2277 wined3d_shader_resource_view_incref(view);
2278 wined3d_srv_bind_count_inc(view);
2281 state->shader_resource_view[type][start_idx + i] = view;
2282 if (prev)
2284 wined3d_srv_bind_count_dec(prev);
2285 wined3d_shader_resource_view_decref(prev);
2288 out:
2289 wined3d_device_context_unlock(context);
2292 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
2293 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
2295 struct wined3d_state *state = context->state;
2296 unsigned int i;
2298 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
2300 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
2302 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
2303 return;
2306 wined3d_device_context_lock(context);
2307 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2308 goto out;
2310 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2311 for (i = 0; i < count; ++i)
2313 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2314 struct wined3d_sampler *sampler = samplers[i];
2316 if (sampler)
2317 wined3d_sampler_incref(sampler);
2318 state->sampler[type][start_idx + i] = sampler;
2319 if (prev)
2320 wined3d_sampler_decref(prev);
2322 out:
2323 wined3d_device_context_unlock(context);
2326 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2327 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2328 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2330 struct wined3d_state *state = context->state;
2331 unsigned int i;
2333 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2334 context, pipeline, start_idx, count, uavs, initial_counts);
2336 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2338 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2339 return;
2342 wined3d_device_context_lock(context);
2343 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2344 goto out;
2346 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2347 for (i = 0; i < count; ++i)
2349 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2350 struct wined3d_unordered_access_view *uav = uavs[i];
2352 if (uav)
2353 wined3d_unordered_access_view_incref(uav);
2354 state->unordered_access_view[pipeline][start_idx + i] = uav;
2355 if (prev)
2356 wined3d_unordered_access_view_decref(prev);
2358 out:
2359 wined3d_device_context_unlock(context);
2362 void CDECL wined3d_device_context_set_render_targets_and_unordered_access_views(struct wined3d_device_context *context,
2363 unsigned int rtv_count, struct wined3d_rendertarget_view *const *render_target_views,
2364 struct wined3d_rendertarget_view *depth_stencil_view, UINT uav_count,
2365 struct wined3d_unordered_access_view *const *unordered_access_views, const unsigned int *initial_counts)
2367 wined3d_device_context_lock(context);
2368 if (rtv_count != ~0u)
2370 if (depth_stencil_view && !(depth_stencil_view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2372 WARN("View resource %p has incompatible %s bind flags.\n",
2373 depth_stencil_view->resource, wined3d_debug_bind_flags(depth_stencil_view->resource->bind_flags));
2374 goto out;
2377 if (FAILED(wined3d_device_context_set_rendertarget_views(context, 0, rtv_count,
2378 render_target_views, FALSE)))
2379 goto out;
2381 wined3d_device_context_set_depth_stencil_view(context, depth_stencil_view);
2384 if (uav_count != ~0u)
2386 wined3d_device_context_set_unordered_access_views(context, WINED3D_PIPELINE_GRAPHICS, 0, uav_count,
2387 unordered_access_views, initial_counts);
2389 out:
2390 wined3d_device_context_unlock(context);
2393 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2394 const struct wined3d_rendertarget_view *view, BOOL dsv)
2396 const struct wined3d_state *state = context->state;
2397 const struct wined3d_resource *resource;
2399 if (!view)
2400 return;
2401 resource = view->resource;
2403 if (resource->srv_bind_count_device)
2405 const struct wined3d_shader_resource_view *srv;
2406 unsigned int i, j;
2408 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2410 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2412 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2413 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2414 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2416 static struct wined3d_shader_resource_view *const null_srv;
2418 WARN("Application sets bound resource as render target.\n");
2419 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2426 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2427 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2429 struct wined3d_state *state = context->state;
2430 unsigned int i, max_rt_count;
2432 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2433 context, start_idx, count, views, set_viewport);
2435 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2436 if (start_idx >= max_rt_count)
2438 WARN("Only %u render targets are supported.\n", max_rt_count);
2439 return WINED3DERR_INVALIDCALL;
2441 count = min(count, max_rt_count - start_idx);
2443 for (i = 0; i < count; ++i)
2445 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2447 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2448 return WINED3DERR_INVALIDCALL;
2452 wined3d_device_context_lock(context);
2453 /* Set the viewport and scissor rectangles, if requested. Tests show that
2454 * stateblock recording is ignored, the change goes directly into the
2455 * primary stateblock. */
2456 if (!start_idx && set_viewport)
2458 state->viewports[0].x = 0;
2459 state->viewports[0].y = 0;
2460 state->viewports[0].width = views[0]->width;
2461 state->viewports[0].height = views[0]->height;
2462 state->viewports[0].min_z = 0.0f;
2463 state->viewports[0].max_z = 1.0f;
2464 state->viewport_count = 1;
2465 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2467 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2468 state->scissor_rect_count = 1;
2469 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2472 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2473 goto out;
2475 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2476 for (i = 0; i < count; ++i)
2478 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2479 struct wined3d_rendertarget_view *view = views[i];
2481 if (view)
2483 wined3d_rendertarget_view_incref(view);
2484 wined3d_rtv_bind_count_inc(view);
2486 state->fb.render_targets[start_idx + i] = view;
2487 /* Release after the assignment, to prevent device_resource_released()
2488 * from seeing the resource as still in use. */
2489 if (prev)
2491 wined3d_rtv_bind_count_dec(prev);
2492 wined3d_rendertarget_view_decref(prev);
2495 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2497 out:
2498 wined3d_device_context_unlock(context);
2499 return WINED3D_OK;
2502 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2503 struct wined3d_rendertarget_view *view)
2505 struct wined3d_fb_state *fb = &context->state->fb;
2506 struct wined3d_rendertarget_view *prev;
2508 TRACE("context %p, view %p.\n", context, view);
2510 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2512 WARN("View resource %p has incompatible %s bind flags.\n",
2513 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2514 return WINED3DERR_INVALIDCALL;
2517 wined3d_device_context_lock(context);
2518 prev = fb->depth_stencil;
2519 if (prev == view)
2521 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2522 goto out;
2525 if ((fb->depth_stencil = view))
2526 wined3d_rendertarget_view_incref(view);
2527 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2528 if (prev)
2529 wined3d_rendertarget_view_decref(prev);
2530 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2531 out:
2532 wined3d_device_context_unlock(context);
2533 return WINED3D_OK;
2536 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2537 struct wined3d_query *predicate, BOOL value)
2539 struct wined3d_state *state = context->state;
2540 struct wined3d_query *prev;
2542 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2544 wined3d_device_context_lock(context);
2545 prev = state->predicate;
2546 if (predicate)
2548 FIXME("Predicated rendering not implemented.\n");
2549 wined3d_query_incref(predicate);
2551 state->predicate = predicate;
2552 state->predicate_value = value;
2553 wined3d_device_context_emit_set_predication(context, predicate, value);
2554 if (prev)
2555 wined3d_query_decref(prev);
2556 wined3d_device_context_unlock(context);
2559 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2560 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2562 struct wined3d_state *state = context->state;
2563 unsigned int i;
2565 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2567 if (start_idx >= WINED3D_MAX_STREAMS)
2569 WARN("Start index %u is out of range.\n", start_idx);
2570 return WINED3DERR_INVALIDCALL;
2573 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2575 for (i = 0; i < count; ++i)
2577 if (streams[i].offset & 0x3)
2579 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2580 return WINED3DERR_INVALIDCALL;
2584 wined3d_device_context_lock(context);
2585 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2586 goto out;
2588 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2589 for (i = 0; i < count; ++i)
2591 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2592 struct wined3d_buffer *buffer = streams[i].buffer;
2594 state->streams[start_idx + i] = streams[i];
2596 if (buffer)
2597 wined3d_buffer_incref(buffer);
2598 if (prev)
2599 wined3d_buffer_decref(prev);
2601 out:
2602 wined3d_device_context_unlock(context);
2603 return WINED3D_OK;
2606 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2607 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2609 struct wined3d_state *state = context->state;
2610 enum wined3d_format_id prev_format;
2611 struct wined3d_buffer *prev_buffer;
2612 unsigned int prev_offset;
2614 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2615 context, buffer, debug_d3dformat(format_id), offset);
2617 wined3d_device_context_lock(context);
2618 prev_buffer = state->index_buffer;
2619 prev_format = state->index_format;
2620 prev_offset = state->index_offset;
2622 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2623 goto out;
2625 if (buffer)
2626 wined3d_buffer_incref(buffer);
2627 state->index_buffer = buffer;
2628 state->index_format = format_id;
2629 state->index_offset = offset;
2630 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2631 if (prev_buffer)
2632 wined3d_buffer_decref(prev_buffer);
2633 out:
2634 wined3d_device_context_unlock(context);
2637 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2638 struct wined3d_vertex_declaration *declaration)
2640 struct wined3d_state *state = context->state;
2641 struct wined3d_vertex_declaration *prev;
2643 TRACE("context %p, declaration %p.\n", context, declaration);
2645 wined3d_device_context_lock(context);
2646 prev = state->vertex_declaration;
2647 if (declaration == prev)
2648 goto out;
2650 if (declaration)
2651 wined3d_vertex_declaration_incref(declaration);
2652 state->vertex_declaration = declaration;
2653 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2654 if (prev)
2655 wined3d_vertex_declaration_decref(prev);
2656 out:
2657 wined3d_device_context_unlock(context);
2660 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2661 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2663 struct wined3d_state *state = context->state;
2664 unsigned int i;
2666 TRACE("context %p, outputs %p.\n", context, outputs);
2668 wined3d_device_context_lock(context);
2669 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2670 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2672 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2673 struct wined3d_buffer *buffer = outputs[i].buffer;
2675 if (buffer)
2676 wined3d_buffer_incref(buffer);
2677 state->stream_output[i] = outputs[i];
2678 if (prev_buffer)
2679 wined3d_buffer_decref(prev_buffer);
2681 wined3d_device_context_unlock(context);
2684 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2685 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2687 struct wined3d_state *state = context->state;
2689 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2690 context, start_vertex, vertex_count, start_instance, instance_count);
2692 wined3d_device_context_lock(context);
2693 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2694 0, start_vertex, vertex_count, start_instance, instance_count, false);
2695 wined3d_device_context_unlock(context);
2698 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2699 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2701 struct wined3d_state *state = context->state;
2703 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2704 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2706 wined3d_device_context_lock(context);
2707 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2708 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2709 wined3d_device_context_unlock(context);
2712 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2713 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2715 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2717 if (idx >= MAX_CONSTANT_BUFFERS)
2719 WARN("Invalid constant buffer index %u.\n", idx);
2720 return;
2723 *state = context->state->cb[shader_type][idx];
2726 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2727 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2729 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2731 WARN("Invalid view index %u.\n", idx);
2732 return NULL;
2735 return context->state->shader_resource_view[shader_type][idx];
2738 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2739 enum wined3d_shader_type shader_type, unsigned int idx)
2741 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2743 if (idx >= MAX_SAMPLER_OBJECTS)
2745 WARN("Invalid sampler index %u.\n", idx);
2746 return NULL;
2749 return context->state->sampler[shader_type][idx];
2752 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2753 unsigned int start_idx, unsigned int count, const BOOL *constants)
2755 unsigned int i;
2757 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2758 device, start_idx, count, constants);
2760 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2761 if (TRACE_ON(d3d))
2763 for (i = 0; i < count; ++i)
2764 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2767 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2770 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2771 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2773 unsigned int i;
2775 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2776 device, start_idx, count, constants);
2778 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2779 if (TRACE_ON(d3d))
2781 for (i = 0; i < count; ++i)
2782 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2785 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2788 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2789 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2791 unsigned int i;
2793 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2794 device, start_idx, count, constants);
2796 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2797 if (TRACE_ON(d3d))
2799 for (i = 0; i < count; ++i)
2800 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2803 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2806 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2807 unsigned int start_idx, unsigned int count, const BOOL *constants)
2809 unsigned int i;
2811 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2812 device, start_idx, count, constants);
2814 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2815 if (TRACE_ON(d3d))
2817 for (i = 0; i < count; ++i)
2818 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2821 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2824 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2825 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2827 unsigned int i;
2829 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2830 device, start_idx, count, constants);
2832 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2833 if (TRACE_ON(d3d))
2835 for (i = 0; i < count; ++i)
2836 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2839 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2842 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2843 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2845 unsigned int i;
2847 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2848 device, start_idx, count, constants);
2850 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2851 if (TRACE_ON(d3d))
2853 for (i = 0; i < count; ++i)
2854 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2857 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2860 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2861 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2863 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2865 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2867 WARN("Invalid UAV index %u.\n", idx);
2868 return NULL;
2871 return context->state->unordered_access_view[pipeline][idx];
2874 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2876 unsigned int i;
2878 if (!latency)
2879 latency = 3;
2881 device->max_frame_latency = latency;
2882 for (i = 0; i < device->swapchain_count; ++i)
2883 swapchain_set_max_frame_latency(device->swapchains[i], device);
2886 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2888 return device->max_frame_latency;
2891 static unsigned int wined3d_get_flexible_vertex_size(uint32_t fvf)
2893 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2894 unsigned int i, size = 0;
2896 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2897 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2898 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2899 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2900 switch (fvf & WINED3DFVF_POSITION_MASK)
2902 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2903 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2904 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2905 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2906 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2907 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2908 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2909 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2910 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2912 for (i = 0; i < texcoord_count; ++i)
2914 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2917 return size;
2920 static void wined3d_format_get_colour(const struct wined3d_format *format,
2921 const void *data, struct wined3d_color *colour)
2923 float *output = &colour->r;
2924 const uint32_t *u32_data;
2925 const uint16_t *u16_data;
2926 const float *f32_data;
2927 unsigned int i;
2929 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2930 static unsigned int warned;
2932 switch (format->id)
2934 case WINED3DFMT_B8G8R8A8_UNORM:
2935 u32_data = data;
2936 wined3d_color_from_d3dcolor(colour, *u32_data);
2937 break;
2939 case WINED3DFMT_R8G8B8A8_UNORM:
2940 u32_data = data;
2941 colour->r = (*u32_data & 0xffu) / 255.0f;
2942 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2943 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2944 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2945 break;
2947 case WINED3DFMT_R16G16_UNORM:
2948 case WINED3DFMT_R16G16B16A16_UNORM:
2949 u16_data = data;
2950 *colour = default_colour;
2951 for (i = 0; i < format->component_count; ++i)
2952 output[i] = u16_data[i] / 65535.0f;
2953 break;
2955 case WINED3DFMT_R32_FLOAT:
2956 case WINED3DFMT_R32G32_FLOAT:
2957 case WINED3DFMT_R32G32B32_FLOAT:
2958 case WINED3DFMT_R32G32B32A32_FLOAT:
2959 f32_data = data;
2960 *colour = default_colour;
2961 for (i = 0; i < format->component_count; ++i)
2962 output[i] = f32_data[i];
2963 break;
2965 default:
2966 *colour = default_colour;
2967 if (!warned++)
2968 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2969 break;
2973 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2974 const struct wined3d_color *material_colour, unsigned int index,
2975 const struct wined3d_stream_info *stream_info)
2977 const struct wined3d_stream_info_element *element = NULL;
2979 switch (mcs)
2981 case WINED3D_MCS_MATERIAL:
2982 *colour = *material_colour;
2983 return;
2985 case WINED3D_MCS_COLOR1:
2986 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2988 colour->r = colour->g = colour->b = colour->a = 1.0f;
2989 return;
2991 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2992 break;
2994 case WINED3D_MCS_COLOR2:
2995 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2997 colour->r = colour->g = colour->b = colour->a = 0.0f;
2998 return;
3000 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3001 break;
3003 default:
3004 colour->r = colour->g = colour->b = colour->a = 0.0f;
3005 ERR("Invalid material colour source %#x.\n", mcs);
3006 return;
3009 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
3012 static float wined3d_clamp(float value, float min_value, float max_value)
3014 return value < min_value ? min_value : value > max_value ? max_value : value;
3017 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3019 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
3022 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3024 v0->x -= v1->x;
3025 v0->y -= v1->y;
3026 v0->z -= v1->z;
3029 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
3031 v->x *= s;
3032 v->y *= s;
3033 v->z *= s;
3036 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
3038 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
3040 if (isfinite(rnorm))
3041 wined3d_vec3_scale(v, rnorm);
3044 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
3045 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
3047 struct wined3d_vec3 tmp;
3049 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
3050 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
3051 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
3053 *dst = tmp;
3056 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
3057 float min_value, float max_value)
3059 dst->r = wined3d_clamp(src->r, min_value, max_value);
3060 dst->g = wined3d_clamp(src->g, min_value, max_value);
3061 dst->b = wined3d_clamp(src->b, min_value, max_value);
3062 dst->a = wined3d_clamp(src->a, min_value, max_value);
3065 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
3067 dst->r += src->r * c;
3068 dst->g += src->g * c;
3069 dst->b += src->b * c;
3072 static void init_transformed_lights(struct lights_settings *ls,
3073 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
3075 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
3076 const struct wined3d_light_info *light_info;
3077 struct light_transformed *light;
3078 struct wined3d_vec4 vec4;
3079 unsigned int light_count;
3080 unsigned int i, index;
3082 memset(ls, 0, sizeof(*ls));
3084 ls->lighting = !!compute_lighting;
3085 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
3086 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
3087 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
3088 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
3089 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
3090 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
3092 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
3093 return;
3095 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
3096 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
3098 if (!compute_lighting)
3099 return;
3101 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
3103 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
3104 ls->legacy_lighting = !!legacy_lighting;
3105 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
3106 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
3108 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
3110 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
3112 if (!light_info->enabled)
3113 continue;
3115 switch (light_info->OriginalParms.type)
3117 case WINED3D_LIGHT_DIRECTIONAL:
3118 ++ls->directional_light_count;
3119 break;
3121 case WINED3D_LIGHT_POINT:
3122 ++ls->point_light_count;
3123 break;
3125 case WINED3D_LIGHT_SPOT:
3126 ++ls->spot_light_count;
3127 break;
3129 case WINED3D_LIGHT_PARALLELPOINT:
3130 ++ls->parallel_point_light_count;
3131 break;
3133 default:
3134 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
3135 continue;
3137 lights[index++] = light_info;
3138 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
3139 break;
3143 light_count = index;
3144 for (i = 0, index = 0; i < light_count; ++i)
3146 light_info = lights[i];
3147 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
3148 continue;
3150 light = &ls->lights[index];
3151 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3152 light->direction = *(struct wined3d_vec3 *)&vec4;
3153 wined3d_vec3_normalise(&light->direction);
3155 light->diffuse = light_info->OriginalParms.diffuse;
3156 light->ambient = light_info->OriginalParms.ambient;
3157 light->specular = light_info->OriginalParms.specular;
3158 ++index;
3161 for (i = 0; i < light_count; ++i)
3163 light_info = lights[i];
3164 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
3165 continue;
3167 light = &ls->lights[index];
3169 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3170 light->range = light_info->OriginalParms.range;
3171 light->c_att = light_info->OriginalParms.attenuation0;
3172 light->l_att = light_info->OriginalParms.attenuation1;
3173 light->q_att = light_info->OriginalParms.attenuation2;
3175 light->diffuse = light_info->OriginalParms.diffuse;
3176 light->ambient = light_info->OriginalParms.ambient;
3177 light->specular = light_info->OriginalParms.specular;
3178 ++index;
3181 for (i = 0; i < light_count; ++i)
3183 light_info = lights[i];
3184 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
3185 continue;
3187 light = &ls->lights[index];
3189 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3190 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3191 light->direction = *(struct wined3d_vec3 *)&vec4;
3192 wined3d_vec3_normalise(&light->direction);
3193 light->range = light_info->OriginalParms.range;
3194 light->falloff = light_info->OriginalParms.falloff;
3195 light->c_att = light_info->OriginalParms.attenuation0;
3196 light->l_att = light_info->OriginalParms.attenuation1;
3197 light->q_att = light_info->OriginalParms.attenuation2;
3198 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
3199 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
3201 light->diffuse = light_info->OriginalParms.diffuse;
3202 light->ambient = light_info->OriginalParms.ambient;
3203 light->specular = light_info->OriginalParms.specular;
3204 ++index;
3207 for (i = 0; i < light_count; ++i)
3209 light_info = lights[i];
3210 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
3211 continue;
3213 light = &ls->lights[index];
3215 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3216 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
3217 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
3218 light->diffuse = light_info->OriginalParms.diffuse;
3219 light->ambient = light_info->OriginalParms.ambient;
3220 light->specular = light_info->OriginalParms.specular;
3221 ++index;
3225 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
3226 const struct wined3d_vec3 *dir, float att, float material_shininess,
3227 const struct wined3d_vec3 *normal_transformed,
3228 const struct wined3d_vec3 *position_transformed_normalised,
3229 const struct light_transformed *light, const struct lights_settings *ls)
3231 struct wined3d_vec3 vec3;
3232 float t, c;
3234 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
3235 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
3237 vec3 = *dir;
3238 if (ls->localviewer)
3239 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
3240 else
3241 vec3.z -= 1.0f;
3242 wined3d_vec3_normalise(&vec3);
3243 t = wined3d_vec3_dot(normal_transformed, &vec3);
3244 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
3245 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
3246 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
3249 static void light_set_vertex_data(struct lights_settings *ls,
3250 const struct wined3d_vec4 *position)
3252 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3253 return;
3255 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3256 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3259 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3260 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3261 float material_shininess)
3263 struct wined3d_vec3 position_transformed_normalised;
3264 struct wined3d_vec3 normal_transformed = {0.0f};
3265 const struct light_transformed *light;
3266 struct wined3d_vec3 dir, dst;
3267 unsigned int i, index;
3268 float att;
3270 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3271 wined3d_vec3_normalise(&position_transformed_normalised);
3273 if (normal)
3275 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3276 if (ls->normalise)
3277 wined3d_vec3_normalise(&normal_transformed);
3280 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3281 *specular = *diffuse;
3282 *ambient = ls->ambient_light;
3284 index = 0;
3285 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3287 light = &ls->lights[index];
3289 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3290 if (normal)
3291 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3292 &normal_transformed, &position_transformed_normalised, light, ls);
3295 for (i = 0; i < ls->point_light_count; ++i, ++index)
3297 light = &ls->lights[index];
3298 dir.x = light->position.x - ls->position_transformed.x;
3299 dir.y = light->position.y - ls->position_transformed.y;
3300 dir.z = light->position.z - ls->position_transformed.z;
3302 dst.z = wined3d_vec3_dot(&dir, &dir);
3303 dst.y = sqrtf(dst.z);
3304 dst.x = 1.0f;
3305 if (ls->legacy_lighting)
3307 dst.y = (light->range - dst.y) / light->range;
3308 if (!(dst.y > 0.0f))
3309 continue;
3310 dst.z = dst.y * dst.y;
3312 else
3314 if (!(dst.y <= light->range))
3315 continue;
3317 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3318 if (!ls->legacy_lighting)
3319 att = 1.0f / att;
3321 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3322 if (normal)
3324 wined3d_vec3_normalise(&dir);
3325 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3326 &normal_transformed, &position_transformed_normalised, light, ls);
3330 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3332 float t;
3334 light = &ls->lights[index];
3336 dir.x = light->position.x - ls->position_transformed.x;
3337 dir.y = light->position.y - ls->position_transformed.y;
3338 dir.z = light->position.z - ls->position_transformed.z;
3340 dst.z = wined3d_vec3_dot(&dir, &dir);
3341 dst.y = sqrtf(dst.z);
3342 dst.x = 1.0f;
3344 if (ls->legacy_lighting)
3346 dst.y = (light->range - dst.y) / light->range;
3347 if (!(dst.y > 0.0f))
3348 continue;
3349 dst.z = dst.y * dst.y;
3351 else
3353 if (!(dst.y <= light->range))
3354 continue;
3356 wined3d_vec3_normalise(&dir);
3357 t = -wined3d_vec3_dot(&dir, &light->direction);
3358 if (t > light->cos_htheta)
3359 att = 1.0f;
3360 else if (t <= light->cos_hphi)
3361 att = 0.0f;
3362 else
3363 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3365 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3366 if (ls->legacy_lighting)
3367 att *= t;
3368 else
3369 att /= t;
3371 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3373 if (normal)
3374 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3375 &normal_transformed, &position_transformed_normalised, light, ls);
3378 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3380 light = &ls->lights[index];
3382 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3383 if (normal)
3384 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3385 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3389 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3391 switch (ls->fog_mode)
3393 case WINED3D_FOG_NONE:
3394 return fog_coord;
3395 case WINED3D_FOG_LINEAR:
3396 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3397 case WINED3D_FOG_EXP:
3398 return expf(-fog_coord * ls->fog_density);
3399 case WINED3D_FOG_EXP2:
3400 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3401 default:
3402 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3403 return 0.0f;
3407 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3409 float fog_coord;
3411 if (ls->fog_mode == WINED3D_FOG_NONE)
3412 return;
3414 switch (ls->fog_coord_mode)
3416 case WINED3D_FFP_VS_FOG_RANGE:
3417 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3418 (const struct wined3d_vec3 *)&ls->position_transformed));
3419 break;
3421 case WINED3D_FFP_VS_FOG_DEPTH:
3422 fog_coord = fabsf(ls->position_transformed.z);
3423 break;
3425 default:
3426 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3427 return;
3429 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3432 /* Context activation is done by the caller. */
3433 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3434 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3435 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, uint32_t flags, uint32_t dst_fvf)
3437 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3438 const struct wined3d_color *material_specular_state_colour;
3439 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3440 const struct wined3d_state *state = device->cs->c.state;
3441 const struct wined3d_format *output_colour_format;
3442 static const struct wined3d_color black;
3443 struct wined3d_map_desc map_desc;
3444 struct wined3d_box box = {0};
3445 struct wined3d_viewport vp;
3446 unsigned int texture_count;
3447 struct lights_settings ls;
3448 unsigned int vertex_size;
3449 BOOL do_clip, lighting;
3450 float min_z, max_z;
3451 unsigned int i;
3452 BYTE *dest_ptr;
3453 HRESULT hr;
3455 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3457 ERR("Source has no position mask.\n");
3458 return WINED3DERR_INVALIDCALL;
3461 if (state->render_states[WINED3D_RS_CLIPPING])
3463 static BOOL warned = FALSE;
3465 * The clipping code is not quite correct. Some things need
3466 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3467 * so disable clipping for now.
3468 * (The graphics in Half-Life are broken, and my processvertices
3469 * test crashes with IDirect3DDevice3)
3470 do_clip = TRUE;
3472 do_clip = FALSE;
3473 if (!warned)
3475 warned = TRUE;
3476 FIXME("Clipping is broken and disabled for now\n");
3479 else
3480 do_clip = FALSE;
3482 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3483 box.left = dwDestIndex * vertex_size;
3484 box.right = box.left + dwCount * vertex_size;
3485 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3487 WARN("Failed to map buffer, hr %#lx.\n", hr);
3488 return hr;
3490 dest_ptr = map_desc.data;
3492 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3493 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3494 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3496 TRACE("View mat:\n");
3497 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3498 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3499 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3500 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3502 TRACE("Proj mat:\n");
3503 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3504 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3505 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3506 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3508 TRACE("World mat:\n");
3509 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3510 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3511 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3512 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3514 /* Get the viewport */
3515 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3516 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3517 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3519 multiply_matrix(&mat,&view_mat,&world_mat);
3520 multiply_matrix(&mat,&proj_mat,&mat);
3522 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3524 lighting = state->render_states[WINED3D_RS_LIGHTING]
3525 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3526 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3527 &ambient_source, &specular_source, state, stream_info);
3528 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3529 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3530 ? &state->material.specular : &black;
3531 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3532 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3534 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3536 for (i = 0; i < dwCount; ++i)
3538 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3539 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3540 struct wined3d_color ambient, diffuse, specular;
3541 struct wined3d_vec4 position;
3542 unsigned int tex_index;
3544 position.x = p[0];
3545 position.y = p[1];
3546 position.z = p[2];
3547 position.w = 1.0f;
3549 light_set_vertex_data(&ls, &position);
3551 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3552 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3553 /* The position first */
3554 float x, y, z, rhw;
3555 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3557 /* Multiplication with world, view and projection matrix. */
3558 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3559 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3560 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3561 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3563 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3565 /* WARNING: The following things are taken from d3d7 and were not yet checked
3566 * against d3d8 or d3d9!
3569 /* Clipping conditions: From msdn
3571 * A vertex is clipped if it does not match the following requirements
3572 * -rhw < x <= rhw
3573 * -rhw < y <= rhw
3574 * 0 < z <= rhw
3575 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3577 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3578 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3582 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3583 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3585 /* "Normal" viewport transformation (not clipped)
3586 * 1) The values are divided by rhw
3587 * 2) The y axis is negative, so multiply it with -1
3588 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3589 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3590 * 4) Multiply x with Width/2 and add Width/2
3591 * 5) The same for the height
3592 * 6) Add the viewpoint X and Y to the 2D coordinates and
3593 * The minimum Z value to z
3594 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3596 * Well, basically it's simply a linear transformation into viewport
3597 * coordinates
3600 x /= rhw;
3601 y /= rhw;
3602 z /= rhw;
3604 y *= -1;
3606 x *= vp.width / 2;
3607 y *= vp.height / 2;
3608 z *= max_z - min_z;
3610 x += vp.width / 2 + vp.x;
3611 y += vp.height / 2 + vp.y;
3612 z += min_z;
3614 rhw = 1 / rhw;
3615 } else {
3616 /* That vertex got clipped
3617 * Contrary to OpenGL it is not dropped completely, it just
3618 * undergoes a different calculation.
3620 TRACE("Vertex got clipped\n");
3621 x += rhw;
3622 y += rhw;
3624 x /= 2;
3625 y /= 2;
3627 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3628 * outside of the main vertex buffer memory. That needs some more
3629 * investigation...
3633 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3636 ( (float *) dest_ptr)[0] = x;
3637 ( (float *) dest_ptr)[1] = y;
3638 ( (float *) dest_ptr)[2] = z;
3639 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3641 dest_ptr += 3 * sizeof(float);
3643 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3644 dest_ptr += sizeof(float);
3647 if (dst_fvf & WINED3DFVF_PSIZE)
3648 dest_ptr += sizeof(DWORD);
3650 if (dst_fvf & WINED3DFVF_NORMAL)
3652 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3653 const float *normal = (const float *)(element->data.addr + i * element->stride);
3654 /* AFAIK this should go into the lighting information */
3655 FIXME("Didn't expect the destination to have a normal\n");
3656 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3659 if (lighting)
3661 const struct wined3d_stream_info_element *element;
3662 struct wined3d_vec3 *normal;
3664 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3666 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3667 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3669 else
3671 normal = NULL;
3673 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3674 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3677 if (dst_fvf & WINED3DFVF_DIFFUSE)
3679 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3681 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3682 &state->material.diffuse, i, stream_info);
3684 if (lighting)
3686 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3687 &state->material.ambient, i, stream_info);
3688 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3689 &state->material.emissive, i, stream_info);
3691 diffuse_colour.r = ambient.r * material_ambient.r
3692 + diffuse.r * material_diffuse.r + material_emissive.r;
3693 diffuse_colour.g = ambient.g * material_ambient.g
3694 + diffuse.g * material_diffuse.g + material_emissive.g;
3695 diffuse_colour.b = ambient.b * material_ambient.b
3696 + diffuse.b * material_diffuse.b + material_emissive.b;
3697 diffuse_colour.a = material_diffuse.a;
3699 else
3701 diffuse_colour = material_diffuse;
3703 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3704 wined3d_format_convert_from_float(output_colour_format, &diffuse_colour, dest_ptr);
3705 dest_ptr += sizeof(DWORD);
3708 if (dst_fvf & WINED3DFVF_SPECULAR)
3710 struct wined3d_color material_specular, specular_colour;
3712 wined3d_colour_from_mcs(&material_specular, specular_source,
3713 material_specular_state_colour, i, stream_info);
3715 if (lighting)
3717 specular_colour.r = specular.r * material_specular.r;
3718 specular_colour.g = specular.g * material_specular.g;
3719 specular_colour.b = specular.b * material_specular.b;
3720 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3722 else
3724 specular_colour = material_specular;
3726 update_fog_factor(&specular_colour.a, &ls);
3727 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3728 wined3d_format_convert_from_float(output_colour_format, &specular_colour, dest_ptr);
3729 dest_ptr += sizeof(DWORD);
3732 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3734 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3735 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3736 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3738 ERR("No source texture, but destination requests one\n");
3739 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3741 else
3743 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3748 wined3d_resource_unmap(&dest->resource, 0);
3750 return WINED3D_OK;
3752 #undef copy_and_next
3754 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3755 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3756 const struct wined3d_vertex_declaration *declaration, uint32_t flags, uint32_t dst_fvf)
3758 struct wined3d_state *state = device->cs->c.state;
3759 struct wined3d_stream_info stream_info;
3760 struct wined3d_resource *resource;
3761 struct wined3d_box box = {0};
3762 struct wined3d_shader *vs;
3763 unsigned int i, j;
3764 uint32_t map;
3765 HRESULT hr;
3767 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3768 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3769 device, src_start_idx, dst_idx, vertex_count,
3770 dst_buffer, declaration, flags, dst_fvf);
3772 if (declaration)
3773 FIXME("Output vertex declaration not implemented yet.\n");
3775 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3776 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3777 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3778 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3780 /* We can't convert FROM a VBO, and vertex buffers used to source into
3781 * process_vertices() are unlikely to ever be used for drawing. Release
3782 * VBOs in those buffers and fix up the stream_info structure.
3784 * Also apply the start index. */
3785 map = stream_info.use_map;
3786 while (map)
3788 struct wined3d_stream_info_element *e;
3789 struct wined3d_map_desc map_desc;
3791 i = wined3d_bit_scan(&map);
3792 e = &stream_info.elements[i];
3793 resource = &state->streams[e->stream_idx].buffer->resource;
3794 box.left = src_start_idx * e->stride;
3795 box.right = box.left + vertex_count * e->stride;
3796 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3798 ERR("Failed to map resource.\n");
3799 map = stream_info.use_map;
3800 while (map)
3802 j = wined3d_bit_scan(&map);
3803 if (j >= i)
3804 break;
3806 e = &stream_info.elements[j];
3807 resource = &state->streams[e->stream_idx].buffer->resource;
3808 if (FAILED(wined3d_resource_unmap(resource, 0)))
3809 ERR("Failed to unmap resource.\n");
3811 return WINED3DERR_INVALIDCALL;
3813 e->data.buffer_object = 0;
3814 e->data.addr += (ULONG_PTR)map_desc.data;
3817 hr = process_vertices_strided(device, dst_idx, vertex_count,
3818 &stream_info, dst_buffer, flags, dst_fvf);
3820 map = stream_info.use_map;
3821 while (map)
3823 i = wined3d_bit_scan(&map);
3824 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3825 if (FAILED(wined3d_resource_unmap(resource, 0)))
3826 ERR("Failed to unmap resource.\n");
3829 return hr;
3832 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3833 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3835 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3837 TRACE("device %p, stage %u, state %s, value %lx.\n",
3838 device, stage, debug_d3dtexturestate(state), value);
3840 if (stage >= d3d_info->limits.ffp_blend_stages)
3842 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3843 stage, d3d_info->limits.ffp_blend_stages - 1);
3844 return;
3847 if (value == device->cs->c.state->texture_states[stage][state])
3849 TRACE("Application is setting the old value over, nothing to do.\n");
3850 return;
3853 device->cs->c.state->texture_states[stage][state] = value;
3855 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
3858 static void wined3d_device_set_texture(struct wined3d_device *device,
3859 UINT stage, struct wined3d_texture *texture)
3861 struct wined3d_state *state = device->cs->c.state;
3862 struct wined3d_texture *prev;
3864 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3866 /* Windows accepts overflowing this array... we do not. */
3867 if (stage >= ARRAY_SIZE(state->textures))
3869 WARN("Ignoring invalid stage %u.\n", stage);
3870 return;
3873 prev = state->textures[stage];
3874 TRACE("Previous texture %p.\n", prev);
3876 if (texture == prev)
3878 TRACE("App is setting the same texture again, nothing to do.\n");
3879 return;
3882 TRACE("Setting new texture to %p.\n", texture);
3883 state->textures[stage] = texture;
3885 if (texture)
3886 wined3d_texture_incref(texture);
3887 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
3888 if (prev)
3889 wined3d_texture_decref(prev);
3891 return;
3894 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3895 struct wined3d_stateblock *stateblock)
3897 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3898 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3899 const struct wined3d_saved_states *changed = &stateblock->changed;
3900 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3901 struct wined3d_device_context *context = &device->cs->c;
3902 unsigned int i, j, start, idx;
3903 bool set_depth_bounds = false;
3904 struct wined3d_range range;
3905 uint32_t map;
3907 TRACE("device %p, stateblock %p.\n", device, stateblock);
3909 if (changed->vertexShader)
3910 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
3911 if (changed->pixelShader)
3912 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
3914 for (start = 0; ; start = range.offset + range.size)
3916 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3917 break;
3919 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3922 map = changed->vertexShaderConstantsI;
3923 for (start = 0; ; start = range.offset + range.size)
3925 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3926 break;
3928 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3931 map = changed->vertexShaderConstantsB;
3932 for (start = 0; ; start = range.offset + range.size)
3934 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3935 break;
3937 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3940 for (start = 0; ; start = range.offset + range.size)
3942 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3943 break;
3945 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3948 map = changed->pixelShaderConstantsI;
3949 for (start = 0; ; start = range.offset + range.size)
3951 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3952 break;
3954 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3957 map = changed->pixelShaderConstantsB;
3958 for (start = 0; ; start = range.offset + range.size)
3960 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3961 break;
3963 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3966 if (changed->lights)
3968 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3970 const struct wined3d_light_info *light;
3972 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3974 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
3975 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3980 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3982 map = changed->renderState[i];
3983 while (map)
3985 j = wined3d_bit_scan(&map);
3986 idx = i * word_bit_count + j;
3988 switch (idx)
3990 case WINED3D_RS_BLENDFACTOR:
3991 case WINED3D_RS_MULTISAMPLEMASK:
3992 case WINED3D_RS_ALPHABLENDENABLE:
3993 case WINED3D_RS_SRCBLEND:
3994 case WINED3D_RS_DESTBLEND:
3995 case WINED3D_RS_BLENDOP:
3996 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
3997 case WINED3D_RS_SRCBLENDALPHA:
3998 case WINED3D_RS_DESTBLENDALPHA:
3999 case WINED3D_RS_BLENDOPALPHA:
4000 case WINED3D_RS_COLORWRITEENABLE:
4001 case WINED3D_RS_COLORWRITEENABLE1:
4002 case WINED3D_RS_COLORWRITEENABLE2:
4003 case WINED3D_RS_COLORWRITEENABLE3:
4004 set_blend_state = TRUE;
4005 break;
4007 case WINED3D_RS_BACK_STENCILFAIL:
4008 case WINED3D_RS_BACK_STENCILFUNC:
4009 case WINED3D_RS_BACK_STENCILPASS:
4010 case WINED3D_RS_BACK_STENCILZFAIL:
4011 case WINED3D_RS_STENCILENABLE:
4012 case WINED3D_RS_STENCILFAIL:
4013 case WINED3D_RS_STENCILFUNC:
4014 case WINED3D_RS_STENCILREF:
4015 case WINED3D_RS_STENCILMASK:
4016 case WINED3D_RS_STENCILPASS:
4017 case WINED3D_RS_STENCILWRITEMASK:
4018 case WINED3D_RS_STENCILZFAIL:
4019 case WINED3D_RS_TWOSIDEDSTENCILMODE:
4020 case WINED3D_RS_ZENABLE:
4021 case WINED3D_RS_ZFUNC:
4022 case WINED3D_RS_ZWRITEENABLE:
4023 set_depth_stencil_state = TRUE;
4024 break;
4026 case WINED3D_RS_FILLMODE:
4027 case WINED3D_RS_CULLMODE:
4028 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
4029 case WINED3D_RS_DEPTHBIAS:
4030 case WINED3D_RS_SCISSORTESTENABLE:
4031 case WINED3D_RS_ANTIALIASEDLINEENABLE:
4032 set_rasterizer_state = TRUE;
4033 break;
4035 case WINED3D_RS_ADAPTIVETESS_X:
4036 case WINED3D_RS_ADAPTIVETESS_Z:
4037 case WINED3D_RS_ADAPTIVETESS_W:
4038 set_depth_bounds = true;
4039 break;
4041 case WINED3D_RS_ADAPTIVETESS_Y:
4042 break;
4044 case WINED3D_RS_ANTIALIAS:
4045 if (state->rs[WINED3D_RS_ANTIALIAS])
4046 FIXME("Antialias not supported yet.\n");
4047 break;
4049 case WINED3D_RS_TEXTUREPERSPECTIVE:
4050 break;
4052 case WINED3D_RS_WRAPU:
4053 if (state->rs[WINED3D_RS_WRAPU])
4054 FIXME("Render state WINED3D_RS_WRAPU not implemented yet.\n");
4055 break;
4057 case WINED3D_RS_WRAPV:
4058 if (state->rs[WINED3D_RS_WRAPV])
4059 FIXME("Render state WINED3D_RS_WRAPV not implemented yet.\n");
4060 break;
4062 case WINED3D_RS_MONOENABLE:
4063 if (state->rs[WINED3D_RS_MONOENABLE])
4064 FIXME("Render state WINED3D_RS_MONOENABLE not implemented yet.\n");
4065 break;
4067 case WINED3D_RS_ROP2:
4068 if (state->rs[WINED3D_RS_ROP2])
4069 FIXME("Render state WINED3D_RS_ROP2 not implemented yet.\n");
4070 break;
4072 case WINED3D_RS_PLANEMASK:
4073 if (state->rs[WINED3D_RS_PLANEMASK])
4074 FIXME("Render state WINED3D_RS_PLANEMASK not implemented yet.\n");
4075 break;
4077 case WINED3D_RS_LASTPIXEL:
4078 if (!state->rs[WINED3D_RS_LASTPIXEL])
4080 static bool warned;
4081 if (!warned)
4083 FIXME("Last Pixel Drawing Disabled, not handled yet.\n");
4084 warned = true;
4087 break;
4089 case WINED3D_RS_ZVISIBLE:
4090 if (state->rs[WINED3D_RS_ZVISIBLE])
4091 FIXME("WINED3D_RS_ZVISIBLE not implemented.\n");
4092 break;
4094 case WINED3D_RS_SUBPIXEL:
4095 if (state->rs[WINED3D_RS_SUBPIXEL])
4096 FIXME("Render state WINED3D_RS_SUBPIXEL not implemented yet.\n");
4097 break;
4099 case WINED3D_RS_SUBPIXELX:
4100 if (state->rs[WINED3D_RS_SUBPIXELX])
4101 FIXME("Render state WINED3D_RS_SUBPIXELX not implemented yet.\n");
4102 break;
4104 case WINED3D_RS_STIPPLEDALPHA:
4105 if (state->rs[WINED3D_RS_STIPPLEDALPHA])
4106 FIXME("Stippled Alpha not supported yet.\n");
4107 break;
4109 case WINED3D_RS_STIPPLEENABLE:
4110 if (state->rs[WINED3D_RS_STIPPLEENABLE])
4111 FIXME("Render state WINED3D_RS_STIPPLEENABLE not implemented yet.\n");
4112 break;
4114 case WINED3D_RS_MIPMAPLODBIAS:
4115 if (state->rs[WINED3D_RS_MIPMAPLODBIAS])
4116 FIXME("Render state WINED3D_RS_MIPMAPLODBIAS not implemented yet.\n");
4117 break;
4119 case WINED3D_RS_ANISOTROPY:
4120 if (state->rs[WINED3D_RS_ANISOTROPY])
4121 FIXME("Render state WINED3D_RS_ANISOTROPY not implemented yet.\n");
4122 break;
4124 case WINED3D_RS_FLUSHBATCH:
4125 if (state->rs[WINED3D_RS_FLUSHBATCH])
4126 FIXME("Render state WINED3D_RS_FLUSHBATCH not implemented yet.\n");
4127 break;
4129 case WINED3D_RS_TRANSLUCENTSORTINDEPENDENT:
4130 if (state->rs[WINED3D_RS_TRANSLUCENTSORTINDEPENDENT])
4131 FIXME("Render state WINED3D_RS_TRANSLUCENTSORTINDEPENDENT not implemented yet.\n");
4132 break;
4134 case WINED3D_RS_WRAP0:
4135 case WINED3D_RS_WRAP1:
4136 case WINED3D_RS_WRAP2:
4137 case WINED3D_RS_WRAP3:
4138 case WINED3D_RS_WRAP4:
4139 case WINED3D_RS_WRAP5:
4140 case WINED3D_RS_WRAP6:
4141 case WINED3D_RS_WRAP7:
4142 case WINED3D_RS_WRAP8:
4143 case WINED3D_RS_WRAP9:
4144 case WINED3D_RS_WRAP10:
4145 case WINED3D_RS_WRAP11:
4146 case WINED3D_RS_WRAP12:
4147 case WINED3D_RS_WRAP13:
4148 case WINED3D_RS_WRAP14:
4149 case WINED3D_RS_WRAP15:
4151 static unsigned int once;
4153 if ((state->rs[idx]) && !once++)
4154 FIXME("(WINED3D_RS_WRAP0) Texture wrapping not yet supported.\n");
4155 break;
4158 case WINED3D_RS_EXTENTS:
4159 if (state->rs[WINED3D_RS_EXTENTS])
4160 FIXME("Render state WINED3D_RS_EXTENTS not implemented yet.\n");
4161 break;
4163 case WINED3D_RS_COLORKEYBLENDENABLE:
4164 if (state->rs[WINED3D_RS_COLORKEYBLENDENABLE])
4165 FIXME("Render state WINED3D_RS_COLORKEYBLENDENABLE not implemented yet.\n");
4166 break;
4168 case WINED3D_RS_SOFTWAREVERTEXPROCESSING:
4170 static unsigned int once;
4172 if ((state->rs[WINED3D_RS_SOFTWAREVERTEXPROCESSING]) && !once++)
4173 FIXME("Software vertex processing not implemented.\n");
4174 break;
4177 case WINED3D_RS_PATCHEDGESTYLE:
4178 if (state->rs[WINED3D_RS_PATCHEDGESTYLE] != WINED3D_PATCH_EDGE_DISCRETE)
4179 FIXME("WINED3D_RS_PATCHEDGESTYLE %#x not yet implemented.\n",
4180 state->rs[WINED3D_RS_PATCHEDGESTYLE]);
4181 break;
4183 case WINED3D_RS_PATCHSEGMENTS:
4185 union
4187 DWORD d;
4188 float f;
4189 } tmpvalue;
4190 tmpvalue.f = 1.0f;
4192 if (state->rs[WINED3D_RS_PATCHSEGMENTS] != tmpvalue.d)
4194 static bool displayed = false;
4196 tmpvalue.d = state->rs[WINED3D_RS_PATCHSEGMENTS];
4197 if(!displayed)
4198 FIXME("(WINED3D_RS_PATCHSEGMENTS,%f) not yet implemented.\n", tmpvalue.f);
4200 displayed = true;
4202 break;
4205 case WINED3D_RS_DEBUGMONITORTOKEN:
4206 WARN("token: %#x.\n", state->rs[WINED3D_RS_DEBUGMONITORTOKEN]);
4207 break;
4209 case WINED3D_RS_INDEXEDVERTEXBLENDENABLE:
4210 break;
4212 case WINED3D_RS_TWEENFACTOR:
4213 break;
4215 case WINED3D_RS_POSITIONDEGREE:
4216 if (state->rs[WINED3D_RS_POSITIONDEGREE] != WINED3D_DEGREE_CUBIC)
4217 FIXME("WINED3D_RS_POSITIONDEGREE %#x not yet implemented.\n",
4218 state->rs[WINED3D_RS_POSITIONDEGREE]);
4219 break;
4221 case WINED3D_RS_NORMALDEGREE:
4222 if (state->rs[WINED3D_RS_NORMALDEGREE] != WINED3D_DEGREE_LINEAR)
4223 FIXME("WINED3D_RS_NORMALDEGREE %#x not yet implemented.\n",
4224 state->rs[WINED3D_RS_NORMALDEGREE]);
4225 break;
4227 case WINED3D_RS_MINTESSELLATIONLEVEL:
4228 break;
4230 case WINED3D_RS_MAXTESSELLATIONLEVEL:
4231 break;
4233 case WINED3D_RS_ENABLEADAPTIVETESSELLATION:
4234 if (state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION])
4235 FIXME("WINED3D_RS_ENABLEADAPTIVETESSELLATION %#x not yet implemented.\n",
4236 state->rs[WINED3D_RS_ENABLEADAPTIVETESSELLATION]);
4237 break;
4239 default:
4240 wined3d_device_set_render_state(device, idx, state->rs[idx]);
4241 break;
4246 if (set_rasterizer_state)
4248 struct wined3d_rasterizer_state *rasterizer_state;
4249 struct wined3d_rasterizer_state_desc desc;
4250 struct wine_rb_entry *entry;
4251 union
4253 DWORD d;
4254 float f;
4255 } bias;
4257 memset(&desc, 0, sizeof(desc));
4258 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
4259 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
4260 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
4261 desc.depth_bias = bias.f;
4262 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
4263 desc.scale_bias = bias.f;
4264 desc.depth_clip = TRUE;
4265 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
4266 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
4268 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
4270 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
4271 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
4273 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
4274 &wined3d_null_parent_ops, &rasterizer_state)))
4276 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
4277 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
4279 ERR("Failed to insert rasterizer state.\n");
4280 wined3d_rasterizer_state_decref(rasterizer_state);
4285 if (set_blend_state || changed->alpha_to_coverage
4286 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
4288 struct wined3d_blend_state *blend_state;
4289 struct wined3d_blend_state_desc desc;
4290 struct wine_rb_entry *entry;
4291 struct wined3d_color colour;
4292 unsigned int sample_mask;
4294 memset(&desc, 0, sizeof(desc));
4295 desc.alpha_to_coverage = state->alpha_to_coverage;
4296 desc.independent = FALSE;
4297 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
4298 desc.alpha_to_coverage = TRUE;
4299 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
4300 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
4301 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
4302 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
4303 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
4305 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
4306 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
4307 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
4309 else
4311 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
4312 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
4313 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
4315 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
4316 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
4317 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
4318 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
4319 if (desc.rt[1].writemask != desc.rt[0].writemask
4320 || desc.rt[2].writemask != desc.rt[0].writemask
4321 || desc.rt[3].writemask != desc.rt[0].writemask)
4323 desc.independent = TRUE;
4324 for (i = 1; i < 4; ++i)
4326 desc.rt[i].enable = desc.rt[0].enable;
4327 desc.rt[i].src = desc.rt[0].src;
4328 desc.rt[i].dst = desc.rt[0].dst;
4329 desc.rt[i].op = desc.rt[0].op;
4330 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
4331 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
4332 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
4336 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
4337 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
4338 else
4339 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
4341 if ((entry = wine_rb_get(&device->blend_states, &desc)))
4343 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
4344 wined3d_device_context_set_blend_state(context, blend_state, &colour,
4345 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4347 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
4348 &wined3d_null_parent_ops, &blend_state)))
4350 wined3d_device_context_set_blend_state(context, blend_state, &colour,
4351 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4352 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
4354 ERR("Failed to insert blend state.\n");
4355 wined3d_blend_state_decref(blend_state);
4360 if (set_depth_stencil_state)
4362 struct wined3d_depth_stencil_state *depth_stencil_state;
4363 struct wined3d_depth_stencil_state_desc desc;
4364 struct wine_rb_entry *entry;
4365 unsigned int stencil_ref;
4367 memset(&desc, 0, sizeof(desc));
4368 switch (state->rs[WINED3D_RS_ZENABLE])
4370 case WINED3D_ZB_FALSE:
4371 desc.depth = FALSE;
4372 break;
4374 case WINED3D_ZB_USEW:
4375 FIXME("W buffer is not well handled.\n");
4376 case WINED3D_ZB_TRUE:
4377 desc.depth = TRUE;
4378 break;
4380 default:
4381 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
4383 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
4384 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
4385 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
4386 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
4387 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
4388 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
4389 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
4390 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
4391 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
4393 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
4395 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
4396 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
4397 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
4398 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
4400 else
4402 desc.back = desc.front;
4405 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
4406 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
4407 else
4408 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
4410 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
4412 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
4413 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
4415 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
4416 &wined3d_null_parent_ops, &depth_stencil_state)))
4418 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
4419 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
4421 ERR("Failed to insert depth/stencil state.\n");
4422 wined3d_depth_stencil_state_decref(depth_stencil_state);
4427 if (set_depth_bounds)
4429 wined3d_device_context_set_depth_bounds(context,
4430 state->rs[WINED3D_RS_ADAPTIVETESS_X] == WINED3DFMT_NVDB,
4431 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_Z]),
4432 int_to_float(state->rs[WINED3D_RS_ADAPTIVETESS_W]));
4435 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
4437 map = changed->textureState[i];
4438 while (map)
4440 j = wined3d_bit_scan(&map);
4441 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
4445 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
4447 map = changed->samplerState[i];
4448 while (map)
4450 j = wined3d_bit_scan(&map);
4451 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
4455 if (changed->transforms)
4457 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
4459 map = changed->transform[i];
4460 while (map)
4462 j = wined3d_bit_scan(&map);
4463 idx = i * word_bit_count + j;
4464 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
4469 if (changed->indices)
4470 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
4471 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
4472 if (changed->vertexDecl)
4473 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
4474 if (changed->material)
4475 wined3d_device_set_material(device, &state->material);
4476 if (changed->viewport)
4477 wined3d_device_context_set_viewports(context, 1, &state->viewport);
4478 if (changed->scissorRect)
4479 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
4481 map = changed->streamSource | changed->streamFreq;
4482 while (map)
4484 i = wined3d_bit_scan(&map);
4485 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
4488 map = changed->textures;
4489 while (map)
4491 i = wined3d_bit_scan(&map);
4492 wined3d_device_set_texture(device, i, state->textures[i]);
4495 map = changed->clipplane;
4496 while (map)
4498 i = wined3d_bit_scan(&map);
4499 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4502 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4504 TRACE("Applied stateblock %p.\n", stateblock);
4507 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4509 TRACE("device %p, caps %p.\n", device, caps);
4511 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4514 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4515 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4517 struct wined3d_swapchain *swapchain;
4519 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4520 device, swapchain_idx, mode, rotation);
4522 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4523 return WINED3DERR_INVALIDCALL;
4525 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4528 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4530 /* At the moment we have no need for any functionality at the beginning
4531 * of a scene. */
4532 TRACE("device %p.\n", device);
4534 if (device->inScene)
4536 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4537 return WINED3DERR_INVALIDCALL;
4539 device->inScene = TRUE;
4540 return WINED3D_OK;
4543 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4545 TRACE("device %p.\n", device);
4547 if (!device->inScene)
4549 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4550 return WINED3DERR_INVALIDCALL;
4553 device->inScene = FALSE;
4554 return WINED3D_OK;
4557 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, unsigned int rect_count,
4558 const RECT *rects, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
4560 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4562 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4563 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4565 if (!rect_count && rects)
4567 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4568 return WINED3D_OK;
4571 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4573 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4574 if (!ds)
4576 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4577 /* TODO: What about depth stencil buffers without stencil bits? */
4578 return WINED3DERR_INVALIDCALL;
4580 else if (flags & WINED3DCLEAR_TARGET)
4582 if (ds->width < fb->render_targets[0]->width
4583 || ds->height < fb->render_targets[0]->height)
4585 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4586 return WINED3D_OK;
4591 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4593 return WINED3D_OK;
4596 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4598 struct wined3d_state *state = context->state;
4600 TRACE("context %p, value %p.\n", context, value);
4602 if (value)
4603 *value = state->predicate_value;
4604 return state->predicate;
4607 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4608 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4610 struct wined3d_state *state = context->state;
4612 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4613 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4615 wined3d_device_context_lock(context);
4616 state->primitive_type = primitive_type;
4617 state->patch_vertex_count = patch_vertex_count;
4618 wined3d_device_context_unlock(context);
4621 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4622 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4624 const struct wined3d_state *state = context->state;
4626 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4627 context, primitive_type, patch_vertex_count);
4629 *primitive_type = state->primitive_type;
4630 if (patch_vertex_count)
4631 *patch_vertex_count = state->patch_vertex_count;
4633 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4636 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4637 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4639 unsigned int src_size, dst_size, src_skip_levels = 0;
4640 unsigned int src_level_count, dst_level_count;
4641 const struct wined3d_dirty_regions *regions;
4642 unsigned int layer_count, level_count, i, j;
4643 enum wined3d_resource_type type;
4644 BOOL entire_texture = TRUE;
4645 struct wined3d_box box;
4647 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4649 /* Verify that the source and destination textures are non-NULL. */
4650 if (!src_texture || !dst_texture)
4652 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4653 return WINED3DERR_INVALIDCALL;
4656 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4657 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4659 WARN("Source resource is GPU accessible or a scratch resource.\n");
4660 return WINED3DERR_INVALIDCALL;
4662 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4664 WARN("Destination resource is CPU accessible.\n");
4665 return WINED3DERR_INVALIDCALL;
4668 /* Verify that the source and destination textures are the same type. */
4669 type = src_texture->resource.type;
4670 if (dst_texture->resource.type != type)
4672 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4673 return WINED3DERR_INVALIDCALL;
4676 layer_count = src_texture->layer_count;
4677 if (layer_count != dst_texture->layer_count)
4679 WARN("Source and destination have different layer counts.\n");
4680 return WINED3DERR_INVALIDCALL;
4683 if (src_texture->resource.format != dst_texture->resource.format)
4685 WARN("Source and destination formats do not match.\n");
4686 return WINED3DERR_INVALIDCALL;
4689 src_level_count = src_texture->level_count;
4690 dst_level_count = dst_texture->level_count;
4691 level_count = min(src_level_count, dst_level_count);
4693 src_size = max(src_texture->resource.width, src_texture->resource.height);
4694 src_size = max(src_size, src_texture->resource.depth);
4695 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4696 dst_size = max(dst_size, dst_texture->resource.depth);
4697 while (src_size > dst_size)
4699 src_size >>= 1;
4700 ++src_skip_levels;
4703 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4704 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4705 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4707 WARN("Source and destination dimensions do not match.\n");
4708 return WINED3DERR_INVALIDCALL;
4711 if ((regions = src_texture->dirty_regions))
4713 for (i = 0; i < layer_count && entire_texture; ++i)
4715 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4716 continue;
4718 entire_texture = FALSE;
4719 break;
4723 /* Update every surface level of the texture. */
4724 if (entire_texture)
4726 for (i = 0; i < level_count; ++i)
4728 wined3d_texture_get_level_box(dst_texture, i, &box);
4729 for (j = 0; j < layer_count; ++j)
4731 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4732 &dst_texture->resource, j * dst_level_count + i, &box,
4733 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4734 0, NULL, WINED3D_TEXF_POINT);
4738 else
4740 unsigned int src_level, box_count, k;
4741 const struct wined3d_box *boxes;
4742 struct wined3d_box b;
4744 for (i = 0; i < layer_count; ++i)
4746 boxes = regions[i].boxes;
4747 box_count = regions[i].box_count;
4748 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4750 boxes = &b;
4751 box_count = 1;
4752 wined3d_texture_get_level_box(dst_texture, i, &b);
4755 for (j = 0; j < level_count; ++j)
4757 src_level = j + src_skip_levels;
4759 /* TODO: We could pass an array of boxes here to avoid
4760 * multiple context acquisitions for the same resource. */
4761 for (k = 0; k < box_count; ++k)
4763 box = boxes[k];
4764 if (src_level)
4766 box.left >>= src_level;
4767 box.top >>= src_level;
4768 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4769 wined3d_texture_get_level_width(src_texture, src_level));
4770 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4771 wined3d_texture_get_level_height(src_texture, src_level));
4772 box.front >>= src_level;
4773 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4774 wined3d_texture_get_level_depth(src_texture, src_level));
4777 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4778 &dst_texture->resource, i * dst_level_count + j, &box,
4779 &src_texture->resource, i * src_level_count + src_level, &box,
4780 0, NULL, WINED3D_TEXF_POINT);
4786 wined3d_texture_clear_dirty_regions(src_texture);
4788 return WINED3D_OK;
4791 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4793 const struct wined3d_state *state = device->cs->c.state;
4794 struct wined3d_texture *texture;
4795 unsigned i;
4797 TRACE("device %p, num_passes %p.\n", device, num_passes);
4799 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4801 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4803 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4804 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4806 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4808 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4809 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4812 texture = state->textures[i];
4813 if (!texture || texture->resource.format_caps & WINED3D_FORMAT_CAP_FILTERING)
4814 continue;
4816 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4818 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4819 return E_FAIL;
4821 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4823 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4824 return E_FAIL;
4826 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4827 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4829 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4830 return E_FAIL;
4834 if (wined3d_state_uses_depth_buffer(state)
4835 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4837 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4838 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4840 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4842 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4843 return WINED3DERR_CONFLICTINGRENDERSTATE;
4847 /* return a sensible default */
4848 *num_passes = 1;
4850 TRACE("returning D3D_OK\n");
4851 return WINED3D_OK;
4854 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4856 static BOOL warned;
4858 TRACE("device %p, software %#x.\n", device, software);
4860 if (!warned)
4862 FIXME("device %p, software %#x stub!\n", device, software);
4863 warned = TRUE;
4866 device->softwareVertexProcessing = software;
4869 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4871 static BOOL warned;
4873 TRACE("device %p.\n", device);
4875 if (!warned)
4877 TRACE("device %p stub!\n", device);
4878 warned = TRUE;
4881 return device->softwareVertexProcessing;
4884 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4885 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4887 struct wined3d_swapchain *swapchain;
4889 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4890 device, swapchain_idx, raster_status);
4892 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4893 return WINED3DERR_INVALIDCALL;
4895 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4898 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4900 static BOOL warned;
4902 TRACE("device %p, segments %.8e.\n", device, segments);
4904 if (segments != 0.0f)
4906 if (!warned)
4908 FIXME("device %p, segments %.8e stub!\n", device, segments);
4909 warned = TRUE;
4913 return WINED3D_OK;
4916 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4918 static BOOL warned;
4920 TRACE("device %p.\n", device);
4922 if (!warned)
4924 FIXME("device %p stub!\n", device);
4925 warned = TRUE;
4928 return 0.0f;
4931 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4932 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4934 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4935 context, dst_buffer, offset, uav);
4937 wined3d_device_context_lock(context);
4938 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4939 wined3d_device_context_unlock(context);
4942 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4943 const struct wined3d_resource *dst_resource)
4945 if (src_resource->format->id == dst_resource->format->id)
4946 return true;
4947 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4948 return true;
4949 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4950 return false;
4951 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
4952 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK))
4953 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4954 if ((src_resource->format_attrs & WINED3D_FORMAT_ATTR_CAST_TO_BLOCK)
4955 && (dst_resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS))
4956 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4957 return false;
4960 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4961 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4963 unsigned int src_row_block_count, dst_row_block_count;
4964 struct wined3d_texture *dst_texture, *src_texture;
4965 unsigned int src_row_count, dst_row_count;
4966 struct wined3d_box src_box, dst_box;
4967 unsigned int i, j;
4969 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4971 if (src_resource == dst_resource)
4973 WARN("Source and destination are the same resource.\n");
4974 return;
4977 if (src_resource->type != dst_resource->type)
4979 WARN("Resource types (%s / %s) don't match.\n",
4980 debug_d3dresourcetype(dst_resource->type),
4981 debug_d3dresourcetype(src_resource->type));
4982 return;
4985 if (!resources_format_compatible(src_resource, dst_resource))
4987 WARN("Resource formats %s and %s are incompatible.\n",
4988 debug_d3dformat(dst_resource->format->id),
4989 debug_d3dformat(src_resource->format->id));
4990 return;
4993 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4994 / src_resource->format->block_width;
4995 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4996 / dst_resource->format->block_width;
4997 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4998 / src_resource->format->block_height;
4999 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
5000 / dst_resource->format->block_height;
5002 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
5003 || src_resource->depth != dst_resource->depth)
5005 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
5006 dst_row_block_count, dst_row_count, dst_resource->depth,
5007 src_row_block_count, src_row_count, src_resource->depth);
5008 return;
5011 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
5013 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
5014 wined3d_device_context_lock(context);
5015 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
5016 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5017 wined3d_device_context_unlock(context);
5018 return;
5021 dst_texture = texture_from_resource(dst_resource);
5022 src_texture = texture_from_resource(src_resource);
5024 if (src_texture->layer_count != dst_texture->layer_count
5025 || src_texture->level_count != dst_texture->level_count)
5027 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
5028 dst_texture->layer_count, dst_texture->level_count,
5029 src_texture->layer_count, src_texture->level_count);
5030 return;
5033 wined3d_device_context_lock(context);
5034 for (i = 0; i < dst_texture->level_count; ++i)
5036 wined3d_texture_get_level_box(src_texture, i, &src_box);
5037 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
5038 for (j = 0; j < dst_texture->layer_count; ++j)
5040 unsigned int idx = j * dst_texture->level_count + i;
5042 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
5043 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5046 wined3d_device_context_unlock(context);
5049 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
5050 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
5051 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
5052 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
5054 struct wined3d_box dst_box, b;
5056 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
5057 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
5058 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
5059 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
5061 if (flags)
5062 FIXME("Ignoring flags %#x.\n", flags);
5064 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
5066 WARN("Source and destination are the same sub-resource.\n");
5067 return WINED3DERR_INVALIDCALL;
5070 if (!resources_format_compatible(src_resource, dst_resource))
5072 WARN("Resource formats %s and %s are incompatible.\n",
5073 debug_d3dformat(dst_resource->format->id),
5074 debug_d3dformat(src_resource->format->id));
5075 return WINED3DERR_INVALIDCALL;
5078 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
5080 if (src_resource->type != WINED3D_RTYPE_BUFFER)
5082 WARN("Resource types (%s / %s) don't match.\n",
5083 debug_d3dresourcetype(dst_resource->type),
5084 debug_d3dresourcetype(src_resource->type));
5085 return WINED3DERR_INVALIDCALL;
5088 if (dst_sub_resource_idx)
5090 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
5091 return WINED3DERR_INVALIDCALL;
5094 if (src_sub_resource_idx)
5096 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
5097 return WINED3DERR_INVALIDCALL;
5100 if (!src_box)
5102 unsigned int dst_w;
5104 dst_w = dst_resource->size - dst_x;
5105 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
5106 src_box = &b;
5108 else if ((src_box->left >= src_box->right
5109 || src_box->top >= src_box->bottom
5110 || src_box->front >= src_box->back))
5112 WARN("Invalid box %s specified.\n", debug_box(src_box));
5113 return WINED3DERR_INVALIDCALL;
5116 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
5117 || src_box->right - src_box->left > dst_resource->size - dst_x)
5119 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
5120 dst_x, src_box->left, src_box->right - src_box->left);
5121 return WINED3DERR_INVALIDCALL;
5124 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
5126 else
5128 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
5129 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
5130 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
5131 unsigned int src_row_block_count, src_row_count;
5133 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
5134 return WINED3DERR_INVALIDCALL;
5136 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx))
5137 return WINED3DERR_INVALIDCALL;
5139 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
5141 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
5142 return WINED3DERR_INVALIDCALL;
5145 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
5147 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
5148 return WINED3DERR_INVALIDCALL;
5151 if (!src_box)
5153 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
5155 src_w = wined3d_texture_get_level_width(src_texture, src_level);
5156 src_h = wined3d_texture_get_level_height(src_texture, src_level);
5157 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
5159 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5160 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
5161 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
5162 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
5164 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
5165 src_box = &b;
5167 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
5169 WARN("Invalid source box %s.\n", debug_box(src_box));
5170 return WINED3DERR_INVALIDCALL;
5173 if (src_resource->format->block_width == dst_resource->format->block_width
5174 && src_resource->format->block_height == dst_resource->format->block_height)
5176 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
5177 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
5179 else
5181 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
5182 / src_resource->format->block_width;
5183 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
5184 / src_resource->format->block_height;
5185 wined3d_box_set(&dst_box, dst_x, dst_y,
5186 dst_x + (src_row_block_count * dst_resource->format->block_width),
5187 dst_y + (src_row_count * dst_resource->format->block_height),
5188 dst_z, dst_z + (src_box->back - src_box->front));
5190 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
5192 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
5193 return WINED3DERR_INVALIDCALL;
5197 wined3d_device_context_lock(context);
5198 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
5199 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5200 wined3d_device_context_unlock(context);
5202 return WINED3D_OK;
5205 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
5206 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
5207 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
5209 struct wined3d_sub_resource_desc desc;
5210 struct wined3d_box b;
5212 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
5213 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
5215 if (flags)
5216 FIXME("Ignoring flags %#x.\n", flags);
5218 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5220 WARN("Resource %p is not GPU accessible.\n", resource);
5221 return;
5224 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
5225 return;
5227 if (!box)
5229 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
5230 box = &b;
5232 else if (box->left >= box->right || box->right > desc.width
5233 || box->top >= box->bottom || box->bottom > desc.height
5234 || box->front >= box->back || box->back > desc.depth)
5236 WARN("Invalid box %s specified.\n", debug_box(box));
5237 return;
5240 wined3d_device_context_lock(context);
5241 wined3d_device_context_emit_update_sub_resource(context, resource,
5242 sub_resource_idx, box, data, row_pitch, depth_pitch);
5243 wined3d_device_context_unlock(context);
5246 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
5247 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
5248 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
5249 enum wined3d_format_id format_id)
5251 struct wined3d_texture *dst_texture, *src_texture;
5252 unsigned int dst_level, src_level;
5253 struct wined3d_blt_fx fx = {0};
5254 RECT dst_rect, src_rect;
5256 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
5257 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
5258 context, dst_resource, dst_sub_resource_idx,
5259 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
5261 if (wined3d_format_is_typeless(dst_resource->format)
5262 || wined3d_format_is_typeless(src_resource->format))
5264 FIXME("Multisample resolve is not fully supported for typeless formats "
5265 "(dst_format %s, src_format %s, format %s).\n",
5266 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
5267 debug_d3dformat(format_id));
5269 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5271 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
5272 return;
5274 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5276 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
5277 return;
5280 wined3d_device_context_lock(context);
5281 fx.resolve_format_id = format_id;
5283 dst_texture = texture_from_resource(dst_resource);
5284 src_texture = texture_from_resource(src_resource);
5286 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5287 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
5288 wined3d_texture_get_level_height(dst_texture, dst_level));
5289 src_level = src_sub_resource_idx % src_texture->level_count;
5290 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
5291 wined3d_texture_get_level_height(src_texture, src_level));
5292 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
5293 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
5294 wined3d_device_context_unlock(context);
5297 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
5298 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
5299 const struct wined3d_color *color, float depth, unsigned int stencil)
5301 struct wined3d_resource *resource;
5302 RECT r;
5304 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
5305 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
5307 if (!flags)
5308 return WINED3D_OK;
5310 resource = view->resource;
5311 if (resource->type == WINED3D_RTYPE_BUFFER)
5313 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
5314 return WINED3DERR_INVALIDCALL;
5317 if (!rect)
5319 SetRect(&r, 0, 0, view->width, view->height);
5320 rect = &r;
5322 else
5324 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
5325 HRESULT hr;
5327 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
5328 return hr;
5331 wined3d_device_context_lock(context);
5332 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
5333 wined3d_device_context_unlock(context);
5335 return WINED3D_OK;
5338 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
5339 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
5341 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
5343 if (!(view->format->attrs & (WINED3D_FORMAT_ATTR_FLOAT | WINED3D_FORMAT_ATTR_NORMALISED)))
5345 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
5346 return;
5349 wined3d_device_context_lock(context);
5350 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
5351 wined3d_device_context_unlock(context);
5354 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
5355 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
5357 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
5359 wined3d_device_context_lock(context);
5360 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
5361 wined3d_device_context_unlock(context);
5364 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
5366 /* Not all flags make sense together, but Windows never returns an error.
5367 * Catch the cases that could cause issues. */
5368 if (flags & WINED3D_MAP_READ)
5370 if (flags & WINED3D_MAP_DISCARD)
5372 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
5373 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5375 if (flags & WINED3D_MAP_NOOVERWRITE)
5377 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
5378 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5381 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5383 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU) || !(resource->usage & WINED3DUSAGE_DYNAMIC))
5385 WARN("DISCARD or NOOVERWRITE map not on dynamic GPU-accessible buffer, ignoring.\n");
5386 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5388 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5389 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5391 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
5392 flags &= ~WINED3D_MAP_DISCARD;
5396 return flags;
5399 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
5400 struct wined3d_resource *resource, unsigned int sub_resource_idx,
5401 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
5403 struct wined3d_sub_resource_desc desc;
5404 struct wined3d_box b;
5405 HRESULT hr;
5407 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
5408 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
5410 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
5412 WARN("No read/write flags specified.\n");
5413 return E_INVALIDARG;
5416 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
5418 WARN("Resource does not have MAP_R access.\n");
5419 return E_INVALIDARG;
5422 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
5424 WARN("Resource does not have MAP_W access.\n");
5425 return E_INVALIDARG;
5428 flags = sanitise_map_flags(resource, flags);
5430 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
5431 return E_INVALIDARG;
5433 if (!box)
5435 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
5436 box = &b;
5438 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
5440 WARN("Map box is invalid.\n");
5442 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
5443 return WINED3DERR_INVALIDCALL;
5445 if ((resource->format_attrs & WINED3D_FORMAT_ATTR_BLOCKS) &&
5446 !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
5447 return WINED3DERR_INVALIDCALL;
5450 wined3d_device_context_lock(context);
5451 hr = wined3d_device_context_emit_map(context, resource, sub_resource_idx, map_desc, box, flags);
5452 wined3d_device_context_unlock(context);
5453 return hr;
5456 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
5457 struct wined3d_resource *resource, unsigned int sub_resource_idx)
5459 HRESULT hr;
5460 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
5462 wined3d_device_context_lock(context);
5463 hr = wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
5464 wined3d_device_context_unlock(context);
5465 return hr;
5468 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
5469 struct wined3d_query *query, unsigned int flags)
5471 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
5473 wined3d_device_context_lock(context);
5474 context->ops->issue_query(context, query, flags);
5475 wined3d_device_context_unlock(context);
5478 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
5479 struct wined3d_command_list *list, bool restore_state)
5481 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
5483 wined3d_device_context_lock(context);
5484 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
5485 wined3d_device_context_unlock(context);
5488 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
5489 const struct wined3d_device_context *context, unsigned int view_idx)
5491 unsigned int max_rt_count;
5493 TRACE("context %p, view_idx %u.\n", context, view_idx);
5495 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
5496 if (view_idx >= max_rt_count)
5498 WARN("Only %u render targets are supported.\n", max_rt_count);
5499 return NULL;
5502 return context->state->fb.render_targets[view_idx];
5505 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
5506 const struct wined3d_device_context *context)
5508 TRACE("context %p.\n", context);
5510 return context->state->fb.depth_stencil;
5513 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
5514 struct wined3d_shader_resource_view *view)
5516 struct wined3d_texture *texture;
5518 TRACE("context %p, view %p.\n", context, view);
5520 if (view->resource->type == WINED3D_RTYPE_BUFFER)
5522 WARN("Called on buffer resource %p.\n", view->resource);
5523 return;
5526 texture = texture_from_resource(view->resource);
5527 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
5529 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
5530 return;
5533 wined3d_device_context_lock(context);
5534 wined3d_device_context_emit_generate_mipmaps(context, view);
5535 wined3d_device_context_unlock(context);
5538 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5539 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5541 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5542 struct wined3d_sub_resource_data data;
5543 struct wined3d_resource_desc desc;
5544 struct wined3d_map_desc map_desc;
5545 struct wined3d_texture *texture;
5546 HRESULT hr;
5548 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5550 ERR("Failed to map source texture.\n");
5551 return NULL;
5554 data.data = map_desc.data;
5555 data.row_pitch = map_desc.row_pitch;
5556 data.slice_pitch = map_desc.slice_pitch;
5558 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5559 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5560 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5561 desc.multisample_quality = 0;
5562 desc.usage = WINED3DUSAGE_DYNAMIC;
5563 desc.bind_flags = 0;
5564 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5565 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5566 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5567 desc.depth = 1;
5568 desc.size = 0;
5570 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5571 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5572 if (FAILED(hr))
5574 ERR("Failed to create cursor texture.\n");
5575 return NULL;
5578 return texture;
5581 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5582 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5584 unsigned int texture_level = sub_resource_idx % texture->level_count;
5585 unsigned int cursor_width, cursor_height;
5586 struct wined3d_map_desc map_desc;
5588 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5589 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5591 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx)
5592 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5593 return WINED3DERR_INVALIDCALL;
5595 if (device->cursor_texture)
5597 wined3d_texture_decref(device->cursor_texture);
5598 device->cursor_texture = NULL;
5601 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5603 WARN("Texture %p has invalid format %s.\n",
5604 texture, debug_d3dformat(texture->resource.format->id));
5605 return WINED3DERR_INVALIDCALL;
5608 /* Cursor width and height must all be powers of two */
5609 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5610 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5611 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5613 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5614 return WINED3DERR_INVALIDCALL;
5617 /* Do not store the surface's pointer because the application may
5618 * release it after setting the cursor image. Windows doesn't
5619 * addref the set surface, so we can't do this either without
5620 * creating circular refcount dependencies. */
5621 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5623 ERR("Failed to create cursor texture.\n");
5624 return WINED3DERR_INVALIDCALL;
5627 if (cursor_width == 32 && cursor_height == 32)
5629 UINT mask_size = cursor_width * cursor_height / 8;
5630 ICONINFO cursor_info;
5631 DWORD *mask_bits;
5632 HCURSOR cursor;
5634 /* 32-bit user32 cursors ignore the alpha channel if it's all
5635 * zeroes, and use the mask instead. Fill the mask with all ones
5636 * to ensure we still get a fully transparent cursor. */
5637 if (!(mask_bits = heap_alloc(mask_size)))
5638 return E_OUTOFMEMORY;
5639 memset(mask_bits, 0xff, mask_size);
5641 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5642 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5643 cursor_info.fIcon = FALSE;
5644 cursor_info.xHotspot = x_hotspot;
5645 cursor_info.yHotspot = y_hotspot;
5646 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5647 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5648 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5650 /* Create our cursor and clean up. */
5651 cursor = CreateIconIndirect(&cursor_info);
5652 if (cursor_info.hbmMask)
5653 DeleteObject(cursor_info.hbmMask);
5654 if (cursor_info.hbmColor)
5655 DeleteObject(cursor_info.hbmColor);
5656 if (device->hardwareCursor)
5657 DestroyCursor(device->hardwareCursor);
5658 device->hardwareCursor = cursor;
5659 if (device->bCursorVisible)
5660 SetCursor(cursor);
5662 heap_free(mask_bits);
5665 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5666 device->cursorWidth = cursor_width;
5667 device->cursorHeight = cursor_height;
5668 device->xHotSpot = x_hotspot;
5669 device->yHotSpot = y_hotspot;
5671 return WINED3D_OK;
5674 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5675 int x_screen_space, int y_screen_space, uint32_t flags)
5677 TRACE("device %p, x %d, y %d, flags %#x.\n",
5678 device, x_screen_space, y_screen_space, flags);
5680 device->xScreenSpace = x_screen_space;
5681 device->yScreenSpace = y_screen_space;
5683 if (device->hardwareCursor)
5685 POINT pt;
5687 GetCursorPos( &pt );
5688 if (x_screen_space == pt.x && y_screen_space == pt.y)
5689 return;
5690 SetCursorPos( x_screen_space, y_screen_space );
5692 /* Switch to the software cursor if position diverges from the hardware one. */
5693 GetCursorPos( &pt );
5694 if (x_screen_space != pt.x || y_screen_space != pt.y)
5696 if (device->bCursorVisible) SetCursor( NULL );
5697 DestroyCursor( device->hardwareCursor );
5698 device->hardwareCursor = 0;
5703 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5705 BOOL oldVisible = device->bCursorVisible;
5707 TRACE("device %p, show %#x.\n", device, show);
5710 * When ShowCursor is first called it should make the cursor appear at the OS's last
5711 * known cursor position.
5713 if (show && !oldVisible)
5715 POINT pt;
5716 GetCursorPos(&pt);
5717 device->xScreenSpace = pt.x;
5718 device->yScreenSpace = pt.y;
5721 if (device->hardwareCursor)
5723 device->bCursorVisible = show;
5724 if (show)
5725 SetCursor(device->hardwareCursor);
5726 else
5727 SetCursor(NULL);
5729 else if (device->cursor_texture)
5731 device->bCursorVisible = show;
5734 return oldVisible;
5737 static void mark_managed_resource_dirty(struct wined3d_resource *resource)
5739 if (resource->type != WINED3D_RTYPE_BUFFER)
5741 struct wined3d_texture *texture = texture_from_resource(resource);
5742 unsigned int i;
5744 if (texture->dirty_regions)
5746 for (i = 0; i < texture->layer_count; ++i)
5747 wined3d_texture_add_dirty_region(texture, i, NULL);
5752 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5754 struct wined3d_resource *resource, *cursor;
5756 TRACE("device %p.\n", device);
5758 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5760 TRACE("Checking resource %p for eviction.\n", resource);
5762 if ((resource->usage & WINED3DUSAGE_MANAGED) && !resource->map_count)
5764 if (resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5766 TRACE("Evicting %p.\n", resource);
5767 wined3d_cs_emit_unload_resource(device->cs, resource);
5770 mark_managed_resource_dirty(resource);
5775 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5777 TRACE("context %p.\n", context);
5779 wined3d_device_context_lock(context);
5780 context->ops->flush(context);
5781 wined3d_device_context_unlock(context);
5784 static void update_swapchain_flags(struct wined3d_texture *texture)
5786 unsigned int flags = texture->swapchain->state.desc.flags;
5788 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5789 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5790 else
5791 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5793 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5794 texture->flags |= WINED3D_TEXTURE_GET_DC;
5795 else
5796 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5799 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5800 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5801 wined3d_device_reset_cb callback, BOOL reset_state)
5803 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
5804 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5805 struct wined3d_device_context *context = &device->cs->c;
5806 struct wined3d_swapchain_state *swapchain_state;
5807 struct wined3d_state *state = context->state;
5808 struct wined3d_swapchain_desc *current_desc;
5809 struct wined3d_resource *resource, *cursor;
5810 struct wined3d_rendertarget_view *view;
5811 struct wined3d_swapchain *swapchain;
5812 struct wined3d_view_desc view_desc;
5813 BOOL backbuffer_resized, windowed;
5814 HRESULT hr = WINED3D_OK;
5815 HWND device_window;
5816 unsigned int i;
5818 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5819 device, swapchain_desc, mode, callback, reset_state);
5821 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5823 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5825 ERR("Failed to get the first implicit swapchain.\n");
5826 return WINED3DERR_INVALIDCALL;
5828 swapchain_state = &swapchain->state;
5829 current_desc = &swapchain_state->desc;
5831 if (reset_state)
5833 if (device->logo_texture)
5835 wined3d_texture_decref(device->logo_texture);
5836 device->logo_texture = NULL;
5838 if (device->cursor_texture)
5840 wined3d_texture_decref(device->cursor_texture);
5841 device->cursor_texture = NULL;
5843 state_unbind_resources(state);
5846 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
5847 wined3d_device_context_set_depth_stencil_view(context, NULL);
5849 if (reset_state)
5851 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5853 TRACE("Enumerating resource %p.\n", resource);
5854 if (FAILED(hr = callback(resource)))
5855 return hr;
5859 TRACE("New params:\n");
5860 TRACE("output %p\n", swapchain_desc->output);
5861 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5862 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5863 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5864 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5865 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5866 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5867 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5868 TRACE("device_window %p\n", swapchain_desc->device_window);
5869 TRACE("windowed %#x\n", swapchain_desc->windowed);
5870 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5871 if (swapchain_desc->enable_auto_depth_stencil)
5872 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5873 TRACE("flags %#x\n", swapchain_desc->flags);
5874 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5875 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5877 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5878 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5880 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5881 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5882 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5883 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5885 /* No special treatment of these parameters. Just store them */
5886 current_desc->swap_effect = swapchain_desc->swap_effect;
5887 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5888 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5889 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5890 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5892 device_window = swapchain_desc->device_window ? swapchain_desc->device_window : device->create_parms.focus_window;
5893 if (device_window && device_window != current_desc->device_window)
5895 TRACE("Changing the device window from %p to %p.\n",
5896 current_desc->device_window, device_window);
5897 current_desc->device_window = device_window;
5898 swapchain_state->device_window = device_window;
5899 wined3d_swapchain_set_window(swapchain, NULL);
5902 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5903 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5904 windowed = current_desc->windowed;
5906 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5907 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5909 /* Switch from windowed to fullscreen. */
5910 if (windowed && !swapchain_desc->windowed)
5912 HWND focus_window = device->create_parms.focus_window;
5914 if (!focus_window)
5915 focus_window = swapchain->state.device_window;
5916 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5918 ERR("Failed to acquire focus window, hr %#lx.\n", hr);
5919 return hr;
5923 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5924 swapchain_desc, mode)))
5925 return hr;
5927 /* Switch from fullscreen to windowed. */
5928 if (!windowed && swapchain_desc->windowed)
5929 wined3d_device_release_focus_window(device);
5931 else if (!swapchain_desc->windowed)
5933 DWORD style = swapchain_state->style;
5934 DWORD exstyle = swapchain_state->exstyle;
5935 struct wined3d_output_desc output_desc;
5937 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5938 * the window back into the right position. Some applications
5939 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5940 * up their mess. Guild Wars also loses the device during that. */
5941 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5943 ERR("Failed to get output description, hr %#lx.\n", hr);
5944 return hr;
5947 swapchain_state->style = 0;
5948 swapchain_state->exstyle = 0;
5949 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5950 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5951 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5952 swapchain_state->style = style;
5953 swapchain_state->exstyle = exstyle;
5956 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5957 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5958 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5959 return hr;
5961 if (swapchain_desc->flags != current_desc->flags)
5963 current_desc->flags = swapchain_desc->flags;
5965 update_swapchain_flags(swapchain->front_buffer);
5966 for (i = 0; i < current_desc->backbuffer_count; ++i)
5968 update_swapchain_flags(swapchain->back_buffers[i]);
5972 if ((view = device->auto_depth_stencil_view))
5974 device->auto_depth_stencil_view = NULL;
5975 wined3d_rendertarget_view_decref(view);
5977 if (current_desc->enable_auto_depth_stencil)
5979 struct wined3d_resource_desc texture_desc;
5980 struct wined3d_texture *texture;
5982 TRACE("Creating the depth stencil buffer.\n");
5984 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5985 texture_desc.format = current_desc->auto_depth_stencil_format;
5986 texture_desc.multisample_type = current_desc->multisample_type;
5987 texture_desc.multisample_quality = current_desc->multisample_quality;
5988 texture_desc.usage = 0;
5989 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5990 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5991 texture_desc.width = current_desc->backbuffer_width;
5992 texture_desc.height = current_desc->backbuffer_height;
5993 texture_desc.depth = 1;
5994 texture_desc.size = 0;
5996 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5997 device->device_parent, &texture_desc, 0, &texture)))
5999 ERR("Failed to create the auto depth/stencil surface, hr %#lx.\n", hr);
6000 return WINED3DERR_INVALIDCALL;
6003 view_desc.format_id = texture->resource.format->id;
6004 view_desc.flags = 0;
6005 view_desc.u.texture.level_idx = 0;
6006 view_desc.u.texture.level_count = 1;
6007 view_desc.u.texture.layer_idx = 0;
6008 view_desc.u.texture.layer_count = 1;
6009 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
6010 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
6011 wined3d_texture_decref(texture);
6012 if (FAILED(hr))
6014 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
6015 return hr;
6019 if ((view = device->back_buffer_view))
6021 device->back_buffer_view = NULL;
6022 wined3d_rendertarget_view_decref(view);
6024 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
6026 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
6028 view_desc.format_id = back_buffer->format->id;
6029 view_desc.flags = 0;
6030 view_desc.u.texture.level_idx = 0;
6031 view_desc.u.texture.level_count = 1;
6032 view_desc.u.texture.layer_idx = 0;
6033 view_desc.u.texture.layer_count = 1;
6034 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
6035 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
6037 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
6038 return hr;
6042 wine_rb_destroy(&device->samplers, device_free_sampler, NULL);
6043 wine_rb_destroy(&device->rasterizer_states, device_free_rasterizer_state, NULL);
6044 wine_rb_destroy(&device->blend_states, device_free_blend_state, NULL);
6045 wine_rb_destroy(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
6047 if (reset_state)
6049 TRACE("Resetting state.\n");
6050 wined3d_device_context_emit_reset_state(&device->cs->c, false);
6051 state_cleanup(state);
6053 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
6055 TRACE("Unloading resource %p.\n", resource);
6056 wined3d_cs_emit_unload_resource(device->cs, resource);
6058 if (resource->usage & WINED3DUSAGE_MANAGED)
6059 mark_managed_resource_dirty(resource);
6062 device->adapter->adapter_ops->adapter_uninit_3d(device);
6064 wined3d_state_reset(state, &device->adapter->d3d_info);
6066 device_init_swapchain_state(device, swapchain);
6067 if (wined3d_settings.logo)
6068 device_load_logo(device, wined3d_settings.logo);
6070 hr = device->adapter->adapter_ops->adapter_init_3d(device);
6072 else
6074 if ((view = device->back_buffer_view))
6075 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
6076 if ((view = device->auto_depth_stencil_view))
6077 wined3d_device_context_set_depth_stencil_view(context, view);
6080 /* All done. There is no need to reload resources or shaders, this will
6081 * happen automatically on the first use. */
6082 return hr;
6085 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
6087 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
6089 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
6091 return WINED3D_OK;
6095 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
6096 struct wined3d_device_creation_parameters *parameters)
6098 TRACE("device %p, parameters %p.\n", device, parameters);
6100 *parameters = device->create_parms;
6103 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
6105 TRACE("device %p.\n", device);
6107 return device->wined3d;
6110 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
6111 UINT swapchain_idx, uint32_t flags, const struct wined3d_gamma_ramp *ramp)
6113 struct wined3d_swapchain *swapchain;
6115 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
6116 device, swapchain_idx, flags, ramp);
6118 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
6119 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
6122 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
6123 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
6125 struct wined3d_swapchain *swapchain;
6127 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
6128 device, swapchain_idx, ramp);
6130 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
6131 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
6134 HDC wined3d_device_gl_get_backup_dc(struct wined3d_device_gl *device_gl)
6136 TRACE("device_gl %p.\n", device_gl);
6138 if (!device_gl->backup_dc)
6140 TRACE("Creating the backup window for device %p.\n", device_gl);
6142 if (!(device_gl->backup_wnd = CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME, "WineD3D fake window",
6143 WS_OVERLAPPEDWINDOW, 10, 10, 10, 10, NULL, NULL, NULL, NULL)))
6145 ERR("Failed to create a window.\n");
6146 return NULL;
6149 if (!(device_gl->backup_dc = GetDC(device_gl->backup_wnd)))
6151 ERR("Failed to get a DC.\n");
6152 DestroyWindow(device_gl->backup_wnd);
6153 device_gl->backup_wnd = NULL;
6154 return NULL;
6158 return device_gl->backup_dc;
6161 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
6163 TRACE("device %p, resource %p.\n", device, resource);
6165 wined3d_not_from_cs(device->cs);
6167 list_add_head(&device->resources, &resource->resource_list_entry);
6170 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
6172 TRACE("device %p, resource %p.\n", device, resource);
6174 wined3d_not_from_cs(device->cs);
6176 list_remove(&resource->resource_list_entry);
6179 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
6181 enum wined3d_resource_type type = resource->type;
6182 struct wined3d_state *state = device->cs->c.state;
6183 struct wined3d_rendertarget_view *rtv;
6184 unsigned int i;
6186 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
6188 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
6190 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
6191 ERR("Resource %p is still in use as render target %u.\n", resource, i);
6194 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
6195 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
6197 switch (type)
6199 case WINED3D_RTYPE_TEXTURE_1D:
6200 case WINED3D_RTYPE_TEXTURE_2D:
6201 case WINED3D_RTYPE_TEXTURE_3D:
6202 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
6204 if (&state->textures[i]->resource == resource)
6206 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
6207 state->textures[i] = NULL;
6210 break;
6212 case WINED3D_RTYPE_BUFFER:
6213 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
6215 if (&state->streams[i].buffer->resource == resource)
6217 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
6218 state->streams[i].buffer = NULL;
6222 if (&state->index_buffer->resource == resource)
6224 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
6225 state->index_buffer = NULL;
6227 break;
6229 default:
6230 break;
6233 /* Remove the resource from the resourceStore */
6234 device_resource_remove(device, resource);
6236 TRACE("Resource released.\n");
6239 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
6241 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
6242 struct wined3d_so_desc_entry, entry)->desc;
6243 const struct wined3d_stream_output_desc *k = key;
6244 unsigned int i;
6245 int ret;
6247 if ((ret = wined3d_uint32_compare(k->element_count, desc->element_count)))
6248 return ret;
6249 if ((ret = wined3d_uint32_compare(k->buffer_stride_count, desc->buffer_stride_count)))
6250 return ret;
6251 if ((ret = wined3d_uint32_compare(k->rasterizer_stream_idx, desc->rasterizer_stream_idx)))
6252 return ret;
6254 for (i = 0; i < k->element_count; ++i)
6256 const struct wined3d_stream_output_element *b = &desc->elements[i];
6257 const struct wined3d_stream_output_element *a = &k->elements[i];
6259 if ((ret = wined3d_uint32_compare(a->stream_idx, b->stream_idx)))
6260 return ret;
6261 if ((ret = (!a->semantic_name - !b->semantic_name)))
6262 return ret;
6263 if (a->semantic_name && (ret = strcmp(a->semantic_name, b->semantic_name)))
6264 return ret;
6265 if ((ret = wined3d_uint32_compare(a->semantic_idx, b->semantic_idx)))
6266 return ret;
6267 if ((ret = wined3d_uint32_compare(a->component_idx, b->component_idx)))
6268 return ret;
6269 if ((ret = wined3d_uint32_compare(a->component_count, b->component_count)))
6270 return ret;
6271 if ((ret = wined3d_uint32_compare(a->output_slot, b->output_slot)))
6272 return ret;
6275 for (i = 0; i < k->buffer_stride_count; ++i)
6277 if ((ret = wined3d_uint32_compare(k->buffer_strides[i], desc->buffer_strides[i])))
6278 return ret;
6281 return 0;
6284 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
6286 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
6288 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
6291 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
6293 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
6295 return memcmp(&state->desc, key, sizeof(state->desc));
6298 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
6300 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
6302 return memcmp(&state->desc, key, sizeof(state->desc));
6305 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
6307 const struct wined3d_depth_stencil_state *state
6308 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
6310 return memcmp(&state->desc, key, sizeof(state->desc));
6313 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
6314 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
6315 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
6316 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
6318 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
6319 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
6320 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
6321 unsigned int i;
6322 HRESULT hr;
6324 device->ref = 1;
6325 device->wined3d = wined3d;
6326 wined3d_incref(device->wined3d);
6327 device->adapter = adapter;
6328 device->device_parent = device_parent;
6329 list_init(&device->resources);
6330 list_init(&device->shaders);
6331 device->surface_alignment = surface_alignment;
6333 /* Save the creation parameters. */
6334 device->create_parms.adapter_idx = adapter_idx;
6335 device->create_parms.device_type = device_type;
6336 device->create_parms.focus_window = focus_window;
6337 device->create_parms.flags = flags;
6339 device->shader_backend = adapter->shader_backend;
6341 vertex_pipeline = adapter->vertex_pipe;
6343 fragment_pipeline = adapter->fragment_pipe;
6345 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
6346 wine_rb_init(&device->samplers, wined3d_sampler_compare);
6347 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
6348 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
6349 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
6351 if (vertex_pipeline->vp_states && fragment_pipeline->states
6352 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
6353 &adapter->d3d_info, supported_extensions, vertex_pipeline,
6354 fragment_pipeline, adapter->misc_state_template)))
6356 ERR("Failed to compile state table, hr %#lx.\n", hr);
6357 wine_rb_destroy(&device->samplers, NULL, NULL);
6358 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6359 wine_rb_destroy(&device->blend_states, NULL, NULL);
6360 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6361 wine_rb_destroy(&device->so_descs, NULL, NULL);
6362 wined3d_decref(device->wined3d);
6363 return hr;
6366 device->max_frame_latency = 3;
6368 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
6370 WARN("Failed to create command stream.\n");
6371 hr = E_FAIL;
6372 goto err;
6375 wined3d_lock_init(&device->bo_map_lock, "wined3d_device.bo_map_lock");
6377 return WINED3D_OK;
6379 err:
6380 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
6382 heap_free(device->multistate_funcs[i]);
6384 wine_rb_destroy(&device->samplers, NULL, NULL);
6385 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6386 wine_rb_destroy(&device->blend_states, NULL, NULL);
6387 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6388 wine_rb_destroy(&device->so_descs, NULL, NULL);
6389 wined3d_decref(device->wined3d);
6390 return hr;
6393 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
6395 unsigned int representative, i, idx, shift;
6396 struct wined3d_context *context;
6398 wined3d_from_cs(device->cs);
6400 if (STATE_IS_COMPUTE(state_id))
6402 for (i = 0; i < device->context_count; ++i)
6403 context_invalidate_compute_state(device->contexts[i], state_id);
6404 return;
6407 representative = device->state_table[state_id].representative;
6408 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
6409 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
6410 for (i = 0; i < device->context_count; ++i)
6412 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
6416 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
6417 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
6419 if (message == WM_DESTROY)
6421 TRACE("unregister window %p.\n", window);
6422 wined3d_unregister_window(window);
6424 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
6425 ERR("Window %p is not the focus window for device %p.\n", window, device);
6427 else if (message == WM_DISPLAYCHANGE)
6429 device->device_parent->ops->mode_changed(device->device_parent);
6431 else if (message == WM_ACTIVATEAPP)
6433 unsigned int i = device->swapchain_count;
6435 /* Deactivating the implicit swapchain may cause the application
6436 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
6437 * deactivate the implicit swapchain last, and to avoid accessing the
6438 * "device" pointer afterwards. */
6439 while (i--)
6440 wined3d_swapchain_activate(device->swapchains[i], wparam);
6442 else if (message == WM_SYSCOMMAND)
6444 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
6446 if (unicode)
6447 DefWindowProcW(window, message, wparam, lparam);
6448 else
6449 DefWindowProcA(window, message, wparam, lparam);
6453 if (unicode)
6454 return CallWindowProcW(proc, window, message, wparam, lparam);
6455 else
6456 return CallWindowProcA(proc, window, message, wparam, lparam);