wined3d: Pass a wined3d_device_context to wined3d_cs_push_constants().
[wine.git] / dlls / wined3d / device.c
blobfc07c78bb430b661922a59bd153708ca2baf06ff
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #ifdef HAVE_FLOAT_H
31 # include <float.h>
32 #endif
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 struct wined3d_matrix_3x3
41 float _11, _12, _13;
42 float _21, _22, _23;
43 float _31, _32, _33;
46 struct light_transformed
48 struct wined3d_color diffuse, specular, ambient;
49 struct wined3d_vec4 position;
50 struct wined3d_vec3 direction;
51 float range, falloff, c_att, l_att, q_att, cos_htheta, cos_hphi;
54 struct lights_settings
56 struct light_transformed lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
57 struct wined3d_color ambient_light;
58 struct wined3d_matrix modelview_matrix;
59 struct wined3d_matrix_3x3 normal_matrix;
60 struct wined3d_vec4 position_transformed;
62 float fog_start, fog_end, fog_density;
64 uint32_t point_light_count : 8;
65 uint32_t spot_light_count : 8;
66 uint32_t directional_light_count : 8;
67 uint32_t parallel_point_light_count : 8;
68 uint32_t lighting : 1;
69 uint32_t legacy_lighting : 1;
70 uint32_t normalise : 1;
71 uint32_t localviewer : 1;
72 uint32_t fog_coord_mode : 2;
73 uint32_t fog_mode : 2;
74 uint32_t padding : 24;
77 /* Define the default light parameters as specified by MSDN. */
78 const struct wined3d_light WINED3D_default_light =
80 WINED3D_LIGHT_DIRECTIONAL, /* Type */
81 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
82 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
83 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
84 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
85 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
86 0.0f, /* Range */
87 0.0f, /* Falloff */
88 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
89 0.0f, /* Theta */
90 0.0f /* Phi */
93 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
95 struct wined3d_context **new_array;
97 TRACE("Adding context %p.\n", context);
99 if (!device->shader_backend->shader_allocate_context_data(context))
101 ERR("Failed to allocate shader backend context data.\n");
102 return FALSE;
104 device->shader_backend->shader_init_context_state(context);
106 if (!device->adapter->fragment_pipe->allocate_context_data(context))
108 ERR("Failed to allocate fragment pipeline context data.\n");
109 device->shader_backend->shader_free_context_data(context);
110 return FALSE;
113 if (!(new_array = heap_realloc(device->contexts, sizeof(*new_array) * (device->context_count + 1))))
115 ERR("Failed to grow the context array.\n");
116 device->adapter->fragment_pipe->free_context_data(context);
117 device->shader_backend->shader_free_context_data(context);
118 return FALSE;
121 new_array[device->context_count++] = context;
122 device->contexts = new_array;
124 return TRUE;
127 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
129 struct wined3d_context **new_array;
130 BOOL found = FALSE;
131 UINT i;
133 TRACE("Removing context %p.\n", context);
135 device->adapter->fragment_pipe->free_context_data(context);
136 device->shader_backend->shader_free_context_data(context);
138 for (i = 0; i < device->context_count; ++i)
140 if (device->contexts[i] == context)
142 found = TRUE;
143 break;
147 if (!found)
149 ERR("Context %p doesn't exist in context array.\n", context);
150 return;
153 if (!--device->context_count)
155 heap_free(device->contexts);
156 device->contexts = NULL;
157 return;
160 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
161 if (!(new_array = heap_realloc(device->contexts, device->context_count * sizeof(*device->contexts))))
163 ERR("Failed to shrink context array. Oh well.\n");
164 return;
167 device->contexts = new_array;
170 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
172 ULONG refcount = InterlockedIncrement(&device->ref);
174 TRACE("%p increasing refcount to %u.\n", device, refcount);
176 return refcount;
179 static void device_free_so_desc(struct wine_rb_entry *entry, void *context)
181 struct wined3d_so_desc_entry *s = WINE_RB_ENTRY_VALUE(entry, struct wined3d_so_desc_entry, entry);
183 heap_free(s);
186 static void device_leftover_sampler(struct wine_rb_entry *entry, void *context)
188 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
190 ERR("Leftover sampler %p.\n", sampler);
193 static void device_leftover_rasterizer_state(struct wine_rb_entry *entry, void *context)
195 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
197 ERR("Leftover rasterizer state %p.\n", state);
200 static void device_leftover_blend_state(struct wine_rb_entry *entry, void *context)
202 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
204 ERR("Leftover blend state %p.\n", blend_state);
207 static void device_leftover_depth_stencil_state(struct wine_rb_entry *entry, void *context)
209 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
211 ERR("Leftover depth/stencil state %p.\n", state);
214 void wined3d_device_cleanup(struct wined3d_device *device)
216 unsigned int i;
218 if (device->swapchain_count)
219 wined3d_device_uninit_3d(device);
221 wined3d_cs_destroy(device->cs);
223 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
225 heap_free(device->multistate_funcs[i]);
226 device->multistate_funcs[i] = NULL;
229 if (!list_empty(&device->resources))
231 struct wined3d_resource *resource;
233 ERR("Device released with resources still bound.\n");
235 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
237 ERR("Leftover resource %p with type %s (%#x).\n",
238 resource, debug_d3dresourcetype(resource->type), resource->type);
242 if (device->contexts)
243 ERR("Context array not freed!\n");
244 if (device->hardwareCursor)
245 DestroyCursor(device->hardwareCursor);
246 device->hardwareCursor = 0;
248 wine_rb_destroy(&device->samplers, device_leftover_sampler, NULL);
249 wine_rb_destroy(&device->rasterizer_states, device_leftover_rasterizer_state, NULL);
250 wine_rb_destroy(&device->blend_states, device_leftover_blend_state, NULL);
251 wine_rb_destroy(&device->depth_stencil_states, device_leftover_depth_stencil_state, NULL);
252 wine_rb_destroy(&device->so_descs, device_free_so_desc, NULL);
254 wined3d_decref(device->wined3d);
255 device->wined3d = NULL;
258 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
260 ULONG refcount = InterlockedDecrement(&device->ref);
262 TRACE("%p decreasing refcount to %u.\n", device, refcount);
264 if (!refcount)
266 device->adapter->adapter_ops->adapter_destroy_device(device);
267 TRACE("Destroyed device %p.\n", device);
270 return refcount;
273 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
275 TRACE("device %p.\n", device);
277 return device->swapchain_count;
280 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
282 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
284 if (swapchain_idx >= device->swapchain_count)
286 WARN("swapchain_idx %u >= swapchain_count %u.\n",
287 swapchain_idx, device->swapchain_count);
288 return NULL;
291 return device->swapchains[swapchain_idx];
294 static void device_load_logo(struct wined3d_device *device, const char *filename)
296 struct wined3d_color_key color_key;
297 struct wined3d_resource_desc desc;
298 HBITMAP hbm;
299 BITMAP bm;
300 HRESULT hr;
301 HDC dcb = NULL, dcs = NULL;
303 if (!(hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)))
305 ERR_(winediag)("Failed to load logo %s.\n", wine_dbgstr_a(filename));
306 return;
308 GetObjectA(hbm, sizeof(BITMAP), &bm);
310 if (!(dcb = CreateCompatibleDC(NULL)))
311 goto out;
312 SelectObject(dcb, hbm);
314 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
315 desc.format = WINED3DFMT_B5G6R5_UNORM;
316 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
317 desc.multisample_quality = 0;
318 desc.usage = WINED3DUSAGE_DYNAMIC;
319 desc.bind_flags = 0;
320 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
321 desc.width = bm.bmWidth;
322 desc.height = bm.bmHeight;
323 desc.depth = 1;
324 desc.size = 0;
325 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, WINED3D_TEXTURE_CREATE_GET_DC,
326 NULL, NULL, &wined3d_null_parent_ops, &device->logo_texture)))
328 ERR("Wine logo requested, but failed to create texture, hr %#x.\n", hr);
329 goto out;
332 if (FAILED(hr = wined3d_texture_get_dc(device->logo_texture, 0, &dcs)))
334 wined3d_texture_decref(device->logo_texture);
335 device->logo_texture = NULL;
336 goto out;
338 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
339 wined3d_texture_release_dc(device->logo_texture, 0, dcs);
341 color_key.color_space_low_value = 0;
342 color_key.color_space_high_value = 0;
343 wined3d_texture_set_color_key(device->logo_texture, WINED3D_CKEY_SRC_BLT, &color_key);
345 out:
346 if (dcb) DeleteDC(dcb);
347 if (hbm) DeleteObject(hbm);
350 /* Context activation is done by the caller. */
351 static void wined3d_device_gl_create_dummy_textures(struct wined3d_device_gl *device_gl,
352 struct wined3d_context_gl *context_gl)
354 struct wined3d_dummy_textures *textures = &device_gl->dummy_textures;
355 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
356 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
357 unsigned int i;
358 DWORD color;
360 if (d3d_info->wined3d_creation_flags & WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR)
361 color = 0x000000ff;
362 else
363 color = 0x00000000;
365 /* Under DirectX you can sample even if no texture is bound, whereas
366 * OpenGL will only allow that when a valid texture is bound.
367 * We emulate this by creating dummy textures and binding them
368 * to each texture stage when the currently set D3D texture is NULL. */
369 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
371 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d);
372 TRACE("Dummy 1D texture given name %u.\n", textures->tex_1d);
373 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
374 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 1, 0,
375 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
377 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d);
378 TRACE("Dummy 2D texture given name %u.\n", textures->tex_2d);
379 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
380 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
381 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
383 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
385 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_rect);
386 TRACE("Dummy rectangle texture given name %u.\n", textures->tex_rect);
387 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
388 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
389 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
392 if (gl_info->supported[EXT_TEXTURE3D])
394 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_3d);
395 TRACE("Dummy 3D texture given name %u.\n", textures->tex_3d);
396 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
397 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0,
398 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
401 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
403 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube);
404 TRACE("Dummy cube texture given name %u.\n", textures->tex_cube);
405 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
406 for (i = GL_TEXTURE_CUBE_MAP_POSITIVE_X; i <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++i)
408 gl_info->gl_ops.gl.p_glTexImage2D(i, 0, GL_RGBA8, 1, 1, 0,
409 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
413 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
415 DWORD cube_array_data[6];
417 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_cube_array);
418 TRACE("Dummy cube array texture given name %u.\n", textures->tex_cube_array);
419 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
420 for (i = 0; i < ARRAY_SIZE(cube_array_data); ++i)
421 cube_array_data[i] = color;
422 GL_EXTCALL(glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_RGBA8, 1, 1, 6, 0,
423 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, cube_array_data));
426 if (gl_info->supported[EXT_TEXTURE_ARRAY])
428 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_1d_array);
429 TRACE("Dummy 1D array texture given name %u.\n", textures->tex_1d_array);
430 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
431 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_RGBA8, 1, 1, 0,
432 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
434 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_array);
435 TRACE("Dummy 2D array texture given name %u.\n", textures->tex_2d_array);
436 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
437 GL_EXTCALL(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 1, 1, 1, 0,
438 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
441 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
443 GLuint buffer;
445 GL_EXTCALL(glGenBuffers(1, &buffer));
446 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, buffer));
447 GL_EXTCALL(glBufferData(GL_TEXTURE_BUFFER, sizeof(color), &color, GL_STATIC_DRAW));
448 GL_EXTCALL(glBindBuffer(GL_TEXTURE_BUFFER, 0));
450 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_buffer);
451 TRACE("Dummy buffer texture given name %u.\n", textures->tex_buffer);
452 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
453 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, buffer));
454 GL_EXTCALL(glDeleteBuffers(1, &buffer));
457 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
459 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms);
460 TRACE("Dummy multisample texture given name %u.\n", textures->tex_2d_ms);
461 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
462 GL_EXTCALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 1, 1, GL_TRUE));
464 gl_info->gl_ops.gl.p_glGenTextures(1, &textures->tex_2d_ms_array);
465 TRACE("Dummy multisample array texture given name %u.\n", textures->tex_2d_ms_array);
466 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
467 GL_EXTCALL(glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 1, 1, 1, GL_TRUE));
469 if (gl_info->supported[ARB_CLEAR_TEXTURE])
471 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
472 GL_EXTCALL(glClearTexImage(textures->tex_2d_ms_array, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
474 else
476 WARN("ARB_clear_texture is currently required to clear dummy multisample textures.\n");
480 checkGLcall("create dummy textures");
482 wined3d_context_gl_bind_dummy_textures(context_gl);
485 /* Context activation is done by the caller. */
486 static void wined3d_device_gl_destroy_dummy_textures(struct wined3d_device_gl *device_gl,
487 struct wined3d_context_gl *context_gl)
489 struct wined3d_dummy_textures *dummy_textures = &device_gl->dummy_textures;
490 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
492 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
494 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms);
495 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_ms_array);
498 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
499 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_buffer);
501 if (gl_info->supported[EXT_TEXTURE_ARRAY])
503 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d_array);
504 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d_array);
507 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
508 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube_array);
510 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
511 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_cube);
513 if (gl_info->supported[EXT_TEXTURE3D])
514 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_3d);
516 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
517 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_rect);
519 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_2d);
520 gl_info->gl_ops.gl.p_glDeleteTextures(1, &dummy_textures->tex_1d);
522 checkGLcall("delete dummy textures");
524 memset(dummy_textures, 0, sizeof(*dummy_textures));
527 /* Context activation is done by the caller. */
528 void wined3d_device_create_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
530 struct wined3d_sampler_desc desc;
531 HRESULT hr;
533 desc.address_u = WINED3D_TADDRESS_WRAP;
534 desc.address_v = WINED3D_TADDRESS_WRAP;
535 desc.address_w = WINED3D_TADDRESS_WRAP;
536 memset(desc.border_color, 0, sizeof(desc.border_color));
537 desc.mag_filter = WINED3D_TEXF_POINT;
538 desc.min_filter = WINED3D_TEXF_POINT;
539 desc.mip_filter = WINED3D_TEXF_NONE;
540 desc.lod_bias = 0.0f;
541 desc.min_lod = -1000.0f;
542 desc.max_lod = 1000.0f;
543 desc.mip_base_level = 0;
544 desc.max_anisotropy = 1;
545 desc.compare = FALSE;
546 desc.comparison_func = WINED3D_CMP_NEVER;
547 desc.srgb_decode = TRUE;
549 /* In SM4+ shaders there is a separation between resources and samplers. Some shader
550 * instructions allow access to resources without using samplers.
551 * In GLSL, resources are always accessed through sampler or image variables. The default
552 * sampler object is used to emulate the direct resource access when there is no sampler state
553 * to use.
555 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->default_sampler)))
557 ERR("Failed to create default sampler, hr %#x.\n", hr);
558 device->default_sampler = NULL;
561 /* In D3D10+, a NULL sampler maps to the default sampler state. */
562 desc.address_u = WINED3D_TADDRESS_CLAMP;
563 desc.address_v = WINED3D_TADDRESS_CLAMP;
564 desc.address_w = WINED3D_TADDRESS_CLAMP;
565 desc.mag_filter = WINED3D_TEXF_LINEAR;
566 desc.min_filter = WINED3D_TEXF_LINEAR;
567 desc.mip_filter = WINED3D_TEXF_LINEAR;
568 if (FAILED(hr = wined3d_sampler_create(device, &desc, NULL, &wined3d_null_parent_ops, &device->null_sampler)))
570 ERR("Failed to create null sampler, hr %#x.\n", hr);
571 device->null_sampler = NULL;
575 /* Context activation is done by the caller. */
576 void wined3d_device_destroy_default_samplers(struct wined3d_device *device, struct wined3d_context *context)
578 wined3d_sampler_decref(device->default_sampler);
579 device->default_sampler = NULL;
580 wined3d_sampler_decref(device->null_sampler);
581 device->null_sampler = NULL;
584 static bool wined3d_null_image_vk_init(struct wined3d_image_vk *image, struct wined3d_context_vk *context_vk,
585 VkCommandBuffer vk_command_buffer, VkImageType type, unsigned int layer_count, unsigned int sample_count)
587 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
588 VkImageSubresourceRange range;
589 uint32_t flags = 0;
591 static const VkClearColorValue colour = {{0}};
593 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
594 image, context_vk, vk_command_buffer, type, layer_count, sample_count);
596 if (type == VK_IMAGE_TYPE_2D && layer_count >= 6)
597 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
599 if (!wined3d_context_vk_create_image(context_vk, type,
600 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_FORMAT_R8G8B8A8_UNORM,
601 1, 1, 1, sample_count, 1, layer_count, flags, image))
603 return false;
606 wined3d_context_vk_reference_image(context_vk, image);
608 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
609 range.baseMipLevel = 0;
610 range.levelCount = 1;
611 range.baseArrayLayer = 0;
612 range.layerCount = layer_count;
614 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
615 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
616 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image->vk_image, &range);
618 VK_CALL(vkCmdClearColorImage(vk_command_buffer, image->vk_image,
619 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour, 1, &range));
621 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
622 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0,
623 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, image->vk_image, &range);
625 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
626 wine_dbgstr_longlong(image->vk_image), wine_dbgstr_longlong(image->vk_memory));
628 return true;
631 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk *device_vk,
632 struct wined3d_context_vk *context_vk)
634 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
635 const struct wined3d_vk_info *vk_info;
636 const struct wined3d_format *format;
637 VkMemoryPropertyFlags memory_type;
638 VkCommandBuffer vk_command_buffer;
639 unsigned int sample_count = 2;
640 VkBufferUsageFlags usage;
642 format = wined3d_get_format(device_vk->d.adapter, WINED3DFMT_R8G8B8A8_UNORM, WINED3D_BIND_SHADER_RESOURCE);
643 while (sample_count && !(sample_count & format->multisample_types))
644 sample_count <<= 1;
646 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
648 ERR("Failed to get command buffer.\n");
649 return false;
652 vk_info = context_vk->vk_info;
654 usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
655 | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
656 memory_type = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
657 if (!wined3d_context_vk_create_bo(context_vk, 16, usage, memory_type, &r->bo))
658 return false;
659 VK_CALL(vkCmdFillBuffer(vk_command_buffer, r->bo.vk_buffer, r->bo.buffer_offset, r->bo.size, 0x00000000u));
660 r->buffer_info.buffer = r->bo.vk_buffer;
661 r->buffer_info.offset = r->bo.buffer_offset;
662 r->buffer_info.range = r->bo.size;
664 if (!wined3d_null_image_vk_init(&r->image_1d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_1D, 1, 1))
666 ERR("Failed to create 1D image.\n");
667 goto fail;
670 if (!wined3d_null_image_vk_init(&r->image_2d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 6, 1))
672 ERR("Failed to create 2D image.\n");
673 goto fail;
676 if (!wined3d_null_image_vk_init(&r->image_2dms, context_vk, vk_command_buffer, VK_IMAGE_TYPE_2D, 1, sample_count))
678 ERR("Failed to create 2D MSAA image.\n");
679 goto fail;
682 if (!wined3d_null_image_vk_init(&r->image_3d, context_vk, vk_command_buffer, VK_IMAGE_TYPE_3D, 1, 1))
684 ERR("Failed to create 3D image.\n");
685 goto fail;
688 return true;
690 fail:
691 if (r->image_2dms.vk_image)
692 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
693 if (r->image_2d.vk_image)
694 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
695 if (r->image_1d.vk_image)
696 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
697 wined3d_context_vk_reference_bo(context_vk, &r->bo);
698 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
699 return false;
702 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk *device_vk,
703 struct wined3d_context_vk *context_vk)
705 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
707 /* We don't track command buffer references to NULL resources. We easily
708 * could, but it doesn't seem worth it. */
709 wined3d_context_vk_reference_image(context_vk, &r->image_3d);
710 wined3d_context_vk_destroy_image(context_vk, &r->image_3d);
711 wined3d_context_vk_reference_image(context_vk, &r->image_2dms);
712 wined3d_context_vk_destroy_image(context_vk, &r->image_2dms);
713 wined3d_context_vk_reference_image(context_vk, &r->image_2d);
714 wined3d_context_vk_destroy_image(context_vk, &r->image_2d);
715 wined3d_context_vk_reference_image(context_vk, &r->image_1d);
716 wined3d_context_vk_destroy_image(context_vk, &r->image_1d);
717 wined3d_context_vk_reference_bo(context_vk, &r->bo);
718 wined3d_context_vk_destroy_bo(context_vk, &r->bo);
721 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
723 struct wined3d_null_resources_vk *r = &device_vk->null_resources_vk;
724 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
725 VkBufferViewCreateInfo buffer_create_info;
726 const struct wined3d_vk_info *vk_info;
727 VkImageViewCreateInfo view_desc;
728 VkResult vr;
730 vk_info = context_vk->vk_info;
732 buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
733 buffer_create_info.pNext = NULL;
734 buffer_create_info.flags = 0;
735 buffer_create_info.buffer = r->bo.vk_buffer;
736 buffer_create_info.format = VK_FORMAT_R32_UINT;
737 buffer_create_info.offset = r->bo.buffer_offset;
738 buffer_create_info.range = r->bo.size;
740 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
741 &buffer_create_info, NULL, &v->vk_view_buffer_uint))) < 0)
743 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
744 return false;
746 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_uint));
748 buffer_create_info.format = VK_FORMAT_R32G32B32A32_SFLOAT;
749 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
750 &buffer_create_info, NULL, &v->vk_view_buffer_float))) < 0)
752 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
753 goto fail;
755 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v->vk_view_buffer_float));
757 view_desc.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
758 view_desc.pNext = NULL;
759 view_desc.flags = 0;
760 view_desc.image = r->image_1d.vk_image;
761 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D;
762 view_desc.format = VK_FORMAT_R8G8B8A8_UNORM;
763 view_desc.components.r = VK_COMPONENT_SWIZZLE_ZERO;
764 view_desc.components.g = VK_COMPONENT_SWIZZLE_ZERO;
765 view_desc.components.b = VK_COMPONENT_SWIZZLE_ZERO;
766 view_desc.components.a = VK_COMPONENT_SWIZZLE_ZERO;
767 view_desc.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
768 view_desc.subresourceRange.baseMipLevel = 0;
769 view_desc.subresourceRange.levelCount = 1;
770 view_desc.subresourceRange.baseArrayLayer = 0;
771 view_desc.subresourceRange.layerCount = 1;
772 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d.imageView))) < 0)
774 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
775 goto fail;
777 v->vk_info_1d.sampler = VK_NULL_HANDLE;
778 v->vk_info_1d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
779 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d.imageView));
781 view_desc.viewType = VK_IMAGE_VIEW_TYPE_1D_ARRAY;
782 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_1d_array.imageView))) < 0)
784 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr));
785 goto fail;
787 v->vk_info_1d_array.sampler = VK_NULL_HANDLE;
788 v->vk_info_1d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
789 TRACE("Created 1D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_1d_array.imageView));
791 view_desc.image = r->image_2d.vk_image;
792 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
793 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
795 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
796 goto fail;
798 v->vk_info_2d.sampler = VK_NULL_HANDLE;
799 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
800 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
802 view_desc.image = r->image_2dms.vk_image;
803 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
804 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
806 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
807 goto fail;
809 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
810 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
811 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
813 view_desc.image = r->image_3d.vk_image;
814 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
815 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
817 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
818 goto fail;
820 v->vk_info_3d.sampler = VK_NULL_HANDLE;
821 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
822 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
824 view_desc.image = r->image_2d.vk_image;
825 view_desc.subresourceRange.layerCount = 6;
826 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
827 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
829 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
830 goto fail;
832 v->vk_info_cube.sampler = VK_NULL_HANDLE;
833 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
834 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
836 view_desc.subresourceRange.layerCount = 1;
837 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
838 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
840 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
841 goto fail;
843 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
844 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
845 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
847 view_desc.image = r->image_2dms.vk_image;
848 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
849 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
851 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
852 goto fail;
854 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
855 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
856 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
858 return true;
860 fail:
861 if (v->vk_info_2d_array.imageView)
862 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
863 if (v->vk_info_cube.imageView)
864 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
865 if (v->vk_info_3d.imageView)
866 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
867 if (v->vk_info_2dms.imageView)
868 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
869 if (v->vk_info_2d.imageView)
870 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
871 if (v->vk_info_1d_array.imageView)
872 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d_array.imageView, NULL));
873 if (v->vk_info_1d.imageView)
874 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
875 if (v->vk_view_buffer_float)
876 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
877 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
878 return false;
881 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
883 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
884 uint64_t id = context_vk->current_command_buffer.id;
886 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
887 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
888 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
889 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
890 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
891 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
892 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d_array.imageView, id);
893 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
895 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
896 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
899 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
901 unsigned int screensaver_active;
903 TRACE("device %p, window %p.\n", device, window);
905 if (!wined3d_register_window(NULL, window, device, 0))
907 ERR("Failed to register window %p.\n", window);
908 return E_FAIL;
911 InterlockedExchangePointer((void **)&device->focus_window, window);
912 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
913 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
914 if ((device->restore_screensaver = !!screensaver_active))
915 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
917 return WINED3D_OK;
920 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
922 TRACE("device %p.\n", device);
924 if (device->focus_window) wined3d_unregister_window(device->focus_window);
925 InterlockedExchangePointer((void **)&device->focus_window, NULL);
926 if (device->restore_screensaver)
928 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
929 device->restore_screensaver = FALSE;
933 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
935 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
936 unsigned int i;
938 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
940 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
942 if (device->back_buffer_view)
943 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
945 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
948 void wined3d_device_delete_opengl_contexts_cs(void *object)
950 struct wined3d_swapchain_gl *swapchain_gl;
951 struct wined3d_device *device = object;
952 struct wined3d_context_gl *context_gl;
953 struct wined3d_device_gl *device_gl;
954 struct wined3d_context *context;
955 struct wined3d_shader *shader;
957 TRACE("device %p.\n", device);
959 device_gl = wined3d_device_gl(device);
961 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
963 device->shader_backend->shader_destroy(shader);
966 context = context_acquire(device, NULL, 0);
967 context_gl = wined3d_context_gl(context);
968 device->blitter->ops->blitter_destroy(device->blitter, context);
969 device->shader_backend->shader_free_private(device, context);
970 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
971 wined3d_device_destroy_default_samplers(device, context);
972 context_release(context);
974 while (device->context_count)
976 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
977 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
978 else
979 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
983 void wined3d_device_create_primary_opengl_context_cs(void *object)
985 struct wined3d_device *device = object;
986 struct wined3d_context_gl *context_gl;
987 struct wined3d_swapchain *swapchain;
988 struct wined3d_context *context;
989 struct wined3d_texture *target;
990 HRESULT hr;
992 TRACE("device %p.\n", device);
994 swapchain = device->swapchains[0];
995 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
996 if (!(context = context_acquire(device, target, 0)))
998 WARN("Failed to acquire context.\n");
999 return;
1002 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1003 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1005 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
1006 context_release(context);
1007 return;
1010 if (!(device->blitter = wined3d_cpu_blitter_create()))
1012 ERR("Failed to create CPU blitter.\n");
1013 device->shader_backend->shader_free_private(device, NULL);
1014 context_release(context);
1015 return;
1018 context_gl = wined3d_context_gl(context);
1020 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1021 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1022 wined3d_arbfp_blitter_create(&device->blitter, device);
1023 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1024 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1026 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device), context_gl);
1027 wined3d_device_create_default_samplers(device, context);
1028 context_release(context);
1031 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1033 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1034 const struct wined3d_swapchain_desc *swapchain_desc;
1035 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1036 DWORD clear_flags = 0;
1037 unsigned int i;
1038 HRESULT hr;
1040 TRACE("device %p, swapchain %p.\n", device, swapchain);
1042 if (device->d3d_initialized)
1043 return WINED3DERR_INVALIDCALL;
1045 device->swapchain_count = 1;
1046 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1048 ERR("Failed to allocate swapchain array.\n");
1049 hr = E_OUTOFMEMORY;
1050 goto err_out;
1052 device->swapchains[0] = swapchain;
1054 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1056 if (fb->render_targets[i])
1057 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1059 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1061 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1062 goto err_out;
1063 device->d3d_initialized = TRUE;
1065 swapchain_desc = &swapchain->state.desc;
1066 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1068 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1069 struct wined3d_view_desc view_desc;
1071 view_desc.format_id = back_buffer->format->id;
1072 view_desc.flags = 0;
1073 view_desc.u.texture.level_idx = 0;
1074 view_desc.u.texture.level_count = 1;
1075 view_desc.u.texture.layer_idx = 0;
1076 view_desc.u.texture.layer_count = 1;
1077 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1078 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1080 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1081 device->adapter->adapter_ops->adapter_uninit_3d(device);
1082 device->d3d_initialized = FALSE;
1083 goto err_out;
1087 device_init_swapchain_state(device, swapchain);
1089 TRACE("All defaults now set up.\n");
1091 /* Clear the screen. */
1092 if (device->back_buffer_view)
1093 clear_flags |= WINED3DCLEAR_TARGET;
1094 if (swapchain_desc->enable_auto_depth_stencil)
1095 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1096 if (clear_flags)
1097 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1099 if (wined3d_settings.logo)
1100 device_load_logo(device, wined3d_settings.logo);
1102 return WINED3D_OK;
1104 err_out:
1105 heap_free(device->swapchains);
1106 device->swapchains = NULL;
1107 device->swapchain_count = 0;
1109 return hr;
1112 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1114 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1116 wined3d_sampler_decref(sampler);
1119 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1121 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1123 wined3d_rasterizer_state_decref(state);
1126 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1128 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1130 wined3d_blend_state_decref(blend_state);
1133 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1135 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1137 wined3d_depth_stencil_state_decref(state);
1140 void wined3d_device_uninit_3d(struct wined3d_device *device)
1142 struct wined3d_state *state = device->cs->c.state;
1143 struct wined3d_resource *resource, *cursor;
1144 struct wined3d_rendertarget_view *view;
1145 struct wined3d_texture *texture;
1147 TRACE("device %p.\n", device);
1149 if (!device->d3d_initialized)
1151 ERR("Called while 3D support was not initialised.\n");
1152 return;
1155 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1157 device->swapchain_count = 0;
1159 if ((texture = device->logo_texture))
1161 device->logo_texture = NULL;
1162 wined3d_texture_decref(texture);
1165 if ((texture = device->cursor_texture))
1167 device->cursor_texture = NULL;
1168 wined3d_texture_decref(texture);
1171 wined3d_cs_emit_reset_state(device->cs);
1172 state_cleanup(state);
1174 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1175 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1176 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1177 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1179 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1181 TRACE("Unloading resource %p.\n", resource);
1182 wined3d_cs_emit_unload_resource(device->cs, resource);
1185 device->adapter->adapter_ops->adapter_uninit_3d(device);
1186 device->d3d_initialized = FALSE;
1188 if ((view = device->auto_depth_stencil_view))
1190 device->auto_depth_stencil_view = NULL;
1191 if (wined3d_rendertarget_view_decref(view))
1192 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1195 if ((view = device->back_buffer_view))
1197 device->back_buffer_view = NULL;
1198 wined3d_rendertarget_view_decref(view);
1201 heap_free(device->swapchains);
1202 device->swapchains = NULL;
1204 wined3d_state_reset(state, &device->adapter->d3d_info);
1207 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1208 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1209 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1211 * There is no way to deactivate thread safety once it is enabled.
1213 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1215 TRACE("device %p.\n", device);
1217 /* For now just store the flag (needed in case of ddraw). */
1218 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1221 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1223 const struct wined3d_driver_info *driver_info;
1225 TRACE("device %p.\n", device);
1227 driver_info = &device->adapter->driver_info;
1229 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1230 wine_dbgstr_longlong(driver_info->vram_bytes),
1231 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1232 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1234 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1237 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1238 struct wined3d_buffer *buffer, UINT offset)
1240 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1242 wined3d_device_context_set_stream_output(&device->cs->c, idx, buffer, offset);
1245 struct wined3d_buffer * CDECL wined3d_device_context_get_stream_output(struct wined3d_device_context *context,
1246 unsigned int idx, unsigned int *offset)
1248 TRACE("context %p, idx %u, offset %p.\n", context, idx, offset);
1250 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1252 WARN("Invalid stream output %u.\n", idx);
1253 return NULL;
1256 if (offset)
1257 *offset = context->state->stream_output[idx].offset;
1258 return context->state->stream_output[idx].buffer;
1261 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1262 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1264 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1265 device, stream_idx, buffer, offset, stride);
1267 return wined3d_device_context_set_stream_source(&device->cs->c, stream_idx, buffer, offset, stride);
1270 HRESULT CDECL wined3d_device_context_get_stream_source(const struct wined3d_device_context *context,
1271 unsigned int stream_idx, struct wined3d_buffer **buffer, unsigned int *offset, unsigned int *stride)
1273 const struct wined3d_stream_state *stream;
1275 TRACE("context %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1276 context, stream_idx, buffer, offset, stride);
1278 if (stream_idx >= WINED3D_MAX_STREAMS)
1280 WARN("Stream index %u out of range.\n", stream_idx);
1281 return WINED3DERR_INVALIDCALL;
1284 stream = &context->state->streams[stream_idx];
1285 *buffer = stream->buffer;
1286 if (offset)
1287 *offset = stream->offset;
1288 *stride = stream->stride;
1290 return WINED3D_OK;
1293 static void wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1295 struct wined3d_stream_state *stream;
1296 UINT old_flags, old_freq;
1298 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1300 stream = &device->cs->c.state->streams[stream_idx];
1301 old_flags = stream->flags;
1302 old_freq = stream->frequency;
1304 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1305 stream->frequency = divider & 0x7fffff;
1306 if (stream->frequency != old_freq || stream->flags != old_flags)
1307 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1310 static void wined3d_device_set_transform(struct wined3d_device *device,
1311 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1313 TRACE("device %p, state %s, matrix %p.\n",
1314 device, debug_d3dtstype(state), matrix);
1315 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1316 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1317 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1318 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1320 /* If the new matrix is the same as the current one,
1321 * we cut off any further processing. this seems to be a reasonable
1322 * optimization because as was noticed, some apps (warcraft3 for example)
1323 * tend towards setting the same matrix repeatedly for some reason.
1325 * From here on we assume that the new matrix is different, wherever it matters. */
1326 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1328 TRACE("The application is setting the same matrix over again.\n");
1329 return;
1332 device->cs->c.state->transforms[state] = *matrix;
1333 wined3d_cs_emit_set_transform(device->cs, state, matrix);
1336 static void wined3d_device_get_transform(const struct wined3d_device *device,
1337 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1339 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1341 *matrix = device->cs->c.state->transforms[state];
1344 /* Note lights are real special cases. Although the device caps state only
1345 * e.g. 8 are supported, you can reference any indexes you want as long as
1346 * that number max are enabled at any one point in time. Therefore since the
1347 * indices can be anything, we need a hashmap of them. However, this causes
1348 * stateblock problems. When capturing the state block, I duplicate the
1349 * hashmap, but when recording, just build a chain pretty much of commands to
1350 * be replayed. */
1351 static void wined3d_device_set_light(struct wined3d_device *device,
1352 UINT light_idx, const struct wined3d_light *light)
1354 struct wined3d_light_info *object = NULL;
1355 float rho;
1357 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1359 if (FAILED(wined3d_light_state_set_light(&device->cs->c.state->light_state, light_idx, light, &object)))
1360 return;
1362 /* Initialize the object. */
1363 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1364 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1365 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1366 light_idx, light->type, debug_color(&light->diffuse),
1367 debug_color(&light->specular), debug_color(&light->ambient),
1368 light->position.x, light->position.y, light->position.z,
1369 light->direction.x, light->direction.y, light->direction.z,
1370 light->range, light->falloff, light->theta, light->phi);
1372 switch (light->type)
1374 case WINED3D_LIGHT_POINT:
1375 /* Position */
1376 object->position.x = light->position.x;
1377 object->position.y = light->position.y;
1378 object->position.z = light->position.z;
1379 object->position.w = 1.0f;
1380 object->cutoff = 180.0f;
1381 /* FIXME: Range */
1382 break;
1384 case WINED3D_LIGHT_DIRECTIONAL:
1385 /* Direction */
1386 object->direction.x = -light->direction.x;
1387 object->direction.y = -light->direction.y;
1388 object->direction.z = -light->direction.z;
1389 object->direction.w = 0.0f;
1390 object->exponent = 0.0f;
1391 object->cutoff = 180.0f;
1392 break;
1394 case WINED3D_LIGHT_SPOT:
1395 /* Position */
1396 object->position.x = light->position.x;
1397 object->position.y = light->position.y;
1398 object->position.z = light->position.z;
1399 object->position.w = 1.0f;
1401 /* Direction */
1402 object->direction.x = light->direction.x;
1403 object->direction.y = light->direction.y;
1404 object->direction.z = light->direction.z;
1405 object->direction.w = 0.0f;
1407 /* opengl-ish and d3d-ish spot lights use too different models
1408 * for the light "intensity" as a function of the angle towards
1409 * the main light direction, so we only can approximate very
1410 * roughly. However, spot lights are rather rarely used in games
1411 * (if ever used at all). Furthermore if still used, probably
1412 * nobody pays attention to such details. */
1413 if (!light->falloff)
1415 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1416 * equations have the falloff resp. exponent parameter as an
1417 * exponent, so the spot light lighting will always be 1.0 for
1418 * both of them, and we don't have to care for the rest of the
1419 * rather complex calculation. */
1420 object->exponent = 0.0f;
1422 else
1424 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1425 if (rho < 0.0001f)
1426 rho = 0.0001f;
1427 object->exponent = -0.3f / logf(cosf(rho / 2));
1430 if (object->exponent > 128.0f)
1431 object->exponent = 128.0f;
1433 object->cutoff = (float)(light->phi * 90 / M_PI);
1434 /* FIXME: Range */
1435 break;
1437 case WINED3D_LIGHT_PARALLELPOINT:
1438 object->position.x = light->position.x;
1439 object->position.y = light->position.y;
1440 object->position.z = light->position.z;
1441 object->position.w = 1.0f;
1442 break;
1444 default:
1445 FIXME("Unrecognized light type %#x.\n", light->type);
1448 wined3d_cs_emit_set_light(device->cs, object);
1451 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1453 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1454 struct wined3d_light_info *light_info;
1456 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1458 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1459 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1461 TRACE("Light enabled requested but light not defined, so defining one!\n");
1462 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1464 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1466 FIXME("Adding default lights has failed dismally\n");
1467 return;
1471 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1472 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1475 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1476 UINT plane_idx, const struct wined3d_vec4 *plane)
1478 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1480 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1482 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1484 TRACE("Application has requested clipplane this device doesn't support.\n");
1485 return WINED3DERR_INVALIDCALL;
1488 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1490 TRACE("Application is setting old values over, nothing to do.\n");
1491 return WINED3D_OK;
1494 clip_planes[plane_idx] = *plane;
1496 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1498 return WINED3D_OK;
1501 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1502 const struct wined3d_clip_status *clip_status)
1504 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1506 if (!clip_status)
1507 return WINED3DERR_INVALIDCALL;
1509 return WINED3D_OK;
1512 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1513 struct wined3d_clip_status *clip_status)
1515 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1517 if (!clip_status)
1518 return WINED3DERR_INVALIDCALL;
1520 return WINED3D_OK;
1523 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1525 TRACE("device %p, material %p.\n", device, material);
1527 device->cs->c.state->material = *material;
1528 wined3d_cs_emit_set_material(device->cs, material);
1531 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1532 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1534 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1535 device, buffer, debug_d3dformat(format_id), offset);
1537 wined3d_device_context_set_index_buffer(&device->cs->c, buffer, format_id, offset);
1540 struct wined3d_buffer * CDECL wined3d_device_context_get_index_buffer(const struct wined3d_device_context *context,
1541 enum wined3d_format_id *format, unsigned int *offset)
1543 const struct wined3d_state *state = context->state;
1545 TRACE("context %p, format %p, offset %p.\n", context, format, offset);
1547 *format = state->index_format;
1548 if (offset)
1549 *offset = state->index_offset;
1550 return state->index_buffer;
1553 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1555 TRACE("device %p, base_index %d.\n", device, base_index);
1557 device->cs->c.state->base_vertex_index = base_index;
1560 void CDECL wined3d_device_set_viewports(struct wined3d_device *device, unsigned int viewport_count,
1561 const struct wined3d_viewport *viewports)
1563 unsigned int i;
1565 TRACE("device %p, viewport_count %u, viewports %p.\n", device, viewport_count, viewports);
1567 for (i = 0; i < viewport_count; ++i)
1569 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1570 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1573 wined3d_device_context_set_viewports(&device->cs->c, viewport_count, viewports);
1576 void CDECL wined3d_device_context_get_viewports(const struct wined3d_device_context *context,
1577 unsigned int *viewport_count, struct wined3d_viewport *viewports)
1579 const struct wined3d_state *state = context->state;
1580 unsigned int count;
1582 TRACE("context %p, viewport_count %p, viewports %p.\n", context, viewport_count, viewports);
1584 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1585 if (count && viewports)
1586 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1587 if (viewport_count)
1588 *viewport_count = state->viewport_count;
1591 static void resolve_depth_buffer(struct wined3d_device *device)
1593 const struct wined3d_state *state = device->cs->c.state;
1594 struct wined3d_rendertarget_view *src_view;
1595 struct wined3d_resource *dst_resource;
1596 struct wined3d_texture *dst_texture;
1598 if (!(dst_texture = state->textures[0]))
1599 return;
1600 dst_resource = &dst_texture->resource;
1601 if (!dst_resource->format->depth_size)
1602 return;
1603 if (!(src_view = state->fb.depth_stencil))
1604 return;
1606 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1607 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1610 void CDECL wined3d_device_set_blend_state(struct wined3d_device *device,
1611 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1613 TRACE("device %p, blend_state %p, blend_factor %s, sample_mask %#x.\n",
1614 device, blend_state, debug_color(blend_factor), sample_mask);
1616 wined3d_device_context_set_blend_state(&device->cs->c, blend_state, blend_factor, sample_mask);
1619 struct wined3d_blend_state * CDECL wined3d_device_context_get_blend_state(const struct wined3d_device_context *context,
1620 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1622 const struct wined3d_state *state = context->state;
1624 TRACE("context %p, blend_factor %p, sample_mask %p.\n", context, blend_factor, sample_mask);
1626 *blend_factor = state->blend_factor;
1627 *sample_mask = state->sample_mask;
1628 return state->blend_state;
1631 void CDECL wined3d_device_set_depth_stencil_state(struct wined3d_device *device,
1632 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1634 TRACE("device %p, depth_stencil_state %p, stencil_ref %u.\n", device, depth_stencil_state, stencil_ref);
1636 wined3d_device_context_set_depth_stencil_state(&device->cs->c, depth_stencil_state, stencil_ref);
1639 struct wined3d_depth_stencil_state * CDECL wined3d_device_context_get_depth_stencil_state(
1640 const struct wined3d_device_context *context, unsigned int *stencil_ref)
1642 const struct wined3d_state *state = context->state;
1644 TRACE("context %p, stencil_ref %p.\n", context, stencil_ref);
1646 *stencil_ref = state->stencil_ref;
1647 return state->depth_stencil_state;
1650 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1651 struct wined3d_rasterizer_state *rasterizer_state)
1653 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
1655 wined3d_device_context_set_rasterizer_state(&device->cs->c, rasterizer_state);
1658 struct wined3d_rasterizer_state * CDECL wined3d_device_context_get_rasterizer_state(
1659 struct wined3d_device_context *context)
1661 TRACE("context %p.\n", context);
1663 return context->state->rasterizer_state;
1666 static void wined3d_device_set_render_state(struct wined3d_device *device,
1667 enum wined3d_render_state state, DWORD value)
1669 if (state > WINEHIGHEST_RENDER_STATE)
1671 WARN("Unhandled render state %#x.\n", state);
1672 return;
1675 if (value == device->cs->c.state->render_states[state])
1676 TRACE("Application is setting the old value over, nothing to do.\n");
1677 else
1679 device->cs->c.state->render_states[state] = value;
1680 wined3d_cs_emit_set_render_state(device->cs, state, value);
1683 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1685 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1686 resolve_depth_buffer(device);
1690 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1692 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1694 return device->cs->c.state->render_states[state];
1697 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1698 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1700 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1701 device, sampler_idx, debug_d3dsamplerstate(state), value);
1703 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1705 TRACE("Application is setting the old value over, nothing to do.\n");
1706 return;
1709 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1710 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
1713 void CDECL wined3d_device_set_scissor_rects(struct wined3d_device *device, unsigned int rect_count,
1714 const RECT *rects)
1716 unsigned int i;
1718 TRACE("device %p, rect_count %u, rects %p.\n", device, rect_count, rects);
1720 for (i = 0; i < rect_count; ++i)
1722 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1725 wined3d_device_context_set_scissor_rects(&device->cs->c, rect_count, rects);
1728 void CDECL wined3d_device_context_get_scissor_rects(const struct wined3d_device_context *context,
1729 unsigned int *rect_count, RECT *rects)
1731 const struct wined3d_state *state = context->state;
1732 unsigned int count;
1734 TRACE("context %p, rect_count %p, rects %p.\n", context, rect_count, rects);
1736 if (rects && (count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1))
1737 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1738 if (rect_count)
1739 *rect_count = state->scissor_rect_count;
1742 void CDECL wined3d_device_set_state(struct wined3d_device *device, struct wined3d_state *state)
1744 struct wined3d_device_context *context = &device->cs->c;
1745 const struct wined3d_light_info *light;
1746 unsigned int i, j;
1748 TRACE("device %p, state %p.\n", device, state);
1750 device->cs->c.state = state;
1751 wined3d_device_context_emit_set_feature_level(context, state->feature_level);
1753 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
1755 wined3d_device_context_emit_set_rendertarget_view(context, i, state->fb.render_targets[i]);
1758 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1759 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1761 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
1763 wined3d_device_context_emit_set_stream_output(context, i,
1764 state->stream_output[i].buffer, state->stream_output[i].offset);
1767 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1769 wined3d_device_context_emit_set_stream_source(context, i, state->streams[i].buffer,
1770 state->streams[i].offset, state->streams[i].stride);
1773 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1774 state->index_format, state->index_offset);
1776 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1778 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1780 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1781 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
1782 wined3d_device_context_emit_set_constant_buffer(context, i, j, state->cb[i][j]);
1783 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
1785 wined3d_device_context_emit_set_sampler(context, i, j, state->sampler[i][j]);
1787 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
1789 wined3d_device_context_emit_set_shader_resource_view(context, i, j, state->shader_resource_view[i][j]);
1793 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1795 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
1797 wined3d_device_context_emit_set_unordered_access_view(context, i, j,
1798 state->unordered_access_view[i][j], ~0);
1802 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_F,
1803 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1804 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_I,
1805 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1806 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_VS_B,
1807 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1809 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_F,
1810 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1811 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_I,
1812 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1813 wined3d_device_context_push_constants(context, WINED3D_PUSH_CONSTANTS_PS_B,
1814 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1816 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1818 wined3d_cs_emit_set_texture(device->cs, i, state->textures[i]);
1819 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1821 wined3d_cs_emit_set_sampler_state(device->cs, i, j, state->sampler_states[i][j]);
1825 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1827 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1829 wined3d_cs_emit_set_texture_state(device->cs, i, j, state->texture_states[i][j]);
1833 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1835 wined3d_cs_emit_set_transform(device->cs, i, state->transforms + i);
1838 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1840 wined3d_cs_emit_set_clip_plane(device->cs, i, state->clip_planes + i);
1843 wined3d_cs_emit_set_material(device->cs, &state->material);
1845 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1846 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1848 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1850 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1852 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
1853 wined3d_cs_emit_set_light_enable(device->cs, light->OriginalIndex, light->glIndex != -1);
1857 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1859 wined3d_cs_emit_set_render_state(device->cs, i, state->render_states[i]);
1862 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1863 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1864 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1867 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1869 TRACE("device %p.\n", device);
1871 return device->cs->c.state;
1874 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1876 TRACE("device %p.\n", device);
1878 return &device->cs->c;
1881 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
1882 struct wined3d_vertex_declaration *declaration)
1884 TRACE("device %p, declaration %p.\n", device, declaration);
1886 wined3d_device_context_set_vertex_declaration(&device->cs->c, declaration);
1889 struct wined3d_vertex_declaration * CDECL wined3d_device_context_get_vertex_declaration(
1890 const struct wined3d_device_context *context)
1892 TRACE("context %p.\n", context);
1894 return context->state->vertex_declaration;
1897 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1898 enum wined3d_shader_type type, struct wined3d_shader *shader)
1900 struct wined3d_state *state = context->state;
1901 struct wined3d_shader *prev;
1903 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1905 prev = state->shader[type];
1906 if (shader == prev)
1907 return;
1909 if (shader)
1910 wined3d_shader_incref(shader);
1911 state->shader[type] = shader;
1912 wined3d_device_context_emit_set_shader(context, type, shader);
1913 if (prev)
1914 wined3d_shader_decref(prev);
1917 struct wined3d_shader * CDECL wined3d_device_context_get_shader(const struct wined3d_device_context *context,
1918 enum wined3d_shader_type type)
1920 TRACE("context %p, type %#x.\n", context, type);
1922 return context->state->shader[type];
1925 void CDECL wined3d_device_context_set_constant_buffer(struct wined3d_device_context *context,
1926 enum wined3d_shader_type type, unsigned int idx, struct wined3d_buffer *buffer)
1928 struct wined3d_state *state = context->state;
1929 struct wined3d_buffer *prev;
1931 TRACE("context %p, type %#x, idx %u, buffer %p.\n", context, type, idx, buffer);
1933 if (idx >= MAX_CONSTANT_BUFFERS)
1935 WARN("Invalid constant buffer index %u.\n", idx);
1936 return;
1939 prev = state->cb[type][idx];
1940 if (buffer == prev)
1941 return;
1943 if (buffer)
1944 wined3d_buffer_incref(buffer);
1945 state->cb[type][idx] = buffer;
1946 wined3d_device_context_emit_set_constant_buffer(context, type, idx, buffer);
1947 if (prev)
1948 wined3d_buffer_decref(prev);
1951 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1952 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1954 struct wined3d_state *state = context->state;
1955 struct wined3d_blend_state *prev;
1957 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1958 context, blend_state, blend_factor, sample_mask);
1960 prev = state->blend_state;
1961 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1962 && sample_mask == state->sample_mask)
1963 return;
1965 if (blend_state)
1966 wined3d_blend_state_incref(blend_state);
1967 state->blend_state = blend_state;
1968 state->blend_factor = *blend_factor;
1969 state->sample_mask = sample_mask;
1970 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1971 if (prev)
1972 wined3d_blend_state_decref(prev);
1975 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1976 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1978 struct wined3d_state *state = context->state;
1979 struct wined3d_depth_stencil_state *prev;
1981 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1983 prev = state->depth_stencil_state;
1984 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1985 return;
1987 if (depth_stencil_state)
1988 wined3d_depth_stencil_state_incref(depth_stencil_state);
1989 state->depth_stencil_state = depth_stencil_state;
1990 state->stencil_ref = stencil_ref;
1991 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1992 if (prev)
1993 wined3d_depth_stencil_state_decref(prev);
1996 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1997 struct wined3d_rasterizer_state *rasterizer_state)
1999 struct wined3d_state *state = context->state;
2000 struct wined3d_rasterizer_state *prev;
2002 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
2004 prev = state->rasterizer_state;
2005 if (prev == rasterizer_state)
2006 return;
2008 if (rasterizer_state)
2009 wined3d_rasterizer_state_incref(rasterizer_state);
2010 state->rasterizer_state = rasterizer_state;
2011 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
2012 if (prev)
2013 wined3d_rasterizer_state_decref(prev);
2016 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
2017 const struct wined3d_viewport *viewports)
2019 struct wined3d_state *state = context->state;
2020 unsigned int i;
2022 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
2024 for (i = 0; i < viewport_count; ++i)
2026 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
2027 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
2030 if (viewport_count)
2031 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
2032 else
2033 memset(state->viewports, 0, sizeof(state->viewports));
2034 state->viewport_count = viewport_count;
2036 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
2039 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
2040 const RECT *rects)
2042 struct wined3d_state *state = context->state;
2043 unsigned int i;
2045 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
2047 for (i = 0; i < rect_count; ++i)
2049 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2052 if (state->scissor_rect_count == rect_count
2053 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
2055 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2056 return;
2059 if (rect_count)
2060 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
2061 else
2062 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
2063 state->scissor_rect_count = rect_count;
2065 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
2068 void CDECL wined3d_device_context_set_shader_resource_view(struct wined3d_device_context *context,
2069 enum wined3d_shader_type type, unsigned int idx, struct wined3d_shader_resource_view *view)
2071 struct wined3d_state *state = context->state;
2072 const struct wined3d_rendertarget_view *dsv;
2073 struct wined3d_shader_resource_view *prev;
2075 TRACE("context %p, type %#x, idx %u, view %p.\n", context, type, idx, view);
2077 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2079 WARN("Invalid view index %u.\n", idx);
2080 return;
2083 prev = state->shader_resource_view[type][idx];
2084 if (view == prev)
2085 return;
2087 if (view && (wined3d_is_srv_rtv_bound(view)
2088 || ((dsv = state->fb.depth_stencil)
2089 && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2091 WARN("Application is trying to bind resource which is attached as render target.\n");
2092 view = NULL;
2095 if (view)
2097 wined3d_shader_resource_view_incref(view);
2098 wined3d_srv_bind_count_inc(view);
2101 state->shader_resource_view[type][idx] = view;
2102 wined3d_device_context_emit_set_shader_resource_view(context, type, idx, view);
2103 if (prev)
2105 wined3d_srv_bind_count_dec(prev);
2106 wined3d_shader_resource_view_decref(prev);
2110 void CDECL wined3d_device_context_set_sampler(struct wined3d_device_context *context,
2111 enum wined3d_shader_type type, unsigned int idx, struct wined3d_sampler *sampler)
2113 struct wined3d_state *state = context->state;
2114 struct wined3d_sampler *prev;
2116 TRACE("context %p, type %#x, idx %u, sampler %p.\n", context, type, idx, sampler);
2118 if (idx >= MAX_SAMPLER_OBJECTS)
2120 WARN("Invalid sampler index %u.\n", idx);
2121 return;
2124 prev = state->sampler[type][idx];
2125 if (sampler == prev)
2126 return;
2128 if (sampler)
2129 wined3d_sampler_incref(sampler);
2130 state->sampler[type][idx] = sampler;
2131 wined3d_device_context_emit_set_sampler(context, type, idx, sampler);
2132 if (prev)
2133 wined3d_sampler_decref(prev);
2136 void CDECL wined3d_device_context_set_unordered_access_view(struct wined3d_device_context *context,
2137 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2138 unsigned int initial_count)
2140 struct wined3d_state *state = context->state;
2141 struct wined3d_unordered_access_view *prev;
2143 TRACE("context %p, pipeline %#x, idx %u, uav %p, initial_count %u.\n", context, pipeline, idx, uav, initial_count);
2145 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2147 WARN("Invalid UAV index %u.\n", idx);
2148 return;
2151 prev = state->unordered_access_view[pipeline][idx];
2152 if (uav == prev && initial_count == ~0u)
2153 return;
2155 if (uav)
2156 wined3d_unordered_access_view_incref(uav);
2157 state->unordered_access_view[pipeline][idx] = uav;
2158 wined3d_device_context_emit_set_unordered_access_view(context, pipeline, idx, uav, initial_count);
2159 if (prev)
2160 wined3d_unordered_access_view_decref(prev);
2163 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2164 const struct wined3d_rendertarget_view *view, BOOL dsv)
2166 struct wined3d_state *state = context->state;
2168 if (view && wined3d_is_rtv_srv_bound(view))
2170 const struct wined3d_resource *resource = view->resource;
2171 const struct wined3d_shader_resource_view *srv;
2172 unsigned int i, j;
2174 WARN("Application sets bound resource as render target.\n");
2176 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2177 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2178 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2179 && ((!dsv && wined3d_is_srv_rtv_bound(srv))
2180 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2181 wined3d_device_context_set_shader_resource_view(context, i, j, NULL);
2185 HRESULT CDECL wined3d_device_context_set_rendertarget_view(struct wined3d_device_context *context,
2186 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
2188 struct wined3d_state *state = context->state;
2189 struct wined3d_rendertarget_view *prev;
2190 unsigned int max_rt_count;
2192 TRACE("context %p, view_idx %u, view %p, set_viewport %#x.\n",
2193 context, view_idx, view, set_viewport);
2195 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2196 if (view_idx >= max_rt_count)
2198 WARN("Only %u render targets are supported.\n", max_rt_count);
2199 return WINED3DERR_INVALIDCALL;
2202 if (view && !(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2204 WARN("View resource %p doesn't have render target bind flags.\n", view->resource);
2205 return WINED3DERR_INVALIDCALL;
2208 /* Set the viewport and scissor rectangles, if requested. Tests show that
2209 * stateblock recording is ignored, the change goes directly into the
2210 * primary stateblock. */
2211 if (!view_idx && set_viewport)
2213 state->viewports[0].x = 0;
2214 state->viewports[0].y = 0;
2215 state->viewports[0].width = view->width;
2216 state->viewports[0].height = view->height;
2217 state->viewports[0].min_z = 0.0f;
2218 state->viewports[0].max_z = 1.0f;
2219 state->viewport_count = 1;
2220 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2222 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
2223 state->scissor_rect_count = 1;
2224 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2227 prev = state->fb.render_targets[view_idx];
2228 if (view == prev)
2229 return WINED3D_OK;
2231 if (view)
2233 wined3d_rendertarget_view_incref(view);
2234 wined3d_rtv_bind_count_inc(view);
2236 state->fb.render_targets[view_idx] = view;
2237 wined3d_device_context_emit_set_rendertarget_view(context, view_idx, view);
2238 /* Release after the assignment, to prevent device_resource_released()
2239 * from seeing the surface as still in use. */
2240 if (prev)
2242 wined3d_rtv_bind_count_dec(prev);
2243 wined3d_rendertarget_view_decref(prev);
2246 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2248 return WINED3D_OK;
2251 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2252 struct wined3d_rendertarget_view *view)
2254 struct wined3d_fb_state *fb = &context->state->fb;
2255 struct wined3d_rendertarget_view *prev;
2257 TRACE("context %p, view %p.\n", context, view);
2259 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2261 WARN("View resource %p has incompatible %s bind flags.\n",
2262 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2263 return WINED3DERR_INVALIDCALL;
2266 prev = fb->depth_stencil;
2267 if (prev == view)
2269 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2270 return WINED3D_OK;
2273 if ((fb->depth_stencil = view))
2274 wined3d_rendertarget_view_incref(view);
2275 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2276 if (prev)
2277 wined3d_rendertarget_view_decref(prev);
2278 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2280 return WINED3D_OK;
2283 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2284 struct wined3d_query *predicate, BOOL value)
2286 struct wined3d_state *state = context->state;
2287 struct wined3d_query *prev;
2289 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2291 prev = state->predicate;
2292 if (predicate)
2294 FIXME("Predicated rendering not implemented.\n");
2295 wined3d_query_incref(predicate);
2297 state->predicate = predicate;
2298 state->predicate_value = value;
2299 wined3d_device_context_emit_set_predication(context, predicate, value);
2300 if (prev)
2301 wined3d_query_decref(prev);
2304 HRESULT CDECL wined3d_device_context_set_stream_source(struct wined3d_device_context *context,
2305 unsigned int stream_idx, struct wined3d_buffer *buffer, unsigned int offset, unsigned int stride)
2307 struct wined3d_stream_state *stream;
2308 struct wined3d_buffer *prev_buffer;
2310 TRACE("context %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2311 context, stream_idx, buffer, offset, stride);
2313 if (stream_idx >= WINED3D_MAX_STREAMS)
2315 WARN("Stream index %u out of range.\n", stream_idx);
2316 return WINED3DERR_INVALIDCALL;
2318 else if (offset & 0x3)
2320 WARN("Offset %u is not 4 byte aligned.\n", offset);
2321 return WINED3DERR_INVALIDCALL;
2324 stream = &context->state->streams[stream_idx];
2325 prev_buffer = stream->buffer;
2327 if (prev_buffer == buffer
2328 && stream->stride == stride
2329 && stream->offset == offset)
2331 TRACE("Application is setting the old values over, nothing to do.\n");
2332 return WINED3D_OK;
2335 stream->buffer = buffer;
2336 stream->stride = stride;
2337 stream->offset = offset;
2338 if (buffer)
2339 wined3d_buffer_incref(buffer);
2340 wined3d_device_context_emit_set_stream_source(context, stream_idx, buffer, offset, stride);
2341 if (prev_buffer)
2342 wined3d_buffer_decref(prev_buffer);
2344 return WINED3D_OK;
2347 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2348 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2350 struct wined3d_state *state = context->state;
2351 enum wined3d_format_id prev_format;
2352 struct wined3d_buffer *prev_buffer;
2353 unsigned int prev_offset;
2355 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2356 context, buffer, debug_d3dformat(format_id), offset);
2358 prev_buffer = state->index_buffer;
2359 prev_format = state->index_format;
2360 prev_offset = state->index_offset;
2362 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2363 return;
2365 if (buffer)
2366 wined3d_buffer_incref(buffer);
2367 state->index_buffer = buffer;
2368 state->index_format = format_id;
2369 state->index_offset = offset;
2370 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2371 if (prev_buffer)
2372 wined3d_buffer_decref(prev_buffer);
2375 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2376 struct wined3d_vertex_declaration *declaration)
2378 struct wined3d_state *state = context->state;
2379 struct wined3d_vertex_declaration *prev;
2381 TRACE("context %p, declaration %p.\n", context, declaration);
2383 prev = state->vertex_declaration;
2384 if (declaration == prev)
2385 return;
2387 if (declaration)
2388 wined3d_vertex_declaration_incref(declaration);
2389 state->vertex_declaration = declaration;
2390 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2391 if (prev)
2392 wined3d_vertex_declaration_decref(prev);
2395 void CDECL wined3d_device_context_set_stream_output(struct wined3d_device_context *context, unsigned int idx,
2396 struct wined3d_buffer *buffer, unsigned int offset)
2398 struct wined3d_stream_output *stream;
2399 struct wined3d_buffer *prev_buffer;
2401 TRACE("context %p, idx %u, buffer %p, offset %u.\n", context, idx, buffer, offset);
2403 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
2405 WARN("Invalid stream output %u.\n", idx);
2406 return;
2409 stream = &context->state->stream_output[idx];
2410 prev_buffer = stream->buffer;
2412 if (buffer)
2413 wined3d_buffer_incref(buffer);
2414 stream->buffer = buffer;
2415 stream->offset = offset;
2416 wined3d_device_context_emit_set_stream_output(context, idx, buffer, offset);
2417 if (prev_buffer)
2418 wined3d_buffer_decref(prev_buffer);
2421 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2422 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2424 struct wined3d_state *state = context->state;
2426 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2427 context, start_vertex, vertex_count, start_instance, instance_count);
2429 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2430 0, start_vertex, vertex_count, start_instance, instance_count, false);
2433 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2434 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2436 struct wined3d_state *state = context->state;
2438 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2439 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2441 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2442 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2445 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2447 TRACE("device %p, shader %p.\n", device, shader);
2449 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, shader);
2452 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2454 TRACE("device %p.\n", device);
2456 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_VERTEX];
2459 void CDECL wined3d_device_set_constant_buffer(struct wined3d_device *device,
2460 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2462 TRACE("device %p, type %#x, idx %u, buffer %p.\n", device, type, idx, buffer);
2464 return wined3d_device_context_set_constant_buffer(&device->cs->c, type, idx, buffer);
2467 struct wined3d_buffer * CDECL wined3d_device_context_get_constant_buffer(const struct wined3d_device_context *context,
2468 enum wined3d_shader_type shader_type, unsigned int idx)
2470 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2472 if (idx >= MAX_CONSTANT_BUFFERS)
2474 WARN("Invalid constant buffer index %u.\n", idx);
2475 return NULL;
2478 return context->state->cb[shader_type][idx];
2481 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2482 UINT idx, struct wined3d_shader_resource_view *view)
2484 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2486 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2489 struct wined3d_shader_resource_view * CDECL wined3d_device_context_get_shader_resource_view(
2490 const struct wined3d_device_context *context, enum wined3d_shader_type shader_type, unsigned int idx)
2492 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2494 WARN("Invalid view index %u.\n", idx);
2495 return NULL;
2498 return context->state->shader_resource_view[shader_type][idx];
2501 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2502 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2504 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2506 WARN("Invalid view index %u.\n", idx);
2507 return NULL;
2510 return device->cs->c.state->shader_resource_view[shader_type][idx];
2513 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2514 UINT idx)
2516 TRACE("device %p, idx %u.\n", device, idx);
2518 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2521 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2523 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2525 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2528 struct wined3d_sampler * CDECL wined3d_device_context_get_sampler(const struct wined3d_device_context *context,
2529 enum wined3d_shader_type shader_type, unsigned int idx)
2531 TRACE("context %p, shader_type %#x, idx %u.\n", context, shader_type, idx);
2533 if (idx >= MAX_SAMPLER_OBJECTS)
2535 WARN("Invalid sampler index %u.\n", idx);
2536 return NULL;
2539 return context->state->sampler[shader_type][idx];
2542 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2543 enum wined3d_shader_type shader_type, unsigned int idx)
2545 if (idx >= MAX_SAMPLER_OBJECTS)
2547 WARN("Invalid sampler index %u.\n", idx);
2548 return NULL;
2551 return device->cs->c.state->sampler[shader_type][idx];
2554 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2556 TRACE("device %p, idx %u.\n", device, idx);
2558 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2561 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2562 unsigned int start_idx, unsigned int count, const BOOL *constants)
2564 unsigned int i;
2566 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2567 device, start_idx, count, constants);
2569 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2570 if (TRACE_ON(d3d))
2572 for (i = 0; i < count; ++i)
2573 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2576 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2579 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2580 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2582 unsigned int i;
2584 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2585 device, start_idx, count, constants);
2587 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2588 if (TRACE_ON(d3d))
2590 for (i = 0; i < count; ++i)
2591 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2594 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2597 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2598 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2600 unsigned int i;
2602 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2603 device, start_idx, count, constants);
2605 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2606 if (TRACE_ON(d3d))
2608 for (i = 0; i < count; ++i)
2609 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2612 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2615 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2617 TRACE("device %p, shader %p.\n", device, shader);
2619 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, shader);
2622 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2624 TRACE("device %p.\n", device);
2626 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_PIXEL];
2629 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2630 UINT idx, struct wined3d_shader_resource_view *view)
2632 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2634 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2637 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2638 UINT idx)
2640 TRACE("device %p, idx %u.\n", device, idx);
2642 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2645 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2647 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2649 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2652 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2654 TRACE("device %p, idx %u.\n", device, idx);
2656 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2659 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2660 unsigned int start_idx, unsigned int count, const BOOL *constants)
2662 unsigned int i;
2664 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2665 device, start_idx, count, constants);
2667 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2668 if (TRACE_ON(d3d))
2670 for (i = 0; i < count; ++i)
2671 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2674 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2677 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2678 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2680 unsigned int i;
2682 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2683 device, start_idx, count, constants);
2685 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2686 if (TRACE_ON(d3d))
2688 for (i = 0; i < count; ++i)
2689 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2692 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2695 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2696 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2698 unsigned int i;
2700 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2701 device, start_idx, count, constants);
2703 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2704 if (TRACE_ON(d3d))
2706 for (i = 0; i < count; ++i)
2707 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2710 wined3d_device_context_push_constants(&device->cs->c, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2713 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2715 TRACE("device %p, shader %p.\n", device, shader);
2717 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_HULL, shader);
2720 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2722 TRACE("device %p.\n", device);
2724 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_HULL];
2727 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2728 unsigned int idx, struct wined3d_shader_resource_view *view)
2730 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2732 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_HULL, idx, view);
2735 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2736 unsigned int idx)
2738 TRACE("device %p, idx %u.\n", device, idx);
2740 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2743 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2744 unsigned int idx, struct wined3d_sampler *sampler)
2746 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2748 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2751 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2753 TRACE("device %p, idx %u.\n", device, idx);
2755 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2758 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2760 TRACE("device %p, shader %p.\n", device, shader);
2762 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, shader);
2765 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2767 TRACE("device %p.\n", device);
2769 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2772 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2773 unsigned int idx, struct wined3d_shader_resource_view *view)
2775 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2777 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2780 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2781 unsigned int idx)
2783 TRACE("device %p, idx %u.\n", device, idx);
2785 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2788 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2789 unsigned int idx, struct wined3d_sampler *sampler)
2791 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2793 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2796 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2798 TRACE("device %p, idx %u.\n", device, idx);
2800 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2803 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2805 TRACE("device %p, shader %p.\n", device, shader);
2807 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2810 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2812 TRACE("device %p.\n", device);
2814 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2817 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2818 UINT idx, struct wined3d_shader_resource_view *view)
2820 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2822 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2825 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2826 UINT idx)
2828 TRACE("device %p, idx %u.\n", device, idx);
2830 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2833 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2835 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2837 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2840 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2842 TRACE("device %p, idx %u.\n", device, idx);
2844 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2847 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2849 TRACE("device %p, shader %p.\n", device, shader);
2851 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, shader);
2854 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2856 TRACE("device %p.\n", device);
2858 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2861 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2862 unsigned int idx, struct wined3d_shader_resource_view *view)
2864 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2866 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2869 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2870 unsigned int idx)
2872 TRACE("device %p, idx %u.\n", device, idx);
2874 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2877 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2878 unsigned int idx, struct wined3d_sampler *sampler)
2880 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2882 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2885 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2887 TRACE("device %p, idx %u.\n", device, idx);
2889 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2892 struct wined3d_unordered_access_view * CDECL wined3d_device_context_get_unordered_access_view(
2893 const struct wined3d_device_context *context, enum wined3d_pipeline pipeline, unsigned int idx)
2895 TRACE("context %p, pipeline %#x, idx %u.\n", context, pipeline, idx);
2897 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2899 WARN("Invalid UAV index %u.\n", idx);
2900 return NULL;
2903 return context->state->unordered_access_view[pipeline][idx];
2906 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2907 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2909 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2911 WARN("Invalid UAV index %u.\n", idx);
2912 return NULL;
2915 return device->cs->c.state->unordered_access_view[pipeline][idx];
2918 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2919 struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2921 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2923 wined3d_device_context_set_unordered_access_view(&device->cs->c, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
2926 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
2927 unsigned int idx)
2929 TRACE("device %p, idx %u.\n", device, idx);
2931 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
2934 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2935 unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2937 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2939 wined3d_device_context_set_unordered_access_view(&device->cs->c, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
2942 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
2943 const struct wined3d_device *device, unsigned int idx)
2945 TRACE("device %p, idx %u.\n", device, idx);
2947 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
2950 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2952 unsigned int i;
2954 if (!latency)
2955 latency = 3;
2957 device->max_frame_latency = latency;
2958 for (i = 0; i < device->swapchain_count; ++i)
2959 swapchain_set_max_frame_latency(device->swapchains[i], device);
2962 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2964 return device->max_frame_latency;
2967 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2969 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2970 unsigned int i, size = 0;
2972 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2973 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2974 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2975 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2976 switch (fvf & WINED3DFVF_POSITION_MASK)
2978 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2979 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2980 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2981 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2982 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2983 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2984 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2985 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2986 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2988 for (i = 0; i < texcoord_count; ++i)
2990 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2993 return size;
2996 static void wined3d_format_get_colour(const struct wined3d_format *format,
2997 const void *data, struct wined3d_color *colour)
2999 float *output = &colour->r;
3000 const uint32_t *u32_data;
3001 const uint16_t *u16_data;
3002 const float *f32_data;
3003 unsigned int i;
3005 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
3006 static unsigned int warned;
3008 switch (format->id)
3010 case WINED3DFMT_B8G8R8A8_UNORM:
3011 u32_data = data;
3012 wined3d_color_from_d3dcolor(colour, *u32_data);
3013 break;
3015 case WINED3DFMT_R8G8B8A8_UNORM:
3016 u32_data = data;
3017 colour->r = (*u32_data & 0xffu) / 255.0f;
3018 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
3019 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
3020 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
3021 break;
3023 case WINED3DFMT_R16G16_UNORM:
3024 case WINED3DFMT_R16G16B16A16_UNORM:
3025 u16_data = data;
3026 *colour = default_colour;
3027 for (i = 0; i < format->component_count; ++i)
3028 output[i] = u16_data[i] / 65535.0f;
3029 break;
3031 case WINED3DFMT_R32_FLOAT:
3032 case WINED3DFMT_R32G32_FLOAT:
3033 case WINED3DFMT_R32G32B32_FLOAT:
3034 case WINED3DFMT_R32G32B32A32_FLOAT:
3035 f32_data = data;
3036 *colour = default_colour;
3037 for (i = 0; i < format->component_count; ++i)
3038 output[i] = f32_data[i];
3039 break;
3041 default:
3042 *colour = default_colour;
3043 if (!warned++)
3044 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
3045 break;
3049 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
3050 const struct wined3d_color *material_colour, unsigned int index,
3051 const struct wined3d_stream_info *stream_info)
3053 const struct wined3d_stream_info_element *element = NULL;
3055 switch (mcs)
3057 case WINED3D_MCS_MATERIAL:
3058 *colour = *material_colour;
3059 return;
3061 case WINED3D_MCS_COLOR1:
3062 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3064 colour->r = colour->g = colour->b = colour->a = 1.0f;
3065 return;
3067 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3068 break;
3070 case WINED3D_MCS_COLOR2:
3071 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3073 colour->r = colour->g = colour->b = colour->a = 0.0f;
3074 return;
3076 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3077 break;
3079 default:
3080 colour->r = colour->g = colour->b = colour->a = 0.0f;
3081 ERR("Invalid material colour source %#x.\n", mcs);
3082 return;
3085 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
3088 static float wined3d_clamp(float value, float min_value, float max_value)
3090 return value < min_value ? min_value : value > max_value ? max_value : value;
3093 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3095 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
3098 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3100 v0->x -= v1->x;
3101 v0->y -= v1->y;
3102 v0->z -= v1->z;
3105 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
3107 v->x *= s;
3108 v->y *= s;
3109 v->z *= s;
3112 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
3114 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
3116 if (isfinite(rnorm))
3117 wined3d_vec3_scale(v, rnorm);
3120 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
3121 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
3123 struct wined3d_vec3 tmp;
3125 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
3126 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
3127 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
3129 *dst = tmp;
3132 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
3133 float min_value, float max_value)
3135 dst->r = wined3d_clamp(src->r, min_value, max_value);
3136 dst->g = wined3d_clamp(src->g, min_value, max_value);
3137 dst->b = wined3d_clamp(src->b, min_value, max_value);
3138 dst->a = wined3d_clamp(src->a, min_value, max_value);
3141 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
3143 dst->r += src->r * c;
3144 dst->g += src->g * c;
3145 dst->b += src->b * c;
3148 static void init_transformed_lights(struct lights_settings *ls,
3149 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
3151 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
3152 const struct wined3d_light_info *light_info;
3153 struct light_transformed *light;
3154 struct wined3d_vec4 vec4;
3155 unsigned int light_count;
3156 unsigned int i, index;
3158 memset(ls, 0, sizeof(*ls));
3160 ls->lighting = !!compute_lighting;
3161 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
3162 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
3163 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
3164 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
3165 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
3166 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
3168 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
3169 return;
3171 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
3172 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
3174 if (!compute_lighting)
3175 return;
3177 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
3179 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
3180 ls->legacy_lighting = !!legacy_lighting;
3181 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
3182 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
3184 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
3186 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
3188 if (!light_info->enabled)
3189 continue;
3191 switch (light_info->OriginalParms.type)
3193 case WINED3D_LIGHT_DIRECTIONAL:
3194 ++ls->directional_light_count;
3195 break;
3197 case WINED3D_LIGHT_POINT:
3198 ++ls->point_light_count;
3199 break;
3201 case WINED3D_LIGHT_SPOT:
3202 ++ls->spot_light_count;
3203 break;
3205 case WINED3D_LIGHT_PARALLELPOINT:
3206 ++ls->parallel_point_light_count;
3207 break;
3209 default:
3210 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
3211 continue;
3213 lights[index++] = light_info;
3214 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
3215 break;
3219 light_count = index;
3220 for (i = 0, index = 0; i < light_count; ++i)
3222 light_info = lights[i];
3223 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
3224 continue;
3226 light = &ls->lights[index];
3227 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3228 light->direction = *(struct wined3d_vec3 *)&vec4;
3229 wined3d_vec3_normalise(&light->direction);
3231 light->diffuse = light_info->OriginalParms.diffuse;
3232 light->ambient = light_info->OriginalParms.ambient;
3233 light->specular = light_info->OriginalParms.specular;
3234 ++index;
3237 for (i = 0; i < light_count; ++i)
3239 light_info = lights[i];
3240 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
3241 continue;
3243 light = &ls->lights[index];
3245 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3246 light->range = light_info->OriginalParms.range;
3247 light->c_att = light_info->OriginalParms.attenuation0;
3248 light->l_att = light_info->OriginalParms.attenuation1;
3249 light->q_att = light_info->OriginalParms.attenuation2;
3251 light->diffuse = light_info->OriginalParms.diffuse;
3252 light->ambient = light_info->OriginalParms.ambient;
3253 light->specular = light_info->OriginalParms.specular;
3254 ++index;
3257 for (i = 0; i < light_count; ++i)
3259 light_info = lights[i];
3260 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
3261 continue;
3263 light = &ls->lights[index];
3265 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3266 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3267 light->direction = *(struct wined3d_vec3 *)&vec4;
3268 wined3d_vec3_normalise(&light->direction);
3269 light->range = light_info->OriginalParms.range;
3270 light->falloff = light_info->OriginalParms.falloff;
3271 light->c_att = light_info->OriginalParms.attenuation0;
3272 light->l_att = light_info->OriginalParms.attenuation1;
3273 light->q_att = light_info->OriginalParms.attenuation2;
3274 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
3275 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
3277 light->diffuse = light_info->OriginalParms.diffuse;
3278 light->ambient = light_info->OriginalParms.ambient;
3279 light->specular = light_info->OriginalParms.specular;
3280 ++index;
3283 for (i = 0; i < light_count; ++i)
3285 light_info = lights[i];
3286 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
3287 continue;
3289 light = &ls->lights[index];
3291 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3292 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
3293 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
3294 light->diffuse = light_info->OriginalParms.diffuse;
3295 light->ambient = light_info->OriginalParms.ambient;
3296 light->specular = light_info->OriginalParms.specular;
3297 ++index;
3301 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
3302 const struct wined3d_vec3 *dir, float att, float material_shininess,
3303 const struct wined3d_vec3 *normal_transformed,
3304 const struct wined3d_vec3 *position_transformed_normalised,
3305 const struct light_transformed *light, const struct lights_settings *ls)
3307 struct wined3d_vec3 vec3;
3308 float t, c;
3310 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
3311 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
3313 vec3 = *dir;
3314 if (ls->localviewer)
3315 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
3316 else
3317 vec3.z -= 1.0f;
3318 wined3d_vec3_normalise(&vec3);
3319 t = wined3d_vec3_dot(normal_transformed, &vec3);
3320 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
3321 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
3322 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
3325 static void light_set_vertex_data(struct lights_settings *ls,
3326 const struct wined3d_vec4 *position)
3328 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3329 return;
3331 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3332 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3335 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3336 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3337 float material_shininess)
3339 struct wined3d_vec3 position_transformed_normalised;
3340 struct wined3d_vec3 normal_transformed = {0.0f};
3341 const struct light_transformed *light;
3342 struct wined3d_vec3 dir, dst;
3343 unsigned int i, index;
3344 float att;
3346 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3347 wined3d_vec3_normalise(&position_transformed_normalised);
3349 if (normal)
3351 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3352 if (ls->normalise)
3353 wined3d_vec3_normalise(&normal_transformed);
3356 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3357 *specular = *diffuse;
3358 *ambient = ls->ambient_light;
3360 index = 0;
3361 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3363 light = &ls->lights[index];
3365 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3366 if (normal)
3367 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3368 &normal_transformed, &position_transformed_normalised, light, ls);
3371 for (i = 0; i < ls->point_light_count; ++i, ++index)
3373 light = &ls->lights[index];
3374 dir.x = light->position.x - ls->position_transformed.x;
3375 dir.y = light->position.y - ls->position_transformed.y;
3376 dir.z = light->position.z - ls->position_transformed.z;
3378 dst.z = wined3d_vec3_dot(&dir, &dir);
3379 dst.y = sqrtf(dst.z);
3380 dst.x = 1.0f;
3381 if (ls->legacy_lighting)
3383 dst.y = (light->range - dst.y) / light->range;
3384 if (!(dst.y > 0.0f))
3385 continue;
3386 dst.z = dst.y * dst.y;
3388 else
3390 if (!(dst.y <= light->range))
3391 continue;
3393 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3394 if (!ls->legacy_lighting)
3395 att = 1.0f / att;
3397 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3398 if (normal)
3400 wined3d_vec3_normalise(&dir);
3401 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3402 &normal_transformed, &position_transformed_normalised, light, ls);
3406 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3408 float t;
3410 light = &ls->lights[index];
3412 dir.x = light->position.x - ls->position_transformed.x;
3413 dir.y = light->position.y - ls->position_transformed.y;
3414 dir.z = light->position.z - ls->position_transformed.z;
3416 dst.z = wined3d_vec3_dot(&dir, &dir);
3417 dst.y = sqrtf(dst.z);
3418 dst.x = 1.0f;
3420 if (ls->legacy_lighting)
3422 dst.y = (light->range - dst.y) / light->range;
3423 if (!(dst.y > 0.0f))
3424 continue;
3425 dst.z = dst.y * dst.y;
3427 else
3429 if (!(dst.y <= light->range))
3430 continue;
3432 wined3d_vec3_normalise(&dir);
3433 t = -wined3d_vec3_dot(&dir, &light->direction);
3434 if (t > light->cos_htheta)
3435 att = 1.0f;
3436 else if (t <= light->cos_hphi)
3437 att = 0.0f;
3438 else
3439 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3441 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3442 if (ls->legacy_lighting)
3443 att *= t;
3444 else
3445 att /= t;
3447 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3449 if (normal)
3450 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3451 &normal_transformed, &position_transformed_normalised, light, ls);
3454 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3456 light = &ls->lights[index];
3458 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3459 if (normal)
3460 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3461 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3465 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3467 switch (ls->fog_mode)
3469 case WINED3D_FOG_NONE:
3470 return fog_coord;
3471 case WINED3D_FOG_LINEAR:
3472 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3473 case WINED3D_FOG_EXP:
3474 return expf(-fog_coord * ls->fog_density);
3475 case WINED3D_FOG_EXP2:
3476 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3477 default:
3478 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3479 return 0.0f;
3483 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3485 float fog_coord;
3487 if (ls->fog_mode == WINED3D_FOG_NONE)
3488 return;
3490 switch (ls->fog_coord_mode)
3492 case WINED3D_FFP_VS_FOG_RANGE:
3493 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3494 (const struct wined3d_vec3 *)&ls->position_transformed));
3495 break;
3497 case WINED3D_FFP_VS_FOG_DEPTH:
3498 fog_coord = fabsf(ls->position_transformed.z);
3499 break;
3501 default:
3502 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3503 return;
3505 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3508 /* Context activation is done by the caller. */
3509 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3510 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3511 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3513 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3514 const struct wined3d_color *material_specular_state_colour;
3515 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3516 const struct wined3d_state *state = device->cs->c.state;
3517 const struct wined3d_format *output_colour_format;
3518 static const struct wined3d_color black;
3519 struct wined3d_map_desc map_desc;
3520 struct wined3d_box box = {0};
3521 struct wined3d_viewport vp;
3522 unsigned int texture_count;
3523 struct lights_settings ls;
3524 unsigned int vertex_size;
3525 BOOL do_clip, lighting;
3526 float min_z, max_z;
3527 unsigned int i;
3528 BYTE *dest_ptr;
3529 HRESULT hr;
3531 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3533 ERR("Source has no position mask.\n");
3534 return WINED3DERR_INVALIDCALL;
3537 if (state->render_states[WINED3D_RS_CLIPPING])
3539 static BOOL warned = FALSE;
3541 * The clipping code is not quite correct. Some things need
3542 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3543 * so disable clipping for now.
3544 * (The graphics in Half-Life are broken, and my processvertices
3545 * test crashes with IDirect3DDevice3)
3546 do_clip = TRUE;
3548 do_clip = FALSE;
3549 if (!warned)
3551 warned = TRUE;
3552 FIXME("Clipping is broken and disabled for now\n");
3555 else
3556 do_clip = FALSE;
3558 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3559 box.left = dwDestIndex * vertex_size;
3560 box.right = box.left + dwCount * vertex_size;
3561 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3563 WARN("Failed to map buffer, hr %#x.\n", hr);
3564 return hr;
3566 dest_ptr = map_desc.data;
3568 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3569 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3570 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3572 TRACE("View mat:\n");
3573 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3574 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3575 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3576 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3578 TRACE("Proj mat:\n");
3579 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3580 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3581 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3582 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3584 TRACE("World mat:\n");
3585 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3586 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3587 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3588 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3590 /* Get the viewport */
3591 wined3d_device_context_get_viewports(&device->cs->c, NULL, &vp);
3592 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3593 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3595 multiply_matrix(&mat,&view_mat,&world_mat);
3596 multiply_matrix(&mat,&proj_mat,&mat);
3598 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3600 lighting = state->render_states[WINED3D_RS_LIGHTING]
3601 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3602 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3603 &ambient_source, &specular_source, state, stream_info);
3604 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3605 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3606 ? &state->material.specular : &black;
3607 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3608 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3610 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3612 for (i = 0; i < dwCount; ++i)
3614 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3615 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3616 struct wined3d_color ambient, diffuse, specular;
3617 struct wined3d_vec4 position;
3618 unsigned int tex_index;
3620 position.x = p[0];
3621 position.y = p[1];
3622 position.z = p[2];
3623 position.w = 1.0f;
3625 light_set_vertex_data(&ls, &position);
3627 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3628 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3629 /* The position first */
3630 float x, y, z, rhw;
3631 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3633 /* Multiplication with world, view and projection matrix. */
3634 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3635 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3636 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3637 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3639 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3641 /* WARNING: The following things are taken from d3d7 and were not yet checked
3642 * against d3d8 or d3d9!
3645 /* Clipping conditions: From msdn
3647 * A vertex is clipped if it does not match the following requirements
3648 * -rhw < x <= rhw
3649 * -rhw < y <= rhw
3650 * 0 < z <= rhw
3651 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3653 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3654 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3658 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3659 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3661 /* "Normal" viewport transformation (not clipped)
3662 * 1) The values are divided by rhw
3663 * 2) The y axis is negative, so multiply it with -1
3664 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3665 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3666 * 4) Multiply x with Width/2 and add Width/2
3667 * 5) The same for the height
3668 * 6) Add the viewpoint X and Y to the 2D coordinates and
3669 * The minimum Z value to z
3670 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3672 * Well, basically it's simply a linear transformation into viewport
3673 * coordinates
3676 x /= rhw;
3677 y /= rhw;
3678 z /= rhw;
3680 y *= -1;
3682 x *= vp.width / 2;
3683 y *= vp.height / 2;
3684 z *= max_z - min_z;
3686 x += vp.width / 2 + vp.x;
3687 y += vp.height / 2 + vp.y;
3688 z += min_z;
3690 rhw = 1 / rhw;
3691 } else {
3692 /* That vertex got clipped
3693 * Contrary to OpenGL it is not dropped completely, it just
3694 * undergoes a different calculation.
3696 TRACE("Vertex got clipped\n");
3697 x += rhw;
3698 y += rhw;
3700 x /= 2;
3701 y /= 2;
3703 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3704 * outside of the main vertex buffer memory. That needs some more
3705 * investigation...
3709 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3712 ( (float *) dest_ptr)[0] = x;
3713 ( (float *) dest_ptr)[1] = y;
3714 ( (float *) dest_ptr)[2] = z;
3715 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3717 dest_ptr += 3 * sizeof(float);
3719 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3720 dest_ptr += sizeof(float);
3723 if (dst_fvf & WINED3DFVF_PSIZE)
3724 dest_ptr += sizeof(DWORD);
3726 if (dst_fvf & WINED3DFVF_NORMAL)
3728 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3729 const float *normal = (const float *)(element->data.addr + i * element->stride);
3730 /* AFAIK this should go into the lighting information */
3731 FIXME("Didn't expect the destination to have a normal\n");
3732 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3735 if (lighting)
3737 const struct wined3d_stream_info_element *element;
3738 struct wined3d_vec3 *normal;
3740 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3742 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3743 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3745 else
3747 normal = NULL;
3749 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3750 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3753 if (dst_fvf & WINED3DFVF_DIFFUSE)
3755 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3757 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3758 &state->material.diffuse, i, stream_info);
3760 if (lighting)
3762 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3763 &state->material.ambient, i, stream_info);
3764 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3765 &state->material.emissive, i, stream_info);
3767 diffuse_colour.r = ambient.r * material_ambient.r
3768 + diffuse.r * material_diffuse.r + material_emissive.r;
3769 diffuse_colour.g = ambient.g * material_ambient.g
3770 + diffuse.g * material_diffuse.g + material_emissive.g;
3771 diffuse_colour.b = ambient.b * material_ambient.b
3772 + diffuse.b * material_diffuse.b + material_emissive.b;
3773 diffuse_colour.a = material_diffuse.a;
3775 else
3777 diffuse_colour = material_diffuse;
3779 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3780 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3781 dest_ptr += sizeof(DWORD);
3784 if (dst_fvf & WINED3DFVF_SPECULAR)
3786 struct wined3d_color material_specular, specular_colour;
3788 wined3d_colour_from_mcs(&material_specular, specular_source,
3789 material_specular_state_colour, i, stream_info);
3791 if (lighting)
3793 specular_colour.r = specular.r * material_specular.r;
3794 specular_colour.g = specular.g * material_specular.g;
3795 specular_colour.b = specular.b * material_specular.b;
3796 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3798 else
3800 specular_colour = material_specular;
3802 update_fog_factor(&specular_colour.a, &ls);
3803 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3804 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3805 dest_ptr += sizeof(DWORD);
3808 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3810 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3811 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3812 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3814 ERR("No source texture, but destination requests one\n");
3815 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3817 else
3819 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3824 wined3d_resource_unmap(&dest->resource, 0);
3826 return WINED3D_OK;
3828 #undef copy_and_next
3830 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3831 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3832 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3834 struct wined3d_state *state = device->cs->c.state;
3835 struct wined3d_stream_info stream_info;
3836 struct wined3d_resource *resource;
3837 struct wined3d_box box = {0};
3838 struct wined3d_shader *vs;
3839 unsigned int i, j;
3840 HRESULT hr;
3841 WORD map;
3843 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3844 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3845 device, src_start_idx, dst_idx, vertex_count,
3846 dst_buffer, declaration, flags, dst_fvf);
3848 if (declaration)
3849 FIXME("Output vertex declaration not implemented yet.\n");
3851 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3852 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3853 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3854 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3856 /* We can't convert FROM a VBO, and vertex buffers used to source into
3857 * process_vertices() are unlikely to ever be used for drawing. Release
3858 * VBOs in those buffers and fix up the stream_info structure.
3860 * Also apply the start index. */
3861 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3863 struct wined3d_stream_info_element *e;
3864 struct wined3d_map_desc map_desc;
3866 if (!(map & 1))
3867 continue;
3869 e = &stream_info.elements[i];
3870 resource = &state->streams[e->stream_idx].buffer->resource;
3871 box.left = src_start_idx * e->stride;
3872 box.right = box.left + vertex_count * e->stride;
3873 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3875 ERR("Failed to map resource.\n");
3876 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3878 if (!(map & 1))
3879 continue;
3881 e = &stream_info.elements[j];
3882 resource = &state->streams[e->stream_idx].buffer->resource;
3883 if (FAILED(wined3d_resource_unmap(resource, 0)))
3884 ERR("Failed to unmap resource.\n");
3886 return WINED3DERR_INVALIDCALL;
3888 e->data.buffer_object = 0;
3889 e->data.addr += (ULONG_PTR)map_desc.data;
3892 hr = process_vertices_strided(device, dst_idx, vertex_count,
3893 &stream_info, dst_buffer, flags, dst_fvf);
3895 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3897 if (!(map & 1))
3898 continue;
3900 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3901 if (FAILED(wined3d_resource_unmap(resource, 0)))
3902 ERR("Failed to unmap resource.\n");
3905 return hr;
3908 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3909 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3911 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3913 TRACE("device %p, stage %u, state %s, value %#x.\n",
3914 device, stage, debug_d3dtexturestate(state), value);
3916 if (stage >= d3d_info->limits.ffp_blend_stages)
3918 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3919 stage, d3d_info->limits.ffp_blend_stages - 1);
3920 return;
3923 if (value == device->cs->c.state->texture_states[stage][state])
3925 TRACE("Application is setting the old value over, nothing to do.\n");
3926 return;
3929 device->cs->c.state->texture_states[stage][state] = value;
3931 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3934 static void wined3d_device_set_texture(struct wined3d_device *device,
3935 UINT stage, struct wined3d_texture *texture)
3937 struct wined3d_state *state = device->cs->c.state;
3938 struct wined3d_texture *prev;
3940 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3942 /* Windows accepts overflowing this array... we do not. */
3943 if (stage >= ARRAY_SIZE(state->textures))
3945 WARN("Ignoring invalid stage %u.\n", stage);
3946 return;
3949 prev = state->textures[stage];
3950 TRACE("Previous texture %p.\n", prev);
3952 if (texture == prev)
3954 TRACE("App is setting the same texture again, nothing to do.\n");
3955 return;
3958 TRACE("Setting new texture to %p.\n", texture);
3959 state->textures[stage] = texture;
3961 if (texture)
3962 wined3d_texture_incref(texture);
3963 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3964 if (prev)
3965 wined3d_texture_decref(prev);
3967 return;
3970 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3971 struct wined3d_stateblock *stateblock)
3973 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3974 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3975 const struct wined3d_saved_states *changed = &stateblock->changed;
3976 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3977 struct wined3d_device_context *context = &device->cs->c;
3978 unsigned int i, j, start, idx;
3979 struct wined3d_range range;
3980 uint32_t map;
3982 TRACE("device %p, stateblock %p.\n", device, stateblock);
3984 if (changed->vertexShader)
3985 wined3d_device_set_vertex_shader(device, state->vs);
3986 if (changed->pixelShader)
3987 wined3d_device_set_pixel_shader(device, state->ps);
3989 for (start = 0; ; start = range.offset + range.size)
3991 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3992 break;
3994 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3997 map = changed->vertexShaderConstantsI;
3998 for (start = 0; ; start = range.offset + range.size)
4000 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
4001 break;
4003 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
4006 map = changed->vertexShaderConstantsB;
4007 for (start = 0; ; start = range.offset + range.size)
4009 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
4010 break;
4012 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
4015 for (start = 0; ; start = range.offset + range.size)
4017 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
4018 break;
4020 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
4023 map = changed->pixelShaderConstantsI;
4024 for (start = 0; ; start = range.offset + range.size)
4026 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
4027 break;
4029 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
4032 map = changed->pixelShaderConstantsB;
4033 for (start = 0; ; start = range.offset + range.size)
4035 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
4036 break;
4038 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
4041 if (changed->lights)
4043 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
4045 const struct wined3d_light_info *light;
4047 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
4049 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
4050 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
4055 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
4057 map = changed->renderState[i];
4058 while (map)
4060 j = wined3d_bit_scan(&map);
4061 idx = i * word_bit_count + j;
4063 switch (idx)
4065 case WINED3D_RS_BLENDFACTOR:
4066 case WINED3D_RS_MULTISAMPLEMASK:
4067 case WINED3D_RS_ALPHABLENDENABLE:
4068 case WINED3D_RS_SRCBLEND:
4069 case WINED3D_RS_DESTBLEND:
4070 case WINED3D_RS_BLENDOP:
4071 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
4072 case WINED3D_RS_SRCBLENDALPHA:
4073 case WINED3D_RS_DESTBLENDALPHA:
4074 case WINED3D_RS_BLENDOPALPHA:
4075 case WINED3D_RS_COLORWRITEENABLE:
4076 case WINED3D_RS_COLORWRITEENABLE1:
4077 case WINED3D_RS_COLORWRITEENABLE2:
4078 case WINED3D_RS_COLORWRITEENABLE3:
4079 set_blend_state = TRUE;
4080 break;
4082 case WINED3D_RS_BACK_STENCILFAIL:
4083 case WINED3D_RS_BACK_STENCILFUNC:
4084 case WINED3D_RS_BACK_STENCILPASS:
4085 case WINED3D_RS_BACK_STENCILZFAIL:
4086 case WINED3D_RS_STENCILENABLE:
4087 case WINED3D_RS_STENCILFAIL:
4088 case WINED3D_RS_STENCILFUNC:
4089 case WINED3D_RS_STENCILREF:
4090 case WINED3D_RS_STENCILMASK:
4091 case WINED3D_RS_STENCILPASS:
4092 case WINED3D_RS_STENCILWRITEMASK:
4093 case WINED3D_RS_STENCILZFAIL:
4094 case WINED3D_RS_TWOSIDEDSTENCILMODE:
4095 case WINED3D_RS_ZENABLE:
4096 case WINED3D_RS_ZFUNC:
4097 case WINED3D_RS_ZWRITEENABLE:
4098 set_depth_stencil_state = TRUE;
4099 break;
4101 case WINED3D_RS_FILLMODE:
4102 case WINED3D_RS_CULLMODE:
4103 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
4104 case WINED3D_RS_DEPTHBIAS:
4105 case WINED3D_RS_SCISSORTESTENABLE:
4106 case WINED3D_RS_ANTIALIASEDLINEENABLE:
4107 set_rasterizer_state = TRUE;
4108 break;
4110 default:
4111 wined3d_device_set_render_state(device, idx, state->rs[idx]);
4112 break;
4117 if (set_rasterizer_state)
4119 struct wined3d_rasterizer_state *rasterizer_state;
4120 struct wined3d_rasterizer_state_desc desc;
4121 struct wine_rb_entry *entry;
4122 union
4124 DWORD d;
4125 float f;
4126 } bias;
4128 memset(&desc, 0, sizeof(desc));
4129 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
4130 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
4131 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
4132 desc.depth_bias = bias.f;
4133 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
4134 desc.scale_bias = bias.f;
4135 desc.depth_clip = TRUE;
4136 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
4137 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
4139 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
4141 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
4142 wined3d_device_set_rasterizer_state(device, rasterizer_state);
4144 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
4145 &wined3d_null_parent_ops, &rasterizer_state)))
4147 wined3d_device_set_rasterizer_state(device, rasterizer_state);
4148 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
4150 ERR("Failed to insert rasterizer state.\n");
4151 wined3d_rasterizer_state_decref(rasterizer_state);
4156 if (set_blend_state || changed->alpha_to_coverage
4157 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
4159 struct wined3d_blend_state *blend_state;
4160 struct wined3d_blend_state_desc desc;
4161 struct wine_rb_entry *entry;
4162 struct wined3d_color colour;
4163 unsigned int sample_mask;
4165 memset(&desc, 0, sizeof(desc));
4166 desc.alpha_to_coverage = state->alpha_to_coverage;
4167 desc.independent = FALSE;
4168 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
4169 desc.alpha_to_coverage = TRUE;
4170 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
4171 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
4172 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
4173 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
4174 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
4176 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
4177 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
4178 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
4180 else
4182 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
4183 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
4184 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
4186 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
4187 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
4188 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
4189 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
4190 if (desc.rt[1].writemask != desc.rt[0].writemask
4191 || desc.rt[2].writemask != desc.rt[0].writemask
4192 || desc.rt[3].writemask != desc.rt[0].writemask)
4194 desc.independent = TRUE;
4195 for (i = 1; i < 4; ++i)
4197 desc.rt[i].enable = desc.rt[0].enable;
4198 desc.rt[i].src = desc.rt[0].src;
4199 desc.rt[i].dst = desc.rt[0].dst;
4200 desc.rt[i].op = desc.rt[0].op;
4201 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
4202 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
4203 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
4207 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
4208 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
4209 else
4210 wined3d_device_context_get_blend_state(context, &colour, &sample_mask);
4212 if ((entry = wine_rb_get(&device->blend_states, &desc)))
4214 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
4215 wined3d_device_set_blend_state(device, blend_state, &colour, state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4217 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
4218 &wined3d_null_parent_ops, &blend_state)))
4220 wined3d_device_set_blend_state(device, blend_state, &colour, state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4221 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
4223 ERR("Failed to insert blend state.\n");
4224 wined3d_blend_state_decref(blend_state);
4229 if (set_depth_stencil_state)
4231 struct wined3d_depth_stencil_state *depth_stencil_state;
4232 struct wined3d_depth_stencil_state_desc desc;
4233 struct wine_rb_entry *entry;
4234 unsigned int stencil_ref;
4236 memset(&desc, 0, sizeof(desc));
4237 switch (state->rs[WINED3D_RS_ZENABLE])
4239 case WINED3D_ZB_FALSE:
4240 desc.depth = FALSE;
4241 break;
4243 case WINED3D_ZB_USEW:
4244 FIXME("W buffer is not well handled.\n");
4245 case WINED3D_ZB_TRUE:
4246 desc.depth = TRUE;
4247 break;
4249 default:
4250 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
4252 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
4253 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
4254 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
4255 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
4256 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
4257 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
4258 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
4259 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
4260 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
4262 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
4264 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
4265 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
4266 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
4267 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
4269 else
4271 desc.back = desc.front;
4274 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
4275 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
4276 else
4277 wined3d_device_context_get_depth_stencil_state(context, &stencil_ref);
4279 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
4281 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
4282 wined3d_device_set_depth_stencil_state(device, depth_stencil_state, stencil_ref);
4284 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
4285 &wined3d_null_parent_ops, &depth_stencil_state)))
4287 wined3d_device_set_depth_stencil_state(device, depth_stencil_state, stencil_ref);
4288 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
4290 ERR("Failed to insert depth/stencil state.\n");
4291 wined3d_depth_stencil_state_decref(depth_stencil_state);
4296 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
4298 map = changed->textureState[i];
4299 while (map)
4301 j = wined3d_bit_scan(&map);
4302 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
4306 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
4308 map = changed->samplerState[i];
4309 while (map)
4311 j = wined3d_bit_scan(&map);
4312 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
4316 if (changed->transforms)
4318 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
4320 map = changed->transform[i];
4321 while (map)
4323 j = wined3d_bit_scan(&map);
4324 idx = i * word_bit_count + j;
4325 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
4330 if (changed->indices)
4331 wined3d_device_set_index_buffer(device, state->index_buffer, state->index_format, 0);
4332 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
4333 if (changed->vertexDecl)
4334 wined3d_device_set_vertex_declaration(device, state->vertex_declaration);
4335 if (changed->material)
4336 wined3d_device_set_material(device, &state->material);
4337 if (changed->viewport)
4338 wined3d_device_set_viewports(device, 1, &state->viewport);
4339 if (changed->scissorRect)
4340 wined3d_device_set_scissor_rects(device, 1, &state->scissor_rect);
4342 map = changed->streamSource;
4343 while (map)
4345 i = wined3d_bit_scan(&map);
4346 wined3d_device_set_stream_source(device, i, state->streams[i].buffer,
4347 state->streams[i].offset, state->streams[i].stride);
4349 map = changed->streamFreq;
4350 while (map)
4352 i = wined3d_bit_scan(&map);
4353 wined3d_device_set_stream_source_freq(device, i,
4354 state->streams[i].frequency | state->streams[i].flags);
4357 map = changed->textures;
4358 while (map)
4360 i = wined3d_bit_scan(&map);
4361 wined3d_device_set_texture(device, i, state->textures[i]);
4364 map = changed->clipplane;
4365 while (map)
4367 i = wined3d_bit_scan(&map);
4368 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4371 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4373 TRACE("Applied stateblock %p.\n", stateblock);
4376 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4378 TRACE("device %p, caps %p.\n", device, caps);
4380 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4383 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4384 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4386 struct wined3d_swapchain *swapchain;
4388 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4389 device, swapchain_idx, mode, rotation);
4391 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4392 return WINED3DERR_INVALIDCALL;
4394 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4397 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4399 /* At the moment we have no need for any functionality at the beginning
4400 * of a scene. */
4401 TRACE("device %p.\n", device);
4403 if (device->inScene)
4405 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4406 return WINED3DERR_INVALIDCALL;
4408 device->inScene = TRUE;
4409 return WINED3D_OK;
4412 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4414 TRACE("device %p.\n", device);
4416 if (!device->inScene)
4418 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4419 return WINED3DERR_INVALIDCALL;
4422 device->inScene = FALSE;
4423 return WINED3D_OK;
4426 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4427 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4429 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4431 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4432 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4434 if (!rect_count && rects)
4436 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4437 return WINED3D_OK;
4440 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4442 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4443 if (!ds)
4445 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4446 /* TODO: What about depth stencil buffers without stencil bits? */
4447 return WINED3DERR_INVALIDCALL;
4449 else if (flags & WINED3DCLEAR_TARGET)
4451 if (ds->width < fb->render_targets[0]->width
4452 || ds->height < fb->render_targets[0]->height)
4454 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4455 return WINED3D_OK;
4460 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4462 return WINED3D_OK;
4465 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
4466 struct wined3d_query *predicate, BOOL value)
4468 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
4470 wined3d_device_context_set_predication(&device->cs->c, predicate, value);
4473 struct wined3d_query * CDECL wined3d_device_context_get_predication(struct wined3d_device_context *context, BOOL *value)
4475 struct wined3d_state *state = context->state;
4477 TRACE("context %p, value %p.\n", context, value);
4479 if (value)
4480 *value = state->predicate_value;
4481 return state->predicate;
4484 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
4485 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
4487 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
4488 device, group_count_x, group_count_y, group_count_z);
4490 wined3d_device_context_dispatch(&device->cs->c, group_count_x, group_count_y, group_count_z);
4493 void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
4494 struct wined3d_buffer *buffer, unsigned int offset)
4496 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4498 wined3d_device_context_dispatch_indirect(&device->cs->c, buffer, offset);
4501 void CDECL wined3d_device_context_set_primitive_type(struct wined3d_device_context *context,
4502 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4504 struct wined3d_state *state = context->state;
4506 TRACE("context %p, primitive_type %s, patch_vertex_count %u.\n",
4507 context, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4509 state->primitive_type = primitive_type;
4510 state->patch_vertex_count = patch_vertex_count;
4513 void CDECL wined3d_device_context_get_primitive_type(const struct wined3d_device_context *context,
4514 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4516 const struct wined3d_state *state = context->state;
4518 TRACE("context %p, primitive_type %p, patch_vertex_count %p.\n",
4519 context, primitive_type, patch_vertex_count);
4521 *primitive_type = state->primitive_type;
4522 if (patch_vertex_count)
4523 *patch_vertex_count = state->patch_vertex_count;
4525 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4528 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4530 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4532 wined3d_device_context_draw(&device->cs->c, start_vertex, vertex_count, 0, 0);
4534 return WINED3D_OK;
4537 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
4538 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
4540 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
4541 device, start_vertex, vertex_count, start_instance, instance_count);
4543 wined3d_device_context_draw(&device->cs->c, start_vertex, vertex_count, start_instance, instance_count);
4546 void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
4547 struct wined3d_buffer *buffer, unsigned int offset)
4549 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4551 wined3d_device_context_draw_indirect(&device->cs->c, buffer, offset, false);
4554 void CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4556 struct wined3d_state *state = device->cs->c.state;
4558 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4560 wined3d_device_context_draw_indexed(&device->cs->c, state->base_vertex_index, start_idx, index_count, 0, 0);
4563 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4564 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4566 struct wined3d_state *state = device->cs->c.state;
4568 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4569 device, start_idx, index_count, start_instance, instance_count);
4571 wined3d_device_context_draw_indexed(&device->cs->c, state->base_vertex_index,
4572 start_idx, index_count, start_instance, instance_count);
4575 void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
4576 struct wined3d_buffer *buffer, unsigned int offset)
4578 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4580 wined3d_device_context_draw_indirect(&device->cs->c, buffer, offset, true);
4583 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4584 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4586 unsigned int src_size, dst_size, src_skip_levels = 0;
4587 unsigned int src_level_count, dst_level_count;
4588 const struct wined3d_dirty_regions *regions;
4589 unsigned int layer_count, level_count, i, j;
4590 enum wined3d_resource_type type;
4591 BOOL entire_texture = TRUE;
4592 struct wined3d_box box;
4594 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4596 /* Verify that the source and destination textures are non-NULL. */
4597 if (!src_texture || !dst_texture)
4599 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4600 return WINED3DERR_INVALIDCALL;
4603 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4604 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4606 WARN("Source resource is GPU accessible or a scratch resource.\n");
4607 return WINED3DERR_INVALIDCALL;
4609 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4611 WARN("Destination resource is CPU accessible.\n");
4612 return WINED3DERR_INVALIDCALL;
4615 /* Verify that the source and destination textures are the same type. */
4616 type = src_texture->resource.type;
4617 if (dst_texture->resource.type != type)
4619 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4620 return WINED3DERR_INVALIDCALL;
4623 layer_count = src_texture->layer_count;
4624 if (layer_count != dst_texture->layer_count)
4626 WARN("Source and destination have different layer counts.\n");
4627 return WINED3DERR_INVALIDCALL;
4630 if (src_texture->resource.format != dst_texture->resource.format)
4632 WARN("Source and destination formats do not match.\n");
4633 return WINED3DERR_INVALIDCALL;
4636 src_level_count = src_texture->level_count;
4637 dst_level_count = dst_texture->level_count;
4638 level_count = min(src_level_count, dst_level_count);
4640 src_size = max(src_texture->resource.width, src_texture->resource.height);
4641 src_size = max(src_size, src_texture->resource.depth);
4642 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4643 dst_size = max(dst_size, dst_texture->resource.depth);
4644 while (src_size > dst_size)
4646 src_size >>= 1;
4647 ++src_skip_levels;
4650 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4651 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4652 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4654 WARN("Source and destination dimensions do not match.\n");
4655 return WINED3DERR_INVALIDCALL;
4658 if ((regions = src_texture->dirty_regions))
4660 for (i = 0; i < layer_count && entire_texture; ++i)
4662 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4663 continue;
4665 entire_texture = FALSE;
4666 break;
4670 /* Update every surface level of the texture. */
4671 if (entire_texture)
4673 for (i = 0; i < level_count; ++i)
4675 wined3d_texture_get_level_box(dst_texture, i, &box);
4676 for (j = 0; j < layer_count; ++j)
4678 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4679 &dst_texture->resource, j * dst_level_count + i, &box,
4680 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4681 0, NULL, WINED3D_TEXF_POINT);
4685 else
4687 unsigned int src_level, box_count, k;
4688 const struct wined3d_box *boxes;
4689 struct wined3d_box b;
4691 for (i = 0; i < layer_count; ++i)
4693 boxes = regions[i].boxes;
4694 box_count = regions[i].box_count;
4695 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4697 boxes = &b;
4698 box_count = 1;
4699 wined3d_texture_get_level_box(dst_texture, i, &b);
4702 for (j = 0; j < level_count; ++j)
4704 src_level = j + src_skip_levels;
4706 /* TODO: We could pass an array of boxes here to avoid
4707 * multiple context acquisitions for the same resource. */
4708 for (k = 0; k < box_count; ++k)
4710 box = boxes[k];
4711 if (src_level)
4713 box.left >>= src_level;
4714 box.top >>= src_level;
4715 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4716 wined3d_texture_get_level_width(src_texture, src_level));
4717 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4718 wined3d_texture_get_level_height(src_texture, src_level));
4719 box.front >>= src_level;
4720 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4721 wined3d_texture_get_level_depth(src_texture, src_level));
4724 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4725 &dst_texture->resource, i * dst_level_count + j, &box,
4726 &src_texture->resource, i * src_level_count + src_level, &box,
4727 0, NULL, WINED3D_TEXF_POINT);
4733 wined3d_texture_clear_dirty_regions(src_texture);
4735 return WINED3D_OK;
4738 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4740 const struct wined3d_state *state = device->cs->c.state;
4741 struct wined3d_texture *texture;
4742 DWORD i;
4744 TRACE("device %p, num_passes %p.\n", device, num_passes);
4746 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4748 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4750 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4751 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4753 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4755 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4756 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4759 texture = state->textures[i];
4760 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4762 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4764 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4765 return E_FAIL;
4767 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4769 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4770 return E_FAIL;
4772 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4773 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4775 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4776 return E_FAIL;
4780 if (wined3d_state_uses_depth_buffer(state)
4781 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4783 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4784 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4786 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4788 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4789 return WINED3DERR_CONFLICTINGRENDERSTATE;
4793 /* return a sensible default */
4794 *num_passes = 1;
4796 TRACE("returning D3D_OK\n");
4797 return WINED3D_OK;
4800 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4802 static BOOL warned;
4804 TRACE("device %p, software %#x.\n", device, software);
4806 if (!warned)
4808 FIXME("device %p, software %#x stub!\n", device, software);
4809 warned = TRUE;
4812 device->softwareVertexProcessing = software;
4815 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4817 static BOOL warned;
4819 TRACE("device %p.\n", device);
4821 if (!warned)
4823 TRACE("device %p stub!\n", device);
4824 warned = TRUE;
4827 return device->softwareVertexProcessing;
4830 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4831 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4833 struct wined3d_swapchain *swapchain;
4835 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4836 device, swapchain_idx, raster_status);
4838 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4839 return WINED3DERR_INVALIDCALL;
4841 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4844 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4846 static BOOL warned;
4848 TRACE("device %p, segments %.8e.\n", device, segments);
4850 if (segments != 0.0f)
4852 if (!warned)
4854 FIXME("device %p, segments %.8e stub!\n", device, segments);
4855 warned = TRUE;
4859 return WINED3D_OK;
4862 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4864 static BOOL warned;
4866 TRACE("device %p.\n", device);
4868 if (!warned)
4870 FIXME("device %p stub!\n", device);
4871 warned = TRUE;
4874 return 0.0f;
4877 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4878 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4880 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4881 context, dst_buffer, offset, uav);
4883 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4886 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4887 const struct wined3d_resource *dst_resource)
4889 if (src_resource->format->id == dst_resource->format->id)
4890 return true;
4891 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4892 return true;
4893 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4894 return false;
4895 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4896 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4897 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4898 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4899 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4900 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4901 return false;
4904 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4905 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4907 unsigned int src_row_block_count, dst_row_block_count;
4908 struct wined3d_texture *dst_texture, *src_texture;
4909 unsigned int src_row_count, dst_row_count;
4910 struct wined3d_box src_box, dst_box;
4911 unsigned int i, j;
4913 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4915 if (src_resource == dst_resource)
4917 WARN("Source and destination are the same resource.\n");
4918 return;
4921 if (src_resource->type != dst_resource->type)
4923 WARN("Resource types (%s / %s) don't match.\n",
4924 debug_d3dresourcetype(dst_resource->type),
4925 debug_d3dresourcetype(src_resource->type));
4926 return;
4929 if (!resources_format_compatible(src_resource, dst_resource))
4931 WARN("Resource formats %s and %s are incompatible.\n",
4932 debug_d3dformat(dst_resource->format->id),
4933 debug_d3dformat(src_resource->format->id));
4934 return;
4937 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4938 / src_resource->format->block_width;
4939 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4940 / dst_resource->format->block_width;
4941 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4942 / src_resource->format->block_height;
4943 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4944 / dst_resource->format->block_height;
4946 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4947 || src_resource->depth != dst_resource->depth)
4949 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4950 dst_row_block_count, dst_row_count, dst_resource->depth,
4951 src_row_block_count, src_row_count, src_resource->depth);
4952 return;
4955 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4957 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4958 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4959 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4960 return;
4963 dst_texture = texture_from_resource(dst_resource);
4964 src_texture = texture_from_resource(src_resource);
4966 if (src_texture->layer_count != dst_texture->layer_count
4967 || src_texture->level_count != dst_texture->level_count)
4969 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4970 dst_texture->layer_count, dst_texture->level_count,
4971 src_texture->layer_count, src_texture->level_count);
4972 return;
4975 for (i = 0; i < dst_texture->level_count; ++i)
4977 wined3d_texture_get_level_box(src_texture, i, &src_box);
4978 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4979 for (j = 0; j < dst_texture->layer_count; ++j)
4981 unsigned int idx = j * dst_texture->level_count + i;
4983 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4984 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4989 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4990 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4991 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4992 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4994 struct wined3d_box dst_box, b;
4996 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4997 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4998 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4999 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
5001 if (flags)
5002 FIXME("Ignoring flags %#x.\n", flags);
5004 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
5006 WARN("Source and destination are the same sub-resource.\n");
5007 return WINED3DERR_INVALIDCALL;
5010 if (!resources_format_compatible(src_resource, dst_resource))
5012 WARN("Resource formats %s and %s are incompatible.\n",
5013 debug_d3dformat(dst_resource->format->id),
5014 debug_d3dformat(src_resource->format->id));
5015 return WINED3DERR_INVALIDCALL;
5018 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
5020 if (src_resource->type != WINED3D_RTYPE_BUFFER)
5022 WARN("Resource types (%s / %s) don't match.\n",
5023 debug_d3dresourcetype(dst_resource->type),
5024 debug_d3dresourcetype(src_resource->type));
5025 return WINED3DERR_INVALIDCALL;
5028 if (dst_sub_resource_idx)
5030 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
5031 return WINED3DERR_INVALIDCALL;
5034 if (src_sub_resource_idx)
5036 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
5037 return WINED3DERR_INVALIDCALL;
5040 if (!src_box)
5042 unsigned int dst_w;
5044 dst_w = dst_resource->size - dst_x;
5045 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
5046 src_box = &b;
5048 else if ((src_box->left >= src_box->right
5049 || src_box->top >= src_box->bottom
5050 || src_box->front >= src_box->back))
5052 WARN("Invalid box %s specified.\n", debug_box(src_box));
5053 return WINED3DERR_INVALIDCALL;
5056 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
5057 || src_box->right - src_box->left > dst_resource->size - dst_x)
5059 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
5060 dst_x, src_box->left, src_box->right - src_box->left);
5061 return WINED3DERR_INVALIDCALL;
5064 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
5066 else
5068 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
5069 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
5070 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
5071 unsigned int src_row_block_count, src_row_count;
5073 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
5075 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
5076 return WINED3DERR_INVALIDCALL;
5079 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
5081 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
5082 return WINED3DERR_INVALIDCALL;
5085 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
5087 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
5088 return WINED3DERR_INVALIDCALL;
5091 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
5093 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
5094 return WINED3DERR_INVALIDCALL;
5097 if (!src_box)
5099 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
5101 src_w = wined3d_texture_get_level_width(src_texture, src_level);
5102 src_h = wined3d_texture_get_level_height(src_texture, src_level);
5103 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
5105 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5106 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
5107 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
5108 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
5110 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
5111 src_box = &b;
5113 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
5115 WARN("Invalid source box %s.\n", debug_box(src_box));
5116 return WINED3DERR_INVALIDCALL;
5119 if (src_resource->format->block_width == dst_resource->format->block_width
5120 && src_resource->format->block_height == dst_resource->format->block_height)
5122 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
5123 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
5125 else
5127 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
5128 / src_resource->format->block_width;
5129 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
5130 / src_resource->format->block_height;
5131 wined3d_box_set(&dst_box, dst_x, dst_y,
5132 dst_x + (src_row_block_count * dst_resource->format->block_width),
5133 dst_y + (src_row_count * dst_resource->format->block_height),
5134 dst_z, dst_z + (src_box->back - src_box->front));
5136 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
5137 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
5139 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
5140 return WINED3DERR_INVALIDCALL;
5144 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
5145 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5147 return WINED3D_OK;
5150 void CDECL wined3d_device_context_update_sub_resource(struct wined3d_device_context *context,
5151 struct wined3d_resource *resource, unsigned int sub_resource_idx, const struct wined3d_box *box,
5152 const void *data, unsigned int row_pitch, unsigned int depth_pitch, unsigned int flags)
5154 unsigned int width, height, depth;
5155 struct wined3d_box b;
5157 TRACE("context %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, flags %#x.\n",
5158 context, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
5160 if (flags)
5161 FIXME("Ignoring flags %#x.\n", flags);
5163 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5165 WARN("Resource %p is not GPU accessible.\n", resource);
5166 return;
5169 if (resource->type == WINED3D_RTYPE_BUFFER)
5171 if (sub_resource_idx > 0)
5173 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
5174 return;
5177 width = resource->size;
5178 height = 1;
5179 depth = 1;
5181 else
5183 struct wined3d_texture *texture = texture_from_resource(resource);
5184 unsigned int level;
5186 if (sub_resource_idx >= texture->level_count * texture->layer_count)
5188 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
5189 return;
5192 level = sub_resource_idx % texture->level_count;
5193 width = wined3d_texture_get_level_width(texture, level);
5194 height = wined3d_texture_get_level_height(texture, level);
5195 depth = wined3d_texture_get_level_depth(texture, level);
5198 if (!box)
5200 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
5201 box = &b;
5203 else if (box->left >= box->right || box->right > width
5204 || box->top >= box->bottom || box->bottom > height
5205 || box->front >= box->back || box->back > depth)
5207 WARN("Invalid box %s specified.\n", debug_box(box));
5208 return;
5211 context->ops->update_sub_resource(context, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
5214 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
5215 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
5216 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
5217 enum wined3d_format_id format_id)
5219 struct wined3d_texture *dst_texture, *src_texture;
5220 unsigned int dst_level, src_level;
5221 struct wined3d_blt_fx fx = {0};
5222 RECT dst_rect, src_rect;
5224 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
5225 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
5226 context, dst_resource, dst_sub_resource_idx,
5227 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
5229 if (wined3d_format_is_typeless(dst_resource->format)
5230 || wined3d_format_is_typeless(src_resource->format))
5232 FIXME("Multisample resolve is not fully supported for typeless formats "
5233 "(dst_format %s, src_format %s, format %s).\n",
5234 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
5235 debug_d3dformat(format_id));
5237 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5239 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
5240 return;
5242 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5244 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
5245 return;
5248 fx.resolve_format_id = format_id;
5250 dst_texture = texture_from_resource(dst_resource);
5251 src_texture = texture_from_resource(src_resource);
5253 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5254 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
5255 wined3d_texture_get_level_height(dst_texture, dst_level));
5256 src_level = src_sub_resource_idx % src_texture->level_count;
5257 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
5258 wined3d_texture_get_level_height(src_texture, src_level));
5259 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
5260 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
5263 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
5264 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
5265 const struct wined3d_color *color, float depth, unsigned int stencil)
5267 struct wined3d_resource *resource;
5268 RECT r;
5270 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
5271 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
5273 if (!flags)
5274 return WINED3D_OK;
5276 resource = view->resource;
5277 if (resource->type == WINED3D_RTYPE_BUFFER)
5279 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
5280 return WINED3DERR_INVALIDCALL;
5283 if (!rect)
5285 SetRect(&r, 0, 0, view->width, view->height);
5286 rect = &r;
5288 else
5290 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
5291 struct wined3d_texture *texture = texture_from_resource(view->resource);
5292 HRESULT hr;
5294 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
5295 view->sub_resource_idx % texture->level_count, &b)))
5296 return hr;
5299 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
5301 return WINED3D_OK;
5304 void CDECL wined3d_device_context_clear_uav_uint(struct wined3d_device_context *context,
5305 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
5307 TRACE("context %p, view %p, clear_value %s.\n", context, view, debug_uvec4(clear_value));
5309 wined3d_device_context_emit_clear_uav_uint(context, view, clear_value);
5312 static unsigned int sanitise_map_flags(const struct wined3d_resource *resource, unsigned int flags)
5314 /* Not all flags make sense together, but Windows never returns an error.
5315 * Catch the cases that could cause issues. */
5316 if (flags & WINED3D_MAP_READ)
5318 if (flags & WINED3D_MAP_DISCARD)
5320 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
5321 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5323 if (flags & WINED3D_MAP_NOOVERWRITE)
5325 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
5326 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5329 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5331 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
5333 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
5334 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
5336 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5337 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
5339 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
5340 flags &= ~WINED3D_MAP_DISCARD;
5344 return flags;
5347 HRESULT CDECL wined3d_device_context_map(struct wined3d_device_context *context,
5348 struct wined3d_resource *resource, unsigned int sub_resource_idx,
5349 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, unsigned int flags)
5351 TRACE("context %p, resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
5352 context, resource, sub_resource_idx, map_desc, debug_box(box), flags);
5354 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
5356 WARN("No read/write flags specified.\n");
5357 return E_INVALIDARG;
5360 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
5362 WARN("Resource does not have MAP_R access.\n");
5363 return E_INVALIDARG;
5366 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
5368 WARN("Resource does not have MAP_W access.\n");
5369 return E_INVALIDARG;
5372 flags = sanitise_map_flags(resource, flags);
5374 return context->ops->map(context, resource, sub_resource_idx, map_desc, box, flags);
5377 HRESULT CDECL wined3d_device_context_unmap(struct wined3d_device_context *context,
5378 struct wined3d_resource *resource, unsigned int sub_resource_idx)
5380 TRACE("context %p, resource %p, sub_resource_idx %u.\n", context, resource, sub_resource_idx);
5382 return context->ops->unmap(context, resource, sub_resource_idx);
5385 void CDECL wined3d_device_context_issue_query(struct wined3d_device_context *context,
5386 struct wined3d_query *query, unsigned int flags)
5388 TRACE("context %p, query %p, flags %#x.\n", context, query, flags);
5390 context->ops->issue_query(context, query, flags);
5393 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_rendertarget_view(
5394 const struct wined3d_device_context *context, unsigned int view_idx)
5396 unsigned int max_rt_count;
5398 TRACE("context %p, view_idx %u.\n", context, view_idx);
5400 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
5401 if (view_idx >= max_rt_count)
5403 WARN("Only %u render targets are supported.\n", max_rt_count);
5404 return NULL;
5407 return context->state->fb.render_targets[view_idx];
5410 struct wined3d_rendertarget_view * CDECL wined3d_device_context_get_depth_stencil_view(
5411 const struct wined3d_device_context *context)
5413 TRACE("context %p.\n", context);
5415 return context->state->fb.depth_stencil;
5418 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
5419 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
5421 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
5422 device, view_idx, view, set_viewport);
5424 return wined3d_device_context_set_rendertarget_view(&device->cs->c, view_idx, view, set_viewport);
5427 HRESULT CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device,
5428 struct wined3d_rendertarget_view *view)
5430 TRACE("device %p, view %p.\n", device, view);
5432 return wined3d_device_context_set_depth_stencil_view(&device->cs->c, view);
5435 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
5436 struct wined3d_shader_resource_view *view)
5438 struct wined3d_texture *texture;
5440 TRACE("context %p, view %p.\n", context, view);
5442 if (view->resource->type == WINED3D_RTYPE_BUFFER)
5444 WARN("Called on buffer resource %p.\n", view->resource);
5445 return;
5448 texture = texture_from_resource(view->resource);
5449 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
5451 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
5452 return;
5455 wined3d_device_context_emit_generate_mipmaps(context, view);
5458 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5459 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5461 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5462 struct wined3d_sub_resource_data data;
5463 struct wined3d_resource_desc desc;
5464 struct wined3d_map_desc map_desc;
5465 struct wined3d_texture *texture;
5466 HRESULT hr;
5468 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5470 ERR("Failed to map source texture.\n");
5471 return NULL;
5474 data.data = map_desc.data;
5475 data.row_pitch = map_desc.row_pitch;
5476 data.slice_pitch = map_desc.slice_pitch;
5478 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5479 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5480 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5481 desc.multisample_quality = 0;
5482 desc.usage = WINED3DUSAGE_DYNAMIC;
5483 desc.bind_flags = 0;
5484 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5485 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5486 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5487 desc.depth = 1;
5488 desc.size = 0;
5490 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5491 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5492 if (FAILED(hr))
5494 ERR("Failed to create cursor texture.\n");
5495 return NULL;
5498 return texture;
5501 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5502 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5504 unsigned int texture_level = sub_resource_idx % texture->level_count;
5505 unsigned int cursor_width, cursor_height;
5506 struct wined3d_map_desc map_desc;
5508 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5509 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5511 if (sub_resource_idx >= texture->level_count * texture->layer_count
5512 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5513 return WINED3DERR_INVALIDCALL;
5515 if (device->cursor_texture)
5517 wined3d_texture_decref(device->cursor_texture);
5518 device->cursor_texture = NULL;
5521 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5523 WARN("Texture %p has invalid format %s.\n",
5524 texture, debug_d3dformat(texture->resource.format->id));
5525 return WINED3DERR_INVALIDCALL;
5528 /* Cursor width and height must all be powers of two */
5529 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5530 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5531 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5533 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5534 return WINED3DERR_INVALIDCALL;
5537 /* Do not store the surface's pointer because the application may
5538 * release it after setting the cursor image. Windows doesn't
5539 * addref the set surface, so we can't do this either without
5540 * creating circular refcount dependencies. */
5541 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5543 ERR("Failed to create cursor texture.\n");
5544 return WINED3DERR_INVALIDCALL;
5547 if (cursor_width == 32 && cursor_height == 32)
5549 UINT mask_size = cursor_width * cursor_height / 8;
5550 ICONINFO cursor_info;
5551 DWORD *mask_bits;
5552 HCURSOR cursor;
5554 /* 32-bit user32 cursors ignore the alpha channel if it's all
5555 * zeroes, and use the mask instead. Fill the mask with all ones
5556 * to ensure we still get a fully transparent cursor. */
5557 if (!(mask_bits = heap_alloc(mask_size)))
5558 return E_OUTOFMEMORY;
5559 memset(mask_bits, 0xff, mask_size);
5561 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5562 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5563 cursor_info.fIcon = FALSE;
5564 cursor_info.xHotspot = x_hotspot;
5565 cursor_info.yHotspot = y_hotspot;
5566 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5567 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5568 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5570 /* Create our cursor and clean up. */
5571 cursor = CreateIconIndirect(&cursor_info);
5572 if (cursor_info.hbmMask)
5573 DeleteObject(cursor_info.hbmMask);
5574 if (cursor_info.hbmColor)
5575 DeleteObject(cursor_info.hbmColor);
5576 if (device->hardwareCursor)
5577 DestroyCursor(device->hardwareCursor);
5578 device->hardwareCursor = cursor;
5579 if (device->bCursorVisible)
5580 SetCursor(cursor);
5582 heap_free(mask_bits);
5585 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5586 device->cursorWidth = cursor_width;
5587 device->cursorHeight = cursor_height;
5588 device->xHotSpot = x_hotspot;
5589 device->yHotSpot = y_hotspot;
5591 return WINED3D_OK;
5594 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5595 int x_screen_space, int y_screen_space, DWORD flags)
5597 TRACE("device %p, x %d, y %d, flags %#x.\n",
5598 device, x_screen_space, y_screen_space, flags);
5600 device->xScreenSpace = x_screen_space;
5601 device->yScreenSpace = y_screen_space;
5603 if (device->hardwareCursor)
5605 POINT pt;
5607 GetCursorPos( &pt );
5608 if (x_screen_space == pt.x && y_screen_space == pt.y)
5609 return;
5610 SetCursorPos( x_screen_space, y_screen_space );
5612 /* Switch to the software cursor if position diverges from the hardware one. */
5613 GetCursorPos( &pt );
5614 if (x_screen_space != pt.x || y_screen_space != pt.y)
5616 if (device->bCursorVisible) SetCursor( NULL );
5617 DestroyCursor( device->hardwareCursor );
5618 device->hardwareCursor = 0;
5623 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5625 BOOL oldVisible = device->bCursorVisible;
5627 TRACE("device %p, show %#x.\n", device, show);
5630 * When ShowCursor is first called it should make the cursor appear at the OS's last
5631 * known cursor position.
5633 if (show && !oldVisible)
5635 POINT pt;
5636 GetCursorPos(&pt);
5637 device->xScreenSpace = pt.x;
5638 device->yScreenSpace = pt.y;
5641 if (device->hardwareCursor)
5643 device->bCursorVisible = show;
5644 if (show)
5645 SetCursor(device->hardwareCursor);
5646 else
5647 SetCursor(NULL);
5649 else if (device->cursor_texture)
5651 device->bCursorVisible = show;
5654 return oldVisible;
5657 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5659 struct wined3d_resource *resource, *cursor;
5661 TRACE("device %p.\n", device);
5663 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5665 TRACE("Checking resource %p for eviction.\n", resource);
5667 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5669 TRACE("Evicting %p.\n", resource);
5670 wined3d_cs_emit_unload_resource(device->cs, resource);
5675 void CDECL wined3d_device_context_flush(struct wined3d_device_context *context)
5677 TRACE("context %p.\n", context);
5679 context->ops->flush(context);
5682 static void update_swapchain_flags(struct wined3d_texture *texture)
5684 unsigned int flags = texture->swapchain->state.desc.flags;
5686 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5687 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5688 else
5689 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5691 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5692 texture->flags |= WINED3D_TEXTURE_GET_DC;
5693 else
5694 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5697 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5698 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5699 wined3d_device_reset_cb callback, BOOL reset_state)
5701 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5702 struct wined3d_swapchain_state *swapchain_state;
5703 struct wined3d_swapchain_desc *current_desc;
5704 struct wined3d_state *state = device->cs->c.state;
5705 struct wined3d_resource *resource, *cursor;
5706 struct wined3d_rendertarget_view *view;
5707 struct wined3d_swapchain *swapchain;
5708 struct wined3d_view_desc view_desc;
5709 BOOL backbuffer_resized, windowed;
5710 HRESULT hr = WINED3D_OK;
5711 unsigned int i;
5713 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5714 device, swapchain_desc, mode, callback, reset_state);
5716 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5718 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5720 ERR("Failed to get the first implicit swapchain.\n");
5721 return WINED3DERR_INVALIDCALL;
5723 swapchain_state = &swapchain->state;
5724 current_desc = &swapchain_state->desc;
5726 if (reset_state)
5728 if (device->logo_texture)
5730 wined3d_texture_decref(device->logo_texture);
5731 device->logo_texture = NULL;
5733 if (device->cursor_texture)
5735 wined3d_texture_decref(device->cursor_texture);
5736 device->cursor_texture = NULL;
5738 state_unbind_resources(state);
5741 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
5743 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
5745 wined3d_device_set_depth_stencil_view(device, NULL);
5747 if (reset_state)
5749 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5751 TRACE("Enumerating resource %p.\n", resource);
5752 if (FAILED(hr = callback(resource)))
5753 return hr;
5757 TRACE("New params:\n");
5758 TRACE("output %p\n", swapchain_desc->output);
5759 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5760 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5761 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5762 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5763 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5764 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5765 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5766 TRACE("device_window %p\n", swapchain_desc->device_window);
5767 TRACE("windowed %#x\n", swapchain_desc->windowed);
5768 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5769 if (swapchain_desc->enable_auto_depth_stencil)
5770 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5771 TRACE("flags %#x\n", swapchain_desc->flags);
5772 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5773 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5775 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5776 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5778 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5779 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5780 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5781 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5783 /* No special treatment of these parameters. Just store them */
5784 current_desc->swap_effect = swapchain_desc->swap_effect;
5785 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5786 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5787 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5788 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5790 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5792 TRACE("Changing the device window from %p to %p.\n",
5793 current_desc->device_window, swapchain_desc->device_window);
5794 current_desc->device_window = swapchain_desc->device_window;
5795 swapchain_state->device_window = swapchain_desc->device_window;
5796 wined3d_swapchain_set_window(swapchain, NULL);
5799 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5800 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5801 windowed = current_desc->windowed;
5803 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5804 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5806 /* Switch from windowed to fullscreen. */
5807 if (windowed && !swapchain_desc->windowed)
5809 HWND focus_window = device->create_parms.focus_window;
5811 if (!focus_window)
5812 focus_window = swapchain->state.device_window;
5813 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5815 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5816 return hr;
5820 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5821 swapchain_desc, mode)))
5822 return hr;
5824 /* Switch from fullscreen to windowed. */
5825 if (!windowed && swapchain_desc->windowed)
5826 wined3d_device_release_focus_window(device);
5828 else if (!swapchain_desc->windowed)
5830 DWORD style = swapchain_state->style;
5831 DWORD exstyle = swapchain_state->exstyle;
5832 struct wined3d_output_desc output_desc;
5834 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5835 * the window back into the right position. Some applications
5836 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5837 * up their mess. Guild Wars also loses the device during that. */
5838 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5840 ERR("Failed to get output description, hr %#x.\n", hr);
5841 return hr;
5844 swapchain_state->style = 0;
5845 swapchain_state->exstyle = 0;
5846 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5847 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5848 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5849 swapchain_state->style = style;
5850 swapchain_state->exstyle = exstyle;
5853 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5854 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5855 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5856 return hr;
5858 if (swapchain_desc->flags != current_desc->flags)
5860 current_desc->flags = swapchain_desc->flags;
5862 update_swapchain_flags(swapchain->front_buffer);
5863 for (i = 0; i < current_desc->backbuffer_count; ++i)
5865 update_swapchain_flags(swapchain->back_buffers[i]);
5869 if ((view = device->auto_depth_stencil_view))
5871 device->auto_depth_stencil_view = NULL;
5872 wined3d_rendertarget_view_decref(view);
5874 if (current_desc->enable_auto_depth_stencil)
5876 struct wined3d_resource_desc texture_desc;
5877 struct wined3d_texture *texture;
5879 TRACE("Creating the depth stencil buffer.\n");
5881 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5882 texture_desc.format = current_desc->auto_depth_stencil_format;
5883 texture_desc.multisample_type = current_desc->multisample_type;
5884 texture_desc.multisample_quality = current_desc->multisample_quality;
5885 texture_desc.usage = 0;
5886 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5887 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5888 texture_desc.width = current_desc->backbuffer_width;
5889 texture_desc.height = current_desc->backbuffer_height;
5890 texture_desc.depth = 1;
5891 texture_desc.size = 0;
5893 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5894 device->device_parent, &texture_desc, 0, &texture)))
5896 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5897 return WINED3DERR_INVALIDCALL;
5900 view_desc.format_id = texture->resource.format->id;
5901 view_desc.flags = 0;
5902 view_desc.u.texture.level_idx = 0;
5903 view_desc.u.texture.level_count = 1;
5904 view_desc.u.texture.layer_idx = 0;
5905 view_desc.u.texture.layer_count = 1;
5906 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5907 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5908 wined3d_texture_decref(texture);
5909 if (FAILED(hr))
5911 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5912 return hr;
5916 if ((view = device->back_buffer_view))
5918 device->back_buffer_view = NULL;
5919 wined3d_rendertarget_view_decref(view);
5921 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5923 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5925 view_desc.format_id = back_buffer->format->id;
5926 view_desc.flags = 0;
5927 view_desc.u.texture.level_idx = 0;
5928 view_desc.u.texture.level_count = 1;
5929 view_desc.u.texture.layer_idx = 0;
5930 view_desc.u.texture.layer_count = 1;
5931 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5932 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5934 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5935 return hr;
5939 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5940 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5941 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5942 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5944 if (reset_state)
5946 TRACE("Resetting state.\n");
5947 wined3d_cs_emit_reset_state(device->cs);
5948 state_cleanup(state);
5950 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5952 TRACE("Unloading resource %p.\n", resource);
5953 wined3d_cs_emit_unload_resource(device->cs, resource);
5956 device->adapter->adapter_ops->adapter_uninit_3d(device);
5958 wined3d_state_reset(state, &device->adapter->d3d_info);
5960 device_init_swapchain_state(device, swapchain);
5961 if (wined3d_settings.logo)
5962 device_load_logo(device, wined3d_settings.logo);
5964 else
5966 if ((view = device->back_buffer_view))
5967 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
5968 if ((view = device->auto_depth_stencil_view))
5969 wined3d_device_set_depth_stencil_view(device, view);
5972 if (reset_state)
5973 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5975 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5976 * first use
5978 return hr;
5981 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5983 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5985 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5987 return WINED3D_OK;
5991 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5992 struct wined3d_device_creation_parameters *parameters)
5994 TRACE("device %p, parameters %p.\n", device, parameters);
5996 *parameters = device->create_parms;
5999 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
6001 TRACE("device %p.\n", device);
6003 return device->wined3d;
6006 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
6007 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
6009 struct wined3d_swapchain *swapchain;
6011 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
6012 device, swapchain_idx, flags, ramp);
6014 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
6015 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
6018 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
6019 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
6021 struct wined3d_swapchain *swapchain;
6023 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
6024 device, swapchain_idx, ramp);
6026 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
6027 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
6030 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
6032 TRACE("device %p, resource %p.\n", device, resource);
6034 wined3d_not_from_cs(device->cs);
6036 list_add_head(&device->resources, &resource->resource_list_entry);
6039 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
6041 TRACE("device %p, resource %p.\n", device, resource);
6043 wined3d_not_from_cs(device->cs);
6045 list_remove(&resource->resource_list_entry);
6048 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
6050 enum wined3d_resource_type type = resource->type;
6051 struct wined3d_state *state = device->cs->c.state;
6052 struct wined3d_rendertarget_view *rtv;
6053 unsigned int i;
6055 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
6057 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
6059 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
6060 ERR("Resource %p is still in use as render target %u.\n", resource, i);
6063 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
6064 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
6066 switch (type)
6068 case WINED3D_RTYPE_TEXTURE_1D:
6069 case WINED3D_RTYPE_TEXTURE_2D:
6070 case WINED3D_RTYPE_TEXTURE_3D:
6071 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
6073 if (&state->textures[i]->resource == resource)
6075 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
6076 state->textures[i] = NULL;
6079 break;
6081 case WINED3D_RTYPE_BUFFER:
6082 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
6084 if (&state->streams[i].buffer->resource == resource)
6086 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
6087 state->streams[i].buffer = NULL;
6091 if (&state->index_buffer->resource == resource)
6093 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
6094 state->index_buffer = NULL;
6096 break;
6098 default:
6099 break;
6102 /* Remove the resource from the resourceStore */
6103 device_resource_remove(device, resource);
6105 TRACE("Resource released.\n");
6108 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
6110 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
6111 struct wined3d_so_desc_entry, entry)->desc;
6112 const struct wined3d_stream_output_desc *k = key;
6113 unsigned int i;
6114 int ret;
6116 if ((ret = (k->element_count - desc->element_count)))
6117 return ret;
6118 if ((ret = (k->buffer_stride_count - desc->buffer_stride_count)))
6119 return ret;
6120 if ((ret = (k->rasterizer_stream_idx - desc->rasterizer_stream_idx)))
6121 return ret;
6123 for (i = 0; i < k->element_count; ++i)
6125 const struct wined3d_stream_output_element *b = &desc->elements[i];
6126 const struct wined3d_stream_output_element *a = &k->elements[i];
6128 if ((ret = (a->stream_idx - b->stream_idx)))
6129 return ret;
6130 if ((ret = strcmp(a->semantic_name, b->semantic_name)))
6131 return ret;
6132 if ((ret = (a->semantic_idx - b->semantic_idx)))
6133 return ret;
6134 if ((ret = (a->component_idx - b->component_idx)))
6135 return ret;
6136 if ((ret = (a->component_count - b->component_count)))
6137 return ret;
6138 if ((ret = (a->output_slot - b->output_slot)))
6139 return ret;
6142 for (i = 0; i < k->buffer_stride_count; ++i)
6144 if ((ret = (k->buffer_strides[i] - desc->buffer_strides[i])))
6145 return ret;
6148 return 0;
6151 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
6153 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
6155 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
6158 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
6160 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
6162 return memcmp(&state->desc, key, sizeof(state->desc));
6165 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
6167 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
6169 return memcmp(&state->desc, key, sizeof(state->desc));
6172 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
6174 const struct wined3d_depth_stencil_state *state
6175 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
6177 return memcmp(&state->desc, key, sizeof(state->desc));
6180 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
6181 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
6182 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
6183 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
6185 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
6186 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
6187 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
6188 unsigned int i;
6189 HRESULT hr;
6191 device->ref = 1;
6192 device->wined3d = wined3d;
6193 wined3d_incref(device->wined3d);
6194 device->adapter = adapter;
6195 device->device_parent = device_parent;
6196 list_init(&device->resources);
6197 list_init(&device->shaders);
6198 device->surface_alignment = surface_alignment;
6200 /* Save the creation parameters. */
6201 device->create_parms.adapter_idx = adapter_idx;
6202 device->create_parms.device_type = device_type;
6203 device->create_parms.focus_window = focus_window;
6204 device->create_parms.flags = flags;
6206 device->shader_backend = adapter->shader_backend;
6208 vertex_pipeline = adapter->vertex_pipe;
6210 fragment_pipeline = adapter->fragment_pipe;
6212 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
6213 wine_rb_init(&device->samplers, wined3d_sampler_compare);
6214 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
6215 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
6216 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
6218 if (vertex_pipeline->vp_states && fragment_pipeline->states
6219 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
6220 &adapter->d3d_info, supported_extensions, vertex_pipeline,
6221 fragment_pipeline, adapter->misc_state_template)))
6223 ERR("Failed to compile state table, hr %#x.\n", hr);
6224 wine_rb_destroy(&device->samplers, NULL, NULL);
6225 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6226 wine_rb_destroy(&device->blend_states, NULL, NULL);
6227 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6228 wine_rb_destroy(&device->so_descs, NULL, NULL);
6229 wined3d_decref(device->wined3d);
6230 return hr;
6233 device->max_frame_latency = 3;
6235 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
6237 WARN("Failed to create command stream.\n");
6238 hr = E_FAIL;
6239 goto err;
6242 return WINED3D_OK;
6244 err:
6245 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
6247 heap_free(device->multistate_funcs[i]);
6249 wine_rb_destroy(&device->samplers, NULL, NULL);
6250 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6251 wine_rb_destroy(&device->blend_states, NULL, NULL);
6252 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6253 wine_rb_destroy(&device->so_descs, NULL, NULL);
6254 wined3d_decref(device->wined3d);
6255 return hr;
6258 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
6260 unsigned int representative, i, idx, shift;
6261 struct wined3d_context *context;
6263 wined3d_from_cs(device->cs);
6265 if (STATE_IS_COMPUTE(state_id))
6267 for (i = 0; i < device->context_count; ++i)
6268 context_invalidate_compute_state(device->contexts[i], state_id);
6269 return;
6272 representative = device->state_table[state_id].representative;
6273 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
6274 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
6275 for (i = 0; i < device->context_count; ++i)
6277 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
6281 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
6282 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
6284 if (message == WM_DESTROY)
6286 TRACE("unregister window %p.\n", window);
6287 wined3d_unregister_window(window);
6289 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
6290 ERR("Window %p is not the focus window for device %p.\n", window, device);
6292 else if (message == WM_DISPLAYCHANGE)
6294 device->device_parent->ops->mode_changed(device->device_parent);
6296 else if (message == WM_ACTIVATEAPP)
6298 unsigned int i = device->swapchain_count;
6300 /* Deactivating the implicit swapchain may cause the application
6301 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
6302 * deactivate the implicit swapchain last, and to avoid accessing the
6303 * "device" pointer afterwards. */
6304 while (i--)
6305 wined3d_swapchain_activate(device->swapchains[i], wparam);
6307 else if (message == WM_SYSCOMMAND)
6309 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
6311 if (unicode)
6312 DefWindowProcW(window, message, wparam, lparam);
6313 else
6314 DefWindowProcA(window, message, wparam, lparam);
6318 if (unicode)
6319 return CallWindowProcW(proc, window, message, wparam, lparam);
6320 else
6321 return CallWindowProcA(proc, window, message, wparam, lparam);