wined3d: Pass a wined3d_device_context to wined3d_cs_emit_clear_unordered_access_view...
[wine.git] / dlls / wined3d / device.c
blob6df681003566bdd0f1474e97c793112c77f2e4eb
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;
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.image = r->image_2d.vk_image;
782 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
783 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d.imageView))) < 0)
785 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr));
786 goto fail;
788 v->vk_info_2d.sampler = VK_NULL_HANDLE;
789 v->vk_info_2d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
790 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d.imageView));
792 view_desc.image = r->image_2dms.vk_image;
793 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D;
794 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms.imageView))) < 0)
796 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr));
797 goto fail;
799 v->vk_info_2dms.sampler = VK_NULL_HANDLE;
800 v->vk_info_2dms.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
801 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms.imageView));
803 view_desc.image = r->image_3d.vk_image;
804 view_desc.viewType = VK_IMAGE_VIEW_TYPE_3D;
805 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_3d.imageView))) < 0)
807 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr));
808 goto fail;
810 v->vk_info_3d.sampler = VK_NULL_HANDLE;
811 v->vk_info_3d.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
812 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_3d.imageView));
814 view_desc.image = r->image_2d.vk_image;
815 view_desc.subresourceRange.layerCount = 6;
816 view_desc.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
817 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_cube.imageView))) < 0)
819 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr));
820 goto fail;
822 v->vk_info_cube.sampler = VK_NULL_HANDLE;
823 v->vk_info_cube.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
824 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_cube.imageView));
826 view_desc.subresourceRange.layerCount = 1;
827 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
828 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2d_array.imageView))) < 0)
830 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr));
831 goto fail;
833 v->vk_info_2d_array.sampler = VK_NULL_HANDLE;
834 v->vk_info_2d_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
835 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2d_array.imageView));
837 view_desc.image = r->image_2dms.vk_image;
838 view_desc.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
839 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &view_desc, NULL, &v->vk_info_2dms_array.imageView))) < 0)
841 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr));
842 goto fail;
844 v->vk_info_2dms_array.sampler = VK_NULL_HANDLE;
845 v->vk_info_2dms_array.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
846 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v->vk_info_2dms_array.imageView));
848 return true;
850 fail:
851 if (v->vk_info_2d_array.imageView)
852 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d_array.imageView, NULL));
853 if (v->vk_info_cube.imageView)
854 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_cube.imageView, NULL));
855 if (v->vk_info_3d.imageView)
856 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_3d.imageView, NULL));
857 if (v->vk_info_2dms.imageView)
858 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2dms.imageView, NULL));
859 if (v->vk_info_2d.imageView)
860 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_2d.imageView, NULL));
861 if (v->vk_info_1d.imageView)
862 VK_CALL(vkDestroyImageView(device_vk->vk_device, v->vk_info_1d.imageView, NULL));
863 if (v->vk_view_buffer_float)
864 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_float, NULL));
865 VK_CALL(vkDestroyBufferView(device_vk->vk_device, v->vk_view_buffer_uint, NULL));
866 return false;
869 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk *device_vk, struct wined3d_context_vk *context_vk)
871 struct wined3d_null_views_vk *v = &device_vk->null_views_vk;
872 uint64_t id = context_vk->current_command_buffer.id;
874 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms_array.imageView, id);
875 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d_array.imageView, id);
876 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_cube.imageView, id);
877 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_3d.imageView, id);
878 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2dms.imageView, id);
879 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_2d.imageView, id);
880 wined3d_context_vk_destroy_vk_image_view(context_vk, v->vk_info_1d.imageView, id);
882 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_float, id);
883 wined3d_context_vk_destroy_vk_buffer_view(context_vk, v->vk_view_buffer_uint, id);
886 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
888 unsigned int screensaver_active;
890 TRACE("device %p, window %p.\n", device, window);
892 if (!wined3d_register_window(NULL, window, device, 0))
894 ERR("Failed to register window %p.\n", window);
895 return E_FAIL;
898 InterlockedExchangePointer((void **)&device->focus_window, window);
899 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
900 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE, 0, &screensaver_active, 0);
901 if ((device->restore_screensaver = !!screensaver_active))
902 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
904 return WINED3D_OK;
907 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
909 TRACE("device %p.\n", device);
911 if (device->focus_window) wined3d_unregister_window(device->focus_window);
912 InterlockedExchangePointer((void **)&device->focus_window, NULL);
913 if (device->restore_screensaver)
915 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
916 device->restore_screensaver = FALSE;
920 static void device_init_swapchain_state(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
922 BOOL ds_enable = swapchain->state.desc.enable_auto_depth_stencil;
923 unsigned int i;
925 for (i = 0; i < device->adapter->d3d_info.limits.max_rt_count; ++i)
927 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
929 if (device->back_buffer_view)
930 wined3d_device_set_rendertarget_view(device, 0, device->back_buffer_view, TRUE);
932 wined3d_device_set_depth_stencil_view(device, ds_enable ? device->auto_depth_stencil_view : NULL);
935 void wined3d_device_delete_opengl_contexts_cs(void *object)
937 struct wined3d_swapchain_gl *swapchain_gl;
938 struct wined3d_device *device = object;
939 struct wined3d_context_gl *context_gl;
940 struct wined3d_device_gl *device_gl;
941 struct wined3d_context *context;
942 struct wined3d_shader *shader;
944 TRACE("device %p.\n", device);
946 device_gl = wined3d_device_gl(device);
948 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
950 device->shader_backend->shader_destroy(shader);
953 context = context_acquire(device, NULL, 0);
954 context_gl = wined3d_context_gl(context);
955 device->blitter->ops->blitter_destroy(device->blitter, context);
956 device->shader_backend->shader_free_private(device, context);
957 wined3d_device_gl_destroy_dummy_textures(device_gl, context_gl);
958 wined3d_device_destroy_default_samplers(device, context);
959 context_release(context);
961 while (device->context_count)
963 if ((swapchain_gl = wined3d_swapchain_gl(device->contexts[0]->swapchain)))
964 wined3d_swapchain_gl_destroy_contexts(swapchain_gl);
965 else
966 wined3d_context_gl_destroy(wined3d_context_gl(device->contexts[0]));
970 void wined3d_device_create_primary_opengl_context_cs(void *object)
972 struct wined3d_device *device = object;
973 struct wined3d_context_gl *context_gl;
974 struct wined3d_swapchain *swapchain;
975 struct wined3d_context *context;
976 struct wined3d_texture *target;
977 HRESULT hr;
979 TRACE("device %p.\n", device);
981 swapchain = device->swapchains[0];
982 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
983 if (!(context = context_acquire(device, target, 0)))
985 WARN("Failed to acquire context.\n");
986 return;
989 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
990 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
992 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
993 context_release(context);
994 return;
997 if (!(device->blitter = wined3d_cpu_blitter_create()))
999 ERR("Failed to create CPU blitter.\n");
1000 device->shader_backend->shader_free_private(device, NULL);
1001 context_release(context);
1002 return;
1005 context_gl = wined3d_context_gl(context);
1007 wined3d_ffp_blitter_create(&device->blitter, context_gl->gl_info);
1008 if (!wined3d_glsl_blitter_create(&device->blitter, device))
1009 wined3d_arbfp_blitter_create(&device->blitter, device);
1010 wined3d_fbo_blitter_create(&device->blitter, context_gl->gl_info);
1011 wined3d_raw_blitter_create(&device->blitter, context_gl->gl_info);
1013 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device), context_gl);
1014 wined3d_device_create_default_samplers(device, context);
1015 context_release(context);
1018 HRESULT wined3d_device_set_implicit_swapchain(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
1020 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1021 const struct wined3d_swapchain_desc *swapchain_desc;
1022 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
1023 DWORD clear_flags = 0;
1024 unsigned int i;
1025 HRESULT hr;
1027 TRACE("device %p, swapchain %p.\n", device, swapchain);
1029 if (device->d3d_initialized)
1030 return WINED3DERR_INVALIDCALL;
1032 device->swapchain_count = 1;
1033 if (!(device->swapchains = heap_calloc(device->swapchain_count, sizeof(*device->swapchains))))
1035 ERR("Failed to allocate swapchain array.\n");
1036 hr = E_OUTOFMEMORY;
1037 goto err_out;
1039 device->swapchains[0] = swapchain;
1041 for (i = 0; i < ARRAY_SIZE(fb->render_targets); ++i)
1043 if (fb->render_targets[i])
1044 wined3d_rtv_bind_count_dec(fb->render_targets[i]);
1046 memset(fb->render_targets, 0, sizeof(fb->render_targets));
1048 if (FAILED(hr = device->adapter->adapter_ops->adapter_init_3d(device)))
1049 goto err_out;
1050 device->d3d_initialized = TRUE;
1052 swapchain_desc = &swapchain->state.desc;
1053 if (swapchain_desc->backbuffer_count && swapchain_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
1055 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
1056 struct wined3d_view_desc view_desc;
1058 view_desc.format_id = back_buffer->format->id;
1059 view_desc.flags = 0;
1060 view_desc.u.texture.level_idx = 0;
1061 view_desc.u.texture.level_count = 1;
1062 view_desc.u.texture.layer_idx = 0;
1063 view_desc.u.texture.layer_count = 1;
1064 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
1065 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
1067 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
1068 device->adapter->adapter_ops->adapter_uninit_3d(device);
1069 device->d3d_initialized = FALSE;
1070 goto err_out;
1074 device_init_swapchain_state(device, swapchain);
1076 TRACE("All defaults now set up.\n");
1078 /* Clear the screen. */
1079 if (device->back_buffer_view)
1080 clear_flags |= WINED3DCLEAR_TARGET;
1081 if (swapchain_desc->enable_auto_depth_stencil)
1082 clear_flags |= WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL;
1083 if (clear_flags)
1084 wined3d_device_clear(device, 0, NULL, clear_flags, &black, 1.0f, 0);
1086 if (wined3d_settings.logo)
1087 device_load_logo(device, wined3d_settings.logo);
1089 return WINED3D_OK;
1091 err_out:
1092 heap_free(device->swapchains);
1093 device->swapchains = NULL;
1094 device->swapchain_count = 0;
1096 return hr;
1099 static void device_free_sampler(struct wine_rb_entry *entry, void *context)
1101 struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
1103 wined3d_sampler_decref(sampler);
1106 static void device_free_rasterizer_state(struct wine_rb_entry *entry, void *context)
1108 struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
1110 wined3d_rasterizer_state_decref(state);
1113 static void device_free_blend_state(struct wine_rb_entry *entry, void *context)
1115 struct wined3d_blend_state *blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
1117 wined3d_blend_state_decref(blend_state);
1120 static void device_free_depth_stencil_state(struct wine_rb_entry *entry, void *context)
1122 struct wined3d_depth_stencil_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
1124 wined3d_depth_stencil_state_decref(state);
1127 void wined3d_device_uninit_3d(struct wined3d_device *device)
1129 struct wined3d_state *state = device->cs->c.state;
1130 struct wined3d_resource *resource, *cursor;
1131 struct wined3d_rendertarget_view *view;
1132 struct wined3d_texture *texture;
1134 TRACE("device %p.\n", device);
1136 if (!device->d3d_initialized)
1138 ERR("Called while 3D support was not initialised.\n");
1139 return;
1142 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1144 device->swapchain_count = 0;
1146 if ((texture = device->logo_texture))
1148 device->logo_texture = NULL;
1149 wined3d_texture_decref(texture);
1152 if ((texture = device->cursor_texture))
1154 device->cursor_texture = NULL;
1155 wined3d_texture_decref(texture);
1158 wined3d_cs_emit_reset_state(device->cs);
1159 state_cleanup(state);
1161 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
1162 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
1163 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
1164 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
1166 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1168 TRACE("Unloading resource %p.\n", resource);
1169 wined3d_cs_emit_unload_resource(device->cs, resource);
1172 device->adapter->adapter_ops->adapter_uninit_3d(device);
1173 device->d3d_initialized = FALSE;
1175 if ((view = device->auto_depth_stencil_view))
1177 device->auto_depth_stencil_view = NULL;
1178 if (wined3d_rendertarget_view_decref(view))
1179 ERR("Something's still holding the auto depth/stencil view (%p).\n", view);
1182 if ((view = device->back_buffer_view))
1184 device->back_buffer_view = NULL;
1185 wined3d_rendertarget_view_decref(view);
1188 heap_free(device->swapchains);
1189 device->swapchains = NULL;
1191 wined3d_state_reset(state, &device->adapter->d3d_info);
1194 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1195 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1196 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1198 * There is no way to deactivate thread safety once it is enabled.
1200 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1202 TRACE("device %p.\n", device);
1204 /* For now just store the flag (needed in case of ddraw). */
1205 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1208 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1210 const struct wined3d_driver_info *driver_info;
1212 TRACE("device %p.\n", device);
1214 driver_info = &device->adapter->driver_info;
1216 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1217 wine_dbgstr_longlong(driver_info->vram_bytes),
1218 wine_dbgstr_longlong(device->adapter->vram_bytes_used),
1219 wine_dbgstr_longlong(driver_info->vram_bytes - device->adapter->vram_bytes_used));
1221 return min(UINT_MAX, driver_info->vram_bytes - device->adapter->vram_bytes_used);
1224 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1225 struct wined3d_buffer *buffer, UINT offset)
1227 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1229 wined3d_device_context_set_stream_output(&device->cs->c, idx, buffer, offset);
1232 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1233 UINT idx, UINT *offset)
1235 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1237 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
1239 WARN("Invalid stream output %u.\n", idx);
1240 return NULL;
1243 if (offset)
1244 *offset = device->cs->c.state->stream_output[idx].offset;
1245 return device->cs->c.state->stream_output[idx].buffer;
1248 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1249 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1251 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1252 device, stream_idx, buffer, offset, stride);
1254 return wined3d_device_context_set_stream_source(&device->cs->c, stream_idx, buffer, offset, stride);
1257 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1258 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1260 const struct wined3d_stream_state *stream;
1262 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1263 device, stream_idx, buffer, offset, stride);
1265 if (stream_idx >= WINED3D_MAX_STREAMS)
1267 WARN("Stream index %u out of range.\n", stream_idx);
1268 return WINED3DERR_INVALIDCALL;
1271 stream = &device->cs->c.state->streams[stream_idx];
1272 *buffer = stream->buffer;
1273 if (offset)
1274 *offset = stream->offset;
1275 *stride = stream->stride;
1277 return WINED3D_OK;
1280 static void wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1282 struct wined3d_stream_state *stream;
1283 UINT old_flags, old_freq;
1285 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1287 stream = &device->cs->c.state->streams[stream_idx];
1288 old_flags = stream->flags;
1289 old_freq = stream->frequency;
1291 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1292 stream->frequency = divider & 0x7fffff;
1293 if (stream->frequency != old_freq || stream->flags != old_flags)
1294 wined3d_cs_emit_set_stream_source_freq(device->cs, stream_idx, stream->frequency, stream->flags);
1297 static void wined3d_device_set_transform(struct wined3d_device *device,
1298 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1300 TRACE("device %p, state %s, matrix %p.\n",
1301 device, debug_d3dtstype(state), matrix);
1302 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_11, matrix->_12, matrix->_13, matrix->_14);
1303 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_21, matrix->_22, matrix->_23, matrix->_24);
1304 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_31, matrix->_32, matrix->_33, matrix->_34);
1305 TRACE("%.8e %.8e %.8e %.8e\n", matrix->_41, matrix->_42, matrix->_43, matrix->_44);
1307 /* If the new matrix is the same as the current one,
1308 * we cut off any further processing. this seems to be a reasonable
1309 * optimization because as was noticed, some apps (warcraft3 for example)
1310 * tend towards setting the same matrix repeatedly for some reason.
1312 * From here on we assume that the new matrix is different, wherever it matters. */
1313 if (!memcmp(&device->cs->c.state->transforms[state], matrix, sizeof(*matrix)))
1315 TRACE("The application is setting the same matrix over again.\n");
1316 return;
1319 device->cs->c.state->transforms[state] = *matrix;
1320 wined3d_cs_emit_set_transform(device->cs, state, matrix);
1323 static void wined3d_device_get_transform(const struct wined3d_device *device,
1324 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1326 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1328 *matrix = device->cs->c.state->transforms[state];
1331 /* Note lights are real special cases. Although the device caps state only
1332 * e.g. 8 are supported, you can reference any indexes you want as long as
1333 * that number max are enabled at any one point in time. Therefore since the
1334 * indices can be anything, we need a hashmap of them. However, this causes
1335 * stateblock problems. When capturing the state block, I duplicate the
1336 * hashmap, but when recording, just build a chain pretty much of commands to
1337 * be replayed. */
1338 static void wined3d_device_set_light(struct wined3d_device *device,
1339 UINT light_idx, const struct wined3d_light *light)
1341 struct wined3d_light_info *object = NULL;
1342 float rho;
1344 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1346 if (FAILED(wined3d_light_state_set_light(&device->cs->c.state->light_state, light_idx, light, &object)))
1347 return;
1349 /* Initialize the object. */
1350 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1351 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1352 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1353 light_idx, light->type, debug_color(&light->diffuse),
1354 debug_color(&light->specular), debug_color(&light->ambient),
1355 light->position.x, light->position.y, light->position.z,
1356 light->direction.x, light->direction.y, light->direction.z,
1357 light->range, light->falloff, light->theta, light->phi);
1359 switch (light->type)
1361 case WINED3D_LIGHT_POINT:
1362 /* Position */
1363 object->position.x = light->position.x;
1364 object->position.y = light->position.y;
1365 object->position.z = light->position.z;
1366 object->position.w = 1.0f;
1367 object->cutoff = 180.0f;
1368 /* FIXME: Range */
1369 break;
1371 case WINED3D_LIGHT_DIRECTIONAL:
1372 /* Direction */
1373 object->direction.x = -light->direction.x;
1374 object->direction.y = -light->direction.y;
1375 object->direction.z = -light->direction.z;
1376 object->direction.w = 0.0f;
1377 object->exponent = 0.0f;
1378 object->cutoff = 180.0f;
1379 break;
1381 case WINED3D_LIGHT_SPOT:
1382 /* Position */
1383 object->position.x = light->position.x;
1384 object->position.y = light->position.y;
1385 object->position.z = light->position.z;
1386 object->position.w = 1.0f;
1388 /* Direction */
1389 object->direction.x = light->direction.x;
1390 object->direction.y = light->direction.y;
1391 object->direction.z = light->direction.z;
1392 object->direction.w = 0.0f;
1394 /* opengl-ish and d3d-ish spot lights use too different models
1395 * for the light "intensity" as a function of the angle towards
1396 * the main light direction, so we only can approximate very
1397 * roughly. However, spot lights are rather rarely used in games
1398 * (if ever used at all). Furthermore if still used, probably
1399 * nobody pays attention to such details. */
1400 if (!light->falloff)
1402 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1403 * equations have the falloff resp. exponent parameter as an
1404 * exponent, so the spot light lighting will always be 1.0 for
1405 * both of them, and we don't have to care for the rest of the
1406 * rather complex calculation. */
1407 object->exponent = 0.0f;
1409 else
1411 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1412 if (rho < 0.0001f)
1413 rho = 0.0001f;
1414 object->exponent = -0.3f / logf(cosf(rho / 2));
1417 if (object->exponent > 128.0f)
1418 object->exponent = 128.0f;
1420 object->cutoff = (float)(light->phi * 90 / M_PI);
1421 /* FIXME: Range */
1422 break;
1424 case WINED3D_LIGHT_PARALLELPOINT:
1425 object->position.x = light->position.x;
1426 object->position.y = light->position.y;
1427 object->position.z = light->position.z;
1428 object->position.w = 1.0f;
1429 break;
1431 default:
1432 FIXME("Unrecognized light type %#x.\n", light->type);
1435 wined3d_cs_emit_set_light(device->cs, object);
1438 static void wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1440 struct wined3d_light_state *light_state = &device->cs->c.state->light_state;
1441 struct wined3d_light_info *light_info;
1443 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1445 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1446 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1448 TRACE("Light enabled requested but light not defined, so defining one!\n");
1449 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1451 if (!(light_info = wined3d_light_state_get_light(light_state, light_idx)))
1453 FIXME("Adding default lights has failed dismally\n");
1454 return;
1458 wined3d_light_state_enable_light(light_state, &device->adapter->d3d_info, light_info, enable);
1459 wined3d_cs_emit_set_light_enable(device->cs, light_idx, enable);
1462 static HRESULT wined3d_device_set_clip_plane(struct wined3d_device *device,
1463 UINT plane_idx, const struct wined3d_vec4 *plane)
1465 struct wined3d_vec4 *clip_planes = device->cs->c.state->clip_planes;
1467 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
1469 if (plane_idx >= device->adapter->d3d_info.limits.max_clip_distances)
1471 TRACE("Application has requested clipplane this device doesn't support.\n");
1472 return WINED3DERR_INVALIDCALL;
1475 if (!memcmp(&clip_planes[plane_idx], plane, sizeof(*plane)))
1477 TRACE("Application is setting old values over, nothing to do.\n");
1478 return WINED3D_OK;
1481 clip_planes[plane_idx] = *plane;
1483 wined3d_cs_emit_set_clip_plane(device->cs, plane_idx, plane);
1485 return WINED3D_OK;
1488 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
1489 const struct wined3d_clip_status *clip_status)
1491 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1493 if (!clip_status)
1494 return WINED3DERR_INVALIDCALL;
1496 return WINED3D_OK;
1499 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
1500 struct wined3d_clip_status *clip_status)
1502 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
1504 if (!clip_status)
1505 return WINED3DERR_INVALIDCALL;
1507 return WINED3D_OK;
1510 static void wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
1512 TRACE("device %p, material %p.\n", device, material);
1514 device->cs->c.state->material = *material;
1515 wined3d_cs_emit_set_material(device->cs, material);
1518 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
1519 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
1521 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1522 device, buffer, debug_d3dformat(format_id), offset);
1524 wined3d_device_context_set_index_buffer(&device->cs->c, buffer, format_id, offset);
1527 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
1528 enum wined3d_format_id *format, unsigned int *offset)
1530 const struct wined3d_state *state = device->cs->c.state;
1532 TRACE("device %p, format %p, offset %p.\n", device, format, offset);
1534 *format = state->index_format;
1535 if (offset)
1536 *offset = state->index_offset;
1537 return state->index_buffer;
1540 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
1542 TRACE("device %p, base_index %d.\n", device, base_index);
1544 device->cs->c.state->base_vertex_index = base_index;
1547 void CDECL wined3d_device_set_viewports(struct wined3d_device *device, unsigned int viewport_count,
1548 const struct wined3d_viewport *viewports)
1550 unsigned int i;
1552 TRACE("device %p, viewport_count %u, viewports %p.\n", device, viewport_count, viewports);
1554 for (i = 0; i < viewport_count; ++i)
1556 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
1557 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
1560 wined3d_device_context_set_viewports(&device->cs->c, viewport_count, viewports);
1563 void CDECL wined3d_device_get_viewports(const struct wined3d_device *device, unsigned int *viewport_count,
1564 struct wined3d_viewport *viewports)
1566 const struct wined3d_state *state = device->cs->c.state;
1567 unsigned int count;
1569 TRACE("device %p, viewport_count %p, viewports %p.\n", device, viewport_count, viewports);
1571 count = viewport_count ? min(*viewport_count, state->viewport_count) : 1;
1572 if (count && viewports)
1573 memcpy(viewports, state->viewports, count * sizeof(*viewports));
1574 if (viewport_count)
1575 *viewport_count = state->viewport_count;
1578 static void resolve_depth_buffer(struct wined3d_device *device)
1580 const struct wined3d_state *state = device->cs->c.state;
1581 struct wined3d_rendertarget_view *src_view;
1582 struct wined3d_resource *dst_resource;
1583 struct wined3d_texture *dst_texture;
1585 if (!(dst_texture = state->textures[0]))
1586 return;
1587 dst_resource = &dst_texture->resource;
1588 if (!dst_resource->format->depth_size)
1589 return;
1590 if (!(src_view = state->fb.depth_stencil))
1591 return;
1593 wined3d_device_context_resolve_sub_resource(&device->cs->c, dst_resource, 0,
1594 src_view->resource, src_view->sub_resource_idx, dst_resource->format->id);
1597 void CDECL wined3d_device_set_blend_state(struct wined3d_device *device,
1598 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1600 TRACE("device %p, blend_state %p, blend_factor %s, sample_mask %#x.\n",
1601 device, blend_state, debug_color(blend_factor), sample_mask);
1603 wined3d_device_context_set_blend_state(&device->cs->c, blend_state, blend_factor, sample_mask);
1606 struct wined3d_blend_state * CDECL wined3d_device_get_blend_state(const struct wined3d_device *device,
1607 struct wined3d_color *blend_factor, unsigned int *sample_mask)
1609 const struct wined3d_state *state = device->cs->c.state;
1611 TRACE("device %p, blend_factor %p, sample_mask %p.\n", device, blend_factor, sample_mask);
1613 *blend_factor = state->blend_factor;
1614 *sample_mask = state->sample_mask;
1615 return state->blend_state;
1618 void CDECL wined3d_device_set_depth_stencil_state(struct wined3d_device *device,
1619 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1621 TRACE("device %p, depth_stencil_state %p, stencil_ref %u.\n", device, depth_stencil_state, stencil_ref);
1623 wined3d_device_context_set_depth_stencil_state(&device->cs->c, depth_stencil_state, stencil_ref);
1626 struct wined3d_depth_stencil_state * CDECL wined3d_device_get_depth_stencil_state(const struct wined3d_device *device,
1627 unsigned int *stencil_ref)
1629 const struct wined3d_state *state = device->cs->c.state;
1631 TRACE("device %p, stencil_ref %p.\n", device, stencil_ref);
1633 *stencil_ref = state->stencil_ref;
1634 return state->depth_stencil_state;
1637 void CDECL wined3d_device_set_rasterizer_state(struct wined3d_device *device,
1638 struct wined3d_rasterizer_state *rasterizer_state)
1640 TRACE("device %p, rasterizer_state %p.\n", device, rasterizer_state);
1642 wined3d_device_context_set_rasterizer_state(&device->cs->c, rasterizer_state);
1645 struct wined3d_rasterizer_state * CDECL wined3d_device_get_rasterizer_state(struct wined3d_device *device)
1647 TRACE("device %p.\n", device);
1649 return device->cs->c.state->rasterizer_state;
1652 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
1653 enum wined3d_render_state state, DWORD value)
1655 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
1657 if (state > WINEHIGHEST_RENDER_STATE)
1659 WARN("Unhandled render state %#x.\n", state);
1660 return;
1663 if (value == device->cs->c.state->render_states[state])
1664 TRACE("Application is setting the old value over, nothing to do.\n");
1665 else
1667 device->cs->c.state->render_states[state] = value;
1668 wined3d_cs_emit_set_render_state(device->cs, state, value);
1671 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
1673 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1674 resolve_depth_buffer(device);
1678 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
1680 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
1682 return device->cs->c.state->render_states[state];
1685 static void wined3d_device_set_sampler_state(struct wined3d_device *device,
1686 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
1688 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1689 device, sampler_idx, debug_d3dsamplerstate(state), value);
1691 if (value == device->cs->c.state->sampler_states[sampler_idx][state])
1693 TRACE("Application is setting the old value over, nothing to do.\n");
1694 return;
1697 device->cs->c.state->sampler_states[sampler_idx][state] = value;
1698 wined3d_cs_emit_set_sampler_state(device->cs, sampler_idx, state, value);
1701 void CDECL wined3d_device_set_scissor_rects(struct wined3d_device *device, unsigned int rect_count,
1702 const RECT *rects)
1704 unsigned int i;
1706 TRACE("device %p, rect_count %u, rects %p.\n", device, rect_count, rects);
1708 for (i = 0; i < rect_count; ++i)
1710 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
1713 wined3d_device_context_set_scissor_rects(&device->cs->c, rect_count, rects);
1716 void CDECL wined3d_device_get_scissor_rects(const struct wined3d_device *device, unsigned int *rect_count, RECT *rects)
1718 const struct wined3d_state *state = device->cs->c.state;
1719 unsigned int count;
1721 TRACE("device %p, rect_count %p, rects %p.\n", device, rect_count, rects);
1723 count = rect_count ? min(*rect_count, state->scissor_rect_count) : 1;
1724 if (count && rects)
1725 memcpy(rects, state->scissor_rects, count * sizeof(*rects));
1726 if (rect_count)
1727 *rect_count = state->scissor_rect_count;
1730 void CDECL wined3d_device_set_state(struct wined3d_device *device, struct wined3d_state *state)
1732 struct wined3d_device_context *context = &device->cs->c;
1733 const struct wined3d_light_info *light;
1734 unsigned int i, j;
1736 TRACE("device %p, state %p.\n", device, state);
1738 device->cs->c.state = state;
1739 wined3d_cs_emit_set_feature_level(device->cs, state->feature_level);
1741 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
1743 wined3d_device_context_emit_set_rendertarget_view(context, i, state->fb.render_targets[i]);
1746 wined3d_device_context_emit_set_depth_stencil_view(context, state->fb.depth_stencil);
1747 wined3d_device_context_emit_set_vertex_declaration(context, state->vertex_declaration);
1749 for (i = 0; i < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++i)
1751 wined3d_device_context_emit_set_stream_output(context, i,
1752 state->stream_output[i].buffer, state->stream_output[i].offset);
1755 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
1757 wined3d_device_context_emit_set_stream_source(context, i, state->streams[i].buffer,
1758 state->streams[i].offset, state->streams[i].stride);
1761 wined3d_device_context_emit_set_index_buffer(context, state->index_buffer,
1762 state->index_format, state->index_offset);
1764 wined3d_device_context_emit_set_predication(context, state->predicate, state->predicate_value);
1766 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
1768 wined3d_device_context_emit_set_shader(context, i, state->shader[i]);
1769 for (j = 0; j < MAX_CONSTANT_BUFFERS; ++j)
1770 wined3d_device_context_emit_set_constant_buffer(context, i, j, state->cb[i][j]);
1771 for (j = 0; j < MAX_SAMPLER_OBJECTS; ++j)
1773 wined3d_device_context_emit_set_sampler(context, i, j, state->sampler[i][j]);
1775 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
1777 wined3d_device_context_emit_set_shader_resource_view(context, i, j, state->shader_resource_view[i][j]);
1781 for (i = 0; i < WINED3D_PIPELINE_COUNT; ++i)
1783 for (j = 0; j < MAX_UNORDERED_ACCESS_VIEWS; ++j)
1785 wined3d_device_context_emit_set_unordered_access_view(context, i, j,
1786 state->unordered_access_view[i][j], ~0);
1790 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F,
1791 0, WINED3D_MAX_VS_CONSTS_F, state->vs_consts_f);
1792 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I,
1793 0, WINED3D_MAX_CONSTS_I, state->vs_consts_i);
1794 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B,
1795 0, WINED3D_MAX_CONSTS_B, state->vs_consts_b);
1797 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F,
1798 0, WINED3D_MAX_PS_CONSTS_F, state->ps_consts_f);
1799 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I,
1800 0, WINED3D_MAX_CONSTS_I, state->ps_consts_i);
1801 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B,
1802 0, WINED3D_MAX_CONSTS_B, state->ps_consts_b);
1804 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
1806 wined3d_cs_emit_set_texture(device->cs, i, state->textures[i]);
1807 for (j = 0; j < WINED3D_HIGHEST_SAMPLER_STATE + 1; ++j)
1809 wined3d_cs_emit_set_sampler_state(device->cs, i, j, state->sampler_states[i][j]);
1813 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1815 for (j = 0; j < WINED3D_HIGHEST_TEXTURE_STATE + 1; ++j)
1817 wined3d_cs_emit_set_texture_state(device->cs, i, j, state->texture_states[i][j]);
1821 for (i = 0; i < WINED3D_HIGHEST_TRANSFORM_STATE + 1; ++i)
1823 wined3d_cs_emit_set_transform(device->cs, i, state->transforms + i);
1826 for (i = 0; i < WINED3D_MAX_CLIP_DISTANCES; ++i)
1828 wined3d_cs_emit_set_clip_plane(device->cs, i, state->clip_planes + i);
1831 wined3d_cs_emit_set_material(device->cs, &state->material);
1833 wined3d_device_context_emit_set_viewports(context, state->viewport_count, state->viewports);
1834 wined3d_device_context_emit_set_scissor_rects(context, state->scissor_rect_count, state->scissor_rects);
1836 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1838 LIST_FOR_EACH_ENTRY(light, &state->light_state.light_map[i], struct wined3d_light_info, entry)
1840 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
1841 wined3d_cs_emit_set_light_enable(device->cs, light->OriginalIndex, light->glIndex != -1);
1845 for (i = 0; i < WINEHIGHEST_RENDER_STATE + 1; ++i)
1847 wined3d_cs_emit_set_render_state(device->cs, i, state->render_states[i]);
1850 wined3d_device_context_emit_set_blend_state(context, state->blend_state, &state->blend_factor, state->sample_mask);
1851 wined3d_device_context_emit_set_depth_stencil_state(context, state->depth_stencil_state, state->stencil_ref);
1852 wined3d_device_context_emit_set_rasterizer_state(context, state->rasterizer_state);
1855 struct wined3d_state * CDECL wined3d_device_get_state(struct wined3d_device *device)
1857 TRACE("device %p.\n", device);
1859 return device->cs->c.state;
1862 struct wined3d_device_context * CDECL wined3d_device_get_immediate_context(struct wined3d_device *device)
1864 TRACE("device %p.\n", device);
1866 return &device->cs->c;
1869 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
1870 struct wined3d_vertex_declaration *declaration)
1872 TRACE("device %p, declaration %p.\n", device, declaration);
1874 wined3d_device_context_set_vertex_declaration(&device->cs->c, declaration);
1877 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
1879 TRACE("device %p.\n", device);
1881 return device->cs->c.state->vertex_declaration;
1884 void CDECL wined3d_device_context_set_shader(struct wined3d_device_context *context,
1885 enum wined3d_shader_type type, struct wined3d_shader *shader)
1887 struct wined3d_state *state = context->state;
1888 struct wined3d_shader *prev;
1890 TRACE("context %p, type %#x, shader %p.\n", context, type, shader);
1892 prev = state->shader[type];
1893 if (shader == prev)
1894 return;
1896 if (shader)
1897 wined3d_shader_incref(shader);
1898 state->shader[type] = shader;
1899 wined3d_device_context_emit_set_shader(context, type, shader);
1900 if (prev)
1901 wined3d_shader_decref(prev);
1904 void CDECL wined3d_device_context_set_constant_buffer(struct wined3d_device_context *context,
1905 enum wined3d_shader_type type, unsigned int idx, struct wined3d_buffer *buffer)
1907 struct wined3d_state *state = context->state;
1908 struct wined3d_buffer *prev;
1910 TRACE("context %p, type %#x, idx %u, buffer %p.\n", context, type, idx, buffer);
1912 if (idx >= MAX_CONSTANT_BUFFERS)
1914 WARN("Invalid constant buffer index %u.\n", idx);
1915 return;
1918 prev = state->cb[type][idx];
1919 if (buffer == prev)
1920 return;
1922 if (buffer)
1923 wined3d_buffer_incref(buffer);
1924 state->cb[type][idx] = buffer;
1925 wined3d_device_context_emit_set_constant_buffer(context, type, idx, buffer);
1926 if (prev)
1927 wined3d_buffer_decref(prev);
1930 void CDECL wined3d_device_context_set_blend_state(struct wined3d_device_context *context,
1931 struct wined3d_blend_state *blend_state, const struct wined3d_color *blend_factor, unsigned int sample_mask)
1933 struct wined3d_state *state = context->state;
1934 struct wined3d_blend_state *prev;
1936 TRACE("context %p, blend_state %p, blend_factor %p, sample_mask %#x.\n",
1937 context, blend_state, blend_factor, sample_mask);
1939 prev = state->blend_state;
1940 if (prev == blend_state && !memcmp(blend_factor, &state->blend_factor, sizeof(*blend_factor))
1941 && sample_mask == state->sample_mask)
1942 return;
1944 if (blend_state)
1945 wined3d_blend_state_incref(blend_state);
1946 state->blend_state = blend_state;
1947 state->blend_factor = *blend_factor;
1948 state->sample_mask = sample_mask;
1949 wined3d_device_context_emit_set_blend_state(context, blend_state, blend_factor, sample_mask);
1950 if (prev)
1951 wined3d_blend_state_decref(prev);
1954 void CDECL wined3d_device_context_set_depth_stencil_state(struct wined3d_device_context *context,
1955 struct wined3d_depth_stencil_state *depth_stencil_state, unsigned int stencil_ref)
1957 struct wined3d_state *state = context->state;
1958 struct wined3d_depth_stencil_state *prev;
1960 TRACE("context %p, depth_stencil_state %p, stencil_ref %u.\n", context, depth_stencil_state, stencil_ref);
1962 prev = state->depth_stencil_state;
1963 if (prev == depth_stencil_state && state->stencil_ref == stencil_ref)
1964 return;
1966 if (depth_stencil_state)
1967 wined3d_depth_stencil_state_incref(depth_stencil_state);
1968 state->depth_stencil_state = depth_stencil_state;
1969 state->stencil_ref = stencil_ref;
1970 wined3d_device_context_emit_set_depth_stencil_state(context, depth_stencil_state, stencil_ref);
1971 if (prev)
1972 wined3d_depth_stencil_state_decref(prev);
1975 void CDECL wined3d_device_context_set_rasterizer_state(struct wined3d_device_context *context,
1976 struct wined3d_rasterizer_state *rasterizer_state)
1978 struct wined3d_state *state = context->state;
1979 struct wined3d_rasterizer_state *prev;
1981 TRACE("context %p, rasterizer_state %p.\n", context, rasterizer_state);
1983 prev = state->rasterizer_state;
1984 if (prev == rasterizer_state)
1985 return;
1987 if (rasterizer_state)
1988 wined3d_rasterizer_state_incref(rasterizer_state);
1989 state->rasterizer_state = rasterizer_state;
1990 wined3d_device_context_emit_set_rasterizer_state(context, rasterizer_state);
1991 if (prev)
1992 wined3d_rasterizer_state_decref(prev);
1995 void CDECL wined3d_device_context_set_viewports(struct wined3d_device_context *context, unsigned int viewport_count,
1996 const struct wined3d_viewport *viewports)
1998 struct wined3d_state *state = context->state;
1999 unsigned int i;
2001 TRACE("context %p, viewport_count %u, viewports %p.\n", context, viewport_count, viewports);
2003 for (i = 0; i < viewport_count; ++i)
2005 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i, viewports[i].x, viewports[i].y,
2006 viewports[i].width, viewports[i].height, viewports[i].min_z, viewports[i].max_z);
2009 if (viewport_count)
2010 memcpy(state->viewports, viewports, viewport_count * sizeof(*viewports));
2011 else
2012 memset(state->viewports, 0, sizeof(state->viewports));
2013 state->viewport_count = viewport_count;
2015 wined3d_device_context_emit_set_viewports(context, viewport_count, viewports);
2018 void CDECL wined3d_device_context_set_scissor_rects(struct wined3d_device_context *context, unsigned int rect_count,
2019 const RECT *rects)
2021 struct wined3d_state *state = context->state;
2022 unsigned int i;
2024 TRACE("context %p, rect_count %u, rects %p.\n", context, rect_count, rects);
2026 for (i = 0; i < rect_count; ++i)
2028 TRACE("%u: %s\n", i, wine_dbgstr_rect(&rects[i]));
2031 if (state->scissor_rect_count == rect_count
2032 && !memcmp(state->scissor_rects, rects, rect_count * sizeof(*rects)))
2034 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
2035 return;
2038 if (rect_count)
2039 memcpy(state->scissor_rects, rects, rect_count * sizeof(*rects));
2040 else
2041 memset(state->scissor_rects, 0, sizeof(state->scissor_rects));
2042 state->scissor_rect_count = rect_count;
2044 wined3d_device_context_emit_set_scissor_rects(context, rect_count, rects);
2047 void CDECL wined3d_device_context_set_shader_resource_view(struct wined3d_device_context *context,
2048 enum wined3d_shader_type type, unsigned int idx, struct wined3d_shader_resource_view *view)
2050 struct wined3d_state *state = context->state;
2051 const struct wined3d_rendertarget_view *dsv;
2052 struct wined3d_shader_resource_view *prev;
2054 TRACE("context %p, type %#x, idx %u, view %p.\n", context, type, idx, view);
2056 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2058 WARN("Invalid view index %u.\n", idx);
2059 return;
2062 prev = state->shader_resource_view[type][idx];
2063 if (view == prev)
2064 return;
2066 if (view && (wined3d_is_srv_rtv_bound(view)
2067 || ((dsv = state->fb.depth_stencil)
2068 && dsv->resource == view->resource && wined3d_dsv_srv_conflict(dsv, view->format))))
2070 WARN("Application is trying to bind resource which is attached as render target.\n");
2071 view = NULL;
2074 if (view)
2076 wined3d_shader_resource_view_incref(view);
2077 wined3d_srv_bind_count_inc(view);
2080 state->shader_resource_view[type][idx] = view;
2081 wined3d_device_context_emit_set_shader_resource_view(context, type, idx, view);
2082 if (prev)
2084 wined3d_srv_bind_count_dec(prev);
2085 wined3d_shader_resource_view_decref(prev);
2089 void CDECL wined3d_device_context_set_sampler(struct wined3d_device_context *context,
2090 enum wined3d_shader_type type, unsigned int idx, struct wined3d_sampler *sampler)
2092 struct wined3d_state *state = context->state;
2093 struct wined3d_sampler *prev;
2095 TRACE("context %p, type %#x, idx %u, sampler %p.\n", context, type, idx, sampler);
2097 if (idx >= MAX_SAMPLER_OBJECTS)
2099 WARN("Invalid sampler index %u.\n", idx);
2100 return;
2103 prev = state->sampler[type][idx];
2104 if (sampler == prev)
2105 return;
2107 if (sampler)
2108 wined3d_sampler_incref(sampler);
2109 state->sampler[type][idx] = sampler;
2110 wined3d_device_context_emit_set_sampler(context, type, idx, sampler);
2111 if (prev)
2112 wined3d_sampler_decref(prev);
2115 void CDECL wined3d_device_context_set_unordered_access_view(struct wined3d_device_context *context,
2116 enum wined3d_pipeline pipeline, unsigned int idx, struct wined3d_unordered_access_view *uav,
2117 unsigned int initial_count)
2119 struct wined3d_state *state = context->state;
2120 struct wined3d_unordered_access_view *prev;
2122 TRACE("context %p, pipeline %#x, idx %u, uav %p, initial_count %u.\n", context, pipeline, idx, uav, initial_count);
2124 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2126 WARN("Invalid UAV index %u.\n", idx);
2127 return;
2130 prev = state->unordered_access_view[pipeline][idx];
2131 if (uav == prev && initial_count == ~0u)
2132 return;
2134 if (uav)
2135 wined3d_unordered_access_view_incref(uav);
2136 state->unordered_access_view[pipeline][idx] = uav;
2137 wined3d_device_context_emit_set_unordered_access_view(context, pipeline, idx, uav, initial_count);
2138 if (prev)
2139 wined3d_unordered_access_view_decref(prev);
2142 static void wined3d_device_context_unbind_srv_for_rtv(struct wined3d_device_context *context,
2143 const struct wined3d_rendertarget_view *view, BOOL dsv)
2145 struct wined3d_state *state = context->state;
2147 if (view && wined3d_is_rtv_srv_bound(view))
2149 const struct wined3d_resource *resource = view->resource;
2150 const struct wined3d_shader_resource_view *srv;
2151 unsigned int i, j;
2153 WARN("Application sets bound resource as render target.\n");
2155 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
2156 for (j = 0; j < MAX_SHADER_RESOURCE_VIEWS; ++j)
2157 if ((srv = state->shader_resource_view[i][j]) && srv->resource == resource
2158 && ((!dsv && wined3d_is_srv_rtv_bound(srv))
2159 || (dsv && wined3d_dsv_srv_conflict(view, srv->format))))
2160 wined3d_device_context_set_shader_resource_view(context, i, j, NULL);
2164 HRESULT CDECL wined3d_device_context_set_rendertarget_view(struct wined3d_device_context *context,
2165 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
2167 struct wined3d_state *state = context->state;
2168 struct wined3d_rendertarget_view *prev;
2169 unsigned int max_rt_count;
2171 TRACE("context %p, view_idx %u, view %p, set_viewport %#x.\n",
2172 context, view_idx, view, set_viewport);
2174 max_rt_count = context->device->adapter->d3d_info.limits.max_rt_count;
2175 if (view_idx >= max_rt_count)
2177 WARN("Only %u render targets are supported.\n", max_rt_count);
2178 return WINED3DERR_INVALIDCALL;
2181 if (view && !(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
2183 WARN("View resource %p doesn't have render target bind flags.\n", view->resource);
2184 return WINED3DERR_INVALIDCALL;
2187 /* Set the viewport and scissor rectangles, if requested. Tests show that
2188 * stateblock recording is ignored, the change goes directly into the
2189 * primary stateblock. */
2190 if (!view_idx && set_viewport)
2192 state->viewports[0].x = 0;
2193 state->viewports[0].y = 0;
2194 state->viewports[0].width = view->width;
2195 state->viewports[0].height = view->height;
2196 state->viewports[0].min_z = 0.0f;
2197 state->viewports[0].max_z = 1.0f;
2198 state->viewport_count = 1;
2199 wined3d_device_context_emit_set_viewports(context, 1, state->viewports);
2201 SetRect(&state->scissor_rects[0], 0, 0, view->width, view->height);
2202 state->scissor_rect_count = 1;
2203 wined3d_device_context_emit_set_scissor_rects(context, 1, state->scissor_rects);
2206 prev = state->fb.render_targets[view_idx];
2207 if (view == prev)
2208 return WINED3D_OK;
2210 if (view)
2212 wined3d_rendertarget_view_incref(view);
2213 wined3d_rtv_bind_count_inc(view);
2215 state->fb.render_targets[view_idx] = view;
2216 wined3d_device_context_emit_set_rendertarget_view(context, view_idx, view);
2217 /* Release after the assignment, to prevent device_resource_released()
2218 * from seeing the surface as still in use. */
2219 if (prev)
2221 wined3d_rtv_bind_count_dec(prev);
2222 wined3d_rendertarget_view_decref(prev);
2225 wined3d_device_context_unbind_srv_for_rtv(context, view, FALSE);
2227 return WINED3D_OK;
2230 HRESULT CDECL wined3d_device_context_set_depth_stencil_view(struct wined3d_device_context *context,
2231 struct wined3d_rendertarget_view *view)
2233 struct wined3d_fb_state *fb = &context->state->fb;
2234 struct wined3d_rendertarget_view *prev;
2236 TRACE("context %p, view %p.\n", context, view);
2238 if (view && !(view->resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
2240 WARN("View resource %p has incompatible %s bind flags.\n",
2241 view->resource, wined3d_debug_bind_flags(view->resource->bind_flags));
2242 return WINED3DERR_INVALIDCALL;
2245 prev = fb->depth_stencil;
2246 if (prev == view)
2248 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
2249 return WINED3D_OK;
2252 if ((fb->depth_stencil = view))
2253 wined3d_rendertarget_view_incref(view);
2254 wined3d_device_context_emit_set_depth_stencil_view(context, view);
2255 if (prev)
2256 wined3d_rendertarget_view_decref(prev);
2257 wined3d_device_context_unbind_srv_for_rtv(context, view, TRUE);
2259 return WINED3D_OK;
2262 void CDECL wined3d_device_context_set_predication(struct wined3d_device_context *context,
2263 struct wined3d_query *predicate, BOOL value)
2265 struct wined3d_state *state = context->state;
2266 struct wined3d_query *prev;
2268 TRACE("context %p, predicate %p, value %#x.\n", context, predicate, value);
2270 prev = state->predicate;
2271 if (predicate)
2273 FIXME("Predicated rendering not implemented.\n");
2274 wined3d_query_incref(predicate);
2276 state->predicate = predicate;
2277 state->predicate_value = value;
2278 wined3d_device_context_emit_set_predication(context, predicate, value);
2279 if (prev)
2280 wined3d_query_decref(prev);
2283 HRESULT CDECL wined3d_device_context_set_stream_source(struct wined3d_device_context *context,
2284 unsigned int stream_idx, struct wined3d_buffer *buffer, unsigned int offset, unsigned int stride)
2286 struct wined3d_stream_state *stream;
2287 struct wined3d_buffer *prev_buffer;
2289 TRACE("context %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2290 context, stream_idx, buffer, offset, stride);
2292 if (stream_idx >= WINED3D_MAX_STREAMS)
2294 WARN("Stream index %u out of range.\n", stream_idx);
2295 return WINED3DERR_INVALIDCALL;
2297 else if (offset & 0x3)
2299 WARN("Offset %u is not 4 byte aligned.\n", offset);
2300 return WINED3DERR_INVALIDCALL;
2303 stream = &context->state->streams[stream_idx];
2304 prev_buffer = stream->buffer;
2306 if (prev_buffer == buffer
2307 && stream->stride == stride
2308 && stream->offset == offset)
2310 TRACE("Application is setting the old values over, nothing to do.\n");
2311 return WINED3D_OK;
2314 stream->buffer = buffer;
2315 stream->stride = stride;
2316 stream->offset = offset;
2317 if (buffer)
2318 wined3d_buffer_incref(buffer);
2319 wined3d_device_context_emit_set_stream_source(context, stream_idx, buffer, offset, stride);
2320 if (prev_buffer)
2321 wined3d_buffer_decref(prev_buffer);
2323 return WINED3D_OK;
2326 void CDECL wined3d_device_context_set_index_buffer(struct wined3d_device_context *context,
2327 struct wined3d_buffer *buffer, enum wined3d_format_id format_id, unsigned int offset)
2329 struct wined3d_state *state = context->state;
2330 enum wined3d_format_id prev_format;
2331 struct wined3d_buffer *prev_buffer;
2332 unsigned int prev_offset;
2334 TRACE("context %p, buffer %p, format %s, offset %u.\n",
2335 context, buffer, debug_d3dformat(format_id), offset);
2337 prev_buffer = state->index_buffer;
2338 prev_format = state->index_format;
2339 prev_offset = state->index_offset;
2341 if (prev_buffer == buffer && prev_format == format_id && prev_offset == offset)
2342 return;
2344 if (buffer)
2345 wined3d_buffer_incref(buffer);
2346 state->index_buffer = buffer;
2347 state->index_format = format_id;
2348 state->index_offset = offset;
2349 wined3d_device_context_emit_set_index_buffer(context, buffer, format_id, offset);
2350 if (prev_buffer)
2351 wined3d_buffer_decref(prev_buffer);
2354 void CDECL wined3d_device_context_set_vertex_declaration(struct wined3d_device_context *context,
2355 struct wined3d_vertex_declaration *declaration)
2357 struct wined3d_state *state = context->state;
2358 struct wined3d_vertex_declaration *prev;
2360 TRACE("context %p, declaration %p.\n", context, declaration);
2362 prev = state->vertex_declaration;
2363 if (declaration == prev)
2364 return;
2366 if (declaration)
2367 wined3d_vertex_declaration_incref(declaration);
2368 state->vertex_declaration = declaration;
2369 wined3d_device_context_emit_set_vertex_declaration(context, declaration);
2370 if (prev)
2371 wined3d_vertex_declaration_decref(prev);
2374 void CDECL wined3d_device_context_set_stream_output(struct wined3d_device_context *context, unsigned int idx,
2375 struct wined3d_buffer *buffer, unsigned int offset)
2377 struct wined3d_stream_output *stream;
2378 struct wined3d_buffer *prev_buffer;
2380 TRACE("context %p, idx %u, buffer %p, offset %u.\n", context, idx, buffer, offset);
2382 if (idx >= WINED3D_MAX_STREAM_OUTPUT_BUFFERS)
2384 WARN("Invalid stream output %u.\n", idx);
2385 return;
2388 stream = &context->state->stream_output[idx];
2389 prev_buffer = stream->buffer;
2391 if (buffer)
2392 wined3d_buffer_incref(buffer);
2393 stream->buffer = buffer;
2394 stream->offset = offset;
2395 wined3d_device_context_emit_set_stream_output(context, idx, buffer, offset);
2396 if (prev_buffer)
2397 wined3d_buffer_decref(prev_buffer);
2400 void CDECL wined3d_device_context_draw(struct wined3d_device_context *context, unsigned int start_vertex,
2401 unsigned int vertex_count, unsigned int start_instance, unsigned int instance_count)
2403 struct wined3d_state *state = context->state;
2405 TRACE("context %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
2406 context, start_vertex, vertex_count, start_instance, instance_count);
2408 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2409 0, start_vertex, vertex_count, start_instance, instance_count, false);
2412 void CDECL wined3d_device_context_draw_indexed(struct wined3d_device_context *context, int base_vertex_index,
2413 unsigned int start_index, unsigned int index_count, unsigned int start_instance, unsigned int instance_count)
2415 struct wined3d_state *state = context->state;
2417 TRACE("context %p, base_vertex_index %d, start_index %u, index_count %u, start_instance %u, instance_count %u.\n",
2418 context, base_vertex_index, start_index, index_count, start_instance, instance_count);
2420 wined3d_device_context_emit_draw(context, state->primitive_type, state->patch_vertex_count,
2421 base_vertex_index, start_index, index_count, start_instance, instance_count, true);
2424 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2426 TRACE("device %p, shader %p.\n", device, shader);
2428 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, shader);
2431 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2433 TRACE("device %p.\n", device);
2435 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_VERTEX];
2438 void CDECL wined3d_device_set_constant_buffer(struct wined3d_device *device,
2439 enum wined3d_shader_type type, UINT idx, struct wined3d_buffer *buffer)
2441 TRACE("device %p, type %#x, idx %u, buffer %p.\n", device, type, idx, buffer);
2443 return wined3d_device_context_set_constant_buffer(&device->cs->c, type, idx, buffer);
2446 struct wined3d_buffer * CDECL wined3d_device_get_constant_buffer(const struct wined3d_device *device,
2447 enum wined3d_shader_type shader_type, unsigned int idx)
2449 TRACE("device %p, shader_type %#x, idx %u.\n", device, shader_type, idx);
2451 if (idx >= MAX_CONSTANT_BUFFERS)
2453 WARN("Invalid constant buffer index %u.\n", idx);
2454 return NULL;
2457 return device->cs->c.state->cb[shader_type][idx];
2460 void CDECL wined3d_device_set_vs_resource_view(struct wined3d_device *device,
2461 UINT idx, struct wined3d_shader_resource_view *view)
2463 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2465 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, idx, view);
2468 static struct wined3d_shader_resource_view *wined3d_device_get_shader_resource_view(
2469 const struct wined3d_device *device, enum wined3d_shader_type shader_type, unsigned int idx)
2471 if (idx >= MAX_SHADER_RESOURCE_VIEWS)
2473 WARN("Invalid view index %u.\n", idx);
2474 return NULL;
2477 return device->cs->c.state->shader_resource_view[shader_type][idx];
2480 struct wined3d_shader_resource_view * CDECL wined3d_device_get_vs_resource_view(const struct wined3d_device *device,
2481 UINT idx)
2483 TRACE("device %p, idx %u.\n", device, idx);
2485 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2488 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2490 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2492 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_VERTEX, idx, sampler);
2495 static struct wined3d_sampler *wined3d_device_get_sampler(const struct wined3d_device *device,
2496 enum wined3d_shader_type shader_type, unsigned int idx)
2498 if (idx >= MAX_SAMPLER_OBJECTS)
2500 WARN("Invalid sampler index %u.\n", idx);
2501 return NULL;
2504 return device->cs->c.state->sampler[shader_type][idx];
2507 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2509 TRACE("device %p, idx %u.\n", device, idx);
2511 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_VERTEX, idx);
2514 static void wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2515 unsigned int start_idx, unsigned int count, const BOOL *constants)
2517 unsigned int i;
2519 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2520 device, start_idx, count, constants);
2522 memcpy(&device->cs->c.state->vs_consts_b[start_idx], constants, count * sizeof(*constants));
2523 if (TRACE_ON(d3d))
2525 for (i = 0; i < count; ++i)
2526 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2529 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_B, start_idx, count, constants);
2532 static void wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2533 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2535 unsigned int i;
2537 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2538 device, start_idx, count, constants);
2540 memcpy(&device->cs->c.state->vs_consts_i[start_idx], constants, count * sizeof(*constants));
2541 if (TRACE_ON(d3d))
2543 for (i = 0; i < count; ++i)
2544 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2547 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_I, start_idx, count, constants);
2550 static void wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2551 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2553 unsigned int i;
2555 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2556 device, start_idx, count, constants);
2558 memcpy(&device->cs->c.state->vs_consts_f[start_idx], constants, count * sizeof(*constants));
2559 if (TRACE_ON(d3d))
2561 for (i = 0; i < count; ++i)
2562 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2565 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_VS_F, start_idx, count, constants);
2568 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2570 TRACE("device %p, shader %p.\n", device, shader);
2572 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, shader);
2575 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2577 TRACE("device %p.\n", device);
2579 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_PIXEL];
2582 void CDECL wined3d_device_set_ps_resource_view(struct wined3d_device *device,
2583 UINT idx, struct wined3d_shader_resource_view *view)
2585 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2587 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, idx, view);
2590 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ps_resource_view(const struct wined3d_device *device,
2591 UINT idx)
2593 TRACE("device %p, idx %u.\n", device, idx);
2595 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2598 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2600 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2602 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_PIXEL, idx, sampler);
2605 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2607 TRACE("device %p, idx %u.\n", device, idx);
2609 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_PIXEL, idx);
2612 static void wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2613 unsigned int start_idx, unsigned int count, const BOOL *constants)
2615 unsigned int i;
2617 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2618 device, start_idx, count, constants);
2620 memcpy(&device->cs->c.state->ps_consts_b[start_idx], constants, count * sizeof(*constants));
2621 if (TRACE_ON(d3d))
2623 for (i = 0; i < count; ++i)
2624 TRACE("Set BOOL constant %u to %#x.\n", start_idx + i, constants[i]);
2627 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_B, start_idx, count, constants);
2630 static void wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2631 unsigned int start_idx, unsigned int count, const struct wined3d_ivec4 *constants)
2633 unsigned int i;
2635 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2636 device, start_idx, count, constants);
2638 memcpy(&device->cs->c.state->ps_consts_i[start_idx], constants, count * sizeof(*constants));
2639 if (TRACE_ON(d3d))
2641 for (i = 0; i < count; ++i)
2642 TRACE("Set ivec4 constant %u to %s.\n", start_idx + i, debug_ivec4(&constants[i]));
2645 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_I, start_idx, count, constants);
2648 static void wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2649 unsigned int start_idx, unsigned int count, const struct wined3d_vec4 *constants)
2651 unsigned int i;
2653 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2654 device, start_idx, count, constants);
2656 memcpy(&device->cs->c.state->ps_consts_f[start_idx], constants, count * sizeof(*constants));
2657 if (TRACE_ON(d3d))
2659 for (i = 0; i < count; ++i)
2660 TRACE("Set vec4 constant %u to %s.\n", start_idx + i, debug_vec4(&constants[i]));
2663 wined3d_cs_push_constants(device->cs, WINED3D_PUSH_CONSTANTS_PS_F, start_idx, count, constants);
2666 void CDECL wined3d_device_set_hull_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2668 TRACE("device %p, shader %p.\n", device, shader);
2670 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_HULL, shader);
2673 struct wined3d_shader * CDECL wined3d_device_get_hull_shader(const struct wined3d_device *device)
2675 TRACE("device %p.\n", device);
2677 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_HULL];
2680 void CDECL wined3d_device_set_hs_resource_view(struct wined3d_device *device,
2681 unsigned int idx, struct wined3d_shader_resource_view *view)
2683 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2685 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_HULL, idx, view);
2688 struct wined3d_shader_resource_view * CDECL wined3d_device_get_hs_resource_view(const struct wined3d_device *device,
2689 unsigned int idx)
2691 TRACE("device %p, idx %u.\n", device, idx);
2693 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_HULL, idx);
2696 void CDECL wined3d_device_set_hs_sampler(struct wined3d_device *device,
2697 unsigned int idx, struct wined3d_sampler *sampler)
2699 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2701 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_HULL, idx, sampler);
2704 struct wined3d_sampler * CDECL wined3d_device_get_hs_sampler(const struct wined3d_device *device, unsigned int idx)
2706 TRACE("device %p, idx %u.\n", device, idx);
2708 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_HULL, idx);
2711 void CDECL wined3d_device_set_domain_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2713 TRACE("device %p, shader %p.\n", device, shader);
2715 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, shader);
2718 struct wined3d_shader * CDECL wined3d_device_get_domain_shader(const struct wined3d_device *device)
2720 TRACE("device %p.\n", device);
2722 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_DOMAIN];
2725 void CDECL wined3d_device_set_ds_resource_view(struct wined3d_device *device,
2726 unsigned int idx, struct wined3d_shader_resource_view *view)
2728 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2730 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, idx, view);
2733 struct wined3d_shader_resource_view * CDECL wined3d_device_get_ds_resource_view(const struct wined3d_device *device,
2734 unsigned int idx)
2736 TRACE("device %p, idx %u.\n", device, idx);
2738 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2741 void CDECL wined3d_device_set_ds_sampler(struct wined3d_device *device,
2742 unsigned int idx, struct wined3d_sampler *sampler)
2744 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2746 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_DOMAIN, idx, sampler);
2749 struct wined3d_sampler * CDECL wined3d_device_get_ds_sampler(const struct wined3d_device *device, unsigned int idx)
2751 TRACE("device %p, idx %u.\n", device, idx);
2753 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_DOMAIN, idx);
2756 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2758 TRACE("device %p, shader %p.\n", device, shader);
2760 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, shader);
2763 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
2765 TRACE("device %p.\n", device);
2767 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
2770 void CDECL wined3d_device_set_gs_resource_view(struct wined3d_device *device,
2771 UINT idx, struct wined3d_shader_resource_view *view)
2773 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2775 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, idx, view);
2778 struct wined3d_shader_resource_view * CDECL wined3d_device_get_gs_resource_view(const struct wined3d_device *device,
2779 UINT idx)
2781 TRACE("device %p, idx %u.\n", device, idx);
2783 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2786 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2788 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2790 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_GEOMETRY, idx, sampler);
2793 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
2795 TRACE("device %p, idx %u.\n", device, idx);
2797 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_GEOMETRY, idx);
2800 void CDECL wined3d_device_set_compute_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2802 TRACE("device %p, shader %p.\n", device, shader);
2804 return wined3d_device_context_set_shader(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, shader);
2807 struct wined3d_shader * CDECL wined3d_device_get_compute_shader(const struct wined3d_device *device)
2809 TRACE("device %p.\n", device);
2811 return device->cs->c.state->shader[WINED3D_SHADER_TYPE_COMPUTE];
2814 void CDECL wined3d_device_set_cs_resource_view(struct wined3d_device *device,
2815 unsigned int idx, struct wined3d_shader_resource_view *view)
2817 TRACE("device %p, idx %u, view %p.\n", device, idx, view);
2819 wined3d_device_context_set_shader_resource_view(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, idx, view);
2822 struct wined3d_shader_resource_view * CDECL wined3d_device_get_cs_resource_view(const struct wined3d_device *device,
2823 unsigned int idx)
2825 TRACE("device %p, idx %u.\n", device, idx);
2827 return wined3d_device_get_shader_resource_view(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2830 void CDECL wined3d_device_set_cs_sampler(struct wined3d_device *device,
2831 unsigned int idx, struct wined3d_sampler *sampler)
2833 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2835 wined3d_device_context_set_sampler(&device->cs->c, WINED3D_SHADER_TYPE_COMPUTE, idx, sampler);
2838 struct wined3d_sampler * CDECL wined3d_device_get_cs_sampler(const struct wined3d_device *device, unsigned int idx)
2840 TRACE("device %p, idx %u.\n", device, idx);
2842 return wined3d_device_get_sampler(device, WINED3D_SHADER_TYPE_COMPUTE, idx);
2845 static struct wined3d_unordered_access_view *wined3d_device_get_pipeline_unordered_access_view(
2846 const struct wined3d_device *device, enum wined3d_pipeline pipeline, unsigned int idx)
2848 if (idx >= MAX_UNORDERED_ACCESS_VIEWS)
2850 WARN("Invalid UAV index %u.\n", idx);
2851 return NULL;
2854 return device->cs->c.state->unordered_access_view[pipeline][idx];
2857 void CDECL wined3d_device_set_cs_uav(struct wined3d_device *device, unsigned int idx,
2858 struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2860 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2862 wined3d_device_context_set_unordered_access_view(&device->cs->c, WINED3D_PIPELINE_COMPUTE, idx, uav, initial_count);
2865 struct wined3d_unordered_access_view * CDECL wined3d_device_get_cs_uav(const struct wined3d_device *device,
2866 unsigned int idx)
2868 TRACE("device %p, idx %u.\n", device, idx);
2870 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_COMPUTE, idx);
2873 void CDECL wined3d_device_set_unordered_access_view(struct wined3d_device *device,
2874 unsigned int idx, struct wined3d_unordered_access_view *uav, unsigned int initial_count)
2876 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device, idx, uav, initial_count);
2878 wined3d_device_context_set_unordered_access_view(&device->cs->c, WINED3D_PIPELINE_GRAPHICS, idx, uav, initial_count);
2881 struct wined3d_unordered_access_view * CDECL wined3d_device_get_unordered_access_view(
2882 const struct wined3d_device *device, unsigned int idx)
2884 TRACE("device %p, idx %u.\n", device, idx);
2886 return wined3d_device_get_pipeline_unordered_access_view(device, WINED3D_PIPELINE_GRAPHICS, idx);
2889 void CDECL wined3d_device_set_max_frame_latency(struct wined3d_device *device, unsigned int latency)
2891 unsigned int i;
2893 if (!latency)
2894 latency = 3;
2896 device->max_frame_latency = latency;
2897 for (i = 0; i < device->swapchain_count; ++i)
2898 swapchain_set_max_frame_latency(device->swapchains[i], device);
2901 unsigned int CDECL wined3d_device_get_max_frame_latency(const struct wined3d_device *device)
2903 return device->max_frame_latency;
2906 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf)
2908 unsigned int texcoord_count = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
2909 unsigned int i, size = 0;
2911 if (fvf & WINED3DFVF_NORMAL) size += 3 * sizeof(float);
2912 if (fvf & WINED3DFVF_DIFFUSE) size += sizeof(DWORD);
2913 if (fvf & WINED3DFVF_SPECULAR) size += sizeof(DWORD);
2914 if (fvf & WINED3DFVF_PSIZE) size += sizeof(DWORD);
2915 switch (fvf & WINED3DFVF_POSITION_MASK)
2917 case WINED3DFVF_XYZ: size += 3 * sizeof(float); break;
2918 case WINED3DFVF_XYZRHW: size += 4 * sizeof(float); break;
2919 case WINED3DFVF_XYZB1: size += 4 * sizeof(float); break;
2920 case WINED3DFVF_XYZB2: size += 5 * sizeof(float); break;
2921 case WINED3DFVF_XYZB3: size += 6 * sizeof(float); break;
2922 case WINED3DFVF_XYZB4: size += 7 * sizeof(float); break;
2923 case WINED3DFVF_XYZB5: size += 8 * sizeof(float); break;
2924 case WINED3DFVF_XYZW: size += 4 * sizeof(float); break;
2925 default: FIXME("Unexpected position mask %#x.\n", fvf & WINED3DFVF_POSITION_MASK);
2927 for (i = 0; i < texcoord_count; ++i)
2929 size += GET_TEXCOORD_SIZE_FROM_FVF(fvf, i) * sizeof(float);
2932 return size;
2935 static void wined3d_format_get_colour(const struct wined3d_format *format,
2936 const void *data, struct wined3d_color *colour)
2938 float *output = &colour->r;
2939 const uint32_t *u32_data;
2940 const uint16_t *u16_data;
2941 const float *f32_data;
2942 unsigned int i;
2944 static const struct wined3d_color default_colour = {0.0f, 0.0f, 0.0f, 1.0f};
2945 static unsigned int warned;
2947 switch (format->id)
2949 case WINED3DFMT_B8G8R8A8_UNORM:
2950 u32_data = data;
2951 wined3d_color_from_d3dcolor(colour, *u32_data);
2952 break;
2954 case WINED3DFMT_R8G8B8A8_UNORM:
2955 u32_data = data;
2956 colour->r = (*u32_data & 0xffu) / 255.0f;
2957 colour->g = ((*u32_data >> 8) & 0xffu) / 255.0f;
2958 colour->b = ((*u32_data >> 16) & 0xffu) / 255.0f;
2959 colour->a = ((*u32_data >> 24) & 0xffu) / 255.0f;
2960 break;
2962 case WINED3DFMT_R16G16_UNORM:
2963 case WINED3DFMT_R16G16B16A16_UNORM:
2964 u16_data = data;
2965 *colour = default_colour;
2966 for (i = 0; i < format->component_count; ++i)
2967 output[i] = u16_data[i] / 65535.0f;
2968 break;
2970 case WINED3DFMT_R32_FLOAT:
2971 case WINED3DFMT_R32G32_FLOAT:
2972 case WINED3DFMT_R32G32B32_FLOAT:
2973 case WINED3DFMT_R32G32B32A32_FLOAT:
2974 f32_data = data;
2975 *colour = default_colour;
2976 for (i = 0; i < format->component_count; ++i)
2977 output[i] = f32_data[i];
2978 break;
2980 default:
2981 *colour = default_colour;
2982 if (!warned++)
2983 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format->id));
2984 break;
2988 static void wined3d_colour_from_mcs(struct wined3d_color *colour, enum wined3d_material_color_source mcs,
2989 const struct wined3d_color *material_colour, unsigned int index,
2990 const struct wined3d_stream_info *stream_info)
2992 const struct wined3d_stream_info_element *element = NULL;
2994 switch (mcs)
2996 case WINED3D_MCS_MATERIAL:
2997 *colour = *material_colour;
2998 return;
3000 case WINED3D_MCS_COLOR1:
3001 if (!(stream_info->use_map & (1u << WINED3D_FFP_DIFFUSE)))
3003 colour->r = colour->g = colour->b = colour->a = 1.0f;
3004 return;
3006 element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3007 break;
3009 case WINED3D_MCS_COLOR2:
3010 if (!(stream_info->use_map & (1u << WINED3D_FFP_SPECULAR)))
3012 colour->r = colour->g = colour->b = colour->a = 0.0f;
3013 return;
3015 element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3016 break;
3018 default:
3019 colour->r = colour->g = colour->b = colour->a = 0.0f;
3020 ERR("Invalid material colour source %#x.\n", mcs);
3021 return;
3024 wined3d_format_get_colour(element->format, &element->data.addr[index * element->stride], colour);
3027 static float wined3d_clamp(float value, float min_value, float max_value)
3029 return value < min_value ? min_value : value > max_value ? max_value : value;
3032 static float wined3d_vec3_dot(const struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3034 return v0->x * v1->x + v0->y * v1->y + v0->z * v1->z;
3037 static void wined3d_vec3_subtract(struct wined3d_vec3 *v0, const struct wined3d_vec3 *v1)
3039 v0->x -= v1->x;
3040 v0->y -= v1->y;
3041 v0->z -= v1->z;
3044 static void wined3d_vec3_scale(struct wined3d_vec3 *v, float s)
3046 v->x *= s;
3047 v->y *= s;
3048 v->z *= s;
3051 static void wined3d_vec3_normalise(struct wined3d_vec3 *v)
3053 float rnorm = 1.0f / sqrtf(wined3d_vec3_dot(v, v));
3055 if (isfinite(rnorm))
3056 wined3d_vec3_scale(v, rnorm);
3059 static void wined3d_vec3_transform(struct wined3d_vec3 *dst,
3060 const struct wined3d_vec3 *v, const struct wined3d_matrix_3x3 *m)
3062 struct wined3d_vec3 tmp;
3064 tmp.x = v->x * m->_11 + v->y * m->_21 + v->z * m->_31;
3065 tmp.y = v->x * m->_12 + v->y * m->_22 + v->z * m->_32;
3066 tmp.z = v->x * m->_13 + v->y * m->_23 + v->z * m->_33;
3068 *dst = tmp;
3071 static void wined3d_color_clamp(struct wined3d_color *dst, const struct wined3d_color *src,
3072 float min_value, float max_value)
3074 dst->r = wined3d_clamp(src->r, min_value, max_value);
3075 dst->g = wined3d_clamp(src->g, min_value, max_value);
3076 dst->b = wined3d_clamp(src->b, min_value, max_value);
3077 dst->a = wined3d_clamp(src->a, min_value, max_value);
3080 static void wined3d_color_rgb_mul_add(struct wined3d_color *dst, const struct wined3d_color *src, float c)
3082 dst->r += src->r * c;
3083 dst->g += src->g * c;
3084 dst->b += src->b * c;
3087 static void init_transformed_lights(struct lights_settings *ls,
3088 const struct wined3d_state *state, BOOL legacy_lighting, BOOL compute_lighting)
3090 const struct wined3d_light_info *lights[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS];
3091 const struct wined3d_light_info *light_info;
3092 struct light_transformed *light;
3093 struct wined3d_vec4 vec4;
3094 unsigned int light_count;
3095 unsigned int i, index;
3097 memset(ls, 0, sizeof(*ls));
3099 ls->lighting = !!compute_lighting;
3100 ls->fog_mode = state->render_states[WINED3D_RS_FOGVERTEXMODE];
3101 ls->fog_coord_mode = state->render_states[WINED3D_RS_RANGEFOGENABLE]
3102 ? WINED3D_FFP_VS_FOG_RANGE : WINED3D_FFP_VS_FOG_DEPTH;
3103 ls->fog_start = wined3d_get_float_state(state, WINED3D_RS_FOGSTART);
3104 ls->fog_end = wined3d_get_float_state(state, WINED3D_RS_FOGEND);
3105 ls->fog_density = wined3d_get_float_state(state, WINED3D_RS_FOGDENSITY);
3107 if (ls->fog_mode == WINED3D_FOG_NONE && !compute_lighting)
3108 return;
3110 multiply_matrix(&ls->modelview_matrix, &state->transforms[WINED3D_TS_VIEW],
3111 &state->transforms[WINED3D_TS_WORLD_MATRIX(0)]);
3113 if (!compute_lighting)
3114 return;
3116 compute_normal_matrix(&ls->normal_matrix._11, legacy_lighting, &ls->modelview_matrix);
3118 wined3d_color_from_d3dcolor(&ls->ambient_light, state->render_states[WINED3D_RS_AMBIENT]);
3119 ls->legacy_lighting = !!legacy_lighting;
3120 ls->normalise = !!state->render_states[WINED3D_RS_NORMALIZENORMALS];
3121 ls->localviewer = !!state->render_states[WINED3D_RS_LOCALVIEWER];
3123 for (i = 0, index = 0; i < LIGHTMAP_SIZE && index < ARRAY_SIZE(lights); ++i)
3125 LIST_FOR_EACH_ENTRY(light_info, &state->light_state.light_map[i], struct wined3d_light_info, entry)
3127 if (!light_info->enabled)
3128 continue;
3130 switch (light_info->OriginalParms.type)
3132 case WINED3D_LIGHT_DIRECTIONAL:
3133 ++ls->directional_light_count;
3134 break;
3136 case WINED3D_LIGHT_POINT:
3137 ++ls->point_light_count;
3138 break;
3140 case WINED3D_LIGHT_SPOT:
3141 ++ls->spot_light_count;
3142 break;
3144 case WINED3D_LIGHT_PARALLELPOINT:
3145 ++ls->parallel_point_light_count;
3146 break;
3148 default:
3149 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
3150 continue;
3152 lights[index++] = light_info;
3153 if (index == WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS)
3154 break;
3158 light_count = index;
3159 for (i = 0, index = 0; i < light_count; ++i)
3161 light_info = lights[i];
3162 if (light_info->OriginalParms.type != WINED3D_LIGHT_DIRECTIONAL)
3163 continue;
3165 light = &ls->lights[index];
3166 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3167 light->direction = *(struct wined3d_vec3 *)&vec4;
3168 wined3d_vec3_normalise(&light->direction);
3170 light->diffuse = light_info->OriginalParms.diffuse;
3171 light->ambient = light_info->OriginalParms.ambient;
3172 light->specular = light_info->OriginalParms.specular;
3173 ++index;
3176 for (i = 0; i < light_count; ++i)
3178 light_info = lights[i];
3179 if (light_info->OriginalParms.type != WINED3D_LIGHT_POINT)
3180 continue;
3182 light = &ls->lights[index];
3184 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3185 light->range = light_info->OriginalParms.range;
3186 light->c_att = light_info->OriginalParms.attenuation0;
3187 light->l_att = light_info->OriginalParms.attenuation1;
3188 light->q_att = light_info->OriginalParms.attenuation2;
3190 light->diffuse = light_info->OriginalParms.diffuse;
3191 light->ambient = light_info->OriginalParms.ambient;
3192 light->specular = light_info->OriginalParms.specular;
3193 ++index;
3196 for (i = 0; i < light_count; ++i)
3198 light_info = lights[i];
3199 if (light_info->OriginalParms.type != WINED3D_LIGHT_SPOT)
3200 continue;
3202 light = &ls->lights[index];
3204 wined3d_vec4_transform(&light->position, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3205 wined3d_vec4_transform(&vec4, &light_info->direction, &state->transforms[WINED3D_TS_VIEW]);
3206 light->direction = *(struct wined3d_vec3 *)&vec4;
3207 wined3d_vec3_normalise(&light->direction);
3208 light->range = light_info->OriginalParms.range;
3209 light->falloff = light_info->OriginalParms.falloff;
3210 light->c_att = light_info->OriginalParms.attenuation0;
3211 light->l_att = light_info->OriginalParms.attenuation1;
3212 light->q_att = light_info->OriginalParms.attenuation2;
3213 light->cos_htheta = cosf(light_info->OriginalParms.theta / 2.0f);
3214 light->cos_hphi = cosf(light_info->OriginalParms.phi / 2.0f);
3216 light->diffuse = light_info->OriginalParms.diffuse;
3217 light->ambient = light_info->OriginalParms.ambient;
3218 light->specular = light_info->OriginalParms.specular;
3219 ++index;
3222 for (i = 0; i < light_count; ++i)
3224 light_info = lights[i];
3225 if (light_info->OriginalParms.type != WINED3D_LIGHT_PARALLELPOINT)
3226 continue;
3228 light = &ls->lights[index];
3230 wined3d_vec4_transform(&vec4, &light_info->position, &state->transforms[WINED3D_TS_VIEW]);
3231 *(struct wined3d_vec3 *)&light->position = *(struct wined3d_vec3 *)&vec4;
3232 wined3d_vec3_normalise((struct wined3d_vec3 *)&light->position);
3233 light->diffuse = light_info->OriginalParms.diffuse;
3234 light->ambient = light_info->OriginalParms.ambient;
3235 light->specular = light_info->OriginalParms.specular;
3236 ++index;
3240 static void update_light_diffuse_specular(struct wined3d_color *diffuse, struct wined3d_color *specular,
3241 const struct wined3d_vec3 *dir, float att, float material_shininess,
3242 const struct wined3d_vec3 *normal_transformed,
3243 const struct wined3d_vec3 *position_transformed_normalised,
3244 const struct light_transformed *light, const struct lights_settings *ls)
3246 struct wined3d_vec3 vec3;
3247 float t, c;
3249 c = wined3d_clamp(wined3d_vec3_dot(dir, normal_transformed), 0.0f, 1.0f);
3250 wined3d_color_rgb_mul_add(diffuse, &light->diffuse, c * att);
3252 vec3 = *dir;
3253 if (ls->localviewer)
3254 wined3d_vec3_subtract(&vec3, position_transformed_normalised);
3255 else
3256 vec3.z -= 1.0f;
3257 wined3d_vec3_normalise(&vec3);
3258 t = wined3d_vec3_dot(normal_transformed, &vec3);
3259 if (t > 0.0f && (!ls->legacy_lighting || material_shininess > 0.0f)
3260 && wined3d_vec3_dot(dir, normal_transformed) > 0.0f)
3261 wined3d_color_rgb_mul_add(specular, &light->specular, att * powf(t, material_shininess));
3264 static void light_set_vertex_data(struct lights_settings *ls,
3265 const struct wined3d_vec4 *position)
3267 if (ls->fog_mode == WINED3D_FOG_NONE && !ls->lighting)
3268 return;
3270 wined3d_vec4_transform(&ls->position_transformed, position, &ls->modelview_matrix);
3271 wined3d_vec3_scale((struct wined3d_vec3 *)&ls->position_transformed, 1.0f / ls->position_transformed.w);
3274 static void compute_light(struct wined3d_color *ambient, struct wined3d_color *diffuse,
3275 struct wined3d_color *specular, struct lights_settings *ls, const struct wined3d_vec3 *normal,
3276 float material_shininess)
3278 struct wined3d_vec3 position_transformed_normalised;
3279 struct wined3d_vec3 normal_transformed = {0.0f};
3280 const struct light_transformed *light;
3281 struct wined3d_vec3 dir, dst;
3282 unsigned int i, index;
3283 float att;
3285 position_transformed_normalised = *(const struct wined3d_vec3 *)&ls->position_transformed;
3286 wined3d_vec3_normalise(&position_transformed_normalised);
3288 if (normal)
3290 wined3d_vec3_transform(&normal_transformed, normal, &ls->normal_matrix);
3291 if (ls->normalise)
3292 wined3d_vec3_normalise(&normal_transformed);
3295 diffuse->r = diffuse->g = diffuse->b = diffuse->a = 0.0f;
3296 *specular = *diffuse;
3297 *ambient = ls->ambient_light;
3299 index = 0;
3300 for (i = 0; i < ls->directional_light_count; ++i, ++index)
3302 light = &ls->lights[index];
3304 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3305 if (normal)
3306 update_light_diffuse_specular(diffuse, specular, &light->direction, 1.0f, material_shininess,
3307 &normal_transformed, &position_transformed_normalised, light, ls);
3310 for (i = 0; i < ls->point_light_count; ++i, ++index)
3312 light = &ls->lights[index];
3313 dir.x = light->position.x - ls->position_transformed.x;
3314 dir.y = light->position.y - ls->position_transformed.y;
3315 dir.z = light->position.z - ls->position_transformed.z;
3317 dst.z = wined3d_vec3_dot(&dir, &dir);
3318 dst.y = sqrtf(dst.z);
3319 dst.x = 1.0f;
3320 if (ls->legacy_lighting)
3322 dst.y = (light->range - dst.y) / light->range;
3323 if (!(dst.y > 0.0f))
3324 continue;
3325 dst.z = dst.y * dst.y;
3327 else
3329 if (!(dst.y <= light->range))
3330 continue;
3332 att = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3333 if (!ls->legacy_lighting)
3334 att = 1.0f / att;
3336 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3337 if (normal)
3339 wined3d_vec3_normalise(&dir);
3340 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3341 &normal_transformed, &position_transformed_normalised, light, ls);
3345 for (i = 0; i < ls->spot_light_count; ++i, ++index)
3347 float t;
3349 light = &ls->lights[index];
3351 dir.x = light->position.x - ls->position_transformed.x;
3352 dir.y = light->position.y - ls->position_transformed.y;
3353 dir.z = light->position.z - ls->position_transformed.z;
3355 dst.z = wined3d_vec3_dot(&dir, &dir);
3356 dst.y = sqrtf(dst.z);
3357 dst.x = 1.0f;
3359 if (ls->legacy_lighting)
3361 dst.y = (light->range - dst.y) / light->range;
3362 if (!(dst.y > 0.0f))
3363 continue;
3364 dst.z = dst.y * dst.y;
3366 else
3368 if (!(dst.y <= light->range))
3369 continue;
3371 wined3d_vec3_normalise(&dir);
3372 t = -wined3d_vec3_dot(&dir, &light->direction);
3373 if (t > light->cos_htheta)
3374 att = 1.0f;
3375 else if (t <= light->cos_hphi)
3376 att = 0.0f;
3377 else
3378 att = powf((t - light->cos_hphi) / (light->cos_htheta - light->cos_hphi), light->falloff);
3380 t = dst.x * light->c_att + dst.y * light->l_att + dst.z * light->q_att;
3381 if (ls->legacy_lighting)
3382 att *= t;
3383 else
3384 att /= t;
3386 wined3d_color_rgb_mul_add(ambient, &light->ambient, att);
3388 if (normal)
3389 update_light_diffuse_specular(diffuse, specular, &dir, att, material_shininess,
3390 &normal_transformed, &position_transformed_normalised, light, ls);
3393 for (i = 0; i < ls->parallel_point_light_count; ++i, ++index)
3395 light = &ls->lights[index];
3397 wined3d_color_rgb_mul_add(ambient, &light->ambient, 1.0f);
3398 if (normal)
3399 update_light_diffuse_specular(diffuse, specular, (const struct wined3d_vec3 *)&light->position,
3400 1.0f, material_shininess, &normal_transformed, &position_transformed_normalised, light, ls);
3404 static float wined3d_calculate_fog_factor(float fog_coord, const struct lights_settings *ls)
3406 switch (ls->fog_mode)
3408 case WINED3D_FOG_NONE:
3409 return fog_coord;
3410 case WINED3D_FOG_LINEAR:
3411 return (ls->fog_end - fog_coord) / (ls->fog_end - ls->fog_start);
3412 case WINED3D_FOG_EXP:
3413 return expf(-fog_coord * ls->fog_density);
3414 case WINED3D_FOG_EXP2:
3415 return expf(-fog_coord * fog_coord * ls->fog_density * ls->fog_density);
3416 default:
3417 ERR("Unhandled fog mode %#x.\n", ls->fog_mode);
3418 return 0.0f;
3422 static void update_fog_factor(float *fog_factor, struct lights_settings *ls)
3424 float fog_coord;
3426 if (ls->fog_mode == WINED3D_FOG_NONE)
3427 return;
3429 switch (ls->fog_coord_mode)
3431 case WINED3D_FFP_VS_FOG_RANGE:
3432 fog_coord = sqrtf(wined3d_vec3_dot((const struct wined3d_vec3 *)&ls->position_transformed,
3433 (const struct wined3d_vec3 *)&ls->position_transformed));
3434 break;
3436 case WINED3D_FFP_VS_FOG_DEPTH:
3437 fog_coord = fabsf(ls->position_transformed.z);
3438 break;
3440 default:
3441 ERR("Unhandled fog coordinate mode %#x.\n", ls->fog_coord_mode);
3442 return;
3444 *fog_factor = wined3d_calculate_fog_factor(fog_coord, ls);
3447 /* Context activation is done by the caller. */
3448 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3449 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3450 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags, DWORD dst_fvf)
3452 enum wined3d_material_color_source diffuse_source, specular_source, ambient_source, emissive_source;
3453 const struct wined3d_color *material_specular_state_colour;
3454 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3455 const struct wined3d_state *state = device->cs->c.state;
3456 const struct wined3d_format *output_colour_format;
3457 static const struct wined3d_color black;
3458 struct wined3d_map_desc map_desc;
3459 struct wined3d_box box = {0};
3460 struct wined3d_viewport vp;
3461 unsigned int texture_count;
3462 struct lights_settings ls;
3463 unsigned int vertex_size;
3464 BOOL do_clip, lighting;
3465 float min_z, max_z;
3466 unsigned int i;
3467 BYTE *dest_ptr;
3468 HRESULT hr;
3470 if (!(stream_info->use_map & (1u << WINED3D_FFP_POSITION)))
3472 ERR("Source has no position mask.\n");
3473 return WINED3DERR_INVALIDCALL;
3476 if (state->render_states[WINED3D_RS_CLIPPING])
3478 static BOOL warned = FALSE;
3480 * The clipping code is not quite correct. Some things need
3481 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3482 * so disable clipping for now.
3483 * (The graphics in Half-Life are broken, and my processvertices
3484 * test crashes with IDirect3DDevice3)
3485 do_clip = TRUE;
3487 do_clip = FALSE;
3488 if (!warned)
3490 warned = TRUE;
3491 FIXME("Clipping is broken and disabled for now\n");
3494 else
3495 do_clip = FALSE;
3497 vertex_size = wined3d_get_flexible_vertex_size(dst_fvf);
3498 box.left = dwDestIndex * vertex_size;
3499 box.right = box.left + dwCount * vertex_size;
3500 if (FAILED(hr = wined3d_resource_map(&dest->resource, 0, &map_desc, &box, WINED3D_MAP_WRITE)))
3502 WARN("Failed to map buffer, hr %#x.\n", hr);
3503 return hr;
3505 dest_ptr = map_desc.data;
3507 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3508 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3509 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3511 TRACE("View mat:\n");
3512 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._11, view_mat._12, view_mat._13, view_mat._14);
3513 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._21, view_mat._22, view_mat._23, view_mat._24);
3514 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._31, view_mat._32, view_mat._33, view_mat._34);
3515 TRACE("%.8e %.8e %.8e %.8e\n", view_mat._41, view_mat._42, view_mat._43, view_mat._44);
3517 TRACE("Proj mat:\n");
3518 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._11, proj_mat._12, proj_mat._13, proj_mat._14);
3519 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._21, proj_mat._22, proj_mat._23, proj_mat._24);
3520 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._31, proj_mat._32, proj_mat._33, proj_mat._34);
3521 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat._41, proj_mat._42, proj_mat._43, proj_mat._44);
3523 TRACE("World mat:\n");
3524 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._11, world_mat._12, world_mat._13, world_mat._14);
3525 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._21, world_mat._22, world_mat._23, world_mat._24);
3526 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._31, world_mat._32, world_mat._33, world_mat._34);
3527 TRACE("%.8e %.8e %.8e %.8e\n", world_mat._41, world_mat._42, world_mat._43, world_mat._44);
3529 /* Get the viewport */
3530 wined3d_device_get_viewports(device, NULL, &vp);
3531 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3532 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3534 multiply_matrix(&mat,&view_mat,&world_mat);
3535 multiply_matrix(&mat,&proj_mat,&mat);
3537 texture_count = (dst_fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3539 lighting = state->render_states[WINED3D_RS_LIGHTING]
3540 && (dst_fvf & (WINED3DFVF_DIFFUSE | WINED3DFVF_SPECULAR));
3541 wined3d_get_material_colour_source(&diffuse_source, &emissive_source,
3542 &ambient_source, &specular_source, state, stream_info);
3543 output_colour_format = wined3d_get_format(device->adapter, WINED3DFMT_B8G8R8A8_UNORM, 0);
3544 material_specular_state_colour = state->render_states[WINED3D_RS_SPECULARENABLE]
3545 ? &state->material.specular : &black;
3546 init_transformed_lights(&ls, state, device->adapter->d3d_info.wined3d_creation_flags
3547 & WINED3D_LEGACY_FFP_LIGHTING, lighting);
3549 wined3d_viewport_get_z_range(&vp, &min_z, &max_z);
3551 for (i = 0; i < dwCount; ++i)
3553 const struct wined3d_stream_info_element *position_element = &stream_info->elements[WINED3D_FFP_POSITION];
3554 const float *p = (const float *)&position_element->data.addr[i * position_element->stride];
3555 struct wined3d_color ambient, diffuse, specular;
3556 struct wined3d_vec4 position;
3557 unsigned int tex_index;
3559 position.x = p[0];
3560 position.y = p[1];
3561 position.z = p[2];
3562 position.w = 1.0f;
3564 light_set_vertex_data(&ls, &position);
3566 if ( ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3567 ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3568 /* The position first */
3569 float x, y, z, rhw;
3570 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3572 /* Multiplication with world, view and projection matrix. */
3573 x = (p[0] * mat._11) + (p[1] * mat._21) + (p[2] * mat._31) + mat._41;
3574 y = (p[0] * mat._12) + (p[1] * mat._22) + (p[2] * mat._32) + mat._42;
3575 z = (p[0] * mat._13) + (p[1] * mat._23) + (p[2] * mat._33) + mat._43;
3576 rhw = (p[0] * mat._14) + (p[1] * mat._24) + (p[2] * mat._34) + mat._44;
3578 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3580 /* WARNING: The following things are taken from d3d7 and were not yet checked
3581 * against d3d8 or d3d9!
3584 /* Clipping conditions: From msdn
3586 * A vertex is clipped if it does not match the following requirements
3587 * -rhw < x <= rhw
3588 * -rhw < y <= rhw
3589 * 0 < z <= rhw
3590 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3592 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3593 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3597 if (!do_clip || (-rhw - eps < x && -rhw - eps < y && -eps < z && x <= rhw + eps
3598 && y <= rhw + eps && z <= rhw + eps && rhw > eps))
3600 /* "Normal" viewport transformation (not clipped)
3601 * 1) The values are divided by rhw
3602 * 2) The y axis is negative, so multiply it with -1
3603 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3604 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3605 * 4) Multiply x with Width/2 and add Width/2
3606 * 5) The same for the height
3607 * 6) Add the viewpoint X and Y to the 2D coordinates and
3608 * The minimum Z value to z
3609 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3611 * Well, basically it's simply a linear transformation into viewport
3612 * coordinates
3615 x /= rhw;
3616 y /= rhw;
3617 z /= rhw;
3619 y *= -1;
3621 x *= vp.width / 2;
3622 y *= vp.height / 2;
3623 z *= max_z - min_z;
3625 x += vp.width / 2 + vp.x;
3626 y += vp.height / 2 + vp.y;
3627 z += min_z;
3629 rhw = 1 / rhw;
3630 } else {
3631 /* That vertex got clipped
3632 * Contrary to OpenGL it is not dropped completely, it just
3633 * undergoes a different calculation.
3635 TRACE("Vertex got clipped\n");
3636 x += rhw;
3637 y += rhw;
3639 x /= 2;
3640 y /= 2;
3642 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3643 * outside of the main vertex buffer memory. That needs some more
3644 * investigation...
3648 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3651 ( (float *) dest_ptr)[0] = x;
3652 ( (float *) dest_ptr)[1] = y;
3653 ( (float *) dest_ptr)[2] = z;
3654 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3656 dest_ptr += 3 * sizeof(float);
3658 if ((dst_fvf & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3659 dest_ptr += sizeof(float);
3662 if (dst_fvf & WINED3DFVF_PSIZE)
3663 dest_ptr += sizeof(DWORD);
3665 if (dst_fvf & WINED3DFVF_NORMAL)
3667 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3668 const float *normal = (const float *)(element->data.addr + i * element->stride);
3669 /* AFAIK this should go into the lighting information */
3670 FIXME("Didn't expect the destination to have a normal\n");
3671 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3674 if (lighting)
3676 const struct wined3d_stream_info_element *element;
3677 struct wined3d_vec3 *normal;
3679 if (stream_info->use_map & (1u << WINED3D_FFP_NORMAL))
3681 element = &stream_info->elements[WINED3D_FFP_NORMAL];
3682 normal = (struct wined3d_vec3 *)&element->data.addr[i * element->stride];
3684 else
3686 normal = NULL;
3688 compute_light(&ambient, &diffuse, &specular, &ls, normal,
3689 state->render_states[WINED3D_RS_SPECULARENABLE] ? state->material.power : 0.0f);
3692 if (dst_fvf & WINED3DFVF_DIFFUSE)
3694 struct wined3d_color material_diffuse, material_ambient, material_emissive, diffuse_colour;
3696 wined3d_colour_from_mcs(&material_diffuse, diffuse_source,
3697 &state->material.diffuse, i, stream_info);
3699 if (lighting)
3701 wined3d_colour_from_mcs(&material_ambient, ambient_source,
3702 &state->material.ambient, i, stream_info);
3703 wined3d_colour_from_mcs(&material_emissive, emissive_source,
3704 &state->material.emissive, i, stream_info);
3706 diffuse_colour.r = ambient.r * material_ambient.r
3707 + diffuse.r * material_diffuse.r + material_emissive.r;
3708 diffuse_colour.g = ambient.g * material_ambient.g
3709 + diffuse.g * material_diffuse.g + material_emissive.g;
3710 diffuse_colour.b = ambient.b * material_ambient.b
3711 + diffuse.b * material_diffuse.b + material_emissive.b;
3712 diffuse_colour.a = material_diffuse.a;
3714 else
3716 diffuse_colour = material_diffuse;
3718 wined3d_color_clamp(&diffuse_colour, &diffuse_colour, 0.0f, 1.0f);
3719 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &diffuse_colour);
3720 dest_ptr += sizeof(DWORD);
3723 if (dst_fvf & WINED3DFVF_SPECULAR)
3725 struct wined3d_color material_specular, specular_colour;
3727 wined3d_colour_from_mcs(&material_specular, specular_source,
3728 material_specular_state_colour, i, stream_info);
3730 if (lighting)
3732 specular_colour.r = specular.r * material_specular.r;
3733 specular_colour.g = specular.g * material_specular.g;
3734 specular_colour.b = specular.b * material_specular.b;
3735 specular_colour.a = ls.legacy_lighting ? 0.0f : material_specular.a;
3737 else
3739 specular_colour = material_specular;
3741 update_fog_factor(&specular_colour.a, &ls);
3742 wined3d_color_clamp(&specular_colour, &specular_colour, 0.0f, 1.0f);
3743 *((DWORD *)dest_ptr) = wined3d_format_convert_from_float(output_colour_format, &specular_colour);
3744 dest_ptr += sizeof(DWORD);
3747 for (tex_index = 0; tex_index < texture_count; ++tex_index)
3749 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3750 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3751 if (!(stream_info->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3753 ERR("No source texture, but destination requests one\n");
3754 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float);
3756 else
3758 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf, tex_index) * sizeof(float));
3763 wined3d_resource_unmap(&dest->resource, 0);
3765 return WINED3D_OK;
3767 #undef copy_and_next
3769 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3770 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3771 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3773 struct wined3d_state *state = device->cs->c.state;
3774 struct wined3d_stream_info stream_info;
3775 struct wined3d_resource *resource;
3776 struct wined3d_box box = {0};
3777 struct wined3d_shader *vs;
3778 unsigned int i, j;
3779 HRESULT hr;
3780 WORD map;
3782 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3783 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3784 device, src_start_idx, dst_idx, vertex_count,
3785 dst_buffer, declaration, flags, dst_fvf);
3787 if (declaration)
3788 FIXME("Output vertex declaration not implemented yet.\n");
3790 vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
3791 state->shader[WINED3D_SHADER_TYPE_VERTEX] = NULL;
3792 wined3d_stream_info_from_declaration(&stream_info, state, &device->adapter->d3d_info);
3793 state->shader[WINED3D_SHADER_TYPE_VERTEX] = vs;
3795 /* We can't convert FROM a VBO, and vertex buffers used to source into
3796 * process_vertices() are unlikely to ever be used for drawing. Release
3797 * VBOs in those buffers and fix up the stream_info structure.
3799 * Also apply the start index. */
3800 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3802 struct wined3d_stream_info_element *e;
3803 struct wined3d_map_desc map_desc;
3805 if (!(map & 1))
3806 continue;
3808 e = &stream_info.elements[i];
3809 resource = &state->streams[e->stream_idx].buffer->resource;
3810 box.left = src_start_idx * e->stride;
3811 box.right = box.left + vertex_count * e->stride;
3812 if (FAILED(wined3d_resource_map(resource, 0, &map_desc, &box, WINED3D_MAP_READ)))
3814 ERR("Failed to map resource.\n");
3815 for (j = 0, map = stream_info.use_map; map && j < i; map >>= 1, ++j)
3817 if (!(map & 1))
3818 continue;
3820 e = &stream_info.elements[j];
3821 resource = &state->streams[e->stream_idx].buffer->resource;
3822 if (FAILED(wined3d_resource_unmap(resource, 0)))
3823 ERR("Failed to unmap resource.\n");
3825 return WINED3DERR_INVALIDCALL;
3827 e->data.buffer_object = 0;
3828 e->data.addr += (ULONG_PTR)map_desc.data;
3831 hr = process_vertices_strided(device, dst_idx, vertex_count,
3832 &stream_info, dst_buffer, flags, dst_fvf);
3834 for (i = 0, map = stream_info.use_map; map; map >>= 1, ++i)
3836 if (!(map & 1))
3837 continue;
3839 resource = &state->streams[stream_info.elements[i].stream_idx].buffer->resource;
3840 if (FAILED(wined3d_resource_unmap(resource, 0)))
3841 ERR("Failed to unmap resource.\n");
3844 return hr;
3847 static void wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3848 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3850 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3852 TRACE("device %p, stage %u, state %s, value %#x.\n",
3853 device, stage, debug_d3dtexturestate(state), value);
3855 if (stage >= d3d_info->limits.ffp_blend_stages)
3857 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3858 stage, d3d_info->limits.ffp_blend_stages - 1);
3859 return;
3862 if (value == device->cs->c.state->texture_states[stage][state])
3864 TRACE("Application is setting the old value over, nothing to do.\n");
3865 return;
3868 device->cs->c.state->texture_states[stage][state] = value;
3870 wined3d_cs_emit_set_texture_state(device->cs, stage, state, value);
3873 static void wined3d_device_set_texture(struct wined3d_device *device,
3874 UINT stage, struct wined3d_texture *texture)
3876 struct wined3d_state *state = device->cs->c.state;
3877 struct wined3d_texture *prev;
3879 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3881 /* Windows accepts overflowing this array... we do not. */
3882 if (stage >= ARRAY_SIZE(state->textures))
3884 WARN("Ignoring invalid stage %u.\n", stage);
3885 return;
3888 prev = state->textures[stage];
3889 TRACE("Previous texture %p.\n", prev);
3891 if (texture == prev)
3893 TRACE("App is setting the same texture again, nothing to do.\n");
3894 return;
3897 TRACE("Setting new texture to %p.\n", texture);
3898 state->textures[stage] = texture;
3900 if (texture)
3901 wined3d_texture_incref(texture);
3902 wined3d_cs_emit_set_texture(device->cs, stage, texture);
3903 if (prev)
3904 wined3d_texture_decref(prev);
3906 return;
3909 void CDECL wined3d_device_apply_stateblock(struct wined3d_device *device,
3910 struct wined3d_stateblock *stateblock)
3912 BOOL set_blend_state = FALSE, set_depth_stencil_state = FALSE, set_rasterizer_state = FALSE;
3913 const struct wined3d_stateblock_state *state = &stateblock->stateblock_state;
3914 const struct wined3d_saved_states *changed = &stateblock->changed;
3915 const unsigned int word_bit_count = sizeof(DWORD) * CHAR_BIT;
3916 unsigned int i, j, start, idx;
3917 struct wined3d_range range;
3918 uint32_t map;
3920 TRACE("device %p, stateblock %p.\n", device, stateblock);
3922 if (changed->vertexShader)
3923 wined3d_device_set_vertex_shader(device, state->vs);
3924 if (changed->pixelShader)
3925 wined3d_device_set_pixel_shader(device, state->ps);
3927 for (start = 0; ; start = range.offset + range.size)
3929 if (!wined3d_bitmap_get_range(changed->vs_consts_f, WINED3D_MAX_VS_CONSTS_F, start, &range))
3930 break;
3932 wined3d_device_set_vs_consts_f(device, range.offset, range.size, &state->vs_consts_f[range.offset]);
3935 map = changed->vertexShaderConstantsI;
3936 for (start = 0; ; start = range.offset + range.size)
3938 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3939 break;
3941 wined3d_device_set_vs_consts_i(device, range.offset, range.size, &state->vs_consts_i[range.offset]);
3944 map = changed->vertexShaderConstantsB;
3945 for (start = 0; ; start = range.offset + range.size)
3947 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3948 break;
3950 wined3d_device_set_vs_consts_b(device, range.offset, range.size, &state->vs_consts_b[range.offset]);
3953 for (start = 0; ; start = range.offset + range.size)
3955 if (!wined3d_bitmap_get_range(changed->ps_consts_f, WINED3D_MAX_PS_CONSTS_F, start, &range))
3956 break;
3958 wined3d_device_set_ps_consts_f(device, range.offset, range.size, &state->ps_consts_f[range.offset]);
3961 map = changed->pixelShaderConstantsI;
3962 for (start = 0; ; start = range.offset + range.size)
3964 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_I, start, &range))
3965 break;
3967 wined3d_device_set_ps_consts_i(device, range.offset, range.size, &state->ps_consts_i[range.offset]);
3970 map = changed->pixelShaderConstantsB;
3971 for (start = 0; ; start = range.offset + range.size)
3973 if (!wined3d_bitmap_get_range(&map, WINED3D_MAX_CONSTS_B, start, &range))
3974 break;
3976 wined3d_device_set_ps_consts_b(device, range.offset, range.size, &state->ps_consts_b[range.offset]);
3979 if (changed->lights)
3981 for (i = 0; i < ARRAY_SIZE(state->light_state->light_map); ++i)
3983 const struct wined3d_light_info *light;
3985 LIST_FOR_EACH_ENTRY(light, &state->light_state->light_map[i], struct wined3d_light_info, entry)
3987 wined3d_device_set_light(device, light->OriginalIndex, &light->OriginalParms);
3988 wined3d_device_set_light_enable(device, light->OriginalIndex, light->glIndex != -1);
3993 for (i = 0; i < ARRAY_SIZE(changed->renderState); ++i)
3995 map = changed->renderState[i];
3996 while (map)
3998 j = wined3d_bit_scan(&map);
3999 idx = i * word_bit_count + j;
4001 switch (idx)
4003 case WINED3D_RS_BLENDFACTOR:
4004 case WINED3D_RS_MULTISAMPLEMASK:
4005 case WINED3D_RS_ALPHABLENDENABLE:
4006 case WINED3D_RS_SRCBLEND:
4007 case WINED3D_RS_DESTBLEND:
4008 case WINED3D_RS_BLENDOP:
4009 case WINED3D_RS_SEPARATEALPHABLENDENABLE:
4010 case WINED3D_RS_SRCBLENDALPHA:
4011 case WINED3D_RS_DESTBLENDALPHA:
4012 case WINED3D_RS_BLENDOPALPHA:
4013 case WINED3D_RS_COLORWRITEENABLE:
4014 case WINED3D_RS_COLORWRITEENABLE1:
4015 case WINED3D_RS_COLORWRITEENABLE2:
4016 case WINED3D_RS_COLORWRITEENABLE3:
4017 set_blend_state = TRUE;
4018 break;
4020 case WINED3D_RS_BACK_STENCILFAIL:
4021 case WINED3D_RS_BACK_STENCILFUNC:
4022 case WINED3D_RS_BACK_STENCILPASS:
4023 case WINED3D_RS_BACK_STENCILZFAIL:
4024 case WINED3D_RS_STENCILENABLE:
4025 case WINED3D_RS_STENCILFAIL:
4026 case WINED3D_RS_STENCILFUNC:
4027 case WINED3D_RS_STENCILREF:
4028 case WINED3D_RS_STENCILMASK:
4029 case WINED3D_RS_STENCILPASS:
4030 case WINED3D_RS_STENCILWRITEMASK:
4031 case WINED3D_RS_STENCILZFAIL:
4032 case WINED3D_RS_TWOSIDEDSTENCILMODE:
4033 case WINED3D_RS_ZENABLE:
4034 case WINED3D_RS_ZFUNC:
4035 case WINED3D_RS_ZWRITEENABLE:
4036 set_depth_stencil_state = TRUE;
4037 break;
4039 case WINED3D_RS_FILLMODE:
4040 case WINED3D_RS_CULLMODE:
4041 case WINED3D_RS_SLOPESCALEDEPTHBIAS:
4042 case WINED3D_RS_DEPTHBIAS:
4043 case WINED3D_RS_SCISSORTESTENABLE:
4044 case WINED3D_RS_ANTIALIASEDLINEENABLE:
4045 set_rasterizer_state = TRUE;
4046 break;
4048 default:
4049 wined3d_device_set_render_state(device, idx, state->rs[idx]);
4050 break;
4055 if (set_rasterizer_state)
4057 struct wined3d_rasterizer_state *rasterizer_state;
4058 struct wined3d_rasterizer_state_desc desc;
4059 struct wine_rb_entry *entry;
4060 union
4062 DWORD d;
4063 float f;
4064 } bias;
4066 memset(&desc, 0, sizeof(desc));
4067 desc.fill_mode = state->rs[WINED3D_RS_FILLMODE];
4068 desc.cull_mode = state->rs[WINED3D_RS_CULLMODE];
4069 bias.d = state->rs[WINED3D_RS_DEPTHBIAS];
4070 desc.depth_bias = bias.f;
4071 bias.d = state->rs[WINED3D_RS_SLOPESCALEDEPTHBIAS];
4072 desc.scale_bias = bias.f;
4073 desc.depth_clip = TRUE;
4074 desc.scissor = state->rs[WINED3D_RS_SCISSORTESTENABLE];
4075 desc.line_antialias = state->rs[WINED3D_RS_ANTIALIASEDLINEENABLE];
4077 if ((entry = wine_rb_get(&device->rasterizer_states, &desc)))
4079 rasterizer_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
4080 wined3d_device_set_rasterizer_state(device, rasterizer_state);
4082 else if (SUCCEEDED(wined3d_rasterizer_state_create(device, &desc, NULL,
4083 &wined3d_null_parent_ops, &rasterizer_state)))
4085 wined3d_device_set_rasterizer_state(device, rasterizer_state);
4086 if (wine_rb_put(&device->rasterizer_states, &desc, &rasterizer_state->entry) == -1)
4088 ERR("Failed to insert rasterizer state.\n");
4089 wined3d_rasterizer_state_decref(rasterizer_state);
4094 if (set_blend_state || changed->alpha_to_coverage
4095 || wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_ADAPTIVETESS_Y))
4097 struct wined3d_blend_state *blend_state;
4098 struct wined3d_blend_state_desc desc;
4099 struct wine_rb_entry *entry;
4100 struct wined3d_color colour;
4101 unsigned int sample_mask;
4103 memset(&desc, 0, sizeof(desc));
4104 desc.alpha_to_coverage = state->alpha_to_coverage;
4105 desc.independent = FALSE;
4106 if (state->rs[WINED3D_RS_ADAPTIVETESS_Y] == WINED3DFMT_ATOC)
4107 desc.alpha_to_coverage = TRUE;
4108 desc.rt[0].enable = state->rs[WINED3D_RS_ALPHABLENDENABLE];
4109 desc.rt[0].src = state->rs[WINED3D_RS_SRCBLEND];
4110 desc.rt[0].dst = state->rs[WINED3D_RS_DESTBLEND];
4111 desc.rt[0].op = state->rs[WINED3D_RS_BLENDOP];
4112 if (state->rs[WINED3D_RS_SEPARATEALPHABLENDENABLE])
4114 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLENDALPHA];
4115 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLENDALPHA];
4116 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOPALPHA];
4118 else
4120 desc.rt[0].src_alpha = state->rs[WINED3D_RS_SRCBLEND];
4121 desc.rt[0].dst_alpha = state->rs[WINED3D_RS_DESTBLEND];
4122 desc.rt[0].op_alpha = state->rs[WINED3D_RS_BLENDOP];
4124 desc.rt[0].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE];
4125 desc.rt[1].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE1];
4126 desc.rt[2].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE2];
4127 desc.rt[3].writemask = state->rs[WINED3D_RS_COLORWRITEENABLE3];
4128 if (desc.rt[1].writemask != desc.rt[0].writemask
4129 || desc.rt[2].writemask != desc.rt[0].writemask
4130 || desc.rt[3].writemask != desc.rt[0].writemask)
4132 desc.independent = TRUE;
4133 for (i = 1; i < 4; ++i)
4135 desc.rt[i].enable = desc.rt[0].enable;
4136 desc.rt[i].src = desc.rt[0].src;
4137 desc.rt[i].dst = desc.rt[0].dst;
4138 desc.rt[i].op = desc.rt[0].op;
4139 desc.rt[i].src_alpha = desc.rt[0].src_alpha;
4140 desc.rt[i].dst_alpha = desc.rt[0].dst_alpha;
4141 desc.rt[i].op_alpha = desc.rt[0].op_alpha;
4145 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_BLENDFACTOR))
4146 wined3d_color_from_d3dcolor(&colour, state->rs[WINED3D_RS_BLENDFACTOR]);
4147 else
4148 wined3d_device_get_blend_state(device, &colour, &sample_mask);
4150 if ((entry = wine_rb_get(&device->blend_states, &desc)))
4152 blend_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
4153 wined3d_device_set_blend_state(device, blend_state, &colour, state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4155 else if (SUCCEEDED(wined3d_blend_state_create(device, &desc, NULL,
4156 &wined3d_null_parent_ops, &blend_state)))
4158 wined3d_device_set_blend_state(device, blend_state, &colour, state->rs[WINED3D_RS_MULTISAMPLEMASK]);
4159 if (wine_rb_put(&device->blend_states, &desc, &blend_state->entry) == -1)
4161 ERR("Failed to insert blend state.\n");
4162 wined3d_blend_state_decref(blend_state);
4167 if (set_depth_stencil_state)
4169 struct wined3d_depth_stencil_state *depth_stencil_state;
4170 struct wined3d_depth_stencil_state_desc desc;
4171 struct wine_rb_entry *entry;
4172 unsigned int stencil_ref;
4174 memset(&desc, 0, sizeof(desc));
4175 switch (state->rs[WINED3D_RS_ZENABLE])
4177 case WINED3D_ZB_FALSE:
4178 desc.depth = FALSE;
4179 break;
4181 case WINED3D_ZB_USEW:
4182 FIXME("W buffer is not well handled.\n");
4183 case WINED3D_ZB_TRUE:
4184 desc.depth = TRUE;
4185 break;
4187 default:
4188 FIXME("Unrecognized depth buffer type %#x.\n", state->rs[WINED3D_RS_ZENABLE]);
4190 desc.depth_write = state->rs[WINED3D_RS_ZWRITEENABLE];
4191 desc.depth_func = state->rs[WINED3D_RS_ZFUNC];
4192 desc.stencil = state->rs[WINED3D_RS_STENCILENABLE];
4193 desc.stencil_read_mask = state->rs[WINED3D_RS_STENCILMASK];
4194 desc.stencil_write_mask = state->rs[WINED3D_RS_STENCILWRITEMASK];
4195 desc.front.fail_op = state->rs[WINED3D_RS_STENCILFAIL];
4196 desc.front.depth_fail_op = state->rs[WINED3D_RS_STENCILZFAIL];
4197 desc.front.pass_op = state->rs[WINED3D_RS_STENCILPASS];
4198 desc.front.func = state->rs[WINED3D_RS_STENCILFUNC];
4200 if (state->rs[WINED3D_RS_TWOSIDEDSTENCILMODE])
4202 desc.back.fail_op = state->rs[WINED3D_RS_BACK_STENCILFAIL];
4203 desc.back.depth_fail_op = state->rs[WINED3D_RS_BACK_STENCILZFAIL];
4204 desc.back.pass_op = state->rs[WINED3D_RS_BACK_STENCILPASS];
4205 desc.back.func = state->rs[WINED3D_RS_BACK_STENCILFUNC];
4207 else
4209 desc.back = desc.front;
4212 if (wined3d_bitmap_is_set(changed->renderState, WINED3D_RS_STENCILREF))
4213 stencil_ref = state->rs[WINED3D_RS_STENCILREF];
4214 else
4215 wined3d_device_get_depth_stencil_state(device, &stencil_ref);
4217 if ((entry = wine_rb_get(&device->depth_stencil_states, &desc)))
4219 depth_stencil_state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
4220 wined3d_device_set_depth_stencil_state(device, depth_stencil_state, stencil_ref);
4222 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device, &desc, NULL,
4223 &wined3d_null_parent_ops, &depth_stencil_state)))
4225 wined3d_device_set_depth_stencil_state(device, depth_stencil_state, stencil_ref);
4226 if (wine_rb_put(&device->depth_stencil_states, &desc, &depth_stencil_state->entry) == -1)
4228 ERR("Failed to insert depth/stencil state.\n");
4229 wined3d_depth_stencil_state_decref(depth_stencil_state);
4234 for (i = 0; i < ARRAY_SIZE(changed->textureState); ++i)
4236 map = changed->textureState[i];
4237 while (map)
4239 j = wined3d_bit_scan(&map);
4240 wined3d_device_set_texture_stage_state(device, i, j, state->texture_states[i][j]);
4244 for (i = 0; i < ARRAY_SIZE(changed->samplerState); ++i)
4246 map = changed->samplerState[i];
4247 while (map)
4249 j = wined3d_bit_scan(&map);
4250 wined3d_device_set_sampler_state(device, i, j, state->sampler_states[i][j]);
4254 if (changed->transforms)
4256 for (i = 0; i < ARRAY_SIZE(changed->transform); ++i)
4258 map = changed->transform[i];
4259 while (map)
4261 j = wined3d_bit_scan(&map);
4262 idx = i * word_bit_count + j;
4263 wined3d_device_set_transform(device, idx, &state->transforms[idx]);
4268 if (changed->indices)
4269 wined3d_device_set_index_buffer(device, state->index_buffer, state->index_format, 0);
4270 wined3d_device_set_base_vertex_index(device, state->base_vertex_index);
4271 if (changed->vertexDecl)
4272 wined3d_device_set_vertex_declaration(device, state->vertex_declaration);
4273 if (changed->material)
4274 wined3d_device_set_material(device, &state->material);
4275 if (changed->viewport)
4276 wined3d_device_set_viewports(device, 1, &state->viewport);
4277 if (changed->scissorRect)
4278 wined3d_device_set_scissor_rects(device, 1, &state->scissor_rect);
4280 map = changed->streamSource;
4281 while (map)
4283 i = wined3d_bit_scan(&map);
4284 wined3d_device_set_stream_source(device, i, state->streams[i].buffer,
4285 state->streams[i].offset, state->streams[i].stride);
4287 map = changed->streamFreq;
4288 while (map)
4290 i = wined3d_bit_scan(&map);
4291 wined3d_device_set_stream_source_freq(device, i,
4292 state->streams[i].frequency | state->streams[i].flags);
4295 map = changed->textures;
4296 while (map)
4298 i = wined3d_bit_scan(&map);
4299 wined3d_device_set_texture(device, i, state->textures[i]);
4302 map = changed->clipplane;
4303 while (map)
4305 i = wined3d_bit_scan(&map);
4306 wined3d_device_set_clip_plane(device, i, &state->clip_planes[i]);
4309 memset(&stateblock->changed, 0, sizeof(stateblock->changed));
4311 TRACE("Applied stateblock %p.\n", stateblock);
4314 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, struct wined3d_caps *caps)
4316 TRACE("device %p, caps %p.\n", device, caps);
4318 return wined3d_get_device_caps(device->adapter, device->create_parms.device_type, caps);
4321 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
4322 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
4324 struct wined3d_swapchain *swapchain;
4326 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
4327 device, swapchain_idx, mode, rotation);
4329 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4330 return WINED3DERR_INVALIDCALL;
4332 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
4335 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
4337 /* At the moment we have no need for any functionality at the beginning
4338 * of a scene. */
4339 TRACE("device %p.\n", device);
4341 if (device->inScene)
4343 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4344 return WINED3DERR_INVALIDCALL;
4346 device->inScene = TRUE;
4347 return WINED3D_OK;
4350 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
4352 TRACE("device %p.\n", device);
4354 if (!device->inScene)
4356 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4357 return WINED3DERR_INVALIDCALL;
4360 device->inScene = FALSE;
4361 return WINED3D_OK;
4364 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4365 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4367 struct wined3d_fb_state *fb = &device->cs->c.state->fb;
4369 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4370 device, rect_count, rects, flags, debug_color(color), depth, stencil);
4372 if (!rect_count && rects)
4374 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
4375 return WINED3D_OK;
4378 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4380 struct wined3d_rendertarget_view *ds = fb->depth_stencil;
4381 if (!ds)
4383 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4384 /* TODO: What about depth stencil buffers without stencil bits? */
4385 return WINED3DERR_INVALIDCALL;
4387 else if (flags & WINED3DCLEAR_TARGET)
4389 if (ds->width < fb->render_targets[0]->width
4390 || ds->height < fb->render_targets[0]->height)
4392 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4393 return WINED3D_OK;
4398 wined3d_cs_emit_clear(device->cs, rect_count, rects, flags, color, depth, stencil);
4400 return WINED3D_OK;
4403 void CDECL wined3d_device_set_predication(struct wined3d_device *device,
4404 struct wined3d_query *predicate, BOOL value)
4406 TRACE("device %p, predicate %p, value %#x.\n", device, predicate, value);
4408 wined3d_device_context_set_predication(&device->cs->c, predicate, value);
4411 struct wined3d_query * CDECL wined3d_device_get_predication(struct wined3d_device *device, BOOL *value)
4413 struct wined3d_state *state = device->cs->c.state;
4415 TRACE("device %p, value %p.\n", device, value);
4417 if (value)
4418 *value = state->predicate_value;
4419 return state->predicate;
4422 void CDECL wined3d_device_dispatch_compute(struct wined3d_device *device,
4423 unsigned int group_count_x, unsigned int group_count_y, unsigned int group_count_z)
4425 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
4426 device, group_count_x, group_count_y, group_count_z);
4428 wined3d_device_context_dispatch(&device->cs->c, group_count_x, group_count_y, group_count_z);
4431 void CDECL wined3d_device_dispatch_compute_indirect(struct wined3d_device *device,
4432 struct wined3d_buffer *buffer, unsigned int offset)
4434 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4436 wined3d_device_context_dispatch_indirect(&device->cs->c, buffer, offset);
4439 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
4440 enum wined3d_primitive_type primitive_type, unsigned int patch_vertex_count)
4442 struct wined3d_state *state = device->cs->c.state;
4444 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
4445 device, debug_d3dprimitivetype(primitive_type), patch_vertex_count);
4447 state->primitive_type = primitive_type;
4448 state->patch_vertex_count = patch_vertex_count;
4451 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
4452 enum wined3d_primitive_type *primitive_type, unsigned int *patch_vertex_count)
4454 const struct wined3d_state *state = device->cs->c.state;
4456 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
4457 device, primitive_type, patch_vertex_count);
4459 *primitive_type = state->primitive_type;
4460 if (patch_vertex_count)
4461 *patch_vertex_count = state->patch_vertex_count;
4463 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type));
4466 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4468 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4470 wined3d_device_context_draw(&device->cs->c, start_vertex, vertex_count, 0, 0);
4472 return WINED3D_OK;
4475 void CDECL wined3d_device_draw_primitive_instanced(struct wined3d_device *device,
4476 UINT start_vertex, UINT vertex_count, UINT start_instance, UINT instance_count)
4478 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
4479 device, start_vertex, vertex_count, start_instance, instance_count);
4481 wined3d_device_context_draw(&device->cs->c, start_vertex, vertex_count, start_instance, instance_count);
4484 void CDECL wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device *device,
4485 struct wined3d_buffer *buffer, unsigned int offset)
4487 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4489 wined3d_device_context_draw_indirect(&device->cs->c, buffer, offset, false);
4492 void CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4494 struct wined3d_state *state = device->cs->c.state;
4496 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4498 wined3d_device_context_draw_indexed(&device->cs->c, state->base_vertex_index, start_idx, index_count, 0, 0);
4501 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4502 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4504 struct wined3d_state *state = device->cs->c.state;
4506 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4507 device, start_idx, index_count, start_instance, instance_count);
4509 wined3d_device_context_draw_indexed(&device->cs->c, state->base_vertex_index,
4510 start_idx, index_count, start_instance, instance_count);
4513 void CDECL wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device *device,
4514 struct wined3d_buffer *buffer, unsigned int offset)
4516 TRACE("device %p, buffer %p, offset %u.\n", device, buffer, offset);
4518 wined3d_device_context_draw_indirect(&device->cs->c, buffer, offset, true);
4521 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4522 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4524 unsigned int src_size, dst_size, src_skip_levels = 0;
4525 unsigned int src_level_count, dst_level_count;
4526 const struct wined3d_dirty_regions *regions;
4527 unsigned int layer_count, level_count, i, j;
4528 enum wined3d_resource_type type;
4529 BOOL entire_texture = TRUE;
4530 struct wined3d_box box;
4532 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4534 /* Verify that the source and destination textures are non-NULL. */
4535 if (!src_texture || !dst_texture)
4537 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4538 return WINED3DERR_INVALIDCALL;
4541 if (src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU
4542 || src_texture->resource.usage & WINED3DUSAGE_SCRATCH)
4544 WARN("Source resource is GPU accessible or a scratch resource.\n");
4545 return WINED3DERR_INVALIDCALL;
4547 if (dst_texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
4549 WARN("Destination resource is CPU accessible.\n");
4550 return WINED3DERR_INVALIDCALL;
4553 /* Verify that the source and destination textures are the same type. */
4554 type = src_texture->resource.type;
4555 if (dst_texture->resource.type != type)
4557 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4558 return WINED3DERR_INVALIDCALL;
4561 layer_count = src_texture->layer_count;
4562 if (layer_count != dst_texture->layer_count)
4564 WARN("Source and destination have different layer counts.\n");
4565 return WINED3DERR_INVALIDCALL;
4568 if (src_texture->resource.format != dst_texture->resource.format)
4570 WARN("Source and destination formats do not match.\n");
4571 return WINED3DERR_INVALIDCALL;
4574 src_level_count = src_texture->level_count;
4575 dst_level_count = dst_texture->level_count;
4576 level_count = min(src_level_count, dst_level_count);
4578 src_size = max(src_texture->resource.width, src_texture->resource.height);
4579 src_size = max(src_size, src_texture->resource.depth);
4580 dst_size = max(dst_texture->resource.width, dst_texture->resource.height);
4581 dst_size = max(dst_size, dst_texture->resource.depth);
4582 while (src_size > dst_size)
4584 src_size >>= 1;
4585 ++src_skip_levels;
4588 if (wined3d_texture_get_level_width(src_texture, src_skip_levels) != dst_texture->resource.width
4589 || wined3d_texture_get_level_height(src_texture, src_skip_levels) != dst_texture->resource.height
4590 || wined3d_texture_get_level_depth(src_texture, src_skip_levels) != dst_texture->resource.depth)
4592 WARN("Source and destination dimensions do not match.\n");
4593 return WINED3DERR_INVALIDCALL;
4596 if ((regions = src_texture->dirty_regions))
4598 for (i = 0; i < layer_count && entire_texture; ++i)
4600 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4601 continue;
4603 entire_texture = FALSE;
4604 break;
4608 /* Update every surface level of the texture. */
4609 if (entire_texture)
4611 for (i = 0; i < level_count; ++i)
4613 wined3d_texture_get_level_box(dst_texture, i, &box);
4614 for (j = 0; j < layer_count; ++j)
4616 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4617 &dst_texture->resource, j * dst_level_count + i, &box,
4618 &src_texture->resource, j * src_level_count + i + src_skip_levels, &box,
4619 0, NULL, WINED3D_TEXF_POINT);
4623 else
4625 unsigned int src_level, box_count, k;
4626 const struct wined3d_box *boxes;
4627 struct wined3d_box b;
4629 for (i = 0; i < layer_count; ++i)
4631 boxes = regions[i].boxes;
4632 box_count = regions[i].box_count;
4633 if (regions[i].box_count >= WINED3D_MAX_DIRTY_REGION_COUNT)
4635 boxes = &b;
4636 box_count = 1;
4637 wined3d_texture_get_level_box(dst_texture, i, &b);
4640 for (j = 0; j < level_count; ++j)
4642 src_level = j + src_skip_levels;
4644 /* TODO: We could pass an array of boxes here to avoid
4645 * multiple context acquisitions for the same resource. */
4646 for (k = 0; k < box_count; ++k)
4648 box = boxes[k];
4649 if (src_level)
4651 box.left >>= src_level;
4652 box.top >>= src_level;
4653 box.right = min((box.right + (1u << src_level) - 1) >> src_level,
4654 wined3d_texture_get_level_width(src_texture, src_level));
4655 box.bottom = min((box.bottom + (1u << src_level) - 1) >> src_level,
4656 wined3d_texture_get_level_height(src_texture, src_level));
4657 box.front >>= src_level;
4658 box.back = min((box.back + (1u << src_level) - 1) >> src_level,
4659 wined3d_texture_get_level_depth(src_texture, src_level));
4662 wined3d_device_context_emit_blt_sub_resource(&device->cs->c,
4663 &dst_texture->resource, i * dst_level_count + j, &box,
4664 &src_texture->resource, i * src_level_count + src_level, &box,
4665 0, NULL, WINED3D_TEXF_POINT);
4671 wined3d_texture_clear_dirty_regions(src_texture);
4673 return WINED3D_OK;
4676 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4678 const struct wined3d_state *state = device->cs->c.state;
4679 struct wined3d_texture *texture;
4680 DWORD i;
4682 TRACE("device %p, num_passes %p.\n", device, num_passes);
4684 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
4686 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4688 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4689 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4691 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4693 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4694 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4697 texture = state->textures[i];
4698 if (!texture || texture->resource.format_flags & WINED3DFMT_FLAG_FILTERING) continue;
4700 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4702 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i);
4703 return E_FAIL;
4705 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4707 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i);
4708 return E_FAIL;
4710 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4711 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4713 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i);
4714 return E_FAIL;
4718 if (wined3d_state_uses_depth_buffer(state)
4719 || (state->depth_stencil_state && state->depth_stencil_state->desc.stencil))
4721 struct wined3d_rendertarget_view *rt = state->fb.render_targets[0];
4722 struct wined3d_rendertarget_view *ds = state->fb.depth_stencil;
4724 if (ds && rt && (ds->width < rt->width || ds->height < rt->height))
4726 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4727 return WINED3DERR_CONFLICTINGRENDERSTATE;
4731 /* return a sensible default */
4732 *num_passes = 1;
4734 TRACE("returning D3D_OK\n");
4735 return WINED3D_OK;
4738 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4740 static BOOL warned;
4742 TRACE("device %p, software %#x.\n", device, software);
4744 if (!warned)
4746 FIXME("device %p, software %#x stub!\n", device, software);
4747 warned = TRUE;
4750 device->softwareVertexProcessing = software;
4753 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4755 static BOOL warned;
4757 TRACE("device %p.\n", device);
4759 if (!warned)
4761 TRACE("device %p stub!\n", device);
4762 warned = TRUE;
4765 return device->softwareVertexProcessing;
4768 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4769 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4771 struct wined3d_swapchain *swapchain;
4773 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4774 device, swapchain_idx, raster_status);
4776 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4777 return WINED3DERR_INVALIDCALL;
4779 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4782 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4784 static BOOL warned;
4786 TRACE("device %p, segments %.8e.\n", device, segments);
4788 if (segments != 0.0f)
4790 if (!warned)
4792 FIXME("device %p, segments %.8e stub!\n", device, segments);
4793 warned = TRUE;
4797 return WINED3D_OK;
4800 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4802 static BOOL warned;
4804 TRACE("device %p.\n", device);
4806 if (!warned)
4808 FIXME("device %p stub!\n", device);
4809 warned = TRUE;
4812 return 0.0f;
4815 void CDECL wined3d_device_context_copy_uav_counter(struct wined3d_device_context *context,
4816 struct wined3d_buffer *dst_buffer, unsigned int offset, struct wined3d_unordered_access_view *uav)
4818 TRACE("context %p, dst_buffer %p, offset %u, uav %p.\n",
4819 context, dst_buffer, offset, uav);
4821 wined3d_device_context_emit_copy_uav_counter(context, dst_buffer, offset, uav);
4824 static bool resources_format_compatible(const struct wined3d_resource *src_resource,
4825 const struct wined3d_resource *dst_resource)
4827 if (src_resource->format->id == dst_resource->format->id)
4828 return true;
4829 if (src_resource->format->typeless_id && src_resource->format->typeless_id == dst_resource->format->typeless_id)
4830 return true;
4831 if (src_resource->device->cs->c.state->feature_level < WINED3D_FEATURE_LEVEL_10_1)
4832 return false;
4833 if ((src_resource->format_flags & WINED3DFMT_FLAG_BLOCKS)
4834 && (dst_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK))
4835 return src_resource->format->block_byte_count == dst_resource->format->byte_count;
4836 if ((src_resource->format_flags & WINED3DFMT_FLAG_CAST_TO_BLOCK)
4837 && (dst_resource->format_flags & WINED3DFMT_FLAG_BLOCKS))
4838 return src_resource->format->byte_count == dst_resource->format->block_byte_count;
4839 return false;
4842 void CDECL wined3d_device_context_copy_resource(struct wined3d_device_context *context,
4843 struct wined3d_resource *dst_resource, struct wined3d_resource *src_resource)
4845 unsigned int src_row_block_count, dst_row_block_count;
4846 struct wined3d_texture *dst_texture, *src_texture;
4847 unsigned int src_row_count, dst_row_count;
4848 struct wined3d_box src_box, dst_box;
4849 unsigned int i, j;
4851 TRACE("context %p, dst_resource %p, src_resource %p.\n", context, dst_resource, src_resource);
4853 if (src_resource == dst_resource)
4855 WARN("Source and destination are the same resource.\n");
4856 return;
4859 if (src_resource->type != dst_resource->type)
4861 WARN("Resource types (%s / %s) don't match.\n",
4862 debug_d3dresourcetype(dst_resource->type),
4863 debug_d3dresourcetype(src_resource->type));
4864 return;
4867 if (!resources_format_compatible(src_resource, dst_resource))
4869 WARN("Resource formats %s and %s are incompatible.\n",
4870 debug_d3dformat(dst_resource->format->id),
4871 debug_d3dformat(src_resource->format->id));
4872 return;
4875 src_row_block_count = (src_resource->width + (src_resource->format->block_width - 1))
4876 / src_resource->format->block_width;
4877 dst_row_block_count = (dst_resource->width + (dst_resource->format->block_width - 1))
4878 / dst_resource->format->block_width;
4879 src_row_count = (src_resource->height + (src_resource->format->block_height - 1))
4880 / src_resource->format->block_height;
4881 dst_row_count = (dst_resource->height + (dst_resource->format->block_height - 1))
4882 / dst_resource->format->block_height;
4884 if (src_row_block_count != dst_row_block_count || src_row_count != dst_row_count
4885 || src_resource->depth != dst_resource->depth)
4887 WARN("Resource block dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4888 dst_row_block_count, dst_row_count, dst_resource->depth,
4889 src_row_block_count, src_row_count, src_resource->depth);
4890 return;
4893 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4895 wined3d_box_set(&src_box, 0, 0, src_resource->size, 1, 0, 1);
4896 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, 0, &src_box,
4897 src_resource, 0, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4898 return;
4901 dst_texture = texture_from_resource(dst_resource);
4902 src_texture = texture_from_resource(src_resource);
4904 if (src_texture->layer_count != dst_texture->layer_count
4905 || src_texture->level_count != dst_texture->level_count)
4907 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4908 dst_texture->layer_count, dst_texture->level_count,
4909 src_texture->layer_count, src_texture->level_count);
4910 return;
4913 for (i = 0; i < dst_texture->level_count; ++i)
4915 wined3d_texture_get_level_box(src_texture, i, &src_box);
4916 wined3d_texture_get_level_box(dst_texture, i, &dst_box);
4917 for (j = 0; j < dst_texture->layer_count; ++j)
4919 unsigned int idx = j * dst_texture->level_count + i;
4921 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, idx, &dst_box,
4922 src_resource, idx, &src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
4927 HRESULT CDECL wined3d_device_context_copy_sub_resource_region(struct wined3d_device_context *context,
4928 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx, unsigned int dst_x,
4929 unsigned int dst_y, unsigned int dst_z, struct wined3d_resource *src_resource,
4930 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box, unsigned int flags)
4932 struct wined3d_box dst_box, b;
4934 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4935 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4936 context, dst_resource, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4937 src_resource, src_sub_resource_idx, debug_box(src_box), flags);
4939 if (flags)
4940 FIXME("Ignoring flags %#x.\n", flags);
4942 if (src_resource == dst_resource && src_sub_resource_idx == dst_sub_resource_idx)
4944 WARN("Source and destination are the same sub-resource.\n");
4945 return WINED3DERR_INVALIDCALL;
4948 if (!resources_format_compatible(src_resource, dst_resource))
4950 WARN("Resource formats %s and %s are incompatible.\n",
4951 debug_d3dformat(dst_resource->format->id),
4952 debug_d3dformat(src_resource->format->id));
4953 return WINED3DERR_INVALIDCALL;
4956 if (dst_resource->type == WINED3D_RTYPE_BUFFER)
4958 if (src_resource->type != WINED3D_RTYPE_BUFFER)
4960 WARN("Resource types (%s / %s) don't match.\n",
4961 debug_d3dresourcetype(dst_resource->type),
4962 debug_d3dresourcetype(src_resource->type));
4963 return WINED3DERR_INVALIDCALL;
4966 if (dst_sub_resource_idx)
4968 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx);
4969 return WINED3DERR_INVALIDCALL;
4972 if (src_sub_resource_idx)
4974 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx);
4975 return WINED3DERR_INVALIDCALL;
4978 if (!src_box)
4980 unsigned int dst_w;
4982 dst_w = dst_resource->size - dst_x;
4983 wined3d_box_set(&b, 0, 0, min(src_resource->size, dst_w), 1, 0, 1);
4984 src_box = &b;
4986 else if ((src_box->left >= src_box->right
4987 || src_box->top >= src_box->bottom
4988 || src_box->front >= src_box->back))
4990 WARN("Invalid box %s specified.\n", debug_box(src_box));
4991 return WINED3DERR_INVALIDCALL;
4994 if (src_box->right > src_resource->size || dst_x >= dst_resource->size
4995 || src_box->right - src_box->left > dst_resource->size - dst_x)
4997 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4998 dst_x, src_box->left, src_box->right - src_box->left);
4999 return WINED3DERR_INVALIDCALL;
5002 wined3d_box_set(&dst_box, dst_x, 0, dst_x + (src_box->right - src_box->left), 1, 0, 1);
5004 else
5006 struct wined3d_texture *dst_texture = texture_from_resource(dst_resource);
5007 struct wined3d_texture *src_texture = texture_from_resource(src_resource);
5008 unsigned int src_level = src_sub_resource_idx % src_texture->level_count;
5009 unsigned int src_row_block_count, src_row_count;
5011 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
5013 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx);
5014 return WINED3DERR_INVALIDCALL;
5017 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count)
5019 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx);
5020 return WINED3DERR_INVALIDCALL;
5023 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
5025 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx);
5026 return WINED3DERR_INVALIDCALL;
5029 if (src_texture->sub_resources[src_sub_resource_idx].map_count)
5031 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx);
5032 return WINED3DERR_INVALIDCALL;
5035 if (!src_box)
5037 unsigned int src_w, src_h, src_d, dst_w, dst_h, dst_d, dst_level;
5039 src_w = wined3d_texture_get_level_width(src_texture, src_level);
5040 src_h = wined3d_texture_get_level_height(src_texture, src_level);
5041 src_d = wined3d_texture_get_level_depth(src_texture, src_level);
5043 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5044 dst_w = wined3d_texture_get_level_width(dst_texture, dst_level) - dst_x;
5045 dst_h = wined3d_texture_get_level_height(dst_texture, dst_level) - dst_y;
5046 dst_d = wined3d_texture_get_level_depth(dst_texture, dst_level) - dst_z;
5048 wined3d_box_set(&b, 0, 0, min(src_w, dst_w), min(src_h, dst_h), 0, min(src_d, dst_d));
5049 src_box = &b;
5051 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture, src_level, src_box)))
5053 WARN("Invalid source box %s.\n", debug_box(src_box));
5054 return WINED3DERR_INVALIDCALL;
5057 if (src_resource->format->block_width == dst_resource->format->block_width
5058 && src_resource->format->block_height == dst_resource->format->block_height)
5060 wined3d_box_set(&dst_box, dst_x, dst_y, dst_x + (src_box->right - src_box->left),
5061 dst_y + (src_box->bottom - src_box->top), dst_z, dst_z + (src_box->back - src_box->front));
5063 else
5065 src_row_block_count = (src_box->right - src_box->left + src_resource->format->block_width - 1)
5066 / src_resource->format->block_width;
5067 src_row_count = (src_box->bottom - src_box->top + src_resource->format->block_height - 1)
5068 / src_resource->format->block_height;
5069 wined3d_box_set(&dst_box, dst_x, dst_y,
5070 dst_x + (src_row_block_count * dst_resource->format->block_width),
5071 dst_y + (src_row_count * dst_resource->format->block_height),
5072 dst_z, dst_z + (src_box->back - src_box->front));
5074 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture,
5075 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
5077 WARN("Invalid destination box %s.\n", debug_box(&dst_box));
5078 return WINED3DERR_INVALIDCALL;
5082 wined3d_device_context_emit_blt_sub_resource(context, dst_resource, dst_sub_resource_idx, &dst_box,
5083 src_resource, src_sub_resource_idx, src_box, WINED3D_BLT_RAW, NULL, WINED3D_TEXF_POINT);
5085 return WINED3D_OK;
5088 void CDECL wined3d_device_update_sub_resource(struct wined3d_device *device, struct wined3d_resource *resource,
5089 unsigned int sub_resource_idx, const struct wined3d_box *box, const void *data, unsigned int row_pitch,
5090 unsigned int depth_pitch, unsigned int flags)
5092 unsigned int width, height, depth;
5093 struct wined3d_box b;
5095 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, "
5096 "flags %#x.\n",
5097 device, resource, sub_resource_idx, debug_box(box), data, row_pitch, depth_pitch, flags);
5099 if (flags)
5100 FIXME("Ignoring flags %#x.\n", flags);
5102 if (!(resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5104 WARN("Resource %p is not GPU accessible.\n", resource);
5105 return;
5108 if (resource->type == WINED3D_RTYPE_BUFFER)
5110 if (sub_resource_idx > 0)
5112 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
5113 return;
5116 width = resource->size;
5117 height = 1;
5118 depth = 1;
5120 else
5122 struct wined3d_texture *texture = texture_from_resource(resource);
5123 unsigned int level;
5125 if (sub_resource_idx >= texture->level_count * texture->layer_count)
5127 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx);
5128 return;
5131 level = sub_resource_idx % texture->level_count;
5132 width = wined3d_texture_get_level_width(texture, level);
5133 height = wined3d_texture_get_level_height(texture, level);
5134 depth = wined3d_texture_get_level_depth(texture, level);
5137 if (!box)
5139 wined3d_box_set(&b, 0, 0, width, height, 0, depth);
5140 box = &b;
5142 else if (box->left >= box->right || box->right > width
5143 || box->top >= box->bottom || box->bottom > height
5144 || box->front >= box->back || box->back > depth)
5146 WARN("Invalid box %s specified.\n", debug_box(box));
5147 return;
5150 wined3d_resource_wait_idle(resource);
5152 wined3d_cs_emit_update_sub_resource(device->cs, resource, sub_resource_idx, box, data, row_pitch, depth_pitch);
5155 void CDECL wined3d_device_context_resolve_sub_resource(struct wined3d_device_context *context,
5156 struct wined3d_resource *dst_resource, unsigned int dst_sub_resource_idx,
5157 struct wined3d_resource *src_resource, unsigned int src_sub_resource_idx,
5158 enum wined3d_format_id format_id)
5160 struct wined3d_texture *dst_texture, *src_texture;
5161 unsigned int dst_level, src_level;
5162 struct wined3d_blt_fx fx = {0};
5163 RECT dst_rect, src_rect;
5165 TRACE("context %p, dst_resource %p, dst_sub_resource_idx %u, "
5166 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
5167 context, dst_resource, dst_sub_resource_idx,
5168 src_resource, src_sub_resource_idx, debug_d3dformat(format_id));
5170 if (wined3d_format_is_typeless(dst_resource->format)
5171 || wined3d_format_is_typeless(src_resource->format))
5173 FIXME("Multisample resolve is not fully supported for typeless formats "
5174 "(dst_format %s, src_format %s, format %s).\n",
5175 debug_d3dformat(dst_resource->format->id), debug_d3dformat(src_resource->format->id),
5176 debug_d3dformat(format_id));
5178 if (dst_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5180 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource->type));
5181 return;
5183 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5185 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource->type));
5186 return;
5189 fx.resolve_format_id = format_id;
5191 dst_texture = texture_from_resource(dst_resource);
5192 src_texture = texture_from_resource(src_resource);
5194 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5195 SetRect(&dst_rect, 0, 0, wined3d_texture_get_level_width(dst_texture, dst_level),
5196 wined3d_texture_get_level_height(dst_texture, dst_level));
5197 src_level = src_sub_resource_idx % src_texture->level_count;
5198 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(src_texture, src_level),
5199 wined3d_texture_get_level_height(src_texture, src_level));
5200 wined3d_device_context_blt(context, dst_texture, dst_sub_resource_idx, &dst_rect,
5201 src_texture, src_sub_resource_idx, &src_rect, 0, &fx, WINED3D_TEXF_POINT);
5204 HRESULT CDECL wined3d_device_context_clear_rendertarget_view(struct wined3d_device_context *context,
5205 struct wined3d_rendertarget_view *view, const RECT *rect, unsigned int flags,
5206 const struct wined3d_color *color, float depth, unsigned int stencil)
5208 struct wined3d_resource *resource;
5209 RECT r;
5211 TRACE("context %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
5212 context, view, wine_dbgstr_rect(rect), flags, debug_color(color), depth, stencil);
5214 if (!flags)
5215 return WINED3D_OK;
5217 resource = view->resource;
5218 if (resource->type == WINED3D_RTYPE_BUFFER)
5220 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
5221 return WINED3DERR_INVALIDCALL;
5224 if (!rect)
5226 SetRect(&r, 0, 0, view->width, view->height);
5227 rect = &r;
5229 else
5231 struct wined3d_box b = {rect->left, rect->top, rect->right, rect->bottom, 0, 1};
5232 struct wined3d_texture *texture = texture_from_resource(view->resource);
5233 HRESULT hr;
5235 if (FAILED(hr = wined3d_texture_check_box_dimensions(texture,
5236 view->sub_resource_idx % texture->level_count, &b)))
5237 return hr;
5240 wined3d_device_context_emit_clear_rendertarget_view(context, view, rect, flags, color, depth, stencil);
5242 return WINED3D_OK;
5245 void CDECL wined3d_device_clear_unordered_access_view_uint(struct wined3d_device *device,
5246 struct wined3d_unordered_access_view *view, const struct wined3d_uvec4 *clear_value)
5248 TRACE("device %p, view %p, clear_value %s.\n", device, view, debug_uvec4(clear_value));
5250 wined3d_device_context_emit_clear_uav_uint(&device->cs->c, view, clear_value);
5253 struct wined3d_rendertarget_view * CDECL wined3d_device_get_rendertarget_view(const struct wined3d_device *device,
5254 unsigned int view_idx)
5256 unsigned int max_rt_count;
5258 TRACE("device %p, view_idx %u.\n", device, view_idx);
5260 max_rt_count = device->adapter->d3d_info.limits.max_rt_count;
5261 if (view_idx >= max_rt_count)
5263 WARN("Only %u render targets are supported.\n", max_rt_count);
5264 return NULL;
5267 return device->cs->c.state->fb.render_targets[view_idx];
5270 struct wined3d_rendertarget_view * CDECL wined3d_device_get_depth_stencil_view(const struct wined3d_device *device)
5272 TRACE("device %p.\n", device);
5274 return device->cs->c.state->fb.depth_stencil;
5277 HRESULT CDECL wined3d_device_set_rendertarget_view(struct wined3d_device *device,
5278 unsigned int view_idx, struct wined3d_rendertarget_view *view, BOOL set_viewport)
5280 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
5281 device, view_idx, view, set_viewport);
5283 return wined3d_device_context_set_rendertarget_view(&device->cs->c, view_idx, view, set_viewport);
5286 HRESULT CDECL wined3d_device_set_depth_stencil_view(struct wined3d_device *device,
5287 struct wined3d_rendertarget_view *view)
5289 TRACE("device %p, view %p.\n", device, view);
5291 return wined3d_device_context_set_depth_stencil_view(&device->cs->c, view);
5294 void CDECL wined3d_device_context_generate_mipmaps(struct wined3d_device_context *context,
5295 struct wined3d_shader_resource_view *view)
5297 struct wined3d_texture *texture;
5299 TRACE("context %p, view %p.\n", context, view);
5301 if (view->resource->type == WINED3D_RTYPE_BUFFER)
5303 WARN("Called on buffer resource %p.\n", view->resource);
5304 return;
5307 texture = texture_from_resource(view->resource);
5308 if (!(texture->flags & WINED3D_TEXTURE_GENERATE_MIPMAPS))
5310 WARN("Texture without the WINED3D_TEXTURE_GENERATE_MIPMAPS flag, ignoring.\n");
5311 return;
5314 wined3d_device_context_emit_generate_mipmaps(context, view);
5317 static struct wined3d_texture *wined3d_device_create_cursor_texture(struct wined3d_device *device,
5318 struct wined3d_texture *cursor_image, unsigned int sub_resource_idx)
5320 unsigned int texture_level = sub_resource_idx % cursor_image->level_count;
5321 struct wined3d_sub_resource_data data;
5322 struct wined3d_resource_desc desc;
5323 struct wined3d_map_desc map_desc;
5324 struct wined3d_texture *texture;
5325 HRESULT hr;
5327 if (FAILED(wined3d_resource_map(&cursor_image->resource, sub_resource_idx, &map_desc, NULL, WINED3D_MAP_READ)))
5329 ERR("Failed to map source texture.\n");
5330 return NULL;
5333 data.data = map_desc.data;
5334 data.row_pitch = map_desc.row_pitch;
5335 data.slice_pitch = map_desc.slice_pitch;
5337 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5338 desc.format = WINED3DFMT_B8G8R8A8_UNORM;
5339 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5340 desc.multisample_quality = 0;
5341 desc.usage = WINED3DUSAGE_DYNAMIC;
5342 desc.bind_flags = 0;
5343 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5344 desc.width = wined3d_texture_get_level_width(cursor_image, texture_level);
5345 desc.height = wined3d_texture_get_level_height(cursor_image, texture_level);
5346 desc.depth = 1;
5347 desc.size = 0;
5349 hr = wined3d_texture_create(device, &desc, 1, 1, 0, &data, NULL, &wined3d_null_parent_ops, &texture);
5350 wined3d_resource_unmap(&cursor_image->resource, sub_resource_idx);
5351 if (FAILED(hr))
5353 ERR("Failed to create cursor texture.\n");
5354 return NULL;
5357 return texture;
5360 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
5361 UINT x_hotspot, UINT y_hotspot, struct wined3d_texture *texture, unsigned int sub_resource_idx)
5363 unsigned int texture_level = sub_resource_idx % texture->level_count;
5364 unsigned int cursor_width, cursor_height;
5365 struct wined3d_map_desc map_desc;
5367 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5368 device, x_hotspot, y_hotspot, texture, sub_resource_idx);
5370 if (sub_resource_idx >= texture->level_count * texture->layer_count
5371 || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
5372 return WINED3DERR_INVALIDCALL;
5374 if (device->cursor_texture)
5376 wined3d_texture_decref(device->cursor_texture);
5377 device->cursor_texture = NULL;
5380 if (texture->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
5382 WARN("Texture %p has invalid format %s.\n",
5383 texture, debug_d3dformat(texture->resource.format->id));
5384 return WINED3DERR_INVALIDCALL;
5387 /* Cursor width and height must all be powers of two */
5388 cursor_width = wined3d_texture_get_level_width(texture, texture_level);
5389 cursor_height = wined3d_texture_get_level_height(texture, texture_level);
5390 if ((cursor_width & (cursor_width - 1)) || (cursor_height & (cursor_height - 1)))
5392 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width, cursor_height);
5393 return WINED3DERR_INVALIDCALL;
5396 /* Do not store the surface's pointer because the application may
5397 * release it after setting the cursor image. Windows doesn't
5398 * addref the set surface, so we can't do this either without
5399 * creating circular refcount dependencies. */
5400 if (!(device->cursor_texture = wined3d_device_create_cursor_texture(device, texture, sub_resource_idx)))
5402 ERR("Failed to create cursor texture.\n");
5403 return WINED3DERR_INVALIDCALL;
5406 if (cursor_width == 32 && cursor_height == 32)
5408 UINT mask_size = cursor_width * cursor_height / 8;
5409 ICONINFO cursor_info;
5410 DWORD *mask_bits;
5411 HCURSOR cursor;
5413 /* 32-bit user32 cursors ignore the alpha channel if it's all
5414 * zeroes, and use the mask instead. Fill the mask with all ones
5415 * to ensure we still get a fully transparent cursor. */
5416 if (!(mask_bits = heap_alloc(mask_size)))
5417 return E_OUTOFMEMORY;
5418 memset(mask_bits, 0xff, mask_size);
5420 wined3d_resource_map(&texture->resource, sub_resource_idx, &map_desc, NULL,
5421 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READ);
5422 cursor_info.fIcon = FALSE;
5423 cursor_info.xHotspot = x_hotspot;
5424 cursor_info.yHotspot = y_hotspot;
5425 cursor_info.hbmMask = CreateBitmap(cursor_width, cursor_height, 1, 1, mask_bits);
5426 cursor_info.hbmColor = CreateBitmap(cursor_width, cursor_height, 1, 32, map_desc.data);
5427 wined3d_resource_unmap(&texture->resource, sub_resource_idx);
5429 /* Create our cursor and clean up. */
5430 cursor = CreateIconIndirect(&cursor_info);
5431 if (cursor_info.hbmMask)
5432 DeleteObject(cursor_info.hbmMask);
5433 if (cursor_info.hbmColor)
5434 DeleteObject(cursor_info.hbmColor);
5435 if (device->hardwareCursor)
5436 DestroyCursor(device->hardwareCursor);
5437 device->hardwareCursor = cursor;
5438 if (device->bCursorVisible)
5439 SetCursor(cursor);
5441 heap_free(mask_bits);
5444 TRACE("New cursor dimensions are %ux%u.\n", cursor_width, cursor_height);
5445 device->cursorWidth = cursor_width;
5446 device->cursorHeight = cursor_height;
5447 device->xHotSpot = x_hotspot;
5448 device->yHotSpot = y_hotspot;
5450 return WINED3D_OK;
5453 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5454 int x_screen_space, int y_screen_space, DWORD flags)
5456 TRACE("device %p, x %d, y %d, flags %#x.\n",
5457 device, x_screen_space, y_screen_space, flags);
5459 device->xScreenSpace = x_screen_space;
5460 device->yScreenSpace = y_screen_space;
5462 if (device->hardwareCursor)
5464 POINT pt;
5466 GetCursorPos( &pt );
5467 if (x_screen_space == pt.x && y_screen_space == pt.y)
5468 return;
5469 SetCursorPos( x_screen_space, y_screen_space );
5471 /* Switch to the software cursor if position diverges from the hardware one. */
5472 GetCursorPos( &pt );
5473 if (x_screen_space != pt.x || y_screen_space != pt.y)
5475 if (device->bCursorVisible) SetCursor( NULL );
5476 DestroyCursor( device->hardwareCursor );
5477 device->hardwareCursor = 0;
5482 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5484 BOOL oldVisible = device->bCursorVisible;
5486 TRACE("device %p, show %#x.\n", device, show);
5489 * When ShowCursor is first called it should make the cursor appear at the OS's last
5490 * known cursor position.
5492 if (show && !oldVisible)
5494 POINT pt;
5495 GetCursorPos(&pt);
5496 device->xScreenSpace = pt.x;
5497 device->yScreenSpace = pt.y;
5500 if (device->hardwareCursor)
5502 device->bCursorVisible = show;
5503 if (show)
5504 SetCursor(device->hardwareCursor);
5505 else
5506 SetCursor(NULL);
5508 else if (device->cursor_texture)
5510 device->bCursorVisible = show;
5513 return oldVisible;
5516 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5518 struct wined3d_resource *resource, *cursor;
5520 TRACE("device %p.\n", device);
5522 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5524 TRACE("Checking resource %p for eviction.\n", resource);
5526 if (wined3d_resource_access_is_managed(resource->access) && !resource->map_count)
5528 TRACE("Evicting %p.\n", resource);
5529 wined3d_cs_emit_unload_resource(device->cs, resource);
5534 void CDECL wined3d_device_flush(struct wined3d_device *device)
5536 TRACE("device %p.\n", device);
5538 wined3d_cs_emit_flush(device->cs);
5541 static void update_swapchain_flags(struct wined3d_texture *texture)
5543 unsigned int flags = texture->swapchain->state.desc.flags;
5545 if (flags & WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER)
5546 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
5547 else
5548 texture->resource.access &= ~(WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W);
5550 if (flags & WINED3D_SWAPCHAIN_GDI_COMPATIBLE)
5551 texture->flags |= WINED3D_TEXTURE_GET_DC;
5552 else
5553 texture->flags &= ~WINED3D_TEXTURE_GET_DC;
5556 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5557 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5558 wined3d_device_reset_cb callback, BOOL reset_state)
5560 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
5561 struct wined3d_swapchain_state *swapchain_state;
5562 struct wined3d_swapchain_desc *current_desc;
5563 struct wined3d_state *state = device->cs->c.state;
5564 struct wined3d_resource *resource, *cursor;
5565 struct wined3d_rendertarget_view *view;
5566 struct wined3d_swapchain *swapchain;
5567 struct wined3d_view_desc view_desc;
5568 BOOL backbuffer_resized, windowed;
5569 HRESULT hr = WINED3D_OK;
5570 unsigned int i;
5572 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5573 device, swapchain_desc, mode, callback, reset_state);
5575 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
5577 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5579 ERR("Failed to get the first implicit swapchain.\n");
5580 return WINED3DERR_INVALIDCALL;
5582 swapchain_state = &swapchain->state;
5583 current_desc = &swapchain_state->desc;
5585 if (reset_state)
5587 if (device->logo_texture)
5589 wined3d_texture_decref(device->logo_texture);
5590 device->logo_texture = NULL;
5592 if (device->cursor_texture)
5594 wined3d_texture_decref(device->cursor_texture);
5595 device->cursor_texture = NULL;
5597 state_unbind_resources(state);
5600 for (i = 0; i < d3d_info->limits.max_rt_count; ++i)
5602 wined3d_device_set_rendertarget_view(device, i, NULL, FALSE);
5604 wined3d_device_set_depth_stencil_view(device, NULL);
5606 if (reset_state)
5608 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5610 TRACE("Enumerating resource %p.\n", resource);
5611 if (FAILED(hr = callback(resource)))
5612 return hr;
5616 TRACE("New params:\n");
5617 TRACE("output %p\n", swapchain_desc->output);
5618 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5619 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5620 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5621 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5622 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5623 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5624 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5625 TRACE("device_window %p\n", swapchain_desc->device_window);
5626 TRACE("windowed %#x\n", swapchain_desc->windowed);
5627 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5628 if (swapchain_desc->enable_auto_depth_stencil)
5629 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5630 TRACE("flags %#x\n", swapchain_desc->flags);
5631 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5632 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5634 if (swapchain_desc->backbuffer_bind_flags && swapchain_desc->backbuffer_bind_flags != WINED3D_BIND_RENDER_TARGET)
5635 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc->backbuffer_bind_flags);
5637 if (swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_DISCARD
5638 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_SEQUENTIAL
5639 && swapchain_desc->swap_effect != WINED3D_SWAP_EFFECT_COPY)
5640 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc->swap_effect);
5642 /* No special treatment of these parameters. Just store them */
5643 current_desc->swap_effect = swapchain_desc->swap_effect;
5644 current_desc->enable_auto_depth_stencil = swapchain_desc->enable_auto_depth_stencil;
5645 current_desc->auto_depth_stencil_format = swapchain_desc->auto_depth_stencil_format;
5646 current_desc->refresh_rate = swapchain_desc->refresh_rate;
5647 current_desc->auto_restore_display_mode = swapchain_desc->auto_restore_display_mode;
5649 if (swapchain_desc->device_window && swapchain_desc->device_window != current_desc->device_window)
5651 TRACE("Changing the device window from %p to %p.\n",
5652 current_desc->device_window, swapchain_desc->device_window);
5653 current_desc->device_window = swapchain_desc->device_window;
5654 swapchain_state->device_window = swapchain_desc->device_window;
5655 wined3d_swapchain_set_window(swapchain, NULL);
5658 backbuffer_resized = swapchain_desc->backbuffer_width != current_desc->backbuffer_width
5659 || swapchain_desc->backbuffer_height != current_desc->backbuffer_height;
5660 windowed = current_desc->windowed;
5662 if (!swapchain_desc->windowed != !windowed || swapchain->reapply_mode
5663 || mode || (!swapchain_desc->windowed && backbuffer_resized))
5665 /* Switch from windowed to fullscreen. */
5666 if (windowed && !swapchain_desc->windowed)
5668 HWND focus_window = device->create_parms.focus_window;
5670 if (!focus_window)
5671 focus_window = swapchain->state.device_window;
5672 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5674 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5675 return hr;
5679 if (FAILED(hr = wined3d_swapchain_state_set_fullscreen(&swapchain->state,
5680 swapchain_desc, mode)))
5681 return hr;
5683 /* Switch from fullscreen to windowed. */
5684 if (!windowed && swapchain_desc->windowed)
5685 wined3d_device_release_focus_window(device);
5687 else if (!swapchain_desc->windowed)
5689 DWORD style = swapchain_state->style;
5690 DWORD exstyle = swapchain_state->exstyle;
5691 struct wined3d_output_desc output_desc;
5693 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5694 * the window back into the right position. Some applications
5695 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5696 * up their mess. Guild Wars also loses the device during that. */
5697 if (FAILED(hr = wined3d_output_get_desc(swapchain_desc->output, &output_desc)))
5699 ERR("Failed to get output description, hr %#x.\n", hr);
5700 return hr;
5703 swapchain_state->style = 0;
5704 swapchain_state->exstyle = 0;
5705 wined3d_swapchain_state_setup_fullscreen(swapchain_state, swapchain_state->device_window,
5706 output_desc.desktop_rect.left, output_desc.desktop_rect.top,
5707 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height);
5708 swapchain_state->style = style;
5709 swapchain_state->exstyle = exstyle;
5712 if (FAILED(hr = wined3d_swapchain_resize_buffers(swapchain, swapchain_desc->backbuffer_count,
5713 swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height, swapchain_desc->backbuffer_format,
5714 swapchain_desc->multisample_type, swapchain_desc->multisample_quality)))
5715 return hr;
5717 if (swapchain_desc->flags != current_desc->flags)
5719 current_desc->flags = swapchain_desc->flags;
5721 update_swapchain_flags(swapchain->front_buffer);
5722 for (i = 0; i < current_desc->backbuffer_count; ++i)
5724 update_swapchain_flags(swapchain->back_buffers[i]);
5728 if ((view = device->auto_depth_stencil_view))
5730 device->auto_depth_stencil_view = NULL;
5731 wined3d_rendertarget_view_decref(view);
5733 if (current_desc->enable_auto_depth_stencil)
5735 struct wined3d_resource_desc texture_desc;
5736 struct wined3d_texture *texture;
5738 TRACE("Creating the depth stencil buffer.\n");
5740 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5741 texture_desc.format = current_desc->auto_depth_stencil_format;
5742 texture_desc.multisample_type = current_desc->multisample_type;
5743 texture_desc.multisample_quality = current_desc->multisample_quality;
5744 texture_desc.usage = 0;
5745 texture_desc.bind_flags = WINED3D_BIND_DEPTH_STENCIL;
5746 texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5747 texture_desc.width = current_desc->backbuffer_width;
5748 texture_desc.height = current_desc->backbuffer_height;
5749 texture_desc.depth = 1;
5750 texture_desc.size = 0;
5752 if (FAILED(hr = device->device_parent->ops->create_swapchain_texture(device->device_parent,
5753 device->device_parent, &texture_desc, 0, &texture)))
5755 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr);
5756 return WINED3DERR_INVALIDCALL;
5759 view_desc.format_id = texture->resource.format->id;
5760 view_desc.flags = 0;
5761 view_desc.u.texture.level_idx = 0;
5762 view_desc.u.texture.level_count = 1;
5763 view_desc.u.texture.layer_idx = 0;
5764 view_desc.u.texture.layer_count = 1;
5765 hr = wined3d_rendertarget_view_create(&view_desc, &texture->resource,
5766 NULL, &wined3d_null_parent_ops, &device->auto_depth_stencil_view);
5767 wined3d_texture_decref(texture);
5768 if (FAILED(hr))
5770 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5771 return hr;
5775 if ((view = device->back_buffer_view))
5777 device->back_buffer_view = NULL;
5778 wined3d_rendertarget_view_decref(view);
5780 if (current_desc->backbuffer_count && current_desc->backbuffer_bind_flags & WINED3D_BIND_RENDER_TARGET)
5782 struct wined3d_resource *back_buffer = &swapchain->back_buffers[0]->resource;
5784 view_desc.format_id = back_buffer->format->id;
5785 view_desc.flags = 0;
5786 view_desc.u.texture.level_idx = 0;
5787 view_desc.u.texture.level_count = 1;
5788 view_desc.u.texture.layer_idx = 0;
5789 view_desc.u.texture.layer_count = 1;
5790 if (FAILED(hr = wined3d_rendertarget_view_create(&view_desc, back_buffer,
5791 NULL, &wined3d_null_parent_ops, &device->back_buffer_view)))
5793 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
5794 return hr;
5798 wine_rb_clear(&device->samplers, device_free_sampler, NULL);
5799 wine_rb_clear(&device->rasterizer_states, device_free_rasterizer_state, NULL);
5800 wine_rb_clear(&device->blend_states, device_free_blend_state, NULL);
5801 wine_rb_clear(&device->depth_stencil_states, device_free_depth_stencil_state, NULL);
5803 if (reset_state)
5805 TRACE("Resetting state.\n");
5806 wined3d_cs_emit_reset_state(device->cs);
5807 state_cleanup(state);
5809 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5811 TRACE("Unloading resource %p.\n", resource);
5812 wined3d_cs_emit_unload_resource(device->cs, resource);
5815 device->adapter->adapter_ops->adapter_uninit_3d(device);
5817 wined3d_state_reset(state, &device->adapter->d3d_info);
5819 device_init_swapchain_state(device, swapchain);
5820 if (wined3d_settings.logo)
5821 device_load_logo(device, wined3d_settings.logo);
5823 else
5825 if ((view = device->back_buffer_view))
5826 wined3d_device_set_rendertarget_view(device, 0, view, FALSE);
5827 if ((view = device->auto_depth_stencil_view))
5828 wined3d_device_set_depth_stencil_view(device, view);
5831 if (reset_state)
5832 hr = device->adapter->adapter_ops->adapter_init_3d(device);
5834 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5835 * first use
5837 return hr;
5840 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5842 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5844 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5846 return WINED3D_OK;
5850 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5851 struct wined3d_device_creation_parameters *parameters)
5853 TRACE("device %p, parameters %p.\n", device, parameters);
5855 *parameters = device->create_parms;
5858 struct wined3d * CDECL wined3d_device_get_wined3d(const struct wined3d_device *device)
5860 TRACE("device %p.\n", device);
5862 return device->wined3d;
5865 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5866 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5868 struct wined3d_swapchain *swapchain;
5870 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5871 device, swapchain_idx, flags, ramp);
5873 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5874 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5877 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5878 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5880 struct wined3d_swapchain *swapchain;
5882 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5883 device, swapchain_idx, ramp);
5885 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5886 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5889 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5891 TRACE("device %p, resource %p.\n", device, resource);
5893 wined3d_not_from_cs(device->cs);
5895 list_add_head(&device->resources, &resource->resource_list_entry);
5898 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5900 TRACE("device %p, resource %p.\n", device, resource);
5902 wined3d_not_from_cs(device->cs);
5904 list_remove(&resource->resource_list_entry);
5907 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5909 enum wined3d_resource_type type = resource->type;
5910 struct wined3d_state *state = device->cs->c.state;
5911 struct wined3d_rendertarget_view *rtv;
5912 unsigned int i;
5914 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5916 for (i = 0; i < ARRAY_SIZE(state->fb.render_targets); ++i)
5918 if ((rtv = state->fb.render_targets[i]) && rtv->resource == resource)
5919 ERR("Resource %p is still in use as render target %u.\n", resource, i);
5922 if ((rtv = state->fb.depth_stencil) && rtv->resource == resource)
5923 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource);
5925 switch (type)
5927 case WINED3D_RTYPE_TEXTURE_1D:
5928 case WINED3D_RTYPE_TEXTURE_2D:
5929 case WINED3D_RTYPE_TEXTURE_3D:
5930 for (i = 0; i < WINED3D_MAX_COMBINED_SAMPLERS; ++i)
5932 if (&state->textures[i]->resource == resource)
5934 ERR("Texture resource %p is still in use, stage %u.\n", resource, i);
5935 state->textures[i] = NULL;
5938 break;
5940 case WINED3D_RTYPE_BUFFER:
5941 for (i = 0; i < WINED3D_MAX_STREAMS; ++i)
5943 if (&state->streams[i].buffer->resource == resource)
5945 ERR("Buffer resource %p is still in use, stream %u.\n", resource, i);
5946 state->streams[i].buffer = NULL;
5950 if (&state->index_buffer->resource == resource)
5952 ERR("Buffer resource %p is still in use as index buffer.\n", resource);
5953 state->index_buffer = NULL;
5955 break;
5957 default:
5958 break;
5961 /* Remove the resource from the resourceStore */
5962 device_resource_remove(device, resource);
5964 TRACE("Resource released.\n");
5967 static int wined3d_so_desc_compare(const void *key, const struct wine_rb_entry *entry)
5969 const struct wined3d_stream_output_desc *desc = &WINE_RB_ENTRY_VALUE(entry,
5970 struct wined3d_so_desc_entry, entry)->desc;
5971 const struct wined3d_stream_output_desc *k = key;
5972 unsigned int i;
5973 int ret;
5975 if ((ret = (k->element_count - desc->element_count)))
5976 return ret;
5977 if ((ret = (k->buffer_stride_count - desc->buffer_stride_count)))
5978 return ret;
5979 if ((ret = (k->rasterizer_stream_idx - desc->rasterizer_stream_idx)))
5980 return ret;
5982 for (i = 0; i < k->element_count; ++i)
5984 const struct wined3d_stream_output_element *b = &desc->elements[i];
5985 const struct wined3d_stream_output_element *a = &k->elements[i];
5987 if ((ret = (a->stream_idx - b->stream_idx)))
5988 return ret;
5989 if ((ret = strcmp(a->semantic_name, b->semantic_name)))
5990 return ret;
5991 if ((ret = (a->semantic_idx - b->semantic_idx)))
5992 return ret;
5993 if ((ret = (a->component_idx - b->component_idx)))
5994 return ret;
5995 if ((ret = (a->component_count - b->component_count)))
5996 return ret;
5997 if ((ret = (a->output_slot - b->output_slot)))
5998 return ret;
6001 for (i = 0; i < k->buffer_stride_count; ++i)
6003 if ((ret = (k->buffer_strides[i] - desc->buffer_strides[i])))
6004 return ret;
6007 return 0;
6010 static int wined3d_sampler_compare(const void *key, const struct wine_rb_entry *entry)
6012 const struct wined3d_sampler *sampler = WINE_RB_ENTRY_VALUE(entry, struct wined3d_sampler, entry);
6014 return memcmp(&sampler->desc, key, sizeof(sampler->desc));
6017 static int wined3d_rasterizer_state_compare(const void *key, const struct wine_rb_entry *entry)
6019 const struct wined3d_rasterizer_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_rasterizer_state, entry);
6021 return memcmp(&state->desc, key, sizeof(state->desc));
6024 static int wined3d_blend_state_compare(const void *key, const struct wine_rb_entry *entry)
6026 const struct wined3d_blend_state *state = WINE_RB_ENTRY_VALUE(entry, struct wined3d_blend_state, entry);
6028 return memcmp(&state->desc, key, sizeof(state->desc));
6031 static int wined3d_depth_stencil_state_compare(const void *key, const struct wine_rb_entry *entry)
6033 const struct wined3d_depth_stencil_state *state
6034 = WINE_RB_ENTRY_VALUE(entry, struct wined3d_depth_stencil_state, entry);
6036 return memcmp(&state->desc, key, sizeof(state->desc));
6039 HRESULT wined3d_device_init(struct wined3d_device *device, struct wined3d *wined3d,
6040 unsigned int adapter_idx, enum wined3d_device_type device_type, HWND focus_window, unsigned int flags,
6041 BYTE surface_alignment, const enum wined3d_feature_level *levels, unsigned int level_count,
6042 const BOOL *supported_extensions, struct wined3d_device_parent *device_parent)
6044 struct wined3d_adapter *adapter = wined3d->adapters[adapter_idx];
6045 const struct wined3d_fragment_pipe_ops *fragment_pipeline;
6046 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
6047 unsigned int i;
6048 HRESULT hr;
6050 device->ref = 1;
6051 device->wined3d = wined3d;
6052 wined3d_incref(device->wined3d);
6053 device->adapter = adapter;
6054 device->device_parent = device_parent;
6055 list_init(&device->resources);
6056 list_init(&device->shaders);
6057 device->surface_alignment = surface_alignment;
6059 /* Save the creation parameters. */
6060 device->create_parms.adapter_idx = adapter_idx;
6061 device->create_parms.device_type = device_type;
6062 device->create_parms.focus_window = focus_window;
6063 device->create_parms.flags = flags;
6065 device->shader_backend = adapter->shader_backend;
6067 vertex_pipeline = adapter->vertex_pipe;
6069 fragment_pipeline = adapter->fragment_pipe;
6071 wine_rb_init(&device->so_descs, wined3d_so_desc_compare);
6072 wine_rb_init(&device->samplers, wined3d_sampler_compare);
6073 wine_rb_init(&device->rasterizer_states, wined3d_rasterizer_state_compare);
6074 wine_rb_init(&device->blend_states, wined3d_blend_state_compare);
6075 wine_rb_init(&device->depth_stencil_states, wined3d_depth_stencil_state_compare);
6077 if (vertex_pipeline->vp_states && fragment_pipeline->states
6078 && FAILED(hr = compile_state_table(device->state_table, device->multistate_funcs,
6079 &adapter->d3d_info, supported_extensions, vertex_pipeline,
6080 fragment_pipeline, adapter->misc_state_template)))
6082 ERR("Failed to compile state table, hr %#x.\n", hr);
6083 wine_rb_destroy(&device->samplers, NULL, NULL);
6084 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6085 wine_rb_destroy(&device->blend_states, NULL, NULL);
6086 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6087 wine_rb_destroy(&device->so_descs, NULL, NULL);
6088 wined3d_decref(device->wined3d);
6089 return hr;
6092 device->max_frame_latency = 3;
6094 if (!(device->cs = wined3d_cs_create(device, levels, level_count)))
6096 WARN("Failed to create command stream.\n");
6097 hr = E_FAIL;
6098 goto err;
6101 return WINED3D_OK;
6103 err:
6104 for (i = 0; i < ARRAY_SIZE(device->multistate_funcs); ++i)
6106 heap_free(device->multistate_funcs[i]);
6108 wine_rb_destroy(&device->samplers, NULL, NULL);
6109 wine_rb_destroy(&device->rasterizer_states, NULL, NULL);
6110 wine_rb_destroy(&device->blend_states, NULL, NULL);
6111 wine_rb_destroy(&device->depth_stencil_states, NULL, NULL);
6112 wine_rb_destroy(&device->so_descs, NULL, NULL);
6113 wined3d_decref(device->wined3d);
6114 return hr;
6117 void device_invalidate_state(const struct wined3d_device *device, unsigned int state_id)
6119 unsigned int representative, i, idx, shift;
6120 struct wined3d_context *context;
6122 wined3d_from_cs(device->cs);
6124 if (STATE_IS_COMPUTE(state_id))
6126 for (i = 0; i < device->context_count; ++i)
6127 context_invalidate_compute_state(device->contexts[i], state_id);
6128 return;
6131 representative = device->state_table[state_id].representative;
6132 idx = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
6133 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
6134 for (i = 0; i < device->context_count; ++i)
6136 device->contexts[i]->dirty_graphics_states[idx] |= (1u << shift);
6140 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
6141 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
6143 if (message == WM_DESTROY)
6145 TRACE("unregister window %p.\n", window);
6146 wined3d_unregister_window(window);
6148 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
6149 ERR("Window %p is not the focus window for device %p.\n", window, device);
6151 else if (message == WM_DISPLAYCHANGE)
6153 device->device_parent->ops->mode_changed(device->device_parent);
6155 else if (message == WM_ACTIVATEAPP)
6157 unsigned int i = device->swapchain_count;
6159 /* Deactivating the implicit swapchain may cause the application
6160 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
6161 * deactivate the implicit swapchain last, and to avoid accessing the
6162 * "device" pointer afterwards. */
6163 while (i--)
6164 wined3d_swapchain_activate(device->swapchains[i], wparam);
6166 else if (message == WM_SYSCOMMAND)
6168 if (wparam == SC_RESTORE && device->wined3d->flags & WINED3D_HANDLE_RESTORE)
6170 if (unicode)
6171 DefWindowProcW(window, message, wparam, lparam);
6172 else
6173 DefWindowProcA(window, message, wparam, lparam);
6177 if (unicode)
6178 return CallWindowProcW(proc, window, message, wparam, lparam);
6179 else
6180 return CallWindowProcA(proc, window, message, wparam, lparam);