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
28 #include "wine/port.h"
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
39 struct wined3d_matrix_3x3
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 */
88 0.0f
, 0.0f
, 0.0f
, /* Attenuation 0,1,2 */
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");
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
);
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
);
121 new_array
[device
->context_count
++] = context
;
122 device
->contexts
= new_array
;
127 void device_context_remove(struct wined3d_device
*device
, struct wined3d_context
*context
)
129 struct wined3d_context
**new_array
;
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
)
149 ERR("Context %p doesn't exist in context array.\n", context
);
153 if (!--device
->context_count
)
155 heap_free(device
->contexts
);
156 device
->contexts
= NULL
;
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");
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
);
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
);
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
)
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
);
266 device
->adapter
->adapter_ops
->adapter_destroy_device(device
);
267 TRACE("Destroyed device %p.\n", device
);
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
);
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
;
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
));
308 GetObjectA(hbm
, sizeof(BITMAP
), &bm
);
310 if (!(dcb
= CreateCompatibleDC(NULL
)))
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
;
320 desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
321 desc
.width
= bm
.bmWidth
;
322 desc
.height
= bm
.bmHeight
;
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
);
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
;
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
);
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
;
360 if (d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_UNBOUND_RESOURCE_COLOR
)
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
])
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
));
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
;
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
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 void wined3d_null_image_vk_cleanup(struct wined3d_null_image_vk
*image
,
585 struct wined3d_context_vk
*context_vk
, uint64_t command_buffer_id
)
587 wined3d_context_vk_destroy_image(context_vk
, image
->vk_image
, command_buffer_id
);
589 wined3d_context_vk_destroy_allocator_block(context_vk
, image
->memory
, command_buffer_id
);
591 wined3d_context_vk_destroy_memory(context_vk
, image
->vk_memory
, command_buffer_id
);
594 static bool wined3d_null_image_vk_init(struct wined3d_null_image_vk
*image
, struct wined3d_context_vk
*context_vk
,
595 VkCommandBuffer vk_command_buffer
, VkImageType type
, unsigned int layer_count
, unsigned int sample_count
)
597 struct wined3d_device_vk
*device_vk
= wined3d_device_vk(context_vk
->c
.device
);
598 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
599 VkMemoryRequirements memory_requirements
;
600 VkImageSubresourceRange range
;
601 VkImageCreateInfo image_desc
;
602 unsigned int memory_type_idx
;
606 static const VkClearColorValue colour
= {{0}};
608 TRACE("image %p, context_vk %p, vk_command_buffer %p, type %#x, layer_count %u, sample_count %u.\n",
609 image
, context_vk
, vk_command_buffer
, type
, layer_count
, sample_count
);
611 if (type
== VK_IMAGE_TYPE_2D
&& layer_count
>= 6)
612 flags
|= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
;
614 image_desc
.sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
;
615 image_desc
.pNext
= NULL
;
616 image_desc
.flags
= flags
;
617 image_desc
.imageType
= type
;
618 image_desc
.format
= VK_FORMAT_R8G8B8A8_UNORM
;
619 image_desc
.extent
.width
= 1;
620 image_desc
.extent
.height
= 1;
621 image_desc
.extent
.depth
= 1;
622 image_desc
.mipLevels
= 1;
623 image_desc
.arrayLayers
= layer_count
;
624 image_desc
.samples
= sample_count
;
625 image_desc
.tiling
= VK_IMAGE_TILING_OPTIMAL
;
626 image_desc
.usage
= VK_IMAGE_USAGE_TRANSFER_DST_BIT
| VK_IMAGE_USAGE_SAMPLED_BIT
;
627 image_desc
.sharingMode
= VK_SHARING_MODE_EXCLUSIVE
;
628 image_desc
.queueFamilyIndexCount
= 0;
629 image_desc
.pQueueFamilyIndices
= NULL
;
630 image_desc
.initialLayout
= VK_IMAGE_LAYOUT_UNDEFINED
;
631 if ((vr
= VK_CALL(vkCreateImage(device_vk
->vk_device
, &image_desc
, NULL
, &image
->vk_image
))) < 0)
633 ERR("Failed to create Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr
));
637 VK_CALL(vkGetImageMemoryRequirements(device_vk
->vk_device
, image
->vk_image
, &memory_requirements
));
639 memory_type_idx
= wined3d_adapter_vk_get_memory_type_index(wined3d_adapter_vk(device_vk
->d
.adapter
),
640 memory_requirements
.memoryTypeBits
, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
);
641 if (memory_type_idx
== ~0u)
643 ERR("Failed to find suitable image memory type.\n");
644 VK_CALL(vkDestroyImage(device_vk
->vk_device
, image
->vk_image
, NULL
));
645 image
->vk_image
= VK_NULL_HANDLE
;
649 image
->memory
= wined3d_context_vk_allocate_memory(context_vk
,
650 memory_type_idx
, memory_requirements
.size
, &image
->vk_memory
);
651 if (!image
->vk_memory
)
653 ERR("Failed to allocate image memory.\n");
654 VK_CALL(vkDestroyImage(device_vk
->vk_device
, image
->vk_image
, NULL
));
655 image
->vk_image
= VK_NULL_HANDLE
;
659 if ((vr
= VK_CALL(vkBindImageMemory(device_vk
->vk_device
, image
->vk_image
,
660 image
->vk_memory
, image
->memory
? image
->memory
->offset
: 0))) < 0)
662 ERR("Failed to bind image memory, vr %s.\n", wined3d_debug_vkresult(vr
));
664 wined3d_allocator_block_free(image
->memory
);
666 VK_CALL(vkFreeMemory(device_vk
->vk_device
, image
->vk_memory
, NULL
));
667 VK_CALL(vkDestroyImage(device_vk
->vk_device
, image
->vk_image
, NULL
));
668 image
->vk_image
= VK_NULL_HANDLE
;
672 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
673 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
, 0, VK_ACCESS_TRANSFER_WRITE_BIT
,
674 VK_IMAGE_LAYOUT_UNDEFINED
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
675 image
->vk_image
, VK_IMAGE_ASPECT_COLOR_BIT
);
677 range
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
678 range
.baseMipLevel
= 0;
679 range
.levelCount
= 1;
680 range
.baseArrayLayer
= 0;
681 range
.layerCount
= layer_count
;
682 VK_CALL(vkCmdClearColorImage(vk_command_buffer
, image
->vk_image
,
683 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, &colour
, 1, &range
));
685 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
686 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_ACCESS_TRANSFER_WRITE_BIT
, 0,
687 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
,
688 image
->vk_image
, VK_IMAGE_ASPECT_COLOR_BIT
);
690 TRACE("Created NULL image 0x%s, memory 0x%s.\n",
691 wine_dbgstr_longlong(image
->vk_image
), wine_dbgstr_longlong(image
->vk_memory
));
696 bool wined3d_device_vk_create_null_resources(struct wined3d_device_vk
*device_vk
,
697 struct wined3d_context_vk
*context_vk
)
699 struct wined3d_null_resources_vk
*r
= &device_vk
->null_resources_vk
;
700 const struct wined3d_vk_info
*vk_info
;
701 const struct wined3d_format
*format
;
702 VkMemoryPropertyFlags memory_type
;
703 VkCommandBuffer vk_command_buffer
;
704 unsigned int sample_count
= 2;
705 VkBufferUsageFlags usage
;
708 format
= wined3d_get_format(device_vk
->d
.adapter
, WINED3DFMT_R8G8B8A8_UNORM
, WINED3D_BIND_SHADER_RESOURCE
);
709 while (sample_count
&& !(sample_count
& format
->multisample_types
))
712 if (!(vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
)))
714 ERR("Failed to get command buffer.\n");
718 vk_info
= context_vk
->vk_info
;
720 usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
| VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
;
721 memory_type
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
;
722 if (!wined3d_context_vk_create_bo(context_vk
, 16, usage
, memory_type
, &r
->bo
))
724 VK_CALL(vkCmdFillBuffer(vk_command_buffer
, r
->bo
.vk_buffer
, r
->bo
.buffer_offset
, r
->bo
.size
, 0x00000000u
));
726 if (!wined3d_null_image_vk_init(&r
->image_1d
, context_vk
, vk_command_buffer
, VK_IMAGE_TYPE_1D
, 1, 1))
728 ERR("Failed to create 1D image.\n");
732 if (!wined3d_null_image_vk_init(&r
->image_2d
, context_vk
, vk_command_buffer
, VK_IMAGE_TYPE_2D
, 6, 1))
734 ERR("Failed to create 2D image.\n");
738 if (!wined3d_null_image_vk_init(&r
->image_2dms
, context_vk
, vk_command_buffer
, VK_IMAGE_TYPE_2D
, 1, sample_count
))
740 ERR("Failed to create 2D MSAA image.\n");
744 if (!wined3d_null_image_vk_init(&r
->image_3d
, context_vk
, vk_command_buffer
, VK_IMAGE_TYPE_3D
, 1, 1))
746 ERR("Failed to create 3D image.\n");
753 id
= context_vk
->current_command_buffer
.id
;
754 if (r
->image_2dms
.vk_image
)
755 wined3d_null_image_vk_cleanup(&r
->image_2dms
, context_vk
, id
);
756 if (r
->image_2d
.vk_image
)
757 wined3d_null_image_vk_cleanup(&r
->image_2d
, context_vk
, id
);
758 if (r
->image_1d
.vk_image
)
759 wined3d_null_image_vk_cleanup(&r
->image_1d
, context_vk
, id
);
760 wined3d_context_vk_reference_bo(context_vk
, &r
->bo
);
761 wined3d_context_vk_destroy_bo(context_vk
, &r
->bo
);
765 void wined3d_device_vk_destroy_null_resources(struct wined3d_device_vk
*device_vk
,
766 struct wined3d_context_vk
*context_vk
)
768 struct wined3d_null_resources_vk
*r
= &device_vk
->null_resources_vk
;
769 uint64_t id
= context_vk
->current_command_buffer
.id
;
771 /* We don't track command buffer references to NULL resources. We easily
772 * could, but it doesn't seem worth it. */
773 wined3d_null_image_vk_cleanup(&r
->image_3d
, context_vk
, id
);
774 wined3d_null_image_vk_cleanup(&r
->image_2dms
, context_vk
, id
);
775 wined3d_null_image_vk_cleanup(&r
->image_2d
, context_vk
, id
);
776 wined3d_null_image_vk_cleanup(&r
->image_1d
, context_vk
, id
);
777 wined3d_context_vk_reference_bo(context_vk
, &r
->bo
);
778 wined3d_context_vk_destroy_bo(context_vk
, &r
->bo
);
781 bool wined3d_device_vk_create_null_views(struct wined3d_device_vk
*device_vk
, struct wined3d_context_vk
*context_vk
)
783 struct wined3d_null_resources_vk
*r
= &device_vk
->null_resources_vk
;
784 struct wined3d_null_views_vk
*v
= &device_vk
->null_views_vk
;
785 VkBufferViewCreateInfo buffer_create_info
;
786 const struct wined3d_vk_info
*vk_info
;
787 VkImageViewCreateInfo view_desc
;
790 vk_info
= context_vk
->vk_info
;
792 buffer_create_info
.sType
= VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
;
793 buffer_create_info
.pNext
= NULL
;
794 buffer_create_info
.flags
= 0;
795 buffer_create_info
.buffer
= r
->bo
.vk_buffer
;
796 buffer_create_info
.format
= VK_FORMAT_R32_UINT
;
797 buffer_create_info
.offset
= r
->bo
.buffer_offset
;
798 buffer_create_info
.range
= r
->bo
.size
;
800 if ((vr
= VK_CALL(vkCreateBufferView(device_vk
->vk_device
,
801 &buffer_create_info
, NULL
, &v
->vk_view_buffer_uint
))) < 0)
803 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr
));
806 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v
->vk_view_buffer_uint
));
808 buffer_create_info
.format
= VK_FORMAT_R32G32B32A32_SFLOAT
;
809 if ((vr
= VK_CALL(vkCreateBufferView(device_vk
->vk_device
,
810 &buffer_create_info
, NULL
, &v
->vk_view_buffer_float
))) < 0)
812 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr
));
815 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(v
->vk_view_buffer_float
));
817 view_desc
.sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
;
818 view_desc
.pNext
= NULL
;
820 view_desc
.image
= r
->image_1d
.vk_image
;
821 view_desc
.viewType
= VK_IMAGE_VIEW_TYPE_1D
;
822 view_desc
.format
= VK_FORMAT_R8G8B8A8_UNORM
;
823 view_desc
.components
.r
= VK_COMPONENT_SWIZZLE_ZERO
;
824 view_desc
.components
.g
= VK_COMPONENT_SWIZZLE_ZERO
;
825 view_desc
.components
.b
= VK_COMPONENT_SWIZZLE_ZERO
;
826 view_desc
.components
.a
= VK_COMPONENT_SWIZZLE_ZERO
;
827 view_desc
.subresourceRange
.aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
;
828 view_desc
.subresourceRange
.baseMipLevel
= 0;
829 view_desc
.subresourceRange
.levelCount
= 1;
830 view_desc
.subresourceRange
.baseArrayLayer
= 0;
831 view_desc
.subresourceRange
.layerCount
= 1;
832 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &view_desc
, NULL
, &v
->vk_info_1d
.imageView
))) < 0)
834 ERR("Failed to create 1D image view, vr %s.\n", wined3d_debug_vkresult(vr
));
837 v
->vk_info_1d
.sampler
= VK_NULL_HANDLE
;
838 v
->vk_info_1d
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
839 TRACE("Created 1D image view 0x%s.\n", wine_dbgstr_longlong(v
->vk_info_1d
.imageView
));
841 view_desc
.image
= r
->image_2d
.vk_image
;
842 view_desc
.viewType
= VK_IMAGE_VIEW_TYPE_2D
;
843 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &view_desc
, NULL
, &v
->vk_info_2d
.imageView
))) < 0)
845 ERR("Failed to create 2D image view, vr %s.\n", wined3d_debug_vkresult(vr
));
848 v
->vk_info_2d
.sampler
= VK_NULL_HANDLE
;
849 v
->vk_info_2d
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
850 TRACE("Created 2D image view 0x%s.\n", wine_dbgstr_longlong(v
->vk_info_2d
.imageView
));
852 view_desc
.image
= r
->image_2dms
.vk_image
;
853 view_desc
.viewType
= VK_IMAGE_VIEW_TYPE_2D
;
854 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &view_desc
, NULL
, &v
->vk_info_2dms
.imageView
))) < 0)
856 ERR("Failed to create 2D MSAA image view, vr %s.\n", wined3d_debug_vkresult(vr
));
859 v
->vk_info_2dms
.sampler
= VK_NULL_HANDLE
;
860 v
->vk_info_2dms
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
861 TRACE("Created 2D MSAA image view 0x%s.\n", wine_dbgstr_longlong(v
->vk_info_2dms
.imageView
));
863 view_desc
.image
= r
->image_3d
.vk_image
;
864 view_desc
.viewType
= VK_IMAGE_VIEW_TYPE_3D
;
865 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &view_desc
, NULL
, &v
->vk_info_3d
.imageView
))) < 0)
867 ERR("Failed to create 3D image view, vr %s.\n", wined3d_debug_vkresult(vr
));
870 v
->vk_info_3d
.sampler
= VK_NULL_HANDLE
;
871 v
->vk_info_3d
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
872 TRACE("Created 3D image view 0x%s.\n", wine_dbgstr_longlong(v
->vk_info_3d
.imageView
));
874 view_desc
.image
= r
->image_2d
.vk_image
;
875 view_desc
.subresourceRange
.layerCount
= 6;
876 view_desc
.viewType
= VK_IMAGE_VIEW_TYPE_CUBE
;
877 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &view_desc
, NULL
, &v
->vk_info_cube
.imageView
))) < 0)
879 ERR("Failed to create cube image view, vr %s.\n", wined3d_debug_vkresult(vr
));
882 v
->vk_info_cube
.sampler
= VK_NULL_HANDLE
;
883 v
->vk_info_cube
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
884 TRACE("Created cube image view 0x%s.\n", wine_dbgstr_longlong(v
->vk_info_cube
.imageView
));
886 view_desc
.subresourceRange
.layerCount
= 1;
887 view_desc
.viewType
= VK_IMAGE_VIEW_TYPE_2D_ARRAY
;
888 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &view_desc
, NULL
, &v
->vk_info_2d_array
.imageView
))) < 0)
890 ERR("Failed to create 2D array image view, vr %s.\n", wined3d_debug_vkresult(vr
));
893 v
->vk_info_2d_array
.sampler
= VK_NULL_HANDLE
;
894 v
->vk_info_2d_array
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
895 TRACE("Created 2D array image view 0x%s.\n", wine_dbgstr_longlong(v
->vk_info_2d_array
.imageView
));
897 view_desc
.image
= r
->image_2dms
.vk_image
;
898 view_desc
.viewType
= VK_IMAGE_VIEW_TYPE_2D_ARRAY
;
899 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &view_desc
, NULL
, &v
->vk_info_2dms_array
.imageView
))) < 0)
901 ERR("Failed to create 2D MSAA array image view, vr %s.\n", wined3d_debug_vkresult(vr
));
904 v
->vk_info_2dms_array
.sampler
= VK_NULL_HANDLE
;
905 v
->vk_info_2dms_array
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
906 TRACE("Created 2D MSAA array image view 0x%s.\n", wine_dbgstr_longlong(v
->vk_info_2dms_array
.imageView
));
911 if (v
->vk_info_2d_array
.imageView
)
912 VK_CALL(vkDestroyImageView(device_vk
->vk_device
, v
->vk_info_2d_array
.imageView
, NULL
));
913 if (v
->vk_info_cube
.imageView
)
914 VK_CALL(vkDestroyImageView(device_vk
->vk_device
, v
->vk_info_cube
.imageView
, NULL
));
915 if (v
->vk_info_3d
.imageView
)
916 VK_CALL(vkDestroyImageView(device_vk
->vk_device
, v
->vk_info_3d
.imageView
, NULL
));
917 if (v
->vk_info_2dms
.imageView
)
918 VK_CALL(vkDestroyImageView(device_vk
->vk_device
, v
->vk_info_2dms
.imageView
, NULL
));
919 if (v
->vk_info_2d
.imageView
)
920 VK_CALL(vkDestroyImageView(device_vk
->vk_device
, v
->vk_info_2d
.imageView
, NULL
));
921 if (v
->vk_info_1d
.imageView
)
922 VK_CALL(vkDestroyImageView(device_vk
->vk_device
, v
->vk_info_1d
.imageView
, NULL
));
923 if (v
->vk_view_buffer_float
)
924 VK_CALL(vkDestroyBufferView(device_vk
->vk_device
, v
->vk_view_buffer_float
, NULL
));
925 VK_CALL(vkDestroyBufferView(device_vk
->vk_device
, v
->vk_view_buffer_uint
, NULL
));
929 void wined3d_device_vk_destroy_null_views(struct wined3d_device_vk
*device_vk
, struct wined3d_context_vk
*context_vk
)
931 struct wined3d_null_views_vk
*v
= &device_vk
->null_views_vk
;
932 uint64_t id
= context_vk
->current_command_buffer
.id
;
934 wined3d_context_vk_destroy_image_view(context_vk
, v
->vk_info_2dms_array
.imageView
, id
);
935 wined3d_context_vk_destroy_image_view(context_vk
, v
->vk_info_2d_array
.imageView
, id
);
936 wined3d_context_vk_destroy_image_view(context_vk
, v
->vk_info_cube
.imageView
, id
);
937 wined3d_context_vk_destroy_image_view(context_vk
, v
->vk_info_3d
.imageView
, id
);
938 wined3d_context_vk_destroy_image_view(context_vk
, v
->vk_info_2dms
.imageView
, id
);
939 wined3d_context_vk_destroy_image_view(context_vk
, v
->vk_info_2d
.imageView
, id
);
940 wined3d_context_vk_destroy_image_view(context_vk
, v
->vk_info_1d
.imageView
, id
);
942 wined3d_context_vk_destroy_buffer_view(context_vk
, v
->vk_view_buffer_float
, id
);
943 wined3d_context_vk_destroy_buffer_view(context_vk
, v
->vk_view_buffer_uint
, id
);
946 HRESULT CDECL
wined3d_device_acquire_focus_window(struct wined3d_device
*device
, HWND window
)
948 unsigned int screensaver_active
;
950 TRACE("device %p, window %p.\n", device
, window
);
952 if (!wined3d_register_window(NULL
, window
, device
, 0))
954 ERR("Failed to register window %p.\n", window
);
958 InterlockedExchangePointer((void **)&device
->focus_window
, window
);
959 SetWindowPos(window
, 0, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
);
960 SystemParametersInfoW(SPI_GETSCREENSAVEACTIVE
, 0, &screensaver_active
, 0);
961 if ((device
->restore_screensaver
= !!screensaver_active
))
962 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, FALSE
, NULL
, 0);
967 void CDECL
wined3d_device_release_focus_window(struct wined3d_device
*device
)
969 TRACE("device %p.\n", device
);
971 if (device
->focus_window
) wined3d_unregister_window(device
->focus_window
);
972 InterlockedExchangePointer((void **)&device
->focus_window
, NULL
);
973 if (device
->restore_screensaver
)
975 SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE
, TRUE
, NULL
, 0);
976 device
->restore_screensaver
= FALSE
;
980 static void device_init_swapchain_state(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
982 BOOL ds_enable
= swapchain
->state
.desc
.enable_auto_depth_stencil
;
985 for (i
= 0; i
< device
->adapter
->d3d_info
.limits
.max_rt_count
; ++i
)
987 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
989 if (device
->back_buffer_view
)
990 wined3d_device_set_rendertarget_view(device
, 0, device
->back_buffer_view
, TRUE
);
992 wined3d_device_set_depth_stencil_view(device
, ds_enable
? device
->auto_depth_stencil_view
: NULL
);
995 void wined3d_device_delete_opengl_contexts_cs(void *object
)
997 struct wined3d_swapchain_gl
*swapchain_gl
;
998 struct wined3d_device
*device
= object
;
999 struct wined3d_context_gl
*context_gl
;
1000 struct wined3d_device_gl
*device_gl
;
1001 struct wined3d_context
*context
;
1002 struct wined3d_shader
*shader
;
1004 device_gl
= wined3d_device_gl(device
);
1006 LIST_FOR_EACH_ENTRY(shader
, &device
->shaders
, struct wined3d_shader
, shader_list_entry
)
1008 device
->shader_backend
->shader_destroy(shader
);
1011 context
= context_acquire(device
, NULL
, 0);
1012 context_gl
= wined3d_context_gl(context
);
1013 device
->blitter
->ops
->blitter_destroy(device
->blitter
, context
);
1014 device
->shader_backend
->shader_free_private(device
, context
);
1015 wined3d_device_gl_destroy_dummy_textures(device_gl
, context_gl
);
1016 wined3d_device_destroy_default_samplers(device
, context
);
1017 context_release(context
);
1019 while (device
->context_count
)
1021 if ((swapchain_gl
= wined3d_swapchain_gl(device
->contexts
[0]->swapchain
)))
1022 wined3d_swapchain_gl_destroy_contexts(swapchain_gl
);
1024 wined3d_context_gl_destroy(wined3d_context_gl(device
->contexts
[0]));
1028 void wined3d_device_create_primary_opengl_context_cs(void *object
)
1030 struct wined3d_device
*device
= object
;
1031 struct wined3d_context_gl
*context_gl
;
1032 struct wined3d_swapchain
*swapchain
;
1033 struct wined3d_context
*context
;
1034 struct wined3d_texture
*target
;
1037 swapchain
= device
->swapchains
[0];
1038 target
= swapchain
->back_buffers
? swapchain
->back_buffers
[0] : swapchain
->front_buffer
;
1039 if (!(context
= context_acquire(device
, target
, 0)))
1041 WARN("Failed to acquire context.\n");
1045 if (FAILED(hr
= device
->shader_backend
->shader_alloc_private(device
,
1046 device
->adapter
->vertex_pipe
, device
->adapter
->fragment_pipe
)))
1048 ERR("Failed to allocate shader private data, hr %#x.\n", hr
);
1049 context_release(context
);
1053 if (!(device
->blitter
= wined3d_cpu_blitter_create()))
1055 ERR("Failed to create CPU blitter.\n");
1056 device
->shader_backend
->shader_free_private(device
, NULL
);
1057 context_release(context
);
1061 context_gl
= wined3d_context_gl(context
);
1063 wined3d_ffp_blitter_create(&device
->blitter
, context_gl
->gl_info
);
1064 if (!wined3d_glsl_blitter_create(&device
->blitter
, device
))
1065 wined3d_arbfp_blitter_create(&device
->blitter
, device
);
1066 wined3d_fbo_blitter_create(&device
->blitter
, context_gl
->gl_info
);
1067 wined3d_raw_blitter_create(&device
->blitter
, context_gl
->gl_info
);
1069 wined3d_device_gl_create_dummy_textures(wined3d_device_gl(device
), context_gl
);
1070 wined3d_device_create_default_samplers(device
, context
);
1071 context_release(context
);
1074 HRESULT
wined3d_device_set_implicit_swapchain(struct wined3d_device
*device
, struct wined3d_swapchain
*swapchain
)
1076 static const struct wined3d_color black
= {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1077 const struct wined3d_swapchain_desc
*swapchain_desc
;
1078 DWORD clear_flags
= 0;
1082 TRACE("device %p, swapchain %p.\n", device
, swapchain
);
1084 if (device
->d3d_initialized
)
1085 return WINED3DERR_INVALIDCALL
;
1087 swapchain_desc
= &swapchain
->state
.desc
;
1088 if (swapchain_desc
->backbuffer_count
&& swapchain_desc
->backbuffer_bind_flags
& WINED3D_BIND_RENDER_TARGET
)
1090 struct wined3d_resource
*back_buffer
= &swapchain
->back_buffers
[0]->resource
;
1091 struct wined3d_view_desc view_desc
;
1093 view_desc
.format_id
= back_buffer
->format
->id
;
1094 view_desc
.flags
= 0;
1095 view_desc
.u
.texture
.level_idx
= 0;
1096 view_desc
.u
.texture
.level_count
= 1;
1097 view_desc
.u
.texture
.layer_idx
= 0;
1098 view_desc
.u
.texture
.layer_count
= 1;
1099 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
, back_buffer
,
1100 NULL
, &wined3d_null_parent_ops
, &device
->back_buffer_view
)))
1102 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
1107 device
->swapchain_count
= 1;
1108 if (!(device
->swapchains
= heap_calloc(device
->swapchain_count
, sizeof(*device
->swapchains
))))
1110 ERR("Failed to allocate swapchain array.\n");
1114 device
->swapchains
[0] = swapchain
;
1116 for (i
= 0; i
< ARRAY_SIZE(device
->state
.fb
.render_targets
); ++i
)
1117 if (device
->state
.fb
.render_targets
[i
])
1118 wined3d_rtv_bind_count_dec(device
->state
.fb
.render_targets
[i
]);
1120 memset(device
->state
.fb
.render_targets
, 0, sizeof(device
->state
.fb
.render_targets
));
1121 if (FAILED(hr
= device
->adapter
->adapter_ops
->adapter_init_3d(device
)))
1123 device
->d3d_initialized
= TRUE
;
1125 device_init_swapchain_state(device
, swapchain
);
1127 TRACE("All defaults now set up.\n");
1129 /* Clear the screen. */
1130 if (device
->back_buffer_view
)
1131 clear_flags
|= WINED3DCLEAR_TARGET
;
1132 if (swapchain_desc
->enable_auto_depth_stencil
)
1133 clear_flags
|= WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
;
1135 wined3d_device_clear(device
, 0, NULL
, clear_flags
, &black
, 1.0f
, 0);
1137 if (wined3d_settings
.logo
)
1138 device_load_logo(device
, wined3d_settings
.logo
);
1143 heap_free(device
->swapchains
);
1144 device
->swapchains
= NULL
;
1145 device
->swapchain_count
= 0;
1146 if (device
->back_buffer_view
)
1148 wined3d_rendertarget_view_decref(device
->back_buffer_view
);
1149 device
->back_buffer_view
= NULL
;
1155 static void device_free_sampler(struct wine_rb_entry
*entry
, void *context
)
1157 struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
1159 wined3d_sampler_decref(sampler
);
1162 static void device_free_rasterizer_state(struct wine_rb_entry
*entry
, void *context
)
1164 struct wined3d_rasterizer_state
*state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_rasterizer_state
, entry
);
1166 wined3d_rasterizer_state_decref(state
);
1169 static void device_free_blend_state(struct wine_rb_entry
*entry
, void *context
)
1171 struct wined3d_blend_state
*blend_state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_blend_state
, entry
);
1173 wined3d_blend_state_decref(blend_state
);
1176 static void device_free_depth_stencil_state(struct wine_rb_entry
*entry
, void *context
)
1178 struct wined3d_depth_stencil_state
*state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_depth_stencil_state
, entry
);
1180 wined3d_depth_stencil_state_decref(state
);
1183 void wined3d_device_uninit_3d(struct wined3d_device
*device
)
1185 struct wined3d_resource
*resource
, *cursor
;
1186 struct wined3d_rendertarget_view
*view
;
1187 struct wined3d_texture
*texture
;
1190 TRACE("device %p.\n", device
);
1192 if (!device
->d3d_initialized
)
1194 ERR("Called while 3D support was not initialised.\n");
1198 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1200 device
->swapchain_count
= 0;
1202 if ((texture
= device
->logo_texture
))
1204 device
->logo_texture
= NULL
;
1205 wined3d_texture_decref(texture
);
1208 if ((texture
= device
->cursor_texture
))
1210 device
->cursor_texture
= NULL
;
1211 wined3d_texture_decref(texture
);
1214 wined3d_cs_emit_reset_state(device
->cs
);
1215 state_cleanup(&device
->state
);
1216 for (i
= 0; i
< device
->adapter
->d3d_info
.limits
.max_rt_count
; ++i
)
1218 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
1221 wine_rb_clear(&device
->samplers
, device_free_sampler
, NULL
);
1222 wine_rb_clear(&device
->rasterizer_states
, device_free_rasterizer_state
, NULL
);
1223 wine_rb_clear(&device
->blend_states
, device_free_blend_state
, NULL
);
1224 wine_rb_clear(&device
->depth_stencil_states
, device_free_depth_stencil_state
, NULL
);
1226 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
1228 TRACE("Unloading resource %p.\n", resource
);
1229 wined3d_cs_emit_unload_resource(device
->cs
, resource
);
1232 device
->adapter
->adapter_ops
->adapter_uninit_3d(device
);
1233 device
->d3d_initialized
= FALSE
;
1235 if ((view
= device
->state
.fb
.depth_stencil
))
1237 TRACE("Releasing depth/stencil view %p.\n", view
);
1239 device
->state
.fb
.depth_stencil
= NULL
;
1240 wined3d_rendertarget_view_decref(view
);
1243 if ((view
= device
->auto_depth_stencil_view
))
1245 device
->auto_depth_stencil_view
= NULL
;
1246 if (wined3d_rendertarget_view_decref(view
))
1247 ERR("Something's still holding the auto depth/stencil view (%p).\n", view
);
1250 if ((view
= device
->back_buffer_view
))
1252 device
->back_buffer_view
= NULL
;
1253 wined3d_rendertarget_view_decref(view
);
1256 heap_free(device
->swapchains
);
1257 device
->swapchains
= NULL
;
1259 memset(&device
->state
, 0, sizeof(device
->state
));
1260 state_init(&device
->state
, &device
->adapter
->d3d_info
, WINED3D_STATE_INIT_DEFAULT
);
1263 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1264 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1265 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1267 * There is no way to deactivate thread safety once it is enabled.
1269 void CDECL
wined3d_device_set_multithreaded(struct wined3d_device
*device
)
1271 TRACE("device %p.\n", device
);
1273 /* For now just store the flag (needed in case of ddraw). */
1274 device
->create_parms
.flags
|= WINED3DCREATE_MULTITHREADED
;
1277 UINT CDECL
wined3d_device_get_available_texture_mem(const struct wined3d_device
*device
)
1279 const struct wined3d_driver_info
*driver_info
;
1281 TRACE("device %p.\n", device
);
1283 driver_info
= &device
->adapter
->driver_info
;
1285 TRACE("Emulating 0x%s bytes. 0x%s used, returning 0x%s left.\n",
1286 wine_dbgstr_longlong(driver_info
->vram_bytes
),
1287 wine_dbgstr_longlong(device
->adapter
->vram_bytes_used
),
1288 wine_dbgstr_longlong(driver_info
->vram_bytes
- device
->adapter
->vram_bytes_used
));
1290 return min(UINT_MAX
, driver_info
->vram_bytes
- device
->adapter
->vram_bytes_used
);
1293 void CDECL
wined3d_device_set_stream_output(struct wined3d_device
*device
, UINT idx
,
1294 struct wined3d_buffer
*buffer
, UINT offset
)
1296 struct wined3d_stream_output
*stream
;
1297 struct wined3d_buffer
*prev_buffer
;
1299 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device
, idx
, buffer
, offset
);
1301 if (idx
>= WINED3D_MAX_STREAM_OUTPUT_BUFFERS
)
1303 WARN("Invalid stream output %u.\n", idx
);
1307 stream
= &device
->state
.stream_output
[idx
];
1308 prev_buffer
= stream
->buffer
;
1311 wined3d_buffer_incref(buffer
);
1312 stream
->buffer
= buffer
;
1313 stream
->offset
= offset
;
1314 wined3d_cs_emit_set_stream_output(device
->cs
, idx
, buffer
, offset
);
1316 wined3d_buffer_decref(prev_buffer
);
1319 struct wined3d_buffer
* CDECL
wined3d_device_get_stream_output(struct wined3d_device
*device
,
1320 UINT idx
, UINT
*offset
)
1322 TRACE("device %p, idx %u, offset %p.\n", device
, idx
, offset
);
1324 if (idx
>= WINED3D_MAX_STREAM_OUTPUT_BUFFERS
)
1326 WARN("Invalid stream output %u.\n", idx
);
1331 *offset
= device
->state
.stream_output
[idx
].offset
;
1332 return device
->state
.stream_output
[idx
].buffer
;
1335 HRESULT CDECL
wined3d_device_set_stream_source(struct wined3d_device
*device
, UINT stream_idx
,
1336 struct wined3d_buffer
*buffer
, UINT offset
, UINT stride
)
1338 struct wined3d_stream_state
*stream
;
1339 struct wined3d_buffer
*prev_buffer
;
1341 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1342 device
, stream_idx
, buffer
, offset
, stride
);
1344 if (stream_idx
>= WINED3D_MAX_STREAMS
)
1346 WARN("Stream index %u out of range.\n", stream_idx
);
1347 return WINED3DERR_INVALIDCALL
;
1349 else if (offset
& 0x3)
1351 WARN("Offset %u is not 4 byte aligned.\n", offset
);
1352 return WINED3DERR_INVALIDCALL
;
1355 stream
= &device
->state
.streams
[stream_idx
];
1356 prev_buffer
= stream
->buffer
;
1358 if (prev_buffer
== buffer
1359 && stream
->stride
== stride
1360 && stream
->offset
== offset
)
1362 TRACE("Application is setting the old values over, nothing to do.\n");
1366 stream
->buffer
= buffer
;
1367 stream
->stride
= stride
;
1368 stream
->offset
= offset
;
1370 wined3d_buffer_incref(buffer
);
1371 wined3d_cs_emit_set_stream_source(device
->cs
, stream_idx
, buffer
, offset
, stride
);
1373 wined3d_buffer_decref(prev_buffer
);
1378 HRESULT CDECL
wined3d_device_get_stream_source(const struct wined3d_device
*device
,
1379 UINT stream_idx
, struct wined3d_buffer
**buffer
, UINT
*offset
, UINT
*stride
)
1381 const struct wined3d_stream_state
*stream
;
1383 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1384 device
, stream_idx
, buffer
, offset
, stride
);
1386 if (stream_idx
>= WINED3D_MAX_STREAMS
)
1388 WARN("Stream index %u out of range.\n", stream_idx
);
1389 return WINED3DERR_INVALIDCALL
;
1392 stream
= &device
->state
.streams
[stream_idx
];
1393 *buffer
= stream
->buffer
;
1395 *offset
= stream
->offset
;
1396 *stride
= stream
->stride
;
1401 static void wined3d_device_set_stream_source_freq(struct wined3d_device
*device
, UINT stream_idx
, UINT divider
)
1403 struct wined3d_stream_state
*stream
;
1404 UINT old_flags
, old_freq
;
1406 TRACE("device %p, stream_idx %u, divider %#x.\n", device
, stream_idx
, divider
);
1408 stream
= &device
->state
.streams
[stream_idx
];
1409 old_flags
= stream
->flags
;
1410 old_freq
= stream
->frequency
;
1412 stream
->flags
= divider
& (WINED3DSTREAMSOURCE_INSTANCEDATA
| WINED3DSTREAMSOURCE_INDEXEDDATA
);
1413 stream
->frequency
= divider
& 0x7fffff;
1414 if (stream
->frequency
!= old_freq
|| stream
->flags
!= old_flags
)
1415 wined3d_cs_emit_set_stream_source_freq(device
->cs
, stream_idx
, stream
->frequency
, stream
->flags
);
1418 static void wined3d_device_set_transform(struct wined3d_device
*device
,
1419 enum wined3d_transform_state d3dts
, const struct wined3d_matrix
*matrix
)
1421 TRACE("device %p, state %s, matrix %p.\n",
1422 device
, debug_d3dtstype(d3dts
), matrix
);
1423 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_11
, matrix
->_12
, matrix
->_13
, matrix
->_14
);
1424 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_21
, matrix
->_22
, matrix
->_23
, matrix
->_24
);
1425 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_31
, matrix
->_32
, matrix
->_33
, matrix
->_34
);
1426 TRACE("%.8e %.8e %.8e %.8e\n", matrix
->_41
, matrix
->_42
, matrix
->_43
, matrix
->_44
);
1428 /* If the new matrix is the same as the current one,
1429 * we cut off any further processing. this seems to be a reasonable
1430 * optimization because as was noticed, some apps (warcraft3 for example)
1431 * tend towards setting the same matrix repeatedly for some reason.
1433 * From here on we assume that the new matrix is different, wherever it matters. */
1434 if (!memcmp(&device
->state
.transforms
[d3dts
], matrix
, sizeof(*matrix
)))
1436 TRACE("The application is setting the same matrix over again.\n");
1440 device
->state
.transforms
[d3dts
] = *matrix
;
1441 wined3d_cs_emit_set_transform(device
->cs
, d3dts
, matrix
);
1444 static void wined3d_device_get_transform(const struct wined3d_device
*device
,
1445 enum wined3d_transform_state state
, struct wined3d_matrix
*matrix
)
1447 TRACE("device %p, state %s, matrix %p.\n", device
, debug_d3dtstype(state
), matrix
);
1449 *matrix
= device
->state
.transforms
[state
];
1452 /* Note lights are real special cases. Although the device caps state only
1453 * e.g. 8 are supported, you can reference any indexes you want as long as
1454 * that number max are enabled at any one point in time. Therefore since the
1455 * indices can be anything, we need a hashmap of them. However, this causes
1456 * stateblock problems. When capturing the state block, I duplicate the
1457 * hashmap, but when recording, just build a chain pretty much of commands to
1459 static void wined3d_device_set_light(struct wined3d_device
*device
,
1460 UINT light_idx
, const struct wined3d_light
*light
)
1462 struct wined3d_light_info
*object
= NULL
;
1465 TRACE("device %p, light_idx %u, light %p.\n", device
, light_idx
, light
);
1467 if (FAILED(wined3d_light_state_set_light(&device
->state
.light_state
, light_idx
, light
, &object
)))
1470 /* Initialize the object. */
1471 TRACE("Light %u setting to type %#x, diffuse %s, specular %s, ambient %s, "
1472 "position {%.8e, %.8e, %.8e}, direction {%.8e, %.8e, %.8e}, "
1473 "range %.8e, falloff %.8e, theta %.8e, phi %.8e.\n",
1474 light_idx
, light
->type
, debug_color(&light
->diffuse
),
1475 debug_color(&light
->specular
), debug_color(&light
->ambient
),
1476 light
->position
.x
, light
->position
.y
, light
->position
.z
,
1477 light
->direction
.x
, light
->direction
.y
, light
->direction
.z
,
1478 light
->range
, light
->falloff
, light
->theta
, light
->phi
);
1480 switch (light
->type
)
1482 case WINED3D_LIGHT_POINT
:
1484 object
->position
.x
= light
->position
.x
;
1485 object
->position
.y
= light
->position
.y
;
1486 object
->position
.z
= light
->position
.z
;
1487 object
->position
.w
= 1.0f
;
1488 object
->cutoff
= 180.0f
;
1492 case WINED3D_LIGHT_DIRECTIONAL
:
1494 object
->direction
.x
= -light
->direction
.x
;
1495 object
->direction
.y
= -light
->direction
.y
;
1496 object
->direction
.z
= -light
->direction
.z
;
1497 object
->direction
.w
= 0.0f
;
1498 object
->exponent
= 0.0f
;
1499 object
->cutoff
= 180.0f
;
1502 case WINED3D_LIGHT_SPOT
:
1504 object
->position
.x
= light
->position
.x
;
1505 object
->position
.y
= light
->position
.y
;
1506 object
->position
.z
= light
->position
.z
;
1507 object
->position
.w
= 1.0f
;
1510 object
->direction
.x
= light
->direction
.x
;
1511 object
->direction
.y
= light
->direction
.y
;
1512 object
->direction
.z
= light
->direction
.z
;
1513 object
->direction
.w
= 0.0f
;
1515 /* opengl-ish and d3d-ish spot lights use too different models
1516 * for the light "intensity" as a function of the angle towards
1517 * the main light direction, so we only can approximate very
1518 * roughly. However, spot lights are rather rarely used in games
1519 * (if ever used at all). Furthermore if still used, probably
1520 * nobody pays attention to such details. */
1521 if (!light
->falloff
)
1523 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1524 * equations have the falloff resp. exponent parameter as an
1525 * exponent, so the spot light lighting will always be 1.0 for
1526 * both of them, and we don't have to care for the rest of the
1527 * rather complex calculation. */
1528 object
->exponent
= 0.0f
;
1532 rho
= light
->theta
+ (light
->phi
- light
->theta
) / (2 * light
->falloff
);
1535 object
->exponent
= -0.3f
/ logf(cosf(rho
/ 2));
1538 if (object
->exponent
> 128.0f
)
1539 object
->exponent
= 128.0f
;
1541 object
->cutoff
= (float)(light
->phi
* 90 / M_PI
);
1545 case WINED3D_LIGHT_PARALLELPOINT
:
1546 object
->position
.x
= light
->position
.x
;
1547 object
->position
.y
= light
->position
.y
;
1548 object
->position
.z
= light
->position
.z
;
1549 object
->position
.w
= 1.0f
;
1553 FIXME("Unrecognized light type %#x.\n", light
->type
);
1556 wined3d_cs_emit_set_light(device
->cs
, object
);
1559 static void wined3d_device_set_light_enable(struct wined3d_device
*device
, UINT light_idx
, BOOL enable
)
1561 struct wined3d_light_info
*light_info
;
1563 TRACE("device %p, light_idx %u, enable %#x.\n", device
, light_idx
, enable
);
1565 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1566 if (!(light_info
= wined3d_light_state_get_light(&device
->state
.light_state
, light_idx
)))
1568 TRACE("Light enabled requested but light not defined, so defining one!\n");
1569 wined3d_device_set_light(device
, light_idx
, &WINED3D_default_light
);
1571 if (!(light_info
= wined3d_light_state_get_light(&device
->state
.light_state
, light_idx
)))
1573 FIXME("Adding default lights has failed dismally\n");
1578 wined3d_light_state_enable_light(&device
->state
.light_state
, &device
->adapter
->d3d_info
, light_info
, enable
);
1579 wined3d_cs_emit_set_light_enable(device
->cs
, light_idx
, enable
);
1582 static HRESULT
wined3d_device_set_clip_plane(struct wined3d_device
*device
,
1583 UINT plane_idx
, const struct wined3d_vec4
*plane
)
1585 TRACE("device %p, plane_idx %u, plane %p.\n", device
, plane_idx
, plane
);
1587 if (plane_idx
>= device
->adapter
->d3d_info
.limits
.max_clip_distances
)
1589 TRACE("Application has requested clipplane this device doesn't support.\n");
1590 return WINED3DERR_INVALIDCALL
;
1593 if (!memcmp(&device
->state
.clip_planes
[plane_idx
], plane
, sizeof(*plane
)))
1595 TRACE("Application is setting old values over, nothing to do.\n");
1599 device
->state
.clip_planes
[plane_idx
] = *plane
;
1601 wined3d_cs_emit_set_clip_plane(device
->cs
, plane_idx
, plane
);
1606 HRESULT CDECL
wined3d_device_set_clip_status(struct wined3d_device
*device
,
1607 const struct wined3d_clip_status
*clip_status
)
1609 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
1612 return WINED3DERR_INVALIDCALL
;
1617 HRESULT CDECL
wined3d_device_get_clip_status(const struct wined3d_device
*device
,
1618 struct wined3d_clip_status
*clip_status
)
1620 FIXME("device %p, clip_status %p stub!\n", device
, clip_status
);
1623 return WINED3DERR_INVALIDCALL
;
1628 static void wined3d_device_set_material(struct wined3d_device
*device
, const struct wined3d_material
*material
)
1630 TRACE("device %p, material %p.\n", device
, material
);
1632 device
->state
.material
= *material
;
1633 wined3d_cs_emit_set_material(device
->cs
, material
);
1636 void CDECL
wined3d_device_set_index_buffer(struct wined3d_device
*device
,
1637 struct wined3d_buffer
*buffer
, enum wined3d_format_id format_id
, unsigned int offset
)
1639 enum wined3d_format_id prev_format
;
1640 struct wined3d_buffer
*prev_buffer
;
1641 unsigned int prev_offset
;
1643 TRACE("device %p, buffer %p, format %s, offset %u.\n",
1644 device
, buffer
, debug_d3dformat(format_id
), offset
);
1646 prev_buffer
= device
->state
.index_buffer
;
1647 prev_format
= device
->state
.index_format
;
1648 prev_offset
= device
->state
.index_offset
;
1650 if (prev_buffer
== buffer
&& prev_format
== format_id
&& prev_offset
== offset
)
1654 wined3d_buffer_incref(buffer
);
1655 device
->state
.index_buffer
= buffer
;
1656 device
->state
.index_format
= format_id
;
1657 device
->state
.index_offset
= offset
;
1658 wined3d_cs_emit_set_index_buffer(device
->cs
, buffer
, format_id
, offset
);
1660 wined3d_buffer_decref(prev_buffer
);
1663 struct wined3d_buffer
* CDECL
wined3d_device_get_index_buffer(const struct wined3d_device
*device
,
1664 enum wined3d_format_id
*format
, unsigned int *offset
)
1666 TRACE("device %p, format %p, offset %p.\n", device
, format
, offset
);
1668 *format
= device
->state
.index_format
;
1670 *offset
= device
->state
.index_offset
;
1671 return device
->state
.index_buffer
;
1674 void CDECL
wined3d_device_set_base_vertex_index(struct wined3d_device
*device
, INT base_index
)
1676 TRACE("device %p, base_index %d.\n", device
, base_index
);
1678 device
->state
.base_vertex_index
= base_index
;
1681 void CDECL
wined3d_device_set_viewports(struct wined3d_device
*device
, unsigned int viewport_count
,
1682 const struct wined3d_viewport
*viewports
)
1686 TRACE("device %p, viewport_count %u, viewports %p.\n", device
, viewport_count
, viewports
);
1688 for (i
= 0; i
< viewport_count
; ++i
)
1690 TRACE("%u: x %.8e, y %.8e, w %.8e, h %.8e, min_z %.8e, max_z %.8e.\n", i
, viewports
[i
].x
, viewports
[i
].y
,
1691 viewports
[i
].width
, viewports
[i
].height
, viewports
[i
].min_z
, viewports
[i
].max_z
);
1695 memcpy(device
->state
.viewports
, viewports
, viewport_count
* sizeof(*viewports
));
1697 memset(device
->state
.viewports
, 0, sizeof(device
->state
.viewports
));
1698 device
->state
.viewport_count
= viewport_count
;
1700 wined3d_cs_emit_set_viewports(device
->cs
, viewport_count
, viewports
);
1703 void CDECL
wined3d_device_get_viewports(const struct wined3d_device
*device
, unsigned int *viewport_count
,
1704 struct wined3d_viewport
*viewports
)
1708 TRACE("device %p, viewport_count %p, viewports %p.\n", device
, viewport_count
, viewports
);
1710 count
= viewport_count
? min(*viewport_count
, device
->state
.viewport_count
) : 1;
1711 if (count
&& viewports
)
1712 memcpy(viewports
, device
->state
.viewports
, count
* sizeof(*viewports
));
1714 *viewport_count
= device
->state
.viewport_count
;
1717 static void resolve_depth_buffer(struct wined3d_device
*device
)
1719 const struct wined3d_state
*state
= &device
->state
;
1720 struct wined3d_rendertarget_view
*src_view
;
1721 struct wined3d_resource
*dst_resource
;
1722 struct wined3d_texture
*dst_texture
;
1724 if (!(dst_texture
= state
->textures
[0]))
1726 dst_resource
= &dst_texture
->resource
;
1727 if (!dst_resource
->format
->depth_size
)
1729 if (!(src_view
= state
->fb
.depth_stencil
))
1732 wined3d_device_resolve_sub_resource(device
, dst_resource
, 0,
1733 src_view
->resource
, src_view
->sub_resource_idx
, dst_resource
->format
->id
);
1736 void CDECL
wined3d_device_set_blend_state(struct wined3d_device
*device
,
1737 struct wined3d_blend_state
*blend_state
, const struct wined3d_color
*blend_factor
, unsigned int sample_mask
)
1739 struct wined3d_state
*state
= &device
->state
;
1740 struct wined3d_blend_state
*prev
;
1742 TRACE("device %p, blend_state %p, blend_factor %s, sample_mask %#x.\n",
1743 device
, blend_state
, debug_color(blend_factor
), sample_mask
);
1745 prev
= state
->blend_state
;
1746 if (prev
== blend_state
&& !memcmp(blend_factor
, &state
->blend_factor
, sizeof(*blend_factor
))
1747 && sample_mask
== state
->sample_mask
)
1751 wined3d_blend_state_incref(blend_state
);
1752 state
->blend_state
= blend_state
;
1753 state
->blend_factor
= *blend_factor
;
1754 state
->sample_mask
= sample_mask
;
1755 wined3d_cs_emit_set_blend_state(device
->cs
, blend_state
, blend_factor
, sample_mask
);
1757 wined3d_blend_state_decref(prev
);
1760 struct wined3d_blend_state
* CDECL
wined3d_device_get_blend_state(const struct wined3d_device
*device
,
1761 struct wined3d_color
*blend_factor
, unsigned int *sample_mask
)
1763 const struct wined3d_state
*state
= &device
->state
;
1765 TRACE("device %p, blend_factor %p, sample_mask %p.\n", device
, blend_factor
, sample_mask
);
1767 *blend_factor
= state
->blend_factor
;
1768 *sample_mask
= state
->sample_mask
;
1769 return state
->blend_state
;
1772 void CDECL
wined3d_device_set_depth_stencil_state(struct wined3d_device
*device
,
1773 struct wined3d_depth_stencil_state
*state
)
1775 struct wined3d_depth_stencil_state
*prev
;
1777 TRACE("device %p, state %p.\n", device
, state
);
1779 prev
= device
->state
.depth_stencil_state
;
1784 wined3d_depth_stencil_state_incref(state
);
1785 device
->state
.depth_stencil_state
= state
;
1786 wined3d_cs_emit_set_depth_stencil_state(device
->cs
, state
);
1788 wined3d_depth_stencil_state_decref(prev
);
1791 struct wined3d_depth_stencil_state
* CDECL
wined3d_device_get_depth_stencil_state(const struct wined3d_device
*device
)
1793 TRACE("device %p.\n", device
);
1795 return device
->state
.depth_stencil_state
;
1798 void CDECL
wined3d_device_set_rasterizer_state(struct wined3d_device
*device
,
1799 struct wined3d_rasterizer_state
*rasterizer_state
)
1801 struct wined3d_rasterizer_state
*prev
;
1803 TRACE("device %p, rasterizer_state %p.\n", device
, rasterizer_state
);
1805 prev
= device
->state
.rasterizer_state
;
1806 if (prev
== rasterizer_state
)
1809 if (rasterizer_state
)
1810 wined3d_rasterizer_state_incref(rasterizer_state
);
1811 device
->state
.rasterizer_state
= rasterizer_state
;
1812 wined3d_cs_emit_set_rasterizer_state(device
->cs
, rasterizer_state
);
1814 wined3d_rasterizer_state_decref(prev
);
1817 struct wined3d_rasterizer_state
* CDECL
wined3d_device_get_rasterizer_state(struct wined3d_device
*device
)
1819 TRACE("device %p.\n", device
);
1821 return device
->state
.rasterizer_state
;
1824 void CDECL
wined3d_device_set_render_state(struct wined3d_device
*device
,
1825 enum wined3d_render_state state
, DWORD value
)
1827 TRACE("device %p, state %s (%#x), value %#x.\n", device
, debug_d3drenderstate(state
), state
, value
);
1829 if (state
> WINEHIGHEST_RENDER_STATE
)
1831 WARN("Unhandled render state %#x.\n", state
);
1835 if (value
== device
->state
.render_states
[state
])
1836 TRACE("Application is setting the old value over, nothing to do.\n");
1839 device
->state
.render_states
[state
] = value
;
1840 wined3d_cs_emit_set_render_state(device
->cs
, state
, value
);
1843 if (state
== WINED3D_RS_POINTSIZE
&& value
== WINED3D_RESZ_CODE
)
1845 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
1846 resolve_depth_buffer(device
);
1850 DWORD CDECL
wined3d_device_get_render_state(const struct wined3d_device
*device
, enum wined3d_render_state state
)
1852 TRACE("device %p, state %s (%#x).\n", device
, debug_d3drenderstate(state
), state
);
1854 return device
->state
.render_states
[state
];
1857 static void wined3d_device_set_sampler_state(struct wined3d_device
*device
,
1858 UINT sampler_idx
, enum wined3d_sampler_state state
, DWORD value
)
1860 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
1861 device
, sampler_idx
, debug_d3dsamplerstate(state
), value
);
1863 if (value
== device
->state
.sampler_states
[sampler_idx
][state
])
1865 TRACE("Application is setting the old value over, nothing to do.\n");
1869 device
->state
.sampler_states
[sampler_idx
][state
] = value
;
1870 wined3d_cs_emit_set_sampler_state(device
->cs
, sampler_idx
, state
, value
);
1873 void CDECL
wined3d_device_set_scissor_rects(struct wined3d_device
*device
, unsigned int rect_count
,
1878 TRACE("device %p, rect_count %u, rects %p.\n", device
, rect_count
, rects
);
1880 for (i
= 0; i
< rect_count
; ++i
)
1882 TRACE("%u: %s\n", i
, wine_dbgstr_rect(&rects
[i
]));
1885 if (device
->state
.scissor_rect_count
== rect_count
1886 && !memcmp(device
->state
.scissor_rects
, rects
, rect_count
* sizeof(*rects
)))
1888 TRACE("App is setting the old scissor rectangles over, nothing to do.\n");
1893 memcpy(device
->state
.scissor_rects
, rects
, rect_count
* sizeof(*rects
));
1895 memset(device
->state
.scissor_rects
, 0, sizeof(device
->state
.scissor_rects
));
1896 device
->state
.scissor_rect_count
= rect_count
;
1898 wined3d_cs_emit_set_scissor_rects(device
->cs
, rect_count
, rects
);
1901 void CDECL
wined3d_device_get_scissor_rects(const struct wined3d_device
*device
, unsigned int *rect_count
, RECT
*rects
)
1905 TRACE("device %p, rect_count %p, rects %p.\n", device
, rect_count
, rects
);
1907 count
= rect_count
? min(*rect_count
, device
->state
.scissor_rect_count
) : 1;
1909 memcpy(rects
, device
->state
.scissor_rects
, count
* sizeof(*rects
));
1911 *rect_count
= device
->state
.scissor_rect_count
;
1914 void CDECL
wined3d_device_set_vertex_declaration(struct wined3d_device
*device
,
1915 struct wined3d_vertex_declaration
*declaration
)
1917 struct wined3d_vertex_declaration
*prev
= device
->state
.vertex_declaration
;
1919 TRACE("device %p, declaration %p.\n", device
, declaration
);
1921 if (declaration
== prev
)
1925 wined3d_vertex_declaration_incref(declaration
);
1926 device
->state
.vertex_declaration
= declaration
;
1927 wined3d_cs_emit_set_vertex_declaration(device
->cs
, declaration
);
1929 wined3d_vertex_declaration_decref(prev
);
1932 struct wined3d_vertex_declaration
* CDECL
wined3d_device_get_vertex_declaration(const struct wined3d_device
*device
)
1934 TRACE("device %p.\n", device
);
1936 return device
->state
.vertex_declaration
;
1939 void CDECL
wined3d_device_set_vertex_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
1941 struct wined3d_shader
*prev
= device
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
];
1943 TRACE("device %p, shader %p.\n", device
, shader
);
1949 wined3d_shader_incref(shader
);
1950 device
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
] = shader
;
1951 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_VERTEX
, shader
);
1953 wined3d_shader_decref(prev
);
1956 struct wined3d_shader
* CDECL
wined3d_device_get_vertex_shader(const struct wined3d_device
*device
)
1958 TRACE("device %p.\n", device
);
1960 return device
->state
.shader
[WINED3D_SHADER_TYPE_VERTEX
];
1963 void CDECL
wined3d_device_set_constant_buffer(struct wined3d_device
*device
,
1964 enum wined3d_shader_type type
, UINT idx
, struct wined3d_buffer
*buffer
)
1966 struct wined3d_buffer
*prev
;
1968 TRACE("device %p, type %#x, idx %u, buffer %p.\n", device
, type
, idx
, buffer
);
1970 if (idx
>= MAX_CONSTANT_BUFFERS
)
1972 WARN("Invalid constant buffer index %u.\n", idx
);
1976 prev
= device
->state
.cb
[type
][idx
];
1981 wined3d_buffer_incref(buffer
);
1982 device
->state
.cb
[type
][idx
] = buffer
;
1983 wined3d_cs_emit_set_constant_buffer(device
->cs
, type
, idx
, buffer
);
1985 wined3d_buffer_decref(prev
);
1988 struct wined3d_buffer
* CDECL
wined3d_device_get_constant_buffer(const struct wined3d_device
*device
,
1989 enum wined3d_shader_type shader_type
, unsigned int idx
)
1991 TRACE("device %p, shader_type %#x, idx %u.\n", device
, shader_type
, idx
);
1993 if (idx
>= MAX_CONSTANT_BUFFERS
)
1995 WARN("Invalid constant buffer index %u.\n", idx
);
1999 return device
->state
.cb
[shader_type
][idx
];
2002 static void wined3d_device_set_shader_resource_view(struct wined3d_device
*device
,
2003 enum wined3d_shader_type type
, UINT idx
, struct wined3d_shader_resource_view
*view
)
2005 const struct wined3d_rendertarget_view
*dsv
;
2006 struct wined3d_shader_resource_view
*prev
;
2008 if (idx
>= MAX_SHADER_RESOURCE_VIEWS
)
2010 WARN("Invalid view index %u.\n", idx
);
2014 prev
= device
->state
.shader_resource_view
[type
][idx
];
2018 if (view
&& (wined3d_is_srv_rtv_bound(view
)
2019 || ((dsv
= device
->state
.fb
.depth_stencil
)
2020 && dsv
->resource
== view
->resource
&& wined3d_dsv_srv_conflict(dsv
, view
->format
))))
2022 WARN("Application is trying to bind resource which is attached as render target.\n");
2028 wined3d_shader_resource_view_incref(view
);
2029 wined3d_srv_bind_count_inc(view
);
2032 device
->state
.shader_resource_view
[type
][idx
] = view
;
2033 wined3d_cs_emit_set_shader_resource_view(device
->cs
, type
, idx
, view
);
2036 wined3d_srv_bind_count_dec(prev
);
2037 wined3d_shader_resource_view_decref(prev
);
2041 void CDECL
wined3d_device_set_vs_resource_view(struct wined3d_device
*device
,
2042 UINT idx
, struct wined3d_shader_resource_view
*view
)
2044 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2046 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, view
);
2049 static struct wined3d_shader_resource_view
*wined3d_device_get_shader_resource_view(
2050 const struct wined3d_device
*device
, enum wined3d_shader_type shader_type
, unsigned int idx
)
2052 if (idx
>= MAX_SHADER_RESOURCE_VIEWS
)
2054 WARN("Invalid view index %u.\n", idx
);
2058 return device
->state
.shader_resource_view
[shader_type
][idx
];
2061 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_vs_resource_view(const struct wined3d_device
*device
,
2064 TRACE("device %p, idx %u.\n", device
, idx
);
2066 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2069 static void wined3d_device_set_sampler(struct wined3d_device
*device
,
2070 enum wined3d_shader_type type
, UINT idx
, struct wined3d_sampler
*sampler
)
2072 struct wined3d_sampler
*prev
;
2074 if (idx
>= MAX_SAMPLER_OBJECTS
)
2076 WARN("Invalid sampler index %u.\n", idx
);
2080 prev
= device
->state
.sampler
[type
][idx
];
2081 if (sampler
== prev
)
2085 wined3d_sampler_incref(sampler
);
2086 device
->state
.sampler
[type
][idx
] = sampler
;
2087 wined3d_cs_emit_set_sampler(device
->cs
, type
, idx
, sampler
);
2089 wined3d_sampler_decref(prev
);
2092 void CDECL
wined3d_device_set_vs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2094 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2096 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
, sampler
);
2099 static struct wined3d_sampler
*wined3d_device_get_sampler(const struct wined3d_device
*device
,
2100 enum wined3d_shader_type shader_type
, unsigned int idx
)
2102 if (idx
>= MAX_SAMPLER_OBJECTS
)
2104 WARN("Invalid sampler index %u.\n", idx
);
2108 return device
->state
.sampler
[shader_type
][idx
];
2111 struct wined3d_sampler
* CDECL
wined3d_device_get_vs_sampler(const struct wined3d_device
*device
, UINT idx
)
2113 TRACE("device %p, idx %u.\n", device
, idx
);
2115 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_VERTEX
, idx
);
2118 static void wined3d_device_set_vs_consts_b(struct wined3d_device
*device
,
2119 unsigned int start_idx
, unsigned int count
, const BOOL
*constants
)
2123 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2124 device
, start_idx
, count
, constants
);
2126 memcpy(&device
->state
.vs_consts_b
[start_idx
], constants
, count
* sizeof(*constants
));
2129 for (i
= 0; i
< count
; ++i
)
2130 TRACE("Set BOOL constant %u to %#x.\n", start_idx
+ i
, constants
[i
]);
2133 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_B
, start_idx
, count
, constants
);
2136 static void wined3d_device_set_vs_consts_i(struct wined3d_device
*device
,
2137 unsigned int start_idx
, unsigned int count
, const struct wined3d_ivec4
*constants
)
2141 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2142 device
, start_idx
, count
, constants
);
2144 memcpy(&device
->state
.vs_consts_i
[start_idx
], constants
, count
* sizeof(*constants
));
2147 for (i
= 0; i
< count
; ++i
)
2148 TRACE("Set ivec4 constant %u to %s.\n", start_idx
+ i
, debug_ivec4(&constants
[i
]));
2151 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_I
, start_idx
, count
, constants
);
2154 static void wined3d_device_set_vs_consts_f(struct wined3d_device
*device
,
2155 unsigned int start_idx
, unsigned int count
, const struct wined3d_vec4
*constants
)
2159 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2160 device
, start_idx
, count
, constants
);
2162 memcpy(&device
->state
.vs_consts_f
[start_idx
], constants
, count
* sizeof(*constants
));
2165 for (i
= 0; i
< count
; ++i
)
2166 TRACE("Set vec4 constant %u to %s.\n", start_idx
+ i
, debug_vec4(&constants
[i
]));
2169 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_VS_F
, start_idx
, count
, constants
);
2172 void CDECL
wined3d_device_set_pixel_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2174 struct wined3d_shader
*prev
= device
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
];
2176 TRACE("device %p, shader %p.\n", device
, shader
);
2182 wined3d_shader_incref(shader
);
2183 device
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
] = shader
;
2184 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_PIXEL
, shader
);
2186 wined3d_shader_decref(prev
);
2189 struct wined3d_shader
* CDECL
wined3d_device_get_pixel_shader(const struct wined3d_device
*device
)
2191 TRACE("device %p.\n", device
);
2193 return device
->state
.shader
[WINED3D_SHADER_TYPE_PIXEL
];
2196 void CDECL
wined3d_device_set_ps_resource_view(struct wined3d_device
*device
,
2197 UINT idx
, struct wined3d_shader_resource_view
*view
)
2199 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2201 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, view
);
2204 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_ps_resource_view(const struct wined3d_device
*device
,
2207 TRACE("device %p, idx %u.\n", device
, idx
);
2209 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2212 void CDECL
wined3d_device_set_ps_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2214 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2216 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
, sampler
);
2219 struct wined3d_sampler
* CDECL
wined3d_device_get_ps_sampler(const struct wined3d_device
*device
, UINT idx
)
2221 TRACE("device %p, idx %u.\n", device
, idx
);
2223 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_PIXEL
, idx
);
2226 static void wined3d_device_set_ps_consts_b(struct wined3d_device
*device
,
2227 unsigned int start_idx
, unsigned int count
, const BOOL
*constants
)
2231 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2232 device
, start_idx
, count
, constants
);
2234 memcpy(&device
->state
.ps_consts_b
[start_idx
], constants
, count
* sizeof(*constants
));
2237 for (i
= 0; i
< count
; ++i
)
2238 TRACE("Set BOOL constant %u to %#x.\n", start_idx
+ i
, constants
[i
]);
2241 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_B
, start_idx
, count
, constants
);
2244 static void wined3d_device_set_ps_consts_i(struct wined3d_device
*device
,
2245 unsigned int start_idx
, unsigned int count
, const struct wined3d_ivec4
*constants
)
2249 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2250 device
, start_idx
, count
, constants
);
2252 memcpy(&device
->state
.ps_consts_i
[start_idx
], constants
, count
* sizeof(*constants
));
2255 for (i
= 0; i
< count
; ++i
)
2256 TRACE("Set ivec4 constant %u to %s.\n", start_idx
+ i
, debug_ivec4(&constants
[i
]));
2259 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_I
, start_idx
, count
, constants
);
2262 static void wined3d_device_set_ps_consts_f(struct wined3d_device
*device
,
2263 unsigned int start_idx
, unsigned int count
, const struct wined3d_vec4
*constants
)
2267 TRACE("device %p, start_idx %u, count %u, constants %p.\n",
2268 device
, start_idx
, count
, constants
);
2270 memcpy(&device
->state
.ps_consts_f
[start_idx
], constants
, count
* sizeof(*constants
));
2273 for (i
= 0; i
< count
; ++i
)
2274 TRACE("Set vec4 constant %u to %s.\n", start_idx
+ i
, debug_vec4(&constants
[i
]));
2277 wined3d_cs_push_constants(device
->cs
, WINED3D_PUSH_CONSTANTS_PS_F
, start_idx
, count
, constants
);
2280 void CDECL
wined3d_device_set_hull_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2282 struct wined3d_shader
*prev
;
2284 TRACE("device %p, shader %p.\n", device
, shader
);
2286 prev
= device
->state
.shader
[WINED3D_SHADER_TYPE_HULL
];
2290 wined3d_shader_incref(shader
);
2291 device
->state
.shader
[WINED3D_SHADER_TYPE_HULL
] = shader
;
2292 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_HULL
, shader
);
2294 wined3d_shader_decref(prev
);
2297 struct wined3d_shader
* CDECL
wined3d_device_get_hull_shader(const struct wined3d_device
*device
)
2299 TRACE("device %p.\n", device
);
2301 return device
->state
.shader
[WINED3D_SHADER_TYPE_HULL
];
2304 void CDECL
wined3d_device_set_hs_resource_view(struct wined3d_device
*device
,
2305 unsigned int idx
, struct wined3d_shader_resource_view
*view
)
2307 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2309 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_HULL
, idx
, view
);
2312 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_hs_resource_view(const struct wined3d_device
*device
,
2315 TRACE("device %p, idx %u.\n", device
, idx
);
2317 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_HULL
, idx
);
2320 void CDECL
wined3d_device_set_hs_sampler(struct wined3d_device
*device
,
2321 unsigned int idx
, struct wined3d_sampler
*sampler
)
2323 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2325 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_HULL
, idx
, sampler
);
2328 struct wined3d_sampler
* CDECL
wined3d_device_get_hs_sampler(const struct wined3d_device
*device
, unsigned int idx
)
2330 TRACE("device %p, idx %u.\n", device
, idx
);
2332 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_HULL
, idx
);
2335 void CDECL
wined3d_device_set_domain_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2337 struct wined3d_shader
*prev
;
2339 TRACE("device %p, shader %p.\n", device
, shader
);
2341 prev
= device
->state
.shader
[WINED3D_SHADER_TYPE_DOMAIN
];
2345 wined3d_shader_incref(shader
);
2346 device
->state
.shader
[WINED3D_SHADER_TYPE_DOMAIN
] = shader
;
2347 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_DOMAIN
, shader
);
2349 wined3d_shader_decref(prev
);
2352 struct wined3d_shader
* CDECL
wined3d_device_get_domain_shader(const struct wined3d_device
*device
)
2354 TRACE("device %p.\n", device
);
2356 return device
->state
.shader
[WINED3D_SHADER_TYPE_DOMAIN
];
2359 void CDECL
wined3d_device_set_ds_resource_view(struct wined3d_device
*device
,
2360 unsigned int idx
, struct wined3d_shader_resource_view
*view
)
2362 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2364 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
, view
);
2367 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_ds_resource_view(const struct wined3d_device
*device
,
2370 TRACE("device %p, idx %u.\n", device
, idx
);
2372 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
);
2375 void CDECL
wined3d_device_set_ds_sampler(struct wined3d_device
*device
,
2376 unsigned int idx
, struct wined3d_sampler
*sampler
)
2378 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2380 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
, sampler
);
2383 struct wined3d_sampler
* CDECL
wined3d_device_get_ds_sampler(const struct wined3d_device
*device
, unsigned int idx
)
2385 TRACE("device %p, idx %u.\n", device
, idx
);
2387 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_DOMAIN
, idx
);
2390 void CDECL
wined3d_device_set_geometry_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2392 struct wined3d_shader
*prev
= device
->state
.shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2394 TRACE("device %p, shader %p.\n", device
, shader
);
2399 wined3d_shader_incref(shader
);
2400 device
->state
.shader
[WINED3D_SHADER_TYPE_GEOMETRY
] = shader
;
2401 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_GEOMETRY
, shader
);
2403 wined3d_shader_decref(prev
);
2406 struct wined3d_shader
* CDECL
wined3d_device_get_geometry_shader(const struct wined3d_device
*device
)
2408 TRACE("device %p.\n", device
);
2410 return device
->state
.shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
2413 void CDECL
wined3d_device_set_gs_resource_view(struct wined3d_device
*device
,
2414 UINT idx
, struct wined3d_shader_resource_view
*view
)
2416 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2418 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, view
);
2421 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_gs_resource_view(const struct wined3d_device
*device
,
2424 TRACE("device %p, idx %u.\n", device
, idx
);
2426 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2429 void CDECL
wined3d_device_set_gs_sampler(struct wined3d_device
*device
, UINT idx
, struct wined3d_sampler
*sampler
)
2431 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2433 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
, sampler
);
2436 struct wined3d_sampler
* CDECL
wined3d_device_get_gs_sampler(const struct wined3d_device
*device
, UINT idx
)
2438 TRACE("device %p, idx %u.\n", device
, idx
);
2440 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_GEOMETRY
, idx
);
2443 void CDECL
wined3d_device_set_compute_shader(struct wined3d_device
*device
, struct wined3d_shader
*shader
)
2445 struct wined3d_shader
*prev
;
2447 TRACE("device %p, shader %p.\n", device
, shader
);
2449 prev
= device
->state
.shader
[WINED3D_SHADER_TYPE_COMPUTE
];
2453 wined3d_shader_incref(shader
);
2454 device
->state
.shader
[WINED3D_SHADER_TYPE_COMPUTE
] = shader
;
2455 wined3d_cs_emit_set_shader(device
->cs
, WINED3D_SHADER_TYPE_COMPUTE
, shader
);
2457 wined3d_shader_decref(prev
);
2460 struct wined3d_shader
* CDECL
wined3d_device_get_compute_shader(const struct wined3d_device
*device
)
2462 TRACE("device %p.\n", device
);
2464 return device
->state
.shader
[WINED3D_SHADER_TYPE_COMPUTE
];
2467 void CDECL
wined3d_device_set_cs_resource_view(struct wined3d_device
*device
,
2468 unsigned int idx
, struct wined3d_shader_resource_view
*view
)
2470 TRACE("device %p, idx %u, view %p.\n", device
, idx
, view
);
2472 wined3d_device_set_shader_resource_view(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, view
);
2475 struct wined3d_shader_resource_view
* CDECL
wined3d_device_get_cs_resource_view(const struct wined3d_device
*device
,
2478 TRACE("device %p, idx %u.\n", device
, idx
);
2480 return wined3d_device_get_shader_resource_view(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2483 void CDECL
wined3d_device_set_cs_sampler(struct wined3d_device
*device
,
2484 unsigned int idx
, struct wined3d_sampler
*sampler
)
2486 TRACE("device %p, idx %u, sampler %p.\n", device
, idx
, sampler
);
2488 wined3d_device_set_sampler(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
, sampler
);
2491 struct wined3d_sampler
* CDECL
wined3d_device_get_cs_sampler(const struct wined3d_device
*device
, unsigned int idx
)
2493 TRACE("device %p, idx %u.\n", device
, idx
);
2495 return wined3d_device_get_sampler(device
, WINED3D_SHADER_TYPE_COMPUTE
, idx
);
2498 static void wined3d_device_set_pipeline_unordered_access_view(struct wined3d_device
*device
,
2499 enum wined3d_pipeline pipeline
, unsigned int idx
, struct wined3d_unordered_access_view
*uav
,
2500 unsigned int initial_count
)
2502 struct wined3d_unordered_access_view
*prev
;
2504 if (idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
2506 WARN("Invalid UAV index %u.\n", idx
);
2510 prev
= device
->state
.unordered_access_view
[pipeline
][idx
];
2511 if (uav
== prev
&& initial_count
== ~0u)
2515 wined3d_unordered_access_view_incref(uav
);
2516 device
->state
.unordered_access_view
[pipeline
][idx
] = uav
;
2517 wined3d_cs_emit_set_unordered_access_view(device
->cs
, pipeline
, idx
, uav
, initial_count
);
2519 wined3d_unordered_access_view_decref(prev
);
2522 static struct wined3d_unordered_access_view
*wined3d_device_get_pipeline_unordered_access_view(
2523 const struct wined3d_device
*device
, enum wined3d_pipeline pipeline
, unsigned int idx
)
2525 if (idx
>= MAX_UNORDERED_ACCESS_VIEWS
)
2527 WARN("Invalid UAV index %u.\n", idx
);
2531 return device
->state
.unordered_access_view
[pipeline
][idx
];
2534 void CDECL
wined3d_device_set_cs_uav(struct wined3d_device
*device
, unsigned int idx
,
2535 struct wined3d_unordered_access_view
*uav
, unsigned int initial_count
)
2537 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device
, idx
, uav
, initial_count
);
2539 wined3d_device_set_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_COMPUTE
, idx
, uav
, initial_count
);
2542 struct wined3d_unordered_access_view
* CDECL
wined3d_device_get_cs_uav(const struct wined3d_device
*device
,
2545 TRACE("device %p, idx %u.\n", device
, idx
);
2547 return wined3d_device_get_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_COMPUTE
, idx
);
2550 void CDECL
wined3d_device_set_unordered_access_view(struct wined3d_device
*device
,
2551 unsigned int idx
, struct wined3d_unordered_access_view
*uav
, unsigned int initial_count
)
2553 TRACE("device %p, idx %u, uav %p, initial_count %#x.\n", device
, idx
, uav
, initial_count
);
2555 wined3d_device_set_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_GRAPHICS
, idx
, uav
, initial_count
);
2558 struct wined3d_unordered_access_view
* CDECL
wined3d_device_get_unordered_access_view(
2559 const struct wined3d_device
*device
, unsigned int idx
)
2561 TRACE("device %p, idx %u.\n", device
, idx
);
2563 return wined3d_device_get_pipeline_unordered_access_view(device
, WINED3D_PIPELINE_GRAPHICS
, idx
);
2566 void CDECL
wined3d_device_set_max_frame_latency(struct wined3d_device
*device
, unsigned int latency
)
2573 device
->max_frame_latency
= latency
;
2574 for (i
= 0; i
< device
->swapchain_count
; ++i
)
2575 swapchain_set_max_frame_latency(device
->swapchains
[i
], device
);
2578 unsigned int CDECL
wined3d_device_get_max_frame_latency(const struct wined3d_device
*device
)
2580 return device
->max_frame_latency
;
2583 static unsigned int wined3d_get_flexible_vertex_size(DWORD fvf
)
2585 unsigned int texcoord_count
= (fvf
& WINED3DFVF_TEXCOUNT_MASK
) >> WINED3DFVF_TEXCOUNT_SHIFT
;
2586 unsigned int i
, size
= 0;
2588 if (fvf
& WINED3DFVF_NORMAL
) size
+= 3 * sizeof(float);
2589 if (fvf
& WINED3DFVF_DIFFUSE
) size
+= sizeof(DWORD
);
2590 if (fvf
& WINED3DFVF_SPECULAR
) size
+= sizeof(DWORD
);
2591 if (fvf
& WINED3DFVF_PSIZE
) size
+= sizeof(DWORD
);
2592 switch (fvf
& WINED3DFVF_POSITION_MASK
)
2594 case WINED3DFVF_XYZ
: size
+= 3 * sizeof(float); break;
2595 case WINED3DFVF_XYZRHW
: size
+= 4 * sizeof(float); break;
2596 case WINED3DFVF_XYZB1
: size
+= 4 * sizeof(float); break;
2597 case WINED3DFVF_XYZB2
: size
+= 5 * sizeof(float); break;
2598 case WINED3DFVF_XYZB3
: size
+= 6 * sizeof(float); break;
2599 case WINED3DFVF_XYZB4
: size
+= 7 * sizeof(float); break;
2600 case WINED3DFVF_XYZB5
: size
+= 8 * sizeof(float); break;
2601 case WINED3DFVF_XYZW
: size
+= 4 * sizeof(float); break;
2602 default: FIXME("Unexpected position mask %#x.\n", fvf
& WINED3DFVF_POSITION_MASK
);
2604 for (i
= 0; i
< texcoord_count
; ++i
)
2606 size
+= GET_TEXCOORD_SIZE_FROM_FVF(fvf
, i
) * sizeof(float);
2612 static void wined3d_format_get_colour(const struct wined3d_format
*format
,
2613 const void *data
, struct wined3d_color
*colour
)
2615 float *output
= &colour
->r
;
2616 const uint32_t *u32_data
;
2617 const uint16_t *u16_data
;
2618 const float *f32_data
;
2621 static const struct wined3d_color default_colour
= {0.0f
, 0.0f
, 0.0f
, 1.0f
};
2622 static unsigned int warned
;
2626 case WINED3DFMT_B8G8R8A8_UNORM
:
2628 wined3d_color_from_d3dcolor(colour
, *u32_data
);
2631 case WINED3DFMT_R8G8B8A8_UNORM
:
2633 colour
->r
= (*u32_data
& 0xffu
) / 255.0f
;
2634 colour
->g
= ((*u32_data
>> 8) & 0xffu
) / 255.0f
;
2635 colour
->b
= ((*u32_data
>> 16) & 0xffu
) / 255.0f
;
2636 colour
->a
= ((*u32_data
>> 24) & 0xffu
) / 255.0f
;
2639 case WINED3DFMT_R16G16_UNORM
:
2640 case WINED3DFMT_R16G16B16A16_UNORM
:
2642 *colour
= default_colour
;
2643 for (i
= 0; i
< format
->component_count
; ++i
)
2644 output
[i
] = u16_data
[i
] / 65535.0f
;
2647 case WINED3DFMT_R32_FLOAT
:
2648 case WINED3DFMT_R32G32_FLOAT
:
2649 case WINED3DFMT_R32G32B32_FLOAT
:
2650 case WINED3DFMT_R32G32B32A32_FLOAT
:
2652 *colour
= default_colour
;
2653 for (i
= 0; i
< format
->component_count
; ++i
)
2654 output
[i
] = f32_data
[i
];
2658 *colour
= default_colour
;
2660 FIXME("Unhandled colour format conversion, format %s.\n", debug_d3dformat(format
->id
));
2665 static void wined3d_colour_from_mcs(struct wined3d_color
*colour
, enum wined3d_material_color_source mcs
,
2666 const struct wined3d_color
*material_colour
, unsigned int index
,
2667 const struct wined3d_stream_info
*stream_info
)
2669 const struct wined3d_stream_info_element
*element
= NULL
;
2673 case WINED3D_MCS_MATERIAL
:
2674 *colour
= *material_colour
;
2677 case WINED3D_MCS_COLOR1
:
2678 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_DIFFUSE
)))
2680 colour
->r
= colour
->g
= colour
->b
= colour
->a
= 1.0f
;
2683 element
= &stream_info
->elements
[WINED3D_FFP_DIFFUSE
];
2686 case WINED3D_MCS_COLOR2
:
2687 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_SPECULAR
)))
2689 colour
->r
= colour
->g
= colour
->b
= colour
->a
= 0.0f
;
2692 element
= &stream_info
->elements
[WINED3D_FFP_SPECULAR
];
2696 colour
->r
= colour
->g
= colour
->b
= colour
->a
= 0.0f
;
2697 ERR("Invalid material colour source %#x.\n", mcs
);
2701 wined3d_format_get_colour(element
->format
, &element
->data
.addr
[index
* element
->stride
], colour
);
2704 static float wined3d_clamp(float value
, float min_value
, float max_value
)
2706 return value
< min_value
? min_value
: value
> max_value
? max_value
: value
;
2709 static float wined3d_vec3_dot(const struct wined3d_vec3
*v0
, const struct wined3d_vec3
*v1
)
2711 return v0
->x
* v1
->x
+ v0
->y
* v1
->y
+ v0
->z
* v1
->z
;
2714 static void wined3d_vec3_subtract(struct wined3d_vec3
*v0
, const struct wined3d_vec3
*v1
)
2721 static void wined3d_vec3_scale(struct wined3d_vec3
*v
, float s
)
2728 static void wined3d_vec3_normalise(struct wined3d_vec3
*v
)
2730 float rnorm
= 1.0f
/ sqrtf(wined3d_vec3_dot(v
, v
));
2732 if (isfinite(rnorm
))
2733 wined3d_vec3_scale(v
, rnorm
);
2736 static void wined3d_vec3_transform(struct wined3d_vec3
*dst
,
2737 const struct wined3d_vec3
*v
, const struct wined3d_matrix_3x3
*m
)
2739 struct wined3d_vec3 tmp
;
2741 tmp
.x
= v
->x
* m
->_11
+ v
->y
* m
->_21
+ v
->z
* m
->_31
;
2742 tmp
.y
= v
->x
* m
->_12
+ v
->y
* m
->_22
+ v
->z
* m
->_32
;
2743 tmp
.z
= v
->x
* m
->_13
+ v
->y
* m
->_23
+ v
->z
* m
->_33
;
2748 static void wined3d_color_clamp(struct wined3d_color
*dst
, const struct wined3d_color
*src
,
2749 float min_value
, float max_value
)
2751 dst
->r
= wined3d_clamp(src
->r
, min_value
, max_value
);
2752 dst
->g
= wined3d_clamp(src
->g
, min_value
, max_value
);
2753 dst
->b
= wined3d_clamp(src
->b
, min_value
, max_value
);
2754 dst
->a
= wined3d_clamp(src
->a
, min_value
, max_value
);
2757 static void wined3d_color_rgb_mul_add(struct wined3d_color
*dst
, const struct wined3d_color
*src
, float c
)
2759 dst
->r
+= src
->r
* c
;
2760 dst
->g
+= src
->g
* c
;
2761 dst
->b
+= src
->b
* c
;
2764 static void init_transformed_lights(struct lights_settings
*ls
,
2765 const struct wined3d_state
*state
, BOOL legacy_lighting
, BOOL compute_lighting
)
2767 const struct wined3d_light_info
*lights
[WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS
];
2768 const struct wined3d_light_info
*light_info
;
2769 struct light_transformed
*light
;
2770 struct wined3d_vec4 vec4
;
2771 unsigned int light_count
;
2772 unsigned int i
, index
;
2774 memset(ls
, 0, sizeof(*ls
));
2776 ls
->lighting
= !!compute_lighting
;
2777 ls
->fog_mode
= state
->render_states
[WINED3D_RS_FOGVERTEXMODE
];
2778 ls
->fog_coord_mode
= state
->render_states
[WINED3D_RS_RANGEFOGENABLE
]
2779 ? WINED3D_FFP_VS_FOG_RANGE
: WINED3D_FFP_VS_FOG_DEPTH
;
2780 ls
->fog_start
= wined3d_get_float_state(state
, WINED3D_RS_FOGSTART
);
2781 ls
->fog_end
= wined3d_get_float_state(state
, WINED3D_RS_FOGEND
);
2782 ls
->fog_density
= wined3d_get_float_state(state
, WINED3D_RS_FOGDENSITY
);
2784 if (ls
->fog_mode
== WINED3D_FOG_NONE
&& !compute_lighting
)
2787 multiply_matrix(&ls
->modelview_matrix
, &state
->transforms
[WINED3D_TS_VIEW
],
2788 &state
->transforms
[WINED3D_TS_WORLD_MATRIX(0)]);
2790 if (!compute_lighting
)
2793 compute_normal_matrix(&ls
->normal_matrix
._11
, legacy_lighting
, &ls
->modelview_matrix
);
2795 wined3d_color_from_d3dcolor(&ls
->ambient_light
, state
->render_states
[WINED3D_RS_AMBIENT
]);
2796 ls
->legacy_lighting
= !!legacy_lighting
;
2797 ls
->normalise
= !!state
->render_states
[WINED3D_RS_NORMALIZENORMALS
];
2798 ls
->localviewer
= !!state
->render_states
[WINED3D_RS_LOCALVIEWER
];
2800 for (i
= 0, index
= 0; i
< LIGHTMAP_SIZE
&& index
< ARRAY_SIZE(lights
); ++i
)
2802 LIST_FOR_EACH_ENTRY(light_info
, &state
->light_state
.light_map
[i
], struct wined3d_light_info
, entry
)
2804 if (!light_info
->enabled
)
2807 switch (light_info
->OriginalParms
.type
)
2809 case WINED3D_LIGHT_DIRECTIONAL
:
2810 ++ls
->directional_light_count
;
2813 case WINED3D_LIGHT_POINT
:
2814 ++ls
->point_light_count
;
2817 case WINED3D_LIGHT_SPOT
:
2818 ++ls
->spot_light_count
;
2821 case WINED3D_LIGHT_PARALLELPOINT
:
2822 ++ls
->parallel_point_light_count
;
2826 FIXME("Unhandled light type %#x.\n", light_info
->OriginalParms
.type
);
2829 lights
[index
++] = light_info
;
2830 if (index
== WINED3D_MAX_SOFTWARE_ACTIVE_LIGHTS
)
2835 light_count
= index
;
2836 for (i
= 0, index
= 0; i
< light_count
; ++i
)
2838 light_info
= lights
[i
];
2839 if (light_info
->OriginalParms
.type
!= WINED3D_LIGHT_DIRECTIONAL
)
2842 light
= &ls
->lights
[index
];
2843 wined3d_vec4_transform(&vec4
, &light_info
->direction
, &state
->transforms
[WINED3D_TS_VIEW
]);
2844 light
->direction
= *(struct wined3d_vec3
*)&vec4
;
2845 wined3d_vec3_normalise(&light
->direction
);
2847 light
->diffuse
= light_info
->OriginalParms
.diffuse
;
2848 light
->ambient
= light_info
->OriginalParms
.ambient
;
2849 light
->specular
= light_info
->OriginalParms
.specular
;
2853 for (i
= 0; i
< light_count
; ++i
)
2855 light_info
= lights
[i
];
2856 if (light_info
->OriginalParms
.type
!= WINED3D_LIGHT_POINT
)
2859 light
= &ls
->lights
[index
];
2861 wined3d_vec4_transform(&light
->position
, &light_info
->position
, &state
->transforms
[WINED3D_TS_VIEW
]);
2862 light
->range
= light_info
->OriginalParms
.range
;
2863 light
->c_att
= light_info
->OriginalParms
.attenuation0
;
2864 light
->l_att
= light_info
->OriginalParms
.attenuation1
;
2865 light
->q_att
= light_info
->OriginalParms
.attenuation2
;
2867 light
->diffuse
= light_info
->OriginalParms
.diffuse
;
2868 light
->ambient
= light_info
->OriginalParms
.ambient
;
2869 light
->specular
= light_info
->OriginalParms
.specular
;
2873 for (i
= 0; i
< light_count
; ++i
)
2875 light_info
= lights
[i
];
2876 if (light_info
->OriginalParms
.type
!= WINED3D_LIGHT_SPOT
)
2879 light
= &ls
->lights
[index
];
2881 wined3d_vec4_transform(&light
->position
, &light_info
->position
, &state
->transforms
[WINED3D_TS_VIEW
]);
2882 wined3d_vec4_transform(&vec4
, &light_info
->direction
, &state
->transforms
[WINED3D_TS_VIEW
]);
2883 light
->direction
= *(struct wined3d_vec3
*)&vec4
;
2884 wined3d_vec3_normalise(&light
->direction
);
2885 light
->range
= light_info
->OriginalParms
.range
;
2886 light
->falloff
= light_info
->OriginalParms
.falloff
;
2887 light
->c_att
= light_info
->OriginalParms
.attenuation0
;
2888 light
->l_att
= light_info
->OriginalParms
.attenuation1
;
2889 light
->q_att
= light_info
->OriginalParms
.attenuation2
;
2890 light
->cos_htheta
= cosf(light_info
->OriginalParms
.theta
/ 2.0f
);
2891 light
->cos_hphi
= cosf(light_info
->OriginalParms
.phi
/ 2.0f
);
2893 light
->diffuse
= light_info
->OriginalParms
.diffuse
;
2894 light
->ambient
= light_info
->OriginalParms
.ambient
;
2895 light
->specular
= light_info
->OriginalParms
.specular
;
2899 for (i
= 0; i
< light_count
; ++i
)
2901 light_info
= lights
[i
];
2902 if (light_info
->OriginalParms
.type
!= WINED3D_LIGHT_PARALLELPOINT
)
2905 light
= &ls
->lights
[index
];
2907 wined3d_vec4_transform(&vec4
, &light_info
->position
, &state
->transforms
[WINED3D_TS_VIEW
]);
2908 *(struct wined3d_vec3
*)&light
->position
= *(struct wined3d_vec3
*)&vec4
;
2909 wined3d_vec3_normalise((struct wined3d_vec3
*)&light
->position
);
2910 light
->diffuse
= light_info
->OriginalParms
.diffuse
;
2911 light
->ambient
= light_info
->OriginalParms
.ambient
;
2912 light
->specular
= light_info
->OriginalParms
.specular
;
2917 static void update_light_diffuse_specular(struct wined3d_color
*diffuse
, struct wined3d_color
*specular
,
2918 const struct wined3d_vec3
*dir
, float att
, float material_shininess
,
2919 const struct wined3d_vec3
*normal_transformed
,
2920 const struct wined3d_vec3
*position_transformed_normalised
,
2921 const struct light_transformed
*light
, const struct lights_settings
*ls
)
2923 struct wined3d_vec3 vec3
;
2926 c
= wined3d_clamp(wined3d_vec3_dot(dir
, normal_transformed
), 0.0f
, 1.0f
);
2927 wined3d_color_rgb_mul_add(diffuse
, &light
->diffuse
, c
* att
);
2930 if (ls
->localviewer
)
2931 wined3d_vec3_subtract(&vec3
, position_transformed_normalised
);
2934 wined3d_vec3_normalise(&vec3
);
2935 t
= wined3d_vec3_dot(normal_transformed
, &vec3
);
2936 if (t
> 0.0f
&& (!ls
->legacy_lighting
|| material_shininess
> 0.0f
)
2937 && wined3d_vec3_dot(dir
, normal_transformed
) > 0.0f
)
2938 wined3d_color_rgb_mul_add(specular
, &light
->specular
, att
* powf(t
, material_shininess
));
2941 static void light_set_vertex_data(struct lights_settings
*ls
,
2942 const struct wined3d_vec4
*position
)
2944 if (ls
->fog_mode
== WINED3D_FOG_NONE
&& !ls
->lighting
)
2947 wined3d_vec4_transform(&ls
->position_transformed
, position
, &ls
->modelview_matrix
);
2948 wined3d_vec3_scale((struct wined3d_vec3
*)&ls
->position_transformed
, 1.0f
/ ls
->position_transformed
.w
);
2951 static void compute_light(struct wined3d_color
*ambient
, struct wined3d_color
*diffuse
,
2952 struct wined3d_color
*specular
, struct lights_settings
*ls
, const struct wined3d_vec3
*normal
,
2953 float material_shininess
)
2955 struct wined3d_vec3 position_transformed_normalised
;
2956 struct wined3d_vec3 normal_transformed
= {0.0f
};
2957 const struct light_transformed
*light
;
2958 struct wined3d_vec3 dir
, dst
;
2959 unsigned int i
, index
;
2962 position_transformed_normalised
= *(const struct wined3d_vec3
*)&ls
->position_transformed
;
2963 wined3d_vec3_normalise(&position_transformed_normalised
);
2967 wined3d_vec3_transform(&normal_transformed
, normal
, &ls
->normal_matrix
);
2969 wined3d_vec3_normalise(&normal_transformed
);
2972 diffuse
->r
= diffuse
->g
= diffuse
->b
= diffuse
->a
= 0.0f
;
2973 *specular
= *diffuse
;
2974 *ambient
= ls
->ambient_light
;
2977 for (i
= 0; i
< ls
->directional_light_count
; ++i
, ++index
)
2979 light
= &ls
->lights
[index
];
2981 wined3d_color_rgb_mul_add(ambient
, &light
->ambient
, 1.0f
);
2983 update_light_diffuse_specular(diffuse
, specular
, &light
->direction
, 1.0f
, material_shininess
,
2984 &normal_transformed
, &position_transformed_normalised
, light
, ls
);
2987 for (i
= 0; i
< ls
->point_light_count
; ++i
, ++index
)
2989 light
= &ls
->lights
[index
];
2990 dir
.x
= light
->position
.x
- ls
->position_transformed
.x
;
2991 dir
.y
= light
->position
.y
- ls
->position_transformed
.y
;
2992 dir
.z
= light
->position
.z
- ls
->position_transformed
.z
;
2994 dst
.z
= wined3d_vec3_dot(&dir
, &dir
);
2995 dst
.y
= sqrtf(dst
.z
);
2997 if (ls
->legacy_lighting
)
2999 dst
.y
= (light
->range
- dst
.y
) / light
->range
;
3000 if (!(dst
.y
> 0.0f
))
3002 dst
.z
= dst
.y
* dst
.y
;
3006 if (!(dst
.y
<= light
->range
))
3009 att
= dst
.x
* light
->c_att
+ dst
.y
* light
->l_att
+ dst
.z
* light
->q_att
;
3010 if (!ls
->legacy_lighting
)
3013 wined3d_color_rgb_mul_add(ambient
, &light
->ambient
, att
);
3016 wined3d_vec3_normalise(&dir
);
3017 update_light_diffuse_specular(diffuse
, specular
, &dir
, att
, material_shininess
,
3018 &normal_transformed
, &position_transformed_normalised
, light
, ls
);
3022 for (i
= 0; i
< ls
->spot_light_count
; ++i
, ++index
)
3026 light
= &ls
->lights
[index
];
3028 dir
.x
= light
->position
.x
- ls
->position_transformed
.x
;
3029 dir
.y
= light
->position
.y
- ls
->position_transformed
.y
;
3030 dir
.z
= light
->position
.z
- ls
->position_transformed
.z
;
3032 dst
.z
= wined3d_vec3_dot(&dir
, &dir
);
3033 dst
.y
= sqrtf(dst
.z
);
3036 if (ls
->legacy_lighting
)
3038 dst
.y
= (light
->range
- dst
.y
) / light
->range
;
3039 if (!(dst
.y
> 0.0f
))
3041 dst
.z
= dst
.y
* dst
.y
;
3045 if (!(dst
.y
<= light
->range
))
3048 wined3d_vec3_normalise(&dir
);
3049 t
= -wined3d_vec3_dot(&dir
, &light
->direction
);
3050 if (t
> light
->cos_htheta
)
3052 else if (t
<= light
->cos_hphi
)
3055 att
= powf((t
- light
->cos_hphi
) / (light
->cos_htheta
- light
->cos_hphi
), light
->falloff
);
3057 t
= dst
.x
* light
->c_att
+ dst
.y
* light
->l_att
+ dst
.z
* light
->q_att
;
3058 if (ls
->legacy_lighting
)
3063 wined3d_color_rgb_mul_add(ambient
, &light
->ambient
, att
);
3066 update_light_diffuse_specular(diffuse
, specular
, &dir
, att
, material_shininess
,
3067 &normal_transformed
, &position_transformed_normalised
, light
, ls
);
3070 for (i
= 0; i
< ls
->parallel_point_light_count
; ++i
, ++index
)
3072 light
= &ls
->lights
[index
];
3074 wined3d_color_rgb_mul_add(ambient
, &light
->ambient
, 1.0f
);
3076 update_light_diffuse_specular(diffuse
, specular
, (const struct wined3d_vec3
*)&light
->position
,
3077 1.0f
, material_shininess
, &normal_transformed
, &position_transformed_normalised
, light
, ls
);
3081 static float wined3d_calculate_fog_factor(float fog_coord
, const struct lights_settings
*ls
)
3083 switch (ls
->fog_mode
)
3085 case WINED3D_FOG_NONE
:
3087 case WINED3D_FOG_LINEAR
:
3088 return (ls
->fog_end
- fog_coord
) / (ls
->fog_end
- ls
->fog_start
);
3089 case WINED3D_FOG_EXP
:
3090 return expf(-fog_coord
* ls
->fog_density
);
3091 case WINED3D_FOG_EXP2
:
3092 return expf(-fog_coord
* fog_coord
* ls
->fog_density
* ls
->fog_density
);
3094 ERR("Unhandled fog mode %#x.\n", ls
->fog_mode
);
3099 static void update_fog_factor(float *fog_factor
, struct lights_settings
*ls
)
3103 if (ls
->fog_mode
== WINED3D_FOG_NONE
)
3106 switch (ls
->fog_coord_mode
)
3108 case WINED3D_FFP_VS_FOG_RANGE
:
3109 fog_coord
= sqrtf(wined3d_vec3_dot((const struct wined3d_vec3
*)&ls
->position_transformed
,
3110 (const struct wined3d_vec3
*)&ls
->position_transformed
));
3113 case WINED3D_FFP_VS_FOG_DEPTH
:
3114 fog_coord
= fabsf(ls
->position_transformed
.z
);
3118 ERR("Unhandled fog coordinate mode %#x.\n", ls
->fog_coord_mode
);
3121 *fog_factor
= wined3d_calculate_fog_factor(fog_coord
, ls
);
3124 /* Context activation is done by the caller. */
3125 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3126 static HRESULT
process_vertices_strided(const struct wined3d_device
*device
, DWORD dwDestIndex
, DWORD dwCount
,
3127 const struct wined3d_stream_info
*stream_info
, struct wined3d_buffer
*dest
, DWORD flags
, DWORD dst_fvf
)
3129 enum wined3d_material_color_source diffuse_source
, specular_source
, ambient_source
, emissive_source
;
3130 const struct wined3d_color
*material_specular_state_colour
;
3131 struct wined3d_matrix mat
, proj_mat
, view_mat
, world_mat
;
3132 const struct wined3d_state
*state
= &device
->state
;
3133 const struct wined3d_format
*output_colour_format
;
3134 static const struct wined3d_color black
;
3135 struct wined3d_map_desc map_desc
;
3136 struct wined3d_box box
= {0};
3137 struct wined3d_viewport vp
;
3138 unsigned int texture_count
;
3139 struct lights_settings ls
;
3140 unsigned int vertex_size
;
3141 BOOL do_clip
, lighting
;
3147 if (!(stream_info
->use_map
& (1u << WINED3D_FFP_POSITION
)))
3149 ERR("Source has no position mask.\n");
3150 return WINED3DERR_INVALIDCALL
;
3153 if (state
->render_states
[WINED3D_RS_CLIPPING
])
3155 static BOOL warned
= FALSE
;
3157 * The clipping code is not quite correct. Some things need
3158 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3159 * so disable clipping for now.
3160 * (The graphics in Half-Life are broken, and my processvertices
3161 * test crashes with IDirect3DDevice3)
3168 FIXME("Clipping is broken and disabled for now\n");
3174 vertex_size
= wined3d_get_flexible_vertex_size(dst_fvf
);
3175 box
.left
= dwDestIndex
* vertex_size
;
3176 box
.right
= box
.left
+ dwCount
* vertex_size
;
3177 if (FAILED(hr
= wined3d_resource_map(&dest
->resource
, 0, &map_desc
, &box
, WINED3D_MAP_WRITE
)))
3179 WARN("Failed to map buffer, hr %#x.\n", hr
);
3182 dest_ptr
= map_desc
.data
;
3184 wined3d_device_get_transform(device
, WINED3D_TS_VIEW
, &view_mat
);
3185 wined3d_device_get_transform(device
, WINED3D_TS_PROJECTION
, &proj_mat
);
3186 wined3d_device_get_transform(device
, WINED3D_TS_WORLD_MATRIX(0), &world_mat
);
3188 TRACE("View mat:\n");
3189 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._11
, view_mat
._12
, view_mat
._13
, view_mat
._14
);
3190 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._21
, view_mat
._22
, view_mat
._23
, view_mat
._24
);
3191 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._31
, view_mat
._32
, view_mat
._33
, view_mat
._34
);
3192 TRACE("%.8e %.8e %.8e %.8e\n", view_mat
._41
, view_mat
._42
, view_mat
._43
, view_mat
._44
);
3194 TRACE("Proj mat:\n");
3195 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._11
, proj_mat
._12
, proj_mat
._13
, proj_mat
._14
);
3196 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._21
, proj_mat
._22
, proj_mat
._23
, proj_mat
._24
);
3197 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._31
, proj_mat
._32
, proj_mat
._33
, proj_mat
._34
);
3198 TRACE("%.8e %.8e %.8e %.8e\n", proj_mat
._41
, proj_mat
._42
, proj_mat
._43
, proj_mat
._44
);
3200 TRACE("World mat:\n");
3201 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._11
, world_mat
._12
, world_mat
._13
, world_mat
._14
);
3202 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._21
, world_mat
._22
, world_mat
._23
, world_mat
._24
);
3203 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._31
, world_mat
._32
, world_mat
._33
, world_mat
._34
);
3204 TRACE("%.8e %.8e %.8e %.8e\n", world_mat
._41
, world_mat
._42
, world_mat
._43
, world_mat
._44
);
3206 /* Get the viewport */
3207 wined3d_device_get_viewports(device
, NULL
, &vp
);
3208 TRACE("viewport x %.8e, y %.8e, width %.8e, height %.8e, min_z %.8e, max_z %.8e.\n",
3209 vp
.x
, vp
.y
, vp
.width
, vp
.height
, vp
.min_z
, vp
.max_z
);
3211 multiply_matrix(&mat
,&view_mat
,&world_mat
);
3212 multiply_matrix(&mat
,&proj_mat
,&mat
);
3214 texture_count
= (dst_fvf
& WINED3DFVF_TEXCOUNT_MASK
) >> WINED3DFVF_TEXCOUNT_SHIFT
;
3216 lighting
= state
->render_states
[WINED3D_RS_LIGHTING
]
3217 && (dst_fvf
& (WINED3DFVF_DIFFUSE
| WINED3DFVF_SPECULAR
));
3218 wined3d_get_material_colour_source(&diffuse_source
, &emissive_source
,
3219 &ambient_source
, &specular_source
, state
, stream_info
);
3220 output_colour_format
= wined3d_get_format(device
->adapter
, WINED3DFMT_B8G8R8A8_UNORM
, 0);
3221 material_specular_state_colour
= state
->render_states
[WINED3D_RS_SPECULARENABLE
]
3222 ? &state
->material
.specular
: &black
;
3223 init_transformed_lights(&ls
, state
, device
->adapter
->d3d_info
.wined3d_creation_flags
3224 & WINED3D_LEGACY_FFP_LIGHTING
, lighting
);
3226 wined3d_viewport_get_z_range(&vp
, &min_z
, &max_z
);
3228 for (i
= 0; i
< dwCount
; ++i
)
3230 const struct wined3d_stream_info_element
*position_element
= &stream_info
->elements
[WINED3D_FFP_POSITION
];
3231 const float *p
= (const float *)&position_element
->data
.addr
[i
* position_element
->stride
];
3232 struct wined3d_color ambient
, diffuse
, specular
;
3233 struct wined3d_vec4 position
;
3234 unsigned int tex_index
;
3241 light_set_vertex_data(&ls
, &position
);
3243 if ( ((dst_fvf
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZ
) ||
3244 ((dst_fvf
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
) ) {
3245 /* The position first */
3247 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p
[0], p
[1], p
[2]);
3249 /* Multiplication with world, view and projection matrix. */
3250 x
= (p
[0] * mat
._11
) + (p
[1] * mat
._21
) + (p
[2] * mat
._31
) + mat
._41
;
3251 y
= (p
[0] * mat
._12
) + (p
[1] * mat
._22
) + (p
[2] * mat
._32
) + mat
._42
;
3252 z
= (p
[0] * mat
._13
) + (p
[1] * mat
._23
) + (p
[2] * mat
._33
) + mat
._43
;
3253 rhw
= (p
[0] * mat
._14
) + (p
[1] * mat
._24
) + (p
[2] * mat
._34
) + mat
._44
;
3255 TRACE("x=%f y=%f z=%f rhw=%f\n", x
, y
, z
, rhw
);
3257 /* WARNING: The following things are taken from d3d7 and were not yet checked
3258 * against d3d8 or d3d9!
3261 /* Clipping conditions: From msdn
3263 * A vertex is clipped if it does not match the following requirements
3267 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3269 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3270 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3274 if (!do_clip
|| (-rhw
- eps
< x
&& -rhw
- eps
< y
&& -eps
< z
&& x
<= rhw
+ eps
3275 && y
<= rhw
+ eps
&& z
<= rhw
+ eps
&& rhw
> eps
))
3277 /* "Normal" viewport transformation (not clipped)
3278 * 1) The values are divided by rhw
3279 * 2) The y axis is negative, so multiply it with -1
3280 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3281 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3282 * 4) Multiply x with Width/2 and add Width/2
3283 * 5) The same for the height
3284 * 6) Add the viewpoint X and Y to the 2D coordinates and
3285 * The minimum Z value to z
3286 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3288 * Well, basically it's simply a linear transformation into viewport
3302 x
+= vp
.width
/ 2 + vp
.x
;
3303 y
+= vp
.height
/ 2 + vp
.y
;
3308 /* That vertex got clipped
3309 * Contrary to OpenGL it is not dropped completely, it just
3310 * undergoes a different calculation.
3312 TRACE("Vertex got clipped\n");
3319 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3320 * outside of the main vertex buffer memory. That needs some more
3325 TRACE("Writing (%f %f %f) %f\n", x
, y
, z
, rhw
);
3328 ( (float *) dest_ptr
)[0] = x
;
3329 ( (float *) dest_ptr
)[1] = y
;
3330 ( (float *) dest_ptr
)[2] = z
;
3331 ( (float *) dest_ptr
)[3] = rhw
; /* SIC, see ddraw test! */
3333 dest_ptr
+= 3 * sizeof(float);
3335 if ((dst_fvf
& WINED3DFVF_POSITION_MASK
) == WINED3DFVF_XYZRHW
)
3336 dest_ptr
+= sizeof(float);
3339 if (dst_fvf
& WINED3DFVF_PSIZE
)
3340 dest_ptr
+= sizeof(DWORD
);
3342 if (dst_fvf
& WINED3DFVF_NORMAL
)
3344 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_NORMAL
];
3345 const float *normal
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3346 /* AFAIK this should go into the lighting information */
3347 FIXME("Didn't expect the destination to have a normal\n");
3348 copy_and_next(dest_ptr
, normal
, 3 * sizeof(float));
3353 const struct wined3d_stream_info_element
*element
;
3354 struct wined3d_vec3
*normal
;
3356 if (stream_info
->use_map
& (1u << WINED3D_FFP_NORMAL
))
3358 element
= &stream_info
->elements
[WINED3D_FFP_NORMAL
];
3359 normal
= (struct wined3d_vec3
*)&element
->data
.addr
[i
* element
->stride
];
3365 compute_light(&ambient
, &diffuse
, &specular
, &ls
, normal
,
3366 state
->render_states
[WINED3D_RS_SPECULARENABLE
] ? state
->material
.power
: 0.0f
);
3369 if (dst_fvf
& WINED3DFVF_DIFFUSE
)
3371 struct wined3d_color material_diffuse
, material_ambient
, material_emissive
, diffuse_colour
;
3373 wined3d_colour_from_mcs(&material_diffuse
, diffuse_source
,
3374 &state
->material
.diffuse
, i
, stream_info
);
3378 wined3d_colour_from_mcs(&material_ambient
, ambient_source
,
3379 &state
->material
.ambient
, i
, stream_info
);
3380 wined3d_colour_from_mcs(&material_emissive
, emissive_source
,
3381 &state
->material
.emissive
, i
, stream_info
);
3383 diffuse_colour
.r
= ambient
.r
* material_ambient
.r
3384 + diffuse
.r
* material_diffuse
.r
+ material_emissive
.r
;
3385 diffuse_colour
.g
= ambient
.g
* material_ambient
.g
3386 + diffuse
.g
* material_diffuse
.g
+ material_emissive
.g
;
3387 diffuse_colour
.b
= ambient
.b
* material_ambient
.b
3388 + diffuse
.b
* material_diffuse
.b
+ material_emissive
.b
;
3389 diffuse_colour
.a
= material_diffuse
.a
;
3393 diffuse_colour
= material_diffuse
;
3395 wined3d_color_clamp(&diffuse_colour
, &diffuse_colour
, 0.0f
, 1.0f
);
3396 *((DWORD
*)dest_ptr
) = wined3d_format_convert_from_float(output_colour_format
, &diffuse_colour
);
3397 dest_ptr
+= sizeof(DWORD
);
3400 if (dst_fvf
& WINED3DFVF_SPECULAR
)
3402 struct wined3d_color material_specular
, specular_colour
;
3404 wined3d_colour_from_mcs(&material_specular
, specular_source
,
3405 material_specular_state_colour
, i
, stream_info
);
3409 specular_colour
.r
= specular
.r
* material_specular
.r
;
3410 specular_colour
.g
= specular
.g
* material_specular
.g
;
3411 specular_colour
.b
= specular
.b
* material_specular
.b
;
3412 specular_colour
.a
= ls
.legacy_lighting
? 0.0f
: material_specular
.a
;
3416 specular_colour
= material_specular
;
3418 update_fog_factor(&specular_colour
.a
, &ls
);
3419 wined3d_color_clamp(&specular_colour
, &specular_colour
, 0.0f
, 1.0f
);
3420 *((DWORD
*)dest_ptr
) = wined3d_format_convert_from_float(output_colour_format
, &specular_colour
);
3421 dest_ptr
+= sizeof(DWORD
);
3424 for (tex_index
= 0; tex_index
< texture_count
; ++tex_index
)
3426 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[WINED3D_FFP_TEXCOORD0
+ tex_index
];
3427 const float *tex_coord
= (const float *)(element
->data
.addr
+ i
* element
->stride
);
3428 if (!(stream_info
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ tex_index
))))
3430 ERR("No source texture, but destination requests one\n");
3431 dest_ptr
+= GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf
, tex_index
) * sizeof(float);
3435 copy_and_next(dest_ptr
, tex_coord
, GET_TEXCOORD_SIZE_FROM_FVF(dst_fvf
, tex_index
) * sizeof(float));
3440 wined3d_resource_unmap(&dest
->resource
, 0);
3444 #undef copy_and_next
3446 HRESULT CDECL
wined3d_device_process_vertices(struct wined3d_device
*device
,
3447 UINT src_start_idx
, UINT dst_idx
, UINT vertex_count
, struct wined3d_buffer
*dst_buffer
,
3448 const struct wined3d_vertex_declaration
*declaration
, DWORD flags
, DWORD dst_fvf
)
3450 struct wined3d_state
*state
= &device
->state
;
3451 struct wined3d_stream_info stream_info
;
3452 struct wined3d_resource
*resource
;
3453 struct wined3d_box box
= {0};
3454 struct wined3d_shader
*vs
;
3459 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3460 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3461 device
, src_start_idx
, dst_idx
, vertex_count
,
3462 dst_buffer
, declaration
, flags
, dst_fvf
);
3465 FIXME("Output vertex declaration not implemented yet.\n");
3467 vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3468 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = NULL
;
3469 wined3d_stream_info_from_declaration(&stream_info
, state
, &device
->adapter
->d3d_info
);
3470 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
] = vs
;
3472 /* We can't convert FROM a VBO, and vertex buffers used to source into
3473 * process_vertices() are unlikely to ever be used for drawing. Release
3474 * VBOs in those buffers and fix up the stream_info structure.
3476 * Also apply the start index. */
3477 for (i
= 0, map
= stream_info
.use_map
; map
; map
>>= 1, ++i
)
3479 struct wined3d_stream_info_element
*e
;
3480 struct wined3d_map_desc map_desc
;
3485 e
= &stream_info
.elements
[i
];
3486 resource
= &state
->streams
[e
->stream_idx
].buffer
->resource
;
3487 box
.left
= src_start_idx
* e
->stride
;
3488 box
.right
= box
.left
+ vertex_count
* e
->stride
;
3489 if (FAILED(wined3d_resource_map(resource
, 0, &map_desc
, &box
, WINED3D_MAP_READ
)))
3491 ERR("Failed to map resource.\n");
3492 for (j
= 0, map
= stream_info
.use_map
; map
&& j
< i
; map
>>= 1, ++j
)
3497 e
= &stream_info
.elements
[j
];
3498 resource
= &state
->streams
[e
->stream_idx
].buffer
->resource
;
3499 if (FAILED(wined3d_resource_unmap(resource
, 0)))
3500 ERR("Failed to unmap resource.\n");
3502 return WINED3DERR_INVALIDCALL
;
3504 e
->data
.buffer_object
= 0;
3505 e
->data
.addr
+= (ULONG_PTR
)map_desc
.data
;
3508 hr
= process_vertices_strided(device
, dst_idx
, vertex_count
,
3509 &stream_info
, dst_buffer
, flags
, dst_fvf
);
3511 for (i
= 0, map
= stream_info
.use_map
; map
; map
>>= 1, ++i
)
3516 resource
= &state
->streams
[stream_info
.elements
[i
].stream_idx
].buffer
->resource
;
3517 if (FAILED(wined3d_resource_unmap(resource
, 0)))
3518 ERR("Failed to unmap resource.\n");
3524 static void wined3d_device_set_texture_stage_state(struct wined3d_device
*device
,
3525 UINT stage
, enum wined3d_texture_stage_state state
, DWORD value
)
3527 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
3529 TRACE("device %p, stage %u, state %s, value %#x.\n",
3530 device
, stage
, debug_d3dtexturestate(state
), value
);
3532 if (stage
>= d3d_info
->limits
.ffp_blend_stages
)
3534 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3535 stage
, d3d_info
->limits
.ffp_blend_stages
- 1);
3539 if (value
== device
->state
.texture_states
[stage
][state
])
3541 TRACE("Application is setting the old value over, nothing to do.\n");
3545 device
->state
.texture_states
[stage
][state
] = value
;
3547 wined3d_cs_emit_set_texture_state(device
->cs
, stage
, state
, value
);
3550 static void wined3d_device_set_texture(struct wined3d_device
*device
,
3551 UINT stage
, struct wined3d_texture
*texture
)
3553 struct wined3d_texture
*prev
;
3555 TRACE("device %p, stage %u, texture %p.\n", device
, stage
, texture
);
3557 /* Windows accepts overflowing this array... we do not. */
3558 if (stage
>= ARRAY_SIZE(device
->state
.textures
))
3560 WARN("Ignoring invalid stage %u.\n", stage
);
3564 prev
= device
->state
.textures
[stage
];
3565 TRACE("Previous texture %p.\n", prev
);
3567 if (texture
== prev
)
3569 TRACE("App is setting the same texture again, nothing to do.\n");
3573 TRACE("Setting new texture to %p.\n", texture
);
3574 device
->state
.textures
[stage
] = texture
;
3577 wined3d_texture_incref(texture
);
3578 wined3d_cs_emit_set_texture(device
->cs
, stage
, texture
);
3580 wined3d_texture_decref(prev
);
3585 void CDECL
wined3d_device_apply_stateblock(struct wined3d_device
*device
,
3586 struct wined3d_stateblock
*stateblock
)
3588 BOOL set_blend_state
= FALSE
, set_depth_stencil_state
= FALSE
, set_rasterizer_state
= FALSE
;
3589 const struct wined3d_stateblock_state
*state
= &stateblock
->stateblock_state
;
3590 const struct wined3d_saved_states
*changed
= &stateblock
->changed
;
3591 const unsigned int word_bit_count
= sizeof(DWORD
) * CHAR_BIT
;
3592 unsigned int i
, j
, start
, idx
;
3593 struct wined3d_range range
;
3596 TRACE("device %p, stateblock %p.\n", device
, stateblock
);
3598 if (changed
->vertexShader
)
3599 wined3d_device_set_vertex_shader(device
, state
->vs
);
3600 if (changed
->pixelShader
)
3601 wined3d_device_set_pixel_shader(device
, state
->ps
);
3603 for (start
= 0; ; start
= range
.offset
+ range
.size
)
3605 if (!wined3d_bitmap_get_range(changed
->vs_consts_f
, WINED3D_MAX_VS_CONSTS_F
, start
, &range
))
3608 wined3d_device_set_vs_consts_f(device
, range
.offset
, range
.size
, &state
->vs_consts_f
[range
.offset
]);
3611 map
= changed
->vertexShaderConstantsI
;
3612 for (start
= 0; ; start
= range
.offset
+ range
.size
)
3614 if (!wined3d_bitmap_get_range(&map
, WINED3D_MAX_CONSTS_I
, start
, &range
))
3617 wined3d_device_set_vs_consts_i(device
, range
.offset
, range
.size
, &state
->vs_consts_i
[range
.offset
]);
3620 map
= changed
->vertexShaderConstantsB
;
3621 for (start
= 0; ; start
= range
.offset
+ range
.size
)
3623 if (!wined3d_bitmap_get_range(&map
, WINED3D_MAX_CONSTS_B
, start
, &range
))
3626 wined3d_device_set_vs_consts_b(device
, range
.offset
, range
.size
, &state
->vs_consts_b
[range
.offset
]);
3629 for (start
= 0; ; start
= range
.offset
+ range
.size
)
3631 if (!wined3d_bitmap_get_range(changed
->ps_consts_f
, WINED3D_MAX_PS_CONSTS_F
, start
, &range
))
3634 wined3d_device_set_ps_consts_f(device
, range
.offset
, range
.size
, &state
->ps_consts_f
[range
.offset
]);
3637 map
= changed
->pixelShaderConstantsI
;
3638 for (start
= 0; ; start
= range
.offset
+ range
.size
)
3640 if (!wined3d_bitmap_get_range(&map
, WINED3D_MAX_CONSTS_I
, start
, &range
))
3643 wined3d_device_set_ps_consts_i(device
, range
.offset
, range
.size
, &state
->ps_consts_i
[range
.offset
]);
3646 map
= changed
->pixelShaderConstantsB
;
3647 for (start
= 0; ; start
= range
.offset
+ range
.size
)
3649 if (!wined3d_bitmap_get_range(&map
, WINED3D_MAX_CONSTS_B
, start
, &range
))
3652 wined3d_device_set_ps_consts_b(device
, range
.offset
, range
.size
, &state
->ps_consts_b
[range
.offset
]);
3655 if (changed
->lights
)
3657 for (i
= 0; i
< ARRAY_SIZE(state
->light_state
->light_map
); ++i
)
3659 const struct wined3d_light_info
*light
;
3661 LIST_FOR_EACH_ENTRY(light
, &state
->light_state
->light_map
[i
], struct wined3d_light_info
, entry
)
3663 wined3d_device_set_light(device
, light
->OriginalIndex
, &light
->OriginalParms
);
3664 wined3d_device_set_light_enable(device
, light
->OriginalIndex
, light
->glIndex
!= -1);
3669 for (i
= 0; i
< ARRAY_SIZE(changed
->renderState
); ++i
)
3671 map
= changed
->renderState
[i
];
3674 j
= wined3d_bit_scan(&map
);
3675 idx
= i
* word_bit_count
+ j
;
3679 case WINED3D_RS_BLENDFACTOR
:
3680 case WINED3D_RS_MULTISAMPLEMASK
:
3681 case WINED3D_RS_ALPHABLENDENABLE
:
3682 case WINED3D_RS_SRCBLEND
:
3683 case WINED3D_RS_DESTBLEND
:
3684 case WINED3D_RS_BLENDOP
:
3685 case WINED3D_RS_SEPARATEALPHABLENDENABLE
:
3686 case WINED3D_RS_SRCBLENDALPHA
:
3687 case WINED3D_RS_DESTBLENDALPHA
:
3688 case WINED3D_RS_BLENDOPALPHA
:
3689 case WINED3D_RS_COLORWRITEENABLE
:
3690 case WINED3D_RS_COLORWRITEENABLE1
:
3691 case WINED3D_RS_COLORWRITEENABLE2
:
3692 case WINED3D_RS_COLORWRITEENABLE3
:
3693 set_blend_state
= TRUE
;
3696 case WINED3D_RS_BACK_STENCILFAIL
:
3697 case WINED3D_RS_BACK_STENCILFUNC
:
3698 case WINED3D_RS_BACK_STENCILPASS
:
3699 case WINED3D_RS_BACK_STENCILZFAIL
:
3700 case WINED3D_RS_STENCILENABLE
:
3701 case WINED3D_RS_STENCILFAIL
:
3702 case WINED3D_RS_STENCILFUNC
:
3703 case WINED3D_RS_STENCILMASK
:
3704 case WINED3D_RS_STENCILPASS
:
3705 case WINED3D_RS_STENCILWRITEMASK
:
3706 case WINED3D_RS_STENCILZFAIL
:
3707 case WINED3D_RS_TWOSIDEDSTENCILMODE
:
3708 case WINED3D_RS_ZENABLE
:
3709 case WINED3D_RS_ZFUNC
:
3710 case WINED3D_RS_ZWRITEENABLE
:
3711 set_depth_stencil_state
= TRUE
;
3714 case WINED3D_RS_FILLMODE
:
3715 case WINED3D_RS_CULLMODE
:
3716 case WINED3D_RS_SLOPESCALEDEPTHBIAS
:
3717 case WINED3D_RS_DEPTHBIAS
:
3718 case WINED3D_RS_SCISSORTESTENABLE
:
3719 case WINED3D_RS_ANTIALIASEDLINEENABLE
:
3720 set_rasterizer_state
= TRUE
;
3724 wined3d_device_set_render_state(device
, idx
, state
->rs
[idx
]);
3730 if (set_rasterizer_state
)
3732 struct wined3d_rasterizer_state
*rasterizer_state
;
3733 struct wined3d_rasterizer_state_desc desc
;
3734 struct wine_rb_entry
*entry
;
3741 memset(&desc
, 0, sizeof(desc
));
3742 desc
.fill_mode
= state
->rs
[WINED3D_RS_FILLMODE
];
3743 desc
.cull_mode
= state
->rs
[WINED3D_RS_CULLMODE
];
3744 bias
.d
= state
->rs
[WINED3D_RS_DEPTHBIAS
];
3745 desc
.depth_bias
= bias
.f
;
3746 bias
.d
= state
->rs
[WINED3D_RS_SLOPESCALEDEPTHBIAS
];
3747 desc
.scale_bias
= bias
.f
;
3748 desc
.depth_clip
= TRUE
;
3749 desc
.scissor
= state
->rs
[WINED3D_RS_SCISSORTESTENABLE
];
3750 desc
.line_antialias
= state
->rs
[WINED3D_RS_ANTIALIASEDLINEENABLE
];
3752 if ((entry
= wine_rb_get(&device
->rasterizer_states
, &desc
)))
3754 rasterizer_state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_rasterizer_state
, entry
);
3755 wined3d_device_set_rasterizer_state(device
, rasterizer_state
);
3757 else if (SUCCEEDED(wined3d_rasterizer_state_create(device
, &desc
, NULL
,
3758 &wined3d_null_parent_ops
, &rasterizer_state
)))
3760 wined3d_device_set_rasterizer_state(device
, rasterizer_state
);
3761 if (wine_rb_put(&device
->rasterizer_states
, &desc
, &rasterizer_state
->entry
) == -1)
3763 ERR("Failed to insert rasterizer state.\n");
3764 wined3d_rasterizer_state_decref(rasterizer_state
);
3769 if (set_blend_state
|| changed
->alpha_to_coverage
3770 || wined3d_bitmap_is_set(changed
->renderState
, WINED3D_RS_ADAPTIVETESS_Y
))
3772 struct wined3d_blend_state
*blend_state
;
3773 struct wined3d_blend_state_desc desc
;
3774 struct wine_rb_entry
*entry
;
3775 struct wined3d_color colour
;
3776 unsigned int sample_mask
;
3778 memset(&desc
, 0, sizeof(desc
));
3779 desc
.alpha_to_coverage
= state
->alpha_to_coverage
;
3780 desc
.independent
= FALSE
;
3781 if (state
->rs
[WINED3D_RS_ADAPTIVETESS_Y
] == WINED3DFMT_ATOC
)
3782 desc
.alpha_to_coverage
= TRUE
;
3783 desc
.rt
[0].enable
= state
->rs
[WINED3D_RS_ALPHABLENDENABLE
];
3784 desc
.rt
[0].src
= state
->rs
[WINED3D_RS_SRCBLEND
];
3785 desc
.rt
[0].dst
= state
->rs
[WINED3D_RS_DESTBLEND
];
3786 desc
.rt
[0].op
= state
->rs
[WINED3D_RS_BLENDOP
];
3787 if (state
->rs
[WINED3D_RS_SEPARATEALPHABLENDENABLE
])
3789 desc
.rt
[0].src_alpha
= state
->rs
[WINED3D_RS_SRCBLENDALPHA
];
3790 desc
.rt
[0].dst_alpha
= state
->rs
[WINED3D_RS_DESTBLENDALPHA
];
3791 desc
.rt
[0].op_alpha
= state
->rs
[WINED3D_RS_BLENDOPALPHA
];
3795 desc
.rt
[0].src_alpha
= state
->rs
[WINED3D_RS_SRCBLEND
];
3796 desc
.rt
[0].dst_alpha
= state
->rs
[WINED3D_RS_DESTBLEND
];
3797 desc
.rt
[0].op_alpha
= state
->rs
[WINED3D_RS_BLENDOP
];
3799 desc
.rt
[0].writemask
= state
->rs
[WINED3D_RS_COLORWRITEENABLE
];
3800 desc
.rt
[1].writemask
= state
->rs
[WINED3D_RS_COLORWRITEENABLE1
];
3801 desc
.rt
[2].writemask
= state
->rs
[WINED3D_RS_COLORWRITEENABLE2
];
3802 desc
.rt
[3].writemask
= state
->rs
[WINED3D_RS_COLORWRITEENABLE3
];
3803 if (desc
.rt
[1].writemask
!= desc
.rt
[0].writemask
3804 || desc
.rt
[2].writemask
!= desc
.rt
[0].writemask
3805 || desc
.rt
[3].writemask
!= desc
.rt
[0].writemask
)
3807 desc
.independent
= TRUE
;
3808 for (i
= 1; i
< 4; ++i
)
3810 desc
.rt
[i
].enable
= desc
.rt
[0].enable
;
3811 desc
.rt
[i
].src
= desc
.rt
[0].src
;
3812 desc
.rt
[i
].dst
= desc
.rt
[0].dst
;
3813 desc
.rt
[i
].op
= desc
.rt
[0].op
;
3814 desc
.rt
[i
].src_alpha
= desc
.rt
[0].src_alpha
;
3815 desc
.rt
[i
].dst_alpha
= desc
.rt
[0].dst_alpha
;
3816 desc
.rt
[i
].op_alpha
= desc
.rt
[0].op_alpha
;
3820 if (wined3d_bitmap_is_set(changed
->renderState
, WINED3D_RS_BLENDFACTOR
))
3821 wined3d_color_from_d3dcolor(&colour
, state
->rs
[WINED3D_RS_BLENDFACTOR
]);
3823 wined3d_device_get_blend_state(device
, &colour
, &sample_mask
);
3825 if ((entry
= wine_rb_get(&device
->blend_states
, &desc
)))
3827 blend_state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_blend_state
, entry
);
3828 wined3d_device_set_blend_state(device
, blend_state
, &colour
, state
->rs
[WINED3D_RS_MULTISAMPLEMASK
]);
3830 else if (SUCCEEDED(wined3d_blend_state_create(device
, &desc
, NULL
,
3831 &wined3d_null_parent_ops
, &blend_state
)))
3833 wined3d_device_set_blend_state(device
, blend_state
, &colour
, state
->rs
[WINED3D_RS_MULTISAMPLEMASK
]);
3834 if (wine_rb_put(&device
->blend_states
, &desc
, &blend_state
->entry
) == -1)
3836 ERR("Failed to insert blend state.\n");
3837 wined3d_blend_state_decref(blend_state
);
3842 if (set_depth_stencil_state
)
3844 struct wined3d_depth_stencil_state
*depth_stencil_state
;
3845 struct wined3d_depth_stencil_state_desc desc
;
3846 struct wine_rb_entry
*entry
;
3848 memset(&desc
, 0, sizeof(desc
));
3849 switch (state
->rs
[WINED3D_RS_ZENABLE
])
3851 case WINED3D_ZB_FALSE
:
3855 case WINED3D_ZB_USEW
:
3856 FIXME("W buffer is not well handled.\n");
3857 case WINED3D_ZB_TRUE
:
3862 FIXME("Unrecognized depth buffer type %#x.\n", state
->rs
[WINED3D_RS_ZENABLE
]);
3864 desc
.depth_write
= state
->rs
[WINED3D_RS_ZWRITEENABLE
];
3865 desc
.depth_func
= state
->rs
[WINED3D_RS_ZFUNC
];
3866 desc
.stencil
= state
->rs
[WINED3D_RS_STENCILENABLE
];
3867 desc
.stencil_read_mask
= state
->rs
[WINED3D_RS_STENCILMASK
];
3868 desc
.stencil_write_mask
= state
->rs
[WINED3D_RS_STENCILWRITEMASK
];
3869 desc
.front
.fail_op
= state
->rs
[WINED3D_RS_STENCILFAIL
];
3870 desc
.front
.depth_fail_op
= state
->rs
[WINED3D_RS_STENCILZFAIL
];
3871 desc
.front
.pass_op
= state
->rs
[WINED3D_RS_STENCILPASS
];
3872 desc
.front
.func
= state
->rs
[WINED3D_RS_STENCILFUNC
];
3874 if (state
->rs
[WINED3D_RS_TWOSIDEDSTENCILMODE
])
3876 desc
.back
.fail_op
= state
->rs
[WINED3D_RS_BACK_STENCILFAIL
];
3877 desc
.back
.depth_fail_op
= state
->rs
[WINED3D_RS_BACK_STENCILZFAIL
];
3878 desc
.back
.pass_op
= state
->rs
[WINED3D_RS_BACK_STENCILPASS
];
3879 desc
.back
.func
= state
->rs
[WINED3D_RS_BACK_STENCILFUNC
];
3883 desc
.back
= desc
.front
;
3886 if ((entry
= wine_rb_get(&device
->depth_stencil_states
, &desc
)))
3888 depth_stencil_state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_depth_stencil_state
, entry
);
3889 wined3d_device_set_depth_stencil_state(device
, depth_stencil_state
);
3891 else if (SUCCEEDED(wined3d_depth_stencil_state_create(device
, &desc
, NULL
,
3892 &wined3d_null_parent_ops
, &depth_stencil_state
)))
3894 wined3d_device_set_depth_stencil_state(device
, depth_stencil_state
);
3895 if (wine_rb_put(&device
->depth_stencil_states
, &desc
, &depth_stencil_state
->entry
) == -1)
3897 ERR("Failed to insert depth/stencil state.\n");
3898 wined3d_depth_stencil_state_decref(depth_stencil_state
);
3903 for (i
= 0; i
< ARRAY_SIZE(changed
->textureState
); ++i
)
3905 map
= changed
->textureState
[i
];
3908 j
= wined3d_bit_scan(&map
);
3909 wined3d_device_set_texture_stage_state(device
, i
, j
, state
->texture_states
[i
][j
]);
3913 for (i
= 0; i
< ARRAY_SIZE(changed
->samplerState
); ++i
)
3915 map
= changed
->samplerState
[i
];
3918 j
= wined3d_bit_scan(&map
);
3919 wined3d_device_set_sampler_state(device
, i
, j
, state
->sampler_states
[i
][j
]);
3923 if (changed
->transforms
)
3925 for (i
= 0; i
< ARRAY_SIZE(changed
->transform
); ++i
)
3927 map
= changed
->transform
[i
];
3930 j
= wined3d_bit_scan(&map
);
3931 idx
= i
* word_bit_count
+ j
;
3932 wined3d_device_set_transform(device
, idx
, &state
->transforms
[idx
]);
3937 if (changed
->indices
)
3938 wined3d_device_set_index_buffer(device
, state
->index_buffer
, state
->index_format
, 0);
3939 wined3d_device_set_base_vertex_index(device
, state
->base_vertex_index
);
3940 if (changed
->vertexDecl
)
3941 wined3d_device_set_vertex_declaration(device
, state
->vertex_declaration
);
3942 if (changed
->material
)
3943 wined3d_device_set_material(device
, &state
->material
);
3944 if (changed
->viewport
)
3945 wined3d_device_set_viewports(device
, 1, &state
->viewport
);
3946 if (changed
->scissorRect
)
3947 wined3d_device_set_scissor_rects(device
, 1, &state
->scissor_rect
);
3949 map
= changed
->streamSource
;
3952 i
= wined3d_bit_scan(&map
);
3953 wined3d_device_set_stream_source(device
, i
, state
->streams
[i
].buffer
,
3954 state
->streams
[i
].offset
, state
->streams
[i
].stride
);
3956 map
= changed
->streamFreq
;
3959 i
= wined3d_bit_scan(&map
);
3960 wined3d_device_set_stream_source_freq(device
, i
,
3961 state
->streams
[i
].frequency
| state
->streams
[i
].flags
);
3964 map
= changed
->textures
;
3967 i
= wined3d_bit_scan(&map
);
3968 wined3d_device_set_texture(device
, i
, state
->textures
[i
]);
3971 map
= changed
->clipplane
;
3974 i
= wined3d_bit_scan(&map
);
3975 wined3d_device_set_clip_plane(device
, i
, &state
->clip_planes
[i
]);
3978 memset(&stateblock
->changed
, 0, sizeof(stateblock
->changed
));
3980 TRACE("Applied stateblock %p.\n", stateblock
);
3983 HRESULT CDECL
wined3d_device_get_device_caps(const struct wined3d_device
*device
, struct wined3d_caps
*caps
)
3985 TRACE("device %p, caps %p.\n", device
, caps
);
3987 return wined3d_get_device_caps(device
->adapter
, device
->create_parms
.device_type
, caps
);
3990 HRESULT CDECL
wined3d_device_get_display_mode(const struct wined3d_device
*device
, UINT swapchain_idx
,
3991 struct wined3d_display_mode
*mode
, enum wined3d_display_rotation
*rotation
)
3993 struct wined3d_swapchain
*swapchain
;
3995 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3996 device
, swapchain_idx
, mode
, rotation
);
3998 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
3999 return WINED3DERR_INVALIDCALL
;
4001 return wined3d_swapchain_get_display_mode(swapchain
, mode
, rotation
);
4004 HRESULT CDECL
wined3d_device_begin_scene(struct wined3d_device
*device
)
4006 /* At the moment we have no need for any functionality at the beginning
4008 TRACE("device %p.\n", device
);
4010 if (device
->inScene
)
4012 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
4013 return WINED3DERR_INVALIDCALL
;
4015 device
->inScene
= TRUE
;
4019 HRESULT CDECL
wined3d_device_end_scene(struct wined3d_device
*device
)
4021 TRACE("device %p.\n", device
);
4023 if (!device
->inScene
)
4025 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
4026 return WINED3DERR_INVALIDCALL
;
4029 device
->inScene
= FALSE
;
4033 HRESULT CDECL
wined3d_device_clear(struct wined3d_device
*device
, DWORD rect_count
,
4034 const RECT
*rects
, DWORD flags
, const struct wined3d_color
*color
, float depth
, DWORD stencil
)
4036 TRACE("device %p, rect_count %u, rects %p, flags %#x, color %s, depth %.8e, stencil %u.\n",
4037 device
, rect_count
, rects
, flags
, debug_color(color
), depth
, stencil
);
4039 if (!rect_count
&& rects
)
4041 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects
);
4045 if (flags
& (WINED3DCLEAR_ZBUFFER
| WINED3DCLEAR_STENCIL
))
4047 struct wined3d_rendertarget_view
*ds
= device
->state
.fb
.depth_stencil
;
4050 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4051 /* TODO: What about depth stencil buffers without stencil bits? */
4052 return WINED3DERR_INVALIDCALL
;
4054 else if (flags
& WINED3DCLEAR_TARGET
)
4056 if (ds
->width
< device
->state
.fb
.render_targets
[0]->width
4057 || ds
->height
< device
->state
.fb
.render_targets
[0]->height
)
4059 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4065 wined3d_cs_emit_clear(device
->cs
, rect_count
, rects
, flags
, color
, depth
, stencil
);
4070 void CDECL
wined3d_device_set_predication(struct wined3d_device
*device
,
4071 struct wined3d_query
*predicate
, BOOL value
)
4073 struct wined3d_query
*prev
;
4075 TRACE("device %p, predicate %p, value %#x.\n", device
, predicate
, value
);
4077 prev
= device
->state
.predicate
;
4080 FIXME("Predicated rendering not implemented.\n");
4081 wined3d_query_incref(predicate
);
4083 device
->state
.predicate
= predicate
;
4084 device
->state
.predicate_value
= value
;
4085 wined3d_cs_emit_set_predication(device
->cs
, predicate
, value
);
4087 wined3d_query_decref(prev
);
4090 struct wined3d_query
* CDECL
wined3d_device_get_predication(struct wined3d_device
*device
, BOOL
*value
)
4092 TRACE("device %p, value %p.\n", device
, value
);
4095 *value
= device
->state
.predicate_value
;
4096 return device
->state
.predicate
;
4099 void CDECL
wined3d_device_dispatch_compute(struct wined3d_device
*device
,
4100 unsigned int group_count_x
, unsigned int group_count_y
, unsigned int group_count_z
)
4102 TRACE("device %p, group_count_x %u, group_count_y %u, group_count_z %u.\n",
4103 device
, group_count_x
, group_count_y
, group_count_z
);
4105 wined3d_cs_emit_dispatch(device
->cs
, group_count_x
, group_count_y
, group_count_z
);
4108 void CDECL
wined3d_device_dispatch_compute_indirect(struct wined3d_device
*device
,
4109 struct wined3d_buffer
*buffer
, unsigned int offset
)
4111 TRACE("device %p, buffer %p, offset %u.\n", device
, buffer
, offset
);
4113 wined3d_cs_emit_dispatch_indirect(device
->cs
, buffer
, offset
);
4116 void CDECL
wined3d_device_set_primitive_type(struct wined3d_device
*device
,
4117 enum wined3d_primitive_type primitive_type
, unsigned int patch_vertex_count
)
4119 TRACE("device %p, primitive_type %s, patch_vertex_count %u.\n",
4120 device
, debug_d3dprimitivetype(primitive_type
), patch_vertex_count
);
4122 device
->state
.primitive_type
= primitive_type
;
4123 device
->state
.patch_vertex_count
= patch_vertex_count
;
4126 void CDECL
wined3d_device_get_primitive_type(const struct wined3d_device
*device
,
4127 enum wined3d_primitive_type
*primitive_type
, unsigned int *patch_vertex_count
)
4129 TRACE("device %p, primitive_type %p, patch_vertex_count %p.\n",
4130 device
, primitive_type
, patch_vertex_count
);
4132 *primitive_type
= device
->state
.primitive_type
;
4133 if (patch_vertex_count
)
4134 *patch_vertex_count
= device
->state
.patch_vertex_count
;
4136 TRACE("Returning %s.\n", debug_d3dprimitivetype(*primitive_type
));
4139 HRESULT CDECL
wined3d_device_draw_primitive(struct wined3d_device
*device
, UINT start_vertex
, UINT vertex_count
)
4141 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device
, start_vertex
, vertex_count
);
4143 wined3d_cs_emit_draw(device
->cs
, device
->state
.primitive_type
,
4144 device
->state
.patch_vertex_count
, 0, start_vertex
, vertex_count
, 0, 0, false);
4149 void CDECL
wined3d_device_draw_primitive_instanced(struct wined3d_device
*device
,
4150 UINT start_vertex
, UINT vertex_count
, UINT start_instance
, UINT instance_count
)
4152 TRACE("device %p, start_vertex %u, vertex_count %u, start_instance %u, instance_count %u.\n",
4153 device
, start_vertex
, vertex_count
, start_instance
, instance_count
);
4155 wined3d_cs_emit_draw(device
->cs
, device
->state
.primitive_type
, device
->state
.patch_vertex_count
,
4156 0, start_vertex
, vertex_count
, start_instance
, instance_count
, false);
4159 void CDECL
wined3d_device_draw_primitive_instanced_indirect(struct wined3d_device
*device
,
4160 struct wined3d_buffer
*buffer
, unsigned int offset
)
4162 TRACE("device %p, buffer %p, offset %u.\n", device
, buffer
, offset
);
4164 wined3d_cs_emit_draw_indirect(device
->cs
, device
->state
.primitive_type
,
4165 device
->state
.patch_vertex_count
, buffer
, offset
, false);
4168 HRESULT CDECL
wined3d_device_draw_indexed_primitive(struct wined3d_device
*device
, UINT start_idx
, UINT index_count
)
4170 TRACE("device %p, start_idx %u, index_count %u.\n", device
, start_idx
, index_count
);
4172 if (!device
->state
.index_buffer
)
4174 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4175 * without an index buffer set. (The first time at least...)
4176 * D3D8 simply dies, but I doubt it can do much harm to return
4177 * D3DERR_INVALIDCALL there as well. */
4178 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4179 return WINED3DERR_INVALIDCALL
;
4182 wined3d_cs_emit_draw(device
->cs
, device
->state
.primitive_type
, device
->state
.patch_vertex_count
,
4183 device
->state
.base_vertex_index
, start_idx
, index_count
, 0, 0, true);
4188 void CDECL
wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device
*device
,
4189 UINT start_idx
, UINT index_count
, UINT start_instance
, UINT instance_count
)
4191 TRACE("device %p, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4192 device
, start_idx
, index_count
, start_instance
, instance_count
);
4194 wined3d_cs_emit_draw(device
->cs
, device
->state
.primitive_type
, device
->state
.patch_vertex_count
,
4195 device
->state
.base_vertex_index
, start_idx
, index_count
, start_instance
, instance_count
, true);
4198 void CDECL
wined3d_device_draw_indexed_primitive_instanced_indirect(struct wined3d_device
*device
,
4199 struct wined3d_buffer
*buffer
, unsigned int offset
)
4201 TRACE("device %p, buffer %p, offset %u.\n", device
, buffer
, offset
);
4203 wined3d_cs_emit_draw_indirect(device
->cs
, device
->state
.primitive_type
,
4204 device
->state
.patch_vertex_count
, buffer
, offset
, true);
4207 HRESULT CDECL
wined3d_device_update_texture(struct wined3d_device
*device
,
4208 struct wined3d_texture
*src_texture
, struct wined3d_texture
*dst_texture
)
4210 unsigned int src_size
, dst_size
, src_skip_levels
= 0;
4211 unsigned int src_level_count
, dst_level_count
;
4212 const struct wined3d_dirty_regions
*regions
;
4213 unsigned int layer_count
, level_count
, i
, j
;
4214 enum wined3d_resource_type type
;
4215 BOOL entire_texture
= TRUE
;
4216 struct wined3d_box box
;
4218 TRACE("device %p, src_texture %p, dst_texture %p.\n", device
, src_texture
, dst_texture
);
4220 /* Verify that the source and destination textures are non-NULL. */
4221 if (!src_texture
|| !dst_texture
)
4223 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4224 return WINED3DERR_INVALIDCALL
;
4227 if (src_texture
->resource
.access
& WINED3D_RESOURCE_ACCESS_GPU
4228 || src_texture
->resource
.usage
& WINED3DUSAGE_SCRATCH
)
4230 WARN("Source resource is GPU accessible or a scratch resource.\n");
4231 return WINED3DERR_INVALIDCALL
;
4233 if (dst_texture
->resource
.access
& WINED3D_RESOURCE_ACCESS_CPU
)
4235 WARN("Destination resource is CPU accessible.\n");
4236 return WINED3DERR_INVALIDCALL
;
4239 /* Verify that the source and destination textures are the same type. */
4240 type
= src_texture
->resource
.type
;
4241 if (dst_texture
->resource
.type
!= type
)
4243 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4244 return WINED3DERR_INVALIDCALL
;
4247 layer_count
= src_texture
->layer_count
;
4248 if (layer_count
!= dst_texture
->layer_count
)
4250 WARN("Source and destination have different layer counts.\n");
4251 return WINED3DERR_INVALIDCALL
;
4254 if (src_texture
->resource
.format
!= dst_texture
->resource
.format
)
4256 WARN("Source and destination formats do not match.\n");
4257 return WINED3DERR_INVALIDCALL
;
4260 src_level_count
= src_texture
->level_count
;
4261 dst_level_count
= dst_texture
->level_count
;
4262 level_count
= min(src_level_count
, dst_level_count
);
4264 src_size
= max(src_texture
->resource
.width
, src_texture
->resource
.height
);
4265 src_size
= max(src_size
, src_texture
->resource
.depth
);
4266 dst_size
= max(dst_texture
->resource
.width
, dst_texture
->resource
.height
);
4267 dst_size
= max(dst_size
, dst_texture
->resource
.depth
);
4268 while (src_size
> dst_size
)
4274 if (wined3d_texture_get_level_width(src_texture
, src_skip_levels
) != dst_texture
->resource
.width
4275 || wined3d_texture_get_level_height(src_texture
, src_skip_levels
) != dst_texture
->resource
.height
4276 || wined3d_texture_get_level_depth(src_texture
, src_skip_levels
) != dst_texture
->resource
.depth
)
4278 WARN("Source and destination dimensions do not match.\n");
4279 return WINED3DERR_INVALIDCALL
;
4282 if ((regions
= src_texture
->dirty_regions
))
4284 for (i
= 0; i
< layer_count
&& entire_texture
; ++i
)
4286 if (regions
[i
].box_count
>= WINED3D_MAX_DIRTY_REGION_COUNT
)
4289 entire_texture
= FALSE
;
4294 /* Update every surface level of the texture. */
4297 for (i
= 0; i
< level_count
; ++i
)
4299 wined3d_texture_get_level_box(dst_texture
, i
, &box
);
4300 for (j
= 0; j
< layer_count
; ++j
)
4302 wined3d_cs_emit_blt_sub_resource(device
->cs
,
4303 &dst_texture
->resource
, j
* dst_level_count
+ i
, &box
,
4304 &src_texture
->resource
, j
* src_level_count
+ i
+ src_skip_levels
, &box
,
4305 0, NULL
, WINED3D_TEXF_POINT
);
4311 unsigned int src_level
, box_count
, k
;
4312 const struct wined3d_box
*boxes
;
4313 struct wined3d_box b
;
4315 for (i
= 0; i
< layer_count
; ++i
)
4317 boxes
= regions
[i
].boxes
;
4318 box_count
= regions
[i
].box_count
;
4319 if (regions
[i
].box_count
>= WINED3D_MAX_DIRTY_REGION_COUNT
)
4323 wined3d_texture_get_level_box(dst_texture
, i
, &b
);
4326 for (j
= 0; j
< level_count
; ++j
)
4328 src_level
= j
+ src_skip_levels
;
4330 /* TODO: We could pass an array of boxes here to avoid
4331 * multiple context acquisitions for the same resource. */
4332 for (k
= 0; k
< box_count
; ++k
)
4337 box
.left
>>= src_level
;
4338 box
.top
>>= src_level
;
4339 box
.right
= min((box
.right
+ (1u << src_level
) - 1) >> src_level
,
4340 wined3d_texture_get_level_width(src_texture
, src_level
));
4341 box
.bottom
= min((box
.bottom
+ (1u << src_level
) - 1) >> src_level
,
4342 wined3d_texture_get_level_height(src_texture
, src_level
));
4343 box
.front
>>= src_level
;
4344 box
.back
= min((box
.back
+ (1u << src_level
) - 1) >> src_level
,
4345 wined3d_texture_get_level_depth(src_texture
, src_level
));
4348 wined3d_cs_emit_blt_sub_resource(device
->cs
,
4349 &dst_texture
->resource
, i
* dst_level_count
+ j
, &box
,
4350 &src_texture
->resource
, i
* src_level_count
+ src_level
, &box
,
4351 0, NULL
, WINED3D_TEXF_POINT
);
4357 wined3d_texture_clear_dirty_regions(src_texture
);
4362 HRESULT CDECL
wined3d_device_validate_device(const struct wined3d_device
*device
, DWORD
*num_passes
)
4364 const struct wined3d_state
*state
= &device
->state
;
4365 struct wined3d_texture
*texture
;
4368 TRACE("device %p, num_passes %p.\n", device
, num_passes
);
4370 for (i
= 0; i
< WINED3D_MAX_COMBINED_SAMPLERS
; ++i
)
4372 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] == WINED3D_TEXF_NONE
)
4374 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
4375 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
4377 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] == WINED3D_TEXF_NONE
)
4379 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i
);
4380 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER
;
4383 texture
= state
->textures
[i
];
4384 if (!texture
|| texture
->resource
.format_flags
& WINED3DFMT_FLAG_FILTERING
) continue;
4386 if (state
->sampler_states
[i
][WINED3D_SAMP_MAG_FILTER
] != WINED3D_TEXF_POINT
)
4388 WARN("Non-filterable texture and mag filter enabled on sampler %u, returning E_FAIL\n", i
);
4391 if (state
->sampler_states
[i
][WINED3D_SAMP_MIN_FILTER
] != WINED3D_TEXF_POINT
)
4393 WARN("Non-filterable texture and min filter enabled on sampler %u, returning E_FAIL\n", i
);
4396 if (state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_NONE
4397 && state
->sampler_states
[i
][WINED3D_SAMP_MIP_FILTER
] != WINED3D_TEXF_POINT
)
4399 WARN("Non-filterable texture and mip filter enabled on sampler %u, returning E_FAIL\n", i
);
4404 if (wined3d_state_uses_depth_buffer(state
)
4405 || (state
->depth_stencil_state
&& state
->depth_stencil_state
->desc
.stencil
))
4407 struct wined3d_rendertarget_view
*rt
= device
->state
.fb
.render_targets
[0];
4408 struct wined3d_rendertarget_view
*ds
= device
->state
.fb
.depth_stencil
;
4410 if (ds
&& rt
&& (ds
->width
< rt
->width
|| ds
->height
< rt
->height
))
4412 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4413 return WINED3DERR_CONFLICTINGRENDERSTATE
;
4417 /* return a sensible default */
4420 TRACE("returning D3D_OK\n");
4424 void CDECL
wined3d_device_set_software_vertex_processing(struct wined3d_device
*device
, BOOL software
)
4428 TRACE("device %p, software %#x.\n", device
, software
);
4432 FIXME("device %p, software %#x stub!\n", device
, software
);
4436 device
->softwareVertexProcessing
= software
;
4439 BOOL CDECL
wined3d_device_get_software_vertex_processing(const struct wined3d_device
*device
)
4443 TRACE("device %p.\n", device
);
4447 TRACE("device %p stub!\n", device
);
4451 return device
->softwareVertexProcessing
;
4454 HRESULT CDECL
wined3d_device_get_raster_status(const struct wined3d_device
*device
,
4455 UINT swapchain_idx
, struct wined3d_raster_status
*raster_status
)
4457 struct wined3d_swapchain
*swapchain
;
4459 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4460 device
, swapchain_idx
, raster_status
);
4462 if (!(swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
4463 return WINED3DERR_INVALIDCALL
;
4465 return wined3d_swapchain_get_raster_status(swapchain
, raster_status
);
4468 HRESULT CDECL
wined3d_device_set_npatch_mode(struct wined3d_device
*device
, float segments
)
4472 TRACE("device %p, segments %.8e.\n", device
, segments
);
4474 if (segments
!= 0.0f
)
4478 FIXME("device %p, segments %.8e stub!\n", device
, segments
);
4486 float CDECL
wined3d_device_get_npatch_mode(const struct wined3d_device
*device
)
4490 TRACE("device %p.\n", device
);
4494 FIXME("device %p stub!\n", device
);
4501 void CDECL
wined3d_device_copy_uav_counter(struct wined3d_device
*device
,
4502 struct wined3d_buffer
*dst_buffer
, unsigned int offset
, struct wined3d_unordered_access_view
*uav
)
4504 TRACE("device %p, dst_buffer %p, offset %u, uav %p.\n",
4505 device
, dst_buffer
, offset
, uav
);
4507 wined3d_cs_emit_copy_uav_counter(device
->cs
, dst_buffer
, offset
, uav
);
4510 void CDECL
wined3d_device_copy_resource(struct wined3d_device
*device
,
4511 struct wined3d_resource
*dst_resource
, struct wined3d_resource
*src_resource
)
4513 struct wined3d_texture
*dst_texture
, *src_texture
;
4514 struct wined3d_box box
;
4517 TRACE("device %p, dst_resource %p, src_resource %p.\n", device
, dst_resource
, src_resource
);
4519 if (src_resource
== dst_resource
)
4521 WARN("Source and destination are the same resource.\n");
4525 if (src_resource
->type
!= dst_resource
->type
)
4527 WARN("Resource types (%s / %s) don't match.\n",
4528 debug_d3dresourcetype(dst_resource
->type
),
4529 debug_d3dresourcetype(src_resource
->type
));
4533 if (src_resource
->width
!= dst_resource
->width
4534 || src_resource
->height
!= dst_resource
->height
4535 || src_resource
->depth
!= dst_resource
->depth
)
4537 WARN("Resource dimensions (%ux%ux%u / %ux%ux%u) don't match.\n",
4538 dst_resource
->width
, dst_resource
->height
, dst_resource
->depth
,
4539 src_resource
->width
, src_resource
->height
, src_resource
->depth
);
4543 if (src_resource
->format
->typeless_id
!= dst_resource
->format
->typeless_id
4544 || (!src_resource
->format
->typeless_id
&& src_resource
->format
->id
!= dst_resource
->format
->id
))
4546 WARN("Resource formats %s and %s are incompatible.\n",
4547 debug_d3dformat(dst_resource
->format
->id
),
4548 debug_d3dformat(src_resource
->format
->id
));
4552 if (dst_resource
->type
== WINED3D_RTYPE_BUFFER
)
4554 wined3d_box_set(&box
, 0, 0, src_resource
->size
, 1, 0, 1);
4555 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, 0, &box
,
4556 src_resource
, 0, &box
, WINED3D_BLT_RAW
, NULL
, WINED3D_TEXF_POINT
);
4560 dst_texture
= texture_from_resource(dst_resource
);
4561 src_texture
= texture_from_resource(src_resource
);
4563 if (src_texture
->layer_count
!= dst_texture
->layer_count
4564 || src_texture
->level_count
!= dst_texture
->level_count
)
4566 WARN("Subresource layouts (%ux%u / %ux%u) don't match.\n",
4567 dst_texture
->layer_count
, dst_texture
->level_count
,
4568 src_texture
->layer_count
, src_texture
->level_count
);
4572 for (i
= 0; i
< dst_texture
->level_count
; ++i
)
4574 wined3d_texture_get_level_box(dst_texture
, i
, &box
);
4575 for (j
= 0; j
< dst_texture
->layer_count
; ++j
)
4577 unsigned int idx
= j
* dst_texture
->level_count
+ i
;
4579 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, idx
, &box
,
4580 src_resource
, idx
, &box
, WINED3D_BLT_RAW
, NULL
, WINED3D_TEXF_POINT
);
4585 HRESULT CDECL
wined3d_device_copy_sub_resource_region(struct wined3d_device
*device
,
4586 struct wined3d_resource
*dst_resource
, unsigned int dst_sub_resource_idx
, unsigned int dst_x
,
4587 unsigned int dst_y
, unsigned int dst_z
, struct wined3d_resource
*src_resource
,
4588 unsigned int src_sub_resource_idx
, const struct wined3d_box
*src_box
, unsigned int flags
)
4590 struct wined3d_box dst_box
, b
;
4592 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4593 "src_resource %p, src_sub_resource_idx %u, src_box %s, flags %#x.\n",
4594 device
, dst_resource
, dst_sub_resource_idx
, dst_x
, dst_y
, dst_z
,
4595 src_resource
, src_sub_resource_idx
, debug_box(src_box
), flags
);
4598 FIXME("Ignoring flags %#x.\n", flags
);
4600 if (src_resource
== dst_resource
&& src_sub_resource_idx
== dst_sub_resource_idx
)
4602 WARN("Source and destination are the same sub-resource.\n");
4603 return WINED3DERR_INVALIDCALL
;
4606 if (src_resource
->format
->typeless_id
!= dst_resource
->format
->typeless_id
4607 || (!src_resource
->format
->typeless_id
&& src_resource
->format
->id
!= dst_resource
->format
->id
))
4609 WARN("Resource formats %s and %s are incompatible.\n",
4610 debug_d3dformat(dst_resource
->format
->id
),
4611 debug_d3dformat(src_resource
->format
->id
));
4612 return WINED3DERR_INVALIDCALL
;
4615 if (dst_resource
->type
== WINED3D_RTYPE_BUFFER
)
4617 if (src_resource
->type
!= WINED3D_RTYPE_BUFFER
)
4619 WARN("Resource types (%s / %s) don't match.\n",
4620 debug_d3dresourcetype(dst_resource
->type
),
4621 debug_d3dresourcetype(src_resource
->type
));
4622 return WINED3DERR_INVALIDCALL
;
4625 if (dst_sub_resource_idx
)
4627 WARN("Invalid dst_sub_resource_idx %u.\n", dst_sub_resource_idx
);
4628 return WINED3DERR_INVALIDCALL
;
4631 if (src_sub_resource_idx
)
4633 WARN("Invalid src_sub_resource_idx %u.\n", src_sub_resource_idx
);
4634 return WINED3DERR_INVALIDCALL
;
4641 dst_w
= dst_resource
->size
- dst_x
;
4642 wined3d_box_set(&b
, 0, 0, min(src_resource
->size
, dst_w
), 1, 0, 1);
4645 else if ((src_box
->left
>= src_box
->right
4646 || src_box
->top
>= src_box
->bottom
4647 || src_box
->front
>= src_box
->back
))
4649 WARN("Invalid box %s specified.\n", debug_box(src_box
));
4650 return WINED3DERR_INVALIDCALL
;
4653 if (src_box
->right
> src_resource
->size
|| dst_x
>= dst_resource
->size
4654 || src_box
->right
- src_box
->left
> dst_resource
->size
- dst_x
)
4656 WARN("Invalid range specified, dst_offset %u, src_offset %u, size %u.\n",
4657 dst_x
, src_box
->left
, src_box
->right
- src_box
->left
);
4658 return WINED3DERR_INVALIDCALL
;
4661 wined3d_box_set(&dst_box
, dst_x
, 0, dst_x
+ (src_box
->right
- src_box
->left
), 1, 0, 1);
4665 struct wined3d_texture
*dst_texture
= texture_from_resource(dst_resource
);
4666 struct wined3d_texture
*src_texture
= texture_from_resource(src_resource
);
4667 unsigned int src_level
= src_sub_resource_idx
% src_texture
->level_count
;
4669 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
)
4671 WARN("Invalid destination sub-resource %u.\n", dst_sub_resource_idx
);
4672 return WINED3DERR_INVALIDCALL
;
4675 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
)
4677 WARN("Invalid source sub-resource %u.\n", src_sub_resource_idx
);
4678 return WINED3DERR_INVALIDCALL
;
4681 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
)
4683 WARN("Destination sub-resource %u is mapped.\n", dst_sub_resource_idx
);
4684 return WINED3DERR_INVALIDCALL
;
4687 if (src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
4689 WARN("Source sub-resource %u is mapped.\n", src_sub_resource_idx
);
4690 return WINED3DERR_INVALIDCALL
;
4695 unsigned int src_w
, src_h
, src_d
, dst_w
, dst_h
, dst_d
, dst_level
;
4697 src_w
= wined3d_texture_get_level_width(src_texture
, src_level
);
4698 src_h
= wined3d_texture_get_level_height(src_texture
, src_level
);
4699 src_d
= wined3d_texture_get_level_depth(src_texture
, src_level
);
4701 dst_level
= dst_sub_resource_idx
% dst_texture
->level_count
;
4702 dst_w
= wined3d_texture_get_level_width(dst_texture
, dst_level
) - dst_x
;
4703 dst_h
= wined3d_texture_get_level_height(dst_texture
, dst_level
) - dst_y
;
4704 dst_d
= wined3d_texture_get_level_depth(dst_texture
, dst_level
) - dst_z
;
4706 wined3d_box_set(&b
, 0, 0, min(src_w
, dst_w
), min(src_h
, dst_h
), 0, min(src_d
, dst_d
));
4709 else if (FAILED(wined3d_texture_check_box_dimensions(src_texture
, src_level
, src_box
)))
4711 WARN("Invalid source box %s.\n", debug_box(src_box
));
4712 return WINED3DERR_INVALIDCALL
;
4715 wined3d_box_set(&dst_box
, dst_x
, dst_y
, dst_x
+ (src_box
->right
- src_box
->left
),
4716 dst_y
+ (src_box
->bottom
- src_box
->top
), dst_z
, dst_z
+ (src_box
->back
- src_box
->front
));
4717 if (FAILED(wined3d_texture_check_box_dimensions(dst_texture
,
4718 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
4720 WARN("Invalid destination box %s.\n", debug_box(&dst_box
));
4721 return WINED3DERR_INVALIDCALL
;
4725 wined3d_cs_emit_blt_sub_resource(device
->cs
, dst_resource
, dst_sub_resource_idx
, &dst_box
,
4726 src_resource
, src_sub_resource_idx
, src_box
, WINED3D_BLT_RAW
, NULL
, WINED3D_TEXF_POINT
);
4731 void CDECL
wined3d_device_update_sub_resource(struct wined3d_device
*device
, struct wined3d_resource
*resource
,
4732 unsigned int sub_resource_idx
, const struct wined3d_box
*box
, const void *data
, unsigned int row_pitch
,
4733 unsigned int depth_pitch
, unsigned int flags
)
4735 unsigned int width
, height
, depth
;
4736 struct wined3d_box b
;
4738 TRACE("device %p, resource %p, sub_resource_idx %u, box %s, data %p, row_pitch %u, depth_pitch %u, "
4740 device
, resource
, sub_resource_idx
, debug_box(box
), data
, row_pitch
, depth_pitch
, flags
);
4743 FIXME("Ignoring flags %#x.\n", flags
);
4745 if (!(resource
->access
& WINED3D_RESOURCE_ACCESS_GPU
))
4747 WARN("Resource %p is not GPU accessible.\n", resource
);
4751 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
4753 if (sub_resource_idx
> 0)
4755 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
4759 width
= resource
->size
;
4765 struct wined3d_texture
*texture
= texture_from_resource(resource
);
4768 if (sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
4770 WARN("Invalid sub_resource_idx %u.\n", sub_resource_idx
);
4774 level
= sub_resource_idx
% texture
->level_count
;
4775 width
= wined3d_texture_get_level_width(texture
, level
);
4776 height
= wined3d_texture_get_level_height(texture
, level
);
4777 depth
= wined3d_texture_get_level_depth(texture
, level
);
4782 wined3d_box_set(&b
, 0, 0, width
, height
, 0, depth
);
4785 else if (box
->left
>= box
->right
|| box
->right
> width
4786 || box
->top
>= box
->bottom
|| box
->bottom
> height
4787 || box
->front
>= box
->back
|| box
->back
> depth
)
4789 WARN("Invalid box %s specified.\n", debug_box(box
));
4793 wined3d_resource_wait_idle(resource
);
4795 wined3d_cs_emit_update_sub_resource(device
->cs
, resource
, sub_resource_idx
, box
, data
, row_pitch
, depth_pitch
);
4798 void CDECL
wined3d_device_resolve_sub_resource(struct wined3d_device
*device
,
4799 struct wined3d_resource
*dst_resource
, unsigned int dst_sub_resource_idx
,
4800 struct wined3d_resource
*src_resource
, unsigned int src_sub_resource_idx
,
4801 enum wined3d_format_id format_id
)
4803 struct wined3d_texture
*dst_texture
, *src_texture
;
4804 unsigned int dst_level
, src_level
;
4805 RECT dst_rect
, src_rect
;
4807 TRACE("device %p, dst_resource %p, dst_sub_resource_idx %u, "
4808 "src_resource %p, src_sub_resource_idx %u, format %s.\n",
4809 device
, dst_resource
, dst_sub_resource_idx
,
4810 src_resource
, src_sub_resource_idx
, debug_d3dformat(format_id
));
4812 if (wined3d_format_is_typeless(dst_resource
->format
)
4813 || wined3d_format_is_typeless(src_resource
->format
))
4815 FIXME("Multisample resolve is not fully supported for typeless formats "
4816 "(dst_format %s, src_format %s, format %s).\n",
4817 debug_d3dformat(dst_resource
->format
->id
), debug_d3dformat(src_resource
->format
->id
),
4818 debug_d3dformat(format_id
));
4820 if (dst_resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
4822 WARN("Invalid destination resource type %s.\n", debug_d3dresourcetype(dst_resource
->type
));
4825 if (src_resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
4827 WARN("Invalid source resource type %s.\n", debug_d3dresourcetype(src_resource
->type
));
4831 dst_texture
= texture_from_resource(dst_resource
);
4832 src_texture
= texture_from_resource(src_resource
);
4834 dst_level
= dst_sub_resource_idx
% dst_texture
->level_count
;
4835 SetRect(&dst_rect
, 0, 0, wined3d_texture_get_level_width(dst_texture
, dst_level
),
4836 wined3d_texture_get_level_height(dst_texture
, dst_level
));
4837 src_level
= src_sub_resource_idx
% src_texture
->level_count
;
4838 SetRect(&src_rect
, 0, 0, wined3d_texture_get_level_width(src_texture
, src_level
),
4839 wined3d_texture_get_level_height(src_texture
, src_level
));
4840 wined3d_texture_blt(dst_texture
, dst_sub_resource_idx
, &dst_rect
,
4841 src_texture
, src_sub_resource_idx
, &src_rect
, 0, NULL
, WINED3D_TEXF_POINT
);
4844 HRESULT CDECL
wined3d_device_clear_rendertarget_view(struct wined3d_device
*device
,
4845 struct wined3d_rendertarget_view
*view
, const RECT
*rect
, DWORD flags
,
4846 const struct wined3d_color
*color
, float depth
, DWORD stencil
)
4848 struct wined3d_resource
*resource
;
4851 TRACE("device %p, view %p, rect %s, flags %#x, color %s, depth %.8e, stencil %u.\n",
4852 device
, view
, wine_dbgstr_rect(rect
), flags
, debug_color(color
), depth
, stencil
);
4857 resource
= view
->resource
;
4858 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
4860 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
4861 return WINED3DERR_INVALIDCALL
;
4864 if (view
->layer_count
!= max(1, resource
->depth
>> view
->desc
.u
.texture
.level_idx
))
4866 FIXME("Layered clears not implemented.\n");
4867 return WINED3DERR_INVALIDCALL
;
4872 SetRect(&r
, 0, 0, view
->width
, view
->height
);
4877 struct wined3d_box b
= {rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1};
4878 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
4881 if (FAILED(hr
= wined3d_texture_check_box_dimensions(texture
,
4882 view
->sub_resource_idx
% texture
->level_count
, &b
)))
4886 wined3d_cs_emit_clear_rendertarget_view(device
->cs
, view
, rect
, flags
, color
, depth
, stencil
);
4891 void CDECL
wined3d_device_clear_unordered_access_view_uint(struct wined3d_device
*device
,
4892 struct wined3d_unordered_access_view
*view
, const struct wined3d_uvec4
*clear_value
)
4894 TRACE("device %p, view %p, clear_value %s.\n", device
, view
, debug_uvec4(clear_value
));
4896 wined3d_cs_emit_clear_unordered_access_view_uint(device
->cs
, view
, clear_value
);
4899 struct wined3d_rendertarget_view
* CDECL
wined3d_device_get_rendertarget_view(const struct wined3d_device
*device
,
4900 unsigned int view_idx
)
4902 unsigned int max_rt_count
;
4904 TRACE("device %p, view_idx %u.\n", device
, view_idx
);
4906 max_rt_count
= device
->adapter
->d3d_info
.limits
.max_rt_count
;
4907 if (view_idx
>= max_rt_count
)
4909 WARN("Only %u render targets are supported.\n", max_rt_count
);
4913 return device
->state
.fb
.render_targets
[view_idx
];
4916 struct wined3d_rendertarget_view
* CDECL
wined3d_device_get_depth_stencil_view(const struct wined3d_device
*device
)
4918 TRACE("device %p.\n", device
);
4920 return device
->state
.fb
.depth_stencil
;
4923 static void wined3d_unbind_srv_for_rtv(struct wined3d_device
*device
,
4924 const struct wined3d_rendertarget_view
*view
, BOOL dsv
)
4926 if (view
&& wined3d_is_rtv_srv_bound(view
))
4928 const struct wined3d_resource
*resource
= view
->resource
;
4929 const struct wined3d_shader_resource_view
*srv
;
4932 WARN("Application sets bound resource as render target.\n");
4934 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
4935 for (j
= 0; j
< MAX_SHADER_RESOURCE_VIEWS
; ++j
)
4936 if ((srv
= device
->state
.shader_resource_view
[i
][j
]) && srv
->resource
== resource
4937 && ((!dsv
&& wined3d_is_srv_rtv_bound(srv
))
4938 || (dsv
&& wined3d_dsv_srv_conflict(view
, srv
->format
))))
4939 wined3d_device_set_shader_resource_view(device
, i
, j
, NULL
);
4943 HRESULT CDECL
wined3d_device_set_rendertarget_view(struct wined3d_device
*device
,
4944 unsigned int view_idx
, struct wined3d_rendertarget_view
*view
, BOOL set_viewport
)
4946 struct wined3d_rendertarget_view
*prev
;
4947 unsigned int max_rt_count
;
4949 TRACE("device %p, view_idx %u, view %p, set_viewport %#x.\n",
4950 device
, view_idx
, view
, set_viewport
);
4952 max_rt_count
= device
->adapter
->d3d_info
.limits
.max_rt_count
;
4953 if (view_idx
>= max_rt_count
)
4955 WARN("Only %u render targets are supported.\n", max_rt_count
);
4956 return WINED3DERR_INVALIDCALL
;
4959 if (view
&& !(view
->resource
->bind_flags
& WINED3D_BIND_RENDER_TARGET
))
4961 WARN("View resource %p doesn't have render target bind flags.\n", view
->resource
);
4962 return WINED3DERR_INVALIDCALL
;
4965 /* Set the viewport and scissor rectangles, if requested. Tests show that
4966 * stateblock recording is ignored, the change goes directly into the
4967 * primary stateblock. */
4968 if (!view_idx
&& set_viewport
)
4970 struct wined3d_state
*state
= &device
->state
;
4972 state
->viewports
[0].x
= 0;
4973 state
->viewports
[0].y
= 0;
4974 state
->viewports
[0].width
= view
->width
;
4975 state
->viewports
[0].height
= view
->height
;
4976 state
->viewports
[0].min_z
= 0.0f
;
4977 state
->viewports
[0].max_z
= 1.0f
;
4978 state
->viewport_count
= 1;
4979 wined3d_cs_emit_set_viewports(device
->cs
, 1, state
->viewports
);
4981 SetRect(&state
->scissor_rects
[0], 0, 0, view
->width
, view
->height
);
4982 state
->scissor_rect_count
= 1;
4983 wined3d_cs_emit_set_scissor_rects(device
->cs
, 1, state
->scissor_rects
);
4986 prev
= device
->state
.fb
.render_targets
[view_idx
];
4992 wined3d_rendertarget_view_incref(view
);
4993 wined3d_rtv_bind_count_inc(view
);
4995 device
->state
.fb
.render_targets
[view_idx
] = view
;
4996 wined3d_cs_emit_set_rendertarget_view(device
->cs
, view_idx
, view
);
4997 /* Release after the assignment, to prevent device_resource_released()
4998 * from seeing the surface as still in use. */
5001 wined3d_rtv_bind_count_dec(prev
);
5002 wined3d_rendertarget_view_decref(prev
);
5005 wined3d_unbind_srv_for_rtv(device
, view
, FALSE
);
5010 HRESULT CDECL
wined3d_device_set_depth_stencil_view(struct wined3d_device
*device
,
5011 struct wined3d_rendertarget_view
*view
)
5013 struct wined3d_rendertarget_view
*prev
;
5015 TRACE("device %p, view %p.\n", device
, view
);
5017 if (view
&& !(view
->resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
))
5019 WARN("View resource %p has incompatible %s bind flags.\n",
5020 view
->resource
, wined3d_debug_bind_flags(view
->resource
->bind_flags
));
5021 return WINED3DERR_INVALIDCALL
;
5024 prev
= device
->state
.fb
.depth_stencil
;
5027 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
5031 if ((device
->state
.fb
.depth_stencil
= view
))
5032 wined3d_rendertarget_view_incref(view
);
5033 wined3d_cs_emit_set_depth_stencil_view(device
->cs
, view
);
5035 wined3d_rendertarget_view_decref(prev
);
5036 wined3d_unbind_srv_for_rtv(device
, view
, TRUE
);
5041 static struct wined3d_texture
*wined3d_device_create_cursor_texture(struct wined3d_device
*device
,
5042 struct wined3d_texture
*cursor_image
, unsigned int sub_resource_idx
)
5044 unsigned int texture_level
= sub_resource_idx
% cursor_image
->level_count
;
5045 struct wined3d_sub_resource_data data
;
5046 struct wined3d_resource_desc desc
;
5047 struct wined3d_map_desc map_desc
;
5048 struct wined3d_texture
*texture
;
5051 if (FAILED(wined3d_resource_map(&cursor_image
->resource
, sub_resource_idx
, &map_desc
, NULL
, WINED3D_MAP_READ
)))
5053 ERR("Failed to map source texture.\n");
5057 data
.data
= map_desc
.data
;
5058 data
.row_pitch
= map_desc
.row_pitch
;
5059 data
.slice_pitch
= map_desc
.slice_pitch
;
5061 desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
5062 desc
.format
= WINED3DFMT_B8G8R8A8_UNORM
;
5063 desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
5064 desc
.multisample_quality
= 0;
5065 desc
.usage
= WINED3DUSAGE_DYNAMIC
;
5066 desc
.bind_flags
= 0;
5067 desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
5068 desc
.width
= wined3d_texture_get_level_width(cursor_image
, texture_level
);
5069 desc
.height
= wined3d_texture_get_level_height(cursor_image
, texture_level
);
5073 hr
= wined3d_texture_create(device
, &desc
, 1, 1, 0, &data
, NULL
, &wined3d_null_parent_ops
, &texture
);
5074 wined3d_resource_unmap(&cursor_image
->resource
, sub_resource_idx
);
5077 ERR("Failed to create cursor texture.\n");
5084 HRESULT CDECL
wined3d_device_set_cursor_properties(struct wined3d_device
*device
,
5085 UINT x_hotspot
, UINT y_hotspot
, struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
5087 unsigned int texture_level
= sub_resource_idx
% texture
->level_count
;
5088 unsigned int cursor_width
, cursor_height
;
5089 struct wined3d_map_desc map_desc
;
5091 TRACE("device %p, x_hotspot %u, y_hotspot %u, texture %p, sub_resource_idx %u.\n",
5092 device
, x_hotspot
, y_hotspot
, texture
, sub_resource_idx
);
5094 if (sub_resource_idx
>= texture
->level_count
* texture
->layer_count
5095 || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
5096 return WINED3DERR_INVALIDCALL
;
5098 if (device
->cursor_texture
)
5100 wined3d_texture_decref(device
->cursor_texture
);
5101 device
->cursor_texture
= NULL
;
5104 if (texture
->resource
.format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
5106 WARN("Texture %p has invalid format %s.\n",
5107 texture
, debug_d3dformat(texture
->resource
.format
->id
));
5108 return WINED3DERR_INVALIDCALL
;
5111 /* Cursor width and height must all be powers of two */
5112 cursor_width
= wined3d_texture_get_level_width(texture
, texture_level
);
5113 cursor_height
= wined3d_texture_get_level_height(texture
, texture_level
);
5114 if ((cursor_width
& (cursor_width
- 1)) || (cursor_height
& (cursor_height
- 1)))
5116 WARN("Cursor size %ux%u are not all powers of two.\n", cursor_width
, cursor_height
);
5117 return WINED3DERR_INVALIDCALL
;
5120 /* Do not store the surface's pointer because the application may
5121 * release it after setting the cursor image. Windows doesn't
5122 * addref the set surface, so we can't do this either without
5123 * creating circular refcount dependencies. */
5124 if (!(device
->cursor_texture
= wined3d_device_create_cursor_texture(device
, texture
, sub_resource_idx
)))
5126 ERR("Failed to create cursor texture.\n");
5127 return WINED3DERR_INVALIDCALL
;
5130 if (cursor_width
== 32 && cursor_height
== 32)
5132 UINT mask_size
= cursor_width
* cursor_height
/ 8;
5133 ICONINFO cursor_info
;
5137 /* 32-bit user32 cursors ignore the alpha channel if it's all
5138 * zeroes, and use the mask instead. Fill the mask with all ones
5139 * to ensure we still get a fully transparent cursor. */
5140 if (!(mask_bits
= heap_alloc(mask_size
)))
5141 return E_OUTOFMEMORY
;
5142 memset(mask_bits
, 0xff, mask_size
);
5144 wined3d_resource_map(&texture
->resource
, sub_resource_idx
, &map_desc
, NULL
,
5145 WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READ
);
5146 cursor_info
.fIcon
= FALSE
;
5147 cursor_info
.xHotspot
= x_hotspot
;
5148 cursor_info
.yHotspot
= y_hotspot
;
5149 cursor_info
.hbmMask
= CreateBitmap(cursor_width
, cursor_height
, 1, 1, mask_bits
);
5150 cursor_info
.hbmColor
= CreateBitmap(cursor_width
, cursor_height
, 1, 32, map_desc
.data
);
5151 wined3d_resource_unmap(&texture
->resource
, sub_resource_idx
);
5153 /* Create our cursor and clean up. */
5154 cursor
= CreateIconIndirect(&cursor_info
);
5155 if (cursor_info
.hbmMask
)
5156 DeleteObject(cursor_info
.hbmMask
);
5157 if (cursor_info
.hbmColor
)
5158 DeleteObject(cursor_info
.hbmColor
);
5159 if (device
->hardwareCursor
)
5160 DestroyCursor(device
->hardwareCursor
);
5161 device
->hardwareCursor
= cursor
;
5162 if (device
->bCursorVisible
)
5165 heap_free(mask_bits
);
5168 TRACE("New cursor dimensions are %ux%u.\n", cursor_width
, cursor_height
);
5169 device
->cursorWidth
= cursor_width
;
5170 device
->cursorHeight
= cursor_height
;
5171 device
->xHotSpot
= x_hotspot
;
5172 device
->yHotSpot
= y_hotspot
;
5177 void CDECL
wined3d_device_set_cursor_position(struct wined3d_device
*device
,
5178 int x_screen_space
, int y_screen_space
, DWORD flags
)
5180 TRACE("device %p, x %d, y %d, flags %#x.\n",
5181 device
, x_screen_space
, y_screen_space
, flags
);
5183 device
->xScreenSpace
= x_screen_space
;
5184 device
->yScreenSpace
= y_screen_space
;
5186 if (device
->hardwareCursor
)
5190 GetCursorPos( &pt
);
5191 if (x_screen_space
== pt
.x
&& y_screen_space
== pt
.y
)
5193 SetCursorPos( x_screen_space
, y_screen_space
);
5195 /* Switch to the software cursor if position diverges from the hardware one. */
5196 GetCursorPos( &pt
);
5197 if (x_screen_space
!= pt
.x
|| y_screen_space
!= pt
.y
)
5199 if (device
->bCursorVisible
) SetCursor( NULL
);
5200 DestroyCursor( device
->hardwareCursor
);
5201 device
->hardwareCursor
= 0;
5206 BOOL CDECL
wined3d_device_show_cursor(struct wined3d_device
*device
, BOOL show
)
5208 BOOL oldVisible
= device
->bCursorVisible
;
5210 TRACE("device %p, show %#x.\n", device
, show
);
5213 * When ShowCursor is first called it should make the cursor appear at the OS's last
5214 * known cursor position.
5216 if (show
&& !oldVisible
)
5220 device
->xScreenSpace
= pt
.x
;
5221 device
->yScreenSpace
= pt
.y
;
5224 if (device
->hardwareCursor
)
5226 device
->bCursorVisible
= show
;
5228 SetCursor(device
->hardwareCursor
);
5232 else if (device
->cursor_texture
)
5234 device
->bCursorVisible
= show
;
5240 void CDECL
wined3d_device_evict_managed_resources(struct wined3d_device
*device
)
5242 struct wined3d_resource
*resource
, *cursor
;
5244 TRACE("device %p.\n", device
);
5246 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
5248 TRACE("Checking resource %p for eviction.\n", resource
);
5250 if (wined3d_resource_access_is_managed(resource
->access
) && !resource
->map_count
)
5252 TRACE("Evicting %p.\n", resource
);
5253 wined3d_cs_emit_unload_resource(device
->cs
, resource
);
5258 void CDECL
wined3d_device_flush(struct wined3d_device
*device
)
5260 TRACE("device %p.\n", device
);
5262 wined3d_cs_emit_flush(device
->cs
);
5265 static void update_swapchain_flags(struct wined3d_texture
*texture
)
5267 unsigned int flags
= texture
->swapchain
->state
.desc
.flags
;
5269 if (flags
& WINED3D_SWAPCHAIN_LOCKABLE_BACKBUFFER
)
5270 texture
->resource
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
5272 texture
->resource
.access
&= ~(WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
);
5274 if (flags
& WINED3D_SWAPCHAIN_GDI_COMPATIBLE
)
5275 texture
->flags
|= WINED3D_TEXTURE_GET_DC
;
5277 texture
->flags
&= ~WINED3D_TEXTURE_GET_DC
;
5280 HRESULT CDECL
wined3d_device_reset(struct wined3d_device
*device
,
5281 const struct wined3d_swapchain_desc
*swapchain_desc
, const struct wined3d_display_mode
*mode
,
5282 wined3d_device_reset_cb callback
, BOOL reset_state
)
5284 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
5285 struct wined3d_swapchain_state
*swapchain_state
;
5286 struct wined3d_swapchain_desc
*current_desc
;
5287 struct wined3d_resource
*resource
, *cursor
;
5288 struct wined3d_rendertarget_view
*view
;
5289 struct wined3d_swapchain
*swapchain
;
5290 struct wined3d_view_desc view_desc
;
5291 BOOL backbuffer_resized
, windowed
;
5292 HRESULT hr
= WINED3D_OK
;
5295 TRACE("device %p, swapchain_desc %p, mode %p, callback %p, reset_state %#x.\n",
5296 device
, swapchain_desc
, mode
, callback
, reset_state
);
5298 wined3d_cs_finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
5300 if (!(swapchain
= wined3d_device_get_swapchain(device
, 0)))
5302 ERR("Failed to get the first implicit swapchain.\n");
5303 return WINED3DERR_INVALIDCALL
;
5305 swapchain_state
= &swapchain
->state
;
5306 current_desc
= &swapchain_state
->desc
;
5310 if (device
->logo_texture
)
5312 wined3d_texture_decref(device
->logo_texture
);
5313 device
->logo_texture
= NULL
;
5315 if (device
->cursor_texture
)
5317 wined3d_texture_decref(device
->cursor_texture
);
5318 device
->cursor_texture
= NULL
;
5320 state_unbind_resources(&device
->state
);
5323 for (i
= 0; i
< d3d_info
->limits
.max_rt_count
; ++i
)
5325 wined3d_device_set_rendertarget_view(device
, i
, NULL
, FALSE
);
5327 wined3d_device_set_depth_stencil_view(device
, NULL
);
5331 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
5333 TRACE("Enumerating resource %p.\n", resource
);
5334 if (FAILED(hr
= callback(resource
)))
5339 TRACE("New params:\n");
5340 TRACE("output %p\n", swapchain_desc
->output
);
5341 TRACE("backbuffer_width %u\n", swapchain_desc
->backbuffer_width
);
5342 TRACE("backbuffer_height %u\n", swapchain_desc
->backbuffer_height
);
5343 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc
->backbuffer_format
));
5344 TRACE("backbuffer_count %u\n", swapchain_desc
->backbuffer_count
);
5345 TRACE("multisample_type %#x\n", swapchain_desc
->multisample_type
);
5346 TRACE("multisample_quality %u\n", swapchain_desc
->multisample_quality
);
5347 TRACE("swap_effect %#x\n", swapchain_desc
->swap_effect
);
5348 TRACE("device_window %p\n", swapchain_desc
->device_window
);
5349 TRACE("windowed %#x\n", swapchain_desc
->windowed
);
5350 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc
->enable_auto_depth_stencil
);
5351 if (swapchain_desc
->enable_auto_depth_stencil
)
5352 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc
->auto_depth_stencil_format
));
5353 TRACE("flags %#x\n", swapchain_desc
->flags
);
5354 TRACE("refresh_rate %u\n", swapchain_desc
->refresh_rate
);
5355 TRACE("auto_restore_display_mode %#x\n", swapchain_desc
->auto_restore_display_mode
);
5357 if (swapchain_desc
->backbuffer_bind_flags
&& swapchain_desc
->backbuffer_bind_flags
!= WINED3D_BIND_RENDER_TARGET
)
5358 FIXME("Got unexpected backbuffer bind flags %#x.\n", swapchain_desc
->backbuffer_bind_flags
);
5360 if (swapchain_desc
->swap_effect
!= WINED3D_SWAP_EFFECT_DISCARD
5361 && swapchain_desc
->swap_effect
!= WINED3D_SWAP_EFFECT_SEQUENTIAL
5362 && swapchain_desc
->swap_effect
!= WINED3D_SWAP_EFFECT_COPY
)
5363 FIXME("Unimplemented swap effect %#x.\n", swapchain_desc
->swap_effect
);
5365 /* No special treatment of these parameters. Just store them */
5366 current_desc
->swap_effect
= swapchain_desc
->swap_effect
;
5367 current_desc
->enable_auto_depth_stencil
= swapchain_desc
->enable_auto_depth_stencil
;
5368 current_desc
->auto_depth_stencil_format
= swapchain_desc
->auto_depth_stencil_format
;
5369 current_desc
->refresh_rate
= swapchain_desc
->refresh_rate
;
5370 current_desc
->auto_restore_display_mode
= swapchain_desc
->auto_restore_display_mode
;
5372 if (swapchain_desc
->device_window
&& swapchain_desc
->device_window
!= current_desc
->device_window
)
5374 TRACE("Changing the device window from %p to %p.\n",
5375 current_desc
->device_window
, swapchain_desc
->device_window
);
5376 current_desc
->device_window
= swapchain_desc
->device_window
;
5377 swapchain_state
->device_window
= swapchain_desc
->device_window
;
5378 wined3d_swapchain_set_window(swapchain
, NULL
);
5381 backbuffer_resized
= swapchain_desc
->backbuffer_width
!= current_desc
->backbuffer_width
5382 || swapchain_desc
->backbuffer_height
!= current_desc
->backbuffer_height
;
5383 windowed
= current_desc
->windowed
;
5385 if (!swapchain_desc
->windowed
!= !windowed
|| swapchain
->reapply_mode
5386 || mode
|| (!swapchain_desc
->windowed
&& backbuffer_resized
))
5388 /* Switch from windowed to fullscreen. */
5389 if (windowed
&& !swapchain_desc
->windowed
)
5391 HWND focus_window
= device
->create_parms
.focus_window
;
5394 focus_window
= swapchain
->state
.device_window
;
5395 if (FAILED(hr
= wined3d_device_acquire_focus_window(device
, focus_window
)))
5397 ERR("Failed to acquire focus window, hr %#x.\n", hr
);
5402 if (FAILED(hr
= wined3d_swapchain_state_set_fullscreen(&swapchain
->state
,
5403 swapchain_desc
, mode
)))
5406 /* Switch from fullscreen to windowed. */
5407 if (!windowed
&& swapchain_desc
->windowed
)
5408 wined3d_device_release_focus_window(device
);
5410 else if (!swapchain_desc
->windowed
)
5412 DWORD style
= swapchain_state
->style
;
5413 DWORD exstyle
= swapchain_state
->exstyle
;
5414 struct wined3d_output_desc output_desc
;
5416 /* If we're in fullscreen, and the mode wasn't changed, we have to get
5417 * the window back into the right position. Some applications
5418 * (Battlefield 2, Guild Wars) move it and then call Reset() to clean
5419 * up their mess. Guild Wars also loses the device during that. */
5420 if (FAILED(hr
= wined3d_output_get_desc(swapchain_desc
->output
, &output_desc
)))
5422 ERR("Failed to get output description, hr %#x.\n", hr
);
5426 swapchain_state
->style
= 0;
5427 swapchain_state
->exstyle
= 0;
5428 wined3d_swapchain_state_setup_fullscreen(swapchain_state
, swapchain_state
->device_window
,
5429 output_desc
.desktop_rect
.left
, output_desc
.desktop_rect
.top
,
5430 swapchain_desc
->backbuffer_width
, swapchain_desc
->backbuffer_height
);
5431 swapchain_state
->style
= style
;
5432 swapchain_state
->exstyle
= exstyle
;
5435 if (FAILED(hr
= wined3d_swapchain_resize_buffers(swapchain
, swapchain_desc
->backbuffer_count
,
5436 swapchain_desc
->backbuffer_width
, swapchain_desc
->backbuffer_height
, swapchain_desc
->backbuffer_format
,
5437 swapchain_desc
->multisample_type
, swapchain_desc
->multisample_quality
)))
5440 if (swapchain_desc
->flags
!= current_desc
->flags
)
5442 current_desc
->flags
= swapchain_desc
->flags
;
5444 update_swapchain_flags(swapchain
->front_buffer
);
5445 for (i
= 0; i
< current_desc
->backbuffer_count
; ++i
)
5447 update_swapchain_flags(swapchain
->back_buffers
[i
]);
5451 if ((view
= device
->auto_depth_stencil_view
))
5453 device
->auto_depth_stencil_view
= NULL
;
5454 wined3d_rendertarget_view_decref(view
);
5456 if (current_desc
->enable_auto_depth_stencil
)
5458 struct wined3d_resource_desc texture_desc
;
5459 struct wined3d_texture
*texture
;
5461 TRACE("Creating the depth stencil buffer.\n");
5463 texture_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
5464 texture_desc
.format
= current_desc
->auto_depth_stencil_format
;
5465 texture_desc
.multisample_type
= current_desc
->multisample_type
;
5466 texture_desc
.multisample_quality
= current_desc
->multisample_quality
;
5467 texture_desc
.usage
= 0;
5468 texture_desc
.bind_flags
= WINED3D_BIND_DEPTH_STENCIL
;
5469 texture_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
5470 texture_desc
.width
= current_desc
->backbuffer_width
;
5471 texture_desc
.height
= current_desc
->backbuffer_height
;
5472 texture_desc
.depth
= 1;
5473 texture_desc
.size
= 0;
5475 if (FAILED(hr
= device
->device_parent
->ops
->create_swapchain_texture(device
->device_parent
,
5476 device
->device_parent
, &texture_desc
, 0, &texture
)))
5478 ERR("Failed to create the auto depth/stencil surface, hr %#x.\n", hr
);
5479 return WINED3DERR_INVALIDCALL
;
5482 view_desc
.format_id
= texture
->resource
.format
->id
;
5483 view_desc
.flags
= 0;
5484 view_desc
.u
.texture
.level_idx
= 0;
5485 view_desc
.u
.texture
.level_count
= 1;
5486 view_desc
.u
.texture
.layer_idx
= 0;
5487 view_desc
.u
.texture
.layer_count
= 1;
5488 hr
= wined3d_rendertarget_view_create(&view_desc
, &texture
->resource
,
5489 NULL
, &wined3d_null_parent_ops
, &device
->auto_depth_stencil_view
);
5490 wined3d_texture_decref(texture
);
5493 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
5498 if ((view
= device
->back_buffer_view
))
5500 device
->back_buffer_view
= NULL
;
5501 wined3d_rendertarget_view_decref(view
);
5503 if (current_desc
->backbuffer_count
&& current_desc
->backbuffer_bind_flags
& WINED3D_BIND_RENDER_TARGET
)
5505 struct wined3d_resource
*back_buffer
= &swapchain
->back_buffers
[0]->resource
;
5507 view_desc
.format_id
= back_buffer
->format
->id
;
5508 view_desc
.flags
= 0;
5509 view_desc
.u
.texture
.level_idx
= 0;
5510 view_desc
.u
.texture
.level_count
= 1;
5511 view_desc
.u
.texture
.layer_idx
= 0;
5512 view_desc
.u
.texture
.layer_count
= 1;
5513 if (FAILED(hr
= wined3d_rendertarget_view_create(&view_desc
, back_buffer
,
5514 NULL
, &wined3d_null_parent_ops
, &device
->back_buffer_view
)))
5516 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
5521 wine_rb_clear(&device
->samplers
, device_free_sampler
, NULL
);
5522 wine_rb_clear(&device
->rasterizer_states
, device_free_rasterizer_state
, NULL
);
5523 wine_rb_clear(&device
->blend_states
, device_free_blend_state
, NULL
);
5524 wine_rb_clear(&device
->depth_stencil_states
, device_free_depth_stencil_state
, NULL
);
5528 TRACE("Resetting state.\n");
5529 wined3d_cs_emit_reset_state(device
->cs
);
5530 state_cleanup(&device
->state
);
5532 LIST_FOR_EACH_ENTRY_SAFE(resource
, cursor
, &device
->resources
, struct wined3d_resource
, resource_list_entry
)
5534 TRACE("Unloading resource %p.\n", resource
);
5535 wined3d_cs_emit_unload_resource(device
->cs
, resource
);
5538 device
->adapter
->adapter_ops
->adapter_uninit_3d(device
);
5540 memset(&device
->state
, 0, sizeof(device
->state
));
5541 state_init(&device
->state
, &device
->adapter
->d3d_info
, WINED3D_STATE_INIT_DEFAULT
);
5543 device_init_swapchain_state(device
, swapchain
);
5544 if (wined3d_settings
.logo
)
5545 device_load_logo(device
, wined3d_settings
.logo
);
5549 if ((view
= device
->back_buffer_view
))
5550 wined3d_device_set_rendertarget_view(device
, 0, view
, FALSE
);
5551 if ((view
= device
->auto_depth_stencil_view
))
5552 wined3d_device_set_depth_stencil_view(device
, view
);
5556 hr
= device
->adapter
->adapter_ops
->adapter_init_3d(device
);
5558 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5564 HRESULT CDECL
wined3d_device_set_dialog_box_mode(struct wined3d_device
*device
, BOOL enable_dialogs
)
5566 TRACE("device %p, enable_dialogs %#x.\n", device
, enable_dialogs
);
5568 if (!enable_dialogs
) FIXME("Dialogs cannot be disabled yet.\n");
5574 void CDECL
wined3d_device_get_creation_parameters(const struct wined3d_device
*device
,
5575 struct wined3d_device_creation_parameters
*parameters
)
5577 TRACE("device %p, parameters %p.\n", device
, parameters
);
5579 *parameters
= device
->create_parms
;
5582 struct wined3d
* CDECL
wined3d_device_get_wined3d(const struct wined3d_device
*device
)
5584 TRACE("device %p.\n", device
);
5586 return device
->wined3d
;
5589 enum wined3d_feature_level CDECL
wined3d_device_get_feature_level(const struct wined3d_device
*device
)
5591 TRACE("device %p.\n", device
);
5593 return device
->feature_level
;
5596 void CDECL
wined3d_device_set_gamma_ramp(const struct wined3d_device
*device
,
5597 UINT swapchain_idx
, DWORD flags
, const struct wined3d_gamma_ramp
*ramp
)
5599 struct wined3d_swapchain
*swapchain
;
5601 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5602 device
, swapchain_idx
, flags
, ramp
);
5604 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
5605 wined3d_swapchain_set_gamma_ramp(swapchain
, flags
, ramp
);
5608 void CDECL
wined3d_device_get_gamma_ramp(const struct wined3d_device
*device
,
5609 UINT swapchain_idx
, struct wined3d_gamma_ramp
*ramp
)
5611 struct wined3d_swapchain
*swapchain
;
5613 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5614 device
, swapchain_idx
, ramp
);
5616 if ((swapchain
= wined3d_device_get_swapchain(device
, swapchain_idx
)))
5617 wined3d_swapchain_get_gamma_ramp(swapchain
, ramp
);
5620 void device_resource_add(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5622 TRACE("device %p, resource %p.\n", device
, resource
);
5624 wined3d_not_from_cs(device
->cs
);
5626 list_add_head(&device
->resources
, &resource
->resource_list_entry
);
5629 static void device_resource_remove(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5631 TRACE("device %p, resource %p.\n", device
, resource
);
5633 wined3d_not_from_cs(device
->cs
);
5635 list_remove(&resource
->resource_list_entry
);
5638 void device_resource_released(struct wined3d_device
*device
, struct wined3d_resource
*resource
)
5640 enum wined3d_resource_type type
= resource
->type
;
5641 struct wined3d_rendertarget_view
*rtv
;
5644 TRACE("device %p, resource %p, type %s.\n", device
, resource
, debug_d3dresourcetype(type
));
5646 for (i
= 0; i
< ARRAY_SIZE(device
->state
.fb
.render_targets
); ++i
)
5648 if ((rtv
= device
->state
.fb
.render_targets
[i
]) && rtv
->resource
== resource
)
5649 ERR("Resource %p is still in use as render target %u.\n", resource
, i
);
5652 if ((rtv
= device
->state
.fb
.depth_stencil
) && rtv
->resource
== resource
)
5653 ERR("Resource %p is still in use as depth/stencil buffer.\n", resource
);
5657 case WINED3D_RTYPE_TEXTURE_1D
:
5658 case WINED3D_RTYPE_TEXTURE_2D
:
5659 case WINED3D_RTYPE_TEXTURE_3D
:
5660 for (i
= 0; i
< WINED3D_MAX_COMBINED_SAMPLERS
; ++i
)
5662 if (&device
->state
.textures
[i
]->resource
== resource
)
5664 ERR("Texture resource %p is still in use, stage %u.\n", resource
, i
);
5665 device
->state
.textures
[i
] = NULL
;
5670 case WINED3D_RTYPE_BUFFER
:
5671 for (i
= 0; i
< WINED3D_MAX_STREAMS
; ++i
)
5673 if (&device
->state
.streams
[i
].buffer
->resource
== resource
)
5675 ERR("Buffer resource %p is still in use, stream %u.\n", resource
, i
);
5676 device
->state
.streams
[i
].buffer
= NULL
;
5680 if (&device
->state
.index_buffer
->resource
== resource
)
5682 ERR("Buffer resource %p is still in use as index buffer.\n", resource
);
5683 device
->state
.index_buffer
= NULL
;
5691 /* Remove the resource from the resourceStore */
5692 device_resource_remove(device
, resource
);
5694 TRACE("Resource released.\n");
5697 static int wined3d_so_desc_compare(const void *key
, const struct wine_rb_entry
*entry
)
5699 const struct wined3d_stream_output_desc
*desc
= &WINE_RB_ENTRY_VALUE(entry
,
5700 struct wined3d_so_desc_entry
, entry
)->desc
;
5701 const struct wined3d_stream_output_desc
*k
= key
;
5705 if ((ret
= (k
->element_count
- desc
->element_count
)))
5707 if ((ret
= (k
->buffer_stride_count
- desc
->buffer_stride_count
)))
5709 if ((ret
= (k
->rasterizer_stream_idx
- desc
->rasterizer_stream_idx
)))
5712 for (i
= 0; i
< k
->element_count
; ++i
)
5714 const struct wined3d_stream_output_element
*b
= &desc
->elements
[i
];
5715 const struct wined3d_stream_output_element
*a
= &k
->elements
[i
];
5717 if ((ret
= (a
->stream_idx
- b
->stream_idx
)))
5719 if ((ret
= strcmp(a
->semantic_name
, b
->semantic_name
)))
5721 if ((ret
= (a
->semantic_idx
- b
->semantic_idx
)))
5723 if ((ret
= (a
->component_idx
- b
->component_idx
)))
5725 if ((ret
= (a
->component_count
- b
->component_count
)))
5727 if ((ret
= (a
->output_slot
- b
->output_slot
)))
5731 for (i
= 0; i
< k
->buffer_stride_count
; ++i
)
5733 if ((ret
= (k
->buffer_strides
[i
] - desc
->buffer_strides
[i
])))
5740 static int wined3d_sampler_compare(const void *key
, const struct wine_rb_entry
*entry
)
5742 const struct wined3d_sampler
*sampler
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_sampler
, entry
);
5744 return memcmp(&sampler
->desc
, key
, sizeof(sampler
->desc
));
5747 static int wined3d_rasterizer_state_compare(const void *key
, const struct wine_rb_entry
*entry
)
5749 const struct wined3d_rasterizer_state
*state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_rasterizer_state
, entry
);
5751 return memcmp(&state
->desc
, key
, sizeof(state
->desc
));
5754 static int wined3d_blend_state_compare(const void *key
, const struct wine_rb_entry
*entry
)
5756 const struct wined3d_blend_state
*state
= WINE_RB_ENTRY_VALUE(entry
, struct wined3d_blend_state
, entry
);
5758 return memcmp(&state
->desc
, key
, sizeof(state
->desc
));
5761 static int wined3d_depth_stencil_state_compare(const void *key
, const struct wine_rb_entry
*entry
)
5763 const struct wined3d_depth_stencil_state
*state
5764 = WINE_RB_ENTRY_VALUE(entry
, struct wined3d_depth_stencil_state
, entry
);
5766 return memcmp(&state
->desc
, key
, sizeof(state
->desc
));
5769 static BOOL
wined3d_select_feature_level(const struct wined3d_adapter
*adapter
,
5770 const enum wined3d_feature_level
*levels
, unsigned int level_count
,
5771 enum wined3d_feature_level
*selected_level
)
5773 const struct wined3d_d3d_info
*d3d_info
= &adapter
->d3d_info
;
5776 for (i
= 0; i
< level_count
; ++i
)
5778 if (levels
[i
] && d3d_info
->feature_level
>= levels
[i
])
5780 *selected_level
= levels
[i
];
5785 FIXME_(winediag
)("None of the requested D3D feature levels is supported on this GPU "
5786 "with the current shader backend.\n");
5790 HRESULT
wined3d_device_init(struct wined3d_device
*device
, struct wined3d
*wined3d
,
5791 unsigned int adapter_idx
, enum wined3d_device_type device_type
, HWND focus_window
, unsigned int flags
,
5792 BYTE surface_alignment
, const enum wined3d_feature_level
*levels
, unsigned int level_count
,
5793 const BOOL
*supported_extensions
, struct wined3d_device_parent
*device_parent
)
5795 struct wined3d_adapter
*adapter
= wined3d
->adapters
[adapter_idx
];
5796 const struct wined3d_fragment_pipe_ops
*fragment_pipeline
;
5797 const struct wined3d_vertex_pipe_ops
*vertex_pipeline
;
5801 if (!wined3d_select_feature_level(adapter
, levels
, level_count
, &device
->feature_level
))
5804 TRACE("Device feature level %s.\n", wined3d_debug_feature_level(device
->feature_level
));
5807 device
->wined3d
= wined3d
;
5808 wined3d_incref(device
->wined3d
);
5809 device
->adapter
= adapter
;
5810 device
->device_parent
= device_parent
;
5811 list_init(&device
->resources
);
5812 list_init(&device
->shaders
);
5813 device
->surface_alignment
= surface_alignment
;
5815 /* Save the creation parameters. */
5816 device
->create_parms
.adapter_idx
= adapter_idx
;
5817 device
->create_parms
.device_type
= device_type
;
5818 device
->create_parms
.focus_window
= focus_window
;
5819 device
->create_parms
.flags
= flags
;
5821 device
->shader_backend
= adapter
->shader_backend
;
5823 vertex_pipeline
= adapter
->vertex_pipe
;
5825 fragment_pipeline
= adapter
->fragment_pipe
;
5827 wine_rb_init(&device
->so_descs
, wined3d_so_desc_compare
);
5828 wine_rb_init(&device
->samplers
, wined3d_sampler_compare
);
5829 wine_rb_init(&device
->rasterizer_states
, wined3d_rasterizer_state_compare
);
5830 wine_rb_init(&device
->blend_states
, wined3d_blend_state_compare
);
5831 wine_rb_init(&device
->depth_stencil_states
, wined3d_depth_stencil_state_compare
);
5833 if (vertex_pipeline
->vp_states
&& fragment_pipeline
->states
5834 && FAILED(hr
= compile_state_table(device
->state_table
, device
->multistate_funcs
,
5835 &adapter
->d3d_info
, supported_extensions
, vertex_pipeline
,
5836 fragment_pipeline
, adapter
->misc_state_template
)))
5838 ERR("Failed to compile state table, hr %#x.\n", hr
);
5839 wine_rb_destroy(&device
->samplers
, NULL
, NULL
);
5840 wine_rb_destroy(&device
->rasterizer_states
, NULL
, NULL
);
5841 wine_rb_destroy(&device
->blend_states
, NULL
, NULL
);
5842 wine_rb_destroy(&device
->depth_stencil_states
, NULL
, NULL
);
5843 wine_rb_destroy(&device
->so_descs
, NULL
, NULL
);
5844 wined3d_decref(device
->wined3d
);
5848 state_init(&device
->state
, &adapter
->d3d_info
, WINED3D_STATE_INIT_DEFAULT
);
5850 device
->max_frame_latency
= 3;
5852 if (!(device
->cs
= wined3d_cs_create(device
)))
5854 WARN("Failed to create command stream.\n");
5855 state_cleanup(&device
->state
);
5863 for (i
= 0; i
< ARRAY_SIZE(device
->multistate_funcs
); ++i
)
5865 heap_free(device
->multistate_funcs
[i
]);
5867 wine_rb_destroy(&device
->samplers
, NULL
, NULL
);
5868 wine_rb_destroy(&device
->rasterizer_states
, NULL
, NULL
);
5869 wine_rb_destroy(&device
->blend_states
, NULL
, NULL
);
5870 wine_rb_destroy(&device
->depth_stencil_states
, NULL
, NULL
);
5871 wine_rb_destroy(&device
->so_descs
, NULL
, NULL
);
5872 wined3d_decref(device
->wined3d
);
5876 void device_invalidate_state(const struct wined3d_device
*device
, unsigned int state_id
)
5878 unsigned int representative
, i
, idx
, shift
;
5879 struct wined3d_context
*context
;
5881 wined3d_from_cs(device
->cs
);
5883 if (STATE_IS_COMPUTE(state_id
))
5885 for (i
= 0; i
< device
->context_count
; ++i
)
5886 context_invalidate_compute_state(device
->contexts
[i
], state_id
);
5890 representative
= device
->state_table
[state_id
].representative
;
5891 idx
= representative
/ (sizeof(*context
->dirty_graphics_states
) * CHAR_BIT
);
5892 shift
= representative
& ((sizeof(*context
->dirty_graphics_states
) * CHAR_BIT
) - 1);
5893 for (i
= 0; i
< device
->context_count
; ++i
)
5895 device
->contexts
[i
]->dirty_graphics_states
[idx
] |= (1u << shift
);
5899 LRESULT
device_process_message(struct wined3d_device
*device
, HWND window
, BOOL unicode
,
5900 UINT message
, WPARAM wparam
, LPARAM lparam
, WNDPROC proc
)
5902 if (message
== WM_DESTROY
)
5904 TRACE("unregister window %p.\n", window
);
5905 wined3d_unregister_window(window
);
5907 if (InterlockedCompareExchangePointer((void **)&device
->focus_window
, NULL
, window
) != window
)
5908 ERR("Window %p is not the focus window for device %p.\n", window
, device
);
5910 else if (message
== WM_DISPLAYCHANGE
)
5912 device
->device_parent
->ops
->mode_changed(device
->device_parent
);
5914 else if (message
== WM_ACTIVATEAPP
)
5916 unsigned int i
= device
->swapchain_count
;
5918 /* Deactivating the implicit swapchain may cause the application
5919 * (e.g. Deus Ex: GOTY) to destroy the device, so take care to
5920 * deactivate the implicit swapchain last, and to avoid accessing the
5921 * "device" pointer afterwards. */
5923 wined3d_swapchain_activate(device
->swapchains
[i
], wparam
);
5925 else if (message
== WM_SYSCOMMAND
)
5927 if (wparam
== SC_RESTORE
&& device
->wined3d
->flags
& WINED3D_HANDLE_RESTORE
)
5930 DefWindowProcW(window
, message
, wparam
, lparam
);
5932 DefWindowProcA(window
, message
, wparam
, lparam
);
5937 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
5939 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);