wined3d: Implement mode setting in the adapter instead of the device.
[wine/multimedia.git] / dlls / wined3d / device.c
blob54eea3fccd176476b058376b6ac859b736478bca
1 /*
2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
48 0.0f, /* Range */
49 0.0f, /* Falloff */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
51 0.0f, /* Theta */
52 0.0f /* Phi */
55 /**********************************************************
56 * Global variable / Constants follow
57 **********************************************************/
58 const struct wined3d_matrix identity =
59 {{{
60 1.0f, 0.0f, 0.0f, 0.0f,
61 0.0f, 1.0f, 0.0f, 0.0f,
62 0.0f, 0.0f, 1.0f, 0.0f,
63 0.0f, 0.0f, 0.0f, 1.0f,
64 }}}; /* When needed for comparisons */
66 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
67 * actually have the same values in GL and D3D. */
68 static GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
70 switch(primitive_type)
72 case WINED3D_PT_POINTLIST:
73 return GL_POINTS;
75 case WINED3D_PT_LINELIST:
76 return GL_LINES;
78 case WINED3D_PT_LINESTRIP:
79 return GL_LINE_STRIP;
81 case WINED3D_PT_TRIANGLELIST:
82 return GL_TRIANGLES;
84 case WINED3D_PT_TRIANGLESTRIP:
85 return GL_TRIANGLE_STRIP;
87 case WINED3D_PT_TRIANGLEFAN:
88 return GL_TRIANGLE_FAN;
90 case WINED3D_PT_LINELIST_ADJ:
91 return GL_LINES_ADJACENCY_ARB;
93 case WINED3D_PT_LINESTRIP_ADJ:
94 return GL_LINE_STRIP_ADJACENCY_ARB;
96 case WINED3D_PT_TRIANGLELIST_ADJ:
97 return GL_TRIANGLES_ADJACENCY_ARB;
99 case WINED3D_PT_TRIANGLESTRIP_ADJ:
100 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
102 default:
103 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
104 return GL_NONE;
108 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
110 switch(primitive_type)
112 case GL_POINTS:
113 return WINED3D_PT_POINTLIST;
115 case GL_LINES:
116 return WINED3D_PT_LINELIST;
118 case GL_LINE_STRIP:
119 return WINED3D_PT_LINESTRIP;
121 case GL_TRIANGLES:
122 return WINED3D_PT_TRIANGLELIST;
124 case GL_TRIANGLE_STRIP:
125 return WINED3D_PT_TRIANGLESTRIP;
127 case GL_TRIANGLE_FAN:
128 return WINED3D_PT_TRIANGLEFAN;
130 case GL_LINES_ADJACENCY_ARB:
131 return WINED3D_PT_LINELIST_ADJ;
133 case GL_LINE_STRIP_ADJACENCY_ARB:
134 return WINED3D_PT_LINESTRIP_ADJ;
136 case GL_TRIANGLES_ADJACENCY_ARB:
137 return WINED3D_PT_TRIANGLELIST_ADJ;
139 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
140 return WINED3D_PT_TRIANGLESTRIP_ADJ;
142 default:
143 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
144 return WINED3D_PT_UNDEFINED;
148 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
150 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
151 *regnum = WINED3D_FFP_POSITION;
152 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
153 *regnum = WINED3D_FFP_BLENDWEIGHT;
154 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
155 *regnum = WINED3D_FFP_BLENDINDICES;
156 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
157 *regnum = WINED3D_FFP_NORMAL;
158 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
159 *regnum = WINED3D_FFP_PSIZE;
160 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
161 *regnum = WINED3D_FFP_DIFFUSE;
162 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
163 *regnum = WINED3D_FFP_SPECULAR;
164 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
165 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
166 else
168 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n", debug_d3ddeclusage(usage), usage_idx);
169 *regnum = ~0U;
170 return FALSE;
173 return TRUE;
176 /* Context activation is done by the caller. */
177 void device_stream_info_from_declaration(struct wined3d_device *device, struct wined3d_stream_info *stream_info)
179 const struct wined3d_state *state = &device->stateBlock->state;
180 /* We need to deal with frequency data! */
181 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
182 BOOL use_vshader;
183 unsigned int i;
185 stream_info->use_map = 0;
186 stream_info->swizzle_map = 0;
188 /* Check for transformed vertices, disable vertex shader if present. */
189 stream_info->position_transformed = declaration->position_transformed;
190 use_vshader = state->vertex_shader && !declaration->position_transformed;
192 /* Translate the declaration into strided data. */
193 for (i = 0; i < declaration->element_count; ++i)
195 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
196 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
197 struct wined3d_buffer *buffer = stream->buffer;
198 struct wined3d_bo_address data;
199 BOOL stride_used;
200 unsigned int idx;
201 DWORD stride;
203 TRACE("%p Element %p (%u of %u)\n", declaration->elements,
204 element, i + 1, declaration->element_count);
206 if (!buffer) continue;
208 data.buffer_object = 0;
209 data.addr = NULL;
211 stride = stream->stride;
212 if (state->user_stream)
214 TRACE("Stream %u is UP, %p\n", element->input_slot, buffer);
215 data.buffer_object = 0;
216 data.addr = (BYTE *)buffer;
218 else
220 TRACE("Stream %u isn't UP, %p\n", element->input_slot, buffer);
221 buffer_get_memory(buffer, &device->adapter->gl_info, &data);
223 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
224 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
225 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
226 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
227 * not, drawStridedSlow is needed, including a vertex buffer path. */
228 if (state->load_base_vertex_index < 0)
230 WARN("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
231 state->load_base_vertex_index);
232 data.buffer_object = 0;
233 data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info);
234 if ((UINT_PTR)data.addr < -state->load_base_vertex_index * stride)
236 FIXME("System memory vertex data load offset is negative!\n");
240 data.addr += element->offset;
242 TRACE("offset %u input_slot %u usage_idx %d\n", element->offset, element->input_slot, element->usage_idx);
244 if (use_vshader)
246 if (element->output_slot == ~0U)
248 /* TODO: Assuming vertexdeclarations are usually used with the
249 * same or a similar shader, it might be worth it to store the
250 * last used output slot and try that one first. */
251 stride_used = vshader_get_input(state->vertex_shader,
252 element->usage, element->usage_idx, &idx);
254 else
256 idx = element->output_slot;
257 stride_used = TRUE;
260 else
262 if (!element->ffp_valid)
264 WARN("Skipping unsupported fixed function element of format %s and usage %s\n",
265 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
266 stride_used = FALSE;
268 else
270 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
274 if (stride_used)
276 TRACE("Load %s array %u [usage %s, usage_idx %u, "
277 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u]\n",
278 use_vshader ? "shader": "fixed function", idx,
279 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
280 element->offset, stride, debug_d3dformat(element->format->id), data.buffer_object);
282 data.addr += stream->offset;
284 stream_info->elements[idx].format = element->format;
285 stream_info->elements[idx].data = data;
286 stream_info->elements[idx].stride = stride;
287 stream_info->elements[idx].stream_idx = element->input_slot;
289 if (!device->adapter->gl_info.supported[ARB_VERTEX_ARRAY_BGRA]
290 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
292 stream_info->swizzle_map |= 1 << idx;
294 stream_info->use_map |= 1 << idx;
298 device->num_buffer_queries = 0;
299 if (!state->user_stream)
301 WORD map = stream_info->use_map;
302 stream_info->all_vbo = 1;
304 /* PreLoad all the vertex buffers. */
305 for (i = 0; map; map >>= 1, ++i)
307 struct wined3d_stream_info_element *element;
308 struct wined3d_buffer *buffer;
310 if (!(map & 1)) continue;
312 element = &stream_info->elements[i];
313 buffer = state->streams[element->stream_idx].buffer;
314 wined3d_buffer_preload(buffer);
316 /* If the preload dropped the buffer object, update the stream info. */
317 if (buffer->buffer_object != element->data.buffer_object)
319 element->data.buffer_object = 0;
320 element->data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info)
321 + (ptrdiff_t)element->data.addr;
324 if (!buffer->buffer_object)
325 stream_info->all_vbo = 0;
327 if (buffer->query)
328 device->buffer_queries[device->num_buffer_queries++] = buffer->query;
331 else
333 stream_info->all_vbo = 0;
337 static void stream_info_element_from_strided(const struct wined3d_gl_info *gl_info,
338 const struct wined3d_strided_element *strided, struct wined3d_stream_info_element *e)
340 e->data.addr = strided->data;
341 e->data.buffer_object = 0;
342 e->format = wined3d_get_format(gl_info, strided->format);
343 e->stride = strided->stride;
344 e->stream_idx = 0;
347 static void device_stream_info_from_strided(const struct wined3d_gl_info *gl_info,
348 const struct wined3d_strided_data *strided, struct wined3d_stream_info *stream_info)
350 unsigned int i;
352 memset(stream_info, 0, sizeof(*stream_info));
354 if (strided->position.data)
355 stream_info_element_from_strided(gl_info, &strided->position, &stream_info->elements[WINED3D_FFP_POSITION]);
356 if (strided->normal.data)
357 stream_info_element_from_strided(gl_info, &strided->normal, &stream_info->elements[WINED3D_FFP_NORMAL]);
358 if (strided->diffuse.data)
359 stream_info_element_from_strided(gl_info, &strided->diffuse, &stream_info->elements[WINED3D_FFP_DIFFUSE]);
360 if (strided->specular.data)
361 stream_info_element_from_strided(gl_info, &strided->specular, &stream_info->elements[WINED3D_FFP_SPECULAR]);
363 for (i = 0; i < WINED3DDP_MAXTEXCOORD; ++i)
365 if (strided->tex_coords[i].data)
366 stream_info_element_from_strided(gl_info, &strided->tex_coords[i],
367 &stream_info->elements[WINED3D_FFP_TEXCOORD0 + i]);
370 stream_info->position_transformed = strided->position_transformed;
372 for (i = 0; i < sizeof(stream_info->elements) / sizeof(*stream_info->elements); ++i)
374 if (!stream_info->elements[i].format) continue;
376 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
377 && stream_info->elements[i].format->id == WINED3DFMT_B8G8R8A8_UNORM)
379 stream_info->swizzle_map |= 1 << i;
381 stream_info->use_map |= 1 << i;
385 static void device_trace_strided_stream_info(const struct wined3d_stream_info *stream_info)
387 TRACE("Strided Data:\n");
388 TRACE_STRIDED(stream_info, WINED3D_FFP_POSITION);
389 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDWEIGHT);
390 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDINDICES);
391 TRACE_STRIDED(stream_info, WINED3D_FFP_NORMAL);
392 TRACE_STRIDED(stream_info, WINED3D_FFP_PSIZE);
393 TRACE_STRIDED(stream_info, WINED3D_FFP_DIFFUSE);
394 TRACE_STRIDED(stream_info, WINED3D_FFP_SPECULAR);
395 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD0);
396 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD1);
397 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD2);
398 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD3);
399 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD4);
400 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD5);
401 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD6);
402 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD7);
405 /* Context activation is done by the caller. */
406 void device_update_stream_info(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
408 struct wined3d_stream_info *stream_info = &device->strided_streams;
409 const struct wined3d_state *state = &device->stateBlock->state;
410 DWORD prev_all_vbo = stream_info->all_vbo;
412 if (device->up_strided)
414 /* Note: this is a ddraw fixed-function code path. */
415 TRACE("=============================== Strided Input ================================\n");
416 device_stream_info_from_strided(gl_info, device->up_strided, stream_info);
417 if (TRACE_ON(d3d)) device_trace_strided_stream_info(stream_info);
419 else
421 TRACE("============================= Vertex Declaration =============================\n");
422 device_stream_info_from_declaration(device, stream_info);
425 if (state->vertex_shader && !stream_info->position_transformed)
427 if (state->vertex_declaration->half_float_conv_needed && !stream_info->all_vbo)
429 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
430 device->useDrawStridedSlow = TRUE;
432 else
434 device->useDrawStridedSlow = FALSE;
437 else
439 WORD slow_mask = (1 << WINED3D_FFP_PSIZE);
440 slow_mask |= -!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
441 & ((1 << WINED3D_FFP_DIFFUSE) | (1 << WINED3D_FFP_SPECULAR));
443 if ((stream_info->position_transformed || (stream_info->use_map & slow_mask)) && !stream_info->all_vbo)
445 device->useDrawStridedSlow = TRUE;
447 else
449 device->useDrawStridedSlow = FALSE;
453 if (prev_all_vbo != stream_info->all_vbo)
454 device_invalidate_state(device, STATE_INDEXBUFFER);
457 static void device_preload_texture(const struct wined3d_state *state, unsigned int idx)
459 struct wined3d_texture *texture;
460 enum WINED3DSRGB srgb;
462 if (!(texture = state->textures[idx])) return;
463 srgb = state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE] ? SRGB_SRGB : SRGB_RGB;
464 texture->texture_ops->texture_preload(texture, srgb);
467 void device_preload_textures(const struct wined3d_device *device)
469 const struct wined3d_state *state = &device->stateBlock->state;
470 unsigned int i;
472 if (use_vs(state))
474 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
476 if (state->vertex_shader->reg_maps.sampler_type[i])
477 device_preload_texture(state, MAX_FRAGMENT_SAMPLERS + i);
481 if (use_ps(state))
483 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
485 if (state->pixel_shader->reg_maps.sampler_type[i])
486 device_preload_texture(state, i);
489 else
491 WORD ffu_map = device->fixed_function_usage_map;
493 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
495 if (ffu_map & 1)
496 device_preload_texture(state, i);
501 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
503 struct wined3d_context **new_array;
505 TRACE("Adding context %p.\n", context);
507 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
508 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
509 sizeof(*new_array) * (device->context_count + 1));
511 if (!new_array)
513 ERR("Failed to grow the context array.\n");
514 return FALSE;
517 new_array[device->context_count++] = context;
518 device->contexts = new_array;
519 return TRUE;
522 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
524 struct wined3d_context **new_array;
525 BOOL found = FALSE;
526 UINT i;
528 TRACE("Removing context %p.\n", context);
530 for (i = 0; i < device->context_count; ++i)
532 if (device->contexts[i] == context)
534 found = TRUE;
535 break;
539 if (!found)
541 ERR("Context %p doesn't exist in context array.\n", context);
542 return;
545 if (!--device->context_count)
547 HeapFree(GetProcessHeap(), 0, device->contexts);
548 device->contexts = NULL;
549 return;
552 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
553 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
554 if (!new_array)
556 ERR("Failed to shrink context array. Oh well.\n");
557 return;
560 device->contexts = new_array;
563 /* Do not call while under the GL lock. */
564 void device_switch_onscreen_ds(struct wined3d_device *device,
565 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
567 if (device->onscreen_depth_stencil)
569 surface_load_ds_location(device->onscreen_depth_stencil, context, SFLAG_INTEXTURE);
571 surface_modify_ds_location(device->onscreen_depth_stencil, SFLAG_INTEXTURE,
572 device->onscreen_depth_stencil->ds_current_size.cx,
573 device->onscreen_depth_stencil->ds_current_size.cy);
574 wined3d_surface_decref(device->onscreen_depth_stencil);
576 device->onscreen_depth_stencil = depth_stencil;
577 wined3d_surface_incref(device->onscreen_depth_stencil);
580 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
582 /* partial draw rect */
583 if (draw_rect->left || draw_rect->top
584 || draw_rect->right < target->resource.width
585 || draw_rect->bottom < target->resource.height)
586 return FALSE;
588 /* partial clear rect */
589 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
590 || clear_rect->right < target->resource.width
591 || clear_rect->bottom < target->resource.height))
592 return FALSE;
594 return TRUE;
597 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
598 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
600 RECT current_rect, r;
602 if (ds->flags & location)
603 SetRect(&current_rect, 0, 0,
604 ds->ds_current_size.cx,
605 ds->ds_current_size.cy);
606 else
607 SetRectEmpty(&current_rect);
609 IntersectRect(&r, draw_rect, &current_rect);
610 if (EqualRect(&r, draw_rect))
612 /* current_rect ⊇ draw_rect, modify only. */
613 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
614 return;
617 if (EqualRect(&r, &current_rect))
619 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
621 if (!clear_rect)
623 /* Full clear, modify only. */
624 *out_rect = *draw_rect;
625 return;
628 IntersectRect(&r, draw_rect, clear_rect);
629 if (EqualRect(&r, draw_rect))
631 /* clear_rect ⊇ draw_rect, modify only. */
632 *out_rect = *draw_rect;
633 return;
637 /* Full load. */
638 surface_load_ds_location(ds, context, location);
639 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
642 /* Do not call while under the GL lock. */
643 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
644 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
645 float depth, DWORD stencil)
647 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
648 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
649 UINT drawable_width, drawable_height;
650 struct wined3d_context *context;
651 GLbitfield clear_mask = 0;
652 BOOL render_offscreen;
653 unsigned int i;
654 RECT ds_rect;
656 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
657 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
658 * for the cleared parts, and the untouched parts.
660 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
661 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
662 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
663 * checking all this if the dest surface is in the drawable anyway. */
664 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
666 for (i = 0; i < rt_count; ++i)
668 struct wined3d_surface *rt = fb->render_targets[i];
669 if (rt)
670 surface_load_location(rt, rt->draw_binding, NULL);
674 context = context_acquire(device, target);
675 if (!context->valid)
677 context_release(context);
678 WARN("Invalid context, skipping clear.\n");
679 return;
682 if (target)
684 render_offscreen = context->render_offscreen;
685 target->get_drawable_size(context, &drawable_width, &drawable_height);
687 else
689 render_offscreen = TRUE;
690 drawable_width = fb->depth_stencil->pow2Width;
691 drawable_height = fb->depth_stencil->pow2Height;
694 if (flags & WINED3DCLEAR_ZBUFFER)
696 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
698 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
699 device_switch_onscreen_ds(device, context, fb->depth_stencil);
700 prepare_ds_clear(fb->depth_stencil, context, location,
701 draw_rect, rect_count, clear_rect, &ds_rect);
704 if (!context_apply_clear_state(context, device, rt_count, fb))
706 context_release(context);
707 WARN("Failed to apply clear state, skipping clear.\n");
708 return;
711 ENTER_GL();
713 /* Only set the values up once, as they are not changing. */
714 if (flags & WINED3DCLEAR_STENCIL)
716 if (context->gl_info->supported[EXT_STENCIL_TWO_SIDE])
718 glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
719 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
721 glStencilMask(~0U);
722 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
723 glClearStencil(stencil);
724 checkGLcall("glClearStencil");
725 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
728 if (flags & WINED3DCLEAR_ZBUFFER)
730 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
732 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
734 glDepthMask(GL_TRUE);
735 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
736 glClearDepth(depth);
737 checkGLcall("glClearDepth");
738 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
741 if (flags & WINED3DCLEAR_TARGET)
743 for (i = 0; i < rt_count; ++i)
745 struct wined3d_surface *rt = fb->render_targets[i];
747 if (rt)
748 surface_modify_location(rt, rt->draw_binding, TRUE);
751 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
752 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
753 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
754 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
755 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
756 glClearColor(color->r, color->g, color->b, color->a);
757 checkGLcall("glClearColor");
758 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
761 if (!clear_rect)
763 if (render_offscreen)
765 glScissor(draw_rect->left, draw_rect->top,
766 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
768 else
770 glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
771 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
773 checkGLcall("glScissor");
774 glClear(clear_mask);
775 checkGLcall("glClear");
777 else
779 RECT current_rect;
781 /* Now process each rect in turn. */
782 for (i = 0; i < rect_count; ++i)
784 /* Note that GL uses lower left, width/height. */
785 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
787 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
788 wine_dbgstr_rect(&clear_rect[i]),
789 wine_dbgstr_rect(&current_rect));
791 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
792 * The rectangle is not cleared, no error is returned, but further rectangles are
793 * still cleared if they are valid. */
794 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
796 TRACE("Rectangle with negative dimensions, ignoring.\n");
797 continue;
800 if (render_offscreen)
802 glScissor(current_rect.left, current_rect.top,
803 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
805 else
807 glScissor(current_rect.left, drawable_height - current_rect.bottom,
808 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
810 checkGLcall("glScissor");
812 glClear(clear_mask);
813 checkGLcall("glClear");
817 LEAVE_GL();
819 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
820 && target->container.type == WINED3D_CONTAINER_SWAPCHAIN
821 && target->container.u.swapchain->front_buffer == target))
822 wglFlush(); /* Flush to ensure ordering across contexts. */
824 context_release(context);
827 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
829 ULONG refcount = InterlockedIncrement(&device->ref);
831 TRACE("%p increasing refcount to %u.\n", device, refcount);
833 return refcount;
836 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
838 ULONG refcount = InterlockedDecrement(&device->ref);
840 TRACE("%p decreasing refcount to %u.\n", device, refcount);
842 if (!refcount)
844 struct wined3d_stateblock *stateblock;
845 UINT i;
847 if (wined3d_stateblock_decref(device->updateStateBlock)
848 && device->updateStateBlock != device->stateBlock)
849 FIXME("Something's still holding the update stateblock.\n");
850 device->updateStateBlock = NULL;
852 stateblock = device->stateBlock;
853 device->stateBlock = NULL;
854 if (wined3d_stateblock_decref(stateblock))
855 FIXME("Something's still holding the stateblock.\n");
857 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
859 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
860 device->multistate_funcs[i] = NULL;
863 if (!list_empty(&device->resources))
865 struct wined3d_resource *resource;
867 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
869 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
871 FIXME("Leftover resource %p with type %s (%#x).\n",
872 resource, debug_d3dresourcetype(resource->type), resource->type);
876 if (device->contexts)
877 ERR("Context array not freed!\n");
878 if (device->hardwareCursor)
879 DestroyCursor(device->hardwareCursor);
880 device->hardwareCursor = 0;
882 wined3d_decref(device->wined3d);
883 device->wined3d = NULL;
884 HeapFree(GetProcessHeap(), 0, device);
885 TRACE("Freed device %p.\n", device);
888 return refcount;
891 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
893 TRACE("device %p.\n", device);
895 return device->swapchain_count;
898 HRESULT CDECL wined3d_device_get_swapchain(const struct wined3d_device *device,
899 UINT swapchain_idx, struct wined3d_swapchain **swapchain)
901 TRACE("device %p, swapchain_idx %u, swapchain %p.\n",
902 device, swapchain_idx, swapchain);
904 if (swapchain_idx >= device->swapchain_count)
906 WARN("swapchain_idx %u >= swapchain_count %u.\n",
907 swapchain_idx, device->swapchain_count);
908 *swapchain = NULL;
910 return WINED3DERR_INVALIDCALL;
913 *swapchain = device->swapchains[swapchain_idx];
914 wined3d_swapchain_incref(*swapchain);
915 TRACE("Returning %p.\n", *swapchain);
917 return WINED3D_OK;
920 static void device_load_logo(struct wined3d_device *device, const char *filename)
922 struct wined3d_color_key color_key;
923 HBITMAP hbm;
924 BITMAP bm;
925 HRESULT hr;
926 HDC dcb = NULL, dcs = NULL;
928 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
929 if(hbm)
931 GetObjectA(hbm, sizeof(BITMAP), &bm);
932 dcb = CreateCompatibleDC(NULL);
933 if(!dcb) goto out;
934 SelectObject(dcb, hbm);
936 else
938 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
939 * couldn't be loaded
941 memset(&bm, 0, sizeof(bm));
942 bm.bmWidth = 32;
943 bm.bmHeight = 32;
946 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0, 0,
947 WINED3D_POOL_DEFAULT, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_TYPE_OPENGL, WINED3D_SURFACE_MAPPABLE,
948 NULL, &wined3d_null_parent_ops, &device->logo_surface);
949 if (FAILED(hr))
951 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
952 goto out;
955 if (dcb)
957 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
958 goto out;
959 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
960 wined3d_surface_releasedc(device->logo_surface, dcs);
962 color_key.color_space_low_value = 0;
963 color_key.color_space_high_value = 0;
964 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
966 else
968 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
969 /* Fill the surface with a white color to show that wined3d is there */
970 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
973 out:
974 if (dcb) DeleteDC(dcb);
975 if (hbm) DeleteObject(hbm);
978 /* Context activation is done by the caller. */
979 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
981 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
982 unsigned int i, j, count;
983 /* Under DirectX you can sample even if no texture is bound, whereas
984 * OpenGL will only allow that when a valid texture is bound.
985 * We emulate this by creating dummy textures and binding them
986 * to each texture stage when the currently set D3D texture is NULL. */
987 ENTER_GL();
989 if (gl_info->supported[APPLE_CLIENT_STORAGE])
991 /* The dummy texture does not have client storage backing */
992 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
993 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
996 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
997 for (i = 0; i < count; ++i)
999 DWORD color = 0x000000ff;
1001 /* Make appropriate texture active */
1002 context_active_texture(context, gl_info, i);
1004 glGenTextures(1, &device->dummy_texture_2d[i]);
1005 checkGLcall("glGenTextures");
1006 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
1008 glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
1009 checkGLcall("glBindTexture");
1011 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1012 checkGLcall("glTexImage2D");
1014 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1016 glGenTextures(1, &device->dummy_texture_rect[i]);
1017 checkGLcall("glGenTextures");
1018 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
1020 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1021 checkGLcall("glBindTexture");
1023 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1024 checkGLcall("glTexImage2D");
1027 if (gl_info->supported[EXT_TEXTURE3D])
1029 glGenTextures(1, &device->dummy_texture_3d[i]);
1030 checkGLcall("glGenTextures");
1031 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
1033 glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1034 checkGLcall("glBindTexture");
1036 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
1037 checkGLcall("glTexImage3D");
1040 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1042 glGenTextures(1, &device->dummy_texture_cube[i]);
1043 checkGLcall("glGenTextures");
1044 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
1046 glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1047 checkGLcall("glBindTexture");
1049 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
1051 glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1052 checkGLcall("glTexImage2D");
1057 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1059 /* Re-enable because if supported it is enabled by default */
1060 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1061 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1064 LEAVE_GL();
1067 /* Context activation is done by the caller. */
1068 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
1070 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1072 ENTER_GL();
1073 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1075 glDeleteTextures(count, device->dummy_texture_cube);
1076 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
1079 if (gl_info->supported[EXT_TEXTURE3D])
1081 glDeleteTextures(count, device->dummy_texture_3d);
1082 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
1085 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1087 glDeleteTextures(count, device->dummy_texture_rect);
1088 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
1091 glDeleteTextures(count, device->dummy_texture_2d);
1092 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
1093 LEAVE_GL();
1095 memset(device->dummy_texture_cube, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_cube));
1096 memset(device->dummy_texture_3d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_3d));
1097 memset(device->dummy_texture_rect, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_rect));
1098 memset(device->dummy_texture_2d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_2d));
1101 static LONG fullscreen_style(LONG style)
1103 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1104 style |= WS_POPUP | WS_SYSMENU;
1105 style &= ~(WS_CAPTION | WS_THICKFRAME);
1107 return style;
1110 static LONG fullscreen_exstyle(LONG exstyle)
1112 /* Filter out window decorations. */
1113 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1115 return exstyle;
1118 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1120 BOOL filter_messages;
1121 LONG style, exstyle;
1123 TRACE("Setting up window %p for fullscreen mode.\n", window);
1125 if (device->style || device->exStyle)
1127 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1128 window, device->style, device->exStyle);
1131 device->style = GetWindowLongW(window, GWL_STYLE);
1132 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1134 style = fullscreen_style(device->style);
1135 exstyle = fullscreen_exstyle(device->exStyle);
1137 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1138 device->style, device->exStyle, style, exstyle);
1140 filter_messages = device->filter_messages;
1141 device->filter_messages = TRUE;
1143 SetWindowLongW(window, GWL_STYLE, style);
1144 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1145 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1147 device->filter_messages = filter_messages;
1150 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1152 BOOL filter_messages;
1153 LONG style, exstyle;
1155 if (!device->style && !device->exStyle) return;
1157 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1158 window, device->style, device->exStyle);
1160 style = GetWindowLongW(window, GWL_STYLE);
1161 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1163 filter_messages = device->filter_messages;
1164 device->filter_messages = TRUE;
1166 /* Only restore the style if the application didn't modify it during the
1167 * fullscreen phase. Some applications change it before calling Reset()
1168 * when switching between windowed and fullscreen modes (HL2), some
1169 * depend on the original style (Eve Online). */
1170 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1172 SetWindowLongW(window, GWL_STYLE, device->style);
1173 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1175 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1177 device->filter_messages = filter_messages;
1179 /* Delete the old values. */
1180 device->style = 0;
1181 device->exStyle = 0;
1184 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1186 TRACE("device %p, window %p.\n", device, window);
1188 if (!wined3d_register_window(window, device))
1190 ERR("Failed to register window %p.\n", window);
1191 return E_FAIL;
1194 InterlockedExchangePointer((void **)&device->focus_window, window);
1195 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1197 return WINED3D_OK;
1200 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1202 TRACE("device %p.\n", device);
1204 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1205 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1208 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1209 struct wined3d_swapchain_desc *swapchain_desc)
1211 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1212 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1213 struct wined3d_swapchain *swapchain = NULL;
1214 struct wined3d_context *context;
1215 HRESULT hr;
1216 DWORD state;
1217 unsigned int i;
1219 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1221 if (device->d3d_initialized)
1222 return WINED3DERR_INVALIDCALL;
1223 if (!device->adapter->opengl)
1224 return WINED3DERR_INVALIDCALL;
1226 device->valid_rt_mask = 0;
1227 for (i = 0; i < gl_info->limits.buffers; ++i)
1228 device->valid_rt_mask |= (1 << i);
1229 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1230 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1232 /* Initialize the texture unit mapping to a 1:1 mapping */
1233 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1235 if (state < gl_info->limits.fragment_samplers)
1237 device->texUnitMap[state] = state;
1238 device->rev_tex_unit_map[state] = state;
1240 else
1242 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1243 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1247 /* Setup the implicit swapchain. This also initializes a context. */
1248 TRACE("Creating implicit swapchain\n");
1249 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1250 swapchain_desc, &swapchain);
1251 if (FAILED(hr))
1253 WARN("Failed to create implicit swapchain\n");
1254 goto err_out;
1257 device->swapchain_count = 1;
1258 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1259 if (!device->swapchains)
1261 ERR("Out of memory!\n");
1262 goto err_out;
1264 device->swapchains[0] = swapchain;
1266 if (swapchain->back_buffers && swapchain->back_buffers[0])
1268 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1269 device->fb.render_targets[0] = swapchain->back_buffers[0];
1271 else
1273 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1274 device->fb.render_targets[0] = swapchain->front_buffer;
1276 wined3d_surface_incref(device->fb.render_targets[0]);
1278 /* Depth Stencil support */
1279 device->fb.depth_stencil = device->auto_depth_stencil;
1280 if (device->fb.depth_stencil)
1281 wined3d_surface_incref(device->fb.depth_stencil);
1283 hr = device->shader_backend->shader_alloc_private(device);
1284 if (FAILED(hr))
1286 TRACE("Shader private data couldn't be allocated\n");
1287 goto err_out;
1289 hr = device->frag_pipe->alloc_private(device);
1290 if (FAILED(hr))
1292 TRACE("Fragment pipeline private data couldn't be allocated\n");
1293 goto err_out;
1295 hr = device->blitter->alloc_private(device);
1296 if (FAILED(hr))
1298 TRACE("Blitter private data couldn't be allocated\n");
1299 goto err_out;
1302 /* Set up some starting GL setup */
1304 /* Setup all the devices defaults */
1305 stateblock_init_default_state(device->stateBlock);
1307 context = context_acquire(device, swapchain->front_buffer);
1309 create_dummy_textures(device, context);
1311 ENTER_GL();
1313 /* Initialize the current view state */
1314 device->view_ident = 1;
1315 device->contexts[0]->last_was_rhw = 0;
1317 switch (wined3d_settings.offscreen_rendering_mode)
1319 case ORM_FBO:
1320 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1321 break;
1323 case ORM_BACKBUFFER:
1325 if (context_get_current()->aux_buffers > 0)
1327 TRACE("Using auxiliary buffer for offscreen rendering\n");
1328 device->offscreenBuffer = GL_AUX0;
1330 else
1332 TRACE("Using back buffer for offscreen rendering\n");
1333 device->offscreenBuffer = GL_BACK;
1338 TRACE("All defaults now set up, leaving 3D init.\n");
1339 LEAVE_GL();
1341 context_release(context);
1343 /* Clear the screen */
1344 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1345 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1346 &black, 1.0f, 0);
1348 device->d3d_initialized = TRUE;
1350 if (wined3d_settings.logo)
1351 device_load_logo(device, wined3d_settings.logo);
1352 return WINED3D_OK;
1354 err_out:
1355 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1356 HeapFree(GetProcessHeap(), 0, device->swapchains);
1357 device->swapchain_count = 0;
1358 if (swapchain)
1359 wined3d_swapchain_decref(swapchain);
1360 if (device->blit_priv)
1361 device->blitter->free_private(device);
1362 if (device->fragment_priv)
1363 device->frag_pipe->free_private(device);
1364 if (device->shader_priv)
1365 device->shader_backend->shader_free_private(device);
1367 return hr;
1370 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1371 struct wined3d_swapchain_desc *swapchain_desc)
1373 struct wined3d_swapchain *swapchain = NULL;
1374 HRESULT hr;
1376 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1378 /* Setup the implicit swapchain */
1379 TRACE("Creating implicit swapchain\n");
1380 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1381 swapchain_desc, &swapchain);
1382 if (FAILED(hr))
1384 WARN("Failed to create implicit swapchain\n");
1385 goto err_out;
1388 device->swapchain_count = 1;
1389 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1390 if (!device->swapchains)
1392 ERR("Out of memory!\n");
1393 goto err_out;
1395 device->swapchains[0] = swapchain;
1396 return WINED3D_OK;
1398 err_out:
1399 wined3d_swapchain_decref(swapchain);
1400 return hr;
1403 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1405 struct wined3d_resource *resource, *cursor;
1406 const struct wined3d_gl_info *gl_info;
1407 struct wined3d_context *context;
1408 struct wined3d_surface *surface;
1409 UINT i;
1411 TRACE("device %p.\n", device);
1413 if (!device->d3d_initialized)
1414 return WINED3DERR_INVALIDCALL;
1416 /* Force making the context current again, to verify it is still valid
1417 * (workaround for broken drivers) */
1418 context_set_current(NULL);
1419 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1420 * it was created. Thus make sure a context is active for the glDelete* calls
1422 context = context_acquire(device, NULL);
1423 gl_info = context->gl_info;
1425 if (device->logo_surface)
1426 wined3d_surface_decref(device->logo_surface);
1428 stateblock_unbind_resources(device->stateBlock);
1430 /* Unload resources */
1431 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1433 TRACE("Unloading resource %p.\n", resource);
1435 resource->resource_ops->resource_unload(resource);
1438 TRACE("Deleting high order patches\n");
1439 for (i = 0; i < PATCHMAP_SIZE; ++i)
1441 struct wined3d_rect_patch *patch;
1442 struct list *e1, *e2;
1444 LIST_FOR_EACH_SAFE(e1, e2, &device->patches[i])
1446 patch = LIST_ENTRY(e1, struct wined3d_rect_patch, entry);
1447 wined3d_device_delete_patch(device, patch->Handle);
1451 /* Delete the mouse cursor texture */
1452 if (device->cursorTexture)
1454 ENTER_GL();
1455 glDeleteTextures(1, &device->cursorTexture);
1456 LEAVE_GL();
1457 device->cursorTexture = 0;
1460 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1461 * private data, it might contain opengl pointers
1463 if (device->depth_blt_texture)
1465 ENTER_GL();
1466 glDeleteTextures(1, &device->depth_blt_texture);
1467 LEAVE_GL();
1468 device->depth_blt_texture = 0;
1471 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1472 device->blitter->free_private(device);
1473 device->frag_pipe->free_private(device);
1474 device->shader_backend->shader_free_private(device);
1476 /* Release the buffers (with sanity checks)*/
1477 if (device->onscreen_depth_stencil)
1479 surface = device->onscreen_depth_stencil;
1480 device->onscreen_depth_stencil = NULL;
1481 wined3d_surface_decref(surface);
1484 if (device->fb.depth_stencil)
1486 surface = device->fb.depth_stencil;
1488 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1490 device->fb.depth_stencil = NULL;
1491 wined3d_surface_decref(surface);
1494 if (device->auto_depth_stencil)
1496 surface = device->auto_depth_stencil;
1497 device->auto_depth_stencil = NULL;
1498 if (wined3d_surface_decref(surface))
1499 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1502 for (i = 1; i < gl_info->limits.buffers; ++i)
1504 wined3d_device_set_render_target(device, i, NULL, FALSE);
1507 surface = device->fb.render_targets[0];
1508 TRACE("Setting rendertarget 0 to NULL\n");
1509 device->fb.render_targets[0] = NULL;
1510 TRACE("Releasing the render target at %p\n", surface);
1511 wined3d_surface_decref(surface);
1513 context_release(context);
1515 for (i = 0; i < device->swapchain_count; ++i)
1517 TRACE("Releasing the implicit swapchain %u.\n", i);
1518 if (wined3d_swapchain_decref(device->swapchains[i]))
1519 FIXME("Something's still holding the implicit swapchain.\n");
1522 HeapFree(GetProcessHeap(), 0, device->swapchains);
1523 device->swapchains = NULL;
1524 device->swapchain_count = 0;
1526 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1527 device->fb.render_targets = NULL;
1529 device->d3d_initialized = FALSE;
1531 return WINED3D_OK;
1534 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1536 unsigned int i;
1538 for (i = 0; i < device->swapchain_count; ++i)
1540 TRACE("Releasing the implicit swapchain %u.\n", i);
1541 if (wined3d_swapchain_decref(device->swapchains[i]))
1542 FIXME("Something's still holding the implicit swapchain.\n");
1545 HeapFree(GetProcessHeap(), 0, device->swapchains);
1546 device->swapchains = NULL;
1547 device->swapchain_count = 0;
1548 return WINED3D_OK;
1551 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1552 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1553 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1555 * There is no way to deactivate thread safety once it is enabled.
1557 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1559 TRACE("device %p.\n", device);
1561 /* For now just store the flag (needed in case of ddraw). */
1562 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1565 HRESULT CDECL wined3d_device_get_wined3d(const struct wined3d_device *device, struct wined3d **wined3d)
1567 TRACE("device %p, wined3d %p.\n", device, wined3d);
1569 *wined3d = device->wined3d;
1570 wined3d_incref(*wined3d);
1572 TRACE("Returning %p.\n", *wined3d);
1574 return WINED3D_OK;
1577 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1579 TRACE("device %p.\n", device);
1581 TRACE("Emulating %d MB, returning %d MB left.\n",
1582 device->adapter->TextureRam / (1024 * 1024),
1583 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1585 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1588 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1589 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1591 struct wined3d_stream_state *stream;
1592 struct wined3d_buffer *prev_buffer;
1594 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1595 device, stream_idx, buffer, offset, stride);
1597 if (stream_idx >= MAX_STREAMS)
1599 WARN("Stream index %u out of range.\n", stream_idx);
1600 return WINED3DERR_INVALIDCALL;
1602 else if (offset & 0x3)
1604 WARN("Offset %u is not 4 byte aligned.\n", offset);
1605 return WINED3DERR_INVALIDCALL;
1608 stream = &device->updateStateBlock->state.streams[stream_idx];
1609 prev_buffer = stream->buffer;
1611 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1613 if (prev_buffer == buffer
1614 && stream->stride == stride
1615 && stream->offset == offset)
1617 TRACE("Application is setting the old values over, nothing to do.\n");
1618 return WINED3D_OK;
1621 stream->buffer = buffer;
1622 if (buffer)
1624 stream->stride = stride;
1625 stream->offset = offset;
1628 /* Handle recording of state blocks. */
1629 if (device->isRecordingState)
1631 TRACE("Recording... not performing anything.\n");
1632 if (buffer)
1633 wined3d_buffer_incref(buffer);
1634 if (prev_buffer)
1635 wined3d_buffer_decref(prev_buffer);
1636 return WINED3D_OK;
1639 if (buffer)
1641 InterlockedIncrement(&buffer->resource.bind_count);
1642 wined3d_buffer_incref(buffer);
1644 if (prev_buffer)
1646 InterlockedDecrement(&prev_buffer->resource.bind_count);
1647 wined3d_buffer_decref(prev_buffer);
1650 device_invalidate_state(device, STATE_STREAMSRC);
1652 return WINED3D_OK;
1655 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1656 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1658 struct wined3d_stream_state *stream;
1660 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1661 device, stream_idx, buffer, offset, stride);
1663 if (stream_idx >= MAX_STREAMS)
1665 WARN("Stream index %u out of range.\n", stream_idx);
1666 return WINED3DERR_INVALIDCALL;
1669 stream = &device->stateBlock->state.streams[stream_idx];
1670 *buffer = stream->buffer;
1671 if (*buffer)
1672 wined3d_buffer_incref(*buffer);
1673 if (offset)
1674 *offset = stream->offset;
1675 *stride = stream->stride;
1677 return WINED3D_OK;
1680 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1682 struct wined3d_stream_state *stream;
1683 UINT old_flags, old_freq;
1685 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1687 /* Verify input. At least in d3d9 this is invalid. */
1688 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1690 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1691 return WINED3DERR_INVALIDCALL;
1693 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1695 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1696 return WINED3DERR_INVALIDCALL;
1698 if (!divider)
1700 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1701 return WINED3DERR_INVALIDCALL;
1704 stream = &device->updateStateBlock->state.streams[stream_idx];
1705 old_flags = stream->flags;
1706 old_freq = stream->frequency;
1708 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1709 stream->frequency = divider & 0x7fffff;
1711 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1713 if (stream->frequency != old_freq || stream->flags != old_flags)
1714 device_invalidate_state(device, STATE_STREAMSRC);
1716 return WINED3D_OK;
1719 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1720 UINT stream_idx, UINT *divider)
1722 struct wined3d_stream_state *stream;
1724 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1726 stream = &device->updateStateBlock->state.streams[stream_idx];
1727 *divider = stream->flags | stream->frequency;
1729 TRACE("Returning %#x.\n", *divider);
1731 return WINED3D_OK;
1734 HRESULT CDECL wined3d_device_set_transform(struct wined3d_device *device,
1735 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1737 TRACE("device %p, state %s, matrix %p.\n",
1738 device, debug_d3dtstype(d3dts), matrix);
1739 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1740 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1741 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1742 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1744 /* Handle recording of state blocks. */
1745 if (device->isRecordingState)
1747 TRACE("Recording... not performing anything.\n");
1748 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1749 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1750 return WINED3D_OK;
1753 /* If the new matrix is the same as the current one,
1754 * we cut off any further processing. this seems to be a reasonable
1755 * optimization because as was noticed, some apps (warcraft3 for example)
1756 * tend towards setting the same matrix repeatedly for some reason.
1758 * From here on we assume that the new matrix is different, wherever it matters. */
1759 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1761 TRACE("The application is setting the same matrix over again.\n");
1762 return WINED3D_OK;
1765 device->stateBlock->state.transforms[d3dts] = *matrix;
1766 if (d3dts == WINED3D_TS_VIEW)
1767 device->view_ident = !memcmp(matrix, &identity, sizeof(identity));
1769 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1770 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1772 return WINED3D_OK;
1776 HRESULT CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1777 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1779 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1781 *matrix = device->stateBlock->state.transforms[state];
1783 return WINED3D_OK;
1786 HRESULT CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1787 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1789 const struct wined3d_matrix *mat = NULL;
1790 struct wined3d_matrix temp;
1792 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1794 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1795 * below means it will be recorded in a state block change, but it
1796 * works regardless where it is recorded.
1797 * If this is found to be wrong, change to StateBlock. */
1798 if (state > HIGHEST_TRANSFORMSTATE)
1800 WARN("Unhandled transform state %#x.\n", state);
1801 return WINED3D_OK;
1804 mat = &device->updateStateBlock->state.transforms[state];
1805 multiply_matrix(&temp, mat, matrix);
1807 /* Apply change via set transform - will reapply to eg. lights this way. */
1808 return wined3d_device_set_transform(device, state, &temp);
1811 /* Note lights are real special cases. Although the device caps state only
1812 * e.g. 8 are supported, you can reference any indexes you want as long as
1813 * that number max are enabled at any one point in time. Therefore since the
1814 * indices can be anything, we need a hashmap of them. However, this causes
1815 * stateblock problems. When capturing the state block, I duplicate the
1816 * hashmap, but when recording, just build a chain pretty much of commands to
1817 * be replayed. */
1818 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1819 UINT light_idx, const struct wined3d_light *light)
1821 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1822 struct wined3d_light_info *object = NULL;
1823 struct list *e;
1824 float rho;
1826 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1828 /* Check the parameter range. Need for speed most wanted sets junk lights
1829 * which confuse the GL driver. */
1830 if (!light)
1831 return WINED3DERR_INVALIDCALL;
1833 switch (light->type)
1835 case WINED3D_LIGHT_POINT:
1836 case WINED3D_LIGHT_SPOT:
1837 case WINED3D_LIGHT_PARALLELPOINT:
1838 case WINED3D_LIGHT_GLSPOT:
1839 /* Incorrect attenuation values can cause the gl driver to crash.
1840 * Happens with Need for speed most wanted. */
1841 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1843 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1844 return WINED3DERR_INVALIDCALL;
1846 break;
1848 case WINED3D_LIGHT_DIRECTIONAL:
1849 /* Ignores attenuation */
1850 break;
1852 default:
1853 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1854 return WINED3DERR_INVALIDCALL;
1857 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1859 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1860 if (object->OriginalIndex == light_idx)
1861 break;
1862 object = NULL;
1865 if (!object)
1867 TRACE("Adding new light\n");
1868 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1869 if (!object)
1871 ERR("Out of memory error when allocating a light\n");
1872 return E_OUTOFMEMORY;
1874 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1875 object->glIndex = -1;
1876 object->OriginalIndex = light_idx;
1879 /* Initialize the object. */
1880 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1881 light_idx, light->type,
1882 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1883 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1884 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1885 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1886 light->direction.x, light->direction.y, light->direction.z);
1887 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1888 light->range, light->falloff, light->theta, light->phi);
1890 /* Save away the information. */
1891 object->OriginalParms = *light;
1893 switch (light->type)
1895 case WINED3D_LIGHT_POINT:
1896 /* Position */
1897 object->lightPosn[0] = light->position.x;
1898 object->lightPosn[1] = light->position.y;
1899 object->lightPosn[2] = light->position.z;
1900 object->lightPosn[3] = 1.0f;
1901 object->cutoff = 180.0f;
1902 /* FIXME: Range */
1903 break;
1905 case WINED3D_LIGHT_DIRECTIONAL:
1906 /* Direction */
1907 object->lightPosn[0] = -light->direction.x;
1908 object->lightPosn[1] = -light->direction.y;
1909 object->lightPosn[2] = -light->direction.z;
1910 object->lightPosn[3] = 0.0f;
1911 object->exponent = 0.0f;
1912 object->cutoff = 180.0f;
1913 break;
1915 case WINED3D_LIGHT_SPOT:
1916 /* Position */
1917 object->lightPosn[0] = light->position.x;
1918 object->lightPosn[1] = light->position.y;
1919 object->lightPosn[2] = light->position.z;
1920 object->lightPosn[3] = 1.0f;
1922 /* Direction */
1923 object->lightDirn[0] = light->direction.x;
1924 object->lightDirn[1] = light->direction.y;
1925 object->lightDirn[2] = light->direction.z;
1926 object->lightDirn[3] = 1.0f;
1928 /* opengl-ish and d3d-ish spot lights use too different models
1929 * for the light "intensity" as a function of the angle towards
1930 * the main light direction, so we only can approximate very
1931 * roughly. However, spot lights are rather rarely used in games
1932 * (if ever used at all). Furthermore if still used, probably
1933 * nobody pays attention to such details. */
1934 if (!light->falloff)
1936 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1937 * equations have the falloff resp. exponent parameter as an
1938 * exponent, so the spot light lighting will always be 1.0 for
1939 * both of them, and we don't have to care for the rest of the
1940 * rather complex calculation. */
1941 object->exponent = 0.0f;
1943 else
1945 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1946 if (rho < 0.0001f)
1947 rho = 0.0001f;
1948 object->exponent = -0.3f / logf(cosf(rho / 2));
1951 if (object->exponent > 128.0f)
1952 object->exponent = 128.0f;
1954 object->cutoff = (float)(light->phi * 90 / M_PI);
1955 /* FIXME: Range */
1956 break;
1958 default:
1959 FIXME("Unrecognized light type %#x.\n", light->type);
1962 /* Update the live definitions if the light is currently assigned a glIndex. */
1963 if (object->glIndex != -1 && !device->isRecordingState)
1964 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1966 return WINED3D_OK;
1969 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1970 UINT light_idx, struct wined3d_light *light)
1972 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1973 struct wined3d_light_info *light_info = NULL;
1974 struct list *e;
1976 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1978 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1980 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1981 if (light_info->OriginalIndex == light_idx)
1982 break;
1983 light_info = NULL;
1986 if (!light_info)
1988 TRACE("Light information requested but light not defined\n");
1989 return WINED3DERR_INVALIDCALL;
1992 *light = light_info->OriginalParms;
1993 return WINED3D_OK;
1996 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1998 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1999 struct wined3d_light_info *light_info = NULL;
2000 struct list *e;
2002 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
2004 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2006 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2007 if (light_info->OriginalIndex == light_idx)
2008 break;
2009 light_info = NULL;
2011 TRACE("Found light %p.\n", light_info);
2013 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
2014 if (!light_info)
2016 TRACE("Light enabled requested but light not defined, so defining one!\n");
2017 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
2019 /* Search for it again! Should be fairly quick as near head of list. */
2020 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2022 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2023 if (light_info->OriginalIndex == light_idx)
2024 break;
2025 light_info = NULL;
2027 if (!light_info)
2029 FIXME("Adding default lights has failed dismally\n");
2030 return WINED3DERR_INVALIDCALL;
2034 if (!enable)
2036 if (light_info->glIndex != -1)
2038 if (!device->isRecordingState)
2039 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
2041 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
2042 light_info->glIndex = -1;
2044 else
2046 TRACE("Light already disabled, nothing to do\n");
2048 light_info->enabled = FALSE;
2050 else
2052 light_info->enabled = TRUE;
2053 if (light_info->glIndex != -1)
2055 TRACE("Nothing to do as light was enabled\n");
2057 else
2059 unsigned int i;
2060 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2061 /* Find a free GL light. */
2062 for (i = 0; i < gl_info->limits.lights; ++i)
2064 if (!device->updateStateBlock->state.lights[i])
2066 device->updateStateBlock->state.lights[i] = light_info;
2067 light_info->glIndex = i;
2068 break;
2071 if (light_info->glIndex == -1)
2073 /* Our tests show that Windows returns D3D_OK in this situation, even with
2074 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
2075 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
2076 * as well for those lights.
2078 * TODO: Test how this affects rendering. */
2079 WARN("Too many concurrently active lights\n");
2080 return WINED3D_OK;
2083 /* i == light_info->glIndex */
2084 if (!device->isRecordingState)
2085 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
2089 return WINED3D_OK;
2092 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
2094 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2095 struct wined3d_light_info *light_info = NULL;
2096 struct list *e;
2098 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
2100 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2102 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2103 if (light_info->OriginalIndex == light_idx)
2104 break;
2105 light_info = NULL;
2108 if (!light_info)
2110 TRACE("Light enabled state requested but light not defined.\n");
2111 return WINED3DERR_INVALIDCALL;
2113 /* true is 128 according to SetLightEnable */
2114 *enable = light_info->enabled ? 128 : 0;
2115 return WINED3D_OK;
2118 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
2119 UINT plane_idx, const struct wined3d_vec4 *plane)
2121 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2123 /* Validate plane_idx. */
2124 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2126 TRACE("Application has requested clipplane this device doesn't support.\n");
2127 return WINED3DERR_INVALIDCALL;
2130 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2132 if (!memcmp(&device->updateStateBlock->state.clip_planes[plane_idx], plane, sizeof(*plane)))
2134 TRACE("Application is setting old values over, nothing to do.\n");
2135 return WINED3D_OK;
2138 device->updateStateBlock->state.clip_planes[plane_idx] = *plane;
2140 /* Handle recording of state blocks. */
2141 if (device->isRecordingState)
2143 TRACE("Recording... not performing anything.\n");
2144 return WINED3D_OK;
2147 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2149 return WINED3D_OK;
2152 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
2153 UINT plane_idx, struct wined3d_vec4 *plane)
2155 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2157 /* Validate plane_idx. */
2158 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2160 TRACE("Application has requested clipplane this device doesn't support.\n");
2161 return WINED3DERR_INVALIDCALL;
2164 *plane = device->stateBlock->state.clip_planes[plane_idx];
2166 return WINED3D_OK;
2169 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2170 const struct wined3d_clip_status *clip_status)
2172 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2174 if (!clip_status)
2175 return WINED3DERR_INVALIDCALL;
2177 return WINED3D_OK;
2180 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2181 struct wined3d_clip_status *clip_status)
2183 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2185 if (!clip_status)
2186 return WINED3DERR_INVALIDCALL;
2188 return WINED3D_OK;
2191 HRESULT CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2193 TRACE("device %p, material %p.\n", device, material);
2195 device->updateStateBlock->changed.material = TRUE;
2196 device->updateStateBlock->state.material = *material;
2198 /* Handle recording of state blocks */
2199 if (device->isRecordingState)
2201 TRACE("Recording... not performing anything.\n");
2202 return WINED3D_OK;
2205 device_invalidate_state(device, STATE_MATERIAL);
2207 return WINED3D_OK;
2210 HRESULT CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2212 TRACE("device %p, material %p.\n", device, material);
2214 *material = device->updateStateBlock->state.material;
2216 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2217 material->diffuse.r, material->diffuse.g,
2218 material->diffuse.b, material->diffuse.a);
2219 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2220 material->ambient.r, material->ambient.g,
2221 material->ambient.b, material->ambient.a);
2222 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2223 material->specular.r, material->specular.g,
2224 material->specular.b, material->specular.a);
2225 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2226 material->emissive.r, material->emissive.g,
2227 material->emissive.b, material->emissive.a);
2228 TRACE("power %.8e.\n", material->power);
2230 return WINED3D_OK;
2233 HRESULT CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2234 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2236 struct wined3d_buffer *prev_buffer;
2238 TRACE("device %p, buffer %p, format %s.\n",
2239 device, buffer, debug_d3dformat(format_id));
2241 prev_buffer = device->updateStateBlock->state.index_buffer;
2243 device->updateStateBlock->changed.indices = TRUE;
2244 device->updateStateBlock->state.index_buffer = buffer;
2245 device->updateStateBlock->state.index_format = format_id;
2247 /* Handle recording of state blocks. */
2248 if (device->isRecordingState)
2250 TRACE("Recording... not performing anything.\n");
2251 if (buffer)
2252 wined3d_buffer_incref(buffer);
2253 if (prev_buffer)
2254 wined3d_buffer_decref(prev_buffer);
2255 return WINED3D_OK;
2258 if (prev_buffer != buffer)
2260 device_invalidate_state(device, STATE_INDEXBUFFER);
2261 if (buffer)
2263 InterlockedIncrement(&buffer->resource.bind_count);
2264 wined3d_buffer_incref(buffer);
2266 if (prev_buffer)
2268 InterlockedDecrement(&prev_buffer->resource.bind_count);
2269 wined3d_buffer_decref(prev_buffer);
2273 return WINED3D_OK;
2276 HRESULT CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device, struct wined3d_buffer **buffer)
2278 TRACE("device %p, buffer %p.\n", device, buffer);
2280 *buffer = device->stateBlock->state.index_buffer;
2282 if (*buffer)
2283 wined3d_buffer_incref(*buffer);
2285 TRACE("Returning %p.\n", *buffer);
2287 return WINED3D_OK;
2290 /* Method to offer d3d9 a simple way to set the base vertex index without messing with the index buffer */
2291 HRESULT CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2293 TRACE("device %p, base_index %d.\n", device, base_index);
2295 if (device->updateStateBlock->state.base_vertex_index == base_index)
2297 TRACE("Application is setting the old value over, nothing to do\n");
2298 return WINED3D_OK;
2301 device->updateStateBlock->state.base_vertex_index = base_index;
2303 if (device->isRecordingState)
2305 TRACE("Recording... not performing anything\n");
2306 return WINED3D_OK;
2308 return WINED3D_OK;
2311 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2313 TRACE("device %p.\n", device);
2315 return device->stateBlock->state.base_vertex_index;
2318 HRESULT CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2320 TRACE("device %p, viewport %p.\n", device, viewport);
2321 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2322 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2324 device->updateStateBlock->changed.viewport = TRUE;
2325 device->updateStateBlock->state.viewport = *viewport;
2327 /* Handle recording of state blocks */
2328 if (device->isRecordingState)
2330 TRACE("Recording... not performing anything\n");
2331 return WINED3D_OK;
2334 device_invalidate_state(device, STATE_VIEWPORT);
2336 return WINED3D_OK;
2339 HRESULT CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2341 TRACE("device %p, viewport %p.\n", device, viewport);
2343 *viewport = device->stateBlock->state.viewport;
2345 return WINED3D_OK;
2348 HRESULT CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2349 enum wined3d_render_state state, DWORD value)
2351 DWORD old_value = device->stateBlock->state.render_states[state];
2353 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2355 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2356 device->updateStateBlock->state.render_states[state] = value;
2358 /* Handle recording of state blocks. */
2359 if (device->isRecordingState)
2361 TRACE("Recording... not performing anything.\n");
2362 return WINED3D_OK;
2365 /* Compared here and not before the assignment to allow proper stateblock recording. */
2366 if (value == old_value)
2367 TRACE("Application is setting the old value over, nothing to do.\n");
2368 else
2369 device_invalidate_state(device, STATE_RENDER(state));
2371 return WINED3D_OK;
2374 HRESULT CDECL wined3d_device_get_render_state(const struct wined3d_device *device,
2375 enum wined3d_render_state state, DWORD *value)
2377 TRACE("device %p, state %s (%#x), value %p.\n", device, debug_d3drenderstate(state), state, value);
2379 *value = device->stateBlock->state.render_states[state];
2381 return WINED3D_OK;
2384 HRESULT CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2385 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2387 DWORD old_value;
2389 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2390 device, sampler_idx, debug_d3dsamplerstate(state), value);
2392 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2393 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2395 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2396 / sizeof(*device->stateBlock->state.sampler_states))
2398 WARN("Invalid sampler %u.\n", sampler_idx);
2399 return WINED3D_OK; /* Windows accepts overflowing this array ... we do not. */
2402 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2403 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2404 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2406 /* Handle recording of state blocks. */
2407 if (device->isRecordingState)
2409 TRACE("Recording... not performing anything.\n");
2410 return WINED3D_OK;
2413 if (old_value == value)
2415 TRACE("Application is setting the old value over, nothing to do.\n");
2416 return WINED3D_OK;
2419 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2421 return WINED3D_OK;
2424 HRESULT CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2425 UINT sampler_idx, enum wined3d_sampler_state state, DWORD *value)
2427 TRACE("device %p, sampler_idx %u, state %s, value %p.\n",
2428 device, sampler_idx, debug_d3dsamplerstate(state), value);
2430 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2431 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2433 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2434 / sizeof(*device->stateBlock->state.sampler_states))
2436 WARN("Invalid sampler %u.\n", sampler_idx);
2437 return WINED3D_OK; /* Windows accepts overflowing this array ... we do not. */
2440 *value = device->stateBlock->state.sampler_states[sampler_idx][state];
2441 TRACE("Returning %#x.\n", *value);
2443 return WINED3D_OK;
2446 HRESULT CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2448 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2450 device->updateStateBlock->changed.scissorRect = TRUE;
2451 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2453 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2454 return WINED3D_OK;
2456 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2458 if (device->isRecordingState)
2460 TRACE("Recording... not performing anything.\n");
2461 return WINED3D_OK;
2464 device_invalidate_state(device, STATE_SCISSORRECT);
2466 return WINED3D_OK;
2469 HRESULT CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2471 TRACE("device %p, rect %p.\n", device, rect);
2473 *rect = device->updateStateBlock->state.scissor_rect;
2474 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2476 return WINED3D_OK;
2479 HRESULT CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2480 struct wined3d_vertex_declaration *declaration)
2482 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2484 TRACE("device %p, declaration %p.\n", device, declaration);
2486 if (declaration)
2487 wined3d_vertex_declaration_incref(declaration);
2488 if (prev)
2489 wined3d_vertex_declaration_decref(prev);
2491 device->updateStateBlock->state.vertex_declaration = declaration;
2492 device->updateStateBlock->changed.vertexDecl = TRUE;
2494 if (device->isRecordingState)
2496 TRACE("Recording... not performing anything.\n");
2497 return WINED3D_OK;
2499 else if (declaration == prev)
2501 /* Checked after the assignment to allow proper stateblock recording. */
2502 TRACE("Application is setting the old declaration over, nothing to do.\n");
2503 return WINED3D_OK;
2506 device_invalidate_state(device, STATE_VDECL);
2507 return WINED3D_OK;
2510 HRESULT CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device,
2511 struct wined3d_vertex_declaration **declaration)
2513 TRACE("device %p, declaration %p.\n", device, declaration);
2515 *declaration = device->stateBlock->state.vertex_declaration;
2516 if (*declaration)
2517 wined3d_vertex_declaration_incref(*declaration);
2519 return WINED3D_OK;
2522 HRESULT CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2524 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2526 TRACE("device %p, shader %p.\n", device, shader);
2528 device->updateStateBlock->state.vertex_shader = shader;
2529 device->updateStateBlock->changed.vertexShader = TRUE;
2531 if (device->isRecordingState)
2533 if (shader)
2534 wined3d_shader_incref(shader);
2535 if (prev)
2536 wined3d_shader_decref(prev);
2537 TRACE("Recording... not performing anything.\n");
2538 return WINED3D_OK;
2541 if (shader == prev)
2543 TRACE("Application is setting the old shader over, nothing to do.\n");
2544 return WINED3D_OK;
2547 if (shader)
2548 wined3d_shader_incref(shader);
2549 if (prev)
2550 wined3d_shader_decref(prev);
2552 device_invalidate_state(device, STATE_VSHADER);
2554 return WINED3D_OK;
2557 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2559 struct wined3d_shader *shader;
2561 TRACE("device %p.\n", device);
2563 shader = device->stateBlock->state.vertex_shader;
2564 if (shader)
2565 wined3d_shader_incref(shader);
2567 TRACE("Returning %p.\n", shader);
2568 return shader;
2571 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2572 UINT start_register, const BOOL *constants, UINT bool_count)
2574 UINT count = min(bool_count, MAX_CONST_B - start_register);
2575 UINT i;
2577 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2578 device, start_register, constants, bool_count);
2580 if (!constants || start_register >= MAX_CONST_B)
2581 return WINED3DERR_INVALIDCALL;
2583 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2584 for (i = 0; i < count; ++i)
2585 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2587 for (i = start_register; i < count + start_register; ++i)
2588 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2590 if (!device->isRecordingState)
2591 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2593 return WINED3D_OK;
2596 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2597 UINT start_register, BOOL *constants, UINT bool_count)
2599 UINT count = min(bool_count, MAX_CONST_B - start_register);
2601 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2602 device, start_register, constants, bool_count);
2604 if (!constants || start_register >= MAX_CONST_B)
2605 return WINED3DERR_INVALIDCALL;
2607 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2609 return WINED3D_OK;
2612 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2613 UINT start_register, const int *constants, UINT vector4i_count)
2615 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2616 UINT i;
2618 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2619 device, start_register, constants, vector4i_count);
2621 if (!constants || start_register >= MAX_CONST_I)
2622 return WINED3DERR_INVALIDCALL;
2624 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2625 for (i = 0; i < count; ++i)
2626 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2627 constants[i * 4], constants[i * 4 + 1],
2628 constants[i * 4 + 2], constants[i * 4 + 3]);
2630 for (i = start_register; i < count + start_register; ++i)
2631 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2633 if (!device->isRecordingState)
2634 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2636 return WINED3D_OK;
2639 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2640 UINT start_register, int *constants, UINT vector4i_count)
2642 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2644 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2645 device, start_register, constants, vector4i_count);
2647 if (!constants || start_register >= MAX_CONST_I)
2648 return WINED3DERR_INVALIDCALL;
2650 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2651 return WINED3D_OK;
2654 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2655 UINT start_register, const float *constants, UINT vector4f_count)
2657 UINT i;
2659 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2660 device, start_register, constants, vector4f_count);
2662 /* Specifically test start_register > limit to catch MAX_UINT overflows
2663 * when adding start_register + vector4f_count. */
2664 if (!constants
2665 || start_register + vector4f_count > device->d3d_vshader_constantF
2666 || start_register > device->d3d_vshader_constantF)
2667 return WINED3DERR_INVALIDCALL;
2669 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2670 constants, vector4f_count * sizeof(float) * 4);
2671 if (TRACE_ON(d3d))
2673 for (i = 0; i < vector4f_count; ++i)
2674 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2675 constants[i * 4], constants[i * 4 + 1],
2676 constants[i * 4 + 2], constants[i * 4 + 3]);
2679 if (!device->isRecordingState)
2681 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2682 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2685 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2686 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2688 return WINED3D_OK;
2691 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2692 UINT start_register, float *constants, UINT vector4f_count)
2694 int count = min(vector4f_count, device->d3d_vshader_constantF - start_register);
2696 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2697 device, start_register, constants, vector4f_count);
2699 if (!constants || count < 0)
2700 return WINED3DERR_INVALIDCALL;
2702 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2704 return WINED3D_OK;
2707 static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2709 DWORD i;
2711 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2713 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2717 static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2719 DWORD i = device->rev_tex_unit_map[unit];
2720 DWORD j = device->texUnitMap[stage];
2722 device->texUnitMap[stage] = unit;
2723 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2724 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2726 device->rev_tex_unit_map[unit] = stage;
2727 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2728 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2731 static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2733 UINT i;
2735 device->fixed_function_usage_map = 0;
2736 for (i = 0; i < MAX_TEXTURES; ++i)
2738 const struct wined3d_state *state = &device->stateBlock->state;
2739 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2740 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2741 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2742 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2743 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2744 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2745 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2746 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2748 /* Not used, and disable higher stages. */
2749 if (color_op == WINED3D_TOP_DISABLE)
2750 break;
2752 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2753 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2754 || ((color_arg3 == WINED3DTA_TEXTURE)
2755 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2756 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2757 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2758 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2759 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2760 device->fixed_function_usage_map |= (1 << i);
2762 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2763 && i < MAX_TEXTURES - 1)
2764 device->fixed_function_usage_map |= (1 << (i + 1));
2768 static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2770 unsigned int i, tex;
2771 WORD ffu_map;
2773 device_update_fixed_function_usage_map(device);
2774 ffu_map = device->fixed_function_usage_map;
2776 if (device->max_ffp_textures == gl_info->limits.texture_stages
2777 || device->stateBlock->state.lowest_disabled_stage <= device->max_ffp_textures)
2779 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2781 if (!(ffu_map & 1)) continue;
2783 if (device->texUnitMap[i] != i)
2785 device_map_stage(device, i, i);
2786 device_invalidate_state(device, STATE_SAMPLER(i));
2787 device_invalidate_texture_stage(device, i);
2790 return;
2793 /* Now work out the mapping */
2794 tex = 0;
2795 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2797 if (!(ffu_map & 1)) continue;
2799 if (device->texUnitMap[i] != tex)
2801 device_map_stage(device, i, tex);
2802 device_invalidate_state(device, STATE_SAMPLER(i));
2803 device_invalidate_texture_stage(device, i);
2806 ++tex;
2810 static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2812 const enum wined3d_sampler_texture_type *sampler_type =
2813 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2814 unsigned int i;
2816 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2818 if (sampler_type[i] && device->texUnitMap[i] != i)
2820 device_map_stage(device, i, i);
2821 device_invalidate_state(device, STATE_SAMPLER(i));
2822 if (i < gl_info->limits.texture_stages)
2823 device_invalidate_texture_stage(device, i);
2828 static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2829 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2830 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2832 DWORD current_mapping = device->rev_tex_unit_map[unit];
2834 /* Not currently used */
2835 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2837 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2838 /* Used by a fragment sampler */
2840 if (!pshader_sampler_tokens) {
2841 /* No pixel shader, check fixed function */
2842 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2845 /* Pixel shader, check the shader's sampler map */
2846 return !pshader_sampler_tokens[current_mapping];
2849 /* Used by a vertex sampler */
2850 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2853 static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2855 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2856 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2857 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2858 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2859 int i;
2861 if (ps)
2863 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2864 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2865 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2868 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2869 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2870 if (vshader_sampler_type[i])
2872 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2874 /* Already mapped somewhere */
2875 continue;
2878 while (start >= 0)
2880 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2882 device_map_stage(device, vsampler_idx, start);
2883 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2885 --start;
2886 break;
2889 --start;
2895 void device_update_tex_unit_map(struct wined3d_device *device)
2897 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2898 const struct wined3d_state *state = &device->stateBlock->state;
2899 BOOL vs = use_vs(state);
2900 BOOL ps = use_ps(state);
2902 * Rules are:
2903 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2904 * that would be really messy and require shader recompilation
2905 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2906 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2908 if (ps)
2909 device_map_psamplers(device, gl_info);
2910 else
2911 device_map_fixed_function_samplers(device, gl_info);
2913 if (vs)
2914 device_map_vsamplers(device, ps, gl_info);
2917 HRESULT CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2919 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2921 TRACE("device %p, shader %p.\n", device, shader);
2923 device->updateStateBlock->state.pixel_shader = shader;
2924 device->updateStateBlock->changed.pixelShader = TRUE;
2926 if (device->isRecordingState)
2928 if (shader)
2929 wined3d_shader_incref(shader);
2930 if (prev)
2931 wined3d_shader_decref(prev);
2932 TRACE("Recording... not performing anything.\n");
2933 return WINED3D_OK;
2936 if (shader == prev)
2938 TRACE("Application is setting the old shader over, nothing to do.\n");
2939 return WINED3D_OK;
2942 if (shader)
2943 wined3d_shader_incref(shader);
2944 if (prev)
2945 wined3d_shader_decref(prev);
2947 device_invalidate_state(device, STATE_PIXELSHADER);
2949 return WINED3D_OK;
2952 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2954 struct wined3d_shader *shader;
2956 TRACE("device %p.\n", device);
2958 shader = device->stateBlock->state.pixel_shader;
2959 if (shader)
2960 wined3d_shader_incref(shader);
2962 TRACE("Returning %p.\n", shader);
2963 return shader;
2966 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2967 UINT start_register, const BOOL *constants, UINT bool_count)
2969 UINT count = min(bool_count, MAX_CONST_B - start_register);
2970 UINT i;
2972 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2973 device, start_register, constants, bool_count);
2975 if (!constants || start_register >= MAX_CONST_B)
2976 return WINED3DERR_INVALIDCALL;
2978 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
2979 for (i = 0; i < count; ++i)
2980 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2982 for (i = start_register; i < count + start_register; ++i)
2983 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
2985 if (!device->isRecordingState)
2986 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
2988 return WINED3D_OK;
2991 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2992 UINT start_register, BOOL *constants, UINT bool_count)
2994 UINT count = min(bool_count, MAX_CONST_B - start_register);
2996 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2997 device, start_register, constants, bool_count);
2999 if (!constants || start_register >= MAX_CONST_B)
3000 return WINED3DERR_INVALIDCALL;
3002 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
3004 return WINED3D_OK;
3007 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
3008 UINT start_register, const int *constants, UINT vector4i_count)
3010 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3011 UINT i;
3013 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3014 device, start_register, constants, vector4i_count);
3016 if (!constants || start_register >= MAX_CONST_I)
3017 return WINED3DERR_INVALIDCALL;
3019 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
3020 for (i = 0; i < count; ++i)
3021 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
3022 constants[i * 4], constants[i * 4 + 1],
3023 constants[i * 4 + 2], constants[i * 4 + 3]);
3025 for (i = start_register; i < count + start_register; ++i)
3026 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
3028 if (!device->isRecordingState)
3029 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3031 return WINED3D_OK;
3034 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
3035 UINT start_register, int *constants, UINT vector4i_count)
3037 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3039 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3040 device, start_register, constants, vector4i_count);
3042 if (!constants || start_register >= MAX_CONST_I)
3043 return WINED3DERR_INVALIDCALL;
3045 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
3047 return WINED3D_OK;
3050 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
3051 UINT start_register, const float *constants, UINT vector4f_count)
3053 UINT i;
3055 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3056 device, start_register, constants, vector4f_count);
3058 /* Specifically test start_register > limit to catch MAX_UINT overflows
3059 * when adding start_register + vector4f_count. */
3060 if (!constants
3061 || start_register + vector4f_count > device->d3d_pshader_constantF
3062 || start_register > device->d3d_pshader_constantF)
3063 return WINED3DERR_INVALIDCALL;
3065 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
3066 constants, vector4f_count * sizeof(float) * 4);
3067 if (TRACE_ON(d3d))
3069 for (i = 0; i < vector4f_count; ++i)
3070 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
3071 constants[i * 4], constants[i * 4 + 1],
3072 constants[i * 4 + 2], constants[i * 4 + 3]);
3075 if (!device->isRecordingState)
3077 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
3078 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3081 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
3082 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
3084 return WINED3D_OK;
3087 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
3088 UINT start_register, float *constants, UINT vector4f_count)
3090 int count = min(vector4f_count, device->d3d_pshader_constantF - start_register);
3092 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3093 device, start_register, constants, vector4f_count);
3095 if (!constants || count < 0)
3096 return WINED3DERR_INVALIDCALL;
3098 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
3100 return WINED3D_OK;
3103 /* Context activation is done by the caller. */
3104 /* Do not call while under the GL lock. */
3105 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3106 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3107 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3108 DWORD DestFVF)
3110 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3111 struct wined3d_viewport vp;
3112 UINT vertex_size;
3113 unsigned int i;
3114 BYTE *dest_ptr;
3115 BOOL doClip;
3116 DWORD numTextures;
3117 HRESULT hr;
3119 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3121 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3124 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3126 ERR("Source has no position mask\n");
3127 return WINED3DERR_INVALIDCALL;
3130 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3132 static BOOL warned = FALSE;
3134 * The clipping code is not quite correct. Some things need
3135 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3136 * so disable clipping for now.
3137 * (The graphics in Half-Life are broken, and my processvertices
3138 * test crashes with IDirect3DDevice3)
3139 doClip = TRUE;
3141 doClip = FALSE;
3142 if(!warned) {
3143 warned = TRUE;
3144 FIXME("Clipping is broken and disabled for now\n");
3147 else
3148 doClip = FALSE;
3150 vertex_size = get_flexible_vertex_size(DestFVF);
3151 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3153 WARN("Failed to map buffer, hr %#x.\n", hr);
3154 return hr;
3157 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3158 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3159 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3161 TRACE("View mat:\n");
3162 TRACE("%f %f %f %f\n", view_mat.u.s._11, view_mat.u.s._12, view_mat.u.s._13, view_mat.u.s._14);
3163 TRACE("%f %f %f %f\n", view_mat.u.s._21, view_mat.u.s._22, view_mat.u.s._23, view_mat.u.s._24);
3164 TRACE("%f %f %f %f\n", view_mat.u.s._31, view_mat.u.s._32, view_mat.u.s._33, view_mat.u.s._34);
3165 TRACE("%f %f %f %f\n", view_mat.u.s._41, view_mat.u.s._42, view_mat.u.s._43, view_mat.u.s._44);
3167 TRACE("Proj mat:\n");
3168 TRACE("%f %f %f %f\n", proj_mat.u.s._11, proj_mat.u.s._12, proj_mat.u.s._13, proj_mat.u.s._14);
3169 TRACE("%f %f %f %f\n", proj_mat.u.s._21, proj_mat.u.s._22, proj_mat.u.s._23, proj_mat.u.s._24);
3170 TRACE("%f %f %f %f\n", proj_mat.u.s._31, proj_mat.u.s._32, proj_mat.u.s._33, proj_mat.u.s._34);
3171 TRACE("%f %f %f %f\n", proj_mat.u.s._41, proj_mat.u.s._42, proj_mat.u.s._43, proj_mat.u.s._44);
3173 TRACE("World mat:\n");
3174 TRACE("%f %f %f %f\n", world_mat.u.s._11, world_mat.u.s._12, world_mat.u.s._13, world_mat.u.s._14);
3175 TRACE("%f %f %f %f\n", world_mat.u.s._21, world_mat.u.s._22, world_mat.u.s._23, world_mat.u.s._24);
3176 TRACE("%f %f %f %f\n", world_mat.u.s._31, world_mat.u.s._32, world_mat.u.s._33, world_mat.u.s._34);
3177 TRACE("%f %f %f %f\n", world_mat.u.s._41, world_mat.u.s._42, world_mat.u.s._43, world_mat.u.s._44);
3179 /* Get the viewport */
3180 wined3d_device_get_viewport(device, &vp);
3181 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3182 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3184 multiply_matrix(&mat,&view_mat,&world_mat);
3185 multiply_matrix(&mat,&proj_mat,&mat);
3187 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3189 for (i = 0; i < dwCount; i+= 1) {
3190 unsigned int tex_index;
3192 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3193 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3194 /* The position first */
3195 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3196 const float *p = (const float *)(element->data.addr + i * element->stride);
3197 float x, y, z, rhw;
3198 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3200 /* Multiplication with world, view and projection matrix */
3201 x = (p[0] * mat.u.s._11) + (p[1] * mat.u.s._21) + (p[2] * mat.u.s._31) + (1.0f * mat.u.s._41);
3202 y = (p[0] * mat.u.s._12) + (p[1] * mat.u.s._22) + (p[2] * mat.u.s._32) + (1.0f * mat.u.s._42);
3203 z = (p[0] * mat.u.s._13) + (p[1] * mat.u.s._23) + (p[2] * mat.u.s._33) + (1.0f * mat.u.s._43);
3204 rhw = (p[0] * mat.u.s._14) + (p[1] * mat.u.s._24) + (p[2] * mat.u.s._34) + (1.0f * mat.u.s._44);
3206 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3208 /* WARNING: The following things are taken from d3d7 and were not yet checked
3209 * against d3d8 or d3d9!
3212 /* Clipping conditions: From msdn
3214 * A vertex is clipped if it does not match the following requirements
3215 * -rhw < x <= rhw
3216 * -rhw < y <= rhw
3217 * 0 < z <= rhw
3218 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3220 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3221 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3225 if( !doClip ||
3226 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3227 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3228 ( rhw > eps ) ) ) {
3230 /* "Normal" viewport transformation (not clipped)
3231 * 1) The values are divided by rhw
3232 * 2) The y axis is negative, so multiply it with -1
3233 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3234 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3235 * 4) Multiply x with Width/2 and add Width/2
3236 * 5) The same for the height
3237 * 6) Add the viewpoint X and Y to the 2D coordinates and
3238 * The minimum Z value to z
3239 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3241 * Well, basically it's simply a linear transformation into viewport
3242 * coordinates
3245 x /= rhw;
3246 y /= rhw;
3247 z /= rhw;
3249 y *= -1;
3251 x *= vp.width / 2;
3252 y *= vp.height / 2;
3253 z *= vp.max_z - vp.min_z;
3255 x += vp.width / 2 + vp.x;
3256 y += vp.height / 2 + vp.y;
3257 z += vp.min_z;
3259 rhw = 1 / rhw;
3260 } else {
3261 /* That vertex got clipped
3262 * Contrary to OpenGL it is not dropped completely, it just
3263 * undergoes a different calculation.
3265 TRACE("Vertex got clipped\n");
3266 x += rhw;
3267 y += rhw;
3269 x /= 2;
3270 y /= 2;
3272 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3273 * outside of the main vertex buffer memory. That needs some more
3274 * investigation...
3278 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3281 ( (float *) dest_ptr)[0] = x;
3282 ( (float *) dest_ptr)[1] = y;
3283 ( (float *) dest_ptr)[2] = z;
3284 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3286 dest_ptr += 3 * sizeof(float);
3288 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3289 dest_ptr += sizeof(float);
3292 if (DestFVF & WINED3DFVF_PSIZE)
3293 dest_ptr += sizeof(DWORD);
3295 if (DestFVF & WINED3DFVF_NORMAL)
3297 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3298 const float *normal = (const float *)(element->data.addr + i * element->stride);
3299 /* AFAIK this should go into the lighting information */
3300 FIXME("Didn't expect the destination to have a normal\n");
3301 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3304 if (DestFVF & WINED3DFVF_DIFFUSE)
3306 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3307 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3308 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3310 static BOOL warned = FALSE;
3312 if(!warned) {
3313 ERR("No diffuse color in source, but destination has one\n");
3314 warned = TRUE;
3317 *( (DWORD *) dest_ptr) = 0xffffffff;
3318 dest_ptr += sizeof(DWORD);
3320 else
3322 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3326 if (DestFVF & WINED3DFVF_SPECULAR)
3328 /* What's the color value in the feedback buffer? */
3329 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3330 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3331 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3333 static BOOL warned = FALSE;
3335 if(!warned) {
3336 ERR("No specular color in source, but destination has one\n");
3337 warned = TRUE;
3340 *( (DWORD *) dest_ptr) = 0xFF000000;
3341 dest_ptr += sizeof(DWORD);
3343 else
3345 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3349 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3351 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3352 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3353 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3355 ERR("No source texture, but destination requests one\n");
3356 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3358 else
3360 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3365 wined3d_buffer_unmap(dest);
3367 return WINED3D_OK;
3369 #undef copy_and_next
3371 /* Do not call while under the GL lock. */
3372 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3373 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3374 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3376 struct wined3d_state *state = &device->stateBlock->state;
3377 struct wined3d_stream_info stream_info;
3378 const struct wined3d_gl_info *gl_info;
3379 BOOL streamWasUP = state->user_stream;
3380 struct wined3d_context *context;
3381 struct wined3d_shader *vs;
3382 unsigned int i;
3383 HRESULT hr;
3385 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3386 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3387 device, src_start_idx, dst_idx, vertex_count,
3388 dst_buffer, declaration, flags, dst_fvf);
3390 if (declaration)
3391 FIXME("Output vertex declaration not implemented yet.\n");
3393 /* Need any context to write to the vbo. */
3394 context = context_acquire(device, NULL);
3395 gl_info = context->gl_info;
3397 /* ProcessVertices reads from vertex buffers, which have to be assigned.
3398 * DrawPrimitive and DrawPrimitiveUP control the streamIsUP flag, thus
3399 * restore it afterwards. */
3400 vs = state->vertex_shader;
3401 state->vertex_shader = NULL;
3402 state->user_stream = FALSE;
3403 device_stream_info_from_declaration(device, &stream_info);
3404 state->user_stream = streamWasUP;
3405 state->vertex_shader = vs;
3407 /* We can't convert FROM a VBO, and vertex buffers used to source into
3408 * process_vertices() are unlikely to ever be used for drawing. Release
3409 * VBOs in those buffers and fix up the stream_info structure.
3411 * Also apply the start index. */
3412 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3414 struct wined3d_stream_info_element *e;
3416 if (!(stream_info.use_map & (1 << i)))
3417 continue;
3419 e = &stream_info.elements[i];
3420 if (e->data.buffer_object)
3422 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3423 e->data.buffer_object = 0;
3424 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3425 ENTER_GL();
3426 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3427 vb->buffer_object = 0;
3428 LEAVE_GL();
3430 if (e->data.addr)
3431 e->data.addr += e->stride * src_start_idx;
3434 hr = process_vertices_strided(device, dst_idx, vertex_count,
3435 &stream_info, dst_buffer, flags, dst_fvf);
3437 context_release(context);
3439 return hr;
3442 HRESULT CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3443 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3445 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3446 DWORD old_value;
3448 TRACE("device %p, stage %u, state %s, value %#x.\n",
3449 device, stage, debug_d3dtexturestate(state), value);
3451 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3453 WARN("Invalid state %#x passed.\n", state);
3454 return WINED3D_OK;
3457 if (stage >= gl_info->limits.texture_stages)
3459 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3460 stage, gl_info->limits.texture_stages - 1);
3461 return WINED3D_OK;
3464 old_value = device->updateStateBlock->state.texture_states[stage][state];
3465 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3466 device->updateStateBlock->state.texture_states[stage][state] = value;
3468 if (device->isRecordingState)
3470 TRACE("Recording... not performing anything.\n");
3471 return WINED3D_OK;
3474 /* Checked after the assignments to allow proper stateblock recording. */
3475 if (old_value == value)
3477 TRACE("Application is setting the old value over, nothing to do.\n");
3478 return WINED3D_OK;
3481 if (stage > device->stateBlock->state.lowest_disabled_stage
3482 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3483 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3485 /* Colorop change above lowest disabled stage? That won't change
3486 * anything in the GL setup. Changes in other states are important on
3487 * disabled stages too. */
3488 return WINED3D_OK;
3491 if (state == WINED3D_TSS_COLOR_OP)
3493 unsigned int i;
3495 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3497 /* Previously enabled stage disabled now. Make sure to dirtify
3498 * all enabled stages above stage, they have to be disabled.
3500 * The current stage is dirtified below. */
3501 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3503 TRACE("Additionally dirtifying stage %u.\n", i);
3504 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3506 device->stateBlock->state.lowest_disabled_stage = stage;
3507 TRACE("New lowest disabled: %u.\n", stage);
3509 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3511 /* Previously disabled stage enabled. Stages above it may need
3512 * enabling. Stage must be lowest_disabled_stage here, if it's
3513 * bigger success is returned above, and stages below the lowest
3514 * disabled stage can't be enabled (because they are enabled
3515 * already).
3517 * Again stage stage doesn't need to be dirtified here, it is
3518 * handled below. */
3519 for (i = stage + 1; i < gl_info->limits.texture_stages; ++i)
3521 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3522 break;
3523 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3524 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3526 device->stateBlock->state.lowest_disabled_stage = i;
3527 TRACE("New lowest disabled: %u.\n", i);
3531 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3533 return WINED3D_OK;
3536 HRESULT CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3537 UINT stage, enum wined3d_texture_stage_state state, DWORD *value)
3539 TRACE("device %p, stage %u, state %s, value %p.\n",
3540 device, stage, debug_d3dtexturestate(state), value);
3542 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3544 WARN("Invalid state %#x passed.\n", state);
3545 return WINED3D_OK;
3548 *value = device->updateStateBlock->state.texture_states[stage][state];
3549 TRACE("Returning %#x.\n", *value);
3551 return WINED3D_OK;
3554 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3555 UINT stage, struct wined3d_texture *texture)
3557 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3558 struct wined3d_texture *prev;
3560 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3562 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3563 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3565 /* Windows accepts overflowing this array... we do not. */
3566 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3568 WARN("Ignoring invalid stage %u.\n", stage);
3569 return WINED3D_OK;
3572 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3574 WARN("Rejecting attempt to set scratch texture.\n");
3575 return WINED3DERR_INVALIDCALL;
3578 device->updateStateBlock->changed.textures |= 1 << stage;
3580 prev = device->updateStateBlock->state.textures[stage];
3581 TRACE("Previous texture %p.\n", prev);
3583 if (texture == prev)
3585 TRACE("App is setting the same texture again, nothing to do.\n");
3586 return WINED3D_OK;
3589 TRACE("Setting new texture to %p.\n", texture);
3590 device->updateStateBlock->state.textures[stage] = texture;
3592 if (device->isRecordingState)
3594 TRACE("Recording... not performing anything\n");
3596 if (texture) wined3d_texture_incref(texture);
3597 if (prev) wined3d_texture_decref(prev);
3599 return WINED3D_OK;
3602 if (texture)
3604 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3606 wined3d_texture_incref(texture);
3608 if (!prev || texture->target != prev->target)
3609 device_invalidate_state(device, STATE_PIXELSHADER);
3611 if (!prev && stage < gl_info->limits.texture_stages)
3613 /* The source arguments for color and alpha ops have different
3614 * meanings when a NULL texture is bound, so the COLOR_OP and
3615 * ALPHA_OP have to be dirtified. */
3616 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3617 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3620 if (bind_count == 1)
3621 texture->sampler = stage;
3624 if (prev)
3626 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3628 wined3d_texture_decref(prev);
3630 if (!texture && stage < gl_info->limits.texture_stages)
3632 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3633 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3636 if (bind_count && prev->sampler == stage)
3638 unsigned int i;
3640 /* Search for other stages the texture is bound to. Shouldn't
3641 * happen if applications bind textures to a single stage only. */
3642 TRACE("Searching for other stages the texture is bound to.\n");
3643 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3645 if (device->updateStateBlock->state.textures[i] == prev)
3647 TRACE("Texture is also bound to stage %u.\n", i);
3648 prev->sampler = i;
3649 break;
3655 device_invalidate_state(device, STATE_SAMPLER(stage));
3657 return WINED3D_OK;
3660 HRESULT CDECL wined3d_device_get_texture(const struct wined3d_device *device,
3661 UINT stage, struct wined3d_texture **texture)
3663 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3665 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3666 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3668 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3670 WARN("Ignoring invalid stage %u.\n", stage);
3671 return WINED3D_OK; /* Windows accepts overflowing this array ... we do not. */
3674 *texture = device->stateBlock->state.textures[stage];
3675 if (*texture)
3676 wined3d_texture_incref(*texture);
3678 TRACE("Returning %p.\n", *texture);
3680 return WINED3D_OK;
3683 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3684 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3686 struct wined3d_swapchain *swapchain;
3687 HRESULT hr;
3689 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3690 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3692 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
3693 if (FAILED(hr))
3695 WARN("Failed to get swapchain %u, hr %#x.\n", swapchain_idx, hr);
3696 return hr;
3699 hr = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type, backbuffer);
3700 wined3d_swapchain_decref(swapchain);
3701 if (FAILED(hr))
3703 WARN("Failed to get backbuffer %u, hr %#x.\n", backbuffer_idx, hr);
3704 return hr;
3707 return WINED3D_OK;
3710 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3712 TRACE("device %p, caps %p.\n", device, caps);
3714 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3715 device->create_parms.device_type, caps);
3718 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device,
3719 UINT swapchain_idx, struct wined3d_display_mode *mode)
3721 struct wined3d_swapchain *swapchain;
3722 HRESULT hr;
3724 TRACE("device %p, swapchain_idx %u, mode %p.\n", device, swapchain_idx, mode);
3726 if (swapchain_idx)
3728 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
3729 if (SUCCEEDED(hr))
3731 hr = wined3d_swapchain_get_display_mode(swapchain, mode);
3732 wined3d_swapchain_decref(swapchain);
3735 else
3737 const struct wined3d_adapter *adapter = device->adapter;
3739 /* Don't read the real display mode, but return the stored mode
3740 * instead. X11 can't change the color depth, and some apps are
3741 * pretty angry if they SetDisplayMode from 24 to 16 bpp and find out
3742 * that GetDisplayMode still returns 24 bpp.
3744 * Also don't relay to the swapchain because with ddraw it's possible
3745 * that there isn't a swapchain at all. */
3746 mode->width = adapter->screen_size.cx;
3747 mode->height = adapter->screen_size.cy;
3748 mode->format_id = adapter->screen_format;
3749 mode->refresh_rate = 0;
3750 hr = WINED3D_OK;
3753 return hr;
3756 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3758 struct wined3d_stateblock *stateblock;
3759 HRESULT hr;
3761 TRACE("device %p.\n", device);
3763 if (device->isRecordingState)
3764 return WINED3DERR_INVALIDCALL;
3766 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3767 if (FAILED(hr))
3768 return hr;
3770 wined3d_stateblock_decref(device->updateStateBlock);
3771 device->updateStateBlock = stateblock;
3772 device->isRecordingState = TRUE;
3774 TRACE("Recording stateblock %p.\n", stateblock);
3776 return WINED3D_OK;
3779 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3780 struct wined3d_stateblock **stateblock)
3782 struct wined3d_stateblock *object = device->updateStateBlock;
3784 TRACE("device %p, stateblock %p.\n", device, stateblock);
3786 if (!device->isRecordingState)
3788 WARN("Not recording.\n");
3789 *stateblock = NULL;
3790 return WINED3DERR_INVALIDCALL;
3793 stateblock_init_contained_states(object);
3795 *stateblock = object;
3796 device->isRecordingState = FALSE;
3797 device->updateStateBlock = device->stateBlock;
3798 wined3d_stateblock_incref(device->updateStateBlock);
3800 TRACE("Returning stateblock %p.\n", *stateblock);
3802 return WINED3D_OK;
3805 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3807 /* At the moment we have no need for any functionality at the beginning
3808 * of a scene. */
3809 TRACE("device %p.\n", device);
3811 if (device->inScene)
3813 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3814 return WINED3DERR_INVALIDCALL;
3816 device->inScene = TRUE;
3817 return WINED3D_OK;
3820 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3822 struct wined3d_context *context;
3824 TRACE("device %p.\n", device);
3826 if (!device->inScene)
3828 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3829 return WINED3DERR_INVALIDCALL;
3832 context = context_acquire(device, NULL);
3833 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3834 wglFlush();
3835 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3836 * fails. */
3837 context_release(context);
3839 device->inScene = FALSE;
3840 return WINED3D_OK;
3843 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3844 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
3846 UINT i;
3848 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
3849 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3850 dst_window_override, dirty_region);
3852 for (i = 0; i < device->swapchain_count; ++i)
3854 wined3d_swapchain_present(device->swapchains[i], src_rect,
3855 dst_rect, dst_window_override, dirty_region, 0);
3858 return WINED3D_OK;
3861 /* Do not call while under the GL lock. */
3862 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3863 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3865 RECT draw_rect;
3867 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3868 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3870 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3872 struct wined3d_surface *ds = device->fb.depth_stencil;
3873 if (!ds)
3875 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3876 /* TODO: What about depth stencil buffers without stencil bits? */
3877 return WINED3DERR_INVALIDCALL;
3879 else if (flags & WINED3DCLEAR_TARGET)
3881 if (ds->resource.width < device->fb.render_targets[0]->resource.width
3882 || ds->resource.height < device->fb.render_targets[0]->resource.height)
3884 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3885 return WINED3D_OK;
3890 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
3891 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
3892 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
3894 return WINED3D_OK;
3897 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3898 enum wined3d_primitive_type primitive_type)
3900 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3902 device->updateStateBlock->changed.primitive_type = TRUE;
3903 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3906 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3907 enum wined3d_primitive_type *primitive_type)
3909 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3911 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
3913 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3916 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3918 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3920 if (!device->stateBlock->state.vertex_declaration)
3922 WARN("Called without a valid vertex declaration set.\n");
3923 return WINED3DERR_INVALIDCALL;
3926 /* The index buffer is not needed here, but restore it, otherwise it is hell to keep track of */
3927 if (device->stateBlock->state.user_stream)
3929 device_invalidate_state(device, STATE_INDEXBUFFER);
3930 device->stateBlock->state.user_stream = FALSE;
3933 if (device->stateBlock->state.load_base_vertex_index)
3935 device->stateBlock->state.load_base_vertex_index = 0;
3936 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3939 /* Account for the loading offset due to index buffers. Instead of
3940 * reloading all sources correct it with the startvertex parameter. */
3941 drawPrimitive(device, vertex_count, start_vertex, FALSE, NULL);
3942 return WINED3D_OK;
3945 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3947 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3949 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3951 if (!device->stateBlock->state.index_buffer)
3953 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3954 * without an index buffer set. (The first time at least...)
3955 * D3D8 simply dies, but I doubt it can do much harm to return
3956 * D3DERR_INVALIDCALL there as well. */
3957 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3958 return WINED3DERR_INVALIDCALL;
3961 if (!device->stateBlock->state.vertex_declaration)
3963 WARN("Called without a valid vertex declaration set.\n");
3964 return WINED3DERR_INVALIDCALL;
3967 if (device->stateBlock->state.user_stream)
3969 device_invalidate_state(device, STATE_INDEXBUFFER);
3970 device->stateBlock->state.user_stream = FALSE;
3973 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3974 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
3976 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
3977 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3980 drawPrimitive(device, index_count, start_idx, TRUE, NULL);
3982 return WINED3D_OK;
3985 HRESULT CDECL wined3d_device_draw_primitive_up(struct wined3d_device *device, UINT vertex_count,
3986 const void *stream_data, UINT stream_stride)
3988 struct wined3d_stream_state *stream;
3989 struct wined3d_buffer *vb;
3991 TRACE("device %p, vertex count %u, stream_data %p, stream_stride %u.\n",
3992 device, vertex_count, stream_data, stream_stride);
3994 if (!device->stateBlock->state.vertex_declaration)
3996 WARN("Called without a valid vertex declaration set.\n");
3997 return WINED3DERR_INVALIDCALL;
4000 /* Note in the following, it's not this type, but that's the purpose of streamIsUP */
4001 stream = &device->stateBlock->state.streams[0];
4002 vb = stream->buffer;
4003 stream->buffer = (struct wined3d_buffer *)stream_data;
4004 if (vb)
4005 wined3d_buffer_decref(vb);
4006 stream->offset = 0;
4007 stream->stride = stream_stride;
4008 device->stateBlock->state.user_stream = TRUE;
4009 if (device->stateBlock->state.load_base_vertex_index)
4011 device->stateBlock->state.load_base_vertex_index = 0;
4012 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4015 /* TODO: Only mark dirty if drawing from a different UP address */
4016 device_invalidate_state(device, STATE_STREAMSRC);
4018 drawPrimitive(device, vertex_count, 0, FALSE, NULL);
4020 /* MSDN specifies stream zero settings must be set to NULL */
4021 stream->buffer = NULL;
4022 stream->stride = 0;
4024 /* stream zero settings set to null at end, as per the msdn. No need to
4025 * mark dirty here, the app has to set the new stream sources or use UP
4026 * drawing again. */
4027 return WINED3D_OK;
4030 HRESULT CDECL wined3d_device_draw_indexed_primitive_up(struct wined3d_device *device,
4031 UINT index_count, const void *index_data, enum wined3d_format_id index_data_format_id,
4032 const void *stream_data, UINT stream_stride)
4034 struct wined3d_stream_state *stream;
4035 struct wined3d_buffer *vb, *ib;
4037 TRACE("device %p, index_count %u, index_data %p, index_data_format %s, stream_data %p, stream_stride %u.\n",
4038 device, index_count, index_data, debug_d3dformat(index_data_format_id), stream_data, stream_stride);
4040 if (!device->stateBlock->state.vertex_declaration)
4042 WARN("(%p) : Called without a valid vertex declaration set\n", device);
4043 return WINED3DERR_INVALIDCALL;
4046 stream = &device->stateBlock->state.streams[0];
4047 vb = stream->buffer;
4048 stream->buffer = (struct wined3d_buffer *)stream_data;
4049 if (vb)
4050 wined3d_buffer_decref(vb);
4051 stream->offset = 0;
4052 stream->stride = stream_stride;
4053 device->stateBlock->state.user_stream = TRUE;
4054 device->stateBlock->state.index_format = index_data_format_id;
4056 /* Set to 0 as per msdn. Do it now due to the stream source loading during drawPrimitive */
4057 device->stateBlock->state.base_vertex_index = 0;
4058 if (device->stateBlock->state.load_base_vertex_index)
4060 device->stateBlock->state.load_base_vertex_index = 0;
4061 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4063 /* Invalidate the state until we have nicer tracking of the stream source pointers */
4064 device_invalidate_state(device, STATE_STREAMSRC);
4065 device_invalidate_state(device, STATE_INDEXBUFFER);
4067 drawPrimitive(device, index_count, 0, TRUE, index_data);
4069 /* MSDN specifies stream zero settings and index buffer must be set to NULL */
4070 stream->buffer = NULL;
4071 stream->stride = 0;
4072 ib = device->stateBlock->state.index_buffer;
4073 if (ib)
4075 wined3d_buffer_decref(ib);
4076 device->stateBlock->state.index_buffer = NULL;
4078 /* No need to mark the stream source state dirty here. Either the app calls UP drawing again, or it has to call
4079 * SetStreamSource to specify a vertex buffer
4082 return WINED3D_OK;
4085 HRESULT CDECL wined3d_device_draw_primitive_strided(struct wined3d_device *device,
4086 UINT vertex_count, const struct wined3d_strided_data *strided_data)
4088 /* Mark the state dirty until we have nicer tracking. It's fine to change
4089 * baseVertexIndex because that call is only called by ddraw which does
4090 * not need that value. */
4091 device_invalidate_state(device, STATE_VDECL);
4092 device_invalidate_state(device, STATE_STREAMSRC);
4093 device_invalidate_state(device, STATE_INDEXBUFFER);
4095 device->stateBlock->state.base_vertex_index = 0;
4096 device->up_strided = strided_data;
4097 drawPrimitive(device, vertex_count, 0, FALSE, NULL);
4098 device->up_strided = NULL;
4100 /* Invalidate the states again to make sure the values from the stateblock
4101 * are properly applied in the next regular draw. Note that the application-
4102 * provided strided data has ovwritten pretty much the entire vertex and
4103 * and index stream related states */
4104 device_invalidate_state(device, STATE_VDECL);
4105 device_invalidate_state(device, STATE_STREAMSRC);
4106 device_invalidate_state(device, STATE_INDEXBUFFER);
4107 return WINED3D_OK;
4110 HRESULT CDECL wined3d_device_draw_indexed_primitive_strided(struct wined3d_device *device,
4111 UINT index_count, const struct wined3d_strided_data *strided_data,
4112 UINT vertex_count, const void *index_data, enum wined3d_format_id index_data_format_id)
4114 enum wined3d_format_id prev_idx_format;
4116 /* Mark the state dirty until we have nicer tracking
4117 * its fine to change baseVertexIndex because that call is only called by ddraw which does not need
4118 * that value.
4120 device_invalidate_state(device, STATE_VDECL);
4121 device_invalidate_state(device, STATE_STREAMSRC);
4122 device_invalidate_state(device, STATE_INDEXBUFFER);
4124 prev_idx_format = device->stateBlock->state.index_format;
4125 device->stateBlock->state.index_format = index_data_format_id;
4126 device->stateBlock->state.user_stream = TRUE;
4127 device->stateBlock->state.base_vertex_index = 0;
4128 device->up_strided = strided_data;
4129 drawPrimitive(device, index_count, 0, TRUE, index_data);
4130 device->up_strided = NULL;
4131 device->stateBlock->state.index_format = prev_idx_format;
4133 device_invalidate_state(device, STATE_VDECL);
4134 device_invalidate_state(device, STATE_STREAMSRC);
4135 device_invalidate_state(device, STATE_INDEXBUFFER);
4136 return WINED3D_OK;
4139 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4140 static HRESULT device_update_volume(struct wined3d_device *device,
4141 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4143 struct wined3d_map_desc src;
4144 struct wined3d_map_desc dst;
4145 HRESULT hr;
4147 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4148 device, src_volume, dst_volume);
4150 /* TODO: Implement direct loading into the gl volume instead of using
4151 * memcpy and dirtification to improve loading performance. */
4152 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
4153 return hr;
4154 if (FAILED(hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3D_MAP_DISCARD)))
4156 wined3d_volume_unmap(src_volume);
4157 return hr;
4160 memcpy(dst.data, src.data, dst_volume->resource.size);
4162 hr = wined3d_volume_unmap(dst_volume);
4163 if (FAILED(hr))
4164 wined3d_volume_unmap(src_volume);
4165 else
4166 hr = wined3d_volume_unmap(src_volume);
4168 return hr;
4171 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4172 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4174 enum wined3d_resource_type type;
4175 unsigned int level_count, i;
4176 HRESULT hr;
4178 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4180 /* Verify that the source and destination textures are non-NULL. */
4181 if (!src_texture || !dst_texture)
4183 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4184 return WINED3DERR_INVALIDCALL;
4187 if (src_texture == dst_texture)
4189 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4190 return WINED3DERR_INVALIDCALL;
4193 /* Verify that the source and destination textures are the same type. */
4194 type = src_texture->resource.type;
4195 if (dst_texture->resource.type != type)
4197 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4198 return WINED3DERR_INVALIDCALL;
4201 /* Check that both textures have the identical numbers of levels. */
4202 level_count = wined3d_texture_get_level_count(src_texture);
4203 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4205 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4206 return WINED3DERR_INVALIDCALL;
4209 /* Make sure that the destination texture is loaded. */
4210 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4212 /* Update every surface level of the texture. */
4213 switch (type)
4215 case WINED3D_RTYPE_TEXTURE:
4217 struct wined3d_surface *src_surface;
4218 struct wined3d_surface *dst_surface;
4220 for (i = 0; i < level_count; ++i)
4222 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4223 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4224 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4225 if (FAILED(hr))
4227 WARN("Failed to update surface, hr %#x.\n", hr);
4228 return hr;
4231 break;
4234 case WINED3D_RTYPE_CUBE_TEXTURE:
4236 struct wined3d_surface *src_surface;
4237 struct wined3d_surface *dst_surface;
4239 for (i = 0; i < level_count * 6; ++i)
4241 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4242 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4243 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4244 if (FAILED(hr))
4246 WARN("Failed to update surface, hr %#x.\n", hr);
4247 return hr;
4250 break;
4253 case WINED3D_RTYPE_VOLUME_TEXTURE:
4255 for (i = 0; i < level_count; ++i)
4257 hr = device_update_volume(device,
4258 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4259 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4260 if (FAILED(hr))
4262 WARN("Failed to update volume, hr %#x.\n", hr);
4263 return hr;
4266 break;
4269 default:
4270 FIXME("Unsupported texture type %#x.\n", type);
4271 return WINED3DERR_INVALIDCALL;
4274 return WINED3D_OK;
4277 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4278 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4280 struct wined3d_swapchain *swapchain;
4281 HRESULT hr;
4283 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4285 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
4286 if (FAILED(hr)) return hr;
4288 hr = wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4289 wined3d_swapchain_decref(swapchain);
4291 return hr;
4294 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4296 const struct wined3d_state *state = &device->stateBlock->state;
4297 struct wined3d_texture *texture;
4298 DWORD i;
4300 TRACE("device %p, num_passes %p.\n", device, num_passes);
4302 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4304 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4306 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4307 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4309 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4311 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4312 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4315 texture = state->textures[i];
4316 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4318 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4320 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4321 return E_FAIL;
4323 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4325 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4326 return E_FAIL;
4328 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4329 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4331 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4332 return E_FAIL;
4336 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4337 || state->render_states[WINED3D_RS_STENCILENABLE])
4339 struct wined3d_surface *ds = device->fb.depth_stencil;
4340 struct wined3d_surface *target = device->fb.render_targets[0];
4342 if(ds && target
4343 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4345 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4346 return WINED3DERR_CONFLICTINGRENDERSTATE;
4350 /* return a sensible default */
4351 *num_passes = 1;
4353 TRACE("returning D3D_OK\n");
4354 return WINED3D_OK;
4357 HRESULT CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4359 static BOOL warned;
4361 TRACE("device %p, software %#x.\n", device, software);
4363 if (!warned)
4365 FIXME("device %p, software %#x stub!\n", device, software);
4366 warned = TRUE;
4369 device->softwareVertexProcessing = software;
4371 return WINED3D_OK;
4374 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4376 static BOOL warned;
4378 TRACE("device %p.\n", device);
4380 if (!warned)
4382 TRACE("device %p stub!\n", device);
4383 warned = TRUE;
4386 return device->softwareVertexProcessing;
4389 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4390 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4392 struct wined3d_swapchain *swapchain;
4393 HRESULT hr;
4395 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4396 device, swapchain_idx, raster_status);
4398 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
4399 if (FAILED(hr))
4401 WARN("Failed to get swapchain %u, hr %#x.\n", swapchain_idx, hr);
4402 return hr;
4405 hr = wined3d_swapchain_get_raster_status(swapchain, raster_status);
4406 wined3d_swapchain_decref(swapchain);
4407 if (FAILED(hr))
4409 WARN("Failed to get raster status, hr %#x.\n", hr);
4410 return hr;
4413 return WINED3D_OK;
4416 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4418 static BOOL warned;
4420 TRACE("device %p, segments %.8e.\n", device, segments);
4422 if (segments != 0.0f)
4424 if (!warned)
4426 FIXME("device %p, segments %.8e stub!\n", device, segments);
4427 warned = TRUE;
4431 return WINED3D_OK;
4434 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4436 static BOOL warned;
4438 TRACE("device %p.\n", device);
4440 if (!warned)
4442 FIXME("device %p stub!\n", device);
4443 warned = TRUE;
4446 return 0.0f;
4449 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4450 struct wined3d_surface *src_surface, const RECT *src_rect,
4451 struct wined3d_surface *dst_surface, const POINT *dst_point)
4453 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4454 device, src_surface, wine_dbgstr_rect(src_rect),
4455 dst_surface, wine_dbgstr_point(dst_point));
4457 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4459 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4460 src_surface, dst_surface);
4461 return WINED3DERR_INVALIDCALL;
4464 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4467 HRESULT CDECL wined3d_device_draw_rect_patch(struct wined3d_device *device, UINT handle,
4468 const float *num_segs, const struct wined3d_rect_patch_info *rect_patch_info)
4470 struct wined3d_rect_patch *patch;
4471 GLenum old_primitive_type;
4472 unsigned int i;
4473 struct list *e;
4474 BOOL found;
4476 TRACE("device %p, handle %#x, num_segs %p, rect_patch_info %p.\n",
4477 device, handle, num_segs, rect_patch_info);
4479 if (!(handle || rect_patch_info))
4481 /* TODO: Write a test for the return value, thus the FIXME */
4482 FIXME("Both handle and rect_patch_info are NULL.\n");
4483 return WINED3DERR_INVALIDCALL;
4486 if (handle)
4488 i = PATCHMAP_HASHFUNC(handle);
4489 found = FALSE;
4490 LIST_FOR_EACH(e, &device->patches[i])
4492 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4493 if (patch->Handle == handle)
4495 found = TRUE;
4496 break;
4500 if (!found)
4502 TRACE("Patch does not exist. Creating a new one\n");
4503 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4504 patch->Handle = handle;
4505 list_add_head(&device->patches[i], &patch->entry);
4506 } else {
4507 TRACE("Found existing patch %p\n", patch);
4510 else
4512 /* Since opengl does not load tesselated vertex attributes into numbered vertex
4513 * attributes we have to tesselate, read back, and draw. This needs a patch
4514 * management structure instance. Create one.
4516 * A possible improvement is to check if a vertex shader is used, and if not directly
4517 * draw the patch.
4519 FIXME("Drawing an uncached patch. This is slow\n");
4520 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4523 if (num_segs[0] != patch->numSegs[0] || num_segs[1] != patch->numSegs[1]
4524 || num_segs[2] != patch->numSegs[2] || num_segs[3] != patch->numSegs[3]
4525 || (rect_patch_info && memcmp(rect_patch_info, &patch->rect_patch_info, sizeof(*rect_patch_info))))
4527 HRESULT hr;
4528 TRACE("Tesselation density or patch info changed, retesselating\n");
4530 if (rect_patch_info)
4531 patch->rect_patch_info = *rect_patch_info;
4533 patch->numSegs[0] = num_segs[0];
4534 patch->numSegs[1] = num_segs[1];
4535 patch->numSegs[2] = num_segs[2];
4536 patch->numSegs[3] = num_segs[3];
4538 hr = tesselate_rectpatch(device, patch);
4539 if (FAILED(hr))
4541 WARN("Patch tesselation failed.\n");
4543 /* Do not release the handle to store the params of the patch */
4544 if (!handle)
4545 HeapFree(GetProcessHeap(), 0, patch);
4547 return hr;
4551 old_primitive_type = device->stateBlock->state.gl_primitive_type;
4552 device->stateBlock->state.gl_primitive_type = GL_TRIANGLES;
4553 wined3d_device_draw_primitive_strided(device, patch->numSegs[0] * patch->numSegs[1] * 2 * 3, &patch->strided);
4554 device->stateBlock->state.gl_primitive_type = old_primitive_type;
4556 /* Destroy uncached patches */
4557 if (!handle)
4559 HeapFree(GetProcessHeap(), 0, patch->mem);
4560 HeapFree(GetProcessHeap(), 0, patch);
4562 return WINED3D_OK;
4565 HRESULT CDECL wined3d_device_draw_tri_patch(struct wined3d_device *device, UINT handle,
4566 const float *segment_count, const struct wined3d_tri_patch_info *patch_info)
4568 FIXME("device %p, handle %#x, segment_count %p, patch_info %p stub!\n",
4569 device, handle, segment_count, patch_info);
4571 return WINED3D_OK;
4574 HRESULT CDECL wined3d_device_delete_patch(struct wined3d_device *device, UINT handle)
4576 struct wined3d_rect_patch *patch;
4577 struct list *e;
4578 int i;
4580 TRACE("device %p, handle %#x.\n", device, handle);
4582 i = PATCHMAP_HASHFUNC(handle);
4583 LIST_FOR_EACH(e, &device->patches[i])
4585 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4586 if (patch->Handle == handle)
4588 TRACE("Deleting patch %p\n", patch);
4589 list_remove(&patch->entry);
4590 HeapFree(GetProcessHeap(), 0, patch->mem);
4591 HeapFree(GetProcessHeap(), 0, patch);
4592 return WINED3D_OK;
4596 /* TODO: Write a test for the return value */
4597 FIXME("Attempt to destroy nonexistent patch\n");
4598 return WINED3DERR_INVALIDCALL;
4601 /* Do not call while under the GL lock. */
4602 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4603 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4605 RECT r;
4607 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4608 device, surface, wine_dbgstr_rect(rect),
4609 color->r, color->g, color->b, color->a);
4611 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4613 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4614 return WINED3DERR_INVALIDCALL;
4617 if (!rect)
4619 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4620 rect = &r;
4623 return surface_color_fill(surface, rect, color);
4626 /* Do not call while under the GL lock. */
4627 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4628 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4630 struct wined3d_resource *resource;
4631 HRESULT hr;
4632 RECT rect;
4634 resource = rendertarget_view->resource;
4635 if (resource->type != WINED3D_RTYPE_SURFACE)
4637 FIXME("Only supported on surface resources\n");
4638 return;
4641 SetRect(&rect, 0, 0, resource->width, resource->height);
4642 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4643 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4646 HRESULT CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4647 UINT render_target_idx, struct wined3d_surface **render_target)
4649 TRACE("device %p, render_target_idx %u, render_target %p.\n",
4650 device, render_target_idx, render_target);
4652 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4654 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4655 return WINED3DERR_INVALIDCALL;
4658 *render_target = device->fb.render_targets[render_target_idx];
4659 TRACE("Returning render target %p.\n", *render_target);
4661 if (!*render_target)
4662 return WINED3DERR_NOTFOUND;
4664 wined3d_surface_incref(*render_target);
4666 return WINED3D_OK;
4669 HRESULT CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device,
4670 struct wined3d_surface **depth_stencil)
4672 TRACE("device %p, depth_stencil %p.\n", device, depth_stencil);
4674 *depth_stencil = device->fb.depth_stencil;
4675 TRACE("Returning depth/stencil surface %p.\n", *depth_stencil);
4677 if (!*depth_stencil)
4678 return WINED3DERR_NOTFOUND;
4680 wined3d_surface_incref(*depth_stencil);
4682 return WINED3D_OK;
4685 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4686 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4688 struct wined3d_surface *prev;
4690 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4691 device, render_target_idx, render_target, set_viewport);
4693 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4695 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4696 return WINED3DERR_INVALIDCALL;
4699 prev = device->fb.render_targets[render_target_idx];
4700 if (render_target == prev)
4702 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4703 return WINED3D_OK;
4706 /* Render target 0 can't be set to NULL. */
4707 if (!render_target && !render_target_idx)
4709 WARN("Trying to set render target 0 to NULL.\n");
4710 return WINED3DERR_INVALIDCALL;
4713 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4715 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4716 return WINED3DERR_INVALIDCALL;
4719 if (render_target)
4720 wined3d_surface_incref(render_target);
4721 device->fb.render_targets[render_target_idx] = render_target;
4722 /* Release after the assignment, to prevent device_resource_released()
4723 * from seeing the surface as still in use. */
4724 if (prev)
4725 wined3d_surface_decref(prev);
4727 /* Render target 0 is special. */
4728 if (!render_target_idx && set_viewport)
4730 /* Set the viewport and scissor rectangles, if requested. Tests show
4731 * that stateblock recording is ignored, the change goes directly
4732 * into the primary stateblock. */
4733 device->stateBlock->state.viewport.height = device->fb.render_targets[0]->resource.height;
4734 device->stateBlock->state.viewport.width = device->fb.render_targets[0]->resource.width;
4735 device->stateBlock->state.viewport.x = 0;
4736 device->stateBlock->state.viewport.y = 0;
4737 device->stateBlock->state.viewport.max_z = 1.0f;
4738 device->stateBlock->state.viewport.min_z = 0.0f;
4739 device_invalidate_state(device, STATE_VIEWPORT);
4741 device->stateBlock->state.scissor_rect.top = 0;
4742 device->stateBlock->state.scissor_rect.left = 0;
4743 device->stateBlock->state.scissor_rect.right = device->stateBlock->state.viewport.width;
4744 device->stateBlock->state.scissor_rect.bottom = device->stateBlock->state.viewport.height;
4745 device_invalidate_state(device, STATE_SCISSORRECT);
4748 device_invalidate_state(device, STATE_FRAMEBUFFER);
4750 return WINED3D_OK;
4753 HRESULT CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4755 struct wined3d_surface *prev = device->fb.depth_stencil;
4757 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4758 device, depth_stencil, prev);
4760 if (prev == depth_stencil)
4762 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4763 return WINED3D_OK;
4766 if (prev)
4768 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4769 || prev->flags & SFLAG_DISCARD)
4771 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4772 prev->resource.width, prev->resource.height);
4773 if (prev == device->onscreen_depth_stencil)
4775 wined3d_surface_decref(device->onscreen_depth_stencil);
4776 device->onscreen_depth_stencil = NULL;
4781 device->fb.depth_stencil = depth_stencil;
4782 if (depth_stencil)
4783 wined3d_surface_incref(depth_stencil);
4785 if (!prev != !depth_stencil)
4787 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4788 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4789 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4790 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4791 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4793 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4795 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4797 if (prev)
4798 wined3d_surface_decref(prev);
4800 device_invalidate_state(device, STATE_FRAMEBUFFER);
4802 return WINED3D_OK;
4805 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4806 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4808 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4809 device, x_hotspot, y_hotspot, cursor_image);
4811 /* some basic validation checks */
4812 if (device->cursorTexture)
4814 struct wined3d_context *context = context_acquire(device, NULL);
4815 ENTER_GL();
4816 glDeleteTextures(1, &device->cursorTexture);
4817 LEAVE_GL();
4818 context_release(context);
4819 device->cursorTexture = 0;
4822 if (cursor_image)
4824 struct wined3d_map_desc map_desc;
4826 /* MSDN: Cursor must be A8R8G8B8 */
4827 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4829 WARN("surface %p has an invalid format.\n", cursor_image);
4830 return WINED3DERR_INVALIDCALL;
4833 /* MSDN: Cursor must be smaller than the display mode */
4834 if (cursor_image->resource.width > device->adapter->screen_size.cx
4835 || cursor_image->resource.height > device->adapter->screen_size.cy)
4837 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4838 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4839 device->adapter->screen_size.cx, device->adapter->screen_size.cy);
4840 return WINED3DERR_INVALIDCALL;
4843 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4845 /* Do not store the surface's pointer because the application may
4846 * release it after setting the cursor image. Windows doesn't
4847 * addref the set surface, so we can't do this either without
4848 * creating circular refcount dependencies. Copy out the gl texture
4849 * instead. */
4850 device->cursorWidth = cursor_image->resource.width;
4851 device->cursorHeight = cursor_image->resource.height;
4852 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4854 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4855 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
4856 struct wined3d_context *context;
4857 char *mem, *bits = map_desc.data;
4858 GLint intfmt = format->glInternal;
4859 GLint gl_format = format->glFormat;
4860 GLint type = format->glType;
4861 INT height = device->cursorHeight;
4862 INT width = device->cursorWidth;
4863 INT bpp = format->byte_count;
4864 INT i;
4866 /* Reformat the texture memory (pitch and width can be
4867 * different) */
4868 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
4869 for (i = 0; i < height; ++i)
4870 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
4871 wined3d_surface_unmap(cursor_image);
4873 context = context_acquire(device, NULL);
4875 ENTER_GL();
4877 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4879 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
4880 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4883 invalidate_active_texture(device, context);
4884 /* Create a new cursor texture */
4885 glGenTextures(1, &device->cursorTexture);
4886 checkGLcall("glGenTextures");
4887 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
4888 /* Copy the bitmap memory into the cursor texture */
4889 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
4890 checkGLcall("glTexImage2D");
4891 HeapFree(GetProcessHeap(), 0, mem);
4893 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4895 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
4896 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4899 LEAVE_GL();
4901 context_release(context);
4903 else
4905 FIXME("A cursor texture was not returned.\n");
4906 device->cursorTexture = 0;
4909 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4911 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4912 ICONINFO cursorInfo;
4913 DWORD *maskBits;
4914 HCURSOR cursor;
4916 /* 32-bit user32 cursors ignore the alpha channel if it's all
4917 * zeroes, and use the mask instead. Fill the mask with all ones
4918 * to ensure we still get a fully transparent cursor. */
4919 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4920 memset(maskBits, 0xff, mask_size);
4921 wined3d_surface_map(cursor_image, &map_desc, NULL,
4922 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4923 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4925 cursorInfo.fIcon = FALSE;
4926 cursorInfo.xHotspot = x_hotspot;
4927 cursorInfo.yHotspot = y_hotspot;
4928 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4929 1, 1, maskBits);
4930 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4931 1, 32, map_desc.data);
4932 wined3d_surface_unmap(cursor_image);
4933 /* Create our cursor and clean up. */
4934 cursor = CreateIconIndirect(&cursorInfo);
4935 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4936 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4937 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4938 device->hardwareCursor = cursor;
4939 if (device->bCursorVisible) SetCursor( cursor );
4940 HeapFree(GetProcessHeap(), 0, maskBits);
4944 device->xHotSpot = x_hotspot;
4945 device->yHotSpot = y_hotspot;
4946 return WINED3D_OK;
4949 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4950 int x_screen_space, int y_screen_space, DWORD flags)
4952 TRACE("device %p, x %d, y %d, flags %#x.\n",
4953 device, x_screen_space, y_screen_space, flags);
4955 device->xScreenSpace = x_screen_space;
4956 device->yScreenSpace = y_screen_space;
4958 if (device->hardwareCursor)
4960 POINT pt;
4962 GetCursorPos( &pt );
4963 if (x_screen_space == pt.x && y_screen_space == pt.y)
4964 return;
4965 SetCursorPos( x_screen_space, y_screen_space );
4967 /* Switch to the software cursor if position diverges from the hardware one. */
4968 GetCursorPos( &pt );
4969 if (x_screen_space != pt.x || y_screen_space != pt.y)
4971 if (device->bCursorVisible) SetCursor( NULL );
4972 DestroyCursor( device->hardwareCursor );
4973 device->hardwareCursor = 0;
4978 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4980 BOOL oldVisible = device->bCursorVisible;
4982 TRACE("device %p, show %#x.\n", device, show);
4985 * When ShowCursor is first called it should make the cursor appear at the OS's last
4986 * known cursor position.
4988 if (show && !oldVisible)
4990 POINT pt;
4991 GetCursorPos(&pt);
4992 device->xScreenSpace = pt.x;
4993 device->yScreenSpace = pt.y;
4996 if (device->hardwareCursor)
4998 device->bCursorVisible = show;
4999 if (show)
5000 SetCursor(device->hardwareCursor);
5001 else
5002 SetCursor(NULL);
5004 else
5006 if (device->cursorTexture)
5007 device->bCursorVisible = show;
5010 return oldVisible;
5013 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5015 struct wined3d_resource *resource, *cursor;
5017 TRACE("device %p.\n", device);
5019 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5021 TRACE("Checking resource %p for eviction.\n", resource);
5023 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
5025 TRACE("Evicting %p.\n", resource);
5026 resource->resource_ops->resource_unload(resource);
5030 /* Invalidate stream sources, the buffer(s) may have been evicted. */
5031 device_invalidate_state(device, STATE_STREAMSRC);
5034 static BOOL is_display_mode_supported(const struct wined3d_device *device,
5035 const struct wined3d_swapchain_desc *swapchain_desc)
5037 struct wined3d_display_mode m;
5038 UINT i, count;
5039 HRESULT hr;
5041 /* All Windowed modes are supported, as is leaving the current mode */
5042 if (swapchain_desc->windowed)
5043 return TRUE;
5044 if (!swapchain_desc->backbuffer_width)
5045 return TRUE;
5046 if (!swapchain_desc->backbuffer_height)
5047 return TRUE;
5049 count = wined3d_get_adapter_mode_count(device->wined3d, device->adapter->ordinal, WINED3DFMT_UNKNOWN);
5050 for (i = 0; i < count; ++i)
5052 memset(&m, 0, sizeof(m));
5053 hr = wined3d_enum_adapter_modes(device->wined3d, device->adapter->ordinal, WINED3DFMT_UNKNOWN, i, &m);
5054 if (FAILED(hr))
5055 ERR("Failed to enumerate adapter mode.\n");
5056 if (m.width == swapchain_desc->backbuffer_width && m.height == swapchain_desc->backbuffer_height)
5057 /* Mode found, it is supported. */
5058 return TRUE;
5060 /* Mode not found -> not supported */
5061 return FALSE;
5064 /* Do not call while under the GL lock. */
5065 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5067 struct wined3d_resource *resource, *cursor;
5068 const struct wined3d_gl_info *gl_info;
5069 struct wined3d_context *context;
5070 struct wined3d_shader *shader;
5072 context = context_acquire(device, NULL);
5073 gl_info = context->gl_info;
5075 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5077 TRACE("Unloading resource %p.\n", resource);
5079 resource->resource_ops->resource_unload(resource);
5082 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
5084 device->shader_backend->shader_destroy(shader);
5087 ENTER_GL();
5088 if (device->depth_blt_texture)
5090 glDeleteTextures(1, &device->depth_blt_texture);
5091 device->depth_blt_texture = 0;
5093 if (device->cursorTexture)
5095 glDeleteTextures(1, &device->cursorTexture);
5096 device->cursorTexture = 0;
5098 LEAVE_GL();
5100 device->blitter->free_private(device);
5101 device->frag_pipe->free_private(device);
5102 device->shader_backend->shader_free_private(device);
5103 destroy_dummy_textures(device, gl_info);
5105 context_release(context);
5107 while (device->context_count)
5109 swapchain_destroy_contexts(device->contexts[0]->swapchain);
5112 HeapFree(GetProcessHeap(), 0, swapchain->context);
5113 swapchain->context = NULL;
5116 /* Do not call while under the GL lock. */
5117 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5119 struct wined3d_context *context;
5120 struct wined3d_surface *target;
5121 HRESULT hr;
5123 /* Recreate the primary swapchain's context */
5124 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
5125 if (!swapchain->context)
5127 ERR("Failed to allocate memory for swapchain context array.\n");
5128 return E_OUTOFMEMORY;
5131 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
5132 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
5134 WARN("Failed to create context.\n");
5135 HeapFree(GetProcessHeap(), 0, swapchain->context);
5136 return E_FAIL;
5139 swapchain->context[0] = context;
5140 swapchain->num_contexts = 1;
5141 create_dummy_textures(device, context);
5142 context_release(context);
5144 hr = device->shader_backend->shader_alloc_private(device);
5145 if (FAILED(hr))
5147 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
5148 goto err;
5151 hr = device->frag_pipe->alloc_private(device);
5152 if (FAILED(hr))
5154 ERR("Failed to allocate fragment pipe private data, hr %#x.\n", hr);
5155 device->shader_backend->shader_free_private(device);
5156 goto err;
5159 hr = device->blitter->alloc_private(device);
5160 if (FAILED(hr))
5162 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
5163 device->frag_pipe->free_private(device);
5164 device->shader_backend->shader_free_private(device);
5165 goto err;
5168 return WINED3D_OK;
5170 err:
5171 context_acquire(device, NULL);
5172 destroy_dummy_textures(device, context->gl_info);
5173 context_release(context);
5174 context_destroy(device, context);
5175 HeapFree(GetProcessHeap(), 0, swapchain->context);
5176 swapchain->num_contexts = 0;
5177 return hr;
5180 /* Do not call while under the GL lock. */
5181 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5182 const struct wined3d_swapchain_desc *swapchain_desc,
5183 wined3d_device_reset_cb callback)
5185 struct wined3d_resource *resource, *cursor;
5186 struct wined3d_swapchain *swapchain;
5187 struct wined3d_display_mode mode;
5188 BOOL DisplayModeChanged = FALSE;
5189 BOOL update_desc = FALSE;
5190 unsigned int i;
5191 HRESULT hr;
5193 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
5195 if (FAILED(hr = wined3d_device_get_swapchain(device, 0, &swapchain)))
5197 ERR("Failed to get the first implicit swapchain.\n");
5198 return hr;
5201 stateblock_unbind_resources(device->stateBlock);
5202 if (swapchain->back_buffers && swapchain->back_buffers[0])
5203 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
5204 else
5205 wined3d_device_set_render_target(device, 0, swapchain->front_buffer, FALSE);
5206 for (i = 1; i < device->adapter->gl_info.limits.buffers; ++i)
5208 wined3d_device_set_render_target(device, i, NULL, FALSE);
5210 wined3d_device_set_depth_stencil(device, NULL);
5212 if (device->onscreen_depth_stencil)
5214 wined3d_surface_decref(device->onscreen_depth_stencil);
5215 device->onscreen_depth_stencil = NULL;
5218 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5220 TRACE("Enumerating resource %p.\n", resource);
5221 if (FAILED(hr = callback(resource)))
5223 wined3d_swapchain_decref(swapchain);
5224 return hr;
5228 if (!is_display_mode_supported(device, swapchain_desc))
5230 WARN("Rejecting reset() call because the requested display mode is not supported.\n");
5231 WARN("Requested mode: %ux%u.\n",
5232 swapchain_desc->backbuffer_width,
5233 swapchain_desc->backbuffer_height);
5234 wined3d_swapchain_decref(swapchain);
5235 return WINED3DERR_INVALIDCALL;
5238 /* Is it necessary to recreate the gl context? Actually every setting can be changed
5239 * on an existing gl context, so there's no real need for recreation.
5241 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
5243 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
5245 TRACE("New params:\n");
5246 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5247 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5248 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5249 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5250 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5251 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5252 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5253 TRACE("device_window %p\n", swapchain_desc->device_window);
5254 TRACE("windowed %#x\n", swapchain_desc->windowed);
5255 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5256 if (swapchain_desc->enable_auto_depth_stencil)
5257 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5258 TRACE("flags %#x\n", swapchain_desc->flags);
5259 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5260 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
5261 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5263 /* No special treatment of these parameters. Just store them */
5264 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
5265 swapchain->desc.flags = swapchain_desc->flags;
5266 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
5267 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
5269 /* What to do about these? */
5270 if (swapchain_desc->backbuffer_count
5271 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
5272 FIXME("Cannot change the back buffer count yet.\n");
5274 if (swapchain_desc->device_window
5275 && swapchain_desc->device_window != swapchain->desc.device_window)
5277 TRACE("Changing the device window from %p to %p.\n",
5278 swapchain->desc.device_window, swapchain_desc->device_window);
5279 swapchain->desc.device_window = swapchain_desc->device_window;
5280 swapchain->device_window = swapchain_desc->device_window;
5281 wined3d_swapchain_set_window(swapchain, NULL);
5284 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
5286 HRESULT hrc;
5288 TRACE("Creating the depth stencil buffer\n");
5290 hrc = device->device_parent->ops->create_depth_stencil(device->device_parent,
5291 swapchain_desc->backbuffer_width,
5292 swapchain_desc->backbuffer_height,
5293 swapchain_desc->auto_depth_stencil_format,
5294 swapchain_desc->multisample_type,
5295 swapchain_desc->multisample_quality,
5296 FALSE,
5297 &device->auto_depth_stencil);
5298 if (FAILED(hrc))
5300 ERR("Failed to create the depth stencil buffer.\n");
5301 wined3d_swapchain_decref(swapchain);
5302 return WINED3DERR_INVALIDCALL;
5306 if (device->onscreen_depth_stencil)
5308 wined3d_surface_decref(device->onscreen_depth_stencil);
5309 device->onscreen_depth_stencil = NULL;
5312 /* Reset the depth stencil */
5313 if (swapchain_desc->enable_auto_depth_stencil)
5314 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5316 TRACE("Resetting stateblock\n");
5317 wined3d_stateblock_decref(device->updateStateBlock);
5318 wined3d_stateblock_decref(device->stateBlock);
5320 if (swapchain_desc->windowed)
5322 mode.width = swapchain->orig_width;
5323 mode.height = swapchain->orig_height;
5324 mode.refresh_rate = 0;
5325 mode.format_id = swapchain->desc.backbuffer_format;
5327 else
5329 mode.width = swapchain_desc->backbuffer_width;
5330 mode.height = swapchain_desc->backbuffer_height;
5331 mode.refresh_rate = swapchain_desc->refresh_rate;
5332 mode.format_id = swapchain_desc->backbuffer_format;
5335 /* Should Width == 800 && Height == 0 set 800x600? */
5336 if (swapchain_desc->backbuffer_width && swapchain_desc->backbuffer_height
5337 && (swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
5338 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height))
5340 if (!swapchain_desc->windowed)
5341 DisplayModeChanged = TRUE;
5343 swapchain->desc.backbuffer_width = swapchain_desc->backbuffer_width;
5344 swapchain->desc.backbuffer_height = swapchain_desc->backbuffer_height;
5345 update_desc = TRUE;
5348 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5349 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5351 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5352 update_desc = TRUE;
5355 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5356 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5358 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5359 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5360 update_desc = TRUE;
5363 if (update_desc)
5365 UINT i;
5367 hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5368 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5369 swapchain->desc.multisample_type, swapchain->desc.multisample_quality);
5370 if (FAILED(hr))
5372 wined3d_swapchain_decref(swapchain);
5373 return hr;
5376 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5378 hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5379 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5380 swapchain->desc.multisample_type, swapchain->desc.multisample_quality);
5381 if (FAILED(hr))
5383 wined3d_swapchain_decref(swapchain);
5384 return hr;
5387 if (device->auto_depth_stencil)
5389 hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5390 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5391 swapchain->desc.multisample_type, swapchain->desc.multisample_quality);
5392 if (FAILED(hr))
5394 wined3d_swapchain_decref(swapchain);
5395 return hr;
5400 if (device->d3d_initialized)
5401 delete_opengl_contexts(device, swapchain);
5403 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5404 || DisplayModeChanged)
5406 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode)))
5408 WARN("Failed to set display mode, hr %#x.\n", hr);
5409 wined3d_swapchain_decref(swapchain);
5410 return hr;
5413 if (!swapchain_desc->windowed)
5415 if (swapchain->desc.windowed)
5417 HWND focus_window = device->create_parms.focus_window;
5418 if (!focus_window)
5419 focus_window = swapchain_desc->device_window;
5420 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5422 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5423 wined3d_swapchain_decref(swapchain);
5424 return hr;
5427 /* switch from windowed to fs */
5428 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5429 swapchain_desc->backbuffer_width,
5430 swapchain_desc->backbuffer_height);
5432 else
5434 /* Fullscreen -> fullscreen mode change */
5435 MoveWindow(swapchain->device_window, 0, 0,
5436 swapchain_desc->backbuffer_width,
5437 swapchain_desc->backbuffer_height,
5438 TRUE);
5441 else if (!swapchain->desc.windowed)
5443 /* Fullscreen -> windowed switch */
5444 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5445 wined3d_device_release_focus_window(device);
5447 swapchain->desc.windowed = swapchain_desc->windowed;
5449 else if (!swapchain_desc->windowed)
5451 DWORD style = device->style;
5452 DWORD exStyle = device->exStyle;
5453 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5454 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5455 * Reset to clear up their mess. Guild Wars also loses the device during that.
5457 device->style = 0;
5458 device->exStyle = 0;
5459 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5460 swapchain_desc->backbuffer_width,
5461 swapchain_desc->backbuffer_height);
5462 device->style = style;
5463 device->exStyle = exStyle;
5466 /* Note: No parent needed for initial internal stateblock */
5467 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5468 if (FAILED(hr))
5469 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5470 else
5471 TRACE("Created stateblock %p.\n", device->stateBlock);
5472 device->updateStateBlock = device->stateBlock;
5473 wined3d_stateblock_incref(device->updateStateBlock);
5475 stateblock_init_default_state(device->stateBlock);
5477 swapchain_update_render_to_fbo(swapchain);
5478 swapchain_update_draw_bindings(swapchain);
5480 if (device->d3d_initialized)
5481 hr = create_primary_opengl_context(device, swapchain);
5482 wined3d_swapchain_decref(swapchain);
5484 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5485 * first use
5487 return hr;
5490 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5492 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5494 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5496 return WINED3D_OK;
5500 HRESULT CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5501 struct wined3d_device_creation_parameters *parameters)
5503 TRACE("device %p, parameters %p.\n", device, parameters);
5505 *parameters = device->create_parms;
5506 return WINED3D_OK;
5509 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5510 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5512 struct wined3d_swapchain *swapchain;
5514 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5515 device, swapchain_idx, flags, ramp);
5517 if (SUCCEEDED(wined3d_device_get_swapchain(device, swapchain_idx, &swapchain)))
5519 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5520 wined3d_swapchain_decref(swapchain);
5524 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5525 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5527 struct wined3d_swapchain *swapchain;
5529 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5530 device, swapchain_idx, ramp);
5532 if (SUCCEEDED(wined3d_device_get_swapchain(device, swapchain_idx, &swapchain)))
5534 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5535 wined3d_swapchain_decref(swapchain);
5539 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5541 TRACE("device %p, resource %p.\n", device, resource);
5543 list_add_head(&device->resources, &resource->resource_list_entry);
5546 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5548 TRACE("device %p, resource %p.\n", device, resource);
5550 list_remove(&resource->resource_list_entry);
5553 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5555 enum wined3d_resource_type type = resource->type;
5556 unsigned int i;
5558 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5560 context_resource_released(device, resource, type);
5562 switch (type)
5564 case WINED3D_RTYPE_SURFACE:
5566 struct wined3d_surface *surface = surface_from_resource(resource);
5568 if (!device->d3d_initialized) break;
5570 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5572 if (device->fb.render_targets[i] == surface)
5574 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5575 device->fb.render_targets[i] = NULL;
5579 if (device->fb.depth_stencil == surface)
5581 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5582 device->fb.depth_stencil = NULL;
5585 break;
5587 case WINED3D_RTYPE_TEXTURE:
5588 case WINED3D_RTYPE_CUBE_TEXTURE:
5589 case WINED3D_RTYPE_VOLUME_TEXTURE:
5590 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5592 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5594 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5596 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5597 texture, device->stateBlock, i);
5598 device->stateBlock->state.textures[i] = NULL;
5601 if (device->updateStateBlock != device->stateBlock
5602 && device->updateStateBlock->state.textures[i] == texture)
5604 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5605 texture, device->updateStateBlock, i);
5606 device->updateStateBlock->state.textures[i] = NULL;
5609 break;
5611 case WINED3D_RTYPE_BUFFER:
5613 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5615 for (i = 0; i < MAX_STREAMS; ++i)
5617 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5619 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5620 buffer, device->stateBlock, i);
5621 device->stateBlock->state.streams[i].buffer = NULL;
5624 if (device->updateStateBlock != device->stateBlock
5625 && device->updateStateBlock->state.streams[i].buffer == buffer)
5627 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5628 buffer, device->updateStateBlock, i);
5629 device->updateStateBlock->state.streams[i].buffer = NULL;
5634 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5636 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5637 buffer, device->stateBlock);
5638 device->stateBlock->state.index_buffer = NULL;
5641 if (device->updateStateBlock != device->stateBlock
5642 && device->updateStateBlock->state.index_buffer == buffer)
5644 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5645 buffer, device->updateStateBlock);
5646 device->updateStateBlock->state.index_buffer = NULL;
5649 break;
5651 default:
5652 break;
5655 /* Remove the resource from the resourceStore */
5656 device_resource_remove(device, resource);
5658 TRACE("Resource released.\n");
5661 HRESULT CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device,
5662 HDC dc, struct wined3d_surface **surface)
5664 struct wined3d_resource *resource;
5666 TRACE("device %p, dc %p, surface %p.\n", device, dc, surface);
5668 if (!dc)
5669 return WINED3DERR_INVALIDCALL;
5671 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5673 if (resource->type == WINED3D_RTYPE_SURFACE)
5675 struct wined3d_surface *s = surface_from_resource(resource);
5677 if (s->hDC == dc)
5679 TRACE("Found surface %p for dc %p.\n", s, dc);
5680 *surface = s;
5681 return WINED3D_OK;
5686 return WINED3DERR_INVALIDCALL;
5689 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5690 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5691 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5693 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5694 const struct fragment_pipeline *fragment_pipeline;
5695 struct wined3d_display_mode mode;
5696 struct shader_caps shader_caps;
5697 struct fragment_caps ffp_caps;
5698 unsigned int i;
5699 HRESULT hr;
5701 device->ref = 1;
5702 device->wined3d = wined3d;
5703 wined3d_incref(device->wined3d);
5704 device->adapter = wined3d->adapter_count ? adapter : NULL;
5705 device->device_parent = device_parent;
5706 list_init(&device->resources);
5707 list_init(&device->shaders);
5708 device->surface_alignment = surface_alignment;
5710 /* Get the initial screen setup for ddraw. */
5711 hr = wined3d_get_adapter_display_mode(wined3d, adapter_idx, &mode);
5712 if (FAILED(hr))
5714 ERR("Failed to get the adapter's display mode, hr %#x.\n", hr);
5715 wined3d_decref(device->wined3d);
5716 return hr;
5718 adapter->screen_size.cx = mode.width;
5719 adapter->screen_size.cy = mode.height;
5720 adapter->screen_format = mode.format_id;
5722 /* Save the creation parameters. */
5723 device->create_parms.adapter_idx = adapter_idx;
5724 device->create_parms.device_type = device_type;
5725 device->create_parms.focus_window = focus_window;
5726 device->create_parms.flags = flags;
5728 for (i = 0; i < PATCHMAP_SIZE; ++i) list_init(&device->patches[i]);
5730 select_shader_mode(&adapter->gl_info, &device->ps_selected_mode, &device->vs_selected_mode);
5731 device->shader_backend = adapter->shader_backend;
5733 if (device->shader_backend)
5735 device->shader_backend->shader_get_caps(&adapter->gl_info, &shader_caps);
5736 device->vshader_version = shader_caps.VertexShaderVersion;
5737 device->pshader_version = shader_caps.PixelShaderVersion;
5738 device->d3d_vshader_constantF = shader_caps.MaxVertexShaderConst;
5739 device->d3d_pshader_constantF = shader_caps.MaxPixelShaderConst;
5740 device->vs_clipping = shader_caps.VSClipping;
5742 fragment_pipeline = adapter->fragment_pipe;
5743 device->frag_pipe = fragment_pipeline;
5744 if (fragment_pipeline)
5746 fragment_pipeline->get_caps(&adapter->gl_info, &ffp_caps);
5747 device->max_ffp_textures = ffp_caps.MaxSimultaneousTextures;
5749 hr = compile_state_table(device->StateTable, device->multistate_funcs, &adapter->gl_info,
5750 ffp_vertexstate_template, fragment_pipeline, misc_state_template);
5751 if (FAILED(hr))
5753 ERR("Failed to compile state table, hr %#x.\n", hr);
5754 wined3d_decref(device->wined3d);
5755 return hr;
5758 device->blitter = adapter->blitter;
5760 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5761 if (FAILED(hr))
5763 WARN("Failed to create stateblock.\n");
5764 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5766 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5768 wined3d_decref(device->wined3d);
5769 return hr;
5772 TRACE("Created stateblock %p.\n", device->stateBlock);
5773 device->updateStateBlock = device->stateBlock;
5774 wined3d_stateblock_incref(device->updateStateBlock);
5776 return WINED3D_OK;
5780 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5782 DWORD rep = device->StateTable[state].representative;
5783 struct wined3d_context *context;
5784 DWORD idx;
5785 BYTE shift;
5786 UINT i;
5788 for (i = 0; i < device->context_count; ++i)
5790 context = device->contexts[i];
5791 if(isStateDirty(context, rep)) continue;
5793 context->dirtyArray[context->numDirtyEntries++] = rep;
5794 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5795 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5796 context->isStateDirty[idx] |= (1 << shift);
5800 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5802 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5803 *width = context->current_rt->pow2Width;
5804 *height = context->current_rt->pow2Height;
5807 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5809 const struct wined3d_swapchain *swapchain = context->swapchain;
5810 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5811 * current context's drawable, which is the size of the back buffer of the swapchain
5812 * the active context belongs to. */
5813 *width = swapchain->desc.backbuffer_width;
5814 *height = swapchain->desc.backbuffer_height;
5817 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5818 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5820 if (device->filter_messages)
5822 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5823 window, message, wparam, lparam);
5824 if (unicode)
5825 return DefWindowProcW(window, message, wparam, lparam);
5826 else
5827 return DefWindowProcA(window, message, wparam, lparam);
5830 if (message == WM_DESTROY)
5832 TRACE("unregister window %p.\n", window);
5833 wined3d_unregister_window(window);
5835 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5836 ERR("Window %p is not the focus window for device %p.\n", window, device);
5838 else if (message == WM_DISPLAYCHANGE)
5840 device->device_parent->ops->mode_changed(device->device_parent);
5843 if (unicode)
5844 return CallWindowProcW(proc, window, message, wparam, lparam);
5845 else
5846 return CallWindowProcA(proc, window, message, wparam, lparam);