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");
713 /* Only set the values up once, as they are not changing. */
714 if (flags & WINED3DCLEAR_STENCIL)
716 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
718 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
719 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
721 gl_info->gl_ops.gl.p_glStencilMask(~0U);
722 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
723 gl_info->gl_ops.gl.p_glClearStencil(stencil);
724 checkGLcall("glClearStencil");
725 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
728 if (flags & WINED3DCLEAR_ZBUFFER)
730 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
732 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
734 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
735 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
736 gl_info->gl_ops.gl.p_glClearDepth(depth);
737 checkGLcall("glClearDepth");
738 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
741 if (flags & WINED3DCLEAR_TARGET)
743 for (i = 0; i < rt_count; ++i)
745 struct wined3d_surface *rt = fb->render_targets[i];
748 surface_modify_location(rt, rt->draw_binding, TRUE);
751 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
752 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
753 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
754 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
755 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
756 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
757 checkGLcall("glClearColor");
758 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
763 if (render_offscreen)
765 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
766 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
770 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
771 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
773 checkGLcall("glScissor");
774 gl_info->gl_ops.gl.p_glClear(clear_mask);
775 checkGLcall("glClear");
781 /* Now process each rect in turn. */
782 for (i = 0; i < rect_count; ++i)
784 /* Note that GL uses lower left, width/height. */
785 IntersectRect(¤t_rect, draw_rect, &clear_rect[i]);
787 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
788 wine_dbgstr_rect(&clear_rect[i]),
789 wine_dbgstr_rect(¤t_rect));
791 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
792 * The rectangle is not cleared, no error is returned, but further rectangles are
793 * still cleared if they are valid. */
794 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
796 TRACE("Rectangle with negative dimensions, ignoring.\n");
800 if (render_offscreen)
802 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
803 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
807 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
808 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
810 checkGLcall("glScissor");
812 gl_info->gl_ops.gl.p_glClear(clear_mask);
813 checkGLcall("glClear");
817 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
818 && target->container.type == WINED3D_CONTAINER_SWAPCHAIN
819 && target->container.u.swapchain->front_buffer == target))
820 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
822 context_release(context);
825 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
827 ULONG refcount = InterlockedIncrement(&device->ref);
829 TRACE("%p increasing refcount to %u.\n", device, refcount);
834 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
836 ULONG refcount = InterlockedDecrement(&device->ref);
838 TRACE("%p decreasing refcount to %u.\n", device, refcount);
842 struct wined3d_stateblock *stateblock;
845 if (wined3d_stateblock_decref(device->updateStateBlock)
846 && device->updateStateBlock != device->stateBlock)
847 FIXME("Something's still holding the update stateblock.\n");
848 device->updateStateBlock = NULL;
850 stateblock = device->stateBlock;
851 device->stateBlock = NULL;
852 if (wined3d_stateblock_decref(stateblock))
853 FIXME("Something's still holding the stateblock.\n");
855 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
857 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
858 device->multistate_funcs[i] = NULL;
861 if (!list_empty(&device->resources))
863 struct wined3d_resource *resource;
865 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
867 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
869 FIXME("Leftover resource %p with type %s (%#x).\n",
870 resource, debug_d3dresourcetype(resource->type), resource->type);
874 if (device->contexts)
875 ERR("Context array not freed!\n");
876 if (device->hardwareCursor)
877 DestroyCursor(device->hardwareCursor);
878 device->hardwareCursor = 0;
880 wined3d_decref(device->wined3d);
881 device->wined3d = NULL;
882 HeapFree(GetProcessHeap(), 0, device);
883 TRACE("Freed device %p.\n", device);
889 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
891 TRACE("device %p.\n", device);
893 return device->swapchain_count;
896 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
898 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
900 if (swapchain_idx >= device->swapchain_count)
902 WARN("swapchain_idx %u >= swapchain_count %u.\n",
903 swapchain_idx, device->swapchain_count);
907 return device->swapchains[swapchain_idx];
910 static void device_load_logo(struct wined3d_device *device, const char *filename)
912 struct wined3d_color_key color_key;
916 HDC dcb = NULL, dcs = NULL;
918 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
921 GetObjectA(hbm, sizeof(BITMAP), &bm);
922 dcb = CreateCompatibleDC(NULL);
924 SelectObject(dcb, hbm);
928 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
931 memset(&bm, 0, sizeof(bm));
936 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0,
937 WINED3D_POOL_SYSTEM_MEM, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_TYPE_OPENGL, WINED3D_SURFACE_MAPPABLE,
938 NULL, &wined3d_null_parent_ops, &device->logo_surface);
941 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
947 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
949 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
950 wined3d_surface_releasedc(device->logo_surface, dcs);
952 color_key.color_space_low_value = 0;
953 color_key.color_space_high_value = 0;
954 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
958 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
959 /* Fill the surface with a white color to show that wined3d is there */
960 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
964 if (dcb) DeleteDC(dcb);
965 if (hbm) DeleteObject(hbm);
968 /* Context activation is done by the caller. */
969 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
971 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
972 unsigned int i, j, count;
973 /* Under DirectX you can sample even if no texture is bound, whereas
974 * OpenGL will only allow that when a valid texture is bound.
975 * We emulate this by creating dummy textures and binding them
976 * to each texture stage when the currently set D3D texture is NULL. */
978 if (gl_info->supported[APPLE_CLIENT_STORAGE])
980 /* The dummy texture does not have client storage backing */
981 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
982 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
985 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
986 for (i = 0; i < count; ++i)
988 DWORD color = 0x000000ff;
990 /* Make appropriate texture active */
991 context_active_texture(context, gl_info, i);
993 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
994 checkGLcall("glGenTextures");
995 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
997 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
998 checkGLcall("glBindTexture");
1000 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
1001 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1002 checkGLcall("glTexImage2D");
1004 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1006 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
1007 checkGLcall("glGenTextures");
1008 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
1010 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1011 checkGLcall("glBindTexture");
1013 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
1014 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1015 checkGLcall("glTexImage2D");
1018 if (gl_info->supported[EXT_TEXTURE3D])
1020 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
1021 checkGLcall("glGenTextures");
1022 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
1024 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1025 checkGLcall("glBindTexture");
1027 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
1028 checkGLcall("glTexImage3D");
1031 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1033 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
1034 checkGLcall("glGenTextures");
1035 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
1037 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1038 checkGLcall("glBindTexture");
1040 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
1042 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
1043 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
1044 checkGLcall("glTexImage2D");
1049 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1051 /* Re-enable because if supported it is enabled by default */
1052 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1053 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1057 /* Context activation is done by the caller. */
1058 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
1060 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1062 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1064 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
1065 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
1068 if (gl_info->supported[EXT_TEXTURE3D])
1070 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
1071 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
1074 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1076 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
1077 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
1080 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
1081 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
1083 memset(device->dummy_texture_cube, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_cube));
1084 memset(device->dummy_texture_3d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_3d));
1085 memset(device->dummy_texture_rect, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_rect));
1086 memset(device->dummy_texture_2d, 0, gl_info->limits.textures * sizeof(*device->dummy_texture_2d));
1089 static LONG fullscreen_style(LONG style)
1091 /* Make sure the window is managed, otherwise we won't get keyboard input. */
1092 style |= WS_POPUP | WS_SYSMENU;
1093 style &= ~(WS_CAPTION | WS_THICKFRAME);
1098 static LONG fullscreen_exstyle(LONG exstyle)
1100 /* Filter out window decorations. */
1101 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1106 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1108 BOOL filter_messages;
1109 LONG style, exstyle;
1111 TRACE("Setting up window %p for fullscreen mode.\n", window);
1113 if (device->style || device->exStyle)
1115 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1116 window, device->style, device->exStyle);
1119 device->style = GetWindowLongW(window, GWL_STYLE);
1120 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1122 style = fullscreen_style(device->style);
1123 exstyle = fullscreen_exstyle(device->exStyle);
1125 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1126 device->style, device->exStyle, style, exstyle);
1128 filter_messages = device->filter_messages;
1129 device->filter_messages = TRUE;
1131 SetWindowLongW(window, GWL_STYLE, style);
1132 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1133 SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1135 device->filter_messages = filter_messages;
1138 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1140 BOOL filter_messages;
1141 LONG style, exstyle;
1143 if (!device->style && !device->exStyle) return;
1145 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1146 window, device->style, device->exStyle);
1148 style = GetWindowLongW(window, GWL_STYLE);
1149 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1151 filter_messages = device->filter_messages;
1152 device->filter_messages = TRUE;
1154 /* Only restore the style if the application didn't modify it during the
1155 * fullscreen phase. Some applications change it before calling Reset()
1156 * when switching between windowed and fullscreen modes (HL2), some
1157 * depend on the original style (Eve Online). */
1158 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1160 SetWindowLongW(window, GWL_STYLE, device->style);
1161 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1163 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1165 device->filter_messages = filter_messages;
1167 /* Delete the old values. */
1169 device->exStyle = 0;
1172 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1174 TRACE("device %p, window %p.\n", device, window);
1176 if (!wined3d_register_window(window, device))
1178 ERR("Failed to register window %p.\n", window);
1182 InterlockedExchangePointer((void **)&device->focus_window, window);
1183 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1188 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1190 TRACE("device %p.\n", device);
1192 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1193 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1196 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1197 struct wined3d_swapchain_desc *swapchain_desc)
1199 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1200 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1201 struct wined3d_swapchain *swapchain = NULL;
1202 struct wined3d_context *context;
1207 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1209 if (device->d3d_initialized)
1210 return WINED3DERR_INVALIDCALL;
1211 if (!device->adapter->opengl)
1212 return WINED3DERR_INVALIDCALL;
1214 device->valid_rt_mask = 0;
1215 for (i = 0; i < gl_info->limits.buffers; ++i)
1216 device->valid_rt_mask |= (1 << i);
1217 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1218 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1220 /* Initialize the texture unit mapping to a 1:1 mapping */
1221 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1223 if (state < gl_info->limits.fragment_samplers)
1225 device->texUnitMap[state] = state;
1226 device->rev_tex_unit_map[state] = state;
1230 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1231 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1235 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
1237 TRACE("Shader private data couldn't be allocated\n");
1240 if (FAILED(hr = device->blitter->alloc_private(device)))
1242 TRACE("Blitter private data couldn't be allocated\n");
1246 /* Setup the implicit swapchain. This also initializes a context. */
1247 TRACE("Creating implicit swapchain\n");
1248 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1249 swapchain_desc, &swapchain);
1252 WARN("Failed to create implicit swapchain\n");
1256 device->swapchain_count = 1;
1257 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1258 if (!device->swapchains)
1260 ERR("Out of memory!\n");
1263 device->swapchains[0] = swapchain;
1265 if (swapchain->back_buffers && swapchain->back_buffers[0])
1267 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1268 device->fb.render_targets[0] = swapchain->back_buffers[0];
1272 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1273 device->fb.render_targets[0] = swapchain->front_buffer;
1275 wined3d_surface_incref(device->fb.render_targets[0]);
1277 /* Depth Stencil support */
1278 device->fb.depth_stencil = device->auto_depth_stencil;
1279 if (device->fb.depth_stencil)
1280 wined3d_surface_incref(device->fb.depth_stencil);
1282 /* Set up some starting GL setup */
1284 /* Setup all the devices defaults */
1285 stateblock_init_default_state(device->stateBlock);
1287 context = context_acquire(device, swapchain->front_buffer);
1289 create_dummy_textures(device, context);
1291 /* Initialize the current view state */
1292 device->view_ident = 1;
1293 device->contexts[0]->last_was_rhw = 0;
1295 switch (wined3d_settings.offscreen_rendering_mode)
1298 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1301 case ORM_BACKBUFFER:
1303 if (context_get_current()->aux_buffers > 0)
1305 TRACE("Using auxiliary buffer for offscreen rendering\n");
1306 device->offscreenBuffer = GL_AUX0;
1310 TRACE("Using back buffer for offscreen rendering\n");
1311 device->offscreenBuffer = GL_BACK;
1316 TRACE("All defaults now set up, leaving 3D init.\n");
1318 context_release(context);
1320 /* Clear the screen */
1321 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1322 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1325 device->d3d_initialized = TRUE;
1327 if (wined3d_settings.logo)
1328 device_load_logo(device, wined3d_settings.logo);
1332 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1333 HeapFree(GetProcessHeap(), 0, device->swapchains);
1334 device->swapchain_count = 0;
1336 wined3d_swapchain_decref(swapchain);
1337 if (device->blit_priv)
1338 device->blitter->free_private(device);
1339 if (device->shader_priv)
1340 device->shader_backend->shader_free_private(device);
1345 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1346 struct wined3d_swapchain_desc *swapchain_desc)
1348 struct wined3d_swapchain *swapchain = NULL;
1351 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1353 /* Setup the implicit swapchain */
1354 TRACE("Creating implicit swapchain\n");
1355 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1356 swapchain_desc, &swapchain);
1359 WARN("Failed to create implicit swapchain\n");
1363 device->swapchain_count = 1;
1364 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1365 if (!device->swapchains)
1367 ERR("Out of memory!\n");
1370 device->swapchains[0] = swapchain;
1374 wined3d_swapchain_decref(swapchain);
1378 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1380 struct wined3d_resource *resource, *cursor;
1381 const struct wined3d_gl_info *gl_info;
1382 struct wined3d_context *context;
1383 struct wined3d_surface *surface;
1386 TRACE("device %p.\n", device);
1388 if (!device->d3d_initialized)
1389 return WINED3DERR_INVALIDCALL;
1391 /* Force making the context current again, to verify it is still valid
1392 * (workaround for broken drivers) */
1393 context_set_current(NULL);
1394 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1395 * it was created. Thus make sure a context is active for the glDelete* calls
1397 context = context_acquire(device, NULL);
1398 gl_info = context->gl_info;
1400 if (device->logo_surface)
1401 wined3d_surface_decref(device->logo_surface);
1403 stateblock_unbind_resources(device->stateBlock);
1405 /* Unload resources */
1406 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1408 TRACE("Unloading resource %p.\n", resource);
1410 resource->resource_ops->resource_unload(resource);
1413 TRACE("Deleting high order patches\n");
1414 for (i = 0; i < PATCHMAP_SIZE; ++i)
1416 struct wined3d_rect_patch *patch;
1417 struct list *e1, *e2;
1419 LIST_FOR_EACH_SAFE(e1, e2, &device->patches[i])
1421 patch = LIST_ENTRY(e1, struct wined3d_rect_patch, entry);
1422 wined3d_device_delete_patch(device, patch->Handle);
1426 /* Delete the mouse cursor texture */
1427 if (device->cursorTexture)
1429 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
1430 device->cursorTexture = 0;
1433 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1434 * private data, it might contain opengl pointers
1436 if (device->depth_blt_texture)
1438 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1439 device->depth_blt_texture = 0;
1442 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1443 device->blitter->free_private(device);
1444 device->shader_backend->shader_free_private(device);
1446 /* Release the buffers (with sanity checks)*/
1447 if (device->onscreen_depth_stencil)
1449 surface = device->onscreen_depth_stencil;
1450 device->onscreen_depth_stencil = NULL;
1451 wined3d_surface_decref(surface);
1454 if (device->fb.depth_stencil)
1456 surface = device->fb.depth_stencil;
1458 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1460 device->fb.depth_stencil = NULL;
1461 wined3d_surface_decref(surface);
1464 if (device->auto_depth_stencil)
1466 surface = device->auto_depth_stencil;
1467 device->auto_depth_stencil = NULL;
1468 if (wined3d_surface_decref(surface))
1469 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1472 for (i = 1; i < gl_info->limits.buffers; ++i)
1474 wined3d_device_set_render_target(device, i, NULL, FALSE);
1477 surface = device->fb.render_targets[0];
1478 TRACE("Setting rendertarget 0 to NULL\n");
1479 device->fb.render_targets[0] = NULL;
1480 TRACE("Releasing the render target at %p\n", surface);
1481 wined3d_surface_decref(surface);
1483 context_release(context);
1485 for (i = 0; i < device->swapchain_count; ++i)
1487 TRACE("Releasing the implicit swapchain %u.\n", i);
1488 if (wined3d_swapchain_decref(device->swapchains[i]))
1489 FIXME("Something's still holding the implicit swapchain.\n");
1492 HeapFree(GetProcessHeap(), 0, device->swapchains);
1493 device->swapchains = NULL;
1494 device->swapchain_count = 0;
1496 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1497 device->fb.render_targets = NULL;
1499 device->d3d_initialized = FALSE;
1504 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1508 for (i = 0; i < device->swapchain_count; ++i)
1510 TRACE("Releasing the implicit swapchain %u.\n", i);
1511 if (wined3d_swapchain_decref(device->swapchains[i]))
1512 FIXME("Something's still holding the implicit swapchain.\n");
1515 HeapFree(GetProcessHeap(), 0, device->swapchains);
1516 device->swapchains = NULL;
1517 device->swapchain_count = 0;
1521 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1522 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1523 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1525 * There is no way to deactivate thread safety once it is enabled.
1527 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1529 TRACE("device %p.\n", device);
1531 /* For now just store the flag (needed in case of ddraw). */
1532 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1535 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1537 TRACE("device %p.\n", device);
1539 TRACE("Emulating %d MB, returning %d MB left.\n",
1540 device->adapter->TextureRam / (1024 * 1024),
1541 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1543 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1546 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1547 struct wined3d_buffer *buffer, UINT offset)
1549 struct wined3d_buffer *prev_buffer;
1551 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1553 if (idx >= MAX_STREAM_OUT)
1555 WARN("Invalid stream output %u.\n", idx);
1559 prev_buffer = device->updateStateBlock->state.stream_output[idx].buffer;
1560 device->updateStateBlock->state.stream_output[idx].buffer = buffer;
1561 device->updateStateBlock->state.stream_output[idx].offset = offset;
1563 if (device->isRecordingState)
1566 wined3d_buffer_incref(buffer);
1568 wined3d_buffer_decref(prev_buffer);
1572 if (prev_buffer != buffer)
1576 InterlockedIncrement(&buffer->resource.bind_count);
1577 wined3d_buffer_incref(buffer);
1581 InterlockedDecrement(&prev_buffer->resource.bind_count);
1582 wined3d_buffer_decref(prev_buffer);
1587 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1588 UINT idx, UINT *offset)
1590 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1592 if (idx >= MAX_STREAM_OUT)
1594 WARN("Invalid stream output %u.\n", idx);
1598 *offset = device->stateBlock->state.stream_output[idx].offset;
1599 return device->stateBlock->state.stream_output[idx].buffer;
1602 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1603 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1605 struct wined3d_stream_state *stream;
1606 struct wined3d_buffer *prev_buffer;
1608 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1609 device, stream_idx, buffer, offset, stride);
1611 if (stream_idx >= MAX_STREAMS)
1613 WARN("Stream index %u out of range.\n", stream_idx);
1614 return WINED3DERR_INVALIDCALL;
1616 else if (offset & 0x3)
1618 WARN("Offset %u is not 4 byte aligned.\n", offset);
1619 return WINED3DERR_INVALIDCALL;
1622 stream = &device->updateStateBlock->state.streams[stream_idx];
1623 prev_buffer = stream->buffer;
1625 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1627 if (prev_buffer == buffer
1628 && stream->stride == stride
1629 && stream->offset == offset)
1631 TRACE("Application is setting the old values over, nothing to do.\n");
1635 stream->buffer = buffer;
1638 stream->stride = stride;
1639 stream->offset = offset;
1642 /* Handle recording of state blocks. */
1643 if (device->isRecordingState)
1645 TRACE("Recording... not performing anything.\n");
1647 wined3d_buffer_incref(buffer);
1649 wined3d_buffer_decref(prev_buffer);
1655 InterlockedIncrement(&buffer->resource.bind_count);
1656 wined3d_buffer_incref(buffer);
1660 InterlockedDecrement(&prev_buffer->resource.bind_count);
1661 wined3d_buffer_decref(prev_buffer);
1664 device_invalidate_state(device, STATE_STREAMSRC);
1669 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1670 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1672 struct wined3d_stream_state *stream;
1674 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1675 device, stream_idx, buffer, offset, stride);
1677 if (stream_idx >= MAX_STREAMS)
1679 WARN("Stream index %u out of range.\n", stream_idx);
1680 return WINED3DERR_INVALIDCALL;
1683 stream = &device->stateBlock->state.streams[stream_idx];
1684 *buffer = stream->buffer;
1686 wined3d_buffer_incref(*buffer);
1688 *offset = stream->offset;
1689 *stride = stream->stride;
1694 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1696 struct wined3d_stream_state *stream;
1697 UINT old_flags, old_freq;
1699 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1701 /* Verify input. At least in d3d9 this is invalid. */
1702 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1704 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1705 return WINED3DERR_INVALIDCALL;
1707 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1709 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1710 return WINED3DERR_INVALIDCALL;
1714 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1715 return WINED3DERR_INVALIDCALL;
1718 stream = &device->updateStateBlock->state.streams[stream_idx];
1719 old_flags = stream->flags;
1720 old_freq = stream->frequency;
1722 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1723 stream->frequency = divider & 0x7fffff;
1725 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1727 if (stream->frequency != old_freq || stream->flags != old_flags)
1728 device_invalidate_state(device, STATE_STREAMSRC);
1733 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1734 UINT stream_idx, UINT *divider)
1736 struct wined3d_stream_state *stream;
1738 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1740 stream = &device->updateStateBlock->state.streams[stream_idx];
1741 *divider = stream->flags | stream->frequency;
1743 TRACE("Returning %#x.\n", *divider);
1748 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1749 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1751 TRACE("device %p, state %s, matrix %p.\n",
1752 device, debug_d3dtstype(d3dts), matrix);
1753 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1754 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1755 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1756 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1758 /* Handle recording of state blocks. */
1759 if (device->isRecordingState)
1761 TRACE("Recording... not performing anything.\n");
1762 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1763 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1767 /* If the new matrix is the same as the current one,
1768 * we cut off any further processing. this seems to be a reasonable
1769 * optimization because as was noticed, some apps (warcraft3 for example)
1770 * tend towards setting the same matrix repeatedly for some reason.
1772 * From here on we assume that the new matrix is different, wherever it matters. */
1773 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1775 TRACE("The application is setting the same matrix over again.\n");
1779 device->stateBlock->state.transforms[d3dts] = *matrix;
1780 if (d3dts == WINED3D_TS_VIEW)
1781 device->view_ident = !memcmp(matrix, &identity, sizeof(identity));
1783 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1784 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1787 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1788 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1790 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1792 *matrix = device->stateBlock->state.transforms[state];
1795 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1796 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1798 const struct wined3d_matrix *mat;
1799 struct wined3d_matrix temp;
1801 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1803 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1804 * below means it will be recorded in a state block change, but it
1805 * works regardless where it is recorded.
1806 * If this is found to be wrong, change to StateBlock. */
1807 if (state > HIGHEST_TRANSFORMSTATE)
1809 WARN("Unhandled transform state %#x.\n", state);
1813 mat = &device->updateStateBlock->state.transforms[state];
1814 multiply_matrix(&temp, mat, matrix);
1816 /* Apply change via set transform - will reapply to eg. lights this way. */
1817 wined3d_device_set_transform(device, state, &temp);
1820 /* Note lights are real special cases. Although the device caps state only
1821 * e.g. 8 are supported, you can reference any indexes you want as long as
1822 * that number max are enabled at any one point in time. Therefore since the
1823 * indices can be anything, we need a hashmap of them. However, this causes
1824 * stateblock problems. When capturing the state block, I duplicate the
1825 * hashmap, but when recording, just build a chain pretty much of commands to
1827 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1828 UINT light_idx, const struct wined3d_light *light)
1830 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1831 struct wined3d_light_info *object = NULL;
1835 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1837 /* Check the parameter range. Need for speed most wanted sets junk lights
1838 * which confuse the GL driver. */
1840 return WINED3DERR_INVALIDCALL;
1842 switch (light->type)
1844 case WINED3D_LIGHT_POINT:
1845 case WINED3D_LIGHT_SPOT:
1846 case WINED3D_LIGHT_PARALLELPOINT:
1847 case WINED3D_LIGHT_GLSPOT:
1848 /* Incorrect attenuation values can cause the gl driver to crash.
1849 * Happens with Need for speed most wanted. */
1850 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1852 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1853 return WINED3DERR_INVALIDCALL;
1857 case WINED3D_LIGHT_DIRECTIONAL:
1858 /* Ignores attenuation */
1862 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1863 return WINED3DERR_INVALIDCALL;
1866 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1868 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1869 if (object->OriginalIndex == light_idx)
1876 TRACE("Adding new light\n");
1877 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1880 ERR("Out of memory error when allocating a light\n");
1881 return E_OUTOFMEMORY;
1883 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1884 object->glIndex = -1;
1885 object->OriginalIndex = light_idx;
1888 /* Initialize the object. */
1889 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1890 light_idx, light->type,
1891 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1892 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1893 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1894 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1895 light->direction.x, light->direction.y, light->direction.z);
1896 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1897 light->range, light->falloff, light->theta, light->phi);
1899 /* Save away the information. */
1900 object->OriginalParms = *light;
1902 switch (light->type)
1904 case WINED3D_LIGHT_POINT:
1906 object->lightPosn[0] = light->position.x;
1907 object->lightPosn[1] = light->position.y;
1908 object->lightPosn[2] = light->position.z;
1909 object->lightPosn[3] = 1.0f;
1910 object->cutoff = 180.0f;
1914 case WINED3D_LIGHT_DIRECTIONAL:
1916 object->lightPosn[0] = -light->direction.x;
1917 object->lightPosn[1] = -light->direction.y;
1918 object->lightPosn[2] = -light->direction.z;
1919 object->lightPosn[3] = 0.0f;
1920 object->exponent = 0.0f;
1921 object->cutoff = 180.0f;
1924 case WINED3D_LIGHT_SPOT:
1926 object->lightPosn[0] = light->position.x;
1927 object->lightPosn[1] = light->position.y;
1928 object->lightPosn[2] = light->position.z;
1929 object->lightPosn[3] = 1.0f;
1932 object->lightDirn[0] = light->direction.x;
1933 object->lightDirn[1] = light->direction.y;
1934 object->lightDirn[2] = light->direction.z;
1935 object->lightDirn[3] = 1.0f;
1937 /* opengl-ish and d3d-ish spot lights use too different models
1938 * for the light "intensity" as a function of the angle towards
1939 * the main light direction, so we only can approximate very
1940 * roughly. However, spot lights are rather rarely used in games
1941 * (if ever used at all). Furthermore if still used, probably
1942 * nobody pays attention to such details. */
1943 if (!light->falloff)
1945 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1946 * equations have the falloff resp. exponent parameter as an
1947 * exponent, so the spot light lighting will always be 1.0 for
1948 * both of them, and we don't have to care for the rest of the
1949 * rather complex calculation. */
1950 object->exponent = 0.0f;
1954 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1957 object->exponent = -0.3f / logf(cosf(rho / 2));
1960 if (object->exponent > 128.0f)
1961 object->exponent = 128.0f;
1963 object->cutoff = (float)(light->phi * 90 / M_PI);
1968 FIXME("Unrecognized light type %#x.\n", light->type);
1971 /* Update the live definitions if the light is currently assigned a glIndex. */
1972 if (object->glIndex != -1 && !device->isRecordingState)
1973 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1978 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1979 UINT light_idx, struct wined3d_light *light)
1981 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1982 struct wined3d_light_info *light_info = NULL;
1985 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1987 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1989 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1990 if (light_info->OriginalIndex == light_idx)
1997 TRACE("Light information requested but light not defined\n");
1998 return WINED3DERR_INVALIDCALL;
2001 *light = light_info->OriginalParms;
2005 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
2007 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2008 struct wined3d_light_info *light_info = NULL;
2011 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
2013 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2015 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2016 if (light_info->OriginalIndex == light_idx)
2020 TRACE("Found light %p.\n", light_info);
2022 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
2025 TRACE("Light enabled requested but light not defined, so defining one!\n");
2026 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
2028 /* Search for it again! Should be fairly quick as near head of list. */
2029 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
2031 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2032 if (light_info->OriginalIndex == light_idx)
2038 FIXME("Adding default lights has failed dismally\n");
2039 return WINED3DERR_INVALIDCALL;
2045 if (light_info->glIndex != -1)
2047 if (!device->isRecordingState)
2048 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
2050 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
2051 light_info->glIndex = -1;
2055 TRACE("Light already disabled, nothing to do\n");
2057 light_info->enabled = FALSE;
2061 light_info->enabled = TRUE;
2062 if (light_info->glIndex != -1)
2064 TRACE("Nothing to do as light was enabled\n");
2069 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2070 /* Find a free GL light. */
2071 for (i = 0; i < gl_info->limits.lights; ++i)
2073 if (!device->updateStateBlock->state.lights[i])
2075 device->updateStateBlock->state.lights[i] = light_info;
2076 light_info->glIndex = i;
2080 if (light_info->glIndex == -1)
2082 /* Our tests show that Windows returns D3D_OK in this situation, even with
2083 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
2084 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
2085 * as well for those lights.
2087 * TODO: Test how this affects rendering. */
2088 WARN("Too many concurrently active lights\n");
2092 /* i == light_info->glIndex */
2093 if (!device->isRecordingState)
2094 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
2101 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
2103 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
2104 struct wined3d_light_info *light_info = NULL;
2107 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
2109 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
2111 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
2112 if (light_info->OriginalIndex == light_idx)
2119 TRACE("Light enabled state requested but light not defined.\n");
2120 return WINED3DERR_INVALIDCALL;
2122 /* true is 128 according to SetLightEnable */
2123 *enable = light_info->enabled ? 128 : 0;
2127 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
2128 UINT plane_idx, const struct wined3d_vec4 *plane)
2130 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2132 /* Validate plane_idx. */
2133 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2135 TRACE("Application has requested clipplane this device doesn't support.\n");
2136 return WINED3DERR_INVALIDCALL;
2139 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2141 if (!memcmp(&device->updateStateBlock->state.clip_planes[plane_idx], plane, sizeof(*plane)))
2143 TRACE("Application is setting old values over, nothing to do.\n");
2147 device->updateStateBlock->state.clip_planes[plane_idx] = *plane;
2149 /* Handle recording of state blocks. */
2150 if (device->isRecordingState)
2152 TRACE("Recording... not performing anything.\n");
2156 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2161 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
2162 UINT plane_idx, struct wined3d_vec4 *plane)
2164 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2166 /* Validate plane_idx. */
2167 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2169 TRACE("Application has requested clipplane this device doesn't support.\n");
2170 return WINED3DERR_INVALIDCALL;
2173 *plane = device->stateBlock->state.clip_planes[plane_idx];
2178 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2179 const struct wined3d_clip_status *clip_status)
2181 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2184 return WINED3DERR_INVALIDCALL;
2189 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2190 struct wined3d_clip_status *clip_status)
2192 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2195 return WINED3DERR_INVALIDCALL;
2200 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2202 TRACE("device %p, material %p.\n", device, material);
2204 device->updateStateBlock->changed.material = TRUE;
2205 device->updateStateBlock->state.material = *material;
2207 /* Handle recording of state blocks */
2208 if (device->isRecordingState)
2210 TRACE("Recording... not performing anything.\n");
2214 device_invalidate_state(device, STATE_MATERIAL);
2217 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2219 TRACE("device %p, material %p.\n", device, material);
2221 *material = device->updateStateBlock->state.material;
2223 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2224 material->diffuse.r, material->diffuse.g,
2225 material->diffuse.b, material->diffuse.a);
2226 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2227 material->ambient.r, material->ambient.g,
2228 material->ambient.b, material->ambient.a);
2229 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2230 material->specular.r, material->specular.g,
2231 material->specular.b, material->specular.a);
2232 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2233 material->emissive.r, material->emissive.g,
2234 material->emissive.b, material->emissive.a);
2235 TRACE("power %.8e.\n", material->power);
2238 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2239 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2241 struct wined3d_buffer *prev_buffer;
2243 TRACE("device %p, buffer %p, format %s.\n",
2244 device, buffer, debug_d3dformat(format_id));
2246 prev_buffer = device->updateStateBlock->state.index_buffer;
2248 device->updateStateBlock->changed.indices = TRUE;
2249 device->updateStateBlock->state.index_buffer = buffer;
2250 device->updateStateBlock->state.index_format = format_id;
2252 /* Handle recording of state blocks. */
2253 if (device->isRecordingState)
2255 TRACE("Recording... not performing anything.\n");
2257 wined3d_buffer_incref(buffer);
2259 wined3d_buffer_decref(prev_buffer);
2263 if (prev_buffer != buffer)
2265 device_invalidate_state(device, STATE_INDEXBUFFER);
2268 InterlockedIncrement(&buffer->resource.bind_count);
2269 wined3d_buffer_incref(buffer);
2273 InterlockedDecrement(&prev_buffer->resource.bind_count);
2274 wined3d_buffer_decref(prev_buffer);
2279 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
2280 enum wined3d_format_id *format)
2282 TRACE("device %p, format %p.\n", device, format);
2284 *format = device->stateBlock->state.index_format;
2285 return device->stateBlock->state.index_buffer;
2288 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2290 TRACE("device %p, base_index %d.\n", device, base_index);
2292 device->updateStateBlock->state.base_vertex_index = base_index;
2295 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2297 TRACE("device %p.\n", device);
2299 return device->stateBlock->state.base_vertex_index;
2302 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2304 TRACE("device %p, viewport %p.\n", device, viewport);
2305 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2306 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2308 device->updateStateBlock->changed.viewport = TRUE;
2309 device->updateStateBlock->state.viewport = *viewport;
2311 /* Handle recording of state blocks */
2312 if (device->isRecordingState)
2314 TRACE("Recording... not performing anything\n");
2318 device_invalidate_state(device, STATE_VIEWPORT);
2321 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2323 TRACE("device %p, viewport %p.\n", device, viewport);
2325 *viewport = device->stateBlock->state.viewport;
2328 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2329 enum wined3d_render_state state, DWORD value)
2331 DWORD old_value = device->stateBlock->state.render_states[state];
2333 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2335 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2336 device->updateStateBlock->state.render_states[state] = value;
2338 /* Handle recording of state blocks. */
2339 if (device->isRecordingState)
2341 TRACE("Recording... not performing anything.\n");
2345 /* Compared here and not before the assignment to allow proper stateblock recording. */
2346 if (value == old_value)
2347 TRACE("Application is setting the old value over, nothing to do.\n");
2349 device_invalidate_state(device, STATE_RENDER(state));
2352 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2354 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2356 return device->stateBlock->state.render_states[state];
2359 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2360 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2364 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2365 device, sampler_idx, debug_d3dsamplerstate(state), value);
2367 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2368 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2370 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2371 / sizeof(*device->stateBlock->state.sampler_states))
2373 WARN("Invalid sampler %u.\n", sampler_idx);
2374 return; /* Windows accepts overflowing this array ... we do not. */
2377 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2378 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2379 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2381 /* Handle recording of state blocks. */
2382 if (device->isRecordingState)
2384 TRACE("Recording... not performing anything.\n");
2388 if (old_value == value)
2390 TRACE("Application is setting the old value over, nothing to do.\n");
2394 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2397 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2398 UINT sampler_idx, enum wined3d_sampler_state state)
2400 TRACE("device %p, sampler_idx %u, state %s.\n",
2401 device, sampler_idx, debug_d3dsamplerstate(state));
2403 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2404 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2406 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2407 / sizeof(*device->stateBlock->state.sampler_states))
2409 WARN("Invalid sampler %u.\n", sampler_idx);
2410 return 0; /* Windows accepts overflowing this array ... we do not. */
2413 return device->stateBlock->state.sampler_states[sampler_idx][state];
2416 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2418 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2420 device->updateStateBlock->changed.scissorRect = TRUE;
2421 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2423 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2426 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2428 if (device->isRecordingState)
2430 TRACE("Recording... not performing anything.\n");
2434 device_invalidate_state(device, STATE_SCISSORRECT);
2437 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2439 TRACE("device %p, rect %p.\n", device, rect);
2441 *rect = device->updateStateBlock->state.scissor_rect;
2442 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2445 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2446 struct wined3d_vertex_declaration *declaration)
2448 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2450 TRACE("device %p, declaration %p.\n", device, declaration);
2453 wined3d_vertex_declaration_incref(declaration);
2455 wined3d_vertex_declaration_decref(prev);
2457 device->updateStateBlock->state.vertex_declaration = declaration;
2458 device->updateStateBlock->changed.vertexDecl = TRUE;
2460 if (device->isRecordingState)
2462 TRACE("Recording... not performing anything.\n");
2466 if (declaration == prev)
2468 /* Checked after the assignment to allow proper stateblock recording. */
2469 TRACE("Application is setting the old declaration over, nothing to do.\n");
2473 device_invalidate_state(device, STATE_VDECL);
2476 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2478 TRACE("device %p.\n", device);
2480 return device->stateBlock->state.vertex_declaration;
2483 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2485 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2487 TRACE("device %p, shader %p.\n", device, shader);
2490 wined3d_shader_incref(shader);
2492 wined3d_shader_decref(prev);
2494 device->updateStateBlock->state.vertex_shader = shader;
2495 device->updateStateBlock->changed.vertexShader = TRUE;
2497 if (device->isRecordingState)
2499 TRACE("Recording... not performing anything.\n");
2505 TRACE("Application is setting the old shader over, nothing to do.\n");
2509 device_invalidate_state(device, STATE_VSHADER);
2512 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2514 TRACE("device %p.\n", device);
2516 return device->stateBlock->state.vertex_shader;
2519 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2521 struct wined3d_buffer *prev;
2523 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2525 if (idx >= MAX_CONSTANT_BUFFERS)
2527 WARN("Invalid constant buffer index %u.\n", idx);
2531 prev = device->updateStateBlock->state.vs_cb[idx];
2532 device->updateStateBlock->state.vs_cb[idx] = buffer;
2534 if (device->isRecordingState)
2537 wined3d_buffer_incref(buffer);
2539 wined3d_buffer_decref(prev);
2547 InterlockedIncrement(&buffer->resource.bind_count);
2548 wined3d_buffer_incref(buffer);
2552 InterlockedDecrement(&prev->resource.bind_count);
2553 wined3d_buffer_decref(prev);
2558 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2560 TRACE("device %p, idx %u.\n", device, idx);
2562 if (idx >= MAX_CONSTANT_BUFFERS)
2564 WARN("Invalid constant buffer index %u.\n", idx);
2568 return device->stateBlock->state.vs_cb[idx];
2571 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2573 struct wined3d_sampler *prev;
2575 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2577 if (idx >= MAX_SAMPLER_OBJECTS)
2579 WARN("Invalid sampler index %u.\n", idx);
2583 prev = device->updateStateBlock->state.vs_sampler[idx];
2584 device->updateStateBlock->state.vs_sampler[idx] = sampler;
2587 wined3d_sampler_incref(sampler);
2589 wined3d_sampler_decref(prev);
2592 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2594 TRACE("device %p, idx %u.\n", device, idx);
2596 if (idx >= MAX_SAMPLER_OBJECTS)
2598 WARN("Invalid sampler index %u.\n", idx);
2602 return device->stateBlock->state.vs_sampler[idx];
2605 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2606 UINT start_register, const BOOL *constants, UINT bool_count)
2608 UINT count = min(bool_count, MAX_CONST_B - start_register);
2611 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2612 device, start_register, constants, bool_count);
2614 if (!constants || start_register >= MAX_CONST_B)
2615 return WINED3DERR_INVALIDCALL;
2617 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2618 for (i = 0; i < count; ++i)
2619 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2621 for (i = start_register; i < count + start_register; ++i)
2622 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2624 if (!device->isRecordingState)
2625 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2630 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2631 UINT start_register, BOOL *constants, UINT bool_count)
2633 UINT count = min(bool_count, MAX_CONST_B - start_register);
2635 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2636 device, start_register, constants, bool_count);
2638 if (!constants || start_register >= MAX_CONST_B)
2639 return WINED3DERR_INVALIDCALL;
2641 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2646 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2647 UINT start_register, const int *constants, UINT vector4i_count)
2649 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2652 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2653 device, start_register, constants, vector4i_count);
2655 if (!constants || start_register >= MAX_CONST_I)
2656 return WINED3DERR_INVALIDCALL;
2658 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2659 for (i = 0; i < count; ++i)
2660 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2661 constants[i * 4], constants[i * 4 + 1],
2662 constants[i * 4 + 2], constants[i * 4 + 3]);
2664 for (i = start_register; i < count + start_register; ++i)
2665 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2667 if (!device->isRecordingState)
2668 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2673 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2674 UINT start_register, int *constants, UINT vector4i_count)
2676 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2678 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2679 device, start_register, constants, vector4i_count);
2681 if (!constants || start_register >= MAX_CONST_I)
2682 return WINED3DERR_INVALIDCALL;
2684 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2688 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2689 UINT start_register, const float *constants, UINT vector4f_count)
2693 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2694 device, start_register, constants, vector4f_count);
2696 /* Specifically test start_register > limit to catch MAX_UINT overflows
2697 * when adding start_register + vector4f_count. */
2699 || start_register + vector4f_count > device->d3d_vshader_constantF
2700 || start_register > device->d3d_vshader_constantF)
2701 return WINED3DERR_INVALIDCALL;
2703 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2704 constants, vector4f_count * sizeof(float) * 4);
2707 for (i = 0; i < vector4f_count; ++i)
2708 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2709 constants[i * 4], constants[i * 4 + 1],
2710 constants[i * 4 + 2], constants[i * 4 + 3]);
2713 if (!device->isRecordingState)
2715 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2716 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2719 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2720 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2725 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2726 UINT start_register, float *constants, UINT vector4f_count)
2728 int count = min(vector4f_count, device->d3d_vshader_constantF - start_register);
2730 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2731 device, start_register, constants, vector4f_count);
2733 if (!constants || count < 0)
2734 return WINED3DERR_INVALIDCALL;
2736 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2741 static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2745 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2747 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2751 static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2753 DWORD i = device->rev_tex_unit_map[unit];
2754 DWORD j = device->texUnitMap[stage];
2756 device->texUnitMap[stage] = unit;
2757 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2758 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2760 device->rev_tex_unit_map[unit] = stage;
2761 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2762 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2765 static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2769 device->fixed_function_usage_map = 0;
2770 for (i = 0; i < MAX_TEXTURES; ++i)
2772 const struct wined3d_state *state = &device->stateBlock->state;
2773 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2774 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2775 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2776 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2777 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2778 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2779 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2780 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2782 /* Not used, and disable higher stages. */
2783 if (color_op == WINED3D_TOP_DISABLE)
2786 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2787 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2788 || ((color_arg3 == WINED3DTA_TEXTURE)
2789 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2790 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2791 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2792 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2793 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2794 device->fixed_function_usage_map |= (1 << i);
2796 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2797 && i < MAX_TEXTURES - 1)
2798 device->fixed_function_usage_map |= (1 << (i + 1));
2802 static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2804 unsigned int i, tex;
2807 device_update_fixed_function_usage_map(device);
2808 ffu_map = device->fixed_function_usage_map;
2810 if (device->max_ffp_textures == gl_info->limits.texture_stages
2811 || device->stateBlock->state.lowest_disabled_stage <= device->max_ffp_textures)
2813 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2815 if (!(ffu_map & 1)) continue;
2817 if (device->texUnitMap[i] != i)
2819 device_map_stage(device, i, i);
2820 device_invalidate_state(device, STATE_SAMPLER(i));
2821 device_invalidate_texture_stage(device, i);
2827 /* Now work out the mapping */
2829 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2831 if (!(ffu_map & 1)) continue;
2833 if (device->texUnitMap[i] != tex)
2835 device_map_stage(device, i, tex);
2836 device_invalidate_state(device, STATE_SAMPLER(i));
2837 device_invalidate_texture_stage(device, i);
2844 static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
2846 const enum wined3d_sampler_texture_type *sampler_type =
2847 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2850 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2852 if (sampler_type[i] && device->texUnitMap[i] != i)
2854 device_map_stage(device, i, i);
2855 device_invalidate_state(device, STATE_SAMPLER(i));
2856 if (i < gl_info->limits.texture_stages)
2857 device_invalidate_texture_stage(device, i);
2862 static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2863 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2864 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2866 DWORD current_mapping = device->rev_tex_unit_map[unit];
2868 /* Not currently used */
2869 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2871 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2872 /* Used by a fragment sampler */
2874 if (!pshader_sampler_tokens) {
2875 /* No pixel shader, check fixed function */
2876 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2879 /* Pixel shader, check the shader's sampler map */
2880 return !pshader_sampler_tokens[current_mapping];
2883 /* Used by a vertex sampler */
2884 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2887 static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2889 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2890 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2891 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2892 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2897 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2898 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2899 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2902 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2903 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2904 if (vshader_sampler_type[i])
2906 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2908 /* Already mapped somewhere */
2914 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2916 device_map_stage(device, vsampler_idx, start);
2917 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2929 void device_update_tex_unit_map(struct wined3d_device *device)
2931 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2932 const struct wined3d_state *state = &device->stateBlock->state;
2933 BOOL vs = use_vs(state);
2934 BOOL ps = use_ps(state);
2937 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2938 * that would be really messy and require shader recompilation
2939 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2940 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2943 device_map_psamplers(device, gl_info);
2945 device_map_fixed_function_samplers(device, gl_info);
2948 device_map_vsamplers(device, ps, gl_info);
2951 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2953 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2955 TRACE("device %p, shader %p.\n", device, shader);
2958 wined3d_shader_incref(shader);
2960 wined3d_shader_decref(prev);
2962 device->updateStateBlock->state.pixel_shader = shader;
2963 device->updateStateBlock->changed.pixelShader = TRUE;
2965 if (device->isRecordingState)
2967 TRACE("Recording... not performing anything.\n");
2973 TRACE("Application is setting the old shader over, nothing to do.\n");
2977 device_invalidate_state(device, STATE_PIXELSHADER);
2980 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2982 TRACE("device %p.\n", device);
2984 return device->stateBlock->state.pixel_shader;
2987 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2989 struct wined3d_buffer *prev;
2991 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2993 if (idx >= MAX_CONSTANT_BUFFERS)
2995 WARN("Invalid constant buffer index %u.\n", idx);
2999 prev = device->updateStateBlock->state.ps_cb[idx];
3000 device->updateStateBlock->state.ps_cb[idx] = buffer;
3002 if (device->isRecordingState)
3005 wined3d_buffer_incref(buffer);
3007 wined3d_buffer_decref(prev);
3015 InterlockedIncrement(&buffer->resource.bind_count);
3016 wined3d_buffer_incref(buffer);
3020 InterlockedDecrement(&prev->resource.bind_count);
3021 wined3d_buffer_decref(prev);
3026 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
3028 TRACE("device %p, idx %u.\n", device, idx);
3030 if (idx >= MAX_CONSTANT_BUFFERS)
3032 WARN("Invalid constant buffer index %u.\n", idx);
3036 return device->stateBlock->state.ps_cb[idx];
3039 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3041 struct wined3d_sampler *prev;
3043 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3045 if (idx >= MAX_SAMPLER_OBJECTS)
3047 WARN("Invalid sampler index %u.\n", idx);
3051 prev = device->updateStateBlock->state.ps_sampler[idx];
3052 device->updateStateBlock->state.ps_sampler[idx] = sampler;
3055 wined3d_sampler_incref(sampler);
3057 wined3d_sampler_decref(prev);
3060 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
3062 TRACE("device %p, idx %u.\n", device, idx);
3064 if (idx >= MAX_SAMPLER_OBJECTS)
3066 WARN("Invalid sampler index %u.\n", idx);
3070 return device->stateBlock->state.ps_sampler[idx];
3073 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
3074 UINT start_register, const BOOL *constants, UINT bool_count)
3076 UINT count = min(bool_count, MAX_CONST_B - start_register);
3079 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3080 device, start_register, constants, bool_count);
3082 if (!constants || start_register >= MAX_CONST_B)
3083 return WINED3DERR_INVALIDCALL;
3085 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
3086 for (i = 0; i < count; ++i)
3087 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
3089 for (i = start_register; i < count + start_register; ++i)
3090 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
3092 if (!device->isRecordingState)
3093 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3098 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
3099 UINT start_register, BOOL *constants, UINT bool_count)
3101 UINT count = min(bool_count, MAX_CONST_B - start_register);
3103 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3104 device, start_register, constants, bool_count);
3106 if (!constants || start_register >= MAX_CONST_B)
3107 return WINED3DERR_INVALIDCALL;
3109 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
3114 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
3115 UINT start_register, const int *constants, UINT vector4i_count)
3117 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3120 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3121 device, start_register, constants, vector4i_count);
3123 if (!constants || start_register >= MAX_CONST_I)
3124 return WINED3DERR_INVALIDCALL;
3126 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
3127 for (i = 0; i < count; ++i)
3128 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
3129 constants[i * 4], constants[i * 4 + 1],
3130 constants[i * 4 + 2], constants[i * 4 + 3]);
3132 for (i = start_register; i < count + start_register; ++i)
3133 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
3135 if (!device->isRecordingState)
3136 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3141 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
3142 UINT start_register, int *constants, UINT vector4i_count)
3144 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3146 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3147 device, start_register, constants, vector4i_count);
3149 if (!constants || start_register >= MAX_CONST_I)
3150 return WINED3DERR_INVALIDCALL;
3152 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
3157 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
3158 UINT start_register, const float *constants, UINT vector4f_count)
3162 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3163 device, start_register, constants, vector4f_count);
3165 /* Specifically test start_register > limit to catch MAX_UINT overflows
3166 * when adding start_register + vector4f_count. */
3168 || start_register + vector4f_count > device->d3d_pshader_constantF
3169 || start_register > device->d3d_pshader_constantF)
3170 return WINED3DERR_INVALIDCALL;
3172 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
3173 constants, vector4f_count * sizeof(float) * 4);
3176 for (i = 0; i < vector4f_count; ++i)
3177 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
3178 constants[i * 4], constants[i * 4 + 1],
3179 constants[i * 4 + 2], constants[i * 4 + 3]);
3182 if (!device->isRecordingState)
3184 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
3185 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3188 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
3189 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
3194 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
3195 UINT start_register, float *constants, UINT vector4f_count)
3197 int count = min(vector4f_count, device->d3d_pshader_constantF - start_register);
3199 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3200 device, start_register, constants, vector4f_count);
3202 if (!constants || count < 0)
3203 return WINED3DERR_INVALIDCALL;
3205 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
3210 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
3212 struct wined3d_shader *prev = device->updateStateBlock->state.geometry_shader;
3214 TRACE("device %p, shader %p.\n", device, shader);
3217 wined3d_shader_incref(shader);
3219 wined3d_shader_decref(prev);
3221 device->updateStateBlock->state.geometry_shader = shader;
3223 if (device->isRecordingState || shader == prev)
3226 device_invalidate_state(device, STATE_GEOMETRY_SHADER);
3229 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
3231 TRACE("device %p.\n", device);
3233 return device->stateBlock->state.geometry_shader;
3236 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
3238 struct wined3d_buffer *prev;
3240 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
3242 if (idx >= MAX_CONSTANT_BUFFERS)
3244 WARN("Invalid constant buffer index %u.\n", idx);
3248 prev = device->updateStateBlock->state.gs_cb[idx];
3249 device->updateStateBlock->state.gs_cb[idx] = buffer;
3251 if (device->isRecordingState)
3254 wined3d_buffer_incref(buffer);
3256 wined3d_buffer_decref(prev);
3264 InterlockedIncrement(&buffer->resource.bind_count);
3265 wined3d_buffer_incref(buffer);
3269 InterlockedDecrement(&prev->resource.bind_count);
3270 wined3d_buffer_decref(prev);
3275 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
3277 TRACE("device %p, idx %u.\n", device, idx);
3279 if (idx >= MAX_CONSTANT_BUFFERS)
3281 WARN("Invalid constant buffer index %u.\n", idx);
3285 return device->stateBlock->state.gs_cb[idx];
3288 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3290 struct wined3d_sampler *prev;
3292 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3294 if (idx >= MAX_SAMPLER_OBJECTS)
3296 WARN("Invalid sampler index %u.\n", idx);
3300 prev = device->updateStateBlock->state.gs_sampler[idx];
3301 device->updateStateBlock->state.gs_sampler[idx] = sampler;
3304 wined3d_sampler_incref(sampler);
3306 wined3d_sampler_decref(prev);
3309 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
3311 TRACE("device %p, idx %u.\n", device, idx);
3313 if (idx >= MAX_SAMPLER_OBJECTS)
3315 WARN("Invalid sampler index %u.\n", idx);
3319 return device->stateBlock->state.gs_sampler[idx];
3322 /* Context activation is done by the caller. */
3323 /* Do not call while under the GL lock. */
3324 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3325 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3326 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3329 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3330 struct wined3d_viewport vp;
3338 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3340 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3343 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3345 ERR("Source has no position mask\n");
3346 return WINED3DERR_INVALIDCALL;
3349 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3351 static BOOL warned = FALSE;
3353 * The clipping code is not quite correct. Some things need
3354 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3355 * so disable clipping for now.
3356 * (The graphics in Half-Life are broken, and my processvertices
3357 * test crashes with IDirect3DDevice3)
3363 FIXME("Clipping is broken and disabled for now\n");
3369 vertex_size = get_flexible_vertex_size(DestFVF);
3370 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3372 WARN("Failed to map buffer, hr %#x.\n", hr);
3376 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3377 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3378 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3380 TRACE("View mat:\n");
3381 TRACE("%f %f %f %f\n", view_mat.u.s._11, view_mat.u.s._12, view_mat.u.s._13, view_mat.u.s._14);
3382 TRACE("%f %f %f %f\n", view_mat.u.s._21, view_mat.u.s._22, view_mat.u.s._23, view_mat.u.s._24);
3383 TRACE("%f %f %f %f\n", view_mat.u.s._31, view_mat.u.s._32, view_mat.u.s._33, view_mat.u.s._34);
3384 TRACE("%f %f %f %f\n", view_mat.u.s._41, view_mat.u.s._42, view_mat.u.s._43, view_mat.u.s._44);
3386 TRACE("Proj mat:\n");
3387 TRACE("%f %f %f %f\n", proj_mat.u.s._11, proj_mat.u.s._12, proj_mat.u.s._13, proj_mat.u.s._14);
3388 TRACE("%f %f %f %f\n", proj_mat.u.s._21, proj_mat.u.s._22, proj_mat.u.s._23, proj_mat.u.s._24);
3389 TRACE("%f %f %f %f\n", proj_mat.u.s._31, proj_mat.u.s._32, proj_mat.u.s._33, proj_mat.u.s._34);
3390 TRACE("%f %f %f %f\n", proj_mat.u.s._41, proj_mat.u.s._42, proj_mat.u.s._43, proj_mat.u.s._44);
3392 TRACE("World mat:\n");
3393 TRACE("%f %f %f %f\n", world_mat.u.s._11, world_mat.u.s._12, world_mat.u.s._13, world_mat.u.s._14);
3394 TRACE("%f %f %f %f\n", world_mat.u.s._21, world_mat.u.s._22, world_mat.u.s._23, world_mat.u.s._24);
3395 TRACE("%f %f %f %f\n", world_mat.u.s._31, world_mat.u.s._32, world_mat.u.s._33, world_mat.u.s._34);
3396 TRACE("%f %f %f %f\n", world_mat.u.s._41, world_mat.u.s._42, world_mat.u.s._43, world_mat.u.s._44);
3398 /* Get the viewport */
3399 wined3d_device_get_viewport(device, &vp);
3400 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3401 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3403 multiply_matrix(&mat,&view_mat,&world_mat);
3404 multiply_matrix(&mat,&proj_mat,&mat);
3406 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3408 for (i = 0; i < dwCount; i+= 1) {
3409 unsigned int tex_index;
3411 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3412 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3413 /* The position first */
3414 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3415 const float *p = (const float *)(element->data.addr + i * element->stride);
3417 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3419 /* Multiplication with world, view and projection matrix */
3420 x = (p[0] * mat.u.s._11) + (p[1] * mat.u.s._21) + (p[2] * mat.u.s._31) + (1.0f * mat.u.s._41);
3421 y = (p[0] * mat.u.s._12) + (p[1] * mat.u.s._22) + (p[2] * mat.u.s._32) + (1.0f * mat.u.s._42);
3422 z = (p[0] * mat.u.s._13) + (p[1] * mat.u.s._23) + (p[2] * mat.u.s._33) + (1.0f * mat.u.s._43);
3423 rhw = (p[0] * mat.u.s._14) + (p[1] * mat.u.s._24) + (p[2] * mat.u.s._34) + (1.0f * mat.u.s._44);
3425 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3427 /* WARNING: The following things are taken from d3d7 and were not yet checked
3428 * against d3d8 or d3d9!
3431 /* Clipping conditions: From msdn
3433 * A vertex is clipped if it does not match the following requirements
3437 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3439 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3440 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3445 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3446 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3449 /* "Normal" viewport transformation (not clipped)
3450 * 1) The values are divided by rhw
3451 * 2) The y axis is negative, so multiply it with -1
3452 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3453 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3454 * 4) Multiply x with Width/2 and add Width/2
3455 * 5) The same for the height
3456 * 6) Add the viewpoint X and Y to the 2D coordinates and
3457 * The minimum Z value to z
3458 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3460 * Well, basically it's simply a linear transformation into viewport
3472 z *= vp.max_z - vp.min_z;
3474 x += vp.width / 2 + vp.x;
3475 y += vp.height / 2 + vp.y;
3480 /* That vertex got clipped
3481 * Contrary to OpenGL it is not dropped completely, it just
3482 * undergoes a different calculation.
3484 TRACE("Vertex got clipped\n");
3491 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3492 * outside of the main vertex buffer memory. That needs some more
3497 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3500 ( (float *) dest_ptr)[0] = x;
3501 ( (float *) dest_ptr)[1] = y;
3502 ( (float *) dest_ptr)[2] = z;
3503 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3505 dest_ptr += 3 * sizeof(float);
3507 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3508 dest_ptr += sizeof(float);
3511 if (DestFVF & WINED3DFVF_PSIZE)
3512 dest_ptr += sizeof(DWORD);
3514 if (DestFVF & WINED3DFVF_NORMAL)
3516 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3517 const float *normal = (const float *)(element->data.addr + i * element->stride);
3518 /* AFAIK this should go into the lighting information */
3519 FIXME("Didn't expect the destination to have a normal\n");
3520 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3523 if (DestFVF & WINED3DFVF_DIFFUSE)
3525 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3526 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3527 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3529 static BOOL warned = FALSE;
3532 ERR("No diffuse color in source, but destination has one\n");
3536 *( (DWORD *) dest_ptr) = 0xffffffff;
3537 dest_ptr += sizeof(DWORD);
3541 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3545 if (DestFVF & WINED3DFVF_SPECULAR)
3547 /* What's the color value in the feedback buffer? */
3548 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3549 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3550 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3552 static BOOL warned = FALSE;
3555 ERR("No specular color in source, but destination has one\n");
3559 *(DWORD *)dest_ptr = 0xff000000;
3560 dest_ptr += sizeof(DWORD);
3564 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3568 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3570 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3571 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3572 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3574 ERR("No source texture, but destination requests one\n");
3575 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3579 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3584 wined3d_buffer_unmap(dest);
3588 #undef copy_and_next
3590 /* Do not call while under the GL lock. */
3591 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3592 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3593 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3595 struct wined3d_state *state = &device->stateBlock->state;
3596 struct wined3d_stream_info stream_info;
3597 const struct wined3d_gl_info *gl_info;
3598 BOOL streamWasUP = state->user_stream;
3599 struct wined3d_context *context;
3600 struct wined3d_shader *vs;
3604 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3605 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3606 device, src_start_idx, dst_idx, vertex_count,
3607 dst_buffer, declaration, flags, dst_fvf);
3610 FIXME("Output vertex declaration not implemented yet.\n");
3612 /* Need any context to write to the vbo. */
3613 context = context_acquire(device, NULL);
3614 gl_info = context->gl_info;
3616 /* ProcessVertices reads from vertex buffers, which have to be assigned.
3617 * DrawPrimitive and DrawPrimitiveUP control the streamIsUP flag, thus
3618 * restore it afterwards. */
3619 vs = state->vertex_shader;
3620 state->vertex_shader = NULL;
3621 state->user_stream = FALSE;
3622 device_stream_info_from_declaration(device, &stream_info);
3623 state->user_stream = streamWasUP;
3624 state->vertex_shader = vs;
3626 /* We can't convert FROM a VBO, and vertex buffers used to source into
3627 * process_vertices() are unlikely to ever be used for drawing. Release
3628 * VBOs in those buffers and fix up the stream_info structure.
3630 * Also apply the start index. */
3631 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3633 struct wined3d_stream_info_element *e;
3635 if (!(stream_info.use_map & (1 << i)))
3638 e = &stream_info.elements[i];
3639 if (e->data.buffer_object)
3641 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3642 e->data.buffer_object = 0;
3643 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3644 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3645 vb->buffer_object = 0;
3648 e->data.addr += e->stride * src_start_idx;
3651 hr = process_vertices_strided(device, dst_idx, vertex_count,
3652 &stream_info, dst_buffer, flags, dst_fvf);
3654 context_release(context);
3659 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3660 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3662 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3665 TRACE("device %p, stage %u, state %s, value %#x.\n",
3666 device, stage, debug_d3dtexturestate(state), value);
3668 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3670 WARN("Invalid state %#x passed.\n", state);
3674 if (stage >= gl_info->limits.texture_stages)
3676 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3677 stage, gl_info->limits.texture_stages - 1);
3681 old_value = device->updateStateBlock->state.texture_states[stage][state];
3682 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3683 device->updateStateBlock->state.texture_states[stage][state] = value;
3685 if (device->isRecordingState)
3687 TRACE("Recording... not performing anything.\n");
3691 /* Checked after the assignments to allow proper stateblock recording. */
3692 if (old_value == value)
3694 TRACE("Application is setting the old value over, nothing to do.\n");
3698 if (stage > device->stateBlock->state.lowest_disabled_stage
3699 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3700 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3702 /* Colorop change above lowest disabled stage? That won't change
3703 * anything in the GL setup. Changes in other states are important on
3704 * disabled stages too. */
3708 if (state == WINED3D_TSS_COLOR_OP)
3712 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3714 /* Previously enabled stage disabled now. Make sure to dirtify
3715 * all enabled stages above stage, they have to be disabled.
3717 * The current stage is dirtified below. */
3718 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3720 TRACE("Additionally dirtifying stage %u.\n", i);
3721 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3723 device->stateBlock->state.lowest_disabled_stage = stage;
3724 TRACE("New lowest disabled: %u.\n", stage);
3726 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3728 /* Previously disabled stage enabled. Stages above it may need
3729 * enabling. Stage must be lowest_disabled_stage here, if it's
3730 * bigger success is returned above, and stages below the lowest
3731 * disabled stage can't be enabled (because they are enabled
3734 * Again stage stage doesn't need to be dirtified here, it is
3736 for (i = stage + 1; i < gl_info->limits.texture_stages; ++i)
3738 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3740 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3741 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3743 device->stateBlock->state.lowest_disabled_stage = i;
3744 TRACE("New lowest disabled: %u.\n", i);
3748 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3751 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3752 UINT stage, enum wined3d_texture_stage_state state)
3754 TRACE("device %p, stage %u, state %s.\n",
3755 device, stage, debug_d3dtexturestate(state));
3757 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3759 WARN("Invalid state %#x passed.\n", state);
3763 return device->updateStateBlock->state.texture_states[stage][state];
3766 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3767 UINT stage, struct wined3d_texture *texture)
3769 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3770 struct wined3d_texture *prev;
3772 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3774 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3775 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3777 /* Windows accepts overflowing this array... we do not. */
3778 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3780 WARN("Ignoring invalid stage %u.\n", stage);
3784 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3786 WARN("Rejecting attempt to set scratch texture.\n");
3787 return WINED3DERR_INVALIDCALL;
3790 device->updateStateBlock->changed.textures |= 1 << stage;
3792 prev = device->updateStateBlock->state.textures[stage];
3793 TRACE("Previous texture %p.\n", prev);
3795 if (texture == prev)
3797 TRACE("App is setting the same texture again, nothing to do.\n");
3801 TRACE("Setting new texture to %p.\n", texture);
3802 device->updateStateBlock->state.textures[stage] = texture;
3804 if (device->isRecordingState)
3806 TRACE("Recording... not performing anything\n");
3808 if (texture) wined3d_texture_incref(texture);
3809 if (prev) wined3d_texture_decref(prev);
3816 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3818 wined3d_texture_incref(texture);
3820 if (!prev || texture->target != prev->target)
3821 device_invalidate_state(device, STATE_PIXELSHADER);
3823 if (!prev && stage < gl_info->limits.texture_stages)
3825 /* The source arguments for color and alpha ops have different
3826 * meanings when a NULL texture is bound, so the COLOR_OP and
3827 * ALPHA_OP have to be dirtified. */
3828 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3829 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3832 if (bind_count == 1)
3833 texture->sampler = stage;
3838 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3840 if (!texture && stage < gl_info->limits.texture_stages)
3842 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3843 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3846 if (bind_count && prev->sampler == stage)
3850 /* Search for other stages the texture is bound to. Shouldn't
3851 * happen if applications bind textures to a single stage only. */
3852 TRACE("Searching for other stages the texture is bound to.\n");
3853 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3855 if (device->updateStateBlock->state.textures[i] == prev)
3857 TRACE("Texture is also bound to stage %u.\n", i);
3864 wined3d_texture_decref(prev);
3867 device_invalidate_state(device, STATE_SAMPLER(stage));
3872 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3874 TRACE("device %p, stage %u.\n", device, stage);
3876 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3877 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3879 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3881 WARN("Ignoring invalid stage %u.\n", stage);
3882 return NULL; /* Windows accepts overflowing this array ... we do not. */
3885 return device->stateBlock->state.textures[stage];
3888 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3889 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3891 struct wined3d_swapchain *swapchain;
3893 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3894 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3896 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3897 return WINED3DERR_INVALIDCALL;
3899 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3900 return WINED3DERR_INVALIDCALL;
3904 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3906 TRACE("device %p, caps %p.\n", device, caps);
3908 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3909 device->create_parms.device_type, caps);
3912 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3913 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3915 struct wined3d_swapchain *swapchain;
3917 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3918 device, swapchain_idx, mode, rotation);
3920 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3921 return WINED3DERR_INVALIDCALL;
3923 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3926 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3928 struct wined3d_stateblock *stateblock;
3931 TRACE("device %p.\n", device);
3933 if (device->isRecordingState)
3934 return WINED3DERR_INVALIDCALL;
3936 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3940 wined3d_stateblock_decref(device->updateStateBlock);
3941 device->updateStateBlock = stateblock;
3942 device->isRecordingState = TRUE;
3944 TRACE("Recording stateblock %p.\n", stateblock);
3949 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3950 struct wined3d_stateblock **stateblock)
3952 struct wined3d_stateblock *object = device->updateStateBlock;
3954 TRACE("device %p, stateblock %p.\n", device, stateblock);
3956 if (!device->isRecordingState)
3958 WARN("Not recording.\n");
3960 return WINED3DERR_INVALIDCALL;
3963 stateblock_init_contained_states(object);
3965 *stateblock = object;
3966 device->isRecordingState = FALSE;
3967 device->updateStateBlock = device->stateBlock;
3968 wined3d_stateblock_incref(device->updateStateBlock);
3970 TRACE("Returning stateblock %p.\n", *stateblock);
3975 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3977 /* At the moment we have no need for any functionality at the beginning
3979 TRACE("device %p.\n", device);
3981 if (device->inScene)
3983 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3984 return WINED3DERR_INVALIDCALL;
3986 device->inScene = TRUE;
3990 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3992 struct wined3d_context *context;
3994 TRACE("device %p.\n", device);
3996 if (!device->inScene)
3998 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3999 return WINED3DERR_INVALIDCALL;
4002 context = context_acquire(device, NULL);
4003 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
4004 context->gl_info->gl_ops.gl.p_glFlush();
4005 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
4007 context_release(context);
4009 device->inScene = FALSE;
4013 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
4014 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
4018 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
4019 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
4020 dst_window_override, dirty_region, flags);
4022 for (i = 0; i < device->swapchain_count; ++i)
4024 wined3d_swapchain_present(device->swapchains[i], src_rect,
4025 dst_rect, dst_window_override, dirty_region, flags);
4031 /* Do not call while under the GL lock. */
4032 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
4033 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
4037 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
4038 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
4040 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
4042 struct wined3d_surface *ds = device->fb.depth_stencil;
4045 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
4046 /* TODO: What about depth stencil buffers without stencil bits? */
4047 return WINED3DERR_INVALIDCALL;
4049 else if (flags & WINED3DCLEAR_TARGET)
4051 if (ds->resource.width < device->fb.render_targets[0]->resource.width
4052 || ds->resource.height < device->fb.render_targets[0]->resource.height)
4054 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
4060 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
4061 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
4062 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
4067 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
4068 enum wined3d_primitive_type primitive_type)
4070 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
4072 device->updateStateBlock->changed.primitive_type = TRUE;
4073 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4076 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
4077 enum wined3d_primitive_type *primitive_type)
4079 TRACE("device %p, primitive_type %p\n", device, primitive_type);
4081 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
4083 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
4086 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4088 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4090 if (!device->stateBlock->state.vertex_declaration)
4092 WARN("Called without a valid vertex declaration set.\n");
4093 return WINED3DERR_INVALIDCALL;
4096 /* The index buffer is not needed here, but restore it, otherwise it is hell to keep track of */
4097 if (device->stateBlock->state.user_stream)
4099 device_invalidate_state(device, STATE_INDEXBUFFER);
4100 device->stateBlock->state.user_stream = FALSE;
4103 if (device->stateBlock->state.load_base_vertex_index)
4105 device->stateBlock->state.load_base_vertex_index = 0;
4106 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4109 /* Account for the loading offset due to index buffers. Instead of
4110 * reloading all sources correct it with the startvertex parameter. */
4111 draw_primitive(device, start_vertex, vertex_count, 0, 0, FALSE, NULL);
4115 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4117 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4119 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4121 if (!device->stateBlock->state.index_buffer)
4123 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4124 * without an index buffer set. (The first time at least...)
4125 * D3D8 simply dies, but I doubt it can do much harm to return
4126 * D3DERR_INVALIDCALL there as well. */
4127 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4128 return WINED3DERR_INVALIDCALL;
4131 if (!device->stateBlock->state.vertex_declaration)
4133 WARN("Called without a valid vertex declaration set.\n");
4134 return WINED3DERR_INVALIDCALL;
4137 if (device->stateBlock->state.user_stream)
4139 device_invalidate_state(device, STATE_INDEXBUFFER);
4140 device->stateBlock->state.user_stream = FALSE;
4143 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
4144 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
4146 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
4147 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4150 draw_primitive(device, start_idx, index_count, 0, 0, TRUE, NULL);
4155 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4156 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4158 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4160 draw_primitive(device, start_idx, index_count, start_instance, instance_count, TRUE, NULL);
4163 HRESULT CDECL wined3d_device_draw_primitive_up(struct wined3d_device *device, UINT vertex_count,
4164 const void *stream_data, UINT stream_stride)
4166 struct wined3d_stream_state *stream;
4167 struct wined3d_buffer *vb;
4169 TRACE("device %p, vertex count %u, stream_data %p, stream_stride %u.\n",
4170 device, vertex_count, stream_data, stream_stride);
4172 if (!device->stateBlock->state.vertex_declaration)
4174 WARN("Called without a valid vertex declaration set.\n");
4175 return WINED3DERR_INVALIDCALL;
4178 /* Note in the following, it's not this type, but that's the purpose of streamIsUP */
4179 stream = &device->stateBlock->state.streams[0];
4180 vb = stream->buffer;
4181 stream->buffer = (struct wined3d_buffer *)stream_data;
4183 wined3d_buffer_decref(vb);
4185 stream->stride = stream_stride;
4186 device->stateBlock->state.user_stream = TRUE;
4187 if (device->stateBlock->state.load_base_vertex_index)
4189 device->stateBlock->state.load_base_vertex_index = 0;
4190 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4193 /* TODO: Only mark dirty if drawing from a different UP address */
4194 device_invalidate_state(device, STATE_STREAMSRC);
4196 draw_primitive(device, 0, vertex_count, 0, 0, FALSE, NULL);
4198 /* MSDN specifies stream zero settings must be set to NULL */
4199 stream->buffer = NULL;
4202 /* stream zero settings set to null at end, as per the msdn. No need to
4203 * mark dirty here, the app has to set the new stream sources or use UP
4208 HRESULT CDECL wined3d_device_draw_indexed_primitive_up(struct wined3d_device *device,
4209 UINT index_count, const void *index_data, enum wined3d_format_id index_data_format_id,
4210 const void *stream_data, UINT stream_stride)
4212 struct wined3d_stream_state *stream;
4213 struct wined3d_buffer *vb, *ib;
4215 TRACE("device %p, index_count %u, index_data %p, index_data_format %s, stream_data %p, stream_stride %u.\n",
4216 device, index_count, index_data, debug_d3dformat(index_data_format_id), stream_data, stream_stride);
4218 if (!device->stateBlock->state.vertex_declaration)
4220 WARN("(%p) : Called without a valid vertex declaration set\n", device);
4221 return WINED3DERR_INVALIDCALL;
4224 stream = &device->stateBlock->state.streams[0];
4225 vb = stream->buffer;
4226 stream->buffer = (struct wined3d_buffer *)stream_data;
4228 wined3d_buffer_decref(vb);
4230 stream->stride = stream_stride;
4231 device->stateBlock->state.user_stream = TRUE;
4232 device->stateBlock->state.index_format = index_data_format_id;
4234 /* Set to 0 as per MSDN. Do it now due to the stream source loading during draw_primitive(). */
4235 device->stateBlock->state.base_vertex_index = 0;
4236 if (device->stateBlock->state.load_base_vertex_index)
4238 device->stateBlock->state.load_base_vertex_index = 0;
4239 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4241 /* Invalidate the state until we have nicer tracking of the stream source pointers */
4242 device_invalidate_state(device, STATE_STREAMSRC);
4243 device_invalidate_state(device, STATE_INDEXBUFFER);
4245 draw_primitive(device, 0, index_count, 0, 0, TRUE, index_data);
4247 /* MSDN specifies stream zero settings and index buffer must be set to NULL */
4248 stream->buffer = NULL;
4250 ib = device->stateBlock->state.index_buffer;
4253 wined3d_buffer_decref(ib);
4254 device->stateBlock->state.index_buffer = NULL;
4256 /* No need to mark the stream source state dirty here. Either the app calls UP drawing again, or it has to call
4257 * SetStreamSource to specify a vertex buffer
4263 HRESULT CDECL wined3d_device_draw_primitive_strided(struct wined3d_device *device,
4264 UINT vertex_count, const struct wined3d_strided_data *strided_data)
4266 /* Mark the state dirty until we have nicer tracking. It's fine to change
4267 * baseVertexIndex because that call is only called by ddraw which does
4268 * not need that value. */
4269 device_invalidate_state(device, STATE_VDECL);
4270 device_invalidate_state(device, STATE_STREAMSRC);
4271 device_invalidate_state(device, STATE_INDEXBUFFER);
4273 device->stateBlock->state.base_vertex_index = 0;
4274 device->up_strided = strided_data;
4275 draw_primitive(device, 0, vertex_count, 0, 0, FALSE, NULL);
4276 device->up_strided = NULL;
4278 /* Invalidate the states again to make sure the values from the stateblock
4279 * are properly applied in the next regular draw. Note that the application-
4280 * provided strided data has ovwritten pretty much the entire vertex and
4281 * and index stream related states */
4282 device_invalidate_state(device, STATE_VDECL);
4283 device_invalidate_state(device, STATE_STREAMSRC);
4284 device_invalidate_state(device, STATE_INDEXBUFFER);
4288 HRESULT CDECL wined3d_device_draw_indexed_primitive_strided(struct wined3d_device *device,
4289 UINT index_count, const struct wined3d_strided_data *strided_data,
4290 UINT vertex_count, const void *index_data, enum wined3d_format_id index_data_format_id)
4292 enum wined3d_format_id prev_idx_format;
4294 /* Mark the state dirty until we have nicer tracking
4295 * its fine to change baseVertexIndex because that call is only called by ddraw which does not need
4298 device_invalidate_state(device, STATE_VDECL);
4299 device_invalidate_state(device, STATE_STREAMSRC);
4300 device_invalidate_state(device, STATE_INDEXBUFFER);
4302 prev_idx_format = device->stateBlock->state.index_format;
4303 device->stateBlock->state.index_format = index_data_format_id;
4304 device->stateBlock->state.user_stream = TRUE;
4305 device->stateBlock->state.base_vertex_index = 0;
4306 device->up_strided = strided_data;
4307 draw_primitive(device, 0, index_count, 0, 0, TRUE, index_data);
4308 device->up_strided = NULL;
4309 device->stateBlock->state.index_format = prev_idx_format;
4311 device_invalidate_state(device, STATE_VDECL);
4312 device_invalidate_state(device, STATE_STREAMSRC);
4313 device_invalidate_state(device, STATE_INDEXBUFFER);
4317 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4318 static HRESULT device_update_volume(struct wined3d_device *device,
4319 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4321 struct wined3d_map_desc src;
4322 struct wined3d_map_desc dst;
4325 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4326 device, src_volume, dst_volume);
4328 /* TODO: Implement direct loading into the gl volume instead of using
4329 * memcpy and dirtification to improve loading performance. */
4330 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
4332 if (FAILED(hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3D_MAP_DISCARD)))
4334 wined3d_volume_unmap(src_volume);
4338 memcpy(dst.data, src.data, dst_volume->resource.size);
4340 hr = wined3d_volume_unmap(dst_volume);
4342 wined3d_volume_unmap(src_volume);
4344 hr = wined3d_volume_unmap(src_volume);
4349 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4350 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4352 enum wined3d_resource_type type;
4353 unsigned int level_count, i;
4356 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4358 /* Verify that the source and destination textures are non-NULL. */
4359 if (!src_texture || !dst_texture)
4361 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4362 return WINED3DERR_INVALIDCALL;
4365 if (src_texture == dst_texture)
4367 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4368 return WINED3DERR_INVALIDCALL;
4371 /* Verify that the source and destination textures are the same type. */
4372 type = src_texture->resource.type;
4373 if (dst_texture->resource.type != type)
4375 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4376 return WINED3DERR_INVALIDCALL;
4379 /* Check that both textures have the identical numbers of levels. */
4380 level_count = wined3d_texture_get_level_count(src_texture);
4381 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4383 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4384 return WINED3DERR_INVALIDCALL;
4387 /* Make sure that the destination texture is loaded. */
4388 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4390 /* Update every surface level of the texture. */
4393 case WINED3D_RTYPE_TEXTURE:
4395 struct wined3d_surface *src_surface;
4396 struct wined3d_surface *dst_surface;
4398 for (i = 0; i < level_count; ++i)
4400 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4401 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4402 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4405 WARN("Failed to update surface, hr %#x.\n", hr);
4412 case WINED3D_RTYPE_CUBE_TEXTURE:
4414 struct wined3d_surface *src_surface;
4415 struct wined3d_surface *dst_surface;
4417 for (i = 0; i < level_count * 6; ++i)
4419 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4420 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4421 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4424 WARN("Failed to update surface, hr %#x.\n", hr);
4431 case WINED3D_RTYPE_VOLUME_TEXTURE:
4433 for (i = 0; i < level_count; ++i)
4435 hr = device_update_volume(device,
4436 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4437 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4440 WARN("Failed to update volume, hr %#x.\n", hr);
4448 FIXME("Unsupported texture type %#x.\n", type);
4449 return WINED3DERR_INVALIDCALL;
4455 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4456 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4458 struct wined3d_swapchain *swapchain;
4460 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4462 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4463 return WINED3DERR_INVALIDCALL;
4465 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4468 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4470 const struct wined3d_state *state = &device->stateBlock->state;
4471 struct wined3d_texture *texture;
4474 TRACE("device %p, num_passes %p.\n", device, num_passes);
4476 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4478 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4480 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4481 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4483 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4485 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4486 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4489 texture = state->textures[i];
4490 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4492 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4494 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4497 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4499 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4502 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4503 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4505 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4510 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4511 || state->render_states[WINED3D_RS_STENCILENABLE])
4513 struct wined3d_surface *ds = device->fb.depth_stencil;
4514 struct wined3d_surface *target = device->fb.render_targets[0];
4517 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4519 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4520 return WINED3DERR_CONFLICTINGRENDERSTATE;
4524 /* return a sensible default */
4527 TRACE("returning D3D_OK\n");
4531 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4535 TRACE("device %p, software %#x.\n", device, software);
4539 FIXME("device %p, software %#x stub!\n", device, software);
4543 device->softwareVertexProcessing = software;
4546 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4550 TRACE("device %p.\n", device);
4554 TRACE("device %p stub!\n", device);
4558 return device->softwareVertexProcessing;
4561 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4562 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4564 struct wined3d_swapchain *swapchain;
4566 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4567 device, swapchain_idx, raster_status);
4569 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4570 return WINED3DERR_INVALIDCALL;
4572 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4575 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4579 TRACE("device %p, segments %.8e.\n", device, segments);
4581 if (segments != 0.0f)
4585 FIXME("device %p, segments %.8e stub!\n", device, segments);
4593 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4597 TRACE("device %p.\n", device);
4601 FIXME("device %p stub!\n", device);
4608 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4609 struct wined3d_surface *src_surface, const RECT *src_rect,
4610 struct wined3d_surface *dst_surface, const POINT *dst_point)
4612 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4613 device, src_surface, wine_dbgstr_rect(src_rect),
4614 dst_surface, wine_dbgstr_point(dst_point));
4616 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4618 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4619 src_surface, dst_surface);
4620 return WINED3DERR_INVALIDCALL;
4623 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4626 HRESULT CDECL wined3d_device_draw_rect_patch(struct wined3d_device *device, UINT handle,
4627 const float *num_segs, const struct wined3d_rect_patch_info *rect_patch_info)
4629 struct wined3d_rect_patch *patch;
4630 GLenum old_primitive_type;
4635 TRACE("device %p, handle %#x, num_segs %p, rect_patch_info %p.\n",
4636 device, handle, num_segs, rect_patch_info);
4638 if (!(handle || rect_patch_info))
4640 /* TODO: Write a test for the return value, thus the FIXME */
4641 FIXME("Both handle and rect_patch_info are NULL.\n");
4642 return WINED3DERR_INVALIDCALL;
4647 i = PATCHMAP_HASHFUNC(handle);
4649 LIST_FOR_EACH(e, &device->patches[i])
4651 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4652 if (patch->Handle == handle)
4661 TRACE("Patch does not exist. Creating a new one\n");
4662 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4663 patch->Handle = handle;
4664 list_add_head(&device->patches[i], &patch->entry);
4666 TRACE("Found existing patch %p\n", patch);
4671 /* Since opengl does not load tesselated vertex attributes into numbered vertex
4672 * attributes we have to tesselate, read back, and draw. This needs a patch
4673 * management structure instance. Create one.
4675 * A possible improvement is to check if a vertex shader is used, and if not directly
4678 FIXME("Drawing an uncached patch. This is slow\n");
4679 patch = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*patch));
4682 if (num_segs[0] != patch->numSegs[0] || num_segs[1] != patch->numSegs[1]
4683 || num_segs[2] != patch->numSegs[2] || num_segs[3] != patch->numSegs[3]
4684 || (rect_patch_info && memcmp(rect_patch_info, &patch->rect_patch_info, sizeof(*rect_patch_info))))
4687 TRACE("Tesselation density or patch info changed, retesselating\n");
4689 if (rect_patch_info)
4690 patch->rect_patch_info = *rect_patch_info;
4692 patch->numSegs[0] = num_segs[0];
4693 patch->numSegs[1] = num_segs[1];
4694 patch->numSegs[2] = num_segs[2];
4695 patch->numSegs[3] = num_segs[3];
4697 hr = tesselate_rectpatch(device, patch);
4700 WARN("Patch tesselation failed.\n");
4702 /* Do not release the handle to store the params of the patch */
4704 HeapFree(GetProcessHeap(), 0, patch);
4710 old_primitive_type = device->stateBlock->state.gl_primitive_type;
4711 device->stateBlock->state.gl_primitive_type = GL_TRIANGLES;
4712 wined3d_device_draw_primitive_strided(device, patch->numSegs[0] * patch->numSegs[1] * 2 * 3, &patch->strided);
4713 device->stateBlock->state.gl_primitive_type = old_primitive_type;
4715 /* Destroy uncached patches */
4718 HeapFree(GetProcessHeap(), 0, patch->mem);
4719 HeapFree(GetProcessHeap(), 0, patch);
4724 HRESULT CDECL wined3d_device_draw_tri_patch(struct wined3d_device *device, UINT handle,
4725 const float *segment_count, const struct wined3d_tri_patch_info *patch_info)
4727 FIXME("device %p, handle %#x, segment_count %p, patch_info %p stub!\n",
4728 device, handle, segment_count, patch_info);
4733 HRESULT CDECL wined3d_device_delete_patch(struct wined3d_device *device, UINT handle)
4735 struct wined3d_rect_patch *patch;
4739 TRACE("device %p, handle %#x.\n", device, handle);
4741 i = PATCHMAP_HASHFUNC(handle);
4742 LIST_FOR_EACH(e, &device->patches[i])
4744 patch = LIST_ENTRY(e, struct wined3d_rect_patch, entry);
4745 if (patch->Handle == handle)
4747 TRACE("Deleting patch %p\n", patch);
4748 list_remove(&patch->entry);
4749 HeapFree(GetProcessHeap(), 0, patch->mem);
4750 HeapFree(GetProcessHeap(), 0, patch);
4755 /* TODO: Write a test for the return value */
4756 FIXME("Attempt to destroy nonexistent patch\n");
4757 return WINED3DERR_INVALIDCALL;
4760 /* Do not call while under the GL lock. */
4761 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4762 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4766 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4767 device, surface, wine_dbgstr_rect(rect),
4768 color->r, color->g, color->b, color->a);
4770 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4772 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4773 return WINED3DERR_INVALIDCALL;
4778 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4782 return surface_color_fill(surface, rect, color);
4785 /* Do not call while under the GL lock. */
4786 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4787 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4789 struct wined3d_resource *resource;
4793 resource = rendertarget_view->resource;
4794 if (resource->type != WINED3D_RTYPE_SURFACE)
4796 FIXME("Only supported on surface resources\n");
4800 SetRect(&rect, 0, 0, resource->width, resource->height);
4801 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4802 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4805 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4806 UINT render_target_idx)
4808 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
4810 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4812 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4816 return device->fb.render_targets[render_target_idx];
4819 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
4821 TRACE("device %p.\n", device);
4823 return device->fb.depth_stencil;
4826 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4827 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4829 struct wined3d_surface *prev;
4831 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4832 device, render_target_idx, render_target, set_viewport);
4834 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4836 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4837 return WINED3DERR_INVALIDCALL;
4840 /* Render target 0 can't be set to NULL. */
4841 if (!render_target && !render_target_idx)
4843 WARN("Trying to set render target 0 to NULL.\n");
4844 return WINED3DERR_INVALIDCALL;
4847 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4849 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4850 return WINED3DERR_INVALIDCALL;
4853 /* Set the viewport and scissor rectangles, if requested. Tests show that
4854 * stateblock recording is ignored, the change goes directly into the
4855 * primary stateblock. */
4856 if (!render_target_idx && set_viewport)
4858 struct wined3d_state *state = &device->stateBlock->state;
4860 state->viewport.x = 0;
4861 state->viewport.y = 0;
4862 state->viewport.width = render_target->resource.width;
4863 state->viewport.height = render_target->resource.height;
4864 state->viewport.min_z = 0.0f;
4865 state->viewport.max_z = 1.0f;
4866 device_invalidate_state(device, STATE_VIEWPORT);
4868 state->scissor_rect.top = 0;
4869 state->scissor_rect.left = 0;
4870 state->scissor_rect.right = render_target->resource.width;
4871 state->scissor_rect.bottom = render_target->resource.height;
4872 device_invalidate_state(device, STATE_SCISSORRECT);
4876 prev = device->fb.render_targets[render_target_idx];
4877 if (render_target == prev)
4881 wined3d_surface_incref(render_target);
4882 device->fb.render_targets[render_target_idx] = render_target;
4883 /* Release after the assignment, to prevent device_resource_released()
4884 * from seeing the surface as still in use. */
4886 wined3d_surface_decref(prev);
4888 device_invalidate_state(device, STATE_FRAMEBUFFER);
4893 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4895 struct wined3d_surface *prev = device->fb.depth_stencil;
4897 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4898 device, depth_stencil, prev);
4900 if (prev == depth_stencil)
4902 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4908 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4909 || prev->flags & SFLAG_DISCARD)
4911 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4912 prev->resource.width, prev->resource.height);
4913 if (prev == device->onscreen_depth_stencil)
4915 wined3d_surface_decref(device->onscreen_depth_stencil);
4916 device->onscreen_depth_stencil = NULL;
4921 device->fb.depth_stencil = depth_stencil;
4923 wined3d_surface_incref(depth_stencil);
4925 if (!prev != !depth_stencil)
4927 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4928 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4929 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4930 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4931 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4933 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4935 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4938 wined3d_surface_decref(prev);
4940 device_invalidate_state(device, STATE_FRAMEBUFFER);
4945 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4946 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4948 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4949 device, x_hotspot, y_hotspot, cursor_image);
4951 /* some basic validation checks */
4952 if (device->cursorTexture)
4954 struct wined3d_context *context = context_acquire(device, NULL);
4955 context->gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4956 context_release(context);
4957 device->cursorTexture = 0;
4962 struct wined3d_display_mode mode;
4963 struct wined3d_map_desc map_desc;
4966 /* MSDN: Cursor must be A8R8G8B8 */
4967 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4969 WARN("surface %p has an invalid format.\n", cursor_image);
4970 return WINED3DERR_INVALIDCALL;
4973 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4975 ERR("Failed to get display mode, hr %#x.\n", hr);
4976 return WINED3DERR_INVALIDCALL;
4979 /* MSDN: Cursor must be smaller than the display mode */
4980 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4982 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4983 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4984 mode.width, mode.height);
4985 return WINED3DERR_INVALIDCALL;
4988 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4990 /* Do not store the surface's pointer because the application may
4991 * release it after setting the cursor image. Windows doesn't
4992 * addref the set surface, so we can't do this either without
4993 * creating circular refcount dependencies. Copy out the gl texture
4995 device->cursorWidth = cursor_image->resource.width;
4996 device->cursorHeight = cursor_image->resource.height;
4997 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4999 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5000 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
5001 struct wined3d_context *context;
5002 char *mem, *bits = map_desc.data;
5003 GLint intfmt = format->glInternal;
5004 GLint gl_format = format->glFormat;
5005 GLint type = format->glType;
5006 INT height = device->cursorHeight;
5007 INT width = device->cursorWidth;
5008 INT bpp = format->byte_count;
5011 /* Reformat the texture memory (pitch and width can be
5013 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
5014 for (i = 0; i < height; ++i)
5015 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
5016 wined3d_surface_unmap(cursor_image);
5018 context = context_acquire(device, NULL);
5020 if (gl_info->supported[APPLE_CLIENT_STORAGE])
5022 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
5023 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
5026 invalidate_active_texture(device, context);
5027 /* Create a new cursor texture */
5028 gl_info->gl_ops.gl.p_glGenTextures(1, &device->cursorTexture);
5029 checkGLcall("glGenTextures");
5030 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
5031 /* Copy the bitmap memory into the cursor texture */
5032 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
5033 checkGLcall("glTexImage2D");
5034 HeapFree(GetProcessHeap(), 0, mem);
5036 if (gl_info->supported[APPLE_CLIENT_STORAGE])
5038 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
5039 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
5042 context_release(context);
5046 FIXME("A cursor texture was not returned.\n");
5047 device->cursorTexture = 0;
5050 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
5052 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
5053 ICONINFO cursorInfo;
5057 /* 32-bit user32 cursors ignore the alpha channel if it's all
5058 * zeroes, and use the mask instead. Fill the mask with all ones
5059 * to ensure we still get a fully transparent cursor. */
5060 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
5061 memset(maskBits, 0xff, mask_size);
5062 wined3d_surface_map(cursor_image, &map_desc, NULL,
5063 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
5064 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
5066 cursorInfo.fIcon = FALSE;
5067 cursorInfo.xHotspot = x_hotspot;
5068 cursorInfo.yHotspot = y_hotspot;
5069 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
5071 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
5072 1, 32, map_desc.data);
5073 wined3d_surface_unmap(cursor_image);
5074 /* Create our cursor and clean up. */
5075 cursor = CreateIconIndirect(&cursorInfo);
5076 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
5077 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
5078 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
5079 device->hardwareCursor = cursor;
5080 if (device->bCursorVisible) SetCursor( cursor );
5081 HeapFree(GetProcessHeap(), 0, maskBits);
5085 device->xHotSpot = x_hotspot;
5086 device->yHotSpot = y_hotspot;
5090 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
5091 int x_screen_space, int y_screen_space, DWORD flags)
5093 TRACE("device %p, x %d, y %d, flags %#x.\n",
5094 device, x_screen_space, y_screen_space, flags);
5096 device->xScreenSpace = x_screen_space;
5097 device->yScreenSpace = y_screen_space;
5099 if (device->hardwareCursor)
5103 GetCursorPos( &pt );
5104 if (x_screen_space == pt.x && y_screen_space == pt.y)
5106 SetCursorPos( x_screen_space, y_screen_space );
5108 /* Switch to the software cursor if position diverges from the hardware one. */
5109 GetCursorPos( &pt );
5110 if (x_screen_space != pt.x || y_screen_space != pt.y)
5112 if (device->bCursorVisible) SetCursor( NULL );
5113 DestroyCursor( device->hardwareCursor );
5114 device->hardwareCursor = 0;
5119 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
5121 BOOL oldVisible = device->bCursorVisible;
5123 TRACE("device %p, show %#x.\n", device, show);
5126 * When ShowCursor is first called it should make the cursor appear at the OS's last
5127 * known cursor position.
5129 if (show && !oldVisible)
5133 device->xScreenSpace = pt.x;
5134 device->yScreenSpace = pt.y;
5137 if (device->hardwareCursor)
5139 device->bCursorVisible = show;
5141 SetCursor(device->hardwareCursor);
5147 if (device->cursorTexture)
5148 device->bCursorVisible = show;
5154 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
5156 struct wined3d_resource *resource, *cursor;
5158 TRACE("device %p.\n", device);
5160 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5162 TRACE("Checking resource %p for eviction.\n", resource);
5164 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
5166 TRACE("Evicting %p.\n", resource);
5167 resource->resource_ops->resource_unload(resource);
5171 /* Invalidate stream sources, the buffer(s) may have been evicted. */
5172 device_invalidate_state(device, STATE_STREAMSRC);
5175 /* Do not call while under the GL lock. */
5176 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5178 struct wined3d_resource *resource, *cursor;
5179 const struct wined3d_gl_info *gl_info;
5180 struct wined3d_context *context;
5181 struct wined3d_shader *shader;
5183 context = context_acquire(device, NULL);
5184 gl_info = context->gl_info;
5186 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5188 TRACE("Unloading resource %p.\n", resource);
5190 resource->resource_ops->resource_unload(resource);
5193 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
5195 device->shader_backend->shader_destroy(shader);
5198 if (device->depth_blt_texture)
5200 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
5201 device->depth_blt_texture = 0;
5203 if (device->cursorTexture)
5205 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
5206 device->cursorTexture = 0;
5209 device->blitter->free_private(device);
5210 device->shader_backend->shader_free_private(device);
5211 destroy_dummy_textures(device, gl_info);
5213 context_release(context);
5215 while (device->context_count)
5217 swapchain_destroy_contexts(device->contexts[0]->swapchain);
5220 HeapFree(GetProcessHeap(), 0, swapchain->context);
5221 swapchain->context = NULL;
5224 /* Do not call while under the GL lock. */
5225 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
5227 struct wined3d_context *context;
5228 struct wined3d_surface *target;
5231 if (FAILED(hr = device->shader_backend->shader_alloc_private(device, device->adapter->fragment_pipe)))
5233 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
5237 if (FAILED(hr = device->blitter->alloc_private(device)))
5239 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
5240 device->shader_backend->shader_free_private(device);
5244 /* Recreate the primary swapchain's context */
5245 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
5246 if (!swapchain->context)
5248 ERR("Failed to allocate memory for swapchain context array.\n");
5249 device->blitter->free_private(device);
5250 device->shader_backend->shader_free_private(device);
5251 return E_OUTOFMEMORY;
5254 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
5255 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
5257 WARN("Failed to create context.\n");
5258 device->blitter->free_private(device);
5259 device->shader_backend->shader_free_private(device);
5260 HeapFree(GetProcessHeap(), 0, swapchain->context);
5264 swapchain->context[0] = context;
5265 swapchain->num_contexts = 1;
5266 create_dummy_textures(device, context);
5267 context_release(context);
5272 /* Do not call while under the GL lock. */
5273 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
5274 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
5275 wined3d_device_reset_cb callback, BOOL reset_state)
5277 struct wined3d_resource *resource, *cursor;
5278 struct wined3d_swapchain *swapchain;
5279 struct wined3d_display_mode m;
5280 BOOL DisplayModeChanged = FALSE;
5281 BOOL update_desc = FALSE;
5282 HRESULT hr = WINED3D_OK;
5285 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
5287 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
5289 ERR("Failed to get the first implicit swapchain.\n");
5290 return WINED3DERR_INVALIDCALL;
5294 stateblock_unbind_resources(device->stateBlock);
5296 if (device->fb.render_targets)
5298 if (swapchain->back_buffers && swapchain->back_buffers[0])
5299 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
5301 wined3d_device_set_render_target(device, 0, swapchain->front_buffer, FALSE);
5302 for (i = 1; i < device->adapter->gl_info.limits.buffers; ++i)
5304 wined3d_device_set_render_target(device, i, NULL, FALSE);
5307 wined3d_device_set_depth_stencil(device, NULL);
5309 if (device->onscreen_depth_stencil)
5311 wined3d_surface_decref(device->onscreen_depth_stencil);
5312 device->onscreen_depth_stencil = NULL;
5317 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
5319 TRACE("Enumerating resource %p.\n", resource);
5320 if (FAILED(hr = callback(resource)))
5325 /* Is it necessary to recreate the gl context? Actually every setting can be changed
5326 * on an existing gl context, so there's no real need for recreation.
5328 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
5330 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
5332 TRACE("New params:\n");
5333 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
5334 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
5335 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
5336 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
5337 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
5338 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
5339 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
5340 TRACE("device_window %p\n", swapchain_desc->device_window);
5341 TRACE("windowed %#x\n", swapchain_desc->windowed);
5342 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
5343 if (swapchain_desc->enable_auto_depth_stencil)
5344 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
5345 TRACE("flags %#x\n", swapchain_desc->flags);
5346 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
5347 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
5348 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
5350 /* No special treatment of these parameters. Just store them */
5351 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
5352 swapchain->desc.flags = swapchain_desc->flags;
5353 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
5354 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
5356 /* What to do about these? */
5357 if (swapchain_desc->backbuffer_count
5358 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
5359 FIXME("Cannot change the back buffer count yet.\n");
5361 if (swapchain_desc->device_window
5362 && swapchain_desc->device_window != swapchain->desc.device_window)
5364 TRACE("Changing the device window from %p to %p.\n",
5365 swapchain->desc.device_window, swapchain_desc->device_window);
5366 swapchain->desc.device_window = swapchain_desc->device_window;
5367 swapchain->device_window = swapchain_desc->device_window;
5368 wined3d_swapchain_set_window(swapchain, NULL);
5371 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
5373 TRACE("Creating the depth stencil buffer\n");
5375 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
5376 device->device_parent, swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height,
5377 swapchain_desc->auto_depth_stencil_format, WINED3DUSAGE_DEPTHSTENCIL,
5378 swapchain_desc->multisample_type, swapchain_desc->multisample_quality,
5379 &device->auto_depth_stencil)))
5381 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
5382 return WINED3DERR_INVALIDCALL;
5386 /* Reset the depth stencil */
5387 if (swapchain_desc->enable_auto_depth_stencil)
5388 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5392 DisplayModeChanged = TRUE;
5395 else if (swapchain_desc->windowed)
5397 m.width = swapchain->orig_width;
5398 m.height = swapchain->orig_height;
5400 m.format_id = swapchain->desc.backbuffer_format;
5401 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5405 m.width = swapchain_desc->backbuffer_width;
5406 m.height = swapchain_desc->backbuffer_height;
5407 m.refresh_rate = swapchain_desc->refresh_rate;
5408 m.format_id = swapchain_desc->backbuffer_format;
5409 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5412 /* Should Width == 800 && Height == 0 set 800x600? */
5413 if (swapchain_desc->backbuffer_width && swapchain_desc->backbuffer_height
5414 && (swapchain_desc->backbuffer_width != swapchain->desc.backbuffer_width
5415 || swapchain_desc->backbuffer_height != swapchain->desc.backbuffer_height))
5417 if (!swapchain_desc->windowed)
5418 DisplayModeChanged = TRUE;
5420 swapchain->desc.backbuffer_width = swapchain_desc->backbuffer_width;
5421 swapchain->desc.backbuffer_height = swapchain_desc->backbuffer_height;
5425 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5426 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5428 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5432 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5433 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5435 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5436 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5444 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5445 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5446 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5449 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5451 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5452 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5453 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5456 if (device->auto_depth_stencil)
5458 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5459 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5460 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5465 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5466 || DisplayModeChanged)
5468 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
5470 WARN("Failed to set display mode, hr %#x.\n", hr);
5471 return WINED3DERR_INVALIDCALL;
5474 if (!swapchain_desc->windowed)
5476 if (swapchain->desc.windowed)
5478 HWND focus_window = device->create_parms.focus_window;
5480 focus_window = swapchain_desc->device_window;
5481 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5483 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5487 /* switch from windowed to fs */
5488 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5489 swapchain_desc->backbuffer_width,
5490 swapchain_desc->backbuffer_height);
5494 /* Fullscreen -> fullscreen mode change */
5495 MoveWindow(swapchain->device_window, 0, 0,
5496 swapchain_desc->backbuffer_width,
5497 swapchain_desc->backbuffer_height,
5501 else if (!swapchain->desc.windowed)
5503 /* Fullscreen -> windowed switch */
5504 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5505 wined3d_device_release_focus_window(device);
5507 swapchain->desc.windowed = swapchain_desc->windowed;
5509 else if (!swapchain_desc->windowed)
5511 DWORD style = device->style;
5512 DWORD exStyle = device->exStyle;
5513 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5514 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5515 * Reset to clear up their mess. Guild Wars also loses the device during that.
5518 device->exStyle = 0;
5519 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5520 swapchain_desc->backbuffer_width,
5521 swapchain_desc->backbuffer_height);
5522 device->style = style;
5523 device->exStyle = exStyle;
5528 TRACE("Resetting stateblock.\n");
5529 wined3d_stateblock_decref(device->updateStateBlock);
5530 wined3d_stateblock_decref(device->stateBlock);
5532 if (device->d3d_initialized)
5533 delete_opengl_contexts(device, swapchain);
5535 /* Note: No parent needed for initial internal stateblock */
5536 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5538 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5540 TRACE("Created stateblock %p.\n", device->stateBlock);
5541 device->updateStateBlock = device->stateBlock;
5542 wined3d_stateblock_incref(device->updateStateBlock);
5544 stateblock_init_default_state(device->stateBlock);
5548 struct wined3d_surface *rt = device->fb.render_targets[0];
5549 struct wined3d_state *state = &device->stateBlock->state;
5551 /* Note the min_z / max_z is not reset. */
5552 state->viewport.x = 0;
5553 state->viewport.y = 0;
5554 state->viewport.width = rt->resource.width;
5555 state->viewport.height = rt->resource.height;
5556 device_invalidate_state(device, STATE_VIEWPORT);
5558 state->scissor_rect.top = 0;
5559 state->scissor_rect.left = 0;
5560 state->scissor_rect.right = rt->resource.width;
5561 state->scissor_rect.bottom = rt->resource.height;
5562 device_invalidate_state(device, STATE_SCISSORRECT);
5565 swapchain_update_render_to_fbo(swapchain);
5566 swapchain_update_draw_bindings(swapchain);
5568 if (reset_state && device->d3d_initialized)
5569 hr = create_primary_opengl_context(device, swapchain);
5571 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5577 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5579 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5581 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5587 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5588 struct wined3d_device_creation_parameters *parameters)
5590 TRACE("device %p, parameters %p.\n", device, parameters);
5592 *parameters = device->create_parms;
5595 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5596 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5598 struct wined3d_swapchain *swapchain;
5600 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5601 device, swapchain_idx, flags, ramp);
5603 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5604 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5607 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5608 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5610 struct wined3d_swapchain *swapchain;
5612 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5613 device, swapchain_idx, ramp);
5615 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5616 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5619 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5621 TRACE("device %p, resource %p.\n", device, resource);
5623 list_add_head(&device->resources, &resource->resource_list_entry);
5626 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5628 TRACE("device %p, resource %p.\n", device, resource);
5630 list_remove(&resource->resource_list_entry);
5633 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5635 enum wined3d_resource_type type = resource->type;
5638 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5640 context_resource_released(device, resource, type);
5644 case WINED3D_RTYPE_SURFACE:
5646 struct wined3d_surface *surface = surface_from_resource(resource);
5648 if (!device->d3d_initialized) break;
5650 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5652 if (device->fb.render_targets[i] == surface)
5654 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5655 device->fb.render_targets[i] = NULL;
5659 if (device->fb.depth_stencil == surface)
5661 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5662 device->fb.depth_stencil = NULL;
5667 case WINED3D_RTYPE_TEXTURE:
5668 case WINED3D_RTYPE_CUBE_TEXTURE:
5669 case WINED3D_RTYPE_VOLUME_TEXTURE:
5670 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5672 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5674 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5676 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5677 texture, device->stateBlock, i);
5678 device->stateBlock->state.textures[i] = NULL;
5681 if (device->updateStateBlock != device->stateBlock
5682 && device->updateStateBlock->state.textures[i] == texture)
5684 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5685 texture, device->updateStateBlock, i);
5686 device->updateStateBlock->state.textures[i] = NULL;
5691 case WINED3D_RTYPE_BUFFER:
5693 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5695 for (i = 0; i < MAX_STREAMS; ++i)
5697 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5699 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5700 buffer, device->stateBlock, i);
5701 device->stateBlock->state.streams[i].buffer = NULL;
5704 if (device->updateStateBlock != device->stateBlock
5705 && device->updateStateBlock->state.streams[i].buffer == buffer)
5707 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5708 buffer, device->updateStateBlock, i);
5709 device->updateStateBlock->state.streams[i].buffer = NULL;
5714 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5716 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5717 buffer, device->stateBlock);
5718 device->stateBlock->state.index_buffer = NULL;
5721 if (device->updateStateBlock != device->stateBlock
5722 && device->updateStateBlock->state.index_buffer == buffer)
5724 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5725 buffer, device->updateStateBlock);
5726 device->updateStateBlock->state.index_buffer = NULL;
5735 /* Remove the resource from the resourceStore */
5736 device_resource_remove(device, resource);
5738 TRACE("Resource released.\n");
5741 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
5743 struct wined3d_resource *resource;
5745 TRACE("device %p, dc %p.\n", device, dc);
5750 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5752 if (resource->type == WINED3D_RTYPE_SURFACE)
5754 struct wined3d_surface *s = surface_from_resource(resource);
5758 TRACE("Found surface %p for dc %p.\n", s, dc);
5767 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5768 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5769 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5771 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5772 const struct fragment_pipeline *fragment_pipeline;
5773 struct shader_caps shader_caps;
5774 struct fragment_caps ffp_caps;
5779 device->wined3d = wined3d;
5780 wined3d_incref(device->wined3d);
5781 device->adapter = wined3d->adapter_count ? adapter : NULL;
5782 device->device_parent = device_parent;
5783 list_init(&device->resources);
5784 list_init(&device->shaders);
5785 device->surface_alignment = surface_alignment;
5787 /* Save the creation parameters. */
5788 device->create_parms.adapter_idx = adapter_idx;
5789 device->create_parms.device_type = device_type;
5790 device->create_parms.focus_window = focus_window;
5791 device->create_parms.flags = flags;
5793 for (i = 0; i < PATCHMAP_SIZE; ++i) list_init(&device->patches[i]);
5795 select_shader_mode(&adapter->gl_info, &device->ps_selected_mode, &device->vs_selected_mode);
5796 device->shader_backend = adapter->shader_backend;
5798 if (device->shader_backend)
5800 device->shader_backend->shader_get_caps(&adapter->gl_info, &shader_caps);
5801 device->vs_version = shader_caps.vs_version;
5802 device->gs_version = shader_caps.gs_version;
5803 device->ps_version = shader_caps.ps_version;
5804 device->d3d_vshader_constantF = shader_caps.vs_uniform_count;
5805 device->d3d_pshader_constantF = shader_caps.ps_uniform_count;
5806 device->vs_clipping = shader_caps.vs_clipping;
5808 fragment_pipeline = adapter->fragment_pipe;
5809 if (fragment_pipeline)
5811 fragment_pipeline->get_caps(&adapter->gl_info, &ffp_caps);
5812 device->max_ffp_textures = ffp_caps.MaxSimultaneousTextures;
5814 hr = compile_state_table(device->StateTable, device->multistate_funcs, &adapter->gl_info,
5815 ffp_vertexstate_template, fragment_pipeline, misc_state_template);
5818 ERR("Failed to compile state table, hr %#x.\n", hr);
5819 wined3d_decref(device->wined3d);
5823 device->blitter = adapter->blitter;
5825 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5828 WARN("Failed to create stateblock.\n");
5829 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5831 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5833 wined3d_decref(device->wined3d);
5837 TRACE("Created stateblock %p.\n", device->stateBlock);
5838 device->updateStateBlock = device->stateBlock;
5839 wined3d_stateblock_incref(device->updateStateBlock);
5845 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5847 DWORD rep = device->StateTable[state].representative;
5848 struct wined3d_context *context;
5853 for (i = 0; i < device->context_count; ++i)
5855 context = device->contexts[i];
5856 if(isStateDirty(context, rep)) continue;
5858 context->dirtyArray[context->numDirtyEntries++] = rep;
5859 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5860 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5861 context->isStateDirty[idx] |= (1 << shift);
5865 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5867 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5868 *width = context->current_rt->pow2Width;
5869 *height = context->current_rt->pow2Height;
5872 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5874 const struct wined3d_swapchain *swapchain = context->swapchain;
5875 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5876 * current context's drawable, which is the size of the back buffer of the swapchain
5877 * the active context belongs to. */
5878 *width = swapchain->desc.backbuffer_width;
5879 *height = swapchain->desc.backbuffer_height;
5882 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5883 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5885 if (device->filter_messages)
5887 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5888 window, message, wparam, lparam);
5890 return DefWindowProcW(window, message, wparam, lparam);
5892 return DefWindowProcA(window, message, wparam, lparam);
5895 if (message == WM_DESTROY)
5897 TRACE("unregister window %p.\n", window);
5898 wined3d_unregister_window(window);
5900 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5901 ERR("Window %p is not the focus window for device %p.\n", window, device);
5903 else if (message == WM_DISPLAYCHANGE)
5905 device->device_parent->ops->mode_changed(device->device_parent);
5909 return CallWindowProcW(proc, window, message, wparam, lparam);
5911 return CallWindowProcA(proc, window, message, wparam, lparam);