Release 6.15.
[wine.git] / dlls / wined3d / device.c
blobd2147447ee0ea2923b775538a2e69ccaca060193
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #ifdef HAVE_FLOAT_H
31 # include <float.h>
32 #endif
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 struct wined3d_matrix_3x3
41 float _11, _12, _13;
42 float _21, _22, _23;
43 float _31, _32, _33;
46 struct light_transformed
48 struct wined3d_color diffuse, specular, ambient;
49 struct wined3d_vec4 position;
50 struct wined3d_vec3 direction;
51 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
54 struct lights_settings
56 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
57 struct wined3d_color ambient_light;
58 struct wined3d_matrix modelview_matrix;
59 struct wined3d_matrix_3x3 normal_matrix;
60 struct wined3d_vec4 position_transformed;
62 float fog_start, fog_end, fog_density;
64 uint32_t point_light_count : 8;
65 uint32_t spot_light_count : 8;
66 uint32_t directional_light_count : 8;
67 uint32_t parallel_point_light_count : 8;
68 uint32_t lighting : 1;
69 uint32_t legacy_lighting : 1;
70 uint32_t normalise : 1;
71 uint32_t localviewer : 1;
72 uint32_t fog_coord_mode : 2;
73 uint32_t fog_mode : 2;
74 uint32_t padding : 24;
77 /* Define the default light parameters as specified by MSDN. */
78 const struct wined3d_light WINED3D_default_light =
80 WINED3D_LIGHT_DIRECTIONAL, /* Type */
81 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
82 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
83 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
84 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
85 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
86 0.0f, /* Range */
87 0.0f, /* Falloff */
88 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
89 0.0f, /* Theta */
90 0.0f /* Phi */
93 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
95 struct wined3d_context **new_array;
97 TRACE("Adding context %p.\n", context);
99 if (!device->shader_backend->shader_allocate_context_data(context))
101 ERR("Failed to allocate shader backend context data.\n");
102 return FALSE;
104 device->shader_backend->shader_init_context_state(context);
106 if (!device->adapter->fragment_pipe->allocate_context_data(context))
108 ERR("Failed to allocate fragment pipeline context data.\n");
109 device->shader_backend->shader_free_context_data(context);
110 return FALSE;
113 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
115 ERR("Failed to grow the context array.\n");
116 device->adapter->fragment_pipe->free_context_data(context);
117 device->shader_backend->shader_free_context_data(context);
118 return FALSE;
121 new_array[device->context_count++] = context;
122 device->contexts = new_array;
124 return TRUE;
127 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
129 struct wined3d_context **new_array;
130 BOOL found = FALSE;
131 UINT i;
133 TRACE("Removing context %p.\n", context);
135 device->adapter->fragment_pipe->free_context_data(context);
136 device->shader_backend->shader_free_context_data(context);
138 for (i = 0; i < device->context_count; ++i)
140 if (device->contexts[i] == context)
142 found = TRUE;
143 break;
147 if (!found)
149 ERR("Context %p doesn't exist in context array.\n", context);
150 return;
153 if (!--device->context_count)
155 heap_free(device->contexts);
156 device->contexts = NULL;
157 return;
160 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
161 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
163 ERR("Failed to shrink context array. Oh well.\n");
164 return;
167 device->contexts = new_array;
170 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
172 ULONG refcount = InterlockedIncrement(&device->ref);
174 TRACE("%p increasing refcount to %u.\n", device, refcount);
176 return refcount;
179 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
181 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
183 heap_free(s);
186 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
188 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
190 ERR("Leftover sampler %p.\n", sampler);
193 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
195 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
197 ERR("Leftover rasterizer state %p.\n", state);
200 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
202 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
204 ERR("Leftover blend state %p.\n", blend_state);
207 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
209 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
211 ERR("Leftover depth/stencil state %p.\n", state);
214 void wined3d_device_cleanup(struct wined3d_device *device)
216 unsigned int i;
218 if (device->swapchain_count)
219 wined3d_device_uninit_3d(device);
221 wined3d_cs_destroy(device->cs);
223 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
225 heap_free(device->multistate_funcs[i]);
226 device->multistate_funcs[i] = NULL;
229 if (!list_empty(&device->resources))
231 struct wined3d_resource *resource;
233 ERR("Device released with resources still bound.\n");
235 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
237 ERR("Leftover resource %p with type %s (%#x).\n",
238 resource, debug_d3dresourcetype(resource->type), resource->type);
242 if (device->contexts)
243 ERR("Context array not freed!\n");
244 if (device->hardwareCursor)
245 DestroyCursor(device->hardwareCursor);
246 device->hardwareCursor = 0;
248 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
249 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
250 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
251 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
252 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
254 wined3d_decref(device->wined3d);
255 device->wined3d = NULL;
258 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
260 ULONG refcount = InterlockedDecrement(&device->ref);
262 TRACE("%p decreasing refcount to %u.\n", device, refcount);
264 if (!refcount)
266 device->adapter->adapter_ops->adapter_destroy_device(device);
267 TRACE("Destroyed device %p.\n", device);
270 return refcount;
273 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
275 TRACE("device %p.\n", device);
277 return device->swapchain_count;
280 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
282 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
284 if (swapchain_idx >= device->swapchain_count)
286 WARN("swapchain_idx %u >= swapchain_count %u.\n",
287 swapchain_idx, device->swapchain_count);
288 return NULL;
291 return device->swapchains[swapchain_idx];
294 static void device_load_logo(struct wined3d_device *device, const char *filename)
296 struct wined3d_color_key color_key;
297 struct wined3d_resource_desc desc;
298 HBITMAP hbm;
299 BITMAP bm;
300 HRESULT hr;
301 HDC dcb = NULL, dcs = NULL;
303 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
305 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
306 return;
308 GetObjectA(hbm, sizeof(BITMAP), &bm);
310 if (!(dcb = CreateCompatibleDC(NULL)))
311 goto out;
312 SelectObject(dcb, hbm);
314 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
315 desc.format = WINED3DFMT_B5G6R5_UNORM;
316 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
317 desc.multisample_quality = 0;
318 desc.usage = WINED3DUSAGE_DYNAMIC;
319 desc.bind_flags = 0;
320 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
321 desc.width = bm.bmWidth;
322 desc.height = bm.bmHeight;
323 desc.depth = 1;
324 desc.size = 0;
325 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
326 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
328 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
329 goto out;
332 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
334 wined3d_texture_decref(device->logo_texture);
335 device->logo_texture = NULL;
336 goto out;
338 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
339 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
341 color_key.color_space_low_value = 0;
342 color_key.color_space_high_value = 0;
343 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
345 out:
346 if (dcb) DeleteDC(dcb);
347 if (hbm) DeleteObject(hbm);
350 /* Context activation is done by the caller. */
351 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
352 struct wined3d_context_gl *context_gl)
354 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
355 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
356 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
357 unsigned int i;
358 DWORD color;
360 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
361 color = 0x000000ff;
362 else
363 color = 0x00000000;
365 /* Under DirectX you can sample even if no texture is bound, whereas
366 * OpenGL will only allow that when a valid texture is bound.
367 * We emulate this by creating dummy textures and binding them
368 * to each texture stage when the currently set D3D texture is NULL. */
369 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
371 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
372 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
373 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
374 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
375 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
377 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
378 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
379 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
380 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
381 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
383 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
385 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
386 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
387 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
388 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
389 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
392 if (gl_info->supported[EXT_TEXTURE3D])
394 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
395 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
396 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
397 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
398 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
401 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
403 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
404 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
405 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
406 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
408 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
409 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
413 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
415 DWORD cube_array_data[6];
417 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
418 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
419 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
420 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
421 cube_array_data[i] = color;
422 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
423 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
426 if (gl_info->supported[EXT_TEXTURE_ARRAY])
428 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
429 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
430 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
431 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
432 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
434 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
435 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
436 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
437 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
438 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
441 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
443 GLuint buffer;
445 GL_EXTCALL(glGenBuffers(1, &buffer));
446 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
447 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
448 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
450 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
451 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
452 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
453 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
454 GL_EXTCALL(glDeleteBuffers(1, &buffer));
457 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
459 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
460 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
461 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
462 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
464 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
465 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
466 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
467 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
469 if (gl_info->supported[ARB_CLEAR_TEXTURE])
471 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
472 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
474 else
476 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
480 checkGLcall("create dummy textures");
482 wined3d_context_gl_bind_dummy_textures(context_gl);
485 /* Context activation is done by the caller. */
486 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
487 struct wined3d_context_gl *context_gl)
489 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
490 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
492 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
494 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
495 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
498 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
499 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
501 if (gl_info->supported[EXT_TEXTURE_ARRAY])
503 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
504 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
507 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
508 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
510 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
511 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
513 if (gl_info->supported[EXT_TEXTURE3D])
514 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
516 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
517 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
519 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
520 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
522 checkGLcall("delete dummy textures");
524 memset(dummy_textures, 0, sizeof(*dummy_textures));
527 /* Context activation is done by the caller. */
528 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
530 struct wined3d_sampler_desc desc;
531 HRESULT hr;
533 desc.address_u = WINED3D_TADDRESS_WRAP;
534 desc.address_v = WINED3D_TADDRESS_WRAP;
535 desc.address_w = WINED3D_TADDRESS_WRAP;
536 memset(desc.border_color, 0, sizeof(desc.border_color));
537 desc.mag_filter = WINED3D_TEXF_POINT;
538 desc.min_filter = WINED3D_TEXF_POINT;
539 desc.mip_filter = WINED3D_TEXF_NONE;
540 desc.lod_bias = 0.0f;
541 desc.min_lod = -1000.0f;
542 desc.max_lod = 1000.0f;
543 desc.mip_base_level = 0;
544 desc.max_anisotropy = 1;
545 desc.compare = FALSE;
546 desc.comparison_func = WINED3D_CMP_NEVER;
547 desc.srgb_decode = TRUE;
549 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
550 * instructions allow access to resources without using samplers.
551 * In GLSL, resources are always accessed through sampler or image variables. The default
552 * sampler object is used to emulate the direct resource access when there is no sampler state
553 * to use.
555 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
557 ERR("Failed to create default sampler, hr %#x.\n", hr);
558 device->default_sampler = NULL;
561 /* In D3D10+, a NULL sampler maps to the default sampler state. */
562 desc.address_u = WINED3D_TADDRESS_CLAMP;
563 desc.address_v = WINED3D_TADDRESS_CLAMP;
564 desc.address_w = WINED3D_TADDRESS_CLAMP;
565 desc.mag_filter = WINED3D_TEXF_LINEAR;
566 desc.min_filter = WINED3D_TEXF_LINEAR;
567 desc.mip_filter = WINED3D_TEXF_LINEAR;
568 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
570 ERR("Failed to create null sampler, hr %#x.\n", hr);
571 device->null_sampler = NULL;
575 /* Context activation is done by the caller. */
576 void wined3d_device_destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
578 wined3d_sampler_decref(device->default_sampler);
579 device->default_sampler = NULL;
580 wined3d_sampler_decref(device->null_sampler);
581 device->null_sampler = NULL;
584 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
585 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
587 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
588 VkImageSubresourceRange range;
589 uint32_t flags = 0;
591 static const VkClearColorValue colour = {{0}};
593 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
594 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
596 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
597 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
599 if (!wined3d_context_vk_create_image(context_vk, type,
600 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
601 1, 1, 1, sample_count, 1, layer_count, flags, image))
603 return false;
606 wined3d_context_vk_reference_image(context_vk, image);
608 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
609 range.baseMipLevel = 0;
610 range.levelCount = 1;
611 range.baseArrayLayer = 0;
612 range.layerCount = layer_count;
614 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
615 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
616 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
618 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
619 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
621 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
622 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
623 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
625 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
626 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
628 return true;
631 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
632 struct wined3d_context_vk *context_vk)
634 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
635 const struct wined3d_vk_info *vk_info;
636 const struct wined3d_format *format;
637 VkMemoryPropertyFlags memory_type;
638 VkCommandBuffer vk_command_buffer;
639 unsigned int sample_count = 2;
640 VkBufferUsageFlags usage;
642 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
643 while (sample_count && !(sample_count & format->multisample_types))
644 sample_count <<= 1;
646 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
648 ERR("Failed to get command buffer.\n");
649 return false;
652 vk_info = context_vk->vk_info;
654 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
655 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
656 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
657 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
658 return false;
659 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.buffer_offset, r->bo.size, 0x00000000u));
660 r->buffer_info.buffer = r->bo.vk_buffer;
661 r->buffer_info.offset = r->bo.buffer_offset;
662 r->buffer_info.range = r->bo.size;
664 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
666 ERR("Failed to create 1D image.\n");
667 goto fail;
670 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
672 ERR("Failed to create 2D image.\n");
673 goto fail;
676 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
678 ERR("Failed to create 2D MSAA image.\n");
679 goto fail;
682 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
684 ERR("Failed to create 3D image.\n");
685 goto fail;
688 return true;
690 fail:
691 if (r->image_2dms.vk_image)
692 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
693 if (r->image_2d.vk_image)
694 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
695 if (r->image_1d.vk_image)
696 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
697 wined3d_context_vk_reference_bo(context_vk, &r->bo);
698 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
699 return false;
702 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
703 struct wined3d_context_vk *context_vk)
705 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
707 /* We don't track command buffer references to NULL resources. We easily
708 * could, but it doesn't seem worth it. */
709 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
710 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
711 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
712 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
713 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
714 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
715 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
716 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
717 wined3d_context_vk_reference_bo(context_vk, &r->bo);
718 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
721 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
723 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
724 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
725 VkBufferViewCreateInfo buffer_create_info;
726 const struct wined3d_vk_info *vk_info;
727 VkImageViewCreateInfo view_desc;
728 VkResult vr;
730 vk_info = context_vk->vk_info;
732 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
733 buffer_create_info.pNext = NULL;
734 buffer_create_info.flags = 0;
735 buffer_create_info.buffer = r->bo.vk_buffer;
736 buffer_create_info.format = VK_FORMAT_R32_UINT;
737 buffer_create_info.offset = r->bo.buffer_offset;
738 buffer_create_info.range = r->bo.size;
740 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
741 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
743 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
744 return false;
746 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
748 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
749 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
750 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
752 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
753 goto fail;
755 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
757 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
758 view_desc.pNext = NULL;
759 view_desc.flags = 0;
760 view_desc.image = r->image_1d.vk_image;
761 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
762 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
763 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
764 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
765 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
766 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
767 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
768 view_desc.subresourceRange.baseMipLevel = 0;
769 view_desc.subresourceRange.levelCount = 1;
770 view_desc.subresourceRange.baseArrayLayer = 0;
771 view_desc.subresourceRange.layerCount = 1;
772 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
774 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
775 goto fail;
777 v->vk_info_1d.sampler = VK_NULL_HANDLE;
778 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
779 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
781 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
782 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
784 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
785 goto fail;
787 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
788 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
789 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
791 view_desc.image = r->image_2d.vk_image;
792 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
793 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
795 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
796 goto fail;
798 v->vk_info_2d.sampler = VK_NULL_HANDLE;
799 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
800 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
802 view_desc.image = r->image_2dms.vk_image;
803 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
804 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
806 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
807 goto fail;
809 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
810 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
811 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
813 view_desc.image = r->image_3d.vk_image;
814 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
815 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
817 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
818 goto fail;
820 v->vk_info_3d.sampler = VK_NULL_HANDLE;
821 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
822 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
824 view_desc.image = r->image_2d.vk_image;
825 view_desc.subresourceRange.layerCount = 6;
826 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
827 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
829 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
830 goto fail;
832 v->vk_info_cube.sampler = VK_NULL_HANDLE;
833 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
834 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
836 view_desc.subresourceRange.layerCount = 1;
837 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
838 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
840 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
841 goto fail;
843 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
844 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
845 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
847 view_desc.image = r->image_2dms.vk_image;
848 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
849 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
851 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
852 goto fail;
854 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
855 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
856 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
858 return true;
860 fail:
861 if (v->vk_info_2d_array.imageView)
862 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
863 if (v->vk_info_cube.imageView)
864 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
865 if (v->vk_info_3d.imageView)
866 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
867 if (v->vk_info_2dms.imageView)
868 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
869 if (v->vk_info_2d.imageView)
870 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
871 if (v->vk_info_1d_array.imageView)
872 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
873 if (v->vk_info_1d.imageView)
874 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
875 if (v->vk_view_buffer_float)
876 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
877 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
878 return false;
881 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
883 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
884 uint64_t id = context_vk->current_command_buffer.id;
886 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
887 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
888 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
889 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
890 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
891 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
892 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
893 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
895 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
896 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
899 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
901 unsigned int screensaver_active;
903 TRACE("device %p, window %p.\n", device, window);
905 if (!wined3d_register_window(NULL, window, device, 0))
907 ERR("Failed to register window %p.\n", window);
908 return E_FAIL;
911 InterlockedExchangePointer((void **)&device->focus_window, window);
912 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
913 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
914 if ((device->restore_screensaver = !!screensaver_active))
915 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
917 return WINED3D_OK;
920 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
922 TRACE("device %p.\n", device);
924 if (device->focus_window) wined3d_unregister_window(device->focus_window);
925 InterlockedExchangePointer((void **)&device->focus_window, NULL);
926 if (device->restore_screensaver)
928 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
929 device->restore_screensaver = FALSE;
933 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
935 struct wined3d_rendertarget_view *views[WINED3D_MAX_RENDER_TARGETS] = {0};
936 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
937 struct wined3d_device_context *context = &device->cs->c;
939 if (device->back_buffer_view)
940 views[0] = device->back_buffer_view;
941 wined3d_device_context_set_rendertarget_views(context, 0,
942 device->adapter->d3d_info.limits.max_rt_count, views, !!device->back_buffer_view);
944 wined3d_device_context_set_depth_stencil_view(context, ds_enable ? device->auto_depth_stencil_view : NULL);
947 void wined3d_device_delete_opengl_contexts_cs(void *object)
949 struct wined3d_swapchain_gl *swapchain_gl;
950 struct wined3d_device *device = object;
951 struct wined3d_context_gl *context_gl;
952 struct wined3d_device_gl *device_gl;
953 struct wined3d_context *context;
954 struct wined3d_shader *shader;
956 TRACE("device %p.\n", device);
958 device_gl = wined3d_device_gl(device);
960 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
962 device->shader_backend->shader_destroy(shader);
965 context = context_acquire(device, NULL, 0);
966 context_gl = wined3d_context_gl(context);
967 device->blitter->ops->blitter_destroy(device->blitter, context);
968 device->shader_backend->shader_free_private(device, context);
969 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
970 wined3d_device_destroy_default_samplers(device, context);
971 context_release(context);
973 while (device->context_count)
975 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
976 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
977 else
978 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
982 void wined3d_device_create_primary_opengl_context_cs(void *object)
984 struct wined3d_device *device = object;
985 struct wined3d_context_gl *context_gl;
986 struct wined3d_swapchain *swapchain;
987 struct wined3d_context *context;
988 struct wined3d_texture *target;
989 HRESULT hr;
991 TRACE("device %p.\n", device);
993 swapchain = device->swapchains[0];
994 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
995 if (!(context = context_acquire(device, target, 0)))
997 WARN("Failed to acquire context.\n");
998 return;
1001 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1002 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1004 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1005 context_release(context);
1006 return;
1009 if (!(device->blitter = wined3d_cpu_blitter_create()))
1011 ERR("Failed to create CPU blitter.\n");
1012 device->shader_backend->shader_free_private(device, NULL);
1013 context_release(context);
1014 return;
1017 context_gl = wined3d_context_gl(context);
1019 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1020 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1021 wined3d_arbfp_blitter_create(&device->blitter, device);
1022 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1023 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1025 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device), context_gl);
1026 wined3d_device_create_default_samplers(device, context);
1027 context_release(context);
1030 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1032 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1033 const struct wined3d_swapchain_desc *swapchain_desc;
1034 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1035 DWORD clear_flags = 0;
1036 unsigned int i;
1037 HRESULT hr;
1039 TRACE("device %p, swapchain %p.\n", device, swapchain);
1041 if (device->d3d_initialized)
1042 return WINED3DERR_INVALIDCALL;
1044 device->swapchain_count = 1;
1045 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1047 ERR("Failed to allocate swapchain array.\n");
1048 hr = E_OUTOFMEMORY;
1049 goto err_out;
1051 device->swapchains[0] = swapchain;
1053 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1055 if (fb->render_targets[i])
1056 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1058 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1060 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1061 goto err_out;
1062 device->d3d_initialized = TRUE;
1064 swapchain_desc = &swapchain->state.desc;
1065 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1067 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1068 struct wined3d_view_desc view_desc;
1070 view_desc.format_id = back_buffer->format->id;
1071 view_desc.flags = 0;
1072 view_desc.u.texture.level_idx = 0;
1073 view_desc.u.texture.level_count = 1;
1074 view_desc.u.texture.layer_idx = 0;
1075 view_desc.u.texture.layer_count = 1;
1076 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1077 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1079 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1080 device->adapter->adapter_ops->adapter_uninit_3d(device);
1081 device->d3d_initialized = FALSE;
1082 goto err_out;
1086 device_init_swapchain_state(device, swapchain);
1088 TRACE("All defaults now set up.\n");
1090 /* Clear the screen. */
1091 if (device->back_buffer_view)
1092 clear_flags |= WINED3DCLEAR_TARGET;
1093 if (swapchain_desc->enable_auto_depth_stencil)
1094 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1095 if (clear_flags)
1096 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1098 if (wined3d_settings.logo)
1099 device_load_logo(device, wined3d_settings.logo);
1101 return WINED3D_OK;
1103 err_out:
1104 heap_free(device->swapchains);
1105 device->swapchains = NULL;
1106 device->swapchain_count = 0;
1108 return hr;
1111 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1113 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1115 wined3d_sampler_decref(sampler);
1118 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1120 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1122 wined3d_rasterizer_state_decref(state);
1125 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1127 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1129 wined3d_blend_state_decref(blend_state);
1132 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1134 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1136 wined3d_depth_stencil_state_decref(state);
1139 void wined3d_device_uninit_3d(struct wined3d_device *device)
1141 struct wined3d_state *state = device->cs->c.state;
1142 struct wined3d_resource *resource, *cursor;
1143 struct wined3d_rendertarget_view *view;
1144 struct wined3d_texture *texture;
1146 TRACE("device %p.\n", device);
1148 if (!device->d3d_initialized)
1150 ERR("Called while 3D support was not initialised.\n");
1151 return;
1154 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1156 device->swapchain_count = 0;
1158 if ((texture = device->logo_texture))
1160 device->logo_texture = NULL;
1161 wined3d_texture_decref(texture);
1164 if ((texture = device->cursor_texture))
1166 device->cursor_texture = NULL;
1167 wined3d_texture_decref(texture);
1170 wined3d_device_context_emit_reset_state(&device->cs->c, false);
1171 state_cleanup(state);
1173 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1174 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1175 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1176 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1178 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1180 TRACE("Unloading resource %p.\n", resource);
1181 wined3d_cs_emit_unload_resource(device->cs, resource);
1184 device->adapter->adapter_ops->adapter_uninit_3d(device);
1185 device->d3d_initialized = FALSE;
1187 if ((view = device->auto_depth_stencil_view))
1189 device->auto_depth_stencil_view = NULL;
1190 if (wined3d_rendertarget_view_decref(view))
1191 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1194 if ((view = device->back_buffer_view))
1196 device->back_buffer_view = NULL;
1197 wined3d_rendertarget_view_decref(view);
1200 heap_free(device->swapchains);
1201 device->swapchains = NULL;
1203 wined3d_state_reset(state, &device->adapter->d3d_info);
1206 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1207 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1208 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1210 * There is no way to deactivate thread safety once it is enabled.
1212 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1214 TRACE("device %p.\n", device);
1216 /* For now just store the flag (needed in case of ddraw). */
1217 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1220 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1222 const struct wined3d_driver_info *driver_info;
1224 TRACE("device %p.\n", device);
1226 driver_info = &device->adapter->driver_info;
1228 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1229 wine_dbgstr_longlong(driver_info->vram_bytes),
1230 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1231 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1233 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1236 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1237 unsigned int idx, unsigned int *offset)
1239 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1241 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1243 WARN("Invalid stream output %u.\n", idx);
1244 return NULL;
1247 if (offset)
1248 *offset = context->state->stream_output[idx].offset;
1249 return context->state->stream_output[idx].buffer;
1252 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1253 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1255 const struct wined3d_stream_state *stream;
1257 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1258 context, stream_idx, buffer, offset, stride);
1260 if (stream_idx >= WINED3D_MAX_STREAMS)
1262 WARN("Stream index %u out of range.\n", stream_idx);
1263 return WINED3DERR_INVALIDCALL;
1266 stream = &context->state->streams[stream_idx];
1267 *buffer = stream->buffer;
1268 if (offset)
1269 *offset = stream->offset;
1270 *stride = stream->stride;
1272 return WINED3D_OK;
1275 static void wined3d_device_set_transform(struct wined3d_device *device,
1276 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1278 TRACE("device %p, state %s, matrix %p.\n",
1279 device, debug_d3dtstype(state), matrix);
1280 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1281 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1282 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1283 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1285 /* If the new matrix is the same as the current one,
1286 * we cut off any further processing. this seems to be a reasonable
1287 * optimization because as was noticed, some apps (warcraft3 for example)
1288 * tend towards setting the same matrix repeatedly for some reason.
1290 * From here on we assume that the new matrix is different, wherever it matters. */
1291 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1293 TRACE("The application is setting the same matrix over again.\n");
1294 return;
1297 device->cs->c.state->transforms[state] = *matrix;
1298 wined3d_device_context_emit_set_transform(&device->cs->c, state, matrix);
1301 static void wined3d_device_get_transform(const struct wined3d_device *device,
1302 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1304 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1306 *matrix = device->cs->c.state->transforms[state];
1309 /* Note lights are real special cases. Although the device caps state only
1310 * e.g. 8 are supported, you can reference any indexes you want as long as
1311 * that number max are enabled at any one point in time. Therefore since the
1312 * indices can be anything, we need a hashmap of them. However, this causes
1313 * stateblock problems. When capturing the state block, I duplicate the
1314 * hashmap, but when recording, just build a chain pretty much of commands to
1315 * be replayed. */
1316 static void wined3d_device_context_set_light(struct wined3d_device_context *context,
1317 unsigned int light_idx, const struct wined3d_light *light)
1319 struct wined3d_light_info *object = NULL;
1320 float rho;
1322 if (FAILED(wined3d_light_state_set_light(&context->state->light_state, light_idx, light, &object)))
1323 return;
1325 /* Initialize the object. */
1326 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1327 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1328 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1329 light_idx, light->type, debug_color(&light->diffuse),
1330 debug_color(&light->specular), debug_color(&light->ambient),
1331 light->position.x, light->position.y, light->position.z,
1332 light->direction.x, light->direction.y, light->direction.z,
1333 light->range, light->falloff, light->theta, light->phi);
1335 switch (light->type)
1337 case WINED3D_LIGHT_POINT:
1338 /* Position */
1339 object->position.x = light->position.x;
1340 object->position.y = light->position.y;
1341 object->position.z = light->position.z;
1342 object->position.w = 1.0f;
1343 object->cutoff = 180.0f;
1344 /* FIXME: Range */
1345 break;
1347 case WINED3D_LIGHT_DIRECTIONAL:
1348 /* Direction */
1349 object->direction.x = -light->direction.x;
1350 object->direction.y = -light->direction.y;
1351 object->direction.z = -light->direction.z;
1352 object->direction.w = 0.0f;
1353 object->exponent = 0.0f;
1354 object->cutoff = 180.0f;
1355 break;
1357 case WINED3D_LIGHT_SPOT:
1358 /* Position */
1359 object->position.x = light->position.x;
1360 object->position.y = light->position.y;
1361 object->position.z = light->position.z;
1362 object->position.w = 1.0f;
1364 /* Direction */
1365 object->direction.x = light->direction.x;
1366 object->direction.y = light->direction.y;
1367 object->direction.z = light->direction.z;
1368 object->direction.w = 0.0f;
1370 /* opengl-ish and d3d-ish spot lights use too different models
1371 * for the light "intensity" as a function of the angle towards
1372 * the main light direction, so we only can approximate very
1373 * roughly. However, spot lights are rather rarely used in games
1374 * (if ever used at all). Furthermore if still used, probably
1375 * nobody pays attention to such details. */
1376 if (!light->falloff)
1378 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1379 * equations have the falloff resp. exponent parameter as an
1380 * exponent, so the spot light lighting will always be 1.0 for
1381 * both of them, and we don't have to care for the rest of the
1382 * rather complex calculation. */
1383 object->exponent = 0.0f;
1385 else
1387 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1388 if (rho < 0.0001f)
1389 rho = 0.0001f;
1390 object->exponent = -0.3f / logf(cosf(rho / 2));
1393 if (object->exponent > 128.0f)
1394 object->exponent = 128.0f;
1396 object->cutoff = (float)(light->phi * 90 / M_PI);
1397 /* FIXME: Range */
1398 break;
1400 case WINED3D_LIGHT_PARALLELPOINT:
1401 object->position.x = light->position.x;
1402 object->position.y = light->position.y;
1403 object->position.z = light->position.z;
1404 object->position.w = 1.0f;
1405 break;
1407 default:
1408 FIXME("Unrecognized light type %#x.\n", light->type);
1411 wined3d_device_context_emit_set_light(context, object);
1414 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1416 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1417 struct wined3d_light_info *light_info;
1419 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1421 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1422 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1424 TRACE("Light enabled requested but light not defined, so defining one!\n");
1425 wined3d_device_context_set_light(&device->cs->c, light_idx, &WINED3D_default_light);
1427 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1429 FIXME("Adding default lights has failed dismally\n");
1430 return;
1434 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1435 wined3d_device_context_emit_set_light_enable(&device->cs->c, light_idx, enable);
1438 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1439 UINT plane_idx, const struct wined3d_vec4 *plane)
1441 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1443 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1445 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1447 TRACE("Application has requested clipplane this device doesn't support.\n");
1448 return WINED3DERR_INVALIDCALL;
1451 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1453 TRACE("Application is setting old values over, nothing to do.\n");
1454 return WINED3D_OK;
1457 clip_planes[plane_idx] = *plane;
1459 wined3d_device_context_emit_set_clip_plane(&device->cs->c, plane_idx, plane);
1461 return WINED3D_OK;
1464 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1465 const struct wined3d_clip_status *clip_status)
1467 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1469 if (!clip_status)
1470 return WINED3DERR_INVALIDCALL;
1472 return WINED3D_OK;
1475 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1476 struct wined3d_clip_status *clip_status)
1478 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1480 if (!clip_status)
1481 return WINED3DERR_INVALIDCALL;
1483 return WINED3D_OK;
1486 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1488 TRACE("device %p, material %p.\n", device, material);
1490 device->cs->c.state->material = *material;
1491 wined3d_device_context_emit_set_material(&device->cs->c, material);
1494 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1495 enum wined3d_format_id *format, unsigned int *offset)
1497 const struct wined3d_state *state = context->state;
1499 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1501 *format = state->index_format;
1502 if (offset)
1503 *offset = state->index_offset;
1504 return state->index_buffer;
1507 static void wined3d_device_set_base_vertex_index(struct wined3d_device *device, int base_index)
1509 TRACE("device %p, base_index %d.\n", device, base_index);
1511 device->cs->c.state->base_vertex_index = base_index;
1514 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1515 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1517 const struct wined3d_state *state = context->state;
1518 unsigned int count;
1520 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1522 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1523 if (count && viewports)
1524 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1525 if (viewport_count)
1526 *viewport_count = state->viewport_count;
1529 static void resolve_depth_buffer(struct wined3d_device *device)
1531 const struct wined3d_state *state = device->cs->c.state;
1532 struct wined3d_rendertarget_view *src_view;
1533 struct wined3d_resource *dst_resource;
1534 struct wined3d_texture *dst_texture;
1536 if (!(dst_texture = state->textures[0]))
1537 return;
1538 dst_resource = &dst_texture->resource;
1539 if (!dst_resource->format->depth_size)
1540 return;
1541 if (!(src_view = state->fb.depth_stencil))
1542 return;
1544 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1545 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1548 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1549 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1551 const struct wined3d_state *state = context->state;
1553 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1555 *blend_factor = state->blend_factor;
1556 *sample_mask = state->sample_mask;
1557 return state->blend_state;
1560 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1561 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1563 const struct wined3d_state *state = context->state;
1565 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1567 *stencil_ref = state->stencil_ref;
1568 return state->depth_stencil_state;
1571 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1572 struct wined3d_device_context *context)
1574 TRACE("context %p.\n", context);
1576 return context->state->rasterizer_state;
1579 static void wined3d_device_set_render_state(struct wined3d_device *device,
1580 enum wined3d_render_state state, DWORD value)
1582 if (state > WINEHIGHEST_RENDER_STATE)
1584 WARN("Unhandled render state %#x.\n", state);
1585 return;
1588 if (value == device->cs->c.state->render_states[state])
1589 TRACE("Application is setting the old value over, nothing to do.\n");
1590 else
1592 device->cs->c.state->render_states[state] = value;
1593 wined3d_device_context_emit_set_render_state(&device->cs->c, state, value);
1596 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1598 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1599 resolve_depth_buffer(device);
1603 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1604 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1606 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1607 device, sampler_idx, debug_d3dsamplerstate(state), value);
1609 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1611 TRACE("Application is setting the old value over, nothing to do.\n");
1612 return;
1615 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1616 wined3d_device_context_emit_set_sampler_state(&device->cs->c, sampler_idx, state, value);
1619 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1620 unsigned int *rect_count, RECT *rects)
1622 const struct wined3d_state *state = context->state;
1623 unsigned int count;
1625 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1627 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1628 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1629 if (rect_count)
1630 *rect_count = state->scissor_rect_count;
1633 void CDECL wined3d_device_context_reset_state(struct wined3d_device_context *context)
1635 TRACE("context %p.\n", context);
1637 state_cleanup(context->state);
1638 wined3d_state_reset(context->state, &context->device->adapter->d3d_info);
1639 wined3d_device_context_emit_reset_state(context, true);
1642 void CDECL wined3d_device_context_set_state(struct wined3d_device_context *context, struct wined3d_state *state)
1644 const struct wined3d_light_info *light;
1645 unsigned int i, j;
1647 TRACE("context %p, state %p.\n", context, state);
1649 context->state = state;
1650 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1652 wined3d_device_context_emit_set_rendertarget_views(context, 0,
1653 ARRAY_SIZE(state->fb.render_targets), state->fb.render_targets);
1655 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1656 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1658 wined3d_device_context_emit_set_stream_outputs(context, state->stream_output);
1660 wined3d_device_context_emit_set_stream_sources(context, 0, WINED3D_MAX_STREAMS, state->streams);
1662 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1663 state->index_format, state->index_offset);
1665 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1667 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1669 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1670 wined3d_device_context_emit_set_constant_buffers(context, i, 0, MAX_CONSTANT_BUFFERS, state->cb[i]);
1671 wined3d_device_context_emit_set_samplers(context, i, 0, MAX_SAMPLER_OBJECTS, state->sampler[i]);
1672 wined3d_device_context_emit_set_shader_resource_views(context, i, 0,
1673 MAX_SHADER_RESOURCE_VIEWS, state->shader_resource_view[i]);
1676 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1677 wined3d_device_context_emit_set_unordered_access_views(context, i, 0, MAX_UNORDERED_ACCESS_VIEWS,
1678 state->unordered_access_view[i], NULL);
1680 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_F,
1681 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1682 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_I,
1683 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1684 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_B,
1685 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1687 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_F,
1688 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1689 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_I,
1690 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1691 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_B,
1692 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1694 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1696 wined3d_device_context_emit_set_texture(context, i, state->textures[i]);
1697 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1699 wined3d_device_context_emit_set_sampler_state(context, i, j, state->sampler_states[i][j]);
1703 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1705 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1707 wined3d_device_context_emit_set_texture_state(context, i, j, state->texture_states[i][j]);
1711 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1713 if (context->device->state_table[STATE_TRANSFORM(i)].representative)
1714 wined3d_device_context_emit_set_transform(context, i, state->transforms + i);
1717 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1719 wined3d_device_context_emit_set_clip_plane(context, i, state->clip_planes + i);
1722 wined3d_device_context_emit_set_material(context, &state->material);
1724 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1725 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1727 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1729 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1731 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
1732 wined3d_device_context_emit_set_light_enable(context, light->OriginalIndex, light->glIndex != -1);
1736 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1738 if (context->device->state_table[STATE_RENDER(i)].representative)
1739 wined3d_device_context_emit_set_render_state(context, i, state->render_states[i]);
1742 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1743 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1744 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1747 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1749 TRACE("device %p.\n", device);
1751 return device->cs->c.state;
1754 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1756 TRACE("device %p.\n", device);
1758 return &device->cs->c;
1761 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1762 const struct wined3d_device_context *context)
1764 TRACE("context %p.\n", context);
1766 return context->state->vertex_declaration;
1769 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1770 enum wined3d_shader_type type, struct wined3d_shader *shader)
1772 struct wined3d_state *state = context->state;
1773 struct wined3d_shader *prev;
1775 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1777 prev = state->shader[type];
1778 if (shader == prev)
1779 return;
1781 if (shader)
1782 wined3d_shader_incref(shader);
1783 state->shader[type] = shader;
1784 wined3d_device_context_emit_set_shader(context, type, shader);
1785 if (prev)
1786 wined3d_shader_decref(prev);
1789 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
1790 enum wined3d_shader_type type)
1792 TRACE("context %p, type %#x.\n", context, type);
1794 return context->state->shader[type];
1797 void CDECL wined3d_device_context_set_constant_buffers(struct wined3d_device_context *context,
1798 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1799 const struct wined3d_constant_buffer_state *buffers)
1801 struct wined3d_state *state = context->state;
1802 unsigned int i;
1804 TRACE("context %p, type %#x, start_idx %u, count %u, buffers %p.\n", context, type, start_idx, count, buffers);
1806 if (!wined3d_bound_range(start_idx, count, MAX_CONSTANT_BUFFERS))
1808 WARN("Invalid constant buffer index %u, count %u.\n", start_idx, count);
1809 return;
1812 if (!memcmp(buffers, &state->cb[type][start_idx], count * sizeof(*buffers)))
1813 return;
1815 wined3d_device_context_emit_set_constant_buffers(context, type, start_idx, count, buffers);
1816 for (i = 0; i < count; ++i)
1818 struct wined3d_buffer *prev = state->cb[type][start_idx + i].buffer;
1819 struct wined3d_buffer *buffer = buffers[i].buffer;
1821 if (buffer)
1822 wined3d_buffer_incref(buffer);
1823 state->cb[type][start_idx + i] = buffers[i];
1824 if (prev)
1825 wined3d_buffer_decref(prev);
1829 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1830 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1832 struct wined3d_state *state = context->state;
1833 struct wined3d_blend_state *prev;
1835 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1836 context, blend_state, blend_factor, sample_mask);
1838 prev = state->blend_state;
1839 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1840 && sample_mask == state->sample_mask)
1841 return;
1843 if (blend_state)
1844 wined3d_blend_state_incref(blend_state);
1845 state->blend_state = blend_state;
1846 state->blend_factor = *blend_factor;
1847 state->sample_mask = sample_mask;
1848 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1849 if (prev)
1850 wined3d_blend_state_decref(prev);
1853 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1854 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1856 struct wined3d_state *state = context->state;
1857 struct wined3d_depth_stencil_state *prev;
1859 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1861 prev = state->depth_stencil_state;
1862 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1863 return;
1865 if (depth_stencil_state)
1866 wined3d_depth_stencil_state_incref(depth_stencil_state);
1867 state->depth_stencil_state = depth_stencil_state;
1868 state->stencil_ref = stencil_ref;
1869 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1870 if (prev)
1871 wined3d_depth_stencil_state_decref(prev);
1874 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1875 struct wined3d_rasterizer_state *rasterizer_state)
1877 struct wined3d_state *state = context->state;
1878 struct wined3d_rasterizer_state *prev;
1880 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
1882 prev = state->rasterizer_state;
1883 if (prev == rasterizer_state)
1884 return;
1886 if (rasterizer_state)
1887 wined3d_rasterizer_state_incref(rasterizer_state);
1888 state->rasterizer_state = rasterizer_state;
1889 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
1890 if (prev)
1891 wined3d_rasterizer_state_decref(prev);
1894 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1895 const struct wined3d_viewport *viewports)
1897 struct wined3d_state *state = context->state;
1898 unsigned int i;
1900 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
1902 for (i = 0; i < viewport_count; ++i)
1904 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1905 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1908 if (viewport_count)
1909 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
1910 else
1911 memset(state->viewports, 0, sizeof(state->viewports));
1912 state->viewport_count = viewport_count;
1914 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
1917 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
1918 const RECT *rects)
1920 struct wined3d_state *state = context->state;
1921 unsigned int i;
1923 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
1925 for (i = 0; i < rect_count; ++i)
1927 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1930 if (state->scissor_rect_count == rect_count
1931 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
1933 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
1934 return;
1937 if (rect_count)
1938 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
1939 else
1940 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
1941 state->scissor_rect_count = rect_count;
1943 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
1946 void CDECL wined3d_device_context_set_shader_resource_views(struct wined3d_device_context *context,
1947 enum wined3d_shader_type type, unsigned int start_idx, unsigned int count,
1948 struct wined3d_shader_resource_view *const *const views)
1950 struct wined3d_shader_resource_view *real_views[MAX_SHADER_RESOURCE_VIEWS];
1951 struct wined3d_state *state = context->state;
1952 const struct wined3d_rendertarget_view *dsv = state->fb.depth_stencil;
1953 unsigned int i;
1955 TRACE("context %p, type %#x, start_idx %u, count %u, views %p.\n", context, type, start_idx, count, views);
1957 if (!wined3d_bound_range(start_idx, count, MAX_SHADER_RESOURCE_VIEWS))
1959 WARN("Invalid view index %u, count %u.\n", start_idx, count);
1960 return;
1963 if (!memcmp(views, &state->shader_resource_view[type][start_idx], count * sizeof(*views)))
1964 return;
1966 memcpy(real_views, views, count * sizeof(*views));
1968 for (i = 0; i < count; ++i)
1970 struct wined3d_shader_resource_view *view = real_views[i];
1972 if (view && (wined3d_is_srv_rtv_bound(state, view)
1973 || (dsv && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
1975 WARN("Application is trying to bind resource which is attached as render target.\n");
1976 real_views[i] = NULL;
1980 wined3d_device_context_emit_set_shader_resource_views(context, type, start_idx, count, real_views);
1981 for (i = 0; i < count; ++i)
1983 struct wined3d_shader_resource_view *prev = state->shader_resource_view[type][start_idx + i];
1984 struct wined3d_shader_resource_view *view = real_views[i];
1986 if (view)
1988 wined3d_shader_resource_view_incref(view);
1989 wined3d_srv_bind_count_inc(view);
1992 state->shader_resource_view[type][start_idx + i] = view;
1993 if (prev)
1995 wined3d_srv_bind_count_dec(prev);
1996 wined3d_shader_resource_view_decref(prev);
2001 void CDECL wined3d_device_context_set_samplers(struct wined3d_device_context *context, enum wined3d_shader_type type,
2002 unsigned int start_idx, unsigned int count, struct wined3d_sampler *const *samplers)
2004 struct wined3d_state *state = context->state;
2005 unsigned int i;
2007 TRACE("context %p, type %#x, start_idx %u, count %u, samplers %p.\n", context, type, start_idx, count, samplers);
2009 if (!wined3d_bound_range(start_idx, count, MAX_SAMPLER_OBJECTS))
2011 WARN("Invalid sampler index %u, count %u.\n", start_idx, count);
2012 return;
2015 if (!memcmp(samplers, &state->sampler[type][start_idx], count * sizeof(*samplers)))
2016 return;
2018 wined3d_device_context_emit_set_samplers(context, type, start_idx, count, samplers);
2019 for (i = 0; i < count; ++i)
2021 struct wined3d_sampler *prev = state->sampler[type][start_idx + i];
2022 struct wined3d_sampler *sampler = samplers[i];
2024 if (sampler)
2025 wined3d_sampler_incref(sampler);
2026 state->sampler[type][start_idx + i] = sampler;
2027 if (prev)
2028 wined3d_sampler_decref(prev);
2032 void CDECL wined3d_device_context_set_unordered_access_views(struct wined3d_device_context *context,
2033 enum wined3d_pipeline pipeline, unsigned int start_idx, unsigned int count,
2034 struct wined3d_unordered_access_view *const *uavs, const unsigned int *initial_counts)
2036 struct wined3d_state *state = context->state;
2037 unsigned int i;
2039 TRACE("context %p, pipeline %#x, start_idx %u, count %u, uavs %p, initial_counts %p.\n",
2040 context, pipeline, start_idx, count, uavs, initial_counts);
2042 if (!wined3d_bound_range(start_idx, count, MAX_UNORDERED_ACCESS_VIEWS))
2044 WARN("Invalid UAV index %u, count %u.\n", start_idx, count);
2045 return;
2048 if (!memcmp(uavs, &state->unordered_access_view[pipeline][start_idx], count * sizeof(*uavs)) && !initial_counts)
2049 return;
2051 wined3d_device_context_emit_set_unordered_access_views(context, pipeline, start_idx, count, uavs, initial_counts);
2052 for (i = 0; i < count; ++i)
2054 struct wined3d_unordered_access_view *prev = state->unordered_access_view[pipeline][start_idx + i];
2055 struct wined3d_unordered_access_view *uav = uavs[i];
2057 if (uav)
2058 wined3d_unordered_access_view_incref(uav);
2059 state->unordered_access_view[pipeline][start_idx + i] = uav;
2060 if (prev)
2061 wined3d_unordered_access_view_decref(prev);
2065 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2066 const struct wined3d_rendertarget_view *view, BOOL dsv)
2068 const struct wined3d_state *state = context->state;
2069 const struct wined3d_resource *resource;
2071 if (!view)
2072 return;
2073 resource = view->resource;
2075 if (resource->srv_bind_count_device)
2077 const struct wined3d_shader_resource_view *srv;
2078 unsigned int i, j;
2080 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2082 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2084 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2085 && ((!dsv && wined3d_is_srv_rtv_bound(state, srv))
2086 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2088 static struct wined3d_shader_resource_view *const null_srv;
2090 WARN("Application sets bound resource as render target.\n");
2091 wined3d_device_context_set_shader_resource_views(context, i, j, 1, &null_srv);
2098 HRESULT CDECL wined3d_device_context_set_rendertarget_views(struct wined3d_device_context *context,
2099 unsigned int start_idx, unsigned int count, struct wined3d_rendertarget_view *const *views, BOOL set_viewport)
2101 struct wined3d_state *state = context->state;
2102 unsigned int i, max_rt_count;
2104 TRACE("context %p, start_idx %u, count %u, views %p, set_viewport %#x.\n",
2105 context, start_idx, count, views, set_viewport);
2107 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2108 if (start_idx >= max_rt_count)
2110 WARN("Only %u render targets are supported.\n", max_rt_count);
2111 return WINED3DERR_INVALIDCALL;
2113 count = min(count, max_rt_count - start_idx);
2115 for (i = 0; i < count; ++i)
2117 if (views[i] && !(views[i]->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2119 WARN("View resource %p doesn't have render target bind flags.\n", views[i]->resource);
2120 return WINED3DERR_INVALIDCALL;
2124 /* Set the viewport and scissor rectangles, if requested. Tests show that
2125 * stateblock recording is ignored, the change goes directly into the
2126 * primary stateblock. */
2127 if (!start_idx && set_viewport)
2129 state->viewports[0].x = 0;
2130 state->viewports[0].y = 0;
2131 state->viewports[0].width = views[0]->width;
2132 state->viewports[0].height = views[0]->height;
2133 state->viewports[0].min_z = 0.0f;
2134 state->viewports[0].max_z = 1.0f;
2135 state->viewport_count = 1;
2136 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2138 SetRect(&state->scissor_rects[0], 0, 0, views[0]->width, views[0]->height);
2139 state->scissor_rect_count = 1;
2140 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2143 if (!memcmp(views, &state->fb.render_targets[start_idx], count * sizeof(*views)))
2144 return WINED3D_OK;
2146 wined3d_device_context_emit_set_rendertarget_views(context, start_idx, count, views);
2147 for (i = 0; i < count; ++i)
2149 struct wined3d_rendertarget_view *prev = state->fb.render_targets[start_idx + i];
2150 struct wined3d_rendertarget_view *view = views[i];
2152 if (view)
2154 wined3d_rendertarget_view_incref(view);
2155 wined3d_rtv_bind_count_inc(view);
2157 state->fb.render_targets[start_idx + i] = view;
2158 /* Release after the assignment, to prevent device_resource_released()
2159 * from seeing the resource as still in use. */
2160 if (prev)
2162 wined3d_rtv_bind_count_dec(prev);
2163 wined3d_rendertarget_view_decref(prev);
2166 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2169 return WINED3D_OK;
2172 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2173 struct wined3d_rendertarget_view *view)
2175 struct wined3d_fb_state *fb = &context->state->fb;
2176 struct wined3d_rendertarget_view *prev;
2178 TRACE("context %p, view %p.\n", context, view);
2180 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2182 WARN("View resource %p has incompatible %s bind flags.\n",
2183 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2184 return WINED3DERR_INVALIDCALL;
2187 prev = fb->depth_stencil;
2188 if (prev == view)
2190 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2191 return WINED3D_OK;
2194 if ((fb->depth_stencil = view))
2195 wined3d_rendertarget_view_incref(view);
2196 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2197 if (prev)
2198 wined3d_rendertarget_view_decref(prev);
2199 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2201 return WINED3D_OK;
2204 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2205 struct wined3d_query *predicate, BOOL value)
2207 struct wined3d_state *state = context->state;
2208 struct wined3d_query *prev;
2210 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2212 prev = state->predicate;
2213 if (predicate)
2215 FIXME("Predicated rendering not implemented.\n");
2216 wined3d_query_incref(predicate);
2218 state->predicate = predicate;
2219 state->predicate_value = value;
2220 wined3d_device_context_emit_set_predication(context, predicate, value);
2221 if (prev)
2222 wined3d_query_decref(prev);
2225 HRESULT CDECL wined3d_device_context_set_stream_sources(struct wined3d_device_context *context,
2226 unsigned int start_idx, unsigned int count, const struct wined3d_stream_state *streams)
2228 struct wined3d_state *state = context->state;
2229 unsigned int i;
2231 TRACE("context %p, start_idx %u, count %u, streams %p.\n", context, start_idx, count, streams);
2233 if (start_idx >= WINED3D_MAX_STREAMS)
2235 WARN("Start index %u is out of range.\n", start_idx);
2236 return WINED3DERR_INVALIDCALL;
2239 count = min(count, WINED3D_MAX_STREAMS - start_idx);
2241 for (i = 0; i < count; ++i)
2243 if (streams[i].offset & 0x3)
2245 WARN("Offset %u is not 4 byte aligned.\n", streams[i].offset);
2246 return WINED3DERR_INVALIDCALL;
2250 if (!memcmp(streams, &state->streams[start_idx], count * sizeof(*streams)))
2251 return WINED3D_OK;
2253 wined3d_device_context_emit_set_stream_sources(context, start_idx, count, streams);
2254 for (i = 0; i < count; ++i)
2256 struct wined3d_buffer *prev = state->streams[start_idx + i].buffer;
2257 struct wined3d_buffer *buffer = streams[i].buffer;
2259 state->streams[start_idx + i] = streams[i];
2261 if (buffer)
2262 wined3d_buffer_incref(buffer);
2263 if (prev)
2264 wined3d_buffer_decref(prev);
2267 return WINED3D_OK;
2270 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2271 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2273 struct wined3d_state *state = context->state;
2274 enum wined3d_format_id prev_format;
2275 struct wined3d_buffer *prev_buffer;
2276 unsigned int prev_offset;
2278 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2279 context, buffer, debug_d3dformat(format_id), offset);
2281 prev_buffer = state->index_buffer;
2282 prev_format = state->index_format;
2283 prev_offset = state->index_offset;
2285 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2286 return;
2288 if (buffer)
2289 wined3d_buffer_incref(buffer);
2290 state->index_buffer = buffer;
2291 state->index_format = format_id;
2292 state->index_offset = offset;
2293 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2294 if (prev_buffer)
2295 wined3d_buffer_decref(prev_buffer);
2298 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2299 struct wined3d_vertex_declaration *declaration)
2301 struct wined3d_state *state = context->state;
2302 struct wined3d_vertex_declaration *prev;
2304 TRACE("context %p, declaration %p.\n", context, declaration);
2306 prev = state->vertex_declaration;
2307 if (declaration == prev)
2308 return;
2310 if (declaration)
2311 wined3d_vertex_declaration_incref(declaration);
2312 state->vertex_declaration = declaration;
2313 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2314 if (prev)
2315 wined3d_vertex_declaration_decref(prev);
2318 void CDECL wined3d_device_context_set_stream_outputs(struct wined3d_device_context *context,
2319 const struct wined3d_stream_output outputs[WINED3D_MAX_STREAM_OUTPUT_BUFFERS])
2321 struct wined3d_state *state = context->state;
2322 unsigned int i;
2324 TRACE("context %p, outputs %p.\n", context, outputs);
2326 wined3d_device_context_emit_set_stream_outputs(context, outputs);
2327 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
2329 struct wined3d_buffer *prev_buffer = state->stream_output[i].buffer;
2330 struct wined3d_buffer *buffer = outputs[i].buffer;
2332 if (buffer)
2333 wined3d_buffer_incref(buffer);
2334 state->stream_output[i] = outputs[i];
2335 if (prev_buffer)
2336 wined3d_buffer_decref(prev_buffer);
2340 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2341 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2343 struct wined3d_state *state = context->state;
2345 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2346 context, start_vertex, vertex_count, start_instance, instance_count);
2348 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2349 0, start_vertex, vertex_count, start_instance, instance_count, false);
2352 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2353 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2355 struct wined3d_state *state = context->state;
2357 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2358 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2360 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2361 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2364 void CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2365 enum wined3d_shader_type shader_type, unsigned int idx, struct wined3d_constant_buffer_state *state)
2367 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2369 if (idx >= MAX_CONSTANT_BUFFERS)
2371 WARN("Invalid constant buffer index %u.\n", idx);
2372 return;
2375 *state = context->state->cb[shader_type][idx];
2378 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2379 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2381 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2383 WARN("Invalid view index %u.\n", idx);
2384 return NULL;
2387 return context->state->shader_resource_view[shader_type][idx];
2390 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2391 enum wined3d_shader_type shader_type, unsigned int idx)
2393 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2395 if (idx >= MAX_SAMPLER_OBJECTS)
2397 WARN("Invalid sampler index %u.\n", idx);
2398 return NULL;
2401 return context->state->sampler[shader_type][idx];
2404 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2405 unsigned int start_idx, unsigned int count, const BOOL *constants)
2407 unsigned int i;
2409 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2410 device, start_idx, count, constants);
2412 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2413 if (TRACE_ON(d3d))
2415 for (i = 0; i < count; ++i)
2416 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2419 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2422 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2423 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2425 unsigned int i;
2427 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2428 device, start_idx, count, constants);
2430 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2431 if (TRACE_ON(d3d))
2433 for (i = 0; i < count; ++i)
2434 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2437 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2440 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2441 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2443 unsigned int i;
2445 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2446 device, start_idx, count, constants);
2448 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2449 if (TRACE_ON(d3d))
2451 for (i = 0; i < count; ++i)
2452 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2455 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2458 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2459 unsigned int start_idx, unsigned int count, const BOOL *constants)
2461 unsigned int i;
2463 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2464 device, start_idx, count, constants);
2466 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2467 if (TRACE_ON(d3d))
2469 for (i = 0; i < count; ++i)
2470 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2473 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2476 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2477 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2479 unsigned int i;
2481 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2482 device, start_idx, count, constants);
2484 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2485 if (TRACE_ON(d3d))
2487 for (i = 0; i < count; ++i)
2488 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2491 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2494 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2495 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2497 unsigned int i;
2499 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2500 device, start_idx, count, constants);
2502 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2503 if (TRACE_ON(d3d))
2505 for (i = 0; i < count; ++i)
2506 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2509 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2512 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2513 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2515 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2517 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2519 WARN("Invalid UAV index %u.\n", idx);
2520 return NULL;
2523 return context->state->unordered_access_view[pipeline][idx];
2526 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2528 unsigned int i;
2530 if (!latency)
2531 latency = 3;
2533 device->max_frame_latency = latency;
2534 for (i = 0; i < device->swapchain_count; ++i)
2535 swapchain_set_max_frame_latency(device->swapchains[i], device);
2538 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2540 return device->max_frame_latency;
2543 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2545 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2546 unsigned int i, size = 0;
2548 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2549 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2550 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2551 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2552 switch (fvf & WINED3DFVF_POSITION_MASK)
2554 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2555 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2556 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2557 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2558 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2559 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2560 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2561 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2562 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2564 for (i = 0; i < texcoord_count; ++i)
2566 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2569 return size;
2572 static void wined3d_format_get_colour(const struct wined3d_format *format,
2573 const void *data, struct wined3d_color *colour)
2575 float *output = &colour->r;
2576 const uint32_t *u32_data;
2577 const uint16_t *u16_data;
2578 const float *f32_data;
2579 unsigned int i;
2581 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2582 static unsigned int warned;
2584 switch (format->id)
2586 case WINED3DFMT_B8G8R8A8_UNORM:
2587 u32_data = data;
2588 wined3d_color_from_d3dcolor(colour, *u32_data);
2589 break;
2591 case WINED3DFMT_R8G8B8A8_UNORM:
2592 u32_data = data;
2593 colour->r = (*u32_data & 0xffu) / 255.0f;
2594 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2595 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2596 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2597 break;
2599 case WINED3DFMT_R16G16_UNORM:
2600 case WINED3DFMT_R16G16B16A16_UNORM:
2601 u16_data = data;
2602 *colour = default_colour;
2603 for (i = 0; i < format->component_count; ++i)
2604 output[i] = u16_data[i] / 65535.0f;
2605 break;
2607 case WINED3DFMT_R32_FLOAT:
2608 case WINED3DFMT_R32G32_FLOAT:
2609 case WINED3DFMT_R32G32B32_FLOAT:
2610 case WINED3DFMT_R32G32B32A32_FLOAT:
2611 f32_data = data;
2612 *colour = default_colour;
2613 for (i = 0; i < format->component_count; ++i)
2614 output[i] = f32_data[i];
2615 break;
2617 default:
2618 *colour = default_colour;
2619 if (!warned++)
2620 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2621 break;
2625 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2626 const struct wined3d_color *material_colour, unsigned int index,
2627 const struct wined3d_stream_info *stream_info)
2629 const struct wined3d_stream_info_element *element = NULL;
2631 switch (mcs)
2633 case WINED3D_MCS_MATERIAL:
2634 *colour = *material_colour;
2635 return;
2637 case WINED3D_MCS_COLOR1:
2638 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
2640 colour->r = colour->g = colour->b = colour->a = 1.0f;
2641 return;
2643 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
2644 break;
2646 case WINED3D_MCS_COLOR2:
2647 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
2649 colour->r = colour->g = colour->b = colour->a = 0.0f;
2650 return;
2652 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
2653 break;
2655 default:
2656 colour->r = colour->g = colour->b = colour->a = 0.0f;
2657 ERR("Invalid material colour source %#x.\n", mcs);
2658 return;
2661 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
2664 static float wined3d_clamp(float value, float min_value, float max_value)
2666 return value < min_value ? min_value : value > max_value ? max_value : value;
2669 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2671 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
2674 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
2676 v0->x -= v1->x;
2677 v0->y -= v1->y;
2678 v0->z -= v1->z;
2681 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
2683 v->x *= s;
2684 v->y *= s;
2685 v->z *= s;
2688 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
2690 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
2692 if (isfinite(rnorm))
2693 wined3d_vec3_scale(v, rnorm);
2696 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
2697 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
2699 struct wined3d_vec3 tmp;
2701 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
2702 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
2703 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
2705 *dst = tmp;
2708 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
2709 float min_value, float max_value)
2711 dst->r = wined3d_clamp(src->r, min_value, max_value);
2712 dst->g = wined3d_clamp(src->g, min_value, max_value);
2713 dst->b = wined3d_clamp(src->b, min_value, max_value);
2714 dst->a = wined3d_clamp(src->a, min_value, max_value);
2717 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
2719 dst->r += src->r * c;
2720 dst->g += src->g * c;
2721 dst->b += src->b * c;
2724 static void init_transformed_lights(struct lights_settings *ls,
2725 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
2727 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
2728 const struct wined3d_light_info *light_info;
2729 struct light_transformed *light;
2730 struct wined3d_vec4 vec4;
2731 unsigned int light_count;
2732 unsigned int i, index;
2734 memset(ls, 0, sizeof(*ls));
2736 ls->lighting = !!compute_lighting;
2737 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
2738 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
2739 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
2740 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
2741 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
2742 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
2744 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
2745 return;
2747 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
2748 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
2750 if (!compute_lighting)
2751 return;
2753 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
2755 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
2756 ls->legacy_lighting = !!legacy_lighting;
2757 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
2758 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
2760 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
2762 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
2764 if (!light_info->enabled)
2765 continue;
2767 switch (light_info->OriginalParms.type)
2769 case WINED3D_LIGHT_DIRECTIONAL:
2770 ++ls->directional_light_count;
2771 break;
2773 case WINED3D_LIGHT_POINT:
2774 ++ls->point_light_count;
2775 break;
2777 case WINED3D_LIGHT_SPOT:
2778 ++ls->spot_light_count;
2779 break;
2781 case WINED3D_LIGHT_PARALLELPOINT:
2782 ++ls->parallel_point_light_count;
2783 break;
2785 default:
2786 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
2787 continue;
2789 lights[index++] = light_info;
2790 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
2791 break;
2795 light_count = index;
2796 for (i = 0, index = 0; i < light_count; ++i)
2798 light_info = lights[i];
2799 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
2800 continue;
2802 light = &ls->lights[index];
2803 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2804 light->direction = *(struct wined3d_vec3 *)&vec4;
2805 wined3d_vec3_normalise(&light->direction);
2807 light->diffuse = light_info->OriginalParms.diffuse;
2808 light->ambient = light_info->OriginalParms.ambient;
2809 light->specular = light_info->OriginalParms.specular;
2810 ++index;
2813 for (i = 0; i < light_count; ++i)
2815 light_info = lights[i];
2816 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
2817 continue;
2819 light = &ls->lights[index];
2821 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2822 light->range = light_info->OriginalParms.range;
2823 light->c_att = light_info->OriginalParms.attenuation0;
2824 light->l_att = light_info->OriginalParms.attenuation1;
2825 light->q_att = light_info->OriginalParms.attenuation2;
2827 light->diffuse = light_info->OriginalParms.diffuse;
2828 light->ambient = light_info->OriginalParms.ambient;
2829 light->specular = light_info->OriginalParms.specular;
2830 ++index;
2833 for (i = 0; i < light_count; ++i)
2835 light_info = lights[i];
2836 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
2837 continue;
2839 light = &ls->lights[index];
2841 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2842 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
2843 light->direction = *(struct wined3d_vec3 *)&vec4;
2844 wined3d_vec3_normalise(&light->direction);
2845 light->range = light_info->OriginalParms.range;
2846 light->falloff = light_info->OriginalParms.falloff;
2847 light->c_att = light_info->OriginalParms.attenuation0;
2848 light->l_att = light_info->OriginalParms.attenuation1;
2849 light->q_att = light_info->OriginalParms.attenuation2;
2850 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
2851 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
2853 light->diffuse = light_info->OriginalParms.diffuse;
2854 light->ambient = light_info->OriginalParms.ambient;
2855 light->specular = light_info->OriginalParms.specular;
2856 ++index;
2859 for (i = 0; i < light_count; ++i)
2861 light_info = lights[i];
2862 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
2863 continue;
2865 light = &ls->lights[index];
2867 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
2868 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
2869 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
2870 light->diffuse = light_info->OriginalParms.diffuse;
2871 light->ambient = light_info->OriginalParms.ambient;
2872 light->specular = light_info->OriginalParms.specular;
2873 ++index;
2877 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
2878 const struct wined3d_vec3 *dir, float att, float material_shininess,
2879 const struct wined3d_vec3 *normal_transformed,
2880 const struct wined3d_vec3 *position_transformed_normalised,
2881 const struct light_transformed *light, const struct lights_settings *ls)
2883 struct wined3d_vec3 vec3;
2884 float t, c;
2886 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
2887 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
2889 vec3 = *dir;
2890 if (ls->localviewer)
2891 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
2892 else
2893 vec3.z -= 1.0f;
2894 wined3d_vec3_normalise(&vec3);
2895 t = wined3d_vec3_dot(normal_transformed, &vec3);
2896 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
2897 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
2898 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
2901 static void light_set_vertex_data(struct lights_settings *ls,
2902 const struct wined3d_vec4 *position)
2904 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
2905 return;
2907 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
2908 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
2911 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
2912 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
2913 float material_shininess)
2915 struct wined3d_vec3 position_transformed_normalised;
2916 struct wined3d_vec3 normal_transformed = {0.0f};
2917 const struct light_transformed *light;
2918 struct wined3d_vec3 dir, dst;
2919 unsigned int i, index;
2920 float att;
2922 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
2923 wined3d_vec3_normalise(&position_transformed_normalised);
2925 if (normal)
2927 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
2928 if (ls->normalise)
2929 wined3d_vec3_normalise(&normal_transformed);
2932 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
2933 *specular = *diffuse;
2934 *ambient = ls->ambient_light;
2936 index = 0;
2937 for (i = 0; i < ls->directional_light_count; ++i, ++index)
2939 light = &ls->lights[index];
2941 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
2942 if (normal)
2943 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
2944 &normal_transformed, &position_transformed_normalised, light, ls);
2947 for (i = 0; i < ls->point_light_count; ++i, ++index)
2949 light = &ls->lights[index];
2950 dir.x = light->position.x - ls->position_transformed.x;
2951 dir.y = light->position.y - ls->position_transformed.y;
2952 dir.z = light->position.z - ls->position_transformed.z;
2954 dst.z = wined3d_vec3_dot(&dir, &dir);
2955 dst.y = sqrtf(dst.z);
2956 dst.x = 1.0f;
2957 if (ls->legacy_lighting)
2959 dst.y = (light->range - dst.y) / light->range;
2960 if (!(dst.y > 0.0f))
2961 continue;
2962 dst.z = dst.y * dst.y;
2964 else
2966 if (!(dst.y <= light->range))
2967 continue;
2969 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
2970 if (!ls->legacy_lighting)
2971 att = 1.0f / att;
2973 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
2974 if (normal)
2976 wined3d_vec3_normalise(&dir);
2977 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
2978 &normal_transformed, &position_transformed_normalised, light, ls);
2982 for (i = 0; i < ls->spot_light_count; ++i, ++index)
2984 float t;
2986 light = &ls->lights[index];
2988 dir.x = light->position.x - ls->position_transformed.x;
2989 dir.y = light->position.y - ls->position_transformed.y;
2990 dir.z = light->position.z - ls->position_transformed.z;
2992 dst.z = wined3d_vec3_dot(&dir, &dir);
2993 dst.y = sqrtf(dst.z);
2994 dst.x = 1.0f;
2996 if (ls->legacy_lighting)
2998 dst.y = (light->range - dst.y) / light->range;
2999 if (!(dst.y > 0.0f))
3000 continue;
3001 dst.z = dst.y * dst.y;
3003 else
3005 if (!(dst.y <= light->range))
3006 continue;
3008 wined3d_vec3_normalise(&dir);
3009 t = -wined3d_vec3_dot(&dir, &light->direction);
3010 if (t > light->cos_htheta)
3011 att = 1.0f;
3012 else if (t <= light->cos_hphi)
3013 att = 0.0f;
3014 else
3015 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3017 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3018 if (ls->legacy_lighting)
3019 att *= t;
3020 else
3021 att /= t;
3023 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3025 if (normal)
3026 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3027 &normal_transformed, &position_transformed_normalised, light, ls);
3030 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3032 light = &ls->lights[index];
3034 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3035 if (normal)
3036 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3037 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3041 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3043 switch (ls->fog_mode)
3045 case WINED3D_FOG_NONE:
3046 return fog_coord;
3047 case WINED3D_FOG_LINEAR:
3048 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3049 case WINED3D_FOG_EXP:
3050 return expf(-fog_coord * ls->fog_density);
3051 case WINED3D_FOG_EXP2:
3052 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3053 default:
3054 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3055 return 0.0f;
3059 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3061 float fog_coord;
3063 if (ls->fog_mode == WINED3D_FOG_NONE)
3064 return;
3066 switch (ls->fog_coord_mode)
3068 case WINED3D_FFP_VS_FOG_RANGE:
3069 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3070 (const struct wined3d_vec3 *)&ls->position_transformed));
3071 break;
3073 case WINED3D_FFP_VS_FOG_DEPTH:
3074 fog_coord = fabsf(ls->position_transformed.z);
3075 break;
3077 default:
3078 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3079 return;
3081 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3084 /* Context activation is done by the caller. */
3085 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3086 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3087 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3089 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3090 const struct wined3d_color *material_specular_state_colour;
3091 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3092 const struct wined3d_state *state = device->cs->c.state;
3093 const struct wined3d_format *output_colour_format;
3094 static const struct wined3d_color black;
3095 struct wined3d_map_desc map_desc;
3096 struct wined3d_box box = {0};
3097 struct wined3d_viewport vp;
3098 unsigned int texture_count;
3099 struct lights_settings ls;
3100 unsigned int vertex_size;
3101 BOOL do_clip, lighting;
3102 float min_z, max_z;
3103 unsigned int i;
3104 BYTE *dest_ptr;
3105 HRESULT hr;
3107 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3109 ERR("Source has no position mask.\n");
3110 return WINED3DERR_INVALIDCALL;
3113 if (state->render_states[WINED3D_RS_CLIPPING])
3115 static BOOL warned = FALSE;
3117 * The clipping code is not quite correct. Some things need
3118 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3119 * so disable clipping for now.
3120 * (The graphics in Half-Life are broken, and my processvertices
3121 * test crashes with IDirect3DDevice3)
3122 do_clip = TRUE;
3124 do_clip = FALSE;
3125 if (!warned)
3127 warned = TRUE;
3128 FIXME("Clipping is broken and disabled for now\n");
3131 else
3132 do_clip = FALSE;
3134 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3135 box.left = dwDestIndex * vertex_size;
3136 box.right = box.left + dwCount * vertex_size;
3137 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3139 WARN("Failed to map buffer, hr %#x.\n", hr);
3140 return hr;
3142 dest_ptr = map_desc.data;
3144 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3145 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3146 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3148 TRACE("View mat:\n");
3149 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3150 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3151 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3152 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3154 TRACE("Proj mat:\n");
3155 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3156 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3157 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3158 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3160 TRACE("World mat:\n");
3161 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3162 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3163 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3164 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3166 /* Get the viewport */
3167 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3168 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3169 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3171 multiply_matrix(&mat,&view_mat,&world_mat);
3172 multiply_matrix(&mat,&proj_mat,&mat);
3174 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3176 lighting = state->render_states[WINED3D_RS_LIGHTING]
3177 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3178 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3179 &ambient_source, &specular_source, state, stream_info);
3180 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3181 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3182 ? &state->material.specular : &black;
3183 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3184 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3186 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3188 for (i = 0; i < dwCount; ++i)
3190 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3191 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3192 struct wined3d_color ambient, diffuse, specular;
3193 struct wined3d_vec4 position;
3194 unsigned int tex_index;
3196 position.x = p[0];
3197 position.y = p[1];
3198 position.z = p[2];
3199 position.w = 1.0f;
3201 light_set_vertex_data(&ls, &position);
3203 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3204 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3205 /* The position first */
3206 float x, y, z, rhw;
3207 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3209 /* Multiplication with world, view and projection matrix. */
3210 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3211 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3212 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3213 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3215 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3217 /* WARNING: The following things are taken from d3d7 and were not yet checked
3218 * against d3d8 or d3d9!
3221 /* Clipping conditions: From msdn
3223 * A vertex is clipped if it does not match the following requirements
3224 * -rhw < x <= rhw
3225 * -rhw < y <= rhw
3226 * 0 < z <= rhw
3227 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3229 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3230 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3234 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3235 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3237 /* "Normal" viewport transformation (not clipped)
3238 * 1) The values are divided by rhw
3239 * 2) The y axis is negative, so multiply it with -1
3240 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3241 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3242 * 4) Multiply x with Width/2 and add Width/2
3243 * 5) The same for the height
3244 * 6) Add the viewpoint X and Y to the 2D coordinates and
3245 * The minimum Z value to z
3246 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3248 * Well, basically it's simply a linear transformation into viewport
3249 * coordinates
3252 x /= rhw;
3253 y /= rhw;
3254 z /= rhw;
3256 y *= -1;
3258 x *= vp.width / 2;
3259 y *= vp.height / 2;
3260 z *= max_z - min_z;
3262 x += vp.width / 2 + vp.x;
3263 y += vp.height / 2 + vp.y;
3264 z += min_z;
3266 rhw = 1 / rhw;
3267 } else {
3268 /* That vertex got clipped
3269 * Contrary to OpenGL it is not dropped completely, it just
3270 * undergoes a different calculation.
3272 TRACE("Vertex got clipped\n");
3273 x += rhw;
3274 y += rhw;
3276 x /= 2;
3277 y /= 2;
3279 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3280 * outside of the main vertex buffer memory. That needs some more
3281 * investigation...
3285 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3288 ( (float *) dest_ptr)[0] = x;
3289 ( (float *) dest_ptr)[1] = y;
3290 ( (float *) dest_ptr)[2] = z;
3291 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3293 dest_ptr += 3 * sizeof(float);
3295 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3296 dest_ptr += sizeof(float);
3299 if (dst_fvf & WINED3DFVF_PSIZE)
3300 dest_ptr += sizeof(DWORD);
3302 if (dst_fvf & WINED3DFVF_NORMAL)
3304 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3305 const float *normal = (const float *)(element->data.addr + i * element->stride);
3306 /* AFAIK this should go into the lighting information */
3307 FIXME("Didn't expect the destination to have a normal\n");
3308 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3311 if (lighting)
3313 const struct wined3d_stream_info_element *element;
3314 struct wined3d_vec3 *normal;
3316 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3318 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3319 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3321 else
3323 normal = NULL;
3325 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3326 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3329 if (dst_fvf & WINED3DFVF_DIFFUSE)
3331 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3333 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3334 &state->material.diffuse, i, stream_info);
3336 if (lighting)
3338 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3339 &state->material.ambient, i, stream_info);
3340 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3341 &state->material.emissive, i, stream_info);
3343 diffuse_colour.r = ambient.r * material_ambient.r
3344 + diffuse.r * material_diffuse.r + material_emissive.r;
3345 diffuse_colour.g = ambient.g * material_ambient.g
3346 + diffuse.g * material_diffuse.g + material_emissive.g;
3347 diffuse_colour.b = ambient.b * material_ambient.b
3348 + diffuse.b * material_diffuse.b + material_emissive.b;
3349 diffuse_colour.a = material_diffuse.a;
3351 else
3353 diffuse_colour = material_diffuse;
3355 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3356 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3357 dest_ptr += sizeof(DWORD);
3360 if (dst_fvf & WINED3DFVF_SPECULAR)
3362 struct wined3d_color material_specular, specular_colour;
3364 wined3d_colour_from_mcs(&material_specular, specular_source,
3365 material_specular_state_colour, i, stream_info);
3367 if (lighting)
3369 specular_colour.r = specular.r * material_specular.r;
3370 specular_colour.g = specular.g * material_specular.g;
3371 specular_colour.b = specular.b * material_specular.b;
3372 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3374 else
3376 specular_colour = material_specular;
3378 update_fog_factor(&specular_colour.a, &ls);
3379 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3380 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3381 dest_ptr += sizeof(DWORD);
3384 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3386 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3387 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3388 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3390 ERR("No source texture, but destination requests one\n");
3391 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3393 else
3395 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3400 wined3d_resource_unmap(&dest->resource, 0);
3402 return WINED3D_OK;
3404 #undef copy_and_next
3406 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3407 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3408 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3410 struct wined3d_state *state = device->cs->c.state;
3411 struct wined3d_stream_info stream_info;
3412 struct wined3d_resource *resource;
3413 struct wined3d_box box = {0};
3414 struct wined3d_shader *vs;
3415 unsigned int i, j;
3416 HRESULT hr;
3417 WORD map;
3419 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3420 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3421 device, src_start_idx, dst_idx, vertex_count,
3422 dst_buffer, declaration, flags, dst_fvf);
3424 if (declaration)
3425 FIXME("Output vertex declaration not implemented yet.\n");
3427 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3428 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3429 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3430 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3432 /* We can't convert FROM a VBO, and vertex buffers used to source into
3433 * process_vertices() are unlikely to ever be used for drawing. Release
3434 * VBOs in those buffers and fix up the stream_info structure.
3436 * Also apply the start index. */
3437 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3439 struct wined3d_stream_info_element *e;
3440 struct wined3d_map_desc map_desc;
3442 if (!(map & 1))
3443 continue;
3445 e = &stream_info.elements[i];
3446 resource = &state->streams[e->stream_idx].buffer->resource;
3447 box.left = src_start_idx * e->stride;
3448 box.right = box.left + vertex_count * e->stride;
3449 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3451 ERR("Failed to map resource.\n");
3452 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3454 if (!(map & 1))
3455 continue;
3457 e = &stream_info.elements[j];
3458 resource = &state->streams[e->stream_idx].buffer->resource;
3459 if (FAILED(wined3d_resource_unmap(resource, 0)))
3460 ERR("Failed to unmap resource.\n");
3462 return WINED3DERR_INVALIDCALL;
3464 e->data.buffer_object = 0;
3465 e->data.addr += (ULONG_PTR)map_desc.data;
3468 hr = process_vertices_strided(device, dst_idx, vertex_count,
3469 &stream_info, dst_buffer, flags, dst_fvf);
3471 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3473 if (!(map & 1))
3474 continue;
3476 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3477 if (FAILED(wined3d_resource_unmap(resource, 0)))
3478 ERR("Failed to unmap resource.\n");
3481 return hr;
3484 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3485 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3487 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3489 TRACE("device %p, stage %u, state %s, value %#x.\n",
3490 device, stage, debug_d3dtexturestate(state), value);
3492 if (stage >= d3d_info->limits.ffp_blend_stages)
3494 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3495 stage, d3d_info->limits.ffp_blend_stages - 1);
3496 return;
3499 if (value == device->cs->c.state->texture_states[stage][state])
3501 TRACE("Application is setting the old value over, nothing to do.\n");
3502 return;
3505 device->cs->c.state->texture_states[stage][state] = value;
3507 wined3d_device_context_emit_set_texture_state(&device->cs->c, stage, state, value);
3510 static void wined3d_device_set_texture(struct wined3d_device *device,
3511 UINT stage, struct wined3d_texture *texture)
3513 struct wined3d_state *state = device->cs->c.state;
3514 struct wined3d_texture *prev;
3516 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3518 /* Windows accepts overflowing this array... we do not. */
3519 if (stage >= ARRAY_SIZE(state->textures))
3521 WARN("Ignoring invalid stage %u.\n", stage);
3522 return;
3525 prev = state->textures[stage];
3526 TRACE("Previous texture %p.\n", prev);
3528 if (texture == prev)
3530 TRACE("App is setting the same texture again, nothing to do.\n");
3531 return;
3534 TRACE("Setting new texture to %p.\n", texture);
3535 state->textures[stage] = texture;
3537 if (texture)
3538 wined3d_texture_incref(texture);
3539 wined3d_device_context_emit_set_texture(&device->cs->c, stage, texture);
3540 if (prev)
3541 wined3d_texture_decref(prev);
3543 return;
3546 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3547 struct wined3d_stateblock *stateblock)
3549 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3550 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3551 const struct wined3d_saved_states *changed = &stateblock->changed;
3552 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3553 struct wined3d_device_context *context = &device->cs->c;
3554 unsigned int i, j, start, idx;
3555 struct wined3d_range range;
3556 uint32_t map;
3558 TRACE("device %p, stateblock %p.\n", device, stateblock);
3560 if (changed->vertexShader)
3561 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_VERTEX, state->vs);
3562 if (changed->pixelShader)
3563 wined3d_device_context_set_shader(context, WINED3D_SHADER_TYPE_PIXEL, state->ps);
3565 for (start = 0; ; start = range.offset + range.size)
3567 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3568 break;
3570 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3573 map = changed->vertexShaderConstantsI;
3574 for (start = 0; ; start = range.offset + range.size)
3576 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3577 break;
3579 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3582 map = changed->vertexShaderConstantsB;
3583 for (start = 0; ; start = range.offset + range.size)
3585 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3586 break;
3588 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3591 for (start = 0; ; start = range.offset + range.size)
3593 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3594 break;
3596 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3599 map = changed->pixelShaderConstantsI;
3600 for (start = 0; ; start = range.offset + range.size)
3602 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3603 break;
3605 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3608 map = changed->pixelShaderConstantsB;
3609 for (start = 0; ; start = range.offset + range.size)
3611 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3612 break;
3614 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3617 if (changed->lights)
3619 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3621 const struct wined3d_light_info *light;
3623 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3625 wined3d_device_context_set_light(context, light->OriginalIndex, &light->OriginalParms);
3626 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3631 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3633 map = changed->renderState[i];
3634 while (map)
3636 j = wined3d_bit_scan(&map);
3637 idx = i * word_bit_count + j;
3639 switch (idx)
3641 case WINED3D_RS_BLENDFACTOR:
3642 case WINED3D_RS_MULTISAMPLEMASK:
3643 case WINED3D_RS_ALPHABLENDENABLE:
3644 case WINED3D_RS_SRCBLEND:
3645 case WINED3D_RS_DESTBLEND:
3646 case WINED3D_RS_BLENDOP:
3647 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
3648 case WINED3D_RS_SRCBLENDALPHA:
3649 case WINED3D_RS_DESTBLENDALPHA:
3650 case WINED3D_RS_BLENDOPALPHA:
3651 case WINED3D_RS_COLORWRITEENABLE:
3652 case WINED3D_RS_COLORWRITEENABLE1:
3653 case WINED3D_RS_COLORWRITEENABLE2:
3654 case WINED3D_RS_COLORWRITEENABLE3:
3655 set_blend_state = TRUE;
3656 break;
3658 case WINED3D_RS_BACK_STENCILFAIL:
3659 case WINED3D_RS_BACK_STENCILFUNC:
3660 case WINED3D_RS_BACK_STENCILPASS:
3661 case WINED3D_RS_BACK_STENCILZFAIL:
3662 case WINED3D_RS_STENCILENABLE:
3663 case WINED3D_RS_STENCILFAIL:
3664 case WINED3D_RS_STENCILFUNC:
3665 case WINED3D_RS_STENCILREF:
3666 case WINED3D_RS_STENCILMASK:
3667 case WINED3D_RS_STENCILPASS:
3668 case WINED3D_RS_STENCILWRITEMASK:
3669 case WINED3D_RS_STENCILZFAIL:
3670 case WINED3D_RS_TWOSIDEDSTENCILMODE:
3671 case WINED3D_RS_ZENABLE:
3672 case WINED3D_RS_ZFUNC:
3673 case WINED3D_RS_ZWRITEENABLE:
3674 set_depth_stencil_state = TRUE;
3675 break;
3677 case WINED3D_RS_FILLMODE:
3678 case WINED3D_RS_CULLMODE:
3679 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
3680 case WINED3D_RS_DEPTHBIAS:
3681 case WINED3D_RS_SCISSORTESTENABLE:
3682 case WINED3D_RS_ANTIALIASEDLINEENABLE:
3683 set_rasterizer_state = TRUE;
3684 break;
3686 default:
3687 wined3d_device_set_render_state(device, idx, state->rs[idx]);
3688 break;
3693 if (set_rasterizer_state)
3695 struct wined3d_rasterizer_state *rasterizer_state;
3696 struct wined3d_rasterizer_state_desc desc;
3697 struct wine_rb_entry *entry;
3698 union
3700 DWORD d;
3701 float f;
3702 } bias;
3704 memset(&desc, 0, sizeof(desc));
3705 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
3706 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
3707 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
3708 desc.depth_bias = bias.f;
3709 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
3710 desc.scale_bias = bias.f;
3711 desc.depth_clip = TRUE;
3712 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
3713 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
3715 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
3717 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
3718 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3720 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
3721 &wined3d_null_parent_ops, &rasterizer_state)))
3723 wined3d_device_context_set_rasterizer_state(context, rasterizer_state);
3724 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
3726 ERR("Failed to insert rasterizer state.\n");
3727 wined3d_rasterizer_state_decref(rasterizer_state);
3732 if (set_blend_state || changed->alpha_to_coverage
3733 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
3735 struct wined3d_blend_state *blend_state;
3736 struct wined3d_blend_state_desc desc;
3737 struct wine_rb_entry *entry;
3738 struct wined3d_color colour;
3739 unsigned int sample_mask;
3741 memset(&desc, 0, sizeof(desc));
3742 desc.alpha_to_coverage = state->alpha_to_coverage;
3743 desc.independent = FALSE;
3744 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
3745 desc.alpha_to_coverage = TRUE;
3746 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
3747 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
3748 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
3749 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
3750 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
3752 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
3753 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
3754 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
3756 else
3758 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
3759 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
3760 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
3762 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
3763 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
3764 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
3765 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
3766 if (desc.rt[1].writemask != desc.rt[0].writemask
3767 || desc.rt[2].writemask != desc.rt[0].writemask
3768 || desc.rt[3].writemask != desc.rt[0].writemask)
3770 desc.independent = TRUE;
3771 for (i = 1; i < 4; ++i)
3773 desc.rt[i].enable = desc.rt[0].enable;
3774 desc.rt[i].src = desc.rt[0].src;
3775 desc.rt[i].dst = desc.rt[0].dst;
3776 desc.rt[i].op = desc.rt[0].op;
3777 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
3778 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
3779 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
3783 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
3784 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
3785 else
3786 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
3788 if ((entry = wine_rb_get(&device->blend_states, &desc)))
3790 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
3791 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3792 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3794 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
3795 &wined3d_null_parent_ops, &blend_state)))
3797 wined3d_device_context_set_blend_state(context, blend_state, &colour,
3798 state->rs[WINED3D_RS_MULTISAMPLEMASK]);
3799 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
3801 ERR("Failed to insert blend state.\n");
3802 wined3d_blend_state_decref(blend_state);
3807 if (set_depth_stencil_state)
3809 struct wined3d_depth_stencil_state *depth_stencil_state;
3810 struct wined3d_depth_stencil_state_desc desc;
3811 struct wine_rb_entry *entry;
3812 unsigned int stencil_ref;
3814 memset(&desc, 0, sizeof(desc));
3815 switch (state->rs[WINED3D_RS_ZENABLE])
3817 case WINED3D_ZB_FALSE:
3818 desc.depth = FALSE;
3819 break;
3821 case WINED3D_ZB_USEW:
3822 FIXME("W buffer is not well handled.\n");
3823 case WINED3D_ZB_TRUE:
3824 desc.depth = TRUE;
3825 break;
3827 default:
3828 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
3830 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
3831 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
3832 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
3833 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
3834 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
3835 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
3836 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
3837 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
3838 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
3840 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
3842 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
3843 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
3844 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
3845 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
3847 else
3849 desc.back = desc.front;
3852 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
3853 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
3854 else
3855 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
3857 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
3859 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
3860 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3862 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
3863 &wined3d_null_parent_ops, &depth_stencil_state)))
3865 wined3d_device_context_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
3866 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
3868 ERR("Failed to insert depth/stencil state.\n");
3869 wined3d_depth_stencil_state_decref(depth_stencil_state);
3874 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
3876 map = changed->textureState[i];
3877 while (map)
3879 j = wined3d_bit_scan(&map);
3880 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
3884 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
3886 map = changed->samplerState[i];
3887 while (map)
3889 j = wined3d_bit_scan(&map);
3890 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
3894 if (changed->transforms)
3896 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
3898 map = changed->transform[i];
3899 while (map)
3901 j = wined3d_bit_scan(&map);
3902 idx = i * word_bit_count + j;
3903 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
3908 if (changed->indices)
3909 wined3d_device_context_set_index_buffer(context, state->index_buffer, state->index_format, 0);
3910 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
3911 if (changed->vertexDecl)
3912 wined3d_device_context_set_vertex_declaration(context, state->vertex_declaration);
3913 if (changed->material)
3914 wined3d_device_set_material(device, &state->material);
3915 if (changed->viewport)
3916 wined3d_device_context_set_viewports(context, 1, &state->viewport);
3917 if (changed->scissorRect)
3918 wined3d_device_context_set_scissor_rects(context, 1, &state->scissor_rect);
3920 map = changed->streamSource | changed->streamFreq;
3921 while (map)
3923 i = wined3d_bit_scan(&map);
3924 wined3d_device_context_set_stream_sources(context, i, 1, &state->streams[i]);
3927 map = changed->textures;
3928 while (map)
3930 i = wined3d_bit_scan(&map);
3931 wined3d_device_set_texture(device, i, state->textures[i]);
3934 map = changed->clipplane;
3935 while (map)
3937 i = wined3d_bit_scan(&map);
3938 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
3941 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
3943 TRACE("Applied stateblock %p.\n", stateblock);
3946 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
3948 TRACE("device %p, caps %p.\n", device, caps);
3950 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
3953 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3954 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3956 struct wined3d_swapchain *swapchain;
3958 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3959 device, swapchain_idx, mode, rotation);
3961 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3962 return WINED3DERR_INVALIDCALL;
3964 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3967 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3969 /* At the moment we have no need for any functionality at the beginning
3970 * of a scene. */
3971 TRACE("device %p.\n", device);
3973 if (device->inScene)
3975 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3976 return WINED3DERR_INVALIDCALL;
3978 device->inScene = TRUE;
3979 return WINED3D_OK;
3982 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3984 TRACE("device %p.\n", device);
3986 if (!device->inScene)
3988 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3989 return WINED3DERR_INVALIDCALL;
3992 device->inScene = FALSE;
3993 return WINED3D_OK;
3996 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3997 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3999 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4001 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4002 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4004 if (!rect_count && rects)
4006 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4007 return WINED3D_OK;
4010 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4012 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4013 if (!ds)
4015 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4016 /* TODO: What about depth stencil buffers without stencil bits? */
4017 return WINED3DERR_INVALIDCALL;
4019 else if (flags & WINED3DCLEAR_TARGET)
4021 if (ds->width < fb->render_targets[0]->width
4022 || ds->height < fb->render_targets[0]->height)
4024 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4025 return WINED3D_OK;
4030 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4032 return WINED3D_OK;
4035 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4037 struct wined3d_state *state = context->state;
4039 TRACE("context %p, value %p.\n", context, value);
4041 if (value)
4042 *value = state->predicate_value;
4043 return state->predicate;
4046 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4047 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4049 struct wined3d_state *state = context->state;
4051 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4052 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4054 state->primitive_type = primitive_type;
4055 state->patch_vertex_count = patch_vertex_count;
4058 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4059 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4061 const struct wined3d_state *state = context->state;
4063 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4064 context, primitive_type, patch_vertex_count);
4066 *primitive_type = state->primitive_type;
4067 if (patch_vertex_count)
4068 *patch_vertex_count = state->patch_vertex_count;
4070 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4073 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4074 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4076 unsigned int src_size, dst_size, src_skip_levels = 0;
4077 unsigned int src_level_count, dst_level_count;
4078 const struct wined3d_dirty_regions *regions;
4079 unsigned int layer_count, level_count, i, j;
4080 enum wined3d_resource_type type;
4081 BOOL entire_texture = TRUE;
4082 struct wined3d_box box;
4084 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4086 /* Verify that the source and destination textures are non-NULL. */
4087 if (!src_texture || !dst_texture)
4089 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4090 return WINED3DERR_INVALIDCALL;
4093 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4094 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4096 WARN("Source resource is GPU accessible or a scratch resource.\n");
4097 return WINED3DERR_INVALIDCALL;
4099 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4101 WARN("Destination resource is CPU accessible.\n");
4102 return WINED3DERR_INVALIDCALL;
4105 /* Verify that the source and destination textures are the same type. */
4106 type = src_texture->resource.type;
4107 if (dst_texture->resource.type != type)
4109 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4110 return WINED3DERR_INVALIDCALL;
4113 layer_count = src_texture->layer_count;
4114 if (layer_count != dst_texture->layer_count)
4116 WARN("Source and destination have different layer counts.\n");
4117 return WINED3DERR_INVALIDCALL;
4120 if (src_texture->resource.format != dst_texture->resource.format)
4122 WARN("Source and destination formats do not match.\n");
4123 return WINED3DERR_INVALIDCALL;
4126 src_level_count = src_texture->level_count;
4127 dst_level_count = dst_texture->level_count;
4128 level_count = min(src_level_count, dst_level_count);
4130 src_size = max(src_texture->resource.width, src_texture->resource.height);
4131 src_size = max(src_size, src_texture->resource.depth);
4132 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4133 dst_size = max(dst_size, dst_texture->resource.depth);
4134 while (src_size > dst_size)
4136 src_size >>= 1;
4137 ++src_skip_levels;
4140 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4141 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4142 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4144 WARN("Source and destination dimensions do not match.\n");
4145 return WINED3DERR_INVALIDCALL;
4148 if ((regions = src_texture->dirty_regions))
4150 for (i = 0; i < layer_count && entire_texture; ++i)
4152 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4153 continue;
4155 entire_texture = FALSE;
4156 break;
4160 /* Update every surface level of the texture. */
4161 if (entire_texture)
4163 for (i = 0; i < level_count; ++i)
4165 wined3d_texture_get_level_box(dst_texture, i, &box);
4166 for (j = 0; j < layer_count; ++j)
4168 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4169 &dst_texture->resource, j * dst_level_count + i, &box,
4170 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4171 0, NULL, WINED3D_TEXF_POINT);
4175 else
4177 unsigned int src_level, box_count, k;
4178 const struct wined3d_box *boxes;
4179 struct wined3d_box b;
4181 for (i = 0; i < layer_count; ++i)
4183 boxes = regions[i].boxes;
4184 box_count = regions[i].box_count;
4185 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4187 boxes = &b;
4188 box_count = 1;
4189 wined3d_texture_get_level_box(dst_texture, i, &b);
4192 for (j = 0; j < level_count; ++j)
4194 src_level = j + src_skip_levels;
4196 /* TODO: We could pass an array of boxes here to avoid
4197 * multiple context acquisitions for the same resource. */
4198 for (k = 0; k < box_count; ++k)
4200 box = boxes[k];
4201 if (src_level)
4203 box.left >>= src_level;
4204 box.top >>= src_level;
4205 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4206 wined3d_texture_get_level_width(src_texture, src_level));
4207 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4208 wined3d_texture_get_level_height(src_texture, src_level));
4209 box.front >>= src_level;
4210 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4211 wined3d_texture_get_level_depth(src_texture, src_level));
4214 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4215 &dst_texture->resource, i * dst_level_count + j, &box,
4216 &src_texture->resource, i * src_level_count + src_level, &box,
4217 0, NULL, WINED3D_TEXF_POINT);
4223 wined3d_texture_clear_dirty_regions(src_texture);
4225 return WINED3D_OK;
4228 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4230 const struct wined3d_state *state = device->cs->c.state;
4231 struct wined3d_texture *texture;
4232 DWORD i;
4234 TRACE("device %p, num_passes %p.\n", device, num_passes);
4236 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4238 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4240 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4241 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4243 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4245 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4246 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4249 texture = state->textures[i];
4250 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4252 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4254 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4255 return E_FAIL;
4257 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4259 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4260 return E_FAIL;
4262 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4263 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4265 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4266 return E_FAIL;
4270 if (wined3d_state_uses_depth_buffer(state)
4271 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4273 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4274 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4276 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4278 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4279 return WINED3DERR_CONFLICTINGRENDERSTATE;
4283 /* return a sensible default */
4284 *num_passes = 1;
4286 TRACE("returning D3D_OK\n");
4287 return WINED3D_OK;
4290 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4292 static BOOL warned;
4294 TRACE("device %p, software %#x.\n", device, software);
4296 if (!warned)
4298 FIXME("device %p, software %#x stub!\n", device, software);
4299 warned = TRUE;
4302 device->softwareVertexProcessing = software;
4305 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4307 static BOOL warned;
4309 TRACE("device %p.\n", device);
4311 if (!warned)
4313 TRACE("device %p stub!\n", device);
4314 warned = TRUE;
4317 return device->softwareVertexProcessing;
4320 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4321 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4323 struct wined3d_swapchain *swapchain;
4325 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4326 device, swapchain_idx, raster_status);
4328 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4329 return WINED3DERR_INVALIDCALL;
4331 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4334 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4336 static BOOL warned;
4338 TRACE("device %p, segments %.8e.\n", device, segments);
4340 if (segments != 0.0f)
4342 if (!warned)
4344 FIXME("device %p, segments %.8e stub!\n", device, segments);
4345 warned = TRUE;
4349 return WINED3D_OK;
4352 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4354 static BOOL warned;
4356 TRACE("device %p.\n", device);
4358 if (!warned)
4360 FIXME("device %p stub!\n", device);
4361 warned = TRUE;
4364 return 0.0f;
4367 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4368 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4370 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4371 context, dst_buffer, offset, uav);
4373 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4376 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4377 const struct wined3d_resource *dst_resource)
4379 if (src_resource->format->id == dst_resource->format->id)
4380 return true;
4381 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4382 return true;
4383 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4384 return false;
4385 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4386 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4387 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4388 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4389 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4390 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4391 return false;
4394 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4395 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4397 unsigned int src_row_block_count, dst_row_block_count;
4398 struct wined3d_texture *dst_texture, *src_texture;
4399 unsigned int src_row_count, dst_row_count;
4400 struct wined3d_box src_box, dst_box;
4401 unsigned int i, j;
4403 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4405 if (src_resource == dst_resource)
4407 WARN("Source and destination are the same resource.\n");
4408 return;
4411 if (src_resource->type != dst_resource->type)
4413 WARN("Resource types (%s / %s) don't match.\n",
4414 debug_d3dresourcetype(dst_resource->type),
4415 debug_d3dresourcetype(src_resource->type));
4416 return;
4419 if (!resources_format_compatible(src_resource, dst_resource))
4421 WARN("Resource formats %s and %s are incompatible.\n",
4422 debug_d3dformat(dst_resource->format->id),
4423 debug_d3dformat(src_resource->format->id));
4424 return;
4427 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4428 / src_resource->format->block_width;
4429 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4430 / dst_resource->format->block_width;
4431 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4432 / src_resource->format->block_height;
4433 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4434 / dst_resource->format->block_height;
4436 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4437 || src_resource->depth != dst_resource->depth)
4439 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4440 dst_row_block_count, dst_row_count, dst_resource->depth,
4441 src_row_block_count, src_row_count, src_resource->depth);
4442 return;
4445 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4447 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4448 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4449 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4450 return;
4453 dst_texture = texture_from_resource(dst_resource);
4454 src_texture = texture_from_resource(src_resource);
4456 if (src_texture->layer_count != dst_texture->layer_count
4457 || src_texture->level_count != dst_texture->level_count)
4459 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4460 dst_texture->layer_count, dst_texture->level_count,
4461 src_texture->layer_count, src_texture->level_count);
4462 return;
4465 for (i = 0; i < dst_texture->level_count; ++i)
4467 wined3d_texture_get_level_box(src_texture, i, &src_box);
4468 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4469 for (j = 0; j < dst_texture->layer_count; ++j)
4471 unsigned int idx = j * dst_texture->level_count + i;
4473 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4474 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4479 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4480 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4481 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4482 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4484 struct wined3d_box dst_box, b;
4486 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4487 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4488 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4489 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4491 if (flags)
4492 FIXME("Ignoring flags %#x.\n", flags);
4494 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4496 WARN("Source and destination are the same sub-resource.\n");
4497 return WINED3DERR_INVALIDCALL;
4500 if (!resources_format_compatible(src_resource, dst_resource))
4502 WARN("Resource formats %s and %s are incompatible.\n",
4503 debug_d3dformat(dst_resource->format->id),
4504 debug_d3dformat(src_resource->format->id));
4505 return WINED3DERR_INVALIDCALL;
4508 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4510 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4512 WARN("Resource types (%s / %s) don't match.\n",
4513 debug_d3dresourcetype(dst_resource->type),
4514 debug_d3dresourcetype(src_resource->type));
4515 return WINED3DERR_INVALIDCALL;
4518 if (dst_sub_resource_idx)
4520 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4521 return WINED3DERR_INVALIDCALL;
4524 if (src_sub_resource_idx)
4526 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4527 return WINED3DERR_INVALIDCALL;
4530 if (!src_box)
4532 unsigned int dst_w;
4534 dst_w = dst_resource->size - dst_x;
4535 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4536 src_box = &b;
4538 else if ((src_box->left >= src_box->right
4539 || src_box->top >= src_box->bottom
4540 || src_box->front >= src_box->back))
4542 WARN("Invalid box %s specified.\n", debug_box(src_box));
4543 return WINED3DERR_INVALIDCALL;
4546 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4547 || src_box->right - src_box->left > dst_resource->size - dst_x)
4549 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4550 dst_x, src_box->left, src_box->right - src_box->left);
4551 return WINED3DERR_INVALIDCALL;
4554 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
4556 else
4558 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
4559 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
4560 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
4561 unsigned int src_row_block_count, src_row_count;
4563 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4565 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
4566 return WINED3DERR_INVALIDCALL;
4569 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
4571 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
4572 return WINED3DERR_INVALIDCALL;
4575 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
4577 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
4578 return WINED3DERR_INVALIDCALL;
4581 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
4583 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
4584 return WINED3DERR_INVALIDCALL;
4587 if (!src_box)
4589 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
4591 src_w = wined3d_texture_get_level_width(src_texture, src_level);
4592 src_h = wined3d_texture_get_level_height(src_texture, src_level);
4593 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
4595 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4596 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
4597 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
4598 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
4600 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
4601 src_box = &b;
4603 else if (FAILED(wined3d_resource_check_box_dimensions(src_resource, src_sub_resource_idx, src_box)))
4605 WARN("Invalid source box %s.\n", debug_box(src_box));
4606 return WINED3DERR_INVALIDCALL;
4609 if (src_resource->format->block_width == dst_resource->format->block_width
4610 && src_resource->format->block_height == dst_resource->format->block_height)
4612 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
4613 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
4615 else
4617 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
4618 / src_resource->format->block_width;
4619 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
4620 / src_resource->format->block_height;
4621 wined3d_box_set(&dst_box, dst_x, dst_y,
4622 dst_x + (src_row_block_count * dst_resource->format->block_width),
4623 dst_y + (src_row_count * dst_resource->format->block_height),
4624 dst_z, dst_z + (src_box->back - src_box->front));
4626 if (FAILED(wined3d_resource_check_box_dimensions(dst_resource, dst_sub_resource_idx, &dst_box)))
4628 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
4629 return WINED3DERR_INVALIDCALL;
4633 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
4634 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4636 return WINED3D_OK;
4639 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
4640 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
4641 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
4643 struct wined3d_sub_resource_desc desc;
4644 struct wined3d_box b;
4646 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
4647 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
4649 if (flags)
4650 FIXME("Ignoring flags %#x.\n", flags);
4652 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
4654 WARN("Resource %p is not GPU accessible.\n", resource);
4655 return;
4658 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4659 return;
4661 if (!box)
4663 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4664 box = &b;
4666 else if (box->left >= box->right || box->right > desc.width
4667 || box->top >= box->bottom || box->bottom > desc.height
4668 || box->front >= box->back || box->back > desc.depth)
4670 WARN("Invalid box %s specified.\n", debug_box(box));
4671 return;
4674 wined3d_device_context_emit_update_sub_resource(context, resource,
4675 sub_resource_idx, box, data, row_pitch, depth_pitch);
4678 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
4679 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
4680 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
4681 enum wined3d_format_id format_id)
4683 struct wined3d_texture *dst_texture, *src_texture;
4684 unsigned int dst_level, src_level;
4685 struct wined3d_blt_fx fx = {0};
4686 RECT dst_rect, src_rect;
4688 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
4689 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4690 context, dst_resource, dst_sub_resource_idx,
4691 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
4693 if (wined3d_format_is_typeless(dst_resource->format)
4694 || wined3d_format_is_typeless(src_resource->format))
4696 FIXME("Multisample resolve is not fully supported for typeless formats "
4697 "(dst_format %s, src_format %s, format %s).\n",
4698 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
4699 debug_d3dformat(format_id));
4701 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4703 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
4704 return;
4706 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
4708 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
4709 return;
4712 fx.resolve_format_id = format_id;
4714 dst_texture = texture_from_resource(dst_resource);
4715 src_texture = texture_from_resource(src_resource);
4717 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4718 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
4719 wined3d_texture_get_level_height(dst_texture, dst_level));
4720 src_level = src_sub_resource_idx % src_texture->level_count;
4721 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
4722 wined3d_texture_get_level_height(src_texture, src_level));
4723 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
4724 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
4727 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
4728 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
4729 const struct wined3d_color *color, float depth, unsigned int stencil)
4731 struct wined3d_resource *resource;
4732 RECT r;
4734 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4735 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
4737 if (!flags)
4738 return WINED3D_OK;
4740 resource = view->resource;
4741 if (resource->type == WINED3D_RTYPE_BUFFER)
4743 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
4744 return WINED3DERR_INVALIDCALL;
4747 if (!rect)
4749 SetRect(&r, 0, 0, view->width, view->height);
4750 rect = &r;
4752 else
4754 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
4755 HRESULT hr;
4757 if (FAILED(hr = wined3d_resource_check_box_dimensions(resource, view->sub_resource_idx, &b)))
4758 return hr;
4761 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
4763 return WINED3D_OK;
4766 void CDECL wined3d_device_context_clear_uav_float(struct wined3d_device_context *context,
4767 struct wined3d_unordered_access_view *view, const struct wined3d_vec4 *clear_value)
4769 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_vec4(clear_value));
4771 if (!(view->format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_FLOAT | WINED3DFMT_FLAG_NORMALISED)))
4773 WARN("Not supported for view format %s.\n", debug_d3dformat(view->format->id));
4774 return;
4777 wined3d_device_context_emit_clear_uav(context, view, (const struct wined3d_uvec4 *)clear_value, true);
4780 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
4781 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
4783 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
4785 wined3d_device_context_emit_clear_uav(context, view, clear_value, false);
4788 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
4790 /* Not all flags make sense together, but Windows never returns an error.
4791 * Catch the cases that could cause issues. */
4792 if (flags & WINED3D_MAP_READ)
4794 if (flags & WINED3D_MAP_DISCARD)
4796 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
4797 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4799 if (flags & WINED3D_MAP_NOOVERWRITE)
4801 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
4802 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4805 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4807 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
4809 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
4810 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
4812 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4813 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
4815 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
4816 flags &= ~WINED3D_MAP_DISCARD;
4820 return flags;
4823 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
4824 struct wined3d_resource *resource, unsigned int sub_resource_idx,
4825 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
4827 struct wined3d_sub_resource_desc desc;
4828 struct wined3d_box b;
4829 HRESULT hr;
4831 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
4832 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
4834 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
4836 WARN("No read/write flags specified.\n");
4837 return E_INVALIDARG;
4840 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
4842 WARN("Resource does not have MAP_R access.\n");
4843 return E_INVALIDARG;
4846 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
4848 WARN("Resource does not have MAP_W access.\n");
4849 return E_INVALIDARG;
4852 flags = sanitise_map_flags(resource, flags);
4854 if (FAILED(wined3d_resource_get_sub_resource_desc(resource, sub_resource_idx, &desc)))
4855 return E_INVALIDARG;
4857 if (!box)
4859 wined3d_box_set(&b, 0, 0, desc.width, desc.height, 0, desc.depth);
4860 box = &b;
4862 else if (FAILED(wined3d_resource_check_box_dimensions(resource, sub_resource_idx, box)))
4864 WARN("Map box is invalid.\n");
4866 if (resource->type != WINED3D_RTYPE_BUFFER && resource->type != WINED3D_RTYPE_TEXTURE_2D)
4867 return WINED3DERR_INVALIDCALL;
4869 if ((resource->format_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
4870 return WINED3DERR_INVALIDCALL;
4873 if (SUCCEEDED(hr = wined3d_device_context_emit_map(context, resource,
4874 sub_resource_idx, &map_desc->data, box, flags)))
4875 wined3d_resource_get_sub_resource_map_pitch(resource, sub_resource_idx,
4876 &map_desc->row_pitch, &map_desc->slice_pitch);
4877 return hr;
4880 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
4881 struct wined3d_resource *resource, unsigned int sub_resource_idx)
4883 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
4885 return wined3d_device_context_emit_unmap(context, resource, sub_resource_idx);
4888 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
4889 struct wined3d_query *query, unsigned int flags)
4891 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
4893 context->ops->issue_query(context, query, flags);
4896 void CDECL wined3d_device_context_execute_command_list(struct wined3d_device_context *context,
4897 struct wined3d_command_list *list, bool restore_state)
4899 TRACE("context %p, list %p, restore_state %d.\n", context, list, restore_state);
4901 wined3d_device_context_emit_execute_command_list(context, list, restore_state);
4904 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
4905 const struct wined3d_device_context *context, unsigned int view_idx)
4907 unsigned int max_rt_count;
4909 TRACE("context %p, view_idx %u.\n", context, view_idx);
4911 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
4912 if (view_idx >= max_rt_count)
4914 WARN("Only %u render targets are supported.\n", max_rt_count);
4915 return NULL;
4918 return context->state->fb.render_targets[view_idx];
4921 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
4922 const struct wined3d_device_context *context)
4924 TRACE("context %p.\n", context);
4926 return context->state->fb.depth_stencil;
4929 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
4930 struct wined3d_shader_resource_view *view)
4932 struct wined3d_texture *texture;
4934 TRACE("context %p, view %p.\n", context, view);
4936 if (view->resource->type == WINED3D_RTYPE_BUFFER)
4938 WARN("Called on buffer resource %p.\n", view->resource);
4939 return;
4942 texture = texture_from_resource(view->resource);
4943 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
4945 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
4946 return;
4949 wined3d_device_context_emit_generate_mipmaps(context, view);
4952 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
4953 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
4955 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
4956 struct wined3d_sub_resource_data data;
4957 struct wined3d_resource_desc desc;
4958 struct wined3d_map_desc map_desc;
4959 struct wined3d_texture *texture;
4960 HRESULT hr;
4962 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
4964 ERR("Failed to map source texture.\n");
4965 return NULL;
4968 data.data = map_desc.data;
4969 data.row_pitch = map_desc.row_pitch;
4970 data.slice_pitch = map_desc.slice_pitch;
4972 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
4973 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
4974 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
4975 desc.multisample_quality = 0;
4976 desc.usage = WINED3DUSAGE_DYNAMIC;
4977 desc.bind_flags = 0;
4978 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
4979 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
4980 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
4981 desc.depth = 1;
4982 desc.size = 0;
4984 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
4985 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
4986 if (FAILED(hr))
4988 ERR("Failed to create cursor texture.\n");
4989 return NULL;
4992 return texture;
4995 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4996 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
4998 unsigned int texture_level = sub_resource_idx % texture->level_count;
4999 unsigned int cursor_width, cursor_height;
5000 struct wined3d_map_desc map_desc;
5002 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5003 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5005 if (sub_resource_idx >= texture->level_count * texture->layer_count
5006 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5007 return WINED3DERR_INVALIDCALL;
5009 if (device->cursor_texture)
5011 wined3d_texture_decref(device->cursor_texture);
5012 device->cursor_texture = NULL;
5015 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5017 WARN("Texture %p has invalid format %s.\n",
5018 texture, debug_d3dformat(texture->resource.format->id));
5019 return WINED3DERR_INVALIDCALL;
5022 /* Cursor width and height must all be powers of two */
5023 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5024 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5025 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5027 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5028 return WINED3DERR_INVALIDCALL;
5031 /* Do not store the surface's pointer because the application may
5032 * release it after setting the cursor image. Windows doesn't
5033 * addref the set surface, so we can't do this either without
5034 * creating circular refcount dependencies. */
5035 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5037 ERR("Failed to create cursor texture.\n");
5038 return WINED3DERR_INVALIDCALL;
5041 if (cursor_width == 32 && cursor_height == 32)
5043 UINT mask_size = cursor_width * cursor_height / 8;
5044 ICONINFO cursor_info;
5045 DWORD *mask_bits;
5046 HCURSOR cursor;
5048 /* 32-bit user32 cursors ignore the alpha channel if it's all
5049 * zeroes, and use the mask instead. Fill the mask with all ones
5050 * to ensure we still get a fully transparent cursor. */
5051 if (!(mask_bits = heap_alloc(mask_size)))
5052 return E_OUTOFMEMORY;
5053 memset(mask_bits, 0xff, mask_size);
5055 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5056 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5057 cursor_info.fIcon = FALSE;
5058 cursor_info.xHotspot = x_hotspot;
5059 cursor_info.yHotspot = y_hotspot;
5060 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5061 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5062 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5064 /* Create our cursor and clean up. */
5065 cursor = CreateIconIndirect(&cursor_info);
5066 if (cursor_info.hbmMask)
5067 DeleteObject(cursor_info.hbmMask);
5068 if (cursor_info.hbmColor)
5069 DeleteObject(cursor_info.hbmColor);
5070 if (device->hardwareCursor)
5071 DestroyCursor(device->hardwareCursor);
5072 device->hardwareCursor = cursor;
5073 if (device->bCursorVisible)
5074 SetCursor(cursor);
5076 heap_free(mask_bits);
5079 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5080 device->cursorWidth = cursor_width;
5081 device->cursorHeight = cursor_height;
5082 device->xHotSpot = x_hotspot;
5083 device->yHotSpot = y_hotspot;
5085 return WINED3D_OK;
5088 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5089 int x_screen_space, int y_screen_space, DWORD flags)
5091 TRACE("device %p, x %d, y %d, flags %#x.\n",
5092 device, x_screen_space, y_screen_space, flags);
5094 device->xScreenSpace = x_screen_space;
5095 device->yScreenSpace = y_screen_space;
5097 if (device->hardwareCursor)
5099 POINT pt;
5101 GetCursorPos( &pt );
5102 if (x_screen_space == pt.x && y_screen_space == pt.y)
5103 return;
5104 SetCursorPos( x_screen_space, y_screen_space );
5106 /* Switch to the software cursor if position diverges from the hardware one. */
5107 GetCursorPos( &pt );
5108 if (x_screen_space != pt.x || y_screen_space != pt.y)
5110 if (device->bCursorVisible) SetCursor( NULL );
5111 DestroyCursor( device->hardwareCursor );
5112 device->hardwareCursor = 0;
5117 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5119 BOOL oldVisible = device->bCursorVisible;
5121 TRACE("device %p, show %#x.\n", device, show);
5124 * When ShowCursor is first called it should make the cursor appear at the OS's last
5125 * known cursor position.
5127 if (show && !oldVisible)
5129 POINT pt;
5130 GetCursorPos(&pt);
5131 device->xScreenSpace = pt.x;
5132 device->yScreenSpace = pt.y;
5135 if (device->hardwareCursor)
5137 device->bCursorVisible = show;
5138 if (show)
5139 SetCursor(device->hardwareCursor);
5140 else
5141 SetCursor(NULL);
5143 else if (device->cursor_texture)
5145 device->bCursorVisible = show;
5148 return oldVisible;
5151 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5153 struct wined3d_resource *resource, *cursor;
5155 TRACE("device %p.\n", device);
5157 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5159 TRACE("Checking resource %p for eviction.\n", resource);
5161 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5163 TRACE("Evicting %p.\n", resource);
5164 wined3d_cs_emit_unload_resource(device->cs, resource);
5169 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5171 TRACE("context %p.\n", context);
5173 context->ops->flush(context);
5176 static void update_swapchain_flags(struct wined3d_texture *texture)
5178 unsigned int flags = texture->swapchain->state.desc.flags;
5180 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5181 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5182 else
5183 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5185 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5186 texture->flags |= WINED3D_TEXTURE_GET_DC;
5187 else
5188 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5191 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5192 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5193 wined3d_device_reset_cb callback, BOOL reset_state)
5195 static struct wined3d_rendertarget_view *const views[WINED3D_MAX_RENDER_TARGETS];
5196 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5197 struct wined3d_device_context *context = &device->cs->c;
5198 struct wined3d_swapchain_state *swapchain_state;
5199 struct wined3d_state *state = context->state;
5200 struct wined3d_swapchain_desc *current_desc;
5201 struct wined3d_resource *resource, *cursor;
5202 struct wined3d_rendertarget_view *view;
5203 struct wined3d_swapchain *swapchain;
5204 struct wined3d_view_desc view_desc;
5205 BOOL backbuffer_resized, windowed;
5206 HRESULT hr = WINED3D_OK;
5207 unsigned int i;
5209 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5210 device, swapchain_desc, mode, callback, reset_state);
5212 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5214 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5216 ERR("Failed to get the first implicit swapchain.\n");
5217 return WINED3DERR_INVALIDCALL;
5219 swapchain_state = &swapchain->state;
5220 current_desc = &swapchain_state->desc;
5222 if (reset_state)
5224 if (device->logo_texture)
5226 wined3d_texture_decref(device->logo_texture);
5227 device->logo_texture = NULL;
5229 if (device->cursor_texture)
5231 wined3d_texture_decref(device->cursor_texture);
5232 device->cursor_texture = NULL;
5234 state_unbind_resources(state);
5237 wined3d_device_context_set_rendertarget_views(context, 0, d3d_info->limits.max_rt_count, views, FALSE);
5238 wined3d_device_context_set_depth_stencil_view(context, NULL);
5240 if (reset_state)
5242 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5244 TRACE("Enumerating resource %p.\n", resource);
5245 if (FAILED(hr = callback(resource)))
5246 return hr;
5250 TRACE("New params:\n");
5251 TRACE("output %p\n", swapchain_desc->output);
5252 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5253 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5254 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5255 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5256 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5257 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5258 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5259 TRACE("device_window %p\n", swapchain_desc->device_window);
5260 TRACE("windowed %#x\n", swapchain_desc->windowed);
5261 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5262 if (swapchain_desc->enable_auto_depth_stencil)
5263 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5264 TRACE("flags %#x\n", swapchain_desc->flags);
5265 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5266 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5268 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5269 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5271 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5272 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5273 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5274 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5276 /* No special treatment of these parameters. Just store them */
5277 current_desc->swap_effect = swapchain_desc->swap_effect;
5278 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5279 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5280 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5281 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5283 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5285 TRACE("Changing the device window from %p to %p.\n",
5286 current_desc->device_window, swapchain_desc->device_window);
5287 current_desc->device_window = swapchain_desc->device_window;
5288 swapchain_state->device_window = swapchain_desc->device_window;
5289 wined3d_swapchain_set_window(swapchain, NULL);
5292 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5293 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5294 windowed = current_desc->windowed;
5296 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5297 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5299 /* Switch from windowed to fullscreen. */
5300 if (windowed && !swapchain_desc->windowed)
5302 HWND focus_window = device->create_parms.focus_window;
5304 if (!focus_window)
5305 focus_window = swapchain->state.device_window;
5306 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5308 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5309 return hr;
5313 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5314 swapchain_desc, mode)))
5315 return hr;
5317 /* Switch from fullscreen to windowed. */
5318 if (!windowed && swapchain_desc->windowed)
5319 wined3d_device_release_focus_window(device);
5321 else if (!swapchain_desc->windowed)
5323 DWORD style = swapchain_state->style;
5324 DWORD exstyle = swapchain_state->exstyle;
5325 struct wined3d_output_desc output_desc;
5327 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5328 * the window back into the right position. Some applications
5329 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5330 * up their mess. Guild Wars also loses the device during that. */
5331 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5333 ERR("Failed to get output description, hr %#x.\n", hr);
5334 return hr;
5337 swapchain_state->style = 0;
5338 swapchain_state->exstyle = 0;
5339 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5340 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5341 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5342 swapchain_state->style = style;
5343 swapchain_state->exstyle = exstyle;
5346 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5347 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5348 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5349 return hr;
5351 if (swapchain_desc->flags != current_desc->flags)
5353 current_desc->flags = swapchain_desc->flags;
5355 update_swapchain_flags(swapchain->front_buffer);
5356 for (i = 0; i < current_desc->backbuffer_count; ++i)
5358 update_swapchain_flags(swapchain->back_buffers[i]);
5362 if ((view = device->auto_depth_stencil_view))
5364 device->auto_depth_stencil_view = NULL;
5365 wined3d_rendertarget_view_decref(view);
5367 if (current_desc->enable_auto_depth_stencil)
5369 struct wined3d_resource_desc texture_desc;
5370 struct wined3d_texture *texture;
5372 TRACE("Creating the depth stencil buffer.\n");
5374 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5375 texture_desc.format = current_desc->auto_depth_stencil_format;
5376 texture_desc.multisample_type = current_desc->multisample_type;
5377 texture_desc.multisample_quality = current_desc->multisample_quality;
5378 texture_desc.usage = 0;
5379 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5380 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5381 texture_desc.width = current_desc->backbuffer_width;
5382 texture_desc.height = current_desc->backbuffer_height;
5383 texture_desc.depth = 1;
5384 texture_desc.size = 0;
5386 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5387 device->device_parent, &texture_desc, 0, &texture)))
5389 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5390 return WINED3DERR_INVALIDCALL;
5393 view_desc.format_id = texture->resource.format->id;
5394 view_desc.flags = 0;
5395 view_desc.u.texture.level_idx = 0;
5396 view_desc.u.texture.level_count = 1;
5397 view_desc.u.texture.layer_idx = 0;
5398 view_desc.u.texture.layer_count = 1;
5399 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5400 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5401 wined3d_texture_decref(texture);
5402 if (FAILED(hr))
5404 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5405 return hr;
5409 if ((view = device->back_buffer_view))
5411 device->back_buffer_view = NULL;
5412 wined3d_rendertarget_view_decref(view);
5414 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5416 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5418 view_desc.format_id = back_buffer->format->id;
5419 view_desc.flags = 0;
5420 view_desc.u.texture.level_idx = 0;
5421 view_desc.u.texture.level_count = 1;
5422 view_desc.u.texture.layer_idx = 0;
5423 view_desc.u.texture.layer_count = 1;
5424 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5425 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5427 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5428 return hr;
5432 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5433 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5434 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5435 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5437 if (reset_state)
5439 TRACE("Resetting state.\n");
5440 wined3d_device_context_emit_reset_state(&device->cs->c, false);
5441 state_cleanup(state);
5443 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5445 TRACE("Unloading resource %p.\n", resource);
5446 wined3d_cs_emit_unload_resource(device->cs, resource);
5449 device->adapter->adapter_ops->adapter_uninit_3d(device);
5451 wined3d_state_reset(state, &device->adapter->d3d_info);
5453 device_init_swapchain_state(device, swapchain);
5454 if (wined3d_settings.logo)
5455 device_load_logo(device, wined3d_settings.logo);
5457 else
5459 if ((view = device->back_buffer_view))
5460 wined3d_device_context_set_rendertarget_views(context, 0, 1, &view, FALSE);
5461 if ((view = device->auto_depth_stencil_view))
5462 wined3d_device_context_set_depth_stencil_view(context, view);
5465 if (reset_state)
5466 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5468 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5469 * first use
5471 return hr;
5474 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5476 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5478 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5480 return WINED3D_OK;
5484 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5485 struct wined3d_device_creation_parameters *parameters)
5487 TRACE("device %p, parameters %p.\n", device, parameters);
5489 *parameters = device->create_parms;
5492 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5494 TRACE("device %p.\n", device);
5496 return device->wined3d;
5499 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5500 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5502 struct wined3d_swapchain *swapchain;
5504 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5505 device, swapchain_idx, flags, ramp);
5507 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5508 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5511 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5512 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5514 struct wined3d_swapchain *swapchain;
5516 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5517 device, swapchain_idx, ramp);
5519 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5520 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5523 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5525 TRACE("device %p, resource %p.\n", device, resource);
5527 wined3d_not_from_cs(device->cs);
5529 list_add_head(&device->resources, &resource->resource_list_entry);
5532 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5534 TRACE("device %p, resource %p.\n", device, resource);
5536 wined3d_not_from_cs(device->cs);
5538 list_remove(&resource->resource_list_entry);
5541 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5543 enum wined3d_resource_type type = resource->type;
5544 struct wined3d_state *state = device->cs->c.state;
5545 struct wined3d_rendertarget_view *rtv;
5546 unsigned int i;
5548 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5550 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5552 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5553 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5556 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5557 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5559 switch (type)
5561 case WINED3D_RTYPE_TEXTURE_1D:
5562 case WINED3D_RTYPE_TEXTURE_2D:
5563 case WINED3D_RTYPE_TEXTURE_3D:
5564 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5566 if (&state->textures[i]->resource == resource)
5568 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5569 state->textures[i] = NULL;
5572 break;
5574 case WINED3D_RTYPE_BUFFER:
5575 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5577 if (&state->streams[i].buffer->resource == resource)
5579 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5580 state->streams[i].buffer = NULL;
5584 if (&state->index_buffer->resource == resource)
5586 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5587 state->index_buffer = NULL;
5589 break;
5591 default:
5592 break;
5595 /* Remove the resource from the resourceStore */
5596 device_resource_remove(device, resource);
5598 TRACE("Resource released.\n");
5601 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5603 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5604 struct wined3d_so_desc_entry, entry)->desc;
5605 const struct wined3d_stream_output_desc *k = key;
5606 unsigned int i;
5607 int ret;
5609 if ((ret = (k->element_count - desc->element_count)))
5610 return ret;
5611 if ((ret = (k->buffer_stride_count - desc->buffer_stride_count)))
5612 return ret;
5613 if ((ret = (k->rasterizer_stream_idx - desc->rasterizer_stream_idx)))
5614 return ret;
5616 for (i = 0; i < k->element_count; ++i)
5618 const struct wined3d_stream_output_element *b = &desc->elements[i];
5619 const struct wined3d_stream_output_element *a = &k->elements[i];
5621 if ((ret = (a->stream_idx - b->stream_idx)))
5622 return ret;
5623 if ((ret = strcmp(a->semantic_name, b->semantic_name)))
5624 return ret;
5625 if ((ret = (a->semantic_idx - b->semantic_idx)))
5626 return ret;
5627 if ((ret = (a->component_idx - b->component_idx)))
5628 return ret;
5629 if ((ret = (a->component_count - b->component_count)))
5630 return ret;
5631 if ((ret = (a->output_slot - b->output_slot)))
5632 return ret;
5635 for (i = 0; i < k->buffer_stride_count; ++i)
5637 if ((ret = (k->buffer_strides[i] - desc->buffer_strides[i])))
5638 return ret;
5641 return 0;
5644 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
5646 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
5648 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
5651 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
5653 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
5655 return memcmp(&state->desc, key, sizeof(state->desc));
5658 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
5660 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
5662 return memcmp(&state->desc, key, sizeof(state->desc));
5665 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
5667 const struct wined3d_depth_stencil_state *state
5668 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
5670 return memcmp(&state->desc, key, sizeof(state->desc));
5673 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
5674 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
5675 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
5676 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
5678 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
5679 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
5680 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5681 unsigned int i;
5682 HRESULT hr;
5684 device->ref = 1;
5685 device->wined3d = wined3d;
5686 wined3d_incref(device->wined3d);
5687 device->adapter = adapter;
5688 device->device_parent = device_parent;
5689 list_init(&device->resources);
5690 list_init(&device->shaders);
5691 device->surface_alignment = surface_alignment;
5693 /* Save the creation parameters. */
5694 device->create_parms.adapter_idx = adapter_idx;
5695 device->create_parms.device_type = device_type;
5696 device->create_parms.focus_window = focus_window;
5697 device->create_parms.flags = flags;
5699 device->shader_backend = adapter->shader_backend;
5701 vertex_pipeline = adapter->vertex_pipe;
5703 fragment_pipeline = adapter->fragment_pipe;
5705 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
5706 wine_rb_init(&device->samplers, wined3d_sampler_compare);
5707 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
5708 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
5709 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
5711 if (vertex_pipeline->vp_states && fragment_pipeline->states
5712 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
5713 &adapter->d3d_info, supported_extensions, vertex_pipeline,
5714 fragment_pipeline, adapter->misc_state_template)))
5716 ERR("Failed to compile state table, hr %#x.\n", hr);
5717 wine_rb_destroy(&device->samplers, NULL, NULL);
5718 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5719 wine_rb_destroy(&device->blend_states, NULL, NULL);
5720 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5721 wine_rb_destroy(&device->so_descs, NULL, NULL);
5722 wined3d_decref(device->wined3d);
5723 return hr;
5726 device->max_frame_latency = 3;
5728 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
5730 WARN("Failed to create command stream.\n");
5731 hr = E_FAIL;
5732 goto err;
5735 return WINED3D_OK;
5737 err:
5738 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
5740 heap_free(device->multistate_funcs[i]);
5742 wine_rb_destroy(&device->samplers, NULL, NULL);
5743 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
5744 wine_rb_destroy(&device->blend_states, NULL, NULL);
5745 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
5746 wine_rb_destroy(&device->so_descs, NULL, NULL);
5747 wined3d_decref(device->wined3d);
5748 return hr;
5751 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
5753 unsigned int representative, i, idx, shift;
5754 struct wined3d_context *context;
5756 wined3d_from_cs(device->cs);
5758 if (STATE_IS_COMPUTE(state_id))
5760 for (i = 0; i < device->context_count; ++i)
5761 context_invalidate_compute_state(device->contexts[i], state_id);
5762 return;
5765 representative = device->state_table[state_id].representative;
5766 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
5767 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
5768 for (i = 0; i < device->context_count; ++i)
5770 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
5774 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5775 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5777 if (message == WM_DESTROY)
5779 TRACE("unregister window %p.\n", window);
5780 wined3d_unregister_window(window);
5782 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5783 ERR("Window %p is not the focus window for device %p.\n", window, device);
5785 else if (message == WM_DISPLAYCHANGE)
5787 device->device_parent->ops->mode_changed(device->device_parent);
5789 else if (message == WM_ACTIVATEAPP)
5791 unsigned int i = device->swapchain_count;
5793 /* Deactivating the implicit swapchain may cause the application
5794 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5795 * deactivate the implicit swapchain last, and to avoid accessing the
5796 * "device" pointer afterwards. */
5797 while (i--)
5798 wined3d_swapchain_activate(device->swapchains[i], wparam);
5800 else if (message == WM_SYSCOMMAND)
5802 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
5804 if (unicode)
5805 DefWindowProcW(window, message, wparam, lparam);
5806 else
5807 DefWindowProcA(window, message, wparam, lparam);
5811 if (unicode)
5812 return CallWindowProcW(proc, window, message, wparam, lparam);
5813 else
5814 return CallWindowProcA(proc, window, message, wparam, lparam);