2 * Copyright 2002 Lionel Ulmer
3 * Copyright 2002-2005 Jason Edmeades
4 * Copyright 2003-2004 Raphael Junqueira
5 * Copyright 2004 Christian Costa
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2006-2008 Henri Verbeet
9 * Copyright 2007 Andrew Riedi
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/port.h"
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39 /* Define the default light parameters as specified by MSDN. */
40 const struct wined3d_light WINED3D_default_light =
42 WINED3D_LIGHT_DIRECTIONAL, /* Type */
43 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
44 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
46 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
47 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
50 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
55 /**********************************************************
56 * Global variable / Constants follow
57 **********************************************************/
58 const struct wined3d_matrix identity =
60 1.0f, 0.0f, 0.0f, 0.0f,
61 0.0f, 1.0f, 0.0f, 0.0f,
62 0.0f, 0.0f, 1.0f, 0.0f,
63 0.0f, 0.0f, 0.0f, 1.0f,
64 }}}; /* When needed for comparisons */
66 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
67 * actually have the same values in GL and D3D. */
68 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
70 switch(primitive_type)
72 case WINED3D_PT_POINTLIST:
75 case WINED3D_PT_LINELIST:
78 case WINED3D_PT_LINESTRIP:
81 case WINED3D_PT_TRIANGLELIST:
84 case WINED3D_PT_TRIANGLESTRIP:
85 return GL_TRIANGLE_STRIP;
87 case WINED3D_PT_TRIANGLEFAN:
88 return GL_TRIANGLE_FAN;
90 case WINED3D_PT_LINELIST_ADJ:
91 return GL_LINES_ADJACENCY_ARB;
93 case WINED3D_PT_LINESTRIP_ADJ:
94 return GL_LINE_STRIP_ADJACENCY_ARB;
96 case WINED3D_PT_TRIANGLELIST_ADJ:
97 return GL_TRIANGLES_ADJACENCY_ARB;
99 case WINED3D_PT_TRIANGLESTRIP_ADJ:
100 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
103 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
108 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
110 switch(primitive_type)
113 return WINED3D_PT_POINTLIST;
116 return WINED3D_PT_LINELIST;
119 return WINED3D_PT_LINESTRIP;
122 return WINED3D_PT_TRIANGLELIST;
124 case GL_TRIANGLE_STRIP:
125 return WINED3D_PT_TRIANGLESTRIP;
127 case GL_TRIANGLE_FAN:
128 return WINED3D_PT_TRIANGLEFAN;
130 case GL_LINES_ADJACENCY_ARB:
131 return WINED3D_PT_LINELIST_ADJ;
133 case GL_LINE_STRIP_ADJACENCY_ARB:
134 return WINED3D_PT_LINESTRIP_ADJ;
136 case GL_TRIANGLES_ADJACENCY_ARB:
137 return WINED3D_PT_TRIANGLELIST_ADJ;
139 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
140 return WINED3D_PT_TRIANGLESTRIP_ADJ;
143 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
144 return WINED3D_PT_UNDEFINED;
148 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
150 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
151 *regnum = WINED3D_FFP_POSITION;
152 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
153 *regnum = WINED3D_FFP_BLENDWEIGHT;
154 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
155 *regnum = WINED3D_FFP_BLENDINDICES;
156 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
157 *regnum = WINED3D_FFP_NORMAL;
158 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
159 *regnum = WINED3D_FFP_PSIZE;
160 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
161 *regnum = WINED3D_FFP_DIFFUSE;
162 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
163 *regnum = WINED3D_FFP_SPECULAR;
164 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
165 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
168 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n", debug_d3ddeclusage(usage), usage_idx);
176 /* Context activation is done by the caller. */
177 void device_stream_info_from_declaration(struct wined3d_device *device, struct wined3d_stream_info *stream_info)
179 const struct wined3d_state *state = &device->stateBlock->state;
180 /* We need to deal with frequency data! */
181 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
185 stream_info->use_map = 0;
186 stream_info->swizzle_map = 0;
188 /* Check for transformed vertices, disable vertex shader if present. */
189 stream_info->position_transformed = declaration->position_transformed;
190 use_vshader = state->vertex_shader && !declaration->position_transformed;
192 /* Translate the declaration into strided data. */
193 for (i = 0; i < declaration->element_count; ++i)
195 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
196 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
197 struct wined3d_buffer *buffer = stream->buffer;
198 struct wined3d_bo_address data;
203 TRACE("%p Element %p (%u of %u)\n", declaration->elements,
204 element, i + 1, declaration->element_count);
206 if (!buffer) continue;
208 data.buffer_object = 0;
211 stride = stream->stride;
212 if (state->user_stream)
214 TRACE("Stream %u is UP, %p\n", element->input_slot, buffer);
215 data.buffer_object = 0;
216 data.addr = (BYTE *)buffer;
220 TRACE("Stream %u isn't UP, %p\n", element->input_slot, buffer);
221 buffer_get_memory(buffer, &device->adapter->gl_info, &data);
223 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
224 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
225 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
226 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
227 * not, drawStridedSlow is needed, including a vertex buffer path. */
228 if (state->load_base_vertex_index < 0)
230 WARN("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
231 state->load_base_vertex_index);
232 data.buffer_object = 0;
233 data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info);
234 if ((UINT_PTR)data.addr < -state->load_base_vertex_index * stride)
236 FIXME("System memory vertex data load offset is negative!\n");
240 data.addr += element->offset;
242 TRACE("offset %u input_slot %u usage_idx %d\n", element->offset, element->input_slot, element->usage_idx);
246 if (element->output_slot == ~0U)
248 /* TODO: Assuming vertexdeclarations are usually used with the
249 * same or a similar shader, it might be worth it to store the
250 * last used output slot and try that one first. */
251 stride_used = vshader_get_input(state->vertex_shader,
252 element->usage, element->usage_idx, &idx);
256 idx = element->output_slot;
262 if (!element->ffp_valid)
264 WARN("Skipping unsupported fixed function element of format %s and usage %s\n",
265 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
270 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
276 TRACE("Load %s array %u [usage %s, usage_idx %u, "
277 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u]\n",
278 use_vshader ? "shader": "fixed function", idx,
279 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
280 element->offset, stride, debug_d3dformat(element->format->id), data.buffer_object);
282 data.addr += stream->offset;
284 stream_info->elements[idx].format = element->format;
285 stream_info->elements[idx].data = data;
286 stream_info->elements[idx].stride = stride;
287 stream_info->elements[idx].stream_idx = element->input_slot;
289 if (!device->adapter->gl_info.supported[ARB_VERTEX_ARRAY_BGRA]
290 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
292 stream_info->swizzle_map |= 1 << idx;
294 stream_info->use_map |= 1 << idx;
298 device->num_buffer_queries = 0;
299 if (!state->user_stream)
301 WORD map = stream_info->use_map;
302 stream_info->all_vbo = 1;
304 /* PreLoad all the vertex buffers. */
305 for (i = 0; map; map >>= 1, ++i)
307 struct wined3d_stream_info_element *element;
308 struct wined3d_buffer *buffer;
310 if (!(map & 1)) continue;
312 element = &stream_info->elements[i];
313 buffer = state->streams[element->stream_idx].buffer;
314 wined3d_buffer_preload(buffer);
316 /* If the preload dropped the buffer object, update the stream info. */
317 if (buffer->buffer_object != element->data.buffer_object)
319 element->data.buffer_object = 0;
320 element->data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info)
321 + (ptrdiff_t)element->data.addr;
324 if (!buffer->buffer_object)
325 stream_info->all_vbo = 0;
328 device->buffer_queries[device->num_buffer_queries++] = buffer->query;
333 stream_info->all_vbo = 0;
337 static void stream_info_element_from_strided(const struct wined3d_gl_info *gl_info,
338 const struct wined3d_strided_element *strided, struct wined3d_stream_info_element *e)
340 e->data.addr = strided->data;
341 e->data.buffer_object = 0;
342 e->format = wined3d_get_format(gl_info, strided->format);
343 e->stride = strided->stride;
347 static void device_stream_info_from_strided(const struct wined3d_gl_info *gl_info,
348 const struct wined3d_strided_data *strided, struct wined3d_stream_info *stream_info)
352 memset(stream_info, 0, sizeof(*stream_info));
354 if (strided->position.data)
355 stream_info_element_from_strided(gl_info, &strided->position, &stream_info->elements[WINED3D_FFP_POSITION]);
356 if (strided->normal.data)
357 stream_info_element_from_strided(gl_info, &strided->normal, &stream_info->elements[WINED3D_FFP_NORMAL]);
358 if (strided->diffuse.data)
359 stream_info_element_from_strided(gl_info, &strided->diffuse, &stream_info->elements[WINED3D_FFP_DIFFUSE]);
360 if (strided->specular.data)
361 stream_info_element_from_strided(gl_info, &strided->specular, &stream_info->elements[WINED3D_FFP_SPECULAR]);
363 for (i = 0; i < WINED3DDP_MAXTEXCOORD; ++i)
365 if (strided->tex_coords[i].data)
366 stream_info_element_from_strided(gl_info, &strided->tex_coords[i],
367 &stream_info->elements[WINED3D_FFP_TEXCOORD0 + i]);
370 stream_info->position_transformed = strided->position_transformed;
372 for (i = 0; i < sizeof(stream_info->elements) / sizeof(*stream_info->elements); ++i)
374 if (!stream_info->elements[i].format) continue;
376 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
377 && stream_info->elements[i].format->id == WINED3DFMT_B8G8R8A8_UNORM)
379 stream_info->swizzle_map |= 1 << i;
381 stream_info->use_map |= 1 << i;
385 static void device_trace_strided_stream_info(const struct wined3d_stream_info *stream_info)
387 TRACE("Strided Data:\n");
388 TRACE_STRIDED(stream_info, WINED3D_FFP_POSITION);
389 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDWEIGHT);
390 TRACE_STRIDED(stream_info, WINED3D_FFP_BLENDINDICES);
391 TRACE_STRIDED(stream_info, WINED3D_FFP_NORMAL);
392 TRACE_STRIDED(stream_info, WINED3D_FFP_PSIZE);
393 TRACE_STRIDED(stream_info, WINED3D_FFP_DIFFUSE);
394 TRACE_STRIDED(stream_info, WINED3D_FFP_SPECULAR);
395 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD0);
396 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD1);
397 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD2);
398 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD3);
399 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD4);
400 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD5);
401 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD6);
402 TRACE_STRIDED(stream_info, WINED3D_FFP_TEXCOORD7);
405 /* Context activation is done by the caller. */
406 void device_update_stream_info(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
408 struct wined3d_stream_info *stream_info = &device->strided_streams;
409 const struct wined3d_state *state = &device->stateBlock->state;
410 DWORD prev_all_vbo = stream_info->all_vbo;
412 if (device->up_strided)
414 /* Note: this is a ddraw fixed-function code path. */
415 TRACE("=============================== Strided Input ================================\n");
416 device_stream_info_from_strided(gl_info, device->up_strided, stream_info);
417 if (TRACE_ON(d3d)) device_trace_strided_stream_info(stream_info);
421 TRACE("============================= Vertex Declaration =============================\n");
422 device_stream_info_from_declaration(device, stream_info);
425 if (state->vertex_shader && !stream_info->position_transformed)
427 if (state->vertex_declaration->half_float_conv_needed && !stream_info->all_vbo)
429 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
430 device->useDrawStridedSlow = TRUE;
434 device->useDrawStridedSlow = FALSE;
439 WORD slow_mask = (1 << WINED3D_FFP_PSIZE);
440 slow_mask |= -!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
441 & ((1 << WINED3D_FFP_DIFFUSE) | (1 << WINED3D_FFP_SPECULAR));
443 if ((stream_info->position_transformed || (stream_info->use_map & slow_mask)) && !stream_info->all_vbo)
445 device->useDrawStridedSlow = TRUE;
449 device->useDrawStridedSlow = FALSE;
453 if (prev_all_vbo != stream_info->all_vbo)
454 device_invalidate_state(device, STATE_INDEXBUFFER);
457 static void device_preload_texture(const struct wined3d_state *state, unsigned int idx)
459 struct wined3d_texture *texture;
460 enum WINED3DSRGB srgb;
462 if (!(texture = state->textures[idx])) return;
463 srgb = state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE] ? SRGB_SRGB : SRGB_RGB;
464 texture->texture_ops->texture_preload(texture, srgb);
467 void device_preload_textures(const struct wined3d_device *device)
469 const struct wined3d_state *state = &device->stateBlock->state;
474 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
476 if (state->vertex_shader->reg_maps.sampler_type[i])
477 device_preload_texture(state, MAX_FRAGMENT_SAMPLERS + i);
483 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
485 if (state->pixel_shader->reg_maps.sampler_type[i])
486 device_preload_texture(state, i);
491 WORD ffu_map = device->fixed_function_usage_map;
493 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
496 device_preload_texture(state, i);
501 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
503 struct wined3d_context **new_array;
505 TRACE("Adding context %p.\n", context);
507 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
508 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
509 sizeof(*new_array) * (device->context_count + 1));
513 ERR("Failed to grow the context array.\n");
517 new_array[device->context_count++] = context;
518 device->contexts = new_array;
522 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
524 struct wined3d_context **new_array;
528 TRACE("Removing context %p.\n", context);
530 for (i = 0; i < device->context_count; ++i)
532 if (device->contexts[i] == context)
541 ERR("Context %p doesn't exist in context array.\n", context);
545 if (!--device->context_count)
547 HeapFree(GetProcessHeap(), 0, device->contexts);
548 device->contexts = NULL;
552 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
553 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
556 ERR("Failed to shrink context array. Oh well.\n");
560 device->contexts = new_array;
563 /* Do not call while under the GL lock. */
564 void device_switch_onscreen_ds(struct wined3d_device *device,
565 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
567 if (device->onscreen_depth_stencil)
569 surface_load_ds_location(device->onscreen_depth_stencil, context, SFLAG_INTEXTURE);
571 surface_modify_ds_location(device->onscreen_depth_stencil, SFLAG_INTEXTURE,
572 device->onscreen_depth_stencil->ds_current_size.cx,
573 device->onscreen_depth_stencil->ds_current_size.cy);
574 wined3d_surface_decref(device->onscreen_depth_stencil);
576 device->onscreen_depth_stencil = depth_stencil;
577 wined3d_surface_incref(device->onscreen_depth_stencil);
580 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
582 /* partial draw rect */
583 if (draw_rect->left || draw_rect->top
584 || draw_rect->right < target->resource.width
585 || draw_rect->bottom < target->resource.height)
588 /* partial clear rect */
589 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
590 || clear_rect->right < target->resource.width
591 || clear_rect->bottom < target->resource.height))
597 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
598 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
600 RECT current_rect, r;
602 if (ds->flags & location)
603 SetRect(¤t_rect, 0, 0,
604 ds->ds_current_size.cx,
605 ds->ds_current_size.cy);
607 SetRectEmpty(¤t_rect);
609 IntersectRect(&r, draw_rect, ¤t_rect);
610 if (EqualRect(&r, draw_rect))
612 /* current_rect ⊇ draw_rect, modify only. */
613 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
617 if (EqualRect(&r, ¤t_rect))
619 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
623 /* Full clear, modify only. */
624 *out_rect = *draw_rect;
628 IntersectRect(&r, draw_rect, clear_rect);
629 if (EqualRect(&r, draw_rect))
631 /* clear_rect ⊇ draw_rect, modify only. */
632 *out_rect = *draw_rect;
638 surface_load_ds_location(ds, context, location);
639 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
642 /* Do not call while under the GL lock. */
643 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
644 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
645 float depth, DWORD stencil)
647 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
648 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
649 const struct wined3d_gl_info *gl_info;
650 UINT drawable_width, drawable_height;
651 struct wined3d_context *context;
652 GLbitfield clear_mask = 0;
653 BOOL render_offscreen;
657 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
658 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
659 * for the cleared parts, and the untouched parts.
661 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
662 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
663 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
664 * checking all this if the dest surface is in the drawable anyway. */
665 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
667 for (i = 0; i < rt_count; ++i)
669 struct wined3d_surface *rt = fb->render_targets[i];
671 surface_load_location(rt, rt->draw_binding, NULL);
675 context = context_acquire(device, target);
678 context_release(context);
679 WARN("Invalid context, skipping clear.\n");
682 gl_info = context->gl_info;
686 render_offscreen = context->render_offscreen;
687 target->get_drawable_size(context, &drawable_width, &drawable_height);
691 render_offscreen = TRUE;
692 drawable_width = fb->depth_stencil->pow2Width;
693 drawable_height = fb->depth_stencil->pow2Height;
696 if (flags & WINED3DCLEAR_ZBUFFER)
698 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
700 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
701 device_switch_onscreen_ds(device, context, fb->depth_stencil);
702 prepare_ds_clear(fb->depth_stencil, context, location,
703 draw_rect, rect_count, clear_rect, &ds_rect);
706 if (!context_apply_clear_state(context, device, rt_count, fb))
708 context_release(context);
709 WARN("Failed to apply clear state, skipping clear.\n");
715 /* Only set the values up once, as they are not changing. */
716 if (flags & WINED3DCLEAR_STENCIL)
718 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
720 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
721 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
723 gl_info->gl_ops.gl.p_glStencilMask(~0U);
724 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
725 gl_info->gl_ops.gl.p_glClearStencil(stencil);
726 checkGLcall("glClearStencil");
727 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
730 if (flags & WINED3DCLEAR_ZBUFFER)
732 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
734 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
736 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
737 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
738 gl_info->gl_ops.gl.p_glClearDepth(depth);
739 checkGLcall("glClearDepth");
740 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
743 if (flags & WINED3DCLEAR_TARGET)
745 for (i = 0; i < rt_count; ++i)
747 struct wined3d_surface *rt = fb->render_targets[i];
750 surface_modify_location(rt, rt->draw_binding, TRUE);
753 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
754 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
755 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
756 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
757 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
758 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
759 checkGLcall("glClearColor");
760 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
765 if (render_offscreen)
767 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
768 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
772 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
773 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
775 checkGLcall("glScissor");
776 gl_info->gl_ops.gl.p_glClear(clear_mask);
777 checkGLcall("glClear");
783 /* Now process each rect in turn. */
784 for (i = 0; i < rect_count; ++i)
786 /* Note that GL uses lower left, width/height. */
787 IntersectRect(¤t_rect, draw_rect, &clear_rect[i]);
789 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
790 wine_dbgstr_rect(&clear_rect[i]),
791 wine_dbgstr_rect(¤t_rect));
793 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
794 * The rectangle is not cleared, no error is returned, but further rectangles are
795 * still cleared if they are valid. */
796 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
798 TRACE("Rectangle with negative dimensions, ignoring.\n");
802 if (render_offscreen)
804 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
805 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
809 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
810 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
812 checkGLcall("glScissor");
814 gl_info->gl_ops.gl.p_glClear(clear_mask);
815 checkGLcall("glClear");
821 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
822 && target->container.type == WINED3D_CONTAINER_SWAPCHAIN
823 && target->container.u.swapchain->front_buffer == target))
824 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
826 context_release(context);
829 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
831 ULONG refcount = InterlockedIncrement(&device->ref);
833 TRACE("%p increasing refcount to %u.\n", device, refcount);
838 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
840 ULONG refcount = InterlockedDecrement(&device->ref);
842 TRACE("%p decreasing refcount to %u.\n", device, refcount);
846 struct wined3d_stateblock *stateblock;
849 if (wined3d_stateblock_decref(device->updateStateBlock)
850 && device->updateStateBlock != device->stateBlock)
851 FIXME("Something's still holding the update stateblock.\n");
852 device->updateStateBlock = NULL;
854 stateblock = device->stateBlock;
855 device->stateBlock = NULL;
856 if (wined3d_stateblock_decref(stateblock))
857 FIXME("Something's still holding the stateblock.\n");
859 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
861 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
862 device->multistate_funcs[i] = NULL;
865 if (!list_empty(&device->resources))
867 struct wined3d_resource *resource;
869 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
871 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
873 FIXME("Leftover resource %p with type %s (%#x).\n",
874 resource, debug_d3dresourcetype(resource->type), resource->type);
878 if (device->contexts)
879 ERR("Context array not freed!\n");
880 if (device->hardwareCursor)
881 DestroyCursor(device->hardwareCursor);
882 device->hardwareCursor = 0;
884 wined3d_decref(device->wined3d);
885 device->wined3d = NULL;
886 HeapFree(GetProcessHeap(), 0, device);
887 TRACE("Freed device %p.\n", device);
893 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
895 TRACE("device %p.\n", device);
897 return device->swapchain_count;
900 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
902 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
904 if (swapchain_idx >= device->swapchain_count)
906 WARN("swapchain_idx %u >= swapchain_count %u.\n",
907 swapchain_idx, device->swapchain_count);
911 return device->swapchains[swapchain_idx];
914 static void device_load_logo(struct wined3d_device *device, const char *filename)
916 struct wined3d_color_key color_key;
920 HDC dcb = NULL, dcs = NULL;
922 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
925 GetObjectA(hbm, sizeof(BITMAP), &bm);
926 dcb = CreateCompatibleDC(NULL);
928 SelectObject(dcb, hbm);
932 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
935 memset(&bm, 0, sizeof(bm));
940 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0,
941 WINED3D_POOL_SYSTEM_MEM, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_TYPE_OPENGL, WINED3D_SURFACE_MAPPABLE,
942 NULL, &wined3d_null_parent_ops, &device->logo_surface);
945 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
951 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
953 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
954 wined3d_surface_releasedc(device->logo_surface, dcs);
956 color_key.color_space_low_value = 0;
957 color_key.color_space_high_value = 0;
958 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
962 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
963 /* Fill the surface with a white color to show that wined3d is there */
964 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
968 if (dcb) DeleteDC(dcb);
969 if (hbm) DeleteObject(hbm);
972 /* Context activation is done by the caller. */
973 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
975 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
976 unsigned int i, j, count;
977 /* Under DirectX you can sample even if no texture is bound, whereas
978 * OpenGL will only allow that when a valid texture is bound.
979 * We emulate this by creating dummy textures and binding them
980 * to each texture stage when the currently set D3D texture is NULL. */
983 if (gl_info->supported[APPLE_CLIENT_STORAGE])
985 /* The dummy texture does not have client storage backing */
986 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
987 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
990 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
991 for (i = 0; i < count; ++i)
993 DWORD color = 0x000000ff;
995 /* Make appropriate texture active */
996 context_active_texture(context, gl_info, i);
998 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
999 checkGLcall("glGenTextures");
1000 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
1002 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
1003 checkGLcall("glBindTexture");
1005 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
1006 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1007 checkGLcall("glTexImage2D");
1009 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1011 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
1012 checkGLcall("glGenTextures");
1013 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
1015 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1016 checkGLcall("glBindTexture");
1018 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
1019 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1020 checkGLcall("glTexImage2D");
1023 if (gl_info->supported[EXT_TEXTURE3D])
1025 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
1026 checkGLcall("glGenTextures");
1027 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
1029 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1030 checkGLcall("glBindTexture");
1032 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
1033 checkGLcall("glTexImage3D");
1036 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1038 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
1039 checkGLcall("glGenTextures");
1040 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
1042 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1043 checkGLcall("glBindTexture");
1045 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
1047 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
1048 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1049 checkGLcall("glTexImage2D");
1054 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1056 /* Re-enable because if supported it is enabled by default */
1057 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1058 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1064 /* Context activation is done by the caller. */
1065 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
1067 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1070 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1072 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
1073 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
1076 if (gl_info->supported[EXT_TEXTURE3D])
1078 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
1079 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
1082 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1084 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
1085 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
1088 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
1089 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
1092 memset(device->dummy_texture_cube, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_cube));
1093 memset(device->dummy_texture_3d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_3d));
1094 memset(device->dummy_texture_rect, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_rect));
1095 memset(device->dummy_texture_2d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_2d));
1098 static LONG fullscreen_style(LONG style)
1100 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1101 style |= WS_POPUP | WS_SYSMENU;
1102 style &= ~(WS_CAPTION | WS_THICKFRAME);
1107 static LONG fullscreen_exstyle(LONG exstyle)
1109 /* Filter out window decorations. */
1110 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1115 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1117 BOOL filter_messages;
1118 LONG style, exstyle;
1120 TRACE("Setting up window %p for fullscreen mode.\n", window);
1122 if (device->style || device->exStyle)
1124 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1125 window, device->style, device->exStyle);
1128 device->style = GetWindowLongW(window, GWL_STYLE);
1129 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1131 style = fullscreen_style(device->style);
1132 exstyle = fullscreen_exstyle(device->exStyle);
1134 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1135 device->style, device->exStyle, style, exstyle);
1137 filter_messages = device->filter_messages;
1138 device->filter_messages = TRUE;
1140 SetWindowLongW(window, GWL_STYLE, style);
1141 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1142 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1144 device->filter_messages = filter_messages;
1147 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1149 BOOL filter_messages;
1150 LONG style, exstyle;
1152 if (!device->style && !device->exStyle) return;
1154 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1155 window, device->style, device->exStyle);
1157 style = GetWindowLongW(window, GWL_STYLE);
1158 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1160 filter_messages = device->filter_messages;
1161 device->filter_messages = TRUE;
1163 /* Only restore the style if the application didn't modify it during the
1164 * fullscreen phase. Some applications change it before calling Reset()
1165 * when switching between windowed and fullscreen modes (HL2), some
1166 * depend on the original style (Eve Online). */
1167 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1169 SetWindowLongW(window, GWL_STYLE, device->style);
1170 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1172 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1174 device->filter_messages = filter_messages;
1176 /* Delete the old values. */
1178 device->exStyle = 0;
1181 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1183 TRACE("device %p, window %p.\n", device, window);
1185 if (!wined3d_register_window(window, device))
1187 ERR("Failed to register window %p.\n", window);
1191 InterlockedExchangePointer((void **)&device->focus_window, window);
1192 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1197 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1199 TRACE("device %p.\n", device);
1201 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1202 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1205 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1206 struct wined3d_swapchain_desc *swapchain_desc)
1208 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1209 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1210 struct wined3d_swapchain *swapchain = NULL;
1211 struct wined3d_context *context;
1216 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1218 if (device->d3d_initialized)
1219 return WINED3DERR_INVALIDCALL;
1220 if (!device->adapter->opengl)
1221 return WINED3DERR_INVALIDCALL;
1223 device->valid_rt_mask = 0;
1224 for (i = 0; i < gl_info->limits.buffers; ++i)
1225 device->valid_rt_mask |= (1 << i);
1226 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1227 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1229 /* Initialize the texture unit mapping to a 1:1 mapping */
1230 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1232 if (state < gl_info->limits.fragment_samplers)
1234 device->texUnitMap[state] = state;
1235 device->rev_tex_unit_map[state] = state;
1239 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1240 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1244 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
1246 TRACE("Shader private data couldn't be allocated\n");
1249 if (FAILED(hr = device->blitter->alloc_private(device)))
1251 TRACE("Blitter private data couldn't be allocated\n");
1255 /* Setup the implicit swapchain. This also initializes a context. */
1256 TRACE("Creating implicit swapchain\n");
1257 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1258 swapchain_desc, &swapchain);
1261 WARN("Failed to create implicit swapchain\n");
1265 device->swapchain_count = 1;
1266 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1267 if (!device->swapchains)
1269 ERR("Out of memory!\n");
1272 device->swapchains[0] = swapchain;
1274 if (swapchain->back_buffers && swapchain->back_buffers[0])
1276 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1277 device->fb.render_targets[0] = swapchain->back_buffers[0];
1281 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1282 device->fb.render_targets[0] = swapchain->front_buffer;
1284 wined3d_surface_incref(device->fb.render_targets[0]);
1286 /* Depth Stencil support */
1287 device->fb.depth_stencil = device->auto_depth_stencil;
1288 if (device->fb.depth_stencil)
1289 wined3d_surface_incref(device->fb.depth_stencil);
1291 /* Set up some starting GL setup */
1293 /* Setup all the devices defaults */
1294 stateblock_init_default_state(device->stateBlock);
1296 context = context_acquire(device, swapchain->front_buffer);
1298 create_dummy_textures(device, context);
1302 /* Initialize the current view state */
1303 device->view_ident = 1;
1304 device->contexts[0]->last_was_rhw = 0;
1306 switch (wined3d_settings.offscreen_rendering_mode)
1309 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1312 case ORM_BACKBUFFER:
1314 if (context_get_current()->aux_buffers > 0)
1316 TRACE("Using auxiliary buffer for offscreen rendering\n");
1317 device->offscreenBuffer = GL_AUX0;
1321 TRACE("Using back buffer for offscreen rendering\n");
1322 device->offscreenBuffer = GL_BACK;
1327 TRACE("All defaults now set up, leaving 3D init.\n");
1330 context_release(context);
1332 /* Clear the screen */
1333 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1334 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1337 device->d3d_initialized = TRUE;
1339 if (wined3d_settings.logo)
1340 device_load_logo(device, wined3d_settings.logo);
1344 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1345 HeapFree(GetProcessHeap(), 0, device->swapchains);
1346 device->swapchain_count = 0;
1348 wined3d_swapchain_decref(swapchain);
1349 if (device->blit_priv)
1350 device->blitter->free_private(device);
1351 if (device->shader_priv)
1352 device->shader_backend->shader_free_private(device);
1357 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1358 struct wined3d_swapchain_desc *swapchain_desc)
1360 struct wined3d_swapchain *swapchain = NULL;
1363 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1365 /* Setup the implicit swapchain */
1366 TRACE("Creating implicit swapchain\n");
1367 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1368 swapchain_desc, &swapchain);
1371 WARN("Failed to create implicit swapchain\n");
1375 device->swapchain_count = 1;
1376 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1377 if (!device->swapchains)
1379 ERR("Out of memory!\n");
1382 device->swapchains[0] = swapchain;
1386 wined3d_swapchain_decref(swapchain);
1390 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1392 struct wined3d_resource *resource, *cursor;
1393 const struct wined3d_gl_info *gl_info;
1394 struct wined3d_context *context;
1395 struct wined3d_surface *surface;
1398 TRACE("device %p.\n", device);
1400 if (!device->d3d_initialized)
1401 return WINED3DERR_INVALIDCALL;
1403 /* Force making the context current again, to verify it is still valid
1404 * (workaround for broken drivers) */
1405 context_set_current(NULL);
1406 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1407 * it was created. Thus make sure a context is active for the glDelete* calls
1409 context = context_acquire(device, NULL);
1410 gl_info = context->gl_info;
1412 if (device->logo_surface)
1413 wined3d_surface_decref(device->logo_surface);
1415 stateblock_unbind_resources(device->stateBlock);
1417 /* Unload resources */
1418 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1420 TRACE("Unloading resource %p.\n", resource);
1422 resource->resource_ops->resource_unload(resource);
1425 TRACE("Deleting high order patches\n");
1426 for (i = 0; i < PATCHMAP_SIZE; ++i)
1428 struct wined3d_rect_patch *patch;
1429 struct list *e1, *e2;
1431 LIST_FOR_EACH_SAFE(e1, e2, &device->patches[i])
1433 patch = LIST_ENTRY(e1, struct wined3d_rect_patch, entry);
1434 wined3d_device_delete_patch(device, patch->Handle);
1438 /* Delete the mouse cursor texture */
1439 if (device->cursorTexture)
1442 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
1444 device->cursorTexture = 0;
1447 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1448 * private data, it might contain opengl pointers
1450 if (device->depth_blt_texture)
1453 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1455 device->depth_blt_texture = 0;
1458 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1459 device->blitter->free_private(device);
1460 device->shader_backend->shader_free_private(device);
1462 /* Release the buffers (with sanity checks)*/
1463 if (device->onscreen_depth_stencil)
1465 surface = device->onscreen_depth_stencil;
1466 device->onscreen_depth_stencil = NULL;
1467 wined3d_surface_decref(surface);
1470 if (device->fb.depth_stencil)
1472 surface = device->fb.depth_stencil;
1474 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1476 device->fb.depth_stencil = NULL;
1477 wined3d_surface_decref(surface);
1480 if (device->auto_depth_stencil)
1482 surface = device->auto_depth_stencil;
1483 device->auto_depth_stencil = NULL;
1484 if (wined3d_surface_decref(surface))
1485 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1488 for (i = 1; i < gl_info->limits.buffers; ++i)
1490 wined3d_device_set_render_target(device, i, NULL, FALSE);
1493 surface = device->fb.render_targets[0];
1494 TRACE("Setting rendertarget 0 to NULL\n");
1495 device->fb.render_targets[0] = NULL;
1496 TRACE("Releasing the render target at %p\n", surface);
1497 wined3d_surface_decref(surface);
1499 context_release(context);
1501 for (i = 0; i < device->swapchain_count; ++i)
1503 TRACE("Releasing the implicit swapchain %u.\n", i);
1504 if (wined3d_swapchain_decref(device->swapchains[i]))
1505 FIXME("Something's still holding the implicit swapchain.\n");
1508 HeapFree(GetProcessHeap(), 0, device->swapchains);
1509 device->swapchains = NULL;
1510 device->swapchain_count = 0;
1512 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1513 device->fb.render_targets = NULL;
1515 device->d3d_initialized = FALSE;
1520 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1524 for (i = 0; i < device->swapchain_count; ++i)
1526 TRACE("Releasing the implicit swapchain %u.\n", i);
1527 if (wined3d_swapchain_decref(device->swapchains[i]))
1528 FIXME("Something's still holding the implicit swapchain.\n");
1531 HeapFree(GetProcessHeap(), 0, device->swapchains);
1532 device->swapchains = NULL;
1533 device->swapchain_count = 0;
1537 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1538 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1539 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1541 * There is no way to deactivate thread safety once it is enabled.
1543 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1545 TRACE("device %p.\n", device);
1547 /* For now just store the flag (needed in case of ddraw). */
1548 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1551 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1553 TRACE("device %p.\n", device);
1555 TRACE("Emulating %d MB, returning %d MB left.\n",
1556 device->adapter->TextureRam / (1024 * 1024),
1557 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1559 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1562 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1563 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1565 struct wined3d_stream_state *stream;
1566 struct wined3d_buffer *prev_buffer;
1568 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1569 device, stream_idx, buffer, offset, stride);
1571 if (stream_idx >= MAX_STREAMS)
1573 WARN("Stream index %u out of range.\n", stream_idx);
1574 return WINED3DERR_INVALIDCALL;
1576 else if (offset & 0x3)
1578 WARN("Offset %u is not 4 byte aligned.\n", offset);
1579 return WINED3DERR_INVALIDCALL;
1582 stream = &device->updateStateBlock->state.streams[stream_idx];
1583 prev_buffer = stream->buffer;
1585 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1587 if (prev_buffer == buffer
1588 && stream->stride == stride
1589 && stream->offset == offset)
1591 TRACE("Application is setting the old values over, nothing to do.\n");
1595 stream->buffer = buffer;
1598 stream->stride = stride;
1599 stream->offset = offset;
1602 /* Handle recording of state blocks. */
1603 if (device->isRecordingState)
1605 TRACE("Recording... not performing anything.\n");
1607 wined3d_buffer_incref(buffer);
1609 wined3d_buffer_decref(prev_buffer);
1615 InterlockedIncrement(&buffer->resource.bind_count);
1616 wined3d_buffer_incref(buffer);
1620 InterlockedDecrement(&prev_buffer->resource.bind_count);
1621 wined3d_buffer_decref(prev_buffer);
1624 device_invalidate_state(device, STATE_STREAMSRC);
1629 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1630 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1632 struct wined3d_stream_state *stream;
1634 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1635 device, stream_idx, buffer, offset, stride);
1637 if (stream_idx >= MAX_STREAMS)
1639 WARN("Stream index %u out of range.\n", stream_idx);
1640 return WINED3DERR_INVALIDCALL;
1643 stream = &device->stateBlock->state.streams[stream_idx];
1644 *buffer = stream->buffer;
1646 wined3d_buffer_incref(*buffer);
1648 *offset = stream->offset;
1649 *stride = stream->stride;
1654 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1656 struct wined3d_stream_state *stream;
1657 UINT old_flags, old_freq;
1659 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1661 /* Verify input. At least in d3d9 this is invalid. */
1662 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1664 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1665 return WINED3DERR_INVALIDCALL;
1667 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1669 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1670 return WINED3DERR_INVALIDCALL;
1674 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1675 return WINED3DERR_INVALIDCALL;
1678 stream = &device->updateStateBlock->state.streams[stream_idx];
1679 old_flags = stream->flags;
1680 old_freq = stream->frequency;
1682 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1683 stream->frequency = divider & 0x7fffff;
1685 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1687 if (stream->frequency != old_freq || stream->flags != old_flags)
1688 device_invalidate_state(device, STATE_STREAMSRC);
1693 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1694 UINT stream_idx, UINT *divider)
1696 struct wined3d_stream_state *stream;
1698 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1700 stream = &device->updateStateBlock->state.streams[stream_idx];
1701 *divider = stream->flags | stream->frequency;
1703 TRACE("Returning %#x.\n", *divider);
1708 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1709 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1711 TRACE("device %p, state %s, matrix %p.\n",
1712 device, debug_d3dtstype(d3dts), matrix);
1713 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1714 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1715 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1716 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1718 /* Handle recording of state blocks. */
1719 if (device->isRecordingState)
1721 TRACE("Recording... not performing anything.\n");
1722 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1723 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1727 /* If the new matrix is the same as the current one,
1728 * we cut off any further processing. this seems to be a reasonable
1729 * optimization because as was noticed, some apps (warcraft3 for example)
1730 * tend towards setting the same matrix repeatedly for some reason.
1732 * From here on we assume that the new matrix is different, wherever it matters. */
1733 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1735 TRACE("The application is setting the same matrix over again.\n");
1739 device->stateBlock->state.transforms[d3dts] = *matrix;
1740 if (d3dts == WINED3D_TS_VIEW)
1741 device->view_ident = !memcmp(matrix, &identity, sizeof(identity));
1743 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1744 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1747 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1748 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1750 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1752 *matrix = device->stateBlock->state.transforms[state];
1755 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1756 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1758 const struct wined3d_matrix *mat;
1759 struct wined3d_matrix temp;
1761 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1763 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1764 * below means it will be recorded in a state block change, but it
1765 * works regardless where it is recorded.
1766 * If this is found to be wrong, change to StateBlock. */
1767 if (state > HIGHEST_TRANSFORMSTATE)
1769 WARN("Unhandled transform state %#x.\n", state);
1773 mat = &device->updateStateBlock->state.transforms[state];
1774 multiply_matrix(&temp, mat, matrix);
1776 /* Apply change via set transform - will reapply to eg. lights this way. */
1777 wined3d_device_set_transform(device, state, &temp);
1780 /* Note lights are real special cases. Although the device caps state only
1781 * e.g. 8 are supported, you can reference any indexes you want as long as
1782 * that number max are enabled at any one point in time. Therefore since the
1783 * indices can be anything, we need a hashmap of them. However, this causes
1784 * stateblock problems. When capturing the state block, I duplicate the
1785 * hashmap, but when recording, just build a chain pretty much of commands to
1787 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1788 UINT light_idx, const struct wined3d_light *light)
1790 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1791 struct wined3d_light_info *object = NULL;
1795 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1797 /* Check the parameter range. Need for speed most wanted sets junk lights
1798 * which confuse the GL driver. */
1800 return WINED3DERR_INVALIDCALL;
1802 switch (light->type)
1804 case WINED3D_LIGHT_POINT:
1805 case WINED3D_LIGHT_SPOT:
1806 case WINED3D_LIGHT_PARALLELPOINT:
1807 case WINED3D_LIGHT_GLSPOT:
1808 /* Incorrect attenuation values can cause the gl driver to crash.
1809 * Happens with Need for speed most wanted. */
1810 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1812 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1813 return WINED3DERR_INVALIDCALL;
1817 case WINED3D_LIGHT_DIRECTIONAL:
1818 /* Ignores attenuation */
1822 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1823 return WINED3DERR_INVALIDCALL;
1826 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1828 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1829 if (object->OriginalIndex == light_idx)
1836 TRACE("Adding new light\n");
1837 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1840 ERR("Out of memory error when allocating a light\n");
1841 return E_OUTOFMEMORY;
1843 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1844 object->glIndex = -1;
1845 object->OriginalIndex = light_idx;
1848 /* Initialize the object. */
1849 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1850 light_idx, light->type,
1851 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1852 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1853 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1854 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1855 light->direction.x, light->direction.y, light->direction.z);
1856 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1857 light->range, light->falloff, light->theta, light->phi);
1859 /* Save away the information. */
1860 object->OriginalParms = *light;
1862 switch (light->type)
1864 case WINED3D_LIGHT_POINT:
1866 object->lightPosn[0] = light->position.x;
1867 object->lightPosn[1] = light->position.y;
1868 object->lightPosn[2] = light->position.z;
1869 object->lightPosn[3] = 1.0f;
1870 object->cutoff = 180.0f;
1874 case WINED3D_LIGHT_DIRECTIONAL:
1876 object->lightPosn[0] = -light->direction.x;
1877 object->lightPosn[1] = -light->direction.y;
1878 object->lightPosn[2] = -light->direction.z;
1879 object->lightPosn[3] = 0.0f;
1880 object->exponent = 0.0f;
1881 object->cutoff = 180.0f;
1884 case WINED3D_LIGHT_SPOT:
1886 object->lightPosn[0] = light->position.x;
1887 object->lightPosn[1] = light->position.y;
1888 object->lightPosn[2] = light->position.z;
1889 object->lightPosn[3] = 1.0f;
1892 object->lightDirn[0] = light->direction.x;
1893 object->lightDirn[1] = light->direction.y;
1894 object->lightDirn[2] = light->direction.z;
1895 object->lightDirn[3] = 1.0f;
1897 /* opengl-ish and d3d-ish spot lights use too different models
1898 * for the light "intensity" as a function of the angle towards
1899 * the main light direction, so we only can approximate very
1900 * roughly. However, spot lights are rather rarely used in games
1901 * (if ever used at all). Furthermore if still used, probably
1902 * nobody pays attention to such details. */
1903 if (!light->falloff)
1905 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1906 * equations have the falloff resp. exponent parameter as an
1907 * exponent, so the spot light lighting will always be 1.0 for
1908 * both of them, and we don't have to care for the rest of the
1909 * rather complex calculation. */
1910 object->exponent = 0.0f;
1914 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1917 object->exponent = -0.3f / logf(cosf(rho / 2));
1920 if (object->exponent > 128.0f)
1921 object->exponent = 128.0f;
1923 object->cutoff = (float)(light->phi * 90 / M_PI);
1928 FIXME("Unrecognized light type %#x.\n", light->type);
1931 /* Update the live definitions if the light is currently assigned a glIndex. */
1932 if (object->glIndex != -1 && !device->isRecordingState)
1933 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1938 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1939 UINT light_idx, struct wined3d_light *light)
1941 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1942 struct wined3d_light_info *light_info = NULL;
1945 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1947 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1949 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1950 if (light_info->OriginalIndex == light_idx)
1957 TRACE("Light information requested but light not defined\n");
1958 return WINED3DERR_INVALIDCALL;
1961 *light = light_info->OriginalParms;
1965 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1967 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1968 struct wined3d_light_info *light_info = NULL;
1971 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1973 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1975 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1976 if (light_info->OriginalIndex == light_idx)
1980 TRACE("Found light %p.\n", light_info);
1982 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1985 TRACE("Light enabled requested but light not defined, so defining one!\n");
1986 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1988 /* Search for it again! Should be fairly quick as near head of list. */
1989 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1991 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1992 if (light_info->OriginalIndex == light_idx)
1998 FIXME("Adding default lights has failed dismally\n");
1999 return WINED3DERR_INVALIDCALL;
2005 if (light_info->glIndex != -1)
2007 if (!device->isRecordingState)
2008 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
2010 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
2011 light_info->glIndex = -1;
2015 TRACE("Light already disabled, nothing to do\n");
2017 light_info->enabled = FALSE;
2021 light_info->enabled = TRUE;
2022 if (light_info->glIndex != -1)
2024 TRACE("Nothing to do as light was enabled\n");
2029 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2030 /* Find a free GL light. */
2031 for (i = 0; i < gl_info->limits.lights; ++i)
2033 if (!device->updateStateBlock->state.lights[i])
2035 device->updateStateBlock->state.lights[i] = light_info;
2036 light_info->glIndex = i;
2040 if (light_info->glIndex == -1)
2042 /* Our tests show that Windows returns D3D_OK in this situation, even with
2043 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
2044 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
2045 * as well for those lights.
2047 * TODO: Test how this affects rendering. */
2048 WARN("Too many concurrently active lights\n");
2052 /* i == light_info->glIndex */
2053 if (!device->isRecordingState)
2054 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
2061 HRESULT CDECL wined3d_device_get_light_enable(const 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;
2067 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
2069 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2071 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2072 if (light_info->OriginalIndex == light_idx)
2079 TRACE("Light enabled state requested but light not defined.\n");
2080 return WINED3DERR_INVALIDCALL;
2082 /* true is 128 according to SetLightEnable */
2083 *enable = light_info->enabled ? 128 : 0;
2087 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
2088 UINT plane_idx, const struct wined3d_vec4 *plane)
2090 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2092 /* Validate plane_idx. */
2093 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2095 TRACE("Application has requested clipplane this device doesn't support.\n");
2096 return WINED3DERR_INVALIDCALL;
2099 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2101 if (!memcmp(&device->updateStateBlock->state.clip_planes[plane_idx], plane, sizeof(*plane)))
2103 TRACE("Application is setting old values over, nothing to do.\n");
2107 device->updateStateBlock->state.clip_planes[plane_idx] = *plane;
2109 /* Handle recording of state blocks. */
2110 if (device->isRecordingState)
2112 TRACE("Recording... not performing anything.\n");
2116 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2121 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
2122 UINT plane_idx, struct wined3d_vec4 *plane)
2124 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2126 /* Validate plane_idx. */
2127 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2129 TRACE("Application has requested clipplane this device doesn't support.\n");
2130 return WINED3DERR_INVALIDCALL;
2133 *plane = device->stateBlock->state.clip_planes[plane_idx];
2138 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2139 const struct wined3d_clip_status *clip_status)
2141 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2144 return WINED3DERR_INVALIDCALL;
2149 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2150 struct wined3d_clip_status *clip_status)
2152 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2155 return WINED3DERR_INVALIDCALL;
2160 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2162 TRACE("device %p, material %p.\n", device, material);
2164 device->updateStateBlock->changed.material = TRUE;
2165 device->updateStateBlock->state.material = *material;
2167 /* Handle recording of state blocks */
2168 if (device->isRecordingState)
2170 TRACE("Recording... not performing anything.\n");
2174 device_invalidate_state(device, STATE_MATERIAL);
2177 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2179 TRACE("device %p, material %p.\n", device, material);
2181 *material = device->updateStateBlock->state.material;
2183 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2184 material->diffuse.r, material->diffuse.g,
2185 material->diffuse.b, material->diffuse.a);
2186 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2187 material->ambient.r, material->ambient.g,
2188 material->ambient.b, material->ambient.a);
2189 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2190 material->specular.r, material->specular.g,
2191 material->specular.b, material->specular.a);
2192 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2193 material->emissive.r, material->emissive.g,
2194 material->emissive.b, material->emissive.a);
2195 TRACE("power %.8e.\n", material->power);
2198 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2199 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2201 struct wined3d_buffer *prev_buffer;
2203 TRACE("device %p, buffer %p, format %s.\n",
2204 device, buffer, debug_d3dformat(format_id));
2206 prev_buffer = device->updateStateBlock->state.index_buffer;
2208 device->updateStateBlock->changed.indices = TRUE;
2209 device->updateStateBlock->state.index_buffer = buffer;
2210 device->updateStateBlock->state.index_format = format_id;
2212 /* Handle recording of state blocks. */
2213 if (device->isRecordingState)
2215 TRACE("Recording... not performing anything.\n");
2217 wined3d_buffer_incref(buffer);
2219 wined3d_buffer_decref(prev_buffer);
2223 if (prev_buffer != buffer)
2225 device_invalidate_state(device, STATE_INDEXBUFFER);
2228 InterlockedIncrement(&buffer->resource.bind_count);
2229 wined3d_buffer_incref(buffer);
2233 InterlockedDecrement(&prev_buffer->resource.bind_count);
2234 wined3d_buffer_decref(prev_buffer);
2239 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device)
2241 TRACE("device %p.\n", device);
2243 return device->stateBlock->state.index_buffer;
2246 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2248 TRACE("device %p, base_index %d.\n", device, base_index);
2250 device->updateStateBlock->state.base_vertex_index = base_index;
2253 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2255 TRACE("device %p.\n", device);
2257 return device->stateBlock->state.base_vertex_index;
2260 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2262 TRACE("device %p, viewport %p.\n", device, viewport);
2263 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2264 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2266 device->updateStateBlock->changed.viewport = TRUE;
2267 device->updateStateBlock->state.viewport = *viewport;
2269 /* Handle recording of state blocks */
2270 if (device->isRecordingState)
2272 TRACE("Recording... not performing anything\n");
2276 device_invalidate_state(device, STATE_VIEWPORT);
2279 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2281 TRACE("device %p, viewport %p.\n", device, viewport);
2283 *viewport = device->stateBlock->state.viewport;
2286 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2287 enum wined3d_render_state state, DWORD value)
2289 DWORD old_value = device->stateBlock->state.render_states[state];
2291 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2293 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2294 device->updateStateBlock->state.render_states[state] = value;
2296 /* Handle recording of state blocks. */
2297 if (device->isRecordingState)
2299 TRACE("Recording... not performing anything.\n");
2303 /* Compared here and not before the assignment to allow proper stateblock recording. */
2304 if (value == old_value)
2305 TRACE("Application is setting the old value over, nothing to do.\n");
2307 device_invalidate_state(device, STATE_RENDER(state));
2310 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2312 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2314 return device->stateBlock->state.render_states[state];
2317 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2318 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2322 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2323 device, sampler_idx, debug_d3dsamplerstate(state), value);
2325 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2326 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2328 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2329 / sizeof(*device->stateBlock->state.sampler_states))
2331 WARN("Invalid sampler %u.\n", sampler_idx);
2332 return; /* Windows accepts overflowing this array ... we do not. */
2335 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2336 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2337 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2339 /* Handle recording of state blocks. */
2340 if (device->isRecordingState)
2342 TRACE("Recording... not performing anything.\n");
2346 if (old_value == value)
2348 TRACE("Application is setting the old value over, nothing to do.\n");
2352 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2355 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2356 UINT sampler_idx, enum wined3d_sampler_state state)
2358 TRACE("device %p, sampler_idx %u, state %s.\n",
2359 device, sampler_idx, debug_d3dsamplerstate(state));
2361 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2362 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2364 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2365 / sizeof(*device->stateBlock->state.sampler_states))
2367 WARN("Invalid sampler %u.\n", sampler_idx);
2368 return 0; /* Windows accepts overflowing this array ... we do not. */
2371 return device->stateBlock->state.sampler_states[sampler_idx][state];
2374 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2376 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2378 device->updateStateBlock->changed.scissorRect = TRUE;
2379 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2381 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2384 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2386 if (device->isRecordingState)
2388 TRACE("Recording... not performing anything.\n");
2392 device_invalidate_state(device, STATE_SCISSORRECT);
2395 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2397 TRACE("device %p, rect %p.\n", device, rect);
2399 *rect = device->updateStateBlock->state.scissor_rect;
2400 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2403 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2404 struct wined3d_vertex_declaration *declaration)
2406 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2408 TRACE("device %p, declaration %p.\n", device, declaration);
2411 wined3d_vertex_declaration_incref(declaration);
2413 wined3d_vertex_declaration_decref(prev);
2415 device->updateStateBlock->state.vertex_declaration = declaration;
2416 device->updateStateBlock->changed.vertexDecl = TRUE;
2418 if (device->isRecordingState)
2420 TRACE("Recording... not performing anything.\n");
2424 if (declaration == prev)
2426 /* Checked after the assignment to allow proper stateblock recording. */
2427 TRACE("Application is setting the old declaration over, nothing to do.\n");
2431 device_invalidate_state(device, STATE_VDECL);
2434 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2436 TRACE("device %p.\n", device);
2438 return device->stateBlock->state.vertex_declaration;
2441 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2443 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2445 TRACE("device %p, shader %p.\n", device, shader);
2448 wined3d_shader_incref(shader);
2450 wined3d_shader_decref(prev);
2452 device->updateStateBlock->state.vertex_shader = shader;
2453 device->updateStateBlock->changed.vertexShader = TRUE;
2455 if (device->isRecordingState)
2457 TRACE("Recording... not performing anything.\n");
2463 TRACE("Application is setting the old shader over, nothing to do.\n");
2467 device_invalidate_state(device, STATE_VSHADER);
2470 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2472 TRACE("device %p.\n", device);
2474 return device->stateBlock->state.vertex_shader;
2477 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2478 UINT start_register, const BOOL *constants, UINT bool_count)
2480 UINT count = min(bool_count, MAX_CONST_B - start_register);
2483 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2484 device, start_register, constants, bool_count);
2486 if (!constants || start_register >= MAX_CONST_B)
2487 return WINED3DERR_INVALIDCALL;
2489 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2490 for (i = 0; i < count; ++i)
2491 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2493 for (i = start_register; i < count + start_register; ++i)
2494 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2496 if (!device->isRecordingState)
2497 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2502 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2503 UINT start_register, BOOL *constants, UINT bool_count)
2505 UINT count = min(bool_count, MAX_CONST_B - start_register);
2507 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2508 device, start_register, constants, bool_count);
2510 if (!constants || start_register >= MAX_CONST_B)
2511 return WINED3DERR_INVALIDCALL;
2513 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2518 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2519 UINT start_register, const int *constants, UINT vector4i_count)
2521 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2524 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2525 device, start_register, constants, vector4i_count);
2527 if (!constants || start_register >= MAX_CONST_I)
2528 return WINED3DERR_INVALIDCALL;
2530 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2531 for (i = 0; i < count; ++i)
2532 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2533 constants[i * 4], constants[i * 4 + 1],
2534 constants[i * 4 + 2], constants[i * 4 + 3]);
2536 for (i = start_register; i < count + start_register; ++i)
2537 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2539 if (!device->isRecordingState)
2540 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2545 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2546 UINT start_register, int *constants, UINT vector4i_count)
2548 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2550 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2551 device, start_register, constants, vector4i_count);
2553 if (!constants || start_register >= MAX_CONST_I)
2554 return WINED3DERR_INVALIDCALL;
2556 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2560 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2561 UINT start_register, const float *constants, UINT vector4f_count)
2565 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2566 device, start_register, constants, vector4f_count);
2568 /* Specifically test start_register > limit to catch MAX_UINT overflows
2569 * when adding start_register + vector4f_count. */
2571 || start_register + vector4f_count > device->d3d_vshader_constantF
2572 || start_register > device->d3d_vshader_constantF)
2573 return WINED3DERR_INVALIDCALL;
2575 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2576 constants, vector4f_count * sizeof(float) * 4);
2579 for (i = 0; i < vector4f_count; ++i)
2580 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2581 constants[i * 4], constants[i * 4 + 1],
2582 constants[i * 4 + 2], constants[i * 4 + 3]);
2585 if (!device->isRecordingState)
2587 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2588 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2591 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2592 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2597 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2598 UINT start_register, float *constants, UINT vector4f_count)
2600 int count = min(vector4f_count, device->d3d_vshader_constantF - start_register);
2602 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2603 device, start_register, constants, vector4f_count);
2605 if (!constants || count < 0)
2606 return WINED3DERR_INVALIDCALL;
2608 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2613 static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2617 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2619 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2623 static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2625 DWORD i = device->rev_tex_unit_map[unit];
2626 DWORD j = device->texUnitMap[stage];
2628 device->texUnitMap[stage] = unit;
2629 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2630 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2632 device->rev_tex_unit_map[unit] = stage;
2633 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2634 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2637 static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2641 device->fixed_function_usage_map = 0;
2642 for (i = 0; i < MAX_TEXTURES; ++i)
2644 const struct wined3d_state *state = &device->stateBlock->state;
2645 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2646 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2647 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2648 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2649 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2650 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2651 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2652 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2654 /* Not used, and disable higher stages. */
2655 if (color_op == WINED3D_TOP_DISABLE)
2658 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2659 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2660 || ((color_arg3 == WINED3DTA_TEXTURE)
2661 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2662 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2663 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2664 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2665 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2666 device->fixed_function_usage_map |= (1 << i);
2668 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2669 && i < MAX_TEXTURES - 1)
2670 device->fixed_function_usage_map |= (1 << (i + 1));
2674 static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2676 unsigned int i, tex;
2679 device_update_fixed_function_usage_map(device);
2680 ffu_map = device->fixed_function_usage_map;
2682 if (device->max_ffp_textures == gl_info->limits.texture_stages
2683 || device->stateBlock->state.lowest_disabled_stage <= device->max_ffp_textures)
2685 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2687 if (!(ffu_map & 1)) continue;
2689 if (device->texUnitMap[i] != i)
2691 device_map_stage(device, i, i);
2692 device_invalidate_state(device, STATE_SAMPLER(i));
2693 device_invalidate_texture_stage(device, i);
2699 /* Now work out the mapping */
2701 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2703 if (!(ffu_map & 1)) continue;
2705 if (device->texUnitMap[i] != tex)
2707 device_map_stage(device, i, tex);
2708 device_invalidate_state(device, STATE_SAMPLER(i));
2709 device_invalidate_texture_stage(device, i);
2716 static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2718 const enum wined3d_sampler_texture_type *sampler_type =
2719 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2722 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2724 if (sampler_type[i] && device->texUnitMap[i] != i)
2726 device_map_stage(device, i, i);
2727 device_invalidate_state(device, STATE_SAMPLER(i));
2728 if (i < gl_info->limits.texture_stages)
2729 device_invalidate_texture_stage(device, i);
2734 static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2735 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2736 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2738 DWORD current_mapping = device->rev_tex_unit_map[unit];
2740 /* Not currently used */
2741 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2743 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2744 /* Used by a fragment sampler */
2746 if (!pshader_sampler_tokens) {
2747 /* No pixel shader, check fixed function */
2748 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2751 /* Pixel shader, check the shader's sampler map */
2752 return !pshader_sampler_tokens[current_mapping];
2755 /* Used by a vertex sampler */
2756 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2759 static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2761 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2762 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2763 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2764 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2769 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2770 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2771 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2774 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2775 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2776 if (vshader_sampler_type[i])
2778 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2780 /* Already mapped somewhere */
2786 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2788 device_map_stage(device, vsampler_idx, start);
2789 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2801 void device_update_tex_unit_map(struct wined3d_device *device)
2803 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2804 const struct wined3d_state *state = &device->stateBlock->state;
2805 BOOL vs = use_vs(state);
2806 BOOL ps = use_ps(state);
2809 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2810 * that would be really messy and require shader recompilation
2811 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2812 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2815 device_map_psamplers(device, gl_info);
2817 device_map_fixed_function_samplers(device, gl_info);
2820 device_map_vsamplers(device, ps, gl_info);
2823 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2825 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2827 TRACE("device %p, shader %p.\n", device, shader);
2830 wined3d_shader_incref(shader);
2832 wined3d_shader_decref(prev);
2834 device->updateStateBlock->state.pixel_shader = shader;
2835 device->updateStateBlock->changed.pixelShader = TRUE;
2837 if (device->isRecordingState)
2839 TRACE("Recording... not performing anything.\n");
2845 TRACE("Application is setting the old shader over, nothing to do.\n");
2849 device_invalidate_state(device, STATE_PIXELSHADER);
2852 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2854 TRACE("device %p.\n", device);
2856 return device->stateBlock->state.pixel_shader;
2859 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2860 UINT start_register, const BOOL *constants, UINT bool_count)
2862 UINT count = min(bool_count, MAX_CONST_B - start_register);
2865 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2866 device, start_register, constants, bool_count);
2868 if (!constants || start_register >= MAX_CONST_B)
2869 return WINED3DERR_INVALIDCALL;
2871 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
2872 for (i = 0; i < count; ++i)
2873 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2875 for (i = start_register; i < count + start_register; ++i)
2876 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
2878 if (!device->isRecordingState)
2879 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
2884 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
2885 UINT start_register, BOOL *constants, UINT bool_count)
2887 UINT count = min(bool_count, MAX_CONST_B - start_register);
2889 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2890 device, start_register, constants, bool_count);
2892 if (!constants || start_register >= MAX_CONST_B)
2893 return WINED3DERR_INVALIDCALL;
2895 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
2900 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
2901 UINT start_register, const int *constants, UINT vector4i_count)
2903 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2906 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2907 device, start_register, constants, vector4i_count);
2909 if (!constants || start_register >= MAX_CONST_I)
2910 return WINED3DERR_INVALIDCALL;
2912 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2913 for (i = 0; i < count; ++i)
2914 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2915 constants[i * 4], constants[i * 4 + 1],
2916 constants[i * 4 + 2], constants[i * 4 + 3]);
2918 for (i = start_register; i < count + start_register; ++i)
2919 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
2921 if (!device->isRecordingState)
2922 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
2927 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
2928 UINT start_register, int *constants, UINT vector4i_count)
2930 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2932 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2933 device, start_register, constants, vector4i_count);
2935 if (!constants || start_register >= MAX_CONST_I)
2936 return WINED3DERR_INVALIDCALL;
2938 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
2943 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
2944 UINT start_register, const float *constants, UINT vector4f_count)
2948 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2949 device, start_register, constants, vector4f_count);
2951 /* Specifically test start_register > limit to catch MAX_UINT overflows
2952 * when adding start_register + vector4f_count. */
2954 || start_register + vector4f_count > device->d3d_pshader_constantF
2955 || start_register > device->d3d_pshader_constantF)
2956 return WINED3DERR_INVALIDCALL;
2958 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
2959 constants, vector4f_count * sizeof(float) * 4);
2962 for (i = 0; i < vector4f_count; ++i)
2963 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2964 constants[i * 4], constants[i * 4 + 1],
2965 constants[i * 4 + 2], constants[i * 4 + 3]);
2968 if (!device->isRecordingState)
2970 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
2971 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
2974 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
2975 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
2980 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
2981 UINT start_register, float *constants, UINT vector4f_count)
2983 int count = min(vector4f_count, device->d3d_pshader_constantF - start_register);
2985 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2986 device, start_register, constants, vector4f_count);
2988 if (!constants || count < 0)
2989 return WINED3DERR_INVALIDCALL;
2991 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
2996 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2998 struct wined3d_shader *prev = device->updateStateBlock->state.geometry_shader;
3000 TRACE("device %p, shader %p.\n", device, shader);
3003 wined3d_shader_incref(shader);
3005 wined3d_shader_decref(prev);
3007 device->updateStateBlock->state.geometry_shader = shader;
3009 if (device->isRecordingState || shader == prev)
3012 device_invalidate_state(device, STATE_GEOMETRY_SHADER);
3015 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
3017 TRACE("device %p.\n", device);
3019 return device->stateBlock->state.geometry_shader;
3022 /* Context activation is done by the caller. */
3023 /* Do not call while under the GL lock. */
3024 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3025 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3026 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3029 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3030 struct wined3d_viewport vp;
3038 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3040 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3043 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3045 ERR("Source has no position mask\n");
3046 return WINED3DERR_INVALIDCALL;
3049 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3051 static BOOL warned = FALSE;
3053 * The clipping code is not quite correct. Some things need
3054 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3055 * so disable clipping for now.
3056 * (The graphics in Half-Life are broken, and my processvertices
3057 * test crashes with IDirect3DDevice3)
3063 FIXME("Clipping is broken and disabled for now\n");
3069 vertex_size = get_flexible_vertex_size(DestFVF);
3070 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3072 WARN("Failed to map buffer, hr %#x.\n", hr);
3076 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3077 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3078 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3080 TRACE("View mat:\n");
3081 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);
3082 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);
3083 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);
3084 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);
3086 TRACE("Proj mat:\n");
3087 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);
3088 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);
3089 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);
3090 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);
3092 TRACE("World mat:\n");
3093 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);
3094 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);
3095 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);
3096 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);
3098 /* Get the viewport */
3099 wined3d_device_get_viewport(device, &vp);
3100 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3101 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3103 multiply_matrix(&mat,&view_mat,&world_mat);
3104 multiply_matrix(&mat,&proj_mat,&mat);
3106 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3108 for (i = 0; i < dwCount; i+= 1) {
3109 unsigned int tex_index;
3111 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3112 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3113 /* The position first */
3114 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3115 const float *p = (const float *)(element->data.addr + i * element->stride);
3117 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3119 /* Multiplication with world, view and projection matrix */
3120 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);
3121 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);
3122 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);
3123 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);
3125 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3127 /* WARNING: The following things are taken from d3d7 and were not yet checked
3128 * against d3d8 or d3d9!
3131 /* Clipping conditions: From msdn
3133 * A vertex is clipped if it does not match the following requirements
3137 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3139 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3140 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3145 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3146 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3149 /* "Normal" viewport transformation (not clipped)
3150 * 1) The values are divided by rhw
3151 * 2) The y axis is negative, so multiply it with -1
3152 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3153 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3154 * 4) Multiply x with Width/2 and add Width/2
3155 * 5) The same for the height
3156 * 6) Add the viewpoint X and Y to the 2D coordinates and
3157 * The minimum Z value to z
3158 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3160 * Well, basically it's simply a linear transformation into viewport
3172 z *= vp.max_z - vp.min_z;
3174 x += vp.width / 2 + vp.x;
3175 y += vp.height / 2 + vp.y;
3180 /* That vertex got clipped
3181 * Contrary to OpenGL it is not dropped completely, it just
3182 * undergoes a different calculation.
3184 TRACE("Vertex got clipped\n");
3191 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3192 * outside of the main vertex buffer memory. That needs some more
3197 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3200 ( (float *) dest_ptr)[0] = x;
3201 ( (float *) dest_ptr)[1] = y;
3202 ( (float *) dest_ptr)[2] = z;
3203 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3205 dest_ptr += 3 * sizeof(float);
3207 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3208 dest_ptr += sizeof(float);
3211 if (DestFVF & WINED3DFVF_PSIZE)
3212 dest_ptr += sizeof(DWORD);
3214 if (DestFVF & WINED3DFVF_NORMAL)
3216 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3217 const float *normal = (const float *)(element->data.addr + i * element->stride);
3218 /* AFAIK this should go into the lighting information */
3219 FIXME("Didn't expect the destination to have a normal\n");
3220 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3223 if (DestFVF & WINED3DFVF_DIFFUSE)
3225 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3226 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3227 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3229 static BOOL warned = FALSE;
3232 ERR("No diffuse color in source, but destination has one\n");
3236 *( (DWORD *) dest_ptr) = 0xffffffff;
3237 dest_ptr += sizeof(DWORD);
3241 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3245 if (DestFVF & WINED3DFVF_SPECULAR)
3247 /* What's the color value in the feedback buffer? */
3248 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3249 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3250 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3252 static BOOL warned = FALSE;
3255 ERR("No specular color in source, but destination has one\n");
3259 *(DWORD *)dest_ptr = 0xff000000;
3260 dest_ptr += sizeof(DWORD);
3264 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3268 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3270 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3271 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3272 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3274 ERR("No source texture, but destination requests one\n");
3275 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3279 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3284 wined3d_buffer_unmap(dest);
3288 #undef copy_and_next
3290 /* Do not call while under the GL lock. */
3291 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3292 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3293 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3295 struct wined3d_state *state = &device->stateBlock->state;
3296 struct wined3d_stream_info stream_info;
3297 const struct wined3d_gl_info *gl_info;
3298 BOOL streamWasUP = state->user_stream;
3299 struct wined3d_context *context;
3300 struct wined3d_shader *vs;
3304 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3305 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3306 device, src_start_idx, dst_idx, vertex_count,
3307 dst_buffer, declaration, flags, dst_fvf);
3310 FIXME("Output vertex declaration not implemented yet.\n");
3312 /* Need any context to write to the vbo. */
3313 context = context_acquire(device, NULL);
3314 gl_info = context->gl_info;
3316 /* ProcessVertices reads from vertex buffers, which have to be assigned.
3317 * DrawPrimitive and DrawPrimitiveUP control the streamIsUP flag, thus
3318 * restore it afterwards. */
3319 vs = state->vertex_shader;
3320 state->vertex_shader = NULL;
3321 state->user_stream = FALSE;
3322 device_stream_info_from_declaration(device, &stream_info);
3323 state->user_stream = streamWasUP;
3324 state->vertex_shader = vs;
3326 /* We can't convert FROM a VBO, and vertex buffers used to source into
3327 * process_vertices() are unlikely to ever be used for drawing. Release
3328 * VBOs in those buffers and fix up the stream_info structure.
3330 * Also apply the start index. */
3331 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3333 struct wined3d_stream_info_element *e;
3335 if (!(stream_info.use_map & (1 << i)))
3338 e = &stream_info.elements[i];
3339 if (e->data.buffer_object)
3341 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3342 e->data.buffer_object = 0;
3343 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3345 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3346 vb->buffer_object = 0;
3350 e->data.addr += e->stride * src_start_idx;
3353 hr = process_vertices_strided(device, dst_idx, vertex_count,
3354 &stream_info, dst_buffer, flags, dst_fvf);
3356 context_release(context);
3361 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3362 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3364 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3367 TRACE("device %p, stage %u, state %s, value %#x.\n",
3368 device, stage, debug_d3dtexturestate(state), value);
3370 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3372 WARN("Invalid state %#x passed.\n", state);
3376 if (stage >= gl_info->limits.texture_stages)
3378 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3379 stage, gl_info->limits.texture_stages - 1);
3383 old_value = device->updateStateBlock->state.texture_states[stage][state];
3384 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3385 device->updateStateBlock->state.texture_states[stage][state] = value;
3387 if (device->isRecordingState)
3389 TRACE("Recording... not performing anything.\n");
3393 /* Checked after the assignments to allow proper stateblock recording. */
3394 if (old_value == value)
3396 TRACE("Application is setting the old value over, nothing to do.\n");
3400 if (stage > device->stateBlock->state.lowest_disabled_stage
3401 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3402 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3404 /* Colorop change above lowest disabled stage? That won't change
3405 * anything in the GL setup. Changes in other states are important on
3406 * disabled stages too. */
3410 if (state == WINED3D_TSS_COLOR_OP)
3414 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3416 /* Previously enabled stage disabled now. Make sure to dirtify
3417 * all enabled stages above stage, they have to be disabled.
3419 * The current stage is dirtified below. */
3420 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3422 TRACE("Additionally dirtifying stage %u.\n", i);
3423 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3425 device->stateBlock->state.lowest_disabled_stage = stage;
3426 TRACE("New lowest disabled: %u.\n", stage);
3428 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3430 /* Previously disabled stage enabled. Stages above it may need
3431 * enabling. Stage must be lowest_disabled_stage here, if it's
3432 * bigger success is returned above, and stages below the lowest
3433 * disabled stage can't be enabled (because they are enabled
3436 * Again stage stage doesn't need to be dirtified here, it is
3438 for (i = stage + 1; i < gl_info->limits.texture_stages; ++i)
3440 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3442 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3443 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3445 device->stateBlock->state.lowest_disabled_stage = i;
3446 TRACE("New lowest disabled: %u.\n", i);
3450 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3453 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3454 UINT stage, enum wined3d_texture_stage_state state)
3456 TRACE("device %p, stage %u, state %s.\n",
3457 device, stage, debug_d3dtexturestate(state));
3459 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3461 WARN("Invalid state %#x passed.\n", state);
3465 return device->updateStateBlock->state.texture_states[stage][state];
3468 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3469 UINT stage, struct wined3d_texture *texture)
3471 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3472 struct wined3d_texture *prev;
3474 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3476 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3477 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3479 /* Windows accepts overflowing this array... we do not. */
3480 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3482 WARN("Ignoring invalid stage %u.\n", stage);
3486 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3488 WARN("Rejecting attempt to set scratch texture.\n");
3489 return WINED3DERR_INVALIDCALL;
3492 device->updateStateBlock->changed.textures |= 1 << stage;
3494 prev = device->updateStateBlock->state.textures[stage];
3495 TRACE("Previous texture %p.\n", prev);
3497 if (texture == prev)
3499 TRACE("App is setting the same texture again, nothing to do.\n");
3503 TRACE("Setting new texture to %p.\n", texture);
3504 device->updateStateBlock->state.textures[stage] = texture;
3506 if (device->isRecordingState)
3508 TRACE("Recording... not performing anything\n");
3510 if (texture) wined3d_texture_incref(texture);
3511 if (prev) wined3d_texture_decref(prev);
3518 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3520 wined3d_texture_incref(texture);
3522 if (!prev || texture->target != prev->target)
3523 device_invalidate_state(device, STATE_PIXELSHADER);
3525 if (!prev && stage < gl_info->limits.texture_stages)
3527 /* The source arguments for color and alpha ops have different
3528 * meanings when a NULL texture is bound, so the COLOR_OP and
3529 * ALPHA_OP have to be dirtified. */
3530 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3531 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3534 if (bind_count == 1)
3535 texture->sampler = stage;
3540 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3542 if (!texture && stage < gl_info->limits.texture_stages)
3544 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3545 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3548 if (bind_count && prev->sampler == stage)
3552 /* Search for other stages the texture is bound to. Shouldn't
3553 * happen if applications bind textures to a single stage only. */
3554 TRACE("Searching for other stages the texture is bound to.\n");
3555 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3557 if (device->updateStateBlock->state.textures[i] == prev)
3559 TRACE("Texture is also bound to stage %u.\n", i);
3566 wined3d_texture_decref(prev);
3569 device_invalidate_state(device, STATE_SAMPLER(stage));
3574 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3576 TRACE("device %p, stage %u.\n", device, stage);
3578 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3579 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3581 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3583 WARN("Ignoring invalid stage %u.\n", stage);
3584 return NULL; /* Windows accepts overflowing this array ... we do not. */
3587 return device->stateBlock->state.textures[stage];
3590 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3591 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3593 struct wined3d_swapchain *swapchain;
3595 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3596 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3598 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3599 return WINED3DERR_INVALIDCALL;
3601 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3602 return WINED3DERR_INVALIDCALL;
3606 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3608 TRACE("device %p, caps %p.\n", device, caps);
3610 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3611 device->create_parms.device_type, caps);
3614 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3615 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3617 struct wined3d_swapchain *swapchain;
3619 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3620 device, swapchain_idx, mode, rotation);
3622 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3623 return WINED3DERR_INVALIDCALL;
3625 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3628 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3630 struct wined3d_stateblock *stateblock;
3633 TRACE("device %p.\n", device);
3635 if (device->isRecordingState)
3636 return WINED3DERR_INVALIDCALL;
3638 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3642 wined3d_stateblock_decref(device->updateStateBlock);
3643 device->updateStateBlock = stateblock;
3644 device->isRecordingState = TRUE;
3646 TRACE("Recording stateblock %p.\n", stateblock);
3651 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3652 struct wined3d_stateblock **stateblock)
3654 struct wined3d_stateblock *object = device->updateStateBlock;
3656 TRACE("device %p, stateblock %p.\n", device, stateblock);
3658 if (!device->isRecordingState)
3660 WARN("Not recording.\n");
3662 return WINED3DERR_INVALIDCALL;
3665 stateblock_init_contained_states(object);
3667 *stateblock = object;
3668 device->isRecordingState = FALSE;
3669 device->updateStateBlock = device->stateBlock;
3670 wined3d_stateblock_incref(device->updateStateBlock);
3672 TRACE("Returning stateblock %p.\n", *stateblock);
3677 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3679 /* At the moment we have no need for any functionality at the beginning
3681 TRACE("device %p.\n", device);
3683 if (device->inScene)
3685 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3686 return WINED3DERR_INVALIDCALL;
3688 device->inScene = TRUE;
3692 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3694 struct wined3d_context *context;
3696 TRACE("device %p.\n", device);
3698 if (!device->inScene)
3700 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3701 return WINED3DERR_INVALIDCALL;
3704 context = context_acquire(device, NULL);
3705 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3706 context->gl_info->gl_ops.gl.p_glFlush();
3707 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3709 context_release(context);
3711 device->inScene = FALSE;
3715 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3716 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3720 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3721 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3722 dst_window_override, dirty_region, flags);
3724 for (i = 0; i < device->swapchain_count; ++i)
3726 wined3d_swapchain_present(device->swapchains[i], src_rect,
3727 dst_rect, dst_window_override, dirty_region, flags);
3733 /* Do not call while under the GL lock. */
3734 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3735 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3739 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3740 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3742 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3744 struct wined3d_surface *ds = device->fb.depth_stencil;
3747 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3748 /* TODO: What about depth stencil buffers without stencil bits? */
3749 return WINED3DERR_INVALIDCALL;
3751 else if (flags & WINED3DCLEAR_TARGET)
3753 if (ds->resource.width < device->fb.render_targets[0]->resource.width
3754 || ds->resource.height < device->fb.render_targets[0]->resource.height)
3756 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3762 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
3763 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
3764 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
3769 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3770 enum wined3d_primitive_type primitive_type)
3772 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3774 device->updateStateBlock->changed.primitive_type = TRUE;
3775 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3778 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3779 enum wined3d_primitive_type *primitive_type)
3781 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3783 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
3785 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
3788 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
3790 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
3792 if (!device->stateBlock->state.vertex_declaration)
3794 WARN("Called without a valid vertex declaration set.\n");
3795 return WINED3DERR_INVALIDCALL;
3798 /* The index buffer is not needed here, but restore it, otherwise it is hell to keep track of */
3799 if (device->stateBlock->state.user_stream)
3801 device_invalidate_state(device, STATE_INDEXBUFFER);
3802 device->stateBlock->state.user_stream = FALSE;
3805 if (device->stateBlock->state.load_base_vertex_index)
3807 device->stateBlock->state.load_base_vertex_index = 0;
3808 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3811 /* Account for the loading offset due to index buffers. Instead of
3812 * reloading all sources correct it with the startvertex parameter. */
3813 drawPrimitive(device, vertex_count, start_vertex, FALSE, NULL);
3817 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
3819 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3821 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
3823 if (!device->stateBlock->state.index_buffer)
3825 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
3826 * without an index buffer set. (The first time at least...)
3827 * D3D8 simply dies, but I doubt it can do much harm to return
3828 * D3DERR_INVALIDCALL there as well. */
3829 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
3830 return WINED3DERR_INVALIDCALL;
3833 if (!device->stateBlock->state.vertex_declaration)
3835 WARN("Called without a valid vertex declaration set.\n");
3836 return WINED3DERR_INVALIDCALL;
3839 if (device->stateBlock->state.user_stream)
3841 device_invalidate_state(device, STATE_INDEXBUFFER);
3842 device->stateBlock->state.user_stream = FALSE;
3845 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
3846 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
3848 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
3849 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3852 drawPrimitive(device, index_count, start_idx, TRUE, NULL);
3857 HRESULT CDECL wined3d_device_draw_primitive_up(struct wined3d_device *device, UINT vertex_count,
3858 const void *stream_data, UINT stream_stride)
3860 struct wined3d_stream_state *stream;
3861 struct wined3d_buffer *vb;
3863 TRACE("device %p, vertex count %u, stream_data %p, stream_stride %u.\n",
3864 device, vertex_count, stream_data, stream_stride);
3866 if (!device->stateBlock->state.vertex_declaration)
3868 WARN("Called without a valid vertex declaration set.\n");
3869 return WINED3DERR_INVALIDCALL;
3872 /* Note in the following, it's not this type, but that's the purpose of streamIsUP */
3873 stream = &device->stateBlock->state.streams[0];
3874 vb = stream->buffer;
3875 stream->buffer = (struct wined3d_buffer *)stream_data;
3877 wined3d_buffer_decref(vb);
3879 stream->stride = stream_stride;
3880 device->stateBlock->state.user_stream = TRUE;
3881 if (device->stateBlock->state.load_base_vertex_index)
3883 device->stateBlock->state.load_base_vertex_index = 0;
3884 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3887 /* TODO: Only mark dirty if drawing from a different UP address */
3888 device_invalidate_state(device, STATE_STREAMSRC);
3890 drawPrimitive(device, vertex_count, 0, FALSE, NULL);
3892 /* MSDN specifies stream zero settings must be set to NULL */
3893 stream->buffer = NULL;
3896 /* stream zero settings set to null at end, as per the msdn. No need to
3897 * mark dirty here, the app has to set the new stream sources or use UP
3902 HRESULT CDECL wined3d_device_draw_indexed_primitive_up(struct wined3d_device *device,
3903 UINT index_count, const void *index_data, enum wined3d_format_id index_data_format_id,
3904 const void *stream_data, UINT stream_stride)
3906 struct wined3d_stream_state *stream;
3907 struct wined3d_buffer *vb, *ib;
3909 TRACE("device %p, index_count %u, index_data %p, index_data_format %s, stream_data %p, stream_stride %u.\n",
3910 device, index_count, index_data, debug_d3dformat(index_data_format_id), stream_data, stream_stride);
3912 if (!device->stateBlock->state.vertex_declaration)
3914 WARN("(%p) : Called without a valid vertex declaration set\n", device);
3915 return WINED3DERR_INVALIDCALL;
3918 stream = &device->stateBlock->state.streams[0];
3919 vb = stream->buffer;
3920 stream->buffer = (struct wined3d_buffer *)stream_data;
3922 wined3d_buffer_decref(vb);
3924 stream->stride = stream_stride;
3925 device->stateBlock->state.user_stream = TRUE;
3926 device->stateBlock->state.index_format = index_data_format_id;
3928 /* Set to 0 as per msdn. Do it now due to the stream source loading during drawPrimitive */
3929 device->stateBlock->state.base_vertex_index = 0;
3930 if (device->stateBlock->state.load_base_vertex_index)
3932 device->stateBlock->state.load_base_vertex_index = 0;
3933 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
3935 /* Invalidate the state until we have nicer tracking of the stream source pointers */
3936 device_invalidate_state(device, STATE_STREAMSRC);
3937 device_invalidate_state(device, STATE_INDEXBUFFER);
3939 drawPrimitive(device, index_count, 0, TRUE, index_data);
3941 /* MSDN specifies stream zero settings and index buffer must be set to NULL */
3942 stream->buffer = NULL;
3944 ib = device->stateBlock->state.index_buffer;
3947 wined3d_buffer_decref(ib);
3948 device->stateBlock->state.index_buffer = NULL;
3950 /* No need to mark the stream source state dirty here. Either the app calls UP drawing again, or it has to call
3951 * SetStreamSource to specify a vertex buffer
3957 HRESULT CDECL wined3d_device_draw_primitive_strided(struct wined3d_device *device,
3958 UINT vertex_count, const struct wined3d_strided_data *strided_data)
3960 /* Mark the state dirty until we have nicer tracking. It's fine to change
3961 * baseVertexIndex because that call is only called by ddraw which does
3962 * not need that value. */
3963 device_invalidate_state(device, STATE_VDECL);
3964 device_invalidate_state(device, STATE_STREAMSRC);
3965 device_invalidate_state(device, STATE_INDEXBUFFER);
3967 device->stateBlock->state.base_vertex_index = 0;
3968 device->up_strided = strided_data;
3969 drawPrimitive(device, vertex_count, 0, FALSE, NULL);
3970 device->up_strided = NULL;
3972 /* Invalidate the states again to make sure the values from the stateblock
3973 * are properly applied in the next regular draw. Note that the application-
3974 * provided strided data has ovwritten pretty much the entire vertex and
3975 * and index stream related states */
3976 device_invalidate_state(device, STATE_VDECL);
3977 device_invalidate_state(device, STATE_STREAMSRC);
3978 device_invalidate_state(device, STATE_INDEXBUFFER);
3982 HRESULT CDECL wined3d_device_draw_indexed_primitive_strided(struct wined3d_device *device,
3983 UINT index_count, const struct wined3d_strided_data *strided_data,
3984 UINT vertex_count, const void *index_data, enum wined3d_format_id index_data_format_id)
3986 enum wined3d_format_id prev_idx_format;
3988 /* Mark the state dirty until we have nicer tracking
3989 * its fine to change baseVertexIndex because that call is only called by ddraw which does not need
3992 device_invalidate_state(device, STATE_VDECL);
3993 device_invalidate_state(device, STATE_STREAMSRC);
3994 device_invalidate_state(device, STATE_INDEXBUFFER);
3996 prev_idx_format = device->stateBlock->state.index_format;
3997 device->stateBlock->state.index_format = index_data_format_id;
3998 device->stateBlock->state.user_stream = TRUE;
3999 device->stateBlock->state.base_vertex_index = 0;
4000 device->up_strided = strided_data;
4001 drawPrimitive(device, index_count, 0, TRUE, index_data);
4002 device->up_strided = NULL;
4003 device->stateBlock->state.index_format = prev_idx_format;
4005 device_invalidate_state(device, STATE_VDECL);
4006 device_invalidate_state(device, STATE_STREAMSRC);
4007 device_invalidate_state(device, STATE_INDEXBUFFER);
4011 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4012 static HRESULT device_update_volume(struct wined3d_device *device,
4013 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4015 struct wined3d_map_desc src;
4016 struct wined3d_map_desc dst;
4019 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4020 device, src_volume, dst_volume);
4022 /* TODO: Implement direct loading into the gl volume instead of using
4023 * memcpy and dirtification to improve loading performance. */
4024 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
4026 if (FAILED(hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3D_MAP_DISCARD)))
4028 wined3d_volume_unmap(src_volume);
4032 memcpy(dst.data, src.data, dst_volume->resource.size);
4034 hr = wined3d_volume_unmap(dst_volume);
4036 wined3d_volume_unmap(src_volume);
4038 hr = wined3d_volume_unmap(src_volume);
4043 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4044 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4046 enum wined3d_resource_type type;
4047 unsigned int level_count, i;
4050 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4052 /* Verify that the source and destination textures are non-NULL. */
4053 if (!src_texture || !dst_texture)
4055 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4056 return WINED3DERR_INVALIDCALL;
4059 if (src_texture == dst_texture)
4061 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4062 return WINED3DERR_INVALIDCALL;
4065 /* Verify that the source and destination textures are the same type. */
4066 type = src_texture->resource.type;
4067 if (dst_texture->resource.type != type)
4069 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4070 return WINED3DERR_INVALIDCALL;
4073 /* Check that both textures have the identical numbers of levels. */
4074 level_count = wined3d_texture_get_level_count(src_texture);
4075 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4077 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4078 return WINED3DERR_INVALIDCALL;
4081 /* Make sure that the destination texture is loaded. */
4082 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4084 /* Update every surface level of the texture. */
4087 case WINED3D_RTYPE_TEXTURE:
4089 struct wined3d_surface *src_surface;
4090 struct wined3d_surface *dst_surface;
4092 for (i = 0; i < level_count; ++i)
4094 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4095 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4096 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4099 WARN("Failed to update surface, hr %#x.\n", hr);
4106 case WINED3D_RTYPE_CUBE_TEXTURE:
4108 struct wined3d_surface *src_surface;
4109 struct wined3d_surface *dst_surface;
4111 for (i = 0; i < level_count * 6; ++i)
4113 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4114 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4115 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4118 WARN("Failed to update surface, hr %#x.\n", hr);
4125 case WINED3D_RTYPE_VOLUME_TEXTURE:
4127 for (i = 0; i < level_count; ++i)
4129 hr = device_update_volume(device,
4130 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4131 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4134 WARN("Failed to update volume, hr %#x.\n", hr);
4142 FIXME("Unsupported texture type %#x.\n", type);
4143 return WINED3DERR_INVALIDCALL;
4149 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4150 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4152 struct wined3d_swapchain *swapchain;
4154 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4156 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4157 return WINED3DERR_INVALIDCALL;
4159 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4162 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4164 const struct wined3d_state *state = &device->stateBlock->state;
4165 struct wined3d_texture *texture;
4168 TRACE("device %p, num_passes %p.\n", device, num_passes);
4170 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4172 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4174 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4175 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4177 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4179 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4180 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4183 texture = state->textures[i];
4184 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4186 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4188 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4191 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4193 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4196 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4197 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4199 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4204 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4205 || state->render_states[WINED3D_RS_STENCILENABLE])
4207 struct wined3d_surface *ds = device->fb.depth_stencil;
4208 struct wined3d_surface *target = device->fb.render_targets[0];
4211 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4213 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4214 return WINED3DERR_CONFLICTINGRENDERSTATE;
4218 /* return a sensible default */
4221 TRACE("returning D3D_OK\n");
4225 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4229 TRACE("device %p, software %#x.\n", device, software);
4233 FIXME("device %p, software %#x stub!\n", device, software);
4237 device->softwareVertexProcessing = software;
4240 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4244 TRACE("device %p.\n", device);
4248 TRACE("device %p stub!\n", device);
4252 return device->softwareVertexProcessing;
4255 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4256 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4258 struct wined3d_swapchain *swapchain;
4260 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4261 device, swapchain_idx, raster_status);
4263 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4264 return WINED3DERR_INVALIDCALL;
4266 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4269 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4273 TRACE("device %p, segments %.8e.\n", device, segments);
4275 if (segments != 0.0f)
4279 FIXME("device %p, segments %.8e stub!\n", device, segments);
4287 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4291 TRACE("device %p.\n", device);
4295 FIXME("device %p stub!\n", device);
4302 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4303 struct wined3d_surface *src_surface, const RECT *src_rect,
4304 struct wined3d_surface *dst_surface, const POINT *dst_point)
4306 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4307 device, src_surface, wine_dbgstr_rect(src_rect),
4308 dst_surface, wine_dbgstr_point(dst_point));
4310 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4312 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4313 src_surface, dst_surface);
4314 return WINED3DERR_INVALIDCALL;
4317 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4320 HRESULT CDECL wined3d_device_draw_rect_patch(struct wined3d_device *device, UINT handle,
4321 const float *num_segs, const struct wined3d_rect_patch_info *rect_patch_info)
4323 struct wined3d_rect_patch *patch;
4324 GLenum old_primitive_type;
4329 TRACE("device %p, handle %#x, num_segs %p, rect_patch_info %p.\n",
4330 device, handle, num_segs, rect_patch_info);
4332 if (!(handle || rect_patch_info))
4334 /* TODO: Write a test for the return value, thus the FIXME */
4335 FIXME("Both handle and rect_patch_info are NULL.\n");
4336 return WINED3DERR_INVALIDCALL;
4341 i = PATCHMAP_HASHFUNC(handle);
4343 LIST_FOR_EACH(e, &device->patches[i])
4345 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4346 if (patch->Handle == handle)
4355 TRACE("Patch does not exist. Creating a new one\n");
4356 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4357 patch->Handle = handle;
4358 list_add_head(&device->patches[i], &patch->entry);
4360 TRACE("Found existing patch %p\n", patch);
4365 /* Since opengl does not load tesselated vertex attributes into numbered vertex
4366 * attributes we have to tesselate, read back, and draw. This needs a patch
4367 * management structure instance. Create one.
4369 * A possible improvement is to check if a vertex shader is used, and if not directly
4372 FIXME("Drawing an uncached patch. This is slow\n");
4373 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4376 if (num_segs[0] != patch->numSegs[0] || num_segs[1] != patch->numSegs[1]
4377 || num_segs[2] != patch->numSegs[2] || num_segs[3] != patch->numSegs[3]
4378 || (rect_patch_info && memcmp(rect_patch_info, &patch->rect_patch_info, sizeof(*rect_patch_info))))
4381 TRACE("Tesselation density or patch info changed, retesselating\n");
4383 if (rect_patch_info)
4384 patch->rect_patch_info = *rect_patch_info;
4386 patch->numSegs[0] = num_segs[0];
4387 patch->numSegs[1] = num_segs[1];
4388 patch->numSegs[2] = num_segs[2];
4389 patch->numSegs[3] = num_segs[3];
4391 hr = tesselate_rectpatch(device, patch);
4394 WARN("Patch tesselation failed.\n");
4396 /* Do not release the handle to store the params of the patch */
4398 HeapFree(GetProcessHeap(), 0, patch);
4404 old_primitive_type = device->stateBlock->state.gl_primitive_type;
4405 device->stateBlock->state.gl_primitive_type = GL_TRIANGLES;
4406 wined3d_device_draw_primitive_strided(device, patch->numSegs[0] * patch->numSegs[1] * 2 * 3, &patch->strided);
4407 device->stateBlock->state.gl_primitive_type = old_primitive_type;
4409 /* Destroy uncached patches */
4412 HeapFree(GetProcessHeap(), 0, patch->mem);
4413 HeapFree(GetProcessHeap(), 0, patch);
4418 HRESULT CDECL wined3d_device_draw_tri_patch(struct wined3d_device *device, UINT handle,
4419 const float *segment_count, const struct wined3d_tri_patch_info *patch_info)
4421 FIXME("device %p, handle %#x, segment_count %p, patch_info %p stub!\n",
4422 device, handle, segment_count, patch_info);
4427 HRESULT CDECL wined3d_device_delete_patch(struct wined3d_device *device, UINT handle)
4429 struct wined3d_rect_patch *patch;
4433 TRACE("device %p, handle %#x.\n", device, handle);
4435 i = PATCHMAP_HASHFUNC(handle);
4436 LIST_FOR_EACH(e, &device->patches[i])
4438 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4439 if (patch->Handle == handle)
4441 TRACE("Deleting patch %p\n", patch);
4442 list_remove(&patch->entry);
4443 HeapFree(GetProcessHeap(), 0, patch->mem);
4444 HeapFree(GetProcessHeap(), 0, patch);
4449 /* TODO: Write a test for the return value */
4450 FIXME("Attempt to destroy nonexistent patch\n");
4451 return WINED3DERR_INVALIDCALL;
4454 /* Do not call while under the GL lock. */
4455 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4456 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4460 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4461 device, surface, wine_dbgstr_rect(rect),
4462 color->r, color->g, color->b, color->a);
4464 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4466 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4467 return WINED3DERR_INVALIDCALL;
4472 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4476 return surface_color_fill(surface, rect, color);
4479 /* Do not call while under the GL lock. */
4480 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4481 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4483 struct wined3d_resource *resource;
4487 resource = rendertarget_view->resource;
4488 if (resource->type != WINED3D_RTYPE_SURFACE)
4490 FIXME("Only supported on surface resources\n");
4494 SetRect(&rect, 0, 0, resource->width, resource->height);
4495 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4496 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4499 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4500 UINT render_target_idx)
4502 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
4504 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4506 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4510 return device->fb.render_targets[render_target_idx];
4513 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
4515 TRACE("device %p.\n", device);
4517 return device->fb.depth_stencil;
4520 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4521 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4523 struct wined3d_surface *prev;
4525 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4526 device, render_target_idx, render_target, set_viewport);
4528 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4530 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4531 return WINED3DERR_INVALIDCALL;
4534 prev = device->fb.render_targets[render_target_idx];
4535 if (render_target == prev)
4537 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4541 /* Render target 0 can't be set to NULL. */
4542 if (!render_target && !render_target_idx)
4544 WARN("Trying to set render target 0 to NULL.\n");
4545 return WINED3DERR_INVALIDCALL;
4548 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4550 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4551 return WINED3DERR_INVALIDCALL;
4555 wined3d_surface_incref(render_target);
4556 device->fb.render_targets[render_target_idx] = render_target;
4557 /* Release after the assignment, to prevent device_resource_released()
4558 * from seeing the surface as still in use. */
4560 wined3d_surface_decref(prev);
4562 /* Render target 0 is special. */
4563 if (!render_target_idx && set_viewport)
4565 /* Set the viewport and scissor rectangles, if requested. Tests show
4566 * that stateblock recording is ignored, the change goes directly
4567 * into the primary stateblock. */
4568 device->stateBlock->state.viewport.height = device->fb.render_targets[0]->resource.height;
4569 device->stateBlock->state.viewport.width = device->fb.render_targets[0]->resource.width;
4570 device->stateBlock->state.viewport.x = 0;
4571 device->stateBlock->state.viewport.y = 0;
4572 device->stateBlock->state.viewport.max_z = 1.0f;
4573 device->stateBlock->state.viewport.min_z = 0.0f;
4574 device_invalidate_state(device, STATE_VIEWPORT);
4576 device->stateBlock->state.scissor_rect.top = 0;
4577 device->stateBlock->state.scissor_rect.left = 0;
4578 device->stateBlock->state.scissor_rect.right = device->stateBlock->state.viewport.width;
4579 device->stateBlock->state.scissor_rect.bottom = device->stateBlock->state.viewport.height;
4580 device_invalidate_state(device, STATE_SCISSORRECT);
4583 device_invalidate_state(device, STATE_FRAMEBUFFER);
4588 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4590 struct wined3d_surface *prev = device->fb.depth_stencil;
4592 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4593 device, depth_stencil, prev);
4595 if (prev == depth_stencil)
4597 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4603 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4604 || prev->flags & SFLAG_DISCARD)
4606 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4607 prev->resource.width, prev->resource.height);
4608 if (prev == device->onscreen_depth_stencil)
4610 wined3d_surface_decref(device->onscreen_depth_stencil);
4611 device->onscreen_depth_stencil = NULL;
4616 device->fb.depth_stencil = depth_stencil;
4618 wined3d_surface_incref(depth_stencil);
4620 if (!prev != !depth_stencil)
4622 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4623 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4624 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4625 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4626 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4628 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4630 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4633 wined3d_surface_decref(prev);
4635 device_invalidate_state(device, STATE_FRAMEBUFFER);
4640 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4641 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4643 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4644 device, x_hotspot, y_hotspot, cursor_image);
4646 /* some basic validation checks */
4647 if (device->cursorTexture)
4649 struct wined3d_context *context = context_acquire(device, NULL);
4651 context->gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4653 context_release(context);
4654 device->cursorTexture = 0;
4659 struct wined3d_display_mode mode;
4660 struct wined3d_map_desc map_desc;
4663 /* MSDN: Cursor must be A8R8G8B8 */
4664 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4666 WARN("surface %p has an invalid format.\n", cursor_image);
4667 return WINED3DERR_INVALIDCALL;
4670 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4672 ERR("Failed to get display mode, hr %#x.\n", hr);
4673 return WINED3DERR_INVALIDCALL;
4676 /* MSDN: Cursor must be smaller than the display mode */
4677 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4679 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4680 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4681 mode.width, mode.height);
4682 return WINED3DERR_INVALIDCALL;
4685 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4687 /* Do not store the surface's pointer because the application may
4688 * release it after setting the cursor image. Windows doesn't
4689 * addref the set surface, so we can't do this either without
4690 * creating circular refcount dependencies. Copy out the gl texture
4692 device->cursorWidth = cursor_image->resource.width;
4693 device->cursorHeight = cursor_image->resource.height;
4694 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4696 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4697 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
4698 struct wined3d_context *context;
4699 char *mem, *bits = map_desc.data;
4700 GLint intfmt = format->glInternal;
4701 GLint gl_format = format->glFormat;
4702 GLint type = format->glType;
4703 INT height = device->cursorHeight;
4704 INT width = device->cursorWidth;
4705 INT bpp = format->byte_count;
4708 /* Reformat the texture memory (pitch and width can be
4710 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
4711 for (i = 0; i < height; ++i)
4712 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
4713 wined3d_surface_unmap(cursor_image);
4715 context = context_acquire(device, NULL);
4719 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4721 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
4722 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4725 invalidate_active_texture(device, context);
4726 /* Create a new cursor texture */
4727 gl_info->gl_ops.gl.p_glGenTextures(1, &device->cursorTexture);
4728 checkGLcall("glGenTextures");
4729 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
4730 /* Copy the bitmap memory into the cursor texture */
4731 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
4732 checkGLcall("glTexImage2D");
4733 HeapFree(GetProcessHeap(), 0, mem);
4735 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4737 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
4738 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4743 context_release(context);
4747 FIXME("A cursor texture was not returned.\n");
4748 device->cursorTexture = 0;
4751 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4753 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4754 ICONINFO cursorInfo;
4758 /* 32-bit user32 cursors ignore the alpha channel if it's all
4759 * zeroes, and use the mask instead. Fill the mask with all ones
4760 * to ensure we still get a fully transparent cursor. */
4761 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4762 memset(maskBits, 0xff, mask_size);
4763 wined3d_surface_map(cursor_image, &map_desc, NULL,
4764 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4765 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4767 cursorInfo.fIcon = FALSE;
4768 cursorInfo.xHotspot = x_hotspot;
4769 cursorInfo.yHotspot = y_hotspot;
4770 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4772 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4773 1, 32, map_desc.data);
4774 wined3d_surface_unmap(cursor_image);
4775 /* Create our cursor and clean up. */
4776 cursor = CreateIconIndirect(&cursorInfo);
4777 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4778 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4779 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4780 device->hardwareCursor = cursor;
4781 if (device->bCursorVisible) SetCursor( cursor );
4782 HeapFree(GetProcessHeap(), 0, maskBits);
4786 device->xHotSpot = x_hotspot;
4787 device->yHotSpot = y_hotspot;
4791 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4792 int x_screen_space, int y_screen_space, DWORD flags)
4794 TRACE("device %p, x %d, y %d, flags %#x.\n",
4795 device, x_screen_space, y_screen_space, flags);
4797 device->xScreenSpace = x_screen_space;
4798 device->yScreenSpace = y_screen_space;
4800 if (device->hardwareCursor)
4804 GetCursorPos( &pt );
4805 if (x_screen_space == pt.x && y_screen_space == pt.y)
4807 SetCursorPos( x_screen_space, y_screen_space );
4809 /* Switch to the software cursor if position diverges from the hardware one. */
4810 GetCursorPos( &pt );
4811 if (x_screen_space != pt.x || y_screen_space != pt.y)
4813 if (device->bCursorVisible) SetCursor( NULL );
4814 DestroyCursor( device->hardwareCursor );
4815 device->hardwareCursor = 0;
4820 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4822 BOOL oldVisible = device->bCursorVisible;
4824 TRACE("device %p, show %#x.\n", device, show);
4827 * When ShowCursor is first called it should make the cursor appear at the OS's last
4828 * known cursor position.
4830 if (show && !oldVisible)
4834 device->xScreenSpace = pt.x;
4835 device->yScreenSpace = pt.y;
4838 if (device->hardwareCursor)
4840 device->bCursorVisible = show;
4842 SetCursor(device->hardwareCursor);
4848 if (device->cursorTexture)
4849 device->bCursorVisible = show;
4855 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4857 struct wined3d_resource *resource, *cursor;
4859 TRACE("device %p.\n", device);
4861 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4863 TRACE("Checking resource %p for eviction.\n", resource);
4865 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4867 TRACE("Evicting %p.\n", resource);
4868 resource->resource_ops->resource_unload(resource);
4872 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4873 device_invalidate_state(device, STATE_STREAMSRC);
4876 /* Do not call while under the GL lock. */
4877 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4879 struct wined3d_resource *resource, *cursor;
4880 const struct wined3d_gl_info *gl_info;
4881 struct wined3d_context *context;
4882 struct wined3d_shader *shader;
4884 context = context_acquire(device, NULL);
4885 gl_info = context->gl_info;
4887 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4889 TRACE("Unloading resource %p.\n", resource);
4891 resource->resource_ops->resource_unload(resource);
4894 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4896 device->shader_backend->shader_destroy(shader);
4900 if (device->depth_blt_texture)
4902 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4903 device->depth_blt_texture = 0;
4905 if (device->cursorTexture)
4907 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4908 device->cursorTexture = 0;
4912 device->blitter->free_private(device);
4913 device->shader_backend->shader_free_private(device);
4914 destroy_dummy_textures(device, gl_info);
4916 context_release(context);
4918 while (device->context_count)
4920 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4923 HeapFree(GetProcessHeap(), 0, swapchain->context);
4924 swapchain->context = NULL;
4927 /* Do not call while under the GL lock. */
4928 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4930 struct wined3d_context *context;
4931 struct wined3d_surface *target;
4934 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
4936 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4940 if (FAILED(hr = device->blitter->alloc_private(device)))
4942 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4943 device->shader_backend->shader_free_private(device);
4947 /* Recreate the primary swapchain's context */
4948 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4949 if (!swapchain->context)
4951 ERR("Failed to allocate memory for swapchain context array.\n");
4952 device->blitter->free_private(device);
4953 device->shader_backend->shader_free_private(device);
4954 return E_OUTOFMEMORY;
4957 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4958 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4960 WARN("Failed to create context.\n");
4961 device->blitter->free_private(device);
4962 device->shader_backend->shader_free_private(device);
4963 HeapFree(GetProcessHeap(), 0, swapchain->context);
4967 swapchain->context[0] = context;
4968 swapchain->num_contexts = 1;
4969 create_dummy_textures(device, context);
4970 context_release(context);
4975 /* Do not call while under the GL lock. */
4976 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4977 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4978 wined3d_device_reset_cb callback)
4980 struct wined3d_resource *resource, *cursor;
4981 struct wined3d_swapchain *swapchain;
4982 struct wined3d_display_mode m;
4983 BOOL DisplayModeChanged = FALSE;
4984 BOOL update_desc = FALSE;
4988 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4990 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4992 ERR("Failed to get the first implicit swapchain.\n");
4993 return WINED3DERR_INVALIDCALL;
4996 stateblock_unbind_resources(device->stateBlock);
4997 if (device->fb.render_targets)
4999 if (swapchain->back_buffers && swapchain->back_buffers[0])
5000 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
5002 wined3d_device_set_render_target(device, 0, swapchain->front_buffer, FALSE);
5003 for (i = 1; i < device->adapter->gl_info.limits.buffers; ++i)
5005 wined3d_device_set_render_target(device, i, NULL, FALSE);
5008 wined3d_device_set_depth_stencil(device, NULL);
5010 if (device->onscreen_depth_stencil)
5012 wined3d_surface_decref(device->onscreen_depth_stencil);
5013 device->onscreen_depth_stencil = NULL;
5016 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5018 TRACE("Enumerating resource %p.\n", resource);
5019 if (FAILED(hr = callback(resource)))
5023 /* Is it necessary to recreate the gl context? Actually every setting can be changed
5024 * on an existing gl context, so there's no real need for recreation.
5026 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
5028 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
5030 TRACE("New params:\n");
5031 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5032 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5033 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5034 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5035 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5036 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5037 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5038 TRACE("device_window %p\n", swapchain_desc->device_window);
5039 TRACE("windowed %#x\n", swapchain_desc->windowed);
5040 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5041 if (swapchain_desc->enable_auto_depth_stencil)
5042 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5043 TRACE("flags %#x\n", swapchain_desc->flags);
5044 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5045 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
5046 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5048 /* No special treatment of these parameters. Just store them */
5049 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
5050 swapchain->desc.flags = swapchain_desc->flags;
5051 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
5052 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
5054 /* What to do about these? */
5055 if (swapchain_desc->backbuffer_count
5056 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
5057 FIXME("Cannot change the back buffer count yet.\n");
5059 if (swapchain_desc->device_window
5060 && swapchain_desc->device_window != swapchain->desc.device_window)
5062 TRACE("Changing the device window from %p to %p.\n",
5063 swapchain->desc.device_window, swapchain_desc->device_window);
5064 swapchain->desc.device_window = swapchain_desc->device_window;
5065 swapchain->device_window = swapchain_desc->device_window;
5066 wined3d_swapchain_set_window(swapchain, NULL);
5069 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
5073 TRACE("Creating the depth stencil buffer\n");
5075 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
5076 device->device_parent, swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height,
5077 swapchain_desc->auto_depth_stencil_format, WINED3DUSAGE_DEPTHSTENCIL,
5078 swapchain_desc->multisample_type, swapchain_desc->multisample_quality,
5079 &device->auto_depth_stencil)))
5081 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
5082 return WINED3DERR_INVALIDCALL;
5086 /* Reset the depth stencil */
5087 if (swapchain_desc->enable_auto_depth_stencil)
5088 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5092 DisplayModeChanged = TRUE;
5095 else if (swapchain_desc->windowed)
5097 m.width = swapchain->orig_width;
5098 m.height = swapchain->orig_height;
5100 m.format_id = swapchain->desc.backbuffer_format;
5101 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5105 m.width = swapchain_desc->backbuffer_width;
5106 m.height = swapchain_desc->backbuffer_height;
5107 m.refresh_rate = swapchain_desc->refresh_rate;
5108 m.format_id = swapchain_desc->backbuffer_format;
5109 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5112 /* Should Width == 800 && Height == 0 set 800x600? */
5113 if (swapchain_desc->backbuffer_width && swapchain_desc->backbuffer_height
5114 && (swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
5115 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height))
5117 if (!swapchain_desc->windowed)
5118 DisplayModeChanged = TRUE;
5120 swapchain->desc.backbuffer_width = swapchain_desc->backbuffer_width;
5121 swapchain->desc.backbuffer_height = swapchain_desc->backbuffer_height;
5125 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5126 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5128 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5132 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5133 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5135 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5136 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5144 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5145 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5146 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5149 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5151 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5152 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5153 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5156 if (device->auto_depth_stencil)
5158 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5159 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5160 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5165 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5166 || DisplayModeChanged)
5168 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
5170 WARN("Failed to set display mode, hr %#x.\n", hr);
5171 return WINED3DERR_INVALIDCALL;
5174 if (!swapchain_desc->windowed)
5176 if (swapchain->desc.windowed)
5178 HWND focus_window = device->create_parms.focus_window;
5180 focus_window = swapchain_desc->device_window;
5181 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5183 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5187 /* switch from windowed to fs */
5188 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5189 swapchain_desc->backbuffer_width,
5190 swapchain_desc->backbuffer_height);
5194 /* Fullscreen -> fullscreen mode change */
5195 MoveWindow(swapchain->device_window, 0, 0,
5196 swapchain_desc->backbuffer_width,
5197 swapchain_desc->backbuffer_height,
5201 else if (!swapchain->desc.windowed)
5203 /* Fullscreen -> windowed switch */
5204 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5205 wined3d_device_release_focus_window(device);
5207 swapchain->desc.windowed = swapchain_desc->windowed;
5209 else if (!swapchain_desc->windowed)
5211 DWORD style = device->style;
5212 DWORD exStyle = device->exStyle;
5213 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5214 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5215 * Reset to clear up their mess. Guild Wars also loses the device during that.
5218 device->exStyle = 0;
5219 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5220 swapchain_desc->backbuffer_width,
5221 swapchain_desc->backbuffer_height);
5222 device->style = style;
5223 device->exStyle = exStyle;
5226 TRACE("Resetting stateblock.\n");
5227 wined3d_stateblock_decref(device->updateStateBlock);
5228 wined3d_stateblock_decref(device->stateBlock);
5230 if (device->d3d_initialized)
5231 delete_opengl_contexts(device, swapchain);
5233 /* Note: No parent needed for initial internal stateblock */
5234 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5236 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5238 TRACE("Created stateblock %p.\n", device->stateBlock);
5239 device->updateStateBlock = device->stateBlock;
5240 wined3d_stateblock_incref(device->updateStateBlock);
5242 stateblock_init_default_state(device->stateBlock);
5244 swapchain_update_render_to_fbo(swapchain);
5245 swapchain_update_draw_bindings(swapchain);
5247 if (device->d3d_initialized)
5248 hr = create_primary_opengl_context(device, swapchain);
5250 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5256 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5258 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5260 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5266 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5267 struct wined3d_device_creation_parameters *parameters)
5269 TRACE("device %p, parameters %p.\n", device, parameters);
5271 *parameters = device->create_parms;
5274 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5275 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5277 struct wined3d_swapchain *swapchain;
5279 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5280 device, swapchain_idx, flags, ramp);
5282 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5283 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5286 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5287 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5289 struct wined3d_swapchain *swapchain;
5291 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5292 device, swapchain_idx, ramp);
5294 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5295 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5298 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5300 TRACE("device %p, resource %p.\n", device, resource);
5302 list_add_head(&device->resources, &resource->resource_list_entry);
5305 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5307 TRACE("device %p, resource %p.\n", device, resource);
5309 list_remove(&resource->resource_list_entry);
5312 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5314 enum wined3d_resource_type type = resource->type;
5317 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5319 context_resource_released(device, resource, type);
5323 case WINED3D_RTYPE_SURFACE:
5325 struct wined3d_surface *surface = surface_from_resource(resource);
5327 if (!device->d3d_initialized) break;
5329 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5331 if (device->fb.render_targets[i] == surface)
5333 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5334 device->fb.render_targets[i] = NULL;
5338 if (device->fb.depth_stencil == surface)
5340 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5341 device->fb.depth_stencil = NULL;
5346 case WINED3D_RTYPE_TEXTURE:
5347 case WINED3D_RTYPE_CUBE_TEXTURE:
5348 case WINED3D_RTYPE_VOLUME_TEXTURE:
5349 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5351 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5353 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5355 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5356 texture, device->stateBlock, i);
5357 device->stateBlock->state.textures[i] = NULL;
5360 if (device->updateStateBlock != device->stateBlock
5361 && device->updateStateBlock->state.textures[i] == texture)
5363 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5364 texture, device->updateStateBlock, i);
5365 device->updateStateBlock->state.textures[i] = NULL;
5370 case WINED3D_RTYPE_BUFFER:
5372 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5374 for (i = 0; i < MAX_STREAMS; ++i)
5376 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5378 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5379 buffer, device->stateBlock, i);
5380 device->stateBlock->state.streams[i].buffer = NULL;
5383 if (device->updateStateBlock != device->stateBlock
5384 && device->updateStateBlock->state.streams[i].buffer == buffer)
5386 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5387 buffer, device->updateStateBlock, i);
5388 device->updateStateBlock->state.streams[i].buffer = NULL;
5393 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5395 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5396 buffer, device->stateBlock);
5397 device->stateBlock->state.index_buffer = NULL;
5400 if (device->updateStateBlock != device->stateBlock
5401 && device->updateStateBlock->state.index_buffer == buffer)
5403 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5404 buffer, device->updateStateBlock);
5405 device->updateStateBlock->state.index_buffer = NULL;
5414 /* Remove the resource from the resourceStore */
5415 device_resource_remove(device, resource);
5417 TRACE("Resource released.\n");
5420 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
5422 struct wined3d_resource *resource;
5424 TRACE("device %p, dc %p.\n", device, dc);
5429 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5431 if (resource->type == WINED3D_RTYPE_SURFACE)
5433 struct wined3d_surface *s = surface_from_resource(resource);
5437 TRACE("Found surface %p for dc %p.\n", s, dc);
5446 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5447 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5448 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5450 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5451 const struct fragment_pipeline *fragment_pipeline;
5452 struct shader_caps shader_caps;
5453 struct fragment_caps ffp_caps;
5458 device->wined3d = wined3d;
5459 wined3d_incref(device->wined3d);
5460 device->adapter = wined3d->adapter_count ? adapter : NULL;
5461 device->device_parent = device_parent;
5462 list_init(&device->resources);
5463 list_init(&device->shaders);
5464 device->surface_alignment = surface_alignment;
5466 /* Save the creation parameters. */
5467 device->create_parms.adapter_idx = adapter_idx;
5468 device->create_parms.device_type = device_type;
5469 device->create_parms.focus_window = focus_window;
5470 device->create_parms.flags = flags;
5472 for (i = 0; i < PATCHMAP_SIZE; ++i) list_init(&device->patches[i]);
5474 select_shader_mode(&adapter->gl_info, &device->ps_selected_mode, &device->vs_selected_mode);
5475 device->shader_backend = adapter->shader_backend;
5477 if (device->shader_backend)
5479 device->shader_backend->shader_get_caps(&adapter->gl_info, &shader_caps);
5480 device->vs_version = shader_caps.vs_version;
5481 device->gs_version = shader_caps.gs_version;
5482 device->ps_version = shader_caps.ps_version;
5483 device->d3d_vshader_constantF = shader_caps.vs_uniform_count;
5484 device->d3d_pshader_constantF = shader_caps.ps_uniform_count;
5485 device->vs_clipping = shader_caps.vs_clipping;
5487 fragment_pipeline = adapter->fragment_pipe;
5488 if (fragment_pipeline)
5490 fragment_pipeline->get_caps(&adapter->gl_info, &ffp_caps);
5491 device->max_ffp_textures = ffp_caps.MaxSimultaneousTextures;
5493 hr = compile_state_table(device->StateTable, device->multistate_funcs, &adapter->gl_info,
5494 ffp_vertexstate_template, fragment_pipeline, misc_state_template);
5497 ERR("Failed to compile state table, hr %#x.\n", hr);
5498 wined3d_decref(device->wined3d);
5502 device->blitter = adapter->blitter;
5504 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5507 WARN("Failed to create stateblock.\n");
5508 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5510 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5512 wined3d_decref(device->wined3d);
5516 TRACE("Created stateblock %p.\n", device->stateBlock);
5517 device->updateStateBlock = device->stateBlock;
5518 wined3d_stateblock_incref(device->updateStateBlock);
5524 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5526 DWORD rep = device->StateTable[state].representative;
5527 struct wined3d_context *context;
5532 for (i = 0; i < device->context_count; ++i)
5534 context = device->contexts[i];
5535 if(isStateDirty(context, rep)) continue;
5537 context->dirtyArray[context->numDirtyEntries++] = rep;
5538 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5539 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5540 context->isStateDirty[idx] |= (1 << shift);
5544 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5546 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5547 *width = context->current_rt->pow2Width;
5548 *height = context->current_rt->pow2Height;
5551 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5553 const struct wined3d_swapchain *swapchain = context->swapchain;
5554 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5555 * current context's drawable, which is the size of the back buffer of the swapchain
5556 * the active context belongs to. */
5557 *width = swapchain->desc.backbuffer_width;
5558 *height = swapchain->desc.backbuffer_height;
5561 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5562 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5564 if (device->filter_messages)
5566 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5567 window, message, wparam, lparam);
5569 return DefWindowProcW(window, message, wparam, lparam);
5571 return DefWindowProcA(window, message, wparam, lparam);
5574 if (message == WM_DESTROY)
5576 TRACE("unregister window %p.\n", window);
5577 wined3d_unregister_window(window);
5579 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5580 ERR("Window %p is not the focus window for device %p.\n", window, device);
5582 else if (message == WM_DISPLAYCHANGE)
5584 device->device_parent->ops->mode_changed(device->device_parent);
5588 return CallWindowProcW(proc, window, message, wparam, lparam);
5590 return CallWindowProcA(proc, window, message, wparam, lparam);