wmiutils: Don't prepend a backslash if WBEMPATH_GET_NAMESPACE_ONLY is specified.
[wine/multimedia.git] / dlls / wined3d / device.c
blob42cd22646134220aad6e642a59b1490480a6476d
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 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 const struct wined3d_gl_info *gl_info;
650 UINT drawable_width, drawable_height;
651 struct wined3d_context *context;
652 GLbitfield clear_mask = 0;
653 BOOL render_offscreen;
654 unsigned int i;
655 RECT ds_rect;
657 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
658 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
659 * for the cleared parts, and the untouched parts.
661 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
662 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
663 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
664 * checking all this if the dest surface is in the drawable anyway. */
665 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
667 for (i = 0; i < rt_count; ++i)
669 struct wined3d_surface *rt = fb->render_targets[i];
670 if (rt)
671 surface_load_location(rt, rt->draw_binding, NULL);
675 context = context_acquire(device, target);
676 if (!context->valid)
678 context_release(context);
679 WARN("Invalid context, skipping clear.\n");
680 return;
682 gl_info = context->gl_info;
684 if (target)
686 render_offscreen = context->render_offscreen;
687 target->get_drawable_size(context, &drawable_width, &drawable_height);
689 else
691 render_offscreen = TRUE;
692 drawable_width = fb->depth_stencil->pow2Width;
693 drawable_height = fb->depth_stencil->pow2Height;
696 if (flags & WINED3DCLEAR_ZBUFFER)
698 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
700 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
701 device_switch_onscreen_ds(device, context, fb->depth_stencil);
702 prepare_ds_clear(fb->depth_stencil, context, location,
703 draw_rect, rect_count, clear_rect, &ds_rect);
706 if (!context_apply_clear_state(context, device, rt_count, fb))
708 context_release(context);
709 WARN("Failed to apply clear state, skipping clear.\n");
710 return;
713 /* Only set the values up once, as they are not changing. */
714 if (flags & WINED3DCLEAR_STENCIL)
716 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
718 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
719 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
721 gl_info->gl_ops.gl.p_glStencilMask(~0U);
722 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
723 gl_info->gl_ops.gl.p_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 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
735 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
736 gl_info->gl_ops.gl.p_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 gl_info->gl_ops.gl.p_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 gl_info->gl_ops.gl.p_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 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
766 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
768 else
770 gl_info->gl_ops.gl.p_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 gl_info->gl_ops.gl.p_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 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
803 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
805 else
807 gl_info->gl_ops.gl.p_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 gl_info->gl_ops.gl.p_glClear(clear_mask);
813 checkGLcall("glClear");
817 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
818 && target->container.type == WINED3D_CONTAINER_SWAPCHAIN
819 && target->container.u.swapchain->front_buffer == target))
820 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
822 context_release(context);
825 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
827 ULONG refcount = InterlockedIncrement(&device->ref);
829 TRACE("%p increasing refcount to %u.\n", device, refcount);
831 return refcount;
834 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
836 ULONG refcount = InterlockedDecrement(&device->ref);
838 TRACE("%p decreasing refcount to %u.\n", device, refcount);
840 if (!refcount)
842 struct wined3d_stateblock *stateblock;
843 UINT i;
845 if (wined3d_stateblock_decref(device->updateStateBlock)
846 && device->updateStateBlock != device->stateBlock)
847 FIXME("Something's still holding the update stateblock.\n");
848 device->updateStateBlock = NULL;
850 stateblock = device->stateBlock;
851 device->stateBlock = NULL;
852 if (wined3d_stateblock_decref(stateblock))
853 FIXME("Something's still holding the stateblock.\n");
855 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
857 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
858 device->multistate_funcs[i] = NULL;
861 if (!list_empty(&device->resources))
863 struct wined3d_resource *resource;
865 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
867 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
869 FIXME("Leftover resource %p with type %s (%#x).\n",
870 resource, debug_d3dresourcetype(resource->type), resource->type);
874 if (device->contexts)
875 ERR("Context array not freed!\n");
876 if (device->hardwareCursor)
877 DestroyCursor(device->hardwareCursor);
878 device->hardwareCursor = 0;
880 wined3d_decref(device->wined3d);
881 device->wined3d = NULL;
882 HeapFree(GetProcessHeap(), 0, device);
883 TRACE("Freed device %p.\n", device);
886 return refcount;
889 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
891 TRACE("device %p.\n", device);
893 return device->swapchain_count;
896 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
898 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
900 if (swapchain_idx >= device->swapchain_count)
902 WARN("swapchain_idx %u >= swapchain_count %u.\n",
903 swapchain_idx, device->swapchain_count);
904 return NULL;
907 return device->swapchains[swapchain_idx];
910 static void device_load_logo(struct wined3d_device *device, const char *filename)
912 struct wined3d_color_key color_key;
913 HBITMAP hbm;
914 BITMAP bm;
915 HRESULT hr;
916 HDC dcb = NULL, dcs = NULL;
918 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
919 if(hbm)
921 GetObjectA(hbm, sizeof(BITMAP), &bm);
922 dcb = CreateCompatibleDC(NULL);
923 if(!dcb) goto out;
924 SelectObject(dcb, hbm);
926 else
928 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
929 * couldn't be loaded
931 memset(&bm, 0, sizeof(bm));
932 bm.bmWidth = 32;
933 bm.bmHeight = 32;
936 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0,
937 WINED3D_POOL_SYSTEM_MEM, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_TYPE_OPENGL, WINED3D_SURFACE_MAPPABLE,
938 NULL, &wined3d_null_parent_ops, &device->logo_surface);
939 if (FAILED(hr))
941 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
942 goto out;
945 if (dcb)
947 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
948 goto out;
949 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
950 wined3d_surface_releasedc(device->logo_surface, dcs);
952 color_key.color_space_low_value = 0;
953 color_key.color_space_high_value = 0;
954 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
956 else
958 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
959 /* Fill the surface with a white color to show that wined3d is there */
960 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
963 out:
964 if (dcb) DeleteDC(dcb);
965 if (hbm) DeleteObject(hbm);
968 /* Context activation is done by the caller. */
969 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
971 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
972 unsigned int i, j, count;
973 /* Under DirectX you can sample even if no texture is bound, whereas
974 * OpenGL will only allow that when a valid texture is bound.
975 * We emulate this by creating dummy textures and binding them
976 * to each texture stage when the currently set D3D texture is NULL. */
978 if (gl_info->supported[APPLE_CLIENT_STORAGE])
980 /* The dummy texture does not have client storage backing */
981 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
982 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
985 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
986 for (i = 0; i < count; ++i)
988 DWORD color = 0x000000ff;
990 /* Make appropriate texture active */
991 context_active_texture(context, gl_info, i);
993 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
994 checkGLcall("glGenTextures");
995 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
997 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
998 checkGLcall("glBindTexture");
1000 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
1001 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1002 checkGLcall("glTexImage2D");
1004 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1006 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
1007 checkGLcall("glGenTextures");
1008 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
1010 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1011 checkGLcall("glBindTexture");
1013 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
1014 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1015 checkGLcall("glTexImage2D");
1018 if (gl_info->supported[EXT_TEXTURE3D])
1020 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
1021 checkGLcall("glGenTextures");
1022 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
1024 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1025 checkGLcall("glBindTexture");
1027 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
1028 checkGLcall("glTexImage3D");
1031 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1033 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
1034 checkGLcall("glGenTextures");
1035 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
1037 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1038 checkGLcall("glBindTexture");
1040 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
1042 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
1043 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1044 checkGLcall("glTexImage2D");
1049 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1051 /* Re-enable because if supported it is enabled by default */
1052 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1053 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1057 /* Context activation is done by the caller. */
1058 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
1060 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1062 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1064 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
1065 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
1068 if (gl_info->supported[EXT_TEXTURE3D])
1070 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
1071 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
1074 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1076 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
1077 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
1080 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
1081 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
1083 memset(device->dummy_texture_cube, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_cube));
1084 memset(device->dummy_texture_3d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_3d));
1085 memset(device->dummy_texture_rect, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_rect));
1086 memset(device->dummy_texture_2d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_2d));
1089 static LONG fullscreen_style(LONG style)
1091 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1092 style |= WS_POPUP | WS_SYSMENU;
1093 style &= ~(WS_CAPTION | WS_THICKFRAME);
1095 return style;
1098 static LONG fullscreen_exstyle(LONG exstyle)
1100 /* Filter out window decorations. */
1101 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1103 return exstyle;
1106 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1108 BOOL filter_messages;
1109 LONG style, exstyle;
1111 TRACE("Setting up window %p for fullscreen mode.\n", window);
1113 if (device->style || device->exStyle)
1115 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1116 window, device->style, device->exStyle);
1119 device->style = GetWindowLongW(window, GWL_STYLE);
1120 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1122 style = fullscreen_style(device->style);
1123 exstyle = fullscreen_exstyle(device->exStyle);
1125 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1126 device->style, device->exStyle, style, exstyle);
1128 filter_messages = device->filter_messages;
1129 device->filter_messages = TRUE;
1131 SetWindowLongW(window, GWL_STYLE, style);
1132 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1133 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1135 device->filter_messages = filter_messages;
1138 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1140 BOOL filter_messages;
1141 LONG style, exstyle;
1143 if (!device->style && !device->exStyle) return;
1145 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1146 window, device->style, device->exStyle);
1148 style = GetWindowLongW(window, GWL_STYLE);
1149 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1151 filter_messages = device->filter_messages;
1152 device->filter_messages = TRUE;
1154 /* Only restore the style if the application didn't modify it during the
1155 * fullscreen phase. Some applications change it before calling Reset()
1156 * when switching between windowed and fullscreen modes (HL2), some
1157 * depend on the original style (Eve Online). */
1158 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1160 SetWindowLongW(window, GWL_STYLE, device->style);
1161 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1163 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1165 device->filter_messages = filter_messages;
1167 /* Delete the old values. */
1168 device->style = 0;
1169 device->exStyle = 0;
1172 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1174 TRACE("device %p, window %p.\n", device, window);
1176 if (!wined3d_register_window(window, device))
1178 ERR("Failed to register window %p.\n", window);
1179 return E_FAIL;
1182 InterlockedExchangePointer((void **)&device->focus_window, window);
1183 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1185 return WINED3D_OK;
1188 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1190 TRACE("device %p.\n", device);
1192 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1193 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1196 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1197 struct wined3d_swapchain_desc *swapchain_desc)
1199 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1200 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1201 struct wined3d_swapchain *swapchain = NULL;
1202 struct wined3d_context *context;
1203 HRESULT hr;
1204 DWORD state;
1205 unsigned int i;
1207 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1209 if (device->d3d_initialized)
1210 return WINED3DERR_INVALIDCALL;
1211 if (!device->adapter->opengl)
1212 return WINED3DERR_INVALIDCALL;
1214 device->valid_rt_mask = 0;
1215 for (i = 0; i < gl_info->limits.buffers; ++i)
1216 device->valid_rt_mask |= (1 << i);
1217 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1218 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1220 /* Initialize the texture unit mapping to a 1:1 mapping */
1221 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1223 if (state < gl_info->limits.fragment_samplers)
1225 device->texUnitMap[state] = state;
1226 device->rev_tex_unit_map[state] = state;
1228 else
1230 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1231 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1235 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
1237 TRACE("Shader private data couldn't be allocated\n");
1238 goto err_out;
1240 if (FAILED(hr = device->blitter->alloc_private(device)))
1242 TRACE("Blitter private data couldn't be allocated\n");
1243 goto err_out;
1246 /* Setup the implicit swapchain. This also initializes a context. */
1247 TRACE("Creating implicit swapchain\n");
1248 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1249 swapchain_desc, &swapchain);
1250 if (FAILED(hr))
1252 WARN("Failed to create implicit swapchain\n");
1253 goto err_out;
1256 device->swapchain_count = 1;
1257 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1258 if (!device->swapchains)
1260 ERR("Out of memory!\n");
1261 goto err_out;
1263 device->swapchains[0] = swapchain;
1265 if (swapchain->back_buffers && swapchain->back_buffers[0])
1267 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1268 device->fb.render_targets[0] = swapchain->back_buffers[0];
1270 else
1272 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1273 device->fb.render_targets[0] = swapchain->front_buffer;
1275 wined3d_surface_incref(device->fb.render_targets[0]);
1277 /* Depth Stencil support */
1278 device->fb.depth_stencil = device->auto_depth_stencil;
1279 if (device->fb.depth_stencil)
1280 wined3d_surface_incref(device->fb.depth_stencil);
1282 /* Set up some starting GL setup */
1284 /* Setup all the devices defaults */
1285 stateblock_init_default_state(device->stateBlock);
1287 context = context_acquire(device, swapchain->front_buffer);
1289 create_dummy_textures(device, context);
1291 /* Initialize the current view state */
1292 device->view_ident = 1;
1293 device->contexts[0]->last_was_rhw = 0;
1295 switch (wined3d_settings.offscreen_rendering_mode)
1297 case ORM_FBO:
1298 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1299 break;
1301 case ORM_BACKBUFFER:
1303 if (context_get_current()->aux_buffers > 0)
1305 TRACE("Using auxiliary buffer for offscreen rendering\n");
1306 device->offscreenBuffer = GL_AUX0;
1308 else
1310 TRACE("Using back buffer for offscreen rendering\n");
1311 device->offscreenBuffer = GL_BACK;
1316 TRACE("All defaults now set up, leaving 3D init.\n");
1318 context_release(context);
1320 /* Clear the screen */
1321 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1322 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1323 &black, 1.0f, 0);
1325 device->d3d_initialized = TRUE;
1327 if (wined3d_settings.logo)
1328 device_load_logo(device, wined3d_settings.logo);
1329 return WINED3D_OK;
1331 err_out:
1332 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1333 HeapFree(GetProcessHeap(), 0, device->swapchains);
1334 device->swapchain_count = 0;
1335 if (swapchain)
1336 wined3d_swapchain_decref(swapchain);
1337 if (device->blit_priv)
1338 device->blitter->free_private(device);
1339 if (device->shader_priv)
1340 device->shader_backend->shader_free_private(device);
1342 return hr;
1345 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1346 struct wined3d_swapchain_desc *swapchain_desc)
1348 struct wined3d_swapchain *swapchain = NULL;
1349 HRESULT hr;
1351 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1353 /* Setup the implicit swapchain */
1354 TRACE("Creating implicit swapchain\n");
1355 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1356 swapchain_desc, &swapchain);
1357 if (FAILED(hr))
1359 WARN("Failed to create implicit swapchain\n");
1360 goto err_out;
1363 device->swapchain_count = 1;
1364 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1365 if (!device->swapchains)
1367 ERR("Out of memory!\n");
1368 goto err_out;
1370 device->swapchains[0] = swapchain;
1371 return WINED3D_OK;
1373 err_out:
1374 wined3d_swapchain_decref(swapchain);
1375 return hr;
1378 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1380 struct wined3d_resource *resource, *cursor;
1381 const struct wined3d_gl_info *gl_info;
1382 struct wined3d_context *context;
1383 struct wined3d_surface *surface;
1384 UINT i;
1386 TRACE("device %p.\n", device);
1388 if (!device->d3d_initialized)
1389 return WINED3DERR_INVALIDCALL;
1391 /* Force making the context current again, to verify it is still valid
1392 * (workaround for broken drivers) */
1393 context_set_current(NULL);
1394 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1395 * it was created. Thus make sure a context is active for the glDelete* calls
1397 context = context_acquire(device, NULL);
1398 gl_info = context->gl_info;
1400 if (device->logo_surface)
1401 wined3d_surface_decref(device->logo_surface);
1403 stateblock_unbind_resources(device->stateBlock);
1405 /* Unload resources */
1406 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1408 TRACE("Unloading resource %p.\n", resource);
1410 resource->resource_ops->resource_unload(resource);
1413 TRACE("Deleting high order patches\n");
1414 for (i = 0; i < PATCHMAP_SIZE; ++i)
1416 struct wined3d_rect_patch *patch;
1417 struct list *e1, *e2;
1419 LIST_FOR_EACH_SAFE(e1, e2, &device->patches[i])
1421 patch = LIST_ENTRY(e1, struct wined3d_rect_patch, entry);
1422 wined3d_device_delete_patch(device, patch->Handle);
1426 /* Delete the mouse cursor texture */
1427 if (device->cursorTexture)
1429 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
1430 device->cursorTexture = 0;
1433 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1434 * private data, it might contain opengl pointers
1436 if (device->depth_blt_texture)
1438 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1439 device->depth_blt_texture = 0;
1442 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1443 device->blitter->free_private(device);
1444 device->shader_backend->shader_free_private(device);
1446 /* Release the buffers (with sanity checks)*/
1447 if (device->onscreen_depth_stencil)
1449 surface = device->onscreen_depth_stencil;
1450 device->onscreen_depth_stencil = NULL;
1451 wined3d_surface_decref(surface);
1454 if (device->fb.depth_stencil)
1456 surface = device->fb.depth_stencil;
1458 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1460 device->fb.depth_stencil = NULL;
1461 wined3d_surface_decref(surface);
1464 if (device->auto_depth_stencil)
1466 surface = device->auto_depth_stencil;
1467 device->auto_depth_stencil = NULL;
1468 if (wined3d_surface_decref(surface))
1469 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1472 for (i = 1; i < gl_info->limits.buffers; ++i)
1474 wined3d_device_set_render_target(device, i, NULL, FALSE);
1477 surface = device->fb.render_targets[0];
1478 TRACE("Setting rendertarget 0 to NULL\n");
1479 device->fb.render_targets[0] = NULL;
1480 TRACE("Releasing the render target at %p\n", surface);
1481 wined3d_surface_decref(surface);
1483 context_release(context);
1485 for (i = 0; i < device->swapchain_count; ++i)
1487 TRACE("Releasing the implicit swapchain %u.\n", i);
1488 if (wined3d_swapchain_decref(device->swapchains[i]))
1489 FIXME("Something's still holding the implicit swapchain.\n");
1492 HeapFree(GetProcessHeap(), 0, device->swapchains);
1493 device->swapchains = NULL;
1494 device->swapchain_count = 0;
1496 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1497 device->fb.render_targets = NULL;
1499 device->d3d_initialized = FALSE;
1501 return WINED3D_OK;
1504 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1506 unsigned int i;
1508 for (i = 0; i < device->swapchain_count; ++i)
1510 TRACE("Releasing the implicit swapchain %u.\n", i);
1511 if (wined3d_swapchain_decref(device->swapchains[i]))
1512 FIXME("Something's still holding the implicit swapchain.\n");
1515 HeapFree(GetProcessHeap(), 0, device->swapchains);
1516 device->swapchains = NULL;
1517 device->swapchain_count = 0;
1518 return WINED3D_OK;
1521 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1522 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1523 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1525 * There is no way to deactivate thread safety once it is enabled.
1527 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1529 TRACE("device %p.\n", device);
1531 /* For now just store the flag (needed in case of ddraw). */
1532 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1535 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1537 TRACE("device %p.\n", device);
1539 TRACE("Emulating %d MB, returning %d MB left.\n",
1540 device->adapter->TextureRam / (1024 * 1024),
1541 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1543 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1546 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1547 struct wined3d_buffer *buffer, UINT offset)
1549 struct wined3d_buffer *prev_buffer;
1551 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1553 if (idx >= MAX_STREAM_OUT)
1555 WARN("Invalid stream output %u.\n", idx);
1556 return;
1559 prev_buffer = device->updateStateBlock->state.stream_output[idx].buffer;
1560 device->updateStateBlock->state.stream_output[idx].buffer = buffer;
1561 device->updateStateBlock->state.stream_output[idx].offset = offset;
1563 if (device->isRecordingState)
1565 if (buffer)
1566 wined3d_buffer_incref(buffer);
1567 if (prev_buffer)
1568 wined3d_buffer_decref(prev_buffer);
1569 return;
1572 if (prev_buffer != buffer)
1574 if (buffer)
1576 InterlockedIncrement(&buffer->resource.bind_count);
1577 wined3d_buffer_incref(buffer);
1579 if (prev_buffer)
1581 InterlockedDecrement(&prev_buffer->resource.bind_count);
1582 wined3d_buffer_decref(prev_buffer);
1587 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1588 UINT idx, UINT *offset)
1590 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1592 if (idx >= MAX_STREAM_OUT)
1594 WARN("Invalid stream output %u.\n", idx);
1595 return NULL;
1598 *offset = device->stateBlock->state.stream_output[idx].offset;
1599 return device->stateBlock->state.stream_output[idx].buffer;
1602 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1603 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1605 struct wined3d_stream_state *stream;
1606 struct wined3d_buffer *prev_buffer;
1608 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1609 device, stream_idx, buffer, offset, stride);
1611 if (stream_idx >= MAX_STREAMS)
1613 WARN("Stream index %u out of range.\n", stream_idx);
1614 return WINED3DERR_INVALIDCALL;
1616 else if (offset & 0x3)
1618 WARN("Offset %u is not 4 byte aligned.\n", offset);
1619 return WINED3DERR_INVALIDCALL;
1622 stream = &device->updateStateBlock->state.streams[stream_idx];
1623 prev_buffer = stream->buffer;
1625 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1627 if (prev_buffer == buffer
1628 && stream->stride == stride
1629 && stream->offset == offset)
1631 TRACE("Application is setting the old values over, nothing to do.\n");
1632 return WINED3D_OK;
1635 stream->buffer = buffer;
1636 if (buffer)
1638 stream->stride = stride;
1639 stream->offset = offset;
1642 /* Handle recording of state blocks. */
1643 if (device->isRecordingState)
1645 TRACE("Recording... not performing anything.\n");
1646 if (buffer)
1647 wined3d_buffer_incref(buffer);
1648 if (prev_buffer)
1649 wined3d_buffer_decref(prev_buffer);
1650 return WINED3D_OK;
1653 if (buffer)
1655 InterlockedIncrement(&buffer->resource.bind_count);
1656 wined3d_buffer_incref(buffer);
1658 if (prev_buffer)
1660 InterlockedDecrement(&prev_buffer->resource.bind_count);
1661 wined3d_buffer_decref(prev_buffer);
1664 device_invalidate_state(device, STATE_STREAMSRC);
1666 return WINED3D_OK;
1669 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1670 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1672 struct wined3d_stream_state *stream;
1674 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1675 device, stream_idx, buffer, offset, stride);
1677 if (stream_idx >= MAX_STREAMS)
1679 WARN("Stream index %u out of range.\n", stream_idx);
1680 return WINED3DERR_INVALIDCALL;
1683 stream = &device->stateBlock->state.streams[stream_idx];
1684 *buffer = stream->buffer;
1685 if (*buffer)
1686 wined3d_buffer_incref(*buffer);
1687 if (offset)
1688 *offset = stream->offset;
1689 *stride = stream->stride;
1691 return WINED3D_OK;
1694 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1696 struct wined3d_stream_state *stream;
1697 UINT old_flags, old_freq;
1699 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1701 /* Verify input. At least in d3d9 this is invalid. */
1702 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1704 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1705 return WINED3DERR_INVALIDCALL;
1707 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1709 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1710 return WINED3DERR_INVALIDCALL;
1712 if (!divider)
1714 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1715 return WINED3DERR_INVALIDCALL;
1718 stream = &device->updateStateBlock->state.streams[stream_idx];
1719 old_flags = stream->flags;
1720 old_freq = stream->frequency;
1722 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1723 stream->frequency = divider & 0x7fffff;
1725 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1727 if (stream->frequency != old_freq || stream->flags != old_flags)
1728 device_invalidate_state(device, STATE_STREAMSRC);
1730 return WINED3D_OK;
1733 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1734 UINT stream_idx, UINT *divider)
1736 struct wined3d_stream_state *stream;
1738 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1740 stream = &device->updateStateBlock->state.streams[stream_idx];
1741 *divider = stream->flags | stream->frequency;
1743 TRACE("Returning %#x.\n", *divider);
1745 return WINED3D_OK;
1748 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1749 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1751 TRACE("device %p, state %s, matrix %p.\n",
1752 device, debug_d3dtstype(d3dts), matrix);
1753 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1754 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1755 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1756 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1758 /* Handle recording of state blocks. */
1759 if (device->isRecordingState)
1761 TRACE("Recording... not performing anything.\n");
1762 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1763 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1764 return;
1767 /* If the new matrix is the same as the current one,
1768 * we cut off any further processing. this seems to be a reasonable
1769 * optimization because as was noticed, some apps (warcraft3 for example)
1770 * tend towards setting the same matrix repeatedly for some reason.
1772 * From here on we assume that the new matrix is different, wherever it matters. */
1773 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1775 TRACE("The application is setting the same matrix over again.\n");
1776 return;
1779 device->stateBlock->state.transforms[d3dts] = *matrix;
1780 if (d3dts == WINED3D_TS_VIEW)
1781 device->view_ident = !memcmp(matrix, &identity, sizeof(identity));
1783 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1784 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1787 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1788 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1790 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1792 *matrix = device->stateBlock->state.transforms[state];
1795 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1796 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1798 const struct wined3d_matrix *mat;
1799 struct wined3d_matrix temp;
1801 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1803 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1804 * below means it will be recorded in a state block change, but it
1805 * works regardless where it is recorded.
1806 * If this is found to be wrong, change to StateBlock. */
1807 if (state > HIGHEST_TRANSFORMSTATE)
1809 WARN("Unhandled transform state %#x.\n", state);
1810 return;
1813 mat = &device->updateStateBlock->state.transforms[state];
1814 multiply_matrix(&temp, mat, matrix);
1816 /* Apply change via set transform - will reapply to eg. lights this way. */
1817 wined3d_device_set_transform(device, state, &temp);
1820 /* Note lights are real special cases. Although the device caps state only
1821 * e.g. 8 are supported, you can reference any indexes you want as long as
1822 * that number max are enabled at any one point in time. Therefore since the
1823 * indices can be anything, we need a hashmap of them. However, this causes
1824 * stateblock problems. When capturing the state block, I duplicate the
1825 * hashmap, but when recording, just build a chain pretty much of commands to
1826 * be replayed. */
1827 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1828 UINT light_idx, const struct wined3d_light *light)
1830 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1831 struct wined3d_light_info *object = NULL;
1832 struct list *e;
1833 float rho;
1835 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1837 /* Check the parameter range. Need for speed most wanted sets junk lights
1838 * which confuse the GL driver. */
1839 if (!light)
1840 return WINED3DERR_INVALIDCALL;
1842 switch (light->type)
1844 case WINED3D_LIGHT_POINT:
1845 case WINED3D_LIGHT_SPOT:
1846 case WINED3D_LIGHT_PARALLELPOINT:
1847 case WINED3D_LIGHT_GLSPOT:
1848 /* Incorrect attenuation values can cause the gl driver to crash.
1849 * Happens with Need for speed most wanted. */
1850 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1852 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1853 return WINED3DERR_INVALIDCALL;
1855 break;
1857 case WINED3D_LIGHT_DIRECTIONAL:
1858 /* Ignores attenuation */
1859 break;
1861 default:
1862 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1863 return WINED3DERR_INVALIDCALL;
1866 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1868 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1869 if (object->OriginalIndex == light_idx)
1870 break;
1871 object = NULL;
1874 if (!object)
1876 TRACE("Adding new light\n");
1877 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1878 if (!object)
1880 ERR("Out of memory error when allocating a light\n");
1881 return E_OUTOFMEMORY;
1883 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1884 object->glIndex = -1;
1885 object->OriginalIndex = light_idx;
1888 /* Initialize the object. */
1889 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1890 light_idx, light->type,
1891 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1892 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1893 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1894 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1895 light->direction.x, light->direction.y, light->direction.z);
1896 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1897 light->range, light->falloff, light->theta, light->phi);
1899 /* Save away the information. */
1900 object->OriginalParms = *light;
1902 switch (light->type)
1904 case WINED3D_LIGHT_POINT:
1905 /* Position */
1906 object->lightPosn[0] = light->position.x;
1907 object->lightPosn[1] = light->position.y;
1908 object->lightPosn[2] = light->position.z;
1909 object->lightPosn[3] = 1.0f;
1910 object->cutoff = 180.0f;
1911 /* FIXME: Range */
1912 break;
1914 case WINED3D_LIGHT_DIRECTIONAL:
1915 /* Direction */
1916 object->lightPosn[0] = -light->direction.x;
1917 object->lightPosn[1] = -light->direction.y;
1918 object->lightPosn[2] = -light->direction.z;
1919 object->lightPosn[3] = 0.0f;
1920 object->exponent = 0.0f;
1921 object->cutoff = 180.0f;
1922 break;
1924 case WINED3D_LIGHT_SPOT:
1925 /* Position */
1926 object->lightPosn[0] = light->position.x;
1927 object->lightPosn[1] = light->position.y;
1928 object->lightPosn[2] = light->position.z;
1929 object->lightPosn[3] = 1.0f;
1931 /* Direction */
1932 object->lightDirn[0] = light->direction.x;
1933 object->lightDirn[1] = light->direction.y;
1934 object->lightDirn[2] = light->direction.z;
1935 object->lightDirn[3] = 1.0f;
1937 /* opengl-ish and d3d-ish spot lights use too different models
1938 * for the light "intensity" as a function of the angle towards
1939 * the main light direction, so we only can approximate very
1940 * roughly. However, spot lights are rather rarely used in games
1941 * (if ever used at all). Furthermore if still used, probably
1942 * nobody pays attention to such details. */
1943 if (!light->falloff)
1945 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1946 * equations have the falloff resp. exponent parameter as an
1947 * exponent, so the spot light lighting will always be 1.0 for
1948 * both of them, and we don't have to care for the rest of the
1949 * rather complex calculation. */
1950 object->exponent = 0.0f;
1952 else
1954 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1955 if (rho < 0.0001f)
1956 rho = 0.0001f;
1957 object->exponent = -0.3f / logf(cosf(rho / 2));
1960 if (object->exponent > 128.0f)
1961 object->exponent = 128.0f;
1963 object->cutoff = (float)(light->phi * 90 / M_PI);
1964 /* FIXME: Range */
1965 break;
1967 default:
1968 FIXME("Unrecognized light type %#x.\n", light->type);
1971 /* Update the live definitions if the light is currently assigned a glIndex. */
1972 if (object->glIndex != -1 && !device->isRecordingState)
1973 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1975 return WINED3D_OK;
1978 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1979 UINT light_idx, struct wined3d_light *light)
1981 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1982 struct wined3d_light_info *light_info = NULL;
1983 struct list *e;
1985 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1987 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1989 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1990 if (light_info->OriginalIndex == light_idx)
1991 break;
1992 light_info = NULL;
1995 if (!light_info)
1997 TRACE("Light information requested but light not defined\n");
1998 return WINED3DERR_INVALIDCALL;
2001 *light = light_info->OriginalParms;
2002 return WINED3D_OK;
2005 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
2007 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2008 struct wined3d_light_info *light_info = NULL;
2009 struct list *e;
2011 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
2013 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2015 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2016 if (light_info->OriginalIndex == light_idx)
2017 break;
2018 light_info = NULL;
2020 TRACE("Found light %p.\n", light_info);
2022 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
2023 if (!light_info)
2025 TRACE("Light enabled requested but light not defined, so defining one!\n");
2026 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
2028 /* Search for it again! Should be fairly quick as near head of list. */
2029 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2031 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2032 if (light_info->OriginalIndex == light_idx)
2033 break;
2034 light_info = NULL;
2036 if (!light_info)
2038 FIXME("Adding default lights has failed dismally\n");
2039 return WINED3DERR_INVALIDCALL;
2043 if (!enable)
2045 if (light_info->glIndex != -1)
2047 if (!device->isRecordingState)
2048 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
2050 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
2051 light_info->glIndex = -1;
2053 else
2055 TRACE("Light already disabled, nothing to do\n");
2057 light_info->enabled = FALSE;
2059 else
2061 light_info->enabled = TRUE;
2062 if (light_info->glIndex != -1)
2064 TRACE("Nothing to do as light was enabled\n");
2066 else
2068 unsigned int i;
2069 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2070 /* Find a free GL light. */
2071 for (i = 0; i < gl_info->limits.lights; ++i)
2073 if (!device->updateStateBlock->state.lights[i])
2075 device->updateStateBlock->state.lights[i] = light_info;
2076 light_info->glIndex = i;
2077 break;
2080 if (light_info->glIndex == -1)
2082 /* Our tests show that Windows returns D3D_OK in this situation, even with
2083 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
2084 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
2085 * as well for those lights.
2087 * TODO: Test how this affects rendering. */
2088 WARN("Too many concurrently active lights\n");
2089 return WINED3D_OK;
2092 /* i == light_info->glIndex */
2093 if (!device->isRecordingState)
2094 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
2098 return WINED3D_OK;
2101 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
2103 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2104 struct wined3d_light_info *light_info = NULL;
2105 struct list *e;
2107 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
2109 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2111 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2112 if (light_info->OriginalIndex == light_idx)
2113 break;
2114 light_info = NULL;
2117 if (!light_info)
2119 TRACE("Light enabled state requested but light not defined.\n");
2120 return WINED3DERR_INVALIDCALL;
2122 /* true is 128 according to SetLightEnable */
2123 *enable = light_info->enabled ? 128 : 0;
2124 return WINED3D_OK;
2127 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
2128 UINT plane_idx, const struct wined3d_vec4 *plane)
2130 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2132 /* Validate plane_idx. */
2133 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2135 TRACE("Application has requested clipplane this device doesn't support.\n");
2136 return WINED3DERR_INVALIDCALL;
2139 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2141 if (!memcmp(&device->updateStateBlock->state.clip_planes[plane_idx], plane, sizeof(*plane)))
2143 TRACE("Application is setting old values over, nothing to do.\n");
2144 return WINED3D_OK;
2147 device->updateStateBlock->state.clip_planes[plane_idx] = *plane;
2149 /* Handle recording of state blocks. */
2150 if (device->isRecordingState)
2152 TRACE("Recording... not performing anything.\n");
2153 return WINED3D_OK;
2156 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2158 return WINED3D_OK;
2161 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
2162 UINT plane_idx, struct wined3d_vec4 *plane)
2164 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2166 /* Validate plane_idx. */
2167 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2169 TRACE("Application has requested clipplane this device doesn't support.\n");
2170 return WINED3DERR_INVALIDCALL;
2173 *plane = device->stateBlock->state.clip_planes[plane_idx];
2175 return WINED3D_OK;
2178 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2179 const struct wined3d_clip_status *clip_status)
2181 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2183 if (!clip_status)
2184 return WINED3DERR_INVALIDCALL;
2186 return WINED3D_OK;
2189 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2190 struct wined3d_clip_status *clip_status)
2192 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2194 if (!clip_status)
2195 return WINED3DERR_INVALIDCALL;
2197 return WINED3D_OK;
2200 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2202 TRACE("device %p, material %p.\n", device, material);
2204 device->updateStateBlock->changed.material = TRUE;
2205 device->updateStateBlock->state.material = *material;
2207 /* Handle recording of state blocks */
2208 if (device->isRecordingState)
2210 TRACE("Recording... not performing anything.\n");
2211 return;
2214 device_invalidate_state(device, STATE_MATERIAL);
2217 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2219 TRACE("device %p, material %p.\n", device, material);
2221 *material = device->updateStateBlock->state.material;
2223 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2224 material->diffuse.r, material->diffuse.g,
2225 material->diffuse.b, material->diffuse.a);
2226 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2227 material->ambient.r, material->ambient.g,
2228 material->ambient.b, material->ambient.a);
2229 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2230 material->specular.r, material->specular.g,
2231 material->specular.b, material->specular.a);
2232 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2233 material->emissive.r, material->emissive.g,
2234 material->emissive.b, material->emissive.a);
2235 TRACE("power %.8e.\n", material->power);
2238 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2239 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2241 struct wined3d_buffer *prev_buffer;
2243 TRACE("device %p, buffer %p, format %s.\n",
2244 device, buffer, debug_d3dformat(format_id));
2246 prev_buffer = device->updateStateBlock->state.index_buffer;
2248 device->updateStateBlock->changed.indices = TRUE;
2249 device->updateStateBlock->state.index_buffer = buffer;
2250 device->updateStateBlock->state.index_format = format_id;
2252 /* Handle recording of state blocks. */
2253 if (device->isRecordingState)
2255 TRACE("Recording... not performing anything.\n");
2256 if (buffer)
2257 wined3d_buffer_incref(buffer);
2258 if (prev_buffer)
2259 wined3d_buffer_decref(prev_buffer);
2260 return;
2263 if (prev_buffer != buffer)
2265 device_invalidate_state(device, STATE_INDEXBUFFER);
2266 if (buffer)
2268 InterlockedIncrement(&buffer->resource.bind_count);
2269 wined3d_buffer_incref(buffer);
2271 if (prev_buffer)
2273 InterlockedDecrement(&prev_buffer->resource.bind_count);
2274 wined3d_buffer_decref(prev_buffer);
2279 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
2280 enum wined3d_format_id *format)
2282 TRACE("device %p, format %p.\n", device, format);
2284 *format = device->stateBlock->state.index_format;
2285 return device->stateBlock->state.index_buffer;
2288 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2290 TRACE("device %p, base_index %d.\n", device, base_index);
2292 device->updateStateBlock->state.base_vertex_index = base_index;
2295 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2297 TRACE("device %p.\n", device);
2299 return device->stateBlock->state.base_vertex_index;
2302 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2304 TRACE("device %p, viewport %p.\n", device, viewport);
2305 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2306 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2308 device->updateStateBlock->changed.viewport = TRUE;
2309 device->updateStateBlock->state.viewport = *viewport;
2311 /* Handle recording of state blocks */
2312 if (device->isRecordingState)
2314 TRACE("Recording... not performing anything\n");
2315 return;
2318 device_invalidate_state(device, STATE_VIEWPORT);
2321 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2323 TRACE("device %p, viewport %p.\n", device, viewport);
2325 *viewport = device->stateBlock->state.viewport;
2328 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2329 enum wined3d_render_state state, DWORD value)
2331 DWORD old_value = device->stateBlock->state.render_states[state];
2333 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2335 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2336 device->updateStateBlock->state.render_states[state] = value;
2338 /* Handle recording of state blocks. */
2339 if (device->isRecordingState)
2341 TRACE("Recording... not performing anything.\n");
2342 return;
2345 /* Compared here and not before the assignment to allow proper stateblock recording. */
2346 if (value == old_value)
2347 TRACE("Application is setting the old value over, nothing to do.\n");
2348 else
2349 device_invalidate_state(device, STATE_RENDER(state));
2352 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2354 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2356 return device->stateBlock->state.render_states[state];
2359 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2360 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2362 DWORD old_value;
2364 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2365 device, sampler_idx, debug_d3dsamplerstate(state), value);
2367 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2368 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2370 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2371 / sizeof(*device->stateBlock->state.sampler_states))
2373 WARN("Invalid sampler %u.\n", sampler_idx);
2374 return; /* Windows accepts overflowing this array ... we do not. */
2377 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2378 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2379 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2381 /* Handle recording of state blocks. */
2382 if (device->isRecordingState)
2384 TRACE("Recording... not performing anything.\n");
2385 return;
2388 if (old_value == value)
2390 TRACE("Application is setting the old value over, nothing to do.\n");
2391 return;
2394 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2397 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2398 UINT sampler_idx, enum wined3d_sampler_state state)
2400 TRACE("device %p, sampler_idx %u, state %s.\n",
2401 device, sampler_idx, debug_d3dsamplerstate(state));
2403 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2404 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2406 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2407 / sizeof(*device->stateBlock->state.sampler_states))
2409 WARN("Invalid sampler %u.\n", sampler_idx);
2410 return 0; /* Windows accepts overflowing this array ... we do not. */
2413 return device->stateBlock->state.sampler_states[sampler_idx][state];
2416 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2418 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2420 device->updateStateBlock->changed.scissorRect = TRUE;
2421 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2423 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2424 return;
2426 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2428 if (device->isRecordingState)
2430 TRACE("Recording... not performing anything.\n");
2431 return;
2434 device_invalidate_state(device, STATE_SCISSORRECT);
2437 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2439 TRACE("device %p, rect %p.\n", device, rect);
2441 *rect = device->updateStateBlock->state.scissor_rect;
2442 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2445 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2446 struct wined3d_vertex_declaration *declaration)
2448 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2450 TRACE("device %p, declaration %p.\n", device, declaration);
2452 if (declaration)
2453 wined3d_vertex_declaration_incref(declaration);
2454 if (prev)
2455 wined3d_vertex_declaration_decref(prev);
2457 device->updateStateBlock->state.vertex_declaration = declaration;
2458 device->updateStateBlock->changed.vertexDecl = TRUE;
2460 if (device->isRecordingState)
2462 TRACE("Recording... not performing anything.\n");
2463 return;
2466 if (declaration == prev)
2468 /* Checked after the assignment to allow proper stateblock recording. */
2469 TRACE("Application is setting the old declaration over, nothing to do.\n");
2470 return;
2473 device_invalidate_state(device, STATE_VDECL);
2476 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2478 TRACE("device %p.\n", device);
2480 return device->stateBlock->state.vertex_declaration;
2483 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2485 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2487 TRACE("device %p, shader %p.\n", device, shader);
2489 if (shader)
2490 wined3d_shader_incref(shader);
2491 if (prev)
2492 wined3d_shader_decref(prev);
2494 device->updateStateBlock->state.vertex_shader = shader;
2495 device->updateStateBlock->changed.vertexShader = TRUE;
2497 if (device->isRecordingState)
2499 TRACE("Recording... not performing anything.\n");
2500 return;
2503 if (shader == prev)
2505 TRACE("Application is setting the old shader over, nothing to do.\n");
2506 return;
2509 device_invalidate_state(device, STATE_VSHADER);
2512 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2514 TRACE("device %p.\n", device);
2516 return device->stateBlock->state.vertex_shader;
2519 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2521 struct wined3d_buffer *prev;
2523 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2525 if (idx >= MAX_CONSTANT_BUFFERS)
2527 WARN("Invalid constant buffer index %u.\n", idx);
2528 return;
2531 prev = device->updateStateBlock->state.vs_cb[idx];
2532 device->updateStateBlock->state.vs_cb[idx] = buffer;
2534 if (device->isRecordingState)
2536 if (buffer)
2537 wined3d_buffer_incref(buffer);
2538 if (prev)
2539 wined3d_buffer_decref(prev);
2540 return;
2543 if (prev != buffer)
2545 if (buffer)
2547 InterlockedIncrement(&buffer->resource.bind_count);
2548 wined3d_buffer_incref(buffer);
2550 if (prev)
2552 InterlockedDecrement(&prev->resource.bind_count);
2553 wined3d_buffer_decref(prev);
2558 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2560 TRACE("device %p, idx %u.\n", device, idx);
2562 if (idx >= MAX_CONSTANT_BUFFERS)
2564 WARN("Invalid constant buffer index %u.\n", idx);
2565 return NULL;
2568 return device->stateBlock->state.vs_cb[idx];
2571 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2573 struct wined3d_sampler *prev;
2575 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2577 if (idx >= MAX_SAMPLER_OBJECTS)
2579 WARN("Invalid sampler index %u.\n", idx);
2580 return;
2583 prev = device->updateStateBlock->state.vs_sampler[idx];
2584 device->updateStateBlock->state.vs_sampler[idx] = sampler;
2586 if (sampler)
2587 wined3d_sampler_incref(sampler);
2588 if (prev)
2589 wined3d_sampler_decref(prev);
2592 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2594 TRACE("device %p, idx %u.\n", device, idx);
2596 if (idx >= MAX_SAMPLER_OBJECTS)
2598 WARN("Invalid sampler index %u.\n", idx);
2599 return NULL;
2602 return device->stateBlock->state.vs_sampler[idx];
2605 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2606 UINT start_register, const BOOL *constants, UINT bool_count)
2608 UINT count = min(bool_count, MAX_CONST_B - start_register);
2609 UINT i;
2611 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2612 device, start_register, constants, bool_count);
2614 if (!constants || start_register >= MAX_CONST_B)
2615 return WINED3DERR_INVALIDCALL;
2617 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2618 for (i = 0; i < count; ++i)
2619 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2621 for (i = start_register; i < count + start_register; ++i)
2622 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2624 if (!device->isRecordingState)
2625 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2627 return WINED3D_OK;
2630 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2631 UINT start_register, BOOL *constants, UINT bool_count)
2633 UINT count = min(bool_count, MAX_CONST_B - start_register);
2635 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2636 device, start_register, constants, bool_count);
2638 if (!constants || start_register >= MAX_CONST_B)
2639 return WINED3DERR_INVALIDCALL;
2641 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2643 return WINED3D_OK;
2646 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2647 UINT start_register, const int *constants, UINT vector4i_count)
2649 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2650 UINT i;
2652 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2653 device, start_register, constants, vector4i_count);
2655 if (!constants || start_register >= MAX_CONST_I)
2656 return WINED3DERR_INVALIDCALL;
2658 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2659 for (i = 0; i < count; ++i)
2660 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2661 constants[i * 4], constants[i * 4 + 1],
2662 constants[i * 4 + 2], constants[i * 4 + 3]);
2664 for (i = start_register; i < count + start_register; ++i)
2665 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2667 if (!device->isRecordingState)
2668 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2670 return WINED3D_OK;
2673 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2674 UINT start_register, int *constants, UINT vector4i_count)
2676 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2678 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2679 device, start_register, constants, vector4i_count);
2681 if (!constants || start_register >= MAX_CONST_I)
2682 return WINED3DERR_INVALIDCALL;
2684 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2685 return WINED3D_OK;
2688 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2689 UINT start_register, const float *constants, UINT vector4f_count)
2691 UINT i;
2693 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2694 device, start_register, constants, vector4f_count);
2696 /* Specifically test start_register > limit to catch MAX_UINT overflows
2697 * when adding start_register + vector4f_count. */
2698 if (!constants
2699 || start_register + vector4f_count > device->d3d_vshader_constantF
2700 || start_register > device->d3d_vshader_constantF)
2701 return WINED3DERR_INVALIDCALL;
2703 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2704 constants, vector4f_count * sizeof(float) * 4);
2705 if (TRACE_ON(d3d))
2707 for (i = 0; i < vector4f_count; ++i)
2708 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2709 constants[i * 4], constants[i * 4 + 1],
2710 constants[i * 4 + 2], constants[i * 4 + 3]);
2713 if (!device->isRecordingState)
2715 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2716 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2719 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2720 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2722 return WINED3D_OK;
2725 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2726 UINT start_register, float *constants, UINT vector4f_count)
2728 int count = min(vector4f_count, device->d3d_vshader_constantF - start_register);
2730 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2731 device, start_register, constants, vector4f_count);
2733 if (!constants || count < 0)
2734 return WINED3DERR_INVALIDCALL;
2736 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2738 return WINED3D_OK;
2741 static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2743 DWORD i;
2745 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2747 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2751 static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2753 DWORD i = device->rev_tex_unit_map[unit];
2754 DWORD j = device->texUnitMap[stage];
2756 device->texUnitMap[stage] = unit;
2757 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2758 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2760 device->rev_tex_unit_map[unit] = stage;
2761 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2762 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2765 static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2767 UINT i;
2769 device->fixed_function_usage_map = 0;
2770 for (i = 0; i < MAX_TEXTURES; ++i)
2772 const struct wined3d_state *state = &device->stateBlock->state;
2773 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2774 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2775 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2776 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2777 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2778 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2779 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2780 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2782 /* Not used, and disable higher stages. */
2783 if (color_op == WINED3D_TOP_DISABLE)
2784 break;
2786 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2787 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2788 || ((color_arg3 == WINED3DTA_TEXTURE)
2789 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2790 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2791 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2792 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2793 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2794 device->fixed_function_usage_map |= (1 << i);
2796 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2797 && i < MAX_TEXTURES - 1)
2798 device->fixed_function_usage_map |= (1 << (i + 1));
2802 static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2804 unsigned int i, tex;
2805 WORD ffu_map;
2807 device_update_fixed_function_usage_map(device);
2808 ffu_map = device->fixed_function_usage_map;
2810 if (device->max_ffp_textures == gl_info->limits.texture_stages
2811 || device->stateBlock->state.lowest_disabled_stage <= device->max_ffp_textures)
2813 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2815 if (!(ffu_map & 1)) continue;
2817 if (device->texUnitMap[i] != i)
2819 device_map_stage(device, i, i);
2820 device_invalidate_state(device, STATE_SAMPLER(i));
2821 device_invalidate_texture_stage(device, i);
2824 return;
2827 /* Now work out the mapping */
2828 tex = 0;
2829 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2831 if (!(ffu_map & 1)) continue;
2833 if (device->texUnitMap[i] != tex)
2835 device_map_stage(device, i, tex);
2836 device_invalidate_state(device, STATE_SAMPLER(i));
2837 device_invalidate_texture_stage(device, i);
2840 ++tex;
2844 static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2846 const enum wined3d_sampler_texture_type *sampler_type =
2847 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2848 unsigned int i;
2850 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2852 if (sampler_type[i] && device->texUnitMap[i] != i)
2854 device_map_stage(device, i, i);
2855 device_invalidate_state(device, STATE_SAMPLER(i));
2856 if (i < gl_info->limits.texture_stages)
2857 device_invalidate_texture_stage(device, i);
2862 static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2863 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2864 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2866 DWORD current_mapping = device->rev_tex_unit_map[unit];
2868 /* Not currently used */
2869 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2871 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2872 /* Used by a fragment sampler */
2874 if (!pshader_sampler_tokens) {
2875 /* No pixel shader, check fixed function */
2876 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2879 /* Pixel shader, check the shader's sampler map */
2880 return !pshader_sampler_tokens[current_mapping];
2883 /* Used by a vertex sampler */
2884 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2887 static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2889 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2890 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2891 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2892 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2893 int i;
2895 if (ps)
2897 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2898 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2899 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2902 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2903 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2904 if (vshader_sampler_type[i])
2906 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2908 /* Already mapped somewhere */
2909 continue;
2912 while (start >= 0)
2914 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2916 device_map_stage(device, vsampler_idx, start);
2917 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2919 --start;
2920 break;
2923 --start;
2929 void device_update_tex_unit_map(struct wined3d_device *device)
2931 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2932 const struct wined3d_state *state = &device->stateBlock->state;
2933 BOOL vs = use_vs(state);
2934 BOOL ps = use_ps(state);
2936 * Rules are:
2937 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2938 * that would be really messy and require shader recompilation
2939 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2940 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2942 if (ps)
2943 device_map_psamplers(device, gl_info);
2944 else
2945 device_map_fixed_function_samplers(device, gl_info);
2947 if (vs)
2948 device_map_vsamplers(device, ps, gl_info);
2951 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2953 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2955 TRACE("device %p, shader %p.\n", device, shader);
2957 if (shader)
2958 wined3d_shader_incref(shader);
2959 if (prev)
2960 wined3d_shader_decref(prev);
2962 device->updateStateBlock->state.pixel_shader = shader;
2963 device->updateStateBlock->changed.pixelShader = TRUE;
2965 if (device->isRecordingState)
2967 TRACE("Recording... not performing anything.\n");
2968 return;
2971 if (shader == prev)
2973 TRACE("Application is setting the old shader over, nothing to do.\n");
2974 return;
2977 device_invalidate_state(device, STATE_PIXELSHADER);
2980 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2982 TRACE("device %p.\n", device);
2984 return device->stateBlock->state.pixel_shader;
2987 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2989 struct wined3d_buffer *prev;
2991 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2993 if (idx >= MAX_CONSTANT_BUFFERS)
2995 WARN("Invalid constant buffer index %u.\n", idx);
2996 return;
2999 prev = device->updateStateBlock->state.ps_cb[idx];
3000 device->updateStateBlock->state.ps_cb[idx] = buffer;
3002 if (device->isRecordingState)
3004 if (buffer)
3005 wined3d_buffer_incref(buffer);
3006 if (prev)
3007 wined3d_buffer_decref(prev);
3008 return;
3011 if (prev != buffer)
3013 if (buffer)
3015 InterlockedIncrement(&buffer->resource.bind_count);
3016 wined3d_buffer_incref(buffer);
3018 if (prev)
3020 InterlockedDecrement(&prev->resource.bind_count);
3021 wined3d_buffer_decref(prev);
3026 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
3028 TRACE("device %p, idx %u.\n", device, idx);
3030 if (idx >= MAX_CONSTANT_BUFFERS)
3032 WARN("Invalid constant buffer index %u.\n", idx);
3033 return NULL;
3036 return device->stateBlock->state.ps_cb[idx];
3039 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3041 struct wined3d_sampler *prev;
3043 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3045 if (idx >= MAX_SAMPLER_OBJECTS)
3047 WARN("Invalid sampler index %u.\n", idx);
3048 return;
3051 prev = device->updateStateBlock->state.ps_sampler[idx];
3052 device->updateStateBlock->state.ps_sampler[idx] = sampler;
3054 if (sampler)
3055 wined3d_sampler_incref(sampler);
3056 if (prev)
3057 wined3d_sampler_decref(prev);
3060 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
3062 TRACE("device %p, idx %u.\n", device, idx);
3064 if (idx >= MAX_SAMPLER_OBJECTS)
3066 WARN("Invalid sampler index %u.\n", idx);
3067 return NULL;
3070 return device->stateBlock->state.ps_sampler[idx];
3073 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
3074 UINT start_register, const BOOL *constants, UINT bool_count)
3076 UINT count = min(bool_count, MAX_CONST_B - start_register);
3077 UINT i;
3079 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3080 device, start_register, constants, bool_count);
3082 if (!constants || start_register >= MAX_CONST_B)
3083 return WINED3DERR_INVALIDCALL;
3085 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
3086 for (i = 0; i < count; ++i)
3087 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
3089 for (i = start_register; i < count + start_register; ++i)
3090 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
3092 if (!device->isRecordingState)
3093 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3095 return WINED3D_OK;
3098 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
3099 UINT start_register, BOOL *constants, UINT bool_count)
3101 UINT count = min(bool_count, MAX_CONST_B - start_register);
3103 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3104 device, start_register, constants, bool_count);
3106 if (!constants || start_register >= MAX_CONST_B)
3107 return WINED3DERR_INVALIDCALL;
3109 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
3111 return WINED3D_OK;
3114 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
3115 UINT start_register, const int *constants, UINT vector4i_count)
3117 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3118 UINT i;
3120 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3121 device, start_register, constants, vector4i_count);
3123 if (!constants || start_register >= MAX_CONST_I)
3124 return WINED3DERR_INVALIDCALL;
3126 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
3127 for (i = 0; i < count; ++i)
3128 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
3129 constants[i * 4], constants[i * 4 + 1],
3130 constants[i * 4 + 2], constants[i * 4 + 3]);
3132 for (i = start_register; i < count + start_register; ++i)
3133 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
3135 if (!device->isRecordingState)
3136 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3138 return WINED3D_OK;
3141 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
3142 UINT start_register, int *constants, UINT vector4i_count)
3144 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3146 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3147 device, start_register, constants, vector4i_count);
3149 if (!constants || start_register >= MAX_CONST_I)
3150 return WINED3DERR_INVALIDCALL;
3152 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
3154 return WINED3D_OK;
3157 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
3158 UINT start_register, const float *constants, UINT vector4f_count)
3160 UINT i;
3162 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3163 device, start_register, constants, vector4f_count);
3165 /* Specifically test start_register > limit to catch MAX_UINT overflows
3166 * when adding start_register + vector4f_count. */
3167 if (!constants
3168 || start_register + vector4f_count > device->d3d_pshader_constantF
3169 || start_register > device->d3d_pshader_constantF)
3170 return WINED3DERR_INVALIDCALL;
3172 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
3173 constants, vector4f_count * sizeof(float) * 4);
3174 if (TRACE_ON(d3d))
3176 for (i = 0; i < vector4f_count; ++i)
3177 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
3178 constants[i * 4], constants[i * 4 + 1],
3179 constants[i * 4 + 2], constants[i * 4 + 3]);
3182 if (!device->isRecordingState)
3184 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
3185 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3188 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
3189 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
3191 return WINED3D_OK;
3194 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
3195 UINT start_register, float *constants, UINT vector4f_count)
3197 int count = min(vector4f_count, device->d3d_pshader_constantF - start_register);
3199 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3200 device, start_register, constants, vector4f_count);
3202 if (!constants || count < 0)
3203 return WINED3DERR_INVALIDCALL;
3205 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
3207 return WINED3D_OK;
3210 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
3212 struct wined3d_shader *prev = device->updateStateBlock->state.geometry_shader;
3214 TRACE("device %p, shader %p.\n", device, shader);
3216 if (shader)
3217 wined3d_shader_incref(shader);
3218 if (prev)
3219 wined3d_shader_decref(prev);
3221 device->updateStateBlock->state.geometry_shader = shader;
3223 if (device->isRecordingState || shader == prev)
3224 return;
3226 device_invalidate_state(device, STATE_GEOMETRY_SHADER);
3229 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
3231 TRACE("device %p.\n", device);
3233 return device->stateBlock->state.geometry_shader;
3236 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
3238 struct wined3d_buffer *prev;
3240 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
3242 if (idx >= MAX_CONSTANT_BUFFERS)
3244 WARN("Invalid constant buffer index %u.\n", idx);
3245 return;
3248 prev = device->updateStateBlock->state.gs_cb[idx];
3249 device->updateStateBlock->state.gs_cb[idx] = buffer;
3251 if (device->isRecordingState)
3253 if (buffer)
3254 wined3d_buffer_incref(buffer);
3255 if (prev)
3256 wined3d_buffer_decref(prev);
3257 return;
3260 if (prev != buffer)
3262 if (buffer)
3264 InterlockedIncrement(&buffer->resource.bind_count);
3265 wined3d_buffer_incref(buffer);
3267 if (prev)
3269 InterlockedDecrement(&prev->resource.bind_count);
3270 wined3d_buffer_decref(prev);
3275 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
3277 TRACE("device %p, idx %u.\n", device, idx);
3279 if (idx >= MAX_CONSTANT_BUFFERS)
3281 WARN("Invalid constant buffer index %u.\n", idx);
3282 return NULL;
3285 return device->stateBlock->state.gs_cb[idx];
3288 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3290 struct wined3d_sampler *prev;
3292 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3294 if (idx >= MAX_SAMPLER_OBJECTS)
3296 WARN("Invalid sampler index %u.\n", idx);
3297 return;
3300 prev = device->updateStateBlock->state.gs_sampler[idx];
3301 device->updateStateBlock->state.gs_sampler[idx] = sampler;
3303 if (sampler)
3304 wined3d_sampler_incref(sampler);
3305 if (prev)
3306 wined3d_sampler_decref(prev);
3309 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
3311 TRACE("device %p, idx %u.\n", device, idx);
3313 if (idx >= MAX_SAMPLER_OBJECTS)
3315 WARN("Invalid sampler index %u.\n", idx);
3316 return NULL;
3319 return device->stateBlock->state.gs_sampler[idx];
3322 /* Context activation is done by the caller. */
3323 /* Do not call while under the GL lock. */
3324 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3325 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3326 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3327 DWORD DestFVF)
3329 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3330 struct wined3d_viewport vp;
3331 UINT vertex_size;
3332 unsigned int i;
3333 BYTE *dest_ptr;
3334 BOOL doClip;
3335 DWORD numTextures;
3336 HRESULT hr;
3338 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3340 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3343 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3345 ERR("Source has no position mask\n");
3346 return WINED3DERR_INVALIDCALL;
3349 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3351 static BOOL warned = FALSE;
3353 * The clipping code is not quite correct. Some things need
3354 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3355 * so disable clipping for now.
3356 * (The graphics in Half-Life are broken, and my processvertices
3357 * test crashes with IDirect3DDevice3)
3358 doClip = TRUE;
3360 doClip = FALSE;
3361 if(!warned) {
3362 warned = TRUE;
3363 FIXME("Clipping is broken and disabled for now\n");
3366 else
3367 doClip = FALSE;
3369 vertex_size = get_flexible_vertex_size(DestFVF);
3370 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3372 WARN("Failed to map buffer, hr %#x.\n", hr);
3373 return hr;
3376 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3377 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3378 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3380 TRACE("View mat:\n");
3381 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);
3382 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);
3383 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);
3384 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);
3386 TRACE("Proj mat:\n");
3387 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);
3388 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);
3389 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);
3390 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);
3392 TRACE("World mat:\n");
3393 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);
3394 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);
3395 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);
3396 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);
3398 /* Get the viewport */
3399 wined3d_device_get_viewport(device, &vp);
3400 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3401 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3403 multiply_matrix(&mat,&view_mat,&world_mat);
3404 multiply_matrix(&mat,&proj_mat,&mat);
3406 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3408 for (i = 0; i < dwCount; i+= 1) {
3409 unsigned int tex_index;
3411 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3412 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3413 /* The position first */
3414 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3415 const float *p = (const float *)(element->data.addr + i * element->stride);
3416 float x, y, z, rhw;
3417 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3419 /* Multiplication with world, view and projection matrix */
3420 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);
3421 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);
3422 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);
3423 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);
3425 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3427 /* WARNING: The following things are taken from d3d7 and were not yet checked
3428 * against d3d8 or d3d9!
3431 /* Clipping conditions: From msdn
3433 * A vertex is clipped if it does not match the following requirements
3434 * -rhw < x <= rhw
3435 * -rhw < y <= rhw
3436 * 0 < z <= rhw
3437 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3439 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3440 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3444 if( !doClip ||
3445 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3446 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3447 ( rhw > eps ) ) ) {
3449 /* "Normal" viewport transformation (not clipped)
3450 * 1) The values are divided by rhw
3451 * 2) The y axis is negative, so multiply it with -1
3452 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3453 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3454 * 4) Multiply x with Width/2 and add Width/2
3455 * 5) The same for the height
3456 * 6) Add the viewpoint X and Y to the 2D coordinates and
3457 * The minimum Z value to z
3458 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3460 * Well, basically it's simply a linear transformation into viewport
3461 * coordinates
3464 x /= rhw;
3465 y /= rhw;
3466 z /= rhw;
3468 y *= -1;
3470 x *= vp.width / 2;
3471 y *= vp.height / 2;
3472 z *= vp.max_z - vp.min_z;
3474 x += vp.width / 2 + vp.x;
3475 y += vp.height / 2 + vp.y;
3476 z += vp.min_z;
3478 rhw = 1 / rhw;
3479 } else {
3480 /* That vertex got clipped
3481 * Contrary to OpenGL it is not dropped completely, it just
3482 * undergoes a different calculation.
3484 TRACE("Vertex got clipped\n");
3485 x += rhw;
3486 y += rhw;
3488 x /= 2;
3489 y /= 2;
3491 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3492 * outside of the main vertex buffer memory. That needs some more
3493 * investigation...
3497 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3500 ( (float *) dest_ptr)[0] = x;
3501 ( (float *) dest_ptr)[1] = y;
3502 ( (float *) dest_ptr)[2] = z;
3503 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3505 dest_ptr += 3 * sizeof(float);
3507 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3508 dest_ptr += sizeof(float);
3511 if (DestFVF & WINED3DFVF_PSIZE)
3512 dest_ptr += sizeof(DWORD);
3514 if (DestFVF & WINED3DFVF_NORMAL)
3516 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3517 const float *normal = (const float *)(element->data.addr + i * element->stride);
3518 /* AFAIK this should go into the lighting information */
3519 FIXME("Didn't expect the destination to have a normal\n");
3520 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3523 if (DestFVF & WINED3DFVF_DIFFUSE)
3525 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3526 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3527 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3529 static BOOL warned = FALSE;
3531 if(!warned) {
3532 ERR("No diffuse color in source, but destination has one\n");
3533 warned = TRUE;
3536 *( (DWORD *) dest_ptr) = 0xffffffff;
3537 dest_ptr += sizeof(DWORD);
3539 else
3541 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3545 if (DestFVF & WINED3DFVF_SPECULAR)
3547 /* What's the color value in the feedback buffer? */
3548 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3549 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3550 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3552 static BOOL warned = FALSE;
3554 if(!warned) {
3555 ERR("No specular color in source, but destination has one\n");
3556 warned = TRUE;
3559 *(DWORD *)dest_ptr = 0xff000000;
3560 dest_ptr += sizeof(DWORD);
3562 else
3564 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3568 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3570 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3571 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3572 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3574 ERR("No source texture, but destination requests one\n");
3575 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3577 else
3579 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3584 wined3d_buffer_unmap(dest);
3586 return WINED3D_OK;
3588 #undef copy_and_next
3590 /* Do not call while under the GL lock. */
3591 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3592 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3593 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3595 struct wined3d_state *state = &device->stateBlock->state;
3596 struct wined3d_stream_info stream_info;
3597 const struct wined3d_gl_info *gl_info;
3598 BOOL streamWasUP = state->user_stream;
3599 struct wined3d_context *context;
3600 struct wined3d_shader *vs;
3601 unsigned int i;
3602 HRESULT hr;
3604 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3605 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3606 device, src_start_idx, dst_idx, vertex_count,
3607 dst_buffer, declaration, flags, dst_fvf);
3609 if (declaration)
3610 FIXME("Output vertex declaration not implemented yet.\n");
3612 /* Need any context to write to the vbo. */
3613 context = context_acquire(device, NULL);
3614 gl_info = context->gl_info;
3616 /* ProcessVertices reads from vertex buffers, which have to be assigned.
3617 * DrawPrimitive and DrawPrimitiveUP control the streamIsUP flag, thus
3618 * restore it afterwards. */
3619 vs = state->vertex_shader;
3620 state->vertex_shader = NULL;
3621 state->user_stream = FALSE;
3622 device_stream_info_from_declaration(device, &stream_info);
3623 state->user_stream = streamWasUP;
3624 state->vertex_shader = vs;
3626 /* We can't convert FROM a VBO, and vertex buffers used to source into
3627 * process_vertices() are unlikely to ever be used for drawing. Release
3628 * VBOs in those buffers and fix up the stream_info structure.
3630 * Also apply the start index. */
3631 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3633 struct wined3d_stream_info_element *e;
3635 if (!(stream_info.use_map & (1 << i)))
3636 continue;
3638 e = &stream_info.elements[i];
3639 if (e->data.buffer_object)
3641 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3642 e->data.buffer_object = 0;
3643 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3644 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3645 vb->buffer_object = 0;
3647 if (e->data.addr)
3648 e->data.addr += e->stride * src_start_idx;
3651 hr = process_vertices_strided(device, dst_idx, vertex_count,
3652 &stream_info, dst_buffer, flags, dst_fvf);
3654 context_release(context);
3656 return hr;
3659 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3660 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3662 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3663 DWORD old_value;
3665 TRACE("device %p, stage %u, state %s, value %#x.\n",
3666 device, stage, debug_d3dtexturestate(state), value);
3668 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3670 WARN("Invalid state %#x passed.\n", state);
3671 return;
3674 if (stage >= gl_info->limits.texture_stages)
3676 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3677 stage, gl_info->limits.texture_stages - 1);
3678 return;
3681 old_value = device->updateStateBlock->state.texture_states[stage][state];
3682 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3683 device->updateStateBlock->state.texture_states[stage][state] = value;
3685 if (device->isRecordingState)
3687 TRACE("Recording... not performing anything.\n");
3688 return;
3691 /* Checked after the assignments to allow proper stateblock recording. */
3692 if (old_value == value)
3694 TRACE("Application is setting the old value over, nothing to do.\n");
3695 return;
3698 if (stage > device->stateBlock->state.lowest_disabled_stage
3699 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3700 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3702 /* Colorop change above lowest disabled stage? That won't change
3703 * anything in the GL setup. Changes in other states are important on
3704 * disabled stages too. */
3705 return;
3708 if (state == WINED3D_TSS_COLOR_OP)
3710 unsigned int i;
3712 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3714 /* Previously enabled stage disabled now. Make sure to dirtify
3715 * all enabled stages above stage, they have to be disabled.
3717 * The current stage is dirtified below. */
3718 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3720 TRACE("Additionally dirtifying stage %u.\n", i);
3721 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3723 device->stateBlock->state.lowest_disabled_stage = stage;
3724 TRACE("New lowest disabled: %u.\n", stage);
3726 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3728 /* Previously disabled stage enabled. Stages above it may need
3729 * enabling. Stage must be lowest_disabled_stage here, if it's
3730 * bigger success is returned above, and stages below the lowest
3731 * disabled stage can't be enabled (because they are enabled
3732 * already).
3734 * Again stage stage doesn't need to be dirtified here, it is
3735 * handled below. */
3736 for (i = stage + 1; i < gl_info->limits.texture_stages; ++i)
3738 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3739 break;
3740 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3741 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3743 device->stateBlock->state.lowest_disabled_stage = i;
3744 TRACE("New lowest disabled: %u.\n", i);
3748 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3751 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3752 UINT stage, enum wined3d_texture_stage_state state)
3754 TRACE("device %p, stage %u, state %s.\n",
3755 device, stage, debug_d3dtexturestate(state));
3757 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3759 WARN("Invalid state %#x passed.\n", state);
3760 return 0;
3763 return device->updateStateBlock->state.texture_states[stage][state];
3766 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3767 UINT stage, struct wined3d_texture *texture)
3769 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3770 struct wined3d_texture *prev;
3772 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3774 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3775 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3777 /* Windows accepts overflowing this array... we do not. */
3778 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3780 WARN("Ignoring invalid stage %u.\n", stage);
3781 return WINED3D_OK;
3784 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3786 WARN("Rejecting attempt to set scratch texture.\n");
3787 return WINED3DERR_INVALIDCALL;
3790 device->updateStateBlock->changed.textures |= 1 << stage;
3792 prev = device->updateStateBlock->state.textures[stage];
3793 TRACE("Previous texture %p.\n", prev);
3795 if (texture == prev)
3797 TRACE("App is setting the same texture again, nothing to do.\n");
3798 return WINED3D_OK;
3801 TRACE("Setting new texture to %p.\n", texture);
3802 device->updateStateBlock->state.textures[stage] = texture;
3804 if (device->isRecordingState)
3806 TRACE("Recording... not performing anything\n");
3808 if (texture) wined3d_texture_incref(texture);
3809 if (prev) wined3d_texture_decref(prev);
3811 return WINED3D_OK;
3814 if (texture)
3816 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3818 wined3d_texture_incref(texture);
3820 if (!prev || texture->target != prev->target)
3821 device_invalidate_state(device, STATE_PIXELSHADER);
3823 if (!prev && stage < gl_info->limits.texture_stages)
3825 /* The source arguments for color and alpha ops have different
3826 * meanings when a NULL texture is bound, so the COLOR_OP and
3827 * ALPHA_OP have to be dirtified. */
3828 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3829 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3832 if (bind_count == 1)
3833 texture->sampler = stage;
3836 if (prev)
3838 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3840 if (!texture && stage < gl_info->limits.texture_stages)
3842 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3843 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3846 if (bind_count && prev->sampler == stage)
3848 unsigned int i;
3850 /* Search for other stages the texture is bound to. Shouldn't
3851 * happen if applications bind textures to a single stage only. */
3852 TRACE("Searching for other stages the texture is bound to.\n");
3853 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3855 if (device->updateStateBlock->state.textures[i] == prev)
3857 TRACE("Texture is also bound to stage %u.\n", i);
3858 prev->sampler = i;
3859 break;
3864 wined3d_texture_decref(prev);
3867 device_invalidate_state(device, STATE_SAMPLER(stage));
3869 return WINED3D_OK;
3872 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3874 TRACE("device %p, stage %u.\n", device, stage);
3876 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3877 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3879 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3881 WARN("Ignoring invalid stage %u.\n", stage);
3882 return NULL; /* Windows accepts overflowing this array ... we do not. */
3885 return device->stateBlock->state.textures[stage];
3888 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3889 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3891 struct wined3d_swapchain *swapchain;
3893 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3894 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3896 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3897 return WINED3DERR_INVALIDCALL;
3899 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3900 return WINED3DERR_INVALIDCALL;
3901 return WINED3D_OK;
3904 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3906 TRACE("device %p, caps %p.\n", device, caps);
3908 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3909 device->create_parms.device_type, caps);
3912 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3913 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3915 struct wined3d_swapchain *swapchain;
3917 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3918 device, swapchain_idx, mode, rotation);
3920 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3921 return WINED3DERR_INVALIDCALL;
3923 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3926 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3928 struct wined3d_stateblock *stateblock;
3929 HRESULT hr;
3931 TRACE("device %p.\n", device);
3933 if (device->isRecordingState)
3934 return WINED3DERR_INVALIDCALL;
3936 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3937 if (FAILED(hr))
3938 return hr;
3940 wined3d_stateblock_decref(device->updateStateBlock);
3941 device->updateStateBlock = stateblock;
3942 device->isRecordingState = TRUE;
3944 TRACE("Recording stateblock %p.\n", stateblock);
3946 return WINED3D_OK;
3949 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3950 struct wined3d_stateblock **stateblock)
3952 struct wined3d_stateblock *object = device->updateStateBlock;
3954 TRACE("device %p, stateblock %p.\n", device, stateblock);
3956 if (!device->isRecordingState)
3958 WARN("Not recording.\n");
3959 *stateblock = NULL;
3960 return WINED3DERR_INVALIDCALL;
3963 stateblock_init_contained_states(object);
3965 *stateblock = object;
3966 device->isRecordingState = FALSE;
3967 device->updateStateBlock = device->stateBlock;
3968 wined3d_stateblock_incref(device->updateStateBlock);
3970 TRACE("Returning stateblock %p.\n", *stateblock);
3972 return WINED3D_OK;
3975 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3977 /* At the moment we have no need for any functionality at the beginning
3978 * of a scene. */
3979 TRACE("device %p.\n", device);
3981 if (device->inScene)
3983 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3984 return WINED3DERR_INVALIDCALL;
3986 device->inScene = TRUE;
3987 return WINED3D_OK;
3990 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3992 struct wined3d_context *context;
3994 TRACE("device %p.\n", device);
3996 if (!device->inScene)
3998 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3999 return WINED3DERR_INVALIDCALL;
4002 context = context_acquire(device, NULL);
4003 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
4004 context->gl_info->gl_ops.gl.p_glFlush();
4005 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
4006 * fails. */
4007 context_release(context);
4009 device->inScene = FALSE;
4010 return WINED3D_OK;
4013 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
4014 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
4016 UINT i;
4018 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
4019 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4020 dst_window_override, dirty_region, flags);
4022 for (i = 0; i < device->swapchain_count; ++i)
4024 wined3d_swapchain_present(device->swapchains[i], src_rect,
4025 dst_rect, dst_window_override, dirty_region, flags);
4028 return WINED3D_OK;
4031 /* Do not call while under the GL lock. */
4032 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4033 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4035 RECT draw_rect;
4037 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
4038 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
4040 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4042 struct wined3d_surface *ds = device->fb.depth_stencil;
4043 if (!ds)
4045 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4046 /* TODO: What about depth stencil buffers without stencil bits? */
4047 return WINED3DERR_INVALIDCALL;
4049 else if (flags & WINED3DCLEAR_TARGET)
4051 if (ds->resource.width < device->fb.render_targets[0]->resource.width
4052 || ds->resource.height < device->fb.render_targets[0]->resource.height)
4054 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4055 return WINED3D_OK;
4060 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
4061 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
4062 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
4064 return WINED3D_OK;
4067 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
4068 enum wined3d_primitive_type primitive_type)
4070 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
4072 device->updateStateBlock->changed.primitive_type = TRUE;
4073 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4076 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
4077 enum wined3d_primitive_type *primitive_type)
4079 TRACE("device %p, primitive_type %p\n", device, primitive_type);
4081 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
4083 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
4086 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4088 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4090 if (!device->stateBlock->state.vertex_declaration)
4092 WARN("Called without a valid vertex declaration set.\n");
4093 return WINED3DERR_INVALIDCALL;
4096 /* The index buffer is not needed here, but restore it, otherwise it is hell to keep track of */
4097 if (device->stateBlock->state.user_stream)
4099 device_invalidate_state(device, STATE_INDEXBUFFER);
4100 device->stateBlock->state.user_stream = FALSE;
4103 if (device->stateBlock->state.load_base_vertex_index)
4105 device->stateBlock->state.load_base_vertex_index = 0;
4106 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4109 /* Account for the loading offset due to index buffers. Instead of
4110 * reloading all sources correct it with the startvertex parameter. */
4111 draw_primitive(device, start_vertex, vertex_count, 0, 0, FALSE, NULL);
4112 return WINED3D_OK;
4115 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4117 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4119 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4121 if (!device->stateBlock->state.index_buffer)
4123 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4124 * without an index buffer set. (The first time at least...)
4125 * D3D8 simply dies, but I doubt it can do much harm to return
4126 * D3DERR_INVALIDCALL there as well. */
4127 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4128 return WINED3DERR_INVALIDCALL;
4131 if (!device->stateBlock->state.vertex_declaration)
4133 WARN("Called without a valid vertex declaration set.\n");
4134 return WINED3DERR_INVALIDCALL;
4137 if (device->stateBlock->state.user_stream)
4139 device_invalidate_state(device, STATE_INDEXBUFFER);
4140 device->stateBlock->state.user_stream = FALSE;
4143 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
4144 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
4146 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
4147 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4150 draw_primitive(device, start_idx, index_count, 0, 0, TRUE, NULL);
4152 return WINED3D_OK;
4155 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4156 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4158 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4160 draw_primitive(device, start_idx, index_count, start_instance, instance_count, TRUE, NULL);
4163 HRESULT CDECL wined3d_device_draw_primitive_strided(struct wined3d_device *device,
4164 UINT vertex_count, const struct wined3d_strided_data *strided_data)
4166 /* Mark the state dirty until we have nicer tracking. It's fine to change
4167 * baseVertexIndex because that call is only called by ddraw which does
4168 * not need that value. */
4169 device_invalidate_state(device, STATE_VDECL);
4170 device_invalidate_state(device, STATE_STREAMSRC);
4171 device_invalidate_state(device, STATE_INDEXBUFFER);
4173 device->stateBlock->state.base_vertex_index = 0;
4174 device->up_strided = strided_data;
4175 draw_primitive(device, 0, vertex_count, 0, 0, FALSE, NULL);
4176 device->up_strided = NULL;
4178 /* Invalidate the states again to make sure the values from the stateblock
4179 * are properly applied in the next regular draw. Note that the application-
4180 * provided strided data has ovwritten pretty much the entire vertex and
4181 * and index stream related states */
4182 device_invalidate_state(device, STATE_VDECL);
4183 device_invalidate_state(device, STATE_STREAMSRC);
4184 device_invalidate_state(device, STATE_INDEXBUFFER);
4185 return WINED3D_OK;
4188 HRESULT CDECL wined3d_device_draw_indexed_primitive_strided(struct wined3d_device *device,
4189 UINT index_count, const struct wined3d_strided_data *strided_data,
4190 UINT vertex_count, const void *index_data, enum wined3d_format_id index_data_format_id)
4192 enum wined3d_format_id prev_idx_format;
4194 /* Mark the state dirty until we have nicer tracking
4195 * its fine to change baseVertexIndex because that call is only called by ddraw which does not need
4196 * that value.
4198 device_invalidate_state(device, STATE_VDECL);
4199 device_invalidate_state(device, STATE_STREAMSRC);
4200 device_invalidate_state(device, STATE_INDEXBUFFER);
4202 prev_idx_format = device->stateBlock->state.index_format;
4203 device->stateBlock->state.index_format = index_data_format_id;
4204 device->stateBlock->state.user_stream = TRUE;
4205 device->stateBlock->state.base_vertex_index = 0;
4206 device->up_strided = strided_data;
4207 draw_primitive(device, 0, index_count, 0, 0, TRUE, index_data);
4208 device->up_strided = NULL;
4209 device->stateBlock->state.index_format = prev_idx_format;
4211 device_invalidate_state(device, STATE_VDECL);
4212 device_invalidate_state(device, STATE_STREAMSRC);
4213 device_invalidate_state(device, STATE_INDEXBUFFER);
4214 return WINED3D_OK;
4217 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4218 static HRESULT device_update_volume(struct wined3d_device *device,
4219 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4221 struct wined3d_map_desc src;
4222 struct wined3d_map_desc dst;
4223 HRESULT hr;
4225 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4226 device, src_volume, dst_volume);
4228 /* TODO: Implement direct loading into the gl volume instead of using
4229 * memcpy and dirtification to improve loading performance. */
4230 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
4231 return hr;
4232 if (FAILED(hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3D_MAP_DISCARD)))
4234 wined3d_volume_unmap(src_volume);
4235 return hr;
4238 memcpy(dst.data, src.data, dst_volume->resource.size);
4240 hr = wined3d_volume_unmap(dst_volume);
4241 if (FAILED(hr))
4242 wined3d_volume_unmap(src_volume);
4243 else
4244 hr = wined3d_volume_unmap(src_volume);
4246 return hr;
4249 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4250 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4252 enum wined3d_resource_type type;
4253 unsigned int level_count, i;
4254 HRESULT hr;
4256 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4258 /* Verify that the source and destination textures are non-NULL. */
4259 if (!src_texture || !dst_texture)
4261 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4262 return WINED3DERR_INVALIDCALL;
4265 if (src_texture == dst_texture)
4267 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4268 return WINED3DERR_INVALIDCALL;
4271 /* Verify that the source and destination textures are the same type. */
4272 type = src_texture->resource.type;
4273 if (dst_texture->resource.type != type)
4275 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4276 return WINED3DERR_INVALIDCALL;
4279 /* Check that both textures have the identical numbers of levels. */
4280 level_count = wined3d_texture_get_level_count(src_texture);
4281 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4283 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4284 return WINED3DERR_INVALIDCALL;
4287 /* Make sure that the destination texture is loaded. */
4288 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4290 /* Update every surface level of the texture. */
4291 switch (type)
4293 case WINED3D_RTYPE_TEXTURE:
4295 struct wined3d_surface *src_surface;
4296 struct wined3d_surface *dst_surface;
4298 for (i = 0; i < level_count; ++i)
4300 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4301 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4302 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4303 if (FAILED(hr))
4305 WARN("Failed to update surface, hr %#x.\n", hr);
4306 return hr;
4309 break;
4312 case WINED3D_RTYPE_CUBE_TEXTURE:
4314 struct wined3d_surface *src_surface;
4315 struct wined3d_surface *dst_surface;
4317 for (i = 0; i < level_count * 6; ++i)
4319 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4320 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4321 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4322 if (FAILED(hr))
4324 WARN("Failed to update surface, hr %#x.\n", hr);
4325 return hr;
4328 break;
4331 case WINED3D_RTYPE_VOLUME_TEXTURE:
4333 for (i = 0; i < level_count; ++i)
4335 hr = device_update_volume(device,
4336 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4337 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4338 if (FAILED(hr))
4340 WARN("Failed to update volume, hr %#x.\n", hr);
4341 return hr;
4344 break;
4347 default:
4348 FIXME("Unsupported texture type %#x.\n", type);
4349 return WINED3DERR_INVALIDCALL;
4352 return WINED3D_OK;
4355 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4356 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4358 struct wined3d_swapchain *swapchain;
4360 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4362 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4363 return WINED3DERR_INVALIDCALL;
4365 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4368 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4370 const struct wined3d_state *state = &device->stateBlock->state;
4371 struct wined3d_texture *texture;
4372 DWORD i;
4374 TRACE("device %p, num_passes %p.\n", device, num_passes);
4376 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4378 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4380 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4381 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4383 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4385 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4386 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4389 texture = state->textures[i];
4390 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4392 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4394 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4395 return E_FAIL;
4397 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4399 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4400 return E_FAIL;
4402 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4403 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4405 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4406 return E_FAIL;
4410 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4411 || state->render_states[WINED3D_RS_STENCILENABLE])
4413 struct wined3d_surface *ds = device->fb.depth_stencil;
4414 struct wined3d_surface *target = device->fb.render_targets[0];
4416 if(ds && target
4417 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4419 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4420 return WINED3DERR_CONFLICTINGRENDERSTATE;
4424 /* return a sensible default */
4425 *num_passes = 1;
4427 TRACE("returning D3D_OK\n");
4428 return WINED3D_OK;
4431 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4433 static BOOL warned;
4435 TRACE("device %p, software %#x.\n", device, software);
4437 if (!warned)
4439 FIXME("device %p, software %#x stub!\n", device, software);
4440 warned = TRUE;
4443 device->softwareVertexProcessing = software;
4446 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4448 static BOOL warned;
4450 TRACE("device %p.\n", device);
4452 if (!warned)
4454 TRACE("device %p stub!\n", device);
4455 warned = TRUE;
4458 return device->softwareVertexProcessing;
4461 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4462 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4464 struct wined3d_swapchain *swapchain;
4466 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4467 device, swapchain_idx, raster_status);
4469 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4470 return WINED3DERR_INVALIDCALL;
4472 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4475 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4477 static BOOL warned;
4479 TRACE("device %p, segments %.8e.\n", device, segments);
4481 if (segments != 0.0f)
4483 if (!warned)
4485 FIXME("device %p, segments %.8e stub!\n", device, segments);
4486 warned = TRUE;
4490 return WINED3D_OK;
4493 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4495 static BOOL warned;
4497 TRACE("device %p.\n", device);
4499 if (!warned)
4501 FIXME("device %p stub!\n", device);
4502 warned = TRUE;
4505 return 0.0f;
4508 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4509 struct wined3d_surface *src_surface, const RECT *src_rect,
4510 struct wined3d_surface *dst_surface, const POINT *dst_point)
4512 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4513 device, src_surface, wine_dbgstr_rect(src_rect),
4514 dst_surface, wine_dbgstr_point(dst_point));
4516 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4518 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4519 src_surface, dst_surface);
4520 return WINED3DERR_INVALIDCALL;
4523 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4526 HRESULT CDECL wined3d_device_draw_rect_patch(struct wined3d_device *device, UINT handle,
4527 const float *num_segs, const struct wined3d_rect_patch_info *rect_patch_info)
4529 struct wined3d_rect_patch *patch;
4530 GLenum old_primitive_type;
4531 unsigned int i;
4532 struct list *e;
4533 BOOL found;
4535 TRACE("device %p, handle %#x, num_segs %p, rect_patch_info %p.\n",
4536 device, handle, num_segs, rect_patch_info);
4538 if (!(handle || rect_patch_info))
4540 /* TODO: Write a test for the return value, thus the FIXME */
4541 FIXME("Both handle and rect_patch_info are NULL.\n");
4542 return WINED3DERR_INVALIDCALL;
4545 if (handle)
4547 i = PATCHMAP_HASHFUNC(handle);
4548 found = FALSE;
4549 LIST_FOR_EACH(e, &device->patches[i])
4551 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4552 if (patch->Handle == handle)
4554 found = TRUE;
4555 break;
4559 if (!found)
4561 TRACE("Patch does not exist. Creating a new one\n");
4562 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4563 patch->Handle = handle;
4564 list_add_head(&device->patches[i], &patch->entry);
4565 } else {
4566 TRACE("Found existing patch %p\n", patch);
4569 else
4571 /* Since opengl does not load tesselated vertex attributes into numbered vertex
4572 * attributes we have to tesselate, read back, and draw. This needs a patch
4573 * management structure instance. Create one.
4575 * A possible improvement is to check if a vertex shader is used, and if not directly
4576 * draw the patch.
4578 FIXME("Drawing an uncached patch. This is slow\n");
4579 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4582 if (num_segs[0] != patch->numSegs[0] || num_segs[1] != patch->numSegs[1]
4583 || num_segs[2] != patch->numSegs[2] || num_segs[3] != patch->numSegs[3]
4584 || (rect_patch_info && memcmp(rect_patch_info, &patch->rect_patch_info, sizeof(*rect_patch_info))))
4586 HRESULT hr;
4587 TRACE("Tesselation density or patch info changed, retesselating\n");
4589 if (rect_patch_info)
4590 patch->rect_patch_info = *rect_patch_info;
4592 patch->numSegs[0] = num_segs[0];
4593 patch->numSegs[1] = num_segs[1];
4594 patch->numSegs[2] = num_segs[2];
4595 patch->numSegs[3] = num_segs[3];
4597 hr = tesselate_rectpatch(device, patch);
4598 if (FAILED(hr))
4600 WARN("Patch tesselation failed.\n");
4602 /* Do not release the handle to store the params of the patch */
4603 if (!handle)
4604 HeapFree(GetProcessHeap(), 0, patch);
4606 return hr;
4610 old_primitive_type = device->stateBlock->state.gl_primitive_type;
4611 device->stateBlock->state.gl_primitive_type = GL_TRIANGLES;
4612 wined3d_device_draw_primitive_strided(device, patch->numSegs[0] * patch->numSegs[1] * 2 * 3, &patch->strided);
4613 device->stateBlock->state.gl_primitive_type = old_primitive_type;
4615 /* Destroy uncached patches */
4616 if (!handle)
4618 HeapFree(GetProcessHeap(), 0, patch->mem);
4619 HeapFree(GetProcessHeap(), 0, patch);
4621 return WINED3D_OK;
4624 HRESULT CDECL wined3d_device_draw_tri_patch(struct wined3d_device *device, UINT handle,
4625 const float *segment_count, const struct wined3d_tri_patch_info *patch_info)
4627 FIXME("device %p, handle %#x, segment_count %p, patch_info %p stub!\n",
4628 device, handle, segment_count, patch_info);
4630 return WINED3D_OK;
4633 HRESULT CDECL wined3d_device_delete_patch(struct wined3d_device *device, UINT handle)
4635 struct wined3d_rect_patch *patch;
4636 struct list *e;
4637 int i;
4639 TRACE("device %p, handle %#x.\n", device, handle);
4641 i = PATCHMAP_HASHFUNC(handle);
4642 LIST_FOR_EACH(e, &device->patches[i])
4644 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4645 if (patch->Handle == handle)
4647 TRACE("Deleting patch %p\n", patch);
4648 list_remove(&patch->entry);
4649 HeapFree(GetProcessHeap(), 0, patch->mem);
4650 HeapFree(GetProcessHeap(), 0, patch);
4651 return WINED3D_OK;
4655 /* TODO: Write a test for the return value */
4656 FIXME("Attempt to destroy nonexistent patch\n");
4657 return WINED3DERR_INVALIDCALL;
4660 /* Do not call while under the GL lock. */
4661 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4662 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4664 RECT r;
4666 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4667 device, surface, wine_dbgstr_rect(rect),
4668 color->r, color->g, color->b, color->a);
4670 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4672 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4673 return WINED3DERR_INVALIDCALL;
4676 if (!rect)
4678 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4679 rect = &r;
4682 return surface_color_fill(surface, rect, color);
4685 /* Do not call while under the GL lock. */
4686 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4687 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4689 struct wined3d_resource *resource;
4690 HRESULT hr;
4691 RECT rect;
4693 resource = rendertarget_view->resource;
4694 if (resource->type != WINED3D_RTYPE_SURFACE)
4696 FIXME("Only supported on surface resources\n");
4697 return;
4700 SetRect(&rect, 0, 0, resource->width, resource->height);
4701 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4702 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4705 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4706 UINT render_target_idx)
4708 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
4710 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4712 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4713 return NULL;
4716 return device->fb.render_targets[render_target_idx];
4719 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
4721 TRACE("device %p.\n", device);
4723 return device->fb.depth_stencil;
4726 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4727 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4729 struct wined3d_surface *prev;
4731 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4732 device, render_target_idx, render_target, set_viewport);
4734 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4736 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4737 return WINED3DERR_INVALIDCALL;
4740 /* Render target 0 can't be set to NULL. */
4741 if (!render_target && !render_target_idx)
4743 WARN("Trying to set render target 0 to NULL.\n");
4744 return WINED3DERR_INVALIDCALL;
4747 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4749 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4750 return WINED3DERR_INVALIDCALL;
4753 /* Set the viewport and scissor rectangles, if requested. Tests show that
4754 * stateblock recording is ignored, the change goes directly into the
4755 * primary stateblock. */
4756 if (!render_target_idx && set_viewport)
4758 struct wined3d_state *state = &device->stateBlock->state;
4760 state->viewport.x = 0;
4761 state->viewport.y = 0;
4762 state->viewport.width = render_target->resource.width;
4763 state->viewport.height = render_target->resource.height;
4764 state->viewport.min_z = 0.0f;
4765 state->viewport.max_z = 1.0f;
4766 device_invalidate_state(device, STATE_VIEWPORT);
4768 state->scissor_rect.top = 0;
4769 state->scissor_rect.left = 0;
4770 state->scissor_rect.right = render_target->resource.width;
4771 state->scissor_rect.bottom = render_target->resource.height;
4772 device_invalidate_state(device, STATE_SCISSORRECT);
4776 prev = device->fb.render_targets[render_target_idx];
4777 if (render_target == prev)
4778 return WINED3D_OK;
4780 if (render_target)
4781 wined3d_surface_incref(render_target);
4782 device->fb.render_targets[render_target_idx] = render_target;
4783 /* Release after the assignment, to prevent device_resource_released()
4784 * from seeing the surface as still in use. */
4785 if (prev)
4786 wined3d_surface_decref(prev);
4788 device_invalidate_state(device, STATE_FRAMEBUFFER);
4790 return WINED3D_OK;
4793 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4795 struct wined3d_surface *prev = device->fb.depth_stencil;
4797 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4798 device, depth_stencil, prev);
4800 if (prev == depth_stencil)
4802 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4803 return;
4806 if (prev)
4808 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4809 || prev->flags & SFLAG_DISCARD)
4811 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4812 prev->resource.width, prev->resource.height);
4813 if (prev == device->onscreen_depth_stencil)
4815 wined3d_surface_decref(device->onscreen_depth_stencil);
4816 device->onscreen_depth_stencil = NULL;
4821 device->fb.depth_stencil = depth_stencil;
4822 if (depth_stencil)
4823 wined3d_surface_incref(depth_stencil);
4825 if (!prev != !depth_stencil)
4827 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4828 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4829 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4830 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4831 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4833 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4835 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4837 if (prev)
4838 wined3d_surface_decref(prev);
4840 device_invalidate_state(device, STATE_FRAMEBUFFER);
4842 return;
4845 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4846 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4848 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4849 device, x_hotspot, y_hotspot, cursor_image);
4851 /* some basic validation checks */
4852 if (device->cursorTexture)
4854 struct wined3d_context *context = context_acquire(device, NULL);
4855 context->gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4856 context_release(context);
4857 device->cursorTexture = 0;
4860 if (cursor_image)
4862 struct wined3d_display_mode mode;
4863 struct wined3d_map_desc map_desc;
4864 HRESULT hr;
4866 /* MSDN: Cursor must be A8R8G8B8 */
4867 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4869 WARN("surface %p has an invalid format.\n", cursor_image);
4870 return WINED3DERR_INVALIDCALL;
4873 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4875 ERR("Failed to get display mode, hr %#x.\n", hr);
4876 return WINED3DERR_INVALIDCALL;
4879 /* MSDN: Cursor must be smaller than the display mode */
4880 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4882 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4883 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4884 mode.width, mode.height);
4885 return WINED3DERR_INVALIDCALL;
4888 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4890 /* Do not store the surface's pointer because the application may
4891 * release it after setting the cursor image. Windows doesn't
4892 * addref the set surface, so we can't do this either without
4893 * creating circular refcount dependencies. Copy out the gl texture
4894 * instead. */
4895 device->cursorWidth = cursor_image->resource.width;
4896 device->cursorHeight = cursor_image->resource.height;
4897 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4899 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4900 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
4901 struct wined3d_context *context;
4902 char *mem, *bits = map_desc.data;
4903 GLint intfmt = format->glInternal;
4904 GLint gl_format = format->glFormat;
4905 GLint type = format->glType;
4906 INT height = device->cursorHeight;
4907 INT width = device->cursorWidth;
4908 INT bpp = format->byte_count;
4909 INT i;
4911 /* Reformat the texture memory (pitch and width can be
4912 * different) */
4913 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
4914 for (i = 0; i < height; ++i)
4915 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
4916 wined3d_surface_unmap(cursor_image);
4918 context = context_acquire(device, NULL);
4920 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4922 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
4923 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4926 invalidate_active_texture(device, context);
4927 /* Create a new cursor texture */
4928 gl_info->gl_ops.gl.p_glGenTextures(1, &device->cursorTexture);
4929 checkGLcall("glGenTextures");
4930 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
4931 /* Copy the bitmap memory into the cursor texture */
4932 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
4933 checkGLcall("glTexImage2D");
4934 HeapFree(GetProcessHeap(), 0, mem);
4936 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4938 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
4939 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4942 context_release(context);
4944 else
4946 FIXME("A cursor texture was not returned.\n");
4947 device->cursorTexture = 0;
4950 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4952 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4953 ICONINFO cursorInfo;
4954 DWORD *maskBits;
4955 HCURSOR cursor;
4957 /* 32-bit user32 cursors ignore the alpha channel if it's all
4958 * zeroes, and use the mask instead. Fill the mask with all ones
4959 * to ensure we still get a fully transparent cursor. */
4960 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4961 memset(maskBits, 0xff, mask_size);
4962 wined3d_surface_map(cursor_image, &map_desc, NULL,
4963 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4964 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4966 cursorInfo.fIcon = FALSE;
4967 cursorInfo.xHotspot = x_hotspot;
4968 cursorInfo.yHotspot = y_hotspot;
4969 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4970 1, 1, maskBits);
4971 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4972 1, 32, map_desc.data);
4973 wined3d_surface_unmap(cursor_image);
4974 /* Create our cursor and clean up. */
4975 cursor = CreateIconIndirect(&cursorInfo);
4976 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4977 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4978 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4979 device->hardwareCursor = cursor;
4980 if (device->bCursorVisible) SetCursor( cursor );
4981 HeapFree(GetProcessHeap(), 0, maskBits);
4985 device->xHotSpot = x_hotspot;
4986 device->yHotSpot = y_hotspot;
4987 return WINED3D_OK;
4990 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4991 int x_screen_space, int y_screen_space, DWORD flags)
4993 TRACE("device %p, x %d, y %d, flags %#x.\n",
4994 device, x_screen_space, y_screen_space, flags);
4996 device->xScreenSpace = x_screen_space;
4997 device->yScreenSpace = y_screen_space;
4999 if (device->hardwareCursor)
5001 POINT pt;
5003 GetCursorPos( &pt );
5004 if (x_screen_space == pt.x && y_screen_space == pt.y)
5005 return;
5006 SetCursorPos( x_screen_space, y_screen_space );
5008 /* Switch to the software cursor if position diverges from the hardware one. */
5009 GetCursorPos( &pt );
5010 if (x_screen_space != pt.x || y_screen_space != pt.y)
5012 if (device->bCursorVisible) SetCursor( NULL );
5013 DestroyCursor( device->hardwareCursor );
5014 device->hardwareCursor = 0;
5019 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5021 BOOL oldVisible = device->bCursorVisible;
5023 TRACE("device %p, show %#x.\n", device, show);
5026 * When ShowCursor is first called it should make the cursor appear at the OS's last
5027 * known cursor position.
5029 if (show && !oldVisible)
5031 POINT pt;
5032 GetCursorPos(&pt);
5033 device->xScreenSpace = pt.x;
5034 device->yScreenSpace = pt.y;
5037 if (device->hardwareCursor)
5039 device->bCursorVisible = show;
5040 if (show)
5041 SetCursor(device->hardwareCursor);
5042 else
5043 SetCursor(NULL);
5045 else
5047 if (device->cursorTexture)
5048 device->bCursorVisible = show;
5051 return oldVisible;
5054 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5056 struct wined3d_resource *resource, *cursor;
5058 TRACE("device %p.\n", device);
5060 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5062 TRACE("Checking resource %p for eviction.\n", resource);
5064 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
5066 TRACE("Evicting %p.\n", resource);
5067 resource->resource_ops->resource_unload(resource);
5071 /* Invalidate stream sources, the buffer(s) may have been evicted. */
5072 device_invalidate_state(device, STATE_STREAMSRC);
5075 /* Do not call while under the GL lock. */
5076 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5078 struct wined3d_resource *resource, *cursor;
5079 const struct wined3d_gl_info *gl_info;
5080 struct wined3d_context *context;
5081 struct wined3d_shader *shader;
5083 context = context_acquire(device, NULL);
5084 gl_info = context->gl_info;
5086 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5088 TRACE("Unloading resource %p.\n", resource);
5090 resource->resource_ops->resource_unload(resource);
5093 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
5095 device->shader_backend->shader_destroy(shader);
5098 if (device->depth_blt_texture)
5100 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
5101 device->depth_blt_texture = 0;
5103 if (device->cursorTexture)
5105 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
5106 device->cursorTexture = 0;
5109 device->blitter->free_private(device);
5110 device->shader_backend->shader_free_private(device);
5111 destroy_dummy_textures(device, gl_info);
5113 context_release(context);
5115 while (device->context_count)
5117 swapchain_destroy_contexts(device->contexts[0]->swapchain);
5120 HeapFree(GetProcessHeap(), 0, swapchain->context);
5121 swapchain->context = NULL;
5124 /* Do not call while under the GL lock. */
5125 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5127 struct wined3d_context *context;
5128 struct wined3d_surface *target;
5129 HRESULT hr;
5131 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
5133 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
5134 return hr;
5137 if (FAILED(hr = device->blitter->alloc_private(device)))
5139 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
5140 device->shader_backend->shader_free_private(device);
5141 return hr;
5144 /* Recreate the primary swapchain's context */
5145 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
5146 if (!swapchain->context)
5148 ERR("Failed to allocate memory for swapchain context array.\n");
5149 device->blitter->free_private(device);
5150 device->shader_backend->shader_free_private(device);
5151 return E_OUTOFMEMORY;
5154 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
5155 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
5157 WARN("Failed to create context.\n");
5158 device->blitter->free_private(device);
5159 device->shader_backend->shader_free_private(device);
5160 HeapFree(GetProcessHeap(), 0, swapchain->context);
5161 return E_FAIL;
5164 swapchain->context[0] = context;
5165 swapchain->num_contexts = 1;
5166 create_dummy_textures(device, context);
5167 context_release(context);
5169 return WINED3D_OK;
5172 /* Do not call while under the GL lock. */
5173 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5174 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5175 wined3d_device_reset_cb callback, BOOL reset_state)
5177 struct wined3d_resource *resource, *cursor;
5178 struct wined3d_swapchain *swapchain;
5179 struct wined3d_display_mode m;
5180 BOOL DisplayModeChanged = FALSE;
5181 BOOL update_desc = FALSE;
5182 HRESULT hr = WINED3D_OK;
5183 unsigned int i;
5185 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
5187 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5189 ERR("Failed to get the first implicit swapchain.\n");
5190 return WINED3DERR_INVALIDCALL;
5193 if (reset_state)
5194 stateblock_unbind_resources(device->stateBlock);
5196 if (device->fb.render_targets)
5198 if (swapchain->back_buffers && swapchain->back_buffers[0])
5199 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
5200 else
5201 wined3d_device_set_render_target(device, 0, swapchain->front_buffer, FALSE);
5202 for (i = 1; i < device->adapter->gl_info.limits.buffers; ++i)
5204 wined3d_device_set_render_target(device, i, NULL, FALSE);
5207 wined3d_device_set_depth_stencil(device, NULL);
5209 if (device->onscreen_depth_stencil)
5211 wined3d_surface_decref(device->onscreen_depth_stencil);
5212 device->onscreen_depth_stencil = NULL;
5215 if (reset_state)
5217 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5219 TRACE("Enumerating resource %p.\n", resource);
5220 if (FAILED(hr = callback(resource)))
5221 return hr;
5225 /* Is it necessary to recreate the gl context? Actually every setting can be changed
5226 * on an existing gl context, so there's no real need for recreation.
5228 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
5230 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
5232 TRACE("New params:\n");
5233 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5234 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5235 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5236 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5237 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5238 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5239 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5240 TRACE("device_window %p\n", swapchain_desc->device_window);
5241 TRACE("windowed %#x\n", swapchain_desc->windowed);
5242 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5243 if (swapchain_desc->enable_auto_depth_stencil)
5244 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5245 TRACE("flags %#x\n", swapchain_desc->flags);
5246 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5247 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
5248 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5250 /* No special treatment of these parameters. Just store them */
5251 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
5252 swapchain->desc.flags = swapchain_desc->flags;
5253 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
5254 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
5256 /* What to do about these? */
5257 if (swapchain_desc->backbuffer_count
5258 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
5259 FIXME("Cannot change the back buffer count yet.\n");
5261 if (swapchain_desc->device_window
5262 && swapchain_desc->device_window != swapchain->desc.device_window)
5264 TRACE("Changing the device window from %p to %p.\n",
5265 swapchain->desc.device_window, swapchain_desc->device_window);
5266 swapchain->desc.device_window = swapchain_desc->device_window;
5267 swapchain->device_window = swapchain_desc->device_window;
5268 wined3d_swapchain_set_window(swapchain, NULL);
5271 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
5273 TRACE("Creating the depth stencil buffer\n");
5275 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
5276 device->device_parent, swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height,
5277 swapchain_desc->auto_depth_stencil_format, WINED3DUSAGE_DEPTHSTENCIL,
5278 swapchain_desc->multisample_type, swapchain_desc->multisample_quality,
5279 &device->auto_depth_stencil)))
5281 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
5282 return WINED3DERR_INVALIDCALL;
5286 /* Reset the depth stencil */
5287 if (swapchain_desc->enable_auto_depth_stencil)
5288 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5290 if (mode)
5292 DisplayModeChanged = TRUE;
5293 m = *mode;
5295 else if (swapchain_desc->windowed)
5297 m.width = swapchain->orig_width;
5298 m.height = swapchain->orig_height;
5299 m.refresh_rate = 0;
5300 m.format_id = swapchain->desc.backbuffer_format;
5301 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5303 else
5305 m.width = swapchain_desc->backbuffer_width;
5306 m.height = swapchain_desc->backbuffer_height;
5307 m.refresh_rate = swapchain_desc->refresh_rate;
5308 m.format_id = swapchain_desc->backbuffer_format;
5309 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5312 /* Should Width == 800 && Height == 0 set 800x600? */
5313 if (swapchain_desc->backbuffer_width && swapchain_desc->backbuffer_height
5314 && (swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
5315 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height))
5317 if (!swapchain_desc->windowed)
5318 DisplayModeChanged = TRUE;
5320 swapchain->desc.backbuffer_width = swapchain_desc->backbuffer_width;
5321 swapchain->desc.backbuffer_height = swapchain_desc->backbuffer_height;
5322 update_desc = TRUE;
5325 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5326 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5328 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5329 update_desc = TRUE;
5332 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5333 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5335 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5336 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5337 update_desc = TRUE;
5340 if (update_desc)
5342 UINT i;
5344 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5345 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5346 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5347 return hr;
5349 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5351 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5352 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5353 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5354 return hr;
5356 if (device->auto_depth_stencil)
5358 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5359 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5360 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5361 return hr;
5365 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5366 || DisplayModeChanged)
5368 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
5370 WARN("Failed to set display mode, hr %#x.\n", hr);
5371 return WINED3DERR_INVALIDCALL;
5374 if (!swapchain_desc->windowed)
5376 if (swapchain->desc.windowed)
5378 HWND focus_window = device->create_parms.focus_window;
5379 if (!focus_window)
5380 focus_window = swapchain_desc->device_window;
5381 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5383 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5384 return hr;
5387 /* switch from windowed to fs */
5388 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5389 swapchain_desc->backbuffer_width,
5390 swapchain_desc->backbuffer_height);
5392 else
5394 /* Fullscreen -> fullscreen mode change */
5395 MoveWindow(swapchain->device_window, 0, 0,
5396 swapchain_desc->backbuffer_width,
5397 swapchain_desc->backbuffer_height,
5398 TRUE);
5401 else if (!swapchain->desc.windowed)
5403 /* Fullscreen -> windowed switch */
5404 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5405 wined3d_device_release_focus_window(device);
5407 swapchain->desc.windowed = swapchain_desc->windowed;
5409 else if (!swapchain_desc->windowed)
5411 DWORD style = device->style;
5412 DWORD exStyle = device->exStyle;
5413 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5414 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5415 * Reset to clear up their mess. Guild Wars also loses the device during that.
5417 device->style = 0;
5418 device->exStyle = 0;
5419 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5420 swapchain_desc->backbuffer_width,
5421 swapchain_desc->backbuffer_height);
5422 device->style = style;
5423 device->exStyle = exStyle;
5426 if (reset_state)
5428 TRACE("Resetting stateblock.\n");
5429 wined3d_stateblock_decref(device->updateStateBlock);
5430 wined3d_stateblock_decref(device->stateBlock);
5432 if (device->d3d_initialized)
5433 delete_opengl_contexts(device, swapchain);
5435 /* Note: No parent needed for initial internal stateblock */
5436 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5437 if (FAILED(hr))
5438 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5439 else
5440 TRACE("Created stateblock %p.\n", device->stateBlock);
5441 device->updateStateBlock = device->stateBlock;
5442 wined3d_stateblock_incref(device->updateStateBlock);
5444 stateblock_init_default_state(device->stateBlock);
5446 else
5448 struct wined3d_surface *rt = device->fb.render_targets[0];
5449 struct wined3d_state *state = &device->stateBlock->state;
5451 /* Note the min_z / max_z is not reset. */
5452 state->viewport.x = 0;
5453 state->viewport.y = 0;
5454 state->viewport.width = rt->resource.width;
5455 state->viewport.height = rt->resource.height;
5456 device_invalidate_state(device, STATE_VIEWPORT);
5458 state->scissor_rect.top = 0;
5459 state->scissor_rect.left = 0;
5460 state->scissor_rect.right = rt->resource.width;
5461 state->scissor_rect.bottom = rt->resource.height;
5462 device_invalidate_state(device, STATE_SCISSORRECT);
5465 swapchain_update_render_to_fbo(swapchain);
5466 swapchain_update_draw_bindings(swapchain);
5468 if (reset_state && device->d3d_initialized)
5469 hr = create_primary_opengl_context(device, swapchain);
5471 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5472 * first use
5474 return hr;
5477 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5479 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5481 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5483 return WINED3D_OK;
5487 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5488 struct wined3d_device_creation_parameters *parameters)
5490 TRACE("device %p, parameters %p.\n", device, parameters);
5492 *parameters = device->create_parms;
5495 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5496 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5498 struct wined3d_swapchain *swapchain;
5500 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5501 device, swapchain_idx, flags, ramp);
5503 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5504 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5507 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5508 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5510 struct wined3d_swapchain *swapchain;
5512 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5513 device, swapchain_idx, ramp);
5515 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5516 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5519 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5521 TRACE("device %p, resource %p.\n", device, resource);
5523 list_add_head(&device->resources, &resource->resource_list_entry);
5526 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5528 TRACE("device %p, resource %p.\n", device, resource);
5530 list_remove(&resource->resource_list_entry);
5533 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5535 enum wined3d_resource_type type = resource->type;
5536 unsigned int i;
5538 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5540 context_resource_released(device, resource, type);
5542 switch (type)
5544 case WINED3D_RTYPE_SURFACE:
5546 struct wined3d_surface *surface = surface_from_resource(resource);
5548 if (!device->d3d_initialized) break;
5550 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5552 if (device->fb.render_targets[i] == surface)
5554 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5555 device->fb.render_targets[i] = NULL;
5559 if (device->fb.depth_stencil == surface)
5561 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5562 device->fb.depth_stencil = NULL;
5565 break;
5567 case WINED3D_RTYPE_TEXTURE:
5568 case WINED3D_RTYPE_CUBE_TEXTURE:
5569 case WINED3D_RTYPE_VOLUME_TEXTURE:
5570 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5572 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5574 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5576 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5577 texture, device->stateBlock, i);
5578 device->stateBlock->state.textures[i] = NULL;
5581 if (device->updateStateBlock != device->stateBlock
5582 && device->updateStateBlock->state.textures[i] == texture)
5584 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5585 texture, device->updateStateBlock, i);
5586 device->updateStateBlock->state.textures[i] = NULL;
5589 break;
5591 case WINED3D_RTYPE_BUFFER:
5593 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5595 for (i = 0; i < MAX_STREAMS; ++i)
5597 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5599 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5600 buffer, device->stateBlock, i);
5601 device->stateBlock->state.streams[i].buffer = NULL;
5604 if (device->updateStateBlock != device->stateBlock
5605 && device->updateStateBlock->state.streams[i].buffer == buffer)
5607 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5608 buffer, device->updateStateBlock, i);
5609 device->updateStateBlock->state.streams[i].buffer = NULL;
5614 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5616 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5617 buffer, device->stateBlock);
5618 device->stateBlock->state.index_buffer = NULL;
5621 if (device->updateStateBlock != device->stateBlock
5622 && device->updateStateBlock->state.index_buffer == buffer)
5624 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5625 buffer, device->updateStateBlock);
5626 device->updateStateBlock->state.index_buffer = NULL;
5629 break;
5631 default:
5632 break;
5635 /* Remove the resource from the resourceStore */
5636 device_resource_remove(device, resource);
5638 TRACE("Resource released.\n");
5641 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
5643 struct wined3d_resource *resource;
5645 TRACE("device %p, dc %p.\n", device, dc);
5647 if (!dc)
5648 return NULL;
5650 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5652 if (resource->type == WINED3D_RTYPE_SURFACE)
5654 struct wined3d_surface *s = surface_from_resource(resource);
5656 if (s->hDC == dc)
5658 TRACE("Found surface %p for dc %p.\n", s, dc);
5659 return s;
5664 return NULL;
5667 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5668 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5669 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5671 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5672 const struct fragment_pipeline *fragment_pipeline;
5673 struct shader_caps shader_caps;
5674 struct fragment_caps ffp_caps;
5675 unsigned int i;
5676 HRESULT hr;
5678 device->ref = 1;
5679 device->wined3d = wined3d;
5680 wined3d_incref(device->wined3d);
5681 device->adapter = wined3d->adapter_count ? adapter : NULL;
5682 device->device_parent = device_parent;
5683 list_init(&device->resources);
5684 list_init(&device->shaders);
5685 device->surface_alignment = surface_alignment;
5687 /* Save the creation parameters. */
5688 device->create_parms.adapter_idx = adapter_idx;
5689 device->create_parms.device_type = device_type;
5690 device->create_parms.focus_window = focus_window;
5691 device->create_parms.flags = flags;
5693 for (i = 0; i < PATCHMAP_SIZE; ++i) list_init(&device->patches[i]);
5695 select_shader_mode(&adapter->gl_info, &device->ps_selected_mode, &device->vs_selected_mode);
5696 device->shader_backend = adapter->shader_backend;
5698 if (device->shader_backend)
5700 device->shader_backend->shader_get_caps(&adapter->gl_info, &shader_caps);
5701 device->vs_version = shader_caps.vs_version;
5702 device->gs_version = shader_caps.gs_version;
5703 device->ps_version = shader_caps.ps_version;
5704 device->d3d_vshader_constantF = shader_caps.vs_uniform_count;
5705 device->d3d_pshader_constantF = shader_caps.ps_uniform_count;
5706 device->vs_clipping = shader_caps.vs_clipping;
5708 fragment_pipeline = adapter->fragment_pipe;
5709 if (fragment_pipeline)
5711 fragment_pipeline->get_caps(&adapter->gl_info, &ffp_caps);
5712 device->max_ffp_textures = ffp_caps.MaxSimultaneousTextures;
5714 hr = compile_state_table(device->StateTable, device->multistate_funcs, &adapter->gl_info,
5715 ffp_vertexstate_template, fragment_pipeline, misc_state_template);
5716 if (FAILED(hr))
5718 ERR("Failed to compile state table, hr %#x.\n", hr);
5719 wined3d_decref(device->wined3d);
5720 return hr;
5723 device->blitter = adapter->blitter;
5725 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5726 if (FAILED(hr))
5728 WARN("Failed to create stateblock.\n");
5729 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5731 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5733 wined3d_decref(device->wined3d);
5734 return hr;
5737 TRACE("Created stateblock %p.\n", device->stateBlock);
5738 device->updateStateBlock = device->stateBlock;
5739 wined3d_stateblock_incref(device->updateStateBlock);
5741 return WINED3D_OK;
5745 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5747 DWORD rep = device->StateTable[state].representative;
5748 struct wined3d_context *context;
5749 DWORD idx;
5750 BYTE shift;
5751 UINT i;
5753 for (i = 0; i < device->context_count; ++i)
5755 context = device->contexts[i];
5756 if(isStateDirty(context, rep)) continue;
5758 context->dirtyArray[context->numDirtyEntries++] = rep;
5759 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5760 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5761 context->isStateDirty[idx] |= (1 << shift);
5765 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5767 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5768 *width = context->current_rt->pow2Width;
5769 *height = context->current_rt->pow2Height;
5772 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5774 const struct wined3d_swapchain *swapchain = context->swapchain;
5775 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5776 * current context's drawable, which is the size of the back buffer of the swapchain
5777 * the active context belongs to. */
5778 *width = swapchain->desc.backbuffer_width;
5779 *height = swapchain->desc.backbuffer_height;
5782 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5783 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5785 if (device->filter_messages)
5787 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5788 window, message, wparam, lparam);
5789 if (unicode)
5790 return DefWindowProcW(window, message, wparam, lparam);
5791 else
5792 return DefWindowProcA(window, message, wparam, lparam);
5795 if (message == WM_DESTROY)
5797 TRACE("unregister window %p.\n", window);
5798 wined3d_unregister_window(window);
5800 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5801 ERR("Window %p is not the focus window for device %p.\n", window, device);
5803 else if (message == WM_DISPLAYCHANGE)
5805 device->device_parent->ops->mode_changed(device->device_parent);
5808 if (unicode)
5809 return CallWindowProcW(proc, window, message, wparam, lparam);
5810 else
5811 return CallWindowProcA(proc, window, message, wparam, lparam);