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);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
40 /* Define the default light parameters as specified by MSDN. */
41 const struct wined3d_light WINED3D_default_light =
43 WINED3D_LIGHT_DIRECTIONAL, /* Type */
44 { 1.0f, 1.0f, 1.0f, 0.0f }, /* Diffuse r,g,b,a */
45 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Specular r,g,b,a */
46 { 0.0f, 0.0f, 0.0f, 0.0f }, /* Ambient r,g,b,a, */
47 { 0.0f, 0.0f, 0.0f }, /* Position x,y,z */
48 { 0.0f, 0.0f, 1.0f }, /* Direction x,y,z */
51 0.0f, 0.0f, 0.0f, /* Attenuation 0,1,2 */
56 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
57 * actually have the same values in GL and D3D. */
58 GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
60 switch(primitive_type)
62 case WINED3D_PT_POINTLIST:
65 case WINED3D_PT_LINELIST:
68 case WINED3D_PT_LINESTRIP:
71 case WINED3D_PT_TRIANGLELIST:
74 case WINED3D_PT_TRIANGLESTRIP:
75 return GL_TRIANGLE_STRIP;
77 case WINED3D_PT_TRIANGLEFAN:
78 return GL_TRIANGLE_FAN;
80 case WINED3D_PT_LINELIST_ADJ:
81 return GL_LINES_ADJACENCY_ARB;
83 case WINED3D_PT_LINESTRIP_ADJ:
84 return GL_LINE_STRIP_ADJACENCY_ARB;
86 case WINED3D_PT_TRIANGLELIST_ADJ:
87 return GL_TRIANGLES_ADJACENCY_ARB;
89 case WINED3D_PT_TRIANGLESTRIP_ADJ:
90 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
93 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
98 static enum wined3d_primitive_type d3d_primitive_type_from_gl(GLenum primitive_type)
100 switch(primitive_type)
103 return WINED3D_PT_POINTLIST;
106 return WINED3D_PT_LINELIST;
109 return WINED3D_PT_LINESTRIP;
112 return WINED3D_PT_TRIANGLELIST;
114 case GL_TRIANGLE_STRIP:
115 return WINED3D_PT_TRIANGLESTRIP;
117 case GL_TRIANGLE_FAN:
118 return WINED3D_PT_TRIANGLEFAN;
120 case GL_LINES_ADJACENCY_ARB:
121 return WINED3D_PT_LINELIST_ADJ;
123 case GL_LINE_STRIP_ADJACENCY_ARB:
124 return WINED3D_PT_LINESTRIP_ADJ;
126 case GL_TRIANGLES_ADJACENCY_ARB:
127 return WINED3D_PT_TRIANGLELIST_ADJ;
129 case GL_TRIANGLE_STRIP_ADJACENCY_ARB:
130 return WINED3D_PT_TRIANGLESTRIP_ADJ;
133 FIXME("Unhandled primitive type %s\n", debug_d3dprimitivetype(primitive_type));
134 return WINED3D_PT_UNDEFINED;
138 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
140 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
141 *regnum = WINED3D_FFP_POSITION;
142 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
143 *regnum = WINED3D_FFP_BLENDWEIGHT;
144 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
145 *regnum = WINED3D_FFP_BLENDINDICES;
146 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
147 *regnum = WINED3D_FFP_NORMAL;
148 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
149 *regnum = WINED3D_FFP_PSIZE;
150 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
151 *regnum = WINED3D_FFP_DIFFUSE;
152 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
153 *regnum = WINED3D_FFP_SPECULAR;
154 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
155 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
158 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n", debug_d3ddeclusage(usage), usage_idx);
166 /* Context activation is done by the caller. */
167 static void device_stream_info_from_declaration(struct wined3d_device *device, struct wined3d_stream_info *stream_info)
169 const struct wined3d_state *state = &device->stateBlock->state;
170 /* We need to deal with frequency data! */
171 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
176 stream_info->use_map = 0;
177 stream_info->swizzle_map = 0;
178 stream_info->all_vbo = 1;
180 /* Check for transformed vertices, disable vertex shader if present. */
181 stream_info->position_transformed = declaration->position_transformed;
182 use_vshader = state->vertex_shader && !declaration->position_transformed;
184 /* Translate the declaration into strided data. */
185 for (i = 0; i < declaration->element_count; ++i)
187 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
188 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
189 struct wined3d_buffer *buffer = stream->buffer;
190 struct wined3d_bo_address data;
195 TRACE("%p Element %p (%u of %u)\n", declaration->elements,
196 element, i + 1, declaration->element_count);
198 if (!buffer) continue;
200 stride = stream->stride;
202 TRACE("Stream %u, buffer %p.\n", element->input_slot, buffer);
203 buffer_get_memory(buffer, &device->adapter->gl_info, &data);
205 /* We can't use VBOs if the base vertex index is negative. OpenGL
206 * doesn't accept negative offsets (or rather offsets bigger than the
207 * VBO, because the pointer is unsigned), so use system memory
208 * sources. In most sane cases the pointer - offset will still be > 0,
209 * otherwise it will wrap around to some big value. Hope that with the
210 * indices, the driver wraps it back internally. If not,
211 * drawStridedSlow() is needed, including a vertex buffer path. */
212 if (state->load_base_vertex_index < 0)
214 WARN_(d3d_perf)("load_base_vertex_index is < 0 (%d), not using VBOs.\n", state->load_base_vertex_index);
215 data.buffer_object = 0;
216 data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info);
217 if ((UINT_PTR)data.addr < -state->load_base_vertex_index * stride)
218 FIXME("System memory vertex data load offset is negative!\n");
220 data.addr += element->offset;
222 TRACE("offset %u input_slot %u usage_idx %d\n", element->offset, element->input_slot, element->usage_idx);
226 if (element->output_slot == ~0U)
228 /* TODO: Assuming vertexdeclarations are usually used with the
229 * same or a similar shader, it might be worth it to store the
230 * last used output slot and try that one first. */
231 stride_used = vshader_get_input(state->vertex_shader,
232 element->usage, element->usage_idx, &idx);
236 idx = element->output_slot;
242 if (!element->ffp_valid)
244 WARN("Skipping unsupported fixed function element of format %s and usage %s\n",
245 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
250 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
256 TRACE("Load %s array %u [usage %s, usage_idx %u, "
257 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u]\n",
258 use_vshader ? "shader": "fixed function", idx,
259 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
260 element->offset, stride, debug_d3dformat(element->format->id), data.buffer_object);
262 data.addr += stream->offset;
264 stream_info->elements[idx].format = element->format;
265 stream_info->elements[idx].data = data;
266 stream_info->elements[idx].stride = stride;
267 stream_info->elements[idx].stream_idx = element->input_slot;
269 if (!device->adapter->gl_info.supported[ARB_VERTEX_ARRAY_BGRA]
270 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
272 stream_info->swizzle_map |= 1 << idx;
274 stream_info->use_map |= 1 << idx;
278 /* Preload the vertex buffers. */
279 device->num_buffer_queries = 0;
280 for (i = 0, map = stream_info->use_map; map; map >>= 1, ++i)
282 struct wined3d_stream_info_element *element;
283 struct wined3d_buffer *buffer;
288 element = &stream_info->elements[i];
289 buffer = state->streams[element->stream_idx].buffer;
290 wined3d_buffer_preload(buffer);
292 /* If the preload dropped the buffer object, update the stream info. */
293 if (buffer->buffer_object != element->data.buffer_object)
295 element->data.buffer_object = 0;
296 element->data.addr = buffer_get_sysmem(buffer, &device->adapter->gl_info) + (ptrdiff_t)element->data.addr;
299 if (!buffer->buffer_object)
300 stream_info->all_vbo = 0;
303 device->buffer_queries[device->num_buffer_queries++] = buffer->query;
307 /* Context activation is done by the caller. */
308 void device_update_stream_info(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
310 struct wined3d_stream_info *stream_info = &device->stream_info;
311 const struct wined3d_state *state = &device->stateBlock->state;
312 DWORD prev_all_vbo = stream_info->all_vbo;
314 TRACE("============================= Vertex Declaration =============================\n");
315 device_stream_info_from_declaration(device, stream_info);
317 if (state->vertex_shader && !stream_info->position_transformed)
319 if (state->vertex_declaration->half_float_conv_needed && !stream_info->all_vbo)
321 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
322 device->useDrawStridedSlow = TRUE;
326 device->useDrawStridedSlow = FALSE;
331 WORD slow_mask = (1 << WINED3D_FFP_PSIZE);
332 slow_mask |= -!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
333 & ((1 << WINED3D_FFP_DIFFUSE) | (1 << WINED3D_FFP_SPECULAR));
335 if ((stream_info->position_transformed || (stream_info->use_map & slow_mask)) && !stream_info->all_vbo)
337 device->useDrawStridedSlow = TRUE;
341 device->useDrawStridedSlow = FALSE;
345 if (prev_all_vbo != stream_info->all_vbo)
346 device_invalidate_state(device, STATE_INDEXBUFFER);
349 static void device_preload_texture(const struct wined3d_state *state, unsigned int idx)
351 struct wined3d_texture *texture;
352 enum WINED3DSRGB srgb;
354 if (!(texture = state->textures[idx])) return;
355 srgb = state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE] ? SRGB_SRGB : SRGB_RGB;
356 texture->texture_ops->texture_preload(texture, srgb);
359 void device_preload_textures(const struct wined3d_device *device)
361 const struct wined3d_state *state = &device->stateBlock->state;
366 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
368 if (state->vertex_shader->reg_maps.sampler_type[i])
369 device_preload_texture(state, MAX_FRAGMENT_SAMPLERS + i);
375 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
377 if (state->pixel_shader->reg_maps.sampler_type[i])
378 device_preload_texture(state, i);
383 WORD ffu_map = device->fixed_function_usage_map;
385 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
388 device_preload_texture(state, i);
393 BOOL device_context_add(struct wined3d_device *device, struct wined3d_context *context)
395 struct wined3d_context **new_array;
397 TRACE("Adding context %p.\n", context);
399 if (!device->contexts) new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
400 else new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts,
401 sizeof(*new_array) * (device->context_count + 1));
405 ERR("Failed to grow the context array.\n");
409 new_array[device->context_count++] = context;
410 device->contexts = new_array;
414 void device_context_remove(struct wined3d_device *device, struct wined3d_context *context)
416 struct wined3d_context **new_array;
420 TRACE("Removing context %p.\n", context);
422 for (i = 0; i < device->context_count; ++i)
424 if (device->contexts[i] == context)
433 ERR("Context %p doesn't exist in context array.\n", context);
437 if (!--device->context_count)
439 HeapFree(GetProcessHeap(), 0, device->contexts);
440 device->contexts = NULL;
444 memmove(&device->contexts[i], &device->contexts[i + 1], (device->context_count - i) * sizeof(*device->contexts));
445 new_array = HeapReAlloc(GetProcessHeap(), 0, device->contexts, device->context_count * sizeof(*device->contexts));
448 ERR("Failed to shrink context array. Oh well.\n");
452 device->contexts = new_array;
455 /* Do not call while under the GL lock. */
456 void device_switch_onscreen_ds(struct wined3d_device *device,
457 struct wined3d_context *context, struct wined3d_surface *depth_stencil)
459 if (device->onscreen_depth_stencil)
461 surface_load_ds_location(device->onscreen_depth_stencil, context, SFLAG_INTEXTURE);
463 surface_modify_ds_location(device->onscreen_depth_stencil, SFLAG_INTEXTURE,
464 device->onscreen_depth_stencil->ds_current_size.cx,
465 device->onscreen_depth_stencil->ds_current_size.cy);
466 wined3d_surface_decref(device->onscreen_depth_stencil);
468 device->onscreen_depth_stencil = depth_stencil;
469 wined3d_surface_incref(device->onscreen_depth_stencil);
472 static BOOL is_full_clear(const struct wined3d_surface *target, const RECT *draw_rect, const RECT *clear_rect)
474 /* partial draw rect */
475 if (draw_rect->left || draw_rect->top
476 || draw_rect->right < target->resource.width
477 || draw_rect->bottom < target->resource.height)
480 /* partial clear rect */
481 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
482 || clear_rect->right < target->resource.width
483 || clear_rect->bottom < target->resource.height))
489 static void prepare_ds_clear(struct wined3d_surface *ds, struct wined3d_context *context,
490 DWORD location, const RECT *draw_rect, UINT rect_count, const RECT *clear_rect, RECT *out_rect)
492 RECT current_rect, r;
494 if (ds->flags & SFLAG_DISCARDED)
496 /* Depth buffer was discarded, make it entirely current in its new location since
497 * there is no other place where we would get data anyway. */
498 SetRect(out_rect, 0, 0, ds->resource.width, ds->resource.height);
502 if (ds->flags & location)
503 SetRect(¤t_rect, 0, 0,
504 ds->ds_current_size.cx,
505 ds->ds_current_size.cy);
507 SetRectEmpty(¤t_rect);
509 IntersectRect(&r, draw_rect, ¤t_rect);
510 if (EqualRect(&r, draw_rect))
512 /* current_rect ⊇ draw_rect, modify only. */
513 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
517 if (EqualRect(&r, ¤t_rect))
519 /* draw_rect ⊇ current_rect, test if we're doing a full clear. */
523 /* Full clear, modify only. */
524 *out_rect = *draw_rect;
528 IntersectRect(&r, draw_rect, clear_rect);
529 if (EqualRect(&r, draw_rect))
531 /* clear_rect ⊇ draw_rect, modify only. */
532 *out_rect = *draw_rect;
538 surface_load_ds_location(ds, context, location);
539 SetRect(out_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
542 /* Do not call while under the GL lock. */
543 void device_clear_render_targets(struct wined3d_device *device, UINT rt_count, const struct wined3d_fb_state *fb,
544 UINT rect_count, const RECT *rects, const RECT *draw_rect, DWORD flags, const struct wined3d_color *color,
545 float depth, DWORD stencil)
547 const RECT *clear_rect = (rect_count > 0 && rects) ? (const RECT *)rects : NULL;
548 struct wined3d_surface *target = rt_count ? fb->render_targets[0] : NULL;
549 const struct wined3d_gl_info *gl_info;
550 UINT drawable_width, drawable_height;
551 struct wined3d_context *context;
552 GLbitfield clear_mask = 0;
553 BOOL render_offscreen;
557 /* When we're clearing parts of the drawable, make sure that the target surface is well up to date in the
558 * drawable. After the clear we'll mark the drawable up to date, so we have to make sure that this is true
559 * for the cleared parts, and the untouched parts.
561 * If we're clearing the whole target there is no need to copy it into the drawable, it will be overwritten
562 * anyway. If we're not clearing the color buffer we don't have to copy either since we're not going to set
563 * the drawable up to date. We have to check all settings that limit the clear area though. Do not bother
564 * checking all this if the dest surface is in the drawable anyway. */
565 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(target, draw_rect, clear_rect))
567 for (i = 0; i < rt_count; ++i)
569 struct wined3d_surface *rt = fb->render_targets[i];
571 surface_load_location(rt, rt->draw_binding, NULL);
575 context = context_acquire(device, target);
578 context_release(context);
579 WARN("Invalid context, skipping clear.\n");
582 gl_info = context->gl_info;
586 render_offscreen = context->render_offscreen;
587 target->get_drawable_size(context, &drawable_width, &drawable_height);
591 render_offscreen = TRUE;
592 drawable_width = fb->depth_stencil->pow2Width;
593 drawable_height = fb->depth_stencil->pow2Height;
596 if (flags & WINED3DCLEAR_ZBUFFER)
598 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
600 if (!render_offscreen && fb->depth_stencil != device->onscreen_depth_stencil)
601 device_switch_onscreen_ds(device, context, fb->depth_stencil);
602 prepare_ds_clear(fb->depth_stencil, context, location,
603 draw_rect, rect_count, clear_rect, &ds_rect);
606 if (!context_apply_clear_state(context, device, rt_count, fb))
608 context_release(context);
609 WARN("Failed to apply clear state, skipping clear.\n");
613 /* Only set the values up once, as they are not changing. */
614 if (flags & WINED3DCLEAR_STENCIL)
616 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
618 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
619 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
621 gl_info->gl_ops.gl.p_glStencilMask(~0U);
622 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
623 gl_info->gl_ops.gl.p_glClearStencil(stencil);
624 checkGLcall("glClearStencil");
625 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
628 if (flags & WINED3DCLEAR_ZBUFFER)
630 DWORD location = render_offscreen ? fb->depth_stencil->draw_binding : SFLAG_INDRAWABLE;
632 surface_modify_ds_location(fb->depth_stencil, location, ds_rect.right, ds_rect.bottom);
634 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
635 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
636 gl_info->gl_ops.gl.p_glClearDepth(depth);
637 checkGLcall("glClearDepth");
638 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
641 if (flags & WINED3DCLEAR_TARGET)
643 for (i = 0; i < rt_count; ++i)
645 struct wined3d_surface *rt = fb->render_targets[i];
648 surface_modify_location(rt, rt->draw_binding, TRUE);
651 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
652 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
653 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
654 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
655 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
656 gl_info->gl_ops.gl.p_glClearColor(color->r, color->g, color->b, color->a);
657 checkGLcall("glClearColor");
658 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
663 if (render_offscreen)
665 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
666 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
670 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
671 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
673 checkGLcall("glScissor");
674 gl_info->gl_ops.gl.p_glClear(clear_mask);
675 checkGLcall("glClear");
681 /* Now process each rect in turn. */
682 for (i = 0; i < rect_count; ++i)
684 /* Note that GL uses lower left, width/height. */
685 IntersectRect(¤t_rect, draw_rect, &clear_rect[i]);
687 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
688 wine_dbgstr_rect(&clear_rect[i]),
689 wine_dbgstr_rect(¤t_rect));
691 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored silently.
692 * The rectangle is not cleared, no error is returned, but further rectangles are
693 * still cleared if they are valid. */
694 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
696 TRACE("Rectangle with negative dimensions, ignoring.\n");
700 if (render_offscreen)
702 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
703 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
707 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
708 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
710 checkGLcall("glScissor");
712 gl_info->gl_ops.gl.p_glClear(clear_mask);
713 checkGLcall("glClear");
717 if (wined3d_settings.strict_draw_ordering || (flags & WINED3DCLEAR_TARGET
718 && target->container.type == WINED3D_CONTAINER_SWAPCHAIN
719 && target->container.u.swapchain->front_buffer == target))
720 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
722 context_release(context);
725 ULONG CDECL wined3d_device_incref(struct wined3d_device *device)
727 ULONG refcount = InterlockedIncrement(&device->ref);
729 TRACE("%p increasing refcount to %u.\n", device, refcount);
734 ULONG CDECL wined3d_device_decref(struct wined3d_device *device)
736 ULONG refcount = InterlockedDecrement(&device->ref);
738 TRACE("%p decreasing refcount to %u.\n", device, refcount);
742 struct wined3d_stateblock *stateblock;
745 if (wined3d_stateblock_decref(device->updateStateBlock)
746 && device->updateStateBlock != device->stateBlock)
747 FIXME("Something's still holding the update stateblock.\n");
748 device->updateStateBlock = NULL;
750 stateblock = device->stateBlock;
751 device->stateBlock = NULL;
752 if (wined3d_stateblock_decref(stateblock))
753 FIXME("Something's still holding the stateblock.\n");
755 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
757 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
758 device->multistate_funcs[i] = NULL;
761 if (!list_empty(&device->resources))
763 struct wined3d_resource *resource;
765 FIXME("Device released with resources still bound, acceptable but unexpected.\n");
767 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
769 FIXME("Leftover resource %p with type %s (%#x).\n",
770 resource, debug_d3dresourcetype(resource->type), resource->type);
774 if (device->contexts)
775 ERR("Context array not freed!\n");
776 if (device->hardwareCursor)
777 DestroyCursor(device->hardwareCursor);
778 device->hardwareCursor = 0;
780 wined3d_decref(device->wined3d);
781 device->wined3d = NULL;
782 HeapFree(GetProcessHeap(), 0, device);
783 TRACE("Freed device %p.\n", device);
789 UINT CDECL wined3d_device_get_swapchain_count(const struct wined3d_device *device)
791 TRACE("device %p.\n", device);
793 return device->swapchain_count;
796 struct wined3d_swapchain * CDECL wined3d_device_get_swapchain(const struct wined3d_device *device, UINT swapchain_idx)
798 TRACE("device %p, swapchain_idx %u.\n", device, swapchain_idx);
800 if (swapchain_idx >= device->swapchain_count)
802 WARN("swapchain_idx %u >= swapchain_count %u.\n",
803 swapchain_idx, device->swapchain_count);
807 return device->swapchains[swapchain_idx];
810 static void device_load_logo(struct wined3d_device *device, const char *filename)
812 struct wined3d_color_key color_key;
816 HDC dcb = NULL, dcs = NULL;
818 hbm = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
821 GetObjectA(hbm, sizeof(BITMAP), &bm);
822 dcb = CreateCompatibleDC(NULL);
824 SelectObject(dcb, hbm);
828 /* Create a 32x32 white surface to indicate that wined3d is used, but the specified image
831 memset(&bm, 0, sizeof(bm));
836 hr = wined3d_surface_create(device, bm.bmWidth, bm.bmHeight, WINED3DFMT_B5G6R5_UNORM, 0,
837 WINED3D_POOL_SYSTEM_MEM, WINED3D_MULTISAMPLE_NONE, 0, WINED3D_SURFACE_MAPPABLE,
838 NULL, &wined3d_null_parent_ops, &device->logo_surface);
841 ERR("Wine logo requested, but failed to create surface, hr %#x.\n", hr);
847 if (FAILED(hr = wined3d_surface_getdc(device->logo_surface, &dcs)))
849 BitBlt(dcs, 0, 0, bm.bmWidth, bm.bmHeight, dcb, 0, 0, SRCCOPY);
850 wined3d_surface_releasedc(device->logo_surface, dcs);
852 color_key.color_space_low_value = 0;
853 color_key.color_space_high_value = 0;
854 wined3d_surface_set_color_key(device->logo_surface, WINEDDCKEY_SRCBLT, &color_key);
858 const struct wined3d_color c = {1.0f, 1.0f, 1.0f, 1.0f};
859 /* Fill the surface with a white color to show that wined3d is there */
860 wined3d_device_color_fill(device, device->logo_surface, NULL, &c);
864 if (dcb) DeleteDC(dcb);
865 if (hbm) DeleteObject(hbm);
868 /* Context activation is done by the caller. */
869 static void create_dummy_textures(struct wined3d_device *device, struct wined3d_context *context)
871 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
872 unsigned int i, j, count;
873 /* Under DirectX you can sample even if no texture is bound, whereas
874 * OpenGL will only allow that when a valid texture is bound.
875 * We emulate this by creating dummy textures and binding them
876 * to each texture stage when the currently set D3D texture is NULL. */
878 if (gl_info->supported[APPLE_CLIENT_STORAGE])
880 /* The dummy texture does not have client storage backing */
881 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
882 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
885 count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
886 for (i = 0; i < count; ++i)
888 DWORD color = 0x000000ff;
890 /* Make appropriate texture active */
891 context_active_texture(context, gl_info, i);
893 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_2d[i]);
894 checkGLcall("glGenTextures");
895 TRACE("Dummy 2D texture %u given name %u.\n", i, device->dummy_texture_2d[i]);
897 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
898 checkGLcall("glBindTexture");
900 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0,
901 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
902 checkGLcall("glTexImage2D");
904 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
906 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_rect[i]);
907 checkGLcall("glGenTextures");
908 TRACE("Dummy rectangle texture %u given name %u.\n", i, device->dummy_texture_rect[i]);
910 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
911 checkGLcall("glBindTexture");
913 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 1, 1, 0,
914 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
915 checkGLcall("glTexImage2D");
918 if (gl_info->supported[EXT_TEXTURE3D])
920 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_3d[i]);
921 checkGLcall("glGenTextures");
922 TRACE("Dummy 3D texture %u given name %u.\n", i, device->dummy_texture_3d[i]);
924 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
925 checkGLcall("glBindTexture");
927 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, 0, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color));
928 checkGLcall("glTexImage3D");
931 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
933 gl_info->gl_ops.gl.p_glGenTextures(1, &device->dummy_texture_cube[i]);
934 checkGLcall("glGenTextures");
935 TRACE("Dummy cube texture %u given name %u.\n", i, device->dummy_texture_cube[i]);
937 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
938 checkGLcall("glBindTexture");
940 for (j = GL_TEXTURE_CUBE_MAP_POSITIVE_X; j <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++j)
942 gl_info->gl_ops.gl.p_glTexImage2D(j, 0, GL_RGBA8, 1, 1, 0,
943 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, &color);
944 checkGLcall("glTexImage2D");
949 if (gl_info->supported[APPLE_CLIENT_STORAGE])
951 /* Re-enable because if supported it is enabled by default */
952 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
953 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
957 /* Context activation is done by the caller. */
958 static void destroy_dummy_textures(struct wined3d_device *device, const struct wined3d_gl_info *gl_info)
960 unsigned int count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
962 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
964 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_cube);
965 checkGLcall("glDeleteTextures(count, device->dummy_texture_cube)");
968 if (gl_info->supported[EXT_TEXTURE3D])
970 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_3d);
971 checkGLcall("glDeleteTextures(count, device->dummy_texture_3d)");
974 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
976 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_rect);
977 checkGLcall("glDeleteTextures(count, device->dummy_texture_rect)");
980 gl_info->gl_ops.gl.p_glDeleteTextures(count, device->dummy_texture_2d);
981 checkGLcall("glDeleteTextures(count, device->dummy_texture_2d)");
983 memset(device->dummy_texture_cube, 0, count * sizeof(*device->dummy_texture_cube));
984 memset(device->dummy_texture_3d, 0, count * sizeof(*device->dummy_texture_3d));
985 memset(device->dummy_texture_rect, 0, count * sizeof(*device->dummy_texture_rect));
986 memset(device->dummy_texture_2d, 0, count * sizeof(*device->dummy_texture_2d));
989 static LONG fullscreen_style(LONG style)
991 /* Make sure the window is managed, otherwise we won't get keyboard input. */
992 style |= WS_POPUP | WS_SYSMENU;
993 style &= ~(WS_CAPTION | WS_THICKFRAME);
998 static LONG fullscreen_exstyle(LONG exstyle)
1000 /* Filter out window decorations. */
1001 exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
1006 void CDECL wined3d_device_setup_fullscreen_window(struct wined3d_device *device, HWND window, UINT w, UINT h)
1008 BOOL filter_messages;
1009 LONG style, exstyle;
1011 TRACE("Setting up window %p for fullscreen mode.\n", window);
1013 if (device->style || device->exStyle)
1015 ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
1016 window, device->style, device->exStyle);
1019 device->style = GetWindowLongW(window, GWL_STYLE);
1020 device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
1022 style = fullscreen_style(device->style);
1023 exstyle = fullscreen_exstyle(device->exStyle);
1025 TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
1026 device->style, device->exStyle, style, exstyle);
1028 filter_messages = device->filter_messages;
1029 device->filter_messages = TRUE;
1031 SetWindowLongW(window, GWL_STYLE, style);
1032 SetWindowLongW(window, GWL_EXSTYLE, exstyle);
1033 SetWindowPos(window, HWND_TOPMOST, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
1035 device->filter_messages = filter_messages;
1038 void CDECL wined3d_device_restore_fullscreen_window(struct wined3d_device *device, HWND window)
1040 BOOL filter_messages;
1041 LONG style, exstyle;
1043 if (!device->style && !device->exStyle) return;
1045 style = GetWindowLongW(window, GWL_STYLE);
1046 exstyle = GetWindowLongW(window, GWL_EXSTYLE);
1048 /* These flags are set by wined3d_device_setup_fullscreen_window, not the
1049 * application, and we want to ignore them in the test below, since it's
1050 * not the application's fault that they changed. Additionally, we want to
1051 * preserve the current status of these flags (i.e. don't restore them) to
1052 * more closely emulate the behavior of Direct3D, which leaves these flags
1053 * alone when returning to windowed mode. */
1054 device->style ^= (device->style ^ style) & WS_VISIBLE;
1055 device->exStyle ^= (device->exStyle ^ exstyle) & WS_EX_TOPMOST;
1057 TRACE("Restoring window style of window %p to %08x, %08x.\n",
1058 window, device->style, device->exStyle);
1060 filter_messages = device->filter_messages;
1061 device->filter_messages = TRUE;
1063 /* Only restore the style if the application didn't modify it during the
1064 * fullscreen phase. Some applications change it before calling Reset()
1065 * when switching between windowed and fullscreen modes (HL2), some
1066 * depend on the original style (Eve Online). */
1067 if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
1069 SetWindowLongW(window, GWL_STYLE, device->style);
1070 SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
1072 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1074 device->filter_messages = filter_messages;
1076 /* Delete the old values. */
1078 device->exStyle = 0;
1081 HRESULT CDECL wined3d_device_acquire_focus_window(struct wined3d_device *device, HWND window)
1083 TRACE("device %p, window %p.\n", device, window);
1085 if (!wined3d_register_window(window, device))
1087 ERR("Failed to register window %p.\n", window);
1091 InterlockedExchangePointer((void **)&device->focus_window, window);
1092 SetWindowPos(window, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1097 void CDECL wined3d_device_release_focus_window(struct wined3d_device *device)
1099 TRACE("device %p.\n", device);
1101 if (device->focus_window) wined3d_unregister_window(device->focus_window);
1102 InterlockedExchangePointer((void **)&device->focus_window, NULL);
1105 HRESULT CDECL wined3d_device_init_3d(struct wined3d_device *device,
1106 struct wined3d_swapchain_desc *swapchain_desc)
1108 static const struct wined3d_color black = {0.0f, 0.0f, 0.0f, 0.0f};
1109 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1110 struct wined3d_swapchain *swapchain = NULL;
1111 struct wined3d_context *context;
1115 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1117 if (device->d3d_initialized)
1118 return WINED3DERR_INVALIDCALL;
1119 if (device->wined3d->flags & WINED3D_NO3D)
1120 return WINED3DERR_INVALIDCALL;
1122 device->fb.render_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1123 sizeof(*device->fb.render_targets) * gl_info->limits.buffers);
1125 /* Initialize the texture unit mapping to a 1:1 mapping */
1126 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
1128 if (state < gl_info->limits.fragment_samplers)
1130 device->texUnitMap[state] = state;
1131 device->rev_tex_unit_map[state] = state;
1135 device->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
1136 device->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
1140 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
1141 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
1143 TRACE("Shader private data couldn't be allocated\n");
1146 if (FAILED(hr = device->blitter->alloc_private(device)))
1148 TRACE("Blitter private data couldn't be allocated\n");
1152 /* Setup the implicit swapchain. This also initializes a context. */
1153 TRACE("Creating implicit swapchain\n");
1154 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1155 swapchain_desc, &swapchain);
1158 WARN("Failed to create implicit swapchain\n");
1162 device->swapchain_count = 1;
1163 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1164 if (!device->swapchains)
1166 ERR("Out of memory!\n");
1169 device->swapchains[0] = swapchain;
1171 if (swapchain->back_buffers && swapchain->back_buffers[0])
1173 TRACE("Setting rendertarget to %p.\n", swapchain->back_buffers);
1174 device->fb.render_targets[0] = swapchain->back_buffers[0];
1178 TRACE("Setting rendertarget to %p.\n", swapchain->front_buffer);
1179 device->fb.render_targets[0] = swapchain->front_buffer;
1181 wined3d_surface_incref(device->fb.render_targets[0]);
1183 /* Depth Stencil support */
1184 device->fb.depth_stencil = device->auto_depth_stencil;
1185 if (device->fb.depth_stencil)
1186 wined3d_surface_incref(device->fb.depth_stencil);
1188 /* Set up some starting GL setup */
1190 /* Setup all the devices defaults */
1191 stateblock_init_default_state(device->stateBlock);
1193 context = context_acquire(device, swapchain->front_buffer);
1195 create_dummy_textures(device, context);
1197 device->contexts[0]->last_was_rhw = 0;
1199 switch (wined3d_settings.offscreen_rendering_mode)
1202 device->offscreenBuffer = GL_COLOR_ATTACHMENT0;
1205 case ORM_BACKBUFFER:
1207 if (context_get_current()->aux_buffers > 0)
1209 TRACE("Using auxiliary buffer for offscreen rendering\n");
1210 device->offscreenBuffer = GL_AUX0;
1214 TRACE("Using back buffer for offscreen rendering\n");
1215 device->offscreenBuffer = GL_BACK;
1220 TRACE("All defaults now set up, leaving 3D init.\n");
1222 context_release(context);
1224 /* Clear the screen */
1225 wined3d_device_clear(device, 0, NULL, WINED3DCLEAR_TARGET
1226 | (swapchain_desc->enable_auto_depth_stencil ? WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL : 0),
1229 device->d3d_initialized = TRUE;
1231 if (wined3d_settings.logo)
1232 device_load_logo(device, wined3d_settings.logo);
1236 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1237 HeapFree(GetProcessHeap(), 0, device->swapchains);
1238 device->swapchain_count = 0;
1240 wined3d_swapchain_decref(swapchain);
1241 if (device->blit_priv)
1242 device->blitter->free_private(device);
1243 if (device->shader_priv)
1244 device->shader_backend->shader_free_private(device);
1249 HRESULT CDECL wined3d_device_init_gdi(struct wined3d_device *device,
1250 struct wined3d_swapchain_desc *swapchain_desc)
1252 struct wined3d_swapchain *swapchain = NULL;
1255 TRACE("device %p, swapchain_desc %p.\n", device, swapchain_desc);
1257 /* Setup the implicit swapchain */
1258 TRACE("Creating implicit swapchain\n");
1259 hr = device->device_parent->ops->create_swapchain(device->device_parent,
1260 swapchain_desc, &swapchain);
1263 WARN("Failed to create implicit swapchain\n");
1267 device->swapchain_count = 1;
1268 device->swapchains = HeapAlloc(GetProcessHeap(), 0, device->swapchain_count * sizeof(*device->swapchains));
1269 if (!device->swapchains)
1271 ERR("Out of memory!\n");
1274 device->swapchains[0] = swapchain;
1278 wined3d_swapchain_decref(swapchain);
1282 HRESULT CDECL wined3d_device_uninit_3d(struct wined3d_device *device)
1284 struct wined3d_resource *resource, *cursor;
1285 const struct wined3d_gl_info *gl_info;
1286 struct wined3d_context *context;
1287 struct wined3d_surface *surface;
1290 TRACE("device %p.\n", device);
1292 if (!device->d3d_initialized)
1293 return WINED3DERR_INVALIDCALL;
1295 /* Force making the context current again, to verify it is still valid
1296 * (workaround for broken drivers) */
1297 context_set_current(NULL);
1298 /* I don't think that the interface guarantees that the device is destroyed from the same thread
1299 * it was created. Thus make sure a context is active for the glDelete* calls
1301 context = context_acquire(device, NULL);
1302 gl_info = context->gl_info;
1304 if (device->logo_surface)
1305 wined3d_surface_decref(device->logo_surface);
1307 stateblock_unbind_resources(device->stateBlock);
1309 /* Unload resources */
1310 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
1312 TRACE("Unloading resource %p.\n", resource);
1314 resource->resource_ops->resource_unload(resource);
1317 /* Delete the mouse cursor texture */
1318 if (device->cursorTexture)
1320 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
1321 device->cursorTexture = 0;
1324 /* Destroy the depth blt resources, they will be invalid after the reset. Also free shader
1325 * private data, it might contain opengl pointers
1327 if (device->depth_blt_texture)
1329 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
1330 device->depth_blt_texture = 0;
1333 /* Destroy the shader backend. Note that this has to happen after all shaders are destroyed. */
1334 device->blitter->free_private(device);
1335 device->shader_backend->shader_free_private(device);
1337 /* Release the buffers (with sanity checks)*/
1338 if (device->onscreen_depth_stencil)
1340 surface = device->onscreen_depth_stencil;
1341 device->onscreen_depth_stencil = NULL;
1342 wined3d_surface_decref(surface);
1345 if (device->fb.depth_stencil)
1347 surface = device->fb.depth_stencil;
1349 TRACE("Releasing depth/stencil buffer %p.\n", surface);
1351 device->fb.depth_stencil = NULL;
1352 wined3d_surface_decref(surface);
1355 if (device->auto_depth_stencil)
1357 surface = device->auto_depth_stencil;
1358 device->auto_depth_stencil = NULL;
1359 if (wined3d_surface_decref(surface))
1360 FIXME("Something's still holding the auto depth stencil buffer (%p).\n", surface);
1363 for (i = 1; i < gl_info->limits.buffers; ++i)
1365 wined3d_device_set_render_target(device, i, NULL, FALSE);
1368 surface = device->fb.render_targets[0];
1369 TRACE("Setting rendertarget 0 to NULL\n");
1370 device->fb.render_targets[0] = NULL;
1371 TRACE("Releasing the render target at %p\n", surface);
1372 wined3d_surface_decref(surface);
1374 context_release(context);
1376 for (i = 0; i < device->swapchain_count; ++i)
1378 TRACE("Releasing the implicit swapchain %u.\n", i);
1379 if (wined3d_swapchain_decref(device->swapchains[i]))
1380 FIXME("Something's still holding the implicit swapchain.\n");
1383 HeapFree(GetProcessHeap(), 0, device->swapchains);
1384 device->swapchains = NULL;
1385 device->swapchain_count = 0;
1387 HeapFree(GetProcessHeap(), 0, device->fb.render_targets);
1388 device->fb.render_targets = NULL;
1390 device->d3d_initialized = FALSE;
1395 HRESULT CDECL wined3d_device_uninit_gdi(struct wined3d_device *device)
1399 for (i = 0; i < device->swapchain_count; ++i)
1401 TRACE("Releasing the implicit swapchain %u.\n", i);
1402 if (wined3d_swapchain_decref(device->swapchains[i]))
1403 FIXME("Something's still holding the implicit swapchain.\n");
1406 HeapFree(GetProcessHeap(), 0, device->swapchains);
1407 device->swapchains = NULL;
1408 device->swapchain_count = 0;
1412 /* Enables thread safety in the wined3d device and its resources. Called by DirectDraw
1413 * from SetCooperativeLevel if DDSCL_MULTITHREADED is specified, and by d3d8/9 from
1414 * CreateDevice if D3DCREATE_MULTITHREADED is passed.
1416 * There is no way to deactivate thread safety once it is enabled.
1418 void CDECL wined3d_device_set_multithreaded(struct wined3d_device *device)
1420 TRACE("device %p.\n", device);
1422 /* For now just store the flag (needed in case of ddraw). */
1423 device->create_parms.flags |= WINED3DCREATE_MULTITHREADED;
1426 UINT CDECL wined3d_device_get_available_texture_mem(const struct wined3d_device *device)
1428 TRACE("device %p.\n", device);
1430 TRACE("Emulating %d MB, returning %d MB left.\n",
1431 device->adapter->TextureRam / (1024 * 1024),
1432 (device->adapter->TextureRam - device->adapter->UsedTextureRam) / (1024 * 1024));
1434 return device->adapter->TextureRam - device->adapter->UsedTextureRam;
1437 void CDECL wined3d_device_set_stream_output(struct wined3d_device *device, UINT idx,
1438 struct wined3d_buffer *buffer, UINT offset)
1440 struct wined3d_buffer *prev_buffer;
1442 TRACE("device %p, idx %u, buffer %p, offset %u.\n", device, idx, buffer, offset);
1444 if (idx >= MAX_STREAM_OUT)
1446 WARN("Invalid stream output %u.\n", idx);
1450 prev_buffer = device->updateStateBlock->state.stream_output[idx].buffer;
1451 device->updateStateBlock->state.stream_output[idx].buffer = buffer;
1452 device->updateStateBlock->state.stream_output[idx].offset = offset;
1454 if (device->isRecordingState)
1457 wined3d_buffer_incref(buffer);
1459 wined3d_buffer_decref(prev_buffer);
1463 if (prev_buffer != buffer)
1467 InterlockedIncrement(&buffer->resource.bind_count);
1468 wined3d_buffer_incref(buffer);
1472 InterlockedDecrement(&prev_buffer->resource.bind_count);
1473 wined3d_buffer_decref(prev_buffer);
1478 struct wined3d_buffer * CDECL wined3d_device_get_stream_output(struct wined3d_device *device,
1479 UINT idx, UINT *offset)
1481 TRACE("device %p, idx %u, offset %p.\n", device, idx, offset);
1483 if (idx >= MAX_STREAM_OUT)
1485 WARN("Invalid stream output %u.\n", idx);
1489 *offset = device->stateBlock->state.stream_output[idx].offset;
1490 return device->stateBlock->state.stream_output[idx].buffer;
1493 HRESULT CDECL wined3d_device_set_stream_source(struct wined3d_device *device, UINT stream_idx,
1494 struct wined3d_buffer *buffer, UINT offset, UINT stride)
1496 struct wined3d_stream_state *stream;
1497 struct wined3d_buffer *prev_buffer;
1499 TRACE("device %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1500 device, stream_idx, buffer, offset, stride);
1502 if (stream_idx >= MAX_STREAMS)
1504 WARN("Stream index %u out of range.\n", stream_idx);
1505 return WINED3DERR_INVALIDCALL;
1507 else if (offset & 0x3)
1509 WARN("Offset %u is not 4 byte aligned.\n", offset);
1510 return WINED3DERR_INVALIDCALL;
1513 stream = &device->updateStateBlock->state.streams[stream_idx];
1514 prev_buffer = stream->buffer;
1516 device->updateStateBlock->changed.streamSource |= 1 << stream_idx;
1518 if (prev_buffer == buffer
1519 && stream->stride == stride
1520 && stream->offset == offset)
1522 TRACE("Application is setting the old values over, nothing to do.\n");
1526 stream->buffer = buffer;
1529 stream->stride = stride;
1530 stream->offset = offset;
1533 /* Handle recording of state blocks. */
1534 if (device->isRecordingState)
1536 TRACE("Recording... not performing anything.\n");
1538 wined3d_buffer_incref(buffer);
1540 wined3d_buffer_decref(prev_buffer);
1546 InterlockedIncrement(&buffer->resource.bind_count);
1547 wined3d_buffer_incref(buffer);
1551 InterlockedDecrement(&prev_buffer->resource.bind_count);
1552 wined3d_buffer_decref(prev_buffer);
1555 device_invalidate_state(device, STATE_STREAMSRC);
1560 HRESULT CDECL wined3d_device_get_stream_source(const struct wined3d_device *device,
1561 UINT stream_idx, struct wined3d_buffer **buffer, UINT *offset, UINT *stride)
1563 struct wined3d_stream_state *stream;
1565 TRACE("device %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1566 device, stream_idx, buffer, offset, stride);
1568 if (stream_idx >= MAX_STREAMS)
1570 WARN("Stream index %u out of range.\n", stream_idx);
1571 return WINED3DERR_INVALIDCALL;
1574 stream = &device->stateBlock->state.streams[stream_idx];
1575 *buffer = stream->buffer;
1577 wined3d_buffer_incref(*buffer);
1579 *offset = stream->offset;
1580 *stride = stream->stride;
1585 HRESULT CDECL wined3d_device_set_stream_source_freq(struct wined3d_device *device, UINT stream_idx, UINT divider)
1587 struct wined3d_stream_state *stream;
1588 UINT old_flags, old_freq;
1590 TRACE("device %p, stream_idx %u, divider %#x.\n", device, stream_idx, divider);
1592 /* Verify input. At least in d3d9 this is invalid. */
1593 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && (divider & WINED3DSTREAMSOURCE_INDEXEDDATA))
1595 WARN("INSTANCEDATA and INDEXEDDATA were set, returning D3DERR_INVALIDCALL.\n");
1596 return WINED3DERR_INVALIDCALL;
1598 if ((divider & WINED3DSTREAMSOURCE_INSTANCEDATA) && !stream_idx)
1600 WARN("INSTANCEDATA used on stream 0, returning D3DERR_INVALIDCALL.\n");
1601 return WINED3DERR_INVALIDCALL;
1605 WARN("Divider is 0, returning D3DERR_INVALIDCALL.\n");
1606 return WINED3DERR_INVALIDCALL;
1609 stream = &device->updateStateBlock->state.streams[stream_idx];
1610 old_flags = stream->flags;
1611 old_freq = stream->frequency;
1613 stream->flags = divider & (WINED3DSTREAMSOURCE_INSTANCEDATA | WINED3DSTREAMSOURCE_INDEXEDDATA);
1614 stream->frequency = divider & 0x7fffff;
1616 device->updateStateBlock->changed.streamFreq |= 1 << stream_idx;
1618 if (stream->frequency != old_freq || stream->flags != old_flags)
1619 device_invalidate_state(device, STATE_STREAMSRC);
1624 HRESULT CDECL wined3d_device_get_stream_source_freq(const struct wined3d_device *device,
1625 UINT stream_idx, UINT *divider)
1627 struct wined3d_stream_state *stream;
1629 TRACE("device %p, stream_idx %u, divider %p.\n", device, stream_idx, divider);
1631 stream = &device->updateStateBlock->state.streams[stream_idx];
1632 *divider = stream->flags | stream->frequency;
1634 TRACE("Returning %#x.\n", *divider);
1639 void CDECL wined3d_device_set_transform(struct wined3d_device *device,
1640 enum wined3d_transform_state d3dts, const struct wined3d_matrix *matrix)
1642 TRACE("device %p, state %s, matrix %p.\n",
1643 device, debug_d3dtstype(d3dts), matrix);
1644 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._11, matrix->u.s._12, matrix->u.s._13, matrix->u.s._14);
1645 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._21, matrix->u.s._22, matrix->u.s._23, matrix->u.s._24);
1646 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._31, matrix->u.s._32, matrix->u.s._33, matrix->u.s._34);
1647 TRACE("%.8e %.8e %.8e %.8e\n", matrix->u.s._41, matrix->u.s._42, matrix->u.s._43, matrix->u.s._44);
1649 /* Handle recording of state blocks. */
1650 if (device->isRecordingState)
1652 TRACE("Recording... not performing anything.\n");
1653 device->updateStateBlock->changed.transform[d3dts >> 5] |= 1 << (d3dts & 0x1f);
1654 device->updateStateBlock->state.transforms[d3dts] = *matrix;
1658 /* If the new matrix is the same as the current one,
1659 * we cut off any further processing. this seems to be a reasonable
1660 * optimization because as was noticed, some apps (warcraft3 for example)
1661 * tend towards setting the same matrix repeatedly for some reason.
1663 * From here on we assume that the new matrix is different, wherever it matters. */
1664 if (!memcmp(&device->stateBlock->state.transforms[d3dts].u.m[0][0], matrix, sizeof(*matrix)))
1666 TRACE("The application is setting the same matrix over again.\n");
1670 device->stateBlock->state.transforms[d3dts] = *matrix;
1672 if (d3dts < WINED3D_TS_WORLD_MATRIX(device->adapter->gl_info.limits.blends))
1673 device_invalidate_state(device, STATE_TRANSFORM(d3dts));
1676 void CDECL wined3d_device_get_transform(const struct wined3d_device *device,
1677 enum wined3d_transform_state state, struct wined3d_matrix *matrix)
1679 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1681 *matrix = device->stateBlock->state.transforms[state];
1684 void CDECL wined3d_device_multiply_transform(struct wined3d_device *device,
1685 enum wined3d_transform_state state, const struct wined3d_matrix *matrix)
1687 const struct wined3d_matrix *mat;
1688 struct wined3d_matrix temp;
1690 TRACE("device %p, state %s, matrix %p.\n", device, debug_d3dtstype(state), matrix);
1692 /* Note: Using 'updateStateBlock' rather than 'stateblock' in the code
1693 * below means it will be recorded in a state block change, but it
1694 * works regardless where it is recorded.
1695 * If this is found to be wrong, change to StateBlock. */
1696 if (state > HIGHEST_TRANSFORMSTATE)
1698 WARN("Unhandled transform state %#x.\n", state);
1702 mat = &device->updateStateBlock->state.transforms[state];
1703 multiply_matrix(&temp, mat, matrix);
1705 /* Apply change via set transform - will reapply to eg. lights this way. */
1706 wined3d_device_set_transform(device, state, &temp);
1709 /* Note lights are real special cases. Although the device caps state only
1710 * e.g. 8 are supported, you can reference any indexes you want as long as
1711 * that number max are enabled at any one point in time. Therefore since the
1712 * indices can be anything, we need a hashmap of them. However, this causes
1713 * stateblock problems. When capturing the state block, I duplicate the
1714 * hashmap, but when recording, just build a chain pretty much of commands to
1716 HRESULT CDECL wined3d_device_set_light(struct wined3d_device *device,
1717 UINT light_idx, const struct wined3d_light *light)
1719 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1720 struct wined3d_light_info *object = NULL;
1724 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1726 /* Check the parameter range. Need for speed most wanted sets junk lights
1727 * which confuse the GL driver. */
1729 return WINED3DERR_INVALIDCALL;
1731 switch (light->type)
1733 case WINED3D_LIGHT_POINT:
1734 case WINED3D_LIGHT_SPOT:
1735 case WINED3D_LIGHT_PARALLELPOINT:
1736 case WINED3D_LIGHT_GLSPOT:
1737 /* Incorrect attenuation values can cause the gl driver to crash.
1738 * Happens with Need for speed most wanted. */
1739 if (light->attenuation0 < 0.0f || light->attenuation1 < 0.0f || light->attenuation2 < 0.0f)
1741 WARN("Attenuation is negative, returning WINED3DERR_INVALIDCALL.\n");
1742 return WINED3DERR_INVALIDCALL;
1746 case WINED3D_LIGHT_DIRECTIONAL:
1747 /* Ignores attenuation */
1751 WARN("Light type out of range, returning WINED3DERR_INVALIDCALL\n");
1752 return WINED3DERR_INVALIDCALL;
1755 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1757 object = LIST_ENTRY(e, struct wined3d_light_info, entry);
1758 if (object->OriginalIndex == light_idx)
1765 TRACE("Adding new light\n");
1766 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1768 return E_OUTOFMEMORY;
1770 list_add_head(&device->updateStateBlock->state.light_map[hash_idx], &object->entry);
1771 object->glIndex = -1;
1772 object->OriginalIndex = light_idx;
1775 /* Initialize the object. */
1776 TRACE("Light %d setting to type %d, Diffuse(%f,%f,%f,%f), Specular(%f,%f,%f,%f), Ambient(%f,%f,%f,%f)\n",
1777 light_idx, light->type,
1778 light->diffuse.r, light->diffuse.g, light->diffuse.b, light->diffuse.a,
1779 light->specular.r, light->specular.g, light->specular.b, light->specular.a,
1780 light->ambient.r, light->ambient.g, light->ambient.b, light->ambient.a);
1781 TRACE("... Pos(%f,%f,%f), Dir(%f,%f,%f)\n", light->position.x, light->position.y, light->position.z,
1782 light->direction.x, light->direction.y, light->direction.z);
1783 TRACE("... Range(%f), Falloff(%f), Theta(%f), Phi(%f)\n",
1784 light->range, light->falloff, light->theta, light->phi);
1786 /* Save away the information. */
1787 object->OriginalParms = *light;
1789 switch (light->type)
1791 case WINED3D_LIGHT_POINT:
1793 object->lightPosn[0] = light->position.x;
1794 object->lightPosn[1] = light->position.y;
1795 object->lightPosn[2] = light->position.z;
1796 object->lightPosn[3] = 1.0f;
1797 object->cutoff = 180.0f;
1801 case WINED3D_LIGHT_DIRECTIONAL:
1803 object->lightPosn[0] = -light->direction.x;
1804 object->lightPosn[1] = -light->direction.y;
1805 object->lightPosn[2] = -light->direction.z;
1806 object->lightPosn[3] = 0.0f;
1807 object->exponent = 0.0f;
1808 object->cutoff = 180.0f;
1811 case WINED3D_LIGHT_SPOT:
1813 object->lightPosn[0] = light->position.x;
1814 object->lightPosn[1] = light->position.y;
1815 object->lightPosn[2] = light->position.z;
1816 object->lightPosn[3] = 1.0f;
1819 object->lightDirn[0] = light->direction.x;
1820 object->lightDirn[1] = light->direction.y;
1821 object->lightDirn[2] = light->direction.z;
1822 object->lightDirn[3] = 1.0f;
1824 /* opengl-ish and d3d-ish spot lights use too different models
1825 * for the light "intensity" as a function of the angle towards
1826 * the main light direction, so we only can approximate very
1827 * roughly. However, spot lights are rather rarely used in games
1828 * (if ever used at all). Furthermore if still used, probably
1829 * nobody pays attention to such details. */
1830 if (!light->falloff)
1832 /* Falloff = 0 is easy, because d3d's and opengl's spot light
1833 * equations have the falloff resp. exponent parameter as an
1834 * exponent, so the spot light lighting will always be 1.0 for
1835 * both of them, and we don't have to care for the rest of the
1836 * rather complex calculation. */
1837 object->exponent = 0.0f;
1841 rho = light->theta + (light->phi - light->theta) / (2 * light->falloff);
1844 object->exponent = -0.3f / logf(cosf(rho / 2));
1847 if (object->exponent > 128.0f)
1848 object->exponent = 128.0f;
1850 object->cutoff = (float)(light->phi * 90 / M_PI);
1855 FIXME("Unrecognized light type %#x.\n", light->type);
1858 /* Update the live definitions if the light is currently assigned a glIndex. */
1859 if (object->glIndex != -1 && !device->isRecordingState)
1860 device_invalidate_state(device, STATE_ACTIVELIGHT(object->glIndex));
1865 HRESULT CDECL wined3d_device_get_light(const struct wined3d_device *device,
1866 UINT light_idx, struct wined3d_light *light)
1868 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1869 struct wined3d_light_info *light_info = NULL;
1872 TRACE("device %p, light_idx %u, light %p.\n", device, light_idx, light);
1874 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1876 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1877 if (light_info->OriginalIndex == light_idx)
1884 TRACE("Light information requested but light not defined\n");
1885 return WINED3DERR_INVALIDCALL;
1888 *light = light_info->OriginalParms;
1892 HRESULT CDECL wined3d_device_set_light_enable(struct wined3d_device *device, UINT light_idx, BOOL enable)
1894 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1895 struct wined3d_light_info *light_info = NULL;
1898 TRACE("device %p, light_idx %u, enable %#x.\n", device, light_idx, enable);
1900 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1902 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1903 if (light_info->OriginalIndex == light_idx)
1907 TRACE("Found light %p.\n", light_info);
1909 /* Special case - enabling an undefined light creates one with a strict set of parameters. */
1912 TRACE("Light enabled requested but light not defined, so defining one!\n");
1913 wined3d_device_set_light(device, light_idx, &WINED3D_default_light);
1915 /* Search for it again! Should be fairly quick as near head of list. */
1916 LIST_FOR_EACH(e, &device->updateStateBlock->state.light_map[hash_idx])
1918 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1919 if (light_info->OriginalIndex == light_idx)
1925 FIXME("Adding default lights has failed dismally\n");
1926 return WINED3DERR_INVALIDCALL;
1932 if (light_info->glIndex != -1)
1934 if (!device->isRecordingState)
1935 device_invalidate_state(device, STATE_ACTIVELIGHT(light_info->glIndex));
1937 device->updateStateBlock->state.lights[light_info->glIndex] = NULL;
1938 light_info->glIndex = -1;
1942 TRACE("Light already disabled, nothing to do\n");
1944 light_info->enabled = FALSE;
1948 light_info->enabled = TRUE;
1949 if (light_info->glIndex != -1)
1951 TRACE("Nothing to do as light was enabled\n");
1956 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1957 /* Find a free GL light. */
1958 for (i = 0; i < gl_info->limits.lights; ++i)
1960 if (!device->updateStateBlock->state.lights[i])
1962 device->updateStateBlock->state.lights[i] = light_info;
1963 light_info->glIndex = i;
1967 if (light_info->glIndex == -1)
1969 /* Our tests show that Windows returns D3D_OK in this situation, even with
1970 * D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE devices. This
1971 * is consistent among ddraw, d3d8 and d3d9. GetLightEnable returns TRUE
1972 * as well for those lights.
1974 * TODO: Test how this affects rendering. */
1975 WARN("Too many concurrently active lights\n");
1979 /* i == light_info->glIndex */
1980 if (!device->isRecordingState)
1981 device_invalidate_state(device, STATE_ACTIVELIGHT(i));
1988 HRESULT CDECL wined3d_device_get_light_enable(const struct wined3d_device *device, UINT light_idx, BOOL *enable)
1990 UINT hash_idx = LIGHTMAP_HASHFUNC(light_idx);
1991 struct wined3d_light_info *light_info = NULL;
1994 TRACE("device %p, light_idx %u, enable %p.\n", device, light_idx, enable);
1996 LIST_FOR_EACH(e, &device->stateBlock->state.light_map[hash_idx])
1998 light_info = LIST_ENTRY(e, struct wined3d_light_info, entry);
1999 if (light_info->OriginalIndex == light_idx)
2006 TRACE("Light enabled state requested but light not defined.\n");
2007 return WINED3DERR_INVALIDCALL;
2009 /* true is 128 according to SetLightEnable */
2010 *enable = light_info->enabled ? 128 : 0;
2014 HRESULT CDECL wined3d_device_set_clip_plane(struct wined3d_device *device,
2015 UINT plane_idx, const struct wined3d_vec4 *plane)
2017 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2019 /* Validate plane_idx. */
2020 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2022 TRACE("Application has requested clipplane this device doesn't support.\n");
2023 return WINED3DERR_INVALIDCALL;
2026 device->updateStateBlock->changed.clipplane |= 1 << plane_idx;
2028 if (!memcmp(&device->updateStateBlock->state.clip_planes[plane_idx], plane, sizeof(*plane)))
2030 TRACE("Application is setting old values over, nothing to do.\n");
2034 device->updateStateBlock->state.clip_planes[plane_idx] = *plane;
2036 /* Handle recording of state blocks. */
2037 if (device->isRecordingState)
2039 TRACE("Recording... not performing anything.\n");
2043 device_invalidate_state(device, STATE_CLIPPLANE(plane_idx));
2048 HRESULT CDECL wined3d_device_get_clip_plane(const struct wined3d_device *device,
2049 UINT plane_idx, struct wined3d_vec4 *plane)
2051 TRACE("device %p, plane_idx %u, plane %p.\n", device, plane_idx, plane);
2053 /* Validate plane_idx. */
2054 if (plane_idx >= device->adapter->gl_info.limits.clipplanes)
2056 TRACE("Application has requested clipplane this device doesn't support.\n");
2057 return WINED3DERR_INVALIDCALL;
2060 *plane = device->stateBlock->state.clip_planes[plane_idx];
2065 HRESULT CDECL wined3d_device_set_clip_status(struct wined3d_device *device,
2066 const struct wined3d_clip_status *clip_status)
2068 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2071 return WINED3DERR_INVALIDCALL;
2076 HRESULT CDECL wined3d_device_get_clip_status(const struct wined3d_device *device,
2077 struct wined3d_clip_status *clip_status)
2079 FIXME("device %p, clip_status %p stub!\n", device, clip_status);
2082 return WINED3DERR_INVALIDCALL;
2087 void CDECL wined3d_device_set_material(struct wined3d_device *device, const struct wined3d_material *material)
2089 TRACE("device %p, material %p.\n", device, material);
2091 device->updateStateBlock->changed.material = TRUE;
2092 device->updateStateBlock->state.material = *material;
2094 /* Handle recording of state blocks */
2095 if (device->isRecordingState)
2097 TRACE("Recording... not performing anything.\n");
2101 device_invalidate_state(device, STATE_MATERIAL);
2104 void CDECL wined3d_device_get_material(const struct wined3d_device *device, struct wined3d_material *material)
2106 TRACE("device %p, material %p.\n", device, material);
2108 *material = device->updateStateBlock->state.material;
2110 TRACE("diffuse {%.8e, %.8e, %.8e, %.8e}\n",
2111 material->diffuse.r, material->diffuse.g,
2112 material->diffuse.b, material->diffuse.a);
2113 TRACE("ambient {%.8e, %.8e, %.8e, %.8e}\n",
2114 material->ambient.r, material->ambient.g,
2115 material->ambient.b, material->ambient.a);
2116 TRACE("specular {%.8e, %.8e, %.8e, %.8e}\n",
2117 material->specular.r, material->specular.g,
2118 material->specular.b, material->specular.a);
2119 TRACE("emissive {%.8e, %.8e, %.8e, %.8e}\n",
2120 material->emissive.r, material->emissive.g,
2121 material->emissive.b, material->emissive.a);
2122 TRACE("power %.8e.\n", material->power);
2125 void CDECL wined3d_device_set_index_buffer(struct wined3d_device *device,
2126 struct wined3d_buffer *buffer, enum wined3d_format_id format_id)
2128 struct wined3d_buffer *prev_buffer;
2130 TRACE("device %p, buffer %p, format %s.\n",
2131 device, buffer, debug_d3dformat(format_id));
2133 prev_buffer = device->updateStateBlock->state.index_buffer;
2135 device->updateStateBlock->changed.indices = TRUE;
2136 device->updateStateBlock->state.index_buffer = buffer;
2137 device->updateStateBlock->state.index_format = format_id;
2139 /* Handle recording of state blocks. */
2140 if (device->isRecordingState)
2142 TRACE("Recording... not performing anything.\n");
2144 wined3d_buffer_incref(buffer);
2146 wined3d_buffer_decref(prev_buffer);
2150 if (prev_buffer != buffer)
2152 device_invalidate_state(device, STATE_INDEXBUFFER);
2155 InterlockedIncrement(&buffer->resource.bind_count);
2156 wined3d_buffer_incref(buffer);
2160 InterlockedDecrement(&prev_buffer->resource.bind_count);
2161 wined3d_buffer_decref(prev_buffer);
2166 struct wined3d_buffer * CDECL wined3d_device_get_index_buffer(const struct wined3d_device *device,
2167 enum wined3d_format_id *format)
2169 TRACE("device %p, format %p.\n", device, format);
2171 *format = device->stateBlock->state.index_format;
2172 return device->stateBlock->state.index_buffer;
2175 void CDECL wined3d_device_set_base_vertex_index(struct wined3d_device *device, INT base_index)
2177 TRACE("device %p, base_index %d.\n", device, base_index);
2179 device->updateStateBlock->state.base_vertex_index = base_index;
2182 INT CDECL wined3d_device_get_base_vertex_index(const struct wined3d_device *device)
2184 TRACE("device %p.\n", device);
2186 return device->stateBlock->state.base_vertex_index;
2189 void CDECL wined3d_device_set_viewport(struct wined3d_device *device, const struct wined3d_viewport *viewport)
2191 TRACE("device %p, viewport %p.\n", device, viewport);
2192 TRACE("x %u, y %u, w %u, h %u, min_z %.8e, max_z %.8e.\n",
2193 viewport->x, viewport->y, viewport->width, viewport->height, viewport->min_z, viewport->max_z);
2195 device->updateStateBlock->changed.viewport = TRUE;
2196 device->updateStateBlock->state.viewport = *viewport;
2198 /* Handle recording of state blocks */
2199 if (device->isRecordingState)
2201 TRACE("Recording... not performing anything\n");
2205 device_invalidate_state(device, STATE_VIEWPORT);
2208 void CDECL wined3d_device_get_viewport(const struct wined3d_device *device, struct wined3d_viewport *viewport)
2210 TRACE("device %p, viewport %p.\n", device, viewport);
2212 *viewport = device->stateBlock->state.viewport;
2215 static void resolve_depth_buffer(struct wined3d_state *state)
2217 struct wined3d_texture *texture = state->textures[0];
2218 struct wined3d_surface *depth_stencil, *surface;
2220 if (!texture || texture->resource.type != WINED3D_RTYPE_TEXTURE
2221 || !(texture->resource.format->flags & WINED3DFMT_FLAG_DEPTH))
2223 surface = surface_from_resource(texture->sub_resources[0]);
2224 depth_stencil = state->fb->depth_stencil;
2228 wined3d_surface_blt(surface, NULL, depth_stencil, NULL, 0, NULL, WINED3D_TEXF_POINT);
2231 void CDECL wined3d_device_set_render_state(struct wined3d_device *device,
2232 enum wined3d_render_state state, DWORD value)
2234 DWORD old_value = device->stateBlock->state.render_states[state];
2236 TRACE("device %p, state %s (%#x), value %#x.\n", device, debug_d3drenderstate(state), state, value);
2238 device->updateStateBlock->changed.renderState[state >> 5] |= 1 << (state & 0x1f);
2239 device->updateStateBlock->state.render_states[state] = value;
2241 /* Handle recording of state blocks. */
2242 if (device->isRecordingState)
2244 TRACE("Recording... not performing anything.\n");
2248 /* Compared here and not before the assignment to allow proper stateblock recording. */
2249 if (value == old_value)
2250 TRACE("Application is setting the old value over, nothing to do.\n");
2252 device_invalidate_state(device, STATE_RENDER(state));
2254 if (state == WINED3D_RS_POINTSIZE && value == WINED3D_RESZ_CODE)
2256 TRACE("RESZ multisampled depth buffer resolve triggered.\n");
2257 resolve_depth_buffer(&device->stateBlock->state);
2261 DWORD CDECL wined3d_device_get_render_state(const struct wined3d_device *device, enum wined3d_render_state state)
2263 TRACE("device %p, state %s (%#x).\n", device, debug_d3drenderstate(state), state);
2265 return device->stateBlock->state.render_states[state];
2268 void CDECL wined3d_device_set_sampler_state(struct wined3d_device *device,
2269 UINT sampler_idx, enum wined3d_sampler_state state, DWORD value)
2273 TRACE("device %p, sampler_idx %u, state %s, value %#x.\n",
2274 device, sampler_idx, debug_d3dsamplerstate(state), value);
2276 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2277 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2279 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2280 / sizeof(*device->stateBlock->state.sampler_states))
2282 WARN("Invalid sampler %u.\n", sampler_idx);
2283 return; /* Windows accepts overflowing this array ... we do not. */
2286 old_value = device->stateBlock->state.sampler_states[sampler_idx][state];
2287 device->updateStateBlock->state.sampler_states[sampler_idx][state] = value;
2288 device->updateStateBlock->changed.samplerState[sampler_idx] |= 1 << state;
2290 /* Handle recording of state blocks. */
2291 if (device->isRecordingState)
2293 TRACE("Recording... not performing anything.\n");
2297 if (old_value == value)
2299 TRACE("Application is setting the old value over, nothing to do.\n");
2303 device_invalidate_state(device, STATE_SAMPLER(sampler_idx));
2306 DWORD CDECL wined3d_device_get_sampler_state(const struct wined3d_device *device,
2307 UINT sampler_idx, enum wined3d_sampler_state state)
2309 TRACE("device %p, sampler_idx %u, state %s.\n",
2310 device, sampler_idx, debug_d3dsamplerstate(state));
2312 if (sampler_idx >= WINED3DVERTEXTEXTURESAMPLER0 && sampler_idx <= WINED3DVERTEXTEXTURESAMPLER3)
2313 sampler_idx -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
2315 if (sampler_idx >= sizeof(device->stateBlock->state.sampler_states)
2316 / sizeof(*device->stateBlock->state.sampler_states))
2318 WARN("Invalid sampler %u.\n", sampler_idx);
2319 return 0; /* Windows accepts overflowing this array ... we do not. */
2322 return device->stateBlock->state.sampler_states[sampler_idx][state];
2325 void CDECL wined3d_device_set_scissor_rect(struct wined3d_device *device, const RECT *rect)
2327 TRACE("device %p, rect %s.\n", device, wine_dbgstr_rect(rect));
2329 device->updateStateBlock->changed.scissorRect = TRUE;
2330 if (EqualRect(&device->updateStateBlock->state.scissor_rect, rect))
2332 TRACE("App is setting the old scissor rectangle over, nothing to do.\n");
2335 CopyRect(&device->updateStateBlock->state.scissor_rect, rect);
2337 if (device->isRecordingState)
2339 TRACE("Recording... not performing anything.\n");
2343 device_invalidate_state(device, STATE_SCISSORRECT);
2346 void CDECL wined3d_device_get_scissor_rect(const struct wined3d_device *device, RECT *rect)
2348 TRACE("device %p, rect %p.\n", device, rect);
2350 *rect = device->updateStateBlock->state.scissor_rect;
2351 TRACE("Returning rect %s.\n", wine_dbgstr_rect(rect));
2354 void CDECL wined3d_device_set_vertex_declaration(struct wined3d_device *device,
2355 struct wined3d_vertex_declaration *declaration)
2357 struct wined3d_vertex_declaration *prev = device->updateStateBlock->state.vertex_declaration;
2359 TRACE("device %p, declaration %p.\n", device, declaration);
2362 wined3d_vertex_declaration_incref(declaration);
2364 wined3d_vertex_declaration_decref(prev);
2366 device->updateStateBlock->state.vertex_declaration = declaration;
2367 device->updateStateBlock->changed.vertexDecl = TRUE;
2369 if (device->isRecordingState)
2371 TRACE("Recording... not performing anything.\n");
2375 if (declaration == prev)
2377 /* Checked after the assignment to allow proper stateblock recording. */
2378 TRACE("Application is setting the old declaration over, nothing to do.\n");
2382 device_invalidate_state(device, STATE_VDECL);
2385 struct wined3d_vertex_declaration * CDECL wined3d_device_get_vertex_declaration(const struct wined3d_device *device)
2387 TRACE("device %p.\n", device);
2389 return device->stateBlock->state.vertex_declaration;
2392 void CDECL wined3d_device_set_vertex_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2394 struct wined3d_shader *prev = device->updateStateBlock->state.vertex_shader;
2396 TRACE("device %p, shader %p.\n", device, shader);
2399 wined3d_shader_incref(shader);
2401 wined3d_shader_decref(prev);
2403 device->updateStateBlock->state.vertex_shader = shader;
2404 device->updateStateBlock->changed.vertexShader = TRUE;
2406 if (device->isRecordingState)
2408 TRACE("Recording... not performing anything.\n");
2414 TRACE("Application is setting the old shader over, nothing to do.\n");
2418 device_invalidate_state(device, STATE_VSHADER);
2421 struct wined3d_shader * CDECL wined3d_device_get_vertex_shader(const struct wined3d_device *device)
2423 TRACE("device %p.\n", device);
2425 return device->stateBlock->state.vertex_shader;
2428 void CDECL wined3d_device_set_vs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2430 struct wined3d_buffer *prev;
2432 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2434 if (idx >= MAX_CONSTANT_BUFFERS)
2436 WARN("Invalid constant buffer index %u.\n", idx);
2440 prev = device->updateStateBlock->state.vs_cb[idx];
2441 device->updateStateBlock->state.vs_cb[idx] = buffer;
2443 if (device->isRecordingState)
2446 wined3d_buffer_incref(buffer);
2448 wined3d_buffer_decref(prev);
2456 InterlockedIncrement(&buffer->resource.bind_count);
2457 wined3d_buffer_incref(buffer);
2461 InterlockedDecrement(&prev->resource.bind_count);
2462 wined3d_buffer_decref(prev);
2467 struct wined3d_buffer * CDECL wined3d_device_get_vs_cb(const struct wined3d_device *device, UINT idx)
2469 TRACE("device %p, idx %u.\n", device, idx);
2471 if (idx >= MAX_CONSTANT_BUFFERS)
2473 WARN("Invalid constant buffer index %u.\n", idx);
2477 return device->stateBlock->state.vs_cb[idx];
2480 void CDECL wined3d_device_set_vs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2482 struct wined3d_sampler *prev;
2484 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2486 if (idx >= MAX_SAMPLER_OBJECTS)
2488 WARN("Invalid sampler index %u.\n", idx);
2492 prev = device->updateStateBlock->state.vs_sampler[idx];
2493 device->updateStateBlock->state.vs_sampler[idx] = sampler;
2496 wined3d_sampler_incref(sampler);
2498 wined3d_sampler_decref(prev);
2501 struct wined3d_sampler * CDECL wined3d_device_get_vs_sampler(const struct wined3d_device *device, UINT idx)
2503 TRACE("device %p, idx %u.\n", device, idx);
2505 if (idx >= MAX_SAMPLER_OBJECTS)
2507 WARN("Invalid sampler index %u.\n", idx);
2511 return device->stateBlock->state.vs_sampler[idx];
2514 HRESULT CDECL wined3d_device_set_vs_consts_b(struct wined3d_device *device,
2515 UINT start_register, const BOOL *constants, UINT bool_count)
2517 UINT count = min(bool_count, MAX_CONST_B - start_register);
2520 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2521 device, start_register, constants, bool_count);
2523 if (!constants || start_register >= MAX_CONST_B)
2524 return WINED3DERR_INVALIDCALL;
2526 memcpy(&device->updateStateBlock->state.vs_consts_b[start_register], constants, count * sizeof(BOOL));
2527 for (i = 0; i < count; ++i)
2528 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
2530 for (i = start_register; i < count + start_register; ++i)
2531 device->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
2533 if (!device->isRecordingState)
2534 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2539 HRESULT CDECL wined3d_device_get_vs_consts_b(const struct wined3d_device *device,
2540 UINT start_register, BOOL *constants, UINT bool_count)
2542 UINT count = min(bool_count, MAX_CONST_B - start_register);
2544 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2545 device, start_register, constants, bool_count);
2547 if (!constants || start_register >= MAX_CONST_B)
2548 return WINED3DERR_INVALIDCALL;
2550 memcpy(constants, &device->stateBlock->state.vs_consts_b[start_register], count * sizeof(BOOL));
2555 HRESULT CDECL wined3d_device_set_vs_consts_i(struct wined3d_device *device,
2556 UINT start_register, const int *constants, UINT vector4i_count)
2558 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2561 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2562 device, start_register, constants, vector4i_count);
2564 if (!constants || start_register >= MAX_CONST_I)
2565 return WINED3DERR_INVALIDCALL;
2567 memcpy(&device->updateStateBlock->state.vs_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
2568 for (i = 0; i < count; ++i)
2569 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
2570 constants[i * 4], constants[i * 4 + 1],
2571 constants[i * 4 + 2], constants[i * 4 + 3]);
2573 for (i = start_register; i < count + start_register; ++i)
2574 device->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
2576 if (!device->isRecordingState)
2577 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2582 HRESULT CDECL wined3d_device_get_vs_consts_i(const struct wined3d_device *device,
2583 UINT start_register, int *constants, UINT vector4i_count)
2585 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
2587 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
2588 device, start_register, constants, vector4i_count);
2590 if (!constants || start_register >= MAX_CONST_I)
2591 return WINED3DERR_INVALIDCALL;
2593 memcpy(constants, &device->stateBlock->state.vs_consts_i[start_register * 4], count * sizeof(int) * 4);
2597 HRESULT CDECL wined3d_device_set_vs_consts_f(struct wined3d_device *device,
2598 UINT start_register, const float *constants, UINT vector4f_count)
2601 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2603 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2604 device, start_register, constants, vector4f_count);
2606 /* Specifically test start_register > limit to catch MAX_UINT overflows
2607 * when adding start_register + vector4f_count. */
2609 || start_register + vector4f_count > d3d_info->limits.vs_uniform_count
2610 || start_register > d3d_info->limits.vs_uniform_count)
2611 return WINED3DERR_INVALIDCALL;
2613 memcpy(&device->updateStateBlock->state.vs_consts_f[start_register * 4],
2614 constants, vector4f_count * sizeof(float) * 4);
2617 for (i = 0; i < vector4f_count; ++i)
2618 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
2619 constants[i * 4], constants[i * 4 + 1],
2620 constants[i * 4 + 2], constants[i * 4 + 3]);
2623 if (!device->isRecordingState)
2625 device->shader_backend->shader_update_float_vertex_constants(device, start_register, vector4f_count);
2626 device_invalidate_state(device, STATE_VERTEXSHADERCONSTANT);
2629 memset(device->updateStateBlock->changed.vertexShaderConstantsF + start_register, 1,
2630 sizeof(*device->updateStateBlock->changed.vertexShaderConstantsF) * vector4f_count);
2635 HRESULT CDECL wined3d_device_get_vs_consts_f(const struct wined3d_device *device,
2636 UINT start_register, float *constants, UINT vector4f_count)
2638 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2639 int count = min(vector4f_count, d3d_info->limits.vs_uniform_count - start_register);
2641 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
2642 device, start_register, constants, vector4f_count);
2644 if (!constants || count < 0)
2645 return WINED3DERR_INVALIDCALL;
2647 memcpy(constants, &device->stateBlock->state.vs_consts_f[start_register * 4], count * sizeof(float) * 4);
2652 static void device_invalidate_texture_stage(const struct wined3d_device *device, DWORD stage)
2656 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
2658 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, i));
2662 static void device_map_stage(struct wined3d_device *device, DWORD stage, DWORD unit)
2664 DWORD i = device->rev_tex_unit_map[unit];
2665 DWORD j = device->texUnitMap[stage];
2667 device->texUnitMap[stage] = unit;
2668 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
2669 device->texUnitMap[i] = WINED3D_UNMAPPED_STAGE;
2671 device->rev_tex_unit_map[unit] = stage;
2672 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
2673 device->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
2676 static void device_update_fixed_function_usage_map(struct wined3d_device *device)
2680 device->fixed_function_usage_map = 0;
2681 for (i = 0; i < MAX_TEXTURES; ++i)
2683 const struct wined3d_state *state = &device->stateBlock->state;
2684 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
2685 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
2686 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
2687 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
2688 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
2689 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
2690 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
2691 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
2693 /* Not used, and disable higher stages. */
2694 if (color_op == WINED3D_TOP_DISABLE)
2697 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
2698 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
2699 || ((color_arg3 == WINED3DTA_TEXTURE)
2700 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
2701 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
2702 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
2703 || ((alpha_arg3 == WINED3DTA_TEXTURE)
2704 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
2705 device->fixed_function_usage_map |= (1 << i);
2707 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
2708 && i < MAX_TEXTURES - 1)
2709 device->fixed_function_usage_map |= (1 << (i + 1));
2713 static void device_map_fixed_function_samplers(struct wined3d_device *device, const struct wined3d_d3d_info *d3d_info)
2715 unsigned int i, tex;
2718 device_update_fixed_function_usage_map(device);
2719 ffu_map = device->fixed_function_usage_map;
2721 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
2722 || device->stateBlock->state.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
2724 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2726 if (!(ffu_map & 1)) continue;
2728 if (device->texUnitMap[i] != i)
2730 device_map_stage(device, i, i);
2731 device_invalidate_state(device, STATE_SAMPLER(i));
2732 device_invalidate_texture_stage(device, i);
2738 /* Now work out the mapping */
2740 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
2742 if (!(ffu_map & 1)) continue;
2744 if (device->texUnitMap[i] != tex)
2746 device_map_stage(device, i, tex);
2747 device_invalidate_state(device, STATE_SAMPLER(i));
2748 device_invalidate_texture_stage(device, i);
2755 static void device_map_psamplers(struct wined3d_device *device, const struct wined3d_d3d_info *d3d_info)
2757 const enum wined3d_sampler_texture_type *sampler_type =
2758 device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2761 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
2763 if (sampler_type[i] && device->texUnitMap[i] != i)
2765 device_map_stage(device, i, i);
2766 device_invalidate_state(device, STATE_SAMPLER(i));
2767 if (i < d3d_info->limits.ffp_blend_stages)
2768 device_invalidate_texture_stage(device, i);
2773 static BOOL device_unit_free_for_vs(const struct wined3d_device *device,
2774 const enum wined3d_sampler_texture_type *pshader_sampler_tokens,
2775 const enum wined3d_sampler_texture_type *vshader_sampler_tokens, DWORD unit)
2777 DWORD current_mapping = device->rev_tex_unit_map[unit];
2779 /* Not currently used */
2780 if (current_mapping == WINED3D_UNMAPPED_STAGE) return TRUE;
2782 if (current_mapping < MAX_FRAGMENT_SAMPLERS) {
2783 /* Used by a fragment sampler */
2785 if (!pshader_sampler_tokens) {
2786 /* No pixel shader, check fixed function */
2787 return current_mapping >= MAX_TEXTURES || !(device->fixed_function_usage_map & (1 << current_mapping));
2790 /* Pixel shader, check the shader's sampler map */
2791 return !pshader_sampler_tokens[current_mapping];
2794 /* Used by a vertex sampler */
2795 return !vshader_sampler_tokens[current_mapping - MAX_FRAGMENT_SAMPLERS];
2798 static void device_map_vsamplers(struct wined3d_device *device, BOOL ps, const struct wined3d_gl_info *gl_info)
2800 const enum wined3d_sampler_texture_type *vshader_sampler_type =
2801 device->stateBlock->state.vertex_shader->reg_maps.sampler_type;
2802 const enum wined3d_sampler_texture_type *pshader_sampler_type = NULL;
2803 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers) - 1;
2808 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2809 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2810 pshader_sampler_type = device->stateBlock->state.pixel_shader->reg_maps.sampler_type;
2813 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
2814 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
2815 if (vshader_sampler_type[i])
2817 if (device->texUnitMap[vsampler_idx] != WINED3D_UNMAPPED_STAGE)
2819 /* Already mapped somewhere */
2825 if (device_unit_free_for_vs(device, pshader_sampler_type, vshader_sampler_type, start))
2827 device_map_stage(device, vsampler_idx, start);
2828 device_invalidate_state(device, STATE_SAMPLER(vsampler_idx));
2840 void device_update_tex_unit_map(struct wined3d_device *device)
2842 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2843 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2844 const struct wined3d_state *state = &device->stateBlock->state;
2845 BOOL vs = use_vs(state);
2846 BOOL ps = use_ps(state);
2849 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2850 * that would be really messy and require shader recompilation
2851 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2852 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2855 device_map_psamplers(device, d3d_info);
2857 device_map_fixed_function_samplers(device, d3d_info);
2860 device_map_vsamplers(device, ps, gl_info);
2863 void CDECL wined3d_device_set_pixel_shader(struct wined3d_device *device, struct wined3d_shader *shader)
2865 struct wined3d_shader *prev = device->updateStateBlock->state.pixel_shader;
2867 TRACE("device %p, shader %p.\n", device, shader);
2870 wined3d_shader_incref(shader);
2872 wined3d_shader_decref(prev);
2874 device->updateStateBlock->state.pixel_shader = shader;
2875 device->updateStateBlock->changed.pixelShader = TRUE;
2877 if (device->isRecordingState)
2879 TRACE("Recording... not performing anything.\n");
2885 TRACE("Application is setting the old shader over, nothing to do.\n");
2889 device_invalidate_state(device, STATE_PIXELSHADER);
2892 struct wined3d_shader * CDECL wined3d_device_get_pixel_shader(const struct wined3d_device *device)
2894 TRACE("device %p.\n", device);
2896 return device->stateBlock->state.pixel_shader;
2899 void CDECL wined3d_device_set_ps_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
2901 struct wined3d_buffer *prev;
2903 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
2905 if (idx >= MAX_CONSTANT_BUFFERS)
2907 WARN("Invalid constant buffer index %u.\n", idx);
2911 prev = device->updateStateBlock->state.ps_cb[idx];
2912 device->updateStateBlock->state.ps_cb[idx] = buffer;
2914 if (device->isRecordingState)
2917 wined3d_buffer_incref(buffer);
2919 wined3d_buffer_decref(prev);
2927 InterlockedIncrement(&buffer->resource.bind_count);
2928 wined3d_buffer_incref(buffer);
2932 InterlockedDecrement(&prev->resource.bind_count);
2933 wined3d_buffer_decref(prev);
2938 struct wined3d_buffer * CDECL wined3d_device_get_ps_cb(const struct wined3d_device *device, UINT idx)
2940 TRACE("device %p, idx %u.\n", device, idx);
2942 if (idx >= MAX_CONSTANT_BUFFERS)
2944 WARN("Invalid constant buffer index %u.\n", idx);
2948 return device->stateBlock->state.ps_cb[idx];
2951 void CDECL wined3d_device_set_ps_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
2953 struct wined3d_sampler *prev;
2955 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
2957 if (idx >= MAX_SAMPLER_OBJECTS)
2959 WARN("Invalid sampler index %u.\n", idx);
2963 prev = device->updateStateBlock->state.ps_sampler[idx];
2964 device->updateStateBlock->state.ps_sampler[idx] = sampler;
2967 wined3d_sampler_incref(sampler);
2969 wined3d_sampler_decref(prev);
2972 struct wined3d_sampler * CDECL wined3d_device_get_ps_sampler(const struct wined3d_device *device, UINT idx)
2974 TRACE("device %p, idx %u.\n", device, idx);
2976 if (idx >= MAX_SAMPLER_OBJECTS)
2978 WARN("Invalid sampler index %u.\n", idx);
2982 return device->stateBlock->state.ps_sampler[idx];
2985 HRESULT CDECL wined3d_device_set_ps_consts_b(struct wined3d_device *device,
2986 UINT start_register, const BOOL *constants, UINT bool_count)
2988 UINT count = min(bool_count, MAX_CONST_B - start_register);
2991 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
2992 device, start_register, constants, bool_count);
2994 if (!constants || start_register >= MAX_CONST_B)
2995 return WINED3DERR_INVALIDCALL;
2997 memcpy(&device->updateStateBlock->state.ps_consts_b[start_register], constants, count * sizeof(BOOL));
2998 for (i = 0; i < count; ++i)
2999 TRACE("Set BOOL constant %u to %s.\n", start_register + i, constants[i] ? "true" : "false");
3001 for (i = start_register; i < count + start_register; ++i)
3002 device->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
3004 if (!device->isRecordingState)
3005 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3010 HRESULT CDECL wined3d_device_get_ps_consts_b(const struct wined3d_device *device,
3011 UINT start_register, BOOL *constants, UINT bool_count)
3013 UINT count = min(bool_count, MAX_CONST_B - start_register);
3015 TRACE("device %p, start_register %u, constants %p, bool_count %u.\n",
3016 device, start_register, constants, bool_count);
3018 if (!constants || start_register >= MAX_CONST_B)
3019 return WINED3DERR_INVALIDCALL;
3021 memcpy(constants, &device->stateBlock->state.ps_consts_b[start_register], count * sizeof(BOOL));
3026 HRESULT CDECL wined3d_device_set_ps_consts_i(struct wined3d_device *device,
3027 UINT start_register, const int *constants, UINT vector4i_count)
3029 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3032 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3033 device, start_register, constants, vector4i_count);
3035 if (!constants || start_register >= MAX_CONST_I)
3036 return WINED3DERR_INVALIDCALL;
3038 memcpy(&device->updateStateBlock->state.ps_consts_i[start_register * 4], constants, count * sizeof(int) * 4);
3039 for (i = 0; i < count; ++i)
3040 TRACE("Set INT constant %u to {%d, %d, %d, %d}.\n", start_register + i,
3041 constants[i * 4], constants[i * 4 + 1],
3042 constants[i * 4 + 2], constants[i * 4 + 3]);
3044 for (i = start_register; i < count + start_register; ++i)
3045 device->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
3047 if (!device->isRecordingState)
3048 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3053 HRESULT CDECL wined3d_device_get_ps_consts_i(const struct wined3d_device *device,
3054 UINT start_register, int *constants, UINT vector4i_count)
3056 UINT count = min(vector4i_count, MAX_CONST_I - start_register);
3058 TRACE("device %p, start_register %u, constants %p, vector4i_count %u.\n",
3059 device, start_register, constants, vector4i_count);
3061 if (!constants || start_register >= MAX_CONST_I)
3062 return WINED3DERR_INVALIDCALL;
3064 memcpy(constants, &device->stateBlock->state.ps_consts_i[start_register * 4], count * sizeof(int) * 4);
3069 HRESULT CDECL wined3d_device_set_ps_consts_f(struct wined3d_device *device,
3070 UINT start_register, const float *constants, UINT vector4f_count)
3073 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3075 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3076 device, start_register, constants, vector4f_count);
3078 /* Specifically test start_register > limit to catch MAX_UINT overflows
3079 * when adding start_register + vector4f_count. */
3081 || start_register + vector4f_count > d3d_info->limits.ps_uniform_count
3082 || start_register > d3d_info->limits.ps_uniform_count)
3083 return WINED3DERR_INVALIDCALL;
3085 memcpy(&device->updateStateBlock->state.ps_consts_f[start_register * 4],
3086 constants, vector4f_count * sizeof(float) * 4);
3089 for (i = 0; i < vector4f_count; ++i)
3090 TRACE("Set FLOAT constant %u to {%.8e, %.8e, %.8e, %.8e}.\n", start_register + i,
3091 constants[i * 4], constants[i * 4 + 1],
3092 constants[i * 4 + 2], constants[i * 4 + 3]);
3095 if (!device->isRecordingState)
3097 device->shader_backend->shader_update_float_pixel_constants(device, start_register, vector4f_count);
3098 device_invalidate_state(device, STATE_PIXELSHADERCONSTANT);
3101 memset(device->updateStateBlock->changed.pixelShaderConstantsF + start_register, 1,
3102 sizeof(*device->updateStateBlock->changed.pixelShaderConstantsF) * vector4f_count);
3107 HRESULT CDECL wined3d_device_get_ps_consts_f(const struct wined3d_device *device,
3108 UINT start_register, float *constants, UINT vector4f_count)
3110 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3111 int count = min(vector4f_count, d3d_info->limits.ps_uniform_count - start_register);
3113 TRACE("device %p, start_register %u, constants %p, vector4f_count %u.\n",
3114 device, start_register, constants, vector4f_count);
3116 if (!constants || count < 0)
3117 return WINED3DERR_INVALIDCALL;
3119 memcpy(constants, &device->stateBlock->state.ps_consts_f[start_register * 4], count * sizeof(float) * 4);
3124 void CDECL wined3d_device_set_geometry_shader(struct wined3d_device *device, struct wined3d_shader *shader)
3126 struct wined3d_shader *prev = device->updateStateBlock->state.geometry_shader;
3128 TRACE("device %p, shader %p.\n", device, shader);
3131 wined3d_shader_incref(shader);
3133 wined3d_shader_decref(prev);
3135 device->updateStateBlock->state.geometry_shader = shader;
3137 if (device->isRecordingState || shader == prev)
3140 device_invalidate_state(device, STATE_GEOMETRY_SHADER);
3143 struct wined3d_shader * CDECL wined3d_device_get_geometry_shader(const struct wined3d_device *device)
3145 TRACE("device %p.\n", device);
3147 return device->stateBlock->state.geometry_shader;
3150 void CDECL wined3d_device_set_gs_cb(struct wined3d_device *device, UINT idx, struct wined3d_buffer *buffer)
3152 struct wined3d_buffer *prev;
3154 TRACE("device %p, idx %u, buffer %p.\n", device, idx, buffer);
3156 if (idx >= MAX_CONSTANT_BUFFERS)
3158 WARN("Invalid constant buffer index %u.\n", idx);
3162 prev = device->updateStateBlock->state.gs_cb[idx];
3163 device->updateStateBlock->state.gs_cb[idx] = buffer;
3165 if (device->isRecordingState)
3168 wined3d_buffer_incref(buffer);
3170 wined3d_buffer_decref(prev);
3178 InterlockedIncrement(&buffer->resource.bind_count);
3179 wined3d_buffer_incref(buffer);
3183 InterlockedDecrement(&prev->resource.bind_count);
3184 wined3d_buffer_decref(prev);
3189 struct wined3d_buffer * CDECL wined3d_device_get_gs_cb(const struct wined3d_device *device, UINT idx)
3191 TRACE("device %p, idx %u.\n", device, idx);
3193 if (idx >= MAX_CONSTANT_BUFFERS)
3195 WARN("Invalid constant buffer index %u.\n", idx);
3199 return device->stateBlock->state.gs_cb[idx];
3202 void CDECL wined3d_device_set_gs_sampler(struct wined3d_device *device, UINT idx, struct wined3d_sampler *sampler)
3204 struct wined3d_sampler *prev;
3206 TRACE("device %p, idx %u, sampler %p.\n", device, idx, sampler);
3208 if (idx >= MAX_SAMPLER_OBJECTS)
3210 WARN("Invalid sampler index %u.\n", idx);
3214 prev = device->updateStateBlock->state.gs_sampler[idx];
3215 device->updateStateBlock->state.gs_sampler[idx] = sampler;
3218 wined3d_sampler_incref(sampler);
3220 wined3d_sampler_decref(prev);
3223 struct wined3d_sampler * CDECL wined3d_device_get_gs_sampler(const struct wined3d_device *device, UINT idx)
3225 TRACE("device %p, idx %u.\n", device, idx);
3227 if (idx >= MAX_SAMPLER_OBJECTS)
3229 WARN("Invalid sampler index %u.\n", idx);
3233 return device->stateBlock->state.gs_sampler[idx];
3236 /* Context activation is done by the caller. */
3237 /* Do not call while under the GL lock. */
3238 #define copy_and_next(dest, src, size) memcpy(dest, src, size); dest += (size)
3239 static HRESULT process_vertices_strided(const struct wined3d_device *device, DWORD dwDestIndex, DWORD dwCount,
3240 const struct wined3d_stream_info *stream_info, struct wined3d_buffer *dest, DWORD flags,
3243 struct wined3d_matrix mat, proj_mat, view_mat, world_mat;
3244 struct wined3d_viewport vp;
3252 if (stream_info->use_map & (1 << WINED3D_FFP_NORMAL))
3254 WARN(" lighting state not saved yet... Some strange stuff may happen !\n");
3257 if (!(stream_info->use_map & (1 << WINED3D_FFP_POSITION)))
3259 ERR("Source has no position mask\n");
3260 return WINED3DERR_INVALIDCALL;
3263 if (device->stateBlock->state.render_states[WINED3D_RS_CLIPPING])
3265 static BOOL warned = FALSE;
3267 * The clipping code is not quite correct. Some things need
3268 * to be checked against IDirect3DDevice3 (!), d3d8 and d3d9,
3269 * so disable clipping for now.
3270 * (The graphics in Half-Life are broken, and my processvertices
3271 * test crashes with IDirect3DDevice3)
3277 FIXME("Clipping is broken and disabled for now\n");
3283 vertex_size = get_flexible_vertex_size(DestFVF);
3284 if (FAILED(hr = wined3d_buffer_map(dest, dwDestIndex * vertex_size, dwCount * vertex_size, &dest_ptr, 0)))
3286 WARN("Failed to map buffer, hr %#x.\n", hr);
3290 wined3d_device_get_transform(device, WINED3D_TS_VIEW, &view_mat);
3291 wined3d_device_get_transform(device, WINED3D_TS_PROJECTION, &proj_mat);
3292 wined3d_device_get_transform(device, WINED3D_TS_WORLD_MATRIX(0), &world_mat);
3294 TRACE("View mat:\n");
3295 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);
3296 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);
3297 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);
3298 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);
3300 TRACE("Proj mat:\n");
3301 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);
3302 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);
3303 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);
3304 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);
3306 TRACE("World mat:\n");
3307 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);
3308 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);
3309 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);
3310 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);
3312 /* Get the viewport */
3313 wined3d_device_get_viewport(device, &vp);
3314 TRACE("viewport x %u, y %u, width %u, height %u, min_z %.8e, max_z %.8e.\n",
3315 vp.x, vp.y, vp.width, vp.height, vp.min_z, vp.max_z);
3317 multiply_matrix(&mat,&view_mat,&world_mat);
3318 multiply_matrix(&mat,&proj_mat,&mat);
3320 numTextures = (DestFVF & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
3322 for (i = 0; i < dwCount; i+= 1) {
3323 unsigned int tex_index;
3325 if ( ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZ ) ||
3326 ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW ) ) {
3327 /* The position first */
3328 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_POSITION];
3329 const float *p = (const float *)(element->data.addr + i * element->stride);
3331 TRACE("In: ( %06.2f %06.2f %06.2f )\n", p[0], p[1], p[2]);
3333 /* Multiplication with world, view and projection matrix */
3334 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);
3335 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);
3336 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);
3337 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);
3339 TRACE("x=%f y=%f z=%f rhw=%f\n", x, y, z, rhw);
3341 /* WARNING: The following things are taken from d3d7 and were not yet checked
3342 * against d3d8 or d3d9!
3345 /* Clipping conditions: From msdn
3347 * A vertex is clipped if it does not match the following requirements
3351 * 0 < rhw ( Not in d3d7, but tested in d3d7)
3353 * If clipping is on is determined by the D3DVOP_CLIP flag in D3D7, and
3354 * by the D3DRS_CLIPPING in D3D9(according to the msdn, not checked)
3359 ( (-rhw -eps < x) && (-rhw -eps < y) && ( -eps < z) &&
3360 (x <= rhw + eps) && (y <= rhw + eps ) && (z <= rhw + eps) &&
3363 /* "Normal" viewport transformation (not clipped)
3364 * 1) The values are divided by rhw
3365 * 2) The y axis is negative, so multiply it with -1
3366 * 3) Screen coordinates go from -(Width/2) to +(Width/2) and
3367 * -(Height/2) to +(Height/2). The z range is MinZ to MaxZ
3368 * 4) Multiply x with Width/2 and add Width/2
3369 * 5) The same for the height
3370 * 6) Add the viewpoint X and Y to the 2D coordinates and
3371 * The minimum Z value to z
3372 * 7) rhw = 1 / rhw Reciprocal of Homogeneous W....
3374 * Well, basically it's simply a linear transformation into viewport
3386 z *= vp.max_z - vp.min_z;
3388 x += vp.width / 2 + vp.x;
3389 y += vp.height / 2 + vp.y;
3394 /* That vertex got clipped
3395 * Contrary to OpenGL it is not dropped completely, it just
3396 * undergoes a different calculation.
3398 TRACE("Vertex got clipped\n");
3405 /* Msdn mentions that Direct3D9 keeps a list of clipped vertices
3406 * outside of the main vertex buffer memory. That needs some more
3411 TRACE("Writing (%f %f %f) %f\n", x, y, z, rhw);
3414 ( (float *) dest_ptr)[0] = x;
3415 ( (float *) dest_ptr)[1] = y;
3416 ( (float *) dest_ptr)[2] = z;
3417 ( (float *) dest_ptr)[3] = rhw; /* SIC, see ddraw test! */
3419 dest_ptr += 3 * sizeof(float);
3421 if ((DestFVF & WINED3DFVF_POSITION_MASK) == WINED3DFVF_XYZRHW)
3422 dest_ptr += sizeof(float);
3425 if (DestFVF & WINED3DFVF_PSIZE)
3426 dest_ptr += sizeof(DWORD);
3428 if (DestFVF & WINED3DFVF_NORMAL)
3430 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_NORMAL];
3431 const float *normal = (const float *)(element->data.addr + i * element->stride);
3432 /* AFAIK this should go into the lighting information */
3433 FIXME("Didn't expect the destination to have a normal\n");
3434 copy_and_next(dest_ptr, normal, 3 * sizeof(float));
3437 if (DestFVF & WINED3DFVF_DIFFUSE)
3439 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_DIFFUSE];
3440 const DWORD *color_d = (const DWORD *)(element->data.addr + i * element->stride);
3441 if (!(stream_info->use_map & (1 << WINED3D_FFP_DIFFUSE)))
3443 static BOOL warned = FALSE;
3446 ERR("No diffuse color in source, but destination has one\n");
3450 *( (DWORD *) dest_ptr) = 0xffffffff;
3451 dest_ptr += sizeof(DWORD);
3455 copy_and_next(dest_ptr, color_d, sizeof(DWORD));
3459 if (DestFVF & WINED3DFVF_SPECULAR)
3461 /* What's the color value in the feedback buffer? */
3462 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_SPECULAR];
3463 const DWORD *color_s = (const DWORD *)(element->data.addr + i * element->stride);
3464 if (!(stream_info->use_map & (1 << WINED3D_FFP_SPECULAR)))
3466 static BOOL warned = FALSE;
3469 ERR("No specular color in source, but destination has one\n");
3473 *(DWORD *)dest_ptr = 0xff000000;
3474 dest_ptr += sizeof(DWORD);
3478 copy_and_next(dest_ptr, color_s, sizeof(DWORD));
3482 for (tex_index = 0; tex_index < numTextures; ++tex_index)
3484 const struct wined3d_stream_info_element *element = &stream_info->elements[WINED3D_FFP_TEXCOORD0 + tex_index];
3485 const float *tex_coord = (const float *)(element->data.addr + i * element->stride);
3486 if (!(stream_info->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + tex_index))))
3488 ERR("No source texture, but destination requests one\n");
3489 dest_ptr += GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float);
3493 copy_and_next(dest_ptr, tex_coord, GET_TEXCOORD_SIZE_FROM_FVF(DestFVF, tex_index) * sizeof(float));
3498 wined3d_buffer_unmap(dest);
3502 #undef copy_and_next
3504 /* Do not call while under the GL lock. */
3505 HRESULT CDECL wined3d_device_process_vertices(struct wined3d_device *device,
3506 UINT src_start_idx, UINT dst_idx, UINT vertex_count, struct wined3d_buffer *dst_buffer,
3507 const struct wined3d_vertex_declaration *declaration, DWORD flags, DWORD dst_fvf)
3509 struct wined3d_state *state = &device->stateBlock->state;
3510 struct wined3d_stream_info stream_info;
3511 const struct wined3d_gl_info *gl_info;
3512 struct wined3d_context *context;
3513 struct wined3d_shader *vs;
3517 TRACE("device %p, src_start_idx %u, dst_idx %u, vertex_count %u, "
3518 "dst_buffer %p, declaration %p, flags %#x, dst_fvf %#x.\n",
3519 device, src_start_idx, dst_idx, vertex_count,
3520 dst_buffer, declaration, flags, dst_fvf);
3523 FIXME("Output vertex declaration not implemented yet.\n");
3525 /* Need any context to write to the vbo. */
3526 context = context_acquire(device, NULL);
3527 gl_info = context->gl_info;
3529 vs = state->vertex_shader;
3530 state->vertex_shader = NULL;
3531 device_stream_info_from_declaration(device, &stream_info);
3532 state->vertex_shader = vs;
3534 /* We can't convert FROM a VBO, and vertex buffers used to source into
3535 * process_vertices() are unlikely to ever be used for drawing. Release
3536 * VBOs in those buffers and fix up the stream_info structure.
3538 * Also apply the start index. */
3539 for (i = 0; i < (sizeof(stream_info.elements) / sizeof(*stream_info.elements)); ++i)
3541 struct wined3d_stream_info_element *e;
3543 if (!(stream_info.use_map & (1 << i)))
3546 e = &stream_info.elements[i];
3547 if (e->data.buffer_object)
3549 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
3550 e->data.buffer_object = 0;
3551 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
3552 GL_EXTCALL(glDeleteBuffersARB(1, &vb->buffer_object));
3553 vb->buffer_object = 0;
3556 e->data.addr += e->stride * src_start_idx;
3559 hr = process_vertices_strided(device, dst_idx, vertex_count,
3560 &stream_info, dst_buffer, flags, dst_fvf);
3562 context_release(context);
3567 void CDECL wined3d_device_set_texture_stage_state(struct wined3d_device *device,
3568 UINT stage, enum wined3d_texture_stage_state state, DWORD value)
3570 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3573 TRACE("device %p, stage %u, state %s, value %#x.\n",
3574 device, stage, debug_d3dtexturestate(state), value);
3576 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3578 WARN("Invalid state %#x passed.\n", state);
3582 if (stage >= d3d_info->limits.ffp_blend_stages)
3584 WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
3585 stage, d3d_info->limits.ffp_blend_stages - 1);
3589 old_value = device->updateStateBlock->state.texture_states[stage][state];
3590 device->updateStateBlock->changed.textureState[stage] |= 1 << state;
3591 device->updateStateBlock->state.texture_states[stage][state] = value;
3593 if (device->isRecordingState)
3595 TRACE("Recording... not performing anything.\n");
3599 /* Checked after the assignments to allow proper stateblock recording. */
3600 if (old_value == value)
3602 TRACE("Application is setting the old value over, nothing to do.\n");
3606 if (stage > device->stateBlock->state.lowest_disabled_stage
3607 && device->StateTable[STATE_TEXTURESTAGE(0, state)].representative
3608 == STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP))
3610 /* Colorop change above lowest disabled stage? That won't change
3611 * anything in the GL setup. Changes in other states are important on
3612 * disabled stages too. */
3616 if (state == WINED3D_TSS_COLOR_OP)
3620 if (value == WINED3D_TOP_DISABLE && old_value != WINED3D_TOP_DISABLE)
3622 /* Previously enabled stage disabled now. Make sure to dirtify
3623 * all enabled stages above stage, they have to be disabled.
3625 * The current stage is dirtified below. */
3626 for (i = stage + 1; i < device->stateBlock->state.lowest_disabled_stage; ++i)
3628 TRACE("Additionally dirtifying stage %u.\n", i);
3629 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3631 device->stateBlock->state.lowest_disabled_stage = stage;
3632 TRACE("New lowest disabled: %u.\n", stage);
3634 else if (value != WINED3D_TOP_DISABLE && old_value == WINED3D_TOP_DISABLE)
3636 /* Previously disabled stage enabled. Stages above it may need
3637 * enabling. Stage must be lowest_disabled_stage here, if it's
3638 * bigger success is returned above, and stages below the lowest
3639 * disabled stage can't be enabled (because they are enabled
3642 * Again stage stage doesn't need to be dirtified here, it is
3644 for (i = stage + 1; i < d3d_info->limits.ffp_blend_stages; ++i)
3646 if (device->updateStateBlock->state.texture_states[i][WINED3D_TSS_COLOR_OP] == WINED3D_TOP_DISABLE)
3648 TRACE("Additionally dirtifying stage %u due to enable.\n", i);
3649 device_invalidate_state(device, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3651 device->stateBlock->state.lowest_disabled_stage = i;
3652 TRACE("New lowest disabled: %u.\n", i);
3656 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, state));
3659 DWORD CDECL wined3d_device_get_texture_stage_state(const struct wined3d_device *device,
3660 UINT stage, enum wined3d_texture_stage_state state)
3662 TRACE("device %p, stage %u, state %s.\n",
3663 device, stage, debug_d3dtexturestate(state));
3665 if (state > WINED3D_HIGHEST_TEXTURE_STATE)
3667 WARN("Invalid state %#x passed.\n", state);
3671 return device->updateStateBlock->state.texture_states[stage][state];
3674 HRESULT CDECL wined3d_device_set_texture(struct wined3d_device *device,
3675 UINT stage, struct wined3d_texture *texture)
3677 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3678 struct wined3d_texture *prev;
3680 TRACE("device %p, stage %u, texture %p.\n", device, stage, texture);
3682 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3683 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3685 /* Windows accepts overflowing this array... we do not. */
3686 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3688 WARN("Ignoring invalid stage %u.\n", stage);
3692 if (texture && texture->resource.pool == WINED3D_POOL_SCRATCH)
3694 WARN("Rejecting attempt to set scratch texture.\n");
3695 return WINED3DERR_INVALIDCALL;
3698 device->updateStateBlock->changed.textures |= 1 << stage;
3700 prev = device->updateStateBlock->state.textures[stage];
3701 TRACE("Previous texture %p.\n", prev);
3703 if (texture == prev)
3705 TRACE("App is setting the same texture again, nothing to do.\n");
3709 TRACE("Setting new texture to %p.\n", texture);
3710 device->updateStateBlock->state.textures[stage] = texture;
3712 if (device->isRecordingState)
3714 TRACE("Recording... not performing anything\n");
3716 if (texture) wined3d_texture_incref(texture);
3717 if (prev) wined3d_texture_decref(prev);
3724 LONG bind_count = InterlockedIncrement(&texture->resource.bind_count);
3726 wined3d_texture_incref(texture);
3728 if (!prev || texture->target != prev->target)
3729 device_invalidate_state(device, STATE_PIXELSHADER);
3731 if (!prev && stage < d3d_info->limits.ffp_blend_stages)
3733 /* The source arguments for color and alpha ops have different
3734 * meanings when a NULL texture is bound, so the COLOR_OP and
3735 * ALPHA_OP have to be dirtified. */
3736 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3737 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3740 if (bind_count == 1)
3741 texture->sampler = stage;
3746 LONG bind_count = InterlockedDecrement(&prev->resource.bind_count);
3748 if (!texture && stage < d3d_info->limits.ffp_blend_stages)
3750 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_COLOR_OP));
3751 device_invalidate_state(device, STATE_TEXTURESTAGE(stage, WINED3D_TSS_ALPHA_OP));
3754 if (bind_count && prev->sampler == stage)
3758 /* Search for other stages the texture is bound to. Shouldn't
3759 * happen if applications bind textures to a single stage only. */
3760 TRACE("Searching for other stages the texture is bound to.\n");
3761 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
3763 if (device->updateStateBlock->state.textures[i] == prev)
3765 TRACE("Texture is also bound to stage %u.\n", i);
3772 wined3d_texture_decref(prev);
3775 device_invalidate_state(device, STATE_SAMPLER(stage));
3780 struct wined3d_texture * CDECL wined3d_device_get_texture(const struct wined3d_device *device, UINT stage)
3782 TRACE("device %p, stage %u.\n", device, stage);
3784 if (stage >= WINED3DVERTEXTEXTURESAMPLER0 && stage <= WINED3DVERTEXTEXTURESAMPLER3)
3785 stage -= (WINED3DVERTEXTEXTURESAMPLER0 - MAX_FRAGMENT_SAMPLERS);
3787 if (stage >= sizeof(device->stateBlock->state.textures) / sizeof(*device->stateBlock->state.textures))
3789 WARN("Ignoring invalid stage %u.\n", stage);
3790 return NULL; /* Windows accepts overflowing this array ... we do not. */
3793 return device->stateBlock->state.textures[stage];
3796 HRESULT CDECL wined3d_device_get_back_buffer(const struct wined3d_device *device, UINT swapchain_idx,
3797 UINT backbuffer_idx, enum wined3d_backbuffer_type backbuffer_type, struct wined3d_surface **backbuffer)
3799 struct wined3d_swapchain *swapchain;
3801 TRACE("device %p, swapchain_idx %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
3802 device, swapchain_idx, backbuffer_idx, backbuffer_type, backbuffer);
3804 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3805 return WINED3DERR_INVALIDCALL;
3807 if (!(*backbuffer = wined3d_swapchain_get_back_buffer(swapchain, backbuffer_idx, backbuffer_type)))
3808 return WINED3DERR_INVALIDCALL;
3812 HRESULT CDECL wined3d_device_get_device_caps(const struct wined3d_device *device, WINED3DCAPS *caps)
3814 TRACE("device %p, caps %p.\n", device, caps);
3816 return wined3d_get_device_caps(device->wined3d, device->adapter->ordinal,
3817 device->create_parms.device_type, caps);
3820 HRESULT CDECL wined3d_device_get_display_mode(const struct wined3d_device *device, UINT swapchain_idx,
3821 struct wined3d_display_mode *mode, enum wined3d_display_rotation *rotation)
3823 struct wined3d_swapchain *swapchain;
3825 TRACE("device %p, swapchain_idx %u, mode %p, rotation %p.\n",
3826 device, swapchain_idx, mode, rotation);
3828 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
3829 return WINED3DERR_INVALIDCALL;
3831 return wined3d_swapchain_get_display_mode(swapchain, mode, rotation);
3834 HRESULT CDECL wined3d_device_begin_stateblock(struct wined3d_device *device)
3836 struct wined3d_stateblock *stateblock;
3839 TRACE("device %p.\n", device);
3841 if (device->isRecordingState)
3842 return WINED3DERR_INVALIDCALL;
3844 hr = wined3d_stateblock_create(device, WINED3D_SBT_RECORDED, &stateblock);
3848 wined3d_stateblock_decref(device->updateStateBlock);
3849 device->updateStateBlock = stateblock;
3850 device->isRecordingState = TRUE;
3852 TRACE("Recording stateblock %p.\n", stateblock);
3857 HRESULT CDECL wined3d_device_end_stateblock(struct wined3d_device *device,
3858 struct wined3d_stateblock **stateblock)
3860 struct wined3d_stateblock *object = device->updateStateBlock;
3862 TRACE("device %p, stateblock %p.\n", device, stateblock);
3864 if (!device->isRecordingState)
3866 WARN("Not recording.\n");
3868 return WINED3DERR_INVALIDCALL;
3871 stateblock_init_contained_states(object);
3873 *stateblock = object;
3874 device->isRecordingState = FALSE;
3875 device->updateStateBlock = device->stateBlock;
3876 wined3d_stateblock_incref(device->updateStateBlock);
3878 TRACE("Returning stateblock %p.\n", *stateblock);
3883 HRESULT CDECL wined3d_device_begin_scene(struct wined3d_device *device)
3885 /* At the moment we have no need for any functionality at the beginning
3887 TRACE("device %p.\n", device);
3889 if (device->inScene)
3891 WARN("Already in scene, returning WINED3DERR_INVALIDCALL.\n");
3892 return WINED3DERR_INVALIDCALL;
3894 device->inScene = TRUE;
3898 HRESULT CDECL wined3d_device_end_scene(struct wined3d_device *device)
3900 struct wined3d_context *context;
3902 TRACE("device %p.\n", device);
3904 if (!device->inScene)
3906 WARN("Not in scene, returning WINED3DERR_INVALIDCALL.\n");
3907 return WINED3DERR_INVALIDCALL;
3910 context = context_acquire(device, NULL);
3911 /* We only have to do this if we need to read the, swapbuffers performs a flush for us */
3912 context->gl_info->gl_ops.gl.p_glFlush();
3913 /* No checkGLcall here to avoid locking the lock just for checking a call that hardly ever
3915 context_release(context);
3917 device->inScene = FALSE;
3921 HRESULT CDECL wined3d_device_present(const struct wined3d_device *device, const RECT *src_rect,
3922 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region, DWORD flags)
3926 TRACE("device %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
3927 device, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
3928 dst_window_override, dirty_region, flags);
3930 for (i = 0; i < device->swapchain_count; ++i)
3932 wined3d_swapchain_present(device->swapchains[i], src_rect,
3933 dst_rect, dst_window_override, dirty_region, flags);
3939 /* Do not call while under the GL lock. */
3940 HRESULT CDECL wined3d_device_clear(struct wined3d_device *device, DWORD rect_count,
3941 const RECT *rects, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
3945 TRACE("device %p, rect_count %u, rects %p, flags %#x, color {%.8e, %.8e, %.8e, %.8e}, depth %.8e, stencil %u.\n",
3946 device, rect_count, rects, flags, color->r, color->g, color->b, color->a, depth, stencil);
3948 if (!rect_count && rects)
3950 WARN("Rects is %p, but rect_count is 0, ignoring clear\n", rects);
3954 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
3956 struct wined3d_surface *ds = device->fb.depth_stencil;
3959 WARN("Clearing depth and/or stencil without a depth stencil buffer attached, returning WINED3DERR_INVALIDCALL\n");
3960 /* TODO: What about depth stencil buffers without stencil bits? */
3961 return WINED3DERR_INVALIDCALL;
3963 else if (flags & WINED3DCLEAR_TARGET)
3965 if (ds->resource.width < device->fb.render_targets[0]->resource.width
3966 || ds->resource.height < device->fb.render_targets[0]->resource.height)
3968 WARN("Silently ignoring depth and target clear with mismatching sizes\n");
3974 wined3d_get_draw_rect(&device->stateBlock->state, &draw_rect);
3975 device_clear_render_targets(device, device->adapter->gl_info.limits.buffers,
3976 &device->fb, rect_count, rects, &draw_rect, flags, color, depth, stencil);
3981 void CDECL wined3d_device_set_primitive_type(struct wined3d_device *device,
3982 enum wined3d_primitive_type primitive_type)
3984 TRACE("device %p, primitive_type %s\n", device, debug_d3dprimitivetype(primitive_type));
3986 device->updateStateBlock->changed.primitive_type = TRUE;
3987 device->updateStateBlock->state.gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
3990 void CDECL wined3d_device_get_primitive_type(const struct wined3d_device *device,
3991 enum wined3d_primitive_type *primitive_type)
3993 TRACE("device %p, primitive_type %p\n", device, primitive_type);
3995 *primitive_type = d3d_primitive_type_from_gl(device->stateBlock->state.gl_primitive_type);
3997 TRACE("Returning %s\n", debug_d3dprimitivetype(*primitive_type));
4000 HRESULT CDECL wined3d_device_draw_primitive(struct wined3d_device *device, UINT start_vertex, UINT vertex_count)
4002 TRACE("device %p, start_vertex %u, vertex_count %u.\n", device, start_vertex, vertex_count);
4004 if (!device->stateBlock->state.vertex_declaration)
4006 WARN("Called without a valid vertex declaration set.\n");
4007 return WINED3DERR_INVALIDCALL;
4010 if (device->stateBlock->state.load_base_vertex_index)
4012 device->stateBlock->state.load_base_vertex_index = 0;
4013 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4016 /* Account for the loading offset due to index buffers. Instead of
4017 * reloading all sources correct it with the startvertex parameter. */
4018 draw_primitive(device, start_vertex, vertex_count, 0, 0, FALSE);
4022 HRESULT CDECL wined3d_device_draw_indexed_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count)
4024 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4026 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4028 if (!device->stateBlock->state.index_buffer)
4030 /* D3D9 returns D3DERR_INVALIDCALL when DrawIndexedPrimitive is called
4031 * without an index buffer set. (The first time at least...)
4032 * D3D8 simply dies, but I doubt it can do much harm to return
4033 * D3DERR_INVALIDCALL there as well. */
4034 WARN("Called without a valid index buffer set, returning WINED3DERR_INVALIDCALL.\n");
4035 return WINED3DERR_INVALIDCALL;
4038 if (!device->stateBlock->state.vertex_declaration)
4040 WARN("Called without a valid vertex declaration set.\n");
4041 return WINED3DERR_INVALIDCALL;
4044 if (!gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] &&
4045 device->stateBlock->state.load_base_vertex_index != device->stateBlock->state.base_vertex_index)
4047 device->stateBlock->state.load_base_vertex_index = device->stateBlock->state.base_vertex_index;
4048 device_invalidate_state(device, STATE_BASEVERTEXINDEX);
4051 draw_primitive(device, start_idx, index_count, 0, 0, TRUE);
4056 void CDECL wined3d_device_draw_indexed_primitive_instanced(struct wined3d_device *device,
4057 UINT start_idx, UINT index_count, UINT start_instance, UINT instance_count)
4059 TRACE("device %p, start_idx %u, index_count %u.\n", device, start_idx, index_count);
4061 draw_primitive(device, start_idx, index_count, start_instance, instance_count, TRUE);
4064 /* This is a helper function for UpdateTexture, there is no UpdateVolume method in D3D. */
4065 static HRESULT device_update_volume(struct wined3d_device *device,
4066 struct wined3d_volume *src_volume, struct wined3d_volume *dst_volume)
4068 struct wined3d_map_desc src;
4069 struct wined3d_map_desc dst;
4072 TRACE("device %p, src_volume %p, dst_volume %p.\n",
4073 device, src_volume, dst_volume);
4075 /* TODO: Implement direct loading into the gl volume instead of using
4076 * memcpy and dirtification to improve loading performance. */
4077 if (FAILED(hr = wined3d_volume_map(src_volume, &src, NULL, WINED3D_MAP_READONLY)))
4079 if (FAILED(hr = wined3d_volume_map(dst_volume, &dst, NULL, WINED3D_MAP_DISCARD)))
4081 wined3d_volume_unmap(src_volume);
4085 memcpy(dst.data, src.data, dst_volume->resource.size);
4087 hr = wined3d_volume_unmap(dst_volume);
4089 wined3d_volume_unmap(src_volume);
4091 hr = wined3d_volume_unmap(src_volume);
4096 HRESULT CDECL wined3d_device_update_texture(struct wined3d_device *device,
4097 struct wined3d_texture *src_texture, struct wined3d_texture *dst_texture)
4099 enum wined3d_resource_type type;
4100 unsigned int level_count, i;
4103 TRACE("device %p, src_texture %p, dst_texture %p.\n", device, src_texture, dst_texture);
4105 /* Verify that the source and destination textures are non-NULL. */
4106 if (!src_texture || !dst_texture)
4108 WARN("Source and destination textures must be non-NULL, returning WINED3DERR_INVALIDCALL.\n");
4109 return WINED3DERR_INVALIDCALL;
4112 if (src_texture == dst_texture)
4114 WARN("Source and destination are the same object, returning WINED3DERR_INVALIDCALL.\n");
4115 return WINED3DERR_INVALIDCALL;
4118 /* Verify that the source and destination textures are the same type. */
4119 type = src_texture->resource.type;
4120 if (dst_texture->resource.type != type)
4122 WARN("Source and destination have different types, returning WINED3DERR_INVALIDCALL.\n");
4123 return WINED3DERR_INVALIDCALL;
4126 /* Check that both textures have the identical numbers of levels. */
4127 level_count = wined3d_texture_get_level_count(src_texture);
4128 if (wined3d_texture_get_level_count(dst_texture) != level_count)
4130 WARN("Source and destination have different level counts, returning WINED3DERR_INVALIDCALL.\n");
4131 return WINED3DERR_INVALIDCALL;
4134 /* Make sure that the destination texture is loaded. */
4135 dst_texture->texture_ops->texture_preload(dst_texture, SRGB_RGB);
4137 /* Update every surface level of the texture. */
4140 case WINED3D_RTYPE_TEXTURE:
4142 struct wined3d_surface *src_surface;
4143 struct wined3d_surface *dst_surface;
4145 for (i = 0; i < level_count; ++i)
4147 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4148 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4149 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4152 WARN("Failed to update surface, hr %#x.\n", hr);
4159 case WINED3D_RTYPE_CUBE_TEXTURE:
4161 struct wined3d_surface *src_surface;
4162 struct wined3d_surface *dst_surface;
4164 for (i = 0; i < level_count * 6; ++i)
4166 src_surface = surface_from_resource(wined3d_texture_get_sub_resource(src_texture, i));
4167 dst_surface = surface_from_resource(wined3d_texture_get_sub_resource(dst_texture, i));
4168 hr = wined3d_device_update_surface(device, src_surface, NULL, dst_surface, NULL);
4171 WARN("Failed to update surface, hr %#x.\n", hr);
4178 case WINED3D_RTYPE_VOLUME_TEXTURE:
4180 for (i = 0; i < level_count; ++i)
4182 hr = device_update_volume(device,
4183 volume_from_resource(wined3d_texture_get_sub_resource(src_texture, i)),
4184 volume_from_resource(wined3d_texture_get_sub_resource(dst_texture, i)));
4187 WARN("Failed to update volume, hr %#x.\n", hr);
4195 FIXME("Unsupported texture type %#x.\n", type);
4196 return WINED3DERR_INVALIDCALL;
4202 HRESULT CDECL wined3d_device_get_front_buffer_data(const struct wined3d_device *device,
4203 UINT swapchain_idx, struct wined3d_surface *dst_surface)
4205 struct wined3d_swapchain *swapchain;
4207 TRACE("device %p, swapchain_idx %u, dst_surface %p.\n", device, swapchain_idx, dst_surface);
4209 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4210 return WINED3DERR_INVALIDCALL;
4212 return wined3d_swapchain_get_front_buffer_data(swapchain, dst_surface);
4215 HRESULT CDECL wined3d_device_validate_device(const struct wined3d_device *device, DWORD *num_passes)
4217 const struct wined3d_state *state = &device->stateBlock->state;
4218 struct wined3d_texture *texture;
4221 TRACE("device %p, num_passes %p.\n", device, num_passes);
4223 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
4225 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] == WINED3D_TEXF_NONE)
4227 WARN("Sampler state %u has minfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4228 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4230 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] == WINED3D_TEXF_NONE)
4232 WARN("Sampler state %u has magfilter D3DTEXF_NONE, returning D3DERR_UNSUPPORTEDTEXTUREFILTER\n", i);
4233 return WINED3DERR_UNSUPPORTEDTEXTUREFILTER;
4236 texture = state->textures[i];
4237 if (!texture || texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING) continue;
4239 if (state->sampler_states[i][WINED3D_SAMP_MAG_FILTER] != WINED3D_TEXF_POINT)
4241 WARN("Non-filterable texture and mag filter enabled on samper %u, returning E_FAIL\n", i);
4244 if (state->sampler_states[i][WINED3D_SAMP_MIN_FILTER] != WINED3D_TEXF_POINT)
4246 WARN("Non-filterable texture and min filter enabled on samper %u, returning E_FAIL\n", i);
4249 if (state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_NONE
4250 && state->sampler_states[i][WINED3D_SAMP_MIP_FILTER] != WINED3D_TEXF_POINT)
4252 WARN("Non-filterable texture and mip filter enabled on samper %u, returning E_FAIL\n", i);
4257 if (state->render_states[WINED3D_RS_ZENABLE] || state->render_states[WINED3D_RS_ZWRITEENABLE]
4258 || state->render_states[WINED3D_RS_STENCILENABLE])
4260 struct wined3d_surface *ds = device->fb.depth_stencil;
4261 struct wined3d_surface *target = device->fb.render_targets[0];
4264 && (ds->resource.width < target->resource.width || ds->resource.height < target->resource.height))
4266 WARN("Depth stencil is smaller than the color buffer, returning D3DERR_CONFLICTINGRENDERSTATE\n");
4267 return WINED3DERR_CONFLICTINGRENDERSTATE;
4271 /* return a sensible default */
4274 TRACE("returning D3D_OK\n");
4278 void CDECL wined3d_device_set_software_vertex_processing(struct wined3d_device *device, BOOL software)
4282 TRACE("device %p, software %#x.\n", device, software);
4286 FIXME("device %p, software %#x stub!\n", device, software);
4290 device->softwareVertexProcessing = software;
4293 BOOL CDECL wined3d_device_get_software_vertex_processing(const struct wined3d_device *device)
4297 TRACE("device %p.\n", device);
4301 TRACE("device %p stub!\n", device);
4305 return device->softwareVertexProcessing;
4308 HRESULT CDECL wined3d_device_get_raster_status(const struct wined3d_device *device,
4309 UINT swapchain_idx, struct wined3d_raster_status *raster_status)
4311 struct wined3d_swapchain *swapchain;
4313 TRACE("device %p, swapchain_idx %u, raster_status %p.\n",
4314 device, swapchain_idx, raster_status);
4316 if (!(swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
4317 return WINED3DERR_INVALIDCALL;
4319 return wined3d_swapchain_get_raster_status(swapchain, raster_status);
4322 HRESULT CDECL wined3d_device_set_npatch_mode(struct wined3d_device *device, float segments)
4326 TRACE("device %p, segments %.8e.\n", device, segments);
4328 if (segments != 0.0f)
4332 FIXME("device %p, segments %.8e stub!\n", device, segments);
4340 float CDECL wined3d_device_get_npatch_mode(const struct wined3d_device *device)
4344 TRACE("device %p.\n", device);
4348 FIXME("device %p stub!\n", device);
4355 HRESULT CDECL wined3d_device_update_surface(struct wined3d_device *device,
4356 struct wined3d_surface *src_surface, const RECT *src_rect,
4357 struct wined3d_surface *dst_surface, const POINT *dst_point)
4359 TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_point %s.\n",
4360 device, src_surface, wine_dbgstr_rect(src_rect),
4361 dst_surface, wine_dbgstr_point(dst_point));
4363 if (src_surface->resource.pool != WINED3D_POOL_SYSTEM_MEM || dst_surface->resource.pool != WINED3D_POOL_DEFAULT)
4365 WARN("source %p must be SYSTEMMEM and dest %p must be DEFAULT, returning WINED3DERR_INVALIDCALL\n",
4366 src_surface, dst_surface);
4367 return WINED3DERR_INVALIDCALL;
4370 return surface_upload_from_surface(dst_surface, dst_point, src_surface, src_rect);
4373 /* Do not call while under the GL lock. */
4374 HRESULT CDECL wined3d_device_color_fill(struct wined3d_device *device,
4375 struct wined3d_surface *surface, const RECT *rect, const struct wined3d_color *color)
4379 TRACE("device %p, surface %p, rect %s, color {%.8e, %.8e, %.8e, %.8e}.\n",
4380 device, surface, wine_dbgstr_rect(rect),
4381 color->r, color->g, color->b, color->a);
4383 if (surface->resource.pool != WINED3D_POOL_DEFAULT && surface->resource.pool != WINED3D_POOL_SYSTEM_MEM)
4385 WARN("Color-fill not allowed on %s surfaces.\n", debug_d3dpool(surface->resource.pool));
4386 return WINED3DERR_INVALIDCALL;
4391 SetRect(&r, 0, 0, surface->resource.width, surface->resource.height);
4395 return surface_color_fill(surface, rect, color);
4398 /* Do not call while under the GL lock. */
4399 void CDECL wined3d_device_clear_rendertarget_view(struct wined3d_device *device,
4400 struct wined3d_rendertarget_view *rendertarget_view, const struct wined3d_color *color)
4402 struct wined3d_resource *resource;
4406 resource = rendertarget_view->resource;
4407 if (resource->type != WINED3D_RTYPE_SURFACE)
4409 FIXME("Only supported on surface resources\n");
4413 SetRect(&rect, 0, 0, resource->width, resource->height);
4414 hr = surface_color_fill(surface_from_resource(resource), &rect, color);
4415 if (FAILED(hr)) ERR("Color fill failed, hr %#x.\n", hr);
4418 struct wined3d_surface * CDECL wined3d_device_get_render_target(const struct wined3d_device *device,
4419 UINT render_target_idx)
4421 TRACE("device %p, render_target_idx %u.\n", device, render_target_idx);
4423 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4425 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4429 return device->fb.render_targets[render_target_idx];
4432 struct wined3d_surface * CDECL wined3d_device_get_depth_stencil(const struct wined3d_device *device)
4434 TRACE("device %p.\n", device);
4436 return device->fb.depth_stencil;
4439 HRESULT CDECL wined3d_device_set_render_target(struct wined3d_device *device,
4440 UINT render_target_idx, struct wined3d_surface *render_target, BOOL set_viewport)
4442 struct wined3d_surface *prev;
4444 TRACE("device %p, render_target_idx %u, render_target %p, set_viewport %#x.\n",
4445 device, render_target_idx, render_target, set_viewport);
4447 if (render_target_idx >= device->adapter->gl_info.limits.buffers)
4449 WARN("Only %u render targets are supported.\n", device->adapter->gl_info.limits.buffers);
4450 return WINED3DERR_INVALIDCALL;
4453 /* Render target 0 can't be set to NULL. */
4454 if (!render_target && !render_target_idx)
4456 WARN("Trying to set render target 0 to NULL.\n");
4457 return WINED3DERR_INVALIDCALL;
4460 if (render_target && !(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET))
4462 FIXME("Surface %p doesn't have render target usage.\n", render_target);
4463 return WINED3DERR_INVALIDCALL;
4466 /* Set the viewport and scissor rectangles, if requested. Tests show that
4467 * stateblock recording is ignored, the change goes directly into the
4468 * primary stateblock. */
4469 if (!render_target_idx && set_viewport)
4471 struct wined3d_state *state = &device->stateBlock->state;
4473 state->viewport.x = 0;
4474 state->viewport.y = 0;
4475 state->viewport.width = render_target->resource.width;
4476 state->viewport.height = render_target->resource.height;
4477 state->viewport.min_z = 0.0f;
4478 state->viewport.max_z = 1.0f;
4479 device_invalidate_state(device, STATE_VIEWPORT);
4481 state->scissor_rect.top = 0;
4482 state->scissor_rect.left = 0;
4483 state->scissor_rect.right = render_target->resource.width;
4484 state->scissor_rect.bottom = render_target->resource.height;
4485 device_invalidate_state(device, STATE_SCISSORRECT);
4489 prev = device->fb.render_targets[render_target_idx];
4490 if (render_target == prev)
4494 wined3d_surface_incref(render_target);
4495 device->fb.render_targets[render_target_idx] = render_target;
4496 /* Release after the assignment, to prevent device_resource_released()
4497 * from seeing the surface as still in use. */
4499 wined3d_surface_decref(prev);
4501 device_invalidate_state(device, STATE_FRAMEBUFFER);
4506 void CDECL wined3d_device_set_depth_stencil(struct wined3d_device *device, struct wined3d_surface *depth_stencil)
4508 struct wined3d_surface *prev = device->fb.depth_stencil;
4510 TRACE("device %p, depth_stencil %p, old depth_stencil %p.\n",
4511 device, depth_stencil, prev);
4513 if (prev == depth_stencil)
4515 TRACE("Trying to do a NOP SetRenderTarget operation.\n");
4521 if (device->swapchains[0]->desc.flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
4522 || prev->flags & SFLAG_DISCARD)
4524 surface_modify_ds_location(prev, SFLAG_DISCARDED,
4525 prev->resource.width, prev->resource.height);
4526 if (prev == device->onscreen_depth_stencil)
4528 wined3d_surface_decref(device->onscreen_depth_stencil);
4529 device->onscreen_depth_stencil = NULL;
4534 device->fb.depth_stencil = depth_stencil;
4536 wined3d_surface_incref(depth_stencil);
4538 if (!prev != !depth_stencil)
4540 /* Swapping NULL / non NULL depth stencil affects the depth and tests */
4541 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_ZENABLE));
4542 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILENABLE));
4543 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
4544 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4546 else if (prev && prev->resource.format->depth_size != depth_stencil->resource.format->depth_size)
4548 device_invalidate_state(device, STATE_RENDER(WINED3D_RS_DEPTHBIAS));
4551 wined3d_surface_decref(prev);
4553 device_invalidate_state(device, STATE_FRAMEBUFFER);
4558 HRESULT CDECL wined3d_device_set_cursor_properties(struct wined3d_device *device,
4559 UINT x_hotspot, UINT y_hotspot, struct wined3d_surface *cursor_image)
4561 TRACE("device %p, x_hotspot %u, y_hotspot %u, cursor_image %p.\n",
4562 device, x_hotspot, y_hotspot, cursor_image);
4564 /* some basic validation checks */
4565 if (device->cursorTexture)
4567 struct wined3d_context *context = context_acquire(device, NULL);
4568 context->gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4569 context_release(context);
4570 device->cursorTexture = 0;
4575 struct wined3d_display_mode mode;
4576 struct wined3d_map_desc map_desc;
4579 /* MSDN: Cursor must be A8R8G8B8 */
4580 if (cursor_image->resource.format->id != WINED3DFMT_B8G8R8A8_UNORM)
4582 WARN("surface %p has an invalid format.\n", cursor_image);
4583 return WINED3DERR_INVALIDCALL;
4586 if (FAILED(hr = wined3d_get_adapter_display_mode(device->wined3d, device->adapter->ordinal, &mode, NULL)))
4588 ERR("Failed to get display mode, hr %#x.\n", hr);
4589 return WINED3DERR_INVALIDCALL;
4592 /* MSDN: Cursor must be smaller than the display mode */
4593 if (cursor_image->resource.width > mode.width || cursor_image->resource.height > mode.height)
4595 WARN("Surface %p dimensions are %ux%u, but screen dimensions are %ux%u.\n",
4596 cursor_image, cursor_image->resource.width, cursor_image->resource.height,
4597 mode.width, mode.height);
4598 return WINED3DERR_INVALIDCALL;
4601 /* TODO: MSDN: Cursor sizes must be a power of 2 */
4603 /* Do not store the surface's pointer because the application may
4604 * release it after setting the cursor image. Windows doesn't
4605 * addref the set surface, so we can't do this either without
4606 * creating circular refcount dependencies. Copy out the gl texture
4608 device->cursorWidth = cursor_image->resource.width;
4609 device->cursorHeight = cursor_image->resource.height;
4610 if (SUCCEEDED(wined3d_surface_map(cursor_image, &map_desc, NULL, WINED3D_MAP_READONLY)))
4612 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4613 const struct wined3d_format *format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
4614 struct wined3d_context *context;
4615 char *mem, *bits = map_desc.data;
4616 GLint intfmt = format->glInternal;
4617 GLint gl_format = format->glFormat;
4618 GLint type = format->glType;
4619 INT height = device->cursorHeight;
4620 INT width = device->cursorWidth;
4621 INT bpp = format->byte_count;
4624 /* Reformat the texture memory (pitch and width can be
4626 mem = HeapAlloc(GetProcessHeap(), 0, width * height * bpp);
4627 for (i = 0; i < height; ++i)
4628 memcpy(&mem[width * bpp * i], &bits[map_desc.row_pitch * i], width * bpp);
4629 wined3d_surface_unmap(cursor_image);
4631 context = context_acquire(device, NULL);
4633 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4635 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
4636 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
4639 invalidate_active_texture(device, context);
4640 /* Create a new cursor texture */
4641 gl_info->gl_ops.gl.p_glGenTextures(1, &device->cursorTexture);
4642 checkGLcall("glGenTextures");
4643 context_bind_texture(context, GL_TEXTURE_2D, device->cursorTexture);
4644 /* Copy the bitmap memory into the cursor texture */
4645 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, intfmt, width, height, 0, gl_format, type, mem);
4646 checkGLcall("glTexImage2D");
4647 HeapFree(GetProcessHeap(), 0, mem);
4649 if (gl_info->supported[APPLE_CLIENT_STORAGE])
4651 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
4652 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
4655 context_release(context);
4659 FIXME("A cursor texture was not returned.\n");
4660 device->cursorTexture = 0;
4663 if (cursor_image->resource.width == 32 && cursor_image->resource.height == 32)
4665 UINT mask_size = cursor_image->resource.width * cursor_image->resource.height / 8;
4666 ICONINFO cursorInfo;
4670 /* 32-bit user32 cursors ignore the alpha channel if it's all
4671 * zeroes, and use the mask instead. Fill the mask with all ones
4672 * to ensure we still get a fully transparent cursor. */
4673 maskBits = HeapAlloc(GetProcessHeap(), 0, mask_size);
4674 memset(maskBits, 0xff, mask_size);
4675 wined3d_surface_map(cursor_image, &map_desc, NULL,
4676 WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY);
4677 TRACE("width: %u height: %u.\n", cursor_image->resource.width, cursor_image->resource.height);
4679 cursorInfo.fIcon = FALSE;
4680 cursorInfo.xHotspot = x_hotspot;
4681 cursorInfo.yHotspot = y_hotspot;
4682 cursorInfo.hbmMask = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4684 cursorInfo.hbmColor = CreateBitmap(cursor_image->resource.width, cursor_image->resource.height,
4685 1, 32, map_desc.data);
4686 wined3d_surface_unmap(cursor_image);
4687 /* Create our cursor and clean up. */
4688 cursor = CreateIconIndirect(&cursorInfo);
4689 if (cursorInfo.hbmMask) DeleteObject(cursorInfo.hbmMask);
4690 if (cursorInfo.hbmColor) DeleteObject(cursorInfo.hbmColor);
4691 if (device->hardwareCursor) DestroyCursor(device->hardwareCursor);
4692 device->hardwareCursor = cursor;
4693 if (device->bCursorVisible) SetCursor( cursor );
4694 HeapFree(GetProcessHeap(), 0, maskBits);
4698 device->xHotSpot = x_hotspot;
4699 device->yHotSpot = y_hotspot;
4703 void CDECL wined3d_device_set_cursor_position(struct wined3d_device *device,
4704 int x_screen_space, int y_screen_space, DWORD flags)
4706 TRACE("device %p, x %d, y %d, flags %#x.\n",
4707 device, x_screen_space, y_screen_space, flags);
4709 device->xScreenSpace = x_screen_space;
4710 device->yScreenSpace = y_screen_space;
4712 if (device->hardwareCursor)
4716 GetCursorPos( &pt );
4717 if (x_screen_space == pt.x && y_screen_space == pt.y)
4719 SetCursorPos( x_screen_space, y_screen_space );
4721 /* Switch to the software cursor if position diverges from the hardware one. */
4722 GetCursorPos( &pt );
4723 if (x_screen_space != pt.x || y_screen_space != pt.y)
4725 if (device->bCursorVisible) SetCursor( NULL );
4726 DestroyCursor( device->hardwareCursor );
4727 device->hardwareCursor = 0;
4732 BOOL CDECL wined3d_device_show_cursor(struct wined3d_device *device, BOOL show)
4734 BOOL oldVisible = device->bCursorVisible;
4736 TRACE("device %p, show %#x.\n", device, show);
4739 * When ShowCursor is first called it should make the cursor appear at the OS's last
4740 * known cursor position.
4742 if (show && !oldVisible)
4746 device->xScreenSpace = pt.x;
4747 device->yScreenSpace = pt.y;
4750 if (device->hardwareCursor)
4752 device->bCursorVisible = show;
4754 SetCursor(device->hardwareCursor);
4760 if (device->cursorTexture)
4761 device->bCursorVisible = show;
4767 void CDECL wined3d_device_evict_managed_resources(struct wined3d_device *device)
4769 struct wined3d_resource *resource, *cursor;
4771 TRACE("device %p.\n", device);
4773 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4775 TRACE("Checking resource %p for eviction.\n", resource);
4777 if (resource->pool == WINED3D_POOL_MANAGED && !resource->map_count)
4779 TRACE("Evicting %p.\n", resource);
4780 resource->resource_ops->resource_unload(resource);
4784 /* Invalidate stream sources, the buffer(s) may have been evicted. */
4785 device_invalidate_state(device, STATE_STREAMSRC);
4788 /* Do not call while under the GL lock. */
4789 static void delete_opengl_contexts(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4791 struct wined3d_resource *resource, *cursor;
4792 const struct wined3d_gl_info *gl_info;
4793 struct wined3d_context *context;
4794 struct wined3d_shader *shader;
4796 context = context_acquire(device, NULL);
4797 gl_info = context->gl_info;
4799 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4801 TRACE("Unloading resource %p.\n", resource);
4803 resource->resource_ops->resource_unload(resource);
4806 LIST_FOR_EACH_ENTRY(shader, &device->shaders, struct wined3d_shader, shader_list_entry)
4808 device->shader_backend->shader_destroy(shader);
4811 if (device->depth_blt_texture)
4813 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->depth_blt_texture);
4814 device->depth_blt_texture = 0;
4816 if (device->cursorTexture)
4818 gl_info->gl_ops.gl.p_glDeleteTextures(1, &device->cursorTexture);
4819 device->cursorTexture = 0;
4822 device->blitter->free_private(device);
4823 device->shader_backend->shader_free_private(device);
4824 destroy_dummy_textures(device, gl_info);
4826 context_release(context);
4828 while (device->context_count)
4830 swapchain_destroy_contexts(device->contexts[0]->swapchain);
4833 HeapFree(GetProcessHeap(), 0, swapchain->context);
4834 swapchain->context = NULL;
4837 /* Do not call while under the GL lock. */
4838 static HRESULT create_primary_opengl_context(struct wined3d_device *device, struct wined3d_swapchain *swapchain)
4840 struct wined3d_context *context;
4841 struct wined3d_surface *target;
4844 if (FAILED(hr = device->shader_backend->shader_alloc_private(device,
4845 device->adapter->vertex_pipe, device->adapter->fragment_pipe)))
4847 ERR("Failed to allocate shader private data, hr %#x.\n", hr);
4851 if (FAILED(hr = device->blitter->alloc_private(device)))
4853 ERR("Failed to allocate blitter private data, hr %#x.\n", hr);
4854 device->shader_backend->shader_free_private(device);
4858 /* Recreate the primary swapchain's context */
4859 swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain->context));
4860 if (!swapchain->context)
4862 ERR("Failed to allocate memory for swapchain context array.\n");
4863 device->blitter->free_private(device);
4864 device->shader_backend->shader_free_private(device);
4865 return E_OUTOFMEMORY;
4868 target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
4869 if (!(context = context_create(swapchain, target, swapchain->ds_format)))
4871 WARN("Failed to create context.\n");
4872 device->blitter->free_private(device);
4873 device->shader_backend->shader_free_private(device);
4874 HeapFree(GetProcessHeap(), 0, swapchain->context);
4878 swapchain->context[0] = context;
4879 swapchain->num_contexts = 1;
4880 create_dummy_textures(device, context);
4881 context_release(context);
4886 /* Do not call while under the GL lock. */
4887 HRESULT CDECL wined3d_device_reset(struct wined3d_device *device,
4888 const struct wined3d_swapchain_desc *swapchain_desc, const struct wined3d_display_mode *mode,
4889 wined3d_device_reset_cb callback, BOOL reset_state)
4891 struct wined3d_resource *resource, *cursor;
4892 struct wined3d_swapchain *swapchain;
4893 struct wined3d_display_mode m;
4894 BOOL DisplayModeChanged = FALSE;
4895 BOOL update_desc = FALSE;
4896 UINT backbuffer_width = swapchain_desc->backbuffer_width;
4897 UINT backbuffer_height = swapchain_desc->backbuffer_height;
4898 HRESULT hr = WINED3D_OK;
4901 TRACE("device %p, swapchain_desc %p, mode %p, callback %p.\n", device, swapchain_desc, mode, callback);
4903 if (!(swapchain = wined3d_device_get_swapchain(device, 0)))
4905 ERR("Failed to get the first implicit swapchain.\n");
4906 return WINED3DERR_INVALIDCALL;
4910 stateblock_unbind_resources(device->stateBlock);
4912 if (device->fb.render_targets)
4914 if (swapchain->back_buffers && swapchain->back_buffers[0])
4915 wined3d_device_set_render_target(device, 0, swapchain->back_buffers[0], FALSE);
4917 wined3d_device_set_render_target(device, 0, swapchain->front_buffer, FALSE);
4918 for (i = 1; i < device->adapter->gl_info.limits.buffers; ++i)
4920 wined3d_device_set_render_target(device, i, NULL, FALSE);
4923 wined3d_device_set_depth_stencil(device, NULL);
4925 if (device->onscreen_depth_stencil)
4927 wined3d_surface_decref(device->onscreen_depth_stencil);
4928 device->onscreen_depth_stencil = NULL;
4933 LIST_FOR_EACH_ENTRY_SAFE(resource, cursor, &device->resources, struct wined3d_resource, resource_list_entry)
4935 TRACE("Enumerating resource %p.\n", resource);
4936 if (FAILED(hr = callback(resource)))
4941 /* Is it necessary to recreate the gl context? Actually every setting can be changed
4942 * on an existing gl context, so there's no real need for recreation.
4944 * TODO: Figure out how Reset influences resources in D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEMORY and D3DPOOL_MANAGED
4946 * TODO: Figure out what happens to explicit swapchains, or if we have more than one implicit swapchain
4948 TRACE("New params:\n");
4949 TRACE("backbuffer_width %u\n", swapchain_desc->backbuffer_width);
4950 TRACE("backbuffer_height %u\n", swapchain_desc->backbuffer_height);
4951 TRACE("backbuffer_format %s\n", debug_d3dformat(swapchain_desc->backbuffer_format));
4952 TRACE("backbuffer_count %u\n", swapchain_desc->backbuffer_count);
4953 TRACE("multisample_type %#x\n", swapchain_desc->multisample_type);
4954 TRACE("multisample_quality %u\n", swapchain_desc->multisample_quality);
4955 TRACE("swap_effect %#x\n", swapchain_desc->swap_effect);
4956 TRACE("device_window %p\n", swapchain_desc->device_window);
4957 TRACE("windowed %#x\n", swapchain_desc->windowed);
4958 TRACE("enable_auto_depth_stencil %#x\n", swapchain_desc->enable_auto_depth_stencil);
4959 if (swapchain_desc->enable_auto_depth_stencil)
4960 TRACE("auto_depth_stencil_format %s\n", debug_d3dformat(swapchain_desc->auto_depth_stencil_format));
4961 TRACE("flags %#x\n", swapchain_desc->flags);
4962 TRACE("refresh_rate %u\n", swapchain_desc->refresh_rate);
4963 TRACE("swap_interval %u\n", swapchain_desc->swap_interval);
4964 TRACE("auto_restore_display_mode %#x\n", swapchain_desc->auto_restore_display_mode);
4966 /* No special treatment of these parameters. Just store them */
4967 swapchain->desc.swap_effect = swapchain_desc->swap_effect;
4968 swapchain->desc.flags = swapchain_desc->flags;
4969 swapchain->desc.swap_interval = swapchain_desc->swap_interval;
4970 swapchain->desc.refresh_rate = swapchain_desc->refresh_rate;
4972 /* What to do about these? */
4973 if (swapchain_desc->backbuffer_count
4974 && swapchain_desc->backbuffer_count != swapchain->desc.backbuffer_count)
4975 FIXME("Cannot change the back buffer count yet.\n");
4977 if (swapchain_desc->device_window
4978 && swapchain_desc->device_window != swapchain->desc.device_window)
4980 TRACE("Changing the device window from %p to %p.\n",
4981 swapchain->desc.device_window, swapchain_desc->device_window);
4982 swapchain->desc.device_window = swapchain_desc->device_window;
4983 swapchain->device_window = swapchain_desc->device_window;
4984 wined3d_swapchain_set_window(swapchain, NULL);
4987 if (swapchain_desc->enable_auto_depth_stencil && !device->auto_depth_stencil)
4989 TRACE("Creating the depth stencil buffer\n");
4991 if (FAILED(hr = device->device_parent->ops->create_swapchain_surface(device->device_parent,
4992 device->device_parent, swapchain_desc->backbuffer_width, swapchain_desc->backbuffer_height,
4993 swapchain_desc->auto_depth_stencil_format, WINED3DUSAGE_DEPTHSTENCIL,
4994 swapchain_desc->multisample_type, swapchain_desc->multisample_quality,
4995 &device->auto_depth_stencil)))
4997 ERR("Failed to create the depth stencil buffer, hr %#x.\n", hr);
4998 return WINED3DERR_INVALIDCALL;
5002 /* Reset the depth stencil */
5003 if (swapchain_desc->enable_auto_depth_stencil)
5004 wined3d_device_set_depth_stencil(device, device->auto_depth_stencil);
5008 DisplayModeChanged = TRUE;
5011 else if (swapchain_desc->windowed)
5013 m.width = swapchain->orig_width;
5014 m.height = swapchain->orig_height;
5016 m.format_id = swapchain->desc.backbuffer_format;
5017 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5021 m.width = swapchain_desc->backbuffer_width;
5022 m.height = swapchain_desc->backbuffer_height;
5023 m.refresh_rate = swapchain_desc->refresh_rate;
5024 m.format_id = swapchain_desc->backbuffer_format;
5025 m.scanline_ordering = WINED3D_SCANLINE_ORDERING_UNKNOWN;
5028 if (!backbuffer_width || !backbuffer_height)
5030 /* The application is requesting that either the swapchain width or
5031 * height be set to the corresponding dimension in the window's
5036 if (!swapchain_desc->windowed)
5037 return WINED3DERR_INVALIDCALL;
5039 if (!GetClientRect(swapchain->device_window, &client_rect))
5041 ERR("Failed to get client rect, last error %#x.\n", GetLastError());
5042 return WINED3DERR_INVALIDCALL;
5045 if (!backbuffer_width)
5046 backbuffer_width = client_rect.right;
5048 if (!backbuffer_height)
5049 backbuffer_height = client_rect.bottom;
5052 if (backbuffer_width != swapchain->desc.backbuffer_width
5053 || backbuffer_height != swapchain->desc.backbuffer_height)
5055 if (!swapchain_desc->windowed)
5056 DisplayModeChanged = TRUE;
5058 swapchain->desc.backbuffer_width = backbuffer_width;
5059 swapchain->desc.backbuffer_height = backbuffer_height;
5063 if (swapchain_desc->backbuffer_format != WINED3DFMT_UNKNOWN
5064 && swapchain_desc->backbuffer_format != swapchain->desc.backbuffer_format)
5066 swapchain->desc.backbuffer_format = swapchain_desc->backbuffer_format;
5070 if (swapchain_desc->multisample_type != swapchain->desc.multisample_type
5071 || swapchain_desc->multisample_quality != swapchain->desc.multisample_quality)
5073 swapchain->desc.multisample_type = swapchain_desc->multisample_type;
5074 swapchain->desc.multisample_quality = swapchain_desc->multisample_quality;
5082 if (FAILED(hr = wined3d_surface_update_desc(swapchain->front_buffer, swapchain->desc.backbuffer_width,
5083 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5084 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5087 for (i = 0; i < swapchain->desc.backbuffer_count; ++i)
5089 if (FAILED(hr = wined3d_surface_update_desc(swapchain->back_buffers[i], swapchain->desc.backbuffer_width,
5090 swapchain->desc.backbuffer_height, swapchain->desc.backbuffer_format,
5091 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5094 if (device->auto_depth_stencil)
5096 if (FAILED(hr = wined3d_surface_update_desc(device->auto_depth_stencil, swapchain->desc.backbuffer_width,
5097 swapchain->desc.backbuffer_height, device->auto_depth_stencil->resource.format->id,
5098 swapchain->desc.multisample_type, swapchain->desc.multisample_quality)))
5103 if (!swapchain_desc->windowed != !swapchain->desc.windowed
5104 || DisplayModeChanged)
5106 if (FAILED(hr = wined3d_set_adapter_display_mode(device->wined3d, device->adapter->ordinal, &m)))
5108 WARN("Failed to set display mode, hr %#x.\n", hr);
5109 return WINED3DERR_INVALIDCALL;
5112 if (!swapchain_desc->windowed)
5114 if (swapchain->desc.windowed)
5116 HWND focus_window = device->create_parms.focus_window;
5118 focus_window = swapchain_desc->device_window;
5119 if (FAILED(hr = wined3d_device_acquire_focus_window(device, focus_window)))
5121 ERR("Failed to acquire focus window, hr %#x.\n", hr);
5125 /* switch from windowed to fs */
5126 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5127 swapchain_desc->backbuffer_width,
5128 swapchain_desc->backbuffer_height);
5132 /* Fullscreen -> fullscreen mode change */
5133 MoveWindow(swapchain->device_window, 0, 0,
5134 swapchain_desc->backbuffer_width,
5135 swapchain_desc->backbuffer_height,
5139 else if (!swapchain->desc.windowed)
5141 /* Fullscreen -> windowed switch */
5142 wined3d_device_restore_fullscreen_window(device, swapchain->device_window);
5143 wined3d_device_release_focus_window(device);
5145 swapchain->desc.windowed = swapchain_desc->windowed;
5147 else if (!swapchain_desc->windowed)
5149 DWORD style = device->style;
5150 DWORD exStyle = device->exStyle;
5151 /* If we're in fullscreen, and the mode wasn't changed, we have to get the window back into
5152 * the right position. Some applications(Battlefield 2, Guild Wars) move it and then call
5153 * Reset to clear up their mess. Guild Wars also loses the device during that.
5156 device->exStyle = 0;
5157 wined3d_device_setup_fullscreen_window(device, swapchain->device_window,
5158 swapchain_desc->backbuffer_width,
5159 swapchain_desc->backbuffer_height);
5160 device->style = style;
5161 device->exStyle = exStyle;
5166 TRACE("Resetting stateblock.\n");
5167 wined3d_stateblock_decref(device->updateStateBlock);
5168 wined3d_stateblock_decref(device->stateBlock);
5170 if (device->d3d_initialized)
5171 delete_opengl_contexts(device, swapchain);
5173 /* Note: No parent needed for initial internal stateblock */
5174 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5176 ERR("Resetting the stateblock failed with error %#x.\n", hr);
5178 TRACE("Created stateblock %p.\n", device->stateBlock);
5179 device->updateStateBlock = device->stateBlock;
5180 wined3d_stateblock_incref(device->updateStateBlock);
5182 stateblock_init_default_state(device->stateBlock);
5186 struct wined3d_surface *rt = device->fb.render_targets[0];
5187 struct wined3d_state *state = &device->stateBlock->state;
5189 /* Note the min_z / max_z is not reset. */
5190 state->viewport.x = 0;
5191 state->viewport.y = 0;
5192 state->viewport.width = rt->resource.width;
5193 state->viewport.height = rt->resource.height;
5194 device_invalidate_state(device, STATE_VIEWPORT);
5196 state->scissor_rect.top = 0;
5197 state->scissor_rect.left = 0;
5198 state->scissor_rect.right = rt->resource.width;
5199 state->scissor_rect.bottom = rt->resource.height;
5200 device_invalidate_state(device, STATE_SCISSORRECT);
5203 swapchain_update_render_to_fbo(swapchain);
5204 swapchain_update_draw_bindings(swapchain);
5206 if (reset_state && device->d3d_initialized)
5207 hr = create_primary_opengl_context(device, swapchain);
5209 /* All done. There is no need to reload resources or shaders, this will happen automatically on the
5215 HRESULT CDECL wined3d_device_set_dialog_box_mode(struct wined3d_device *device, BOOL enable_dialogs)
5217 TRACE("device %p, enable_dialogs %#x.\n", device, enable_dialogs);
5219 if (!enable_dialogs) FIXME("Dialogs cannot be disabled yet.\n");
5225 void CDECL wined3d_device_get_creation_parameters(const struct wined3d_device *device,
5226 struct wined3d_device_creation_parameters *parameters)
5228 TRACE("device %p, parameters %p.\n", device, parameters);
5230 *parameters = device->create_parms;
5233 void CDECL wined3d_device_set_gamma_ramp(const struct wined3d_device *device,
5234 UINT swapchain_idx, DWORD flags, const struct wined3d_gamma_ramp *ramp)
5236 struct wined3d_swapchain *swapchain;
5238 TRACE("device %p, swapchain_idx %u, flags %#x, ramp %p.\n",
5239 device, swapchain_idx, flags, ramp);
5241 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5242 wined3d_swapchain_set_gamma_ramp(swapchain, flags, ramp);
5245 void CDECL wined3d_device_get_gamma_ramp(const struct wined3d_device *device,
5246 UINT swapchain_idx, struct wined3d_gamma_ramp *ramp)
5248 struct wined3d_swapchain *swapchain;
5250 TRACE("device %p, swapchain_idx %u, ramp %p.\n",
5251 device, swapchain_idx, ramp);
5253 if ((swapchain = wined3d_device_get_swapchain(device, swapchain_idx)))
5254 wined3d_swapchain_get_gamma_ramp(swapchain, ramp);
5257 void device_resource_add(struct wined3d_device *device, struct wined3d_resource *resource)
5259 TRACE("device %p, resource %p.\n", device, resource);
5261 list_add_head(&device->resources, &resource->resource_list_entry);
5264 static void device_resource_remove(struct wined3d_device *device, struct wined3d_resource *resource)
5266 TRACE("device %p, resource %p.\n", device, resource);
5268 list_remove(&resource->resource_list_entry);
5271 void device_resource_released(struct wined3d_device *device, struct wined3d_resource *resource)
5273 enum wined3d_resource_type type = resource->type;
5276 TRACE("device %p, resource %p, type %s.\n", device, resource, debug_d3dresourcetype(type));
5278 context_resource_released(device, resource, type);
5282 case WINED3D_RTYPE_SURFACE:
5284 struct wined3d_surface *surface = surface_from_resource(resource);
5286 if (!device->d3d_initialized) break;
5288 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
5290 if (device->fb.render_targets[i] == surface)
5292 ERR("Surface %p is still in use as render target %u.\n", surface, i);
5293 device->fb.render_targets[i] = NULL;
5297 if (device->fb.depth_stencil == surface)
5299 ERR("Surface %p is still in use as depth/stencil buffer.\n", surface);
5300 device->fb.depth_stencil = NULL;
5305 case WINED3D_RTYPE_TEXTURE:
5306 case WINED3D_RTYPE_CUBE_TEXTURE:
5307 case WINED3D_RTYPE_VOLUME_TEXTURE:
5308 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
5310 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
5312 if (device->stateBlock && device->stateBlock->state.textures[i] == texture)
5314 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5315 texture, device->stateBlock, i);
5316 device->stateBlock->state.textures[i] = NULL;
5319 if (device->updateStateBlock != device->stateBlock
5320 && device->updateStateBlock->state.textures[i] == texture)
5322 ERR("Texture %p is still in use by stateblock %p, stage %u.\n",
5323 texture, device->updateStateBlock, i);
5324 device->updateStateBlock->state.textures[i] = NULL;
5329 case WINED3D_RTYPE_BUFFER:
5331 struct wined3d_buffer *buffer = buffer_from_resource(resource);
5333 for (i = 0; i < MAX_STREAMS; ++i)
5335 if (device->stateBlock && device->stateBlock->state.streams[i].buffer == buffer)
5337 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5338 buffer, device->stateBlock, i);
5339 device->stateBlock->state.streams[i].buffer = NULL;
5342 if (device->updateStateBlock != device->stateBlock
5343 && device->updateStateBlock->state.streams[i].buffer == buffer)
5345 ERR("Buffer %p is still in use by stateblock %p, stream %u.\n",
5346 buffer, device->updateStateBlock, i);
5347 device->updateStateBlock->state.streams[i].buffer = NULL;
5352 if (device->stateBlock && device->stateBlock->state.index_buffer == buffer)
5354 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5355 buffer, device->stateBlock);
5356 device->stateBlock->state.index_buffer = NULL;
5359 if (device->updateStateBlock != device->stateBlock
5360 && device->updateStateBlock->state.index_buffer == buffer)
5362 ERR("Buffer %p is still in use by stateblock %p as index buffer.\n",
5363 buffer, device->updateStateBlock);
5364 device->updateStateBlock->state.index_buffer = NULL;
5373 /* Remove the resource from the resourceStore */
5374 device_resource_remove(device, resource);
5376 TRACE("Resource released.\n");
5379 struct wined3d_surface * CDECL wined3d_device_get_surface_from_dc(const struct wined3d_device *device, HDC dc)
5381 struct wined3d_resource *resource;
5383 TRACE("device %p, dc %p.\n", device, dc);
5388 LIST_FOR_EACH_ENTRY(resource, &device->resources, struct wined3d_resource, resource_list_entry)
5390 if (resource->type == WINED3D_RTYPE_SURFACE)
5392 struct wined3d_surface *s = surface_from_resource(resource);
5396 TRACE("Found surface %p for dc %p.\n", s, dc);
5405 HRESULT device_init(struct wined3d_device *device, struct wined3d *wined3d,
5406 UINT adapter_idx, enum wined3d_device_type device_type, HWND focus_window, DWORD flags,
5407 BYTE surface_alignment, struct wined3d_device_parent *device_parent)
5409 struct wined3d_adapter *adapter = &wined3d->adapters[adapter_idx];
5410 const struct fragment_pipeline *fragment_pipeline;
5411 const struct wined3d_vertex_pipe_ops *vertex_pipeline;
5416 device->wined3d = wined3d;
5417 wined3d_incref(device->wined3d);
5418 device->adapter = wined3d->adapter_count ? adapter : NULL;
5419 device->device_parent = device_parent;
5420 list_init(&device->resources);
5421 list_init(&device->shaders);
5422 device->surface_alignment = surface_alignment;
5424 /* Save the creation parameters. */
5425 device->create_parms.adapter_idx = adapter_idx;
5426 device->create_parms.device_type = device_type;
5427 device->create_parms.focus_window = focus_window;
5428 device->create_parms.flags = flags;
5430 device->shader_backend = adapter->shader_backend;
5432 vertex_pipeline = adapter->vertex_pipe;
5434 fragment_pipeline = adapter->fragment_pipe;
5436 if (vertex_pipeline->vp_states && fragment_pipeline->states
5437 && FAILED(hr = compile_state_table(device->StateTable, device->multistate_funcs,
5438 &adapter->gl_info, &adapter->d3d_info, vertex_pipeline,
5439 fragment_pipeline, misc_state_template)))
5441 ERR("Failed to compile state table, hr %#x.\n", hr);
5442 wined3d_decref(device->wined3d);
5446 device->blitter = adapter->blitter;
5448 hr = wined3d_stateblock_create(device, WINED3D_SBT_INIT, &device->stateBlock);
5451 WARN("Failed to create stateblock.\n");
5452 for (i = 0; i < sizeof(device->multistate_funcs) / sizeof(device->multistate_funcs[0]); ++i)
5454 HeapFree(GetProcessHeap(), 0, device->multistate_funcs[i]);
5456 wined3d_decref(device->wined3d);
5460 TRACE("Created stateblock %p.\n", device->stateBlock);
5461 device->updateStateBlock = device->stateBlock;
5462 wined3d_stateblock_incref(device->updateStateBlock);
5468 void device_invalidate_state(const struct wined3d_device *device, DWORD state)
5470 DWORD rep = device->StateTable[state].representative;
5471 struct wined3d_context *context;
5476 for (i = 0; i < device->context_count; ++i)
5478 context = device->contexts[i];
5479 if(isStateDirty(context, rep)) continue;
5481 context->dirtyArray[context->numDirtyEntries++] = rep;
5482 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
5483 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
5484 context->isStateDirty[idx] |= (1 << shift);
5488 void get_drawable_size_fbo(const struct wined3d_context *context, UINT *width, UINT *height)
5490 /* The drawable size of a fbo target is the opengl texture size, which is the power of two size. */
5491 *width = context->current_rt->pow2Width;
5492 *height = context->current_rt->pow2Height;
5495 void get_drawable_size_backbuffer(const struct wined3d_context *context, UINT *width, UINT *height)
5497 const struct wined3d_swapchain *swapchain = context->swapchain;
5498 /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
5499 * current context's drawable, which is the size of the back buffer of the swapchain
5500 * the active context belongs to. */
5501 *width = swapchain->desc.backbuffer_width;
5502 *height = swapchain->desc.backbuffer_height;
5505 LRESULT device_process_message(struct wined3d_device *device, HWND window, BOOL unicode,
5506 UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc)
5508 if (device->filter_messages)
5510 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
5511 window, message, wparam, lparam);
5513 return DefWindowProcW(window, message, wparam, lparam);
5515 return DefWindowProcA(window, message, wparam, lparam);
5518 if (message == WM_DESTROY)
5520 TRACE("unregister window %p.\n", window);
5521 wined3d_unregister_window(window);
5523 if (InterlockedCompareExchangePointer((void **)&device->focus_window, NULL, window) != window)
5524 ERR("Window %p is not the focus window for device %p.\n", window, device);
5526 else if (message == WM_DISPLAYCHANGE)
5528 device->device_parent->ops->mode_changed(device->device_parent);
5532 return CallWindowProcW(proc, window, message, wparam, lparam);
5534 return CallWindowProcA(proc, window, message, wparam, lparam);