wined3d: Get rid of the conv_mat macro.
[wine/multimedia.git] / dlls / wined3d / device.c
blobe1e28d1f2d3aba51b6476a196ea9ac78c98e1df3
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 <stdio.h>
29 #ifdef HAVE_FLOAT_H
30 # include <float.h>
31 #endif
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
36 /* Define the default light parameters as specified by MSDN. */
37 const struct wined3d_light WINED3D_default_light =
39 WINED3D_LIGHT_DIRECTIONAL, /* Type */
40 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
41 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
42 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
43 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
44 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
45 0.0f, /* Range */
46 0.0f, /* Falloff */
47 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
48 0.0f, /* Theta */
49 0.0f /* Phi */
52 /**********************************************************
53 * Global variable / Constants follow
54 **********************************************************/
55 const struct wined3d_matrix identity =
56 {{{
57 1.0f, 0.0f, 0.0f, 0.0f,
58 0.0f, 1.0f, 0.0f, 0.0f,
59 0.0f, 0.0f, 1.0f, 0.0f,
60 0.0f, 0.0f, 0.0f, 1.0f,
61 }}}; /* When needed for comparisons */
63 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
64 * actually have the same values in GL and D3D. */
65 static GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
67 switch(primitive_type)
69 case WINED3D_PT_POINTLIST:
70 return GL_POINTS;
72 case WINED3D_PT_LINELIST:
73 return GL_LINES;
75 case WINED3D_PT_LINESTRIP:
76 return GL_LINE_STRIP;
78 case WINED3D_PT_TRIANGLELIST:
79 return GL_TRIANGLES;
81 case WINED3D_PT_TRIANGLESTRIP:
82 return GL_TRIANGLE_STRIP;
84 case WINED3D_PT_TRIANGLEFAN:
85 return GL_TRIANGLE_FAN;
87 case WINED3D_PT_LINELIST_ADJ:
88 return GL_LINES_ADJACENCY_ARB;
90 case WINED3D_PT_LINESTRIP_ADJ:
91 return GL_LINE_STRIP_ADJACENCY_ARB;
93 case WINED3D_PT_TRIANGLELIST_ADJ:
94 return GL_TRIANGLES_ADJACENCY_ARB;
96 case WINED3D_PT_TRIANGLESTRIP_ADJ:
97 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
99 default:
100 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
101 return GL_NONE;
105 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
107 switch(primitive_type)
109 case GL_POINTS:
110 return WINED3D_PT_POINTLIST;
112 case GL_LINES:
113 return WINED3D_PT_LINELIST;
115 case GL_LINE_STRIP:
116 return WINED3D_PT_LINESTRIP;
118 case GL_TRIANGLES:
119 return WINED3D_PT_TRIANGLELIST;
121 case GL_TRIANGLE_STRIP:
122 return WINED3D_PT_TRIANGLESTRIP;
124 case GL_TRIANGLE_FAN:
125 return WINED3D_PT_TRIANGLEFAN;
127 case GL_LINES_ADJACENCY_ARB:
128 return WINED3D_PT_LINELIST_ADJ;
130 case GL_LINE_STRIP_ADJACENCY_ARB:
131 return WINED3D_PT_LINESTRIP_ADJ;
133 case GL_TRIANGLES_ADJACENCY_ARB:
134 return WINED3D_PT_TRIANGLELIST_ADJ;
136 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
137 return WINED3D_PT_TRIANGLESTRIP_ADJ;
139 default:
140 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
141 return WINED3D_PT_UNDEFINED;
145 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
147 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
148 *regnum = WINED3D_FFP_POSITION;
149 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
150 *regnum = WINED3D_FFP_BLENDWEIGHT;
151 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
152 *regnum = WINED3D_FFP_BLENDINDICES;
153 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
154 *regnum = WINED3D_FFP_NORMAL;
155 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
156 *regnum = WINED3D_FFP_PSIZE;
157 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
158 *regnum = WINED3D_FFP_DIFFUSE;
159 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
160 *regnum = WINED3D_FFP_SPECULAR;
161 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
162 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
163 else
165 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n", debug_d3ddeclusage(usage), usage_idx);
166 *regnum = ~0U;
167 return FALSE;
170 return TRUE;
173 /* Context activation is done by the caller. */
174 void device_stream_info_from_declaration(struct wined3d_device *device, struct wined3d_stream_info *stream_info)
176 const struct wined3d_state *state = &device->stateBlock->state;
177 /* We need to deal with frequency data! */
178 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
179 BOOL use_vshader;
180 unsigned int i;
182 stream_info->use_map = 0;
183 stream_info->swizzle_map = 0;
185 /* Check for transformed vertices, disable vertex shader if present. */
186 stream_info->position_transformed = declaration->position_transformed;
187 use_vshader = state->vertex_shader && !declaration->position_transformed;
189 /* Translate the declaration into strided data. */
190 for (i = 0; i < declaration->element_count; ++i)
192 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
193 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
194 struct wined3d_buffer *buffer = stream->buffer;
195 struct wined3d_bo_address data;
196 BOOL stride_used;
197 unsigned int idx;
198 DWORD stride;
200 TRACE("%p Element %p (%u of %u)\n", declaration->elements,
201 element, i + 1, declaration->element_count);
203 if (!buffer) continue;
205 data.buffer_object = 0;
206 data.addr = NULL;
208 stride = stream->stride;
209 if (state->user_stream)
211 TRACE("Stream %u is UP, %p\n", element->input_slot, buffer);
212 data.buffer_object = 0;
213 data.addr = (BYTE *)buffer;
215 else
217 TRACE("Stream %u isn't UP, %p\n", element->input_slot, buffer);
218 buffer_get_memory(buffer, &device->adapter->gl_info, &data);
220 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
221 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
222 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
223 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
224 * not, drawStridedSlow is needed, including a vertex buffer path. */
225 if (state->load_base_vertex_index < 0)
227 WARN("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
228 state->load_base_vertex_index);
229 data.buffer_object = 0;
230 data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info);
231 if ((UINT_PTR)data.addr < -state->load_base_vertex_index * stride)
233 FIXME("System memory vertex data load offset is negative!\n");
237 data.addr += element->offset;
239 TRACE("offset %u input_slot %u usage_idx %d\n", element->offset, element->input_slot, element->usage_idx);
241 if (use_vshader)
243 if (element->output_slot == ~0U)
245 /* TODO: Assuming vertexdeclarations are usually used with the
246 * same or a similar shader, it might be worth it to store the
247 * last used output slot and try that one first. */
248 stride_used = vshader_get_input(state->vertex_shader,
249 element->usage, element->usage_idx, &idx);
251 else
253 idx = element->output_slot;
254 stride_used = TRUE;
257 else
259 if (!element->ffp_valid)
261 WARN("Skipping unsupported fixed function element of format %s and usage %s\n",
262 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
263 stride_used = FALSE;
265 else
267 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
271 if (stride_used)
273 TRACE("Load %s array %u [usage %s, usage_idx %u, "
274 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u]\n",
275 use_vshader ? "shader": "fixed function", idx,
276 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
277 element->offset, stride, debug_d3dformat(element->format->id), data.buffer_object);
279 data.addr += stream->offset;
281 stream_info->elements[idx].format = element->format;
282 stream_info->elements[idx].data = data;
283 stream_info->elements[idx].stride = stride;
284 stream_info->elements[idx].stream_idx = element->input_slot;
286 if (!device->adapter->gl_info.supported[ARB_VERTEX_ARRAY_BGRA]
287 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
289 stream_info->swizzle_map |= 1 << idx;
291 stream_info->use_map |= 1 << idx;
295 device->num_buffer_queries = 0;
296 if (!state->user_stream)
298 WORD map = stream_info->use_map;
299 stream_info->all_vbo = 1;
301 /* PreLoad all the vertex buffers. */
302 for (i = 0; map; map >>= 1, ++i)
304 struct wined3d_stream_info_element *element;
305 struct wined3d_buffer *buffer;
307 if (!(map & 1)) continue;
309 element = &stream_info->elements[i];
310 buffer = state->streams[element->stream_idx].buffer;
311 wined3d_buffer_preload(buffer);
313 /* If the preload dropped the buffer object, update the stream info. */
314 if (buffer->buffer_object != element->data.buffer_object)
316 element->data.buffer_object = 0;
317 element->data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info)
318 + (ptrdiff_t)element->data.addr;
321 if (!buffer->buffer_object)
322 stream_info->all_vbo = 0;
324 if (buffer->query)
325 device->buffer_queries[device->num_buffer_queries++] = buffer->query;
328 else
330 stream_info->all_vbo = 0;
334 static void stream_info_element_from_strided(const struct wined3d_gl_info *gl_info,
335 const struct wined3d_strided_element *strided, struct wined3d_stream_info_element *e)
337 e->data.addr = strided->data;
338 e->data.buffer_object = 0;
339 e->format = wined3d_get_format(gl_info, strided->format);
340 e->stride = strided->stride;
341 e->stream_idx = 0;
344 static void device_stream_info_from_strided(const struct wined3d_gl_info *gl_info,
345 const struct wined3d_strided_data *strided, struct wined3d_stream_info *stream_info)
347 unsigned int i;
349 memset(stream_info, 0, sizeof(*stream_info));
351 if (strided->position.data)
352 stream_info_element_from_strided(gl_info, &strided->position, &stream_info->elements[WINED3D_FFP_POSITION]);
353 if (strided->normal.data)
354 stream_info_element_from_strided(gl_info, &strided->normal, &stream_info->elements[WINED3D_FFP_NORMAL]);
355 if (strided->diffuse.data)
356 stream_info_element_from_strided(gl_info, &strided->diffuse, &stream_info->elements[WINED3D_FFP_DIFFUSE]);
357 if (strided->specular.data)
358 stream_info_element_from_strided(gl_info, &strided->specular, &stream_info->elements[WINED3D_FFP_SPECULAR]);
360 for (i = 0; i < WINED3DDP_MAXTEXCOORD; ++i)
362 if (strided->tex_coords[i].data)
363 stream_info_element_from_strided(gl_info, &strided->tex_coords[i],
364 &stream_info->elements[WINED3D_FFP_TEXCOORD0 + i]);
367 stream_info->position_transformed = strided->position_transformed;
369 for (i = 0; i < sizeof(stream_info->elements) / sizeof(*stream_info->elements); ++i)
371 if (!stream_info->elements[i].format) continue;
373 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
374 && stream_info->elements[i].format->id == WINED3DFMT_B8G8R8A8_UNORM)
376 stream_info->swizzle_map |= 1 << i;
378 stream_info->use_map |= 1 << i;
382 static void device_trace_strided_stream_info(const struct wined3d_stream_info *stream_info)
384 TRACE("Strided Data:\n");
385 TRACE_STRIDED(stream_info, WINED3D_FFP_POSITION);
386 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDWEIGHT);
387 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDINDICES);
388 TRACE_STRIDED(stream_info, WINED3D_FFP_NORMAL);
389 TRACE_STRIDED(stream_info, WINED3D_FFP_PSIZE);
390 TRACE_STRIDED(stream_info, WINED3D_FFP_DIFFUSE);
391 TRACE_STRIDED(stream_info, WINED3D_FFP_SPECULAR);
392 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD0);
393 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD1);
394 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD2);
395 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD3);
396 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD4);
397 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD5);
398 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD6);
399 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD7);
402 /* Context activation is done by the caller. */
403 void device_update_stream_info(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
405 struct wined3d_stream_info *stream_info = &device->strided_streams;
406 const struct wined3d_state *state = &device->stateBlock->state;
407 DWORD prev_all_vbo = stream_info->all_vbo;
409 if (device->up_strided)
411 /* Note: this is a ddraw fixed-function code path. */
412 TRACE("=============================== Strided Input ================================\n");
413 device_stream_info_from_strided(gl_info, device->up_strided, stream_info);
414 if (TRACE_ON(d3d)) device_trace_strided_stream_info(stream_info);
416 else
418 TRACE("============================= Vertex Declaration =============================\n");
419 device_stream_info_from_declaration(device, stream_info);
422 if (state->vertex_shader && !stream_info->position_transformed)
424 if (state->vertex_declaration->half_float_conv_needed && !stream_info->all_vbo)
426 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
427 device->useDrawStridedSlow = TRUE;
429 else
431 device->useDrawStridedSlow = FALSE;
434 else
436 WORD slow_mask = (1 << WINED3D_FFP_PSIZE);
437 slow_mask |= -!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
438 & ((1 << WINED3D_FFP_DIFFUSE) | (1 << WINED3D_FFP_SPECULAR));
440 if ((stream_info->position_transformed || (stream_info->use_map & slow_mask)) && !stream_info->all_vbo)
442 device->useDrawStridedSlow = TRUE;
444 else
446 device->useDrawStridedSlow = FALSE;
450 if (state->index_buffer && !state->user_stream)
452 if (prev_all_vbo != stream_info->all_vbo)
453 device_invalidate_state(device, STATE_INDEXBUFFER);
454 if (stream_info->all_vbo)
455 wined3d_buffer_preload(state->index_buffer);
456 else
457 buffer_get_sysmem(state->index_buffer, gl_info);
461 static void device_preload_texture(const struct wined3d_state *state, unsigned int idx)
463 struct wined3d_texture *texture;
464 enum WINED3DSRGB srgb;
466 if (!(texture = state->textures[idx])) return;
467 srgb = state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE] ? SRGB_SRGB : SRGB_RGB;
468 texture->texture_ops->texture_preload(texture, srgb);
471 void device_preload_textures(const struct wined3d_device *device)
473 const struct wined3d_state *state = &device->stateBlock->state;
474 unsigned int i;
476 if (use_vs(state))
478 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
480 if (state->vertex_shader->reg_maps.sampler_type[i])
481 device_preload_texture(state, MAX_FRAGMENT_SAMPLERS + i);
485 if (use_ps(state))
487 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
489 if (state->pixel_shader->reg_maps.sampler_type[i])
490 device_preload_texture(state, i);
493 else
495 WORD ffu_map = device->fixed_function_usage_map;
497 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
499 if (ffu_map & 1)
500 device_preload_texture(state, i);
505 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
507 struct wined3d_context **new_array;
509 TRACE("Adding context %p.\n", context);
511 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
512 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
513 sizeof(*new_array) * (device->context_count + 1));
515 if (!new_array)
517 ERR("Failed to grow the context array.\n");
518 return FALSE;
521 new_array[device->context_count++] = context;
522 device->contexts = new_array;
523 return TRUE;
526 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
528 struct wined3d_context **new_array;
529 BOOL found = FALSE;
530 UINT i;
532 TRACE("Removing context %p.\n", context);
534 for (i = 0; i < device->context_count; ++i)
536 if (device->contexts[i] == context)
538 found = TRUE;
539 break;
543 if (!found)
545 ERR("Context %p doesn't exist in context array.\n", context);
546 return;
549 if (!--device->context_count)
551 HeapFree(GetProcessHeap(), 0, device->contexts);
552 device->contexts = NULL;
553 return;
556 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
557 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
558 if (!new_array)
560 ERR("Failed to shrink context array. Oh well.\n");
561 return;
564 device->contexts = new_array;
567 /* Do not call while under the GL lock. */
568 void device_switch_onscreen_ds(struct wined3d_device *device,
569 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
571 if (device->onscreen_depth_stencil)
573 surface_load_ds_location(device->onscreen_depth_stencil, context, SFLAG_INTEXTURE);
575 surface_modify_ds_location(device->onscreen_depth_stencil, SFLAG_INTEXTURE,
576 device->onscreen_depth_stencil->ds_current_size.cx,
577 device->onscreen_depth_stencil->ds_current_size.cy);
578 wined3d_surface_decref(device->onscreen_depth_stencil);
580 device->onscreen_depth_stencil = depth_stencil;
581 wined3d_surface_incref(device->onscreen_depth_stencil);
584 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
586 /* partial draw rect */
587 if (draw_rect->left || draw_rect->top
588 || draw_rect->right < target->resource.width
589 || draw_rect->bottom < target->resource.height)
590 return FALSE;
592 /* partial clear rect */
593 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
594 || clear_rect->right < target->resource.width
595 || clear_rect->bottom < target->resource.height))
596 return FALSE;
598 return TRUE;
601 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
602 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
604 RECT current_rect, r;
606 if (ds->flags & location)
607 SetRect(&current_rect, 0, 0,
608 ds->ds_current_size.cx,
609 ds->ds_current_size.cy);
610 else
611 SetRectEmpty(&current_rect);
613 IntersectRect(&r, draw_rect, &current_rect);
614 if (EqualRect(&r, draw_rect))
616 /* current_rect ⊇ draw_rect, modify only. */
617 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
618 return;
621 if (EqualRect(&r, &current_rect))
623 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
625 if (!clear_rect)
627 /* Full clear, modify only. */
628 *out_rect = *draw_rect;
629 return;
632 IntersectRect(&r, draw_rect, clear_rect);
633 if (EqualRect(&r, draw_rect))
635 /* clear_rect ⊇ draw_rect, modify only. */
636 *out_rect = *draw_rect;
637 return;
641 /* Full load. */
642 surface_load_ds_location(ds, context, location);
643 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
646 /* Do not call while under the GL lock. */
647 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
648 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
649 float depth, DWORD stencil)
651 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
652 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
653 UINT drawable_width, drawable_height;
654 struct wined3d_context *context;
655 GLbitfield clear_mask = 0;
656 BOOL render_offscreen;
657 unsigned int i;
658 RECT ds_rect;
660 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
661 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
662 * for the cleared parts, and the untouched parts.
664 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
665 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
666 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
667 * checking all this if the dest surface is in the drawable anyway. */
668 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
670 for (i = 0; i < rt_count; ++i)
672 struct wined3d_surface *rt = fb->render_targets[i];
673 if (rt)
674 surface_load_location(rt, rt->draw_binding, NULL);
678 context = context_acquire(device, target);
679 if (!context->valid)
681 context_release(context);
682 WARN("Invalid context, skipping clear.\n");
683 return;
686 if (target)
688 render_offscreen = context->render_offscreen;
689 target->get_drawable_size(context, &drawable_width, &drawable_height);
691 else
693 render_offscreen = TRUE;
694 drawable_width = fb->depth_stencil->pow2Width;
695 drawable_height = fb->depth_stencil->pow2Height;
698 if (flags & WINED3DCLEAR_ZBUFFER)
700 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
702 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
703 device_switch_onscreen_ds(device, context, fb->depth_stencil);
704 prepare_ds_clear(fb->depth_stencil, context, location,
705 draw_rect, rect_count, clear_rect, &ds_rect);
708 if (!context_apply_clear_state(context, device, rt_count, fb))
710 context_release(context);
711 WARN("Failed to apply clear state, skipping clear.\n");
712 return;
715 ENTER_GL();
717 /* Only set the values up once, as they are not changing. */
718 if (flags & WINED3DCLEAR_STENCIL)
720 if (context->gl_info->supported[EXT_STENCIL_TWO_SIDE])
722 glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
723 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
725 glStencilMask(~0U);
726 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
727 glClearStencil(stencil);
728 checkGLcall("glClearStencil");
729 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
732 if (flags & WINED3DCLEAR_ZBUFFER)
734 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
736 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
738 glDepthMask(GL_TRUE);
739 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
740 glClearDepth(depth);
741 checkGLcall("glClearDepth");
742 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
745 if (flags & WINED3DCLEAR_TARGET)
747 for (i = 0; i < rt_count; ++i)
749 struct wined3d_surface *rt = fb->render_targets[i];
751 if (rt)
752 surface_modify_location(rt, rt->draw_binding, TRUE);
755 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
756 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
757 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
758 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
759 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
760 glClearColor(color->r, color->g, color->b, color->a);
761 checkGLcall("glClearColor");
762 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
765 if (!clear_rect)
767 if (render_offscreen)
769 glScissor(draw_rect->left, draw_rect->top,
770 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
772 else
774 glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
775 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
777 checkGLcall("glScissor");
778 glClear(clear_mask);
779 checkGLcall("glClear");
781 else
783 RECT current_rect;
785 /* Now process each rect in turn. */
786 for (i = 0; i < rect_count; ++i)
788 /* Note that GL uses lower left, width/height. */
789 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
791 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
792 wine_dbgstr_rect(&clear_rect[i]),
793 wine_dbgstr_rect(&current_rect));
795 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
796 * The rectangle is not cleared, no error is returned, but further rectangles are
797 * still cleared if they are valid. */
798 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
800 TRACE("Rectangle with negative dimensions, ignoring.\n");
801 continue;
804 if (render_offscreen)
806 glScissor(current_rect.left, current_rect.top,
807 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
809 else
811 glScissor(current_rect.left, drawable_height - current_rect.bottom,
812 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
814 checkGLcall("glScissor");
816 glClear(clear_mask);
817 checkGLcall("glClear");
821 LEAVE_GL();
823 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
824 && target->container.type == WINED3D_CONTAINER_SWAPCHAIN
825 && target->container.u.swapchain->front_buffer == target))
826 wglFlush(); /* Flush to ensure ordering across contexts. */
828 context_release(context);
831 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
833 ULONG refcount = InterlockedIncrement(&device->ref);
835 TRACE("%p increasing refcount to %u.\n", device, refcount);
837 return refcount;
840 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
842 ULONG refcount = InterlockedDecrement(&device->ref);
844 TRACE("%p decreasing refcount to %u.\n", device, refcount);
846 if (!refcount)
848 struct wined3d_stateblock *stateblock;
849 UINT i;
851 if (wined3d_stateblock_decref(device->updateStateBlock)
852 && device->updateStateBlock != device->stateBlock)
853 FIXME("Something's still holding the update stateblock.\n");
854 device->updateStateBlock = NULL;
856 stateblock = device->stateBlock;
857 device->stateBlock = NULL;
858 if (wined3d_stateblock_decref(stateblock))
859 FIXME("Something's still holding the stateblock.\n");
861 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
863 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
864 device->multistate_funcs[i] = NULL;
867 if (!list_empty(&device->resources))
869 struct wined3d_resource *resource;
871 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
873 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
875 FIXME("Leftover resource %p with type %s (%#x).\n",
876 resource, debug_d3dresourcetype(resource->type), resource->type);
880 if (device->contexts)
881 ERR("Context array not freed!\n");
882 if (device->hardwareCursor)
883 DestroyCursor(device->hardwareCursor);
884 device->hardwareCursor = 0;
886 wined3d_decref(device->wined3d);
887 device->wined3d = NULL;
888 HeapFree(GetProcessHeap(), 0, device);
889 TRACE("Freed device %p.\n", device);
892 return refcount;
895 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
897 TRACE("device %p.\n", device);
899 return device->swapchain_count;
902 HRESULT CDECL wined3d_device_get_swapchain(const struct wined3d_device *device,
903 UINT swapchain_idx, struct wined3d_swapchain **swapchain)
905 TRACE("device %p, swapchain_idx %u, swapchain %p.\n",
906 device, swapchain_idx, swapchain);
908 if (swapchain_idx >= device->swapchain_count)
910 WARN("swapchain_idx %u >= swapchain_count %u.\n",
911 swapchain_idx, device->swapchain_count);
912 *swapchain = NULL;
914 return WINED3DERR_INVALIDCALL;
917 *swapchain = device->swapchains[swapchain_idx];
918 wined3d_swapchain_incref(*swapchain);
919 TRACE("Returning %p.\n", *swapchain);
921 return WINED3D_OK;
924 static void device_load_logo(struct wined3d_device *device, const char *filename)
926 struct wined3d_color_key color_key;
927 HBITMAP hbm;
928 BITMAP bm;
929 HRESULT hr;
930 HDC dcb = NULL, dcs = NULL;
932 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
933 if(hbm)
935 GetObjectA(hbm, sizeof(BITMAP), &bm);
936 dcb = CreateCompatibleDC(NULL);
937 if(!dcb) goto out;
938 SelectObject(dcb, hbm);
940 else
942 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
943 * couldn't be loaded
945 memset(&bm, 0, sizeof(bm));
946 bm.bmWidth = 32;
947 bm.bmHeight = 32;
950 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0, 0,
951 WINED3D_POOL_DEFAULT, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_TYPE_OPENGL, WINED3D_SURFACE_MAPPABLE,
952 NULL, &wined3d_null_parent_ops, &device->logo_surface);
953 if (FAILED(hr))
955 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
956 goto out;
959 if (dcb)
961 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
962 goto out;
963 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
964 wined3d_surface_releasedc(device->logo_surface, dcs);
966 color_key.color_space_low_value = 0;
967 color_key.color_space_high_value = 0;
968 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
970 else
972 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
973 /* Fill the surface with a white color to show that wined3d is there */
974 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
977 out:
978 if (dcb) DeleteDC(dcb);
979 if (hbm) DeleteObject(hbm);
982 /* Context activation is done by the caller. */
983 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
985 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
986 unsigned int i, j, count;
987 /* Under DirectX you can sample even if no texture is bound, whereas
988 * OpenGL will only allow that when a valid texture is bound.
989 * We emulate this by creating dummy textures and binding them
990 * to each texture stage when the currently set D3D texture is NULL. */
991 ENTER_GL();
993 if (gl_info->supported[APPLE_CLIENT_STORAGE])
995 /* The dummy texture does not have client storage backing */
996 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
997 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1000 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1001 for (i = 0; i < count; ++i)
1003 DWORD color = 0x000000ff;
1005 /* Make appropriate texture active */
1006 context_active_texture(context, gl_info, i);
1008 glGenTextures(1, &device->dummy_texture_2d[i]);
1009 checkGLcall("glGenTextures");
1010 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
1012 glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
1013 checkGLcall("glBindTexture");
1015 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1016 checkGLcall("glTexImage2D");
1018 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1020 glGenTextures(1, &device->dummy_texture_rect[i]);
1021 checkGLcall("glGenTextures");
1022 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
1024 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1025 checkGLcall("glBindTexture");
1027 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1028 checkGLcall("glTexImage2D");
1031 if (gl_info->supported[EXT_TEXTURE3D])
1033 glGenTextures(1, &device->dummy_texture_3d[i]);
1034 checkGLcall("glGenTextures");
1035 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
1037 glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1038 checkGLcall("glBindTexture");
1040 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
1041 checkGLcall("glTexImage3D");
1044 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1046 glGenTextures(1, &device->dummy_texture_cube[i]);
1047 checkGLcall("glGenTextures");
1048 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
1050 glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1051 checkGLcall("glBindTexture");
1053 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
1055 glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1056 checkGLcall("glTexImage2D");
1061 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1063 /* Re-enable because if supported it is enabled by default */
1064 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1065 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1068 LEAVE_GL();
1071 /* Context activation is done by the caller. */
1072 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
1074 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1076 ENTER_GL();
1077 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1079 glDeleteTextures(count, device->dummy_texture_cube);
1080 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
1083 if (gl_info->supported[EXT_TEXTURE3D])
1085 glDeleteTextures(count, device->dummy_texture_3d);
1086 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
1089 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1091 glDeleteTextures(count, device->dummy_texture_rect);
1092 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
1095 glDeleteTextures(count, device->dummy_texture_2d);
1096 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
1097 LEAVE_GL();
1099 memset(device->dummy_texture_cube, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_cube));
1100 memset(device->dummy_texture_3d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_3d));
1101 memset(device->dummy_texture_rect, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_rect));
1102 memset(device->dummy_texture_2d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_2d));
1105 static LONG fullscreen_style(LONG style)
1107 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1108 style |= WS_POPUP | WS_SYSMENU;
1109 style &= ~(WS_CAPTION | WS_THICKFRAME);
1111 return style;
1114 static LONG fullscreen_exstyle(LONG exstyle)
1116 /* Filter out window decorations. */
1117 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1119 return exstyle;
1122 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1124 BOOL filter_messages;
1125 LONG style, exstyle;
1127 TRACE("Setting up window %p for fullscreen mode.\n", window);
1129 if (device->style || device->exStyle)
1131 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1132 window, device->style, device->exStyle);
1135 device->style = GetWindowLongW(window, GWL_STYLE);
1136 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1138 style = fullscreen_style(device->style);
1139 exstyle = fullscreen_exstyle(device->exStyle);
1141 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1142 device->style, device->exStyle, style, exstyle);
1144 filter_messages = device->filter_messages;
1145 device->filter_messages = TRUE;
1147 SetWindowLongW(window, GWL_STYLE, style);
1148 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1149 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1151 device->filter_messages = filter_messages;
1154 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1156 BOOL filter_messages;
1157 LONG style, exstyle;
1159 if (!device->style && !device->exStyle) return;
1161 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1162 window, device->style, device->exStyle);
1164 style = GetWindowLongW(window, GWL_STYLE);
1165 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1167 filter_messages = device->filter_messages;
1168 device->filter_messages = TRUE;
1170 /* Only restore the style if the application didn't modify it during the
1171 * fullscreen phase. Some applications change it before calling Reset()
1172 * when switching between windowed and fullscreen modes (HL2), some
1173 * depend on the original style (Eve Online). */
1174 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1176 SetWindowLongW(window, GWL_STYLE, device->style);
1177 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1179 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1181 device->filter_messages = filter_messages;
1183 /* Delete the old values. */
1184 device->style = 0;
1185 device->exStyle = 0;
1188 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1190 TRACE("device %p, window %p.\n", device, window);
1192 if (!wined3d_register_window(window, device))
1194 ERR("Failed to register window %p.\n", window);
1195 return E_FAIL;
1198 InterlockedExchangePointer((void **)&device->focus_window, window);
1199 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1201 return WINED3D_OK;
1204 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1206 TRACE("device %p.\n", device);
1208 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1209 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1212 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1213 struct wined3d_swapchain_desc *swapchain_desc)
1215 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1216 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1217 struct wined3d_swapchain *swapchain = NULL;
1218 struct wined3d_context *context;
1219 HRESULT hr;
1220 DWORD state;
1221 unsigned int i;
1223 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1225 if (device->d3d_initialized)
1226 return WINED3DERR_INVALIDCALL;
1227 if (!device->adapter->opengl)
1228 return WINED3DERR_INVALIDCALL;
1230 device->valid_rt_mask = 0;
1231 for (i = 0; i < gl_info->limits.buffers; ++i)
1232 device->valid_rt_mask |= (1 << i);
1233 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1234 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1236 /* Initialize the texture unit mapping to a 1:1 mapping */
1237 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1239 if (state < gl_info->limits.fragment_samplers)
1241 device->texUnitMap[state] = state;
1242 device->rev_tex_unit_map[state] = state;
1244 else
1246 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1247 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1251 /* Setup the implicit swapchain. This also initializes a context. */
1252 TRACE("Creating implicit swapchain\n");
1253 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1254 swapchain_desc, &swapchain);
1255 if (FAILED(hr))
1257 WARN("Failed to create implicit swapchain\n");
1258 goto err_out;
1261 device->swapchain_count = 1;
1262 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1263 if (!device->swapchains)
1265 ERR("Out of memory!\n");
1266 goto err_out;
1268 device->swapchains[0] = swapchain;
1270 if (swapchain->back_buffers && swapchain->back_buffers[0])
1272 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1273 device->fb.render_targets[0] = swapchain->back_buffers[0];
1275 else
1277 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1278 device->fb.render_targets[0] = swapchain->front_buffer;
1280 wined3d_surface_incref(device->fb.render_targets[0]);
1282 /* Depth Stencil support */
1283 device->fb.depth_stencil = device->auto_depth_stencil;
1284 if (device->fb.depth_stencil)
1285 wined3d_surface_incref(device->fb.depth_stencil);
1287 hr = device->shader_backend->shader_alloc_private(device);
1288 if (FAILED(hr))
1290 TRACE("Shader private data couldn't be allocated\n");
1291 goto err_out;
1293 hr = device->frag_pipe->alloc_private(device);
1294 if (FAILED(hr))
1296 TRACE("Fragment pipeline private data couldn't be allocated\n");
1297 goto err_out;
1299 hr = device->blitter->alloc_private(device);
1300 if (FAILED(hr))
1302 TRACE("Blitter private data couldn't be allocated\n");
1303 goto err_out;
1306 /* Set up some starting GL setup */
1308 /* Setup all the devices defaults */
1309 stateblock_init_default_state(device->stateBlock);
1311 context = context_acquire(device, swapchain->front_buffer);
1313 create_dummy_textures(device, context);
1315 ENTER_GL();
1317 /* Initialize the current view state */
1318 device->view_ident = 1;
1319 device->contexts[0]->last_was_rhw = 0;
1321 switch (wined3d_settings.offscreen_rendering_mode)
1323 case ORM_FBO:
1324 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1325 break;
1327 case ORM_BACKBUFFER:
1329 if (context_get_current()->aux_buffers > 0)
1331 TRACE("Using auxiliary buffer for offscreen rendering\n");
1332 device->offscreenBuffer = GL_AUX0;
1334 else
1336 TRACE("Using back buffer for offscreen rendering\n");
1337 device->offscreenBuffer = GL_BACK;
1342 TRACE("All defaults now set up, leaving 3D init.\n");
1343 LEAVE_GL();
1345 context_release(context);
1347 /* Clear the screen */
1348 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1349 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1350 &black, 1.0f, 0);
1352 device->d3d_initialized = TRUE;
1354 if (wined3d_settings.logo)
1355 device_load_logo(device, wined3d_settings.logo);
1356 return WINED3D_OK;
1358 err_out:
1359 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1360 HeapFree(GetProcessHeap(), 0, device->swapchains);
1361 device->swapchain_count = 0;
1362 if (swapchain)
1363 wined3d_swapchain_decref(swapchain);
1364 if (device->blit_priv)
1365 device->blitter->free_private(device);
1366 if (device->fragment_priv)
1367 device->frag_pipe->free_private(device);
1368 if (device->shader_priv)
1369 device->shader_backend->shader_free_private(device);
1371 return hr;
1374 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1375 struct wined3d_swapchain_desc *swapchain_desc)
1377 struct wined3d_swapchain *swapchain = NULL;
1378 HRESULT hr;
1380 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1382 /* Setup the implicit swapchain */
1383 TRACE("Creating implicit swapchain\n");
1384 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1385 swapchain_desc, &swapchain);
1386 if (FAILED(hr))
1388 WARN("Failed to create implicit swapchain\n");
1389 goto err_out;
1392 device->swapchain_count = 1;
1393 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1394 if (!device->swapchains)
1396 ERR("Out of memory!\n");
1397 goto err_out;
1399 device->swapchains[0] = swapchain;
1400 return WINED3D_OK;
1402 err_out:
1403 wined3d_swapchain_decref(swapchain);
1404 return hr;
1407 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1409 struct wined3d_resource *resource, *cursor;
1410 const struct wined3d_gl_info *gl_info;
1411 struct wined3d_context *context;
1412 struct wined3d_surface *surface;
1413 UINT i;
1415 TRACE("device %p.\n", device);
1417 if (!device->d3d_initialized)
1418 return WINED3DERR_INVALIDCALL;
1420 /* Force making the context current again, to verify it is still valid
1421 * (workaround for broken drivers) */
1422 context_set_current(NULL);
1423 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1424 * it was created. Thus make sure a context is active for the glDelete* calls
1426 context = context_acquire(device, NULL);
1427 gl_info = context->gl_info;
1429 if (device->logo_surface)
1430 wined3d_surface_decref(device->logo_surface);
1432 stateblock_unbind_resources(device->stateBlock);
1434 /* Unload resources */
1435 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1437 TRACE("Unloading resource %p.\n", resource);
1439 resource->resource_ops->resource_unload(resource);
1442 TRACE("Deleting high order patches\n");
1443 for (i = 0; i < PATCHMAP_SIZE; ++i)
1445 struct wined3d_rect_patch *patch;
1446 struct list *e1, *e2;
1448 LIST_FOR_EACH_SAFE(e1, e2, &device->patches[i])
1450 patch = LIST_ENTRY(e1, struct wined3d_rect_patch, entry);
1451 wined3d_device_delete_patch(device, patch->Handle);
1455 /* Delete the mouse cursor texture */
1456 if (device->cursorTexture)
1458 ENTER_GL();
1459 glDeleteTextures(1, &device->cursorTexture);
1460 LEAVE_GL();
1461 device->cursorTexture = 0;
1464 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1465 * private data, it might contain opengl pointers
1467 if (device->depth_blt_texture)
1469 ENTER_GL();
1470 glDeleteTextures(1, &device->depth_blt_texture);
1471 LEAVE_GL();
1472 device->depth_blt_texture = 0;
1475 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1476 device->blitter->free_private(device);
1477 device->frag_pipe->free_private(device);
1478 device->shader_backend->shader_free_private(device);
1480 /* Release the buffers (with sanity checks)*/
1481 if (device->onscreen_depth_stencil)
1483 surface = device->onscreen_depth_stencil;
1484 device->onscreen_depth_stencil = NULL;
1485 wined3d_surface_decref(surface);
1488 if (device->fb.depth_stencil)
1490 surface = device->fb.depth_stencil;
1492 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1494 device->fb.depth_stencil = NULL;
1495 wined3d_surface_decref(surface);
1498 if (device->auto_depth_stencil)
1500 surface = device->auto_depth_stencil;
1501 device->auto_depth_stencil = NULL;
1502 if (wined3d_surface_decref(surface))
1503 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1506 for (i = 1; i < gl_info->limits.buffers; ++i)
1508 wined3d_device_set_render_target(device, i, NULL, FALSE);
1511 surface = device->fb.render_targets[0];
1512 TRACE("Setting rendertarget 0 to NULL\n");
1513 device->fb.render_targets[0] = NULL;
1514 TRACE("Releasing the render target at %p\n", surface);
1515 wined3d_surface_decref(surface);
1517 context_release(context);
1519 for (i = 0; i < device->swapchain_count; ++i)
1521 TRACE("Releasing the implicit swapchain %u.\n", i);
1522 if (wined3d_swapchain_decref(device->swapchains[i]))
1523 FIXME("Something's still holding the implicit swapchain.\n");
1526 HeapFree(GetProcessHeap(), 0, device->swapchains);
1527 device->swapchains = NULL;
1528 device->swapchain_count = 0;
1530 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1531 device->fb.render_targets = NULL;
1533 device->d3d_initialized = FALSE;
1535 return WINED3D_OK;
1538 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1540 unsigned int i;
1542 for (i = 0; i < device->swapchain_count; ++i)
1544 TRACE("Releasing the implicit swapchain %u.\n", i);
1545 if (wined3d_swapchain_decref(device->swapchains[i]))
1546 FIXME("Something's still holding the implicit swapchain.\n");
1549 HeapFree(GetProcessHeap(), 0, device->swapchains);
1550 device->swapchains = NULL;
1551 device->swapchain_count = 0;
1552 return WINED3D_OK;
1555 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1556 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1557 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1559 * There is no way to deactivate thread safety once it is enabled.
1561 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1563 TRACE("device %p.\n", device);
1565 /* For now just store the flag (needed in case of ddraw). */
1566 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1569 HRESULT CDECL wined3d_device_set_display_mode(struct wined3d_device *device,
1570 UINT swapchain_idx, const struct wined3d_display_mode *mode)
1572 struct wined3d_adapter *adapter = device->adapter;
1573 const struct wined3d_format *format = wined3d_get_format(&adapter->gl_info, mode->format_id);
1574 DEVMODEW devmode;
1575 LONG ret;
1576 RECT clip_rc;
1578 TRACE("device %p, swapchain_idx %u, mode %p (%ux%u@%u %s).\n", device, swapchain_idx, mode,
1579 mode->width, mode->height, mode->refresh_rate, debug_d3dformat(mode->format_id));
1581 /* Resize the screen even without a window:
1582 * The app could have unset it with SetCooperativeLevel, but not called
1583 * RestoreDisplayMode first. Then the release will call RestoreDisplayMode,
1584 * but we don't have any hwnd
1587 memset(&devmode, 0, sizeof(devmode));
1588 devmode.dmSize = sizeof(devmode);
1589 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1590 devmode.dmBitsPerPel = format->byte_count * CHAR_BIT;
1591 devmode.dmPelsWidth = mode->width;
1592 devmode.dmPelsHeight = mode->height;
1594 devmode.dmDisplayFrequency = mode->refresh_rate;
1595 if (mode->refresh_rate)
1596 devmode.dmFields |= DM_DISPLAYFREQUENCY;
1598 /* Only change the mode if necessary */
1599 if (adapter->screen_size.cx == mode->width && adapter->screen_size.cy == mode->height
1600 && adapter->screen_format == mode->format_id && !mode->refresh_rate)
1601 return WINED3D_OK;
1603 ret = ChangeDisplaySettingsExW(NULL, &devmode, NULL, CDS_FULLSCREEN, NULL);
1604 if (ret != DISP_CHANGE_SUCCESSFUL)
1606 if (devmode.dmDisplayFrequency)
1608 WARN("ChangeDisplaySettingsExW failed, trying without the refresh rate\n");
1609 devmode.dmFields &= ~DM_DISPLAYFREQUENCY;
1610 devmode.dmDisplayFrequency = 0;
1611 ret = ChangeDisplaySettingsExW(NULL, &devmode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL;
1613 if(ret != DISP_CHANGE_SUCCESSFUL) {
1614 return WINED3DERR_NOTAVAILABLE;
1618 /* Store the new values */
1619 adapter->screen_size.cx = mode->width;
1620 adapter->screen_size.cy = mode->height;
1621 adapter->screen_format = mode->format_id;
1623 /* And finally clip mouse to our screen */
1624 SetRect(&clip_rc, 0, 0, mode->width, mode->height);
1625 ClipCursor(&clip_rc);
1627 return WINED3D_OK;
1630 HRESULT CDECL wined3d_device_get_wined3d(const struct wined3d_device *device, struct wined3d **wined3d)
1632 TRACE("device %p, wined3d %p.\n", device, wined3d);
1634 *wined3d = device->wined3d;
1635 wined3d_incref(*wined3d);
1637 TRACE("Returning %p.\n", *wined3d);
1639 return WINED3D_OK;
1642 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1644 TRACE("device %p.\n", device);
1646 TRACE("Emulating %d MB, returning %d MB left.\n",
1647 device->adapter->TextureRam / (1024 * 1024),
1648 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1650 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1653 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1654 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1656 struct wined3d_stream_state *stream;
1657 struct wined3d_buffer *prev_buffer;
1659 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1660 device, stream_idx, buffer, offset, stride);
1662 if (stream_idx >= MAX_STREAMS)
1664 WARN("Stream index %u out of range.\n", stream_idx);
1665 return WINED3DERR_INVALIDCALL;
1667 else if (offset & 0x3)
1669 WARN("Offset %u is not 4 byte aligned.\n", offset);
1670 return WINED3DERR_INVALIDCALL;
1673 stream = &device->updateStateBlock->state.streams[stream_idx];
1674 prev_buffer = stream->buffer;
1676 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1678 if (prev_buffer == buffer
1679 && stream->stride == stride
1680 && stream->offset == offset)
1682 TRACE("Application is setting the old values over, nothing to do.\n");
1683 return WINED3D_OK;
1686 stream->buffer = buffer;
1687 if (buffer)
1689 stream->stride = stride;
1690 stream->offset = offset;
1693 /* Handle recording of state blocks. */
1694 if (device->isRecordingState)
1696 TRACE("Recording... not performing anything.\n");
1697 if (buffer)
1698 wined3d_buffer_incref(buffer);
1699 if (prev_buffer)
1700 wined3d_buffer_decref(prev_buffer);
1701 return WINED3D_OK;
1704 if (buffer)
1706 InterlockedIncrement(&buffer->resource.bind_count);
1707 wined3d_buffer_incref(buffer);
1709 if (prev_buffer)
1711 InterlockedDecrement(&prev_buffer->resource.bind_count);
1712 wined3d_buffer_decref(prev_buffer);
1715 device_invalidate_state(device, STATE_STREAMSRC);
1717 return WINED3D_OK;
1720 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1721 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1723 struct wined3d_stream_state *stream;
1725 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1726 device, stream_idx, buffer, offset, stride);
1728 if (stream_idx >= MAX_STREAMS)
1730 WARN("Stream index %u out of range.\n", stream_idx);
1731 return WINED3DERR_INVALIDCALL;
1734 stream = &device->stateBlock->state.streams[stream_idx];
1735 *buffer = stream->buffer;
1736 if (*buffer)
1737 wined3d_buffer_incref(*buffer);
1738 if (offset)
1739 *offset = stream->offset;
1740 *stride = stream->stride;
1742 return WINED3D_OK;
1745 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1747 struct wined3d_stream_state *stream;
1748 UINT old_flags, old_freq;
1750 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1752 /* Verify input. At least in d3d9 this is invalid. */
1753 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1755 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1756 return WINED3DERR_INVALIDCALL;
1758 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1760 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1761 return WINED3DERR_INVALIDCALL;
1763 if (!divider)
1765 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1766 return WINED3DERR_INVALIDCALL;
1769 stream = &device->updateStateBlock->state.streams[stream_idx];
1770 old_flags = stream->flags;
1771 old_freq = stream->frequency;
1773 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1774 stream->frequency = divider & 0x7fffff;
1776 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1778 if (stream->frequency != old_freq || stream->flags != old_flags)
1779 device_invalidate_state(device, STATE_STREAMSRC);
1781 return WINED3D_OK;
1784 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1785 UINT stream_idx, UINT *divider)
1787 struct wined3d_stream_state *stream;
1789 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1791 stream = &device->updateStateBlock->state.streams[stream_idx];
1792 *divider = stream->flags | stream->frequency;
1794 TRACE("Returning %#x.\n", *divider);
1796 return WINED3D_OK;
1799 HRESULT CDECL wined3d_device_set_transform(struct wined3d_device *device,
1800 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1802 TRACE("device %p, state %s, matrix %p.\n",
1803 device, debug_d3dtstype(d3dts), matrix);
1804 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1805 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1806 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1807 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1809 /* Handle recording of state blocks. */
1810 if (device->isRecordingState)
1812 TRACE("Recording... not performing anything.\n");
1813 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1814 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1815 return WINED3D_OK;
1818 /* If the new matrix is the same as the current one,
1819 * we cut off any further processing. this seems to be a reasonable
1820 * optimization because as was noticed, some apps (warcraft3 for example)
1821 * tend towards setting the same matrix repeatedly for some reason.
1823 * From here on we assume that the new matrix is different, wherever it matters. */
1824 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1826 TRACE("The application is setting the same matrix over again.\n");
1827 return WINED3D_OK;
1830 device->stateBlock->state.transforms[d3dts] = *matrix;
1831 if (d3dts == WINED3D_TS_VIEW)
1832 device->view_ident = !memcmp(matrix, &identity, sizeof(identity));
1834 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1835 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1837 return WINED3D_OK;
1841 HRESULT CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1842 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1844 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1846 *matrix = device->stateBlock->state.transforms[state];
1848 return WINED3D_OK;
1851 HRESULT CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1852 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1854 const struct wined3d_matrix *mat = NULL;
1855 struct wined3d_matrix temp;
1857 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1859 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1860 * below means it will be recorded in a state block change, but it
1861 * works regardless where it is recorded.
1862 * If this is found to be wrong, change to StateBlock. */
1863 if (state > HIGHEST_TRANSFORMSTATE)
1865 WARN("Unhandled transform state %#x.\n", state);
1866 return WINED3D_OK;
1869 mat = &device->updateStateBlock->state.transforms[state];
1870 multiply_matrix(&temp, mat, matrix);
1872 /* Apply change via set transform - will reapply to eg. lights this way. */
1873 return wined3d_device_set_transform(device, state, &temp);
1876 /* Note lights are real special cases. Although the device caps state only
1877 * e.g. 8 are supported, you can reference any indexes you want as long as
1878 * that number max are enabled at any one point in time. Therefore since the
1879 * indices can be anything, we need a hashmap of them. However, this causes
1880 * stateblock problems. When capturing the state block, I duplicate the
1881 * hashmap, but when recording, just build a chain pretty much of commands to
1882 * be replayed. */
1883 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1884 UINT light_idx, const struct wined3d_light *light)
1886 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1887 struct wined3d_light_info *object = NULL;
1888 struct list *e;
1889 float rho;
1891 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1893 /* Check the parameter range. Need for speed most wanted sets junk lights
1894 * which confuse the GL driver. */
1895 if (!light)
1896 return WINED3DERR_INVALIDCALL;
1898 switch (light->type)
1900 case WINED3D_LIGHT_POINT:
1901 case WINED3D_LIGHT_SPOT:
1902 case WINED3D_LIGHT_PARALLELPOINT:
1903 case WINED3D_LIGHT_GLSPOT:
1904 /* Incorrect attenuation values can cause the gl driver to crash.
1905 * Happens with Need for speed most wanted. */
1906 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1908 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1909 return WINED3DERR_INVALIDCALL;
1911 break;
1913 case WINED3D_LIGHT_DIRECTIONAL:
1914 /* Ignores attenuation */
1915 break;
1917 default:
1918 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1919 return WINED3DERR_INVALIDCALL;
1922 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1924 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1925 if (object->OriginalIndex == light_idx)
1926 break;
1927 object = NULL;
1930 if (!object)
1932 TRACE("Adding new light\n");
1933 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1934 if (!object)
1936 ERR("Out of memory error when allocating a light\n");
1937 return E_OUTOFMEMORY;
1939 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1940 object->glIndex = -1;
1941 object->OriginalIndex = light_idx;
1944 /* Initialize the object. */
1945 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1946 light_idx, light->type,
1947 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1948 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1949 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1950 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1951 light->direction.x, light->direction.y, light->direction.z);
1952 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1953 light->range, light->falloff, light->theta, light->phi);
1955 /* Save away the information. */
1956 object->OriginalParms = *light;
1958 switch (light->type)
1960 case WINED3D_LIGHT_POINT:
1961 /* Position */
1962 object->lightPosn[0] = light->position.x;
1963 object->lightPosn[1] = light->position.y;
1964 object->lightPosn[2] = light->position.z;
1965 object->lightPosn[3] = 1.0f;
1966 object->cutoff = 180.0f;
1967 /* FIXME: Range */
1968 break;
1970 case WINED3D_LIGHT_DIRECTIONAL:
1971 /* Direction */
1972 object->lightPosn[0] = -light->direction.x;
1973 object->lightPosn[1] = -light->direction.y;
1974 object->lightPosn[2] = -light->direction.z;
1975 object->lightPosn[3] = 0.0f;
1976 object->exponent = 0.0f;
1977 object->cutoff = 180.0f;
1978 break;
1980 case WINED3D_LIGHT_SPOT:
1981 /* Position */
1982 object->lightPosn[0] = light->position.x;
1983 object->lightPosn[1] = light->position.y;
1984 object->lightPosn[2] = light->position.z;
1985 object->lightPosn[3] = 1.0f;
1987 /* Direction */
1988 object->lightDirn[0] = light->direction.x;
1989 object->lightDirn[1] = light->direction.y;
1990 object->lightDirn[2] = light->direction.z;
1991 object->lightDirn[3] = 1.0f;
1993 /* opengl-ish and d3d-ish spot lights use too different models
1994 * for the light "intensity" as a function of the angle towards
1995 * the main light direction, so we only can approximate very
1996 * roughly. However, spot lights are rather rarely used in games
1997 * (if ever used at all). Furthermore if still used, probably
1998 * nobody pays attention to such details. */
1999 if (!light->falloff)
2001 /* Falloff = 0 is easy, because d3d's and opengl's spot light
2002 * equations have the falloff resp. exponent parameter as an
2003 * exponent, so the spot light lighting will always be 1.0 for
2004 * both of them, and we don't have to care for the rest of the
2005 * rather complex calculation. */
2006 object->exponent = 0.0f;
2008 else
2010 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
2011 if (rho < 0.0001f)
2012 rho = 0.0001f;
2013 object->exponent = -0.3f / logf(cosf(rho / 2));
2016 if (object->exponent > 128.0f)
2017 object->exponent = 128.0f;
2019 object->cutoff = (float)(light->phi * 90 / M_PI);
2020 /* FIXME: Range */
2021 break;
2023 default:
2024 FIXME("Unrecognized light type %#x.\n", light->type);
2027 /* Update the live definitions if the light is currently assigned a glIndex. */
2028 if (object->glIndex != -1 && !device->isRecordingState)
2029 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
2031 return WINED3D_OK;
2034 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
2035 UINT light_idx, struct wined3d_light *light)
2037 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2038 struct wined3d_light_info *light_info = NULL;
2039 struct list *e;
2041 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
2043 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2045 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2046 if (light_info->OriginalIndex == light_idx)
2047 break;
2048 light_info = NULL;
2051 if (!light_info)
2053 TRACE("Light information requested but light not defined\n");
2054 return WINED3DERR_INVALIDCALL;
2057 *light = light_info->OriginalParms;
2058 return WINED3D_OK;
2061 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
2063 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2064 struct wined3d_light_info *light_info = NULL;
2065 struct list *e;
2067 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
2069 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2071 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2072 if (light_info->OriginalIndex == light_idx)
2073 break;
2074 light_info = NULL;
2076 TRACE("Found light %p.\n", light_info);
2078 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
2079 if (!light_info)
2081 TRACE("Light enabled requested but light not defined, so defining one!\n");
2082 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
2084 /* Search for it again! Should be fairly quick as near head of list. */
2085 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2087 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2088 if (light_info->OriginalIndex == light_idx)
2089 break;
2090 light_info = NULL;
2092 if (!light_info)
2094 FIXME("Adding default lights has failed dismally\n");
2095 return WINED3DERR_INVALIDCALL;
2099 if (!enable)
2101 if (light_info->glIndex != -1)
2103 if (!device->isRecordingState)
2104 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
2106 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
2107 light_info->glIndex = -1;
2109 else
2111 TRACE("Light already disabled, nothing to do\n");
2113 light_info->enabled = FALSE;
2115 else
2117 light_info->enabled = TRUE;
2118 if (light_info->glIndex != -1)
2120 TRACE("Nothing to do as light was enabled\n");
2122 else
2124 unsigned int i;
2125 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2126 /* Find a free GL light. */
2127 for (i = 0; i < gl_info->limits.lights; ++i)
2129 if (!device->updateStateBlock->state.lights[i])
2131 device->updateStateBlock->state.lights[i] = light_info;
2132 light_info->glIndex = i;
2133 break;
2136 if (light_info->glIndex == -1)
2138 /* Our tests show that Windows returns D3D_OK in this situation, even with
2139 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
2140 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
2141 * as well for those lights.
2143 * TODO: Test how this affects rendering. */
2144 WARN("Too many concurrently active lights\n");
2145 return WINED3D_OK;
2148 /* i == light_info->glIndex */
2149 if (!device->isRecordingState)
2150 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
2154 return WINED3D_OK;
2157 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
2159 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2160 struct wined3d_light_info *light_info = NULL;
2161 struct list *e;
2163 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
2165 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2167 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2168 if (light_info->OriginalIndex == light_idx)
2169 break;
2170 light_info = NULL;
2173 if (!light_info)
2175 TRACE("Light enabled state requested but light not defined.\n");
2176 return WINED3DERR_INVALIDCALL;
2178 /* true is 128 according to SetLightEnable */
2179 *enable = light_info->enabled ? 128 : 0;
2180 return WINED3D_OK;
2183 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device, UINT plane_idx, const float *plane)
2185 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2187 /* Validate plane_idx. */
2188 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2190 TRACE("Application has requested clipplane this device doesn't support.\n");
2191 return WINED3DERR_INVALIDCALL;
2194 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2196 if (device->updateStateBlock->state.clip_planes[plane_idx][0] == plane[0]
2197 && device->updateStateBlock->state.clip_planes[plane_idx][1] == plane[1]
2198 && device->updateStateBlock->state.clip_planes[plane_idx][2] == plane[2]
2199 && device->updateStateBlock->state.clip_planes[plane_idx][3] == plane[3])
2201 TRACE("Application is setting old values over, nothing to do.\n");
2202 return WINED3D_OK;
2205 device->updateStateBlock->state.clip_planes[plane_idx][0] = plane[0];
2206 device->updateStateBlock->state.clip_planes[plane_idx][1] = plane[1];
2207 device->updateStateBlock->state.clip_planes[plane_idx][2] = plane[2];
2208 device->updateStateBlock->state.clip_planes[plane_idx][3] = plane[3];
2210 /* Handle recording of state blocks. */
2211 if (device->isRecordingState)
2213 TRACE("Recording... not performing anything.\n");
2214 return WINED3D_OK;
2217 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2219 return WINED3D_OK;
2222 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device, UINT plane_idx, float *plane)
2224 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2226 /* Validate plane_idx. */
2227 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2229 TRACE("Application has requested clipplane this device doesn't support.\n");
2230 return WINED3DERR_INVALIDCALL;
2233 plane[0] = (float)device->stateBlock->state.clip_planes[plane_idx][0];
2234 plane[1] = (float)device->stateBlock->state.clip_planes[plane_idx][1];
2235 plane[2] = (float)device->stateBlock->state.clip_planes[plane_idx][2];
2236 plane[3] = (float)device->stateBlock->state.clip_planes[plane_idx][3];
2238 return WINED3D_OK;
2241 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2242 const struct wined3d_clip_status *clip_status)
2244 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2246 if (!clip_status)
2247 return WINED3DERR_INVALIDCALL;
2249 return WINED3D_OK;
2252 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2253 struct wined3d_clip_status *clip_status)
2255 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2257 if (!clip_status)
2258 return WINED3DERR_INVALIDCALL;
2260 return WINED3D_OK;
2263 HRESULT CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2265 TRACE("device %p, material %p.\n", device, material);
2267 device->updateStateBlock->changed.material = TRUE;
2268 device->updateStateBlock->state.material = *material;
2270 /* Handle recording of state blocks */
2271 if (device->isRecordingState)
2273 TRACE("Recording... not performing anything.\n");
2274 return WINED3D_OK;
2277 device_invalidate_state(device, STATE_MATERIAL);
2279 return WINED3D_OK;
2282 HRESULT CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2284 TRACE("device %p, material %p.\n", device, material);
2286 *material = device->updateStateBlock->state.material;
2288 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2289 material->diffuse.r, material->diffuse.g,
2290 material->diffuse.b, material->diffuse.a);
2291 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2292 material->ambient.r, material->ambient.g,
2293 material->ambient.b, material->ambient.a);
2294 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2295 material->specular.r, material->specular.g,
2296 material->specular.b, material->specular.a);
2297 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2298 material->emissive.r, material->emissive.g,
2299 material->emissive.b, material->emissive.a);
2300 TRACE("power %.8e.\n", material->power);
2302 return WINED3D_OK;
2305 HRESULT CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2306 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2308 struct wined3d_buffer *prev_buffer;
2310 TRACE("device %p, buffer %p, format %s.\n",
2311 device, buffer, debug_d3dformat(format_id));
2313 prev_buffer = device->updateStateBlock->state.index_buffer;
2315 device->updateStateBlock->changed.indices = TRUE;
2316 device->updateStateBlock->state.index_buffer = buffer;
2317 device->updateStateBlock->state.index_format = format_id;
2319 /* Handle recording of state blocks. */
2320 if (device->isRecordingState)
2322 TRACE("Recording... not performing anything.\n");
2323 if (buffer)
2324 wined3d_buffer_incref(buffer);
2325 if (prev_buffer)
2326 wined3d_buffer_decref(prev_buffer);
2327 return WINED3D_OK;
2330 if (prev_buffer != buffer)
2332 device_invalidate_state(device, STATE_INDEXBUFFER);
2333 if (buffer)
2335 InterlockedIncrement(&buffer->resource.bind_count);
2336 wined3d_buffer_incref(buffer);
2338 if (prev_buffer)
2340 InterlockedDecrement(&prev_buffer->resource.bind_count);
2341 wined3d_buffer_decref(prev_buffer);
2345 return WINED3D_OK;
2348 HRESULT CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device, struct wined3d_buffer **buffer)
2350 TRACE("device %p, buffer %p.\n", device, buffer);
2352 *buffer = device->stateBlock->state.index_buffer;
2354 if (*buffer)
2355 wined3d_buffer_incref(*buffer);
2357 TRACE("Returning %p.\n", *buffer);
2359 return WINED3D_OK;
2362 /* Method to offer d3d9 a simple way to set the base vertex index without messing with the index buffer */
2363 HRESULT CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2365 TRACE("device %p, base_index %d.\n", device, base_index);
2367 if (device->updateStateBlock->state.base_vertex_index == base_index)
2369 TRACE("Application is setting the old value over, nothing to do\n");
2370 return WINED3D_OK;
2373 device->updateStateBlock->state.base_vertex_index = base_index;
2375 if (device->isRecordingState)
2377 TRACE("Recording... not performing anything\n");
2378 return WINED3D_OK;
2380 return WINED3D_OK;
2383 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2385 TRACE("device %p.\n", device);
2387 return device->stateBlock->state.base_vertex_index;
2390 HRESULT CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2392 TRACE("device %p, viewport %p.\n", device, viewport);
2393 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2394 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2396 device->updateStateBlock->changed.viewport = TRUE;
2397 device->updateStateBlock->state.viewport = *viewport;
2399 /* Handle recording of state blocks */
2400 if (device->isRecordingState)
2402 TRACE("Recording... not performing anything\n");
2403 return WINED3D_OK;
2406 device_invalidate_state(device, STATE_VIEWPORT);
2408 return WINED3D_OK;
2411 HRESULT CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2413 TRACE("device %p, viewport %p.\n", device, viewport);
2415 *viewport = device->stateBlock->state.viewport;
2417 return WINED3D_OK;
2420 HRESULT CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2421 enum wined3d_render_state state, DWORD value)
2423 DWORD old_value = device->stateBlock->state.render_states[state];
2425 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2427 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2428 device->updateStateBlock->state.render_states[state] = value;
2430 /* Handle recording of state blocks. */
2431 if (device->isRecordingState)
2433 TRACE("Recording... not performing anything.\n");
2434 return WINED3D_OK;
2437 /* Compared here and not before the assignment to allow proper stateblock recording. */
2438 if (value == old_value)
2439 TRACE("Application is setting the old value over, nothing to do.\n");
2440 else
2441 device_invalidate_state(device, STATE_RENDER(state));
2443 return WINED3D_OK;
2446 HRESULT CDECL wined3d_device_get_render_state(const struct wined3d_device *device,
2447 enum wined3d_render_state state, DWORD *value)
2449 TRACE("device %p, state %s (%#x), value %p.\n", device, debug_d3drenderstate(state), state, value);
2451 *value = device->stateBlock->state.render_states[state];
2453 return WINED3D_OK;
2456 HRESULT CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2457 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2459 DWORD old_value;
2461 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2462 device, sampler_idx, debug_d3dsamplerstate(state), value);
2464 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2465 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2467 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2468 / sizeof(*device->stateBlock->state.sampler_states))
2470 WARN("Invalid sampler %u.\n", sampler_idx);
2471 return WINED3D_OK; /* Windows accepts overflowing this array ... we do not. */
2474 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2475 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2476 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2478 /* Handle recording of state blocks. */
2479 if (device->isRecordingState)
2481 TRACE("Recording... not performing anything.\n");
2482 return WINED3D_OK;
2485 if (old_value == value)
2487 TRACE("Application is setting the old value over, nothing to do.\n");
2488 return WINED3D_OK;
2491 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2493 return WINED3D_OK;
2496 HRESULT CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2497 UINT sampler_idx, enum wined3d_sampler_state state, DWORD *value)
2499 TRACE("device %p, sampler_idx %u, state %s, value %p.\n",
2500 device, sampler_idx, debug_d3dsamplerstate(state), value);
2502 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2503 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2505 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2506 / sizeof(*device->stateBlock->state.sampler_states))
2508 WARN("Invalid sampler %u.\n", sampler_idx);
2509 return WINED3D_OK; /* Windows accepts overflowing this array ... we do not. */
2512 *value = device->stateBlock->state.sampler_states[sampler_idx][state];
2513 TRACE("Returning %#x.\n", *value);
2515 return WINED3D_OK;
2518 HRESULT CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2520 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2522 device->updateStateBlock->changed.scissorRect = TRUE;
2523 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2525 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2526 return WINED3D_OK;
2528 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2530 if (device->isRecordingState)
2532 TRACE("Recording... not performing anything.\n");
2533 return WINED3D_OK;
2536 device_invalidate_state(device, STATE_SCISSORRECT);
2538 return WINED3D_OK;
2541 HRESULT CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2543 TRACE("device %p, rect %p.\n", device, rect);
2545 *rect = device->updateStateBlock->state.scissor_rect;
2546 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2548 return WINED3D_OK;
2551 HRESULT CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2552 struct wined3d_vertex_declaration *declaration)
2554 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2556 TRACE("device %p, declaration %p.\n", device, declaration);
2558 if (declaration)
2559 wined3d_vertex_declaration_incref(declaration);
2560 if (prev)
2561 wined3d_vertex_declaration_decref(prev);
2563 device->updateStateBlock->state.vertex_declaration = declaration;
2564 device->updateStateBlock->changed.vertexDecl = TRUE;
2566 if (device->isRecordingState)
2568 TRACE("Recording... not performing anything.\n");
2569 return WINED3D_OK;
2571 else if (declaration == prev)
2573 /* Checked after the assignment to allow proper stateblock recording. */
2574 TRACE("Application is setting the old declaration over, nothing to do.\n");
2575 return WINED3D_OK;
2578 device_invalidate_state(device, STATE_VDECL);
2579 return WINED3D_OK;
2582 HRESULT CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device,
2583 struct wined3d_vertex_declaration **declaration)
2585 TRACE("device %p, declaration %p.\n", device, declaration);
2587 *declaration = device->stateBlock->state.vertex_declaration;
2588 if (*declaration)
2589 wined3d_vertex_declaration_incref(*declaration);
2591 return WINED3D_OK;
2594 HRESULT CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2596 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2598 TRACE("device %p, shader %p.\n", device, shader);
2600 device->updateStateBlock->state.vertex_shader = shader;
2601 device->updateStateBlock->changed.vertexShader = TRUE;
2603 if (device->isRecordingState)
2605 if (shader)
2606 wined3d_shader_incref(shader);
2607 if (prev)
2608 wined3d_shader_decref(prev);
2609 TRACE("Recording... not performing anything.\n");
2610 return WINED3D_OK;
2613 if (shader == prev)
2615 TRACE("Application is setting the old shader over, nothing to do.\n");
2616 return WINED3D_OK;
2619 if (shader)
2620 wined3d_shader_incref(shader);
2621 if (prev)
2622 wined3d_shader_decref(prev);
2624 device_invalidate_state(device, STATE_VSHADER);
2626 return WINED3D_OK;
2629 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2631 struct wined3d_shader *shader;
2633 TRACE("device %p.\n", device);
2635 shader = device->stateBlock->state.vertex_shader;
2636 if (shader)
2637 wined3d_shader_incref(shader);
2639 TRACE("Returning %p.\n", shader);
2640 return shader;
2643 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2644 UINT start_register, const BOOL *constants, UINT bool_count)
2646 UINT count = min(bool_count, MAX_CONST_B - start_register);
2647 UINT i;
2649 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2650 device, start_register, constants, bool_count);
2652 if (!constants || start_register >= MAX_CONST_B)
2653 return WINED3DERR_INVALIDCALL;
2655 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2656 for (i = 0; i < count; ++i)
2657 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2659 for (i = start_register; i < count + start_register; ++i)
2660 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2662 if (!device->isRecordingState)
2663 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2665 return WINED3D_OK;
2668 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2669 UINT start_register, BOOL *constants, UINT bool_count)
2671 UINT count = min(bool_count, MAX_CONST_B - start_register);
2673 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2674 device, start_register, constants, bool_count);
2676 if (!constants || start_register >= MAX_CONST_B)
2677 return WINED3DERR_INVALIDCALL;
2679 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2681 return WINED3D_OK;
2684 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2685 UINT start_register, const int *constants, UINT vector4i_count)
2687 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2688 UINT i;
2690 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2691 device, start_register, constants, vector4i_count);
2693 if (!constants || start_register >= MAX_CONST_I)
2694 return WINED3DERR_INVALIDCALL;
2696 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2697 for (i = 0; i < count; ++i)
2698 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2699 constants[i * 4], constants[i * 4 + 1],
2700 constants[i * 4 + 2], constants[i * 4 + 3]);
2702 for (i = start_register; i < count + start_register; ++i)
2703 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2705 if (!device->isRecordingState)
2706 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2708 return WINED3D_OK;
2711 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2712 UINT start_register, int *constants, UINT vector4i_count)
2714 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2716 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2717 device, start_register, constants, vector4i_count);
2719 if (!constants || start_register >= MAX_CONST_I)
2720 return WINED3DERR_INVALIDCALL;
2722 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2723 return WINED3D_OK;
2726 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2727 UINT start_register, const float *constants, UINT vector4f_count)
2729 UINT i;
2731 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2732 device, start_register, constants, vector4f_count);
2734 /* Specifically test start_register > limit to catch MAX_UINT overflows
2735 * when adding start_register + vector4f_count. */
2736 if (!constants
2737 || start_register + vector4f_count > device->d3d_vshader_constantF
2738 || start_register > device->d3d_vshader_constantF)
2739 return WINED3DERR_INVALIDCALL;
2741 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2742 constants, vector4f_count * sizeof(float) * 4);
2743 if (TRACE_ON(d3d))
2745 for (i = 0; i < vector4f_count; ++i)
2746 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2747 constants[i * 4], constants[i * 4 + 1],
2748 constants[i * 4 + 2], constants[i * 4 + 3]);
2751 if (!device->isRecordingState)
2753 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2754 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2757 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2758 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2760 return WINED3D_OK;
2763 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2764 UINT start_register, float *constants, UINT vector4f_count)
2766 int count = min(vector4f_count, device->d3d_vshader_constantF - start_register);
2768 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2769 device, start_register, constants, vector4f_count);
2771 if (!constants || count < 0)
2772 return WINED3DERR_INVALIDCALL;
2774 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2776 return WINED3D_OK;
2779 static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2781 DWORD i;
2783 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2785 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2789 static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2791 DWORD i = device->rev_tex_unit_map[unit];
2792 DWORD j = device->texUnitMap[stage];
2794 device->texUnitMap[stage] = unit;
2795 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2796 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2798 device->rev_tex_unit_map[unit] = stage;
2799 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2800 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2803 static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2805 UINT i;
2807 device->fixed_function_usage_map = 0;
2808 for (i = 0; i < MAX_TEXTURES; ++i)
2810 const struct wined3d_state *state = &device->stateBlock->state;
2811 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2812 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2813 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2814 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2815 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2816 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2817 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2818 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2820 /* Not used, and disable higher stages. */
2821 if (color_op == WINED3D_TOP_DISABLE)
2822 break;
2824 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2825 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2826 || ((color_arg3 == WINED3DTA_TEXTURE)
2827 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2828 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2829 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2830 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2831 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2832 device->fixed_function_usage_map |= (1 << i);
2834 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2835 && i < MAX_TEXTURES - 1)
2836 device->fixed_function_usage_map |= (1 << (i + 1));
2840 static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2842 unsigned int i, tex;
2843 WORD ffu_map;
2845 device_update_fixed_function_usage_map(device);
2846 ffu_map = device->fixed_function_usage_map;
2848 if (device->max_ffp_textures == gl_info->limits.texture_stages
2849 || device->stateBlock->state.lowest_disabled_stage <= device->max_ffp_textures)
2851 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2853 if (!(ffu_map & 1)) continue;
2855 if (device->texUnitMap[i] != i)
2857 device_map_stage(device, i, i);
2858 device_invalidate_state(device, STATE_SAMPLER(i));
2859 device_invalidate_texture_stage(device, i);
2862 return;
2865 /* Now work out the mapping */
2866 tex = 0;
2867 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2869 if (!(ffu_map & 1)) continue;
2871 if (device->texUnitMap[i] != tex)
2873 device_map_stage(device, i, tex);
2874 device_invalidate_state(device, STATE_SAMPLER(i));
2875 device_invalidate_texture_stage(device, i);
2878 ++tex;
2882 static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2884 const enum wined3d_sampler_texture_type *sampler_type =
2885 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2886 unsigned int i;
2888 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2890 if (sampler_type[i] && device->texUnitMap[i] != i)
2892 device_map_stage(device, i, i);
2893 device_invalidate_state(device, STATE_SAMPLER(i));
2894 if (i < gl_info->limits.texture_stages)
2895 device_invalidate_texture_stage(device, i);
2900 static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2901 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2902 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2904 DWORD current_mapping = device->rev_tex_unit_map[unit];
2906 /* Not currently used */
2907 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2909 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2910 /* Used by a fragment sampler */
2912 if (!pshader_sampler_tokens) {
2913 /* No pixel shader, check fixed function */
2914 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2917 /* Pixel shader, check the shader's sampler map */
2918 return !pshader_sampler_tokens[current_mapping];
2921 /* Used by a vertex sampler */
2922 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2925 static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2927 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2928 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2929 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2930 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2931 int i;
2933 if (ps)
2935 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2936 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2937 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2940 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2941 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2942 if (vshader_sampler_type[i])
2944 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2946 /* Already mapped somewhere */
2947 continue;
2950 while (start >= 0)
2952 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2954 device_map_stage(device, vsampler_idx, start);
2955 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2957 --start;
2958 break;
2961 --start;
2967 void device_update_tex_unit_map(struct wined3d_device *device)
2969 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2970 const struct wined3d_state *state = &device->stateBlock->state;
2971 BOOL vs = use_vs(state);
2972 BOOL ps = use_ps(state);
2974 * Rules are:
2975 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2976 * that would be really messy and require shader recompilation
2977 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2978 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2980 if (ps)
2981 device_map_psamplers(device, gl_info);
2982 else
2983 device_map_fixed_function_samplers(device, gl_info);
2985 if (vs)
2986 device_map_vsamplers(device, ps, gl_info);
2989 HRESULT CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2991 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2993 TRACE("device %p, shader %p.\n", device, shader);
2995 device->updateStateBlock->state.pixel_shader = shader;
2996 device->updateStateBlock->changed.pixelShader = TRUE;
2998 if (device->isRecordingState)
3000 if (shader)
3001 wined3d_shader_incref(shader);
3002 if (prev)
3003 wined3d_shader_decref(prev);
3004 TRACE("Recording... not performing anything.\n");
3005 return WINED3D_OK;
3008 if (shader == prev)
3010 TRACE("Application is setting the old shader over, nothing to do.\n");
3011 return WINED3D_OK;
3014 if (shader)
3015 wined3d_shader_incref(shader);
3016 if (prev)
3017 wined3d_shader_decref(prev);
3019 device_invalidate_state(device, STATE_PIXELSHADER);
3021 return WINED3D_OK;
3024 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
3026 struct wined3d_shader *shader;
3028 TRACE("device %p.\n", device);
3030 shader = device->stateBlock->state.pixel_shader;
3031 if (shader)
3032 wined3d_shader_incref(shader);
3034 TRACE("Returning %p.\n", shader);
3035 return shader;
3038 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
3039 UINT start_register, const BOOL *constants, UINT bool_count)
3041 UINT count = min(bool_count, MAX_CONST_B - start_register);
3042 UINT i;
3044 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3045 device, start_register, constants, bool_count);
3047 if (!constants || start_register >= MAX_CONST_B)
3048 return WINED3DERR_INVALIDCALL;
3050 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
3051 for (i = 0; i < count; ++i)
3052 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
3054 for (i = start_register; i < count + start_register; ++i)
3055 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
3057 if (!device->isRecordingState)
3058 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3060 return WINED3D_OK;
3063 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
3064 UINT start_register, BOOL *constants, UINT bool_count)
3066 UINT count = min(bool_count, MAX_CONST_B - start_register);
3068 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3069 device, start_register, constants, bool_count);
3071 if (!constants || start_register >= MAX_CONST_B)
3072 return WINED3DERR_INVALIDCALL;
3074 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
3076 return WINED3D_OK;
3079 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
3080 UINT start_register, const int *constants, UINT vector4i_count)
3082 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3083 UINT i;
3085 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3086 device, start_register, constants, vector4i_count);
3088 if (!constants || start_register >= MAX_CONST_I)
3089 return WINED3DERR_INVALIDCALL;
3091 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
3092 for (i = 0; i < count; ++i)
3093 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
3094 constants[i * 4], constants[i * 4 + 1],
3095 constants[i * 4 + 2], constants[i * 4 + 3]);
3097 for (i = start_register; i < count + start_register; ++i)
3098 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
3100 if (!device->isRecordingState)
3101 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3103 return WINED3D_OK;
3106 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
3107 UINT start_register, int *constants, UINT vector4i_count)
3109 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3111 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3112 device, start_register, constants, vector4i_count);
3114 if (!constants || start_register >= MAX_CONST_I)
3115 return WINED3DERR_INVALIDCALL;
3117 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
3119 return WINED3D_OK;
3122 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
3123 UINT start_register, const float *constants, UINT vector4f_count)
3125 UINT i;
3127 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3128 device, start_register, constants, vector4f_count);
3130 /* Specifically test start_register > limit to catch MAX_UINT overflows
3131 * when adding start_register + vector4f_count. */
3132 if (!constants
3133 || start_register + vector4f_count > device->d3d_pshader_constantF
3134 || start_register > device->d3d_pshader_constantF)
3135 return WINED3DERR_INVALIDCALL;
3137 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
3138 constants, vector4f_count * sizeof(float) * 4);
3139 if (TRACE_ON(d3d))
3141 for (i = 0; i < vector4f_count; ++i)
3142 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
3143 constants[i * 4], constants[i * 4 + 1],
3144 constants[i * 4 + 2], constants[i * 4 + 3]);
3147 if (!device->isRecordingState)
3149 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
3150 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3153 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
3154 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
3156 return WINED3D_OK;
3159 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
3160 UINT start_register, float *constants, UINT vector4f_count)
3162 int count = min(vector4f_count, device->d3d_pshader_constantF - start_register);
3164 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3165 device, start_register, constants, vector4f_count);
3167 if (!constants || count < 0)
3168 return WINED3DERR_INVALIDCALL;
3170 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
3172 return WINED3D_OK;
3175 /* Context activation is done by the caller. */
3176 /* Do not call while under the GL lock. */
3177 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3178 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3179 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3180 DWORD DestFVF)
3182 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3183 struct wined3d_viewport vp;
3184 UINT vertex_size;
3185 unsigned int i;
3186 BYTE *dest_ptr;
3187 BOOL doClip;
3188 DWORD numTextures;
3189 HRESULT hr;
3191 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3193 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3196 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3198 ERR("Source has no position mask\n");
3199 return WINED3DERR_INVALIDCALL;
3202 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3204 static BOOL warned = FALSE;
3206 * The clipping code is not quite correct. Some things need
3207 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3208 * so disable clipping for now.
3209 * (The graphics in Half-Life are broken, and my processvertices
3210 * test crashes with IDirect3DDevice3)
3211 doClip = TRUE;
3213 doClip = FALSE;
3214 if(!warned) {
3215 warned = TRUE;
3216 FIXME("Clipping is broken and disabled for now\n");
3219 else
3220 doClip = FALSE;
3222 vertex_size = get_flexible_vertex_size(DestFVF);
3223 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3225 WARN("Failed to map buffer, hr %#x.\n", hr);
3226 return hr;
3229 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3230 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3231 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3233 TRACE("View mat:\n");
3234 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);
3235 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);
3236 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);
3237 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);
3239 TRACE("Proj mat:\n");
3240 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);
3241 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);
3242 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);
3243 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);
3245 TRACE("World mat:\n");
3246 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);
3247 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);
3248 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);
3249 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);
3251 /* Get the viewport */
3252 wined3d_device_get_viewport(device, &vp);
3253 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3254 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3256 multiply_matrix(&mat,&view_mat,&world_mat);
3257 multiply_matrix(&mat,&proj_mat,&mat);
3259 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3261 for (i = 0; i < dwCount; i+= 1) {
3262 unsigned int tex_index;
3264 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3265 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3266 /* The position first */
3267 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3268 const float *p = (const float *)(element->data.addr + i * element->stride);
3269 float x, y, z, rhw;
3270 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3272 /* Multiplication with world, view and projection matrix */
3273 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);
3274 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);
3275 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);
3276 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);
3278 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3280 /* WARNING: The following things are taken from d3d7 and were not yet checked
3281 * against d3d8 or d3d9!
3284 /* Clipping conditions: From msdn
3286 * A vertex is clipped if it does not match the following requirements
3287 * -rhw < x <= rhw
3288 * -rhw < y <= rhw
3289 * 0 < z <= rhw
3290 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3292 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3293 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3297 if( !doClip ||
3298 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3299 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3300 ( rhw > eps ) ) ) {
3302 /* "Normal" viewport transformation (not clipped)
3303 * 1) The values are divided by rhw
3304 * 2) The y axis is negative, so multiply it with -1
3305 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3306 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3307 * 4) Multiply x with Width/2 and add Width/2
3308 * 5) The same for the height
3309 * 6) Add the viewpoint X and Y to the 2D coordinates and
3310 * The minimum Z value to z
3311 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3313 * Well, basically it's simply a linear transformation into viewport
3314 * coordinates
3317 x /= rhw;
3318 y /= rhw;
3319 z /= rhw;
3321 y *= -1;
3323 x *= vp.width / 2;
3324 y *= vp.height / 2;
3325 z *= vp.max_z - vp.min_z;
3327 x += vp.width / 2 + vp.x;
3328 y += vp.height / 2 + vp.y;
3329 z += vp.min_z;
3331 rhw = 1 / rhw;
3332 } else {
3333 /* That vertex got clipped
3334 * Contrary to OpenGL it is not dropped completely, it just
3335 * undergoes a different calculation.
3337 TRACE("Vertex got clipped\n");
3338 x += rhw;
3339 y += rhw;
3341 x /= 2;
3342 y /= 2;
3344 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3345 * outside of the main vertex buffer memory. That needs some more
3346 * investigation...
3350 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3353 ( (float *) dest_ptr)[0] = x;
3354 ( (float *) dest_ptr)[1] = y;
3355 ( (float *) dest_ptr)[2] = z;
3356 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3358 dest_ptr += 3 * sizeof(float);
3360 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3361 dest_ptr += sizeof(float);
3364 if (DestFVF & WINED3DFVF_PSIZE)
3365 dest_ptr += sizeof(DWORD);
3367 if (DestFVF & WINED3DFVF_NORMAL)
3369 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3370 const float *normal = (const float *)(element->data.addr + i * element->stride);
3371 /* AFAIK this should go into the lighting information */
3372 FIXME("Didn't expect the destination to have a normal\n");
3373 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3376 if (DestFVF & WINED3DFVF_DIFFUSE)
3378 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3379 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3380 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3382 static BOOL warned = FALSE;
3384 if(!warned) {
3385 ERR("No diffuse color in source, but destination has one\n");
3386 warned = TRUE;
3389 *( (DWORD *) dest_ptr) = 0xffffffff;
3390 dest_ptr += sizeof(DWORD);
3392 else
3394 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3398 if (DestFVF & WINED3DFVF_SPECULAR)
3400 /* What's the color value in the feedback buffer? */
3401 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3402 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3403 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3405 static BOOL warned = FALSE;
3407 if(!warned) {
3408 ERR("No specular color in source, but destination has one\n");
3409 warned = TRUE;
3412 *( (DWORD *) dest_ptr) = 0xFF000000;
3413 dest_ptr += sizeof(DWORD);
3415 else
3417 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3421 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3423 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3424 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3425 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3427 ERR("No source texture, but destination requests one\n");
3428 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3430 else
3432 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3437 wined3d_buffer_unmap(dest);
3439 return WINED3D_OK;
3441 #undef copy_and_next
3443 /* Do not call while under the GL lock. */
3444 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3445 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3446 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3448 struct wined3d_state *state = &device->stateBlock->state;
3449 struct wined3d_stream_info stream_info;
3450 const struct wined3d_gl_info *gl_info;
3451 BOOL streamWasUP = state->user_stream;
3452 struct wined3d_context *context;
3453 struct wined3d_shader *vs;
3454 unsigned int i;
3455 HRESULT hr;
3457 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3458 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3459 device, src_start_idx, dst_idx, vertex_count,
3460 dst_buffer, declaration, flags, dst_fvf);
3462 if (declaration)
3463 FIXME("Output vertex declaration not implemented yet.\n");
3465 /* Need any context to write to the vbo. */
3466 context = context_acquire(device, NULL);
3467 gl_info = context->gl_info;
3469 /* ProcessVertices reads from vertex buffers, which have to be assigned.
3470 * DrawPrimitive and DrawPrimitiveUP control the streamIsUP flag, thus
3471 * restore it afterwards. */
3472 vs = state->vertex_shader;
3473 state->vertex_shader = NULL;
3474 state->user_stream = FALSE;
3475 device_stream_info_from_declaration(device, &stream_info);
3476 state->user_stream = streamWasUP;
3477 state->vertex_shader = vs;
3479 /* We can't convert FROM a VBO, and vertex buffers used to source into
3480 * process_vertices() are unlikely to ever be used for drawing. Release
3481 * VBOs in those buffers and fix up the stream_info structure.
3483 * Also apply the start index. */
3484 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3486 struct wined3d_stream_info_element *e;
3488 if (!(stream_info.use_map & (1 << i)))
3489 continue;
3491 e = &stream_info.elements[i];
3492 if (e->data.buffer_object)
3494 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3495 e->data.buffer_object = 0;
3496 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3497 ENTER_GL();
3498 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3499 vb->buffer_object = 0;
3500 LEAVE_GL();
3502 if (e->data.addr)
3503 e->data.addr += e->stride * src_start_idx;
3506 hr = process_vertices_strided(device, dst_idx, vertex_count,
3507 &stream_info, dst_buffer, flags, dst_fvf);
3509 context_release(context);
3511 return hr;
3514 HRESULT CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3515 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3517 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3518 DWORD old_value;
3520 TRACE("device %p, stage %u, state %s, value %#x.\n",
3521 device, stage, debug_d3dtexturestate(state), value);
3523 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3525 WARN("Invalid state %#x passed.\n", state);
3526 return WINED3D_OK;
3529 if (stage >= gl_info->limits.texture_stages)
3531 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3532 stage, gl_info->limits.texture_stages - 1);
3533 return WINED3D_OK;
3536 old_value = device->updateStateBlock->state.texture_states[stage][state];
3537 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3538 device->updateStateBlock->state.texture_states[stage][state] = value;
3540 if (device->isRecordingState)
3542 TRACE("Recording... not performing anything.\n");
3543 return WINED3D_OK;
3546 /* Checked after the assignments to allow proper stateblock recording. */
3547 if (old_value == value)
3549 TRACE("Application is setting the old value over, nothing to do.\n");
3550 return WINED3D_OK;
3553 if (stage > device->stateBlock->state.lowest_disabled_stage
3554 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3555 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3557 /* Colorop change above lowest disabled stage? That won't change
3558 * anything in the GL setup. Changes in other states are important on
3559 * disabled stages too. */
3560 return WINED3D_OK;
3563 if (state == WINED3D_TSS_COLOR_OP)
3565 unsigned int i;
3567 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3569 /* Previously enabled stage disabled now. Make sure to dirtify
3570 * all enabled stages above stage, they have to be disabled.
3572 * The current stage is dirtified below. */
3573 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3575 TRACE("Additionally dirtifying stage %u.\n", i);
3576 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3578 device->stateBlock->state.lowest_disabled_stage = stage;
3579 TRACE("New lowest disabled: %u.\n", stage);
3581 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3583 /* Previously disabled stage enabled. Stages above it may need
3584 * enabling. Stage must be lowest_disabled_stage here, if it's
3585 * bigger success is returned above, and stages below the lowest
3586 * disabled stage can't be enabled (because they are enabled
3587 * already).
3589 * Again stage stage doesn't need to be dirtified here, it is
3590 * handled below. */
3591 for (i = stage + 1; i < gl_info->limits.texture_stages; ++i)
3593 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3594 break;
3595 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3596 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3598 device->stateBlock->state.lowest_disabled_stage = i;
3599 TRACE("New lowest disabled: %u.\n", i);
3603 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3605 return WINED3D_OK;
3608 HRESULT CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3609 UINT stage, enum wined3d_texture_stage_state state, DWORD *value)
3611 TRACE("device %p, stage %u, state %s, value %p.\n",
3612 device, stage, debug_d3dtexturestate(state), value);
3614 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3616 WARN("Invalid state %#x passed.\n", state);
3617 return WINED3D_OK;
3620 *value = device->updateStateBlock->state.texture_states[stage][state];
3621 TRACE("Returning %#x.\n", *value);
3623 return WINED3D_OK;
3626 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3627 UINT stage, struct wined3d_texture *texture)
3629 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3630 struct wined3d_texture *prev;
3632 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3634 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3635 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3637 /* Windows accepts overflowing this array... we do not. */
3638 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3640 WARN("Ignoring invalid stage %u.\n", stage);
3641 return WINED3D_OK;
3644 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3646 WARN("Rejecting attempt to set scratch texture.\n");
3647 return WINED3DERR_INVALIDCALL;
3650 device->updateStateBlock->changed.textures |= 1 << stage;
3652 prev = device->updateStateBlock->state.textures[stage];
3653 TRACE("Previous texture %p.\n", prev);
3655 if (texture == prev)
3657 TRACE("App is setting the same texture again, nothing to do.\n");
3658 return WINED3D_OK;
3661 TRACE("Setting new texture to %p.\n", texture);
3662 device->updateStateBlock->state.textures[stage] = texture;
3664 if (device->isRecordingState)
3666 TRACE("Recording... not performing anything\n");
3668 if (texture) wined3d_texture_incref(texture);
3669 if (prev) wined3d_texture_decref(prev);
3671 return WINED3D_OK;
3674 if (texture)
3676 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3678 wined3d_texture_incref(texture);
3680 if (!prev || texture->target != prev->target)
3681 device_invalidate_state(device, STATE_PIXELSHADER);
3683 if (!prev && stage < gl_info->limits.texture_stages)
3685 /* The source arguments for color and alpha ops have different
3686 * meanings when a NULL texture is bound, so the COLOR_OP and
3687 * ALPHA_OP have to be dirtified. */
3688 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3689 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3692 if (bind_count == 1)
3693 texture->sampler = stage;
3696 if (prev)
3698 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3700 wined3d_texture_decref(prev);
3702 if (!texture && stage < gl_info->limits.texture_stages)
3704 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3705 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3708 if (bind_count && prev->sampler == stage)
3710 unsigned int i;
3712 /* Search for other stages the texture is bound to. Shouldn't
3713 * happen if applications bind textures to a single stage only. */
3714 TRACE("Searching for other stages the texture is bound to.\n");
3715 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3717 if (device->updateStateBlock->state.textures[i] == prev)
3719 TRACE("Texture is also bound to stage %u.\n", i);
3720 prev->sampler = i;
3721 break;
3727 device_invalidate_state(device, STATE_SAMPLER(stage));
3729 return WINED3D_OK;
3732 HRESULT CDECL wined3d_device_get_texture(const struct wined3d_device *device,
3733 UINT stage, struct wined3d_texture **texture)
3735 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3737 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3738 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3740 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3742 WARN("Ignoring invalid stage %u.\n", stage);
3743 return WINED3D_OK; /* Windows accepts overflowing this array ... we do not. */
3746 *texture = device->stateBlock->state.textures[stage];
3747 if (*texture)
3748 wined3d_texture_incref(*texture);
3750 TRACE("Returning %p.\n", *texture);
3752 return WINED3D_OK;
3755 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3756 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3758 struct wined3d_swapchain *swapchain;
3759 HRESULT hr;
3761 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3762 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3764 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
3765 if (FAILED(hr))
3767 WARN("Failed to get swapchain %u, hr %#x.\n", swapchain_idx, hr);
3768 return hr;
3771 hr = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type, backbuffer);
3772 wined3d_swapchain_decref(swapchain);
3773 if (FAILED(hr))
3775 WARN("Failed to get backbuffer %u, hr %#x.\n", backbuffer_idx, hr);
3776 return hr;
3779 return WINED3D_OK;
3782 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3784 TRACE("device %p, caps %p.\n", device, caps);
3786 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3787 device->create_parms.device_type, caps);
3790 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device,
3791 UINT swapchain_idx, struct wined3d_display_mode *mode)
3793 struct wined3d_swapchain *swapchain;
3794 HRESULT hr;
3796 TRACE("device %p, swapchain_idx %u, mode %p.\n", device, swapchain_idx, mode);
3798 if (swapchain_idx)
3800 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
3801 if (SUCCEEDED(hr))
3803 hr = wined3d_swapchain_get_display_mode(swapchain, mode);
3804 wined3d_swapchain_decref(swapchain);
3807 else
3809 const struct wined3d_adapter *adapter = device->adapter;
3811 /* Don't read the real display mode, but return the stored mode
3812 * instead. X11 can't change the color depth, and some apps are
3813 * pretty angry if they SetDisplayMode from 24 to 16 bpp and find out
3814 * that GetDisplayMode still returns 24 bpp.
3816 * Also don't relay to the swapchain because with ddraw it's possible
3817 * that there isn't a swapchain at all. */
3818 mode->width = adapter->screen_size.cx;
3819 mode->height = adapter->screen_size.cy;
3820 mode->format_id = adapter->screen_format;
3821 mode->refresh_rate = 0;
3822 hr = WINED3D_OK;
3825 return hr;
3828 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3830 struct wined3d_stateblock *stateblock;
3831 HRESULT hr;
3833 TRACE("device %p.\n", device);
3835 if (device->isRecordingState)
3836 return WINED3DERR_INVALIDCALL;
3838 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3839 if (FAILED(hr))
3840 return hr;
3842 wined3d_stateblock_decref(device->updateStateBlock);
3843 device->updateStateBlock = stateblock;
3844 device->isRecordingState = TRUE;
3846 TRACE("Recording stateblock %p.\n", stateblock);
3848 return WINED3D_OK;
3851 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3852 struct wined3d_stateblock **stateblock)
3854 struct wined3d_stateblock *object = device->updateStateBlock;
3856 TRACE("device %p, stateblock %p.\n", device, stateblock);
3858 if (!device->isRecordingState)
3860 WARN("Not recording.\n");
3861 *stateblock = NULL;
3862 return WINED3DERR_INVALIDCALL;
3865 stateblock_init_contained_states(object);
3867 *stateblock = object;
3868 device->isRecordingState = FALSE;
3869 device->updateStateBlock = device->stateBlock;
3870 wined3d_stateblock_incref(device->updateStateBlock);
3872 TRACE("Returning stateblock %p.\n", *stateblock);
3874 return WINED3D_OK;
3877 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3879 /* At the moment we have no need for any functionality at the beginning
3880 * of a scene. */
3881 TRACE("device %p.\n", device);
3883 if (device->inScene)
3885 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3886 return WINED3DERR_INVALIDCALL;
3888 device->inScene = TRUE;
3889 return WINED3D_OK;
3892 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3894 struct wined3d_context *context;
3896 TRACE("device %p.\n", device);
3898 if (!device->inScene)
3900 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3901 return WINED3DERR_INVALIDCALL;
3904 context = context_acquire(device, NULL);
3905 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3906 wglFlush();
3907 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3908 * fails. */
3909 context_release(context);
3911 device->inScene = FALSE;
3912 return WINED3D_OK;
3915 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3916 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
3918 UINT i;
3920 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
3921 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3922 dst_window_override, dirty_region);
3924 for (i = 0; i < device->swapchain_count; ++i)
3926 wined3d_swapchain_present(device->swapchains[i], src_rect,
3927 dst_rect, dst_window_override, dirty_region, 0);
3930 return WINED3D_OK;
3933 /* Do not call while under the GL lock. */
3934 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3935 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3937 RECT draw_rect;
3939 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3940 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3942 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3944 struct wined3d_surface *ds = device->fb.depth_stencil;
3945 if (!ds)
3947 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3948 /* TODO: What about depth stencil buffers without stencil bits? */
3949 return WINED3DERR_INVALIDCALL;
3951 else if (flags & WINED3DCLEAR_TARGET)
3953 if (ds->resource.width < device->fb.render_targets[0]->resource.width
3954 || ds->resource.height < device->fb.render_targets[0]->resource.height)
3956 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3957 return WINED3D_OK;
3962 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
3963 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
3964 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
3966 return WINED3D_OK;
3969 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3970 enum wined3d_primitive_type primitive_type)
3972 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3974 device->updateStateBlock->changed.primitive_type = TRUE;
3975 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3978 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3979 enum wined3d_primitive_type *primitive_type)
3981 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3983 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
3985 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3988 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3990 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3992 if (!device->stateBlock->state.vertex_declaration)
3994 WARN("Called without a valid vertex declaration set.\n");
3995 return WINED3DERR_INVALIDCALL;
3998 /* The index buffer is not needed here, but restore it, otherwise it is hell to keep track of */
3999 if (device->stateBlock->state.user_stream)
4001 device_invalidate_state(device, STATE_INDEXBUFFER);
4002 device->stateBlock->state.user_stream = FALSE;
4005 if (device->stateBlock->state.load_base_vertex_index)
4007 device->stateBlock->state.load_base_vertex_index = 0;
4008 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4011 /* Account for the loading offset due to index buffers. Instead of
4012 * reloading all sources correct it with the startvertex parameter. */
4013 drawPrimitive(device, vertex_count, start_vertex, FALSE, NULL);
4014 return WINED3D_OK;
4017 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4019 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4021 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4023 if (!device->stateBlock->state.index_buffer)
4025 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4026 * without an index buffer set. (The first time at least...)
4027 * D3D8 simply dies, but I doubt it can do much harm to return
4028 * D3DERR_INVALIDCALL there as well. */
4029 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4030 return WINED3DERR_INVALIDCALL;
4033 if (!device->stateBlock->state.vertex_declaration)
4035 WARN("Called without a valid vertex declaration set.\n");
4036 return WINED3DERR_INVALIDCALL;
4039 if (device->stateBlock->state.user_stream)
4041 device_invalidate_state(device, STATE_INDEXBUFFER);
4042 device->stateBlock->state.user_stream = FALSE;
4045 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
4046 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
4048 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
4049 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4052 drawPrimitive(device, index_count, start_idx, TRUE, NULL);
4054 return WINED3D_OK;
4057 HRESULT CDECL wined3d_device_draw_primitive_up(struct wined3d_device *device, UINT vertex_count,
4058 const void *stream_data, UINT stream_stride)
4060 struct wined3d_stream_state *stream;
4061 struct wined3d_buffer *vb;
4063 TRACE("device %p, vertex count %u, stream_data %p, stream_stride %u.\n",
4064 device, vertex_count, stream_data, stream_stride);
4066 if (!device->stateBlock->state.vertex_declaration)
4068 WARN("Called without a valid vertex declaration set.\n");
4069 return WINED3DERR_INVALIDCALL;
4072 /* Note in the following, it's not this type, but that's the purpose of streamIsUP */
4073 stream = &device->stateBlock->state.streams[0];
4074 vb = stream->buffer;
4075 stream->buffer = (struct wined3d_buffer *)stream_data;
4076 if (vb)
4077 wined3d_buffer_decref(vb);
4078 stream->offset = 0;
4079 stream->stride = stream_stride;
4080 device->stateBlock->state.user_stream = TRUE;
4081 if (device->stateBlock->state.load_base_vertex_index)
4083 device->stateBlock->state.load_base_vertex_index = 0;
4084 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4087 /* TODO: Only mark dirty if drawing from a different UP address */
4088 device_invalidate_state(device, STATE_STREAMSRC);
4090 drawPrimitive(device, vertex_count, 0, FALSE, NULL);
4092 /* MSDN specifies stream zero settings must be set to NULL */
4093 stream->buffer = NULL;
4094 stream->stride = 0;
4096 /* stream zero settings set to null at end, as per the msdn. No need to
4097 * mark dirty here, the app has to set the new stream sources or use UP
4098 * drawing again. */
4099 return WINED3D_OK;
4102 HRESULT CDECL wined3d_device_draw_indexed_primitive_up(struct wined3d_device *device,
4103 UINT index_count, const void *index_data, enum wined3d_format_id index_data_format_id,
4104 const void *stream_data, UINT stream_stride)
4106 struct wined3d_stream_state *stream;
4107 struct wined3d_buffer *vb, *ib;
4109 TRACE("device %p, index_count %u, index_data %p, index_data_format %s, stream_data %p, stream_stride %u.\n",
4110 device, index_count, index_data, debug_d3dformat(index_data_format_id), stream_data, stream_stride);
4112 if (!device->stateBlock->state.vertex_declaration)
4114 WARN("(%p) : Called without a valid vertex declaration set\n", device);
4115 return WINED3DERR_INVALIDCALL;
4118 stream = &device->stateBlock->state.streams[0];
4119 vb = stream->buffer;
4120 stream->buffer = (struct wined3d_buffer *)stream_data;
4121 if (vb)
4122 wined3d_buffer_decref(vb);
4123 stream->offset = 0;
4124 stream->stride = stream_stride;
4125 device->stateBlock->state.user_stream = TRUE;
4126 device->stateBlock->state.index_format = index_data_format_id;
4128 /* Set to 0 as per msdn. Do it now due to the stream source loading during drawPrimitive */
4129 device->stateBlock->state.base_vertex_index = 0;
4130 if (device->stateBlock->state.load_base_vertex_index)
4132 device->stateBlock->state.load_base_vertex_index = 0;
4133 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4135 /* Invalidate the state until we have nicer tracking of the stream source pointers */
4136 device_invalidate_state(device, STATE_STREAMSRC);
4137 device_invalidate_state(device, STATE_INDEXBUFFER);
4139 drawPrimitive(device, index_count, 0, TRUE, index_data);
4141 /* MSDN specifies stream zero settings and index buffer must be set to NULL */
4142 stream->buffer = NULL;
4143 stream->stride = 0;
4144 ib = device->stateBlock->state.index_buffer;
4145 if (ib)
4147 wined3d_buffer_decref(ib);
4148 device->stateBlock->state.index_buffer = NULL;
4150 /* No need to mark the stream source state dirty here. Either the app calls UP drawing again, or it has to call
4151 * SetStreamSource to specify a vertex buffer
4154 return WINED3D_OK;
4157 HRESULT CDECL wined3d_device_draw_primitive_strided(struct wined3d_device *device,
4158 UINT vertex_count, const struct wined3d_strided_data *strided_data)
4160 /* Mark the state dirty until we have nicer tracking. It's fine to change
4161 * baseVertexIndex because that call is only called by ddraw which does
4162 * not need that value. */
4163 device_invalidate_state(device, STATE_VDECL);
4164 device_invalidate_state(device, STATE_STREAMSRC);
4165 device_invalidate_state(device, STATE_INDEXBUFFER);
4167 device->stateBlock->state.base_vertex_index = 0;
4168 device->up_strided = strided_data;
4169 drawPrimitive(device, vertex_count, 0, FALSE, NULL);
4170 device->up_strided = NULL;
4172 /* Invalidate the states again to make sure the values from the stateblock
4173 * are properly applied in the next regular draw. Note that the application-
4174 * provided strided data has ovwritten pretty much the entire vertex and
4175 * and index stream related states */
4176 device_invalidate_state(device, STATE_VDECL);
4177 device_invalidate_state(device, STATE_STREAMSRC);
4178 device_invalidate_state(device, STATE_INDEXBUFFER);
4179 return WINED3D_OK;
4182 HRESULT CDECL wined3d_device_draw_indexed_primitive_strided(struct wined3d_device *device,
4183 UINT index_count, const struct wined3d_strided_data *strided_data,
4184 UINT vertex_count, const void *index_data, enum wined3d_format_id index_data_format_id)
4186 enum wined3d_format_id prev_idx_format;
4188 /* Mark the state dirty until we have nicer tracking
4189 * its fine to change baseVertexIndex because that call is only called by ddraw which does not need
4190 * that value.
4192 device_invalidate_state(device, STATE_VDECL);
4193 device_invalidate_state(device, STATE_STREAMSRC);
4194 device_invalidate_state(device, STATE_INDEXBUFFER);
4196 prev_idx_format = device->stateBlock->state.index_format;
4197 device->stateBlock->state.index_format = index_data_format_id;
4198 device->stateBlock->state.user_stream = TRUE;
4199 device->stateBlock->state.base_vertex_index = 0;
4200 device->up_strided = strided_data;
4201 drawPrimitive(device, index_count, 0, TRUE, index_data);
4202 device->up_strided = NULL;
4203 device->stateBlock->state.index_format = prev_idx_format;
4205 device_invalidate_state(device, STATE_VDECL);
4206 device_invalidate_state(device, STATE_STREAMSRC);
4207 device_invalidate_state(device, STATE_INDEXBUFFER);
4208 return WINED3D_OK;
4211 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4212 static HRESULT device_update_volume(struct wined3d_device *device,
4213 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4215 struct wined3d_map_desc src;
4216 struct wined3d_map_desc dst;
4217 HRESULT hr;
4219 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4220 device, src_volume, dst_volume);
4222 /* TODO: Implement direct loading into the gl volume instead of using
4223 * memcpy and dirtification to improve loading performance. */
4224 hr = wined3d_volume_map(src_volume, &src, NULL, WINED3DLOCK_READONLY);
4225 if (FAILED(hr)) return hr;
4226 hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3DLOCK_DISCARD);
4227 if (FAILED(hr))
4229 wined3d_volume_unmap(src_volume);
4230 return hr;
4233 memcpy(dst.data, src.data, dst_volume->resource.size);
4235 hr = wined3d_volume_unmap(dst_volume);
4236 if (FAILED(hr))
4237 wined3d_volume_unmap(src_volume);
4238 else
4239 hr = wined3d_volume_unmap(src_volume);
4241 return hr;
4244 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4245 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4247 enum wined3d_resource_type type;
4248 unsigned int level_count, i;
4249 HRESULT hr;
4251 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4253 /* Verify that the source and destination textures are non-NULL. */
4254 if (!src_texture || !dst_texture)
4256 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4257 return WINED3DERR_INVALIDCALL;
4260 if (src_texture == dst_texture)
4262 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4263 return WINED3DERR_INVALIDCALL;
4266 /* Verify that the source and destination textures are the same type. */
4267 type = src_texture->resource.type;
4268 if (dst_texture->resource.type != type)
4270 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4271 return WINED3DERR_INVALIDCALL;
4274 /* Check that both textures have the identical numbers of levels. */
4275 level_count = wined3d_texture_get_level_count(src_texture);
4276 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4278 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4279 return WINED3DERR_INVALIDCALL;
4282 /* Make sure that the destination texture is loaded. */
4283 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4285 /* Update every surface level of the texture. */
4286 switch (type)
4288 case WINED3D_RTYPE_TEXTURE:
4290 struct wined3d_surface *src_surface;
4291 struct wined3d_surface *dst_surface;
4293 for (i = 0; i < level_count; ++i)
4295 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4296 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4297 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4298 if (FAILED(hr))
4300 WARN("Failed to update surface, hr %#x.\n", hr);
4301 return hr;
4304 break;
4307 case WINED3D_RTYPE_CUBE_TEXTURE:
4309 struct wined3d_surface *src_surface;
4310 struct wined3d_surface *dst_surface;
4312 for (i = 0; i < level_count * 6; ++i)
4314 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4315 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4316 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4317 if (FAILED(hr))
4319 WARN("Failed to update surface, hr %#x.\n", hr);
4320 return hr;
4323 break;
4326 case WINED3D_RTYPE_VOLUME_TEXTURE:
4328 for (i = 0; i < level_count; ++i)
4330 hr = device_update_volume(device,
4331 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4332 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4333 if (FAILED(hr))
4335 WARN("Failed to update volume, hr %#x.\n", hr);
4336 return hr;
4339 break;
4342 default:
4343 FIXME("Unsupported texture type %#x.\n", type);
4344 return WINED3DERR_INVALIDCALL;
4347 return WINED3D_OK;
4350 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4351 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4353 struct wined3d_swapchain *swapchain;
4354 HRESULT hr;
4356 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4358 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
4359 if (FAILED(hr)) return hr;
4361 hr = wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4362 wined3d_swapchain_decref(swapchain);
4364 return hr;
4367 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4369 const struct wined3d_state *state = &device->stateBlock->state;
4370 struct wined3d_texture *texture;
4371 DWORD i;
4373 TRACE("device %p, num_passes %p.\n", device, num_passes);
4375 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4377 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4379 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4380 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4382 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4384 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4385 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4388 texture = state->textures[i];
4389 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4391 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4393 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4394 return E_FAIL;
4396 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4398 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4399 return E_FAIL;
4401 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4402 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4404 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4405 return E_FAIL;
4409 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4410 || state->render_states[WINED3D_RS_STENCILENABLE])
4412 struct wined3d_surface *ds = device->fb.depth_stencil;
4413 struct wined3d_surface *target = device->fb.render_targets[0];
4415 if(ds && target
4416 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4418 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4419 return WINED3DERR_CONFLICTINGRENDERSTATE;
4423 /* return a sensible default */
4424 *num_passes = 1;
4426 TRACE("returning D3D_OK\n");
4427 return WINED3D_OK;
4430 HRESULT CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4432 static BOOL warned;
4434 TRACE("device %p, software %#x.\n", device, software);
4436 if (!warned)
4438 FIXME("device %p, software %#x stub!\n", device, software);
4439 warned = TRUE;
4442 device->softwareVertexProcessing = software;
4444 return WINED3D_OK;
4447 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4449 static BOOL warned;
4451 TRACE("device %p.\n", device);
4453 if (!warned)
4455 TRACE("device %p stub!\n", device);
4456 warned = TRUE;
4459 return device->softwareVertexProcessing;
4462 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4463 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4465 struct wined3d_swapchain *swapchain;
4466 HRESULT hr;
4468 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4469 device, swapchain_idx, raster_status);
4471 hr = wined3d_device_get_swapchain(device, swapchain_idx, &swapchain);
4472 if (FAILED(hr))
4474 WARN("Failed to get swapchain %u, hr %#x.\n", swapchain_idx, hr);
4475 return hr;
4478 hr = wined3d_swapchain_get_raster_status(swapchain, raster_status);
4479 wined3d_swapchain_decref(swapchain);
4480 if (FAILED(hr))
4482 WARN("Failed to get raster status, hr %#x.\n", hr);
4483 return hr;
4486 return WINED3D_OK;
4489 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4491 static BOOL warned;
4493 TRACE("device %p, segments %.8e.\n", device, segments);
4495 if (segments != 0.0f)
4497 if (!warned)
4499 FIXME("device %p, segments %.8e stub!\n", device, segments);
4500 warned = TRUE;
4504 return WINED3D_OK;
4507 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4509 static BOOL warned;
4511 TRACE("device %p.\n", device);
4513 if (!warned)
4515 FIXME("device %p stub!\n", device);
4516 warned = TRUE;
4519 return 0.0f;
4522 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4523 struct wined3d_surface *src_surface, const RECT *src_rect,
4524 struct wined3d_surface *dst_surface, const POINT *dst_point)
4526 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4527 device, src_surface, wine_dbgstr_rect(src_rect),
4528 dst_surface, wine_dbgstr_point(dst_point));
4530 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4532 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4533 src_surface, dst_surface);
4534 return WINED3DERR_INVALIDCALL;
4537 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4540 HRESULT CDECL wined3d_device_draw_rect_patch(struct wined3d_device *device, UINT handle,
4541 const float *num_segs, const struct wined3d_rect_patch_info *rect_patch_info)
4543 struct wined3d_rect_patch *patch;
4544 GLenum old_primitive_type;
4545 unsigned int i;
4546 struct list *e;
4547 BOOL found;
4549 TRACE("device %p, handle %#x, num_segs %p, rect_patch_info %p.\n",
4550 device, handle, num_segs, rect_patch_info);
4552 if (!(handle || rect_patch_info))
4554 /* TODO: Write a test for the return value, thus the FIXME */
4555 FIXME("Both handle and rect_patch_info are NULL.\n");
4556 return WINED3DERR_INVALIDCALL;
4559 if (handle)
4561 i = PATCHMAP_HASHFUNC(handle);
4562 found = FALSE;
4563 LIST_FOR_EACH(e, &device->patches[i])
4565 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4566 if (patch->Handle == handle)
4568 found = TRUE;
4569 break;
4573 if (!found)
4575 TRACE("Patch does not exist. Creating a new one\n");
4576 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4577 patch->Handle = handle;
4578 list_add_head(&device->patches[i], &patch->entry);
4579 } else {
4580 TRACE("Found existing patch %p\n", patch);
4583 else
4585 /* Since opengl does not load tesselated vertex attributes into numbered vertex
4586 * attributes we have to tesselate, read back, and draw. This needs a patch
4587 * management structure instance. Create one.
4589 * A possible improvement is to check if a vertex shader is used, and if not directly
4590 * draw the patch.
4592 FIXME("Drawing an uncached patch. This is slow\n");
4593 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4596 if (num_segs[0] != patch->numSegs[0] || num_segs[1] != patch->numSegs[1]
4597 || num_segs[2] != patch->numSegs[2] || num_segs[3] != patch->numSegs[3]
4598 || (rect_patch_info && memcmp(rect_patch_info, &patch->rect_patch_info, sizeof(*rect_patch_info))))
4600 HRESULT hr;
4601 TRACE("Tesselation density or patch info changed, retesselating\n");
4603 if (rect_patch_info)
4604 patch->rect_patch_info = *rect_patch_info;
4606 patch->numSegs[0] = num_segs[0];
4607 patch->numSegs[1] = num_segs[1];
4608 patch->numSegs[2] = num_segs[2];
4609 patch->numSegs[3] = num_segs[3];
4611 hr = tesselate_rectpatch(device, patch);
4612 if (FAILED(hr))
4614 WARN("Patch tesselation failed.\n");
4616 /* Do not release the handle to store the params of the patch */
4617 if (!handle)
4618 HeapFree(GetProcessHeap(), 0, patch);
4620 return hr;
4624 old_primitive_type = device->stateBlock->state.gl_primitive_type;
4625 device->stateBlock->state.gl_primitive_type = GL_TRIANGLES;
4626 wined3d_device_draw_primitive_strided(device, patch->numSegs[0] * patch->numSegs[1] * 2 * 3, &patch->strided);
4627 device->stateBlock->state.gl_primitive_type = old_primitive_type;
4629 /* Destroy uncached patches */
4630 if (!handle)
4632 HeapFree(GetProcessHeap(), 0, patch->mem);
4633 HeapFree(GetProcessHeap(), 0, patch);
4635 return WINED3D_OK;
4638 HRESULT CDECL wined3d_device_draw_tri_patch(struct wined3d_device *device, UINT handle,
4639 const float *segment_count, const struct wined3d_tri_patch_info *patch_info)
4641 FIXME("device %p, handle %#x, segment_count %p, patch_info %p stub!\n",
4642 device, handle, segment_count, patch_info);
4644 return WINED3D_OK;
4647 HRESULT CDECL wined3d_device_delete_patch(struct wined3d_device *device, UINT handle)
4649 struct wined3d_rect_patch *patch;
4650 struct list *e;
4651 int i;
4653 TRACE("device %p, handle %#x.\n", device, handle);
4655 i = PATCHMAP_HASHFUNC(handle);
4656 LIST_FOR_EACH(e, &device->patches[i])
4658 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4659 if (patch->Handle == handle)
4661 TRACE("Deleting patch %p\n", patch);
4662 list_remove(&patch->entry);
4663 HeapFree(GetProcessHeap(), 0, patch->mem);
4664 HeapFree(GetProcessHeap(), 0, patch);
4665 return WINED3D_OK;
4669 /* TODO: Write a test for the return value */
4670 FIXME("Attempt to destroy nonexistent patch\n");
4671 return WINED3DERR_INVALIDCALL;
4674 /* Do not call while under the GL lock. */
4675 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4676 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4678 RECT r;
4680 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4681 device, surface, wine_dbgstr_rect(rect),
4682 color->r, color->g, color->b, color->a);
4684 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4686 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4687 return WINED3DERR_INVALIDCALL;
4690 if (!rect)
4692 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4693 rect = &r;
4696 return surface_color_fill(surface, rect, color);
4699 /* Do not call while under the GL lock. */
4700 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4701 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4703 struct wined3d_resource *resource;
4704 HRESULT hr;
4705 RECT rect;
4707 resource = rendertarget_view->resource;
4708 if (resource->type != WINED3D_RTYPE_SURFACE)
4710 FIXME("Only supported on surface resources\n");
4711 return;
4714 SetRect(&rect, 0, 0, resource->width, resource->height);
4715 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4716 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4719 HRESULT CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4720 UINT render_target_idx, struct wined3d_surface **render_target)
4722 TRACE("device %p, render_target_idx %u, render_target %p.\n",
4723 device, render_target_idx, render_target);
4725 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4727 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4728 return WINED3DERR_INVALIDCALL;
4731 *render_target = device->fb.render_targets[render_target_idx];
4732 TRACE("Returning render target %p.\n", *render_target);
4734 if (!*render_target)
4735 return WINED3DERR_NOTFOUND;
4737 wined3d_surface_incref(*render_target);
4739 return WINED3D_OK;
4742 HRESULT CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device,
4743 struct wined3d_surface **depth_stencil)
4745 TRACE("device %p, depth_stencil %p.\n", device, depth_stencil);
4747 *depth_stencil = device->fb.depth_stencil;
4748 TRACE("Returning depth/stencil surface %p.\n", *depth_stencil);
4750 if (!*depth_stencil)
4751 return WINED3DERR_NOTFOUND;
4753 wined3d_surface_incref(*depth_stencil);
4755 return WINED3D_OK;
4758 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4759 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4761 struct wined3d_surface *prev;
4763 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4764 device, render_target_idx, render_target, set_viewport);
4766 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4768 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4769 return WINED3DERR_INVALIDCALL;
4772 prev = device->fb.render_targets[render_target_idx];
4773 if (render_target == prev)
4775 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4776 return WINED3D_OK;
4779 /* Render target 0 can't be set to NULL. */
4780 if (!render_target && !render_target_idx)
4782 WARN("Trying to set render target 0 to NULL.\n");
4783 return WINED3DERR_INVALIDCALL;
4786 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4788 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4789 return WINED3DERR_INVALIDCALL;
4792 if (render_target)
4793 wined3d_surface_incref(render_target);
4794 device->fb.render_targets[render_target_idx] = render_target;
4795 /* Release after the assignment, to prevent device_resource_released()
4796 * from seeing the surface as still in use. */
4797 if (prev)
4798 wined3d_surface_decref(prev);
4800 /* Render target 0 is special. */
4801 if (!render_target_idx && set_viewport)
4803 /* Set the viewport and scissor rectangles, if requested. Tests show
4804 * that stateblock recording is ignored, the change goes directly
4805 * into the primary stateblock. */
4806 device->stateBlock->state.viewport.height = device->fb.render_targets[0]->resource.height;
4807 device->stateBlock->state.viewport.width = device->fb.render_targets[0]->resource.width;
4808 device->stateBlock->state.viewport.x = 0;
4809 device->stateBlock->state.viewport.y = 0;
4810 device->stateBlock->state.viewport.max_z = 1.0f;
4811 device->stateBlock->state.viewport.min_z = 0.0f;
4812 device_invalidate_state(device, STATE_VIEWPORT);
4814 device->stateBlock->state.scissor_rect.top = 0;
4815 device->stateBlock->state.scissor_rect.left = 0;
4816 device->stateBlock->state.scissor_rect.right = device->stateBlock->state.viewport.width;
4817 device->stateBlock->state.scissor_rect.bottom = device->stateBlock->state.viewport.height;
4818 device_invalidate_state(device, STATE_SCISSORRECT);
4821 device_invalidate_state(device, STATE_FRAMEBUFFER);
4823 return WINED3D_OK;
4826 HRESULT CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4828 struct wined3d_surface *prev = device->fb.depth_stencil;
4830 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4831 device, depth_stencil, prev);
4833 if (prev == depth_stencil)
4835 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4836 return WINED3D_OK;
4839 if (prev)
4841 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4842 || prev->flags & SFLAG_DISCARD)
4844 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4845 prev->resource.width, prev->resource.height);
4846 if (prev == device->onscreen_depth_stencil)
4848 wined3d_surface_decref(device->onscreen_depth_stencil);
4849 device->onscreen_depth_stencil = NULL;
4854 device->fb.depth_stencil = depth_stencil;
4855 if (depth_stencil)
4856 wined3d_surface_incref(depth_stencil);
4858 if (!prev != !depth_stencil)
4860 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4861 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4862 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4863 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4864 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4866 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4868 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4870 if (prev)
4871 wined3d_surface_decref(prev);
4873 device_invalidate_state(device, STATE_FRAMEBUFFER);
4875 return WINED3D_OK;
4878 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4879 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4881 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4882 device, x_hotspot, y_hotspot, cursor_image);
4884 /* some basic validation checks */
4885 if (device->cursorTexture)
4887 struct wined3d_context *context = context_acquire(device, NULL);
4888 ENTER_GL();
4889 glDeleteTextures(1, &device->cursorTexture);
4890 LEAVE_GL();
4891 context_release(context);
4892 device->cursorTexture = 0;
4895 if (cursor_image)
4897 struct wined3d_map_desc map_desc;
4899 /* MSDN: Cursor must be A8R8G8B8 */
4900 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4902 WARN("surface %p has an invalid format.\n", cursor_image);
4903 return WINED3DERR_INVALIDCALL;
4906 /* MSDN: Cursor must be smaller than the display mode */
4907 if (cursor_image->resource.width > device->adapter->screen_size.cx
4908 || cursor_image->resource.height > device->adapter->screen_size.cy)
4910 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4911 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4912 device->adapter->screen_size.cx, device->adapter->screen_size.cy);
4913 return WINED3DERR_INVALIDCALL;
4916 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4918 /* Do not store the surface's pointer because the application may
4919 * release it after setting the cursor image. Windows doesn't
4920 * addref the set surface, so we can't do this either without
4921 * creating circular refcount dependencies. Copy out the gl texture
4922 * instead. */
4923 device->cursorWidth = cursor_image->resource.width;
4924 device->cursorHeight = cursor_image->resource.height;
4925 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3DLOCK_READONLY)))
4927 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4928 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
4929 struct wined3d_context *context;
4930 char *mem, *bits = map_desc.data;
4931 GLint intfmt = format->glInternal;
4932 GLint gl_format = format->glFormat;
4933 GLint type = format->glType;
4934 INT height = device->cursorHeight;
4935 INT width = device->cursorWidth;
4936 INT bpp = format->byte_count;
4937 INT i;
4939 /* Reformat the texture memory (pitch and width can be
4940 * different) */
4941 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
4942 for (i = 0; i < height; ++i)
4943 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
4944 wined3d_surface_unmap(cursor_image);
4946 context = context_acquire(device, NULL);
4948 ENTER_GL();
4950 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4952 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
4953 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4956 invalidate_active_texture(device, context);
4957 /* Create a new cursor texture */
4958 glGenTextures(1, &device->cursorTexture);
4959 checkGLcall("glGenTextures");
4960 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
4961 /* Copy the bitmap memory into the cursor texture */
4962 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
4963 checkGLcall("glTexImage2D");
4964 HeapFree(GetProcessHeap(), 0, mem);
4966 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4968 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
4969 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4972 LEAVE_GL();
4974 context_release(context);
4976 else
4978 FIXME("A cursor texture was not returned.\n");
4979 device->cursorTexture = 0;
4982 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4984 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4985 ICONINFO cursorInfo;
4986 DWORD *maskBits;
4987 HCURSOR cursor;
4989 /* 32-bit user32 cursors ignore the alpha channel if it's all
4990 * zeroes, and use the mask instead. Fill the mask with all ones
4991 * to ensure we still get a fully transparent cursor. */
4992 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4993 memset(maskBits, 0xff, mask_size);
4994 wined3d_surface_map(cursor_image, &map_desc, NULL,
4995 WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY);
4996 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4998 cursorInfo.fIcon = FALSE;
4999 cursorInfo.xHotspot = x_hotspot;
5000 cursorInfo.yHotspot = y_hotspot;
5001 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
5002 1, 1, maskBits);
5003 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
5004 1, 32, map_desc.data);
5005 wined3d_surface_unmap(cursor_image);
5006 /* Create our cursor and clean up. */
5007 cursor = CreateIconIndirect(&cursorInfo);
5008 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
5009 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
5010 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
5011 device->hardwareCursor = cursor;
5012 if (device->bCursorVisible) SetCursor( cursor );
5013 HeapFree(GetProcessHeap(), 0, maskBits);
5017 device->xHotSpot = x_hotspot;
5018 device->yHotSpot = y_hotspot;
5019 return WINED3D_OK;
5022 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5023 int x_screen_space, int y_screen_space, DWORD flags)
5025 TRACE("device %p, x %d, y %d, flags %#x.\n",
5026 device, x_screen_space, y_screen_space, flags);
5028 device->xScreenSpace = x_screen_space;
5029 device->yScreenSpace = y_screen_space;
5031 if (device->hardwareCursor)
5033 POINT pt;
5035 GetCursorPos( &pt );
5036 if (x_screen_space == pt.x && y_screen_space == pt.y)
5037 return;
5038 SetCursorPos( x_screen_space, y_screen_space );
5040 /* Switch to the software cursor if position diverges from the hardware one. */
5041 GetCursorPos( &pt );
5042 if (x_screen_space != pt.x || y_screen_space != pt.y)
5044 if (device->bCursorVisible) SetCursor( NULL );
5045 DestroyCursor( device->hardwareCursor );
5046 device->hardwareCursor = 0;
5051 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5053 BOOL oldVisible = device->bCursorVisible;
5055 TRACE("device %p, show %#x.\n", device, show);
5058 * When ShowCursor is first called it should make the cursor appear at the OS's last
5059 * known cursor position.
5061 if (show && !oldVisible)
5063 POINT pt;
5064 GetCursorPos(&pt);
5065 device->xScreenSpace = pt.x;
5066 device->yScreenSpace = pt.y;
5069 if (device->hardwareCursor)
5071 device->bCursorVisible = show;
5072 if (show)
5073 SetCursor(device->hardwareCursor);
5074 else
5075 SetCursor(NULL);
5077 else
5079 if (device->cursorTexture)
5080 device->bCursorVisible = show;
5083 return oldVisible;
5086 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5088 struct wined3d_resource *resource, *cursor;
5090 TRACE("device %p.\n", device);
5092 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5094 TRACE("Checking resource %p for eviction.\n", resource);
5096 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
5098 TRACE("Evicting %p.\n", resource);
5099 resource->resource_ops->resource_unload(resource);
5103 /* Invalidate stream sources, the buffer(s) may have been evicted. */
5104 device_invalidate_state(device, STATE_STREAMSRC);
5107 static BOOL is_display_mode_supported(const struct wined3d_device *device,
5108 const struct wined3d_swapchain_desc *swapchain_desc)
5110 struct wined3d_display_mode m;
5111 UINT i, count;
5112 HRESULT hr;
5114 /* All Windowed modes are supported, as is leaving the current mode */
5115 if (swapchain_desc->windowed)
5116 return TRUE;
5117 if (!swapchain_desc->backbuffer_width)
5118 return TRUE;
5119 if (!swapchain_desc->backbuffer_height)
5120 return TRUE;
5122 count = wined3d_get_adapter_mode_count(device->wined3d, device->adapter->ordinal, WINED3DFMT_UNKNOWN);
5123 for (i = 0; i < count; ++i)
5125 memset(&m, 0, sizeof(m));
5126 hr = wined3d_enum_adapter_modes(device->wined3d, device->adapter->ordinal, WINED3DFMT_UNKNOWN, i, &m);
5127 if (FAILED(hr))
5128 ERR("Failed to enumerate adapter mode.\n");
5129 if (m.width == swapchain_desc->backbuffer_width && m.height == swapchain_desc->backbuffer_height)
5130 /* Mode found, it is supported. */
5131 return TRUE;
5133 /* Mode not found -> not supported */
5134 return FALSE;
5137 /* Do not call while under the GL lock. */
5138 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5140 struct wined3d_resource *resource, *cursor;
5141 const struct wined3d_gl_info *gl_info;
5142 struct wined3d_context *context;
5143 struct wined3d_shader *shader;
5145 context = context_acquire(device, NULL);
5146 gl_info = context->gl_info;
5148 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5150 TRACE("Unloading resource %p.\n", resource);
5152 resource->resource_ops->resource_unload(resource);
5155 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
5157 device->shader_backend->shader_destroy(shader);
5160 ENTER_GL();
5161 if (device->depth_blt_texture)
5163 glDeleteTextures(1, &device->depth_blt_texture);
5164 device->depth_blt_texture = 0;
5166 if (device->cursorTexture)
5168 glDeleteTextures(1, &device->cursorTexture);
5169 device->cursorTexture = 0;
5171 LEAVE_GL();
5173 device->blitter->free_private(device);
5174 device->frag_pipe->free_private(device);
5175 device->shader_backend->shader_free_private(device);
5176 destroy_dummy_textures(device, gl_info);
5178 context_release(context);
5180 while (device->context_count)
5182 swapchain_destroy_contexts(device->contexts[0]->swapchain);
5185 HeapFree(GetProcessHeap(), 0, swapchain->context);
5186 swapchain->context = NULL;
5189 /* Do not call while under the GL lock. */
5190 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5192 struct wined3d_context *context;
5193 struct wined3d_surface *target;
5194 HRESULT hr;
5196 /* Recreate the primary swapchain's context */
5197 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
5198 if (!swapchain->context)
5200 ERR("Failed to allocate memory for swapchain context array.\n");
5201 return E_OUTOFMEMORY;
5204 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
5205 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
5207 WARN("Failed to create context.\n");
5208 HeapFree(GetProcessHeap(), 0, swapchain->context);
5209 return E_FAIL;
5212 swapchain->context[0] = context;
5213 swapchain->num_contexts = 1;
5214 create_dummy_textures(device, context);
5215 context_release(context);
5217 hr = device->shader_backend->shader_alloc_private(device);
5218 if (FAILED(hr))
5220 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
5221 goto err;
5224 hr = device->frag_pipe->alloc_private(device);
5225 if (FAILED(hr))
5227 ERR("Failed to allocate fragment pipe private data, hr %#x.\n", hr);
5228 device->shader_backend->shader_free_private(device);
5229 goto err;
5232 hr = device->blitter->alloc_private(device);
5233 if (FAILED(hr))
5235 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
5236 device->frag_pipe->free_private(device);
5237 device->shader_backend->shader_free_private(device);
5238 goto err;
5241 return WINED3D_OK;
5243 err:
5244 context_acquire(device, NULL);
5245 destroy_dummy_textures(device, context->gl_info);
5246 context_release(context);
5247 context_destroy(device, context);
5248 HeapFree(GetProcessHeap(), 0, swapchain->context);
5249 swapchain->num_contexts = 0;
5250 return hr;
5253 /* Do not call while under the GL lock. */
5254 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5255 const struct wined3d_swapchain_desc *swapchain_desc,
5256 wined3d_device_reset_cb callback)
5258 struct wined3d_resource *resource, *cursor;
5259 struct wined3d_swapchain *swapchain;
5260 struct wined3d_display_mode mode;
5261 BOOL DisplayModeChanged = FALSE;
5262 BOOL update_desc = FALSE;
5263 HRESULT hr;
5265 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
5267 stateblock_unbind_resources(device->stateBlock);
5269 if (device->onscreen_depth_stencil)
5271 wined3d_surface_decref(device->onscreen_depth_stencil);
5272 device->onscreen_depth_stencil = NULL;
5275 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5277 TRACE("Enumerating resource %p.\n", resource);
5278 if (FAILED(hr = callback(resource)))
5279 return hr;
5282 hr = wined3d_device_get_swapchain(device, 0, &swapchain);
5283 if (FAILED(hr))
5285 ERR("Failed to get the first implicit swapchain\n");
5286 return hr;
5289 if (!is_display_mode_supported(device, swapchain_desc))
5291 WARN("Rejecting reset() call because the requested display mode is not supported.\n");
5292 WARN("Requested mode: %ux%u.\n",
5293 swapchain_desc->backbuffer_width,
5294 swapchain_desc->backbuffer_height);
5295 wined3d_swapchain_decref(swapchain);
5296 return WINED3DERR_INVALIDCALL;
5299 /* Is it necessary to recreate the gl context? Actually every setting can be changed
5300 * on an existing gl context, so there's no real need for recreation.
5302 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
5304 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
5306 TRACE("New params:\n");
5307 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5308 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5309 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5310 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5311 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5312 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5313 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5314 TRACE("device_window %p\n", swapchain_desc->device_window);
5315 TRACE("windowed %#x\n", swapchain_desc->windowed);
5316 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5317 if (swapchain_desc->enable_auto_depth_stencil)
5318 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5319 TRACE("flags %#x\n", swapchain_desc->flags);
5320 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5321 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
5322 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5324 /* No special treatment of these parameters. Just store them */
5325 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
5326 swapchain->desc.flags = swapchain_desc->flags;
5327 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
5328 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
5330 /* What to do about these? */
5331 if (swapchain_desc->backbuffer_count
5332 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
5333 FIXME("Cannot change the back buffer count yet.\n");
5335 if (swapchain_desc->device_window
5336 && swapchain_desc->device_window != swapchain->desc.device_window)
5338 TRACE("Changing the device window from %p to %p.\n",
5339 swapchain->desc.device_window, swapchain_desc->device_window);
5340 swapchain->desc.device_window = swapchain_desc->device_window;
5341 swapchain->device_window = swapchain_desc->device_window;
5342 wined3d_swapchain_set_window(swapchain, NULL);
5345 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
5347 HRESULT hrc;
5349 TRACE("Creating the depth stencil buffer\n");
5351 hrc = device->device_parent->ops->create_depth_stencil(device->device_parent,
5352 swapchain_desc->backbuffer_width,
5353 swapchain_desc->backbuffer_height,
5354 swapchain_desc->auto_depth_stencil_format,
5355 swapchain_desc->multisample_type,
5356 swapchain_desc->multisample_quality,
5357 FALSE,
5358 &device->auto_depth_stencil);
5359 if (FAILED(hrc))
5361 ERR("Failed to create the depth stencil buffer.\n");
5362 wined3d_swapchain_decref(swapchain);
5363 return WINED3DERR_INVALIDCALL;
5367 if (device->onscreen_depth_stencil)
5369 wined3d_surface_decref(device->onscreen_depth_stencil);
5370 device->onscreen_depth_stencil = NULL;
5373 /* Reset the depth stencil */
5374 if (swapchain_desc->enable_auto_depth_stencil)
5375 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5376 else
5377 wined3d_device_set_depth_stencil(device, NULL);
5379 TRACE("Resetting stateblock\n");
5380 wined3d_stateblock_decref(device->updateStateBlock);
5381 wined3d_stateblock_decref(device->stateBlock);
5383 if (swapchain_desc->windowed)
5385 mode.width = swapchain->orig_width;
5386 mode.height = swapchain->orig_height;
5387 mode.refresh_rate = 0;
5388 mode.format_id = swapchain->desc.backbuffer_format;
5390 else
5392 mode.width = swapchain_desc->backbuffer_width;
5393 mode.height = swapchain_desc->backbuffer_height;
5394 mode.refresh_rate = swapchain_desc->refresh_rate;
5395 mode.format_id = swapchain_desc->backbuffer_format;
5398 /* Should Width == 800 && Height == 0 set 800x600? */
5399 if (swapchain_desc->backbuffer_width && swapchain_desc->backbuffer_height
5400 && (swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
5401 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height))
5403 if (!swapchain_desc->windowed)
5404 DisplayModeChanged = TRUE;
5406 swapchain->desc.backbuffer_width = swapchain_desc->backbuffer_width;
5407 swapchain->desc.backbuffer_height = swapchain_desc->backbuffer_height;
5408 update_desc = TRUE;
5411 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5412 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5414 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5415 update_desc = TRUE;
5418 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5419 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5421 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5422 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5423 update_desc = TRUE;
5426 if (update_desc)
5428 UINT i;
5430 hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5431 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5432 swapchain->desc.multisample_type, swapchain->desc.multisample_quality);
5433 if (FAILED(hr))
5435 wined3d_swapchain_decref(swapchain);
5436 return hr;
5439 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5441 hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5442 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5443 swapchain->desc.multisample_type, swapchain->desc.multisample_quality);
5444 if (FAILED(hr))
5446 wined3d_swapchain_decref(swapchain);
5447 return hr;
5450 if (device->auto_depth_stencil)
5452 hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5453 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5454 swapchain->desc.multisample_type, swapchain->desc.multisample_quality);
5455 if (FAILED(hr))
5457 wined3d_swapchain_decref(swapchain);
5458 return hr;
5463 if (device->d3d_initialized)
5464 delete_opengl_contexts(device, swapchain);
5466 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5467 || DisplayModeChanged)
5469 wined3d_device_set_display_mode(device, 0, &mode);
5471 if (!swapchain_desc->windowed)
5473 if (swapchain->desc.windowed)
5475 HWND focus_window = device->create_parms.focus_window;
5476 if (!focus_window)
5477 focus_window = swapchain_desc->device_window;
5478 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5480 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5481 wined3d_swapchain_decref(swapchain);
5482 return hr;
5485 /* switch from windowed to fs */
5486 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5487 swapchain_desc->backbuffer_width,
5488 swapchain_desc->backbuffer_height);
5490 else
5492 /* Fullscreen -> fullscreen mode change */
5493 MoveWindow(swapchain->device_window, 0, 0,
5494 swapchain_desc->backbuffer_width,
5495 swapchain_desc->backbuffer_height,
5496 TRUE);
5499 else if (!swapchain->desc.windowed)
5501 /* Fullscreen -> windowed switch */
5502 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5503 wined3d_device_release_focus_window(device);
5505 swapchain->desc.windowed = swapchain_desc->windowed;
5507 else if (!swapchain_desc->windowed)
5509 DWORD style = device->style;
5510 DWORD exStyle = device->exStyle;
5511 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5512 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5513 * Reset to clear up their mess. Guild Wars also loses the device during that.
5515 device->style = 0;
5516 device->exStyle = 0;
5517 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5518 swapchain_desc->backbuffer_width,
5519 swapchain_desc->backbuffer_height);
5520 device->style = style;
5521 device->exStyle = exStyle;
5524 /* Note: No parent needed for initial internal stateblock */
5525 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5526 if (FAILED(hr))
5527 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5528 else
5529 TRACE("Created stateblock %p.\n", device->stateBlock);
5530 device->updateStateBlock = device->stateBlock;
5531 wined3d_stateblock_incref(device->updateStateBlock);
5533 stateblock_init_default_state(device->stateBlock);
5535 swapchain_update_render_to_fbo(swapchain);
5536 swapchain_update_draw_bindings(swapchain);
5538 if (device->d3d_initialized)
5539 hr = create_primary_opengl_context(device, swapchain);
5540 wined3d_swapchain_decref(swapchain);
5542 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5543 * first use
5545 return hr;
5548 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5550 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5552 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5554 return WINED3D_OK;
5558 HRESULT CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5559 struct wined3d_device_creation_parameters *parameters)
5561 TRACE("device %p, parameters %p.\n", device, parameters);
5563 *parameters = device->create_parms;
5564 return WINED3D_OK;
5567 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5568 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5570 struct wined3d_swapchain *swapchain;
5572 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5573 device, swapchain_idx, flags, ramp);
5575 if (SUCCEEDED(wined3d_device_get_swapchain(device, swapchain_idx, &swapchain)))
5577 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5578 wined3d_swapchain_decref(swapchain);
5582 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5583 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5585 struct wined3d_swapchain *swapchain;
5587 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5588 device, swapchain_idx, ramp);
5590 if (SUCCEEDED(wined3d_device_get_swapchain(device, swapchain_idx, &swapchain)))
5592 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5593 wined3d_swapchain_decref(swapchain);
5597 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5599 TRACE("device %p, resource %p.\n", device, resource);
5601 list_add_head(&device->resources, &resource->resource_list_entry);
5604 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5606 TRACE("device %p, resource %p.\n", device, resource);
5608 list_remove(&resource->resource_list_entry);
5611 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5613 enum wined3d_resource_type type = resource->type;
5614 unsigned int i;
5616 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5618 context_resource_released(device, resource, type);
5620 switch (type)
5622 case WINED3D_RTYPE_SURFACE:
5624 struct wined3d_surface *surface = surface_from_resource(resource);
5626 if (!device->d3d_initialized) break;
5628 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5630 if (device->fb.render_targets[i] == surface)
5632 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5633 device->fb.render_targets[i] = NULL;
5637 if (device->fb.depth_stencil == surface)
5639 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5640 device->fb.depth_stencil = NULL;
5643 break;
5645 case WINED3D_RTYPE_TEXTURE:
5646 case WINED3D_RTYPE_CUBE_TEXTURE:
5647 case WINED3D_RTYPE_VOLUME_TEXTURE:
5648 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5650 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5652 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5654 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5655 texture, device->stateBlock, i);
5656 device->stateBlock->state.textures[i] = NULL;
5659 if (device->updateStateBlock != device->stateBlock
5660 && device->updateStateBlock->state.textures[i] == texture)
5662 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5663 texture, device->updateStateBlock, i);
5664 device->updateStateBlock->state.textures[i] = NULL;
5667 break;
5669 case WINED3D_RTYPE_BUFFER:
5671 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5673 for (i = 0; i < MAX_STREAMS; ++i)
5675 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5677 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5678 buffer, device->stateBlock, i);
5679 device->stateBlock->state.streams[i].buffer = NULL;
5682 if (device->updateStateBlock != device->stateBlock
5683 && device->updateStateBlock->state.streams[i].buffer == buffer)
5685 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5686 buffer, device->updateStateBlock, i);
5687 device->updateStateBlock->state.streams[i].buffer = NULL;
5692 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5694 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5695 buffer, device->stateBlock);
5696 device->stateBlock->state.index_buffer = NULL;
5699 if (device->updateStateBlock != device->stateBlock
5700 && device->updateStateBlock->state.index_buffer == buffer)
5702 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5703 buffer, device->updateStateBlock);
5704 device->updateStateBlock->state.index_buffer = NULL;
5707 break;
5709 default:
5710 break;
5713 /* Remove the resource from the resourceStore */
5714 device_resource_remove(device, resource);
5716 TRACE("Resource released.\n");
5719 HRESULT CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device,
5720 HDC dc, struct wined3d_surface **surface)
5722 struct wined3d_resource *resource;
5724 TRACE("device %p, dc %p, surface %p.\n", device, dc, surface);
5726 if (!dc)
5727 return WINED3DERR_INVALIDCALL;
5729 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5731 if (resource->type == WINED3D_RTYPE_SURFACE)
5733 struct wined3d_surface *s = surface_from_resource(resource);
5735 if (s->hDC == dc)
5737 TRACE("Found surface %p for dc %p.\n", s, dc);
5738 *surface = s;
5739 return WINED3D_OK;
5744 return WINED3DERR_INVALIDCALL;
5747 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5748 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5749 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5751 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5752 const struct fragment_pipeline *fragment_pipeline;
5753 struct wined3d_display_mode mode;
5754 struct shader_caps shader_caps;
5755 struct fragment_caps ffp_caps;
5756 unsigned int i;
5757 HRESULT hr;
5759 device->ref = 1;
5760 device->wined3d = wined3d;
5761 wined3d_incref(device->wined3d);
5762 device->adapter = wined3d->adapter_count ? adapter : NULL;
5763 device->device_parent = device_parent;
5764 list_init(&device->resources);
5765 list_init(&device->shaders);
5766 device->surface_alignment = surface_alignment;
5768 /* Get the initial screen setup for ddraw. */
5769 hr = wined3d_get_adapter_display_mode(wined3d, adapter_idx, &mode);
5770 if (FAILED(hr))
5772 ERR("Failed to get the adapter's display mode, hr %#x.\n", hr);
5773 wined3d_decref(device->wined3d);
5774 return hr;
5776 adapter->screen_size.cx = mode.width;
5777 adapter->screen_size.cy = mode.height;
5778 adapter->screen_format = mode.format_id;
5780 /* Save the creation parameters. */
5781 device->create_parms.adapter_idx = adapter_idx;
5782 device->create_parms.device_type = device_type;
5783 device->create_parms.focus_window = focus_window;
5784 device->create_parms.flags = flags;
5786 for (i = 0; i < PATCHMAP_SIZE; ++i) list_init(&device->patches[i]);
5788 select_shader_mode(&adapter->gl_info, &device->ps_selected_mode, &device->vs_selected_mode);
5789 device->shader_backend = adapter->shader_backend;
5791 if (device->shader_backend)
5793 device->shader_backend->shader_get_caps(&adapter->gl_info, &shader_caps);
5794 device->vshader_version = shader_caps.VertexShaderVersion;
5795 device->pshader_version = shader_caps.PixelShaderVersion;
5796 device->d3d_vshader_constantF = shader_caps.MaxVertexShaderConst;
5797 device->d3d_pshader_constantF = shader_caps.MaxPixelShaderConst;
5798 device->vs_clipping = shader_caps.VSClipping;
5800 fragment_pipeline = adapter->fragment_pipe;
5801 device->frag_pipe = fragment_pipeline;
5802 if (fragment_pipeline)
5804 fragment_pipeline->get_caps(&adapter->gl_info, &ffp_caps);
5805 device->max_ffp_textures = ffp_caps.MaxSimultaneousTextures;
5807 hr = compile_state_table(device->StateTable, device->multistate_funcs, &adapter->gl_info,
5808 ffp_vertexstate_template, fragment_pipeline, misc_state_template);
5809 if (FAILED(hr))
5811 ERR("Failed to compile state table, hr %#x.\n", hr);
5812 wined3d_decref(device->wined3d);
5813 return hr;
5816 device->blitter = adapter->blitter;
5818 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5819 if (FAILED(hr))
5821 WARN("Failed to create stateblock.\n");
5822 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5824 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5826 wined3d_decref(device->wined3d);
5827 return hr;
5830 TRACE("Created stateblock %p.\n", device->stateBlock);
5831 device->updateStateBlock = device->stateBlock;
5832 wined3d_stateblock_incref(device->updateStateBlock);
5834 return WINED3D_OK;
5838 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5840 DWORD rep = device->StateTable[state].representative;
5841 struct wined3d_context *context;
5842 DWORD idx;
5843 BYTE shift;
5844 UINT i;
5846 for (i = 0; i < device->context_count; ++i)
5848 context = device->contexts[i];
5849 if(isStateDirty(context, rep)) continue;
5851 context->dirtyArray[context->numDirtyEntries++] = rep;
5852 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5853 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5854 context->isStateDirty[idx] |= (1 << shift);
5858 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5860 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5861 *width = context->current_rt->pow2Width;
5862 *height = context->current_rt->pow2Height;
5865 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5867 const struct wined3d_swapchain *swapchain = context->swapchain;
5868 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5869 * current context's drawable, which is the size of the back buffer of the swapchain
5870 * the active context belongs to. */
5871 *width = swapchain->desc.backbuffer_width;
5872 *height = swapchain->desc.backbuffer_height;
5875 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5876 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5878 if (device->filter_messages)
5880 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5881 window, message, wparam, lparam);
5882 if (unicode)
5883 return DefWindowProcW(window, message, wparam, lparam);
5884 else
5885 return DefWindowProcA(window, message, wparam, lparam);
5888 if (message == WM_DESTROY)
5890 TRACE("unregister window %p.\n", window);
5891 wined3d_unregister_window(window);
5893 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5894 ERR("Window %p is not the focus window for device %p.\n", window, device);
5896 else if (message == WM_DISPLAYCHANGE)
5898 device->device_parent->ops->mode_changed(device->device_parent);
5901 if (unicode)
5902 return CallWindowProcW(proc, window, message, wparam, lparam);
5903 else
5904 return CallWindowProcA(proc, window, message, wparam, lparam);